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 ObtainReadLock(&avc->lock);
192 ObtainReadLock(&tdc->lock);
194 * Make sure that the data in the cache is current. There are two
195 * cases we need to worry about:
196 * 1. The cache data is being fetched by another process.
197 * 2. The cache data is no longer valid
199 while ((avc->states & CStatd)
200 && (tdc->dflags & DFFetching)
201 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
202 ReleaseReadLock(&tdc->lock);
203 ReleaseReadLock(&avc->lock);
204 afs_osi_Sleep(&tdc->validPos);
205 ObtainReadLock(&avc->lock);
206 ObtainReadLock(&tdc->lock);
208 if (!(avc->states & CStatd)
209 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
210 ReleaseReadLock(&tdc->lock);
211 ReleaseReadLock(&avc->lock);
216 /* Fill in until we get an error or we're done. This implementation
217 * takes an offset in units of blobs, rather than bytes.
220 offset = (int) fp->f_pos;
222 dirpos = BlobScan(tdc, offset);
226 de = afs_dir_GetBlob(tdc, dirpos);
230 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
233 len = strlen(de->name);
235 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
236 (unsigned long)&tdc->f.inode, dirpos);
237 DRelease((struct buffer *) de, 0);
239 ReleaseReadLock(&avc->lock);
244 /* filldir returns -EINVAL when the buffer is full. */
245 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
247 unsigned int type = DT_UNKNOWN;
248 struct VenusFid afid;
251 afid.Cell = avc->fid.Cell;
252 afid.Fid.Volume = avc->fid.Fid.Volume;
253 afid.Fid.Vnode = ntohl(de->fid.vnode);
254 afid.Fid.Unique = ntohl(de->fid.vunique);
255 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
257 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
260 } else if (((tvc->states) & (CStatd | CTruth))) {
261 /* CTruth will be set if the object has
266 else if (vtype == VREG)
268 /* Don't do this until we're sure it can't be a mtpt */
269 /* else if (vtype == VLNK)
271 /* what other types does AFS support? */
273 /* clean up from afs_FindVCache */
276 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
279 code = (*filldir) (dirbuf, de->name, len, offset, ino);
281 DRelease((struct buffer *)de, 0);
284 offset = dirpos + 1 + ((len + 16) >> 5);
286 /* If filldir didn't fill in the last one this is still pointing to that
289 fp->f_pos = (loff_t) offset;
291 ReleaseReadLock(&tdc->lock);
293 ReleaseReadLock(&avc->lock);
297 afs_PutFakeStat(&fakestat);
300 #if defined(AFS_LINUX26_ENV)
307 /* in afs_pioctl.c */
308 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
311 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
312 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
314 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
321 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
323 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
324 cred_t *credp = crref();
325 struct vrequest treq;
329 #if defined(AFS_LINUX24_ENV)
330 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
331 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
332 vmap->vm_end - vmap->vm_start);
334 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
335 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
336 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
340 /* get a validated vcache entry */
341 code = afs_InitReq(&treq, credp);
345 code = afs_VerifyVCache(vcp, &treq);
349 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
352 code = generic_file_mmap(fp, vmap);
355 vcp->states |= CMAPPED;
368 afs_linux_open(struct inode *ip, struct file *fp)
370 struct vcache *vcp = VTOAFS(ip);
371 cred_t *credp = crref();
374 #ifdef AFS_LINUX24_ENV
378 code = afs_open(&vcp, fp->f_flags, credp);
380 #ifdef AFS_LINUX24_ENV
389 afs_linux_release(struct inode *ip, struct file *fp)
391 struct vcache *vcp = VTOAFS(ip);
392 cred_t *credp = crref();
395 #ifdef AFS_LINUX24_ENV
399 code = afs_close(vcp, fp->f_flags, credp);
401 #ifdef AFS_LINUX24_ENV
410 #if defined(AFS_LINUX24_ENV)
411 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
413 afs_linux_fsync(struct file *fp, struct dentry *dp)
417 struct inode *ip = FILE_INODE(fp);
418 cred_t *credp = crref();
420 #ifdef AFS_LINUX24_ENV
424 code = afs_fsync(VTOAFS(ip), credp);
426 #ifdef AFS_LINUX24_ENV
436 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
439 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
440 cred_t *credp = crref();
441 struct AFS_FLOCK flock;
442 /* Convert to a lock format afs_lockctl understands. */
443 memset((char *)&flock, 0, sizeof(flock));
444 flock.l_type = flp->fl_type;
445 flock.l_pid = flp->fl_pid;
447 flock.l_start = flp->fl_start;
448 flock.l_len = flp->fl_end - flp->fl_start;
450 /* Safe because there are no large files, yet */
451 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
452 if (cmd == F_GETLK64)
454 else if (cmd == F_SETLK64)
456 else if (cmd == F_SETLKW64)
458 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
461 code = afs_lockctl(vcp, &flock, cmd, credp);
464 #ifdef AFS_LINUX24_ENV
465 if (code == 0 && (cmd == F_SETLK || cmd == F_SETLKW)) {
466 struct file_lock flp2;
468 #ifdef AFS_LINUX26_ENV
469 flp2.fl_flags &=~ FL_SLEEP;
471 code = posix_lock_file(fp, &flp2);
472 osi_Assert(code != -EAGAIN); /* there should be no conflicts */
474 struct AFS_FLOCK flock2;
476 flock2.l_type = F_UNLCK;
478 afs_lockctl(vcp, &flock2, F_SETLK, credp);
483 /* Convert flock back to Linux's file_lock */
484 flp->fl_type = flock.l_type;
485 flp->fl_pid = flock.l_pid;
486 flp->fl_start = flock.l_start;
487 flp->fl_end = flock.l_start + flock.l_len;
495 * essentially the same as afs_fsync() but we need to get the return
496 * code for the sys_close() here, not afs_linux_release(), so call
497 * afs_StoreAllSegments() with AFS_LASTSTORE
500 afs_linux_flush(struct file *fp)
502 struct vrequest treq;
503 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
504 cred_t *credp = crref();
509 code = afs_InitReq(&treq, credp);
513 ObtainSharedLock(&vcp->lock, 535);
514 if (vcp->execsOrWriters > 0) {
515 UpgradeSToWLock(&vcp->lock, 536);
516 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
517 ConvertWToSLock(&vcp->lock);
519 code = afs_CheckCode(code, &treq, 54);
520 ReleaseSharedLock(&vcp->lock);
529 #if !defined(AFS_LINUX24_ENV)
530 /* Not allowed to directly read a directory. */
532 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
540 struct file_operations afs_dir_fops = {
541 #if !defined(AFS_LINUX24_ENV)
542 .read = afs_linux_dir_read,
543 .lock = afs_linux_lock,
544 .fsync = afs_linux_fsync,
546 .read = generic_read_dir,
548 .readdir = afs_linux_readdir,
549 #ifdef HAVE_UNLOCKED_IOCTL
550 .unlocked_ioctl = afs_unlocked_xioctl,
554 #ifdef HAVE_COMPAT_IOCTL
555 .compat_ioctl = afs_unlocked_xioctl,
557 .open = afs_linux_open,
558 .release = afs_linux_release,
561 struct file_operations afs_file_fops = {
562 .read = afs_linux_read,
563 .write = afs_linux_write,
564 #ifdef HAVE_UNLOCKED_IOCTL
565 .unlocked_ioctl = afs_unlocked_xioctl,
569 #ifdef HAVE_COMPAT_IOCTL
570 .compat_ioctl = afs_unlocked_xioctl,
572 .mmap = afs_linux_mmap,
573 .open = afs_linux_open,
574 .flush = afs_linux_flush,
575 #ifdef AFS_LINUX26_ENV
576 .sendfile = generic_file_sendfile,
578 .release = afs_linux_release,
579 .fsync = afs_linux_fsync,
580 .lock = afs_linux_lock,
584 /**********************************************************************
585 * AFS Linux dentry operations
586 **********************************************************************/
588 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
589 * that has its mvid (parent dir's fid) pointer set to the wrong directory
590 * due to being mounted in multiple points at once. If so, check_bad_parent()
591 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
592 * dotdotfid and mtpoint fid members.
594 * dp - dentry to be checked.
598 * This dentry's vcache's mvid will be set to the correct parent directory's
600 * This root vnode's volume will have its dotdotfid and mtpoint fids set
601 * to the correct parent and mountpoint fids.
605 check_bad_parent(struct dentry *dp)
608 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
609 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
611 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
614 /* force a lookup, so vcp->mvid is fixed up */
615 afs_lookup(pvc, dp->d_name.name, &avc, credp);
616 if (!avc || vcp != avc) { /* bad, very bad.. */
617 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
618 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
619 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
620 ICL_TYPE_POINTER, dp);
623 AFS_RELE(AFSTOV(avc));
630 /* afs_linux_revalidate
631 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
634 afs_linux_revalidate(struct dentry *dp)
637 struct vcache *vcp = VTOAFS(dp->d_inode);
641 #ifdef AFS_LINUX24_ENV
647 /* Make this a fast path (no crref), since it's called so often. */
648 if (vcp->states & CStatd) {
650 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
651 check_bad_parent(dp); /* check and correct mvid */
654 #ifdef AFS_LINUX24_ENV
662 code = afs_getattr(vcp, &vattr, credp);
664 vattr2inode(AFSTOV(vcp), &vattr);
667 #ifdef AFS_LINUX24_ENV
675 #if defined(AFS_LINUX26_ENV)
677 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
679 int err = afs_linux_revalidate(dentry);
681 generic_fillattr(dentry->d_inode, stat);
687 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
688 * In kernels 2.2.10 and above, we are passed an additional flags var which
689 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
690 * we are advised to follow the entry if it is a link or to make sure that
691 * it is a directory. But since the kernel itself checks these possibilities
692 * later on, we shouldn't have to do it until later. Perhaps in the future..
695 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
696 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
697 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
699 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
702 afs_linux_dentry_revalidate(struct dentry *dp)
706 cred_t *credp = NULL;
707 struct vcache *vcp, *pvcp, *tvc = NULL;
710 #ifdef AFS_LINUX24_ENV
717 vcp = VTOAFS(dp->d_inode);
718 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
720 if (vcp == afs_globalVp)
723 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
724 check_bad_parent(dp); /* check and correct mvid */
727 /* If the last looker changes, we should make sure the current
728 * looker still has permission to examine this file. This would
729 * always require a crref() which would be "slow".
731 if (vcp->last_looker != treq.uid) {
732 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
735 vcp->last_looker = treq.uid;
739 /* If the parent's DataVersion has changed or the vnode
740 * is longer valid, we need to do a full lookup. VerifyVCache
741 * isn't enough since the vnode may have been renamed.
744 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
747 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
748 if (!tvc || tvc != vcp)
751 if (afs_getattr(vcp, &vattr, credp))
754 vattr2inode(AFSTOV(vcp), &vattr);
755 dp->d_time = hgetlo(pvcp->m.DataVersion);
758 /* should we always update the attributes at this point? */
759 /* unlikely--the vcache entry hasn't changed */
763 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
764 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
768 /* No change in parent's DataVersion so this negative
769 * lookup is still valid. BUT, if a server is down a
770 * negative lookup can result so there should be a
771 * liftime as well. For now, always expire.
789 shrink_dcache_parent(dp);
792 #ifdef AFS_LINUX24_ENV
803 afs_dentry_iput(struct dentry *dp, struct inode *ip)
805 struct vcache *vcp = VTOAFS(ip);
808 if (vcp->states & CUnlinked)
809 (void) afs_InactiveVCache(vcp, NULL);
816 afs_dentry_delete(struct dentry *dp)
818 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
819 return 1; /* bad inode? */
824 struct dentry_operations afs_dentry_operations = {
825 .d_revalidate = afs_linux_dentry_revalidate,
826 .d_delete = afs_dentry_delete,
827 .d_iput = afs_dentry_iput,
830 /**********************************************************************
831 * AFS Linux inode operations
832 **********************************************************************/
836 * Merely need to set enough of vattr to get us through the create. Note
837 * that the higher level code (open_namei) will take care of any tuncation
838 * explicitly. Exclusive open is also taken care of in open_namei.
840 * name is in kernel space at this point.
843 #ifdef IOP_CREATE_TAKES_NAMEIDATA
844 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
845 struct nameidata *nd)
847 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
851 cred_t *credp = crref();
852 const char *name = dp->d_name.name;
857 vattr.va_mode = mode;
858 vattr.va_type = mode & S_IFMT;
860 #if defined(AFS_LINUX26_ENV)
864 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
868 struct inode *ip = AFSTOV(vcp);
870 afs_getattr(vcp, &vattr, credp);
871 afs_fill_inode(ip, &vattr);
872 dp->d_op = &afs_dentry_operations;
873 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
874 d_instantiate(dp, ip);
878 #if defined(AFS_LINUX26_ENV)
885 /* afs_linux_lookup */
886 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
887 static struct dentry *
888 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
889 afs_linux_lookup(struct inode *dip, struct dentry *dp,
890 struct nameidata *nd)
892 afs_linux_lookup(struct inode *dip, struct dentry *dp)
896 afs_linux_lookup(struct inode *dip, struct dentry *dp)
899 cred_t *credp = crref();
900 struct vcache *vcp = NULL;
901 const char *comp = dp->d_name.name;
902 struct inode *ip = NULL;
905 #if defined(AFS_LINUX26_ENV)
909 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
915 afs_getattr(vcp, &vattr, credp);
916 afs_fill_inode(ip, &vattr);
918 dp->d_op = &afs_dentry_operations;
919 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
922 #if defined(AFS_LINUX24_ENV)
923 if (ip && S_ISDIR(ip->i_mode)) {
924 struct dentry *alias;
926 alias = d_find_alias(ip);
928 if (d_invalidate(alias) == 0) {
932 #if defined(AFS_LINUX26_ENV)
942 #if defined(AFS_LINUX26_ENV)
947 /* It's ok for the file to not be found. That's noted by the caller by
948 * seeing that the dp->d_inode field is NULL.
950 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
954 return ERR_PTR(-code);
963 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
966 cred_t *credp = crref();
967 const char *name = newdp->d_name.name;
968 struct inode *oldip = olddp->d_inode;
970 /* If afs_link returned the vnode, we could instantiate the
971 * dentry. Since it's not, we drop this one and do a new lookup.
976 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
984 afs_linux_unlink(struct inode *dip, struct dentry *dp)
987 cred_t *credp = crref();
988 const char *name = dp->d_name.name;
989 struct vcache *tvc = VTOAFS(dp->d_inode);
991 #if defined(AFS_LINUX26_ENV)
994 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
995 && !(tvc->states & CUnlinked)) {
998 extern char *afs_newname();
1007 osi_FreeSmallSpace(__name);
1008 __name = afs_newname();
1011 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1015 } while (__dp->d_inode != NULL);
1018 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1020 tvc->mvid = (void *) __name;
1023 crfree(tvc->uncred);
1025 tvc->uncred = credp;
1026 tvc->states |= CUnlinked;
1031 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1040 code = afs_remove(VTOAFS(dip), name, credp);
1045 #if defined(AFS_LINUX26_ENV)
1054 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1057 cred_t *credp = crref();
1059 const char *name = dp->d_name.name;
1061 /* If afs_symlink returned the vnode, we could instantiate the
1062 * dentry. Since it's not, we drop this one and do a new lookup.
1068 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1075 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1078 cred_t *credp = crref();
1079 struct vcache *tvcp = NULL;
1081 const char *name = dp->d_name.name;
1083 #if defined(AFS_LINUX26_ENV)
1087 vattr.va_mask = ATTR_MODE;
1088 vattr.va_mode = mode;
1090 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1093 struct inode *ip = AFSTOV(tvcp);
1095 afs_getattr(tvcp, &vattr, credp);
1096 afs_fill_inode(ip, &vattr);
1098 dp->d_op = &afs_dentry_operations;
1099 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1100 d_instantiate(dp, ip);
1104 #if defined(AFS_LINUX26_ENV)
1112 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1115 cred_t *credp = crref();
1116 const char *name = dp->d_name.name;
1118 /* locking kernel conflicts with glock? */
1121 code = afs_rmdir(VTOAFS(dip), name, credp);
1124 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1125 * that failed because a directory is not empty. So, we map
1126 * EEXIST to ENOTEMPTY on linux.
1128 if (code == EEXIST) {
1142 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1143 struct inode *newip, struct dentry *newdp)
1146 cred_t *credp = crref();
1147 const char *oldname = olddp->d_name.name;
1148 const char *newname = newdp->d_name.name;
1149 struct dentry *rehash = NULL;
1151 #if defined(AFS_LINUX26_ENV)
1152 /* Prevent any new references during rename operation. */
1155 /* Remove old and new entries from name hash. New one will change below.
1156 * While it's optimal to catch failures and re-insert newdp into hash,
1157 * it's also error prone and in that case we're already dealing with error
1158 * cases. Let another lookup put things right, if need be.
1160 #if defined(AFS_LINUX26_ENV)
1161 if (!d_unhashed(newdp)) {
1166 if (!list_empty(&newdp->d_hash)) {
1172 #if defined(AFS_LINUX24_ENV)
1173 if (atomic_read(&olddp->d_count) > 1)
1174 shrink_dcache_parent(olddp);
1178 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1184 #if defined(AFS_LINUX26_ENV)
1193 /* afs_linux_ireadlink
1194 * Internal readlink which can return link contents to user or kernel space.
1195 * Note that the buffer is NOT supposed to be null-terminated.
1198 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1201 cred_t *credp = crref();
1205 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1206 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1210 return maxlen - tuio.uio_resid;
1215 #if !defined(AFS_LINUX24_ENV)
1216 /* afs_linux_readlink
1217 * Fill target (which is in user space) with contents of symlink.
1220 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1223 struct inode *ip = dp->d_inode;
1226 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1232 /* afs_linux_follow_link
1233 * a file system dependent link following routine.
1235 static struct dentry *
1236 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1237 unsigned int follow)
1245 name = osi_Alloc(PATH_MAX + 1);
1249 return ERR_PTR(-EIO);
1252 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1257 res = ERR_PTR(code);
1260 res = lookup_dentry(name, basep, follow);
1264 osi_Free(name, PATH_MAX + 1);
1270 /* afs_linux_readpage
1271 * all reads come through here. A strategy-like read call.
1274 afs_linux_readpage(struct file *fp, struct page *pp)
1277 cred_t *credp = crref();
1278 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1280 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1282 ulong address = afs_linux_page_address(pp);
1283 afs_offs_t offset = pageoff(pp);
1287 struct inode *ip = FILE_INODE(fp);
1288 int cnt = page_count(pp);
1289 struct vcache *avc = VTOAFS(ip);
1292 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1296 atomic_add(1, &pp->count);
1297 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1298 clear_bit(PG_error, &pp->flags);
1301 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1303 #ifdef AFS_LINUX24_ENV
1307 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 */
1308 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1309 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1310 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1313 #ifdef AFS_LINUX24_ENV
1318 if (tuio.uio_resid) /* zero remainder of page */
1319 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1322 flush_dcache_page(pp);
1323 SetPageUptodate(pp);
1325 set_bit(PG_uptodate, &pp->flags);
1329 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1333 clear_bit(PG_locked, &pp->flags);
1338 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1340 struct vrequest treq;
1343 code = afs_InitReq(&treq, credp);
1344 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1345 tdc = afs_FindDCache(avc, offset);
1347 if (!(tdc->mflags & DFNextStarted))
1348 afs_PrefetchChunk(avc, tdc, credp, &treq);
1351 ReleaseWriteLock(&avc->lock);
1361 #if defined(AFS_LINUX24_ENV)
1363 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1364 unsigned long offset, unsigned int count)
1366 struct vcache *vcp = VTOAFS(ip);
1375 buffer = kmap(pp) + offset;
1376 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1381 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1382 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1383 ICL_TYPE_INT32, 99999);
1385 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1387 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1389 ip->i_size = vcp->m.Length;
1390 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1393 struct vrequest treq;
1395 ObtainWriteLock(&vcp->lock, 533);
1396 if (!afs_InitReq(&treq, credp))
1397 code = afs_DoPartialWrite(vcp, &treq);
1398 ReleaseWriteLock(&vcp->lock);
1400 code = code ? -code : count - tuio.uio_resid;
1402 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1403 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1404 ICL_TYPE_INT32, code);
1416 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1417 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1419 afs_linux_writepage(struct page *pp)
1422 struct address_space *mapping = pp->mapping;
1423 struct inode *inode;
1424 unsigned long end_index;
1425 unsigned offset = PAGE_CACHE_SIZE;
1428 #if defined(AFS_LINUX26_ENV)
1429 if (PageReclaim(pp)) {
1430 # if defined(WRITEPAGE_ACTIVATE)
1431 return WRITEPAGE_ACTIVATE;
1433 return AOP_WRITEPAGE_ACTIVATE;
1437 if (PageLaunder(pp)) {
1438 return(fail_writepage(pp));
1442 inode = (struct inode *)mapping->host;
1443 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1446 if (pp->index < end_index)
1448 /* things got complicated... */
1449 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1450 /* OK, are we completely out? */
1451 if (pp->index >= end_index + 1 || !offset)
1454 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1455 SetPageUptodate(pp);
1457 if (status == offset)
1463 /* afs_linux_updatepage
1464 * What one would have thought was writepage - write dirty page to file.
1465 * Called from generic_file_write. buffer is still in user space. pagep
1466 * has been filled in with old data if we're updating less than a page.
1469 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1470 unsigned int count, int sync)
1472 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1473 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1479 set_bit(PG_locked, &pp->flags);
1483 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1484 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1485 ICL_TYPE_INT32, 99999);
1486 setup_uio(&tuio, &iovec, page_addr + offset,
1487 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1490 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1492 ip->i_size = vcp->m.Length;
1493 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1496 struct vrequest treq;
1498 ObtainWriteLock(&vcp->lock, 533);
1499 vcp->m.Date = osi_Time(); /* set modification time */
1500 if (!afs_InitReq(&treq, credp))
1501 code = afs_DoPartialWrite(vcp, &treq);
1502 ReleaseWriteLock(&vcp->lock);
1505 code = code ? -code : count - tuio.uio_resid;
1506 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1507 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1508 ICL_TYPE_INT32, code);
1513 clear_bit(PG_locked, &pp->flags);
1518 /* afs_linux_permission
1519 * Check access rights - returns error if can't check or permission denied.
1522 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1523 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1525 afs_linux_permission(struct inode *ip, int mode)
1529 cred_t *credp = crref();
1533 if (mode & MAY_EXEC)
1535 if (mode & MAY_READ)
1537 if (mode & MAY_WRITE)
1539 code = afs_access(VTOAFS(ip), tmp, credp);
1546 #if defined(AFS_LINUX24_ENV)
1548 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1553 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1554 offset, to - offset);
1555 #if !defined(AFS_LINUX26_ENV)
1563 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1566 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1567 call kmap directly instead of relying on us to do it */
1568 #if !defined(AFS_LINUX26_ENV)
1574 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1577 static struct inode_operations afs_file_iops = {
1578 #if defined(AFS_LINUX26_ENV)
1579 .permission = afs_linux_permission,
1580 .getattr = afs_linux_getattr,
1581 .setattr = afs_notify_change,
1582 #elif defined(AFS_LINUX24_ENV)
1583 .permission = afs_linux_permission,
1584 .revalidate = afs_linux_revalidate,
1585 .setattr = afs_notify_change,
1587 .default_file_ops = &afs_file_fops,
1588 .readpage = afs_linux_readpage,
1589 .revalidate = afs_linux_revalidate,
1590 .updatepage = afs_linux_updatepage,
1594 #if defined(AFS_LINUX24_ENV)
1595 static struct address_space_operations afs_file_aops = {
1596 .readpage = afs_linux_readpage,
1597 .writepage = afs_linux_writepage,
1598 .commit_write = afs_linux_commit_write,
1599 .prepare_write = afs_linux_prepare_write,
1604 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1605 * by what sort of operation is allowed.....
1608 static struct inode_operations afs_dir_iops = {
1609 #if !defined(AFS_LINUX24_ENV)
1610 .default_file_ops = &afs_dir_fops,
1612 .setattr = afs_notify_change,
1614 .create = afs_linux_create,
1615 .lookup = afs_linux_lookup,
1616 .link = afs_linux_link,
1617 .unlink = afs_linux_unlink,
1618 .symlink = afs_linux_symlink,
1619 .mkdir = afs_linux_mkdir,
1620 .rmdir = afs_linux_rmdir,
1621 .rename = afs_linux_rename,
1622 #if defined(AFS_LINUX26_ENV)
1623 .getattr = afs_linux_getattr,
1625 .revalidate = afs_linux_revalidate,
1627 .permission = afs_linux_permission,
1630 /* We really need a separate symlink set of ops, since do_follow_link()
1631 * determines if it _is_ a link by checking if the follow_link op is set.
1633 #if defined(AFS_LINUX24_ENV)
1635 afs_symlink_filler(struct file *file, struct page *page)
1637 struct inode *ip = (struct inode *)page->mapping->host;
1638 char *p = (char *)kmap(page);
1643 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1648 p[code] = '\0'; /* null terminate? */
1651 SetPageUptodate(page);
1665 static struct address_space_operations afs_symlink_aops = {
1666 .readpage = afs_symlink_filler
1670 static struct inode_operations afs_symlink_iops = {
1671 #if defined(AFS_LINUX24_ENV)
1672 .readlink = page_readlink,
1673 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1674 .follow_link = page_follow_link,
1676 .follow_link = page_follow_link_light,
1677 .put_link = page_put_link,
1679 .setattr = afs_notify_change,
1681 .readlink = afs_linux_readlink,
1682 .follow_link = afs_linux_follow_link,
1683 .permission = afs_linux_permission,
1684 .revalidate = afs_linux_revalidate,
1689 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1693 vattr2inode(ip, vattr);
1695 /* Reset ops if symlink or directory. */
1696 if (S_ISREG(ip->i_mode)) {
1697 ip->i_op = &afs_file_iops;
1698 #if defined(AFS_LINUX24_ENV)
1699 ip->i_fop = &afs_file_fops;
1700 ip->i_data.a_ops = &afs_file_aops;
1703 } else if (S_ISDIR(ip->i_mode)) {
1704 ip->i_op = &afs_dir_iops;
1705 #if defined(AFS_LINUX24_ENV)
1706 ip->i_fop = &afs_dir_fops;
1709 } else if (S_ISLNK(ip->i_mode)) {
1710 ip->i_op = &afs_symlink_iops;
1711 #if defined(AFS_LINUX24_ENV)
1712 ip->i_data.a_ops = &afs_symlink_aops;
1713 ip->i_mapping = &ip->i_data;
1717 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */