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 /* Convert to a lock format afs_lockctl understands. */
508 memset((char *)&flock, 0, sizeof(flock));
509 flock.l_type = flp->fl_type;
510 flock.l_pid = flp->fl_pid;
512 flock.l_start = flp->fl_start;
513 flock.l_len = flp->fl_end - flp->fl_start;
515 /* Safe because there are no large files, yet */
516 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
517 if (cmd == F_GETLK64)
519 else if (cmd == F_SETLK64)
521 else if (cmd == F_SETLKW64)
523 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
526 code = afs_lockctl(vcp, &flock, cmd, credp);
529 #ifdef AFS_LINUX24_ENV
530 if ((code == 0 || flp->fl_type == F_UNLCK) &&
531 (cmd == F_SETLK || cmd == F_SETLKW)) {
532 #ifdef POSIX_LOCK_FILE_WAIT_ARG
533 code = posix_lock_file(fp, flp, 0);
535 flp->fl_flags &=~ FL_SLEEP;
536 code = posix_lock_file(fp, flp);
538 if (code && flp->fl_type != F_UNLCK) {
539 struct AFS_FLOCK flock2;
541 flock2.l_type = F_UNLCK;
543 afs_lockctl(vcp, &flock2, F_SETLK, credp);
548 /* Convert flock back to Linux's file_lock */
549 flp->fl_type = flock.l_type;
550 flp->fl_pid = flock.l_pid;
551 flp->fl_start = flock.l_start;
552 flp->fl_end = flock.l_start + flock.l_len;
559 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
561 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
563 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
564 cred_t *credp = crref();
565 struct AFS_FLOCK flock;
566 /* Convert to a lock format afs_lockctl understands. */
567 memset((char *)&flock, 0, sizeof(flock));
568 flock.l_type = flp->fl_type;
569 flock.l_pid = flp->fl_pid;
572 flock.l_len = OFFSET_MAX;
574 /* Safe because there are no large files, yet */
575 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
576 if (cmd == F_GETLK64)
578 else if (cmd == F_SETLK64)
580 else if (cmd == F_SETLKW64)
582 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
585 code = afs_lockctl(vcp, &flock, cmd, credp);
588 if ((code == 0 || flp->fl_type == F_UNLCK) &&
589 (cmd == F_SETLK || cmd == F_SETLKW)) {
590 flp->fl_flags &=~ FL_SLEEP;
591 code = flock_lock_file_wait(fp, flp);
592 if (code && flp->fl_type != F_UNLCK) {
593 struct AFS_FLOCK flock2;
595 flock2.l_type = F_UNLCK;
597 afs_lockctl(vcp, &flock2, F_SETLK, credp);
601 /* Convert flock back to Linux's file_lock */
602 flp->fl_type = flock.l_type;
603 flp->fl_pid = flock.l_pid;
611 * essentially the same as afs_fsync() but we need to get the return
612 * code for the sys_close() here, not afs_linux_release(), so call
613 * afs_StoreAllSegments() with AFS_LASTSTORE
616 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
617 afs_linux_flush(struct file *fp, fl_owner_t id)
619 afs_linux_flush(struct file *fp)
622 struct vrequest treq;
626 #if defined(AFS_CACHE_BYPASS)
632 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
640 vcp = VTOAFS(FILE_INODE(fp));
642 code = afs_InitReq(&treq, credp);
645 #if defined(AFS_CACHE_BYPASS)
646 /* If caching is bypassed for this file, or globally, just return 0 */
647 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
650 ObtainReadLock(&vcp->lock);
651 if(vcp->cachingStates & FCSBypass)
653 ReleaseReadLock(&vcp->lock);
656 /* future proof: don't rely on 0 return from afs_InitReq */
661 ObtainSharedLock(&vcp->lock, 535);
662 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
663 UpgradeSToWLock(&vcp->lock, 536);
664 if (!AFS_IS_DISCONNECTED) {
665 code = afs_StoreAllSegments(vcp,
667 AFS_SYNC | AFS_LASTSTORE);
669 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
671 ConvertWToSLock(&vcp->lock);
673 code = afs_CheckCode(code, &treq, 54);
674 ReleaseSharedLock(&vcp->lock);
684 #if !defined(AFS_LINUX24_ENV)
685 /* Not allowed to directly read a directory. */
687 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
695 struct file_operations afs_dir_fops = {
696 #if !defined(AFS_LINUX24_ENV)
697 .read = afs_linux_dir_read,
698 .lock = afs_linux_lock,
699 .fsync = afs_linux_fsync,
701 .read = generic_read_dir,
703 .readdir = afs_linux_readdir,
704 #ifdef HAVE_UNLOCKED_IOCTL
705 .unlocked_ioctl = afs_unlocked_xioctl,
709 #ifdef HAVE_COMPAT_IOCTL
710 .compat_ioctl = afs_unlocked_xioctl,
712 .open = afs_linux_open,
713 .release = afs_linux_release,
716 struct file_operations afs_file_fops = {
717 .read = afs_linux_read,
718 .write = afs_linux_write,
719 #ifdef GENERIC_FILE_AIO_READ
720 .aio_read = generic_file_aio_read,
721 .aio_write = generic_file_aio_write,
723 #ifdef HAVE_UNLOCKED_IOCTL
724 .unlocked_ioctl = afs_unlocked_xioctl,
728 #ifdef HAVE_COMPAT_IOCTL
729 .compat_ioctl = afs_unlocked_xioctl,
731 .mmap = afs_linux_mmap,
732 .open = afs_linux_open,
733 .flush = afs_linux_flush,
734 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
735 .sendfile = generic_file_sendfile,
737 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
738 .splice_write = generic_file_splice_write,
739 .splice_read = generic_file_splice_read,
741 .release = afs_linux_release,
742 .fsync = afs_linux_fsync,
743 .lock = afs_linux_lock,
744 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
745 .flock = afs_linux_flock,
750 /**********************************************************************
751 * AFS Linux dentry operations
752 **********************************************************************/
754 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
755 * that has its mvid (parent dir's fid) pointer set to the wrong directory
756 * due to being mounted in multiple points at once. If so, check_bad_parent()
757 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
758 * dotdotfid and mtpoint fid members.
760 * dp - dentry to be checked.
764 * This dentry's vcache's mvid will be set to the correct parent directory's
766 * This root vnode's volume will have its dotdotfid and mtpoint fids set
767 * to the correct parent and mountpoint fids.
771 check_bad_parent(struct dentry *dp)
774 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
775 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
777 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
780 /* force a lookup, so vcp->mvid is fixed up */
781 afs_lookup(pvc, dp->d_name.name, &avc, credp);
782 if (!avc || vcp != avc) { /* bad, very bad.. */
783 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
784 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
785 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
786 ICL_TYPE_POINTER, dp);
789 AFS_RELE(AFSTOV(avc));
796 /* afs_linux_revalidate
797 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
800 afs_linux_revalidate(struct dentry *dp)
803 struct vcache *vcp = VTOAFS(dp->d_inode);
807 #ifdef AFS_LINUX24_ENV
813 /* Make this a fast path (no crref), since it's called so often. */
814 if (vcp->f.states & CStatd) {
816 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
817 check_bad_parent(dp); /* check and correct mvid */
820 #ifdef AFS_LINUX24_ENV
828 code = afs_getattr(vcp, &vattr, credp);
830 afs_fill_inode(AFSTOV(vcp), &vattr);
833 #ifdef AFS_LINUX24_ENV
834 maybe_unlock_kernel();
841 #if defined(AFS_LINUX26_ENV)
843 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
845 int err = afs_linux_revalidate(dentry);
847 generic_fillattr(dentry->d_inode, stat);
853 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
854 * In kernels 2.2.10 and above, we are passed an additional flags var which
855 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
856 * we are advised to follow the entry if it is a link or to make sure that
857 * it is a directory. But since the kernel itself checks these possibilities
858 * later on, we shouldn't have to do it until later. Perhaps in the future..
861 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
862 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
863 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
865 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
868 afs_linux_dentry_revalidate(struct dentry *dp)
872 cred_t *credp = NULL;
873 struct vcache *vcp, *pvcp, *tvc = NULL;
875 struct afs_fakestat_state fakestate;
877 #ifdef AFS_LINUX24_ENV
881 afs_InitFakeStat(&fakestate);
885 vcp = VTOAFS(dp->d_inode);
886 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
888 if (vcp == afs_globalVp)
891 if (vcp->mvstat == 1) { /* mount point */
892 if (vcp->mvid && (vcp->f.states & CMValid)) {
895 struct vrequest treq;
898 code = afs_InitReq(&treq, credp);
900 #ifdef AFS_DARWIN_ENV
901 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
902 (strcmp(dp->d_name.name, "Contents") == 0) ||
904 (strcmp(dp->d_name.name, ".directory") == 0)) {
908 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
910 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
911 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
912 /* a mount point, not yet replaced by its directory */
917 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
918 check_bad_parent(dp); /* check and correct mvid */
921 /* If the last looker changes, we should make sure the current
922 * looker still has permission to examine this file. This would
923 * always require a crref() which would be "slow".
925 if (vcp->last_looker != treq.uid) {
926 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
929 vcp->last_looker = treq.uid;
933 /* If the parent's DataVersion has changed or the vnode
934 * is longer valid, we need to do a full lookup. VerifyVCache
935 * isn't enough since the vnode may have been renamed.
938 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
941 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
942 if (!tvc || tvc != vcp)
945 if (afs_getattr(vcp, &vattr, credp))
948 vattr2inode(AFSTOV(vcp), &vattr);
949 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
952 /* should we always update the attributes at this point? */
953 /* unlikely--the vcache entry hasn't changed */
957 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
958 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
962 /* No change in parent's DataVersion so this negative
963 * lookup is still valid. BUT, if a server is down a
964 * negative lookup can result so there should be a
965 * liftime as well. For now, always expire.
978 afs_PutFakeStat(&fakestate);
984 shrink_dcache_parent(dp);
987 #ifdef AFS_LINUX24_ENV
988 maybe_unlock_kernel();
993 if (have_submounts(dp))
1001 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1003 struct vcache *vcp = VTOAFS(ip);
1006 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1007 (void) afs_InactiveVCache(vcp, NULL);
1010 #ifdef DCACHE_NFSFS_RENAMED
1011 #ifdef AFS_LINUX26_ENV
1012 spin_lock(&dp->d_lock);
1014 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1015 #ifdef AFS_LINUX26_ENV
1016 spin_unlock(&dp->d_lock);
1024 afs_dentry_delete(struct dentry *dp)
1026 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1027 return 1; /* bad inode? */
1032 struct dentry_operations afs_dentry_operations = {
1033 .d_revalidate = afs_linux_dentry_revalidate,
1034 .d_delete = afs_dentry_delete,
1035 .d_iput = afs_dentry_iput,
1038 /**********************************************************************
1039 * AFS Linux inode operations
1040 **********************************************************************/
1044 * Merely need to set enough of vattr to get us through the create. Note
1045 * that the higher level code (open_namei) will take care of any tuncation
1046 * explicitly. Exclusive open is also taken care of in open_namei.
1048 * name is in kernel space at this point.
1051 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1052 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1053 struct nameidata *nd)
1055 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1059 cred_t *credp = crref();
1060 const char *name = dp->d_name.name;
1065 vattr.va_mode = mode;
1066 vattr.va_type = mode & S_IFMT;
1068 #if defined(AFS_LINUX26_ENV)
1069 maybe_lock_kernel();
1072 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1076 struct inode *ip = AFSTOV(vcp);
1078 afs_getattr(vcp, &vattr, credp);
1079 afs_fill_inode(ip, &vattr);
1080 insert_inode_hash(ip);
1081 dp->d_op = &afs_dentry_operations;
1082 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1083 d_instantiate(dp, ip);
1087 #if defined(AFS_LINUX26_ENV)
1088 maybe_unlock_kernel();
1094 /* afs_linux_lookup */
1095 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1096 static struct dentry *
1097 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1098 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1099 struct nameidata *nd)
1101 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1105 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1108 cred_t *credp = crref();
1109 struct vcache *vcp = NULL;
1110 const char *comp = dp->d_name.name;
1111 struct inode *ip = NULL;
1112 #if defined(AFS_LINUX26_ENV)
1113 struct dentry *newdp = NULL;
1117 #if defined(AFS_LINUX26_ENV)
1118 maybe_lock_kernel();
1121 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1127 afs_getattr(vcp, &vattr, credp);
1128 afs_fill_inode(ip, &vattr);
1130 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1131 hlist_unhashed(&ip->i_hash)
1132 #elif defined(AFS_LINUX26_ENV)
1133 ip->i_hash.pprev == NULL
1135 ip->i_hash.prev == NULL
1138 insert_inode_hash(ip);
1140 dp->d_op = &afs_dentry_operations;
1141 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1144 #if defined(AFS_LINUX24_ENV)
1145 if (ip && S_ISDIR(ip->i_mode)) {
1146 struct dentry *alias;
1148 /* Try to invalidate an existing alias in favor of our new one */
1149 alias = d_find_alias(ip);
1150 #if defined(AFS_LINUX26_ENV)
1151 /* But not if it's disconnected; then we want d_splice_alias below */
1152 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1156 if (d_invalidate(alias) == 0) {
1160 #if defined(AFS_LINUX26_ENV)
1169 #if defined(AFS_LINUX26_ENV)
1170 newdp = d_splice_alias(ip, dp);
1175 #if defined(AFS_LINUX26_ENV)
1176 maybe_unlock_kernel();
1180 /* It's ok for the file to not be found. That's noted by the caller by
1181 * seeing that the dp->d_inode field is NULL.
1183 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1184 #if defined(AFS_LINUX26_ENV)
1185 if (!code || code == ENOENT)
1191 else if ((code > 0) && (code <= MAX_ERRNO))
1192 return ERR_PTR(-code);
1194 return ERR_PTR(-EIO);
1203 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1206 cred_t *credp = crref();
1207 const char *name = newdp->d_name.name;
1208 struct inode *oldip = olddp->d_inode;
1210 /* If afs_link returned the vnode, we could instantiate the
1211 * dentry. Since it's not, we drop this one and do a new lookup.
1216 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1224 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1227 cred_t *credp = crref();
1228 const char *name = dp->d_name.name;
1229 struct vcache *tvc = VTOAFS(dp->d_inode);
1231 #if defined(AFS_LINUX26_ENV)
1232 maybe_lock_kernel();
1234 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1235 && !(tvc->f.states & CUnlinked)) {
1236 struct dentry *__dp;
1246 osi_FreeSmallSpace(__name);
1247 __name = afs_newname();
1250 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1254 } while (__dp->d_inode != NULL);
1257 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1259 tvc->mvid = (void *) __name;
1262 crfree(tvc->uncred);
1264 tvc->uncred = credp;
1265 tvc->f.states |= CUnlinked;
1266 #ifdef DCACHE_NFSFS_RENAMED
1267 #ifdef AFS_LINUX26_ENV
1268 spin_lock(&dp->d_lock);
1270 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1271 #ifdef AFS_LINUX26_ENV
1272 spin_unlock(&dp->d_lock);
1276 osi_FreeSmallSpace(__name);
1281 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1290 code = afs_remove(VTOAFS(dip), name, credp);
1295 #if defined(AFS_LINUX26_ENV)
1296 maybe_unlock_kernel();
1304 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1307 cred_t *credp = crref();
1309 const char *name = dp->d_name.name;
1311 /* If afs_symlink returned the vnode, we could instantiate the
1312 * dentry. Since it's not, we drop this one and do a new lookup.
1318 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1325 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1328 cred_t *credp = crref();
1329 struct vcache *tvcp = NULL;
1331 const char *name = dp->d_name.name;
1333 #if defined(AFS_LINUX26_ENV)
1334 maybe_lock_kernel();
1337 vattr.va_mask = ATTR_MODE;
1338 vattr.va_mode = mode;
1340 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1343 struct inode *ip = AFSTOV(tvcp);
1345 afs_getattr(tvcp, &vattr, credp);
1346 afs_fill_inode(ip, &vattr);
1348 dp->d_op = &afs_dentry_operations;
1349 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1350 d_instantiate(dp, ip);
1354 #if defined(AFS_LINUX26_ENV)
1355 maybe_unlock_kernel();
1362 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1365 cred_t *credp = crref();
1366 const char *name = dp->d_name.name;
1368 /* locking kernel conflicts with glock? */
1371 code = afs_rmdir(VTOAFS(dip), name, credp);
1374 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1375 * that failed because a directory is not empty. So, we map
1376 * EEXIST to ENOTEMPTY on linux.
1378 if (code == EEXIST) {
1392 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1393 struct inode *newip, struct dentry *newdp)
1396 cred_t *credp = crref();
1397 const char *oldname = olddp->d_name.name;
1398 const char *newname = newdp->d_name.name;
1399 struct dentry *rehash = NULL;
1401 #if defined(AFS_LINUX26_ENV)
1402 /* Prevent any new references during rename operation. */
1403 maybe_lock_kernel();
1405 if (!d_unhashed(newdp)) {
1410 if (!list_empty(&newdp->d_hash)) {
1416 #if defined(AFS_LINUX24_ENV)
1417 if (atomic_read(&olddp->d_count) > 1)
1418 shrink_dcache_parent(olddp);
1422 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1426 olddp->d_time = 0; /* force to revalidate */
1431 #if defined(AFS_LINUX26_ENV)
1432 maybe_unlock_kernel();
1440 /* afs_linux_ireadlink
1441 * Internal readlink which can return link contents to user or kernel space.
1442 * Note that the buffer is NOT supposed to be null-terminated.
1445 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1448 cred_t *credp = crref();
1452 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1453 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1457 return maxlen - tuio.uio_resid;
1462 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1463 /* afs_linux_readlink
1464 * Fill target (which is in user space) with contents of symlink.
1467 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1470 struct inode *ip = dp->d_inode;
1473 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1479 /* afs_linux_follow_link
1480 * a file system dependent link following routine.
1482 #if defined(AFS_LINUX24_ENV)
1483 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1488 name = osi_Alloc(PATH_MAX);
1494 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1502 code = vfs_follow_link(nd, name);
1505 osi_Free(name, PATH_MAX);
1510 #else /* !defined(AFS_LINUX24_ENV) */
1512 static struct dentry *
1513 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1514 unsigned int follow)
1522 name = osi_Alloc(PATH_MAX + 1);
1526 return ERR_PTR(-EIO);
1529 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1534 if (code < -MAX_ERRNO)
1535 res = ERR_PTR(-EIO);
1537 res = ERR_PTR(code);
1540 res = lookup_dentry(name, basep, follow);
1544 osi_Free(name, PATH_MAX + 1);
1548 #endif /* AFS_LINUX24_ENV */
1549 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1551 #if defined(AFS_CACHE_BYPASS)
1553 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1555 /* The kernel calls readpages before trying readpage, with a list of
1556 * pages. The readahead algorithm expands num_pages when it thinks
1557 * the application will benefit. Unlike readpage, the pages are not
1558 * necessarily allocated. If we do not a) allocate required pages and
1559 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1560 * for each required page (and the page will be pre-allocated) */
1563 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1564 struct list_head *page_list, unsigned num_pages)
1569 struct iovec* iovecp;
1570 struct nocache_read_request *ancr;
1571 struct page *pp, *ppt;
1572 struct pagevec lrupv;
1576 struct inode *ip = FILE_INODE(fp);
1577 struct vcache *avc = VTOAFS(ip);
1578 afs_int32 bypasscache = 0; /* bypass for this read */
1579 afs_int32 base_index = 0;
1580 afs_int32 page_count = 0;
1585 switch(cache_bypass_strategy) {
1586 case NEVER_BYPASS_CACHE:
1588 case ALWAYS_BYPASS_CACHE:
1591 case LARGE_FILES_BYPASS_CACHE:
1592 if(i_size_read(ip) > cache_bypass_threshold) {
1600 /* In the new incarnation of selective caching, a file's caching policy
1601 * can change, eg because file size exceeds threshold, etc. */
1602 trydo_cache_transition(avc, credp, bypasscache);
1605 while(!list_empty(page_list)) {
1606 pp = list_entry(page_list->prev, struct page, lru);
1611 /* background thread must free: iovecp, auio, ancr */
1612 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1614 auio = osi_Alloc(sizeof(uio_t));
1615 auio->uio_iov = iovecp;
1616 auio->uio_iovcnt = num_pages;
1617 auio->uio_flag = UIO_READ;
1618 auio->uio_seg = AFS_UIOSYS;
1619 auio->uio_resid = num_pages * PAGE_SIZE;
1621 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1623 ancr->offset = auio->uio_offset;
1624 ancr->length = auio->uio_resid;
1626 pagevec_init(&lrupv, 0);
1628 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1630 if(list_empty(page_list))
1633 pp = list_entry(page_list->prev, struct page, lru);
1634 /* If we allocate a page and don't remove it from page_list,
1635 * the page cache gets upset. */
1637 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1638 if(pp->index > isize) {
1645 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1646 auio->uio_offset = offset;
1647 base_index = pp->index;
1649 iovecp[page_ix].iov_len = PAGE_SIZE;
1650 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1651 if(base_index != pp->index) {
1654 page_cache_release(pp);
1655 iovecp[page_ix].iov_base = (void *) 0;
1663 page_cache_release(pp);
1664 iovecp[page_ix].iov_base = (void *) 0;
1667 if(!PageLocked(pp)) {
1671 /* save the page for background map */
1672 iovecp[page_ix].iov_base = (void*) pp;
1674 /* and put it on the LRU cache */
1675 if (!pagevec_add(&lrupv, pp))
1676 __pagevec_lru_add(&lrupv);
1680 /* If there were useful pages in the page list, make sure all pages
1681 * are in the LRU cache, then schedule the read */
1683 pagevec_lru_add(&lrupv);
1684 code = afs_ReadNoCache(avc, ancr, credp);
1686 /* If there is nothing for the background thread to handle,
1687 * it won't be freeing the things that we never gave it */
1688 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1689 osi_Free(auio, sizeof(uio_t));
1690 osi_Free(ancr, sizeof(struct nocache_read_request));
1692 /* we do not flush, release, or unmap pages--that will be
1693 * done for us by the background thread as each page comes in
1694 * from the fileserver */
1701 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1702 #endif /* defined(AFS_CACHE_BYPASS */
1705 /* afs_linux_readpage
1706 * all reads come through here. A strategy-like read call.
1709 afs_linux_readpage(struct file *fp, struct page *pp)
1712 cred_t *credp = crref();
1713 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1715 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1717 ulong address = afs_linux_page_address(pp);
1718 afs_offs_t offset = pageoff(pp);
1720 #if defined(AFS_CACHE_BYPASS)
1721 afs_int32 bypasscache = 0; /* bypass for this read */
1722 struct nocache_read_request *ancr;
1726 struct iovec *iovecp;
1727 struct inode *ip = FILE_INODE(fp);
1728 afs_int32 cnt = page_count(pp);
1729 struct vcache *avc = VTOAFS(ip);
1731 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1735 atomic_add(1, &pp->count);
1736 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1737 clear_bit(PG_error, &pp->flags);
1739 #if defined(AFS_CACHE_BYPASS)
1740 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1741 /* If the page is past the end of the file, skip it */
1742 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1743 if(pp->index > isize) {
1750 /* if bypasscache, receiver frees, else we do */
1751 auio = osi_Alloc(sizeof(uio_t));
1752 iovecp = osi_Alloc(sizeof(struct iovec));
1754 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1757 #if defined(AFS_CACHE_BYPASS)
1759 switch(cache_bypass_strategy) {
1760 case NEVER_BYPASS_CACHE:
1762 case ALWAYS_BYPASS_CACHE:
1765 case LARGE_FILES_BYPASS_CACHE:
1766 if(i_size_read(ip) > cache_bypass_threshold) {
1774 /* In the new incarnation of selective caching, a file's caching policy
1775 * can change, eg because file size exceeds threshold, etc. */
1776 trydo_cache_transition(avc, credp, bypasscache);
1781 /* save the page for background map */
1782 auio->uio_iov->iov_base = (void*) pp;
1783 /* the background thread will free this */
1784 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1786 ancr->offset = offset;
1787 ancr->length = PAGE_SIZE;
1789 maybe_lock_kernel();
1790 code = afs_ReadNoCache(avc, ancr, credp);
1791 maybe_unlock_kernel();
1793 goto done; /* skips release page, doing it in bg thread */
1797 #ifdef AFS_LINUX24_ENV
1798 maybe_lock_kernel();
1802 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 */
1804 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1806 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1807 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1809 AFS_DISCON_UNLOCK();
1811 #ifdef AFS_LINUX24_ENV
1812 maybe_unlock_kernel();
1815 /* XXX valid for no-cache also? Check last bits of files... :)
1816 * Cognate code goes in afs_NoCacheFetchProc. */
1817 if (auio->uio_resid) /* zero remainder of page */
1818 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1821 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1822 flush_dcache_page(pp);
1823 SetPageUptodate(pp);
1825 set_bit(PG_uptodate, &pp->flags);
1829 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1833 clear_bit(PG_locked, &pp->flags);
1838 #if defined(AFS_CACHE_BYPASS)
1840 /* do not call afs_GetDCache if cache is bypassed */
1846 /* free if not bypassing cache */
1847 osi_Free(auio, sizeof(uio_t));
1848 osi_Free(iovecp, sizeof(struct iovec));
1850 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1852 struct vrequest treq;
1855 code = afs_InitReq(&treq, credp);
1856 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1857 tdc = afs_FindDCache(avc, offset);
1859 if (!(tdc->mflags & DFNextStarted))
1860 afs_PrefetchChunk(avc, tdc, credp, &treq);
1863 ReleaseWriteLock(&avc->lock);
1874 #if defined(AFS_LINUX24_ENV)
1876 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1877 unsigned long offset, unsigned int count)
1879 struct vcache *vcp = VTOAFS(ip);
1888 buffer = kmap(pp) + offset;
1889 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1892 maybe_lock_kernel();
1894 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1895 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1896 ICL_TYPE_INT32, 99999);
1898 ObtainWriteLock(&vcp->lock, 532);
1899 if (vcp->f.states & CPageWrite) {
1900 ReleaseWriteLock(&vcp->lock);
1902 maybe_unlock_kernel();
1905 #ifdef AFS_LINUX26_ENV
1906 #if defined(WRITEPAGE_ACTIVATE)
1907 return WRITEPAGE_ACTIVATE;
1909 return AOP_WRITEPAGE_ACTIVATE;
1912 /* should mark it dirty? */
1916 vcp->f.states |= CPageWrite;
1917 ReleaseWriteLock(&vcp->lock);
1919 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1921 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1923 i_size_write(ip, vcp->f.m.Length);
1924 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1926 ObtainWriteLock(&vcp->lock, 533);
1928 struct vrequest treq;
1930 if (!afs_InitReq(&treq, credp))
1931 code = afs_DoPartialWrite(vcp, &treq);
1933 code = code ? -code : count - tuio.uio_resid;
1935 vcp->f.states &= ~CPageWrite;
1936 ReleaseWriteLock(&vcp->lock);
1938 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1939 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1940 ICL_TYPE_INT32, code);
1943 maybe_unlock_kernel();
1952 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1953 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1955 afs_linux_writepage(struct page *pp)
1958 struct address_space *mapping = pp->mapping;
1959 struct inode *inode;
1960 unsigned long end_index;
1961 unsigned offset = PAGE_CACHE_SIZE;
1964 #if defined(AFS_LINUX26_ENV)
1965 if (PageReclaim(pp)) {
1966 # if defined(WRITEPAGE_ACTIVATE)
1967 return WRITEPAGE_ACTIVATE;
1969 return AOP_WRITEPAGE_ACTIVATE;
1973 if (PageLaunder(pp)) {
1974 return(fail_writepage(pp));
1978 inode = (struct inode *)mapping->host;
1979 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
1982 if (pp->index < end_index)
1984 /* things got complicated... */
1985 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1986 /* OK, are we completely out? */
1987 if (pp->index >= end_index + 1 || !offset)
1990 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1991 SetPageUptodate(pp);
1992 #if defined(WRITEPAGE_ACTIVATE)
1993 if ( status != WRITEPAGE_ACTIVATE )
1995 if ( status != AOP_WRITEPAGE_ACTIVATE )
1998 if (status == offset)
2004 /* afs_linux_updatepage
2005 * What one would have thought was writepage - write dirty page to file.
2006 * Called from generic_file_write. buffer is still in user space. pagep
2007 * has been filled in with old data if we're updating less than a page.
2010 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2011 unsigned int count, int sync)
2013 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2014 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2020 set_bit(PG_locked, &pp->flags);
2025 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2026 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2027 ICL_TYPE_INT32, 99999);
2028 setup_uio(&tuio, &iovec, page_addr + offset,
2029 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2032 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2034 i_size_write(ip, vcp->f.m.Length);
2035 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2038 struct vrequest treq;
2040 ObtainWriteLock(&vcp->lock, 533);
2041 vcp->f.m.Date = osi_Time(); /* set modification time */
2042 if (!afs_InitReq(&treq, credp))
2043 code = afs_DoPartialWrite(vcp, &treq);
2044 ReleaseWriteLock(&vcp->lock);
2047 code = code ? -code : count - tuio.uio_resid;
2048 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2049 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2050 ICL_TYPE_INT32, code);
2052 AFS_DISCON_UNLOCK();
2056 clear_bit(PG_locked, &pp->flags);
2061 /* afs_linux_permission
2062 * Check access rights - returns error if can't check or permission denied.
2065 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2066 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2068 afs_linux_permission(struct inode *ip, int mode)
2072 cred_t *credp = crref();
2076 if (mode & MAY_EXEC)
2078 if (mode & MAY_READ)
2080 if (mode & MAY_WRITE)
2082 code = afs_access(VTOAFS(ip), tmp, credp);
2089 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2091 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2096 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2097 offset, to - offset);
2098 #if !defined(AFS_LINUX26_ENV)
2106 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2109 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2110 call kmap directly instead of relying on us to do it */
2111 #if !defined(AFS_LINUX26_ENV)
2118 #if defined(HAVE_WRITE_BEGIN)
2120 afs_linux_write_end(struct file *file, struct address_space *mapping,
2121 loff_t pos, unsigned len, unsigned copied,
2122 struct page *page, void *fsdata)
2125 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2126 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2128 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2131 page_cache_release(page);
2136 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2137 loff_t pos, unsigned len, unsigned flags,
2138 struct page **pagep, void **fsdata)
2141 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2142 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2143 page = grab_cache_page_write_begin(mapping, index, flags);
2145 page = __grab_cache_page(mapping, index);
2154 static struct inode_operations afs_file_iops = {
2155 #if defined(AFS_LINUX26_ENV)
2156 .permission = afs_linux_permission,
2157 .getattr = afs_linux_getattr,
2158 .setattr = afs_notify_change,
2159 #elif defined(AFS_LINUX24_ENV)
2160 .permission = afs_linux_permission,
2161 .revalidate = afs_linux_revalidate,
2162 .setattr = afs_notify_change,
2164 .default_file_ops = &afs_file_fops,
2165 .readpage = afs_linux_readpage,
2166 .revalidate = afs_linux_revalidate,
2167 .updatepage = afs_linux_updatepage,
2171 #if defined(AFS_LINUX24_ENV)
2172 static struct address_space_operations afs_file_aops = {
2173 .readpage = afs_linux_readpage,
2174 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2175 .readpages = afs_linux_readpages,
2177 .writepage = afs_linux_writepage,
2178 #if defined (HAVE_WRITE_BEGIN)
2179 .write_begin = afs_linux_write_begin,
2180 .write_end = afs_linux_write_end,
2182 .commit_write = afs_linux_commit_write,
2183 .prepare_write = afs_linux_prepare_write,
2189 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2190 * by what sort of operation is allowed.....
2193 static struct inode_operations afs_dir_iops = {
2194 #if !defined(AFS_LINUX24_ENV)
2195 .default_file_ops = &afs_dir_fops,
2197 .setattr = afs_notify_change,
2199 .create = afs_linux_create,
2200 .lookup = afs_linux_lookup,
2201 .link = afs_linux_link,
2202 .unlink = afs_linux_unlink,
2203 .symlink = afs_linux_symlink,
2204 .mkdir = afs_linux_mkdir,
2205 .rmdir = afs_linux_rmdir,
2206 .rename = afs_linux_rename,
2207 #if defined(AFS_LINUX26_ENV)
2208 .getattr = afs_linux_getattr,
2210 .revalidate = afs_linux_revalidate,
2212 .permission = afs_linux_permission,
2215 /* We really need a separate symlink set of ops, since do_follow_link()
2216 * determines if it _is_ a link by checking if the follow_link op is set.
2218 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2220 afs_symlink_filler(struct file *file, struct page *page)
2222 struct inode *ip = (struct inode *)page->mapping->host;
2223 char *p = (char *)kmap(page);
2226 maybe_lock_kernel();
2228 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2233 p[code] = '\0'; /* null terminate? */
2234 maybe_unlock_kernel();
2236 SetPageUptodate(page);
2242 maybe_unlock_kernel();
2250 static struct address_space_operations afs_symlink_aops = {
2251 .readpage = afs_symlink_filler
2253 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2255 static struct inode_operations afs_symlink_iops = {
2256 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2257 .readlink = page_readlink,
2258 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2259 .follow_link = page_follow_link,
2261 .follow_link = page_follow_link_light,
2262 .put_link = page_put_link,
2264 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2265 .readlink = afs_linux_readlink,
2266 .follow_link = afs_linux_follow_link,
2267 #if !defined(AFS_LINUX24_ENV)
2268 .permission = afs_linux_permission,
2269 .revalidate = afs_linux_revalidate,
2271 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2272 #if defined(AFS_LINUX24_ENV)
2273 .setattr = afs_notify_change,
2278 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2282 vattr2inode(ip, vattr);
2284 #if defined(AFS_LINUX26_ENV)
2285 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2287 /* Reset ops if symlink or directory. */
2288 if (S_ISREG(ip->i_mode)) {
2289 ip->i_op = &afs_file_iops;
2290 #if defined(AFS_LINUX24_ENV)
2291 ip->i_fop = &afs_file_fops;
2292 ip->i_data.a_ops = &afs_file_aops;
2295 } else if (S_ISDIR(ip->i_mode)) {
2296 ip->i_op = &afs_dir_iops;
2297 #if defined(AFS_LINUX24_ENV)
2298 ip->i_fop = &afs_dir_fops;
2301 } else if (S_ISLNK(ip->i_mode)) {
2302 ip->i_op = &afs_symlink_iops;
2303 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2304 ip->i_data.a_ops = &afs_symlink_aops;
2305 ip->i_mapping = &ip->i_data;