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"
27 #include "afs/sysincludes.h"
28 #include "afsincludes.h"
29 #include "afs/afs_stats.h"
31 #ifdef HAVE_MM_INLINE_H
32 #include "h/mm_inline.h"
34 #include "h/pagemap.h"
35 #if defined(AFS_LINUX24_ENV)
36 #include "h/smp_lock.h"
38 #if defined(AFS_LINUX26_ENV)
39 #include "h/writeback.h"
40 #include "h/pagevec.h"
42 #if defined(AFS_CACHE_BYPASS)
44 #include "afs/afs_bypasscache.h"
48 #define pageoff(pp) pgoff2loff((pp)->index)
50 #define pageoff(pp) pp->offset
54 #define MAX_ERRNO 1000L
57 #if defined(AFS_LINUX26_ENV)
58 #define LockPage(pp) lock_page(pp)
59 #define UnlockPage(pp) unlock_page(pp)
60 extern struct backing_dev_info afs_backing_dev_info;
63 extern struct vcache *afs_globalVp;
64 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
65 #if defined(AFS_LINUX24_ENV)
66 /* Some uses of BKL are perhaps not needed for bypass or memcache--
67 * why don't we try it out? */
68 extern struct afs_cacheOps afs_UfsCacheOps;
69 #define maybe_lock_kernel() \
71 if(afs_cacheType == &afs_UfsCacheOps) \
76 #define maybe_unlock_kernel() \
78 if(afs_cacheType == &afs_UfsCacheOps) \
81 #endif /* AFS_LINUX24_ENV */
84 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
87 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
88 cred_t *credp = crref();
90 afs_size_t isize, offindex;
92 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
93 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
95 /* get a validated vcache entry */
96 code = afs_InitReq(&treq, credp);
98 code = afs_VerifyVCache(vcp, &treq);
103 #if defined(AFS_CACHE_BYPASS)
104 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
105 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
106 offindex = *offp >> PAGE_CACHE_SHIFT;
107 if(offindex > isize) {
113 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
116 code = do_sync_read(fp, buf, count, offp);
118 code = generic_file_read(fp, buf, count, offp);
123 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
124 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
133 /* Now we have integrated VM for writes as well as reads. generic_file_write
134 * also takes care of re-positioning the pointer if file is open in append
135 * mode. Call fake open/close to ensure we do writes of core dumps.
138 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
141 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
142 struct vrequest treq;
143 cred_t *credp = crref();
147 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
148 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
149 (fp->f_flags & O_APPEND) ? 99998 : 99999);
152 /* get a validated vcache entry */
153 code = (ssize_t) afs_InitReq(&treq, credp);
155 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
157 ObtainWriteLock(&vcp->lock, 529);
159 ReleaseWriteLock(&vcp->lock);
165 code = do_sync_write(fp, buf, count, offp);
167 code = generic_file_write(fp, buf, count, offp);
172 ObtainWriteLock(&vcp->lock, 530);
173 afs_FakeClose(vcp, credp);
174 ReleaseWriteLock(&vcp->lock);
176 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
177 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
185 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
187 /* This is a complete rewrite of afs_readdir, since we can make use of
188 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
189 * handling and use of bulkstats will need to be reflected here as well.
192 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
194 struct vcache *avc = VTOAFS(FILE_INODE(fp));
195 struct vrequest treq;
196 register struct dcache *tdc;
203 afs_size_t origOffset, tlen;
204 cred_t *credp = crref();
205 struct afs_fakestat_state fakestat;
207 #if defined(AFS_LINUX26_ENV)
211 AFS_STATCNT(afs_readdir);
213 code = afs_InitReq(&treq, credp);
218 afs_InitFakeStat(&fakestat);
219 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
223 /* update the cache entry */
225 code = afs_VerifyVCache(avc, &treq);
229 /* get a reference to the entire directory */
230 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
236 ObtainSharedLock(&avc->lock, 810);
237 UpgradeSToWLock(&avc->lock, 811);
238 ObtainReadLock(&tdc->lock);
240 * Make sure that the data in the cache is current. There are two
241 * cases we need to worry about:
242 * 1. The cache data is being fetched by another process.
243 * 2. The cache data is no longer valid
245 while ((avc->f.states & CStatd)
246 && (tdc->dflags & DFFetching)
247 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
248 ReleaseReadLock(&tdc->lock);
249 ReleaseSharedLock(&avc->lock);
250 afs_osi_Sleep(&tdc->validPos);
251 ObtainSharedLock(&avc->lock, 812);
252 ObtainReadLock(&tdc->lock);
254 if (!(avc->f.states & CStatd)
255 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
256 ReleaseReadLock(&tdc->lock);
257 ReleaseSharedLock(&avc->lock);
262 /* Set the readdir-in-progress flag, and downgrade the lock
263 * to shared so others will be able to acquire a read lock.
265 avc->f.states |= CReadDir;
266 avc->dcreaddir = tdc;
267 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
268 ConvertWToSLock(&avc->lock);
270 /* Fill in until we get an error or we're done. This implementation
271 * takes an offset in units of blobs, rather than bytes.
274 offset = (int) fp->f_pos;
276 dirpos = BlobScan(tdc, offset);
280 de = afs_dir_GetBlob(tdc, dirpos);
284 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
287 len = strlen(de->name);
289 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
290 (unsigned long)&tdc->f.inode, dirpos);
291 DRelease((struct buffer *) de, 0);
292 ReleaseSharedLock(&avc->lock);
298 /* filldir returns -EINVAL when the buffer is full. */
299 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
301 unsigned int type = DT_UNKNOWN;
302 struct VenusFid afid;
305 afid.Cell = avc->f.fid.Cell;
306 afid.Fid.Volume = avc->f.fid.Fid.Volume;
307 afid.Fid.Vnode = ntohl(de->fid.vnode);
308 afid.Fid.Unique = ntohl(de->fid.vunique);
309 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
311 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
314 } else if (((tvc->f.states) & (CStatd | CTruth))) {
315 /* CTruth will be set if the object has
320 else if (vtype == VREG)
322 /* Don't do this until we're sure it can't be a mtpt */
323 /* else if (vtype == VLNK)
325 /* what other types does AFS support? */
327 /* clean up from afs_FindVCache */
331 * If this is NFS readdirplus, then the filler is going to
332 * call getattr on this inode, which will deadlock if we're
336 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
340 code = (*filldir) (dirbuf, de->name, len, offset, ino);
342 DRelease((struct buffer *)de, 0);
345 offset = dirpos + 1 + ((len + 16) >> 5);
347 /* If filldir didn't fill in the last one this is still pointing to that
350 fp->f_pos = (loff_t) offset;
352 ReleaseReadLock(&tdc->lock);
354 UpgradeSToWLock(&avc->lock, 813);
355 avc->f.states &= ~CReadDir;
357 avc->readdir_pid = 0;
358 ReleaseSharedLock(&avc->lock);
362 afs_PutFakeStat(&fakestat);
365 #if defined(AFS_LINUX26_ENV)
366 maybe_unlock_kernel();
372 /* in afs_pioctl.c */
373 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
376 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
377 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
379 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
386 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
388 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
389 cred_t *credp = crref();
390 struct vrequest treq;
394 #if defined(AFS_LINUX24_ENV)
395 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
396 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
397 vmap->vm_end - vmap->vm_start);
399 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
400 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
401 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
405 /* get a validated vcache entry */
406 code = afs_InitReq(&treq, credp);
410 code = afs_VerifyVCache(vcp, &treq);
414 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
417 code = generic_file_mmap(fp, vmap);
420 vcp->f.states |= CMAPPED;
433 afs_linux_open(struct inode *ip, struct file *fp)
435 struct vcache *vcp = VTOAFS(ip);
436 cred_t *credp = crref();
439 #ifdef AFS_LINUX24_ENV
443 code = afs_open(&vcp, fp->f_flags, credp);
445 #ifdef AFS_LINUX24_ENV
446 maybe_unlock_kernel();
454 afs_linux_release(struct inode *ip, struct file *fp)
456 struct vcache *vcp = VTOAFS(ip);
457 cred_t *credp = crref();
460 #ifdef AFS_LINUX24_ENV
464 code = afs_close(vcp, fp->f_flags, credp);
466 #ifdef AFS_LINUX24_ENV
467 maybe_unlock_kernel();
475 #if defined(AFS_LINUX24_ENV)
476 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
478 afs_linux_fsync(struct file *fp, struct dentry *dp)
482 struct inode *ip = FILE_INODE(fp);
483 cred_t *credp = crref();
485 #ifdef AFS_LINUX24_ENV
489 code = afs_fsync(VTOAFS(ip), credp);
491 #ifdef AFS_LINUX24_ENV
492 maybe_unlock_kernel();
501 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
504 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
505 cred_t *credp = crref();
506 struct AFS_FLOCK flock;
507 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
508 struct file_lock conflict;
509 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
510 struct file_lock *conflict;
513 /* Convert to a lock format afs_lockctl understands. */
514 memset((char *)&flock, 0, sizeof(flock));
515 flock.l_type = flp->fl_type;
516 flock.l_pid = flp->fl_pid;
518 flock.l_start = flp->fl_start;
519 flock.l_len = flp->fl_end - flp->fl_start + 1;
521 /* Safe because there are no large files, yet */
522 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
523 if (cmd == F_GETLK64)
525 else if (cmd == F_SETLK64)
527 else if (cmd == F_SETLKW64)
529 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
532 code = afs_lockctl(vcp, &flock, cmd, credp);
535 #ifdef AFS_LINUX24_ENV
536 if ((code == 0 || flp->fl_type == F_UNLCK) &&
537 (cmd == F_SETLK || cmd == F_SETLKW)) {
538 # ifdef POSIX_LOCK_FILE_WAIT_ARG
539 code = posix_lock_file(fp, flp, 0);
541 flp->fl_flags &=~ FL_SLEEP;
542 code = posix_lock_file(fp, flp);
544 if (code && flp->fl_type != F_UNLCK) {
545 struct AFS_FLOCK flock2;
547 flock2.l_type = F_UNLCK;
549 afs_lockctl(vcp, &flock2, F_SETLK, credp);
553 /* If lockctl says there are no conflicting locks, then also check with the
554 * kernel, as lockctl knows nothing about byte range locks
556 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
557 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
558 if (posix_test_lock(fp, flp, &conflict)) {
559 locks_copy_lock(flp, &conflict);
560 flp->fl_type = F_UNLCK;
564 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
565 if ((conflict = posix_test_lock(fp, flp))) {
566 locks_copy_lock(flp, conflict);
567 flp->fl_type = F_UNLCK;
572 posix_test_lock(fp, flp);
573 /* If we found a lock in the kernel's structure, return it */
574 if (flp->fl_type != F_UNLCK) {
582 /* Convert flock back to Linux's file_lock */
583 flp->fl_type = flock.l_type;
584 flp->fl_pid = flock.l_pid;
585 flp->fl_start = flock.l_start;
586 flp->fl_end = flock.l_start + flock.l_len - 1;
593 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
595 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
597 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
598 cred_t *credp = crref();
599 struct AFS_FLOCK flock;
600 /* Convert to a lock format afs_lockctl understands. */
601 memset((char *)&flock, 0, sizeof(flock));
602 flock.l_type = flp->fl_type;
603 flock.l_pid = flp->fl_pid;
606 flock.l_len = OFFSET_MAX;
608 /* Safe because there are no large files, yet */
609 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
610 if (cmd == F_GETLK64)
612 else if (cmd == F_SETLK64)
614 else if (cmd == F_SETLKW64)
616 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
619 code = afs_lockctl(vcp, &flock, cmd, credp);
622 if ((code == 0 || flp->fl_type == F_UNLCK) &&
623 (cmd == F_SETLK || cmd == F_SETLKW)) {
624 flp->fl_flags &=~ FL_SLEEP;
625 code = flock_lock_file_wait(fp, flp);
626 if (code && flp->fl_type != F_UNLCK) {
627 struct AFS_FLOCK flock2;
629 flock2.l_type = F_UNLCK;
631 afs_lockctl(vcp, &flock2, F_SETLK, credp);
635 /* Convert flock back to Linux's file_lock */
636 flp->fl_type = flock.l_type;
637 flp->fl_pid = flock.l_pid;
645 * essentially the same as afs_fsync() but we need to get the return
646 * code for the sys_close() here, not afs_linux_release(), so call
647 * afs_StoreAllSegments() with AFS_LASTSTORE
650 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
651 afs_linux_flush(struct file *fp, fl_owner_t id)
653 afs_linux_flush(struct file *fp)
656 struct vrequest treq;
660 #if defined(AFS_CACHE_BYPASS)
666 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
674 vcp = VTOAFS(FILE_INODE(fp));
676 code = afs_InitReq(&treq, credp);
679 #if defined(AFS_CACHE_BYPASS)
680 /* If caching is bypassed for this file, or globally, just return 0 */
681 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
684 ObtainReadLock(&vcp->lock);
685 if(vcp->cachingStates & FCSBypass)
687 ReleaseReadLock(&vcp->lock);
690 /* future proof: don't rely on 0 return from afs_InitReq */
695 ObtainSharedLock(&vcp->lock, 535);
696 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
697 UpgradeSToWLock(&vcp->lock, 536);
698 if (!AFS_IS_DISCONNECTED) {
699 code = afs_StoreAllSegments(vcp,
701 AFS_SYNC | AFS_LASTSTORE);
703 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
705 ConvertWToSLock(&vcp->lock);
707 code = afs_CheckCode(code, &treq, 54);
708 ReleaseSharedLock(&vcp->lock);
718 #if !defined(AFS_LINUX24_ENV)
719 /* Not allowed to directly read a directory. */
721 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
729 struct file_operations afs_dir_fops = {
730 #if !defined(AFS_LINUX24_ENV)
731 .read = afs_linux_dir_read,
732 .lock = afs_linux_lock,
733 .fsync = afs_linux_fsync,
735 .read = generic_read_dir,
737 .readdir = afs_linux_readdir,
738 #ifdef HAVE_UNLOCKED_IOCTL
739 .unlocked_ioctl = afs_unlocked_xioctl,
743 #ifdef HAVE_COMPAT_IOCTL
744 .compat_ioctl = afs_unlocked_xioctl,
746 .open = afs_linux_open,
747 .release = afs_linux_release,
750 struct file_operations afs_file_fops = {
751 .read = afs_linux_read,
752 .write = afs_linux_write,
753 #ifdef GENERIC_FILE_AIO_READ
754 .aio_read = generic_file_aio_read,
755 .aio_write = generic_file_aio_write,
757 #ifdef HAVE_UNLOCKED_IOCTL
758 .unlocked_ioctl = afs_unlocked_xioctl,
762 #ifdef HAVE_COMPAT_IOCTL
763 .compat_ioctl = afs_unlocked_xioctl,
765 .mmap = afs_linux_mmap,
766 .open = afs_linux_open,
767 .flush = afs_linux_flush,
768 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
769 .sendfile = generic_file_sendfile,
771 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
772 .splice_write = generic_file_splice_write,
773 .splice_read = generic_file_splice_read,
775 .release = afs_linux_release,
776 .fsync = afs_linux_fsync,
777 .lock = afs_linux_lock,
778 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
779 .flock = afs_linux_flock,
784 /**********************************************************************
785 * AFS Linux dentry operations
786 **********************************************************************/
788 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
789 * that has its mvid (parent dir's fid) pointer set to the wrong directory
790 * due to being mounted in multiple points at once. If so, check_bad_parent()
791 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
792 * dotdotfid and mtpoint fid members.
794 * dp - dentry to be checked.
798 * This dentry's vcache's mvid will be set to the correct parent directory's
800 * This root vnode's volume will have its dotdotfid and mtpoint fids set
801 * to the correct parent and mountpoint fids.
805 check_bad_parent(struct dentry *dp)
808 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
809 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
811 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
814 /* force a lookup, so vcp->mvid is fixed up */
815 afs_lookup(pvc, dp->d_name.name, &avc, credp);
816 if (!avc || vcp != avc) { /* bad, very bad.. */
817 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
818 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
819 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
820 ICL_TYPE_POINTER, dp);
823 AFS_RELE(AFSTOV(avc));
830 /* afs_linux_revalidate
831 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
834 afs_linux_revalidate(struct dentry *dp)
837 struct vcache *vcp = VTOAFS(dp->d_inode);
841 #ifdef AFS_LINUX24_ENV
847 /* Make this a fast path (no crref), since it's called so often. */
848 if (vcp->f.states & CStatd) {
850 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
851 check_bad_parent(dp); /* check and correct mvid */
854 #ifdef AFS_LINUX24_ENV
862 code = afs_getattr(vcp, &vattr, credp);
864 afs_fill_inode(AFSTOV(vcp), &vattr);
867 #ifdef AFS_LINUX24_ENV
868 maybe_unlock_kernel();
875 #if defined(AFS_LINUX26_ENV)
877 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
879 int err = afs_linux_revalidate(dentry);
881 generic_fillattr(dentry->d_inode, stat);
887 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
888 * In kernels 2.2.10 and above, we are passed an additional flags var which
889 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
890 * we are advised to follow the entry if it is a link or to make sure that
891 * it is a directory. But since the kernel itself checks these possibilities
892 * later on, we shouldn't have to do it until later. Perhaps in the future..
895 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
896 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
897 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
899 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
902 afs_linux_dentry_revalidate(struct dentry *dp)
906 cred_t *credp = NULL;
907 struct vcache *vcp, *pvcp, *tvc = NULL;
909 struct afs_fakestat_state fakestate;
911 #ifdef AFS_LINUX24_ENV
915 afs_InitFakeStat(&fakestate);
919 vcp = VTOAFS(dp->d_inode);
920 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
922 if (vcp == afs_globalVp)
925 if (vcp->mvstat == 1) { /* mount point */
926 if (vcp->mvid && (vcp->f.states & CMValid)) {
929 struct vrequest treq;
932 code = afs_InitReq(&treq, credp);
934 #ifdef AFS_DARWIN_ENV
935 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
936 (strcmp(dp->d_name.name, "Contents") == 0) ||
938 (strcmp(dp->d_name.name, ".directory") == 0)) {
942 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
944 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
945 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
946 /* a mount point, not yet replaced by its directory */
951 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
952 check_bad_parent(dp); /* check and correct mvid */
955 /* If the last looker changes, we should make sure the current
956 * looker still has permission to examine this file. This would
957 * always require a crref() which would be "slow".
959 if (vcp->last_looker != treq.uid) {
960 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
963 vcp->last_looker = treq.uid;
967 /* If the parent's DataVersion has changed or the vnode
968 * is longer valid, we need to do a full lookup. VerifyVCache
969 * isn't enough since the vnode may have been renamed.
972 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
975 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
976 if (!tvc || tvc != vcp)
979 if (afs_getattr(vcp, &vattr, credp))
982 vattr2inode(AFSTOV(vcp), &vattr);
983 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
986 /* should we always update the attributes at this point? */
987 /* unlikely--the vcache entry hasn't changed */
991 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
992 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
996 /* No change in parent's DataVersion so this negative
997 * lookup is still valid. BUT, if a server is down a
998 * negative lookup can result so there should be a
999 * liftime as well. For now, always expire.
1012 afs_PutFakeStat(&fakestate);
1018 shrink_dcache_parent(dp);
1021 #ifdef AFS_LINUX24_ENV
1022 maybe_unlock_kernel();
1027 if (have_submounts(dp))
1035 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1037 struct vcache *vcp = VTOAFS(ip);
1040 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1041 (void) afs_InactiveVCache(vcp, NULL);
1044 #ifdef DCACHE_NFSFS_RENAMED
1045 #ifdef AFS_LINUX26_ENV
1046 spin_lock(&dp->d_lock);
1048 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1049 #ifdef AFS_LINUX26_ENV
1050 spin_unlock(&dp->d_lock);
1058 afs_dentry_delete(struct dentry *dp)
1060 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1061 return 1; /* bad inode? */
1066 struct dentry_operations afs_dentry_operations = {
1067 .d_revalidate = afs_linux_dentry_revalidate,
1068 .d_delete = afs_dentry_delete,
1069 .d_iput = afs_dentry_iput,
1072 /**********************************************************************
1073 * AFS Linux inode operations
1074 **********************************************************************/
1078 * Merely need to set enough of vattr to get us through the create. Note
1079 * that the higher level code (open_namei) will take care of any tuncation
1080 * explicitly. Exclusive open is also taken care of in open_namei.
1082 * name is in kernel space at this point.
1085 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1086 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1087 struct nameidata *nd)
1089 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1093 cred_t *credp = crref();
1094 const char *name = dp->d_name.name;
1099 vattr.va_mode = mode;
1100 vattr.va_type = mode & S_IFMT;
1102 #if defined(AFS_LINUX26_ENV)
1103 maybe_lock_kernel();
1106 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1110 struct inode *ip = AFSTOV(vcp);
1112 afs_getattr(vcp, &vattr, credp);
1113 afs_fill_inode(ip, &vattr);
1114 insert_inode_hash(ip);
1115 dp->d_op = &afs_dentry_operations;
1116 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1117 d_instantiate(dp, ip);
1121 #if defined(AFS_LINUX26_ENV)
1122 maybe_unlock_kernel();
1128 /* afs_linux_lookup */
1129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1130 static struct dentry *
1131 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1132 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1133 struct nameidata *nd)
1135 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1139 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1142 cred_t *credp = crref();
1143 struct vcache *vcp = NULL;
1144 const char *comp = dp->d_name.name;
1145 struct inode *ip = NULL;
1146 #if defined(AFS_LINUX26_ENV)
1147 struct dentry *newdp = NULL;
1151 #if defined(AFS_LINUX26_ENV)
1152 maybe_lock_kernel();
1155 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1161 afs_getattr(vcp, &vattr, credp);
1162 afs_fill_inode(ip, &vattr);
1164 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1165 hlist_unhashed(&ip->i_hash)
1166 #elif defined(AFS_LINUX26_ENV)
1167 ip->i_hash.pprev == NULL
1169 ip->i_hash.prev == NULL
1172 insert_inode_hash(ip);
1174 dp->d_op = &afs_dentry_operations;
1175 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1178 #if defined(AFS_LINUX24_ENV)
1179 if (ip && S_ISDIR(ip->i_mode)) {
1180 struct dentry *alias;
1182 /* Try to invalidate an existing alias in favor of our new one */
1183 alias = d_find_alias(ip);
1184 #if defined(AFS_LINUX26_ENV)
1185 /* But not if it's disconnected; then we want d_splice_alias below */
1186 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1190 if (d_invalidate(alias) == 0) {
1194 #if defined(AFS_LINUX26_ENV)
1203 #if defined(AFS_LINUX26_ENV)
1204 newdp = d_splice_alias(ip, dp);
1209 #if defined(AFS_LINUX26_ENV)
1210 maybe_unlock_kernel();
1214 /* It's ok for the file to not be found. That's noted by the caller by
1215 * seeing that the dp->d_inode field is NULL.
1217 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1218 #if defined(AFS_LINUX26_ENV)
1219 if (!code || code == ENOENT)
1225 else if ((code >= 0) && (code <= MAX_ERRNO))
1226 return ERR_PTR(-code);
1228 return ERR_PTR(-EIO);
1237 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1240 cred_t *credp = crref();
1241 const char *name = newdp->d_name.name;
1242 struct inode *oldip = olddp->d_inode;
1244 /* If afs_link returned the vnode, we could instantiate the
1245 * dentry. Since it's not, we drop this one and do a new lookup.
1250 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1258 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1261 cred_t *credp = crref();
1262 const char *name = dp->d_name.name;
1263 struct vcache *tvc = VTOAFS(dp->d_inode);
1265 #if defined(AFS_LINUX26_ENV)
1266 maybe_lock_kernel();
1268 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1269 && !(tvc->f.states & CUnlinked)) {
1270 struct dentry *__dp;
1280 osi_FreeSmallSpace(__name);
1281 __name = afs_newname();
1284 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1288 } while (__dp->d_inode != NULL);
1291 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1293 tvc->mvid = (void *) __name;
1296 crfree(tvc->uncred);
1298 tvc->uncred = credp;
1299 tvc->f.states |= CUnlinked;
1300 #ifdef DCACHE_NFSFS_RENAMED
1301 #ifdef AFS_LINUX26_ENV
1302 spin_lock(&dp->d_lock);
1304 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1305 #ifdef AFS_LINUX26_ENV
1306 spin_unlock(&dp->d_lock);
1310 osi_FreeSmallSpace(__name);
1315 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1324 code = afs_remove(VTOAFS(dip), name, credp);
1329 #if defined(AFS_LINUX26_ENV)
1330 maybe_unlock_kernel();
1338 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1341 cred_t *credp = crref();
1343 const char *name = dp->d_name.name;
1345 /* If afs_symlink returned the vnode, we could instantiate the
1346 * dentry. Since it's not, we drop this one and do a new lookup.
1352 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1359 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1362 cred_t *credp = crref();
1363 struct vcache *tvcp = NULL;
1365 const char *name = dp->d_name.name;
1367 #if defined(AFS_LINUX26_ENV)
1368 maybe_lock_kernel();
1371 vattr.va_mask = ATTR_MODE;
1372 vattr.va_mode = mode;
1374 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1377 struct inode *ip = AFSTOV(tvcp);
1379 afs_getattr(tvcp, &vattr, credp);
1380 afs_fill_inode(ip, &vattr);
1382 dp->d_op = &afs_dentry_operations;
1383 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1384 d_instantiate(dp, ip);
1388 #if defined(AFS_LINUX26_ENV)
1389 maybe_unlock_kernel();
1396 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1399 cred_t *credp = crref();
1400 const char *name = dp->d_name.name;
1402 /* locking kernel conflicts with glock? */
1405 code = afs_rmdir(VTOAFS(dip), name, credp);
1408 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1409 * that failed because a directory is not empty. So, we map
1410 * EEXIST to ENOTEMPTY on linux.
1412 if (code == EEXIST) {
1426 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1427 struct inode *newip, struct dentry *newdp)
1430 cred_t *credp = crref();
1431 const char *oldname = olddp->d_name.name;
1432 const char *newname = newdp->d_name.name;
1433 struct dentry *rehash = NULL;
1435 #if defined(AFS_LINUX26_ENV)
1436 /* Prevent any new references during rename operation. */
1437 maybe_lock_kernel();
1439 if (!d_unhashed(newdp)) {
1444 if (!list_empty(&newdp->d_hash)) {
1450 #if defined(AFS_LINUX24_ENV)
1451 if (atomic_read(&olddp->d_count) > 1)
1452 shrink_dcache_parent(olddp);
1456 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1460 olddp->d_time = 0; /* force to revalidate */
1465 #if defined(AFS_LINUX26_ENV)
1466 maybe_unlock_kernel();
1474 /* afs_linux_ireadlink
1475 * Internal readlink which can return link contents to user or kernel space.
1476 * Note that the buffer is NOT supposed to be null-terminated.
1479 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1482 cred_t *credp = crref();
1486 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1487 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1491 return maxlen - tuio.uio_resid;
1496 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1497 /* afs_linux_readlink
1498 * Fill target (which is in user space) with contents of symlink.
1501 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1504 struct inode *ip = dp->d_inode;
1507 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1513 /* afs_linux_follow_link
1514 * a file system dependent link following routine.
1516 #if defined(AFS_LINUX24_ENV)
1517 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1522 name = osi_Alloc(PATH_MAX);
1528 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1536 code = vfs_follow_link(nd, name);
1539 osi_Free(name, PATH_MAX);
1544 #else /* !defined(AFS_LINUX24_ENV) */
1546 static struct dentry *
1547 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1548 unsigned int follow)
1556 name = osi_Alloc(PATH_MAX + 1);
1560 return ERR_PTR(-EIO);
1563 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1568 if (code < -MAX_ERRNO)
1569 res = ERR_PTR(-EIO);
1571 res = ERR_PTR(code);
1574 res = lookup_dentry(name, basep, follow);
1578 osi_Free(name, PATH_MAX + 1);
1582 #endif /* AFS_LINUX24_ENV */
1583 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1585 #if defined(AFS_CACHE_BYPASS)
1587 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1589 /* The kernel calls readpages before trying readpage, with a list of
1590 * pages. The readahead algorithm expands num_pages when it thinks
1591 * the application will benefit. Unlike readpage, the pages are not
1592 * necessarily allocated. If we do not a) allocate required pages and
1593 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1594 * for each required page (and the page will be pre-allocated) */
1597 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1598 struct list_head *page_list, unsigned num_pages)
1603 struct iovec* iovecp;
1604 struct nocache_read_request *ancr;
1605 struct page *pp, *ppt;
1606 struct pagevec lrupv;
1610 struct inode *ip = FILE_INODE(fp);
1611 struct vcache *avc = VTOAFS(ip);
1612 afs_int32 bypasscache = 0; /* bypass for this read */
1613 afs_int32 base_index = 0;
1614 afs_int32 page_count = 0;
1619 switch(cache_bypass_strategy) {
1620 case NEVER_BYPASS_CACHE:
1622 case ALWAYS_BYPASS_CACHE:
1625 case LARGE_FILES_BYPASS_CACHE:
1626 if(i_size_read(ip) > cache_bypass_threshold) {
1634 /* In the new incarnation of selective caching, a file's caching policy
1635 * can change, eg because file size exceeds threshold, etc. */
1636 trydo_cache_transition(avc, credp, bypasscache);
1639 while(!list_empty(page_list)) {
1640 pp = list_entry(page_list->prev, struct page, lru);
1645 /* background thread must free: iovecp, auio, ancr */
1646 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1648 auio = osi_Alloc(sizeof(uio_t));
1649 auio->uio_iov = iovecp;
1650 auio->uio_iovcnt = num_pages;
1651 auio->uio_flag = UIO_READ;
1652 auio->uio_seg = AFS_UIOSYS;
1653 auio->uio_resid = num_pages * PAGE_SIZE;
1655 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1657 ancr->offset = auio->uio_offset;
1658 ancr->length = auio->uio_resid;
1660 pagevec_init(&lrupv, 0);
1662 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1664 if(list_empty(page_list))
1667 pp = list_entry(page_list->prev, struct page, lru);
1668 /* If we allocate a page and don't remove it from page_list,
1669 * the page cache gets upset. */
1671 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1672 if(pp->index > isize) {
1679 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1680 auio->uio_offset = offset;
1681 base_index = pp->index;
1683 iovecp[page_ix].iov_len = PAGE_SIZE;
1684 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1685 if(base_index != pp->index) {
1688 page_cache_release(pp);
1689 iovecp[page_ix].iov_base = (void *) 0;
1697 page_cache_release(pp);
1698 iovecp[page_ix].iov_base = (void *) 0;
1701 if(!PageLocked(pp)) {
1705 /* save the page for background map */
1706 iovecp[page_ix].iov_base = (void*) pp;
1708 /* and put it on the LRU cache */
1709 if (!pagevec_add(&lrupv, pp))
1710 __pagevec_lru_add(&lrupv);
1714 /* If there were useful pages in the page list, make sure all pages
1715 * are in the LRU cache, then schedule the read */
1717 pagevec_lru_add(&lrupv);
1718 code = afs_ReadNoCache(avc, ancr, credp);
1720 /* If there is nothing for the background thread to handle,
1721 * it won't be freeing the things that we never gave it */
1722 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1723 osi_Free(auio, sizeof(uio_t));
1724 osi_Free(ancr, sizeof(struct nocache_read_request));
1726 /* we do not flush, release, or unmap pages--that will be
1727 * done for us by the background thread as each page comes in
1728 * from the fileserver */
1735 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1736 #endif /* defined(AFS_CACHE_BYPASS */
1739 /* afs_linux_readpage
1740 * all reads come through here. A strategy-like read call.
1743 afs_linux_readpage(struct file *fp, struct page *pp)
1746 cred_t *credp = crref();
1747 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1749 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1751 ulong address = afs_linux_page_address(pp);
1752 afs_offs_t offset = pageoff(pp);
1754 #if defined(AFS_CACHE_BYPASS)
1755 afs_int32 bypasscache = 0; /* bypass for this read */
1756 struct nocache_read_request *ancr;
1760 struct iovec *iovecp;
1761 struct inode *ip = FILE_INODE(fp);
1762 afs_int32 cnt = page_count(pp);
1763 struct vcache *avc = VTOAFS(ip);
1765 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1769 atomic_add(1, &pp->count);
1770 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1771 clear_bit(PG_error, &pp->flags);
1773 #if defined(AFS_CACHE_BYPASS)
1774 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1775 /* If the page is past the end of the file, skip it */
1776 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1777 if(pp->index > isize) {
1784 /* if bypasscache, receiver frees, else we do */
1785 auio = osi_Alloc(sizeof(uio_t));
1786 iovecp = osi_Alloc(sizeof(struct iovec));
1788 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1791 #if defined(AFS_CACHE_BYPASS)
1793 switch(cache_bypass_strategy) {
1794 case NEVER_BYPASS_CACHE:
1796 case ALWAYS_BYPASS_CACHE:
1799 case LARGE_FILES_BYPASS_CACHE:
1800 if(i_size_read(ip) > cache_bypass_threshold) {
1808 /* In the new incarnation of selective caching, a file's caching policy
1809 * can change, eg because file size exceeds threshold, etc. */
1810 trydo_cache_transition(avc, credp, bypasscache);
1815 /* save the page for background map */
1816 auio->uio_iov->iov_base = (void*) pp;
1817 /* the background thread will free this */
1818 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1820 ancr->offset = offset;
1821 ancr->length = PAGE_SIZE;
1823 maybe_lock_kernel();
1824 code = afs_ReadNoCache(avc, ancr, credp);
1825 maybe_unlock_kernel();
1827 goto done; /* skips release page, doing it in bg thread */
1831 #ifdef AFS_LINUX24_ENV
1832 maybe_lock_kernel();
1836 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 */
1838 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1840 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1841 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1843 AFS_DISCON_UNLOCK();
1845 #ifdef AFS_LINUX24_ENV
1846 maybe_unlock_kernel();
1849 /* XXX valid for no-cache also? Check last bits of files... :)
1850 * Cognate code goes in afs_NoCacheFetchProc. */
1851 if (auio->uio_resid) /* zero remainder of page */
1852 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1855 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1856 flush_dcache_page(pp);
1857 SetPageUptodate(pp);
1859 set_bit(PG_uptodate, &pp->flags);
1863 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1867 clear_bit(PG_locked, &pp->flags);
1872 #if defined(AFS_CACHE_BYPASS)
1874 /* do not call afs_GetDCache if cache is bypassed */
1880 /* free if not bypassing cache */
1881 osi_Free(auio, sizeof(uio_t));
1882 osi_Free(iovecp, sizeof(struct iovec));
1884 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1886 struct vrequest treq;
1889 code = afs_InitReq(&treq, credp);
1890 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1891 tdc = afs_FindDCache(avc, offset);
1893 if (!(tdc->mflags & DFNextStarted))
1894 afs_PrefetchChunk(avc, tdc, credp, &treq);
1897 ReleaseWriteLock(&avc->lock);
1908 #if defined(AFS_LINUX24_ENV)
1910 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1911 unsigned long offset, unsigned int count)
1913 struct vcache *vcp = VTOAFS(ip);
1922 buffer = kmap(pp) + offset;
1923 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1926 maybe_lock_kernel();
1928 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1929 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1930 ICL_TYPE_INT32, 99999);
1932 ObtainWriteLock(&vcp->lock, 532);
1933 if (vcp->f.states & CPageWrite) {
1934 ReleaseWriteLock(&vcp->lock);
1936 maybe_unlock_kernel();
1939 #ifdef AFS_LINUX26_ENV
1940 #if defined(WRITEPAGE_ACTIVATE)
1941 return WRITEPAGE_ACTIVATE;
1943 return AOP_WRITEPAGE_ACTIVATE;
1946 /* should mark it dirty? */
1950 vcp->f.states |= CPageWrite;
1951 ReleaseWriteLock(&vcp->lock);
1953 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1955 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1957 i_size_write(ip, vcp->f.m.Length);
1958 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1960 ObtainWriteLock(&vcp->lock, 533);
1962 struct vrequest treq;
1964 if (!afs_InitReq(&treq, credp))
1965 code = afs_DoPartialWrite(vcp, &treq);
1967 code = code ? -code : count - tuio.uio_resid;
1969 vcp->f.states &= ~CPageWrite;
1970 ReleaseWriteLock(&vcp->lock);
1972 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1973 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1974 ICL_TYPE_INT32, code);
1977 maybe_unlock_kernel();
1986 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1987 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1989 afs_linux_writepage(struct page *pp)
1992 struct address_space *mapping = pp->mapping;
1993 struct inode *inode;
1994 unsigned long end_index;
1995 unsigned offset = PAGE_CACHE_SIZE;
1998 #if defined(AFS_LINUX26_ENV)
1999 if (PageReclaim(pp)) {
2000 # if defined(WRITEPAGE_ACTIVATE)
2001 return WRITEPAGE_ACTIVATE;
2003 return AOP_WRITEPAGE_ACTIVATE;
2007 if (PageLaunder(pp)) {
2008 return(fail_writepage(pp));
2012 inode = (struct inode *)mapping->host;
2013 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
2016 if (pp->index < end_index)
2018 /* things got complicated... */
2019 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
2020 /* OK, are we completely out? */
2021 if (pp->index >= end_index + 1 || !offset)
2024 status = afs_linux_writepage_sync(inode, pp, 0, offset);
2025 SetPageUptodate(pp);
2026 #if defined(WRITEPAGE_ACTIVATE)
2027 if ( status != WRITEPAGE_ACTIVATE )
2029 if ( status != AOP_WRITEPAGE_ACTIVATE )
2032 if (status == offset)
2038 /* afs_linux_updatepage
2039 * What one would have thought was writepage - write dirty page to file.
2040 * Called from generic_file_write. buffer is still in user space. pagep
2041 * has been filled in with old data if we're updating less than a page.
2044 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2045 unsigned int count, int sync)
2047 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2048 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2054 set_bit(PG_locked, &pp->flags);
2059 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2060 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2061 ICL_TYPE_INT32, 99999);
2062 setup_uio(&tuio, &iovec, page_addr + offset,
2063 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2066 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2068 i_size_write(ip, vcp->f.m.Length);
2069 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2072 struct vrequest treq;
2074 ObtainWriteLock(&vcp->lock, 533);
2075 vcp->f.m.Date = osi_Time(); /* set modification time */
2076 if (!afs_InitReq(&treq, credp))
2077 code = afs_DoPartialWrite(vcp, &treq);
2078 ReleaseWriteLock(&vcp->lock);
2081 code = code ? -code : count - tuio.uio_resid;
2082 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2083 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2084 ICL_TYPE_INT32, code);
2086 AFS_DISCON_UNLOCK();
2090 clear_bit(PG_locked, &pp->flags);
2095 /* afs_linux_permission
2096 * Check access rights - returns error if can't check or permission denied.
2099 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2100 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2102 afs_linux_permission(struct inode *ip, int mode)
2106 cred_t *credp = crref();
2110 if (mode & MAY_EXEC)
2112 if (mode & MAY_READ)
2114 if (mode & MAY_WRITE)
2116 code = afs_access(VTOAFS(ip), tmp, credp);
2123 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2125 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2130 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2131 offset, to - offset);
2132 #if !defined(AFS_LINUX26_ENV)
2140 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2143 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2144 call kmap directly instead of relying on us to do it */
2145 #if !defined(AFS_LINUX26_ENV)
2152 #if defined(HAVE_WRITE_BEGIN)
2154 afs_linux_write_end(struct file *file, struct address_space *mapping,
2155 loff_t pos, unsigned len, unsigned copied,
2156 struct page *page, void *fsdata)
2159 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2160 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2162 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2165 page_cache_release(page);
2170 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2171 loff_t pos, unsigned len, unsigned flags,
2172 struct page **pagep, void **fsdata)
2175 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2176 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2177 page = grab_cache_page_write_begin(mapping, index, flags);
2179 page = __grab_cache_page(mapping, index);
2188 static struct inode_operations afs_file_iops = {
2189 #if defined(AFS_LINUX26_ENV)
2190 .permission = afs_linux_permission,
2191 .getattr = afs_linux_getattr,
2192 .setattr = afs_notify_change,
2193 #elif defined(AFS_LINUX24_ENV)
2194 .permission = afs_linux_permission,
2195 .revalidate = afs_linux_revalidate,
2196 .setattr = afs_notify_change,
2198 .default_file_ops = &afs_file_fops,
2199 .readpage = afs_linux_readpage,
2200 .revalidate = afs_linux_revalidate,
2201 .updatepage = afs_linux_updatepage,
2205 #if defined(AFS_LINUX24_ENV)
2206 static struct address_space_operations afs_file_aops = {
2207 .readpage = afs_linux_readpage,
2208 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2209 .readpages = afs_linux_readpages,
2211 .writepage = afs_linux_writepage,
2212 #if defined (HAVE_WRITE_BEGIN)
2213 .write_begin = afs_linux_write_begin,
2214 .write_end = afs_linux_write_end,
2216 .commit_write = afs_linux_commit_write,
2217 .prepare_write = afs_linux_prepare_write,
2223 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2224 * by what sort of operation is allowed.....
2227 static struct inode_operations afs_dir_iops = {
2228 #if !defined(AFS_LINUX24_ENV)
2229 .default_file_ops = &afs_dir_fops,
2231 .setattr = afs_notify_change,
2233 .create = afs_linux_create,
2234 .lookup = afs_linux_lookup,
2235 .link = afs_linux_link,
2236 .unlink = afs_linux_unlink,
2237 .symlink = afs_linux_symlink,
2238 .mkdir = afs_linux_mkdir,
2239 .rmdir = afs_linux_rmdir,
2240 .rename = afs_linux_rename,
2241 #if defined(AFS_LINUX26_ENV)
2242 .getattr = afs_linux_getattr,
2244 .revalidate = afs_linux_revalidate,
2246 .permission = afs_linux_permission,
2249 /* We really need a separate symlink set of ops, since do_follow_link()
2250 * determines if it _is_ a link by checking if the follow_link op is set.
2252 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2254 afs_symlink_filler(struct file *file, struct page *page)
2256 struct inode *ip = (struct inode *)page->mapping->host;
2257 char *p = (char *)kmap(page);
2260 maybe_lock_kernel();
2262 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2267 p[code] = '\0'; /* null terminate? */
2268 maybe_unlock_kernel();
2270 SetPageUptodate(page);
2276 maybe_unlock_kernel();
2284 static struct address_space_operations afs_symlink_aops = {
2285 .readpage = afs_symlink_filler
2287 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2289 static struct inode_operations afs_symlink_iops = {
2290 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2291 .readlink = page_readlink,
2292 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2293 .follow_link = page_follow_link,
2295 .follow_link = page_follow_link_light,
2296 .put_link = page_put_link,
2298 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2299 .readlink = afs_linux_readlink,
2300 .follow_link = afs_linux_follow_link,
2301 #if !defined(AFS_LINUX24_ENV)
2302 .permission = afs_linux_permission,
2303 .revalidate = afs_linux_revalidate,
2305 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2306 #if defined(AFS_LINUX24_ENV)
2307 .setattr = afs_notify_change,
2312 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2316 vattr2inode(ip, vattr);
2318 #if defined(AFS_LINUX26_ENV)
2319 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2321 /* Reset ops if symlink or directory. */
2322 if (S_ISREG(ip->i_mode)) {
2323 ip->i_op = &afs_file_iops;
2324 #if defined(AFS_LINUX24_ENV)
2325 ip->i_fop = &afs_file_fops;
2326 ip->i_data.a_ops = &afs_file_aops;
2329 } else if (S_ISDIR(ip->i_mode)) {
2330 ip->i_op = &afs_dir_iops;
2331 #if defined(AFS_LINUX24_ENV)
2332 ip->i_fop = &afs_dir_fops;
2335 } else if (S_ISLNK(ip->i_mode)) {
2336 ip->i_op = &afs_symlink_iops;
2337 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2338 ip->i_data.a_ops = &afs_symlink_aops;
2339 ip->i_mapping = &ip->i_data;