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))
98 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
101 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
102 cred_t *credp = crref();
103 struct vrequest treq;
104 afs_size_t isize, offindex;
106 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
107 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
109 /* get a validated vcache entry */
110 code = afs_InitReq(&treq, credp);
112 code = afs_VerifyVCache(vcp, &treq);
115 code = afs_convert_code(code);
117 #if defined(AFS_CACHE_BYPASS)
118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
119 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
120 offindex = *offp >> PAGE_CACHE_SHIFT;
121 if(offindex > isize) {
127 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
130 code = do_sync_read(fp, buf, count, offp);
132 code = generic_file_read(fp, buf, count, offp);
137 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
138 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
147 /* Now we have integrated VM for writes as well as reads. generic_file_write
148 * also takes care of re-positioning the pointer if file is open in append
149 * mode. Call fake open/close to ensure we do writes of core dumps.
152 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
155 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
156 struct vrequest treq;
157 cred_t *credp = crref();
161 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
162 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
163 (fp->f_flags & O_APPEND) ? 99998 : 99999);
166 /* get a validated vcache entry */
167 code = (ssize_t) afs_InitReq(&treq, credp);
169 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
171 ObtainWriteLock(&vcp->lock, 529);
173 ReleaseWriteLock(&vcp->lock);
175 code = afs_convert_code(code);
179 code = do_sync_write(fp, buf, count, offp);
181 code = generic_file_write(fp, buf, count, offp);
186 ObtainWriteLock(&vcp->lock, 530);
187 afs_FakeClose(vcp, credp);
188 ReleaseWriteLock(&vcp->lock);
190 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
191 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
199 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
201 /* This is a complete rewrite of afs_readdir, since we can make use of
202 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
203 * handling and use of bulkstats will need to be reflected here as well.
206 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
208 struct vcache *avc = VTOAFS(FILE_INODE(fp));
209 struct vrequest treq;
210 register struct dcache *tdc;
217 afs_size_t origOffset, tlen;
218 cred_t *credp = crref();
219 struct afs_fakestat_state fakestat;
221 #if defined(AFS_LINUX26_ENV)
225 AFS_STATCNT(afs_readdir);
227 code = afs_InitReq(&treq, credp);
230 code = afs_convert_code(code);
234 afs_InitFakeStat(&fakestat);
235 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
237 code = afs_convert_code(code);
241 /* update the cache entry */
243 code = afs_VerifyVCache(avc, &treq);
245 code = afs_convert_code(code);
249 /* get a reference to the entire directory */
250 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
256 ObtainSharedLock(&avc->lock, 810);
257 UpgradeSToWLock(&avc->lock, 811);
258 ObtainReadLock(&tdc->lock);
260 * Make sure that the data in the cache is current. There are two
261 * cases we need to worry about:
262 * 1. The cache data is being fetched by another process.
263 * 2. The cache data is no longer valid
265 while ((avc->f.states & CStatd)
266 && (tdc->dflags & DFFetching)
267 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
268 ReleaseReadLock(&tdc->lock);
269 ReleaseSharedLock(&avc->lock);
270 afs_osi_Sleep(&tdc->validPos);
271 ObtainSharedLock(&avc->lock, 812);
272 ObtainReadLock(&tdc->lock);
274 if (!(avc->f.states & CStatd)
275 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
276 ReleaseReadLock(&tdc->lock);
277 ReleaseSharedLock(&avc->lock);
282 /* Set the readdir-in-progress flag, and downgrade the lock
283 * to shared so others will be able to acquire a read lock.
285 avc->f.states |= CReadDir;
286 avc->dcreaddir = tdc;
287 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
288 ConvertWToSLock(&avc->lock);
290 /* Fill in until we get an error or we're done. This implementation
291 * takes an offset in units of blobs, rather than bytes.
294 offset = (int) fp->f_pos;
296 dirpos = BlobScan(tdc, offset);
300 de = afs_dir_GetBlob(tdc, dirpos);
304 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
307 len = strlen(de->name);
309 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
310 (unsigned long)&tdc->f.inode, dirpos);
311 DRelease((struct buffer *) de, 0);
312 ReleaseSharedLock(&avc->lock);
318 /* filldir returns -EINVAL when the buffer is full. */
319 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
321 unsigned int type = DT_UNKNOWN;
322 struct VenusFid afid;
325 afid.Cell = avc->f.fid.Cell;
326 afid.Fid.Volume = avc->f.fid.Fid.Volume;
327 afid.Fid.Vnode = ntohl(de->fid.vnode);
328 afid.Fid.Unique = ntohl(de->fid.vunique);
329 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
331 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
334 } else if (((tvc->f.states) & (CStatd | CTruth))) {
335 /* CTruth will be set if the object has
340 else if (vtype == VREG)
342 /* Don't do this until we're sure it can't be a mtpt */
343 /* else if (vtype == VLNK)
345 /* what other types does AFS support? */
347 /* clean up from afs_FindVCache */
351 * If this is NFS readdirplus, then the filler is going to
352 * call getattr on this inode, which will deadlock if we're
356 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
360 code = (*filldir) (dirbuf, de->name, len, offset, ino);
362 DRelease((struct buffer *)de, 0);
365 offset = dirpos + 1 + ((len + 16) >> 5);
367 /* If filldir didn't fill in the last one this is still pointing to that
370 fp->f_pos = (loff_t) offset;
372 ReleaseReadLock(&tdc->lock);
374 UpgradeSToWLock(&avc->lock, 813);
375 avc->f.states &= ~CReadDir;
377 avc->readdir_pid = 0;
378 ReleaseSharedLock(&avc->lock);
382 afs_PutFakeStat(&fakestat);
385 #if defined(AFS_LINUX26_ENV)
386 maybe_unlock_kernel();
392 /* in afs_pioctl.c */
393 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
396 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
397 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
399 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
406 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
408 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
409 cred_t *credp = crref();
410 struct vrequest treq;
414 #if defined(AFS_LINUX24_ENV)
415 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
416 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
417 vmap->vm_end - vmap->vm_start);
419 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
420 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
421 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
425 /* get a validated vcache entry */
426 code = afs_InitReq(&treq, credp);
430 code = afs_VerifyVCache(vcp, &treq);
434 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
437 code = generic_file_mmap(fp, vmap);
440 vcp->f.states |= CMAPPED;
448 code = afs_convert_code(code);
453 afs_linux_open(struct inode *ip, struct file *fp)
455 struct vcache *vcp = VTOAFS(ip);
456 cred_t *credp = crref();
459 #ifdef AFS_LINUX24_ENV
463 code = afs_open(&vcp, fp->f_flags, credp);
465 #ifdef AFS_LINUX24_ENV
466 maybe_unlock_kernel();
470 return afs_convert_code(code);
474 afs_linux_release(struct inode *ip, struct file *fp)
476 struct vcache *vcp = VTOAFS(ip);
477 cred_t *credp = crref();
480 #ifdef AFS_LINUX24_ENV
484 code = afs_close(vcp, fp->f_flags, credp);
486 #ifdef AFS_LINUX24_ENV
487 maybe_unlock_kernel();
491 return afs_convert_code(code);
495 #if defined(AFS_LINUX24_ENV)
496 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
498 afs_linux_fsync(struct file *fp, struct dentry *dp)
502 struct inode *ip = FILE_INODE(fp);
503 cred_t *credp = crref();
505 #ifdef AFS_LINUX24_ENV
509 code = afs_fsync(VTOAFS(ip), credp);
511 #ifdef AFS_LINUX24_ENV
512 maybe_unlock_kernel();
515 return afs_convert_code(code);
521 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
524 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
525 cred_t *credp = crref();
526 struct AFS_FLOCK flock;
527 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
528 struct file_lock conflict;
529 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
530 struct file_lock *conflict;
533 /* Convert to a lock format afs_lockctl understands. */
534 memset((char *)&flock, 0, sizeof(flock));
535 flock.l_type = flp->fl_type;
536 flock.l_pid = flp->fl_pid;
538 flock.l_start = flp->fl_start;
539 flock.l_len = flp->fl_end - flp->fl_start + 1;
541 /* Safe because there are no large files, yet */
542 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
543 if (cmd == F_GETLK64)
545 else if (cmd == F_SETLK64)
547 else if (cmd == F_SETLKW64)
549 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
552 code = afs_lockctl(vcp, &flock, cmd, credp);
555 #ifdef AFS_LINUX24_ENV
556 if ((code == 0 || flp->fl_type == F_UNLCK) &&
557 (cmd == F_SETLK || cmd == F_SETLKW)) {
558 # ifdef POSIX_LOCK_FILE_WAIT_ARG
559 code = posix_lock_file(fp, flp, 0);
561 flp->fl_flags &=~ FL_SLEEP;
562 code = posix_lock_file(fp, flp);
564 if (code && flp->fl_type != F_UNLCK) {
565 struct AFS_FLOCK flock2;
567 flock2.l_type = F_UNLCK;
569 afs_lockctl(vcp, &flock2, F_SETLK, credp);
573 /* If lockctl says there are no conflicting locks, then also check with the
574 * kernel, as lockctl knows nothing about byte range locks
576 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
577 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
578 if (posix_test_lock(fp, flp, &conflict)) {
579 locks_copy_lock(flp, &conflict);
580 flp->fl_type = F_UNLCK;
584 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
585 if ((conflict = posix_test_lock(fp, flp))) {
586 locks_copy_lock(flp, conflict);
587 flp->fl_type = F_UNLCK;
592 posix_test_lock(fp, flp);
593 /* If we found a lock in the kernel's structure, return it */
594 if (flp->fl_type != F_UNLCK) {
602 /* Convert flock back to Linux's file_lock */
603 flp->fl_type = flock.l_type;
604 flp->fl_pid = flock.l_pid;
605 flp->fl_start = flock.l_start;
606 flp->fl_end = flock.l_start + flock.l_len - 1;
609 return afs_convert_code(code);
612 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
614 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
616 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
617 cred_t *credp = crref();
618 struct AFS_FLOCK flock;
619 /* Convert to a lock format afs_lockctl understands. */
620 memset((char *)&flock, 0, sizeof(flock));
621 flock.l_type = flp->fl_type;
622 flock.l_pid = flp->fl_pid;
625 flock.l_len = OFFSET_MAX;
627 /* Safe because there are no large files, yet */
628 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
629 if (cmd == F_GETLK64)
631 else if (cmd == F_SETLK64)
633 else if (cmd == F_SETLKW64)
635 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
638 code = afs_lockctl(vcp, &flock, cmd, credp);
641 if ((code == 0 || flp->fl_type == F_UNLCK) &&
642 (cmd == F_SETLK || cmd == F_SETLKW)) {
643 flp->fl_flags &=~ FL_SLEEP;
644 code = flock_lock_file_wait(fp, flp);
645 if (code && flp->fl_type != F_UNLCK) {
646 struct AFS_FLOCK flock2;
648 flock2.l_type = F_UNLCK;
650 afs_lockctl(vcp, &flock2, F_SETLK, credp);
654 /* Convert flock back to Linux's file_lock */
655 flp->fl_type = flock.l_type;
656 flp->fl_pid = flock.l_pid;
659 return afs_convert_code(code);
664 * essentially the same as afs_fsync() but we need to get the return
665 * code for the sys_close() here, not afs_linux_release(), so call
666 * afs_StoreAllSegments() with AFS_LASTSTORE
669 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
670 afs_linux_flush(struct file *fp, fl_owner_t id)
672 afs_linux_flush(struct file *fp)
675 struct vrequest treq;
679 #if defined(AFS_CACHE_BYPASS)
685 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
693 vcp = VTOAFS(FILE_INODE(fp));
695 code = afs_InitReq(&treq, credp);
698 #if defined(AFS_CACHE_BYPASS)
699 /* If caching is bypassed for this file, or globally, just return 0 */
700 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
703 ObtainReadLock(&vcp->lock);
704 if(vcp->cachingStates & FCSBypass)
706 ReleaseReadLock(&vcp->lock);
709 /* future proof: don't rely on 0 return from afs_InitReq */
714 ObtainSharedLock(&vcp->lock, 535);
715 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
716 UpgradeSToWLock(&vcp->lock, 536);
717 if (!AFS_IS_DISCONNECTED) {
718 code = afs_StoreAllSegments(vcp,
720 AFS_SYNC | AFS_LASTSTORE);
722 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
724 ConvertWToSLock(&vcp->lock);
726 code = afs_CheckCode(code, &treq, 54);
727 ReleaseSharedLock(&vcp->lock);
734 return afs_convert_code(code);
737 #if !defined(AFS_LINUX24_ENV)
738 /* Not allowed to directly read a directory. */
740 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
748 struct file_operations afs_dir_fops = {
749 #if !defined(AFS_LINUX24_ENV)
750 .read = afs_linux_dir_read,
751 .lock = afs_linux_lock,
752 .fsync = afs_linux_fsync,
754 .read = generic_read_dir,
756 .readdir = afs_linux_readdir,
757 #ifdef HAVE_UNLOCKED_IOCTL
758 .unlocked_ioctl = afs_unlocked_xioctl,
762 #ifdef HAVE_COMPAT_IOCTL
763 .compat_ioctl = afs_unlocked_xioctl,
765 .open = afs_linux_open,
766 .release = afs_linux_release,
769 struct file_operations afs_file_fops = {
770 .read = afs_linux_read,
771 .write = afs_linux_write,
772 #ifdef GENERIC_FILE_AIO_READ
773 .aio_read = generic_file_aio_read,
774 .aio_write = generic_file_aio_write,
776 #ifdef HAVE_UNLOCKED_IOCTL
777 .unlocked_ioctl = afs_unlocked_xioctl,
781 #ifdef HAVE_COMPAT_IOCTL
782 .compat_ioctl = afs_unlocked_xioctl,
784 .mmap = afs_linux_mmap,
785 .open = afs_linux_open,
786 .flush = afs_linux_flush,
787 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
788 .sendfile = generic_file_sendfile,
790 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
791 .splice_write = generic_file_splice_write,
792 .splice_read = generic_file_splice_read,
794 .release = afs_linux_release,
795 .fsync = afs_linux_fsync,
796 .lock = afs_linux_lock,
797 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
798 .flock = afs_linux_flock,
803 /**********************************************************************
804 * AFS Linux dentry operations
805 **********************************************************************/
807 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
808 * that has its mvid (parent dir's fid) pointer set to the wrong directory
809 * due to being mounted in multiple points at once. If so, check_bad_parent()
810 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
811 * dotdotfid and mtpoint fid members.
813 * dp - dentry to be checked.
817 * This dentry's vcache's mvid will be set to the correct parent directory's
819 * This root vnode's volume will have its dotdotfid and mtpoint fids set
820 * to the correct parent and mountpoint fids.
824 check_bad_parent(struct dentry *dp)
827 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
828 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
830 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
833 /* force a lookup, so vcp->mvid is fixed up */
834 afs_lookup(pvc, dp->d_name.name, &avc, credp);
835 if (!avc || vcp != avc) { /* bad, very bad.. */
836 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
837 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
838 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
839 ICL_TYPE_POINTER, dp);
842 AFS_RELE(AFSTOV(avc));
849 /* afs_linux_revalidate
850 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
853 afs_linux_revalidate(struct dentry *dp)
856 struct vcache *vcp = VTOAFS(dp->d_inode);
860 #ifdef AFS_LINUX24_ENV
866 /* Make this a fast path (no crref), since it's called so often. */
867 if (vcp->f.states & CStatd) {
869 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
870 check_bad_parent(dp); /* check and correct mvid */
873 #ifdef AFS_LINUX24_ENV
881 code = afs_getattr(vcp, &vattr, credp);
883 afs_fill_inode(AFSTOV(vcp), &vattr);
886 #ifdef AFS_LINUX24_ENV
887 maybe_unlock_kernel();
891 return afs_convert_code(code);
894 #if defined(AFS_LINUX26_ENV)
896 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
898 int err = afs_linux_revalidate(dentry);
900 generic_fillattr(dentry->d_inode, stat);
906 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
907 * In kernels 2.2.10 and above, we are passed an additional flags var which
908 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
909 * we are advised to follow the entry if it is a link or to make sure that
910 * it is a directory. But since the kernel itself checks these possibilities
911 * later on, we shouldn't have to do it until later. Perhaps in the future..
914 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
915 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
916 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
918 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
921 afs_linux_dentry_revalidate(struct dentry *dp)
925 cred_t *credp = NULL;
926 struct vcache *vcp, *pvcp, *tvc = NULL;
928 struct afs_fakestat_state fakestate;
930 #ifdef AFS_LINUX24_ENV
934 afs_InitFakeStat(&fakestate);
938 vcp = VTOAFS(dp->d_inode);
939 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
941 if (vcp == afs_globalVp)
944 if (vcp->mvstat == 1) { /* mount point */
945 if (vcp->mvid && (vcp->f.states & CMValid)) {
948 struct vrequest treq;
951 code = afs_InitReq(&treq, credp);
953 #ifdef AFS_DARWIN_ENV
954 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
955 (strcmp(dp->d_name.name, "Contents") == 0) ||
957 (strcmp(dp->d_name.name, ".directory") == 0)) {
961 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
963 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
964 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
965 /* a mount point, not yet replaced by its directory */
970 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
971 check_bad_parent(dp); /* check and correct mvid */
974 /* If the last looker changes, we should make sure the current
975 * looker still has permission to examine this file. This would
976 * always require a crref() which would be "slow".
978 if (vcp->last_looker != treq.uid) {
979 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
982 vcp->last_looker = treq.uid;
986 /* If the parent's DataVersion has changed or the vnode
987 * is longer valid, we need to do a full lookup. VerifyVCache
988 * isn't enough since the vnode may have been renamed.
991 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
994 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
995 if (!tvc || tvc != vcp)
998 if (afs_getattr(vcp, &vattr, credp))
1001 vattr2inode(AFSTOV(vcp), &vattr);
1002 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1005 /* should we always update the attributes at this point? */
1006 /* unlikely--the vcache entry hasn't changed */
1010 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
1011 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1015 /* No change in parent's DataVersion so this negative
1016 * lookup is still valid. BUT, if a server is down a
1017 * negative lookup can result so there should be a
1018 * liftime as well. For now, always expire.
1031 afs_PutFakeStat(&fakestate);
1037 shrink_dcache_parent(dp);
1040 #ifdef AFS_LINUX24_ENV
1041 maybe_unlock_kernel();
1046 if (have_submounts(dp))
1054 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1056 struct vcache *vcp = VTOAFS(ip);
1059 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1060 (void) afs_InactiveVCache(vcp, NULL);
1063 #ifdef DCACHE_NFSFS_RENAMED
1064 #ifdef AFS_LINUX26_ENV
1065 spin_lock(&dp->d_lock);
1067 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1068 #ifdef AFS_LINUX26_ENV
1069 spin_unlock(&dp->d_lock);
1077 afs_dentry_delete(struct dentry *dp)
1079 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1080 return 1; /* bad inode? */
1085 struct dentry_operations afs_dentry_operations = {
1086 .d_revalidate = afs_linux_dentry_revalidate,
1087 .d_delete = afs_dentry_delete,
1088 .d_iput = afs_dentry_iput,
1091 /**********************************************************************
1092 * AFS Linux inode operations
1093 **********************************************************************/
1097 * Merely need to set enough of vattr to get us through the create. Note
1098 * that the higher level code (open_namei) will take care of any tuncation
1099 * explicitly. Exclusive open is also taken care of in open_namei.
1101 * name is in kernel space at this point.
1104 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1105 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1106 struct nameidata *nd)
1108 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1112 cred_t *credp = crref();
1113 const char *name = dp->d_name.name;
1118 vattr.va_mode = mode;
1119 vattr.va_type = mode & S_IFMT;
1121 #if defined(AFS_LINUX26_ENV)
1122 maybe_lock_kernel();
1125 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1129 struct inode *ip = AFSTOV(vcp);
1131 afs_getattr(vcp, &vattr, credp);
1132 afs_fill_inode(ip, &vattr);
1133 insert_inode_hash(ip);
1134 dp->d_op = &afs_dentry_operations;
1135 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1136 d_instantiate(dp, ip);
1140 #if defined(AFS_LINUX26_ENV)
1141 maybe_unlock_kernel();
1144 return afs_convert_code(code);
1147 /* afs_linux_lookup */
1148 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1149 static struct dentry *
1150 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1151 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1152 struct nameidata *nd)
1154 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1158 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1161 cred_t *credp = crref();
1162 struct vcache *vcp = NULL;
1163 const char *comp = dp->d_name.name;
1164 struct inode *ip = NULL;
1165 #if defined(AFS_LINUX26_ENV)
1166 struct dentry *newdp = NULL;
1170 #if defined(AFS_LINUX26_ENV)
1171 maybe_lock_kernel();
1174 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1180 afs_getattr(vcp, &vattr, credp);
1181 afs_fill_inode(ip, &vattr);
1183 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1184 hlist_unhashed(&ip->i_hash)
1185 #elif defined(AFS_LINUX26_ENV)
1186 ip->i_hash.pprev == NULL
1188 ip->i_hash.prev == NULL
1191 insert_inode_hash(ip);
1193 dp->d_op = &afs_dentry_operations;
1194 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1197 #if defined(AFS_LINUX24_ENV)
1198 if (ip && S_ISDIR(ip->i_mode)) {
1199 struct dentry *alias;
1201 /* Try to invalidate an existing alias in favor of our new one */
1202 alias = d_find_alias(ip);
1203 #if defined(AFS_LINUX26_ENV)
1204 /* But not if it's disconnected; then we want d_splice_alias below */
1205 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1209 if (d_invalidate(alias) == 0) {
1213 #if defined(AFS_LINUX26_ENV)
1222 #if defined(AFS_LINUX26_ENV)
1223 newdp = d_splice_alias(ip, dp);
1228 #if defined(AFS_LINUX26_ENV)
1229 maybe_unlock_kernel();
1233 /* It's ok for the file to not be found. That's noted by the caller by
1234 * seeing that the dp->d_inode field is NULL.
1236 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1237 #if defined(AFS_LINUX26_ENV)
1238 if (!code || code == ENOENT)
1245 return ERR_PTR(afs_convert_code(code));
1249 return afs_convert_code(code);
1254 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1257 cred_t *credp = crref();
1258 const char *name = newdp->d_name.name;
1259 struct inode *oldip = olddp->d_inode;
1261 /* If afs_link returned the vnode, we could instantiate the
1262 * dentry. Since it's not, we drop this one and do a new lookup.
1267 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1271 return afs_convert_code(code);
1275 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1278 cred_t *credp = crref();
1279 const char *name = dp->d_name.name;
1280 struct vcache *tvc = VTOAFS(dp->d_inode);
1282 #if defined(AFS_LINUX26_ENV)
1283 maybe_lock_kernel();
1285 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1286 && !(tvc->f.states & CUnlinked)) {
1287 struct dentry *__dp;
1297 osi_FreeSmallSpace(__name);
1298 __name = afs_newname();
1301 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1305 } while (__dp->d_inode != NULL);
1308 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1310 tvc->mvid = (void *) __name;
1313 crfree(tvc->uncred);
1315 tvc->uncred = credp;
1316 tvc->f.states |= CUnlinked;
1317 #ifdef DCACHE_NFSFS_RENAMED
1318 #ifdef AFS_LINUX26_ENV
1319 spin_lock(&dp->d_lock);
1321 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1322 #ifdef AFS_LINUX26_ENV
1323 spin_unlock(&dp->d_lock);
1327 osi_FreeSmallSpace(__name);
1332 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1341 code = afs_remove(VTOAFS(dip), name, credp);
1346 #if defined(AFS_LINUX26_ENV)
1347 maybe_unlock_kernel();
1350 return afs_convert_code(code);
1355 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1358 cred_t *credp = crref();
1360 const char *name = dp->d_name.name;
1362 /* If afs_symlink returned the vnode, we could instantiate the
1363 * dentry. Since it's not, we drop this one and do a new lookup.
1369 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1372 return afs_convert_code(code);
1376 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1379 cred_t *credp = crref();
1380 struct vcache *tvcp = NULL;
1382 const char *name = dp->d_name.name;
1384 #if defined(AFS_LINUX26_ENV)
1385 maybe_lock_kernel();
1388 vattr.va_mask = ATTR_MODE;
1389 vattr.va_mode = mode;
1391 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1394 struct inode *ip = AFSTOV(tvcp);
1396 afs_getattr(tvcp, &vattr, credp);
1397 afs_fill_inode(ip, &vattr);
1399 dp->d_op = &afs_dentry_operations;
1400 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1401 d_instantiate(dp, ip);
1405 #if defined(AFS_LINUX26_ENV)
1406 maybe_unlock_kernel();
1409 return afs_convert_code(code);
1413 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1416 cred_t *credp = crref();
1417 const char *name = dp->d_name.name;
1419 /* locking kernel conflicts with glock? */
1422 code = afs_rmdir(VTOAFS(dip), name, credp);
1425 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1426 * that failed because a directory is not empty. So, we map
1427 * EEXIST to ENOTEMPTY on linux.
1429 if (code == EEXIST) {
1438 return afs_convert_code(code);
1443 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1444 struct inode *newip, struct dentry *newdp)
1447 cred_t *credp = crref();
1448 const char *oldname = olddp->d_name.name;
1449 const char *newname = newdp->d_name.name;
1450 struct dentry *rehash = NULL;
1452 #if defined(AFS_LINUX26_ENV)
1453 /* Prevent any new references during rename operation. */
1454 maybe_lock_kernel();
1456 if (!d_unhashed(newdp)) {
1461 if (!list_empty(&newdp->d_hash)) {
1467 #if defined(AFS_LINUX24_ENV)
1468 if (atomic_read(&olddp->d_count) > 1)
1469 shrink_dcache_parent(olddp);
1473 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1477 olddp->d_time = 0; /* force to revalidate */
1482 #if defined(AFS_LINUX26_ENV)
1483 maybe_unlock_kernel();
1487 return afs_convert_code(code);
1491 /* afs_linux_ireadlink
1492 * Internal readlink which can return link contents to user or kernel space.
1493 * Note that the buffer is NOT supposed to be null-terminated.
1496 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1499 cred_t *credp = crref();
1503 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1504 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1508 return maxlen - tuio.uio_resid;
1510 return afs_convert_code(code);
1513 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1514 /* afs_linux_readlink
1515 * Fill target (which is in user space) with contents of symlink.
1518 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1521 struct inode *ip = dp->d_inode;
1524 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1530 /* afs_linux_follow_link
1531 * a file system dependent link following routine.
1533 #if defined(AFS_LINUX24_ENV)
1534 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1539 name = osi_Alloc(PATH_MAX);
1545 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1553 code = vfs_follow_link(nd, name);
1556 osi_Free(name, PATH_MAX);
1561 #else /* !defined(AFS_LINUX24_ENV) */
1563 static struct dentry *
1564 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1565 unsigned int follow)
1573 name = osi_Alloc(PATH_MAX + 1);
1577 return ERR_PTR(-EIO);
1580 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1585 if (code < -MAX_ERRNO)
1586 res = ERR_PTR(-EIO);
1588 res = ERR_PTR(code);
1591 res = lookup_dentry(name, basep, follow);
1595 osi_Free(name, PATH_MAX + 1);
1599 #endif /* AFS_LINUX24_ENV */
1600 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1602 #if defined(AFS_CACHE_BYPASS)
1604 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1606 /* The kernel calls readpages before trying readpage, with a list of
1607 * pages. The readahead algorithm expands num_pages when it thinks
1608 * the application will benefit. Unlike readpage, the pages are not
1609 * necessarily allocated. If we do not a) allocate required pages and
1610 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1611 * for each required page (and the page will be pre-allocated) */
1614 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1615 struct list_head *page_list, unsigned num_pages)
1620 struct iovec* iovecp;
1621 struct nocache_read_request *ancr;
1622 struct page *pp, *ppt;
1623 struct pagevec lrupv;
1627 struct inode *ip = FILE_INODE(fp);
1628 struct vcache *avc = VTOAFS(ip);
1629 afs_int32 bypasscache = 0; /* bypass for this read */
1630 afs_int32 base_index = 0;
1631 afs_int32 page_count = 0;
1636 switch(cache_bypass_strategy) {
1637 case NEVER_BYPASS_CACHE:
1639 case ALWAYS_BYPASS_CACHE:
1642 case LARGE_FILES_BYPASS_CACHE:
1643 if(i_size_read(ip) > cache_bypass_threshold) {
1651 /* In the new incarnation of selective caching, a file's caching policy
1652 * can change, eg because file size exceeds threshold, etc. */
1653 trydo_cache_transition(avc, credp, bypasscache);
1656 while(!list_empty(page_list)) {
1657 pp = list_entry(page_list->prev, struct page, lru);
1662 /* background thread must free: iovecp, auio, ancr */
1663 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1665 auio = osi_Alloc(sizeof(uio_t));
1666 auio->uio_iov = iovecp;
1667 auio->uio_iovcnt = num_pages;
1668 auio->uio_flag = UIO_READ;
1669 auio->uio_seg = AFS_UIOSYS;
1670 auio->uio_resid = num_pages * PAGE_SIZE;
1672 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1674 ancr->offset = auio->uio_offset;
1675 ancr->length = auio->uio_resid;
1677 pagevec_init(&lrupv, 0);
1679 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1681 if(list_empty(page_list))
1684 pp = list_entry(page_list->prev, struct page, lru);
1685 /* If we allocate a page and don't remove it from page_list,
1686 * the page cache gets upset. */
1688 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1689 if(pp->index > isize) {
1696 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1697 auio->uio_offset = offset;
1698 base_index = pp->index;
1700 iovecp[page_ix].iov_len = PAGE_SIZE;
1701 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1702 if(base_index != pp->index) {
1705 page_cache_release(pp);
1706 iovecp[page_ix].iov_base = (void *) 0;
1714 page_cache_release(pp);
1715 iovecp[page_ix].iov_base = (void *) 0;
1718 if(!PageLocked(pp)) {
1722 /* save the page for background map */
1723 iovecp[page_ix].iov_base = (void*) pp;
1725 /* and put it on the LRU cache */
1726 if (!pagevec_add(&lrupv, pp))
1727 __pagevec_lru_add(&lrupv);
1731 /* If there were useful pages in the page list, make sure all pages
1732 * are in the LRU cache, then schedule the read */
1734 pagevec_lru_add(&lrupv);
1735 code = afs_ReadNoCache(avc, ancr, credp);
1737 /* If there is nothing for the background thread to handle,
1738 * it won't be freeing the things that we never gave it */
1739 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1740 osi_Free(auio, sizeof(uio_t));
1741 osi_Free(ancr, sizeof(struct nocache_read_request));
1743 /* we do not flush, release, or unmap pages--that will be
1744 * done for us by the background thread as each page comes in
1745 * from the fileserver */
1749 return afs_convert_code(code);
1752 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1753 #endif /* defined(AFS_CACHE_BYPASS */
1756 /* afs_linux_readpage
1757 * all reads come through here. A strategy-like read call.
1760 afs_linux_readpage(struct file *fp, struct page *pp)
1763 cred_t *credp = crref();
1764 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1766 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1768 ulong address = afs_linux_page_address(pp);
1769 afs_offs_t offset = pageoff(pp);
1771 #if defined(AFS_CACHE_BYPASS)
1772 afs_int32 bypasscache = 0; /* bypass for this read */
1773 struct nocache_read_request *ancr;
1777 struct iovec *iovecp;
1778 struct inode *ip = FILE_INODE(fp);
1779 afs_int32 cnt = page_count(pp);
1780 struct vcache *avc = VTOAFS(ip);
1782 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1786 atomic_add(1, &pp->count);
1787 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1788 clear_bit(PG_error, &pp->flags);
1790 #if defined(AFS_CACHE_BYPASS)
1791 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1792 /* If the page is past the end of the file, skip it */
1793 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1794 if(pp->index > isize) {
1801 /* if bypasscache, receiver frees, else we do */
1802 auio = osi_Alloc(sizeof(uio_t));
1803 iovecp = osi_Alloc(sizeof(struct iovec));
1805 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1808 #if defined(AFS_CACHE_BYPASS)
1810 switch(cache_bypass_strategy) {
1811 case NEVER_BYPASS_CACHE:
1813 case ALWAYS_BYPASS_CACHE:
1816 case LARGE_FILES_BYPASS_CACHE:
1817 if(i_size_read(ip) > cache_bypass_threshold) {
1825 /* In the new incarnation of selective caching, a file's caching policy
1826 * can change, eg because file size exceeds threshold, etc. */
1827 trydo_cache_transition(avc, credp, bypasscache);
1832 /* save the page for background map */
1833 auio->uio_iov->iov_base = (void*) pp;
1834 /* the background thread will free this */
1835 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1837 ancr->offset = offset;
1838 ancr->length = PAGE_SIZE;
1840 maybe_lock_kernel();
1841 code = afs_ReadNoCache(avc, ancr, credp);
1842 maybe_unlock_kernel();
1844 goto done; /* skips release page, doing it in bg thread */
1848 #ifdef AFS_LINUX24_ENV
1849 maybe_lock_kernel();
1853 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip, ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32, 99999); /* not a possible code value */
1855 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1857 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1858 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1860 AFS_DISCON_UNLOCK();
1862 #ifdef AFS_LINUX24_ENV
1863 maybe_unlock_kernel();
1866 /* XXX valid for no-cache also? Check last bits of files... :)
1867 * Cognate code goes in afs_NoCacheFetchProc. */
1868 if (auio->uio_resid) /* zero remainder of page */
1869 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1872 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1873 flush_dcache_page(pp);
1874 SetPageUptodate(pp);
1876 set_bit(PG_uptodate, &pp->flags);
1880 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1884 clear_bit(PG_locked, &pp->flags);
1889 #if defined(AFS_CACHE_BYPASS)
1891 /* do not call afs_GetDCache if cache is bypassed */
1897 /* free if not bypassing cache */
1898 osi_Free(auio, sizeof(uio_t));
1899 osi_Free(iovecp, sizeof(struct iovec));
1901 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1903 struct vrequest treq;
1906 code = afs_InitReq(&treq, credp);
1907 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1908 tdc = afs_FindDCache(avc, offset);
1910 if (!(tdc->mflags & DFNextStarted))
1911 afs_PrefetchChunk(avc, tdc, credp, &treq);
1914 ReleaseWriteLock(&avc->lock);
1921 return afs_convert_code(code);
1925 #if defined(AFS_LINUX24_ENV)
1927 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1928 unsigned long offset, unsigned int count)
1930 struct vcache *vcp = VTOAFS(ip);
1939 buffer = kmap(pp) + offset;
1940 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1943 maybe_lock_kernel();
1945 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1946 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1947 ICL_TYPE_INT32, 99999);
1949 ObtainWriteLock(&vcp->lock, 532);
1950 if (vcp->f.states & CPageWrite) {
1951 ReleaseWriteLock(&vcp->lock);
1953 maybe_unlock_kernel();
1956 #ifdef AFS_LINUX26_ENV
1957 #if defined(WRITEPAGE_ACTIVATE)
1958 return WRITEPAGE_ACTIVATE;
1960 return AOP_WRITEPAGE_ACTIVATE;
1963 /* should mark it dirty? */
1967 vcp->f.states |= CPageWrite;
1968 ReleaseWriteLock(&vcp->lock);
1970 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1972 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1974 i_size_write(ip, vcp->f.m.Length);
1975 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1977 ObtainWriteLock(&vcp->lock, 533);
1979 struct vrequest treq;
1981 if (!afs_InitReq(&treq, credp))
1982 code = afs_DoPartialWrite(vcp, &treq);
1984 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1986 vcp->f.states &= ~CPageWrite;
1987 ReleaseWriteLock(&vcp->lock);
1989 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1990 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1991 ICL_TYPE_INT32, code);
1994 maybe_unlock_kernel();
2003 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2004 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2006 afs_linux_writepage(struct page *pp)
2009 struct address_space *mapping = pp->mapping;
2010 struct inode *inode;
2011 unsigned long end_index;
2012 unsigned offset = PAGE_CACHE_SIZE;
2015 #if defined(AFS_LINUX26_ENV)
2016 if (PageReclaim(pp)) {
2017 # if defined(WRITEPAGE_ACTIVATE)
2018 return WRITEPAGE_ACTIVATE;
2020 return AOP_WRITEPAGE_ACTIVATE;
2024 if (PageLaunder(pp)) {
2025 return(fail_writepage(pp));
2029 inode = (struct inode *)mapping->host;
2030 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
2033 if (pp->index < end_index)
2035 /* things got complicated... */
2036 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
2037 /* OK, are we completely out? */
2038 if (pp->index >= end_index + 1 || !offset)
2041 status = afs_linux_writepage_sync(inode, pp, 0, offset);
2042 SetPageUptodate(pp);
2043 #if defined(AFS_LINUX26_ENV)
2044 #if defined(WRITEPAGE_ACTIVATE)
2045 if ( status != WRITEPAGE_ACTIVATE )
2047 if ( status != AOP_WRITEPAGE_ACTIVATE )
2051 if (status == offset)
2057 /* afs_linux_updatepage
2058 * What one would have thought was writepage - write dirty page to file.
2059 * Called from generic_file_write. buffer is still in user space. pagep
2060 * has been filled in with old data if we're updating less than a page.
2063 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2064 unsigned int count, int sync)
2066 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2067 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2073 set_bit(PG_locked, &pp->flags);
2078 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2079 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2080 ICL_TYPE_INT32, 99999);
2081 setup_uio(&tuio, &iovec, page_addr + offset,
2082 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2085 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2087 i_size_write(ip, vcp->f.m.Length);
2088 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2091 struct vrequest treq;
2093 ObtainWriteLock(&vcp->lock, 533);
2094 vcp->f.m.Date = osi_Time(); /* set modification time */
2095 if (!afs_InitReq(&treq, credp))
2096 code = afs_DoPartialWrite(vcp, &treq);
2097 ReleaseWriteLock(&vcp->lock);
2100 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2101 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2102 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2103 ICL_TYPE_INT32, code);
2105 AFS_DISCON_UNLOCK();
2109 clear_bit(PG_locked, &pp->flags);
2114 /* afs_linux_permission
2115 * Check access rights - returns error if can't check or permission denied.
2118 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2119 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2121 afs_linux_permission(struct inode *ip, int mode)
2125 cred_t *credp = crref();
2129 if (mode & MAY_EXEC)
2131 if (mode & MAY_READ)
2133 if (mode & MAY_WRITE)
2135 code = afs_access(VTOAFS(ip), tmp, credp);
2139 return afs_convert_code(code);
2142 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2144 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2149 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2150 offset, to - offset);
2151 #if !defined(AFS_LINUX26_ENV)
2159 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2162 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2163 call kmap directly instead of relying on us to do it */
2164 #if !defined(AFS_LINUX26_ENV)
2171 #if defined(HAVE_WRITE_BEGIN)
2173 afs_linux_write_end(struct file *file, struct address_space *mapping,
2174 loff_t pos, unsigned len, unsigned copied,
2175 struct page *page, void *fsdata)
2178 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2179 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2181 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2184 page_cache_release(page);
2189 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2190 loff_t pos, unsigned len, unsigned flags,
2191 struct page **pagep, void **fsdata)
2194 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2195 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2196 page = grab_cache_page_write_begin(mapping, index, flags);
2198 page = __grab_cache_page(mapping, index);
2207 static struct inode_operations afs_file_iops = {
2208 #if defined(AFS_LINUX26_ENV)
2209 .permission = afs_linux_permission,
2210 .getattr = afs_linux_getattr,
2211 .setattr = afs_notify_change,
2212 #elif defined(AFS_LINUX24_ENV)
2213 .permission = afs_linux_permission,
2214 .revalidate = afs_linux_revalidate,
2215 .setattr = afs_notify_change,
2217 .default_file_ops = &afs_file_fops,
2218 .readpage = afs_linux_readpage,
2219 .revalidate = afs_linux_revalidate,
2220 .updatepage = afs_linux_updatepage,
2224 #if defined(AFS_LINUX24_ENV)
2225 static struct address_space_operations afs_file_aops = {
2226 .readpage = afs_linux_readpage,
2227 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2228 .readpages = afs_linux_readpages,
2230 .writepage = afs_linux_writepage,
2231 #if defined (HAVE_WRITE_BEGIN)
2232 .write_begin = afs_linux_write_begin,
2233 .write_end = afs_linux_write_end,
2235 .commit_write = afs_linux_commit_write,
2236 .prepare_write = afs_linux_prepare_write,
2242 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2243 * by what sort of operation is allowed.....
2246 static struct inode_operations afs_dir_iops = {
2247 #if !defined(AFS_LINUX24_ENV)
2248 .default_file_ops = &afs_dir_fops,
2250 .setattr = afs_notify_change,
2252 .create = afs_linux_create,
2253 .lookup = afs_linux_lookup,
2254 .link = afs_linux_link,
2255 .unlink = afs_linux_unlink,
2256 .symlink = afs_linux_symlink,
2257 .mkdir = afs_linux_mkdir,
2258 .rmdir = afs_linux_rmdir,
2259 .rename = afs_linux_rename,
2260 #if defined(AFS_LINUX26_ENV)
2261 .getattr = afs_linux_getattr,
2263 .revalidate = afs_linux_revalidate,
2265 .permission = afs_linux_permission,
2268 /* We really need a separate symlink set of ops, since do_follow_link()
2269 * determines if it _is_ a link by checking if the follow_link op is set.
2271 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2273 afs_symlink_filler(struct file *file, struct page *page)
2275 struct inode *ip = (struct inode *)page->mapping->host;
2276 char *p = (char *)kmap(page);
2279 maybe_lock_kernel();
2281 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2286 p[code] = '\0'; /* null terminate? */
2287 maybe_unlock_kernel();
2289 SetPageUptodate(page);
2295 maybe_unlock_kernel();
2303 static struct address_space_operations afs_symlink_aops = {
2304 .readpage = afs_symlink_filler
2306 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2308 static struct inode_operations afs_symlink_iops = {
2309 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2310 .readlink = page_readlink,
2311 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2312 .follow_link = page_follow_link,
2314 .follow_link = page_follow_link_light,
2315 .put_link = page_put_link,
2317 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2318 .readlink = afs_linux_readlink,
2319 .follow_link = afs_linux_follow_link,
2320 #if !defined(AFS_LINUX24_ENV)
2321 .permission = afs_linux_permission,
2322 .revalidate = afs_linux_revalidate,
2324 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2325 #if defined(AFS_LINUX24_ENV)
2326 .setattr = afs_notify_change,
2331 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2335 vattr2inode(ip, vattr);
2337 #if defined(AFS_LINUX26_ENV)
2338 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2340 /* Reset ops if symlink or directory. */
2341 if (S_ISREG(ip->i_mode)) {
2342 ip->i_op = &afs_file_iops;
2343 #if defined(AFS_LINUX24_ENV)
2344 ip->i_fop = &afs_file_fops;
2345 ip->i_data.a_ops = &afs_file_aops;
2348 } else if (S_ISDIR(ip->i_mode)) {
2349 ip->i_op = &afs_dir_iops;
2350 #if defined(AFS_LINUX24_ENV)
2351 ip->i_fop = &afs_dir_fops;
2354 } else if (S_ISLNK(ip->i_mode)) {
2355 ip->i_op = &afs_symlink_iops;
2356 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2357 ip->i_data.a_ops = &afs_symlink_aops;
2358 ip->i_mapping = &ip->i_data;