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;
56 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
59 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
60 cred_t *credp = crref();
64 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
65 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
68 /* get a validated vcache entry */
69 code = afs_InitReq(&treq, credp);
71 code = afs_VerifyVCache(vcp, &treq);
76 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
78 code = generic_file_read(fp, buf, count, offp);
82 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
83 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
92 /* Now we have integrated VM for writes as well as reads. generic_file_write
93 * also takes care of re-positioning the pointer if file is open in append
94 * mode. Call fake open/close to ensure we do writes of core dumps.
97 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
101 struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
102 struct vrequest treq;
103 cred_t *credp = crref();
107 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
108 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
109 (fp->f_flags & O_APPEND) ? 99998 : 99999);
112 /* get a validated vcache entry */
113 code = (ssize_t) afs_InitReq(&treq, credp);
115 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
117 ObtainWriteLock(&vcp->lock, 529);
119 ReleaseWriteLock(&vcp->lock);
124 code = generic_file_write(fp, buf, count, offp);
128 ObtainWriteLock(&vcp->lock, 530);
129 afs_FakeClose(vcp, credp);
130 ReleaseWriteLock(&vcp->lock);
132 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
133 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
141 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
143 /* This is a complete rewrite of afs_readdir, since we can make use of
144 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
145 * handling and use of bulkstats will need to be reflected here as well.
148 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
150 extern struct DirEntry *afs_dir_GetBlob();
151 struct vcache *avc = VTOAFS(FILE_INODE(fp));
152 struct vrequest treq;
153 register struct dcache *tdc;
160 afs_size_t origOffset, tlen;
161 cred_t *credp = crref();
162 struct afs_fakestat_state fakestat;
164 #if defined(AFS_LINUX26_ENV)
168 AFS_STATCNT(afs_readdir);
170 code = afs_InitReq(&treq, credp);
175 afs_InitFakeStat(&fakestat);
176 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
180 /* update the cache entry */
182 code = afs_VerifyVCache(avc, &treq);
186 /* get a reference to the entire directory */
187 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
193 ObtainReadLock(&avc->lock);
194 ObtainReadLock(&tdc->lock);
196 * Make sure that the data in the cache is current. There are two
197 * cases we need to worry about:
198 * 1. The cache data is being fetched by another process.
199 * 2. The cache data is no longer valid
201 while ((avc->states & CStatd)
202 && (tdc->dflags & DFFetching)
203 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
204 ReleaseReadLock(&tdc->lock);
205 ReleaseReadLock(&avc->lock);
206 afs_osi_Sleep(&tdc->validPos);
207 ObtainReadLock(&avc->lock);
208 ObtainReadLock(&tdc->lock);
210 if (!(avc->states & CStatd)
211 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
212 ReleaseReadLock(&tdc->lock);
213 ReleaseReadLock(&avc->lock);
218 /* Fill in until we get an error or we're done. This implementation
219 * takes an offset in units of blobs, rather than bytes.
222 offset = (int) fp->f_pos;
224 dirpos = BlobScan(tdc, offset);
228 de = afs_dir_GetBlob(tdc, dirpos);
232 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
233 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
235 len = strlen(de->name);
237 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
238 (unsigned long)&tdc->f.inode, dirpos);
239 DRelease((struct buffer *) de, 0);
241 ReleaseReadLock(&avc->lock);
246 /* filldir returns -EINVAL when the buffer is full. */
247 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
249 unsigned int type = DT_UNKNOWN;
250 struct VenusFid afid;
253 afid.Cell = avc->fid.Cell;
254 afid.Fid.Volume = avc->fid.Fid.Volume;
255 afid.Fid.Vnode = ntohl(de->fid.vnode);
256 afid.Fid.Unique = ntohl(de->fid.vunique);
257 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
259 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
262 } else if (((tvc->states) & (CStatd | CTruth))) {
263 /* CTruth will be set if the object has
268 else if (vtype == VREG)
270 /* Don't do this until we're sure it can't be a mtpt */
271 /* else if (vtype == VLNK)
273 /* what other types does AFS support? */
275 /* clean up from afs_FindVCache */
278 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
281 code = (*filldir) (dirbuf, de->name, len, offset, ino);
283 DRelease((struct buffer *)de, 0);
286 offset = dirpos + 1 + ((len + 16) >> 5);
288 /* If filldir didn't fill in the last one this is still pointing to that
291 fp->f_pos = (loff_t) offset;
293 ReleaseReadLock(&tdc->lock);
295 ReleaseReadLock(&avc->lock);
299 afs_PutFakeStat(&fakestat);
302 #if defined(AFS_LINUX26_ENV)
309 /* in afs_pioctl.c */
310 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
313 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
314 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
316 return afs_xioctl(FILE_INODE(fp), fp, com, arg);
323 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
325 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
326 cred_t *credp = crref();
327 struct vrequest treq;
331 #if defined(AFS_LINUX24_ENV)
332 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
333 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
334 vmap->vm_end - vmap->vm_start);
336 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
337 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
338 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
342 /* get a validated vcache entry */
343 code = afs_InitReq(&treq, credp);
347 code = afs_VerifyVCache(vcp, &treq);
351 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
354 code = generic_file_mmap(fp, vmap);
357 vcp->states |= CMAPPED;
370 afs_linux_open(struct inode *ip, struct file *fp)
372 struct vcache *vcp = VTOAFS(ip);
373 cred_t *credp = crref();
376 #ifdef AFS_LINUX24_ENV
380 code = afs_open(&vcp, fp->f_flags, credp);
382 #ifdef AFS_LINUX24_ENV
391 afs_linux_release(struct inode *ip, struct file *fp)
393 struct vcache *vcp = VTOAFS(ip);
394 cred_t *credp = crref();
397 #ifdef AFS_LINUX24_ENV
401 code = afs_close(vcp, fp->f_flags, credp);
403 #ifdef AFS_LINUX24_ENV
412 #if defined(AFS_LINUX24_ENV)
413 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
415 afs_linux_fsync(struct file *fp, struct dentry *dp)
419 struct inode *ip = FILE_INODE(fp);
420 cred_t *credp = crref();
422 #ifdef AFS_LINUX24_ENV
426 code = afs_fsync(VTOAFS(ip), credp);
428 #ifdef AFS_LINUX24_ENV
438 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
441 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
442 cred_t *credp = crref();
443 struct AFS_FLOCK flock;
444 /* Convert to a lock format afs_lockctl understands. */
445 memset((char *)&flock, 0, sizeof(flock));
446 flock.l_type = flp->fl_type;
447 flock.l_pid = flp->fl_pid;
449 flock.l_start = flp->fl_start;
450 flock.l_len = flp->fl_end - flp->fl_start;
452 /* Safe because there are no large files, yet */
453 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
454 if (cmd == F_GETLK64)
456 else if (cmd == F_SETLK64)
458 else if (cmd == F_SETLKW64)
460 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
463 code = afs_lockctl(vcp, &flock, cmd, credp);
466 /* Convert flock back to Linux's file_lock */
467 flp->fl_type = flock.l_type;
468 flp->fl_pid = flock.l_pid;
469 flp->fl_start = flock.l_start;
470 flp->fl_end = flock.l_start + flock.l_len;
478 * essentially the same as afs_fsync() but we need to get the return
479 * code for the sys_close() here, not afs_linux_release(), so call
480 * afs_StoreAllSegments() with AFS_LASTSTORE
483 afs_linux_flush(struct file *fp)
485 struct vrequest treq;
486 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
487 cred_t *credp = crref();
492 code = afs_InitReq(&treq, credp);
496 ObtainSharedLock(&vcp->lock, 535);
497 if (vcp->execsOrWriters > 0) {
498 UpgradeSToWLock(&vcp->lock, 536);
499 code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
500 ConvertWToSLock(&vcp->lock);
502 code = afs_CheckCode(code, &treq, 54);
503 ReleaseSharedLock(&vcp->lock);
512 #if !defined(AFS_LINUX24_ENV)
513 /* Not allowed to directly read a directory. */
515 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
523 struct file_operations afs_dir_fops = {
524 #if !defined(AFS_LINUX24_ENV)
525 .read = afs_linux_dir_read,
526 .lock = afs_linux_lock,
527 .fsync = afs_linux_fsync,
529 .read = generic_read_dir,
531 .readdir = afs_linux_readdir,
532 #ifdef HAVE_UNLOCKED_IOCTL
533 .unlocked_ioctl = afs_unlocked_xioctl,
537 #ifdef HAVE_COMPAT_IOCTL
538 .compat_ioctl = afs_unlocked_xioctl,
540 .open = afs_linux_open,
541 .release = afs_linux_release,
544 struct file_operations afs_file_fops = {
545 .read = afs_linux_read,
546 .write = afs_linux_write,
547 #ifdef HAVE_UNLOCKED_IOCTL
548 .unlocked_ioctl = afs_unlocked_xioctl,
552 #ifdef HAVE_COMPAT_IOCTL
553 .compat_ioctl = afs_unlocked_xioctl,
555 .mmap = afs_linux_mmap,
556 .open = afs_linux_open,
557 .flush = afs_linux_flush,
558 #ifdef AFS_LINUX26_ENV
559 .sendfile = generic_file_sendfile,
561 .release = afs_linux_release,
562 .fsync = afs_linux_fsync,
563 .lock = afs_linux_lock,
567 /**********************************************************************
568 * AFS Linux dentry operations
569 **********************************************************************/
571 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
572 * that has its mvid (parent dir's fid) pointer set to the wrong directory
573 * due to being mounted in multiple points at once. If so, check_bad_parent()
574 * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
575 * dotdotfid and mtpoint fid members.
577 * dp - dentry to be checked.
581 * This dentry's vcache's mvid will be set to the correct parent directory's
583 * This root vnode's volume will have its dotdotfid and mtpoint fids set
584 * to the correct parent and mountpoint fids.
588 check_bad_parent(struct dentry *dp)
591 struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
592 struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
594 if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
597 /* force a lookup, so vcp->mvid is fixed up */
598 afs_lookup(pvc, dp->d_name.name, &avc, credp);
599 if (!avc || vcp != avc) { /* bad, very bad.. */
600 afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
601 "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
602 ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
603 ICL_TYPE_POINTER, dp);
606 AFS_RELE(AFSTOV(avc));
613 /* afs_linux_revalidate
614 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
617 afs_linux_revalidate(struct dentry *dp)
620 struct vcache *vcp = VTOAFS(dp->d_inode);
624 #ifdef AFS_LINUX24_ENV
630 /* Make this a fast path (no crref), since it's called so often. */
631 if (vcp->states & CStatd) {
633 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
634 check_bad_parent(dp); /* check and correct mvid */
637 #ifdef AFS_LINUX24_ENV
645 code = afs_getattr(vcp, &vattr, credp);
647 vattr2inode(AFSTOV(vcp), &vattr);
650 #ifdef AFS_LINUX24_ENV
658 #if defined(AFS_LINUX26_ENV)
660 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
662 int err = afs_linux_revalidate(dentry);
664 generic_fillattr(dentry->d_inode, stat);
670 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
671 * In kernels 2.2.10 and above, we are passed an additional flags var which
672 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
673 * we are advised to follow the entry if it is a link or to make sure that
674 * it is a directory. But since the kernel itself checks these possibilities
675 * later on, we shouldn't have to do it until later. Perhaps in the future..
678 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
679 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
680 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
682 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
685 afs_linux_dentry_revalidate(struct dentry *dp)
689 cred_t *credp = NULL;
690 struct vcache *vcp, *pvcp, *tvc = NULL;
693 #ifdef AFS_LINUX24_ENV
700 vcp = VTOAFS(dp->d_inode);
701 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
703 if (vcp == afs_globalVp)
706 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
707 check_bad_parent(dp); /* check and correct mvid */
710 /* If the last looker changes, we should make sure the current
711 * looker still has permission to examine this file. This would
712 * always require a crref() which would be "slow".
714 if (vcp->last_looker != treq.uid) {
715 if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
718 vcp->last_looker = treq.uid;
722 /* If the parent's DataVersion has changed or the vnode
723 * is longer valid, we need to do a full lookup. VerifyVCache
724 * isn't enough since the vnode may have been renamed.
727 if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
730 afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
731 if (!tvc || tvc != vcp)
734 if (afs_getattr(vcp, &vattr, credp))
737 vattr2inode(AFSTOV(vcp), &vattr);
738 dp->d_time = hgetlo(pvcp->m.DataVersion);
741 /* should we always update the attributes at this point? */
742 /* unlikely--the vcache entry hasn't changed */
746 pvcp = VTOAFS(dp->d_parent->d_inode); /* dget_parent()? */
747 if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
751 /* No change in parent's DataVersion so this negative
752 * lookup is still valid. BUT, if a server is down a
753 * negative lookup can result so there should be a
754 * liftime as well. For now, always expire.
772 shrink_dcache_parent(dp);
775 #ifdef AFS_LINUX24_ENV
786 afs_dentry_iput(struct dentry *dp, struct inode *ip)
788 struct vcache *vcp = VTOAFS(ip);
791 if (vcp->states & CUnlinked)
792 (void) afs_InactiveVCache(vcp, NULL);
799 afs_dentry_delete(struct dentry *dp)
801 if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
802 return 1; /* bad inode? */
807 struct dentry_operations afs_dentry_operations = {
808 .d_revalidate = afs_linux_dentry_revalidate,
809 .d_delete = afs_dentry_delete,
810 .d_iput = afs_dentry_iput,
813 /**********************************************************************
814 * AFS Linux inode operations
815 **********************************************************************/
819 * Merely need to set enough of vattr to get us through the create. Note
820 * that the higher level code (open_namei) will take care of any tuncation
821 * explicitly. Exclusive open is also taken care of in open_namei.
823 * name is in kernel space at this point.
826 #ifdef IOP_CREATE_TAKES_NAMEIDATA
827 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
828 struct nameidata *nd)
830 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
834 cred_t *credp = crref();
835 const char *name = dp->d_name.name;
840 vattr.va_mode = mode;
841 vattr.va_type = mode & S_IFMT;
843 #if defined(AFS_LINUX26_ENV)
847 code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
851 struct inode *ip = AFSTOV(vcp);
853 afs_getattr(vcp, &vattr, credp);
854 afs_fill_inode(ip, &vattr);
855 dp->d_op = &afs_dentry_operations;
856 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
857 d_instantiate(dp, ip);
861 #if defined(AFS_LINUX26_ENV)
868 /* afs_linux_lookup */
869 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
870 static struct dentry *
871 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
872 afs_linux_lookup(struct inode *dip, struct dentry *dp,
873 struct nameidata *nd)
875 afs_linux_lookup(struct inode *dip, struct dentry *dp)
879 afs_linux_lookup(struct inode *dip, struct dentry *dp)
882 cred_t *credp = crref();
883 struct vcache *vcp = NULL;
884 const char *comp = dp->d_name.name;
885 struct inode *ip = NULL;
888 #if defined(AFS_LINUX26_ENV)
892 code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
898 afs_getattr(vcp, &vattr, credp);
899 afs_fill_inode(ip, &vattr);
901 dp->d_op = &afs_dentry_operations;
902 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
905 #if defined(AFS_LINUX24_ENV)
906 if (ip && S_ISDIR(ip->i_mode)) {
907 struct dentry *alias;
909 alias = d_find_alias(ip);
911 if (d_invalidate(alias) == 0) {
915 #if defined(AFS_LINUX26_ENV)
925 #if defined(AFS_LINUX26_ENV)
930 /* It's ok for the file to not be found. That's noted by the caller by
931 * seeing that the dp->d_inode field is NULL.
933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
937 return ERR_PTR(-code);
946 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
949 cred_t *credp = crref();
950 const char *name = newdp->d_name.name;
951 struct inode *oldip = olddp->d_inode;
953 /* If afs_link returned the vnode, we could instantiate the
954 * dentry. Since it's not, we drop this one and do a new lookup.
959 code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
967 afs_linux_unlink(struct inode *dip, struct dentry *dp)
970 cred_t *credp = crref();
971 const char *name = dp->d_name.name;
972 struct vcache *tvc = VTOAFS(dp->d_inode);
974 #if defined(AFS_LINUX26_ENV)
977 if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
978 && !(tvc->states & CUnlinked)) {
981 extern char *afs_newname();
990 osi_FreeSmallSpace(__name);
991 __name = afs_newname();
994 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
998 } while (__dp->d_inode != NULL);
1001 code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1003 tvc->mvid = (void *) __name;
1006 crfree(tvc->uncred);
1008 tvc->uncred = credp;
1009 tvc->states |= CUnlinked;
1014 __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1023 code = afs_remove(VTOAFS(dip), name, credp);
1028 #if defined(AFS_LINUX26_ENV)
1037 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1040 cred_t *credp = crref();
1042 const char *name = dp->d_name.name;
1044 /* If afs_symlink returned the vnode, we could instantiate the
1045 * dentry. Since it's not, we drop this one and do a new lookup.
1051 code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1058 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1061 cred_t *credp = crref();
1062 struct vcache *tvcp = NULL;
1064 const char *name = dp->d_name.name;
1066 #if defined(AFS_LINUX26_ENV)
1070 vattr.va_mask = ATTR_MODE;
1071 vattr.va_mode = mode;
1073 code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1076 struct inode *ip = AFSTOV(tvcp);
1078 afs_getattr(tvcp, &vattr, credp);
1079 afs_fill_inode(ip, &vattr);
1081 dp->d_op = &afs_dentry_operations;
1082 dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1083 d_instantiate(dp, ip);
1087 #if defined(AFS_LINUX26_ENV)
1095 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1098 cred_t *credp = crref();
1099 const char *name = dp->d_name.name;
1101 /* locking kernel conflicts with glock? */
1104 code = afs_rmdir(VTOAFS(dip), name, credp);
1107 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1108 * that failed because a directory is not empty. So, we map
1109 * EEXIST to ENOTEMPTY on linux.
1111 if (code == EEXIST) {
1125 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1126 struct inode *newip, struct dentry *newdp)
1129 cred_t *credp = crref();
1130 const char *oldname = olddp->d_name.name;
1131 const char *newname = newdp->d_name.name;
1132 struct dentry *rehash = NULL;
1134 #if defined(AFS_LINUX26_ENV)
1135 /* Prevent any new references during rename operation. */
1138 /* Remove old and new entries from name hash. New one will change below.
1139 * While it's optimal to catch failures and re-insert newdp into hash,
1140 * it's also error prone and in that case we're already dealing with error
1141 * cases. Let another lookup put things right, if need be.
1143 #if defined(AFS_LINUX26_ENV)
1144 if (!d_unhashed(newdp)) {
1149 if (!list_empty(&newdp->d_hash)) {
1155 #if defined(AFS_LINUX24_ENV)
1156 if (atomic_read(&olddp->d_count) > 1)
1157 shrink_dcache_parent(olddp);
1161 code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1167 #if defined(AFS_LINUX26_ENV)
1176 /* afs_linux_ireadlink
1177 * Internal readlink which can return link contents to user or kernel space.
1178 * Note that the buffer is NOT supposed to be null-terminated.
1181 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1184 cred_t *credp = crref();
1188 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1189 code = afs_readlink(VTOAFS(ip), &tuio, credp);
1193 return maxlen - tuio.uio_resid;
1198 #if !defined(AFS_LINUX24_ENV)
1199 /* afs_linux_readlink
1200 * Fill target (which is in user space) with contents of symlink.
1203 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1206 struct inode *ip = dp->d_inode;
1209 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1215 /* afs_linux_follow_link
1216 * a file system dependent link following routine.
1218 static struct dentry *
1219 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1220 unsigned int follow)
1228 name = osi_Alloc(PATH_MAX + 1);
1232 return ERR_PTR(-EIO);
1235 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1240 res = ERR_PTR(code);
1243 res = lookup_dentry(name, basep, follow);
1247 osi_Free(name, PATH_MAX + 1);
1253 /* afs_linux_readpage
1254 * all reads come through here. A strategy-like read call.
1257 afs_linux_readpage(struct file *fp, struct page *pp)
1260 cred_t *credp = crref();
1261 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1263 afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1265 ulong address = afs_linux_page_address(pp);
1266 afs_offs_t offset = pageoff(pp);
1270 struct inode *ip = FILE_INODE(fp);
1271 int cnt = page_count(pp);
1272 struct vcache *avc = VTOAFS(ip);
1275 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1279 atomic_add(1, &pp->count);
1280 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1281 clear_bit(PG_error, &pp->flags);
1284 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1286 #ifdef AFS_LINUX24_ENV
1290 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 */
1291 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1292 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1293 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1296 #ifdef AFS_LINUX24_ENV
1301 if (tuio.uio_resid) /* zero remainder of page */
1302 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1304 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1305 flush_dcache_page(pp);
1306 SetPageUptodate(pp);
1308 set_bit(PG_uptodate, &pp->flags);
1312 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1316 clear_bit(PG_locked, &pp->flags);
1321 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1323 struct vrequest treq;
1326 code = afs_InitReq(&treq, credp);
1327 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1328 tdc = afs_FindDCache(avc, offset);
1330 if (!(tdc->mflags & DFNextStarted))
1331 afs_PrefetchChunk(avc, tdc, credp, &treq);
1334 ReleaseWriteLock(&avc->lock);
1344 #if defined(AFS_LINUX24_ENV)
1346 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1347 unsigned long offset, unsigned int count)
1349 struct vcache *vcp = VTOAFS(ip);
1358 buffer = kmap(pp) + offset;
1359 base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT) + offset;
1364 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1365 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1366 ICL_TYPE_INT32, 99999);
1368 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1370 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1372 ip->i_size = vcp->m.Length;
1373 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1376 && afs_stats_cmperf.cacheCurrDirtyChunks >
1377 afs_stats_cmperf.cacheMaxDirtyChunks) {
1378 struct vrequest treq;
1380 ObtainWriteLock(&vcp->lock, 533);
1381 if (!afs_InitReq(&treq, credp))
1382 code = afs_DoPartialWrite(vcp, &treq);
1383 ReleaseWriteLock(&vcp->lock);
1385 code = code ? -code : count - tuio.uio_resid;
1387 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1388 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1389 ICL_TYPE_INT32, code);
1401 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1402 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1404 afs_linux_writepage(struct page *pp)
1407 struct address_space *mapping = pp->mapping;
1408 struct inode *inode;
1409 unsigned long end_index;
1410 unsigned offset = PAGE_CACHE_SIZE;
1413 #if defined(AFS_LINUX26_ENV)
1414 if (PageReclaim(pp)) {
1415 # if defined(WRITEPAGE_ACTIVATE)
1416 return WRITEPAGE_ACTIVATE;
1418 return AOP_WRITEPAGE_ACTIVATE;
1422 if (PageLaunder(pp)) {
1423 return(fail_writepage(pp));
1427 inode = (struct inode *)mapping->host;
1428 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1431 if (pp->index < end_index)
1433 /* things got complicated... */
1434 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1435 /* OK, are we completely out? */
1436 if (pp->index >= end_index + 1 || !offset)
1439 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1440 SetPageUptodate(pp);
1442 if (status == offset)
1448 /* afs_linux_updatepage
1449 * What one would have thought was writepage - write dirty page to file.
1450 * Called from generic_file_write. buffer is still in user space. pagep
1451 * has been filled in with old data if we're updating less than a page.
1454 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1455 unsigned int count, int sync)
1457 struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1458 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1464 set_bit(PG_locked, &pp->flags);
1468 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1469 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1470 ICL_TYPE_INT32, 99999);
1471 setup_uio(&tuio, &iovec, page_addr + offset,
1472 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1475 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1477 ip->i_size = vcp->m.Length;
1478 ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1481 struct vrequest treq;
1483 ObtainWriteLock(&vcp->lock, 533);
1484 vcp->m.Date = osi_Time(); /* set modification time */
1485 if (!afs_InitReq(&treq, credp))
1486 code = afs_DoPartialWrite(vcp, &treq);
1487 ReleaseWriteLock(&vcp->lock);
1490 struct vrequest treq;
1492 ObtainWriteLock(&vcp->lock, 533);
1493 vcp->m.Date = osi_Time(); /* set modification time */
1494 if (!afs_InitReq(&treq, credp))
1495 code = afs_DoPartialWrite(vcp, &treq);
1496 ReleaseWriteLock(&vcp->lock);
1499 code = code ? -code : count - tuio.uio_resid;
1500 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1501 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1502 ICL_TYPE_INT32, code);
1507 clear_bit(PG_locked, &pp->flags);
1512 /* afs_linux_permission
1513 * Check access rights - returns error if can't check or permission denied.
1516 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1517 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1519 afs_linux_permission(struct inode *ip, int mode)
1523 cred_t *credp = crref();
1527 if (mode & MAY_EXEC)
1529 if (mode & MAY_READ)
1531 if (mode & MAY_WRITE)
1533 code = afs_access(VTOAFS(ip), tmp, credp);
1540 #if defined(AFS_LINUX24_ENV)
1542 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1547 code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1548 offset, to - offset);
1549 #if !defined(AFS_LINUX26_ENV)
1557 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1560 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1561 call kmap directly instead of relying on us to do it */
1562 #if !defined(AFS_LINUX26_ENV)
1568 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1571 static struct inode_operations afs_file_iops = {
1572 #if defined(AFS_LINUX26_ENV)
1573 .permission = afs_linux_permission,
1574 .getattr = afs_linux_getattr,
1575 .setattr = afs_notify_change,
1576 #elif defined(AFS_LINUX24_ENV)
1577 .permission = afs_linux_permission,
1578 .revalidate = afs_linux_revalidate,
1579 .setattr = afs_notify_change,
1581 .default_file_ops = &afs_file_fops,
1582 .readpage = afs_linux_readpage,
1583 .revalidate = afs_linux_revalidate,
1584 .updatepage = afs_linux_updatepage,
1588 #if defined(AFS_LINUX24_ENV)
1589 static struct address_space_operations afs_file_aops = {
1590 .readpage = afs_linux_readpage,
1591 .writepage = afs_linux_writepage,
1592 .commit_write = afs_linux_commit_write,
1593 .prepare_write = afs_linux_prepare_write,
1598 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1599 * by what sort of operation is allowed.....
1602 static struct inode_operations afs_dir_iops = {
1603 #if !defined(AFS_LINUX24_ENV)
1604 .default_file_ops = &afs_dir_fops,
1606 .setattr = afs_notify_change,
1608 .create = afs_linux_create,
1609 .lookup = afs_linux_lookup,
1610 .link = afs_linux_link,
1611 .unlink = afs_linux_unlink,
1612 .symlink = afs_linux_symlink,
1613 .mkdir = afs_linux_mkdir,
1614 .rmdir = afs_linux_rmdir,
1615 .rename = afs_linux_rename,
1616 #if defined(AFS_LINUX26_ENV)
1617 .getattr = afs_linux_getattr,
1619 .revalidate = afs_linux_revalidate,
1621 .permission = afs_linux_permission,
1624 /* We really need a separate symlink set of ops, since do_follow_link()
1625 * determines if it _is_ a link by checking if the follow_link op is set.
1627 #if defined(AFS_LINUX24_ENV)
1629 afs_symlink_filler(struct file *file, struct page *page)
1631 struct inode *ip = (struct inode *)page->mapping->host;
1632 char *p = (char *)kmap(page);
1637 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1642 p[code] = '\0'; /* null terminate? */
1645 SetPageUptodate(page);
1659 static struct address_space_operations afs_symlink_aops = {
1660 .readpage = afs_symlink_filler
1664 static struct inode_operations afs_symlink_iops = {
1665 #if defined(AFS_LINUX24_ENV)
1666 .readlink = page_readlink,
1667 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1668 .follow_link = page_follow_link,
1670 .follow_link = page_follow_link_light,
1671 .put_link = page_put_link,
1673 .setattr = afs_notify_change,
1675 .readlink = afs_linux_readlink,
1676 .follow_link = afs_linux_follow_link,
1677 .permission = afs_linux_permission,
1678 .revalidate = afs_linux_revalidate,
1683 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1687 vattr2inode(ip, vattr);
1689 /* Reset ops if symlink or directory. */
1690 if (S_ISREG(ip->i_mode)) {
1691 ip->i_op = &afs_file_iops;
1692 #if defined(AFS_LINUX24_ENV)
1693 ip->i_fop = &afs_file_fops;
1694 ip->i_data.a_ops = &afs_file_aops;
1697 } else if (S_ISDIR(ip->i_mode)) {
1698 ip->i_op = &afs_dir_iops;
1699 #if defined(AFS_LINUX24_ENV)
1700 ip->i_fop = &afs_dir_fops;
1703 } else if (S_ISLNK(ip->i_mode)) {
1704 ip->i_op = &afs_symlink_iops;
1705 #if defined(AFS_LINUX24_ENV)
1706 ip->i_data.a_ops = &afs_symlink_aops;
1707 ip->i_mapping = &ip->i_data;
1711 /* insert_inode_hash(ip); -- this would make iget() work (if we used it) */