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/writeback.h>
34 #include <linux/pagevec.h>
36 #include "afs/afs_bypasscache.h"
38 #include "osi_compat.h"
39 #include "osi_pagecopy.h"
41 #ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
42 #define __pagevec_lru_add_file __pagevec_lru_add
46 #define MAX_ERRNO 1000L
49 extern struct backing_dev_info *afs_backing_dev_info;
51 extern struct vcache *afs_globalVp;
52 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
54 /* This function converts a positive error code from AFS into a negative
55 * code suitable for passing into the Linux VFS layer. It checks that the
56 * error code is within the permissable bounds for the ERR_PTR mechanism.
58 * _All_ error codes which come from the AFS layer should be passed through
59 * this function before being returned to the kernel.
63 afs_convert_code(int code) {
64 if ((code >= 0) && (code <= MAX_ERRNO))
70 /* Linux doesn't require a credp for many functions, and crref is an expensive
71 * operation. This helper function avoids obtaining it for VerifyVCache calls
75 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
80 if (avc->f.states & CStatd) {
88 code = afs_InitReq(&treq, credp);
90 code = afs_VerifyVCache2(avc, &treq);
97 return afs_convert_code(code);
100 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
102 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *iov, unsigned long segs, loff_t pos)
104 struct file *fp = iocb->ki_filp;
106 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
109 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
110 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
112 code = afs_linux_VerifyVCache(vcp, NULL);
115 /* Linux's FlushPages implementation doesn't ever use credp,
116 * so we optimise by not using it */
117 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
119 code = generic_file_aio_read(iocb, iov, segs, pos);
123 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
124 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
131 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
134 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
137 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
138 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
140 code = afs_linux_VerifyVCache(vcp, NULL);
143 /* Linux's FlushPages implementation doesn't ever use credp,
144 * so we optimise by not using it */
145 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
147 code = do_sync_read(fp, buf, count, offp);
151 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
152 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
160 /* Now we have integrated VM for writes as well as reads. the generic write operations
161 * also take care of re-positioning the pointer if file is open in append
162 * mode. Call fake open/close to ensure we do writes of core dumps.
164 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
166 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *iov, unsigned long segs, loff_t pos)
169 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
174 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
175 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
176 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
178 code = afs_linux_VerifyVCache(vcp, &credp);
180 ObtainWriteLock(&vcp->lock, 529);
182 ReleaseWriteLock(&vcp->lock);
185 code = generic_file_aio_write(iocb, iov, segs, pos);
189 ObtainWriteLock(&vcp->lock, 530);
191 if (vcp->execsOrWriters == 1 && !credp)
194 afs_FakeClose(vcp, credp);
195 ReleaseWriteLock(&vcp->lock);
197 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
198 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32, segs, ICL_TYPE_INT32,
208 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
211 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
216 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
217 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
218 (fp->f_flags & O_APPEND) ? 99998 : 99999);
220 code = afs_linux_VerifyVCache(vcp, &credp);
222 ObtainWriteLock(&vcp->lock, 529);
224 ReleaseWriteLock(&vcp->lock);
227 code = do_sync_write(fp, buf, count, offp);
231 ObtainWriteLock(&vcp->lock, 530);
233 if (vcp->execsOrWriters == 1 && !credp)
236 afs_FakeClose(vcp, credp);
237 ReleaseWriteLock(&vcp->lock);
239 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
240 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
250 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
252 /* This is a complete rewrite of afs_readdir, since we can make use of
253 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
254 * handling and use of bulkstats will need to be reflected here as well.
257 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
259 struct vcache *avc = VTOAFS(FILE_INODE(fp));
260 struct vrequest treq;
268 afs_size_t origOffset, tlen;
269 cred_t *credp = crref();
270 struct afs_fakestat_state fakestat;
273 AFS_STATCNT(afs_readdir);
275 code = afs_convert_code(afs_InitReq(&treq, credp));
280 afs_InitFakeStat(&fakestat);
281 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
285 /* update the cache entry */
287 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
291 /* get a reference to the entire directory */
292 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
298 ObtainSharedLock(&avc->lock, 810);
299 UpgradeSToWLock(&avc->lock, 811);
300 ObtainReadLock(&tdc->lock);
302 * Make sure that the data in the cache is current. There are two
303 * cases we need to worry about:
304 * 1. The cache data is being fetched by another process.
305 * 2. The cache data is no longer valid
307 while ((avc->f.states & CStatd)
308 && (tdc->dflags & DFFetching)
309 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
310 ReleaseReadLock(&tdc->lock);
311 ReleaseSharedLock(&avc->lock);
312 afs_osi_Sleep(&tdc->validPos);
313 ObtainSharedLock(&avc->lock, 812);
314 ObtainReadLock(&tdc->lock);
316 if (!(avc->f.states & CStatd)
317 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
318 ReleaseReadLock(&tdc->lock);
319 ReleaseSharedLock(&avc->lock);
324 /* Set the readdir-in-progress flag, and downgrade the lock
325 * to shared so others will be able to acquire a read lock.
327 avc->f.states |= CReadDir;
328 avc->dcreaddir = tdc;
329 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
330 ConvertWToSLock(&avc->lock);
332 /* Fill in until we get an error or we're done. This implementation
333 * takes an offset in units of blobs, rather than bytes.
336 offset = (int) fp->f_pos;
338 dirpos = BlobScan(tdc, offset);
342 de = afs_dir_GetBlob(tdc, dirpos);
346 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
349 len = strlen(de->name);
351 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
352 (unsigned long)&tdc->f.inode, dirpos);
354 ReleaseSharedLock(&avc->lock);
360 /* filldir returns -EINVAL when the buffer is full. */
362 unsigned int type = DT_UNKNOWN;
363 struct VenusFid afid;
366 afid.Cell = avc->f.fid.Cell;
367 afid.Fid.Volume = avc->f.fid.Fid.Volume;
368 afid.Fid.Vnode = ntohl(de->fid.vnode);
369 afid.Fid.Unique = ntohl(de->fid.vunique);
370 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
372 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
375 } else if (((tvc->f.states) & (CStatd | CTruth))) {
376 /* CTruth will be set if the object has
381 else if (vtype == VREG)
383 /* Don't do this until we're sure it can't be a mtpt */
384 /* else if (vtype == VLNK)
386 /* what other types does AFS support? */
388 /* clean up from afs_FindVCache */
392 * If this is NFS readdirplus, then the filler is going to
393 * call getattr on this inode, which will deadlock if we're
397 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
403 offset = dirpos + 1 + ((len + 16) >> 5);
405 /* If filldir didn't fill in the last one this is still pointing to that
408 fp->f_pos = (loff_t) offset;
410 ReleaseReadLock(&tdc->lock);
412 UpgradeSToWLock(&avc->lock, 813);
413 avc->f.states &= ~CReadDir;
415 avc->readdir_pid = 0;
416 ReleaseSharedLock(&avc->lock);
420 afs_PutFakeStat(&fakestat);
427 /* in afs_pioctl.c */
428 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
431 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
432 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
434 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
441 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
443 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
447 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
448 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
449 vmap->vm_end - vmap->vm_start);
451 /* get a validated vcache entry */
452 code = afs_linux_VerifyVCache(vcp, NULL);
455 /* Linux's Flushpage implementation doesn't use credp, so optimise
456 * our code to not need to crref() it */
457 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
459 code = generic_file_mmap(fp, vmap);
462 vcp->f.states |= CMAPPED;
470 afs_linux_open(struct inode *ip, struct file *fp)
472 struct vcache *vcp = VTOAFS(ip);
473 cred_t *credp = crref();
477 code = afs_open(&vcp, fp->f_flags, credp);
481 return afs_convert_code(code);
485 afs_linux_release(struct inode *ip, struct file *fp)
487 struct vcache *vcp = VTOAFS(ip);
488 cred_t *credp = crref();
492 code = afs_close(vcp, fp->f_flags, credp);
493 ObtainWriteLock(&vcp->lock, 807);
498 ReleaseWriteLock(&vcp->lock);
502 return afs_convert_code(code);
506 #if defined(FOP_FSYNC_TAKES_DENTRY)
507 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
509 afs_linux_fsync(struct file *fp, int datasync)
513 struct inode *ip = FILE_INODE(fp);
514 cred_t *credp = crref();
517 code = afs_fsync(VTOAFS(ip), credp);
520 return afs_convert_code(code);
526 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
529 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
530 cred_t *credp = crref();
531 struct AFS_FLOCK flock;
533 /* Convert to a lock format afs_lockctl understands. */
534 memset(&flock, 0, sizeof(flock));
535 flock.l_type = flp->fl_type;
536 flock.l_pid = flp->fl_pid;
538 flock.l_start = flp->fl_start;
539 if (flp->fl_end == OFFSET_MAX)
540 flock.l_len = 0; /* Lock to end of file */
542 flock.l_len = flp->fl_end - flp->fl_start + 1;
544 /* Safe because there are no large files, yet */
545 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
546 if (cmd == F_GETLK64)
548 else if (cmd == F_SETLK64)
550 else if (cmd == F_SETLKW64)
552 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
555 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
558 if ((code == 0 || flp->fl_type == F_UNLCK) &&
559 (cmd == F_SETLK || cmd == F_SETLKW)) {
560 code = afs_posix_lock_file(fp, flp);
561 if (code && flp->fl_type != F_UNLCK) {
562 struct AFS_FLOCK flock2;
564 flock2.l_type = F_UNLCK;
566 afs_lockctl(vcp, &flock2, F_SETLK, credp);
570 /* If lockctl says there are no conflicting locks, then also check with the
571 * kernel, as lockctl knows nothing about byte range locks
573 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
574 afs_posix_test_lock(fp, flp);
575 /* If we found a lock in the kernel's structure, return it */
576 if (flp->fl_type != F_UNLCK) {
582 /* Convert flock back to Linux's file_lock */
583 flp->fl_type = flock.l_type;
584 flp->fl_pid = flock.l_pid;
585 flp->fl_start = flock.l_start;
586 if (flock.l_len == 0)
587 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
589 flp->fl_end = flock.l_start + flock.l_len - 1;
595 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
597 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
599 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
600 cred_t *credp = crref();
601 struct AFS_FLOCK flock;
602 /* Convert to a lock format afs_lockctl understands. */
603 memset(&flock, 0, sizeof(flock));
604 flock.l_type = flp->fl_type;
605 flock.l_pid = flp->fl_pid;
610 /* Safe because there are no large files, yet */
611 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
612 if (cmd == F_GETLK64)
614 else if (cmd == F_SETLK64)
616 else if (cmd == F_SETLKW64)
618 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
621 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
624 if ((code == 0 || flp->fl_type == F_UNLCK) &&
625 (cmd == F_SETLK || cmd == F_SETLKW)) {
626 flp->fl_flags &=~ FL_SLEEP;
627 code = flock_lock_file_wait(fp, flp);
628 if (code && flp->fl_type != F_UNLCK) {
629 struct AFS_FLOCK flock2;
631 flock2.l_type = F_UNLCK;
633 afs_lockctl(vcp, &flock2, F_SETLK, credp);
637 /* Convert flock back to Linux's file_lock */
638 flp->fl_type = flock.l_type;
639 flp->fl_pid = flock.l_pid;
647 * essentially the same as afs_fsync() but we need to get the return
648 * code for the sys_close() here, not afs_linux_release(), so call
649 * afs_StoreAllSegments() with AFS_LASTSTORE
652 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
653 afs_linux_flush(struct file *fp, fl_owner_t id)
655 afs_linux_flush(struct file *fp)
658 struct vrequest treq;
666 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
674 vcp = VTOAFS(FILE_INODE(fp));
676 code = afs_InitReq(&treq, credp);
679 /* If caching is bypassed for this file, or globally, just return 0 */
680 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
683 ObtainReadLock(&vcp->lock);
684 if (vcp->cachingStates & FCSBypass)
686 ReleaseReadLock(&vcp->lock);
689 /* future proof: don't rely on 0 return from afs_InitReq */
694 ObtainSharedLock(&vcp->lock, 535);
695 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
696 UpgradeSToWLock(&vcp->lock, 536);
697 if (!AFS_IS_DISCONNECTED) {
698 code = afs_StoreAllSegments(vcp,
700 AFS_SYNC | AFS_LASTSTORE);
702 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
704 ConvertWToSLock(&vcp->lock);
706 code = afs_CheckCode(code, &treq, 54);
707 ReleaseSharedLock(&vcp->lock);
714 return afs_convert_code(code);
717 struct file_operations afs_dir_fops = {
718 .read = generic_read_dir,
719 .readdir = afs_linux_readdir,
720 #ifdef HAVE_UNLOCKED_IOCTL
721 .unlocked_ioctl = afs_unlocked_xioctl,
725 #ifdef HAVE_COMPAT_IOCTL
726 .compat_ioctl = afs_unlocked_xioctl,
728 .open = afs_linux_open,
729 .release = afs_linux_release,
730 .llseek = default_llseek,
733 struct file_operations afs_file_fops = {
734 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
735 .aio_read = afs_linux_aio_read,
736 .aio_write = afs_linux_aio_write,
738 .read = afs_linux_read,
739 .write = afs_linux_write,
741 #ifdef HAVE_UNLOCKED_IOCTL
742 .unlocked_ioctl = afs_unlocked_xioctl,
746 #ifdef HAVE_COMPAT_IOCTL
747 .compat_ioctl = afs_unlocked_xioctl,
749 .mmap = afs_linux_mmap,
750 .open = afs_linux_open,
751 .flush = afs_linux_flush,
752 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
753 .sendfile = generic_file_sendfile,
755 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
756 .splice_write = generic_file_splice_write,
757 .splice_read = generic_file_splice_read,
759 .release = afs_linux_release,
760 .fsync = afs_linux_fsync,
761 .lock = afs_linux_lock,
762 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
763 .flock = afs_linux_flock,
765 .llseek = default_llseek,
769 /**********************************************************************
770 * AFS Linux dentry operations
771 **********************************************************************/
773 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
774 * that has its mvid (parent dir's fid) pointer set to the wrong directory
775 * due to being mounted in multiple points at once. fix_bad_parent()
776 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
777 * dotdotfid and mtpoint fid members.
779 * dp - dentry to be checked.
780 * credp - credentials
781 * vcp, pvc - item's and parent's vcache pointer
785 * This dentry's vcache's mvid will be set to the correct parent directory's
787 * This root vnode's volume will have its dotdotfid and mtpoint fids set
788 * to the correct parent and mountpoint fids.
792 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
794 struct vcache *avc = NULL;
796 /* force a lookup, so vcp->mvid is fixed up */
797 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
798 if (!avc || vcp != avc) { /* bad, very bad.. */
799 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
800 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
801 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
802 ICL_TYPE_POINTER, dp);
805 AFS_RELE(AFSTOV(avc));
810 /* afs_linux_revalidate
811 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
814 afs_linux_revalidate(struct dentry *dp)
817 struct vcache *vcp = VTOAFS(dp->d_inode);
821 if (afs_shuttingdown)
827 /* Make this a fast path (no crref), since it's called so often. */
828 if (vcp->states & CStatd) {
829 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
831 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
832 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
835 fix_bad_parent(dp); /* check and correct mvid */
844 /* This avoids the crref when we don't have to do it. Watch for
845 * changes in afs_getattr that don't get replicated here!
847 if (vcp->f.states & CStatd &&
848 (!afs_fakestat_enable || vcp->mvstat != 1) &&
850 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
851 code = afs_CopyOutAttrs(vcp, &vattr);
854 code = afs_getattr(vcp, &vattr, credp);
859 afs_fill_inode(AFSTOV(vcp), &vattr);
863 return afs_convert_code(code);
867 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
869 int err = afs_linux_revalidate(dentry);
871 generic_fillattr(dentry->d_inode, stat);
876 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
877 * In kernels 2.2.10 and above, we are passed an additional flags var which
878 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
879 * we are advised to follow the entry if it is a link or to make sure that
880 * it is a directory. But since the kernel itself checks these possibilities
881 * later on, we shouldn't have to do it until later. Perhaps in the future..
883 * The code here assumes that on entry the global lock is not held
886 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
887 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
889 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
893 cred_t *credp = NULL;
894 struct vcache *vcp, *pvcp, *tvc = NULL;
895 struct dentry *parent;
897 struct afs_fakestat_state fakestate;
901 /* We don't support RCU path walking */
902 if (nd->flags & LOOKUP_RCU)
906 afs_InitFakeStat(&fakestate);
909 vcp = VTOAFS(dp->d_inode);
911 if (vcp == afs_globalVp)
914 parent = dget_parent(dp);
915 pvcp = VTOAFS(parent->d_inode);
917 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
923 if (locked && vcp->mvstat == 1) { /* mount point */
924 if (vcp->mvid && (vcp->f.states & CMValid)) {
927 struct vrequest treq;
929 code = afs_InitReq(&treq, credp);
931 (strcmp(dp->d_name.name, ".directory") == 0)) {
935 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
937 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
938 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
939 /* a mount point, not yet replaced by its directory */
944 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
945 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
946 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
951 /* If the last looker changes, we should make sure the current
952 * looker still has permission to examine this file. This would
953 * always require a crref() which would be "slow".
955 if (vcp->last_looker != treq.uid) {
956 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
959 vcp->last_looker = treq.uid;
964 /* If the parent's DataVersion has changed or the vnode
965 * is longer valid, we need to do a full lookup. VerifyVCache
966 * isn't enough since the vnode may have been renamed.
969 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
975 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
976 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
977 if (!tvc || tvc != vcp) {
982 if (afs_getattr(vcp, &vattr, credp)) {
987 vattr2inode(AFSTOV(vcp), &vattr);
988 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
991 /* should we always update the attributes at this point? */
992 /* unlikely--the vcache entry hasn't changed */
997 /* If this code is ever enabled, we should use dget_parent to handle
998 * getting the parent, and dput() to dispose of it. See above for an
1000 pvcp = VTOAFS(dp->d_parent->d_inode);
1001 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1005 /* No change in parent's DataVersion so this negative
1006 * lookup is still valid. BUT, if a server is down a
1007 * negative lookup can result so there should be a
1008 * liftime as well. For now, always expire.
1021 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1023 /* we hold the global lock if we evaluated a mount point */
1030 shrink_dcache_parent(dp);
1036 if (have_submounts(dp))
1044 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1046 struct vcache *vcp = VTOAFS(ip);
1049 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1050 (void) afs_InactiveVCache(vcp, NULL);
1053 afs_linux_clear_nfsfs_renamed(dp);
1059 afs_dentry_delete(struct dentry *dp)
1061 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1062 return 1; /* bad inode? */
1067 struct dentry_operations afs_dentry_operations = {
1068 .d_revalidate = afs_linux_dentry_revalidate,
1069 .d_delete = afs_dentry_delete,
1070 .d_iput = afs_dentry_iput,
1073 /**********************************************************************
1074 * AFS Linux inode operations
1075 **********************************************************************/
1079 * Merely need to set enough of vattr to get us through the create. Note
1080 * that the higher level code (open_namei) will take care of any tuncation
1081 * explicitly. Exclusive open is also taken care of in open_namei.
1083 * name is in kernel space at this point.
1086 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1087 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1088 struct nameidata *nd)
1090 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1094 cred_t *credp = crref();
1095 const char *name = dp->d_name.name;
1100 vattr.va_mode = mode;
1101 vattr.va_type = mode & S_IFMT;
1104 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1108 struct inode *ip = AFSTOV(vcp);
1110 afs_getattr(vcp, &vattr, credp);
1111 afs_fill_inode(ip, &vattr);
1112 insert_inode_hash(ip);
1113 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1114 dp->d_op = &afs_dentry_operations;
1116 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1117 d_instantiate(dp, ip);
1122 return afs_convert_code(code);
1125 /* afs_linux_lookup */
1126 static struct dentry *
1127 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1128 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1129 struct nameidata *nd)
1131 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1134 cred_t *credp = crref();
1135 struct vcache *vcp = NULL;
1136 const char *comp = dp->d_name.name;
1137 struct inode *ip = NULL;
1138 struct dentry *newdp = NULL;
1142 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1148 afs_getattr(vcp, &vattr, credp);
1149 afs_fill_inode(ip, &vattr);
1150 if (hlist_unhashed(&ip->i_hash))
1151 insert_inode_hash(ip);
1153 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1154 dp->d_op = &afs_dentry_operations;
1156 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1159 if (ip && S_ISDIR(ip->i_mode)) {
1160 struct dentry *alias;
1162 /* Try to invalidate an existing alias in favor of our new one */
1163 alias = d_find_alias(ip);
1164 /* But not if it's disconnected; then we want d_splice_alias below */
1165 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1166 if (d_invalidate(alias) == 0) {
1175 newdp = d_splice_alias(ip, dp);
1179 /* It's ok for the file to not be found. That's noted by the caller by
1180 * seeing that the dp->d_inode field is NULL.
1182 if (!code || code == ENOENT)
1185 return ERR_PTR(afs_convert_code(code));
1189 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1192 cred_t *credp = crref();
1193 const char *name = newdp->d_name.name;
1194 struct inode *oldip = olddp->d_inode;
1196 /* If afs_link returned the vnode, we could instantiate the
1197 * dentry. Since it's not, we drop this one and do a new lookup.
1202 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1206 return afs_convert_code(code);
1209 /* We have to have a Linux specific sillyrename function, because we
1210 * also have to keep the dcache up to date when we're doing a silly
1211 * rename - so we don't want the generic vnodeops doing this behind our
1216 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1219 struct vcache *tvc = VTOAFS(dentry->d_inode);
1220 struct dentry *__dp = NULL;
1221 char *__name = NULL;
1224 if (afs_linux_nfsfs_renamed(dentry))
1232 osi_FreeSmallSpace(__name);
1233 __name = afs_newname();
1236 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1239 osi_FreeSmallSpace(__name);
1242 } while (__dp->d_inode != NULL);
1245 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1246 VTOAFS(dir), (char *)__dp->d_name.name,
1249 tvc->mvid = (void *) __name;
1252 crfree(tvc->uncred);
1254 tvc->uncred = credp;
1255 tvc->f.states |= CUnlinked;
1256 afs_linux_set_nfsfs_renamed(dentry);
1258 osi_FreeSmallSpace(__name);
1263 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1264 d_move(dentry, __dp);
1273 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1276 cred_t *credp = crref();
1277 const char *name = dp->d_name.name;
1278 struct vcache *tvc = VTOAFS(dp->d_inode);
1280 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1281 && !(tvc->f.states & CUnlinked)) {
1283 code = afs_linux_sillyrename(dip, dp, credp);
1286 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1293 return afs_convert_code(code);
1298 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1301 cred_t *credp = crref();
1303 const char *name = dp->d_name.name;
1305 /* If afs_symlink returned the vnode, we could instantiate the
1306 * dentry. Since it's not, we drop this one and do a new lookup.
1312 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1315 return afs_convert_code(code);
1319 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1322 cred_t *credp = crref();
1323 struct vcache *tvcp = NULL;
1325 const char *name = dp->d_name.name;
1328 vattr.va_mask = ATTR_MODE;
1329 vattr.va_mode = mode;
1331 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1334 struct inode *ip = AFSTOV(tvcp);
1336 afs_getattr(tvcp, &vattr, credp);
1337 afs_fill_inode(ip, &vattr);
1339 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1340 dp->d_op = &afs_dentry_operations;
1342 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1343 d_instantiate(dp, ip);
1348 return afs_convert_code(code);
1352 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1355 cred_t *credp = crref();
1356 const char *name = dp->d_name.name;
1358 /* locking kernel conflicts with glock? */
1361 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1364 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1365 * that failed because a directory is not empty. So, we map
1366 * EEXIST to ENOTEMPTY on linux.
1368 if (code == EEXIST) {
1377 return afs_convert_code(code);
1382 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1383 struct inode *newip, struct dentry *newdp)
1386 cred_t *credp = crref();
1387 const char *oldname = olddp->d_name.name;
1388 const char *newname = newdp->d_name.name;
1389 struct dentry *rehash = NULL;
1391 /* Prevent any new references during rename operation. */
1393 if (!d_unhashed(newdp)) {
1398 #if defined(D_COUNT_INT)
1399 spin_lock(&olddp->d_lock);
1400 if (olddp->d_count > 1) {
1401 spin_unlock(&olddp->d_lock);
1402 shrink_dcache_parent(olddp);
1404 spin_unlock(&olddp->d_lock);
1406 if (atomic_read(&olddp->d_count) > 1)
1407 shrink_dcache_parent(olddp);
1411 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1415 olddp->d_time = 0; /* force to revalidate */
1421 return afs_convert_code(code);
1425 /* afs_linux_ireadlink
1426 * Internal readlink which can return link contents to user or kernel space.
1427 * Note that the buffer is NOT supposed to be null-terminated.
1430 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1433 cred_t *credp = crref();
1437 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1438 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1442 return maxlen - tuio.uio_resid;
1444 return afs_convert_code(code);
1447 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1448 /* afs_linux_readlink
1449 * Fill target (which is in user space) with contents of symlink.
1452 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1455 struct inode *ip = dp->d_inode;
1458 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1464 /* afs_linux_follow_link
1465 * a file system dependent link following routine.
1467 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1472 name = osi_Alloc(PATH_MAX);
1478 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1486 nd_set_link(nd, name);
1491 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1493 char *name = nd_get_link(nd);
1494 if (name && !IS_ERR(name)) {
1495 osi_Free(name, PATH_MAX);
1499 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1501 /* Populate a page by filling it from the cache file pointed at by cachefp
1502 * (which contains indicated chunk)
1503 * If task is NULL, the page copy occurs syncronously, and the routine
1504 * returns with page still locked. If task is non-NULL, then page copies
1505 * may occur in the background, and the page will be unlocked when it is
1509 afs_linux_read_cache(struct file *cachefp, struct page *page,
1510 int chunk, struct pagevec *lrupv,
1511 struct afs_pagecopy_task *task) {
1512 loff_t offset = page_offset(page);
1513 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1514 struct page *newpage, *cachepage;
1515 struct address_space *cachemapping;
1516 int pageindex, endindex;
1519 cachemapping = cacheinode->i_mapping;
1523 /* If we're trying to read a page that's past the end of the disk
1524 * cache file, then just return a zeroed page */
1525 if (offset >= i_size_read(cacheinode)) {
1526 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1527 SetPageUptodate(page);
1533 /* From our offset, we now need to work out which page in the disk
1534 * file it corresponds to. This will be fun ... */
1535 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1537 while (cachepage == NULL) {
1538 cachepage = find_get_page(cachemapping, pageindex);
1541 newpage = page_cache_alloc_cold(cachemapping);
1547 code = add_to_page_cache(newpage, cachemapping,
1548 pageindex, GFP_KERNEL);
1550 cachepage = newpage;
1553 page_cache_get(cachepage);
1554 if (!pagevec_add(lrupv, cachepage))
1555 __pagevec_lru_add_file(lrupv);
1558 page_cache_release(newpage);
1560 if (code != -EEXIST)
1564 lock_page(cachepage);
1568 if (!PageUptodate(cachepage)) {
1569 ClearPageError(cachepage);
1570 code = cachemapping->a_ops->readpage(NULL, cachepage);
1571 if (!code && !task) {
1572 wait_on_page_locked(cachepage);
1575 unlock_page(cachepage);
1579 if (PageUptodate(cachepage)) {
1580 copy_highpage(page, cachepage);
1581 flush_dcache_page(page);
1582 SetPageUptodate(page);
1587 afs_pagecopy_queue_page(task, cachepage, page);
1599 page_cache_release(cachepage);
1605 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1607 loff_t offset = page_offset(pp);
1608 struct inode *ip = FILE_INODE(fp);
1609 struct vcache *avc = VTOAFS(ip);
1611 struct file *cacheFp = NULL;
1614 struct pagevec lrupv;
1616 /* Not a UFS cache, don't do anything */
1617 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1620 /* Can't do anything if the vcache isn't statd , or if the read
1621 * crosses a chunk boundary.
1623 if (!(avc->f.states & CStatd) ||
1624 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1628 ObtainWriteLock(&avc->lock, 911);
1630 /* XXX - See if hinting actually makes things faster !!! */
1632 /* See if we have a suitable entry already cached */
1636 /* We need to lock xdcache, then dcache, to handle situations where
1637 * the hint is on the free list. However, we can't safely do this
1638 * according to the locking hierarchy. So, use a non blocking lock.
1640 ObtainReadLock(&afs_xdcache);
1641 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1643 if (dcLocked && (tdc->index != NULLIDX)
1644 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1645 && tdc->f.chunk == AFS_CHUNK(offset)
1646 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1647 /* Bonus - the hint was correct */
1650 /* Only destroy the hint if its actually invalid, not if there's
1651 * just been a locking failure */
1653 ReleaseReadLock(&tdc->lock);
1660 ReleaseReadLock(&afs_xdcache);
1663 /* No hint, or hint is no longer valid - see if we can get something
1664 * directly from the dcache
1667 tdc = afs_FindDCache(avc, offset);
1670 ReleaseWriteLock(&avc->lock);
1675 ObtainReadLock(&tdc->lock);
1677 /* Is the dcache we've been given currently up to date */
1678 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1679 (tdc->dflags & DFFetching)) {
1680 ReleaseWriteLock(&avc->lock);
1681 ReleaseReadLock(&tdc->lock);
1686 /* Update our hint for future abuse */
1689 /* Okay, so we've now got a cache file that is up to date */
1691 /* XXX - I suspect we should be locking the inodes before we use them! */
1693 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1694 pagevec_init(&lrupv, 0);
1696 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1698 if (pagevec_count(&lrupv))
1699 __pagevec_lru_add_file(&lrupv);
1701 filp_close(cacheFp, NULL);
1704 ReleaseReadLock(&tdc->lock);
1705 ReleaseWriteLock(&avc->lock);
1712 /* afs_linux_readpage
1714 * This function is split into two, because prepare_write/begin_write
1715 * require a readpage call which doesn't unlock the resulting page upon
1719 afs_linux_fillpage(struct file *fp, struct page *pp)
1724 struct iovec *iovecp;
1725 struct inode *ip = FILE_INODE(fp);
1726 afs_int32 cnt = page_count(pp);
1727 struct vcache *avc = VTOAFS(ip);
1728 afs_offs_t offset = page_offset(pp);
1732 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1742 auio = osi_Alloc(sizeof(uio_t));
1743 iovecp = osi_Alloc(sizeof(struct iovec));
1745 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1750 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1751 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1752 99999); /* not a possible code value */
1754 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1756 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1757 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1759 AFS_DISCON_UNLOCK();
1762 /* XXX valid for no-cache also? Check last bits of files... :)
1763 * Cognate code goes in afs_NoCacheFetchProc. */
1764 if (auio->uio_resid) /* zero remainder of page */
1765 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1768 flush_dcache_page(pp);
1769 SetPageUptodate(pp);
1774 osi_Free(auio, sizeof(uio_t));
1775 osi_Free(iovecp, sizeof(struct iovec));
1778 return afs_convert_code(code);
1782 afs_linux_prefetch(struct file *fp, struct page *pp)
1785 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1786 afs_offs_t offset = page_offset(pp);
1788 if (AFS_CHUNKOFFSET(offset) == 0) {
1790 struct vrequest treq;
1795 code = afs_InitReq(&treq, credp);
1796 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1797 tdc = afs_FindDCache(avc, offset);
1799 if (!(tdc->mflags & DFNextStarted))
1800 afs_PrefetchChunk(avc, tdc, credp, &treq);
1803 ReleaseWriteLock(&avc->lock);
1808 return afs_convert_code(code);
1813 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1814 struct list_head *page_list, unsigned num_pages)
1819 struct iovec* iovecp;
1820 struct nocache_read_request *ancr;
1822 struct pagevec lrupv;
1826 struct inode *ip = FILE_INODE(fp);
1827 struct vcache *avc = VTOAFS(ip);
1828 afs_int32 base_index = 0;
1829 afs_int32 page_count = 0;
1832 /* background thread must free: iovecp, auio, ancr */
1833 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1835 auio = osi_Alloc(sizeof(uio_t));
1836 auio->uio_iov = iovecp;
1837 auio->uio_iovcnt = num_pages;
1838 auio->uio_flag = UIO_READ;
1839 auio->uio_seg = AFS_UIOSYS;
1840 auio->uio_resid = num_pages * PAGE_SIZE;
1842 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1844 ancr->offset = auio->uio_offset;
1845 ancr->length = auio->uio_resid;
1847 pagevec_init(&lrupv, 0);
1849 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1851 if(list_empty(page_list))
1854 pp = list_entry(page_list->prev, struct page, lru);
1855 /* If we allocate a page and don't remove it from page_list,
1856 * the page cache gets upset. */
1858 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1859 if(pp->index > isize) {
1866 offset = page_offset(pp);
1867 auio->uio_offset = offset;
1868 base_index = pp->index;
1870 iovecp[page_ix].iov_len = PAGE_SIZE;
1871 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1872 if(base_index != pp->index) {
1875 page_cache_release(pp);
1876 iovecp[page_ix].iov_base = (void *) 0;
1878 ancr->length -= PAGE_SIZE;
1885 page_cache_release(pp);
1886 iovecp[page_ix].iov_base = (void *) 0;
1889 if(!PageLocked(pp)) {
1893 /* increment page refcount--our original design assumed
1894 * that locking it would effectively pin it; protect
1895 * ourselves from the possiblity that this assumption is
1896 * is faulty, at low cost (provided we do not fail to
1897 * do the corresponding decref on the other side) */
1900 /* save the page for background map */
1901 iovecp[page_ix].iov_base = (void*) pp;
1903 /* and put it on the LRU cache */
1904 if (!pagevec_add(&lrupv, pp))
1905 __pagevec_lru_add_file(&lrupv);
1909 /* If there were useful pages in the page list, make sure all pages
1910 * are in the LRU cache, then schedule the read */
1912 if (pagevec_count(&lrupv))
1913 __pagevec_lru_add_file(&lrupv);
1915 code = afs_ReadNoCache(avc, ancr, credp);
1918 /* If there is nothing for the background thread to handle,
1919 * it won't be freeing the things that we never gave it */
1920 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1921 osi_Free(auio, sizeof(uio_t));
1922 osi_Free(ancr, sizeof(struct nocache_read_request));
1924 /* we do not flush, release, or unmap pages--that will be
1925 * done for us by the background thread as each page comes in
1926 * from the fileserver */
1927 return afs_convert_code(code);
1932 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1934 cred_t *credp = NULL;
1936 struct iovec *iovecp;
1937 struct nocache_read_request *ancr;
1941 * Special case: if page is at or past end of file, just zero it and set
1944 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1945 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1946 SetPageUptodate(pp);
1953 /* receiver frees */
1954 auio = osi_Alloc(sizeof(uio_t));
1955 iovecp = osi_Alloc(sizeof(struct iovec));
1957 /* address can be NULL, because we overwrite it with 'pp', below */
1958 setup_uio(auio, iovecp, NULL, page_offset(pp),
1959 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1961 /* save the page for background map */
1962 get_page(pp); /* see above */
1963 auio->uio_iov->iov_base = (void*) pp;
1964 /* the background thread will free this */
1965 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1967 ancr->offset = page_offset(pp);
1968 ancr->length = PAGE_SIZE;
1971 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1974 return afs_convert_code(code);
1978 afs_linux_can_bypass(struct inode *ip) {
1979 switch(cache_bypass_strategy) {
1980 case NEVER_BYPASS_CACHE:
1982 case ALWAYS_BYPASS_CACHE:
1984 case LARGE_FILES_BYPASS_CACHE:
1985 if(i_size_read(ip) > cache_bypass_threshold)
1992 /* Check if a file is permitted to bypass the cache by policy, and modify
1993 * the cache bypass state recorded for that file */
1996 afs_linux_bypass_check(struct inode *ip) {
1999 int bypass = afs_linux_can_bypass(ip);
2002 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2010 afs_linux_readpage(struct file *fp, struct page *pp)
2014 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2015 code = afs_linux_bypass_readpage(fp, pp);
2017 code = afs_linux_fillpage(fp, pp);
2019 code = afs_linux_prefetch(fp, pp);
2026 /* Readpages reads a number of pages for a particular file. We use
2027 * this to optimise the reading, by limiting the number of times upon which
2028 * we have to lookup, lock and open vcaches and dcaches
2032 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2033 struct list_head *page_list, unsigned int num_pages)
2035 struct inode *inode = mapping->host;
2036 struct vcache *avc = VTOAFS(inode);
2038 struct file *cacheFp = NULL;
2040 unsigned int page_idx;
2042 struct pagevec lrupv;
2043 struct afs_pagecopy_task *task;
2045 if (afs_linux_bypass_check(inode))
2046 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2048 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2052 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2057 ObtainWriteLock(&avc->lock, 912);
2060 task = afs_pagecopy_init_task();
2063 pagevec_init(&lrupv, 0);
2064 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2065 struct page *page = list_entry(page_list->prev, struct page, lru);
2066 list_del(&page->lru);
2067 offset = page_offset(page);
2069 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2071 ReleaseReadLock(&tdc->lock);
2076 filp_close(cacheFp, NULL);
2081 if ((tdc = afs_FindDCache(avc, offset))) {
2082 ObtainReadLock(&tdc->lock);
2083 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2084 (tdc->dflags & DFFetching)) {
2085 ReleaseReadLock(&tdc->lock);
2092 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2095 if (tdc && !add_to_page_cache(page, mapping, page->index,
2097 page_cache_get(page);
2098 if (!pagevec_add(&lrupv, page))
2099 __pagevec_lru_add_file(&lrupv);
2101 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2103 page_cache_release(page);
2105 if (pagevec_count(&lrupv))
2106 __pagevec_lru_add_file(&lrupv);
2109 filp_close(cacheFp, NULL);
2111 afs_pagecopy_put_task(task);
2115 ReleaseReadLock(&tdc->lock);
2119 ReleaseWriteLock(&avc->lock);
2124 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2127 afs_linux_prepare_writeback(struct vcache *avc) {
2128 if (avc->f.states & CPageWrite) {
2129 return AOP_WRITEPAGE_ACTIVATE;
2131 avc->f.states |= CPageWrite;
2136 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2137 struct vrequest treq;
2140 if (!afs_InitReq(&treq, credp))
2141 code = afs_DoPartialWrite(avc, &treq);
2143 return afs_convert_code(code);
2147 afs_linux_complete_writeback(struct vcache *avc) {
2148 avc->f.states &= ~CPageWrite;
2151 /* Writeback a given page syncronously. Called with no AFS locks held */
2153 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2154 unsigned long offset, unsigned int count,
2157 struct vcache *vcp = VTOAFS(ip);
2165 buffer = kmap(pp) + offset;
2166 base = page_offset(pp) + offset;
2169 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2170 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2171 ICL_TYPE_INT32, 99999);
2173 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2175 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2177 i_size_write(ip, vcp->f.m.Length);
2178 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2180 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2182 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2183 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2184 ICL_TYPE_INT32, code);
2193 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2194 unsigned long offset, unsigned int count)
2198 struct vcache *vcp = VTOAFS(ip);
2201 /* Catch recursive writeback. This occurs if the kernel decides
2202 * writeback is required whilst we are writing to the cache, or
2203 * flushing to the server. When we're running syncronously (as
2204 * opposed to from writepage) we can't actually do anything about
2205 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2208 ObtainWriteLock(&vcp->lock, 532);
2209 afs_linux_prepare_writeback(vcp);
2210 ReleaseWriteLock(&vcp->lock);
2214 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2217 ObtainWriteLock(&vcp->lock, 533);
2219 code1 = afs_linux_dopartialwrite(vcp, credp);
2220 afs_linux_complete_writeback(vcp);
2221 ReleaseWriteLock(&vcp->lock);
2232 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2233 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2235 afs_linux_writepage(struct page *pp)
2238 struct address_space *mapping = pp->mapping;
2239 struct inode *inode;
2242 unsigned int to = PAGE_CACHE_SIZE;
2247 if (PageReclaim(pp)) {
2248 return AOP_WRITEPAGE_ACTIVATE;
2249 /* XXX - Do we need to redirty the page here? */
2254 inode = mapping->host;
2255 vcp = VTOAFS(inode);
2256 isize = i_size_read(inode);
2258 /* Don't defeat an earlier truncate */
2259 if (page_offset(pp) > isize) {
2260 set_page_writeback(pp);
2266 ObtainWriteLock(&vcp->lock, 537);
2267 code = afs_linux_prepare_writeback(vcp);
2268 if (code == AOP_WRITEPAGE_ACTIVATE) {
2269 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2270 * to return with the page still locked */
2271 ReleaseWriteLock(&vcp->lock);
2276 /* Grab the creds structure currently held in the vnode, and
2277 * get a reference to it, in case it goes away ... */
2283 ReleaseWriteLock(&vcp->lock);
2286 set_page_writeback(pp);
2288 SetPageUptodate(pp);
2290 /* We can unlock the page here, because it's protected by the
2291 * page_writeback flag. This should make us less vulnerable to
2292 * deadlocking in afs_write and afs_DoPartialWrite
2296 /* If this is the final page, then just write the number of bytes that
2297 * are actually in it */
2298 if ((isize - page_offset(pp)) < to )
2299 to = isize - page_offset(pp);
2301 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2304 ObtainWriteLock(&vcp->lock, 538);
2306 /* As much as we might like to ignore a file server error here,
2307 * and just try again when we close(), unfortunately StoreAllSegments
2308 * will invalidate our chunks if the server returns a permanent error,
2309 * so we need to at least try and get that error back to the user
2312 code1 = afs_linux_dopartialwrite(vcp, credp);
2314 afs_linux_complete_writeback(vcp);
2315 ReleaseWriteLock(&vcp->lock);
2320 end_page_writeback(pp);
2321 page_cache_release(pp);
2332 /* afs_linux_permission
2333 * Check access rights - returns error if can't check or permission denied.
2336 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2337 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2338 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2339 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2341 afs_linux_permission(struct inode *ip, int mode)
2348 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2349 /* We don't support RCU path walking */
2350 if (flags & IPERM_FLAG_RCU)
2356 if (mode & MAY_EXEC)
2358 if (mode & MAY_READ)
2360 if (mode & MAY_WRITE)
2362 code = afs_access(VTOAFS(ip), tmp, credp);
2366 return afs_convert_code(code);
2370 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2374 struct inode *inode = FILE_INODE(file);
2375 loff_t pagebase = page_offset(page);
2377 if (i_size_read(inode) < (pagebase + offset))
2378 i_size_write(inode, pagebase + offset);
2380 if (PageChecked(page)) {
2381 SetPageUptodate(page);
2382 ClearPageChecked(page);
2385 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2391 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2395 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2396 * prepare_write. Essentially, if the page exists within the file,
2397 * and is not being fully written, then we should populate it.
2400 if (!PageUptodate(page)) {
2401 loff_t pagebase = page_offset(page);
2402 loff_t isize = i_size_read(page->mapping->host);
2404 /* Is the location we are writing to beyond the end of the file? */
2405 if (pagebase >= isize ||
2406 ((from == 0) && (pagebase + to) >= isize)) {
2407 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2408 SetPageChecked(page);
2409 /* Are we we writing a full page */
2410 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2411 SetPageChecked(page);
2412 /* Is the page readable, if it's wronly, we don't care, because we're
2413 * not actually going to read from it ... */
2414 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2415 /* We don't care if fillpage fails, because if it does the page
2416 * won't be marked as up to date
2418 afs_linux_fillpage(file, page);
2424 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2426 afs_linux_write_end(struct file *file, struct address_space *mapping,
2427 loff_t pos, unsigned len, unsigned copied,
2428 struct page *page, void *fsdata)
2431 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2433 code = afs_linux_commit_write(file, page, from, from + len);
2436 page_cache_release(page);
2441 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2442 loff_t pos, unsigned len, unsigned flags,
2443 struct page **pagep, void **fsdata)
2446 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2447 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2450 page = grab_cache_page_write_begin(mapping, index, flags);
2453 code = afs_linux_prepare_write(file, page, from, from + len);
2456 page_cache_release(page);
2464 static struct inode_operations afs_file_iops = {
2465 .permission = afs_linux_permission,
2466 .getattr = afs_linux_getattr,
2467 .setattr = afs_notify_change,
2470 static struct address_space_operations afs_file_aops = {
2471 .readpage = afs_linux_readpage,
2472 .readpages = afs_linux_readpages,
2473 .writepage = afs_linux_writepage,
2474 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2475 .write_begin = afs_linux_write_begin,
2476 .write_end = afs_linux_write_end,
2478 .commit_write = afs_linux_commit_write,
2479 .prepare_write = afs_linux_prepare_write,
2484 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2485 * by what sort of operation is allowed.....
2488 static struct inode_operations afs_dir_iops = {
2489 .setattr = afs_notify_change,
2490 .create = afs_linux_create,
2491 .lookup = afs_linux_lookup,
2492 .link = afs_linux_link,
2493 .unlink = afs_linux_unlink,
2494 .symlink = afs_linux_symlink,
2495 .mkdir = afs_linux_mkdir,
2496 .rmdir = afs_linux_rmdir,
2497 .rename = afs_linux_rename,
2498 .getattr = afs_linux_getattr,
2499 .permission = afs_linux_permission,
2502 /* We really need a separate symlink set of ops, since do_follow_link()
2503 * determines if it _is_ a link by checking if the follow_link op is set.
2505 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2507 afs_symlink_filler(struct file *file, struct page *page)
2509 struct inode *ip = (struct inode *)page->mapping->host;
2510 char *p = (char *)kmap(page);
2514 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2519 p[code] = '\0'; /* null terminate? */
2521 SetPageUptodate(page);
2533 static struct address_space_operations afs_symlink_aops = {
2534 .readpage = afs_symlink_filler
2536 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2538 static struct inode_operations afs_symlink_iops = {
2539 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2540 .readlink = page_readlink,
2541 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2542 .follow_link = page_follow_link,
2544 .follow_link = page_follow_link_light,
2545 .put_link = page_put_link,
2547 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2548 .readlink = afs_linux_readlink,
2549 .follow_link = afs_linux_follow_link,
2550 .put_link = afs_linux_put_link,
2551 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2552 .setattr = afs_notify_change,
2556 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2560 vattr2inode(ip, vattr);
2562 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2563 /* Reset ops if symlink or directory. */
2564 if (S_ISREG(ip->i_mode)) {
2565 ip->i_op = &afs_file_iops;
2566 ip->i_fop = &afs_file_fops;
2567 ip->i_data.a_ops = &afs_file_aops;
2569 } else if (S_ISDIR(ip->i_mode)) {
2570 ip->i_op = &afs_dir_iops;
2571 ip->i_fop = &afs_dir_fops;
2573 } else if (S_ISLNK(ip->i_mode)) {
2574 ip->i_op = &afs_symlink_iops;
2575 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2576 ip->i_data.a_ops = &afs_symlink_aops;
2577 ip->i_mapping = &ip->i_data;