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
53 #if defined(AFS_LINUX26_ENV)
54 #define LockPage(pp) lock_page(pp)
55 #define UnlockPage(pp) unlock_page(pp)
56 extern struct backing_dev_info afs_backing_dev_info;
59 extern struct vcache *afs_globalVp;
60 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
61 #if defined(AFS_LINUX24_ENV)
62 /* Some uses of BKL are perhaps not needed for bypass or memcache--
63 * why don't we try it out? */
64 extern struct afs_cacheOps afs_UfsCacheOps;
65 #define maybe_lock_kernel() \
67 if(afs_cacheType == &afs_UfsCacheOps) \
72 #define maybe_unlock_kernel() \
74 if(afs_cacheType == &afs_UfsCacheOps) \
77 #endif /* AFS_LINUX24_ENV */
80 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
83 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
84 cred_t *credp = crref();
86 afs_size_t isize, offindex;
88 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
89 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
91 /* get a validated vcache entry */
92 code = afs_InitReq(&treq, credp);
94 code = afs_VerifyVCache(vcp, &treq);
99 #if defined(AFS_CACHE_BYPASS)
100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
101 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
102 offindex = *offp >> PAGE_CACHE_SHIFT;
103 if(offindex > isize) {
109 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
112 code = do_sync_read(fp, buf, count, offp);
114 code = generic_file_read(fp, buf, count, offp);
119 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
120 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
129 /* Now we have integrated VM for writes as well as reads. generic_file_write
130 * also takes care of re-positioning the pointer if file is open in append
131 * mode. Call fake open/close to ensure we do writes of core dumps.
134 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
137 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
138 struct vrequest treq;
139 cred_t *credp = crref();
143 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
144 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
145 (fp->f_flags & O_APPEND) ? 99998 : 99999);
148 /* get a validated vcache entry */
149 code = (ssize_t) afs_InitReq(&treq, credp);
151 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
153 ObtainWriteLock(&vcp->lock, 529);
155 ReleaseWriteLock(&vcp->lock);
161 code = do_sync_write(fp, buf, count, offp);
163 code = generic_file_write(fp, buf, count, offp);
168 ObtainWriteLock(&vcp->lock, 530);
169 afs_FakeClose(vcp, credp);
170 ReleaseWriteLock(&vcp->lock);
172 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
173 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
181 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
183 /* This is a complete rewrite of afs_readdir, since we can make use of
184 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
185 * handling and use of bulkstats will need to be reflected here as well.
188 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
190 struct vcache *avc = VTOAFS(FILE_INODE(fp));
191 struct vrequest treq;
192 register struct dcache *tdc;
199 afs_size_t origOffset, tlen;
200 cred_t *credp = crref();
201 struct afs_fakestat_state fakestat;
203 #if defined(AFS_LINUX26_ENV)
207 AFS_STATCNT(afs_readdir);
209 code = afs_InitReq(&treq, credp);
214 afs_InitFakeStat(&fakestat);
215 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
219 /* update the cache entry */
221 code = afs_VerifyVCache(avc, &treq);
225 /* get a reference to the entire directory */
226 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
232 ObtainSharedLock(&avc->lock, 810);
233 UpgradeSToWLock(&avc->lock, 811);
234 ObtainReadLock(&tdc->lock);
236 * Make sure that the data in the cache is current. There are two
237 * cases we need to worry about:
238 * 1. The cache data is being fetched by another process.
239 * 2. The cache data is no longer valid
241 while ((avc->f.states & CStatd)
242 && (tdc->dflags & DFFetching)
243 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
244 ReleaseReadLock(&tdc->lock);
245 ReleaseSharedLock(&avc->lock);
246 afs_osi_Sleep(&tdc->validPos);
247 ObtainSharedLock(&avc->lock, 812);
248 ObtainReadLock(&tdc->lock);
250 if (!(avc->f.states & CStatd)
251 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
252 ReleaseReadLock(&tdc->lock);
253 ReleaseSharedLock(&avc->lock);
258 /* Set the readdir-in-progress flag, and downgrade the lock
259 * to shared so others will be able to acquire a read lock.
261 avc->f.states |= CReadDir;
262 avc->dcreaddir = tdc;
263 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
264 ConvertWToSLock(&avc->lock);
266 /* Fill in until we get an error or we're done. This implementation
267 * takes an offset in units of blobs, rather than bytes.
270 offset = (int) fp->f_pos;
272 dirpos = BlobScan(tdc, offset);
276 de = afs_dir_GetBlob(tdc, dirpos);
280 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
283 len = strlen(de->name);
285 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
286 (unsigned long)&tdc->f.inode, dirpos);
287 DRelease((struct buffer *) de, 0);
288 ReleaseSharedLock(&avc->lock);
294 /* filldir returns -EINVAL when the buffer is full. */
295 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
297 unsigned int type = DT_UNKNOWN;
298 struct VenusFid afid;
301 afid.Cell = avc->f.fid.Cell;
302 afid.Fid.Volume = avc->f.fid.Fid.Volume;
303 afid.Fid.Vnode = ntohl(de->fid.vnode);
304 afid.Fid.Unique = ntohl(de->fid.vunique);
305 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
307 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
310 } else if (((tvc->f.states) & (CStatd | CTruth))) {
311 /* CTruth will be set if the object has
316 else if (vtype == VREG)
318 /* Don't do this until we're sure it can't be a mtpt */
319 /* else if (vtype == VLNK)
321 /* what other types does AFS support? */
323 /* clean up from afs_FindVCache */
327 * If this is NFS readdirplus, then the filler is going to
328 * call getattr on this inode, which will deadlock if we're
332 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
336 code = (*filldir) (dirbuf, de->name, len, offset, ino);
338 DRelease((struct buffer *)de, 0);
341 offset = dirpos + 1 + ((len + 16) >> 5);
343 /* If filldir didn't fill in the last one this is still pointing to that
346 fp->f_pos = (loff_t) offset;
348 ReleaseReadLock(&tdc->lock);
350 UpgradeSToWLock(&avc->lock, 813);
351 avc->f.states &= ~CReadDir;
353 avc->readdir_pid = 0;
354 ReleaseSharedLock(&avc->lock);
358 afs_PutFakeStat(&fakestat);
361 #if defined(AFS_LINUX26_ENV)
362 maybe_unlock_kernel();
368 /* in afs_pioctl.c */
369 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
372 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
373 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
375 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
382 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
384 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
385 cred_t *credp = crref();
386 struct vrequest treq;
390 #if defined(AFS_LINUX24_ENV)
391 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
392 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
393 vmap->vm_end - vmap->vm_start);
395 afs_Trace4(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, ICL_TYPE_INT32,
401 /* get a validated vcache entry */
402 code = afs_InitReq(&treq, credp);
406 code = afs_VerifyVCache(vcp, &treq);
410 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
413 code = generic_file_mmap(fp, vmap);
416 vcp->f.states |= CMAPPED;
429 afs_linux_open(struct inode *ip, struct file *fp)
431 struct vcache *vcp = VTOAFS(ip);
432 cred_t *credp = crref();
435 #ifdef AFS_LINUX24_ENV
439 code = afs_open(&vcp, fp->f_flags, credp);
441 #ifdef AFS_LINUX24_ENV
442 maybe_unlock_kernel();
450 afs_linux_release(struct inode *ip, struct file *fp)
452 struct vcache *vcp = VTOAFS(ip);
453 cred_t *credp = crref();
456 #ifdef AFS_LINUX24_ENV
460 code = afs_close(vcp, fp->f_flags, credp);
462 #ifdef AFS_LINUX24_ENV
463 maybe_unlock_kernel();
471 #if defined(AFS_LINUX24_ENV)
472 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
474 afs_linux_fsync(struct file *fp, struct dentry *dp)
478 struct inode *ip = FILE_INODE(fp);
479 cred_t *credp = crref();
481 #ifdef AFS_LINUX24_ENV
485 code = afs_fsync(VTOAFS(ip), credp);
487 #ifdef AFS_LINUX24_ENV
488 maybe_unlock_kernel();
497 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
500 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
501 cred_t *credp = crref();
502 struct AFS_FLOCK flock;
503 /* Convert to a lock format afs_lockctl understands. */
504 memset((char *)&flock, 0, sizeof(flock));
505 flock.l_type = flp->fl_type;
506 flock.l_pid = flp->fl_pid;
508 flock.l_start = flp->fl_start;
509 flock.l_len = flp->fl_end - flp->fl_start;
511 /* Safe because there are no large files, yet */
512 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
513 if (cmd == F_GETLK64)
515 else if (cmd == F_SETLK64)
517 else if (cmd == F_SETLKW64)
519 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
522 code = afs_lockctl(vcp, &flock, cmd, credp);
525 #ifdef AFS_LINUX24_ENV
526 if ((code == 0 || flp->fl_type == F_UNLCK) &&
527 (cmd == F_SETLK || cmd == F_SETLKW)) {
528 #ifdef POSIX_LOCK_FILE_WAIT_ARG
529 code = posix_lock_file(fp, flp, 0);
531 flp->fl_flags &=~ FL_SLEEP;
532 code = posix_lock_file(fp, flp);
534 if (code && flp->fl_type != F_UNLCK) {
535 struct AFS_FLOCK flock2;
537 flock2.l_type = F_UNLCK;
539 afs_lockctl(vcp, &flock2, F_SETLK, credp);
544 /* Convert flock back to Linux's file_lock */
545 flp->fl_type = flock.l_type;
546 flp->fl_pid = flock.l_pid;
547 flp->fl_start = flock.l_start;
548 flp->fl_end = flock.l_start + flock.l_len;
555 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
557 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
559 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
560 cred_t *credp = crref();
561 struct AFS_FLOCK flock;
562 /* Convert to a lock format afs_lockctl understands. */
563 memset((char *)&flock, 0, sizeof(flock));
564 flock.l_type = flp->fl_type;
565 flock.l_pid = flp->fl_pid;
568 flock.l_len = OFFSET_MAX;
570 /* Safe because there are no large files, yet */
571 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
572 if (cmd == F_GETLK64)
574 else if (cmd == F_SETLK64)
576 else if (cmd == F_SETLKW64)
578 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
581 code = afs_lockctl(vcp, &flock, cmd, credp);
584 if ((code == 0 || flp->fl_type == F_UNLCK) &&
585 (cmd == F_SETLK || cmd == F_SETLKW)) {
586 flp->fl_flags &=~ FL_SLEEP;
587 code = flock_lock_file_wait(fp, flp);
588 if (code && flp->fl_type != F_UNLCK) {
589 struct AFS_FLOCK flock2;
591 flock2.l_type = F_UNLCK;
593 afs_lockctl(vcp, &flock2, F_SETLK, credp);
597 /* Convert flock back to Linux's file_lock */
598 flp->fl_type = flock.l_type;
599 flp->fl_pid = flock.l_pid;
607 * essentially the same as afs_fsync() but we need to get the return
608 * code for the sys_close() here, not afs_linux_release(), so call
609 * afs_StoreAllSegments() with AFS_LASTSTORE
612 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
613 afs_linux_flush(struct file *fp, fl_owner_t id)
615 afs_linux_flush(struct file *fp)
618 struct vrequest treq;
622 #if defined(AFS_CACHE_BYPASS)
628 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
636 vcp = VTOAFS(FILE_INODE(fp));
638 code = afs_InitReq(&treq, credp);
641 #if defined(AFS_CACHE_BYPASS)
642 /* If caching is bypassed for this file, or globally, just return 0 */
643 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
646 ObtainReadLock(&vcp->lock);
647 if(vcp->cachingStates & FCSBypass)
649 ReleaseReadLock(&vcp->lock);
652 /* future proof: don't rely on 0 return from afs_InitReq */
657 ObtainSharedLock(&vcp->lock, 535);
658 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
659 UpgradeSToWLock(&vcp->lock, 536);
660 if (!AFS_IS_DISCONNECTED) {
661 code = afs_StoreAllSegments(vcp,
663 AFS_SYNC | AFS_LASTSTORE);
665 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
667 ConvertWToSLock(&vcp->lock);
669 code = afs_CheckCode(code, &treq, 54);
670 ReleaseSharedLock(&vcp->lock);
680 #if !defined(AFS_LINUX24_ENV)
681 /* Not allowed to directly read a directory. */
683 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
691 struct file_operations afs_dir_fops = {
692 #if !defined(AFS_LINUX24_ENV)
693 .read = afs_linux_dir_read,
694 .lock = afs_linux_lock,
695 .fsync = afs_linux_fsync,
697 .read = generic_read_dir,
699 .readdir = afs_linux_readdir,
700 #ifdef HAVE_UNLOCKED_IOCTL
701 .unlocked_ioctl = afs_unlocked_xioctl,
705 #ifdef HAVE_COMPAT_IOCTL
706 .compat_ioctl = afs_unlocked_xioctl,
708 .open = afs_linux_open,
709 .release = afs_linux_release,
712 struct file_operations afs_file_fops = {
713 .read = afs_linux_read,
714 .write = afs_linux_write,
715 #ifdef GENERIC_FILE_AIO_READ
716 .aio_read = generic_file_aio_read,
717 .aio_write = generic_file_aio_write,
719 #ifdef HAVE_UNLOCKED_IOCTL
720 .unlocked_ioctl = afs_unlocked_xioctl,
724 #ifdef HAVE_COMPAT_IOCTL
725 .compat_ioctl = afs_unlocked_xioctl,
727 .mmap = afs_linux_mmap,
728 .open = afs_linux_open,
729 .flush = afs_linux_flush,
730 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
731 .sendfile = generic_file_sendfile,
733 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
734 .splice_write = generic_file_splice_write,
735 .splice_read = generic_file_splice_read,
737 .release = afs_linux_release,
738 .fsync = afs_linux_fsync,
739 .lock = afs_linux_lock,
740 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
741 .flock = afs_linux_flock,
746 /**********************************************************************
747 * AFS Linux dentry operations
748 **********************************************************************/
750 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
751 * that has its mvid (parent dir's fid) pointer set to the wrong directory
752 * due to being mounted in multiple points at once. If so, check_bad_parent()
753 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
754 * dotdotfid and mtpoint fid members.
756 * dp - dentry to be checked.
760 * This dentry's vcache's mvid will be set to the correct parent directory's
762 * This root vnode's volume will have its dotdotfid and mtpoint fids set
763 * to the correct parent and mountpoint fids.
767 check_bad_parent(struct dentry *dp)
770 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
771 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
773 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
776 /* force a lookup, so vcp->mvid is fixed up */
777 afs_lookup(pvc, dp->d_name.name, &avc, credp);
778 if (!avc || vcp != avc) { /* bad, very bad.. */
779 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
780 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
781 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
782 ICL_TYPE_POINTER, dp);
785 AFS_RELE(AFSTOV(avc));
792 /* afs_linux_revalidate
793 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
796 afs_linux_revalidate(struct dentry *dp)
799 struct vcache *vcp = VTOAFS(dp->d_inode);
803 #ifdef AFS_LINUX24_ENV
809 /* Make this a fast path (no crref), since it's called so often. */
810 if (vcp->f.states & CStatd) {
812 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
813 check_bad_parent(dp); /* check and correct mvid */
816 #ifdef AFS_LINUX24_ENV
824 code = afs_getattr(vcp, &vattr, credp);
826 afs_fill_inode(AFSTOV(vcp), &vattr);
829 #ifdef AFS_LINUX24_ENV
830 maybe_unlock_kernel();
837 #if defined(AFS_LINUX26_ENV)
839 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
841 int err = afs_linux_revalidate(dentry);
843 generic_fillattr(dentry->d_inode, stat);
849 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
850 * In kernels 2.2.10 and above, we are passed an additional flags var which
851 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
852 * we are advised to follow the entry if it is a link or to make sure that
853 * it is a directory. But since the kernel itself checks these possibilities
854 * later on, we shouldn't have to do it until later. Perhaps in the future..
857 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
858 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
859 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
861 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
864 afs_linux_dentry_revalidate(struct dentry *dp)
868 cred_t *credp = NULL;
869 struct vcache *vcp, *pvcp, *tvc = NULL;
871 struct afs_fakestat_state fakestate;
873 #ifdef AFS_LINUX24_ENV
877 afs_InitFakeStat(&fakestate);
881 vcp = VTOAFS(dp->d_inode);
882 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
884 if (vcp == afs_globalVp)
887 if (vcp->mvstat == 1) { /* mount point */
888 if (vcp->mvid && (vcp->f.states & CMValid)) {
891 struct vrequest treq;
894 code = afs_InitReq(&treq, credp);
896 #ifdef AFS_DARWIN_ENV
897 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
898 (strcmp(dp->d_name.name, "Contents") == 0) ||
900 (strcmp(dp->d_name.name, ".directory") == 0)) {
904 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
906 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
907 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
908 /* a mount point, not yet replaced by its directory */
913 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
914 check_bad_parent(dp); /* check and correct mvid */
917 /* If the last looker changes, we should make sure the current
918 * looker still has permission to examine this file. This would
919 * always require a crref() which would be "slow".
921 if (vcp->last_looker != treq.uid) {
922 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
925 vcp->last_looker = treq.uid;
929 /* If the parent's DataVersion has changed or the vnode
930 * is longer valid, we need to do a full lookup. VerifyVCache
931 * isn't enough since the vnode may have been renamed.
934 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
937 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
938 if (!tvc || tvc != vcp)
941 if (afs_getattr(vcp, &vattr, credp))
944 vattr2inode(AFSTOV(vcp), &vattr);
945 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
948 /* should we always update the attributes at this point? */
949 /* unlikely--the vcache entry hasn't changed */
953 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
954 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
958 /* No change in parent's DataVersion so this negative
959 * lookup is still valid. BUT, if a server is down a
960 * negative lookup can result so there should be a
961 * liftime as well. For now, always expire.
974 afs_PutFakeStat(&fakestate);
980 shrink_dcache_parent(dp);
983 #ifdef AFS_LINUX24_ENV
984 maybe_unlock_kernel();
989 if (have_submounts(dp))
997 afs_dentry_iput(struct dentry *dp, struct inode *ip)
999 struct vcache *vcp = VTOAFS(ip);
1002 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1003 (void) afs_InactiveVCache(vcp, NULL);
1006 #ifdef DCACHE_NFSFS_RENAMED
1007 #ifdef AFS_LINUX26_ENV
1008 spin_lock(&dp->d_lock);
1010 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1011 #ifdef AFS_LINUX26_ENV
1012 spin_unlock(&dp->d_lock);
1020 afs_dentry_delete(struct dentry *dp)
1022 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1023 return 1; /* bad inode? */
1028 struct dentry_operations afs_dentry_operations = {
1029 .d_revalidate = afs_linux_dentry_revalidate,
1030 .d_delete = afs_dentry_delete,
1031 .d_iput = afs_dentry_iput,
1034 /**********************************************************************
1035 * AFS Linux inode operations
1036 **********************************************************************/
1040 * Merely need to set enough of vattr to get us through the create. Note
1041 * that the higher level code (open_namei) will take care of any tuncation
1042 * explicitly. Exclusive open is also taken care of in open_namei.
1044 * name is in kernel space at this point.
1047 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1048 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1049 struct nameidata *nd)
1051 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1055 cred_t *credp = crref();
1056 const char *name = dp->d_name.name;
1061 vattr.va_mode = mode;
1062 vattr.va_type = mode & S_IFMT;
1064 #if defined(AFS_LINUX26_ENV)
1065 maybe_lock_kernel();
1068 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1072 struct inode *ip = AFSTOV(vcp);
1074 afs_getattr(vcp, &vattr, credp);
1075 afs_fill_inode(ip, &vattr);
1076 insert_inode_hash(ip);
1077 dp->d_op = &afs_dentry_operations;
1078 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1079 d_instantiate(dp, ip);
1083 #if defined(AFS_LINUX26_ENV)
1084 maybe_unlock_kernel();
1090 /* afs_linux_lookup */
1091 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1092 static struct dentry *
1093 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1094 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1095 struct nameidata *nd)
1097 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1101 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1104 cred_t *credp = crref();
1105 struct vcache *vcp = NULL;
1106 const char *comp = dp->d_name.name;
1107 struct inode *ip = NULL;
1108 #if defined(AFS_LINUX26_ENV)
1109 struct dentry *newdp = NULL;
1113 #if defined(AFS_LINUX26_ENV)
1114 maybe_lock_kernel();
1117 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1123 afs_getattr(vcp, &vattr, credp);
1124 afs_fill_inode(ip, &vattr);
1126 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1127 hlist_unhashed(&ip->i_hash)
1128 #elif defined(AFS_LINUX26_ENV)
1129 ip->i_hash.pprev == NULL
1131 ip->i_hash.prev == NULL
1134 insert_inode_hash(ip);
1136 dp->d_op = &afs_dentry_operations;
1137 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1140 #if defined(AFS_LINUX24_ENV)
1141 if (ip && S_ISDIR(ip->i_mode)) {
1142 struct dentry *alias;
1144 /* Try to invalidate an existing alias in favor of our new one */
1145 alias = d_find_alias(ip);
1146 #if defined(AFS_LINUX26_ENV)
1147 /* But not if it's disconnected; then we want d_splice_alias below */
1148 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1152 if (d_invalidate(alias) == 0) {
1156 #if defined(AFS_LINUX26_ENV)
1165 #if defined(AFS_LINUX26_ENV)
1166 newdp = d_splice_alias(ip, dp);
1171 #if defined(AFS_LINUX26_ENV)
1172 maybe_unlock_kernel();
1176 /* It's ok for the file to not be found. That's noted by the caller by
1177 * seeing that the dp->d_inode field is NULL.
1179 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1180 #if defined(AFS_LINUX26_ENV)
1181 if (!code || code == ENOENT)
1188 return ERR_PTR(-code);
1197 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1200 cred_t *credp = crref();
1201 const char *name = newdp->d_name.name;
1202 struct inode *oldip = olddp->d_inode;
1204 /* If afs_link returned the vnode, we could instantiate the
1205 * dentry. Since it's not, we drop this one and do a new lookup.
1210 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1218 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1221 cred_t *credp = crref();
1222 const char *name = dp->d_name.name;
1223 struct vcache *tvc = VTOAFS(dp->d_inode);
1225 #if defined(AFS_LINUX26_ENV)
1226 maybe_lock_kernel();
1228 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1229 && !(tvc->f.states & CUnlinked)) {
1230 struct dentry *__dp;
1240 osi_FreeSmallSpace(__name);
1241 __name = afs_newname();
1244 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1248 } while (__dp->d_inode != NULL);
1251 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1253 tvc->mvid = (void *) __name;
1256 crfree(tvc->uncred);
1258 tvc->uncred = credp;
1259 tvc->f.states |= CUnlinked;
1260 #ifdef DCACHE_NFSFS_RENAMED
1261 #ifdef AFS_LINUX26_ENV
1262 spin_lock(&dp->d_lock);
1264 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1265 #ifdef AFS_LINUX26_ENV
1266 spin_unlock(&dp->d_lock);
1270 osi_FreeSmallSpace(__name);
1275 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1284 code = afs_remove(VTOAFS(dip), name, credp);
1289 #if defined(AFS_LINUX26_ENV)
1290 maybe_unlock_kernel();
1298 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1301 cred_t *credp = crref();
1303 const char *name = dp->d_name.name;
1305 /* If afs_symlink returned the vnode, we could instantiate the
1306 * dentry. Since it's not, we drop this one and do a new lookup.
1312 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1319 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1322 cred_t *credp = crref();
1323 struct vcache *tvcp = NULL;
1325 const char *name = dp->d_name.name;
1327 #if defined(AFS_LINUX26_ENV)
1328 maybe_lock_kernel();
1331 vattr.va_mask = ATTR_MODE;
1332 vattr.va_mode = mode;
1334 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1337 struct inode *ip = AFSTOV(tvcp);
1339 afs_getattr(tvcp, &vattr, credp);
1340 afs_fill_inode(ip, &vattr);
1342 dp->d_op = &afs_dentry_operations;
1343 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1344 d_instantiate(dp, ip);
1348 #if defined(AFS_LINUX26_ENV)
1349 maybe_unlock_kernel();
1356 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1359 cred_t *credp = crref();
1360 const char *name = dp->d_name.name;
1362 /* locking kernel conflicts with glock? */
1365 code = afs_rmdir(VTOAFS(dip), name, credp);
1368 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1369 * that failed because a directory is not empty. So, we map
1370 * EEXIST to ENOTEMPTY on linux.
1372 if (code == EEXIST) {
1386 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1387 struct inode *newip, struct dentry *newdp)
1390 cred_t *credp = crref();
1391 const char *oldname = olddp->d_name.name;
1392 const char *newname = newdp->d_name.name;
1393 struct dentry *rehash = NULL;
1395 #if defined(AFS_LINUX26_ENV)
1396 /* Prevent any new references during rename operation. */
1397 maybe_lock_kernel();
1399 if (!d_unhashed(newdp)) {
1404 if (!list_empty(&newdp->d_hash)) {
1410 #if defined(AFS_LINUX24_ENV)
1411 if (atomic_read(&olddp->d_count) > 1)
1412 shrink_dcache_parent(olddp);
1416 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1420 olddp->d_time = 0; /* force to revalidate */
1425 #if defined(AFS_LINUX26_ENV)
1426 maybe_unlock_kernel();
1434 /* afs_linux_ireadlink
1435 * Internal readlink which can return link contents to user or kernel space.
1436 * Note that the buffer is NOT supposed to be null-terminated.
1439 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1442 cred_t *credp = crref();
1446 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1447 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1451 return maxlen - tuio.uio_resid;
1456 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1457 /* afs_linux_readlink
1458 * Fill target (which is in user space) with contents of symlink.
1461 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1464 struct inode *ip = dp->d_inode;
1467 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1473 /* afs_linux_follow_link
1474 * a file system dependent link following routine.
1476 #if defined(AFS_LINUX24_ENV)
1477 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1482 name = osi_Alloc(PATH_MAX);
1488 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1496 code = vfs_follow_link(nd, name);
1499 osi_Free(name, PATH_MAX);
1504 #else /* !defined(AFS_LINUX24_ENV) */
1506 static struct dentry *
1507 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1508 unsigned int follow)
1516 name = osi_Alloc(PATH_MAX + 1);
1520 return ERR_PTR(-EIO);
1523 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1528 res = ERR_PTR(code);
1531 res = lookup_dentry(name, basep, follow);
1535 osi_Free(name, PATH_MAX + 1);
1539 #endif /* AFS_LINUX24_ENV */
1540 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1542 #if defined(AFS_CACHE_BYPASS)
1544 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1546 /* The kernel calls readpages before trying readpage, with a list of
1547 * pages. The readahead algorithm expands num_pages when it thinks
1548 * the application will benefit. Unlike readpage, the pages are not
1549 * necessarily allocated. If we do not a) allocate required pages and
1550 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1551 * for each required page (and the page will be pre-allocated) */
1554 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1555 struct list_head *page_list, unsigned num_pages)
1560 struct iovec* iovecp;
1561 struct nocache_read_request *ancr;
1562 struct page *pp, *ppt;
1563 struct pagevec lrupv;
1567 struct inode *ip = FILE_INODE(fp);
1568 struct vcache *avc = VTOAFS(ip);
1569 afs_int32 bypasscache = 0; /* bypass for this read */
1570 afs_int32 base_index = 0;
1571 afs_int32 page_count = 0;
1576 switch(cache_bypass_strategy) {
1577 case NEVER_BYPASS_CACHE:
1579 case ALWAYS_BYPASS_CACHE:
1582 case LARGE_FILES_BYPASS_CACHE:
1583 if(i_size_read(ip) > cache_bypass_threshold) {
1591 /* In the new incarnation of selective caching, a file's caching policy
1592 * can change, eg because file size exceeds threshold, etc. */
1593 trydo_cache_transition(avc, credp, bypasscache);
1596 while(!list_empty(page_list)) {
1597 pp = list_entry(page_list->prev, struct page, lru);
1602 /* background thread must free: iovecp, auio, ancr */
1603 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1605 auio = osi_Alloc(sizeof(uio_t));
1606 auio->uio_iov = iovecp;
1607 auio->uio_iovcnt = num_pages;
1608 auio->uio_flag = UIO_READ;
1609 auio->uio_seg = AFS_UIOSYS;
1610 auio->uio_resid = num_pages * PAGE_SIZE;
1612 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1614 ancr->offset = auio->uio_offset;
1615 ancr->length = auio->uio_resid;
1617 pagevec_init(&lrupv, 0);
1619 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1621 if(list_empty(page_list))
1624 pp = list_entry(page_list->prev, struct page, lru);
1625 /* If we allocate a page and don't remove it from page_list,
1626 * the page cache gets upset. */
1628 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1629 if(pp->index > isize) {
1636 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1637 auio->uio_offset = offset;
1638 base_index = pp->index;
1640 iovecp[page_ix].iov_len = PAGE_SIZE;
1641 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1642 if(base_index != pp->index) {
1645 page_cache_release(pp);
1646 iovecp[page_ix].iov_base = (void *) 0;
1654 page_cache_release(pp);
1655 iovecp[page_ix].iov_base = (void *) 0;
1658 if(!PageLocked(pp)) {
1662 /* save the page for background map */
1663 iovecp[page_ix].iov_base = (void*) pp;
1665 /* and put it on the LRU cache */
1666 if (!pagevec_add(&lrupv, pp))
1667 __pagevec_lru_add(&lrupv);
1671 /* If there were useful pages in the page list, make sure all pages
1672 * are in the LRU cache, then schedule the read */
1674 pagevec_lru_add(&lrupv);
1675 code = afs_ReadNoCache(avc, ancr, credp);
1677 /* If there is nothing for the background thread to handle,
1678 * it won't be freeing the things that we never gave it */
1679 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1680 osi_Free(auio, sizeof(uio_t));
1681 osi_Free(ancr, sizeof(struct nocache_read_request));
1683 /* we do not flush, release, or unmap pages--that will be
1684 * done for us by the background thread as each page comes in
1685 * from the fileserver */
1692 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1693 #endif /* defined(AFS_CACHE_BYPASS */
1696 /* afs_linux_readpage
1697 * all reads come through here. A strategy-like read call.
1700 afs_linux_readpage(struct file *fp, struct page *pp)
1703 cred_t *credp = crref();
1704 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1706 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1708 ulong address = afs_linux_page_address(pp);
1709 afs_offs_t offset = pageoff(pp);
1711 #if defined(AFS_CACHE_BYPASS)
1712 afs_int32 bypasscache = 0; /* bypass for this read */
1713 struct nocache_read_request *ancr;
1717 struct iovec *iovecp;
1718 struct inode *ip = FILE_INODE(fp);
1719 afs_int32 cnt = page_count(pp);
1720 struct vcache *avc = VTOAFS(ip);
1722 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1726 atomic_add(1, &pp->count);
1727 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1728 clear_bit(PG_error, &pp->flags);
1730 #if defined(AFS_CACHE_BYPASS)
1731 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1732 /* If the page is past the end of the file, skip it */
1733 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1734 if(pp->index > isize) {
1741 /* if bypasscache, receiver frees, else we do */
1742 auio = osi_Alloc(sizeof(uio_t));
1743 iovecp = osi_Alloc(sizeof(struct iovec));
1745 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1748 #if defined(AFS_CACHE_BYPASS)
1750 switch(cache_bypass_strategy) {
1751 case NEVER_BYPASS_CACHE:
1753 case ALWAYS_BYPASS_CACHE:
1756 case LARGE_FILES_BYPASS_CACHE:
1757 if(i_size_read(ip) > cache_bypass_threshold) {
1765 /* In the new incarnation of selective caching, a file's caching policy
1766 * can change, eg because file size exceeds threshold, etc. */
1767 trydo_cache_transition(avc, credp, bypasscache);
1772 /* save the page for background map */
1773 auio->uio_iov->iov_base = (void*) pp;
1774 /* the background thread will free this */
1775 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1777 ancr->offset = offset;
1778 ancr->length = PAGE_SIZE;
1780 maybe_lock_kernel();
1781 code = afs_ReadNoCache(avc, ancr, credp);
1782 maybe_unlock_kernel();
1784 goto done; /* skips release page, doing it in bg thread */
1788 #ifdef AFS_LINUX24_ENV
1789 maybe_lock_kernel();
1793 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 */
1795 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1797 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1798 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1800 AFS_DISCON_UNLOCK();
1802 #ifdef AFS_LINUX24_ENV
1803 maybe_unlock_kernel();
1806 /* XXX valid for no-cache also? Check last bits of files... :)
1807 * Cognate code goes in afs_NoCacheFetchProc. */
1808 if (auio->uio_resid) /* zero remainder of page */
1809 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1812 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1813 flush_dcache_page(pp);
1814 SetPageUptodate(pp);
1816 set_bit(PG_uptodate, &pp->flags);
1820 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1824 clear_bit(PG_locked, &pp->flags);
1829 #if defined(AFS_CACHE_BYPASS)
1831 /* do not call afs_GetDCache if cache is bypassed */
1837 /* free if not bypassing cache */
1838 osi_Free(auio, sizeof(uio_t));
1839 osi_Free(iovecp, sizeof(struct iovec));
1841 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1843 struct vrequest treq;
1846 code = afs_InitReq(&treq, credp);
1847 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1848 tdc = afs_FindDCache(avc, offset);
1850 if (!(tdc->mflags & DFNextStarted))
1851 afs_PrefetchChunk(avc, tdc, credp, &treq);
1854 ReleaseWriteLock(&avc->lock);
1865 #if defined(AFS_LINUX24_ENV)
1867 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1868 unsigned long offset, unsigned int count)
1870 struct vcache *vcp = VTOAFS(ip);
1879 buffer = kmap(pp) + offset;
1880 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1883 maybe_lock_kernel();
1885 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1886 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1887 ICL_TYPE_INT32, 99999);
1889 ObtainReadLock(&vcp->lock);
1890 if (vcp->f.states & CPageWrite) {
1891 ReleaseReadLock(&vcp->lock);
1893 maybe_unlock_kernel();
1896 #ifdef AFS_LINUX26_ENV
1897 #if defined(WRITEPAGE_ACTIVATE)
1898 return WRITEPAGE_ACTIVATE;
1900 return AOP_WRITEPAGE_ACTIVATE;
1903 /* should mark it dirty? */
1907 ReleaseReadLock(&vcp->lock);
1909 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1911 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1913 i_size_write(ip, vcp->f.m.Length);
1914 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1917 struct vrequest treq;
1919 ObtainWriteLock(&vcp->lock, 533);
1920 if (!afs_InitReq(&treq, credp))
1921 code = afs_DoPartialWrite(vcp, &treq);
1922 ReleaseWriteLock(&vcp->lock);
1924 code = code ? -code : count - tuio.uio_resid;
1926 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1927 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1928 ICL_TYPE_INT32, code);
1931 maybe_unlock_kernel();
1940 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1941 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1943 afs_linux_writepage(struct page *pp)
1946 struct address_space *mapping = pp->mapping;
1947 struct inode *inode;
1948 unsigned long end_index;
1949 unsigned offset = PAGE_CACHE_SIZE;
1952 #if defined(AFS_LINUX26_ENV)
1953 if (PageReclaim(pp)) {
1954 # if defined(WRITEPAGE_ACTIVATE)
1955 return WRITEPAGE_ACTIVATE;
1957 return AOP_WRITEPAGE_ACTIVATE;
1961 if (PageLaunder(pp)) {
1962 return(fail_writepage(pp));
1966 inode = (struct inode *)mapping->host;
1967 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
1970 if (pp->index < end_index)
1972 /* things got complicated... */
1973 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1974 /* OK, are we completely out? */
1975 if (pp->index >= end_index + 1 || !offset)
1978 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1979 SetPageUptodate(pp);
1981 if (status == offset)
1987 /* afs_linux_updatepage
1988 * What one would have thought was writepage - write dirty page to file.
1989 * Called from generic_file_write. buffer is still in user space. pagep
1990 * has been filled in with old data if we're updating less than a page.
1993 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1994 unsigned int count, int sync)
1996 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1997 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2003 set_bit(PG_locked, &pp->flags);
2008 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2009 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2010 ICL_TYPE_INT32, 99999);
2011 setup_uio(&tuio, &iovec, page_addr + offset,
2012 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2015 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2017 i_size_write(ip, vcp->f.m.Length);
2018 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2021 struct vrequest treq;
2023 ObtainWriteLock(&vcp->lock, 533);
2024 vcp->f.m.Date = osi_Time(); /* set modification time */
2025 if (!afs_InitReq(&treq, credp))
2026 code = afs_DoPartialWrite(vcp, &treq);
2027 ReleaseWriteLock(&vcp->lock);
2030 code = code ? -code : count - tuio.uio_resid;
2031 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2032 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2033 ICL_TYPE_INT32, code);
2035 AFS_DISCON_UNLOCK();
2039 clear_bit(PG_locked, &pp->flags);
2044 /* afs_linux_permission
2045 * Check access rights - returns error if can't check or permission denied.
2048 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2049 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2051 afs_linux_permission(struct inode *ip, int mode)
2055 cred_t *credp = crref();
2059 if (mode & MAY_EXEC)
2061 if (mode & MAY_READ)
2063 if (mode & MAY_WRITE)
2065 code = afs_access(VTOAFS(ip), tmp, credp);
2072 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2074 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2079 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2080 offset, to - offset);
2081 #if !defined(AFS_LINUX26_ENV)
2089 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2092 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2093 call kmap directly instead of relying on us to do it */
2094 #if !defined(AFS_LINUX26_ENV)
2101 #if defined(HAVE_WRITE_BEGIN)
2103 afs_linux_write_end(struct file *file, struct address_space *mapping,
2104 loff_t pos, unsigned len, unsigned copied,
2105 struct page *page, void *fsdata)
2108 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2109 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2111 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2114 page_cache_release(page);
2119 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2120 loff_t pos, unsigned len, unsigned flags,
2121 struct page **pagep, void **fsdata)
2124 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2125 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2126 page = grab_cache_page_write_begin(mapping, index, flags);
2128 page = __grab_cache_page(mapping, index);
2137 static struct inode_operations afs_file_iops = {
2138 #if defined(AFS_LINUX26_ENV)
2139 .permission = afs_linux_permission,
2140 .getattr = afs_linux_getattr,
2141 .setattr = afs_notify_change,
2142 #elif defined(AFS_LINUX24_ENV)
2143 .permission = afs_linux_permission,
2144 .revalidate = afs_linux_revalidate,
2145 .setattr = afs_notify_change,
2147 .default_file_ops = &afs_file_fops,
2148 .readpage = afs_linux_readpage,
2149 .revalidate = afs_linux_revalidate,
2150 .updatepage = afs_linux_updatepage,
2154 #if defined(AFS_LINUX24_ENV)
2155 static struct address_space_operations afs_file_aops = {
2156 .readpage = afs_linux_readpage,
2157 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2158 .readpages = afs_linux_readpages,
2160 .writepage = afs_linux_writepage,
2161 #if defined (HAVE_WRITE_BEGIN)
2162 .write_begin = afs_linux_write_begin,
2163 .write_end = afs_linux_write_end,
2165 .commit_write = afs_linux_commit_write,
2166 .prepare_write = afs_linux_prepare_write,
2172 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2173 * by what sort of operation is allowed.....
2176 static struct inode_operations afs_dir_iops = {
2177 #if !defined(AFS_LINUX24_ENV)
2178 .default_file_ops = &afs_dir_fops,
2180 .setattr = afs_notify_change,
2182 .create = afs_linux_create,
2183 .lookup = afs_linux_lookup,
2184 .link = afs_linux_link,
2185 .unlink = afs_linux_unlink,
2186 .symlink = afs_linux_symlink,
2187 .mkdir = afs_linux_mkdir,
2188 .rmdir = afs_linux_rmdir,
2189 .rename = afs_linux_rename,
2190 #if defined(AFS_LINUX26_ENV)
2191 .getattr = afs_linux_getattr,
2193 .revalidate = afs_linux_revalidate,
2195 .permission = afs_linux_permission,
2198 /* We really need a separate symlink set of ops, since do_follow_link()
2199 * determines if it _is_ a link by checking if the follow_link op is set.
2201 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2203 afs_symlink_filler(struct file *file, struct page *page)
2205 struct inode *ip = (struct inode *)page->mapping->host;
2206 char *p = (char *)kmap(page);
2209 maybe_lock_kernel();
2211 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2216 p[code] = '\0'; /* null terminate? */
2217 maybe_unlock_kernel();
2219 SetPageUptodate(page);
2225 maybe_unlock_kernel();
2233 static struct address_space_operations afs_symlink_aops = {
2234 .readpage = afs_symlink_filler
2236 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2238 static struct inode_operations afs_symlink_iops = {
2239 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2240 .readlink = page_readlink,
2241 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2242 .follow_link = page_follow_link,
2244 .follow_link = page_follow_link_light,
2245 .put_link = page_put_link,
2247 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2248 .readlink = afs_linux_readlink,
2249 .follow_link = afs_linux_follow_link,
2250 #if !defined(AFS_LINUX24_ENV)
2251 .permission = afs_linux_permission,
2252 .revalidate = afs_linux_revalidate,
2254 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2255 #if defined(AFS_LINUX24_ENV)
2256 .setattr = afs_notify_change,
2261 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2265 vattr2inode(ip, vattr);
2267 #if defined(AFS_LINUX26_ENV)
2268 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2270 /* Reset ops if symlink or directory. */
2271 if (S_ISREG(ip->i_mode)) {
2272 ip->i_op = &afs_file_iops;
2273 #if defined(AFS_LINUX24_ENV)
2274 ip->i_fop = &afs_file_fops;
2275 ip->i_data.a_ops = &afs_file_aops;
2278 } else if (S_ISDIR(ip->i_mode)) {
2279 ip->i_op = &afs_dir_iops;
2280 #if defined(AFS_LINUX24_ENV)
2281 ip->i_fop = &afs_dir_fops;
2284 } else if (S_ISLNK(ip->i_mode)) {
2285 ip->i_op = &afs_symlink_iops;
2286 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2287 ip->i_data.a_ops = &afs_symlink_aops;
2288 ip->i_mapping = &ip->i_data;