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)
102 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
103 struct vrequest treq;
104 cred_t *credp = crref();
108 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
109 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
110 (fp->f_flags & O_APPEND) ? 99998 : 99999);
113 /* get a validated vcache entry */
114 code = (ssize_t) afs_InitReq(&treq, credp);
116 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
118 ObtainWriteLock(&vcp->lock, 529);
120 ReleaseWriteLock(&vcp->lock);
126 code = do_sync_write(fp, buf, count, offp);
128 code = generic_file_write(fp, buf, count, offp);
133 ObtainWriteLock(&vcp->lock, 530);
134 afs_FakeClose(vcp, credp);
135 ReleaseWriteLock(&vcp->lock);
137 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
138 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
146 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
148 /* This is a complete rewrite of afs_readdir, since we can make use of
149 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
150 * handling and use of bulkstats will need to be reflected here as well.
153 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
155 extern struct DirEntry *afs_dir_GetBlob();
156 struct vcache *avc = VTOAFS(FILE_INODE(fp));
157 struct vrequest treq;
158 register struct dcache *tdc;
165 afs_size_t origOffset, tlen;
166 cred_t *credp = crref();
167 struct afs_fakestat_state fakestat;
169 #if defined(AFS_LINUX26_ENV)
173 AFS_STATCNT(afs_readdir);
175 code = afs_InitReq(&treq, credp);
180 afs_InitFakeStat(&fakestat);
181 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
185 /* update the cache entry */
187 code = afs_VerifyVCache(avc, &treq);
191 /* get a reference to the entire directory */
192 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
198 ObtainSharedLock(&avc->lock, 810);
199 UpgradeSToWLock(&avc->lock, 811);
200 ObtainReadLock(&tdc->lock);
202 * Make sure that the data in the cache is current. There are two
203 * cases we need to worry about:
204 * 1. The cache data is being fetched by another process.
205 * 2. The cache data is no longer valid
207 while ((avc->states & CStatd)
208 && (tdc->dflags & DFFetching)
209 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
210 ReleaseReadLock(&tdc->lock);
211 ReleaseSharedLock(&avc->lock);
212 afs_osi_Sleep(&tdc->validPos);
213 ObtainSharedLock(&avc->lock, 812);
214 ObtainReadLock(&tdc->lock);
216 if (!(avc->states & CStatd)
217 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
218 ReleaseReadLock(&tdc->lock);
219 ReleaseSharedLock(&avc->lock);
224 /* Set the readdir-in-progress flag, and downgrade the lock
225 * to shared so others will be able to acquire a read lock.
227 avc->states |= CReadDir;
228 avc->dcreaddir = tdc;
229 avc->readdir_pid = MyPidxx;
230 ConvertWToSLock(&avc->lock);
232 /* Fill in until we get an error or we're done. This implementation
233 * takes an offset in units of blobs, rather than bytes.
236 offset = (int) fp->f_pos;
238 dirpos = BlobScan(tdc, offset);
242 de = afs_dir_GetBlob(tdc, dirpos);
246 ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
249 len = strlen(de->name);
251 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
252 (unsigned long)&tdc->f.inode, dirpos);
253 DRelease((struct buffer *) de, 0);
254 ReleaseSharedLock(&avc->lock);
260 /* filldir returns -EINVAL when the buffer is full. */
261 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
263 unsigned int type = DT_UNKNOWN;
264 struct VenusFid afid;
267 afid.Cell = avc->fid.Cell;
268 afid.Fid.Volume = avc->fid.Fid.Volume;
269 afid.Fid.Vnode = ntohl(de->fid.vnode);
270 afid.Fid.Unique = ntohl(de->fid.vunique);
271 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
273 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
276 } else if (((tvc->states) & (CStatd | CTruth))) {
277 /* CTruth will be set if the object has
282 else if (vtype == VREG)
284 /* Don't do this until we're sure it can't be a mtpt */
285 /* else if (vtype == VLNK)
287 /* what other types does AFS support? */
289 /* clean up from afs_FindVCache */
293 * If this is NFS readdirplus, then the filler is going to
294 * call getattr on this inode, which will deadlock if we're
298 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
302 code = (*filldir) (dirbuf, de->name, len, offset, ino);
304 DRelease((struct buffer *)de, 0);
307 offset = dirpos + 1 + ((len + 16) >> 5);
309 /* If filldir didn't fill in the last one this is still pointing to that
312 fp->f_pos = (loff_t) offset;
314 ReleaseReadLock(&tdc->lock);
316 UpgradeSToWLock(&avc->lock, 813);
317 avc->states &= ~CReadDir;
319 avc->readdir_pid = 0;
320 ReleaseSharedLock(&avc->lock);
324 afs_PutFakeStat(&fakestat);
327 #if defined(AFS_LINUX26_ENV)
334 /* in afs_pioctl.c */
335 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
338 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
339 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
341 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
348 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
350 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
351 cred_t *credp = crref();
352 struct vrequest treq;
356 #if defined(AFS_LINUX24_ENV)
357 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
358 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
359 vmap->vm_end - vmap->vm_start);
361 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
362 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
363 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
367 /* get a validated vcache entry */
368 code = afs_InitReq(&treq, credp);
372 code = afs_VerifyVCache(vcp, &treq);
376 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
379 code = generic_file_mmap(fp, vmap);
382 vcp->states |= CMAPPED;
395 afs_linux_open(struct inode *ip, struct file *fp)
397 struct vcache *vcp = VTOAFS(ip);
398 cred_t *credp = crref();
401 #ifdef AFS_LINUX24_ENV
405 code = afs_open(&vcp, fp->f_flags, credp);
407 #ifdef AFS_LINUX24_ENV
416 afs_linux_release(struct inode *ip, struct file *fp)
418 struct vcache *vcp = VTOAFS(ip);
419 cred_t *credp = crref();
422 #ifdef AFS_LINUX24_ENV
426 code = afs_close(vcp, fp->f_flags, credp);
428 #ifdef AFS_LINUX24_ENV
437 #if defined(AFS_LINUX24_ENV)
438 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
440 afs_linux_fsync(struct file *fp, struct dentry *dp)
444 struct inode *ip = FILE_INODE(fp);
445 cred_t *credp = crref();
447 #ifdef AFS_LINUX24_ENV
451 code = afs_fsync(VTOAFS(ip), credp);
453 #ifdef AFS_LINUX24_ENV
463 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
466 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
467 cred_t *credp = crref();
468 struct AFS_FLOCK flock;
469 /* Convert to a lock format afs_lockctl understands. */
470 memset((char *)&flock, 0, sizeof(flock));
471 flock.l_type = flp->fl_type;
472 flock.l_pid = flp->fl_pid;
474 flock.l_start = flp->fl_start;
475 flock.l_len = flp->fl_end - flp->fl_start;
477 /* Safe because there are no large files, yet */
478 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
479 if (cmd == F_GETLK64)
481 else if (cmd == F_SETLK64)
483 else if (cmd == F_SETLKW64)
485 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
488 code = afs_lockctl(vcp, &flock, cmd, credp);
491 #ifdef AFS_LINUX24_ENV
492 if ((code == 0 || flp->fl_type == F_UNLCK) &&
493 (cmd == F_SETLK || cmd == F_SETLKW)) {
494 #ifdef POSIX_LOCK_FILE_WAIT_ARG
495 code = posix_lock_file(fp, flp, 0);
497 flp->fl_flags &=~ FL_SLEEP;
498 code = posix_lock_file(fp, flp);
500 if (code && flp->fl_type != F_UNLCK) {
501 struct AFS_FLOCK flock2;
503 flock2.l_type = F_UNLCK;
505 afs_lockctl(vcp, &flock2, F_SETLK, credp);
510 /* Convert flock back to Linux's file_lock */
511 flp->fl_type = flock.l_type;
512 flp->fl_pid = flock.l_pid;
513 flp->fl_start = flock.l_start;
514 flp->fl_end = flock.l_start + flock.l_len;
521 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
523 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
525 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
526 cred_t *credp = crref();
527 struct AFS_FLOCK flock;
528 /* Convert to a lock format afs_lockctl understands. */
529 memset((char *)&flock, 0, sizeof(flock));
530 flock.l_type = flp->fl_type;
531 flock.l_pid = flp->fl_pid;
534 flock.l_len = OFFSET_MAX;
536 /* Safe because there are no large files, yet */
537 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
538 if (cmd == F_GETLK64)
540 else if (cmd == F_SETLK64)
542 else if (cmd == F_SETLKW64)
544 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
547 code = afs_lockctl(vcp, &flock, cmd, credp);
550 if ((code == 0 || flp->fl_type == F_UNLCK) &&
551 (cmd == F_SETLK || cmd == F_SETLKW)) {
552 flp->fl_flags &=~ FL_SLEEP;
553 code = flock_lock_file_wait(fp, flp);
554 if (code && flp->fl_type != F_UNLCK) {
555 struct AFS_FLOCK flock2;
557 flock2.l_type = F_UNLCK;
559 afs_lockctl(vcp, &flock2, F_SETLK, credp);
563 /* Convert flock back to Linux's file_lock */
564 flp->fl_type = flock.l_type;
565 flp->fl_pid = flock.l_pid;
573 * essentially the same as afs_fsync() but we need to get the return
574 * code for the sys_close() here, not afs_linux_release(), so call
575 * afs_StoreAllSegments() with AFS_LASTSTORE
578 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
579 afs_linux_flush(struct file *fp, fl_owner_t id)
581 afs_linux_flush(struct file *fp)
584 struct vrequest treq;
591 if (fp->f_flags | O_RDONLY) /* readers dont flush */
595 vcp = VTOAFS(FILE_INODE(fp));
597 code = afs_InitReq(&treq, credp);
601 ObtainSharedLock(&vcp->lock, 535);
602 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
603 UpgradeSToWLock(&vcp->lock, 536);
604 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
605 ConvertWToSLock(&vcp->lock);
607 code = afs_CheckCode(code, &treq, 54);
608 ReleaseSharedLock(&vcp->lock);
617 #if !defined(AFS_LINUX24_ENV)
618 /* Not allowed to directly read a directory. */
620 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
628 struct file_operations afs_dir_fops = {
629 #if !defined(AFS_LINUX24_ENV)
630 .read = afs_linux_dir_read,
631 .lock = afs_linux_lock,
632 .fsync = afs_linux_fsync,
634 .read = generic_read_dir,
636 .readdir = afs_linux_readdir,
637 #ifdef HAVE_UNLOCKED_IOCTL
638 .unlocked_ioctl = afs_unlocked_xioctl,
642 #ifdef HAVE_COMPAT_IOCTL
643 .compat_ioctl = afs_unlocked_xioctl,
645 .open = afs_linux_open,
646 .release = afs_linux_release,
649 struct file_operations afs_file_fops = {
650 .read = afs_linux_read,
651 .write = afs_linux_write,
652 #ifdef GENERIC_FILE_AIO_READ
653 .aio_read = generic_file_aio_read,
654 .aio_write = generic_file_aio_write,
656 #ifdef HAVE_UNLOCKED_IOCTL
657 .unlocked_ioctl = afs_unlocked_xioctl,
661 #ifdef HAVE_COMPAT_IOCTL
662 .compat_ioctl = afs_unlocked_xioctl,
664 .mmap = afs_linux_mmap,
665 .open = afs_linux_open,
666 .flush = afs_linux_flush,
667 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
668 .sendfile = generic_file_sendfile,
670 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
671 .splice_write = generic_file_splice_write,
672 .splice_read = generic_file_splice_read,
674 .release = afs_linux_release,
675 .fsync = afs_linux_fsync,
676 .lock = afs_linux_lock,
677 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
678 .flock = afs_linux_flock,
683 /**********************************************************************
684 * AFS Linux dentry operations
685 **********************************************************************/
687 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
688 * that has its mvid (parent dir's fid) pointer set to the wrong directory
689 * due to being mounted in multiple points at once. If so, check_bad_parent()
690 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
691 * dotdotfid and mtpoint fid members.
693 * dp - dentry to be checked.
697 * This dentry's vcache's mvid will be set to the correct parent directory's
699 * This root vnode's volume will have its dotdotfid and mtpoint fids set
700 * to the correct parent and mountpoint fids.
704 check_bad_parent(struct dentry *dp)
707 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
708 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
710 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
713 /* force a lookup, so vcp->mvid is fixed up */
714 afs_lookup(pvc, dp->d_name.name, &avc, credp);
715 if (!avc || vcp != avc) { /* bad, very bad.. */
716 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
717 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
718 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
719 ICL_TYPE_POINTER, dp);
722 AFS_RELE(AFSTOV(avc));
729 /* afs_linux_revalidate
730 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
733 afs_linux_revalidate(struct dentry *dp)
736 struct vcache *vcp = VTOAFS(dp->d_inode);
740 #ifdef AFS_LINUX24_ENV
746 /* Make this a fast path (no crref), since it's called so often. */
747 if (vcp->states & CStatd) {
749 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
750 check_bad_parent(dp); /* check and correct mvid */
753 #ifdef AFS_LINUX24_ENV
761 code = afs_getattr(vcp, &vattr, credp);
763 vattr2inode(AFSTOV(vcp), &vattr);
766 #ifdef AFS_LINUX24_ENV
774 #if defined(AFS_LINUX26_ENV)
776 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
778 int err = afs_linux_revalidate(dentry);
780 generic_fillattr(dentry->d_inode, stat);
786 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
787 * In kernels 2.2.10 and above, we are passed an additional flags var which
788 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
789 * we are advised to follow the entry if it is a link or to make sure that
790 * it is a directory. But since the kernel itself checks these possibilities
791 * later on, we shouldn't have to do it until later. Perhaps in the future..
794 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
795 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
796 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
798 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
801 afs_linux_dentry_revalidate(struct dentry *dp)
805 cred_t *credp = NULL;
806 struct vcache *vcp, *pvcp, *tvc = NULL;
809 #ifdef AFS_LINUX24_ENV
816 vcp = VTOAFS(dp->d_inode);
817 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
819 if (vcp == afs_globalVp)
822 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
823 check_bad_parent(dp); /* check and correct mvid */
826 /* If the last looker changes, we should make sure the current
827 * looker still has permission to examine this file. This would
828 * always require a crref() which would be "slow".
830 if (vcp->last_looker != treq.uid) {
831 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
834 vcp->last_looker = treq.uid;
838 /* If the parent's DataVersion has changed or the vnode
839 * is longer valid, we need to do a full lookup. VerifyVCache
840 * isn't enough since the vnode may have been renamed.
843 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
846 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
847 if (!tvc || tvc != vcp)
850 if (afs_getattr(vcp, &vattr, credp))
853 vattr2inode(AFSTOV(vcp), &vattr);
854 dp->d_time = hgetlo(pvcp->m.DataVersion);
857 /* should we always update the attributes at this point? */
858 /* unlikely--the vcache entry hasn't changed */
862 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
863 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
867 /* No change in parent's DataVersion so this negative
868 * lookup is still valid. BUT, if a server is down a
869 * negative lookup can result so there should be a
870 * liftime as well. For now, always expire.
888 shrink_dcache_parent(dp);
891 #ifdef AFS_LINUX24_ENV
902 afs_dentry_iput(struct dentry *dp, struct inode *ip)
904 struct vcache *vcp = VTOAFS(ip);
907 (void) afs_InactiveVCache(vcp, NULL);
909 #ifdef DCACHE_NFSFS_RENAMED
910 #ifdef AFS_LINUX26_ENV
911 spin_lock(&dp->d_lock);
913 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
914 #ifdef AFS_LINUX26_ENV
915 spin_unlock(&dp->d_lock);
923 afs_dentry_delete(struct dentry *dp)
925 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
926 return 1; /* bad inode? */
931 struct dentry_operations afs_dentry_operations = {
932 .d_revalidate = afs_linux_dentry_revalidate,
933 .d_delete = afs_dentry_delete,
934 .d_iput = afs_dentry_iput,
937 /**********************************************************************
938 * AFS Linux inode operations
939 **********************************************************************/
943 * Merely need to set enough of vattr to get us through the create. Note
944 * that the higher level code (open_namei) will take care of any tuncation
945 * explicitly. Exclusive open is also taken care of in open_namei.
947 * name is in kernel space at this point.
950 #ifdef IOP_CREATE_TAKES_NAMEIDATA
951 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
952 struct nameidata *nd)
954 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
958 cred_t *credp = crref();
959 const char *name = dp->d_name.name;
964 vattr.va_mode = mode;
965 vattr.va_type = mode & S_IFMT;
967 #if defined(AFS_LINUX26_ENV)
971 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
975 struct inode *ip = AFSTOV(vcp);
977 afs_getattr(vcp, &vattr, credp);
978 afs_fill_inode(ip, &vattr);
979 insert_inode_hash(ip);
980 dp->d_op = &afs_dentry_operations;
981 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
982 d_instantiate(dp, ip);
986 #if defined(AFS_LINUX26_ENV)
993 /* afs_linux_lookup */
994 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
995 static struct dentry *
996 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
997 afs_linux_lookup(struct inode *dip, struct dentry *dp,
998 struct nameidata *nd)
1000 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1004 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1007 cred_t *credp = crref();
1008 struct vcache *vcp = NULL;
1009 const char *comp = dp->d_name.name;
1010 struct inode *ip = NULL;
1011 #if defined(AFS_LINUX26_ENV)
1012 struct dentry *newdp = NULL;
1016 #if defined(AFS_LINUX26_ENV)
1020 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1026 afs_getattr(vcp, &vattr, credp);
1027 afs_fill_inode(ip, &vattr);
1028 if (hlist_unhashed(&ip->i_hash))
1029 insert_inode_hash(ip);
1031 dp->d_op = &afs_dentry_operations;
1032 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1035 #if defined(AFS_LINUX24_ENV)
1036 if (ip && S_ISDIR(ip->i_mode)) {
1037 struct dentry *alias;
1039 /* Try to invalidate an existing alias in favor of our new one */
1040 alias = d_find_alias(ip);
1041 #if defined(AFS_LINUX26_ENV)
1042 /* But not if it's disconnected; then we want d_splice_alias below */
1043 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1047 if (d_invalidate(alias) == 0) {
1051 #if defined(AFS_LINUX26_ENV)
1060 #if defined(AFS_LINUX26_ENV)
1061 newdp = d_splice_alias(ip, dp);
1066 #if defined(AFS_LINUX26_ENV)
1071 /* It's ok for the file to not be found. That's noted by the caller by
1072 * seeing that the dp->d_inode field is NULL.
1074 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1075 #if defined(AFS_LINUX26_ENV)
1076 if (!code || code == ENOENT)
1083 return ERR_PTR(-code);
1092 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1095 cred_t *credp = crref();
1096 const char *name = newdp->d_name.name;
1097 struct inode *oldip = olddp->d_inode;
1099 /* If afs_link returned the vnode, we could instantiate the
1100 * dentry. Since it's not, we drop this one and do a new lookup.
1105 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1113 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1116 cred_t *credp = crref();
1117 const char *name = dp->d_name.name;
1118 struct vcache *tvc = VTOAFS(dp->d_inode);
1120 #if defined(AFS_LINUX26_ENV)
1123 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1124 && !(tvc->states & CUnlinked)) {
1125 struct dentry *__dp;
1127 extern char *afs_newname();
1136 osi_FreeSmallSpace(__name);
1137 __name = afs_newname();
1140 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1144 } while (__dp->d_inode != NULL);
1147 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1149 tvc->mvid = (void *) __name;
1152 crfree(tvc->uncred);
1154 tvc->uncred = credp;
1155 tvc->states |= CUnlinked;
1156 #ifdef DCACHE_NFSFS_RENAMED
1157 #ifdef AFS_LINUX26_ENV
1158 spin_lock(&dp->d_lock);
1160 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1161 #ifdef AFS_LINUX26_ENV
1162 spin_unlock(&dp->d_lock);
1166 osi_FreeSmallSpace(__name);
1171 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1180 code = afs_remove(VTOAFS(dip), name, credp);
1185 #if defined(AFS_LINUX26_ENV)
1194 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1197 cred_t *credp = crref();
1199 const char *name = dp->d_name.name;
1201 /* If afs_symlink returned the vnode, we could instantiate the
1202 * dentry. Since it's not, we drop this one and do a new lookup.
1208 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1215 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1218 cred_t *credp = crref();
1219 struct vcache *tvcp = NULL;
1221 const char *name = dp->d_name.name;
1223 #if defined(AFS_LINUX26_ENV)
1227 vattr.va_mask = ATTR_MODE;
1228 vattr.va_mode = mode;
1230 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1233 struct inode *ip = AFSTOV(tvcp);
1235 afs_getattr(tvcp, &vattr, credp);
1236 afs_fill_inode(ip, &vattr);
1238 dp->d_op = &afs_dentry_operations;
1239 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1240 d_instantiate(dp, ip);
1244 #if defined(AFS_LINUX26_ENV)
1252 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1255 cred_t *credp = crref();
1256 const char *name = dp->d_name.name;
1258 /* locking kernel conflicts with glock? */
1261 code = afs_rmdir(VTOAFS(dip), name, credp);
1264 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1265 * that failed because a directory is not empty. So, we map
1266 * EEXIST to ENOTEMPTY on linux.
1268 if (code == EEXIST) {
1282 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1283 struct inode *newip, struct dentry *newdp)
1286 cred_t *credp = crref();
1287 const char *oldname = olddp->d_name.name;
1288 const char *newname = newdp->d_name.name;
1289 struct dentry *rehash = NULL;
1291 #if defined(AFS_LINUX26_ENV)
1292 /* Prevent any new references during rename operation. */
1295 /* Remove old and new entries from name hash. New one will change below.
1296 * While it's optimal to catch failures and re-insert newdp into hash,
1297 * it's also error prone and in that case we're already dealing with error
1298 * cases. Let another lookup put things right, if need be.
1300 #if defined(AFS_LINUX26_ENV)
1301 if (!d_unhashed(newdp)) {
1306 if (!list_empty(&newdp->d_hash)) {
1312 #if defined(AFS_LINUX24_ENV)
1313 if (atomic_read(&olddp->d_count) > 1)
1314 shrink_dcache_parent(olddp);
1318 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1324 #if defined(AFS_LINUX26_ENV)
1333 /* afs_linux_ireadlink
1334 * Internal readlink which can return link contents to user or kernel space.
1335 * Note that the buffer is NOT supposed to be null-terminated.
1338 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1341 cred_t *credp = crref();
1345 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1346 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1350 return maxlen - tuio.uio_resid;
1355 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1356 /* afs_linux_readlink
1357 * Fill target (which is in user space) with contents of symlink.
1360 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1363 struct inode *ip = dp->d_inode;
1366 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1372 /* afs_linux_follow_link
1373 * a file system dependent link following routine.
1375 #if defined(AFS_LINUX24_ENV)
1376 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1381 name = osi_Alloc(PATH_MAX);
1387 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1395 code = vfs_follow_link(nd, name);
1398 osi_Free(name, PATH_MAX);
1403 #else /* !defined(AFS_LINUX24_ENV) */
1405 static struct dentry *
1406 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1407 unsigned int follow)
1415 name = osi_Alloc(PATH_MAX + 1);
1419 return ERR_PTR(-EIO);
1422 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1427 res = ERR_PTR(code);
1430 res = lookup_dentry(name, basep, follow);
1434 osi_Free(name, PATH_MAX + 1);
1438 #endif /* AFS_LINUX24_ENV */
1439 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1441 /* afs_linux_readpage
1442 * all reads come through here. A strategy-like read call.
1445 afs_linux_readpage(struct file *fp, struct page *pp)
1448 cred_t *credp = crref();
1449 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1451 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1453 ulong address = afs_linux_page_address(pp);
1454 afs_offs_t offset = pageoff(pp);
1458 struct inode *ip = FILE_INODE(fp);
1459 int cnt = page_count(pp);
1460 struct vcache *avc = VTOAFS(ip);
1463 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1467 atomic_add(1, &pp->count);
1468 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1469 clear_bit(PG_error, &pp->flags);
1472 setup_uio(&tuio, &iovec, (char *)address, offset, PAGE_SIZE, UIO_READ,
1474 #ifdef AFS_LINUX24_ENV
1478 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 */
1479 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1480 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1481 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1484 #ifdef AFS_LINUX24_ENV
1489 if (tuio.uio_resid) /* zero remainder of page */
1490 memset((void *)(address + (PAGE_SIZE - tuio.uio_resid)), 0,
1492 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1493 flush_dcache_page(pp);
1494 SetPageUptodate(pp);
1496 set_bit(PG_uptodate, &pp->flags);
1500 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1504 clear_bit(PG_locked, &pp->flags);
1509 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1511 struct vrequest treq;
1514 code = afs_InitReq(&treq, credp);
1515 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1516 tdc = afs_FindDCache(avc, offset);
1518 if (!(tdc->mflags & DFNextStarted))
1519 afs_PrefetchChunk(avc, tdc, credp, &treq);
1522 ReleaseWriteLock(&avc->lock);
1532 #if defined(AFS_LINUX24_ENV)
1534 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1535 unsigned long offset, unsigned int count)
1537 struct vcache *vcp = VTOAFS(ip);
1546 buffer = kmap(pp) + offset;
1547 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1552 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1553 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1554 ICL_TYPE_INT32, 99999);
1556 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1558 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1560 ip->i_size = vcp->m.Length;
1561 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1564 struct vrequest treq;
1566 ObtainWriteLock(&vcp->lock, 533);
1567 if (!afs_InitReq(&treq, credp))
1568 code = afs_DoPartialWrite(vcp, &treq);
1569 ReleaseWriteLock(&vcp->lock);
1571 code = code ? -code : count - tuio.uio_resid;
1573 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1574 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1575 ICL_TYPE_INT32, code);
1587 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1588 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1590 afs_linux_writepage(struct page *pp)
1593 struct address_space *mapping = pp->mapping;
1594 struct inode *inode;
1595 unsigned long end_index;
1596 unsigned offset = PAGE_CACHE_SIZE;
1599 #if defined(AFS_LINUX26_ENV)
1600 if (PageReclaim(pp)) {
1601 # if defined(WRITEPAGE_ACTIVATE)
1602 return WRITEPAGE_ACTIVATE;
1604 return AOP_WRITEPAGE_ACTIVATE;
1608 if (PageLaunder(pp)) {
1609 return(fail_writepage(pp));
1613 inode = (struct inode *)mapping->host;
1614 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1617 if (pp->index < end_index)
1619 /* things got complicated... */
1620 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1621 /* OK, are we completely out? */
1622 if (pp->index >= end_index + 1 || !offset)
1625 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1626 SetPageUptodate(pp);
1628 if (status == offset)
1634 /* afs_linux_updatepage
1635 * What one would have thought was writepage - write dirty page to file.
1636 * Called from generic_file_write. buffer is still in user space. pagep
1637 * has been filled in with old data if we're updating less than a page.
1640 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1641 unsigned int count, int sync)
1643 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1644 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1650 set_bit(PG_locked, &pp->flags);
1654 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1655 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1656 ICL_TYPE_INT32, 99999);
1657 setup_uio(&tuio, &iovec, page_addr + offset,
1658 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1661 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1663 ip->i_size = vcp->m.Length;
1664 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1667 struct vrequest treq;
1669 ObtainWriteLock(&vcp->lock, 533);
1670 vcp->m.Date = osi_Time(); /* set modification time */
1671 if (!afs_InitReq(&treq, credp))
1672 code = afs_DoPartialWrite(vcp, &treq);
1673 ReleaseWriteLock(&vcp->lock);
1676 code = code ? -code : count - tuio.uio_resid;
1677 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1678 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1679 ICL_TYPE_INT32, code);
1684 clear_bit(PG_locked, &pp->flags);
1689 /* afs_linux_permission
1690 * Check access rights - returns error if can't check or permission denied.
1693 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1694 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1696 afs_linux_permission(struct inode *ip, int mode)
1700 cred_t *credp = crref();
1704 if (mode & MAY_EXEC)
1706 if (mode & MAY_READ)
1708 if (mode & MAY_WRITE)
1710 code = afs_access(VTOAFS(ip), tmp, credp);
1717 #if defined(AFS_LINUX24_ENV)
1719 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1724 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1725 offset, to - offset);
1726 #if !defined(AFS_LINUX26_ENV)
1734 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1737 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1738 call kmap directly instead of relying on us to do it */
1739 #if !defined(AFS_LINUX26_ENV)
1745 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1748 static struct inode_operations afs_file_iops = {
1749 #if defined(AFS_LINUX26_ENV)
1750 .permission = afs_linux_permission,
1751 .getattr = afs_linux_getattr,
1752 .setattr = afs_notify_change,
1753 #elif defined(AFS_LINUX24_ENV)
1754 .permission = afs_linux_permission,
1755 .revalidate = afs_linux_revalidate,
1756 .setattr = afs_notify_change,
1758 .default_file_ops = &afs_file_fops,
1759 .readpage = afs_linux_readpage,
1760 .revalidate = afs_linux_revalidate,
1761 .updatepage = afs_linux_updatepage,
1765 #if defined(AFS_LINUX24_ENV)
1766 static struct address_space_operations afs_file_aops = {
1767 .readpage = afs_linux_readpage,
1768 .writepage = afs_linux_writepage,
1769 .commit_write = afs_linux_commit_write,
1770 .prepare_write = afs_linux_prepare_write,
1775 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1776 * by what sort of operation is allowed.....
1779 static struct inode_operations afs_dir_iops = {
1780 #if !defined(AFS_LINUX24_ENV)
1781 .default_file_ops = &afs_dir_fops,
1783 .setattr = afs_notify_change,
1785 .create = afs_linux_create,
1786 .lookup = afs_linux_lookup,
1787 .link = afs_linux_link,
1788 .unlink = afs_linux_unlink,
1789 .symlink = afs_linux_symlink,
1790 .mkdir = afs_linux_mkdir,
1791 .rmdir = afs_linux_rmdir,
1792 .rename = afs_linux_rename,
1793 #if defined(AFS_LINUX26_ENV)
1794 .getattr = afs_linux_getattr,
1796 .revalidate = afs_linux_revalidate,
1798 .permission = afs_linux_permission,
1801 /* We really need a separate symlink set of ops, since do_follow_link()
1802 * determines if it _is_ a link by checking if the follow_link op is set.
1804 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1806 afs_symlink_filler(struct file *file, struct page *page)
1808 struct inode *ip = (struct inode *)page->mapping->host;
1809 char *p = (char *)kmap(page);
1814 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1819 p[code] = '\0'; /* null terminate? */
1822 SetPageUptodate(page);
1836 static struct address_space_operations afs_symlink_aops = {
1837 .readpage = afs_symlink_filler
1839 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1841 static struct inode_operations afs_symlink_iops = {
1842 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1843 .readlink = page_readlink,
1844 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1845 .follow_link = page_follow_link,
1847 .follow_link = page_follow_link_light,
1848 .put_link = page_put_link,
1850 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
1851 .readlink = afs_linux_readlink,
1852 .follow_link = afs_linux_follow_link,
1853 #if !defined(AFS_LINUX24_ENV)
1854 .permission = afs_linux_permission,
1855 .revalidate = afs_linux_revalidate,
1857 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1858 #if defined(AFS_LINUX24_ENV)
1859 .setattr = afs_notify_change,
1864 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1868 vattr2inode(ip, vattr);
1870 /* Reset ops if symlink or directory. */
1871 if (S_ISREG(ip->i_mode)) {
1872 ip->i_op = &afs_file_iops;
1873 #if defined(AFS_LINUX24_ENV)
1874 ip->i_fop = &afs_file_fops;
1875 ip->i_data.a_ops = &afs_file_aops;
1878 } else if (S_ISDIR(ip->i_mode)) {
1879 ip->i_op = &afs_dir_iops;
1880 #if defined(AFS_LINUX24_ENV)
1881 ip->i_fop = &afs_dir_fops;
1884 } else if (S_ISLNK(ip->i_mode)) {
1885 ip->i_op = &afs_symlink_iops;
1886 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1887 ip->i_data.a_ops = &afs_symlink_aops;
1888 ip->i_mapping = &ip->i_data;