2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
12 * AFS vnodeops. The "NOTUSED" #define is used to indicate routines and
13 * calling sequences present in an ops table that we don't actually use.
14 * They are present solely for documentation purposes.
16 * So far the only truly scary part is that Linux relies on the inode cache
17 * to be up to date. Don't you dare break a callback and expect an fstat
18 * to give you meaningful information. This appears to be fixed in the 2.1
19 * development kernels. As it is we can fix this now by intercepting the
23 #include "../afs/param.h"
24 #include "../afs/sysincludes.h"
25 #include "../afs/afsincludes.h"
26 #include "../afs/afs_stats.h"
28 #include "../h/pagemap.h"
29 #if defined(AFS_LINUX24_ENV)
30 #include "../h/smp_lock.h"
34 #define pageoff(pp) pgoff2loff((pp)->index)
36 #define pageoff(pp) pp->offset
39 extern struct vcache *afs_globalVp;
41 extern struct dentry_operations *afs_dops;
42 #if defined(AFS_LINUX24_ENV)
43 extern struct inode_operations afs_file_iops;
44 extern struct address_space_operations afs_file_aops;
45 struct address_space_operations afs_symlink_aops;
47 extern struct inode_operations afs_dir_iops;
48 extern struct inode_operations afs_symlink_iops;
52 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
55 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
59 struct vcache *vcp = (struct vcache*)fp->f_dentry->d_inode;
60 cred_t *credp = crref();
64 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
65 ICL_TYPE_INT32, (int)*offp,
66 ICL_TYPE_INT32, count,
67 ICL_TYPE_INT32, 99999);
69 /* get a validated vcache entry */
70 code = afs_InitReq(&treq, credp);
72 code = afs_VerifyVCache(vcp, &treq);
77 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
79 code = generic_file_read(fp, buf, count, offp);
83 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
84 ICL_TYPE_INT32, (int)*offp,
85 ICL_TYPE_INT32, count,
86 ICL_TYPE_INT32, code);
94 /* Now we have integrated VM for writes as well as reads. generic_file_write
95 * also takes care of re-positioning the pointer if file is open in append
96 * mode. Call fake open/close to ensure we do writes of core dumps.
98 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
103 struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
104 struct vrequest treq;
105 cred_t *credp = crref();
109 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
110 ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
111 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
114 /* get a validated vcache entry */
115 code = (ssize_t)afs_InitReq(&treq, credp);
117 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
119 ObtainWriteLock(&vcp->lock, 529);
121 ReleaseWriteLock(&vcp->lock);
126 code = generic_file_write(fp, buf, count, offp);
130 ObtainWriteLock(&vcp->lock, 530);
131 vcp->m.Date = osi_Time(); /* set modification time */
132 afs_FakeClose(vcp, credp);
134 code2 = afs_DoPartialWrite(vcp, &treq);
135 if (code2 && code >=0)
136 code = (ssize_t) -code2;
137 ReleaseWriteLock(&vcp->lock);
139 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
140 ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
141 ICL_TYPE_INT32, code);
148 /* This is a complete rewrite of afs_readdir, since we can make use of
149 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
150 * handling and use of bulkstats will need to be reflected here as well.
152 static int afs_linux_readdir(struct file *fp,
153 void *dirbuf, filldir_t filldir)
155 extern struct DirEntry * afs_dir_GetBlob();
156 struct vcache *avc = (struct vcache*)FILE_INODE(fp);
157 struct vrequest treq;
158 register struct dcache *tdc;
166 cred_t *credp = crref();
169 AFS_STATCNT(afs_readdir);
171 code = afs_InitReq(&treq, credp);
178 /* update the cache entry */
180 code = afs_VerifyVCache(avc, &treq);
186 /* get a reference to the entire directory */
187 tdc = afs_GetDCache(avc, 0, &treq, &origOffset, &len, 1);
192 ObtainReadLock(&avc->lock);
194 * Make sure that the data in the cache is current. There are two
195 * cases we need to worry about:
196 * 1. The cache data is being fetched by another process.
197 * 2. The cache data is no longer valid
199 while ((avc->states & CStatd)
200 && (tdc->flags & DFFetching)
201 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
202 tdc->flags |= DFWaiting;
203 ReleaseReadLock(&avc->lock);
204 afs_osi_Sleep(&tdc->validPos);
205 ObtainReadLock(&avc->lock);
207 if (!(avc->states & CStatd)
208 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
209 ReleaseReadLock(&avc->lock);
214 /* Fill in until we get an error or we're done. This implementation
215 * takes an offset in units of blobs, rather than bytes.
218 offset = (int)fp->f_pos;
220 dirpos = BlobScan(&tdc->f.inode, offset);
224 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
228 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
229 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
230 len = strlen(de->name);
232 /* filldir returns -EINVAL when the buffer is full. */
233 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
235 unsigned int type=DT_UNKNOWN;
236 struct VenusFid afid;
239 afid.Cell=avc->fid.Cell;
240 afid.Fid.Volume=avc->fid.Fid.Volume;
241 afid.Fid.Vnode=ntohl(de->fid.vnode);
242 afid.Fid.Unique=ntohl(de->fid.vunique);
243 if ((avc->states & CForeign) == 0 &&
244 (ntohl(de->fid.vnode) & 1)) {
246 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
249 } else if (((tvc->states) & (CStatd|CTruth))) {
250 /* CTruth will be set if the object has
255 else if (vtype == VREG)
257 /* Don't do this until we're sure it can't be a mtpt */
258 /* else if (vtype == VLNK)
260 /* what other types does AFS support? */
262 /* clean up from afs_FindVCache */
263 afs_PutVCache(tvc, WRITE_LOCK);
265 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
268 code = (*filldir)(dirbuf, de->name, len, offset, ino);
273 offset = dirpos + 1 + ((len+16)>>5);
275 /* If filldir didn't fill in the last one this is still pointing to that
278 fp->f_pos = (loff_t)offset;
281 ReleaseReadLock(&avc->lock);
287 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
290 /* in afs_pioctl.c */
291 extern int afs_xioctl(struct inode *ip, struct file *fp,
292 unsigned int com, unsigned long arg);
295 /* We need to detect unmap's after close. To do that, we need our own
296 * vm_operations_struct's. And we need to set them up for both the
297 * private and shared mappings. The fun part is that these are all static
298 * so we'll have to initialize on the fly!
300 static struct vm_operations_struct afs_private_mmap_ops;
301 static int afs_private_mmap_ops_inited = 0;
302 static struct vm_operations_struct afs_shared_mmap_ops;
303 static int afs_shared_mmap_ops_inited = 0;
305 void afs_linux_vma_close(struct vm_area_struct *vmap)
313 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
318 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
319 ICL_TYPE_POINTER, vcp,
320 ICL_TYPE_INT32, vcp->mapcnt,
321 ICL_TYPE_INT32, vcp->opens,
322 ICL_TYPE_INT32, vcp->execsOrWriters);
323 ObtainWriteLock(&vcp->lock, 532);
326 ReleaseWriteLock(&vcp->lock);
329 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
330 /* only decrement the execsOrWriters flag if this is not a writable
332 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
333 vcp->execsOrWriters--;
335 vcp->states &= ~CMAPPED;
340 ReleaseWriteLock(&vcp->lock);
347 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
349 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
350 cred_t *credp = crref();
351 struct vrequest treq;
355 #if defined(AFS_LINUX24_ENV)
356 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
357 ICL_TYPE_POINTER, vmap->vm_start,
358 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
360 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
361 ICL_TYPE_POINTER, vmap->vm_start,
362 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
363 ICL_TYPE_INT32, vmap->vm_offset);
366 /* get a validated vcache entry */
367 code = afs_InitReq(&treq, credp);
369 code = afs_VerifyVCache(vcp, &treq);
375 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
378 code = generic_file_mmap(fp, vmap);
383 ObtainWriteLock(&vcp->lock,531);
384 /* Set out vma ops so we catch the close. The following test should be
385 * the same as used in generic_file_mmap.
387 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
388 if (!afs_shared_mmap_ops_inited) {
389 afs_shared_mmap_ops_inited = 1;
390 afs_shared_mmap_ops = *vmap->vm_ops;
391 afs_shared_mmap_ops.close = afs_linux_vma_close;
393 vmap->vm_ops = &afs_shared_mmap_ops;
396 if (!afs_private_mmap_ops_inited) {
397 afs_private_mmap_ops_inited = 1;
398 afs_private_mmap_ops = *vmap->vm_ops;
399 afs_private_mmap_ops.close = afs_linux_vma_close;
401 vmap->vm_ops = &afs_private_mmap_ops;
405 /* Add an open reference on the first mapping. */
406 if (vcp->mapcnt == 0) {
407 vcp->execsOrWriters++;
409 vcp->states |= CMAPPED;
411 ReleaseWriteLock(&vcp->lock);
420 int afs_linux_open(struct inode *ip, struct file *fp)
423 cred_t *credp = crref();
426 #ifdef AFS_LINUX24_ENV
429 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
430 #ifdef AFS_LINUX24_ENV
439 /* afs_Close is called from release, since release is used to handle all
440 * file closings. In addition afs_linux_flush is called from sys_close to
441 * handle flushing the data back to the server. The kicker is that we could
442 * ignore flush completely if only sys_close took it's return value from
443 * fput. See afs_linux_flush for notes on interactions between release and
446 static int afs_linux_release(struct inode *ip, struct file *fp)
449 cred_t *credp = crref();
450 struct vcache *vcp = (struct vcache*)ip;
453 #ifdef AFS_LINUX24_ENV
457 vcp->flushcnt--; /* protected by AFS global lock. */
460 code = afs_close(vcp, fp->f_flags, credp);
462 #ifdef AFS_LINUX24_ENV
471 #if defined(AFS_LINUX24_ENV)
472 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
474 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
478 struct inode *ip = FILE_INODE(fp);
479 cred_t *credp = crref();
482 #ifdef AFS_LINUX24_ENV
485 code = afs_fsync((struct vcache*)ip, credp);
486 #ifdef AFS_LINUX24_ENV
496 /* No support for async i/o */
497 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
499 /* I don't think it will, at least not as can be detected here. */
500 int afs_linux_check_media_change(kdev_t dev);
502 /* Revalidate media and file system. */
503 int afs_linux_file_revalidate(kdev_t dev);
506 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
509 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
510 cred_t *credp = crref();
511 #ifdef AFS_LINUX24_ENV
512 struct flock64 flock;
517 /* Convert to a lock format afs_lockctl understands. */
518 memset((char*)&flock, 0, sizeof(flock));
519 flock.l_type = flp->fl_type;
520 flock.l_pid = flp->fl_pid;
522 flock.l_start = flp->fl_start;
523 flock.l_len = flp->fl_end - flp->fl_start;
526 code = afs_lockctl(vcp, &flock, cmd, credp);
534 * flush is called from sys_close. We could ignore it, but sys_close return
535 * code comes from flush, not release. We need to use release to keep
536 * the vcache open count correct. Note that flush is called before release
537 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
538 * races and also avoid calling afs_close twice when closing the file.
539 * If we merely checked for opens > 0 in afs_linux_release, then if an
540 * new open occurred when storing back the file, afs_linux_release would
541 * incorrectly close the file and decrement the opens count. Calling afs_close
542 * on the just flushed file is wasteful, since the background daemon will
543 * execute the code that finally decides there is nothing to do.
545 int afs_linux_flush(struct file *fp)
547 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
551 /* Only do this on the last close of the file pointer. */
552 #if defined(AFS_LINUX24_ENV)
553 if (atomic_read(&fp->f_count) > 1)
562 code = afs_close(vcp, fp->f_flags, credp);
563 vcp->flushcnt++; /* protected by AFS global lock. */
570 /* Not allowed to directly read a directory. */
571 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
578 #if defined(AFS_LINUX24_ENV)
579 struct file_operations afs_dir_fops = {
580 read: generic_read_dir,
581 readdir: afs_linux_readdir,
583 open: afs_linux_open,
584 release: afs_linux_release,
587 struct file_operations afs_dir_fops = {
588 NULL, /* afs_linux_lseek */
590 NULL, /* afs_linux_write */
592 NULL, /* afs_linux_select */
593 afs_xioctl, /* close enough to use the ported AFS one */
594 NULL, /* afs_linux_mmap */
596 NULL, /* afs_linux_flush */
599 NULL, /* afs_linux_fasync */
600 NULL, /* afs_linux_check_media_change */
601 NULL, /* afs_linux_file_revalidate */
606 #if defined(AFS_LINUX24_ENV)
607 struct file_operations afs_file_fops = {
608 read: afs_linux_read,
609 write: afs_linux_write,
611 mmap: afs_linux_mmap,
612 open: afs_linux_open,
613 flush: afs_linux_flush,
614 release: afs_linux_release,
615 fsync: afs_linux_fsync,
616 lock: afs_linux_lock,
619 struct file_operations afs_file_fops = {
620 NULL, /* afs_linux_lseek */
623 NULL, /* afs_linux_readdir */
624 NULL, /* afs_linux_select */
625 afs_xioctl, /* close enough to use the ported AFS one */
631 NULL, /* afs_linux_fasync */
632 NULL, /* afs_linux_check_media_change */
633 NULL, /* afs_linux_file_revalidate */
639 /**********************************************************************
640 * AFS Linux dentry operations
641 **********************************************************************/
643 /* afs_linux_revalidate
644 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
646 static int afs_linux_revalidate(struct dentry *dp)
650 struct vrequest treq;
651 struct vcache *vcp = (struct vcache*)dp->d_inode;
654 #ifdef AFS_LINUX24_ENV
658 /* Make this a fast path (no crref), since it's called so often. */
659 if (vcp->states & CStatd) {
660 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
661 check_bad_parent(dp); /* check and correct mvid */
663 #ifdef AFS_LINUX24_ENV
671 code = afs_InitReq(&treq, credp);
673 code = afs_VerifyVCache(vcp, &treq);
675 #ifdef AFS_LINUX24_ENV
684 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
685 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
687 static int afs_linux_dentry_revalidate(struct dentry *dp)
692 struct vrequest treq;
693 struct inode *ip = (struct inode *)dp->d_inode;
695 unsigned long timeout = 3*HZ; /* 3 seconds */
697 if (!(flags & LOOKUP_CONTINUE)) {
698 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
704 if (time_after(jiffies, dp->d_time + timeout))
714 /* afs_dentry_iput */
715 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
720 #if defined(AFS_LINUX24_ENV)
721 struct dentry_operations afs_dentry_operations = {
722 d_revalidate: afs_linux_dentry_revalidate,
723 d_iput: afs_dentry_iput,
725 struct dentry_operations *afs_dops = &afs_dentry_operations;
727 struct dentry_operations afs_dentry_operations = {
728 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
730 NULL, /* d_compare */
731 NULL, /* d_delete(struct dentry *) */
732 NULL, /* d_release(struct dentry *) */
733 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
735 struct dentry_operations *afs_dops = &afs_dentry_operations;
738 /**********************************************************************
739 * AFS Linux inode operations
740 **********************************************************************/
744 * Merely need to set enough of vattr to get us through the create. Note
745 * that the higher level code (open_namei) will take care of any tuncation
746 * explicitly. Exclusive open is also taken care of in open_namei.
748 * name is in kernel space at this point.
750 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
753 cred_t *credp = crref();
756 const char *name = dp->d_name.name;
760 vattr.va_mode = mode;
763 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
764 (struct vcache**)&ip, credp);
767 vattr2inode(ip, &vattr);
768 /* Reset ops if symlink or directory. */
769 #if defined(AFS_LINUX24_ENV)
770 if (S_ISREG(ip->i_mode)) {
771 ip->i_op = &afs_file_iops;
772 ip->i_fop = &afs_file_fops;
773 ip->i_data.a_ops = &afs_file_aops;
774 } else if (S_ISDIR(ip->i_mode)) {
775 ip->i_op = &afs_dir_iops;
776 ip->i_fop = &afs_dir_fops;
777 } else if (S_ISLNK(ip->i_mode)) {
778 ip->i_op = &afs_symlink_iops;
779 ip->i_data.a_ops = &afs_symlink_aops;
780 ip->i_mapping = &ip->i_data;
782 printk("afs_linux_create: FIXME\n");
784 if (S_ISDIR(ip->i_mode))
785 ip->i_op = &afs_dir_iops;
786 else if (S_ISLNK(ip->i_mode))
787 ip->i_op = &afs_symlink_iops;
791 dp->d_time = jiffies;
792 d_instantiate(dp, ip);
800 /* afs_linux_lookup */
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
802 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
804 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
808 cred_t *credp = crref();
809 struct vcache *vcp=NULL;
810 const char *comp = dp->d_name.name;
812 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
815 struct inode *ip = (struct inode*)vcp;
816 /* Reset ops if symlink or directory. */
817 #if defined(AFS_LINUX24_ENV)
818 if (S_ISREG(ip->i_mode)) {
819 ip->i_op = &afs_file_iops;
820 ip->i_fop = &afs_file_fops;
821 ip->i_data.a_ops = &afs_file_aops;
822 } else if (S_ISDIR(ip->i_mode)) {
823 ip->i_op = &afs_dir_iops;
824 ip->i_fop = &afs_dir_fops;
825 } else if (S_ISLNK(ip->i_mode)) {
826 ip->i_op = &afs_symlink_iops;
827 ip->i_data.a_ops = &afs_symlink_aops;
828 ip->i_mapping = &ip->i_data;
830 printk("afs_linux_lookup: FIXME\n");
832 if (S_ISDIR(ip->i_mode))
833 ip->i_op = &afs_dir_iops;
834 else if (S_ISLNK(ip->i_mode))
835 ip->i_op = &afs_symlink_iops;
838 dp->d_time = jiffies;
840 d_add(dp, (struct inode*)vcp);
845 /* It's ok for the file to not be found. That's noted by the caller by
846 * seeing that the dp->d_inode field is NULL.
848 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
852 return ERR_PTR(-code);
860 int afs_linux_link(struct dentry *olddp, struct inode *dip,
861 struct dentry *newdp)
864 cred_t *credp = crref();
865 const char *name = newdp->d_name.name;
866 struct inode *oldip = olddp->d_inode;
868 /* If afs_link returned the vnode, we could instantiate the
869 * dentry. Since it's not, we drop this one and do a new lookup.
874 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
881 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
884 cred_t *credp = crref();
885 const char *name = dp->d_name.name;
888 if (!list_empty(&dp->d_hash)) {
890 /* Install a definite non-existence if we're the only user. */
891 #if defined(AFS_LINUX24_ENV)
892 if (atomic_read(&dp->d_count) == 1)
894 if (dp->d_count == 1)
900 code = afs_remove((struct vcache*)dip, name, credp);
905 dp->d_time = jiffies;
906 d_add(dp, NULL); /* means definitely does _not_ exist */
914 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
918 cred_t *credp = crref();
920 const char *name = dp->d_name.name;
922 /* If afs_symlink returned the vnode, we could instantiate the
923 * dentry. Since it's not, we drop this one and do a new lookup.
929 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
935 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
938 cred_t *credp = crref();
939 struct vcache *tvcp = NULL;
941 const char *name = dp->d_name.name;
945 vattr.va_mask = ATTR_MODE;
946 vattr.va_mode = mode;
947 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
950 tvcp->v.v_op = &afs_dir_iops;
951 #if defined(AFS_LINUX24_ENV)
952 tvcp->v.v_fop = &afs_dir_fops;
955 dp->d_time = jiffies;
956 d_instantiate(dp, (struct inode*)tvcp);
963 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
966 cred_t *credp = crref();
967 const char *name = dp->d_name.name;
970 code = afs_rmdir((struct vcache*)dip, name, credp);
972 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
973 * that failed because a directory is not empty. So, we map
974 * EEXIST to ENOTEMPTY on linux.
976 if (code == EEXIST) {
991 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
992 struct inode *newip, struct dentry *newdp)
995 cred_t *credp = crref();
996 const char *oldname = olddp->d_name.name;
997 const char *newname = newdp->d_name.name;
999 /* Remove old and new entries from name hash. New one will change below.
1000 * While it's optimal to catch failures and re-insert newdp into hash,
1001 * it's also error prone and in that case we're already dealing with error
1002 * cases. Let another lookup put things right, if need be.
1004 if (!list_empty(&olddp->d_hash)) {
1007 if (!list_empty(&newdp->d_hash)) {
1011 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1016 /* update time so it doesn't expire immediately */
1017 newdp->d_time = jiffies;
1018 d_move(olddp, newdp);
1026 /* afs_linux_ireadlink
1027 * Internal readlink which can return link contents to user or kernel space.
1028 * Note that the buffer is NOT supposed to be null-terminated.
1030 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1034 cred_t *credp = crref();
1038 setup_uio(&tuio, &iov, target, 0, maxlen, UIO_READ, seg);
1039 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1043 return maxlen - tuio.uio_resid;
1048 #if !defined(AFS_LINUX24_ENV)
1049 /* afs_linux_readlink
1050 * Fill target (which is in user space) with contents of symlink.
1052 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1055 struct inode *ip = dp->d_inode;
1058 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1064 /* afs_linux_follow_link
1065 * a file system dependent link following routine.
1067 struct dentry * afs_linux_follow_link(struct dentry *dp,
1068 struct dentry *basep,
1069 unsigned int follow)
1076 name = osi_Alloc(PATH_MAX+1);
1080 return ERR_PTR(-EIO);
1083 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1088 res = ERR_PTR(code);
1092 res = lookup_dentry(name, basep, follow);
1096 osi_Free(name, PATH_MAX+1);
1102 /* afs_linux_readpage
1103 * all reads come through here. A strategy-like read call.
1105 int afs_linux_readpage(struct file *fp, struct page *pp)
1108 cred_t *credp = crref();
1109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1111 loff_t offset = pp->index << PAGE_CACHE_SHIFT;
1113 ulong address = afs_linux_page_address(pp);
1114 loff_t offset = pageoff(pp);
1118 struct inode *ip = FILE_INODE(fp);
1119 int cnt = atomic_read(&pp->count);
1122 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1123 ICL_TYPE_POINTER, ip,
1124 ICL_TYPE_POINTER, pp,
1125 ICL_TYPE_INT32, cnt,
1126 ICL_TYPE_INT32, 99999); /* not a possible code value */
1128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1134 atomic_add(1, &pp->count);
1135 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1136 clear_bit(PG_error, &pp->flags);
1139 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1140 UIO_READ, AFS_UIOSYS);
1141 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1142 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1147 if (tuio.uio_resid) /* zero remainder of page */
1148 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1150 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1151 flush_dcache_page(pp);
1152 SetPageUptodate(pp);
1154 set_bit(PG_uptodate, &pp->flags);
1158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1162 clear_bit(PG_locked, &pp->flags);
1168 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1169 ICL_TYPE_POINTER, ip,
1170 ICL_TYPE_POINTER, pp,
1171 ICL_TYPE_INT32, cnt,
1172 ICL_TYPE_INT32, code);
1177 #if defined(AFS_LINUX24_ENV)
1178 int afs_linux_writepage(struct page *pp)
1180 struct address_space *mapping = pp->mapping;
1181 struct inode *inode;
1182 unsigned long end_index;
1183 unsigned offset = PAGE_CACHE_SIZE;
1186 inode = (struct inode *) mapping->host;
1187 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1190 if (pp->index < end_index)
1192 /* things got complicated... */
1193 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1194 /* OK, are we completely out? */
1195 if (pp->index >= end_index+1 || !offset)
1199 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1201 SetPageUptodate(pp);
1203 if (status == offset)
1211 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1212 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1214 /* afs_linux_truncate
1215 * Handles discarding disk blocks if this were a device. ext2 indicates we
1216 * may need to zero partial last pages of memory mapped files.
1218 void afs_linux_truncate(struct inode *ip)
1223 /* afs_linux_permission
1224 * Check access rights - returns error if can't check or permission denied.
1226 int afs_linux_permission(struct inode *ip, int mode)
1229 cred_t *credp = crref();
1233 if (mode & MAY_EXEC) tmp |= VEXEC;
1234 if (mode & MAY_READ) tmp |= VREAD;
1235 if (mode & MAY_WRITE) tmp |= VWRITE;
1236 code = afs_access((struct vcache*)ip, tmp, credp);
1245 /* msdos sector mapping hack for memory mapping. */
1246 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1249 #if defined(AFS_LINUX24_ENV)
1250 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1251 unsigned long offset,
1254 struct vcache *vcp = (struct vcache *) ip;
1263 buffer = kmap(pp) + offset;
1264 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1267 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1268 ICL_TYPE_POINTER, pp,
1269 ICL_TYPE_INT32, atomic_read(&pp->count),
1270 ICL_TYPE_INT32, 99999);
1271 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1273 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1277 code = code ? -code : count - tuio.uio_resid;
1278 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1279 ICL_TYPE_POINTER, pp,
1280 ICL_TYPE_INT32, atomic_read(&pp->count),
1281 ICL_TYPE_INT32, code);
1290 afs_linux_updatepage(struct file *file, struct page *page,
1291 unsigned long offset, unsigned int count)
1293 struct dentry *dentry = file->f_dentry;
1295 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1298 /* afs_linux_updatepage
1299 * What one would have thought was writepage - write dirty page to file.
1300 * Called from generic_file_write. buffer is still in user space. pagep
1301 * has been filled in with old data if we're updating less than a page.
1303 int afs_linux_updatepage(struct file *fp, struct page *pp,
1304 unsigned long offset,
1305 unsigned int count, int sync)
1307 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1308 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1314 set_bit(PG_locked, &pp->flags);
1318 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1319 ICL_TYPE_POINTER, pp,
1320 ICL_TYPE_INT32, atomic_read(&pp->count),
1321 ICL_TYPE_INT32, 99999);
1322 setup_uio(&tuio, &iovec, page_addr + offset, pageoff(pp) + offset, count,
1323 UIO_WRITE, AFS_UIOSYS);
1325 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1329 code = code ? -code : count - tuio.uio_resid;
1330 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1331 ICL_TYPE_POINTER, pp,
1332 ICL_TYPE_INT32, atomic_read(&pp->count),
1333 ICL_TYPE_INT32, code);
1338 clear_bit(PG_locked, &pp->flags);
1343 #if defined(AFS_LINUX24_ENV)
1344 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1350 code = afs_linux_updatepage(file, page, offset, to-offset);
1358 static int afs_linux_prepare_write(struct file *file, struct page *page,
1359 unsigned from, unsigned to)
1365 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1368 #if defined(AFS_LINUX24_ENV)
1369 struct inode_operations afs_file_iops = {
1370 revalidate: afs_linux_revalidate,
1371 setattr: afs_notify_change,
1372 permission: afs_linux_permission,
1374 struct address_space_operations afs_file_aops = {
1375 readpage: afs_linux_readpage,
1376 writepage: afs_linux_writepage,
1377 commit_write: afs_linux_commit_write,
1378 prepare_write: afs_linux_prepare_write,
1381 struct inode_operations *afs_ops = &afs_file_iops;
1383 struct inode_operations afs_iops = {
1384 &afs_file_fops, /* file operations */
1385 NULL, /* afs_linux_create */
1386 NULL, /* afs_linux_lookup */
1387 NULL, /* afs_linux_link */
1388 NULL, /* afs_linux_unlink */
1389 NULL, /* afs_linux_symlink */
1390 NULL, /* afs_linux_mkdir */
1391 NULL, /* afs_linux_rmdir */
1392 NULL, /* afs_linux_mknod */
1393 NULL, /* afs_linux_rename */
1394 NULL, /* afs_linux_readlink */
1395 NULL, /* afs_linux_follow_link */
1397 NULL, /* afs_linux_writepage */
1398 NULL, /* afs_linux_bmap */
1399 NULL, /* afs_linux_truncate */
1400 afs_linux_permission,
1401 NULL, /* afs_linux_smap */
1402 afs_linux_updatepage,
1403 afs_linux_revalidate,
1406 struct inode_operations *afs_ops = &afs_iops;
1409 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1410 * by what sort of operation is allowed.....
1412 #if defined(AFS_LINUX24_ENV)
1413 struct inode_operations afs_dir_iops = {
1414 create: afs_linux_create,
1415 lookup: afs_linux_lookup,
1416 link: afs_linux_link,
1417 unlink: afs_linux_unlink,
1418 symlink: afs_linux_symlink,
1419 mkdir: afs_linux_mkdir,
1420 rmdir: afs_linux_rmdir,
1421 rename: afs_linux_rename,
1422 revalidate: afs_linux_revalidate,
1423 setattr: afs_notify_change,
1424 permission: afs_linux_permission,
1427 struct inode_operations afs_dir_iops = {
1428 &afs_dir_fops, /* file operations for directories */
1436 NULL, /* afs_linux_mknod */
1438 NULL, /* afs_linux_readlink */
1439 NULL, /* afs_linux_follow_link */
1440 NULL, /* afs_linux_readpage */
1441 NULL, /* afs_linux_writepage */
1442 NULL, /* afs_linux_bmap */
1443 NULL, /* afs_linux_truncate */
1444 afs_linux_permission,
1445 NULL, /* afs_linux_smap */
1446 NULL, /* afs_linux_updatepage */
1447 afs_linux_revalidate,
1451 /* We really need a separate symlink set of ops, since do_follow_link()
1452 * determines if it _is_ a link by checking if the follow_link op is set.
1454 #if defined(AFS_LINUX24_ENV)
1455 static int afs_symlink_filler(struct file *file, struct page *page)
1457 struct inode *ip = (struct inode *) page->mapping->host;
1458 char *p = (char *)kmap(page);
1463 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1467 p[code] = '\0'; /* null terminate? */
1471 SetPageUptodate(page);
1486 struct address_space_operations afs_symlink_aops = {
1487 readpage: afs_symlink_filler
1490 struct inode_operations afs_symlink_iops = {
1491 readlink: page_readlink,
1492 follow_link: page_follow_link,
1493 setattr: afs_notify_change,
1496 struct inode_operations afs_symlink_iops = {
1497 NULL, /* file operations */
1505 NULL, /* afs_linux_mknod */
1508 afs_linux_follow_link,
1509 NULL, /* readpage */
1510 NULL, /* afs_linux_writepage */
1511 NULL, /* afs_linux_bmap */
1512 NULL, /* afs_linux_truncate */
1513 afs_linux_permission, /* tho the code appears to indicate not used? */
1514 NULL, /* afs_linux_smap */
1515 NULL, /* updatepage */
1516 afs_linux_revalidate, /* tho the code appears to indicate not used? */