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 "h/mm_inline.h"
32 #include "h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "h/smp_lock.h"
37 #include "afs/afs_bypasscache.h"
40 #define pageoff(pp) pgoff2loff((pp)->index)
42 #define pageoff(pp) pp->offset
46 #define MAX_ERRNO 1000L
49 extern struct vcache *afs_globalVp;
50 #if defined(AFS_LINUX24_ENV)
51 /* Some uses of BKL are perhaps not needed for bypass or memcache--
52 * why don't we try it out? */
53 extern struct afs_cacheOps afs_UfsCacheOps;
54 #define maybe_lock_kernel() \
56 if(afs_cacheType == &afs_UfsCacheOps) \
61 #define maybe_unlock_kernel() \
63 if(afs_cacheType == &afs_UfsCacheOps) \
66 #endif /* AFS_LINUX24_ENV */
69 /* This function converts a positive error code from AFS into a negative
70 * code suitable for passing into the Linux VFS layer. It checks that the
71 * error code is within the permissable bounds for the ERR_PTR mechanism.
73 * _All_ error codes which come from the AFS layer should be passed through
74 * this function before being returned to the kernel.
77 static inline int afs_convert_code(int code) {
78 if ((code >= 0) && (code <= MAX_ERRNO))
84 /* Linux doesn't require a credp for many functions, and crref is an expensive
85 * operation. This helper function avoids obtaining it for VerifyVCache calls
88 static inline int afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
93 if (avc->f.states & CStatd) {
101 code = afs_InitReq(&treq, credp);
103 code = afs_VerifyVCache2(avc, &treq);
110 return afs_convert_code(code);
114 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
117 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
119 afs_size_t isize, offindex;
123 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
124 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
126 code = afs_linux_VerifyVCache(vcp, NULL);
129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
130 isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
131 offindex = *offp >> PAGE_CACHE_SHIFT;
132 if(offindex > isize) {
137 /* Linux's FlushPages implementation doesn't ever use credp,
138 * so we optimise by not using it */
139 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
141 #ifdef HAVE_LINUX_DO_SYNC_READ
142 code = do_sync_read(fp, buf, count, offp);
144 code = generic_file_read(fp, buf, count, offp);
149 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
150 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
160 /* Now we have integrated VM for writes as well as reads. generic_file_write
161 * also takes care of re-positioning the pointer if file is open in append
162 * mode. Call fake open/close to ensure we do writes of core dumps.
165 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
168 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
173 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
174 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
175 (fp->f_flags & O_APPEND) ? 99998 : 99999);
177 code = afs_linux_VerifyVCache(vcp, &credp);
179 ObtainWriteLock(&vcp->lock, 529);
181 ReleaseWriteLock(&vcp->lock);
185 code = do_sync_write(fp, buf, count, offp);
187 code = generic_file_write(fp, buf, count, offp);
192 ObtainWriteLock(&vcp->lock, 530);
194 if (vcp->execsOrWriters == 1 && !credp)
197 afs_FakeClose(vcp, credp);
198 ReleaseWriteLock(&vcp->lock);
200 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
201 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
210 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
212 /* This is a complete rewrite of afs_readdir, since we can make use of
213 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
214 * handling and use of bulkstats will need to be reflected here as well.
217 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
219 struct vcache *avc = VTOAFS(FILE_INODE(fp));
220 struct vrequest treq;
226 struct DirBuffer entry;
229 afs_size_t origOffset, tlen;
230 cred_t *credp = crref();
231 struct afs_fakestat_state fakestat;
234 AFS_STATCNT(afs_readdir);
236 code = afs_convert_code(afs_InitReq(&treq, credp));
241 afs_InitFakeStat(&fakestat);
242 code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
246 /* update the cache entry */
248 code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
252 /* get a reference to the entire directory */
253 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
259 ObtainSharedLock(&avc->lock, 810);
260 UpgradeSToWLock(&avc->lock, 811);
261 ObtainReadLock(&tdc->lock);
263 * Make sure that the data in the cache is current. There are two
264 * cases we need to worry about:
265 * 1. The cache data is being fetched by another process.
266 * 2. The cache data is no longer valid
268 while ((avc->f.states & CStatd)
269 && (tdc->dflags & DFFetching)
270 && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
271 ReleaseReadLock(&tdc->lock);
272 ReleaseSharedLock(&avc->lock);
273 afs_osi_Sleep(&tdc->validPos);
274 ObtainSharedLock(&avc->lock, 812);
275 ObtainReadLock(&tdc->lock);
277 if (!(avc->f.states & CStatd)
278 || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
279 ReleaseReadLock(&tdc->lock);
280 ReleaseSharedLock(&avc->lock);
285 /* Set the readdir-in-progress flag, and downgrade the lock
286 * to shared so others will be able to acquire a read lock.
288 avc->f.states |= CReadDir;
289 avc->dcreaddir = tdc;
290 avc->readdir_pid = MyPidxx2Pid(MyPidxx);
291 ConvertWToSLock(&avc->lock);
293 /* Fill in until we get an error or we're done. This implementation
294 * takes an offset in units of blobs, rather than bytes.
297 offset = (int) fp->f_pos;
299 dirpos = BlobScan(tdc, offset);
303 code = afs_dir_GetBlob(tdc, dirpos, &entry);
306 de = (struct DirEntry *)entry.data;
308 ino = afs_calc_inum(avc->f.fid.Cell, avc->f.fid.Fid.Volume,
309 ntohl(de->fid.vnode));
312 len = strlen(de->name);
314 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
315 (unsigned long)&tdc->f.inode, dirpos);
317 ReleaseSharedLock(&avc->lock);
323 /* filldir returns -EINVAL when the buffer is full. */
324 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
326 unsigned int type = DT_UNKNOWN;
327 struct VenusFid afid;
330 afid.Cell = avc->f.fid.Cell;
331 afid.Fid.Volume = avc->f.fid.Fid.Volume;
332 afid.Fid.Vnode = ntohl(de->fid.vnode);
333 afid.Fid.Unique = ntohl(de->fid.vunique);
334 if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
336 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
339 } else if (((tvc->f.states) & (CStatd | CTruth))) {
340 /* CTruth will be set if the object has
345 else if (vtype == VREG)
347 /* Don't do this until we're sure it can't be a mtpt */
348 /* else if (vtype == VLNK)
350 /* what other types does AFS support? */
352 /* clean up from afs_FindVCache */
356 * If this is NFS readdirplus, then the filler is going to
357 * call getattr on this inode, which will deadlock if we're
361 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
365 code = (*filldir) (dirbuf, de->name, len, offset, ino);
370 offset = dirpos + 1 + ((len + 16) >> 5);
372 /* If filldir didn't fill in the last one this is still pointing to that
375 fp->f_pos = (loff_t) offset;
377 ReleaseReadLock(&tdc->lock);
379 UpgradeSToWLock(&avc->lock, 813);
380 avc->f.states &= ~CReadDir;
382 avc->readdir_pid = 0;
383 ReleaseSharedLock(&avc->lock);
387 afs_PutFakeStat(&fakestat);
394 /* in afs_pioctl.c */
395 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
398 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
399 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
401 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
408 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
410 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
414 #if defined(AFS_LINUX24_ENV)
415 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
416 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
417 vmap->vm_end - vmap->vm_start);
419 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
420 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
421 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
425 /* get a validated vcache entry */
426 code = afs_linux_VerifyVCache(vcp, NULL);
428 /* Linux's Flushpage implementation doesn't use credp, so optimise
429 * our code to not need to crref() it */
430 osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
432 code = generic_file_mmap(fp, vmap);
435 vcp->f.states |= CMAPPED;
442 afs_linux_open(struct inode *ip, struct file *fp)
444 struct vcache *vcp = VTOAFS(ip);
445 cred_t *credp = crref();
448 #ifdef AFS_LINUX24_ENV
452 code = afs_open(&vcp, fp->f_flags, credp);
454 #ifdef AFS_LINUX24_ENV
455 maybe_unlock_kernel();
459 return afs_convert_code(code);
463 afs_linux_release(struct inode *ip, struct file *fp)
465 struct vcache *vcp = VTOAFS(ip);
466 cred_t *credp = crref();
469 #ifdef AFS_LINUX24_ENV
473 code = afs_close(vcp, fp->f_flags, credp);
475 #ifdef AFS_LINUX24_ENV
476 maybe_unlock_kernel();
480 return afs_convert_code(code);
484 #if defined(AFS_LINUX24_ENV)
485 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
487 afs_linux_fsync(struct file *fp, struct dentry *dp)
491 struct inode *ip = FILE_INODE(fp);
492 cred_t *credp = crref();
494 #ifdef AFS_LINUX24_ENV
498 code = afs_fsync(VTOAFS(ip), credp);
500 #ifdef AFS_LINUX24_ENV
501 maybe_unlock_kernel();
504 return afs_convert_code(code);
510 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
513 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
514 cred_t *credp = crref();
515 struct AFS_FLOCK flock;
516 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
517 struct file_lock conflict;
518 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
519 struct file_lock *conflict;
522 /* Convert to a lock format afs_lockctl understands. */
523 memset(&flock, 0, sizeof(flock));
524 flock.l_type = flp->fl_type;
525 flock.l_pid = flp->fl_pid;
527 flock.l_start = flp->fl_start;
528 if (flp->fl_end == OFFSET_MAX)
529 flock.l_len = 0; /* Lock to end of file */
531 flock.l_len = flp->fl_end - flp->fl_start + 1;
533 /* Safe because there are no large files, yet */
534 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
535 if (cmd == F_GETLK64)
537 else if (cmd == F_SETLK64)
539 else if (cmd == F_SETLKW64)
541 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
544 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
547 #ifdef AFS_LINUX24_ENV
548 if ((code == 0 || flp->fl_type == F_UNLCK) &&
549 (cmd == F_SETLK || cmd == F_SETLKW)) {
550 # ifdef POSIX_LOCK_FILE_WAIT_ARG
551 code = posix_lock_file(fp, flp, 0);
553 flp->fl_flags &=~ FL_SLEEP;
554 code = posix_lock_file(fp, flp);
556 if (code && flp->fl_type != F_UNLCK) {
557 struct AFS_FLOCK flock2;
559 flock2.l_type = F_UNLCK;
561 afs_lockctl(vcp, &flock2, F_SETLK, credp);
565 /* If lockctl says there are no conflicting locks, then also check with the
566 * kernel, as lockctl knows nothing about byte range locks
568 if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
569 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
570 if (posix_test_lock(fp, flp, &conflict)) {
571 locks_copy_lock(flp, &conflict);
572 flp->fl_type = F_UNLCK;
576 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
577 if ((conflict = posix_test_lock(fp, flp))) {
578 locks_copy_lock(flp, conflict);
579 flp->fl_type = F_UNLCK;
584 posix_test_lock(fp, flp);
585 /* If we found a lock in the kernel's structure, return it */
586 if (flp->fl_type != F_UNLCK) {
594 /* Convert flock back to Linux's file_lock */
595 flp->fl_type = flock.l_type;
596 flp->fl_pid = flock.l_pid;
597 flp->fl_start = flock.l_start;
598 if (flock.l_len == 0)
599 flp->fl_end = OFFSET_MAX; /* Lock to end of file */
601 flp->fl_end = flock.l_start + flock.l_len - 1;
607 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
609 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
611 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
612 cred_t *credp = crref();
613 struct AFS_FLOCK flock;
614 /* Convert to a lock format afs_lockctl understands. */
615 memset(&flock, 0, sizeof(flock));
616 flock.l_type = flp->fl_type;
617 flock.l_pid = flp->fl_pid;
622 /* Safe because there are no large files, yet */
623 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
624 if (cmd == F_GETLK64)
626 else if (cmd == F_SETLK64)
628 else if (cmd == F_SETLKW64)
630 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
633 code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
636 if ((code == 0 || flp->fl_type == F_UNLCK) &&
637 (cmd == F_SETLK || cmd == F_SETLKW)) {
638 flp->fl_flags &=~ FL_SLEEP;
639 code = flock_lock_file_wait(fp, flp);
640 if (code && flp->fl_type != F_UNLCK) {
641 struct AFS_FLOCK flock2;
643 flock2.l_type = F_UNLCK;
645 afs_lockctl(vcp, &flock2, F_SETLK, credp);
649 /* Convert flock back to Linux's file_lock */
650 flp->fl_type = flock.l_type;
651 flp->fl_pid = flock.l_pid;
659 * essentially the same as afs_fsync() but we need to get the return
660 * code for the sys_close() here, not afs_linux_release(), so call
661 * afs_StoreAllSegments() with AFS_LASTSTORE
664 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
665 afs_linux_flush(struct file *fp, fl_owner_t id)
667 afs_linux_flush(struct file *fp)
670 struct vrequest treq;
678 if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
686 vcp = VTOAFS(FILE_INODE(fp));
688 code = afs_InitReq(&treq, credp);
691 /* If caching is bypassed for this file, or globally, just return 0 */
692 if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
695 ObtainReadLock(&vcp->lock);
696 if(vcp->cachingStates & FCSBypass)
698 ReleaseReadLock(&vcp->lock);
701 /* future proof: don't rely on 0 return from afs_InitReq */
705 ObtainSharedLock(&vcp->lock, 535);
706 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
707 UpgradeSToWLock(&vcp->lock, 536);
708 if (!AFS_IS_DISCONNECTED) {
709 code = afs_StoreAllSegments(vcp,
711 AFS_SYNC | AFS_LASTSTORE);
713 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
715 ConvertWToSLock(&vcp->lock);
717 code = afs_CheckCode(code, &treq, 54);
718 ReleaseSharedLock(&vcp->lock);
725 return afs_convert_code(code);
728 #if !defined(AFS_LINUX24_ENV)
729 /* Not allowed to directly read a directory. */
731 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
739 struct file_operations afs_dir_fops = {
740 #if !defined(AFS_LINUX24_ENV)
741 .read = afs_linux_dir_read,
742 .lock = afs_linux_lock,
743 .fsync = afs_linux_fsync,
745 .read = generic_read_dir,
747 .readdir = afs_linux_readdir,
748 #ifdef HAVE_UNLOCKED_IOCTL
749 .unlocked_ioctl = afs_unlocked_xioctl,
753 #ifdef HAVE_COMPAT_IOCTL
754 .compat_ioctl = afs_unlocked_xioctl,
756 .open = afs_linux_open,
757 .release = afs_linux_release,
760 struct file_operations afs_file_fops = {
761 .read = afs_linux_read,
762 .write = afs_linux_write,
763 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
764 .aio_read = generic_file_aio_read,
765 .aio_write = generic_file_aio_write,
767 #ifdef HAVE_UNLOCKED_IOCTL
768 .unlocked_ioctl = afs_unlocked_xioctl,
772 #ifdef HAVE_COMPAT_IOCTL
773 .compat_ioctl = afs_unlocked_xioctl,
775 .mmap = afs_linux_mmap,
776 .open = afs_linux_open,
777 .flush = afs_linux_flush,
778 .release = afs_linux_release,
779 .fsync = afs_linux_fsync,
780 .lock = afs_linux_lock,
781 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
782 .flock = afs_linux_flock,
786 static struct dentry *
787 canonical_dentry(struct inode *ip)
789 struct vcache *vcp = VTOAFS(ip);
790 struct dentry *first = NULL, *ret = NULL, *cur;
791 struct list_head *head, *prev, *tmp;
794 * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
795 * otherwise, use the first dentry in ip->i_dentry.
796 * if ip->i_dentry is empty, use the 'dentry' argument we were given.
798 /* note that vcp->target_link specifies which dentry to use, but we have
799 * no reference held on that dentry. so, we cannot use or dereference
800 * vcp->target_link itself, since it may have been freed. instead, we only
801 * use it to compare to pointers in the ip->i_dentry list. */
805 spin_lock(&dcache_lock);
807 head = &ip->i_dentry;
808 prev = ip->i_dentry.prev;
810 while (prev != head) {
813 cur = list_entry(tmp, struct dentry, d_alias);
815 if (!vcp->target_link || cur == vcp->target_link) {
828 vcp->target_link = ret;
833 spin_unlock(&dcache_lock);
838 /**********************************************************************
839 * AFS Linux dentry operations
840 **********************************************************************/
842 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
843 * that has its mvid (parent dir's fid) pointer set to the wrong directory
844 * due to being mounted in multiple points at once. If so, check_bad_parent()
845 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
846 * dotdotfid and mtpoint fid members.
848 * dp - dentry to be checked.
852 * This dentry's vcache's mvid will be set to the correct parent directory's
854 * This root vnode's volume will have its dotdotfid and mtpoint fids set
855 * to the correct parent and mountpoint fids.
859 check_bad_parent(struct dentry *dp)
862 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
863 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
865 if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) { /* bad parent */
868 /* force a lookup, so vcp->mvid is fixed up */
869 afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
870 if (!avc || vcp != avc) { /* bad, very bad.. */
871 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
872 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
873 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
874 ICL_TYPE_POINTER, dp);
877 AFS_RELE(AFSTOV(avc));
884 /* afs_linux_revalidate
885 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
888 afs_linux_revalidate(struct dentry *dp)
891 struct vcache *vcp = VTOAFS(dp->d_inode);
895 if (afs_shuttingdown)
898 #ifdef AFS_LINUX24_ENV
904 /* Make this a fast path (no crref), since it's called so often. */
905 if (vcp->f.states & CStatd) {
907 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
908 check_bad_parent(dp); /* check and correct mvid */
911 #ifdef AFS_LINUX24_ENV
918 /* This avoids the crref when we don't have to do it. Watch for
919 * changes in afs_getattr that don't get replicated here!
921 if (vcp->f.states & CStatd &&
922 (!afs_fakestat_enable || vcp->mvstat != 1) &&
924 (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
925 code = afs_CopyOutAttrs(vcp, &vattr);
928 code = afs_getattr(vcp, &vattr, credp);
932 afs_fill_inode(AFSTOV(vcp), &vattr);
935 #ifdef AFS_LINUX24_ENV
936 maybe_unlock_kernel();
939 return afs_convert_code(code);
943 * Set iattr data into vattr. Assume vattr cleared before call.
946 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
948 vattrp->va_mask = iattrp->ia_valid;
949 if (iattrp->ia_valid & ATTR_MODE)
950 vattrp->va_mode = iattrp->ia_mode;
951 if (iattrp->ia_valid & ATTR_UID)
952 vattrp->va_uid = iattrp->ia_uid;
953 if (iattrp->ia_valid & ATTR_GID)
954 vattrp->va_gid = iattrp->ia_gid;
955 if (iattrp->ia_valid & ATTR_SIZE)
956 vattrp->va_size = iattrp->ia_size;
957 if (iattrp->ia_valid & ATTR_ATIME) {
958 vattrp->va_atime.tv_sec = iattrp->ia_atime;
959 vattrp->va_atime.tv_usec = 0;
961 if (iattrp->ia_valid & ATTR_MTIME) {
962 vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
963 vattrp->va_mtime.tv_usec = 0;
965 if (iattrp->ia_valid & ATTR_CTIME) {
966 vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
967 vattrp->va_ctime.tv_usec = 0;
972 * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
975 vattr2inode(struct inode *ip, struct vattr *vp)
977 ip->i_ino = vp->va_nodeid;
978 ip->i_nlink = vp->va_nlink;
979 ip->i_blocks = vp->va_blocks;
980 #ifdef STRUCT_INODE_HAS_I_BLKBITS
981 ip->i_blkbits = AFS_BLKBITS;
983 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
984 ip->i_blksize = vp->va_blocksize;
986 ip->i_rdev = vp->va_rdev;
987 ip->i_mode = vp->va_mode;
988 ip->i_uid = vp->va_uid;
989 ip->i_gid = vp->va_gid;
990 i_size_write(ip, vp->va_size);
991 ip->i_atime = vp->va_atime.tv_sec;
992 ip->i_mtime = vp->va_mtime.tv_sec;
993 ip->i_ctime = vp->va_ctime.tv_sec;
997 * Linux version of setattr call. What to change is in the iattr struct.
998 * We need to set bits in both the Linux inode as well as the vcache.
1001 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1004 cred_t *credp = crref();
1005 struct inode *ip = dp->d_inode;
1009 iattr2vattr(&vattr, iattrp); /* Convert for AFS vnodeops call. */
1012 code = afs_setattr(VTOAFS(ip), &vattr, credp);
1014 afs_getattr(VTOAFS(ip), &vattr, credp);
1015 vattr2inode(ip, &vattr);
1019 return afs_convert_code(code);
1022 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1023 * In kernels 2.2.10 and above, we are passed an additional flags var which
1024 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1025 * we are advised to follow the entry if it is a link or to make sure that
1026 * it is a directory. But since the kernel itself checks these possibilities
1027 * later on, we shouldn't have to do it until later. Perhaps in the future..
1030 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1031 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
1032 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1034 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1037 afs_linux_dentry_revalidate(struct dentry *dp)
1041 cred_t *credp = NULL;
1042 struct vcache *vcp, *pvcp, *tvc = NULL;
1044 struct afs_fakestat_state fakestate;
1046 #ifdef AFS_LINUX24_ENV
1047 maybe_lock_kernel();
1050 afs_InitFakeStat(&fakestate);
1054 vcp = VTOAFS(dp->d_inode);
1055 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
1057 if (vcp == afs_globalVp)
1060 if (vcp->mvstat == 1) { /* mount point */
1061 if (vcp->mvid && (vcp->f.states & CMValid)) {
1062 int tryEvalOnly = 0;
1064 struct vrequest treq;
1067 code = afs_InitReq(&treq, credp);
1069 #ifdef AFS_DARWIN_ENV
1070 (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
1071 (strcmp(dp->d_name.name, "Contents") == 0) ||
1073 (strcmp(dp->d_name.name, ".directory") == 0)) {
1077 code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
1079 code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
1080 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1081 /* a mount point, not yet replaced by its directory */
1086 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
1087 check_bad_parent(dp); /* check and correct mvid */
1090 /* If the last looker changes, we should make sure the current
1091 * looker still has permission to examine this file. This would
1092 * always require a crref() which would be "slow".
1094 if (vcp->last_looker != treq.uid) {
1095 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
1098 vcp->last_looker = treq.uid;
1102 /* If the parent's DataVersion has changed or the vnode
1103 * is longer valid, we need to do a full lookup. VerifyVCache
1104 * isn't enough since the vnode may have been renamed.
1107 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
1110 afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1111 if (!tvc || tvc != vcp)
1114 if (afs_getattr(vcp, &vattr, credp))
1117 vattr2inode(AFSTOV(vcp), &vattr);
1118 dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1121 /* should we always update the attributes at this point? */
1122 /* unlikely--the vcache entry hasn't changed */
1126 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
1127 if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1131 /* No change in parent's DataVersion so this negative
1132 * lookup is still valid. BUT, if a server is down a
1133 * negative lookup can result so there should be a
1134 * liftime as well. For now, always expire.
1147 afs_PutFakeStat(&fakestate);
1153 shrink_dcache_parent(dp);
1156 #ifdef AFS_LINUX24_ENV
1157 maybe_unlock_kernel();
1162 if (have_submounts(dp))
1170 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1172 struct vcache *vcp = VTOAFS(ip);
1175 if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1176 (void) afs_InactiveVCache(vcp, NULL);
1179 #ifdef DCACHE_NFSFS_RENAMED
1180 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
1187 afs_dentry_delete(struct dentry *dp)
1189 if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1190 return 1; /* bad inode? */
1195 struct dentry_operations afs_dentry_operations = {
1196 .d_revalidate = afs_linux_dentry_revalidate,
1197 .d_delete = afs_dentry_delete,
1198 .d_iput = afs_dentry_iput,
1201 /**********************************************************************
1202 * AFS Linux inode operations
1203 **********************************************************************/
1207 * Merely need to set enough of vattr to get us through the create. Note
1208 * that the higher level code (open_namei) will take care of any tuncation
1209 * explicitly. Exclusive open is also taken care of in open_namei.
1211 * name is in kernel space at this point.
1214 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1215 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1216 struct nameidata *nd)
1218 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1222 cred_t *credp = crref();
1223 const char *name = dp->d_name.name;
1228 vattr.va_mode = mode;
1229 vattr.va_type = mode & S_IFMT;
1232 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1236 struct inode *ip = AFSTOV(vcp);
1238 afs_getattr(vcp, &vattr, credp);
1239 afs_fill_inode(ip, &vattr);
1240 insert_inode_hash(ip);
1241 dp->d_op = &afs_dentry_operations;
1242 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1243 d_instantiate(dp, ip);
1248 return afs_convert_code(code);
1251 /* afs_linux_lookup */
1252 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1253 static struct dentry *
1254 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1255 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1256 struct nameidata *nd)
1258 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1262 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1265 cred_t *credp = crref();
1266 struct vcache *vcp = NULL;
1267 const char *comp = dp->d_name.name;
1268 struct inode *ip = NULL;
1272 code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1276 struct vcache *parent_vc = VTOAFS(dip);
1278 if (parent_vc == vcp) {
1279 /* This is possible if the parent dir is a mountpoint to a volume,
1280 * and the dir entry we looked up is a mountpoint to the same
1281 * volume. Linux cannot cope with this, so return an error instead
1282 * of risking a deadlock or panic. */
1290 afs_getattr(vcp, &vattr, credp);
1291 afs_fill_inode(ip, &vattr);
1293 #ifdef HAVE_LINUX_HLIST_UNHASHED
1294 hlist_unhashed(&ip->i_hash)
1296 ip->i_hash.prev == NULL
1299 insert_inode_hash(ip);
1301 dp->d_op = &afs_dentry_operations;
1302 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1305 #if defined(AFS_LINUX24_ENV)
1306 if (ip && S_ISDIR(ip->i_mode)) {
1308 struct dentry *alias;
1313 /* Try to invalidate an existing alias in favor of our new one */
1314 alias = d_find_alias(ip);
1315 /* But not if it's disconnected; then we want d_splice_alias below */
1317 if (d_invalidate(alias) == 0) {
1318 /* there may be more aliases; try again until we run out */
1332 /* It's ok for the file to not be found. That's noted by the caller by
1333 * seeing that the dp->d_inode field is NULL.
1335 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1339 return ERR_PTR(afs_convert_code(code));
1343 return afs_convert_code(code);
1348 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1351 cred_t *credp = crref();
1352 const char *name = newdp->d_name.name;
1353 struct inode *oldip = olddp->d_inode;
1355 /* If afs_link returned the vnode, we could instantiate the
1356 * dentry. Since it's not, we drop this one and do a new lookup.
1361 code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1365 return afs_convert_code(code);
1369 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1372 cred_t *credp = crref();
1373 const char *name = dp->d_name.name;
1374 struct vcache *tvc = VTOAFS(dp->d_inode);
1376 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1377 && !(tvc->f.states & CUnlinked)) {
1378 struct dentry *__dp;
1388 osi_FreeSmallSpace(__name);
1389 __name = afs_newname();
1392 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1396 } while (__dp->d_inode != NULL);
1399 code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1401 tvc->mvid = (void *) __name;
1404 crfree(tvc->uncred);
1406 tvc->uncred = credp;
1407 tvc->f.states |= CUnlinked;
1408 #ifdef DCACHE_NFSFS_RENAMED
1409 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1412 osi_FreeSmallSpace(__name);
1417 __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1426 code = afs_remove(VTOAFS(dip), (char *)name, credp);
1432 return afs_convert_code(code);
1437 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1440 cred_t *credp = crref();
1442 const char *name = dp->d_name.name;
1444 /* If afs_symlink returned the vnode, we could instantiate the
1445 * dentry. Since it's not, we drop this one and do a new lookup.
1451 code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1454 return afs_convert_code(code);
1458 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1461 cred_t *credp = crref();
1462 struct vcache *tvcp = NULL;
1464 const char *name = dp->d_name.name;
1467 vattr.va_mask = ATTR_MODE;
1468 vattr.va_mode = mode;
1470 code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1473 struct inode *ip = AFSTOV(tvcp);
1475 afs_getattr(tvcp, &vattr, credp);
1476 afs_fill_inode(ip, &vattr);
1478 dp->d_op = &afs_dentry_operations;
1479 dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1480 d_instantiate(dp, ip);
1485 return afs_convert_code(code);
1489 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1492 cred_t *credp = crref();
1493 const char *name = dp->d_name.name;
1495 /* locking kernel conflicts with glock? */
1498 code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1501 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1502 * that failed because a directory is not empty. So, we map
1503 * EEXIST to ENOTEMPTY on linux.
1505 if (code == EEXIST) {
1514 return afs_convert_code(code);
1519 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1520 struct inode *newip, struct dentry *newdp)
1523 cred_t *credp = crref();
1524 const char *oldname = olddp->d_name.name;
1525 const char *newname = newdp->d_name.name;
1526 struct dentry *rehash = NULL;
1528 if (!list_empty(&newdp->d_hash)) {
1533 #if defined(AFS_LINUX24_ENV)
1534 if (atomic_read(&olddp->d_count) > 1)
1535 shrink_dcache_parent(olddp);
1539 code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1543 olddp->d_time = 0; /* force to revalidate */
1549 return afs_convert_code(code);
1553 /* afs_linux_ireadlink
1554 * Internal readlink which can return link contents to user or kernel space.
1555 * Note that the buffer is NOT supposed to be null-terminated.
1558 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1561 cred_t *credp = crref();
1565 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1566 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1570 return maxlen - tuio.uio_resid;
1572 return afs_convert_code(code);
1575 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1576 /* afs_linux_readlink
1577 * Fill target (which is in user space) with contents of symlink.
1580 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1583 struct inode *ip = dp->d_inode;
1586 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1592 /* afs_linux_follow_link
1593 * a file system dependent link following routine.
1595 #if defined(AFS_LINUX24_ENV)
1596 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1601 name = osi_Alloc(PATH_MAX);
1607 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1615 code = vfs_follow_link(nd, name);
1618 osi_Free(name, PATH_MAX);
1623 #else /* !defined(AFS_LINUX24_ENV) */
1625 static struct dentry *
1626 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1627 unsigned int follow)
1635 name = osi_Alloc(PATH_MAX + 1);
1639 return ERR_PTR(-EIO);
1642 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1647 if (code < -MAX_ERRNO)
1648 res = ERR_PTR(-EIO);
1650 res = ERR_PTR(code);
1653 res = lookup_dentry(name, basep, follow);
1657 osi_Free(name, PATH_MAX + 1);
1661 #endif /* AFS_LINUX24_ENV */
1662 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1665 afs_linux_can_bypass(struct inode *ip) {
1666 switch(cache_bypass_strategy) {
1667 case NEVER_BYPASS_CACHE:
1669 case ALWAYS_BYPASS_CACHE:
1671 case LARGE_FILES_BYPASS_CACHE:
1672 if(i_size_read(ip) > cache_bypass_threshold)
1679 /* afs_linux_readpage
1680 * all reads come through here. A strategy-like read call.
1683 afs_linux_readpage(struct file *fp, struct page *pp)
1686 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1688 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1690 ulong address = afs_linux_page_address(pp);
1691 afs_offs_t offset = pageoff(pp);
1693 afs_int32 bypasscache = 0; /* bypass for this read */
1694 struct nocache_read_request *ancr;
1695 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1699 struct iovec *iovecp;
1700 struct inode *ip = FILE_INODE(fp);
1701 afs_int32 cnt = page_count(pp);
1702 struct vcache *avc = VTOAFS(ip);
1706 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1710 atomic_add(1, &pp->count);
1711 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1712 clear_bit(PG_error, &pp->flags);
1714 /* if bypasscache, receiver frees, else we do */
1715 auio = osi_Alloc(sizeof(struct uio));
1716 iovecp = osi_Alloc(sizeof(struct iovec));
1718 setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1721 bypasscache = afs_linux_can_bypass(ip);
1723 /* In the new incarnation of selective caching, a file's caching policy
1724 * can change, eg because file size exceeds threshold, etc. */
1725 trydo_cache_transition(avc, credp, bypasscache);
1730 /* save the page for background map */
1731 auio->uio_iov->iov_base = (void*) pp;
1732 /* the background thread will free this */
1733 ancr = osi_Alloc(sizeof(struct nocache_read_request));
1735 ancr->offset = offset;
1736 ancr->length = PAGE_SIZE;
1738 maybe_lock_kernel();
1739 code = afs_ReadNoCache(avc, ancr, credp);
1740 maybe_unlock_kernel();
1742 goto done; /* skips release page, doing it in bg thread */
1745 #ifdef AFS_LINUX24_ENV
1746 maybe_lock_kernel();
1750 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1751 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1752 99999); /* not a possible code value */
1754 code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1756 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1757 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1759 AFS_DISCON_UNLOCK();
1761 #ifdef AFS_LINUX24_ENV
1762 maybe_unlock_kernel();
1765 /* XXX valid for no-cache also? Check last bits of files... :)
1766 * Cognate code goes in afs_NoCacheFetchProc. */
1767 if (auio->uio_resid) /* zero remainder of page */
1768 memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1771 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1772 flush_dcache_page(pp);
1773 SetPageUptodate(pp);
1775 set_bit(PG_uptodate, &pp->flags);
1779 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1783 clear_bit(PG_locked, &pp->flags);
1788 /* do not call afs_GetDCache if cache is bypassed */
1792 /* free if not bypassing cache */
1793 osi_Free(auio, sizeof(struct uio));
1794 osi_Free(iovecp, sizeof(struct iovec));
1796 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1798 struct vrequest treq;
1801 code = afs_InitReq(&treq, credp);
1802 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1803 tdc = afs_FindDCache(avc, offset);
1805 if (!(tdc->mflags & DFNextStarted))
1806 afs_PrefetchChunk(avc, tdc, credp, &treq);
1809 ReleaseWriteLock(&avc->lock);
1816 return afs_convert_code(code);
1819 #if defined(AFS_LINUX24_ENV)
1821 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1822 unsigned long offset, unsigned int count)
1824 struct vcache *vcp = VTOAFS(ip);
1833 buffer = kmap(pp) + offset;
1834 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1837 maybe_lock_kernel();
1839 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1840 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1841 ICL_TYPE_INT32, 99999);
1843 ObtainWriteLock(&vcp->lock, 532);
1844 if (vcp->f.states & CPageWrite) {
1845 ReleaseWriteLock(&vcp->lock);
1847 maybe_unlock_kernel();
1850 /* should mark it dirty? */
1853 vcp->f.states |= CPageWrite;
1854 ReleaseWriteLock(&vcp->lock);
1856 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1858 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1860 i_size_write(ip, vcp->f.m.Length);
1861 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1863 ObtainWriteLock(&vcp->lock, 533);
1865 struct vrequest treq;
1867 if (!afs_InitReq(&treq, credp))
1868 code = afs_DoPartialWrite(vcp, &treq);
1870 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1872 vcp->f.states &= ~CPageWrite;
1873 ReleaseWriteLock(&vcp->lock);
1875 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1876 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1877 ICL_TYPE_INT32, code);
1880 maybe_unlock_kernel();
1889 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1890 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1892 afs_linux_writepage(struct page *pp)
1895 struct address_space *mapping = pp->mapping;
1896 struct inode *inode;
1897 unsigned long end_index;
1898 unsigned offset = PAGE_CACHE_SIZE;
1901 if (PageLaunder(pp)) {
1902 return(fail_writepage(pp));
1905 inode = (struct inode *)mapping->host;
1906 end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
1909 if (pp->index < end_index)
1911 /* things got complicated... */
1912 offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1913 /* OK, are we completely out? */
1914 if (pp->index >= end_index + 1 || !offset)
1917 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1918 SetPageUptodate(pp);
1920 if (status == offset)
1926 /* afs_linux_updatepage
1927 * What one would have thought was writepage - write dirty page to file.
1928 * Called from generic_file_write. buffer is still in user space. pagep
1929 * has been filled in with old data if we're updating less than a page.
1932 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1933 unsigned int count, int sync)
1935 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1936 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1942 set_bit(PG_locked, &pp->flags);
1947 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1948 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1949 ICL_TYPE_INT32, 99999);
1950 setup_uio(&tuio, &iovec, page_addr + offset,
1951 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1954 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1956 i_size_write(ip, vcp->f.m.Length);
1957 ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1960 struct vrequest treq;
1962 ObtainWriteLock(&vcp->lock, 533);
1963 vcp->f.m.Date = osi_Time(); /* set modification time */
1964 if (!afs_InitReq(&treq, credp))
1965 code = afs_DoPartialWrite(vcp, &treq);
1966 ReleaseWriteLock(&vcp->lock);
1969 code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1970 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1971 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1972 ICL_TYPE_INT32, code);
1974 AFS_DISCON_UNLOCK();
1978 clear_bit(PG_locked, &pp->flags);
1983 /* afs_linux_permission
1984 * Check access rights - returns error if can't check or permission denied.
1987 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1988 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1990 afs_linux_permission(struct inode *ip, int mode)
1994 cred_t *credp = crref();
1998 if (mode & MAY_EXEC)
2000 if (mode & MAY_READ)
2002 if (mode & MAY_WRITE)
2004 code = afs_access(VTOAFS(ip), tmp, credp);
2008 return afs_convert_code(code);
2011 #if defined(AFS_LINUX24_ENV) && !defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2013 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2018 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2019 offset, to - offset);
2026 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2029 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2030 call kmap directly instead of relying on us to do it */
2036 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2038 afs_linux_write_end(struct file *file, struct address_space *mapping,
2039 loff_t pos, unsigned len, unsigned copied,
2040 struct page *page, void *fsdata)
2043 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2045 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2048 page_cache_release(page);
2053 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2054 loff_t pos, unsigned len, unsigned flags,
2055 struct page **pagep, void **fsdata)
2058 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2059 #if defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
2060 page = grab_cache_page_write_begin(mapping, index, flags);
2062 page = __grab_cache_page(mapping, index);
2071 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2073 struct dentry **dpp;
2074 struct dentry *target;
2076 target = canonical_dentry(dentry->d_inode);
2085 *dpp = dget(dentry);
2091 static struct inode_operations afs_file_iops = {
2092 #if defined(AFS_LINUX24_ENV)
2093 .permission = afs_linux_permission,
2094 .revalidate = afs_linux_revalidate,
2095 .setattr = afs_notify_change,
2097 .default_file_ops = &afs_file_fops,
2098 .readpage = afs_linux_readpage,
2099 .revalidate = afs_linux_revalidate,
2100 .updatepage = afs_linux_updatepage,
2104 #if defined(AFS_LINUX24_ENV)
2105 static struct address_space_operations afs_file_aops = {
2106 .readpage = afs_linux_readpage,
2107 .writepage = afs_linux_writepage,
2108 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2109 .write_begin = afs_linux_write_begin,
2110 .write_end = afs_linux_write_end,
2112 .commit_write = afs_linux_commit_write,
2113 .prepare_write = afs_linux_prepare_write,
2119 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2120 * by what sort of operation is allowed.....
2123 static struct inode_operations afs_dir_iops = {
2124 #if !defined(AFS_LINUX24_ENV)
2125 .default_file_ops = &afs_dir_fops,
2127 .setattr = afs_notify_change,
2129 .create = afs_linux_create,
2130 .lookup = afs_linux_lookup,
2131 .link = afs_linux_link,
2132 .unlink = afs_linux_unlink,
2133 .symlink = afs_linux_symlink,
2134 .mkdir = afs_linux_mkdir,
2135 .rmdir = afs_linux_rmdir,
2136 .rename = afs_linux_rename,
2137 .revalidate = afs_linux_revalidate,
2138 .permission = afs_linux_permission,
2139 .follow_link = afs_linux_dir_follow_link,
2142 /* We really need a separate symlink set of ops, since do_follow_link()
2143 * determines if it _is_ a link by checking if the follow_link op is set.
2145 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2147 afs_symlink_filler(struct file *file, struct page *page)
2149 struct inode *ip = (struct inode *)page->mapping->host;
2150 char *p = (char *)kmap(page);
2153 maybe_lock_kernel();
2155 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2160 p[code] = '\0'; /* null terminate? */
2161 maybe_unlock_kernel();
2163 SetPageUptodate(page);
2169 maybe_unlock_kernel();
2177 static struct address_space_operations afs_symlink_aops = {
2178 .readpage = afs_symlink_filler
2180 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2182 static struct inode_operations afs_symlink_iops = {
2183 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2184 .readlink = page_readlink,
2185 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2186 .follow_link = page_follow_link,
2188 .follow_link = page_follow_link_light,
2189 .put_link = page_put_link,
2191 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2192 .readlink = afs_linux_readlink,
2193 .follow_link = afs_linux_follow_link,
2194 #if !defined(AFS_LINUX24_ENV)
2195 .permission = afs_linux_permission,
2196 .revalidate = afs_linux_revalidate,
2198 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2199 #if defined(AFS_LINUX24_ENV)
2200 .setattr = afs_notify_change,
2205 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2209 vattr2inode(ip, vattr);
2211 /* Reset ops if symlink or directory. */
2212 if (S_ISREG(ip->i_mode)) {
2213 ip->i_op = &afs_file_iops;
2214 #if defined(AFS_LINUX24_ENV)
2215 ip->i_fop = &afs_file_fops;
2216 ip->i_data.a_ops = &afs_file_aops;
2219 } else if (S_ISDIR(ip->i_mode)) {
2220 ip->i_op = &afs_dir_iops;
2221 #if defined(AFS_LINUX24_ENV)
2222 ip->i_fop = &afs_dir_fops;
2225 } else if (S_ISLNK(ip->i_mode)) {
2226 ip->i_op = &afs_symlink_iops;
2227 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2228 ip->i_data.a_ops = &afs_symlink_aops;
2229 ip->i_mapping = &ip->i_data;