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 /* This function converts a positive error code from AFS into a negative
85 * code suitable for passing into the Linux VFS layer. It checks that the
86 * error code is within the permissable bounds for the ERR_PTR mechanism.
88 * _All_ error codes which come from the AFS layer should be passed through
89 * this function before being returned to the kernel.
92 static inline int afs_convert_code(int code) {
93 if ((code >= 0) && (code <= MAX_ERRNO))
100 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
103 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
104 cred_t *credp = crref();
105 struct vrequest treq;
106 afs_size_t isize, offindex;
108 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
109 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
111 /* get a validated vcache entry */
112 code = afs_InitReq(&treq, credp);
114 code = afs_VerifyVCache(vcp, &treq);
117 code = afs_convert_code(code);
119 #if defined(AFS_CACHE_BYPASS)
120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
121 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
122 offindex = *offp >> PAGE_CACHE_SHIFT;
123 if(offindex > isize) {
129 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
132 code = do_sync_read(fp, buf, count, offp);
134 code = generic_file_read(fp, buf, count, offp);
139 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
140 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
149 /* Now we have integrated VM for writes as well as reads. generic_file_write
150 * also takes care of re-positioning the pointer if file is open in append
151 * mode. Call fake open/close to ensure we do writes of core dumps.
154 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
157 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
158 struct vrequest treq;
159 cred_t *credp = crref();
163 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
164 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
165 (fp->f_flags & O_APPEND) ? 99998 : 99999);
168 /* get a validated vcache entry */
169 code = (ssize_t) afs_InitReq(&treq, credp);
171 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
173 ObtainWriteLock(&vcp->lock, 529);
175 ReleaseWriteLock(&vcp->lock);
177 code = afs_convert_code(code);
181 code = do_sync_write(fp, buf, count, offp);
183 code = generic_file_write(fp, buf, count, offp);
188 ObtainWriteLock(&vcp->lock, 530);
189 afs_FakeClose(vcp, credp);
190 ReleaseWriteLock(&vcp->lock);
192 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
193 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
201 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
203 /* This is a complete rewrite of afs_readdir, since we can make use of
204 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
205 * handling and use of bulkstats will need to be reflected here as well.
208 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
210 struct vcache *avc = VTOAFS(FILE_INODE(fp));
211 struct vrequest treq;
212 register struct dcache *tdc;
219 afs_size_t origOffset, tlen;
220 cred_t *credp = crref();
221 struct afs_fakestat_state fakestat;
223 #if defined(AFS_LINUX26_ENV)
227 AFS_STATCNT(afs_readdir);
229 code = afs_InitReq(&treq, credp);
232 code = afs_convert_code(code);
236 afs_InitFakeStat(&fakestat);
237 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
239 code = afs_convert_code(code);
243 /* update the cache entry */
245 code = afs_VerifyVCache(avc, &treq);
247 code = afs_convert_code(code);
251 /* get a reference to the entire directory */
252 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
258 ObtainSharedLock(&avc->lock, 810);
259 UpgradeSToWLock(&avc->lock, 811);
260 ObtainReadLock(&tdc->lock);
262 * Make sure that the data in the cache is current. There are two
263 * cases we need to worry about:
264 * 1. The cache data is being fetched by another process.
265 * 2. The cache data is no longer valid
267 while ((avc->f.states & CStatd)
268 && (tdc->dflags & DFFetching)
269 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
270 ReleaseReadLock(&tdc->lock);
271 ReleaseSharedLock(&avc->lock);
272 afs_osi_Sleep(&tdc->validPos);
273 ObtainSharedLock(&avc->lock, 812);
274 ObtainReadLock(&tdc->lock);
276 if (!(avc->f.states & CStatd)
277 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
278 ReleaseReadLock(&tdc->lock);
279 ReleaseSharedLock(&avc->lock);
284 /* Set the readdir-in-progress flag, and downgrade the lock
285 * to shared so others will be able to acquire a read lock.
287 avc->f.states |= CReadDir;
288 avc->dcreaddir = tdc;
289 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
290 ConvertWToSLock(&avc->lock);
292 /* Fill in until we get an error or we're done. This implementation
293 * takes an offset in units of blobs, rather than bytes.
296 offset = (int) fp->f_pos;
298 dirpos = BlobScan(tdc, offset);
302 de = afs_dir_GetBlob(tdc, dirpos);
306 ino = afs_calc_inum (avc->f.fid.Fid.Volume, ntohl(de->fid.vnode));
309 len = strlen(de->name);
311 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
312 (unsigned long)&tdc->f.inode, dirpos);
313 DRelease((struct buffer *) de, 0);
314 ReleaseSharedLock(&avc->lock);
320 /* filldir returns -EINVAL when the buffer is full. */
321 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
323 unsigned int type = DT_UNKNOWN;
324 struct VenusFid afid;
327 afid.Cell = avc->f.fid.Cell;
328 afid.Fid.Volume = avc->f.fid.Fid.Volume;
329 afid.Fid.Vnode = ntohl(de->fid.vnode);
330 afid.Fid.Unique = ntohl(de->fid.vunique);
331 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
333 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
336 } else if (((tvc->f.states) & (CStatd | CTruth))) {
337 /* CTruth will be set if the object has
342 else if (vtype == VREG)
344 /* Don't do this until we're sure it can't be a mtpt */
345 /* else if (vtype == VLNK)
347 /* what other types does AFS support? */
349 /* clean up from afs_FindVCache */
353 * If this is NFS readdirplus, then the filler is going to
354 * call getattr on this inode, which will deadlock if we're
358 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
362 code = (*filldir) (dirbuf, de->name, len, offset, ino);
364 DRelease((struct buffer *)de, 0);
367 offset = dirpos + 1 + ((len + 16) >> 5);
369 /* If filldir didn't fill in the last one this is still pointing to that
372 fp->f_pos = (loff_t) offset;
374 ReleaseReadLock(&tdc->lock);
376 UpgradeSToWLock(&avc->lock, 813);
377 avc->f.states &= ~CReadDir;
379 avc->readdir_pid = 0;
380 ReleaseSharedLock(&avc->lock);
384 afs_PutFakeStat(&fakestat);
387 #if defined(AFS_LINUX26_ENV)
388 maybe_unlock_kernel();
394 /* in afs_pioctl.c */
395 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
398 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
399 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
401 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
408 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
410 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
411 cred_t *credp = crref();
412 struct vrequest treq;
416 #if defined(AFS_LINUX24_ENV)
417 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
418 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
419 vmap->vm_end - vmap->vm_start);
421 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
422 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
423 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
427 /* get a validated vcache entry */
428 code = afs_InitReq(&treq, credp);
432 code = afs_VerifyVCache(vcp, &treq);
436 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
439 code = generic_file_mmap(fp, vmap);
442 vcp->f.states |= CMAPPED;
450 code = afs_convert_code(code);
455 afs_linux_open(struct inode *ip, struct file *fp)
457 struct vcache *vcp = VTOAFS(ip);
458 cred_t *credp = crref();
461 #ifdef AFS_LINUX24_ENV
465 code = afs_open(&vcp, fp->f_flags, credp);
467 #ifdef AFS_LINUX24_ENV
468 maybe_unlock_kernel();
472 return afs_convert_code(code);
476 afs_linux_release(struct inode *ip, struct file *fp)
478 struct vcache *vcp = VTOAFS(ip);
479 cred_t *credp = crref();
482 #ifdef AFS_LINUX24_ENV
486 code = afs_close(vcp, fp->f_flags, credp);
488 #ifdef AFS_LINUX24_ENV
489 maybe_unlock_kernel();
493 return afs_convert_code(code);
497 #if defined(AFS_LINUX24_ENV)
498 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
500 afs_linux_fsync(struct file *fp, struct dentry *dp)
504 struct inode *ip = FILE_INODE(fp);
505 cred_t *credp = crref();
507 #ifdef AFS_LINUX24_ENV
511 code = afs_fsync(VTOAFS(ip), credp);
513 #ifdef AFS_LINUX24_ENV
514 maybe_unlock_kernel();
517 return afs_convert_code(code);
523 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
526 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
527 cred_t *credp = crref();
528 struct AFS_FLOCK flock;
529 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
530 struct file_lock conflict;
531 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
532 struct file_lock *conflict;
535 /* Convert to a lock format afs_lockctl understands. */
536 memset((char *)&flock, 0, sizeof(flock));
537 flock.l_type = flp->fl_type;
538 flock.l_pid = flp->fl_pid;
540 flock.l_start = flp->fl_start;
541 flock.l_len = flp->fl_end - flp->fl_start + 1;
543 /* Safe because there are no large files, yet */
544 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
545 if (cmd == F_GETLK64)
547 else if (cmd == F_SETLK64)
549 else if (cmd == F_SETLKW64)
551 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
554 code = afs_lockctl(vcp, &flock, cmd, credp);
557 #ifdef AFS_LINUX24_ENV
558 if ((code == 0 || flp->fl_type == F_UNLCK) &&
559 (cmd == F_SETLK || cmd == F_SETLKW)) {
560 # ifdef POSIX_LOCK_FILE_WAIT_ARG
561 code = posix_lock_file(fp, flp, 0);
563 flp->fl_flags &=~ FL_SLEEP;
564 code = posix_lock_file(fp, flp);
566 if (code && flp->fl_type != F_UNLCK) {
567 struct AFS_FLOCK flock2;
569 flock2.l_type = F_UNLCK;
571 afs_lockctl(vcp, &flock2, F_SETLK, credp);
575 /* If lockctl says there are no conflicting locks, then also check with the
576 * kernel, as lockctl knows nothing about byte range locks
578 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
579 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
580 if (posix_test_lock(fp, flp, &conflict)) {
581 locks_copy_lock(flp, &conflict);
582 flp->fl_type = F_UNLCK;
586 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
587 if ((conflict = posix_test_lock(fp, flp))) {
588 locks_copy_lock(flp, conflict);
589 flp->fl_type = F_UNLCK;
594 posix_test_lock(fp, flp);
595 /* If we found a lock in the kernel's structure, return it */
596 if (flp->fl_type != F_UNLCK) {
604 /* Convert flock back to Linux's file_lock */
605 flp->fl_type = flock.l_type;
606 flp->fl_pid = flock.l_pid;
607 flp->fl_start = flock.l_start;
608 flp->fl_end = flock.l_start + flock.l_len - 1;
611 return afs_convert_code(code);
614 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
616 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
618 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
619 cred_t *credp = crref();
620 struct AFS_FLOCK flock;
621 /* Convert to a lock format afs_lockctl understands. */
622 memset((char *)&flock, 0, sizeof(flock));
623 flock.l_type = flp->fl_type;
624 flock.l_pid = flp->fl_pid;
627 flock.l_len = OFFSET_MAX;
629 /* Safe because there are no large files, yet */
630 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
631 if (cmd == F_GETLK64)
633 else if (cmd == F_SETLK64)
635 else if (cmd == F_SETLKW64)
637 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
640 code = afs_lockctl(vcp, &flock, cmd, credp);
643 if ((code == 0 || flp->fl_type == F_UNLCK) &&
644 (cmd == F_SETLK || cmd == F_SETLKW)) {
645 flp->fl_flags &=~ FL_SLEEP;
646 code = flock_lock_file_wait(fp, flp);
647 if (code && flp->fl_type != F_UNLCK) {
648 struct AFS_FLOCK flock2;
650 flock2.l_type = F_UNLCK;
652 afs_lockctl(vcp, &flock2, F_SETLK, credp);
656 /* Convert flock back to Linux's file_lock */
657 flp->fl_type = flock.l_type;
658 flp->fl_pid = flock.l_pid;
661 return afs_convert_code(code);
666 * essentially the same as afs_fsync() but we need to get the return
667 * code for the sys_close() here, not afs_linux_release(), so call
668 * afs_StoreAllSegments() with AFS_LASTSTORE
671 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
672 afs_linux_flush(struct file *fp, fl_owner_t id)
674 afs_linux_flush(struct file *fp)
677 struct vrequest treq;
681 #if defined(AFS_CACHE_BYPASS)
687 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
695 vcp = VTOAFS(FILE_INODE(fp));
697 code = afs_InitReq(&treq, credp);
700 #if defined(AFS_CACHE_BYPASS)
701 /* If caching is bypassed for this file, or globally, just return 0 */
702 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
705 ObtainReadLock(&vcp->lock);
706 if(vcp->cachingStates & FCSBypass)
708 ReleaseReadLock(&vcp->lock);
711 /* future proof: don't rely on 0 return from afs_InitReq */
716 ObtainSharedLock(&vcp->lock, 535);
717 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
718 UpgradeSToWLock(&vcp->lock, 536);
719 if (!AFS_IS_DISCONNECTED) {
720 code = afs_StoreAllSegments(vcp,
722 AFS_SYNC | AFS_LASTSTORE);
724 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
726 ConvertWToSLock(&vcp->lock);
728 code = afs_CheckCode(code, &treq, 54);
729 ReleaseSharedLock(&vcp->lock);
736 return afs_convert_code(code);
739 #if !defined(AFS_LINUX24_ENV)
740 /* Not allowed to directly read a directory. */
742 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
750 struct file_operations afs_dir_fops = {
751 #if !defined(AFS_LINUX24_ENV)
752 .read = afs_linux_dir_read,
753 .lock = afs_linux_lock,
754 .fsync = afs_linux_fsync,
756 .read = generic_read_dir,
758 .readdir = afs_linux_readdir,
759 #ifdef HAVE_UNLOCKED_IOCTL
760 .unlocked_ioctl = afs_unlocked_xioctl,
764 #ifdef HAVE_COMPAT_IOCTL
765 .compat_ioctl = afs_unlocked_xioctl,
767 .open = afs_linux_open,
768 .release = afs_linux_release,
771 struct file_operations afs_file_fops = {
772 .read = afs_linux_read,
773 .write = afs_linux_write,
774 #ifdef GENERIC_FILE_AIO_READ
775 .aio_read = generic_file_aio_read,
776 .aio_write = generic_file_aio_write,
778 #ifdef HAVE_UNLOCKED_IOCTL
779 .unlocked_ioctl = afs_unlocked_xioctl,
783 #ifdef HAVE_COMPAT_IOCTL
784 .compat_ioctl = afs_unlocked_xioctl,
786 .mmap = afs_linux_mmap,
787 .open = afs_linux_open,
788 .flush = afs_linux_flush,
789 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
790 .sendfile = generic_file_sendfile,
792 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
793 .splice_write = generic_file_splice_write,
794 .splice_read = generic_file_splice_read,
796 .release = afs_linux_release,
797 .fsync = afs_linux_fsync,
798 .lock = afs_linux_lock,
799 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
800 .flock = afs_linux_flock,
805 /**********************************************************************
806 * AFS Linux dentry operations
807 **********************************************************************/
809 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
810 * that has its mvid (parent dir's fid) pointer set to the wrong directory
811 * due to being mounted in multiple points at once. If so, check_bad_parent()
812 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
813 * dotdotfid and mtpoint fid members.
815 * dp - dentry to be checked.
819 * This dentry's vcache's mvid will be set to the correct parent directory's
821 * This root vnode's volume will have its dotdotfid and mtpoint fids set
822 * to the correct parent and mountpoint fids.
826 check_bad_parent(struct dentry *dp)
829 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
830 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
832 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
835 /* force a lookup, so vcp->mvid is fixed up */
836 afs_lookup(pvc, dp->d_name.name, &avc, credp);
837 if (!avc || vcp != avc) { /* bad, very bad.. */
838 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
839 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
840 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
841 ICL_TYPE_POINTER, dp);
844 AFS_RELE(AFSTOV(avc));
851 /* afs_linux_revalidate
852 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
855 afs_linux_revalidate(struct dentry *dp)
858 struct vcache *vcp = VTOAFS(dp->d_inode);
862 #ifdef AFS_LINUX24_ENV
868 /* Make this a fast path (no crref), since it's called so often. */
869 if (vcp->f.states & CStatd) {
871 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
872 check_bad_parent(dp); /* check and correct mvid */
875 #ifdef AFS_LINUX24_ENV
883 code = afs_getattr(vcp, &vattr, credp);
885 afs_fill_inode(AFSTOV(vcp), &vattr);
888 #ifdef AFS_LINUX24_ENV
889 maybe_unlock_kernel();
893 return afs_convert_code(code);
896 #if defined(AFS_LINUX26_ENV)
898 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
900 int err = afs_linux_revalidate(dentry);
902 generic_fillattr(dentry->d_inode, stat);
908 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
909 * In kernels 2.2.10 and above, we are passed an additional flags var which
910 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
911 * we are advised to follow the entry if it is a link or to make sure that
912 * it is a directory. But since the kernel itself checks these possibilities
913 * later on, we shouldn't have to do it until later. Perhaps in the future..
916 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
917 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
918 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
920 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
923 afs_linux_dentry_revalidate(struct dentry *dp)
927 cred_t *credp = NULL;
928 struct vcache *vcp, *pvcp, *tvc = NULL;
930 struct afs_fakestat_state fakestate;
932 #ifdef AFS_LINUX24_ENV
936 afs_InitFakeStat(&fakestate);
940 vcp = VTOAFS(dp->d_inode);
941 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
943 if (vcp == afs_globalVp)
946 if (vcp->mvstat == 1) { /* mount point */
947 if (vcp->mvid && (vcp->f.states & CMValid)) {
950 struct vrequest treq;
953 code = afs_InitReq(&treq, credp);
955 #ifdef AFS_DARWIN_ENV
956 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
957 (strcmp(dp->d_name.name, "Contents") == 0) ||
959 (strcmp(dp->d_name.name, ".directory") == 0)) {
963 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
965 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
966 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
967 /* a mount point, not yet replaced by its directory */
972 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
973 check_bad_parent(dp); /* check and correct mvid */
976 /* If the last looker changes, we should make sure the current
977 * looker still has permission to examine this file. This would
978 * always require a crref() which would be "slow".
980 if (vcp->last_looker != treq.uid) {
981 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
984 vcp->last_looker = treq.uid;
988 /* If the parent's DataVersion has changed or the vnode
989 * is longer valid, we need to do a full lookup. VerifyVCache
990 * isn't enough since the vnode may have been renamed.
993 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
996 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
997 if (!tvc || tvc != vcp)
1000 if (afs_getattr(vcp, &vattr, credp))
1003 vattr2inode(AFSTOV(vcp), &vattr);
1004 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1007 /* should we always update the attributes at this point? */
1008 /* unlikely--the vcache entry hasn't changed */
1012 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
1013 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1017 /* No change in parent's DataVersion so this negative
1018 * lookup is still valid. BUT, if a server is down a
1019 * negative lookup can result so there should be a
1020 * liftime as well. For now, always expire.
1033 afs_PutFakeStat(&fakestate);
1039 shrink_dcache_parent(dp);
1042 #ifdef AFS_LINUX24_ENV
1043 maybe_unlock_kernel();
1048 if (have_submounts(dp))
1056 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1058 struct vcache *vcp = VTOAFS(ip);
1061 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1062 (void) afs_InactiveVCache(vcp, NULL);
1065 #ifdef DCACHE_NFSFS_RENAMED
1066 #ifdef AFS_LINUX26_ENV
1067 spin_lock(&dp->d_lock);
1069 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1070 #ifdef AFS_LINUX26_ENV
1071 spin_unlock(&dp->d_lock);
1079 afs_dentry_delete(struct dentry *dp)
1081 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1082 return 1; /* bad inode? */
1087 struct dentry_operations afs_dentry_operations = {
1088 .d_revalidate = afs_linux_dentry_revalidate,
1089 .d_delete = afs_dentry_delete,
1090 .d_iput = afs_dentry_iput,
1093 /**********************************************************************
1094 * AFS Linux inode operations
1095 **********************************************************************/
1099 * Merely need to set enough of vattr to get us through the create. Note
1100 * that the higher level code (open_namei) will take care of any tuncation
1101 * explicitly. Exclusive open is also taken care of in open_namei.
1103 * name is in kernel space at this point.
1106 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1107 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1108 struct nameidata *nd)
1110 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1114 cred_t *credp = crref();
1115 const char *name = dp->d_name.name;
1120 vattr.va_mode = mode;
1121 vattr.va_type = mode & S_IFMT;
1123 #if defined(AFS_LINUX26_ENV)
1124 maybe_lock_kernel();
1127 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1131 struct inode *ip = AFSTOV(vcp);
1133 afs_getattr(vcp, &vattr, credp);
1134 afs_fill_inode(ip, &vattr);
1135 insert_inode_hash(ip);
1136 dp->d_op = &afs_dentry_operations;
1137 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1138 d_instantiate(dp, ip);
1142 #if defined(AFS_LINUX26_ENV)
1143 maybe_unlock_kernel();
1146 return afs_convert_code(code);
1149 /* afs_linux_lookup */
1150 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1151 static struct dentry *
1152 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1153 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1154 struct nameidata *nd)
1156 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1160 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1163 cred_t *credp = crref();
1164 struct vcache *vcp = NULL;
1165 const char *comp = dp->d_name.name;
1166 struct inode *ip = NULL;
1167 #if defined(AFS_LINUX26_ENV)
1168 struct dentry *newdp = NULL;
1172 #if defined(AFS_LINUX26_ENV)
1173 maybe_lock_kernel();
1176 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1182 afs_getattr(vcp, &vattr, credp);
1183 afs_fill_inode(ip, &vattr);
1185 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1186 hlist_unhashed(&ip->i_hash)
1187 #elif defined(AFS_LINUX26_ENV)
1188 ip->i_hash.pprev == NULL
1190 ip->i_hash.prev == NULL
1193 insert_inode_hash(ip);
1195 dp->d_op = &afs_dentry_operations;
1196 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1199 #if defined(AFS_LINUX24_ENV)
1200 if (ip && S_ISDIR(ip->i_mode)) {
1201 struct dentry *alias;
1203 /* Try to invalidate an existing alias in favor of our new one */
1204 alias = d_find_alias(ip);
1205 #if defined(AFS_LINUX26_ENV)
1206 /* But not if it's disconnected; then we want d_splice_alias below */
1207 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1211 if (d_invalidate(alias) == 0) {
1215 #if defined(AFS_LINUX26_ENV)
1224 #if defined(AFS_LINUX26_ENV)
1225 newdp = d_splice_alias(ip, dp);
1230 #if defined(AFS_LINUX26_ENV)
1231 maybe_unlock_kernel();
1235 /* It's ok for the file to not be found. That's noted by the caller by
1236 * seeing that the dp->d_inode field is NULL.
1238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1239 #if defined(AFS_LINUX26_ENV)
1240 if (!code || code == ENOENT)
1247 return ERR_PTR(afs_convert_code(code));
1251 return afs_convert_code(code);
1256 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1259 cred_t *credp = crref();
1260 const char *name = newdp->d_name.name;
1261 struct inode *oldip = olddp->d_inode;
1263 /* If afs_link returned the vnode, we could instantiate the
1264 * dentry. Since it's not, we drop this one and do a new lookup.
1269 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1273 return afs_convert_code(code);
1277 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1280 cred_t *credp = crref();
1281 const char *name = dp->d_name.name;
1282 struct vcache *tvc = VTOAFS(dp->d_inode);
1284 #if defined(AFS_LINUX26_ENV)
1285 maybe_lock_kernel();
1287 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1288 && !(tvc->f.states & CUnlinked)) {
1289 struct dentry *__dp;
1299 osi_FreeSmallSpace(__name);
1300 __name = afs_newname();
1303 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1307 } while (__dp->d_inode != NULL);
1310 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1312 tvc->mvid = (void *) __name;
1315 crfree(tvc->uncred);
1317 tvc->uncred = credp;
1318 tvc->f.states |= CUnlinked;
1319 #ifdef DCACHE_NFSFS_RENAMED
1320 #ifdef AFS_LINUX26_ENV
1321 spin_lock(&dp->d_lock);
1323 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1324 #ifdef AFS_LINUX26_ENV
1325 spin_unlock(&dp->d_lock);
1329 osi_FreeSmallSpace(__name);
1334 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1343 code = afs_remove(VTOAFS(dip), name, credp);
1348 #if defined(AFS_LINUX26_ENV)
1349 maybe_unlock_kernel();
1352 return afs_convert_code(code);
1357 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1360 cred_t *credp = crref();
1362 const char *name = dp->d_name.name;
1364 /* If afs_symlink returned the vnode, we could instantiate the
1365 * dentry. Since it's not, we drop this one and do a new lookup.
1371 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1374 return afs_convert_code(code);
1378 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1381 cred_t *credp = crref();
1382 struct vcache *tvcp = NULL;
1384 const char *name = dp->d_name.name;
1386 #if defined(AFS_LINUX26_ENV)
1387 maybe_lock_kernel();
1390 vattr.va_mask = ATTR_MODE;
1391 vattr.va_mode = mode;
1393 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1396 struct inode *ip = AFSTOV(tvcp);
1398 afs_getattr(tvcp, &vattr, credp);
1399 afs_fill_inode(ip, &vattr);
1401 dp->d_op = &afs_dentry_operations;
1402 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1403 d_instantiate(dp, ip);
1407 #if defined(AFS_LINUX26_ENV)
1408 maybe_unlock_kernel();
1411 return afs_convert_code(code);
1415 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1418 cred_t *credp = crref();
1419 const char *name = dp->d_name.name;
1421 /* locking kernel conflicts with glock? */
1424 code = afs_rmdir(VTOAFS(dip), name, credp);
1427 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1428 * that failed because a directory is not empty. So, we map
1429 * EEXIST to ENOTEMPTY on linux.
1431 if (code == EEXIST) {
1440 return afs_convert_code(code);
1445 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1446 struct inode *newip, struct dentry *newdp)
1449 cred_t *credp = crref();
1450 const char *oldname = olddp->d_name.name;
1451 const char *newname = newdp->d_name.name;
1452 struct dentry *rehash = NULL;
1454 #if defined(AFS_LINUX26_ENV)
1455 /* Prevent any new references during rename operation. */
1456 maybe_lock_kernel();
1458 if (!d_unhashed(newdp)) {
1463 if (!list_empty(&newdp->d_hash)) {
1469 #if defined(AFS_LINUX24_ENV)
1470 if (atomic_read(&olddp->d_count) > 1)
1471 shrink_dcache_parent(olddp);
1475 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1479 olddp->d_time = 0; /* force to revalidate */
1484 #if defined(AFS_LINUX26_ENV)
1485 maybe_unlock_kernel();
1489 return afs_convert_code(code);
1493 /* afs_linux_ireadlink
1494 * Internal readlink which can return link contents to user or kernel space.
1495 * Note that the buffer is NOT supposed to be null-terminated.
1498 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1501 cred_t *credp = crref();
1505 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1506 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1510 return maxlen - tuio.uio_resid;
1512 return afs_convert_code(code);
1515 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1516 /* afs_linux_readlink
1517 * Fill target (which is in user space) with contents of symlink.
1520 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1523 struct inode *ip = dp->d_inode;
1526 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1532 /* afs_linux_follow_link
1533 * a file system dependent link following routine.
1535 #if defined(AFS_LINUX24_ENV)
1536 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1541 name = osi_Alloc(PATH_MAX);
1547 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1555 code = vfs_follow_link(nd, name);
1558 osi_Free(name, PATH_MAX);
1563 #else /* !defined(AFS_LINUX24_ENV) */
1565 static struct dentry *
1566 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1567 unsigned int follow)
1575 name = osi_Alloc(PATH_MAX + 1);
1579 return ERR_PTR(-EIO);
1582 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1587 if (code < -MAX_ERRNO)
1588 res = ERR_PTR(-EIO);
1590 res = ERR_PTR(code);
1593 res = lookup_dentry(name, basep, follow);
1597 osi_Free(name, PATH_MAX + 1);
1601 #endif /* AFS_LINUX24_ENV */
1602 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1604 #if defined(AFS_CACHE_BYPASS)
1606 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1608 /* The kernel calls readpages before trying readpage, with a list of
1609 * pages. The readahead algorithm expands num_pages when it thinks
1610 * the application will benefit. Unlike readpage, the pages are not
1611 * necessarily allocated. If we do not a) allocate required pages and
1612 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1613 * for each required page (and the page will be pre-allocated) */
1616 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1617 struct list_head *page_list, unsigned num_pages)
1622 struct iovec* iovecp;
1623 struct nocache_read_request *ancr;
1624 struct page *pp, *ppt;
1625 struct pagevec lrupv;
1629 struct inode *ip = FILE_INODE(fp);
1630 struct vcache *avc = VTOAFS(ip);
1631 afs_int32 bypasscache = 0; /* bypass for this read */
1632 afs_int32 base_index = 0;
1633 afs_int32 page_count = 0;
1638 switch(cache_bypass_strategy) {
1639 case NEVER_BYPASS_CACHE:
1641 case ALWAYS_BYPASS_CACHE:
1644 case LARGE_FILES_BYPASS_CACHE:
1645 if(i_size_read(ip) > cache_bypass_threshold) {
1653 /* In the new incarnation of selective caching, a file's caching policy
1654 * can change, eg because file size exceeds threshold, etc. */
1655 trydo_cache_transition(avc, credp, bypasscache);
1658 while(!list_empty(page_list)) {
1659 pp = list_entry(page_list->prev, struct page, lru);
1664 /* background thread must free: iovecp, auio, ancr */
1665 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1667 auio = osi_Alloc(sizeof(uio_t));
1668 auio->uio_iov = iovecp;
1669 auio->uio_iovcnt = num_pages;
1670 auio->uio_flag = UIO_READ;
1671 auio->uio_seg = AFS_UIOSYS;
1672 auio->uio_resid = num_pages * PAGE_SIZE;
1674 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1676 ancr->offset = auio->uio_offset;
1677 ancr->length = auio->uio_resid;
1679 pagevec_init(&lrupv, 0);
1681 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1683 if(list_empty(page_list))
1686 pp = list_entry(page_list->prev, struct page, lru);
1687 /* If we allocate a page and don't remove it from page_list,
1688 * the page cache gets upset. */
1690 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1691 if(pp->index > isize) {
1698 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1699 auio->uio_offset = offset;
1700 base_index = pp->index;
1702 iovecp[page_ix].iov_len = PAGE_SIZE;
1703 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1704 if(base_index != pp->index) {
1707 page_cache_release(pp);
1708 iovecp[page_ix].iov_base = (void *) 0;
1716 page_cache_release(pp);
1717 iovecp[page_ix].iov_base = (void *) 0;
1720 if(!PageLocked(pp)) {
1724 /* save the page for background map */
1725 iovecp[page_ix].iov_base = (void*) pp;
1727 /* and put it on the LRU cache */
1728 if (!pagevec_add(&lrupv, pp))
1729 __pagevec_lru_add(&lrupv);
1733 /* If there were useful pages in the page list, make sure all pages
1734 * are in the LRU cache, then schedule the read */
1736 pagevec_lru_add(&lrupv);
1737 code = afs_ReadNoCache(avc, ancr, credp);
1739 /* If there is nothing for the background thread to handle,
1740 * it won't be freeing the things that we never gave it */
1741 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1742 osi_Free(auio, sizeof(uio_t));
1743 osi_Free(ancr, sizeof(struct nocache_read_request));
1745 /* we do not flush, release, or unmap pages--that will be
1746 * done for us by the background thread as each page comes in
1747 * from the fileserver */
1751 return afs_convert_code(code);
1754 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1755 #endif /* defined(AFS_CACHE_BYPASS */
1758 /* afs_linux_readpage
1759 * all reads come through here. A strategy-like read call.
1762 afs_linux_readpage(struct file *fp, struct page *pp)
1765 cred_t *credp = crref();
1766 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1768 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1770 ulong address = afs_linux_page_address(pp);
1771 afs_offs_t offset = pageoff(pp);
1773 #if defined(AFS_CACHE_BYPASS)
1774 afs_int32 bypasscache = 0; /* bypass for this read */
1775 struct nocache_read_request *ancr;
1779 struct iovec *iovecp;
1780 struct inode *ip = FILE_INODE(fp);
1781 afs_int32 cnt = page_count(pp);
1782 struct vcache *avc = VTOAFS(ip);
1784 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1788 atomic_add(1, &pp->count);
1789 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1790 clear_bit(PG_error, &pp->flags);
1792 #if defined(AFS_CACHE_BYPASS)
1793 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1794 /* If the page is past the end of the file, skip it */
1795 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1796 if(pp->index > isize) {
1803 /* if bypasscache, receiver frees, else we do */
1804 auio = osi_Alloc(sizeof(uio_t));
1805 iovecp = osi_Alloc(sizeof(struct iovec));
1807 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1810 #if defined(AFS_CACHE_BYPASS)
1812 switch(cache_bypass_strategy) {
1813 case NEVER_BYPASS_CACHE:
1815 case ALWAYS_BYPASS_CACHE:
1818 case LARGE_FILES_BYPASS_CACHE:
1819 if(i_size_read(ip) > cache_bypass_threshold) {
1827 /* In the new incarnation of selective caching, a file's caching policy
1828 * can change, eg because file size exceeds threshold, etc. */
1829 trydo_cache_transition(avc, credp, bypasscache);
1834 /* save the page for background map */
1835 auio->uio_iov->iov_base = (void*) pp;
1836 /* the background thread will free this */
1837 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1839 ancr->offset = offset;
1840 ancr->length = PAGE_SIZE;
1842 maybe_lock_kernel();
1843 code = afs_ReadNoCache(avc, ancr, credp);
1844 maybe_unlock_kernel();
1846 goto done; /* skips release page, doing it in bg thread */
1850 #ifdef AFS_LINUX24_ENV
1851 maybe_lock_kernel();
1855 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 */
1857 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1859 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1860 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1862 AFS_DISCON_UNLOCK();
1864 #ifdef AFS_LINUX24_ENV
1865 maybe_unlock_kernel();
1868 /* XXX valid for no-cache also? Check last bits of files... :)
1869 * Cognate code goes in afs_NoCacheFetchProc. */
1870 if (auio->uio_resid) /* zero remainder of page */
1871 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1874 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1875 flush_dcache_page(pp);
1876 SetPageUptodate(pp);
1878 set_bit(PG_uptodate, &pp->flags);
1882 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1886 clear_bit(PG_locked, &pp->flags);
1891 #if defined(AFS_CACHE_BYPASS)
1893 /* do not call afs_GetDCache if cache is bypassed */
1899 /* free if not bypassing cache */
1900 osi_Free(auio, sizeof(uio_t));
1901 osi_Free(iovecp, sizeof(struct iovec));
1903 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1905 struct vrequest treq;
1908 code = afs_InitReq(&treq, credp);
1909 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1910 tdc = afs_FindDCache(avc, offset);
1912 if (!(tdc->mflags & DFNextStarted))
1913 afs_PrefetchChunk(avc, tdc, credp, &treq);
1916 ReleaseWriteLock(&avc->lock);
1923 return afs_convert_code(code);
1927 #if defined(AFS_LINUX24_ENV)
1929 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1930 unsigned long offset, unsigned int count)
1932 struct vcache *vcp = VTOAFS(ip);
1941 buffer = kmap(pp) + offset;
1942 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1945 maybe_lock_kernel();
1947 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1948 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1949 ICL_TYPE_INT32, 99999);
1951 ObtainWriteLock(&vcp->lock, 532);
1952 if (vcp->f.states & CPageWrite) {
1953 ReleaseWriteLock(&vcp->lock);
1955 maybe_unlock_kernel();
1958 #ifdef AFS_LINUX26_ENV
1959 #if defined(WRITEPAGE_ACTIVATE)
1960 return WRITEPAGE_ACTIVATE;
1962 return AOP_WRITEPAGE_ACTIVATE;
1965 /* should mark it dirty? */
1969 vcp->f.states |= CPageWrite;
1970 ReleaseWriteLock(&vcp->lock);
1972 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1974 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1976 i_size_write(ip, vcp->f.m.Length);
1977 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1979 ObtainWriteLock(&vcp->lock, 533);
1981 struct vrequest treq;
1983 if (!afs_InitReq(&treq, credp))
1984 code = afs_DoPartialWrite(vcp, &treq);
1986 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1988 vcp->f.states &= ~CPageWrite;
1989 ReleaseWriteLock(&vcp->lock);
1991 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1992 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1993 ICL_TYPE_INT32, code);
1996 maybe_unlock_kernel();
2005 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2006 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2008 afs_linux_writepage(struct page *pp)
2011 struct address_space *mapping = pp->mapping;
2012 struct inode *inode;
2013 unsigned long end_index;
2014 unsigned offset = PAGE_CACHE_SIZE;
2017 #if defined(AFS_LINUX26_ENV)
2018 if (PageReclaim(pp)) {
2019 # if defined(WRITEPAGE_ACTIVATE)
2020 return WRITEPAGE_ACTIVATE;
2022 return AOP_WRITEPAGE_ACTIVATE;
2026 if (PageLaunder(pp)) {
2027 return(fail_writepage(pp));
2031 inode = (struct inode *)mapping->host;
2032 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
2035 if (pp->index < end_index)
2037 /* things got complicated... */
2038 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
2039 /* OK, are we completely out? */
2040 if (pp->index >= end_index + 1 || !offset)
2043 status = afs_linux_writepage_sync(inode, pp, 0, offset);
2044 SetPageUptodate(pp);
2045 #if defined(WRITEPAGE_ACTIVATE)
2046 if ( status != WRITEPAGE_ACTIVATE )
2048 if ( status != AOP_WRITEPAGE_ACTIVATE )
2051 if (status == offset)
2057 /* afs_linux_updatepage
2058 * What one would have thought was writepage - write dirty page to file.
2059 * Called from generic_file_write. buffer is still in user space. pagep
2060 * has been filled in with old data if we're updating less than a page.
2063 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2064 unsigned int count, int sync)
2066 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2067 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2073 set_bit(PG_locked, &pp->flags);
2078 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2079 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2080 ICL_TYPE_INT32, 99999);
2081 setup_uio(&tuio, &iovec, page_addr + offset,
2082 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2085 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2087 i_size_write(ip, vcp->f.m.Length);
2088 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2091 struct vrequest treq;
2093 ObtainWriteLock(&vcp->lock, 533);
2094 vcp->f.m.Date = osi_Time(); /* set modification time */
2095 if (!afs_InitReq(&treq, credp))
2096 code = afs_DoPartialWrite(vcp, &treq);
2097 ReleaseWriteLock(&vcp->lock);
2100 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2101 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2102 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2103 ICL_TYPE_INT32, code);
2105 AFS_DISCON_UNLOCK();
2109 clear_bit(PG_locked, &pp->flags);
2114 /* afs_linux_permission
2115 * Check access rights - returns error if can't check or permission denied.
2118 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2119 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2121 afs_linux_permission(struct inode *ip, int mode)
2125 cred_t *credp = crref();
2129 if (mode & MAY_EXEC)
2131 if (mode & MAY_READ)
2133 if (mode & MAY_WRITE)
2135 code = afs_access(VTOAFS(ip), tmp, credp);
2139 return afs_convert_code(code);
2142 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2144 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2149 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2150 offset, to - offset);
2151 #if !defined(AFS_LINUX26_ENV)
2159 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2162 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2163 call kmap directly instead of relying on us to do it */
2164 #if !defined(AFS_LINUX26_ENV)
2171 #if defined(HAVE_WRITE_BEGIN)
2173 afs_linux_write_end(struct file *file, struct address_space *mapping,
2174 loff_t pos, unsigned len, unsigned copied,
2175 struct page *page, void *fsdata)
2178 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2179 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2181 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2184 page_cache_release(page);
2189 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2190 loff_t pos, unsigned len, unsigned flags,
2191 struct page **pagep, void **fsdata)
2194 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2195 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2196 page = grab_cache_page_write_begin(mapping, index, flags);
2198 page = __grab_cache_page(mapping, index);
2207 static struct inode_operations afs_file_iops = {
2208 #if defined(AFS_LINUX26_ENV)
2209 .permission = afs_linux_permission,
2210 .getattr = afs_linux_getattr,
2211 .setattr = afs_notify_change,
2212 #elif defined(AFS_LINUX24_ENV)
2213 .permission = afs_linux_permission,
2214 .revalidate = afs_linux_revalidate,
2215 .setattr = afs_notify_change,
2217 .default_file_ops = &afs_file_fops,
2218 .readpage = afs_linux_readpage,
2219 .revalidate = afs_linux_revalidate,
2220 .updatepage = afs_linux_updatepage,
2224 #if defined(AFS_LINUX24_ENV)
2225 static struct address_space_operations afs_file_aops = {
2226 .readpage = afs_linux_readpage,
2227 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2228 .readpages = afs_linux_readpages,
2230 .writepage = afs_linux_writepage,
2231 #if defined (HAVE_WRITE_BEGIN)
2232 .write_begin = afs_linux_write_begin,
2233 .write_end = afs_linux_write_end,
2235 .commit_write = afs_linux_commit_write,
2236 .prepare_write = afs_linux_prepare_write,
2242 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2243 * by what sort of operation is allowed.....
2246 static struct inode_operations afs_dir_iops = {
2247 #if !defined(AFS_LINUX24_ENV)
2248 .default_file_ops = &afs_dir_fops,
2250 .setattr = afs_notify_change,
2252 .create = afs_linux_create,
2253 .lookup = afs_linux_lookup,
2254 .link = afs_linux_link,
2255 .unlink = afs_linux_unlink,
2256 .symlink = afs_linux_symlink,
2257 .mkdir = afs_linux_mkdir,
2258 .rmdir = afs_linux_rmdir,
2259 .rename = afs_linux_rename,
2260 #if defined(AFS_LINUX26_ENV)
2261 .getattr = afs_linux_getattr,
2263 .revalidate = afs_linux_revalidate,
2265 .permission = afs_linux_permission,
2268 /* We really need a separate symlink set of ops, since do_follow_link()
2269 * determines if it _is_ a link by checking if the follow_link op is set.
2271 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2273 afs_symlink_filler(struct file *file, struct page *page)
2275 struct inode *ip = (struct inode *)page->mapping->host;
2276 char *p = (char *)kmap(page);
2279 maybe_lock_kernel();
2281 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2286 p[code] = '\0'; /* null terminate? */
2287 maybe_unlock_kernel();
2289 SetPageUptodate(page);
2295 maybe_unlock_kernel();
2303 static struct address_space_operations afs_symlink_aops = {
2304 .readpage = afs_symlink_filler
2306 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2308 static struct inode_operations afs_symlink_iops = {
2309 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2310 .readlink = page_readlink,
2311 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2312 .follow_link = page_follow_link,
2314 .follow_link = page_follow_link_light,
2315 .put_link = page_put_link,
2317 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2318 .readlink = afs_linux_readlink,
2319 .follow_link = afs_linux_follow_link,
2320 #if !defined(AFS_LINUX24_ENV)
2321 .permission = afs_linux_permission,
2322 .revalidate = afs_linux_revalidate,
2324 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2325 #if defined(AFS_LINUX24_ENV)
2326 .setattr = afs_notify_change,
2331 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2335 vattr2inode(ip, vattr);
2337 #if defined(AFS_LINUX26_ENV)
2338 ip->i_mapping->backing_dev_info = &afs_backing_dev_info;
2340 /* Reset ops if symlink or directory. */
2341 if (S_ISREG(ip->i_mode)) {
2342 ip->i_op = &afs_file_iops;
2343 #if defined(AFS_LINUX24_ENV)
2344 ip->i_fop = &afs_file_fops;
2345 ip->i_data.a_ops = &afs_file_aops;
2348 } else if (S_ISDIR(ip->i_mode)) {
2349 ip->i_op = &afs_dir_iops;
2350 #if defined(AFS_LINUX24_ENV)
2351 ip->i_fop = &afs_dir_fops;
2354 } else if (S_ISLNK(ip->i_mode)) {
2355 ip->i_op = &afs_symlink_iops;
2356 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2357 ip->i_data.a_ops = &afs_symlink_aops;
2358 ip->i_mapping = &ip->i_data;