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 #include "h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "h/smp_lock.h"
38 #define pageoff(pp) pgoff2loff((pp)->index)
40 #define pageoff(pp) pp->offset
43 #if defined(AFS_LINUX26_ENV)
44 #define UnlockPage(pp) unlock_page(pp)
47 extern struct vcache *afs_globalVp;
48 extern afs_rwlock_t afs_xvcache;
50 #if defined(AFS_LINUX24_ENV)
51 extern struct inode_operations afs_file_iops;
52 extern struct address_space_operations afs_file_aops;
53 struct address_space_operations afs_symlink_aops;
55 extern struct inode_operations afs_dir_iops;
56 extern struct inode_operations afs_symlink_iops;
60 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
63 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
64 cred_t *credp = crref();
68 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
69 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
72 /* get a validated vcache entry */
73 code = afs_InitReq(&treq, credp);
75 code = afs_VerifyVCache(vcp, &treq);
80 #ifdef AFS_64BIT_CLIENT
81 if (*offp + count > afs_vmMappingEnd) {
84 afs_size_t oldOffset = *offp;
87 if (*offp < afs_vmMappingEnd) {
88 /* special case of a buffer crossing the VM mapping end */
89 afs_int32 tcount = afs_vmMappingEnd - *offp;
91 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
93 code = generic_file_read(fp, buf, tcount, offp);
100 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
101 UIO_READ, AFS_UIOSYS);
102 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
103 xfered += count - tuio.uio_resid;
105 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
106 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
107 ICL_TYPE_INT32, code);
109 *offp += count - tuio.uio_resid;
117 #endif /* AFS_64BIT_CLIENT */
118 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
120 code = generic_file_read(fp, buf, count, offp);
122 #ifdef AFS_64BIT_CLIENT
124 #endif /* AFS_64BIT_CLIENT */
127 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
128 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
137 /* Now we have integrated VM for writes as well as reads. generic_file_write
138 * also takes care of re-positioning the pointer if file is open in append
139 * mode. Call fake open/close to ensure we do writes of core dumps.
142 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
146 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
147 struct vrequest treq;
148 cred_t *credp = crref();
153 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
154 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
155 (fp->f_flags & O_APPEND) ? 99998 : 99999);
158 /* get a validated vcache entry */
159 code = (ssize_t) afs_InitReq(&treq, credp);
161 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
163 ObtainWriteLock(&vcp->lock, 529);
165 ReleaseWriteLock(&vcp->lock);
169 #ifdef AFS_64BIT_CLIENT
171 if (fp->f_flags & O_APPEND)
172 toffs += vcp->m.Length;
173 if (toffs + count > afs_vmMappingEnd) {
176 afs_size_t oldOffset = *offp;
177 afs_int32 xfered = 0;
179 if (toffs < afs_vmMappingEnd) {
180 /* special case of a buffer crossing the VM mapping end */
181 afs_int32 tcount = afs_vmMappingEnd - *offp;
184 code = generic_file_write(fp, buf, tcount, offp);
186 if (code != tcount) {
192 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
193 UIO_WRITE, AFS_UIOSYS);
194 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
195 xfered += count - tuio.uio_resid;
198 *offp += count - tuio.uio_resid;
200 /* Purge dirty chunks of file if there are too many dirty chunks.
201 * Inside the write loop, we only do this at a chunk boundary.
202 * Clean up partial chunk if necessary at end of loop.
204 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
205 AFS_CHUNKBASE(oldOffset)) {
206 ObtainWriteLock(&vcp->lock, 402);
207 code = afs_DoPartialWrite(vcp, &treq);
208 vcp->states |= CDirty;
209 ReleaseWriteLock(&vcp->lock);
214 ObtainWriteLock(&vcp->lock, 400);
215 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
217 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
218 vcp->m.Length = toffs;
220 ReleaseWriteLock(&vcp->lock);
225 #endif /* AFS_64BIT_CLIENT */
227 code = generic_file_write(fp, buf, count, offp);
229 #ifdef AFS_64BIT_CLIENT
231 #endif /* AFS_64BIT_CLIENT */
234 ObtainWriteLock(&vcp->lock, 530);
235 vcp->m.Date = osi_Time(); /* set modification time */
236 afs_FakeClose(vcp, credp);
238 code2 = afs_DoPartialWrite(vcp, &treq);
239 if (code2 && code >= 0)
240 code = (ssize_t) - code2;
241 ReleaseWriteLock(&vcp->lock);
243 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
244 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
252 /* This is a complete rewrite of afs_readdir, since we can make use of
253 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
254 * handling and use of bulkstats will need to be reflected here as well.
257 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
259 extern struct DirEntry *afs_dir_GetBlob();
260 struct vcache *avc = ITOAFS(FILE_INODE(fp));
261 struct vrequest treq;
262 register struct dcache *tdc;
269 afs_size_t origOffset, tlen;
270 cred_t *credp = crref();
271 struct afs_fakestat_state fakestat;
274 #if defined(AFS_LINUX26_ENV)
277 AFS_STATCNT(afs_readdir);
279 code = afs_InitReq(&treq, credp);
284 afs_InitFakeStat(&fakestat);
285 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
289 /* update the cache entry */
291 code = afs_VerifyVCache(avc, &treq);
295 /* get a reference to the entire directory */
296 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
302 ObtainReadLock(&avc->lock);
303 ObtainReadLock(&tdc->lock);
305 * Make sure that the data in the cache is current. There are two
306 * cases we need to worry about:
307 * 1. The cache data is being fetched by another process.
308 * 2. The cache data is no longer valid
310 while ((avc->states & CStatd)
311 && (tdc->dflags & DFFetching)
312 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
313 ReleaseReadLock(&tdc->lock);
314 ReleaseReadLock(&avc->lock);
315 afs_osi_Sleep(&tdc->validPos);
316 ObtainReadLock(&avc->lock);
317 ObtainReadLock(&tdc->lock);
319 if (!(avc->states & CStatd)
320 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
321 ReleaseReadLock(&tdc->lock);
322 ReleaseReadLock(&avc->lock);
327 /* Fill in until we get an error or we're done. This implementation
328 * takes an offset in units of blobs, rather than bytes.
331 offset = (int) fp->f_pos;
333 dirpos = BlobScan(&tdc->f.inode, offset);
337 de = afs_dir_GetBlob(&tdc->f.inode, dirpos);
341 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
342 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
344 len = strlen(de->name);
346 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %x, dirpos %d)\n",
347 &tdc->f.inode, dirpos);
348 DRelease((struct buffer *) de, 0);
350 ReleaseReadLock(&avc->lock);
355 /* filldir returns -EINVAL when the buffer is full. */
356 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
358 unsigned int type = DT_UNKNOWN;
359 struct VenusFid afid;
362 afid.Cell = avc->fid.Cell;
363 afid.Fid.Volume = avc->fid.Fid.Volume;
364 afid.Fid.Vnode = ntohl(de->fid.vnode);
365 afid.Fid.Unique = ntohl(de->fid.vunique);
366 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
368 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
371 } else if (((tvc->states) & (CStatd | CTruth))) {
372 /* CTruth will be set if the object has
377 else if (vtype == VREG)
379 /* Don't do this until we're sure it can't be a mtpt */
380 /* else if (vtype == VLNK)
382 /* what other types does AFS support? */
384 /* clean up from afs_FindVCache */
387 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
390 code = (*filldir) (dirbuf, de->name, len, offset, ino);
392 DRelease((struct buffer *)de, 0);
395 offset = dirpos + 1 + ((len + 16) >> 5);
397 /* If filldir didn't fill in the last one this is still pointing to that
400 fp->f_pos = (loff_t) offset;
402 ReleaseReadLock(&tdc->lock);
404 ReleaseReadLock(&avc->lock);
408 afs_PutFakeStat(&fakestat);
410 #if defined(AFS_LINUX26_ENV)
418 /* in afs_pioctl.c */
419 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
423 /* We need to detect unmap's after close. To do that, we need our own
424 * vm_operations_struct's. And we need to set them up for both the
425 * private and shared mappings. The fun part is that these are all static
426 * so we'll have to initialize on the fly!
428 static struct vm_operations_struct afs_private_mmap_ops;
429 static int afs_private_mmap_ops_inited = 0;
430 static struct vm_operations_struct afs_shared_mmap_ops;
431 static int afs_shared_mmap_ops_inited = 0;
434 afs_linux_vma_close(struct vm_area_struct *vmap)
443 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
448 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
449 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
450 ICL_TYPE_INT32, vcp->execsOrWriters);
451 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
452 ObtainWriteLock(&vcp->lock, 532);
455 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
456 MyPidxx, &vcp, &vmap);
460 ReleaseWriteLock(&vcp->lock);
462 if (need_unlock && vcp->execsOrWriters < 2) {
464 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
465 /* only decrement the execsOrWriters flag if this is not a
467 if (!(vcp->states & CRO) )
468 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
469 vcp->execsOrWriters--;
470 vcp->states &= ~CMAPPED;
472 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
473 vcp->execsOrWriters--;
474 /* If we did not have the lock */
477 if (!vcp->execsOrWriters)
478 vcp->execsOrWriters = 1;
483 ReleaseWriteLock(&vcp->lock);
491 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
493 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
494 cred_t *credp = crref();
495 struct vrequest treq;
499 #if defined(AFS_LINUX24_ENV)
500 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
501 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
502 vmap->vm_end - vmap->vm_start);
504 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
505 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
506 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
510 /* get a validated vcache entry */
511 code = afs_InitReq(&treq, credp);
513 code = afs_VerifyVCache(vcp, &treq);
519 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
522 code = generic_file_mmap(fp, vmap);
527 ObtainWriteLock(&vcp->lock, 531);
528 /* Set out vma ops so we catch the close. The following test should be
529 * the same as used in generic_file_mmap.
531 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
532 if (!afs_shared_mmap_ops_inited) {
533 afs_shared_mmap_ops_inited = 1;
534 afs_shared_mmap_ops = *vmap->vm_ops;
535 afs_shared_mmap_ops.close = afs_linux_vma_close;
537 vmap->vm_ops = &afs_shared_mmap_ops;
539 if (!afs_private_mmap_ops_inited) {
540 afs_private_mmap_ops_inited = 1;
541 afs_private_mmap_ops = *vmap->vm_ops;
542 afs_private_mmap_ops.close = afs_linux_vma_close;
544 vmap->vm_ops = &afs_private_mmap_ops;
548 /* Add an open reference on the first mapping. */
549 if (vcp->mapcnt == 0) {
550 if (!(vcp->states & CRO))
551 vcp->execsOrWriters++;
553 vcp->states |= CMAPPED;
555 ReleaseWriteLock(&vcp->lock);
565 afs_linux_open(struct inode *ip, struct file *fp)
568 cred_t *credp = crref();
571 #ifdef AFS_LINUX24_ENV
574 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
575 #ifdef AFS_LINUX24_ENV
584 /* afs_Close is called from release, since release is used to handle all
585 * file closings. In addition afs_linux_flush is called from sys_close to
586 * handle flushing the data back to the server. The kicker is that we could
587 * ignore flush completely if only sys_close took it's return value from
588 * fput. See afs_linux_flush for notes on interactions between release and
592 afs_linux_release(struct inode *ip, struct file *fp)
595 cred_t *credp = crref();
596 struct vcache *vcp = ITOAFS(ip);
599 #ifdef AFS_LINUX24_ENV
603 vcp->flushcnt--; /* protected by AFS global lock. */
605 code = afs_close(vcp, fp->f_flags, credp);
607 #ifdef AFS_LINUX24_ENV
616 #if defined(AFS_LINUX24_ENV)
618 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
621 afs_linux_fsync(struct file *fp, struct dentry *dp)
625 struct inode *ip = FILE_INODE(fp);
626 cred_t *credp = crref();
629 #ifdef AFS_LINUX24_ENV
632 code = afs_fsync(ITOAFS(ip), credp);
633 #ifdef AFS_LINUX24_ENV
644 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
647 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
648 cred_t *credp = crref();
649 #ifdef AFS_LINUX24_ENV
650 struct flock64 flock;
655 /* Convert to a lock format afs_lockctl understands. */
656 memset((char *)&flock, 0, sizeof(flock));
657 flock.l_type = flp->fl_type;
658 flock.l_pid = flp->fl_pid;
660 flock.l_start = flp->fl_start;
661 flock.l_len = flp->fl_end - flp->fl_start;
663 /* Safe because there are no large files, yet */
664 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
665 if (cmd == F_GETLK64)
667 else if (cmd == F_SETLK64)
669 else if (cmd == F_SETLKW64)
671 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
674 code = afs_lockctl(vcp, &flock, cmd, credp);
677 /* Convert flock back to Linux's file_lock */
678 flp->fl_type = flock.l_type;
679 flp->fl_pid = flock.l_pid;
680 flp->fl_start = flock.l_start;
681 flp->fl_end = flock.l_start + flock.l_len;
689 * flush is called from sys_close. We could ignore it, but sys_close return
690 * code comes from flush, not release. We need to use release to keep
691 * the vcache open count correct. Note that flush is called before release
692 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
693 * races and also avoid calling afs_close twice when closing the file.
694 * If we merely checked for opens > 0 in afs_linux_release, then if an
695 * new open occurred when storing back the file, afs_linux_release would
696 * incorrectly close the file and decrement the opens count. Calling afs_close
697 * on the just flushed file is wasteful, since the background daemon will
698 * execute the code that finally decides there is nothing to do.
701 afs_linux_flush(struct file *fp)
703 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
707 /* Only do this on the last close of the file pointer. */
708 #if defined(AFS_LINUX24_ENV)
709 if (atomic_read(&fp->f_count) > 1)
718 code = afs_close(vcp, fp->f_flags, credp);
719 vcp->flushcnt++; /* protected by AFS global lock. */
726 #if !defined(AFS_LINUX24_ENV)
727 /* Not allowed to directly read a directory. */
729 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
737 struct file_operations afs_dir_fops = {
738 #if !defined(AFS_LINUX24_ENV)
739 .read = afs_linux_dir_read,
740 .lock = afs_linux_lock,
741 .fsync = afs_linux_fsync,
743 .read = generic_read_dir,
745 .readdir = afs_linux_readdir,
747 .open = afs_linux_open,
748 .release = afs_linux_release,
751 struct file_operations afs_file_fops = {
752 .read = afs_linux_read,
753 .write = afs_linux_write,
755 .mmap = afs_linux_mmap,
756 .open = afs_linux_open,
757 .flush = afs_linux_flush,
758 .release = afs_linux_release,
759 .fsync = afs_linux_fsync,
760 .lock = afs_linux_lock,
764 /**********************************************************************
765 * AFS Linux dentry operations
766 **********************************************************************/
768 /* afs_linux_revalidate
769 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
772 afs_linux_revalidate(struct dentry *dp)
776 struct vrequest treq;
777 struct vcache *vcp = ITOAFS(dp->d_inode);
778 struct vcache *rootvp = NULL;
782 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
783 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
784 ObtainSharedLock(&afs_xvcache, 680);
785 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
786 ReleaseSharedLock(&afs_xvcache);
788 #ifdef AFS_LINUX24_ENV
792 /* Make this a fast path (no crref), since it's called so often. */
793 if (vcp->states & CStatd) {
794 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
795 check_bad_parent(dp); /* check and correct mvid */
797 vcache2fakeinode(rootvp, vcp);
800 #ifdef AFS_LINUX24_ENV
804 afs_PutVCache(rootvp);
810 code = afs_InitReq(&treq, credp);
812 code = afs_VerifyVCache(vcp, &treq);
814 #ifdef AFS_LINUX24_ENV
823 #if defined(AFS_LINUX26_ENV)
825 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
827 int err = afs_linux_revalidate(dentry);
829 generic_fillattr(dentry->d_inode, stat);
834 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
835 * In kernels 2.2.10 and above, we are passed an additional flags var which
836 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
837 * we are advised to follow the entry if it is a link or to make sure that
838 * it is a directory. But since the kernel itself checks these possibilities
839 * later on, we shouldn't have to do it until later. Perhaps in the future..
841 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
843 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
846 afs_linux_dentry_revalidate(struct dentry *dp)
850 cred_t *credp = crref();
851 struct vrequest treq;
852 struct vcache *lookupvcp = NULL;
853 int code, bad_dentry = 1;
854 struct sysname_info sysState;
855 struct vcache *vcp, *parentvcp;
857 sysState.allocked = 0;
862 vcp = ITOAFS(dp->d_inode);
863 parentvcp = ITOAFS(dp->d_parent->d_inode);
865 /* If it's a negative dentry, then there's nothing to do. */
866 if (!vcp || !parentvcp)
869 /* If it is the AFS root, then there's no chance it needs
871 if (vcp == afs_globalVp) {
876 if ((code = afs_InitReq(&treq, credp)))
879 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
880 name = sysState.name;
882 /* First try looking up the DNLC */
883 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
884 /* Verify that the dentry does not point to an old inode */
885 if (vcp != lookupvcp)
887 /* Check and correct mvid */
888 if (*name != '/' && vcp->mvstat == 2)
889 check_bad_parent(dp);
895 /* A DNLC lookup failure cannot be trusted. Try a real lookup.
896 Make sure to try the real name and not the @sys expansion;
897 afs_lookup will expand @sys itself. */
899 code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
901 /* Verify that the dentry does not point to an old inode */
902 if (vcp != lookupvcp)
910 afs_PutVCache(lookupvcp);
911 if (sysState.allocked)
912 osi_FreeLargeSpace(name);
915 shrink_dcache_parent(dp);
926 #if !defined(AFS_LINUX26_ENV)
927 /* afs_dentry_iput */
929 afs_dentry_iput(struct dentry *dp, struct inode *ip)
931 if (ICL_SETACTIVE(afs_iclSetp)) {
933 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
934 ICL_TYPE_STRING, dp->d_parent->d_name.name,
935 ICL_TYPE_STRING, dp->d_name.name);
944 afs_dentry_delete(struct dentry *dp)
946 if (ICL_SETACTIVE(afs_iclSetp)) {
948 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
949 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
950 ICL_TYPE_STRING, dp->d_name.name);
954 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
955 return 1; /* bad inode? */
960 struct dentry_operations afs_dentry_operations = {
961 .d_revalidate = afs_linux_dentry_revalidate,
962 .d_delete = afs_dentry_delete,
963 #if !defined(AFS_LINUX26_ENV)
964 .d_iput = afs_dentry_iput,
968 /**********************************************************************
969 * AFS Linux inode operations
970 **********************************************************************/
974 * Merely need to set enough of vattr to get us through the create. Note
975 * that the higher level code (open_namei) will take care of any tuncation
976 * explicitly. Exclusive open is also taken care of in open_namei.
978 * name is in kernel space at this point.
981 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
984 cred_t *credp = crref();
987 const char *name = dp->d_name.name;
991 vattr.va_mode = mode;
994 #if defined(AFS_LINUX26_ENV)
998 afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
999 (struct vcache **)&ip, credp);
1002 vattr2inode(ip, &vattr);
1003 /* Reset ops if symlink or directory. */
1004 #if defined(AFS_LINUX24_ENV)
1005 if (S_ISREG(ip->i_mode)) {
1006 ip->i_op = &afs_file_iops;
1007 ip->i_fop = &afs_file_fops;
1008 ip->i_data.a_ops = &afs_file_aops;
1009 } else if (S_ISDIR(ip->i_mode)) {
1010 ip->i_op = &afs_dir_iops;
1011 ip->i_fop = &afs_dir_fops;
1012 } else if (S_ISLNK(ip->i_mode)) {
1013 ip->i_op = &afs_symlink_iops;
1014 ip->i_data.a_ops = &afs_symlink_aops;
1015 ip->i_mapping = &ip->i_data;
1017 printk("afs_linux_create: FIXME\n");
1019 if (S_ISDIR(ip->i_mode))
1020 ip->i_op = &afs_dir_iops;
1021 else if (S_ISLNK(ip->i_mode))
1022 ip->i_op = &afs_symlink_iops;
1025 dp->d_op = &afs_dentry_operations;
1026 dp->d_time = jiffies;
1027 d_instantiate(dp, ip);
1030 #if defined(AFS_LINUX26_ENV)
1038 /* afs_linux_lookup */
1039 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1041 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1044 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1048 cred_t *credp = crref();
1049 struct vcache *vcp = NULL;
1050 const char *comp = dp->d_name.name;
1053 #if defined(AFS_LINUX26_ENV)
1056 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1059 struct inode *ip = AFSTOI(vcp);
1060 /* Reset ops if symlink or directory. */
1061 #if defined(AFS_LINUX24_ENV)
1062 if (S_ISREG(ip->i_mode)) {
1063 ip->i_op = &afs_file_iops;
1064 ip->i_fop = &afs_file_fops;
1065 ip->i_data.a_ops = &afs_file_aops;
1066 } else if (S_ISDIR(ip->i_mode)) {
1067 ip->i_op = &afs_dir_iops;
1068 ip->i_fop = &afs_dir_fops;
1069 } else if (S_ISLNK(ip->i_mode)) {
1070 ip->i_op = &afs_symlink_iops;
1071 ip->i_data.a_ops = &afs_symlink_aops;
1072 ip->i_mapping = &ip->i_data;
1075 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1076 ip->i_mode, dp->d_name.name, code);
1078 if (S_ISDIR(ip->i_mode))
1079 ip->i_op = &afs_dir_iops;
1080 else if (S_ISLNK(ip->i_mode))
1081 ip->i_op = &afs_symlink_iops;
1084 dp->d_time = jiffies;
1085 dp->d_op = &afs_dentry_operations;
1086 d_add(dp, AFSTOI(vcp));
1088 #if defined(AFS_LINUX26_ENV)
1094 /* It's ok for the file to not be found. That's noted by the caller by
1095 * seeing that the dp->d_inode field is NULL.
1097 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1101 return ERR_PTR(-code);
1110 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1113 cred_t *credp = crref();
1114 const char *name = newdp->d_name.name;
1115 struct inode *oldip = olddp->d_inode;
1117 /* If afs_link returned the vnode, we could instantiate the
1118 * dentry. Since it's not, we drop this one and do a new lookup.
1123 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1131 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1134 cred_t *credp = crref();
1135 const char *name = dp->d_name.name;
1138 #if defined(AFS_LINUX26_ENV)
1141 code = afs_remove(ITOAFS(dip), name, credp);
1144 #if defined(AFS_LINUX26_ENV)
1154 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1157 cred_t *credp = crref();
1159 const char *name = dp->d_name.name;
1161 /* If afs_symlink returned the vnode, we could instantiate the
1162 * dentry. Since it's not, we drop this one and do a new lookup.
1168 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1175 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1178 cred_t *credp = crref();
1179 struct vcache *tvcp = NULL;
1181 const char *name = dp->d_name.name;
1184 #if defined(AFS_LINUX26_ENV)
1188 vattr.va_mask = ATTR_MODE;
1189 vattr.va_mode = mode;
1190 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1193 tvcp->v.v_op = &afs_dir_iops;
1194 #if defined(AFS_LINUX24_ENV)
1195 tvcp->v.v_fop = &afs_dir_fops;
1197 dp->d_op = &afs_dentry_operations;
1198 dp->d_time = jiffies;
1199 d_instantiate(dp, AFSTOI(tvcp));
1202 #if defined(AFS_LINUX26_ENV)
1211 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1214 cred_t *credp = crref();
1215 const char *name = dp->d_name.name;
1218 #if defined(AFS_LINUX26_ENV)
1221 code = afs_rmdir(ITOAFS(dip), name, credp);
1223 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1224 * that failed because a directory is not empty. So, we map
1225 * EEXIST to ENOTEMPTY on linux.
1227 if (code == EEXIST) {
1235 #if defined(AFS_LINUX26_ENV)
1246 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1247 struct inode *newip, struct dentry *newdp)
1250 cred_t *credp = crref();
1251 const char *oldname = olddp->d_name.name;
1252 const char *newname = newdp->d_name.name;
1255 #if defined(AFS_LINUX26_ENV)
1258 /* Remove old and new entries from name hash. New one will change below.
1259 * While it's optimal to catch failures and re-insert newdp into hash,
1260 * it's also error prone and in that case we're already dealing with error
1261 * cases. Let another lookup put things right, if need be.
1263 #if defined(AFS_LINUX26_ENV)
1264 if (!d_unhashed(olddp))
1266 if (!d_unhashed(newdp))
1269 if (!list_empty(&olddp->d_hash))
1271 if (!list_empty(&newdp->d_hash))
1274 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1277 /* update time so it doesn't expire immediately */
1278 newdp->d_time = jiffies;
1279 d_move(olddp, newdp);
1282 #if defined(AFS_LINUX26_ENV)
1292 /* afs_linux_ireadlink
1293 * Internal readlink which can return link contents to user or kernel space.
1294 * Note that the buffer is NOT supposed to be null-terminated.
1297 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1300 cred_t *credp = crref();
1304 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1305 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1309 return maxlen - tuio.uio_resid;
1314 #if !defined(AFS_LINUX24_ENV)
1315 /* afs_linux_readlink
1316 * Fill target (which is in user space) with contents of symlink.
1319 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1322 struct inode *ip = dp->d_inode;
1325 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1331 /* afs_linux_follow_link
1332 * a file system dependent link following routine.
1335 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1336 unsigned int follow)
1344 name = osi_Alloc(PATH_MAX + 1);
1348 return ERR_PTR(-EIO);
1351 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1356 res = ERR_PTR(code);
1359 res = lookup_dentry(name, basep, follow);
1363 osi_Free(name, PATH_MAX + 1);
1369 /* afs_linux_readpage
1370 * all reads come through here. A strategy-like read call.
1373 afs_linux_readpage(struct file *fp, struct page *pp)
1376 cred_t *credp = crref();
1377 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1379 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1381 ulong address = afs_linux_page_address(pp);
1382 afs_offs_t offset = pageoff(pp);
1386 struct inode *ip = FILE_INODE(fp);
1387 int cnt = page_count(pp);
1388 struct vcache *avc = ITOAFS(ip);
1391 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 */
1392 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1398 atomic_add(1, &pp->count);
1399 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1400 clear_bit(PG_error, &pp->flags);
1403 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1405 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1406 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1411 if (tuio.uio_resid) /* zero remainder of page */
1412 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1414 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1415 flush_dcache_page(pp);
1416 SetPageUptodate(pp);
1418 set_bit(PG_uptodate, &pp->flags);
1421 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1425 clear_bit(PG_locked, &pp->flags);
1430 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1432 struct vrequest treq;
1434 code = afs_InitReq(&treq, credp);
1435 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1436 tdc = afs_FindDCache(avc, offset);
1438 if (!(tdc->mflags & DFNextStarted))
1439 afs_PrefetchChunk(avc, tdc, credp, &treq);
1442 ReleaseWriteLock(&avc->lock);
1447 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1448 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1454 #if defined(AFS_LINUX24_ENV)
1456 afs_linux_writepage(struct page *pp)
1458 struct address_space *mapping = pp->mapping;
1459 struct inode *inode;
1460 unsigned long end_index;
1461 unsigned offset = PAGE_CACHE_SIZE;
1464 inode = (struct inode *)mapping->host;
1465 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1468 if (pp->index < end_index)
1470 /* things got complicated... */
1471 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1472 /* OK, are we completely out? */
1473 if (pp->index >= end_index + 1 || !offset)
1477 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1479 SetPageUptodate(pp);
1481 if (status == offset)
1488 /* afs_linux_permission
1489 * Check access rights - returns error if can't check or permission denied.
1492 afs_linux_permission(struct inode *ip, int mode)
1495 cred_t *credp = crref();
1499 if (mode & MAY_EXEC)
1501 if (mode & MAY_READ)
1503 if (mode & MAY_WRITE)
1505 code = afs_access(ITOAFS(ip), tmp, credp);
1513 #if defined(AFS_LINUX24_ENV)
1515 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1516 unsigned long offset, unsigned int count)
1518 struct vcache *vcp = ITOAFS(ip);
1527 buffer = kmap(pp) + offset;
1528 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1531 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1532 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1533 ICL_TYPE_INT32, 99999);
1535 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1537 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1542 && afs_stats_cmperf.cacheCurrDirtyChunks >
1543 afs_stats_cmperf.cacheMaxDirtyChunks) {
1544 struct vrequest treq;
1546 ObtainWriteLock(&vcp->lock, 533);
1547 if (!afs_InitReq(&treq, credp))
1548 code = afs_DoPartialWrite(vcp, &treq);
1549 ReleaseWriteLock(&vcp->lock);
1551 code = code ? -code : count - tuio.uio_resid;
1553 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1554 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1555 ICL_TYPE_INT32, code);
1564 afs_linux_updatepage(struct file *file, struct page *page,
1565 unsigned long offset, unsigned int count)
1567 struct dentry *dentry = file->f_dentry;
1569 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1572 /* afs_linux_updatepage
1573 * What one would have thought was writepage - write dirty page to file.
1574 * Called from generic_file_write. buffer is still in user space. pagep
1575 * has been filled in with old data if we're updating less than a page.
1578 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1579 unsigned int count, int sync)
1581 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1582 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1588 set_bit(PG_locked, &pp->flags);
1592 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1593 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1594 ICL_TYPE_INT32, 99999);
1595 setup_uio(&tuio, &iovec, page_addr + offset,
1596 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1599 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1603 code = code ? -code : count - tuio.uio_resid;
1604 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1605 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1606 ICL_TYPE_INT32, code);
1611 clear_bit(PG_locked, &pp->flags);
1616 #if defined(AFS_LINUX24_ENV)
1618 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1625 code = afs_linux_updatepage(file, page, offset, to - offset);
1634 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1641 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1644 struct inode_operations afs_file_iops = {
1645 #if defined(AFS_LINUX26_ENV)
1646 .permission = afs_linux_permission,
1647 .getattr = afs_linux_getattr,
1648 .setattr = afs_notify_change,
1649 #elif defined(AFS_LINUX24_ENV)
1650 .permission = afs_linux_permission,
1651 .revalidate = afs_linux_revalidate,
1652 .setattr = afs_notify_change,
1654 .default_file_ops = &afs_file_fops,
1655 .readpage = afs_linux_readpage,
1656 .revalidate = afs_linux_revalidate,
1657 .updatepage = afs_linux_updatepage,
1661 #if defined(AFS_LINUX24_ENV)
1662 struct address_space_operations afs_file_aops = {
1663 .readpage = afs_linux_readpage,
1664 .writepage = afs_linux_writepage,
1665 .commit_write = afs_linux_commit_write,
1666 .prepare_write = afs_linux_prepare_write,
1671 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1672 * by what sort of operation is allowed.....
1675 struct inode_operations afs_dir_iops = {
1676 #if !defined(AFS_LINUX24_ENV)
1677 .default_file_ops = &afs_dir_fops,
1679 .setattr = afs_notify_change,
1681 .create = afs_linux_create,
1682 .lookup = afs_linux_lookup,
1683 .link = afs_linux_link,
1684 .unlink = afs_linux_unlink,
1685 .symlink = afs_linux_symlink,
1686 .mkdir = afs_linux_mkdir,
1687 .rmdir = afs_linux_rmdir,
1688 .rename = afs_linux_rename,
1689 #if defined(AFS_LINUX26_ENV)
1690 .getattr = afs_linux_getattr,
1692 .revalidate = afs_linux_revalidate,
1694 .permission = afs_linux_permission,
1697 /* We really need a separate symlink set of ops, since do_follow_link()
1698 * determines if it _is_ a link by checking if the follow_link op is set.
1700 #if defined(AFS_LINUX24_ENV)
1702 afs_symlink_filler(struct file *file, struct page *page)
1704 struct inode *ip = (struct inode *)page->mapping->host;
1705 char *p = (char *)kmap(page);
1710 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1714 p[code] = '\0'; /* null terminate? */
1718 SetPageUptodate(page);
1733 struct address_space_operations afs_symlink_aops = {
1734 .readpage = afs_symlink_filler
1738 struct inode_operations afs_symlink_iops = {
1739 #if defined(AFS_LINUX24_ENV)
1740 .readlink = page_readlink,
1741 .follow_link = page_follow_link,
1742 .setattr = afs_notify_change,
1744 .readlink = afs_linux_readlink,
1745 .follow_link = afs_linux_follow_link,
1746 .permission = afs_linux_permission,
1747 .revalidate = afs_linux_revalidate,