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)
505 #elif defined(FOP_FSYNC_TAKES_RANGE)
506 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
508 afs_linux_fsync(struct file *fp, int datasync)
512 struct inode *ip = FILE_INODE(fp);
513 cred_t *credp = crref();
515 #if defined(FOP_FSYNC_TAKES_RANGE)
516 mutex_lock(&ip->i_mutex);
519 code = afs_fsync(VTOAFS(ip), credp);
521 #if defined(FOP_FSYNC_TAKES_RANGE)
522 mutex_unlock(&ip->i_mutex);
525 return afs_convert_code(code);
531 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
534 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
535 cred_t *credp = crref();
536 struct AFS_FLOCK flock;
538 /* Convert to a lock format afs_lockctl understands. */
539 memset(&flock, 0, sizeof(flock));
540 flock.l_type = flp->fl_type;
541 flock.l_pid = flp->fl_pid;
543 flock.l_start = flp->fl_start;
544 if (flp->fl_end == OFFSET_MAX)
545 flock.l_len = 0; /* Lock to end of file */
547 flock.l_len = flp->fl_end - flp->fl_start + 1;
549 /* Safe because there are no large files, yet */
550 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
551 if (cmd == F_GETLK64)
553 else if (cmd == F_SETLK64)
555 else if (cmd == F_SETLKW64)
557 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
560 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
563 if ((code == 0 || flp->fl_type == F_UNLCK) &&
564 (cmd == F_SETLK || cmd == F_SETLKW)) {
565 code = afs_posix_lock_file(fp, flp);
566 if (code && flp->fl_type != F_UNLCK) {
567 struct AFS_FLOCK flock2;
569 flock2.l_type = F_UNLCK;
571 afs_lockctl(vcp, &flock2, F_SETLK, credp);
575 /* If lockctl says there are no conflicting locks, then also check with the
576 * kernel, as lockctl knows nothing about byte range locks
578 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
579 afs_posix_test_lock(fp, flp);
580 /* If we found a lock in the kernel's structure, return it */
581 if (flp->fl_type != F_UNLCK) {
587 /* Convert flock back to Linux's file_lock */
588 flp->fl_type = flock.l_type;
589 flp->fl_pid = flock.l_pid;
590 flp->fl_start = flock.l_start;
591 if (flock.l_len == 0)
592 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
594 flp->fl_end = flock.l_start + flock.l_len - 1;
600 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
602 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
604 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
605 cred_t *credp = crref();
606 struct AFS_FLOCK flock;
607 /* Convert to a lock format afs_lockctl understands. */
608 memset(&flock, 0, sizeof(flock));
609 flock.l_type = flp->fl_type;
610 flock.l_pid = flp->fl_pid;
615 /* Safe because there are no large files, yet */
616 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
617 if (cmd == F_GETLK64)
619 else if (cmd == F_SETLK64)
621 else if (cmd == F_SETLKW64)
623 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
626 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
629 if ((code == 0 || flp->fl_type == F_UNLCK) &&
630 (cmd == F_SETLK || cmd == F_SETLKW)) {
631 flp->fl_flags &=~ FL_SLEEP;
632 code = flock_lock_file_wait(fp, flp);
633 if (code && flp->fl_type != F_UNLCK) {
634 struct AFS_FLOCK flock2;
636 flock2.l_type = F_UNLCK;
638 afs_lockctl(vcp, &flock2, F_SETLK, credp);
642 /* Convert flock back to Linux's file_lock */
643 flp->fl_type = flock.l_type;
644 flp->fl_pid = flock.l_pid;
652 * essentially the same as afs_fsync() but we need to get the return
653 * code for the sys_close() here, not afs_linux_release(), so call
654 * afs_StoreAllSegments() with AFS_LASTSTORE
657 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
658 afs_linux_flush(struct file *fp, fl_owner_t id)
660 afs_linux_flush(struct file *fp)
663 struct vrequest treq;
671 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
679 vcp = VTOAFS(FILE_INODE(fp));
681 code = afs_InitReq(&treq, credp);
684 /* If caching is bypassed for this file, or globally, just return 0 */
685 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
688 ObtainReadLock(&vcp->lock);
689 if (vcp->cachingStates & FCSBypass)
691 ReleaseReadLock(&vcp->lock);
694 /* future proof: don't rely on 0 return from afs_InitReq */
699 ObtainSharedLock(&vcp->lock, 535);
700 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
701 UpgradeSToWLock(&vcp->lock, 536);
702 if (!AFS_IS_DISCONNECTED) {
703 code = afs_StoreAllSegments(vcp,
705 AFS_SYNC | AFS_LASTSTORE);
707 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
709 ConvertWToSLock(&vcp->lock);
711 code = afs_CheckCode(code, &treq, 54);
712 ReleaseSharedLock(&vcp->lock);
719 return afs_convert_code(code);
722 struct file_operations afs_dir_fops = {
723 .read = generic_read_dir,
724 .readdir = afs_linux_readdir,
725 #ifdef HAVE_UNLOCKED_IOCTL
726 .unlocked_ioctl = afs_unlocked_xioctl,
730 #ifdef HAVE_COMPAT_IOCTL
731 .compat_ioctl = afs_unlocked_xioctl,
733 .open = afs_linux_open,
734 .release = afs_linux_release,
735 .llseek = default_llseek,
738 struct file_operations afs_file_fops = {
739 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
740 .aio_read = afs_linux_aio_read,
741 .aio_write = afs_linux_aio_write,
743 .read = afs_linux_read,
744 .write = afs_linux_write,
746 #ifdef HAVE_UNLOCKED_IOCTL
747 .unlocked_ioctl = afs_unlocked_xioctl,
751 #ifdef HAVE_COMPAT_IOCTL
752 .compat_ioctl = afs_unlocked_xioctl,
754 .mmap = afs_linux_mmap,
755 .open = afs_linux_open,
756 .flush = afs_linux_flush,
757 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
758 .sendfile = generic_file_sendfile,
760 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
761 .splice_write = generic_file_splice_write,
762 .splice_read = generic_file_splice_read,
764 .release = afs_linux_release,
765 .fsync = afs_linux_fsync,
766 .lock = afs_linux_lock,
767 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
768 .flock = afs_linux_flock,
770 .llseek = default_llseek,
774 /**********************************************************************
775 * AFS Linux dentry operations
776 **********************************************************************/
778 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
779 * that has its mvid (parent dir's fid) pointer set to the wrong directory
780 * due to being mounted in multiple points at once. fix_bad_parent()
781 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
782 * dotdotfid and mtpoint fid members.
784 * dp - dentry to be checked.
785 * credp - credentials
786 * vcp, pvc - item's and parent's vcache pointer
790 * This dentry's vcache's mvid will be set to the correct parent directory's
792 * This root vnode's volume will have its dotdotfid and mtpoint fids set
793 * to the correct parent and mountpoint fids.
797 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
799 struct vcache *avc = NULL;
801 /* force a lookup, so vcp->mvid is fixed up */
802 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
803 if (!avc || vcp != avc) { /* bad, very bad.. */
804 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
805 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
806 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
807 ICL_TYPE_POINTER, dp);
810 AFS_RELE(AFSTOV(avc));
815 /* afs_linux_revalidate
816 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
819 afs_linux_revalidate(struct dentry *dp)
822 struct vcache *vcp = VTOAFS(dp->d_inode);
826 if (afs_shuttingdown)
832 /* Make this a fast path (no crref), since it's called so often. */
833 if (vcp->states & CStatd) {
834 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
836 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
837 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
840 fix_bad_parent(dp); /* check and correct mvid */
849 /* This avoids the crref when we don't have to do it. Watch for
850 * changes in afs_getattr that don't get replicated here!
852 if (vcp->f.states & CStatd &&
853 (!afs_fakestat_enable || vcp->mvstat != 1) &&
855 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
856 code = afs_CopyOutAttrs(vcp, &vattr);
859 code = afs_getattr(vcp, &vattr, credp);
864 afs_fill_inode(AFSTOV(vcp), &vattr);
868 return afs_convert_code(code);
872 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
874 int err = afs_linux_revalidate(dentry);
876 generic_fillattr(dentry->d_inode, stat);
881 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
882 * In kernels 2.2.10 and above, we are passed an additional flags var which
883 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
884 * we are advised to follow the entry if it is a link or to make sure that
885 * it is a directory. But since the kernel itself checks these possibilities
886 * later on, we shouldn't have to do it until later. Perhaps in the future..
888 * The code here assumes that on entry the global lock is not held
891 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
892 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
894 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
898 cred_t *credp = NULL;
899 struct vcache *vcp, *pvcp, *tvc = NULL;
900 struct dentry *parent;
902 struct afs_fakestat_state fakestate;
906 /* We don't support RCU path walking */
907 if (nd->flags & LOOKUP_RCU)
911 afs_InitFakeStat(&fakestate);
914 vcp = VTOAFS(dp->d_inode);
916 if (vcp == afs_globalVp)
919 parent = dget_parent(dp);
920 pvcp = VTOAFS(parent->d_inode);
922 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
928 if (locked && vcp->mvstat == 1) { /* mount point */
929 if (vcp->mvid && (vcp->f.states & CMValid)) {
932 struct vrequest treq;
934 code = afs_InitReq(&treq, credp);
936 (strcmp(dp->d_name.name, ".directory") == 0)) {
940 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
942 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
943 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
944 /* a mount point, not yet replaced by its directory */
949 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
950 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
951 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
956 /* If the last looker changes, we should make sure the current
957 * looker still has permission to examine this file. This would
958 * always require a crref() which would be "slow".
960 if (vcp->last_looker != treq.uid) {
961 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
964 vcp->last_looker = treq.uid;
969 /* If the parent's DataVersion has changed or the vnode
970 * is longer valid, we need to do a full lookup. VerifyVCache
971 * isn't enough since the vnode may have been renamed.
974 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
980 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
981 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
982 if (!tvc || tvc != vcp) {
987 if (afs_getattr(vcp, &vattr, credp)) {
992 vattr2inode(AFSTOV(vcp), &vattr);
993 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
996 /* should we always update the attributes at this point? */
997 /* unlikely--the vcache entry hasn't changed */
1002 /* If this code is ever enabled, we should use dget_parent to handle
1003 * getting the parent, and dput() to dispose of it. See above for an
1005 pvcp = VTOAFS(dp->d_parent->d_inode);
1006 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1010 /* No change in parent's DataVersion so this negative
1011 * lookup is still valid. BUT, if a server is down a
1012 * negative lookup can result so there should be a
1013 * liftime as well. For now, always expire.
1026 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1028 /* we hold the global lock if we evaluated a mount point */
1035 shrink_dcache_parent(dp);
1041 if (have_submounts(dp))
1049 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1051 struct vcache *vcp = VTOAFS(ip);
1054 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1055 (void) afs_InactiveVCache(vcp, NULL);
1058 afs_linux_clear_nfsfs_renamed(dp);
1064 afs_dentry_delete(struct dentry *dp)
1066 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1067 return 1; /* bad inode? */
1072 struct dentry_operations afs_dentry_operations = {
1073 .d_revalidate = afs_linux_dentry_revalidate,
1074 .d_delete = afs_dentry_delete,
1075 .d_iput = afs_dentry_iput,
1078 /**********************************************************************
1079 * AFS Linux inode operations
1080 **********************************************************************/
1084 * Merely need to set enough of vattr to get us through the create. Note
1085 * that the higher level code (open_namei) will take care of any tuncation
1086 * explicitly. Exclusive open is also taken care of in open_namei.
1088 * name is in kernel space at this point.
1091 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1092 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1093 struct nameidata *nd)
1095 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1099 cred_t *credp = crref();
1100 const char *name = dp->d_name.name;
1105 vattr.va_mode = mode;
1106 vattr.va_type = mode & S_IFMT;
1109 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1113 struct inode *ip = AFSTOV(vcp);
1115 afs_getattr(vcp, &vattr, credp);
1116 afs_fill_inode(ip, &vattr);
1117 insert_inode_hash(ip);
1118 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1119 dp->d_op = &afs_dentry_operations;
1121 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1122 d_instantiate(dp, ip);
1127 return afs_convert_code(code);
1130 /* afs_linux_lookup */
1131 static struct dentry *
1132 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1133 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1134 struct nameidata *nd)
1136 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1139 cred_t *credp = crref();
1140 struct vcache *vcp = NULL;
1141 const char *comp = dp->d_name.name;
1142 struct inode *ip = NULL;
1143 struct dentry *newdp = NULL;
1147 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1153 afs_getattr(vcp, &vattr, credp);
1154 afs_fill_inode(ip, &vattr);
1155 if (hlist_unhashed(&ip->i_hash))
1156 insert_inode_hash(ip);
1158 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1159 dp->d_op = &afs_dentry_operations;
1161 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1164 if (ip && S_ISDIR(ip->i_mode)) {
1165 struct dentry *alias;
1167 /* Try to invalidate an existing alias in favor of our new one */
1168 alias = d_find_alias(ip);
1169 /* But not if it's disconnected; then we want d_splice_alias below */
1170 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1171 if (d_invalidate(alias) == 0) {
1180 newdp = d_splice_alias(ip, dp);
1184 /* It's ok for the file to not be found. That's noted by the caller by
1185 * seeing that the dp->d_inode field is NULL.
1187 if (!code || code == ENOENT)
1190 return ERR_PTR(afs_convert_code(code));
1194 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1197 cred_t *credp = crref();
1198 const char *name = newdp->d_name.name;
1199 struct inode *oldip = olddp->d_inode;
1201 /* If afs_link returned the vnode, we could instantiate the
1202 * dentry. Since it's not, we drop this one and do a new lookup.
1207 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1211 return afs_convert_code(code);
1214 /* We have to have a Linux specific sillyrename function, because we
1215 * also have to keep the dcache up to date when we're doing a silly
1216 * rename - so we don't want the generic vnodeops doing this behind our
1221 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1224 struct vcache *tvc = VTOAFS(dentry->d_inode);
1225 struct dentry *__dp = NULL;
1226 char *__name = NULL;
1229 if (afs_linux_nfsfs_renamed(dentry))
1237 osi_FreeSmallSpace(__name);
1238 __name = afs_newname();
1241 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1244 osi_FreeSmallSpace(__name);
1247 } while (__dp->d_inode != NULL);
1250 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1251 VTOAFS(dir), (char *)__dp->d_name.name,
1254 tvc->mvid = (void *) __name;
1257 crfree(tvc->uncred);
1259 tvc->uncred = credp;
1260 tvc->f.states |= CUnlinked;
1261 afs_linux_set_nfsfs_renamed(dentry);
1263 osi_FreeSmallSpace(__name);
1268 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1269 d_move(dentry, __dp);
1278 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1281 cred_t *credp = crref();
1282 const char *name = dp->d_name.name;
1283 struct vcache *tvc = VTOAFS(dp->d_inode);
1285 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1286 && !(tvc->f.states & CUnlinked)) {
1288 code = afs_linux_sillyrename(dip, dp, credp);
1291 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1298 return afs_convert_code(code);
1303 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1306 cred_t *credp = crref();
1308 const char *name = dp->d_name.name;
1310 /* If afs_symlink returned the vnode, we could instantiate the
1311 * dentry. Since it's not, we drop this one and do a new lookup.
1317 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1320 return afs_convert_code(code);
1324 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1327 cred_t *credp = crref();
1328 struct vcache *tvcp = NULL;
1330 const char *name = dp->d_name.name;
1333 vattr.va_mask = ATTR_MODE;
1334 vattr.va_mode = mode;
1336 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1339 struct inode *ip = AFSTOV(tvcp);
1341 afs_getattr(tvcp, &vattr, credp);
1342 afs_fill_inode(ip, &vattr);
1344 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1345 dp->d_op = &afs_dentry_operations;
1347 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1348 d_instantiate(dp, ip);
1353 return afs_convert_code(code);
1357 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1360 cred_t *credp = crref();
1361 const char *name = dp->d_name.name;
1363 /* locking kernel conflicts with glock? */
1366 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1369 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1370 * that failed because a directory is not empty. So, we map
1371 * EEXIST to ENOTEMPTY on linux.
1373 if (code == EEXIST) {
1382 return afs_convert_code(code);
1387 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1388 struct inode *newip, struct dentry *newdp)
1391 cred_t *credp = crref();
1392 const char *oldname = olddp->d_name.name;
1393 const char *newname = newdp->d_name.name;
1394 struct dentry *rehash = NULL;
1396 /* Prevent any new references during rename operation. */
1398 if (!d_unhashed(newdp)) {
1403 #if defined(D_COUNT_INT)
1404 spin_lock(&olddp->d_lock);
1405 if (olddp->d_count > 1) {
1406 spin_unlock(&olddp->d_lock);
1407 shrink_dcache_parent(olddp);
1409 spin_unlock(&olddp->d_lock);
1411 if (atomic_read(&olddp->d_count) > 1)
1412 shrink_dcache_parent(olddp);
1416 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1420 olddp->d_time = 0; /* force to revalidate */
1426 return afs_convert_code(code);
1430 /* afs_linux_ireadlink
1431 * Internal readlink which can return link contents to user or kernel space.
1432 * Note that the buffer is NOT supposed to be null-terminated.
1435 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1438 cred_t *credp = crref();
1442 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1443 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1447 return maxlen - tuio.uio_resid;
1449 return afs_convert_code(code);
1452 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1453 /* afs_linux_readlink
1454 * Fill target (which is in user space) with contents of symlink.
1457 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1460 struct inode *ip = dp->d_inode;
1463 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1469 /* afs_linux_follow_link
1470 * a file system dependent link following routine.
1472 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1477 name = kmalloc(PATH_MAX, GFP_NOFS);
1483 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1491 nd_set_link(nd, name);
1496 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1498 char *name = nd_get_link(nd);
1500 if (name && !IS_ERR(name))
1504 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1506 /* Populate a page by filling it from the cache file pointed at by cachefp
1507 * (which contains indicated chunk)
1508 * If task is NULL, the page copy occurs syncronously, and the routine
1509 * returns with page still locked. If task is non-NULL, then page copies
1510 * may occur in the background, and the page will be unlocked when it is
1514 afs_linux_read_cache(struct file *cachefp, struct page *page,
1515 int chunk, struct pagevec *lrupv,
1516 struct afs_pagecopy_task *task) {
1517 loff_t offset = page_offset(page);
1518 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1519 struct page *newpage, *cachepage;
1520 struct address_space *cachemapping;
1524 cachemapping = cacheinode->i_mapping;
1528 /* If we're trying to read a page that's past the end of the disk
1529 * cache file, then just return a zeroed page */
1530 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1531 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1532 SetPageUptodate(page);
1538 /* From our offset, we now need to work out which page in the disk
1539 * file it corresponds to. This will be fun ... */
1540 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1542 while (cachepage == NULL) {
1543 cachepage = find_get_page(cachemapping, pageindex);
1546 newpage = page_cache_alloc_cold(cachemapping);
1552 code = add_to_page_cache(newpage, cachemapping,
1553 pageindex, GFP_KERNEL);
1555 cachepage = newpage;
1558 page_cache_get(cachepage);
1559 if (!pagevec_add(lrupv, cachepage))
1560 __pagevec_lru_add_file(lrupv);
1563 page_cache_release(newpage);
1565 if (code != -EEXIST)
1569 lock_page(cachepage);
1573 if (!PageUptodate(cachepage)) {
1574 ClearPageError(cachepage);
1575 code = cachemapping->a_ops->readpage(NULL, cachepage);
1576 if (!code && !task) {
1577 wait_on_page_locked(cachepage);
1580 unlock_page(cachepage);
1584 if (PageUptodate(cachepage)) {
1585 copy_highpage(page, cachepage);
1586 flush_dcache_page(page);
1587 SetPageUptodate(page);
1592 afs_pagecopy_queue_page(task, cachepage, page);
1604 page_cache_release(cachepage);
1610 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1612 loff_t offset = page_offset(pp);
1613 struct inode *ip = FILE_INODE(fp);
1614 struct vcache *avc = VTOAFS(ip);
1616 struct file *cacheFp = NULL;
1619 struct pagevec lrupv;
1621 /* Not a UFS cache, don't do anything */
1622 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1625 /* Can't do anything if the vcache isn't statd , or if the read
1626 * crosses a chunk boundary.
1628 if (!(avc->f.states & CStatd) ||
1629 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1633 ObtainWriteLock(&avc->lock, 911);
1635 /* XXX - See if hinting actually makes things faster !!! */
1637 /* See if we have a suitable entry already cached */
1641 /* We need to lock xdcache, then dcache, to handle situations where
1642 * the hint is on the free list. However, we can't safely do this
1643 * according to the locking hierarchy. So, use a non blocking lock.
1645 ObtainReadLock(&afs_xdcache);
1646 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1648 if (dcLocked && (tdc->index != NULLIDX)
1649 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1650 && tdc->f.chunk == AFS_CHUNK(offset)
1651 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1652 /* Bonus - the hint was correct */
1655 /* Only destroy the hint if its actually invalid, not if there's
1656 * just been a locking failure */
1658 ReleaseReadLock(&tdc->lock);
1665 ReleaseReadLock(&afs_xdcache);
1668 /* No hint, or hint is no longer valid - see if we can get something
1669 * directly from the dcache
1672 tdc = afs_FindDCache(avc, offset);
1675 ReleaseWriteLock(&avc->lock);
1680 ObtainReadLock(&tdc->lock);
1682 /* Is the dcache we've been given currently up to date */
1683 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1684 (tdc->dflags & DFFetching)) {
1685 ReleaseWriteLock(&avc->lock);
1686 ReleaseReadLock(&tdc->lock);
1691 /* Update our hint for future abuse */
1694 /* Okay, so we've now got a cache file that is up to date */
1696 /* XXX - I suspect we should be locking the inodes before we use them! */
1698 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1699 pagevec_init(&lrupv, 0);
1701 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1703 if (pagevec_count(&lrupv))
1704 __pagevec_lru_add_file(&lrupv);
1706 filp_close(cacheFp, NULL);
1709 ReleaseReadLock(&tdc->lock);
1710 ReleaseWriteLock(&avc->lock);
1717 /* afs_linux_readpage
1719 * This function is split into two, because prepare_write/begin_write
1720 * require a readpage call which doesn't unlock the resulting page upon
1724 afs_linux_fillpage(struct file *fp, struct page *pp)
1729 struct iovec *iovecp;
1730 struct inode *ip = FILE_INODE(fp);
1731 afs_int32 cnt = page_count(pp);
1732 struct vcache *avc = VTOAFS(ip);
1733 afs_offs_t offset = page_offset(pp);
1737 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1747 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
1748 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
1750 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1755 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1756 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1757 99999); /* not a possible code value */
1759 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1761 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1762 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1764 AFS_DISCON_UNLOCK();
1767 /* XXX valid for no-cache also? Check last bits of files... :)
1768 * Cognate code goes in afs_NoCacheFetchProc. */
1769 if (auio->uio_resid) /* zero remainder of page */
1770 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1773 flush_dcache_page(pp);
1774 SetPageUptodate(pp);
1783 return afs_convert_code(code);
1787 afs_linux_prefetch(struct file *fp, struct page *pp)
1790 struct vcache *avc = VTOAFS(FILE_INODE(fp));
1791 afs_offs_t offset = page_offset(pp);
1793 if (AFS_CHUNKOFFSET(offset) == 0) {
1795 struct vrequest treq;
1800 code = afs_InitReq(&treq, credp);
1801 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1802 tdc = afs_FindDCache(avc, offset);
1804 if (!(tdc->mflags & DFNextStarted))
1805 afs_PrefetchChunk(avc, tdc, credp, &treq);
1808 ReleaseWriteLock(&avc->lock);
1813 return afs_convert_code(code);
1818 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
1819 struct list_head *page_list, unsigned num_pages)
1824 struct iovec* iovecp;
1825 struct nocache_read_request *ancr;
1827 struct pagevec lrupv;
1831 struct inode *ip = FILE_INODE(fp);
1832 struct vcache *avc = VTOAFS(ip);
1833 afs_int32 base_index = 0;
1834 afs_int32 page_count = 0;
1837 /* background thread must free: iovecp, auio, ancr */
1838 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1840 auio = osi_Alloc(sizeof(struct uio));
1841 auio->uio_iov = iovecp;
1842 auio->uio_iovcnt = num_pages;
1843 auio->uio_flag = UIO_READ;
1844 auio->uio_seg = AFS_UIOSYS;
1845 auio->uio_resid = num_pages * PAGE_SIZE;
1847 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1849 ancr->offset = auio->uio_offset;
1850 ancr->length = auio->uio_resid;
1852 pagevec_init(&lrupv, 0);
1854 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1856 if(list_empty(page_list))
1859 pp = list_entry(page_list->prev, struct page, lru);
1860 /* If we allocate a page and don't remove it from page_list,
1861 * the page cache gets upset. */
1863 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1864 if(pp->index > isize) {
1871 offset = page_offset(pp);
1872 auio->uio_offset = offset;
1873 base_index = pp->index;
1875 iovecp[page_ix].iov_len = PAGE_SIZE;
1876 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1877 if(base_index != pp->index) {
1880 page_cache_release(pp);
1881 iovecp[page_ix].iov_base = (void *) 0;
1883 ancr->length -= PAGE_SIZE;
1890 page_cache_release(pp);
1891 iovecp[page_ix].iov_base = (void *) 0;
1894 if(!PageLocked(pp)) {
1898 /* increment page refcount--our original design assumed
1899 * that locking it would effectively pin it; protect
1900 * ourselves from the possiblity that this assumption is
1901 * is faulty, at low cost (provided we do not fail to
1902 * do the corresponding decref on the other side) */
1905 /* save the page for background map */
1906 iovecp[page_ix].iov_base = (void*) pp;
1908 /* and put it on the LRU cache */
1909 if (!pagevec_add(&lrupv, pp))
1910 __pagevec_lru_add_file(&lrupv);
1914 /* If there were useful pages in the page list, make sure all pages
1915 * are in the LRU cache, then schedule the read */
1917 if (pagevec_count(&lrupv))
1918 __pagevec_lru_add_file(&lrupv);
1920 code = afs_ReadNoCache(avc, ancr, credp);
1923 /* If there is nothing for the background thread to handle,
1924 * it won't be freeing the things that we never gave it */
1925 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1926 osi_Free(auio, sizeof(struct uio));
1927 osi_Free(ancr, sizeof(struct nocache_read_request));
1929 /* we do not flush, release, or unmap pages--that will be
1930 * done for us by the background thread as each page comes in
1931 * from the fileserver */
1932 return afs_convert_code(code);
1937 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
1939 cred_t *credp = NULL;
1941 struct iovec *iovecp;
1942 struct nocache_read_request *ancr;
1946 * Special case: if page is at or past end of file, just zero it and set
1949 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
1950 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
1951 SetPageUptodate(pp);
1958 /* receiver frees */
1959 auio = osi_Alloc(sizeof(struct uio));
1960 iovecp = osi_Alloc(sizeof(struct iovec));
1962 /* address can be NULL, because we overwrite it with 'pp', below */
1963 setup_uio(auio, iovecp, NULL, page_offset(pp),
1964 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
1966 /* save the page for background map */
1967 get_page(pp); /* see above */
1968 auio->uio_iov->iov_base = (void*) pp;
1969 /* the background thread will free this */
1970 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1972 ancr->offset = page_offset(pp);
1973 ancr->length = PAGE_SIZE;
1976 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
1979 return afs_convert_code(code);
1983 afs_linux_can_bypass(struct inode *ip) {
1984 switch(cache_bypass_strategy) {
1985 case NEVER_BYPASS_CACHE:
1987 case ALWAYS_BYPASS_CACHE:
1989 case LARGE_FILES_BYPASS_CACHE:
1990 if(i_size_read(ip) > cache_bypass_threshold)
1997 /* Check if a file is permitted to bypass the cache by policy, and modify
1998 * the cache bypass state recorded for that file */
2001 afs_linux_bypass_check(struct inode *ip) {
2004 int bypass = afs_linux_can_bypass(ip);
2007 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2015 afs_linux_readpage(struct file *fp, struct page *pp)
2019 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2020 code = afs_linux_bypass_readpage(fp, pp);
2022 code = afs_linux_fillpage(fp, pp);
2024 code = afs_linux_prefetch(fp, pp);
2031 /* Readpages reads a number of pages for a particular file. We use
2032 * this to optimise the reading, by limiting the number of times upon which
2033 * we have to lookup, lock and open vcaches and dcaches
2037 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2038 struct list_head *page_list, unsigned int num_pages)
2040 struct inode *inode = mapping->host;
2041 struct vcache *avc = VTOAFS(inode);
2043 struct file *cacheFp = NULL;
2045 unsigned int page_idx;
2047 struct pagevec lrupv;
2048 struct afs_pagecopy_task *task;
2050 if (afs_linux_bypass_check(inode))
2051 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2053 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2057 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2062 ObtainWriteLock(&avc->lock, 912);
2065 task = afs_pagecopy_init_task();
2068 pagevec_init(&lrupv, 0);
2069 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2070 struct page *page = list_entry(page_list->prev, struct page, lru);
2071 list_del(&page->lru);
2072 offset = page_offset(page);
2074 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2076 ReleaseReadLock(&tdc->lock);
2081 filp_close(cacheFp, NULL);
2086 if ((tdc = afs_FindDCache(avc, offset))) {
2087 ObtainReadLock(&tdc->lock);
2088 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2089 (tdc->dflags & DFFetching)) {
2090 ReleaseReadLock(&tdc->lock);
2097 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2100 if (tdc && !add_to_page_cache(page, mapping, page->index,
2102 page_cache_get(page);
2103 if (!pagevec_add(&lrupv, page))
2104 __pagevec_lru_add_file(&lrupv);
2106 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2108 page_cache_release(page);
2110 if (pagevec_count(&lrupv))
2111 __pagevec_lru_add_file(&lrupv);
2114 filp_close(cacheFp, NULL);
2116 afs_pagecopy_put_task(task);
2120 ReleaseReadLock(&tdc->lock);
2124 ReleaseWriteLock(&avc->lock);
2129 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2132 afs_linux_prepare_writeback(struct vcache *avc) {
2133 if (avc->f.states & CPageWrite) {
2134 return AOP_WRITEPAGE_ACTIVATE;
2136 avc->f.states |= CPageWrite;
2141 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2142 struct vrequest treq;
2145 if (!afs_InitReq(&treq, credp))
2146 code = afs_DoPartialWrite(avc, &treq);
2148 return afs_convert_code(code);
2152 afs_linux_complete_writeback(struct vcache *avc) {
2153 avc->f.states &= ~CPageWrite;
2156 /* Writeback a given page syncronously. Called with no AFS locks held */
2158 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2159 unsigned long offset, unsigned int count,
2162 struct vcache *vcp = VTOAFS(ip);
2170 buffer = kmap(pp) + offset;
2171 base = page_offset(pp) + offset;
2174 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2175 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2176 ICL_TYPE_INT32, 99999);
2178 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2180 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2182 i_size_write(ip, vcp->f.m.Length);
2183 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2185 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2187 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2188 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2189 ICL_TYPE_INT32, code);
2198 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2199 unsigned long offset, unsigned int count)
2203 struct vcache *vcp = VTOAFS(ip);
2206 /* Catch recursive writeback. This occurs if the kernel decides
2207 * writeback is required whilst we are writing to the cache, or
2208 * flushing to the server. When we're running syncronously (as
2209 * opposed to from writepage) we can't actually do anything about
2210 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2213 ObtainWriteLock(&vcp->lock, 532);
2214 afs_linux_prepare_writeback(vcp);
2215 ReleaseWriteLock(&vcp->lock);
2219 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2222 ObtainWriteLock(&vcp->lock, 533);
2224 code1 = afs_linux_dopartialwrite(vcp, credp);
2225 afs_linux_complete_writeback(vcp);
2226 ReleaseWriteLock(&vcp->lock);
2237 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2238 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2240 afs_linux_writepage(struct page *pp)
2243 struct address_space *mapping = pp->mapping;
2244 struct inode *inode;
2247 unsigned int to = PAGE_CACHE_SIZE;
2252 if (PageReclaim(pp)) {
2253 return AOP_WRITEPAGE_ACTIVATE;
2254 /* XXX - Do we need to redirty the page here? */
2259 inode = mapping->host;
2260 vcp = VTOAFS(inode);
2261 isize = i_size_read(inode);
2263 /* Don't defeat an earlier truncate */
2264 if (page_offset(pp) > isize) {
2265 set_page_writeback(pp);
2271 ObtainWriteLock(&vcp->lock, 537);
2272 code = afs_linux_prepare_writeback(vcp);
2273 if (code == AOP_WRITEPAGE_ACTIVATE) {
2274 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2275 * to return with the page still locked */
2276 ReleaseWriteLock(&vcp->lock);
2281 /* Grab the creds structure currently held in the vnode, and
2282 * get a reference to it, in case it goes away ... */
2288 ReleaseWriteLock(&vcp->lock);
2291 set_page_writeback(pp);
2293 SetPageUptodate(pp);
2295 /* We can unlock the page here, because it's protected by the
2296 * page_writeback flag. This should make us less vulnerable to
2297 * deadlocking in afs_write and afs_DoPartialWrite
2301 /* If this is the final page, then just write the number of bytes that
2302 * are actually in it */
2303 if ((isize - page_offset(pp)) < to )
2304 to = isize - page_offset(pp);
2306 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2309 ObtainWriteLock(&vcp->lock, 538);
2311 /* As much as we might like to ignore a file server error here,
2312 * and just try again when we close(), unfortunately StoreAllSegments
2313 * will invalidate our chunks if the server returns a permanent error,
2314 * so we need to at least try and get that error back to the user
2317 code1 = afs_linux_dopartialwrite(vcp, credp);
2319 afs_linux_complete_writeback(vcp);
2320 ReleaseWriteLock(&vcp->lock);
2325 end_page_writeback(pp);
2326 page_cache_release(pp);
2337 /* afs_linux_permission
2338 * Check access rights - returns error if can't check or permission denied.
2341 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2342 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2343 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2344 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2346 afs_linux_permission(struct inode *ip, int mode)
2353 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2354 /* We don't support RCU path walking */
2355 if (flags & IPERM_FLAG_RCU)
2361 if (mode & MAY_EXEC)
2363 if (mode & MAY_READ)
2365 if (mode & MAY_WRITE)
2367 code = afs_access(VTOAFS(ip), tmp, credp);
2371 return afs_convert_code(code);
2375 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2379 struct inode *inode = FILE_INODE(file);
2380 loff_t pagebase = page_offset(page);
2382 if (i_size_read(inode) < (pagebase + offset))
2383 i_size_write(inode, pagebase + offset);
2385 if (PageChecked(page)) {
2386 SetPageUptodate(page);
2387 ClearPageChecked(page);
2390 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2396 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2400 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2401 * prepare_write. Essentially, if the page exists within the file,
2402 * and is not being fully written, then we should populate it.
2405 if (!PageUptodate(page)) {
2406 loff_t pagebase = page_offset(page);
2407 loff_t isize = i_size_read(page->mapping->host);
2409 /* Is the location we are writing to beyond the end of the file? */
2410 if (pagebase >= isize ||
2411 ((from == 0) && (pagebase + to) >= isize)) {
2412 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2413 SetPageChecked(page);
2414 /* Are we we writing a full page */
2415 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2416 SetPageChecked(page);
2417 /* Is the page readable, if it's wronly, we don't care, because we're
2418 * not actually going to read from it ... */
2419 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2420 /* We don't care if fillpage fails, because if it does the page
2421 * won't be marked as up to date
2423 afs_linux_fillpage(file, page);
2429 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2431 afs_linux_write_end(struct file *file, struct address_space *mapping,
2432 loff_t pos, unsigned len, unsigned copied,
2433 struct page *page, void *fsdata)
2436 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2438 code = afs_linux_commit_write(file, page, from, from + len);
2441 page_cache_release(page);
2446 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2447 loff_t pos, unsigned len, unsigned flags,
2448 struct page **pagep, void **fsdata)
2451 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2452 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2455 page = grab_cache_page_write_begin(mapping, index, flags);
2458 code = afs_linux_prepare_write(file, page, from, from + len);
2461 page_cache_release(page);
2469 static struct inode_operations afs_file_iops = {
2470 .permission = afs_linux_permission,
2471 .getattr = afs_linux_getattr,
2472 .setattr = afs_notify_change,
2475 static struct address_space_operations afs_file_aops = {
2476 .readpage = afs_linux_readpage,
2477 .readpages = afs_linux_readpages,
2478 .writepage = afs_linux_writepage,
2479 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2480 .write_begin = afs_linux_write_begin,
2481 .write_end = afs_linux_write_end,
2483 .commit_write = afs_linux_commit_write,
2484 .prepare_write = afs_linux_prepare_write,
2489 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2490 * by what sort of operation is allowed.....
2493 static struct inode_operations afs_dir_iops = {
2494 .setattr = afs_notify_change,
2495 .create = afs_linux_create,
2496 .lookup = afs_linux_lookup,
2497 .link = afs_linux_link,
2498 .unlink = afs_linux_unlink,
2499 .symlink = afs_linux_symlink,
2500 .mkdir = afs_linux_mkdir,
2501 .rmdir = afs_linux_rmdir,
2502 .rename = afs_linux_rename,
2503 .getattr = afs_linux_getattr,
2504 .permission = afs_linux_permission,
2507 /* We really need a separate symlink set of ops, since do_follow_link()
2508 * determines if it _is_ a link by checking if the follow_link op is set.
2510 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2512 afs_symlink_filler(struct file *file, struct page *page)
2514 struct inode *ip = (struct inode *)page->mapping->host;
2515 char *p = (char *)kmap(page);
2519 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2524 p[code] = '\0'; /* null terminate? */
2526 SetPageUptodate(page);
2538 static struct address_space_operations afs_symlink_aops = {
2539 .readpage = afs_symlink_filler
2541 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2543 static struct inode_operations afs_symlink_iops = {
2544 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2545 .readlink = page_readlink,
2546 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2547 .follow_link = page_follow_link,
2549 .follow_link = page_follow_link_light,
2550 .put_link = page_put_link,
2552 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2553 .readlink = afs_linux_readlink,
2554 .follow_link = afs_linux_follow_link,
2555 .put_link = afs_linux_put_link,
2556 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2557 .setattr = afs_notify_change,
2561 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2565 vattr2inode(ip, vattr);
2567 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2568 /* Reset ops if symlink or directory. */
2569 if (S_ISREG(ip->i_mode)) {
2570 ip->i_op = &afs_file_iops;
2571 ip->i_fop = &afs_file_fops;
2572 ip->i_data.a_ops = &afs_file_aops;
2574 } else if (S_ISDIR(ip->i_mode)) {
2575 ip->i_op = &afs_dir_iops;
2576 ip->i_fop = &afs_dir_fops;
2578 } else if (S_ISLNK(ip->i_mode)) {
2579 ip->i_op = &afs_symlink_iops;
2580 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2581 ip->i_data.a_ops = &afs_symlink_aops;
2582 ip->i_mapping = &ip->i_data;