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 #if defined(HAVE_LINUX_LRU_CACHE_ADD_FILE)
35 # include <linux/swap.h>
37 # include <linux/pagevec.h>
39 #include <linux/aio.h>
41 #include "afs/afs_bypasscache.h"
43 #include "osi_compat.h"
44 #include "osi_pagecopy.h"
47 #define MAX_ERRNO 1000L
50 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
51 /* Enable our workaround for a race with d_splice_alias. The race was fixed in
52 * 2.6.34, so don't do it after that point. */
53 # define D_SPLICE_ALIAS_RACE
56 /* Workaround for RH 7.5 which introduced file operation iterate() but requires
57 * each file->f_mode to be marked with FMODE_KABI_ITERATE. Instead OpenAFS will
58 * continue to use file opearation readdir() in this case.
60 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE) && !defined(FMODE_KABI_ITERATE)
61 #define USE_FOP_ITERATE 1
63 #undef USE_FOP_ITERATE
66 int cachefs_noreadpage = 0;
68 extern struct backing_dev_info *afs_backing_dev_info;
70 extern struct vcache *afs_globalVp;
72 /* Handle interfacing with Linux's pagevec/lru facilities */
74 #if defined(HAVE_LINUX_LRU_CACHE_ADD_FILE) || defined(HAVE_LINUX_LRU_CACHE_ADD)
77 * Linux's lru_cache_add_file provides a simplified LRU interface without
80 struct afs_lru_pages {
85 afs_lru_cache_init(struct afs_lru_pages *alrupages)
91 afs_lru_cache_add(struct afs_lru_pages *alrupages, struct page *page)
93 # if defined(HAVE_LINUX_LRU_CACHE_ADD)
95 # elif defined(HAVE_LINUX_LRU_CACHE_ADD_FILE)
96 lru_cache_add_file(page);
98 # error need a kernel function to add a page to the kernel lru cache
103 afs_lru_cache_finalize(struct afs_lru_pages *alrupages)
109 /* Linux's pagevec/lru interfaces require a pagevec */
110 struct afs_lru_pages {
111 struct pagevec lrupv;
115 afs_lru_cache_init(struct afs_lru_pages *alrupages)
117 # if defined(PAGEVEC_INIT_COLD_ARG)
118 pagevec_init(&alrupages->lrupv, 0);
120 pagevec_init(&alrupages->lrupv);
124 # ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
125 # define __pagevec_lru_add_file __pagevec_lru_add
129 afs_lru_cache_add(struct afs_lru_pages *alrupages, struct page *page)
132 if (!pagevec_add(&alrupages->lrupv, page))
133 __pagevec_lru_add_file(&alrupages->lrupv);
137 afs_lru_cache_finalize(struct afs_lru_pages *alrupages)
139 if (pagevec_count(&alrupages->lrupv))
140 __pagevec_lru_add_file(&alrupages->lrupv);
142 #endif /* !HAVE_LINUX_LRU_ADD_FILE */
144 /* This function converts a positive error code from AFS into a negative
145 * code suitable for passing into the Linux VFS layer. It checks that the
146 * error code is within the permissable bounds for the ERR_PTR mechanism.
148 * _All_ error codes which come from the AFS layer should be passed through
149 * this function before being returned to the kernel.
153 afs_convert_code(int code) {
154 if ((code >= 0) && (code <= MAX_ERRNO))
160 /* Linux doesn't require a credp for many functions, and crref is an expensive
161 * operation. This helper function avoids obtaining it for VerifyVCache calls
165 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
166 cred_t *credp = NULL;
167 struct vrequest *treq = NULL;
170 if (avc->f.states & CStatd) {
178 code = afs_CreateReq(&treq, credp);
180 code = afs_VerifyVCache(avc, treq);
181 afs_DestroyReq(treq);
189 return afs_convert_code(code);
192 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
193 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
195 afs_linux_read_iter(struct kiocb *iocb, struct iov_iter *iter)
196 # elif defined(LINUX_HAS_NONVECTOR_AIO)
198 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
202 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
203 unsigned long bufsize, loff_t pos)
206 struct file *fp = iocb->ki_filp;
208 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
209 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
210 loff_t pos = iocb->ki_pos;
211 unsigned long bufsize = iter->nr_segs;
216 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
217 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
218 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
219 code = afs_linux_VerifyVCache(vcp, NULL);
222 /* Linux's FlushPages implementation doesn't ever use credp,
223 * so we optimise by not using it */
224 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
226 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
227 code = generic_file_read_iter(iocb, iter);
229 code = generic_file_aio_read(iocb, buf, bufsize, pos);
234 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
235 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
236 (afs_int32)bufsize, ICL_TYPE_INT32, code);
242 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
245 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
248 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
249 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
251 code = afs_linux_VerifyVCache(vcp, NULL);
254 /* Linux's FlushPages implementation doesn't ever use credp,
255 * so we optimise by not using it */
256 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
258 code = do_sync_read(fp, buf, count, offp);
262 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
263 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
271 /* Now we have integrated VM for writes as well as reads. the generic write operations
272 * also take care of re-positioning the pointer if file is open in append
273 * mode. Call fake open/close to ensure we do writes of core dumps.
275 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
276 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
278 afs_linux_write_iter(struct kiocb *iocb, struct iov_iter *iter)
279 # elif defined(LINUX_HAS_NONVECTOR_AIO)
281 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
285 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
286 unsigned long bufsize, loff_t pos)
290 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
292 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
293 loff_t pos = iocb->ki_pos;
294 unsigned long bufsize = iter->nr_segs;
299 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
300 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
301 (afs_int32)bufsize, ICL_TYPE_INT32,
302 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
304 code = afs_linux_VerifyVCache(vcp, &credp);
306 ObtainWriteLock(&vcp->lock, 529);
308 ReleaseWriteLock(&vcp->lock);
311 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
312 code = generic_file_write_iter(iocb, iter);
314 code = generic_file_aio_write(iocb, buf, bufsize, pos);
319 ObtainWriteLock(&vcp->lock, 530);
321 if (vcp->execsOrWriters == 1 && !credp)
324 afs_FakeClose(vcp, credp);
325 ReleaseWriteLock(&vcp->lock);
327 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
328 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
329 (afs_int32)bufsize, ICL_TYPE_INT32, code);
338 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
341 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
346 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
347 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
348 (fp->f_flags & O_APPEND) ? 99998 : 99999);
350 code = afs_linux_VerifyVCache(vcp, &credp);
352 ObtainWriteLock(&vcp->lock, 529);
354 ReleaseWriteLock(&vcp->lock);
357 code = do_sync_write(fp, buf, count, offp);
361 ObtainWriteLock(&vcp->lock, 530);
363 if (vcp->execsOrWriters == 1 && !credp)
366 afs_FakeClose(vcp, credp);
367 ReleaseWriteLock(&vcp->lock);
369 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
370 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
380 extern int BlobScan(struct dcache * afile, afs_int32 ablob, afs_int32 *ablobOut);
382 /* This is a complete rewrite of afs_readdir, since we can make use of
383 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
384 * handling and use of bulkstats will need to be reflected here as well.
387 #if defined(USE_FOP_ITERATE)
388 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
390 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
393 struct vcache *avc = VTOAFS(FILE_INODE(fp));
394 struct vrequest *treq = NULL;
400 struct DirBuffer entry;
403 afs_size_t origOffset, tlen;
404 cred_t *credp = crref();
405 struct afs_fakestat_state fakestat;
408 AFS_STATCNT(afs_readdir);
410 code = afs_convert_code(afs_CreateReq(&treq, credp));
415 afs_InitFakeStat(&fakestat);
416 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, treq));
420 /* update the cache entry */
422 code = afs_convert_code(afs_VerifyVCache(avc, treq));
426 /* get a reference to the entire directory */
427 tdc = afs_GetDCache(avc, (afs_size_t) 0, treq, &origOffset, &tlen, 1);
433 ObtainWriteLock(&avc->lock, 811);
434 ObtainReadLock(&tdc->lock);
436 * Make sure that the data in the cache is current. There are two
437 * cases we need to worry about:
438 * 1. The cache data is being fetched by another process.
439 * 2. The cache data is no longer valid
441 while ((avc->f.states & CStatd)
442 && (tdc->dflags & DFFetching)
443 && afs_IsDCacheFresh(tdc, avc)) {
444 ReleaseReadLock(&tdc->lock);
445 ReleaseWriteLock(&avc->lock);
446 afs_osi_Sleep(&tdc->validPos);
447 ObtainWriteLock(&avc->lock, 812);
448 ObtainReadLock(&tdc->lock);
450 if (!(avc->f.states & CStatd)
451 || !afs_IsDCacheFresh(tdc, avc)) {
452 ReleaseReadLock(&tdc->lock);
453 ReleaseWriteLock(&avc->lock);
458 /* Set the readdir-in-progress flag, and downgrade the lock
459 * to shared so others will be able to acquire a read lock.
461 avc->f.states |= CReadDir;
462 avc->dcreaddir = tdc;
463 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
464 ConvertWToSLock(&avc->lock);
466 /* Fill in until we get an error or we're done. This implementation
467 * takes an offset in units of blobs, rather than bytes.
470 #if defined(USE_FOP_ITERATE)
473 offset = (int) fp->f_pos;
477 code = BlobScan(tdc, offset, &dirpos);
478 if (code == 0 && dirpos == 0) {
479 /* We've reached EOF of the dir blob, so we can stop looking for
485 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
488 if (!(avc->f.states & CCorrupt)) {
489 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
490 afs_warn("afs: Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)\n",
491 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
492 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
493 tc ? tc->cellName : "",
494 (unsigned long)&tdc->f.inode, dirpos);
496 afs_PutCell(tc, READ_LOCK);
497 UpgradeSToWLock(&avc->lock, 814);
498 avc->f.states |= CCorrupt;
504 de = (struct DirEntry *)entry.data;
505 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
506 ntohl(de->fid.vnode));
507 len = strlen(de->name);
509 /* filldir returns -EINVAL when the buffer is full. */
511 unsigned int type = DT_UNKNOWN;
512 struct VenusFid afid;
515 afid.Cell = avc->f.fid.Cell;
516 afid.Fid.Volume = avc->f.fid.Fid.Volume;
517 afid.Fid.Vnode = ntohl(de->fid.vnode);
518 afid.Fid.Unique = ntohl(de->fid.vunique);
519 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
521 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
522 if (tvc->mvstat != AFS_MVSTAT_FILE) {
524 } else if (((tvc->f.states) & (CStatd | CTruth))) {
525 /* CTruth will be set if the object has
530 else if (vtype == VREG)
532 /* Don't do this until we're sure it can't be a mtpt */
533 /* else if (vtype == VLNK)
535 /* what other types does AFS support? */
537 /* clean up from afs_FindVCache */
541 * If this is NFS readdirplus, then the filler is going to
542 * call getattr on this inode, which will deadlock if we're
546 #if defined(USE_FOP_ITERATE)
547 /* dir_emit returns a bool - true when it succeeds.
548 * Inverse the result to fit with how we check "code" */
549 code = !dir_emit(ctx, de->name, len, ino, type);
551 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
558 offset = dirpos + 1 + ((len + 16) >> 5);
560 /* If filldir didn't fill in the last one this is still pointing to that
566 #if defined(USE_FOP_ITERATE)
567 ctx->pos = (loff_t) offset;
569 fp->f_pos = (loff_t) offset;
571 ReleaseReadLock(&tdc->lock);
573 UpgradeSToWLock(&avc->lock, 813);
574 avc->f.states &= ~CReadDir;
576 avc->readdir_pid = 0;
577 ReleaseSharedLock(&avc->lock);
580 afs_PutFakeStat(&fakestat);
581 afs_DestroyReq(treq);
588 /* in afs_pioctl.c */
589 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
592 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
593 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
595 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
602 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
604 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
608 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
609 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_LONG,
610 vmap->vm_end - vmap->vm_start, ICL_TYPE_LONG, 0);
612 /* get a validated vcache entry */
613 code = afs_linux_VerifyVCache(vcp, NULL);
616 /* Linux's Flushpage implementation doesn't use credp, so optimise
617 * our code to not need to crref() it */
618 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
620 code = generic_file_mmap(fp, vmap);
623 vcp->f.states |= CMAPPED;
631 afs_linux_open(struct inode *ip, struct file *fp)
633 struct vcache *vcp = VTOAFS(ip);
634 cred_t *credp = crref();
638 code = afs_open(&vcp, fp->f_flags, credp);
642 return afs_convert_code(code);
646 afs_linux_release(struct inode *ip, struct file *fp)
648 struct vcache *vcp = VTOAFS(ip);
649 cred_t *credp = crref();
653 code = afs_close(vcp, fp->f_flags, credp);
654 ObtainWriteLock(&vcp->lock, 807);
659 ReleaseWriteLock(&vcp->lock);
663 return afs_convert_code(code);
667 #if defined(FOP_FSYNC_TAKES_DENTRY)
668 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
669 #elif defined(FOP_FSYNC_TAKES_RANGE)
670 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
672 afs_linux_fsync(struct file *fp, int datasync)
676 struct inode *ip = FILE_INODE(fp);
677 cred_t *credp = crref();
679 #if defined(FOP_FSYNC_TAKES_RANGE)
680 afs_linux_lock_inode(ip);
683 code = afs_fsync(VTOAFS(ip), credp);
685 #if defined(FOP_FSYNC_TAKES_RANGE)
686 afs_linux_unlock_inode(ip);
689 return afs_convert_code(code);
695 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
698 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
699 cred_t *credp = crref();
700 struct AFS_FLOCK flock;
702 /* Convert to a lock format afs_lockctl understands. */
703 memset(&flock, 0, sizeof(flock));
704 flock.l_type = flp->fl_type;
705 flock.l_pid = flp->fl_pid;
707 flock.l_start = flp->fl_start;
708 if (flp->fl_end == OFFSET_MAX)
709 flock.l_len = 0; /* Lock to end of file */
711 flock.l_len = flp->fl_end - flp->fl_start + 1;
713 /* Safe because there are no large files, yet */
714 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
715 if (cmd == F_GETLK64)
717 else if (cmd == F_SETLK64)
719 else if (cmd == F_SETLKW64)
721 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
724 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
727 if ((code == 0 || flp->fl_type == F_UNLCK) &&
728 (cmd == F_SETLK || cmd == F_SETLKW)) {
729 code = afs_posix_lock_file(fp, flp);
730 if (code && flp->fl_type != F_UNLCK) {
731 struct AFS_FLOCK flock2;
733 flock2.l_type = F_UNLCK;
735 afs_lockctl(vcp, &flock2, F_SETLK, credp);
739 /* If lockctl says there are no conflicting locks, then also check with the
740 * kernel, as lockctl knows nothing about byte range locks
742 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
743 afs_posix_test_lock(fp, flp);
744 /* If we found a lock in the kernel's structure, return it */
745 if (flp->fl_type != F_UNLCK) {
751 /* Convert flock back to Linux's file_lock */
752 flp->fl_type = flock.l_type;
753 flp->fl_pid = flock.l_pid;
754 flp->fl_start = flock.l_start;
755 if (flock.l_len == 0)
756 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
758 flp->fl_end = flock.l_start + flock.l_len - 1;
764 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
766 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
768 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
769 cred_t *credp = crref();
770 struct AFS_FLOCK flock;
771 /* Convert to a lock format afs_lockctl understands. */
772 memset(&flock, 0, sizeof(flock));
773 flock.l_type = flp->fl_type;
774 flock.l_pid = flp->fl_pid;
779 /* Safe because there are no large files, yet */
780 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
781 if (cmd == F_GETLK64)
783 else if (cmd == F_SETLK64)
785 else if (cmd == F_SETLKW64)
787 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
790 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
793 if ((code == 0 || flp->fl_type == F_UNLCK) &&
794 (cmd == F_SETLK || cmd == F_SETLKW)) {
795 flp->fl_flags &=~ FL_SLEEP;
796 code = flock_lock_file_wait(fp, flp);
797 if (code && flp->fl_type != F_UNLCK) {
798 struct AFS_FLOCK flock2;
800 flock2.l_type = F_UNLCK;
802 afs_lockctl(vcp, &flock2, F_SETLK, credp);
806 /* Convert flock back to Linux's file_lock */
807 flp->fl_type = flock.l_type;
808 flp->fl_pid = flock.l_pid;
816 * essentially the same as afs_fsync() but we need to get the return
817 * code for the sys_close() here, not afs_linux_release(), so call
818 * afs_StoreAllSegments() with AFS_LASTSTORE
821 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
822 afs_linux_flush(struct file *fp, fl_owner_t id)
824 afs_linux_flush(struct file *fp)
827 struct vrequest *treq = NULL;
835 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
843 vcp = VTOAFS(FILE_INODE(fp));
845 code = afs_CreateReq(&treq, credp);
848 /* If caching is bypassed for this file, or globally, just return 0 */
849 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
852 ObtainReadLock(&vcp->lock);
853 if (vcp->cachingStates & FCSBypass)
855 ReleaseReadLock(&vcp->lock);
858 /* future proof: don't rely on 0 return from afs_InitReq */
863 ObtainSharedLock(&vcp->lock, 535);
864 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
865 UpgradeSToWLock(&vcp->lock, 536);
866 if (!AFS_IS_DISCONNECTED) {
867 code = afs_StoreAllSegments(vcp,
869 AFS_SYNC | AFS_LASTSTORE);
871 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
873 ConvertWToSLock(&vcp->lock);
875 code = afs_CheckCode(code, treq, 54);
876 ReleaseSharedLock(&vcp->lock);
879 afs_DestroyReq(treq);
884 return afs_convert_code(code);
887 struct file_operations afs_dir_fops = {
888 .read = generic_read_dir,
889 #if defined(USE_FOP_ITERATE)
890 .iterate = afs_linux_readdir,
892 .readdir = afs_linux_readdir,
894 #ifdef HAVE_UNLOCKED_IOCTL
895 .unlocked_ioctl = afs_unlocked_xioctl,
899 #ifdef HAVE_COMPAT_IOCTL
900 .compat_ioctl = afs_unlocked_xioctl,
902 .open = afs_linux_open,
903 .release = afs_linux_release,
904 .llseek = default_llseek,
905 #ifdef HAVE_LINUX_NOOP_FSYNC
908 .fsync = simple_sync_file,
912 struct file_operations afs_file_fops = {
913 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
914 .read_iter = afs_linux_read_iter,
915 .write_iter = afs_linux_write_iter,
916 # if !defined(HAVE_LINUX___VFS_WRITE) && !defined(HAVE_LINUX_KERNEL_WRITE)
917 .read = new_sync_read,
918 .write = new_sync_write,
920 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
921 .aio_read = afs_linux_aio_read,
922 .aio_write = afs_linux_aio_write,
923 .read = do_sync_read,
924 .write = do_sync_write,
926 .read = afs_linux_read,
927 .write = afs_linux_write,
929 #ifdef HAVE_UNLOCKED_IOCTL
930 .unlocked_ioctl = afs_unlocked_xioctl,
934 #ifdef HAVE_COMPAT_IOCTL
935 .compat_ioctl = afs_unlocked_xioctl,
937 .mmap = afs_linux_mmap,
938 .open = afs_linux_open,
939 .flush = afs_linux_flush,
940 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
941 .sendfile = generic_file_sendfile,
943 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE) && !defined(HAVE_LINUX_DEFAULT_FILE_SPLICE_READ)
944 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
945 .splice_write = iter_file_splice_write,
947 .splice_write = generic_file_splice_write,
949 .splice_read = generic_file_splice_read,
951 .release = afs_linux_release,
952 .fsync = afs_linux_fsync,
953 .lock = afs_linux_lock,
954 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
955 .flock = afs_linux_flock,
957 .llseek = default_llseek,
960 static struct dentry *
961 canonical_dentry(struct inode *ip)
963 struct vcache *vcp = VTOAFS(ip);
964 struct dentry *first = NULL, *ret = NULL, *cur;
965 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
966 struct hlist_node *p;
970 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
971 * otherwise, use the first dentry in ip->i_dentry.
972 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
974 /* note that vcp->target_link specifies which dentry to use, but we have
975 * no reference held on that dentry. so, we cannot use or dereference
976 * vcp->target_link itself, since it may have been freed. instead, we only
977 * use it to compare to pointers in the ip->i_dentry list. */
981 afs_d_alias_lock(ip);
983 #if defined(D_ALIAS_IS_HLIST)
984 # if defined(HLIST_ITERATOR_NO_NODE)
985 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
987 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
990 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
993 if (!vcp->target_link || cur == vcp->target_link) {
1002 if (!ret && first) {
1006 vcp->target_link = ret;
1009 afs_linux_dget(ret);
1011 afs_d_alias_unlock(ip);
1016 /**********************************************************************
1017 * AFS Linux dentry operations
1018 **********************************************************************/
1020 /* afs_linux_revalidate
1021 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
1024 afs_linux_revalidate(struct dentry *dp)
1026 struct vattr *vattr = NULL;
1027 struct vcache *vcp = VTOAFS(dp->d_inode);
1031 if (afs_shuttingdown != AFS_RUNNING)
1036 code = afs_CreateAttr(&vattr);
1041 /* This avoids the crref when we don't have to do it. Watch for
1042 * changes in afs_getattr that don't get replicated here!
1044 if (vcp->f.states & CStatd &&
1045 (!afs_fakestat_enable || vcp->mvstat != AFS_MVSTAT_MTPT) &&
1047 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
1048 code = afs_CopyOutAttrs(vcp, vattr);
1051 code = afs_getattr(vcp, vattr, credp);
1056 afs_fill_inode(AFSTOV(vcp), vattr);
1058 afs_DestroyAttr(vattr);
1063 return afs_convert_code(code);
1067 * Set iattr data into vattr. Assume vattr cleared before call.
1070 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1072 vattrp->va_mask = iattrp->ia_valid;
1073 if (iattrp->ia_valid & ATTR_MODE)
1074 vattrp->va_mode = iattrp->ia_mode;
1075 if (iattrp->ia_valid & ATTR_UID)
1076 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1077 if (iattrp->ia_valid & ATTR_GID)
1078 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1079 if (iattrp->ia_valid & ATTR_SIZE)
1080 vattrp->va_size = iattrp->ia_size;
1081 if (iattrp->ia_valid & ATTR_ATIME) {
1082 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1083 vattrp->va_atime.tv_nsec = 0;
1085 if (iattrp->ia_valid & ATTR_MTIME) {
1086 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1087 vattrp->va_mtime.tv_nsec = 0;
1089 if (iattrp->ia_valid & ATTR_CTIME) {
1090 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1091 vattrp->va_ctime.tv_nsec = 0;
1096 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1099 vattr2inode(struct inode *ip, struct vattr *vp)
1101 ip->i_ino = vp->va_nodeid;
1102 #ifdef HAVE_LINUX_SET_NLINK
1103 set_nlink(ip, vp->va_nlink);
1105 ip->i_nlink = vp->va_nlink;
1107 ip->i_blocks = vp->va_blocks;
1108 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1109 ip->i_blkbits = AFS_BLKBITS;
1111 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1112 ip->i_blksize = vp->va_blocksize;
1114 ip->i_rdev = vp->va_rdev;
1115 ip->i_mode = vp->va_mode;
1116 ip->i_uid = afs_make_kuid(vp->va_uid);
1117 ip->i_gid = afs_make_kgid(vp->va_gid);
1118 i_size_write(ip, vp->va_size);
1119 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1120 ip->i_atime.tv_nsec = 0;
1121 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1122 /* Set the mtime nanoseconds to the sysname generation number.
1123 * This convinces NFS clients that all directories have changed
1124 * any time the sysname list changes.
1126 ip->i_mtime.tv_nsec = afs_sysnamegen;
1127 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1128 ip->i_ctime.tv_nsec = 0;
1131 /* afs_notify_change
1132 * Linux version of setattr call. What to change is in the iattr struct.
1133 * We need to set bits in both the Linux inode as well as the vcache.
1136 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1138 struct vattr *vattr = NULL;
1139 cred_t *credp = crref();
1140 struct inode *ip = dp->d_inode;
1144 code = afs_CreateAttr(&vattr);
1149 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1151 code = afs_setattr(VTOAFS(ip), vattr, credp);
1153 afs_getattr(VTOAFS(ip), vattr, credp);
1154 vattr2inode(ip, vattr);
1156 afs_DestroyAttr(vattr);
1161 return afs_convert_code(code);
1164 #if defined(IOP_GETATTR_TAKES_PATH_STRUCT)
1166 afs_linux_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int sync_mode)
1168 int err = afs_linux_revalidate(path->dentry);
1170 generic_fillattr(path->dentry->d_inode, stat);
1176 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1178 int err = afs_linux_revalidate(dentry);
1180 generic_fillattr(dentry->d_inode, stat);
1187 parent_vcache_dv(struct inode *inode, cred_t *credp)
1190 struct vcache *pvcp;
1193 * If parent is a mount point and we are using fakestat, we may need
1194 * to look at the fake vcache entry instead of what the vfs is giving
1195 * us. The fake entry is the one with the useful DataVersion.
1197 pvcp = VTOAFS(inode);
1198 if (pvcp->mvstat == AFS_MVSTAT_MTPT && afs_fakestat_enable) {
1199 struct vrequest treq;
1200 struct afs_fakestat_state fakestate;
1206 afs_InitReq(&treq, credp);
1207 afs_InitFakeStat(&fakestate);
1208 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1211 afs_PutFakeStat(&fakestate);
1213 return hgetlo(pvcp->f.m.DataVersion);
1217 filter_enoent(int code)
1219 #ifdef HAVE_LINUX_FATAL_SIGNAL_PENDING
1220 if (code == ENOENT && fatal_signal_pending(current)) {
1227 #ifndef D_SPLICE_ALIAS_RACE
1229 static inline void dentry_race_lock(void) {}
1230 static inline void dentry_race_unlock(void) {}
1234 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
1235 static DEFINE_MUTEX(dentry_race_sem);
1237 static DECLARE_MUTEX(dentry_race_sem);
1241 dentry_race_lock(void)
1243 mutex_lock(&dentry_race_sem);
1246 dentry_race_unlock(void)
1248 mutex_unlock(&dentry_race_sem);
1251 /* Leave some trace that this code is enabled; otherwise it's pretty hard to
1253 static __attribute__((used)) const char dentry_race_marker[] = "d_splice_alias race workaround enabled";
1256 check_dentry_race(struct dentry *dp)
1260 /* In Linux, before commit 4919c5e45a91b5db5a41695fe0357fbdff0d5767,
1261 * d_splice_alias can momentarily hash a dentry before it's fully
1262 * populated. This only happens for a moment, since it's unhashed again
1263 * right after (in d_move), but this can make the dentry be found by
1264 * __d_lookup, and then given to us.
1266 * So check if the dentry is unhashed; if it is, then the dentry is not
1267 * valid. We lock dentry_race_lock() to ensure that d_splice_alias is
1268 * no longer running. Locking d_lock is required to check the dentry's
1269 * flags, so lock that, too.
1272 spin_lock(&dp->d_lock);
1273 if (d_unhashed(dp)) {
1276 spin_unlock(&dp->d_lock);
1277 dentry_race_unlock();
1281 #endif /* D_SPLICE_ALIAS_RACE */
1283 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1284 * In kernels 2.2.10 and above, we are passed an additional flags var which
1285 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1286 * we are advised to follow the entry if it is a link or to make sure that
1287 * it is a directory. But since the kernel itself checks these possibilities
1288 * later on, we shouldn't have to do it until later. Perhaps in the future..
1290 * The code here assumes that on entry the global lock is not held
1293 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1294 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1295 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1296 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1298 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1301 cred_t *credp = NULL;
1302 struct vcache *vcp, *pvcp, *tvc = NULL;
1303 struct dentry *parent;
1305 struct afs_fakestat_state fakestate;
1307 afs_uint32 parent_dv;
1310 /* We don't support RCU path walking */
1311 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1312 if (flags & LOOKUP_RCU)
1314 if (nd->flags & LOOKUP_RCU)
1319 #ifdef D_SPLICE_ALIAS_RACE
1320 if (check_dentry_race(dp)) {
1327 afs_InitFakeStat(&fakestate);
1330 vcp = VTOAFS(dp->d_inode);
1332 if (vcp == afs_globalVp)
1335 if (vcp->mvstat == AFS_MVSTAT_MTPT) {
1336 if (vcp->mvid.target_root && (vcp->f.states & CMValid)) {
1337 int tryEvalOnly = 0;
1339 struct vrequest *treq = NULL;
1343 code = afs_CreateReq(&treq, credp);
1347 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1351 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1353 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1354 afs_DestroyReq(treq);
1355 if ((tryEvalOnly && vcp->mvstat == AFS_MVSTAT_MTPT) || code) {
1356 /* a mount point, not yet replaced by its directory */
1360 } else if (vcp->mvstat == AFS_MVSTAT_ROOT && *dp->d_name.name != '/') {
1361 osi_Assert(vcp->mvid.parent != NULL);
1364 parent = dget_parent(dp);
1365 pvcp = VTOAFS(parent->d_inode);
1366 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1368 /* If the parent's DataVersion has changed or the vnode
1369 * is longer valid, we need to do a full lookup. VerifyVCache
1370 * isn't enough since the vnode may have been renamed.
1373 if (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) {
1374 struct vattr *vattr = NULL;
1378 if (credp == NULL) {
1381 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1382 code = filter_enoent(code);
1385 /* We couldn't perform the lookup, so we're not okay. */
1388 } else if (tvc == vcp) {
1389 /* We got back the same vcache, so we're good. */
1392 } else if (tvc == VTOAFS(dp->d_inode)) {
1393 /* We got back the same vcache, so we're good. This is
1394 * different from the above case, because sometimes 'vcp' is
1395 * not the same as the vcache for dp->d_inode, if 'vcp' was a
1396 * mtpt and we evaluated it to a root dir. In rare cases,
1397 * afs_lookup might not evalute the mtpt when we do, or vice
1398 * versa, so the previous case will not succeed. But this is
1399 * still 'correct', so make sure not to mark the dentry as
1400 * invalid; it still points to the same thing! */
1404 /* We got back a different file, so we're definitely not
1411 /* Force unhash; the name doesn't point to this file
1414 if (code && code != ENOENT) {
1415 /* ...except if we couldn't perform the actual lookup,
1416 * we don't know if the name points to this file or not. */
1422 code = afs_CreateAttr(&vattr);
1428 if (afs_getattr(vcp, vattr, credp)) {
1430 afs_DestroyAttr(vattr);
1434 vattr2inode(AFSTOV(vcp), vattr);
1435 dp->d_time = parent_dv;
1437 afs_DestroyAttr(vattr);
1440 /* should we always update the attributes at this point? */
1441 /* unlikely--the vcache entry hasn't changed */
1447 /* 'dp' represents a cached negative lookup. */
1449 parent = dget_parent(dp);
1450 pvcp = VTOAFS(parent->d_inode);
1451 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1453 if (parent_dv > dp->d_time || !(pvcp->f.states & CStatd)
1454 || afs_IsDynroot(pvcp)) {
1468 #ifndef D_INVALIDATE_IS_VOID
1469 /* When (v3.18) d_invalidate was converted to void, it also started
1470 * being called automatically from revalidate, and automatically
1472 * - shrink_dcache_parent
1473 * - automatic detach of submounts
1475 * Therefore, after that point, OpenAFS revalidate logic no longer needs
1476 * to do any of those things itself for invalid dentry structs. We only need
1477 * to tell VFS it's invalid (by returning 0), and VFS will handle the rest.
1479 if (have_submounts(dp))
1487 afs_PutFakeStat(&fakestate);
1492 #ifndef D_INVALIDATE_IS_VOID
1495 * If we had a negative lookup for the name we want to forcibly
1496 * unhash the dentry.
1497 * Otherwise use d_invalidate which will not unhash it if still in use.
1500 shrink_dcache_parent(dp);
1511 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1513 struct vcache *vcp = VTOAFS(ip);
1514 int haveGlock = ISAFS_GLOCK();
1520 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1521 (void) afs_InactiveVCache(vcp, NULL);
1528 afs_linux_clear_nfsfs_renamed(dp);
1534 #if defined(DOP_D_DELETE_TAKES_CONST)
1535 afs_dentry_delete(const struct dentry *dp)
1537 afs_dentry_delete(struct dentry *dp)
1540 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1541 return 1; /* bad inode? */
1546 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1547 static struct vfsmount *
1548 afs_dentry_automount(afs_linux_path_t *path)
1550 struct dentry *target;
1553 * Avoid symlink resolution limits when resolving; we cannot contribute to
1554 * an infinite symlink loop.
1556 * On newer kernels the field has moved to the private nameidata structure
1557 * so we can't adjust it here. This may cause ELOOP when using a path with
1558 * 40 or more directories that are not already in the dentry cache.
1560 #if defined(STRUCT_TASK_STRUCT_HAS_TOTAL_LINK_COUNT)
1561 current->total_link_count--;
1564 target = canonical_dentry(path->dentry->d_inode);
1566 if (target == path->dentry) {
1573 path->dentry = target;
1576 spin_lock(&path->dentry->d_lock);
1577 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1578 spin_unlock(&path->dentry->d_lock);
1583 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1585 struct dentry_operations afs_dentry_operations = {
1586 .d_revalidate = afs_linux_dentry_revalidate,
1587 .d_delete = afs_dentry_delete,
1588 .d_iput = afs_dentry_iput,
1589 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1590 .d_automount = afs_dentry_automount,
1591 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1594 /**********************************************************************
1595 * AFS Linux inode operations
1596 **********************************************************************/
1600 * Merely need to set enough of vattr to get us through the create. Note
1601 * that the higher level code (open_namei) will take care of any tuncation
1602 * explicitly. Exclusive open is also taken care of in open_namei.
1604 * name is in kernel space at this point.
1607 #if defined(IOP_CREATE_TAKES_BOOL)
1608 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1610 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1611 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1612 struct nameidata *nd)
1613 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1614 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1615 struct nameidata *nd)
1617 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1620 struct vattr *vattr = NULL;
1621 cred_t *credp = crref();
1622 const char *name = dp->d_name.name;
1628 code = afs_CreateAttr(&vattr);
1632 vattr->va_mode = mode;
1633 vattr->va_type = mode & S_IFMT;
1635 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1639 struct inode *ip = AFSTOV(vcp);
1641 afs_getattr(vcp, vattr, credp);
1642 afs_fill_inode(ip, vattr);
1643 insert_inode_hash(ip);
1644 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1645 dp->d_op = &afs_dentry_operations;
1647 dp->d_time = parent_vcache_dv(dip, credp);
1648 d_instantiate(dp, ip);
1651 afs_DestroyAttr(vattr);
1657 return afs_convert_code(code);
1660 /* afs_linux_lookup */
1661 static struct dentry *
1662 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1663 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1665 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1666 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1667 struct nameidata *nd)
1669 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1672 cred_t *credp = crref();
1673 struct vcache *vcp = NULL;
1674 const char *comp = dp->d_name.name;
1675 struct inode *ip = NULL;
1676 struct dentry *newdp = NULL;
1681 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1682 code = filter_enoent(code);
1683 if (code == ENOENT) {
1684 /* It's ok for the file to not be found. That's noted by the caller by
1685 * seeing that the dp->d_inode field is NULL (set by d_splice_alias or
1688 osi_Assert(vcp == NULL);
1696 struct vattr *vattr = NULL;
1697 struct vcache *parent_vc = VTOAFS(dip);
1699 if (parent_vc == vcp) {
1700 /* This is possible if the parent dir is a mountpoint to a volume,
1701 * and the dir entry we looked up is a mountpoint to the same
1702 * volume. Linux cannot cope with this, so return an error instead
1703 * of risking a deadlock or panic. */
1710 code = afs_CreateAttr(&vattr);
1718 afs_getattr(vcp, vattr, credp);
1719 afs_fill_inode(ip, vattr);
1720 if (hlist_unhashed(&ip->i_hash))
1721 insert_inode_hash(ip);
1723 afs_DestroyAttr(vattr);
1725 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1726 dp->d_op = &afs_dentry_operations;
1728 dp->d_time = parent_vcache_dv(dip, credp);
1732 if (ip && S_ISDIR(ip->i_mode)) {
1733 d_prune_aliases(ip);
1735 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1736 /* Only needed if this is a volume root */
1737 if (vcp->mvstat == 2)
1738 ip->i_flags |= S_AUTOMOUNT;
1742 * Take an extra reference so the inode doesn't go away if
1743 * d_splice_alias drops our reference on error.
1746 #ifdef HAVE_LINUX_IHOLD
1753 newdp = d_splice_alias(ip, dp);
1754 dentry_race_unlock();
1759 if (IS_ERR(newdp)) {
1760 /* d_splice_alias can return an error (EIO) if there is an existing
1761 * connected directory alias for this dentry. Add our dentry manually
1762 * ourselves if this happens. */
1765 #if defined(D_SPLICE_ALIAS_LEAK_ON_ERROR)
1766 /* Depending on the kernel version, d_splice_alias may or may not drop
1767 * the inode reference on error. If it didn't, do it here. */
1776 return ERR_PTR(afs_convert_code(code));
1784 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1787 cred_t *credp = crref();
1788 const char *name = newdp->d_name.name;
1789 struct inode *oldip = olddp->d_inode;
1791 /* If afs_link returned the vnode, we could instantiate the
1792 * dentry. Since it's not, we drop this one and do a new lookup.
1797 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1801 return afs_convert_code(code);
1804 /* We have to have a Linux specific sillyrename function, because we
1805 * also have to keep the dcache up to date when we're doing a silly
1806 * rename - so we don't want the generic vnodeops doing this behind our
1811 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1814 struct vcache *tvc = VTOAFS(dentry->d_inode);
1815 struct dentry *__dp = NULL;
1816 char *__name = NULL;
1819 if (afs_linux_nfsfs_renamed(dentry))
1827 osi_FreeSmallSpace(__name);
1828 __name = afs_newname();
1831 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1834 osi_FreeSmallSpace(__name);
1837 } while (__dp->d_inode != NULL);
1840 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1841 VTOAFS(dir), (char *)__dp->d_name.name,
1844 tvc->mvid.silly_name = __name;
1847 crfree(tvc->uncred);
1849 tvc->uncred = credp;
1850 tvc->f.states |= CUnlinked;
1851 afs_linux_set_nfsfs_renamed(dentry);
1853 __dp->d_time = 0; /* force to revalidate */
1854 d_move(dentry, __dp);
1856 osi_FreeSmallSpace(__name);
1867 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1870 cred_t *credp = crref();
1871 const char *name = dp->d_name.name;
1872 struct vcache *tvc = VTOAFS(dp->d_inode);
1874 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1875 && !(tvc->f.states & CUnlinked)) {
1877 code = afs_linux_sillyrename(dip, dp, credp);
1880 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1887 return afs_convert_code(code);
1892 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1895 cred_t *credp = crref();
1896 struct vattr *vattr = NULL;
1897 const char *name = dp->d_name.name;
1899 /* If afs_symlink returned the vnode, we could instantiate the
1900 * dentry. Since it's not, we drop this one and do a new lookup.
1905 code = afs_CreateAttr(&vattr);
1910 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1912 afs_DestroyAttr(vattr);
1917 return afs_convert_code(code);
1921 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1922 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1924 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1928 cred_t *credp = crref();
1929 struct vcache *tvcp = NULL;
1930 struct vattr *vattr = NULL;
1931 const char *name = dp->d_name.name;
1934 code = afs_CreateAttr(&vattr);
1939 vattr->va_mask = ATTR_MODE;
1940 vattr->va_mode = mode;
1942 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1945 struct inode *ip = AFSTOV(tvcp);
1947 afs_getattr(tvcp, vattr, credp);
1948 afs_fill_inode(ip, vattr);
1950 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1951 dp->d_op = &afs_dentry_operations;
1953 dp->d_time = parent_vcache_dv(dip, credp);
1954 d_instantiate(dp, ip);
1956 afs_DestroyAttr(vattr);
1962 return afs_convert_code(code);
1966 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1969 cred_t *credp = crref();
1970 const char *name = dp->d_name.name;
1972 /* locking kernel conflicts with glock? */
1975 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1978 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1979 * that failed because a directory is not empty. So, we map
1980 * EEXIST to ENOTEMPTY on linux.
1982 if (code == EEXIST) {
1991 return afs_convert_code(code);
1996 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1997 struct inode *newip, struct dentry *newdp
1998 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1999 , unsigned int flags
2004 cred_t *credp = crref();
2005 const char *oldname = olddp->d_name.name;
2006 const char *newname = newdp->d_name.name;
2007 struct dentry *rehash = NULL;
2009 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
2011 return -EINVAL; /* no support for new flags yet */
2014 /* Prevent any new references during rename operation. */
2016 if (!d_unhashed(newdp)) {
2021 afs_maybe_shrink_dcache(olddp);
2024 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
2028 olddp->d_time = 0; /* force to revalidate */
2034 return afs_convert_code(code);
2038 /* afs_linux_ireadlink
2039 * Internal readlink which can return link contents to user or kernel space.
2040 * Note that the buffer is NOT supposed to be null-terminated.
2043 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
2046 cred_t *credp = crref();
2050 memset(&tuio, 0, sizeof(tuio));
2051 memset(&iov, 0, sizeof(iov));
2053 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
2054 code = afs_readlink(VTOAFS(ip), &tuio, credp);
2058 return maxlen - tuio.uio_resid;
2060 return afs_convert_code(code);
2063 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2064 /* afs_linux_readlink
2065 * Fill target (which is in user space) with contents of symlink.
2068 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
2071 struct inode *ip = dp->d_inode;
2074 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
2080 /* afs_linux_follow_link
2081 * a file system dependent link following routine.
2083 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2084 static const char *afs_linux_follow_link(struct dentry *dentry, void **link_data)
2086 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
2092 name = kmalloc(PATH_MAX, GFP_NOFS);
2094 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2095 return ERR_PTR(-EIO);
2102 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
2106 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2107 return ERR_PTR(code);
2114 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2115 return *link_data = name;
2117 nd_set_link(nd, name);
2122 #if defined(HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA)
2124 afs_linux_put_link(struct inode *inode, void *link_data)
2126 char *name = link_data;
2128 if (name && !IS_ERR(name))
2133 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
2135 char *name = nd_get_link(nd);
2137 if (name && !IS_ERR(name))
2140 #endif /* HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA */
2142 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2144 /* Populate a page by filling it from the cache file pointed at by cachefp
2145 * (which contains indicated chunk)
2146 * If task is NULL, the page copy occurs syncronously, and the routine
2147 * returns with page still locked. If task is non-NULL, then page copies
2148 * may occur in the background, and the page will be unlocked when it is
2149 * ready for use. Note that if task is non-NULL and we encounter an error
2150 * before we start the background copy, we MUST unlock 'page' before we return.
2153 afs_linux_read_cache(struct file *cachefp, struct page *page,
2154 int chunk, struct afs_lru_pages *alrupages,
2155 struct afs_pagecopy_task *task) {
2156 loff_t offset = page_offset(page);
2157 struct inode *cacheinode = cachefp->f_dentry->d_inode;
2158 struct page *newpage, *cachepage;
2159 struct address_space *cachemapping;
2163 cachemapping = cacheinode->i_mapping;
2167 /* If we're trying to read a page that's past the end of the disk
2168 * cache file, then just return a zeroed page */
2169 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
2170 zero_user_segment(page, 0, PAGE_SIZE);
2171 SetPageUptodate(page);
2177 /* From our offset, we now need to work out which page in the disk
2178 * file it corresponds to. This will be fun ... */
2179 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_SHIFT;
2181 while (cachepage == NULL) {
2182 cachepage = find_get_page(cachemapping, pageindex);
2185 newpage = page_cache_alloc(cachemapping);
2191 code = add_to_page_cache(newpage, cachemapping,
2192 pageindex, GFP_KERNEL);
2194 cachepage = newpage;
2196 afs_lru_cache_add(alrupages, cachepage);
2200 if (code != -EEXIST)
2204 lock_page(cachepage);
2208 if (!PageUptodate(cachepage)) {
2209 ClearPageError(cachepage);
2210 /* Note that ->readpage always handles unlocking the given page, even
2211 * when an error is returned. */
2212 code = cachemapping->a_ops->readpage(NULL, cachepage);
2213 if (!code && !task) {
2214 wait_on_page_locked(cachepage);
2217 unlock_page(cachepage);
2221 if (PageUptodate(cachepage)) {
2222 copy_highpage(page, cachepage);
2223 flush_dcache_page(page);
2224 SetPageUptodate(page);
2229 afs_pagecopy_queue_page(task, cachepage, page);
2241 put_page(cachepage);
2247 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2249 loff_t offset = page_offset(pp);
2250 struct inode *ip = FILE_INODE(fp);
2251 struct vcache *avc = VTOAFS(ip);
2253 struct file *cacheFp = NULL;
2256 struct afs_lru_pages lrupages;
2258 /* Not a UFS cache, don't do anything */
2259 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2262 /* No readpage (ex: tmpfs) , skip */
2263 if (cachefs_noreadpage)
2266 /* Can't do anything if the vcache isn't statd , or if the read
2267 * crosses a chunk boundary.
2269 if (!(avc->f.states & CStatd) ||
2270 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2274 ObtainWriteLock(&avc->lock, 911);
2276 /* XXX - See if hinting actually makes things faster !!! */
2278 /* See if we have a suitable entry already cached */
2282 /* We need to lock xdcache, then dcache, to handle situations where
2283 * the hint is on the free list. However, we can't safely do this
2284 * according to the locking hierarchy. So, use a non blocking lock.
2286 ObtainReadLock(&afs_xdcache);
2287 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2289 if (dcLocked && (tdc->index != NULLIDX)
2290 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2291 && tdc->f.chunk == AFS_CHUNK(offset)
2292 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2293 /* Bonus - the hint was correct */
2296 /* Only destroy the hint if its actually invalid, not if there's
2297 * just been a locking failure */
2299 ReleaseReadLock(&tdc->lock);
2306 ReleaseReadLock(&afs_xdcache);
2309 /* No hint, or hint is no longer valid - see if we can get something
2310 * directly from the dcache
2313 tdc = afs_FindDCache(avc, offset);
2316 ReleaseWriteLock(&avc->lock);
2321 ObtainReadLock(&tdc->lock);
2323 /* Is the dcache we've been given currently up to date */
2324 if (!afs_IsDCacheFresh(tdc, avc) ||
2325 (tdc->dflags & DFFetching))
2328 /* Update our hint for future abuse */
2331 /* Okay, so we've now got a cache file that is up to date */
2333 /* XXX - I suspect we should be locking the inodes before we use them! */
2335 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2336 if (cacheFp == NULL) {
2337 /* Problem getting the inode */
2341 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2342 cachefs_noreadpage = 1;
2347 afs_lru_cache_init(&lrupages);
2349 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupages, NULL);
2351 afs_lru_cache_finalize(&lrupages);
2353 filp_close(cacheFp, NULL);
2356 ReleaseReadLock(&tdc->lock);
2357 ReleaseWriteLock(&avc->lock);
2364 if (cacheFp != NULL) {
2365 filp_close(cacheFp, NULL);
2367 ReleaseWriteLock(&avc->lock);
2368 ReleaseReadLock(&tdc->lock);
2373 /* afs_linux_readpage
2375 * This function is split into two, because prepare_write/begin_write
2376 * require a readpage call which doesn't unlock the resulting page upon
2380 afs_linux_fillpage(struct file *fp, struct page *pp)
2385 struct iovec *iovecp;
2386 struct inode *ip = FILE_INODE(fp);
2387 afs_int32 cnt = page_count(pp);
2388 struct vcache *avc = VTOAFS(ip);
2389 afs_offs_t offset = page_offset(pp);
2393 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2403 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2404 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2406 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2411 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2412 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2413 99999); /* not a possible code value */
2415 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2417 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2418 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2420 AFS_DISCON_UNLOCK();
2423 /* XXX valid for no-cache also? Check last bits of files... :)
2424 * Cognate code goes in afs_NoCacheFetchProc. */
2425 if (auio->uio_resid) /* zero remainder of page */
2426 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2429 flush_dcache_page(pp);
2430 SetPageUptodate(pp);
2439 return afs_convert_code(code);
2443 afs_linux_prefetch(struct file *fp, struct page *pp)
2446 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2447 afs_offs_t offset = page_offset(pp);
2449 if (AFS_CHUNKOFFSET(offset) == 0) {
2451 struct vrequest *treq = NULL;
2456 code = afs_CreateReq(&treq, credp);
2457 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2458 tdc = afs_FindDCache(avc, offset);
2460 if (!(tdc->mflags & DFNextStarted))
2461 afs_PrefetchChunk(avc, tdc, credp, treq);
2464 ReleaseWriteLock(&avc->lock);
2466 afs_DestroyReq(treq);
2470 return afs_convert_code(code);
2475 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2476 struct list_head *page_list, unsigned num_pages)
2481 struct iovec* iovecp;
2482 struct nocache_read_request *ancr;
2484 struct afs_lru_pages lrupages;
2488 struct inode *ip = FILE_INODE(fp);
2489 struct vcache *avc = VTOAFS(ip);
2490 afs_int32 base_index = 0;
2491 afs_int32 page_count = 0;
2494 /* background thread must free: iovecp, auio, ancr */
2495 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2497 auio = osi_Alloc(sizeof(struct uio));
2498 auio->uio_iov = iovecp;
2499 auio->uio_iovcnt = num_pages;
2500 auio->uio_flag = UIO_READ;
2501 auio->uio_seg = AFS_UIOSYS;
2502 auio->uio_resid = num_pages * PAGE_SIZE;
2504 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2506 ancr->offset = auio->uio_offset;
2507 ancr->length = auio->uio_resid;
2509 afs_lru_cache_init(&lrupages);
2511 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2513 if(list_empty(page_list))
2516 pp = list_entry(page_list->prev, struct page, lru);
2517 /* If we allocate a page and don't remove it from page_list,
2518 * the page cache gets upset. */
2520 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2521 if(pp->index > isize) {
2528 offset = page_offset(pp);
2529 ancr->offset = auio->uio_offset = offset;
2530 base_index = pp->index;
2532 iovecp[page_ix].iov_len = PAGE_SIZE;
2533 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2534 if(base_index != pp->index) {
2538 iovecp[page_ix].iov_base = (void *) 0;
2540 ancr->length -= PAGE_SIZE;
2548 iovecp[page_ix].iov_base = (void *) 0;
2551 if(!PageLocked(pp)) {
2555 /* save the page for background map */
2556 iovecp[page_ix].iov_base = (void*) pp;
2558 /* and put it on the LRU cache */
2559 afs_lru_cache_add(&lrupages, pp);
2563 /* If there were useful pages in the page list, make sure all pages
2564 * are in the LRU cache, then schedule the read */
2566 afs_lru_cache_finalize(&lrupages);
2568 code = afs_ReadNoCache(avc, ancr, credp);
2571 /* If there is nothing for the background thread to handle,
2572 * it won't be freeing the things that we never gave it */
2573 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2574 osi_Free(auio, sizeof(struct uio));
2575 osi_Free(ancr, sizeof(struct nocache_read_request));
2577 /* we do not flush, release, or unmap pages--that will be
2578 * done for us by the background thread as each page comes in
2579 * from the fileserver */
2580 return afs_convert_code(code);
2585 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2587 cred_t *credp = NULL;
2589 struct iovec *iovecp;
2590 struct nocache_read_request *ancr;
2594 * Special case: if page is at or past end of file, just zero it and set
2597 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2598 zero_user_segment(pp, 0, PAGE_SIZE);
2599 SetPageUptodate(pp);
2606 /* receiver frees */
2607 auio = osi_Alloc(sizeof(struct uio));
2608 iovecp = osi_Alloc(sizeof(struct iovec));
2610 /* address can be NULL, because we overwrite it with 'pp', below */
2611 setup_uio(auio, iovecp, NULL, page_offset(pp),
2612 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2614 /* save the page for background map */
2615 get_page(pp); /* see above */
2616 auio->uio_iov->iov_base = (void*) pp;
2617 /* the background thread will free this */
2618 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2620 ancr->offset = page_offset(pp);
2621 ancr->length = PAGE_SIZE;
2624 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2627 return afs_convert_code(code);
2631 afs_linux_can_bypass(struct inode *ip) {
2633 switch(cache_bypass_strategy) {
2634 case NEVER_BYPASS_CACHE:
2636 case ALWAYS_BYPASS_CACHE:
2638 case LARGE_FILES_BYPASS_CACHE:
2639 if (i_size_read(ip) > cache_bypass_threshold)
2647 /* Check if a file is permitted to bypass the cache by policy, and modify
2648 * the cache bypass state recorded for that file */
2651 afs_linux_bypass_check(struct inode *ip) {
2654 int bypass = afs_linux_can_bypass(ip);
2657 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2665 afs_linux_readpage(struct file *fp, struct page *pp)
2669 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2670 code = afs_linux_bypass_readpage(fp, pp);
2672 code = afs_linux_fillpage(fp, pp);
2674 code = afs_linux_prefetch(fp, pp);
2681 /* Readpages reads a number of pages for a particular file. We use
2682 * this to optimise the reading, by limiting the number of times upon which
2683 * we have to lookup, lock and open vcaches and dcaches
2687 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2688 struct list_head *page_list, unsigned int num_pages)
2690 struct inode *inode = mapping->host;
2691 struct vcache *avc = VTOAFS(inode);
2693 struct file *cacheFp = NULL;
2695 unsigned int page_idx;
2697 struct afs_lru_pages lrupages;
2698 struct afs_pagecopy_task *task;
2700 if (afs_linux_bypass_check(inode))
2701 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2703 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2706 /* No readpage (ex: tmpfs) , skip */
2707 if (cachefs_noreadpage)
2711 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2716 ObtainWriteLock(&avc->lock, 912);
2719 task = afs_pagecopy_init_task();
2723 afs_lru_cache_init(&lrupages);
2725 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2726 struct page *page = list_entry(page_list->prev, struct page, lru);
2727 list_del(&page->lru);
2728 offset = page_offset(page);
2730 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2732 ReleaseReadLock(&tdc->lock);
2737 filp_close(cacheFp, NULL);
2744 if ((tdc = afs_FindDCache(avc, offset))) {
2745 ObtainReadLock(&tdc->lock);
2746 if (!afs_IsDCacheFresh(tdc, avc) ||
2747 (tdc->dflags & DFFetching)) {
2748 ReleaseReadLock(&tdc->lock);
2755 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2756 if (cacheFp == NULL) {
2757 /* Problem getting the inode */
2760 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2761 cachefs_noreadpage = 1;
2767 if (tdc && !add_to_page_cache(page, mapping, page->index,
2769 afs_lru_cache_add(&lrupages, page);
2771 /* Note that add_to_page_cache() locked 'page'.
2772 * afs_linux_read_cache() is guaranteed to handle unlocking it. */
2773 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupages, task);
2777 afs_lru_cache_finalize(&lrupages);
2781 filp_close(cacheFp, NULL);
2783 afs_pagecopy_put_task(task);
2787 ReleaseReadLock(&tdc->lock);
2791 ReleaseWriteLock(&avc->lock);
2796 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2799 afs_linux_prepare_writeback(struct vcache *avc) {
2801 struct pagewriter *pw;
2803 pid = MyPidxx2Pid(MyPidxx);
2804 /* Prevent recursion into the writeback code */
2805 spin_lock(&avc->pagewriter_lock);
2806 list_for_each_entry(pw, &avc->pagewriters, link) {
2807 if (pw->writer == pid) {
2808 spin_unlock(&avc->pagewriter_lock);
2809 return AOP_WRITEPAGE_ACTIVATE;
2812 spin_unlock(&avc->pagewriter_lock);
2814 /* Add ourselves to writer list */
2815 pw = osi_Alloc(sizeof(struct pagewriter));
2817 spin_lock(&avc->pagewriter_lock);
2818 list_add_tail(&pw->link, &avc->pagewriters);
2819 spin_unlock(&avc->pagewriter_lock);
2825 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2826 struct vrequest *treq = NULL;
2829 if (!afs_CreateReq(&treq, credp)) {
2830 code = afs_DoPartialWrite(avc, treq);
2831 afs_DestroyReq(treq);
2834 return afs_convert_code(code);
2838 afs_linux_complete_writeback(struct vcache *avc) {
2839 struct pagewriter *pw, *store;
2841 struct list_head tofree;
2843 INIT_LIST_HEAD(&tofree);
2844 pid = MyPidxx2Pid(MyPidxx);
2845 /* Remove ourselves from writer list */
2846 spin_lock(&avc->pagewriter_lock);
2847 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
2848 if (pw->writer == pid) {
2849 list_del(&pw->link);
2850 /* osi_Free may sleep so we need to defer it */
2851 list_add_tail(&pw->link, &tofree);
2854 spin_unlock(&avc->pagewriter_lock);
2855 list_for_each_entry_safe(pw, store, &tofree, link) {
2856 list_del(&pw->link);
2857 osi_Free(pw, sizeof(struct pagewriter));
2861 /* Writeback a given page syncronously. Called with no AFS locks held */
2863 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2864 unsigned long offset, unsigned int count,
2867 struct vcache *vcp = VTOAFS(ip);
2875 memset(&tuio, 0, sizeof(tuio));
2876 memset(&iovec, 0, sizeof(iovec));
2878 buffer = kmap(pp) + offset;
2879 base = page_offset(pp) + offset;
2882 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2883 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2884 ICL_TYPE_INT32, 99999);
2886 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2888 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2890 i_size_write(ip, vcp->f.m.Length);
2891 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2893 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2895 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2896 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2897 ICL_TYPE_INT32, code);
2906 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2907 unsigned long offset, unsigned int count)
2911 struct vcache *vcp = VTOAFS(ip);
2914 /* Catch recursive writeback. This occurs if the kernel decides
2915 * writeback is required whilst we are writing to the cache, or
2916 * flushing to the server. When we're running syncronously (as
2917 * opposed to from writepage) we can't actually do anything about
2918 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2921 ObtainWriteLock(&vcp->lock, 532);
2922 afs_linux_prepare_writeback(vcp);
2923 ReleaseWriteLock(&vcp->lock);
2927 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2930 ObtainWriteLock(&vcp->lock, 533);
2932 code1 = afs_linux_dopartialwrite(vcp, credp);
2933 afs_linux_complete_writeback(vcp);
2934 ReleaseWriteLock(&vcp->lock);
2945 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2946 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2948 afs_linux_writepage(struct page *pp)
2951 struct address_space *mapping = pp->mapping;
2952 struct inode *inode;
2955 unsigned int to = PAGE_SIZE;
2962 inode = mapping->host;
2963 vcp = VTOAFS(inode);
2964 isize = i_size_read(inode);
2966 /* Don't defeat an earlier truncate */
2967 if (page_offset(pp) > isize) {
2968 set_page_writeback(pp);
2974 ObtainWriteLock(&vcp->lock, 537);
2975 code = afs_linux_prepare_writeback(vcp);
2976 if (code == AOP_WRITEPAGE_ACTIVATE) {
2977 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2978 * to return with the page still locked */
2979 ReleaseWriteLock(&vcp->lock);
2984 /* Grab the creds structure currently held in the vnode, and
2985 * get a reference to it, in case it goes away ... */
2991 ReleaseWriteLock(&vcp->lock);
2994 set_page_writeback(pp);
2996 SetPageUptodate(pp);
2998 /* We can unlock the page here, because it's protected by the
2999 * page_writeback flag. This should make us less vulnerable to
3000 * deadlocking in afs_write and afs_DoPartialWrite
3004 /* If this is the final page, then just write the number of bytes that
3005 * are actually in it */
3006 if ((isize - page_offset(pp)) < to )
3007 to = isize - page_offset(pp);
3009 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
3012 ObtainWriteLock(&vcp->lock, 538);
3014 /* As much as we might like to ignore a file server error here,
3015 * and just try again when we close(), unfortunately StoreAllSegments
3016 * will invalidate our chunks if the server returns a permanent error,
3017 * so we need to at least try and get that error back to the user
3020 code1 = afs_linux_dopartialwrite(vcp, credp);
3022 afs_linux_complete_writeback(vcp);
3023 ReleaseWriteLock(&vcp->lock);
3028 end_page_writeback(pp);
3040 /* afs_linux_permission
3041 * Check access rights - returns error if can't check or permission denied.
3044 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3045 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
3046 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
3047 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
3049 afs_linux_permission(struct inode *ip, int mode)
3056 /* Check for RCU path walking */
3057 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3058 if (flags & IPERM_FLAG_RCU)
3060 #elif defined(MAY_NOT_BLOCK)
3061 if (mode & MAY_NOT_BLOCK)
3067 if (mode & MAY_EXEC)
3069 if (mode & MAY_READ)
3071 if (mode & MAY_WRITE)
3073 code = afs_access(VTOAFS(ip), tmp, credp);
3077 return afs_convert_code(code);
3081 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
3085 struct inode *inode = FILE_INODE(file);
3086 loff_t pagebase = page_offset(page);
3088 if (i_size_read(inode) < (pagebase + offset))
3089 i_size_write(inode, pagebase + offset);
3091 if (PageChecked(page)) {
3092 SetPageUptodate(page);
3093 ClearPageChecked(page);
3096 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
3102 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
3106 /* http://kerneltrap.org/node/4941 details the expected behaviour of
3107 * prepare_write. Essentially, if the page exists within the file,
3108 * and is not being fully written, then we should populate it.
3111 if (!PageUptodate(page)) {
3112 loff_t pagebase = page_offset(page);
3113 loff_t isize = i_size_read(page->mapping->host);
3115 /* Is the location we are writing to beyond the end of the file? */
3116 if (pagebase >= isize ||
3117 ((from == 0) && (pagebase + to) >= isize)) {
3118 zero_user_segments(page, 0, from, to, PAGE_SIZE);
3119 SetPageChecked(page);
3120 /* Are we we writing a full page */
3121 } else if (from == 0 && to == PAGE_SIZE) {
3122 SetPageChecked(page);
3123 /* Is the page readable, if it's wronly, we don't care, because we're
3124 * not actually going to read from it ... */
3125 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
3126 /* We don't care if fillpage fails, because if it does the page
3127 * won't be marked as up to date
3129 afs_linux_fillpage(file, page);
3135 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3137 afs_linux_write_end(struct file *file, struct address_space *mapping,
3138 loff_t pos, unsigned len, unsigned copied,
3139 struct page *page, void *fsdata)
3142 unsigned int from = pos & (PAGE_SIZE - 1);
3144 code = afs_linux_commit_write(file, page, from, from + copied);
3152 afs_linux_write_begin(struct file *file, struct address_space *mapping,
3153 loff_t pos, unsigned len, unsigned flags,
3154 struct page **pagep, void **fsdata)
3157 pgoff_t index = pos >> PAGE_SHIFT;
3158 unsigned int from = pos & (PAGE_SIZE - 1);
3161 page = grab_cache_page_write_begin(mapping, index, flags);
3168 code = afs_linux_prepare_write(file, page, from, from + len);
3178 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3180 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
3182 struct dentry **dpp;
3183 struct dentry *target;
3185 if (current->total_link_count > 0) {
3186 /* avoid symlink resolution limits when resolving; we cannot contribute to
3187 * an infinite symlink loop */
3188 /* only do this for follow_link when total_link_count is positive to be
3189 * on the safe side; there is at least one code path in the Linux
3190 * kernel where it seems like it may be possible to get here without
3191 * total_link_count getting incremented. it is not clear on how that
3192 * path is actually reached, but guard against it just to be safe */
3193 current->total_link_count--;
3196 target = canonical_dentry(dentry->d_inode);
3198 # ifdef STRUCT_NAMEIDATA_HAS_PATH
3199 dpp = &nd->path.dentry;
3209 *dpp = dget(dentry);
3212 nd->last_type = LAST_BIND;
3216 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
3219 static struct inode_operations afs_file_iops = {
3220 .permission = afs_linux_permission,
3221 .getattr = afs_linux_getattr,
3222 .setattr = afs_notify_change,
3225 static struct address_space_operations afs_file_aops = {
3226 .readpage = afs_linux_readpage,
3227 .readpages = afs_linux_readpages,
3228 .writepage = afs_linux_writepage,
3229 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3230 .write_begin = afs_linux_write_begin,
3231 .write_end = afs_linux_write_end,
3233 .commit_write = afs_linux_commit_write,
3234 .prepare_write = afs_linux_prepare_write,
3239 /* Separate ops vector for directories. Linux 2.2 tests type of inode
3240 * by what sort of operation is allowed.....
3243 static struct inode_operations afs_dir_iops = {
3244 .setattr = afs_notify_change,
3245 .create = afs_linux_create,
3246 .lookup = afs_linux_lookup,
3247 .link = afs_linux_link,
3248 .unlink = afs_linux_unlink,
3249 .symlink = afs_linux_symlink,
3250 .mkdir = afs_linux_mkdir,
3251 .rmdir = afs_linux_rmdir,
3252 .rename = afs_linux_rename,
3253 .getattr = afs_linux_getattr,
3254 .permission = afs_linux_permission,
3255 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3256 .follow_link = afs_linux_dir_follow_link,
3260 /* We really need a separate symlink set of ops, since do_follow_link()
3261 * determines if it _is_ a link by checking if the follow_link op is set.
3263 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3265 afs_symlink_filler(struct file *file, struct page *page)
3267 struct inode *ip = (struct inode *)page->mapping->host;
3268 char *p = (char *)kmap(page);
3272 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3277 p[code] = '\0'; /* null terminate? */
3279 SetPageUptodate(page);
3291 static struct address_space_operations afs_symlink_aops = {
3292 .readpage = afs_symlink_filler
3294 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3296 static struct inode_operations afs_symlink_iops = {
3297 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3298 .readlink = page_readlink,
3299 # if defined(HAVE_LINUX_PAGE_GET_LINK)
3300 .get_link = page_get_link,
3301 # elif defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3302 .follow_link = page_follow_link,
3304 .follow_link = page_follow_link_light,
3305 .put_link = page_put_link,
3307 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3308 .readlink = afs_linux_readlink,
3309 .follow_link = afs_linux_follow_link,
3310 .put_link = afs_linux_put_link,
3311 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3312 .setattr = afs_notify_change,
3316 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3319 vattr2inode(ip, vattr);
3321 #ifdef STRUCT_ADDRESS_SPACE_HAS_BACKING_DEV_INFO
3322 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3324 /* Reset ops if symlink or directory. */
3325 if (S_ISREG(ip->i_mode)) {
3326 ip->i_op = &afs_file_iops;
3327 ip->i_fop = &afs_file_fops;
3328 ip->i_data.a_ops = &afs_file_aops;
3330 } else if (S_ISDIR(ip->i_mode)) {
3331 ip->i_op = &afs_dir_iops;
3332 ip->i_fop = &afs_dir_fops;
3334 } else if (S_ISLNK(ip->i_mode)) {
3335 ip->i_op = &afs_symlink_iops;
3336 #if defined(HAVE_LINUX_INODE_NOHIGHMEM)
3337 inode_nohighmem(ip);
3339 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3340 ip->i_data.a_ops = &afs_symlink_aops;
3341 ip->i_mapping = &ip->i_data;