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"
30 #include "afs/afs_osidnlc.h"
32 #ifdef HAVE_MM_INLINE_H
33 #include "h/mm_inline.h"
35 #include "h/pagemap.h"
36 #if defined(AFS_LINUX24_ENV)
37 #include "h/smp_lock.h"
39 #if defined(AFS_LINUX26_ENV)
40 #include "h/writeback.h"
44 #define pageoff(pp) pgoff2loff((pp)->index)
46 #define pageoff(pp) pp->offset
49 #if defined(AFS_LINUX26_ENV)
50 #define UnlockPage(pp) unlock_page(pp)
53 extern struct vcache *afs_globalVp;
54 extern afs_rwlock_t afs_xvcache;
56 #if defined(AFS_LINUX24_ENV)
57 extern struct inode_operations afs_file_iops;
58 extern struct address_space_operations afs_file_aops;
59 struct address_space_operations afs_symlink_aops;
61 extern struct inode_operations afs_dir_iops;
62 extern struct inode_operations afs_symlink_iops;
66 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
69 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
70 cred_t *credp = crref();
74 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
75 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
78 /* get a validated vcache entry */
79 code = afs_InitReq(&treq, credp);
81 code = afs_VerifyVCache(vcp, &treq);
86 #ifdef AFS_64BIT_CLIENT
87 if (*offp + count > afs_vmMappingEnd) {
92 if (*offp < afs_vmMappingEnd) {
93 /* special case of a buffer crossing the VM mapping end */
94 afs_int32 tcount = afs_vmMappingEnd - *offp;
96 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
98 code = generic_file_read(fp, buf, tcount, offp);
100 if (code != tcount) {
105 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
106 UIO_READ, AFS_UIOSYS);
107 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
108 xfered += count - tuio.uio_resid;
110 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
111 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
112 ICL_TYPE_INT32, code);
114 *offp += count - tuio.uio_resid;
122 #endif /* AFS_64BIT_CLIENT */
123 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
125 code = generic_file_read(fp, buf, count, offp);
127 #ifdef AFS_64BIT_CLIENT
129 #endif /* AFS_64BIT_CLIENT */
132 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
133 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
142 /* Now we have integrated VM for writes as well as reads. generic_file_write
143 * also takes care of re-positioning the pointer if file is open in append
144 * mode. Call fake open/close to ensure we do writes of core dumps.
147 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
151 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
152 struct vrequest treq;
153 cred_t *credp = crref();
158 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
159 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
160 (fp->f_flags & O_APPEND) ? 99998 : 99999);
163 /* get a validated vcache entry */
164 code = (ssize_t) afs_InitReq(&treq, credp);
166 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
168 ObtainWriteLock(&vcp->lock, 529);
170 ReleaseWriteLock(&vcp->lock);
174 #ifdef AFS_64BIT_CLIENT
176 if (fp->f_flags & O_APPEND)
177 toffs += vcp->m.Length;
178 if (toffs + count > afs_vmMappingEnd) {
181 afs_size_t oldOffset = *offp;
182 afs_int32 xfered = 0;
184 if (toffs < afs_vmMappingEnd) {
185 /* special case of a buffer crossing the VM mapping end */
186 afs_int32 tcount = afs_vmMappingEnd - *offp;
189 code = generic_file_write(fp, buf, tcount, offp);
191 if (code != tcount) {
197 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
198 UIO_WRITE, AFS_UIOSYS);
199 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
200 xfered += count - tuio.uio_resid;
203 *offp += count - tuio.uio_resid;
205 /* Purge dirty chunks of file if there are too many dirty chunks.
206 * Inside the write loop, we only do this at a chunk boundary.
207 * Clean up partial chunk if necessary at end of loop.
209 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
210 AFS_CHUNKBASE(oldOffset)) {
211 ObtainWriteLock(&vcp->lock, 402);
212 code = afs_DoPartialWrite(vcp, &treq);
213 vcp->states |= CDirty;
214 ReleaseWriteLock(&vcp->lock);
219 ObtainWriteLock(&vcp->lock, 400);
220 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
222 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
223 vcp->m.Length = toffs;
225 ReleaseWriteLock(&vcp->lock);
230 #endif /* AFS_64BIT_CLIENT */
232 code = generic_file_write(fp, buf, count, offp);
234 #ifdef AFS_64BIT_CLIENT
236 #endif /* AFS_64BIT_CLIENT */
239 ObtainWriteLock(&vcp->lock, 530);
240 vcp->m.Date = osi_Time(); /* set modification time */
241 afs_FakeClose(vcp, credp);
243 code2 = afs_DoPartialWrite(vcp, &treq);
244 if (code2 && code >= 0)
245 code = (ssize_t) - code2;
246 ReleaseWriteLock(&vcp->lock);
248 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
249 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
257 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
259 /* This is a complete rewrite of afs_readdir, since we can make use of
260 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
261 * handling and use of bulkstats will need to be reflected here as well.
264 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
266 extern struct DirEntry *afs_dir_GetBlob();
267 struct vcache *avc = ITOAFS(FILE_INODE(fp));
268 struct vrequest treq;
269 register struct dcache *tdc;
276 afs_size_t origOffset, tlen;
277 cred_t *credp = crref();
278 struct afs_fakestat_state fakestat;
280 #if defined(AFS_LINUX26_ENV)
284 AFS_STATCNT(afs_readdir);
286 code = afs_InitReq(&treq, credp);
291 afs_InitFakeStat(&fakestat);
292 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
296 /* update the cache entry */
298 code = afs_VerifyVCache(avc, &treq);
302 /* get a reference to the entire directory */
303 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
309 ObtainReadLock(&avc->lock);
310 ObtainReadLock(&tdc->lock);
312 * Make sure that the data in the cache is current. There are two
313 * cases we need to worry about:
314 * 1. The cache data is being fetched by another process.
315 * 2. The cache data is no longer valid
317 while ((avc->states & CStatd)
318 && (tdc->dflags & DFFetching)
319 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
320 ReleaseReadLock(&tdc->lock);
321 ReleaseReadLock(&avc->lock);
322 afs_osi_Sleep(&tdc->validPos);
323 ObtainReadLock(&avc->lock);
324 ObtainReadLock(&tdc->lock);
326 if (!(avc->states & CStatd)
327 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
328 ReleaseReadLock(&tdc->lock);
329 ReleaseReadLock(&avc->lock);
334 /* Fill in until we get an error or we're done. This implementation
335 * takes an offset in units of blobs, rather than bytes.
338 offset = (int) fp->f_pos;
340 dirpos = BlobScan(tdc, offset);
344 de = afs_dir_GetBlob(tdc, dirpos);
348 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
349 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
351 len = strlen(de->name);
353 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n",
354 (unsigned long)&tdc->f.inode, dirpos);
355 DRelease((struct buffer *) de, 0);
357 ReleaseReadLock(&avc->lock);
362 /* filldir returns -EINVAL when the buffer is full. */
363 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
365 unsigned int type = DT_UNKNOWN;
366 struct VenusFid afid;
369 afid.Cell = avc->fid.Cell;
370 afid.Fid.Volume = avc->fid.Fid.Volume;
371 afid.Fid.Vnode = ntohl(de->fid.vnode);
372 afid.Fid.Unique = ntohl(de->fid.vunique);
373 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
375 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
378 } else if (((tvc->states) & (CStatd | CTruth))) {
379 /* CTruth will be set if the object has
384 else if (vtype == VREG)
386 /* Don't do this until we're sure it can't be a mtpt */
387 /* else if (vtype == VLNK)
389 /* what other types does AFS support? */
391 /* clean up from afs_FindVCache */
394 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
397 code = (*filldir) (dirbuf, de->name, len, offset, ino);
399 DRelease((struct buffer *)de, 0);
402 offset = dirpos + 1 + ((len + 16) >> 5);
404 /* If filldir didn't fill in the last one this is still pointing to that
407 fp->f_pos = (loff_t) offset;
409 ReleaseReadLock(&tdc->lock);
411 ReleaseReadLock(&avc->lock);
415 afs_PutFakeStat(&fakestat);
418 #if defined(AFS_LINUX26_ENV)
425 /* in afs_pioctl.c */
426 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
430 /* We need to detect unmap's after close. To do that, we need our own
431 * vm_operations_struct's. And we need to set them up for both the
432 * private and shared mappings. The fun part is that these are all static
433 * so we'll have to initialize on the fly!
435 static struct vm_operations_struct afs_private_mmap_ops;
436 static int afs_private_mmap_ops_inited = 0;
437 static struct vm_operations_struct afs_shared_mmap_ops;
438 static int afs_shared_mmap_ops_inited = 0;
441 afs_linux_vma_close(struct vm_area_struct *vmap)
450 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
455 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
456 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
457 ICL_TYPE_INT32, vcp->execsOrWriters);
458 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
459 ObtainWriteLock(&vcp->lock, 532);
462 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
463 MyPidxx, &vcp, &vmap);
467 ReleaseWriteLock(&vcp->lock);
469 if (need_unlock && vcp->execsOrWriters < 2) {
471 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
472 /* only decrement the execsOrWriters flag if this is not a
474 if (!(vcp->states & CRO) )
475 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
476 vcp->execsOrWriters--;
477 vcp->states &= ~CMAPPED;
479 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
480 vcp->execsOrWriters--;
481 /* If we did not have the lock */
484 if (!vcp->execsOrWriters)
485 vcp->execsOrWriters = 1;
490 ReleaseWriteLock(&vcp->lock);
497 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
499 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
500 cred_t *credp = crref();
501 struct vrequest treq;
505 #if defined(AFS_LINUX24_ENV)
506 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
507 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
508 vmap->vm_end - vmap->vm_start);
510 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
511 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
512 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
516 /* get a validated vcache entry */
517 code = afs_InitReq(&treq, credp);
519 code = afs_VerifyVCache(vcp, &treq);
525 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
528 code = generic_file_mmap(fp, vmap);
533 ObtainWriteLock(&vcp->lock, 531);
534 /* Set out vma ops so we catch the close. The following test should be
535 * the same as used in generic_file_mmap.
537 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
538 if (!afs_shared_mmap_ops_inited) {
539 afs_shared_mmap_ops_inited = 1;
540 afs_shared_mmap_ops = *vmap->vm_ops;
541 afs_shared_mmap_ops.close = afs_linux_vma_close;
543 vmap->vm_ops = &afs_shared_mmap_ops;
545 if (!afs_private_mmap_ops_inited) {
546 afs_private_mmap_ops_inited = 1;
547 afs_private_mmap_ops = *vmap->vm_ops;
548 afs_private_mmap_ops.close = afs_linux_vma_close;
550 vmap->vm_ops = &afs_private_mmap_ops;
554 /* Add an open reference on the first mapping. */
555 if (vcp->mapcnt == 0) {
556 if (!(vcp->states & CRO))
557 vcp->execsOrWriters++;
559 vcp->states |= CMAPPED;
561 ReleaseWriteLock(&vcp->lock);
571 afs_linux_open(struct inode *ip, struct file *fp)
574 cred_t *credp = crref();
576 #ifdef AFS_LINUX24_ENV
580 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
582 #ifdef AFS_LINUX24_ENV
590 /* afs_Close is called from release, since release is used to handle all
591 * file closings. In addition afs_linux_flush is called from sys_close to
592 * handle flushing the data back to the server. The kicker is that we could
593 * ignore flush completely if only sys_close took it's return value from
594 * fput. See afs_linux_flush for notes on interactions between release and
598 afs_linux_release(struct inode *ip, struct file *fp)
601 cred_t *credp = crref();
602 struct vcache *vcp = ITOAFS(ip);
604 #ifdef AFS_LINUX24_ENV
609 vcp->flushcnt--; /* protected by AFS global lock. */
611 code = afs_close(vcp, fp->f_flags, credp);
614 #ifdef AFS_LINUX24_ENV
622 #if defined(AFS_LINUX24_ENV)
624 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
627 afs_linux_fsync(struct file *fp, struct dentry *dp)
631 struct inode *ip = FILE_INODE(fp);
632 cred_t *credp = crref();
634 #ifdef AFS_LINUX24_ENV
638 code = afs_fsync(ITOAFS(ip), credp);
640 #ifdef AFS_LINUX24_ENV
650 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
653 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
654 cred_t *credp = crref();
655 struct AFS_FLOCK flock;
656 /* Convert to a lock format afs_lockctl understands. */
657 memset((char *)&flock, 0, sizeof(flock));
658 flock.l_type = flp->fl_type;
659 flock.l_pid = flp->fl_pid;
661 flock.l_start = flp->fl_start;
662 flock.l_len = flp->fl_end - flp->fl_start;
664 /* Safe because there are no large files, yet */
665 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
666 if (cmd == F_GETLK64)
668 else if (cmd == F_SETLK64)
670 else if (cmd == F_SETLKW64)
672 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
675 code = afs_lockctl(vcp, &flock, cmd, credp);
678 /* Convert flock back to Linux's file_lock */
679 flp->fl_type = flock.l_type;
680 flp->fl_pid = flock.l_pid;
681 flp->fl_start = flock.l_start;
682 flp->fl_end = flock.l_start + flock.l_len;
690 * flush is called from sys_close. We could ignore it, but sys_close return
691 * code comes from flush, not release. We need to use release to keep
692 * the vcache open count correct. Note that flush is called before release
693 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
694 * races and also avoid calling afs_close twice when closing the file.
695 * If we merely checked for opens > 0 in afs_linux_release, then if an
696 * new open occurred when storing back the file, afs_linux_release would
697 * incorrectly close the file and decrement the opens count. Calling afs_close
698 * on the just flushed file is wasteful, since the background daemon will
699 * execute the code that finally decides there is nothing to do.
702 afs_linux_flush(struct file *fp)
704 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
708 /* Only do this on the last close of the file pointer. */
709 #if defined(AFS_LINUX24_ENV)
710 if (atomic_read(&fp->f_count) > 1)
719 code = afs_close(vcp, fp->f_flags, credp);
720 vcp->flushcnt++; /* protected by AFS global lock. */
727 #if !defined(AFS_LINUX24_ENV)
728 /* Not allowed to directly read a directory. */
730 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
738 struct file_operations afs_dir_fops = {
739 #if !defined(AFS_LINUX24_ENV)
740 .read = afs_linux_dir_read,
741 .lock = afs_linux_lock,
742 .fsync = afs_linux_fsync,
744 .read = generic_read_dir,
746 .readdir = afs_linux_readdir,
748 .open = afs_linux_open,
749 .release = afs_linux_release,
752 struct file_operations afs_file_fops = {
753 .read = afs_linux_read,
754 .write = afs_linux_write,
756 .mmap = afs_linux_mmap,
757 .open = afs_linux_open,
758 .flush = afs_linux_flush,
759 .release = afs_linux_release,
760 .fsync = afs_linux_fsync,
761 .lock = afs_linux_lock,
765 /**********************************************************************
766 * AFS Linux dentry operations
767 **********************************************************************/
769 /* afs_linux_revalidate
770 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
773 afs_linux_revalidate(struct dentry *dp)
777 struct vrequest treq;
778 struct vcache *vcp = ITOAFS(dp->d_inode);
779 struct vcache *rootvp = NULL;
781 #ifdef AFS_LINUX24_ENV
786 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
787 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
788 ObtainSharedLock(&afs_xvcache, 680);
789 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
790 ReleaseSharedLock(&afs_xvcache);
793 /* Make this a fast path (no crref), since it's called so often. */
794 if (vcp->states & CStatd) {
795 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
796 check_bad_parent(dp); /* check and correct mvid */
798 vcache2fakeinode(rootvp, vcp);
802 afs_PutVCache(rootvp);
804 #ifdef AFS_LINUX24_ENV
811 code = afs_InitReq(&treq, credp);
813 code = afs_VerifyVCache(vcp, &treq);
816 #ifdef AFS_LINUX24_ENV
824 #if defined(AFS_LINUX26_ENV)
826 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
828 int err = afs_linux_revalidate(dentry);
830 generic_fillattr(dentry->d_inode, stat);
835 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
836 * In kernels 2.2.10 and above, we are passed an additional flags var which
837 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
838 * we are advised to follow the entry if it is a link or to make sure that
839 * it is a directory. But since the kernel itself checks these possibilities
840 * later on, we shouldn't have to do it until later. Perhaps in the future..
842 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
843 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
845 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
848 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
852 afs_linux_dentry_revalidate(struct dentry *dp)
856 cred_t *credp = crref();
857 struct vrequest treq;
858 struct vcache *lookupvcp = NULL;
859 int code, bad_dentry = 1;
860 struct sysname_info sysState;
861 struct vcache *vcp, *parentvcp;
863 sysState.allocked = 0;
865 #ifdef AFS_LINUX24_ENV
870 vcp = ITOAFS(dp->d_inode);
871 parentvcp = ITOAFS(dp->d_parent->d_inode);
873 /* If it's a negative dentry, then there's nothing to do. */
874 if (!vcp || !parentvcp)
877 /* If it is the AFS root, then there's no chance it needs
879 if (vcp == afs_globalVp) {
884 if ((code = afs_InitReq(&treq, credp)))
887 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
888 name = sysState.name;
890 /* First try looking up the DNLC */
891 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
892 /* Verify that the dentry does not point to an old inode */
893 if (vcp != lookupvcp)
895 /* Check and correct mvid */
896 if (*name != '/' && vcp->mvstat == 2)
897 check_bad_parent(dp);
903 /* A DNLC lookup failure cannot be trusted. Try a real lookup.
904 Make sure to try the real name and not the @sys expansion;
905 afs_lookup will expand @sys itself. */
907 code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
909 /* Verify that the dentry does not point to an old inode */
910 if (vcp != lookupvcp)
918 afs_PutVCache(lookupvcp);
919 if (sysState.allocked)
920 osi_FreeLargeSpace(name);
925 shrink_dcache_parent(dp);
929 #ifdef AFS_LINUX24_ENV
937 #if !defined(AFS_LINUX26_ENV)
938 /* afs_dentry_iput */
940 afs_dentry_iput(struct dentry *dp, struct inode *ip)
944 if (ICL_SETACTIVE(afs_iclSetp)) {
945 isglock = ISAFS_GLOCK();
946 if (!isglock) AFS_GLOCK();
947 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
948 ICL_TYPE_STRING, dp->d_parent->d_name.name,
949 ICL_TYPE_STRING, dp->d_name.name);
950 if (!isglock) AFS_GUNLOCK();
958 afs_dentry_delete(struct dentry *dp)
961 if (ICL_SETACTIVE(afs_iclSetp)) {
962 isglock = ISAFS_GLOCK();
963 if (!isglock) AFS_GLOCK();
964 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
965 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
966 ICL_TYPE_STRING, dp->d_name.name);
967 if (!isglock) AFS_GUNLOCK();
970 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
971 return 1; /* bad inode? */
976 struct dentry_operations afs_dentry_operations = {
977 .d_revalidate = afs_linux_dentry_revalidate,
978 .d_delete = afs_dentry_delete,
979 #if !defined(AFS_LINUX26_ENV)
980 .d_iput = afs_dentry_iput,
984 /**********************************************************************
985 * AFS Linux inode operations
986 **********************************************************************/
990 * Merely need to set enough of vattr to get us through the create. Note
991 * that the higher level code (open_namei) will take care of any tuncation
992 * explicitly. Exclusive open is also taken care of in open_namei.
994 * name is in kernel space at this point.
996 #ifdef IOP_CREATE_TAKES_NAMEIDATA
998 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
999 struct nameidata *nd)
1002 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1006 cred_t *credp = crref();
1008 const char *name = dp->d_name.name;
1012 vattr.va_mode = mode;
1014 #if defined(AFS_LINUX26_ENV)
1019 afs_create(ITOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1020 (struct vcache **)&ip, credp);
1023 vattr2inode(ip, &vattr);
1024 /* Reset ops if symlink or directory. */
1025 #if defined(AFS_LINUX24_ENV)
1026 if (S_ISREG(ip->i_mode)) {
1027 ip->i_op = &afs_file_iops;
1028 ip->i_fop = &afs_file_fops;
1029 ip->i_data.a_ops = &afs_file_aops;
1030 } else if (S_ISDIR(ip->i_mode)) {
1031 ip->i_op = &afs_dir_iops;
1032 ip->i_fop = &afs_dir_fops;
1033 } else if (S_ISLNK(ip->i_mode)) {
1034 ip->i_op = &afs_symlink_iops;
1035 ip->i_data.a_ops = &afs_symlink_aops;
1036 ip->i_mapping = &ip->i_data;
1038 printk("afs_linux_create: FIXME\n");
1040 if (S_ISDIR(ip->i_mode))
1041 ip->i_op = &afs_dir_iops;
1042 else if (S_ISLNK(ip->i_mode))
1043 ip->i_op = &afs_symlink_iops;
1046 dp->d_op = &afs_dentry_operations;
1047 dp->d_time = jiffies;
1048 d_instantiate(dp, ip);
1052 #if defined(AFS_LINUX26_ENV)
1059 /* afs_linux_lookup */
1060 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1061 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1063 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1064 struct nameidata *nd)
1067 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1071 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1075 cred_t *credp = crref();
1076 struct vcache *vcp = NULL;
1077 const char *comp = dp->d_name.name;
1079 #if defined(AFS_LINUX26_ENV)
1083 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1087 struct inode *ip = AFSTOI(vcp);
1088 /* Reset ops if symlink or directory. */
1089 #if defined(AFS_LINUX24_ENV)
1090 if (S_ISREG(ip->i_mode)) {
1091 ip->i_op = &afs_file_iops;
1092 ip->i_fop = &afs_file_fops;
1093 ip->i_data.a_ops = &afs_file_aops;
1094 } else if (S_ISDIR(ip->i_mode)) {
1095 ip->i_op = &afs_dir_iops;
1096 ip->i_fop = &afs_dir_fops;
1097 } else if (S_ISLNK(ip->i_mode)) {
1098 ip->i_op = &afs_symlink_iops;
1099 ip->i_data.a_ops = &afs_symlink_aops;
1100 ip->i_mapping = &ip->i_data;
1103 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1104 ip->i_mode, dp->d_name.name, code);
1105 #ifdef STRUCT_INODE_HAS_I_SECURITY
1106 if (ip->i_security == NULL) {
1107 if (security_inode_alloc(ip))
1108 panic("afs_linux_lookup: Cannot allocate inode security");
1112 if (S_ISDIR(ip->i_mode))
1113 ip->i_op = &afs_dir_iops;
1114 else if (S_ISLNK(ip->i_mode))
1115 ip->i_op = &afs_symlink_iops;
1118 dp->d_time = jiffies;
1119 dp->d_op = &afs_dentry_operations;
1120 d_add(dp, AFSTOI(vcp));
1122 #if defined(AFS_LINUX26_ENV)
1127 /* It's ok for the file to not be found. That's noted by the caller by
1128 * seeing that the dp->d_inode field is NULL.
1130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1134 return ERR_PTR(-code);
1143 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1146 cred_t *credp = crref();
1147 const char *name = newdp->d_name.name;
1148 struct inode *oldip = olddp->d_inode;
1150 /* If afs_link returned the vnode, we could instantiate the
1151 * dentry. Since it's not, we drop this one and do a new lookup.
1156 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1164 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1167 cred_t *credp = crref();
1168 const char *name = dp->d_name.name;
1169 struct vcache *tvc = ITOAFS(dp->d_inode);
1171 #if defined(AFS_LINUX26_ENV)
1174 if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
1175 && !(tvc->states & CUnlinked)) {
1176 struct dentry *__dp;
1178 extern char *afs_newname();
1187 osi_FreeSmallSpace(__name);
1188 __name = afs_newname();
1191 __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1195 } while (__dp->d_inode != NULL);
1198 code = afs_rename(ITOAFS(dip), dp->d_name.name, ITOAFS(dip), __dp->d_name.name, credp);
1203 crfree(tvc->uncred);
1205 tvc->uncred = credp;
1206 tvc->states |= CUnlinked;
1218 code = afs_remove(ITOAFS(dip), name, credp);
1223 #if defined(AFS_LINUX26_ENV)
1232 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1235 cred_t *credp = crref();
1237 const char *name = dp->d_name.name;
1239 /* If afs_symlink returned the vnode, we could instantiate the
1240 * dentry. Since it's not, we drop this one and do a new lookup.
1246 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1253 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1256 cred_t *credp = crref();
1257 struct vcache *tvcp = NULL;
1259 const char *name = dp->d_name.name;
1261 #if defined(AFS_LINUX26_ENV)
1266 vattr.va_mask = ATTR_MODE;
1267 vattr.va_mode = mode;
1268 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1272 tvcp->v.v_op = &afs_dir_iops;
1273 #if defined(AFS_LINUX24_ENV)
1274 tvcp->v.v_fop = &afs_dir_fops;
1276 dp->d_op = &afs_dentry_operations;
1277 dp->d_time = jiffies;
1278 d_instantiate(dp, AFSTOI(tvcp));
1281 #if defined(AFS_LINUX26_ENV)
1289 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1292 cred_t *credp = crref();
1293 const char *name = dp->d_name.name;
1295 #if defined(AFS_LINUX26_ENV)
1299 code = afs_rmdir(ITOAFS(dip), name, credp);
1302 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1303 * that failed because a directory is not empty. So, we map
1304 * EEXIST to ENOTEMPTY on linux.
1306 if (code == EEXIST) {
1314 #if defined(AFS_LINUX26_ENV)
1324 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1325 struct inode *newip, struct dentry *newdp)
1328 cred_t *credp = crref();
1329 const char *oldname = olddp->d_name.name;
1330 const char *newname = newdp->d_name.name;
1332 #if defined(AFS_LINUX26_ENV)
1335 /* Remove old and new entries from name hash. New one will change below.
1336 * While it's optimal to catch failures and re-insert newdp into hash,
1337 * it's also error prone and in that case we're already dealing with error
1338 * cases. Let another lookup put things right, if need be.
1340 #if defined(AFS_LINUX26_ENV)
1341 if (!d_unhashed(olddp))
1343 if (!d_unhashed(newdp))
1346 if (!list_empty(&olddp->d_hash))
1348 if (!list_empty(&newdp->d_hash))
1352 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1356 /* update time so it doesn't expire immediately */
1357 newdp->d_time = jiffies;
1358 d_move(olddp, newdp);
1361 #if defined(AFS_LINUX26_ENV)
1370 /* afs_linux_ireadlink
1371 * Internal readlink which can return link contents to user or kernel space.
1372 * Note that the buffer is NOT supposed to be null-terminated.
1375 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1378 cred_t *credp = crref();
1382 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1383 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1387 return maxlen - tuio.uio_resid;
1392 #if !defined(AFS_LINUX24_ENV)
1393 /* afs_linux_readlink
1394 * Fill target (which is in user space) with contents of symlink.
1397 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1400 struct inode *ip = dp->d_inode;
1403 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1409 /* afs_linux_follow_link
1410 * a file system dependent link following routine.
1413 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1414 unsigned int follow)
1422 name = osi_Alloc(PATH_MAX + 1);
1426 return ERR_PTR(-EIO);
1429 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1434 res = ERR_PTR(code);
1437 res = lookup_dentry(name, basep, follow);
1441 osi_Free(name, PATH_MAX + 1);
1447 /* afs_linux_readpage
1448 * all reads come through here. A strategy-like read call.
1451 afs_linux_readpage(struct file *fp, struct page *pp)
1454 cred_t *credp = crref();
1455 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1457 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1459 ulong address = afs_linux_page_address(pp);
1460 afs_offs_t offset = pageoff(pp);
1464 struct inode *ip = FILE_INODE(fp);
1465 int cnt = page_count(pp);
1466 struct vcache *avc = ITOAFS(ip);
1469 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1473 atomic_add(1, &pp->count);
1474 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1475 clear_bit(PG_error, &pp->flags);
1478 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1480 #ifdef AFS_LINUX24_ENV
1484 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 */
1485 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1486 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1487 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1490 #ifdef AFS_LINUX24_ENV
1495 if (tuio.uio_resid) /* zero remainder of page */
1496 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1498 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1499 flush_dcache_page(pp);
1500 SetPageUptodate(pp);
1502 set_bit(PG_uptodate, &pp->flags);
1506 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1510 clear_bit(PG_locked, &pp->flags);
1515 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1517 struct vrequest treq;
1520 code = afs_InitReq(&treq, credp);
1521 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1522 tdc = afs_FindDCache(avc, offset);
1524 if (!(tdc->mflags & DFNextStarted))
1525 afs_PrefetchChunk(avc, tdc, credp, &treq);
1528 ReleaseWriteLock(&avc->lock);
1537 #if defined(AFS_LINUX24_ENV)
1538 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1540 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1543 afs_linux_writepage(struct page *pp)
1546 struct address_space *mapping = pp->mapping;
1547 struct inode *inode;
1548 unsigned long end_index;
1549 unsigned offset = PAGE_CACHE_SIZE;
1552 #if defined(AFS_LINUX26_ENV)
1553 if (PageReclaim(pp)) {
1554 return WRITEPAGE_ACTIVATE;
1557 if (PageLaunder(pp)) {
1558 return(fail_writepage(pp));
1562 inode = (struct inode *)mapping->host;
1563 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1566 if (pp->index < end_index)
1568 /* things got complicated... */
1569 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1570 /* OK, are we completely out? */
1571 if (pp->index >= end_index + 1 || !offset)
1575 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1577 SetPageUptodate(pp);
1579 if (status == offset)
1586 /* afs_linux_permission
1587 * Check access rights - returns error if can't check or permission denied.
1589 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1591 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1594 afs_linux_permission(struct inode *ip, int mode)
1598 cred_t *credp = crref();
1602 if (mode & MAY_EXEC)
1604 if (mode & MAY_READ)
1606 if (mode & MAY_WRITE)
1608 code = afs_access(ITOAFS(ip), tmp, credp);
1616 #if defined(AFS_LINUX24_ENV)
1618 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1619 unsigned long offset, unsigned int count)
1621 struct vcache *vcp = ITOAFS(ip);
1630 buffer = kmap(pp) + offset;
1631 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1634 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1635 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1636 ICL_TYPE_INT32, 99999);
1638 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1640 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1645 && afs_stats_cmperf.cacheCurrDirtyChunks >
1646 afs_stats_cmperf.cacheMaxDirtyChunks) {
1647 struct vrequest treq;
1649 ObtainWriteLock(&vcp->lock, 533);
1650 if (!afs_InitReq(&treq, credp))
1651 code = afs_DoPartialWrite(vcp, &treq);
1652 ReleaseWriteLock(&vcp->lock);
1654 code = code ? -code : count - tuio.uio_resid;
1656 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1657 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1658 ICL_TYPE_INT32, code);
1667 afs_linux_updatepage(struct file *file, struct page *page,
1668 unsigned long offset, unsigned int count)
1670 struct dentry *dentry = file->f_dentry;
1672 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1675 /* afs_linux_updatepage
1676 * What one would have thought was writepage - write dirty page to file.
1677 * Called from generic_file_write. buffer is still in user space. pagep
1678 * has been filled in with old data if we're updating less than a page.
1681 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1682 unsigned int count, int sync)
1684 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1685 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1691 set_bit(PG_locked, &pp->flags);
1695 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1696 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1697 ICL_TYPE_INT32, 99999);
1698 setup_uio(&tuio, &iovec, page_addr + offset,
1699 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1702 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1706 code = code ? -code : count - tuio.uio_resid;
1707 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1708 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1709 ICL_TYPE_INT32, code);
1714 clear_bit(PG_locked, &pp->flags);
1719 #if defined(AFS_LINUX24_ENV)
1721 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1728 code = afs_linux_updatepage(file, page, offset, to - offset);
1737 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1744 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1747 struct inode_operations afs_file_iops = {
1748 #if defined(AFS_LINUX26_ENV)
1749 .permission = afs_linux_permission,
1750 .getattr = afs_linux_getattr,
1751 .setattr = afs_notify_change,
1752 #elif defined(AFS_LINUX24_ENV)
1753 .permission = afs_linux_permission,
1754 .revalidate = afs_linux_revalidate,
1755 .setattr = afs_notify_change,
1757 .default_file_ops = &afs_file_fops,
1758 .readpage = afs_linux_readpage,
1759 .revalidate = afs_linux_revalidate,
1760 .updatepage = afs_linux_updatepage,
1764 #if defined(AFS_LINUX24_ENV)
1765 struct address_space_operations afs_file_aops = {
1766 .readpage = afs_linux_readpage,
1767 .writepage = afs_linux_writepage,
1768 .commit_write = afs_linux_commit_write,
1769 .prepare_write = afs_linux_prepare_write,
1774 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1775 * by what sort of operation is allowed.....
1778 struct inode_operations afs_dir_iops = {
1779 #if !defined(AFS_LINUX24_ENV)
1780 .default_file_ops = &afs_dir_fops,
1782 .setattr = afs_notify_change,
1784 .create = afs_linux_create,
1785 .lookup = afs_linux_lookup,
1786 .link = afs_linux_link,
1787 .unlink = afs_linux_unlink,
1788 .symlink = afs_linux_symlink,
1789 .mkdir = afs_linux_mkdir,
1790 .rmdir = afs_linux_rmdir,
1791 .rename = afs_linux_rename,
1792 #if defined(AFS_LINUX26_ENV)
1793 .getattr = afs_linux_getattr,
1795 .revalidate = afs_linux_revalidate,
1797 .permission = afs_linux_permission,
1800 /* We really need a separate symlink set of ops, since do_follow_link()
1801 * determines if it _is_ a link by checking if the follow_link op is set.
1803 #if defined(AFS_LINUX24_ENV)
1805 afs_symlink_filler(struct file *file, struct page *page)
1807 struct inode *ip = (struct inode *)page->mapping->host;
1808 char *p = (char *)kmap(page);
1813 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1818 p[code] = '\0'; /* null terminate? */
1821 SetPageUptodate(page);
1835 struct address_space_operations afs_symlink_aops = {
1836 .readpage = afs_symlink_filler
1840 struct inode_operations afs_symlink_iops = {
1841 #if defined(AFS_LINUX24_ENV)
1842 .readlink = page_readlink,
1843 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1844 .follow_link = page_follow_link,
1846 .follow_link = page_follow_link_light,
1848 .setattr = afs_notify_change,
1850 .readlink = afs_linux_readlink,
1851 .follow_link = afs_linux_follow_link,
1852 .permission = afs_linux_permission,
1853 .revalidate = afs_linux_revalidate,