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 /* Kernels from before 2.6.19 may not be able to return errors from
68 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
69 # define ERRORS_FROM_D_REVALIDATE
72 int cachefs_noreadpage = 0;
74 extern struct backing_dev_info *afs_backing_dev_info;
76 extern struct vcache *afs_globalVp;
78 /* Handle interfacing with Linux's pagevec/lru facilities */
80 #if defined(HAVE_LINUX_LRU_CACHE_ADD_FILE) || defined(HAVE_LINUX_LRU_CACHE_ADD)
83 * Linux's lru_cache_add_file provides a simplified LRU interface without
86 struct afs_lru_pages {
91 afs_lru_cache_init(struct afs_lru_pages *alrupages)
97 afs_lru_cache_add(struct afs_lru_pages *alrupages, struct page *page)
99 # if defined(HAVE_LINUX_LRU_CACHE_ADD)
101 # elif defined(HAVE_LINUX_LRU_CACHE_ADD_FILE)
102 lru_cache_add_file(page);
104 # error need a kernel function to add a page to the kernel lru cache
109 afs_lru_cache_finalize(struct afs_lru_pages *alrupages)
115 /* Linux's pagevec/lru interfaces require a pagevec */
116 struct afs_lru_pages {
117 struct pagevec lrupv;
121 afs_lru_cache_init(struct afs_lru_pages *alrupages)
123 # if defined(PAGEVEC_INIT_COLD_ARG)
124 pagevec_init(&alrupages->lrupv, 0);
126 pagevec_init(&alrupages->lrupv);
130 # ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
131 # define __pagevec_lru_add_file __pagevec_lru_add
135 afs_lru_cache_add(struct afs_lru_pages *alrupages, struct page *page)
138 if (!pagevec_add(&alrupages->lrupv, page))
139 __pagevec_lru_add_file(&alrupages->lrupv);
143 afs_lru_cache_finalize(struct afs_lru_pages *alrupages)
145 if (pagevec_count(&alrupages->lrupv))
146 __pagevec_lru_add_file(&alrupages->lrupv);
148 #endif /* !HAVE_LINUX_LRU_ADD_FILE */
150 /* This function converts a positive error code from AFS into a negative
151 * code suitable for passing into the Linux VFS layer. It checks that the
152 * error code is within the permissable bounds for the ERR_PTR mechanism.
154 * _All_ error codes which come from the AFS layer should be passed through
155 * this function before being returned to the kernel.
159 afs_convert_code(int code) {
160 if ((code >= 0) && (code <= MAX_ERRNO))
166 /* Linux doesn't require a credp for many functions, and crref is an expensive
167 * operation. This helper function avoids obtaining it for VerifyVCache calls
171 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
172 cred_t *credp = NULL;
173 struct vrequest *treq = NULL;
176 if (avc->f.states & CStatd) {
184 code = afs_CreateReq(&treq, credp);
186 code = afs_VerifyVCache(avc, treq);
187 afs_DestroyReq(treq);
195 return afs_convert_code(code);
198 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
199 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
201 afs_linux_read_iter(struct kiocb *iocb, struct iov_iter *iter)
202 # elif defined(LINUX_HAS_NONVECTOR_AIO)
204 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
208 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
209 unsigned long bufsize, loff_t pos)
212 struct file *fp = iocb->ki_filp;
214 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
215 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
216 loff_t pos = iocb->ki_pos;
217 unsigned long bufsize = iter->nr_segs;
222 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
223 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
224 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
225 code = afs_linux_VerifyVCache(vcp, NULL);
228 /* Linux's FlushPages implementation doesn't ever use credp,
229 * so we optimise by not using it */
230 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
232 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
233 code = generic_file_read_iter(iocb, iter);
235 code = generic_file_aio_read(iocb, buf, bufsize, pos);
240 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
241 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
242 (afs_int32)bufsize, ICL_TYPE_INT32, code);
248 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
251 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
254 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
255 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
257 code = afs_linux_VerifyVCache(vcp, NULL);
260 /* Linux's FlushPages implementation doesn't ever use credp,
261 * so we optimise by not using it */
262 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
264 code = do_sync_read(fp, buf, count, offp);
268 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
269 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
277 /* Now we have integrated VM for writes as well as reads. the generic write operations
278 * also take care of re-positioning the pointer if file is open in append
279 * mode. Call fake open/close to ensure we do writes of core dumps.
281 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
282 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
284 afs_linux_write_iter(struct kiocb *iocb, struct iov_iter *iter)
285 # elif defined(LINUX_HAS_NONVECTOR_AIO)
287 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
291 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
292 unsigned long bufsize, loff_t pos)
296 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
298 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
299 loff_t pos = iocb->ki_pos;
300 unsigned long bufsize = iter->nr_segs;
305 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
306 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
307 (afs_int32)bufsize, ICL_TYPE_INT32,
308 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
310 code = afs_linux_VerifyVCache(vcp, &credp);
312 ObtainWriteLock(&vcp->lock, 529);
314 ReleaseWriteLock(&vcp->lock);
317 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
318 code = generic_file_write_iter(iocb, iter);
320 code = generic_file_aio_write(iocb, buf, bufsize, pos);
325 ObtainWriteLock(&vcp->lock, 530);
327 if (vcp->execsOrWriters == 1 && !credp)
330 afs_FakeClose(vcp, credp);
331 ReleaseWriteLock(&vcp->lock);
333 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
334 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
335 (afs_int32)bufsize, ICL_TYPE_INT32, code);
344 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
347 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
352 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
353 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
354 (fp->f_flags & O_APPEND) ? 99998 : 99999);
356 code = afs_linux_VerifyVCache(vcp, &credp);
358 ObtainWriteLock(&vcp->lock, 529);
360 ReleaseWriteLock(&vcp->lock);
363 code = do_sync_write(fp, buf, count, offp);
367 ObtainWriteLock(&vcp->lock, 530);
369 if (vcp->execsOrWriters == 1 && !credp)
372 afs_FakeClose(vcp, credp);
373 ReleaseWriteLock(&vcp->lock);
375 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
376 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
386 extern int BlobScan(struct dcache * afile, afs_int32 ablob, afs_int32 *ablobOut);
388 /* This is a complete rewrite of afs_readdir, since we can make use of
389 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
390 * handling and use of bulkstats will need to be reflected here as well.
393 #if defined(USE_FOP_ITERATE)
394 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
396 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
399 struct vcache *avc = VTOAFS(FILE_INODE(fp));
400 struct vrequest *treq = NULL;
406 struct DirBuffer entry;
409 afs_size_t origOffset, tlen;
410 cred_t *credp = crref();
411 struct afs_fakestat_state fakestat;
414 AFS_STATCNT(afs_readdir);
416 code = afs_convert_code(afs_CreateReq(&treq, credp));
421 afs_InitFakeStat(&fakestat);
422 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, treq));
426 /* update the cache entry */
428 code = afs_convert_code(afs_VerifyVCache(avc, treq));
432 /* get a reference to the entire directory */
433 tdc = afs_GetDCache(avc, (afs_size_t) 0, treq, &origOffset, &tlen, 1);
439 ObtainWriteLock(&avc->lock, 811);
440 ObtainReadLock(&tdc->lock);
442 * Make sure that the data in the cache is current. There are two
443 * cases we need to worry about:
444 * 1. The cache data is being fetched by another process.
445 * 2. The cache data is no longer valid
447 while ((avc->f.states & CStatd)
448 && (tdc->dflags & DFFetching)
449 && afs_IsDCacheFresh(tdc, avc)) {
450 ReleaseReadLock(&tdc->lock);
451 ReleaseWriteLock(&avc->lock);
452 afs_osi_Sleep(&tdc->validPos);
453 ObtainWriteLock(&avc->lock, 812);
454 ObtainReadLock(&tdc->lock);
456 if (!(avc->f.states & CStatd)
457 || !afs_IsDCacheFresh(tdc, avc)) {
458 ReleaseReadLock(&tdc->lock);
459 ReleaseWriteLock(&avc->lock);
464 /* Set the readdir-in-progress flag, and downgrade the lock
465 * to shared so others will be able to acquire a read lock.
467 avc->f.states |= CReadDir;
468 avc->dcreaddir = tdc;
469 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
470 ConvertWToSLock(&avc->lock);
472 /* Fill in until we get an error or we're done. This implementation
473 * takes an offset in units of blobs, rather than bytes.
476 #if defined(USE_FOP_ITERATE)
479 offset = (int) fp->f_pos;
483 code = BlobScan(tdc, offset, &dirpos);
484 if (code == 0 && dirpos == 0) {
485 /* We've reached EOF of the dir blob, so we can stop looking for
491 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
494 if (!(avc->f.states & CCorrupt)) {
495 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
496 afs_warn("afs: Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)\n",
497 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
498 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
499 tc ? tc->cellName : "",
500 (unsigned long)&tdc->f.inode, dirpos);
502 afs_PutCell(tc, READ_LOCK);
503 UpgradeSToWLock(&avc->lock, 814);
504 avc->f.states |= CCorrupt;
510 de = (struct DirEntry *)entry.data;
511 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
512 ntohl(de->fid.vnode));
513 len = strlen(de->name);
515 /* filldir returns -EINVAL when the buffer is full. */
517 unsigned int type = DT_UNKNOWN;
518 struct VenusFid afid;
521 afid.Cell = avc->f.fid.Cell;
522 afid.Fid.Volume = avc->f.fid.Fid.Volume;
523 afid.Fid.Vnode = ntohl(de->fid.vnode);
524 afid.Fid.Unique = ntohl(de->fid.vunique);
525 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
527 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
528 if (tvc->mvstat != AFS_MVSTAT_FILE) {
530 } else if (((tvc->f.states) & (CStatd | CTruth))) {
531 /* CTruth will be set if the object has
536 else if (vtype == VREG)
538 /* Don't do this until we're sure it can't be a mtpt */
539 /* else if (vtype == VLNK)
541 /* what other types does AFS support? */
543 /* clean up from afs_FindVCache */
547 * If this is NFS readdirplus, then the filler is going to
548 * call getattr on this inode, which will deadlock if we're
552 #if defined(USE_FOP_ITERATE)
553 /* dir_emit returns a bool - true when it succeeds.
554 * Inverse the result to fit with how we check "code" */
555 code = !dir_emit(ctx, de->name, len, ino, type);
557 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
564 offset = dirpos + 1 + ((len + 16) >> 5);
566 /* If filldir didn't fill in the last one this is still pointing to that
572 #if defined(USE_FOP_ITERATE)
573 ctx->pos = (loff_t) offset;
575 fp->f_pos = (loff_t) offset;
577 ReleaseReadLock(&tdc->lock);
579 UpgradeSToWLock(&avc->lock, 813);
580 avc->f.states &= ~CReadDir;
582 avc->readdir_pid = 0;
583 ReleaseSharedLock(&avc->lock);
586 afs_PutFakeStat(&fakestat);
587 afs_DestroyReq(treq);
594 /* in afs_pioctl.c */
595 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
598 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
600 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
606 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
608 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
612 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
613 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_LONG,
614 vmap->vm_end - vmap->vm_start, ICL_TYPE_LONG, 0);
616 /* get a validated vcache entry */
617 code = afs_linux_VerifyVCache(vcp, NULL);
620 /* Linux's Flushpage implementation doesn't use credp, so optimise
621 * our code to not need to crref() it */
622 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
624 code = generic_file_mmap(fp, vmap);
627 vcp->f.states |= CMAPPED;
635 afs_linux_open(struct inode *ip, struct file *fp)
637 struct vcache *vcp = VTOAFS(ip);
638 cred_t *credp = crref();
642 code = afs_open(&vcp, fp->f_flags, credp);
646 return afs_convert_code(code);
650 afs_linux_release(struct inode *ip, struct file *fp)
652 struct vcache *vcp = VTOAFS(ip);
653 cred_t *credp = crref();
657 code = afs_close(vcp, fp->f_flags, credp);
658 ObtainWriteLock(&vcp->lock, 807);
663 ReleaseWriteLock(&vcp->lock);
667 return afs_convert_code(code);
671 #if defined(FOP_FSYNC_TAKES_DENTRY)
672 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
673 #elif defined(FOP_FSYNC_TAKES_RANGE)
674 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
676 afs_linux_fsync(struct file *fp, int datasync)
680 struct inode *ip = FILE_INODE(fp);
681 cred_t *credp = crref();
683 #if defined(FOP_FSYNC_TAKES_RANGE)
684 afs_linux_lock_inode(ip);
687 code = afs_fsync(VTOAFS(ip), credp);
689 #if defined(FOP_FSYNC_TAKES_RANGE)
690 afs_linux_unlock_inode(ip);
693 return afs_convert_code(code);
699 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
702 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
703 cred_t *credp = crref();
704 struct AFS_FLOCK flock;
706 /* Convert to a lock format afs_lockctl understands. */
707 memset(&flock, 0, sizeof(flock));
708 flock.l_type = flp->fl_type;
709 flock.l_pid = flp->fl_pid;
711 flock.l_start = flp->fl_start;
712 if (flp->fl_end == OFFSET_MAX)
713 flock.l_len = 0; /* Lock to end of file */
715 flock.l_len = flp->fl_end - flp->fl_start + 1;
717 /* Safe because there are no large files, yet */
718 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
719 if (cmd == F_GETLK64)
721 else if (cmd == F_SETLK64)
723 else if (cmd == F_SETLKW64)
725 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
728 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
731 if ((code == 0 || flp->fl_type == F_UNLCK) &&
732 (cmd == F_SETLK || cmd == F_SETLKW)) {
733 code = afs_posix_lock_file(fp, flp);
734 if (code && flp->fl_type != F_UNLCK) {
735 struct AFS_FLOCK flock2;
737 flock2.l_type = F_UNLCK;
739 afs_lockctl(vcp, &flock2, F_SETLK, credp);
743 /* If lockctl says there are no conflicting locks, then also check with the
744 * kernel, as lockctl knows nothing about byte range locks
746 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
747 afs_posix_test_lock(fp, flp);
748 /* If we found a lock in the kernel's structure, return it */
749 if (flp->fl_type != F_UNLCK) {
755 /* Convert flock back to Linux's file_lock */
756 flp->fl_type = flock.l_type;
757 flp->fl_pid = flock.l_pid;
758 flp->fl_start = flock.l_start;
759 if (flock.l_len == 0)
760 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
762 flp->fl_end = flock.l_start + flock.l_len - 1;
768 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
770 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
772 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
773 cred_t *credp = crref();
774 struct AFS_FLOCK flock;
775 /* Convert to a lock format afs_lockctl understands. */
776 memset(&flock, 0, sizeof(flock));
777 flock.l_type = flp->fl_type;
778 flock.l_pid = flp->fl_pid;
783 /* Safe because there are no large files, yet */
784 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
785 if (cmd == F_GETLK64)
787 else if (cmd == F_SETLK64)
789 else if (cmd == F_SETLKW64)
791 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
794 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
797 if ((code == 0 || flp->fl_type == F_UNLCK) &&
798 (cmd == F_SETLK || cmd == F_SETLKW)) {
799 flp->fl_flags &=~ FL_SLEEP;
800 code = flock_lock_file_wait(fp, flp);
801 if (code && flp->fl_type != F_UNLCK) {
802 struct AFS_FLOCK flock2;
804 flock2.l_type = F_UNLCK;
806 afs_lockctl(vcp, &flock2, F_SETLK, credp);
810 /* Convert flock back to Linux's file_lock */
811 flp->fl_type = flock.l_type;
812 flp->fl_pid = flock.l_pid;
820 * essentially the same as afs_fsync() but we need to get the return
821 * code for the sys_close() here, not afs_linux_release(), so call
822 * afs_StoreAllSegments() with AFS_LASTSTORE
825 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
826 afs_linux_flush(struct file *fp, fl_owner_t id)
828 afs_linux_flush(struct file *fp)
831 struct vrequest *treq = NULL;
839 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
847 vcp = VTOAFS(FILE_INODE(fp));
849 code = afs_CreateReq(&treq, credp);
852 /* If caching is bypassed for this file, or globally, just return 0 */
853 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
856 ObtainReadLock(&vcp->lock);
857 if (vcp->cachingStates & FCSBypass)
859 ReleaseReadLock(&vcp->lock);
862 /* future proof: don't rely on 0 return from afs_InitReq */
867 ObtainSharedLock(&vcp->lock, 535);
868 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
869 UpgradeSToWLock(&vcp->lock, 536);
870 if (!AFS_IS_DISCONNECTED) {
871 code = afs_StoreAllSegments(vcp,
873 AFS_SYNC | AFS_LASTSTORE);
875 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
877 ConvertWToSLock(&vcp->lock);
879 code = afs_CheckCode(code, treq, 54);
880 ReleaseSharedLock(&vcp->lock);
883 afs_DestroyReq(treq);
888 return afs_convert_code(code);
891 struct file_operations afs_dir_fops = {
892 .read = generic_read_dir,
893 #if defined(USE_FOP_ITERATE)
894 .iterate = afs_linux_readdir,
896 .readdir = afs_linux_readdir,
898 .unlocked_ioctl = afs_unlocked_xioctl,
899 .compat_ioctl = afs_unlocked_xioctl,
900 .open = afs_linux_open,
901 .release = afs_linux_release,
902 .llseek = default_llseek,
903 #ifdef HAVE_LINUX_NOOP_FSYNC
906 .fsync = simple_sync_file,
910 struct file_operations afs_file_fops = {
911 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
912 .read_iter = afs_linux_read_iter,
913 .write_iter = afs_linux_write_iter,
914 # if !defined(HAVE_LINUX___VFS_WRITE) && !defined(HAVE_LINUX_KERNEL_WRITE)
915 .read = new_sync_read,
916 .write = new_sync_write,
918 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
919 .aio_read = afs_linux_aio_read,
920 .aio_write = afs_linux_aio_write,
921 .read = do_sync_read,
922 .write = do_sync_write,
924 .read = afs_linux_read,
925 .write = afs_linux_write,
927 .unlocked_ioctl = afs_unlocked_xioctl,
928 .compat_ioctl = afs_unlocked_xioctl,
929 .mmap = afs_linux_mmap,
930 .open = afs_linux_open,
931 .flush = afs_linux_flush,
932 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
933 .sendfile = generic_file_sendfile,
935 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE) && !defined(HAVE_LINUX_DEFAULT_FILE_SPLICE_READ)
936 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
937 .splice_write = iter_file_splice_write,
939 .splice_write = generic_file_splice_write,
941 .splice_read = generic_file_splice_read,
943 .release = afs_linux_release,
944 .fsync = afs_linux_fsync,
945 .lock = afs_linux_lock,
946 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
947 .flock = afs_linux_flock,
949 .llseek = default_llseek,
952 static struct dentry *
953 canonical_dentry(struct inode *ip)
955 struct vcache *vcp = VTOAFS(ip);
956 struct dentry *first = NULL, *ret = NULL, *cur;
957 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
958 struct hlist_node *p;
962 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
963 * otherwise, use the first dentry in ip->i_dentry.
964 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
966 /* note that vcp->target_link specifies which dentry to use, but we have
967 * no reference held on that dentry. so, we cannot use or dereference
968 * vcp->target_link itself, since it may have been freed. instead, we only
969 * use it to compare to pointers in the ip->i_dentry list. */
973 afs_d_alias_lock(ip);
975 #if defined(D_ALIAS_IS_HLIST)
976 # if defined(HLIST_ITERATOR_NO_NODE)
977 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
979 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
982 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
985 if (!vcp->target_link || cur == vcp->target_link) {
998 vcp->target_link = ret;
1001 afs_linux_dget(ret);
1003 afs_d_alias_unlock(ip);
1008 /**********************************************************************
1009 * AFS Linux dentry operations
1010 **********************************************************************/
1012 /* afs_linux_revalidate
1013 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
1016 afs_linux_revalidate(struct dentry *dp)
1018 struct vattr *vattr = NULL;
1019 struct vcache *vcp = VTOAFS(dp->d_inode);
1023 if (afs_shuttingdown != AFS_RUNNING)
1028 code = afs_CreateAttr(&vattr);
1033 /* This avoids the crref when we don't have to do it. Watch for
1034 * changes in afs_getattr that don't get replicated here!
1036 if (vcp->f.states & CStatd &&
1037 (!afs_fakestat_enable || vcp->mvstat != AFS_MVSTAT_MTPT) &&
1039 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
1040 code = afs_CopyOutAttrs(vcp, vattr);
1043 code = afs_getattr(vcp, vattr, credp);
1048 afs_fill_inode(AFSTOV(vcp), vattr);
1050 afs_DestroyAttr(vattr);
1055 return afs_convert_code(code);
1059 * Set iattr data into vattr. Assume vattr cleared before call.
1062 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1064 vattrp->va_mask = iattrp->ia_valid;
1065 if (iattrp->ia_valid & ATTR_MODE)
1066 vattrp->va_mode = iattrp->ia_mode;
1067 if (iattrp->ia_valid & ATTR_UID)
1068 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1069 if (iattrp->ia_valid & ATTR_GID)
1070 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1071 if (iattrp->ia_valid & ATTR_SIZE)
1072 vattrp->va_size = iattrp->ia_size;
1073 if (iattrp->ia_valid & ATTR_ATIME) {
1074 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1075 vattrp->va_atime.tv_nsec = 0;
1077 if (iattrp->ia_valid & ATTR_MTIME) {
1078 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1079 vattrp->va_mtime.tv_nsec = 0;
1081 if (iattrp->ia_valid & ATTR_CTIME) {
1082 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1083 vattrp->va_ctime.tv_nsec = 0;
1088 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1091 vattr2inode(struct inode *ip, struct vattr *vp)
1093 ip->i_ino = vp->va_nodeid;
1094 #ifdef HAVE_LINUX_SET_NLINK
1095 set_nlink(ip, vp->va_nlink);
1097 ip->i_nlink = vp->va_nlink;
1099 ip->i_blocks = vp->va_blocks;
1100 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1101 ip->i_blkbits = AFS_BLKBITS;
1103 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1104 ip->i_blksize = vp->va_blocksize;
1106 ip->i_rdev = vp->va_rdev;
1107 ip->i_mode = vp->va_mode;
1108 ip->i_uid = afs_make_kuid(vp->va_uid);
1109 ip->i_gid = afs_make_kgid(vp->va_gid);
1110 i_size_write(ip, vp->va_size);
1111 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1112 ip->i_atime.tv_nsec = 0;
1113 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1114 /* Set the mtime nanoseconds to the sysname generation number.
1115 * This convinces NFS clients that all directories have changed
1116 * any time the sysname list changes.
1118 ip->i_mtime.tv_nsec = afs_sysnamegen;
1119 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1120 ip->i_ctime.tv_nsec = 0;
1123 /* afs_notify_change
1124 * Linux version of setattr call. What to change is in the iattr struct.
1125 * We need to set bits in both the Linux inode as well as the vcache.
1128 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1130 struct vattr *vattr = NULL;
1131 cred_t *credp = crref();
1132 struct inode *ip = dp->d_inode;
1136 code = afs_CreateAttr(&vattr);
1141 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1143 code = afs_setattr(VTOAFS(ip), vattr, credp);
1145 afs_getattr(VTOAFS(ip), vattr, credp);
1146 vattr2inode(ip, vattr);
1148 afs_DestroyAttr(vattr);
1153 return afs_convert_code(code);
1156 #if defined(IOP_GETATTR_TAKES_PATH_STRUCT)
1158 afs_linux_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int sync_mode)
1160 int err = afs_linux_revalidate(path->dentry);
1162 generic_fillattr(path->dentry->d_inode, stat);
1168 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1170 int err = afs_linux_revalidate(dentry);
1172 generic_fillattr(dentry->d_inode, stat);
1179 parent_vcache_dv(struct inode *inode, cred_t *credp)
1182 struct vcache *pvcp;
1185 * If parent is a mount point and we are using fakestat, we may need
1186 * to look at the fake vcache entry instead of what the vfs is giving
1187 * us. The fake entry is the one with the useful DataVersion.
1189 pvcp = VTOAFS(inode);
1190 if (pvcp->mvstat == AFS_MVSTAT_MTPT && afs_fakestat_enable) {
1191 struct vrequest treq;
1192 struct afs_fakestat_state fakestate;
1198 afs_InitReq(&treq, credp);
1199 afs_InitFakeStat(&fakestate);
1200 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1203 afs_PutFakeStat(&fakestate);
1205 return hgetlo(pvcp->f.m.DataVersion);
1209 filter_enoent(int code)
1211 #ifdef HAVE_LINUX_FATAL_SIGNAL_PENDING
1212 if (code == ENOENT && fatal_signal_pending(current)) {
1219 #ifndef D_SPLICE_ALIAS_RACE
1221 static inline void dentry_race_lock(void) {}
1222 static inline void dentry_race_unlock(void) {}
1226 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
1227 static DEFINE_MUTEX(dentry_race_sem);
1229 static DECLARE_MUTEX(dentry_race_sem);
1233 dentry_race_lock(void)
1235 mutex_lock(&dentry_race_sem);
1238 dentry_race_unlock(void)
1240 mutex_unlock(&dentry_race_sem);
1243 /* Leave some trace that this code is enabled; otherwise it's pretty hard to
1245 static __attribute__((used)) const char dentry_race_marker[] = "d_splice_alias race workaround enabled";
1248 check_dentry_race(struct dentry *dp)
1252 /* In Linux, before commit 4919c5e45a91b5db5a41695fe0357fbdff0d5767,
1253 * d_splice_alias can momentarily hash a dentry before it's fully
1254 * populated. This only happens for a moment, since it's unhashed again
1255 * right after (in d_move), but this can make the dentry be found by
1256 * __d_lookup, and then given to us.
1258 * So check if the dentry is unhashed; if it is, then the dentry is not
1259 * valid. We lock dentry_race_lock() to ensure that d_splice_alias is
1260 * no longer running. Locking d_lock is required to check the dentry's
1261 * flags, so lock that, too.
1264 spin_lock(&dp->d_lock);
1265 if (d_unhashed(dp)) {
1268 spin_unlock(&dp->d_lock);
1269 dentry_race_unlock();
1273 #endif /* D_SPLICE_ALIAS_RACE */
1275 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1276 * In kernels 2.2.10 and above, we are passed an additional flags var which
1277 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1278 * we are advised to follow the entry if it is a link or to make sure that
1279 * it is a directory. But since the kernel itself checks these possibilities
1280 * later on, we shouldn't have to do it until later. Perhaps in the future..
1282 * The code here assumes that on entry the global lock is not held
1285 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1286 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1287 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1288 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1290 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1293 cred_t *credp = NULL;
1294 struct vcache *vcp, *pvcp, *tvc = NULL;
1295 struct dentry *parent;
1297 struct afs_fakestat_state fakestate;
1299 afs_uint32 parent_dv;
1303 /* We don't support RCU path walking */
1304 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1305 if (flags & LOOKUP_RCU)
1307 if (nd->flags & LOOKUP_RCU)
1312 #ifdef D_SPLICE_ALIAS_RACE
1313 if (check_dentry_race(dp)) {
1320 afs_InitFakeStat(&fakestate);
1323 vcp = VTOAFS(dp->d_inode);
1325 if (vcp == afs_globalVp)
1328 if (vcp->mvstat == AFS_MVSTAT_MTPT) {
1329 if (vcp->mvid.target_root && (vcp->f.states & CMValid)) {
1330 int tryEvalOnly = 0;
1331 struct vrequest *treq = NULL;
1335 code = afs_CreateReq(&treq, credp);
1339 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1343 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1345 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1346 afs_DestroyReq(treq);
1350 if (tryEvalOnly && vcp->mvstat == AFS_MVSTAT_MTPT) {
1351 /* a mount point, not yet replaced by its directory */
1355 } else if (vcp->mvstat == AFS_MVSTAT_ROOT && *dp->d_name.name != '/') {
1356 osi_Assert(vcp->mvid.parent != NULL);
1359 parent = dget_parent(dp);
1360 pvcp = VTOAFS(parent->d_inode);
1361 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1363 /* If the parent's DataVersion has changed or the vnode
1364 * is longer valid, we need to do a full lookup. VerifyVCache
1365 * isn't enough since the vnode may have been renamed.
1368 if (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) {
1369 struct vattr *vattr = NULL;
1371 if (credp == NULL) {
1374 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1375 code = filter_enoent(code);
1376 if (code == ENOENT) {
1377 /* ENOENT is not an error here. */
1379 osi_Assert(tvc == NULL);
1383 /* We couldn't perform the lookup, so we don't know if the
1384 * dentry is valid or not. */
1390 /* 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 know this dentry is
1405 * _not_ okay. Force it to be unhashed, since the given name
1406 * doesn't point to this file anymore.
1413 code = afs_CreateAttr(&vattr);
1419 if (afs_getattr(vcp, vattr, credp)) {
1421 afs_DestroyAttr(vattr);
1426 vattr2inode(AFSTOV(vcp), vattr);
1427 dp->d_time = parent_dv;
1429 afs_DestroyAttr(vattr);
1432 /* should we always update the attributes at this point? */
1433 /* unlikely--the vcache entry hasn't changed */
1439 /* 'dp' represents a cached negative lookup. */
1441 parent = dget_parent(dp);
1442 pvcp = VTOAFS(parent->d_inode);
1443 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1445 if (parent_dv > dp->d_time || !(pvcp->f.states & CStatd)
1446 || afs_IsDynroot(pvcp)) {
1462 #ifndef D_INVALIDATE_IS_VOID
1463 /* When (v3.18) d_invalidate was converted to void, it also started
1464 * being called automatically from revalidate, and automatically
1466 * - shrink_dcache_parent
1467 * - automatic detach of submounts
1469 * Therefore, after that point, OpenAFS revalidate logic no longer needs
1470 * to do any of those things itself for invalid dentry structs. We only need
1471 * to tell VFS it's invalid (by returning 0), and VFS will handle the rest.
1473 if (have_submounts(dp))
1481 afs_PutFakeStat(&fakestate);
1486 #ifdef ERRORS_FROM_D_REVALIDATE
1489 * If code is nonzero, we don't know whether this dentry is valid or
1490 * not; we couldn't successfully perform the relevant lookup in order
1491 * to tell. So we must not return 'valid' (1) or 'not valid' (0); we
1492 * need to return an error (e.g. -EIO).
1498 #ifndef D_INVALIDATE_IS_VOID
1501 * If we had a negative lookup for the name we want to forcibly
1502 * unhash the dentry.
1503 * Otherwise use d_invalidate which will not unhash it if still in use.
1506 shrink_dcache_parent(dp);
1518 #ifdef ERRORS_FROM_D_REVALIDATE
1522 /* We can't return an error, so default to saying the dentry is invalid. */
1528 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1530 struct vcache *vcp = VTOAFS(ip);
1531 int haveGlock = ISAFS_GLOCK();
1537 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1538 (void) afs_InactiveVCache(vcp, NULL);
1545 afs_linux_clear_nfsfs_renamed(dp);
1551 #if defined(DOP_D_DELETE_TAKES_CONST)
1552 afs_dentry_delete(const struct dentry *dp)
1554 afs_dentry_delete(struct dentry *dp)
1557 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1558 return 1; /* bad inode? */
1563 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1564 static struct vfsmount *
1565 afs_dentry_automount(afs_linux_path_t *path)
1567 struct dentry *target;
1570 * Avoid symlink resolution limits when resolving; we cannot contribute to
1571 * an infinite symlink loop.
1573 * On newer kernels the field has moved to the private nameidata structure
1574 * so we can't adjust it here. This may cause ELOOP when using a path with
1575 * 40 or more directories that are not already in the dentry cache.
1577 #if defined(STRUCT_TASK_STRUCT_HAS_TOTAL_LINK_COUNT)
1578 current->total_link_count--;
1581 target = canonical_dentry(path->dentry->d_inode);
1583 if (target == path->dentry) {
1590 path->dentry = target;
1593 spin_lock(&path->dentry->d_lock);
1594 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1595 spin_unlock(&path->dentry->d_lock);
1600 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1602 struct dentry_operations afs_dentry_operations = {
1603 .d_revalidate = afs_linux_dentry_revalidate,
1604 .d_delete = afs_dentry_delete,
1605 .d_iput = afs_dentry_iput,
1606 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1607 .d_automount = afs_dentry_automount,
1608 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1611 /**********************************************************************
1612 * AFS Linux inode operations
1613 **********************************************************************/
1617 * Merely need to set enough of vattr to get us through the create. Note
1618 * that the higher level code (open_namei) will take care of any tuncation
1619 * explicitly. Exclusive open is also taken care of in open_namei.
1621 * name is in kernel space at this point.
1624 #if defined(IOP_CREATE_TAKES_BOOL)
1625 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1627 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1628 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1629 struct nameidata *nd)
1630 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1631 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1632 struct nameidata *nd)
1634 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1637 struct vattr *vattr = NULL;
1638 cred_t *credp = crref();
1639 const char *name = dp->d_name.name;
1645 code = afs_CreateAttr(&vattr);
1649 vattr->va_mode = mode;
1650 vattr->va_type = mode & S_IFMT;
1652 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1656 struct inode *ip = AFSTOV(vcp);
1658 afs_getattr(vcp, vattr, credp);
1659 afs_fill_inode(ip, vattr);
1660 insert_inode_hash(ip);
1661 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1662 dp->d_op = &afs_dentry_operations;
1664 dp->d_time = parent_vcache_dv(dip, credp);
1665 d_instantiate(dp, ip);
1668 afs_DestroyAttr(vattr);
1674 return afs_convert_code(code);
1677 /* afs_linux_lookup */
1678 static struct dentry *
1679 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1680 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1682 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1683 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1684 struct nameidata *nd)
1686 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1689 cred_t *credp = crref();
1690 struct vcache *vcp = NULL;
1691 const char *comp = dp->d_name.name;
1692 struct inode *ip = NULL;
1693 struct dentry *newdp = NULL;
1698 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1699 code = filter_enoent(code);
1700 if (code == ENOENT) {
1701 /* It's ok for the file to not be found. That's noted by the caller by
1702 * seeing that the dp->d_inode field is NULL (set by d_splice_alias or
1705 osi_Assert(vcp == NULL);
1713 struct vattr *vattr = NULL;
1714 struct vcache *parent_vc = VTOAFS(dip);
1716 if (parent_vc == vcp) {
1717 /* This is possible if the parent dir is a mountpoint to a volume,
1718 * and the dir entry we looked up is a mountpoint to the same
1719 * volume. Linux cannot cope with this, so return an error instead
1720 * of risking a deadlock or panic. */
1727 code = afs_CreateAttr(&vattr);
1735 afs_getattr(vcp, vattr, credp);
1736 afs_fill_inode(ip, vattr);
1737 if (hlist_unhashed(&ip->i_hash))
1738 insert_inode_hash(ip);
1740 afs_DestroyAttr(vattr);
1742 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1743 dp->d_op = &afs_dentry_operations;
1745 dp->d_time = parent_vcache_dv(dip, credp);
1749 if (ip && S_ISDIR(ip->i_mode)) {
1750 d_prune_aliases(ip);
1752 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1753 /* Only needed if this is a volume root */
1754 if (vcp->mvstat == 2)
1755 ip->i_flags |= S_AUTOMOUNT;
1759 * Take an extra reference so the inode doesn't go away if
1760 * d_splice_alias drops our reference on error.
1763 #ifdef HAVE_LINUX_IHOLD
1770 newdp = d_splice_alias(ip, dp);
1771 dentry_race_unlock();
1776 if (IS_ERR(newdp)) {
1777 /* d_splice_alias can return an error (EIO) if there is an existing
1778 * connected directory alias for this dentry. Add our dentry manually
1779 * ourselves if this happens. */
1782 #if defined(D_SPLICE_ALIAS_LEAK_ON_ERROR)
1783 /* Depending on the kernel version, d_splice_alias may or may not drop
1784 * the inode reference on error. If it didn't, do it here. */
1793 return ERR_PTR(afs_convert_code(code));
1801 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1804 cred_t *credp = crref();
1805 const char *name = newdp->d_name.name;
1806 struct inode *oldip = olddp->d_inode;
1808 /* If afs_link returned the vnode, we could instantiate the
1809 * dentry. Since it's not, we drop this one and do a new lookup.
1814 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1818 return afs_convert_code(code);
1821 /* We have to have a Linux specific sillyrename function, because we
1822 * also have to keep the dcache up to date when we're doing a silly
1823 * rename - so we don't want the generic vnodeops doing this behind our
1828 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1831 struct vcache *tvc = VTOAFS(dentry->d_inode);
1832 struct dentry *__dp = NULL;
1833 char *__name = NULL;
1836 if (afs_linux_nfsfs_renamed(dentry))
1844 osi_FreeSmallSpace(__name);
1845 __name = afs_newname();
1848 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1851 osi_FreeSmallSpace(__name);
1854 } while (__dp->d_inode != NULL);
1857 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1858 VTOAFS(dir), (char *)__dp->d_name.name,
1861 tvc->mvid.silly_name = __name;
1864 crfree(tvc->uncred);
1866 tvc->uncred = credp;
1867 tvc->f.states |= CUnlinked;
1868 afs_linux_set_nfsfs_renamed(dentry);
1870 __dp->d_time = 0; /* force to revalidate */
1871 d_move(dentry, __dp);
1873 osi_FreeSmallSpace(__name);
1884 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1887 cred_t *credp = crref();
1888 const char *name = dp->d_name.name;
1889 struct vcache *tvc = VTOAFS(dp->d_inode);
1891 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1892 && !(tvc->f.states & CUnlinked)) {
1894 code = afs_linux_sillyrename(dip, dp, credp);
1897 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1904 return afs_convert_code(code);
1909 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1912 cred_t *credp = crref();
1913 struct vattr *vattr = NULL;
1914 const char *name = dp->d_name.name;
1916 /* If afs_symlink returned the vnode, we could instantiate the
1917 * dentry. Since it's not, we drop this one and do a new lookup.
1922 code = afs_CreateAttr(&vattr);
1927 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1929 afs_DestroyAttr(vattr);
1934 return afs_convert_code(code);
1938 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1939 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1941 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1945 cred_t *credp = crref();
1946 struct vcache *tvcp = NULL;
1947 struct vattr *vattr = NULL;
1948 const char *name = dp->d_name.name;
1951 code = afs_CreateAttr(&vattr);
1956 vattr->va_mask = ATTR_MODE;
1957 vattr->va_mode = mode;
1959 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1962 struct inode *ip = AFSTOV(tvcp);
1964 afs_getattr(tvcp, vattr, credp);
1965 afs_fill_inode(ip, vattr);
1967 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1968 dp->d_op = &afs_dentry_operations;
1970 dp->d_time = parent_vcache_dv(dip, credp);
1971 d_instantiate(dp, ip);
1973 afs_DestroyAttr(vattr);
1979 return afs_convert_code(code);
1983 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1986 cred_t *credp = crref();
1987 const char *name = dp->d_name.name;
1989 /* locking kernel conflicts with glock? */
1992 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1995 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1996 * that failed because a directory is not empty. So, we map
1997 * EEXIST to ENOTEMPTY on linux.
1999 if (code == EEXIST) {
2008 return afs_convert_code(code);
2013 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
2014 struct inode *newip, struct dentry *newdp
2015 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
2016 , unsigned int flags
2021 cred_t *credp = crref();
2022 const char *oldname = olddp->d_name.name;
2023 const char *newname = newdp->d_name.name;
2024 struct dentry *rehash = NULL;
2026 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
2028 return -EINVAL; /* no support for new flags yet */
2031 /* Prevent any new references during rename operation. */
2033 if (!d_unhashed(newdp)) {
2038 afs_maybe_shrink_dcache(olddp);
2041 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
2045 olddp->d_time = 0; /* force to revalidate */
2051 return afs_convert_code(code);
2055 /* afs_linux_ireadlink
2056 * Internal readlink which can return link contents to user or kernel space.
2057 * Note that the buffer is NOT supposed to be null-terminated.
2060 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
2063 cred_t *credp = crref();
2067 memset(&tuio, 0, sizeof(tuio));
2068 memset(&iov, 0, sizeof(iov));
2070 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
2071 code = afs_readlink(VTOAFS(ip), &tuio, credp);
2075 return maxlen - tuio.uio_resid;
2077 return afs_convert_code(code);
2080 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2081 /* afs_linux_readlink
2082 * Fill target (which is in user space) with contents of symlink.
2085 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
2088 struct inode *ip = dp->d_inode;
2091 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
2097 /* afs_linux_follow_link
2098 * a file system dependent link following routine.
2100 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2101 static const char *afs_linux_follow_link(struct dentry *dentry, void **link_data)
2103 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
2109 name = kmalloc(PATH_MAX, GFP_NOFS);
2111 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2112 return ERR_PTR(-EIO);
2119 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
2123 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2124 return ERR_PTR(code);
2131 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2132 return *link_data = name;
2134 nd_set_link(nd, name);
2139 #if defined(HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA)
2141 afs_linux_put_link(struct inode *inode, void *link_data)
2143 char *name = link_data;
2145 if (name && !IS_ERR(name))
2150 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
2152 char *name = nd_get_link(nd);
2154 if (name && !IS_ERR(name))
2157 #endif /* HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA */
2159 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2161 /* Populate a page by filling it from the cache file pointed at by cachefp
2162 * (which contains indicated chunk)
2163 * If task is NULL, the page copy occurs syncronously, and the routine
2164 * returns with page still locked. If task is non-NULL, then page copies
2165 * may occur in the background, and the page will be unlocked when it is
2166 * ready for use. Note that if task is non-NULL and we encounter an error
2167 * before we start the background copy, we MUST unlock 'page' before we return.
2170 afs_linux_read_cache(struct file *cachefp, struct page *page,
2171 int chunk, struct afs_lru_pages *alrupages,
2172 struct afs_pagecopy_task *task) {
2173 loff_t offset = page_offset(page);
2174 struct inode *cacheinode = cachefp->f_dentry->d_inode;
2175 struct page *newpage, *cachepage;
2176 struct address_space *cachemapping;
2180 cachemapping = cacheinode->i_mapping;
2184 /* If we're trying to read a page that's past the end of the disk
2185 * cache file, then just return a zeroed page */
2186 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
2187 zero_user_segment(page, 0, PAGE_SIZE);
2188 SetPageUptodate(page);
2194 /* From our offset, we now need to work out which page in the disk
2195 * file it corresponds to. This will be fun ... */
2196 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_SHIFT;
2198 while (cachepage == NULL) {
2199 cachepage = find_get_page(cachemapping, pageindex);
2202 newpage = page_cache_alloc(cachemapping);
2208 code = add_to_page_cache(newpage, cachemapping,
2209 pageindex, GFP_KERNEL);
2211 cachepage = newpage;
2213 afs_lru_cache_add(alrupages, cachepage);
2217 if (code != -EEXIST)
2221 lock_page(cachepage);
2225 if (!PageUptodate(cachepage)) {
2226 ClearPageError(cachepage);
2227 /* Note that ->readpage always handles unlocking the given page, even
2228 * when an error is returned. */
2229 code = cachemapping->a_ops->readpage(NULL, cachepage);
2230 if (!code && !task) {
2231 wait_on_page_locked(cachepage);
2234 unlock_page(cachepage);
2238 if (PageUptodate(cachepage)) {
2239 copy_highpage(page, cachepage);
2240 flush_dcache_page(page);
2241 SetPageUptodate(page);
2246 afs_pagecopy_queue_page(task, cachepage, page);
2258 put_page(cachepage);
2264 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2266 loff_t offset = page_offset(pp);
2267 struct inode *ip = FILE_INODE(fp);
2268 struct vcache *avc = VTOAFS(ip);
2270 struct file *cacheFp = NULL;
2273 struct afs_lru_pages lrupages;
2275 /* Not a UFS cache, don't do anything */
2276 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2279 /* No readpage (ex: tmpfs) , skip */
2280 if (cachefs_noreadpage)
2283 /* Can't do anything if the vcache isn't statd , or if the read
2284 * crosses a chunk boundary.
2286 if (!(avc->f.states & CStatd) ||
2287 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2291 ObtainWriteLock(&avc->lock, 911);
2293 /* XXX - See if hinting actually makes things faster !!! */
2295 /* See if we have a suitable entry already cached */
2299 /* We need to lock xdcache, then dcache, to handle situations where
2300 * the hint is on the free list. However, we can't safely do this
2301 * according to the locking hierarchy. So, use a non blocking lock.
2303 ObtainReadLock(&afs_xdcache);
2304 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2306 if (dcLocked && (tdc->index != NULLIDX)
2307 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2308 && tdc->f.chunk == AFS_CHUNK(offset)
2309 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2310 /* Bonus - the hint was correct */
2313 /* Only destroy the hint if its actually invalid, not if there's
2314 * just been a locking failure */
2316 ReleaseReadLock(&tdc->lock);
2323 ReleaseReadLock(&afs_xdcache);
2326 /* No hint, or hint is no longer valid - see if we can get something
2327 * directly from the dcache
2330 tdc = afs_FindDCache(avc, offset);
2333 ReleaseWriteLock(&avc->lock);
2338 ObtainReadLock(&tdc->lock);
2340 /* Is the dcache we've been given currently up to date */
2341 if (!afs_IsDCacheFresh(tdc, avc) ||
2342 (tdc->dflags & DFFetching))
2345 /* Update our hint for future abuse */
2348 /* Okay, so we've now got a cache file that is up to date */
2350 /* XXX - I suspect we should be locking the inodes before we use them! */
2352 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2353 if (cacheFp == NULL) {
2354 /* Problem getting the inode */
2358 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2359 cachefs_noreadpage = 1;
2364 afs_lru_cache_init(&lrupages);
2366 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupages, NULL);
2368 afs_lru_cache_finalize(&lrupages);
2370 filp_close(cacheFp, NULL);
2373 ReleaseReadLock(&tdc->lock);
2374 ReleaseWriteLock(&avc->lock);
2381 if (cacheFp != NULL) {
2382 filp_close(cacheFp, NULL);
2384 ReleaseWriteLock(&avc->lock);
2385 ReleaseReadLock(&tdc->lock);
2390 /* afs_linux_readpage
2392 * This function is split into two, because prepare_write/begin_write
2393 * require a readpage call which doesn't unlock the resulting page upon
2397 afs_linux_fillpage(struct file *fp, struct page *pp)
2402 struct iovec *iovecp;
2403 struct inode *ip = FILE_INODE(fp);
2404 afs_int32 cnt = page_count(pp);
2405 struct vcache *avc = VTOAFS(ip);
2406 afs_offs_t offset = page_offset(pp);
2410 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2420 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2421 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2423 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2428 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2429 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2430 99999); /* not a possible code value */
2432 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2434 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2435 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2437 AFS_DISCON_UNLOCK();
2440 /* XXX valid for no-cache also? Check last bits of files... :)
2441 * Cognate code goes in afs_NoCacheFetchProc. */
2442 if (auio->uio_resid) /* zero remainder of page */
2443 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2446 flush_dcache_page(pp);
2447 SetPageUptodate(pp);
2456 return afs_convert_code(code);
2460 afs_linux_prefetch(struct file *fp, struct page *pp)
2463 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2464 afs_offs_t offset = page_offset(pp);
2466 if (AFS_CHUNKOFFSET(offset) == 0) {
2468 struct vrequest *treq = NULL;
2473 code = afs_CreateReq(&treq, credp);
2474 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2475 tdc = afs_FindDCache(avc, offset);
2477 if (!(tdc->mflags & DFNextStarted))
2478 afs_PrefetchChunk(avc, tdc, credp, treq);
2481 ReleaseWriteLock(&avc->lock);
2483 afs_DestroyReq(treq);
2487 return afs_convert_code(code);
2492 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2493 struct list_head *page_list, unsigned num_pages)
2498 struct iovec* iovecp;
2499 struct nocache_read_request *ancr;
2501 struct afs_lru_pages lrupages;
2505 struct inode *ip = FILE_INODE(fp);
2506 struct vcache *avc = VTOAFS(ip);
2507 afs_int32 base_index = 0;
2508 afs_int32 page_count = 0;
2511 /* background thread must free: iovecp, auio, ancr */
2512 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2514 auio = osi_Alloc(sizeof(struct uio));
2515 auio->uio_iov = iovecp;
2516 auio->uio_iovcnt = num_pages;
2517 auio->uio_flag = UIO_READ;
2518 auio->uio_seg = AFS_UIOSYS;
2519 auio->uio_resid = num_pages * PAGE_SIZE;
2521 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2523 ancr->offset = auio->uio_offset;
2524 ancr->length = auio->uio_resid;
2526 afs_lru_cache_init(&lrupages);
2528 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2530 if(list_empty(page_list))
2533 pp = list_entry(page_list->prev, struct page, lru);
2534 /* If we allocate a page and don't remove it from page_list,
2535 * the page cache gets upset. */
2537 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2538 if(pp->index > isize) {
2545 offset = page_offset(pp);
2546 ancr->offset = auio->uio_offset = offset;
2547 base_index = pp->index;
2549 iovecp[page_ix].iov_len = PAGE_SIZE;
2550 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2551 if(base_index != pp->index) {
2555 iovecp[page_ix].iov_base = (void *) 0;
2557 ancr->length -= PAGE_SIZE;
2565 iovecp[page_ix].iov_base = (void *) 0;
2568 if(!PageLocked(pp)) {
2572 /* save the page for background map */
2573 iovecp[page_ix].iov_base = (void*) pp;
2575 /* and put it on the LRU cache */
2576 afs_lru_cache_add(&lrupages, pp);
2580 /* If there were useful pages in the page list, make sure all pages
2581 * are in the LRU cache, then schedule the read */
2583 afs_lru_cache_finalize(&lrupages);
2585 code = afs_ReadNoCache(avc, ancr, credp);
2588 /* If there is nothing for the background thread to handle,
2589 * it won't be freeing the things that we never gave it */
2590 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2591 osi_Free(auio, sizeof(struct uio));
2592 osi_Free(ancr, sizeof(struct nocache_read_request));
2594 /* we do not flush, release, or unmap pages--that will be
2595 * done for us by the background thread as each page comes in
2596 * from the fileserver */
2597 return afs_convert_code(code);
2602 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2604 cred_t *credp = NULL;
2606 struct iovec *iovecp;
2607 struct nocache_read_request *ancr;
2611 * Special case: if page is at or past end of file, just zero it and set
2614 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2615 zero_user_segment(pp, 0, PAGE_SIZE);
2616 SetPageUptodate(pp);
2623 /* receiver frees */
2624 auio = osi_Alloc(sizeof(struct uio));
2625 iovecp = osi_Alloc(sizeof(struct iovec));
2627 /* address can be NULL, because we overwrite it with 'pp', below */
2628 setup_uio(auio, iovecp, NULL, page_offset(pp),
2629 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2631 /* save the page for background map */
2632 get_page(pp); /* see above */
2633 auio->uio_iov->iov_base = (void*) pp;
2634 /* the background thread will free this */
2635 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2637 ancr->offset = page_offset(pp);
2638 ancr->length = PAGE_SIZE;
2641 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2644 return afs_convert_code(code);
2648 afs_linux_can_bypass(struct inode *ip) {
2650 switch(cache_bypass_strategy) {
2651 case NEVER_BYPASS_CACHE:
2653 case ALWAYS_BYPASS_CACHE:
2655 case LARGE_FILES_BYPASS_CACHE:
2656 if (i_size_read(ip) > cache_bypass_threshold)
2664 /* Check if a file is permitted to bypass the cache by policy, and modify
2665 * the cache bypass state recorded for that file */
2668 afs_linux_bypass_check(struct inode *ip) {
2671 int bypass = afs_linux_can_bypass(ip);
2674 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2682 afs_linux_readpage(struct file *fp, struct page *pp)
2686 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2687 code = afs_linux_bypass_readpage(fp, pp);
2689 code = afs_linux_fillpage(fp, pp);
2691 code = afs_linux_prefetch(fp, pp);
2698 /* Readpages reads a number of pages for a particular file. We use
2699 * this to optimise the reading, by limiting the number of times upon which
2700 * we have to lookup, lock and open vcaches and dcaches
2704 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2705 struct list_head *page_list, unsigned int num_pages)
2707 struct inode *inode = mapping->host;
2708 struct vcache *avc = VTOAFS(inode);
2710 struct file *cacheFp = NULL;
2712 unsigned int page_idx;
2714 struct afs_lru_pages lrupages;
2715 struct afs_pagecopy_task *task;
2717 if (afs_linux_bypass_check(inode))
2718 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2720 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2723 /* No readpage (ex: tmpfs) , skip */
2724 if (cachefs_noreadpage)
2728 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2733 ObtainWriteLock(&avc->lock, 912);
2736 task = afs_pagecopy_init_task();
2740 afs_lru_cache_init(&lrupages);
2742 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2743 struct page *page = list_entry(page_list->prev, struct page, lru);
2744 list_del(&page->lru);
2745 offset = page_offset(page);
2747 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2749 ReleaseReadLock(&tdc->lock);
2754 filp_close(cacheFp, NULL);
2761 if ((tdc = afs_FindDCache(avc, offset))) {
2762 ObtainReadLock(&tdc->lock);
2763 if (!afs_IsDCacheFresh(tdc, avc) ||
2764 (tdc->dflags & DFFetching)) {
2765 ReleaseReadLock(&tdc->lock);
2772 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2773 if (cacheFp == NULL) {
2774 /* Problem getting the inode */
2777 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2778 cachefs_noreadpage = 1;
2784 if (tdc && !add_to_page_cache(page, mapping, page->index,
2786 afs_lru_cache_add(&lrupages, page);
2788 /* Note that add_to_page_cache() locked 'page'.
2789 * afs_linux_read_cache() is guaranteed to handle unlocking it. */
2790 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupages, task);
2794 afs_lru_cache_finalize(&lrupages);
2798 filp_close(cacheFp, NULL);
2800 afs_pagecopy_put_task(task);
2804 ReleaseReadLock(&tdc->lock);
2808 ReleaseWriteLock(&avc->lock);
2813 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2816 afs_linux_prepare_writeback(struct vcache *avc) {
2818 struct pagewriter *pw;
2820 pid = MyPidxx2Pid(MyPidxx);
2821 /* Prevent recursion into the writeback code */
2822 spin_lock(&avc->pagewriter_lock);
2823 list_for_each_entry(pw, &avc->pagewriters, link) {
2824 if (pw->writer == pid) {
2825 spin_unlock(&avc->pagewriter_lock);
2826 return AOP_WRITEPAGE_ACTIVATE;
2829 spin_unlock(&avc->pagewriter_lock);
2831 /* Add ourselves to writer list */
2832 pw = osi_Alloc(sizeof(struct pagewriter));
2834 spin_lock(&avc->pagewriter_lock);
2835 list_add_tail(&pw->link, &avc->pagewriters);
2836 spin_unlock(&avc->pagewriter_lock);
2842 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2843 struct vrequest *treq = NULL;
2846 if (!afs_CreateReq(&treq, credp)) {
2847 code = afs_DoPartialWrite(avc, treq);
2848 afs_DestroyReq(treq);
2851 return afs_convert_code(code);
2855 afs_linux_complete_writeback(struct vcache *avc) {
2856 struct pagewriter *pw, *store;
2858 struct list_head tofree;
2860 INIT_LIST_HEAD(&tofree);
2861 pid = MyPidxx2Pid(MyPidxx);
2862 /* Remove ourselves from writer list */
2863 spin_lock(&avc->pagewriter_lock);
2864 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
2865 if (pw->writer == pid) {
2866 list_del(&pw->link);
2867 /* osi_Free may sleep so we need to defer it */
2868 list_add_tail(&pw->link, &tofree);
2871 spin_unlock(&avc->pagewriter_lock);
2872 list_for_each_entry_safe(pw, store, &tofree, link) {
2873 list_del(&pw->link);
2874 osi_Free(pw, sizeof(struct pagewriter));
2878 /* Writeback a given page syncronously. Called with no AFS locks held */
2880 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2881 unsigned long offset, unsigned int count,
2884 struct vcache *vcp = VTOAFS(ip);
2892 memset(&tuio, 0, sizeof(tuio));
2893 memset(&iovec, 0, sizeof(iovec));
2895 buffer = kmap(pp) + offset;
2896 base = page_offset(pp) + offset;
2899 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2900 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2901 ICL_TYPE_INT32, 99999);
2903 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2905 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2907 i_size_write(ip, vcp->f.m.Length);
2908 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2910 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2912 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2913 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2914 ICL_TYPE_INT32, code);
2923 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2924 unsigned long offset, unsigned int count)
2928 struct vcache *vcp = VTOAFS(ip);
2931 /* Catch recursive writeback. This occurs if the kernel decides
2932 * writeback is required whilst we are writing to the cache, or
2933 * flushing to the server. When we're running syncronously (as
2934 * opposed to from writepage) we can't actually do anything about
2935 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2938 ObtainWriteLock(&vcp->lock, 532);
2939 afs_linux_prepare_writeback(vcp);
2940 ReleaseWriteLock(&vcp->lock);
2944 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2947 ObtainWriteLock(&vcp->lock, 533);
2949 code1 = afs_linux_dopartialwrite(vcp, credp);
2950 afs_linux_complete_writeback(vcp);
2951 ReleaseWriteLock(&vcp->lock);
2962 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2963 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2965 afs_linux_writepage(struct page *pp)
2968 struct address_space *mapping = pp->mapping;
2969 struct inode *inode;
2972 unsigned int to = PAGE_SIZE;
2979 inode = mapping->host;
2980 vcp = VTOAFS(inode);
2981 isize = i_size_read(inode);
2983 /* Don't defeat an earlier truncate */
2984 if (page_offset(pp) > isize) {
2985 set_page_writeback(pp);
2991 ObtainWriteLock(&vcp->lock, 537);
2992 code = afs_linux_prepare_writeback(vcp);
2993 if (code == AOP_WRITEPAGE_ACTIVATE) {
2994 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2995 * to return with the page still locked */
2996 ReleaseWriteLock(&vcp->lock);
3001 /* Grab the creds structure currently held in the vnode, and
3002 * get a reference to it, in case it goes away ... */
3008 ReleaseWriteLock(&vcp->lock);
3011 set_page_writeback(pp);
3013 SetPageUptodate(pp);
3015 /* We can unlock the page here, because it's protected by the
3016 * page_writeback flag. This should make us less vulnerable to
3017 * deadlocking in afs_write and afs_DoPartialWrite
3021 /* If this is the final page, then just write the number of bytes that
3022 * are actually in it */
3023 if ((isize - page_offset(pp)) < to )
3024 to = isize - page_offset(pp);
3026 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
3029 ObtainWriteLock(&vcp->lock, 538);
3031 /* As much as we might like to ignore a file server error here,
3032 * and just try again when we close(), unfortunately StoreAllSegments
3033 * will invalidate our chunks if the server returns a permanent error,
3034 * so we need to at least try and get that error back to the user
3037 code1 = afs_linux_dopartialwrite(vcp, credp);
3039 afs_linux_complete_writeback(vcp);
3040 ReleaseWriteLock(&vcp->lock);
3045 end_page_writeback(pp);
3057 /* afs_linux_permission
3058 * Check access rights - returns error if can't check or permission denied.
3061 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3062 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
3063 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
3064 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
3066 afs_linux_permission(struct inode *ip, int mode)
3073 /* Check for RCU path walking */
3074 #if defined(IOP_PERMISSION_TAKES_FLAGS)
3075 if (flags & IPERM_FLAG_RCU)
3077 #elif defined(MAY_NOT_BLOCK)
3078 if (mode & MAY_NOT_BLOCK)
3084 if (mode & MAY_EXEC)
3086 if (mode & MAY_READ)
3088 if (mode & MAY_WRITE)
3090 code = afs_access(VTOAFS(ip), tmp, credp);
3094 return afs_convert_code(code);
3098 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
3102 struct inode *inode = FILE_INODE(file);
3103 loff_t pagebase = page_offset(page);
3105 if (i_size_read(inode) < (pagebase + offset))
3106 i_size_write(inode, pagebase + offset);
3108 if (PageChecked(page)) {
3109 SetPageUptodate(page);
3110 ClearPageChecked(page);
3113 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
3119 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
3123 /* http://kerneltrap.org/node/4941 details the expected behaviour of
3124 * prepare_write. Essentially, if the page exists within the file,
3125 * and is not being fully written, then we should populate it.
3128 if (!PageUptodate(page)) {
3129 loff_t pagebase = page_offset(page);
3130 loff_t isize = i_size_read(page->mapping->host);
3132 /* Is the location we are writing to beyond the end of the file? */
3133 if (pagebase >= isize ||
3134 ((from == 0) && (pagebase + to) >= isize)) {
3135 zero_user_segments(page, 0, from, to, PAGE_SIZE);
3136 SetPageChecked(page);
3137 /* Are we we writing a full page */
3138 } else if (from == 0 && to == PAGE_SIZE) {
3139 SetPageChecked(page);
3140 /* Is the page readable, if it's wronly, we don't care, because we're
3141 * not actually going to read from it ... */
3142 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
3143 /* We don't care if fillpage fails, because if it does the page
3144 * won't be marked as up to date
3146 afs_linux_fillpage(file, page);
3152 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3154 afs_linux_write_end(struct file *file, struct address_space *mapping,
3155 loff_t pos, unsigned len, unsigned copied,
3156 struct page *page, void *fsdata)
3159 unsigned int from = pos & (PAGE_SIZE - 1);
3161 code = afs_linux_commit_write(file, page, from, from + copied);
3169 afs_linux_write_begin(struct file *file, struct address_space *mapping,
3170 loff_t pos, unsigned len, unsigned flags,
3171 struct page **pagep, void **fsdata)
3174 pgoff_t index = pos >> PAGE_SHIFT;
3175 unsigned int from = pos & (PAGE_SIZE - 1);
3178 page = grab_cache_page_write_begin(mapping, index, flags);
3185 code = afs_linux_prepare_write(file, page, from, from + len);
3195 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3197 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
3199 struct dentry **dpp;
3200 struct dentry *target;
3202 if (current->total_link_count > 0) {
3203 /* avoid symlink resolution limits when resolving; we cannot contribute to
3204 * an infinite symlink loop */
3205 /* only do this for follow_link when total_link_count is positive to be
3206 * on the safe side; there is at least one code path in the Linux
3207 * kernel where it seems like it may be possible to get here without
3208 * total_link_count getting incremented. it is not clear on how that
3209 * path is actually reached, but guard against it just to be safe */
3210 current->total_link_count--;
3213 target = canonical_dentry(dentry->d_inode);
3215 # ifdef STRUCT_NAMEIDATA_HAS_PATH
3216 dpp = &nd->path.dentry;
3226 *dpp = dget(dentry);
3229 nd->last_type = LAST_BIND;
3233 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
3236 static struct inode_operations afs_file_iops = {
3237 .permission = afs_linux_permission,
3238 .getattr = afs_linux_getattr,
3239 .setattr = afs_notify_change,
3242 static struct address_space_operations afs_file_aops = {
3243 .readpage = afs_linux_readpage,
3244 .readpages = afs_linux_readpages,
3245 .writepage = afs_linux_writepage,
3246 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3247 .write_begin = afs_linux_write_begin,
3248 .write_end = afs_linux_write_end,
3250 .commit_write = afs_linux_commit_write,
3251 .prepare_write = afs_linux_prepare_write,
3256 /* Separate ops vector for directories. Linux 2.2 tests type of inode
3257 * by what sort of operation is allowed.....
3260 static struct inode_operations afs_dir_iops = {
3261 .setattr = afs_notify_change,
3262 .create = afs_linux_create,
3263 .lookup = afs_linux_lookup,
3264 .link = afs_linux_link,
3265 .unlink = afs_linux_unlink,
3266 .symlink = afs_linux_symlink,
3267 .mkdir = afs_linux_mkdir,
3268 .rmdir = afs_linux_rmdir,
3269 .rename = afs_linux_rename,
3270 .getattr = afs_linux_getattr,
3271 .permission = afs_linux_permission,
3272 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3273 .follow_link = afs_linux_dir_follow_link,
3277 /* We really need a separate symlink set of ops, since do_follow_link()
3278 * determines if it _is_ a link by checking if the follow_link op is set.
3280 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3282 afs_symlink_filler(struct file *file, struct page *page)
3284 struct inode *ip = (struct inode *)page->mapping->host;
3285 char *p = (char *)kmap(page);
3289 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3294 p[code] = '\0'; /* null terminate? */
3296 SetPageUptodate(page);
3308 static struct address_space_operations afs_symlink_aops = {
3309 .readpage = afs_symlink_filler
3311 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3313 static struct inode_operations afs_symlink_iops = {
3314 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3315 .readlink = page_readlink,
3316 # if defined(HAVE_LINUX_PAGE_GET_LINK)
3317 .get_link = page_get_link,
3318 # elif defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3319 .follow_link = page_follow_link,
3321 .follow_link = page_follow_link_light,
3322 .put_link = page_put_link,
3324 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3325 .readlink = afs_linux_readlink,
3326 .follow_link = afs_linux_follow_link,
3327 .put_link = afs_linux_put_link,
3328 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3329 .setattr = afs_notify_change,
3333 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3336 vattr2inode(ip, vattr);
3338 #ifdef STRUCT_ADDRESS_SPACE_HAS_BACKING_DEV_INFO
3339 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3341 /* Reset ops if symlink or directory. */
3342 if (S_ISREG(ip->i_mode)) {
3343 ip->i_op = &afs_file_iops;
3344 ip->i_fop = &afs_file_fops;
3345 ip->i_data.a_ops = &afs_file_aops;
3347 } else if (S_ISDIR(ip->i_mode)) {
3348 ip->i_op = &afs_dir_iops;
3349 ip->i_fop = &afs_dir_fops;
3351 } else if (S_ISLNK(ip->i_mode)) {
3352 ip->i_op = &afs_symlink_iops;
3353 #if defined(HAVE_LINUX_INODE_NOHIGHMEM)
3354 inode_nohighmem(ip);
3356 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3357 ip->i_data.a_ops = &afs_symlink_aops;
3358 ip->i_mapping = &ip->i_data;