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);
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 dirpos = BlobScan(tdc, offset);
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("Corrupt directory (%d.%d.%d.%d [%s] @%lx, pos %d)",
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))) {
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 mutex_lock(&ip->i_mutex);
589 code = afs_fsync(VTOAFS(ip), credp);
591 #if defined(FOP_FSYNC_TAKES_RANGE)
592 mutex_unlock(&ip->i_mutex);
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 if ((vcp->f.states & CRO)) {
631 if (flp->fl_type == F_WRLCK) {
640 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
643 if ((code == 0 || flp->fl_type == F_UNLCK) &&
644 (cmd == F_SETLK || cmd == F_SETLKW)) {
645 code = afs_posix_lock_file(fp, flp);
646 if (code && flp->fl_type != F_UNLCK) {
647 struct AFS_FLOCK flock2;
649 flock2.l_type = F_UNLCK;
651 afs_lockctl(vcp, &flock2, F_SETLK, credp);
655 /* If lockctl says there are no conflicting locks, then also check with the
656 * kernel, as lockctl knows nothing about byte range locks
658 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
659 afs_posix_test_lock(fp, flp);
660 /* If we found a lock in the kernel's structure, return it */
661 if (flp->fl_type != F_UNLCK) {
667 /* Convert flock back to Linux's file_lock */
668 flp->fl_type = flock.l_type;
669 flp->fl_pid = flock.l_pid;
670 flp->fl_start = flock.l_start;
671 if (flock.l_len == 0)
672 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
674 flp->fl_end = flock.l_start + flock.l_len - 1;
680 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
682 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
684 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
685 cred_t *credp = crref();
686 struct AFS_FLOCK flock;
687 /* Convert to a lock format afs_lockctl understands. */
688 memset(&flock, 0, sizeof(flock));
689 flock.l_type = flp->fl_type;
690 flock.l_pid = flp->fl_pid;
695 /* Safe because there are no large files, yet */
696 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
697 if (cmd == F_GETLK64)
699 else if (cmd == F_SETLK64)
701 else if (cmd == F_SETLKW64)
703 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
706 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
709 if ((code == 0 || flp->fl_type == F_UNLCK) &&
710 (cmd == F_SETLK || cmd == F_SETLKW)) {
711 flp->fl_flags &=~ FL_SLEEP;
712 code = flock_lock_file_wait(fp, flp);
713 if (code && flp->fl_type != F_UNLCK) {
714 struct AFS_FLOCK flock2;
716 flock2.l_type = F_UNLCK;
718 afs_lockctl(vcp, &flock2, F_SETLK, credp);
722 /* Convert flock back to Linux's file_lock */
723 flp->fl_type = flock.l_type;
724 flp->fl_pid = flock.l_pid;
732 * essentially the same as afs_fsync() but we need to get the return
733 * code for the sys_close() here, not afs_linux_release(), so call
734 * afs_StoreAllSegments() with AFS_LASTSTORE
737 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
738 afs_linux_flush(struct file *fp, fl_owner_t id)
740 afs_linux_flush(struct file *fp)
743 struct vrequest *treq = NULL;
751 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
759 vcp = VTOAFS(FILE_INODE(fp));
761 code = afs_CreateReq(&treq, credp);
764 /* If caching is bypassed for this file, or globally, just return 0 */
765 if (cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
768 ObtainReadLock(&vcp->lock);
769 if (vcp->cachingStates & FCSBypass)
771 ReleaseReadLock(&vcp->lock);
774 /* future proof: don't rely on 0 return from afs_InitReq */
779 ObtainSharedLock(&vcp->lock, 535);
780 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
781 UpgradeSToWLock(&vcp->lock, 536);
782 if (!AFS_IS_DISCONNECTED) {
783 code = afs_StoreAllSegments(vcp,
785 AFS_SYNC | AFS_LASTSTORE);
787 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
789 ConvertWToSLock(&vcp->lock);
791 code = afs_CheckCode(code, treq, 54);
792 ReleaseSharedLock(&vcp->lock);
795 afs_DestroyReq(treq);
800 return afs_convert_code(code);
803 struct file_operations afs_dir_fops = {
804 .read = generic_read_dir,
805 #if defined(STRUCT_FILE_OPERATIONS_HAS_ITERATE)
806 .iterate = afs_linux_readdir,
808 .readdir = afs_linux_readdir,
810 #ifdef HAVE_UNLOCKED_IOCTL
811 .unlocked_ioctl = afs_unlocked_xioctl,
815 #ifdef HAVE_COMPAT_IOCTL
816 .compat_ioctl = afs_unlocked_xioctl,
818 .open = afs_linux_open,
819 .release = afs_linux_release,
820 .llseek = default_llseek,
821 #ifdef HAVE_LINUX_NOOP_FSYNC
824 .fsync = simple_sync_file,
828 struct file_operations afs_file_fops = {
829 #ifdef STRUCT_FILE_OPERATIONS_HAS_READ_ITER
830 .read_iter = afs_linux_read_iter,
831 .write_iter = afs_linux_write_iter,
832 .read = new_sync_read,
833 .write = new_sync_write,
834 #elif defined(HAVE_LINUX_GENERIC_FILE_AIO_READ)
835 .aio_read = afs_linux_aio_read,
836 .aio_write = afs_linux_aio_write,
837 .read = do_sync_read,
838 .write = do_sync_write,
840 .read = afs_linux_read,
841 .write = afs_linux_write,
843 #ifdef HAVE_UNLOCKED_IOCTL
844 .unlocked_ioctl = afs_unlocked_xioctl,
848 #ifdef HAVE_COMPAT_IOCTL
849 .compat_ioctl = afs_unlocked_xioctl,
851 .mmap = afs_linux_mmap,
852 .open = afs_linux_open,
853 .flush = afs_linux_flush,
854 #if defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
855 .sendfile = generic_file_sendfile,
857 #if defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
858 # if defined(HAVE_LINUX_ITER_FILE_SPLICE_WRITE)
859 .splice_write = iter_file_splice_write,
861 .splice_write = generic_file_splice_write,
863 .splice_read = generic_file_splice_read,
865 .release = afs_linux_release,
866 .fsync = afs_linux_fsync,
867 .lock = afs_linux_lock,
868 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
869 .flock = afs_linux_flock,
871 .llseek = default_llseek,
874 static struct dentry *
875 canonical_dentry(struct inode *ip)
877 struct vcache *vcp = VTOAFS(ip);
878 struct dentry *first = NULL, *ret = NULL, *cur;
879 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
880 struct hlist_node *p;
884 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
885 * otherwise, use the first dentry in ip->i_dentry.
886 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
888 /* note that vcp->target_link specifies which dentry to use, but we have
889 * no reference held on that dentry. so, we cannot use or dereference
890 * vcp->target_link itself, since it may have been freed. instead, we only
891 * use it to compare to pointers in the ip->i_dentry list. */
895 # ifdef HAVE_DCACHE_LOCK
896 spin_lock(&dcache_lock);
898 spin_lock(&ip->i_lock);
901 #if defined(D_ALIAS_IS_HLIST)
902 # if defined(HLIST_ITERATOR_NO_NODE)
903 hlist_for_each_entry(cur, &ip->i_dentry, d_alias) {
905 hlist_for_each_entry(cur, p, &ip->i_dentry, d_alias) {
908 list_for_each_entry_reverse(cur, &ip->i_dentry, d_alias) {
911 if (!vcp->target_link || cur == vcp->target_link) {
924 vcp->target_link = ret;
926 # ifdef HAVE_DCACHE_LOCK
930 spin_unlock(&dcache_lock);
935 spin_unlock(&ip->i_lock);
941 /**********************************************************************
942 * AFS Linux dentry operations
943 **********************************************************************/
945 /* fix_bad_parent() : called if this dentry's vcache is a root vcache
946 * that has its mvid (parent dir's fid) pointer set to the wrong directory
947 * due to being mounted in multiple points at once. fix_bad_parent()
948 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
949 * dotdotfid and mtpoint fid members.
951 * dp - dentry to be checked.
952 * credp - credentials
953 * vcp, pvc - item's and parent's vcache pointer
957 * This dentry's vcache's mvid will be set to the correct parent directory's
959 * This root vnode's volume will have its dotdotfid and mtpoint fids set
960 * to the correct parent and mountpoint fids.
964 fix_bad_parent(struct dentry *dp, cred_t *credp, struct vcache *vcp, struct vcache *pvc)
966 struct vcache *avc = NULL;
968 /* force a lookup, so vcp->mvid is fixed up */
969 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
970 if (!avc || vcp != avc) { /* bad, very bad.. */
971 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
972 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
973 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
974 ICL_TYPE_POINTER, dp);
977 AFS_RELE(AFSTOV(avc));
982 /* afs_linux_revalidate
983 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
986 afs_linux_revalidate(struct dentry *dp)
988 struct vattr *vattr = NULL;
989 struct vcache *vcp = VTOAFS(dp->d_inode);
993 if (afs_shuttingdown)
998 code = afs_CreateAttr(&vattr);
1004 /* Make this a fast path (no crref), since it's called so often. */
1005 if (vcp->states & CStatd) {
1006 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
1008 if (*dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
1009 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
1012 fix_bad_parent(dp); /* check and correct mvid */
1017 afs_DestroyAttr(vattr);
1022 /* This avoids the crref when we don't have to do it. Watch for
1023 * changes in afs_getattr that don't get replicated here!
1025 if (vcp->f.states & CStatd &&
1026 (!afs_fakestat_enable || vcp->mvstat != 1) &&
1028 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
1029 code = afs_CopyOutAttrs(vcp, vattr);
1032 code = afs_getattr(vcp, vattr, credp);
1037 afs_fill_inode(AFSTOV(vcp), vattr);
1039 afs_DestroyAttr(vattr);
1044 return afs_convert_code(code);
1048 * Set iattr data into vattr. Assume vattr cleared before call.
1051 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
1053 vattrp->va_mask = iattrp->ia_valid;
1054 if (iattrp->ia_valid & ATTR_MODE)
1055 vattrp->va_mode = iattrp->ia_mode;
1056 if (iattrp->ia_valid & ATTR_UID)
1057 vattrp->va_uid = afs_from_kuid(iattrp->ia_uid);
1058 if (iattrp->ia_valid & ATTR_GID)
1059 vattrp->va_gid = afs_from_kgid(iattrp->ia_gid);
1060 if (iattrp->ia_valid & ATTR_SIZE)
1061 vattrp->va_size = iattrp->ia_size;
1062 if (iattrp->ia_valid & ATTR_ATIME) {
1063 vattrp->va_atime.tv_sec = iattrp->ia_atime.tv_sec;
1064 vattrp->va_atime.tv_usec = 0;
1066 if (iattrp->ia_valid & ATTR_MTIME) {
1067 vattrp->va_mtime.tv_sec = iattrp->ia_mtime.tv_sec;
1068 vattrp->va_mtime.tv_usec = 0;
1070 if (iattrp->ia_valid & ATTR_CTIME) {
1071 vattrp->va_ctime.tv_sec = iattrp->ia_ctime.tv_sec;
1072 vattrp->va_ctime.tv_usec = 0;
1077 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
1080 vattr2inode(struct inode *ip, struct vattr *vp)
1082 ip->i_ino = vp->va_nodeid;
1083 #ifdef HAVE_LINUX_SET_NLINK
1084 set_nlink(ip, vp->va_nlink);
1086 ip->i_nlink = vp->va_nlink;
1088 ip->i_blocks = vp->va_blocks;
1089 #ifdef STRUCT_INODE_HAS_I_BLKBITS
1090 ip->i_blkbits = AFS_BLKBITS;
1092 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
1093 ip->i_blksize = vp->va_blocksize;
1095 ip->i_rdev = vp->va_rdev;
1096 ip->i_mode = vp->va_mode;
1097 ip->i_uid = afs_make_kuid(vp->va_uid);
1098 ip->i_gid = afs_make_kgid(vp->va_gid);
1099 i_size_write(ip, vp->va_size);
1100 ip->i_atime.tv_sec = vp->va_atime.tv_sec;
1101 ip->i_atime.tv_nsec = 0;
1102 ip->i_mtime.tv_sec = vp->va_mtime.tv_sec;
1103 /* Set the mtime nanoseconds to the sysname generation number.
1104 * This convinces NFS clients that all directories have changed
1105 * any time the sysname list changes.
1107 ip->i_mtime.tv_nsec = afs_sysnamegen;
1108 ip->i_ctime.tv_sec = vp->va_ctime.tv_sec;
1109 ip->i_ctime.tv_nsec = 0;
1112 /* afs_notify_change
1113 * Linux version of setattr call. What to change is in the iattr struct.
1114 * We need to set bits in both the Linux inode as well as the vcache.
1117 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1119 struct vattr *vattr = NULL;
1120 cred_t *credp = crref();
1121 struct inode *ip = dp->d_inode;
1125 code = afs_CreateAttr(&vattr);
1130 iattr2vattr(vattr, iattrp); /* Convert for AFS vnodeops call. */
1132 code = afs_setattr(VTOAFS(ip), vattr, credp);
1134 afs_getattr(VTOAFS(ip), vattr, credp);
1135 vattr2inode(ip, vattr);
1137 afs_DestroyAttr(vattr);
1142 return afs_convert_code(code);
1146 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1148 int err = afs_linux_revalidate(dentry);
1150 generic_fillattr(dentry->d_inode, stat);
1156 parent_vcache_dv(struct inode *inode, cred_t *credp)
1159 struct vcache *pvcp;
1162 * If parent is a mount point and we are using fakestat, we may need
1163 * to look at the fake vcache entry instead of what the vfs is giving
1164 * us. The fake entry is the one with the useful DataVersion.
1166 pvcp = VTOAFS(inode);
1167 if (pvcp->mvstat == 1 && afs_fakestat_enable) {
1168 struct vrequest treq;
1169 struct afs_fakestat_state fakestate;
1175 afs_InitReq(&treq, credp);
1176 afs_InitFakeStat(&fakestate);
1177 afs_TryEvalFakeStat(&pvcp, &fakestate, &treq);
1180 afs_PutFakeStat(&fakestate);
1182 return hgetlo(pvcp->f.m.DataVersion);
1185 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1186 * In kernels 2.2.10 and above, we are passed an additional flags var which
1187 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1188 * we are advised to follow the entry if it is a link or to make sure that
1189 * it is a directory. But since the kernel itself checks these possibilities
1190 * later on, we shouldn't have to do it until later. Perhaps in the future..
1192 * The code here assumes that on entry the global lock is not held
1195 #if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1196 afs_linux_dentry_revalidate(struct dentry *dp, unsigned int flags)
1197 #elif defined(DOP_REVALIDATE_TAKES_NAMEIDATA)
1198 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1200 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1203 cred_t *credp = NULL;
1204 struct vcache *vcp, *pvcp, *tvc = NULL;
1205 struct dentry *parent;
1207 struct afs_fakestat_state fakestate;
1210 afs_uint32 parent_dv;
1213 /* We don't support RCU path walking */
1214 # if defined(DOP_REVALIDATE_TAKES_UNSIGNED)
1215 if (flags & LOOKUP_RCU)
1217 if (nd->flags & LOOKUP_RCU)
1222 afs_InitFakeStat(&fakestate);
1225 vcp = VTOAFS(dp->d_inode);
1227 if (vcp == afs_globalVp)
1230 parent = dget_parent(dp);
1231 pvcp = VTOAFS(parent->d_inode);
1233 if ((vcp->mvstat == 1) || (vcp->mvstat == 2) ||
1234 (pvcp->mvstat == 1 && afs_fakestat_enable)) { /* need to lock */
1240 if (locked && vcp->mvstat == 1) { /* mount point */
1241 if (vcp->mvid && (vcp->f.states & CMValid)) {
1242 int tryEvalOnly = 0;
1244 struct vrequest *treq = NULL;
1246 code = afs_CreateReq(&treq, credp);
1251 if ((strcmp(dp->d_name.name, ".directory") == 0)) {
1255 code = afs_TryEvalFakeStat(&vcp, &fakestate, treq);
1257 code = afs_EvalFakeStat(&vcp, &fakestate, treq);
1258 afs_DestroyReq(treq);
1259 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1260 /* a mount point, not yet replaced by its directory */
1266 if (locked && *dp->d_name.name != '/' && vcp->mvstat == 2) { /* root vnode */
1267 if (vcp->mvid->Fid.Volume != pvcp->f.fid.Fid.Volume) { /* bad parent */
1268 fix_bad_parent(dp, credp, vcp, pvcp); /* check and correct mvid */
1273 /* If the last looker changes, we should make sure the current
1274 * looker still has permission to examine this file. This would
1275 * always require a crref() which would be "slow".
1277 if (vcp->last_looker != treq.uid) {
1278 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS)) {
1283 vcp->last_looker = treq.uid;
1287 parent_dv = parent_vcache_dv(parent->d_inode, credp);
1289 /* If the parent's DataVersion has changed or the vnode
1290 * is longer valid, we need to do a full lookup. VerifyVCache
1291 * isn't enough since the vnode may have been renamed.
1294 if ((!locked) && (parent_dv > dp->d_time || !(vcp->f.states & CStatd)) ) {
1300 if (locked && (parent_dv > dp->d_time || !(vcp->f.states & CStatd))) {
1301 struct vattr *vattr = NULL;
1304 code = afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1305 if (!tvc || tvc != vcp) {
1307 /* Force unhash if name is known not to exist. */
1313 code = afs_CreateAttr(&vattr);
1319 if (afs_getattr(vcp, vattr, credp)) {
1321 afs_DestroyAttr(vattr);
1325 vattr2inode(AFSTOV(vcp), vattr);
1326 dp->d_time = parent_dv;
1328 afs_DestroyAttr(vattr);
1331 /* should we always update the attributes at this point? */
1332 /* unlikely--the vcache entry hasn't changed */
1337 /* If this code is ever enabled, we should use dget_parent to handle
1338 * getting the parent, and dput() to dispose of it. See above for an
1340 pvcp = VTOAFS(dp->d_parent->d_inode);
1341 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1345 /* No change in parent's DataVersion so this negative
1346 * lookup is still valid. BUT, if a server is down a
1347 * negative lookup can result so there should be a
1348 * liftime as well. For now, always expire.
1361 afs_PutFakeStat(&fakestate); /* from here on vcp may be no longer valid */
1363 /* we hold the global lock if we evaluated a mount point */
1371 * If we had a negative lookup for the name we want to forcibly
1372 * unhash the dentry.
1373 * Otherwise use d_invalidate which will not unhash it if still in use.
1376 shrink_dcache_parent(dp);
1385 if (have_submounts(dp))
1393 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1395 struct vcache *vcp = VTOAFS(ip);
1398 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1399 (void) afs_InactiveVCache(vcp, NULL);
1402 afs_linux_clear_nfsfs_renamed(dp);
1408 #if defined(DOP_D_DELETE_TAKES_CONST)
1409 afs_dentry_delete(const struct dentry *dp)
1411 afs_dentry_delete(struct dentry *dp)
1414 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1415 return 1; /* bad inode? */
1420 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1421 static struct vfsmount *
1422 afs_dentry_automount(afs_linux_path_t *path)
1424 struct dentry *target;
1426 /* avoid symlink resolution limits when resolving; we cannot contribute to
1427 * an infinite symlink loop */
1428 current->total_link_count--;
1430 target = canonical_dentry(path->dentry->d_inode);
1432 if (target == path->dentry) {
1439 path->dentry = target;
1442 spin_lock(&path->dentry->d_lock);
1443 path->dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
1444 spin_unlock(&path->dentry->d_lock);
1449 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1451 struct dentry_operations afs_dentry_operations = {
1452 .d_revalidate = afs_linux_dentry_revalidate,
1453 .d_delete = afs_dentry_delete,
1454 .d_iput = afs_dentry_iput,
1455 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1456 .d_automount = afs_dentry_automount,
1457 #endif /* STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
1460 /**********************************************************************
1461 * AFS Linux inode operations
1462 **********************************************************************/
1466 * Merely need to set enough of vattr to get us through the create. Note
1467 * that the higher level code (open_namei) will take care of any tuncation
1468 * explicitly. Exclusive open is also taken care of in open_namei.
1470 * name is in kernel space at this point.
1473 #if defined(IOP_CREATE_TAKES_BOOL)
1474 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1476 #elif defined(IOP_CREATE_TAKES_UMODE_T)
1477 afs_linux_create(struct inode *dip, struct dentry *dp, umode_t mode,
1478 struct nameidata *nd)
1479 #elif defined(IOP_CREATE_TAKES_NAMEIDATA)
1480 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1481 struct nameidata *nd)
1483 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1486 struct vattr *vattr = NULL;
1487 cred_t *credp = crref();
1488 const char *name = dp->d_name.name;
1494 code = afs_CreateAttr(&vattr);
1498 vattr->va_mode = mode;
1499 vattr->va_type = mode & S_IFMT;
1501 code = afs_create(VTOAFS(dip), (char *)name, vattr, NONEXCL, mode,
1505 struct inode *ip = AFSTOV(vcp);
1507 afs_getattr(vcp, vattr, credp);
1508 afs_fill_inode(ip, vattr);
1509 insert_inode_hash(ip);
1510 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1511 dp->d_op = &afs_dentry_operations;
1513 dp->d_time = parent_vcache_dv(dip, credp);
1514 d_instantiate(dp, ip);
1517 afs_DestroyAttr(vattr);
1523 return afs_convert_code(code);
1526 /* afs_linux_lookup */
1527 static struct dentry *
1528 #if defined(IOP_LOOKUP_TAKES_UNSIGNED)
1529 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1531 #elif defined(IOP_LOOKUP_TAKES_NAMEIDATA)
1532 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1533 struct nameidata *nd)
1535 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1538 cred_t *credp = crref();
1539 struct vcache *vcp = NULL;
1540 const char *comp = dp->d_name.name;
1541 struct inode *ip = NULL;
1542 struct dentry *newdp = NULL;
1546 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1549 struct vattr *vattr = NULL;
1550 struct vcache *parent_vc = VTOAFS(dip);
1552 if (parent_vc == vcp) {
1553 /* This is possible if the parent dir is a mountpoint to a volume,
1554 * and the dir entry we looked up is a mountpoint to the same
1555 * volume. Linux cannot cope with this, so return an error instead
1556 * of risking a deadlock or panic. */
1563 code = afs_CreateAttr(&vattr);
1571 afs_getattr(vcp, vattr, credp);
1572 afs_fill_inode(ip, vattr);
1573 if (hlist_unhashed(&ip->i_hash))
1574 insert_inode_hash(ip);
1576 afs_DestroyAttr(vattr);
1578 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1579 dp->d_op = &afs_dentry_operations;
1581 dp->d_time = parent_vcache_dv(dip, credp);
1585 if (ip && S_ISDIR(ip->i_mode)) {
1586 d_prune_aliases(ip);
1588 #ifdef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
1589 ip->i_flags |= S_AUTOMOUNT;
1592 newdp = d_splice_alias(ip, dp);
1597 /* It's ok for the file to not be found. That's noted by the caller by
1598 * seeing that the dp->d_inode field is NULL.
1600 if (!code || code == ENOENT)
1603 return ERR_PTR(afs_convert_code(code));
1607 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1610 cred_t *credp = crref();
1611 const char *name = newdp->d_name.name;
1612 struct inode *oldip = olddp->d_inode;
1614 /* If afs_link returned the vnode, we could instantiate the
1615 * dentry. Since it's not, we drop this one and do a new lookup.
1620 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1624 return afs_convert_code(code);
1627 /* We have to have a Linux specific sillyrename function, because we
1628 * also have to keep the dcache up to date when we're doing a silly
1629 * rename - so we don't want the generic vnodeops doing this behind our
1634 afs_linux_sillyrename(struct inode *dir, struct dentry *dentry,
1637 struct vcache *tvc = VTOAFS(dentry->d_inode);
1638 struct dentry *__dp = NULL;
1639 char *__name = NULL;
1642 if (afs_linux_nfsfs_renamed(dentry))
1650 osi_FreeSmallSpace(__name);
1651 __name = afs_newname();
1654 __dp = lookup_one_len(__name, dentry->d_parent, strlen(__name));
1657 osi_FreeSmallSpace(__name);
1660 } while (__dp->d_inode != NULL);
1663 code = afs_rename(VTOAFS(dir), (char *)dentry->d_name.name,
1664 VTOAFS(dir), (char *)__dp->d_name.name,
1667 tvc->mvid = (void *) __name;
1670 crfree(tvc->uncred);
1672 tvc->uncred = credp;
1673 tvc->f.states |= CUnlinked;
1674 afs_linux_set_nfsfs_renamed(dentry);
1676 osi_FreeSmallSpace(__name);
1681 __dp->d_time = hgetlo(VTOAFS(dir)->f.m.DataVersion);
1682 d_move(dentry, __dp);
1691 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1694 cred_t *credp = crref();
1695 const char *name = dp->d_name.name;
1696 struct vcache *tvc = VTOAFS(dp->d_inode);
1698 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1699 && !(tvc->f.states & CUnlinked)) {
1701 code = afs_linux_sillyrename(dip, dp, credp);
1704 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1711 return afs_convert_code(code);
1716 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1719 cred_t *credp = crref();
1720 struct vattr *vattr = NULL;
1721 const char *name = dp->d_name.name;
1723 /* If afs_symlink returned the vnode, we could instantiate the
1724 * dentry. Since it's not, we drop this one and do a new lookup.
1729 code = afs_CreateAttr(&vattr);
1734 code = afs_symlink(VTOAFS(dip), (char *)name, vattr, (char *)target, NULL,
1736 afs_DestroyAttr(vattr);
1741 return afs_convert_code(code);
1745 #if defined(IOP_MKDIR_TAKES_UMODE_T)
1746 afs_linux_mkdir(struct inode *dip, struct dentry *dp, umode_t mode)
1748 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1752 cred_t *credp = crref();
1753 struct vcache *tvcp = NULL;
1754 struct vattr *vattr = NULL;
1755 const char *name = dp->d_name.name;
1758 code = afs_CreateAttr(&vattr);
1763 vattr->va_mask = ATTR_MODE;
1764 vattr->va_mode = mode;
1766 code = afs_mkdir(VTOAFS(dip), (char *)name, vattr, &tvcp, credp);
1769 struct inode *ip = AFSTOV(tvcp);
1771 afs_getattr(tvcp, vattr, credp);
1772 afs_fill_inode(ip, vattr);
1774 #if !defined(STRUCT_SUPER_BLOCK_HAS_S_D_OP)
1775 dp->d_op = &afs_dentry_operations;
1777 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1778 d_instantiate(dp, ip);
1780 afs_DestroyAttr(vattr);
1786 return afs_convert_code(code);
1790 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1793 cred_t *credp = crref();
1794 const char *name = dp->d_name.name;
1796 /* locking kernel conflicts with glock? */
1799 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1802 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1803 * that failed because a directory is not empty. So, we map
1804 * EEXIST to ENOTEMPTY on linux.
1806 if (code == EEXIST) {
1815 return afs_convert_code(code);
1820 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1821 struct inode *newip, struct dentry *newdp)
1824 cred_t *credp = crref();
1825 const char *oldname = olddp->d_name.name;
1826 const char *newname = newdp->d_name.name;
1827 struct dentry *rehash = NULL;
1829 /* Prevent any new references during rename operation. */
1831 if (!d_unhashed(newdp)) {
1836 afs_maybe_shrink_dcache(olddp);
1839 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1843 olddp->d_time = 0; /* force to revalidate */
1849 return afs_convert_code(code);
1853 /* afs_linux_ireadlink
1854 * Internal readlink which can return link contents to user or kernel space.
1855 * Note that the buffer is NOT supposed to be null-terminated.
1858 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1861 cred_t *credp = crref();
1865 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1866 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1870 return maxlen - tuio.uio_resid;
1872 return afs_convert_code(code);
1875 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1876 /* afs_linux_readlink
1877 * Fill target (which is in user space) with contents of symlink.
1880 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1883 struct inode *ip = dp->d_inode;
1886 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1892 /* afs_linux_follow_link
1893 * a file system dependent link following routine.
1895 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1900 name = kmalloc(PATH_MAX, GFP_NOFS);
1906 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1914 nd_set_link(nd, name);
1919 afs_linux_put_link(struct dentry *dentry, struct nameidata *nd)
1921 char *name = nd_get_link(nd);
1923 if (name && !IS_ERR(name))
1927 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1929 /* Populate a page by filling it from the cache file pointed at by cachefp
1930 * (which contains indicated chunk)
1931 * If task is NULL, the page copy occurs syncronously, and the routine
1932 * returns with page still locked. If task is non-NULL, then page copies
1933 * may occur in the background, and the page will be unlocked when it is
1937 afs_linux_read_cache(struct file *cachefp, struct page *page,
1938 int chunk, struct pagevec *lrupv,
1939 struct afs_pagecopy_task *task) {
1940 loff_t offset = page_offset(page);
1941 struct inode *cacheinode = cachefp->f_dentry->d_inode;
1942 struct page *newpage, *cachepage;
1943 struct address_space *cachemapping;
1947 cachemapping = cacheinode->i_mapping;
1951 /* If we're trying to read a page that's past the end of the disk
1952 * cache file, then just return a zeroed page */
1953 if (AFS_CHUNKOFFSET(offset) >= i_size_read(cacheinode)) {
1954 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1955 SetPageUptodate(page);
1961 /* From our offset, we now need to work out which page in the disk
1962 * file it corresponds to. This will be fun ... */
1963 pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1965 while (cachepage == NULL) {
1966 cachepage = find_get_page(cachemapping, pageindex);
1969 newpage = page_cache_alloc_cold(cachemapping);
1975 code = add_to_page_cache(newpage, cachemapping,
1976 pageindex, GFP_KERNEL);
1978 cachepage = newpage;
1981 page_cache_get(cachepage);
1982 if (!pagevec_add(lrupv, cachepage))
1983 __pagevec_lru_add_file(lrupv);
1986 page_cache_release(newpage);
1988 if (code != -EEXIST)
1992 lock_page(cachepage);
1996 if (!PageUptodate(cachepage)) {
1997 ClearPageError(cachepage);
1998 code = cachemapping->a_ops->readpage(NULL, cachepage);
1999 if (!code && !task) {
2000 wait_on_page_locked(cachepage);
2003 unlock_page(cachepage);
2007 if (PageUptodate(cachepage)) {
2008 copy_highpage(page, cachepage);
2009 flush_dcache_page(page);
2010 SetPageUptodate(page);
2015 afs_pagecopy_queue_page(task, cachepage, page);
2027 page_cache_release(cachepage);
2033 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
2035 loff_t offset = page_offset(pp);
2036 struct inode *ip = FILE_INODE(fp);
2037 struct vcache *avc = VTOAFS(ip);
2039 struct file *cacheFp = NULL;
2042 struct pagevec lrupv;
2044 /* Not a UFS cache, don't do anything */
2045 if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
2048 /* No readpage (ex: tmpfs) , skip */
2049 if (cachefs_noreadpage)
2052 /* Can't do anything if the vcache isn't statd , or if the read
2053 * crosses a chunk boundary.
2055 if (!(avc->f.states & CStatd) ||
2056 AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
2060 ObtainWriteLock(&avc->lock, 911);
2062 /* XXX - See if hinting actually makes things faster !!! */
2064 /* See if we have a suitable entry already cached */
2068 /* We need to lock xdcache, then dcache, to handle situations where
2069 * the hint is on the free list. However, we can't safely do this
2070 * according to the locking hierarchy. So, use a non blocking lock.
2072 ObtainReadLock(&afs_xdcache);
2073 dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
2075 if (dcLocked && (tdc->index != NULLIDX)
2076 && !FidCmp(&tdc->f.fid, &avc->f.fid)
2077 && tdc->f.chunk == AFS_CHUNK(offset)
2078 && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
2079 /* Bonus - the hint was correct */
2082 /* Only destroy the hint if its actually invalid, not if there's
2083 * just been a locking failure */
2085 ReleaseReadLock(&tdc->lock);
2092 ReleaseReadLock(&afs_xdcache);
2095 /* No hint, or hint is no longer valid - see if we can get something
2096 * directly from the dcache
2099 tdc = afs_FindDCache(avc, offset);
2102 ReleaseWriteLock(&avc->lock);
2107 ObtainReadLock(&tdc->lock);
2109 /* Is the dcache we've been given currently up to date */
2110 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2111 (tdc->dflags & DFFetching))
2114 /* Update our hint for future abuse */
2117 /* Okay, so we've now got a cache file that is up to date */
2119 /* XXX - I suspect we should be locking the inodes before we use them! */
2121 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2122 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2123 cachefs_noreadpage = 1;
2127 pagevec_init(&lrupv, 0);
2129 code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
2131 if (pagevec_count(&lrupv))
2132 __pagevec_lru_add_file(&lrupv);
2134 filp_close(cacheFp, NULL);
2137 ReleaseReadLock(&tdc->lock);
2138 ReleaseWriteLock(&avc->lock);
2145 ReleaseWriteLock(&avc->lock);
2146 ReleaseReadLock(&tdc->lock);
2151 /* afs_linux_readpage
2153 * This function is split into two, because prepare_write/begin_write
2154 * require a readpage call which doesn't unlock the resulting page upon
2158 afs_linux_fillpage(struct file *fp, struct page *pp)
2163 struct iovec *iovecp;
2164 struct inode *ip = FILE_INODE(fp);
2165 afs_int32 cnt = page_count(pp);
2166 struct vcache *avc = VTOAFS(ip);
2167 afs_offs_t offset = page_offset(pp);
2171 if (afs_linux_readpage_fastpath(fp, pp, &code)) {
2181 auio = kmalloc(sizeof(struct uio), GFP_NOFS);
2182 iovecp = kmalloc(sizeof(struct iovec), GFP_NOFS);
2184 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
2189 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2190 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2191 99999); /* not a possible code value */
2193 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
2195 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
2196 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
2198 AFS_DISCON_UNLOCK();
2201 /* XXX valid for no-cache also? Check last bits of files... :)
2202 * Cognate code goes in afs_NoCacheFetchProc. */
2203 if (auio->uio_resid) /* zero remainder of page */
2204 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
2207 flush_dcache_page(pp);
2208 SetPageUptodate(pp);
2217 return afs_convert_code(code);
2221 afs_linux_prefetch(struct file *fp, struct page *pp)
2224 struct vcache *avc = VTOAFS(FILE_INODE(fp));
2225 afs_offs_t offset = page_offset(pp);
2227 if (AFS_CHUNKOFFSET(offset) == 0) {
2229 struct vrequest *treq = NULL;
2234 code = afs_CreateReq(&treq, credp);
2235 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
2236 tdc = afs_FindDCache(avc, offset);
2238 if (!(tdc->mflags & DFNextStarted))
2239 afs_PrefetchChunk(avc, tdc, credp, treq);
2242 ReleaseWriteLock(&avc->lock);
2244 afs_DestroyReq(treq);
2248 return afs_convert_code(code);
2253 afs_linux_bypass_readpages(struct file *fp, struct address_space *mapping,
2254 struct list_head *page_list, unsigned num_pages)
2259 struct iovec* iovecp;
2260 struct nocache_read_request *ancr;
2262 struct pagevec lrupv;
2266 struct inode *ip = FILE_INODE(fp);
2267 struct vcache *avc = VTOAFS(ip);
2268 afs_int32 base_index = 0;
2269 afs_int32 page_count = 0;
2272 /* background thread must free: iovecp, auio, ancr */
2273 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
2275 auio = osi_Alloc(sizeof(struct uio));
2276 auio->uio_iov = iovecp;
2277 auio->uio_iovcnt = num_pages;
2278 auio->uio_flag = UIO_READ;
2279 auio->uio_seg = AFS_UIOSYS;
2280 auio->uio_resid = num_pages * PAGE_SIZE;
2282 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2284 ancr->offset = auio->uio_offset;
2285 ancr->length = auio->uio_resid;
2287 pagevec_init(&lrupv, 0);
2289 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
2291 if(list_empty(page_list))
2294 pp = list_entry(page_list->prev, struct page, lru);
2295 /* If we allocate a page and don't remove it from page_list,
2296 * the page cache gets upset. */
2298 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
2299 if(pp->index > isize) {
2306 offset = page_offset(pp);
2307 ancr->offset = auio->uio_offset = offset;
2308 base_index = pp->index;
2310 iovecp[page_ix].iov_len = PAGE_SIZE;
2311 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
2312 if(base_index != pp->index) {
2315 page_cache_release(pp);
2316 iovecp[page_ix].iov_base = (void *) 0;
2318 ancr->length -= PAGE_SIZE;
2325 page_cache_release(pp);
2326 iovecp[page_ix].iov_base = (void *) 0;
2329 if(!PageLocked(pp)) {
2333 /* increment page refcount--our original design assumed
2334 * that locking it would effectively pin it; protect
2335 * ourselves from the possiblity that this assumption is
2336 * is faulty, at low cost (provided we do not fail to
2337 * do the corresponding decref on the other side) */
2340 /* save the page for background map */
2341 iovecp[page_ix].iov_base = (void*) pp;
2343 /* and put it on the LRU cache */
2344 if (!pagevec_add(&lrupv, pp))
2345 __pagevec_lru_add_file(&lrupv);
2349 /* If there were useful pages in the page list, make sure all pages
2350 * are in the LRU cache, then schedule the read */
2352 if (pagevec_count(&lrupv))
2353 __pagevec_lru_add_file(&lrupv);
2355 code = afs_ReadNoCache(avc, ancr, credp);
2358 /* If there is nothing for the background thread to handle,
2359 * it won't be freeing the things that we never gave it */
2360 osi_Free(iovecp, num_pages * sizeof(struct iovec));
2361 osi_Free(auio, sizeof(struct uio));
2362 osi_Free(ancr, sizeof(struct nocache_read_request));
2364 /* we do not flush, release, or unmap pages--that will be
2365 * done for us by the background thread as each page comes in
2366 * from the fileserver */
2367 return afs_convert_code(code);
2372 afs_linux_bypass_readpage(struct file *fp, struct page *pp)
2374 cred_t *credp = NULL;
2376 struct iovec *iovecp;
2377 struct nocache_read_request *ancr;
2381 * Special case: if page is at or past end of file, just zero it and set
2384 if (page_offset(pp) >= i_size_read(fp->f_mapping->host)) {
2385 zero_user_segment(pp, 0, PAGE_CACHE_SIZE);
2386 SetPageUptodate(pp);
2393 /* receiver frees */
2394 auio = osi_Alloc(sizeof(struct uio));
2395 iovecp = osi_Alloc(sizeof(struct iovec));
2397 /* address can be NULL, because we overwrite it with 'pp', below */
2398 setup_uio(auio, iovecp, NULL, page_offset(pp),
2399 PAGE_SIZE, UIO_READ, AFS_UIOSYS);
2401 /* save the page for background map */
2402 get_page(pp); /* see above */
2403 auio->uio_iov->iov_base = (void*) pp;
2404 /* the background thread will free this */
2405 ancr = osi_Alloc(sizeof(struct nocache_read_request));
2407 ancr->offset = page_offset(pp);
2408 ancr->length = PAGE_SIZE;
2411 code = afs_ReadNoCache(VTOAFS(FILE_INODE(fp)), ancr, credp);
2414 return afs_convert_code(code);
2418 afs_linux_can_bypass(struct inode *ip) {
2420 switch(cache_bypass_strategy) {
2421 case NEVER_BYPASS_CACHE:
2423 case ALWAYS_BYPASS_CACHE:
2425 case LARGE_FILES_BYPASS_CACHE:
2426 if (i_size_read(ip) > cache_bypass_threshold)
2433 /* Check if a file is permitted to bypass the cache by policy, and modify
2434 * the cache bypass state recorded for that file */
2437 afs_linux_bypass_check(struct inode *ip) {
2440 int bypass = afs_linux_can_bypass(ip);
2443 trydo_cache_transition(VTOAFS(ip), credp, bypass);
2451 afs_linux_readpage(struct file *fp, struct page *pp)
2455 if (afs_linux_bypass_check(FILE_INODE(fp))) {
2456 code = afs_linux_bypass_readpage(fp, pp);
2458 code = afs_linux_fillpage(fp, pp);
2460 code = afs_linux_prefetch(fp, pp);
2467 /* Readpages reads a number of pages for a particular file. We use
2468 * this to optimise the reading, by limiting the number of times upon which
2469 * we have to lookup, lock and open vcaches and dcaches
2473 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2474 struct list_head *page_list, unsigned int num_pages)
2476 struct inode *inode = mapping->host;
2477 struct vcache *avc = VTOAFS(inode);
2479 struct file *cacheFp = NULL;
2481 unsigned int page_idx;
2483 struct pagevec lrupv;
2484 struct afs_pagecopy_task *task;
2486 if (afs_linux_bypass_check(inode))
2487 return afs_linux_bypass_readpages(fp, mapping, page_list, num_pages);
2489 if (cacheDiskType == AFS_FCACHE_TYPE_MEM)
2492 /* No readpage (ex: tmpfs) , skip */
2493 if (cachefs_noreadpage)
2497 if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2502 ObtainWriteLock(&avc->lock, 912);
2505 task = afs_pagecopy_init_task();
2508 pagevec_init(&lrupv, 0);
2509 for (page_idx = 0; page_idx < num_pages; page_idx++) {
2510 struct page *page = list_entry(page_list->prev, struct page, lru);
2511 list_del(&page->lru);
2512 offset = page_offset(page);
2514 if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2516 ReleaseReadLock(&tdc->lock);
2521 filp_close(cacheFp, NULL);
2526 if ((tdc = afs_FindDCache(avc, offset))) {
2527 ObtainReadLock(&tdc->lock);
2528 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2529 (tdc->dflags & DFFetching)) {
2530 ReleaseReadLock(&tdc->lock);
2537 cacheFp = afs_linux_raw_open(&tdc->f.inode);
2538 if (!cacheFp->f_dentry->d_inode->i_mapping->a_ops->readpage) {
2539 cachefs_noreadpage = 1;
2545 if (tdc && !add_to_page_cache(page, mapping, page->index,
2547 page_cache_get(page);
2548 if (!pagevec_add(&lrupv, page))
2549 __pagevec_lru_add_file(&lrupv);
2551 afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2553 page_cache_release(page);
2555 if (pagevec_count(&lrupv))
2556 __pagevec_lru_add_file(&lrupv);
2560 filp_close(cacheFp, NULL);
2562 afs_pagecopy_put_task(task);
2566 ReleaseReadLock(&tdc->lock);
2570 ReleaseWriteLock(&avc->lock);
2575 /* Prepare an AFS vcache for writeback. Should be called with the vcache
2578 afs_linux_prepare_writeback(struct vcache *avc) {
2579 if (avc->f.states & CPageWrite) {
2580 return AOP_WRITEPAGE_ACTIVATE;
2582 avc->f.states |= CPageWrite;
2587 afs_linux_dopartialwrite(struct vcache *avc, cred_t *credp) {
2588 struct vrequest *treq = NULL;
2591 if (!afs_CreateReq(&treq, credp)) {
2592 code = afs_DoPartialWrite(avc, treq);
2593 afs_DestroyReq(treq);
2596 return afs_convert_code(code);
2600 afs_linux_complete_writeback(struct vcache *avc) {
2601 avc->f.states &= ~CPageWrite;
2604 /* Writeback a given page syncronously. Called with no AFS locks held */
2606 afs_linux_page_writeback(struct inode *ip, struct page *pp,
2607 unsigned long offset, unsigned int count,
2610 struct vcache *vcp = VTOAFS(ip);
2618 buffer = kmap(pp) + offset;
2619 base = page_offset(pp) + offset;
2622 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2623 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2624 ICL_TYPE_INT32, 99999);
2626 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2628 code = afs_write(vcp, &tuio, f_flags, credp, 0);
2630 i_size_write(ip, vcp->f.m.Length);
2631 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2633 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2635 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2636 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2637 ICL_TYPE_INT32, code);
2646 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2647 unsigned long offset, unsigned int count)
2651 struct vcache *vcp = VTOAFS(ip);
2654 /* Catch recursive writeback. This occurs if the kernel decides
2655 * writeback is required whilst we are writing to the cache, or
2656 * flushing to the server. When we're running syncronously (as
2657 * opposed to from writepage) we can't actually do anything about
2658 * this case - as we can't return AOP_WRITEPAGE_ACTIVATE to write()
2661 ObtainWriteLock(&vcp->lock, 532);
2662 afs_linux_prepare_writeback(vcp);
2663 ReleaseWriteLock(&vcp->lock);
2667 code = afs_linux_page_writeback(ip, pp, offset, count, credp);
2670 ObtainWriteLock(&vcp->lock, 533);
2672 code1 = afs_linux_dopartialwrite(vcp, credp);
2673 afs_linux_complete_writeback(vcp);
2674 ReleaseWriteLock(&vcp->lock);
2685 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2686 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2688 afs_linux_writepage(struct page *pp)
2691 struct address_space *mapping = pp->mapping;
2692 struct inode *inode;
2695 unsigned int to = PAGE_CACHE_SIZE;
2702 inode = mapping->host;
2703 vcp = VTOAFS(inode);
2704 isize = i_size_read(inode);
2706 /* Don't defeat an earlier truncate */
2707 if (page_offset(pp) > isize) {
2708 set_page_writeback(pp);
2714 ObtainWriteLock(&vcp->lock, 537);
2715 code = afs_linux_prepare_writeback(vcp);
2716 if (code == AOP_WRITEPAGE_ACTIVATE) {
2717 /* WRITEPAGE_ACTIVATE is the only return value that permits us
2718 * to return with the page still locked */
2719 ReleaseWriteLock(&vcp->lock);
2724 /* Grab the creds structure currently held in the vnode, and
2725 * get a reference to it, in case it goes away ... */
2731 ReleaseWriteLock(&vcp->lock);
2734 set_page_writeback(pp);
2736 SetPageUptodate(pp);
2738 /* We can unlock the page here, because it's protected by the
2739 * page_writeback flag. This should make us less vulnerable to
2740 * deadlocking in afs_write and afs_DoPartialWrite
2744 /* If this is the final page, then just write the number of bytes that
2745 * are actually in it */
2746 if ((isize - page_offset(pp)) < to )
2747 to = isize - page_offset(pp);
2749 code = afs_linux_page_writeback(inode, pp, 0, to, credp);
2752 ObtainWriteLock(&vcp->lock, 538);
2754 /* As much as we might like to ignore a file server error here,
2755 * and just try again when we close(), unfortunately StoreAllSegments
2756 * will invalidate our chunks if the server returns a permanent error,
2757 * so we need to at least try and get that error back to the user
2760 code1 = afs_linux_dopartialwrite(vcp, credp);
2762 afs_linux_complete_writeback(vcp);
2763 ReleaseWriteLock(&vcp->lock);
2768 end_page_writeback(pp);
2769 page_cache_release(pp);
2780 /* afs_linux_permission
2781 * Check access rights - returns error if can't check or permission denied.
2784 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2785 afs_linux_permission(struct inode *ip, int mode, unsigned int flags)
2786 #elif defined(IOP_PERMISSION_TAKES_NAMEIDATA)
2787 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2789 afs_linux_permission(struct inode *ip, int mode)
2796 /* Check for RCU path walking */
2797 #if defined(IOP_PERMISSION_TAKES_FLAGS)
2798 if (flags & IPERM_FLAG_RCU)
2800 #elif defined(MAY_NOT_BLOCK)
2801 if (mode & MAY_NOT_BLOCK)
2807 if (mode & MAY_EXEC)
2809 if (mode & MAY_READ)
2811 if (mode & MAY_WRITE)
2813 code = afs_access(VTOAFS(ip), tmp, credp);
2817 return afs_convert_code(code);
2821 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2825 struct inode *inode = FILE_INODE(file);
2826 loff_t pagebase = page_offset(page);
2828 if (i_size_read(inode) < (pagebase + offset))
2829 i_size_write(inode, pagebase + offset);
2831 if (PageChecked(page)) {
2832 SetPageUptodate(page);
2833 ClearPageChecked(page);
2836 code = afs_linux_writepage_sync(inode, page, offset, to - offset);
2842 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2846 /* http://kerneltrap.org/node/4941 details the expected behaviour of
2847 * prepare_write. Essentially, if the page exists within the file,
2848 * and is not being fully written, then we should populate it.
2851 if (!PageUptodate(page)) {
2852 loff_t pagebase = page_offset(page);
2853 loff_t isize = i_size_read(page->mapping->host);
2855 /* Is the location we are writing to beyond the end of the file? */
2856 if (pagebase >= isize ||
2857 ((from == 0) && (pagebase + to) >= isize)) {
2858 zero_user_segments(page, 0, from, to, PAGE_CACHE_SIZE);
2859 SetPageChecked(page);
2860 /* Are we we writing a full page */
2861 } else if (from == 0 && to == PAGE_CACHE_SIZE) {
2862 SetPageChecked(page);
2863 /* Is the page readable, if it's wronly, we don't care, because we're
2864 * not actually going to read from it ... */
2865 } else if ((file->f_flags && O_ACCMODE) != O_WRONLY) {
2866 /* We don't care if fillpage fails, because if it does the page
2867 * won't be marked as up to date
2869 afs_linux_fillpage(file, page);
2875 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2877 afs_linux_write_end(struct file *file, struct address_space *mapping,
2878 loff_t pos, unsigned len, unsigned copied,
2879 struct page *page, void *fsdata)
2882 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2884 code = afs_linux_commit_write(file, page, from, from + len);
2887 page_cache_release(page);
2892 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2893 loff_t pos, unsigned len, unsigned flags,
2894 struct page **pagep, void **fsdata)
2897 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2898 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
2901 page = grab_cache_page_write_begin(mapping, index, flags);
2904 code = afs_linux_prepare_write(file, page, from, from + len);
2907 page_cache_release(page);
2914 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2916 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2918 struct dentry **dpp;
2919 struct dentry *target;
2921 if (current->total_link_count > 0) {
2922 /* avoid symlink resolution limits when resolving; we cannot contribute to
2923 * an infinite symlink loop */
2924 /* only do this for follow_link when total_link_count is positive to be
2925 * on the safe side; there is at least one code path in the Linux
2926 * kernel where it seems like it may be possible to get here without
2927 * total_link_count getting incremented. it is not clear on how that
2928 * path is actually reached, but guard against it just to be safe */
2929 current->total_link_count--;
2932 target = canonical_dentry(dentry->d_inode);
2934 # ifdef STRUCT_NAMEIDATA_HAS_PATH
2935 dpp = &nd->path.dentry;
2945 *dpp = dget(dentry);
2948 nd->last_type = LAST_BIND;
2952 #endif /* !STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT */
2955 static struct inode_operations afs_file_iops = {
2956 .permission = afs_linux_permission,
2957 .getattr = afs_linux_getattr,
2958 .setattr = afs_notify_change,
2961 static struct address_space_operations afs_file_aops = {
2962 .readpage = afs_linux_readpage,
2963 .readpages = afs_linux_readpages,
2964 .writepage = afs_linux_writepage,
2965 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2966 .write_begin = afs_linux_write_begin,
2967 .write_end = afs_linux_write_end,
2969 .commit_write = afs_linux_commit_write,
2970 .prepare_write = afs_linux_prepare_write,
2975 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2976 * by what sort of operation is allowed.....
2979 static struct inode_operations afs_dir_iops = {
2980 .setattr = afs_notify_change,
2981 .create = afs_linux_create,
2982 .lookup = afs_linux_lookup,
2983 .link = afs_linux_link,
2984 .unlink = afs_linux_unlink,
2985 .symlink = afs_linux_symlink,
2986 .mkdir = afs_linux_mkdir,
2987 .rmdir = afs_linux_rmdir,
2988 .rename = afs_linux_rename,
2989 .getattr = afs_linux_getattr,
2990 .permission = afs_linux_permission,
2991 #ifndef STRUCT_DENTRY_OPERATIONS_HAS_D_AUTOMOUNT
2992 .follow_link = afs_linux_dir_follow_link,
2996 /* We really need a separate symlink set of ops, since do_follow_link()
2997 * determines if it _is_ a link by checking if the follow_link op is set.
2999 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3001 afs_symlink_filler(struct file *file, struct page *page)
3003 struct inode *ip = (struct inode *)page->mapping->host;
3004 char *p = (char *)kmap(page);
3008 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
3013 p[code] = '\0'; /* null terminate? */
3015 SetPageUptodate(page);
3027 static struct address_space_operations afs_symlink_aops = {
3028 .readpage = afs_symlink_filler
3030 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3032 static struct inode_operations afs_symlink_iops = {
3033 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3034 .readlink = page_readlink,
3035 # if defined(HAVE_LINUX_PAGE_FOLLOW_LINK)
3036 .follow_link = page_follow_link,
3038 .follow_link = page_follow_link_light,
3039 .put_link = page_put_link,
3041 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
3042 .readlink = afs_linux_readlink,
3043 .follow_link = afs_linux_follow_link,
3044 .put_link = afs_linux_put_link,
3045 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
3046 .setattr = afs_notify_change,
3050 afs_fill_inode(struct inode *ip, struct vattr *vattr)
3054 vattr2inode(ip, vattr);
3056 ip->i_mapping->backing_dev_info = afs_backing_dev_info;
3057 /* Reset ops if symlink or directory. */
3058 if (S_ISREG(ip->i_mode)) {
3059 ip->i_op = &afs_file_iops;
3060 ip->i_fop = &afs_file_fops;
3061 ip->i_data.a_ops = &afs_file_aops;
3063 } else if (S_ISDIR(ip->i_mode)) {
3064 ip->i_op = &afs_dir_iops;
3065 ip->i_fop = &afs_dir_fops;
3067 } else if (S_ISLNK(ip->i_mode)) {
3068 ip->i_op = &afs_symlink_iops;
3069 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
3070 ip->i_data.a_ops = &afs_symlink_aops;
3071 ip->i_mapping = &ip->i_data;