2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
29 #ifdef HAVE_MM_INLINE_H
30 #include <linux/mm_inline.h>
32 #include <linux/pagemap.h>
33 #include <linux/writeback.h>
34 #include <linux/pagevec.h>
35 #include <linux/aio.h>
37 #include "afs/afs_bypasscache.h"
39 #include "osi_compat.h"
40 #include "osi_pagecopy.h"
42 #ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
43 #define __pagevec_lru_add_file __pagevec_lru_add
47 #define MAX_ERRNO 1000L
50 #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 int cachefs_noreadpage = 0;
58 extern struct backing_dev_info *afs_backing_dev_info;
60 extern struct vcache *afs_globalVp;
62 /* This function converts a positive error code from AFS into a negative
63 * code suitable for passing into the Linux VFS layer. It checks that the
64 * error code is within the permissable bounds for the ERR_PTR mechanism.
66 * _All_ error codes which come from the AFS layer should be passed through
67 * this function before being returned to the kernel.
71 afs_convert_code(int code) {
72 if ((code >= 0) && (code <= MAX_ERRNO))
78 /* Linux doesn't require a credp for many functions, and crref is an expensive
79 * operation. This helper function avoids obtaining it for VerifyVCache calls
83 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
85 struct vrequest *treq = NULL;
88 if (avc->f.states & CStatd) {
96 code = afs_CreateReq(&treq, credp);
98 code = afs_VerifyVCache2(avc, treq);
107 return afs_convert_code(code);
110 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
111 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
113 afs_linux_read_iter(struct kiocb *iocb, struct iov_iter *iter)
114 # elif defined(LINUX_HAS_NONVECTOR_AIO)
116 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
120 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
121 unsigned long bufsize, loff_t pos)
124 struct file *fp = iocb->ki_filp;
126 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
127 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
128 loff_t pos = iocb->ki_pos;
129 unsigned long bufsize = iter->nr_segs;
134 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
135 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
136 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
137 code = afs_linux_VerifyVCache(vcp, NULL);
140 /* Linux's FlushPages implementation doesn't ever use credp,
141 * so we optimise by not using it */
142 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
144 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
145 code = generic_file_read_iter(iocb, iter);
147 code = generic_file_aio_read(iocb, buf, bufsize, pos);
152 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
153 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
154 (afs_int32)bufsize, ICL_TYPE_INT32, code);
160 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
163 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
166 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
167 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
169 code = afs_linux_VerifyVCache(vcp, NULL);
172 /* Linux's FlushPages implementation doesn't ever use credp,
173 * so we optimise by not using it */
174 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
176 code = do_sync_read(fp, buf, count, offp);
180 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
181 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
189 /* Now we have integrated VM for writes as well as reads. the generic write operations
190 * also take care of re-positioning the pointer if file is open in append
191 * mode. Call fake open/close to ensure we do writes of core dumps.
193 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
194 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
196 afs_linux_write_iter(struct kiocb *iocb, struct iov_iter *iter)
197 # elif defined(LINUX_HAS_NONVECTOR_AIO)
199 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
203 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
204 unsigned long bufsize, loff_t pos)
208 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
210 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
211 loff_t pos = iocb->ki_pos;
212 unsigned long bufsize = iter->nr_segs;
217 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
218 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
219 (afs_int32)bufsize, ICL_TYPE_INT32,
220 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
222 code = afs_linux_VerifyVCache(vcp, &credp);
224 ObtainWriteLock(&vcp->lock, 529);
226 ReleaseWriteLock(&vcp->lock);
229 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
230 code = generic_file_write_iter(iocb, iter);
232 code = generic_file_aio_write(iocb, buf, bufsize, pos);
237 ObtainWriteLock(&vcp->lock, 530);
239 if (vcp->execsOrWriters == 1 && !credp)
242 afs_FakeClose(vcp, credp);
243 ReleaseWriteLock(&vcp->lock);
245 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
246 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
247 (afs_int32)bufsize, ICL_TYPE_INT32, code);
256 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
259 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
264 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
265 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
266 (fp->f_flags & O_APPEND) ? 99998 : 99999);
268 code = afs_linux_VerifyVCache(vcp, &credp);
270 ObtainWriteLock(&vcp->lock, 529);
272 ReleaseWriteLock(&vcp->lock);
275 code = do_sync_write(fp, buf, count, offp);
279 ObtainWriteLock(&vcp->lock, 530);
281 if (vcp->execsOrWriters == 1 && !credp)
284 afs_FakeClose(vcp, credp);
285 ReleaseWriteLock(&vcp->lock);
287 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
288 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
298 extern int BlobScan(struct dcache * afile, afs_int32 ablob, afs_int32 *ablobOut);
300 /* This is a complete rewrite of afs_readdir, since we can make use of
301 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
302 * handling and use of bulkstats will need to be reflected here as well.
305 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
306 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
308 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
311 struct vcache *avc = VTOAFS(FILE_INODE(fp));
312 struct vrequest *treq = NULL;
318 struct DirBuffer entry;
321 afs_size_t origOffset, tlen;
322 cred_t *credp = crref();
323 struct afs_fakestat_state fakestat;
326 AFS_STATCNT(afs_readdir);
328 code = afs_convert_code(afs_CreateReq(&treq, credp));
333 afs_InitFakeStat(&fakestat);
334 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, treq));
338 /* update the cache entry */
340 code = afs_convert_code(afs_VerifyVCache2(avc, treq));
344 /* get a reference to the entire directory */
345 tdc = afs_GetDCache(avc, (afs_size_t) 0, treq, &origOffset, &tlen, 1);
351 ObtainWriteLock(&avc->lock, 811);
352 ObtainReadLock(&tdc->lock);
354 * Make sure that the data in the cache is current. There are two
355 * cases we need to worry about:
356 * 1. The cache data is being fetched by another process.
357 * 2. The cache data is no longer valid
359 while ((avc->f.states & CStatd)
360 && (tdc->dflags & DFFetching)
361 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
362 ReleaseReadLock(&tdc->lock);
363 ReleaseWriteLock(&avc->lock);
364 afs_osi_Sleep(&tdc->validPos);
365 ObtainWriteLock(&avc->lock, 812);
366 ObtainReadLock(&tdc->lock);
368 if (!(avc->f.states & CStatd)
369 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
370 ReleaseReadLock(&tdc->lock);
371 ReleaseWriteLock(&avc->lock);
376 /* Set the readdir-in-progress flag, and downgrade the lock
377 * to shared so others will be able to acquire a read lock.
379 avc->f.states |= CReadDir;
380 avc->dcreaddir = tdc;
381 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
382 ConvertWToSLock(&avc->lock);
384 /* Fill in until we get an error or we're done. This implementation
385 * takes an offset in units of blobs, rather than bytes.
388 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
391 offset = (int) fp->f_pos;
394 code = BlobScan(tdc, offset, &dirpos);
398 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
400 if (!(avc->f.states & CCorrupt)) {
401 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
402 afs_warn("afs: Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)\n",
403 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
404 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
405 tc ? tc->cellName : "",
406 (unsigned long)&tdc->f.inode, dirpos);
408 afs_PutCell(tc, READ_LOCK);
409 UpgradeSToWLock(&avc->lock, 814);
410 avc->f.states |= CCorrupt;
416 de = (struct DirEntry *)entry.data;
417 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
418 ntohl(de->fid.vnode));
419 len = strlen(de->name);
421 /* filldir returns -EINVAL when the buffer is full. */
423 unsigned int type = DT_UNKNOWN;
424 struct VenusFid afid;
427 afid.Cell = avc->f.fid.Cell;
428 afid.Fid.Volume = avc->f.fid.Fid.Volume;
429 afid.Fid.Vnode = ntohl(de->fid.vnode);
430 afid.Fid.Unique = ntohl(de->fid.vunique);
431 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
433 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
434 if (tvc->mvstat != AFS_MVSTAT_FILE) {
436 } else if (((tvc->f.states) & (CStatd | CTruth))) {
437 /* CTruth will be set if the object has
442 else if (vtype == VREG)
444 /* Don't do this until we're sure it can't be a mtpt */
445 /* else if (vtype == VLNK)
447 /* what other types does AFS support? */
449 /* clean up from afs_FindVCache */
453 * If this is NFS readdirplus, then the filler is going to
454 * call getattr on this inode, which will deadlock if we're
458 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
459 /* dir_emit returns a bool - true when it succeeds.
460 * Inverse the result to fit with how we check "code" */
461 code = !dir_emit(ctx, de->name, len, ino, type);
463 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
470 offset = dirpos + 1 + ((len + 16) >> 5);
472 /* If filldir didn't fill in the last one this is still pointing to that
478 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
479 ctx->pos = (loff_t) offset;
481 fp->f_pos = (loff_t) offset;
483 ReleaseReadLock(&tdc->lock);
485 UpgradeSToWLock(&avc->lock, 813);
486 avc->f.states &= ~CReadDir;
488 avc->readdir_pid = 0;
489 ReleaseSharedLock(&avc->lock);
492 afs_PutFakeStat(&fakestat);
493 afs_DestroyReq(treq);
500 /* in afs_pioctl.c */
501 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
504 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
505 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
507 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
514 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
516 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
520 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
521 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
522 vmap->vm_end - vmap->vm_start);
524 /* get a validated vcache entry */
525 code = afs_linux_VerifyVCache(vcp, NULL);
528 /* Linux's Flushpage implementation doesn't use credp, so optimise
529 * our code to not need to crref() it */
530 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
532 code = generic_file_mmap(fp, vmap);
535 vcp->f.states |= CMAPPED;
543 afs_linux_open(struct inode *ip, struct file *fp)
545 struct vcache *vcp = VTOAFS(ip);
546 cred_t *credp = crref();
550 code = afs_open(&vcp, fp->f_flags, credp);
554 return afs_convert_code(code);
558 afs_linux_release(struct inode *ip, struct file *fp)
560 struct vcache *vcp = VTOAFS(ip);
561 cred_t *credp = crref();
565 code = afs_close(vcp, fp->f_flags, credp);
566 ObtainWriteLock(&vcp->lock, 807);
571 ReleaseWriteLock(&vcp->lock);
575 return afs_convert_code(code);
579 #if defined(FOP_FSYNC_TAKES_DENTRY)
580 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
581 #elif defined(FOP_FSYNC_TAKES_RANGE)
582 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
584 afs_linux_fsync(struct file *fp, int datasync)
588 struct inode *ip = FILE_INODE(fp);
589 cred_t *credp = crref();
591 #if defined(FOP_FSYNC_TAKES_RANGE)
592 afs_linux_lock_inode(ip);
595 code = afs_fsync(VTOAFS(ip), credp);
597 #if defined(FOP_FSYNC_TAKES_RANGE)
598 afs_linux_unlock_inode(ip);
601 return afs_convert_code(code);
607 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
610 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
611 cred_t *credp = crref();
612 struct AFS_FLOCK flock;
614 /* Convert to a lock format afs_lockctl understands. */
615 memset(&flock, 0, sizeof(flock));
616 flock.l_type = flp->fl_type;
617 flock.l_pid = flp->fl_pid;
619 flock.l_start = flp->fl_start;
620 if (flp->fl_end == OFFSET_MAX)
621 flock.l_len = 0; /* Lock to end of file */
623 flock.l_len = flp->fl_end - flp->fl_start + 1;
625 /* Safe because there are no large files, yet */
626 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
627 if (cmd == F_GETLK64)
629 else if (cmd == F_SETLK64)
631 else if (cmd == F_SETLKW64)
633 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
636 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
639 if ((code == 0 || flp->fl_type == F_UNLCK) &&
640 (cmd == F_SETLK || cmd == F_SETLKW)) {
641 code = afs_posix_lock_file(fp, flp);
642 if (code && flp->fl_type != F_UNLCK) {
643 struct AFS_FLOCK flock2;
645 flock2.l_type = F_UNLCK;
647 afs_lockctl(vcp, &flock2, F_SETLK, credp);
651 /* If lockctl says there are no conflicting locks, then also check with the
652 * kernel, as lockctl knows nothing about byte range locks
654 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
655 afs_posix_test_lock(fp, flp);
656 /* If we found a lock in the kernel's structure, return it */
657 if (flp->fl_type != F_UNLCK) {
663 /* Convert flock back to Linux's file_lock */
664 flp->fl_type = flock.l_type;
665 flp->fl_pid = flock.l_pid;
666 flp->fl_start = flock.l_start;
667 if (flock.l_len == 0)
668 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
670 flp->fl_end = flock.l_start + flock.l_len - 1;
676 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
678 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
680 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
681 cred_t *credp = crref();
682 struct AFS_FLOCK flock;
683 /* Convert to a lock format afs_lockctl understands. */
684 memset(&flock, 0, sizeof(flock));
685 flock.l_type = flp->fl_type;
686 flock.l_pid = flp->fl_pid;
691 /* Safe because there are no large files, yet */
692 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
693 if (cmd == F_GETLK64)
695 else if (cmd == F_SETLK64)
697 else if (cmd == F_SETLKW64)
699 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
702 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
705 if ((code == 0 || flp->fl_type == F_UNLCK) &&
706 (cmd == F_SETLK || cmd == F_SETLKW)) {
707 flp->fl_flags &=~ FL_SLEEP;
708 code = flock_lock_file_wait(fp, flp);
709 if (code && flp->fl_type != F_UNLCK) {
710 struct AFS_FLOCK flock2;
712 flock2.l_type = F_UNLCK;
714 afs_lockctl(vcp, &flock2, F_SETLK, credp);
718 /* Convert flock back to Linux's file_lock */
719 flp->fl_type = flock.l_type;
720 flp->fl_pid = flock.l_pid;
728 * essentially the same as afs_fsync() but we need to get the return
729 * code for the sys_close() here, not afs_linux_release(), so call
730 * afs_StoreAllSegments() with AFS_LASTSTORE
733 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
734 afs_linux_flush(struct file *fp, fl_owner_t id)
736 afs_linux_flush(struct file *fp)
739 struct vrequest *treq = NULL;
747 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
755 vcp = VTOAFS(FILE_INODE(fp));
757 code = afs_CreateReq(&treq, credp);
760 /* If caching is bypassed for this file, or globally, just return 0 */
761 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
764 ObtainReadLock(&vcp->lock);
765 if (vcp->cachingStates & FCSBypass)
767 ReleaseReadLock(&vcp->lock);
770 /* future proof: don't rely on 0 return from afs_InitReq */
775 ObtainSharedLock(&vcp->lock, 535);
776 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
777 UpgradeSToWLock(&vcp->lock, 536);
778 if (!AFS_IS_DISCONNECTED) {
779 code = afs_StoreAllSegments(vcp,
781 AFS_SYNC | AFS_LASTSTORE);
783 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
785 ConvertWToSLock(&vcp->lock);
787 code = afs_CheckCode(code, treq, 54);
788 ReleaseSharedLock(&vcp->lock);
791 afs_DestroyReq(treq);
796 return afs_convert_code(code);
799 struct file_operations afs_dir_fops = {
800 .read = generic_read_dir,
801 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
802 .iterate = afs_linux_readdir,
804 .readdir = afs_linux_readdir,
806 #ifdef HAVE_UNLOCKED_IOCTL
807 .unlocked_ioctl = afs_unlocked_xioctl,
811 #ifdef HAVE_COMPAT_IOCTL
812 .compat_ioctl = afs_unlocked_xioctl,
814 .open = afs_linux_open,
815 .release = afs_linux_release,
816 .llseek = default_llseek,
817 #ifdef HAVE_LINUX_NOOP_FSYNC
820 .fsync = simple_sync_file,
824 struct file_operations afs_file_fops = {
825 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
826 .read_iter = afs_linux_read_iter,
827 .write_iter = afs_linux_write_iter,
828 # if !defined(HAVE_LINUX___VFS_WRITE) && !defined(HAVE_LINUX_KERNEL_WRITE)
829 .read = new_sync_read,
830 .write = new_sync_write,
832 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
833 .aio_read = afs_linux_aio_read,
834 .aio_write = afs_linux_aio_write,
835 .read = do_sync_read,
836 .write = do_sync_write,
838 .read = afs_linux_read,
839 .write = afs_linux_write,
841 #ifdef HAVE_UNLOCKED_IOCTL
842 .unlocked_ioctl = afs_unlocked_xioctl,
846 #ifdef HAVE_COMPAT_IOCTL
847 .compat_ioctl = afs_unlocked_xioctl,
849 .mmap = afs_linux_mmap,
850 .open = afs_linux_open,
851 .flush = afs_linux_flush,
852 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
853 .sendfile = generic_file_sendfile,
855 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE) && !defined(HAVE_LINUX_DEFAULT_FILE_SPLICE_READ)
856 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
857 .splice_write = iter_file_splice_write,
859 .splice_write = generic_file_splice_write,
861 .splice_read = generic_file_splice_read,
863 .release = afs_linux_release,
864 .fsync = afs_linux_fsync,
865 .lock = afs_linux_lock,
866 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
867 .flock = afs_linux_flock,
869 .llseek = default_llseek,
872 static struct dentry *
873 canonical_dentry(struct inode *ip)
875 struct vcache *vcp = VTOAFS(ip);
876 struct dentry *first = NULL, *ret = NULL, *cur;
877 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
878 struct hlist_node *p;
882 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
883 * otherwise, use the first dentry in ip->i_dentry.
884 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
886 /* note that vcp->target_link specifies which dentry to use, but we have
887 * no reference held on that dentry. so, we cannot use or dereference
888 * vcp->target_link itself, since it may have been freed. instead, we only
889 * use it to compare to pointers in the ip->i_dentry list. */
893 afs_d_alias_lock(ip);
895 #if defined(D_ALIAS_IS_HLIST)
896 # if defined(HLIST_ITERATOR_NO_NODE)
897 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
899 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
902 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
905 if (!vcp->target_link || cur == vcp->target_link) {
918 vcp->target_link = ret;
923 afs_d_alias_unlock(ip);
928 /**********************************************************************
929 * AFS Linux dentry operations
930 **********************************************************************/
932 /* afs_linux_revalidate
933 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
936 afs_linux_revalidate(struct dentry *dp)
938 struct vattr *vattr = NULL;
939 struct vcache *vcp = VTOAFS(dp->d_inode);
943 if (afs_shuttingdown != AFS_RUNNING)
948 code = afs_CreateAttr(&vattr);
953 /* This avoids the crref when we don't have to do it. Watch for
954 * changes in afs_getattr that don't get replicated here!
956 if (vcp->f.states & CStatd &&
957 (!afs_fakestat_enable || vcp->mvstat != AFS_MVSTAT_MTPT) &&
959 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
960 code = afs_CopyOutAttrs(vcp, vattr);
963 code = afs_getattr(vcp, vattr, credp);
968 afs_fill_inode(AFSTOV(vcp), vattr);
970 afs_DestroyAttr(vattr);
975 return afs_convert_code(code);
979 * Set iattr data into vattr. Assume vattr cleared before call.
982 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
984 vattrp->va_mask = iattrp->ia_valid;
985 if (iattrp->ia_valid & ATTR_MODE)
986 vattrp->va_mode = iattrp->ia_mode;
987 if (iattrp->ia_valid & ATTR_UID)
988 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
989 if (iattrp->ia_valid & ATTR_GID)
990 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
991 if (iattrp->ia_valid & ATTR_SIZE)
992 vattrp->va_size = iattrp->ia_size;
993 if (iattrp->ia_valid & ATTR_ATIME) {
994 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
995 vattrp->va_atime.tv_usec = 0;
997 if (iattrp->ia_valid & ATTR_MTIME) {
998 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
999 vattrp->va_mtime.tv_usec = 0;
1001 if (iattrp->ia_valid & ATTR_CTIME) {
1002 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1003 vattrp->va_ctime.tv_usec = 0;
1008 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1011 vattr2inode(struct inode *ip, struct vattr *vp)
1013 ip->i_ino = vp->va_nodeid;
1014 #ifdef HAVE_LINUX_SET_NLINK
1015 set_nlink(ip, vp->va_nlink);
1017 ip->i_nlink = vp->va_nlink;
1019 ip->i_blocks = vp->va_blocks;
1020 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1021 ip->i_blkbits = AFS_BLKBITS;
1023 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1024 ip->i_blksize = vp->va_blocksize;
1026 ip->i_rdev = vp->va_rdev;
1027 ip->i_mode = vp->va_mode;
1028 ip->i_uid = afs_make_kuid(vp->va_uid);
1029 ip->i_gid = afs_make_kgid(vp->va_gid);
1030 i_size_write(ip, vp->va_size);
1031 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1032 ip->i_atime.tv_nsec = 0;
1033 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1034 /* Set the mtime nanoseconds to the sysname generation number.
1035 * This convinces NFS clients that all directories have changed
1036 * any time the sysname list changes.
1038 ip->i_mtime.tv_nsec = afs_sysnamegen;
1039 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1040 ip->i_ctime.tv_nsec = 0;
1043 /* afs_notify_change
1044 * Linux version of setattr call. What to change is in the iattr struct.
1045 * We need to set bits in both the Linux inode as well as the vcache.
1048 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1050 struct vattr *vattr = NULL;
1051 cred_t *credp = crref();
1052 struct inode *ip = dp->d_inode;
1056 code = afs_CreateAttr(&vattr);
1061 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1063 code = afs_setattr(VTOAFS(ip), vattr, credp);
1065 afs_getattr(VTOAFS(ip), vattr, credp);
1066 vattr2inode(ip, vattr);
1068 afs_DestroyAttr(vattr);
1073 return afs_convert_code(code);
1076 #if defined(IOP_GETATTR_TAKES_PATH_STRUCT)
1078 afs_linux_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int sync_mode)
1080 int err = afs_linux_revalidate(path->dentry);
1082 generic_fillattr(path->dentry->d_inode, stat);
1088 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1090 int err = afs_linux_revalidate(dentry);
1092 generic_fillattr(dentry->d_inode, stat);
1099 parent_vcache_dv(struct inode *inode, cred_t *credp)
1102 struct vcache *pvcp;
1105 * If parent is a mount point and we are using fakestat, we may need
1106 * to look at the fake vcache entry instead of what the vfs is giving
1107 * us. The fake entry is the one with the useful DataVersion.
1109 pvcp = VTOAFS(inode);
1110 if (pvcp->mvstat == AFS_MVSTAT_MTPT && afs_fakestat_enable) {
1111 struct vrequest treq;
1112 struct afs_fakestat_state fakestate;
1118 afs_InitReq(&treq, credp);
1119 afs_InitFakeStat(&fakestate);
1120 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1123 afs_PutFakeStat(&fakestate);
1125 return hgetlo(pvcp->f.m.DataVersion);
1128 #ifndef D_SPLICE_ALIAS_RACE
1130 static inline void dentry_race_lock(void) {}
1131 static inline void dentry_race_unlock(void) {}
1135 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
1136 static DEFINE_MUTEX(dentry_race_sem);
1138 static DECLARE_MUTEX(dentry_race_sem);
1142 dentry_race_lock(void)
1144 mutex_lock(&dentry_race_sem);
1147 dentry_race_unlock(void)
1149 mutex_unlock(&dentry_race_sem);
1152 /* Leave some trace that this code is enabled; otherwise it's pretty hard to
1154 static __attribute__((used)) const char dentry_race_marker[] = "d_splice_alias race workaround enabled";
1157 check_dentry_race(struct dentry *dp)
1161 /* In Linux, before commit 4919c5e45a91b5db5a41695fe0357fbdff0d5767,
1162 * d_splice_alias can momentarily hash a dentry before it's fully
1163 * populated. This only happens for a moment, since it's unhashed again
1164 * right after (in d_move), but this can make the dentry be found by
1165 * __d_lookup, and then given to us.
1167 * So check if the dentry is unhashed; if it is, then the dentry is not
1168 * valid. We lock dentry_race_lock() to ensure that d_splice_alias is
1169 * no longer running. Locking d_lock is required to check the dentry's
1170 * flags, so lock that, too.
1173 spin_lock(&dp->d_lock);
1174 if (d_unhashed(dp)) {
1177 spin_unlock(&dp->d_lock);
1178 dentry_race_unlock();
1182 #endif /* D_SPLICE_ALIAS_RACE */
1184 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1185 * In kernels 2.2.10 and above, we are passed an additional flags var which
1186 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1187 * we are advised to follow the entry if it is a link or to make sure that
1188 * it is a directory. But since the kernel itself checks these possibilities
1189 * later on, we shouldn't have to do it until later. Perhaps in the future..
1191 * The code here assumes that on entry the global lock is not held
1194 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1195 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1196 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1197 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1199 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1202 cred_t *credp = NULL;
1203 struct vcache *vcp, *pvcp, *tvc = NULL;
1204 struct dentry *parent;
1206 struct afs_fakestat_state fakestate;
1208 afs_uint32 parent_dv;
1211 /* We don't support RCU path walking */
1212 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1213 if (flags & LOOKUP_RCU)
1215 if (nd->flags & LOOKUP_RCU)
1220 #ifdef D_SPLICE_ALIAS_RACE
1221 if (check_dentry_race(dp)) {
1228 afs_InitFakeStat(&fakestate);
1231 vcp = VTOAFS(dp->d_inode);
1233 if (vcp == afs_globalVp)
1236 if (vcp->mvstat == AFS_MVSTAT_MTPT) {
1237 if (vcp->mvid.target_root && (vcp->f.states & CMValid)) {
1238 int tryEvalOnly = 0;
1240 struct vrequest *treq = NULL;
1244 code = afs_CreateReq(&treq, credp);
1248 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1252 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1254 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1255 afs_DestroyReq(treq);
1256 if ((tryEvalOnly && vcp->mvstat == AFS_MVSTAT_MTPT) || code) {
1257 /* a mount point, not yet replaced by its directory */
1261 } else if (vcp->mvstat == AFS_MVSTAT_ROOT && *dp->d_name.name != '/') {
1262 osi_Assert(vcp->mvid.parent != NULL);
1266 /* If the last looker changes, we should make sure the current
1267 * looker still has permission to examine this file. This would
1268 * always require a crref() which would be "slow".
1270 if (vcp->last_looker != treq.uid) {
1271 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1275 vcp->last_looker = treq.uid;
1279 parent = dget_parent(dp);
1280 pvcp = VTOAFS(parent->d_inode);
1281 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1283 /* If the parent's DataVersion has changed or the vnode
1284 * is longer valid, we need to do a full lookup. VerifyVCache
1285 * isn't enough since the vnode may have been renamed.
1288 if (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) {
1289 struct vattr *vattr = NULL;
1293 if (credp == NULL) {
1296 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1299 /* We couldn't perform the lookup, so we're not okay. */
1302 } else if (tvc == vcp) {
1303 /* We got back the same vcache, so we're good. */
1306 } else if (tvc == VTOAFS(dp->d_inode)) {
1307 /* We got back the same vcache, so we're good. This is
1308 * different from the above case, because sometimes 'vcp' is
1309 * not the same as the vcache for dp->d_inode, if 'vcp' was a
1310 * mtpt and we evaluated it to a root dir. In rare cases,
1311 * afs_lookup might not evalute the mtpt when we do, or vice
1312 * versa, so the previous case will not succeed. But this is
1313 * still 'correct', so make sure not to mark the dentry as
1314 * invalid; it still points to the same thing! */
1318 /* We got back a different file, so we're definitely not
1325 /* Force unhash; the name doesn't point to this file
1328 if (code && code != ENOENT) {
1329 /* ...except if we couldn't perform the actual lookup,
1330 * we don't know if the name points to this file or not. */
1336 code = afs_CreateAttr(&vattr);
1342 if (afs_getattr(vcp, vattr, credp)) {
1344 afs_DestroyAttr(vattr);
1348 vattr2inode(AFSTOV(vcp), vattr);
1349 dp->d_time = parent_dv;
1351 afs_DestroyAttr(vattr);
1354 /* should we always update the attributes at this point? */
1355 /* unlikely--the vcache entry hasn't changed */
1361 /* 'dp' represents a cached negative lookup. */
1363 parent = dget_parent(dp);
1364 pvcp = VTOAFS(parent->d_inode);
1365 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1367 if (parent_dv > dp->d_time || !(pvcp->f.states & CStatd)
1368 || afs_IsDynroot(pvcp)) {
1382 #ifndef D_INVALIDATE_IS_VOID
1383 /* When (v3.18) d_invalidate was converted to void, it also started
1384 * being called automatically from revalidate, and automatically
1386 * - shrink_dcache_parent
1387 * - automatic detach of submounts
1389 * Therefore, after that point, OpenAFS revalidate logic no longer needs
1390 * to do any of those things itself for invalid dentry structs. We only need
1391 * to tell VFS it's invalid (by returning 0), and VFS will handle the rest.
1393 if (have_submounts(dp))
1401 afs_PutFakeStat(&fakestate);
1406 #ifndef D_INVALIDATE_IS_VOID
1409 * If we had a negative lookup for the name we want to forcibly
1410 * unhash the dentry.
1411 * Otherwise use d_invalidate which will not unhash it if still in use.
1414 shrink_dcache_parent(dp);
1425 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1427 struct vcache *vcp = VTOAFS(ip);
1430 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1431 (void) afs_InactiveVCache(vcp, NULL);
1434 afs_linux_clear_nfsfs_renamed(dp);
1440 #if defined(DOP_D_DELETE_TAKES_CONST)
1441 afs_dentry_delete(const struct dentry *dp)
1443 afs_dentry_delete(struct dentry *dp)
1446 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1447 return 1; /* bad inode? */
1452 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1453 static struct vfsmount *
1454 afs_dentry_automount(afs_linux_path_t *path)
1456 struct dentry *target;
1459 * Avoid symlink resolution limits when resolving; we cannot contribute to
1460 * an infinite symlink loop.
1462 * On newer kernels the field has moved to the private nameidata structure
1463 * so we can't adjust it here. This may cause ELOOP when using a path with
1464 * 40 or more directories that are not already in the dentry cache.
1466 #if defined(STRUCT_TASK_STRUCT_HAS_TOTAL_LINK_COUNT)
1467 current->total_link_count--;
1470 target = canonical_dentry(path->dentry->d_inode);
1472 if (target == path->dentry) {
1479 path->dentry = target;
1482 spin_lock(&path->dentry->d_lock);
1483 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1484 spin_unlock(&path->dentry->d_lock);
1489 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1491 struct dentry_operations afs_dentry_operations = {
1492 .d_revalidate = afs_linux_dentry_revalidate,
1493 .d_delete = afs_dentry_delete,
1494 .d_iput = afs_dentry_iput,
1495 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1496 .d_automount = afs_dentry_automount,
1497 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1500 /**********************************************************************
1501 * AFS Linux inode operations
1502 **********************************************************************/
1506 * Merely need to set enough of vattr to get us through the create. Note
1507 * that the higher level code (open_namei) will take care of any tuncation
1508 * explicitly. Exclusive open is also taken care of in open_namei.
1510 * name is in kernel space at this point.
1513 #if defined(IOP_CREATE_TAKES_BOOL)
1514 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1516 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1517 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1518 struct nameidata *nd)
1519 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1520 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1521 struct nameidata *nd)
1523 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1526 struct vattr *vattr = NULL;
1527 cred_t *credp = crref();
1528 const char *name = dp->d_name.name;
1534 code = afs_CreateAttr(&vattr);
1538 vattr->va_mode = mode;
1539 vattr->va_type = mode & S_IFMT;
1541 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1545 struct inode *ip = AFSTOV(vcp);
1547 afs_getattr(vcp, vattr, credp);
1548 afs_fill_inode(ip, vattr);
1549 insert_inode_hash(ip);
1550 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1551 dp->d_op = &afs_dentry_operations;
1553 dp->d_time = parent_vcache_dv(dip, credp);
1554 d_instantiate(dp, ip);
1557 afs_DestroyAttr(vattr);
1563 return afs_convert_code(code);
1566 /* afs_linux_lookup */
1567 static struct dentry *
1568 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1569 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1571 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1572 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1573 struct nameidata *nd)
1575 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1578 cred_t *credp = crref();
1579 struct vcache *vcp = NULL;
1580 const char *comp = dp->d_name.name;
1581 struct inode *ip = NULL;
1582 struct dentry *newdp = NULL;
1587 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1588 if (code == ENOENT) {
1589 /* It's ok for the file to not be found. That's noted by the caller by
1590 * seeing that the dp->d_inode field is NULL (set by d_splice_alias or
1593 osi_Assert(vcp == NULL);
1601 struct vattr *vattr = NULL;
1602 struct vcache *parent_vc = VTOAFS(dip);
1604 if (parent_vc == vcp) {
1605 /* This is possible if the parent dir is a mountpoint to a volume,
1606 * and the dir entry we looked up is a mountpoint to the same
1607 * volume. Linux cannot cope with this, so return an error instead
1608 * of risking a deadlock or panic. */
1615 code = afs_CreateAttr(&vattr);
1623 afs_getattr(vcp, vattr, credp);
1624 afs_fill_inode(ip, vattr);
1625 if (hlist_unhashed(&ip->i_hash))
1626 insert_inode_hash(ip);
1628 afs_DestroyAttr(vattr);
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);
1637 if (ip && S_ISDIR(ip->i_mode)) {
1638 d_prune_aliases(ip);
1640 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1641 /* Only needed if this is a volume root */
1642 if (vcp->mvstat == 2)
1643 ip->i_flags |= S_AUTOMOUNT;
1647 * Take an extra reference so the inode doesn't go away if
1648 * d_splice_alias drops our reference on error.
1651 #ifdef HAVE_LINUX_IHOLD
1658 newdp = d_splice_alias(ip, dp);
1659 dentry_race_unlock();
1664 if (IS_ERR(newdp)) {
1665 /* d_splice_alias can return an error (EIO) if there is an existing
1666 * connected directory alias for this dentry. Add our dentry manually
1667 * ourselves if this happens. */
1670 #if defined(D_SPLICE_ALIAS_LEAK_ON_ERROR)
1671 /* Depending on the kernel version, d_splice_alias may or may not drop
1672 * the inode reference on error. If it didn't, do it here. */
1681 return ERR_PTR(afs_convert_code(code));
1689 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1692 cred_t *credp = crref();
1693 const char *name = newdp->d_name.name;
1694 struct inode *oldip = olddp->d_inode;
1696 /* If afs_link returned the vnode, we could instantiate the
1697 * dentry. Since it's not, we drop this one and do a new lookup.
1702 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1706 return afs_convert_code(code);
1709 /* We have to have a Linux specific sillyrename function, because we
1710 * also have to keep the dcache up to date when we're doing a silly
1711 * rename - so we don't want the generic vnodeops doing this behind our
1716 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1719 struct vcache *tvc = VTOAFS(dentry->d_inode);
1720 struct dentry *__dp = NULL;
1721 char *__name = NULL;
1724 if (afs_linux_nfsfs_renamed(dentry))
1732 osi_FreeSmallSpace(__name);
1733 __name = afs_newname();
1736 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1739 osi_FreeSmallSpace(__name);
1742 } while (__dp->d_inode != NULL);
1745 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1746 VTOAFS(dir), (char *)__dp->d_name.name,
1749 tvc->mvid.silly_name = __name;
1752 crfree(tvc->uncred);
1754 tvc->uncred = credp;
1755 tvc->f.states |= CUnlinked;
1756 afs_linux_set_nfsfs_renamed(dentry);
1758 __dp->d_time = 0; /* force to revalidate */
1759 d_move(dentry, __dp);
1761 osi_FreeSmallSpace(__name);
1772 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1775 cred_t *credp = crref();
1776 const char *name = dp->d_name.name;
1777 struct vcache *tvc = VTOAFS(dp->d_inode);
1779 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1780 && !(tvc->f.states & CUnlinked)) {
1782 code = afs_linux_sillyrename(dip, dp, credp);
1785 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1792 return afs_convert_code(code);
1797 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1800 cred_t *credp = crref();
1801 struct vattr *vattr = NULL;
1802 const char *name = dp->d_name.name;
1804 /* If afs_symlink returned the vnode, we could instantiate the
1805 * dentry. Since it's not, we drop this one and do a new lookup.
1810 code = afs_CreateAttr(&vattr);
1815 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1817 afs_DestroyAttr(vattr);
1822 return afs_convert_code(code);
1826 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1827 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1829 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1833 cred_t *credp = crref();
1834 struct vcache *tvcp = NULL;
1835 struct vattr *vattr = NULL;
1836 const char *name = dp->d_name.name;
1839 code = afs_CreateAttr(&vattr);
1844 vattr->va_mask = ATTR_MODE;
1845 vattr->va_mode = mode;
1847 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1850 struct inode *ip = AFSTOV(tvcp);
1852 afs_getattr(tvcp, vattr, credp);
1853 afs_fill_inode(ip, vattr);
1855 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1856 dp->d_op = &afs_dentry_operations;
1858 dp->d_time = parent_vcache_dv(dip, credp);
1859 d_instantiate(dp, ip);
1861 afs_DestroyAttr(vattr);
1867 return afs_convert_code(code);
1871 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1874 cred_t *credp = crref();
1875 const char *name = dp->d_name.name;
1877 /* locking kernel conflicts with glock? */
1880 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1883 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1884 * that failed because a directory is not empty. So, we map
1885 * EEXIST to ENOTEMPTY on linux.
1887 if (code == EEXIST) {
1896 return afs_convert_code(code);
1901 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1902 struct inode *newip, struct dentry *newdp
1903 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1904 , unsigned int flags
1909 cred_t *credp = crref();
1910 const char *oldname = olddp->d_name.name;
1911 const char *newname = newdp->d_name.name;
1912 struct dentry *rehash = NULL;
1914 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1916 return -EINVAL; /* no support for new flags yet */
1919 /* Prevent any new references during rename operation. */
1921 if (!d_unhashed(newdp)) {
1926 afs_maybe_shrink_dcache(olddp);
1929 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1933 olddp->d_time = 0; /* force to revalidate */
1939 return afs_convert_code(code);
1943 /* afs_linux_ireadlink
1944 * Internal readlink which can return link contents to user or kernel space.
1945 * Note that the buffer is NOT supposed to be null-terminated.
1948 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1951 cred_t *credp = crref();
1955 memset(&tuio, 0, sizeof(tuio));
1956 memset(&iov, 0, sizeof(iov));
1958 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1959 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1963 return maxlen - tuio.uio_resid;
1965 return afs_convert_code(code);
1968 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1969 /* afs_linux_readlink
1970 * Fill target (which is in user space) with contents of symlink.
1973 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1976 struct inode *ip = dp->d_inode;
1979 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1985 /* afs_linux_follow_link
1986 * a file system dependent link following routine.
1988 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
1989 static const char *afs_linux_follow_link(struct dentry *dentry, void **link_data)
1991 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1997 name = kmalloc(PATH_MAX, GFP_NOFS);
1999 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2000 return ERR_PTR(-EIO);
2007 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
2011 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2012 return ERR_PTR(code);
2019 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
2020 return *link_data = name;
2022 nd_set_link(nd, name);
2027 #if defined(HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA)
2029 afs_linux_put_link(struct inode *inode, void *link_data)
2031 char *name = link_data;
2033 if (name && !IS_ERR(name))
2038 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
2040 char *name = nd_get_link(nd);
2042 if (name && !IS_ERR(name))
2045 #endif /* HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA */
2047 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2049 /* Populate a page by filling it from the cache file pointed at by cachefp
2050 * (which contains indicated chunk)
2051 * If task is NULL, the page copy occurs syncronously, and the routine
2052 * returns with page still locked. If task is non-NULL, then page copies
2053 * may occur in the background, and the page will be unlocked when it is
2057 afs_linux_read_cache(struct file *cachefp, struct page *page,
2058 int chunk, struct pagevec *lrupv,
2059 struct afs_pagecopy_task *task) {
2060 loff_t offset = page_offset(page);
2061 struct inode *cacheinode = cachefp->f_dentry->d_inode;
2062 struct page *newpage, *cachepage;
2063 struct address_space *cachemapping;
2067 cachemapping = cacheinode->i_mapping;
2071 /* If we're trying to read a page that's past the end of the disk
2072 * cache file, then just return a zeroed page */
2073 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
2074 zero_user_segment(page, 0, PAGE_SIZE);
2075 SetPageUptodate(page);
2081 /* From our offset, we now need to work out which page in the disk
2082 * file it corresponds to. This will be fun ... */
2083 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_SHIFT;
2085 while (cachepage == NULL) {
2086 cachepage = find_get_page(cachemapping, pageindex);
2089 newpage = page_cache_alloc(cachemapping);
2095 code = add_to_page_cache(newpage, cachemapping,
2096 pageindex, GFP_KERNEL);
2098 cachepage = newpage;
2101 get_page(cachepage);
2102 if (!pagevec_add(lrupv, cachepage))
2103 __pagevec_lru_add_file(lrupv);
2108 if (code != -EEXIST)
2112 lock_page(cachepage);
2116 if (!PageUptodate(cachepage)) {
2117 ClearPageError(cachepage);
2118 code = cachemapping->a_ops->readpage(NULL, cachepage);
2119 if (!code && !task) {
2120 wait_on_page_locked(cachepage);
2123 unlock_page(cachepage);
2127 if (PageUptodate(cachepage)) {
2128 copy_highpage(page, cachepage);
2129 flush_dcache_page(page);
2130 SetPageUptodate(page);
2135 afs_pagecopy_queue_page(task, cachepage, page);
2147 put_page(cachepage);
2153 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2155 loff_t offset = page_offset(pp);
2156 struct inode *ip = FILE_INODE(fp);
2157 struct vcache *avc = VTOAFS(ip);
2159 struct file *cacheFp = NULL;
2162 struct pagevec lrupv;
2164 /* Not a UFS cache, don't do anything */
2165 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2168 /* No readpage (ex: tmpfs) , skip */
2169 if (cachefs_noreadpage)
2172 /* Can't do anything if the vcache isn't statd , or if the read
2173 * crosses a chunk boundary.
2175 if (!(avc->f.states & CStatd) ||
2176 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2180 ObtainWriteLock(&avc->lock, 911);
2182 /* XXX - See if hinting actually makes things faster !!! */
2184 /* See if we have a suitable entry already cached */
2188 /* We need to lock xdcache, then dcache, to handle situations where
2189 * the hint is on the free list. However, we can't safely do this
2190 * according to the locking hierarchy. So, use a non blocking lock.
2192 ObtainReadLock(&afs_xdcache);
2193 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2195 if (dcLocked && (tdc->index != NULLIDX)
2196 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2197 && tdc->f.chunk == AFS_CHUNK(offset)
2198 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2199 /* Bonus - the hint was correct */
2202 /* Only destroy the hint if its actually invalid, not if there's
2203 * just been a locking failure */
2205 ReleaseReadLock(&tdc->lock);
2212 ReleaseReadLock(&afs_xdcache);
2215 /* No hint, or hint is no longer valid - see if we can get something
2216 * directly from the dcache
2219 tdc = afs_FindDCache(avc, offset);
2222 ReleaseWriteLock(&avc->lock);
2227 ObtainReadLock(&tdc->lock);
2229 /* Is the dcache we've been given currently up to date */
2230 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2231 (tdc->dflags & DFFetching))
2234 /* Update our hint for future abuse */
2237 /* Okay, so we've now got a cache file that is up to date */
2239 /* XXX - I suspect we should be locking the inodes before we use them! */
2241 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2242 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2243 cachefs_noreadpage = 1;
2247 #if defined(PAGEVEC_INIT_COLD_ARG)
2248 pagevec_init(&lrupv, 0);
2250 pagevec_init(&lrupv);
2253 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
2255 if (pagevec_count(&lrupv))
2256 __pagevec_lru_add_file(&lrupv);
2258 filp_close(cacheFp, NULL);
2261 ReleaseReadLock(&tdc->lock);
2262 ReleaseWriteLock(&avc->lock);
2269 ReleaseWriteLock(&avc->lock);
2270 ReleaseReadLock(&tdc->lock);
2275 /* afs_linux_readpage
2277 * This function is split into two, because prepare_write/begin_write
2278 * require a readpage call which doesn't unlock the resulting page upon
2282 afs_linux_fillpage(struct file *fp, struct page *pp)
2287 struct iovec *iovecp;
2288 struct inode *ip = FILE_INODE(fp);
2289 afs_int32 cnt = page_count(pp);
2290 struct vcache *avc = VTOAFS(ip);
2291 afs_offs_t offset = page_offset(pp);
2295 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2305 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2306 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2308 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2313 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2314 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2315 99999); /* not a possible code value */
2317 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2319 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2320 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2322 AFS_DISCON_UNLOCK();
2325 /* XXX valid for no-cache also? Check last bits of files... :)
2326 * Cognate code goes in afs_NoCacheFetchProc. */
2327 if (auio->uio_resid) /* zero remainder of page */
2328 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2331 flush_dcache_page(pp);
2332 SetPageUptodate(pp);
2341 return afs_convert_code(code);
2345 afs_linux_prefetch(struct file *fp, struct page *pp)
2348 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2349 afs_offs_t offset = page_offset(pp);
2351 if (AFS_CHUNKOFFSET(offset) == 0) {
2353 struct vrequest *treq = NULL;
2358 code = afs_CreateReq(&treq, credp);
2359 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2360 tdc = afs_FindDCache(avc, offset);
2362 if (!(tdc->mflags & DFNextStarted))
2363 afs_PrefetchChunk(avc, tdc, credp, treq);
2366 ReleaseWriteLock(&avc->lock);
2368 afs_DestroyReq(treq);
2372 return afs_convert_code(code);
2377 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2378 struct list_head *page_list, unsigned num_pages)
2383 struct iovec* iovecp;
2384 struct nocache_read_request *ancr;
2386 struct pagevec lrupv;
2390 struct inode *ip = FILE_INODE(fp);
2391 struct vcache *avc = VTOAFS(ip);
2392 afs_int32 base_index = 0;
2393 afs_int32 page_count = 0;
2396 /* background thread must free: iovecp, auio, ancr */
2397 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2399 auio = osi_Alloc(sizeof(struct uio));
2400 auio->uio_iov = iovecp;
2401 auio->uio_iovcnt = num_pages;
2402 auio->uio_flag = UIO_READ;
2403 auio->uio_seg = AFS_UIOSYS;
2404 auio->uio_resid = num_pages * PAGE_SIZE;
2406 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2408 ancr->offset = auio->uio_offset;
2409 ancr->length = auio->uio_resid;
2411 #if defined(PAGEVEC_INIT_COLD_ARG)
2412 pagevec_init(&lrupv, 0);
2414 pagevec_init(&lrupv);
2417 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2419 if(list_empty(page_list))
2422 pp = list_entry(page_list->prev, struct page, lru);
2423 /* If we allocate a page and don't remove it from page_list,
2424 * the page cache gets upset. */
2426 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2427 if(pp->index > isize) {
2434 offset = page_offset(pp);
2435 ancr->offset = auio->uio_offset = offset;
2436 base_index = pp->index;
2438 iovecp[page_ix].iov_len = PAGE_SIZE;
2439 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2440 if(base_index != pp->index) {
2444 iovecp[page_ix].iov_base = (void *) 0;
2446 ancr->length -= PAGE_SIZE;
2454 iovecp[page_ix].iov_base = (void *) 0;
2457 if(!PageLocked(pp)) {
2461 /* increment page refcount--our original design assumed
2462 * that locking it would effectively pin it; protect
2463 * ourselves from the possiblity that this assumption is
2464 * is faulty, at low cost (provided we do not fail to
2465 * do the corresponding decref on the other side) */
2468 /* save the page for background map */
2469 iovecp[page_ix].iov_base = (void*) pp;
2471 /* and put it on the LRU cache */
2472 if (!pagevec_add(&lrupv, pp))
2473 __pagevec_lru_add_file(&lrupv);
2477 /* If there were useful pages in the page list, make sure all pages
2478 * are in the LRU cache, then schedule the read */
2480 if (pagevec_count(&lrupv))
2481 __pagevec_lru_add_file(&lrupv);
2483 code = afs_ReadNoCache(avc, ancr, credp);
2486 /* If there is nothing for the background thread to handle,
2487 * it won't be freeing the things that we never gave it */
2488 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2489 osi_Free(auio, sizeof(struct uio));
2490 osi_Free(ancr, sizeof(struct nocache_read_request));
2492 /* we do not flush, release, or unmap pages--that will be
2493 * done for us by the background thread as each page comes in
2494 * from the fileserver */
2495 return afs_convert_code(code);
2500 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2502 cred_t *credp = NULL;
2504 struct iovec *iovecp;
2505 struct nocache_read_request *ancr;
2509 * Special case: if page is at or past end of file, just zero it and set
2512 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2513 zero_user_segment(pp, 0, PAGE_SIZE);
2514 SetPageUptodate(pp);
2521 /* receiver frees */
2522 auio = osi_Alloc(sizeof(struct uio));
2523 iovecp = osi_Alloc(sizeof(struct iovec));
2525 /* address can be NULL, because we overwrite it with 'pp', below */
2526 setup_uio(auio, iovecp, NULL, page_offset(pp),
2527 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2529 /* save the page for background map */
2530 get_page(pp); /* see above */
2531 auio->uio_iov->iov_base = (void*) pp;
2532 /* the background thread will free this */
2533 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2535 ancr->offset = page_offset(pp);
2536 ancr->length = PAGE_SIZE;
2539 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2542 return afs_convert_code(code);
2546 afs_linux_can_bypass(struct inode *ip) {
2548 switch(cache_bypass_strategy) {
2549 case NEVER_BYPASS_CACHE:
2551 case ALWAYS_BYPASS_CACHE:
2553 case LARGE_FILES_BYPASS_CACHE:
2554 if (i_size_read(ip) > cache_bypass_threshold)
2561 /* Check if a file is permitted to bypass the cache by policy, and modify
2562 * the cache bypass state recorded for that file */
2565 afs_linux_bypass_check(struct inode *ip) {
2568 int bypass = afs_linux_can_bypass(ip);
2571 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2579 afs_linux_readpage(struct file *fp, struct page *pp)
2583 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2584 code = afs_linux_bypass_readpage(fp, pp);
2586 code = afs_linux_fillpage(fp, pp);
2588 code = afs_linux_prefetch(fp, pp);
2595 /* Readpages reads a number of pages for a particular file. We use
2596 * this to optimise the reading, by limiting the number of times upon which
2597 * we have to lookup, lock and open vcaches and dcaches
2601 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2602 struct list_head *page_list, unsigned int num_pages)
2604 struct inode *inode = mapping->host;
2605 struct vcache *avc = VTOAFS(inode);
2607 struct file *cacheFp = NULL;
2609 unsigned int page_idx;
2611 struct pagevec lrupv;
2612 struct afs_pagecopy_task *task;
2614 if (afs_linux_bypass_check(inode))
2615 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2617 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2620 /* No readpage (ex: tmpfs) , skip */
2621 if (cachefs_noreadpage)
2625 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2630 ObtainWriteLock(&avc->lock, 912);
2633 task = afs_pagecopy_init_task();
2636 #if defined(PAGEVEC_INIT_COLD_ARG)
2637 pagevec_init(&lrupv, 0);
2639 pagevec_init(&lrupv);
2641 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2642 struct page *page = list_entry(page_list->prev, struct page, lru);
2643 list_del(&page->lru);
2644 offset = page_offset(page);
2646 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2648 ReleaseReadLock(&tdc->lock);
2653 filp_close(cacheFp, NULL);
2658 if ((tdc = afs_FindDCache(avc, offset))) {
2659 ObtainReadLock(&tdc->lock);
2660 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2661 (tdc->dflags & DFFetching)) {
2662 ReleaseReadLock(&tdc->lock);
2669 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2670 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2671 cachefs_noreadpage = 1;
2677 if (tdc && !add_to_page_cache(page, mapping, page->index,
2680 if (!pagevec_add(&lrupv, page))
2681 __pagevec_lru_add_file(&lrupv);
2683 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2687 if (pagevec_count(&lrupv))
2688 __pagevec_lru_add_file(&lrupv);
2692 filp_close(cacheFp, NULL);
2694 afs_pagecopy_put_task(task);
2698 ReleaseReadLock(&tdc->lock);
2702 ReleaseWriteLock(&avc->lock);
2707 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2710 afs_linux_prepare_writeback(struct vcache *avc) {
2712 struct pagewriter *pw;
2714 pid = MyPidxx2Pid(MyPidxx);
2715 /* Prevent recursion into the writeback code */
2716 spin_lock(&avc->pagewriter_lock);
2717 list_for_each_entry(pw, &avc->pagewriters, link) {
2718 if (pw->writer == pid) {
2719 spin_unlock(&avc->pagewriter_lock);
2720 return AOP_WRITEPAGE_ACTIVATE;
2723 spin_unlock(&avc->pagewriter_lock);
2725 /* Add ourselves to writer list */
2726 pw = osi_Alloc(sizeof(struct pagewriter));
2728 spin_lock(&avc->pagewriter_lock);
2729 list_add_tail(&pw->link, &avc->pagewriters);
2730 spin_unlock(&avc->pagewriter_lock);
2736 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2737 struct vrequest *treq = NULL;
2740 if (!afs_CreateReq(&treq, credp)) {
2741 code = afs_DoPartialWrite(avc, treq);
2742 afs_DestroyReq(treq);
2745 return afs_convert_code(code);
2749 afs_linux_complete_writeback(struct vcache *avc) {
2750 struct pagewriter *pw, *store;
2752 struct list_head tofree;
2754 INIT_LIST_HEAD(&tofree);
2755 pid = MyPidxx2Pid(MyPidxx);
2756 /* Remove ourselves from writer list */
2757 spin_lock(&avc->pagewriter_lock);
2758 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
2759 if (pw->writer == pid) {
2760 list_del(&pw->link);
2761 /* osi_Free may sleep so we need to defer it */
2762 list_add_tail(&pw->link, &tofree);
2765 spin_unlock(&avc->pagewriter_lock);
2766 list_for_each_entry_safe(pw, store, &tofree, link) {
2767 list_del(&pw->link);
2768 osi_Free(pw, sizeof(struct pagewriter));
2772 /* Writeback a given page syncronously. Called with no AFS locks held */
2774 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2775 unsigned long offset, unsigned int count,
2778 struct vcache *vcp = VTOAFS(ip);
2786 memset(&tuio, 0, sizeof(tuio));
2787 memset(&iovec, 0, sizeof(iovec));
2789 buffer = kmap(pp) + offset;
2790 base = page_offset(pp) + offset;
2793 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2794 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2795 ICL_TYPE_INT32, 99999);
2797 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2799 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2801 i_size_write(ip, vcp->f.m.Length);
2802 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2804 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2806 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2807 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2808 ICL_TYPE_INT32, code);
2817 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2818 unsigned long offset, unsigned int count)
2822 struct vcache *vcp = VTOAFS(ip);
2825 /* Catch recursive writeback. This occurs if the kernel decides
2826 * writeback is required whilst we are writing to the cache, or
2827 * flushing to the server. When we're running syncronously (as
2828 * opposed to from writepage) we can't actually do anything about
2829 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2832 ObtainWriteLock(&vcp->lock, 532);
2833 afs_linux_prepare_writeback(vcp);
2834 ReleaseWriteLock(&vcp->lock);
2838 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2841 ObtainWriteLock(&vcp->lock, 533);
2843 code1 = afs_linux_dopartialwrite(vcp, credp);
2844 afs_linux_complete_writeback(vcp);
2845 ReleaseWriteLock(&vcp->lock);
2856 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2857 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2859 afs_linux_writepage(struct page *pp)
2862 struct address_space *mapping = pp->mapping;
2863 struct inode *inode;
2866 unsigned int to = PAGE_SIZE;
2873 inode = mapping->host;
2874 vcp = VTOAFS(inode);
2875 isize = i_size_read(inode);
2877 /* Don't defeat an earlier truncate */
2878 if (page_offset(pp) > isize) {
2879 set_page_writeback(pp);
2885 ObtainWriteLock(&vcp->lock, 537);
2886 code = afs_linux_prepare_writeback(vcp);
2887 if (code == AOP_WRITEPAGE_ACTIVATE) {
2888 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2889 * to return with the page still locked */
2890 ReleaseWriteLock(&vcp->lock);
2895 /* Grab the creds structure currently held in the vnode, and
2896 * get a reference to it, in case it goes away ... */
2902 ReleaseWriteLock(&vcp->lock);
2905 set_page_writeback(pp);
2907 SetPageUptodate(pp);
2909 /* We can unlock the page here, because it's protected by the
2910 * page_writeback flag. This should make us less vulnerable to
2911 * deadlocking in afs_write and afs_DoPartialWrite
2915 /* If this is the final page, then just write the number of bytes that
2916 * are actually in it */
2917 if ((isize - page_offset(pp)) < to )
2918 to = isize - page_offset(pp);
2920 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2923 ObtainWriteLock(&vcp->lock, 538);
2925 /* As much as we might like to ignore a file server error here,
2926 * and just try again when we close(), unfortunately StoreAllSegments
2927 * will invalidate our chunks if the server returns a permanent error,
2928 * so we need to at least try and get that error back to the user
2931 code1 = afs_linux_dopartialwrite(vcp, credp);
2933 afs_linux_complete_writeback(vcp);
2934 ReleaseWriteLock(&vcp->lock);
2939 end_page_writeback(pp);
2951 /* afs_linux_permission
2952 * Check access rights - returns error if can't check or permission denied.
2955 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2956 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2957 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2958 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2960 afs_linux_permission(struct inode *ip, int mode)
2967 /* Check for RCU path walking */
2968 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2969 if (flags & IPERM_FLAG_RCU)
2971 #elif defined(MAY_NOT_BLOCK)
2972 if (mode & MAY_NOT_BLOCK)
2978 if (mode & MAY_EXEC)
2980 if (mode & MAY_READ)
2982 if (mode & MAY_WRITE)
2984 code = afs_access(VTOAFS(ip), tmp, credp);
2988 return afs_convert_code(code);
2992 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2996 struct inode *inode = FILE_INODE(file);
2997 loff_t pagebase = page_offset(page);
2999 if (i_size_read(inode) < (pagebase + offset))
3000 i_size_write(inode, pagebase + offset);
3002 if (PageChecked(page)) {
3003 SetPageUptodate(page);
3004 ClearPageChecked(page);
3007 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
3013 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
3017 /* http://kerneltrap.org/node/4941 details the expected behaviour of
3018 * prepare_write. Essentially, if the page exists within the file,
3019 * and is not being fully written, then we should populate it.
3022 if (!PageUptodate(page)) {
3023 loff_t pagebase = page_offset(page);
3024 loff_t isize = i_size_read(page->mapping->host);
3026 /* Is the location we are writing to beyond the end of the file? */
3027 if (pagebase >= isize ||
3028 ((from == 0) && (pagebase + to) >= isize)) {
3029 zero_user_segments(page, 0, from, to, PAGE_SIZE);
3030 SetPageChecked(page);
3031 /* Are we we writing a full page */
3032 } else if (from == 0 && to == PAGE_SIZE) {
3033 SetPageChecked(page);
3034 /* Is the page readable, if it's wronly, we don't care, because we're
3035 * not actually going to read from it ... */
3036 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
3037 /* We don't care if fillpage fails, because if it does the page
3038 * won't be marked as up to date
3040 afs_linux_fillpage(file, page);
3046 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3048 afs_linux_write_end(struct file *file, struct address_space *mapping,
3049 loff_t pos, unsigned len, unsigned copied,
3050 struct page *page, void *fsdata)
3053 unsigned int from = pos & (PAGE_SIZE - 1);
3055 code = afs_linux_commit_write(file, page, from, from + copied);
3063 afs_linux_write_begin(struct file *file, struct address_space *mapping,
3064 loff_t pos, unsigned len, unsigned flags,
3065 struct page **pagep, void **fsdata)
3068 pgoff_t index = pos >> PAGE_SHIFT;
3069 unsigned int from = pos & (PAGE_SIZE - 1);
3072 page = grab_cache_page_write_begin(mapping, index, flags);
3075 code = afs_linux_prepare_write(file, page, from, from + len);
3085 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3087 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
3089 struct dentry **dpp;
3090 struct dentry *target;
3092 if (current->total_link_count > 0) {
3093 /* avoid symlink resolution limits when resolving; we cannot contribute to
3094 * an infinite symlink loop */
3095 /* only do this for follow_link when total_link_count is positive to be
3096 * on the safe side; there is at least one code path in the Linux
3097 * kernel where it seems like it may be possible to get here without
3098 * total_link_count getting incremented. it is not clear on how that
3099 * path is actually reached, but guard against it just to be safe */
3100 current->total_link_count--;
3103 target = canonical_dentry(dentry->d_inode);
3105 # ifdef STRUCT_NAMEIDATA_HAS_PATH
3106 dpp = &nd->path.dentry;
3116 *dpp = dget(dentry);
3119 nd->last_type = LAST_BIND;
3123 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
3126 static struct inode_operations afs_file_iops = {
3127 .permission = afs_linux_permission,
3128 .getattr = afs_linux_getattr,
3129 .setattr = afs_notify_change,
3132 static struct address_space_operations afs_file_aops = {
3133 .readpage = afs_linux_readpage,
3134 .readpages = afs_linux_readpages,
3135 .writepage = afs_linux_writepage,
3136 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3137 .write_begin = afs_linux_write_begin,
3138 .write_end = afs_linux_write_end,
3140 .commit_write = afs_linux_commit_write,
3141 .prepare_write = afs_linux_prepare_write,
3146 /* Separate ops vector for directories. Linux 2.2 tests type of inode
3147 * by what sort of operation is allowed.....
3150 static struct inode_operations afs_dir_iops = {
3151 .setattr = afs_notify_change,
3152 .create = afs_linux_create,
3153 .lookup = afs_linux_lookup,
3154 .link = afs_linux_link,
3155 .unlink = afs_linux_unlink,
3156 .symlink = afs_linux_symlink,
3157 .mkdir = afs_linux_mkdir,
3158 .rmdir = afs_linux_rmdir,
3159 .rename = afs_linux_rename,
3160 .getattr = afs_linux_getattr,
3161 .permission = afs_linux_permission,
3162 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3163 .follow_link = afs_linux_dir_follow_link,
3167 /* We really need a separate symlink set of ops, since do_follow_link()
3168 * determines if it _is_ a link by checking if the follow_link op is set.
3170 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3172 afs_symlink_filler(struct file *file, struct page *page)
3174 struct inode *ip = (struct inode *)page->mapping->host;
3175 char *p = (char *)kmap(page);
3179 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3184 p[code] = '\0'; /* null terminate? */
3186 SetPageUptodate(page);
3198 static struct address_space_operations afs_symlink_aops = {
3199 .readpage = afs_symlink_filler
3201 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3203 static struct inode_operations afs_symlink_iops = {
3204 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3205 .readlink = page_readlink,
3206 # if defined(HAVE_LINUX_PAGE_GET_LINK)
3207 .get_link = page_get_link,
3208 # elif defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3209 .follow_link = page_follow_link,
3211 .follow_link = page_follow_link_light,
3212 .put_link = page_put_link,
3214 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3215 .readlink = afs_linux_readlink,
3216 .follow_link = afs_linux_follow_link,
3217 .put_link = afs_linux_put_link,
3218 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3219 .setattr = afs_notify_change,
3223 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3226 vattr2inode(ip, vattr);
3228 #ifdef STRUCT_ADDRESS_SPACE_HAS_BACKING_DEV_INFO
3229 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3231 /* Reset ops if symlink or directory. */
3232 if (S_ISREG(ip->i_mode)) {
3233 ip->i_op = &afs_file_iops;
3234 ip->i_fop = &afs_file_fops;
3235 ip->i_data.a_ops = &afs_file_aops;
3237 } else if (S_ISDIR(ip->i_mode)) {
3238 ip->i_op = &afs_dir_iops;
3239 ip->i_fop = &afs_dir_fops;
3241 } else if (S_ISLNK(ip->i_mode)) {
3242 ip->i_op = &afs_symlink_iops;
3243 #if defined(HAVE_LINUX_INODE_NOHIGHMEM)
3244 inode_nohighmem(ip);
3246 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3247 ip->i_data.a_ops = &afs_symlink_aops;
3248 ip->i_mapping = &ip->i_data;