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 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
594 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
600 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
602 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
606 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
607 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_LONG,
608 vmap->vm_end - vmap->vm_start, ICL_TYPE_LONG, 0);
610 /* get a validated vcache entry */
611 code = afs_linux_VerifyVCache(vcp, NULL);
614 /* Linux's Flushpage implementation doesn't use credp, so optimise
615 * our code to not need to crref() it */
616 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
618 code = generic_file_mmap(fp, vmap);
621 vcp->f.states |= CMAPPED;
629 afs_linux_open(struct inode *ip, struct file *fp)
631 struct vcache *vcp = VTOAFS(ip);
632 cred_t *credp = crref();
636 code = afs_open(&vcp, fp->f_flags, credp);
640 return afs_convert_code(code);
644 afs_linux_release(struct inode *ip, struct file *fp)
646 struct vcache *vcp = VTOAFS(ip);
647 cred_t *credp = crref();
651 code = afs_close(vcp, fp->f_flags, credp);
652 ObtainWriteLock(&vcp->lock, 807);
657 ReleaseWriteLock(&vcp->lock);
661 return afs_convert_code(code);
665 #if defined(FOP_FSYNC_TAKES_DENTRY)
666 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
667 #elif defined(FOP_FSYNC_TAKES_RANGE)
668 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
670 afs_linux_fsync(struct file *fp, int datasync)
674 struct inode *ip = FILE_INODE(fp);
675 cred_t *credp = crref();
677 #if defined(FOP_FSYNC_TAKES_RANGE)
678 afs_linux_lock_inode(ip);
681 code = afs_fsync(VTOAFS(ip), credp);
683 #if defined(FOP_FSYNC_TAKES_RANGE)
684 afs_linux_unlock_inode(ip);
687 return afs_convert_code(code);
693 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
696 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
697 cred_t *credp = crref();
698 struct AFS_FLOCK flock;
700 /* Convert to a lock format afs_lockctl understands. */
701 memset(&flock, 0, sizeof(flock));
702 flock.l_type = flp->fl_type;
703 flock.l_pid = flp->fl_pid;
705 flock.l_start = flp->fl_start;
706 if (flp->fl_end == OFFSET_MAX)
707 flock.l_len = 0; /* Lock to end of file */
709 flock.l_len = flp->fl_end - flp->fl_start + 1;
711 /* Safe because there are no large files, yet */
712 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
713 if (cmd == F_GETLK64)
715 else if (cmd == F_SETLK64)
717 else if (cmd == F_SETLKW64)
719 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
722 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
725 if ((code == 0 || flp->fl_type == F_UNLCK) &&
726 (cmd == F_SETLK || cmd == F_SETLKW)) {
727 code = afs_posix_lock_file(fp, flp);
728 if (code && flp->fl_type != F_UNLCK) {
729 struct AFS_FLOCK flock2;
731 flock2.l_type = F_UNLCK;
733 afs_lockctl(vcp, &flock2, F_SETLK, credp);
737 /* If lockctl says there are no conflicting locks, then also check with the
738 * kernel, as lockctl knows nothing about byte range locks
740 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
741 afs_posix_test_lock(fp, flp);
742 /* If we found a lock in the kernel's structure, return it */
743 if (flp->fl_type != F_UNLCK) {
749 /* Convert flock back to Linux's file_lock */
750 flp->fl_type = flock.l_type;
751 flp->fl_pid = flock.l_pid;
752 flp->fl_start = flock.l_start;
753 if (flock.l_len == 0)
754 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
756 flp->fl_end = flock.l_start + flock.l_len - 1;
762 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
764 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
766 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
767 cred_t *credp = crref();
768 struct AFS_FLOCK flock;
769 /* Convert to a lock format afs_lockctl understands. */
770 memset(&flock, 0, sizeof(flock));
771 flock.l_type = flp->fl_type;
772 flock.l_pid = flp->fl_pid;
777 /* Safe because there are no large files, yet */
778 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
779 if (cmd == F_GETLK64)
781 else if (cmd == F_SETLK64)
783 else if (cmd == F_SETLKW64)
785 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
788 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
791 if ((code == 0 || flp->fl_type == F_UNLCK) &&
792 (cmd == F_SETLK || cmd == F_SETLKW)) {
793 flp->fl_flags &=~ FL_SLEEP;
794 code = flock_lock_file_wait(fp, flp);
795 if (code && flp->fl_type != F_UNLCK) {
796 struct AFS_FLOCK flock2;
798 flock2.l_type = F_UNLCK;
800 afs_lockctl(vcp, &flock2, F_SETLK, credp);
804 /* Convert flock back to Linux's file_lock */
805 flp->fl_type = flock.l_type;
806 flp->fl_pid = flock.l_pid;
814 * essentially the same as afs_fsync() but we need to get the return
815 * code for the sys_close() here, not afs_linux_release(), so call
816 * afs_StoreAllSegments() with AFS_LASTSTORE
819 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
820 afs_linux_flush(struct file *fp, fl_owner_t id)
822 afs_linux_flush(struct file *fp)
825 struct vrequest *treq = NULL;
833 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
841 vcp = VTOAFS(FILE_INODE(fp));
843 code = afs_CreateReq(&treq, credp);
846 /* If caching is bypassed for this file, or globally, just return 0 */
847 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
850 ObtainReadLock(&vcp->lock);
851 if (vcp->cachingStates & FCSBypass)
853 ReleaseReadLock(&vcp->lock);
856 /* future proof: don't rely on 0 return from afs_InitReq */
861 ObtainSharedLock(&vcp->lock, 535);
862 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
863 UpgradeSToWLock(&vcp->lock, 536);
864 if (!AFS_IS_DISCONNECTED) {
865 code = afs_StoreAllSegments(vcp,
867 AFS_SYNC | AFS_LASTSTORE);
869 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
871 ConvertWToSLock(&vcp->lock);
873 code = afs_CheckCode(code, treq, 54);
874 ReleaseSharedLock(&vcp->lock);
877 afs_DestroyReq(treq);
882 return afs_convert_code(code);
885 struct file_operations afs_dir_fops = {
886 .read = generic_read_dir,
887 #if defined(USE_FOP_ITERATE)
888 .iterate = afs_linux_readdir,
890 .readdir = afs_linux_readdir,
892 .unlocked_ioctl = afs_unlocked_xioctl,
893 .compat_ioctl = afs_unlocked_xioctl,
894 .open = afs_linux_open,
895 .release = afs_linux_release,
896 .llseek = default_llseek,
897 #ifdef HAVE_LINUX_NOOP_FSYNC
900 .fsync = simple_sync_file,
904 struct file_operations afs_file_fops = {
905 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
906 .read_iter = afs_linux_read_iter,
907 .write_iter = afs_linux_write_iter,
908 # if !defined(HAVE_LINUX___VFS_WRITE) && !defined(HAVE_LINUX_KERNEL_WRITE)
909 .read = new_sync_read,
910 .write = new_sync_write,
912 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
913 .aio_read = afs_linux_aio_read,
914 .aio_write = afs_linux_aio_write,
915 .read = do_sync_read,
916 .write = do_sync_write,
918 .read = afs_linux_read,
919 .write = afs_linux_write,
921 .unlocked_ioctl = afs_unlocked_xioctl,
922 .compat_ioctl = afs_unlocked_xioctl,
923 .mmap = afs_linux_mmap,
924 .open = afs_linux_open,
925 .flush = afs_linux_flush,
926 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
927 .sendfile = generic_file_sendfile,
929 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE) && !defined(HAVE_LINUX_DEFAULT_FILE_SPLICE_READ)
930 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
931 .splice_write = iter_file_splice_write,
933 .splice_write = generic_file_splice_write,
935 .splice_read = generic_file_splice_read,
937 .release = afs_linux_release,
938 .fsync = afs_linux_fsync,
939 .lock = afs_linux_lock,
940 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
941 .flock = afs_linux_flock,
943 .llseek = default_llseek,
946 static struct dentry *
947 canonical_dentry(struct inode *ip)
949 struct vcache *vcp = VTOAFS(ip);
950 struct dentry *first = NULL, *ret = NULL, *cur;
951 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
952 struct hlist_node *p;
956 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
957 * otherwise, use the first dentry in ip->i_dentry.
958 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
960 /* note that vcp->target_link specifies which dentry to use, but we have
961 * no reference held on that dentry. so, we cannot use or dereference
962 * vcp->target_link itself, since it may have been freed. instead, we only
963 * use it to compare to pointers in the ip->i_dentry list. */
967 afs_d_alias_lock(ip);
969 #if defined(D_ALIAS_IS_HLIST)
970 # if defined(HLIST_ITERATOR_NO_NODE)
971 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
973 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
976 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
979 if (!vcp->target_link || cur == vcp->target_link) {
992 vcp->target_link = ret;
997 afs_d_alias_unlock(ip);
1002 /**********************************************************************
1003 * AFS Linux dentry operations
1004 **********************************************************************/
1006 /* afs_linux_revalidate
1007 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
1010 afs_linux_revalidate(struct dentry *dp)
1012 struct vattr *vattr = NULL;
1013 struct vcache *vcp = VTOAFS(dp->d_inode);
1017 if (afs_shuttingdown != AFS_RUNNING)
1022 code = afs_CreateAttr(&vattr);
1027 /* This avoids the crref when we don't have to do it. Watch for
1028 * changes in afs_getattr that don't get replicated here!
1030 if (vcp->f.states & CStatd &&
1031 (!afs_fakestat_enable || vcp->mvstat != AFS_MVSTAT_MTPT) &&
1033 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
1034 code = afs_CopyOutAttrs(vcp, vattr);
1037 code = afs_getattr(vcp, vattr, credp);
1042 afs_fill_inode(AFSTOV(vcp), vattr);
1044 afs_DestroyAttr(vattr);
1049 return afs_convert_code(code);
1053 * Set iattr data into vattr. Assume vattr cleared before call.
1056 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1058 vattrp->va_mask = iattrp->ia_valid;
1059 if (iattrp->ia_valid & ATTR_MODE)
1060 vattrp->va_mode = iattrp->ia_mode;
1061 if (iattrp->ia_valid & ATTR_UID)
1062 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1063 if (iattrp->ia_valid & ATTR_GID)
1064 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1065 if (iattrp->ia_valid & ATTR_SIZE)
1066 vattrp->va_size = iattrp->ia_size;
1067 if (iattrp->ia_valid & ATTR_ATIME) {
1068 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1069 vattrp->va_atime.tv_nsec = 0;
1071 if (iattrp->ia_valid & ATTR_MTIME) {
1072 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1073 vattrp->va_mtime.tv_nsec = 0;
1075 if (iattrp->ia_valid & ATTR_CTIME) {
1076 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1077 vattrp->va_ctime.tv_nsec = 0;
1082 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1085 vattr2inode(struct inode *ip, struct vattr *vp)
1087 ip->i_ino = vp->va_nodeid;
1088 #ifdef HAVE_LINUX_SET_NLINK
1089 set_nlink(ip, vp->va_nlink);
1091 ip->i_nlink = vp->va_nlink;
1093 ip->i_blocks = vp->va_blocks;
1094 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1095 ip->i_blkbits = AFS_BLKBITS;
1097 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1098 ip->i_blksize = vp->va_blocksize;
1100 ip->i_rdev = vp->va_rdev;
1101 ip->i_mode = vp->va_mode;
1102 ip->i_uid = afs_make_kuid(vp->va_uid);
1103 ip->i_gid = afs_make_kgid(vp->va_gid);
1104 i_size_write(ip, vp->va_size);
1105 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1106 ip->i_atime.tv_nsec = 0;
1107 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1108 /* Set the mtime nanoseconds to the sysname generation number.
1109 * This convinces NFS clients that all directories have changed
1110 * any time the sysname list changes.
1112 ip->i_mtime.tv_nsec = afs_sysnamegen;
1113 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1114 ip->i_ctime.tv_nsec = 0;
1117 /* afs_notify_change
1118 * Linux version of setattr call. What to change is in the iattr struct.
1119 * We need to set bits in both the Linux inode as well as the vcache.
1122 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1124 struct vattr *vattr = NULL;
1125 cred_t *credp = crref();
1126 struct inode *ip = dp->d_inode;
1130 code = afs_CreateAttr(&vattr);
1135 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1137 code = afs_setattr(VTOAFS(ip), vattr, credp);
1139 afs_getattr(VTOAFS(ip), vattr, credp);
1140 vattr2inode(ip, vattr);
1142 afs_DestroyAttr(vattr);
1147 return afs_convert_code(code);
1150 #if defined(IOP_GETATTR_TAKES_PATH_STRUCT)
1152 afs_linux_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int sync_mode)
1154 int err = afs_linux_revalidate(path->dentry);
1156 generic_fillattr(path->dentry->d_inode, stat);
1162 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1164 int err = afs_linux_revalidate(dentry);
1166 generic_fillattr(dentry->d_inode, stat);
1173 parent_vcache_dv(struct inode *inode, cred_t *credp)
1176 struct vcache *pvcp;
1179 * If parent is a mount point and we are using fakestat, we may need
1180 * to look at the fake vcache entry instead of what the vfs is giving
1181 * us. The fake entry is the one with the useful DataVersion.
1183 pvcp = VTOAFS(inode);
1184 if (pvcp->mvstat == AFS_MVSTAT_MTPT && afs_fakestat_enable) {
1185 struct vrequest treq;
1186 struct afs_fakestat_state fakestate;
1192 afs_InitReq(&treq, credp);
1193 afs_InitFakeStat(&fakestate);
1194 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1197 afs_PutFakeStat(&fakestate);
1199 return hgetlo(pvcp->f.m.DataVersion);
1203 filter_enoent(int code)
1205 #ifdef HAVE_LINUX_FATAL_SIGNAL_PENDING
1206 if (code == ENOENT && fatal_signal_pending(current)) {
1213 #ifndef D_SPLICE_ALIAS_RACE
1215 static inline void dentry_race_lock(void) {}
1216 static inline void dentry_race_unlock(void) {}
1220 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
1221 static DEFINE_MUTEX(dentry_race_sem);
1223 static DECLARE_MUTEX(dentry_race_sem);
1227 dentry_race_lock(void)
1229 mutex_lock(&dentry_race_sem);
1232 dentry_race_unlock(void)
1234 mutex_unlock(&dentry_race_sem);
1237 /* Leave some trace that this code is enabled; otherwise it's pretty hard to
1239 static __attribute__((used)) const char dentry_race_marker[] = "d_splice_alias race workaround enabled";
1242 check_dentry_race(struct dentry *dp)
1246 /* In Linux, before commit 4919c5e45a91b5db5a41695fe0357fbdff0d5767,
1247 * d_splice_alias can momentarily hash a dentry before it's fully
1248 * populated. This only happens for a moment, since it's unhashed again
1249 * right after (in d_move), but this can make the dentry be found by
1250 * __d_lookup, and then given to us.
1252 * So check if the dentry is unhashed; if it is, then the dentry is not
1253 * valid. We lock dentry_race_lock() to ensure that d_splice_alias is
1254 * no longer running. Locking d_lock is required to check the dentry's
1255 * flags, so lock that, too.
1258 spin_lock(&dp->d_lock);
1259 if (d_unhashed(dp)) {
1262 spin_unlock(&dp->d_lock);
1263 dentry_race_unlock();
1267 #endif /* D_SPLICE_ALIAS_RACE */
1269 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1270 * In kernels 2.2.10 and above, we are passed an additional flags var which
1271 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1272 * we are advised to follow the entry if it is a link or to make sure that
1273 * it is a directory. But since the kernel itself checks these possibilities
1274 * later on, we shouldn't have to do it until later. Perhaps in the future..
1276 * The code here assumes that on entry the global lock is not held
1279 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1280 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1281 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1282 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1284 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1287 cred_t *credp = NULL;
1288 struct vcache *vcp, *pvcp, *tvc = NULL;
1289 struct dentry *parent;
1291 struct afs_fakestat_state fakestate;
1293 afs_uint32 parent_dv;
1296 /* We don't support RCU path walking */
1297 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1298 if (flags & LOOKUP_RCU)
1300 if (nd->flags & LOOKUP_RCU)
1305 #ifdef D_SPLICE_ALIAS_RACE
1306 if (check_dentry_race(dp)) {
1313 afs_InitFakeStat(&fakestate);
1316 vcp = VTOAFS(dp->d_inode);
1318 if (vcp == afs_globalVp)
1321 if (vcp->mvstat == AFS_MVSTAT_MTPT) {
1322 if (vcp->mvid.target_root && (vcp->f.states & CMValid)) {
1323 int tryEvalOnly = 0;
1325 struct vrequest *treq = NULL;
1329 code = afs_CreateReq(&treq, credp);
1333 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1337 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1339 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1340 afs_DestroyReq(treq);
1341 if ((tryEvalOnly && vcp->mvstat == AFS_MVSTAT_MTPT) || code) {
1342 /* a mount point, not yet replaced by its directory */
1346 } else if (vcp->mvstat == AFS_MVSTAT_ROOT && *dp->d_name.name != '/') {
1347 osi_Assert(vcp->mvid.parent != NULL);
1350 parent = dget_parent(dp);
1351 pvcp = VTOAFS(parent->d_inode);
1352 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1354 /* If the parent's DataVersion has changed or the vnode
1355 * is longer valid, we need to do a full lookup. VerifyVCache
1356 * isn't enough since the vnode may have been renamed.
1359 if (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) {
1360 struct vattr *vattr = NULL;
1364 if (credp == NULL) {
1367 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1368 code = filter_enoent(code);
1371 /* We couldn't perform the lookup, so we're not okay. */
1374 } else if (tvc == vcp) {
1375 /* We got back the same vcache, so we're good. */
1378 } else if (tvc == VTOAFS(dp->d_inode)) {
1379 /* We got back the same vcache, so we're good. This is
1380 * different from the above case, because sometimes 'vcp' is
1381 * not the same as the vcache for dp->d_inode, if 'vcp' was a
1382 * mtpt and we evaluated it to a root dir. In rare cases,
1383 * afs_lookup might not evalute the mtpt when we do, or vice
1384 * versa, so the previous case will not succeed. But this is
1385 * still 'correct', so make sure not to mark the dentry as
1386 * invalid; it still points to the same thing! */
1390 /* We got back a different file, so we're definitely not
1397 /* Force unhash; the name doesn't point to this file
1400 if (code && code != ENOENT) {
1401 /* ...except if we couldn't perform the actual lookup,
1402 * we don't know if the name points to this file or not. */
1408 code = afs_CreateAttr(&vattr);
1414 if (afs_getattr(vcp, vattr, credp)) {
1416 afs_DestroyAttr(vattr);
1420 vattr2inode(AFSTOV(vcp), vattr);
1421 dp->d_time = parent_dv;
1423 afs_DestroyAttr(vattr);
1426 /* should we always update the attributes at this point? */
1427 /* unlikely--the vcache entry hasn't changed */
1433 /* 'dp' represents a cached negative lookup. */
1435 parent = dget_parent(dp);
1436 pvcp = VTOAFS(parent->d_inode);
1437 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1439 if (parent_dv > dp->d_time || !(pvcp->f.states & CStatd)
1440 || afs_IsDynroot(pvcp)) {
1454 #ifndef D_INVALIDATE_IS_VOID
1455 /* When (v3.18) d_invalidate was converted to void, it also started
1456 * being called automatically from revalidate, and automatically
1458 * - shrink_dcache_parent
1459 * - automatic detach of submounts
1461 * Therefore, after that point, OpenAFS revalidate logic no longer needs
1462 * to do any of those things itself for invalid dentry structs. We only need
1463 * to tell VFS it's invalid (by returning 0), and VFS will handle the rest.
1465 if (have_submounts(dp))
1473 afs_PutFakeStat(&fakestate);
1478 #ifndef D_INVALIDATE_IS_VOID
1481 * If we had a negative lookup for the name we want to forcibly
1482 * unhash the dentry.
1483 * Otherwise use d_invalidate which will not unhash it if still in use.
1486 shrink_dcache_parent(dp);
1497 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1499 struct vcache *vcp = VTOAFS(ip);
1500 int haveGlock = ISAFS_GLOCK();
1506 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1507 (void) afs_InactiveVCache(vcp, NULL);
1514 afs_linux_clear_nfsfs_renamed(dp);
1520 #if defined(DOP_D_DELETE_TAKES_CONST)
1521 afs_dentry_delete(const struct dentry *dp)
1523 afs_dentry_delete(struct dentry *dp)
1526 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1527 return 1; /* bad inode? */
1532 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1533 static struct vfsmount *
1534 afs_dentry_automount(afs_linux_path_t *path)
1536 struct dentry *target;
1539 * Avoid symlink resolution limits when resolving; we cannot contribute to
1540 * an infinite symlink loop.
1542 * On newer kernels the field has moved to the private nameidata structure
1543 * so we can't adjust it here. This may cause ELOOP when using a path with
1544 * 40 or more directories that are not already in the dentry cache.
1546 #if defined(STRUCT_TASK_STRUCT_HAS_TOTAL_LINK_COUNT)
1547 current->total_link_count--;
1550 target = canonical_dentry(path->dentry->d_inode);
1552 if (target == path->dentry) {
1559 path->dentry = target;
1562 spin_lock(&path->dentry->d_lock);
1563 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1564 spin_unlock(&path->dentry->d_lock);
1569 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1571 struct dentry_operations afs_dentry_operations = {
1572 .d_revalidate = afs_linux_dentry_revalidate,
1573 .d_delete = afs_dentry_delete,
1574 .d_iput = afs_dentry_iput,
1575 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1576 .d_automount = afs_dentry_automount,
1577 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1580 /**********************************************************************
1581 * AFS Linux inode operations
1582 **********************************************************************/
1586 * Merely need to set enough of vattr to get us through the create. Note
1587 * that the higher level code (open_namei) will take care of any tuncation
1588 * explicitly. Exclusive open is also taken care of in open_namei.
1590 * name is in kernel space at this point.
1593 #if defined(IOP_CREATE_TAKES_BOOL)
1594 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1596 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1597 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1598 struct nameidata *nd)
1599 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1600 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1601 struct nameidata *nd)
1603 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1606 struct vattr *vattr = NULL;
1607 cred_t *credp = crref();
1608 const char *name = dp->d_name.name;
1614 code = afs_CreateAttr(&vattr);
1618 vattr->va_mode = mode;
1619 vattr->va_type = mode & S_IFMT;
1621 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1625 struct inode *ip = AFSTOV(vcp);
1627 afs_getattr(vcp, vattr, credp);
1628 afs_fill_inode(ip, vattr);
1629 insert_inode_hash(ip);
1630 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1631 dp->d_op = &afs_dentry_operations;
1633 dp->d_time = parent_vcache_dv(dip, credp);
1634 d_instantiate(dp, ip);
1637 afs_DestroyAttr(vattr);
1643 return afs_convert_code(code);
1646 /* afs_linux_lookup */
1647 static struct dentry *
1648 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1649 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1651 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1652 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1653 struct nameidata *nd)
1655 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1658 cred_t *credp = crref();
1659 struct vcache *vcp = NULL;
1660 const char *comp = dp->d_name.name;
1661 struct inode *ip = NULL;
1662 struct dentry *newdp = NULL;
1667 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1668 code = filter_enoent(code);
1669 if (code == ENOENT) {
1670 /* It's ok for the file to not be found. That's noted by the caller by
1671 * seeing that the dp->d_inode field is NULL (set by d_splice_alias or
1674 osi_Assert(vcp == NULL);
1682 struct vattr *vattr = NULL;
1683 struct vcache *parent_vc = VTOAFS(dip);
1685 if (parent_vc == vcp) {
1686 /* This is possible if the parent dir is a mountpoint to a volume,
1687 * and the dir entry we looked up is a mountpoint to the same
1688 * volume. Linux cannot cope with this, so return an error instead
1689 * of risking a deadlock or panic. */
1696 code = afs_CreateAttr(&vattr);
1704 afs_getattr(vcp, vattr, credp);
1705 afs_fill_inode(ip, vattr);
1706 if (hlist_unhashed(&ip->i_hash))
1707 insert_inode_hash(ip);
1709 afs_DestroyAttr(vattr);
1711 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1712 dp->d_op = &afs_dentry_operations;
1714 dp->d_time = parent_vcache_dv(dip, credp);
1718 if (ip && S_ISDIR(ip->i_mode)) {
1719 d_prune_aliases(ip);
1721 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1722 /* Only needed if this is a volume root */
1723 if (vcp->mvstat == 2)
1724 ip->i_flags |= S_AUTOMOUNT;
1728 * Take an extra reference so the inode doesn't go away if
1729 * d_splice_alias drops our reference on error.
1732 #ifdef HAVE_LINUX_IHOLD
1739 newdp = d_splice_alias(ip, dp);
1740 dentry_race_unlock();
1745 if (IS_ERR(newdp)) {
1746 /* d_splice_alias can return an error (EIO) if there is an existing
1747 * connected directory alias for this dentry. Add our dentry manually
1748 * ourselves if this happens. */
1751 #if defined(D_SPLICE_ALIAS_LEAK_ON_ERROR)
1752 /* Depending on the kernel version, d_splice_alias may or may not drop
1753 * the inode reference on error. If it didn't, do it here. */
1762 return ERR_PTR(afs_convert_code(code));
1770 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1773 cred_t *credp = crref();
1774 const char *name = newdp->d_name.name;
1775 struct inode *oldip = olddp->d_inode;
1777 /* If afs_link returned the vnode, we could instantiate the
1778 * dentry. Since it's not, we drop this one and do a new lookup.
1783 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1787 return afs_convert_code(code);
1790 /* We have to have a Linux specific sillyrename function, because we
1791 * also have to keep the dcache up to date when we're doing a silly
1792 * rename - so we don't want the generic vnodeops doing this behind our
1797 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1800 struct vcache *tvc = VTOAFS(dentry->d_inode);
1801 struct dentry *__dp = NULL;
1802 char *__name = NULL;
1805 if (afs_linux_nfsfs_renamed(dentry))
1813 osi_FreeSmallSpace(__name);
1814 __name = afs_newname();
1817 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1820 osi_FreeSmallSpace(__name);
1823 } while (__dp->d_inode != NULL);
1826 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1827 VTOAFS(dir), (char *)__dp->d_name.name,
1830 tvc->mvid.silly_name = __name;
1833 crfree(tvc->uncred);
1835 tvc->uncred = credp;
1836 tvc->f.states |= CUnlinked;
1837 afs_linux_set_nfsfs_renamed(dentry);
1839 __dp->d_time = 0; /* force to revalidate */
1840 d_move(dentry, __dp);
1842 osi_FreeSmallSpace(__name);
1853 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1856 cred_t *credp = crref();
1857 const char *name = dp->d_name.name;
1858 struct vcache *tvc = VTOAFS(dp->d_inode);
1860 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1861 && !(tvc->f.states & CUnlinked)) {
1863 code = afs_linux_sillyrename(dip, dp, credp);
1866 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1873 return afs_convert_code(code);
1878 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1881 cred_t *credp = crref();
1882 struct vattr *vattr = NULL;
1883 const char *name = dp->d_name.name;
1885 /* If afs_symlink returned the vnode, we could instantiate the
1886 * dentry. Since it's not, we drop this one and do a new lookup.
1891 code = afs_CreateAttr(&vattr);
1896 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1898 afs_DestroyAttr(vattr);
1903 return afs_convert_code(code);
1907 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1908 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1910 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1914 cred_t *credp = crref();
1915 struct vcache *tvcp = NULL;
1916 struct vattr *vattr = NULL;
1917 const char *name = dp->d_name.name;
1920 code = afs_CreateAttr(&vattr);
1925 vattr->va_mask = ATTR_MODE;
1926 vattr->va_mode = mode;
1928 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1931 struct inode *ip = AFSTOV(tvcp);
1933 afs_getattr(tvcp, vattr, credp);
1934 afs_fill_inode(ip, vattr);
1936 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1937 dp->d_op = &afs_dentry_operations;
1939 dp->d_time = parent_vcache_dv(dip, credp);
1940 d_instantiate(dp, ip);
1942 afs_DestroyAttr(vattr);
1948 return afs_convert_code(code);
1952 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1955 cred_t *credp = crref();
1956 const char *name = dp->d_name.name;
1958 /* locking kernel conflicts with glock? */
1961 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1964 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1965 * that failed because a directory is not empty. So, we map
1966 * EEXIST to ENOTEMPTY on linux.
1968 if (code == EEXIST) {
1977 return afs_convert_code(code);
1982 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1983 struct inode *newip, struct dentry *newdp
1984 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1985 , unsigned int flags
1990 cred_t *credp = crref();
1991 const char *oldname = olddp->d_name.name;
1992 const char *newname = newdp->d_name.name;
1993 struct dentry *rehash = NULL;
1995 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1997 return -EINVAL; /* no support for new flags yet */
2000 /* Prevent any new references during rename operation. */
2002 if (!d_unhashed(newdp)) {
2007 afs_maybe_shrink_dcache(olddp);
2010 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
2014 olddp->d_time = 0; /* force to revalidate */
2020 return afs_convert_code(code);
2024 /* afs_linux_ireadlink
2025 * Internal readlink which can return link contents to user or kernel space.
2026 * Note that the buffer is NOT supposed to be null-terminated.
2029 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
2032 cred_t *credp = crref();
2036 memset(&tuio, 0, sizeof(tuio));
2037 memset(&iov, 0, sizeof(iov));
2039 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
2040 code = afs_readlink(VTOAFS(ip), &tuio, credp);
2044 return maxlen - tuio.uio_resid;
2046 return afs_convert_code(code);
2049 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2050 /* afs_linux_readlink
2051 * Fill target (which is in user space) with contents of symlink.
2054 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
2057 struct inode *ip = dp->d_inode;
2060 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
2066 /* afs_linux_follow_link
2067 * a file system dependent link following routine.
2069 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2070 static const char *afs_linux_follow_link(struct dentry *dentry, void **link_data)
2072 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
2078 name = kmalloc(PATH_MAX, GFP_NOFS);
2080 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2081 return ERR_PTR(-EIO);
2088 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
2092 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2093 return ERR_PTR(code);
2100 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2101 return *link_data = name;
2103 nd_set_link(nd, name);
2108 #if defined(HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA)
2110 afs_linux_put_link(struct inode *inode, void *link_data)
2112 char *name = link_data;
2114 if (name && !IS_ERR(name))
2119 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
2121 char *name = nd_get_link(nd);
2123 if (name && !IS_ERR(name))
2126 #endif /* HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA */
2128 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2130 /* Populate a page by filling it from the cache file pointed at by cachefp
2131 * (which contains indicated chunk)
2132 * If task is NULL, the page copy occurs syncronously, and the routine
2133 * returns with page still locked. If task is non-NULL, then page copies
2134 * may occur in the background, and the page will be unlocked when it is
2135 * ready for use. Note that if task is non-NULL and we encounter an error
2136 * before we start the background copy, we MUST unlock 'page' before we return.
2139 afs_linux_read_cache(struct file *cachefp, struct page *page,
2140 int chunk, struct afs_lru_pages *alrupages,
2141 struct afs_pagecopy_task *task) {
2142 loff_t offset = page_offset(page);
2143 struct inode *cacheinode = cachefp->f_dentry->d_inode;
2144 struct page *newpage, *cachepage;
2145 struct address_space *cachemapping;
2149 cachemapping = cacheinode->i_mapping;
2153 /* If we're trying to read a page that's past the end of the disk
2154 * cache file, then just return a zeroed page */
2155 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
2156 zero_user_segment(page, 0, PAGE_SIZE);
2157 SetPageUptodate(page);
2163 /* From our offset, we now need to work out which page in the disk
2164 * file it corresponds to. This will be fun ... */
2165 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_SHIFT;
2167 while (cachepage == NULL) {
2168 cachepage = find_get_page(cachemapping, pageindex);
2171 newpage = page_cache_alloc(cachemapping);
2177 code = add_to_page_cache(newpage, cachemapping,
2178 pageindex, GFP_KERNEL);
2180 cachepage = newpage;
2182 afs_lru_cache_add(alrupages, cachepage);
2186 if (code != -EEXIST)
2190 lock_page(cachepage);
2194 if (!PageUptodate(cachepage)) {
2195 ClearPageError(cachepage);
2196 /* Note that ->readpage always handles unlocking the given page, even
2197 * when an error is returned. */
2198 code = cachemapping->a_ops->readpage(NULL, cachepage);
2199 if (!code && !task) {
2200 wait_on_page_locked(cachepage);
2203 unlock_page(cachepage);
2207 if (PageUptodate(cachepage)) {
2208 copy_highpage(page, cachepage);
2209 flush_dcache_page(page);
2210 SetPageUptodate(page);
2215 afs_pagecopy_queue_page(task, cachepage, page);
2227 put_page(cachepage);
2233 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2235 loff_t offset = page_offset(pp);
2236 struct inode *ip = FILE_INODE(fp);
2237 struct vcache *avc = VTOAFS(ip);
2239 struct file *cacheFp = NULL;
2242 struct afs_lru_pages lrupages;
2244 /* Not a UFS cache, don't do anything */
2245 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2248 /* No readpage (ex: tmpfs) , skip */
2249 if (cachefs_noreadpage)
2252 /* Can't do anything if the vcache isn't statd , or if the read
2253 * crosses a chunk boundary.
2255 if (!(avc->f.states & CStatd) ||
2256 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2260 ObtainWriteLock(&avc->lock, 911);
2262 /* XXX - See if hinting actually makes things faster !!! */
2264 /* See if we have a suitable entry already cached */
2268 /* We need to lock xdcache, then dcache, to handle situations where
2269 * the hint is on the free list. However, we can't safely do this
2270 * according to the locking hierarchy. So, use a non blocking lock.
2272 ObtainReadLock(&afs_xdcache);
2273 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2275 if (dcLocked && (tdc->index != NULLIDX)
2276 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2277 && tdc->f.chunk == AFS_CHUNK(offset)
2278 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2279 /* Bonus - the hint was correct */
2282 /* Only destroy the hint if its actually invalid, not if there's
2283 * just been a locking failure */
2285 ReleaseReadLock(&tdc->lock);
2292 ReleaseReadLock(&afs_xdcache);
2295 /* No hint, or hint is no longer valid - see if we can get something
2296 * directly from the dcache
2299 tdc = afs_FindDCache(avc, offset);
2302 ReleaseWriteLock(&avc->lock);
2307 ObtainReadLock(&tdc->lock);
2309 /* Is the dcache we've been given currently up to date */
2310 if (!afs_IsDCacheFresh(tdc, avc) ||
2311 (tdc->dflags & DFFetching))
2314 /* Update our hint for future abuse */
2317 /* Okay, so we've now got a cache file that is up to date */
2319 /* XXX - I suspect we should be locking the inodes before we use them! */
2321 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2322 if (cacheFp == NULL) {
2323 /* Problem getting the inode */
2327 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2328 cachefs_noreadpage = 1;
2333 afs_lru_cache_init(&lrupages);
2335 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupages, NULL);
2337 afs_lru_cache_finalize(&lrupages);
2339 filp_close(cacheFp, NULL);
2342 ReleaseReadLock(&tdc->lock);
2343 ReleaseWriteLock(&avc->lock);
2350 if (cacheFp != NULL) {
2351 filp_close(cacheFp, NULL);
2353 ReleaseWriteLock(&avc->lock);
2354 ReleaseReadLock(&tdc->lock);
2359 /* afs_linux_readpage
2361 * This function is split into two, because prepare_write/begin_write
2362 * require a readpage call which doesn't unlock the resulting page upon
2366 afs_linux_fillpage(struct file *fp, struct page *pp)
2371 struct iovec *iovecp;
2372 struct inode *ip = FILE_INODE(fp);
2373 afs_int32 cnt = page_count(pp);
2374 struct vcache *avc = VTOAFS(ip);
2375 afs_offs_t offset = page_offset(pp);
2379 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2389 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2390 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2392 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2397 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2398 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2399 99999); /* not a possible code value */
2401 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2403 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2404 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2406 AFS_DISCON_UNLOCK();
2409 /* XXX valid for no-cache also? Check last bits of files... :)
2410 * Cognate code goes in afs_NoCacheFetchProc. */
2411 if (auio->uio_resid) /* zero remainder of page */
2412 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2415 flush_dcache_page(pp);
2416 SetPageUptodate(pp);
2425 return afs_convert_code(code);
2429 afs_linux_prefetch(struct file *fp, struct page *pp)
2432 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2433 afs_offs_t offset = page_offset(pp);
2435 if (AFS_CHUNKOFFSET(offset) == 0) {
2437 struct vrequest *treq = NULL;
2442 code = afs_CreateReq(&treq, credp);
2443 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2444 tdc = afs_FindDCache(avc, offset);
2446 if (!(tdc->mflags & DFNextStarted))
2447 afs_PrefetchChunk(avc, tdc, credp, treq);
2450 ReleaseWriteLock(&avc->lock);
2452 afs_DestroyReq(treq);
2456 return afs_convert_code(code);
2461 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2462 struct list_head *page_list, unsigned num_pages)
2467 struct iovec* iovecp;
2468 struct nocache_read_request *ancr;
2470 struct afs_lru_pages lrupages;
2474 struct inode *ip = FILE_INODE(fp);
2475 struct vcache *avc = VTOAFS(ip);
2476 afs_int32 base_index = 0;
2477 afs_int32 page_count = 0;
2480 /* background thread must free: iovecp, auio, ancr */
2481 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2483 auio = osi_Alloc(sizeof(struct uio));
2484 auio->uio_iov = iovecp;
2485 auio->uio_iovcnt = num_pages;
2486 auio->uio_flag = UIO_READ;
2487 auio->uio_seg = AFS_UIOSYS;
2488 auio->uio_resid = num_pages * PAGE_SIZE;
2490 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2492 ancr->offset = auio->uio_offset;
2493 ancr->length = auio->uio_resid;
2495 afs_lru_cache_init(&lrupages);
2497 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2499 if(list_empty(page_list))
2502 pp = list_entry(page_list->prev, struct page, lru);
2503 /* If we allocate a page and don't remove it from page_list,
2504 * the page cache gets upset. */
2506 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2507 if(pp->index > isize) {
2514 offset = page_offset(pp);
2515 ancr->offset = auio->uio_offset = offset;
2516 base_index = pp->index;
2518 iovecp[page_ix].iov_len = PAGE_SIZE;
2519 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2520 if(base_index != pp->index) {
2524 iovecp[page_ix].iov_base = (void *) 0;
2526 ancr->length -= PAGE_SIZE;
2534 iovecp[page_ix].iov_base = (void *) 0;
2537 if(!PageLocked(pp)) {
2541 /* save the page for background map */
2542 iovecp[page_ix].iov_base = (void*) pp;
2544 /* and put it on the LRU cache */
2545 afs_lru_cache_add(&lrupages, pp);
2549 /* If there were useful pages in the page list, make sure all pages
2550 * are in the LRU cache, then schedule the read */
2552 afs_lru_cache_finalize(&lrupages);
2554 code = afs_ReadNoCache(avc, ancr, credp);
2557 /* If there is nothing for the background thread to handle,
2558 * it won't be freeing the things that we never gave it */
2559 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2560 osi_Free(auio, sizeof(struct uio));
2561 osi_Free(ancr, sizeof(struct nocache_read_request));
2563 /* we do not flush, release, or unmap pages--that will be
2564 * done for us by the background thread as each page comes in
2565 * from the fileserver */
2566 return afs_convert_code(code);
2571 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2573 cred_t *credp = NULL;
2575 struct iovec *iovecp;
2576 struct nocache_read_request *ancr;
2580 * Special case: if page is at or past end of file, just zero it and set
2583 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2584 zero_user_segment(pp, 0, PAGE_SIZE);
2585 SetPageUptodate(pp);
2592 /* receiver frees */
2593 auio = osi_Alloc(sizeof(struct uio));
2594 iovecp = osi_Alloc(sizeof(struct iovec));
2596 /* address can be NULL, because we overwrite it with 'pp', below */
2597 setup_uio(auio, iovecp, NULL, page_offset(pp),
2598 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2600 /* save the page for background map */
2601 get_page(pp); /* see above */
2602 auio->uio_iov->iov_base = (void*) pp;
2603 /* the background thread will free this */
2604 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2606 ancr->offset = page_offset(pp);
2607 ancr->length = PAGE_SIZE;
2610 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2613 return afs_convert_code(code);
2617 afs_linux_can_bypass(struct inode *ip) {
2619 switch(cache_bypass_strategy) {
2620 case NEVER_BYPASS_CACHE:
2622 case ALWAYS_BYPASS_CACHE:
2624 case LARGE_FILES_BYPASS_CACHE:
2625 if (i_size_read(ip) > cache_bypass_threshold)
2633 /* Check if a file is permitted to bypass the cache by policy, and modify
2634 * the cache bypass state recorded for that file */
2637 afs_linux_bypass_check(struct inode *ip) {
2640 int bypass = afs_linux_can_bypass(ip);
2643 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2651 afs_linux_readpage(struct file *fp, struct page *pp)
2655 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2656 code = afs_linux_bypass_readpage(fp, pp);
2658 code = afs_linux_fillpage(fp, pp);
2660 code = afs_linux_prefetch(fp, pp);
2667 /* Readpages reads a number of pages for a particular file. We use
2668 * this to optimise the reading, by limiting the number of times upon which
2669 * we have to lookup, lock and open vcaches and dcaches
2673 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2674 struct list_head *page_list, unsigned int num_pages)
2676 struct inode *inode = mapping->host;
2677 struct vcache *avc = VTOAFS(inode);
2679 struct file *cacheFp = NULL;
2681 unsigned int page_idx;
2683 struct afs_lru_pages lrupages;
2684 struct afs_pagecopy_task *task;
2686 if (afs_linux_bypass_check(inode))
2687 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2689 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2692 /* No readpage (ex: tmpfs) , skip */
2693 if (cachefs_noreadpage)
2697 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2702 ObtainWriteLock(&avc->lock, 912);
2705 task = afs_pagecopy_init_task();
2709 afs_lru_cache_init(&lrupages);
2711 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2712 struct page *page = list_entry(page_list->prev, struct page, lru);
2713 list_del(&page->lru);
2714 offset = page_offset(page);
2716 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2718 ReleaseReadLock(&tdc->lock);
2723 filp_close(cacheFp, NULL);
2730 if ((tdc = afs_FindDCache(avc, offset))) {
2731 ObtainReadLock(&tdc->lock);
2732 if (!afs_IsDCacheFresh(tdc, avc) ||
2733 (tdc->dflags & DFFetching)) {
2734 ReleaseReadLock(&tdc->lock);
2741 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2742 if (cacheFp == NULL) {
2743 /* Problem getting the inode */
2746 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2747 cachefs_noreadpage = 1;
2753 if (tdc && !add_to_page_cache(page, mapping, page->index,
2755 afs_lru_cache_add(&lrupages, page);
2757 /* Note that add_to_page_cache() locked 'page'.
2758 * afs_linux_read_cache() is guaranteed to handle unlocking it. */
2759 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupages, task);
2763 afs_lru_cache_finalize(&lrupages);
2767 filp_close(cacheFp, NULL);
2769 afs_pagecopy_put_task(task);
2773 ReleaseReadLock(&tdc->lock);
2777 ReleaseWriteLock(&avc->lock);
2782 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2785 afs_linux_prepare_writeback(struct vcache *avc) {
2787 struct pagewriter *pw;
2789 pid = MyPidxx2Pid(MyPidxx);
2790 /* Prevent recursion into the writeback code */
2791 spin_lock(&avc->pagewriter_lock);
2792 list_for_each_entry(pw, &avc->pagewriters, link) {
2793 if (pw->writer == pid) {
2794 spin_unlock(&avc->pagewriter_lock);
2795 return AOP_WRITEPAGE_ACTIVATE;
2798 spin_unlock(&avc->pagewriter_lock);
2800 /* Add ourselves to writer list */
2801 pw = osi_Alloc(sizeof(struct pagewriter));
2803 spin_lock(&avc->pagewriter_lock);
2804 list_add_tail(&pw->link, &avc->pagewriters);
2805 spin_unlock(&avc->pagewriter_lock);
2811 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2812 struct vrequest *treq = NULL;
2815 if (!afs_CreateReq(&treq, credp)) {
2816 code = afs_DoPartialWrite(avc, treq);
2817 afs_DestroyReq(treq);
2820 return afs_convert_code(code);
2824 afs_linux_complete_writeback(struct vcache *avc) {
2825 struct pagewriter *pw, *store;
2827 struct list_head tofree;
2829 INIT_LIST_HEAD(&tofree);
2830 pid = MyPidxx2Pid(MyPidxx);
2831 /* Remove ourselves from writer list */
2832 spin_lock(&avc->pagewriter_lock);
2833 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
2834 if (pw->writer == pid) {
2835 list_del(&pw->link);
2836 /* osi_Free may sleep so we need to defer it */
2837 list_add_tail(&pw->link, &tofree);
2840 spin_unlock(&avc->pagewriter_lock);
2841 list_for_each_entry_safe(pw, store, &tofree, link) {
2842 list_del(&pw->link);
2843 osi_Free(pw, sizeof(struct pagewriter));
2847 /* Writeback a given page syncronously. Called with no AFS locks held */
2849 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2850 unsigned long offset, unsigned int count,
2853 struct vcache *vcp = VTOAFS(ip);
2861 memset(&tuio, 0, sizeof(tuio));
2862 memset(&iovec, 0, sizeof(iovec));
2864 buffer = kmap(pp) + offset;
2865 base = page_offset(pp) + offset;
2868 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2869 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2870 ICL_TYPE_INT32, 99999);
2872 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2874 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2876 i_size_write(ip, vcp->f.m.Length);
2877 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2879 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2881 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2882 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2883 ICL_TYPE_INT32, code);
2892 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2893 unsigned long offset, unsigned int count)
2897 struct vcache *vcp = VTOAFS(ip);
2900 /* Catch recursive writeback. This occurs if the kernel decides
2901 * writeback is required whilst we are writing to the cache, or
2902 * flushing to the server. When we're running syncronously (as
2903 * opposed to from writepage) we can't actually do anything about
2904 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2907 ObtainWriteLock(&vcp->lock, 532);
2908 afs_linux_prepare_writeback(vcp);
2909 ReleaseWriteLock(&vcp->lock);
2913 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2916 ObtainWriteLock(&vcp->lock, 533);
2918 code1 = afs_linux_dopartialwrite(vcp, credp);
2919 afs_linux_complete_writeback(vcp);
2920 ReleaseWriteLock(&vcp->lock);
2931 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2932 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2934 afs_linux_writepage(struct page *pp)
2937 struct address_space *mapping = pp->mapping;
2938 struct inode *inode;
2941 unsigned int to = PAGE_SIZE;
2948 inode = mapping->host;
2949 vcp = VTOAFS(inode);
2950 isize = i_size_read(inode);
2952 /* Don't defeat an earlier truncate */
2953 if (page_offset(pp) > isize) {
2954 set_page_writeback(pp);
2960 ObtainWriteLock(&vcp->lock, 537);
2961 code = afs_linux_prepare_writeback(vcp);
2962 if (code == AOP_WRITEPAGE_ACTIVATE) {
2963 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2964 * to return with the page still locked */
2965 ReleaseWriteLock(&vcp->lock);
2970 /* Grab the creds structure currently held in the vnode, and
2971 * get a reference to it, in case it goes away ... */
2977 ReleaseWriteLock(&vcp->lock);
2980 set_page_writeback(pp);
2982 SetPageUptodate(pp);
2984 /* We can unlock the page here, because it's protected by the
2985 * page_writeback flag. This should make us less vulnerable to
2986 * deadlocking in afs_write and afs_DoPartialWrite
2990 /* If this is the final page, then just write the number of bytes that
2991 * are actually in it */
2992 if ((isize - page_offset(pp)) < to )
2993 to = isize - page_offset(pp);
2995 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2998 ObtainWriteLock(&vcp->lock, 538);
3000 /* As much as we might like to ignore a file server error here,
3001 * and just try again when we close(), unfortunately StoreAllSegments
3002 * will invalidate our chunks if the server returns a permanent error,
3003 * so we need to at least try and get that error back to the user
3006 code1 = afs_linux_dopartialwrite(vcp, credp);
3008 afs_linux_complete_writeback(vcp);
3009 ReleaseWriteLock(&vcp->lock);
3014 end_page_writeback(pp);
3026 /* afs_linux_permission
3027 * Check access rights - returns error if can't check or permission denied.
3030 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3031 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
3032 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
3033 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
3035 afs_linux_permission(struct inode *ip, int mode)
3042 /* Check for RCU path walking */
3043 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3044 if (flags & IPERM_FLAG_RCU)
3046 #elif defined(MAY_NOT_BLOCK)
3047 if (mode & MAY_NOT_BLOCK)
3053 if (mode & MAY_EXEC)
3055 if (mode & MAY_READ)
3057 if (mode & MAY_WRITE)
3059 code = afs_access(VTOAFS(ip), tmp, credp);
3063 return afs_convert_code(code);
3067 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
3071 struct inode *inode = FILE_INODE(file);
3072 loff_t pagebase = page_offset(page);
3074 if (i_size_read(inode) < (pagebase + offset))
3075 i_size_write(inode, pagebase + offset);
3077 if (PageChecked(page)) {
3078 SetPageUptodate(page);
3079 ClearPageChecked(page);
3082 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
3088 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
3092 /* http://kerneltrap.org/node/4941 details the expected behaviour of
3093 * prepare_write. Essentially, if the page exists within the file,
3094 * and is not being fully written, then we should populate it.
3097 if (!PageUptodate(page)) {
3098 loff_t pagebase = page_offset(page);
3099 loff_t isize = i_size_read(page->mapping->host);
3101 /* Is the location we are writing to beyond the end of the file? */
3102 if (pagebase >= isize ||
3103 ((from == 0) && (pagebase + to) >= isize)) {
3104 zero_user_segments(page, 0, from, to, PAGE_SIZE);
3105 SetPageChecked(page);
3106 /* Are we we writing a full page */
3107 } else if (from == 0 && to == PAGE_SIZE) {
3108 SetPageChecked(page);
3109 /* Is the page readable, if it's wronly, we don't care, because we're
3110 * not actually going to read from it ... */
3111 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
3112 /* We don't care if fillpage fails, because if it does the page
3113 * won't be marked as up to date
3115 afs_linux_fillpage(file, page);
3121 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3123 afs_linux_write_end(struct file *file, struct address_space *mapping,
3124 loff_t pos, unsigned len, unsigned copied,
3125 struct page *page, void *fsdata)
3128 unsigned int from = pos & (PAGE_SIZE - 1);
3130 code = afs_linux_commit_write(file, page, from, from + copied);
3138 afs_linux_write_begin(struct file *file, struct address_space *mapping,
3139 loff_t pos, unsigned len, unsigned flags,
3140 struct page **pagep, void **fsdata)
3143 pgoff_t index = pos >> PAGE_SHIFT;
3144 unsigned int from = pos & (PAGE_SIZE - 1);
3147 page = grab_cache_page_write_begin(mapping, index, flags);
3154 code = afs_linux_prepare_write(file, page, from, from + len);
3164 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3166 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
3168 struct dentry **dpp;
3169 struct dentry *target;
3171 if (current->total_link_count > 0) {
3172 /* avoid symlink resolution limits when resolving; we cannot contribute to
3173 * an infinite symlink loop */
3174 /* only do this for follow_link when total_link_count is positive to be
3175 * on the safe side; there is at least one code path in the Linux
3176 * kernel where it seems like it may be possible to get here without
3177 * total_link_count getting incremented. it is not clear on how that
3178 * path is actually reached, but guard against it just to be safe */
3179 current->total_link_count--;
3182 target = canonical_dentry(dentry->d_inode);
3184 # ifdef STRUCT_NAMEIDATA_HAS_PATH
3185 dpp = &nd->path.dentry;
3195 *dpp = dget(dentry);
3198 nd->last_type = LAST_BIND;
3202 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
3205 static struct inode_operations afs_file_iops = {
3206 .permission = afs_linux_permission,
3207 .getattr = afs_linux_getattr,
3208 .setattr = afs_notify_change,
3211 static struct address_space_operations afs_file_aops = {
3212 .readpage = afs_linux_readpage,
3213 .readpages = afs_linux_readpages,
3214 .writepage = afs_linux_writepage,
3215 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3216 .write_begin = afs_linux_write_begin,
3217 .write_end = afs_linux_write_end,
3219 .commit_write = afs_linux_commit_write,
3220 .prepare_write = afs_linux_prepare_write,
3225 /* Separate ops vector for directories. Linux 2.2 tests type of inode
3226 * by what sort of operation is allowed.....
3229 static struct inode_operations afs_dir_iops = {
3230 .setattr = afs_notify_change,
3231 .create = afs_linux_create,
3232 .lookup = afs_linux_lookup,
3233 .link = afs_linux_link,
3234 .unlink = afs_linux_unlink,
3235 .symlink = afs_linux_symlink,
3236 .mkdir = afs_linux_mkdir,
3237 .rmdir = afs_linux_rmdir,
3238 .rename = afs_linux_rename,
3239 .getattr = afs_linux_getattr,
3240 .permission = afs_linux_permission,
3241 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3242 .follow_link = afs_linux_dir_follow_link,
3246 /* We really need a separate symlink set of ops, since do_follow_link()
3247 * determines if it _is_ a link by checking if the follow_link op is set.
3249 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3251 afs_symlink_filler(struct file *file, struct page *page)
3253 struct inode *ip = (struct inode *)page->mapping->host;
3254 char *p = (char *)kmap(page);
3258 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3263 p[code] = '\0'; /* null terminate? */
3265 SetPageUptodate(page);
3277 static struct address_space_operations afs_symlink_aops = {
3278 .readpage = afs_symlink_filler
3280 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3282 static struct inode_operations afs_symlink_iops = {
3283 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3284 .readlink = page_readlink,
3285 # if defined(HAVE_LINUX_PAGE_GET_LINK)
3286 .get_link = page_get_link,
3287 # elif defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3288 .follow_link = page_follow_link,
3290 .follow_link = page_follow_link_light,
3291 .put_link = page_put_link,
3293 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3294 .readlink = afs_linux_readlink,
3295 .follow_link = afs_linux_follow_link,
3296 .put_link = afs_linux_put_link,
3297 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3298 .setattr = afs_notify_change,
3302 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3305 vattr2inode(ip, vattr);
3307 #ifdef STRUCT_ADDRESS_SPACE_HAS_BACKING_DEV_INFO
3308 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3310 /* Reset ops if symlink or directory. */
3311 if (S_ISREG(ip->i_mode)) {
3312 ip->i_op = &afs_file_iops;
3313 ip->i_fop = &afs_file_fops;
3314 ip->i_data.a_ops = &afs_file_aops;
3316 } else if (S_ISDIR(ip->i_mode)) {
3317 ip->i_op = &afs_dir_iops;
3318 ip->i_fop = &afs_dir_fops;
3320 } else if (S_ISLNK(ip->i_mode)) {
3321 ip->i_op = &afs_symlink_iops;
3322 #if defined(HAVE_LINUX_INODE_NOHIGHMEM)
3323 inode_nohighmem(ip);
3325 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3326 ip->i_data.a_ops = &afs_symlink_aops;
3327 ip->i_mapping = &ip->i_data;