2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
29 #ifdef HAVE_MM_INLINE_H
30 #include <linux/mm_inline.h>
32 #include <linux/pagemap.h>
33 #include <linux/writeback.h>
34 #include <linux/pagevec.h>
35 #include <linux/aio.h>
37 #include "afs/afs_bypasscache.h"
39 #include "osi_compat.h"
40 #include "osi_pagecopy.h"
42 #ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
43 #define __pagevec_lru_add_file __pagevec_lru_add
47 #define MAX_ERRNO 1000L
50 int cachefs_noreadpage = 0;
52 extern struct backing_dev_info *afs_backing_dev_info;
54 extern struct vcache *afs_globalVp;
56 /* This function converts a positive error code from AFS into a negative
57 * code suitable for passing into the Linux VFS layer. It checks that the
58 * error code is within the permissable bounds for the ERR_PTR mechanism.
60 * _All_ error codes which come from the AFS layer should be passed through
61 * this function before being returned to the kernel.
65 afs_convert_code(int code) {
66 if ((code >= 0) && (code <= MAX_ERRNO))
72 /* Linux doesn't require a credp for many functions, and crref is an expensive
73 * operation. This helper function avoids obtaining it for VerifyVCache calls
77 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
82 if (avc->f.states & CStatd) {
90 code = afs_InitReq(&treq, credp);
92 code = afs_VerifyVCache2(avc, &treq);
99 return afs_convert_code(code);
102 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
103 # ifdef LINUX_HAS_NONVECTOR_AIO
105 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
109 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
110 unsigned long bufsize, loff_t pos)
113 struct file *fp = iocb->ki_filp;
115 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
118 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
119 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
120 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
121 code = afs_linux_VerifyVCache(vcp, NULL);
124 /* Linux's FlushPages implementation doesn't ever use credp,
125 * so we optimise by not using it */
126 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
128 code = generic_file_aio_read(iocb, buf, bufsize, pos);
132 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
133 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
134 (afs_int32)bufsize, ICL_TYPE_INT32, code);
140 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
143 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
146 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
147 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
149 code = afs_linux_VerifyVCache(vcp, NULL);
152 /* Linux's FlushPages implementation doesn't ever use credp,
153 * so we optimise by not using it */
154 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
156 code = do_sync_read(fp, buf, count, offp);
160 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
161 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
169 /* Now we have integrated VM for writes as well as reads. the generic write operations
170 * also take care of re-positioning the pointer if file is open in append
171 * mode. Call fake open/close to ensure we do writes of core dumps.
173 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
174 # ifdef LINUX_HAS_NONVECTOR_AIO
176 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
180 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
181 unsigned long bufsize, loff_t pos)
185 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
190 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
191 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
192 (afs_int32)bufsize, ICL_TYPE_INT32,
193 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
195 code = afs_linux_VerifyVCache(vcp, &credp);
197 ObtainWriteLock(&vcp->lock, 529);
199 ReleaseWriteLock(&vcp->lock);
202 code = generic_file_aio_write(iocb, buf, bufsize, pos);
206 ObtainWriteLock(&vcp->lock, 530);
208 if (vcp->execsOrWriters == 1 && !credp)
211 afs_FakeClose(vcp, credp);
212 ReleaseWriteLock(&vcp->lock);
214 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
215 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
216 (afs_int32)bufsize, ICL_TYPE_INT32, code);
225 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
228 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
233 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
234 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
235 (fp->f_flags & O_APPEND) ? 99998 : 99999);
237 code = afs_linux_VerifyVCache(vcp, &credp);
239 ObtainWriteLock(&vcp->lock, 529);
241 ReleaseWriteLock(&vcp->lock);
244 code = do_sync_write(fp, buf, count, offp);
248 ObtainWriteLock(&vcp->lock, 530);
250 if (vcp->execsOrWriters == 1 && !credp)
253 afs_FakeClose(vcp, credp);
254 ReleaseWriteLock(&vcp->lock);
256 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
257 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
267 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
269 /* This is a complete rewrite of afs_readdir, since we can make use of
270 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
271 * handling and use of bulkstats will need to be reflected here as well.
274 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
275 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
277 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
280 struct vcache *avc = VTOAFS(FILE_INODE(fp));
281 struct vrequest treq;
287 struct DirBuffer entry;
290 afs_size_t origOffset, tlen;
291 cred_t *credp = crref();
292 struct afs_fakestat_state fakestat;
295 AFS_STATCNT(afs_readdir);
297 code = afs_convert_code(afs_InitReq(&treq, credp));
302 afs_InitFakeStat(&fakestat);
303 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
307 /* update the cache entry */
309 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
313 /* get a reference to the entire directory */
314 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
320 ObtainWriteLock(&avc->lock, 811);
321 ObtainReadLock(&tdc->lock);
323 * Make sure that the data in the cache is current. There are two
324 * cases we need to worry about:
325 * 1. The cache data is being fetched by another process.
326 * 2. The cache data is no longer valid
328 while ((avc->f.states & CStatd)
329 && (tdc->dflags & DFFetching)
330 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
331 ReleaseReadLock(&tdc->lock);
332 ReleaseWriteLock(&avc->lock);
333 afs_osi_Sleep(&tdc->validPos);
334 ObtainWriteLock(&avc->lock, 812);
335 ObtainReadLock(&tdc->lock);
337 if (!(avc->f.states & CStatd)
338 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
339 ReleaseReadLock(&tdc->lock);
340 ReleaseWriteLock(&avc->lock);
345 /* Set the readdir-in-progress flag, and downgrade the lock
346 * to shared so others will be able to acquire a read lock.
348 avc->f.states |= CReadDir;
349 avc->dcreaddir = tdc;
350 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
351 ConvertWToSLock(&avc->lock);
353 /* Fill in until we get an error or we're done. This implementation
354 * takes an offset in units of blobs, rather than bytes.
357 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
360 offset = (int) fp->f_pos;
363 dirpos = BlobScan(tdc, offset);
367 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
369 afs_warn("Corrupt directory (inode %lx, dirpos %d)",
370 (unsigned long)&tdc->f.inode, dirpos);
371 ReleaseSharedLock(&avc->lock);
377 de = (struct DirEntry *)entry.data;
378 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
379 ntohl(de->fid.vnode));
380 len = strlen(de->name);
382 /* filldir returns -EINVAL when the buffer is full. */
384 unsigned int type = DT_UNKNOWN;
385 struct VenusFid afid;
388 afid.Cell = avc->f.fid.Cell;
389 afid.Fid.Volume = avc->f.fid.Fid.Volume;
390 afid.Fid.Vnode = ntohl(de->fid.vnode);
391 afid.Fid.Unique = ntohl(de->fid.vunique);
392 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
394 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
397 } else if (((tvc->f.states) & (CStatd | CTruth))) {
398 /* CTruth will be set if the object has
403 else if (vtype == VREG)
405 /* Don't do this until we're sure it can't be a mtpt */
406 /* else if (vtype == VLNK)
408 /* what other types does AFS support? */
410 /* clean up from afs_FindVCache */
414 * If this is NFS readdirplus, then the filler is going to
415 * call getattr on this inode, which will deadlock if we're
419 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
420 /* dir_emit returns a bool - true when it succeeds.
421 * Inverse the result to fit with how we check "code" */
422 code = !dir_emit(ctx, de->name, len, ino, type);
424 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
431 offset = dirpos + 1 + ((len + 16) >> 5);
433 /* If filldir didn't fill in the last one this is still pointing to that
436 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
437 ctx->pos = (loff_t) offset;
439 fp->f_pos = (loff_t) offset;
442 ReleaseReadLock(&tdc->lock);
444 UpgradeSToWLock(&avc->lock, 813);
445 avc->f.states &= ~CReadDir;
447 avc->readdir_pid = 0;
448 ReleaseSharedLock(&avc->lock);
452 afs_PutFakeStat(&fakestat);
459 /* in afs_pioctl.c */
460 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
463 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
464 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
466 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
473 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
475 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
479 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
480 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
481 vmap->vm_end - vmap->vm_start);
483 /* get a validated vcache entry */
484 code = afs_linux_VerifyVCache(vcp, NULL);
487 /* Linux's Flushpage implementation doesn't use credp, so optimise
488 * our code to not need to crref() it */
489 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
491 code = generic_file_mmap(fp, vmap);
494 vcp->f.states |= CMAPPED;
502 afs_linux_open(struct inode *ip, struct file *fp)
504 struct vcache *vcp = VTOAFS(ip);
505 cred_t *credp = crref();
509 code = afs_open(&vcp, fp->f_flags, credp);
513 return afs_convert_code(code);
517 afs_linux_release(struct inode *ip, struct file *fp)
519 struct vcache *vcp = VTOAFS(ip);
520 cred_t *credp = crref();
524 code = afs_close(vcp, fp->f_flags, credp);
525 ObtainWriteLock(&vcp->lock, 807);
530 ReleaseWriteLock(&vcp->lock);
534 return afs_convert_code(code);
538 #if defined(FOP_FSYNC_TAKES_DENTRY)
539 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
540 #elif defined(FOP_FSYNC_TAKES_RANGE)
541 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
543 afs_linux_fsync(struct file *fp, int datasync)
547 struct inode *ip = FILE_INODE(fp);
548 cred_t *credp = crref();
550 #if defined(FOP_FSYNC_TAKES_RANGE)
551 mutex_lock(&ip->i_mutex);
554 code = afs_fsync(VTOAFS(ip), credp);
556 #if defined(FOP_FSYNC_TAKES_RANGE)
557 mutex_unlock(&ip->i_mutex);
560 return afs_convert_code(code);
566 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
569 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
570 cred_t *credp = crref();
571 struct AFS_FLOCK flock;
573 /* Convert to a lock format afs_lockctl understands. */
574 memset(&flock, 0, sizeof(flock));
575 flock.l_type = flp->fl_type;
576 flock.l_pid = flp->fl_pid;
578 flock.l_start = flp->fl_start;
579 if (flp->fl_end == OFFSET_MAX)
580 flock.l_len = 0; /* Lock to end of file */
582 flock.l_len = flp->fl_end - flp->fl_start + 1;
584 /* Safe because there are no large files, yet */
585 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
586 if (cmd == F_GETLK64)
588 else if (cmd == F_SETLK64)
590 else if (cmd == F_SETLKW64)
592 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
595 if ((vcp->f.states & CRO)) {
596 if (flp->fl_type == F_WRLCK) {
605 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
608 if ((code == 0 || flp->fl_type == F_UNLCK) &&
609 (cmd == F_SETLK || cmd == F_SETLKW)) {
610 code = afs_posix_lock_file(fp, flp);
611 if (code && flp->fl_type != F_UNLCK) {
612 struct AFS_FLOCK flock2;
614 flock2.l_type = F_UNLCK;
616 afs_lockctl(vcp, &flock2, F_SETLK, credp);
620 /* If lockctl says there are no conflicting locks, then also check with the
621 * kernel, as lockctl knows nothing about byte range locks
623 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
624 afs_posix_test_lock(fp, flp);
625 /* If we found a lock in the kernel's structure, return it */
626 if (flp->fl_type != F_UNLCK) {
632 /* Convert flock back to Linux's file_lock */
633 flp->fl_type = flock.l_type;
634 flp->fl_pid = flock.l_pid;
635 flp->fl_start = flock.l_start;
636 if (flock.l_len == 0)
637 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
639 flp->fl_end = flock.l_start + flock.l_len - 1;
645 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
647 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
649 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
650 cred_t *credp = crref();
651 struct AFS_FLOCK flock;
652 /* Convert to a lock format afs_lockctl understands. */
653 memset(&flock, 0, sizeof(flock));
654 flock.l_type = flp->fl_type;
655 flock.l_pid = flp->fl_pid;
660 /* Safe because there are no large files, yet */
661 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
662 if (cmd == F_GETLK64)
664 else if (cmd == F_SETLK64)
666 else if (cmd == F_SETLKW64)
668 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
671 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
674 if ((code == 0 || flp->fl_type == F_UNLCK) &&
675 (cmd == F_SETLK || cmd == F_SETLKW)) {
676 flp->fl_flags &=~ FL_SLEEP;
677 code = flock_lock_file_wait(fp, flp);
678 if (code && flp->fl_type != F_UNLCK) {
679 struct AFS_FLOCK flock2;
681 flock2.l_type = F_UNLCK;
683 afs_lockctl(vcp, &flock2, F_SETLK, credp);
687 /* Convert flock back to Linux's file_lock */
688 flp->fl_type = flock.l_type;
689 flp->fl_pid = flock.l_pid;
697 * essentially the same as afs_fsync() but we need to get the return
698 * code for the sys_close() here, not afs_linux_release(), so call
699 * afs_StoreAllSegments() with AFS_LASTSTORE
702 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
703 afs_linux_flush(struct file *fp, fl_owner_t id)
705 afs_linux_flush(struct file *fp)
708 struct vrequest treq;
716 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
724 vcp = VTOAFS(FILE_INODE(fp));
726 code = afs_InitReq(&treq, credp);
729 /* If caching is bypassed for this file, or globally, just return 0 */
730 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
733 ObtainReadLock(&vcp->lock);
734 if (vcp->cachingStates & FCSBypass)
736 ReleaseReadLock(&vcp->lock);
739 /* future proof: don't rely on 0 return from afs_InitReq */
744 ObtainSharedLock(&vcp->lock, 535);
745 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
746 UpgradeSToWLock(&vcp->lock, 536);
747 if (!AFS_IS_DISCONNECTED) {
748 code = afs_StoreAllSegments(vcp,
750 AFS_SYNC | AFS_LASTSTORE);
752 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
754 ConvertWToSLock(&vcp->lock);
756 code = afs_CheckCode(code, &treq, 54);
757 ReleaseSharedLock(&vcp->lock);
764 return afs_convert_code(code);
767 struct file_operations afs_dir_fops = {
768 .read = generic_read_dir,
769 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
770 .iterate = afs_linux_readdir,
772 .readdir = afs_linux_readdir,
774 #ifdef HAVE_UNLOCKED_IOCTL
775 .unlocked_ioctl = afs_unlocked_xioctl,
779 #ifdef HAVE_COMPAT_IOCTL
780 .compat_ioctl = afs_unlocked_xioctl,
782 .open = afs_linux_open,
783 .release = afs_linux_release,
784 .llseek = default_llseek,
785 #ifdef HAVE_LINUX_NOOP_FSYNC
788 .fsync = simple_sync_file,
792 struct file_operations afs_file_fops = {
793 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
794 .aio_read = afs_linux_aio_read,
795 .aio_write = afs_linux_aio_write,
797 .read = afs_linux_read,
798 .write = afs_linux_write,
800 #ifdef HAVE_UNLOCKED_IOCTL
801 .unlocked_ioctl = afs_unlocked_xioctl,
805 #ifdef HAVE_COMPAT_IOCTL
806 .compat_ioctl = afs_unlocked_xioctl,
808 .mmap = afs_linux_mmap,
809 .open = afs_linux_open,
810 .flush = afs_linux_flush,
811 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
812 .sendfile = generic_file_sendfile,
814 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
815 .splice_write = generic_file_splice_write,
816 .splice_read = generic_file_splice_read,
818 .release = afs_linux_release,
819 .fsync = afs_linux_fsync,
820 .lock = afs_linux_lock,
821 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
822 .flock = afs_linux_flock,
824 .llseek = default_llseek,
827 static struct dentry *
828 canonical_dentry(struct inode *ip)
830 struct vcache *vcp = VTOAFS(ip);
831 struct dentry *first = NULL, *ret = NULL, *cur;
832 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
833 struct hlist_node *p;
837 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
838 * otherwise, use the first dentry in ip->i_dentry.
839 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
841 /* note that vcp->target_link specifies which dentry to use, but we have
842 * no reference held on that dentry. so, we cannot use or dereference
843 * vcp->target_link itself, since it may have been freed. instead, we only
844 * use it to compare to pointers in the ip->i_dentry list. */
848 # ifdef HAVE_DCACHE_LOCK
849 spin_lock(&dcache_lock);
851 spin_lock(&ip->i_lock);
854 #if defined(D_ALIAS_IS_HLIST)
855 # if defined(HLIST_ITERATOR_NO_NODE)
856 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
858 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
861 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
864 if (!vcp->target_link || cur == vcp->target_link) {
877 vcp->target_link = ret;
879 # ifdef HAVE_DCACHE_LOCK
883 spin_unlock(&dcache_lock);
888 spin_unlock(&ip->i_lock);
894 /**********************************************************************
895 * AFS Linux dentry operations
896 **********************************************************************/
898 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
899 * that has its mvid (parent dir's fid) pointer set to the wrong directory
900 * due to being mounted in multiple points at once. fix_bad_parent()
901 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
902 * dotdotfid and mtpoint fid members.
904 * dp - dentry to be checked.
905 * credp - credentials
906 * vcp, pvc - item's and parent's vcache pointer
910 * This dentry's vcache's mvid will be set to the correct parent directory's
912 * This root vnode's volume will have its dotdotfid and mtpoint fids set
913 * to the correct parent and mountpoint fids.
917 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
919 struct vcache *avc = NULL;
921 /* force a lookup, so vcp->mvid is fixed up */
922 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
923 if (!avc || vcp != avc) { /* bad, very bad.. */
924 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
925 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
926 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
927 ICL_TYPE_POINTER, dp);
930 AFS_RELE(AFSTOV(avc));
935 /* afs_linux_revalidate
936 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
939 afs_linux_revalidate(struct dentry *dp)
942 struct vcache *vcp = VTOAFS(dp->d_inode);
946 if (afs_shuttingdown)
952 /* Make this a fast path (no crref), since it's called so often. */
953 if (vcp->states & CStatd) {
954 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
956 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
957 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
960 fix_bad_parent(dp); /* check and correct mvid */
969 /* This avoids the crref when we don't have to do it. Watch for
970 * changes in afs_getattr that don't get replicated here!
972 if (vcp->f.states & CStatd &&
973 (!afs_fakestat_enable || vcp->mvstat != 1) &&
975 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
976 code = afs_CopyOutAttrs(vcp, &vattr);
979 code = afs_getattr(vcp, &vattr, credp);
984 afs_fill_inode(AFSTOV(vcp), &vattr);
988 return afs_convert_code(code);
992 * Set iattr data into vattr. Assume vattr cleared before call.
995 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
997 vattrp->va_mask = iattrp->ia_valid;
998 if (iattrp->ia_valid & ATTR_MODE)
999 vattrp->va_mode = iattrp->ia_mode;
1000 if (iattrp->ia_valid & ATTR_UID)
1001 vattrp->va_uid = iattrp->ia_uid;
1002 if (iattrp->ia_valid & ATTR_GID)
1003 vattrp->va_gid = iattrp->ia_gid;
1004 if (iattrp->ia_valid & ATTR_SIZE)
1005 vattrp->va_size = iattrp->ia_size;
1006 if (iattrp->ia_valid & ATTR_ATIME) {
1007 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1008 vattrp->va_atime.tv_usec = 0;
1010 if (iattrp->ia_valid & ATTR_MTIME) {
1011 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1012 vattrp->va_mtime.tv_usec = 0;
1014 if (iattrp->ia_valid & ATTR_CTIME) {
1015 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1016 vattrp->va_ctime.tv_usec = 0;
1021 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1024 vattr2inode(struct inode *ip, struct vattr *vp)
1026 ip->i_ino = vp->va_nodeid;
1027 #ifdef HAVE_LINUX_SET_NLINK
1028 set_nlink(ip, vp->va_nlink);
1030 ip->i_nlink = vp->va_nlink;
1032 ip->i_blocks = vp->va_blocks;
1033 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1034 ip->i_blkbits = AFS_BLKBITS;
1036 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1037 ip->i_blksize = vp->va_blocksize;
1039 ip->i_rdev = vp->va_rdev;
1040 ip->i_mode = vp->va_mode;
1041 ip->i_uid = vp->va_uid;
1042 ip->i_gid = vp->va_gid;
1043 i_size_write(ip, vp->va_size);
1044 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1045 ip->i_atime.tv_nsec = 0;
1046 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1047 /* Set the mtime nanoseconds to the sysname generation number.
1048 * This convinces NFS clients that all directories have changed
1049 * any time the sysname list changes.
1051 ip->i_mtime.tv_nsec = afs_sysnamegen;
1052 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1053 ip->i_ctime.tv_nsec = 0;
1056 /* afs_notify_change
1057 * Linux version of setattr call. What to change is in the iattr struct.
1058 * We need to set bits in both the Linux inode as well as the vcache.
1061 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1064 cred_t *credp = crref();
1065 struct inode *ip = dp->d_inode;
1069 iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
1072 code = afs_setattr(VTOAFS(ip), &vattr, credp);
1074 afs_getattr(VTOAFS(ip), &vattr, credp);
1075 vattr2inode(ip, &vattr);
1079 return afs_convert_code(code);
1083 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1085 int err = afs_linux_revalidate(dentry);
1087 generic_fillattr(dentry->d_inode, stat);
1092 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1093 * In kernels 2.2.10 and above, we are passed an additional flags var which
1094 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1095 * we are advised to follow the entry if it is a link or to make sure that
1096 * it is a directory. But since the kernel itself checks these possibilities
1097 * later on, we shouldn't have to do it until later. Perhaps in the future..
1099 * The code here assumes that on entry the global lock is not held
1102 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1103 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1104 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1105 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1107 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1111 cred_t *credp = NULL;
1112 struct vcache *vcp, *pvcp, *tvc = NULL;
1113 struct dentry *parent;
1115 struct afs_fakestat_state fakestate;
1119 /* We don't support RCU path walking */
1120 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1121 if (flags & LOOKUP_RCU)
1123 if (nd->flags & LOOKUP_RCU)
1128 afs_InitFakeStat(&fakestate);
1131 vcp = VTOAFS(dp->d_inode);
1133 if (vcp == afs_globalVp)
1136 parent = dget_parent(dp);
1137 pvcp = VTOAFS(parent->d_inode);
1139 if ((vcp->mvstat == 1) || (vcp->mvstat == 2)) { /* need to lock */
1145 if (locked && vcp->mvstat == 1) { /* mount point */
1146 if (vcp->mvid && (vcp->f.states & CMValid)) {
1147 int tryEvalOnly = 0;
1149 struct vrequest treq;
1151 code = afs_InitReq(&treq, credp);
1153 (strcmp(dp->d_name.name, ".directory") == 0)) {
1157 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
1159 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
1160 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1161 /* a mount point, not yet replaced by its directory */
1167 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
1168 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
1169 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
1174 /* If the last looker changes, we should make sure the current
1175 * looker still has permission to examine this file. This would
1176 * always require a crref() which would be "slow".
1178 if (vcp->last_looker != treq.uid) {
1179 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1184 vcp->last_looker = treq.uid;
1189 /* If the parent's DataVersion has changed or the vnode
1190 * is longer valid, we need to do a full lookup. VerifyVCache
1191 * isn't enough since the vnode may have been renamed.
1194 if ((!locked) && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) ) {
1200 if (locked && (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd))) {
1201 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1202 if (!tvc || tvc != vcp) {
1207 if (afs_getattr(vcp, &vattr, credp)) {
1212 vattr2inode(AFSTOV(vcp), &vattr);
1213 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1216 /* should we always update the attributes at this point? */
1217 /* unlikely--the vcache entry hasn't changed */
1222 /* If this code is ever enabled, we should use dget_parent to handle
1223 * getting the parent, and dput() to dispose of it. See above for an
1225 pvcp = VTOAFS(dp->d_parent->d_inode);
1226 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1230 /* No change in parent's DataVersion so this negative
1231 * lookup is still valid. BUT, if a server is down a
1232 * negative lookup can result so there should be a
1233 * liftime as well. For now, always expire.
1246 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1248 /* we hold the global lock if we evaluated a mount point */
1255 shrink_dcache_parent(dp);
1261 if (have_submounts(dp))
1269 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1271 struct vcache *vcp = VTOAFS(ip);
1274 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1275 (void) afs_InactiveVCache(vcp, NULL);
1278 afs_linux_clear_nfsfs_renamed(dp);
1284 #if defined(DOP_D_DELETE_TAKES_CONST)
1285 afs_dentry_delete(const struct dentry *dp)
1287 afs_dentry_delete(struct dentry *dp)
1290 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1291 return 1; /* bad inode? */
1296 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1297 static struct vfsmount *
1298 afs_dentry_automount(afs_linux_path_t *path)
1300 struct dentry *target;
1302 /* avoid symlink resolution limits when resolving; we cannot contribute to
1303 * an infinite symlink loop */
1304 current->total_link_count--;
1306 target = canonical_dentry(path->dentry->d_inode);
1308 if (target == path->dentry) {
1315 path->dentry = target;
1318 spin_lock(&path->dentry->d_lock);
1319 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1320 spin_unlock(&path->dentry->d_lock);
1325 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1327 struct dentry_operations afs_dentry_operations = {
1328 .d_revalidate = afs_linux_dentry_revalidate,
1329 .d_delete = afs_dentry_delete,
1330 .d_iput = afs_dentry_iput,
1331 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1332 .d_automount = afs_dentry_automount,
1333 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1336 /**********************************************************************
1337 * AFS Linux inode operations
1338 **********************************************************************/
1342 * Merely need to set enough of vattr to get us through the create. Note
1343 * that the higher level code (open_namei) will take care of any tuncation
1344 * explicitly. Exclusive open is also taken care of in open_namei.
1346 * name is in kernel space at this point.
1349 #if defined(IOP_CREATE_TAKES_BOOL)
1350 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1352 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1353 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1354 struct nameidata *nd)
1355 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1356 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1357 struct nameidata *nd)
1359 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1363 cred_t *credp = crref();
1364 const char *name = dp->d_name.name;
1369 vattr.va_mode = mode;
1370 vattr.va_type = mode & S_IFMT;
1373 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1377 struct inode *ip = AFSTOV(vcp);
1379 afs_getattr(vcp, &vattr, credp);
1380 afs_fill_inode(ip, &vattr);
1381 insert_inode_hash(ip);
1382 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1383 dp->d_op = &afs_dentry_operations;
1385 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1386 d_instantiate(dp, ip);
1391 return afs_convert_code(code);
1394 /* afs_linux_lookup */
1395 static struct dentry *
1396 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1397 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1399 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1400 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1401 struct nameidata *nd)
1403 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1406 cred_t *credp = crref();
1407 struct vcache *vcp = NULL;
1408 const char *comp = dp->d_name.name;
1409 struct inode *ip = NULL;
1410 struct dentry *newdp = NULL;
1414 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1418 struct vcache *parent_vc = VTOAFS(dip);
1420 if (parent_vc == vcp) {
1421 /* This is possible if the parent dir is a mountpoint to a volume,
1422 * and the dir entry we looked up is a mountpoint to the same
1423 * volume. Linux cannot cope with this, so return an error instead
1424 * of risking a deadlock or panic. */
1432 afs_getattr(vcp, &vattr, credp);
1433 afs_fill_inode(ip, &vattr);
1434 if (hlist_unhashed(&ip->i_hash))
1435 insert_inode_hash(ip);
1437 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1438 dp->d_op = &afs_dentry_operations;
1440 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1443 if (ip && S_ISDIR(ip->i_mode)) {
1444 d_prune_aliases(ip);
1446 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1447 ip->i_flags |= S_AUTOMOUNT;
1450 newdp = d_splice_alias(ip, dp);
1455 /* It's ok for the file to not be found. That's noted by the caller by
1456 * seeing that the dp->d_inode field is NULL.
1458 if (!code || code == ENOENT)
1461 return ERR_PTR(afs_convert_code(code));
1465 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1468 cred_t *credp = crref();
1469 const char *name = newdp->d_name.name;
1470 struct inode *oldip = olddp->d_inode;
1472 /* If afs_link returned the vnode, we could instantiate the
1473 * dentry. Since it's not, we drop this one and do a new lookup.
1478 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1482 return afs_convert_code(code);
1485 /* We have to have a Linux specific sillyrename function, because we
1486 * also have to keep the dcache up to date when we're doing a silly
1487 * rename - so we don't want the generic vnodeops doing this behind our
1492 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1495 struct vcache *tvc = VTOAFS(dentry->d_inode);
1496 struct dentry *__dp = NULL;
1497 char *__name = NULL;
1500 if (afs_linux_nfsfs_renamed(dentry))
1508 osi_FreeSmallSpace(__name);
1509 __name = afs_newname();
1512 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1515 osi_FreeSmallSpace(__name);
1518 } while (__dp->d_inode != NULL);
1521 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1522 VTOAFS(dir), (char *)__dp->d_name.name,
1525 tvc->mvid = (void *) __name;
1528 crfree(tvc->uncred);
1530 tvc->uncred = credp;
1531 tvc->f.states |= CUnlinked;
1532 afs_linux_set_nfsfs_renamed(dentry);
1534 osi_FreeSmallSpace(__name);
1539 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1540 d_move(dentry, __dp);
1549 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1552 cred_t *credp = crref();
1553 const char *name = dp->d_name.name;
1554 struct vcache *tvc = VTOAFS(dp->d_inode);
1556 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1557 && !(tvc->f.states & CUnlinked)) {
1559 code = afs_linux_sillyrename(dip, dp, credp);
1562 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1569 return afs_convert_code(code);
1574 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1577 cred_t *credp = crref();
1579 const char *name = dp->d_name.name;
1581 /* If afs_symlink returned the vnode, we could instantiate the
1582 * dentry. Since it's not, we drop this one and do a new lookup.
1588 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1591 return afs_convert_code(code);
1595 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1596 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1598 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1602 cred_t *credp = crref();
1603 struct vcache *tvcp = NULL;
1605 const char *name = dp->d_name.name;
1608 vattr.va_mask = ATTR_MODE;
1609 vattr.va_mode = mode;
1611 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1614 struct inode *ip = AFSTOV(tvcp);
1616 afs_getattr(tvcp, &vattr, credp);
1617 afs_fill_inode(ip, &vattr);
1619 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1620 dp->d_op = &afs_dentry_operations;
1622 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1623 d_instantiate(dp, ip);
1628 return afs_convert_code(code);
1632 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1635 cred_t *credp = crref();
1636 const char *name = dp->d_name.name;
1638 /* locking kernel conflicts with glock? */
1641 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1644 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1645 * that failed because a directory is not empty. So, we map
1646 * EEXIST to ENOTEMPTY on linux.
1648 if (code == EEXIST) {
1657 return afs_convert_code(code);
1662 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1663 struct inode *newip, struct dentry *newdp)
1666 cred_t *credp = crref();
1667 const char *oldname = olddp->d_name.name;
1668 const char *newname = newdp->d_name.name;
1669 struct dentry *rehash = NULL;
1671 /* Prevent any new references during rename operation. */
1673 if (!d_unhashed(newdp)) {
1678 #if defined(D_COUNT_INT)
1679 spin_lock(&olddp->d_lock);
1680 if (olddp->d_count > 1) {
1681 spin_unlock(&olddp->d_lock);
1682 shrink_dcache_parent(olddp);
1684 spin_unlock(&olddp->d_lock);
1686 if (atomic_read(&olddp->d_count) > 1)
1687 shrink_dcache_parent(olddp);
1691 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1695 olddp->d_time = 0; /* force to revalidate */
1701 return afs_convert_code(code);
1705 /* afs_linux_ireadlink
1706 * Internal readlink which can return link contents to user or kernel space.
1707 * Note that the buffer is NOT supposed to be null-terminated.
1710 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1713 cred_t *credp = crref();
1717 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1718 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1722 return maxlen - tuio.uio_resid;
1724 return afs_convert_code(code);
1727 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1728 /* afs_linux_readlink
1729 * Fill target (which is in user space) with contents of symlink.
1732 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1735 struct inode *ip = dp->d_inode;
1738 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1744 /* afs_linux_follow_link
1745 * a file system dependent link following routine.
1747 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1752 name = kmalloc(PATH_MAX, GFP_NOFS);
1758 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1766 nd_set_link(nd, name);
1771 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1773 char *name = nd_get_link(nd);
1775 if (name && !IS_ERR(name))
1779 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1781 /* Populate a page by filling it from the cache file pointed at by cachefp
1782 * (which contains indicated chunk)
1783 * If task is NULL, the page copy occurs syncronously, and the routine
1784 * returns with page still locked. If task is non-NULL, then page copies
1785 * may occur in the background, and the page will be unlocked when it is
1789 afs_linux_read_cache(struct file *cachefp, struct page *page,
1790 int chunk, struct pagevec *lrupv,
1791 struct afs_pagecopy_task *task) {
1792 loff_t offset = page_offset(page);
1793 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1794 struct page *newpage, *cachepage;
1795 struct address_space *cachemapping;
1799 cachemapping = cacheinode->i_mapping;
1803 /* If we're trying to read a page that's past the end of the disk
1804 * cache file, then just return a zeroed page */
1805 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1806 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1807 SetPageUptodate(page);
1813 /* From our offset, we now need to work out which page in the disk
1814 * file it corresponds to. This will be fun ... */
1815 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1817 while (cachepage == NULL) {
1818 cachepage = find_get_page(cachemapping, pageindex);
1821 newpage = page_cache_alloc_cold(cachemapping);
1827 code = add_to_page_cache(newpage, cachemapping,
1828 pageindex, GFP_KERNEL);
1830 cachepage = newpage;
1833 page_cache_get(cachepage);
1834 if (!pagevec_add(lrupv, cachepage))
1835 __pagevec_lru_add_file(lrupv);
1838 page_cache_release(newpage);
1840 if (code != -EEXIST)
1844 lock_page(cachepage);
1848 if (!PageUptodate(cachepage)) {
1849 ClearPageError(cachepage);
1850 code = cachemapping->a_ops->readpage(NULL, cachepage);
1851 if (!code && !task) {
1852 wait_on_page_locked(cachepage);
1855 unlock_page(cachepage);
1859 if (PageUptodate(cachepage)) {
1860 copy_highpage(page, cachepage);
1861 flush_dcache_page(page);
1862 SetPageUptodate(page);
1867 afs_pagecopy_queue_page(task, cachepage, page);
1879 page_cache_release(cachepage);
1885 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1887 loff_t offset = page_offset(pp);
1888 struct inode *ip = FILE_INODE(fp);
1889 struct vcache *avc = VTOAFS(ip);
1891 struct file *cacheFp = NULL;
1894 struct pagevec lrupv;
1896 /* Not a UFS cache, don't do anything */
1897 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1900 /* No readpage (ex: tmpfs) , skip */
1901 if (cachefs_noreadpage)
1904 /* Can't do anything if the vcache isn't statd , or if the read
1905 * crosses a chunk boundary.
1907 if (!(avc->f.states & CStatd) ||
1908 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1912 ObtainWriteLock(&avc->lock, 911);
1914 /* XXX - See if hinting actually makes things faster !!! */
1916 /* See if we have a suitable entry already cached */
1920 /* We need to lock xdcache, then dcache, to handle situations where
1921 * the hint is on the free list. However, we can't safely do this
1922 * according to the locking hierarchy. So, use a non blocking lock.
1924 ObtainReadLock(&afs_xdcache);
1925 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1927 if (dcLocked && (tdc->index != NULLIDX)
1928 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1929 && tdc->f.chunk == AFS_CHUNK(offset)
1930 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1931 /* Bonus - the hint was correct */
1934 /* Only destroy the hint if its actually invalid, not if there's
1935 * just been a locking failure */
1937 ReleaseReadLock(&tdc->lock);
1944 ReleaseReadLock(&afs_xdcache);
1947 /* No hint, or hint is no longer valid - see if we can get something
1948 * directly from the dcache
1951 tdc = afs_FindDCache(avc, offset);
1954 ReleaseWriteLock(&avc->lock);
1959 ObtainReadLock(&tdc->lock);
1961 /* Is the dcache we've been given currently up to date */
1962 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1963 (tdc->dflags & DFFetching))
1966 /* Update our hint for future abuse */
1969 /* Okay, so we've now got a cache file that is up to date */
1971 /* XXX - I suspect we should be locking the inodes before we use them! */
1973 cacheFp = afs_linux_raw_open(&tdc->f.inode);
1974 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
1975 cachefs_noreadpage = 1;
1979 pagevec_init(&lrupv, 0);
1981 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1983 if (pagevec_count(&lrupv))
1984 __pagevec_lru_add_file(&lrupv);
1986 filp_close(cacheFp, NULL);
1989 ReleaseReadLock(&tdc->lock);
1990 ReleaseWriteLock(&avc->lock);
1997 ReleaseWriteLock(&avc->lock);
1998 ReleaseReadLock(&tdc->lock);
2003 /* afs_linux_readpage
2005 * This function is split into two, because prepare_write/begin_write
2006 * require a readpage call which doesn't unlock the resulting page upon
2010 afs_linux_fillpage(struct file *fp, struct page *pp)
2015 struct iovec *iovecp;
2016 struct inode *ip = FILE_INODE(fp);
2017 afs_int32 cnt = page_count(pp);
2018 struct vcache *avc = VTOAFS(ip);
2019 afs_offs_t offset = page_offset(pp);
2023 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2033 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2034 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2036 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2041 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2042 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2043 99999); /* not a possible code value */
2045 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2047 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2048 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2050 AFS_DISCON_UNLOCK();
2053 /* XXX valid for no-cache also? Check last bits of files... :)
2054 * Cognate code goes in afs_NoCacheFetchProc. */
2055 if (auio->uio_resid) /* zero remainder of page */
2056 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2059 flush_dcache_page(pp);
2060 SetPageUptodate(pp);
2069 return afs_convert_code(code);
2073 afs_linux_prefetch(struct file *fp, struct page *pp)
2076 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2077 afs_offs_t offset = page_offset(pp);
2079 if (AFS_CHUNKOFFSET(offset) == 0) {
2081 struct vrequest treq;
2086 code = afs_InitReq(&treq, credp);
2087 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2088 tdc = afs_FindDCache(avc, offset);
2090 if (!(tdc->mflags & DFNextStarted))
2091 afs_PrefetchChunk(avc, tdc, credp, &treq);
2094 ReleaseWriteLock(&avc->lock);
2099 return afs_convert_code(code);
2104 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2105 struct list_head *page_list, unsigned num_pages)
2110 struct iovec* iovecp;
2111 struct nocache_read_request *ancr;
2113 struct pagevec lrupv;
2117 struct inode *ip = FILE_INODE(fp);
2118 struct vcache *avc = VTOAFS(ip);
2119 afs_int32 base_index = 0;
2120 afs_int32 page_count = 0;
2123 /* background thread must free: iovecp, auio, ancr */
2124 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2126 auio = osi_Alloc(sizeof(struct uio));
2127 auio->uio_iov = iovecp;
2128 auio->uio_iovcnt = num_pages;
2129 auio->uio_flag = UIO_READ;
2130 auio->uio_seg = AFS_UIOSYS;
2131 auio->uio_resid = num_pages * PAGE_SIZE;
2133 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2135 ancr->offset = auio->uio_offset;
2136 ancr->length = auio->uio_resid;
2138 pagevec_init(&lrupv, 0);
2140 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2142 if(list_empty(page_list))
2145 pp = list_entry(page_list->prev, struct page, lru);
2146 /* If we allocate a page and don't remove it from page_list,
2147 * the page cache gets upset. */
2149 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
2150 if(pp->index > isize) {
2157 offset = page_offset(pp);
2158 ancr->offset = auio->uio_offset = offset;
2159 base_index = pp->index;
2161 iovecp[page_ix].iov_len = PAGE_SIZE;
2162 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2163 if(base_index != pp->index) {
2166 page_cache_release(pp);
2167 iovecp[page_ix].iov_base = (void *) 0;
2169 ancr->length -= PAGE_SIZE;
2176 page_cache_release(pp);
2177 iovecp[page_ix].iov_base = (void *) 0;
2180 if(!PageLocked(pp)) {
2184 /* increment page refcount--our original design assumed
2185 * that locking it would effectively pin it; protect
2186 * ourselves from the possiblity that this assumption is
2187 * is faulty, at low cost (provided we do not fail to
2188 * do the corresponding decref on the other side) */
2191 /* save the page for background map */
2192 iovecp[page_ix].iov_base = (void*) pp;
2194 /* and put it on the LRU cache */
2195 if (!pagevec_add(&lrupv, pp))
2196 __pagevec_lru_add_file(&lrupv);
2200 /* If there were useful pages in the page list, make sure all pages
2201 * are in the LRU cache, then schedule the read */
2203 if (pagevec_count(&lrupv))
2204 __pagevec_lru_add_file(&lrupv);
2206 code = afs_ReadNoCache(avc, ancr, credp);
2209 /* If there is nothing for the background thread to handle,
2210 * it won't be freeing the things that we never gave it */
2211 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2212 osi_Free(auio, sizeof(struct uio));
2213 osi_Free(ancr, sizeof(struct nocache_read_request));
2215 /* we do not flush, release, or unmap pages--that will be
2216 * done for us by the background thread as each page comes in
2217 * from the fileserver */
2218 return afs_convert_code(code);
2223 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2225 cred_t *credp = NULL;
2227 struct iovec *iovecp;
2228 struct nocache_read_request *ancr;
2232 * Special case: if page is at or past end of file, just zero it and set
2235 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2236 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
2237 SetPageUptodate(pp);
2244 /* receiver frees */
2245 auio = osi_Alloc(sizeof(struct uio));
2246 iovecp = osi_Alloc(sizeof(struct iovec));
2248 /* address can be NULL, because we overwrite it with 'pp', below */
2249 setup_uio(auio, iovecp, NULL, page_offset(pp),
2250 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2252 /* save the page for background map */
2253 get_page(pp); /* see above */
2254 auio->uio_iov->iov_base = (void*) pp;
2255 /* the background thread will free this */
2256 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2258 ancr->offset = page_offset(pp);
2259 ancr->length = PAGE_SIZE;
2262 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2265 return afs_convert_code(code);
2269 afs_linux_can_bypass(struct inode *ip) {
2271 switch(cache_bypass_strategy) {
2272 case NEVER_BYPASS_CACHE:
2274 case ALWAYS_BYPASS_CACHE:
2276 case LARGE_FILES_BYPASS_CACHE:
2277 if (i_size_read(ip) > cache_bypass_threshold)
2284 /* Check if a file is permitted to bypass the cache by policy, and modify
2285 * the cache bypass state recorded for that file */
2288 afs_linux_bypass_check(struct inode *ip) {
2291 int bypass = afs_linux_can_bypass(ip);
2294 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2302 afs_linux_readpage(struct file *fp, struct page *pp)
2306 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2307 code = afs_linux_bypass_readpage(fp, pp);
2309 code = afs_linux_fillpage(fp, pp);
2311 code = afs_linux_prefetch(fp, pp);
2318 /* Readpages reads a number of pages for a particular file. We use
2319 * this to optimise the reading, by limiting the number of times upon which
2320 * we have to lookup, lock and open vcaches and dcaches
2324 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2325 struct list_head *page_list, unsigned int num_pages)
2327 struct inode *inode = mapping->host;
2328 struct vcache *avc = VTOAFS(inode);
2330 struct file *cacheFp = NULL;
2332 unsigned int page_idx;
2334 struct pagevec lrupv;
2335 struct afs_pagecopy_task *task;
2337 if (afs_linux_bypass_check(inode))
2338 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2340 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2343 /* No readpage (ex: tmpfs) , skip */
2344 if (cachefs_noreadpage)
2348 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2353 ObtainWriteLock(&avc->lock, 912);
2356 task = afs_pagecopy_init_task();
2359 pagevec_init(&lrupv, 0);
2360 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2361 struct page *page = list_entry(page_list->prev, struct page, lru);
2362 list_del(&page->lru);
2363 offset = page_offset(page);
2365 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2367 ReleaseReadLock(&tdc->lock);
2372 filp_close(cacheFp, NULL);
2377 if ((tdc = afs_FindDCache(avc, offset))) {
2378 ObtainReadLock(&tdc->lock);
2379 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2380 (tdc->dflags & DFFetching)) {
2382 ReleaseReadLock(&tdc->lock);
2389 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2390 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2391 cachefs_noreadpage = 1;
2397 if (tdc && !add_to_page_cache(page, mapping, page->index,
2399 page_cache_get(page);
2400 if (!pagevec_add(&lrupv, page))
2401 __pagevec_lru_add_file(&lrupv);
2403 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2405 page_cache_release(page);
2407 if (pagevec_count(&lrupv))
2408 __pagevec_lru_add_file(&lrupv);
2412 filp_close(cacheFp, NULL);
2414 afs_pagecopy_put_task(task);
2418 ReleaseReadLock(&tdc->lock);
2422 ReleaseWriteLock(&avc->lock);
2427 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2430 afs_linux_prepare_writeback(struct vcache *avc) {
2431 if (avc->f.states & CPageWrite) {
2432 return AOP_WRITEPAGE_ACTIVATE;
2434 avc->f.states |= CPageWrite;
2439 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2440 struct vrequest treq;
2443 if (!afs_InitReq(&treq, credp))
2444 code = afs_DoPartialWrite(avc, &treq);
2446 return afs_convert_code(code);
2450 afs_linux_complete_writeback(struct vcache *avc) {
2451 avc->f.states &= ~CPageWrite;
2454 /* Writeback a given page syncronously. Called with no AFS locks held */
2456 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2457 unsigned long offset, unsigned int count,
2460 struct vcache *vcp = VTOAFS(ip);
2468 buffer = kmap(pp) + offset;
2469 base = page_offset(pp) + offset;
2472 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2473 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2474 ICL_TYPE_INT32, 99999);
2476 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2478 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2480 i_size_write(ip, vcp->f.m.Length);
2481 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2483 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2485 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2486 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2487 ICL_TYPE_INT32, code);
2496 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2497 unsigned long offset, unsigned int count)
2501 struct vcache *vcp = VTOAFS(ip);
2504 /* Catch recursive writeback. This occurs if the kernel decides
2505 * writeback is required whilst we are writing to the cache, or
2506 * flushing to the server. When we're running syncronously (as
2507 * opposed to from writepage) we can't actually do anything about
2508 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2511 ObtainWriteLock(&vcp->lock, 532);
2512 afs_linux_prepare_writeback(vcp);
2513 ReleaseWriteLock(&vcp->lock);
2517 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2520 ObtainWriteLock(&vcp->lock, 533);
2522 code1 = afs_linux_dopartialwrite(vcp, credp);
2523 afs_linux_complete_writeback(vcp);
2524 ReleaseWriteLock(&vcp->lock);
2535 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2536 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2538 afs_linux_writepage(struct page *pp)
2541 struct address_space *mapping = pp->mapping;
2542 struct inode *inode;
2545 unsigned int to = PAGE_CACHE_SIZE;
2550 if (PageReclaim(pp)) {
2551 return AOP_WRITEPAGE_ACTIVATE;
2552 /* XXX - Do we need to redirty the page here? */
2557 inode = mapping->host;
2558 vcp = VTOAFS(inode);
2559 isize = i_size_read(inode);
2561 /* Don't defeat an earlier truncate */
2562 if (page_offset(pp) > isize) {
2563 set_page_writeback(pp);
2569 ObtainWriteLock(&vcp->lock, 537);
2570 code = afs_linux_prepare_writeback(vcp);
2571 if (code == AOP_WRITEPAGE_ACTIVATE) {
2572 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2573 * to return with the page still locked */
2574 ReleaseWriteLock(&vcp->lock);
2579 /* Grab the creds structure currently held in the vnode, and
2580 * get a reference to it, in case it goes away ... */
2586 ReleaseWriteLock(&vcp->lock);
2589 set_page_writeback(pp);
2591 SetPageUptodate(pp);
2593 /* We can unlock the page here, because it's protected by the
2594 * page_writeback flag. This should make us less vulnerable to
2595 * deadlocking in afs_write and afs_DoPartialWrite
2599 /* If this is the final page, then just write the number of bytes that
2600 * are actually in it */
2601 if ((isize - page_offset(pp)) < to )
2602 to = isize - page_offset(pp);
2604 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2607 ObtainWriteLock(&vcp->lock, 538);
2609 /* As much as we might like to ignore a file server error here,
2610 * and just try again when we close(), unfortunately StoreAllSegments
2611 * will invalidate our chunks if the server returns a permanent error,
2612 * so we need to at least try and get that error back to the user
2615 code1 = afs_linux_dopartialwrite(vcp, credp);
2617 afs_linux_complete_writeback(vcp);
2618 ReleaseWriteLock(&vcp->lock);
2623 end_page_writeback(pp);
2624 page_cache_release(pp);
2635 /* afs_linux_permission
2636 * Check access rights - returns error if can't check or permission denied.
2639 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2640 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2641 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2642 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2644 afs_linux_permission(struct inode *ip, int mode)
2651 /* Check for RCU path walking */
2652 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2653 if (flags & IPERM_FLAG_RCU)
2655 #elif defined(MAY_NOT_BLOCK)
2656 if (mode & MAY_NOT_BLOCK)
2662 if (mode & MAY_EXEC)
2664 if (mode & MAY_READ)
2666 if (mode & MAY_WRITE)
2668 code = afs_access(VTOAFS(ip), tmp, credp);
2672 return afs_convert_code(code);
2676 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2680 struct inode *inode = FILE_INODE(file);
2681 loff_t pagebase = page_offset(page);
2683 if (i_size_read(inode) < (pagebase + offset))
2684 i_size_write(inode, pagebase + offset);
2686 if (PageChecked(page)) {
2687 SetPageUptodate(page);
2688 ClearPageChecked(page);
2691 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2697 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2701 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2702 * prepare_write. Essentially, if the page exists within the file,
2703 * and is not being fully written, then we should populate it.
2706 if (!PageUptodate(page)) {
2707 loff_t pagebase = page_offset(page);
2708 loff_t isize = i_size_read(page->mapping->host);
2710 /* Is the location we are writing to beyond the end of the file? */
2711 if (pagebase >= isize ||
2712 ((from == 0) && (pagebase + to) >= isize)) {
2713 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2714 SetPageChecked(page);
2715 /* Are we we writing a full page */
2716 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2717 SetPageChecked(page);
2718 /* Is the page readable, if it's wronly, we don't care, because we're
2719 * not actually going to read from it ... */
2720 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2721 /* We don't care if fillpage fails, because if it does the page
2722 * won't be marked as up to date
2724 afs_linux_fillpage(file, page);
2730 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2732 afs_linux_write_end(struct file *file, struct address_space *mapping,
2733 loff_t pos, unsigned len, unsigned copied,
2734 struct page *page, void *fsdata)
2737 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2739 code = afs_linux_commit_write(file, page, from, from + len);
2742 page_cache_release(page);
2747 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2748 loff_t pos, unsigned len, unsigned flags,
2749 struct page **pagep, void **fsdata)
2752 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2753 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2756 page = grab_cache_page_write_begin(mapping, index, flags);
2759 code = afs_linux_prepare_write(file, page, from, from + len);
2762 page_cache_release(page);
2769 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2771 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2773 struct dentry **dpp;
2774 struct dentry *target;
2776 if (current->total_link_count > 0) {
2777 /* avoid symlink resolution limits when resolving; we cannot contribute to
2778 * an infinite symlink loop */
2779 /* only do this for follow_link when total_link_count is positive to be
2780 * on the safe side; there is at least one code path in the Linux
2781 * kernel where it seems like it may be possible to get here without
2782 * total_link_count getting incremented. it is not clear on how that
2783 * path is actually reached, but guard against it just to be safe */
2784 current->total_link_count--;
2787 target = canonical_dentry(dentry->d_inode);
2789 # ifdef STRUCT_NAMEIDATA_HAS_PATH
2790 dpp = &nd->path.dentry;
2800 *dpp = dget(dentry);
2803 nd->last_type = LAST_BIND;
2807 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
2810 static struct inode_operations afs_file_iops = {
2811 .permission = afs_linux_permission,
2812 .getattr = afs_linux_getattr,
2813 .setattr = afs_notify_change,
2816 static struct address_space_operations afs_file_aops = {
2817 .readpage = afs_linux_readpage,
2818 .readpages = afs_linux_readpages,
2819 .writepage = afs_linux_writepage,
2820 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2821 .write_begin = afs_linux_write_begin,
2822 .write_end = afs_linux_write_end,
2824 .commit_write = afs_linux_commit_write,
2825 .prepare_write = afs_linux_prepare_write,
2830 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2831 * by what sort of operation is allowed.....
2834 static struct inode_operations afs_dir_iops = {
2835 .setattr = afs_notify_change,
2836 .create = afs_linux_create,
2837 .lookup = afs_linux_lookup,
2838 .link = afs_linux_link,
2839 .unlink = afs_linux_unlink,
2840 .symlink = afs_linux_symlink,
2841 .mkdir = afs_linux_mkdir,
2842 .rmdir = afs_linux_rmdir,
2843 .rename = afs_linux_rename,
2844 .getattr = afs_linux_getattr,
2845 .permission = afs_linux_permission,
2846 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2847 .follow_link = afs_linux_dir_follow_link,
2851 /* We really need a separate symlink set of ops, since do_follow_link()
2852 * determines if it _is_ a link by checking if the follow_link op is set.
2854 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2856 afs_symlink_filler(struct file *file, struct page *page)
2858 struct inode *ip = (struct inode *)page->mapping->host;
2859 char *p = (char *)kmap(page);
2863 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2868 p[code] = '\0'; /* null terminate? */
2870 SetPageUptodate(page);
2882 static struct address_space_operations afs_symlink_aops = {
2883 .readpage = afs_symlink_filler
2885 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2887 static struct inode_operations afs_symlink_iops = {
2888 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2889 .readlink = page_readlink,
2890 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2891 .follow_link = page_follow_link,
2893 .follow_link = page_follow_link_light,
2894 .put_link = page_put_link,
2896 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2897 .readlink = afs_linux_readlink,
2898 .follow_link = afs_linux_follow_link,
2899 .put_link = afs_linux_put_link,
2900 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2901 .setattr = afs_notify_change,
2905 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2909 vattr2inode(ip, vattr);
2911 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2912 /* Reset ops if symlink or directory. */
2913 if (S_ISREG(ip->i_mode)) {
2914 ip->i_op = &afs_file_iops;
2915 ip->i_fop = &afs_file_fops;
2916 ip->i_data.a_ops = &afs_file_aops;
2918 } else if (S_ISDIR(ip->i_mode)) {
2919 ip->i_op = &afs_dir_iops;
2920 ip->i_fop = &afs_dir_fops;
2922 } else if (S_ISLNK(ip->i_mode)) {
2923 ip->i_op = &afs_symlink_iops;
2924 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2925 ip->i_data.a_ops = &afs_symlink_aops;
2926 ip->i_mapping = &ip->i_data;