2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
29 #ifdef HAVE_MM_INLINE_H
30 #include <linux/mm_inline.h>
32 #include <linux/pagemap.h>
33 #include <linux/writeback.h>
34 #include <linux/pagevec.h>
35 #include <linux/aio.h>
37 #include "afs/afs_bypasscache.h"
39 #include "osi_compat.h"
40 #include "osi_pagecopy.h"
42 #ifndef HAVE_LINUX_PAGEVEC_LRU_ADD_FILE
43 #define __pagevec_lru_add_file __pagevec_lru_add
47 #define MAX_ERRNO 1000L
50 int cachefs_noreadpage = 0;
52 extern struct backing_dev_info *afs_backing_dev_info;
54 extern struct vcache *afs_globalVp;
56 /* This function converts a positive error code from AFS into a negative
57 * code suitable for passing into the Linux VFS layer. It checks that the
58 * error code is within the permissable bounds for the ERR_PTR mechanism.
60 * _All_ error codes which come from the AFS layer should be passed through
61 * this function before being returned to the kernel.
65 afs_convert_code(int code) {
66 if ((code >= 0) && (code <= MAX_ERRNO))
72 /* Linux doesn't require a credp for many functions, and crref is an expensive
73 * operation. This helper function avoids obtaining it for VerifyVCache calls
77 afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
79 struct vrequest *treq = NULL;
82 if (avc->f.states & CStatd) {
90 code = afs_CreateReq(&treq, credp);
92 code = afs_VerifyVCache2(avc, treq);
101 return afs_convert_code(code);
104 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
105 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
107 afs_linux_read_iter(struct kiocb *iocb, struct iov_iter *iter)
108 # elif defined(LINUX_HAS_NONVECTOR_AIO)
110 afs_linux_aio_read(struct kiocb *iocb, char __user *buf, size_t bufsize,
114 afs_linux_aio_read(struct kiocb *iocb, const struct iovec *buf,
115 unsigned long bufsize, loff_t pos)
118 struct file *fp = iocb->ki_filp;
120 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
121 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
122 loff_t pos = iocb->ki_pos;
123 unsigned long bufsize = iter->nr_segs;
128 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
129 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
130 (afs_int32)bufsize, ICL_TYPE_INT32, 99999);
131 code = afs_linux_VerifyVCache(vcp, NULL);
134 /* Linux's FlushPages implementation doesn't ever use credp,
135 * so we optimise by not using it */
136 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
138 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
139 code = generic_file_read_iter(iocb, iter);
141 code = generic_file_aio_read(iocb, buf, bufsize, pos);
146 afs_Trace4(afs_iclSetp, CM_TRACE_AIOREADOP, ICL_TYPE_POINTER, vcp,
147 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
148 (afs_int32)bufsize, ICL_TYPE_INT32, code);
154 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
157 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
160 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
161 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
163 code = afs_linux_VerifyVCache(vcp, NULL);
166 /* Linux's FlushPages implementation doesn't ever use credp,
167 * so we optimise by not using it */
168 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
170 code = do_sync_read(fp, buf, count, offp);
174 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
175 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
183 /* Now we have integrated VM for writes as well as reads. the generic write operations
184 * also take care of re-positioning the pointer if file is open in append
185 * mode. Call fake open/close to ensure we do writes of core dumps.
187 #if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER) || defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
188 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
190 afs_linux_write_iter(struct kiocb *iocb, struct iov_iter *iter)
191 # elif defined(LINUX_HAS_NONVECTOR_AIO)
193 afs_linux_aio_write(struct kiocb *iocb, const char __user *buf, size_t bufsize,
197 afs_linux_aio_write(struct kiocb *iocb, const struct iovec *buf,
198 unsigned long bufsize, loff_t pos)
202 struct vcache *vcp = VTOAFS(iocb->ki_filp->f_dentry->d_inode);
204 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
205 loff_t pos = iocb->ki_pos;
206 unsigned long bufsize = iter->nr_segs;
211 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
212 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
213 (afs_int32)bufsize, ICL_TYPE_INT32,
214 (iocb->ki_filp->f_flags & O_APPEND) ? 99998 : 99999);
216 code = afs_linux_VerifyVCache(vcp, &credp);
218 ObtainWriteLock(&vcp->lock, 529);
220 ReleaseWriteLock(&vcp->lock);
223 # if defined(STRUCT_FILE_OPERATIONS_HAS_READ_ITER)
224 code = generic_file_write_iter(iocb, iter);
226 code = generic_file_aio_write(iocb, buf, bufsize, pos);
231 ObtainWriteLock(&vcp->lock, 530);
233 if (vcp->execsOrWriters == 1 && !credp)
236 afs_FakeClose(vcp, credp);
237 ReleaseWriteLock(&vcp->lock);
239 afs_Trace4(afs_iclSetp, CM_TRACE_AIOWRITEOP, ICL_TYPE_POINTER, vcp,
240 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(pos), ICL_TYPE_INT32,
241 (afs_int32)bufsize, ICL_TYPE_INT32, code);
250 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
253 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
258 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
259 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
260 (fp->f_flags & O_APPEND) ? 99998 : 99999);
262 code = afs_linux_VerifyVCache(vcp, &credp);
264 ObtainWriteLock(&vcp->lock, 529);
266 ReleaseWriteLock(&vcp->lock);
269 code = do_sync_write(fp, buf, count, offp);
273 ObtainWriteLock(&vcp->lock, 530);
275 if (vcp->execsOrWriters == 1 && !credp)
278 afs_FakeClose(vcp, credp);
279 ReleaseWriteLock(&vcp->lock);
281 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
282 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
292 extern int BlobScan(struct dcache * afile, afs_int32 ablob, afs_int32 *ablobOut);
294 /* This is a complete rewrite of afs_readdir, since we can make use of
295 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
296 * handling and use of bulkstats will need to be reflected here as well.
299 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
300 afs_linux_readdir(struct file *fp, struct dir_context *ctx)
302 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
305 struct vcache *avc = VTOAFS(FILE_INODE(fp));
306 struct vrequest *treq = NULL;
312 struct DirBuffer entry;
315 afs_size_t origOffset, tlen;
316 cred_t *credp = crref();
317 struct afs_fakestat_state fakestat;
320 AFS_STATCNT(afs_readdir);
322 code = afs_convert_code(afs_CreateReq(&treq, credp));
327 afs_InitFakeStat(&fakestat);
328 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, treq));
332 /* update the cache entry */
334 code = afs_convert_code(afs_VerifyVCache2(avc, treq));
338 /* get a reference to the entire directory */
339 tdc = afs_GetDCache(avc, (afs_size_t) 0, treq, &origOffset, &tlen, 1);
345 ObtainWriteLock(&avc->lock, 811);
346 ObtainReadLock(&tdc->lock);
348 * Make sure that the data in the cache is current. There are two
349 * cases we need to worry about:
350 * 1. The cache data is being fetched by another process.
351 * 2. The cache data is no longer valid
353 while ((avc->f.states & CStatd)
354 && (tdc->dflags & DFFetching)
355 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
356 ReleaseReadLock(&tdc->lock);
357 ReleaseWriteLock(&avc->lock);
358 afs_osi_Sleep(&tdc->validPos);
359 ObtainWriteLock(&avc->lock, 812);
360 ObtainReadLock(&tdc->lock);
362 if (!(avc->f.states & CStatd)
363 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
364 ReleaseReadLock(&tdc->lock);
365 ReleaseWriteLock(&avc->lock);
370 /* Set the readdir-in-progress flag, and downgrade the lock
371 * to shared so others will be able to acquire a read lock.
373 avc->f.states |= CReadDir;
374 avc->dcreaddir = tdc;
375 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
376 ConvertWToSLock(&avc->lock);
378 /* Fill in until we get an error or we're done. This implementation
379 * takes an offset in units of blobs, rather than bytes.
382 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
385 offset = (int) fp->f_pos;
388 code = BlobScan(tdc, offset, &dirpos);
392 code = afs_dir_GetVerifiedBlob(tdc, dirpos, &entry);
394 if (!(avc->f.states & CCorrupt)) {
395 struct cell *tc = afs_GetCellStale(avc->f.fid.Cell, READ_LOCK);
396 afs_warn("afs: Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)\n",
397 avc->f.fid.Cell, avc->f.fid.Fid.Volume,
398 avc->f.fid.Fid.Vnode, avc->f.fid.Fid.Unique,
399 tc ? tc->cellName : "",
400 (unsigned long)&tdc->f.inode, dirpos);
402 afs_PutCell(tc, READ_LOCK);
403 UpgradeSToWLock(&avc->lock, 814);
404 avc->f.states |= CCorrupt;
410 de = (struct DirEntry *)entry.data;
411 ino = afs_calc_inum (avc->f.fid.Cell, avc->f.fid.Fid.Volume,
412 ntohl(de->fid.vnode));
413 len = strlen(de->name);
415 /* filldir returns -EINVAL when the buffer is full. */
417 unsigned int type = DT_UNKNOWN;
418 struct VenusFid afid;
421 afid.Cell = avc->f.fid.Cell;
422 afid.Fid.Volume = avc->f.fid.Fid.Volume;
423 afid.Fid.Vnode = ntohl(de->fid.vnode);
424 afid.Fid.Unique = ntohl(de->fid.vunique);
425 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
427 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
428 if (tvc->mvstat != AFS_MVSTAT_FILE) {
430 } else if (((tvc->f.states) & (CStatd | CTruth))) {
431 /* CTruth will be set if the object has
436 else if (vtype == VREG)
438 /* Don't do this until we're sure it can't be a mtpt */
439 /* else if (vtype == VLNK)
441 /* what other types does AFS support? */
443 /* clean up from afs_FindVCache */
447 * If this is NFS readdirplus, then the filler is going to
448 * call getattr on this inode, which will deadlock if we're
452 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
453 /* dir_emit returns a bool - true when it succeeds.
454 * Inverse the result to fit with how we check "code" */
455 code = !dir_emit(ctx, de->name, len, ino, type);
457 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
464 offset = dirpos + 1 + ((len + 16) >> 5);
466 /* If filldir didn't fill in the last one this is still pointing to that
472 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
473 ctx->pos = (loff_t) offset;
475 fp->f_pos = (loff_t) offset;
477 ReleaseReadLock(&tdc->lock);
479 UpgradeSToWLock(&avc->lock, 813);
480 avc->f.states &= ~CReadDir;
482 avc->readdir_pid = 0;
483 ReleaseSharedLock(&avc->lock);
486 afs_PutFakeStat(&fakestat);
487 afs_DestroyReq(treq);
494 /* in afs_pioctl.c */
495 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
498 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
499 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
501 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
508 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
510 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
514 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
515 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
516 vmap->vm_end - vmap->vm_start);
518 /* get a validated vcache entry */
519 code = afs_linux_VerifyVCache(vcp, NULL);
522 /* Linux's Flushpage implementation doesn't use credp, so optimise
523 * our code to not need to crref() it */
524 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
526 code = generic_file_mmap(fp, vmap);
529 vcp->f.states |= CMAPPED;
537 afs_linux_open(struct inode *ip, struct file *fp)
539 struct vcache *vcp = VTOAFS(ip);
540 cred_t *credp = crref();
544 code = afs_open(&vcp, fp->f_flags, credp);
548 return afs_convert_code(code);
552 afs_linux_release(struct inode *ip, struct file *fp)
554 struct vcache *vcp = VTOAFS(ip);
555 cred_t *credp = crref();
559 code = afs_close(vcp, fp->f_flags, credp);
560 ObtainWriteLock(&vcp->lock, 807);
565 ReleaseWriteLock(&vcp->lock);
569 return afs_convert_code(code);
573 #if defined(FOP_FSYNC_TAKES_DENTRY)
574 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
575 #elif defined(FOP_FSYNC_TAKES_RANGE)
576 afs_linux_fsync(struct file *fp, loff_t start, loff_t end, int datasync)
578 afs_linux_fsync(struct file *fp, int datasync)
582 struct inode *ip = FILE_INODE(fp);
583 cred_t *credp = crref();
585 #if defined(FOP_FSYNC_TAKES_RANGE)
586 afs_linux_lock_inode(ip);
589 code = afs_fsync(VTOAFS(ip), credp);
591 #if defined(FOP_FSYNC_TAKES_RANGE)
592 afs_linux_unlock_inode(ip);
595 return afs_convert_code(code);
601 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
604 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
605 cred_t *credp = crref();
606 struct AFS_FLOCK flock;
608 /* Convert to a lock format afs_lockctl understands. */
609 memset(&flock, 0, sizeof(flock));
610 flock.l_type = flp->fl_type;
611 flock.l_pid = flp->fl_pid;
613 flock.l_start = flp->fl_start;
614 if (flp->fl_end == OFFSET_MAX)
615 flock.l_len = 0; /* Lock to end of file */
617 flock.l_len = flp->fl_end - flp->fl_start + 1;
619 /* Safe because there are no large files, yet */
620 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
621 if (cmd == F_GETLK64)
623 else if (cmd == F_SETLK64)
625 else if (cmd == F_SETLKW64)
627 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
630 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
633 if ((code == 0 || flp->fl_type == F_UNLCK) &&
634 (cmd == F_SETLK || cmd == F_SETLKW)) {
635 code = afs_posix_lock_file(fp, flp);
636 if (code && flp->fl_type != F_UNLCK) {
637 struct AFS_FLOCK flock2;
639 flock2.l_type = F_UNLCK;
641 afs_lockctl(vcp, &flock2, F_SETLK, credp);
645 /* If lockctl says there are no conflicting locks, then also check with the
646 * kernel, as lockctl knows nothing about byte range locks
648 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
649 afs_posix_test_lock(fp, flp);
650 /* If we found a lock in the kernel's structure, return it */
651 if (flp->fl_type != F_UNLCK) {
657 /* Convert flock back to Linux's file_lock */
658 flp->fl_type = flock.l_type;
659 flp->fl_pid = flock.l_pid;
660 flp->fl_start = flock.l_start;
661 if (flock.l_len == 0)
662 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
664 flp->fl_end = flock.l_start + flock.l_len - 1;
670 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
672 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
674 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
675 cred_t *credp = crref();
676 struct AFS_FLOCK flock;
677 /* Convert to a lock format afs_lockctl understands. */
678 memset(&flock, 0, sizeof(flock));
679 flock.l_type = flp->fl_type;
680 flock.l_pid = flp->fl_pid;
685 /* Safe because there are no large files, yet */
686 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
687 if (cmd == F_GETLK64)
689 else if (cmd == F_SETLK64)
691 else if (cmd == F_SETLKW64)
693 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
696 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
699 if ((code == 0 || flp->fl_type == F_UNLCK) &&
700 (cmd == F_SETLK || cmd == F_SETLKW)) {
701 flp->fl_flags &=~ FL_SLEEP;
702 code = flock_lock_file_wait(fp, flp);
703 if (code && flp->fl_type != F_UNLCK) {
704 struct AFS_FLOCK flock2;
706 flock2.l_type = F_UNLCK;
708 afs_lockctl(vcp, &flock2, F_SETLK, credp);
712 /* Convert flock back to Linux's file_lock */
713 flp->fl_type = flock.l_type;
714 flp->fl_pid = flock.l_pid;
722 * essentially the same as afs_fsync() but we need to get the return
723 * code for the sys_close() here, not afs_linux_release(), so call
724 * afs_StoreAllSegments() with AFS_LASTSTORE
727 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
728 afs_linux_flush(struct file *fp, fl_owner_t id)
730 afs_linux_flush(struct file *fp)
733 struct vrequest *treq = NULL;
741 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
749 vcp = VTOAFS(FILE_INODE(fp));
751 code = afs_CreateReq(&treq, credp);
754 /* If caching is bypassed for this file, or globally, just return 0 */
755 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
758 ObtainReadLock(&vcp->lock);
759 if (vcp->cachingStates & FCSBypass)
761 ReleaseReadLock(&vcp->lock);
764 /* future proof: don't rely on 0 return from afs_InitReq */
769 ObtainSharedLock(&vcp->lock, 535);
770 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
771 UpgradeSToWLock(&vcp->lock, 536);
772 if (!AFS_IS_DISCONNECTED) {
773 code = afs_StoreAllSegments(vcp,
775 AFS_SYNC | AFS_LASTSTORE);
777 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
779 ConvertWToSLock(&vcp->lock);
781 code = afs_CheckCode(code, treq, 54);
782 ReleaseSharedLock(&vcp->lock);
785 afs_DestroyReq(treq);
790 return afs_convert_code(code);
793 struct file_operations afs_dir_fops = {
794 .read = generic_read_dir,
795 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
796 .iterate = afs_linux_readdir,
798 .readdir = afs_linux_readdir,
800 #ifdef HAVE_UNLOCKED_IOCTL
801 .unlocked_ioctl = afs_unlocked_xioctl,
805 #ifdef HAVE_COMPAT_IOCTL
806 .compat_ioctl = afs_unlocked_xioctl,
808 .open = afs_linux_open,
809 .release = afs_linux_release,
810 .llseek = default_llseek,
811 #ifdef HAVE_LINUX_NOOP_FSYNC
814 .fsync = simple_sync_file,
818 struct file_operations afs_file_fops = {
819 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
820 .read_iter = afs_linux_read_iter,
821 .write_iter = afs_linux_write_iter,
822 # if !defined(HAVE_LINUX___VFS_READ)
823 .read = new_sync_read,
824 .write = new_sync_write,
826 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
827 .aio_read = afs_linux_aio_read,
828 .aio_write = afs_linux_aio_write,
829 .read = do_sync_read,
830 .write = do_sync_write,
832 .read = afs_linux_read,
833 .write = afs_linux_write,
835 #ifdef HAVE_UNLOCKED_IOCTL
836 .unlocked_ioctl = afs_unlocked_xioctl,
840 #ifdef HAVE_COMPAT_IOCTL
841 .compat_ioctl = afs_unlocked_xioctl,
843 .mmap = afs_linux_mmap,
844 .open = afs_linux_open,
845 .flush = afs_linux_flush,
846 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
847 .sendfile = generic_file_sendfile,
849 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE) && !defined(HAVE_LINUX_DEFAULT_FILE_SPLICE_READ)
850 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
851 .splice_write = iter_file_splice_write,
853 .splice_write = generic_file_splice_write,
855 .splice_read = generic_file_splice_read,
857 .release = afs_linux_release,
858 .fsync = afs_linux_fsync,
859 .lock = afs_linux_lock,
860 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
861 .flock = afs_linux_flock,
863 .llseek = default_llseek,
866 static struct dentry *
867 canonical_dentry(struct inode *ip)
869 struct vcache *vcp = VTOAFS(ip);
870 struct dentry *first = NULL, *ret = NULL, *cur;
871 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
872 struct hlist_node *p;
876 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
877 * otherwise, use the first dentry in ip->i_dentry.
878 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
880 /* note that vcp->target_link specifies which dentry to use, but we have
881 * no reference held on that dentry. so, we cannot use or dereference
882 * vcp->target_link itself, since it may have been freed. instead, we only
883 * use it to compare to pointers in the ip->i_dentry list. */
887 # ifdef HAVE_DCACHE_LOCK
888 spin_lock(&dcache_lock);
890 spin_lock(&ip->i_lock);
893 #if defined(D_ALIAS_IS_HLIST)
894 # if defined(HLIST_ITERATOR_NO_NODE)
895 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
897 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
900 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
903 if (!vcp->target_link || cur == vcp->target_link) {
916 vcp->target_link = ret;
918 # ifdef HAVE_DCACHE_LOCK
922 spin_unlock(&dcache_lock);
927 spin_unlock(&ip->i_lock);
933 /**********************************************************************
934 * AFS Linux dentry operations
935 **********************************************************************/
937 /* afs_linux_revalidate
938 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
941 afs_linux_revalidate(struct dentry *dp)
943 struct vattr *vattr = NULL;
944 struct vcache *vcp = VTOAFS(dp->d_inode);
948 if (afs_shuttingdown != AFS_RUNNING)
953 code = afs_CreateAttr(&vattr);
958 /* This avoids the crref when we don't have to do it. Watch for
959 * changes in afs_getattr that don't get replicated here!
961 if (vcp->f.states & CStatd &&
962 (!afs_fakestat_enable || vcp->mvstat != AFS_MVSTAT_MTPT) &&
964 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
965 code = afs_CopyOutAttrs(vcp, vattr);
968 code = afs_getattr(vcp, vattr, credp);
973 afs_fill_inode(AFSTOV(vcp), vattr);
975 afs_DestroyAttr(vattr);
980 return afs_convert_code(code);
984 * Set iattr data into vattr. Assume vattr cleared before call.
987 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
989 vattrp->va_mask = iattrp->ia_valid;
990 if (iattrp->ia_valid & ATTR_MODE)
991 vattrp->va_mode = iattrp->ia_mode;
992 if (iattrp->ia_valid & ATTR_UID)
993 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
994 if (iattrp->ia_valid & ATTR_GID)
995 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
996 if (iattrp->ia_valid & ATTR_SIZE)
997 vattrp->va_size = iattrp->ia_size;
998 if (iattrp->ia_valid & ATTR_ATIME) {
999 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1000 vattrp->va_atime.tv_usec = 0;
1002 if (iattrp->ia_valid & ATTR_MTIME) {
1003 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1004 vattrp->va_mtime.tv_usec = 0;
1006 if (iattrp->ia_valid & ATTR_CTIME) {
1007 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1008 vattrp->va_ctime.tv_usec = 0;
1013 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1016 vattr2inode(struct inode *ip, struct vattr *vp)
1018 ip->i_ino = vp->va_nodeid;
1019 #ifdef HAVE_LINUX_SET_NLINK
1020 set_nlink(ip, vp->va_nlink);
1022 ip->i_nlink = vp->va_nlink;
1024 ip->i_blocks = vp->va_blocks;
1025 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1026 ip->i_blkbits = AFS_BLKBITS;
1028 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1029 ip->i_blksize = vp->va_blocksize;
1031 ip->i_rdev = vp->va_rdev;
1032 ip->i_mode = vp->va_mode;
1033 ip->i_uid = afs_make_kuid(vp->va_uid);
1034 ip->i_gid = afs_make_kgid(vp->va_gid);
1035 i_size_write(ip, vp->va_size);
1036 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1037 ip->i_atime.tv_nsec = 0;
1038 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1039 /* Set the mtime nanoseconds to the sysname generation number.
1040 * This convinces NFS clients that all directories have changed
1041 * any time the sysname list changes.
1043 ip->i_mtime.tv_nsec = afs_sysnamegen;
1044 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1045 ip->i_ctime.tv_nsec = 0;
1048 /* afs_notify_change
1049 * Linux version of setattr call. What to change is in the iattr struct.
1050 * We need to set bits in both the Linux inode as well as the vcache.
1053 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1055 struct vattr *vattr = NULL;
1056 cred_t *credp = crref();
1057 struct inode *ip = dp->d_inode;
1061 code = afs_CreateAttr(&vattr);
1066 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1068 code = afs_setattr(VTOAFS(ip), vattr, credp);
1070 afs_getattr(VTOAFS(ip), vattr, credp);
1071 vattr2inode(ip, vattr);
1073 afs_DestroyAttr(vattr);
1078 return afs_convert_code(code);
1081 #if defined(IOP_GETATTR_TAKES_PATH_STRUCT)
1083 afs_linux_getattr(const struct path *path, struct kstat *stat, u32 request_mask, unsigned int sync_mode)
1085 int err = afs_linux_revalidate(path->dentry);
1087 generic_fillattr(path->dentry->d_inode, stat);
1093 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1095 int err = afs_linux_revalidate(dentry);
1097 generic_fillattr(dentry->d_inode, stat);
1104 parent_vcache_dv(struct inode *inode, cred_t *credp)
1107 struct vcache *pvcp;
1110 * If parent is a mount point and we are using fakestat, we may need
1111 * to look at the fake vcache entry instead of what the vfs is giving
1112 * us. The fake entry is the one with the useful DataVersion.
1114 pvcp = VTOAFS(inode);
1115 if (pvcp->mvstat == AFS_MVSTAT_MTPT && afs_fakestat_enable) {
1116 struct vrequest treq;
1117 struct afs_fakestat_state fakestate;
1123 afs_InitReq(&treq, credp);
1124 afs_InitFakeStat(&fakestate);
1125 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1128 afs_PutFakeStat(&fakestate);
1130 return hgetlo(pvcp->f.m.DataVersion);
1133 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1134 * In kernels 2.2.10 and above, we are passed an additional flags var which
1135 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1136 * we are advised to follow the entry if it is a link or to make sure that
1137 * it is a directory. But since the kernel itself checks these possibilities
1138 * later on, we shouldn't have to do it until later. Perhaps in the future..
1140 * The code here assumes that on entry the global lock is not held
1143 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1144 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1145 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1146 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1148 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1151 cred_t *credp = NULL;
1152 struct vcache *vcp, *pvcp, *tvc = NULL;
1153 struct dentry *parent;
1155 struct afs_fakestat_state fakestate;
1157 afs_uint32 parent_dv;
1160 /* We don't support RCU path walking */
1161 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1162 if (flags & LOOKUP_RCU)
1164 if (nd->flags & LOOKUP_RCU)
1170 afs_InitFakeStat(&fakestate);
1173 vcp = VTOAFS(dp->d_inode);
1175 if (vcp == afs_globalVp)
1178 if (vcp->mvstat == AFS_MVSTAT_MTPT) {
1179 if (vcp->mvid.target_root && (vcp->f.states & CMValid)) {
1180 int tryEvalOnly = 0;
1182 struct vrequest *treq = NULL;
1186 code = afs_CreateReq(&treq, credp);
1190 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1194 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1196 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1197 afs_DestroyReq(treq);
1198 if ((tryEvalOnly && vcp->mvstat == AFS_MVSTAT_MTPT) || code) {
1199 /* a mount point, not yet replaced by its directory */
1203 } else if (vcp->mvstat == AFS_MVSTAT_ROOT && *dp->d_name.name != '/') {
1204 osi_Assert(vcp->mvid.parent != NULL);
1208 /* If the last looker changes, we should make sure the current
1209 * looker still has permission to examine this file. This would
1210 * always require a crref() which would be "slow".
1212 if (vcp->last_looker != treq.uid) {
1213 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1217 vcp->last_looker = treq.uid;
1221 parent = dget_parent(dp);
1222 pvcp = VTOAFS(parent->d_inode);
1223 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1225 /* If the parent's DataVersion has changed or the vnode
1226 * is longer valid, we need to do a full lookup. VerifyVCache
1227 * isn't enough since the vnode may have been renamed.
1230 if (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) {
1231 struct vattr *vattr = NULL;
1235 if (credp == NULL) {
1238 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1241 /* We couldn't perform the lookup, so we're not okay. */
1244 } else if (tvc == vcp) {
1245 /* We got back the same vcache, so we're good. */
1248 } else if (tvc == VTOAFS(dp->d_inode)) {
1249 /* We got back the same vcache, so we're good. This is
1250 * different from the above case, because sometimes 'vcp' is
1251 * not the same as the vcache for dp->d_inode, if 'vcp' was a
1252 * mtpt and we evaluated it to a root dir. In rare cases,
1253 * afs_lookup might not evalute the mtpt when we do, or vice
1254 * versa, so the previous case will not succeed. But this is
1255 * still 'correct', so make sure not to mark the dentry as
1256 * invalid; it still points to the same thing! */
1260 /* We got back a different file, so we're definitely not
1267 /* Force unhash; the name doesn't point to this file
1270 if (code && code != ENOENT) {
1271 /* ...except if we couldn't perform the actual lookup,
1272 * we don't know if the name points to this file or not. */
1278 code = afs_CreateAttr(&vattr);
1284 if (afs_getattr(vcp, vattr, credp)) {
1286 afs_DestroyAttr(vattr);
1290 vattr2inode(AFSTOV(vcp), vattr);
1291 dp->d_time = parent_dv;
1293 afs_DestroyAttr(vattr);
1296 /* should we always update the attributes at this point? */
1297 /* unlikely--the vcache entry hasn't changed */
1303 /* 'dp' represents a cached negative lookup. */
1305 parent = dget_parent(dp);
1306 pvcp = VTOAFS(parent->d_inode);
1307 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1309 if (parent_dv > dp->d_time || !(pvcp->f.states & CStatd)
1310 || afs_IsDynroot(pvcp)) {
1324 #ifndef D_INVALIDATE_IS_VOID
1325 /* When (v3.18) d_invalidate was converted to void, it also started
1326 * being called automatically from revalidate, and automatically
1328 * - shrink_dcache_parent
1329 * - automatic detach of submounts
1331 * Therefore, after that point, OpenAFS revalidate logic no longer needs
1332 * to do any of those things itself for invalid dentry structs. We only need
1333 * to tell VFS it's invalid (by returning 0), and VFS will handle the rest.
1335 if (have_submounts(dp))
1343 afs_PutFakeStat(&fakestate);
1348 #ifndef D_INVALIDATE_IS_VOID
1351 * If we had a negative lookup for the name we want to forcibly
1352 * unhash the dentry.
1353 * Otherwise use d_invalidate which will not unhash it if still in use.
1356 shrink_dcache_parent(dp);
1367 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1369 struct vcache *vcp = VTOAFS(ip);
1372 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1373 (void) afs_InactiveVCache(vcp, NULL);
1376 afs_linux_clear_nfsfs_renamed(dp);
1382 #if defined(DOP_D_DELETE_TAKES_CONST)
1383 afs_dentry_delete(const struct dentry *dp)
1385 afs_dentry_delete(struct dentry *dp)
1388 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1389 return 1; /* bad inode? */
1394 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1395 static struct vfsmount *
1396 afs_dentry_automount(afs_linux_path_t *path)
1398 struct dentry *target;
1401 * Avoid symlink resolution limits when resolving; we cannot contribute to
1402 * an infinite symlink loop.
1404 * On newer kernels the field has moved to the private nameidata structure
1405 * so we can't adjust it here. This may cause ELOOP when using a path with
1406 * 40 or more directories that are not already in the dentry cache.
1408 #if defined(STRUCT_TASK_STRUCT_HAS_TOTAL_LINK_COUNT)
1409 current->total_link_count--;
1412 target = canonical_dentry(path->dentry->d_inode);
1414 if (target == path->dentry) {
1421 path->dentry = target;
1424 spin_lock(&path->dentry->d_lock);
1425 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1426 spin_unlock(&path->dentry->d_lock);
1431 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1433 struct dentry_operations afs_dentry_operations = {
1434 .d_revalidate = afs_linux_dentry_revalidate,
1435 .d_delete = afs_dentry_delete,
1436 .d_iput = afs_dentry_iput,
1437 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1438 .d_automount = afs_dentry_automount,
1439 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1442 /**********************************************************************
1443 * AFS Linux inode operations
1444 **********************************************************************/
1448 * Merely need to set enough of vattr to get us through the create. Note
1449 * that the higher level code (open_namei) will take care of any tuncation
1450 * explicitly. Exclusive open is also taken care of in open_namei.
1452 * name is in kernel space at this point.
1455 #if defined(IOP_CREATE_TAKES_BOOL)
1456 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1458 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1459 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1460 struct nameidata *nd)
1461 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1462 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1463 struct nameidata *nd)
1465 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1468 struct vattr *vattr = NULL;
1469 cred_t *credp = crref();
1470 const char *name = dp->d_name.name;
1476 code = afs_CreateAttr(&vattr);
1480 vattr->va_mode = mode;
1481 vattr->va_type = mode & S_IFMT;
1483 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1487 struct inode *ip = AFSTOV(vcp);
1489 afs_getattr(vcp, vattr, credp);
1490 afs_fill_inode(ip, vattr);
1491 insert_inode_hash(ip);
1492 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1493 dp->d_op = &afs_dentry_operations;
1495 dp->d_time = parent_vcache_dv(dip, credp);
1496 d_instantiate(dp, ip);
1499 afs_DestroyAttr(vattr);
1505 return afs_convert_code(code);
1508 /* afs_linux_lookup */
1509 static struct dentry *
1510 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1511 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1513 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1514 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1515 struct nameidata *nd)
1517 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1520 cred_t *credp = crref();
1521 struct vcache *vcp = NULL;
1522 const char *comp = dp->d_name.name;
1523 struct inode *ip = NULL;
1524 struct dentry *newdp = NULL;
1529 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1530 if (code == ENOENT) {
1531 /* It's ok for the file to not be found. That's noted by the caller by
1532 * seeing that the dp->d_inode field is NULL (set by d_splice_alias or
1535 osi_Assert(vcp == NULL);
1543 struct vattr *vattr = NULL;
1544 struct vcache *parent_vc = VTOAFS(dip);
1546 if (parent_vc == vcp) {
1547 /* This is possible if the parent dir is a mountpoint to a volume,
1548 * and the dir entry we looked up is a mountpoint to the same
1549 * volume. Linux cannot cope with this, so return an error instead
1550 * of risking a deadlock or panic. */
1557 code = afs_CreateAttr(&vattr);
1565 afs_getattr(vcp, vattr, credp);
1566 afs_fill_inode(ip, vattr);
1567 if (hlist_unhashed(&ip->i_hash))
1568 insert_inode_hash(ip);
1570 afs_DestroyAttr(vattr);
1572 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1573 dp->d_op = &afs_dentry_operations;
1575 dp->d_time = parent_vcache_dv(dip, credp);
1579 if (ip && S_ISDIR(ip->i_mode)) {
1580 d_prune_aliases(ip);
1582 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1583 /* Only needed if this is a volume root */
1584 if (vcp->mvstat == 2)
1585 ip->i_flags |= S_AUTOMOUNT;
1589 * Take an extra reference so the inode doesn't go away if
1590 * d_splice_alias drops our reference on error.
1593 #ifdef HAVE_LINUX_IHOLD
1599 newdp = d_splice_alias(ip, dp);
1604 if (IS_ERR(newdp)) {
1605 /* d_splice_alias can return an error (EIO) if there is an existing
1606 * connected directory alias for this dentry. Add our dentry manually
1607 * ourselves if this happens. */
1610 #if defined(D_SPLICE_ALIAS_LEAK_ON_ERROR)
1611 /* Depending on the kernel version, d_splice_alias may or may not drop
1612 * the inode reference on error. If it didn't, do it here. */
1621 return ERR_PTR(afs_convert_code(code));
1629 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1632 cred_t *credp = crref();
1633 const char *name = newdp->d_name.name;
1634 struct inode *oldip = olddp->d_inode;
1636 /* If afs_link returned the vnode, we could instantiate the
1637 * dentry. Since it's not, we drop this one and do a new lookup.
1642 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1646 return afs_convert_code(code);
1649 /* We have to have a Linux specific sillyrename function, because we
1650 * also have to keep the dcache up to date when we're doing a silly
1651 * rename - so we don't want the generic vnodeops doing this behind our
1656 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1659 struct vcache *tvc = VTOAFS(dentry->d_inode);
1660 struct dentry *__dp = NULL;
1661 char *__name = NULL;
1664 if (afs_linux_nfsfs_renamed(dentry))
1672 osi_FreeSmallSpace(__name);
1673 __name = afs_newname();
1676 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1679 osi_FreeSmallSpace(__name);
1682 } while (__dp->d_inode != NULL);
1685 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1686 VTOAFS(dir), (char *)__dp->d_name.name,
1689 tvc->mvid.silly_name = __name;
1692 crfree(tvc->uncred);
1694 tvc->uncred = credp;
1695 tvc->f.states |= CUnlinked;
1696 afs_linux_set_nfsfs_renamed(dentry);
1698 __dp->d_time = 0; /* force to revalidate */
1699 d_move(dentry, __dp);
1701 osi_FreeSmallSpace(__name);
1712 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1715 cred_t *credp = crref();
1716 const char *name = dp->d_name.name;
1717 struct vcache *tvc = VTOAFS(dp->d_inode);
1719 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1720 && !(tvc->f.states & CUnlinked)) {
1722 code = afs_linux_sillyrename(dip, dp, credp);
1725 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1732 return afs_convert_code(code);
1737 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1740 cred_t *credp = crref();
1741 struct vattr *vattr = NULL;
1742 const char *name = dp->d_name.name;
1744 /* If afs_symlink returned the vnode, we could instantiate the
1745 * dentry. Since it's not, we drop this one and do a new lookup.
1750 code = afs_CreateAttr(&vattr);
1755 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1757 afs_DestroyAttr(vattr);
1762 return afs_convert_code(code);
1766 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1767 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1769 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1773 cred_t *credp = crref();
1774 struct vcache *tvcp = NULL;
1775 struct vattr *vattr = NULL;
1776 const char *name = dp->d_name.name;
1779 code = afs_CreateAttr(&vattr);
1784 vattr->va_mask = ATTR_MODE;
1785 vattr->va_mode = mode;
1787 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1790 struct inode *ip = AFSTOV(tvcp);
1792 afs_getattr(tvcp, vattr, credp);
1793 afs_fill_inode(ip, vattr);
1795 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1796 dp->d_op = &afs_dentry_operations;
1798 dp->d_time = parent_vcache_dv(dip, credp);
1799 d_instantiate(dp, ip);
1801 afs_DestroyAttr(vattr);
1807 return afs_convert_code(code);
1811 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1814 cred_t *credp = crref();
1815 const char *name = dp->d_name.name;
1817 /* locking kernel conflicts with glock? */
1820 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1823 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1824 * that failed because a directory is not empty. So, we map
1825 * EEXIST to ENOTEMPTY on linux.
1827 if (code == EEXIST) {
1836 return afs_convert_code(code);
1841 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1842 struct inode *newip, struct dentry *newdp
1843 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1844 , unsigned int flags
1849 cred_t *credp = crref();
1850 const char *oldname = olddp->d_name.name;
1851 const char *newname = newdp->d_name.name;
1852 struct dentry *rehash = NULL;
1854 #ifdef HAVE_LINUX_INODE_OPERATIONS_RENAME_TAKES_FLAGS
1856 return -EINVAL; /* no support for new flags yet */
1859 /* Prevent any new references during rename operation. */
1861 if (!d_unhashed(newdp)) {
1866 afs_maybe_shrink_dcache(olddp);
1869 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1873 olddp->d_time = 0; /* force to revalidate */
1879 return afs_convert_code(code);
1883 /* afs_linux_ireadlink
1884 * Internal readlink which can return link contents to user or kernel space.
1885 * Note that the buffer is NOT supposed to be null-terminated.
1888 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1891 cred_t *credp = crref();
1895 memset(&tuio, 0, sizeof(tuio));
1896 memset(&iov, 0, sizeof(iov));
1898 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1899 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1903 return maxlen - tuio.uio_resid;
1905 return afs_convert_code(code);
1908 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1909 /* afs_linux_readlink
1910 * Fill target (which is in user space) with contents of symlink.
1913 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1916 struct inode *ip = dp->d_inode;
1919 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1925 /* afs_linux_follow_link
1926 * a file system dependent link following routine.
1928 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
1929 static const char *afs_linux_follow_link(struct dentry *dentry, void **link_data)
1931 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1937 name = kmalloc(PATH_MAX, GFP_NOFS);
1939 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
1940 return ERR_PTR(-EIO);
1947 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1951 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
1952 return ERR_PTR(code);
1959 #if defined(HAVE_LINUX_INODE_OPERATIONS_FOLLOW_LINK_NO_NAMEIDATA)
1960 return *link_data = name;
1962 nd_set_link(nd, name);
1967 #if defined(HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA)
1969 afs_linux_put_link(struct inode *inode, void *link_data)
1971 char *name = link_data;
1973 if (name && !IS_ERR(name))
1978 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1980 char *name = nd_get_link(nd);
1982 if (name && !IS_ERR(name))
1985 #endif /* HAVE_LINUX_INODE_OPERATIONS_PUT_LINK_NO_NAMEIDATA */
1987 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1989 /* Populate a page by filling it from the cache file pointed at by cachefp
1990 * (which contains indicated chunk)
1991 * If task is NULL, the page copy occurs syncronously, and the routine
1992 * returns with page still locked. If task is non-NULL, then page copies
1993 * may occur in the background, and the page will be unlocked when it is
1997 afs_linux_read_cache(struct file *cachefp, struct page *page,
1998 int chunk, struct pagevec *lrupv,
1999 struct afs_pagecopy_task *task) {
2000 loff_t offset = page_offset(page);
2001 struct inode *cacheinode = cachefp->f_dentry->d_inode;
2002 struct page *newpage, *cachepage;
2003 struct address_space *cachemapping;
2007 cachemapping = cacheinode->i_mapping;
2011 /* If we're trying to read a page that's past the end of the disk
2012 * cache file, then just return a zeroed page */
2013 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
2014 zero_user_segment(page, 0, PAGE_SIZE);
2015 SetPageUptodate(page);
2021 /* From our offset, we now need to work out which page in the disk
2022 * file it corresponds to. This will be fun ... */
2023 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_SHIFT;
2025 while (cachepage == NULL) {
2026 cachepage = find_get_page(cachemapping, pageindex);
2029 newpage = page_cache_alloc_cold(cachemapping);
2035 code = add_to_page_cache(newpage, cachemapping,
2036 pageindex, GFP_KERNEL);
2038 cachepage = newpage;
2041 get_page(cachepage);
2042 if (!pagevec_add(lrupv, cachepage))
2043 __pagevec_lru_add_file(lrupv);
2048 if (code != -EEXIST)
2052 lock_page(cachepage);
2056 if (!PageUptodate(cachepage)) {
2057 ClearPageError(cachepage);
2058 code = cachemapping->a_ops->readpage(NULL, cachepage);
2059 if (!code && !task) {
2060 wait_on_page_locked(cachepage);
2063 unlock_page(cachepage);
2067 if (PageUptodate(cachepage)) {
2068 copy_highpage(page, cachepage);
2069 flush_dcache_page(page);
2070 SetPageUptodate(page);
2075 afs_pagecopy_queue_page(task, cachepage, page);
2087 put_page(cachepage);
2093 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2095 loff_t offset = page_offset(pp);
2096 struct inode *ip = FILE_INODE(fp);
2097 struct vcache *avc = VTOAFS(ip);
2099 struct file *cacheFp = NULL;
2102 struct pagevec lrupv;
2104 /* Not a UFS cache, don't do anything */
2105 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2108 /* No readpage (ex: tmpfs) , skip */
2109 if (cachefs_noreadpage)
2112 /* Can't do anything if the vcache isn't statd , or if the read
2113 * crosses a chunk boundary.
2115 if (!(avc->f.states & CStatd) ||
2116 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2120 ObtainWriteLock(&avc->lock, 911);
2122 /* XXX - See if hinting actually makes things faster !!! */
2124 /* See if we have a suitable entry already cached */
2128 /* We need to lock xdcache, then dcache, to handle situations where
2129 * the hint is on the free list. However, we can't safely do this
2130 * according to the locking hierarchy. So, use a non blocking lock.
2132 ObtainReadLock(&afs_xdcache);
2133 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2135 if (dcLocked && (tdc->index != NULLIDX)
2136 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2137 && tdc->f.chunk == AFS_CHUNK(offset)
2138 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2139 /* Bonus - the hint was correct */
2142 /* Only destroy the hint if its actually invalid, not if there's
2143 * just been a locking failure */
2145 ReleaseReadLock(&tdc->lock);
2152 ReleaseReadLock(&afs_xdcache);
2155 /* No hint, or hint is no longer valid - see if we can get something
2156 * directly from the dcache
2159 tdc = afs_FindDCache(avc, offset);
2162 ReleaseWriteLock(&avc->lock);
2167 ObtainReadLock(&tdc->lock);
2169 /* Is the dcache we've been given currently up to date */
2170 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2171 (tdc->dflags & DFFetching))
2174 /* Update our hint for future abuse */
2177 /* Okay, so we've now got a cache file that is up to date */
2179 /* XXX - I suspect we should be locking the inodes before we use them! */
2181 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2182 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2183 cachefs_noreadpage = 1;
2187 pagevec_init(&lrupv, 0);
2189 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
2191 if (pagevec_count(&lrupv))
2192 __pagevec_lru_add_file(&lrupv);
2194 filp_close(cacheFp, NULL);
2197 ReleaseReadLock(&tdc->lock);
2198 ReleaseWriteLock(&avc->lock);
2205 ReleaseWriteLock(&avc->lock);
2206 ReleaseReadLock(&tdc->lock);
2211 /* afs_linux_readpage
2213 * This function is split into two, because prepare_write/begin_write
2214 * require a readpage call which doesn't unlock the resulting page upon
2218 afs_linux_fillpage(struct file *fp, struct page *pp)
2223 struct iovec *iovecp;
2224 struct inode *ip = FILE_INODE(fp);
2225 afs_int32 cnt = page_count(pp);
2226 struct vcache *avc = VTOAFS(ip);
2227 afs_offs_t offset = page_offset(pp);
2231 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2241 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2242 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2244 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2249 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2250 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2251 99999); /* not a possible code value */
2253 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2255 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2256 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2258 AFS_DISCON_UNLOCK();
2261 /* XXX valid for no-cache also? Check last bits of files... :)
2262 * Cognate code goes in afs_NoCacheFetchProc. */
2263 if (auio->uio_resid) /* zero remainder of page */
2264 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2267 flush_dcache_page(pp);
2268 SetPageUptodate(pp);
2277 return afs_convert_code(code);
2281 afs_linux_prefetch(struct file *fp, struct page *pp)
2284 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2285 afs_offs_t offset = page_offset(pp);
2287 if (AFS_CHUNKOFFSET(offset) == 0) {
2289 struct vrequest *treq = NULL;
2294 code = afs_CreateReq(&treq, credp);
2295 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2296 tdc = afs_FindDCache(avc, offset);
2298 if (!(tdc->mflags & DFNextStarted))
2299 afs_PrefetchChunk(avc, tdc, credp, treq);
2302 ReleaseWriteLock(&avc->lock);
2304 afs_DestroyReq(treq);
2308 return afs_convert_code(code);
2313 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2314 struct list_head *page_list, unsigned num_pages)
2319 struct iovec* iovecp;
2320 struct nocache_read_request *ancr;
2322 struct pagevec lrupv;
2326 struct inode *ip = FILE_INODE(fp);
2327 struct vcache *avc = VTOAFS(ip);
2328 afs_int32 base_index = 0;
2329 afs_int32 page_count = 0;
2332 /* background thread must free: iovecp, auio, ancr */
2333 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2335 auio = osi_Alloc(sizeof(struct uio));
2336 auio->uio_iov = iovecp;
2337 auio->uio_iovcnt = num_pages;
2338 auio->uio_flag = UIO_READ;
2339 auio->uio_seg = AFS_UIOSYS;
2340 auio->uio_resid = num_pages * PAGE_SIZE;
2342 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2344 ancr->offset = auio->uio_offset;
2345 ancr->length = auio->uio_resid;
2347 pagevec_init(&lrupv, 0);
2349 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2351 if(list_empty(page_list))
2354 pp = list_entry(page_list->prev, struct page, lru);
2355 /* If we allocate a page and don't remove it from page_list,
2356 * the page cache gets upset. */
2358 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_SHIFT;
2359 if(pp->index > isize) {
2366 offset = page_offset(pp);
2367 ancr->offset = auio->uio_offset = offset;
2368 base_index = pp->index;
2370 iovecp[page_ix].iov_len = PAGE_SIZE;
2371 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2372 if(base_index != pp->index) {
2376 iovecp[page_ix].iov_base = (void *) 0;
2378 ancr->length -= PAGE_SIZE;
2386 iovecp[page_ix].iov_base = (void *) 0;
2389 if(!PageLocked(pp)) {
2393 /* increment page refcount--our original design assumed
2394 * that locking it would effectively pin it; protect
2395 * ourselves from the possiblity that this assumption is
2396 * is faulty, at low cost (provided we do not fail to
2397 * do the corresponding decref on the other side) */
2400 /* save the page for background map */
2401 iovecp[page_ix].iov_base = (void*) pp;
2403 /* and put it on the LRU cache */
2404 if (!pagevec_add(&lrupv, pp))
2405 __pagevec_lru_add_file(&lrupv);
2409 /* If there were useful pages in the page list, make sure all pages
2410 * are in the LRU cache, then schedule the read */
2412 if (pagevec_count(&lrupv))
2413 __pagevec_lru_add_file(&lrupv);
2415 code = afs_ReadNoCache(avc, ancr, credp);
2418 /* If there is nothing for the background thread to handle,
2419 * it won't be freeing the things that we never gave it */
2420 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2421 osi_Free(auio, sizeof(struct uio));
2422 osi_Free(ancr, sizeof(struct nocache_read_request));
2424 /* we do not flush, release, or unmap pages--that will be
2425 * done for us by the background thread as each page comes in
2426 * from the fileserver */
2427 return afs_convert_code(code);
2432 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2434 cred_t *credp = NULL;
2436 struct iovec *iovecp;
2437 struct nocache_read_request *ancr;
2441 * Special case: if page is at or past end of file, just zero it and set
2444 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2445 zero_user_segment(pp, 0, PAGE_SIZE);
2446 SetPageUptodate(pp);
2453 /* receiver frees */
2454 auio = osi_Alloc(sizeof(struct uio));
2455 iovecp = osi_Alloc(sizeof(struct iovec));
2457 /* address can be NULL, because we overwrite it with 'pp', below */
2458 setup_uio(auio, iovecp, NULL, page_offset(pp),
2459 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2461 /* save the page for background map */
2462 get_page(pp); /* see above */
2463 auio->uio_iov->iov_base = (void*) pp;
2464 /* the background thread will free this */
2465 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2467 ancr->offset = page_offset(pp);
2468 ancr->length = PAGE_SIZE;
2471 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2474 return afs_convert_code(code);
2478 afs_linux_can_bypass(struct inode *ip) {
2480 switch(cache_bypass_strategy) {
2481 case NEVER_BYPASS_CACHE:
2483 case ALWAYS_BYPASS_CACHE:
2485 case LARGE_FILES_BYPASS_CACHE:
2486 if (i_size_read(ip) > cache_bypass_threshold)
2493 /* Check if a file is permitted to bypass the cache by policy, and modify
2494 * the cache bypass state recorded for that file */
2497 afs_linux_bypass_check(struct inode *ip) {
2500 int bypass = afs_linux_can_bypass(ip);
2503 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2511 afs_linux_readpage(struct file *fp, struct page *pp)
2515 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2516 code = afs_linux_bypass_readpage(fp, pp);
2518 code = afs_linux_fillpage(fp, pp);
2520 code = afs_linux_prefetch(fp, pp);
2527 /* Readpages reads a number of pages for a particular file. We use
2528 * this to optimise the reading, by limiting the number of times upon which
2529 * we have to lookup, lock and open vcaches and dcaches
2533 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2534 struct list_head *page_list, unsigned int num_pages)
2536 struct inode *inode = mapping->host;
2537 struct vcache *avc = VTOAFS(inode);
2539 struct file *cacheFp = NULL;
2541 unsigned int page_idx;
2543 struct pagevec lrupv;
2544 struct afs_pagecopy_task *task;
2546 if (afs_linux_bypass_check(inode))
2547 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2549 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2552 /* No readpage (ex: tmpfs) , skip */
2553 if (cachefs_noreadpage)
2557 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2562 ObtainWriteLock(&avc->lock, 912);
2565 task = afs_pagecopy_init_task();
2568 pagevec_init(&lrupv, 0);
2569 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2570 struct page *page = list_entry(page_list->prev, struct page, lru);
2571 list_del(&page->lru);
2572 offset = page_offset(page);
2574 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2576 ReleaseReadLock(&tdc->lock);
2581 filp_close(cacheFp, NULL);
2586 if ((tdc = afs_FindDCache(avc, offset))) {
2587 ObtainReadLock(&tdc->lock);
2588 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2589 (tdc->dflags & DFFetching)) {
2590 ReleaseReadLock(&tdc->lock);
2597 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2598 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2599 cachefs_noreadpage = 1;
2605 if (tdc && !add_to_page_cache(page, mapping, page->index,
2608 if (!pagevec_add(&lrupv, page))
2609 __pagevec_lru_add_file(&lrupv);
2611 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2615 if (pagevec_count(&lrupv))
2616 __pagevec_lru_add_file(&lrupv);
2620 filp_close(cacheFp, NULL);
2622 afs_pagecopy_put_task(task);
2626 ReleaseReadLock(&tdc->lock);
2630 ReleaseWriteLock(&avc->lock);
2635 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2638 afs_linux_prepare_writeback(struct vcache *avc) {
2640 struct pagewriter *pw;
2642 pid = MyPidxx2Pid(MyPidxx);
2643 /* Prevent recursion into the writeback code */
2644 spin_lock(&avc->pagewriter_lock);
2645 list_for_each_entry(pw, &avc->pagewriters, link) {
2646 if (pw->writer == pid) {
2647 spin_unlock(&avc->pagewriter_lock);
2648 return AOP_WRITEPAGE_ACTIVATE;
2651 spin_unlock(&avc->pagewriter_lock);
2653 /* Add ourselves to writer list */
2654 pw = osi_Alloc(sizeof(struct pagewriter));
2656 spin_lock(&avc->pagewriter_lock);
2657 list_add_tail(&pw->link, &avc->pagewriters);
2658 spin_unlock(&avc->pagewriter_lock);
2664 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2665 struct vrequest *treq = NULL;
2668 if (!afs_CreateReq(&treq, credp)) {
2669 code = afs_DoPartialWrite(avc, treq);
2670 afs_DestroyReq(treq);
2673 return afs_convert_code(code);
2677 afs_linux_complete_writeback(struct vcache *avc) {
2678 struct pagewriter *pw, *store;
2680 struct list_head tofree;
2682 INIT_LIST_HEAD(&tofree);
2683 pid = MyPidxx2Pid(MyPidxx);
2684 /* Remove ourselves from writer list */
2685 spin_lock(&avc->pagewriter_lock);
2686 list_for_each_entry_safe(pw, store, &avc->pagewriters, link) {
2687 if (pw->writer == pid) {
2688 list_del(&pw->link);
2689 /* osi_Free may sleep so we need to defer it */
2690 list_add_tail(&pw->link, &tofree);
2693 spin_unlock(&avc->pagewriter_lock);
2694 list_for_each_entry_safe(pw, store, &tofree, link) {
2695 list_del(&pw->link);
2696 osi_Free(pw, sizeof(struct pagewriter));
2700 /* Writeback a given page syncronously. Called with no AFS locks held */
2702 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2703 unsigned long offset, unsigned int count,
2706 struct vcache *vcp = VTOAFS(ip);
2714 memset(&tuio, 0, sizeof(tuio));
2715 memset(&iovec, 0, sizeof(iovec));
2717 buffer = kmap(pp) + offset;
2718 base = page_offset(pp) + offset;
2721 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2722 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2723 ICL_TYPE_INT32, 99999);
2725 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2727 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2729 i_size_write(ip, vcp->f.m.Length);
2730 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2732 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2734 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2735 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2736 ICL_TYPE_INT32, code);
2745 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2746 unsigned long offset, unsigned int count)
2750 struct vcache *vcp = VTOAFS(ip);
2753 /* Catch recursive writeback. This occurs if the kernel decides
2754 * writeback is required whilst we are writing to the cache, or
2755 * flushing to the server. When we're running syncronously (as
2756 * opposed to from writepage) we can't actually do anything about
2757 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2760 ObtainWriteLock(&vcp->lock, 532);
2761 afs_linux_prepare_writeback(vcp);
2762 ReleaseWriteLock(&vcp->lock);
2766 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2769 ObtainWriteLock(&vcp->lock, 533);
2771 code1 = afs_linux_dopartialwrite(vcp, credp);
2772 afs_linux_complete_writeback(vcp);
2773 ReleaseWriteLock(&vcp->lock);
2784 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2785 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2787 afs_linux_writepage(struct page *pp)
2790 struct address_space *mapping = pp->mapping;
2791 struct inode *inode;
2794 unsigned int to = PAGE_SIZE;
2801 inode = mapping->host;
2802 vcp = VTOAFS(inode);
2803 isize = i_size_read(inode);
2805 /* Don't defeat an earlier truncate */
2806 if (page_offset(pp) > isize) {
2807 set_page_writeback(pp);
2813 ObtainWriteLock(&vcp->lock, 537);
2814 code = afs_linux_prepare_writeback(vcp);
2815 if (code == AOP_WRITEPAGE_ACTIVATE) {
2816 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2817 * to return with the page still locked */
2818 ReleaseWriteLock(&vcp->lock);
2823 /* Grab the creds structure currently held in the vnode, and
2824 * get a reference to it, in case it goes away ... */
2830 ReleaseWriteLock(&vcp->lock);
2833 set_page_writeback(pp);
2835 SetPageUptodate(pp);
2837 /* We can unlock the page here, because it's protected by the
2838 * page_writeback flag. This should make us less vulnerable to
2839 * deadlocking in afs_write and afs_DoPartialWrite
2843 /* If this is the final page, then just write the number of bytes that
2844 * are actually in it */
2845 if ((isize - page_offset(pp)) < to )
2846 to = isize - page_offset(pp);
2848 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2851 ObtainWriteLock(&vcp->lock, 538);
2853 /* As much as we might like to ignore a file server error here,
2854 * and just try again when we close(), unfortunately StoreAllSegments
2855 * will invalidate our chunks if the server returns a permanent error,
2856 * so we need to at least try and get that error back to the user
2859 code1 = afs_linux_dopartialwrite(vcp, credp);
2861 afs_linux_complete_writeback(vcp);
2862 ReleaseWriteLock(&vcp->lock);
2867 end_page_writeback(pp);
2879 /* afs_linux_permission
2880 * Check access rights - returns error if can't check or permission denied.
2883 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2884 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2885 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2886 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2888 afs_linux_permission(struct inode *ip, int mode)
2895 /* Check for RCU path walking */
2896 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2897 if (flags & IPERM_FLAG_RCU)
2899 #elif defined(MAY_NOT_BLOCK)
2900 if (mode & MAY_NOT_BLOCK)
2906 if (mode & MAY_EXEC)
2908 if (mode & MAY_READ)
2910 if (mode & MAY_WRITE)
2912 code = afs_access(VTOAFS(ip), tmp, credp);
2916 return afs_convert_code(code);
2920 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2924 struct inode *inode = FILE_INODE(file);
2925 loff_t pagebase = page_offset(page);
2927 if (i_size_read(inode) < (pagebase + offset))
2928 i_size_write(inode, pagebase + offset);
2930 if (PageChecked(page)) {
2931 SetPageUptodate(page);
2932 ClearPageChecked(page);
2935 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2941 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2945 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2946 * prepare_write. Essentially, if the page exists within the file,
2947 * and is not being fully written, then we should populate it.
2950 if (!PageUptodate(page)) {
2951 loff_t pagebase = page_offset(page);
2952 loff_t isize = i_size_read(page->mapping->host);
2954 /* Is the location we are writing to beyond the end of the file? */
2955 if (pagebase >= isize ||
2956 ((from == 0) && (pagebase + to) >= isize)) {
2957 zero_user_segments(page, 0, from, to, PAGE_SIZE);
2958 SetPageChecked(page);
2959 /* Are we we writing a full page */
2960 } else if (from == 0 && to == PAGE_SIZE) {
2961 SetPageChecked(page);
2962 /* Is the page readable, if it's wronly, we don't care, because we're
2963 * not actually going to read from it ... */
2964 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2965 /* We don't care if fillpage fails, because if it does the page
2966 * won't be marked as up to date
2968 afs_linux_fillpage(file, page);
2974 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2976 afs_linux_write_end(struct file *file, struct address_space *mapping,
2977 loff_t pos, unsigned len, unsigned copied,
2978 struct page *page, void *fsdata)
2981 unsigned int from = pos & (PAGE_SIZE - 1);
2983 code = afs_linux_commit_write(file, page, from, from + copied);
2991 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2992 loff_t pos, unsigned len, unsigned flags,
2993 struct page **pagep, void **fsdata)
2996 pgoff_t index = pos >> PAGE_SHIFT;
2997 unsigned int from = pos & (PAGE_SIZE - 1);
3000 page = grab_cache_page_write_begin(mapping, index, flags);
3003 code = afs_linux_prepare_write(file, page, from, from + len);
3013 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3015 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
3017 struct dentry **dpp;
3018 struct dentry *target;
3020 if (current->total_link_count > 0) {
3021 /* avoid symlink resolution limits when resolving; we cannot contribute to
3022 * an infinite symlink loop */
3023 /* only do this for follow_link when total_link_count is positive to be
3024 * on the safe side; there is at least one code path in the Linux
3025 * kernel where it seems like it may be possible to get here without
3026 * total_link_count getting incremented. it is not clear on how that
3027 * path is actually reached, but guard against it just to be safe */
3028 current->total_link_count--;
3031 target = canonical_dentry(dentry->d_inode);
3033 # ifdef STRUCT_NAMEIDATA_HAS_PATH
3034 dpp = &nd->path.dentry;
3044 *dpp = dget(dentry);
3047 nd->last_type = LAST_BIND;
3051 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
3054 static struct inode_operations afs_file_iops = {
3055 .permission = afs_linux_permission,
3056 .getattr = afs_linux_getattr,
3057 .setattr = afs_notify_change,
3060 static struct address_space_operations afs_file_aops = {
3061 .readpage = afs_linux_readpage,
3062 .readpages = afs_linux_readpages,
3063 .writepage = afs_linux_writepage,
3064 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
3065 .write_begin = afs_linux_write_begin,
3066 .write_end = afs_linux_write_end,
3068 .commit_write = afs_linux_commit_write,
3069 .prepare_write = afs_linux_prepare_write,
3074 /* Separate ops vector for directories. Linux 2.2 tests type of inode
3075 * by what sort of operation is allowed.....
3078 static struct inode_operations afs_dir_iops = {
3079 .setattr = afs_notify_change,
3080 .create = afs_linux_create,
3081 .lookup = afs_linux_lookup,
3082 .link = afs_linux_link,
3083 .unlink = afs_linux_unlink,
3084 .symlink = afs_linux_symlink,
3085 .mkdir = afs_linux_mkdir,
3086 .rmdir = afs_linux_rmdir,
3087 .rename = afs_linux_rename,
3088 .getattr = afs_linux_getattr,
3089 .permission = afs_linux_permission,
3090 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
3091 .follow_link = afs_linux_dir_follow_link,
3095 /* We really need a separate symlink set of ops, since do_follow_link()
3096 * determines if it _is_ a link by checking if the follow_link op is set.
3098 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3100 afs_symlink_filler(struct file *file, struct page *page)
3102 struct inode *ip = (struct inode *)page->mapping->host;
3103 char *p = (char *)kmap(page);
3107 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3112 p[code] = '\0'; /* null terminate? */
3114 SetPageUptodate(page);
3126 static struct address_space_operations afs_symlink_aops = {
3127 .readpage = afs_symlink_filler
3129 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3131 static struct inode_operations afs_symlink_iops = {
3132 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3133 .readlink = page_readlink,
3134 # if defined(HAVE_LINUX_PAGE_GET_LINK)
3135 .get_link = page_get_link,
3136 # elif defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3137 .follow_link = page_follow_link,
3139 .follow_link = page_follow_link_light,
3140 .put_link = page_put_link,
3142 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3143 .readlink = afs_linux_readlink,
3144 .follow_link = afs_linux_follow_link,
3145 .put_link = afs_linux_put_link,
3146 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3147 .setattr = afs_notify_change,
3151 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3154 vattr2inode(ip, vattr);
3156 #ifdef STRUCT_ADDRESS_SPACE_HAS_BACKING_DEV_INFO
3157 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3159 /* Reset ops if symlink or directory. */
3160 if (S_ISREG(ip->i_mode)) {
3161 ip->i_op = &afs_file_iops;
3162 ip->i_fop = &afs_file_fops;
3163 ip->i_data.a_ops = &afs_file_aops;
3165 } else if (S_ISDIR(ip->i_mode)) {
3166 ip->i_op = &afs_dir_iops;
3167 ip->i_fop = &afs_dir_fops;
3169 } else if (S_ISLNK(ip->i_mode)) {
3170 ip->i_op = &afs_symlink_iops;
3171 #if defined(HAVE_LINUX_INODE_NOHIGHMEM)
3172 inode_nohighmem(ip);
3174 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3175 ip->i_data.a_ops = &afs_symlink_aops;
3176 ip->i_mapping = &ip->i_data;