2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
27 #include "afs/sysincludes.h"
28 #include "afsincludes.h"
29 #include "afs/afs_stats.h"
31 #ifdef HAVE_MM_INLINE_H
32 #include "h/mm_inline.h"
34 #include "h/pagemap.h"
35 #if defined(AFS_LINUX24_ENV)
36 #include "h/smp_lock.h"
38 #if defined(AFS_LINUX26_ENV)
39 #include "h/writeback.h"
43 #define pageoff(pp) pgoff2loff((pp)->index)
45 #define pageoff(pp) pp->offset
48 #if defined(AFS_LINUX26_ENV)
49 #define UnlockPage(pp) unlock_page(pp)
52 extern struct vcache *afs_globalVp;
54 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
57 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
58 cred_t *credp = crref();
62 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
63 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
66 /* get a validated vcache entry */
67 code = afs_InitReq(&treq, credp);
69 code = afs_VerifyVCache(vcp, &treq);
74 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
76 code = generic_file_read(fp, buf, count, offp);
80 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
81 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
90 /* Now we have integrated VM for writes as well as reads. generic_file_write
91 * also takes care of re-positioning the pointer if file is open in append
92 * mode. Call fake open/close to ensure we do writes of core dumps.
95 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
99 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
100 struct vrequest treq;
101 cred_t *credp = crref();
105 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
106 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
107 (fp->f_flags & O_APPEND) ? 99998 : 99999);
110 /* get a validated vcache entry */
111 code = (ssize_t) afs_InitReq(&treq, credp);
113 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
115 ObtainWriteLock(&vcp->lock, 529);
117 ReleaseWriteLock(&vcp->lock);
122 code = generic_file_write(fp, buf, count, offp);
126 ObtainWriteLock(&vcp->lock, 530);
127 afs_FakeClose(vcp, credp);
128 ReleaseWriteLock(&vcp->lock);
130 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
131 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
139 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
141 /* This is a complete rewrite of afs_readdir, since we can make use of
142 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
143 * handling and use of bulkstats will need to be reflected here as well.
146 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
148 extern struct DirEntry *afs_dir_GetBlob();
149 struct vcache *avc = VTOAFS(FILE_INODE(fp));
150 struct vrequest treq;
151 register struct dcache *tdc;
158 afs_size_t origOffset, tlen;
159 cred_t *credp = crref();
160 struct afs_fakestat_state fakestat;
162 #if defined(AFS_LINUX26_ENV)
166 AFS_STATCNT(afs_readdir);
168 code = afs_InitReq(&treq, credp);
173 afs_InitFakeStat(&fakestat);
174 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
178 /* update the cache entry */
180 code = afs_VerifyVCache(avc, &treq);
184 /* get a reference to the entire directory */
185 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
191 ObtainSharedLock(&avc->lock, 810);
192 UpgradeSToWLock(&avc->lock, 811);
193 ObtainReadLock(&tdc->lock);
195 * Make sure that the data in the cache is current. There are two
196 * cases we need to worry about:
197 * 1. The cache data is being fetched by another process.
198 * 2. The cache data is no longer valid
200 while ((avc->states & CStatd)
201 && (tdc->dflags & DFFetching)
202 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
203 ReleaseReadLock(&tdc->lock);
204 ReleaseSharedLock(&avc->lock);
205 afs_osi_Sleep(&tdc->validPos);
206 ObtainSharedLock(&avc->lock, 812);
207 ObtainReadLock(&tdc->lock);
209 if (!(avc->states & CStatd)
210 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
211 ReleaseReadLock(&tdc->lock);
212 ReleaseSharedLock(&avc->lock);
217 /* Set the readdir-in-progress flag, and downgrade the lock
218 * to shared so others will be able to acquire a read lock.
220 avc->states |= CReadDir;
221 avc->dcreaddir = tdc;
222 avc->readdir_pid = MyPidxx;
223 ConvertWToSLock(&avc->lock);
225 /* Fill in until we get an error or we're done. This implementation
226 * takes an offset in units of blobs, rather than bytes.
229 offset = (int) fp->f_pos;
231 dirpos = BlobScan(tdc, offset);
235 de = afs_dir_GetBlob(tdc, dirpos);
239 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
242 len = strlen(de->name);
244 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
245 (unsigned long)&tdc->f.inode, dirpos);
246 DRelease((struct buffer *) de, 0);
247 ReleaseSharedLock(&avc->lock);
253 /* filldir returns -EINVAL when the buffer is full. */
254 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
256 unsigned int type = DT_UNKNOWN;
257 struct VenusFid afid;
260 afid.Cell = avc->fid.Cell;
261 afid.Fid.Volume = avc->fid.Fid.Volume;
262 afid.Fid.Vnode = ntohl(de->fid.vnode);
263 afid.Fid.Unique = ntohl(de->fid.vunique);
264 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
266 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
269 } else if (((tvc->states) & (CStatd | CTruth))) {
270 /* CTruth will be set if the object has
275 else if (vtype == VREG)
277 /* Don't do this until we're sure it can't be a mtpt */
278 /* else if (vtype == VLNK)
280 /* what other types does AFS support? */
282 /* clean up from afs_FindVCache */
286 * If this is NFS readdirplus, then the filler is going to
287 * call getattr on this inode, which will deadlock if we're
291 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
295 code = (*filldir) (dirbuf, de->name, len, offset, ino);
297 DRelease((struct buffer *)de, 0);
300 offset = dirpos + 1 + ((len + 16) >> 5);
302 /* If filldir didn't fill in the last one this is still pointing to that
305 fp->f_pos = (loff_t) offset;
307 ReleaseReadLock(&tdc->lock);
309 UpgradeSToWLock(&avc->lock, 813);
310 avc->states &= ~CReadDir;
312 avc->readdir_pid = 0;
313 ReleaseSharedLock(&avc->lock);
317 afs_PutFakeStat(&fakestat);
320 #if defined(AFS_LINUX26_ENV)
327 /* in afs_pioctl.c */
328 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
331 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
332 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
334 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
341 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
343 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
344 cred_t *credp = crref();
345 struct vrequest treq;
349 #if defined(AFS_LINUX24_ENV)
350 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
351 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
352 vmap->vm_end - vmap->vm_start);
354 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
355 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
356 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
360 /* get a validated vcache entry */
361 code = afs_InitReq(&treq, credp);
365 code = afs_VerifyVCache(vcp, &treq);
369 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
372 code = generic_file_mmap(fp, vmap);
375 vcp->states |= CMAPPED;
388 afs_linux_open(struct inode *ip, struct file *fp)
390 struct vcache *vcp = VTOAFS(ip);
391 cred_t *credp = crref();
394 #ifdef AFS_LINUX24_ENV
398 code = afs_open(&vcp, fp->f_flags, credp);
400 #ifdef AFS_LINUX24_ENV
409 afs_linux_release(struct inode *ip, struct file *fp)
411 struct vcache *vcp = VTOAFS(ip);
412 cred_t *credp = crref();
415 #ifdef AFS_LINUX24_ENV
419 code = afs_close(vcp, fp->f_flags, credp);
421 #ifdef AFS_LINUX24_ENV
430 #if defined(AFS_LINUX24_ENV)
431 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
433 afs_linux_fsync(struct file *fp, struct dentry *dp)
437 struct inode *ip = FILE_INODE(fp);
438 cred_t *credp = crref();
440 #ifdef AFS_LINUX24_ENV
444 code = afs_fsync(VTOAFS(ip), credp);
446 #ifdef AFS_LINUX24_ENV
456 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
459 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
460 cred_t *credp = crref();
461 struct AFS_FLOCK flock;
462 /* Convert to a lock format afs_lockctl understands. */
463 memset((char *)&flock, 0, sizeof(flock));
464 flock.l_type = flp->fl_type;
465 flock.l_pid = flp->fl_pid;
467 flock.l_start = flp->fl_start;
468 flock.l_len = flp->fl_end - flp->fl_start;
470 /* Safe because there are no large files, yet */
471 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
472 if (cmd == F_GETLK64)
474 else if (cmd == F_SETLK64)
476 else if (cmd == F_SETLKW64)
478 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
481 code = afs_lockctl(vcp, &flock, cmd, credp);
484 #ifdef AFS_LINUX24_ENV
485 if (code == 0 && (cmd == F_SETLK || cmd == F_SETLKW)) {
486 #ifdef AFS_LINUX26_ENV
487 struct file_lock flp2;
489 flp2.fl_flags &=~ FL_SLEEP;
490 code = posix_lock_file(fp, &flp2);
492 code = posix_lock_file(fp, flp, 0);
494 osi_Assert(code != -EAGAIN); /* there should be no conflicts */
496 struct AFS_FLOCK flock2;
498 flock2.l_type = F_UNLCK;
500 afs_lockctl(vcp, &flock2, F_SETLK, credp);
505 /* Convert flock back to Linux's file_lock */
506 flp->fl_type = flock.l_type;
507 flp->fl_pid = flock.l_pid;
508 flp->fl_start = flock.l_start;
509 flp->fl_end = flock.l_start + flock.l_len;
517 * essentially the same as afs_fsync() but we need to get the return
518 * code for the sys_close() here, not afs_linux_release(), so call
519 * afs_StoreAllSegments() with AFS_LASTSTORE
522 afs_linux_flush(struct file *fp)
524 struct vrequest treq;
525 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
526 cred_t *credp = crref();
531 code = afs_InitReq(&treq, credp);
535 ObtainSharedLock(&vcp->lock, 535);
536 if (vcp->execsOrWriters > 0) {
537 UpgradeSToWLock(&vcp->lock, 536);
538 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
539 ConvertWToSLock(&vcp->lock);
541 code = afs_CheckCode(code, &treq, 54);
542 ReleaseSharedLock(&vcp->lock);
551 #if !defined(AFS_LINUX24_ENV)
552 /* Not allowed to directly read a directory. */
554 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
562 struct file_operations afs_dir_fops = {
563 #if !defined(AFS_LINUX24_ENV)
564 .read = afs_linux_dir_read,
565 .lock = afs_linux_lock,
566 .fsync = afs_linux_fsync,
568 .read = generic_read_dir,
570 .readdir = afs_linux_readdir,
571 #ifdef HAVE_UNLOCKED_IOCTL
572 .unlocked_ioctl = afs_unlocked_xioctl,
576 #ifdef HAVE_COMPAT_IOCTL
577 .compat_ioctl = afs_unlocked_xioctl,
579 .open = afs_linux_open,
580 .release = afs_linux_release,
583 struct file_operations afs_file_fops = {
584 .read = afs_linux_read,
585 .write = afs_linux_write,
586 #ifdef HAVE_UNLOCKED_IOCTL
587 .unlocked_ioctl = afs_unlocked_xioctl,
591 #ifdef HAVE_COMPAT_IOCTL
592 .compat_ioctl = afs_unlocked_xioctl,
594 .mmap = afs_linux_mmap,
595 .open = afs_linux_open,
596 .flush = afs_linux_flush,
597 #ifdef AFS_LINUX26_ENV
598 .sendfile = generic_file_sendfile,
600 .release = afs_linux_release,
601 .fsync = afs_linux_fsync,
602 .lock = afs_linux_lock,
606 /**********************************************************************
607 * AFS Linux dentry operations
608 **********************************************************************/
610 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
611 * that has its mvid (parent dir's fid) pointer set to the wrong directory
612 * due to being mounted in multiple points at once. If so, check_bad_parent()
613 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
614 * dotdotfid and mtpoint fid members.
616 * dp - dentry to be checked.
620 * This dentry's vcache's mvid will be set to the correct parent directory's
622 * This root vnode's volume will have its dotdotfid and mtpoint fids set
623 * to the correct parent and mountpoint fids.
627 check_bad_parent(struct dentry *dp)
630 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
631 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
633 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
636 /* force a lookup, so vcp->mvid is fixed up */
637 afs_lookup(pvc, dp->d_name.name, &avc, credp);
638 if (!avc || vcp != avc) { /* bad, very bad.. */
639 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
640 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
641 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
642 ICL_TYPE_POINTER, dp);
645 AFS_RELE(AFSTOV(avc));
652 /* afs_linux_revalidate
653 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
656 afs_linux_revalidate(struct dentry *dp)
659 struct vcache *vcp = VTOAFS(dp->d_inode);
663 #ifdef AFS_LINUX24_ENV
669 /* Make this a fast path (no crref), since it's called so often. */
670 if (vcp->states & CStatd) {
672 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
673 check_bad_parent(dp); /* check and correct mvid */
676 #ifdef AFS_LINUX24_ENV
684 code = afs_getattr(vcp, &vattr, credp);
686 vattr2inode(AFSTOV(vcp), &vattr);
689 #ifdef AFS_LINUX24_ENV
697 #if defined(AFS_LINUX26_ENV)
699 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
701 int err = afs_linux_revalidate(dentry);
703 generic_fillattr(dentry->d_inode, stat);
709 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
710 * In kernels 2.2.10 and above, we are passed an additional flags var which
711 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
712 * we are advised to follow the entry if it is a link or to make sure that
713 * it is a directory. But since the kernel itself checks these possibilities
714 * later on, we shouldn't have to do it until later. Perhaps in the future..
717 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
718 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
719 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
721 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
724 afs_linux_dentry_revalidate(struct dentry *dp)
728 cred_t *credp = NULL;
729 struct vcache *vcp, *pvcp, *tvc = NULL;
732 #ifdef AFS_LINUX24_ENV
739 vcp = VTOAFS(dp->d_inode);
740 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
742 if (vcp == afs_globalVp)
745 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
746 check_bad_parent(dp); /* check and correct mvid */
749 /* If the last looker changes, we should make sure the current
750 * looker still has permission to examine this file. This would
751 * always require a crref() which would be "slow".
753 if (vcp->last_looker != treq.uid) {
754 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
757 vcp->last_looker = treq.uid;
761 /* If the parent's DataVersion has changed or the vnode
762 * is longer valid, we need to do a full lookup. VerifyVCache
763 * isn't enough since the vnode may have been renamed.
766 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
769 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
770 if (!tvc || tvc != vcp)
773 if (afs_getattr(vcp, &vattr, credp))
776 vattr2inode(AFSTOV(vcp), &vattr);
777 dp->d_time = hgetlo(pvcp->m.DataVersion);
780 /* should we always update the attributes at this point? */
781 /* unlikely--the vcache entry hasn't changed */
785 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
786 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
790 /* No change in parent's DataVersion so this negative
791 * lookup is still valid. BUT, if a server is down a
792 * negative lookup can result so there should be a
793 * liftime as well. For now, always expire.
811 shrink_dcache_parent(dp);
814 #ifdef AFS_LINUX24_ENV
825 afs_dentry_iput(struct dentry *dp, struct inode *ip)
827 struct vcache *vcp = VTOAFS(ip);
830 if (vcp->states & CUnlinked)
831 (void) afs_InactiveVCache(vcp, NULL);
838 afs_dentry_delete(struct dentry *dp)
840 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
841 return 1; /* bad inode? */
846 struct dentry_operations afs_dentry_operations = {
847 .d_revalidate = afs_linux_dentry_revalidate,
848 .d_delete = afs_dentry_delete,
849 .d_iput = afs_dentry_iput,
852 /**********************************************************************
853 * AFS Linux inode operations
854 **********************************************************************/
858 * Merely need to set enough of vattr to get us through the create. Note
859 * that the higher level code (open_namei) will take care of any tuncation
860 * explicitly. Exclusive open is also taken care of in open_namei.
862 * name is in kernel space at this point.
865 #ifdef IOP_CREATE_TAKES_NAMEIDATA
866 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
867 struct nameidata *nd)
869 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
873 cred_t *credp = crref();
874 const char *name = dp->d_name.name;
879 vattr.va_mode = mode;
880 vattr.va_type = mode & S_IFMT;
882 #if defined(AFS_LINUX26_ENV)
886 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
890 struct inode *ip = AFSTOV(vcp);
892 afs_getattr(vcp, &vattr, credp);
893 afs_fill_inode(ip, &vattr);
894 dp->d_op = &afs_dentry_operations;
895 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
896 d_instantiate(dp, ip);
900 #if defined(AFS_LINUX26_ENV)
907 /* afs_linux_lookup */
908 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
909 static struct dentry *
910 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
911 afs_linux_lookup(struct inode *dip, struct dentry *dp,
912 struct nameidata *nd)
914 afs_linux_lookup(struct inode *dip, struct dentry *dp)
918 afs_linux_lookup(struct inode *dip, struct dentry *dp)
921 cred_t *credp = crref();
922 struct vcache *vcp = NULL;
923 const char *comp = dp->d_name.name;
924 struct inode *ip = NULL;
925 #if defined(AFS_LINUX26_ENV)
926 struct dentry *newdp = NULL;
930 #if defined(AFS_LINUX26_ENV)
934 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
940 afs_getattr(vcp, &vattr, credp);
941 afs_fill_inode(ip, &vattr);
943 dp->d_op = &afs_dentry_operations;
944 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
947 #if defined(AFS_LINUX24_ENV)
948 if (ip && S_ISDIR(ip->i_mode)) {
949 struct dentry *alias;
951 /* Try to invalidate an existing alias in favor of our new one */
952 alias = d_find_alias(ip);
953 #if defined(AFS_LINUX26_ENV)
954 /* But not if it's disconnected; then we want d_splice_alias below */
955 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
959 if (d_invalidate(alias) == 0) {
963 #if defined(AFS_LINUX26_ENV)
971 #if defined(AFS_LINUX26_ENV)
972 newdp = d_splice_alias(ip, dp);
977 #if defined(AFS_LINUX26_ENV)
982 /* It's ok for the file to not be found. That's noted by the caller by
983 * seeing that the dp->d_inode field is NULL.
985 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
986 #if defined(AFS_LINUX26_ENV)
987 if (!code || code == ENOENT)
994 return ERR_PTR(-code);
1003 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1006 cred_t *credp = crref();
1007 const char *name = newdp->d_name.name;
1008 struct inode *oldip = olddp->d_inode;
1010 /* If afs_link returned the vnode, we could instantiate the
1011 * dentry. Since it's not, we drop this one and do a new lookup.
1016 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1024 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1027 cred_t *credp = crref();
1028 const char *name = dp->d_name.name;
1029 struct vcache *tvc = VTOAFS(dp->d_inode);
1031 #if defined(AFS_LINUX26_ENV)
1034 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1035 && !(tvc->states & CUnlinked)) {
1036 struct dentry *__dp;
1038 extern char *afs_newname();
1047 osi_FreeSmallSpace(__name);
1048 __name = afs_newname();
1051 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1055 } while (__dp->d_inode != NULL);
1058 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1060 tvc->mvid = (void *) __name;
1063 crfree(tvc->uncred);
1065 tvc->uncred = credp;
1066 tvc->states |= CUnlinked;
1071 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1080 code = afs_remove(VTOAFS(dip), name, credp);
1085 #if defined(AFS_LINUX26_ENV)
1094 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1097 cred_t *credp = crref();
1099 const char *name = dp->d_name.name;
1101 /* If afs_symlink returned the vnode, we could instantiate the
1102 * dentry. Since it's not, we drop this one and do a new lookup.
1108 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1115 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1118 cred_t *credp = crref();
1119 struct vcache *tvcp = NULL;
1121 const char *name = dp->d_name.name;
1123 #if defined(AFS_LINUX26_ENV)
1127 vattr.va_mask = ATTR_MODE;
1128 vattr.va_mode = mode;
1130 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1133 struct inode *ip = AFSTOV(tvcp);
1135 afs_getattr(tvcp, &vattr, credp);
1136 afs_fill_inode(ip, &vattr);
1138 dp->d_op = &afs_dentry_operations;
1139 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1140 d_instantiate(dp, ip);
1144 #if defined(AFS_LINUX26_ENV)
1152 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1155 cred_t *credp = crref();
1156 const char *name = dp->d_name.name;
1158 /* locking kernel conflicts with glock? */
1161 code = afs_rmdir(VTOAFS(dip), name, credp);
1164 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1165 * that failed because a directory is not empty. So, we map
1166 * EEXIST to ENOTEMPTY on linux.
1168 if (code == EEXIST) {
1182 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1183 struct inode *newip, struct dentry *newdp)
1186 cred_t *credp = crref();
1187 const char *oldname = olddp->d_name.name;
1188 const char *newname = newdp->d_name.name;
1189 struct dentry *rehash = NULL;
1191 #if defined(AFS_LINUX26_ENV)
1192 /* Prevent any new references during rename operation. */
1195 /* Remove old and new entries from name hash. New one will change below.
1196 * While it's optimal to catch failures and re-insert newdp into hash,
1197 * it's also error prone and in that case we're already dealing with error
1198 * cases. Let another lookup put things right, if need be.
1200 #if defined(AFS_LINUX26_ENV)
1201 if (!d_unhashed(newdp)) {
1206 if (!list_empty(&newdp->d_hash)) {
1212 #if defined(AFS_LINUX24_ENV)
1213 if (atomic_read(&olddp->d_count) > 1)
1214 shrink_dcache_parent(olddp);
1218 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1224 #if defined(AFS_LINUX26_ENV)
1233 /* afs_linux_ireadlink
1234 * Internal readlink which can return link contents to user or kernel space.
1235 * Note that the buffer is NOT supposed to be null-terminated.
1238 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1241 cred_t *credp = crref();
1245 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1246 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1250 return maxlen - tuio.uio_resid;
1255 #if !defined(AFS_LINUX24_ENV)
1256 /* afs_linux_readlink
1257 * Fill target (which is in user space) with contents of symlink.
1260 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1263 struct inode *ip = dp->d_inode;
1266 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1272 /* afs_linux_follow_link
1273 * a file system dependent link following routine.
1275 static struct dentry *
1276 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1277 unsigned int follow)
1285 name = osi_Alloc(PATH_MAX + 1);
1289 return ERR_PTR(-EIO);
1292 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1297 res = ERR_PTR(code);
1300 res = lookup_dentry(name, basep, follow);
1304 osi_Free(name, PATH_MAX + 1);
1310 /* afs_linux_readpage
1311 * all reads come through here. A strategy-like read call.
1314 afs_linux_readpage(struct file *fp, struct page *pp)
1317 cred_t *credp = crref();
1318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1320 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1322 ulong address = afs_linux_page_address(pp);
1323 afs_offs_t offset = pageoff(pp);
1327 struct inode *ip = FILE_INODE(fp);
1328 int cnt = page_count(pp);
1329 struct vcache *avc = VTOAFS(ip);
1332 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1336 atomic_add(1, &pp->count);
1337 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1338 clear_bit(PG_error, &pp->flags);
1341 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1343 #ifdef AFS_LINUX24_ENV
1347 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 */
1348 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1349 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1350 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1353 #ifdef AFS_LINUX24_ENV
1358 if (tuio.uio_resid) /* zero remainder of page */
1359 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1361 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1362 flush_dcache_page(pp);
1363 SetPageUptodate(pp);
1365 set_bit(PG_uptodate, &pp->flags);
1369 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1373 clear_bit(PG_locked, &pp->flags);
1378 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1380 struct vrequest treq;
1383 code = afs_InitReq(&treq, credp);
1384 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1385 tdc = afs_FindDCache(avc, offset);
1387 if (!(tdc->mflags & DFNextStarted))
1388 afs_PrefetchChunk(avc, tdc, credp, &treq);
1391 ReleaseWriteLock(&avc->lock);
1401 #if defined(AFS_LINUX24_ENV)
1403 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1404 unsigned long offset, unsigned int count)
1406 struct vcache *vcp = VTOAFS(ip);
1415 buffer = kmap(pp) + offset;
1416 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1421 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1422 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1423 ICL_TYPE_INT32, 99999);
1425 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1427 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1429 ip->i_size = vcp->m.Length;
1430 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1433 struct vrequest treq;
1435 ObtainWriteLock(&vcp->lock, 533);
1436 if (!afs_InitReq(&treq, credp))
1437 code = afs_DoPartialWrite(vcp, &treq);
1438 ReleaseWriteLock(&vcp->lock);
1440 code = code ? -code : count - tuio.uio_resid;
1442 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1443 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1444 ICL_TYPE_INT32, code);
1456 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1457 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1459 afs_linux_writepage(struct page *pp)
1462 struct address_space *mapping = pp->mapping;
1463 struct inode *inode;
1464 unsigned long end_index;
1465 unsigned offset = PAGE_CACHE_SIZE;
1468 #if defined(AFS_LINUX26_ENV)
1469 if (PageReclaim(pp)) {
1470 # if defined(WRITEPAGE_ACTIVATE)
1471 return WRITEPAGE_ACTIVATE;
1473 return AOP_WRITEPAGE_ACTIVATE;
1477 if (PageLaunder(pp)) {
1478 return(fail_writepage(pp));
1482 inode = (struct inode *)mapping->host;
1483 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1486 if (pp->index < end_index)
1488 /* things got complicated... */
1489 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1490 /* OK, are we completely out? */
1491 if (pp->index >= end_index + 1 || !offset)
1494 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1495 SetPageUptodate(pp);
1497 if (status == offset)
1503 /* afs_linux_updatepage
1504 * What one would have thought was writepage - write dirty page to file.
1505 * Called from generic_file_write. buffer is still in user space. pagep
1506 * has been filled in with old data if we're updating less than a page.
1509 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1510 unsigned int count, int sync)
1512 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1513 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1519 set_bit(PG_locked, &pp->flags);
1523 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1524 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1525 ICL_TYPE_INT32, 99999);
1526 setup_uio(&tuio, &iovec, page_addr + offset,
1527 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1530 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1532 ip->i_size = vcp->m.Length;
1533 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1536 struct vrequest treq;
1538 ObtainWriteLock(&vcp->lock, 533);
1539 vcp->m.Date = osi_Time(); /* set modification time */
1540 if (!afs_InitReq(&treq, credp))
1541 code = afs_DoPartialWrite(vcp, &treq);
1542 ReleaseWriteLock(&vcp->lock);
1545 code = code ? -code : count - tuio.uio_resid;
1546 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1547 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1548 ICL_TYPE_INT32, code);
1553 clear_bit(PG_locked, &pp->flags);
1558 /* afs_linux_permission
1559 * Check access rights - returns error if can't check or permission denied.
1562 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1563 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1565 afs_linux_permission(struct inode *ip, int mode)
1569 cred_t *credp = crref();
1573 if (mode & MAY_EXEC)
1575 if (mode & MAY_READ)
1577 if (mode & MAY_WRITE)
1579 code = afs_access(VTOAFS(ip), tmp, credp);
1586 #if defined(AFS_LINUX24_ENV)
1588 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1593 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1594 offset, to - offset);
1595 #if !defined(AFS_LINUX26_ENV)
1603 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1606 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1607 call kmap directly instead of relying on us to do it */
1608 #if !defined(AFS_LINUX26_ENV)
1614 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1617 static struct inode_operations afs_file_iops = {
1618 #if defined(AFS_LINUX26_ENV)
1619 .permission = afs_linux_permission,
1620 .getattr = afs_linux_getattr,
1621 .setattr = afs_notify_change,
1622 #elif defined(AFS_LINUX24_ENV)
1623 .permission = afs_linux_permission,
1624 .revalidate = afs_linux_revalidate,
1625 .setattr = afs_notify_change,
1627 .default_file_ops = &afs_file_fops,
1628 .readpage = afs_linux_readpage,
1629 .revalidate = afs_linux_revalidate,
1630 .updatepage = afs_linux_updatepage,
1634 #if defined(AFS_LINUX24_ENV)
1635 static struct address_space_operations afs_file_aops = {
1636 .readpage = afs_linux_readpage,
1637 .writepage = afs_linux_writepage,
1638 .commit_write = afs_linux_commit_write,
1639 .prepare_write = afs_linux_prepare_write,
1644 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1645 * by what sort of operation is allowed.....
1648 static struct inode_operations afs_dir_iops = {
1649 #if !defined(AFS_LINUX24_ENV)
1650 .default_file_ops = &afs_dir_fops,
1652 .setattr = afs_notify_change,
1654 .create = afs_linux_create,
1655 .lookup = afs_linux_lookup,
1656 .link = afs_linux_link,
1657 .unlink = afs_linux_unlink,
1658 .symlink = afs_linux_symlink,
1659 .mkdir = afs_linux_mkdir,
1660 .rmdir = afs_linux_rmdir,
1661 .rename = afs_linux_rename,
1662 #if defined(AFS_LINUX26_ENV)
1663 .getattr = afs_linux_getattr,
1665 .revalidate = afs_linux_revalidate,
1667 .permission = afs_linux_permission,
1670 /* We really need a separate symlink set of ops, since do_follow_link()
1671 * determines if it _is_ a link by checking if the follow_link op is set.
1673 #if defined(AFS_LINUX24_ENV)
1675 afs_symlink_filler(struct file *file, struct page *page)
1677 struct inode *ip = (struct inode *)page->mapping->host;
1678 char *p = (char *)kmap(page);
1683 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1688 p[code] = '\0'; /* null terminate? */
1691 SetPageUptodate(page);
1705 static struct address_space_operations afs_symlink_aops = {
1706 .readpage = afs_symlink_filler
1710 static struct inode_operations afs_symlink_iops = {
1711 #if defined(AFS_LINUX24_ENV)
1712 .readlink = page_readlink,
1713 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1714 .follow_link = page_follow_link,
1716 .follow_link = page_follow_link_light,
1717 .put_link = page_put_link,
1719 .setattr = afs_notify_change,
1721 .readlink = afs_linux_readlink,
1722 .follow_link = afs_linux_follow_link,
1723 .permission = afs_linux_permission,
1724 .revalidate = afs_linux_revalidate,
1729 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1733 vattr2inode(ip, vattr);
1735 /* Reset ops if symlink or directory. */
1736 if (S_ISREG(ip->i_mode)) {
1737 ip->i_op = &afs_file_iops;
1738 #if defined(AFS_LINUX24_ENV)
1739 ip->i_fop = &afs_file_fops;
1740 ip->i_data.a_ops = &afs_file_aops;
1743 } else if (S_ISDIR(ip->i_mode)) {
1744 ip->i_op = &afs_dir_iops;
1745 #if defined(AFS_LINUX24_ENV)
1746 ip->i_fop = &afs_dir_fops;
1749 } else if (S_ISLNK(ip->i_mode)) {
1750 ip->i_op = &afs_symlink_iops;
1751 #if defined(AFS_LINUX24_ENV)
1752 ip->i_data.a_ops = &afs_symlink_aops;
1753 ip->i_mapping = &ip->i_data;
1757 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */