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 */
77 code = do_sync_read(fp, buf, count, offp);
79 code = generic_file_read(fp, buf, count, offp);
84 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
85 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
94 /* Now we have integrated VM for writes as well as reads. generic_file_write
95 * also takes care of re-positioning the pointer if file is open in append
96 * mode. Call fake open/close to ensure we do writes of core dumps.
99 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
103 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
104 struct vrequest treq;
105 cred_t *credp = crref();
109 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
110 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
111 (fp->f_flags & O_APPEND) ? 99998 : 99999);
114 /* get a validated vcache entry */
115 code = (ssize_t) afs_InitReq(&treq, credp);
117 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
119 ObtainWriteLock(&vcp->lock, 529);
121 ReleaseWriteLock(&vcp->lock);
127 code = do_sync_write(fp, buf, count, offp);
129 code = generic_file_write(fp, buf, count, offp);
134 ObtainWriteLock(&vcp->lock, 530);
135 afs_FakeClose(vcp, credp);
136 ReleaseWriteLock(&vcp->lock);
138 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
139 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
147 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
149 /* This is a complete rewrite of afs_readdir, since we can make use of
150 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
151 * handling and use of bulkstats will need to be reflected here as well.
154 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
156 extern struct DirEntry *afs_dir_GetBlob();
157 struct vcache *avc = VTOAFS(FILE_INODE(fp));
158 struct vrequest treq;
159 register struct dcache *tdc;
166 afs_size_t origOffset, tlen;
167 cred_t *credp = crref();
168 struct afs_fakestat_state fakestat;
170 #if defined(AFS_LINUX26_ENV)
174 AFS_STATCNT(afs_readdir);
176 code = afs_InitReq(&treq, credp);
181 afs_InitFakeStat(&fakestat);
182 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
186 /* update the cache entry */
188 code = afs_VerifyVCache(avc, &treq);
192 /* get a reference to the entire directory */
193 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
199 ObtainSharedLock(&avc->lock, 810);
200 UpgradeSToWLock(&avc->lock, 811);
201 ObtainReadLock(&tdc->lock);
203 * Make sure that the data in the cache is current. There are two
204 * cases we need to worry about:
205 * 1. The cache data is being fetched by another process.
206 * 2. The cache data is no longer valid
208 while ((avc->states & CStatd)
209 && (tdc->dflags & DFFetching)
210 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
211 ReleaseReadLock(&tdc->lock);
212 ReleaseSharedLock(&avc->lock);
213 afs_osi_Sleep(&tdc->validPos);
214 ObtainSharedLock(&avc->lock, 812);
215 ObtainReadLock(&tdc->lock);
217 if (!(avc->states & CStatd)
218 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
219 ReleaseReadLock(&tdc->lock);
220 ReleaseSharedLock(&avc->lock);
225 /* Set the readdir-in-progress flag, and downgrade the lock
226 * to shared so others will be able to acquire a read lock.
228 avc->states |= CReadDir;
229 avc->dcreaddir = tdc;
230 avc->readdir_pid = MyPidxx;
231 ConvertWToSLock(&avc->lock);
233 /* Fill in until we get an error or we're done. This implementation
234 * takes an offset in units of blobs, rather than bytes.
237 offset = (int) fp->f_pos;
239 dirpos = BlobScan(tdc, offset);
243 de = afs_dir_GetBlob(tdc, dirpos);
247 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
250 len = strlen(de->name);
252 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
253 (unsigned long)&tdc->f.inode, dirpos);
254 DRelease((struct buffer *) de, 0);
255 ReleaseSharedLock(&avc->lock);
261 /* filldir returns -EINVAL when the buffer is full. */
262 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
264 unsigned int type = DT_UNKNOWN;
265 struct VenusFid afid;
268 afid.Cell = avc->fid.Cell;
269 afid.Fid.Volume = avc->fid.Fid.Volume;
270 afid.Fid.Vnode = ntohl(de->fid.vnode);
271 afid.Fid.Unique = ntohl(de->fid.vunique);
272 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
274 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
277 } else if (((tvc->states) & (CStatd | CTruth))) {
278 /* CTruth will be set if the object has
283 else if (vtype == VREG)
285 /* Don't do this until we're sure it can't be a mtpt */
286 /* else if (vtype == VLNK)
288 /* what other types does AFS support? */
290 /* clean up from afs_FindVCache */
294 * If this is NFS readdirplus, then the filler is going to
295 * call getattr on this inode, which will deadlock if we're
299 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
303 code = (*filldir) (dirbuf, de->name, len, offset, ino);
305 DRelease((struct buffer *)de, 0);
308 offset = dirpos + 1 + ((len + 16) >> 5);
310 /* If filldir didn't fill in the last one this is still pointing to that
313 fp->f_pos = (loff_t) offset;
315 ReleaseReadLock(&tdc->lock);
317 UpgradeSToWLock(&avc->lock, 813);
318 avc->states &= ~CReadDir;
320 avc->readdir_pid = 0;
321 ReleaseSharedLock(&avc->lock);
325 afs_PutFakeStat(&fakestat);
328 #if defined(AFS_LINUX26_ENV)
335 /* in afs_pioctl.c */
336 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
339 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
340 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
342 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
349 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
351 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
352 cred_t *credp = crref();
353 struct vrequest treq;
357 #if defined(AFS_LINUX24_ENV)
358 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
359 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
360 vmap->vm_end - vmap->vm_start);
362 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
363 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
364 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
368 /* get a validated vcache entry */
369 code = afs_InitReq(&treq, credp);
373 code = afs_VerifyVCache(vcp, &treq);
377 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
380 code = generic_file_mmap(fp, vmap);
383 vcp->states |= CMAPPED;
396 afs_linux_open(struct inode *ip, struct file *fp)
398 struct vcache *vcp = VTOAFS(ip);
399 cred_t *credp = crref();
402 #ifdef AFS_LINUX24_ENV
406 code = afs_open(&vcp, fp->f_flags, credp);
408 #ifdef AFS_LINUX24_ENV
417 afs_linux_release(struct inode *ip, struct file *fp)
419 struct vcache *vcp = VTOAFS(ip);
420 cred_t *credp = crref();
423 #ifdef AFS_LINUX24_ENV
427 code = afs_close(vcp, fp->f_flags, credp);
429 #ifdef AFS_LINUX24_ENV
438 #if defined(AFS_LINUX24_ENV)
439 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
441 afs_linux_fsync(struct file *fp, struct dentry *dp)
445 struct inode *ip = FILE_INODE(fp);
446 cred_t *credp = crref();
448 #ifdef AFS_LINUX24_ENV
452 code = afs_fsync(VTOAFS(ip), credp);
454 #ifdef AFS_LINUX24_ENV
464 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
467 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
468 cred_t *credp = crref();
469 struct AFS_FLOCK flock;
470 /* Convert to a lock format afs_lockctl understands. */
471 memset((char *)&flock, 0, sizeof(flock));
472 flock.l_type = flp->fl_type;
473 flock.l_pid = flp->fl_pid;
475 flock.l_start = flp->fl_start;
476 flock.l_len = flp->fl_end - flp->fl_start;
478 /* Safe because there are no large files, yet */
479 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
480 if (cmd == F_GETLK64)
482 else if (cmd == F_SETLK64)
484 else if (cmd == F_SETLKW64)
486 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
489 code = afs_lockctl(vcp, &flock, cmd, credp);
492 #ifdef AFS_LINUX24_ENV
493 if (code == 0 && (cmd == F_SETLK || cmd == F_SETLKW)) {
494 #ifdef AFS_LINUX26_ENV
495 struct file_lock flp2;
497 flp2.fl_flags &=~ FL_SLEEP;
498 code = posix_lock_file(fp, &flp2);
500 code = posix_lock_file(fp, flp, 0);
502 osi_Assert(code != -EAGAIN); /* there should be no conflicts */
504 struct AFS_FLOCK flock2;
506 flock2.l_type = F_UNLCK;
508 afs_lockctl(vcp, &flock2, F_SETLK, credp);
513 /* Convert flock back to Linux's file_lock */
514 flp->fl_type = flock.l_type;
515 flp->fl_pid = flock.l_pid;
516 flp->fl_start = flock.l_start;
517 flp->fl_end = flock.l_start + flock.l_len;
525 * essentially the same as afs_fsync() but we need to get the return
526 * code for the sys_close() here, not afs_linux_release(), so call
527 * afs_StoreAllSegments() with AFS_LASTSTORE
530 afs_linux_flush(struct file *fp)
532 struct vrequest treq;
533 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
534 cred_t *credp = crref();
539 code = afs_InitReq(&treq, credp);
543 ObtainSharedLock(&vcp->lock, 535);
544 if (vcp->execsOrWriters > 0) {
545 UpgradeSToWLock(&vcp->lock, 536);
546 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
547 ConvertWToSLock(&vcp->lock);
549 code = afs_CheckCode(code, &treq, 54);
550 ReleaseSharedLock(&vcp->lock);
559 #if !defined(AFS_LINUX24_ENV)
560 /* Not allowed to directly read a directory. */
562 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
570 struct file_operations afs_dir_fops = {
571 #if !defined(AFS_LINUX24_ENV)
572 .read = afs_linux_dir_read,
573 .lock = afs_linux_lock,
574 .fsync = afs_linux_fsync,
576 .read = generic_read_dir,
578 .readdir = afs_linux_readdir,
579 #ifdef HAVE_UNLOCKED_IOCTL
580 .unlocked_ioctl = afs_unlocked_xioctl,
584 #ifdef HAVE_COMPAT_IOCTL
585 .compat_ioctl = afs_unlocked_xioctl,
587 .open = afs_linux_open,
588 .release = afs_linux_release,
591 struct file_operations afs_file_fops = {
592 .read = afs_linux_read,
593 .write = afs_linux_write,
594 #ifdef GENERIC_FILE_AIO_READ
595 .aio_read = generic_file_aio_read,
596 .aio_write = generic_file_aio_write,
598 #ifdef HAVE_UNLOCKED_IOCTL
599 .unlocked_ioctl = afs_unlocked_xioctl,
603 #ifdef HAVE_COMPAT_IOCTL
604 .compat_ioctl = afs_unlocked_xioctl,
606 .mmap = afs_linux_mmap,
607 .open = afs_linux_open,
608 .flush = afs_linux_flush,
609 #ifdef AFS_LINUX26_ENV
610 .sendfile = generic_file_sendfile,
612 .release = afs_linux_release,
613 .fsync = afs_linux_fsync,
614 .lock = afs_linux_lock,
618 /**********************************************************************
619 * AFS Linux dentry operations
620 **********************************************************************/
622 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
623 * that has its mvid (parent dir's fid) pointer set to the wrong directory
624 * due to being mounted in multiple points at once. If so, check_bad_parent()
625 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
626 * dotdotfid and mtpoint fid members.
628 * dp - dentry to be checked.
632 * This dentry's vcache's mvid will be set to the correct parent directory's
634 * This root vnode's volume will have its dotdotfid and mtpoint fids set
635 * to the correct parent and mountpoint fids.
639 check_bad_parent(struct dentry *dp)
642 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
643 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
645 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
648 /* force a lookup, so vcp->mvid is fixed up */
649 afs_lookup(pvc, dp->d_name.name, &avc, credp);
650 if (!avc || vcp != avc) { /* bad, very bad.. */
651 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
652 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
653 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
654 ICL_TYPE_POINTER, dp);
657 AFS_RELE(AFSTOV(avc));
664 /* afs_linux_revalidate
665 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
668 afs_linux_revalidate(struct dentry *dp)
671 struct vcache *vcp = VTOAFS(dp->d_inode);
675 #ifdef AFS_LINUX24_ENV
681 /* Make this a fast path (no crref), since it's called so often. */
682 if (vcp->states & CStatd) {
684 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
685 check_bad_parent(dp); /* check and correct mvid */
688 #ifdef AFS_LINUX24_ENV
696 code = afs_getattr(vcp, &vattr, credp);
698 vattr2inode(AFSTOV(vcp), &vattr);
701 #ifdef AFS_LINUX24_ENV
709 #if defined(AFS_LINUX26_ENV)
711 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
713 int err = afs_linux_revalidate(dentry);
715 generic_fillattr(dentry->d_inode, stat);
721 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
722 * In kernels 2.2.10 and above, we are passed an additional flags var which
723 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
724 * we are advised to follow the entry if it is a link or to make sure that
725 * it is a directory. But since the kernel itself checks these possibilities
726 * later on, we shouldn't have to do it until later. Perhaps in the future..
729 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
730 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
731 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
733 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
736 afs_linux_dentry_revalidate(struct dentry *dp)
740 cred_t *credp = NULL;
741 struct vcache *vcp, *pvcp, *tvc = NULL;
744 #ifdef AFS_LINUX24_ENV
751 vcp = VTOAFS(dp->d_inode);
752 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
754 if (vcp == afs_globalVp)
757 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
758 check_bad_parent(dp); /* check and correct mvid */
761 /* If the last looker changes, we should make sure the current
762 * looker still has permission to examine this file. This would
763 * always require a crref() which would be "slow".
765 if (vcp->last_looker != treq.uid) {
766 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
769 vcp->last_looker = treq.uid;
773 /* If the parent's DataVersion has changed or the vnode
774 * is longer valid, we need to do a full lookup. VerifyVCache
775 * isn't enough since the vnode may have been renamed.
778 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
781 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
782 if (!tvc || tvc != vcp)
785 if (afs_getattr(vcp, &vattr, credp))
788 vattr2inode(AFSTOV(vcp), &vattr);
789 dp->d_time = hgetlo(pvcp->m.DataVersion);
792 /* should we always update the attributes at this point? */
793 /* unlikely--the vcache entry hasn't changed */
797 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
798 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
802 /* No change in parent's DataVersion so this negative
803 * lookup is still valid. BUT, if a server is down a
804 * negative lookup can result so there should be a
805 * liftime as well. For now, always expire.
823 shrink_dcache_parent(dp);
826 #ifdef AFS_LINUX24_ENV
837 afs_dentry_iput(struct dentry *dp, struct inode *ip)
839 struct vcache *vcp = VTOAFS(ip);
842 (void) afs_InactiveVCache(vcp, NULL);
849 afs_dentry_delete(struct dentry *dp)
851 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
852 return 1; /* bad inode? */
857 struct dentry_operations afs_dentry_operations = {
858 .d_revalidate = afs_linux_dentry_revalidate,
859 .d_delete = afs_dentry_delete,
860 .d_iput = afs_dentry_iput,
863 /**********************************************************************
864 * AFS Linux inode operations
865 **********************************************************************/
869 * Merely need to set enough of vattr to get us through the create. Note
870 * that the higher level code (open_namei) will take care of any tuncation
871 * explicitly. Exclusive open is also taken care of in open_namei.
873 * name is in kernel space at this point.
876 #ifdef IOP_CREATE_TAKES_NAMEIDATA
877 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
878 struct nameidata *nd)
880 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
884 cred_t *credp = crref();
885 const char *name = dp->d_name.name;
890 vattr.va_mode = mode;
891 vattr.va_type = mode & S_IFMT;
893 #if defined(AFS_LINUX26_ENV)
897 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
901 struct inode *ip = AFSTOV(vcp);
903 afs_getattr(vcp, &vattr, credp);
904 afs_fill_inode(ip, &vattr);
905 dp->d_op = &afs_dentry_operations;
906 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
907 d_instantiate(dp, ip);
911 #if defined(AFS_LINUX26_ENV)
918 /* afs_linux_lookup */
919 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
920 static struct dentry *
921 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
922 afs_linux_lookup(struct inode *dip, struct dentry *dp,
923 struct nameidata *nd)
925 afs_linux_lookup(struct inode *dip, struct dentry *dp)
929 afs_linux_lookup(struct inode *dip, struct dentry *dp)
932 cred_t *credp = crref();
933 struct vcache *vcp = NULL;
934 const char *comp = dp->d_name.name;
935 struct inode *ip = NULL;
936 #if defined(AFS_LINUX26_ENV)
937 struct dentry *newdp = NULL;
941 #if defined(AFS_LINUX26_ENV)
945 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
951 afs_getattr(vcp, &vattr, credp);
952 afs_fill_inode(ip, &vattr);
954 dp->d_op = &afs_dentry_operations;
955 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
958 #if defined(AFS_LINUX24_ENV)
959 if (ip && S_ISDIR(ip->i_mode)) {
960 struct dentry *alias;
962 /* Try to invalidate an existing alias in favor of our new one */
963 alias = d_find_alias(ip);
964 #if defined(AFS_LINUX26_ENV)
965 /* But not if it's disconnected; then we want d_splice_alias below */
966 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
970 if (d_invalidate(alias) == 0) {
974 #if defined(AFS_LINUX26_ENV)
982 #if defined(AFS_LINUX26_ENV)
983 newdp = d_splice_alias(ip, dp);
988 #if defined(AFS_LINUX26_ENV)
993 /* It's ok for the file to not be found. That's noted by the caller by
994 * seeing that the dp->d_inode field is NULL.
996 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
997 #if defined(AFS_LINUX26_ENV)
998 if (!code || code == ENOENT)
1005 return ERR_PTR(-code);
1014 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1017 cred_t *credp = crref();
1018 const char *name = newdp->d_name.name;
1019 struct inode *oldip = olddp->d_inode;
1021 /* If afs_link returned the vnode, we could instantiate the
1022 * dentry. Since it's not, we drop this one and do a new lookup.
1027 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1035 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1038 cred_t *credp = crref();
1039 const char *name = dp->d_name.name;
1040 struct vcache *tvc = VTOAFS(dp->d_inode);
1042 #if defined(AFS_LINUX26_ENV)
1045 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1046 && !(tvc->states & CUnlinked)) {
1047 struct dentry *__dp;
1049 extern char *afs_newname();
1058 osi_FreeSmallSpace(__name);
1059 __name = afs_newname();
1062 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1066 } while (__dp->d_inode != NULL);
1069 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1071 tvc->mvid = (void *) __name;
1074 crfree(tvc->uncred);
1076 tvc->uncred = credp;
1077 tvc->states |= CUnlinked;
1082 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1091 code = afs_remove(VTOAFS(dip), name, credp);
1096 #if defined(AFS_LINUX26_ENV)
1105 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1108 cred_t *credp = crref();
1110 const char *name = dp->d_name.name;
1112 /* If afs_symlink returned the vnode, we could instantiate the
1113 * dentry. Since it's not, we drop this one and do a new lookup.
1119 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1126 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1129 cred_t *credp = crref();
1130 struct vcache *tvcp = NULL;
1132 const char *name = dp->d_name.name;
1134 #if defined(AFS_LINUX26_ENV)
1138 vattr.va_mask = ATTR_MODE;
1139 vattr.va_mode = mode;
1141 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1144 struct inode *ip = AFSTOV(tvcp);
1146 afs_getattr(tvcp, &vattr, credp);
1147 afs_fill_inode(ip, &vattr);
1149 dp->d_op = &afs_dentry_operations;
1150 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1151 d_instantiate(dp, ip);
1155 #if defined(AFS_LINUX26_ENV)
1163 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1166 cred_t *credp = crref();
1167 const char *name = dp->d_name.name;
1169 /* locking kernel conflicts with glock? */
1172 code = afs_rmdir(VTOAFS(dip), name, credp);
1175 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1176 * that failed because a directory is not empty. So, we map
1177 * EEXIST to ENOTEMPTY on linux.
1179 if (code == EEXIST) {
1193 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1194 struct inode *newip, struct dentry *newdp)
1197 cred_t *credp = crref();
1198 const char *oldname = olddp->d_name.name;
1199 const char *newname = newdp->d_name.name;
1200 struct dentry *rehash = NULL;
1202 #if defined(AFS_LINUX26_ENV)
1203 /* Prevent any new references during rename operation. */
1206 /* Remove old and new entries from name hash. New one will change below.
1207 * While it's optimal to catch failures and re-insert newdp into hash,
1208 * it's also error prone and in that case we're already dealing with error
1209 * cases. Let another lookup put things right, if need be.
1211 #if defined(AFS_LINUX26_ENV)
1212 if (!d_unhashed(newdp)) {
1217 if (!list_empty(&newdp->d_hash)) {
1223 #if defined(AFS_LINUX24_ENV)
1224 if (atomic_read(&olddp->d_count) > 1)
1225 shrink_dcache_parent(olddp);
1229 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1235 #if defined(AFS_LINUX26_ENV)
1244 /* afs_linux_ireadlink
1245 * Internal readlink which can return link contents to user or kernel space.
1246 * Note that the buffer is NOT supposed to be null-terminated.
1249 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1252 cred_t *credp = crref();
1256 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1257 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1261 return maxlen - tuio.uio_resid;
1266 #if !defined(AFS_LINUX24_ENV)
1267 /* afs_linux_readlink
1268 * Fill target (which is in user space) with contents of symlink.
1271 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1274 struct inode *ip = dp->d_inode;
1277 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1283 /* afs_linux_follow_link
1284 * a file system dependent link following routine.
1286 static struct dentry *
1287 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1288 unsigned int follow)
1296 name = osi_Alloc(PATH_MAX + 1);
1300 return ERR_PTR(-EIO);
1303 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1308 res = ERR_PTR(code);
1311 res = lookup_dentry(name, basep, follow);
1315 osi_Free(name, PATH_MAX + 1);
1321 /* afs_linux_readpage
1322 * all reads come through here. A strategy-like read call.
1325 afs_linux_readpage(struct file *fp, struct page *pp)
1328 cred_t *credp = crref();
1329 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1331 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1333 ulong address = afs_linux_page_address(pp);
1334 afs_offs_t offset = pageoff(pp);
1338 struct inode *ip = FILE_INODE(fp);
1339 int cnt = page_count(pp);
1340 struct vcache *avc = VTOAFS(ip);
1343 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1347 atomic_add(1, &pp->count);
1348 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1349 clear_bit(PG_error, &pp->flags);
1352 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1354 #ifdef AFS_LINUX24_ENV
1358 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 */
1359 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1360 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1361 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1364 #ifdef AFS_LINUX24_ENV
1369 if (tuio.uio_resid) /* zero remainder of page */
1370 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1372 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1373 flush_dcache_page(pp);
1374 SetPageUptodate(pp);
1376 set_bit(PG_uptodate, &pp->flags);
1380 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1384 clear_bit(PG_locked, &pp->flags);
1389 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1391 struct vrequest treq;
1394 code = afs_InitReq(&treq, credp);
1395 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1396 tdc = afs_FindDCache(avc, offset);
1398 if (!(tdc->mflags & DFNextStarted))
1399 afs_PrefetchChunk(avc, tdc, credp, &treq);
1402 ReleaseWriteLock(&avc->lock);
1412 #if defined(AFS_LINUX24_ENV)
1414 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1415 unsigned long offset, unsigned int count)
1417 struct vcache *vcp = VTOAFS(ip);
1426 buffer = kmap(pp) + offset;
1427 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1432 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1433 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1434 ICL_TYPE_INT32, 99999);
1436 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1438 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1440 ip->i_size = vcp->m.Length;
1441 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1444 struct vrequest treq;
1446 ObtainWriteLock(&vcp->lock, 533);
1447 if (!afs_InitReq(&treq, credp))
1448 code = afs_DoPartialWrite(vcp, &treq);
1449 ReleaseWriteLock(&vcp->lock);
1451 code = code ? -code : count - tuio.uio_resid;
1453 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1454 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1455 ICL_TYPE_INT32, code);
1467 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1468 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1470 afs_linux_writepage(struct page *pp)
1473 struct address_space *mapping = pp->mapping;
1474 struct inode *inode;
1475 unsigned long end_index;
1476 unsigned offset = PAGE_CACHE_SIZE;
1479 #if defined(AFS_LINUX26_ENV)
1480 if (PageReclaim(pp)) {
1481 # if defined(WRITEPAGE_ACTIVATE)
1482 return WRITEPAGE_ACTIVATE;
1484 return AOP_WRITEPAGE_ACTIVATE;
1488 if (PageLaunder(pp)) {
1489 return(fail_writepage(pp));
1493 inode = (struct inode *)mapping->host;
1494 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1497 if (pp->index < end_index)
1499 /* things got complicated... */
1500 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1501 /* OK, are we completely out? */
1502 if (pp->index >= end_index + 1 || !offset)
1505 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1506 SetPageUptodate(pp);
1508 if (status == offset)
1514 /* afs_linux_updatepage
1515 * What one would have thought was writepage - write dirty page to file.
1516 * Called from generic_file_write. buffer is still in user space. pagep
1517 * has been filled in with old data if we're updating less than a page.
1520 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1521 unsigned int count, int sync)
1523 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1524 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1530 set_bit(PG_locked, &pp->flags);
1534 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1535 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1536 ICL_TYPE_INT32, 99999);
1537 setup_uio(&tuio, &iovec, page_addr + offset,
1538 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1541 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1543 ip->i_size = vcp->m.Length;
1544 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1547 struct vrequest treq;
1549 ObtainWriteLock(&vcp->lock, 533);
1550 vcp->m.Date = osi_Time(); /* set modification time */
1551 if (!afs_InitReq(&treq, credp))
1552 code = afs_DoPartialWrite(vcp, &treq);
1553 ReleaseWriteLock(&vcp->lock);
1556 code = code ? -code : count - tuio.uio_resid;
1557 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1558 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1559 ICL_TYPE_INT32, code);
1564 clear_bit(PG_locked, &pp->flags);
1569 /* afs_linux_permission
1570 * Check access rights - returns error if can't check or permission denied.
1573 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1574 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1576 afs_linux_permission(struct inode *ip, int mode)
1580 cred_t *credp = crref();
1584 if (mode & MAY_EXEC)
1586 if (mode & MAY_READ)
1588 if (mode & MAY_WRITE)
1590 code = afs_access(VTOAFS(ip), tmp, credp);
1597 #if defined(AFS_LINUX24_ENV)
1599 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1604 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1605 offset, to - offset);
1606 #if !defined(AFS_LINUX26_ENV)
1614 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1617 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1618 call kmap directly instead of relying on us to do it */
1619 #if !defined(AFS_LINUX26_ENV)
1625 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1628 static struct inode_operations afs_file_iops = {
1629 #if defined(AFS_LINUX26_ENV)
1630 .permission = afs_linux_permission,
1631 .getattr = afs_linux_getattr,
1632 .setattr = afs_notify_change,
1633 #elif defined(AFS_LINUX24_ENV)
1634 .permission = afs_linux_permission,
1635 .revalidate = afs_linux_revalidate,
1636 .setattr = afs_notify_change,
1638 .default_file_ops = &afs_file_fops,
1639 .readpage = afs_linux_readpage,
1640 .revalidate = afs_linux_revalidate,
1641 .updatepage = afs_linux_updatepage,
1645 #if defined(AFS_LINUX24_ENV)
1646 static struct address_space_operations afs_file_aops = {
1647 .readpage = afs_linux_readpage,
1648 .writepage = afs_linux_writepage,
1649 .commit_write = afs_linux_commit_write,
1650 .prepare_write = afs_linux_prepare_write,
1655 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1656 * by what sort of operation is allowed.....
1659 static struct inode_operations afs_dir_iops = {
1660 #if !defined(AFS_LINUX24_ENV)
1661 .default_file_ops = &afs_dir_fops,
1663 .setattr = afs_notify_change,
1665 .create = afs_linux_create,
1666 .lookup = afs_linux_lookup,
1667 .link = afs_linux_link,
1668 .unlink = afs_linux_unlink,
1669 .symlink = afs_linux_symlink,
1670 .mkdir = afs_linux_mkdir,
1671 .rmdir = afs_linux_rmdir,
1672 .rename = afs_linux_rename,
1673 #if defined(AFS_LINUX26_ENV)
1674 .getattr = afs_linux_getattr,
1676 .revalidate = afs_linux_revalidate,
1678 .permission = afs_linux_permission,
1681 /* We really need a separate symlink set of ops, since do_follow_link()
1682 * determines if it _is_ a link by checking if the follow_link op is set.
1684 #if defined(AFS_LINUX24_ENV)
1686 afs_symlink_filler(struct file *file, struct page *page)
1688 struct inode *ip = (struct inode *)page->mapping->host;
1689 char *p = (char *)kmap(page);
1694 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1699 p[code] = '\0'; /* null terminate? */
1702 SetPageUptodate(page);
1716 static struct address_space_operations afs_symlink_aops = {
1717 .readpage = afs_symlink_filler
1721 static struct inode_operations afs_symlink_iops = {
1722 #if defined(AFS_LINUX24_ENV)
1723 .readlink = page_readlink,
1724 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1725 .follow_link = page_follow_link,
1727 .follow_link = page_follow_link_light,
1728 .put_link = page_put_link,
1730 .setattr = afs_notify_change,
1732 .readlink = afs_linux_readlink,
1733 .follow_link = afs_linux_follow_link,
1734 .permission = afs_linux_permission,
1735 .revalidate = afs_linux_revalidate,
1740 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1744 vattr2inode(ip, vattr);
1746 /* Reset ops if symlink or directory. */
1747 if (S_ISREG(ip->i_mode)) {
1748 ip->i_op = &afs_file_iops;
1749 #if defined(AFS_LINUX24_ENV)
1750 ip->i_fop = &afs_file_fops;
1751 ip->i_data.a_ops = &afs_file_aops;
1754 } else if (S_ISDIR(ip->i_mode)) {
1755 ip->i_op = &afs_dir_iops;
1756 #if defined(AFS_LINUX24_ENV)
1757 ip->i_fop = &afs_dir_fops;
1760 } else if (S_ISLNK(ip->i_mode)) {
1761 ip->i_op = &afs_symlink_iops;
1762 #if defined(AFS_LINUX24_ENV)
1763 ip->i_data.a_ops = &afs_symlink_aops;
1764 ip->i_mapping = &ip->i_data;
1768 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */