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;
189 register struct dcache *tdc;
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_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;
520 return afs_convert_code(code);
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_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;
570 return afs_convert_code(code);
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 */
625 ObtainSharedLock(&vcp->lock, 535);
626 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
627 UpgradeSToWLock(&vcp->lock, 536);
628 if (!AFS_IS_DISCONNECTED) {
629 code = afs_StoreAllSegments(vcp,
631 AFS_SYNC | AFS_LASTSTORE);
633 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
635 ConvertWToSLock(&vcp->lock);
637 code = afs_CheckCode(code, &treq, 54);
638 ReleaseSharedLock(&vcp->lock);
645 return afs_convert_code(code);
648 struct file_operations afs_dir_fops = {
649 .read = generic_read_dir,
650 .readdir = afs_linux_readdir,
651 #ifdef HAVE_UNLOCKED_IOCTL
652 .unlocked_ioctl = afs_unlocked_xioctl,
656 #ifdef HAVE_COMPAT_IOCTL
657 .compat_ioctl = afs_unlocked_xioctl,
659 .open = afs_linux_open,
660 .release = afs_linux_release,
663 struct file_operations afs_file_fops = {
664 .read = afs_linux_read,
665 .write = afs_linux_write,
666 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
667 .aio_read = generic_file_aio_read,
668 .aio_write = generic_file_aio_write,
670 #ifdef HAVE_UNLOCKED_IOCTL
671 .unlocked_ioctl = afs_unlocked_xioctl,
675 #ifdef HAVE_COMPAT_IOCTL
676 .compat_ioctl = afs_unlocked_xioctl,
678 .mmap = afs_linux_mmap,
679 .open = afs_linux_open,
680 .flush = afs_linux_flush,
681 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
682 .sendfile = generic_file_sendfile,
684 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
685 .splice_write = generic_file_splice_write,
686 .splice_read = generic_file_splice_read,
688 .release = afs_linux_release,
689 .fsync = afs_linux_fsync,
690 .lock = afs_linux_lock,
691 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
692 .flock = afs_linux_flock,
697 /**********************************************************************
698 * AFS Linux dentry operations
699 **********************************************************************/
701 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
702 * that has its mvid (parent dir's fid) pointer set to the wrong directory
703 * due to being mounted in multiple points at once. If so, check_bad_parent()
704 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
705 * dotdotfid and mtpoint fid members.
707 * dp - dentry to be checked.
711 * This dentry's vcache's mvid will be set to the correct parent directory's
713 * This root vnode's volume will have its dotdotfid and mtpoint fids set
714 * to the correct parent and mountpoint fids.
718 check_bad_parent(struct dentry *dp)
721 struct dentry *parent;
722 struct vcache *vcp, *pvc, *avc = NULL;
724 vcp = VTOAFS(dp->d_inode);
725 parent = dget_parent(dp);
726 pvc = VTOAFS(parent->d_inode);
728 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
731 /* force a lookup, so vcp->mvid is fixed up */
732 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
733 if (!avc || vcp != avc) { /* bad, very bad.. */
734 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
735 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
736 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
737 ICL_TYPE_POINTER, dp);
740 AFS_RELE(AFSTOV(avc));
749 /* afs_linux_revalidate
750 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
753 afs_linux_revalidate(struct dentry *dp)
756 struct vcache *vcp = VTOAFS(dp->d_inode);
760 if (afs_shuttingdown)
766 /* Make this a fast path (no crref), since it's called so often. */
767 if (vcp->f.states & CStatd) {
769 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
770 check_bad_parent(dp); /* check and correct mvid */
777 /* This avoids the crref when we don't have to do it. Watch for
778 * changes in afs_getattr that don't get replicated here!
780 if (vcp->f.states & CStatd &&
781 (!afs_fakestat_enable || vcp->mvstat != 1) &&
783 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
784 code = afs_CopyOutAttrs(vcp, &vattr);
787 code = afs_getattr(vcp, &vattr, credp);
791 afs_fill_inode(AFSTOV(vcp), &vattr);
795 return afs_convert_code(code);
799 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
801 int err = afs_linux_revalidate(dentry);
803 generic_fillattr(dentry->d_inode, stat);
808 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
809 * In kernels 2.2.10 and above, we are passed an additional flags var which
810 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
811 * we are advised to follow the entry if it is a link or to make sure that
812 * it is a directory. But since the kernel itself checks these possibilities
813 * later on, we shouldn't have to do it until later. Perhaps in the future..
816 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
817 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
819 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
823 cred_t *credp = NULL;
824 struct vcache *vcp, *pvcp, *tvc = NULL;
825 struct dentry *parent;
827 struct afs_fakestat_state fakestate;
830 afs_InitFakeStat(&fakestate);
833 vcp = VTOAFS(dp->d_inode);
835 if (vcp == afs_globalVp)
838 if (vcp->mvstat == 1) { /* mount point */
839 if (vcp->mvid && (vcp->f.states & CMValid)) {
842 struct vrequest treq;
845 code = afs_InitReq(&treq, credp);
847 (strcmp(dp->d_name.name, ".directory") == 0)) {
851 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
853 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
854 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
855 /* a mount point, not yet replaced by its directory */
860 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
861 check_bad_parent(dp); /* check and correct mvid */
864 /* If the last looker changes, we should make sure the current
865 * looker still has permission to examine this file. This would
866 * always require a crref() which would be "slow".
868 if (vcp->last_looker != treq.uid) {
869 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
872 vcp->last_looker = treq.uid;
876 parent = dget_parent(dp);
877 pvcp = VTOAFS(parent->d_inode);
879 /* If the parent's DataVersion has changed or the vnode
880 * is longer valid, we need to do a full lookup. VerifyVCache
881 * isn't enough since the vnode may have been renamed.
884 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
887 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
888 if (!tvc || tvc != vcp) {
893 if (afs_getattr(vcp, &vattr, credp)) {
898 vattr2inode(AFSTOV(vcp), &vattr);
899 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
902 /* should we always update the attributes at this point? */
903 /* unlikely--the vcache entry hasn't changed */
908 /* If this code is ever enabled, we should use dget_parent to handle
909 * getting the parent, and dput() to dispose of it. See above for an
911 pvcp = VTOAFS(dp->d_parent->d_inode);
912 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
916 /* No change in parent's DataVersion so this negative
917 * lookup is still valid. BUT, if a server is down a
918 * negative lookup can result so there should be a
919 * liftime as well. For now, always expire.
932 afs_PutFakeStat(&fakestate);
938 shrink_dcache_parent(dp);
944 if (have_submounts(dp))
952 afs_dentry_iput(struct dentry *dp, struct inode *ip)
954 struct vcache *vcp = VTOAFS(ip);
957 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
958 (void) afs_InactiveVCache(vcp, NULL);
961 afs_linux_clear_nfsfs_renamed(dp);
967 afs_dentry_delete(struct dentry *dp)
969 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
970 return 1; /* bad inode? */
975 struct dentry_operations afs_dentry_operations = {
976 .d_revalidate = afs_linux_dentry_revalidate,
977 .d_delete = afs_dentry_delete,
978 .d_iput = afs_dentry_iput,
981 /**********************************************************************
982 * AFS Linux inode operations
983 **********************************************************************/
987 * Merely need to set enough of vattr to get us through the create. Note
988 * that the higher level code (open_namei) will take care of any tuncation
989 * explicitly. Exclusive open is also taken care of in open_namei.
991 * name is in kernel space at this point.
994 #ifdef IOP_CREATE_TAKES_NAMEIDATA
995 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
996 struct nameidata *nd)
998 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1002 cred_t *credp = crref();
1003 const char *name = dp->d_name.name;
1008 vattr.va_mode = mode;
1009 vattr.va_type = mode & S_IFMT;
1012 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1016 struct inode *ip = AFSTOV(vcp);
1018 afs_getattr(vcp, &vattr, credp);
1019 afs_fill_inode(ip, &vattr);
1020 insert_inode_hash(ip);
1021 dp->d_op = &afs_dentry_operations;
1022 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1023 d_instantiate(dp, ip);
1028 return afs_convert_code(code);
1031 /* afs_linux_lookup */
1032 static struct dentry *
1033 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1034 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1035 struct nameidata *nd)
1037 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1040 cred_t *credp = crref();
1041 struct vcache *vcp = NULL;
1042 const char *comp = dp->d_name.name;
1043 struct inode *ip = NULL;
1044 struct dentry *newdp = NULL;
1048 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1054 afs_getattr(vcp, &vattr, credp);
1055 afs_fill_inode(ip, &vattr);
1056 if (hlist_unhashed(&ip->i_hash))
1057 insert_inode_hash(ip);
1059 dp->d_op = &afs_dentry_operations;
1060 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1063 if (ip && S_ISDIR(ip->i_mode)) {
1064 struct dentry *alias;
1066 /* Try to invalidate an existing alias in favor of our new one */
1067 alias = d_find_alias(ip);
1068 /* But not if it's disconnected; then we want d_splice_alias below */
1069 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1070 if (d_invalidate(alias) == 0) {
1079 newdp = d_splice_alias(ip, dp);
1083 /* It's ok for the file to not be found. That's noted by the caller by
1084 * seeing that the dp->d_inode field is NULL.
1086 if (!code || code == ENOENT)
1089 return ERR_PTR(afs_convert_code(code));
1093 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1096 cred_t *credp = crref();
1097 const char *name = newdp->d_name.name;
1098 struct inode *oldip = olddp->d_inode;
1100 /* If afs_link returned the vnode, we could instantiate the
1101 * dentry. Since it's not, we drop this one and do a new lookup.
1106 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1110 return afs_convert_code(code);
1113 /* We have to have a Linux specific sillyrename function, because we
1114 * also have to keep the dcache up to date when we're doing a silly
1115 * rename - so we don't want the generic vnodeops doing this behind our
1120 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1123 struct vcache *tvc = VTOAFS(dentry->d_inode);
1124 struct dentry *__dp = NULL;
1125 char *__name = NULL;
1128 if (afs_linux_nfsfs_renamed(dentry))
1136 osi_FreeSmallSpace(__name);
1137 __name = afs_newname();
1140 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1143 osi_FreeSmallSpace(__name);
1146 } while (__dp->d_inode != NULL);
1149 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1150 VTOAFS(dir), (char *)__dp->d_name.name,
1153 tvc->mvid = (void *) __name;
1156 crfree(tvc->uncred);
1158 tvc->uncred = credp;
1159 tvc->f.states |= CUnlinked;
1160 afs_linux_set_nfsfs_renamed(dentry);
1162 osi_FreeSmallSpace(__name);
1167 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1168 d_move(dentry, __dp);
1177 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1180 cred_t *credp = crref();
1181 const char *name = dp->d_name.name;
1182 struct vcache *tvc = VTOAFS(dp->d_inode);
1184 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1185 && !(tvc->f.states & CUnlinked)) {
1187 code = afs_linux_sillyrename(dip, dp, credp);
1190 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1197 return afs_convert_code(code);
1202 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1205 cred_t *credp = crref();
1207 const char *name = dp->d_name.name;
1209 /* If afs_symlink returned the vnode, we could instantiate the
1210 * dentry. Since it's not, we drop this one and do a new lookup.
1216 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1219 return afs_convert_code(code);
1223 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1226 cred_t *credp = crref();
1227 struct vcache *tvcp = NULL;
1229 const char *name = dp->d_name.name;
1232 vattr.va_mask = ATTR_MODE;
1233 vattr.va_mode = mode;
1235 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1238 struct inode *ip = AFSTOV(tvcp);
1240 afs_getattr(tvcp, &vattr, credp);
1241 afs_fill_inode(ip, &vattr);
1243 dp->d_op = &afs_dentry_operations;
1244 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1245 d_instantiate(dp, ip);
1250 return afs_convert_code(code);
1254 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1257 cred_t *credp = crref();
1258 const char *name = dp->d_name.name;
1260 /* locking kernel conflicts with glock? */
1263 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1266 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1267 * that failed because a directory is not empty. So, we map
1268 * EEXIST to ENOTEMPTY on linux.
1270 if (code == EEXIST) {
1279 return afs_convert_code(code);
1284 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1285 struct inode *newip, struct dentry *newdp)
1288 cred_t *credp = crref();
1289 const char *oldname = olddp->d_name.name;
1290 const char *newname = newdp->d_name.name;
1291 struct dentry *rehash = NULL;
1293 /* Prevent any new references during rename operation. */
1295 if (!d_unhashed(newdp)) {
1300 if (atomic_read(&olddp->d_count) > 1)
1301 shrink_dcache_parent(olddp);
1304 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1308 olddp->d_time = 0; /* force to revalidate */
1314 return afs_convert_code(code);
1318 /* afs_linux_ireadlink
1319 * Internal readlink which can return link contents to user or kernel space.
1320 * Note that the buffer is NOT supposed to be null-terminated.
1323 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1326 cred_t *credp = crref();
1330 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1331 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1335 return maxlen - tuio.uio_resid;
1337 return afs_convert_code(code);
1340 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1341 /* afs_linux_readlink
1342 * Fill target (which is in user space) with contents of symlink.
1345 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1348 struct inode *ip = dp->d_inode;
1351 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1357 /* afs_linux_follow_link
1358 * a file system dependent link following routine.
1360 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1365 name = osi_Alloc(PATH_MAX);
1371 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1379 code = vfs_follow_link(nd, name);
1382 osi_Free(name, PATH_MAX);
1387 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1389 #if defined(AFS_CACHE_BYPASS)
1390 #endif /* defined(AFS_CACHE_BYPASS */
1392 /* Populate a page by filling it from the cache file pointed at by cachefp
1393 * (which contains indicated chunk)
1394 * If task is NULL, the page copy occurs syncronously, and the routine
1395 * returns with page still locked. If task is non-NULL, then page copies
1396 * may occur in the background, and the page will be unlocked when it is
1400 afs_linux_read_cache(struct file *cachefp, struct page *page,
1401 int chunk, struct pagevec *lrupv,
1402 struct afs_pagecopy_task *task) {
1403 loff_t offset = page_offset(page);
1404 struct page *newpage, *cachepage;
1405 struct address_space *cachemapping;
1409 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1413 /* From our offset, we now need to work out which page in the disk
1414 * file it corresponds to. This will be fun ... */
1415 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1417 while (cachepage == NULL) {
1418 cachepage = find_get_page(cachemapping, pageindex);
1421 newpage = page_cache_alloc_cold(cachemapping);
1427 code = add_to_page_cache(newpage, cachemapping,
1428 pageindex, GFP_KERNEL);
1430 cachepage = newpage;
1433 page_cache_get(cachepage);
1434 if (!pagevec_add(lrupv, cachepage))
1435 __pagevec_lru_add_file(lrupv);
1438 page_cache_release(newpage);
1440 if (code != -EEXIST)
1444 lock_page(cachepage);
1448 if (!PageUptodate(cachepage)) {
1449 ClearPageError(cachepage);
1450 code = cachemapping->a_ops->readpage(NULL, cachepage);
1451 if (!code && !task) {
1452 wait_on_page_locked(cachepage);
1455 unlock_page(cachepage);
1459 if (PageUptodate(cachepage)) {
1460 copy_highpage(page, cachepage);
1461 flush_dcache_page(page);
1462 SetPageUptodate(page);
1467 afs_pagecopy_queue_page(task, cachepage, page);
1479 page_cache_release(cachepage);
1485 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1487 loff_t offset = page_offset(pp);
1488 struct inode *ip = FILE_INODE(fp);
1489 struct vcache *avc = VTOAFS(ip);
1491 struct file *cacheFp = NULL;
1494 struct pagevec lrupv;
1496 /* Not a UFS cache, don't do anything */
1497 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1500 /* Can't do anything if the vcache isn't statd , or if the read
1501 * crosses a chunk boundary.
1503 if (!(avc->f.states & CStatd) ||
1504 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1508 ObtainWriteLock(&avc->lock, 911);
1510 /* XXX - See if hinting actually makes things faster !!! */
1512 /* See if we have a suitable entry already cached */
1516 /* We need to lock xdcache, then dcache, to handle situations where
1517 * the hint is on the free list. However, we can't safely do this
1518 * according to the locking hierarchy. So, use a non blocking lock.
1520 ObtainReadLock(&afs_xdcache);
1521 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1523 if (dcLocked && (tdc->index != NULLIDX)
1524 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1525 && tdc->f.chunk == AFS_CHUNK(offset)
1526 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1527 /* Bonus - the hint was correct */
1530 /* Only destroy the hint if its actually invalid, not if there's
1531 * just been a locking failure */
1533 ReleaseReadLock(&tdc->lock);
1540 ReleaseReadLock(&afs_xdcache);
1543 /* No hint, or hint is no longer valid - see if we can get something
1544 * directly from the dcache
1547 tdc = afs_FindDCache(avc, offset);
1550 ReleaseWriteLock(&avc->lock);
1555 ObtainReadLock(&tdc->lock);
1557 /* Is the dcache we've been given currently up to date */
1558 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1559 (tdc->dflags & DFFetching)) {
1560 ReleaseWriteLock(&avc->lock);
1561 ReleaseReadLock(&tdc->lock);
1566 /* Update our hint for future abuse */
1569 /* Okay, so we've now got a cache file that is up to date */
1571 /* XXX - I suspect we should be locking the inodes before we use them! */
1573 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1574 pagevec_init(&lrupv, 0);
1576 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1578 if (pagevec_count(&lrupv))
1579 __pagevec_lru_add_file(&lrupv);
1581 filp_close(cacheFp, NULL);
1584 ReleaseReadLock(&tdc->lock);
1585 ReleaseWriteLock(&avc->lock);
1592 /* afs_linux_readpage
1594 * This function is split into two, because prepare_write/begin_write
1595 * require a readpage call which doesn't unlock the resulting page upon
1599 afs_linux_fillpage(struct file *fp, struct page *pp)
1604 struct iovec *iovecp;
1605 struct inode *ip = FILE_INODE(fp);
1606 afs_int32 cnt = page_count(pp);
1607 struct vcache *avc = VTOAFS(ip);
1608 afs_offs_t offset = page_offset(pp);
1612 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1622 auio = osi_Alloc(sizeof(uio_t));
1623 iovecp = osi_Alloc(sizeof(struct iovec));
1625 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1630 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1631 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1632 99999); /* not a possible code value */
1634 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1636 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1637 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1639 AFS_DISCON_UNLOCK();
1642 /* XXX valid for no-cache also? Check last bits of files... :)
1643 * Cognate code goes in afs_NoCacheFetchProc. */
1644 if (auio->uio_resid) /* zero remainder of page */
1645 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1648 flush_dcache_page(pp);
1649 SetPageUptodate(pp);
1654 osi_Free(auio, sizeof(uio_t));
1655 osi_Free(iovecp, sizeof(struct iovec));
1658 return afs_convert_code(code);
1662 afs_linux_prefetch(struct file *fp, struct page *pp)
1665 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1666 afs_offs_t offset = page_offset(pp);
1668 if (AFS_CHUNKOFFSET(offset) == 0) {
1670 struct vrequest treq;
1675 code = afs_InitReq(&treq, credp);
1676 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1677 tdc = afs_FindDCache(avc, offset);
1679 if (!(tdc->mflags & DFNextStarted))
1680 afs_PrefetchChunk(avc, tdc, credp, &treq);
1683 ReleaseWriteLock(&avc->lock);
1688 return afs_convert_code(code);
1692 #if defined(AFS_CACHE_BYPASS)
1695 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1696 struct list_head *page_list, unsigned num_pages)
1701 struct iovec* iovecp;
1702 struct nocache_read_request *ancr;
1704 struct pagevec lrupv;
1708 struct inode *ip = FILE_INODE(fp);
1709 struct vcache *avc = VTOAFS(ip);
1710 afs_int32 base_index = 0;
1711 afs_int32 page_count = 0;
1714 /* background thread must free: iovecp, auio, ancr */
1715 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1717 auio = osi_Alloc(sizeof(uio_t));
1718 auio->uio_iov = iovecp;
1719 auio->uio_iovcnt = num_pages;
1720 auio->uio_flag = UIO_READ;
1721 auio->uio_seg = AFS_UIOSYS;
1722 auio->uio_resid = num_pages * PAGE_SIZE;
1724 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1726 ancr->offset = auio->uio_offset;
1727 ancr->length = auio->uio_resid;
1729 pagevec_init(&lrupv, 0);
1731 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1733 if(list_empty(page_list))
1736 pp = list_entry(page_list->prev, struct page, lru);
1737 /* If we allocate a page and don't remove it from page_list,
1738 * the page cache gets upset. */
1740 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1741 if(pp->index > isize) {
1748 offset = page_offset(pp);
1749 auio->uio_offset = offset;
1750 base_index = pp->index;
1752 iovecp[page_ix].iov_len = PAGE_SIZE;
1753 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1754 if(base_index != pp->index) {
1757 page_cache_release(pp);
1758 iovecp[page_ix].iov_base = (void *) 0;
1766 page_cache_release(pp);
1767 iovecp[page_ix].iov_base = (void *) 0;
1770 if(!PageLocked(pp)) {
1774 /* save the page for background map */
1775 iovecp[page_ix].iov_base = (void*) pp;
1777 /* and put it on the LRU cache */
1778 if (!pagevec_add(&lrupv, pp))
1779 __pagevec_lru_add_file(&lrupv);
1783 /* If there were useful pages in the page list, make sure all pages
1784 * are in the LRU cache, then schedule the read */
1786 if (pagevec_count(&lrupv))
1787 __pagevec_lru_add_file(&lrupv);
1789 code = afs_ReadNoCache(avc, ancr, credp);
1792 /* If there is nothing for the background thread to handle,
1793 * it won't be freeing the things that we never gave it */
1794 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1795 osi_Free(auio, sizeof(uio_t));
1796 osi_Free(ancr, sizeof(struct nocache_read_request));
1798 /* we do not flush, release, or unmap pages--that will be
1799 * done for us by the background thread as each page comes in
1800 * from the fileserver */
1801 return afs_convert_code(code);
1806 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1808 cred_t *credp = NULL;
1810 struct iovec *iovecp;
1811 struct nocache_read_request *ancr;
1816 /* receiver frees */
1817 auio = osi_Alloc(sizeof(uio_t));
1818 iovecp = osi_Alloc(sizeof(struct iovec));
1820 /* address can be NULL, because we overwrite it with 'pp', below */
1821 setup_uio(auio, iovecp, NULL, page_offset(pp),
1822 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1824 /* save the page for background map */
1825 /* XXX - Shouldn't we get a reference count here? */
1826 auio->uio_iov->iov_base = (void*) pp;
1827 /* the background thread will free this */
1828 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1830 ancr->offset = page_offset(pp);
1831 ancr->length = PAGE_SIZE;
1834 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1837 return afs_convert_code(code);
1841 afs_linux_can_bypass(struct inode *ip) {
1842 switch(cache_bypass_strategy) {
1843 case NEVER_BYPASS_CACHE:
1845 case ALWAYS_BYPASS_CACHE:
1847 case LARGE_FILES_BYPASS_CACHE:
1848 if(i_size_read(ip) > cache_bypass_threshold)
1855 /* Check if a file is permitted to bypass the cache by policy, and modify
1856 * the cache bypass state recorded for that file */
1859 afs_linux_bypass_check(struct inode *ip) {
1862 int bypass = afs_linux_can_bypass(ip);
1865 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1873 afs_linux_bypass_check(struct inode *ip) {
1877 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1881 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1882 struct list_head *page_list, unsigned int num_pages) {
1888 afs_linux_readpage(struct file *fp, struct page *pp)
1892 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1893 code = afs_linux_bypass_readpage(fp, pp);
1895 code = afs_linux_fillpage(fp, pp);
1897 code = afs_linux_prefetch(fp, pp);
1904 /* Readpages reads a number of pages for a particular file. We use
1905 * this to optimise the reading, by limiting the number of times upon which
1906 * we have to lookup, lock and open vcaches and dcaches
1910 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1911 struct list_head *page_list, unsigned int num_pages)
1913 struct inode *inode = mapping->host;
1914 struct vcache *avc = VTOAFS(inode);
1916 struct file *cacheFp = NULL;
1918 unsigned int page_idx;
1920 struct pagevec lrupv;
1921 struct afs_pagecopy_task *task;
1923 if (afs_linux_bypass_check(inode))
1924 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1926 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1930 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1935 ObtainWriteLock(&avc->lock, 912);
1938 task = afs_pagecopy_init_task();
1941 pagevec_init(&lrupv, 0);
1942 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1943 struct page *page = list_entry(page_list->prev, struct page, lru);
1944 list_del(&page->lru);
1945 offset = page_offset(page);
1947 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1949 ReleaseReadLock(&tdc->lock);
1954 filp_close(cacheFp, NULL);
1959 if ((tdc = afs_FindDCache(avc, offset))) {
1960 ObtainReadLock(&tdc->lock);
1961 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1962 (tdc->dflags & DFFetching)) {
1963 ReleaseReadLock(&tdc->lock);
1970 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1973 if (tdc && !add_to_page_cache(page, mapping, page->index,
1975 page_cache_get(page);
1976 if (!pagevec_add(&lrupv, page))
1977 __pagevec_lru_add_file(&lrupv);
1979 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
1981 page_cache_release(page);
1983 if (pagevec_count(&lrupv))
1984 __pagevec_lru_add_file(&lrupv);
1987 filp_close(cacheFp, NULL);
1989 afs_pagecopy_put_task(task);
1993 ReleaseReadLock(&tdc->lock);
1997 ReleaseWriteLock(&avc->lock);
2002 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2005 afs_linux_prepare_writeback(struct vcache *avc) {
2006 if (avc->f.states & CPageWrite) {
2007 return AOP_WRITEPAGE_ACTIVATE;
2009 avc->f.states |= CPageWrite;
2014 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2015 struct vrequest treq;
2018 if (!afs_InitReq(&treq, credp))
2019 code = afs_DoPartialWrite(avc, &treq);
2021 return afs_convert_code(code);
2025 afs_linux_complete_writeback(struct vcache *avc) {
2026 avc->f.states &= ~CPageWrite;
2029 /* Writeback a given page syncronously. Called with no AFS locks held */
2031 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2032 unsigned long offset, unsigned int count,
2035 struct vcache *vcp = VTOAFS(ip);
2043 buffer = kmap(pp) + offset;
2044 base = page_offset(pp) + offset;
2047 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2048 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2049 ICL_TYPE_INT32, 99999);
2051 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2053 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2055 i_size_write(ip, vcp->f.m.Length);
2056 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2058 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2060 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2061 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2062 ICL_TYPE_INT32, code);
2071 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2072 unsigned long offset, unsigned int count)
2076 struct vcache *vcp = VTOAFS(ip);
2079 /* Catch recursive writeback. This occurs if the kernel decides
2080 * writeback is required whilst we are writing to the cache, or
2081 * flushing to the server. When we're running syncronously (as
2082 * opposed to from writepage) we can't actually do anything about
2083 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2086 ObtainWriteLock(&vcp->lock, 532);
2087 afs_linux_prepare_writeback(vcp);
2088 ReleaseWriteLock(&vcp->lock);
2092 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2095 ObtainWriteLock(&vcp->lock, 533);
2097 code1 = afs_linux_dopartialwrite(vcp, credp);
2098 afs_linux_complete_writeback(vcp);
2099 ReleaseWriteLock(&vcp->lock);
2110 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2111 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2113 afs_linux_writepage(struct page *pp)
2116 struct address_space *mapping = pp->mapping;
2117 struct inode *inode;
2120 unsigned int to = PAGE_CACHE_SIZE;
2125 if (PageReclaim(pp)) {
2126 return AOP_WRITEPAGE_ACTIVATE;
2127 /* XXX - Do we need to redirty the page here? */
2132 inode = mapping->host;
2133 vcp = VTOAFS(inode);
2134 isize = i_size_read(inode);
2136 /* Don't defeat an earlier truncate */
2137 if (page_offset(pp) > isize) {
2138 set_page_writeback(pp);
2144 ObtainWriteLock(&vcp->lock, 537);
2145 code = afs_linux_prepare_writeback(vcp);
2146 if (code == AOP_WRITEPAGE_ACTIVATE) {
2147 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2148 * to return with the page still locked */
2149 ReleaseWriteLock(&vcp->lock);
2154 /* Grab the creds structure currently held in the vnode, and
2155 * get a reference to it, in case it goes away ... */
2161 ReleaseWriteLock(&vcp->lock);
2164 set_page_writeback(pp);
2166 SetPageUptodate(pp);
2168 /* We can unlock the page here, because it's protected by the
2169 * page_writeback flag. This should make us less vulnerable to
2170 * deadlocking in afs_write and afs_DoPartialWrite
2174 /* If this is the final page, then just write the number of bytes that
2175 * are actually in it */
2176 if ((isize - page_offset(pp)) < to )
2177 to = isize - page_offset(pp);
2179 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2182 ObtainWriteLock(&vcp->lock, 538);
2184 /* As much as we might like to ignore a file server error here,
2185 * and just try again when we close(), unfortunately StoreAllSegments
2186 * will invalidate our chunks if the server returns a permanent error,
2187 * so we need to at least try and get that error back to the user
2190 code1 = afs_linux_dopartialwrite(vcp, credp);
2192 afs_linux_complete_writeback(vcp);
2193 ReleaseWriteLock(&vcp->lock);
2198 end_page_writeback(pp);
2199 page_cache_release(pp);
2210 /* afs_linux_permission
2211 * Check access rights - returns error if can't check or permission denied.
2214 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2215 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2217 afs_linux_permission(struct inode *ip, int mode)
2221 cred_t *credp = crref();
2225 if (mode & MAY_EXEC)
2227 if (mode & MAY_READ)
2229 if (mode & MAY_WRITE)
2231 code = afs_access(VTOAFS(ip), tmp, credp);
2235 return afs_convert_code(code);
2239 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2243 struct inode *inode = FILE_INODE(file);
2244 loff_t pagebase = page_offset(page);
2246 if (i_size_read(inode) < (pagebase + offset))
2247 i_size_write(inode, pagebase + offset);
2249 if (PageChecked(page)) {
2250 SetPageUptodate(page);
2251 ClearPageChecked(page);
2254 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2260 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2264 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2265 * prepare_write. Essentially, if the page exists within the file,
2266 * and is not being fully written, then we should populate it.
2269 if (!PageUptodate(page)) {
2270 loff_t pagebase = page_offset(page);
2271 loff_t isize = i_size_read(page->mapping->host);
2273 /* Is the location we are writing to beyond the end of the file? */
2274 if (pagebase >= isize ||
2275 ((from == 0) && (pagebase + to) >= isize)) {
2276 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2277 SetPageChecked(page);
2278 /* Are we we writing a full page */
2279 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2280 SetPageChecked(page);
2281 /* Is the page readable, if it's wronly, we don't care, because we're
2282 * not actually going to read from it ... */
2283 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2284 /* We don't care if fillpage fails, because if it does the page
2285 * won't be marked as up to date
2287 afs_linux_fillpage(file, page);
2293 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2295 afs_linux_write_end(struct file *file, struct address_space *mapping,
2296 loff_t pos, unsigned len, unsigned copied,
2297 struct page *page, void *fsdata)
2300 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2302 code = afs_linux_commit_write(file, page, from, from + len);
2305 page_cache_release(page);
2310 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2311 loff_t pos, unsigned len, unsigned flags,
2312 struct page **pagep, void **fsdata)
2315 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2316 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2319 page = grab_cache_page_write_begin(mapping, index, flags);
2322 code = afs_linux_prepare_write(file, page, from, from + len);
2325 page_cache_release(page);
2333 static struct inode_operations afs_file_iops = {
2334 .permission = afs_linux_permission,
2335 .getattr = afs_linux_getattr,
2336 .setattr = afs_notify_change,
2339 static struct address_space_operations afs_file_aops = {
2340 .readpage = afs_linux_readpage,
2341 .readpages = afs_linux_readpages,
2342 .writepage = afs_linux_writepage,
2343 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2344 .write_begin = afs_linux_write_begin,
2345 .write_end = afs_linux_write_end,
2347 .commit_write = afs_linux_commit_write,
2348 .prepare_write = afs_linux_prepare_write,
2353 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2354 * by what sort of operation is allowed.....
2357 static struct inode_operations afs_dir_iops = {
2358 .setattr = afs_notify_change,
2359 .create = afs_linux_create,
2360 .lookup = afs_linux_lookup,
2361 .link = afs_linux_link,
2362 .unlink = afs_linux_unlink,
2363 .symlink = afs_linux_symlink,
2364 .mkdir = afs_linux_mkdir,
2365 .rmdir = afs_linux_rmdir,
2366 .rename = afs_linux_rename,
2367 .getattr = afs_linux_getattr,
2368 .permission = afs_linux_permission,
2371 /* We really need a separate symlink set of ops, since do_follow_link()
2372 * determines if it _is_ a link by checking if the follow_link op is set.
2374 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2376 afs_symlink_filler(struct file *file, struct page *page)
2378 struct inode *ip = (struct inode *)page->mapping->host;
2379 char *p = (char *)kmap(page);
2383 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2388 p[code] = '\0'; /* null terminate? */
2390 SetPageUptodate(page);
2402 static struct address_space_operations afs_symlink_aops = {
2403 .readpage = afs_symlink_filler
2405 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2407 static struct inode_operations afs_symlink_iops = {
2408 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2409 .readlink = page_readlink,
2410 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2411 .follow_link = page_follow_link,
2413 .follow_link = page_follow_link_light,
2414 .put_link = page_put_link,
2416 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2417 .readlink = afs_linux_readlink,
2418 .follow_link = afs_linux_follow_link,
2419 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2420 .setattr = afs_notify_change,
2424 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2428 vattr2inode(ip, vattr);
2430 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2431 /* Reset ops if symlink or directory. */
2432 if (S_ISREG(ip->i_mode)) {
2433 ip->i_op = &afs_file_iops;
2434 ip->i_fop = &afs_file_fops;
2435 ip->i_data.a_ops = &afs_file_aops;
2437 } else if (S_ISDIR(ip->i_mode)) {
2438 ip->i_op = &afs_dir_iops;
2439 ip->i_fop = &afs_dir_fops;
2441 } else if (S_ISLNK(ip->i_mode)) {
2442 ip->i_op = &afs_symlink_iops;
2443 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2444 ip->i_data.a_ops = &afs_symlink_aops;
2445 ip->i_mapping = &ip->i_data;