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 if (!(avc->f.states & CCorrupt)) {
370 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
371 afs_warn("Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)",
372 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
373 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
374 tc ? tc->cellName : "",
375 (unsigned long)&tdc->f.inode, dirpos);
377 afs_PutCell(tc, READ_LOCK);
378 UpgradeSToWLock(&avc->lock, 814);
379 avc->f.states |= CCorrupt;
385 de = (struct DirEntry *)entry.data;
386 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
387 ntohl(de->fid.vnode));
388 len = strlen(de->name);
390 /* filldir returns -EINVAL when the buffer is full. */
392 unsigned int type = DT_UNKNOWN;
393 struct VenusFid afid;
396 afid.Cell = avc->f.fid.Cell;
397 afid.Fid.Volume = avc->f.fid.Fid.Volume;
398 afid.Fid.Vnode = ntohl(de->fid.vnode);
399 afid.Fid.Unique = ntohl(de->fid.vunique);
400 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
402 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
405 } else if (((tvc->f.states) & (CStatd | CTruth))) {
406 /* CTruth will be set if the object has
411 else if (vtype == VREG)
413 /* Don't do this until we're sure it can't be a mtpt */
414 /* else if (vtype == VLNK)
416 /* what other types does AFS support? */
418 /* clean up from afs_FindVCache */
422 * If this is NFS readdirplus, then the filler is going to
423 * call getattr on this inode, which will deadlock if we're
427 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
428 /* dir_emit returns a bool - true when it succeeds.
429 * Inverse the result to fit with how we check "code" */
430 code = !dir_emit(ctx, de->name, len, ino, type);
432 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
439 offset = dirpos + 1 + ((len + 16) >> 5);
441 /* If filldir didn't fill in the last one this is still pointing to that
447 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
448 ctx->pos = (loff_t) offset;
450 fp->f_pos = (loff_t) offset;
452 ReleaseReadLock(&tdc->lock);
454 UpgradeSToWLock(&avc->lock, 813);
455 avc->f.states &= ~CReadDir;
457 avc->readdir_pid = 0;
458 ReleaseSharedLock(&avc->lock);
461 afs_PutFakeStat(&fakestat);
468 /* in afs_pioctl.c */
469 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
472 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
473 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
475 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
482 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
484 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
488 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
489 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
490 vmap->vm_end - vmap->vm_start);
492 /* get a validated vcache entry */
493 code = afs_linux_VerifyVCache(vcp, NULL);
496 /* Linux's Flushpage implementation doesn't use credp, so optimise
497 * our code to not need to crref() it */
498 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
500 code = generic_file_mmap(fp, vmap);
503 vcp->f.states |= CMAPPED;
511 afs_linux_open(struct inode *ip, struct file *fp)
513 struct vcache *vcp = VTOAFS(ip);
514 cred_t *credp = crref();
518 code = afs_open(&vcp, fp->f_flags, credp);
522 return afs_convert_code(code);
526 afs_linux_release(struct inode *ip, struct file *fp)
528 struct vcache *vcp = VTOAFS(ip);
529 cred_t *credp = crref();
533 code = afs_close(vcp, fp->f_flags, credp);
534 ObtainWriteLock(&vcp->lock, 807);
539 ReleaseWriteLock(&vcp->lock);
543 return afs_convert_code(code);
547 #if defined(FOP_FSYNC_TAKES_DENTRY)
548 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
549 #elif defined(FOP_FSYNC_TAKES_RANGE)
550 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
552 afs_linux_fsync(struct file *fp, int datasync)
556 struct inode *ip = FILE_INODE(fp);
557 cred_t *credp = crref();
559 #if defined(FOP_FSYNC_TAKES_RANGE)
560 mutex_lock(&ip->i_mutex);
563 code = afs_fsync(VTOAFS(ip), credp);
565 #if defined(FOP_FSYNC_TAKES_RANGE)
566 mutex_unlock(&ip->i_mutex);
569 return afs_convert_code(code);
575 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
578 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
579 cred_t *credp = crref();
580 struct AFS_FLOCK flock;
582 /* Convert to a lock format afs_lockctl understands. */
583 memset(&flock, 0, sizeof(flock));
584 flock.l_type = flp->fl_type;
585 flock.l_pid = flp->fl_pid;
587 flock.l_start = flp->fl_start;
588 if (flp->fl_end == OFFSET_MAX)
589 flock.l_len = 0; /* Lock to end of file */
591 flock.l_len = flp->fl_end - flp->fl_start + 1;
593 /* Safe because there are no large files, yet */
594 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
595 if (cmd == F_GETLK64)
597 else if (cmd == F_SETLK64)
599 else if (cmd == F_SETLKW64)
601 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
604 if ((vcp->f.states & CRO)) {
605 if (flp->fl_type == F_WRLCK) {
614 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
617 if ((code == 0 || flp->fl_type == F_UNLCK) &&
618 (cmd == F_SETLK || cmd == F_SETLKW)) {
619 code = afs_posix_lock_file(fp, flp);
620 if (code && flp->fl_type != F_UNLCK) {
621 struct AFS_FLOCK flock2;
623 flock2.l_type = F_UNLCK;
625 afs_lockctl(vcp, &flock2, F_SETLK, credp);
629 /* If lockctl says there are no conflicting locks, then also check with the
630 * kernel, as lockctl knows nothing about byte range locks
632 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
633 afs_posix_test_lock(fp, flp);
634 /* If we found a lock in the kernel's structure, return it */
635 if (flp->fl_type != F_UNLCK) {
641 /* Convert flock back to Linux's file_lock */
642 flp->fl_type = flock.l_type;
643 flp->fl_pid = flock.l_pid;
644 flp->fl_start = flock.l_start;
645 if (flock.l_len == 0)
646 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
648 flp->fl_end = flock.l_start + flock.l_len - 1;
654 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
656 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
658 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
659 cred_t *credp = crref();
660 struct AFS_FLOCK flock;
661 /* Convert to a lock format afs_lockctl understands. */
662 memset(&flock, 0, sizeof(flock));
663 flock.l_type = flp->fl_type;
664 flock.l_pid = flp->fl_pid;
669 /* Safe because there are no large files, yet */
670 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
671 if (cmd == F_GETLK64)
673 else if (cmd == F_SETLK64)
675 else if (cmd == F_SETLKW64)
677 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
680 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
683 if ((code == 0 || flp->fl_type == F_UNLCK) &&
684 (cmd == F_SETLK || cmd == F_SETLKW)) {
685 flp->fl_flags &=~ FL_SLEEP;
686 code = flock_lock_file_wait(fp, flp);
687 if (code && flp->fl_type != F_UNLCK) {
688 struct AFS_FLOCK flock2;
690 flock2.l_type = F_UNLCK;
692 afs_lockctl(vcp, &flock2, F_SETLK, credp);
696 /* Convert flock back to Linux's file_lock */
697 flp->fl_type = flock.l_type;
698 flp->fl_pid = flock.l_pid;
706 * essentially the same as afs_fsync() but we need to get the return
707 * code for the sys_close() here, not afs_linux_release(), so call
708 * afs_StoreAllSegments() with AFS_LASTSTORE
711 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
712 afs_linux_flush(struct file *fp, fl_owner_t id)
714 afs_linux_flush(struct file *fp)
717 struct vrequest treq;
725 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
733 vcp = VTOAFS(FILE_INODE(fp));
735 code = afs_InitReq(&treq, credp);
738 /* If caching is bypassed for this file, or globally, just return 0 */
739 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
742 ObtainReadLock(&vcp->lock);
743 if (vcp->cachingStates & FCSBypass)
745 ReleaseReadLock(&vcp->lock);
748 /* future proof: don't rely on 0 return from afs_InitReq */
753 ObtainSharedLock(&vcp->lock, 535);
754 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
755 UpgradeSToWLock(&vcp->lock, 536);
756 if (!AFS_IS_DISCONNECTED) {
757 code = afs_StoreAllSegments(vcp,
759 AFS_SYNC | AFS_LASTSTORE);
761 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
763 ConvertWToSLock(&vcp->lock);
765 code = afs_CheckCode(code, &treq, 54);
766 ReleaseSharedLock(&vcp->lock);
773 return afs_convert_code(code);
776 struct file_operations afs_dir_fops = {
777 .read = generic_read_dir,
778 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
779 .iterate = afs_linux_readdir,
781 .readdir = afs_linux_readdir,
783 #ifdef HAVE_UNLOCKED_IOCTL
784 .unlocked_ioctl = afs_unlocked_xioctl,
788 #ifdef HAVE_COMPAT_IOCTL
789 .compat_ioctl = afs_unlocked_xioctl,
791 .open = afs_linux_open,
792 .release = afs_linux_release,
793 .llseek = default_llseek,
794 #ifdef HAVE_LINUX_NOOP_FSYNC
797 .fsync = simple_sync_file,
801 struct file_operations afs_file_fops = {
802 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
803 .aio_read = afs_linux_aio_read,
804 .aio_write = afs_linux_aio_write,
805 .read = do_sync_read,
806 .write = do_sync_write,
808 .read = afs_linux_read,
809 .write = afs_linux_write,
811 #ifdef HAVE_UNLOCKED_IOCTL
812 .unlocked_ioctl = afs_unlocked_xioctl,
816 #ifdef HAVE_COMPAT_IOCTL
817 .compat_ioctl = afs_unlocked_xioctl,
819 .mmap = afs_linux_mmap,
820 .open = afs_linux_open,
821 .flush = afs_linux_flush,
822 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
823 .sendfile = generic_file_sendfile,
825 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
826 .splice_write = generic_file_splice_write,
827 .splice_read = generic_file_splice_read,
829 .release = afs_linux_release,
830 .fsync = afs_linux_fsync,
831 .lock = afs_linux_lock,
832 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
833 .flock = afs_linux_flock,
835 .llseek = default_llseek,
838 static struct dentry *
839 canonical_dentry(struct inode *ip)
841 struct vcache *vcp = VTOAFS(ip);
842 struct dentry *first = NULL, *ret = NULL, *cur;
843 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
844 struct hlist_node *p;
848 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
849 * otherwise, use the first dentry in ip->i_dentry.
850 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
852 /* note that vcp->target_link specifies which dentry to use, but we have
853 * no reference held on that dentry. so, we cannot use or dereference
854 * vcp->target_link itself, since it may have been freed. instead, we only
855 * use it to compare to pointers in the ip->i_dentry list. */
859 # ifdef HAVE_DCACHE_LOCK
860 spin_lock(&dcache_lock);
862 spin_lock(&ip->i_lock);
865 #if defined(D_ALIAS_IS_HLIST)
866 # if defined(HLIST_ITERATOR_NO_NODE)
867 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
869 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
872 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
875 if (!vcp->target_link || cur == vcp->target_link) {
888 vcp->target_link = ret;
890 # ifdef HAVE_DCACHE_LOCK
894 spin_unlock(&dcache_lock);
899 spin_unlock(&ip->i_lock);
905 /**********************************************************************
906 * AFS Linux dentry operations
907 **********************************************************************/
909 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
910 * that has its mvid (parent dir's fid) pointer set to the wrong directory
911 * due to being mounted in multiple points at once. fix_bad_parent()
912 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
913 * dotdotfid and mtpoint fid members.
915 * dp - dentry to be checked.
916 * credp - credentials
917 * vcp, pvc - item's and parent's vcache pointer
921 * This dentry's vcache's mvid will be set to the correct parent directory's
923 * This root vnode's volume will have its dotdotfid and mtpoint fids set
924 * to the correct parent and mountpoint fids.
928 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
930 struct vcache *avc = NULL;
932 /* force a lookup, so vcp->mvid is fixed up */
933 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
934 if (!avc || vcp != avc) { /* bad, very bad.. */
935 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
936 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
937 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
938 ICL_TYPE_POINTER, dp);
941 AFS_RELE(AFSTOV(avc));
946 /* afs_linux_revalidate
947 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
950 afs_linux_revalidate(struct dentry *dp)
953 struct vcache *vcp = VTOAFS(dp->d_inode);
957 if (afs_shuttingdown)
963 /* Make this a fast path (no crref), since it's called so often. */
964 if (vcp->states & CStatd) {
965 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
967 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
968 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
971 fix_bad_parent(dp); /* check and correct mvid */
980 /* This avoids the crref when we don't have to do it. Watch for
981 * changes in afs_getattr that don't get replicated here!
983 if (vcp->f.states & CStatd &&
984 (!afs_fakestat_enable || vcp->mvstat != 1) &&
986 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
987 code = afs_CopyOutAttrs(vcp, &vattr);
990 code = afs_getattr(vcp, &vattr, credp);
995 afs_fill_inode(AFSTOV(vcp), &vattr);
999 return afs_convert_code(code);
1003 * Set iattr data into vattr. Assume vattr cleared before call.
1006 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1008 vattrp->va_mask = iattrp->ia_valid;
1009 if (iattrp->ia_valid & ATTR_MODE)
1010 vattrp->va_mode = iattrp->ia_mode;
1011 if (iattrp->ia_valid & ATTR_UID)
1012 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1013 if (iattrp->ia_valid & ATTR_GID)
1014 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1015 if (iattrp->ia_valid & ATTR_SIZE)
1016 vattrp->va_size = iattrp->ia_size;
1017 if (iattrp->ia_valid & ATTR_ATIME) {
1018 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1019 vattrp->va_atime.tv_usec = 0;
1021 if (iattrp->ia_valid & ATTR_MTIME) {
1022 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1023 vattrp->va_mtime.tv_usec = 0;
1025 if (iattrp->ia_valid & ATTR_CTIME) {
1026 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1027 vattrp->va_ctime.tv_usec = 0;
1032 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1035 vattr2inode(struct inode *ip, struct vattr *vp)
1037 ip->i_ino = vp->va_nodeid;
1038 #ifdef HAVE_LINUX_SET_NLINK
1039 set_nlink(ip, vp->va_nlink);
1041 ip->i_nlink = vp->va_nlink;
1043 ip->i_blocks = vp->va_blocks;
1044 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1045 ip->i_blkbits = AFS_BLKBITS;
1047 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1048 ip->i_blksize = vp->va_blocksize;
1050 ip->i_rdev = vp->va_rdev;
1051 ip->i_mode = vp->va_mode;
1052 ip->i_uid = afs_make_kuid(vp->va_uid);
1053 ip->i_gid = afs_make_kgid(vp->va_gid);
1054 i_size_write(ip, vp->va_size);
1055 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1056 ip->i_atime.tv_nsec = 0;
1057 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1058 /* Set the mtime nanoseconds to the sysname generation number.
1059 * This convinces NFS clients that all directories have changed
1060 * any time the sysname list changes.
1062 ip->i_mtime.tv_nsec = afs_sysnamegen;
1063 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1064 ip->i_ctime.tv_nsec = 0;
1067 /* afs_notify_change
1068 * Linux version of setattr call. What to change is in the iattr struct.
1069 * We need to set bits in both the Linux inode as well as the vcache.
1072 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1075 cred_t *credp = crref();
1076 struct inode *ip = dp->d_inode;
1080 iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
1083 code = afs_setattr(VTOAFS(ip), &vattr, credp);
1085 afs_getattr(VTOAFS(ip), &vattr, credp);
1086 vattr2inode(ip, &vattr);
1090 return afs_convert_code(code);
1094 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1096 int err = afs_linux_revalidate(dentry);
1098 generic_fillattr(dentry->d_inode, stat);
1104 parent_vcache_dv(struct inode *inode, cred_t *credp)
1107 struct vcache *pvcp;
1110 * If parent is a mount point and we are using fakestat, we may need
1111 * to look at the fake vcache entry instead of what the vfs is giving
1112 * us. The fake entry is the one with the useful DataVersion.
1114 pvcp = VTOAFS(inode);
1115 if (pvcp->mvstat == 1 && afs_fakestat_enable) {
1116 struct vrequest treq;
1117 struct afs_fakestat_state fakestate;
1123 afs_InitReq(&treq, credp);
1124 afs_InitFakeStat(&fakestate);
1125 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1128 afs_PutFakeStat(&fakestate);
1130 return hgetlo(pvcp->f.m.DataVersion);
1133 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1134 * In kernels 2.2.10 and above, we are passed an additional flags var which
1135 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1136 * we are advised to follow the entry if it is a link or to make sure that
1137 * it is a directory. But since the kernel itself checks these possibilities
1138 * later on, we shouldn't have to do it until later. Perhaps in the future..
1140 * The code here assumes that on entry the global lock is not held
1143 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1144 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1145 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1146 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1148 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1152 cred_t *credp = NULL;
1153 struct vcache *vcp, *pvcp, *tvc = NULL;
1154 struct dentry *parent;
1156 struct afs_fakestat_state fakestate;
1159 afs_uint32 parent_dv;
1162 /* We don't support RCU path walking */
1163 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1164 if (flags & LOOKUP_RCU)
1166 if (nd->flags & LOOKUP_RCU)
1171 afs_InitFakeStat(&fakestate);
1174 vcp = VTOAFS(dp->d_inode);
1176 if (vcp == afs_globalVp)
1179 parent = dget_parent(dp);
1180 pvcp = VTOAFS(parent->d_inode);
1182 if ((vcp->mvstat == 1) || (vcp->mvstat == 2) ||
1183 (pvcp->mvstat == 1 && afs_fakestat_enable)) { /* need to lock */
1189 if (locked && vcp->mvstat == 1) { /* mount point */
1190 if (vcp->mvid && (vcp->f.states & CMValid)) {
1191 int tryEvalOnly = 0;
1193 struct vrequest treq;
1195 code = afs_InitReq(&treq, credp);
1197 (strcmp(dp->d_name.name, ".directory") == 0)) {
1201 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
1203 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
1204 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1205 /* a mount point, not yet replaced by its directory */
1211 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
1212 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
1213 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
1218 /* If the last looker changes, we should make sure the current
1219 * looker still has permission to examine this file. This would
1220 * always require a crref() which would be "slow".
1222 if (vcp->last_looker != treq.uid) {
1223 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1228 vcp->last_looker = treq.uid;
1232 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1234 /* If the parent's DataVersion has changed or the vnode
1235 * is longer valid, we need to do a full lookup. VerifyVCache
1236 * isn't enough since the vnode may have been renamed.
1239 if ((!locked) && (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) ) {
1245 if (locked && (parent_dv > dp->d_time || !(vcp->f.states & CStatd))) {
1248 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1249 if (!tvc || tvc != vcp) {
1251 /* Force unhash if name is known not to exist. */
1257 if (afs_getattr(vcp, &vattr, credp)) {
1262 vattr2inode(AFSTOV(vcp), &vattr);
1263 dp->d_time = parent_dv;
1266 /* should we always update the attributes at this point? */
1267 /* unlikely--the vcache entry hasn't changed */
1272 /* If this code is ever enabled, we should use dget_parent to handle
1273 * getting the parent, and dput() to dispose of it. See above for an
1275 pvcp = VTOAFS(dp->d_parent->d_inode);
1276 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1280 /* No change in parent's DataVersion so this negative
1281 * lookup is still valid. BUT, if a server is down a
1282 * negative lookup can result so there should be a
1283 * liftime as well. For now, always expire.
1296 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1298 /* we hold the global lock if we evaluated a mount point */
1306 * If we had a negative lookup for the name we want to forcibly
1307 * unhash the dentry.
1308 * Otherwise use d_invalidate which will not unhash it if still in use.
1311 shrink_dcache_parent(dp);
1320 if (have_submounts(dp))
1328 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1330 struct vcache *vcp = VTOAFS(ip);
1333 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1334 (void) afs_InactiveVCache(vcp, NULL);
1337 afs_linux_clear_nfsfs_renamed(dp);
1343 #if defined(DOP_D_DELETE_TAKES_CONST)
1344 afs_dentry_delete(const struct dentry *dp)
1346 afs_dentry_delete(struct dentry *dp)
1349 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1350 return 1; /* bad inode? */
1355 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1356 static struct vfsmount *
1357 afs_dentry_automount(afs_linux_path_t *path)
1359 struct dentry *target;
1361 /* avoid symlink resolution limits when resolving; we cannot contribute to
1362 * an infinite symlink loop */
1363 current->total_link_count--;
1365 target = canonical_dentry(path->dentry->d_inode);
1367 if (target == path->dentry) {
1374 path->dentry = target;
1377 spin_lock(&path->dentry->d_lock);
1378 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1379 spin_unlock(&path->dentry->d_lock);
1384 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1386 struct dentry_operations afs_dentry_operations = {
1387 .d_revalidate = afs_linux_dentry_revalidate,
1388 .d_delete = afs_dentry_delete,
1389 .d_iput = afs_dentry_iput,
1390 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1391 .d_automount = afs_dentry_automount,
1392 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1395 /**********************************************************************
1396 * AFS Linux inode operations
1397 **********************************************************************/
1401 * Merely need to set enough of vattr to get us through the create. Note
1402 * that the higher level code (open_namei) will take care of any tuncation
1403 * explicitly. Exclusive open is also taken care of in open_namei.
1405 * name is in kernel space at this point.
1408 #if defined(IOP_CREATE_TAKES_BOOL)
1409 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1411 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1412 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1413 struct nameidata *nd)
1414 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1415 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1416 struct nameidata *nd)
1418 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1422 cred_t *credp = crref();
1423 const char *name = dp->d_name.name;
1428 vattr.va_mode = mode;
1429 vattr.va_type = mode & S_IFMT;
1432 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1436 struct inode *ip = AFSTOV(vcp);
1438 afs_getattr(vcp, &vattr, credp);
1439 afs_fill_inode(ip, &vattr);
1440 insert_inode_hash(ip);
1441 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1442 dp->d_op = &afs_dentry_operations;
1444 dp->d_time = parent_vcache_dv(dip, credp);
1445 d_instantiate(dp, ip);
1450 return afs_convert_code(code);
1453 /* afs_linux_lookup */
1454 static struct dentry *
1455 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1456 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1458 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1459 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1460 struct nameidata *nd)
1462 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1465 cred_t *credp = crref();
1466 struct vcache *vcp = NULL;
1467 const char *comp = dp->d_name.name;
1468 struct inode *ip = NULL;
1469 struct dentry *newdp = NULL;
1473 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1477 struct vcache *parent_vc = VTOAFS(dip);
1479 if (parent_vc == vcp) {
1480 /* This is possible if the parent dir is a mountpoint to a volume,
1481 * and the dir entry we looked up is a mountpoint to the same
1482 * volume. Linux cannot cope with this, so return an error instead
1483 * of risking a deadlock or panic. */
1491 afs_getattr(vcp, &vattr, credp);
1492 afs_fill_inode(ip, &vattr);
1493 if (hlist_unhashed(&ip->i_hash))
1494 insert_inode_hash(ip);
1496 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1497 dp->d_op = &afs_dentry_operations;
1499 dp->d_time = parent_vcache_dv(dip, credp);
1503 if (ip && S_ISDIR(ip->i_mode)) {
1504 d_prune_aliases(ip);
1506 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1507 ip->i_flags |= S_AUTOMOUNT;
1510 newdp = d_splice_alias(ip, dp);
1515 /* It's ok for the file to not be found. That's noted by the caller by
1516 * seeing that the dp->d_inode field is NULL.
1518 if (!code || code == ENOENT)
1521 return ERR_PTR(afs_convert_code(code));
1525 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1528 cred_t *credp = crref();
1529 const char *name = newdp->d_name.name;
1530 struct inode *oldip = olddp->d_inode;
1532 /* If afs_link returned the vnode, we could instantiate the
1533 * dentry. Since it's not, we drop this one and do a new lookup.
1538 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1542 return afs_convert_code(code);
1545 /* We have to have a Linux specific sillyrename function, because we
1546 * also have to keep the dcache up to date when we're doing a silly
1547 * rename - so we don't want the generic vnodeops doing this behind our
1552 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1555 struct vcache *tvc = VTOAFS(dentry->d_inode);
1556 struct dentry *__dp = NULL;
1557 char *__name = NULL;
1560 if (afs_linux_nfsfs_renamed(dentry))
1568 osi_FreeSmallSpace(__name);
1569 __name = afs_newname();
1572 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1575 osi_FreeSmallSpace(__name);
1578 } while (__dp->d_inode != NULL);
1581 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1582 VTOAFS(dir), (char *)__dp->d_name.name,
1585 tvc->mvid = (void *) __name;
1588 crfree(tvc->uncred);
1590 tvc->uncred = credp;
1591 tvc->f.states |= CUnlinked;
1592 afs_linux_set_nfsfs_renamed(dentry);
1594 osi_FreeSmallSpace(__name);
1599 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1600 d_move(dentry, __dp);
1609 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1612 cred_t *credp = crref();
1613 const char *name = dp->d_name.name;
1614 struct vcache *tvc = VTOAFS(dp->d_inode);
1616 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1617 && !(tvc->f.states & CUnlinked)) {
1619 code = afs_linux_sillyrename(dip, dp, credp);
1622 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1629 return afs_convert_code(code);
1634 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1637 cred_t *credp = crref();
1639 const char *name = dp->d_name.name;
1641 /* If afs_symlink returned the vnode, we could instantiate the
1642 * dentry. Since it's not, we drop this one and do a new lookup.
1648 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, NULL,
1652 return afs_convert_code(code);
1656 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1657 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1659 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1663 cred_t *credp = crref();
1664 struct vcache *tvcp = NULL;
1666 const char *name = dp->d_name.name;
1669 vattr.va_mask = ATTR_MODE;
1670 vattr.va_mode = mode;
1672 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1675 struct inode *ip = AFSTOV(tvcp);
1677 afs_getattr(tvcp, &vattr, credp);
1678 afs_fill_inode(ip, &vattr);
1680 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1681 dp->d_op = &afs_dentry_operations;
1683 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1684 d_instantiate(dp, ip);
1689 return afs_convert_code(code);
1693 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1696 cred_t *credp = crref();
1697 const char *name = dp->d_name.name;
1699 /* locking kernel conflicts with glock? */
1702 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1705 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1706 * that failed because a directory is not empty. So, we map
1707 * EEXIST to ENOTEMPTY on linux.
1709 if (code == EEXIST) {
1718 return afs_convert_code(code);
1723 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1724 struct inode *newip, struct dentry *newdp)
1727 cred_t *credp = crref();
1728 const char *oldname = olddp->d_name.name;
1729 const char *newname = newdp->d_name.name;
1730 struct dentry *rehash = NULL;
1732 /* Prevent any new references during rename operation. */
1734 if (!d_unhashed(newdp)) {
1739 afs_maybe_shrink_dcache(olddp);
1742 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1746 olddp->d_time = 0; /* force to revalidate */
1752 return afs_convert_code(code);
1756 /* afs_linux_ireadlink
1757 * Internal readlink which can return link contents to user or kernel space.
1758 * Note that the buffer is NOT supposed to be null-terminated.
1761 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1764 cred_t *credp = crref();
1768 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1769 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1773 return maxlen - tuio.uio_resid;
1775 return afs_convert_code(code);
1778 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1779 /* afs_linux_readlink
1780 * Fill target (which is in user space) with contents of symlink.
1783 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1786 struct inode *ip = dp->d_inode;
1789 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1795 /* afs_linux_follow_link
1796 * a file system dependent link following routine.
1798 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1803 name = kmalloc(PATH_MAX, GFP_NOFS);
1809 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1817 nd_set_link(nd, name);
1822 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1824 char *name = nd_get_link(nd);
1826 if (name && !IS_ERR(name))
1830 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1832 /* Populate a page by filling it from the cache file pointed at by cachefp
1833 * (which contains indicated chunk)
1834 * If task is NULL, the page copy occurs syncronously, and the routine
1835 * returns with page still locked. If task is non-NULL, then page copies
1836 * may occur in the background, and the page will be unlocked when it is
1840 afs_linux_read_cache(struct file *cachefp, struct page *page,
1841 int chunk, struct pagevec *lrupv,
1842 struct afs_pagecopy_task *task) {
1843 loff_t offset = page_offset(page);
1844 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1845 struct page *newpage, *cachepage;
1846 struct address_space *cachemapping;
1850 cachemapping = cacheinode->i_mapping;
1854 /* If we're trying to read a page that's past the end of the disk
1855 * cache file, then just return a zeroed page */
1856 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1857 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1858 SetPageUptodate(page);
1864 /* From our offset, we now need to work out which page in the disk
1865 * file it corresponds to. This will be fun ... */
1866 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1868 while (cachepage == NULL) {
1869 cachepage = find_get_page(cachemapping, pageindex);
1872 newpage = page_cache_alloc_cold(cachemapping);
1878 code = add_to_page_cache(newpage, cachemapping,
1879 pageindex, GFP_KERNEL);
1881 cachepage = newpage;
1884 page_cache_get(cachepage);
1885 if (!pagevec_add(lrupv, cachepage))
1886 __pagevec_lru_add_file(lrupv);
1889 page_cache_release(newpage);
1891 if (code != -EEXIST)
1895 lock_page(cachepage);
1899 if (!PageUptodate(cachepage)) {
1900 ClearPageError(cachepage);
1901 code = cachemapping->a_ops->readpage(NULL, cachepage);
1902 if (!code && !task) {
1903 wait_on_page_locked(cachepage);
1906 unlock_page(cachepage);
1910 if (PageUptodate(cachepage)) {
1911 copy_highpage(page, cachepage);
1912 flush_dcache_page(page);
1913 SetPageUptodate(page);
1918 afs_pagecopy_queue_page(task, cachepage, page);
1930 page_cache_release(cachepage);
1936 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1938 loff_t offset = page_offset(pp);
1939 struct inode *ip = FILE_INODE(fp);
1940 struct vcache *avc = VTOAFS(ip);
1942 struct file *cacheFp = NULL;
1945 struct pagevec lrupv;
1947 /* Not a UFS cache, don't do anything */
1948 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1951 /* No readpage (ex: tmpfs) , skip */
1952 if (cachefs_noreadpage)
1955 /* Can't do anything if the vcache isn't statd , or if the read
1956 * crosses a chunk boundary.
1958 if (!(avc->f.states & CStatd) ||
1959 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1963 ObtainWriteLock(&avc->lock, 911);
1965 /* XXX - See if hinting actually makes things faster !!! */
1967 /* See if we have a suitable entry already cached */
1971 /* We need to lock xdcache, then dcache, to handle situations where
1972 * the hint is on the free list. However, we can't safely do this
1973 * according to the locking hierarchy. So, use a non blocking lock.
1975 ObtainReadLock(&afs_xdcache);
1976 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1978 if (dcLocked && (tdc->index != NULLIDX)
1979 && !FidCmp(&tdc->f.fid, &avc->f.fid)
1980 && tdc->f.chunk == AFS_CHUNK(offset)
1981 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1982 /* Bonus - the hint was correct */
1985 /* Only destroy the hint if its actually invalid, not if there's
1986 * just been a locking failure */
1988 ReleaseReadLock(&tdc->lock);
1995 ReleaseReadLock(&afs_xdcache);
1998 /* No hint, or hint is no longer valid - see if we can get something
1999 * directly from the dcache
2002 tdc = afs_FindDCache(avc, offset);
2005 ReleaseWriteLock(&avc->lock);
2010 ObtainReadLock(&tdc->lock);
2012 /* Is the dcache we've been given currently up to date */
2013 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2014 (tdc->dflags & DFFetching))
2017 /* Update our hint for future abuse */
2020 /* Okay, so we've now got a cache file that is up to date */
2022 /* XXX - I suspect we should be locking the inodes before we use them! */
2024 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2025 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2026 cachefs_noreadpage = 1;
2030 pagevec_init(&lrupv, 0);
2032 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
2034 if (pagevec_count(&lrupv))
2035 __pagevec_lru_add_file(&lrupv);
2037 filp_close(cacheFp, NULL);
2040 ReleaseReadLock(&tdc->lock);
2041 ReleaseWriteLock(&avc->lock);
2048 ReleaseWriteLock(&avc->lock);
2049 ReleaseReadLock(&tdc->lock);
2054 /* afs_linux_readpage
2056 * This function is split into two, because prepare_write/begin_write
2057 * require a readpage call which doesn't unlock the resulting page upon
2061 afs_linux_fillpage(struct file *fp, struct page *pp)
2066 struct iovec *iovecp;
2067 struct inode *ip = FILE_INODE(fp);
2068 afs_int32 cnt = page_count(pp);
2069 struct vcache *avc = VTOAFS(ip);
2070 afs_offs_t offset = page_offset(pp);
2074 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2084 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2085 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2087 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2092 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2093 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2094 99999); /* not a possible code value */
2096 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2098 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2099 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2101 AFS_DISCON_UNLOCK();
2104 /* XXX valid for no-cache also? Check last bits of files... :)
2105 * Cognate code goes in afs_NoCacheFetchProc. */
2106 if (auio->uio_resid) /* zero remainder of page */
2107 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2110 flush_dcache_page(pp);
2111 SetPageUptodate(pp);
2120 return afs_convert_code(code);
2124 afs_linux_prefetch(struct file *fp, struct page *pp)
2127 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2128 afs_offs_t offset = page_offset(pp);
2130 if (AFS_CHUNKOFFSET(offset) == 0) {
2132 struct vrequest treq;
2137 code = afs_InitReq(&treq, credp);
2138 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2139 tdc = afs_FindDCache(avc, offset);
2141 if (!(tdc->mflags & DFNextStarted))
2142 afs_PrefetchChunk(avc, tdc, credp, &treq);
2145 ReleaseWriteLock(&avc->lock);
2150 return afs_convert_code(code);
2155 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2156 struct list_head *page_list, unsigned num_pages)
2161 struct iovec* iovecp;
2162 struct nocache_read_request *ancr;
2164 struct pagevec lrupv;
2168 struct inode *ip = FILE_INODE(fp);
2169 struct vcache *avc = VTOAFS(ip);
2170 afs_int32 base_index = 0;
2171 afs_int32 page_count = 0;
2174 /* background thread must free: iovecp, auio, ancr */
2175 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2177 auio = osi_Alloc(sizeof(struct uio));
2178 auio->uio_iov = iovecp;
2179 auio->uio_iovcnt = num_pages;
2180 auio->uio_flag = UIO_READ;
2181 auio->uio_seg = AFS_UIOSYS;
2182 auio->uio_resid = num_pages * PAGE_SIZE;
2184 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2186 ancr->offset = auio->uio_offset;
2187 ancr->length = auio->uio_resid;
2189 pagevec_init(&lrupv, 0);
2191 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2193 if(list_empty(page_list))
2196 pp = list_entry(page_list->prev, struct page, lru);
2197 /* If we allocate a page and don't remove it from page_list,
2198 * the page cache gets upset. */
2200 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
2201 if(pp->index > isize) {
2208 offset = page_offset(pp);
2209 ancr->offset = auio->uio_offset = offset;
2210 base_index = pp->index;
2212 iovecp[page_ix].iov_len = PAGE_SIZE;
2213 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2214 if(base_index != pp->index) {
2217 page_cache_release(pp);
2218 iovecp[page_ix].iov_base = (void *) 0;
2220 ancr->length -= PAGE_SIZE;
2227 page_cache_release(pp);
2228 iovecp[page_ix].iov_base = (void *) 0;
2231 if(!PageLocked(pp)) {
2235 /* increment page refcount--our original design assumed
2236 * that locking it would effectively pin it; protect
2237 * ourselves from the possiblity that this assumption is
2238 * is faulty, at low cost (provided we do not fail to
2239 * do the corresponding decref on the other side) */
2242 /* save the page for background map */
2243 iovecp[page_ix].iov_base = (void*) pp;
2245 /* and put it on the LRU cache */
2246 if (!pagevec_add(&lrupv, pp))
2247 __pagevec_lru_add_file(&lrupv);
2251 /* If there were useful pages in the page list, make sure all pages
2252 * are in the LRU cache, then schedule the read */
2254 if (pagevec_count(&lrupv))
2255 __pagevec_lru_add_file(&lrupv);
2257 code = afs_ReadNoCache(avc, ancr, credp);
2260 /* If there is nothing for the background thread to handle,
2261 * it won't be freeing the things that we never gave it */
2262 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2263 osi_Free(auio, sizeof(struct uio));
2264 osi_Free(ancr, sizeof(struct nocache_read_request));
2266 /* we do not flush, release, or unmap pages--that will be
2267 * done for us by the background thread as each page comes in
2268 * from the fileserver */
2269 return afs_convert_code(code);
2274 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2276 cred_t *credp = NULL;
2278 struct iovec *iovecp;
2279 struct nocache_read_request *ancr;
2283 * Special case: if page is at or past end of file, just zero it and set
2286 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2287 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
2288 SetPageUptodate(pp);
2295 /* receiver frees */
2296 auio = osi_Alloc(sizeof(struct uio));
2297 iovecp = osi_Alloc(sizeof(struct iovec));
2299 /* address can be NULL, because we overwrite it with 'pp', below */
2300 setup_uio(auio, iovecp, NULL, page_offset(pp),
2301 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2303 /* save the page for background map */
2304 get_page(pp); /* see above */
2305 auio->uio_iov->iov_base = (void*) pp;
2306 /* the background thread will free this */
2307 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2309 ancr->offset = page_offset(pp);
2310 ancr->length = PAGE_SIZE;
2313 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2316 return afs_convert_code(code);
2320 afs_linux_can_bypass(struct inode *ip) {
2322 switch(cache_bypass_strategy) {
2323 case NEVER_BYPASS_CACHE:
2325 case ALWAYS_BYPASS_CACHE:
2327 case LARGE_FILES_BYPASS_CACHE:
2328 if (i_size_read(ip) > cache_bypass_threshold)
2335 /* Check if a file is permitted to bypass the cache by policy, and modify
2336 * the cache bypass state recorded for that file */
2339 afs_linux_bypass_check(struct inode *ip) {
2342 int bypass = afs_linux_can_bypass(ip);
2345 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2353 afs_linux_readpage(struct file *fp, struct page *pp)
2357 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2358 code = afs_linux_bypass_readpage(fp, pp);
2360 code = afs_linux_fillpage(fp, pp);
2362 code = afs_linux_prefetch(fp, pp);
2369 /* Readpages reads a number of pages for a particular file. We use
2370 * this to optimise the reading, by limiting the number of times upon which
2371 * we have to lookup, lock and open vcaches and dcaches
2375 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2376 struct list_head *page_list, unsigned int num_pages)
2378 struct inode *inode = mapping->host;
2379 struct vcache *avc = VTOAFS(inode);
2381 struct file *cacheFp = NULL;
2383 unsigned int page_idx;
2385 struct pagevec lrupv;
2386 struct afs_pagecopy_task *task;
2388 if (afs_linux_bypass_check(inode))
2389 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2391 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2394 /* No readpage (ex: tmpfs) , skip */
2395 if (cachefs_noreadpage)
2399 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2404 ObtainWriteLock(&avc->lock, 912);
2407 task = afs_pagecopy_init_task();
2410 pagevec_init(&lrupv, 0);
2411 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2412 struct page *page = list_entry(page_list->prev, struct page, lru);
2413 list_del(&page->lru);
2414 offset = page_offset(page);
2416 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2418 ReleaseReadLock(&tdc->lock);
2423 filp_close(cacheFp, NULL);
2428 if ((tdc = afs_FindDCache(avc, offset))) {
2429 ObtainReadLock(&tdc->lock);
2430 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2431 (tdc->dflags & DFFetching)) {
2432 ReleaseReadLock(&tdc->lock);
2439 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2440 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2441 cachefs_noreadpage = 1;
2447 if (tdc && !add_to_page_cache(page, mapping, page->index,
2449 page_cache_get(page);
2450 if (!pagevec_add(&lrupv, page))
2451 __pagevec_lru_add_file(&lrupv);
2453 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2455 page_cache_release(page);
2457 if (pagevec_count(&lrupv))
2458 __pagevec_lru_add_file(&lrupv);
2462 filp_close(cacheFp, NULL);
2464 afs_pagecopy_put_task(task);
2468 ReleaseReadLock(&tdc->lock);
2472 ReleaseWriteLock(&avc->lock);
2477 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2480 afs_linux_prepare_writeback(struct vcache *avc) {
2481 if (avc->f.states & CPageWrite) {
2482 return AOP_WRITEPAGE_ACTIVATE;
2484 avc->f.states |= CPageWrite;
2489 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2490 struct vrequest treq;
2493 if (!afs_InitReq(&treq, credp))
2494 code = afs_DoPartialWrite(avc, &treq);
2496 return afs_convert_code(code);
2500 afs_linux_complete_writeback(struct vcache *avc) {
2501 avc->f.states &= ~CPageWrite;
2504 /* Writeback a given page syncronously. Called with no AFS locks held */
2506 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2507 unsigned long offset, unsigned int count,
2510 struct vcache *vcp = VTOAFS(ip);
2518 buffer = kmap(pp) + offset;
2519 base = page_offset(pp) + offset;
2522 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2523 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2524 ICL_TYPE_INT32, 99999);
2526 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2528 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2530 i_size_write(ip, vcp->f.m.Length);
2531 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2533 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2535 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2536 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2537 ICL_TYPE_INT32, code);
2546 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2547 unsigned long offset, unsigned int count)
2551 struct vcache *vcp = VTOAFS(ip);
2554 /* Catch recursive writeback. This occurs if the kernel decides
2555 * writeback is required whilst we are writing to the cache, or
2556 * flushing to the server. When we're running syncronously (as
2557 * opposed to from writepage) we can't actually do anything about
2558 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2561 ObtainWriteLock(&vcp->lock, 532);
2562 afs_linux_prepare_writeback(vcp);
2563 ReleaseWriteLock(&vcp->lock);
2567 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2570 ObtainWriteLock(&vcp->lock, 533);
2572 code1 = afs_linux_dopartialwrite(vcp, credp);
2573 afs_linux_complete_writeback(vcp);
2574 ReleaseWriteLock(&vcp->lock);
2585 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2586 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2588 afs_linux_writepage(struct page *pp)
2591 struct address_space *mapping = pp->mapping;
2592 struct inode *inode;
2595 unsigned int to = PAGE_CACHE_SIZE;
2600 if (PageReclaim(pp)) {
2601 return AOP_WRITEPAGE_ACTIVATE;
2602 /* XXX - Do we need to redirty the page here? */
2607 inode = mapping->host;
2608 vcp = VTOAFS(inode);
2609 isize = i_size_read(inode);
2611 /* Don't defeat an earlier truncate */
2612 if (page_offset(pp) > isize) {
2613 set_page_writeback(pp);
2619 ObtainWriteLock(&vcp->lock, 537);
2620 code = afs_linux_prepare_writeback(vcp);
2621 if (code == AOP_WRITEPAGE_ACTIVATE) {
2622 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2623 * to return with the page still locked */
2624 ReleaseWriteLock(&vcp->lock);
2629 /* Grab the creds structure currently held in the vnode, and
2630 * get a reference to it, in case it goes away ... */
2636 ReleaseWriteLock(&vcp->lock);
2639 set_page_writeback(pp);
2641 SetPageUptodate(pp);
2643 /* We can unlock the page here, because it's protected by the
2644 * page_writeback flag. This should make us less vulnerable to
2645 * deadlocking in afs_write and afs_DoPartialWrite
2649 /* If this is the final page, then just write the number of bytes that
2650 * are actually in it */
2651 if ((isize - page_offset(pp)) < to )
2652 to = isize - page_offset(pp);
2654 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2657 ObtainWriteLock(&vcp->lock, 538);
2659 /* As much as we might like to ignore a file server error here,
2660 * and just try again when we close(), unfortunately StoreAllSegments
2661 * will invalidate our chunks if the server returns a permanent error,
2662 * so we need to at least try and get that error back to the user
2665 code1 = afs_linux_dopartialwrite(vcp, credp);
2667 afs_linux_complete_writeback(vcp);
2668 ReleaseWriteLock(&vcp->lock);
2673 end_page_writeback(pp);
2674 page_cache_release(pp);
2685 /* afs_linux_permission
2686 * Check access rights - returns error if can't check or permission denied.
2689 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2690 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2691 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2692 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2694 afs_linux_permission(struct inode *ip, int mode)
2701 /* Check for RCU path walking */
2702 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2703 if (flags & IPERM_FLAG_RCU)
2705 #elif defined(MAY_NOT_BLOCK)
2706 if (mode & MAY_NOT_BLOCK)
2712 if (mode & MAY_EXEC)
2714 if (mode & MAY_READ)
2716 if (mode & MAY_WRITE)
2718 code = afs_access(VTOAFS(ip), tmp, credp);
2722 return afs_convert_code(code);
2726 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2730 struct inode *inode = FILE_INODE(file);
2731 loff_t pagebase = page_offset(page);
2733 if (i_size_read(inode) < (pagebase + offset))
2734 i_size_write(inode, pagebase + offset);
2736 if (PageChecked(page)) {
2737 SetPageUptodate(page);
2738 ClearPageChecked(page);
2741 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2747 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2751 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2752 * prepare_write. Essentially, if the page exists within the file,
2753 * and is not being fully written, then we should populate it.
2756 if (!PageUptodate(page)) {
2757 loff_t pagebase = page_offset(page);
2758 loff_t isize = i_size_read(page->mapping->host);
2760 /* Is the location we are writing to beyond the end of the file? */
2761 if (pagebase >= isize ||
2762 ((from == 0) && (pagebase + to) >= isize)) {
2763 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2764 SetPageChecked(page);
2765 /* Are we we writing a full page */
2766 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2767 SetPageChecked(page);
2768 /* Is the page readable, if it's wronly, we don't care, because we're
2769 * not actually going to read from it ... */
2770 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2771 /* We don't care if fillpage fails, because if it does the page
2772 * won't be marked as up to date
2774 afs_linux_fillpage(file, page);
2780 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2782 afs_linux_write_end(struct file *file, struct address_space *mapping,
2783 loff_t pos, unsigned len, unsigned copied,
2784 struct page *page, void *fsdata)
2787 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2789 code = afs_linux_commit_write(file, page, from, from + len);
2792 page_cache_release(page);
2797 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2798 loff_t pos, unsigned len, unsigned flags,
2799 struct page **pagep, void **fsdata)
2802 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2803 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2806 page = grab_cache_page_write_begin(mapping, index, flags);
2809 code = afs_linux_prepare_write(file, page, from, from + len);
2812 page_cache_release(page);
2819 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2821 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2823 struct dentry **dpp;
2824 struct dentry *target;
2826 if (current->total_link_count > 0) {
2827 /* avoid symlink resolution limits when resolving; we cannot contribute to
2828 * an infinite symlink loop */
2829 /* only do this for follow_link when total_link_count is positive to be
2830 * on the safe side; there is at least one code path in the Linux
2831 * kernel where it seems like it may be possible to get here without
2832 * total_link_count getting incremented. it is not clear on how that
2833 * path is actually reached, but guard against it just to be safe */
2834 current->total_link_count--;
2837 target = canonical_dentry(dentry->d_inode);
2839 # ifdef STRUCT_NAMEIDATA_HAS_PATH
2840 dpp = &nd->path.dentry;
2850 *dpp = dget(dentry);
2853 nd->last_type = LAST_BIND;
2857 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
2860 static struct inode_operations afs_file_iops = {
2861 .permission = afs_linux_permission,
2862 .getattr = afs_linux_getattr,
2863 .setattr = afs_notify_change,
2866 static struct address_space_operations afs_file_aops = {
2867 .readpage = afs_linux_readpage,
2868 .readpages = afs_linux_readpages,
2869 .writepage = afs_linux_writepage,
2870 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2871 .write_begin = afs_linux_write_begin,
2872 .write_end = afs_linux_write_end,
2874 .commit_write = afs_linux_commit_write,
2875 .prepare_write = afs_linux_prepare_write,
2880 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2881 * by what sort of operation is allowed.....
2884 static struct inode_operations afs_dir_iops = {
2885 .setattr = afs_notify_change,
2886 .create = afs_linux_create,
2887 .lookup = afs_linux_lookup,
2888 .link = afs_linux_link,
2889 .unlink = afs_linux_unlink,
2890 .symlink = afs_linux_symlink,
2891 .mkdir = afs_linux_mkdir,
2892 .rmdir = afs_linux_rmdir,
2893 .rename = afs_linux_rename,
2894 .getattr = afs_linux_getattr,
2895 .permission = afs_linux_permission,
2896 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2897 .follow_link = afs_linux_dir_follow_link,
2901 /* We really need a separate symlink set of ops, since do_follow_link()
2902 * determines if it _is_ a link by checking if the follow_link op is set.
2904 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2906 afs_symlink_filler(struct file *file, struct page *page)
2908 struct inode *ip = (struct inode *)page->mapping->host;
2909 char *p = (char *)kmap(page);
2913 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2918 p[code] = '\0'; /* null terminate? */
2920 SetPageUptodate(page);
2932 static struct address_space_operations afs_symlink_aops = {
2933 .readpage = afs_symlink_filler
2935 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2937 static struct inode_operations afs_symlink_iops = {
2938 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2939 .readlink = page_readlink,
2940 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
2941 .follow_link = page_follow_link,
2943 .follow_link = page_follow_link_light,
2944 .put_link = page_put_link,
2946 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2947 .readlink = afs_linux_readlink,
2948 .follow_link = afs_linux_follow_link,
2949 .put_link = afs_linux_put_link,
2950 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2951 .setattr = afs_notify_change,
2955 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2959 vattr2inode(ip, vattr);
2961 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
2962 /* Reset ops if symlink or directory. */
2963 if (S_ISREG(ip->i_mode)) {
2964 ip->i_op = &afs_file_iops;
2965 ip->i_fop = &afs_file_fops;
2966 ip->i_data.a_ops = &afs_file_aops;
2968 } else if (S_ISDIR(ip->i_mode)) {
2969 ip->i_op = &afs_dir_iops;
2970 ip->i_fop = &afs_dir_fops;
2972 } else if (S_ISLNK(ip->i_mode)) {
2973 ip->i_op = &afs_symlink_iops;
2974 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2975 ip->i_data.a_ops = &afs_symlink_aops;
2976 ip->i_mapping = &ip->i_data;