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/smp_lock.h>
34 #include <linux/writeback.h>
35 #include <linux/pagevec.h>
36 #if defined(AFS_CACHE_BYPASS)
38 #include "afs/afs_bypasscache.h"
41 #include "osi_compat.h"
42 #include "osi_pagecopy.h"
44 #ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
45 #define __pagevec_lru_add_file __pagevec_lru_add
49 #define MAX_ERRNO 1000L
52 extern struct backing_dev_info *afs_backing_dev_info;
54 extern struct vcache *afs_globalVp;
55 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
56 /* Some uses of BKL are perhaps not needed for bypass or memcache--
57 * why don't we try it out? */
58 extern struct afs_cacheOps afs_UfsCacheOps;
61 afs_maybe_lock_kernel(void) {
62 if(afs_cacheType == &afs_UfsCacheOps)
67 afs_maybe_unlock_kernel(void) {
68 if(afs_cacheType == &afs_UfsCacheOps)
72 /* This function converts a positive error code from AFS into a negative
73 * code suitable for passing into the Linux VFS layer. It checks that the
74 * error code is within the permissable bounds for the ERR_PTR mechanism.
76 * _All_ error codes which come from the AFS layer should be passed through
77 * this function before being returned to the kernel.
81 afs_convert_code(int code) {
82 if ((code >= 0) && (code <= MAX_ERRNO))
88 /* Linux doesn't require a credp for many functions, and crref is an expensive
89 * operation. This helper function avoids obtaining it for VerifyVCache calls
93 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
98 if (avc->f.states & CStatd) {
106 code = afs_InitReq(&treq, credp);
108 code = afs_VerifyVCache2(avc, &treq);
115 return afs_convert_code(code);
119 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
122 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
125 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
126 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
128 code = afs_linux_VerifyVCache(vcp, NULL);
131 /* Linux's FlushPages implementation doesn't ever use credp,
132 * so we optimise by not using it */
133 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
135 code = do_sync_read(fp, buf, count, offp);
139 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
140 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
147 /* Now we have integrated VM for writes as well as reads. generic_file_write
148 * also takes care of re-positioning the pointer if file is open in append
149 * mode. Call fake open/close to ensure we do writes of core dumps.
152 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
155 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
160 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
161 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
162 (fp->f_flags & O_APPEND) ? 99998 : 99999);
164 code = afs_linux_VerifyVCache(vcp, &credp);
166 ObtainWriteLock(&vcp->lock, 529);
168 ReleaseWriteLock(&vcp->lock);
171 code = do_sync_write(fp, buf, count, offp);
175 ObtainWriteLock(&vcp->lock, 530);
177 if (vcp->execsOrWriters == 1 && !credp)
180 afs_FakeClose(vcp, credp);
181 ReleaseWriteLock(&vcp->lock);
183 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
184 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
193 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
195 /* This is a complete rewrite of afs_readdir, since we can make use of
196 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
197 * handling and use of bulkstats will need to be reflected here as well.
200 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
202 struct vcache *avc = VTOAFS(FILE_INODE(fp));
203 struct vrequest treq;
204 register struct dcache *tdc;
211 afs_size_t origOffset, tlen;
212 cred_t *credp = crref();
213 struct afs_fakestat_state fakestat;
215 afs_maybe_lock_kernel();
217 AFS_STATCNT(afs_readdir);
219 code = afs_convert_code(afs_InitReq(&treq, credp));
224 afs_InitFakeStat(&fakestat);
225 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
229 /* update the cache entry */
231 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
235 /* get a reference to the entire directory */
236 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
242 ObtainSharedLock(&avc->lock, 810);
243 UpgradeSToWLock(&avc->lock, 811);
244 ObtainReadLock(&tdc->lock);
246 * Make sure that the data in the cache is current. There are two
247 * cases we need to worry about:
248 * 1. The cache data is being fetched by another process.
249 * 2. The cache data is no longer valid
251 while ((avc->f.states & CStatd)
252 && (tdc->dflags & DFFetching)
253 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
254 ReleaseReadLock(&tdc->lock);
255 ReleaseSharedLock(&avc->lock);
256 afs_osi_Sleep(&tdc->validPos);
257 ObtainSharedLock(&avc->lock, 812);
258 ObtainReadLock(&tdc->lock);
260 if (!(avc->f.states & CStatd)
261 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
262 ReleaseReadLock(&tdc->lock);
263 ReleaseSharedLock(&avc->lock);
268 /* Set the readdir-in-progress flag, and downgrade the lock
269 * to shared so others will be able to acquire a read lock.
271 avc->f.states |= CReadDir;
272 avc->dcreaddir = tdc;
273 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
274 ConvertWToSLock(&avc->lock);
276 /* Fill in until we get an error or we're done. This implementation
277 * takes an offset in units of blobs, rather than bytes.
280 offset = (int) fp->f_pos;
282 dirpos = BlobScan(tdc, offset);
286 de = afs_dir_GetBlob(tdc, dirpos);
290 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
293 len = strlen(de->name);
295 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
296 (unsigned long)&tdc->f.inode, dirpos);
298 ReleaseSharedLock(&avc->lock);
304 /* filldir returns -EINVAL when the buffer is full. */
306 unsigned int type = DT_UNKNOWN;
307 struct VenusFid afid;
310 afid.Cell = avc->f.fid.Cell;
311 afid.Fid.Volume = avc->f.fid.Fid.Volume;
312 afid.Fid.Vnode = ntohl(de->fid.vnode);
313 afid.Fid.Unique = ntohl(de->fid.vunique);
314 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
316 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
319 } else if (((tvc->f.states) & (CStatd | CTruth))) {
320 /* CTruth will be set if the object has
325 else if (vtype == VREG)
327 /* Don't do this until we're sure it can't be a mtpt */
328 /* else if (vtype == VLNK)
330 /* what other types does AFS support? */
332 /* clean up from afs_FindVCache */
336 * If this is NFS readdirplus, then the filler is going to
337 * call getattr on this inode, which will deadlock if we're
341 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
347 offset = dirpos + 1 + ((len + 16) >> 5);
349 /* If filldir didn't fill in the last one this is still pointing to that
352 fp->f_pos = (loff_t) offset;
354 ReleaseReadLock(&tdc->lock);
356 UpgradeSToWLock(&avc->lock, 813);
357 avc->f.states &= ~CReadDir;
359 avc->readdir_pid = 0;
360 ReleaseSharedLock(&avc->lock);
364 afs_PutFakeStat(&fakestat);
367 afs_maybe_unlock_kernel();
372 /* in afs_pioctl.c */
373 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
376 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
377 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
379 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
386 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
388 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
392 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
393 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
394 vmap->vm_end - vmap->vm_start);
396 /* get a validated vcache entry */
397 code = afs_linux_VerifyVCache(vcp, NULL);
399 /* Linux's Flushpage implementation doesn't use credp, so optimise
400 * our code to not need to crref() it */
401 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
403 code = generic_file_mmap(fp, vmap);
406 vcp->f.states |= CMAPPED;
413 afs_linux_open(struct inode *ip, struct file *fp)
415 struct vcache *vcp = VTOAFS(ip);
416 cred_t *credp = crref();
419 afs_maybe_lock_kernel();
421 code = afs_open(&vcp, fp->f_flags, credp);
423 afs_maybe_unlock_kernel();
426 return afs_convert_code(code);
430 afs_linux_release(struct inode *ip, struct file *fp)
432 struct vcache *vcp = VTOAFS(ip);
433 cred_t *credp = crref();
436 afs_maybe_lock_kernel();
438 code = afs_close(vcp, fp->f_flags, credp);
439 ObtainWriteLock(&vcp->lock, 807);
444 ReleaseWriteLock(&vcp->lock);
446 afs_maybe_unlock_kernel();
449 return afs_convert_code(code);
453 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
456 struct inode *ip = FILE_INODE(fp);
457 cred_t *credp = crref();
459 afs_maybe_lock_kernel();
461 code = afs_fsync(VTOAFS(ip), credp);
463 afs_maybe_unlock_kernel();
465 return afs_convert_code(code);
471 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
474 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
475 cred_t *credp = crref();
476 struct AFS_FLOCK flock;
478 /* Convert to a lock format afs_lockctl understands. */
479 memset(&flock, 0, sizeof(flock));
480 flock.l_type = flp->fl_type;
481 flock.l_pid = flp->fl_pid;
483 flock.l_start = flp->fl_start;
484 if (flp->fl_end == OFFSET_MAX)
485 flock.l_len = 0; /* Lock to end of file */
487 flock.l_len = flp->fl_end - flp->fl_start + 1;
489 /* Safe because there are no large files, yet */
490 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
491 if (cmd == F_GETLK64)
493 else if (cmd == F_SETLK64)
495 else if (cmd == F_SETLKW64)
497 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
500 code = afs_lockctl(vcp, &flock, cmd, credp);
503 if ((code == 0 || flp->fl_type == F_UNLCK) &&
504 (cmd == F_SETLK || cmd == F_SETLKW)) {
505 code = afs_posix_lock_file(fp, flp);
506 if (code && flp->fl_type != F_UNLCK) {
507 struct AFS_FLOCK flock2;
509 flock2.l_type = F_UNLCK;
511 afs_lockctl(vcp, &flock2, F_SETLK, credp);
515 /* If lockctl says there are no conflicting locks, then also check with the
516 * kernel, as lockctl knows nothing about byte range locks
518 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
519 afs_posix_test_lock(fp, flp);
520 /* If we found a lock in the kernel's structure, return it */
521 if (flp->fl_type != F_UNLCK) {
527 /* Convert flock back to Linux's file_lock */
528 flp->fl_type = flock.l_type;
529 flp->fl_pid = flock.l_pid;
530 flp->fl_start = flock.l_start;
531 if (flock.l_len == 0)
532 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
534 flp->fl_end = flock.l_start + flock.l_len - 1;
537 return afs_convert_code(code);
540 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
542 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
544 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
545 cred_t *credp = crref();
546 struct AFS_FLOCK flock;
547 /* Convert to a lock format afs_lockctl understands. */
548 memset(&flock, 0, sizeof(flock));
549 flock.l_type = flp->fl_type;
550 flock.l_pid = flp->fl_pid;
555 /* Safe because there are no large files, yet */
556 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
557 if (cmd == F_GETLK64)
559 else if (cmd == F_SETLK64)
561 else if (cmd == F_SETLKW64)
563 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
566 code = afs_lockctl(vcp, &flock, cmd, credp);
569 if ((code == 0 || flp->fl_type == F_UNLCK) &&
570 (cmd == F_SETLK || cmd == F_SETLKW)) {
571 flp->fl_flags &=~ FL_SLEEP;
572 code = flock_lock_file_wait(fp, flp);
573 if (code && flp->fl_type != F_UNLCK) {
574 struct AFS_FLOCK flock2;
576 flock2.l_type = F_UNLCK;
578 afs_lockctl(vcp, &flock2, F_SETLK, credp);
582 /* Convert flock back to Linux's file_lock */
583 flp->fl_type = flock.l_type;
584 flp->fl_pid = flock.l_pid;
587 return afs_convert_code(code);
592 * essentially the same as afs_fsync() but we need to get the return
593 * code for the sys_close() here, not afs_linux_release(), so call
594 * afs_StoreAllSegments() with AFS_LASTSTORE
597 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
598 afs_linux_flush(struct file *fp, fl_owner_t id)
600 afs_linux_flush(struct file *fp)
603 struct vrequest treq;
607 #if defined(AFS_CACHE_BYPASS)
613 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
621 vcp = VTOAFS(FILE_INODE(fp));
623 code = afs_InitReq(&treq, credp);
626 #if defined(AFS_CACHE_BYPASS)
627 /* If caching is bypassed for this file, or globally, just return 0 */
628 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
631 ObtainReadLock(&vcp->lock);
632 if(vcp->cachingStates & FCSBypass)
634 ReleaseReadLock(&vcp->lock);
637 /* future proof: don't rely on 0 return from afs_InitReq */
642 ObtainSharedLock(&vcp->lock, 535);
643 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
644 UpgradeSToWLock(&vcp->lock, 536);
645 if (!AFS_IS_DISCONNECTED) {
646 code = afs_StoreAllSegments(vcp,
648 AFS_SYNC | AFS_LASTSTORE);
650 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
652 ConvertWToSLock(&vcp->lock);
654 code = afs_CheckCode(code, &treq, 54);
655 ReleaseSharedLock(&vcp->lock);
662 return afs_convert_code(code);
665 struct file_operations afs_dir_fops = {
666 .read = generic_read_dir,
667 .readdir = afs_linux_readdir,
668 #ifdef HAVE_UNLOCKED_IOCTL
669 .unlocked_ioctl = afs_unlocked_xioctl,
673 #ifdef HAVE_COMPAT_IOCTL
674 .compat_ioctl = afs_unlocked_xioctl,
676 .open = afs_linux_open,
677 .release = afs_linux_release,
680 struct file_operations afs_file_fops = {
681 .read = afs_linux_read,
682 .write = afs_linux_write,
683 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
684 .aio_read = generic_file_aio_read,
685 .aio_write = generic_file_aio_write,
687 #ifdef HAVE_UNLOCKED_IOCTL
688 .unlocked_ioctl = afs_unlocked_xioctl,
692 #ifdef HAVE_COMPAT_IOCTL
693 .compat_ioctl = afs_unlocked_xioctl,
695 .mmap = afs_linux_mmap,
696 .open = afs_linux_open,
697 .flush = afs_linux_flush,
698 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
699 .sendfile = generic_file_sendfile,
701 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
702 .splice_write = generic_file_splice_write,
703 .splice_read = generic_file_splice_read,
705 .release = afs_linux_release,
706 .fsync = afs_linux_fsync,
707 .lock = afs_linux_lock,
708 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
709 .flock = afs_linux_flock,
714 /**********************************************************************
715 * AFS Linux dentry operations
716 **********************************************************************/
718 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
719 * that has its mvid (parent dir's fid) pointer set to the wrong directory
720 * due to being mounted in multiple points at once. If so, check_bad_parent()
721 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
722 * dotdotfid and mtpoint fid members.
724 * dp - dentry to be checked.
728 * This dentry's vcache's mvid will be set to the correct parent directory's
730 * This root vnode's volume will have its dotdotfid and mtpoint fids set
731 * to the correct parent and mountpoint fids.
735 check_bad_parent(struct dentry *dp)
738 struct dentry *parent;
739 struct vcache *vcp, *pvc, *avc = NULL;
741 vcp = VTOAFS(dp->d_inode);
742 parent = dget_parent(dp);
743 pvc = VTOAFS(parent->d_inode);
745 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
748 /* force a lookup, so vcp->mvid is fixed up */
749 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
750 if (!avc || vcp != avc) { /* bad, very bad.. */
751 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
752 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
753 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
754 ICL_TYPE_POINTER, dp);
757 AFS_RELE(AFSTOV(avc));
766 /* afs_linux_revalidate
767 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
770 afs_linux_revalidate(struct dentry *dp)
773 struct vcache *vcp = VTOAFS(dp->d_inode);
777 if (afs_shuttingdown)
780 afs_maybe_lock_kernel();
784 /* Make this a fast path (no crref), since it's called so often. */
785 if (vcp->f.states & CStatd) {
787 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
788 check_bad_parent(dp); /* check and correct mvid */
796 /* This avoids the crref when we don't have to do it. Watch for
797 * changes in afs_getattr that don't get replicated here!
799 if (vcp->f.states & CStatd &&
800 (!afs_fakestat_enable || vcp->mvstat != 1) &&
802 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
803 code = afs_CopyOutAttrs(vcp, &vattr);
806 code = afs_getattr(vcp, &vattr, credp);
810 afs_fill_inode(AFSTOV(vcp), &vattr);
813 afs_maybe_unlock_kernel();
815 return afs_convert_code(code);
819 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
821 int err = afs_linux_revalidate(dentry);
823 generic_fillattr(dentry->d_inode, stat);
828 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
829 * In kernels 2.2.10 and above, we are passed an additional flags var which
830 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
831 * we are advised to follow the entry if it is a link or to make sure that
832 * it is a directory. But since the kernel itself checks these possibilities
833 * later on, we shouldn't have to do it until later. Perhaps in the future..
836 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
837 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
839 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
843 cred_t *credp = NULL;
844 struct vcache *vcp, *pvcp, *tvc = NULL;
845 struct dentry *parent;
847 struct afs_fakestat_state fakestate;
849 afs_maybe_lock_kernel();
851 afs_InitFakeStat(&fakestate);
854 vcp = VTOAFS(dp->d_inode);
856 if (vcp == afs_globalVp)
859 if (vcp->mvstat == 1) { /* mount point */
860 if (vcp->mvid && (vcp->f.states & CMValid)) {
863 struct vrequest treq;
866 code = afs_InitReq(&treq, credp);
868 (strcmp(dp->d_name.name, ".directory") == 0)) {
872 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
874 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
875 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
876 /* a mount point, not yet replaced by its directory */
881 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
882 check_bad_parent(dp); /* check and correct mvid */
885 /* If the last looker changes, we should make sure the current
886 * looker still has permission to examine this file. This would
887 * always require a crref() which would be "slow".
889 if (vcp->last_looker != treq.uid) {
890 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
893 vcp->last_looker = treq.uid;
897 parent = dget_parent(dp);
898 pvcp = VTOAFS(parent->d_inode);
900 /* If the parent's DataVersion has changed or the vnode
901 * is longer valid, we need to do a full lookup. VerifyVCache
902 * isn't enough since the vnode may have been renamed.
905 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
908 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
909 if (!tvc || tvc != vcp) {
914 if (afs_getattr(vcp, &vattr, credp)) {
919 vattr2inode(AFSTOV(vcp), &vattr);
920 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
923 /* should we always update the attributes at this point? */
924 /* unlikely--the vcache entry hasn't changed */
929 /* If this code is ever enabled, we should use dget_parent to handle
930 * getting the parent, and dput() to dispose of it. See above for an
932 pvcp = VTOAFS(dp->d_parent->d_inode);
933 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
937 /* No change in parent's DataVersion so this negative
938 * lookup is still valid. BUT, if a server is down a
939 * negative lookup can result so there should be a
940 * liftime as well. For now, always expire.
953 afs_PutFakeStat(&fakestate);
959 shrink_dcache_parent(dp);
962 afs_maybe_unlock_kernel();
966 if (have_submounts(dp))
974 afs_dentry_iput(struct dentry *dp, struct inode *ip)
976 struct vcache *vcp = VTOAFS(ip);
979 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
980 (void) afs_InactiveVCache(vcp, NULL);
983 afs_linux_clear_nfsfs_renamed(dp);
989 afs_dentry_delete(struct dentry *dp)
991 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
992 return 1; /* bad inode? */
997 struct dentry_operations afs_dentry_operations = {
998 .d_revalidate = afs_linux_dentry_revalidate,
999 .d_delete = afs_dentry_delete,
1000 .d_iput = afs_dentry_iput,
1003 /**********************************************************************
1004 * AFS Linux inode operations
1005 **********************************************************************/
1009 * Merely need to set enough of vattr to get us through the create. Note
1010 * that the higher level code (open_namei) will take care of any tuncation
1011 * explicitly. Exclusive open is also taken care of in open_namei.
1013 * name is in kernel space at this point.
1016 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1017 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1018 struct nameidata *nd)
1020 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1024 cred_t *credp = crref();
1025 const char *name = dp->d_name.name;
1030 vattr.va_mode = mode;
1031 vattr.va_type = mode & S_IFMT;
1033 afs_maybe_lock_kernel();
1035 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1039 struct inode *ip = AFSTOV(vcp);
1041 afs_getattr(vcp, &vattr, credp);
1042 afs_fill_inode(ip, &vattr);
1043 insert_inode_hash(ip);
1044 dp->d_op = &afs_dentry_operations;
1045 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1046 d_instantiate(dp, ip);
1050 afs_maybe_unlock_kernel();
1052 return afs_convert_code(code);
1055 /* afs_linux_lookup */
1056 static struct dentry *
1057 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1058 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1059 struct nameidata *nd)
1061 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1064 cred_t *credp = crref();
1065 struct vcache *vcp = NULL;
1066 const char *comp = dp->d_name.name;
1067 struct inode *ip = NULL;
1068 struct dentry *newdp = NULL;
1071 afs_maybe_lock_kernel();
1073 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1079 afs_getattr(vcp, &vattr, credp);
1080 afs_fill_inode(ip, &vattr);
1081 if (hlist_unhashed(&ip->i_hash))
1082 insert_inode_hash(ip);
1084 dp->d_op = &afs_dentry_operations;
1085 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1088 if (ip && S_ISDIR(ip->i_mode)) {
1089 struct dentry *alias;
1091 /* Try to invalidate an existing alias in favor of our new one */
1092 alias = d_find_alias(ip);
1093 /* But not if it's disconnected; then we want d_splice_alias below */
1094 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1095 if (d_invalidate(alias) == 0) {
1099 afs_maybe_unlock_kernel();
1105 newdp = d_splice_alias(ip, dp);
1107 afs_maybe_unlock_kernel();
1110 /* It's ok for the file to not be found. That's noted by the caller by
1111 * seeing that the dp->d_inode field is NULL.
1113 if (!code || code == ENOENT)
1116 return ERR_PTR(afs_convert_code(code));
1120 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1123 cred_t *credp = crref();
1124 const char *name = newdp->d_name.name;
1125 struct inode *oldip = olddp->d_inode;
1127 /* If afs_link returned the vnode, we could instantiate the
1128 * dentry. Since it's not, we drop this one and do a new lookup.
1133 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1137 return afs_convert_code(code);
1140 /* We have to have a Linux specific sillyrename function, because we
1141 * also have to keep the dcache up to date when we're doing a silly
1142 * rename - so we don't want the generic vnodeops doing this behind our
1147 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1150 struct vcache *tvc = VTOAFS(dentry->d_inode);
1151 struct dentry *__dp = NULL;
1152 char *__name = NULL;
1155 if (afs_linux_nfsfs_renamed(dentry))
1163 osi_FreeSmallSpace(__name);
1164 __name = afs_newname();
1167 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1170 osi_FreeSmallSpace(__name);
1173 } while (__dp->d_inode != NULL);
1176 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1177 VTOAFS(dir), (char *)__dp->d_name.name,
1180 tvc->mvid = (void *) __name;
1183 crfree(tvc->uncred);
1185 tvc->uncred = credp;
1186 tvc->f.states |= CUnlinked;
1187 afs_linux_set_nfsfs_renamed(dentry);
1189 osi_FreeSmallSpace(__name);
1194 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1195 d_move(dentry, __dp);
1204 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1207 cred_t *credp = crref();
1208 const char *name = dp->d_name.name;
1209 struct vcache *tvc = VTOAFS(dp->d_inode);
1211 afs_maybe_lock_kernel();
1213 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1214 && !(tvc->f.states & CUnlinked)) {
1216 code = afs_linux_sillyrename(dip, dp, credp);
1219 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1225 afs_maybe_unlock_kernel();
1227 return afs_convert_code(code);
1232 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1235 cred_t *credp = crref();
1237 const char *name = dp->d_name.name;
1239 /* If afs_symlink returned the vnode, we could instantiate the
1240 * dentry. Since it's not, we drop this one and do a new lookup.
1246 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1249 return afs_convert_code(code);
1253 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1256 cred_t *credp = crref();
1257 struct vcache *tvcp = NULL;
1259 const char *name = dp->d_name.name;
1261 afs_maybe_lock_kernel();
1263 vattr.va_mask = ATTR_MODE;
1264 vattr.va_mode = mode;
1266 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1269 struct inode *ip = AFSTOV(tvcp);
1271 afs_getattr(tvcp, &vattr, credp);
1272 afs_fill_inode(ip, &vattr);
1274 dp->d_op = &afs_dentry_operations;
1275 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1276 d_instantiate(dp, ip);
1280 afs_maybe_unlock_kernel();
1282 return afs_convert_code(code);
1286 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1289 cred_t *credp = crref();
1290 const char *name = dp->d_name.name;
1292 /* locking kernel conflicts with glock? */
1295 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1298 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1299 * that failed because a directory is not empty. So, we map
1300 * EEXIST to ENOTEMPTY on linux.
1302 if (code == EEXIST) {
1311 return afs_convert_code(code);
1316 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1317 struct inode *newip, struct dentry *newdp)
1320 cred_t *credp = crref();
1321 const char *oldname = olddp->d_name.name;
1322 const char *newname = newdp->d_name.name;
1323 struct dentry *rehash = NULL;
1325 /* Prevent any new references during rename operation. */
1326 afs_maybe_lock_kernel();
1328 if (!d_unhashed(newdp)) {
1333 if (atomic_read(&olddp->d_count) > 1)
1334 shrink_dcache_parent(olddp);
1337 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1341 olddp->d_time = 0; /* force to revalidate */
1346 afs_maybe_unlock_kernel();
1349 return afs_convert_code(code);
1353 /* afs_linux_ireadlink
1354 * Internal readlink which can return link contents to user or kernel space.
1355 * Note that the buffer is NOT supposed to be null-terminated.
1358 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1361 cred_t *credp = crref();
1365 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1366 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1370 return maxlen - tuio.uio_resid;
1372 return afs_convert_code(code);
1375 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1376 /* afs_linux_readlink
1377 * Fill target (which is in user space) with contents of symlink.
1380 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1383 struct inode *ip = dp->d_inode;
1386 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1392 /* afs_linux_follow_link
1393 * a file system dependent link following routine.
1395 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1400 name = osi_Alloc(PATH_MAX);
1406 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1414 code = vfs_follow_link(nd, name);
1417 osi_Free(name, PATH_MAX);
1422 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1424 #if defined(AFS_CACHE_BYPASS)
1425 #endif /* defined(AFS_CACHE_BYPASS */
1427 /* Populate a page by filling it from the cache file pointed at by cachefp
1428 * (which contains indicated chunk)
1429 * If task is NULL, the page copy occurs syncronously, and the routine
1430 * returns with page still locked. If task is non-NULL, then page copies
1431 * may occur in the background, and the page will be unlocked when it is
1435 afs_linux_read_cache(struct file *cachefp, struct page *page,
1436 int chunk, struct pagevec *lrupv,
1437 struct afs_pagecopy_task *task) {
1438 loff_t offset = page_offset(page);
1439 struct page *newpage, *cachepage;
1440 struct address_space *cachemapping;
1444 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1448 /* From our offset, we now need to work out which page in the disk
1449 * file it corresponds to. This will be fun ... */
1450 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1452 while (cachepage == NULL) {
1453 cachepage = find_get_page(cachemapping, pageindex);
1456 newpage = page_cache_alloc_cold(cachemapping);
1462 code = add_to_page_cache(newpage, cachemapping,
1463 pageindex, GFP_KERNEL);
1465 cachepage = newpage;
1468 page_cache_get(cachepage);
1469 if (!pagevec_add(lrupv, cachepage))
1470 __pagevec_lru_add_file(lrupv);
1473 page_cache_release(newpage);
1475 if (code != -EEXIST)
1479 lock_page(cachepage);
1483 if (!PageUptodate(cachepage)) {
1484 ClearPageError(cachepage);
1485 code = cachemapping->a_ops->readpage(NULL, cachepage);
1486 if (!code && !task) {
1487 wait_on_page_locked(cachepage);
1490 unlock_page(cachepage);
1494 if (PageUptodate(cachepage)) {
1495 copy_highpage(page, cachepage);
1496 flush_dcache_page(page);
1497 SetPageUptodate(page);
1502 afs_pagecopy_queue_page(task, cachepage, page);
1514 page_cache_release(cachepage);
1520 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1522 loff_t offset = page_offset(pp);
1523 struct inode *ip = FILE_INODE(fp);
1524 struct vcache *avc = VTOAFS(ip);
1526 struct file *cacheFp = NULL;
1529 struct pagevec lrupv;
1531 /* Not a UFS cache, don't do anything */
1532 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1535 /* Can't do anything if the vcache isn't statd , or if the read
1536 * crosses a chunk boundary.
1538 if (!(avc->f.states & CStatd) ||
1539 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1543 ObtainWriteLock(&avc->lock, 911);
1545 /* XXX - See if hinting actually makes things faster !!! */
1547 /* See if we have a suitable entry already cached */
1551 /* We need to lock xdcache, then dcache, to handle situations where
1552 * the hint is on the free list. However, we can't safely do this
1553 * according to the locking hierarchy. So, use a non blocking lock.
1555 ObtainReadLock(&afs_xdcache);
1556 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1558 if (dcLocked && (tdc->index != NULLIDX)
1559 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1560 && tdc->f.chunk == AFS_CHUNK(offset)
1561 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1562 /* Bonus - the hint was correct */
1565 /* Only destroy the hint if its actually invalid, not if there's
1566 * just been a locking failure */
1568 ReleaseReadLock(&tdc->lock);
1575 ReleaseReadLock(&afs_xdcache);
1578 /* No hint, or hint is no longer valid - see if we can get something
1579 * directly from the dcache
1582 tdc = afs_FindDCache(avc, offset);
1585 ReleaseWriteLock(&avc->lock);
1590 ObtainReadLock(&tdc->lock);
1592 /* Is the dcache we've been given currently up to date */
1593 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1594 (tdc->dflags & DFFetching)) {
1595 ReleaseWriteLock(&avc->lock);
1596 ReleaseReadLock(&tdc->lock);
1601 /* Update our hint for future abuse */
1604 /* Okay, so we've now got a cache file that is up to date */
1606 /* XXX - I suspect we should be locking the inodes before we use them! */
1608 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1609 pagevec_init(&lrupv, 0);
1611 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1613 if (pagevec_count(&lrupv))
1614 __pagevec_lru_add_file(&lrupv);
1616 filp_close(cacheFp, NULL);
1619 ReleaseReadLock(&tdc->lock);
1620 ReleaseWriteLock(&avc->lock);
1627 /* afs_linux_readpage
1629 * This function is split into two, because prepare_write/begin_write
1630 * require a readpage call which doesn't unlock the resulting page upon
1634 afs_linux_fillpage(struct file *fp, struct page *pp)
1639 struct iovec *iovecp;
1640 struct inode *ip = FILE_INODE(fp);
1641 afs_int32 cnt = page_count(pp);
1642 struct vcache *avc = VTOAFS(ip);
1643 afs_offs_t offset = page_offset(pp);
1647 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1657 auio = osi_Alloc(sizeof(uio_t));
1658 iovecp = osi_Alloc(sizeof(struct iovec));
1660 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1663 afs_maybe_lock_kernel();
1666 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1667 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1668 99999); /* not a possible code value */
1670 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1672 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1673 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1675 AFS_DISCON_UNLOCK();
1677 afs_maybe_unlock_kernel();
1679 /* XXX valid for no-cache also? Check last bits of files... :)
1680 * Cognate code goes in afs_NoCacheFetchProc. */
1681 if (auio->uio_resid) /* zero remainder of page */
1682 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1685 flush_dcache_page(pp);
1686 SetPageUptodate(pp);
1691 osi_Free(auio, sizeof(uio_t));
1692 osi_Free(iovecp, sizeof(struct iovec));
1695 return afs_convert_code(code);
1699 afs_linux_prefetch(struct file *fp, struct page *pp)
1702 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1703 afs_offs_t offset = page_offset(pp);
1705 if (AFS_CHUNKOFFSET(offset) == 0) {
1707 struct vrequest treq;
1712 code = afs_InitReq(&treq, credp);
1713 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1714 tdc = afs_FindDCache(avc, offset);
1716 if (!(tdc->mflags & DFNextStarted))
1717 afs_PrefetchChunk(avc, tdc, credp, &treq);
1720 ReleaseWriteLock(&avc->lock);
1725 return afs_convert_code(code);
1729 #if defined(AFS_CACHE_BYPASS)
1732 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1733 struct list_head *page_list, unsigned num_pages)
1738 struct iovec* iovecp;
1739 struct nocache_read_request *ancr;
1741 struct pagevec lrupv;
1745 struct inode *ip = FILE_INODE(fp);
1746 struct vcache *avc = VTOAFS(ip);
1747 afs_int32 base_index = 0;
1748 afs_int32 page_count = 0;
1751 /* background thread must free: iovecp, auio, ancr */
1752 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1754 auio = osi_Alloc(sizeof(uio_t));
1755 auio->uio_iov = iovecp;
1756 auio->uio_iovcnt = num_pages;
1757 auio->uio_flag = UIO_READ;
1758 auio->uio_seg = AFS_UIOSYS;
1759 auio->uio_resid = num_pages * PAGE_SIZE;
1761 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1763 ancr->offset = auio->uio_offset;
1764 ancr->length = auio->uio_resid;
1766 pagevec_init(&lrupv, 0);
1768 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1770 if(list_empty(page_list))
1773 pp = list_entry(page_list->prev, struct page, lru);
1774 /* If we allocate a page and don't remove it from page_list,
1775 * the page cache gets upset. */
1777 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1778 if(pp->index > isize) {
1785 offset = page_offset(pp);
1786 auio->uio_offset = offset;
1787 base_index = pp->index;
1789 iovecp[page_ix].iov_len = PAGE_SIZE;
1790 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1791 if(base_index != pp->index) {
1794 page_cache_release(pp);
1795 iovecp[page_ix].iov_base = (void *) 0;
1803 page_cache_release(pp);
1804 iovecp[page_ix].iov_base = (void *) 0;
1807 if(!PageLocked(pp)) {
1811 /* save the page for background map */
1812 iovecp[page_ix].iov_base = (void*) pp;
1814 /* and put it on the LRU cache */
1815 if (!pagevec_add(&lrupv, pp))
1816 __pagevec_lru_add(&lrupv);
1820 /* If there were useful pages in the page list, make sure all pages
1821 * are in the LRU cache, then schedule the read */
1823 pagevec_lru_add(&lrupv);
1825 code = afs_ReadNoCache(avc, ancr, credp);
1828 /* If there is nothing for the background thread to handle,
1829 * it won't be freeing the things that we never gave it */
1830 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1831 osi_Free(auio, sizeof(uio_t));
1832 osi_Free(ancr, sizeof(struct nocache_read_request));
1834 /* we do not flush, release, or unmap pages--that will be
1835 * done for us by the background thread as each page comes in
1836 * from the fileserver */
1837 return afs_convert_code(code);
1842 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1844 cred_t *credp = NULL;
1846 struct iovec *iovecp;
1847 struct nocache_read_request *ancr;
1852 /* receiver frees */
1853 auio = osi_Alloc(sizeof(uio_t));
1854 iovecp = osi_Alloc(sizeof(struct iovec));
1856 /* address can be NULL, because we overwrite it with 'pp', below */
1857 setup_uio(auio, iovecp, NULL, page_offset(pp),
1858 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1860 /* save the page for background map */
1861 /* XXX - Shouldn't we get a reference count here? */
1862 auio->uio_iov->iov_base = (void*) pp;
1863 /* the background thread will free this */
1864 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1866 ancr->offset = page_offset(pp);
1867 ancr->length = PAGE_SIZE;
1870 afs_maybe_lock_kernel();
1871 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1872 afs_maybe_unlock_kernel();
1875 return afs_convert_code(code);
1879 afs_linux_can_bypass(struct inode *ip) {
1880 switch(cache_bypass_strategy) {
1881 case NEVER_BYPASS_CACHE:
1883 case ALWAYS_BYPASS_CACHE:
1885 case LARGE_FILES_BYPASS_CACHE:
1886 if(i_size_read(ip) > cache_bypass_threshold)
1893 /* Check if a file is permitted to bypass the cache by policy, and modify
1894 * the cache bypass state recorded for that file */
1897 afs_linux_bypass_check(struct inode *ip) {
1900 int bypass = afs_linux_can_bypass(ip);
1903 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1911 afs_linux_bypass_check(struct inode *ip) {
1915 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1919 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1920 struct list_head *page_list, unsigned int num_pages) {
1926 afs_linux_readpage(struct file *fp, struct page *pp)
1930 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1931 code = afs_linux_bypass_readpage(fp, pp);
1933 code = afs_linux_fillpage(fp, pp);
1935 code = afs_linux_prefetch(fp, pp);
1942 /* Readpages reads a number of pages for a particular file. We use
1943 * this to optimise the reading, by limiting the number of times upon which
1944 * we have to lookup, lock and open vcaches and dcaches
1948 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1949 struct list_head *page_list, unsigned int num_pages)
1951 struct inode *inode = mapping->host;
1952 struct vcache *avc = VTOAFS(inode);
1954 struct file *cacheFp = NULL;
1956 unsigned int page_idx;
1958 struct pagevec lrupv;
1959 struct afs_pagecopy_task *task;
1961 if (afs_linux_bypass_check(inode))
1962 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1964 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1968 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1973 ObtainWriteLock(&avc->lock, 912);
1976 task = afs_pagecopy_init_task();
1979 pagevec_init(&lrupv, 0);
1980 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1981 struct page *page = list_entry(page_list->prev, struct page, lru);
1982 list_del(&page->lru);
1983 offset = page_offset(page);
1985 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1987 ReleaseReadLock(&tdc->lock);
1992 filp_close(cacheFp, NULL);
1997 if ((tdc = afs_FindDCache(avc, offset))) {
1998 ObtainReadLock(&tdc->lock);
1999 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2000 (tdc->dflags & DFFetching)) {
2001 ReleaseReadLock(&tdc->lock);
2008 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2011 if (tdc && !add_to_page_cache(page, mapping, page->index,
2013 page_cache_get(page);
2014 if (!pagevec_add(&lrupv, page))
2015 __pagevec_lru_add_file(&lrupv);
2017 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2019 page_cache_release(page);
2021 if (pagevec_count(&lrupv))
2022 __pagevec_lru_add_file(&lrupv);
2025 filp_close(cacheFp, NULL);
2027 afs_pagecopy_put_task(task);
2031 ReleaseReadLock(&tdc->lock);
2035 ReleaseWriteLock(&avc->lock);
2040 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2043 afs_linux_prepare_writeback(struct vcache *avc) {
2044 if (avc->f.states & CPageWrite) {
2045 return AOP_WRITEPAGE_ACTIVATE;
2047 avc->f.states |= CPageWrite;
2052 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2053 struct vrequest treq;
2056 if (!afs_InitReq(&treq, credp))
2057 code = afs_DoPartialWrite(avc, &treq);
2059 return afs_convert_code(code);
2063 afs_linux_complete_writeback(struct vcache *avc) {
2064 avc->f.states &= ~CPageWrite;
2067 /* Writeback a given page syncronously. Called with no AFS locks held */
2069 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2070 unsigned long offset, unsigned int count,
2073 struct vcache *vcp = VTOAFS(ip);
2081 buffer = kmap(pp) + offset;
2082 base = page_offset(pp) + offset;
2084 afs_maybe_lock_kernel();
2086 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2087 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2088 ICL_TYPE_INT32, 99999);
2090 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2092 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2094 i_size_write(ip, vcp->f.m.Length);
2095 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2097 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2099 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2100 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2101 ICL_TYPE_INT32, code);
2104 afs_maybe_unlock_kernel();
2111 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2112 unsigned long offset, unsigned int count)
2116 struct vcache *vcp = VTOAFS(ip);
2119 /* Catch recursive writeback. This occurs if the kernel decides
2120 * writeback is required whilst we are writing to the cache, or
2121 * flushing to the server. When we're running syncronously (as
2122 * opposed to from writepage) we can't actually do anything about
2123 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2126 ObtainWriteLock(&vcp->lock, 532);
2127 afs_linux_prepare_writeback(vcp);
2128 ReleaseWriteLock(&vcp->lock);
2132 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2134 afs_maybe_lock_kernel();
2136 ObtainWriteLock(&vcp->lock, 533);
2138 code1 = afs_linux_dopartialwrite(vcp, credp);
2139 afs_linux_complete_writeback(vcp);
2140 ReleaseWriteLock(&vcp->lock);
2142 afs_maybe_unlock_kernel();
2152 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2153 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2155 afs_linux_writepage(struct page *pp)
2158 struct address_space *mapping = pp->mapping;
2159 struct inode *inode;
2162 unsigned int to = PAGE_CACHE_SIZE;
2167 if (PageReclaim(pp)) {
2168 return AOP_WRITEPAGE_ACTIVATE;
2169 /* XXX - Do we need to redirty the page here? */
2174 inode = mapping->host;
2175 vcp = VTOAFS(inode);
2176 isize = i_size_read(inode);
2178 /* Don't defeat an earlier truncate */
2179 if (page_offset(pp) > isize) {
2180 set_page_writeback(pp);
2186 ObtainWriteLock(&vcp->lock, 537);
2187 code = afs_linux_prepare_writeback(vcp);
2188 if (code == AOP_WRITEPAGE_ACTIVATE) {
2189 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2190 * to return with the page still locked */
2191 ReleaseWriteLock(&vcp->lock);
2196 /* Grab the creds structure currently held in the vnode, and
2197 * get a reference to it, in case it goes away ... */
2203 ReleaseWriteLock(&vcp->lock);
2206 set_page_writeback(pp);
2208 SetPageUptodate(pp);
2210 /* We can unlock the page here, because it's protected by the
2211 * page_writeback flag. This should make us less vulnerable to
2212 * deadlocking in afs_write and afs_DoPartialWrite
2216 /* If this is the final page, then just write the number of bytes that
2217 * are actually in it */
2218 if ((isize - page_offset(pp)) < to )
2219 to = isize - page_offset(pp);
2221 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2223 afs_maybe_lock_kernel();
2225 ObtainWriteLock(&vcp->lock, 538);
2227 /* As much as we might like to ignore a file server error here,
2228 * and just try again when we close(), unfortunately StoreAllSegments
2229 * will invalidate our chunks if the server returns a permanent error,
2230 * so we need to at least try and get that error back to the user
2233 code1 = afs_linux_dopartialwrite(vcp, credp);
2235 afs_linux_complete_writeback(vcp);
2236 ReleaseWriteLock(&vcp->lock);
2239 afs_maybe_unlock_kernel();
2242 end_page_writeback(pp);
2243 page_cache_release(pp);
2254 /* afs_linux_permission
2255 * Check access rights - returns error if can't check or permission denied.
2258 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2259 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2261 afs_linux_permission(struct inode *ip, int mode)
2265 cred_t *credp = crref();
2269 if (mode & MAY_EXEC)
2271 if (mode & MAY_READ)
2273 if (mode & MAY_WRITE)
2275 code = afs_access(VTOAFS(ip), tmp, credp);
2279 return afs_convert_code(code);
2283 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2287 struct inode *inode = FILE_INODE(file);
2288 loff_t pagebase = page_offset(page);
2290 if (i_size_read(inode) < (pagebase + offset))
2291 i_size_write(inode, pagebase + offset);
2293 if (PageChecked(page)) {
2294 SetPageUptodate(page);
2295 ClearPageChecked(page);
2298 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2304 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2308 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2309 * prepare_write. Essentially, if the page exists within the file,
2310 * and is not being fully written, then we should populate it.
2313 if (!PageUptodate(page)) {
2314 loff_t pagebase = page_offset(page);
2315 loff_t isize = i_size_read(page->mapping->host);
2317 /* Is the location we are writing to beyond the end of the file? */
2318 if (pagebase >= isize ||
2319 ((from == 0) && (pagebase + to) >= isize)) {
2320 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2321 SetPageChecked(page);
2322 /* Are we we writing a full page */
2323 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2324 SetPageChecked(page);
2325 /* Is the page readable, if it's wronly, we don't care, because we're
2326 * not actually going to read from it ... */
2327 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2328 /* We don't care if fillpage fails, because if it does the page
2329 * won't be marked as up to date
2331 afs_linux_fillpage(file, page);
2337 #if defined(HAVE_WRITE_BEGIN)
2339 afs_linux_write_end(struct file *file, struct address_space *mapping,
2340 loff_t pos, unsigned len, unsigned copied,
2341 struct page *page, void *fsdata)
2344 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2346 code = afs_linux_commit_write(file, page, from, from + len);
2349 page_cache_release(page);
2354 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2355 loff_t pos, unsigned len, unsigned flags,
2356 struct page **pagep, void **fsdata)
2359 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2360 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2363 page = grab_cache_page_write_begin(mapping, index, flags);
2366 code = afs_linux_prepare_write(file, page, from, from + len);
2369 page_cache_release(page);
2377 static struct inode_operations afs_file_iops = {
2378 .permission = afs_linux_permission,
2379 .getattr = afs_linux_getattr,
2380 .setattr = afs_notify_change,
2383 static struct address_space_operations afs_file_aops = {
2384 .readpage = afs_linux_readpage,
2385 .readpages = afs_linux_readpages,
2386 .writepage = afs_linux_writepage,
2387 #if defined (HAVE_WRITE_BEGIN)
2388 .write_begin = afs_linux_write_begin,
2389 .write_end = afs_linux_write_end,
2391 .commit_write = afs_linux_commit_write,
2392 .prepare_write = afs_linux_prepare_write,
2397 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2398 * by what sort of operation is allowed.....
2401 static struct inode_operations afs_dir_iops = {
2402 .setattr = afs_notify_change,
2403 .create = afs_linux_create,
2404 .lookup = afs_linux_lookup,
2405 .link = afs_linux_link,
2406 .unlink = afs_linux_unlink,
2407 .symlink = afs_linux_symlink,
2408 .mkdir = afs_linux_mkdir,
2409 .rmdir = afs_linux_rmdir,
2410 .rename = afs_linux_rename,
2411 .getattr = afs_linux_getattr,
2412 .permission = afs_linux_permission,
2415 /* We really need a separate symlink set of ops, since do_follow_link()
2416 * determines if it _is_ a link by checking if the follow_link op is set.
2418 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2420 afs_symlink_filler(struct file *file, struct page *page)
2422 struct inode *ip = (struct inode *)page->mapping->host;
2423 char *p = (char *)kmap(page);
2426 afs_maybe_lock_kernel();
2428 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2433 p[code] = '\0'; /* null terminate? */
2434 afs_maybe_unlock_kernel();
2436 SetPageUptodate(page);
2442 afs_maybe_unlock_kernel();
2450 static struct address_space_operations afs_symlink_aops = {
2451 .readpage = afs_symlink_filler
2453 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2455 static struct inode_operations afs_symlink_iops = {
2456 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2457 .readlink = page_readlink,
2458 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2459 .follow_link = page_follow_link,
2461 .follow_link = page_follow_link_light,
2462 .put_link = page_put_link,
2464 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2465 .readlink = afs_linux_readlink,
2466 .follow_link = afs_linux_follow_link,
2467 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2468 .setattr = afs_notify_change,
2472 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2476 vattr2inode(ip, vattr);
2478 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2479 /* Reset ops if symlink or directory. */
2480 if (S_ISREG(ip->i_mode)) {
2481 ip->i_op = &afs_file_iops;
2482 ip->i_fop = &afs_file_fops;
2483 ip->i_data.a_ops = &afs_file_aops;
2485 } else if (S_ISDIR(ip->i_mode)) {
2486 ip->i_op = &afs_dir_iops;
2487 ip->i_fop = &afs_dir_fops;
2489 } else if (S_ISLNK(ip->i_mode)) {
2490 ip->i_op = &afs_symlink_iops;
2491 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2492 ip->i_data.a_ops = &afs_symlink_aops;
2493 ip->i_mapping = &ip->i_data;