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"
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
29 #ifdef HAVE_MM_INLINE_H
30 #include <linux/mm_inline.h>
32 #include <linux/pagemap.h>
33 #include <linux/writeback.h>
34 #include <linux/pagevec.h>
36 #include "afs/afs_bypasscache.h"
38 #include "osi_compat.h"
39 #include "osi_pagecopy.h"
41 #ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
42 #define __pagevec_lru_add_file __pagevec_lru_add
46 #define MAX_ERRNO 1000L
49 extern struct backing_dev_info *afs_backing_dev_info;
51 extern struct vcache *afs_globalVp;
52 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
54 /* This function converts a positive error code from AFS into a negative
55 * code suitable for passing into the Linux VFS layer. It checks that the
56 * error code is within the permissable bounds for the ERR_PTR mechanism.
58 * _All_ error codes which come from the AFS layer should be passed through
59 * this function before being returned to the kernel.
63 afs_convert_code(int code) {
64 if ((code >= 0) && (code <= MAX_ERRNO))
70 /* Linux doesn't require a credp for many functions, and crref is an expensive
71 * operation. This helper function avoids obtaining it for VerifyVCache calls
75 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
80 if (avc->f.states & CStatd) {
88 code = afs_InitReq(&treq, credp);
90 code = afs_VerifyVCache2(avc, &treq);
97 return afs_convert_code(code);
100 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
102 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *iov, unsigned long segs, loff_t pos)
104 struct file *fp = iocb->ki_filp;
106 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
109 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
110 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
112 code = afs_linux_VerifyVCache(vcp, NULL);
115 /* Linux's FlushPages implementation doesn't ever use credp,
116 * so we optimise by not using it */
117 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
119 code = generic_file_aio_read(iocb, iov, segs, pos);
123 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
124 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
131 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
134 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
137 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
138 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
140 code = afs_linux_VerifyVCache(vcp, NULL);
143 /* Linux's FlushPages implementation doesn't ever use credp,
144 * so we optimise by not using it */
145 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
147 code = do_sync_read(fp, buf, count, offp);
151 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
152 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
160 /* Now we have integrated VM for writes as well as reads. the generic write operations
161 * also take care of re-positioning the pointer if file is open in append
162 * mode. Call fake open/close to ensure we do writes of core dumps.
164 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
166 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long segs, loff_t pos)
169 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
174 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
175 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
176 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
178 code = afs_linux_VerifyVCache(vcp, &credp);
180 ObtainWriteLock(&vcp->lock, 529);
182 ReleaseWriteLock(&vcp->lock);
185 code = generic_file_aio_write(iocb, iov, segs, pos);
189 ObtainWriteLock(&vcp->lock, 530);
191 if (vcp->execsOrWriters == 1 && !credp)
194 afs_FakeClose(vcp, credp);
195 ReleaseWriteLock(&vcp->lock);
197 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
198 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
208 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
211 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
216 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
217 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
218 (fp->f_flags & O_APPEND) ? 99998 : 99999);
220 code = afs_linux_VerifyVCache(vcp, &credp);
222 ObtainWriteLock(&vcp->lock, 529);
224 ReleaseWriteLock(&vcp->lock);
227 code = do_sync_write(fp, buf, count, offp);
231 ObtainWriteLock(&vcp->lock, 530);
233 if (vcp->execsOrWriters == 1 && !credp)
236 afs_FakeClose(vcp, credp);
237 ReleaseWriteLock(&vcp->lock);
239 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
240 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
250 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
252 /* This is a complete rewrite of afs_readdir, since we can make use of
253 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
254 * handling and use of bulkstats will need to be reflected here as well.
257 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
259 struct vcache *avc = VTOAFS(FILE_INODE(fp));
260 struct vrequest treq;
266 struct DirBuffer entry;
269 afs_size_t origOffset, tlen;
270 cred_t *credp = crref();
271 struct afs_fakestat_state fakestat;
274 AFS_STATCNT(afs_readdir);
276 code = afs_convert_code(afs_InitReq(&treq, credp));
281 afs_InitFakeStat(&fakestat);
282 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
286 /* update the cache entry */
288 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
292 /* get a reference to the entire directory */
293 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
299 ObtainSharedLock(&avc->lock, 810);
300 UpgradeSToWLock(&avc->lock, 811);
301 ObtainReadLock(&tdc->lock);
303 * Make sure that the data in the cache is current. There are two
304 * cases we need to worry about:
305 * 1. The cache data is being fetched by another process.
306 * 2. The cache data is no longer valid
308 while ((avc->f.states & CStatd)
309 && (tdc->dflags & DFFetching)
310 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
311 ReleaseReadLock(&tdc->lock);
312 ReleaseSharedLock(&avc->lock);
313 afs_osi_Sleep(&tdc->validPos);
314 ObtainSharedLock(&avc->lock, 812);
315 ObtainReadLock(&tdc->lock);
317 if (!(avc->f.states & CStatd)
318 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
319 ReleaseReadLock(&tdc->lock);
320 ReleaseSharedLock(&avc->lock);
325 /* Set the readdir-in-progress flag, and downgrade the lock
326 * to shared so others will be able to acquire a read lock.
328 avc->f.states |= CReadDir;
329 avc->dcreaddir = tdc;
330 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
331 ConvertWToSLock(&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 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
345 afs_warn("Corrupt directory (inode %lx, dirpos %d)",
346 (unsigned long)&tdc->f.inode, dirpos);
347 ReleaseSharedLock(&avc->lock);
353 de = (struct DirEntry *)entry.data;
354 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
355 ntohl(de->fid.vnode));
356 len = strlen(de->name);
358 /* filldir returns -EINVAL when the buffer is full. */
360 unsigned int type = DT_UNKNOWN;
361 struct VenusFid afid;
364 afid.Cell = avc->f.fid.Cell;
365 afid.Fid.Volume = avc->f.fid.Fid.Volume;
366 afid.Fid.Vnode = ntohl(de->fid.vnode);
367 afid.Fid.Unique = ntohl(de->fid.vunique);
368 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
370 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
373 } else if (((tvc->f.states) & (CStatd | CTruth))) {
374 /* CTruth will be set if the object has
379 else if (vtype == VREG)
381 /* Don't do this until we're sure it can't be a mtpt */
382 /* else if (vtype == VLNK)
384 /* what other types does AFS support? */
386 /* clean up from afs_FindVCache */
390 * If this is NFS readdirplus, then the filler is going to
391 * call getattr on this inode, which will deadlock if we're
395 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
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 UpgradeSToWLock(&avc->lock, 813);
411 avc->f.states &= ~CReadDir;
413 avc->readdir_pid = 0;
414 ReleaseSharedLock(&avc->lock);
418 afs_PutFakeStat(&fakestat);
425 /* in afs_pioctl.c */
426 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
429 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
430 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
432 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
439 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
441 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
445 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
446 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
447 vmap->vm_end - vmap->vm_start);
449 /* get a validated vcache entry */
450 code = afs_linux_VerifyVCache(vcp, NULL);
453 /* Linux's Flushpage implementation doesn't use credp, so optimise
454 * our code to not need to crref() it */
455 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
457 code = generic_file_mmap(fp, vmap);
460 vcp->f.states |= CMAPPED;
468 afs_linux_open(struct inode *ip, struct file *fp)
470 struct vcache *vcp = VTOAFS(ip);
471 cred_t *credp = crref();
475 code = afs_open(&vcp, fp->f_flags, credp);
479 return afs_convert_code(code);
483 afs_linux_release(struct inode *ip, struct file *fp)
485 struct vcache *vcp = VTOAFS(ip);
486 cred_t *credp = crref();
490 code = afs_close(vcp, fp->f_flags, credp);
491 ObtainWriteLock(&vcp->lock, 807);
496 ReleaseWriteLock(&vcp->lock);
500 return afs_convert_code(code);
504 #if defined(FOP_FSYNC_TAKES_DENTRY)
505 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
507 afs_linux_fsync(struct file *fp, int datasync)
511 struct inode *ip = FILE_INODE(fp);
512 cred_t *credp = crref();
515 code = afs_fsync(VTOAFS(ip), credp);
518 return afs_convert_code(code);
524 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
527 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
528 cred_t *credp = crref();
529 struct AFS_FLOCK flock;
531 /* Convert to a lock format afs_lockctl understands. */
532 memset(&flock, 0, sizeof(flock));
533 flock.l_type = flp->fl_type;
534 flock.l_pid = flp->fl_pid;
536 flock.l_start = flp->fl_start;
537 if (flp->fl_end == OFFSET_MAX)
538 flock.l_len = 0; /* Lock to end of file */
540 flock.l_len = flp->fl_end - flp->fl_start + 1;
542 /* Safe because there are no large files, yet */
543 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
544 if (cmd == F_GETLK64)
546 else if (cmd == F_SETLK64)
548 else if (cmd == F_SETLKW64)
550 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
553 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
556 if ((code == 0 || flp->fl_type == F_UNLCK) &&
557 (cmd == F_SETLK || cmd == F_SETLKW)) {
558 code = afs_posix_lock_file(fp, flp);
559 if (code && flp->fl_type != F_UNLCK) {
560 struct AFS_FLOCK flock2;
562 flock2.l_type = F_UNLCK;
564 afs_lockctl(vcp, &flock2, F_SETLK, credp);
568 /* If lockctl says there are no conflicting locks, then also check with the
569 * kernel, as lockctl knows nothing about byte range locks
571 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
572 afs_posix_test_lock(fp, flp);
573 /* If we found a lock in the kernel's structure, return it */
574 if (flp->fl_type != F_UNLCK) {
580 /* Convert flock back to Linux's file_lock */
581 flp->fl_type = flock.l_type;
582 flp->fl_pid = flock.l_pid;
583 flp->fl_start = flock.l_start;
584 if (flock.l_len == 0)
585 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
587 flp->fl_end = flock.l_start + flock.l_len - 1;
593 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
595 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
597 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
598 cred_t *credp = crref();
599 struct AFS_FLOCK flock;
600 /* Convert to a lock format afs_lockctl understands. */
601 memset(&flock, 0, sizeof(flock));
602 flock.l_type = flp->fl_type;
603 flock.l_pid = flp->fl_pid;
608 /* Safe because there are no large files, yet */
609 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
610 if (cmd == F_GETLK64)
612 else if (cmd == F_SETLK64)
614 else if (cmd == F_SETLKW64)
616 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
619 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
622 if ((code == 0 || flp->fl_type == F_UNLCK) &&
623 (cmd == F_SETLK || cmd == F_SETLKW)) {
624 flp->fl_flags &=~ FL_SLEEP;
625 code = flock_lock_file_wait(fp, flp);
626 if (code && flp->fl_type != F_UNLCK) {
627 struct AFS_FLOCK flock2;
629 flock2.l_type = F_UNLCK;
631 afs_lockctl(vcp, &flock2, F_SETLK, credp);
635 /* Convert flock back to Linux's file_lock */
636 flp->fl_type = flock.l_type;
637 flp->fl_pid = flock.l_pid;
645 * essentially the same as afs_fsync() but we need to get the return
646 * code for the sys_close() here, not afs_linux_release(), so call
647 * afs_StoreAllSegments() with AFS_LASTSTORE
650 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
651 afs_linux_flush(struct file *fp, fl_owner_t id)
653 afs_linux_flush(struct file *fp)
656 struct vrequest treq;
664 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
672 vcp = VTOAFS(FILE_INODE(fp));
674 code = afs_InitReq(&treq, credp);
677 /* If caching is bypassed for this file, or globally, just return 0 */
678 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
681 ObtainReadLock(&vcp->lock);
682 if (vcp->cachingStates & FCSBypass)
684 ReleaseReadLock(&vcp->lock);
687 /* future proof: don't rely on 0 return from afs_InitReq */
692 ObtainSharedLock(&vcp->lock, 535);
693 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
694 UpgradeSToWLock(&vcp->lock, 536);
695 if (!AFS_IS_DISCONNECTED) {
696 code = afs_StoreAllSegments(vcp,
698 AFS_SYNC | AFS_LASTSTORE);
700 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
702 ConvertWToSLock(&vcp->lock);
704 code = afs_CheckCode(code, &treq, 54);
705 ReleaseSharedLock(&vcp->lock);
712 return afs_convert_code(code);
715 struct file_operations afs_dir_fops = {
716 .read = generic_read_dir,
717 .readdir = afs_linux_readdir,
718 #ifdef HAVE_UNLOCKED_IOCTL
719 .unlocked_ioctl = afs_unlocked_xioctl,
723 #ifdef HAVE_COMPAT_IOCTL
724 .compat_ioctl = afs_unlocked_xioctl,
726 .open = afs_linux_open,
727 .release = afs_linux_release,
728 .llseek = default_llseek,
731 struct file_operations afs_file_fops = {
732 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
733 .aio_read = afs_linux_aio_read,
734 .aio_write = afs_linux_aio_write,
736 .read = afs_linux_read,
737 .write = afs_linux_write,
739 #ifdef HAVE_UNLOCKED_IOCTL
740 .unlocked_ioctl = afs_unlocked_xioctl,
744 #ifdef HAVE_COMPAT_IOCTL
745 .compat_ioctl = afs_unlocked_xioctl,
747 .mmap = afs_linux_mmap,
748 .open = afs_linux_open,
749 .flush = afs_linux_flush,
750 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
751 .sendfile = generic_file_sendfile,
753 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
754 .splice_write = generic_file_splice_write,
755 .splice_read = generic_file_splice_read,
757 .release = afs_linux_release,
758 .fsync = afs_linux_fsync,
759 .lock = afs_linux_lock,
760 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
761 .flock = afs_linux_flock,
763 .llseek = default_llseek,
767 /**********************************************************************
768 * AFS Linux dentry operations
769 **********************************************************************/
771 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
772 * that has its mvid (parent dir's fid) pointer set to the wrong directory
773 * due to being mounted in multiple points at once. fix_bad_parent()
774 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
775 * dotdotfid and mtpoint fid members.
777 * dp - dentry to be checked.
778 * credp - credentials
779 * vcp, pvc - item's and parent's vcache pointer
783 * This dentry's vcache's mvid will be set to the correct parent directory's
785 * This root vnode's volume will have its dotdotfid and mtpoint fids set
786 * to the correct parent and mountpoint fids.
790 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
792 struct vcache *avc = NULL;
794 /* force a lookup, so vcp->mvid is fixed up */
795 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
796 if (!avc || vcp != avc) { /* bad, very bad.. */
797 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
798 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
799 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
800 ICL_TYPE_POINTER, dp);
803 AFS_RELE(AFSTOV(avc));
808 /* afs_linux_revalidate
809 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
812 afs_linux_revalidate(struct dentry *dp)
815 struct vcache *vcp = VTOAFS(dp->d_inode);
819 if (afs_shuttingdown)
825 /* Make this a fast path (no crref), since it's called so often. */
826 if (vcp->states & CStatd) {
827 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
829 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
830 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
833 fix_bad_parent(dp); /* check and correct mvid */
842 /* This avoids the crref when we don't have to do it. Watch for
843 * changes in afs_getattr that don't get replicated here!
845 if (vcp->f.states & CStatd &&
846 (!afs_fakestat_enable || vcp->mvstat != 1) &&
848 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
849 code = afs_CopyOutAttrs(vcp, &vattr);
852 code = afs_getattr(vcp, &vattr, credp);
857 afs_fill_inode(AFSTOV(vcp), &vattr);
861 return afs_convert_code(code);
865 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
867 int err = afs_linux_revalidate(dentry);
869 generic_fillattr(dentry->d_inode, stat);
874 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
875 * In kernels 2.2.10 and above, we are passed an additional flags var which
876 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
877 * we are advised to follow the entry if it is a link or to make sure that
878 * it is a directory. But since the kernel itself checks these possibilities
879 * later on, we shouldn't have to do it until later. Perhaps in the future..
881 * The code here assumes that on entry the global lock is not held
884 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
885 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
887 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
891 cred_t *credp = NULL;
892 struct vcache *vcp, *pvcp, *tvc = NULL;
893 struct dentry *parent;
895 struct afs_fakestat_state fakestate;
899 /* We don't support RCU path walking */
900 if (nd->flags & LOOKUP_RCU)
904 afs_InitFakeStat(&fakestate);
907 vcp = VTOAFS(dp->d_inode);
909 if (vcp == afs_globalVp)
912 parent = dget_parent(dp);
913 pvcp = VTOAFS(parent->d_inode);
915 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
921 if (locked && vcp->mvstat == 1) { /* mount point */
922 if (vcp->mvid && (vcp->f.states & CMValid)) {
925 struct vrequest treq;
927 code = afs_InitReq(&treq, credp);
929 (strcmp(dp->d_name.name, ".directory") == 0)) {
933 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
935 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
936 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
937 /* a mount point, not yet replaced by its directory */
942 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
943 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
944 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
949 /* If the last looker changes, we should make sure the current
950 * looker still has permission to examine this file. This would
951 * always require a crref() which would be "slow".
953 if (vcp->last_looker != treq.uid) {
954 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
957 vcp->last_looker = treq.uid;
962 /* If the parent's DataVersion has changed or the vnode
963 * is longer valid, we need to do a full lookup. VerifyVCache
964 * isn't enough since the vnode may have been renamed.
967 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
973 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
974 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
975 if (!tvc || tvc != vcp) {
980 if (afs_getattr(vcp, &vattr, credp)) {
985 vattr2inode(AFSTOV(vcp), &vattr);
986 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
989 /* should we always update the attributes at this point? */
990 /* unlikely--the vcache entry hasn't changed */
995 /* If this code is ever enabled, we should use dget_parent to handle
996 * getting the parent, and dput() to dispose of it. See above for an
998 pvcp = VTOAFS(dp->d_parent->d_inode);
999 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1003 /* No change in parent's DataVersion so this negative
1004 * lookup is still valid. BUT, if a server is down a
1005 * negative lookup can result so there should be a
1006 * liftime as well. For now, always expire.
1019 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1021 /* we hold the global lock if we evaluated a mount point */
1028 shrink_dcache_parent(dp);
1034 if (have_submounts(dp))
1042 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1044 struct vcache *vcp = VTOAFS(ip);
1047 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1048 (void) afs_InactiveVCache(vcp, NULL);
1051 afs_linux_clear_nfsfs_renamed(dp);
1057 afs_dentry_delete(struct dentry *dp)
1059 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1060 return 1; /* bad inode? */
1065 struct dentry_operations afs_dentry_operations = {
1066 .d_revalidate = afs_linux_dentry_revalidate,
1067 .d_delete = afs_dentry_delete,
1068 .d_iput = afs_dentry_iput,
1071 /**********************************************************************
1072 * AFS Linux inode operations
1073 **********************************************************************/
1077 * Merely need to set enough of vattr to get us through the create. Note
1078 * that the higher level code (open_namei) will take care of any tuncation
1079 * explicitly. Exclusive open is also taken care of in open_namei.
1081 * name is in kernel space at this point.
1084 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1085 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1086 struct nameidata *nd)
1088 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1092 cred_t *credp = crref();
1093 const char *name = dp->d_name.name;
1098 vattr.va_mode = mode;
1099 vattr.va_type = mode & S_IFMT;
1102 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1106 struct inode *ip = AFSTOV(vcp);
1108 afs_getattr(vcp, &vattr, credp);
1109 afs_fill_inode(ip, &vattr);
1110 insert_inode_hash(ip);
1111 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1112 dp->d_op = &afs_dentry_operations;
1114 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1115 d_instantiate(dp, ip);
1120 return afs_convert_code(code);
1123 /* afs_linux_lookup */
1124 static struct dentry *
1125 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1126 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1127 struct nameidata *nd)
1129 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1132 cred_t *credp = crref();
1133 struct vcache *vcp = NULL;
1134 const char *comp = dp->d_name.name;
1135 struct inode *ip = NULL;
1136 struct dentry *newdp = NULL;
1140 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1146 afs_getattr(vcp, &vattr, credp);
1147 afs_fill_inode(ip, &vattr);
1148 if (hlist_unhashed(&ip->i_hash))
1149 insert_inode_hash(ip);
1151 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1152 dp->d_op = &afs_dentry_operations;
1154 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1157 if (ip && S_ISDIR(ip->i_mode)) {
1158 struct dentry *alias;
1160 /* Try to invalidate an existing alias in favor of our new one */
1161 alias = d_find_alias(ip);
1162 /* But not if it's disconnected; then we want d_splice_alias below */
1163 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1164 if (d_invalidate(alias) == 0) {
1173 newdp = d_splice_alias(ip, dp);
1177 /* It's ok for the file to not be found. That's noted by the caller by
1178 * seeing that the dp->d_inode field is NULL.
1180 if (!code || code == ENOENT)
1183 return ERR_PTR(afs_convert_code(code));
1187 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1190 cred_t *credp = crref();
1191 const char *name = newdp->d_name.name;
1192 struct inode *oldip = olddp->d_inode;
1194 /* If afs_link returned the vnode, we could instantiate the
1195 * dentry. Since it's not, we drop this one and do a new lookup.
1200 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1204 return afs_convert_code(code);
1207 /* We have to have a Linux specific sillyrename function, because we
1208 * also have to keep the dcache up to date when we're doing a silly
1209 * rename - so we don't want the generic vnodeops doing this behind our
1214 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1217 struct vcache *tvc = VTOAFS(dentry->d_inode);
1218 struct dentry *__dp = NULL;
1219 char *__name = NULL;
1222 if (afs_linux_nfsfs_renamed(dentry))
1230 osi_FreeSmallSpace(__name);
1231 __name = afs_newname();
1234 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1237 osi_FreeSmallSpace(__name);
1240 } while (__dp->d_inode != NULL);
1243 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1244 VTOAFS(dir), (char *)__dp->d_name.name,
1247 tvc->mvid = (void *) __name;
1250 crfree(tvc->uncred);
1252 tvc->uncred = credp;
1253 tvc->f.states |= CUnlinked;
1254 afs_linux_set_nfsfs_renamed(dentry);
1256 osi_FreeSmallSpace(__name);
1261 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1262 d_move(dentry, __dp);
1271 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1274 cred_t *credp = crref();
1275 const char *name = dp->d_name.name;
1276 struct vcache *tvc = VTOAFS(dp->d_inode);
1278 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1279 && !(tvc->f.states & CUnlinked)) {
1281 code = afs_linux_sillyrename(dip, dp, credp);
1284 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1291 return afs_convert_code(code);
1296 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1299 cred_t *credp = crref();
1301 const char *name = dp->d_name.name;
1303 /* If afs_symlink returned the vnode, we could instantiate the
1304 * dentry. Since it's not, we drop this one and do a new lookup.
1310 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1313 return afs_convert_code(code);
1317 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1320 cred_t *credp = crref();
1321 struct vcache *tvcp = NULL;
1323 const char *name = dp->d_name.name;
1326 vattr.va_mask = ATTR_MODE;
1327 vattr.va_mode = mode;
1329 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1332 struct inode *ip = AFSTOV(tvcp);
1334 afs_getattr(tvcp, &vattr, credp);
1335 afs_fill_inode(ip, &vattr);
1337 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1338 dp->d_op = &afs_dentry_operations;
1340 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1341 d_instantiate(dp, ip);
1346 return afs_convert_code(code);
1350 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1353 cred_t *credp = crref();
1354 const char *name = dp->d_name.name;
1356 /* locking kernel conflicts with glock? */
1359 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1362 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1363 * that failed because a directory is not empty. So, we map
1364 * EEXIST to ENOTEMPTY on linux.
1366 if (code == EEXIST) {
1375 return afs_convert_code(code);
1380 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1381 struct inode *newip, struct dentry *newdp)
1384 cred_t *credp = crref();
1385 const char *oldname = olddp->d_name.name;
1386 const char *newname = newdp->d_name.name;
1387 struct dentry *rehash = NULL;
1389 /* Prevent any new references during rename operation. */
1391 if (!d_unhashed(newdp)) {
1396 #if defined(D_COUNT_INT)
1397 spin_lock(&olddp->d_lock);
1398 if (olddp->d_count > 1) {
1399 spin_unlock(&olddp->d_lock);
1400 shrink_dcache_parent(olddp);
1402 spin_unlock(&olddp->d_lock);
1404 if (atomic_read(&olddp->d_count) > 1)
1405 shrink_dcache_parent(olddp);
1409 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1413 olddp->d_time = 0; /* force to revalidate */
1419 return afs_convert_code(code);
1423 /* afs_linux_ireadlink
1424 * Internal readlink which can return link contents to user or kernel space.
1425 * Note that the buffer is NOT supposed to be null-terminated.
1428 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1431 cred_t *credp = crref();
1435 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1436 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1440 return maxlen - tuio.uio_resid;
1442 return afs_convert_code(code);
1445 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1446 /* afs_linux_readlink
1447 * Fill target (which is in user space) with contents of symlink.
1450 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1453 struct inode *ip = dp->d_inode;
1456 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1462 /* afs_linux_follow_link
1463 * a file system dependent link following routine.
1465 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1470 name = kmalloc(PATH_MAX, GFP_NOFS);
1476 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1484 nd_set_link(nd, name);
1489 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1491 char *name = nd_get_link(nd);
1493 if (name && !IS_ERR(name))
1497 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1499 /* Populate a page by filling it from the cache file pointed at by cachefp
1500 * (which contains indicated chunk)
1501 * If task is NULL, the page copy occurs syncronously, and the routine
1502 * returns with page still locked. If task is non-NULL, then page copies
1503 * may occur in the background, and the page will be unlocked when it is
1507 afs_linux_read_cache(struct file *cachefp, struct page *page,
1508 int chunk, struct pagevec *lrupv,
1509 struct afs_pagecopy_task *task) {
1510 loff_t offset = page_offset(page);
1511 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1512 struct page *newpage, *cachepage;
1513 struct address_space *cachemapping;
1517 cachemapping = cacheinode->i_mapping;
1521 /* If we're trying to read a page that's past the end of the disk
1522 * cache file, then just return a zeroed page */
1523 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1524 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1525 SetPageUptodate(page);
1531 /* From our offset, we now need to work out which page in the disk
1532 * file it corresponds to. This will be fun ... */
1533 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1535 while (cachepage == NULL) {
1536 cachepage = find_get_page(cachemapping, pageindex);
1539 newpage = page_cache_alloc_cold(cachemapping);
1545 code = add_to_page_cache(newpage, cachemapping,
1546 pageindex, GFP_KERNEL);
1548 cachepage = newpage;
1551 page_cache_get(cachepage);
1552 if (!pagevec_add(lrupv, cachepage))
1553 __pagevec_lru_add_file(lrupv);
1556 page_cache_release(newpage);
1558 if (code != -EEXIST)
1562 lock_page(cachepage);
1566 if (!PageUptodate(cachepage)) {
1567 ClearPageError(cachepage);
1568 code = cachemapping->a_ops->readpage(NULL, cachepage);
1569 if (!code && !task) {
1570 wait_on_page_locked(cachepage);
1573 unlock_page(cachepage);
1577 if (PageUptodate(cachepage)) {
1578 copy_highpage(page, cachepage);
1579 flush_dcache_page(page);
1580 SetPageUptodate(page);
1585 afs_pagecopy_queue_page(task, cachepage, page);
1597 page_cache_release(cachepage);
1603 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1605 loff_t offset = page_offset(pp);
1606 struct inode *ip = FILE_INODE(fp);
1607 struct vcache *avc = VTOAFS(ip);
1609 struct file *cacheFp = NULL;
1612 struct pagevec lrupv;
1614 /* Not a UFS cache, don't do anything */
1615 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1618 /* Can't do anything if the vcache isn't statd , or if the read
1619 * crosses a chunk boundary.
1621 if (!(avc->f.states & CStatd) ||
1622 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1626 ObtainWriteLock(&avc->lock, 911);
1628 /* XXX - See if hinting actually makes things faster !!! */
1630 /* See if we have a suitable entry already cached */
1634 /* We need to lock xdcache, then dcache, to handle situations where
1635 * the hint is on the free list. However, we can't safely do this
1636 * according to the locking hierarchy. So, use a non blocking lock.
1638 ObtainReadLock(&afs_xdcache);
1639 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1641 if (dcLocked && (tdc->index != NULLIDX)
1642 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1643 && tdc->f.chunk == AFS_CHUNK(offset)
1644 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1645 /* Bonus - the hint was correct */
1648 /* Only destroy the hint if its actually invalid, not if there's
1649 * just been a locking failure */
1651 ReleaseReadLock(&tdc->lock);
1658 ReleaseReadLock(&afs_xdcache);
1661 /* No hint, or hint is no longer valid - see if we can get something
1662 * directly from the dcache
1665 tdc = afs_FindDCache(avc, offset);
1668 ReleaseWriteLock(&avc->lock);
1673 ObtainReadLock(&tdc->lock);
1675 /* Is the dcache we've been given currently up to date */
1676 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1677 (tdc->dflags & DFFetching)) {
1678 ReleaseWriteLock(&avc->lock);
1679 ReleaseReadLock(&tdc->lock);
1684 /* Update our hint for future abuse */
1687 /* Okay, so we've now got a cache file that is up to date */
1689 /* XXX - I suspect we should be locking the inodes before we use them! */
1691 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1692 pagevec_init(&lrupv, 0);
1694 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1696 if (pagevec_count(&lrupv))
1697 __pagevec_lru_add_file(&lrupv);
1699 filp_close(cacheFp, NULL);
1702 ReleaseReadLock(&tdc->lock);
1703 ReleaseWriteLock(&avc->lock);
1710 /* afs_linux_readpage
1712 * This function is split into two, because prepare_write/begin_write
1713 * require a readpage call which doesn't unlock the resulting page upon
1717 afs_linux_fillpage(struct file *fp, struct page *pp)
1722 struct iovec *iovecp;
1723 struct inode *ip = FILE_INODE(fp);
1724 afs_int32 cnt = page_count(pp);
1725 struct vcache *avc = VTOAFS(ip);
1726 afs_offs_t offset = page_offset(pp);
1730 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1740 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
1741 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
1743 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1748 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1749 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1750 99999); /* not a possible code value */
1752 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1754 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1755 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1757 AFS_DISCON_UNLOCK();
1760 /* XXX valid for no-cache also? Check last bits of files... :)
1761 * Cognate code goes in afs_NoCacheFetchProc. */
1762 if (auio->uio_resid) /* zero remainder of page */
1763 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1766 flush_dcache_page(pp);
1767 SetPageUptodate(pp);
1776 return afs_convert_code(code);
1780 afs_linux_prefetch(struct file *fp, struct page *pp)
1783 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1784 afs_offs_t offset = page_offset(pp);
1786 if (AFS_CHUNKOFFSET(offset) == 0) {
1788 struct vrequest treq;
1793 code = afs_InitReq(&treq, credp);
1794 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1795 tdc = afs_FindDCache(avc, offset);
1797 if (!(tdc->mflags & DFNextStarted))
1798 afs_PrefetchChunk(avc, tdc, credp, &treq);
1801 ReleaseWriteLock(&avc->lock);
1806 return afs_convert_code(code);
1811 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1812 struct list_head *page_list, unsigned num_pages)
1817 struct iovec* iovecp;
1818 struct nocache_read_request *ancr;
1820 struct pagevec lrupv;
1824 struct inode *ip = FILE_INODE(fp);
1825 struct vcache *avc = VTOAFS(ip);
1826 afs_int32 base_index = 0;
1827 afs_int32 page_count = 0;
1830 /* background thread must free: iovecp, auio, ancr */
1831 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1833 auio = osi_Alloc(sizeof(struct uio));
1834 auio->uio_iov = iovecp;
1835 auio->uio_iovcnt = num_pages;
1836 auio->uio_flag = UIO_READ;
1837 auio->uio_seg = AFS_UIOSYS;
1838 auio->uio_resid = num_pages * PAGE_SIZE;
1840 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1842 ancr->offset = auio->uio_offset;
1843 ancr->length = auio->uio_resid;
1845 pagevec_init(&lrupv, 0);
1847 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1849 if(list_empty(page_list))
1852 pp = list_entry(page_list->prev, struct page, lru);
1853 /* If we allocate a page and don't remove it from page_list,
1854 * the page cache gets upset. */
1856 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1857 if(pp->index > isize) {
1864 offset = page_offset(pp);
1865 auio->uio_offset = offset;
1866 base_index = pp->index;
1868 iovecp[page_ix].iov_len = PAGE_SIZE;
1869 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1870 if(base_index != pp->index) {
1873 page_cache_release(pp);
1874 iovecp[page_ix].iov_base = (void *) 0;
1876 ancr->length -= PAGE_SIZE;
1883 page_cache_release(pp);
1884 iovecp[page_ix].iov_base = (void *) 0;
1887 if(!PageLocked(pp)) {
1891 /* increment page refcount--our original design assumed
1892 * that locking it would effectively pin it; protect
1893 * ourselves from the possiblity that this assumption is
1894 * is faulty, at low cost (provided we do not fail to
1895 * do the corresponding decref on the other side) */
1898 /* save the page for background map */
1899 iovecp[page_ix].iov_base = (void*) pp;
1901 /* and put it on the LRU cache */
1902 if (!pagevec_add(&lrupv, pp))
1903 __pagevec_lru_add_file(&lrupv);
1907 /* If there were useful pages in the page list, make sure all pages
1908 * are in the LRU cache, then schedule the read */
1910 if (pagevec_count(&lrupv))
1911 __pagevec_lru_add_file(&lrupv);
1913 code = afs_ReadNoCache(avc, ancr, credp);
1916 /* If there is nothing for the background thread to handle,
1917 * it won't be freeing the things that we never gave it */
1918 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1919 osi_Free(auio, sizeof(struct uio));
1920 osi_Free(ancr, sizeof(struct nocache_read_request));
1922 /* we do not flush, release, or unmap pages--that will be
1923 * done for us by the background thread as each page comes in
1924 * from the fileserver */
1925 return afs_convert_code(code);
1930 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1932 cred_t *credp = NULL;
1934 struct iovec *iovecp;
1935 struct nocache_read_request *ancr;
1939 * Special case: if page is at or past end of file, just zero it and set
1942 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1943 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1944 SetPageUptodate(pp);
1951 /* receiver frees */
1952 auio = osi_Alloc(sizeof(struct uio));
1953 iovecp = osi_Alloc(sizeof(struct iovec));
1955 /* address can be NULL, because we overwrite it with 'pp', below */
1956 setup_uio(auio, iovecp, NULL, page_offset(pp),
1957 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1959 /* save the page for background map */
1960 get_page(pp); /* see above */
1961 auio->uio_iov->iov_base = (void*) pp;
1962 /* the background thread will free this */
1963 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1965 ancr->offset = page_offset(pp);
1966 ancr->length = PAGE_SIZE;
1969 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1972 return afs_convert_code(code);
1976 afs_linux_can_bypass(struct inode *ip) {
1977 switch(cache_bypass_strategy) {
1978 case NEVER_BYPASS_CACHE:
1980 case ALWAYS_BYPASS_CACHE:
1982 case LARGE_FILES_BYPASS_CACHE:
1983 if(i_size_read(ip) > cache_bypass_threshold)
1990 /* Check if a file is permitted to bypass the cache by policy, and modify
1991 * the cache bypass state recorded for that file */
1994 afs_linux_bypass_check(struct inode *ip) {
1997 int bypass = afs_linux_can_bypass(ip);
2000 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2008 afs_linux_readpage(struct file *fp, struct page *pp)
2012 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2013 code = afs_linux_bypass_readpage(fp, pp);
2015 code = afs_linux_fillpage(fp, pp);
2017 code = afs_linux_prefetch(fp, pp);
2024 /* Readpages reads a number of pages for a particular file. We use
2025 * this to optimise the reading, by limiting the number of times upon which
2026 * we have to lookup, lock and open vcaches and dcaches
2030 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2031 struct list_head *page_list, unsigned int num_pages)
2033 struct inode *inode = mapping->host;
2034 struct vcache *avc = VTOAFS(inode);
2036 struct file *cacheFp = NULL;
2038 unsigned int page_idx;
2040 struct pagevec lrupv;
2041 struct afs_pagecopy_task *task;
2043 if (afs_linux_bypass_check(inode))
2044 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2046 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2050 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2055 ObtainWriteLock(&avc->lock, 912);
2058 task = afs_pagecopy_init_task();
2061 pagevec_init(&lrupv, 0);
2062 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2063 struct page *page = list_entry(page_list->prev, struct page, lru);
2064 list_del(&page->lru);
2065 offset = page_offset(page);
2067 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2069 ReleaseReadLock(&tdc->lock);
2074 filp_close(cacheFp, NULL);
2079 if ((tdc = afs_FindDCache(avc, offset))) {
2080 ObtainReadLock(&tdc->lock);
2081 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2082 (tdc->dflags & DFFetching)) {
2083 ReleaseReadLock(&tdc->lock);
2090 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2093 if (tdc && !add_to_page_cache(page, mapping, page->index,
2095 page_cache_get(page);
2096 if (!pagevec_add(&lrupv, page))
2097 __pagevec_lru_add_file(&lrupv);
2099 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2101 page_cache_release(page);
2103 if (pagevec_count(&lrupv))
2104 __pagevec_lru_add_file(&lrupv);
2107 filp_close(cacheFp, NULL);
2109 afs_pagecopy_put_task(task);
2113 ReleaseReadLock(&tdc->lock);
2117 ReleaseWriteLock(&avc->lock);
2122 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2125 afs_linux_prepare_writeback(struct vcache *avc) {
2126 if (avc->f.states & CPageWrite) {
2127 return AOP_WRITEPAGE_ACTIVATE;
2129 avc->f.states |= CPageWrite;
2134 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2135 struct vrequest treq;
2138 if (!afs_InitReq(&treq, credp))
2139 code = afs_DoPartialWrite(avc, &treq);
2141 return afs_convert_code(code);
2145 afs_linux_complete_writeback(struct vcache *avc) {
2146 avc->f.states &= ~CPageWrite;
2149 /* Writeback a given page syncronously. Called with no AFS locks held */
2151 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2152 unsigned long offset, unsigned int count,
2155 struct vcache *vcp = VTOAFS(ip);
2163 buffer = kmap(pp) + offset;
2164 base = page_offset(pp) + offset;
2167 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2168 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2169 ICL_TYPE_INT32, 99999);
2171 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2173 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2175 i_size_write(ip, vcp->f.m.Length);
2176 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2178 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2180 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2181 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2182 ICL_TYPE_INT32, code);
2191 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2192 unsigned long offset, unsigned int count)
2196 struct vcache *vcp = VTOAFS(ip);
2199 /* Catch recursive writeback. This occurs if the kernel decides
2200 * writeback is required whilst we are writing to the cache, or
2201 * flushing to the server. When we're running syncronously (as
2202 * opposed to from writepage) we can't actually do anything about
2203 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2206 ObtainWriteLock(&vcp->lock, 532);
2207 afs_linux_prepare_writeback(vcp);
2208 ReleaseWriteLock(&vcp->lock);
2212 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2215 ObtainWriteLock(&vcp->lock, 533);
2217 code1 = afs_linux_dopartialwrite(vcp, credp);
2218 afs_linux_complete_writeback(vcp);
2219 ReleaseWriteLock(&vcp->lock);
2230 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2231 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2233 afs_linux_writepage(struct page *pp)
2236 struct address_space *mapping = pp->mapping;
2237 struct inode *inode;
2240 unsigned int to = PAGE_CACHE_SIZE;
2245 if (PageReclaim(pp)) {
2246 return AOP_WRITEPAGE_ACTIVATE;
2247 /* XXX - Do we need to redirty the page here? */
2252 inode = mapping->host;
2253 vcp = VTOAFS(inode);
2254 isize = i_size_read(inode);
2256 /* Don't defeat an earlier truncate */
2257 if (page_offset(pp) > isize) {
2258 set_page_writeback(pp);
2264 ObtainWriteLock(&vcp->lock, 537);
2265 code = afs_linux_prepare_writeback(vcp);
2266 if (code == AOP_WRITEPAGE_ACTIVATE) {
2267 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2268 * to return with the page still locked */
2269 ReleaseWriteLock(&vcp->lock);
2274 /* Grab the creds structure currently held in the vnode, and
2275 * get a reference to it, in case it goes away ... */
2281 ReleaseWriteLock(&vcp->lock);
2284 set_page_writeback(pp);
2286 SetPageUptodate(pp);
2288 /* We can unlock the page here, because it's protected by the
2289 * page_writeback flag. This should make us less vulnerable to
2290 * deadlocking in afs_write and afs_DoPartialWrite
2294 /* If this is the final page, then just write the number of bytes that
2295 * are actually in it */
2296 if ((isize - page_offset(pp)) < to )
2297 to = isize - page_offset(pp);
2299 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2302 ObtainWriteLock(&vcp->lock, 538);
2304 /* As much as we might like to ignore a file server error here,
2305 * and just try again when we close(), unfortunately StoreAllSegments
2306 * will invalidate our chunks if the server returns a permanent error,
2307 * so we need to at least try and get that error back to the user
2310 code1 = afs_linux_dopartialwrite(vcp, credp);
2312 afs_linux_complete_writeback(vcp);
2313 ReleaseWriteLock(&vcp->lock);
2318 end_page_writeback(pp);
2319 page_cache_release(pp);
2330 /* afs_linux_permission
2331 * Check access rights - returns error if can't check or permission denied.
2334 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2335 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2336 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2337 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2339 afs_linux_permission(struct inode *ip, int mode)
2346 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2347 /* We don't support RCU path walking */
2348 if (flags & IPERM_FLAG_RCU)
2354 if (mode & MAY_EXEC)
2356 if (mode & MAY_READ)
2358 if (mode & MAY_WRITE)
2360 code = afs_access(VTOAFS(ip), tmp, credp);
2364 return afs_convert_code(code);
2368 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2372 struct inode *inode = FILE_INODE(file);
2373 loff_t pagebase = page_offset(page);
2375 if (i_size_read(inode) < (pagebase + offset))
2376 i_size_write(inode, pagebase + offset);
2378 if (PageChecked(page)) {
2379 SetPageUptodate(page);
2380 ClearPageChecked(page);
2383 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2389 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2393 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2394 * prepare_write. Essentially, if the page exists within the file,
2395 * and is not being fully written, then we should populate it.
2398 if (!PageUptodate(page)) {
2399 loff_t pagebase = page_offset(page);
2400 loff_t isize = i_size_read(page->mapping->host);
2402 /* Is the location we are writing to beyond the end of the file? */
2403 if (pagebase >= isize ||
2404 ((from == 0) && (pagebase + to) >= isize)) {
2405 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2406 SetPageChecked(page);
2407 /* Are we we writing a full page */
2408 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2409 SetPageChecked(page);
2410 /* Is the page readable, if it's wronly, we don't care, because we're
2411 * not actually going to read from it ... */
2412 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2413 /* We don't care if fillpage fails, because if it does the page
2414 * won't be marked as up to date
2416 afs_linux_fillpage(file, page);
2422 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2424 afs_linux_write_end(struct file *file, struct address_space *mapping,
2425 loff_t pos, unsigned len, unsigned copied,
2426 struct page *page, void *fsdata)
2429 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2431 code = afs_linux_commit_write(file, page, from, from + len);
2434 page_cache_release(page);
2439 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2440 loff_t pos, unsigned len, unsigned flags,
2441 struct page **pagep, void **fsdata)
2444 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2445 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2448 page = grab_cache_page_write_begin(mapping, index, flags);
2451 code = afs_linux_prepare_write(file, page, from, from + len);
2454 page_cache_release(page);
2462 static struct inode_operations afs_file_iops = {
2463 .permission = afs_linux_permission,
2464 .getattr = afs_linux_getattr,
2465 .setattr = afs_notify_change,
2468 static struct address_space_operations afs_file_aops = {
2469 .readpage = afs_linux_readpage,
2470 .readpages = afs_linux_readpages,
2471 .writepage = afs_linux_writepage,
2472 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2473 .write_begin = afs_linux_write_begin,
2474 .write_end = afs_linux_write_end,
2476 .commit_write = afs_linux_commit_write,
2477 .prepare_write = afs_linux_prepare_write,
2482 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2483 * by what sort of operation is allowed.....
2486 static struct inode_operations afs_dir_iops = {
2487 .setattr = afs_notify_change,
2488 .create = afs_linux_create,
2489 .lookup = afs_linux_lookup,
2490 .link = afs_linux_link,
2491 .unlink = afs_linux_unlink,
2492 .symlink = afs_linux_symlink,
2493 .mkdir = afs_linux_mkdir,
2494 .rmdir = afs_linux_rmdir,
2495 .rename = afs_linux_rename,
2496 .getattr = afs_linux_getattr,
2497 .permission = afs_linux_permission,
2500 /* We really need a separate symlink set of ops, since do_follow_link()
2501 * determines if it _is_ a link by checking if the follow_link op is set.
2503 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2505 afs_symlink_filler(struct file *file, struct page *page)
2507 struct inode *ip = (struct inode *)page->mapping->host;
2508 char *p = (char *)kmap(page);
2512 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2517 p[code] = '\0'; /* null terminate? */
2519 SetPageUptodate(page);
2531 static struct address_space_operations afs_symlink_aops = {
2532 .readpage = afs_symlink_filler
2534 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2536 static struct inode_operations afs_symlink_iops = {
2537 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2538 .readlink = page_readlink,
2539 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2540 .follow_link = page_follow_link,
2542 .follow_link = page_follow_link_light,
2543 .put_link = page_put_link,
2545 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2546 .readlink = afs_linux_readlink,
2547 .follow_link = afs_linux_follow_link,
2548 .put_link = afs_linux_put_link,
2549 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2550 .setattr = afs_notify_change,
2554 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2558 vattr2inode(ip, vattr);
2560 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2561 /* Reset ops if symlink or directory. */
2562 if (S_ISREG(ip->i_mode)) {
2563 ip->i_op = &afs_file_iops;
2564 ip->i_fop = &afs_file_fops;
2565 ip->i_data.a_ops = &afs_file_aops;
2567 } else if (S_ISDIR(ip->i_mode)) {
2568 ip->i_op = &afs_dir_iops;
2569 ip->i_fop = &afs_dir_fops;
2571 } else if (S_ISLNK(ip->i_mode)) {
2572 ip->i_op = &afs_symlink_iops;
2573 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2574 ip->i_data.a_ops = &afs_symlink_aops;
2575 ip->i_mapping = &ip->i_data;