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"
27 #include "afs/sysincludes.h"
28 #include "afsincludes.h"
29 #include "afs/afs_stats.h"
31 #ifdef HAVE_MM_INLINE_H
32 #include "h/mm_inline.h"
34 #include "h/pagemap.h"
35 #if defined(AFS_LINUX24_ENV)
36 #include "h/smp_lock.h"
38 #if defined(AFS_LINUX26_ENV)
39 #include "h/writeback.h"
40 #include "h/pagevec.h"
42 #if defined(AFS_CACHE_BYPASS)
44 #include "afs/afs_bypasscache.h"
48 #define pageoff(pp) pgoff2loff((pp)->index)
50 #define pageoff(pp) pp->offset
53 #if defined(AFS_LINUX26_ENV)
54 #define LockPage(pp) lock_page(pp)
55 #define UnlockPage(pp) unlock_page(pp)
58 extern struct vcache *afs_globalVp;
59 #if defined(AFS_LINUX26_ENV)
60 /* Some uses of BKL are perhaps not needed for bypass or memcache--
61 * why don't we try it out? */
62 extern struct afs_cacheOps afs_UfsCacheOps;
63 #define maybe_lock_kernel() \
65 if(afs_cacheType == &afs_UfsCacheOps) \
70 #define maybe_unlock_kernel() \
72 if(afs_cacheType == &afs_UfsCacheOps) \
75 #endif /* AFS_CACHE_BYPASS */
78 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
81 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
82 cred_t *credp = crref();
84 afs_size_t isize, offindex;
86 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
87 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
89 /* get a validated vcache entry */
90 code = afs_InitReq(&treq, credp);
92 code = afs_VerifyVCache(vcp, &treq);
97 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
98 offindex = *offp >> PAGE_CACHE_SHIFT;
99 if(offindex > isize) {
104 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
107 code = do_sync_read(fp, buf, count, offp);
109 code = generic_file_read(fp, buf, count, offp);
114 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
115 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
124 /* Now we have integrated VM for writes as well as reads. generic_file_write
125 * also takes care of re-positioning the pointer if file is open in append
126 * mode. Call fake open/close to ensure we do writes of core dumps.
129 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
132 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
133 struct vrequest treq;
134 cred_t *credp = crref();
138 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
139 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
140 (fp->f_flags & O_APPEND) ? 99998 : 99999);
143 /* get a validated vcache entry */
144 code = (ssize_t) afs_InitReq(&treq, credp);
146 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
148 ObtainWriteLock(&vcp->lock, 529);
150 ReleaseWriteLock(&vcp->lock);
156 code = do_sync_write(fp, buf, count, offp);
158 code = generic_file_write(fp, buf, count, offp);
163 ObtainWriteLock(&vcp->lock, 530);
164 afs_FakeClose(vcp, credp);
165 ReleaseWriteLock(&vcp->lock);
167 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
168 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
176 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
178 /* This is a complete rewrite of afs_readdir, since we can make use of
179 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
180 * handling and use of bulkstats will need to be reflected here as well.
183 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
185 extern struct DirEntry *afs_dir_GetBlob();
186 struct vcache *avc = VTOAFS(FILE_INODE(fp));
187 struct vrequest treq;
188 register struct dcache *tdc;
195 afs_size_t origOffset, tlen;
196 cred_t *credp = crref();
197 struct afs_fakestat_state fakestat;
199 #if defined(AFS_LINUX26_ENV)
203 AFS_STATCNT(afs_readdir);
205 code = afs_InitReq(&treq, credp);
210 afs_InitFakeStat(&fakestat);
211 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
215 /* update the cache entry */
217 code = afs_VerifyVCache(avc, &treq);
221 /* get a reference to the entire directory */
222 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
228 ObtainSharedLock(&avc->lock, 810);
229 UpgradeSToWLock(&avc->lock, 811);
230 ObtainReadLock(&tdc->lock);
232 * Make sure that the data in the cache is current. There are two
233 * cases we need to worry about:
234 * 1. The cache data is being fetched by another process.
235 * 2. The cache data is no longer valid
237 while ((avc->states & CStatd)
238 && (tdc->dflags & DFFetching)
239 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
240 ReleaseReadLock(&tdc->lock);
241 ReleaseSharedLock(&avc->lock);
242 afs_osi_Sleep(&tdc->validPos);
243 ObtainSharedLock(&avc->lock, 812);
244 ObtainReadLock(&tdc->lock);
246 if (!(avc->states & CStatd)
247 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
248 ReleaseReadLock(&tdc->lock);
249 ReleaseSharedLock(&avc->lock);
254 /* Set the readdir-in-progress flag, and downgrade the lock
255 * to shared so others will be able to acquire a read lock.
257 avc->states |= CReadDir;
258 avc->dcreaddir = tdc;
259 avc->readdir_pid = MyPidxx;
260 ConvertWToSLock(&avc->lock);
262 /* Fill in until we get an error or we're done. This implementation
263 * takes an offset in units of blobs, rather than bytes.
266 offset = (int) fp->f_pos;
268 dirpos = BlobScan(tdc, offset);
272 de = afs_dir_GetBlob(tdc, dirpos);
276 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
279 len = strlen(de->name);
281 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
282 (unsigned long)&tdc->f.inode, dirpos);
283 DRelease((struct buffer *) de, 0);
284 ReleaseSharedLock(&avc->lock);
290 /* filldir returns -EINVAL when the buffer is full. */
291 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
293 unsigned int type = DT_UNKNOWN;
294 struct VenusFid afid;
297 afid.Cell = avc->fid.Cell;
298 afid.Fid.Volume = avc->fid.Fid.Volume;
299 afid.Fid.Vnode = ntohl(de->fid.vnode);
300 afid.Fid.Unique = ntohl(de->fid.vunique);
301 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
303 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
306 } else if (((tvc->states) & (CStatd | CTruth))) {
307 /* CTruth will be set if the object has
312 else if (vtype == VREG)
314 /* Don't do this until we're sure it can't be a mtpt */
315 /* else if (vtype == VLNK)
317 /* what other types does AFS support? */
319 /* clean up from afs_FindVCache */
323 * If this is NFS readdirplus, then the filler is going to
324 * call getattr on this inode, which will deadlock if we're
328 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
332 code = (*filldir) (dirbuf, de->name, len, offset, ino);
334 DRelease((struct buffer *)de, 0);
337 offset = dirpos + 1 + ((len + 16) >> 5);
339 /* If filldir didn't fill in the last one this is still pointing to that
342 fp->f_pos = (loff_t) offset;
344 ReleaseReadLock(&tdc->lock);
346 UpgradeSToWLock(&avc->lock, 813);
347 avc->states &= ~CReadDir;
349 avc->readdir_pid = 0;
350 ReleaseSharedLock(&avc->lock);
354 afs_PutFakeStat(&fakestat);
357 #if defined(AFS_LINUX26_ENV)
358 maybe_unlock_kernel();
364 /* in afs_pioctl.c */
365 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
368 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
369 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
371 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
378 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
380 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
381 cred_t *credp = crref();
382 struct vrequest treq;
386 #if defined(AFS_LINUX24_ENV)
387 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
388 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
389 vmap->vm_end - vmap->vm_start);
391 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
392 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
393 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
397 /* get a validated vcache entry */
398 code = afs_InitReq(&treq, credp);
402 code = afs_VerifyVCache(vcp, &treq);
406 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
409 code = generic_file_mmap(fp, vmap);
412 vcp->states |= CMAPPED;
425 afs_linux_open(struct inode *ip, struct file *fp)
427 struct vcache *vcp = VTOAFS(ip);
428 cred_t *credp = crref();
431 #ifdef AFS_LINUX24_ENV
435 code = afs_open(&vcp, fp->f_flags, credp);
437 #ifdef AFS_LINUX24_ENV
438 maybe_unlock_kernel();
446 afs_linux_release(struct inode *ip, struct file *fp)
448 struct vcache *vcp = VTOAFS(ip);
449 cred_t *credp = crref();
452 #ifdef AFS_LINUX24_ENV
456 code = afs_close(vcp, fp->f_flags, credp);
458 #ifdef AFS_LINUX24_ENV
459 maybe_unlock_kernel();
467 #if defined(AFS_LINUX24_ENV)
468 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
470 afs_linux_fsync(struct file *fp, struct dentry *dp)
474 struct inode *ip = FILE_INODE(fp);
475 cred_t *credp = crref();
477 #ifdef AFS_LINUX24_ENV
481 code = afs_fsync(VTOAFS(ip), credp);
483 #ifdef AFS_LINUX24_ENV
484 maybe_unlock_kernel();
493 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
496 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
497 cred_t *credp = crref();
498 struct AFS_FLOCK flock;
499 /* Convert to a lock format afs_lockctl understands. */
500 memset((char *)&flock, 0, sizeof(flock));
501 flock.l_type = flp->fl_type;
502 flock.l_pid = flp->fl_pid;
504 flock.l_start = flp->fl_start;
505 flock.l_len = flp->fl_end - flp->fl_start;
507 /* Safe because there are no large files, yet */
508 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
509 if (cmd == F_GETLK64)
511 else if (cmd == F_SETLK64)
513 else if (cmd == F_SETLKW64)
515 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
518 code = afs_lockctl(vcp, &flock, cmd, credp);
521 #ifdef AFS_LINUX24_ENV
522 if ((code == 0 || flp->fl_type == F_UNLCK) &&
523 (cmd == F_SETLK || cmd == F_SETLKW)) {
524 #ifdef POSIX_LOCK_FILE_WAIT_ARG
525 code = posix_lock_file(fp, flp, 0);
527 flp->fl_flags &=~ FL_SLEEP;
528 code = posix_lock_file(fp, flp);
530 if (code && flp->fl_type != F_UNLCK) {
531 struct AFS_FLOCK flock2;
533 flock2.l_type = F_UNLCK;
535 afs_lockctl(vcp, &flock2, F_SETLK, credp);
540 /* Convert flock back to Linux's file_lock */
541 flp->fl_type = flock.l_type;
542 flp->fl_pid = flock.l_pid;
543 flp->fl_start = flock.l_start;
544 flp->fl_end = flock.l_start + flock.l_len;
551 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
553 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
555 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
556 cred_t *credp = crref();
557 struct AFS_FLOCK flock;
558 /* Convert to a lock format afs_lockctl understands. */
559 memset((char *)&flock, 0, sizeof(flock));
560 flock.l_type = flp->fl_type;
561 flock.l_pid = flp->fl_pid;
564 flock.l_len = OFFSET_MAX;
566 /* Safe because there are no large files, yet */
567 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
568 if (cmd == F_GETLK64)
570 else if (cmd == F_SETLK64)
572 else if (cmd == F_SETLKW64)
574 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
577 code = afs_lockctl(vcp, &flock, cmd, credp);
580 if ((code == 0 || flp->fl_type == F_UNLCK) &&
581 (cmd == F_SETLK || cmd == F_SETLKW)) {
582 flp->fl_flags &=~ FL_SLEEP;
583 code = flock_lock_file_wait(fp, flp);
584 if (code && flp->fl_type != F_UNLCK) {
585 struct AFS_FLOCK flock2;
587 flock2.l_type = F_UNLCK;
589 afs_lockctl(vcp, &flock2, F_SETLK, credp);
593 /* Convert flock back to Linux's file_lock */
594 flp->fl_type = flock.l_type;
595 flp->fl_pid = flock.l_pid;
603 * essentially the same as afs_fsync() but we need to get the return
604 * code for the sys_close() here, not afs_linux_release(), so call
605 * afs_StoreAllSegments() with AFS_LASTSTORE
608 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
609 afs_linux_flush(struct file *fp, fl_owner_t id)
611 afs_linux_flush(struct file *fp)
614 struct vrequest treq;
618 #if defined(AFS_CACHE_BYPASS)
624 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
632 vcp = VTOAFS(FILE_INODE(fp));
634 code = afs_InitReq(&treq, credp);
637 #if defined(AFS_CACHE_BYPASS)
638 /* If caching is bypassed for this file, or globally, just return 0 */
639 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
642 ObtainReadLock(&vcp->lock);
643 if(vcp->cachingStates & FCSBypass)
645 ReleaseReadLock(&vcp->lock);
648 /* future proof: don't rely on 0 return from afs_InitReq */
653 ObtainSharedLock(&vcp->lock, 535);
654 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
655 UpgradeSToWLock(&vcp->lock, 536);
656 if (!AFS_IS_DISCONNECTED) {
657 code = afs_StoreAllSegments(vcp,
659 AFS_SYNC | AFS_LASTSTORE);
661 #if defined(AFS_DISCON_ENV)
662 if (!vcp->ddirty_flags ||
663 (vcp->ddirty_flags == VDisconShadowed)) {
665 ObtainWriteLock(&afs_DDirtyVCListLock, 710);
666 AFS_DISCON_ADD_DIRTY(vcp);
667 ReleaseWriteLock(&afs_DDirtyVCListLock);
670 /* Set disconnected write flag. */
671 vcp->ddirty_flags |= VDisconWriteOsiFlush;
675 ConvertWToSLock(&vcp->lock);
677 code = afs_CheckCode(code, &treq, 54);
678 ReleaseSharedLock(&vcp->lock);
688 #if !defined(AFS_LINUX24_ENV)
689 /* Not allowed to directly read a directory. */
691 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
699 struct file_operations afs_dir_fops = {
700 #if !defined(AFS_LINUX24_ENV)
701 .read = afs_linux_dir_read,
702 .lock = afs_linux_lock,
703 .fsync = afs_linux_fsync,
705 .read = generic_read_dir,
707 .readdir = afs_linux_readdir,
708 #ifdef HAVE_UNLOCKED_IOCTL
709 .unlocked_ioctl = afs_unlocked_xioctl,
713 #ifdef HAVE_COMPAT_IOCTL
714 .compat_ioctl = afs_unlocked_xioctl,
716 .open = afs_linux_open,
717 .release = afs_linux_release,
720 struct file_operations afs_file_fops = {
721 .read = afs_linux_read,
722 .write = afs_linux_write,
723 #ifdef GENERIC_FILE_AIO_READ
724 .aio_read = generic_file_aio_read,
725 .aio_write = generic_file_aio_write,
727 #ifdef HAVE_UNLOCKED_IOCTL
728 .unlocked_ioctl = afs_unlocked_xioctl,
732 #ifdef HAVE_COMPAT_IOCTL
733 .compat_ioctl = afs_unlocked_xioctl,
735 .mmap = afs_linux_mmap,
736 .open = afs_linux_open,
737 .flush = afs_linux_flush,
738 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
739 .sendfile = generic_file_sendfile,
741 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
742 .splice_write = generic_file_splice_write,
743 .splice_read = generic_file_splice_read,
745 .release = afs_linux_release,
746 .fsync = afs_linux_fsync,
747 .lock = afs_linux_lock,
748 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
749 .flock = afs_linux_flock,
754 /**********************************************************************
755 * AFS Linux dentry operations
756 **********************************************************************/
758 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
759 * that has its mvid (parent dir's fid) pointer set to the wrong directory
760 * due to being mounted in multiple points at once. If so, check_bad_parent()
761 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
762 * dotdotfid and mtpoint fid members.
764 * dp - dentry to be checked.
768 * This dentry's vcache's mvid will be set to the correct parent directory's
770 * This root vnode's volume will have its dotdotfid and mtpoint fids set
771 * to the correct parent and mountpoint fids.
775 check_bad_parent(struct dentry *dp)
778 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
779 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
781 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
784 /* force a lookup, so vcp->mvid is fixed up */
785 afs_lookup(pvc, dp->d_name.name, &avc, credp);
786 if (!avc || vcp != avc) { /* bad, very bad.. */
787 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
788 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
789 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
790 ICL_TYPE_POINTER, dp);
793 AFS_RELE(AFSTOV(avc));
800 /* afs_linux_revalidate
801 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
804 afs_linux_revalidate(struct dentry *dp)
807 struct vcache *vcp = VTOAFS(dp->d_inode);
811 #ifdef AFS_LINUX24_ENV
817 /* Make this a fast path (no crref), since it's called so often. */
818 if (vcp->states & CStatd) {
820 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
821 check_bad_parent(dp); /* check and correct mvid */
824 #ifdef AFS_LINUX24_ENV
832 code = afs_getattr(vcp, &vattr, credp);
834 afs_fill_inode(AFSTOV(vcp), &vattr);
837 #ifdef AFS_LINUX24_ENV
838 maybe_unlock_kernel();
845 #if defined(AFS_LINUX26_ENV)
847 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
849 int err = afs_linux_revalidate(dentry);
851 generic_fillattr(dentry->d_inode, stat);
857 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
858 * In kernels 2.2.10 and above, we are passed an additional flags var which
859 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
860 * we are advised to follow the entry if it is a link or to make sure that
861 * it is a directory. But since the kernel itself checks these possibilities
862 * later on, we shouldn't have to do it until later. Perhaps in the future..
865 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
866 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
867 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
869 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
872 afs_linux_dentry_revalidate(struct dentry *dp)
876 cred_t *credp = NULL;
877 struct vcache *vcp, *pvcp, *tvc = NULL;
880 #ifdef AFS_LINUX24_ENV
887 vcp = VTOAFS(dp->d_inode);
888 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
890 if (vcp == afs_globalVp)
893 if (vcp->mvstat == 1) { /* mount point */
894 if (vcp->mvid && (vcp->states & CMValid)) {
895 /* a mount point, not yet replaced by its directory */
899 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
900 check_bad_parent(dp); /* check and correct mvid */
903 /* If the last looker changes, we should make sure the current
904 * looker still has permission to examine this file. This would
905 * always require a crref() which would be "slow".
907 if (vcp->last_looker != treq.uid) {
908 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
911 vcp->last_looker = treq.uid;
915 /* If the parent's DataVersion has changed or the vnode
916 * is longer valid, we need to do a full lookup. VerifyVCache
917 * isn't enough since the vnode may have been renamed.
920 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
923 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
924 if (!tvc || tvc != vcp)
927 if (afs_getattr(vcp, &vattr, credp))
930 vattr2inode(AFSTOV(vcp), &vattr);
931 dp->d_time = hgetlo(pvcp->m.DataVersion);
934 /* should we always update the attributes at this point? */
935 /* unlikely--the vcache entry hasn't changed */
939 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
940 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
944 /* No change in parent's DataVersion so this negative
945 * lookup is still valid. BUT, if a server is down a
946 * negative lookup can result so there should be a
947 * liftime as well. For now, always expire.
965 shrink_dcache_parent(dp);
968 #ifdef AFS_LINUX24_ENV
969 maybe_unlock_kernel();
979 afs_dentry_iput(struct dentry *dp, struct inode *ip)
981 struct vcache *vcp = VTOAFS(ip);
984 (void) afs_InactiveVCache(vcp, NULL);
986 #ifdef DCACHE_NFSFS_RENAMED
987 #ifdef AFS_LINUX26_ENV
988 spin_lock(&dp->d_lock);
990 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
991 #ifdef AFS_LINUX26_ENV
992 spin_unlock(&dp->d_lock);
1000 afs_dentry_delete(struct dentry *dp)
1002 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
1003 return 1; /* bad inode? */
1008 struct dentry_operations afs_dentry_operations = {
1009 .d_revalidate = afs_linux_dentry_revalidate,
1010 .d_delete = afs_dentry_delete,
1011 .d_iput = afs_dentry_iput,
1014 /**********************************************************************
1015 * AFS Linux inode operations
1016 **********************************************************************/
1020 * Merely need to set enough of vattr to get us through the create. Note
1021 * that the higher level code (open_namei) will take care of any tuncation
1022 * explicitly. Exclusive open is also taken care of in open_namei.
1024 * name is in kernel space at this point.
1027 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1028 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1029 struct nameidata *nd)
1031 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1035 cred_t *credp = crref();
1036 const char *name = dp->d_name.name;
1041 vattr.va_mode = mode;
1042 vattr.va_type = mode & S_IFMT;
1044 #if defined(AFS_LINUX26_ENV)
1045 maybe_lock_kernel();
1048 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1052 struct inode *ip = AFSTOV(vcp);
1054 afs_getattr(vcp, &vattr, credp);
1055 afs_fill_inode(ip, &vattr);
1056 insert_inode_hash(ip);
1057 dp->d_op = &afs_dentry_operations;
1058 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1059 d_instantiate(dp, ip);
1063 #if defined(AFS_LINUX26_ENV)
1064 maybe_unlock_kernel();
1070 /* afs_linux_lookup */
1071 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1072 static struct dentry *
1073 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1074 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1075 struct nameidata *nd)
1077 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1081 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1084 cred_t *credp = crref();
1085 struct vcache *vcp = NULL;
1086 const char *comp = dp->d_name.name;
1087 struct inode *ip = NULL;
1088 #if defined(AFS_LINUX26_ENV)
1089 struct dentry *newdp = NULL;
1093 #if defined(AFS_LINUX26_ENV)
1094 maybe_lock_kernel();
1097 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1103 afs_getattr(vcp, &vattr, credp);
1104 afs_fill_inode(ip, &vattr);
1106 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1107 hlist_unhashed(&ip->i_hash)
1108 #elif defined(AFS_LINUX26_ENV)
1109 ip->i_hash.pprev == NULL
1111 ip->i_hash.prev == NULL
1114 insert_inode_hash(ip);
1116 dp->d_op = &afs_dentry_operations;
1117 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1120 #if defined(AFS_LINUX24_ENV)
1121 if (ip && S_ISDIR(ip->i_mode)) {
1122 struct dentry *alias;
1124 /* Try to invalidate an existing alias in favor of our new one */
1125 alias = d_find_alias(ip);
1126 #if defined(AFS_LINUX26_ENV)
1127 /* But not if it's disconnected; then we want d_splice_alias below */
1128 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1132 if (d_invalidate(alias) == 0) {
1136 #if defined(AFS_LINUX26_ENV)
1145 #if defined(AFS_LINUX26_ENV)
1146 newdp = d_splice_alias(ip, dp);
1151 #if defined(AFS_LINUX26_ENV)
1152 maybe_unlock_kernel();
1156 /* It's ok for the file to not be found. That's noted by the caller by
1157 * seeing that the dp->d_inode field is NULL.
1159 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1160 #if defined(AFS_LINUX26_ENV)
1161 if (!code || code == ENOENT)
1168 return ERR_PTR(-code);
1177 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1180 cred_t *credp = crref();
1181 const char *name = newdp->d_name.name;
1182 struct inode *oldip = olddp->d_inode;
1184 /* If afs_link returned the vnode, we could instantiate the
1185 * dentry. Since it's not, we drop this one and do a new lookup.
1190 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1198 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1201 cred_t *credp = crref();
1202 const char *name = dp->d_name.name;
1203 struct vcache *tvc = VTOAFS(dp->d_inode);
1205 #if defined(AFS_LINUX26_ENV)
1206 maybe_lock_kernel();
1208 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1209 && !(tvc->states & CUnlinked)) {
1210 struct dentry *__dp;
1212 extern char *afs_newname();
1221 osi_FreeSmallSpace(__name);
1222 __name = afs_newname();
1225 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1229 } while (__dp->d_inode != NULL);
1232 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1234 tvc->mvid = (void *) __name;
1237 crfree(tvc->uncred);
1239 tvc->uncred = credp;
1240 tvc->states |= CUnlinked;
1241 #ifdef DCACHE_NFSFS_RENAMED
1242 #ifdef AFS_LINUX26_ENV
1243 spin_lock(&dp->d_lock);
1245 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1246 #ifdef AFS_LINUX26_ENV
1247 spin_unlock(&dp->d_lock);
1251 osi_FreeSmallSpace(__name);
1256 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1265 code = afs_remove(VTOAFS(dip), name, credp);
1270 #if defined(AFS_LINUX26_ENV)
1271 maybe_unlock_kernel();
1279 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1282 cred_t *credp = crref();
1284 const char *name = dp->d_name.name;
1286 /* If afs_symlink returned the vnode, we could instantiate the
1287 * dentry. Since it's not, we drop this one and do a new lookup.
1293 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1300 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1303 cred_t *credp = crref();
1304 struct vcache *tvcp = NULL;
1306 const char *name = dp->d_name.name;
1308 #if defined(AFS_LINUX26_ENV)
1309 maybe_lock_kernel();
1312 vattr.va_mask = ATTR_MODE;
1313 vattr.va_mode = mode;
1315 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1318 struct inode *ip = AFSTOV(tvcp);
1320 afs_getattr(tvcp, &vattr, credp);
1321 afs_fill_inode(ip, &vattr);
1323 dp->d_op = &afs_dentry_operations;
1324 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1325 d_instantiate(dp, ip);
1329 #if defined(AFS_LINUX26_ENV)
1330 maybe_unlock_kernel();
1337 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1340 cred_t *credp = crref();
1341 const char *name = dp->d_name.name;
1343 /* locking kernel conflicts with glock? */
1346 code = afs_rmdir(VTOAFS(dip), name, credp);
1349 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1350 * that failed because a directory is not empty. So, we map
1351 * EEXIST to ENOTEMPTY on linux.
1353 if (code == EEXIST) {
1367 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1368 struct inode *newip, struct dentry *newdp)
1371 cred_t *credp = crref();
1372 const char *oldname = olddp->d_name.name;
1373 const char *newname = newdp->d_name.name;
1374 struct dentry *rehash = NULL;
1376 #if defined(AFS_LINUX26_ENV)
1377 /* Prevent any new references during rename operation. */
1378 maybe_lock_kernel();
1380 if (!d_unhashed(newdp)) {
1385 if (!list_empty(&newdp->d_hash)) {
1391 #if defined(AFS_LINUX24_ENV)
1392 if (atomic_read(&olddp->d_count) > 1)
1393 shrink_dcache_parent(olddp);
1397 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1401 olddp->d_time = 0; /* force to revalidate */
1406 #if defined(AFS_LINUX26_ENV)
1407 maybe_unlock_kernel();
1415 /* afs_linux_ireadlink
1416 * Internal readlink which can return link contents to user or kernel space.
1417 * Note that the buffer is NOT supposed to be null-terminated.
1420 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1423 cred_t *credp = crref();
1427 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1428 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1432 return maxlen - tuio.uio_resid;
1437 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1438 /* afs_linux_readlink
1439 * Fill target (which is in user space) with contents of symlink.
1442 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1445 struct inode *ip = dp->d_inode;
1448 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1454 /* afs_linux_follow_link
1455 * a file system dependent link following routine.
1457 #if defined(AFS_LINUX24_ENV)
1458 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1463 name = osi_Alloc(PATH_MAX);
1469 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1477 code = vfs_follow_link(nd, name);
1480 osi_Free(name, PATH_MAX);
1485 #else /* !defined(AFS_LINUX24_ENV) */
1487 static struct dentry *
1488 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1489 unsigned int follow)
1497 name = osi_Alloc(PATH_MAX + 1);
1501 return ERR_PTR(-EIO);
1504 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1509 res = ERR_PTR(code);
1512 res = lookup_dentry(name, basep, follow);
1516 osi_Free(name, PATH_MAX + 1);
1520 #endif /* AFS_LINUX24_ENV */
1521 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1523 #if defined(AFS_CACHE_BYPASS)
1525 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1527 /* The kernel calls readpages before trying readpage, with a list of
1528 * pages. The readahead algorithm expands num_pages when it thinks
1529 * the application will benefit. Unlike readpage, the pages are not
1530 * necessarily allocated. If we do not a) allocate required pages and
1531 * b) remove them from page_list, linux will re-enter at afs_linux_readpage
1532 * for each required page (and the page will be pre-allocated) */
1535 afs_linux_readpages(struct file *fp, struct address_space *mapping,
1536 struct list_head *page_list, unsigned num_pages)
1541 struct iovec* iovecp;
1542 struct nocache_read_request *ancr;
1543 struct page *pp, *ppt;
1544 struct pagevec lrupv;
1548 struct inode *ip = FILE_INODE(fp);
1549 struct vcache *avc = VTOAFS(ip);
1550 afs_int32 bypasscache = 0; /* bypass for this read */
1551 afs_int32 base_index = 0;
1552 afs_int32 page_count = 0;
1557 switch(cache_bypass_strategy) {
1558 case NEVER_BYPASS_CACHE:
1560 case ALWAYS_BYPASS_CACHE:
1563 case LARGE_FILES_BYPASS_CACHE:
1564 if(i_size_read(ip) > cache_bypass_threshold) {
1572 /* In the new incarnation of selective caching, a file's caching policy
1573 * can change, eg because file size exceeds threshold, etc. */
1574 trydo_cache_transition(avc, credp, bypasscache);
1577 while(!list_empty(page_list)) {
1578 pp = list_entry(page_list->prev, struct page, lru);
1583 /* background thread must free: iovecp, auio, ancr */
1584 iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1586 auio = osi_Alloc(sizeof(uio_t));
1587 auio->uio_iov = iovecp;
1588 auio->uio_iovcnt = num_pages;
1589 auio->uio_flag = UIO_READ;
1590 auio->uio_seg = AFS_UIOSYS;
1591 auio->uio_resid = num_pages * PAGE_SIZE;
1593 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1595 ancr->offset = auio->uio_offset;
1596 ancr->length = auio->uio_resid;
1598 pagevec_init(&lrupv, 0);
1600 for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1602 if(list_empty(page_list))
1605 pp = list_entry(page_list->prev, struct page, lru);
1606 /* If we allocate a page and don't remove it from page_list,
1607 * the page cache gets upset. */
1609 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1610 if(pp->index > isize) {
1617 offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1618 auio->uio_offset = offset;
1619 base_index = pp->index;
1621 iovecp[page_ix].iov_len = PAGE_SIZE;
1622 code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1623 if(base_index != pp->index) {
1626 page_cache_release(pp);
1627 iovecp[page_ix].iov_base = (void *) 0;
1635 page_cache_release(pp);
1636 iovecp[page_ix].iov_base = (void *) 0;
1639 if(!PageLocked(pp)) {
1643 /* save the page for background map */
1644 iovecp[page_ix].iov_base = (void*) pp;
1646 /* and put it on the LRU cache */
1647 if (!pagevec_add(&lrupv, pp))
1648 __pagevec_lru_add(&lrupv);
1652 /* If there were useful pages in the page list, make sure all pages
1653 * are in the LRU cache, then schedule the read */
1655 pagevec_lru_add(&lrupv);
1656 code = afs_ReadNoCache(avc, ancr, credp);
1658 /* If there is nothing for the background thread to handle,
1659 * it won't be freeing the things that we never gave it */
1660 osi_Free(iovecp, num_pages * sizeof(struct iovec));
1661 osi_Free(auio, sizeof(uio_t));
1662 osi_Free(ancr, sizeof(struct nocache_read_request));
1664 /* we do not flush, release, or unmap pages--that will be
1665 * done for us by the background thread as each page comes in
1666 * from the fileserver */
1673 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1674 #endif /* defined(AFS_CACHE_BYPASS */
1677 /* afs_linux_readpage
1678 * all reads come through here. A strategy-like read call.
1681 afs_linux_readpage(struct file *fp, struct page *pp)
1684 cred_t *credp = crref();
1685 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1687 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1689 ulong address = afs_linux_page_address(pp);
1690 afs_offs_t offset = pageoff(pp);
1692 #if defined(AFS_CACHE_BYPASS)
1693 afs_int32 bypasscache = 0; /* bypass for this read */
1694 struct nocache_read_request *ancr;
1698 struct iovec *iovecp;
1699 struct inode *ip = FILE_INODE(fp);
1700 afs_int32 cnt = page_count(pp);
1701 struct vcache *avc = VTOAFS(ip);
1703 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1707 atomic_add(1, &pp->count);
1708 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1709 clear_bit(PG_error, &pp->flags);
1711 /* If the page is past the end of the file, skip it */
1712 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1713 if(pp->index > isize) {
1718 /* if bypasscache, receiver frees, else we do */
1719 auio = osi_Alloc(sizeof(uio_t));
1720 iovecp = osi_Alloc(sizeof(struct iovec));
1722 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1725 #if defined(AFS_CACHE_BYPASS)
1727 switch(cache_bypass_strategy) {
1728 case NEVER_BYPASS_CACHE:
1730 case ALWAYS_BYPASS_CACHE:
1733 case LARGE_FILES_BYPASS_CACHE:
1734 if(i_size_read(ip) > cache_bypass_threshold) {
1742 /* In the new incarnation of selective caching, a file's caching policy
1743 * can change, eg because file size exceeds threshold, etc. */
1744 trydo_cache_transition(avc, credp, bypasscache);
1749 /* save the page for background map */
1750 auio->uio_iov->iov_base = (void*) pp;
1751 /* the background thread will free this */
1752 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1754 ancr->offset = offset;
1755 ancr->length = PAGE_SIZE;
1757 maybe_lock_kernel();
1758 code = afs_ReadNoCache(avc, ancr, credp);
1759 maybe_unlock_kernel();
1761 goto done; /* skips release page, doing it in bg thread */
1765 #ifdef AFS_LINUX24_ENV
1766 maybe_lock_kernel();
1769 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip, ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32, 99999); /* not a possible code value */
1771 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1773 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1774 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1777 #ifdef AFS_LINUX24_ENV
1778 maybe_unlock_kernel();
1781 /* XXX valid for no-cache also? Check last bits of files... :)
1782 * Cognate code goes in afs_NoCacheFetchProc. */
1783 if (auio->uio_resid) /* zero remainder of page */
1784 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1787 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1788 flush_dcache_page(pp);
1789 SetPageUptodate(pp);
1791 set_bit(PG_uptodate, &pp->flags);
1795 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1799 clear_bit(PG_locked, &pp->flags);
1804 #if defined(AFS_CACHE_BYPASS)
1806 /* do not call afs_GetDCache if cache is bypassed */
1812 /* free if not bypassing cache */
1813 osi_Free(auio, sizeof(uio_t));
1814 osi_Free(iovecp, sizeof(struct iovec));
1816 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1818 struct vrequest treq;
1821 code = afs_InitReq(&treq, credp);
1822 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1823 tdc = afs_FindDCache(avc, offset);
1825 if (!(tdc->mflags & DFNextStarted))
1826 afs_PrefetchChunk(avc, tdc, credp, &treq);
1829 ReleaseWriteLock(&avc->lock);
1840 #if defined(AFS_LINUX24_ENV)
1842 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1843 unsigned long offset, unsigned int count)
1845 struct vcache *vcp = VTOAFS(ip);
1854 buffer = kmap(pp) + offset;
1855 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1858 maybe_lock_kernel();
1860 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1861 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1862 ICL_TYPE_INT32, 99999);
1864 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1866 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1868 ip->i_size = vcp->m.Length;
1869 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1872 struct vrequest treq;
1874 ObtainWriteLock(&vcp->lock, 533);
1875 if (!afs_InitReq(&treq, credp))
1876 code = afs_DoPartialWrite(vcp, &treq);
1877 ReleaseWriteLock(&vcp->lock);
1879 code = code ? -code : count - tuio.uio_resid;
1881 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1882 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1883 ICL_TYPE_INT32, code);
1886 maybe_unlock_kernel();
1895 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1896 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1898 afs_linux_writepage(struct page *pp)
1901 struct address_space *mapping = pp->mapping;
1902 struct inode *inode;
1903 unsigned long end_index;
1904 unsigned offset = PAGE_CACHE_SIZE;
1907 #if defined(AFS_LINUX26_ENV)
1908 if (PageReclaim(pp)) {
1909 # if defined(WRITEPAGE_ACTIVATE)
1910 return WRITEPAGE_ACTIVATE;
1912 return AOP_WRITEPAGE_ACTIVATE;
1916 if (PageLaunder(pp)) {
1917 return(fail_writepage(pp));
1921 inode = (struct inode *)mapping->host;
1922 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1925 if (pp->index < end_index)
1927 /* things got complicated... */
1928 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1929 /* OK, are we completely out? */
1930 if (pp->index >= end_index + 1 || !offset)
1933 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1934 SetPageUptodate(pp);
1936 if (status == offset)
1942 /* afs_linux_updatepage
1943 * What one would have thought was writepage - write dirty page to file.
1944 * Called from generic_file_write. buffer is still in user space. pagep
1945 * has been filled in with old data if we're updating less than a page.
1948 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1949 unsigned int count, int sync)
1951 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1952 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1958 set_bit(PG_locked, &pp->flags);
1962 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1963 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1964 ICL_TYPE_INT32, 99999);
1965 setup_uio(&tuio, &iovec, page_addr + offset,
1966 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1969 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1971 ip->i_size = vcp->m.Length;
1972 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1975 struct vrequest treq;
1977 ObtainWriteLock(&vcp->lock, 533);
1978 vcp->m.Date = osi_Time(); /* set modification time */
1979 if (!afs_InitReq(&treq, credp))
1980 code = afs_DoPartialWrite(vcp, &treq);
1981 ReleaseWriteLock(&vcp->lock);
1984 code = code ? -code : count - tuio.uio_resid;
1985 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1986 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1987 ICL_TYPE_INT32, code);
1992 clear_bit(PG_locked, &pp->flags);
1997 /* afs_linux_permission
1998 * Check access rights - returns error if can't check or permission denied.
2001 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2002 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2004 afs_linux_permission(struct inode *ip, int mode)
2008 cred_t *credp = crref();
2012 if (mode & MAY_EXEC)
2014 if (mode & MAY_READ)
2016 if (mode & MAY_WRITE)
2018 code = afs_access(VTOAFS(ip), tmp, credp);
2025 #if defined(AFS_LINUX24_ENV)
2027 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2032 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2033 offset, to - offset);
2034 #if !defined(AFS_LINUX26_ENV)
2042 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2045 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2046 call kmap directly instead of relying on us to do it */
2047 #if !defined(AFS_LINUX26_ENV)
2053 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
2056 static struct inode_operations afs_file_iops = {
2057 #if defined(AFS_LINUX26_ENV)
2058 .permission = afs_linux_permission,
2059 .getattr = afs_linux_getattr,
2060 .setattr = afs_notify_change,
2061 #elif defined(AFS_LINUX24_ENV)
2062 .permission = afs_linux_permission,
2063 .revalidate = afs_linux_revalidate,
2064 .setattr = afs_notify_change,
2066 .default_file_ops = &afs_file_fops,
2067 .readpage = afs_linux_readpage,
2068 .revalidate = afs_linux_revalidate,
2069 .updatepage = afs_linux_updatepage,
2073 #if defined(AFS_LINUX24_ENV)
2074 static struct address_space_operations afs_file_aops = {
2075 .readpage = afs_linux_readpage,
2076 #if defined(AFS_CACHE_BYPASS) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2077 .readpages = afs_linux_readpages,
2079 .writepage = afs_linux_writepage,
2080 .commit_write = afs_linux_commit_write,
2081 .prepare_write = afs_linux_prepare_write,
2086 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2087 * by what sort of operation is allowed.....
2090 static struct inode_operations afs_dir_iops = {
2091 #if !defined(AFS_LINUX24_ENV)
2092 .default_file_ops = &afs_dir_fops,
2094 .setattr = afs_notify_change,
2096 .create = afs_linux_create,
2097 .lookup = afs_linux_lookup,
2098 .link = afs_linux_link,
2099 .unlink = afs_linux_unlink,
2100 .symlink = afs_linux_symlink,
2101 .mkdir = afs_linux_mkdir,
2102 .rmdir = afs_linux_rmdir,
2103 .rename = afs_linux_rename,
2104 #if defined(AFS_LINUX26_ENV)
2105 .getattr = afs_linux_getattr,
2107 .revalidate = afs_linux_revalidate,
2109 .permission = afs_linux_permission,
2112 /* We really need a separate symlink set of ops, since do_follow_link()
2113 * determines if it _is_ a link by checking if the follow_link op is set.
2115 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2117 afs_symlink_filler(struct file *file, struct page *page)
2119 struct inode *ip = (struct inode *)page->mapping->host;
2120 char *p = (char *)kmap(page);
2123 maybe_lock_kernel();
2125 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2130 p[code] = '\0'; /* null terminate? */
2131 maybe_unlock_kernel();
2133 SetPageUptodate(page);
2139 maybe_unlock_kernel();
2147 static struct address_space_operations afs_symlink_aops = {
2148 .readpage = afs_symlink_filler
2150 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2152 static struct inode_operations afs_symlink_iops = {
2153 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2154 .readlink = page_readlink,
2155 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2156 .follow_link = page_follow_link,
2158 .follow_link = page_follow_link_light,
2159 .put_link = page_put_link,
2161 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2162 .readlink = afs_linux_readlink,
2163 .follow_link = afs_linux_follow_link,
2164 #if !defined(AFS_LINUX24_ENV)
2165 .permission = afs_linux_permission,
2166 .revalidate = afs_linux_revalidate,
2168 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2169 #if defined(AFS_LINUX24_ENV)
2170 .setattr = afs_notify_change,
2175 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2179 vattr2inode(ip, vattr);
2181 /* Reset ops if symlink or directory. */
2182 if (S_ISREG(ip->i_mode)) {
2183 ip->i_op = &afs_file_iops;
2184 #if defined(AFS_LINUX24_ENV)
2185 ip->i_fop = &afs_file_fops;
2186 ip->i_data.a_ops = &afs_file_aops;
2189 } else if (S_ISDIR(ip->i_mode)) {
2190 ip->i_op = &afs_dir_iops;
2191 #if defined(AFS_LINUX24_ENV)
2192 ip->i_fop = &afs_dir_fops;
2195 } else if (S_ISLNK(ip->i_mode)) {
2196 ip->i_op = &afs_symlink_iops;
2197 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2198 ip->i_data.a_ops = &afs_symlink_aops;
2199 ip->i_mapping = &ip->i_data;