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 <afsconfig.h>
24 #include "../afs/param.h"
28 #include "../afs/sysincludes.h"
29 #include "../afs/afsincludes.h"
30 #include "../afs/afs_stats.h"
31 #include "../afs/afs_osidnlc.h"
33 #include "../h/pagemap.h"
34 #if defined(AFS_LINUX24_ENV)
35 #include "../h/smp_lock.h"
39 #define pageoff(pp) pgoff2loff((pp)->index)
41 #define pageoff(pp) pp->offset
44 extern struct vcache *afs_globalVp;
45 extern afs_rwlock_t afs_xvcache;
47 extern struct dentry_operations *afs_dops;
48 #if defined(AFS_LINUX24_ENV)
49 extern struct inode_operations afs_file_iops;
50 extern struct address_space_operations afs_file_aops;
51 struct address_space_operations afs_symlink_aops;
53 extern struct inode_operations afs_dir_iops;
54 extern struct inode_operations afs_symlink_iops;
58 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
61 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
65 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
66 cred_t *credp = crref();
70 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
71 ICL_TYPE_OFFSET, offp,
72 ICL_TYPE_INT32, count,
73 ICL_TYPE_INT32, 99999);
75 /* get a validated vcache entry */
76 code = afs_InitReq(&treq, credp);
78 code = afs_VerifyVCache(vcp, &treq);
83 #ifdef AFS_64BIT_CLIENT
84 if (*offp + count > afs_vmMappingEnd) {
87 afs_size_t oldOffset = *offp;
90 if (*offp < afs_vmMappingEnd) {
91 /* special case of a buffer crossing the VM mapping end */
92 afs_int32 tcount = afs_vmMappingEnd - *offp;
94 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
96 code = generic_file_read(fp, buf, tcount, offp);
103 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
104 UIO_READ, AFS_UIOSYS);
105 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
106 xfered += count - tuio.uio_resid;
109 *offp += count - tuio.uio_resid;
116 #endif /* AFS_64BIT_CLIENT */
117 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
119 code = generic_file_read(fp, buf, count, offp);
121 #ifdef AFS_64BIT_CLIENT
123 #endif /* AFS_64BIT_CLIENT */
126 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
127 ICL_TYPE_OFFSET, offp,
128 ICL_TYPE_INT32, count,
129 ICL_TYPE_INT32, code);
137 /* Now we have integrated VM for writes as well as reads. generic_file_write
138 * also takes care of re-positioning the pointer if file is open in append
139 * mode. Call fake open/close to ensure we do writes of core dumps.
141 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
146 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
147 struct vrequest treq;
148 cred_t *credp = crref();
152 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
153 ICL_TYPE_OFFSET, offp,
154 ICL_TYPE_INT32, count,
155 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
158 /* get a validated vcache entry */
159 code = (ssize_t)afs_InitReq(&treq, credp);
161 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
163 ObtainWriteLock(&vcp->lock, 529);
165 ReleaseWriteLock(&vcp->lock);
169 #ifdef AFS_64BIT_CLIENT
170 if (*offp + count > afs_vmMappingEnd) {
173 afs_size_t oldOffset = *offp;
174 afs_int32 xfered = 0;
176 if (*offp < afs_vmMappingEnd) {
177 /* special case of a buffer crossing the VM mapping end */
178 afs_int32 tcount = afs_vmMappingEnd - *offp;
181 code = generic_file_write(fp, buf, tcount, offp);
183 if (code != tcount) {
188 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
189 UIO_WRITE, AFS_UIOSYS);
190 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
191 xfered += count - tuio.uio_resid;
194 *offp += count - tuio.uio_resid;
196 /* Purge dirty chunks of file if there are too many dirty chunks.
197 * Inside the write loop, we only do this at a chunk boundary.
198 * Clean up partial chunk if necessary at end of loop.
200 if (AFS_CHUNKBASE(tuio.afsio_offset) != AFS_CHUNKBASE(oldOffset)) {
201 ObtainWriteLock(&vcp->lock,402);
202 code = afs_DoPartialWrite(vcp, &treq);
203 vcp->states |= CDirty;
204 ReleaseWriteLock(&vcp->lock);
208 ObtainWriteLock(&vcp->lock,400);
209 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
211 if (*offp > vcp->m.Length) {
212 vcp->m.Length = *offp;
214 ReleaseWriteLock(&vcp->lock);
218 #endif /* AFS_64BIT_CLIENT */
220 code = generic_file_write(fp, buf, count, offp);
222 #ifdef AFS_64BIT_CLIENT
224 #endif /* AFS_64BIT_CLIENT */
227 ObtainWriteLock(&vcp->lock, 530);
228 vcp->m.Date = osi_Time(); /* set modification time */
229 afs_FakeClose(vcp, credp);
231 code2 = afs_DoPartialWrite(vcp, &treq);
232 if (code2 && code >=0)
233 code = (ssize_t) -code2;
234 ReleaseWriteLock(&vcp->lock);
236 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
237 ICL_TYPE_OFFSET, offp,
238 ICL_TYPE_INT32, count,
239 ICL_TYPE_INT32, code);
246 /* This is a complete rewrite of afs_readdir, since we can make use of
247 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
248 * handling and use of bulkstats will need to be reflected here as well.
250 static int afs_linux_readdir(struct file *fp,
251 void *dirbuf, filldir_t filldir)
253 extern struct DirEntry * afs_dir_GetBlob();
254 struct vcache *avc = ITOAFS(FILE_INODE(fp));
255 struct vrequest treq;
256 register struct dcache *tdc;
263 afs_size_t origOffset, tlen;
264 cred_t *credp = crref();
265 struct afs_fakestat_state fakestat;
268 AFS_STATCNT(afs_readdir);
270 code = afs_InitReq(&treq, credp);
277 afs_InitFakeStat(&fakestat);
278 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
280 afs_PutFakeStat(&fakestat);
285 /* update the cache entry */
287 code = afs_VerifyVCache(avc, &treq);
289 afs_PutFakeStat(&fakestat);
294 /* get a reference to the entire directory */
295 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
298 afs_PutFakeStat(&fakestat);
302 ObtainReadLock(&avc->lock);
303 ObtainReadLock(&tdc->lock);
305 * Make sure that the data in the cache is current. There are two
306 * cases we need to worry about:
307 * 1. The cache data is being fetched by another process.
308 * 2. The cache data is no longer valid
310 while ((avc->states & CStatd)
311 && (tdc->dflags & DFFetching)
312 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
313 ReleaseReadLock(&tdc->lock);
314 ReleaseReadLock(&avc->lock);
315 afs_osi_Sleep(&tdc->validPos);
316 ObtainReadLock(&avc->lock);
317 ObtainReadLock(&tdc->lock);
319 if (!(avc->states & CStatd)
320 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
321 ReleaseReadLock(&tdc->lock);
322 ReleaseReadLock(&avc->lock);
327 /* Fill in until we get an error or we're done. This implementation
328 * takes an offset in units of blobs, rather than bytes.
331 offset = (int)fp->f_pos;
333 dirpos = BlobScan(&tdc->f.inode, offset);
337 de = afs_dir_GetBlob(&tdc->f.inode, dirpos);
341 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
342 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
343 len = strlen(de->name);
345 /* filldir returns -EINVAL when the buffer is full. */
346 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
348 unsigned int type=DT_UNKNOWN;
349 struct VenusFid afid;
352 afid.Cell=avc->fid.Cell;
353 afid.Fid.Volume=avc->fid.Fid.Volume;
354 afid.Fid.Vnode=ntohl(de->fid.vnode);
355 afid.Fid.Unique=ntohl(de->fid.vunique);
356 if ((avc->states & CForeign) == 0 &&
357 (ntohl(de->fid.vnode) & 1)) {
359 } else if ((tvc=afs_FindVCache(&afid,0,0))) {
362 } else if (((tvc->states) & (CStatd|CTruth))) {
363 /* CTruth will be set if the object has
368 else if (vtype == VREG)
370 /* Don't do this until we're sure it can't be a mtpt */
371 /* else if (vtype == VLNK)
373 /* what other types does AFS support? */
375 /* clean up from afs_FindVCache */
378 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
381 code = (*filldir)(dirbuf, de->name, len, offset, ino);
386 offset = dirpos + 1 + ((len+16)>>5);
388 /* If filldir didn't fill in the last one this is still pointing to that
391 fp->f_pos = (loff_t)offset;
393 ReleaseReadLock(&tdc->lock);
395 ReleaseReadLock(&avc->lock);
396 afs_PutFakeStat(&fakestat);
402 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
405 /* in afs_pioctl.c */
406 extern int afs_xioctl(struct inode *ip, struct file *fp,
407 unsigned int com, unsigned long arg);
410 /* We need to detect unmap's after close. To do that, we need our own
411 * vm_operations_struct's. And we need to set them up for both the
412 * private and shared mappings. The fun part is that these are all static
413 * so we'll have to initialize on the fly!
415 static struct vm_operations_struct afs_private_mmap_ops;
416 static int afs_private_mmap_ops_inited = 0;
417 static struct vm_operations_struct afs_shared_mmap_ops;
418 static int afs_shared_mmap_ops_inited = 0;
420 void afs_linux_vma_close(struct vm_area_struct *vmap)
428 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
433 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
434 ICL_TYPE_POINTER, vcp,
435 ICL_TYPE_INT32, vcp->mapcnt,
436 ICL_TYPE_INT32, vcp->opens,
437 ICL_TYPE_INT32, vcp->execsOrWriters);
438 ObtainWriteLock(&vcp->lock, 532);
441 ReleaseWriteLock(&vcp->lock);
444 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
445 /* only decrement the execsOrWriters flag if this is not a writable
447 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
448 vcp->execsOrWriters--;
450 vcp->states &= ~CMAPPED;
455 ReleaseWriteLock(&vcp->lock);
462 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
464 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
465 cred_t *credp = crref();
466 struct vrequest treq;
470 #if defined(AFS_LINUX24_ENV)
471 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
472 ICL_TYPE_POINTER, vmap->vm_start,
473 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
475 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
476 ICL_TYPE_POINTER, vmap->vm_start,
477 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
478 ICL_TYPE_INT32, vmap->vm_offset);
481 /* get a validated vcache entry */
482 code = afs_InitReq(&treq, credp);
484 code = afs_VerifyVCache(vcp, &treq);
490 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
493 code = generic_file_mmap(fp, vmap);
498 ObtainWriteLock(&vcp->lock,531);
499 /* Set out vma ops so we catch the close. The following test should be
500 * the same as used in generic_file_mmap.
502 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
503 if (!afs_shared_mmap_ops_inited) {
504 afs_shared_mmap_ops_inited = 1;
505 afs_shared_mmap_ops = *vmap->vm_ops;
506 afs_shared_mmap_ops.close = afs_linux_vma_close;
508 vmap->vm_ops = &afs_shared_mmap_ops;
511 if (!afs_private_mmap_ops_inited) {
512 afs_private_mmap_ops_inited = 1;
513 afs_private_mmap_ops = *vmap->vm_ops;
514 afs_private_mmap_ops.close = afs_linux_vma_close;
516 vmap->vm_ops = &afs_private_mmap_ops;
520 /* Add an open reference on the first mapping. */
521 if (vcp->mapcnt == 0) {
522 vcp->execsOrWriters++;
524 vcp->states |= CMAPPED;
526 ReleaseWriteLock(&vcp->lock);
535 int afs_linux_open(struct inode *ip, struct file *fp)
538 cred_t *credp = crref();
541 #ifdef AFS_LINUX24_ENV
544 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
545 #ifdef AFS_LINUX24_ENV
554 /* afs_Close is called from release, since release is used to handle all
555 * file closings. In addition afs_linux_flush is called from sys_close to
556 * handle flushing the data back to the server. The kicker is that we could
557 * ignore flush completely if only sys_close took it's return value from
558 * fput. See afs_linux_flush for notes on interactions between release and
561 static int afs_linux_release(struct inode *ip, struct file *fp)
564 cred_t *credp = crref();
565 struct vcache *vcp = ITOAFS(ip);
568 #ifdef AFS_LINUX24_ENV
572 vcp->flushcnt--; /* protected by AFS global lock. */
575 code = afs_close(vcp, fp->f_flags, credp);
577 #ifdef AFS_LINUX24_ENV
586 #if defined(AFS_LINUX24_ENV)
587 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
589 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
593 struct inode *ip = FILE_INODE(fp);
594 cred_t *credp = crref();
597 #ifdef AFS_LINUX24_ENV
600 code = afs_fsync(ITOAFS(ip), credp);
601 #ifdef AFS_LINUX24_ENV
611 /* No support for async i/o */
612 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
614 /* I don't think it will, at least not as can be detected here. */
615 int afs_linux_check_media_change(kdev_t dev);
617 /* Revalidate media and file system. */
618 int afs_linux_file_revalidate(kdev_t dev);
621 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
624 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
625 cred_t *credp = crref();
626 #ifdef AFS_LINUX24_ENV
627 struct flock64 flock;
632 /* Convert to a lock format afs_lockctl understands. */
633 memset((char*)&flock, 0, sizeof(flock));
634 flock.l_type = flp->fl_type;
635 flock.l_pid = flp->fl_pid;
637 flock.l_start = flp->fl_start;
638 flock.l_len = flp->fl_end - flp->fl_start;
641 code = afs_lockctl(vcp, &flock, cmd, credp);
644 /* Convert flock back to Linux's file_lock */
645 flp->fl_type = flock.l_type;
646 flp->fl_pid = flock.l_pid;
647 flp->fl_start = flock.l_start;
648 flp->fl_end = flock.l_start + flock.l_len;
656 * flush is called from sys_close. We could ignore it, but sys_close return
657 * code comes from flush, not release. We need to use release to keep
658 * the vcache open count correct. Note that flush is called before release
659 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
660 * races and also avoid calling afs_close twice when closing the file.
661 * If we merely checked for opens > 0 in afs_linux_release, then if an
662 * new open occurred when storing back the file, afs_linux_release would
663 * incorrectly close the file and decrement the opens count. Calling afs_close
664 * on the just flushed file is wasteful, since the background daemon will
665 * execute the code that finally decides there is nothing to do.
667 int afs_linux_flush(struct file *fp)
669 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
673 /* Only do this on the last close of the file pointer. */
674 #if defined(AFS_LINUX24_ENV)
675 if (atomic_read(&fp->f_count) > 1)
684 code = afs_close(vcp, fp->f_flags, credp);
685 vcp->flushcnt++; /* protected by AFS global lock. */
692 /* Not allowed to directly read a directory. */
693 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
700 #if defined(AFS_LINUX24_ENV)
701 struct file_operations afs_dir_fops = {
702 read: generic_read_dir,
703 readdir: afs_linux_readdir,
705 open: afs_linux_open,
706 release: afs_linux_release,
709 struct file_operations afs_dir_fops = {
710 NULL, /* afs_linux_lseek */
712 NULL, /* afs_linux_write */
714 NULL, /* afs_linux_select */
715 afs_xioctl, /* close enough to use the ported AFS one */
716 NULL, /* afs_linux_mmap */
718 NULL, /* afs_linux_flush */
721 NULL, /* afs_linux_fasync */
722 NULL, /* afs_linux_check_media_change */
723 NULL, /* afs_linux_file_revalidate */
728 #if defined(AFS_LINUX24_ENV)
729 struct file_operations afs_file_fops = {
730 read: afs_linux_read,
731 write: afs_linux_write,
733 mmap: afs_linux_mmap,
734 open: afs_linux_open,
735 flush: afs_linux_flush,
736 release: afs_linux_release,
737 fsync: afs_linux_fsync,
738 lock: afs_linux_lock,
741 struct file_operations afs_file_fops = {
742 NULL, /* afs_linux_lseek */
745 NULL, /* afs_linux_readdir */
746 NULL, /* afs_linux_select */
747 afs_xioctl, /* close enough to use the ported AFS one */
753 NULL, /* afs_linux_fasync */
754 NULL, /* afs_linux_check_media_change */
755 NULL, /* afs_linux_file_revalidate */
761 /**********************************************************************
762 * AFS Linux dentry operations
763 **********************************************************************/
765 /* afs_linux_revalidate
766 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
768 static int afs_linux_revalidate(struct dentry *dp)
772 struct vrequest treq;
773 struct vcache *vcp = ITOAFS(dp->d_inode);
774 struct vcache *rootvp = NULL;
778 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid &&
779 (vcp->states & CMValid) && (vcp->states & CStatd)) {
780 ObtainSharedLock(&afs_xvcache, 680);
781 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
782 ReleaseSharedLock(&afs_xvcache);
785 #ifdef AFS_LINUX24_ENV
789 /* Make this a fast path (no crref), since it's called so often. */
790 if (vcp->states & CStatd) {
791 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
792 check_bad_parent(dp); /* check and correct mvid */
794 vcache2fakeinode(rootvp, vcp);
797 #ifdef AFS_LINUX24_ENV
800 if (rootvp) afs_PutVCache(rootvp);
806 code = afs_InitReq(&treq, credp);
808 code = afs_VerifyVCache(vcp, &treq);
810 #ifdef AFS_LINUX24_ENV
820 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
821 * In kernels 2.2.10 and above, we are passed an additional flags var which
822 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
823 * we are advised to follow the entry if it is a link or to make sure that
824 * it is a directory. But since the kernel itself checks these possibilities
825 * later on, we shouldn't have to do it until later. Perhaps in the future..
827 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
828 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
830 static int afs_linux_dentry_revalidate(struct dentry *dp)
834 cred_t *credp = crref();
835 struct vrequest treq;
836 struct vcache *lookupvcp = NULL;
837 int code, bad_dentry = 1;
838 struct sysname_info sysState;
839 struct vcache *vcp = ITOAFS(dp->d_inode);
840 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
845 sysState.allocked = 0;
847 /* If it's a negative dentry, then there's nothing to do. */
848 if (!vcp || !parentvcp)
851 /* If it is the AFS root, then there's no chance it needs
853 if (vcp == afs_globalVp) {
858 if ((code = afs_InitReq(&treq, credp)))
861 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
862 name = sysState.name;
864 /* First try looking up the DNLC */
865 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
866 /* Verify that the dentry does not point to an old inode */
867 if (vcp != lookupvcp)
869 /* Check and correct mvid */
870 if (*name != '/' && vcp->mvstat == 2)
871 check_bad_parent(dp);
877 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
878 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
880 /* Verify that the dentry does not point to an old inode */
881 if (vcp != lookupvcp)
889 afs_PutVCache(lookupvcp);
890 if (sysState.allocked)
891 osi_FreeLargeSpace(name);
897 shrink_dcache_parent(dp);
906 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
907 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
909 static int afs_linux_dentry_revalidate(struct dentry *dp)
914 struct vrequest treq;
915 struct inode *ip = AFSTOI(dp->d_inode);
917 unsigned long timeout = 3*HZ; /* 3 seconds */
920 printk("negative dentry: %s\n", dp->d_name.name);
922 if (!(flags & LOOKUP_CONTINUE)) {
923 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
929 if (time_after(jiffies, dp->d_time + timeout))
940 /* afs_dentry_iput */
941 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
943 if (ICL_SETACTIVE(afs_iclSetp)) {
945 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT,
946 ICL_TYPE_POINTER, ip,
947 ICL_TYPE_STRING, dp->d_parent->d_name.name,
948 ICL_TYPE_STRING, dp->d_name.name);
955 static int afs_dentry_delete(struct dentry *dp)
957 if (ICL_SETACTIVE(afs_iclSetp)) {
959 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
960 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
961 ICL_TYPE_STRING, dp->d_name.name);
965 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
966 return 1; /* bad inode? */
971 #if defined(AFS_LINUX24_ENV)
972 struct dentry_operations afs_dentry_operations = {
973 d_revalidate: afs_linux_dentry_revalidate,
974 d_iput: afs_dentry_iput,
975 d_delete: afs_dentry_delete,
977 struct dentry_operations *afs_dops = &afs_dentry_operations;
979 struct dentry_operations afs_dentry_operations = {
980 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
982 NULL, /* d_compare */
983 afs_dentry_delete, /* d_delete(struct dentry *) */
984 NULL, /* d_release(struct dentry *) */
985 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
987 struct dentry_operations *afs_dops = &afs_dentry_operations;
990 /**********************************************************************
991 * AFS Linux inode operations
992 **********************************************************************/
996 * Merely need to set enough of vattr to get us through the create. Note
997 * that the higher level code (open_namei) will take care of any tuncation
998 * explicitly. Exclusive open is also taken care of in open_namei.
1000 * name is in kernel space at this point.
1002 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1005 cred_t *credp = crref();
1008 const char *name = dp->d_name.name;
1012 vattr.va_mode = mode;
1015 code = afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
1016 (struct vcache**)&ip, credp);
1019 vattr2inode(ip, &vattr);
1020 /* Reset ops if symlink or directory. */
1021 #if defined(AFS_LINUX24_ENV)
1022 if (S_ISREG(ip->i_mode)) {
1023 ip->i_op = &afs_file_iops;
1024 ip->i_fop = &afs_file_fops;
1025 ip->i_data.a_ops = &afs_file_aops;
1026 } else if (S_ISDIR(ip->i_mode)) {
1027 ip->i_op = &afs_dir_iops;
1028 ip->i_fop = &afs_dir_fops;
1029 } else if (S_ISLNK(ip->i_mode)) {
1030 ip->i_op = &afs_symlink_iops;
1031 ip->i_data.a_ops = &afs_symlink_aops;
1032 ip->i_mapping = &ip->i_data;
1034 printk("afs_linux_create: FIXME\n");
1036 if (S_ISDIR(ip->i_mode))
1037 ip->i_op = &afs_dir_iops;
1038 else if (S_ISLNK(ip->i_mode))
1039 ip->i_op = &afs_symlink_iops;
1042 dp->d_op = afs_dops;
1043 dp->d_time = jiffies;
1044 d_instantiate(dp, ip);
1052 /* afs_linux_lookup */
1053 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1054 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
1056 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
1060 cred_t *credp = crref();
1061 struct vcache *vcp=NULL;
1062 const char *comp = dp->d_name.name;
1064 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1067 struct inode *ip = AFSTOI(vcp);
1068 /* Reset ops if symlink or directory. */
1069 #if defined(AFS_LINUX24_ENV)
1070 if (S_ISREG(ip->i_mode)) {
1071 ip->i_op = &afs_file_iops;
1072 ip->i_fop = &afs_file_fops;
1073 ip->i_data.a_ops = &afs_file_aops;
1074 } else if (S_ISDIR(ip->i_mode)) {
1075 ip->i_op = &afs_dir_iops;
1076 ip->i_fop = &afs_dir_fops;
1077 } else if (S_ISLNK(ip->i_mode)) {
1078 ip->i_op = &afs_symlink_iops;
1079 ip->i_data.a_ops = &afs_symlink_aops;
1080 ip->i_mapping = &ip->i_data;
1082 printk("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n", ip->i_mode, dp->d_name.name, code);
1084 if (S_ISDIR(ip->i_mode))
1085 ip->i_op = &afs_dir_iops;
1086 else if (S_ISLNK(ip->i_mode))
1087 ip->i_op = &afs_symlink_iops;
1090 dp->d_time = jiffies;
1091 dp->d_op = afs_dops;
1092 d_add(dp, AFSTOI(vcp));
1097 /* It's ok for the file to not be found. That's noted by the caller by
1098 * seeing that the dp->d_inode field is NULL.
1100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1104 return ERR_PTR(-code);
1112 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1113 struct dentry *newdp)
1116 cred_t *credp = crref();
1117 const char *name = newdp->d_name.name;
1118 struct inode *oldip = olddp->d_inode;
1120 /* If afs_link returned the vnode, we could instantiate the
1121 * dentry. Since it's not, we drop this one and do a new lookup.
1126 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1133 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1136 cred_t *credp = crref();
1137 const char *name = dp->d_name.name;
1140 code = afs_remove(ITOAFS(dip), name, credp);
1149 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1153 cred_t *credp = crref();
1155 const char *name = dp->d_name.name;
1157 /* If afs_symlink returned the vnode, we could instantiate the
1158 * dentry. Since it's not, we drop this one and do a new lookup.
1164 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1170 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1173 cred_t *credp = crref();
1174 struct vcache *tvcp = NULL;
1176 const char *name = dp->d_name.name;
1180 vattr.va_mask = ATTR_MODE;
1181 vattr.va_mode = mode;
1182 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1185 tvcp->v.v_op = &afs_dir_iops;
1186 #if defined(AFS_LINUX24_ENV)
1187 tvcp->v.v_fop = &afs_dir_fops;
1189 dp->d_op = afs_dops;
1190 dp->d_time = jiffies;
1191 d_instantiate(dp, AFSTOI(tvcp));
1199 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1202 cred_t *credp = crref();
1203 const char *name = dp->d_name.name;
1206 code = afs_rmdir(ITOAFS(dip), name, credp);
1208 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1209 * that failed because a directory is not empty. So, we map
1210 * EEXIST to ENOTEMPTY on linux.
1212 if (code == EEXIST) {
1227 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1228 struct inode *newip, struct dentry *newdp)
1231 cred_t *credp = crref();
1232 const char *oldname = olddp->d_name.name;
1233 const char *newname = newdp->d_name.name;
1235 /* Remove old and new entries from name hash. New one will change below.
1236 * While it's optimal to catch failures and re-insert newdp into hash,
1237 * it's also error prone and in that case we're already dealing with error
1238 * cases. Let another lookup put things right, if need be.
1240 if (!list_empty(&olddp->d_hash)) {
1243 if (!list_empty(&newdp->d_hash)) {
1247 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip),
1252 /* update time so it doesn't expire immediately */
1253 newdp->d_time = jiffies;
1254 d_move(olddp, newdp);
1262 /* afs_linux_ireadlink
1263 * Internal readlink which can return link contents to user or kernel space.
1264 * Note that the buffer is NOT supposed to be null-terminated.
1266 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1270 cred_t *credp = crref();
1274 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1275 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1279 return maxlen - tuio.uio_resid;
1284 #if !defined(AFS_LINUX24_ENV)
1285 /* afs_linux_readlink
1286 * Fill target (which is in user space) with contents of symlink.
1288 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1291 struct inode *ip = dp->d_inode;
1294 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1300 /* afs_linux_follow_link
1301 * a file system dependent link following routine.
1303 struct dentry * afs_linux_follow_link(struct dentry *dp,
1304 struct dentry *basep,
1305 unsigned int follow)
1313 name = osi_Alloc(PATH_MAX+1);
1317 return ERR_PTR(-EIO);
1320 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1325 res = ERR_PTR(code);
1329 res = lookup_dentry(name, basep, follow);
1333 osi_Free(name, PATH_MAX+1);
1339 /* afs_linux_readpage
1340 * all reads come through here. A strategy-like read call.
1342 int afs_linux_readpage(struct file *fp, struct page *pp)
1345 cred_t *credp = crref();
1346 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1348 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1350 ulong address = afs_linux_page_address(pp);
1351 afs_offs_t offset = pageoff(pp);
1355 struct inode *ip = FILE_INODE(fp);
1356 int cnt = atomic_read(&pp->count);
1357 struct vcache *avc = ITOAFS(ip);
1360 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1361 ICL_TYPE_POINTER, ip,
1362 ICL_TYPE_POINTER, pp,
1363 ICL_TYPE_INT32, cnt,
1364 ICL_TYPE_INT32, 99999); /* not a possible code value */
1365 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1371 atomic_add(1, &pp->count);
1372 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1373 clear_bit(PG_error, &pp->flags);
1376 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1377 UIO_READ, AFS_UIOSYS);
1378 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1379 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1384 if (tuio.uio_resid) /* zero remainder of page */
1385 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1387 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1388 flush_dcache_page(pp);
1389 SetPageUptodate(pp);
1391 set_bit(PG_uptodate, &pp->flags);
1395 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1399 clear_bit(PG_locked, &pp->flags);
1404 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1406 struct vrequest treq;
1408 code = afs_InitReq(&treq, credp);
1409 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1410 tdc = afs_FindDCache(avc, offset);
1412 if (!(tdc->mflags & DFNextStarted))
1413 afs_PrefetchChunk(avc, tdc, credp, &treq);
1416 ReleaseWriteLock(&avc->lock);
1421 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1422 ICL_TYPE_POINTER, ip,
1423 ICL_TYPE_POINTER, pp,
1424 ICL_TYPE_INT32, cnt,
1425 ICL_TYPE_INT32, code);
1430 #if defined(AFS_LINUX24_ENV)
1431 int afs_linux_writepage(struct page *pp)
1433 struct address_space *mapping = pp->mapping;
1434 struct inode *inode;
1435 unsigned long end_index;
1436 unsigned offset = PAGE_CACHE_SIZE;
1439 inode = (struct inode *) mapping->host;
1440 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1443 if (pp->index < end_index)
1445 /* things got complicated... */
1446 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1447 /* OK, are we completely out? */
1448 if (pp->index >= end_index+1 || !offset)
1452 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1454 SetPageUptodate(pp);
1456 if (status == offset)
1464 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1465 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1467 /* afs_linux_truncate
1468 * Handles discarding disk blocks if this were a device. ext2 indicates we
1469 * may need to zero partial last pages of memory mapped files.
1471 void afs_linux_truncate(struct inode *ip)
1476 /* afs_linux_permission
1477 * Check access rights - returns error if can't check or permission denied.
1479 int afs_linux_permission(struct inode *ip, int mode)
1482 cred_t *credp = crref();
1486 if (mode & MAY_EXEC) tmp |= VEXEC;
1487 if (mode & MAY_READ) tmp |= VREAD;
1488 if (mode & MAY_WRITE) tmp |= VWRITE;
1489 code = afs_access(ITOAFS(ip), tmp, credp);
1498 /* msdos sector mapping hack for memory mapping. */
1499 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1502 #if defined(AFS_LINUX24_ENV)
1503 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1504 unsigned long offset,
1507 struct vcache *vcp = ITOAFS(ip);
1516 buffer = kmap(pp) + offset;
1517 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1520 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1521 ICL_TYPE_POINTER, pp,
1522 ICL_TYPE_INT32, atomic_read(&pp->count),
1523 ICL_TYPE_INT32, 99999);
1525 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1527 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1531 if (!code && afs_stats_cmperf.cacheCurrDirtyChunks >
1532 afs_stats_cmperf.cacheMaxDirtyChunks) {
1533 struct vrequest treq;
1535 ObtainWriteLock(&vcp->lock, 533);
1536 if (!afs_InitReq(&treq, credp))
1537 code = afs_DoPartialWrite(vcp, &treq);
1538 ReleaseWriteLock(&vcp->lock);
1540 code = code ? -code : count - tuio.uio_resid;
1542 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1543 ICL_TYPE_POINTER, pp,
1544 ICL_TYPE_INT32, atomic_read(&pp->count),
1545 ICL_TYPE_INT32, code);
1554 afs_linux_updatepage(struct file *file, struct page *page,
1555 unsigned long offset, unsigned int count)
1557 struct dentry *dentry = file->f_dentry;
1559 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1562 /* afs_linux_updatepage
1563 * What one would have thought was writepage - write dirty page to file.
1564 * Called from generic_file_write. buffer is still in user space. pagep
1565 * has been filled in with old data if we're updating less than a page.
1567 int afs_linux_updatepage(struct file *fp, struct page *pp,
1568 unsigned long offset,
1569 unsigned int count, int sync)
1571 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1572 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1578 set_bit(PG_locked, &pp->flags);
1582 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1583 ICL_TYPE_POINTER, pp,
1584 ICL_TYPE_INT32, atomic_read(&pp->count),
1585 ICL_TYPE_INT32, 99999);
1586 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1587 count, UIO_WRITE, AFS_UIOSYS);
1589 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1593 code = code ? -code : count - tuio.uio_resid;
1594 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1595 ICL_TYPE_POINTER, pp,
1596 ICL_TYPE_INT32, atomic_read(&pp->count),
1597 ICL_TYPE_INT32, code);
1602 clear_bit(PG_locked, &pp->flags);
1607 #if defined(AFS_LINUX24_ENV)
1608 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1614 code = afs_linux_updatepage(file, page, offset, to-offset);
1622 static int afs_linux_prepare_write(struct file *file, struct page *page,
1623 unsigned from, unsigned to)
1629 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1632 #if defined(AFS_LINUX24_ENV)
1633 struct inode_operations afs_file_iops = {
1634 revalidate: afs_linux_revalidate,
1635 setattr: afs_notify_change,
1636 permission: afs_linux_permission,
1638 struct address_space_operations afs_file_aops = {
1639 readpage: afs_linux_readpage,
1640 writepage: afs_linux_writepage,
1641 commit_write: afs_linux_commit_write,
1642 prepare_write: afs_linux_prepare_write,
1645 struct inode_operations *afs_ops = &afs_file_iops;
1647 struct inode_operations afs_iops = {
1648 &afs_file_fops, /* file operations */
1649 NULL, /* afs_linux_create */
1650 NULL, /* afs_linux_lookup */
1651 NULL, /* afs_linux_link */
1652 NULL, /* afs_linux_unlink */
1653 NULL, /* afs_linux_symlink */
1654 NULL, /* afs_linux_mkdir */
1655 NULL, /* afs_linux_rmdir */
1656 NULL, /* afs_linux_mknod */
1657 NULL, /* afs_linux_rename */
1658 NULL, /* afs_linux_readlink */
1659 NULL, /* afs_linux_follow_link */
1661 NULL, /* afs_linux_writepage */
1662 NULL, /* afs_linux_bmap */
1663 NULL, /* afs_linux_truncate */
1664 afs_linux_permission,
1665 NULL, /* afs_linux_smap */
1666 afs_linux_updatepage,
1667 afs_linux_revalidate,
1670 struct inode_operations *afs_ops = &afs_iops;
1673 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1674 * by what sort of operation is allowed.....
1676 #if defined(AFS_LINUX24_ENV)
1677 struct inode_operations afs_dir_iops = {
1678 create: afs_linux_create,
1679 lookup: afs_linux_lookup,
1680 link: afs_linux_link,
1681 unlink: afs_linux_unlink,
1682 symlink: afs_linux_symlink,
1683 mkdir: afs_linux_mkdir,
1684 rmdir: afs_linux_rmdir,
1685 rename: afs_linux_rename,
1686 revalidate: afs_linux_revalidate,
1687 setattr: afs_notify_change,
1688 permission: afs_linux_permission,
1691 struct inode_operations afs_dir_iops = {
1692 &afs_dir_fops, /* file operations for directories */
1700 NULL, /* afs_linux_mknod */
1702 NULL, /* afs_linux_readlink */
1703 NULL, /* afs_linux_follow_link */
1704 NULL, /* afs_linux_readpage */
1705 NULL, /* afs_linux_writepage */
1706 NULL, /* afs_linux_bmap */
1707 NULL, /* afs_linux_truncate */
1708 afs_linux_permission,
1709 NULL, /* afs_linux_smap */
1710 NULL, /* afs_linux_updatepage */
1711 afs_linux_revalidate,
1715 /* We really need a separate symlink set of ops, since do_follow_link()
1716 * determines if it _is_ a link by checking if the follow_link op is set.
1718 #if defined(AFS_LINUX24_ENV)
1719 static int afs_symlink_filler(struct file *file, struct page *page)
1721 struct inode *ip = (struct inode *) page->mapping->host;
1722 char *p = (char *)kmap(page);
1727 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1731 p[code] = '\0'; /* null terminate? */
1735 SetPageUptodate(page);
1750 struct address_space_operations afs_symlink_aops = {
1751 readpage: afs_symlink_filler
1754 struct inode_operations afs_symlink_iops = {
1755 readlink: page_readlink,
1756 follow_link: page_follow_link,
1757 setattr: afs_notify_change,
1760 struct inode_operations afs_symlink_iops = {
1761 NULL, /* file operations */
1769 NULL, /* afs_linux_mknod */
1772 afs_linux_follow_link,
1773 NULL, /* readpage */
1774 NULL, /* afs_linux_writepage */
1775 NULL, /* afs_linux_bmap */
1776 NULL, /* afs_linux_truncate */
1777 afs_linux_permission, /* tho the code appears to indicate not used? */
1778 NULL, /* afs_linux_smap */
1779 NULL, /* updatepage */
1780 afs_linux_revalidate, /* tho the code appears to indicate not used? */