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;
56 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
59 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
60 cred_t *credp = crref();
64 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
65 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
68 /* get a validated vcache entry */
69 code = afs_InitReq(&treq, credp);
71 code = afs_VerifyVCache(vcp, &treq);
76 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
78 code = generic_file_read(fp, buf, count, offp);
82 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
83 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
92 /* Now we have integrated VM for writes as well as reads. generic_file_write
93 * also takes care of re-positioning the pointer if file is open in append
94 * mode. Call fake open/close to ensure we do writes of core dumps.
97 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
101 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
102 struct vrequest treq;
103 cred_t *credp = crref();
107 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
108 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
109 (fp->f_flags & O_APPEND) ? 99998 : 99999);
112 /* get a validated vcache entry */
113 code = (ssize_t) afs_InitReq(&treq, credp);
115 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
117 ObtainWriteLock(&vcp->lock, 529);
119 ReleaseWriteLock(&vcp->lock);
124 code = generic_file_write(fp, buf, count, offp);
128 ObtainWriteLock(&vcp->lock, 530);
129 vcp->m.Date = osi_Time(); /* set modification time */
130 afs_FakeClose(vcp, credp);
132 code2 = afs_DoPartialWrite(vcp, &treq);
133 if (code2 && code >= 0)
134 code = (ssize_t) - code2;
135 ReleaseWriteLock(&vcp->lock);
137 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
138 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
146 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
148 /* This is a complete rewrite of afs_readdir, since we can make use of
149 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
150 * handling and use of bulkstats will need to be reflected here as well.
153 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
155 extern struct DirEntry *afs_dir_GetBlob();
156 struct vcache *avc = VTOAFS(FILE_INODE(fp));
157 struct vrequest treq;
158 register struct dcache *tdc;
165 afs_size_t origOffset, tlen;
166 cred_t *credp = crref();
167 struct afs_fakestat_state fakestat;
169 #if defined(AFS_LINUX26_ENV)
173 AFS_STATCNT(afs_readdir);
175 code = afs_InitReq(&treq, credp);
180 afs_InitFakeStat(&fakestat);
181 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
185 /* update the cache entry */
187 code = afs_VerifyVCache(avc, &treq);
191 /* get a reference to the entire directory */
192 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
198 ObtainReadLock(&avc->lock);
199 ObtainReadLock(&tdc->lock);
201 * Make sure that the data in the cache is current. There are two
202 * cases we need to worry about:
203 * 1. The cache data is being fetched by another process.
204 * 2. The cache data is no longer valid
206 while ((avc->states & CStatd)
207 && (tdc->dflags & DFFetching)
208 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
209 ReleaseReadLock(&tdc->lock);
210 ReleaseReadLock(&avc->lock);
211 afs_osi_Sleep(&tdc->validPos);
212 ObtainReadLock(&avc->lock);
213 ObtainReadLock(&tdc->lock);
215 if (!(avc->states & CStatd)
216 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
217 ReleaseReadLock(&tdc->lock);
218 ReleaseReadLock(&avc->lock);
223 /* Fill in until we get an error or we're done. This implementation
224 * takes an offset in units of blobs, rather than bytes.
227 offset = (int) fp->f_pos;
229 dirpos = BlobScan(tdc, offset);
233 de = afs_dir_GetBlob(tdc, dirpos);
237 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
238 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
240 len = strlen(de->name);
242 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
243 (unsigned long)&tdc->f.inode, dirpos);
244 DRelease((struct buffer *) de, 0);
246 ReleaseReadLock(&avc->lock);
251 /* filldir returns -EINVAL when the buffer is full. */
252 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
254 unsigned int type = DT_UNKNOWN;
255 struct VenusFid afid;
258 afid.Cell = avc->fid.Cell;
259 afid.Fid.Volume = avc->fid.Fid.Volume;
260 afid.Fid.Vnode = ntohl(de->fid.vnode);
261 afid.Fid.Unique = ntohl(de->fid.vunique);
262 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
264 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
267 } else if (((tvc->states) & (CStatd | CTruth))) {
268 /* CTruth will be set if the object has
273 else if (vtype == VREG)
275 /* Don't do this until we're sure it can't be a mtpt */
276 /* else if (vtype == VLNK)
278 /* what other types does AFS support? */
280 /* clean up from afs_FindVCache */
283 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
286 code = (*filldir) (dirbuf, de->name, len, offset, ino);
288 DRelease((struct buffer *)de, 0);
291 offset = dirpos + 1 + ((len + 16) >> 5);
293 /* If filldir didn't fill in the last one this is still pointing to that
296 fp->f_pos = (loff_t) offset;
298 ReleaseReadLock(&tdc->lock);
300 ReleaseReadLock(&avc->lock);
304 afs_PutFakeStat(&fakestat);
307 #if defined(AFS_LINUX26_ENV)
314 /* in afs_pioctl.c */
315 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
318 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
319 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
321 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
328 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
330 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
331 cred_t *credp = crref();
332 struct vrequest treq;
336 #if defined(AFS_LINUX24_ENV)
337 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
338 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
339 vmap->vm_end - vmap->vm_start);
341 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
342 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
343 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
347 /* get a validated vcache entry */
348 code = afs_InitReq(&treq, credp);
352 code = afs_VerifyVCache(vcp, &treq);
356 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
359 code = generic_file_mmap(fp, vmap);
362 vcp->states |= CMAPPED;
375 afs_linux_open(struct inode *ip, struct file *fp)
377 struct vcache *vcp = VTOAFS(ip);
378 cred_t *credp = crref();
381 #ifdef AFS_LINUX24_ENV
385 code = afs_open(&vcp, fp->f_flags, credp);
387 #ifdef AFS_LINUX24_ENV
396 afs_linux_release(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_close(vcp, fp->f_flags, credp);
408 #ifdef AFS_LINUX24_ENV
417 #if defined(AFS_LINUX24_ENV)
418 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
420 afs_linux_fsync(struct file *fp, struct dentry *dp)
424 struct inode *ip = FILE_INODE(fp);
425 cred_t *credp = crref();
427 #ifdef AFS_LINUX24_ENV
431 code = afs_fsync(VTOAFS(ip), credp);
433 #ifdef AFS_LINUX24_ENV
443 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
446 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
447 cred_t *credp = crref();
448 struct AFS_FLOCK flock;
449 /* Convert to a lock format afs_lockctl understands. */
450 memset((char *)&flock, 0, sizeof(flock));
451 flock.l_type = flp->fl_type;
452 flock.l_pid = flp->fl_pid;
454 flock.l_start = flp->fl_start;
455 flock.l_len = flp->fl_end - flp->fl_start;
457 /* Safe because there are no large files, yet */
458 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
459 if (cmd == F_GETLK64)
461 else if (cmd == F_SETLK64)
463 else if (cmd == F_SETLKW64)
465 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
468 code = afs_lockctl(vcp, &flock, cmd, credp);
471 /* Convert flock back to Linux's file_lock */
472 flp->fl_type = flock.l_type;
473 flp->fl_pid = flock.l_pid;
474 flp->fl_start = flock.l_start;
475 flp->fl_end = flock.l_start + flock.l_len;
483 * essentially the same as afs_fsync() but we need to get the return
484 * code for the sys_close() here, not afs_linux_release(), so call
485 * afs_StoreAllSegments() with AFS_LASTSTORE
488 afs_linux_flush(struct file *fp)
490 struct vrequest treq;
491 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
492 cred_t *credp = crref();
497 code = afs_InitReq(&treq, credp);
501 ObtainSharedLock(&vcp->lock, 535);
502 if (vcp->execsOrWriters > 0) {
503 UpgradeSToWLock(&vcp->lock, 536);
504 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
505 ConvertWToSLock(&vcp->lock);
507 code = afs_CheckCode(code, &treq, 54);
508 ReleaseSharedLock(&vcp->lock);
517 #if !defined(AFS_LINUX24_ENV)
518 /* Not allowed to directly read a directory. */
520 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
528 struct file_operations afs_dir_fops = {
529 #if !defined(AFS_LINUX24_ENV)
530 .read = afs_linux_dir_read,
531 .lock = afs_linux_lock,
532 .fsync = afs_linux_fsync,
534 .read = generic_read_dir,
536 .readdir = afs_linux_readdir,
537 #ifdef HAVE_UNLOCKED_IOCTL
538 .unlocked_ioctl = afs_unlocked_xioctl,
542 #ifdef HAVE_COMPAT_IOCTL
543 .compat_ioctl = afs_unlocked_xioctl,
545 .open = afs_linux_open,
546 .release = afs_linux_release,
549 struct file_operations afs_file_fops = {
550 .read = afs_linux_read,
551 .write = afs_linux_write,
552 #ifdef HAVE_UNLOCKED_IOCTL
553 .unlocked_ioctl = afs_unlocked_xioctl,
557 #ifdef HAVE_COMPAT_IOCTL
558 .compat_ioctl = afs_unlocked_xioctl,
560 .mmap = afs_linux_mmap,
561 .open = afs_linux_open,
562 .flush = afs_linux_flush,
563 #ifdef AFS_LINUX26_ENV
564 .sendfile = generic_file_sendfile,
566 .release = afs_linux_release,
567 .fsync = afs_linux_fsync,
568 .lock = afs_linux_lock,
572 /**********************************************************************
573 * AFS Linux dentry operations
574 **********************************************************************/
576 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
577 * that has its mvid (parent dir's fid) pointer set to the wrong directory
578 * due to being mounted in multiple points at once. If so, check_bad_parent()
579 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
580 * dotdotfid and mtpoint fid members.
582 * dp - dentry to be checked.
586 * This dentry's vcache's mvid will be set to the correct parent directory's
588 * This root vnode's volume will have its dotdotfid and mtpoint fids set
589 * to the correct parent and mountpoint fids.
593 check_bad_parent(struct dentry *dp)
596 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
597 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
599 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
602 /* force a lookup, so vcp->mvid is fixed up */
603 afs_lookup(pvc, dp->d_name.name, &avc, credp);
604 if (!avc || vcp != avc) { /* bad, very bad.. */
605 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
606 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
607 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
608 ICL_TYPE_POINTER, dp);
611 AFS_RELE(AFSTOV(avc));
618 /* afs_linux_revalidate
619 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
622 afs_linux_revalidate(struct dentry *dp)
625 struct vcache *vcp = VTOAFS(dp->d_inode);
629 #ifdef AFS_LINUX24_ENV
634 /* Make this a fast path (no crref), since it's called so often. */
635 if (vcp->states & CStatd) {
637 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
638 check_bad_parent(dp); /* check and correct mvid */
641 #ifdef AFS_LINUX24_ENV
648 code = afs_getattr(vcp, &vattr, credp);
650 vattr2inode(AFSTOV(vcp), &vattr);
653 #ifdef AFS_LINUX24_ENV
661 #if defined(AFS_LINUX26_ENV)
663 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
665 int err = afs_linux_revalidate(dentry);
667 generic_fillattr(dentry->d_inode, stat);
673 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
674 * In kernels 2.2.10 and above, we are passed an additional flags var which
675 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
676 * we are advised to follow the entry if it is a link or to make sure that
677 * it is a directory. But since the kernel itself checks these possibilities
678 * later on, we shouldn't have to do it until later. Perhaps in the future..
681 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
682 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
683 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
685 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
688 afs_linux_dentry_revalidate(struct dentry *dp)
692 cred_t *credp = NULL;
693 struct vcache *vcp, *pvcp, *tvc = NULL;
696 #ifdef AFS_LINUX24_ENV
703 vcp = VTOAFS(dp->d_inode);
704 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
706 if (vcp == afs_globalVp)
709 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
710 check_bad_parent(dp); /* check and correct mvid */
713 /* If the last looker changes, we should make sure the current
714 * looker still has permission to examine this file. This would
715 * always require a crref() which would be "slow".
717 if (vcp->last_looker != treq.uid) {
718 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
721 vcp->last_looker = treq.uid;
725 /* If the parent's DataVersion has changed or the vnode
726 * is longer valid, we need to do a full lookup. VerifyVCache
727 * isn't enough since the vnode may have been renamed.
730 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
733 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
734 if (!tvc || tvc != vcp)
737 if (afs_getattr(vcp, &vattr, credp))
740 vattr2inode(AFSTOV(vcp), &vattr);
741 dp->d_time = hgetlo(pvcp->m.DataVersion);
744 /* should we always update the attributes at this point? */
745 /* unlikely--the vcache entry hasn't changed */
749 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
750 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
754 /* No change in parent's DataVersion so this negative
755 * lookup is still valid. BUT, if a server is down a
756 * negative lookup can result so there should be a
757 * liftime as well. For now, always expire.
775 shrink_dcache_parent(dp);
778 #ifdef AFS_LINUX24_ENV
789 afs_dentry_iput(struct dentry *dp, struct inode *ip)
791 struct vcache *vcp = VTOAFS(ip);
794 if (vcp->states & CUnlinked)
795 (void) afs_remunlink(vcp, 1); /* perhaps afs_InactiveVCache() instead */
802 afs_dentry_delete(struct dentry *dp)
804 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
805 return 1; /* bad inode? */
810 struct dentry_operations afs_dentry_operations = {
811 .d_revalidate = afs_linux_dentry_revalidate,
812 .d_delete = afs_dentry_delete,
813 .d_iput = afs_dentry_iput,
816 /**********************************************************************
817 * AFS Linux inode operations
818 **********************************************************************/
822 * Merely need to set enough of vattr to get us through the create. Note
823 * that the higher level code (open_namei) will take care of any tuncation
824 * explicitly. Exclusive open is also taken care of in open_namei.
826 * name is in kernel space at this point.
829 #ifdef IOP_CREATE_TAKES_NAMEIDATA
830 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
831 struct nameidata *nd)
833 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
837 cred_t *credp = crref();
838 const char *name = dp->d_name.name;
843 vattr.va_mode = mode;
844 vattr.va_type = mode & S_IFMT;
846 #if defined(AFS_LINUX26_ENV)
850 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
854 struct inode *ip = AFSTOV(vcp);
856 afs_getattr(vcp, &vattr, credp);
857 afs_fill_inode(ip, &vattr);
858 dp->d_op = &afs_dentry_operations;
859 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
860 d_instantiate(dp, ip);
864 #if defined(AFS_LINUX26_ENV)
871 /* afs_linux_lookup */
872 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
873 static struct dentry *
874 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
875 afs_linux_lookup(struct inode *dip, struct dentry *dp,
876 struct nameidata *nd)
878 afs_linux_lookup(struct inode *dip, struct dentry *dp)
882 afs_linux_lookup(struct inode *dip, struct dentry *dp)
886 cred_t *credp = crref();
887 struct vcache *vcp = NULL;
888 const char *comp = dp->d_name.name;
889 struct dentry *res = NULL;
890 struct inode *ip = NULL;
893 #if defined(AFS_LINUX26_ENV)
897 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
902 afs_getattr(vcp, &vattr, credp);
903 afs_fill_inode(ip, &vattr);
905 dp->d_op = &afs_dentry_operations;
906 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
908 #if defined(AFS_LINUX24_ENV)
909 if (ip && S_ISDIR(ip->i_mode)) {
911 res = d_find_alias(ip);
920 #if defined(AFS_LINUX26_ENV)
925 /* It's ok for the file to not be found. That's noted by the caller by
926 * seeing that the dp->d_inode field is NULL.
928 #if defined(AFS_LINUX24_ENV)
932 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
936 return ERR_PTR(-code);
945 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
948 cred_t *credp = crref();
949 const char *name = newdp->d_name.name;
950 struct inode *oldip = olddp->d_inode;
952 /* If afs_link returned the vnode, we could instantiate the
953 * dentry. Since it's not, we drop this one and do a new lookup.
958 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
966 afs_linux_unlink(struct inode *dip, struct dentry *dp)
969 cred_t *credp = crref();
970 const char *name = dp->d_name.name;
971 struct vcache *tvc = VTOAFS(dp->d_inode);
973 #if defined(AFS_LINUX26_ENV)
976 if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
977 && !(tvc->states & CUnlinked)) {
980 extern char *afs_newname();
989 osi_FreeSmallSpace(__name);
990 __name = afs_newname();
993 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
997 } while (__dp->d_inode != NULL);
1000 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1002 tvc->mvid = (void *) __name;
1005 crfree(tvc->uncred);
1007 tvc->uncred = credp;
1008 tvc->states |= CUnlinked;
1013 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1022 code = afs_remove(VTOAFS(dip), name, credp);
1027 #if defined(AFS_LINUX26_ENV)
1036 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1039 cred_t *credp = crref();
1041 const char *name = dp->d_name.name;
1043 /* If afs_symlink returned the vnode, we could instantiate the
1044 * dentry. Since it's not, we drop this one and do a new lookup.
1050 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1057 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1060 cred_t *credp = crref();
1061 struct vcache *tvcp = NULL;
1063 const char *name = dp->d_name.name;
1065 #if defined(AFS_LINUX26_ENV)
1069 vattr.va_mask = ATTR_MODE;
1070 vattr.va_mode = mode;
1072 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1075 struct inode *ip = AFSTOV(tvcp);
1077 afs_getattr(tvcp, &vattr, credp);
1078 afs_fill_inode(ip, &vattr);
1080 dp->d_op = &afs_dentry_operations;
1081 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1082 d_instantiate(dp, ip);
1086 #if defined(AFS_LINUX26_ENV)
1094 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1097 cred_t *credp = crref();
1098 const char *name = dp->d_name.name;
1100 #if defined(AFS_LINUX26_ENV)
1104 code = afs_rmdir(VTOAFS(dip), name, credp);
1107 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1108 * that failed because a directory is not empty. So, we map
1109 * EEXIST to ENOTEMPTY on linux.
1111 if (code == EEXIST) {
1119 #if defined(AFS_LINUX26_ENV)
1128 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1129 struct inode *newip, struct dentry *newdp)
1132 cred_t *credp = crref();
1133 const char *oldname = olddp->d_name.name;
1134 const char *newname = newdp->d_name.name;
1135 struct dentry *rehash = NULL;
1137 #if defined(AFS_LINUX26_ENV)
1138 /* Prevent any new references during rename operation. */
1141 /* Remove old and new entries from name hash. New one will change below.
1142 * While it's optimal to catch failures and re-insert newdp into hash,
1143 * it's also error prone and in that case we're already dealing with error
1144 * cases. Let another lookup put things right, if need be.
1146 #if defined(AFS_LINUX26_ENV)
1147 if (!d_unhashed(newdp)) {
1152 if (!list_empty(&newdp->d_hash)) {
1158 #if defined(AFS_LINUX24_ENV)
1159 if (atomic_read(&olddp->d_count) > 1)
1160 shrink_dcache_parent(olddp);
1164 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1170 #if defined(AFS_LINUX26_ENV)
1179 /* afs_linux_ireadlink
1180 * Internal readlink which can return link contents to user or kernel space.
1181 * Note that the buffer is NOT supposed to be null-terminated.
1184 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1187 cred_t *credp = crref();
1191 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1192 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1196 return maxlen - tuio.uio_resid;
1201 #if !defined(AFS_LINUX24_ENV)
1202 /* afs_linux_readlink
1203 * Fill target (which is in user space) with contents of symlink.
1206 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1209 struct inode *ip = dp->d_inode;
1212 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1218 /* afs_linux_follow_link
1219 * a file system dependent link following routine.
1221 static struct dentry *
1222 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1223 unsigned int follow)
1231 name = osi_Alloc(PATH_MAX + 1);
1235 return ERR_PTR(-EIO);
1238 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1243 res = ERR_PTR(code);
1246 res = lookup_dentry(name, basep, follow);
1250 osi_Free(name, PATH_MAX + 1);
1256 /* afs_linux_readpage
1257 * all reads come through here. A strategy-like read call.
1260 afs_linux_readpage(struct file *fp, struct page *pp)
1263 cred_t *credp = crref();
1264 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1266 afs_offs_t offset = page_offset(pp);
1268 ulong address = afs_linux_page_address(pp);
1269 afs_offs_t offset = pageoff(pp);
1273 struct inode *ip = FILE_INODE(fp);
1274 int cnt = page_count(pp);
1275 struct vcache *avc = VTOAFS(ip);
1278 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1282 atomic_add(1, &pp->count);
1283 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1284 clear_bit(PG_error, &pp->flags);
1287 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1289 #ifdef AFS_LINUX24_ENV
1293 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 */
1294 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1295 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1296 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1299 #ifdef AFS_LINUX24_ENV
1304 if (tuio.uio_resid) /* zero remainder of page */
1305 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1307 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1308 flush_dcache_page(pp);
1309 SetPageUptodate(pp);
1311 set_bit(PG_uptodate, &pp->flags);
1315 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1319 clear_bit(PG_locked, &pp->flags);
1324 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1326 struct vrequest treq;
1329 code = afs_InitReq(&treq, credp);
1330 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1331 tdc = afs_FindDCache(avc, offset);
1333 if (!(tdc->mflags & DFNextStarted))
1334 afs_PrefetchChunk(avc, tdc, credp, &treq);
1337 ReleaseWriteLock(&avc->lock);
1347 #if defined(AFS_LINUX24_ENV)
1349 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1350 unsigned long offset, unsigned int count)
1352 struct vcache *vcp = VTOAFS(ip);
1361 buffer = kmap(pp) + offset;
1362 base = page_offset(pp) + offset;
1367 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1368 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1369 ICL_TYPE_INT32, 99999);
1371 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1373 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1375 ip->i_size = vcp->m.Length;
1376 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1379 && afs_stats_cmperf.cacheCurrDirtyChunks >
1380 afs_stats_cmperf.cacheMaxDirtyChunks) {
1381 struct vrequest treq;
1383 ObtainWriteLock(&vcp->lock, 533);
1384 if (!afs_InitReq(&treq, credp))
1385 code = afs_DoPartialWrite(vcp, &treq);
1386 ReleaseWriteLock(&vcp->lock);
1388 code = code ? -code : count - tuio.uio_resid;
1390 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1391 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1392 ICL_TYPE_INT32, code);
1404 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1405 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1407 afs_linux_writepage(struct page *pp)
1410 struct address_space *mapping = pp->mapping;
1411 struct inode *inode;
1412 unsigned long end_index;
1413 unsigned offset = PAGE_CACHE_SIZE;
1416 #if defined(AFS_LINUX26_ENV)
1417 if (PageReclaim(pp)) {
1418 return WRITEPAGE_ACTIVATE;
1421 if (PageLaunder(pp)) {
1422 return(fail_writepage(pp));
1426 inode = (struct inode *)mapping->host;
1427 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1430 if (pp->index < end_index)
1432 /* things got complicated... */
1433 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1434 /* OK, are we completely out? */
1435 if (pp->index >= end_index + 1 || !offset)
1438 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1439 SetPageUptodate(pp);
1441 if (status == offset)
1447 /* afs_linux_updatepage
1448 * What one would have thought was writepage - write dirty page to file.
1449 * Called from generic_file_write. buffer is still in user space. pagep
1450 * has been filled in with old data if we're updating less than a page.
1453 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1454 unsigned int count, int sync)
1456 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1457 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1463 set_bit(PG_locked, &pp->flags);
1467 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1468 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1469 ICL_TYPE_INT32, 99999);
1470 setup_uio(&tuio, &iovec, page_addr + offset,
1471 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1474 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1476 ip->i_size = vcp->m.Length;
1477 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1479 code = code ? -code : count - tuio.uio_resid;
1480 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1481 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1482 ICL_TYPE_INT32, code);
1487 clear_bit(PG_locked, &pp->flags);
1492 /* afs_linux_permission
1493 * Check access rights - returns error if can't check or permission denied.
1496 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1497 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1499 afs_linux_permission(struct inode *ip, int mode)
1503 cred_t *credp = crref();
1507 if (mode & MAY_EXEC)
1509 if (mode & MAY_READ)
1511 if (mode & MAY_WRITE)
1513 code = afs_access(VTOAFS(ip), tmp, credp);
1520 #if defined(AFS_LINUX24_ENV)
1522 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1527 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1528 offset, to - offset);
1529 #if !defined(AFS_LINUX26_ENV)
1537 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1540 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1541 call kmap directly instead of relying on us to do it */
1542 #if !defined(AFS_LINUX26_ENV)
1548 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1551 static struct inode_operations afs_file_iops = {
1552 #if defined(AFS_LINUX26_ENV)
1553 .permission = afs_linux_permission,
1554 .getattr = afs_linux_getattr,
1555 .setattr = afs_notify_change,
1556 #elif defined(AFS_LINUX24_ENV)
1557 .permission = afs_linux_permission,
1558 .revalidate = afs_linux_revalidate,
1559 .setattr = afs_notify_change,
1561 .default_file_ops = &afs_file_fops,
1562 .readpage = afs_linux_readpage,
1563 .revalidate = afs_linux_revalidate,
1564 .updatepage = afs_linux_updatepage,
1568 #if defined(AFS_LINUX24_ENV)
1569 static struct address_space_operations afs_file_aops = {
1570 .readpage = afs_linux_readpage,
1571 .writepage = afs_linux_writepage,
1572 .commit_write = afs_linux_commit_write,
1573 .prepare_write = afs_linux_prepare_write,
1578 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1579 * by what sort of operation is allowed.....
1582 static struct inode_operations afs_dir_iops = {
1583 #if !defined(AFS_LINUX24_ENV)
1584 .default_file_ops = &afs_dir_fops,
1586 .setattr = afs_notify_change,
1588 .create = afs_linux_create,
1589 .lookup = afs_linux_lookup,
1590 .link = afs_linux_link,
1591 .unlink = afs_linux_unlink,
1592 .symlink = afs_linux_symlink,
1593 .mkdir = afs_linux_mkdir,
1594 .rmdir = afs_linux_rmdir,
1595 .rename = afs_linux_rename,
1596 #if defined(AFS_LINUX26_ENV)
1597 .getattr = afs_linux_getattr,
1599 .revalidate = afs_linux_revalidate,
1601 .permission = afs_linux_permission,
1604 /* We really need a separate symlink set of ops, since do_follow_link()
1605 * determines if it _is_ a link by checking if the follow_link op is set.
1607 #if defined(AFS_LINUX24_ENV)
1609 afs_symlink_filler(struct file *file, struct page *page)
1611 struct inode *ip = (struct inode *)page->mapping->host;
1612 char *p = (char *)kmap(page);
1617 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1622 p[code] = '\0'; /* null terminate? */
1625 SetPageUptodate(page);
1639 static struct address_space_operations afs_symlink_aops = {
1640 .readpage = afs_symlink_filler
1644 static struct inode_operations afs_symlink_iops = {
1645 #if defined(AFS_LINUX24_ENV)
1646 .readlink = page_readlink,
1647 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1648 .follow_link = page_follow_link,
1650 .follow_link = page_follow_link_light,
1651 .put_link = page_put_link,
1653 .setattr = afs_notify_change,
1655 .readlink = afs_linux_readlink,
1656 .follow_link = afs_linux_follow_link,
1657 .permission = afs_linux_permission,
1658 .revalidate = afs_linux_revalidate,
1663 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1667 vattr2inode(ip, vattr);
1669 /* Reset ops if symlink or directory. */
1670 if (S_ISREG(ip->i_mode)) {
1671 ip->i_op = &afs_file_iops;
1672 #if defined(AFS_LINUX24_ENV)
1673 ip->i_fop = &afs_file_fops;
1674 ip->i_data.a_ops = &afs_file_aops;
1677 } else if (S_ISDIR(ip->i_mode)) {
1678 ip->i_op = &afs_dir_iops;
1679 #if defined(AFS_LINUX24_ENV)
1680 ip->i_fop = &afs_dir_fops;
1683 } else if (S_ISLNK(ip->i_mode)) {
1684 ip->i_op = &afs_symlink_iops;
1685 #if defined(AFS_LINUX24_ENV)
1686 ip->i_data.a_ops = &afs_symlink_aops;
1687 ip->i_mapping = &ip->i_data;
1691 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */