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 "h/mm_inline.h"
32 #include "h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "h/smp_lock.h"
36 #if defined(AFS_LINUX26_ENV)
37 #include "h/writeback.h"
38 #include "h/pagevec.h"
40 #if defined(AFS_CACHE_BYPASS)
42 #include "afs/afs_bypasscache.h"
46 #define pageoff(pp) pgoff2loff((pp)->index)
48 #define pageoff(pp) pp->offset
52 #define MAX_ERRNO 1000L
55 #if defined(AFS_LINUX26_ENV)
56 #define LockPage(pp) lock_page(pp)
57 #define UnlockPage(pp) unlock_page(pp)
58 extern struct backing_dev_info afs_backing_dev_info;
61 extern struct vcache *afs_globalVp;
62 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
63 #if defined(AFS_LINUX24_ENV)
64 /* Some uses of BKL are perhaps not needed for bypass or memcache--
65 * why don't we try it out? */
66 extern struct afs_cacheOps afs_UfsCacheOps;
67 #define maybe_lock_kernel() \
69 if(afs_cacheType == &afs_UfsCacheOps) \
74 #define maybe_unlock_kernel() \
76 if(afs_cacheType == &afs_UfsCacheOps) \
79 #endif /* AFS_LINUX24_ENV */
82 /* This function converts a positive error code from AFS into a negative
83 * code suitable for passing into the Linux VFS layer. It checks that the
84 * error code is within the permissable bounds for the ERR_PTR mechanism.
86 * _All_ error codes which come from the AFS layer should be passed through
87 * this function before being returned to the kernel.
90 static inline int afs_convert_code(int code) {
91 if ((code >= 0) && (code <= MAX_ERRNO))
97 /* Linux doesn't require a credp for many functions, and crref is an expensive
98 * operation. This helper function avoids obtaining it for VerifyVCache calls
101 static inline int afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
102 cred_t *credp = NULL;
103 struct vrequest treq;
106 if (avc->f.states & CStatd) {
114 code = afs_InitReq(&treq, credp);
116 code = afs_VerifyVCache2(avc, &treq);
123 return afs_convert_code(code);
127 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
130 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
131 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
132 afs_size_t isize, offindex;
136 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
137 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
139 code = afs_linux_VerifyVCache(vcp, NULL);
142 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
143 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
144 offindex = *offp >> PAGE_CACHE_SHIFT;
145 if(offindex > isize) {
150 /* Linux's FlushPages implementation doesn't ever use credp,
151 * so we optimise by not using it */
152 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
155 code = do_sync_read(fp, buf, count, offp);
157 code = generic_file_read(fp, buf, count, offp);
162 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
163 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
165 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
173 /* Now we have integrated VM for writes as well as reads. generic_file_write
174 * also takes care of re-positioning the pointer if file is open in append
175 * mode. Call fake open/close to ensure we do writes of core dumps.
178 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
181 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
186 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
187 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
188 (fp->f_flags & O_APPEND) ? 99998 : 99999);
190 code = afs_linux_VerifyVCache(vcp, &credp);
192 ObtainWriteLock(&vcp->lock, 529);
194 ReleaseWriteLock(&vcp->lock);
198 code = do_sync_write(fp, buf, count, offp);
200 code = generic_file_write(fp, buf, count, offp);
205 ObtainWriteLock(&vcp->lock, 530);
207 if (vcp->execsOrWriters == 1 && !credp)
210 afs_FakeClose(vcp, credp);
211 ReleaseWriteLock(&vcp->lock);
213 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
214 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
223 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
225 /* This is a complete rewrite of afs_readdir, since we can make use of
226 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
227 * handling and use of bulkstats will need to be reflected here as well.
230 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
232 struct vcache *avc = VTOAFS(FILE_INODE(fp));
233 struct vrequest treq;
234 register struct dcache *tdc;
241 afs_size_t origOffset, tlen;
242 cred_t *credp = crref();
243 struct afs_fakestat_state fakestat;
245 #if defined(AFS_LINUX26_ENV)
249 AFS_STATCNT(afs_readdir);
251 code = afs_convert_code(afs_InitReq(&treq, credp));
256 afs_InitFakeStat(&fakestat);
257 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
261 /* update the cache entry */
263 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
267 /* get a reference to the entire directory */
268 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
274 ObtainSharedLock(&avc->lock, 810);
275 UpgradeSToWLock(&avc->lock, 811);
276 ObtainReadLock(&tdc->lock);
278 * Make sure that the data in the cache is current. There are two
279 * cases we need to worry about:
280 * 1. The cache data is being fetched by another process.
281 * 2. The cache data is no longer valid
283 while ((avc->f.states & CStatd)
284 && (tdc->dflags & DFFetching)
285 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
286 ReleaseReadLock(&tdc->lock);
287 ReleaseSharedLock(&avc->lock);
288 afs_osi_Sleep(&tdc->validPos);
289 ObtainSharedLock(&avc->lock, 812);
290 ObtainReadLock(&tdc->lock);
292 if (!(avc->f.states & CStatd)
293 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
294 ReleaseReadLock(&tdc->lock);
295 ReleaseSharedLock(&avc->lock);
300 /* Set the readdir-in-progress flag, and downgrade the lock
301 * to shared so others will be able to acquire a read lock.
303 avc->f.states |= CReadDir;
304 avc->dcreaddir = tdc;
305 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
306 ConvertWToSLock(&avc->lock);
308 /* Fill in until we get an error or we're done. This implementation
309 * takes an offset in units of blobs, rather than bytes.
312 offset = (int) fp->f_pos;
314 dirpos = BlobScan(tdc, offset);
318 de = afs_dir_GetBlob(tdc, dirpos);
322 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
325 len = strlen(de->name);
327 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
328 (unsigned long)&tdc->f.inode, dirpos);
330 ReleaseSharedLock(&avc->lock);
336 /* filldir returns -EINVAL when the buffer is full. */
337 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
339 unsigned int type = DT_UNKNOWN;
340 struct VenusFid afid;
343 afid.Cell = avc->f.fid.Cell;
344 afid.Fid.Volume = avc->f.fid.Fid.Volume;
345 afid.Fid.Vnode = ntohl(de->fid.vnode);
346 afid.Fid.Unique = ntohl(de->fid.vunique);
347 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
349 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
352 } else if (((tvc->f.states) & (CStatd | CTruth))) {
353 /* CTruth will be set if the object has
358 else if (vtype == VREG)
360 /* Don't do this until we're sure it can't be a mtpt */
361 /* else if (vtype == VLNK)
363 /* what other types does AFS support? */
365 /* clean up from afs_FindVCache */
369 * If this is NFS readdirplus, then the filler is going to
370 * call getattr on this inode, which will deadlock if we're
374 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
378 code = (*filldir) (dirbuf, de->name, len, offset, ino);
383 offset = dirpos + 1 + ((len + 16) >> 5);
385 /* If filldir didn't fill in the last one this is still pointing to that
388 fp->f_pos = (loff_t) offset;
390 ReleaseReadLock(&tdc->lock);
392 UpgradeSToWLock(&avc->lock, 813);
393 avc->f.states &= ~CReadDir;
395 avc->readdir_pid = 0;
396 ReleaseSharedLock(&avc->lock);
400 afs_PutFakeStat(&fakestat);
403 #if defined(AFS_LINUX26_ENV)
404 maybe_unlock_kernel();
410 /* in afs_pioctl.c */
411 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
414 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
415 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
417 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
424 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
426 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
430 #if defined(AFS_LINUX24_ENV)
431 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
432 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
433 vmap->vm_end - vmap->vm_start);
435 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
436 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
437 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
441 /* get a validated vcache entry */
442 code = afs_linux_VerifyVCache(vcp, NULL);
444 /* Linux's Flushpage implementation doesn't use credp, so optimise
445 * our code to not need to crref() it */
446 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
448 code = generic_file_mmap(fp, vmap);
451 vcp->f.states |= CMAPPED;
458 afs_linux_open(struct inode *ip, struct file *fp)
460 struct vcache *vcp = VTOAFS(ip);
461 cred_t *credp = crref();
464 #ifdef AFS_LINUX24_ENV
468 code = afs_open(&vcp, fp->f_flags, credp);
470 #ifdef AFS_LINUX24_ENV
471 maybe_unlock_kernel();
475 return afs_convert_code(code);
479 afs_linux_release(struct inode *ip, struct file *fp)
481 struct vcache *vcp = VTOAFS(ip);
482 cred_t *credp = crref();
485 #ifdef AFS_LINUX24_ENV
489 code = afs_close(vcp, fp->f_flags, credp);
491 #ifdef AFS_LINUX24_ENV
492 maybe_unlock_kernel();
496 return afs_convert_code(code);
500 #if defined(AFS_LINUX24_ENV)
501 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
503 afs_linux_fsync(struct file *fp, struct dentry *dp)
507 struct inode *ip = FILE_INODE(fp);
508 cred_t *credp = crref();
510 #ifdef AFS_LINUX24_ENV
514 code = afs_fsync(VTOAFS(ip), credp);
516 #ifdef AFS_LINUX24_ENV
517 maybe_unlock_kernel();
520 return afs_convert_code(code);
526 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
529 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
530 cred_t *credp = crref();
531 struct AFS_FLOCK flock;
532 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
533 struct file_lock conflict;
534 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
535 struct file_lock *conflict;
538 /* Convert to a lock format afs_lockctl understands. */
539 memset((char *)&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 flock.l_len = flp->fl_end - flp->fl_start + 1;
546 /* Safe because there are no large files, yet */
547 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
548 if (cmd == F_GETLK64)
550 else if (cmd == F_SETLK64)
552 else if (cmd == F_SETLKW64)
554 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
557 code = afs_lockctl(vcp, &flock, cmd, credp);
560 #ifdef AFS_LINUX24_ENV
561 if ((code == 0 || flp->fl_type == F_UNLCK) &&
562 (cmd == F_SETLK || cmd == F_SETLKW)) {
563 # ifdef POSIX_LOCK_FILE_WAIT_ARG
564 code = posix_lock_file(fp, flp, 0);
566 flp->fl_flags &=~ FL_SLEEP;
567 code = posix_lock_file(fp, flp);
569 if (code && flp->fl_type != F_UNLCK) {
570 struct AFS_FLOCK flock2;
572 flock2.l_type = F_UNLCK;
574 afs_lockctl(vcp, &flock2, F_SETLK, credp);
578 /* If lockctl says there are no conflicting locks, then also check with the
579 * kernel, as lockctl knows nothing about byte range locks
581 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
582 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
583 if (posix_test_lock(fp, flp, &conflict)) {
584 locks_copy_lock(flp, &conflict);
585 flp->fl_type = F_UNLCK;
589 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
590 if ((conflict = posix_test_lock(fp, flp))) {
591 locks_copy_lock(flp, conflict);
592 flp->fl_type = F_UNLCK;
597 posix_test_lock(fp, flp);
598 /* If we found a lock in the kernel's structure, return it */
599 if (flp->fl_type != F_UNLCK) {
607 /* Convert flock back to Linux's file_lock */
608 flp->fl_type = flock.l_type;
609 flp->fl_pid = flock.l_pid;
610 flp->fl_start = flock.l_start;
611 flp->fl_end = flock.l_start + flock.l_len - 1;
614 return afs_convert_code(code);
617 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
619 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
621 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
622 cred_t *credp = crref();
623 struct AFS_FLOCK flock;
624 /* Convert to a lock format afs_lockctl understands. */
625 memset((char *)&flock, 0, sizeof(flock));
626 flock.l_type = flp->fl_type;
627 flock.l_pid = flp->fl_pid;
630 flock.l_len = OFFSET_MAX;
632 /* Safe because there are no large files, yet */
633 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
634 if (cmd == F_GETLK64)
636 else if (cmd == F_SETLK64)
638 else if (cmd == F_SETLKW64)
640 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
643 code = afs_lockctl(vcp, &flock, cmd, credp);
646 if ((code == 0 || flp->fl_type == F_UNLCK) &&
647 (cmd == F_SETLK || cmd == F_SETLKW)) {
648 flp->fl_flags &=~ FL_SLEEP;
649 code = flock_lock_file_wait(fp, flp);
650 if (code && flp->fl_type != F_UNLCK) {
651 struct AFS_FLOCK flock2;
653 flock2.l_type = F_UNLCK;
655 afs_lockctl(vcp, &flock2, F_SETLK, credp);
659 /* Convert flock back to Linux's file_lock */
660 flp->fl_type = flock.l_type;
661 flp->fl_pid = flock.l_pid;
664 return afs_convert_code(code);
669 * essentially the same as afs_fsync() but we need to get the return
670 * code for the sys_close() here, not afs_linux_release(), so call
671 * afs_StoreAllSegments() with AFS_LASTSTORE
674 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
675 afs_linux_flush(struct file *fp, fl_owner_t id)
677 afs_linux_flush(struct file *fp)
680 struct vrequest treq;
684 #if defined(AFS_CACHE_BYPASS)
690 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
698 vcp = VTOAFS(FILE_INODE(fp));
700 code = afs_InitReq(&treq, credp);
703 #if defined(AFS_CACHE_BYPASS)
704 /* If caching is bypassed for this file, or globally, just return 0 */
705 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
708 ObtainReadLock(&vcp->lock);
709 if(vcp->cachingStates & FCSBypass)
711 ReleaseReadLock(&vcp->lock);
714 /* future proof: don't rely on 0 return from afs_InitReq */
719 ObtainSharedLock(&vcp->lock, 535);
720 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
721 UpgradeSToWLock(&vcp->lock, 536);
722 if (!AFS_IS_DISCONNECTED) {
723 code = afs_StoreAllSegments(vcp,
725 AFS_SYNC | AFS_LASTSTORE);
727 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
729 ConvertWToSLock(&vcp->lock);
731 code = afs_CheckCode(code, &treq, 54);
732 ReleaseSharedLock(&vcp->lock);
739 return afs_convert_code(code);
742 #if !defined(AFS_LINUX24_ENV)
743 /* Not allowed to directly read a directory. */
745 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
753 struct file_operations afs_dir_fops = {
754 #if !defined(AFS_LINUX24_ENV)
755 .read = afs_linux_dir_read,
756 .lock = afs_linux_lock,
757 .fsync = afs_linux_fsync,
759 .read = generic_read_dir,
761 .readdir = afs_linux_readdir,
762 #ifdef HAVE_UNLOCKED_IOCTL
763 .unlocked_ioctl = afs_unlocked_xioctl,
767 #ifdef HAVE_COMPAT_IOCTL
768 .compat_ioctl = afs_unlocked_xioctl,
770 .open = afs_linux_open,
771 .release = afs_linux_release,
774 struct file_operations afs_file_fops = {
775 .read = afs_linux_read,
776 .write = afs_linux_write,
777 #ifdef GENERIC_FILE_AIO_READ
778 .aio_read = generic_file_aio_read,
779 .aio_write = generic_file_aio_write,
781 #ifdef HAVE_UNLOCKED_IOCTL
782 .unlocked_ioctl = afs_unlocked_xioctl,
786 #ifdef HAVE_COMPAT_IOCTL
787 .compat_ioctl = afs_unlocked_xioctl,
789 .mmap = afs_linux_mmap,
790 .open = afs_linux_open,
791 .flush = afs_linux_flush,
792 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
793 .sendfile = generic_file_sendfile,
795 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
796 .splice_write = generic_file_splice_write,
797 .splice_read = generic_file_splice_read,
799 .release = afs_linux_release,
800 .fsync = afs_linux_fsync,
801 .lock = afs_linux_lock,
802 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
803 .flock = afs_linux_flock,
808 /**********************************************************************
809 * AFS Linux dentry operations
810 **********************************************************************/
812 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
813 * that has its mvid (parent dir's fid) pointer set to the wrong directory
814 * due to being mounted in multiple points at once. If so, check_bad_parent()
815 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
816 * dotdotfid and mtpoint fid members.
818 * dp - dentry to be checked.
822 * This dentry's vcache's mvid will be set to the correct parent directory's
824 * This root vnode's volume will have its dotdotfid and mtpoint fids set
825 * to the correct parent and mountpoint fids.
829 check_bad_parent(struct dentry *dp)
832 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
833 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
835 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
838 /* force a lookup, so vcp->mvid is fixed up */
839 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
840 if (!avc || vcp != avc) { /* bad, very bad.. */
841 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
842 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
843 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
844 ICL_TYPE_POINTER, dp);
847 AFS_RELE(AFSTOV(avc));
854 /* afs_linux_revalidate
855 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
858 afs_linux_revalidate(struct dentry *dp)
861 struct vcache *vcp = VTOAFS(dp->d_inode);
865 if (afs_shuttingdown)
868 #ifdef AFS_LINUX24_ENV
874 /* Make this a fast path (no crref), since it's called so often. */
875 if (vcp->f.states & CStatd) {
877 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
878 check_bad_parent(dp); /* check and correct mvid */
881 #ifdef AFS_LINUX24_ENV
888 /* This avoids the crref when we don't have to do it. Watch for
889 * changes in afs_getattr that don't get replicated here!
891 if (vcp->f.states & CStatd &&
892 (!afs_fakestat_enable || vcp->mvstat != 1) &&
894 code = afs_CopyOutAttrs(vcp, &vattr);
897 code = afs_getattr(vcp, &vattr, credp);
901 afs_fill_inode(AFSTOV(vcp), &vattr);
904 #ifdef AFS_LINUX24_ENV
905 maybe_unlock_kernel();
908 return afs_convert_code(code);
911 #if defined(AFS_LINUX26_ENV)
913 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
915 int err = afs_linux_revalidate(dentry);
917 generic_fillattr(dentry->d_inode, stat);
923 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
924 * In kernels 2.2.10 and above, we are passed an additional flags var which
925 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
926 * we are advised to follow the entry if it is a link or to make sure that
927 * it is a directory. But since the kernel itself checks these possibilities
928 * later on, we shouldn't have to do it until later. Perhaps in the future..
931 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
932 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
933 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
935 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
938 afs_linux_dentry_revalidate(struct dentry *dp)
942 cred_t *credp = NULL;
943 struct vcache *vcp, *pvcp, *tvc = NULL;
945 struct afs_fakestat_state fakestate;
947 #ifdef AFS_LINUX24_ENV
951 afs_InitFakeStat(&fakestate);
955 vcp = VTOAFS(dp->d_inode);
956 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
958 if (vcp == afs_globalVp)
961 if (vcp->mvstat == 1) { /* mount point */
962 if (vcp->mvid && (vcp->f.states & CMValid)) {
965 struct vrequest treq;
968 code = afs_InitReq(&treq, credp);
970 #ifdef AFS_DARWIN_ENV
971 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
972 (strcmp(dp->d_name.name, "Contents") == 0) ||
974 (strcmp(dp->d_name.name, ".directory") == 0)) {
978 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
980 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
981 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
982 /* a mount point, not yet replaced by its directory */
987 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
988 check_bad_parent(dp); /* check and correct mvid */
991 /* If the last looker changes, we should make sure the current
992 * looker still has permission to examine this file. This would
993 * always require a crref() which would be "slow".
995 if (vcp->last_looker != treq.uid) {
996 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
999 vcp->last_looker = treq.uid;
1003 /* If the parent's DataVersion has changed or the vnode
1004 * is longer valid, we need to do a full lookup. VerifyVCache
1005 * isn't enough since the vnode may have been renamed.
1008 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
1011 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1012 if (!tvc || tvc != vcp)
1015 if (afs_getattr(vcp, &vattr, credp))
1018 vattr2inode(AFSTOV(vcp), &vattr);
1019 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1022 /* should we always update the attributes at this point? */
1023 /* unlikely--the vcache entry hasn't changed */
1027 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
1028 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1032 /* No change in parent's DataVersion so this negative
1033 * lookup is still valid. BUT, if a server is down a
1034 * negative lookup can result so there should be a
1035 * liftime as well. For now, always expire.
1048 afs_PutFakeStat(&fakestate);
1054 shrink_dcache_parent(dp);
1057 #ifdef AFS_LINUX24_ENV
1058 maybe_unlock_kernel();
1063 if (have_submounts(dp))
1071 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1073 struct vcache *vcp = VTOAFS(ip);
1076 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1077 (void) afs_InactiveVCache(vcp, NULL);
1080 #ifdef DCACHE_NFSFS_RENAMED
1081 #ifdef AFS_LINUX26_ENV
1082 spin_lock(&dp->d_lock);
1084 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1085 #ifdef AFS_LINUX26_ENV
1086 spin_unlock(&dp->d_lock);
1094 afs_dentry_delete(struct dentry *dp)
1096 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1097 return 1; /* bad inode? */
1102 struct dentry_operations afs_dentry_operations = {
1103 .d_revalidate = afs_linux_dentry_revalidate,
1104 .d_delete = afs_dentry_delete,
1105 .d_iput = afs_dentry_iput,
1108 /**********************************************************************
1109 * AFS Linux inode operations
1110 **********************************************************************/
1114 * Merely need to set enough of vattr to get us through the create. Note
1115 * that the higher level code (open_namei) will take care of any tuncation
1116 * explicitly. Exclusive open is also taken care of in open_namei.
1118 * name is in kernel space at this point.
1121 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1122 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1123 struct nameidata *nd)
1125 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1129 cred_t *credp = crref();
1130 const char *name = dp->d_name.name;
1135 vattr.va_mode = mode;
1136 vattr.va_type = mode & S_IFMT;
1138 #if defined(AFS_LINUX26_ENV)
1139 maybe_lock_kernel();
1142 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1146 struct inode *ip = AFSTOV(vcp);
1148 afs_getattr(vcp, &vattr, credp);
1149 afs_fill_inode(ip, &vattr);
1150 insert_inode_hash(ip);
1151 dp->d_op = &afs_dentry_operations;
1152 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1153 d_instantiate(dp, ip);
1157 #if defined(AFS_LINUX26_ENV)
1158 maybe_unlock_kernel();
1161 return afs_convert_code(code);
1164 /* afs_linux_lookup */
1165 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1166 static struct dentry *
1167 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1168 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1169 struct nameidata *nd)
1171 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1175 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1178 cred_t *credp = crref();
1179 struct vcache *vcp = NULL;
1180 const char *comp = dp->d_name.name;
1181 struct inode *ip = NULL;
1182 #if defined(AFS_LINUX26_ENV)
1183 struct dentry *newdp = NULL;
1187 #if defined(AFS_LINUX26_ENV)
1188 maybe_lock_kernel();
1191 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1197 afs_getattr(vcp, &vattr, credp);
1198 afs_fill_inode(ip, &vattr);
1200 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1201 hlist_unhashed(&ip->i_hash)
1202 #elif defined(AFS_LINUX26_ENV)
1203 ip->i_hash.pprev == NULL
1205 ip->i_hash.prev == NULL
1208 insert_inode_hash(ip);
1210 dp->d_op = &afs_dentry_operations;
1211 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1214 #if defined(AFS_LINUX24_ENV)
1215 if (ip && S_ISDIR(ip->i_mode)) {
1216 struct dentry *alias;
1218 /* Try to invalidate an existing alias in favor of our new one */
1219 alias = d_find_alias(ip);
1220 #if defined(AFS_LINUX26_ENV)
1221 /* But not if it's disconnected; then we want d_splice_alias below */
1222 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1226 if (d_invalidate(alias) == 0) {
1230 #if defined(AFS_LINUX26_ENV)
1239 #if defined(AFS_LINUX26_ENV)
1240 newdp = d_splice_alias(ip, dp);
1245 #if defined(AFS_LINUX26_ENV)
1246 maybe_unlock_kernel();
1250 /* It's ok for the file to not be found. That's noted by the caller by
1251 * seeing that the dp->d_inode field is NULL.
1253 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1254 #if defined(AFS_LINUX26_ENV)
1255 if (!code || code == ENOENT)
1262 return ERR_PTR(afs_convert_code(code));
1266 return afs_convert_code(code);
1271 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1274 cred_t *credp = crref();
1275 const char *name = newdp->d_name.name;
1276 struct inode *oldip = olddp->d_inode;
1278 /* If afs_link returned the vnode, we could instantiate the
1279 * dentry. Since it's not, we drop this one and do a new lookup.
1284 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1288 return afs_convert_code(code);
1292 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1295 cred_t *credp = crref();
1296 const char *name = dp->d_name.name;
1297 struct vcache *tvc = VTOAFS(dp->d_inode);
1299 #if defined(AFS_LINUX26_ENV)
1300 maybe_lock_kernel();
1302 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1303 && !(tvc->f.states & CUnlinked)) {
1304 struct dentry *__dp;
1314 osi_FreeSmallSpace(__name);
1315 __name = afs_newname();
1318 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1322 } while (__dp->d_inode != NULL);
1325 code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1327 tvc->mvid = (void *) __name;
1330 crfree(tvc->uncred);
1332 tvc->uncred = credp;
1333 tvc->f.states |= CUnlinked;
1334 #ifdef DCACHE_NFSFS_RENAMED
1335 #ifdef AFS_LINUX26_ENV
1336 spin_lock(&dp->d_lock);
1338 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1339 #ifdef AFS_LINUX26_ENV
1340 spin_unlock(&dp->d_lock);
1344 osi_FreeSmallSpace(__name);
1349 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1358 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1363 #if defined(AFS_LINUX26_ENV)
1364 maybe_unlock_kernel();
1367 return afs_convert_code(code);
1372 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1375 cred_t *credp = crref();
1377 const char *name = dp->d_name.name;
1379 /* If afs_symlink returned the vnode, we could instantiate the
1380 * dentry. Since it's not, we drop this one and do a new lookup.
1386 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1389 return afs_convert_code(code);
1393 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1396 cred_t *credp = crref();
1397 struct vcache *tvcp = NULL;
1399 const char *name = dp->d_name.name;
1401 #if defined(AFS_LINUX26_ENV)
1402 maybe_lock_kernel();
1405 vattr.va_mask = ATTR_MODE;
1406 vattr.va_mode = mode;
1408 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1411 struct inode *ip = AFSTOV(tvcp);
1413 afs_getattr(tvcp, &vattr, credp);
1414 afs_fill_inode(ip, &vattr);
1416 dp->d_op = &afs_dentry_operations;
1417 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1418 d_instantiate(dp, ip);
1422 #if defined(AFS_LINUX26_ENV)
1423 maybe_unlock_kernel();
1426 return afs_convert_code(code);
1430 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1433 cred_t *credp = crref();
1434 const char *name = dp->d_name.name;
1436 /* locking kernel conflicts with glock? */
1439 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1442 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1443 * that failed because a directory is not empty. So, we map
1444 * EEXIST to ENOTEMPTY on linux.
1446 if (code == EEXIST) {
1455 return afs_convert_code(code);
1460 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1461 struct inode *newip, struct dentry *newdp)
1464 cred_t *credp = crref();
1465 const char *oldname = olddp->d_name.name;
1466 const char *newname = newdp->d_name.name;
1467 struct dentry *rehash = NULL;
1469 #if defined(AFS_LINUX26_ENV)
1470 /* Prevent any new references during rename operation. */
1471 maybe_lock_kernel();
1473 if (!d_unhashed(newdp)) {
1478 if (!list_empty(&newdp->d_hash)) {
1484 #if defined(AFS_LINUX24_ENV)
1485 if (atomic_read(&olddp->d_count) > 1)
1486 shrink_dcache_parent(olddp);
1490 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1494 olddp->d_time = 0; /* force to revalidate */
1499 #if defined(AFS_LINUX26_ENV)
1500 maybe_unlock_kernel();
1504 return afs_convert_code(code);
1508 /* afs_linux_ireadlink
1509 * Internal readlink which can return link contents to user or kernel space.
1510 * Note that the buffer is NOT supposed to be null-terminated.
1513 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1516 cred_t *credp = crref();
1520 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1521 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1525 return maxlen - tuio.uio_resid;
1527 return afs_convert_code(code);
1530 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1531 /* afs_linux_readlink
1532 * Fill target (which is in user space) with contents of symlink.
1535 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1538 struct inode *ip = dp->d_inode;
1541 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1547 /* afs_linux_follow_link
1548 * a file system dependent link following routine.
1550 #if defined(AFS_LINUX24_ENV)
1551 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1556 name = osi_Alloc(PATH_MAX);
1562 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1570 code = vfs_follow_link(nd, name);
1573 osi_Free(name, PATH_MAX);
1578 #else /* !defined(AFS_LINUX24_ENV) */
1580 static struct dentry *
1581 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1582 unsigned int follow)
1590 name = osi_Alloc(PATH_MAX + 1);
1594 return ERR_PTR(-EIO);
1597 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1602 if (code < -MAX_ERRNO)
1603 res = ERR_PTR(-EIO);
1605 res = ERR_PTR(code);
1608 res = lookup_dentry(name, basep, follow);
1612 osi_Free(name, PATH_MAX + 1);
1616 #endif /* AFS_LINUX24_ENV */
1617 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1619 #if defined(AFS_CACHE_BYPASS)
1621 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1624 afs_linux_can_bypass(struct inode *ip) {
1625 switch(cache_bypass_strategy) {
1626 case NEVER_BYPASS_CACHE:
1628 case ALWAYS_BYPASS_CACHE:
1630 case LARGE_FILES_BYPASS_CACHE:
1631 if(i_size_read(ip) > cache_bypass_threshold)
1638 /* The kernel calls readpages before trying readpage, with a list of
1639 * pages. The readahead algorithm expands num_pages when it thinks
1640 * the application will benefit. Unlike readpage, the pages are not
1641 * necessarily allocated. If we do not a) allocate required pages and
1642 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1643 * for each required page (and the page will be pre-allocated) */
1646 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1647 struct list_head *page_list, unsigned num_pages)
1652 struct iovec* iovecp;
1653 struct nocache_read_request *ancr;
1654 struct page *pp, *ppt;
1655 struct pagevec lrupv;
1659 struct inode *ip = FILE_INODE(fp);
1660 struct vcache *avc = VTOAFS(ip);
1661 afs_int32 bypasscache = 0; /* bypass for this read */
1662 afs_int32 base_index = 0;
1663 afs_int32 page_count = 0;
1667 bypasscache = afs_linux_can_bypass(ip);
1669 /* In the new incarnation of selective caching, a file's caching policy
1670 * can change, eg because file size exceeds threshold, etc. */
1671 trydo_cache_transition(avc, credp, bypasscache);
1674 while(!list_empty(page_list)) {
1675 pp = list_entry(page_list->prev, struct page, lru);
1680 /* background thread must free: iovecp, auio, ancr */
1681 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1683 auio = osi_Alloc(sizeof(uio_t));
1684 auio->uio_iov = iovecp;
1685 auio->uio_iovcnt = num_pages;
1686 auio->uio_flag = UIO_READ;
1687 auio->uio_seg = AFS_UIOSYS;
1688 auio->uio_resid = num_pages * PAGE_SIZE;
1690 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1692 ancr->offset = auio->uio_offset;
1693 ancr->length = auio->uio_resid;
1695 pagevec_init(&lrupv, 0);
1697 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1699 if(list_empty(page_list))
1702 pp = list_entry(page_list->prev, struct page, lru);
1703 /* If we allocate a page and don't remove it from page_list,
1704 * the page cache gets upset. */
1706 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1707 if(pp->index > isize) {
1714 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1715 auio->uio_offset = offset;
1716 base_index = pp->index;
1718 iovecp[page_ix].iov_len = PAGE_SIZE;
1719 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1720 if(base_index != pp->index) {
1723 page_cache_release(pp);
1724 iovecp[page_ix].iov_base = (void *) 0;
1732 page_cache_release(pp);
1733 iovecp[page_ix].iov_base = (void *) 0;
1736 if(!PageLocked(pp)) {
1740 /* save the page for background map */
1741 iovecp[page_ix].iov_base = (void*) pp;
1743 /* and put it on the LRU cache */
1744 if (!pagevec_add(&lrupv, pp))
1745 __pagevec_lru_add(&lrupv);
1749 /* If there were useful pages in the page list, make sure all pages
1750 * are in the LRU cache, then schedule the read */
1752 pagevec_lru_add(&lrupv);
1753 code = afs_ReadNoCache(avc, ancr, credp);
1755 /* If there is nothing for the background thread to handle,
1756 * it won't be freeing the things that we never gave it */
1757 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1758 osi_Free(auio, sizeof(uio_t));
1759 osi_Free(ancr, sizeof(struct nocache_read_request));
1761 /* we do not flush, release, or unmap pages--that will be
1762 * done for us by the background thread as each page comes in
1763 * from the fileserver */
1767 return afs_convert_code(code);
1770 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1771 #endif /* defined(AFS_CACHE_BYPASS */
1774 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1776 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1777 struct inode *ip = FILE_INODE(fp);
1778 struct vcache *avc = VTOAFS(ip);
1780 struct file *cacheFp;
1784 mm_segment_t old_fs;
1786 /* Not a UFS cache, don't do anything */
1787 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1790 /* Can't do anything if the vcache isn't statd , or if the read
1791 * crosses a chunk boundary.
1793 if (!(avc->f.states & CStatd) ||
1794 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1798 ObtainWriteLock(&avc->lock, 911);
1800 /* XXX - See if hinting actually makes things faster !!! */
1802 /* See if we have a suitable entry already cached */
1806 /* We need to lock xdcache, then dcache, to handle situations where
1807 * the hint is on the free list. However, we can't safely do this
1808 * according to the locking hierarchy. So, use a non blocking lock.
1810 ObtainReadLock(&afs_xdcache);
1811 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1813 if (dcLocked && (tdc->index != NULLIDX)
1814 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1815 && tdc->f.chunk == AFS_CHUNK(offset)
1816 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1817 /* Bonus - the hint was correct */
1820 /* Only destroy the hint if its actually invalid, not if there's
1821 * just been a locking failure */
1823 ReleaseReadLock(&tdc->lock);
1830 ReleaseReadLock(&afs_xdcache);
1833 /* No hint, or hint is no longer valid - see if we can get something
1834 * directly from the dcache
1837 tdc = afs_FindDCache(avc, offset);
1840 ReleaseWriteLock(&avc->lock);
1845 ObtainReadLock(&tdc->lock);
1847 /* Is the dcache we've been given currently up to date */
1848 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1849 (tdc->dflags & DFFetching)) {
1850 ReleaseWriteLock(&avc->lock);
1851 ReleaseReadLock(&tdc->lock);
1856 /* Update our hint for future abuse */
1859 /* Okay, so we've now got a cache file that is up to date */
1861 offset -= AFS_CHUNKTOBASE(tdc->f.chunk);
1863 /* XXX - I suspect we should be locking the inodes before we use them! */
1865 cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1866 if (cacheFp->f_op->llseek)
1867 cacheFp->f_op->llseek(cacheFp, offset, 0);
1869 cacheFp->f_pos = offset;
1875 size = cacheFp->f_op->read(cacheFp, address, PAGE_SIZE, &cacheFp->f_pos);
1879 if (size != PAGE_SIZE)
1880 memset((void *)(address + size), 0, PAGE_SIZE - size);
1882 flush_dcache_page(pp);
1883 SetPageUptodate(pp);
1889 filp_close(cacheFp, NULL);
1892 ReleaseReadLock(&tdc->lock);
1893 ReleaseWriteLock(&avc->lock);
1900 /* afs_linux_readpage
1901 * all reads come through here. A strategy-like read call.
1904 afs_linux_readpage(struct file *fp, struct page *pp)
1907 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1909 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1911 ulong address = afs_linux_page_address(pp);
1912 afs_offs_t offset = pageoff(pp);
1914 #if defined(AFS_CACHE_BYPASS)
1915 afs_int32 bypasscache = 0; /* bypass for this read */
1916 struct nocache_read_request *ancr;
1917 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1922 struct iovec *iovecp;
1923 struct inode *ip = FILE_INODE(fp);
1924 afs_int32 cnt = page_count(pp);
1925 struct vcache *avc = VTOAFS(ip);
1928 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1930 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1938 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1942 atomic_add(1, &pp->count);
1943 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1944 clear_bit(PG_error, &pp->flags);
1946 #if defined(AFS_CACHE_BYPASS)
1947 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1948 /* If the page is past the end of the file, skip it */
1949 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1950 if(pp->index > isize) {
1957 /* if bypasscache, receiver frees, else we do */
1958 auio = osi_Alloc(sizeof(uio_t));
1959 iovecp = osi_Alloc(sizeof(struct iovec));
1961 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1964 #if defined(AFS_CACHE_BYPASS)
1965 bypasscache = afs_linux_can_bypass(ip);
1967 /* In the new incarnation of selective caching, a file's caching policy
1968 * can change, eg because file size exceeds threshold, etc. */
1969 trydo_cache_transition(avc, credp, bypasscache);
1974 /* save the page for background map */
1975 auio->uio_iov->iov_base = (void*) pp;
1976 /* the background thread will free this */
1977 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1979 ancr->offset = offset;
1980 ancr->length = PAGE_SIZE;
1982 maybe_lock_kernel();
1983 code = afs_ReadNoCache(avc, ancr, credp);
1984 maybe_unlock_kernel();
1986 goto done; /* skips release page, doing it in bg thread */
1990 #ifdef AFS_LINUX24_ENV
1991 maybe_lock_kernel();
1995 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1996 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1997 99999); /* not a possible code value */
1999 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2001 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2002 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2004 AFS_DISCON_UNLOCK();
2006 #ifdef AFS_LINUX24_ENV
2007 maybe_unlock_kernel();
2010 /* XXX valid for no-cache also? Check last bits of files... :)
2011 * Cognate code goes in afs_NoCacheFetchProc. */
2012 if (auio->uio_resid) /* zero remainder of page */
2013 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2016 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2017 flush_dcache_page(pp);
2018 SetPageUptodate(pp);
2020 set_bit(PG_uptodate, &pp->flags);
2024 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2028 clear_bit(PG_locked, &pp->flags);
2033 #if defined(AFS_CACHE_BYPASS)
2034 /* do not call afs_GetDCache if cache is bypassed */
2039 /* free if not bypassing cache */
2040 osi_Free(auio, sizeof(uio_t));
2041 osi_Free(iovecp, sizeof(struct iovec));
2043 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
2045 struct vrequest treq;
2048 code = afs_InitReq(&treq, credp);
2049 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2050 tdc = afs_FindDCache(avc, offset);
2052 if (!(tdc->mflags & DFNextStarted))
2053 afs_PrefetchChunk(avc, tdc, credp, &treq);
2056 ReleaseWriteLock(&avc->lock);
2061 #if defined(AFS_CACHE_BYPASS)
2065 return afs_convert_code(code);
2069 #if defined(AFS_LINUX24_ENV)
2071 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2072 unsigned long offset, unsigned int count)
2074 struct vcache *vcp = VTOAFS(ip);
2083 buffer = kmap(pp) + offset;
2084 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
2087 maybe_lock_kernel();
2089 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2090 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2091 ICL_TYPE_INT32, 99999);
2093 ObtainWriteLock(&vcp->lock, 532);
2094 if (vcp->f.states & CPageWrite) {
2095 ReleaseWriteLock(&vcp->lock);
2097 maybe_unlock_kernel();
2100 #ifdef AFS_LINUX26_ENV
2101 #if defined(WRITEPAGE_ACTIVATE)
2102 return WRITEPAGE_ACTIVATE;
2104 return AOP_WRITEPAGE_ACTIVATE;
2107 /* should mark it dirty? */
2111 vcp->f.states |= CPageWrite;
2112 ReleaseWriteLock(&vcp->lock);
2114 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2116 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2118 i_size_write(ip, vcp->f.m.Length);
2119 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2121 ObtainWriteLock(&vcp->lock, 533);
2123 struct vrequest treq;
2125 if (!afs_InitReq(&treq, credp))
2126 code = afs_DoPartialWrite(vcp, &treq);
2128 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2130 vcp->f.states &= ~CPageWrite;
2131 ReleaseWriteLock(&vcp->lock);
2133 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2134 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2135 ICL_TYPE_INT32, code);
2138 maybe_unlock_kernel();
2147 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2148 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2150 afs_linux_writepage(struct page *pp)
2153 struct address_space *mapping = pp->mapping;
2154 struct inode *inode;
2155 unsigned long end_index;
2156 unsigned offset = PAGE_CACHE_SIZE;
2159 #if defined(AFS_LINUX26_ENV)
2160 if (PageReclaim(pp)) {
2161 # if defined(WRITEPAGE_ACTIVATE)
2162 return WRITEPAGE_ACTIVATE;
2164 return AOP_WRITEPAGE_ACTIVATE;
2168 if (PageLaunder(pp)) {
2169 return(fail_writepage(pp));
2173 inode = (struct inode *)mapping->host;
2174 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
2177 if (pp->index < end_index)
2179 /* things got complicated... */
2180 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
2181 /* OK, are we completely out? */
2182 if (pp->index >= end_index + 1 || !offset)
2185 status = afs_linux_writepage_sync(inode, pp, 0, offset);
2186 SetPageUptodate(pp);
2187 #if defined(AFS_LINUX26_ENV)
2188 #if defined(WRITEPAGE_ACTIVATE)
2189 if ( status != WRITEPAGE_ACTIVATE )
2191 if ( status != AOP_WRITEPAGE_ACTIVATE )
2195 if (status == offset)
2201 /* afs_linux_updatepage
2202 * What one would have thought was writepage - write dirty page to file.
2203 * Called from generic_file_write. buffer is still in user space. pagep
2204 * has been filled in with old data if we're updating less than a page.
2207 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2208 unsigned int count, int sync)
2210 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2211 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2217 set_bit(PG_locked, &pp->flags);
2222 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2223 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2224 ICL_TYPE_INT32, 99999);
2225 setup_uio(&tuio, &iovec, page_addr + offset,
2226 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2229 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2231 i_size_write(ip, vcp->f.m.Length);
2232 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2235 struct vrequest treq;
2237 ObtainWriteLock(&vcp->lock, 533);
2238 vcp->f.m.Date = osi_Time(); /* set modification time */
2239 if (!afs_InitReq(&treq, credp))
2240 code = afs_DoPartialWrite(vcp, &treq);
2241 ReleaseWriteLock(&vcp->lock);
2244 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2245 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2246 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2247 ICL_TYPE_INT32, code);
2249 AFS_DISCON_UNLOCK();
2253 clear_bit(PG_locked, &pp->flags);
2258 /* afs_linux_permission
2259 * Check access rights - returns error if can't check or permission denied.
2262 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2263 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2265 afs_linux_permission(struct inode *ip, int mode)
2269 cred_t *credp = crref();
2273 if (mode & MAY_EXEC)
2275 if (mode & MAY_READ)
2277 if (mode & MAY_WRITE)
2279 code = afs_access(VTOAFS(ip), tmp, credp);
2283 return afs_convert_code(code);
2286 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2288 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2293 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2294 offset, to - offset);
2295 #if !defined(AFS_LINUX26_ENV)
2303 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2306 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2307 call kmap directly instead of relying on us to do it */
2308 #if !defined(AFS_LINUX26_ENV)
2315 #if defined(HAVE_WRITE_BEGIN)
2317 afs_linux_write_end(struct file *file, struct address_space *mapping,
2318 loff_t pos, unsigned len, unsigned copied,
2319 struct page *page, void *fsdata)
2322 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2324 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2327 page_cache_release(page);
2332 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2333 loff_t pos, unsigned len, unsigned flags,
2334 struct page **pagep, void **fsdata)
2337 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2338 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2339 page = grab_cache_page_write_begin(mapping, index, flags);
2341 page = __grab_cache_page(mapping, index);
2350 static struct inode_operations afs_file_iops = {
2351 #if defined(AFS_LINUX26_ENV)
2352 .permission = afs_linux_permission,
2353 .getattr = afs_linux_getattr,
2354 .setattr = afs_notify_change,
2355 #elif defined(AFS_LINUX24_ENV)
2356 .permission = afs_linux_permission,
2357 .revalidate = afs_linux_revalidate,
2358 .setattr = afs_notify_change,
2360 .default_file_ops = &afs_file_fops,
2361 .readpage = afs_linux_readpage,
2362 .revalidate = afs_linux_revalidate,
2363 .updatepage = afs_linux_updatepage,
2367 #if defined(AFS_LINUX24_ENV)
2368 static struct address_space_operations afs_file_aops = {
2369 .readpage = afs_linux_readpage,
2370 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2371 .readpages = afs_linux_readpages,
2373 .writepage = afs_linux_writepage,
2374 #if defined (HAVE_WRITE_BEGIN)
2375 .write_begin = afs_linux_write_begin,
2376 .write_end = afs_linux_write_end,
2378 .commit_write = afs_linux_commit_write,
2379 .prepare_write = afs_linux_prepare_write,
2385 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2386 * by what sort of operation is allowed.....
2389 static struct inode_operations afs_dir_iops = {
2390 #if !defined(AFS_LINUX24_ENV)
2391 .default_file_ops = &afs_dir_fops,
2393 .setattr = afs_notify_change,
2395 .create = afs_linux_create,
2396 .lookup = afs_linux_lookup,
2397 .link = afs_linux_link,
2398 .unlink = afs_linux_unlink,
2399 .symlink = afs_linux_symlink,
2400 .mkdir = afs_linux_mkdir,
2401 .rmdir = afs_linux_rmdir,
2402 .rename = afs_linux_rename,
2403 #if defined(AFS_LINUX26_ENV)
2404 .getattr = afs_linux_getattr,
2406 .revalidate = afs_linux_revalidate,
2408 .permission = afs_linux_permission,
2411 /* We really need a separate symlink set of ops, since do_follow_link()
2412 * determines if it _is_ a link by checking if the follow_link op is set.
2414 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2416 afs_symlink_filler(struct file *file, struct page *page)
2418 struct inode *ip = (struct inode *)page->mapping->host;
2419 char *p = (char *)kmap(page);
2422 maybe_lock_kernel();
2424 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2429 p[code] = '\0'; /* null terminate? */
2430 maybe_unlock_kernel();
2432 SetPageUptodate(page);
2438 maybe_unlock_kernel();
2446 static struct address_space_operations afs_symlink_aops = {
2447 .readpage = afs_symlink_filler
2449 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2451 static struct inode_operations afs_symlink_iops = {
2452 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2453 .readlink = page_readlink,
2454 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2455 .follow_link = page_follow_link,
2457 .follow_link = page_follow_link_light,
2458 .put_link = page_put_link,
2460 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2461 .readlink = afs_linux_readlink,
2462 .follow_link = afs_linux_follow_link,
2463 #if !defined(AFS_LINUX24_ENV)
2464 .permission = afs_linux_permission,
2465 .revalidate = afs_linux_revalidate,
2467 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2468 #if defined(AFS_LINUX24_ENV)
2469 .setattr = afs_notify_change,
2474 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2478 vattr2inode(ip, vattr);
2480 #if defined(AFS_LINUX26_ENV)
2481 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2483 /* Reset ops if symlink or directory. */
2484 if (S_ISREG(ip->i_mode)) {
2485 ip->i_op = &afs_file_iops;
2486 #if defined(AFS_LINUX24_ENV)
2487 ip->i_fop = &afs_file_fops;
2488 ip->i_data.a_ops = &afs_file_aops;
2491 } else if (S_ISDIR(ip->i_mode)) {
2492 ip->i_op = &afs_dir_iops;
2493 #if defined(AFS_LINUX24_ENV)
2494 ip->i_fop = &afs_dir_fops;
2497 } else if (S_ISLNK(ip->i_mode)) {
2498 ip->i_op = &afs_symlink_iops;
2499 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2500 ip->i_data.a_ops = &afs_symlink_aops;
2501 ip->i_mapping = &ip->i_data;