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) {
79 struct vrequest *treq = NULL;
82 if (avc->f.states & CStatd) {
90 code = afs_CreateReq(&treq, credp);
92 code = afs_VerifyVCache2(avc, treq);
101 return afs_convert_code(code);
104 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
105 # ifdef LINUX_HAS_NONVECTOR_AIO
107 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
111 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
112 unsigned long bufsize, loff_t pos)
115 struct file *fp = iocb->ki_filp;
117 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
120 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
121 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
122 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
123 code = afs_linux_VerifyVCache(vcp, NULL);
126 /* Linux's FlushPages implementation doesn't ever use credp,
127 * so we optimise by not using it */
128 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
130 code = generic_file_aio_read(iocb, buf, bufsize, pos);
134 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
135 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
136 (afs_int32)bufsize, ICL_TYPE_INT32, code);
142 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
145 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
148 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
149 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
151 code = afs_linux_VerifyVCache(vcp, NULL);
154 /* Linux's FlushPages implementation doesn't ever use credp,
155 * so we optimise by not using it */
156 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
158 code = do_sync_read(fp, buf, count, offp);
162 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
163 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
171 /* Now we have integrated VM for writes as well as reads. the generic write operations
172 * also take care of re-positioning the pointer if file is open in append
173 * mode. Call fake open/close to ensure we do writes of core dumps.
175 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
176 # ifdef LINUX_HAS_NONVECTOR_AIO
178 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
182 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
183 unsigned long bufsize, loff_t pos)
187 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
192 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
193 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
194 (afs_int32)bufsize, ICL_TYPE_INT32,
195 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
197 code = afs_linux_VerifyVCache(vcp, &credp);
199 ObtainWriteLock(&vcp->lock, 529);
201 ReleaseWriteLock(&vcp->lock);
204 code = generic_file_aio_write(iocb, buf, bufsize, pos);
208 ObtainWriteLock(&vcp->lock, 530);
210 if (vcp->execsOrWriters == 1 && !credp)
213 afs_FakeClose(vcp, credp);
214 ReleaseWriteLock(&vcp->lock);
216 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
217 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
218 (afs_int32)bufsize, ICL_TYPE_INT32, code);
227 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
230 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
235 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
236 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
237 (fp->f_flags & O_APPEND) ? 99998 : 99999);
239 code = afs_linux_VerifyVCache(vcp, &credp);
241 ObtainWriteLock(&vcp->lock, 529);
243 ReleaseWriteLock(&vcp->lock);
246 code = do_sync_write(fp, buf, count, offp);
250 ObtainWriteLock(&vcp->lock, 530);
252 if (vcp->execsOrWriters == 1 && !credp)
255 afs_FakeClose(vcp, credp);
256 ReleaseWriteLock(&vcp->lock);
258 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
259 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
269 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
271 /* This is a complete rewrite of afs_readdir, since we can make use of
272 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
273 * handling and use of bulkstats will need to be reflected here as well.
276 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
277 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
279 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
282 struct vcache *avc = VTOAFS(FILE_INODE(fp));
283 struct vrequest *treq = NULL;
289 struct DirBuffer entry;
292 afs_size_t origOffset, tlen;
293 cred_t *credp = crref();
294 struct afs_fakestat_state fakestat;
297 AFS_STATCNT(afs_readdir);
299 code = afs_convert_code(afs_CreateReq(&treq, credp));
304 afs_InitFakeStat(&fakestat);
305 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, treq));
309 /* update the cache entry */
311 code = afs_convert_code(afs_VerifyVCache2(avc, treq));
315 /* get a reference to the entire directory */
316 tdc = afs_GetDCache(avc, (afs_size_t) 0, treq, &origOffset, &tlen, 1);
322 ObtainWriteLock(&avc->lock, 811);
323 ObtainReadLock(&tdc->lock);
325 * Make sure that the data in the cache is current. There are two
326 * cases we need to worry about:
327 * 1. The cache data is being fetched by another process.
328 * 2. The cache data is no longer valid
330 while ((avc->f.states & CStatd)
331 && (tdc->dflags & DFFetching)
332 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
333 ReleaseReadLock(&tdc->lock);
334 ReleaseWriteLock(&avc->lock);
335 afs_osi_Sleep(&tdc->validPos);
336 ObtainWriteLock(&avc->lock, 812);
337 ObtainReadLock(&tdc->lock);
339 if (!(avc->f.states & CStatd)
340 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
341 ReleaseReadLock(&tdc->lock);
342 ReleaseWriteLock(&avc->lock);
347 /* Set the readdir-in-progress flag, and downgrade the lock
348 * to shared so others will be able to acquire a read lock.
350 avc->f.states |= CReadDir;
351 avc->dcreaddir = tdc;
352 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
353 ConvertWToSLock(&avc->lock);
355 /* Fill in until we get an error or we're done. This implementation
356 * takes an offset in units of blobs, rather than bytes.
359 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
362 offset = (int) fp->f_pos;
365 dirpos = BlobScan(tdc, offset);
369 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
371 if (!(avc->f.states & CCorrupt)) {
372 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
373 afs_warn("Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)",
374 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
375 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
376 tc ? tc->cellName : "",
377 (unsigned long)&tdc->f.inode, dirpos);
379 afs_PutCell(tc, READ_LOCK);
380 UpgradeSToWLock(&avc->lock, 814);
381 avc->f.states |= CCorrupt;
387 de = (struct DirEntry *)entry.data;
388 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
389 ntohl(de->fid.vnode));
390 len = strlen(de->name);
392 /* filldir returns -EINVAL when the buffer is full. */
394 unsigned int type = DT_UNKNOWN;
395 struct VenusFid afid;
398 afid.Cell = avc->f.fid.Cell;
399 afid.Fid.Volume = avc->f.fid.Fid.Volume;
400 afid.Fid.Vnode = ntohl(de->fid.vnode);
401 afid.Fid.Unique = ntohl(de->fid.vunique);
402 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
404 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
407 } else if (((tvc->f.states) & (CStatd | CTruth))) {
408 /* CTruth will be set if the object has
413 else if (vtype == VREG)
415 /* Don't do this until we're sure it can't be a mtpt */
416 /* else if (vtype == VLNK)
418 /* what other types does AFS support? */
420 /* clean up from afs_FindVCache */
424 * If this is NFS readdirplus, then the filler is going to
425 * call getattr on this inode, which will deadlock if we're
429 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
430 /* dir_emit returns a bool - true when it succeeds.
431 * Inverse the result to fit with how we check "code" */
432 code = !dir_emit(ctx, de->name, len, ino, type);
434 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
441 offset = dirpos + 1 + ((len + 16) >> 5);
443 /* If filldir didn't fill in the last one this is still pointing to that
449 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
450 ctx->pos = (loff_t) offset;
452 fp->f_pos = (loff_t) offset;
454 ReleaseReadLock(&tdc->lock);
456 UpgradeSToWLock(&avc->lock, 813);
457 avc->f.states &= ~CReadDir;
459 avc->readdir_pid = 0;
460 ReleaseSharedLock(&avc->lock);
463 afs_PutFakeStat(&fakestat);
464 afs_DestroyReq(treq);
471 /* in afs_pioctl.c */
472 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
475 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
476 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
478 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
485 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
487 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
491 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
492 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
493 vmap->vm_end - vmap->vm_start);
495 /* get a validated vcache entry */
496 code = afs_linux_VerifyVCache(vcp, NULL);
499 /* Linux's Flushpage implementation doesn't use credp, so optimise
500 * our code to not need to crref() it */
501 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
503 code = generic_file_mmap(fp, vmap);
506 vcp->f.states |= CMAPPED;
514 afs_linux_open(struct inode *ip, struct file *fp)
516 struct vcache *vcp = VTOAFS(ip);
517 cred_t *credp = crref();
521 code = afs_open(&vcp, fp->f_flags, credp);
525 return afs_convert_code(code);
529 afs_linux_release(struct inode *ip, struct file *fp)
531 struct vcache *vcp = VTOAFS(ip);
532 cred_t *credp = crref();
536 code = afs_close(vcp, fp->f_flags, credp);
537 ObtainWriteLock(&vcp->lock, 807);
542 ReleaseWriteLock(&vcp->lock);
546 return afs_convert_code(code);
550 #if defined(FOP_FSYNC_TAKES_DENTRY)
551 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
552 #elif defined(FOP_FSYNC_TAKES_RANGE)
553 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
555 afs_linux_fsync(struct file *fp, int datasync)
559 struct inode *ip = FILE_INODE(fp);
560 cred_t *credp = crref();
562 #if defined(FOP_FSYNC_TAKES_RANGE)
563 mutex_lock(&ip->i_mutex);
566 code = afs_fsync(VTOAFS(ip), credp);
568 #if defined(FOP_FSYNC_TAKES_RANGE)
569 mutex_unlock(&ip->i_mutex);
572 return afs_convert_code(code);
578 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
581 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
582 cred_t *credp = crref();
583 struct AFS_FLOCK flock;
585 /* Convert to a lock format afs_lockctl understands. */
586 memset(&flock, 0, sizeof(flock));
587 flock.l_type = flp->fl_type;
588 flock.l_pid = flp->fl_pid;
590 flock.l_start = flp->fl_start;
591 if (flp->fl_end == OFFSET_MAX)
592 flock.l_len = 0; /* Lock to end of file */
594 flock.l_len = flp->fl_end - flp->fl_start + 1;
596 /* Safe because there are no large files, yet */
597 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
598 if (cmd == F_GETLK64)
600 else if (cmd == F_SETLK64)
602 else if (cmd == F_SETLKW64)
604 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
607 if ((vcp->f.states & CRO)) {
608 if (flp->fl_type == F_WRLCK) {
617 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
620 if ((code == 0 || flp->fl_type == F_UNLCK) &&
621 (cmd == F_SETLK || cmd == F_SETLKW)) {
622 code = afs_posix_lock_file(fp, flp);
623 if (code && flp->fl_type != F_UNLCK) {
624 struct AFS_FLOCK flock2;
626 flock2.l_type = F_UNLCK;
628 afs_lockctl(vcp, &flock2, F_SETLK, credp);
632 /* If lockctl says there are no conflicting locks, then also check with the
633 * kernel, as lockctl knows nothing about byte range locks
635 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
636 afs_posix_test_lock(fp, flp);
637 /* If we found a lock in the kernel's structure, return it */
638 if (flp->fl_type != F_UNLCK) {
644 /* Convert flock back to Linux's file_lock */
645 flp->fl_type = flock.l_type;
646 flp->fl_pid = flock.l_pid;
647 flp->fl_start = flock.l_start;
648 if (flock.l_len == 0)
649 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
651 flp->fl_end = flock.l_start + flock.l_len - 1;
657 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
659 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
661 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
662 cred_t *credp = crref();
663 struct AFS_FLOCK flock;
664 /* Convert to a lock format afs_lockctl understands. */
665 memset(&flock, 0, sizeof(flock));
666 flock.l_type = flp->fl_type;
667 flock.l_pid = flp->fl_pid;
672 /* Safe because there are no large files, yet */
673 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
674 if (cmd == F_GETLK64)
676 else if (cmd == F_SETLK64)
678 else if (cmd == F_SETLKW64)
680 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
683 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
686 if ((code == 0 || flp->fl_type == F_UNLCK) &&
687 (cmd == F_SETLK || cmd == F_SETLKW)) {
688 flp->fl_flags &=~ FL_SLEEP;
689 code = flock_lock_file_wait(fp, flp);
690 if (code && flp->fl_type != F_UNLCK) {
691 struct AFS_FLOCK flock2;
693 flock2.l_type = F_UNLCK;
695 afs_lockctl(vcp, &flock2, F_SETLK, credp);
699 /* Convert flock back to Linux's file_lock */
700 flp->fl_type = flock.l_type;
701 flp->fl_pid = flock.l_pid;
709 * essentially the same as afs_fsync() but we need to get the return
710 * code for the sys_close() here, not afs_linux_release(), so call
711 * afs_StoreAllSegments() with AFS_LASTSTORE
714 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
715 afs_linux_flush(struct file *fp, fl_owner_t id)
717 afs_linux_flush(struct file *fp)
720 struct vrequest *treq = NULL;
728 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
736 vcp = VTOAFS(FILE_INODE(fp));
738 code = afs_CreateReq(&treq, credp);
741 /* If caching is bypassed for this file, or globally, just return 0 */
742 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
745 ObtainReadLock(&vcp->lock);
746 if (vcp->cachingStates & FCSBypass)
748 ReleaseReadLock(&vcp->lock);
751 /* future proof: don't rely on 0 return from afs_InitReq */
756 ObtainSharedLock(&vcp->lock, 535);
757 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
758 UpgradeSToWLock(&vcp->lock, 536);
759 if (!AFS_IS_DISCONNECTED) {
760 code = afs_StoreAllSegments(vcp,
762 AFS_SYNC | AFS_LASTSTORE);
764 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
766 ConvertWToSLock(&vcp->lock);
768 code = afs_CheckCode(code, treq, 54);
769 ReleaseSharedLock(&vcp->lock);
772 afs_DestroyReq(treq);
777 return afs_convert_code(code);
780 struct file_operations afs_dir_fops = {
781 .read = generic_read_dir,
782 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
783 .iterate = afs_linux_readdir,
785 .readdir = afs_linux_readdir,
787 #ifdef HAVE_UNLOCKED_IOCTL
788 .unlocked_ioctl = afs_unlocked_xioctl,
792 #ifdef HAVE_COMPAT_IOCTL
793 .compat_ioctl = afs_unlocked_xioctl,
795 .open = afs_linux_open,
796 .release = afs_linux_release,
797 .llseek = default_llseek,
798 #ifdef HAVE_LINUX_NOOP_FSYNC
801 .fsync = simple_sync_file,
805 struct file_operations afs_file_fops = {
806 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
807 .aio_read = afs_linux_aio_read,
808 .aio_write = afs_linux_aio_write,
809 .read = do_sync_read,
810 .write = do_sync_write,
812 .read = afs_linux_read,
813 .write = afs_linux_write,
815 #ifdef HAVE_UNLOCKED_IOCTL
816 .unlocked_ioctl = afs_unlocked_xioctl,
820 #ifdef HAVE_COMPAT_IOCTL
821 .compat_ioctl = afs_unlocked_xioctl,
823 .mmap = afs_linux_mmap,
824 .open = afs_linux_open,
825 .flush = afs_linux_flush,
826 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
827 .sendfile = generic_file_sendfile,
829 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
830 .splice_write = generic_file_splice_write,
831 .splice_read = generic_file_splice_read,
833 .release = afs_linux_release,
834 .fsync = afs_linux_fsync,
835 .lock = afs_linux_lock,
836 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
837 .flock = afs_linux_flock,
839 .llseek = default_llseek,
842 static struct dentry *
843 canonical_dentry(struct inode *ip)
845 struct vcache *vcp = VTOAFS(ip);
846 struct dentry *first = NULL, *ret = NULL, *cur;
847 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
848 struct hlist_node *p;
852 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
853 * otherwise, use the first dentry in ip->i_dentry.
854 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
856 /* note that vcp->target_link specifies which dentry to use, but we have
857 * no reference held on that dentry. so, we cannot use or dereference
858 * vcp->target_link itself, since it may have been freed. instead, we only
859 * use it to compare to pointers in the ip->i_dentry list. */
863 # ifdef HAVE_DCACHE_LOCK
864 spin_lock(&dcache_lock);
866 spin_lock(&ip->i_lock);
869 #if defined(D_ALIAS_IS_HLIST)
870 # if defined(HLIST_ITERATOR_NO_NODE)
871 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
873 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
876 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
879 if (!vcp->target_link || cur == vcp->target_link) {
892 vcp->target_link = ret;
894 # ifdef HAVE_DCACHE_LOCK
898 spin_unlock(&dcache_lock);
903 spin_unlock(&ip->i_lock);
909 /**********************************************************************
910 * AFS Linux dentry operations
911 **********************************************************************/
913 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
914 * that has its mvid (parent dir's fid) pointer set to the wrong directory
915 * due to being mounted in multiple points at once. fix_bad_parent()
916 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
917 * dotdotfid and mtpoint fid members.
919 * dp - dentry to be checked.
920 * credp - credentials
921 * vcp, pvc - item's and parent's vcache pointer
925 * This dentry's vcache's mvid will be set to the correct parent directory's
927 * This root vnode's volume will have its dotdotfid and mtpoint fids set
928 * to the correct parent and mountpoint fids.
932 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
934 struct vcache *avc = NULL;
936 /* force a lookup, so vcp->mvid is fixed up */
937 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
938 if (!avc || vcp != avc) { /* bad, very bad.. */
939 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
940 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
941 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
942 ICL_TYPE_POINTER, dp);
945 AFS_RELE(AFSTOV(avc));
950 /* afs_linux_revalidate
951 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
954 afs_linux_revalidate(struct dentry *dp)
956 struct vattr *vattr = NULL;
957 struct vcache *vcp = VTOAFS(dp->d_inode);
961 if (afs_shuttingdown)
966 code = afs_CreateAttr(&vattr);
972 /* Make this a fast path (no crref), since it's called so often. */
973 if (vcp->states & CStatd) {
974 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
976 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
977 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
980 fix_bad_parent(dp); /* check and correct mvid */
985 afs_DestroyAttr(vattr);
990 /* This avoids the crref when we don't have to do it. Watch for
991 * changes in afs_getattr that don't get replicated here!
993 if (vcp->f.states & CStatd &&
994 (!afs_fakestat_enable || vcp->mvstat != 1) &&
996 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
997 code = afs_CopyOutAttrs(vcp, vattr);
1000 code = afs_getattr(vcp, vattr, credp);
1005 afs_fill_inode(AFSTOV(vcp), vattr);
1007 afs_DestroyAttr(vattr);
1012 return afs_convert_code(code);
1016 * Set iattr data into vattr. Assume vattr cleared before call.
1019 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1021 vattrp->va_mask = iattrp->ia_valid;
1022 if (iattrp->ia_valid & ATTR_MODE)
1023 vattrp->va_mode = iattrp->ia_mode;
1024 if (iattrp->ia_valid & ATTR_UID)
1025 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1026 if (iattrp->ia_valid & ATTR_GID)
1027 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1028 if (iattrp->ia_valid & ATTR_SIZE)
1029 vattrp->va_size = iattrp->ia_size;
1030 if (iattrp->ia_valid & ATTR_ATIME) {
1031 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1032 vattrp->va_atime.tv_usec = 0;
1034 if (iattrp->ia_valid & ATTR_MTIME) {
1035 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1036 vattrp->va_mtime.tv_usec = 0;
1038 if (iattrp->ia_valid & ATTR_CTIME) {
1039 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1040 vattrp->va_ctime.tv_usec = 0;
1045 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1048 vattr2inode(struct inode *ip, struct vattr *vp)
1050 ip->i_ino = vp->va_nodeid;
1051 #ifdef HAVE_LINUX_SET_NLINK
1052 set_nlink(ip, vp->va_nlink);
1054 ip->i_nlink = vp->va_nlink;
1056 ip->i_blocks = vp->va_blocks;
1057 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1058 ip->i_blkbits = AFS_BLKBITS;
1060 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1061 ip->i_blksize = vp->va_blocksize;
1063 ip->i_rdev = vp->va_rdev;
1064 ip->i_mode = vp->va_mode;
1065 ip->i_uid = afs_make_kuid(vp->va_uid);
1066 ip->i_gid = afs_make_kgid(vp->va_gid);
1067 i_size_write(ip, vp->va_size);
1068 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1069 ip->i_atime.tv_nsec = 0;
1070 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1071 /* Set the mtime nanoseconds to the sysname generation number.
1072 * This convinces NFS clients that all directories have changed
1073 * any time the sysname list changes.
1075 ip->i_mtime.tv_nsec = afs_sysnamegen;
1076 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1077 ip->i_ctime.tv_nsec = 0;
1080 /* afs_notify_change
1081 * Linux version of setattr call. What to change is in the iattr struct.
1082 * We need to set bits in both the Linux inode as well as the vcache.
1085 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1087 struct vattr *vattr = NULL;
1088 cred_t *credp = crref();
1089 struct inode *ip = dp->d_inode;
1093 code = afs_CreateAttr(&vattr);
1098 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1100 code = afs_setattr(VTOAFS(ip), vattr, credp);
1102 afs_getattr(VTOAFS(ip), vattr, credp);
1103 vattr2inode(ip, vattr);
1105 afs_DestroyAttr(vattr);
1110 return afs_convert_code(code);
1114 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1116 int err = afs_linux_revalidate(dentry);
1118 generic_fillattr(dentry->d_inode, stat);
1124 parent_vcache_dv(struct inode *inode, cred_t *credp)
1127 struct vcache *pvcp;
1130 * If parent is a mount point and we are using fakestat, we may need
1131 * to look at the fake vcache entry instead of what the vfs is giving
1132 * us. The fake entry is the one with the useful DataVersion.
1134 pvcp = VTOAFS(inode);
1135 if (pvcp->mvstat == 1 && afs_fakestat_enable) {
1136 struct vrequest treq;
1137 struct afs_fakestat_state fakestate;
1143 afs_InitReq(&treq, credp);
1144 afs_InitFakeStat(&fakestate);
1145 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1148 afs_PutFakeStat(&fakestate);
1150 return hgetlo(pvcp->f.m.DataVersion);
1153 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1154 * In kernels 2.2.10 and above, we are passed an additional flags var which
1155 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1156 * we are advised to follow the entry if it is a link or to make sure that
1157 * it is a directory. But since the kernel itself checks these possibilities
1158 * later on, we shouldn't have to do it until later. Perhaps in the future..
1160 * The code here assumes that on entry the global lock is not held
1163 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1164 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1165 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1166 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1168 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1171 cred_t *credp = NULL;
1172 struct vcache *vcp, *pvcp, *tvc = NULL;
1173 struct dentry *parent;
1175 struct afs_fakestat_state fakestate;
1178 afs_uint32 parent_dv;
1181 /* We don't support RCU path walking */
1182 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1183 if (flags & LOOKUP_RCU)
1185 if (nd->flags & LOOKUP_RCU)
1190 afs_InitFakeStat(&fakestate);
1193 vcp = VTOAFS(dp->d_inode);
1195 if (vcp == afs_globalVp)
1198 parent = dget_parent(dp);
1199 pvcp = VTOAFS(parent->d_inode);
1201 if ((vcp->mvstat == 1) || (vcp->mvstat == 2) ||
1202 (pvcp->mvstat == 1 && afs_fakestat_enable)) { /* need to lock */
1208 if (locked && vcp->mvstat == 1) { /* mount point */
1209 if (vcp->mvid && (vcp->f.states & CMValid)) {
1210 int tryEvalOnly = 0;
1212 struct vrequest *treq = NULL;
1214 code = afs_CreateReq(&treq, credp);
1219 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1223 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1225 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1226 afs_DestroyReq(treq);
1227 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1228 /* a mount point, not yet replaced by its directory */
1234 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
1235 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
1236 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
1241 /* If the last looker changes, we should make sure the current
1242 * looker still has permission to examine this file. This would
1243 * always require a crref() which would be "slow".
1245 if (vcp->last_looker != treq.uid) {
1246 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1251 vcp->last_looker = treq.uid;
1255 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1257 /* If the parent's DataVersion has changed or the vnode
1258 * is longer valid, we need to do a full lookup. VerifyVCache
1259 * isn't enough since the vnode may have been renamed.
1262 if ((!locked) && (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) ) {
1268 if (locked && (parent_dv > dp->d_time || !(vcp->f.states & CStatd))) {
1269 struct vattr *vattr = NULL;
1272 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1273 if (!tvc || tvc != vcp) {
1275 /* Force unhash if name is known not to exist. */
1281 code = afs_CreateAttr(&vattr);
1287 if (afs_getattr(vcp, vattr, credp)) {
1289 afs_DestroyAttr(vattr);
1293 vattr2inode(AFSTOV(vcp), vattr);
1294 dp->d_time = parent_dv;
1296 afs_DestroyAttr(vattr);
1299 /* should we always update the attributes at this point? */
1300 /* unlikely--the vcache entry hasn't changed */
1305 /* If this code is ever enabled, we should use dget_parent to handle
1306 * getting the parent, and dput() to dispose of it. See above for an
1308 pvcp = VTOAFS(dp->d_parent->d_inode);
1309 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1313 /* No change in parent's DataVersion so this negative
1314 * lookup is still valid. BUT, if a server is down a
1315 * negative lookup can result so there should be a
1316 * liftime as well. For now, always expire.
1329 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1331 /* we hold the global lock if we evaluated a mount point */
1339 * If we had a negative lookup for the name we want to forcibly
1340 * unhash the dentry.
1341 * Otherwise use d_invalidate which will not unhash it if still in use.
1344 shrink_dcache_parent(dp);
1353 if (have_submounts(dp))
1361 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1363 struct vcache *vcp = VTOAFS(ip);
1366 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1367 (void) afs_InactiveVCache(vcp, NULL);
1370 afs_linux_clear_nfsfs_renamed(dp);
1376 #if defined(DOP_D_DELETE_TAKES_CONST)
1377 afs_dentry_delete(const struct dentry *dp)
1379 afs_dentry_delete(struct dentry *dp)
1382 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1383 return 1; /* bad inode? */
1388 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1389 static struct vfsmount *
1390 afs_dentry_automount(afs_linux_path_t *path)
1392 struct dentry *target;
1394 /* avoid symlink resolution limits when resolving; we cannot contribute to
1395 * an infinite symlink loop */
1396 current->total_link_count--;
1398 target = canonical_dentry(path->dentry->d_inode);
1400 if (target == path->dentry) {
1407 path->dentry = target;
1410 spin_lock(&path->dentry->d_lock);
1411 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1412 spin_unlock(&path->dentry->d_lock);
1417 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1419 struct dentry_operations afs_dentry_operations = {
1420 .d_revalidate = afs_linux_dentry_revalidate,
1421 .d_delete = afs_dentry_delete,
1422 .d_iput = afs_dentry_iput,
1423 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1424 .d_automount = afs_dentry_automount,
1425 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1428 /**********************************************************************
1429 * AFS Linux inode operations
1430 **********************************************************************/
1434 * Merely need to set enough of vattr to get us through the create. Note
1435 * that the higher level code (open_namei) will take care of any tuncation
1436 * explicitly. Exclusive open is also taken care of in open_namei.
1438 * name is in kernel space at this point.
1441 #if defined(IOP_CREATE_TAKES_BOOL)
1442 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1444 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1445 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1446 struct nameidata *nd)
1447 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1448 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1449 struct nameidata *nd)
1451 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1454 struct vattr *vattr = NULL;
1455 cred_t *credp = crref();
1456 const char *name = dp->d_name.name;
1462 code = afs_CreateAttr(&vattr);
1466 vattr->va_mode = mode;
1467 vattr->va_type = mode & S_IFMT;
1469 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1473 struct inode *ip = AFSTOV(vcp);
1475 afs_getattr(vcp, vattr, credp);
1476 afs_fill_inode(ip, vattr);
1477 insert_inode_hash(ip);
1478 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1479 dp->d_op = &afs_dentry_operations;
1481 dp->d_time = parent_vcache_dv(dip, credp);
1482 d_instantiate(dp, ip);
1485 afs_DestroyAttr(vattr);
1491 return afs_convert_code(code);
1494 /* afs_linux_lookup */
1495 static struct dentry *
1496 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1497 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1499 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1500 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1501 struct nameidata *nd)
1503 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1506 cred_t *credp = crref();
1507 struct vcache *vcp = NULL;
1508 const char *comp = dp->d_name.name;
1509 struct inode *ip = NULL;
1510 struct dentry *newdp = NULL;
1514 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1517 struct vattr *vattr = NULL;
1518 struct vcache *parent_vc = VTOAFS(dip);
1520 if (parent_vc == vcp) {
1521 /* This is possible if the parent dir is a mountpoint to a volume,
1522 * and the dir entry we looked up is a mountpoint to the same
1523 * volume. Linux cannot cope with this, so return an error instead
1524 * of risking a deadlock or panic. */
1531 code = afs_CreateAttr(&vattr);
1539 afs_getattr(vcp, vattr, credp);
1540 afs_fill_inode(ip, vattr);
1541 if (hlist_unhashed(&ip->i_hash))
1542 insert_inode_hash(ip);
1544 afs_DestroyAttr(vattr);
1546 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1547 dp->d_op = &afs_dentry_operations;
1549 dp->d_time = parent_vcache_dv(dip, credp);
1553 if (ip && S_ISDIR(ip->i_mode)) {
1554 d_prune_aliases(ip);
1556 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1557 ip->i_flags |= S_AUTOMOUNT;
1560 newdp = d_splice_alias(ip, dp);
1565 /* It's ok for the file to not be found. That's noted by the caller by
1566 * seeing that the dp->d_inode field is NULL.
1568 if (!code || code == ENOENT)
1571 return ERR_PTR(afs_convert_code(code));
1575 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1578 cred_t *credp = crref();
1579 const char *name = newdp->d_name.name;
1580 struct inode *oldip = olddp->d_inode;
1582 /* If afs_link returned the vnode, we could instantiate the
1583 * dentry. Since it's not, we drop this one and do a new lookup.
1588 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1592 return afs_convert_code(code);
1595 /* We have to have a Linux specific sillyrename function, because we
1596 * also have to keep the dcache up to date when we're doing a silly
1597 * rename - so we don't want the generic vnodeops doing this behind our
1602 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1605 struct vcache *tvc = VTOAFS(dentry->d_inode);
1606 struct dentry *__dp = NULL;
1607 char *__name = NULL;
1610 if (afs_linux_nfsfs_renamed(dentry))
1618 osi_FreeSmallSpace(__name);
1619 __name = afs_newname();
1622 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1625 osi_FreeSmallSpace(__name);
1628 } while (__dp->d_inode != NULL);
1631 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1632 VTOAFS(dir), (char *)__dp->d_name.name,
1635 tvc->mvid = (void *) __name;
1638 crfree(tvc->uncred);
1640 tvc->uncred = credp;
1641 tvc->f.states |= CUnlinked;
1642 afs_linux_set_nfsfs_renamed(dentry);
1644 osi_FreeSmallSpace(__name);
1649 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1650 d_move(dentry, __dp);
1659 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1662 cred_t *credp = crref();
1663 const char *name = dp->d_name.name;
1664 struct vcache *tvc = VTOAFS(dp->d_inode);
1666 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1667 && !(tvc->f.states & CUnlinked)) {
1669 code = afs_linux_sillyrename(dip, dp, credp);
1672 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1679 return afs_convert_code(code);
1684 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1687 cred_t *credp = crref();
1688 struct vattr *vattr = NULL;
1689 const char *name = dp->d_name.name;
1691 /* If afs_symlink returned the vnode, we could instantiate the
1692 * dentry. Since it's not, we drop this one and do a new lookup.
1697 code = afs_CreateAttr(&vattr);
1702 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1704 afs_DestroyAttr(vattr);
1709 return afs_convert_code(code);
1713 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1714 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1716 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1720 cred_t *credp = crref();
1721 struct vcache *tvcp = NULL;
1722 struct vattr *vattr = NULL;
1723 const char *name = dp->d_name.name;
1726 code = afs_CreateAttr(&vattr);
1731 vattr->va_mask = ATTR_MODE;
1732 vattr->va_mode = mode;
1734 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1737 struct inode *ip = AFSTOV(tvcp);
1739 afs_getattr(tvcp, vattr, credp);
1740 afs_fill_inode(ip, vattr);
1742 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1743 dp->d_op = &afs_dentry_operations;
1745 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1746 d_instantiate(dp, ip);
1748 afs_DestroyAttr(vattr);
1754 return afs_convert_code(code);
1758 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1761 cred_t *credp = crref();
1762 const char *name = dp->d_name.name;
1764 /* locking kernel conflicts with glock? */
1767 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1770 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1771 * that failed because a directory is not empty. So, we map
1772 * EEXIST to ENOTEMPTY on linux.
1774 if (code == EEXIST) {
1783 return afs_convert_code(code);
1788 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1789 struct inode *newip, struct dentry *newdp)
1792 cred_t *credp = crref();
1793 const char *oldname = olddp->d_name.name;
1794 const char *newname = newdp->d_name.name;
1795 struct dentry *rehash = NULL;
1797 /* Prevent any new references during rename operation. */
1799 if (!d_unhashed(newdp)) {
1804 afs_maybe_shrink_dcache(olddp);
1807 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1811 olddp->d_time = 0; /* force to revalidate */
1817 return afs_convert_code(code);
1821 /* afs_linux_ireadlink
1822 * Internal readlink which can return link contents to user or kernel space.
1823 * Note that the buffer is NOT supposed to be null-terminated.
1826 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1829 cred_t *credp = crref();
1833 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1834 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1838 return maxlen - tuio.uio_resid;
1840 return afs_convert_code(code);
1843 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1844 /* afs_linux_readlink
1845 * Fill target (which is in user space) with contents of symlink.
1848 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1851 struct inode *ip = dp->d_inode;
1854 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1860 /* afs_linux_follow_link
1861 * a file system dependent link following routine.
1863 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1868 name = kmalloc(PATH_MAX, GFP_NOFS);
1874 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1882 nd_set_link(nd, name);
1887 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1889 char *name = nd_get_link(nd);
1891 if (name && !IS_ERR(name))
1895 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1897 /* Populate a page by filling it from the cache file pointed at by cachefp
1898 * (which contains indicated chunk)
1899 * If task is NULL, the page copy occurs syncronously, and the routine
1900 * returns with page still locked. If task is non-NULL, then page copies
1901 * may occur in the background, and the page will be unlocked when it is
1905 afs_linux_read_cache(struct file *cachefp, struct page *page,
1906 int chunk, struct pagevec *lrupv,
1907 struct afs_pagecopy_task *task) {
1908 loff_t offset = page_offset(page);
1909 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1910 struct page *newpage, *cachepage;
1911 struct address_space *cachemapping;
1915 cachemapping = cacheinode->i_mapping;
1919 /* If we're trying to read a page that's past the end of the disk
1920 * cache file, then just return a zeroed page */
1921 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1922 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1923 SetPageUptodate(page);
1929 /* From our offset, we now need to work out which page in the disk
1930 * file it corresponds to. This will be fun ... */
1931 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1933 while (cachepage == NULL) {
1934 cachepage = find_get_page(cachemapping, pageindex);
1937 newpage = page_cache_alloc_cold(cachemapping);
1943 code = add_to_page_cache(newpage, cachemapping,
1944 pageindex, GFP_KERNEL);
1946 cachepage = newpage;
1949 page_cache_get(cachepage);
1950 if (!pagevec_add(lrupv, cachepage))
1951 __pagevec_lru_add_file(lrupv);
1954 page_cache_release(newpage);
1956 if (code != -EEXIST)
1960 lock_page(cachepage);
1964 if (!PageUptodate(cachepage)) {
1965 ClearPageError(cachepage);
1966 code = cachemapping->a_ops->readpage(NULL, cachepage);
1967 if (!code && !task) {
1968 wait_on_page_locked(cachepage);
1971 unlock_page(cachepage);
1975 if (PageUptodate(cachepage)) {
1976 copy_highpage(page, cachepage);
1977 flush_dcache_page(page);
1978 SetPageUptodate(page);
1983 afs_pagecopy_queue_page(task, cachepage, page);
1995 page_cache_release(cachepage);
2001 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2003 loff_t offset = page_offset(pp);
2004 struct inode *ip = FILE_INODE(fp);
2005 struct vcache *avc = VTOAFS(ip);
2007 struct file *cacheFp = NULL;
2010 struct pagevec lrupv;
2012 /* Not a UFS cache, don't do anything */
2013 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2016 /* No readpage (ex: tmpfs) , skip */
2017 if (cachefs_noreadpage)
2020 /* Can't do anything if the vcache isn't statd , or if the read
2021 * crosses a chunk boundary.
2023 if (!(avc->f.states & CStatd) ||
2024 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2028 ObtainWriteLock(&avc->lock, 911);
2030 /* XXX - See if hinting actually makes things faster !!! */
2032 /* See if we have a suitable entry already cached */
2036 /* We need to lock xdcache, then dcache, to handle situations where
2037 * the hint is on the free list. However, we can't safely do this
2038 * according to the locking hierarchy. So, use a non blocking lock.
2040 ObtainReadLock(&afs_xdcache);
2041 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2043 if (dcLocked && (tdc->index != NULLIDX)
2044 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2045 && tdc->f.chunk == AFS_CHUNK(offset)
2046 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2047 /* Bonus - the hint was correct */
2050 /* Only destroy the hint if its actually invalid, not if there's
2051 * just been a locking failure */
2053 ReleaseReadLock(&tdc->lock);
2060 ReleaseReadLock(&afs_xdcache);
2063 /* No hint, or hint is no longer valid - see if we can get something
2064 * directly from the dcache
2067 tdc = afs_FindDCache(avc, offset);
2070 ReleaseWriteLock(&avc->lock);
2075 ObtainReadLock(&tdc->lock);
2077 /* Is the dcache we've been given currently up to date */
2078 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2079 (tdc->dflags & DFFetching))
2082 /* Update our hint for future abuse */
2085 /* Okay, so we've now got a cache file that is up to date */
2087 /* XXX - I suspect we should be locking the inodes before we use them! */
2089 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2090 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2091 cachefs_noreadpage = 1;
2095 pagevec_init(&lrupv, 0);
2097 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
2099 if (pagevec_count(&lrupv))
2100 __pagevec_lru_add_file(&lrupv);
2102 filp_close(cacheFp, NULL);
2105 ReleaseReadLock(&tdc->lock);
2106 ReleaseWriteLock(&avc->lock);
2113 ReleaseWriteLock(&avc->lock);
2114 ReleaseReadLock(&tdc->lock);
2119 /* afs_linux_readpage
2121 * This function is split into two, because prepare_write/begin_write
2122 * require a readpage call which doesn't unlock the resulting page upon
2126 afs_linux_fillpage(struct file *fp, struct page *pp)
2131 struct iovec *iovecp;
2132 struct inode *ip = FILE_INODE(fp);
2133 afs_int32 cnt = page_count(pp);
2134 struct vcache *avc = VTOAFS(ip);
2135 afs_offs_t offset = page_offset(pp);
2139 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2149 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2150 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2152 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2157 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2158 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2159 99999); /* not a possible code value */
2161 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2163 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2164 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2166 AFS_DISCON_UNLOCK();
2169 /* XXX valid for no-cache also? Check last bits of files... :)
2170 * Cognate code goes in afs_NoCacheFetchProc. */
2171 if (auio->uio_resid) /* zero remainder of page */
2172 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2175 flush_dcache_page(pp);
2176 SetPageUptodate(pp);
2185 return afs_convert_code(code);
2189 afs_linux_prefetch(struct file *fp, struct page *pp)
2192 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2193 afs_offs_t offset = page_offset(pp);
2195 if (AFS_CHUNKOFFSET(offset) == 0) {
2197 struct vrequest *treq = NULL;
2202 code = afs_CreateReq(&treq, credp);
2203 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2204 tdc = afs_FindDCache(avc, offset);
2206 if (!(tdc->mflags & DFNextStarted))
2207 afs_PrefetchChunk(avc, tdc, credp, treq);
2210 ReleaseWriteLock(&avc->lock);
2212 afs_DestroyReq(treq);
2216 return afs_convert_code(code);
2221 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2222 struct list_head *page_list, unsigned num_pages)
2227 struct iovec* iovecp;
2228 struct nocache_read_request *ancr;
2230 struct pagevec lrupv;
2234 struct inode *ip = FILE_INODE(fp);
2235 struct vcache *avc = VTOAFS(ip);
2236 afs_int32 base_index = 0;
2237 afs_int32 page_count = 0;
2240 /* background thread must free: iovecp, auio, ancr */
2241 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2243 auio = osi_Alloc(sizeof(struct uio));
2244 auio->uio_iov = iovecp;
2245 auio->uio_iovcnt = num_pages;
2246 auio->uio_flag = UIO_READ;
2247 auio->uio_seg = AFS_UIOSYS;
2248 auio->uio_resid = num_pages * PAGE_SIZE;
2250 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2252 ancr->offset = auio->uio_offset;
2253 ancr->length = auio->uio_resid;
2255 pagevec_init(&lrupv, 0);
2257 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2259 if(list_empty(page_list))
2262 pp = list_entry(page_list->prev, struct page, lru);
2263 /* If we allocate a page and don't remove it from page_list,
2264 * the page cache gets upset. */
2266 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
2267 if(pp->index > isize) {
2274 offset = page_offset(pp);
2275 ancr->offset = auio->uio_offset = offset;
2276 base_index = pp->index;
2278 iovecp[page_ix].iov_len = PAGE_SIZE;
2279 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2280 if(base_index != pp->index) {
2283 page_cache_release(pp);
2284 iovecp[page_ix].iov_base = (void *) 0;
2286 ancr->length -= PAGE_SIZE;
2293 page_cache_release(pp);
2294 iovecp[page_ix].iov_base = (void *) 0;
2297 if(!PageLocked(pp)) {
2301 /* increment page refcount--our original design assumed
2302 * that locking it would effectively pin it; protect
2303 * ourselves from the possiblity that this assumption is
2304 * is faulty, at low cost (provided we do not fail to
2305 * do the corresponding decref on the other side) */
2308 /* save the page for background map */
2309 iovecp[page_ix].iov_base = (void*) pp;
2311 /* and put it on the LRU cache */
2312 if (!pagevec_add(&lrupv, pp))
2313 __pagevec_lru_add_file(&lrupv);
2317 /* If there were useful pages in the page list, make sure all pages
2318 * are in the LRU cache, then schedule the read */
2320 if (pagevec_count(&lrupv))
2321 __pagevec_lru_add_file(&lrupv);
2323 code = afs_ReadNoCache(avc, ancr, credp);
2326 /* If there is nothing for the background thread to handle,
2327 * it won't be freeing the things that we never gave it */
2328 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2329 osi_Free(auio, sizeof(struct uio));
2330 osi_Free(ancr, sizeof(struct nocache_read_request));
2332 /* we do not flush, release, or unmap pages--that will be
2333 * done for us by the background thread as each page comes in
2334 * from the fileserver */
2335 return afs_convert_code(code);
2340 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2342 cred_t *credp = NULL;
2344 struct iovec *iovecp;
2345 struct nocache_read_request *ancr;
2349 * Special case: if page is at or past end of file, just zero it and set
2352 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2353 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
2354 SetPageUptodate(pp);
2361 /* receiver frees */
2362 auio = osi_Alloc(sizeof(struct uio));
2363 iovecp = osi_Alloc(sizeof(struct iovec));
2365 /* address can be NULL, because we overwrite it with 'pp', below */
2366 setup_uio(auio, iovecp, NULL, page_offset(pp),
2367 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2369 /* save the page for background map */
2370 get_page(pp); /* see above */
2371 auio->uio_iov->iov_base = (void*) pp;
2372 /* the background thread will free this */
2373 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2375 ancr->offset = page_offset(pp);
2376 ancr->length = PAGE_SIZE;
2379 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2382 return afs_convert_code(code);
2386 afs_linux_can_bypass(struct inode *ip) {
2388 switch(cache_bypass_strategy) {
2389 case NEVER_BYPASS_CACHE:
2391 case ALWAYS_BYPASS_CACHE:
2393 case LARGE_FILES_BYPASS_CACHE:
2394 if (i_size_read(ip) > cache_bypass_threshold)
2401 /* Check if a file is permitted to bypass the cache by policy, and modify
2402 * the cache bypass state recorded for that file */
2405 afs_linux_bypass_check(struct inode *ip) {
2408 int bypass = afs_linux_can_bypass(ip);
2411 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2419 afs_linux_readpage(struct file *fp, struct page *pp)
2423 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2424 code = afs_linux_bypass_readpage(fp, pp);
2426 code = afs_linux_fillpage(fp, pp);
2428 code = afs_linux_prefetch(fp, pp);
2435 /* Readpages reads a number of pages for a particular file. We use
2436 * this to optimise the reading, by limiting the number of times upon which
2437 * we have to lookup, lock and open vcaches and dcaches
2441 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2442 struct list_head *page_list, unsigned int num_pages)
2444 struct inode *inode = mapping->host;
2445 struct vcache *avc = VTOAFS(inode);
2447 struct file *cacheFp = NULL;
2449 unsigned int page_idx;
2451 struct pagevec lrupv;
2452 struct afs_pagecopy_task *task;
2454 if (afs_linux_bypass_check(inode))
2455 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2457 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2460 /* No readpage (ex: tmpfs) , skip */
2461 if (cachefs_noreadpage)
2465 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2470 ObtainWriteLock(&avc->lock, 912);
2473 task = afs_pagecopy_init_task();
2476 pagevec_init(&lrupv, 0);
2477 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2478 struct page *page = list_entry(page_list->prev, struct page, lru);
2479 list_del(&page->lru);
2480 offset = page_offset(page);
2482 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2484 ReleaseReadLock(&tdc->lock);
2489 filp_close(cacheFp, NULL);
2494 if ((tdc = afs_FindDCache(avc, offset))) {
2495 ObtainReadLock(&tdc->lock);
2496 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2497 (tdc->dflags & DFFetching)) {
2498 ReleaseReadLock(&tdc->lock);
2505 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2506 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2507 cachefs_noreadpage = 1;
2513 if (tdc && !add_to_page_cache(page, mapping, page->index,
2515 page_cache_get(page);
2516 if (!pagevec_add(&lrupv, page))
2517 __pagevec_lru_add_file(&lrupv);
2519 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2521 page_cache_release(page);
2523 if (pagevec_count(&lrupv))
2524 __pagevec_lru_add_file(&lrupv);
2528 filp_close(cacheFp, NULL);
2530 afs_pagecopy_put_task(task);
2534 ReleaseReadLock(&tdc->lock);
2538 ReleaseWriteLock(&avc->lock);
2543 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2546 afs_linux_prepare_writeback(struct vcache *avc) {
2547 if (avc->f.states & CPageWrite) {
2548 return AOP_WRITEPAGE_ACTIVATE;
2550 avc->f.states |= CPageWrite;
2555 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2556 struct vrequest *treq = NULL;
2559 if (!afs_CreateReq(&treq, credp)) {
2560 code = afs_DoPartialWrite(avc, treq);
2561 afs_DestroyReq(treq);
2564 return afs_convert_code(code);
2568 afs_linux_complete_writeback(struct vcache *avc) {
2569 avc->f.states &= ~CPageWrite;
2572 /* Writeback a given page syncronously. Called with no AFS locks held */
2574 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2575 unsigned long offset, unsigned int count,
2578 struct vcache *vcp = VTOAFS(ip);
2586 buffer = kmap(pp) + offset;
2587 base = page_offset(pp) + offset;
2590 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2591 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2592 ICL_TYPE_INT32, 99999);
2594 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2596 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2598 i_size_write(ip, vcp->f.m.Length);
2599 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2601 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2603 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2604 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2605 ICL_TYPE_INT32, code);
2614 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2615 unsigned long offset, unsigned int count)
2619 struct vcache *vcp = VTOAFS(ip);
2622 /* Catch recursive writeback. This occurs if the kernel decides
2623 * writeback is required whilst we are writing to the cache, or
2624 * flushing to the server. When we're running syncronously (as
2625 * opposed to from writepage) we can't actually do anything about
2626 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2629 ObtainWriteLock(&vcp->lock, 532);
2630 afs_linux_prepare_writeback(vcp);
2631 ReleaseWriteLock(&vcp->lock);
2635 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2638 ObtainWriteLock(&vcp->lock, 533);
2640 code1 = afs_linux_dopartialwrite(vcp, credp);
2641 afs_linux_complete_writeback(vcp);
2642 ReleaseWriteLock(&vcp->lock);
2653 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2654 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2656 afs_linux_writepage(struct page *pp)
2659 struct address_space *mapping = pp->mapping;
2660 struct inode *inode;
2663 unsigned int to = PAGE_CACHE_SIZE;
2668 if (PageReclaim(pp)) {
2669 return AOP_WRITEPAGE_ACTIVATE;
2670 /* XXX - Do we need to redirty the page here? */
2675 inode = mapping->host;
2676 vcp = VTOAFS(inode);
2677 isize = i_size_read(inode);
2679 /* Don't defeat an earlier truncate */
2680 if (page_offset(pp) > isize) {
2681 set_page_writeback(pp);
2687 ObtainWriteLock(&vcp->lock, 537);
2688 code = afs_linux_prepare_writeback(vcp);
2689 if (code == AOP_WRITEPAGE_ACTIVATE) {
2690 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2691 * to return with the page still locked */
2692 ReleaseWriteLock(&vcp->lock);
2697 /* Grab the creds structure currently held in the vnode, and
2698 * get a reference to it, in case it goes away ... */
2704 ReleaseWriteLock(&vcp->lock);
2707 set_page_writeback(pp);
2709 SetPageUptodate(pp);
2711 /* We can unlock the page here, because it's protected by the
2712 * page_writeback flag. This should make us less vulnerable to
2713 * deadlocking in afs_write and afs_DoPartialWrite
2717 /* If this is the final page, then just write the number of bytes that
2718 * are actually in it */
2719 if ((isize - page_offset(pp)) < to )
2720 to = isize - page_offset(pp);
2722 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2725 ObtainWriteLock(&vcp->lock, 538);
2727 /* As much as we might like to ignore a file server error here,
2728 * and just try again when we close(), unfortunately StoreAllSegments
2729 * will invalidate our chunks if the server returns a permanent error,
2730 * so we need to at least try and get that error back to the user
2733 code1 = afs_linux_dopartialwrite(vcp, credp);
2735 afs_linux_complete_writeback(vcp);
2736 ReleaseWriteLock(&vcp->lock);
2741 end_page_writeback(pp);
2742 page_cache_release(pp);
2753 /* afs_linux_permission
2754 * Check access rights - returns error if can't check or permission denied.
2757 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2758 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2759 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2760 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2762 afs_linux_permission(struct inode *ip, int mode)
2769 /* Check for RCU path walking */
2770 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2771 if (flags & IPERM_FLAG_RCU)
2773 #elif defined(MAY_NOT_BLOCK)
2774 if (mode & MAY_NOT_BLOCK)
2780 if (mode & MAY_EXEC)
2782 if (mode & MAY_READ)
2784 if (mode & MAY_WRITE)
2786 code = afs_access(VTOAFS(ip), tmp, credp);
2790 return afs_convert_code(code);
2794 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2798 struct inode *inode = FILE_INODE(file);
2799 loff_t pagebase = page_offset(page);
2801 if (i_size_read(inode) < (pagebase + offset))
2802 i_size_write(inode, pagebase + offset);
2804 if (PageChecked(page)) {
2805 SetPageUptodate(page);
2806 ClearPageChecked(page);
2809 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2815 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2819 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2820 * prepare_write. Essentially, if the page exists within the file,
2821 * and is not being fully written, then we should populate it.
2824 if (!PageUptodate(page)) {
2825 loff_t pagebase = page_offset(page);
2826 loff_t isize = i_size_read(page->mapping->host);
2828 /* Is the location we are writing to beyond the end of the file? */
2829 if (pagebase >= isize ||
2830 ((from == 0) && (pagebase + to) >= isize)) {
2831 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2832 SetPageChecked(page);
2833 /* Are we we writing a full page */
2834 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2835 SetPageChecked(page);
2836 /* Is the page readable, if it's wronly, we don't care, because we're
2837 * not actually going to read from it ... */
2838 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2839 /* We don't care if fillpage fails, because if it does the page
2840 * won't be marked as up to date
2842 afs_linux_fillpage(file, page);
2848 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2850 afs_linux_write_end(struct file *file, struct address_space *mapping,
2851 loff_t pos, unsigned len, unsigned copied,
2852 struct page *page, void *fsdata)
2855 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2857 code = afs_linux_commit_write(file, page, from, from + len);
2860 page_cache_release(page);
2865 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2866 loff_t pos, unsigned len, unsigned flags,
2867 struct page **pagep, void **fsdata)
2870 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2871 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2874 page = grab_cache_page_write_begin(mapping, index, flags);
2877 code = afs_linux_prepare_write(file, page, from, from + len);
2880 page_cache_release(page);
2887 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2889 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2891 struct dentry **dpp;
2892 struct dentry *target;
2894 if (current->total_link_count > 0) {
2895 /* avoid symlink resolution limits when resolving; we cannot contribute to
2896 * an infinite symlink loop */
2897 /* only do this for follow_link when total_link_count is positive to be
2898 * on the safe side; there is at least one code path in the Linux
2899 * kernel where it seems like it may be possible to get here without
2900 * total_link_count getting incremented. it is not clear on how that
2901 * path is actually reached, but guard against it just to be safe */
2902 current->total_link_count--;
2905 target = canonical_dentry(dentry->d_inode);
2907 # ifdef STRUCT_NAMEIDATA_HAS_PATH
2908 dpp = &nd->path.dentry;
2918 *dpp = dget(dentry);
2921 nd->last_type = LAST_BIND;
2925 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
2928 static struct inode_operations afs_file_iops = {
2929 .permission = afs_linux_permission,
2930 .getattr = afs_linux_getattr,
2931 .setattr = afs_notify_change,
2934 static struct address_space_operations afs_file_aops = {
2935 .readpage = afs_linux_readpage,
2936 .readpages = afs_linux_readpages,
2937 .writepage = afs_linux_writepage,
2938 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2939 .write_begin = afs_linux_write_begin,
2940 .write_end = afs_linux_write_end,
2942 .commit_write = afs_linux_commit_write,
2943 .prepare_write = afs_linux_prepare_write,
2948 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2949 * by what sort of operation is allowed.....
2952 static struct inode_operations afs_dir_iops = {
2953 .setattr = afs_notify_change,
2954 .create = afs_linux_create,
2955 .lookup = afs_linux_lookup,
2956 .link = afs_linux_link,
2957 .unlink = afs_linux_unlink,
2958 .symlink = afs_linux_symlink,
2959 .mkdir = afs_linux_mkdir,
2960 .rmdir = afs_linux_rmdir,
2961 .rename = afs_linux_rename,
2962 .getattr = afs_linux_getattr,
2963 .permission = afs_linux_permission,
2964 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2965 .follow_link = afs_linux_dir_follow_link,
2969 /* We really need a separate symlink set of ops, since do_follow_link()
2970 * determines if it _is_ a link by checking if the follow_link op is set.
2972 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2974 afs_symlink_filler(struct file *file, struct page *page)
2976 struct inode *ip = (struct inode *)page->mapping->host;
2977 char *p = (char *)kmap(page);
2981 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2986 p[code] = '\0'; /* null terminate? */
2988 SetPageUptodate(page);
3000 static struct address_space_operations afs_symlink_aops = {
3001 .readpage = afs_symlink_filler
3003 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3005 static struct inode_operations afs_symlink_iops = {
3006 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3007 .readlink = page_readlink,
3008 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3009 .follow_link = page_follow_link,
3011 .follow_link = page_follow_link_light,
3012 .put_link = page_put_link,
3014 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3015 .readlink = afs_linux_readlink,
3016 .follow_link = afs_linux_follow_link,
3017 .put_link = afs_linux_put_link,
3018 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3019 .setattr = afs_notify_change,
3023 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3027 vattr2inode(ip, vattr);
3029 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3030 /* Reset ops if symlink or directory. */
3031 if (S_ISREG(ip->i_mode)) {
3032 ip->i_op = &afs_file_iops;
3033 ip->i_fop = &afs_file_fops;
3034 ip->i_data.a_ops = &afs_file_aops;
3036 } else if (S_ISDIR(ip->i_mode)) {
3037 ip->i_op = &afs_dir_iops;
3038 ip->i_fop = &afs_dir_fops;
3040 } else if (S_ISLNK(ip->i_mode)) {
3041 ip->i_op = &afs_symlink_iops;
3042 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3043 ip->i_data.a_ops = &afs_symlink_aops;
3044 ip->i_mapping = &ip->i_data;