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,
662 .llseek = default_llseek,
665 struct file_operations afs_file_fops = {
666 .read = afs_linux_read,
667 .write = afs_linux_write,
668 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
669 .aio_read = generic_file_aio_read,
670 .aio_write = generic_file_aio_write,
672 #ifdef HAVE_UNLOCKED_IOCTL
673 .unlocked_ioctl = afs_unlocked_xioctl,
677 #ifdef HAVE_COMPAT_IOCTL
678 .compat_ioctl = afs_unlocked_xioctl,
680 .mmap = afs_linux_mmap,
681 .open = afs_linux_open,
682 .flush = afs_linux_flush,
683 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
684 .sendfile = generic_file_sendfile,
686 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
687 .splice_write = generic_file_splice_write,
688 .splice_read = generic_file_splice_read,
690 .release = afs_linux_release,
691 .fsync = afs_linux_fsync,
692 .lock = afs_linux_lock,
693 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
694 .flock = afs_linux_flock,
696 .llseek = default_llseek,
700 /**********************************************************************
701 * AFS Linux dentry operations
702 **********************************************************************/
704 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
705 * that has its mvid (parent dir's fid) pointer set to the wrong directory
706 * due to being mounted in multiple points at once. fix_bad_parent()
707 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
708 * dotdotfid and mtpoint fid members.
710 * dp - dentry to be checked.
711 * credp - credentials
712 * vcp, pvc - item's and parent's vcache pointer
716 * This dentry's vcache's mvid will be set to the correct parent directory's
718 * This root vnode's volume will have its dotdotfid and mtpoint fids set
719 * to the correct parent and mountpoint fids.
723 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
725 struct vcache *avc = NULL;
727 /* force a lookup, so vcp->mvid is fixed up */
728 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
729 if (!avc || vcp != avc) { /* bad, very bad.. */
730 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
731 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
732 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
733 ICL_TYPE_POINTER, dp);
736 AFS_RELE(AFSTOV(avc));
741 /* afs_linux_revalidate
742 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
745 afs_linux_revalidate(struct dentry *dp)
748 struct vcache *vcp = VTOAFS(dp->d_inode);
752 if (afs_shuttingdown)
758 /* Make this a fast path (no crref), since it's called so often. */
759 if (vcp->states & CStatd) {
760 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
762 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
763 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
766 fix_bad_parent(dp); /* check and correct mvid */
775 /* This avoids the crref when we don't have to do it. Watch for
776 * changes in afs_getattr that don't get replicated here!
778 if (vcp->f.states & CStatd &&
779 (!afs_fakestat_enable || vcp->mvstat != 1) &&
781 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
782 code = afs_CopyOutAttrs(vcp, &vattr);
785 code = afs_getattr(vcp, &vattr, credp);
790 afs_fill_inode(AFSTOV(vcp), &vattr);
794 return afs_convert_code(code);
798 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
800 int err = afs_linux_revalidate(dentry);
802 generic_fillattr(dentry->d_inode, stat);
807 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
808 * In kernels 2.2.10 and above, we are passed an additional flags var which
809 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
810 * we are advised to follow the entry if it is a link or to make sure that
811 * it is a directory. But since the kernel itself checks these possibilities
812 * later on, we shouldn't have to do it until later. Perhaps in the future..
814 * The code here assumes that on entry the global lock is not held
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 parent = dget_parent(dp);
835 pvcp = VTOAFS(parent->d_inode);
836 vcp = VTOAFS(dp->d_inode);
838 if (vcp == afs_globalVp)
841 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
847 if (locked && vcp->mvstat == 1) { /* mount point */
848 if (vcp->mvid && (vcp->f.states & CMValid)) {
851 struct vrequest treq;
853 code = afs_InitReq(&treq, credp);
855 (strcmp(dp->d_name.name, ".directory") == 0)) {
859 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
861 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
862 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
863 /* a mount point, not yet replaced by its directory */
868 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
869 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
870 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
875 /* If the last looker changes, we should make sure the current
876 * looker still has permission to examine this file. This would
877 * always require a crref() which would be "slow".
879 if (vcp->last_looker != treq.uid) {
880 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
883 vcp->last_looker = treq.uid;
888 /* If the parent's DataVersion has changed or the vnode
889 * is longer valid, we need to do a full lookup. VerifyVCache
890 * isn't enough since the vnode may have been renamed.
893 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
899 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
900 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
901 if (!tvc || tvc != vcp) {
906 if (afs_getattr(vcp, &vattr, credp)) {
911 vattr2inode(AFSTOV(vcp), &vattr);
912 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
915 /* should we always update the attributes at this point? */
916 /* unlikely--the vcache entry hasn't changed */
921 /* If this code is ever enabled, we should use dget_parent to handle
922 * getting the parent, and dput() to dispose of it. See above for an
924 pvcp = VTOAFS(dp->d_parent->d_inode);
925 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
929 /* No change in parent's DataVersion so this negative
930 * lookup is still valid. BUT, if a server is down a
931 * negative lookup can result so there should be a
932 * liftime as well. For now, always expire.
945 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
947 /* we hold the global lock if we evaluated a mount point */
954 shrink_dcache_parent(dp);
960 if (have_submounts(dp))
968 afs_dentry_iput(struct dentry *dp, struct inode *ip)
970 struct vcache *vcp = VTOAFS(ip);
973 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
974 (void) afs_InactiveVCache(vcp, NULL);
977 afs_linux_clear_nfsfs_renamed(dp);
983 afs_dentry_delete(struct dentry *dp)
985 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
986 return 1; /* bad inode? */
991 struct dentry_operations afs_dentry_operations = {
992 .d_revalidate = afs_linux_dentry_revalidate,
993 .d_delete = afs_dentry_delete,
994 .d_iput = afs_dentry_iput,
997 /**********************************************************************
998 * AFS Linux inode operations
999 **********************************************************************/
1003 * Merely need to set enough of vattr to get us through the create. Note
1004 * that the higher level code (open_namei) will take care of any tuncation
1005 * explicitly. Exclusive open is also taken care of in open_namei.
1007 * name is in kernel space at this point.
1010 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1011 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1012 struct nameidata *nd)
1014 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1018 cred_t *credp = crref();
1019 const char *name = dp->d_name.name;
1024 vattr.va_mode = mode;
1025 vattr.va_type = mode & S_IFMT;
1028 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1032 struct inode *ip = AFSTOV(vcp);
1034 afs_getattr(vcp, &vattr, credp);
1035 afs_fill_inode(ip, &vattr);
1036 insert_inode_hash(ip);
1037 dp->d_op = &afs_dentry_operations;
1038 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1039 d_instantiate(dp, ip);
1044 return afs_convert_code(code);
1047 /* afs_linux_lookup */
1048 static struct dentry *
1049 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1050 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1051 struct nameidata *nd)
1053 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1056 cred_t *credp = crref();
1057 struct vcache *vcp = NULL;
1058 const char *comp = dp->d_name.name;
1059 struct inode *ip = NULL;
1060 struct dentry *newdp = NULL;
1064 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1070 afs_getattr(vcp, &vattr, credp);
1071 afs_fill_inode(ip, &vattr);
1072 if (hlist_unhashed(&ip->i_hash))
1073 insert_inode_hash(ip);
1075 dp->d_op = &afs_dentry_operations;
1076 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1079 if (ip && S_ISDIR(ip->i_mode)) {
1080 struct dentry *alias;
1082 /* Try to invalidate an existing alias in favor of our new one */
1083 alias = d_find_alias(ip);
1084 /* But not if it's disconnected; then we want d_splice_alias below */
1085 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1086 if (d_invalidate(alias) == 0) {
1095 newdp = d_splice_alias(ip, dp);
1099 /* It's ok for the file to not be found. That's noted by the caller by
1100 * seeing that the dp->d_inode field is NULL.
1102 if (!code || code == ENOENT)
1105 return ERR_PTR(afs_convert_code(code));
1109 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1112 cred_t *credp = crref();
1113 const char *name = newdp->d_name.name;
1114 struct inode *oldip = olddp->d_inode;
1116 /* If afs_link returned the vnode, we could instantiate the
1117 * dentry. Since it's not, we drop this one and do a new lookup.
1122 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1126 return afs_convert_code(code);
1129 /* We have to have a Linux specific sillyrename function, because we
1130 * also have to keep the dcache up to date when we're doing a silly
1131 * rename - so we don't want the generic vnodeops doing this behind our
1136 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1139 struct vcache *tvc = VTOAFS(dentry->d_inode);
1140 struct dentry *__dp = NULL;
1141 char *__name = NULL;
1144 if (afs_linux_nfsfs_renamed(dentry))
1152 osi_FreeSmallSpace(__name);
1153 __name = afs_newname();
1156 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1159 osi_FreeSmallSpace(__name);
1162 } while (__dp->d_inode != NULL);
1165 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1166 VTOAFS(dir), (char *)__dp->d_name.name,
1169 tvc->mvid = (void *) __name;
1172 crfree(tvc->uncred);
1174 tvc->uncred = credp;
1175 tvc->f.states |= CUnlinked;
1176 afs_linux_set_nfsfs_renamed(dentry);
1178 osi_FreeSmallSpace(__name);
1183 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1184 d_move(dentry, __dp);
1193 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1196 cred_t *credp = crref();
1197 const char *name = dp->d_name.name;
1198 struct vcache *tvc = VTOAFS(dp->d_inode);
1200 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1201 && !(tvc->f.states & CUnlinked)) {
1203 code = afs_linux_sillyrename(dip, dp, credp);
1206 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1213 return afs_convert_code(code);
1218 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1221 cred_t *credp = crref();
1223 const char *name = dp->d_name.name;
1225 /* If afs_symlink returned the vnode, we could instantiate the
1226 * dentry. Since it's not, we drop this one and do a new lookup.
1232 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1235 return afs_convert_code(code);
1239 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1242 cred_t *credp = crref();
1243 struct vcache *tvcp = NULL;
1245 const char *name = dp->d_name.name;
1248 vattr.va_mask = ATTR_MODE;
1249 vattr.va_mode = mode;
1251 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1254 struct inode *ip = AFSTOV(tvcp);
1256 afs_getattr(tvcp, &vattr, credp);
1257 afs_fill_inode(ip, &vattr);
1259 dp->d_op = &afs_dentry_operations;
1260 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1261 d_instantiate(dp, ip);
1266 return afs_convert_code(code);
1270 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1273 cred_t *credp = crref();
1274 const char *name = dp->d_name.name;
1276 /* locking kernel conflicts with glock? */
1279 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1282 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1283 * that failed because a directory is not empty. So, we map
1284 * EEXIST to ENOTEMPTY on linux.
1286 if (code == EEXIST) {
1295 return afs_convert_code(code);
1300 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1301 struct inode *newip, struct dentry *newdp)
1304 cred_t *credp = crref();
1305 const char *oldname = olddp->d_name.name;
1306 const char *newname = newdp->d_name.name;
1307 struct dentry *rehash = NULL;
1309 /* Prevent any new references during rename operation. */
1311 if (!d_unhashed(newdp)) {
1316 if (atomic_read(&olddp->d_count) > 1)
1317 shrink_dcache_parent(olddp);
1320 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1324 olddp->d_time = 0; /* force to revalidate */
1330 return afs_convert_code(code);
1334 /* afs_linux_ireadlink
1335 * Internal readlink which can return link contents to user or kernel space.
1336 * Note that the buffer is NOT supposed to be null-terminated.
1339 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1342 cred_t *credp = crref();
1346 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1347 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1351 return maxlen - tuio.uio_resid;
1353 return afs_convert_code(code);
1356 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1357 /* afs_linux_readlink
1358 * Fill target (which is in user space) with contents of symlink.
1361 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1364 struct inode *ip = dp->d_inode;
1367 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1373 /* afs_linux_follow_link
1374 * a file system dependent link following routine.
1376 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1381 name = osi_Alloc(PATH_MAX);
1387 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1395 code = vfs_follow_link(nd, name);
1398 osi_Free(name, PATH_MAX);
1403 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1405 #if defined(AFS_CACHE_BYPASS)
1406 #endif /* defined(AFS_CACHE_BYPASS */
1408 /* Populate a page by filling it from the cache file pointed at by cachefp
1409 * (which contains indicated chunk)
1410 * If task is NULL, the page copy occurs syncronously, and the routine
1411 * returns with page still locked. If task is non-NULL, then page copies
1412 * may occur in the background, and the page will be unlocked when it is
1416 afs_linux_read_cache(struct file *cachefp, struct page *page,
1417 int chunk, struct pagevec *lrupv,
1418 struct afs_pagecopy_task *task) {
1419 loff_t offset = page_offset(page);
1420 struct page *newpage, *cachepage;
1421 struct address_space *cachemapping;
1425 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1429 /* From our offset, we now need to work out which page in the disk
1430 * file it corresponds to. This will be fun ... */
1431 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1433 while (cachepage == NULL) {
1434 cachepage = find_get_page(cachemapping, pageindex);
1437 newpage = page_cache_alloc_cold(cachemapping);
1443 code = add_to_page_cache(newpage, cachemapping,
1444 pageindex, GFP_KERNEL);
1446 cachepage = newpage;
1449 page_cache_get(cachepage);
1450 if (!pagevec_add(lrupv, cachepage))
1451 __pagevec_lru_add_file(lrupv);
1454 page_cache_release(newpage);
1456 if (code != -EEXIST)
1460 lock_page(cachepage);
1464 if (!PageUptodate(cachepage)) {
1465 ClearPageError(cachepage);
1466 code = cachemapping->a_ops->readpage(NULL, cachepage);
1467 if (!code && !task) {
1468 wait_on_page_locked(cachepage);
1471 unlock_page(cachepage);
1475 if (PageUptodate(cachepage)) {
1476 copy_highpage(page, cachepage);
1477 flush_dcache_page(page);
1478 SetPageUptodate(page);
1483 afs_pagecopy_queue_page(task, cachepage, page);
1495 page_cache_release(cachepage);
1501 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1503 loff_t offset = page_offset(pp);
1504 struct inode *ip = FILE_INODE(fp);
1505 struct vcache *avc = VTOAFS(ip);
1507 struct file *cacheFp = NULL;
1510 struct pagevec lrupv;
1512 /* Not a UFS cache, don't do anything */
1513 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1516 /* Can't do anything if the vcache isn't statd , or if the read
1517 * crosses a chunk boundary.
1519 if (!(avc->f.states & CStatd) ||
1520 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1524 ObtainWriteLock(&avc->lock, 911);
1526 /* XXX - See if hinting actually makes things faster !!! */
1528 /* See if we have a suitable entry already cached */
1532 /* We need to lock xdcache, then dcache, to handle situations where
1533 * the hint is on the free list. However, we can't safely do this
1534 * according to the locking hierarchy. So, use a non blocking lock.
1536 ObtainReadLock(&afs_xdcache);
1537 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1539 if (dcLocked && (tdc->index != NULLIDX)
1540 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1541 && tdc->f.chunk == AFS_CHUNK(offset)
1542 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1543 /* Bonus - the hint was correct */
1546 /* Only destroy the hint if its actually invalid, not if there's
1547 * just been a locking failure */
1549 ReleaseReadLock(&tdc->lock);
1556 ReleaseReadLock(&afs_xdcache);
1559 /* No hint, or hint is no longer valid - see if we can get something
1560 * directly from the dcache
1563 tdc = afs_FindDCache(avc, offset);
1566 ReleaseWriteLock(&avc->lock);
1571 ObtainReadLock(&tdc->lock);
1573 /* Is the dcache we've been given currently up to date */
1574 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1575 (tdc->dflags & DFFetching)) {
1576 ReleaseWriteLock(&avc->lock);
1577 ReleaseReadLock(&tdc->lock);
1582 /* Update our hint for future abuse */
1585 /* Okay, so we've now got a cache file that is up to date */
1587 /* XXX - I suspect we should be locking the inodes before we use them! */
1589 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1590 pagevec_init(&lrupv, 0);
1592 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1594 if (pagevec_count(&lrupv))
1595 __pagevec_lru_add_file(&lrupv);
1597 filp_close(cacheFp, NULL);
1600 ReleaseReadLock(&tdc->lock);
1601 ReleaseWriteLock(&avc->lock);
1608 /* afs_linux_readpage
1610 * This function is split into two, because prepare_write/begin_write
1611 * require a readpage call which doesn't unlock the resulting page upon
1615 afs_linux_fillpage(struct file *fp, struct page *pp)
1620 struct iovec *iovecp;
1621 struct inode *ip = FILE_INODE(fp);
1622 afs_int32 cnt = page_count(pp);
1623 struct vcache *avc = VTOAFS(ip);
1624 afs_offs_t offset = page_offset(pp);
1628 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1638 auio = osi_Alloc(sizeof(uio_t));
1639 iovecp = osi_Alloc(sizeof(struct iovec));
1641 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1646 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1647 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1648 99999); /* not a possible code value */
1650 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1652 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1653 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1655 AFS_DISCON_UNLOCK();
1658 /* XXX valid for no-cache also? Check last bits of files... :)
1659 * Cognate code goes in afs_NoCacheFetchProc. */
1660 if (auio->uio_resid) /* zero remainder of page */
1661 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1664 flush_dcache_page(pp);
1665 SetPageUptodate(pp);
1670 osi_Free(auio, sizeof(uio_t));
1671 osi_Free(iovecp, sizeof(struct iovec));
1674 return afs_convert_code(code);
1678 afs_linux_prefetch(struct file *fp, struct page *pp)
1681 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1682 afs_offs_t offset = page_offset(pp);
1684 if (AFS_CHUNKOFFSET(offset) == 0) {
1686 struct vrequest treq;
1691 code = afs_InitReq(&treq, credp);
1692 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1693 tdc = afs_FindDCache(avc, offset);
1695 if (!(tdc->mflags & DFNextStarted))
1696 afs_PrefetchChunk(avc, tdc, credp, &treq);
1699 ReleaseWriteLock(&avc->lock);
1704 return afs_convert_code(code);
1708 #if defined(AFS_CACHE_BYPASS)
1711 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1712 struct list_head *page_list, unsigned num_pages)
1717 struct iovec* iovecp;
1718 struct nocache_read_request *ancr;
1720 struct pagevec lrupv;
1724 struct inode *ip = FILE_INODE(fp);
1725 struct vcache *avc = VTOAFS(ip);
1726 afs_int32 base_index = 0;
1727 afs_int32 page_count = 0;
1730 /* background thread must free: iovecp, auio, ancr */
1731 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1733 auio = osi_Alloc(sizeof(uio_t));
1734 auio->uio_iov = iovecp;
1735 auio->uio_iovcnt = num_pages;
1736 auio->uio_flag = UIO_READ;
1737 auio->uio_seg = AFS_UIOSYS;
1738 auio->uio_resid = num_pages * PAGE_SIZE;
1740 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1742 ancr->offset = auio->uio_offset;
1743 ancr->length = auio->uio_resid;
1745 pagevec_init(&lrupv, 0);
1747 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1749 if(list_empty(page_list))
1752 pp = list_entry(page_list->prev, struct page, lru);
1753 /* If we allocate a page and don't remove it from page_list,
1754 * the page cache gets upset. */
1756 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1757 if(pp->index > isize) {
1764 offset = page_offset(pp);
1765 auio->uio_offset = offset;
1766 base_index = pp->index;
1768 iovecp[page_ix].iov_len = PAGE_SIZE;
1769 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1770 if(base_index != pp->index) {
1773 page_cache_release(pp);
1774 iovecp[page_ix].iov_base = (void *) 0;
1776 ancr->length -= PAGE_SIZE;
1783 page_cache_release(pp);
1784 iovecp[page_ix].iov_base = (void *) 0;
1787 if(!PageLocked(pp)) {
1791 /* increment page refcount--our original design assumed
1792 * that locking it would effectively pin it; protect
1793 * ourselves from the possiblity that this assumption is
1794 * is faulty, at low cost (provided we do not fail to
1795 * do the corresponding decref on the other side) */
1798 /* save the page for background map */
1799 iovecp[page_ix].iov_base = (void*) pp;
1801 /* and put it on the LRU cache */
1802 if (!pagevec_add(&lrupv, pp))
1803 __pagevec_lru_add_file(&lrupv);
1807 /* If there were useful pages in the page list, make sure all pages
1808 * are in the LRU cache, then schedule the read */
1810 if (pagevec_count(&lrupv))
1811 __pagevec_lru_add_file(&lrupv);
1813 code = afs_ReadNoCache(avc, ancr, credp);
1816 /* If there is nothing for the background thread to handle,
1817 * it won't be freeing the things that we never gave it */
1818 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1819 osi_Free(auio, sizeof(uio_t));
1820 osi_Free(ancr, sizeof(struct nocache_read_request));
1822 /* we do not flush, release, or unmap pages--that will be
1823 * done for us by the background thread as each page comes in
1824 * from the fileserver */
1825 return afs_convert_code(code);
1830 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1832 cred_t *credp = NULL;
1834 struct iovec *iovecp;
1835 struct nocache_read_request *ancr;
1839 * Special case: if page is at or past end of file, just zero it and set
1842 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1843 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1844 SetPageUptodate(pp);
1851 /* receiver frees */
1852 auio = osi_Alloc(sizeof(uio_t));
1853 iovecp = osi_Alloc(sizeof(struct iovec));
1855 /* address can be NULL, because we overwrite it with 'pp', below */
1856 setup_uio(auio, iovecp, NULL, page_offset(pp),
1857 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1859 /* save the page for background map */
1860 get_page(pp); /* see above */
1861 auio->uio_iov->iov_base = (void*) pp;
1862 /* the background thread will free this */
1863 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1865 ancr->offset = page_offset(pp);
1866 ancr->length = PAGE_SIZE;
1869 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1872 return afs_convert_code(code);
1876 afs_linux_can_bypass(struct inode *ip) {
1877 switch(cache_bypass_strategy) {
1878 case NEVER_BYPASS_CACHE:
1880 case ALWAYS_BYPASS_CACHE:
1882 case LARGE_FILES_BYPASS_CACHE:
1883 if(i_size_read(ip) > cache_bypass_threshold)
1890 /* Check if a file is permitted to bypass the cache by policy, and modify
1891 * the cache bypass state recorded for that file */
1894 afs_linux_bypass_check(struct inode *ip) {
1897 int bypass = afs_linux_can_bypass(ip);
1900 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1908 afs_linux_bypass_check(struct inode *ip) {
1912 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1916 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1917 struct list_head *page_list, unsigned int num_pages) {
1923 afs_linux_readpage(struct file *fp, struct page *pp)
1927 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1928 code = afs_linux_bypass_readpage(fp, pp);
1930 code = afs_linux_fillpage(fp, pp);
1932 code = afs_linux_prefetch(fp, pp);
1939 /* Readpages reads a number of pages for a particular file. We use
1940 * this to optimise the reading, by limiting the number of times upon which
1941 * we have to lookup, lock and open vcaches and dcaches
1945 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1946 struct list_head *page_list, unsigned int num_pages)
1948 struct inode *inode = mapping->host;
1949 struct vcache *avc = VTOAFS(inode);
1951 struct file *cacheFp = NULL;
1953 unsigned int page_idx;
1955 struct pagevec lrupv;
1956 struct afs_pagecopy_task *task;
1958 if (afs_linux_bypass_check(inode))
1959 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1961 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1965 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1970 ObtainWriteLock(&avc->lock, 912);
1973 task = afs_pagecopy_init_task();
1976 pagevec_init(&lrupv, 0);
1977 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1978 struct page *page = list_entry(page_list->prev, struct page, lru);
1979 list_del(&page->lru);
1980 offset = page_offset(page);
1982 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1984 ReleaseReadLock(&tdc->lock);
1989 filp_close(cacheFp, NULL);
1994 if ((tdc = afs_FindDCache(avc, offset))) {
1995 ObtainReadLock(&tdc->lock);
1996 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1997 (tdc->dflags & DFFetching)) {
1998 ReleaseReadLock(&tdc->lock);
2005 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2008 if (tdc && !add_to_page_cache(page, mapping, page->index,
2010 page_cache_get(page);
2011 if (!pagevec_add(&lrupv, page))
2012 __pagevec_lru_add_file(&lrupv);
2014 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2016 page_cache_release(page);
2018 if (pagevec_count(&lrupv))
2019 __pagevec_lru_add_file(&lrupv);
2022 filp_close(cacheFp, NULL);
2024 afs_pagecopy_put_task(task);
2028 ReleaseReadLock(&tdc->lock);
2032 ReleaseWriteLock(&avc->lock);
2037 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2040 afs_linux_prepare_writeback(struct vcache *avc) {
2041 if (avc->f.states & CPageWrite) {
2042 return AOP_WRITEPAGE_ACTIVATE;
2044 avc->f.states |= CPageWrite;
2049 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2050 struct vrequest treq;
2053 if (!afs_InitReq(&treq, credp))
2054 code = afs_DoPartialWrite(avc, &treq);
2056 return afs_convert_code(code);
2060 afs_linux_complete_writeback(struct vcache *avc) {
2061 avc->f.states &= ~CPageWrite;
2064 /* Writeback a given page syncronously. Called with no AFS locks held */
2066 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2067 unsigned long offset, unsigned int count,
2070 struct vcache *vcp = VTOAFS(ip);
2078 buffer = kmap(pp) + offset;
2079 base = page_offset(pp) + offset;
2082 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2083 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2084 ICL_TYPE_INT32, 99999);
2086 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2088 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2090 i_size_write(ip, vcp->f.m.Length);
2091 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2093 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2095 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2096 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2097 ICL_TYPE_INT32, code);
2106 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2107 unsigned long offset, unsigned int count)
2111 struct vcache *vcp = VTOAFS(ip);
2114 /* Catch recursive writeback. This occurs if the kernel decides
2115 * writeback is required whilst we are writing to the cache, or
2116 * flushing to the server. When we're running syncronously (as
2117 * opposed to from writepage) we can't actually do anything about
2118 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2121 ObtainWriteLock(&vcp->lock, 532);
2122 afs_linux_prepare_writeback(vcp);
2123 ReleaseWriteLock(&vcp->lock);
2127 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2130 ObtainWriteLock(&vcp->lock, 533);
2132 code1 = afs_linux_dopartialwrite(vcp, credp);
2133 afs_linux_complete_writeback(vcp);
2134 ReleaseWriteLock(&vcp->lock);
2145 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2146 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2148 afs_linux_writepage(struct page *pp)
2151 struct address_space *mapping = pp->mapping;
2152 struct inode *inode;
2155 unsigned int to = PAGE_CACHE_SIZE;
2160 if (PageReclaim(pp)) {
2161 return AOP_WRITEPAGE_ACTIVATE;
2162 /* XXX - Do we need to redirty the page here? */
2167 inode = mapping->host;
2168 vcp = VTOAFS(inode);
2169 isize = i_size_read(inode);
2171 /* Don't defeat an earlier truncate */
2172 if (page_offset(pp) > isize) {
2173 set_page_writeback(pp);
2179 ObtainWriteLock(&vcp->lock, 537);
2180 code = afs_linux_prepare_writeback(vcp);
2181 if (code == AOP_WRITEPAGE_ACTIVATE) {
2182 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2183 * to return with the page still locked */
2184 ReleaseWriteLock(&vcp->lock);
2189 /* Grab the creds structure currently held in the vnode, and
2190 * get a reference to it, in case it goes away ... */
2196 ReleaseWriteLock(&vcp->lock);
2199 set_page_writeback(pp);
2201 SetPageUptodate(pp);
2203 /* We can unlock the page here, because it's protected by the
2204 * page_writeback flag. This should make us less vulnerable to
2205 * deadlocking in afs_write and afs_DoPartialWrite
2209 /* If this is the final page, then just write the number of bytes that
2210 * are actually in it */
2211 if ((isize - page_offset(pp)) < to )
2212 to = isize - page_offset(pp);
2214 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2217 ObtainWriteLock(&vcp->lock, 538);
2219 /* As much as we might like to ignore a file server error here,
2220 * and just try again when we close(), unfortunately StoreAllSegments
2221 * will invalidate our chunks if the server returns a permanent error,
2222 * so we need to at least try and get that error back to the user
2225 code1 = afs_linux_dopartialwrite(vcp, credp);
2227 afs_linux_complete_writeback(vcp);
2228 ReleaseWriteLock(&vcp->lock);
2233 end_page_writeback(pp);
2234 page_cache_release(pp);
2245 /* afs_linux_permission
2246 * Check access rights - returns error if can't check or permission denied.
2249 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2250 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2252 afs_linux_permission(struct inode *ip, int mode)
2256 cred_t *credp = crref();
2260 if (mode & MAY_EXEC)
2262 if (mode & MAY_READ)
2264 if (mode & MAY_WRITE)
2266 code = afs_access(VTOAFS(ip), tmp, credp);
2270 return afs_convert_code(code);
2274 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2278 struct inode *inode = FILE_INODE(file);
2279 loff_t pagebase = page_offset(page);
2281 if (i_size_read(inode) < (pagebase + offset))
2282 i_size_write(inode, pagebase + offset);
2284 if (PageChecked(page)) {
2285 SetPageUptodate(page);
2286 ClearPageChecked(page);
2289 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2295 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2299 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2300 * prepare_write. Essentially, if the page exists within the file,
2301 * and is not being fully written, then we should populate it.
2304 if (!PageUptodate(page)) {
2305 loff_t pagebase = page_offset(page);
2306 loff_t isize = i_size_read(page->mapping->host);
2308 /* Is the location we are writing to beyond the end of the file? */
2309 if (pagebase >= isize ||
2310 ((from == 0) && (pagebase + to) >= isize)) {
2311 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2312 SetPageChecked(page);
2313 /* Are we we writing a full page */
2314 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2315 SetPageChecked(page);
2316 /* Is the page readable, if it's wronly, we don't care, because we're
2317 * not actually going to read from it ... */
2318 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2319 /* We don't care if fillpage fails, because if it does the page
2320 * won't be marked as up to date
2322 afs_linux_fillpage(file, page);
2328 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2330 afs_linux_write_end(struct file *file, struct address_space *mapping,
2331 loff_t pos, unsigned len, unsigned copied,
2332 struct page *page, void *fsdata)
2335 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2337 code = afs_linux_commit_write(file, page, from, from + len);
2340 page_cache_release(page);
2345 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2346 loff_t pos, unsigned len, unsigned flags,
2347 struct page **pagep, void **fsdata)
2350 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2351 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2354 page = grab_cache_page_write_begin(mapping, index, flags);
2357 code = afs_linux_prepare_write(file, page, from, from + len);
2360 page_cache_release(page);
2368 static struct inode_operations afs_file_iops = {
2369 .permission = afs_linux_permission,
2370 .getattr = afs_linux_getattr,
2371 .setattr = afs_notify_change,
2374 static struct address_space_operations afs_file_aops = {
2375 .readpage = afs_linux_readpage,
2376 .readpages = afs_linux_readpages,
2377 .writepage = afs_linux_writepage,
2378 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2379 .write_begin = afs_linux_write_begin,
2380 .write_end = afs_linux_write_end,
2382 .commit_write = afs_linux_commit_write,
2383 .prepare_write = afs_linux_prepare_write,
2388 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2389 * by what sort of operation is allowed.....
2392 static struct inode_operations afs_dir_iops = {
2393 .setattr = afs_notify_change,
2394 .create = afs_linux_create,
2395 .lookup = afs_linux_lookup,
2396 .link = afs_linux_link,
2397 .unlink = afs_linux_unlink,
2398 .symlink = afs_linux_symlink,
2399 .mkdir = afs_linux_mkdir,
2400 .rmdir = afs_linux_rmdir,
2401 .rename = afs_linux_rename,
2402 .getattr = afs_linux_getattr,
2403 .permission = afs_linux_permission,
2406 /* We really need a separate symlink set of ops, since do_follow_link()
2407 * determines if it _is_ a link by checking if the follow_link op is set.
2409 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2411 afs_symlink_filler(struct file *file, struct page *page)
2413 struct inode *ip = (struct inode *)page->mapping->host;
2414 char *p = (char *)kmap(page);
2418 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2423 p[code] = '\0'; /* null terminate? */
2425 SetPageUptodate(page);
2437 static struct address_space_operations afs_symlink_aops = {
2438 .readpage = afs_symlink_filler
2440 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2442 static struct inode_operations afs_symlink_iops = {
2443 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2444 .readlink = page_readlink,
2445 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2446 .follow_link = page_follow_link,
2448 .follow_link = page_follow_link_light,
2449 .put_link = page_put_link,
2451 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2452 .readlink = afs_linux_readlink,
2453 .follow_link = afs_linux_follow_link,
2454 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2455 .setattr = afs_notify_change,
2459 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2463 vattr2inode(ip, vattr);
2465 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2466 /* Reset ops if symlink or directory. */
2467 if (S_ISREG(ip->i_mode)) {
2468 ip->i_op = &afs_file_iops;
2469 ip->i_fop = &afs_file_fops;
2470 ip->i_data.a_ops = &afs_file_aops;
2472 } else if (S_ISDIR(ip->i_mode)) {
2473 ip->i_op = &afs_dir_iops;
2474 ip->i_fop = &afs_dir_fops;
2476 } else if (S_ISLNK(ip->i_mode)) {
2477 ip->i_op = &afs_symlink_iops;
2478 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2479 ip->i_data.a_ops = &afs_symlink_aops;
2480 ip->i_mapping = &ip->i_data;