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 #ifdef AFS_LINUX26_ENV
467 struct file_lock flp2;
469 flp2.fl_flags &=~ FL_SLEEP;
470 code = posix_lock_file(fp, &flp2);
472 code = posix_lock_file(fp, flp, 0);
474 osi_Assert(code != -EAGAIN); /* there should be no conflicts */
476 struct AFS_FLOCK flock2;
478 flock2.l_type = F_UNLCK;
480 afs_lockctl(vcp, &flock2, F_SETLK, credp);
485 /* Convert flock back to Linux's file_lock */
486 flp->fl_type = flock.l_type;
487 flp->fl_pid = flock.l_pid;
488 flp->fl_start = flock.l_start;
489 flp->fl_end = flock.l_start + flock.l_len;
497 * essentially the same as afs_fsync() but we need to get the return
498 * code for the sys_close() here, not afs_linux_release(), so call
499 * afs_StoreAllSegments() with AFS_LASTSTORE
502 afs_linux_flush(struct file *fp)
504 struct vrequest treq;
505 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
506 cred_t *credp = crref();
511 code = afs_InitReq(&treq, credp);
515 ObtainSharedLock(&vcp->lock, 535);
516 if (vcp->execsOrWriters > 0) {
517 UpgradeSToWLock(&vcp->lock, 536);
518 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
519 ConvertWToSLock(&vcp->lock);
521 code = afs_CheckCode(code, &treq, 54);
522 ReleaseSharedLock(&vcp->lock);
531 #if !defined(AFS_LINUX24_ENV)
532 /* Not allowed to directly read a directory. */
534 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
542 struct file_operations afs_dir_fops = {
543 #if !defined(AFS_LINUX24_ENV)
544 .read = afs_linux_dir_read,
545 .lock = afs_linux_lock,
546 .fsync = afs_linux_fsync,
548 .read = generic_read_dir,
550 .readdir = afs_linux_readdir,
551 #ifdef HAVE_UNLOCKED_IOCTL
552 .unlocked_ioctl = afs_unlocked_xioctl,
556 #ifdef HAVE_COMPAT_IOCTL
557 .compat_ioctl = afs_unlocked_xioctl,
559 .open = afs_linux_open,
560 .release = afs_linux_release,
563 struct file_operations afs_file_fops = {
564 .read = afs_linux_read,
565 .write = afs_linux_write,
566 #ifdef HAVE_UNLOCKED_IOCTL
567 .unlocked_ioctl = afs_unlocked_xioctl,
571 #ifdef HAVE_COMPAT_IOCTL
572 .compat_ioctl = afs_unlocked_xioctl,
574 .mmap = afs_linux_mmap,
575 .open = afs_linux_open,
576 .flush = afs_linux_flush,
577 #ifdef AFS_LINUX26_ENV
578 .sendfile = generic_file_sendfile,
580 .release = afs_linux_release,
581 .fsync = afs_linux_fsync,
582 .lock = afs_linux_lock,
586 /**********************************************************************
587 * AFS Linux dentry operations
588 **********************************************************************/
590 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
591 * that has its mvid (parent dir's fid) pointer set to the wrong directory
592 * due to being mounted in multiple points at once. If so, check_bad_parent()
593 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
594 * dotdotfid and mtpoint fid members.
596 * dp - dentry to be checked.
600 * This dentry's vcache's mvid will be set to the correct parent directory's
602 * This root vnode's volume will have its dotdotfid and mtpoint fids set
603 * to the correct parent and mountpoint fids.
607 check_bad_parent(struct dentry *dp)
610 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
611 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
613 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
616 /* force a lookup, so vcp->mvid is fixed up */
617 afs_lookup(pvc, dp->d_name.name, &avc, credp);
618 if (!avc || vcp != avc) { /* bad, very bad.. */
619 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
620 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
621 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
622 ICL_TYPE_POINTER, dp);
625 AFS_RELE(AFSTOV(avc));
632 /* afs_linux_revalidate
633 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
636 afs_linux_revalidate(struct dentry *dp)
639 struct vcache *vcp = VTOAFS(dp->d_inode);
643 #ifdef AFS_LINUX24_ENV
649 /* Make this a fast path (no crref), since it's called so often. */
650 if (vcp->states & CStatd) {
652 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
653 check_bad_parent(dp); /* check and correct mvid */
656 #ifdef AFS_LINUX24_ENV
664 code = afs_getattr(vcp, &vattr, credp);
666 vattr2inode(AFSTOV(vcp), &vattr);
669 #ifdef AFS_LINUX24_ENV
677 #if defined(AFS_LINUX26_ENV)
679 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
681 int err = afs_linux_revalidate(dentry);
683 generic_fillattr(dentry->d_inode, stat);
689 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
690 * In kernels 2.2.10 and above, we are passed an additional flags var which
691 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
692 * we are advised to follow the entry if it is a link or to make sure that
693 * it is a directory. But since the kernel itself checks these possibilities
694 * later on, we shouldn't have to do it until later. Perhaps in the future..
697 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
698 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
699 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
701 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
704 afs_linux_dentry_revalidate(struct dentry *dp)
708 cred_t *credp = NULL;
709 struct vcache *vcp, *pvcp, *tvc = NULL;
712 #ifdef AFS_LINUX24_ENV
719 vcp = VTOAFS(dp->d_inode);
720 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
722 if (vcp == afs_globalVp)
725 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
726 check_bad_parent(dp); /* check and correct mvid */
729 /* If the last looker changes, we should make sure the current
730 * looker still has permission to examine this file. This would
731 * always require a crref() which would be "slow".
733 if (vcp->last_looker != treq.uid) {
734 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
737 vcp->last_looker = treq.uid;
741 /* If the parent's DataVersion has changed or the vnode
742 * is longer valid, we need to do a full lookup. VerifyVCache
743 * isn't enough since the vnode may have been renamed.
746 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
749 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
750 if (!tvc || tvc != vcp)
753 if (afs_getattr(vcp, &vattr, credp))
756 vattr2inode(AFSTOV(vcp), &vattr);
757 dp->d_time = hgetlo(pvcp->m.DataVersion);
760 /* should we always update the attributes at this point? */
761 /* unlikely--the vcache entry hasn't changed */
765 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
766 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
770 /* No change in parent's DataVersion so this negative
771 * lookup is still valid. BUT, if a server is down a
772 * negative lookup can result so there should be a
773 * liftime as well. For now, always expire.
791 shrink_dcache_parent(dp);
794 #ifdef AFS_LINUX24_ENV
805 afs_dentry_iput(struct dentry *dp, struct inode *ip)
807 struct vcache *vcp = VTOAFS(ip);
810 if (vcp->states & CUnlinked)
811 (void) afs_InactiveVCache(vcp, NULL);
818 afs_dentry_delete(struct dentry *dp)
820 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
821 return 1; /* bad inode? */
826 struct dentry_operations afs_dentry_operations = {
827 .d_revalidate = afs_linux_dentry_revalidate,
828 .d_delete = afs_dentry_delete,
829 .d_iput = afs_dentry_iput,
832 /**********************************************************************
833 * AFS Linux inode operations
834 **********************************************************************/
838 * Merely need to set enough of vattr to get us through the create. Note
839 * that the higher level code (open_namei) will take care of any tuncation
840 * explicitly. Exclusive open is also taken care of in open_namei.
842 * name is in kernel space at this point.
845 #ifdef IOP_CREATE_TAKES_NAMEIDATA
846 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
847 struct nameidata *nd)
849 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
853 cred_t *credp = crref();
854 const char *name = dp->d_name.name;
859 vattr.va_mode = mode;
860 vattr.va_type = mode & S_IFMT;
862 #if defined(AFS_LINUX26_ENV)
866 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
870 struct inode *ip = AFSTOV(vcp);
872 afs_getattr(vcp, &vattr, credp);
873 afs_fill_inode(ip, &vattr);
874 dp->d_op = &afs_dentry_operations;
875 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
876 d_instantiate(dp, ip);
880 #if defined(AFS_LINUX26_ENV)
887 /* afs_linux_lookup */
888 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
889 static struct dentry *
890 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
891 afs_linux_lookup(struct inode *dip, struct dentry *dp,
892 struct nameidata *nd)
894 afs_linux_lookup(struct inode *dip, struct dentry *dp)
898 afs_linux_lookup(struct inode *dip, struct dentry *dp)
901 cred_t *credp = crref();
902 struct vcache *vcp = NULL;
903 const char *comp = dp->d_name.name;
904 struct inode *ip = NULL;
907 #if defined(AFS_LINUX26_ENV)
911 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
917 afs_getattr(vcp, &vattr, credp);
918 afs_fill_inode(ip, &vattr);
920 dp->d_op = &afs_dentry_operations;
921 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
924 #if defined(AFS_LINUX24_ENV)
925 if (ip && S_ISDIR(ip->i_mode)) {
926 struct dentry *alias;
928 alias = d_find_alias(ip);
930 if (d_invalidate(alias) == 0) {
934 #if defined(AFS_LINUX26_ENV)
944 #if defined(AFS_LINUX26_ENV)
949 /* It's ok for the file to not be found. That's noted by the caller by
950 * seeing that the dp->d_inode field is NULL.
952 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
956 return ERR_PTR(-code);
965 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
968 cred_t *credp = crref();
969 const char *name = newdp->d_name.name;
970 struct inode *oldip = olddp->d_inode;
972 /* If afs_link returned the vnode, we could instantiate the
973 * dentry. Since it's not, we drop this one and do a new lookup.
978 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
986 afs_linux_unlink(struct inode *dip, struct dentry *dp)
989 cred_t *credp = crref();
990 const char *name = dp->d_name.name;
991 struct vcache *tvc = VTOAFS(dp->d_inode);
993 #if defined(AFS_LINUX26_ENV)
996 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
997 && !(tvc->states & CUnlinked)) {
1000 extern char *afs_newname();
1009 osi_FreeSmallSpace(__name);
1010 __name = afs_newname();
1013 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1017 } while (__dp->d_inode != NULL);
1020 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1022 tvc->mvid = (void *) __name;
1025 crfree(tvc->uncred);
1027 tvc->uncred = credp;
1028 tvc->states |= CUnlinked;
1033 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1042 code = afs_remove(VTOAFS(dip), name, credp);
1047 #if defined(AFS_LINUX26_ENV)
1056 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1059 cred_t *credp = crref();
1061 const char *name = dp->d_name.name;
1063 /* If afs_symlink returned the vnode, we could instantiate the
1064 * dentry. Since it's not, we drop this one and do a new lookup.
1070 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1077 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1080 cred_t *credp = crref();
1081 struct vcache *tvcp = NULL;
1083 const char *name = dp->d_name.name;
1085 #if defined(AFS_LINUX26_ENV)
1089 vattr.va_mask = ATTR_MODE;
1090 vattr.va_mode = mode;
1092 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1095 struct inode *ip = AFSTOV(tvcp);
1097 afs_getattr(tvcp, &vattr, credp);
1098 afs_fill_inode(ip, &vattr);
1100 dp->d_op = &afs_dentry_operations;
1101 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1102 d_instantiate(dp, ip);
1106 #if defined(AFS_LINUX26_ENV)
1114 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1117 cred_t *credp = crref();
1118 const char *name = dp->d_name.name;
1120 /* locking kernel conflicts with glock? */
1123 code = afs_rmdir(VTOAFS(dip), name, credp);
1126 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1127 * that failed because a directory is not empty. So, we map
1128 * EEXIST to ENOTEMPTY on linux.
1130 if (code == EEXIST) {
1144 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1145 struct inode *newip, struct dentry *newdp)
1148 cred_t *credp = crref();
1149 const char *oldname = olddp->d_name.name;
1150 const char *newname = newdp->d_name.name;
1151 struct dentry *rehash = NULL;
1153 #if defined(AFS_LINUX26_ENV)
1154 /* Prevent any new references during rename operation. */
1157 /* Remove old and new entries from name hash. New one will change below.
1158 * While it's optimal to catch failures and re-insert newdp into hash,
1159 * it's also error prone and in that case we're already dealing with error
1160 * cases. Let another lookup put things right, if need be.
1162 #if defined(AFS_LINUX26_ENV)
1163 if (!d_unhashed(newdp)) {
1168 if (!list_empty(&newdp->d_hash)) {
1174 #if defined(AFS_LINUX24_ENV)
1175 if (atomic_read(&olddp->d_count) > 1)
1176 shrink_dcache_parent(olddp);
1180 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1186 #if defined(AFS_LINUX26_ENV)
1195 /* afs_linux_ireadlink
1196 * Internal readlink which can return link contents to user or kernel space.
1197 * Note that the buffer is NOT supposed to be null-terminated.
1200 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1203 cred_t *credp = crref();
1207 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1208 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1212 return maxlen - tuio.uio_resid;
1217 #if !defined(AFS_LINUX24_ENV)
1218 /* afs_linux_readlink
1219 * Fill target (which is in user space) with contents of symlink.
1222 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1225 struct inode *ip = dp->d_inode;
1228 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1234 /* afs_linux_follow_link
1235 * a file system dependent link following routine.
1237 static struct dentry *
1238 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1239 unsigned int follow)
1247 name = osi_Alloc(PATH_MAX + 1);
1251 return ERR_PTR(-EIO);
1254 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1259 res = ERR_PTR(code);
1262 res = lookup_dentry(name, basep, follow);
1266 osi_Free(name, PATH_MAX + 1);
1272 /* afs_linux_readpage
1273 * all reads come through here. A strategy-like read call.
1276 afs_linux_readpage(struct file *fp, struct page *pp)
1279 cred_t *credp = crref();
1280 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1282 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1284 ulong address = afs_linux_page_address(pp);
1285 afs_offs_t offset = pageoff(pp);
1289 struct inode *ip = FILE_INODE(fp);
1290 int cnt = page_count(pp);
1291 struct vcache *avc = VTOAFS(ip);
1294 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1298 atomic_add(1, &pp->count);
1299 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1300 clear_bit(PG_error, &pp->flags);
1303 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1305 #ifdef AFS_LINUX24_ENV
1309 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 */
1310 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1311 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1312 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1315 #ifdef AFS_LINUX24_ENV
1320 if (tuio.uio_resid) /* zero remainder of page */
1321 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1323 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1324 flush_dcache_page(pp);
1325 SetPageUptodate(pp);
1327 set_bit(PG_uptodate, &pp->flags);
1331 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1335 clear_bit(PG_locked, &pp->flags);
1340 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1342 struct vrequest treq;
1345 code = afs_InitReq(&treq, credp);
1346 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1347 tdc = afs_FindDCache(avc, offset);
1349 if (!(tdc->mflags & DFNextStarted))
1350 afs_PrefetchChunk(avc, tdc, credp, &treq);
1353 ReleaseWriteLock(&avc->lock);
1363 #if defined(AFS_LINUX24_ENV)
1365 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1366 unsigned long offset, unsigned int count)
1368 struct vcache *vcp = VTOAFS(ip);
1377 buffer = kmap(pp) + offset;
1378 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1383 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1384 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1385 ICL_TYPE_INT32, 99999);
1387 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1389 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1391 ip->i_size = vcp->m.Length;
1392 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1395 struct vrequest treq;
1397 ObtainWriteLock(&vcp->lock, 533);
1398 if (!afs_InitReq(&treq, credp))
1399 code = afs_DoPartialWrite(vcp, &treq);
1400 ReleaseWriteLock(&vcp->lock);
1402 code = code ? -code : count - tuio.uio_resid;
1404 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1405 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1406 ICL_TYPE_INT32, code);
1418 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1419 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1421 afs_linux_writepage(struct page *pp)
1424 struct address_space *mapping = pp->mapping;
1425 struct inode *inode;
1426 unsigned long end_index;
1427 unsigned offset = PAGE_CACHE_SIZE;
1430 #if defined(AFS_LINUX26_ENV)
1431 if (PageReclaim(pp)) {
1432 # if defined(WRITEPAGE_ACTIVATE)
1433 return WRITEPAGE_ACTIVATE;
1435 return AOP_WRITEPAGE_ACTIVATE;
1439 if (PageLaunder(pp)) {
1440 return(fail_writepage(pp));
1444 inode = (struct inode *)mapping->host;
1445 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1448 if (pp->index < end_index)
1450 /* things got complicated... */
1451 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1452 /* OK, are we completely out? */
1453 if (pp->index >= end_index + 1 || !offset)
1456 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1457 SetPageUptodate(pp);
1459 if (status == offset)
1465 /* afs_linux_updatepage
1466 * What one would have thought was writepage - write dirty page to file.
1467 * Called from generic_file_write. buffer is still in user space. pagep
1468 * has been filled in with old data if we're updating less than a page.
1471 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1472 unsigned int count, int sync)
1474 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1475 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1481 set_bit(PG_locked, &pp->flags);
1485 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1486 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1487 ICL_TYPE_INT32, 99999);
1488 setup_uio(&tuio, &iovec, page_addr + offset,
1489 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1492 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1494 ip->i_size = vcp->m.Length;
1495 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1498 struct vrequest treq;
1500 ObtainWriteLock(&vcp->lock, 533);
1501 vcp->m.Date = osi_Time(); /* set modification time */
1502 if (!afs_InitReq(&treq, credp))
1503 code = afs_DoPartialWrite(vcp, &treq);
1504 ReleaseWriteLock(&vcp->lock);
1507 code = code ? -code : count - tuio.uio_resid;
1508 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1509 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1510 ICL_TYPE_INT32, code);
1515 clear_bit(PG_locked, &pp->flags);
1520 /* afs_linux_permission
1521 * Check access rights - returns error if can't check or permission denied.
1524 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1525 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1527 afs_linux_permission(struct inode *ip, int mode)
1531 cred_t *credp = crref();
1535 if (mode & MAY_EXEC)
1537 if (mode & MAY_READ)
1539 if (mode & MAY_WRITE)
1541 code = afs_access(VTOAFS(ip), tmp, credp);
1548 #if defined(AFS_LINUX24_ENV)
1550 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1555 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1556 offset, to - offset);
1557 #if !defined(AFS_LINUX26_ENV)
1565 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1568 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1569 call kmap directly instead of relying on us to do it */
1570 #if !defined(AFS_LINUX26_ENV)
1576 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1579 static struct inode_operations afs_file_iops = {
1580 #if defined(AFS_LINUX26_ENV)
1581 .permission = afs_linux_permission,
1582 .getattr = afs_linux_getattr,
1583 .setattr = afs_notify_change,
1584 #elif defined(AFS_LINUX24_ENV)
1585 .permission = afs_linux_permission,
1586 .revalidate = afs_linux_revalidate,
1587 .setattr = afs_notify_change,
1589 .default_file_ops = &afs_file_fops,
1590 .readpage = afs_linux_readpage,
1591 .revalidate = afs_linux_revalidate,
1592 .updatepage = afs_linux_updatepage,
1596 #if defined(AFS_LINUX24_ENV)
1597 static struct address_space_operations afs_file_aops = {
1598 .readpage = afs_linux_readpage,
1599 .writepage = afs_linux_writepage,
1600 .commit_write = afs_linux_commit_write,
1601 .prepare_write = afs_linux_prepare_write,
1606 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1607 * by what sort of operation is allowed.....
1610 static struct inode_operations afs_dir_iops = {
1611 #if !defined(AFS_LINUX24_ENV)
1612 .default_file_ops = &afs_dir_fops,
1614 .setattr = afs_notify_change,
1616 .create = afs_linux_create,
1617 .lookup = afs_linux_lookup,
1618 .link = afs_linux_link,
1619 .unlink = afs_linux_unlink,
1620 .symlink = afs_linux_symlink,
1621 .mkdir = afs_linux_mkdir,
1622 .rmdir = afs_linux_rmdir,
1623 .rename = afs_linux_rename,
1624 #if defined(AFS_LINUX26_ENV)
1625 .getattr = afs_linux_getattr,
1627 .revalidate = afs_linux_revalidate,
1629 .permission = afs_linux_permission,
1632 /* We really need a separate symlink set of ops, since do_follow_link()
1633 * determines if it _is_ a link by checking if the follow_link op is set.
1635 #if defined(AFS_LINUX24_ENV)
1637 afs_symlink_filler(struct file *file, struct page *page)
1639 struct inode *ip = (struct inode *)page->mapping->host;
1640 char *p = (char *)kmap(page);
1645 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1650 p[code] = '\0'; /* null terminate? */
1653 SetPageUptodate(page);
1667 static struct address_space_operations afs_symlink_aops = {
1668 .readpage = afs_symlink_filler
1672 static struct inode_operations afs_symlink_iops = {
1673 #if defined(AFS_LINUX24_ENV)
1674 .readlink = page_readlink,
1675 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1676 .follow_link = page_follow_link,
1678 .follow_link = page_follow_link_light,
1679 .put_link = page_put_link,
1681 .setattr = afs_notify_change,
1683 .readlink = afs_linux_readlink,
1684 .follow_link = afs_linux_follow_link,
1685 .permission = afs_linux_permission,
1686 .revalidate = afs_linux_revalidate,
1691 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1695 vattr2inode(ip, vattr);
1697 /* Reset ops if symlink or directory. */
1698 if (S_ISREG(ip->i_mode)) {
1699 ip->i_op = &afs_file_iops;
1700 #if defined(AFS_LINUX24_ENV)
1701 ip->i_fop = &afs_file_fops;
1702 ip->i_data.a_ops = &afs_file_aops;
1705 } else if (S_ISDIR(ip->i_mode)) {
1706 ip->i_op = &afs_dir_iops;
1707 #if defined(AFS_LINUX24_ENV)
1708 ip->i_fop = &afs_dir_fops;
1711 } else if (S_ISLNK(ip->i_mode)) {
1712 ip->i_op = &afs_symlink_iops;
1713 #if defined(AFS_LINUX24_ENV)
1714 ip->i_data.a_ops = &afs_symlink_aops;
1715 ip->i_mapping = &ip->i_data;
1719 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */