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;
834 struct afs_fakestat_state fakestate;
836 afs_maybe_lock_kernel();
838 afs_InitFakeStat(&fakestate);
842 vcp = VTOAFS(dp->d_inode);
843 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
845 if (vcp == afs_globalVp)
848 if (vcp->mvstat == 1) { /* mount point */
849 if (vcp->mvid && (vcp->f.states & CMValid)) {
852 struct vrequest treq;
855 code = afs_InitReq(&treq, credp);
857 (strcmp(dp->d_name.name, ".directory") == 0)) {
861 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
863 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
864 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
865 /* a mount point, not yet replaced by its directory */
870 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
871 check_bad_parent(dp); /* check and correct mvid */
874 /* If the last looker changes, we should make sure the current
875 * looker still has permission to examine this file. This would
876 * always require a crref() which would be "slow".
878 if (vcp->last_looker != treq.uid) {
879 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
882 vcp->last_looker = treq.uid;
886 /* If the parent's DataVersion has changed or the vnode
887 * is longer valid, we need to do a full lookup. VerifyVCache
888 * isn't enough since the vnode may have been renamed.
891 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
894 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
895 if (!tvc || tvc != vcp)
898 if (afs_getattr(vcp, &vattr, credp))
901 vattr2inode(AFSTOV(vcp), &vattr);
902 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
905 /* should we always update the attributes at this point? */
906 /* unlikely--the vcache entry hasn't changed */
910 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
911 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
915 /* No change in parent's DataVersion so this negative
916 * lookup is still valid. BUT, if a server is down a
917 * negative lookup can result so there should be a
918 * liftime as well. For now, always expire.
931 afs_PutFakeStat(&fakestate);
937 shrink_dcache_parent(dp);
940 afs_maybe_unlock_kernel();
944 if (have_submounts(dp))
952 afs_dentry_iput(struct dentry *dp, struct inode *ip)
954 struct vcache *vcp = VTOAFS(ip);
957 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
958 (void) afs_InactiveVCache(vcp, NULL);
961 afs_linux_clear_nfsfs_renamed(dp);
967 afs_dentry_delete(struct dentry *dp)
969 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
970 return 1; /* bad inode? */
975 struct dentry_operations afs_dentry_operations = {
976 .d_revalidate = afs_linux_dentry_revalidate,
977 .d_delete = afs_dentry_delete,
978 .d_iput = afs_dentry_iput,
981 /**********************************************************************
982 * AFS Linux inode operations
983 **********************************************************************/
987 * Merely need to set enough of vattr to get us through the create. Note
988 * that the higher level code (open_namei) will take care of any tuncation
989 * explicitly. Exclusive open is also taken care of in open_namei.
991 * name is in kernel space at this point.
994 #ifdef IOP_CREATE_TAKES_NAMEIDATA
995 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
996 struct nameidata *nd)
998 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1002 cred_t *credp = crref();
1003 const char *name = dp->d_name.name;
1008 vattr.va_mode = mode;
1009 vattr.va_type = mode & S_IFMT;
1011 afs_maybe_lock_kernel();
1013 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1017 struct inode *ip = AFSTOV(vcp);
1019 afs_getattr(vcp, &vattr, credp);
1020 afs_fill_inode(ip, &vattr);
1021 insert_inode_hash(ip);
1022 dp->d_op = &afs_dentry_operations;
1023 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1024 d_instantiate(dp, ip);
1028 afs_maybe_unlock_kernel();
1030 return afs_convert_code(code);
1033 /* afs_linux_lookup */
1034 static struct dentry *
1035 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1036 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1037 struct nameidata *nd)
1039 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1042 cred_t *credp = crref();
1043 struct vcache *vcp = NULL;
1044 const char *comp = dp->d_name.name;
1045 struct inode *ip = NULL;
1046 struct dentry *newdp = NULL;
1049 afs_maybe_lock_kernel();
1051 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1057 afs_getattr(vcp, &vattr, credp);
1058 afs_fill_inode(ip, &vattr);
1059 if (hlist_unhashed(&ip->i_hash))
1060 insert_inode_hash(ip);
1062 dp->d_op = &afs_dentry_operations;
1063 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1066 if (ip && S_ISDIR(ip->i_mode)) {
1067 struct dentry *alias;
1069 /* Try to invalidate an existing alias in favor of our new one */
1070 alias = d_find_alias(ip);
1071 /* But not if it's disconnected; then we want d_splice_alias below */
1072 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1073 if (d_invalidate(alias) == 0) {
1077 afs_maybe_unlock_kernel();
1083 newdp = d_splice_alias(ip, dp);
1085 afs_maybe_unlock_kernel();
1088 /* It's ok for the file to not be found. That's noted by the caller by
1089 * seeing that the dp->d_inode field is NULL.
1091 if (!code || code == ENOENT)
1094 return ERR_PTR(afs_convert_code(code));
1098 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1101 cred_t *credp = crref();
1102 const char *name = newdp->d_name.name;
1103 struct inode *oldip = olddp->d_inode;
1105 /* If afs_link returned the vnode, we could instantiate the
1106 * dentry. Since it's not, we drop this one and do a new lookup.
1111 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1115 return afs_convert_code(code);
1118 /* We have to have a Linux specific sillyrename function, because we
1119 * also have to keep the dcache up to date when we're doing a silly
1120 * rename - so we don't want the generic vnodeops doing this behind our
1125 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1128 struct vcache *tvc = VTOAFS(dentry->d_inode);
1129 struct dentry *__dp = NULL;
1130 char *__name = NULL;
1133 if (afs_linux_nfsfs_renamed(dentry))
1141 osi_FreeSmallSpace(__name);
1142 __name = afs_newname();
1145 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1148 osi_FreeSmallSpace(__name);
1151 } while (__dp->d_inode != NULL);
1154 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1155 VTOAFS(dir), (char *)__dp->d_name.name,
1158 tvc->mvid = (void *) __name;
1161 crfree(tvc->uncred);
1163 tvc->uncred = credp;
1164 tvc->f.states |= CUnlinked;
1165 afs_linux_set_nfsfs_renamed(dentry);
1167 osi_FreeSmallSpace(__name);
1172 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1173 d_move(dentry, __dp);
1182 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1185 cred_t *credp = crref();
1186 const char *name = dp->d_name.name;
1187 struct vcache *tvc = VTOAFS(dp->d_inode);
1189 afs_maybe_lock_kernel();
1191 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1192 && !(tvc->f.states & CUnlinked)) {
1194 code = afs_linux_sillyrename(dip, dp, credp);
1197 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1203 afs_maybe_unlock_kernel();
1205 return afs_convert_code(code);
1210 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1213 cred_t *credp = crref();
1215 const char *name = dp->d_name.name;
1217 /* If afs_symlink returned the vnode, we could instantiate the
1218 * dentry. Since it's not, we drop this one and do a new lookup.
1224 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1227 return afs_convert_code(code);
1231 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1234 cred_t *credp = crref();
1235 struct vcache *tvcp = NULL;
1237 const char *name = dp->d_name.name;
1239 afs_maybe_lock_kernel();
1241 vattr.va_mask = ATTR_MODE;
1242 vattr.va_mode = mode;
1244 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1247 struct inode *ip = AFSTOV(tvcp);
1249 afs_getattr(tvcp, &vattr, credp);
1250 afs_fill_inode(ip, &vattr);
1252 dp->d_op = &afs_dentry_operations;
1253 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1254 d_instantiate(dp, ip);
1258 afs_maybe_unlock_kernel();
1260 return afs_convert_code(code);
1264 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1267 cred_t *credp = crref();
1268 const char *name = dp->d_name.name;
1270 /* locking kernel conflicts with glock? */
1273 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1276 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1277 * that failed because a directory is not empty. So, we map
1278 * EEXIST to ENOTEMPTY on linux.
1280 if (code == EEXIST) {
1289 return afs_convert_code(code);
1294 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1295 struct inode *newip, struct dentry *newdp)
1298 cred_t *credp = crref();
1299 const char *oldname = olddp->d_name.name;
1300 const char *newname = newdp->d_name.name;
1301 struct dentry *rehash = NULL;
1303 /* Prevent any new references during rename operation. */
1304 afs_maybe_lock_kernel();
1306 if (!d_unhashed(newdp)) {
1311 if (atomic_read(&olddp->d_count) > 1)
1312 shrink_dcache_parent(olddp);
1315 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1319 olddp->d_time = 0; /* force to revalidate */
1324 afs_maybe_unlock_kernel();
1327 return afs_convert_code(code);
1331 /* afs_linux_ireadlink
1332 * Internal readlink which can return link contents to user or kernel space.
1333 * Note that the buffer is NOT supposed to be null-terminated.
1336 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1339 cred_t *credp = crref();
1343 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1344 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1348 return maxlen - tuio.uio_resid;
1350 return afs_convert_code(code);
1353 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1354 /* afs_linux_readlink
1355 * Fill target (which is in user space) with contents of symlink.
1358 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1361 struct inode *ip = dp->d_inode;
1364 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1370 /* afs_linux_follow_link
1371 * a file system dependent link following routine.
1373 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1378 name = osi_Alloc(PATH_MAX);
1384 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1392 code = vfs_follow_link(nd, name);
1395 osi_Free(name, PATH_MAX);
1400 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1402 #if defined(AFS_CACHE_BYPASS)
1403 #endif /* defined(AFS_CACHE_BYPASS */
1405 /* Populate a page by filling it from the cache file pointed at by cachefp
1406 * (which contains indicated chunk)
1407 * If task is NULL, the page copy occurs syncronously, and the routine
1408 * returns with page still locked. If task is non-NULL, then page copies
1409 * may occur in the background, and the page will be unlocked when it is
1413 afs_linux_read_cache(struct file *cachefp, struct page *page,
1414 int chunk, struct pagevec *lrupv,
1415 struct afs_pagecopy_task *task) {
1416 loff_t offset = page_offset(page);
1417 struct page *newpage, *cachepage;
1418 struct address_space *cachemapping;
1422 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1426 /* From our offset, we now need to work out which page in the disk
1427 * file it corresponds to. This will be fun ... */
1428 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1430 while (cachepage == NULL) {
1431 cachepage = find_get_page(cachemapping, pageindex);
1434 newpage = page_cache_alloc_cold(cachemapping);
1440 code = add_to_page_cache(newpage, cachemapping,
1441 pageindex, GFP_KERNEL);
1443 cachepage = newpage;
1446 page_cache_get(cachepage);
1447 if (!pagevec_add(lrupv, cachepage))
1448 __pagevec_lru_add_file(lrupv);
1451 page_cache_release(newpage);
1453 if (code != -EEXIST)
1457 lock_page(cachepage);
1461 if (!PageUptodate(cachepage)) {
1462 ClearPageError(cachepage);
1463 code = cachemapping->a_ops->readpage(NULL, cachepage);
1464 if (!code && !task) {
1465 wait_on_page_locked(cachepage);
1468 unlock_page(cachepage);
1472 if (PageUptodate(cachepage)) {
1473 copy_highpage(page, cachepage);
1474 flush_dcache_page(page);
1475 SetPageUptodate(page);
1480 afs_pagecopy_queue_page(task, cachepage, page);
1492 page_cache_release(cachepage);
1498 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1500 loff_t offset = page_offset(pp);
1501 struct inode *ip = FILE_INODE(fp);
1502 struct vcache *avc = VTOAFS(ip);
1504 struct file *cacheFp = NULL;
1507 struct pagevec lrupv;
1509 /* Not a UFS cache, don't do anything */
1510 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1513 /* Can't do anything if the vcache isn't statd , or if the read
1514 * crosses a chunk boundary.
1516 if (!(avc->f.states & CStatd) ||
1517 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1521 ObtainWriteLock(&avc->lock, 911);
1523 /* XXX - See if hinting actually makes things faster !!! */
1525 /* See if we have a suitable entry already cached */
1529 /* We need to lock xdcache, then dcache, to handle situations where
1530 * the hint is on the free list. However, we can't safely do this
1531 * according to the locking hierarchy. So, use a non blocking lock.
1533 ObtainReadLock(&afs_xdcache);
1534 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1536 if (dcLocked && (tdc->index != NULLIDX)
1537 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1538 && tdc->f.chunk == AFS_CHUNK(offset)
1539 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1540 /* Bonus - the hint was correct */
1543 /* Only destroy the hint if its actually invalid, not if there's
1544 * just been a locking failure */
1546 ReleaseReadLock(&tdc->lock);
1553 ReleaseReadLock(&afs_xdcache);
1556 /* No hint, or hint is no longer valid - see if we can get something
1557 * directly from the dcache
1560 tdc = afs_FindDCache(avc, offset);
1563 ReleaseWriteLock(&avc->lock);
1568 ObtainReadLock(&tdc->lock);
1570 /* Is the dcache we've been given currently up to date */
1571 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1572 (tdc->dflags & DFFetching)) {
1573 ReleaseWriteLock(&avc->lock);
1574 ReleaseReadLock(&tdc->lock);
1579 /* Update our hint for future abuse */
1582 /* Okay, so we've now got a cache file that is up to date */
1584 /* XXX - I suspect we should be locking the inodes before we use them! */
1586 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1587 pagevec_init(&lrupv, 0);
1589 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1591 if (pagevec_count(&lrupv))
1592 __pagevec_lru_add_file(&lrupv);
1594 filp_close(cacheFp, NULL);
1597 ReleaseReadLock(&tdc->lock);
1598 ReleaseWriteLock(&avc->lock);
1605 /* afs_linux_readpage
1607 * This function is split into two, because prepare_write/begin_write
1608 * require a readpage call which doesn't unlock the resulting page upon
1612 afs_linux_fillpage(struct file *fp, struct page *pp)
1617 struct iovec *iovecp;
1618 struct inode *ip = FILE_INODE(fp);
1619 afs_int32 cnt = page_count(pp);
1620 struct vcache *avc = VTOAFS(ip);
1621 afs_offs_t offset = page_offset(pp);
1625 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1635 auio = osi_Alloc(sizeof(uio_t));
1636 iovecp = osi_Alloc(sizeof(struct iovec));
1638 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1641 afs_maybe_lock_kernel();
1644 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1645 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1646 99999); /* not a possible code value */
1648 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1650 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1651 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1653 AFS_DISCON_UNLOCK();
1655 afs_maybe_unlock_kernel();
1657 /* XXX valid for no-cache also? Check last bits of files... :)
1658 * Cognate code goes in afs_NoCacheFetchProc. */
1659 if (auio->uio_resid) /* zero remainder of page */
1660 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1663 flush_dcache_page(pp);
1664 SetPageUptodate(pp);
1669 osi_Free(auio, sizeof(uio_t));
1670 osi_Free(iovecp, sizeof(struct iovec));
1673 return afs_convert_code(code);
1677 afs_linux_prefetch(struct file *fp, struct page *pp)
1680 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1681 afs_offs_t offset = page_offset(pp);
1683 if (AFS_CHUNKOFFSET(offset) == 0) {
1685 struct vrequest treq;
1690 code = afs_InitReq(&treq, credp);
1691 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1692 tdc = afs_FindDCache(avc, offset);
1694 if (!(tdc->mflags & DFNextStarted))
1695 afs_PrefetchChunk(avc, tdc, credp, &treq);
1698 ReleaseWriteLock(&avc->lock);
1703 return afs_convert_code(code);
1707 #if defined(AFS_CACHE_BYPASS)
1710 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1711 struct list_head *page_list, unsigned num_pages)
1716 struct iovec* iovecp;
1717 struct nocache_read_request *ancr;
1719 struct pagevec lrupv;
1723 struct inode *ip = FILE_INODE(fp);
1724 struct vcache *avc = VTOAFS(ip);
1725 afs_int32 base_index = 0;
1726 afs_int32 page_count = 0;
1729 /* background thread must free: iovecp, auio, ancr */
1730 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1732 auio = osi_Alloc(sizeof(uio_t));
1733 auio->uio_iov = iovecp;
1734 auio->uio_iovcnt = num_pages;
1735 auio->uio_flag = UIO_READ;
1736 auio->uio_seg = AFS_UIOSYS;
1737 auio->uio_resid = num_pages * PAGE_SIZE;
1739 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1741 ancr->offset = auio->uio_offset;
1742 ancr->length = auio->uio_resid;
1744 pagevec_init(&lrupv, 0);
1746 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1748 if(list_empty(page_list))
1751 pp = list_entry(page_list->prev, struct page, lru);
1752 /* If we allocate a page and don't remove it from page_list,
1753 * the page cache gets upset. */
1755 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1756 if(pp->index > isize) {
1763 offset = page_offset(pp);
1764 auio->uio_offset = offset;
1765 base_index = pp->index;
1767 iovecp[page_ix].iov_len = PAGE_SIZE;
1768 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1769 if(base_index != pp->index) {
1772 page_cache_release(pp);
1773 iovecp[page_ix].iov_base = (void *) 0;
1781 page_cache_release(pp);
1782 iovecp[page_ix].iov_base = (void *) 0;
1785 if(!PageLocked(pp)) {
1789 /* save the page for background map */
1790 iovecp[page_ix].iov_base = (void*) pp;
1792 /* and put it on the LRU cache */
1793 if (!pagevec_add(&lrupv, pp))
1794 __pagevec_lru_add(&lrupv);
1798 /* If there were useful pages in the page list, make sure all pages
1799 * are in the LRU cache, then schedule the read */
1801 pagevec_lru_add(&lrupv);
1803 code = afs_ReadNoCache(avc, ancr, credp);
1806 /* If there is nothing for the background thread to handle,
1807 * it won't be freeing the things that we never gave it */
1808 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1809 osi_Free(auio, sizeof(uio_t));
1810 osi_Free(ancr, sizeof(struct nocache_read_request));
1812 /* we do not flush, release, or unmap pages--that will be
1813 * done for us by the background thread as each page comes in
1814 * from the fileserver */
1815 return afs_convert_code(code);
1820 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1822 cred_t *credp = NULL;
1824 struct iovec *iovecp;
1825 struct nocache_read_request *ancr;
1830 /* receiver frees */
1831 auio = osi_Alloc(sizeof(uio_t));
1832 iovecp = osi_Alloc(sizeof(struct iovec));
1834 /* address can be NULL, because we overwrite it with 'pp', below */
1835 setup_uio(auio, iovecp, NULL, page_offset(pp),
1836 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1838 /* save the page for background map */
1839 /* XXX - Shouldn't we get a reference count here? */
1840 auio->uio_iov->iov_base = (void*) pp;
1841 /* the background thread will free this */
1842 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1844 ancr->offset = page_offset(pp);
1845 ancr->length = PAGE_SIZE;
1848 afs_maybe_lock_kernel();
1849 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1850 afs_maybe_unlock_kernel();
1853 return afs_convert_code(code);
1857 afs_linux_can_bypass(struct inode *ip) {
1858 switch(cache_bypass_strategy) {
1859 case NEVER_BYPASS_CACHE:
1861 case ALWAYS_BYPASS_CACHE:
1863 case LARGE_FILES_BYPASS_CACHE:
1864 if(i_size_read(ip) > cache_bypass_threshold)
1871 /* Check if a file is permitted to bypass the cache by policy, and modify
1872 * the cache bypass state recorded for that file */
1875 afs_linux_bypass_check(struct inode *ip) {
1878 int bypass = afs_linux_can_bypass(ip);
1881 trydo_cache_transition(VTOAFS(ip), credp, bypass);
1889 afs_linux_bypass_check(struct inode *ip) {
1893 afs_linux_bypass_readpage(struct file *fp, struct page *pp) {
1897 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1898 struct list_head *page_list, unsigned int num_pages) {
1904 afs_linux_readpage(struct file *fp, struct page *pp)
1908 if (afs_linux_bypass_check(FILE_INODE(fp))) {
1909 code = afs_linux_bypass_readpage(fp, pp);
1911 code = afs_linux_fillpage(fp, pp);
1913 code = afs_linux_prefetch(fp, pp);
1920 /* Readpages reads a number of pages for a particular file. We use
1921 * this to optimise the reading, by limiting the number of times upon which
1922 * we have to lookup, lock and open vcaches and dcaches
1926 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1927 struct list_head *page_list, unsigned int num_pages)
1929 struct inode *inode = mapping->host;
1930 struct vcache *avc = VTOAFS(inode);
1932 struct file *cacheFp = NULL;
1934 unsigned int page_idx;
1936 struct pagevec lrupv;
1937 struct afs_pagecopy_task *task;
1939 if (afs_linux_bypass_check(inode))
1940 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
1943 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
1948 ObtainWriteLock(&avc->lock, 912);
1951 task = afs_pagecopy_init_task();
1954 pagevec_init(&lrupv, 0);
1955 for (page_idx = 0; page_idx < num_pages; page_idx++) {
1956 struct page *page = list_entry(page_list->prev, struct page, lru);
1957 list_del(&page->lru);
1958 offset = page_offset(page);
1960 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
1962 ReleaseReadLock(&tdc->lock);
1967 filp_close(cacheFp, NULL);
1972 if ((tdc = afs_FindDCache(avc, offset))) {
1973 ObtainReadLock(&tdc->lock);
1974 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1975 (tdc->dflags & DFFetching)) {
1976 ReleaseReadLock(&tdc->lock);
1983 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1986 if (tdc && !add_to_page_cache(page, mapping, page->index,
1988 page_cache_get(page);
1989 if (!pagevec_add(&lrupv, page))
1990 __pagevec_lru_add_file(&lrupv);
1992 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
1994 page_cache_release(page);
1996 if (pagevec_count(&lrupv))
1997 __pagevec_lru_add_file(&lrupv);
2000 filp_close(cacheFp, NULL);
2002 afs_pagecopy_put_task(task);
2006 ReleaseReadLock(&tdc->lock);
2010 ReleaseWriteLock(&avc->lock);
2015 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2018 afs_linux_prepare_writeback(struct vcache *avc) {
2019 if (avc->f.states & CPageWrite) {
2020 return AOP_WRITEPAGE_ACTIVATE;
2022 avc->f.states |= CPageWrite;
2027 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2028 struct vrequest treq;
2031 if (!afs_InitReq(&treq, credp))
2032 code = afs_DoPartialWrite(avc, &treq);
2034 return afs_convert_code(code);
2038 afs_linux_complete_writeback(struct vcache *avc) {
2039 avc->f.states &= ~CPageWrite;
2042 /* Writeback a given page syncronously. Called with no AFS locks held */
2044 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2045 unsigned long offset, unsigned int count,
2048 struct vcache *vcp = VTOAFS(ip);
2056 buffer = kmap(pp) + offset;
2057 base = page_offset(pp) + offset;
2059 afs_maybe_lock_kernel();
2061 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2062 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2063 ICL_TYPE_INT32, 99999);
2065 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2067 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2069 i_size_write(ip, vcp->f.m.Length);
2070 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2072 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
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, code);
2079 afs_maybe_unlock_kernel();
2086 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2087 unsigned long offset, unsigned int count)
2091 struct vcache *vcp = VTOAFS(ip);
2094 /* Catch recursive writeback. This occurs if the kernel decides
2095 * writeback is required whilst we are writing to the cache, or
2096 * flushing to the server. When we're running syncronously (as
2097 * opposed to from writepage) we can't actually do anything about
2098 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2101 ObtainWriteLock(&vcp->lock, 532);
2102 afs_linux_prepare_writeback(vcp);
2103 ReleaseWriteLock(&vcp->lock);
2107 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2109 afs_maybe_lock_kernel();
2111 ObtainWriteLock(&vcp->lock, 533);
2113 code1 = afs_linux_dopartialwrite(vcp, credp);
2114 afs_linux_complete_writeback(vcp);
2115 ReleaseWriteLock(&vcp->lock);
2117 afs_maybe_unlock_kernel();
2127 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2128 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2130 afs_linux_writepage(struct page *pp)
2133 struct address_space *mapping = pp->mapping;
2134 struct inode *inode;
2137 unsigned int to = PAGE_CACHE_SIZE;
2142 if (PageReclaim(pp)) {
2143 return AOP_WRITEPAGE_ACTIVATE;
2144 /* XXX - Do we need to redirty the page here? */
2149 inode = mapping->host;
2150 vcp = VTOAFS(inode);
2151 isize = i_size_read(inode);
2153 /* Don't defeat an earlier truncate */
2154 if (page_offset(pp) > isize) {
2155 set_page_writeback(pp);
2161 ObtainWriteLock(&vcp->lock, 537);
2162 code = afs_linux_prepare_writeback(vcp);
2163 if (code == AOP_WRITEPAGE_ACTIVATE) {
2164 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2165 * to return with the page still locked */
2166 ReleaseWriteLock(&vcp->lock);
2171 /* Grab the creds structure currently held in the vnode, and
2172 * get a reference to it, in case it goes away ... */
2175 ReleaseWriteLock(&vcp->lock);
2178 set_page_writeback(pp);
2180 SetPageUptodate(pp);
2182 /* We can unlock the page here, because it's protected by the
2183 * page_writeback flag. This should make us less vulnerable to
2184 * deadlocking in afs_write and afs_DoPartialWrite
2188 /* If this is the final page, then just write the number of bytes that
2189 * are actually in it */
2190 if ((isize - page_offset(pp)) < to )
2191 to = isize - page_offset(pp);
2193 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2195 afs_maybe_lock_kernel();
2197 ObtainWriteLock(&vcp->lock, 538);
2199 /* As much as we might like to ignore a file server error here,
2200 * and just try again when we close(), unfortunately StoreAllSegments
2201 * will invalidate our chunks if the server returns a permanent error,
2202 * so we need to at least try and get that error back to the user
2205 code1 = afs_linux_dopartialwrite(vcp, credp);
2207 afs_linux_complete_writeback(vcp);
2208 ReleaseWriteLock(&vcp->lock);
2211 afs_maybe_unlock_kernel();
2214 end_page_writeback(pp);
2215 page_cache_release(pp);
2226 /* afs_linux_permission
2227 * Check access rights - returns error if can't check or permission denied.
2230 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2231 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2233 afs_linux_permission(struct inode *ip, int mode)
2237 cred_t *credp = crref();
2241 if (mode & MAY_EXEC)
2243 if (mode & MAY_READ)
2245 if (mode & MAY_WRITE)
2247 code = afs_access(VTOAFS(ip), tmp, credp);
2251 return afs_convert_code(code);
2255 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2259 struct inode *inode = FILE_INODE(file);
2260 loff_t pagebase = page_offset(page);
2262 if (i_size_read(inode) < (pagebase + offset))
2263 i_size_write(inode, pagebase + offset);
2265 if (PageChecked(page)) {
2266 SetPageUptodate(page);
2267 ClearPageChecked(page);
2270 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2276 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2280 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2281 * prepare_write. Essentially, if the page exists within the file,
2282 * and is not being fully written, then we should populate it.
2285 if (!PageUptodate(page)) {
2286 loff_t pagebase = page_offset(page);
2287 loff_t isize = i_size_read(page->mapping->host);
2289 /* Is the location we are writing to beyond the end of the file? */
2290 if (pagebase >= isize ||
2291 ((from == 0) && (pagebase + to) >= isize)) {
2292 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2293 SetPageChecked(page);
2294 /* Are we we writing a full page */
2295 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2296 SetPageChecked(page);
2297 /* Is the page readable, if it's wronly, we don't care, because we're
2298 * not actually going to read from it ... */
2299 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2300 /* We don't care if fillpage fails, because if it does the page
2301 * won't be marked as up to date
2303 afs_linux_fillpage(file, page);
2309 #if defined(HAVE_WRITE_BEGIN)
2311 afs_linux_write_end(struct file *file, struct address_space *mapping,
2312 loff_t pos, unsigned len, unsigned copied,
2313 struct page *page, void *fsdata)
2316 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2318 code = afs_linux_commit_write(file, page, from, from + len);
2321 page_cache_release(page);
2326 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2327 loff_t pos, unsigned len, unsigned flags,
2328 struct page **pagep, void **fsdata)
2331 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2332 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2335 page = grab_cache_page_write_begin(mapping, index, flags);
2338 code = afs_linux_prepare_write(file, page, from, from + len);
2341 page_cache_release(page);
2349 static struct inode_operations afs_file_iops = {
2350 .permission = afs_linux_permission,
2351 .getattr = afs_linux_getattr,
2352 .setattr = afs_notify_change,
2355 static struct address_space_operations afs_file_aops = {
2356 .readpage = afs_linux_readpage,
2357 .readpages = afs_linux_readpages,
2358 .writepage = afs_linux_writepage,
2359 #if defined (HAVE_WRITE_BEGIN)
2360 .write_begin = afs_linux_write_begin,
2361 .write_end = afs_linux_write_end,
2363 .commit_write = afs_linux_commit_write,
2364 .prepare_write = afs_linux_prepare_write,
2369 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2370 * by what sort of operation is allowed.....
2373 static struct inode_operations afs_dir_iops = {
2374 .setattr = afs_notify_change,
2375 .create = afs_linux_create,
2376 .lookup = afs_linux_lookup,
2377 .link = afs_linux_link,
2378 .unlink = afs_linux_unlink,
2379 .symlink = afs_linux_symlink,
2380 .mkdir = afs_linux_mkdir,
2381 .rmdir = afs_linux_rmdir,
2382 .rename = afs_linux_rename,
2383 .getattr = afs_linux_getattr,
2384 .permission = afs_linux_permission,
2387 /* We really need a separate symlink set of ops, since do_follow_link()
2388 * determines if it _is_ a link by checking if the follow_link op is set.
2390 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2392 afs_symlink_filler(struct file *file, struct page *page)
2394 struct inode *ip = (struct inode *)page->mapping->host;
2395 char *p = (char *)kmap(page);
2398 afs_maybe_lock_kernel();
2400 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2405 p[code] = '\0'; /* null terminate? */
2406 afs_maybe_unlock_kernel();
2408 SetPageUptodate(page);
2414 afs_maybe_unlock_kernel();
2422 static struct address_space_operations afs_symlink_aops = {
2423 .readpage = afs_symlink_filler
2425 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2427 static struct inode_operations afs_symlink_iops = {
2428 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2429 .readlink = page_readlink,
2430 # if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2431 .follow_link = page_follow_link,
2433 .follow_link = page_follow_link_light,
2434 .put_link = page_put_link,
2436 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2437 .readlink = afs_linux_readlink,
2438 .follow_link = afs_linux_follow_link,
2439 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2440 .setattr = afs_notify_change,
2444 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2448 vattr2inode(ip, vattr);
2450 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2451 /* Reset ops if symlink or directory. */
2452 if (S_ISREG(ip->i_mode)) {
2453 ip->i_op = &afs_file_iops;
2454 ip->i_fop = &afs_file_fops;
2455 ip->i_data.a_ops = &afs_file_aops;
2457 } else if (S_ISDIR(ip->i_mode)) {
2458 ip->i_op = &afs_dir_iops;
2459 ip->i_fop = &afs_dir_fops;
2461 } else if (S_ISLNK(ip->i_mode)) {
2462 ip->i_op = &afs_symlink_iops;
2463 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2464 ip->i_data.a_ops = &afs_symlink_aops;
2465 ip->i_mapping = &ip->i_data;