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_PAGEVEC_LRU_ADD_FILE
45 #define __pagevec_lru_add_file __pagevec_lru_add
49 #define MAX_ERRNO 1000L
52 extern struct backing_dev_info *afs_backing_dev_info;
54 extern struct vcache *afs_globalVp;
55 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
56 /* Some uses of BKL are perhaps not needed for bypass or memcache--
57 * why don't we try it out? */
58 extern struct afs_cacheOps afs_UfsCacheOps;
61 afs_maybe_lock_kernel(void) {
62 if(afs_cacheType == &afs_UfsCacheOps)
67 afs_maybe_unlock_kernel(void) {
68 if(afs_cacheType == &afs_UfsCacheOps)
72 /* This function converts a positive error code from AFS into a negative
73 * code suitable for passing into the Linux VFS layer. It checks that the
74 * error code is within the permissable bounds for the ERR_PTR mechanism.
76 * _All_ error codes which come from the AFS layer should be passed through
77 * this function before being returned to the kernel.
81 afs_convert_code(int code) {
82 if ((code >= 0) && (code <= MAX_ERRNO))
88 /* Linux doesn't require a credp for many functions, and crref is an expensive
89 * operation. This helper function avoids obtaining it for VerifyVCache calls
93 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
98 if (avc->f.states & CStatd) {
106 code = afs_InitReq(&treq, credp);
108 code = afs_VerifyVCache2(avc, &treq);
115 return afs_convert_code(code);
119 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
122 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
125 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
126 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
128 code = afs_linux_VerifyVCache(vcp, NULL);
131 /* Linux's FlushPages implementation doesn't ever use credp,
132 * so we optimise by not using it */
133 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
135 code = do_sync_read(fp, buf, count, offp);
139 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
140 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
147 /* Now we have integrated VM for writes as well as reads. generic_file_write
148 * also takes care of re-positioning the pointer if file is open in append
149 * mode. Call fake open/close to ensure we do writes of core dumps.
152 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
155 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
160 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
161 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
162 (fp->f_flags & O_APPEND) ? 99998 : 99999);
164 code = afs_linux_VerifyVCache(vcp, &credp);
166 ObtainWriteLock(&vcp->lock, 529);
168 ReleaseWriteLock(&vcp->lock);
171 code = do_sync_write(fp, buf, count, offp);
175 ObtainWriteLock(&vcp->lock, 530);
177 if (vcp->execsOrWriters == 1 && !credp)
180 afs_FakeClose(vcp, credp);
181 ReleaseWriteLock(&vcp->lock);
183 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
184 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
193 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
195 /* This is a complete rewrite of afs_readdir, since we can make use of
196 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
197 * handling and use of bulkstats will need to be reflected here as well.
200 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
202 struct vcache *avc = VTOAFS(FILE_INODE(fp));
203 struct vrequest treq;
204 register struct dcache *tdc;
211 afs_size_t origOffset, tlen;
212 cred_t *credp = crref();
213 struct afs_fakestat_state fakestat;
215 afs_maybe_lock_kernel();
217 AFS_STATCNT(afs_readdir);
219 code = afs_convert_code(afs_InitReq(&treq, credp));
224 afs_InitFakeStat(&fakestat);
225 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
229 /* update the cache entry */
231 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
235 /* get a reference to the entire directory */
236 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
242 ObtainSharedLock(&avc->lock, 810);
243 UpgradeSToWLock(&avc->lock, 811);
244 ObtainReadLock(&tdc->lock);
246 * Make sure that the data in the cache is current. There are two
247 * cases we need to worry about:
248 * 1. The cache data is being fetched by another process.
249 * 2. The cache data is no longer valid
251 while ((avc->f.states & CStatd)
252 && (tdc->dflags & DFFetching)
253 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
254 ReleaseReadLock(&tdc->lock);
255 ReleaseSharedLock(&avc->lock);
256 afs_osi_Sleep(&tdc->validPos);
257 ObtainSharedLock(&avc->lock, 812);
258 ObtainReadLock(&tdc->lock);
260 if (!(avc->f.states & CStatd)
261 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
262 ReleaseReadLock(&tdc->lock);
263 ReleaseSharedLock(&avc->lock);
268 /* Set the readdir-in-progress flag, and downgrade the lock
269 * to shared so others will be able to acquire a read lock.
271 avc->f.states |= CReadDir;
272 avc->dcreaddir = tdc;
273 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
274 ConvertWToSLock(&avc->lock);
276 /* Fill in until we get an error or we're done. This implementation
277 * takes an offset in units of blobs, rather than bytes.
280 offset = (int) fp->f_pos;
282 dirpos = BlobScan(tdc, offset);
286 de = afs_dir_GetBlob(tdc, dirpos);
290 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
293 len = strlen(de->name);
295 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
296 (unsigned long)&tdc->f.inode, dirpos);
298 ReleaseSharedLock(&avc->lock);
304 /* filldir returns -EINVAL when the buffer is full. */
306 unsigned int type = DT_UNKNOWN;
307 struct VenusFid afid;
310 afid.Cell = avc->f.fid.Cell;
311 afid.Fid.Volume = avc->f.fid.Fid.Volume;
312 afid.Fid.Vnode = ntohl(de->fid.vnode);
313 afid.Fid.Unique = ntohl(de->fid.vunique);
314 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
316 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
319 } else if (((tvc->f.states) & (CStatd | CTruth))) {
320 /* CTruth will be set if the object has
325 else if (vtype == VREG)
327 /* Don't do this until we're sure it can't be a mtpt */
328 /* else if (vtype == VLNK)
330 /* what other types does AFS support? */
332 /* clean up from afs_FindVCache */
336 * If this is NFS readdirplus, then the filler is going to
337 * call getattr on this inode, which will deadlock if we're
341 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
347 offset = dirpos + 1 + ((len + 16) >> 5);
349 /* If filldir didn't fill in the last one this is still pointing to that
352 fp->f_pos = (loff_t) offset;
354 ReleaseReadLock(&tdc->lock);
356 UpgradeSToWLock(&avc->lock, 813);
357 avc->f.states &= ~CReadDir;
359 avc->readdir_pid = 0;
360 ReleaseSharedLock(&avc->lock);
364 afs_PutFakeStat(&fakestat);
367 afs_maybe_unlock_kernel();
372 /* in afs_pioctl.c */
373 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
376 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
377 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
379 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
386 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
388 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
392 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
393 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
394 vmap->vm_end - vmap->vm_start);
396 /* get a validated vcache entry */
397 code = afs_linux_VerifyVCache(vcp, NULL);
399 /* Linux's Flushpage implementation doesn't use credp, so optimise
400 * our code to not need to crref() it */
401 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
403 code = generic_file_mmap(fp, vmap);
406 vcp->f.states |= CMAPPED;
413 afs_linux_open(struct inode *ip, struct file *fp)
415 struct vcache *vcp = VTOAFS(ip);
416 cred_t *credp = crref();
419 afs_maybe_lock_kernel();
421 code = afs_open(&vcp, fp->f_flags, credp);
423 afs_maybe_unlock_kernel();
426 return afs_convert_code(code);
430 afs_linux_release(struct inode *ip, struct file *fp)
432 struct vcache *vcp = VTOAFS(ip);
433 cred_t *credp = crref();
436 afs_maybe_lock_kernel();
438 code = afs_close(vcp, fp->f_flags, credp);
439 ObtainWriteLock(&vcp->lock, 807);
444 ReleaseWriteLock(&vcp->lock);
446 afs_maybe_unlock_kernel();
449 return afs_convert_code(code);
453 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
456 struct inode *ip = FILE_INODE(fp);
457 cred_t *credp = crref();
459 afs_maybe_lock_kernel();
461 code = afs_fsync(VTOAFS(ip), credp);
463 afs_maybe_unlock_kernel();
465 return afs_convert_code(code);
471 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
474 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
475 cred_t *credp = crref();
476 struct AFS_FLOCK flock;
478 /* Convert to a lock format afs_lockctl understands. */
479 memset(&flock, 0, sizeof(flock));
480 flock.l_type = flp->fl_type;
481 flock.l_pid = flp->fl_pid;
483 flock.l_start = flp->fl_start;
484 flock.l_len = flp->fl_end - flp->fl_start + 1;
486 /* Safe because there are no large files, yet */
487 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
488 if (cmd == F_GETLK64)
490 else if (cmd == F_SETLK64)
492 else if (cmd == F_SETLKW64)
494 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
497 code = afs_lockctl(vcp, &flock, cmd, credp);
500 if ((code == 0 || flp->fl_type == F_UNLCK) &&
501 (cmd == F_SETLK || cmd == F_SETLKW)) {
502 code = afs_posix_lock_file(fp, flp);
503 if (code && flp->fl_type != F_UNLCK) {
504 struct AFS_FLOCK flock2;
506 flock2.l_type = F_UNLCK;
508 afs_lockctl(vcp, &flock2, F_SETLK, credp);
512 /* If lockctl says there are no conflicting locks, then also check with the
513 * kernel, as lockctl knows nothing about byte range locks
515 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
516 afs_posix_test_lock(fp, flp);
517 /* If we found a lock in the kernel's structure, return it */
518 if (flp->fl_type != F_UNLCK) {
524 /* Convert flock back to Linux's file_lock */
525 flp->fl_type = flock.l_type;
526 flp->fl_pid = flock.l_pid;
527 flp->fl_start = flock.l_start;
528 flp->fl_end = flock.l_start + flock.l_len - 1;
531 return afs_convert_code(code);
534 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
536 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
538 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
539 cred_t *credp = crref();
540 struct AFS_FLOCK flock;
541 /* Convert to a lock format afs_lockctl understands. */
542 memset(&flock, 0, sizeof(flock));
543 flock.l_type = flp->fl_type;
544 flock.l_pid = flp->fl_pid;
547 flock.l_len = OFFSET_MAX;
549 /* Safe because there are no large files, yet */
550 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
551 if (cmd == F_GETLK64)
553 else if (cmd == F_SETLK64)
555 else if (cmd == F_SETLKW64)
557 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
560 code = afs_lockctl(vcp, &flock, cmd, credp);
563 if ((code == 0 || flp->fl_type == F_UNLCK) &&
564 (cmd == F_SETLK || cmd == F_SETLKW)) {
565 flp->fl_flags &=~ FL_SLEEP;
566 code = flock_lock_file_wait(fp, flp);
567 if (code && flp->fl_type != F_UNLCK) {
568 struct AFS_FLOCK flock2;
570 flock2.l_type = F_UNLCK;
572 afs_lockctl(vcp, &flock2, F_SETLK, credp);
576 /* Convert flock back to Linux's file_lock */
577 flp->fl_type = flock.l_type;
578 flp->fl_pid = flock.l_pid;
581 return afs_convert_code(code);
586 * essentially the same as afs_fsync() but we need to get the return
587 * code for the sys_close() here, not afs_linux_release(), so call
588 * afs_StoreAllSegments() with AFS_LASTSTORE
591 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
592 afs_linux_flush(struct file *fp, fl_owner_t id)
594 afs_linux_flush(struct file *fp)
597 struct vrequest treq;
601 #if defined(AFS_CACHE_BYPASS)
607 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
615 vcp = VTOAFS(FILE_INODE(fp));
617 code = afs_InitReq(&treq, credp);
620 #if defined(AFS_CACHE_BYPASS)
621 /* If caching is bypassed for this file, or globally, just return 0 */
622 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
625 ObtainReadLock(&vcp->lock);
626 if(vcp->cachingStates & FCSBypass)
628 ReleaseReadLock(&vcp->lock);
631 /* future proof: don't rely on 0 return from afs_InitReq */
636 ObtainSharedLock(&vcp->lock, 535);
637 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
638 UpgradeSToWLock(&vcp->lock, 536);
639 if (!AFS_IS_DISCONNECTED) {
640 code = afs_StoreAllSegments(vcp,
642 AFS_SYNC | AFS_LASTSTORE);
644 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
646 ConvertWToSLock(&vcp->lock);
648 code = afs_CheckCode(code, &treq, 54);
649 ReleaseSharedLock(&vcp->lock);
656 return afs_convert_code(code);
659 struct file_operations afs_dir_fops = {
660 .read = generic_read_dir,
661 .readdir = afs_linux_readdir,
662 #ifdef HAVE_UNLOCKED_IOCTL
663 .unlocked_ioctl = afs_unlocked_xioctl,
667 #ifdef HAVE_COMPAT_IOCTL
668 .compat_ioctl = afs_unlocked_xioctl,
670 .open = afs_linux_open,
671 .release = afs_linux_release,
674 struct file_operations afs_file_fops = {
675 .read = afs_linux_read,
676 .write = afs_linux_write,
677 #ifdef GENERIC_FILE_AIO_READ
678 .aio_read = generic_file_aio_read,
679 .aio_write = generic_file_aio_write,
681 #ifdef HAVE_UNLOCKED_IOCTL
682 .unlocked_ioctl = afs_unlocked_xioctl,
686 #ifdef HAVE_COMPAT_IOCTL
687 .compat_ioctl = afs_unlocked_xioctl,
689 .mmap = afs_linux_mmap,
690 .open = afs_linux_open,
691 .flush = afs_linux_flush,
692 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
693 .sendfile = generic_file_sendfile,
695 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
696 .splice_write = generic_file_splice_write,
697 .splice_read = generic_file_splice_read,
699 .release = afs_linux_release,
700 .fsync = afs_linux_fsync,
701 .lock = afs_linux_lock,
702 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
703 .flock = afs_linux_flock,
708 /**********************************************************************
709 * AFS Linux dentry operations
710 **********************************************************************/
712 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
713 * that has its mvid (parent dir's fid) pointer set to the wrong directory
714 * due to being mounted in multiple points at once. If so, check_bad_parent()
715 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
716 * dotdotfid and mtpoint fid members.
718 * dp - dentry to be checked.
722 * This dentry's vcache's mvid will be set to the correct parent directory's
724 * This root vnode's volume will have its dotdotfid and mtpoint fids set
725 * to the correct parent and mountpoint fids.
729 check_bad_parent(struct dentry *dp)
732 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
733 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
735 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
738 /* force a lookup, so vcp->mvid is fixed up */
739 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
740 if (!avc || vcp != avc) { /* bad, very bad.. */
741 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
742 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
743 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
744 ICL_TYPE_POINTER, dp);
747 AFS_RELE(AFSTOV(avc));
754 /* afs_linux_revalidate
755 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
758 afs_linux_revalidate(struct dentry *dp)
761 struct vcache *vcp = VTOAFS(dp->d_inode);
765 if (afs_shuttingdown)
768 afs_maybe_lock_kernel();
772 /* Make this a fast path (no crref), since it's called so often. */
773 if (vcp->f.states & CStatd) {
775 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
776 check_bad_parent(dp); /* check and correct mvid */
784 /* This avoids the crref when we don't have to do it. Watch for
785 * changes in afs_getattr that don't get replicated here!
787 if (vcp->f.states & CStatd &&
788 (!afs_fakestat_enable || vcp->mvstat != 1) &&
790 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
791 code = afs_CopyOutAttrs(vcp, &vattr);
794 code = afs_getattr(vcp, &vattr, credp);
798 afs_fill_inode(AFSTOV(vcp), &vattr);
801 afs_maybe_unlock_kernel();
803 return afs_convert_code(code);
807 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
809 int err = afs_linux_revalidate(dentry);
811 generic_fillattr(dentry->d_inode, stat);
816 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
817 * In kernels 2.2.10 and above, we are passed an additional flags var which
818 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
819 * we are advised to follow the entry if it is a link or to make sure that
820 * it is a directory. But since the kernel itself checks these possibilities
821 * later on, we shouldn't have to do it until later. Perhaps in the future..
824 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
825 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
827 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
831 cred_t *credp = NULL;
832 struct vcache *vcp, *pvcp, *tvc = NULL;
833 struct dentry *parent;
835 struct afs_fakestat_state fakestate;
837 afs_maybe_lock_kernel();
839 afs_InitFakeStat(&fakestate);
842 vcp = VTOAFS(dp->d_inode);
844 if (vcp == afs_globalVp)
847 if (vcp->mvstat == 1) { /* mount point */
848 if (vcp->mvid && (vcp->f.states & CMValid)) {
851 struct vrequest treq;
854 code = afs_InitReq(&treq, credp);
856 (strcmp(dp->d_name.name, ".directory") == 0)) {
860 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
862 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
863 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
864 /* a mount point, not yet replaced by its directory */
869 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
870 check_bad_parent(dp); /* check and correct mvid */
873 /* If the last looker changes, we should make sure the current
874 * looker still has permission to examine this file. This would
875 * always require a crref() which would be "slow".
877 if (vcp->last_looker != treq.uid) {
878 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
881 vcp->last_looker = treq.uid;
885 parent = dget_parent(dp);
886 pvcp = VTOAFS(parent->d_inode);
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 (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
896 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
897 if (!tvc || tvc != vcp) {
902 if (afs_getattr(vcp, &vattr, credp)) {
907 vattr2inode(AFSTOV(vcp), &vattr);
908 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
911 /* should we always update the attributes at this point? */
912 /* unlikely--the vcache entry hasn't changed */
917 /* If this code is ever enabled, we should use dget_parent to handle
918 * getting the parent, and dput() to dispose of it. See above for an
920 pvcp = VTOAFS(dp->d_parent->d_inode);
921 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
925 /* No change in parent's DataVersion so this negative
926 * lookup is still valid. BUT, if a server is down a
927 * negative lookup can result so there should be a
928 * liftime as well. For now, always expire.
941 afs_PutFakeStat(&fakestate);
947 shrink_dcache_parent(dp);
950 afs_maybe_unlock_kernel();
954 if (have_submounts(dp))
962 afs_dentry_iput(struct dentry *dp, struct inode *ip)
964 struct vcache *vcp = VTOAFS(ip);
967 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
968 (void) afs_InactiveVCache(vcp, NULL);
971 afs_linux_clear_nfsfs_renamed(dp);
977 afs_dentry_delete(struct dentry *dp)
979 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
980 return 1; /* bad inode? */
985 struct dentry_operations afs_dentry_operations = {
986 .d_revalidate = afs_linux_dentry_revalidate,
987 .d_delete = afs_dentry_delete,
988 .d_iput = afs_dentry_iput,
991 /**********************************************************************
992 * AFS Linux inode operations
993 **********************************************************************/
997 * Merely need to set enough of vattr to get us through the create. Note
998 * that the higher level code (open_namei) will take care of any tuncation
999 * explicitly. Exclusive open is also taken care of in open_namei.
1001 * name is in kernel space at this point.
1004 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1005 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1006 struct nameidata *nd)
1008 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1012 cred_t *credp = crref();
1013 const char *name = dp->d_name.name;
1018 vattr.va_mode = mode;
1019 vattr.va_type = mode & S_IFMT;
1021 afs_maybe_lock_kernel();
1023 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1027 struct inode *ip = AFSTOV(vcp);
1029 afs_getattr(vcp, &vattr, credp);
1030 afs_fill_inode(ip, &vattr);
1031 insert_inode_hash(ip);
1032 dp->d_op = &afs_dentry_operations;
1033 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1034 d_instantiate(dp, ip);
1038 afs_maybe_unlock_kernel();
1040 return afs_convert_code(code);
1043 /* afs_linux_lookup */
1044 static struct dentry *
1045 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1046 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1047 struct nameidata *nd)
1049 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1052 cred_t *credp = crref();
1053 struct vcache *vcp = NULL;
1054 const char *comp = dp->d_name.name;
1055 struct inode *ip = NULL;
1056 struct dentry *newdp = NULL;
1059 afs_maybe_lock_kernel();
1061 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1067 afs_getattr(vcp, &vattr, credp);
1068 afs_fill_inode(ip, &vattr);
1069 if (hlist_unhashed(&ip->i_hash))
1070 insert_inode_hash(ip);
1072 dp->d_op = &afs_dentry_operations;
1073 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1076 if (ip && S_ISDIR(ip->i_mode)) {
1077 struct dentry *alias;
1079 /* Try to invalidate an existing alias in favor of our new one */
1080 alias = d_find_alias(ip);
1081 /* But not if it's disconnected; then we want d_splice_alias below */
1082 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1083 if (d_invalidate(alias) == 0) {
1087 afs_maybe_unlock_kernel();
1093 newdp = d_splice_alias(ip, dp);
1095 afs_maybe_unlock_kernel();
1098 /* It's ok for the file to not be found. That's noted by the caller by
1099 * seeing that the dp->d_inode field is NULL.
1101 if (!code || code == ENOENT)
1104 return ERR_PTR(afs_convert_code(code));
1108 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1111 cred_t *credp = crref();
1112 const char *name = newdp->d_name.name;
1113 struct inode *oldip = olddp->d_inode;
1115 /* If afs_link returned the vnode, we could instantiate the
1116 * dentry. Since it's not, we drop this one and do a new lookup.
1121 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1125 return afs_convert_code(code);
1128 /* We have to have a Linux specific sillyrename function, because we
1129 * also have to keep the dcache up to date when we're doing a silly
1130 * rename - so we don't want the generic vnodeops doing this behind our
1135 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1138 struct vcache *tvc = VTOAFS(dentry->d_inode);
1139 struct dentry *__dp = NULL;
1140 char *__name = NULL;
1143 if (afs_linux_nfsfs_renamed(dentry))
1151 osi_FreeSmallSpace(__name);
1152 __name = afs_newname();
1155 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1158 osi_FreeSmallSpace(__name);
1161 } while (__dp->d_inode != NULL);
1164 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1165 VTOAFS(dir), (char *)__dp->d_name.name,
1168 tvc->mvid = (void *) __name;
1171 crfree(tvc->uncred);
1173 tvc->uncred = credp;
1174 tvc->f.states |= CUnlinked;
1175 afs_linux_set_nfsfs_renamed(dentry);
1177 osi_FreeSmallSpace(__name);
1182 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1183 d_move(dentry, __dp);
1192 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1195 cred_t *credp = crref();
1196 const char *name = dp->d_name.name;
1197 struct vcache *tvc = VTOAFS(dp->d_inode);
1199 afs_maybe_lock_kernel();
1201 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1202 && !(tvc->f.states & CUnlinked)) {
1204 code = afs_linux_sillyrename(dip, dp, credp);
1207 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1213 afs_maybe_unlock_kernel();
1215 return afs_convert_code(code);
1220 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1223 cred_t *credp = crref();
1225 const char *name = dp->d_name.name;
1227 /* If afs_symlink returned the vnode, we could instantiate the
1228 * dentry. Since it's not, we drop this one and do a new lookup.
1234 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1237 return afs_convert_code(code);
1241 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1244 cred_t *credp = crref();
1245 struct vcache *tvcp = NULL;
1247 const char *name = dp->d_name.name;
1249 afs_maybe_lock_kernel();
1251 vattr.va_mask = ATTR_MODE;
1252 vattr.va_mode = mode;
1254 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1257 struct inode *ip = AFSTOV(tvcp);
1259 afs_getattr(tvcp, &vattr, credp);
1260 afs_fill_inode(ip, &vattr);
1262 dp->d_op = &afs_dentry_operations;
1263 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1264 d_instantiate(dp, ip);
1268 afs_maybe_unlock_kernel();
1270 return afs_convert_code(code);
1274 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1277 cred_t *credp = crref();
1278 const char *name = dp->d_name.name;
1280 /* locking kernel conflicts with glock? */
1283 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1286 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1287 * that failed because a directory is not empty. So, we map
1288 * EEXIST to ENOTEMPTY on linux.
1290 if (code == EEXIST) {
1299 return afs_convert_code(code);
1304 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1305 struct inode *newip, struct dentry *newdp)
1308 cred_t *credp = crref();
1309 const char *oldname = olddp->d_name.name;
1310 const char *newname = newdp->d_name.name;
1311 struct dentry *rehash = NULL;
1313 /* Prevent any new references during rename operation. */
1314 afs_maybe_lock_kernel();
1316 if (!d_unhashed(newdp)) {
1321 if (atomic_read(&olddp->d_count) > 1)
1322 shrink_dcache_parent(olddp);
1325 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1329 olddp->d_time = 0; /* force to revalidate */
1334 afs_maybe_unlock_kernel();
1337 return afs_convert_code(code);
1341 /* afs_linux_ireadlink
1342 * Internal readlink which can return link contents to user or kernel space.
1343 * Note that the buffer is NOT supposed to be null-terminated.
1346 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1349 cred_t *credp = crref();
1353 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1354 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1358 return maxlen - tuio.uio_resid;
1360 return afs_convert_code(code);
1363 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1364 /* afs_linux_readlink
1365 * Fill target (which is in user space) with contents of symlink.
1368 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1371 struct inode *ip = dp->d_inode;
1374 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1380 /* afs_linux_follow_link
1381 * a file system dependent link following routine.
1383 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1388 name = osi_Alloc(PATH_MAX);
1394 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1402 code = vfs_follow_link(nd, name);
1405 osi_Free(name, PATH_MAX);
1410 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1412 #if defined(AFS_CACHE_BYPASS)
1413 #endif /* defined(AFS_CACHE_BYPASS */
1415 /* Populate a page by filling it from the cache file pointed at by cachefp
1416 * (which contains indicated chunk)
1417 * If task is NULL, the page copy occurs syncronously, and the routine
1418 * returns with page still locked. If task is non-NULL, then page copies
1419 * may occur in the background, and the page will be unlocked when it is
1423 afs_linux_read_cache(struct file *cachefp, struct page *page,
1424 int chunk, struct pagevec *lrupv,
1425 struct afs_pagecopy_task *task) {
1426 loff_t offset = page_offset(page);
1427 struct page *newpage, *cachepage;
1428 struct address_space *cachemapping;
1432 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1436 /* From our offset, we now need to work out which page in the disk
1437 * file it corresponds to. This will be fun ... */
1438 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1440 while (cachepage == NULL) {
1441 cachepage = find_get_page(cachemapping, pageindex);
1444 newpage = page_cache_alloc_cold(cachemapping);
1450 code = add_to_page_cache(newpage, cachemapping,
1451 pageindex, GFP_KERNEL);
1453 cachepage = newpage;
1456 page_cache_get(cachepage);
1457 if (!pagevec_add(lrupv, cachepage))
1458 __pagevec_lru_add_file(lrupv);
1461 page_cache_release(newpage);
1463 if (code != -EEXIST)
1467 lock_page(cachepage);
1471 if (!PageUptodate(cachepage)) {
1472 ClearPageError(cachepage);
1473 code = cachemapping->a_ops->readpage(NULL, cachepage);
1474 if (!code && !task) {
1475 wait_on_page_locked(cachepage);
1478 unlock_page(cachepage);
1482 if (PageUptodate(cachepage)) {
1483 copy_highpage(page, cachepage);
1484 flush_dcache_page(page);
1485 SetPageUptodate(page);
1490 afs_pagecopy_queue_page(task, cachepage, page);
1502 page_cache_release(cachepage);
1508 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1510 loff_t offset = page_offset(pp);
1511 struct inode *ip = FILE_INODE(fp);
1512 struct vcache *avc = VTOAFS(ip);
1514 struct file *cacheFp = NULL;
1517 struct pagevec lrupv;
1519 /* Not a UFS cache, don't do anything */
1520 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1523 /* Can't do anything if the vcache isn't statd , or if the read
1524 * crosses a chunk boundary.
1526 if (!(avc->f.states & CStatd) ||
1527 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1531 ObtainWriteLock(&avc->lock, 911);
1533 /* XXX - See if hinting actually makes things faster !!! */
1535 /* See if we have a suitable entry already cached */
1539 /* We need to lock xdcache, then dcache, to handle situations where
1540 * the hint is on the free list. However, we can't safely do this
1541 * according to the locking hierarchy. So, use a non blocking lock.
1543 ObtainReadLock(&afs_xdcache);
1544 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1546 if (dcLocked && (tdc->index != NULLIDX)
1547 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1548 && tdc->f.chunk == AFS_CHUNK(offset)
1549 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1550 /* Bonus - the hint was correct */
1553 /* Only destroy the hint if its actually invalid, not if there's
1554 * just been a locking failure */
1556 ReleaseReadLock(&tdc->lock);
1563 ReleaseReadLock(&afs_xdcache);
1566 /* No hint, or hint is no longer valid - see if we can get something
1567 * directly from the dcache
1570 tdc = afs_FindDCache(avc, offset);
1573 ReleaseWriteLock(&avc->lock);
1578 ObtainReadLock(&tdc->lock);
1580 /* Is the dcache we've been given currently up to date */
1581 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1582 (tdc->dflags & DFFetching)) {
1583 ReleaseWriteLock(&avc->lock);
1584 ReleaseReadLock(&tdc->lock);
1589 /* Update our hint for future abuse */
1592 /* Okay, so we've now got a cache file that is up to date */
1594 /* XXX - I suspect we should be locking the inodes before we use them! */
1596 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1597 pagevec_init(&lrupv, 0);
1599 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1601 if (pagevec_count(&lrupv))
1602 __pagevec_lru_add_file(&lrupv);
1604 filp_close(cacheFp, NULL);
1607 ReleaseReadLock(&tdc->lock);
1608 ReleaseWriteLock(&avc->lock);
1615 /* afs_linux_readpage
1617 * This function is split into two, because prepare_write/begin_write
1618 * require a readpage call which doesn't unlock the resulting page upon
1622 afs_linux_fillpage(struct file *fp, struct page *pp)
1627 struct iovec *iovecp;
1628 struct inode *ip = FILE_INODE(fp);
1629 afs_int32 cnt = page_count(pp);
1630 struct vcache *avc = VTOAFS(ip);
1631 afs_offs_t offset = page_offset(pp);
1635 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1645 auio = osi_Alloc(sizeof(uio_t));
1646 iovecp = osi_Alloc(sizeof(struct iovec));
1648 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1651 afs_maybe_lock_kernel();
1654 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1655 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1656 99999); /* not a possible code value */
1658 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1660 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1661 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1663 AFS_DISCON_UNLOCK();
1665 afs_maybe_unlock_kernel();
1667 /* XXX valid for no-cache also? Check last bits of files... :)
1668 * Cognate code goes in afs_NoCacheFetchProc. */
1669 if (auio->uio_resid) /* zero remainder of page */
1670 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1673 flush_dcache_page(pp);
1674 SetPageUptodate(pp);
1679 osi_Free(auio, sizeof(uio_t));
1680 osi_Free(iovecp, sizeof(struct iovec));
1683 return afs_convert_code(code);
1687 afs_linux_prefetch(struct file *fp, struct page *pp)
1690 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1691 afs_offs_t offset = page_offset(pp);
1693 if (AFS_CHUNKOFFSET(offset) == 0) {
1695 struct vrequest treq;
1700 code = afs_InitReq(&treq, credp);
1701 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1702 tdc = afs_FindDCache(avc, offset);
1704 if (!(tdc->mflags & DFNextStarted))
1705 afs_PrefetchChunk(avc, tdc, credp, &treq);
1708 ReleaseWriteLock(&avc->lock);
1713 return afs_convert_code(code);
1717 #if defined(AFS_CACHE_BYPASS)
1720 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1721 struct list_head *page_list, unsigned num_pages)
1726 struct iovec* iovecp;
1727 struct nocache_read_request *ancr;
1729 struct pagevec lrupv;
1733 struct inode *ip = FILE_INODE(fp);
1734 struct vcache *avc = VTOAFS(ip);
1735 afs_int32 base_index = 0;
1736 afs_int32 page_count = 0;
1739 /* background thread must free: iovecp, auio, ancr */
1740 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1742 auio = osi_Alloc(sizeof(uio_t));
1743 auio->uio_iov = iovecp;
1744 auio->uio_iovcnt = num_pages;
1745 auio->uio_flag = UIO_READ;
1746 auio->uio_seg = AFS_UIOSYS;
1747 auio->uio_resid = num_pages * PAGE_SIZE;
1749 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1751 ancr->offset = auio->uio_offset;
1752 ancr->length = auio->uio_resid;
1754 pagevec_init(&lrupv, 0);
1756 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1758 if(list_empty(page_list))
1761 pp = list_entry(page_list->prev, struct page, lru);
1762 /* If we allocate a page and don't remove it from page_list,
1763 * the page cache gets upset. */
1765 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1766 if(pp->index > isize) {
1773 offset = page_offset(pp);
1774 auio->uio_offset = offset;
1775 base_index = pp->index;
1777 iovecp[page_ix].iov_len = PAGE_SIZE;
1778 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1779 if(base_index != pp->index) {
1782 page_cache_release(pp);
1783 iovecp[page_ix].iov_base = (void *) 0;
1791 page_cache_release(pp);
1792 iovecp[page_ix].iov_base = (void *) 0;
1795 if(!PageLocked(pp)) {
1799 /* save the page for background map */
1800 iovecp[page_ix].iov_base = (void*) pp;
1802 /* and put it on the LRU cache */
1803 if (!pagevec_add(&lrupv, pp))
1804 __pagevec_lru_add(&lrupv);
1808 /* If there were useful pages in the page list, make sure all pages
1809 * are in the LRU cache, then schedule the read */
1811 pagevec_lru_add(&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;
1840 /* receiver frees */
1841 auio = osi_Alloc(sizeof(uio_t));
1842 iovecp = osi_Alloc(sizeof(struct iovec));
1844 /* address can be NULL, because we overwrite it with 'pp', below */
1845 setup_uio(auio, iovecp, NULL, page_offset(pp),
1846 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1848 /* save the page for background map */
1849 /* XXX - Shouldn't we get a reference count here? */
1850 auio->uio_iov->iov_base = (void*) pp;
1851 /* the background thread will free this */
1852 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1854 ancr->offset = page_offset(pp);
1855 ancr->length = PAGE_SIZE;
1858 afs_maybe_lock_kernel();
1859 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1860 afs_maybe_unlock_kernel();
1863 return afs_convert_code(code);
1867 afs_linux_can_bypass(struct inode *ip) {
1868 switch(cache_bypass_strategy) {
1869 case NEVER_BYPASS_CACHE:
1871 case ALWAYS_BYPASS_CACHE:
1873 case LARGE_FILES_BYPASS_CACHE:
1874 if(i_size_read(ip) > cache_bypass_threshold)
1881 /* Check if a file is permitted to bypass the cache by policy, and modify
1882 * the cache bypass state recorded for that file */
1885 afs_linux_bypass_check(struct inode *ip) {
1888 int bypass = afs_linux_can_bypass(ip);
1891 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1899 afs_linux_bypass_check(struct inode *ip) {
1903 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1907 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1908 struct list_head *page_list, unsigned int num_pages) {
1914 afs_linux_readpage(struct file *fp, struct page *pp)
1918 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1919 code = afs_linux_bypass_readpage(fp, pp);
1921 code = afs_linux_fillpage(fp, pp);
1923 code = afs_linux_prefetch(fp, pp);
1930 /* Readpages reads a number of pages for a particular file. We use
1931 * this to optimise the reading, by limiting the number of times upon which
1932 * we have to lookup, lock and open vcaches and dcaches
1936 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1937 struct list_head *page_list, unsigned int num_pages)
1939 struct inode *inode = mapping->host;
1940 struct vcache *avc = VTOAFS(inode);
1942 struct file *cacheFp = NULL;
1944 unsigned int page_idx;
1946 struct pagevec lrupv;
1947 struct afs_pagecopy_task *task;
1949 if (afs_linux_bypass_check(inode))
1950 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1952 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
1956 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1961 ObtainWriteLock(&avc->lock, 912);
1964 task = afs_pagecopy_init_task();
1967 pagevec_init(&lrupv, 0);
1968 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1969 struct page *page = list_entry(page_list->prev, struct page, lru);
1970 list_del(&page->lru);
1971 offset = page_offset(page);
1973 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1975 ReleaseReadLock(&tdc->lock);
1980 filp_close(cacheFp, NULL);
1985 if ((tdc = afs_FindDCache(avc, offset))) {
1986 ObtainReadLock(&tdc->lock);
1987 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1988 (tdc->dflags & DFFetching)) {
1989 ReleaseReadLock(&tdc->lock);
1996 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1999 if (tdc && !add_to_page_cache(page, mapping, page->index,
2001 page_cache_get(page);
2002 if (!pagevec_add(&lrupv, page))
2003 __pagevec_lru_add_file(&lrupv);
2005 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2007 page_cache_release(page);
2009 if (pagevec_count(&lrupv))
2010 __pagevec_lru_add_file(&lrupv);
2013 filp_close(cacheFp, NULL);
2015 afs_pagecopy_put_task(task);
2019 ReleaseReadLock(&tdc->lock);
2023 ReleaseWriteLock(&avc->lock);
2028 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2031 afs_linux_prepare_writeback(struct vcache *avc) {
2032 if (avc->f.states & CPageWrite) {
2033 return AOP_WRITEPAGE_ACTIVATE;
2035 avc->f.states |= CPageWrite;
2040 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2041 struct vrequest treq;
2044 if (!afs_InitReq(&treq, credp))
2045 code = afs_DoPartialWrite(avc, &treq);
2047 return afs_convert_code(code);
2051 afs_linux_complete_writeback(struct vcache *avc) {
2052 avc->f.states &= ~CPageWrite;
2055 /* Writeback a given page syncronously. Called with no AFS locks held */
2057 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2058 unsigned long offset, unsigned int count,
2061 struct vcache *vcp = VTOAFS(ip);
2069 buffer = kmap(pp) + offset;
2070 base = page_offset(pp) + offset;
2072 afs_maybe_lock_kernel();
2074 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2075 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2076 ICL_TYPE_INT32, 99999);
2078 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2080 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2082 i_size_write(ip, vcp->f.m.Length);
2083 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2085 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2087 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2088 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2089 ICL_TYPE_INT32, code);
2092 afs_maybe_unlock_kernel();
2099 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2100 unsigned long offset, unsigned int count)
2104 struct vcache *vcp = VTOAFS(ip);
2107 /* Catch recursive writeback. This occurs if the kernel decides
2108 * writeback is required whilst we are writing to the cache, or
2109 * flushing to the server. When we're running syncronously (as
2110 * opposed to from writepage) we can't actually do anything about
2111 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2114 ObtainWriteLock(&vcp->lock, 532);
2115 afs_linux_prepare_writeback(vcp);
2116 ReleaseWriteLock(&vcp->lock);
2120 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2122 afs_maybe_lock_kernel();
2124 ObtainWriteLock(&vcp->lock, 533);
2126 code1 = afs_linux_dopartialwrite(vcp, credp);
2127 afs_linux_complete_writeback(vcp);
2128 ReleaseWriteLock(&vcp->lock);
2130 afs_maybe_unlock_kernel();
2140 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2141 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2143 afs_linux_writepage(struct page *pp)
2146 struct address_space *mapping = pp->mapping;
2147 struct inode *inode;
2150 unsigned int to = PAGE_CACHE_SIZE;
2155 if (PageReclaim(pp)) {
2156 return AOP_WRITEPAGE_ACTIVATE;
2157 /* XXX - Do we need to redirty the page here? */
2162 inode = mapping->host;
2163 vcp = VTOAFS(inode);
2164 isize = i_size_read(inode);
2166 /* Don't defeat an earlier truncate */
2167 if (page_offset(pp) > isize) {
2168 set_page_writeback(pp);
2174 ObtainWriteLock(&vcp->lock, 537);
2175 code = afs_linux_prepare_writeback(vcp);
2176 if (code == AOP_WRITEPAGE_ACTIVATE) {
2177 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2178 * to return with the page still locked */
2179 ReleaseWriteLock(&vcp->lock);
2184 /* Grab the creds structure currently held in the vnode, and
2185 * get a reference to it, in case it goes away ... */
2191 ReleaseWriteLock(&vcp->lock);
2194 set_page_writeback(pp);
2196 SetPageUptodate(pp);
2198 /* We can unlock the page here, because it's protected by the
2199 * page_writeback flag. This should make us less vulnerable to
2200 * deadlocking in afs_write and afs_DoPartialWrite
2204 /* If this is the final page, then just write the number of bytes that
2205 * are actually in it */
2206 if ((isize - page_offset(pp)) < to )
2207 to = isize - page_offset(pp);
2209 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2211 afs_maybe_lock_kernel();
2213 ObtainWriteLock(&vcp->lock, 538);
2215 /* As much as we might like to ignore a file server error here,
2216 * and just try again when we close(), unfortunately StoreAllSegments
2217 * will invalidate our chunks if the server returns a permanent error,
2218 * so we need to at least try and get that error back to the user
2221 code1 = afs_linux_dopartialwrite(vcp, credp);
2223 afs_linux_complete_writeback(vcp);
2224 ReleaseWriteLock(&vcp->lock);
2227 afs_maybe_unlock_kernel();
2230 end_page_writeback(pp);
2231 page_cache_release(pp);
2242 /* afs_linux_permission
2243 * Check access rights - returns error if can't check or permission denied.
2246 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2247 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2249 afs_linux_permission(struct inode *ip, int mode)
2253 cred_t *credp = crref();
2257 if (mode & MAY_EXEC)
2259 if (mode & MAY_READ)
2261 if (mode & MAY_WRITE)
2263 code = afs_access(VTOAFS(ip), tmp, credp);
2267 return afs_convert_code(code);
2271 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2275 struct inode *inode = FILE_INODE(file);
2276 loff_t pagebase = page_offset(page);
2278 if (i_size_read(inode) < (pagebase + offset))
2279 i_size_write(inode, pagebase + offset);
2281 if (PageChecked(page)) {
2282 SetPageUptodate(page);
2283 ClearPageChecked(page);
2286 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2292 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2296 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2297 * prepare_write. Essentially, if the page exists within the file,
2298 * and is not being fully written, then we should populate it.
2301 if (!PageUptodate(page)) {
2302 loff_t pagebase = page_offset(page);
2303 loff_t isize = i_size_read(page->mapping->host);
2305 /* Is the location we are writing to beyond the end of the file? */
2306 if (pagebase >= isize ||
2307 ((from == 0) && (pagebase + to) >= isize)) {
2308 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2309 SetPageChecked(page);
2310 /* Are we we writing a full page */
2311 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2312 SetPageChecked(page);
2313 /* Is the page readable, if it's wronly, we don't care, because we're
2314 * not actually going to read from it ... */
2315 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2316 /* We don't care if fillpage fails, because if it does the page
2317 * won't be marked as up to date
2319 afs_linux_fillpage(file, page);
2325 #if defined(HAVE_WRITE_BEGIN)
2327 afs_linux_write_end(struct file *file, struct address_space *mapping,
2328 loff_t pos, unsigned len, unsigned copied,
2329 struct page *page, void *fsdata)
2332 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2334 code = afs_linux_commit_write(file, page, from, from + len);
2337 page_cache_release(page);
2342 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2343 loff_t pos, unsigned len, unsigned flags,
2344 struct page **pagep, void **fsdata)
2347 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2348 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2351 page = grab_cache_page_write_begin(mapping, index, flags);
2354 code = afs_linux_prepare_write(file, page, from, from + len);
2357 page_cache_release(page);
2365 static struct inode_operations afs_file_iops = {
2366 .permission = afs_linux_permission,
2367 .getattr = afs_linux_getattr,
2368 .setattr = afs_notify_change,
2371 static struct address_space_operations afs_file_aops = {
2372 .readpage = afs_linux_readpage,
2373 .readpages = afs_linux_readpages,
2374 .writepage = afs_linux_writepage,
2375 #if defined (HAVE_WRITE_BEGIN)
2376 .write_begin = afs_linux_write_begin,
2377 .write_end = afs_linux_write_end,
2379 .commit_write = afs_linux_commit_write,
2380 .prepare_write = afs_linux_prepare_write,
2385 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2386 * by what sort of operation is allowed.....
2389 static struct inode_operations afs_dir_iops = {
2390 .setattr = afs_notify_change,
2391 .create = afs_linux_create,
2392 .lookup = afs_linux_lookup,
2393 .link = afs_linux_link,
2394 .unlink = afs_linux_unlink,
2395 .symlink = afs_linux_symlink,
2396 .mkdir = afs_linux_mkdir,
2397 .rmdir = afs_linux_rmdir,
2398 .rename = afs_linux_rename,
2399 .getattr = afs_linux_getattr,
2400 .permission = afs_linux_permission,
2403 /* We really need a separate symlink set of ops, since do_follow_link()
2404 * determines if it _is_ a link by checking if the follow_link op is set.
2406 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2408 afs_symlink_filler(struct file *file, struct page *page)
2410 struct inode *ip = (struct inode *)page->mapping->host;
2411 char *p = (char *)kmap(page);
2414 afs_maybe_lock_kernel();
2416 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2421 p[code] = '\0'; /* null terminate? */
2422 afs_maybe_unlock_kernel();
2424 SetPageUptodate(page);
2430 afs_maybe_unlock_kernel();
2438 static struct address_space_operations afs_symlink_aops = {
2439 .readpage = afs_symlink_filler
2441 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2443 static struct inode_operations afs_symlink_iops = {
2444 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2445 .readlink = page_readlink,
2446 # if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2447 .follow_link = page_follow_link,
2449 .follow_link = page_follow_link_light,
2450 .put_link = page_put_link,
2452 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2453 .readlink = afs_linux_readlink,
2454 .follow_link = afs_linux_follow_link,
2455 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2456 .setattr = afs_notify_change,
2460 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2464 vattr2inode(ip, vattr);
2466 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2467 /* Reset ops if symlink or directory. */
2468 if (S_ISREG(ip->i_mode)) {
2469 ip->i_op = &afs_file_iops;
2470 ip->i_fop = &afs_file_fops;
2471 ip->i_data.a_ops = &afs_file_aops;
2473 } else if (S_ISDIR(ip->i_mode)) {
2474 ip->i_op = &afs_dir_iops;
2475 ip->i_fop = &afs_dir_fops;
2477 } else if (S_ISLNK(ip->i_mode)) {
2478 ip->i_op = &afs_symlink_iops;
2479 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2480 ip->i_data.a_ops = &afs_symlink_aops;
2481 ip->i_mapping = &ip->i_data;