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;
56 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
59 struct vcache *vcp = VTOAFS(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_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
68 /* get a validated vcache entry */
69 code = afs_InitReq(&treq, credp);
71 code = afs_VerifyVCache(vcp, &treq);
76 #ifdef AFS_64BIT_CLIENT
77 if (*offp + count > afs_vmMappingEnd) {
82 if (*offp < afs_vmMappingEnd) {
83 /* special case of a buffer crossing the VM mapping end */
84 afs_int32 tcount = afs_vmMappingEnd - *offp;
86 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
88 code = generic_file_read(fp, buf, tcount, offp);
95 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
96 UIO_READ, AFS_UIOSYS);
97 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
98 xfered += count - tuio.uio_resid;
100 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
101 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
102 ICL_TYPE_INT32, code);
104 *offp += count - tuio.uio_resid;
112 #endif /* AFS_64BIT_CLIENT */
113 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
115 code = generic_file_read(fp, buf, count, offp);
117 #ifdef AFS_64BIT_CLIENT
119 #endif /* AFS_64BIT_CLIENT */
122 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
123 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
132 /* Now we have integrated VM for writes as well as reads. generic_file_write
133 * also takes care of re-positioning the pointer if file is open in append
134 * mode. Call fake open/close to ensure we do writes of core dumps.
137 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
141 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
142 struct vrequest treq;
143 cred_t *credp = crref();
148 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
149 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
150 (fp->f_flags & O_APPEND) ? 99998 : 99999);
153 /* get a validated vcache entry */
154 code = (ssize_t) afs_InitReq(&treq, credp);
156 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
158 ObtainWriteLock(&vcp->lock, 529);
160 ReleaseWriteLock(&vcp->lock);
164 #ifdef AFS_64BIT_CLIENT
166 if (fp->f_flags & O_APPEND)
167 toffs += vcp->m.Length;
168 if (toffs + count > afs_vmMappingEnd) {
171 afs_size_t oldOffset = *offp;
172 afs_int32 xfered = 0;
174 if (toffs < afs_vmMappingEnd) {
175 /* special case of a buffer crossing the VM mapping end */
176 afs_int32 tcount = afs_vmMappingEnd - *offp;
179 code = generic_file_write(fp, buf, tcount, offp);
181 if (code != tcount) {
187 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
188 UIO_WRITE, AFS_UIOSYS);
189 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
190 xfered += count - tuio.uio_resid;
193 *offp += count - tuio.uio_resid;
195 /* Purge dirty chunks of file if there are too many dirty chunks.
196 * Inside the write loop, we only do this at a chunk boundary.
197 * Clean up partial chunk if necessary at end of loop.
199 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
200 AFS_CHUNKBASE(oldOffset)) {
201 ObtainWriteLock(&vcp->lock, 402);
202 code = afs_DoPartialWrite(vcp, &treq);
203 vcp->states |= CDirty;
204 ReleaseWriteLock(&vcp->lock);
209 ObtainWriteLock(&vcp->lock, 400);
210 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
212 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
213 vcp->m.Length = toffs;
215 ReleaseWriteLock(&vcp->lock);
220 #endif /* AFS_64BIT_CLIENT */
222 code = generic_file_write(fp, buf, count, offp);
224 #ifdef AFS_64BIT_CLIENT
226 #endif /* AFS_64BIT_CLIENT */
229 ObtainWriteLock(&vcp->lock, 530);
230 vcp->m.Date = osi_Time(); /* set modification time */
231 afs_FakeClose(vcp, credp);
233 code2 = afs_DoPartialWrite(vcp, &treq);
234 if (code2 && code >= 0)
235 code = (ssize_t) - code2;
236 ReleaseWriteLock(&vcp->lock);
238 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
239 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
247 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
249 /* This is a complete rewrite of afs_readdir, since we can make use of
250 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
251 * handling and use of bulkstats will need to be reflected here as well.
254 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
256 extern struct DirEntry *afs_dir_GetBlob();
257 struct vcache *avc = VTOAFS(FILE_INODE(fp));
258 struct vrequest treq;
259 register struct dcache *tdc;
266 afs_size_t origOffset, tlen;
267 cred_t *credp = crref();
268 struct afs_fakestat_state fakestat;
270 #if defined(AFS_LINUX26_ENV)
274 AFS_STATCNT(afs_readdir);
276 code = afs_InitReq(&treq, credp);
281 afs_InitFakeStat(&fakestat);
282 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
286 /* update the cache entry */
288 code = afs_VerifyVCache(avc, &treq);
292 /* get a reference to the entire directory */
293 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
299 ObtainReadLock(&avc->lock);
300 ObtainReadLock(&tdc->lock);
302 * Make sure that the data in the cache is current. There are two
303 * cases we need to worry about:
304 * 1. The cache data is being fetched by another process.
305 * 2. The cache data is no longer valid
307 while ((avc->states & CStatd)
308 && (tdc->dflags & DFFetching)
309 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
310 ReleaseReadLock(&tdc->lock);
311 ReleaseReadLock(&avc->lock);
312 afs_osi_Sleep(&tdc->validPos);
313 ObtainReadLock(&avc->lock);
314 ObtainReadLock(&tdc->lock);
316 if (!(avc->states & CStatd)
317 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
318 ReleaseReadLock(&tdc->lock);
319 ReleaseReadLock(&avc->lock);
324 /* Fill in until we get an error or we're done. This implementation
325 * takes an offset in units of blobs, rather than bytes.
328 offset = (int) fp->f_pos;
330 dirpos = BlobScan(tdc, offset);
334 de = afs_dir_GetBlob(tdc, dirpos);
338 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
339 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
341 len = strlen(de->name);
343 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
344 (unsigned long)&tdc->f.inode, dirpos);
345 DRelease((struct buffer *) de, 0);
347 ReleaseReadLock(&avc->lock);
352 /* filldir returns -EINVAL when the buffer is full. */
353 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
355 unsigned int type = DT_UNKNOWN;
356 struct VenusFid afid;
359 afid.Cell = avc->fid.Cell;
360 afid.Fid.Volume = avc->fid.Fid.Volume;
361 afid.Fid.Vnode = ntohl(de->fid.vnode);
362 afid.Fid.Unique = ntohl(de->fid.vunique);
363 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
365 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
368 } else if (((tvc->states) & (CStatd | CTruth))) {
369 /* CTruth will be set if the object has
374 else if (vtype == VREG)
376 /* Don't do this until we're sure it can't be a mtpt */
377 /* else if (vtype == VLNK)
379 /* what other types does AFS support? */
381 /* clean up from afs_FindVCache */
384 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
387 code = (*filldir) (dirbuf, de->name, len, offset, ino);
389 DRelease((struct buffer *)de, 0);
392 offset = dirpos + 1 + ((len + 16) >> 5);
394 /* If filldir didn't fill in the last one this is still pointing to that
397 fp->f_pos = (loff_t) offset;
399 ReleaseReadLock(&tdc->lock);
401 ReleaseReadLock(&avc->lock);
405 afs_PutFakeStat(&fakestat);
408 #if defined(AFS_LINUX26_ENV)
415 /* in afs_pioctl.c */
416 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
419 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
420 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
422 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
427 /* We need to detect unmap's after close. To do that, we need our own
428 * vm_operations_struct's. And we need to set them up for both the
429 * private and shared mappings. The fun part is that these are all static
430 * so we'll have to initialize on the fly!
432 static struct vm_operations_struct afs_private_mmap_ops;
433 static int afs_private_mmap_ops_inited = 0;
434 static struct vm_operations_struct afs_shared_mmap_ops;
435 static int afs_shared_mmap_ops_inited = 0;
438 afs_linux_vma_close(struct vm_area_struct *vmap)
447 vcp = VTOAFS(FILE_INODE(vmap->vm_file));
452 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
453 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
454 ICL_TYPE_INT32, vcp->execsOrWriters);
455 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
456 ObtainWriteLock(&vcp->lock, 532);
459 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
460 MyPidxx, &vcp, &vmap);
464 ReleaseWriteLock(&vcp->lock);
466 if (need_unlock && vcp->execsOrWriters < 2) {
468 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
469 /* only decrement the execsOrWriters flag if this is not a
471 if (!(vcp->states & CRO) )
472 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
473 vcp->execsOrWriters--;
474 vcp->states &= ~CMAPPED;
476 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
477 vcp->execsOrWriters--;
478 /* If we did not have the lock */
481 if (!vcp->execsOrWriters)
482 vcp->execsOrWriters = 1;
487 ReleaseWriteLock(&vcp->lock);
494 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
496 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
497 cred_t *credp = crref();
498 struct vrequest treq;
502 #if defined(AFS_LINUX24_ENV)
503 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
504 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
505 vmap->vm_end - vmap->vm_start);
507 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
508 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
509 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
513 /* get a validated vcache entry */
514 code = afs_InitReq(&treq, credp);
516 code = afs_VerifyVCache(vcp, &treq);
518 if (!code && (vcp->states & CRO) &&
519 (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
525 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
528 code = generic_file_mmap(fp, vmap);
533 ObtainWriteLock(&vcp->lock, 531);
534 /* Set out vma ops so we catch the close. The following test should be
535 * the same as used in generic_file_mmap.
537 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
538 if (!afs_shared_mmap_ops_inited) {
539 afs_shared_mmap_ops_inited = 1;
540 afs_shared_mmap_ops = *vmap->vm_ops;
541 afs_shared_mmap_ops.close = afs_linux_vma_close;
543 vmap->vm_ops = &afs_shared_mmap_ops;
545 if (!afs_private_mmap_ops_inited) {
546 afs_private_mmap_ops_inited = 1;
547 afs_private_mmap_ops = *vmap->vm_ops;
548 afs_private_mmap_ops.close = afs_linux_vma_close;
550 vmap->vm_ops = &afs_private_mmap_ops;
554 /* Add an open reference on the first mapping. */
555 if (vcp->mapcnt == 0) {
556 if (!(vcp->states & CRO))
557 vcp->execsOrWriters++;
559 vcp->states |= CMAPPED;
561 ReleaseWriteLock(&vcp->lock);
571 afs_linux_open(struct inode *ip, struct file *fp)
573 struct vcache *vcp = VTOAFS(ip);
574 cred_t *credp = crref();
577 #ifdef AFS_LINUX24_ENV
581 code = afs_open(&vcp, fp->f_flags, credp);
583 #ifdef AFS_LINUX24_ENV
592 afs_linux_release(struct inode *ip, struct file *fp)
594 struct vcache *vcp = VTOAFS(ip);
595 cred_t *credp = crref();
598 #ifdef AFS_LINUX24_ENV
602 code = afs_close(vcp, fp->f_flags, credp);
604 #ifdef AFS_LINUX24_ENV
613 #if defined(AFS_LINUX24_ENV)
614 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
616 afs_linux_fsync(struct file *fp, struct dentry *dp)
620 struct inode *ip = FILE_INODE(fp);
621 cred_t *credp = crref();
623 #ifdef AFS_LINUX24_ENV
627 code = afs_fsync(VTOAFS(ip), credp);
629 #ifdef AFS_LINUX24_ENV
639 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
642 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
643 cred_t *credp = crref();
644 struct AFS_FLOCK flock;
645 /* Convert to a lock format afs_lockctl understands. */
646 memset((char *)&flock, 0, sizeof(flock));
647 flock.l_type = flp->fl_type;
648 flock.l_pid = flp->fl_pid;
650 flock.l_start = flp->fl_start;
651 flock.l_len = flp->fl_end - flp->fl_start;
653 /* Safe because there are no large files, yet */
654 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
655 if (cmd == F_GETLK64)
657 else if (cmd == F_SETLK64)
659 else if (cmd == F_SETLKW64)
661 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
664 code = afs_lockctl(vcp, &flock, cmd, credp);
667 /* Convert flock back to Linux's file_lock */
668 flp->fl_type = flock.l_type;
669 flp->fl_pid = flock.l_pid;
670 flp->fl_start = flock.l_start;
671 flp->fl_end = flock.l_start + flock.l_len;
679 * essentially the same as afs_fsync() but we need to get the return
680 * code for the sys_close() here, not afs_linux_release(), so call
681 * afs_StoreAllSegments() with AFS_LASTSTORE
684 afs_linux_flush(struct file *fp)
686 struct vrequest treq;
687 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
688 cred_t *credp = crref();
693 code = afs_InitReq(&treq, credp);
697 ObtainSharedLock(&vcp->lock, 535);
698 if (vcp->execsOrWriters > 0) {
699 UpgradeSToWLock(&vcp->lock, 536);
700 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
701 ConvertWToSLock(&vcp->lock);
703 code = afs_CheckCode(code, &treq, 54);
704 ReleaseSharedLock(&vcp->lock);
713 #if !defined(AFS_LINUX24_ENV)
714 /* Not allowed to directly read a directory. */
716 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
724 struct file_operations afs_dir_fops = {
725 #if !defined(AFS_LINUX24_ENV)
726 .read = afs_linux_dir_read,
727 .lock = afs_linux_lock,
728 .fsync = afs_linux_fsync,
730 .read = generic_read_dir,
732 .readdir = afs_linux_readdir,
733 #ifdef HAVE_UNLOCKED_IOCTL
734 .unlocked_ioctl = afs_unlocked_xioctl,
738 #ifdef HAVE_COMPAT_IOCTL
739 .compat_ioctl = afs_unlocked_xioctl,
741 .open = afs_linux_open,
742 .release = afs_linux_release,
745 struct file_operations afs_file_fops = {
746 .read = afs_linux_read,
747 .write = afs_linux_write,
748 #ifdef HAVE_UNLOCKED_IOCTL
749 .unlocked_ioctl = afs_unlocked_xioctl,
753 #ifdef HAVE_COMPAT_IOCTL
754 .compat_ioctl = afs_unlocked_xioctl,
756 .mmap = afs_linux_mmap,
757 .open = afs_linux_open,
758 .flush = afs_linux_flush,
759 #ifdef AFS_LINUX26_ENV
760 .sendfile = generic_file_sendfile,
762 .release = afs_linux_release,
763 .fsync = afs_linux_fsync,
764 .lock = afs_linux_lock,
768 /**********************************************************************
769 * AFS Linux dentry operations
770 **********************************************************************/
772 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
773 * that has its mvid (parent dir's fid) pointer set to the wrong directory
774 * due to being mounted in multiple points at once. If so, check_bad_parent()
775 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
776 * dotdotfid and mtpoint fid members.
778 * dp - dentry to be checked.
782 * This dentry's vcache's mvid will be set to the correct parent directory's
784 * This root vnode's volume will have its dotdotfid and mtpoint fids set
785 * to the correct parent and mountpoint fids.
789 check_bad_parent(struct dentry *dp)
792 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
793 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
795 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
796 printk("check_bad_parent(%s): bad parent vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume\n", dp->d_name.name);
799 /* force a lookup, so vcp->mvid is fixed up */
800 afs_lookup(pvc, dp->d_name.name, &avc, credp);
801 if (!avc || vcp != avc) { /* bad, very bad.. */
802 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
803 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
804 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
805 ICL_TYPE_POINTER, dp);
808 AFS_RELE(AFSTOV(avc));
815 /* afs_linux_revalidate
816 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
819 afs_linux_revalidate(struct dentry *dp)
822 struct vcache *vcp = VTOAFS(dp->d_inode);
826 #ifdef AFS_LINUX24_ENV
831 /* Make this a fast path (no crref), since it's called so often. */
832 if (vcp->states & CStatd) {
834 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
835 check_bad_parent(dp); /* check and correct mvid */
838 #ifdef AFS_LINUX24_ENV
845 code = afs_getattr(vcp, &vattr, credp);
847 vattr2inode(AFSTOV(vcp), &vattr);
850 #ifdef AFS_LINUX24_ENV
858 #if defined(AFS_LINUX26_ENV)
860 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
862 int err = afs_linux_revalidate(dentry);
864 generic_fillattr(dentry->d_inode, stat);
870 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
871 * In kernels 2.2.10 and above, we are passed an additional flags var which
872 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
873 * we are advised to follow the entry if it is a link or to make sure that
874 * it is a directory. But since the kernel itself checks these possibilities
875 * later on, we shouldn't have to do it until later. Perhaps in the future..
878 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
879 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
880 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
882 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
885 afs_linux_dentry_revalidate(struct dentry *dp)
889 cred_t *credp = NULL;
890 struct vcache *vcp, *pvcp, *tvc = NULL;
893 #ifdef AFS_LINUX24_ENV
900 vcp = VTOAFS(dp->d_inode);
901 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
903 if (vcp == afs_globalVp)
906 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
907 check_bad_parent(dp); /* check and correct mvid */
910 /* If the last looker changes, we should make sure the current
911 * looker still has permission to examine this file. This would
912 * always require a crref() which would be "slow".
914 if (vcp->last_looker != treq.uid) {
915 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
918 vcp->last_looker = treq.uid;
922 /* If the parent's DataVersion has changed or the vnode
923 * is longer valid, we need to do a full lookup. VerifyVCache
924 * isn't enough since the vnode may have been renamed.
927 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
930 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
931 if (!tvc || tvc != vcp)
934 if (afs_getattr(vcp, &vattr, credp))
937 vattr2inode(AFSTOV(vcp), &vattr);
938 dp->d_time = hgetlo(pvcp->m.DataVersion);
941 /* should we always update the attributes at this point? */
942 /* unlikely--the vcache entry hasn't changed */
946 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
947 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
951 /* No change in parent's DataVersion so this negative
952 * lookup is still valid. BUT, if a server is down a
953 * negative lookup can result so there should be a
954 * liftime as well. For now, always expire.
972 shrink_dcache_parent(dp);
975 #ifdef AFS_LINUX24_ENV
986 afs_dentry_iput(struct dentry *dp, struct inode *ip)
988 struct vcache *vcp = VTOAFS(ip);
991 if (vcp->states & CUnlinked)
992 (void) afs_remunlink(vcp, 1); /* perhaps afs_InactiveVCache() instead */
999 afs_dentry_delete(struct dentry *dp)
1001 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
1002 return 1; /* bad inode? */
1007 struct dentry_operations afs_dentry_operations = {
1008 .d_revalidate = afs_linux_dentry_revalidate,
1009 .d_delete = afs_dentry_delete,
1010 .d_iput = afs_dentry_iput,
1013 /**********************************************************************
1014 * AFS Linux inode operations
1015 **********************************************************************/
1019 * Merely need to set enough of vattr to get us through the create. Note
1020 * that the higher level code (open_namei) will take care of any tuncation
1021 * explicitly. Exclusive open is also taken care of in open_namei.
1023 * name is in kernel space at this point.
1026 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1027 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1028 struct nameidata *nd)
1030 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1034 cred_t *credp = crref();
1035 const char *name = dp->d_name.name;
1040 vattr.va_mode = mode;
1041 vattr.va_type = mode & S_IFMT;
1043 #if defined(AFS_LINUX26_ENV)
1047 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1051 struct inode *ip = AFSTOV(vcp);
1053 afs_getattr(vcp, &vattr, credp);
1054 afs_fill_inode(ip, &vattr);
1055 dp->d_op = &afs_dentry_operations;
1056 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1057 d_instantiate(dp, ip);
1061 #if defined(AFS_LINUX26_ENV)
1068 /* afs_linux_lookup */
1069 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1070 static struct dentry *
1071 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1072 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1073 struct nameidata *nd)
1075 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1079 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1083 cred_t *credp = crref();
1084 struct vcache *vcp = NULL;
1085 const char *comp = dp->d_name.name;
1086 struct dentry *res = NULL;
1087 struct inode *ip = NULL;
1090 #if defined(AFS_LINUX26_ENV)
1094 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1099 afs_getattr(vcp, &vattr, credp);
1100 afs_fill_inode(ip, &vattr);
1102 dp->d_op = &afs_dentry_operations;
1103 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1105 #if defined(AFS_LINUX24_ENV)
1106 if (ip && S_ISDIR(ip->i_mode)) {
1107 d_prune_aliases(ip);
1108 res = d_find_alias(ip);
1111 if (d_unhashed(res))
1117 #if defined(AFS_LINUX26_ENV)
1122 /* It's ok for the file to not be found. That's noted by the caller by
1123 * seeing that the dp->d_inode field is NULL.
1125 #if defined(AFS_LINUX24_ENV)
1129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1133 return ERR_PTR(-code);
1142 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1145 cred_t *credp = crref();
1146 const char *name = newdp->d_name.name;
1147 struct inode *oldip = olddp->d_inode;
1149 /* If afs_link returned the vnode, we could instantiate the
1150 * dentry. Since it's not, we drop this one and do a new lookup.
1155 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1163 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1166 cred_t *credp = crref();
1167 const char *name = dp->d_name.name;
1168 struct vcache *tvc = VTOAFS(dp->d_inode);
1170 #if defined(AFS_LINUX26_ENV)
1173 if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
1174 && !(tvc->states & CUnlinked)) {
1175 struct dentry *__dp;
1177 extern char *afs_newname();
1186 osi_FreeSmallSpace(__name);
1187 __name = afs_newname();
1190 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1194 } while (__dp->d_inode != NULL);
1197 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1199 tvc->mvid = (void *) __name;
1202 crfree(tvc->uncred);
1204 tvc->uncred = credp;
1205 tvc->states |= CUnlinked;
1210 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1219 code = afs_remove(VTOAFS(dip), name, credp);
1224 #if defined(AFS_LINUX26_ENV)
1233 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1236 cred_t *credp = crref();
1238 const char *name = dp->d_name.name;
1240 /* If afs_symlink returned the vnode, we could instantiate the
1241 * dentry. Since it's not, we drop this one and do a new lookup.
1247 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1254 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1257 cred_t *credp = crref();
1258 struct vcache *tvcp = NULL;
1260 const char *name = dp->d_name.name;
1262 #if defined(AFS_LINUX26_ENV)
1266 vattr.va_mask = ATTR_MODE;
1267 vattr.va_mode = mode;
1269 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1272 struct inode *ip = AFSTOV(tvcp);
1274 afs_getattr(tvcp, &vattr, credp);
1275 afs_fill_inode(ip, &vattr);
1277 dp->d_op = &afs_dentry_operations;
1278 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1279 d_instantiate(dp, ip);
1283 #if defined(AFS_LINUX26_ENV)
1291 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1294 cred_t *credp = crref();
1295 const char *name = dp->d_name.name;
1297 #if defined(AFS_LINUX26_ENV)
1301 code = afs_rmdir(VTOAFS(dip), name, credp);
1304 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1305 * that failed because a directory is not empty. So, we map
1306 * EEXIST to ENOTEMPTY on linux.
1308 if (code == EEXIST) {
1316 #if defined(AFS_LINUX26_ENV)
1325 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1326 struct inode *newip, struct dentry *newdp)
1329 cred_t *credp = crref();
1330 const char *oldname = olddp->d_name.name;
1331 const char *newname = newdp->d_name.name;
1332 struct dentry *rehash = NULL;
1334 #if defined(AFS_LINUX26_ENV)
1335 /* Prevent any new references during rename operation. */
1338 /* Remove old and new entries from name hash. New one will change below.
1339 * While it's optimal to catch failures and re-insert newdp into hash,
1340 * it's also error prone and in that case we're already dealing with error
1341 * cases. Let another lookup put things right, if need be.
1343 #if defined(AFS_LINUX26_ENV)
1344 if (!d_unhashed(newdp)) {
1349 if (!list_empty(&newdp->d_hash)) {
1355 #if defined(AFS_LINUX24_ENV)
1356 if (atomic_read(&olddp->d_count) > 1)
1357 shrink_dcache_parent(olddp);
1361 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1367 #if defined(AFS_LINUX26_ENV)
1376 /* afs_linux_ireadlink
1377 * Internal readlink which can return link contents to user or kernel space.
1378 * Note that the buffer is NOT supposed to be null-terminated.
1381 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1384 cred_t *credp = crref();
1388 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1389 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1393 return maxlen - tuio.uio_resid;
1398 #if !defined(AFS_LINUX24_ENV)
1399 /* afs_linux_readlink
1400 * Fill target (which is in user space) with contents of symlink.
1403 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1406 struct inode *ip = dp->d_inode;
1409 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1415 /* afs_linux_follow_link
1416 * a file system dependent link following routine.
1418 static struct dentry *
1419 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1420 unsigned int follow)
1428 name = osi_Alloc(PATH_MAX + 1);
1432 return ERR_PTR(-EIO);
1435 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1440 res = ERR_PTR(code);
1443 res = lookup_dentry(name, basep, follow);
1447 osi_Free(name, PATH_MAX + 1);
1453 /* afs_linux_readpage
1454 * all reads come through here. A strategy-like read call.
1457 afs_linux_readpage(struct file *fp, struct page *pp)
1460 cred_t *credp = crref();
1461 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1463 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1465 ulong address = afs_linux_page_address(pp);
1466 afs_offs_t offset = pageoff(pp);
1470 struct inode *ip = FILE_INODE(fp);
1471 int cnt = page_count(pp);
1472 struct vcache *avc = VTOAFS(ip);
1475 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1479 atomic_add(1, &pp->count);
1480 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1481 clear_bit(PG_error, &pp->flags);
1484 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1486 #ifdef AFS_LINUX24_ENV
1490 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 */
1491 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1492 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1493 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1496 #ifdef AFS_LINUX24_ENV
1501 if (tuio.uio_resid) /* zero remainder of page */
1502 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1504 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1505 flush_dcache_page(pp);
1506 SetPageUptodate(pp);
1508 set_bit(PG_uptodate, &pp->flags);
1512 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1516 clear_bit(PG_locked, &pp->flags);
1521 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1523 struct vrequest treq;
1526 code = afs_InitReq(&treq, credp);
1527 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1528 tdc = afs_FindDCache(avc, offset);
1530 if (!(tdc->mflags & DFNextStarted))
1531 afs_PrefetchChunk(avc, tdc, credp, &treq);
1534 ReleaseWriteLock(&avc->lock);
1544 #if defined(AFS_LINUX24_ENV)
1546 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1547 unsigned long offset, unsigned int count)
1549 struct vcache *vcp = VTOAFS(ip);
1558 buffer = kmap(pp) + offset;
1559 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1564 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1565 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1566 ICL_TYPE_INT32, 99999);
1568 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1570 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1572 ip->i_size = vcp->m.Length;
1573 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1576 && afs_stats_cmperf.cacheCurrDirtyChunks >
1577 afs_stats_cmperf.cacheMaxDirtyChunks) {
1578 struct vrequest treq;
1580 ObtainWriteLock(&vcp->lock, 533);
1581 if (!afs_InitReq(&treq, credp))
1582 code = afs_DoPartialWrite(vcp, &treq);
1583 ReleaseWriteLock(&vcp->lock);
1585 code = code ? -code : count - tuio.uio_resid;
1587 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1588 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1589 ICL_TYPE_INT32, code);
1601 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1602 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1604 afs_linux_writepage(struct page *pp)
1607 struct address_space *mapping = pp->mapping;
1608 struct inode *inode;
1609 unsigned long end_index;
1610 unsigned offset = PAGE_CACHE_SIZE;
1613 #if defined(AFS_LINUX26_ENV)
1614 if (PageReclaim(pp)) {
1615 return WRITEPAGE_ACTIVATE;
1618 if (PageLaunder(pp)) {
1619 return(fail_writepage(pp));
1623 inode = (struct inode *)mapping->host;
1624 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1627 if (pp->index < end_index)
1629 /* things got complicated... */
1630 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1631 /* OK, are we completely out? */
1632 if (pp->index >= end_index + 1 || !offset)
1635 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1636 SetPageUptodate(pp);
1638 if (status == offset)
1644 /* afs_linux_updatepage
1645 * What one would have thought was writepage - write dirty page to file.
1646 * Called from generic_file_write. buffer is still in user space. pagep
1647 * has been filled in with old data if we're updating less than a page.
1650 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1651 unsigned int count, int sync)
1653 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1654 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1660 set_bit(PG_locked, &pp->flags);
1664 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1665 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1666 ICL_TYPE_INT32, 99999);
1667 setup_uio(&tuio, &iovec, page_addr + offset,
1668 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1671 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1673 ip->i_size = vcp->m.Length;
1674 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1676 code = code ? -code : count - tuio.uio_resid;
1677 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1678 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1679 ICL_TYPE_INT32, code);
1684 clear_bit(PG_locked, &pp->flags);
1689 /* afs_linux_permission
1690 * Check access rights - returns error if can't check or permission denied.
1693 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1694 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1696 afs_linux_permission(struct inode *ip, int mode)
1700 cred_t *credp = crref();
1704 if (mode & MAY_EXEC)
1706 if (mode & MAY_READ)
1708 if (mode & MAY_WRITE)
1710 code = afs_access(VTOAFS(ip), tmp, credp);
1717 #if defined(AFS_LINUX24_ENV)
1719 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1724 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1725 offset, to - offset);
1726 #if !defined(AFS_LINUX26_ENV)
1734 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1737 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1738 call kmap directly instead of relying on us to do it */
1739 #if !defined(AFS_LINUX26_ENV)
1745 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1748 static struct inode_operations afs_file_iops = {
1749 #if defined(AFS_LINUX26_ENV)
1750 .permission = afs_linux_permission,
1751 .getattr = afs_linux_getattr,
1752 .setattr = afs_notify_change,
1753 #elif defined(AFS_LINUX24_ENV)
1754 .permission = afs_linux_permission,
1755 .revalidate = afs_linux_revalidate,
1756 .setattr = afs_notify_change,
1758 .default_file_ops = &afs_file_fops,
1759 .readpage = afs_linux_readpage,
1760 .revalidate = afs_linux_revalidate,
1761 .updatepage = afs_linux_updatepage,
1765 #if defined(AFS_LINUX24_ENV)
1766 static struct address_space_operations afs_file_aops = {
1767 .readpage = afs_linux_readpage,
1768 .writepage = afs_linux_writepage,
1769 .commit_write = afs_linux_commit_write,
1770 .prepare_write = afs_linux_prepare_write,
1775 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1776 * by what sort of operation is allowed.....
1779 static struct inode_operations afs_dir_iops = {
1780 #if !defined(AFS_LINUX24_ENV)
1781 .default_file_ops = &afs_dir_fops,
1783 .setattr = afs_notify_change,
1785 .create = afs_linux_create,
1786 .lookup = afs_linux_lookup,
1787 .link = afs_linux_link,
1788 .unlink = afs_linux_unlink,
1789 .symlink = afs_linux_symlink,
1790 .mkdir = afs_linux_mkdir,
1791 .rmdir = afs_linux_rmdir,
1792 .rename = afs_linux_rename,
1793 #if defined(AFS_LINUX26_ENV)
1794 .getattr = afs_linux_getattr,
1796 .revalidate = afs_linux_revalidate,
1798 .permission = afs_linux_permission,
1801 /* We really need a separate symlink set of ops, since do_follow_link()
1802 * determines if it _is_ a link by checking if the follow_link op is set.
1804 #if defined(AFS_LINUX24_ENV)
1806 afs_symlink_filler(struct file *file, struct page *page)
1808 struct inode *ip = (struct inode *)page->mapping->host;
1809 char *p = (char *)kmap(page);
1814 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1819 p[code] = '\0'; /* null terminate? */
1822 SetPageUptodate(page);
1836 static struct address_space_operations afs_symlink_aops = {
1837 .readpage = afs_symlink_filler
1841 static struct inode_operations afs_symlink_iops = {
1842 #if defined(AFS_LINUX24_ENV)
1843 .readlink = page_readlink,
1844 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1845 .follow_link = page_follow_link,
1847 .follow_link = page_follow_link_light,
1848 .put_link = page_put_link,
1850 .setattr = afs_notify_change,
1852 .readlink = afs_linux_readlink,
1853 .follow_link = afs_linux_follow_link,
1854 .permission = afs_linux_permission,
1855 .revalidate = afs_linux_revalidate,
1860 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1864 vattr2inode(ip, vattr);
1866 /* Reset ops if symlink or directory. */
1867 if (S_ISREG(ip->i_mode)) {
1868 ip->i_op = &afs_file_iops;
1869 #if defined(AFS_LINUX24_ENV)
1870 ip->i_fop = &afs_file_fops;
1871 ip->i_data.a_ops = &afs_file_aops;
1874 } else if (S_ISDIR(ip->i_mode)) {
1875 ip->i_op = &afs_dir_iops;
1876 #if defined(AFS_LINUX24_ENV)
1877 ip->i_fop = &afs_dir_fops;
1880 } else if (S_ISLNK(ip->i_mode)) {
1881 ip->i_op = &afs_symlink_iops;
1882 #if defined(AFS_LINUX24_ENV)
1883 ip->i_data.a_ops = &afs_symlink_aops;
1884 ip->i_mapping = &ip->i_data;
1888 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */