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;
266 struct DirBuffer entry;
269 afs_size_t origOffset, tlen;
270 cred_t *credp = crref();
271 struct afs_fakestat_state fakestat;
274 AFS_STATCNT(afs_readdir);
276 code = afs_convert_code(afs_InitReq(&treq, credp));
281 afs_InitFakeStat(&fakestat);
282 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
286 /* update the cache entry */
288 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
292 /* get a reference to the entire directory */
293 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
299 ObtainSharedLock(&avc->lock, 810);
300 UpgradeSToWLock(&avc->lock, 811);
301 ObtainReadLock(&tdc->lock);
303 * Make sure that the data in the cache is current. There are two
304 * cases we need to worry about:
305 * 1. The cache data is being fetched by another process.
306 * 2. The cache data is no longer valid
308 while ((avc->f.states & CStatd)
309 && (tdc->dflags & DFFetching)
310 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
311 ReleaseReadLock(&tdc->lock);
312 ReleaseSharedLock(&avc->lock);
313 afs_osi_Sleep(&tdc->validPos);
314 ObtainSharedLock(&avc->lock, 812);
315 ObtainReadLock(&tdc->lock);
317 if (!(avc->f.states & CStatd)
318 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
319 ReleaseReadLock(&tdc->lock);
320 ReleaseSharedLock(&avc->lock);
325 /* Set the readdir-in-progress flag, and downgrade the lock
326 * to shared so others will be able to acquire a read lock.
328 avc->f.states |= CReadDir;
329 avc->dcreaddir = tdc;
330 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
331 ConvertWToSLock(&avc->lock);
333 /* Fill in until we get an error or we're done. This implementation
334 * takes an offset in units of blobs, rather than bytes.
337 offset = (int) fp->f_pos;
339 dirpos = BlobScan(tdc, offset);
343 code = afs_dir_GetBlob(tdc, dirpos, &entry);
346 de = (struct DirEntry *)entry.data;
348 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
351 len = strlen(de->name);
353 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
354 (unsigned long)&tdc->f.inode, dirpos);
356 ReleaseSharedLock(&avc->lock);
362 /* filldir returns -EINVAL when the buffer is full. */
364 unsigned int type = DT_UNKNOWN;
365 struct VenusFid afid;
368 afid.Cell = avc->f.fid.Cell;
369 afid.Fid.Volume = avc->f.fid.Fid.Volume;
370 afid.Fid.Vnode = ntohl(de->fid.vnode);
371 afid.Fid.Unique = ntohl(de->fid.vunique);
372 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
374 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
377 } else if (((tvc->f.states) & (CStatd | CTruth))) {
378 /* CTruth will be set if the object has
383 else if (vtype == VREG)
385 /* Don't do this until we're sure it can't be a mtpt */
386 /* else if (vtype == VLNK)
388 /* what other types does AFS support? */
390 /* clean up from afs_FindVCache */
394 * If this is NFS readdirplus, then the filler is going to
395 * call getattr on this inode, which will deadlock if we're
399 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
405 offset = dirpos + 1 + ((len + 16) >> 5);
407 /* If filldir didn't fill in the last one this is still pointing to that
410 fp->f_pos = (loff_t) offset;
412 ReleaseReadLock(&tdc->lock);
414 UpgradeSToWLock(&avc->lock, 813);
415 avc->f.states &= ~CReadDir;
417 avc->readdir_pid = 0;
418 ReleaseSharedLock(&avc->lock);
422 afs_PutFakeStat(&fakestat);
429 /* in afs_pioctl.c */
430 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
433 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
434 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
436 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
443 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
445 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
449 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
450 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
451 vmap->vm_end - vmap->vm_start);
453 /* get a validated vcache entry */
454 code = afs_linux_VerifyVCache(vcp, NULL);
457 /* Linux's Flushpage implementation doesn't use credp, so optimise
458 * our code to not need to crref() it */
459 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
461 code = generic_file_mmap(fp, vmap);
464 vcp->f.states |= CMAPPED;
472 afs_linux_open(struct inode *ip, struct file *fp)
474 struct vcache *vcp = VTOAFS(ip);
475 cred_t *credp = crref();
479 code = afs_open(&vcp, fp->f_flags, credp);
483 return afs_convert_code(code);
487 afs_linux_release(struct inode *ip, struct file *fp)
489 struct vcache *vcp = VTOAFS(ip);
490 cred_t *credp = crref();
494 code = afs_close(vcp, fp->f_flags, credp);
495 ObtainWriteLock(&vcp->lock, 807);
500 ReleaseWriteLock(&vcp->lock);
504 return afs_convert_code(code);
508 #if defined(FOP_FSYNC_TAKES_DENTRY)
509 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
511 afs_linux_fsync(struct file *fp, int datasync)
515 struct inode *ip = FILE_INODE(fp);
516 cred_t *credp = crref();
519 code = afs_fsync(VTOAFS(ip), credp);
522 return afs_convert_code(code);
528 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
531 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
532 cred_t *credp = crref();
533 struct AFS_FLOCK flock;
535 /* Convert to a lock format afs_lockctl understands. */
536 memset(&flock, 0, sizeof(flock));
537 flock.l_type = flp->fl_type;
538 flock.l_pid = flp->fl_pid;
540 flock.l_start = flp->fl_start;
541 if (flp->fl_end == OFFSET_MAX)
542 flock.l_len = 0; /* Lock to end of file */
544 flock.l_len = flp->fl_end - flp->fl_start + 1;
546 /* Safe because there are no large files, yet */
547 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
548 if (cmd == F_GETLK64)
550 else if (cmd == F_SETLK64)
552 else if (cmd == F_SETLKW64)
554 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
557 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
560 if ((code == 0 || flp->fl_type == F_UNLCK) &&
561 (cmd == F_SETLK || cmd == F_SETLKW)) {
562 code = afs_posix_lock_file(fp, flp);
563 if (code && flp->fl_type != F_UNLCK) {
564 struct AFS_FLOCK flock2;
566 flock2.l_type = F_UNLCK;
568 afs_lockctl(vcp, &flock2, F_SETLK, credp);
572 /* If lockctl says there are no conflicting locks, then also check with the
573 * kernel, as lockctl knows nothing about byte range locks
575 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
576 afs_posix_test_lock(fp, flp);
577 /* If we found a lock in the kernel's structure, return it */
578 if (flp->fl_type != F_UNLCK) {
584 /* Convert flock back to Linux's file_lock */
585 flp->fl_type = flock.l_type;
586 flp->fl_pid = flock.l_pid;
587 flp->fl_start = flock.l_start;
588 if (flock.l_len == 0)
589 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
591 flp->fl_end = flock.l_start + flock.l_len - 1;
597 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
599 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
601 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
602 cred_t *credp = crref();
603 struct AFS_FLOCK flock;
604 /* Convert to a lock format afs_lockctl understands. */
605 memset(&flock, 0, sizeof(flock));
606 flock.l_type = flp->fl_type;
607 flock.l_pid = flp->fl_pid;
612 /* Safe because there are no large files, yet */
613 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
614 if (cmd == F_GETLK64)
616 else if (cmd == F_SETLK64)
618 else if (cmd == F_SETLKW64)
620 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
623 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
626 if ((code == 0 || flp->fl_type == F_UNLCK) &&
627 (cmd == F_SETLK || cmd == F_SETLKW)) {
628 flp->fl_flags &=~ FL_SLEEP;
629 code = flock_lock_file_wait(fp, flp);
630 if (code && flp->fl_type != F_UNLCK) {
631 struct AFS_FLOCK flock2;
633 flock2.l_type = F_UNLCK;
635 afs_lockctl(vcp, &flock2, F_SETLK, credp);
639 /* Convert flock back to Linux's file_lock */
640 flp->fl_type = flock.l_type;
641 flp->fl_pid = flock.l_pid;
649 * essentially the same as afs_fsync() but we need to get the return
650 * code for the sys_close() here, not afs_linux_release(), so call
651 * afs_StoreAllSegments() with AFS_LASTSTORE
654 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
655 afs_linux_flush(struct file *fp, fl_owner_t id)
657 afs_linux_flush(struct file *fp)
660 struct vrequest treq;
668 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
676 vcp = VTOAFS(FILE_INODE(fp));
678 code = afs_InitReq(&treq, credp);
681 /* If caching is bypassed for this file, or globally, just return 0 */
682 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
685 ObtainReadLock(&vcp->lock);
686 if (vcp->cachingStates & FCSBypass)
688 ReleaseReadLock(&vcp->lock);
691 /* future proof: don't rely on 0 return from afs_InitReq */
696 ObtainSharedLock(&vcp->lock, 535);
697 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
698 UpgradeSToWLock(&vcp->lock, 536);
699 if (!AFS_IS_DISCONNECTED) {
700 code = afs_StoreAllSegments(vcp,
702 AFS_SYNC | AFS_LASTSTORE);
704 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
706 ConvertWToSLock(&vcp->lock);
708 code = afs_CheckCode(code, &treq, 54);
709 ReleaseSharedLock(&vcp->lock);
716 return afs_convert_code(code);
719 struct file_operations afs_dir_fops = {
720 .read = generic_read_dir,
721 .readdir = afs_linux_readdir,
722 #ifdef HAVE_UNLOCKED_IOCTL
723 .unlocked_ioctl = afs_unlocked_xioctl,
727 #ifdef HAVE_COMPAT_IOCTL
728 .compat_ioctl = afs_unlocked_xioctl,
730 .open = afs_linux_open,
731 .release = afs_linux_release,
732 .llseek = default_llseek,
735 struct file_operations afs_file_fops = {
736 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
737 .aio_read = afs_linux_aio_read,
738 .aio_write = afs_linux_aio_write,
740 .read = afs_linux_read,
741 .write = afs_linux_write,
743 #ifdef HAVE_UNLOCKED_IOCTL
744 .unlocked_ioctl = afs_unlocked_xioctl,
748 #ifdef HAVE_COMPAT_IOCTL
749 .compat_ioctl = afs_unlocked_xioctl,
751 .mmap = afs_linux_mmap,
752 .open = afs_linux_open,
753 .flush = afs_linux_flush,
754 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
755 .sendfile = generic_file_sendfile,
757 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
758 .splice_write = generic_file_splice_write,
759 .splice_read = generic_file_splice_read,
761 .release = afs_linux_release,
762 .fsync = afs_linux_fsync,
763 .lock = afs_linux_lock,
764 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
765 .flock = afs_linux_flock,
767 .llseek = default_llseek,
771 /**********************************************************************
772 * AFS Linux dentry operations
773 **********************************************************************/
775 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
776 * that has its mvid (parent dir's fid) pointer set to the wrong directory
777 * due to being mounted in multiple points at once. fix_bad_parent()
778 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
779 * dotdotfid and mtpoint fid members.
781 * dp - dentry to be checked.
782 * credp - credentials
783 * vcp, pvc - item's and parent's vcache pointer
787 * This dentry's vcache's mvid will be set to the correct parent directory's
789 * This root vnode's volume will have its dotdotfid and mtpoint fids set
790 * to the correct parent and mountpoint fids.
794 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
796 struct vcache *avc = NULL;
798 /* force a lookup, so vcp->mvid is fixed up */
799 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
800 if (!avc || vcp != avc) { /* bad, very bad.. */
801 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
802 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
803 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
804 ICL_TYPE_POINTER, dp);
807 AFS_RELE(AFSTOV(avc));
812 /* afs_linux_revalidate
813 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
816 afs_linux_revalidate(struct dentry *dp)
819 struct vcache *vcp = VTOAFS(dp->d_inode);
823 if (afs_shuttingdown)
829 /* Make this a fast path (no crref), since it's called so often. */
830 if (vcp->states & CStatd) {
831 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
833 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
834 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
837 fix_bad_parent(dp); /* check and correct mvid */
846 /* This avoids the crref when we don't have to do it. Watch for
847 * changes in afs_getattr that don't get replicated here!
849 if (vcp->f.states & CStatd &&
850 (!afs_fakestat_enable || vcp->mvstat != 1) &&
852 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
853 code = afs_CopyOutAttrs(vcp, &vattr);
856 code = afs_getattr(vcp, &vattr, credp);
861 afs_fill_inode(AFSTOV(vcp), &vattr);
865 return afs_convert_code(code);
869 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
871 int err = afs_linux_revalidate(dentry);
873 generic_fillattr(dentry->d_inode, stat);
878 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
879 * In kernels 2.2.10 and above, we are passed an additional flags var which
880 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
881 * we are advised to follow the entry if it is a link or to make sure that
882 * it is a directory. But since the kernel itself checks these possibilities
883 * later on, we shouldn't have to do it until later. Perhaps in the future..
885 * The code here assumes that on entry the global lock is not held
888 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
889 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
891 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
895 cred_t *credp = NULL;
896 struct vcache *vcp, *pvcp, *tvc = NULL;
897 struct dentry *parent;
899 struct afs_fakestat_state fakestate;
903 /* We don't support RCU path walking */
904 if (nd->flags & LOOKUP_RCU)
908 afs_InitFakeStat(&fakestate);
911 vcp = VTOAFS(dp->d_inode);
913 if (vcp == afs_globalVp)
916 parent = dget_parent(dp);
917 pvcp = VTOAFS(parent->d_inode);
919 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
925 if (locked && vcp->mvstat == 1) { /* mount point */
926 if (vcp->mvid && (vcp->f.states & CMValid)) {
929 struct vrequest treq;
931 code = afs_InitReq(&treq, credp);
933 (strcmp(dp->d_name.name, ".directory") == 0)) {
937 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
939 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
940 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
941 /* a mount point, not yet replaced by its directory */
946 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
947 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
948 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
953 /* If the last looker changes, we should make sure the current
954 * looker still has permission to examine this file. This would
955 * always require a crref() which would be "slow".
957 if (vcp->last_looker != treq.uid) {
958 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
961 vcp->last_looker = treq.uid;
966 /* If the parent's DataVersion has changed or the vnode
967 * is longer valid, we need to do a full lookup. VerifyVCache
968 * isn't enough since the vnode may have been renamed.
971 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
977 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
978 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
979 if (!tvc || tvc != vcp) {
984 if (afs_getattr(vcp, &vattr, credp)) {
989 vattr2inode(AFSTOV(vcp), &vattr);
990 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
993 /* should we always update the attributes at this point? */
994 /* unlikely--the vcache entry hasn't changed */
999 /* If this code is ever enabled, we should use dget_parent to handle
1000 * getting the parent, and dput() to dispose of it. See above for an
1002 pvcp = VTOAFS(dp->d_parent->d_inode);
1003 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1007 /* No change in parent's DataVersion so this negative
1008 * lookup is still valid. BUT, if a server is down a
1009 * negative lookup can result so there should be a
1010 * liftime as well. For now, always expire.
1023 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1025 /* we hold the global lock if we evaluated a mount point */
1032 shrink_dcache_parent(dp);
1038 if (have_submounts(dp))
1046 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1048 struct vcache *vcp = VTOAFS(ip);
1051 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1052 (void) afs_InactiveVCache(vcp, NULL);
1055 afs_linux_clear_nfsfs_renamed(dp);
1061 afs_dentry_delete(struct dentry *dp)
1063 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1064 return 1; /* bad inode? */
1069 struct dentry_operations afs_dentry_operations = {
1070 .d_revalidate = afs_linux_dentry_revalidate,
1071 .d_delete = afs_dentry_delete,
1072 .d_iput = afs_dentry_iput,
1075 /**********************************************************************
1076 * AFS Linux inode operations
1077 **********************************************************************/
1081 * Merely need to set enough of vattr to get us through the create. Note
1082 * that the higher level code (open_namei) will take care of any tuncation
1083 * explicitly. Exclusive open is also taken care of in open_namei.
1085 * name is in kernel space at this point.
1088 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1089 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1090 struct nameidata *nd)
1092 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1096 cred_t *credp = crref();
1097 const char *name = dp->d_name.name;
1102 vattr.va_mode = mode;
1103 vattr.va_type = mode & S_IFMT;
1106 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1110 struct inode *ip = AFSTOV(vcp);
1112 afs_getattr(vcp, &vattr, credp);
1113 afs_fill_inode(ip, &vattr);
1114 insert_inode_hash(ip);
1115 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1116 dp->d_op = &afs_dentry_operations;
1118 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1119 d_instantiate(dp, ip);
1124 return afs_convert_code(code);
1127 /* afs_linux_lookup */
1128 static struct dentry *
1129 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1130 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1131 struct nameidata *nd)
1133 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1136 cred_t *credp = crref();
1137 struct vcache *vcp = NULL;
1138 const char *comp = dp->d_name.name;
1139 struct inode *ip = NULL;
1140 struct dentry *newdp = NULL;
1144 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1150 afs_getattr(vcp, &vattr, credp);
1151 afs_fill_inode(ip, &vattr);
1152 if (hlist_unhashed(&ip->i_hash))
1153 insert_inode_hash(ip);
1155 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1156 dp->d_op = &afs_dentry_operations;
1158 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1161 if (ip && S_ISDIR(ip->i_mode)) {
1162 struct dentry *alias;
1164 /* Try to invalidate an existing alias in favor of our new one */
1165 alias = d_find_alias(ip);
1166 /* But not if it's disconnected; then we want d_splice_alias below */
1167 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1168 if (d_invalidate(alias) == 0) {
1177 newdp = d_splice_alias(ip, dp);
1181 /* It's ok for the file to not be found. That's noted by the caller by
1182 * seeing that the dp->d_inode field is NULL.
1184 if (!code || code == ENOENT)
1187 return ERR_PTR(afs_convert_code(code));
1191 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1194 cred_t *credp = crref();
1195 const char *name = newdp->d_name.name;
1196 struct inode *oldip = olddp->d_inode;
1198 /* If afs_link returned the vnode, we could instantiate the
1199 * dentry. Since it's not, we drop this one and do a new lookup.
1204 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1208 return afs_convert_code(code);
1211 /* We have to have a Linux specific sillyrename function, because we
1212 * also have to keep the dcache up to date when we're doing a silly
1213 * rename - so we don't want the generic vnodeops doing this behind our
1218 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1221 struct vcache *tvc = VTOAFS(dentry->d_inode);
1222 struct dentry *__dp = NULL;
1223 char *__name = NULL;
1226 if (afs_linux_nfsfs_renamed(dentry))
1234 osi_FreeSmallSpace(__name);
1235 __name = afs_newname();
1238 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1241 osi_FreeSmallSpace(__name);
1244 } while (__dp->d_inode != NULL);
1247 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1248 VTOAFS(dir), (char *)__dp->d_name.name,
1251 tvc->mvid = (void *) __name;
1254 crfree(tvc->uncred);
1256 tvc->uncred = credp;
1257 tvc->f.states |= CUnlinked;
1258 afs_linux_set_nfsfs_renamed(dentry);
1260 osi_FreeSmallSpace(__name);
1265 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1266 d_move(dentry, __dp);
1275 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1278 cred_t *credp = crref();
1279 const char *name = dp->d_name.name;
1280 struct vcache *tvc = VTOAFS(dp->d_inode);
1282 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1283 && !(tvc->f.states & CUnlinked)) {
1285 code = afs_linux_sillyrename(dip, dp, credp);
1288 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1295 return afs_convert_code(code);
1300 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1303 cred_t *credp = crref();
1305 const char *name = dp->d_name.name;
1307 /* If afs_symlink returned the vnode, we could instantiate the
1308 * dentry. Since it's not, we drop this one and do a new lookup.
1314 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1317 return afs_convert_code(code);
1321 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1324 cred_t *credp = crref();
1325 struct vcache *tvcp = NULL;
1327 const char *name = dp->d_name.name;
1330 vattr.va_mask = ATTR_MODE;
1331 vattr.va_mode = mode;
1333 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1336 struct inode *ip = AFSTOV(tvcp);
1338 afs_getattr(tvcp, &vattr, credp);
1339 afs_fill_inode(ip, &vattr);
1341 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1342 dp->d_op = &afs_dentry_operations;
1344 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1345 d_instantiate(dp, ip);
1350 return afs_convert_code(code);
1354 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1357 cred_t *credp = crref();
1358 const char *name = dp->d_name.name;
1360 /* locking kernel conflicts with glock? */
1363 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1366 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1367 * that failed because a directory is not empty. So, we map
1368 * EEXIST to ENOTEMPTY on linux.
1370 if (code == EEXIST) {
1379 return afs_convert_code(code);
1384 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1385 struct inode *newip, struct dentry *newdp)
1388 cred_t *credp = crref();
1389 const char *oldname = olddp->d_name.name;
1390 const char *newname = newdp->d_name.name;
1391 struct dentry *rehash = NULL;
1393 /* Prevent any new references during rename operation. */
1395 if (!d_unhashed(newdp)) {
1400 #if defined(D_COUNT_INT)
1401 spin_lock(&olddp->d_lock);
1402 if (olddp->d_count > 1) {
1403 spin_unlock(&olddp->d_lock);
1404 shrink_dcache_parent(olddp);
1406 spin_unlock(&olddp->d_lock);
1408 if (atomic_read(&olddp->d_count) > 1)
1409 shrink_dcache_parent(olddp);
1413 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1417 olddp->d_time = 0; /* force to revalidate */
1423 return afs_convert_code(code);
1427 /* afs_linux_ireadlink
1428 * Internal readlink which can return link contents to user or kernel space.
1429 * Note that the buffer is NOT supposed to be null-terminated.
1432 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1435 cred_t *credp = crref();
1439 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1440 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1444 return maxlen - tuio.uio_resid;
1446 return afs_convert_code(code);
1449 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1450 /* afs_linux_readlink
1451 * Fill target (which is in user space) with contents of symlink.
1454 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1457 struct inode *ip = dp->d_inode;
1460 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1466 /* afs_linux_follow_link
1467 * a file system dependent link following routine.
1469 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1474 name = osi_Alloc(PATH_MAX);
1480 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1488 nd_set_link(nd, name);
1493 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1495 char *name = nd_get_link(nd);
1496 if (name && !IS_ERR(name)) {
1497 osi_Free(name, PATH_MAX);
1501 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1503 /* Populate a page by filling it from the cache file pointed at by cachefp
1504 * (which contains indicated chunk)
1505 * If task is NULL, the page copy occurs syncronously, and the routine
1506 * returns with page still locked. If task is non-NULL, then page copies
1507 * may occur in the background, and the page will be unlocked when it is
1511 afs_linux_read_cache(struct file *cachefp, struct page *page,
1512 int chunk, struct pagevec *lrupv,
1513 struct afs_pagecopy_task *task) {
1514 loff_t offset = page_offset(page);
1515 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1516 struct page *newpage, *cachepage;
1517 struct address_space *cachemapping;
1518 int pageindex, endindex;
1521 cachemapping = cacheinode->i_mapping;
1525 /* If we're trying to read a page that's past the end of the disk
1526 * cache file, then just return a zeroed page */
1527 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1528 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1529 SetPageUptodate(page);
1535 /* From our offset, we now need to work out which page in the disk
1536 * file it corresponds to. This will be fun ... */
1537 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1539 while (cachepage == NULL) {
1540 cachepage = find_get_page(cachemapping, pageindex);
1543 newpage = page_cache_alloc_cold(cachemapping);
1549 code = add_to_page_cache(newpage, cachemapping,
1550 pageindex, GFP_KERNEL);
1552 cachepage = newpage;
1555 page_cache_get(cachepage);
1556 if (!pagevec_add(lrupv, cachepage))
1557 __pagevec_lru_add_file(lrupv);
1560 page_cache_release(newpage);
1562 if (code != -EEXIST)
1566 lock_page(cachepage);
1570 if (!PageUptodate(cachepage)) {
1571 ClearPageError(cachepage);
1572 code = cachemapping->a_ops->readpage(NULL, cachepage);
1573 if (!code && !task) {
1574 wait_on_page_locked(cachepage);
1577 unlock_page(cachepage);
1581 if (PageUptodate(cachepage)) {
1582 copy_highpage(page, cachepage);
1583 flush_dcache_page(page);
1584 SetPageUptodate(page);
1589 afs_pagecopy_queue_page(task, cachepage, page);
1601 page_cache_release(cachepage);
1607 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1609 loff_t offset = page_offset(pp);
1610 struct inode *ip = FILE_INODE(fp);
1611 struct vcache *avc = VTOAFS(ip);
1613 struct file *cacheFp = NULL;
1616 struct pagevec lrupv;
1618 /* Not a UFS cache, don't do anything */
1619 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1622 /* Can't do anything if the vcache isn't statd , or if the read
1623 * crosses a chunk boundary.
1625 if (!(avc->f.states & CStatd) ||
1626 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1630 ObtainWriteLock(&avc->lock, 911);
1632 /* XXX - See if hinting actually makes things faster !!! */
1634 /* See if we have a suitable entry already cached */
1638 /* We need to lock xdcache, then dcache, to handle situations where
1639 * the hint is on the free list. However, we can't safely do this
1640 * according to the locking hierarchy. So, use a non blocking lock.
1642 ObtainReadLock(&afs_xdcache);
1643 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1645 if (dcLocked && (tdc->index != NULLIDX)
1646 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1647 && tdc->f.chunk == AFS_CHUNK(offset)
1648 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1649 /* Bonus - the hint was correct */
1652 /* Only destroy the hint if its actually invalid, not if there's
1653 * just been a locking failure */
1655 ReleaseReadLock(&tdc->lock);
1662 ReleaseReadLock(&afs_xdcache);
1665 /* No hint, or hint is no longer valid - see if we can get something
1666 * directly from the dcache
1669 tdc = afs_FindDCache(avc, offset);
1672 ReleaseWriteLock(&avc->lock);
1677 ObtainReadLock(&tdc->lock);
1679 /* Is the dcache we've been given currently up to date */
1680 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1681 (tdc->dflags & DFFetching)) {
1682 ReleaseWriteLock(&avc->lock);
1683 ReleaseReadLock(&tdc->lock);
1688 /* Update our hint for future abuse */
1691 /* Okay, so we've now got a cache file that is up to date */
1693 /* XXX - I suspect we should be locking the inodes before we use them! */
1695 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1696 pagevec_init(&lrupv, 0);
1698 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1700 if (pagevec_count(&lrupv))
1701 __pagevec_lru_add_file(&lrupv);
1703 filp_close(cacheFp, NULL);
1706 ReleaseReadLock(&tdc->lock);
1707 ReleaseWriteLock(&avc->lock);
1714 /* afs_linux_readpage
1716 * This function is split into two, because prepare_write/begin_write
1717 * require a readpage call which doesn't unlock the resulting page upon
1721 afs_linux_fillpage(struct file *fp, struct page *pp)
1726 struct iovec *iovecp;
1727 struct inode *ip = FILE_INODE(fp);
1728 afs_int32 cnt = page_count(pp);
1729 struct vcache *avc = VTOAFS(ip);
1730 afs_offs_t offset = page_offset(pp);
1734 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1744 auio = osi_Alloc(sizeof(struct uio));
1745 iovecp = osi_Alloc(sizeof(struct iovec));
1747 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1752 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1753 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1754 99999); /* not a possible code value */
1756 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1758 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1759 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1761 AFS_DISCON_UNLOCK();
1764 /* XXX valid for no-cache also? Check last bits of files... :)
1765 * Cognate code goes in afs_NoCacheFetchProc. */
1766 if (auio->uio_resid) /* zero remainder of page */
1767 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1770 flush_dcache_page(pp);
1771 SetPageUptodate(pp);
1776 osi_Free(auio, sizeof(struct uio));
1777 osi_Free(iovecp, sizeof(struct iovec));
1780 return afs_convert_code(code);
1784 afs_linux_prefetch(struct file *fp, struct page *pp)
1787 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1788 afs_offs_t offset = page_offset(pp);
1790 if (AFS_CHUNKOFFSET(offset) == 0) {
1792 struct vrequest treq;
1797 code = afs_InitReq(&treq, credp);
1798 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1799 tdc = afs_FindDCache(avc, offset);
1801 if (!(tdc->mflags & DFNextStarted))
1802 afs_PrefetchChunk(avc, tdc, credp, &treq);
1805 ReleaseWriteLock(&avc->lock);
1810 return afs_convert_code(code);
1815 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1816 struct list_head *page_list, unsigned num_pages)
1821 struct iovec* iovecp;
1822 struct nocache_read_request *ancr;
1824 struct pagevec lrupv;
1828 struct inode *ip = FILE_INODE(fp);
1829 struct vcache *avc = VTOAFS(ip);
1830 afs_int32 base_index = 0;
1831 afs_int32 page_count = 0;
1834 /* background thread must free: iovecp, auio, ancr */
1835 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1837 auio = osi_Alloc(sizeof(struct uio));
1838 auio->uio_iov = iovecp;
1839 auio->uio_iovcnt = num_pages;
1840 auio->uio_flag = UIO_READ;
1841 auio->uio_seg = AFS_UIOSYS;
1842 auio->uio_resid = num_pages * PAGE_SIZE;
1844 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1846 ancr->offset = auio->uio_offset;
1847 ancr->length = auio->uio_resid;
1849 pagevec_init(&lrupv, 0);
1851 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1853 if(list_empty(page_list))
1856 pp = list_entry(page_list->prev, struct page, lru);
1857 /* If we allocate a page and don't remove it from page_list,
1858 * the page cache gets upset. */
1860 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1861 if(pp->index > isize) {
1868 offset = page_offset(pp);
1869 auio->uio_offset = offset;
1870 base_index = pp->index;
1872 iovecp[page_ix].iov_len = PAGE_SIZE;
1873 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1874 if(base_index != pp->index) {
1877 page_cache_release(pp);
1878 iovecp[page_ix].iov_base = (void *) 0;
1880 ancr->length -= PAGE_SIZE;
1887 page_cache_release(pp);
1888 iovecp[page_ix].iov_base = (void *) 0;
1891 if(!PageLocked(pp)) {
1895 /* increment page refcount--our original design assumed
1896 * that locking it would effectively pin it; protect
1897 * ourselves from the possiblity that this assumption is
1898 * is faulty, at low cost (provided we do not fail to
1899 * do the corresponding decref on the other side) */
1902 /* save the page for background map */
1903 iovecp[page_ix].iov_base = (void*) pp;
1905 /* and put it on the LRU cache */
1906 if (!pagevec_add(&lrupv, pp))
1907 __pagevec_lru_add_file(&lrupv);
1911 /* If there were useful pages in the page list, make sure all pages
1912 * are in the LRU cache, then schedule the read */
1914 if (pagevec_count(&lrupv))
1915 __pagevec_lru_add_file(&lrupv);
1917 code = afs_ReadNoCache(avc, ancr, credp);
1920 /* If there is nothing for the background thread to handle,
1921 * it won't be freeing the things that we never gave it */
1922 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1923 osi_Free(auio, sizeof(struct uio));
1924 osi_Free(ancr, sizeof(struct nocache_read_request));
1926 /* we do not flush, release, or unmap pages--that will be
1927 * done for us by the background thread as each page comes in
1928 * from the fileserver */
1929 return afs_convert_code(code);
1934 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1936 cred_t *credp = NULL;
1938 struct iovec *iovecp;
1939 struct nocache_read_request *ancr;
1943 * Special case: if page is at or past end of file, just zero it and set
1946 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1947 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1948 SetPageUptodate(pp);
1955 /* receiver frees */
1956 auio = osi_Alloc(sizeof(struct uio));
1957 iovecp = osi_Alloc(sizeof(struct iovec));
1959 /* address can be NULL, because we overwrite it with 'pp', below */
1960 setup_uio(auio, iovecp, NULL, page_offset(pp),
1961 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1963 /* save the page for background map */
1964 get_page(pp); /* see above */
1965 auio->uio_iov->iov_base = (void*) pp;
1966 /* the background thread will free this */
1967 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1969 ancr->offset = page_offset(pp);
1970 ancr->length = PAGE_SIZE;
1973 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1976 return afs_convert_code(code);
1980 afs_linux_can_bypass(struct inode *ip) {
1981 switch(cache_bypass_strategy) {
1982 case NEVER_BYPASS_CACHE:
1984 case ALWAYS_BYPASS_CACHE:
1986 case LARGE_FILES_BYPASS_CACHE:
1987 if(i_size_read(ip) > cache_bypass_threshold)
1994 /* Check if a file is permitted to bypass the cache by policy, and modify
1995 * the cache bypass state recorded for that file */
1998 afs_linux_bypass_check(struct inode *ip) {
2001 int bypass = afs_linux_can_bypass(ip);
2004 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2012 afs_linux_readpage(struct file *fp, struct page *pp)
2016 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2017 code = afs_linux_bypass_readpage(fp, pp);
2019 code = afs_linux_fillpage(fp, pp);
2021 code = afs_linux_prefetch(fp, pp);
2028 /* Readpages reads a number of pages for a particular file. We use
2029 * this to optimise the reading, by limiting the number of times upon which
2030 * we have to lookup, lock and open vcaches and dcaches
2034 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2035 struct list_head *page_list, unsigned int num_pages)
2037 struct inode *inode = mapping->host;
2038 struct vcache *avc = VTOAFS(inode);
2040 struct file *cacheFp = NULL;
2042 unsigned int page_idx;
2044 struct pagevec lrupv;
2045 struct afs_pagecopy_task *task;
2047 if (afs_linux_bypass_check(inode))
2048 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2050 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2054 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2059 ObtainWriteLock(&avc->lock, 912);
2062 task = afs_pagecopy_init_task();
2065 pagevec_init(&lrupv, 0);
2066 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2067 struct page *page = list_entry(page_list->prev, struct page, lru);
2068 list_del(&page->lru);
2069 offset = page_offset(page);
2071 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2073 ReleaseReadLock(&tdc->lock);
2078 filp_close(cacheFp, NULL);
2083 if ((tdc = afs_FindDCache(avc, offset))) {
2084 ObtainReadLock(&tdc->lock);
2085 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2086 (tdc->dflags & DFFetching)) {
2087 ReleaseReadLock(&tdc->lock);
2094 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2097 if (tdc && !add_to_page_cache(page, mapping, page->index,
2099 page_cache_get(page);
2100 if (!pagevec_add(&lrupv, page))
2101 __pagevec_lru_add_file(&lrupv);
2103 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2105 page_cache_release(page);
2107 if (pagevec_count(&lrupv))
2108 __pagevec_lru_add_file(&lrupv);
2111 filp_close(cacheFp, NULL);
2113 afs_pagecopy_put_task(task);
2117 ReleaseReadLock(&tdc->lock);
2121 ReleaseWriteLock(&avc->lock);
2126 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2129 afs_linux_prepare_writeback(struct vcache *avc) {
2130 if (avc->f.states & CPageWrite) {
2131 return AOP_WRITEPAGE_ACTIVATE;
2133 avc->f.states |= CPageWrite;
2138 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2139 struct vrequest treq;
2142 if (!afs_InitReq(&treq, credp))
2143 code = afs_DoPartialWrite(avc, &treq);
2145 return afs_convert_code(code);
2149 afs_linux_complete_writeback(struct vcache *avc) {
2150 avc->f.states &= ~CPageWrite;
2153 /* Writeback a given page syncronously. Called with no AFS locks held */
2155 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2156 unsigned long offset, unsigned int count,
2159 struct vcache *vcp = VTOAFS(ip);
2167 buffer = kmap(pp) + offset;
2168 base = page_offset(pp) + offset;
2171 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2172 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2173 ICL_TYPE_INT32, 99999);
2175 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2177 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2179 i_size_write(ip, vcp->f.m.Length);
2180 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2182 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2184 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2185 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2186 ICL_TYPE_INT32, code);
2195 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2196 unsigned long offset, unsigned int count)
2200 struct vcache *vcp = VTOAFS(ip);
2203 /* Catch recursive writeback. This occurs if the kernel decides
2204 * writeback is required whilst we are writing to the cache, or
2205 * flushing to the server. When we're running syncronously (as
2206 * opposed to from writepage) we can't actually do anything about
2207 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2210 ObtainWriteLock(&vcp->lock, 532);
2211 afs_linux_prepare_writeback(vcp);
2212 ReleaseWriteLock(&vcp->lock);
2216 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2219 ObtainWriteLock(&vcp->lock, 533);
2221 code1 = afs_linux_dopartialwrite(vcp, credp);
2222 afs_linux_complete_writeback(vcp);
2223 ReleaseWriteLock(&vcp->lock);
2234 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2235 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2237 afs_linux_writepage(struct page *pp)
2240 struct address_space *mapping = pp->mapping;
2241 struct inode *inode;
2244 unsigned int to = PAGE_CACHE_SIZE;
2249 if (PageReclaim(pp)) {
2250 return AOP_WRITEPAGE_ACTIVATE;
2251 /* XXX - Do we need to redirty the page here? */
2256 inode = mapping->host;
2257 vcp = VTOAFS(inode);
2258 isize = i_size_read(inode);
2260 /* Don't defeat an earlier truncate */
2261 if (page_offset(pp) > isize) {
2262 set_page_writeback(pp);
2268 ObtainWriteLock(&vcp->lock, 537);
2269 code = afs_linux_prepare_writeback(vcp);
2270 if (code == AOP_WRITEPAGE_ACTIVATE) {
2271 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2272 * to return with the page still locked */
2273 ReleaseWriteLock(&vcp->lock);
2278 /* Grab the creds structure currently held in the vnode, and
2279 * get a reference to it, in case it goes away ... */
2285 ReleaseWriteLock(&vcp->lock);
2288 set_page_writeback(pp);
2290 SetPageUptodate(pp);
2292 /* We can unlock the page here, because it's protected by the
2293 * page_writeback flag. This should make us less vulnerable to
2294 * deadlocking in afs_write and afs_DoPartialWrite
2298 /* If this is the final page, then just write the number of bytes that
2299 * are actually in it */
2300 if ((isize - page_offset(pp)) < to )
2301 to = isize - page_offset(pp);
2303 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2306 ObtainWriteLock(&vcp->lock, 538);
2308 /* As much as we might like to ignore a file server error here,
2309 * and just try again when we close(), unfortunately StoreAllSegments
2310 * will invalidate our chunks if the server returns a permanent error,
2311 * so we need to at least try and get that error back to the user
2314 code1 = afs_linux_dopartialwrite(vcp, credp);
2316 afs_linux_complete_writeback(vcp);
2317 ReleaseWriteLock(&vcp->lock);
2322 end_page_writeback(pp);
2323 page_cache_release(pp);
2334 /* afs_linux_permission
2335 * Check access rights - returns error if can't check or permission denied.
2338 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2339 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2340 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2341 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2343 afs_linux_permission(struct inode *ip, int mode)
2350 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2351 /* We don't support RCU path walking */
2352 if (flags & IPERM_FLAG_RCU)
2358 if (mode & MAY_EXEC)
2360 if (mode & MAY_READ)
2362 if (mode & MAY_WRITE)
2364 code = afs_access(VTOAFS(ip), tmp, credp);
2368 return afs_convert_code(code);
2372 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2376 struct inode *inode = FILE_INODE(file);
2377 loff_t pagebase = page_offset(page);
2379 if (i_size_read(inode) < (pagebase + offset))
2380 i_size_write(inode, pagebase + offset);
2382 if (PageChecked(page)) {
2383 SetPageUptodate(page);
2384 ClearPageChecked(page);
2387 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2393 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2397 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2398 * prepare_write. Essentially, if the page exists within the file,
2399 * and is not being fully written, then we should populate it.
2402 if (!PageUptodate(page)) {
2403 loff_t pagebase = page_offset(page);
2404 loff_t isize = i_size_read(page->mapping->host);
2406 /* Is the location we are writing to beyond the end of the file? */
2407 if (pagebase >= isize ||
2408 ((from == 0) && (pagebase + to) >= isize)) {
2409 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2410 SetPageChecked(page);
2411 /* Are we we writing a full page */
2412 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2413 SetPageChecked(page);
2414 /* Is the page readable, if it's wronly, we don't care, because we're
2415 * not actually going to read from it ... */
2416 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2417 /* We don't care if fillpage fails, because if it does the page
2418 * won't be marked as up to date
2420 afs_linux_fillpage(file, page);
2426 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2428 afs_linux_write_end(struct file *file, struct address_space *mapping,
2429 loff_t pos, unsigned len, unsigned copied,
2430 struct page *page, void *fsdata)
2433 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2435 code = afs_linux_commit_write(file, page, from, from + len);
2438 page_cache_release(page);
2443 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2444 loff_t pos, unsigned len, unsigned flags,
2445 struct page **pagep, void **fsdata)
2448 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2449 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2452 page = grab_cache_page_write_begin(mapping, index, flags);
2455 code = afs_linux_prepare_write(file, page, from, from + len);
2458 page_cache_release(page);
2466 static struct inode_operations afs_file_iops = {
2467 .permission = afs_linux_permission,
2468 .getattr = afs_linux_getattr,
2469 .setattr = afs_notify_change,
2472 static struct address_space_operations afs_file_aops = {
2473 .readpage = afs_linux_readpage,
2474 .readpages = afs_linux_readpages,
2475 .writepage = afs_linux_writepage,
2476 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2477 .write_begin = afs_linux_write_begin,
2478 .write_end = afs_linux_write_end,
2480 .commit_write = afs_linux_commit_write,
2481 .prepare_write = afs_linux_prepare_write,
2486 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2487 * by what sort of operation is allowed.....
2490 static struct inode_operations afs_dir_iops = {
2491 .setattr = afs_notify_change,
2492 .create = afs_linux_create,
2493 .lookup = afs_linux_lookup,
2494 .link = afs_linux_link,
2495 .unlink = afs_linux_unlink,
2496 .symlink = afs_linux_symlink,
2497 .mkdir = afs_linux_mkdir,
2498 .rmdir = afs_linux_rmdir,
2499 .rename = afs_linux_rename,
2500 .getattr = afs_linux_getattr,
2501 .permission = afs_linux_permission,
2504 /* We really need a separate symlink set of ops, since do_follow_link()
2505 * determines if it _is_ a link by checking if the follow_link op is set.
2507 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2509 afs_symlink_filler(struct file *file, struct page *page)
2511 struct inode *ip = (struct inode *)page->mapping->host;
2512 char *p = (char *)kmap(page);
2516 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2521 p[code] = '\0'; /* null terminate? */
2523 SetPageUptodate(page);
2535 static struct address_space_operations afs_symlink_aops = {
2536 .readpage = afs_symlink_filler
2538 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2540 static struct inode_operations afs_symlink_iops = {
2541 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2542 .readlink = page_readlink,
2543 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2544 .follow_link = page_follow_link,
2546 .follow_link = page_follow_link_light,
2547 .put_link = page_put_link,
2549 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2550 .readlink = afs_linux_readlink,
2551 .follow_link = afs_linux_follow_link,
2552 .put_link = afs_linux_put_link,
2553 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2554 .setattr = afs_notify_change,
2558 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2562 vattr2inode(ip, vattr);
2564 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2565 /* Reset ops if symlink or directory. */
2566 if (S_ISREG(ip->i_mode)) {
2567 ip->i_op = &afs_file_iops;
2568 ip->i_fop = &afs_file_fops;
2569 ip->i_data.a_ops = &afs_file_aops;
2571 } else if (S_ISDIR(ip->i_mode)) {
2572 ip->i_op = &afs_dir_iops;
2573 ip->i_fop = &afs_dir_fops;
2575 } else if (S_ISLNK(ip->i_mode)) {
2576 ip->i_op = &afs_symlink_iops;
2577 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2578 ip->i_data.a_ops = &afs_symlink_aops;
2579 ip->i_mapping = &ip->i_data;