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"
43 #define pageoff(pp) pgoff2loff((pp)->index)
45 #define pageoff(pp) pp->offset
48 #if defined(AFS_LINUX26_ENV)
49 #define UnlockPage(pp) unlock_page(pp)
52 extern struct vcache *afs_globalVp;
54 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
57 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
58 cred_t *credp = crref();
62 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
63 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
66 /* get a validated vcache entry */
67 code = afs_InitReq(&treq, credp);
69 code = afs_VerifyVCache(vcp, &treq);
74 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
77 code = do_sync_read(fp, buf, count, offp);
79 code = generic_file_read(fp, buf, count, offp);
84 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
85 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
94 /* Now we have integrated VM for writes as well as reads. generic_file_write
95 * also takes care of re-positioning the pointer if file is open in append
96 * mode. Call fake open/close to ensure we do writes of core dumps.
99 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
103 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
104 struct vrequest treq;
105 cred_t *credp = crref();
109 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
110 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
111 (fp->f_flags & O_APPEND) ? 99998 : 99999);
114 /* get a validated vcache entry */
115 code = (ssize_t) afs_InitReq(&treq, credp);
117 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
119 ObtainWriteLock(&vcp->lock, 529);
121 ReleaseWriteLock(&vcp->lock);
127 code = do_sync_write(fp, buf, count, offp);
129 code = generic_file_write(fp, buf, count, offp);
134 ObtainWriteLock(&vcp->lock, 530);
135 afs_FakeClose(vcp, credp);
136 ReleaseWriteLock(&vcp->lock);
138 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
139 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
147 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
149 /* This is a complete rewrite of afs_readdir, since we can make use of
150 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
151 * handling and use of bulkstats will need to be reflected here as well.
154 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
156 extern struct DirEntry *afs_dir_GetBlob();
157 struct vcache *avc = VTOAFS(FILE_INODE(fp));
158 struct vrequest treq;
159 register struct dcache *tdc;
166 afs_size_t origOffset, tlen;
167 cred_t *credp = crref();
168 struct afs_fakestat_state fakestat;
170 #if defined(AFS_LINUX26_ENV)
174 AFS_STATCNT(afs_readdir);
176 code = afs_InitReq(&treq, credp);
181 afs_InitFakeStat(&fakestat);
182 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
186 /* update the cache entry */
188 code = afs_VerifyVCache(avc, &treq);
192 /* get a reference to the entire directory */
193 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
199 ObtainSharedLock(&avc->lock, 810);
200 UpgradeSToWLock(&avc->lock, 811);
201 ObtainReadLock(&tdc->lock);
203 * Make sure that the data in the cache is current. There are two
204 * cases we need to worry about:
205 * 1. The cache data is being fetched by another process.
206 * 2. The cache data is no longer valid
208 while ((avc->states & CStatd)
209 && (tdc->dflags & DFFetching)
210 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
211 ReleaseReadLock(&tdc->lock);
212 ReleaseSharedLock(&avc->lock);
213 afs_osi_Sleep(&tdc->validPos);
214 ObtainSharedLock(&avc->lock, 812);
215 ObtainReadLock(&tdc->lock);
217 if (!(avc->states & CStatd)
218 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
219 ReleaseReadLock(&tdc->lock);
220 ReleaseSharedLock(&avc->lock);
225 /* Set the readdir-in-progress flag, and downgrade the lock
226 * to shared so others will be able to acquire a read lock.
228 avc->states |= CReadDir;
229 avc->dcreaddir = tdc;
230 avc->readdir_pid = MyPidxx;
231 ConvertWToSLock(&avc->lock);
233 /* Fill in until we get an error or we're done. This implementation
234 * takes an offset in units of blobs, rather than bytes.
237 offset = (int) fp->f_pos;
239 dirpos = BlobScan(tdc, offset);
243 de = afs_dir_GetBlob(tdc, dirpos);
247 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
250 len = strlen(de->name);
252 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
253 (unsigned long)&tdc->f.inode, dirpos);
254 DRelease((struct buffer *) de, 0);
255 ReleaseSharedLock(&avc->lock);
261 /* filldir returns -EINVAL when the buffer is full. */
262 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
264 unsigned int type = DT_UNKNOWN;
265 struct VenusFid afid;
268 afid.Cell = avc->fid.Cell;
269 afid.Fid.Volume = avc->fid.Fid.Volume;
270 afid.Fid.Vnode = ntohl(de->fid.vnode);
271 afid.Fid.Unique = ntohl(de->fid.vunique);
272 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
274 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
277 } else if (((tvc->states) & (CStatd | CTruth))) {
278 /* CTruth will be set if the object has
283 else if (vtype == VREG)
285 /* Don't do this until we're sure it can't be a mtpt */
286 /* else if (vtype == VLNK)
288 /* what other types does AFS support? */
290 /* clean up from afs_FindVCache */
294 * If this is NFS readdirplus, then the filler is going to
295 * call getattr on this inode, which will deadlock if we're
299 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
303 code = (*filldir) (dirbuf, de->name, len, offset, ino);
305 DRelease((struct buffer *)de, 0);
308 offset = dirpos + 1 + ((len + 16) >> 5);
310 /* If filldir didn't fill in the last one this is still pointing to that
313 fp->f_pos = (loff_t) offset;
315 ReleaseReadLock(&tdc->lock);
317 UpgradeSToWLock(&avc->lock, 813);
318 avc->states &= ~CReadDir;
320 avc->readdir_pid = 0;
321 ReleaseSharedLock(&avc->lock);
325 afs_PutFakeStat(&fakestat);
328 #if defined(AFS_LINUX26_ENV)
335 /* in afs_pioctl.c */
336 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
339 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
340 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
342 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
349 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
351 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
352 cred_t *credp = crref();
353 struct vrequest treq;
357 #if defined(AFS_LINUX24_ENV)
358 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
359 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
360 vmap->vm_end - vmap->vm_start);
362 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
363 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
364 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
368 /* get a validated vcache entry */
369 code = afs_InitReq(&treq, credp);
373 code = afs_VerifyVCache(vcp, &treq);
377 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
380 code = generic_file_mmap(fp, vmap);
383 vcp->states |= CMAPPED;
396 afs_linux_open(struct inode *ip, struct file *fp)
398 struct vcache *vcp = VTOAFS(ip);
399 cred_t *credp = crref();
402 #ifdef AFS_LINUX24_ENV
406 code = afs_open(&vcp, fp->f_flags, credp);
408 #ifdef AFS_LINUX24_ENV
417 afs_linux_release(struct inode *ip, struct file *fp)
419 struct vcache *vcp = VTOAFS(ip);
420 cred_t *credp = crref();
423 #ifdef AFS_LINUX24_ENV
427 code = afs_close(vcp, fp->f_flags, credp);
429 #ifdef AFS_LINUX24_ENV
438 #if defined(AFS_LINUX24_ENV)
439 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
441 afs_linux_fsync(struct file *fp, struct dentry *dp)
445 struct inode *ip = FILE_INODE(fp);
446 cred_t *credp = crref();
448 #ifdef AFS_LINUX24_ENV
452 code = afs_fsync(VTOAFS(ip), credp);
454 #ifdef AFS_LINUX24_ENV
464 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
467 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
468 cred_t *credp = crref();
469 struct AFS_FLOCK flock;
470 /* Convert to a lock format afs_lockctl understands. */
471 memset((char *)&flock, 0, sizeof(flock));
472 flock.l_type = flp->fl_type;
473 flock.l_pid = flp->fl_pid;
475 flock.l_start = flp->fl_start;
476 flock.l_len = flp->fl_end - flp->fl_start;
478 /* Safe because there are no large files, yet */
479 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
480 if (cmd == F_GETLK64)
482 else if (cmd == F_SETLK64)
484 else if (cmd == F_SETLKW64)
486 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
489 code = afs_lockctl(vcp, &flock, cmd, credp);
492 #ifdef AFS_LINUX24_ENV
493 if ((code == 0 || flp->fl_type == F_UNLCK) &&
494 (cmd == F_SETLK || cmd == F_SETLKW)) {
495 #ifdef AFS_LINUX26_ENV
496 struct file_lock flp2;
498 flp2.fl_flags &=~ FL_SLEEP;
499 code = posix_lock_file(fp, &flp2);
501 code = posix_lock_file(fp, flp, 0);
503 if (code && flp->fl_type != F_UNLCK) {
504 struct AFS_FLOCK flock2;
506 flock2.l_type = F_UNLCK;
508 afs_lockctl(vcp, &flock2, F_SETLK, credp);
513 /* Convert flock back to Linux's file_lock */
514 flp->fl_type = flock.l_type;
515 flp->fl_pid = flock.l_pid;
516 flp->fl_start = flock.l_start;
517 flp->fl_end = flock.l_start + flock.l_len;
524 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
526 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
528 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
529 cred_t *credp = crref();
530 struct AFS_FLOCK flock;
531 /* Convert to a lock format afs_lockctl understands. */
532 memset((char *)&flock, 0, sizeof(flock));
533 flock.l_type = flp->fl_type;
534 flock.l_pid = flp->fl_pid;
537 flock.l_len = OFFSET_MAX;
539 /* Safe because there are no large files, yet */
540 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
541 if (cmd == F_GETLK64)
543 else if (cmd == F_SETLK64)
545 else if (cmd == F_SETLKW64)
547 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
550 code = afs_lockctl(vcp, &flock, cmd, credp);
553 if ((code == 0 || flp->fl_type == F_UNLCK) &&
554 (cmd == F_SETLK || cmd == F_SETLKW)) {
555 struct file_lock flp2;
557 flp2.fl_flags &=~ FL_SLEEP;
558 code = flock_lock_file_wait(fp, &flp2);
559 if (code && flp->fl_type != F_UNLCK) {
560 struct AFS_FLOCK flock2;
562 flock2.l_type = F_UNLCK;
564 afs_lockctl(vcp, &flock2, F_SETLK, credp);
568 /* Convert flock back to Linux's file_lock */
569 flp->fl_type = flock.l_type;
570 flp->fl_pid = flock.l_pid;
578 * essentially the same as afs_fsync() but we need to get the return
579 * code for the sys_close() here, not afs_linux_release(), so call
580 * afs_StoreAllSegments() with AFS_LASTSTORE
583 afs_linux_flush(struct file *fp)
585 struct vrequest treq;
586 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
587 cred_t *credp = crref();
592 code = afs_InitReq(&treq, credp);
596 ObtainSharedLock(&vcp->lock, 535);
597 if (vcp->execsOrWriters > 0) {
598 UpgradeSToWLock(&vcp->lock, 536);
599 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
600 ConvertWToSLock(&vcp->lock);
602 code = afs_CheckCode(code, &treq, 54);
603 ReleaseSharedLock(&vcp->lock);
612 #if !defined(AFS_LINUX24_ENV)
613 /* Not allowed to directly read a directory. */
615 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
623 struct file_operations afs_dir_fops = {
624 #if !defined(AFS_LINUX24_ENV)
625 .read = afs_linux_dir_read,
626 .lock = afs_linux_lock,
627 .fsync = afs_linux_fsync,
629 .read = generic_read_dir,
631 .readdir = afs_linux_readdir,
632 #ifdef HAVE_UNLOCKED_IOCTL
633 .unlocked_ioctl = afs_unlocked_xioctl,
637 #ifdef HAVE_COMPAT_IOCTL
638 .compat_ioctl = afs_unlocked_xioctl,
640 .open = afs_linux_open,
641 .release = afs_linux_release,
644 struct file_operations afs_file_fops = {
645 .read = afs_linux_read,
646 .write = afs_linux_write,
647 #ifdef GENERIC_FILE_AIO_READ
648 .aio_read = generic_file_aio_read,
649 .aio_write = generic_file_aio_write,
651 #ifdef HAVE_UNLOCKED_IOCTL
652 .unlocked_ioctl = afs_unlocked_xioctl,
656 #ifdef HAVE_COMPAT_IOCTL
657 .compat_ioctl = afs_unlocked_xioctl,
659 .mmap = afs_linux_mmap,
660 .open = afs_linux_open,
661 .flush = afs_linux_flush,
662 #ifdef AFS_LINUX26_ENV
663 .sendfile = generic_file_sendfile,
665 .release = afs_linux_release,
666 .fsync = afs_linux_fsync,
667 .lock = afs_linux_lock,
668 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
669 .flock = afs_linux_flock,
674 /**********************************************************************
675 * AFS Linux dentry operations
676 **********************************************************************/
678 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
679 * that has its mvid (parent dir's fid) pointer set to the wrong directory
680 * due to being mounted in multiple points at once. If so, check_bad_parent()
681 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
682 * dotdotfid and mtpoint fid members.
684 * dp - dentry to be checked.
688 * This dentry's vcache's mvid will be set to the correct parent directory's
690 * This root vnode's volume will have its dotdotfid and mtpoint fids set
691 * to the correct parent and mountpoint fids.
695 check_bad_parent(struct dentry *dp)
698 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
699 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
701 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
704 /* force a lookup, so vcp->mvid is fixed up */
705 afs_lookup(pvc, dp->d_name.name, &avc, credp);
706 if (!avc || vcp != avc) { /* bad, very bad.. */
707 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
708 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
709 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
710 ICL_TYPE_POINTER, dp);
713 AFS_RELE(AFSTOV(avc));
720 /* afs_linux_revalidate
721 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
724 afs_linux_revalidate(struct dentry *dp)
727 struct vcache *vcp = VTOAFS(dp->d_inode);
731 #ifdef AFS_LINUX24_ENV
737 /* Make this a fast path (no crref), since it's called so often. */
738 if (vcp->states & CStatd) {
740 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
741 check_bad_parent(dp); /* check and correct mvid */
744 #ifdef AFS_LINUX24_ENV
752 code = afs_getattr(vcp, &vattr, credp);
754 vattr2inode(AFSTOV(vcp), &vattr);
757 #ifdef AFS_LINUX24_ENV
765 #if defined(AFS_LINUX26_ENV)
767 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
769 int err = afs_linux_revalidate(dentry);
771 generic_fillattr(dentry->d_inode, stat);
777 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
778 * In kernels 2.2.10 and above, we are passed an additional flags var which
779 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
780 * we are advised to follow the entry if it is a link or to make sure that
781 * it is a directory. But since the kernel itself checks these possibilities
782 * later on, we shouldn't have to do it until later. Perhaps in the future..
785 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
786 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
787 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
789 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
792 afs_linux_dentry_revalidate(struct dentry *dp)
796 cred_t *credp = NULL;
797 struct vcache *vcp, *pvcp, *tvc = NULL;
800 #ifdef AFS_LINUX24_ENV
807 vcp = VTOAFS(dp->d_inode);
808 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
810 if (vcp == afs_globalVp)
813 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
814 check_bad_parent(dp); /* check and correct mvid */
817 /* If the last looker changes, we should make sure the current
818 * looker still has permission to examine this file. This would
819 * always require a crref() which would be "slow".
821 if (vcp->last_looker != treq.uid) {
822 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
825 vcp->last_looker = treq.uid;
829 /* If the parent's DataVersion has changed or the vnode
830 * is longer valid, we need to do a full lookup. VerifyVCache
831 * isn't enough since the vnode may have been renamed.
834 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
837 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
838 if (!tvc || tvc != vcp)
841 if (afs_getattr(vcp, &vattr, credp))
844 vattr2inode(AFSTOV(vcp), &vattr);
845 dp->d_time = hgetlo(pvcp->m.DataVersion);
848 /* should we always update the attributes at this point? */
849 /* unlikely--the vcache entry hasn't changed */
853 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
854 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
858 /* No change in parent's DataVersion so this negative
859 * lookup is still valid. BUT, if a server is down a
860 * negative lookup can result so there should be a
861 * liftime as well. For now, always expire.
879 shrink_dcache_parent(dp);
882 #ifdef AFS_LINUX24_ENV
893 afs_dentry_iput(struct dentry *dp, struct inode *ip)
895 struct vcache *vcp = VTOAFS(ip);
898 (void) afs_InactiveVCache(vcp, NULL);
905 afs_dentry_delete(struct dentry *dp)
907 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
908 return 1; /* bad inode? */
913 struct dentry_operations afs_dentry_operations = {
914 .d_revalidate = afs_linux_dentry_revalidate,
915 .d_delete = afs_dentry_delete,
916 .d_iput = afs_dentry_iput,
919 /**********************************************************************
920 * AFS Linux inode operations
921 **********************************************************************/
925 * Merely need to set enough of vattr to get us through the create. Note
926 * that the higher level code (open_namei) will take care of any tuncation
927 * explicitly. Exclusive open is also taken care of in open_namei.
929 * name is in kernel space at this point.
932 #ifdef IOP_CREATE_TAKES_NAMEIDATA
933 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
934 struct nameidata *nd)
936 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
940 cred_t *credp = crref();
941 const char *name = dp->d_name.name;
946 vattr.va_mode = mode;
947 vattr.va_type = mode & S_IFMT;
949 #if defined(AFS_LINUX26_ENV)
953 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
957 struct inode *ip = AFSTOV(vcp);
959 afs_getattr(vcp, &vattr, credp);
960 afs_fill_inode(ip, &vattr);
961 dp->d_op = &afs_dentry_operations;
962 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
963 d_instantiate(dp, ip);
967 #if defined(AFS_LINUX26_ENV)
974 /* afs_linux_lookup */
975 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
976 static struct dentry *
977 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
978 afs_linux_lookup(struct inode *dip, struct dentry *dp,
979 struct nameidata *nd)
981 afs_linux_lookup(struct inode *dip, struct dentry *dp)
985 afs_linux_lookup(struct inode *dip, struct dentry *dp)
988 cred_t *credp = crref();
989 struct vcache *vcp = NULL;
990 const char *comp = dp->d_name.name;
991 struct inode *ip = NULL;
992 #if defined(AFS_LINUX26_ENV)
993 struct dentry *newdp = NULL;
997 #if defined(AFS_LINUX26_ENV)
1001 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1007 afs_getattr(vcp, &vattr, credp);
1008 afs_fill_inode(ip, &vattr);
1010 dp->d_op = &afs_dentry_operations;
1011 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1014 #if defined(AFS_LINUX24_ENV)
1015 if (ip && S_ISDIR(ip->i_mode)) {
1016 struct dentry *alias;
1018 /* Try to invalidate an existing alias in favor of our new one */
1019 alias = d_find_alias(ip);
1020 #if defined(AFS_LINUX26_ENV)
1021 /* But not if it's disconnected; then we want d_splice_alias below */
1022 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1026 if (d_invalidate(alias) == 0) {
1030 #if defined(AFS_LINUX26_ENV)
1038 #if defined(AFS_LINUX26_ENV)
1039 newdp = d_splice_alias(ip, dp);
1044 #if defined(AFS_LINUX26_ENV)
1049 /* It's ok for the file to not be found. That's noted by the caller by
1050 * seeing that the dp->d_inode field is NULL.
1052 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1053 #if defined(AFS_LINUX26_ENV)
1054 if (!code || code == ENOENT)
1061 return ERR_PTR(-code);
1070 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1073 cred_t *credp = crref();
1074 const char *name = newdp->d_name.name;
1075 struct inode *oldip = olddp->d_inode;
1077 /* If afs_link returned the vnode, we could instantiate the
1078 * dentry. Since it's not, we drop this one and do a new lookup.
1083 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1091 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1094 cred_t *credp = crref();
1095 const char *name = dp->d_name.name;
1096 struct vcache *tvc = VTOAFS(dp->d_inode);
1098 #if defined(AFS_LINUX26_ENV)
1101 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1102 && !(tvc->states & CUnlinked)) {
1103 struct dentry *__dp;
1105 extern char *afs_newname();
1114 osi_FreeSmallSpace(__name);
1115 __name = afs_newname();
1118 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1122 } while (__dp->d_inode != NULL);
1125 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1127 tvc->mvid = (void *) __name;
1130 crfree(tvc->uncred);
1132 tvc->uncred = credp;
1133 tvc->states |= CUnlinked;
1138 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1147 code = afs_remove(VTOAFS(dip), name, credp);
1152 #if defined(AFS_LINUX26_ENV)
1161 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1164 cred_t *credp = crref();
1166 const char *name = dp->d_name.name;
1168 /* If afs_symlink returned the vnode, we could instantiate the
1169 * dentry. Since it's not, we drop this one and do a new lookup.
1175 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1182 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1185 cred_t *credp = crref();
1186 struct vcache *tvcp = NULL;
1188 const char *name = dp->d_name.name;
1190 #if defined(AFS_LINUX26_ENV)
1194 vattr.va_mask = ATTR_MODE;
1195 vattr.va_mode = mode;
1197 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1200 struct inode *ip = AFSTOV(tvcp);
1202 afs_getattr(tvcp, &vattr, credp);
1203 afs_fill_inode(ip, &vattr);
1205 dp->d_op = &afs_dentry_operations;
1206 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1207 d_instantiate(dp, ip);
1211 #if defined(AFS_LINUX26_ENV)
1219 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1222 cred_t *credp = crref();
1223 const char *name = dp->d_name.name;
1225 /* locking kernel conflicts with glock? */
1228 code = afs_rmdir(VTOAFS(dip), name, credp);
1231 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1232 * that failed because a directory is not empty. So, we map
1233 * EEXIST to ENOTEMPTY on linux.
1235 if (code == EEXIST) {
1249 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1250 struct inode *newip, struct dentry *newdp)
1253 cred_t *credp = crref();
1254 const char *oldname = olddp->d_name.name;
1255 const char *newname = newdp->d_name.name;
1256 struct dentry *rehash = NULL;
1258 #if defined(AFS_LINUX26_ENV)
1259 /* Prevent any new references during rename operation. */
1262 /* Remove old and new entries from name hash. New one will change below.
1263 * While it's optimal to catch failures and re-insert newdp into hash,
1264 * it's also error prone and in that case we're already dealing with error
1265 * cases. Let another lookup put things right, if need be.
1267 #if defined(AFS_LINUX26_ENV)
1268 if (!d_unhashed(newdp)) {
1273 if (!list_empty(&newdp->d_hash)) {
1279 #if defined(AFS_LINUX24_ENV)
1280 if (atomic_read(&olddp->d_count) > 1)
1281 shrink_dcache_parent(olddp);
1285 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1291 #if defined(AFS_LINUX26_ENV)
1300 /* afs_linux_ireadlink
1301 * Internal readlink which can return link contents to user or kernel space.
1302 * Note that the buffer is NOT supposed to be null-terminated.
1305 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1308 cred_t *credp = crref();
1312 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1313 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1317 return maxlen - tuio.uio_resid;
1322 #if !defined(AFS_LINUX24_ENV)
1323 /* afs_linux_readlink
1324 * Fill target (which is in user space) with contents of symlink.
1327 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1330 struct inode *ip = dp->d_inode;
1333 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1339 /* afs_linux_follow_link
1340 * a file system dependent link following routine.
1342 static struct dentry *
1343 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1344 unsigned int follow)
1352 name = osi_Alloc(PATH_MAX + 1);
1356 return ERR_PTR(-EIO);
1359 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1364 res = ERR_PTR(code);
1367 res = lookup_dentry(name, basep, follow);
1371 osi_Free(name, PATH_MAX + 1);
1377 /* afs_linux_readpage
1378 * all reads come through here. A strategy-like read call.
1381 afs_linux_readpage(struct file *fp, struct page *pp)
1384 cred_t *credp = crref();
1385 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1387 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1389 ulong address = afs_linux_page_address(pp);
1390 afs_offs_t offset = pageoff(pp);
1394 struct inode *ip = FILE_INODE(fp);
1395 int cnt = page_count(pp);
1396 struct vcache *avc = VTOAFS(ip);
1399 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1403 atomic_add(1, &pp->count);
1404 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1405 clear_bit(PG_error, &pp->flags);
1408 setup_uio(&tuio, &iovec, (char *)address, offset, PAGE_SIZE, UIO_READ,
1410 #ifdef AFS_LINUX24_ENV
1414 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 */
1415 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1416 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1417 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1420 #ifdef AFS_LINUX24_ENV
1425 if (tuio.uio_resid) /* zero remainder of page */
1426 memset((void *)(address + (PAGE_SIZE - tuio.uio_resid)), 0,
1428 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1429 flush_dcache_page(pp);
1430 SetPageUptodate(pp);
1432 set_bit(PG_uptodate, &pp->flags);
1436 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1440 clear_bit(PG_locked, &pp->flags);
1445 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1447 struct vrequest treq;
1450 code = afs_InitReq(&treq, credp);
1451 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1452 tdc = afs_FindDCache(avc, offset);
1454 if (!(tdc->mflags & DFNextStarted))
1455 afs_PrefetchChunk(avc, tdc, credp, &treq);
1458 ReleaseWriteLock(&avc->lock);
1468 #if defined(AFS_LINUX24_ENV)
1470 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1471 unsigned long offset, unsigned int count)
1473 struct vcache *vcp = VTOAFS(ip);
1482 buffer = kmap(pp) + offset;
1483 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1488 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1489 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1490 ICL_TYPE_INT32, 99999);
1492 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1494 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1496 ip->i_size = vcp->m.Length;
1497 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1500 struct vrequest treq;
1502 ObtainWriteLock(&vcp->lock, 533);
1503 if (!afs_InitReq(&treq, credp))
1504 code = afs_DoPartialWrite(vcp, &treq);
1505 ReleaseWriteLock(&vcp->lock);
1507 code = code ? -code : count - tuio.uio_resid;
1509 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1510 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1511 ICL_TYPE_INT32, code);
1523 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1524 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1526 afs_linux_writepage(struct page *pp)
1529 struct address_space *mapping = pp->mapping;
1530 struct inode *inode;
1531 unsigned long end_index;
1532 unsigned offset = PAGE_CACHE_SIZE;
1535 #if defined(AFS_LINUX26_ENV)
1536 if (PageReclaim(pp)) {
1537 # if defined(WRITEPAGE_ACTIVATE)
1538 return WRITEPAGE_ACTIVATE;
1540 return AOP_WRITEPAGE_ACTIVATE;
1544 if (PageLaunder(pp)) {
1545 return(fail_writepage(pp));
1549 inode = (struct inode *)mapping->host;
1550 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1553 if (pp->index < end_index)
1555 /* things got complicated... */
1556 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1557 /* OK, are we completely out? */
1558 if (pp->index >= end_index + 1 || !offset)
1561 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1562 SetPageUptodate(pp);
1564 if (status == offset)
1570 /* afs_linux_updatepage
1571 * What one would have thought was writepage - write dirty page to file.
1572 * Called from generic_file_write. buffer is still in user space. pagep
1573 * has been filled in with old data if we're updating less than a page.
1576 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1577 unsigned int count, int sync)
1579 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1580 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1586 set_bit(PG_locked, &pp->flags);
1590 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1591 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1592 ICL_TYPE_INT32, 99999);
1593 setup_uio(&tuio, &iovec, page_addr + offset,
1594 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1597 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1599 ip->i_size = vcp->m.Length;
1600 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1603 struct vrequest treq;
1605 ObtainWriteLock(&vcp->lock, 533);
1606 vcp->m.Date = osi_Time(); /* set modification time */
1607 if (!afs_InitReq(&treq, credp))
1608 code = afs_DoPartialWrite(vcp, &treq);
1609 ReleaseWriteLock(&vcp->lock);
1612 code = code ? -code : count - tuio.uio_resid;
1613 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1614 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1615 ICL_TYPE_INT32, code);
1620 clear_bit(PG_locked, &pp->flags);
1625 /* afs_linux_permission
1626 * Check access rights - returns error if can't check or permission denied.
1629 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1630 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1632 afs_linux_permission(struct inode *ip, int mode)
1636 cred_t *credp = crref();
1640 if (mode & MAY_EXEC)
1642 if (mode & MAY_READ)
1644 if (mode & MAY_WRITE)
1646 code = afs_access(VTOAFS(ip), tmp, credp);
1653 #if defined(AFS_LINUX24_ENV)
1655 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1660 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1661 offset, to - offset);
1662 #if !defined(AFS_LINUX26_ENV)
1670 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1673 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1674 call kmap directly instead of relying on us to do it */
1675 #if !defined(AFS_LINUX26_ENV)
1681 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1684 static struct inode_operations afs_file_iops = {
1685 #if defined(AFS_LINUX26_ENV)
1686 .permission = afs_linux_permission,
1687 .getattr = afs_linux_getattr,
1688 .setattr = afs_notify_change,
1689 #elif defined(AFS_LINUX24_ENV)
1690 .permission = afs_linux_permission,
1691 .revalidate = afs_linux_revalidate,
1692 .setattr = afs_notify_change,
1694 .default_file_ops = &afs_file_fops,
1695 .readpage = afs_linux_readpage,
1696 .revalidate = afs_linux_revalidate,
1697 .updatepage = afs_linux_updatepage,
1701 #if defined(AFS_LINUX24_ENV)
1702 static struct address_space_operations afs_file_aops = {
1703 .readpage = afs_linux_readpage,
1704 .writepage = afs_linux_writepage,
1705 .commit_write = afs_linux_commit_write,
1706 .prepare_write = afs_linux_prepare_write,
1711 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1712 * by what sort of operation is allowed.....
1715 static struct inode_operations afs_dir_iops = {
1716 #if !defined(AFS_LINUX24_ENV)
1717 .default_file_ops = &afs_dir_fops,
1719 .setattr = afs_notify_change,
1721 .create = afs_linux_create,
1722 .lookup = afs_linux_lookup,
1723 .link = afs_linux_link,
1724 .unlink = afs_linux_unlink,
1725 .symlink = afs_linux_symlink,
1726 .mkdir = afs_linux_mkdir,
1727 .rmdir = afs_linux_rmdir,
1728 .rename = afs_linux_rename,
1729 #if defined(AFS_LINUX26_ENV)
1730 .getattr = afs_linux_getattr,
1732 .revalidate = afs_linux_revalidate,
1734 .permission = afs_linux_permission,
1737 /* We really need a separate symlink set of ops, since do_follow_link()
1738 * determines if it _is_ a link by checking if the follow_link op is set.
1740 #if defined(AFS_LINUX24_ENV)
1742 afs_symlink_filler(struct file *file, struct page *page)
1744 struct inode *ip = (struct inode *)page->mapping->host;
1745 char *p = (char *)kmap(page);
1750 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1755 p[code] = '\0'; /* null terminate? */
1758 SetPageUptodate(page);
1772 static struct address_space_operations afs_symlink_aops = {
1773 .readpage = afs_symlink_filler
1777 static struct inode_operations afs_symlink_iops = {
1778 #if defined(AFS_LINUX24_ENV)
1779 .readlink = page_readlink,
1780 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1781 .follow_link = page_follow_link,
1783 .follow_link = page_follow_link_light,
1784 .put_link = page_put_link,
1786 .setattr = afs_notify_change,
1788 .readlink = afs_linux_readlink,
1789 .follow_link = afs_linux_follow_link,
1790 .permission = afs_linux_permission,
1791 .revalidate = afs_linux_revalidate,
1796 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1800 vattr2inode(ip, vattr);
1802 /* Reset ops if symlink or directory. */
1803 if (S_ISREG(ip->i_mode)) {
1804 ip->i_op = &afs_file_iops;
1805 #if defined(AFS_LINUX24_ENV)
1806 ip->i_fop = &afs_file_fops;
1807 ip->i_data.a_ops = &afs_file_aops;
1810 } else if (S_ISDIR(ip->i_mode)) {
1811 ip->i_op = &afs_dir_iops;
1812 #if defined(AFS_LINUX24_ENV)
1813 ip->i_fop = &afs_dir_fops;
1816 } else if (S_ISLNK(ip->i_mode)) {
1817 ip->i_op = &afs_symlink_iops;
1818 #if defined(AFS_LINUX24_ENV)
1819 ip->i_data.a_ops = &afs_symlink_aops;
1820 ip->i_mapping = &ip->i_data;
1824 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */