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"
45 #include "osi_pagecopy.h"
48 #define pageoff(pp) pgoff2loff((pp)->index)
50 #define pageoff(pp) pp->offset
53 #ifndef __pagevec_lru_add_file
54 #define __pagevec_lru_add_file __pagevec_lru_add
58 #define MAX_ERRNO 1000L
61 #if defined(AFS_LINUX26_ENV)
62 #define LockPage(pp) lock_page(pp)
63 #define UnlockPage(pp) unlock_page(pp)
64 extern struct backing_dev_info afs_backing_dev_info;
67 extern struct vcache *afs_globalVp;
68 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
69 #if defined(AFS_LINUX24_ENV)
70 /* Some uses of BKL are perhaps not needed for bypass or memcache--
71 * why don't we try it out? */
72 extern struct afs_cacheOps afs_UfsCacheOps;
73 #define maybe_lock_kernel() \
75 if(afs_cacheType == &afs_UfsCacheOps) \
80 #define maybe_unlock_kernel() \
82 if(afs_cacheType == &afs_UfsCacheOps) \
85 #endif /* AFS_LINUX24_ENV */
88 /* This function converts a positive error code from AFS into a negative
89 * code suitable for passing into the Linux VFS layer. It checks that the
90 * error code is within the permissable bounds for the ERR_PTR mechanism.
92 * _All_ error codes which come from the AFS layer should be passed through
93 * this function before being returned to the kernel.
96 static inline int afs_convert_code(int code) {
97 if ((code >= 0) && (code <= MAX_ERRNO))
103 /* Linux doesn't require a credp for many functions, and crref is an expensive
104 * operation. This helper function avoids obtaining it for VerifyVCache calls
107 static inline int afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
108 cred_t *credp = NULL;
109 struct vrequest treq;
112 if (avc->f.states & CStatd) {
120 code = afs_InitReq(&treq, credp);
122 code = afs_VerifyVCache2(avc, &treq);
129 return afs_convert_code(code);
133 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
136 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
137 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
138 afs_size_t isize, offindex;
142 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
143 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
145 code = afs_linux_VerifyVCache(vcp, NULL);
148 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
149 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
150 offindex = *offp >> PAGE_CACHE_SHIFT;
151 if(offindex > isize) {
156 /* Linux's FlushPages implementation doesn't ever use credp,
157 * so we optimise by not using it */
158 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
161 code = do_sync_read(fp, buf, count, offp);
163 code = generic_file_read(fp, buf, count, offp);
168 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
169 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
171 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
179 /* Now we have integrated VM for writes as well as reads. generic_file_write
180 * also takes care of re-positioning the pointer if file is open in append
181 * mode. Call fake open/close to ensure we do writes of core dumps.
184 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
187 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
192 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
193 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
194 (fp->f_flags & O_APPEND) ? 99998 : 99999);
196 code = afs_linux_VerifyVCache(vcp, &credp);
198 ObtainWriteLock(&vcp->lock, 529);
200 ReleaseWriteLock(&vcp->lock);
204 code = do_sync_write(fp, buf, count, offp);
206 code = generic_file_write(fp, buf, count, offp);
211 ObtainWriteLock(&vcp->lock, 530);
213 if (vcp->execsOrWriters == 1 && !credp)
216 afs_FakeClose(vcp, credp);
217 ReleaseWriteLock(&vcp->lock);
219 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
220 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
229 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
231 /* This is a complete rewrite of afs_readdir, since we can make use of
232 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
233 * handling and use of bulkstats will need to be reflected here as well.
236 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
238 struct vcache *avc = VTOAFS(FILE_INODE(fp));
239 struct vrequest treq;
240 register struct dcache *tdc;
247 afs_size_t origOffset, tlen;
248 cred_t *credp = crref();
249 struct afs_fakestat_state fakestat;
251 #if defined(AFS_LINUX26_ENV)
255 AFS_STATCNT(afs_readdir);
257 code = afs_convert_code(afs_InitReq(&treq, credp));
262 afs_InitFakeStat(&fakestat);
263 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
267 /* update the cache entry */
269 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
273 /* get a reference to the entire directory */
274 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
280 ObtainSharedLock(&avc->lock, 810);
281 UpgradeSToWLock(&avc->lock, 811);
282 ObtainReadLock(&tdc->lock);
284 * Make sure that the data in the cache is current. There are two
285 * cases we need to worry about:
286 * 1. The cache data is being fetched by another process.
287 * 2. The cache data is no longer valid
289 while ((avc->f.states & CStatd)
290 && (tdc->dflags & DFFetching)
291 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
292 ReleaseReadLock(&tdc->lock);
293 ReleaseSharedLock(&avc->lock);
294 afs_osi_Sleep(&tdc->validPos);
295 ObtainSharedLock(&avc->lock, 812);
296 ObtainReadLock(&tdc->lock);
298 if (!(avc->f.states & CStatd)
299 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
300 ReleaseReadLock(&tdc->lock);
301 ReleaseSharedLock(&avc->lock);
306 /* Set the readdir-in-progress flag, and downgrade the lock
307 * to shared so others will be able to acquire a read lock.
309 avc->f.states |= CReadDir;
310 avc->dcreaddir = tdc;
311 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
312 ConvertWToSLock(&avc->lock);
314 /* Fill in until we get an error or we're done. This implementation
315 * takes an offset in units of blobs, rather than bytes.
318 offset = (int) fp->f_pos;
320 dirpos = BlobScan(tdc, offset);
324 de = afs_dir_GetBlob(tdc, dirpos);
328 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
331 len = strlen(de->name);
333 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
334 (unsigned long)&tdc->f.inode, dirpos);
336 ReleaseSharedLock(&avc->lock);
342 /* filldir returns -EINVAL when the buffer is full. */
343 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
345 unsigned int type = DT_UNKNOWN;
346 struct VenusFid afid;
349 afid.Cell = avc->f.fid.Cell;
350 afid.Fid.Volume = avc->f.fid.Fid.Volume;
351 afid.Fid.Vnode = ntohl(de->fid.vnode);
352 afid.Fid.Unique = ntohl(de->fid.vunique);
353 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
355 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
358 } else if (((tvc->f.states) & (CStatd | CTruth))) {
359 /* CTruth will be set if the object has
364 else if (vtype == VREG)
366 /* Don't do this until we're sure it can't be a mtpt */
367 /* else if (vtype == VLNK)
369 /* what other types does AFS support? */
371 /* clean up from afs_FindVCache */
375 * If this is NFS readdirplus, then the filler is going to
376 * call getattr on this inode, which will deadlock if we're
380 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
384 code = (*filldir) (dirbuf, de->name, len, offset, ino);
389 offset = dirpos + 1 + ((len + 16) >> 5);
391 /* If filldir didn't fill in the last one this is still pointing to that
394 fp->f_pos = (loff_t) offset;
396 ReleaseReadLock(&tdc->lock);
398 UpgradeSToWLock(&avc->lock, 813);
399 avc->f.states &= ~CReadDir;
401 avc->readdir_pid = 0;
402 ReleaseSharedLock(&avc->lock);
406 afs_PutFakeStat(&fakestat);
409 #if defined(AFS_LINUX26_ENV)
410 maybe_unlock_kernel();
416 /* in afs_pioctl.c */
417 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
420 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
421 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
423 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
430 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
432 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
436 #if defined(AFS_LINUX24_ENV)
437 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
438 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
439 vmap->vm_end - vmap->vm_start);
441 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
442 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
443 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
447 /* get a validated vcache entry */
448 code = afs_linux_VerifyVCache(vcp, NULL);
450 /* Linux's Flushpage implementation doesn't use credp, so optimise
451 * our code to not need to crref() it */
452 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
454 code = generic_file_mmap(fp, vmap);
457 vcp->f.states |= CMAPPED;
464 afs_linux_open(struct inode *ip, struct file *fp)
466 struct vcache *vcp = VTOAFS(ip);
467 cred_t *credp = crref();
470 #ifdef AFS_LINUX24_ENV
474 code = afs_open(&vcp, fp->f_flags, credp);
476 #ifdef AFS_LINUX24_ENV
477 maybe_unlock_kernel();
481 return afs_convert_code(code);
485 afs_linux_release(struct inode *ip, struct file *fp)
487 struct vcache *vcp = VTOAFS(ip);
488 cred_t *credp = crref();
491 #ifdef AFS_LINUX24_ENV
495 code = afs_close(vcp, fp->f_flags, credp);
497 #ifdef AFS_LINUX24_ENV
498 maybe_unlock_kernel();
502 return afs_convert_code(code);
506 #if defined(AFS_LINUX24_ENV)
507 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
509 afs_linux_fsync(struct file *fp, struct dentry *dp)
513 struct inode *ip = FILE_INODE(fp);
514 cred_t *credp = crref();
516 #ifdef AFS_LINUX24_ENV
520 code = afs_fsync(VTOAFS(ip), credp);
522 #ifdef AFS_LINUX24_ENV
523 maybe_unlock_kernel();
526 return afs_convert_code(code);
532 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
535 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
536 cred_t *credp = crref();
537 struct AFS_FLOCK flock;
538 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
539 struct file_lock conflict;
540 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
541 struct file_lock *conflict;
544 /* Convert to a lock format afs_lockctl understands. */
545 memset((char *)&flock, 0, sizeof(flock));
546 flock.l_type = flp->fl_type;
547 flock.l_pid = flp->fl_pid;
549 flock.l_start = flp->fl_start;
550 flock.l_len = flp->fl_end - flp->fl_start + 1;
552 /* Safe because there are no large files, yet */
553 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
554 if (cmd == F_GETLK64)
556 else if (cmd == F_SETLK64)
558 else if (cmd == F_SETLKW64)
560 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
563 code = afs_lockctl(vcp, &flock, cmd, credp);
566 #ifdef AFS_LINUX24_ENV
567 if ((code == 0 || flp->fl_type == F_UNLCK) &&
568 (cmd == F_SETLK || cmd == F_SETLKW)) {
569 # ifdef POSIX_LOCK_FILE_WAIT_ARG
570 code = posix_lock_file(fp, flp, 0);
572 flp->fl_flags &=~ FL_SLEEP;
573 code = posix_lock_file(fp, flp);
575 if (code && flp->fl_type != F_UNLCK) {
576 struct AFS_FLOCK flock2;
578 flock2.l_type = F_UNLCK;
580 afs_lockctl(vcp, &flock2, F_SETLK, credp);
584 /* If lockctl says there are no conflicting locks, then also check with the
585 * kernel, as lockctl knows nothing about byte range locks
587 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
588 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
589 if (posix_test_lock(fp, flp, &conflict)) {
590 locks_copy_lock(flp, &conflict);
591 flp->fl_type = F_UNLCK;
595 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
596 if ((conflict = posix_test_lock(fp, flp))) {
597 locks_copy_lock(flp, conflict);
598 flp->fl_type = F_UNLCK;
603 posix_test_lock(fp, flp);
604 /* If we found a lock in the kernel's structure, return it */
605 if (flp->fl_type != F_UNLCK) {
613 /* Convert flock back to Linux's file_lock */
614 flp->fl_type = flock.l_type;
615 flp->fl_pid = flock.l_pid;
616 flp->fl_start = flock.l_start;
617 flp->fl_end = flock.l_start + flock.l_len - 1;
620 return afs_convert_code(code);
623 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
625 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
627 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
628 cred_t *credp = crref();
629 struct AFS_FLOCK flock;
630 /* Convert to a lock format afs_lockctl understands. */
631 memset((char *)&flock, 0, sizeof(flock));
632 flock.l_type = flp->fl_type;
633 flock.l_pid = flp->fl_pid;
636 flock.l_len = OFFSET_MAX;
638 /* Safe because there are no large files, yet */
639 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
640 if (cmd == F_GETLK64)
642 else if (cmd == F_SETLK64)
644 else if (cmd == F_SETLKW64)
646 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
649 code = afs_lockctl(vcp, &flock, cmd, credp);
652 if ((code == 0 || flp->fl_type == F_UNLCK) &&
653 (cmd == F_SETLK || cmd == F_SETLKW)) {
654 flp->fl_flags &=~ FL_SLEEP;
655 code = flock_lock_file_wait(fp, flp);
656 if (code && flp->fl_type != F_UNLCK) {
657 struct AFS_FLOCK flock2;
659 flock2.l_type = F_UNLCK;
661 afs_lockctl(vcp, &flock2, F_SETLK, credp);
665 /* Convert flock back to Linux's file_lock */
666 flp->fl_type = flock.l_type;
667 flp->fl_pid = flock.l_pid;
670 return afs_convert_code(code);
675 * essentially the same as afs_fsync() but we need to get the return
676 * code for the sys_close() here, not afs_linux_release(), so call
677 * afs_StoreAllSegments() with AFS_LASTSTORE
680 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
681 afs_linux_flush(struct file *fp, fl_owner_t id)
683 afs_linux_flush(struct file *fp)
686 struct vrequest treq;
690 #if defined(AFS_CACHE_BYPASS)
696 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
704 vcp = VTOAFS(FILE_INODE(fp));
706 code = afs_InitReq(&treq, credp);
709 #if defined(AFS_CACHE_BYPASS)
710 /* If caching is bypassed for this file, or globally, just return 0 */
711 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
714 ObtainReadLock(&vcp->lock);
715 if(vcp->cachingStates & FCSBypass)
717 ReleaseReadLock(&vcp->lock);
720 /* future proof: don't rely on 0 return from afs_InitReq */
725 ObtainSharedLock(&vcp->lock, 535);
726 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
727 UpgradeSToWLock(&vcp->lock, 536);
728 if (!AFS_IS_DISCONNECTED) {
729 code = afs_StoreAllSegments(vcp,
731 AFS_SYNC | AFS_LASTSTORE);
733 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
735 ConvertWToSLock(&vcp->lock);
737 code = afs_CheckCode(code, &treq, 54);
738 ReleaseSharedLock(&vcp->lock);
745 return afs_convert_code(code);
748 #if !defined(AFS_LINUX24_ENV)
749 /* Not allowed to directly read a directory. */
751 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
759 struct file_operations afs_dir_fops = {
760 #if !defined(AFS_LINUX24_ENV)
761 .read = afs_linux_dir_read,
762 .lock = afs_linux_lock,
763 .fsync = afs_linux_fsync,
765 .read = generic_read_dir,
767 .readdir = afs_linux_readdir,
768 #ifdef HAVE_UNLOCKED_IOCTL
769 .unlocked_ioctl = afs_unlocked_xioctl,
773 #ifdef HAVE_COMPAT_IOCTL
774 .compat_ioctl = afs_unlocked_xioctl,
776 .open = afs_linux_open,
777 .release = afs_linux_release,
780 struct file_operations afs_file_fops = {
781 .read = afs_linux_read,
782 .write = afs_linux_write,
783 #ifdef GENERIC_FILE_AIO_READ
784 .aio_read = generic_file_aio_read,
785 .aio_write = generic_file_aio_write,
787 #ifdef HAVE_UNLOCKED_IOCTL
788 .unlocked_ioctl = afs_unlocked_xioctl,
792 #ifdef HAVE_COMPAT_IOCTL
793 .compat_ioctl = afs_unlocked_xioctl,
795 .mmap = afs_linux_mmap,
796 .open = afs_linux_open,
797 .flush = afs_linux_flush,
798 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
799 .sendfile = generic_file_sendfile,
801 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
802 .splice_write = generic_file_splice_write,
803 .splice_read = generic_file_splice_read,
805 .release = afs_linux_release,
806 .fsync = afs_linux_fsync,
807 .lock = afs_linux_lock,
808 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
809 .flock = afs_linux_flock,
814 /**********************************************************************
815 * AFS Linux dentry operations
816 **********************************************************************/
818 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
819 * that has its mvid (parent dir's fid) pointer set to the wrong directory
820 * due to being mounted in multiple points at once. If so, check_bad_parent()
821 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
822 * dotdotfid and mtpoint fid members.
824 * dp - dentry to be checked.
828 * This dentry's vcache's mvid will be set to the correct parent directory's
830 * This root vnode's volume will have its dotdotfid and mtpoint fids set
831 * to the correct parent and mountpoint fids.
835 check_bad_parent(struct dentry *dp)
838 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
839 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
841 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
844 /* force a lookup, so vcp->mvid is fixed up */
845 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
846 if (!avc || vcp != avc) { /* bad, very bad.. */
847 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
848 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
849 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
850 ICL_TYPE_POINTER, dp);
853 AFS_RELE(AFSTOV(avc));
860 /* afs_linux_revalidate
861 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
864 afs_linux_revalidate(struct dentry *dp)
867 struct vcache *vcp = VTOAFS(dp->d_inode);
871 if (afs_shuttingdown)
874 #ifdef AFS_LINUX24_ENV
880 /* Make this a fast path (no crref), since it's called so often. */
881 if (vcp->f.states & CStatd) {
883 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
884 check_bad_parent(dp); /* check and correct mvid */
887 #ifdef AFS_LINUX24_ENV
894 /* This avoids the crref when we don't have to do it. Watch for
895 * changes in afs_getattr that don't get replicated here!
897 if (vcp->f.states & CStatd &&
898 (!afs_fakestat_enable || vcp->mvstat != 1) &&
900 code = afs_CopyOutAttrs(vcp, &vattr);
903 code = afs_getattr(vcp, &vattr, credp);
907 afs_fill_inode(AFSTOV(vcp), &vattr);
910 #ifdef AFS_LINUX24_ENV
911 maybe_unlock_kernel();
914 return afs_convert_code(code);
917 #if defined(AFS_LINUX26_ENV)
919 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
921 int err = afs_linux_revalidate(dentry);
923 generic_fillattr(dentry->d_inode, stat);
929 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
930 * In kernels 2.2.10 and above, we are passed an additional flags var which
931 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
932 * we are advised to follow the entry if it is a link or to make sure that
933 * it is a directory. But since the kernel itself checks these possibilities
934 * later on, we shouldn't have to do it until later. Perhaps in the future..
937 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
938 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
939 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
941 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
944 afs_linux_dentry_revalidate(struct dentry *dp)
948 cred_t *credp = NULL;
949 struct vcache *vcp, *pvcp, *tvc = NULL;
951 struct afs_fakestat_state fakestate;
953 #ifdef AFS_LINUX24_ENV
957 afs_InitFakeStat(&fakestate);
961 vcp = VTOAFS(dp->d_inode);
962 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
964 if (vcp == afs_globalVp)
967 if (vcp->mvstat == 1) { /* mount point */
968 if (vcp->mvid && (vcp->f.states & CMValid)) {
971 struct vrequest treq;
974 code = afs_InitReq(&treq, credp);
976 #ifdef AFS_DARWIN_ENV
977 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
978 (strcmp(dp->d_name.name, "Contents") == 0) ||
980 (strcmp(dp->d_name.name, ".directory") == 0)) {
984 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
986 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
987 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
988 /* a mount point, not yet replaced by its directory */
993 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
994 check_bad_parent(dp); /* check and correct mvid */
997 /* If the last looker changes, we should make sure the current
998 * looker still has permission to examine this file. This would
999 * always require a crref() which would be "slow".
1001 if (vcp->last_looker != treq.uid) {
1002 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
1005 vcp->last_looker = treq.uid;
1009 /* If the parent's DataVersion has changed or the vnode
1010 * is longer valid, we need to do a full lookup. VerifyVCache
1011 * isn't enough since the vnode may have been renamed.
1014 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
1017 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1018 if (!tvc || tvc != vcp)
1021 if (afs_getattr(vcp, &vattr, credp))
1024 vattr2inode(AFSTOV(vcp), &vattr);
1025 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1028 /* should we always update the attributes at this point? */
1029 /* unlikely--the vcache entry hasn't changed */
1033 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
1034 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1038 /* No change in parent's DataVersion so this negative
1039 * lookup is still valid. BUT, if a server is down a
1040 * negative lookup can result so there should be a
1041 * liftime as well. For now, always expire.
1054 afs_PutFakeStat(&fakestate);
1060 shrink_dcache_parent(dp);
1063 #ifdef AFS_LINUX24_ENV
1064 maybe_unlock_kernel();
1069 if (have_submounts(dp))
1077 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1079 struct vcache *vcp = VTOAFS(ip);
1082 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1083 (void) afs_InactiveVCache(vcp, NULL);
1086 #ifdef DCACHE_NFSFS_RENAMED
1087 #ifdef AFS_LINUX26_ENV
1088 spin_lock(&dp->d_lock);
1090 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1091 #ifdef AFS_LINUX26_ENV
1092 spin_unlock(&dp->d_lock);
1100 afs_dentry_delete(struct dentry *dp)
1102 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1103 return 1; /* bad inode? */
1108 struct dentry_operations afs_dentry_operations = {
1109 .d_revalidate = afs_linux_dentry_revalidate,
1110 .d_delete = afs_dentry_delete,
1111 .d_iput = afs_dentry_iput,
1114 /**********************************************************************
1115 * AFS Linux inode operations
1116 **********************************************************************/
1120 * Merely need to set enough of vattr to get us through the create. Note
1121 * that the higher level code (open_namei) will take care of any tuncation
1122 * explicitly. Exclusive open is also taken care of in open_namei.
1124 * name is in kernel space at this point.
1127 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1128 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1129 struct nameidata *nd)
1131 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1135 cred_t *credp = crref();
1136 const char *name = dp->d_name.name;
1141 vattr.va_mode = mode;
1142 vattr.va_type = mode & S_IFMT;
1144 #if defined(AFS_LINUX26_ENV)
1145 maybe_lock_kernel();
1148 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1152 struct inode *ip = AFSTOV(vcp);
1154 afs_getattr(vcp, &vattr, credp);
1155 afs_fill_inode(ip, &vattr);
1156 insert_inode_hash(ip);
1157 dp->d_op = &afs_dentry_operations;
1158 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1159 d_instantiate(dp, ip);
1163 #if defined(AFS_LINUX26_ENV)
1164 maybe_unlock_kernel();
1167 return afs_convert_code(code);
1170 /* afs_linux_lookup */
1171 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1172 static struct dentry *
1173 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1174 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1175 struct nameidata *nd)
1177 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1181 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1184 cred_t *credp = crref();
1185 struct vcache *vcp = NULL;
1186 const char *comp = dp->d_name.name;
1187 struct inode *ip = NULL;
1188 #if defined(AFS_LINUX26_ENV)
1189 struct dentry *newdp = NULL;
1193 #if defined(AFS_LINUX26_ENV)
1194 maybe_lock_kernel();
1197 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1203 afs_getattr(vcp, &vattr, credp);
1204 afs_fill_inode(ip, &vattr);
1206 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1207 hlist_unhashed(&ip->i_hash)
1208 #elif defined(AFS_LINUX26_ENV)
1209 ip->i_hash.pprev == NULL
1211 ip->i_hash.prev == NULL
1214 insert_inode_hash(ip);
1216 dp->d_op = &afs_dentry_operations;
1217 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1220 #if defined(AFS_LINUX24_ENV)
1221 if (ip && S_ISDIR(ip->i_mode)) {
1222 struct dentry *alias;
1224 /* Try to invalidate an existing alias in favor of our new one */
1225 alias = d_find_alias(ip);
1226 #if defined(AFS_LINUX26_ENV)
1227 /* But not if it's disconnected; then we want d_splice_alias below */
1228 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1232 if (d_invalidate(alias) == 0) {
1236 #if defined(AFS_LINUX26_ENV)
1245 #if defined(AFS_LINUX26_ENV)
1246 newdp = d_splice_alias(ip, dp);
1251 #if defined(AFS_LINUX26_ENV)
1252 maybe_unlock_kernel();
1256 /* It's ok for the file to not be found. That's noted by the caller by
1257 * seeing that the dp->d_inode field is NULL.
1259 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1260 #if defined(AFS_LINUX26_ENV)
1261 if (!code || code == ENOENT)
1268 return ERR_PTR(afs_convert_code(code));
1272 return afs_convert_code(code);
1277 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1280 cred_t *credp = crref();
1281 const char *name = newdp->d_name.name;
1282 struct inode *oldip = olddp->d_inode;
1284 /* If afs_link returned the vnode, we could instantiate the
1285 * dentry. Since it's not, we drop this one and do a new lookup.
1290 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1294 return afs_convert_code(code);
1298 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1301 cred_t *credp = crref();
1302 const char *name = dp->d_name.name;
1303 struct vcache *tvc = VTOAFS(dp->d_inode);
1305 #if defined(AFS_LINUX26_ENV)
1306 maybe_lock_kernel();
1308 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1309 && !(tvc->f.states & CUnlinked)) {
1310 struct dentry *__dp;
1320 osi_FreeSmallSpace(__name);
1321 __name = afs_newname();
1324 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1328 } while (__dp->d_inode != NULL);
1331 code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1333 tvc->mvid = (void *) __name;
1336 crfree(tvc->uncred);
1338 tvc->uncred = credp;
1339 tvc->f.states |= CUnlinked;
1340 #ifdef DCACHE_NFSFS_RENAMED
1341 #ifdef AFS_LINUX26_ENV
1342 spin_lock(&dp->d_lock);
1344 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1345 #ifdef AFS_LINUX26_ENV
1346 spin_unlock(&dp->d_lock);
1350 osi_FreeSmallSpace(__name);
1355 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1364 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1369 #if defined(AFS_LINUX26_ENV)
1370 maybe_unlock_kernel();
1373 return afs_convert_code(code);
1378 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1381 cred_t *credp = crref();
1383 const char *name = dp->d_name.name;
1385 /* If afs_symlink returned the vnode, we could instantiate the
1386 * dentry. Since it's not, we drop this one and do a new lookup.
1392 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1395 return afs_convert_code(code);
1399 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1402 cred_t *credp = crref();
1403 struct vcache *tvcp = NULL;
1405 const char *name = dp->d_name.name;
1407 #if defined(AFS_LINUX26_ENV)
1408 maybe_lock_kernel();
1411 vattr.va_mask = ATTR_MODE;
1412 vattr.va_mode = mode;
1414 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1417 struct inode *ip = AFSTOV(tvcp);
1419 afs_getattr(tvcp, &vattr, credp);
1420 afs_fill_inode(ip, &vattr);
1422 dp->d_op = &afs_dentry_operations;
1423 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1424 d_instantiate(dp, ip);
1428 #if defined(AFS_LINUX26_ENV)
1429 maybe_unlock_kernel();
1432 return afs_convert_code(code);
1436 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1439 cred_t *credp = crref();
1440 const char *name = dp->d_name.name;
1442 /* locking kernel conflicts with glock? */
1445 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1448 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1449 * that failed because a directory is not empty. So, we map
1450 * EEXIST to ENOTEMPTY on linux.
1452 if (code == EEXIST) {
1461 return afs_convert_code(code);
1466 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1467 struct inode *newip, struct dentry *newdp)
1470 cred_t *credp = crref();
1471 const char *oldname = olddp->d_name.name;
1472 const char *newname = newdp->d_name.name;
1473 struct dentry *rehash = NULL;
1475 #if defined(AFS_LINUX26_ENV)
1476 /* Prevent any new references during rename operation. */
1477 maybe_lock_kernel();
1479 if (!d_unhashed(newdp)) {
1484 if (!list_empty(&newdp->d_hash)) {
1490 #if defined(AFS_LINUX24_ENV)
1491 if (atomic_read(&olddp->d_count) > 1)
1492 shrink_dcache_parent(olddp);
1496 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1500 olddp->d_time = 0; /* force to revalidate */
1505 #if defined(AFS_LINUX26_ENV)
1506 maybe_unlock_kernel();
1510 return afs_convert_code(code);
1514 /* afs_linux_ireadlink
1515 * Internal readlink which can return link contents to user or kernel space.
1516 * Note that the buffer is NOT supposed to be null-terminated.
1519 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1522 cred_t *credp = crref();
1526 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1527 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1531 return maxlen - tuio.uio_resid;
1533 return afs_convert_code(code);
1536 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1537 /* afs_linux_readlink
1538 * Fill target (which is in user space) with contents of symlink.
1541 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1544 struct inode *ip = dp->d_inode;
1547 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1553 /* afs_linux_follow_link
1554 * a file system dependent link following routine.
1556 #if defined(AFS_LINUX24_ENV)
1557 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1562 name = osi_Alloc(PATH_MAX);
1568 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1576 code = vfs_follow_link(nd, name);
1579 osi_Free(name, PATH_MAX);
1584 #else /* !defined(AFS_LINUX24_ENV) */
1586 static struct dentry *
1587 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1588 unsigned int follow)
1596 name = osi_Alloc(PATH_MAX + 1);
1600 return ERR_PTR(-EIO);
1603 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1608 if (code < -MAX_ERRNO)
1609 res = ERR_PTR(-EIO);
1611 res = ERR_PTR(code);
1614 res = lookup_dentry(name, basep, follow);
1618 osi_Free(name, PATH_MAX + 1);
1622 #endif /* AFS_LINUX24_ENV */
1623 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1625 #if defined(AFS_CACHE_BYPASS)
1627 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1630 afs_linux_can_bypass(struct inode *ip) {
1631 switch(cache_bypass_strategy) {
1632 case NEVER_BYPASS_CACHE:
1634 case ALWAYS_BYPASS_CACHE:
1636 case LARGE_FILES_BYPASS_CACHE:
1637 if(i_size_read(ip) > cache_bypass_threshold)
1645 afs_linux_cache_bypass_read(struct file *fp, struct address_space *mapping,
1646 struct list_head *page_list, unsigned num_pages)
1651 struct iovec* iovecp;
1652 struct nocache_read_request *ancr;
1653 struct page *pp, *ppt;
1654 struct pagevec lrupv;
1658 struct inode *ip = FILE_INODE(fp);
1659 struct vcache *avc = VTOAFS(ip);
1660 afs_int32 bypasscache = 0; /* bypass for this read */
1661 afs_int32 base_index = 0;
1662 afs_int32 page_count = 0;
1665 /* background thread must free: iovecp, auio, ancr */
1666 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1668 auio = osi_Alloc(sizeof(uio_t));
1669 auio->uio_iov = iovecp;
1670 auio->uio_iovcnt = num_pages;
1671 auio->uio_flag = UIO_READ;
1672 auio->uio_seg = AFS_UIOSYS;
1673 auio->uio_resid = num_pages * PAGE_SIZE;
1675 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1677 ancr->offset = auio->uio_offset;
1678 ancr->length = auio->uio_resid;
1680 pagevec_init(&lrupv, 0);
1682 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1684 if(list_empty(page_list))
1687 pp = list_entry(page_list->prev, struct page, lru);
1688 /* If we allocate a page and don't remove it from page_list,
1689 * the page cache gets upset. */
1691 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1692 if(pp->index > isize) {
1699 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1700 auio->uio_offset = offset;
1701 base_index = pp->index;
1703 iovecp[page_ix].iov_len = PAGE_SIZE;
1704 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1705 if(base_index != pp->index) {
1708 page_cache_release(pp);
1709 iovecp[page_ix].iov_base = (void *) 0;
1717 page_cache_release(pp);
1718 iovecp[page_ix].iov_base = (void *) 0;
1721 if(!PageLocked(pp)) {
1725 /* save the page for background map */
1726 iovecp[page_ix].iov_base = (void*) pp;
1728 /* and put it on the LRU cache */
1729 if (!pagevec_add(&lrupv, pp))
1730 __pagevec_lru_add(&lrupv);
1734 /* If there were useful pages in the page list, make sure all pages
1735 * are in the LRU cache, then schedule the read */
1737 pagevec_lru_add(&lrupv);
1739 code = afs_ReadNoCache(avc, ancr, credp);
1742 /* If there is nothing for the background thread to handle,
1743 * it won't be freeing the things that we never gave it */
1744 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1745 osi_Free(auio, sizeof(uio_t));
1746 osi_Free(ancr, sizeof(struct nocache_read_request));
1748 /* we do not flush, release, or unmap pages--that will be
1749 * done for us by the background thread as each page comes in
1750 * from the fileserver */
1752 return afs_convert_code(code);
1755 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1756 #endif /* defined(AFS_CACHE_BYPASS */
1759 afs_linux_read_cache(struct file *cachefp, struct page *page,
1760 int chunk, struct pagevec *lrupv,
1761 struct afs_pagecopy_task *task) {
1762 loff_t offset = page_offset(page);
1763 struct page *newpage, *cachepage;
1764 struct address_space *cachemapping;
1768 cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1772 /* From our offset, we now need to work out which page in the disk
1773 * file it corresponds to. This will be fun ... */
1774 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1776 while (cachepage == NULL) {
1777 cachepage = find_get_page(cachemapping, pageindex);
1780 newpage = page_cache_alloc_cold(cachemapping);
1786 code = add_to_page_cache(newpage, cachemapping,
1787 pageindex, GFP_KERNEL);
1789 cachepage = newpage;
1792 page_cache_get(cachepage);
1793 if (!pagevec_add(lrupv, cachepage))
1794 __pagevec_lru_add_file(lrupv);
1797 page_cache_release(newpage);
1799 if (code != -EEXIST)
1803 lock_page(cachepage);
1807 if (!PageUptodate(cachepage)) {
1808 ClearPageError(cachepage);
1809 code = cachemapping->a_ops->readpage(NULL, cachepage);
1810 if (!code && !task) {
1811 wait_on_page_locked(cachepage);
1814 unlock_page(cachepage);
1818 if (PageUptodate(cachepage)) {
1819 copy_highpage(page, cachepage);
1820 flush_dcache_page(page);
1821 SetPageUptodate(page);
1824 afs_pagecopy_queue_page(task, cachepage, page);
1836 page_cache_release(cachepage);
1842 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1844 loff_t offset = page_offset(pp);
1845 struct inode *ip = FILE_INODE(fp);
1846 struct vcache *avc = VTOAFS(ip);
1848 struct file *cacheFp = NULL;
1851 struct pagevec lrupv;
1853 /* Not a UFS cache, don't do anything */
1854 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1857 /* Can't do anything if the vcache isn't statd , or if the read
1858 * crosses a chunk boundary.
1860 if (!(avc->f.states & CStatd) ||
1861 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1865 ObtainWriteLock(&avc->lock, 911);
1867 /* XXX - See if hinting actually makes things faster !!! */
1869 /* See if we have a suitable entry already cached */
1873 /* We need to lock xdcache, then dcache, to handle situations where
1874 * the hint is on the free list. However, we can't safely do this
1875 * according to the locking hierarchy. So, use a non blocking lock.
1877 ObtainReadLock(&afs_xdcache);
1878 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1880 if (dcLocked && (tdc->index != NULLIDX)
1881 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1882 && tdc->f.chunk == AFS_CHUNK(offset)
1883 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1884 /* Bonus - the hint was correct */
1887 /* Only destroy the hint if its actually invalid, not if there's
1888 * just been a locking failure */
1890 ReleaseReadLock(&tdc->lock);
1897 ReleaseReadLock(&afs_xdcache);
1900 /* No hint, or hint is no longer valid - see if we can get something
1901 * directly from the dcache
1904 tdc = afs_FindDCache(avc, offset);
1907 ReleaseWriteLock(&avc->lock);
1912 ObtainReadLock(&tdc->lock);
1914 /* Is the dcache we've been given currently up to date */
1915 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1916 (tdc->dflags & DFFetching)) {
1917 ReleaseWriteLock(&avc->lock);
1918 ReleaseReadLock(&tdc->lock);
1923 /* Update our hint for future abuse */
1926 /* Okay, so we've now got a cache file that is up to date */
1928 /* XXX - I suspect we should be locking the inodes before we use them! */
1930 cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1931 pagevec_init(&lrupv, 0);
1933 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1935 if (pagevec_count(&lrupv))
1936 __pagevec_lru_add_file(&lrupv);
1938 filp_close(cacheFp, NULL);
1941 ReleaseReadLock(&tdc->lock);
1942 ReleaseWriteLock(&avc->lock);
1949 /* afs_linux_readpage
1950 * all reads come through here. A strategy-like read call.
1953 afs_linux_readpage(struct file *fp, struct page *pp)
1956 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1958 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1960 ulong address = afs_linux_page_address(pp);
1961 afs_offs_t offset = pageoff(pp);
1963 #if defined(AFS_CACHE_BYPASS)
1964 afs_int32 bypasscache = 0; /* bypass for this read */
1965 struct nocache_read_request *ancr;
1966 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1971 struct iovec *iovecp;
1972 struct inode *ip = FILE_INODE(fp);
1973 afs_int32 cnt = page_count(pp);
1974 struct vcache *avc = VTOAFS(ip);
1977 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1979 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1987 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1991 atomic_add(1, &pp->count);
1992 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1993 clear_bit(PG_error, &pp->flags);
1995 #if defined(AFS_CACHE_BYPASS)
1996 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1997 /* If the page is past the end of the file, skip it */
1998 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1999 if(pp->index > isize) {
2006 /* if bypasscache, receiver frees, else we do */
2007 auio = osi_Alloc(sizeof(uio_t));
2008 iovecp = osi_Alloc(sizeof(struct iovec));
2010 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2013 #if defined(AFS_CACHE_BYPASS)
2014 bypasscache = afs_linux_can_bypass(ip);
2016 /* In the new incarnation of selective caching, a file's caching policy
2017 * can change, eg because file size exceeds threshold, etc. */
2018 trydo_cache_transition(avc, credp, bypasscache);
2023 /* save the page for background map */
2024 auio->uio_iov->iov_base = (void*) pp;
2025 /* the background thread will free this */
2026 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2028 ancr->offset = offset;
2029 ancr->length = PAGE_SIZE;
2031 maybe_lock_kernel();
2032 code = afs_ReadNoCache(avc, ancr, credp);
2033 maybe_unlock_kernel();
2035 goto done; /* skips release page, doing it in bg thread */
2039 #ifdef AFS_LINUX24_ENV
2040 maybe_lock_kernel();
2044 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2045 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2046 99999); /* not a possible code value */
2048 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2050 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2051 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2053 AFS_DISCON_UNLOCK();
2055 #ifdef AFS_LINUX24_ENV
2056 maybe_unlock_kernel();
2059 /* XXX valid for no-cache also? Check last bits of files... :)
2060 * Cognate code goes in afs_NoCacheFetchProc. */
2061 if (auio->uio_resid) /* zero remainder of page */
2062 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2065 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2066 flush_dcache_page(pp);
2067 SetPageUptodate(pp);
2069 set_bit(PG_uptodate, &pp->flags);
2073 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
2077 clear_bit(PG_locked, &pp->flags);
2082 #if defined(AFS_CACHE_BYPASS)
2083 /* do not call afs_GetDCache if cache is bypassed */
2088 /* free if not bypassing cache */
2089 osi_Free(auio, sizeof(uio_t));
2090 osi_Free(iovecp, sizeof(struct iovec));
2092 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
2094 struct vrequest treq;
2097 code = afs_InitReq(&treq, credp);
2098 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2099 tdc = afs_FindDCache(avc, offset);
2101 if (!(tdc->mflags & DFNextStarted))
2102 afs_PrefetchChunk(avc, tdc, credp, &treq);
2105 ReleaseWriteLock(&avc->lock);
2110 #if defined(AFS_CACHE_BYPASS)
2114 return afs_convert_code(code);
2117 /* Readpages reads a number of pages for a particular file. We use
2118 * this to optimise the reading, by limiting the number of times upon which
2119 * we have to lookup, lock and open vcaches and dcaches
2123 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2124 struct list_head *page_list, unsigned int num_pages)
2126 struct inode *inode = mapping->host;
2127 struct vcache *avc = VTOAFS(inode);
2129 struct file *cacheFp = NULL;
2131 unsigned int page_idx;
2133 struct pagevec lrupv;
2134 struct afs_pagecopy_task *task;
2136 #if defined(AFS_CACHE_BYPASS)
2137 bypasscache = afs_linux_can_bypass(ip);
2139 /* In the new incarnation of selective caching, a file's caching policy
2140 * can change, eg because file size exceeds threshold, etc. */
2141 trydo_cache_transition(avc, credp, bypasscache);
2144 return afs_linux_cache_bypass_read(ip, mapping, page_list, num_pages);
2148 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2153 ObtainWriteLock(&avc->lock, 912);
2156 task = afs_pagecopy_init_task();
2159 pagevec_init(&lrupv, 0);
2160 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2161 struct page *page = list_entry(page_list->prev, struct page, lru);
2162 list_del(&page->lru);
2163 offset = page_offset(page);
2165 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2167 ReleaseReadLock(&tdc->lock);
2172 filp_close(cacheFp, NULL);
2177 if ((tdc = afs_FindDCache(avc, offset))) {
2178 ObtainReadLock(&tdc->lock);
2179 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2180 (tdc->dflags & DFFetching)) {
2181 ReleaseReadLock(&tdc->lock);
2188 cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
2191 if (tdc && !add_to_page_cache(page, mapping, page->index,
2193 page_cache_get(page);
2194 if (!pagevec_add(&lrupv, page))
2195 __pagevec_lru_add_file(&lrupv);
2197 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2199 page_cache_release(page);
2201 if (pagevec_count(&lrupv))
2202 __pagevec_lru_add_file(&lrupv);
2205 filp_close(cacheFp, NULL);
2207 afs_pagecopy_put_task(task);
2211 ReleaseReadLock(&tdc->lock);
2215 ReleaseWriteLock(&avc->lock);
2220 #if defined(AFS_LINUX24_ENV)
2222 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2223 unsigned long offset, unsigned int count)
2225 struct vcache *vcp = VTOAFS(ip);
2234 buffer = kmap(pp) + offset;
2235 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
2238 maybe_lock_kernel();
2240 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2241 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2242 ICL_TYPE_INT32, 99999);
2244 ObtainWriteLock(&vcp->lock, 532);
2245 if (vcp->f.states & CPageWrite) {
2246 ReleaseWriteLock(&vcp->lock);
2248 maybe_unlock_kernel();
2251 #ifdef AFS_LINUX26_ENV
2252 #if defined(WRITEPAGE_ACTIVATE)
2253 return WRITEPAGE_ACTIVATE;
2255 return AOP_WRITEPAGE_ACTIVATE;
2258 /* should mark it dirty? */
2262 vcp->f.states |= CPageWrite;
2263 ReleaseWriteLock(&vcp->lock);
2265 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2267 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2269 i_size_write(ip, vcp->f.m.Length);
2270 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2272 ObtainWriteLock(&vcp->lock, 533);
2274 struct vrequest treq;
2276 if (!afs_InitReq(&treq, credp))
2277 code = afs_DoPartialWrite(vcp, &treq);
2279 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2281 vcp->f.states &= ~CPageWrite;
2282 ReleaseWriteLock(&vcp->lock);
2284 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2285 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2286 ICL_TYPE_INT32, code);
2289 maybe_unlock_kernel();
2298 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2299 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2301 afs_linux_writepage(struct page *pp)
2304 struct address_space *mapping = pp->mapping;
2305 struct inode *inode;
2306 unsigned long end_index;
2307 unsigned offset = PAGE_CACHE_SIZE;
2310 #if defined(AFS_LINUX26_ENV)
2311 if (PageReclaim(pp)) {
2312 # if defined(WRITEPAGE_ACTIVATE)
2313 return WRITEPAGE_ACTIVATE;
2315 return AOP_WRITEPAGE_ACTIVATE;
2319 if (PageLaunder(pp)) {
2320 return(fail_writepage(pp));
2324 inode = (struct inode *)mapping->host;
2325 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
2328 if (pp->index < end_index)
2330 /* things got complicated... */
2331 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
2332 /* OK, are we completely out? */
2333 if (pp->index >= end_index + 1 || !offset)
2336 status = afs_linux_writepage_sync(inode, pp, 0, offset);
2337 SetPageUptodate(pp);
2338 #if defined(AFS_LINUX26_ENV)
2339 #if defined(WRITEPAGE_ACTIVATE)
2340 if ( status != WRITEPAGE_ACTIVATE )
2342 if ( status != AOP_WRITEPAGE_ACTIVATE )
2346 if (status == offset)
2352 /* afs_linux_updatepage
2353 * What one would have thought was writepage - write dirty page to file.
2354 * Called from generic_file_write. buffer is still in user space. pagep
2355 * has been filled in with old data if we're updating less than a page.
2358 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2359 unsigned int count, int sync)
2361 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2362 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2368 set_bit(PG_locked, &pp->flags);
2373 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2374 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2375 ICL_TYPE_INT32, 99999);
2376 setup_uio(&tuio, &iovec, page_addr + offset,
2377 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2380 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2382 i_size_write(ip, vcp->f.m.Length);
2383 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2386 struct vrequest treq;
2388 ObtainWriteLock(&vcp->lock, 533);
2389 vcp->f.m.Date = osi_Time(); /* set modification time */
2390 if (!afs_InitReq(&treq, credp))
2391 code = afs_DoPartialWrite(vcp, &treq);
2392 ReleaseWriteLock(&vcp->lock);
2395 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2396 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2397 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2398 ICL_TYPE_INT32, code);
2400 AFS_DISCON_UNLOCK();
2404 clear_bit(PG_locked, &pp->flags);
2409 /* afs_linux_permission
2410 * Check access rights - returns error if can't check or permission denied.
2413 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2414 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2416 afs_linux_permission(struct inode *ip, int mode)
2420 cred_t *credp = crref();
2424 if (mode & MAY_EXEC)
2426 if (mode & MAY_READ)
2428 if (mode & MAY_WRITE)
2430 code = afs_access(VTOAFS(ip), tmp, credp);
2434 return afs_convert_code(code);
2437 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2439 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2444 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2445 offset, to - offset);
2446 #if !defined(AFS_LINUX26_ENV)
2454 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2457 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2458 call kmap directly instead of relying on us to do it */
2459 #if !defined(AFS_LINUX26_ENV)
2466 #if defined(HAVE_WRITE_BEGIN)
2468 afs_linux_write_end(struct file *file, struct address_space *mapping,
2469 loff_t pos, unsigned len, unsigned copied,
2470 struct page *page, void *fsdata)
2473 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2475 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2478 page_cache_release(page);
2483 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2484 loff_t pos, unsigned len, unsigned flags,
2485 struct page **pagep, void **fsdata)
2488 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2489 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2490 page = grab_cache_page_write_begin(mapping, index, flags);
2492 page = __grab_cache_page(mapping, index);
2501 static struct inode_operations afs_file_iops = {
2502 #if defined(AFS_LINUX26_ENV)
2503 .permission = afs_linux_permission,
2504 .getattr = afs_linux_getattr,
2505 .setattr = afs_notify_change,
2506 #elif defined(AFS_LINUX24_ENV)
2507 .permission = afs_linux_permission,
2508 .revalidate = afs_linux_revalidate,
2509 .setattr = afs_notify_change,
2511 .default_file_ops = &afs_file_fops,
2512 .readpage = afs_linux_readpage,
2513 .revalidate = afs_linux_revalidate,
2514 .updatepage = afs_linux_updatepage,
2518 #if defined(AFS_LINUX24_ENV)
2519 static struct address_space_operations afs_file_aops = {
2520 .readpage = afs_linux_readpage,
2521 .readpages = afs_linux_readpages,
2522 .writepage = afs_linux_writepage,
2523 #if defined (HAVE_WRITE_BEGIN)
2524 .write_begin = afs_linux_write_begin,
2525 .write_end = afs_linux_write_end,
2527 .commit_write = afs_linux_commit_write,
2528 .prepare_write = afs_linux_prepare_write,
2534 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2535 * by what sort of operation is allowed.....
2538 static struct inode_operations afs_dir_iops = {
2539 #if !defined(AFS_LINUX24_ENV)
2540 .default_file_ops = &afs_dir_fops,
2542 .setattr = afs_notify_change,
2544 .create = afs_linux_create,
2545 .lookup = afs_linux_lookup,
2546 .link = afs_linux_link,
2547 .unlink = afs_linux_unlink,
2548 .symlink = afs_linux_symlink,
2549 .mkdir = afs_linux_mkdir,
2550 .rmdir = afs_linux_rmdir,
2551 .rename = afs_linux_rename,
2552 #if defined(AFS_LINUX26_ENV)
2553 .getattr = afs_linux_getattr,
2555 .revalidate = afs_linux_revalidate,
2557 .permission = afs_linux_permission,
2560 /* We really need a separate symlink set of ops, since do_follow_link()
2561 * determines if it _is_ a link by checking if the follow_link op is set.
2563 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2565 afs_symlink_filler(struct file *file, struct page *page)
2567 struct inode *ip = (struct inode *)page->mapping->host;
2568 char *p = (char *)kmap(page);
2571 maybe_lock_kernel();
2573 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2578 p[code] = '\0'; /* null terminate? */
2579 maybe_unlock_kernel();
2581 SetPageUptodate(page);
2587 maybe_unlock_kernel();
2595 static struct address_space_operations afs_symlink_aops = {
2596 .readpage = afs_symlink_filler
2598 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2600 static struct inode_operations afs_symlink_iops = {
2601 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2602 .readlink = page_readlink,
2603 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2604 .follow_link = page_follow_link,
2606 .follow_link = page_follow_link_light,
2607 .put_link = page_put_link,
2609 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2610 .readlink = afs_linux_readlink,
2611 .follow_link = afs_linux_follow_link,
2612 #if !defined(AFS_LINUX24_ENV)
2613 .permission = afs_linux_permission,
2614 .revalidate = afs_linux_revalidate,
2616 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2617 #if defined(AFS_LINUX24_ENV)
2618 .setattr = afs_notify_change,
2623 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2627 vattr2inode(ip, vattr);
2629 #if defined(AFS_LINUX26_ENV)
2630 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2632 /* Reset ops if symlink or directory. */
2633 if (S_ISREG(ip->i_mode)) {
2634 ip->i_op = &afs_file_iops;
2635 #if defined(AFS_LINUX24_ENV)
2636 ip->i_fop = &afs_file_fops;
2637 ip->i_data.a_ops = &afs_file_aops;
2640 } else if (S_ISDIR(ip->i_mode)) {
2641 ip->i_op = &afs_dir_iops;
2642 #if defined(AFS_LINUX24_ENV)
2643 ip->i_fop = &afs_dir_fops;
2646 } else if (S_ISLNK(ip->i_mode)) {
2647 ip->i_op = &afs_symlink_iops;
2648 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2649 ip->i_data.a_ops = &afs_symlink_aops;
2650 ip->i_mapping = &ip->i_data;