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 */
76 code = generic_file_read(fp, buf, count, offp);
80 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
81 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
90 /* Now we have integrated VM for writes as well as reads. generic_file_write
91 * also takes care of re-positioning the pointer if file is open in append
92 * mode. Call fake open/close to ensure we do writes of core dumps.
95 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
99 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
100 struct vrequest treq;
101 cred_t *credp = crref();
105 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
106 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
107 (fp->f_flags & O_APPEND) ? 99998 : 99999);
110 /* get a validated vcache entry */
111 code = (ssize_t) afs_InitReq(&treq, credp);
113 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
115 ObtainWriteLock(&vcp->lock, 529);
117 ReleaseWriteLock(&vcp->lock);
122 code = generic_file_write(fp, buf, count, offp);
126 ObtainWriteLock(&vcp->lock, 530);
127 afs_FakeClose(vcp, credp);
128 ReleaseWriteLock(&vcp->lock);
130 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
131 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
139 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
141 /* This is a complete rewrite of afs_readdir, since we can make use of
142 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
143 * handling and use of bulkstats will need to be reflected here as well.
146 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
148 extern struct DirEntry *afs_dir_GetBlob();
149 struct vcache *avc = VTOAFS(FILE_INODE(fp));
150 struct vrequest treq;
151 register struct dcache *tdc;
158 afs_size_t origOffset, tlen;
159 cred_t *credp = crref();
160 struct afs_fakestat_state fakestat;
162 #if defined(AFS_LINUX26_ENV)
166 AFS_STATCNT(afs_readdir);
168 code = afs_InitReq(&treq, credp);
173 afs_InitFakeStat(&fakestat);
174 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
178 /* update the cache entry */
180 code = afs_VerifyVCache(avc, &treq);
184 /* get a reference to the entire directory */
185 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
191 ObtainSharedLock(&avc->lock, 810);
192 UpgradeSToWLock(&avc->lock, 811);
193 ObtainReadLock(&tdc->lock);
195 * Make sure that the data in the cache is current. There are two
196 * cases we need to worry about:
197 * 1. The cache data is being fetched by another process.
198 * 2. The cache data is no longer valid
200 while ((avc->states & CStatd)
201 && (tdc->dflags & DFFetching)
202 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
203 ReleaseReadLock(&tdc->lock);
204 ReleaseSharedLock(&avc->lock);
205 afs_osi_Sleep(&tdc->validPos);
206 ObtainSharedLock(&avc->lock, 812);
207 ObtainReadLock(&tdc->lock);
209 if (!(avc->states & CStatd)
210 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
211 ReleaseReadLock(&tdc->lock);
212 ReleaseSharedLock(&avc->lock);
217 /* Set the readdir-in-progress flag, and downgrade the lock
218 * to shared so others will be able to acquire a read lock.
220 avc->states |= CReadDir;
221 avc->dcreaddir = tdc;
222 avc->readdir_pid = MyPidxx;
223 ConvertWToSLock(&avc->lock);
225 /* Fill in until we get an error or we're done. This implementation
226 * takes an offset in units of blobs, rather than bytes.
229 offset = (int) fp->f_pos;
231 dirpos = BlobScan(tdc, offset);
235 de = afs_dir_GetBlob(tdc, dirpos);
239 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
242 len = strlen(de->name);
244 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
245 (unsigned long)&tdc->f.inode, dirpos);
246 DRelease((struct buffer *) de, 0);
247 ReleaseSharedLock(&avc->lock);
253 /* filldir returns -EINVAL when the buffer is full. */
254 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
256 unsigned int type = DT_UNKNOWN;
257 struct VenusFid afid;
260 afid.Cell = avc->fid.Cell;
261 afid.Fid.Volume = avc->fid.Fid.Volume;
262 afid.Fid.Vnode = ntohl(de->fid.vnode);
263 afid.Fid.Unique = ntohl(de->fid.vunique);
264 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
266 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
269 } else if (((tvc->states) & (CStatd | CTruth))) {
270 /* CTruth will be set if the object has
275 else if (vtype == VREG)
277 /* Don't do this until we're sure it can't be a mtpt */
278 /* else if (vtype == VLNK)
280 /* what other types does AFS support? */
282 /* clean up from afs_FindVCache */
286 * If this is NFS readdirplus, then the filler is going to
287 * call getattr on this inode, which will deadlock if we're
291 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
295 code = (*filldir) (dirbuf, de->name, len, offset, ino);
297 DRelease((struct buffer *)de, 0);
300 offset = dirpos + 1 + ((len + 16) >> 5);
302 /* If filldir didn't fill in the last one this is still pointing to that
305 fp->f_pos = (loff_t) offset;
307 ReleaseReadLock(&tdc->lock);
309 UpgradeSToWLock(&avc->lock, 813);
310 avc->states &= ~CReadDir;
312 avc->readdir_pid = 0;
313 ReleaseSharedLock(&avc->lock);
317 afs_PutFakeStat(&fakestat);
320 #if defined(AFS_LINUX26_ENV)
327 /* in afs_pioctl.c */
328 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
331 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
332 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
334 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
341 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
343 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
344 cred_t *credp = crref();
345 struct vrequest treq;
349 #if defined(AFS_LINUX24_ENV)
350 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
351 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
352 vmap->vm_end - vmap->vm_start);
354 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
355 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
356 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
360 /* get a validated vcache entry */
361 code = afs_InitReq(&treq, credp);
365 code = afs_VerifyVCache(vcp, &treq);
369 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
372 code = generic_file_mmap(fp, vmap);
375 vcp->states |= CMAPPED;
388 afs_linux_open(struct inode *ip, struct file *fp)
390 struct vcache *vcp = VTOAFS(ip);
391 cred_t *credp = crref();
394 #ifdef AFS_LINUX24_ENV
398 code = afs_open(&vcp, fp->f_flags, credp);
400 #ifdef AFS_LINUX24_ENV
409 afs_linux_release(struct inode *ip, struct file *fp)
411 struct vcache *vcp = VTOAFS(ip);
412 cred_t *credp = crref();
415 #ifdef AFS_LINUX24_ENV
419 code = afs_close(vcp, fp->f_flags, credp);
421 #ifdef AFS_LINUX24_ENV
430 #if defined(AFS_LINUX24_ENV)
431 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
433 afs_linux_fsync(struct file *fp, struct dentry *dp)
437 struct inode *ip = FILE_INODE(fp);
438 cred_t *credp = crref();
440 #ifdef AFS_LINUX24_ENV
444 code = afs_fsync(VTOAFS(ip), credp);
446 #ifdef AFS_LINUX24_ENV
456 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
459 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
460 cred_t *credp = crref();
461 struct AFS_FLOCK flock;
462 /* Convert to a lock format afs_lockctl understands. */
463 memset((char *)&flock, 0, sizeof(flock));
464 flock.l_type = flp->fl_type;
465 flock.l_pid = flp->fl_pid;
467 flock.l_start = flp->fl_start;
468 flock.l_len = flp->fl_end - flp->fl_start;
470 /* Safe because there are no large files, yet */
471 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
472 if (cmd == F_GETLK64)
474 else if (cmd == F_SETLK64)
476 else if (cmd == F_SETLKW64)
478 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
481 code = afs_lockctl(vcp, &flock, cmd, credp);
484 #ifdef AFS_LINUX24_ENV
485 if (code == 0 && (cmd == F_SETLK || cmd == F_SETLKW)) {
486 struct file_lock flp2;
488 #ifdef AFS_LINUX26_ENV
489 flp2.fl_flags &=~ FL_SLEEP;
491 code = posix_lock_file(fp, &flp2);
492 osi_Assert(code != -EAGAIN); /* there should be no conflicts */
494 struct AFS_FLOCK flock2;
496 flock2.l_type = F_UNLCK;
498 afs_lockctl(vcp, &flock2, F_SETLK, credp);
503 /* Convert flock back to Linux's file_lock */
504 flp->fl_type = flock.l_type;
505 flp->fl_pid = flock.l_pid;
506 flp->fl_start = flock.l_start;
507 flp->fl_end = flock.l_start + flock.l_len;
515 * essentially the same as afs_fsync() but we need to get the return
516 * code for the sys_close() here, not afs_linux_release(), so call
517 * afs_StoreAllSegments() with AFS_LASTSTORE
520 afs_linux_flush(struct file *fp)
522 struct vrequest treq;
523 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
524 cred_t *credp = crref();
529 code = afs_InitReq(&treq, credp);
533 ObtainSharedLock(&vcp->lock, 535);
534 if (vcp->execsOrWriters > 0) {
535 UpgradeSToWLock(&vcp->lock, 536);
536 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
537 ConvertWToSLock(&vcp->lock);
539 code = afs_CheckCode(code, &treq, 54);
540 ReleaseSharedLock(&vcp->lock);
549 #if !defined(AFS_LINUX24_ENV)
550 /* Not allowed to directly read a directory. */
552 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
560 struct file_operations afs_dir_fops = {
561 #if !defined(AFS_LINUX24_ENV)
562 .read = afs_linux_dir_read,
563 .lock = afs_linux_lock,
564 .fsync = afs_linux_fsync,
566 .read = generic_read_dir,
568 .readdir = afs_linux_readdir,
569 #ifdef HAVE_UNLOCKED_IOCTL
570 .unlocked_ioctl = afs_unlocked_xioctl,
574 #ifdef HAVE_COMPAT_IOCTL
575 .compat_ioctl = afs_unlocked_xioctl,
577 .open = afs_linux_open,
578 .release = afs_linux_release,
581 struct file_operations afs_file_fops = {
582 .read = afs_linux_read,
583 .write = afs_linux_write,
584 #ifdef HAVE_UNLOCKED_IOCTL
585 .unlocked_ioctl = afs_unlocked_xioctl,
589 #ifdef HAVE_COMPAT_IOCTL
590 .compat_ioctl = afs_unlocked_xioctl,
592 .mmap = afs_linux_mmap,
593 .open = afs_linux_open,
594 .flush = afs_linux_flush,
595 #ifdef AFS_LINUX26_ENV
596 .sendfile = generic_file_sendfile,
598 .release = afs_linux_release,
599 .fsync = afs_linux_fsync,
600 .lock = afs_linux_lock,
604 /**********************************************************************
605 * AFS Linux dentry operations
606 **********************************************************************/
608 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
609 * that has its mvid (parent dir's fid) pointer set to the wrong directory
610 * due to being mounted in multiple points at once. If so, check_bad_parent()
611 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
612 * dotdotfid and mtpoint fid members.
614 * dp - dentry to be checked.
618 * This dentry's vcache's mvid will be set to the correct parent directory's
620 * This root vnode's volume will have its dotdotfid and mtpoint fids set
621 * to the correct parent and mountpoint fids.
625 check_bad_parent(struct dentry *dp)
628 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
629 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
631 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
634 /* force a lookup, so vcp->mvid is fixed up */
635 afs_lookup(pvc, dp->d_name.name, &avc, credp);
636 if (!avc || vcp != avc) { /* bad, very bad.. */
637 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
638 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
639 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
640 ICL_TYPE_POINTER, dp);
643 AFS_RELE(AFSTOV(avc));
650 /* afs_linux_revalidate
651 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
654 afs_linux_revalidate(struct dentry *dp)
657 struct vcache *vcp = VTOAFS(dp->d_inode);
661 #ifdef AFS_LINUX24_ENV
667 /* Make this a fast path (no crref), since it's called so often. */
668 if (vcp->states & CStatd) {
670 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
671 check_bad_parent(dp); /* check and correct mvid */
674 #ifdef AFS_LINUX24_ENV
682 code = afs_getattr(vcp, &vattr, credp);
684 vattr2inode(AFSTOV(vcp), &vattr);
687 #ifdef AFS_LINUX24_ENV
695 #if defined(AFS_LINUX26_ENV)
697 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
699 int err = afs_linux_revalidate(dentry);
701 generic_fillattr(dentry->d_inode, stat);
707 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
708 * In kernels 2.2.10 and above, we are passed an additional flags var which
709 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
710 * we are advised to follow the entry if it is a link or to make sure that
711 * it is a directory. But since the kernel itself checks these possibilities
712 * later on, we shouldn't have to do it until later. Perhaps in the future..
715 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
716 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
717 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
719 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
722 afs_linux_dentry_revalidate(struct dentry *dp)
726 cred_t *credp = NULL;
727 struct vcache *vcp, *pvcp, *tvc = NULL;
730 #ifdef AFS_LINUX24_ENV
737 vcp = VTOAFS(dp->d_inode);
738 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
740 if (vcp == afs_globalVp)
743 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
744 check_bad_parent(dp); /* check and correct mvid */
747 /* If the last looker changes, we should make sure the current
748 * looker still has permission to examine this file. This would
749 * always require a crref() which would be "slow".
751 if (vcp->last_looker != treq.uid) {
752 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
755 vcp->last_looker = treq.uid;
759 /* If the parent's DataVersion has changed or the vnode
760 * is longer valid, we need to do a full lookup. VerifyVCache
761 * isn't enough since the vnode may have been renamed.
764 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
767 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
768 if (!tvc || tvc != vcp)
771 if (afs_getattr(vcp, &vattr, credp))
774 vattr2inode(AFSTOV(vcp), &vattr);
775 dp->d_time = hgetlo(pvcp->m.DataVersion);
778 /* should we always update the attributes at this point? */
779 /* unlikely--the vcache entry hasn't changed */
783 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
784 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
788 /* No change in parent's DataVersion so this negative
789 * lookup is still valid. BUT, if a server is down a
790 * negative lookup can result so there should be a
791 * liftime as well. For now, always expire.
809 shrink_dcache_parent(dp);
812 #ifdef AFS_LINUX24_ENV
823 afs_dentry_iput(struct dentry *dp, struct inode *ip)
825 struct vcache *vcp = VTOAFS(ip);
828 if (vcp->states & CUnlinked)
829 (void) afs_InactiveVCache(vcp, NULL);
836 afs_dentry_delete(struct dentry *dp)
838 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
839 return 1; /* bad inode? */
844 struct dentry_operations afs_dentry_operations = {
845 .d_revalidate = afs_linux_dentry_revalidate,
846 .d_delete = afs_dentry_delete,
847 .d_iput = afs_dentry_iput,
850 /**********************************************************************
851 * AFS Linux inode operations
852 **********************************************************************/
856 * Merely need to set enough of vattr to get us through the create. Note
857 * that the higher level code (open_namei) will take care of any tuncation
858 * explicitly. Exclusive open is also taken care of in open_namei.
860 * name is in kernel space at this point.
863 #ifdef IOP_CREATE_TAKES_NAMEIDATA
864 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
865 struct nameidata *nd)
867 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
871 cred_t *credp = crref();
872 const char *name = dp->d_name.name;
877 vattr.va_mode = mode;
878 vattr.va_type = mode & S_IFMT;
880 #if defined(AFS_LINUX26_ENV)
884 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
888 struct inode *ip = AFSTOV(vcp);
890 afs_getattr(vcp, &vattr, credp);
891 afs_fill_inode(ip, &vattr);
892 dp->d_op = &afs_dentry_operations;
893 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
894 d_instantiate(dp, ip);
898 #if defined(AFS_LINUX26_ENV)
905 /* afs_linux_lookup */
906 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
907 static struct dentry *
908 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
909 afs_linux_lookup(struct inode *dip, struct dentry *dp,
910 struct nameidata *nd)
912 afs_linux_lookup(struct inode *dip, struct dentry *dp)
916 afs_linux_lookup(struct inode *dip, struct dentry *dp)
919 cred_t *credp = crref();
920 struct vcache *vcp = NULL;
921 const char *comp = dp->d_name.name;
922 struct inode *ip = NULL;
923 #if defined(AFS_LINUX26_ENV)
924 struct dentry *newdp = NULL;
928 #if defined(AFS_LINUX26_ENV)
932 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
938 afs_getattr(vcp, &vattr, credp);
939 afs_fill_inode(ip, &vattr);
941 dp->d_op = &afs_dentry_operations;
942 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
945 #if defined(AFS_LINUX24_ENV)
946 if (ip && S_ISDIR(ip->i_mode)) {
947 struct dentry *alias;
949 /* Try to invalidate an existing alias in favor of our new one */
950 alias = d_find_alias(ip);
951 #if defined(AFS_LINUX26_ENV)
952 /* But not if it's disconnected; then we want d_splice_alias below */
953 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
957 if (d_invalidate(alias) == 0) {
961 #if defined(AFS_LINUX26_ENV)
969 #if defined(AFS_LINUX26_ENV)
970 newdp = d_splice_alias(ip, dp);
975 #if defined(AFS_LINUX26_ENV)
980 /* It's ok for the file to not be found. That's noted by the caller by
981 * seeing that the dp->d_inode field is NULL.
983 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
984 #if defined(AFS_LINUX26_ENV)
985 if (!code || code == ENOENT)
992 return ERR_PTR(-code);
1001 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1004 cred_t *credp = crref();
1005 const char *name = newdp->d_name.name;
1006 struct inode *oldip = olddp->d_inode;
1008 /* If afs_link returned the vnode, we could instantiate the
1009 * dentry. Since it's not, we drop this one and do a new lookup.
1014 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1022 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1025 cred_t *credp = crref();
1026 const char *name = dp->d_name.name;
1027 struct vcache *tvc = VTOAFS(dp->d_inode);
1029 #if defined(AFS_LINUX26_ENV)
1032 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1033 && !(tvc->states & CUnlinked)) {
1034 struct dentry *__dp;
1036 extern char *afs_newname();
1045 osi_FreeSmallSpace(__name);
1046 __name = afs_newname();
1049 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1053 } while (__dp->d_inode != NULL);
1056 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1058 tvc->mvid = (void *) __name;
1061 crfree(tvc->uncred);
1063 tvc->uncred = credp;
1064 tvc->states |= CUnlinked;
1069 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1078 code = afs_remove(VTOAFS(dip), name, credp);
1083 #if defined(AFS_LINUX26_ENV)
1092 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1095 cred_t *credp = crref();
1097 const char *name = dp->d_name.name;
1099 /* If afs_symlink returned the vnode, we could instantiate the
1100 * dentry. Since it's not, we drop this one and do a new lookup.
1106 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1113 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1116 cred_t *credp = crref();
1117 struct vcache *tvcp = NULL;
1119 const char *name = dp->d_name.name;
1121 #if defined(AFS_LINUX26_ENV)
1125 vattr.va_mask = ATTR_MODE;
1126 vattr.va_mode = mode;
1128 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1131 struct inode *ip = AFSTOV(tvcp);
1133 afs_getattr(tvcp, &vattr, credp);
1134 afs_fill_inode(ip, &vattr);
1136 dp->d_op = &afs_dentry_operations;
1137 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1138 d_instantiate(dp, ip);
1142 #if defined(AFS_LINUX26_ENV)
1150 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1153 cred_t *credp = crref();
1154 const char *name = dp->d_name.name;
1156 /* locking kernel conflicts with glock? */
1159 code = afs_rmdir(VTOAFS(dip), name, credp);
1162 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1163 * that failed because a directory is not empty. So, we map
1164 * EEXIST to ENOTEMPTY on linux.
1166 if (code == EEXIST) {
1180 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1181 struct inode *newip, struct dentry *newdp)
1184 cred_t *credp = crref();
1185 const char *oldname = olddp->d_name.name;
1186 const char *newname = newdp->d_name.name;
1187 struct dentry *rehash = NULL;
1189 #if defined(AFS_LINUX26_ENV)
1190 /* Prevent any new references during rename operation. */
1193 /* Remove old and new entries from name hash. New one will change below.
1194 * While it's optimal to catch failures and re-insert newdp into hash,
1195 * it's also error prone and in that case we're already dealing with error
1196 * cases. Let another lookup put things right, if need be.
1198 #if defined(AFS_LINUX26_ENV)
1199 if (!d_unhashed(newdp)) {
1204 if (!list_empty(&newdp->d_hash)) {
1210 #if defined(AFS_LINUX24_ENV)
1211 if (atomic_read(&olddp->d_count) > 1)
1212 shrink_dcache_parent(olddp);
1216 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1222 #if defined(AFS_LINUX26_ENV)
1231 /* afs_linux_ireadlink
1232 * Internal readlink which can return link contents to user or kernel space.
1233 * Note that the buffer is NOT supposed to be null-terminated.
1236 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1239 cred_t *credp = crref();
1243 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1244 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1248 return maxlen - tuio.uio_resid;
1253 #if !defined(AFS_LINUX24_ENV)
1254 /* afs_linux_readlink
1255 * Fill target (which is in user space) with contents of symlink.
1258 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1261 struct inode *ip = dp->d_inode;
1264 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1270 /* afs_linux_follow_link
1271 * a file system dependent link following routine.
1273 static struct dentry *
1274 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1275 unsigned int follow)
1283 name = osi_Alloc(PATH_MAX + 1);
1287 return ERR_PTR(-EIO);
1290 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1295 res = ERR_PTR(code);
1298 res = lookup_dentry(name, basep, follow);
1302 osi_Free(name, PATH_MAX + 1);
1308 /* afs_linux_readpage
1309 * all reads come through here. A strategy-like read call.
1312 afs_linux_readpage(struct file *fp, struct page *pp)
1315 cred_t *credp = crref();
1316 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1318 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1320 ulong address = afs_linux_page_address(pp);
1321 afs_offs_t offset = pageoff(pp);
1325 struct inode *ip = FILE_INODE(fp);
1326 int cnt = page_count(pp);
1327 struct vcache *avc = VTOAFS(ip);
1330 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1334 atomic_add(1, &pp->count);
1335 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1336 clear_bit(PG_error, &pp->flags);
1339 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1341 #ifdef AFS_LINUX24_ENV
1345 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 */
1346 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1347 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1348 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1351 #ifdef AFS_LINUX24_ENV
1356 if (tuio.uio_resid) /* zero remainder of page */
1357 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1359 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1360 flush_dcache_page(pp);
1361 SetPageUptodate(pp);
1363 set_bit(PG_uptodate, &pp->flags);
1367 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1371 clear_bit(PG_locked, &pp->flags);
1376 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1378 struct vrequest treq;
1381 code = afs_InitReq(&treq, credp);
1382 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1383 tdc = afs_FindDCache(avc, offset);
1385 if (!(tdc->mflags & DFNextStarted))
1386 afs_PrefetchChunk(avc, tdc, credp, &treq);
1389 ReleaseWriteLock(&avc->lock);
1399 #if defined(AFS_LINUX24_ENV)
1401 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1402 unsigned long offset, unsigned int count)
1404 struct vcache *vcp = VTOAFS(ip);
1413 buffer = kmap(pp) + offset;
1414 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1419 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1420 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1421 ICL_TYPE_INT32, 99999);
1423 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1425 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1427 ip->i_size = vcp->m.Length;
1428 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1431 struct vrequest treq;
1433 ObtainWriteLock(&vcp->lock, 533);
1434 if (!afs_InitReq(&treq, credp))
1435 code = afs_DoPartialWrite(vcp, &treq);
1436 ReleaseWriteLock(&vcp->lock);
1438 code = code ? -code : count - tuio.uio_resid;
1440 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1441 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1442 ICL_TYPE_INT32, code);
1454 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1455 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1457 afs_linux_writepage(struct page *pp)
1460 struct address_space *mapping = pp->mapping;
1461 struct inode *inode;
1462 unsigned long end_index;
1463 unsigned offset = PAGE_CACHE_SIZE;
1466 #if defined(AFS_LINUX26_ENV)
1467 if (PageReclaim(pp)) {
1468 # if defined(WRITEPAGE_ACTIVATE)
1469 return WRITEPAGE_ACTIVATE;
1471 return AOP_WRITEPAGE_ACTIVATE;
1475 if (PageLaunder(pp)) {
1476 return(fail_writepage(pp));
1480 inode = (struct inode *)mapping->host;
1481 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1484 if (pp->index < end_index)
1486 /* things got complicated... */
1487 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1488 /* OK, are we completely out? */
1489 if (pp->index >= end_index + 1 || !offset)
1492 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1493 SetPageUptodate(pp);
1495 if (status == offset)
1501 /* afs_linux_updatepage
1502 * What one would have thought was writepage - write dirty page to file.
1503 * Called from generic_file_write. buffer is still in user space. pagep
1504 * has been filled in with old data if we're updating less than a page.
1507 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1508 unsigned int count, int sync)
1510 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1511 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1517 set_bit(PG_locked, &pp->flags);
1521 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1522 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1523 ICL_TYPE_INT32, 99999);
1524 setup_uio(&tuio, &iovec, page_addr + offset,
1525 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1528 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1530 ip->i_size = vcp->m.Length;
1531 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1534 struct vrequest treq;
1536 ObtainWriteLock(&vcp->lock, 533);
1537 vcp->m.Date = osi_Time(); /* set modification time */
1538 if (!afs_InitReq(&treq, credp))
1539 code = afs_DoPartialWrite(vcp, &treq);
1540 ReleaseWriteLock(&vcp->lock);
1543 code = code ? -code : count - tuio.uio_resid;
1544 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1545 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1546 ICL_TYPE_INT32, code);
1551 clear_bit(PG_locked, &pp->flags);
1556 /* afs_linux_permission
1557 * Check access rights - returns error if can't check or permission denied.
1560 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1561 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1563 afs_linux_permission(struct inode *ip, int mode)
1567 cred_t *credp = crref();
1571 if (mode & MAY_EXEC)
1573 if (mode & MAY_READ)
1575 if (mode & MAY_WRITE)
1577 code = afs_access(VTOAFS(ip), tmp, credp);
1584 #if defined(AFS_LINUX24_ENV)
1586 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1591 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1592 offset, to - offset);
1593 #if !defined(AFS_LINUX26_ENV)
1601 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1604 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1605 call kmap directly instead of relying on us to do it */
1606 #if !defined(AFS_LINUX26_ENV)
1612 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1615 static struct inode_operations afs_file_iops = {
1616 #if defined(AFS_LINUX26_ENV)
1617 .permission = afs_linux_permission,
1618 .getattr = afs_linux_getattr,
1619 .setattr = afs_notify_change,
1620 #elif defined(AFS_LINUX24_ENV)
1621 .permission = afs_linux_permission,
1622 .revalidate = afs_linux_revalidate,
1623 .setattr = afs_notify_change,
1625 .default_file_ops = &afs_file_fops,
1626 .readpage = afs_linux_readpage,
1627 .revalidate = afs_linux_revalidate,
1628 .updatepage = afs_linux_updatepage,
1632 #if defined(AFS_LINUX24_ENV)
1633 static struct address_space_operations afs_file_aops = {
1634 .readpage = afs_linux_readpage,
1635 .writepage = afs_linux_writepage,
1636 .commit_write = afs_linux_commit_write,
1637 .prepare_write = afs_linux_prepare_write,
1642 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1643 * by what sort of operation is allowed.....
1646 static struct inode_operations afs_dir_iops = {
1647 #if !defined(AFS_LINUX24_ENV)
1648 .default_file_ops = &afs_dir_fops,
1650 .setattr = afs_notify_change,
1652 .create = afs_linux_create,
1653 .lookup = afs_linux_lookup,
1654 .link = afs_linux_link,
1655 .unlink = afs_linux_unlink,
1656 .symlink = afs_linux_symlink,
1657 .mkdir = afs_linux_mkdir,
1658 .rmdir = afs_linux_rmdir,
1659 .rename = afs_linux_rename,
1660 #if defined(AFS_LINUX26_ENV)
1661 .getattr = afs_linux_getattr,
1663 .revalidate = afs_linux_revalidate,
1665 .permission = afs_linux_permission,
1668 /* We really need a separate symlink set of ops, since do_follow_link()
1669 * determines if it _is_ a link by checking if the follow_link op is set.
1671 #if defined(AFS_LINUX24_ENV)
1673 afs_symlink_filler(struct file *file, struct page *page)
1675 struct inode *ip = (struct inode *)page->mapping->host;
1676 char *p = (char *)kmap(page);
1681 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1686 p[code] = '\0'; /* null terminate? */
1689 SetPageUptodate(page);
1703 static struct address_space_operations afs_symlink_aops = {
1704 .readpage = afs_symlink_filler
1708 static struct inode_operations afs_symlink_iops = {
1709 #if defined(AFS_LINUX24_ENV)
1710 .readlink = page_readlink,
1711 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1712 .follow_link = page_follow_link,
1714 .follow_link = page_follow_link_light,
1715 .put_link = page_put_link,
1717 .setattr = afs_notify_change,
1719 .readlink = afs_linux_readlink,
1720 .follow_link = afs_linux_follow_link,
1721 .permission = afs_linux_permission,
1722 .revalidate = afs_linux_revalidate,
1727 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1731 vattr2inode(ip, vattr);
1733 /* Reset ops if symlink or directory. */
1734 if (S_ISREG(ip->i_mode)) {
1735 ip->i_op = &afs_file_iops;
1736 #if defined(AFS_LINUX24_ENV)
1737 ip->i_fop = &afs_file_fops;
1738 ip->i_data.a_ops = &afs_file_aops;
1741 } else if (S_ISDIR(ip->i_mode)) {
1742 ip->i_op = &afs_dir_iops;
1743 #if defined(AFS_LINUX24_ENV)
1744 ip->i_fop = &afs_dir_fops;
1747 } else if (S_ISLNK(ip->i_mode)) {
1748 ip->i_op = &afs_symlink_iops;
1749 #if defined(AFS_LINUX24_ENV)
1750 ip->i_data.a_ops = &afs_symlink_aops;
1751 ip->i_mapping = &ip->i_data;
1755 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */