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_ACCMODE) == O_RDONLY) { /* readers dont flush */
597 vcp = VTOAFS(FILE_INODE(fp));
599 code = afs_InitReq(&treq, credp);
603 ObtainSharedLock(&vcp->lock, 535);
604 if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
605 UpgradeSToWLock(&vcp->lock, 536);
606 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
607 ConvertWToSLock(&vcp->lock);
609 code = afs_CheckCode(code, &treq, 54);
610 ReleaseSharedLock(&vcp->lock);
619 #if !defined(AFS_LINUX24_ENV)
620 /* Not allowed to directly read a directory. */
622 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
630 struct file_operations afs_dir_fops = {
631 #if !defined(AFS_LINUX24_ENV)
632 .read = afs_linux_dir_read,
633 .lock = afs_linux_lock,
634 .fsync = afs_linux_fsync,
636 .read = generic_read_dir,
638 .readdir = afs_linux_readdir,
639 #ifdef HAVE_UNLOCKED_IOCTL
640 .unlocked_ioctl = afs_unlocked_xioctl,
644 #ifdef HAVE_COMPAT_IOCTL
645 .compat_ioctl = afs_unlocked_xioctl,
647 .open = afs_linux_open,
648 .release = afs_linux_release,
651 struct file_operations afs_file_fops = {
652 .read = afs_linux_read,
653 .write = afs_linux_write,
654 #ifdef GENERIC_FILE_AIO_READ
655 .aio_read = generic_file_aio_read,
656 .aio_write = generic_file_aio_write,
658 #ifdef HAVE_UNLOCKED_IOCTL
659 .unlocked_ioctl = afs_unlocked_xioctl,
663 #ifdef HAVE_COMPAT_IOCTL
664 .compat_ioctl = afs_unlocked_xioctl,
666 .mmap = afs_linux_mmap,
667 .open = afs_linux_open,
668 .flush = afs_linux_flush,
669 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
670 .sendfile = generic_file_sendfile,
672 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
673 .splice_write = generic_file_splice_write,
674 .splice_read = generic_file_splice_read,
676 .release = afs_linux_release,
677 .fsync = afs_linux_fsync,
678 .lock = afs_linux_lock,
679 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
680 .flock = afs_linux_flock,
685 /**********************************************************************
686 * AFS Linux dentry operations
687 **********************************************************************/
689 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
690 * that has its mvid (parent dir's fid) pointer set to the wrong directory
691 * due to being mounted in multiple points at once. If so, check_bad_parent()
692 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
693 * dotdotfid and mtpoint fid members.
695 * dp - dentry to be checked.
699 * This dentry's vcache's mvid will be set to the correct parent directory's
701 * This root vnode's volume will have its dotdotfid and mtpoint fids set
702 * to the correct parent and mountpoint fids.
706 check_bad_parent(struct dentry *dp)
709 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
710 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
712 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
715 /* force a lookup, so vcp->mvid is fixed up */
716 afs_lookup(pvc, dp->d_name.name, &avc, credp);
717 if (!avc || vcp != avc) { /* bad, very bad.. */
718 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
719 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
720 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
721 ICL_TYPE_POINTER, dp);
724 AFS_RELE(AFSTOV(avc));
731 /* afs_linux_revalidate
732 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
735 afs_linux_revalidate(struct dentry *dp)
738 struct vcache *vcp = VTOAFS(dp->d_inode);
742 #ifdef AFS_LINUX24_ENV
748 /* Make this a fast path (no crref), since it's called so often. */
749 if (vcp->states & CStatd) {
751 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
752 check_bad_parent(dp); /* check and correct mvid */
755 #ifdef AFS_LINUX24_ENV
763 code = afs_getattr(vcp, &vattr, credp);
765 vattr2inode(AFSTOV(vcp), &vattr);
768 #ifdef AFS_LINUX24_ENV
776 #if defined(AFS_LINUX26_ENV)
778 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
780 int err = afs_linux_revalidate(dentry);
782 generic_fillattr(dentry->d_inode, stat);
788 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
789 * In kernels 2.2.10 and above, we are passed an additional flags var which
790 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
791 * we are advised to follow the entry if it is a link or to make sure that
792 * it is a directory. But since the kernel itself checks these possibilities
793 * later on, we shouldn't have to do it until later. Perhaps in the future..
796 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
797 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
798 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
800 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
803 afs_linux_dentry_revalidate(struct dentry *dp)
807 cred_t *credp = NULL;
808 struct vcache *vcp, *pvcp, *tvc = NULL;
811 #ifdef AFS_LINUX24_ENV
818 vcp = VTOAFS(dp->d_inode);
819 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
821 if (vcp == afs_globalVp)
824 if (vcp->mvstat == 1) { /* mount point */
825 if (vcp->mvid && (vcp->states & CMValid)) {
826 /* a mount point, not yet replaced by its directory */
830 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
831 check_bad_parent(dp); /* check and correct mvid */
834 /* If the last looker changes, we should make sure the current
835 * looker still has permission to examine this file. This would
836 * always require a crref() which would be "slow".
838 if (vcp->last_looker != treq.uid) {
839 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
842 vcp->last_looker = treq.uid;
846 /* If the parent's DataVersion has changed or the vnode
847 * is longer valid, we need to do a full lookup. VerifyVCache
848 * isn't enough since the vnode may have been renamed.
851 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
854 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
855 if (!tvc || tvc != vcp)
858 if (afs_getattr(vcp, &vattr, credp))
861 vattr2inode(AFSTOV(vcp), &vattr);
862 dp->d_time = hgetlo(pvcp->m.DataVersion);
865 /* should we always update the attributes at this point? */
866 /* unlikely--the vcache entry hasn't changed */
870 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
871 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
875 /* No change in parent's DataVersion so this negative
876 * lookup is still valid. BUT, if a server is down a
877 * negative lookup can result so there should be a
878 * liftime as well. For now, always expire.
896 shrink_dcache_parent(dp);
899 #ifdef AFS_LINUX24_ENV
910 afs_dentry_iput(struct dentry *dp, struct inode *ip)
912 struct vcache *vcp = VTOAFS(ip);
915 (void) afs_InactiveVCache(vcp, NULL);
917 #ifdef DCACHE_NFSFS_RENAMED
918 #ifdef AFS_LINUX26_ENV
919 spin_lock(&dp->d_lock);
921 dp->d_flags &= ~DCACHE_NFSFS_RENAMED;
922 #ifdef AFS_LINUX26_ENV
923 spin_unlock(&dp->d_lock);
931 afs_dentry_delete(struct dentry *dp)
933 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
934 return 1; /* bad inode? */
939 struct dentry_operations afs_dentry_operations = {
940 .d_revalidate = afs_linux_dentry_revalidate,
941 .d_delete = afs_dentry_delete,
942 .d_iput = afs_dentry_iput,
945 /**********************************************************************
946 * AFS Linux inode operations
947 **********************************************************************/
951 * Merely need to set enough of vattr to get us through the create. Note
952 * that the higher level code (open_namei) will take care of any tuncation
953 * explicitly. Exclusive open is also taken care of in open_namei.
955 * name is in kernel space at this point.
958 #ifdef IOP_CREATE_TAKES_NAMEIDATA
959 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
960 struct nameidata *nd)
962 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
966 cred_t *credp = crref();
967 const char *name = dp->d_name.name;
972 vattr.va_mode = mode;
973 vattr.va_type = mode & S_IFMT;
975 #if defined(AFS_LINUX26_ENV)
979 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
983 struct inode *ip = AFSTOV(vcp);
985 afs_getattr(vcp, &vattr, credp);
986 afs_fill_inode(ip, &vattr);
987 insert_inode_hash(ip);
988 dp->d_op = &afs_dentry_operations;
989 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
990 d_instantiate(dp, ip);
994 #if defined(AFS_LINUX26_ENV)
1001 /* afs_linux_lookup */
1002 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1003 static struct dentry *
1004 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1005 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1006 struct nameidata *nd)
1008 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1012 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1015 cred_t *credp = crref();
1016 struct vcache *vcp = NULL;
1017 const char *comp = dp->d_name.name;
1018 struct inode *ip = NULL;
1019 #if defined(AFS_LINUX26_ENV)
1020 struct dentry *newdp = NULL;
1024 #if defined(AFS_LINUX26_ENV)
1028 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1034 afs_getattr(vcp, &vattr, credp);
1035 afs_fill_inode(ip, &vattr);
1037 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1038 hlist_unhashed(&ip->i_hash)
1039 #elif defined(AFS_LINUX26_ENV)
1040 ip->i_hash.pprev == NULL
1042 ip->i_hash.prev == NULL
1045 insert_inode_hash(ip);
1047 dp->d_op = &afs_dentry_operations;
1048 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1051 #if defined(AFS_LINUX24_ENV)
1052 if (ip && S_ISDIR(ip->i_mode)) {
1053 struct dentry *alias;
1055 /* Try to invalidate an existing alias in favor of our new one */
1056 alias = d_find_alias(ip);
1057 #if defined(AFS_LINUX26_ENV)
1058 /* But not if it's disconnected; then we want d_splice_alias below */
1059 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1063 if (d_invalidate(alias) == 0) {
1067 #if defined(AFS_LINUX26_ENV)
1076 #if defined(AFS_LINUX26_ENV)
1077 newdp = d_splice_alias(ip, dp);
1082 #if defined(AFS_LINUX26_ENV)
1087 /* It's ok for the file to not be found. That's noted by the caller by
1088 * seeing that the dp->d_inode field is NULL.
1090 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1091 #if defined(AFS_LINUX26_ENV)
1092 if (!code || code == ENOENT)
1099 return ERR_PTR(-code);
1108 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1111 cred_t *credp = crref();
1112 const char *name = newdp->d_name.name;
1113 struct inode *oldip = olddp->d_inode;
1115 /* If afs_link returned the vnode, we could instantiate the
1116 * dentry. Since it's not, we drop this one and do a new lookup.
1121 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1129 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1132 cred_t *credp = crref();
1133 const char *name = dp->d_name.name;
1134 struct vcache *tvc = VTOAFS(dp->d_inode);
1136 #if defined(AFS_LINUX26_ENV)
1139 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1140 && !(tvc->states & CUnlinked)) {
1141 struct dentry *__dp;
1143 extern char *afs_newname();
1152 osi_FreeSmallSpace(__name);
1153 __name = afs_newname();
1156 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1160 } while (__dp->d_inode != NULL);
1163 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1165 tvc->mvid = (void *) __name;
1168 crfree(tvc->uncred);
1170 tvc->uncred = credp;
1171 tvc->states |= CUnlinked;
1172 #ifdef DCACHE_NFSFS_RENAMED
1173 #ifdef AFS_LINUX26_ENV
1174 spin_lock(&dp->d_lock);
1176 dp->d_flags |= DCACHE_NFSFS_RENAMED;
1177 #ifdef AFS_LINUX26_ENV
1178 spin_unlock(&dp->d_lock);
1182 osi_FreeSmallSpace(__name);
1187 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1196 code = afs_remove(VTOAFS(dip), name, credp);
1201 #if defined(AFS_LINUX26_ENV)
1210 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1213 cred_t *credp = crref();
1215 const char *name = dp->d_name.name;
1217 /* If afs_symlink returned the vnode, we could instantiate the
1218 * dentry. Since it's not, we drop this one and do a new lookup.
1224 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1231 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1234 cred_t *credp = crref();
1235 struct vcache *tvcp = NULL;
1237 const char *name = dp->d_name.name;
1239 #if defined(AFS_LINUX26_ENV)
1243 vattr.va_mask = ATTR_MODE;
1244 vattr.va_mode = mode;
1246 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1249 struct inode *ip = AFSTOV(tvcp);
1251 afs_getattr(tvcp, &vattr, credp);
1252 afs_fill_inode(ip, &vattr);
1254 dp->d_op = &afs_dentry_operations;
1255 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1256 d_instantiate(dp, ip);
1260 #if defined(AFS_LINUX26_ENV)
1268 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1271 cred_t *credp = crref();
1272 const char *name = dp->d_name.name;
1274 /* locking kernel conflicts with glock? */
1277 code = afs_rmdir(VTOAFS(dip), name, credp);
1280 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1281 * that failed because a directory is not empty. So, we map
1282 * EEXIST to ENOTEMPTY on linux.
1284 if (code == EEXIST) {
1298 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1299 struct inode *newip, struct dentry *newdp)
1302 cred_t *credp = crref();
1303 const char *oldname = olddp->d_name.name;
1304 const char *newname = newdp->d_name.name;
1305 struct dentry *rehash = NULL;
1307 #if defined(AFS_LINUX26_ENV)
1308 /* Prevent any new references during rename operation. */
1311 if (!d_unhashed(newdp)) {
1316 if (!list_empty(&newdp->d_hash)) {
1322 #if defined(AFS_LINUX24_ENV)
1323 if (atomic_read(&olddp->d_count) > 1)
1324 shrink_dcache_parent(olddp);
1328 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1332 olddp->d_time = 0; /* force to revalidate */
1337 #if defined(AFS_LINUX26_ENV)
1346 /* afs_linux_ireadlink
1347 * Internal readlink which can return link contents to user or kernel space.
1348 * Note that the buffer is NOT supposed to be null-terminated.
1351 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1354 cred_t *credp = crref();
1358 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1359 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1363 return maxlen - tuio.uio_resid;
1368 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1369 /* afs_linux_readlink
1370 * Fill target (which is in user space) with contents of symlink.
1373 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1376 struct inode *ip = dp->d_inode;
1379 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1385 /* afs_linux_follow_link
1386 * a file system dependent link following routine.
1388 #if defined(AFS_LINUX24_ENV)
1389 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1394 name = osi_Alloc(PATH_MAX);
1400 code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1408 code = vfs_follow_link(nd, name);
1411 osi_Free(name, PATH_MAX);
1416 #else /* !defined(AFS_LINUX24_ENV) */
1418 static struct dentry *
1419 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1420 unsigned int follow)
1428 name = osi_Alloc(PATH_MAX + 1);
1432 return ERR_PTR(-EIO);
1435 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1440 res = ERR_PTR(code);
1443 res = lookup_dentry(name, basep, follow);
1447 osi_Free(name, PATH_MAX + 1);
1451 #endif /* AFS_LINUX24_ENV */
1452 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1454 /* afs_linux_readpage
1455 * all reads come through here. A strategy-like read call.
1458 afs_linux_readpage(struct file *fp, struct page *pp)
1461 cred_t *credp = crref();
1462 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1464 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1466 ulong address = afs_linux_page_address(pp);
1467 afs_offs_t offset = pageoff(pp);
1471 struct inode *ip = FILE_INODE(fp);
1472 int cnt = page_count(pp);
1473 struct vcache *avc = VTOAFS(ip);
1476 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1480 atomic_add(1, &pp->count);
1481 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1482 clear_bit(PG_error, &pp->flags);
1485 setup_uio(&tuio, &iovec, (char *)address, offset, PAGE_SIZE, UIO_READ,
1487 #ifdef AFS_LINUX24_ENV
1491 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 */
1492 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1493 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1494 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1497 #ifdef AFS_LINUX24_ENV
1502 if (tuio.uio_resid) /* zero remainder of page */
1503 memset((void *)(address + (PAGE_SIZE - tuio.uio_resid)), 0,
1505 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1506 flush_dcache_page(pp);
1507 SetPageUptodate(pp);
1509 set_bit(PG_uptodate, &pp->flags);
1513 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1517 clear_bit(PG_locked, &pp->flags);
1522 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1524 struct vrequest treq;
1527 code = afs_InitReq(&treq, credp);
1528 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1529 tdc = afs_FindDCache(avc, offset);
1531 if (!(tdc->mflags & DFNextStarted))
1532 afs_PrefetchChunk(avc, tdc, credp, &treq);
1535 ReleaseWriteLock(&avc->lock);
1545 #if defined(AFS_LINUX24_ENV)
1547 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1548 unsigned long offset, unsigned int count)
1550 struct vcache *vcp = VTOAFS(ip);
1559 buffer = kmap(pp) + offset;
1560 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1565 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1566 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1567 ICL_TYPE_INT32, 99999);
1569 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1571 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1573 ip->i_size = vcp->m.Length;
1574 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1577 struct vrequest treq;
1579 ObtainWriteLock(&vcp->lock, 533);
1580 if (!afs_InitReq(&treq, credp))
1581 code = afs_DoPartialWrite(vcp, &treq);
1582 ReleaseWriteLock(&vcp->lock);
1584 code = code ? -code : count - tuio.uio_resid;
1586 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1587 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1588 ICL_TYPE_INT32, code);
1600 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1601 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1603 afs_linux_writepage(struct page *pp)
1606 struct address_space *mapping = pp->mapping;
1607 struct inode *inode;
1608 unsigned long end_index;
1609 unsigned offset = PAGE_CACHE_SIZE;
1612 #if defined(AFS_LINUX26_ENV)
1613 if (PageReclaim(pp)) {
1614 # if defined(WRITEPAGE_ACTIVATE)
1615 return WRITEPAGE_ACTIVATE;
1617 return AOP_WRITEPAGE_ACTIVATE;
1621 if (PageLaunder(pp)) {
1622 return(fail_writepage(pp));
1626 inode = (struct inode *)mapping->host;
1627 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1630 if (pp->index < end_index)
1632 /* things got complicated... */
1633 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1634 /* OK, are we completely out? */
1635 if (pp->index >= end_index + 1 || !offset)
1638 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1639 SetPageUptodate(pp);
1641 if (status == offset)
1647 /* afs_linux_updatepage
1648 * What one would have thought was writepage - write dirty page to file.
1649 * Called from generic_file_write. buffer is still in user space. pagep
1650 * has been filled in with old data if we're updating less than a page.
1653 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1654 unsigned int count, int sync)
1656 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1657 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1663 set_bit(PG_locked, &pp->flags);
1667 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1668 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1669 ICL_TYPE_INT32, 99999);
1670 setup_uio(&tuio, &iovec, page_addr + offset,
1671 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1674 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1676 ip->i_size = vcp->m.Length;
1677 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1680 struct vrequest treq;
1682 ObtainWriteLock(&vcp->lock, 533);
1683 vcp->m.Date = osi_Time(); /* set modification time */
1684 if (!afs_InitReq(&treq, credp))
1685 code = afs_DoPartialWrite(vcp, &treq);
1686 ReleaseWriteLock(&vcp->lock);
1689 code = code ? -code : count - tuio.uio_resid;
1690 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1691 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1692 ICL_TYPE_INT32, code);
1697 clear_bit(PG_locked, &pp->flags);
1702 /* afs_linux_permission
1703 * Check access rights - returns error if can't check or permission denied.
1706 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1707 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1709 afs_linux_permission(struct inode *ip, int mode)
1713 cred_t *credp = crref();
1717 if (mode & MAY_EXEC)
1719 if (mode & MAY_READ)
1721 if (mode & MAY_WRITE)
1723 code = afs_access(VTOAFS(ip), tmp, credp);
1730 #if defined(AFS_LINUX24_ENV)
1732 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1737 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1738 offset, to - offset);
1739 #if !defined(AFS_LINUX26_ENV)
1747 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1750 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1751 call kmap directly instead of relying on us to do it */
1752 #if !defined(AFS_LINUX26_ENV)
1758 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1761 static struct inode_operations afs_file_iops = {
1762 #if defined(AFS_LINUX26_ENV)
1763 .permission = afs_linux_permission,
1764 .getattr = afs_linux_getattr,
1765 .setattr = afs_notify_change,
1766 #elif defined(AFS_LINUX24_ENV)
1767 .permission = afs_linux_permission,
1768 .revalidate = afs_linux_revalidate,
1769 .setattr = afs_notify_change,
1771 .default_file_ops = &afs_file_fops,
1772 .readpage = afs_linux_readpage,
1773 .revalidate = afs_linux_revalidate,
1774 .updatepage = afs_linux_updatepage,
1778 #if defined(AFS_LINUX24_ENV)
1779 static struct address_space_operations afs_file_aops = {
1780 .readpage = afs_linux_readpage,
1781 .writepage = afs_linux_writepage,
1782 .commit_write = afs_linux_commit_write,
1783 .prepare_write = afs_linux_prepare_write,
1788 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1789 * by what sort of operation is allowed.....
1792 static struct inode_operations afs_dir_iops = {
1793 #if !defined(AFS_LINUX24_ENV)
1794 .default_file_ops = &afs_dir_fops,
1796 .setattr = afs_notify_change,
1798 .create = afs_linux_create,
1799 .lookup = afs_linux_lookup,
1800 .link = afs_linux_link,
1801 .unlink = afs_linux_unlink,
1802 .symlink = afs_linux_symlink,
1803 .mkdir = afs_linux_mkdir,
1804 .rmdir = afs_linux_rmdir,
1805 .rename = afs_linux_rename,
1806 #if defined(AFS_LINUX26_ENV)
1807 .getattr = afs_linux_getattr,
1809 .revalidate = afs_linux_revalidate,
1811 .permission = afs_linux_permission,
1814 /* We really need a separate symlink set of ops, since do_follow_link()
1815 * determines if it _is_ a link by checking if the follow_link op is set.
1817 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1819 afs_symlink_filler(struct file *file, struct page *page)
1821 struct inode *ip = (struct inode *)page->mapping->host;
1822 char *p = (char *)kmap(page);
1827 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1832 p[code] = '\0'; /* null terminate? */
1835 SetPageUptodate(page);
1849 static struct address_space_operations afs_symlink_aops = {
1850 .readpage = afs_symlink_filler
1852 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1854 static struct inode_operations afs_symlink_iops = {
1855 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1856 .readlink = page_readlink,
1857 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1858 .follow_link = page_follow_link,
1860 .follow_link = page_follow_link_light,
1861 .put_link = page_put_link,
1863 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
1864 .readlink = afs_linux_readlink,
1865 .follow_link = afs_linux_follow_link,
1866 #if !defined(AFS_LINUX24_ENV)
1867 .permission = afs_linux_permission,
1868 .revalidate = afs_linux_revalidate,
1870 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1871 #if defined(AFS_LINUX24_ENV)
1872 .setattr = afs_notify_change,
1877 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1881 vattr2inode(ip, vattr);
1883 /* Reset ops if symlink or directory. */
1884 if (S_ISREG(ip->i_mode)) {
1885 ip->i_op = &afs_file_iops;
1886 #if defined(AFS_LINUX24_ENV)
1887 ip->i_fop = &afs_file_fops;
1888 ip->i_data.a_ops = &afs_file_aops;
1891 } else if (S_ISDIR(ip->i_mode)) {
1892 ip->i_op = &afs_dir_iops;
1893 #if defined(AFS_LINUX24_ENV)
1894 ip->i_fop = &afs_dir_fops;
1897 } else if (S_ISLNK(ip->i_mode)) {
1898 ip->i_op = &afs_symlink_iops;
1899 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1900 ip->i_data.a_ops = &afs_symlink_aops;
1901 ip->i_mapping = &ip->i_data;