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 /* check_bad_parent() : Checks 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. If so, check_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.
712 * This dentry's vcache's mvid will be set to the correct parent directory's
714 * This root vnode's volume will have its dotdotfid and mtpoint fids set
715 * to the correct parent and mountpoint fids.
719 check_bad_parent(struct dentry *dp)
722 struct dentry *parent;
723 struct vcache *vcp, *pvc, *avc = NULL;
725 vcp = VTOAFS(dp->d_inode);
726 parent = dget_parent(dp);
727 pvc = VTOAFS(parent->d_inode);
729 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
732 /* force a lookup, so vcp->mvid is fixed up */
733 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
734 if (!avc || vcp != avc) { /* bad, very bad.. */
735 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
736 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
737 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
738 ICL_TYPE_POINTER, dp);
741 AFS_RELE(AFSTOV(avc));
750 /* afs_linux_revalidate
751 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
754 afs_linux_revalidate(struct dentry *dp)
757 struct vcache *vcp = VTOAFS(dp->d_inode);
761 if (afs_shuttingdown)
767 /* Make this a fast path (no crref), since it's called so often. */
768 if (vcp->f.states & CStatd) {
770 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
771 check_bad_parent(dp); /* check and correct mvid */
778 /* This avoids the crref when we don't have to do it. Watch for
779 * changes in afs_getattr that don't get replicated here!
781 if (vcp->f.states & CStatd &&
782 (!afs_fakestat_enable || vcp->mvstat != 1) &&
784 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
785 code = afs_CopyOutAttrs(vcp, &vattr);
788 code = afs_getattr(vcp, &vattr, credp);
792 afs_fill_inode(AFSTOV(vcp), &vattr);
796 return afs_convert_code(code);
800 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
802 int err = afs_linux_revalidate(dentry);
804 generic_fillattr(dentry->d_inode, stat);
809 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
810 * In kernels 2.2.10 and above, we are passed an additional flags var which
811 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
812 * we are advised to follow the entry if it is a link or to make sure that
813 * it is a directory. But since the kernel itself checks these possibilities
814 * later on, we shouldn't have to do it until later. Perhaps in the future..
817 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
818 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
820 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
824 cred_t *credp = NULL;
825 struct vcache *vcp, *pvcp, *tvc = NULL;
826 struct dentry *parent;
828 struct afs_fakestat_state fakestate;
831 afs_InitFakeStat(&fakestate);
834 vcp = VTOAFS(dp->d_inode);
836 if (vcp == afs_globalVp)
839 if (vcp->mvstat == 1) { /* mount point */
840 if (vcp->mvid && (vcp->f.states & CMValid)) {
843 struct vrequest treq;
846 code = afs_InitReq(&treq, credp);
848 (strcmp(dp->d_name.name, ".directory") == 0)) {
852 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
854 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
855 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
856 /* a mount point, not yet replaced by its directory */
861 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
862 check_bad_parent(dp); /* check and correct mvid */
865 /* If the last looker changes, we should make sure the current
866 * looker still has permission to examine this file. This would
867 * always require a crref() which would be "slow".
869 if (vcp->last_looker != treq.uid) {
870 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
873 vcp->last_looker = treq.uid;
877 parent = dget_parent(dp);
878 pvcp = VTOAFS(parent->d_inode);
880 /* If the parent's DataVersion has changed or the vnode
881 * is longer valid, we need to do a full lookup. VerifyVCache
882 * isn't enough since the vnode may have been renamed.
885 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
888 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
889 if (!tvc || tvc != vcp) {
894 if (afs_getattr(vcp, &vattr, credp)) {
899 vattr2inode(AFSTOV(vcp), &vattr);
900 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
903 /* should we always update the attributes at this point? */
904 /* unlikely--the vcache entry hasn't changed */
909 /* If this code is ever enabled, we should use dget_parent to handle
910 * getting the parent, and dput() to dispose of it. See above for an
912 pvcp = VTOAFS(dp->d_parent->d_inode);
913 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
917 /* No change in parent's DataVersion so this negative
918 * lookup is still valid. BUT, if a server is down a
919 * negative lookup can result so there should be a
920 * liftime as well. For now, always expire.
933 afs_PutFakeStat(&fakestate);
939 shrink_dcache_parent(dp);
945 if (have_submounts(dp))
953 afs_dentry_iput(struct dentry *dp, struct inode *ip)
955 struct vcache *vcp = VTOAFS(ip);
958 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
959 (void) afs_InactiveVCache(vcp, NULL);
962 afs_linux_clear_nfsfs_renamed(dp);
968 afs_dentry_delete(struct dentry *dp)
970 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
971 return 1; /* bad inode? */
976 struct dentry_operations afs_dentry_operations = {
977 .d_revalidate = afs_linux_dentry_revalidate,
978 .d_delete = afs_dentry_delete,
979 .d_iput = afs_dentry_iput,
982 /**********************************************************************
983 * AFS Linux inode operations
984 **********************************************************************/
988 * Merely need to set enough of vattr to get us through the create. Note
989 * that the higher level code (open_namei) will take care of any tuncation
990 * explicitly. Exclusive open is also taken care of in open_namei.
992 * name is in kernel space at this point.
995 #ifdef IOP_CREATE_TAKES_NAMEIDATA
996 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
997 struct nameidata *nd)
999 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1003 cred_t *credp = crref();
1004 const char *name = dp->d_name.name;
1009 vattr.va_mode = mode;
1010 vattr.va_type = mode & S_IFMT;
1013 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1017 struct inode *ip = AFSTOV(vcp);
1019 afs_getattr(vcp, &vattr, credp);
1020 afs_fill_inode(ip, &vattr);
1021 insert_inode_hash(ip);
1022 dp->d_op = &afs_dentry_operations;
1023 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1024 d_instantiate(dp, ip);
1029 return afs_convert_code(code);
1032 /* afs_linux_lookup */
1033 static struct dentry *
1034 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1035 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1036 struct nameidata *nd)
1038 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1041 cred_t *credp = crref();
1042 struct vcache *vcp = NULL;
1043 const char *comp = dp->d_name.name;
1044 struct inode *ip = NULL;
1045 struct dentry *newdp = NULL;
1049 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1055 afs_getattr(vcp, &vattr, credp);
1056 afs_fill_inode(ip, &vattr);
1057 if (hlist_unhashed(&ip->i_hash))
1058 insert_inode_hash(ip);
1060 dp->d_op = &afs_dentry_operations;
1061 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1064 if (ip && S_ISDIR(ip->i_mode)) {
1065 struct dentry *alias;
1067 /* Try to invalidate an existing alias in favor of our new one */
1068 alias = d_find_alias(ip);
1069 /* But not if it's disconnected; then we want d_splice_alias below */
1070 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1071 if (d_invalidate(alias) == 0) {
1080 newdp = d_splice_alias(ip, dp);
1084 /* It's ok for the file to not be found. That's noted by the caller by
1085 * seeing that the dp->d_inode field is NULL.
1087 if (!code || code == ENOENT)
1090 return ERR_PTR(afs_convert_code(code));
1094 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1097 cred_t *credp = crref();
1098 const char *name = newdp->d_name.name;
1099 struct inode *oldip = olddp->d_inode;
1101 /* If afs_link returned the vnode, we could instantiate the
1102 * dentry. Since it's not, we drop this one and do a new lookup.
1107 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1111 return afs_convert_code(code);
1114 /* We have to have a Linux specific sillyrename function, because we
1115 * also have to keep the dcache up to date when we're doing a silly
1116 * rename - so we don't want the generic vnodeops doing this behind our
1121 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1124 struct vcache *tvc = VTOAFS(dentry->d_inode);
1125 struct dentry *__dp = NULL;
1126 char *__name = NULL;
1129 if (afs_linux_nfsfs_renamed(dentry))
1137 osi_FreeSmallSpace(__name);
1138 __name = afs_newname();
1141 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1144 osi_FreeSmallSpace(__name);
1147 } while (__dp->d_inode != NULL);
1150 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1151 VTOAFS(dir), (char *)__dp->d_name.name,
1154 tvc->mvid = (void *) __name;
1157 crfree(tvc->uncred);
1159 tvc->uncred = credp;
1160 tvc->f.states |= CUnlinked;
1161 afs_linux_set_nfsfs_renamed(dentry);
1163 osi_FreeSmallSpace(__name);
1168 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1169 d_move(dentry, __dp);
1178 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1181 cred_t *credp = crref();
1182 const char *name = dp->d_name.name;
1183 struct vcache *tvc = VTOAFS(dp->d_inode);
1185 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1186 && !(tvc->f.states & CUnlinked)) {
1188 code = afs_linux_sillyrename(dip, dp, credp);
1191 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1198 return afs_convert_code(code);
1203 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1206 cred_t *credp = crref();
1208 const char *name = dp->d_name.name;
1210 /* If afs_symlink returned the vnode, we could instantiate the
1211 * dentry. Since it's not, we drop this one and do a new lookup.
1217 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1220 return afs_convert_code(code);
1224 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1227 cred_t *credp = crref();
1228 struct vcache *tvcp = NULL;
1230 const char *name = dp->d_name.name;
1233 vattr.va_mask = ATTR_MODE;
1234 vattr.va_mode = mode;
1236 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1239 struct inode *ip = AFSTOV(tvcp);
1241 afs_getattr(tvcp, &vattr, credp);
1242 afs_fill_inode(ip, &vattr);
1244 dp->d_op = &afs_dentry_operations;
1245 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1246 d_instantiate(dp, ip);
1251 return afs_convert_code(code);
1255 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1258 cred_t *credp = crref();
1259 const char *name = dp->d_name.name;
1261 /* locking kernel conflicts with glock? */
1264 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1267 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1268 * that failed because a directory is not empty. So, we map
1269 * EEXIST to ENOTEMPTY on linux.
1271 if (code == EEXIST) {
1280 return afs_convert_code(code);
1285 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1286 struct inode *newip, struct dentry *newdp)
1289 cred_t *credp = crref();
1290 const char *oldname = olddp->d_name.name;
1291 const char *newname = newdp->d_name.name;
1292 struct dentry *rehash = NULL;
1294 /* Prevent any new references during rename operation. */
1296 if (!d_unhashed(newdp)) {
1301 if (atomic_read(&olddp->d_count) > 1)
1302 shrink_dcache_parent(olddp);
1305 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1309 olddp->d_time = 0; /* force to revalidate */
1315 return afs_convert_code(code);
1319 /* afs_linux_ireadlink
1320 * Internal readlink which can return link contents to user or kernel space.
1321 * Note that the buffer is NOT supposed to be null-terminated.
1324 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1327 cred_t *credp = crref();
1331 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1332 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1336 return maxlen - tuio.uio_resid;
1338 return afs_convert_code(code);
1341 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1342 /* afs_linux_readlink
1343 * Fill target (which is in user space) with contents of symlink.
1346 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1349 struct inode *ip = dp->d_inode;
1352 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1358 /* afs_linux_follow_link
1359 * a file system dependent link following routine.
1361 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1366 name = osi_Alloc(PATH_MAX);
1372 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1380 code = vfs_follow_link(nd, name);
1383 osi_Free(name, PATH_MAX);
1388 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1390 #if defined(AFS_CACHE_BYPASS)
1391 #endif /* defined(AFS_CACHE_BYPASS */
1393 /* Populate a page by filling it from the cache file pointed at by cachefp
1394 * (which contains indicated chunk)
1395 * If task is NULL, the page copy occurs syncronously, and the routine
1396 * returns with page still locked. If task is non-NULL, then page copies
1397 * may occur in the background, and the page will be unlocked when it is
1401 afs_linux_read_cache(struct file *cachefp, struct page *page,
1402 int chunk, struct pagevec *lrupv,
1403 struct afs_pagecopy_task *task) {
1404 loff_t offset = page_offset(page);
1405 struct page *newpage, *cachepage;
1406 struct address_space *cachemapping;
1410 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1414 /* From our offset, we now need to work out which page in the disk
1415 * file it corresponds to. This will be fun ... */
1416 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1418 while (cachepage == NULL) {
1419 cachepage = find_get_page(cachemapping, pageindex);
1422 newpage = page_cache_alloc_cold(cachemapping);
1428 code = add_to_page_cache(newpage, cachemapping,
1429 pageindex, GFP_KERNEL);
1431 cachepage = newpage;
1434 page_cache_get(cachepage);
1435 if (!pagevec_add(lrupv, cachepage))
1436 __pagevec_lru_add_file(lrupv);
1439 page_cache_release(newpage);
1441 if (code != -EEXIST)
1445 lock_page(cachepage);
1449 if (!PageUptodate(cachepage)) {
1450 ClearPageError(cachepage);
1451 code = cachemapping->a_ops->readpage(NULL, cachepage);
1452 if (!code && !task) {
1453 wait_on_page_locked(cachepage);
1456 unlock_page(cachepage);
1460 if (PageUptodate(cachepage)) {
1461 copy_highpage(page, cachepage);
1462 flush_dcache_page(page);
1463 SetPageUptodate(page);
1468 afs_pagecopy_queue_page(task, cachepage, page);
1480 page_cache_release(cachepage);
1486 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1488 loff_t offset = page_offset(pp);
1489 struct inode *ip = FILE_INODE(fp);
1490 struct vcache *avc = VTOAFS(ip);
1492 struct file *cacheFp = NULL;
1495 struct pagevec lrupv;
1497 /* Not a UFS cache, don't do anything */
1498 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1501 /* Can't do anything if the vcache isn't statd , or if the read
1502 * crosses a chunk boundary.
1504 if (!(avc->f.states & CStatd) ||
1505 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1509 ObtainWriteLock(&avc->lock, 911);
1511 /* XXX - See if hinting actually makes things faster !!! */
1513 /* See if we have a suitable entry already cached */
1517 /* We need to lock xdcache, then dcache, to handle situations where
1518 * the hint is on the free list. However, we can't safely do this
1519 * according to the locking hierarchy. So, use a non blocking lock.
1521 ObtainReadLock(&afs_xdcache);
1522 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1524 if (dcLocked && (tdc->index != NULLIDX)
1525 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1526 && tdc->f.chunk == AFS_CHUNK(offset)
1527 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1528 /* Bonus - the hint was correct */
1531 /* Only destroy the hint if its actually invalid, not if there's
1532 * just been a locking failure */
1534 ReleaseReadLock(&tdc->lock);
1541 ReleaseReadLock(&afs_xdcache);
1544 /* No hint, or hint is no longer valid - see if we can get something
1545 * directly from the dcache
1548 tdc = afs_FindDCache(avc, offset);
1551 ReleaseWriteLock(&avc->lock);
1556 ObtainReadLock(&tdc->lock);
1558 /* Is the dcache we've been given currently up to date */
1559 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1560 (tdc->dflags & DFFetching)) {
1561 ReleaseWriteLock(&avc->lock);
1562 ReleaseReadLock(&tdc->lock);
1567 /* Update our hint for future abuse */
1570 /* Okay, so we've now got a cache file that is up to date */
1572 /* XXX - I suspect we should be locking the inodes before we use them! */
1574 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1575 pagevec_init(&lrupv, 0);
1577 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1579 if (pagevec_count(&lrupv))
1580 __pagevec_lru_add_file(&lrupv);
1582 filp_close(cacheFp, NULL);
1585 ReleaseReadLock(&tdc->lock);
1586 ReleaseWriteLock(&avc->lock);
1593 /* afs_linux_readpage
1595 * This function is split into two, because prepare_write/begin_write
1596 * require a readpage call which doesn't unlock the resulting page upon
1600 afs_linux_fillpage(struct file *fp, struct page *pp)
1605 struct iovec *iovecp;
1606 struct inode *ip = FILE_INODE(fp);
1607 afs_int32 cnt = page_count(pp);
1608 struct vcache *avc = VTOAFS(ip);
1609 afs_offs_t offset = page_offset(pp);
1613 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1623 auio = osi_Alloc(sizeof(uio_t));
1624 iovecp = osi_Alloc(sizeof(struct iovec));
1626 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1631 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1632 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1633 99999); /* not a possible code value */
1635 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1637 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1638 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1640 AFS_DISCON_UNLOCK();
1643 /* XXX valid for no-cache also? Check last bits of files... :)
1644 * Cognate code goes in afs_NoCacheFetchProc. */
1645 if (auio->uio_resid) /* zero remainder of page */
1646 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1649 flush_dcache_page(pp);
1650 SetPageUptodate(pp);
1655 osi_Free(auio, sizeof(uio_t));
1656 osi_Free(iovecp, sizeof(struct iovec));
1659 return afs_convert_code(code);
1663 afs_linux_prefetch(struct file *fp, struct page *pp)
1666 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1667 afs_offs_t offset = page_offset(pp);
1669 if (AFS_CHUNKOFFSET(offset) == 0) {
1671 struct vrequest treq;
1676 code = afs_InitReq(&treq, credp);
1677 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1678 tdc = afs_FindDCache(avc, offset);
1680 if (!(tdc->mflags & DFNextStarted))
1681 afs_PrefetchChunk(avc, tdc, credp, &treq);
1684 ReleaseWriteLock(&avc->lock);
1689 return afs_convert_code(code);
1693 #if defined(AFS_CACHE_BYPASS)
1696 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1697 struct list_head *page_list, unsigned num_pages)
1702 struct iovec* iovecp;
1703 struct nocache_read_request *ancr;
1705 struct pagevec lrupv;
1709 struct inode *ip = FILE_INODE(fp);
1710 struct vcache *avc = VTOAFS(ip);
1711 afs_int32 base_index = 0;
1712 afs_int32 page_count = 0;
1715 /* background thread must free: iovecp, auio, ancr */
1716 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1718 auio = osi_Alloc(sizeof(uio_t));
1719 auio->uio_iov = iovecp;
1720 auio->uio_iovcnt = num_pages;
1721 auio->uio_flag = UIO_READ;
1722 auio->uio_seg = AFS_UIOSYS;
1723 auio->uio_resid = num_pages * PAGE_SIZE;
1725 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1727 ancr->offset = auio->uio_offset;
1728 ancr->length = auio->uio_resid;
1730 pagevec_init(&lrupv, 0);
1732 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1734 if(list_empty(page_list))
1737 pp = list_entry(page_list->prev, struct page, lru);
1738 /* If we allocate a page and don't remove it from page_list,
1739 * the page cache gets upset. */
1741 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1742 if(pp->index > isize) {
1749 offset = page_offset(pp);
1750 auio->uio_offset = offset;
1751 base_index = pp->index;
1753 iovecp[page_ix].iov_len = PAGE_SIZE;
1754 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1755 if(base_index != pp->index) {
1758 page_cache_release(pp);
1759 iovecp[page_ix].iov_base = (void *) 0;
1761 ancr->length -= PAGE_SIZE;
1768 page_cache_release(pp);
1769 iovecp[page_ix].iov_base = (void *) 0;
1772 if(!PageLocked(pp)) {
1776 /* increment page refcount--our original design assumed
1777 * that locking it would effectively pin it; protect
1778 * ourselves from the possiblity that this assumption is
1779 * is faulty, at low cost (provided we do not fail to
1780 * do the corresponding decref on the other side) */
1783 /* save the page for background map */
1784 iovecp[page_ix].iov_base = (void*) pp;
1786 /* and put it on the LRU cache */
1787 if (!pagevec_add(&lrupv, pp))
1788 __pagevec_lru_add_file(&lrupv);
1792 /* If there were useful pages in the page list, make sure all pages
1793 * are in the LRU cache, then schedule the read */
1795 if (pagevec_count(&lrupv))
1796 __pagevec_lru_add_file(&lrupv);
1798 code = afs_ReadNoCache(avc, ancr, credp);
1801 /* If there is nothing for the background thread to handle,
1802 * it won't be freeing the things that we never gave it */
1803 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1804 osi_Free(auio, sizeof(uio_t));
1805 osi_Free(ancr, sizeof(struct nocache_read_request));
1807 /* we do not flush, release, or unmap pages--that will be
1808 * done for us by the background thread as each page comes in
1809 * from the fileserver */
1810 return afs_convert_code(code);
1815 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1817 cred_t *credp = NULL;
1819 struct iovec *iovecp;
1820 struct nocache_read_request *ancr;
1824 * Special case: if page is at or past end of file, just zero it and set
1827 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1828 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1829 SetPageUptodate(pp);
1836 /* receiver frees */
1837 auio = osi_Alloc(sizeof(uio_t));
1838 iovecp = osi_Alloc(sizeof(struct iovec));
1840 /* address can be NULL, because we overwrite it with 'pp', below */
1841 setup_uio(auio, iovecp, NULL, page_offset(pp),
1842 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1844 /* save the page for background map */
1845 get_page(pp); /* see above */
1846 auio->uio_iov->iov_base = (void*) pp;
1847 /* the background thread will free this */
1848 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1850 ancr->offset = page_offset(pp);
1851 ancr->length = PAGE_SIZE;
1854 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1857 return afs_convert_code(code);
1861 afs_linux_can_bypass(struct inode *ip) {
1862 switch(cache_bypass_strategy) {
1863 case NEVER_BYPASS_CACHE:
1865 case ALWAYS_BYPASS_CACHE:
1867 case LARGE_FILES_BYPASS_CACHE:
1868 if(i_size_read(ip) > cache_bypass_threshold)
1875 /* Check if a file is permitted to bypass the cache by policy, and modify
1876 * the cache bypass state recorded for that file */
1879 afs_linux_bypass_check(struct inode *ip) {
1882 int bypass = afs_linux_can_bypass(ip);
1885 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1893 afs_linux_bypass_check(struct inode *ip) {
1897 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1901 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1902 struct list_head *page_list, unsigned int num_pages) {
1908 afs_linux_readpage(struct file *fp, struct page *pp)
1912 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1913 code = afs_linux_bypass_readpage(fp, pp);
1915 code = afs_linux_fillpage(fp, pp);
1917 code = afs_linux_prefetch(fp, pp);
1924 /* Readpages reads a number of pages for a particular file. We use
1925 * this to optimise the reading, by limiting the number of times upon which
1926 * we have to lookup, lock and open vcaches and dcaches
1930 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1931 struct list_head *page_list, unsigned int num_pages)
1933 struct inode *inode = mapping->host;
1934 struct vcache *avc = VTOAFS(inode);
1936 struct file *cacheFp = NULL;
1938 unsigned int page_idx;
1940 struct pagevec lrupv;
1941 struct afs_pagecopy_task *task;
1943 if (afs_linux_bypass_check(inode))
1944 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1946 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1950 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1955 ObtainWriteLock(&avc->lock, 912);
1958 task = afs_pagecopy_init_task();
1961 pagevec_init(&lrupv, 0);
1962 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1963 struct page *page = list_entry(page_list->prev, struct page, lru);
1964 list_del(&page->lru);
1965 offset = page_offset(page);
1967 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1969 ReleaseReadLock(&tdc->lock);
1974 filp_close(cacheFp, NULL);
1979 if ((tdc = afs_FindDCache(avc, offset))) {
1980 ObtainReadLock(&tdc->lock);
1981 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1982 (tdc->dflags & DFFetching)) {
1983 ReleaseReadLock(&tdc->lock);
1990 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1993 if (tdc && !add_to_page_cache(page, mapping, page->index,
1995 page_cache_get(page);
1996 if (!pagevec_add(&lrupv, page))
1997 __pagevec_lru_add_file(&lrupv);
1999 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2001 page_cache_release(page);
2003 if (pagevec_count(&lrupv))
2004 __pagevec_lru_add_file(&lrupv);
2007 filp_close(cacheFp, NULL);
2009 afs_pagecopy_put_task(task);
2013 ReleaseReadLock(&tdc->lock);
2017 ReleaseWriteLock(&avc->lock);
2022 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2025 afs_linux_prepare_writeback(struct vcache *avc) {
2026 if (avc->f.states & CPageWrite) {
2027 return AOP_WRITEPAGE_ACTIVATE;
2029 avc->f.states |= CPageWrite;
2034 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2035 struct vrequest treq;
2038 if (!afs_InitReq(&treq, credp))
2039 code = afs_DoPartialWrite(avc, &treq);
2041 return afs_convert_code(code);
2045 afs_linux_complete_writeback(struct vcache *avc) {
2046 avc->f.states &= ~CPageWrite;
2049 /* Writeback a given page syncronously. Called with no AFS locks held */
2051 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2052 unsigned long offset, unsigned int count,
2055 struct vcache *vcp = VTOAFS(ip);
2063 buffer = kmap(pp) + offset;
2064 base = page_offset(pp) + offset;
2067 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2068 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2069 ICL_TYPE_INT32, 99999);
2071 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2073 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2075 i_size_write(ip, vcp->f.m.Length);
2076 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2078 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
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, code);
2091 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2092 unsigned long offset, unsigned int count)
2096 struct vcache *vcp = VTOAFS(ip);
2099 /* Catch recursive writeback. This occurs if the kernel decides
2100 * writeback is required whilst we are writing to the cache, or
2101 * flushing to the server. When we're running syncronously (as
2102 * opposed to from writepage) we can't actually do anything about
2103 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2106 ObtainWriteLock(&vcp->lock, 532);
2107 afs_linux_prepare_writeback(vcp);
2108 ReleaseWriteLock(&vcp->lock);
2112 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2115 ObtainWriteLock(&vcp->lock, 533);
2117 code1 = afs_linux_dopartialwrite(vcp, credp);
2118 afs_linux_complete_writeback(vcp);
2119 ReleaseWriteLock(&vcp->lock);
2130 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2131 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2133 afs_linux_writepage(struct page *pp)
2136 struct address_space *mapping = pp->mapping;
2137 struct inode *inode;
2140 unsigned int to = PAGE_CACHE_SIZE;
2145 if (PageReclaim(pp)) {
2146 return AOP_WRITEPAGE_ACTIVATE;
2147 /* XXX - Do we need to redirty the page here? */
2152 inode = mapping->host;
2153 vcp = VTOAFS(inode);
2154 isize = i_size_read(inode);
2156 /* Don't defeat an earlier truncate */
2157 if (page_offset(pp) > isize) {
2158 set_page_writeback(pp);
2164 ObtainWriteLock(&vcp->lock, 537);
2165 code = afs_linux_prepare_writeback(vcp);
2166 if (code == AOP_WRITEPAGE_ACTIVATE) {
2167 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2168 * to return with the page still locked */
2169 ReleaseWriteLock(&vcp->lock);
2174 /* Grab the creds structure currently held in the vnode, and
2175 * get a reference to it, in case it goes away ... */
2181 ReleaseWriteLock(&vcp->lock);
2184 set_page_writeback(pp);
2186 SetPageUptodate(pp);
2188 /* We can unlock the page here, because it's protected by the
2189 * page_writeback flag. This should make us less vulnerable to
2190 * deadlocking in afs_write and afs_DoPartialWrite
2194 /* If this is the final page, then just write the number of bytes that
2195 * are actually in it */
2196 if ((isize - page_offset(pp)) < to )
2197 to = isize - page_offset(pp);
2199 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2202 ObtainWriteLock(&vcp->lock, 538);
2204 /* As much as we might like to ignore a file server error here,
2205 * and just try again when we close(), unfortunately StoreAllSegments
2206 * will invalidate our chunks if the server returns a permanent error,
2207 * so we need to at least try and get that error back to the user
2210 code1 = afs_linux_dopartialwrite(vcp, credp);
2212 afs_linux_complete_writeback(vcp);
2213 ReleaseWriteLock(&vcp->lock);
2218 end_page_writeback(pp);
2219 page_cache_release(pp);
2230 /* afs_linux_permission
2231 * Check access rights - returns error if can't check or permission denied.
2234 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2235 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2237 afs_linux_permission(struct inode *ip, int mode)
2241 cred_t *credp = crref();
2245 if (mode & MAY_EXEC)
2247 if (mode & MAY_READ)
2249 if (mode & MAY_WRITE)
2251 code = afs_access(VTOAFS(ip), tmp, credp);
2255 return afs_convert_code(code);
2259 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2263 struct inode *inode = FILE_INODE(file);
2264 loff_t pagebase = page_offset(page);
2266 if (i_size_read(inode) < (pagebase + offset))
2267 i_size_write(inode, pagebase + offset);
2269 if (PageChecked(page)) {
2270 SetPageUptodate(page);
2271 ClearPageChecked(page);
2274 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2280 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2284 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2285 * prepare_write. Essentially, if the page exists within the file,
2286 * and is not being fully written, then we should populate it.
2289 if (!PageUptodate(page)) {
2290 loff_t pagebase = page_offset(page);
2291 loff_t isize = i_size_read(page->mapping->host);
2293 /* Is the location we are writing to beyond the end of the file? */
2294 if (pagebase >= isize ||
2295 ((from == 0) && (pagebase + to) >= isize)) {
2296 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2297 SetPageChecked(page);
2298 /* Are we we writing a full page */
2299 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2300 SetPageChecked(page);
2301 /* Is the page readable, if it's wronly, we don't care, because we're
2302 * not actually going to read from it ... */
2303 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2304 /* We don't care if fillpage fails, because if it does the page
2305 * won't be marked as up to date
2307 afs_linux_fillpage(file, page);
2313 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2315 afs_linux_write_end(struct file *file, struct address_space *mapping,
2316 loff_t pos, unsigned len, unsigned copied,
2317 struct page *page, void *fsdata)
2320 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2322 code = afs_linux_commit_write(file, page, from, from + len);
2325 page_cache_release(page);
2330 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2331 loff_t pos, unsigned len, unsigned flags,
2332 struct page **pagep, void **fsdata)
2335 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2336 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2339 page = grab_cache_page_write_begin(mapping, index, flags);
2342 code = afs_linux_prepare_write(file, page, from, from + len);
2345 page_cache_release(page);
2353 static struct inode_operations afs_file_iops = {
2354 .permission = afs_linux_permission,
2355 .getattr = afs_linux_getattr,
2356 .setattr = afs_notify_change,
2359 static struct address_space_operations afs_file_aops = {
2360 .readpage = afs_linux_readpage,
2361 .readpages = afs_linux_readpages,
2362 .writepage = afs_linux_writepage,
2363 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2364 .write_begin = afs_linux_write_begin,
2365 .write_end = afs_linux_write_end,
2367 .commit_write = afs_linux_commit_write,
2368 .prepare_write = afs_linux_prepare_write,
2373 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2374 * by what sort of operation is allowed.....
2377 static struct inode_operations afs_dir_iops = {
2378 .setattr = afs_notify_change,
2379 .create = afs_linux_create,
2380 .lookup = afs_linux_lookup,
2381 .link = afs_linux_link,
2382 .unlink = afs_linux_unlink,
2383 .symlink = afs_linux_symlink,
2384 .mkdir = afs_linux_mkdir,
2385 .rmdir = afs_linux_rmdir,
2386 .rename = afs_linux_rename,
2387 .getattr = afs_linux_getattr,
2388 .permission = afs_linux_permission,
2391 /* We really need a separate symlink set of ops, since do_follow_link()
2392 * determines if it _is_ a link by checking if the follow_link op is set.
2394 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2396 afs_symlink_filler(struct file *file, struct page *page)
2398 struct inode *ip = (struct inode *)page->mapping->host;
2399 char *p = (char *)kmap(page);
2403 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2408 p[code] = '\0'; /* null terminate? */
2410 SetPageUptodate(page);
2422 static struct address_space_operations afs_symlink_aops = {
2423 .readpage = afs_symlink_filler
2425 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2427 static struct inode_operations afs_symlink_iops = {
2428 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2429 .readlink = page_readlink,
2430 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2431 .follow_link = page_follow_link,
2433 .follow_link = page_follow_link_light,
2434 .put_link = page_put_link,
2436 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2437 .readlink = afs_linux_readlink,
2438 .follow_link = afs_linux_follow_link,
2439 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2440 .setattr = afs_notify_change,
2444 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2448 vattr2inode(ip, vattr);
2450 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2451 /* Reset ops if symlink or directory. */
2452 if (S_ISREG(ip->i_mode)) {
2453 ip->i_op = &afs_file_iops;
2454 ip->i_fop = &afs_file_fops;
2455 ip->i_data.a_ops = &afs_file_aops;
2457 } else if (S_ISDIR(ip->i_mode)) {
2458 ip->i_op = &afs_dir_iops;
2459 ip->i_fop = &afs_dir_fops;
2461 } else if (S_ISLNK(ip->i_mode)) {
2462 ip->i_op = &afs_symlink_iops;
2463 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2464 ip->i_data.a_ops = &afs_symlink_aops;
2465 ip->i_mapping = &ip->i_data;