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 ObtainWriteLock(&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 ReleaseWriteLock(&avc->lock);
312 afs_osi_Sleep(&tdc->validPos);
313 ObtainWriteLock(&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 ReleaseWriteLock(&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 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
344 afs_warn("Corrupt directory (inode %lx, dirpos %d)",
345 (unsigned long)&tdc->f.inode, dirpos);
346 ReleaseSharedLock(&avc->lock);
352 de = (struct DirEntry *)entry.data;
353 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
354 ntohl(de->fid.vnode));
355 len = strlen(de->name);
357 /* filldir returns -EINVAL when the buffer is full. */
359 unsigned int type = DT_UNKNOWN;
360 struct VenusFid afid;
363 afid.Cell = avc->f.fid.Cell;
364 afid.Fid.Volume = avc->f.fid.Fid.Volume;
365 afid.Fid.Vnode = ntohl(de->fid.vnode);
366 afid.Fid.Unique = ntohl(de->fid.vunique);
367 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
369 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
372 } else if (((tvc->f.states) & (CStatd | CTruth))) {
373 /* CTruth will be set if the object has
378 else if (vtype == VREG)
380 /* Don't do this until we're sure it can't be a mtpt */
381 /* else if (vtype == VLNK)
383 /* what other types does AFS support? */
385 /* clean up from afs_FindVCache */
389 * If this is NFS readdirplus, then the filler is going to
390 * call getattr on this inode, which will deadlock if we're
394 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
400 offset = dirpos + 1 + ((len + 16) >> 5);
402 /* If filldir didn't fill in the last one this is still pointing to that
405 fp->f_pos = (loff_t) offset;
407 ReleaseReadLock(&tdc->lock);
409 UpgradeSToWLock(&avc->lock, 813);
410 avc->f.states &= ~CReadDir;
412 avc->readdir_pid = 0;
413 ReleaseSharedLock(&avc->lock);
417 afs_PutFakeStat(&fakestat);
424 /* in afs_pioctl.c */
425 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
428 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
429 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
431 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
438 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
440 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
444 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
445 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
446 vmap->vm_end - vmap->vm_start);
448 /* get a validated vcache entry */
449 code = afs_linux_VerifyVCache(vcp, NULL);
452 /* Linux's Flushpage implementation doesn't use credp, so optimise
453 * our code to not need to crref() it */
454 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
456 code = generic_file_mmap(fp, vmap);
459 vcp->f.states |= CMAPPED;
467 afs_linux_open(struct inode *ip, struct file *fp)
469 struct vcache *vcp = VTOAFS(ip);
470 cred_t *credp = crref();
474 code = afs_open(&vcp, fp->f_flags, credp);
478 return afs_convert_code(code);
482 afs_linux_release(struct inode *ip, struct file *fp)
484 struct vcache *vcp = VTOAFS(ip);
485 cred_t *credp = crref();
489 code = afs_close(vcp, fp->f_flags, credp);
490 ObtainWriteLock(&vcp->lock, 807);
495 ReleaseWriteLock(&vcp->lock);
499 return afs_convert_code(code);
503 #if defined(FOP_FSYNC_TAKES_DENTRY)
504 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
506 afs_linux_fsync(struct file *fp, int datasync)
510 struct inode *ip = FILE_INODE(fp);
511 cred_t *credp = crref();
514 code = afs_fsync(VTOAFS(ip), credp);
517 return afs_convert_code(code);
523 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
526 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
527 cred_t *credp = crref();
528 struct AFS_FLOCK flock;
530 /* Convert to a lock format afs_lockctl understands. */
531 memset(&flock, 0, sizeof(flock));
532 flock.l_type = flp->fl_type;
533 flock.l_pid = flp->fl_pid;
535 flock.l_start = flp->fl_start;
536 if (flp->fl_end == OFFSET_MAX)
537 flock.l_len = 0; /* Lock to end of file */
539 flock.l_len = flp->fl_end - flp->fl_start + 1;
541 /* Safe because there are no large files, yet */
542 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
543 if (cmd == F_GETLK64)
545 else if (cmd == F_SETLK64)
547 else if (cmd == F_SETLKW64)
549 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
552 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
555 if ((code == 0 || flp->fl_type == F_UNLCK) &&
556 (cmd == F_SETLK || cmd == F_SETLKW)) {
557 code = afs_posix_lock_file(fp, flp);
558 if (code && flp->fl_type != F_UNLCK) {
559 struct AFS_FLOCK flock2;
561 flock2.l_type = F_UNLCK;
563 afs_lockctl(vcp, &flock2, F_SETLK, credp);
567 /* If lockctl says there are no conflicting locks, then also check with the
568 * kernel, as lockctl knows nothing about byte range locks
570 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
571 afs_posix_test_lock(fp, flp);
572 /* If we found a lock in the kernel's structure, return it */
573 if (flp->fl_type != F_UNLCK) {
579 /* Convert flock back to Linux's file_lock */
580 flp->fl_type = flock.l_type;
581 flp->fl_pid = flock.l_pid;
582 flp->fl_start = flock.l_start;
583 if (flock.l_len == 0)
584 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
586 flp->fl_end = flock.l_start + flock.l_len - 1;
592 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
594 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
596 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
597 cred_t *credp = crref();
598 struct AFS_FLOCK flock;
599 /* Convert to a lock format afs_lockctl understands. */
600 memset(&flock, 0, sizeof(flock));
601 flock.l_type = flp->fl_type;
602 flock.l_pid = flp->fl_pid;
607 /* Safe because there are no large files, yet */
608 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
609 if (cmd == F_GETLK64)
611 else if (cmd == F_SETLK64)
613 else if (cmd == F_SETLKW64)
615 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
618 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
621 if ((code == 0 || flp->fl_type == F_UNLCK) &&
622 (cmd == F_SETLK || cmd == F_SETLKW)) {
623 flp->fl_flags &=~ FL_SLEEP;
624 code = flock_lock_file_wait(fp, flp);
625 if (code && flp->fl_type != F_UNLCK) {
626 struct AFS_FLOCK flock2;
628 flock2.l_type = F_UNLCK;
630 afs_lockctl(vcp, &flock2, F_SETLK, credp);
634 /* Convert flock back to Linux's file_lock */
635 flp->fl_type = flock.l_type;
636 flp->fl_pid = flock.l_pid;
644 * essentially the same as afs_fsync() but we need to get the return
645 * code for the sys_close() here, not afs_linux_release(), so call
646 * afs_StoreAllSegments() with AFS_LASTSTORE
649 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
650 afs_linux_flush(struct file *fp, fl_owner_t id)
652 afs_linux_flush(struct file *fp)
655 struct vrequest treq;
663 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
671 vcp = VTOAFS(FILE_INODE(fp));
673 code = afs_InitReq(&treq, credp);
676 /* If caching is bypassed for this file, or globally, just return 0 */
677 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
680 ObtainReadLock(&vcp->lock);
681 if (vcp->cachingStates & FCSBypass)
683 ReleaseReadLock(&vcp->lock);
686 /* future proof: don't rely on 0 return from afs_InitReq */
691 ObtainSharedLock(&vcp->lock, 535);
692 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
693 UpgradeSToWLock(&vcp->lock, 536);
694 if (!AFS_IS_DISCONNECTED) {
695 code = afs_StoreAllSegments(vcp,
697 AFS_SYNC | AFS_LASTSTORE);
699 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
701 ConvertWToSLock(&vcp->lock);
703 code = afs_CheckCode(code, &treq, 54);
704 ReleaseSharedLock(&vcp->lock);
711 return afs_convert_code(code);
714 struct file_operations afs_dir_fops = {
715 .read = generic_read_dir,
716 .readdir = afs_linux_readdir,
717 #ifdef HAVE_UNLOCKED_IOCTL
718 .unlocked_ioctl = afs_unlocked_xioctl,
722 #ifdef HAVE_COMPAT_IOCTL
723 .compat_ioctl = afs_unlocked_xioctl,
725 .open = afs_linux_open,
726 .release = afs_linux_release,
727 .llseek = default_llseek,
730 struct file_operations afs_file_fops = {
731 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
732 .aio_read = afs_linux_aio_read,
733 .aio_write = afs_linux_aio_write,
735 .read = afs_linux_read,
736 .write = afs_linux_write,
738 #ifdef HAVE_UNLOCKED_IOCTL
739 .unlocked_ioctl = afs_unlocked_xioctl,
743 #ifdef HAVE_COMPAT_IOCTL
744 .compat_ioctl = afs_unlocked_xioctl,
746 .mmap = afs_linux_mmap,
747 .open = afs_linux_open,
748 .flush = afs_linux_flush,
749 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
750 .sendfile = generic_file_sendfile,
752 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
753 .splice_write = generic_file_splice_write,
754 .splice_read = generic_file_splice_read,
756 .release = afs_linux_release,
757 .fsync = afs_linux_fsync,
758 .lock = afs_linux_lock,
759 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
760 .flock = afs_linux_flock,
762 .llseek = default_llseek,
766 /**********************************************************************
767 * AFS Linux dentry operations
768 **********************************************************************/
770 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
771 * that has its mvid (parent dir's fid) pointer set to the wrong directory
772 * due to being mounted in multiple points at once. fix_bad_parent()
773 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
774 * dotdotfid and mtpoint fid members.
776 * dp - dentry to be checked.
777 * credp - credentials
778 * vcp, pvc - item's and parent's vcache pointer
782 * This dentry's vcache's mvid will be set to the correct parent directory's
784 * This root vnode's volume will have its dotdotfid and mtpoint fids set
785 * to the correct parent and mountpoint fids.
789 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
791 struct vcache *avc = NULL;
793 /* force a lookup, so vcp->mvid is fixed up */
794 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
795 if (!avc || vcp != avc) { /* bad, very bad.. */
796 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
797 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
798 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
799 ICL_TYPE_POINTER, dp);
802 AFS_RELE(AFSTOV(avc));
807 /* afs_linux_revalidate
808 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
811 afs_linux_revalidate(struct dentry *dp)
814 struct vcache *vcp = VTOAFS(dp->d_inode);
818 if (afs_shuttingdown)
824 /* Make this a fast path (no crref), since it's called so often. */
825 if (vcp->states & CStatd) {
826 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
828 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
829 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
832 fix_bad_parent(dp); /* check and correct mvid */
841 /* This avoids the crref when we don't have to do it. Watch for
842 * changes in afs_getattr that don't get replicated here!
844 if (vcp->f.states & CStatd &&
845 (!afs_fakestat_enable || vcp->mvstat != 1) &&
847 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
848 code = afs_CopyOutAttrs(vcp, &vattr);
851 code = afs_getattr(vcp, &vattr, credp);
856 afs_fill_inode(AFSTOV(vcp), &vattr);
860 return afs_convert_code(code);
864 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
866 int err = afs_linux_revalidate(dentry);
868 generic_fillattr(dentry->d_inode, stat);
873 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
874 * In kernels 2.2.10 and above, we are passed an additional flags var which
875 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
876 * we are advised to follow the entry if it is a link or to make sure that
877 * it is a directory. But since the kernel itself checks these possibilities
878 * later on, we shouldn't have to do it until later. Perhaps in the future..
880 * The code here assumes that on entry the global lock is not held
883 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
884 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
886 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
890 cred_t *credp = NULL;
891 struct vcache *vcp, *pvcp, *tvc = NULL;
892 struct dentry *parent;
894 struct afs_fakestat_state fakestate;
898 /* We don't support RCU path walking */
899 if (nd->flags & LOOKUP_RCU)
903 afs_InitFakeStat(&fakestate);
906 vcp = VTOAFS(dp->d_inode);
908 if (vcp == afs_globalVp)
911 parent = dget_parent(dp);
912 pvcp = VTOAFS(parent->d_inode);
914 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
920 if (locked && vcp->mvstat == 1) { /* mount point */
921 if (vcp->mvid && (vcp->f.states & CMValid)) {
924 struct vrequest treq;
926 code = afs_InitReq(&treq, credp);
928 (strcmp(dp->d_name.name, ".directory") == 0)) {
932 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
934 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
935 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
936 /* a mount point, not yet replaced by its directory */
941 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
942 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
943 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
948 /* If the last looker changes, we should make sure the current
949 * looker still has permission to examine this file. This would
950 * always require a crref() which would be "slow".
952 if (vcp->last_looker != treq.uid) {
953 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
956 vcp->last_looker = treq.uid;
961 /* If the parent's DataVersion has changed or the vnode
962 * is longer valid, we need to do a full lookup. VerifyVCache
963 * isn't enough since the vnode may have been renamed.
966 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
972 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
973 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
974 if (!tvc || tvc != vcp) {
979 if (afs_getattr(vcp, &vattr, credp)) {
984 vattr2inode(AFSTOV(vcp), &vattr);
985 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
988 /* should we always update the attributes at this point? */
989 /* unlikely--the vcache entry hasn't changed */
994 /* If this code is ever enabled, we should use dget_parent to handle
995 * getting the parent, and dput() to dispose of it. See above for an
997 pvcp = VTOAFS(dp->d_parent->d_inode);
998 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1002 /* No change in parent's DataVersion so this negative
1003 * lookup is still valid. BUT, if a server is down a
1004 * negative lookup can result so there should be a
1005 * liftime as well. For now, always expire.
1018 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1020 /* we hold the global lock if we evaluated a mount point */
1027 shrink_dcache_parent(dp);
1033 if (have_submounts(dp))
1041 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1043 struct vcache *vcp = VTOAFS(ip);
1046 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1047 (void) afs_InactiveVCache(vcp, NULL);
1050 afs_linux_clear_nfsfs_renamed(dp);
1056 afs_dentry_delete(struct dentry *dp)
1058 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1059 return 1; /* bad inode? */
1064 struct dentry_operations afs_dentry_operations = {
1065 .d_revalidate = afs_linux_dentry_revalidate,
1066 .d_delete = afs_dentry_delete,
1067 .d_iput = afs_dentry_iput,
1070 /**********************************************************************
1071 * AFS Linux inode operations
1072 **********************************************************************/
1076 * Merely need to set enough of vattr to get us through the create. Note
1077 * that the higher level code (open_namei) will take care of any tuncation
1078 * explicitly. Exclusive open is also taken care of in open_namei.
1080 * name is in kernel space at this point.
1083 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1084 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1085 struct nameidata *nd)
1087 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1091 cred_t *credp = crref();
1092 const char *name = dp->d_name.name;
1097 vattr.va_mode = mode;
1098 vattr.va_type = mode & S_IFMT;
1101 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1105 struct inode *ip = AFSTOV(vcp);
1107 afs_getattr(vcp, &vattr, credp);
1108 afs_fill_inode(ip, &vattr);
1109 insert_inode_hash(ip);
1110 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1111 dp->d_op = &afs_dentry_operations;
1113 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1114 d_instantiate(dp, ip);
1119 return afs_convert_code(code);
1122 /* afs_linux_lookup */
1123 static struct dentry *
1124 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1125 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1126 struct nameidata *nd)
1128 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1131 cred_t *credp = crref();
1132 struct vcache *vcp = NULL;
1133 const char *comp = dp->d_name.name;
1134 struct inode *ip = NULL;
1135 struct dentry *newdp = NULL;
1139 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1145 afs_getattr(vcp, &vattr, credp);
1146 afs_fill_inode(ip, &vattr);
1147 if (hlist_unhashed(&ip->i_hash))
1148 insert_inode_hash(ip);
1150 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1151 dp->d_op = &afs_dentry_operations;
1153 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1156 if (ip && S_ISDIR(ip->i_mode)) {
1157 struct dentry *alias;
1159 /* Try to invalidate an existing alias in favor of our new one */
1160 alias = d_find_alias(ip);
1161 /* But not if it's disconnected; then we want d_splice_alias below */
1162 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1163 if (d_invalidate(alias) == 0) {
1172 newdp = d_splice_alias(ip, dp);
1176 /* It's ok for the file to not be found. That's noted by the caller by
1177 * seeing that the dp->d_inode field is NULL.
1179 if (!code || code == ENOENT)
1182 return ERR_PTR(afs_convert_code(code));
1186 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1189 cred_t *credp = crref();
1190 const char *name = newdp->d_name.name;
1191 struct inode *oldip = olddp->d_inode;
1193 /* If afs_link returned the vnode, we could instantiate the
1194 * dentry. Since it's not, we drop this one and do a new lookup.
1199 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1203 return afs_convert_code(code);
1206 /* We have to have a Linux specific sillyrename function, because we
1207 * also have to keep the dcache up to date when we're doing a silly
1208 * rename - so we don't want the generic vnodeops doing this behind our
1213 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1216 struct vcache *tvc = VTOAFS(dentry->d_inode);
1217 struct dentry *__dp = NULL;
1218 char *__name = NULL;
1221 if (afs_linux_nfsfs_renamed(dentry))
1229 osi_FreeSmallSpace(__name);
1230 __name = afs_newname();
1233 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1236 osi_FreeSmallSpace(__name);
1239 } while (__dp->d_inode != NULL);
1242 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1243 VTOAFS(dir), (char *)__dp->d_name.name,
1246 tvc->mvid = (void *) __name;
1249 crfree(tvc->uncred);
1251 tvc->uncred = credp;
1252 tvc->f.states |= CUnlinked;
1253 afs_linux_set_nfsfs_renamed(dentry);
1255 osi_FreeSmallSpace(__name);
1260 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1261 d_move(dentry, __dp);
1270 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1273 cred_t *credp = crref();
1274 const char *name = dp->d_name.name;
1275 struct vcache *tvc = VTOAFS(dp->d_inode);
1277 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1278 && !(tvc->f.states & CUnlinked)) {
1280 code = afs_linux_sillyrename(dip, dp, credp);
1283 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1290 return afs_convert_code(code);
1295 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1298 cred_t *credp = crref();
1300 const char *name = dp->d_name.name;
1302 /* If afs_symlink returned the vnode, we could instantiate the
1303 * dentry. Since it's not, we drop this one and do a new lookup.
1309 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1312 return afs_convert_code(code);
1316 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1319 cred_t *credp = crref();
1320 struct vcache *tvcp = NULL;
1322 const char *name = dp->d_name.name;
1325 vattr.va_mask = ATTR_MODE;
1326 vattr.va_mode = mode;
1328 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1331 struct inode *ip = AFSTOV(tvcp);
1333 afs_getattr(tvcp, &vattr, credp);
1334 afs_fill_inode(ip, &vattr);
1336 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1337 dp->d_op = &afs_dentry_operations;
1339 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1340 d_instantiate(dp, ip);
1345 return afs_convert_code(code);
1349 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1352 cred_t *credp = crref();
1353 const char *name = dp->d_name.name;
1355 /* locking kernel conflicts with glock? */
1358 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1361 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1362 * that failed because a directory is not empty. So, we map
1363 * EEXIST to ENOTEMPTY on linux.
1365 if (code == EEXIST) {
1374 return afs_convert_code(code);
1379 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1380 struct inode *newip, struct dentry *newdp)
1383 cred_t *credp = crref();
1384 const char *oldname = olddp->d_name.name;
1385 const char *newname = newdp->d_name.name;
1386 struct dentry *rehash = NULL;
1388 /* Prevent any new references during rename operation. */
1390 if (!d_unhashed(newdp)) {
1395 #if defined(D_COUNT_INT)
1396 spin_lock(&olddp->d_lock);
1397 if (olddp->d_count > 1) {
1398 spin_unlock(&olddp->d_lock);
1399 shrink_dcache_parent(olddp);
1401 spin_unlock(&olddp->d_lock);
1403 if (atomic_read(&olddp->d_count) > 1)
1404 shrink_dcache_parent(olddp);
1408 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1412 olddp->d_time = 0; /* force to revalidate */
1418 return afs_convert_code(code);
1422 /* afs_linux_ireadlink
1423 * Internal readlink which can return link contents to user or kernel space.
1424 * Note that the buffer is NOT supposed to be null-terminated.
1427 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1430 cred_t *credp = crref();
1434 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1435 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1439 return maxlen - tuio.uio_resid;
1441 return afs_convert_code(code);
1444 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1445 /* afs_linux_readlink
1446 * Fill target (which is in user space) with contents of symlink.
1449 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1452 struct inode *ip = dp->d_inode;
1455 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1461 /* afs_linux_follow_link
1462 * a file system dependent link following routine.
1464 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1469 name = kmalloc(PATH_MAX, GFP_NOFS);
1475 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1483 nd_set_link(nd, name);
1488 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1490 char *name = nd_get_link(nd);
1492 if (name && !IS_ERR(name))
1496 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1498 /* Populate a page by filling it from the cache file pointed at by cachefp
1499 * (which contains indicated chunk)
1500 * If task is NULL, the page copy occurs syncronously, and the routine
1501 * returns with page still locked. If task is non-NULL, then page copies
1502 * may occur in the background, and the page will be unlocked when it is
1506 afs_linux_read_cache(struct file *cachefp, struct page *page,
1507 int chunk, struct pagevec *lrupv,
1508 struct afs_pagecopy_task *task) {
1509 loff_t offset = page_offset(page);
1510 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1511 struct page *newpage, *cachepage;
1512 struct address_space *cachemapping;
1516 cachemapping = cacheinode->i_mapping;
1520 /* If we're trying to read a page that's past the end of the disk
1521 * cache file, then just return a zeroed page */
1522 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1523 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1524 SetPageUptodate(page);
1530 /* From our offset, we now need to work out which page in the disk
1531 * file it corresponds to. This will be fun ... */
1532 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1534 while (cachepage == NULL) {
1535 cachepage = find_get_page(cachemapping, pageindex);
1538 newpage = page_cache_alloc_cold(cachemapping);
1544 code = add_to_page_cache(newpage, cachemapping,
1545 pageindex, GFP_KERNEL);
1547 cachepage = newpage;
1550 page_cache_get(cachepage);
1551 if (!pagevec_add(lrupv, cachepage))
1552 __pagevec_lru_add_file(lrupv);
1555 page_cache_release(newpage);
1557 if (code != -EEXIST)
1561 lock_page(cachepage);
1565 if (!PageUptodate(cachepage)) {
1566 ClearPageError(cachepage);
1567 code = cachemapping->a_ops->readpage(NULL, cachepage);
1568 if (!code && !task) {
1569 wait_on_page_locked(cachepage);
1572 unlock_page(cachepage);
1576 if (PageUptodate(cachepage)) {
1577 copy_highpage(page, cachepage);
1578 flush_dcache_page(page);
1579 SetPageUptodate(page);
1584 afs_pagecopy_queue_page(task, cachepage, page);
1596 page_cache_release(cachepage);
1602 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1604 loff_t offset = page_offset(pp);
1605 struct inode *ip = FILE_INODE(fp);
1606 struct vcache *avc = VTOAFS(ip);
1608 struct file *cacheFp = NULL;
1611 struct pagevec lrupv;
1613 /* Not a UFS cache, don't do anything */
1614 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1617 /* Can't do anything if the vcache isn't statd , or if the read
1618 * crosses a chunk boundary.
1620 if (!(avc->f.states & CStatd) ||
1621 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1625 ObtainWriteLock(&avc->lock, 911);
1627 /* XXX - See if hinting actually makes things faster !!! */
1629 /* See if we have a suitable entry already cached */
1633 /* We need to lock xdcache, then dcache, to handle situations where
1634 * the hint is on the free list. However, we can't safely do this
1635 * according to the locking hierarchy. So, use a non blocking lock.
1637 ObtainReadLock(&afs_xdcache);
1638 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1640 if (dcLocked && (tdc->index != NULLIDX)
1641 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1642 && tdc->f.chunk == AFS_CHUNK(offset)
1643 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1644 /* Bonus - the hint was correct */
1647 /* Only destroy the hint if its actually invalid, not if there's
1648 * just been a locking failure */
1650 ReleaseReadLock(&tdc->lock);
1657 ReleaseReadLock(&afs_xdcache);
1660 /* No hint, or hint is no longer valid - see if we can get something
1661 * directly from the dcache
1664 tdc = afs_FindDCache(avc, offset);
1667 ReleaseWriteLock(&avc->lock);
1672 ObtainReadLock(&tdc->lock);
1674 /* Is the dcache we've been given currently up to date */
1675 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1676 (tdc->dflags & DFFetching)) {
1677 ReleaseWriteLock(&avc->lock);
1678 ReleaseReadLock(&tdc->lock);
1683 /* Update our hint for future abuse */
1686 /* Okay, so we've now got a cache file that is up to date */
1688 /* XXX - I suspect we should be locking the inodes before we use them! */
1690 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1691 pagevec_init(&lrupv, 0);
1693 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1695 if (pagevec_count(&lrupv))
1696 __pagevec_lru_add_file(&lrupv);
1698 filp_close(cacheFp, NULL);
1701 ReleaseReadLock(&tdc->lock);
1702 ReleaseWriteLock(&avc->lock);
1709 /* afs_linux_readpage
1711 * This function is split into two, because prepare_write/begin_write
1712 * require a readpage call which doesn't unlock the resulting page upon
1716 afs_linux_fillpage(struct file *fp, struct page *pp)
1721 struct iovec *iovecp;
1722 struct inode *ip = FILE_INODE(fp);
1723 afs_int32 cnt = page_count(pp);
1724 struct vcache *avc = VTOAFS(ip);
1725 afs_offs_t offset = page_offset(pp);
1729 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1739 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
1740 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
1742 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1747 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1748 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1749 99999); /* not a possible code value */
1751 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1753 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1754 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1756 AFS_DISCON_UNLOCK();
1759 /* XXX valid for no-cache also? Check last bits of files... :)
1760 * Cognate code goes in afs_NoCacheFetchProc. */
1761 if (auio->uio_resid) /* zero remainder of page */
1762 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1765 flush_dcache_page(pp);
1766 SetPageUptodate(pp);
1775 return afs_convert_code(code);
1779 afs_linux_prefetch(struct file *fp, struct page *pp)
1782 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1783 afs_offs_t offset = page_offset(pp);
1785 if (AFS_CHUNKOFFSET(offset) == 0) {
1787 struct vrequest treq;
1792 code = afs_InitReq(&treq, credp);
1793 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1794 tdc = afs_FindDCache(avc, offset);
1796 if (!(tdc->mflags & DFNextStarted))
1797 afs_PrefetchChunk(avc, tdc, credp, &treq);
1800 ReleaseWriteLock(&avc->lock);
1805 return afs_convert_code(code);
1810 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1811 struct list_head *page_list, unsigned num_pages)
1816 struct iovec* iovecp;
1817 struct nocache_read_request *ancr;
1819 struct pagevec lrupv;
1823 struct inode *ip = FILE_INODE(fp);
1824 struct vcache *avc = VTOAFS(ip);
1825 afs_int32 base_index = 0;
1826 afs_int32 page_count = 0;
1829 /* background thread must free: iovecp, auio, ancr */
1830 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1832 auio = osi_Alloc(sizeof(struct uio));
1833 auio->uio_iov = iovecp;
1834 auio->uio_iovcnt = num_pages;
1835 auio->uio_flag = UIO_READ;
1836 auio->uio_seg = AFS_UIOSYS;
1837 auio->uio_resid = num_pages * PAGE_SIZE;
1839 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1841 ancr->offset = auio->uio_offset;
1842 ancr->length = auio->uio_resid;
1844 pagevec_init(&lrupv, 0);
1846 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1848 if(list_empty(page_list))
1851 pp = list_entry(page_list->prev, struct page, lru);
1852 /* If we allocate a page and don't remove it from page_list,
1853 * the page cache gets upset. */
1855 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1856 if(pp->index > isize) {
1863 offset = page_offset(pp);
1864 auio->uio_offset = offset;
1865 base_index = pp->index;
1867 iovecp[page_ix].iov_len = PAGE_SIZE;
1868 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1869 if(base_index != pp->index) {
1872 page_cache_release(pp);
1873 iovecp[page_ix].iov_base = (void *) 0;
1875 ancr->length -= PAGE_SIZE;
1882 page_cache_release(pp);
1883 iovecp[page_ix].iov_base = (void *) 0;
1886 if(!PageLocked(pp)) {
1890 /* increment page refcount--our original design assumed
1891 * that locking it would effectively pin it; protect
1892 * ourselves from the possiblity that this assumption is
1893 * is faulty, at low cost (provided we do not fail to
1894 * do the corresponding decref on the other side) */
1897 /* save the page for background map */
1898 iovecp[page_ix].iov_base = (void*) pp;
1900 /* and put it on the LRU cache */
1901 if (!pagevec_add(&lrupv, pp))
1902 __pagevec_lru_add_file(&lrupv);
1906 /* If there were useful pages in the page list, make sure all pages
1907 * are in the LRU cache, then schedule the read */
1909 if (pagevec_count(&lrupv))
1910 __pagevec_lru_add_file(&lrupv);
1912 code = afs_ReadNoCache(avc, ancr, credp);
1915 /* If there is nothing for the background thread to handle,
1916 * it won't be freeing the things that we never gave it */
1917 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1918 osi_Free(auio, sizeof(struct uio));
1919 osi_Free(ancr, sizeof(struct nocache_read_request));
1921 /* we do not flush, release, or unmap pages--that will be
1922 * done for us by the background thread as each page comes in
1923 * from the fileserver */
1924 return afs_convert_code(code);
1929 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1931 cred_t *credp = NULL;
1933 struct iovec *iovecp;
1934 struct nocache_read_request *ancr;
1938 * Special case: if page is at or past end of file, just zero it and set
1941 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1942 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1943 SetPageUptodate(pp);
1950 /* receiver frees */
1951 auio = osi_Alloc(sizeof(struct uio));
1952 iovecp = osi_Alloc(sizeof(struct iovec));
1954 /* address can be NULL, because we overwrite it with 'pp', below */
1955 setup_uio(auio, iovecp, NULL, page_offset(pp),
1956 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1958 /* save the page for background map */
1959 get_page(pp); /* see above */
1960 auio->uio_iov->iov_base = (void*) pp;
1961 /* the background thread will free this */
1962 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1964 ancr->offset = page_offset(pp);
1965 ancr->length = PAGE_SIZE;
1968 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1971 return afs_convert_code(code);
1975 afs_linux_can_bypass(struct inode *ip) {
1976 switch(cache_bypass_strategy) {
1977 case NEVER_BYPASS_CACHE:
1979 case ALWAYS_BYPASS_CACHE:
1981 case LARGE_FILES_BYPASS_CACHE:
1982 if(i_size_read(ip) > cache_bypass_threshold)
1989 /* Check if a file is permitted to bypass the cache by policy, and modify
1990 * the cache bypass state recorded for that file */
1993 afs_linux_bypass_check(struct inode *ip) {
1996 int bypass = afs_linux_can_bypass(ip);
1999 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2007 afs_linux_readpage(struct file *fp, struct page *pp)
2011 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2012 code = afs_linux_bypass_readpage(fp, pp);
2014 code = afs_linux_fillpage(fp, pp);
2016 code = afs_linux_prefetch(fp, pp);
2023 /* Readpages reads a number of pages for a particular file. We use
2024 * this to optimise the reading, by limiting the number of times upon which
2025 * we have to lookup, lock and open vcaches and dcaches
2029 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2030 struct list_head *page_list, unsigned int num_pages)
2032 struct inode *inode = mapping->host;
2033 struct vcache *avc = VTOAFS(inode);
2035 struct file *cacheFp = NULL;
2037 unsigned int page_idx;
2039 struct pagevec lrupv;
2040 struct afs_pagecopy_task *task;
2042 if (afs_linux_bypass_check(inode))
2043 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2045 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2049 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2054 ObtainWriteLock(&avc->lock, 912);
2057 task = afs_pagecopy_init_task();
2060 pagevec_init(&lrupv, 0);
2061 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2062 struct page *page = list_entry(page_list->prev, struct page, lru);
2063 list_del(&page->lru);
2064 offset = page_offset(page);
2066 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2068 ReleaseReadLock(&tdc->lock);
2073 filp_close(cacheFp, NULL);
2078 if ((tdc = afs_FindDCache(avc, offset))) {
2079 ObtainReadLock(&tdc->lock);
2080 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2081 (tdc->dflags & DFFetching)) {
2082 ReleaseReadLock(&tdc->lock);
2089 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2092 if (tdc && !add_to_page_cache(page, mapping, page->index,
2094 page_cache_get(page);
2095 if (!pagevec_add(&lrupv, page))
2096 __pagevec_lru_add_file(&lrupv);
2098 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2100 page_cache_release(page);
2102 if (pagevec_count(&lrupv))
2103 __pagevec_lru_add_file(&lrupv);
2106 filp_close(cacheFp, NULL);
2108 afs_pagecopy_put_task(task);
2112 ReleaseReadLock(&tdc->lock);
2116 ReleaseWriteLock(&avc->lock);
2121 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2124 afs_linux_prepare_writeback(struct vcache *avc) {
2125 if (avc->f.states & CPageWrite) {
2126 return AOP_WRITEPAGE_ACTIVATE;
2128 avc->f.states |= CPageWrite;
2133 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2134 struct vrequest treq;
2137 if (!afs_InitReq(&treq, credp))
2138 code = afs_DoPartialWrite(avc, &treq);
2140 return afs_convert_code(code);
2144 afs_linux_complete_writeback(struct vcache *avc) {
2145 avc->f.states &= ~CPageWrite;
2148 /* Writeback a given page syncronously. Called with no AFS locks held */
2150 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2151 unsigned long offset, unsigned int count,
2154 struct vcache *vcp = VTOAFS(ip);
2162 buffer = kmap(pp) + offset;
2163 base = page_offset(pp) + offset;
2166 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2167 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2168 ICL_TYPE_INT32, 99999);
2170 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2172 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2174 i_size_write(ip, vcp->f.m.Length);
2175 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2177 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2179 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2180 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2181 ICL_TYPE_INT32, code);
2190 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2191 unsigned long offset, unsigned int count)
2195 struct vcache *vcp = VTOAFS(ip);
2198 /* Catch recursive writeback. This occurs if the kernel decides
2199 * writeback is required whilst we are writing to the cache, or
2200 * flushing to the server. When we're running syncronously (as
2201 * opposed to from writepage) we can't actually do anything about
2202 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2205 ObtainWriteLock(&vcp->lock, 532);
2206 afs_linux_prepare_writeback(vcp);
2207 ReleaseWriteLock(&vcp->lock);
2211 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2214 ObtainWriteLock(&vcp->lock, 533);
2216 code1 = afs_linux_dopartialwrite(vcp, credp);
2217 afs_linux_complete_writeback(vcp);
2218 ReleaseWriteLock(&vcp->lock);
2229 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2230 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2232 afs_linux_writepage(struct page *pp)
2235 struct address_space *mapping = pp->mapping;
2236 struct inode *inode;
2239 unsigned int to = PAGE_CACHE_SIZE;
2244 if (PageReclaim(pp)) {
2245 return AOP_WRITEPAGE_ACTIVATE;
2246 /* XXX - Do we need to redirty the page here? */
2251 inode = mapping->host;
2252 vcp = VTOAFS(inode);
2253 isize = i_size_read(inode);
2255 /* Don't defeat an earlier truncate */
2256 if (page_offset(pp) > isize) {
2257 set_page_writeback(pp);
2263 ObtainWriteLock(&vcp->lock, 537);
2264 code = afs_linux_prepare_writeback(vcp);
2265 if (code == AOP_WRITEPAGE_ACTIVATE) {
2266 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2267 * to return with the page still locked */
2268 ReleaseWriteLock(&vcp->lock);
2273 /* Grab the creds structure currently held in the vnode, and
2274 * get a reference to it, in case it goes away ... */
2280 ReleaseWriteLock(&vcp->lock);
2283 set_page_writeback(pp);
2285 SetPageUptodate(pp);
2287 /* We can unlock the page here, because it's protected by the
2288 * page_writeback flag. This should make us less vulnerable to
2289 * deadlocking in afs_write and afs_DoPartialWrite
2293 /* If this is the final page, then just write the number of bytes that
2294 * are actually in it */
2295 if ((isize - page_offset(pp)) < to )
2296 to = isize - page_offset(pp);
2298 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2301 ObtainWriteLock(&vcp->lock, 538);
2303 /* As much as we might like to ignore a file server error here,
2304 * and just try again when we close(), unfortunately StoreAllSegments
2305 * will invalidate our chunks if the server returns a permanent error,
2306 * so we need to at least try and get that error back to the user
2309 code1 = afs_linux_dopartialwrite(vcp, credp);
2311 afs_linux_complete_writeback(vcp);
2312 ReleaseWriteLock(&vcp->lock);
2317 end_page_writeback(pp);
2318 page_cache_release(pp);
2329 /* afs_linux_permission
2330 * Check access rights - returns error if can't check or permission denied.
2333 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2334 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2335 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2336 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2338 afs_linux_permission(struct inode *ip, int mode)
2345 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2346 /* We don't support RCU path walking */
2347 if (flags & IPERM_FLAG_RCU)
2353 if (mode & MAY_EXEC)
2355 if (mode & MAY_READ)
2357 if (mode & MAY_WRITE)
2359 code = afs_access(VTOAFS(ip), tmp, credp);
2363 return afs_convert_code(code);
2367 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2371 struct inode *inode = FILE_INODE(file);
2372 loff_t pagebase = page_offset(page);
2374 if (i_size_read(inode) < (pagebase + offset))
2375 i_size_write(inode, pagebase + offset);
2377 if (PageChecked(page)) {
2378 SetPageUptodate(page);
2379 ClearPageChecked(page);
2382 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2388 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2392 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2393 * prepare_write. Essentially, if the page exists within the file,
2394 * and is not being fully written, then we should populate it.
2397 if (!PageUptodate(page)) {
2398 loff_t pagebase = page_offset(page);
2399 loff_t isize = i_size_read(page->mapping->host);
2401 /* Is the location we are writing to beyond the end of the file? */
2402 if (pagebase >= isize ||
2403 ((from == 0) && (pagebase + to) >= isize)) {
2404 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2405 SetPageChecked(page);
2406 /* Are we we writing a full page */
2407 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2408 SetPageChecked(page);
2409 /* Is the page readable, if it's wronly, we don't care, because we're
2410 * not actually going to read from it ... */
2411 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2412 /* We don't care if fillpage fails, because if it does the page
2413 * won't be marked as up to date
2415 afs_linux_fillpage(file, page);
2421 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2423 afs_linux_write_end(struct file *file, struct address_space *mapping,
2424 loff_t pos, unsigned len, unsigned copied,
2425 struct page *page, void *fsdata)
2428 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2430 code = afs_linux_commit_write(file, page, from, from + len);
2433 page_cache_release(page);
2438 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2439 loff_t pos, unsigned len, unsigned flags,
2440 struct page **pagep, void **fsdata)
2443 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2444 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2447 page = grab_cache_page_write_begin(mapping, index, flags);
2450 code = afs_linux_prepare_write(file, page, from, from + len);
2453 page_cache_release(page);
2461 static struct inode_operations afs_file_iops = {
2462 .permission = afs_linux_permission,
2463 .getattr = afs_linux_getattr,
2464 .setattr = afs_notify_change,
2467 static struct address_space_operations afs_file_aops = {
2468 .readpage = afs_linux_readpage,
2469 .readpages = afs_linux_readpages,
2470 .writepage = afs_linux_writepage,
2471 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2472 .write_begin = afs_linux_write_begin,
2473 .write_end = afs_linux_write_end,
2475 .commit_write = afs_linux_commit_write,
2476 .prepare_write = afs_linux_prepare_write,
2481 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2482 * by what sort of operation is allowed.....
2485 static struct inode_operations afs_dir_iops = {
2486 .setattr = afs_notify_change,
2487 .create = afs_linux_create,
2488 .lookup = afs_linux_lookup,
2489 .link = afs_linux_link,
2490 .unlink = afs_linux_unlink,
2491 .symlink = afs_linux_symlink,
2492 .mkdir = afs_linux_mkdir,
2493 .rmdir = afs_linux_rmdir,
2494 .rename = afs_linux_rename,
2495 .getattr = afs_linux_getattr,
2496 .permission = afs_linux_permission,
2499 /* We really need a separate symlink set of ops, since do_follow_link()
2500 * determines if it _is_ a link by checking if the follow_link op is set.
2502 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2504 afs_symlink_filler(struct file *file, struct page *page)
2506 struct inode *ip = (struct inode *)page->mapping->host;
2507 char *p = (char *)kmap(page);
2511 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2516 p[code] = '\0'; /* null terminate? */
2518 SetPageUptodate(page);
2530 static struct address_space_operations afs_symlink_aops = {
2531 .readpage = afs_symlink_filler
2533 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2535 static struct inode_operations afs_symlink_iops = {
2536 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2537 .readlink = page_readlink,
2538 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2539 .follow_link = page_follow_link,
2541 .follow_link = page_follow_link_light,
2542 .put_link = page_put_link,
2544 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2545 .readlink = afs_linux_readlink,
2546 .follow_link = afs_linux_follow_link,
2547 .put_link = afs_linux_put_link,
2548 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2549 .setattr = afs_notify_change,
2553 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2557 vattr2inode(ip, vattr);
2559 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2560 /* Reset ops if symlink or directory. */
2561 if (S_ISREG(ip->i_mode)) {
2562 ip->i_op = &afs_file_iops;
2563 ip->i_fop = &afs_file_fops;
2564 ip->i_data.a_ops = &afs_file_aops;
2566 } else if (S_ISDIR(ip->i_mode)) {
2567 ip->i_op = &afs_dir_iops;
2568 ip->i_fop = &afs_dir_fops;
2570 } else if (S_ISLNK(ip->i_mode)) {
2571 ip->i_op = &afs_symlink_iops;
2572 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2573 ip->i_data.a_ops = &afs_symlink_aops;
2574 ip->i_mapping = &ip->i_data;