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
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
27 #include "afs/sysincludes.h"
28 #include "afsincludes.h"
29 #include "afs/afs_stats.h"
31 #ifdef HAVE_MM_INLINE_H
32 #include "h/mm_inline.h"
34 #include "h/pagemap.h"
35 #if defined(AFS_LINUX24_ENV)
36 #include "h/smp_lock.h"
38 #if defined(AFS_LINUX26_ENV)
39 #include "h/writeback.h"
43 #define pageoff(pp) pgoff2loff((pp)->index)
45 #define pageoff(pp) pp->offset
48 #if defined(AFS_LINUX26_ENV)
49 #define UnlockPage(pp) unlock_page(pp)
52 extern struct vcache *afs_globalVp;
53 extern afs_rwlock_t afs_xvcache;
55 #if defined(AFS_LINUX24_ENV)
56 extern struct inode_operations afs_file_iops;
57 extern struct address_space_operations afs_file_aops;
58 struct address_space_operations afs_symlink_aops;
60 extern struct inode_operations afs_dir_iops;
61 extern struct inode_operations afs_symlink_iops;
65 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
68 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
69 cred_t *credp = crref();
73 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
74 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
77 /* get a validated vcache entry */
78 code = afs_InitReq(&treq, credp);
80 code = afs_VerifyVCache(vcp, &treq);
85 #ifdef AFS_64BIT_CLIENT
86 if (*offp + count > afs_vmMappingEnd) {
91 if (*offp < afs_vmMappingEnd) {
92 /* special case of a buffer crossing the VM mapping end */
93 afs_int32 tcount = afs_vmMappingEnd - *offp;
95 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
97 code = generic_file_read(fp, buf, tcount, offp);
104 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
105 UIO_READ, AFS_UIOSYS);
106 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
107 xfered += count - tuio.uio_resid;
109 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
110 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
111 ICL_TYPE_INT32, code);
113 *offp += count - tuio.uio_resid;
121 #endif /* AFS_64BIT_CLIENT */
122 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
124 code = generic_file_read(fp, buf, count, offp);
126 #ifdef AFS_64BIT_CLIENT
128 #endif /* AFS_64BIT_CLIENT */
131 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
132 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
141 /* Now we have integrated VM for writes as well as reads. generic_file_write
142 * also takes care of re-positioning the pointer if file is open in append
143 * mode. Call fake open/close to ensure we do writes of core dumps.
146 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
150 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
151 struct vrequest treq;
152 cred_t *credp = crref();
157 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
158 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
159 (fp->f_flags & O_APPEND) ? 99998 : 99999);
162 /* get a validated vcache entry */
163 code = (ssize_t) afs_InitReq(&treq, credp);
165 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
167 ObtainWriteLock(&vcp->lock, 529);
169 ReleaseWriteLock(&vcp->lock);
173 #ifdef AFS_64BIT_CLIENT
175 if (fp->f_flags & O_APPEND)
176 toffs += vcp->m.Length;
177 if (toffs + count > afs_vmMappingEnd) {
180 afs_size_t oldOffset = *offp;
181 afs_int32 xfered = 0;
183 if (toffs < afs_vmMappingEnd) {
184 /* special case of a buffer crossing the VM mapping end */
185 afs_int32 tcount = afs_vmMappingEnd - *offp;
188 code = generic_file_write(fp, buf, tcount, offp);
190 if (code != tcount) {
196 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
197 UIO_WRITE, AFS_UIOSYS);
198 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
199 xfered += count - tuio.uio_resid;
202 *offp += count - tuio.uio_resid;
204 /* Purge dirty chunks of file if there are too many dirty chunks.
205 * Inside the write loop, we only do this at a chunk boundary.
206 * Clean up partial chunk if necessary at end of loop.
208 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
209 AFS_CHUNKBASE(oldOffset)) {
210 ObtainWriteLock(&vcp->lock, 402);
211 code = afs_DoPartialWrite(vcp, &treq);
212 vcp->states |= CDirty;
213 ReleaseWriteLock(&vcp->lock);
218 ObtainWriteLock(&vcp->lock, 400);
219 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
221 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
222 vcp->m.Length = toffs;
224 ReleaseWriteLock(&vcp->lock);
229 #endif /* AFS_64BIT_CLIENT */
231 code = generic_file_write(fp, buf, count, offp);
233 #ifdef AFS_64BIT_CLIENT
235 #endif /* AFS_64BIT_CLIENT */
238 ObtainWriteLock(&vcp->lock, 530);
239 vcp->m.Date = osi_Time(); /* set modification time */
240 afs_FakeClose(vcp, credp);
242 code2 = afs_DoPartialWrite(vcp, &treq);
243 if (code2 && code >= 0)
244 code = (ssize_t) - code2;
245 ReleaseWriteLock(&vcp->lock);
247 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
248 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
256 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
258 /* This is a complete rewrite of afs_readdir, since we can make use of
259 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
260 * handling and use of bulkstats will need to be reflected here as well.
263 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
265 extern struct DirEntry *afs_dir_GetBlob();
266 struct vcache *avc = ITOAFS(FILE_INODE(fp));
267 struct vrequest treq;
268 register struct dcache *tdc;
275 afs_size_t origOffset, tlen;
276 cred_t *credp = crref();
277 struct afs_fakestat_state fakestat;
279 #if defined(AFS_LINUX26_ENV)
283 AFS_STATCNT(afs_readdir);
285 code = afs_InitReq(&treq, credp);
290 afs_InitFakeStat(&fakestat);
291 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
295 /* update the cache entry */
297 code = afs_VerifyVCache(avc, &treq);
301 /* get a reference to the entire directory */
302 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
308 ObtainReadLock(&avc->lock);
309 ObtainReadLock(&tdc->lock);
311 * Make sure that the data in the cache is current. There are two
312 * cases we need to worry about:
313 * 1. The cache data is being fetched by another process.
314 * 2. The cache data is no longer valid
316 while ((avc->states & CStatd)
317 && (tdc->dflags & DFFetching)
318 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
319 ReleaseReadLock(&tdc->lock);
320 ReleaseReadLock(&avc->lock);
321 afs_osi_Sleep(&tdc->validPos);
322 ObtainReadLock(&avc->lock);
323 ObtainReadLock(&tdc->lock);
325 if (!(avc->states & CStatd)
326 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
327 ReleaseReadLock(&tdc->lock);
328 ReleaseReadLock(&avc->lock);
333 /* Fill in until we get an error or we're done. This implementation
334 * takes an offset in units of blobs, rather than bytes.
337 offset = (int) fp->f_pos;
339 dirpos = BlobScan(tdc, offset);
343 de = afs_dir_GetBlob(tdc, dirpos);
347 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
348 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
350 len = strlen(de->name);
352 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
353 (unsigned long)&tdc->f.inode, dirpos);
354 DRelease((struct buffer *) de, 0);
356 ReleaseReadLock(&avc->lock);
361 /* filldir returns -EINVAL when the buffer is full. */
362 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
364 unsigned int type = DT_UNKNOWN;
365 struct VenusFid afid;
368 afid.Cell = avc->fid.Cell;
369 afid.Fid.Volume = avc->fid.Fid.Volume;
370 afid.Fid.Vnode = ntohl(de->fid.vnode);
371 afid.Fid.Unique = ntohl(de->fid.vunique);
372 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
374 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
377 } else if (((tvc->states) & (CStatd | CTruth))) {
378 /* CTruth will be set if the object has
383 else if (vtype == VREG)
385 /* Don't do this until we're sure it can't be a mtpt */
386 /* else if (vtype == VLNK)
388 /* what other types does AFS support? */
390 /* clean up from afs_FindVCache */
393 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
396 code = (*filldir) (dirbuf, de->name, len, offset, ino);
398 DRelease((struct buffer *)de, 0);
401 offset = dirpos + 1 + ((len + 16) >> 5);
403 /* If filldir didn't fill in the last one this is still pointing to that
406 fp->f_pos = (loff_t) offset;
408 ReleaseReadLock(&tdc->lock);
410 ReleaseReadLock(&avc->lock);
414 afs_PutFakeStat(&fakestat);
417 #if defined(AFS_LINUX26_ENV)
424 /* in afs_pioctl.c */
425 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
428 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
429 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
431 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
436 /* We need to detect unmap's after close. To do that, we need our own
437 * vm_operations_struct's. And we need to set them up for both the
438 * private and shared mappings. The fun part is that these are all static
439 * so we'll have to initialize on the fly!
441 static struct vm_operations_struct afs_private_mmap_ops;
442 static int afs_private_mmap_ops_inited = 0;
443 static struct vm_operations_struct afs_shared_mmap_ops;
444 static int afs_shared_mmap_ops_inited = 0;
447 afs_linux_vma_close(struct vm_area_struct *vmap)
456 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
461 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
462 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
463 ICL_TYPE_INT32, vcp->execsOrWriters);
464 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
465 ObtainWriteLock(&vcp->lock, 532);
468 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
469 MyPidxx, &vcp, &vmap);
473 ReleaseWriteLock(&vcp->lock);
475 if (need_unlock && vcp->execsOrWriters < 2) {
477 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
478 /* only decrement the execsOrWriters flag if this is not a
480 if (!(vcp->states & CRO) )
481 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
482 vcp->execsOrWriters--;
483 vcp->states &= ~CMAPPED;
485 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
486 vcp->execsOrWriters--;
487 /* If we did not have the lock */
490 if (!vcp->execsOrWriters)
491 vcp->execsOrWriters = 1;
496 ReleaseWriteLock(&vcp->lock);
503 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
505 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
506 cred_t *credp = crref();
507 struct vrequest treq;
511 #if defined(AFS_LINUX24_ENV)
512 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
513 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
514 vmap->vm_end - vmap->vm_start);
516 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
517 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
518 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
522 /* get a validated vcache entry */
523 code = afs_InitReq(&treq, credp);
525 code = afs_VerifyVCache(vcp, &treq);
527 if (!code && (vcp->states & CRO) &&
528 (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
534 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
537 code = generic_file_mmap(fp, vmap);
542 ObtainWriteLock(&vcp->lock, 531);
543 /* Set out vma ops so we catch the close. The following test should be
544 * the same as used in generic_file_mmap.
546 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
547 if (!afs_shared_mmap_ops_inited) {
548 afs_shared_mmap_ops_inited = 1;
549 afs_shared_mmap_ops = *vmap->vm_ops;
550 afs_shared_mmap_ops.close = afs_linux_vma_close;
552 vmap->vm_ops = &afs_shared_mmap_ops;
554 if (!afs_private_mmap_ops_inited) {
555 afs_private_mmap_ops_inited = 1;
556 afs_private_mmap_ops = *vmap->vm_ops;
557 afs_private_mmap_ops.close = afs_linux_vma_close;
559 vmap->vm_ops = &afs_private_mmap_ops;
563 /* Add an open reference on the first mapping. */
564 if (vcp->mapcnt == 0) {
565 if (!(vcp->states & CRO))
566 vcp->execsOrWriters++;
568 vcp->states |= CMAPPED;
570 ReleaseWriteLock(&vcp->lock);
580 afs_linux_open(struct inode *ip, struct file *fp)
583 cred_t *credp = crref();
585 #ifdef AFS_LINUX24_ENV
589 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
591 #ifdef AFS_LINUX24_ENV
600 afs_linux_release(struct inode *ip, struct file *fp)
602 struct vcache *vcp = ITOAFS(ip);
603 cred_t *credp = crref();
606 #ifdef AFS_LINUX24_ENV
610 code = afs_close(vcp, fp->f_flags, credp);
612 #ifdef AFS_LINUX24_ENV
620 #if defined(AFS_LINUX24_ENV)
622 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
625 afs_linux_fsync(struct file *fp, struct dentry *dp)
629 struct inode *ip = FILE_INODE(fp);
630 cred_t *credp = crref();
632 #ifdef AFS_LINUX24_ENV
636 code = afs_fsync(ITOAFS(ip), credp);
638 #ifdef AFS_LINUX24_ENV
648 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
651 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
652 cred_t *credp = crref();
653 struct AFS_FLOCK flock;
654 /* Convert to a lock format afs_lockctl understands. */
655 memset((char *)&flock, 0, sizeof(flock));
656 flock.l_type = flp->fl_type;
657 flock.l_pid = flp->fl_pid;
659 flock.l_start = flp->fl_start;
660 flock.l_len = flp->fl_end - flp->fl_start;
662 /* Safe because there are no large files, yet */
663 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
664 if (cmd == F_GETLK64)
666 else if (cmd == F_SETLK64)
668 else if (cmd == F_SETLKW64)
670 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
673 code = afs_lockctl(vcp, &flock, cmd, credp);
676 /* Convert flock back to Linux's file_lock */
677 flp->fl_type = flock.l_type;
678 flp->fl_pid = flock.l_pid;
679 flp->fl_start = flock.l_start;
680 flp->fl_end = flock.l_start + flock.l_len;
688 * essentially the same as afs_fsync() but we need to get the return
689 * code for the sys_close() here, not afs_linux_release(), so call
690 * afs_StoreAllSegments() with AFS_LASTSTORE
693 afs_linux_flush(struct file *fp)
695 struct vrequest treq;
696 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
697 cred_t *credp = crref();
702 code = afs_InitReq(&treq, credp);
706 ObtainSharedLock(&vcp->lock, 535);
707 if (vcp->execsOrWriters > 0) {
708 UpgradeSToWLock(&vcp->lock, 536);
709 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
710 ConvertWToSLock(&vcp->lock);
712 code = afs_CheckCode(code, &treq, 54);
713 ReleaseSharedLock(&vcp->lock);
722 #if !defined(AFS_LINUX24_ENV)
723 /* Not allowed to directly read a directory. */
725 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
733 struct file_operations afs_dir_fops = {
734 #if !defined(AFS_LINUX24_ENV)
735 .read = afs_linux_dir_read,
736 .lock = afs_linux_lock,
737 .fsync = afs_linux_fsync,
739 .read = generic_read_dir,
741 .readdir = afs_linux_readdir,
742 #ifdef HAVE_UNLOCKED_IOCTL
743 .unlocked_ioctl = afs_unlocked_xioctl,
747 #ifdef HAVE_COMPAT_IOCTL
748 .compat_ioctl = afs_unlocked_xioctl,
750 .open = afs_linux_open,
751 .release = afs_linux_release,
754 struct file_operations afs_file_fops = {
755 .read = afs_linux_read,
756 .write = afs_linux_write,
757 #ifdef HAVE_UNLOCKED_IOCTL
758 .unlocked_ioctl = afs_unlocked_xioctl,
762 #ifdef HAVE_COMPAT_IOCTL
763 .compat_ioctl = afs_unlocked_xioctl,
765 .mmap = afs_linux_mmap,
766 .open = afs_linux_open,
767 .flush = afs_linux_flush,
768 #ifdef AFS_LINUX26_ENV
769 .sendfile = generic_file_sendfile,
771 .release = afs_linux_release,
772 .fsync = afs_linux_fsync,
773 .lock = afs_linux_lock,
777 /**********************************************************************
778 * AFS Linux dentry operations
779 **********************************************************************/
781 /* afs_linux_revalidate
782 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
785 afs_linux_revalidate(struct dentry *dp)
789 struct vrequest treq;
790 struct vcache *vcp = ITOAFS(dp->d_inode);
791 struct vcache *rootvp = NULL;
793 #ifdef AFS_LINUX24_ENV
798 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
799 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
800 ObtainSharedLock(&afs_xvcache, 680);
801 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
802 ReleaseSharedLock(&afs_xvcache);
805 /* Make this a fast path (no crref), since it's called so often. */
806 if (vcp->states & CStatd) {
807 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
808 check_bad_parent(dp); /* check and correct mvid */
810 vcache2fakeinode(rootvp, vcp);
814 afs_PutVCache(rootvp);
816 #ifdef AFS_LINUX24_ENV
823 code = afs_InitReq(&treq, credp);
825 code = afs_VerifyVCache(vcp, &treq);
828 #ifdef AFS_LINUX24_ENV
836 #if defined(AFS_LINUX26_ENV)
838 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
840 int err = afs_linux_revalidate(dentry);
842 generic_fillattr(dentry->d_inode, stat);
847 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
848 * In kernels 2.2.10 and above, we are passed an additional flags var which
849 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
850 * we are advised to follow the entry if it is a link or to make sure that
851 * it is a directory. But since the kernel itself checks these possibilities
852 * later on, we shouldn't have to do it until later. Perhaps in the future..
854 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
855 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
857 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
860 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
864 afs_linux_dentry_revalidate(struct dentry *dp)
867 struct vrequest treq;
868 cred_t *credp = NULL;
869 struct vcache *vcp, *pvcp, *tvc = NULL;
872 #ifdef AFS_LINUX24_ENV
877 vcp = ITOAFS(dp->d_inode);
878 pvcp = ITOAFS(dp->d_parent->d_inode); /* dget_parent()? */
882 /* If it's the AFS root no chance it needs revalidating */
883 if (vcp == afs_globalVp)
886 /* check_bad_parent()? */
889 /* If the last looker changes, we should make sure the current
890 * looker still has permission to examine this file. This would
891 * always require a crref() which would be "slow".
893 if (vcp->last_looker != treq.uid) {
894 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
897 vcp->last_looker = treq.uid;
901 /* If the parent's DataVersion has changed or the vnode
902 * is longer valid, we need to do a full lookup. VerifyVCache
903 * isn't enough since the vnode may have been renamed.
905 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
908 if (afs_InitReq(&treq, credp))
910 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
911 if (!tvc || tvc != vcp)
913 if (afs_VerifyVCache(vcp, &treq)) /* update inode attributes */
916 dp->d_time = hgetlo(pvcp->m.DataVersion);
920 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
923 /* No change in parent's DataVersion so this negative
924 * lookup is still valid.
940 shrink_dcache_parent(dp);
943 #ifdef AFS_LINUX24_ENV
953 #if !defined(AFS_LINUX24_ENV)
954 /* afs_dentry_iput */
956 afs_dentry_iput(struct dentry *dp, struct inode *ip)
963 afs_dentry_delete(struct dentry *dp)
965 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
966 return 1; /* bad inode? */
971 struct dentry_operations afs_dentry_operations = {
972 .d_revalidate = afs_linux_dentry_revalidate,
973 .d_delete = afs_dentry_delete,
974 #if !defined(AFS_LINUX24_ENV)
975 .d_iput = afs_dentry_iput,
979 /**********************************************************************
980 * AFS Linux inode operations
981 **********************************************************************/
985 * Merely need to set enough of vattr to get us through the create. Note
986 * that the higher level code (open_namei) will take care of any tuncation
987 * explicitly. Exclusive open is also taken care of in open_namei.
989 * name is in kernel space at this point.
991 #ifdef IOP_CREATE_TAKES_NAMEIDATA
993 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
994 struct nameidata *nd)
997 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1001 cred_t *credp = crref();
1003 const char *name = dp->d_name.name;
1007 vattr.va_mode = mode;
1009 #if defined(AFS_LINUX26_ENV)
1014 afs_create(ITOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1015 (struct vcache **)&ip, credp);
1018 vattr2inode(ip, &vattr);
1019 /* Reset ops if symlink or directory. */
1020 #if defined(AFS_LINUX24_ENV)
1021 if (S_ISREG(ip->i_mode)) {
1022 ip->i_op = &afs_file_iops;
1023 ip->i_fop = &afs_file_fops;
1024 ip->i_data.a_ops = &afs_file_aops;
1025 } else if (S_ISDIR(ip->i_mode)) {
1026 ip->i_op = &afs_dir_iops;
1027 ip->i_fop = &afs_dir_fops;
1028 } else if (S_ISLNK(ip->i_mode)) {
1029 ip->i_op = &afs_symlink_iops;
1030 ip->i_data.a_ops = &afs_symlink_aops;
1031 ip->i_mapping = &ip->i_data;
1033 printk("afs_linux_create: FIXME\n");
1035 if (S_ISDIR(ip->i_mode))
1036 ip->i_op = &afs_dir_iops;
1037 else if (S_ISLNK(ip->i_mode))
1038 ip->i_op = &afs_symlink_iops;
1041 dp->d_op = &afs_dentry_operations;
1042 dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1043 d_instantiate(dp, ip);
1047 #if defined(AFS_LINUX26_ENV)
1054 /* afs_linux_lookup */
1055 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1056 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1058 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1059 struct nameidata *nd)
1062 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1066 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1070 cred_t *credp = crref();
1071 struct vcache *vcp = NULL;
1072 const char *comp = dp->d_name.name;
1074 struct dentry *res = 0;
1077 #if defined(AFS_LINUX26_ENV)
1081 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1085 struct inode *ip = AFSTOI(vcp);
1086 /* Reset ops if symlink or directory. */
1087 #if defined(AFS_LINUX24_ENV)
1088 if (S_ISREG(ip->i_mode)) {
1089 ip->i_op = &afs_file_iops;
1090 ip->i_fop = &afs_file_fops;
1091 ip->i_data.a_ops = &afs_file_aops;
1092 } else if (S_ISDIR(ip->i_mode)) {
1093 ip->i_op = &afs_dir_iops;
1094 ip->i_fop = &afs_dir_fops;
1095 d_prune_aliases(ip);
1096 res = d_find_alias(ip);
1097 } else if (S_ISLNK(ip->i_mode)) {
1098 ip->i_op = &afs_symlink_iops;
1099 ip->i_data.a_ops = &afs_symlink_aops;
1100 ip->i_mapping = &ip->i_data;
1103 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1104 ip->i_mode, dp->d_name.name, code);
1105 #ifdef STRUCT_INODE_HAS_I_SECURITY
1106 if (ip->i_security == NULL) {
1107 if (security_inode_alloc(ip))
1108 panic("afs_linux_lookup: Cannot allocate inode security");
1112 if (S_ISDIR(ip->i_mode))
1113 ip->i_op = &afs_dir_iops;
1114 else if (S_ISLNK(ip->i_mode))
1115 ip->i_op = &afs_symlink_iops;
1118 dp->d_op = &afs_dentry_operations;
1119 dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1120 #if defined(AFS_LINUX24_ENV)
1122 if (d_unhashed(res))
1126 d_add(dp, AFSTOI(vcp));
1128 #if defined(AFS_LINUX26_ENV)
1133 /* It's ok for the file to not be found. That's noted by the caller by
1134 * seeing that the dp->d_inode field is NULL.
1136 #if defined(AFS_LINUX24_ENV)
1140 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1144 return ERR_PTR(-code);
1153 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1156 cred_t *credp = crref();
1157 const char *name = newdp->d_name.name;
1158 struct inode *oldip = olddp->d_inode;
1160 /* If afs_link returned the vnode, we could instantiate the
1161 * dentry. Since it's not, we drop this one and do a new lookup.
1166 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1174 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1177 cred_t *credp = crref();
1178 const char *name = dp->d_name.name;
1179 struct vcache *tvc = ITOAFS(dp->d_inode);
1181 #if defined(AFS_LINUX26_ENV)
1184 if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
1185 && !(tvc->states & CUnlinked)) {
1186 struct dentry *__dp;
1188 extern char *afs_newname();
1197 osi_FreeSmallSpace(__name);
1198 __name = afs_newname();
1201 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1205 } while (__dp->d_inode != NULL);
1208 code = afs_rename(ITOAFS(dip), dp->d_name.name, ITOAFS(dip), __dp->d_name.name, credp);
1213 crfree(tvc->uncred);
1215 tvc->uncred = credp;
1216 tvc->states |= CUnlinked;
1221 __dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1230 code = afs_remove(ITOAFS(dip), name, credp);
1235 #if defined(AFS_LINUX26_ENV)
1244 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1247 cred_t *credp = crref();
1249 const char *name = dp->d_name.name;
1251 /* If afs_symlink returned the vnode, we could instantiate the
1252 * dentry. Since it's not, we drop this one and do a new lookup.
1258 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1265 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1268 cred_t *credp = crref();
1269 struct vcache *tvcp = NULL;
1271 const char *name = dp->d_name.name;
1273 #if defined(AFS_LINUX26_ENV)
1278 vattr.va_mask = ATTR_MODE;
1279 vattr.va_mode = mode;
1280 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1284 tvcp->v.v_op = &afs_dir_iops;
1285 #if defined(AFS_LINUX24_ENV)
1286 tvcp->v.v_fop = &afs_dir_fops;
1288 dp->d_op = &afs_dentry_operations;
1289 dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1290 d_instantiate(dp, AFSTOI(tvcp));
1293 #if defined(AFS_LINUX26_ENV)
1301 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1304 cred_t *credp = crref();
1305 const char *name = dp->d_name.name;
1307 #if defined(AFS_LINUX26_ENV)
1311 code = afs_rmdir(ITOAFS(dip), name, credp);
1314 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1315 * that failed because a directory is not empty. So, we map
1316 * EEXIST to ENOTEMPTY on linux.
1318 if (code == EEXIST) {
1326 #if defined(AFS_LINUX26_ENV)
1336 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1337 struct inode *newip, struct dentry *newdp)
1340 cred_t *credp = crref();
1341 const char *oldname = olddp->d_name.name;
1342 const char *newname = newdp->d_name.name;
1343 struct dentry *rehash = NULL;
1345 #if defined(AFS_LINUX26_ENV)
1346 /* Prevent any new references during rename operation. */
1349 /* Remove old and new entries from name hash. New one will change below.
1350 * While it's optimal to catch failures and re-insert newdp into hash,
1351 * it's also error prone and in that case we're already dealing with error
1352 * cases. Let another lookup put things right, if need be.
1354 #if defined(AFS_LINUX26_ENV)
1355 if (!d_unhashed(newdp)) {
1360 if (!list_empty(&newdp->d_hash)) {
1366 #if defined(AFS_LINUX24_ENV)
1367 if (atomic_read(&olddp->d_count) > 1)
1368 shrink_dcache_parent(olddp);
1372 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1378 #if defined(AFS_LINUX26_ENV)
1387 /* afs_linux_ireadlink
1388 * Internal readlink which can return link contents to user or kernel space.
1389 * Note that the buffer is NOT supposed to be null-terminated.
1392 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1395 cred_t *credp = crref();
1399 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1400 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1404 return maxlen - tuio.uio_resid;
1409 #if !defined(AFS_LINUX24_ENV)
1410 /* afs_linux_readlink
1411 * Fill target (which is in user space) with contents of symlink.
1414 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1417 struct inode *ip = dp->d_inode;
1420 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1426 /* afs_linux_follow_link
1427 * a file system dependent link following routine.
1430 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1431 unsigned int follow)
1439 name = osi_Alloc(PATH_MAX + 1);
1443 return ERR_PTR(-EIO);
1446 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1451 res = ERR_PTR(code);
1454 res = lookup_dentry(name, basep, follow);
1458 osi_Free(name, PATH_MAX + 1);
1464 /* afs_linux_readpage
1465 * all reads come through here. A strategy-like read call.
1468 afs_linux_readpage(struct file *fp, struct page *pp)
1471 cred_t *credp = crref();
1472 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1474 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1476 ulong address = afs_linux_page_address(pp);
1477 afs_offs_t offset = pageoff(pp);
1481 struct inode *ip = FILE_INODE(fp);
1482 int cnt = page_count(pp);
1483 struct vcache *avc = ITOAFS(ip);
1486 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1490 atomic_add(1, &pp->count);
1491 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1492 clear_bit(PG_error, &pp->flags);
1495 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1497 #ifdef AFS_LINUX24_ENV
1501 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip, ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32, 99999); /* not a possible code value */
1502 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1503 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1504 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1507 #ifdef AFS_LINUX24_ENV
1512 if (tuio.uio_resid) /* zero remainder of page */
1513 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1515 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1516 flush_dcache_page(pp);
1517 SetPageUptodate(pp);
1519 set_bit(PG_uptodate, &pp->flags);
1523 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1527 clear_bit(PG_locked, &pp->flags);
1532 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1534 struct vrequest treq;
1537 code = afs_InitReq(&treq, credp);
1538 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1539 tdc = afs_FindDCache(avc, offset);
1541 if (!(tdc->mflags & DFNextStarted))
1542 afs_PrefetchChunk(avc, tdc, credp, &treq);
1545 ReleaseWriteLock(&avc->lock);
1554 #if defined(AFS_LINUX24_ENV)
1555 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1557 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1560 afs_linux_writepage(struct page *pp)
1563 struct address_space *mapping = pp->mapping;
1564 struct inode *inode;
1565 unsigned long end_index;
1566 unsigned offset = PAGE_CACHE_SIZE;
1569 #if defined(AFS_LINUX26_ENV)
1570 if (PageReclaim(pp)) {
1571 return WRITEPAGE_ACTIVATE;
1574 if (PageLaunder(pp)) {
1575 return(fail_writepage(pp));
1579 inode = (struct inode *)mapping->host;
1580 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1583 if (pp->index < end_index)
1585 /* things got complicated... */
1586 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1587 /* OK, are we completely out? */
1588 if (pp->index >= end_index + 1 || !offset)
1591 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1592 SetPageUptodate(pp);
1594 if (status == offset)
1601 /* afs_linux_permission
1602 * Check access rights - returns error if can't check or permission denied.
1604 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1606 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1609 afs_linux_permission(struct inode *ip, int mode)
1613 cred_t *credp = crref();
1617 if (mode & MAY_EXEC)
1619 if (mode & MAY_READ)
1621 if (mode & MAY_WRITE)
1623 code = afs_access(ITOAFS(ip), tmp, credp);
1631 #if defined(AFS_LINUX24_ENV)
1633 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1634 unsigned long offset, unsigned int count)
1636 struct vcache *vcp = ITOAFS(ip);
1645 buffer = kmap(pp) + offset;
1646 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1651 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1652 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1653 ICL_TYPE_INT32, 99999);
1655 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1657 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1662 && afs_stats_cmperf.cacheCurrDirtyChunks >
1663 afs_stats_cmperf.cacheMaxDirtyChunks) {
1664 struct vrequest treq;
1666 ObtainWriteLock(&vcp->lock, 533);
1667 if (!afs_InitReq(&treq, credp))
1668 code = afs_DoPartialWrite(vcp, &treq);
1669 ReleaseWriteLock(&vcp->lock);
1671 code = code ? -code : count - tuio.uio_resid;
1673 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1674 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1675 ICL_TYPE_INT32, code);
1686 /* afs_linux_updatepage
1687 * What one would have thought was writepage - write dirty page to file.
1688 * Called from generic_file_write. buffer is still in user space. pagep
1689 * has been filled in with old data if we're updating less than a page.
1692 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1693 unsigned int count, int sync)
1695 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1696 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1702 set_bit(PG_locked, &pp->flags);
1706 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1707 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1708 ICL_TYPE_INT32, 99999);
1709 setup_uio(&tuio, &iovec, page_addr + offset,
1710 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1713 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1717 code = code ? -code : count - tuio.uio_resid;
1718 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1719 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1720 ICL_TYPE_INT32, code);
1725 clear_bit(PG_locked, &pp->flags);
1730 #if defined(AFS_LINUX24_ENV)
1732 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1737 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1738 offset, to - offset);
1739 #if !defined(AFS_LINUX26_ENV)
1747 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1750 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1751 call kmap directly instead of relying on us to do it */
1752 #if !defined(AFS_LINUX26_ENV)
1758 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1761 struct inode_operations afs_file_iops = {
1762 #if defined(AFS_LINUX26_ENV)
1763 .permission = afs_linux_permission,
1764 .getattr = afs_linux_getattr,
1765 .setattr = afs_notify_change,
1766 #elif defined(AFS_LINUX24_ENV)
1767 .permission = afs_linux_permission,
1768 .revalidate = afs_linux_revalidate,
1769 .setattr = afs_notify_change,
1771 .default_file_ops = &afs_file_fops,
1772 .readpage = afs_linux_readpage,
1773 .revalidate = afs_linux_revalidate,
1774 .updatepage = afs_linux_updatepage,
1778 #if defined(AFS_LINUX24_ENV)
1779 struct address_space_operations afs_file_aops = {
1780 .readpage = afs_linux_readpage,
1781 .writepage = afs_linux_writepage,
1782 .commit_write = afs_linux_commit_write,
1783 .prepare_write = afs_linux_prepare_write,
1788 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1789 * by what sort of operation is allowed.....
1792 struct inode_operations afs_dir_iops = {
1793 #if !defined(AFS_LINUX24_ENV)
1794 .default_file_ops = &afs_dir_fops,
1796 .setattr = afs_notify_change,
1798 .create = afs_linux_create,
1799 .lookup = afs_linux_lookup,
1800 .link = afs_linux_link,
1801 .unlink = afs_linux_unlink,
1802 .symlink = afs_linux_symlink,
1803 .mkdir = afs_linux_mkdir,
1804 .rmdir = afs_linux_rmdir,
1805 .rename = afs_linux_rename,
1806 #if defined(AFS_LINUX26_ENV)
1807 .getattr = afs_linux_getattr,
1809 .revalidate = afs_linux_revalidate,
1811 .permission = afs_linux_permission,
1814 /* We really need a separate symlink set of ops, since do_follow_link()
1815 * determines if it _is_ a link by checking if the follow_link op is set.
1817 #if defined(AFS_LINUX24_ENV)
1819 afs_symlink_filler(struct file *file, struct page *page)
1821 struct inode *ip = (struct inode *)page->mapping->host;
1822 char *p = (char *)kmap(page);
1827 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1832 p[code] = '\0'; /* null terminate? */
1835 SetPageUptodate(page);
1849 struct address_space_operations afs_symlink_aops = {
1850 .readpage = afs_symlink_filler
1854 struct inode_operations afs_symlink_iops = {
1855 #if defined(AFS_LINUX24_ENV)
1856 .readlink = page_readlink,
1857 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1858 .follow_link = page_follow_link,
1860 .follow_link = page_follow_link_light,
1861 .put_link = page_put_link,
1863 .setattr = afs_notify_change,
1865 .readlink = afs_linux_readlink,
1866 .follow_link = afs_linux_follow_link,
1867 .permission = afs_linux_permission,
1868 .revalidate = afs_linux_revalidate,