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);
57 /* This function converts a positive error code from AFS into a negative
58 * code suitable for passing into the Linux VFS layer. It checks that the
59 * error code is within the permissable bounds for the ERR_PTR mechanism.
61 * _All_ error codes which come from the AFS layer should be passed through
62 * this function before being returned to the kernel.
66 afs_convert_code(int code) {
67 if ((code >= 0) && (code <= MAX_ERRNO))
73 /* Linux doesn't require a credp for many functions, and crref is an expensive
74 * operation. This helper function avoids obtaining it for VerifyVCache calls
78 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
83 if (avc->f.states & CStatd) {
91 code = afs_InitReq(&treq, credp);
93 code = afs_VerifyVCache2(avc, &treq);
100 return afs_convert_code(code);
104 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
107 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
110 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
111 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
113 code = afs_linux_VerifyVCache(vcp, NULL);
116 /* Linux's FlushPages implementation doesn't ever use credp,
117 * so we optimise by not using it */
118 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
120 code = do_sync_read(fp, buf, count, offp);
124 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
125 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
132 /* Now we have integrated VM for writes as well as reads. generic_file_write
133 * also takes care of re-positioning the pointer if file is open in append
134 * mode. Call fake open/close to ensure we do writes of core dumps.
137 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
140 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
145 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
146 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
147 (fp->f_flags & O_APPEND) ? 99998 : 99999);
149 code = afs_linux_VerifyVCache(vcp, &credp);
151 ObtainWriteLock(&vcp->lock, 529);
153 ReleaseWriteLock(&vcp->lock);
156 code = do_sync_write(fp, buf, count, offp);
160 ObtainWriteLock(&vcp->lock, 530);
162 if (vcp->execsOrWriters == 1 && !credp)
165 afs_FakeClose(vcp, credp);
166 ReleaseWriteLock(&vcp->lock);
168 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
169 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
178 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
180 /* This is a complete rewrite of afs_readdir, since we can make use of
181 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
182 * handling and use of bulkstats will need to be reflected here as well.
185 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
187 struct vcache *avc = VTOAFS(FILE_INODE(fp));
188 struct vrequest treq;
196 afs_size_t origOffset, tlen;
197 cred_t *credp = crref();
198 struct afs_fakestat_state fakestat;
201 AFS_STATCNT(afs_readdir);
203 code = afs_convert_code(afs_InitReq(&treq, credp));
208 afs_InitFakeStat(&fakestat);
209 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
213 /* update the cache entry */
215 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
219 /* get a reference to the entire directory */
220 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
226 ObtainSharedLock(&avc->lock, 810);
227 UpgradeSToWLock(&avc->lock, 811);
228 ObtainReadLock(&tdc->lock);
230 * Make sure that the data in the cache is current. There are two
231 * cases we need to worry about:
232 * 1. The cache data is being fetched by another process.
233 * 2. The cache data is no longer valid
235 while ((avc->f.states & CStatd)
236 && (tdc->dflags & DFFetching)
237 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
238 ReleaseReadLock(&tdc->lock);
239 ReleaseSharedLock(&avc->lock);
240 afs_osi_Sleep(&tdc->validPos);
241 ObtainSharedLock(&avc->lock, 812);
242 ObtainReadLock(&tdc->lock);
244 if (!(avc->f.states & CStatd)
245 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
246 ReleaseReadLock(&tdc->lock);
247 ReleaseSharedLock(&avc->lock);
252 /* Set the readdir-in-progress flag, and downgrade the lock
253 * to shared so others will be able to acquire a read lock.
255 avc->f.states |= CReadDir;
256 avc->dcreaddir = tdc;
257 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
258 ConvertWToSLock(&avc->lock);
260 /* Fill in until we get an error or we're done. This implementation
261 * takes an offset in units of blobs, rather than bytes.
264 offset = (int) fp->f_pos;
266 dirpos = BlobScan(tdc, offset);
270 de = afs_dir_GetBlob(tdc, dirpos);
274 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
277 len = strlen(de->name);
279 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
280 (unsigned long)&tdc->f.inode, dirpos);
282 ReleaseSharedLock(&avc->lock);
288 /* filldir returns -EINVAL when the buffer is full. */
290 unsigned int type = DT_UNKNOWN;
291 struct VenusFid afid;
294 afid.Cell = avc->f.fid.Cell;
295 afid.Fid.Volume = avc->f.fid.Fid.Volume;
296 afid.Fid.Vnode = ntohl(de->fid.vnode);
297 afid.Fid.Unique = ntohl(de->fid.vunique);
298 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
300 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
303 } else if (((tvc->f.states) & (CStatd | CTruth))) {
304 /* CTruth will be set if the object has
309 else if (vtype == VREG)
311 /* Don't do this until we're sure it can't be a mtpt */
312 /* else if (vtype == VLNK)
314 /* what other types does AFS support? */
316 /* clean up from afs_FindVCache */
320 * If this is NFS readdirplus, then the filler is going to
321 * call getattr on this inode, which will deadlock if we're
325 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
331 offset = dirpos + 1 + ((len + 16) >> 5);
333 /* If filldir didn't fill in the last one this is still pointing to that
336 fp->f_pos = (loff_t) offset;
338 ReleaseReadLock(&tdc->lock);
340 UpgradeSToWLock(&avc->lock, 813);
341 avc->f.states &= ~CReadDir;
343 avc->readdir_pid = 0;
344 ReleaseSharedLock(&avc->lock);
348 afs_PutFakeStat(&fakestat);
355 /* in afs_pioctl.c */
356 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
359 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
360 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
362 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
369 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
371 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
375 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
376 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
377 vmap->vm_end - vmap->vm_start);
379 /* get a validated vcache entry */
380 code = afs_linux_VerifyVCache(vcp, NULL);
383 /* Linux's Flushpage implementation doesn't use credp, so optimise
384 * our code to not need to crref() it */
385 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
387 code = generic_file_mmap(fp, vmap);
390 vcp->f.states |= CMAPPED;
398 afs_linux_open(struct inode *ip, struct file *fp)
400 struct vcache *vcp = VTOAFS(ip);
401 cred_t *credp = crref();
405 code = afs_open(&vcp, fp->f_flags, credp);
409 return afs_convert_code(code);
413 afs_linux_release(struct inode *ip, struct file *fp)
415 struct vcache *vcp = VTOAFS(ip);
416 cred_t *credp = crref();
420 code = afs_close(vcp, fp->f_flags, credp);
421 ObtainWriteLock(&vcp->lock, 807);
426 ReleaseWriteLock(&vcp->lock);
430 return afs_convert_code(code);
434 #if defined(FOP_FSYNC_TAKES_DENTRY)
435 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
437 afs_linux_fsync(struct file *fp, int datasync)
441 struct inode *ip = FILE_INODE(fp);
442 cred_t *credp = crref();
445 code = afs_fsync(VTOAFS(ip), credp);
448 return afs_convert_code(code);
454 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
457 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
458 cred_t *credp = crref();
459 struct AFS_FLOCK flock;
461 /* Convert to a lock format afs_lockctl understands. */
462 memset(&flock, 0, sizeof(flock));
463 flock.l_type = flp->fl_type;
464 flock.l_pid = flp->fl_pid;
466 flock.l_start = flp->fl_start;
467 if (flp->fl_end == OFFSET_MAX)
468 flock.l_len = 0; /* Lock to end of file */
470 flock.l_len = flp->fl_end - flp->fl_start + 1;
472 /* Safe because there are no large files, yet */
473 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
474 if (cmd == F_GETLK64)
476 else if (cmd == F_SETLK64)
478 else if (cmd == F_SETLKW64)
480 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
483 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
486 if ((code == 0 || flp->fl_type == F_UNLCK) &&
487 (cmd == F_SETLK || cmd == F_SETLKW)) {
488 code = afs_posix_lock_file(fp, flp);
489 if (code && flp->fl_type != F_UNLCK) {
490 struct AFS_FLOCK flock2;
492 flock2.l_type = F_UNLCK;
494 afs_lockctl(vcp, &flock2, F_SETLK, credp);
498 /* If lockctl says there are no conflicting locks, then also check with the
499 * kernel, as lockctl knows nothing about byte range locks
501 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
502 afs_posix_test_lock(fp, flp);
503 /* If we found a lock in the kernel's structure, return it */
504 if (flp->fl_type != F_UNLCK) {
510 /* Convert flock back to Linux's file_lock */
511 flp->fl_type = flock.l_type;
512 flp->fl_pid = flock.l_pid;
513 flp->fl_start = flock.l_start;
514 if (flock.l_len == 0)
515 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
517 flp->fl_end = flock.l_start + flock.l_len - 1;
523 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
525 afs_linux_flock(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;
530 /* Convert to a lock format afs_lockctl understands. */
531 memset(&flock, 0, sizeof(flock));
532 flock.l_type = flp->fl_type;
533 flock.l_pid = flp->fl_pid;
538 /* Safe because there are no large files, yet */
539 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
540 if (cmd == F_GETLK64)
542 else if (cmd == F_SETLK64)
544 else if (cmd == F_SETLKW64)
546 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
549 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
552 if ((code == 0 || flp->fl_type == F_UNLCK) &&
553 (cmd == F_SETLK || cmd == F_SETLKW)) {
554 flp->fl_flags &=~ FL_SLEEP;
555 code = flock_lock_file_wait(fp, flp);
556 if (code && flp->fl_type != F_UNLCK) {
557 struct AFS_FLOCK flock2;
559 flock2.l_type = F_UNLCK;
561 afs_lockctl(vcp, &flock2, F_SETLK, credp);
565 /* Convert flock back to Linux's file_lock */
566 flp->fl_type = flock.l_type;
567 flp->fl_pid = flock.l_pid;
575 * essentially the same as afs_fsync() but we need to get the return
576 * code for the sys_close() here, not afs_linux_release(), so call
577 * afs_StoreAllSegments() with AFS_LASTSTORE
580 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
581 afs_linux_flush(struct file *fp, fl_owner_t id)
583 afs_linux_flush(struct file *fp)
586 struct vrequest treq;
590 #if defined(AFS_CACHE_BYPASS)
596 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
604 vcp = VTOAFS(FILE_INODE(fp));
606 code = afs_InitReq(&treq, credp);
609 #if defined(AFS_CACHE_BYPASS)
610 /* If caching is bypassed for this file, or globally, just return 0 */
611 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
614 ObtainReadLock(&vcp->lock);
615 if (vcp->cachingStates & FCSBypass)
617 ReleaseReadLock(&vcp->lock);
620 /* future proof: don't rely on 0 return from afs_InitReq */
626 ObtainSharedLock(&vcp->lock, 535);
627 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
628 UpgradeSToWLock(&vcp->lock, 536);
629 if (!AFS_IS_DISCONNECTED) {
630 code = afs_StoreAllSegments(vcp,
632 AFS_SYNC | AFS_LASTSTORE);
634 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
636 ConvertWToSLock(&vcp->lock);
638 code = afs_CheckCode(code, &treq, 54);
639 ReleaseSharedLock(&vcp->lock);
646 return afs_convert_code(code);
649 struct file_operations afs_dir_fops = {
650 .read = generic_read_dir,
651 .readdir = afs_linux_readdir,
652 #ifdef HAVE_UNLOCKED_IOCTL
653 .unlocked_ioctl = afs_unlocked_xioctl,
657 #ifdef HAVE_COMPAT_IOCTL
658 .compat_ioctl = afs_unlocked_xioctl,
660 .open = afs_linux_open,
661 .release = afs_linux_release,
664 struct file_operations afs_file_fops = {
665 .read = afs_linux_read,
666 .write = afs_linux_write,
667 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
668 .aio_read = generic_file_aio_read,
669 .aio_write = generic_file_aio_write,
671 #ifdef HAVE_UNLOCKED_IOCTL
672 .unlocked_ioctl = afs_unlocked_xioctl,
676 #ifdef HAVE_COMPAT_IOCTL
677 .compat_ioctl = afs_unlocked_xioctl,
679 .mmap = afs_linux_mmap,
680 .open = afs_linux_open,
681 .flush = afs_linux_flush,
682 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
683 .sendfile = generic_file_sendfile,
685 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
686 .splice_write = generic_file_splice_write,
687 .splice_read = generic_file_splice_read,
689 .release = afs_linux_release,
690 .fsync = afs_linux_fsync,
691 .lock = afs_linux_lock,
692 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
693 .flock = afs_linux_flock,
698 /**********************************************************************
699 * AFS Linux dentry operations
700 **********************************************************************/
702 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
703 * that has its mvid (parent dir's fid) pointer set to the wrong directory
704 * due to being mounted in multiple points at once. fix_bad_parent()
705 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
706 * dotdotfid and mtpoint fid members.
708 * dp - dentry to be checked.
709 * credp - credentials
710 * vcp, pvc - item's and parent's vcache pointer
714 * This dentry's vcache's mvid will be set to the correct parent directory's
716 * This root vnode's volume will have its dotdotfid and mtpoint fids set
717 * to the correct parent and mountpoint fids.
721 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
723 struct vcache *avc = NULL;
725 /* force a lookup, so vcp->mvid is fixed up */
726 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
727 if (!avc || vcp != avc) { /* bad, very bad.. */
728 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
729 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
730 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
731 ICL_TYPE_POINTER, dp);
734 AFS_RELE(AFSTOV(avc));
739 /* afs_linux_revalidate
740 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
743 afs_linux_revalidate(struct dentry *dp)
746 struct vcache *vcp = VTOAFS(dp->d_inode);
750 if (afs_shuttingdown)
756 /* Make this a fast path (no crref), since it's called so often. */
757 if (vcp->states & CStatd) {
758 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
760 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
761 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
764 fix_bad_parent(dp); /* check and correct mvid */
773 /* This avoids the crref when we don't have to do it. Watch for
774 * changes in afs_getattr that don't get replicated here!
776 if (vcp->f.states & CStatd &&
777 (!afs_fakestat_enable || vcp->mvstat != 1) &&
779 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
780 code = afs_CopyOutAttrs(vcp, &vattr);
783 code = afs_getattr(vcp, &vattr, credp);
788 afs_fill_inode(AFSTOV(vcp), &vattr);
792 return afs_convert_code(code);
796 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
798 int err = afs_linux_revalidate(dentry);
800 generic_fillattr(dentry->d_inode, stat);
805 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
806 * In kernels 2.2.10 and above, we are passed an additional flags var which
807 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
808 * we are advised to follow the entry if it is a link or to make sure that
809 * it is a directory. But since the kernel itself checks these possibilities
810 * later on, we shouldn't have to do it until later. Perhaps in the future..
812 * The code here assumes that on entry the global lock is not held
815 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
816 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
818 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
822 cred_t *credp = NULL;
823 struct vcache *vcp, *pvcp, *tvc = NULL;
824 struct dentry *parent;
826 struct afs_fakestat_state fakestate;
829 afs_InitFakeStat(&fakestate);
832 parent = dget_parent(dp);
833 pvcp = VTOAFS(parent->d_inode);
834 vcp = VTOAFS(dp->d_inode);
836 if (vcp == afs_globalVp)
839 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
845 if (locked && vcp->mvstat == 1) { /* mount point */
846 if (vcp->mvid && (vcp->f.states & CMValid)) {
849 struct vrequest treq;
851 code = afs_InitReq(&treq, credp);
853 (strcmp(dp->d_name.name, ".directory") == 0)) {
857 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
859 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
860 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
861 /* a mount point, not yet replaced by its directory */
866 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
867 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
868 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
873 /* If the last looker changes, we should make sure the current
874 * looker still has permission to examine this file. This would
875 * always require a crref() which would be "slow".
877 if (vcp->last_looker != treq.uid) {
878 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
881 vcp->last_looker = treq.uid;
886 /* If the parent's DataVersion has changed or the vnode
887 * is longer valid, we need to do a full lookup. VerifyVCache
888 * isn't enough since the vnode may have been renamed.
891 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
897 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
898 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
899 if (!tvc || tvc != vcp) {
904 if (afs_getattr(vcp, &vattr, credp)) {
909 vattr2inode(AFSTOV(vcp), &vattr);
910 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
913 /* should we always update the attributes at this point? */
914 /* unlikely--the vcache entry hasn't changed */
919 /* If this code is ever enabled, we should use dget_parent to handle
920 * getting the parent, and dput() to dispose of it. See above for an
922 pvcp = VTOAFS(dp->d_parent->d_inode);
923 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
927 /* No change in parent's DataVersion so this negative
928 * lookup is still valid. BUT, if a server is down a
929 * negative lookup can result so there should be a
930 * liftime as well. For now, always expire.
943 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
945 /* we hold the global lock if we evaluated a mount point */
952 shrink_dcache_parent(dp);
958 if (have_submounts(dp))
966 afs_dentry_iput(struct dentry *dp, struct inode *ip)
968 struct vcache *vcp = VTOAFS(ip);
971 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
972 (void) afs_InactiveVCache(vcp, NULL);
975 afs_linux_clear_nfsfs_renamed(dp);
981 afs_dentry_delete(struct dentry *dp)
983 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
984 return 1; /* bad inode? */
989 struct dentry_operations afs_dentry_operations = {
990 .d_revalidate = afs_linux_dentry_revalidate,
991 .d_delete = afs_dentry_delete,
992 .d_iput = afs_dentry_iput,
995 /**********************************************************************
996 * AFS Linux inode operations
997 **********************************************************************/
1001 * Merely need to set enough of vattr to get us through the create. Note
1002 * that the higher level code (open_namei) will take care of any tuncation
1003 * explicitly. Exclusive open is also taken care of in open_namei.
1005 * name is in kernel space at this point.
1008 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1009 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1010 struct nameidata *nd)
1012 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1016 cred_t *credp = crref();
1017 const char *name = dp->d_name.name;
1022 vattr.va_mode = mode;
1023 vattr.va_type = mode & S_IFMT;
1026 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1030 struct inode *ip = AFSTOV(vcp);
1032 afs_getattr(vcp, &vattr, credp);
1033 afs_fill_inode(ip, &vattr);
1034 insert_inode_hash(ip);
1035 dp->d_op = &afs_dentry_operations;
1036 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1037 d_instantiate(dp, ip);
1042 return afs_convert_code(code);
1045 /* afs_linux_lookup */
1046 static struct dentry *
1047 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1048 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1049 struct nameidata *nd)
1051 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1054 cred_t *credp = crref();
1055 struct vcache *vcp = NULL;
1056 const char *comp = dp->d_name.name;
1057 struct inode *ip = NULL;
1058 struct dentry *newdp = NULL;
1062 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1068 afs_getattr(vcp, &vattr, credp);
1069 afs_fill_inode(ip, &vattr);
1070 if (hlist_unhashed(&ip->i_hash))
1071 insert_inode_hash(ip);
1073 dp->d_op = &afs_dentry_operations;
1074 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1077 if (ip && S_ISDIR(ip->i_mode)) {
1078 struct dentry *alias;
1080 /* Try to invalidate an existing alias in favor of our new one */
1081 alias = d_find_alias(ip);
1082 /* But not if it's disconnected; then we want d_splice_alias below */
1083 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1084 if (d_invalidate(alias) == 0) {
1093 newdp = d_splice_alias(ip, dp);
1097 /* It's ok for the file to not be found. That's noted by the caller by
1098 * seeing that the dp->d_inode field is NULL.
1100 if (!code || code == ENOENT)
1103 return ERR_PTR(afs_convert_code(code));
1107 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1110 cred_t *credp = crref();
1111 const char *name = newdp->d_name.name;
1112 struct inode *oldip = olddp->d_inode;
1114 /* If afs_link returned the vnode, we could instantiate the
1115 * dentry. Since it's not, we drop this one and do a new lookup.
1120 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1124 return afs_convert_code(code);
1127 /* We have to have a Linux specific sillyrename function, because we
1128 * also have to keep the dcache up to date when we're doing a silly
1129 * rename - so we don't want the generic vnodeops doing this behind our
1134 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1137 struct vcache *tvc = VTOAFS(dentry->d_inode);
1138 struct dentry *__dp = NULL;
1139 char *__name = NULL;
1142 if (afs_linux_nfsfs_renamed(dentry))
1150 osi_FreeSmallSpace(__name);
1151 __name = afs_newname();
1154 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1157 osi_FreeSmallSpace(__name);
1160 } while (__dp->d_inode != NULL);
1163 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1164 VTOAFS(dir), (char *)__dp->d_name.name,
1167 tvc->mvid = (void *) __name;
1170 crfree(tvc->uncred);
1172 tvc->uncred = credp;
1173 tvc->f.states |= CUnlinked;
1174 afs_linux_set_nfsfs_renamed(dentry);
1176 osi_FreeSmallSpace(__name);
1181 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1182 d_move(dentry, __dp);
1191 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1194 cred_t *credp = crref();
1195 const char *name = dp->d_name.name;
1196 struct vcache *tvc = VTOAFS(dp->d_inode);
1198 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1199 && !(tvc->f.states & CUnlinked)) {
1201 code = afs_linux_sillyrename(dip, dp, credp);
1204 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1211 return afs_convert_code(code);
1216 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1219 cred_t *credp = crref();
1221 const char *name = dp->d_name.name;
1223 /* If afs_symlink returned the vnode, we could instantiate the
1224 * dentry. Since it's not, we drop this one and do a new lookup.
1230 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1233 return afs_convert_code(code);
1237 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1240 cred_t *credp = crref();
1241 struct vcache *tvcp = NULL;
1243 const char *name = dp->d_name.name;
1246 vattr.va_mask = ATTR_MODE;
1247 vattr.va_mode = mode;
1249 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1252 struct inode *ip = AFSTOV(tvcp);
1254 afs_getattr(tvcp, &vattr, credp);
1255 afs_fill_inode(ip, &vattr);
1257 dp->d_op = &afs_dentry_operations;
1258 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1259 d_instantiate(dp, ip);
1264 return afs_convert_code(code);
1268 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1271 cred_t *credp = crref();
1272 const char *name = dp->d_name.name;
1274 /* locking kernel conflicts with glock? */
1277 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1280 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1281 * that failed because a directory is not empty. So, we map
1282 * EEXIST to ENOTEMPTY on linux.
1284 if (code == EEXIST) {
1293 return afs_convert_code(code);
1298 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1299 struct inode *newip, struct dentry *newdp)
1302 cred_t *credp = crref();
1303 const char *oldname = olddp->d_name.name;
1304 const char *newname = newdp->d_name.name;
1305 struct dentry *rehash = NULL;
1307 /* Prevent any new references during rename operation. */
1309 if (!d_unhashed(newdp)) {
1314 if (atomic_read(&olddp->d_count) > 1)
1315 shrink_dcache_parent(olddp);
1318 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1322 olddp->d_time = 0; /* force to revalidate */
1328 return afs_convert_code(code);
1332 /* afs_linux_ireadlink
1333 * Internal readlink which can return link contents to user or kernel space.
1334 * Note that the buffer is NOT supposed to be null-terminated.
1337 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1340 cred_t *credp = crref();
1344 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1345 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1349 return maxlen - tuio.uio_resid;
1351 return afs_convert_code(code);
1354 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1355 /* afs_linux_readlink
1356 * Fill target (which is in user space) with contents of symlink.
1359 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1362 struct inode *ip = dp->d_inode;
1365 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1371 /* afs_linux_follow_link
1372 * a file system dependent link following routine.
1374 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1379 name = osi_Alloc(PATH_MAX);
1385 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1393 code = vfs_follow_link(nd, name);
1396 osi_Free(name, PATH_MAX);
1401 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1403 #if defined(AFS_CACHE_BYPASS)
1404 #endif /* defined(AFS_CACHE_BYPASS */
1406 /* Populate a page by filling it from the cache file pointed at by cachefp
1407 * (which contains indicated chunk)
1408 * If task is NULL, the page copy occurs syncronously, and the routine
1409 * returns with page still locked. If task is non-NULL, then page copies
1410 * may occur in the background, and the page will be unlocked when it is
1414 afs_linux_read_cache(struct file *cachefp, struct page *page,
1415 int chunk, struct pagevec *lrupv,
1416 struct afs_pagecopy_task *task) {
1417 loff_t offset = page_offset(page);
1418 struct page *newpage, *cachepage;
1419 struct address_space *cachemapping;
1423 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1427 /* From our offset, we now need to work out which page in the disk
1428 * file it corresponds to. This will be fun ... */
1429 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1431 while (cachepage == NULL) {
1432 cachepage = find_get_page(cachemapping, pageindex);
1435 newpage = page_cache_alloc_cold(cachemapping);
1441 code = add_to_page_cache(newpage, cachemapping,
1442 pageindex, GFP_KERNEL);
1444 cachepage = newpage;
1447 page_cache_get(cachepage);
1448 if (!pagevec_add(lrupv, cachepage))
1449 __pagevec_lru_add_file(lrupv);
1452 page_cache_release(newpage);
1454 if (code != -EEXIST)
1458 lock_page(cachepage);
1462 if (!PageUptodate(cachepage)) {
1463 ClearPageError(cachepage);
1464 code = cachemapping->a_ops->readpage(NULL, cachepage);
1465 if (!code && !task) {
1466 wait_on_page_locked(cachepage);
1469 unlock_page(cachepage);
1473 if (PageUptodate(cachepage)) {
1474 copy_highpage(page, cachepage);
1475 flush_dcache_page(page);
1476 SetPageUptodate(page);
1481 afs_pagecopy_queue_page(task, cachepage, page);
1493 page_cache_release(cachepage);
1499 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1501 loff_t offset = page_offset(pp);
1502 struct inode *ip = FILE_INODE(fp);
1503 struct vcache *avc = VTOAFS(ip);
1505 struct file *cacheFp = NULL;
1508 struct pagevec lrupv;
1510 /* Not a UFS cache, don't do anything */
1511 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1514 /* Can't do anything if the vcache isn't statd , or if the read
1515 * crosses a chunk boundary.
1517 if (!(avc->f.states & CStatd) ||
1518 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1522 ObtainWriteLock(&avc->lock, 911);
1524 /* XXX - See if hinting actually makes things faster !!! */
1526 /* See if we have a suitable entry already cached */
1530 /* We need to lock xdcache, then dcache, to handle situations where
1531 * the hint is on the free list. However, we can't safely do this
1532 * according to the locking hierarchy. So, use a non blocking lock.
1534 ObtainReadLock(&afs_xdcache);
1535 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1537 if (dcLocked && (tdc->index != NULLIDX)
1538 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1539 && tdc->f.chunk == AFS_CHUNK(offset)
1540 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1541 /* Bonus - the hint was correct */
1544 /* Only destroy the hint if its actually invalid, not if there's
1545 * just been a locking failure */
1547 ReleaseReadLock(&tdc->lock);
1554 ReleaseReadLock(&afs_xdcache);
1557 /* No hint, or hint is no longer valid - see if we can get something
1558 * directly from the dcache
1561 tdc = afs_FindDCache(avc, offset);
1564 ReleaseWriteLock(&avc->lock);
1569 ObtainReadLock(&tdc->lock);
1571 /* Is the dcache we've been given currently up to date */
1572 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1573 (tdc->dflags & DFFetching)) {
1574 ReleaseWriteLock(&avc->lock);
1575 ReleaseReadLock(&tdc->lock);
1580 /* Update our hint for future abuse */
1583 /* Okay, so we've now got a cache file that is up to date */
1585 /* XXX - I suspect we should be locking the inodes before we use them! */
1587 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1588 pagevec_init(&lrupv, 0);
1590 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1592 if (pagevec_count(&lrupv))
1593 __pagevec_lru_add_file(&lrupv);
1595 filp_close(cacheFp, NULL);
1598 ReleaseReadLock(&tdc->lock);
1599 ReleaseWriteLock(&avc->lock);
1606 /* afs_linux_readpage
1608 * This function is split into two, because prepare_write/begin_write
1609 * require a readpage call which doesn't unlock the resulting page upon
1613 afs_linux_fillpage(struct file *fp, struct page *pp)
1618 struct iovec *iovecp;
1619 struct inode *ip = FILE_INODE(fp);
1620 afs_int32 cnt = page_count(pp);
1621 struct vcache *avc = VTOAFS(ip);
1622 afs_offs_t offset = page_offset(pp);
1626 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1636 auio = osi_Alloc(sizeof(uio_t));
1637 iovecp = osi_Alloc(sizeof(struct iovec));
1639 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1644 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1645 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1646 99999); /* not a possible code value */
1648 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1650 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1651 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1653 AFS_DISCON_UNLOCK();
1656 /* XXX valid for no-cache also? Check last bits of files... :)
1657 * Cognate code goes in afs_NoCacheFetchProc. */
1658 if (auio->uio_resid) /* zero remainder of page */
1659 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1662 flush_dcache_page(pp);
1663 SetPageUptodate(pp);
1668 osi_Free(auio, sizeof(uio_t));
1669 osi_Free(iovecp, sizeof(struct iovec));
1672 return afs_convert_code(code);
1676 afs_linux_prefetch(struct file *fp, struct page *pp)
1679 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1680 afs_offs_t offset = page_offset(pp);
1682 if (AFS_CHUNKOFFSET(offset) == 0) {
1684 struct vrequest treq;
1689 code = afs_InitReq(&treq, credp);
1690 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1691 tdc = afs_FindDCache(avc, offset);
1693 if (!(tdc->mflags & DFNextStarted))
1694 afs_PrefetchChunk(avc, tdc, credp, &treq);
1697 ReleaseWriteLock(&avc->lock);
1702 return afs_convert_code(code);
1706 #if defined(AFS_CACHE_BYPASS)
1709 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1710 struct list_head *page_list, unsigned num_pages)
1715 struct iovec* iovecp;
1716 struct nocache_read_request *ancr;
1718 struct pagevec lrupv;
1722 struct inode *ip = FILE_INODE(fp);
1723 struct vcache *avc = VTOAFS(ip);
1724 afs_int32 base_index = 0;
1725 afs_int32 page_count = 0;
1728 /* background thread must free: iovecp, auio, ancr */
1729 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1731 auio = osi_Alloc(sizeof(uio_t));
1732 auio->uio_iov = iovecp;
1733 auio->uio_iovcnt = num_pages;
1734 auio->uio_flag = UIO_READ;
1735 auio->uio_seg = AFS_UIOSYS;
1736 auio->uio_resid = num_pages * PAGE_SIZE;
1738 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1740 ancr->offset = auio->uio_offset;
1741 ancr->length = auio->uio_resid;
1743 pagevec_init(&lrupv, 0);
1745 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1747 if(list_empty(page_list))
1750 pp = list_entry(page_list->prev, struct page, lru);
1751 /* If we allocate a page and don't remove it from page_list,
1752 * the page cache gets upset. */
1754 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1755 if(pp->index > isize) {
1762 offset = page_offset(pp);
1763 auio->uio_offset = offset;
1764 base_index = pp->index;
1766 iovecp[page_ix].iov_len = PAGE_SIZE;
1767 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1768 if(base_index != pp->index) {
1771 page_cache_release(pp);
1772 iovecp[page_ix].iov_base = (void *) 0;
1774 ancr->length -= PAGE_SIZE;
1781 page_cache_release(pp);
1782 iovecp[page_ix].iov_base = (void *) 0;
1785 if(!PageLocked(pp)) {
1789 /* increment page refcount--our original design assumed
1790 * that locking it would effectively pin it; protect
1791 * ourselves from the possiblity that this assumption is
1792 * is faulty, at low cost (provided we do not fail to
1793 * do the corresponding decref on the other side) */
1796 /* save the page for background map */
1797 iovecp[page_ix].iov_base = (void*) pp;
1799 /* and put it on the LRU cache */
1800 if (!pagevec_add(&lrupv, pp))
1801 __pagevec_lru_add_file(&lrupv);
1805 /* If there were useful pages in the page list, make sure all pages
1806 * are in the LRU cache, then schedule the read */
1808 if (pagevec_count(&lrupv))
1809 __pagevec_lru_add_file(&lrupv);
1811 code = afs_ReadNoCache(avc, ancr, credp);
1814 /* If there is nothing for the background thread to handle,
1815 * it won't be freeing the things that we never gave it */
1816 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1817 osi_Free(auio, sizeof(uio_t));
1818 osi_Free(ancr, sizeof(struct nocache_read_request));
1820 /* we do not flush, release, or unmap pages--that will be
1821 * done for us by the background thread as each page comes in
1822 * from the fileserver */
1823 return afs_convert_code(code);
1828 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1830 cred_t *credp = NULL;
1832 struct iovec *iovecp;
1833 struct nocache_read_request *ancr;
1837 * Special case: if page is at or past end of file, just zero it and set
1840 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1841 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1842 SetPageUptodate(pp);
1849 /* receiver frees */
1850 auio = osi_Alloc(sizeof(uio_t));
1851 iovecp = osi_Alloc(sizeof(struct iovec));
1853 /* address can be NULL, because we overwrite it with 'pp', below */
1854 setup_uio(auio, iovecp, NULL, page_offset(pp),
1855 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1857 /* save the page for background map */
1858 get_page(pp); /* see above */
1859 auio->uio_iov->iov_base = (void*) pp;
1860 /* the background thread will free this */
1861 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1863 ancr->offset = page_offset(pp);
1864 ancr->length = PAGE_SIZE;
1867 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1870 return afs_convert_code(code);
1874 afs_linux_can_bypass(struct inode *ip) {
1875 switch(cache_bypass_strategy) {
1876 case NEVER_BYPASS_CACHE:
1878 case ALWAYS_BYPASS_CACHE:
1880 case LARGE_FILES_BYPASS_CACHE:
1881 if(i_size_read(ip) > cache_bypass_threshold)
1888 /* Check if a file is permitted to bypass the cache by policy, and modify
1889 * the cache bypass state recorded for that file */
1892 afs_linux_bypass_check(struct inode *ip) {
1895 int bypass = afs_linux_can_bypass(ip);
1898 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1906 afs_linux_bypass_check(struct inode *ip) {
1910 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1914 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1915 struct list_head *page_list, unsigned int num_pages) {
1921 afs_linux_readpage(struct file *fp, struct page *pp)
1925 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1926 code = afs_linux_bypass_readpage(fp, pp);
1928 code = afs_linux_fillpage(fp, pp);
1930 code = afs_linux_prefetch(fp, pp);
1937 /* Readpages reads a number of pages for a particular file. We use
1938 * this to optimise the reading, by limiting the number of times upon which
1939 * we have to lookup, lock and open vcaches and dcaches
1943 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1944 struct list_head *page_list, unsigned int num_pages)
1946 struct inode *inode = mapping->host;
1947 struct vcache *avc = VTOAFS(inode);
1949 struct file *cacheFp = NULL;
1951 unsigned int page_idx;
1953 struct pagevec lrupv;
1954 struct afs_pagecopy_task *task;
1956 if (afs_linux_bypass_check(inode))
1957 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1959 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1963 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1968 ObtainWriteLock(&avc->lock, 912);
1971 task = afs_pagecopy_init_task();
1974 pagevec_init(&lrupv, 0);
1975 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1976 struct page *page = list_entry(page_list->prev, struct page, lru);
1977 list_del(&page->lru);
1978 offset = page_offset(page);
1980 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1982 ReleaseReadLock(&tdc->lock);
1987 filp_close(cacheFp, NULL);
1992 if ((tdc = afs_FindDCache(avc, offset))) {
1993 ObtainReadLock(&tdc->lock);
1994 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1995 (tdc->dflags & DFFetching)) {
1996 ReleaseReadLock(&tdc->lock);
2003 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2006 if (tdc && !add_to_page_cache(page, mapping, page->index,
2008 page_cache_get(page);
2009 if (!pagevec_add(&lrupv, page))
2010 __pagevec_lru_add_file(&lrupv);
2012 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2014 page_cache_release(page);
2016 if (pagevec_count(&lrupv))
2017 __pagevec_lru_add_file(&lrupv);
2020 filp_close(cacheFp, NULL);
2022 afs_pagecopy_put_task(task);
2026 ReleaseReadLock(&tdc->lock);
2030 ReleaseWriteLock(&avc->lock);
2035 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2038 afs_linux_prepare_writeback(struct vcache *avc) {
2039 if (avc->f.states & CPageWrite) {
2040 return AOP_WRITEPAGE_ACTIVATE;
2042 avc->f.states |= CPageWrite;
2047 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2048 struct vrequest treq;
2051 if (!afs_InitReq(&treq, credp))
2052 code = afs_DoPartialWrite(avc, &treq);
2054 return afs_convert_code(code);
2058 afs_linux_complete_writeback(struct vcache *avc) {
2059 avc->f.states &= ~CPageWrite;
2062 /* Writeback a given page syncronously. Called with no AFS locks held */
2064 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2065 unsigned long offset, unsigned int count,
2068 struct vcache *vcp = VTOAFS(ip);
2076 buffer = kmap(pp) + offset;
2077 base = page_offset(pp) + offset;
2080 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2081 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2082 ICL_TYPE_INT32, 99999);
2084 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2086 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2088 i_size_write(ip, vcp->f.m.Length);
2089 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2091 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2093 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2094 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2095 ICL_TYPE_INT32, code);
2104 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2105 unsigned long offset, unsigned int count)
2109 struct vcache *vcp = VTOAFS(ip);
2112 /* Catch recursive writeback. This occurs if the kernel decides
2113 * writeback is required whilst we are writing to the cache, or
2114 * flushing to the server. When we're running syncronously (as
2115 * opposed to from writepage) we can't actually do anything about
2116 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2119 ObtainWriteLock(&vcp->lock, 532);
2120 afs_linux_prepare_writeback(vcp);
2121 ReleaseWriteLock(&vcp->lock);
2125 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2128 ObtainWriteLock(&vcp->lock, 533);
2130 code1 = afs_linux_dopartialwrite(vcp, credp);
2131 afs_linux_complete_writeback(vcp);
2132 ReleaseWriteLock(&vcp->lock);
2143 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2144 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2146 afs_linux_writepage(struct page *pp)
2149 struct address_space *mapping = pp->mapping;
2150 struct inode *inode;
2153 unsigned int to = PAGE_CACHE_SIZE;
2158 if (PageReclaim(pp)) {
2159 return AOP_WRITEPAGE_ACTIVATE;
2160 /* XXX - Do we need to redirty the page here? */
2165 inode = mapping->host;
2166 vcp = VTOAFS(inode);
2167 isize = i_size_read(inode);
2169 /* Don't defeat an earlier truncate */
2170 if (page_offset(pp) > isize) {
2171 set_page_writeback(pp);
2177 ObtainWriteLock(&vcp->lock, 537);
2178 code = afs_linux_prepare_writeback(vcp);
2179 if (code == AOP_WRITEPAGE_ACTIVATE) {
2180 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2181 * to return with the page still locked */
2182 ReleaseWriteLock(&vcp->lock);
2187 /* Grab the creds structure currently held in the vnode, and
2188 * get a reference to it, in case it goes away ... */
2194 ReleaseWriteLock(&vcp->lock);
2197 set_page_writeback(pp);
2199 SetPageUptodate(pp);
2201 /* We can unlock the page here, because it's protected by the
2202 * page_writeback flag. This should make us less vulnerable to
2203 * deadlocking in afs_write and afs_DoPartialWrite
2207 /* If this is the final page, then just write the number of bytes that
2208 * are actually in it */
2209 if ((isize - page_offset(pp)) < to )
2210 to = isize - page_offset(pp);
2212 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2215 ObtainWriteLock(&vcp->lock, 538);
2217 /* As much as we might like to ignore a file server error here,
2218 * and just try again when we close(), unfortunately StoreAllSegments
2219 * will invalidate our chunks if the server returns a permanent error,
2220 * so we need to at least try and get that error back to the user
2223 code1 = afs_linux_dopartialwrite(vcp, credp);
2225 afs_linux_complete_writeback(vcp);
2226 ReleaseWriteLock(&vcp->lock);
2231 end_page_writeback(pp);
2232 page_cache_release(pp);
2243 /* afs_linux_permission
2244 * Check access rights - returns error if can't check or permission denied.
2247 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2248 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2250 afs_linux_permission(struct inode *ip, int mode)
2254 cred_t *credp = crref();
2258 if (mode & MAY_EXEC)
2260 if (mode & MAY_READ)
2262 if (mode & MAY_WRITE)
2264 code = afs_access(VTOAFS(ip), tmp, credp);
2268 return afs_convert_code(code);
2272 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2276 struct inode *inode = FILE_INODE(file);
2277 loff_t pagebase = page_offset(page);
2279 if (i_size_read(inode) < (pagebase + offset))
2280 i_size_write(inode, pagebase + offset);
2282 if (PageChecked(page)) {
2283 SetPageUptodate(page);
2284 ClearPageChecked(page);
2287 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2293 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2297 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2298 * prepare_write. Essentially, if the page exists within the file,
2299 * and is not being fully written, then we should populate it.
2302 if (!PageUptodate(page)) {
2303 loff_t pagebase = page_offset(page);
2304 loff_t isize = i_size_read(page->mapping->host);
2306 /* Is the location we are writing to beyond the end of the file? */
2307 if (pagebase >= isize ||
2308 ((from == 0) && (pagebase + to) >= isize)) {
2309 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2310 SetPageChecked(page);
2311 /* Are we we writing a full page */
2312 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2313 SetPageChecked(page);
2314 /* Is the page readable, if it's wronly, we don't care, because we're
2315 * not actually going to read from it ... */
2316 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2317 /* We don't care if fillpage fails, because if it does the page
2318 * won't be marked as up to date
2320 afs_linux_fillpage(file, page);
2326 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2328 afs_linux_write_end(struct file *file, struct address_space *mapping,
2329 loff_t pos, unsigned len, unsigned copied,
2330 struct page *page, void *fsdata)
2333 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2335 code = afs_linux_commit_write(file, page, from, from + len);
2338 page_cache_release(page);
2343 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2344 loff_t pos, unsigned len, unsigned flags,
2345 struct page **pagep, void **fsdata)
2348 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2349 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2352 page = grab_cache_page_write_begin(mapping, index, flags);
2355 code = afs_linux_prepare_write(file, page, from, from + len);
2358 page_cache_release(page);
2366 static struct inode_operations afs_file_iops = {
2367 .permission = afs_linux_permission,
2368 .getattr = afs_linux_getattr,
2369 .setattr = afs_notify_change,
2372 static struct address_space_operations afs_file_aops = {
2373 .readpage = afs_linux_readpage,
2374 .readpages = afs_linux_readpages,
2375 .writepage = afs_linux_writepage,
2376 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2377 .write_begin = afs_linux_write_begin,
2378 .write_end = afs_linux_write_end,
2380 .commit_write = afs_linux_commit_write,
2381 .prepare_write = afs_linux_prepare_write,
2386 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2387 * by what sort of operation is allowed.....
2390 static struct inode_operations afs_dir_iops = {
2391 .setattr = afs_notify_change,
2392 .create = afs_linux_create,
2393 .lookup = afs_linux_lookup,
2394 .link = afs_linux_link,
2395 .unlink = afs_linux_unlink,
2396 .symlink = afs_linux_symlink,
2397 .mkdir = afs_linux_mkdir,
2398 .rmdir = afs_linux_rmdir,
2399 .rename = afs_linux_rename,
2400 .getattr = afs_linux_getattr,
2401 .permission = afs_linux_permission,
2404 /* We really need a separate symlink set of ops, since do_follow_link()
2405 * determines if it _is_ a link by checking if the follow_link op is set.
2407 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2409 afs_symlink_filler(struct file *file, struct page *page)
2411 struct inode *ip = (struct inode *)page->mapping->host;
2412 char *p = (char *)kmap(page);
2416 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2421 p[code] = '\0'; /* null terminate? */
2423 SetPageUptodate(page);
2435 static struct address_space_operations afs_symlink_aops = {
2436 .readpage = afs_symlink_filler
2438 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2440 static struct inode_operations afs_symlink_iops = {
2441 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2442 .readlink = page_readlink,
2443 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2444 .follow_link = page_follow_link,
2446 .follow_link = page_follow_link_light,
2447 .put_link = page_put_link,
2449 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2450 .readlink = afs_linux_readlink,
2451 .follow_link = afs_linux_follow_link,
2452 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2453 .setattr = afs_notify_change,
2457 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2461 vattr2inode(ip, vattr);
2463 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2464 /* Reset ops if symlink or directory. */
2465 if (S_ISREG(ip->i_mode)) {
2466 ip->i_op = &afs_file_iops;
2467 ip->i_fop = &afs_file_fops;
2468 ip->i_data.a_ops = &afs_file_aops;
2470 } else if (S_ISDIR(ip->i_mode)) {
2471 ip->i_op = &afs_dir_iops;
2472 ip->i_fop = &afs_dir_fops;
2474 } else if (S_ISLNK(ip->i_mode)) {
2475 ip->i_op = &afs_symlink_iops;
2476 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2477 ip->i_data.a_ops = &afs_symlink_aops;
2478 ip->i_mapping = &ip->i_data;