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"
41 #define pageoff(pp) pgoff2loff((pp)->index)
43 #define pageoff(pp) pp->offset
46 #if defined(AFS_LINUX26_ENV)
47 #define UnlockPage(pp) unlock_page(pp)
50 extern struct vcache *afs_globalVp;
51 extern afs_rwlock_t afs_xvcache;
53 #if defined(AFS_LINUX24_ENV)
54 extern struct inode_operations afs_file_iops;
55 extern struct address_space_operations afs_file_aops;
56 struct address_space_operations afs_symlink_aops;
58 extern struct inode_operations afs_dir_iops;
59 extern struct inode_operations afs_symlink_iops;
63 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
66 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
67 cred_t *credp = crref();
71 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
72 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
75 /* get a validated vcache entry */
76 code = afs_InitReq(&treq, credp);
78 code = afs_VerifyVCache(vcp, &treq);
83 #ifdef AFS_64BIT_CLIENT
84 if (*offp + count > afs_vmMappingEnd) {
87 afs_size_t oldOffset = *offp;
90 if (*offp < afs_vmMappingEnd) {
91 /* special case of a buffer crossing the VM mapping end */
92 afs_int32 tcount = afs_vmMappingEnd - *offp;
94 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
96 code = generic_file_read(fp, buf, tcount, offp);
103 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
104 UIO_READ, AFS_UIOSYS);
105 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
106 xfered += count - tuio.uio_resid;
108 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
109 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
110 ICL_TYPE_INT32, code);
112 *offp += count - tuio.uio_resid;
120 #endif /* AFS_64BIT_CLIENT */
121 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
123 code = generic_file_read(fp, buf, count, offp);
125 #ifdef AFS_64BIT_CLIENT
127 #endif /* AFS_64BIT_CLIENT */
130 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
131 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
140 /* Now we have integrated VM for writes as well as reads. generic_file_write
141 * also takes care of re-positioning the pointer if file is open in append
142 * mode. Call fake open/close to ensure we do writes of core dumps.
145 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
149 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
150 struct vrequest treq;
151 cred_t *credp = crref();
156 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
157 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
158 (fp->f_flags & O_APPEND) ? 99998 : 99999);
161 /* get a validated vcache entry */
162 code = (ssize_t) afs_InitReq(&treq, credp);
164 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
166 ObtainWriteLock(&vcp->lock, 529);
168 ReleaseWriteLock(&vcp->lock);
172 #ifdef AFS_64BIT_CLIENT
174 if (fp->f_flags & O_APPEND)
175 toffs += vcp->m.Length;
176 if (toffs + count > afs_vmMappingEnd) {
179 afs_size_t oldOffset = *offp;
180 afs_int32 xfered = 0;
182 if (toffs < afs_vmMappingEnd) {
183 /* special case of a buffer crossing the VM mapping end */
184 afs_int32 tcount = afs_vmMappingEnd - *offp;
187 code = generic_file_write(fp, buf, tcount, offp);
189 if (code != tcount) {
195 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
196 UIO_WRITE, AFS_UIOSYS);
197 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
198 xfered += count - tuio.uio_resid;
201 *offp += count - tuio.uio_resid;
203 /* Purge dirty chunks of file if there are too many dirty chunks.
204 * Inside the write loop, we only do this at a chunk boundary.
205 * Clean up partial chunk if necessary at end of loop.
207 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
208 AFS_CHUNKBASE(oldOffset)) {
209 ObtainWriteLock(&vcp->lock, 402);
210 code = afs_DoPartialWrite(vcp, &treq);
211 vcp->states |= CDirty;
212 ReleaseWriteLock(&vcp->lock);
217 ObtainWriteLock(&vcp->lock, 400);
218 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
220 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
221 vcp->m.Length = toffs;
223 ReleaseWriteLock(&vcp->lock);
228 #endif /* AFS_64BIT_CLIENT */
230 code = generic_file_write(fp, buf, count, offp);
232 #ifdef AFS_64BIT_CLIENT
234 #endif /* AFS_64BIT_CLIENT */
237 ObtainWriteLock(&vcp->lock, 530);
238 vcp->m.Date = osi_Time(); /* set modification time */
239 afs_FakeClose(vcp, credp);
241 code2 = afs_DoPartialWrite(vcp, &treq);
242 if (code2 && code >= 0)
243 code = (ssize_t) - code2;
244 ReleaseWriteLock(&vcp->lock);
246 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
247 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
255 /* This is a complete rewrite of afs_readdir, since we can make use of
256 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
257 * handling and use of bulkstats will need to be reflected here as well.
260 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
262 extern struct DirEntry *afs_dir_GetBlob();
263 struct vcache *avc = ITOAFS(FILE_INODE(fp));
264 struct vrequest treq;
265 register struct dcache *tdc;
272 afs_size_t origOffset, tlen;
273 cred_t *credp = crref();
274 struct afs_fakestat_state fakestat;
276 #if defined(AFS_LINUX26_ENV)
280 AFS_STATCNT(afs_readdir);
282 code = afs_InitReq(&treq, credp);
287 afs_InitFakeStat(&fakestat);
288 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
292 /* update the cache entry */
294 code = afs_VerifyVCache(avc, &treq);
298 /* get a reference to the entire directory */
299 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
305 ObtainReadLock(&avc->lock);
306 ObtainReadLock(&tdc->lock);
308 * Make sure that the data in the cache is current. There are two
309 * cases we need to worry about:
310 * 1. The cache data is being fetched by another process.
311 * 2. The cache data is no longer valid
313 while ((avc->states & CStatd)
314 && (tdc->dflags & DFFetching)
315 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
316 ReleaseReadLock(&tdc->lock);
317 ReleaseReadLock(&avc->lock);
318 afs_osi_Sleep(&tdc->validPos);
319 ObtainReadLock(&avc->lock);
320 ObtainReadLock(&tdc->lock);
322 if (!(avc->states & CStatd)
323 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
324 ReleaseReadLock(&tdc->lock);
325 ReleaseReadLock(&avc->lock);
330 /* Fill in until we get an error or we're done. This implementation
331 * takes an offset in units of blobs, rather than bytes.
334 offset = (int) fp->f_pos;
336 dirpos = BlobScan(&tdc->f.inode, offset);
340 de = afs_dir_GetBlob(&tdc->f.inode, dirpos);
344 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
345 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
347 len = strlen(de->name);
349 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %x, dirpos %d)\n",
350 &tdc->f.inode, dirpos);
351 DRelease((struct buffer *) de, 0);
353 ReleaseReadLock(&avc->lock);
358 /* filldir returns -EINVAL when the buffer is full. */
359 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
361 unsigned int type = DT_UNKNOWN;
362 struct VenusFid afid;
365 afid.Cell = avc->fid.Cell;
366 afid.Fid.Volume = avc->fid.Fid.Volume;
367 afid.Fid.Vnode = ntohl(de->fid.vnode);
368 afid.Fid.Unique = ntohl(de->fid.vunique);
369 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
371 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
374 } else if (((tvc->states) & (CStatd | CTruth))) {
375 /* CTruth will be set if the object has
380 else if (vtype == VREG)
382 /* Don't do this until we're sure it can't be a mtpt */
383 /* else if (vtype == VLNK)
385 /* what other types does AFS support? */
387 /* clean up from afs_FindVCache */
390 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
393 code = (*filldir) (dirbuf, de->name, len, offset, ino);
395 DRelease((struct buffer *)de, 0);
398 offset = dirpos + 1 + ((len + 16) >> 5);
400 /* If filldir didn't fill in the last one this is still pointing to that
403 fp->f_pos = (loff_t) offset;
405 ReleaseReadLock(&tdc->lock);
407 ReleaseReadLock(&avc->lock);
411 afs_PutFakeStat(&fakestat);
414 #if defined(AFS_LINUX26_ENV)
421 /* in afs_pioctl.c */
422 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
426 /* We need to detect unmap's after close. To do that, we need our own
427 * vm_operations_struct's. And we need to set them up for both the
428 * private and shared mappings. The fun part is that these are all static
429 * so we'll have to initialize on the fly!
431 static struct vm_operations_struct afs_private_mmap_ops;
432 static int afs_private_mmap_ops_inited = 0;
433 static struct vm_operations_struct afs_shared_mmap_ops;
434 static int afs_shared_mmap_ops_inited = 0;
437 afs_linux_vma_close(struct vm_area_struct *vmap)
446 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
451 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
452 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
453 ICL_TYPE_INT32, vcp->execsOrWriters);
454 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
455 ObtainWriteLock(&vcp->lock, 532);
458 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
459 MyPidxx, &vcp, &vmap);
463 ReleaseWriteLock(&vcp->lock);
465 if (need_unlock && vcp->execsOrWriters < 2) {
467 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
468 /* only decrement the execsOrWriters flag if this is not a
470 if (!(vcp->states & CRO) )
471 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
472 vcp->execsOrWriters--;
473 vcp->states &= ~CMAPPED;
475 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
476 vcp->execsOrWriters--;
477 /* If we did not have the lock */
480 if (!vcp->execsOrWriters)
481 vcp->execsOrWriters = 1;
486 ReleaseWriteLock(&vcp->lock);
494 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
496 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
497 cred_t *credp = crref();
498 struct vrequest treq;
502 #if defined(AFS_LINUX24_ENV)
503 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
504 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
505 vmap->vm_end - vmap->vm_start);
507 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
508 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
509 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
513 /* get a validated vcache entry */
514 code = afs_InitReq(&treq, credp);
516 code = afs_VerifyVCache(vcp, &treq);
522 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
525 code = generic_file_mmap(fp, vmap);
530 ObtainWriteLock(&vcp->lock, 531);
531 /* Set out vma ops so we catch the close. The following test should be
532 * the same as used in generic_file_mmap.
534 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
535 if (!afs_shared_mmap_ops_inited) {
536 afs_shared_mmap_ops_inited = 1;
537 afs_shared_mmap_ops = *vmap->vm_ops;
538 afs_shared_mmap_ops.close = afs_linux_vma_close;
540 vmap->vm_ops = &afs_shared_mmap_ops;
542 if (!afs_private_mmap_ops_inited) {
543 afs_private_mmap_ops_inited = 1;
544 afs_private_mmap_ops = *vmap->vm_ops;
545 afs_private_mmap_ops.close = afs_linux_vma_close;
547 vmap->vm_ops = &afs_private_mmap_ops;
551 /* Add an open reference on the first mapping. */
552 if (vcp->mapcnt == 0) {
553 if (!(vcp->states & CRO))
554 vcp->execsOrWriters++;
556 vcp->states |= CMAPPED;
558 ReleaseWriteLock(&vcp->lock);
568 afs_linux_open(struct inode *ip, struct file *fp)
571 cred_t *credp = crref();
573 #ifdef AFS_LINUX24_ENV
577 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
579 #ifdef AFS_LINUX24_ENV
587 /* afs_Close is called from release, since release is used to handle all
588 * file closings. In addition afs_linux_flush is called from sys_close to
589 * handle flushing the data back to the server. The kicker is that we could
590 * ignore flush completely if only sys_close took it's return value from
591 * fput. See afs_linux_flush for notes on interactions between release and
595 afs_linux_release(struct inode *ip, struct file *fp)
598 cred_t *credp = crref();
599 struct vcache *vcp = ITOAFS(ip);
601 #ifdef AFS_LINUX24_ENV
606 vcp->flushcnt--; /* protected by AFS global lock. */
608 code = afs_close(vcp, fp->f_flags, credp);
611 #ifdef AFS_LINUX24_ENV
619 #if defined(AFS_LINUX24_ENV)
621 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
624 afs_linux_fsync(struct file *fp, struct dentry *dp)
628 struct inode *ip = FILE_INODE(fp);
629 cred_t *credp = crref();
631 #ifdef AFS_LINUX24_ENV
635 code = afs_fsync(ITOAFS(ip), credp);
637 #ifdef AFS_LINUX24_ENV
647 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
650 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
651 cred_t *credp = crref();
652 struct AFS_FLOCK flock;
653 /* Convert to a lock format afs_lockctl understands. */
654 memset((char *)&flock, 0, sizeof(flock));
655 flock.l_type = flp->fl_type;
656 flock.l_pid = flp->fl_pid;
658 flock.l_start = flp->fl_start;
659 flock.l_len = flp->fl_end - flp->fl_start;
661 /* Safe because there are no large files, yet */
662 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
663 if (cmd == F_GETLK64)
665 else if (cmd == F_SETLK64)
667 else if (cmd == F_SETLKW64)
669 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
672 code = afs_lockctl(vcp, &flock, cmd, credp);
675 /* Convert flock back to Linux's file_lock */
676 flp->fl_type = flock.l_type;
677 flp->fl_pid = flock.l_pid;
678 flp->fl_start = flock.l_start;
679 flp->fl_end = flock.l_start + flock.l_len;
687 * flush is called from sys_close. We could ignore it, but sys_close return
688 * code comes from flush, not release. We need to use release to keep
689 * the vcache open count correct. Note that flush is called before release
690 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
691 * races and also avoid calling afs_close twice when closing the file.
692 * If we merely checked for opens > 0 in afs_linux_release, then if an
693 * new open occurred when storing back the file, afs_linux_release would
694 * incorrectly close the file and decrement the opens count. Calling afs_close
695 * on the just flushed file is wasteful, since the background daemon will
696 * execute the code that finally decides there is nothing to do.
699 afs_linux_flush(struct file *fp)
701 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
705 /* Only do this on the last close of the file pointer. */
706 #if defined(AFS_LINUX24_ENV)
707 if (atomic_read(&fp->f_count) > 1)
716 code = afs_close(vcp, fp->f_flags, credp);
717 vcp->flushcnt++; /* protected by AFS global lock. */
724 #if !defined(AFS_LINUX24_ENV)
725 /* Not allowed to directly read a directory. */
727 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
735 struct file_operations afs_dir_fops = {
736 #if !defined(AFS_LINUX24_ENV)
737 .read = afs_linux_dir_read,
738 .lock = afs_linux_lock,
739 .fsync = afs_linux_fsync,
741 .read = generic_read_dir,
743 .readdir = afs_linux_readdir,
745 .open = afs_linux_open,
746 .release = afs_linux_release,
749 struct file_operations afs_file_fops = {
750 .read = afs_linux_read,
751 .write = afs_linux_write,
753 .mmap = afs_linux_mmap,
754 .open = afs_linux_open,
755 .flush = afs_linux_flush,
756 .release = afs_linux_release,
757 .fsync = afs_linux_fsync,
758 .lock = afs_linux_lock,
762 /**********************************************************************
763 * AFS Linux dentry operations
764 **********************************************************************/
766 /* afs_linux_revalidate
767 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
770 afs_linux_revalidate(struct dentry *dp)
774 struct vrequest treq;
775 struct vcache *vcp = ITOAFS(dp->d_inode);
776 struct vcache *rootvp = NULL;
778 #ifdef AFS_LINUX24_ENV
783 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
784 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
785 ObtainSharedLock(&afs_xvcache, 680);
786 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
787 ReleaseSharedLock(&afs_xvcache);
790 /* Make this a fast path (no crref), since it's called so often. */
791 if (vcp->states & CStatd) {
792 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
793 check_bad_parent(dp); /* check and correct mvid */
795 vcache2fakeinode(rootvp, vcp);
799 afs_PutVCache(rootvp);
801 #ifdef AFS_LINUX24_ENV
808 code = afs_InitReq(&treq, credp);
810 code = afs_VerifyVCache(vcp, &treq);
813 #ifdef AFS_LINUX24_ENV
821 #if defined(AFS_LINUX26_ENV)
823 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
825 int err = afs_linux_revalidate(dentry);
827 generic_fillattr(dentry->d_inode, stat);
832 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
833 * In kernels 2.2.10 and above, we are passed an additional flags var which
834 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
835 * we are advised to follow the entry if it is a link or to make sure that
836 * it is a directory. But since the kernel itself checks these possibilities
837 * later on, we shouldn't have to do it until later. Perhaps in the future..
839 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
841 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
844 afs_linux_dentry_revalidate(struct dentry *dp)
848 cred_t *credp = crref();
849 struct vrequest treq;
850 struct vcache *lookupvcp = NULL;
851 int code, bad_dentry = 1;
852 struct sysname_info sysState;
853 struct vcache *vcp, *parentvcp;
855 sysState.allocked = 0;
857 #ifdef AFS_LINUX24_ENV
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);
917 shrink_dcache_parent(dp);
921 #ifdef AFS_LINUX24_ENV
929 #if !defined(AFS_LINUX26_ENV)
930 /* afs_dentry_iput */
932 afs_dentry_iput(struct dentry *dp, struct inode *ip)
936 if (ICL_SETACTIVE(afs_iclSetp)) {
937 isglock = ISAFS_GLOCK();
938 if (!isglock) AFS_GLOCK();
939 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
940 ICL_TYPE_STRING, dp->d_parent->d_name.name,
941 ICL_TYPE_STRING, dp->d_name.name);
942 if (!isglock) AFS_GUNLOCK();
950 afs_dentry_delete(struct dentry *dp)
953 if (ICL_SETACTIVE(afs_iclSetp)) {
954 isglock = ISAFS_GLOCK();
955 if (!isglock) AFS_GLOCK();
956 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
957 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
958 ICL_TYPE_STRING, dp->d_name.name);
959 if (!isglock) AFS_GUNLOCK();
962 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
963 return 1; /* bad inode? */
968 struct dentry_operations afs_dentry_operations = {
969 .d_revalidate = afs_linux_dentry_revalidate,
970 .d_delete = afs_dentry_delete,
971 #if !defined(AFS_LINUX26_ENV)
972 .d_iput = afs_dentry_iput,
976 /**********************************************************************
977 * AFS Linux inode operations
978 **********************************************************************/
982 * Merely need to set enough of vattr to get us through the create. Note
983 * that the higher level code (open_namei) will take care of any tuncation
984 * explicitly. Exclusive open is also taken care of in open_namei.
986 * name is in kernel space at this point.
989 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
992 cred_t *credp = crref();
995 const char *name = dp->d_name.name;
999 vattr.va_mode = mode;
1001 #if defined(AFS_LINUX26_ENV)
1006 afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
1007 (struct vcache **)&ip, credp);
1010 vattr2inode(ip, &vattr);
1011 /* Reset ops if symlink or directory. */
1012 #if defined(AFS_LINUX24_ENV)
1013 if (S_ISREG(ip->i_mode)) {
1014 ip->i_op = &afs_file_iops;
1015 ip->i_fop = &afs_file_fops;
1016 ip->i_data.a_ops = &afs_file_aops;
1017 } else if (S_ISDIR(ip->i_mode)) {
1018 ip->i_op = &afs_dir_iops;
1019 ip->i_fop = &afs_dir_fops;
1020 } else if (S_ISLNK(ip->i_mode)) {
1021 ip->i_op = &afs_symlink_iops;
1022 ip->i_data.a_ops = &afs_symlink_aops;
1023 ip->i_mapping = &ip->i_data;
1025 printk("afs_linux_create: FIXME\n");
1027 if (S_ISDIR(ip->i_mode))
1028 ip->i_op = &afs_dir_iops;
1029 else if (S_ISLNK(ip->i_mode))
1030 ip->i_op = &afs_symlink_iops;
1033 dp->d_op = &afs_dentry_operations;
1034 dp->d_time = jiffies;
1035 d_instantiate(dp, ip);
1039 #if defined(AFS_LINUX26_ENV)
1046 /* afs_linux_lookup */
1047 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1049 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1052 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1056 cred_t *credp = crref();
1057 struct vcache *vcp = NULL;
1058 const char *comp = dp->d_name.name;
1060 #if defined(AFS_LINUX26_ENV)
1064 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1068 struct inode *ip = AFSTOI(vcp);
1069 /* Reset ops if symlink or directory. */
1070 #if defined(AFS_LINUX24_ENV)
1071 if (S_ISREG(ip->i_mode)) {
1072 ip->i_op = &afs_file_iops;
1073 ip->i_fop = &afs_file_fops;
1074 ip->i_data.a_ops = &afs_file_aops;
1075 } else if (S_ISDIR(ip->i_mode)) {
1076 ip->i_op = &afs_dir_iops;
1077 ip->i_fop = &afs_dir_fops;
1078 } else if (S_ISLNK(ip->i_mode)) {
1079 ip->i_op = &afs_symlink_iops;
1080 ip->i_data.a_ops = &afs_symlink_aops;
1081 ip->i_mapping = &ip->i_data;
1084 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1085 ip->i_mode, dp->d_name.name, code);
1086 #ifdef STRUCT_INODE_HAS_I_SECURITY
1087 if (ip->i_security == NULL) {
1088 if (security_inode_alloc(ip))
1089 panic("afs_linux_lookup: Cannot allocate inode security");
1093 if (S_ISDIR(ip->i_mode))
1094 ip->i_op = &afs_dir_iops;
1095 else if (S_ISLNK(ip->i_mode))
1096 ip->i_op = &afs_symlink_iops;
1099 dp->d_time = jiffies;
1100 dp->d_op = &afs_dentry_operations;
1101 d_add(dp, AFSTOI(vcp));
1103 #if defined(AFS_LINUX26_ENV)
1108 /* It's ok for the file to not be found. That's noted by the caller by
1109 * seeing that the dp->d_inode field is NULL.
1111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1115 return ERR_PTR(-code);
1124 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1127 cred_t *credp = crref();
1128 const char *name = newdp->d_name.name;
1129 struct inode *oldip = olddp->d_inode;
1131 /* If afs_link returned the vnode, we could instantiate the
1132 * dentry. Since it's not, we drop this one and do a new lookup.
1137 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1145 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1148 cred_t *credp = crref();
1149 const char *name = dp->d_name.name;
1151 #if defined(AFS_LINUX26_ENV)
1155 code = afs_remove(ITOAFS(dip), name, credp);
1159 #if defined(AFS_LINUX26_ENV)
1168 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1171 cred_t *credp = crref();
1173 const char *name = dp->d_name.name;
1175 /* If afs_symlink returned the vnode, we could instantiate the
1176 * dentry. Since it's not, we drop this one and do a new lookup.
1182 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1189 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1192 cred_t *credp = crref();
1193 struct vcache *tvcp = NULL;
1195 const char *name = dp->d_name.name;
1197 #if defined(AFS_LINUX26_ENV)
1202 vattr.va_mask = ATTR_MODE;
1203 vattr.va_mode = mode;
1204 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1208 tvcp->v.v_op = &afs_dir_iops;
1209 #if defined(AFS_LINUX24_ENV)
1210 tvcp->v.v_fop = &afs_dir_fops;
1212 dp->d_op = &afs_dentry_operations;
1213 dp->d_time = jiffies;
1214 d_instantiate(dp, AFSTOI(tvcp));
1217 #if defined(AFS_LINUX26_ENV)
1225 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1228 cred_t *credp = crref();
1229 const char *name = dp->d_name.name;
1231 #if defined(AFS_LINUX26_ENV)
1235 code = afs_rmdir(ITOAFS(dip), name, credp);
1238 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1239 * that failed because a directory is not empty. So, we map
1240 * EEXIST to ENOTEMPTY on linux.
1242 if (code == EEXIST) {
1250 #if defined(AFS_LINUX26_ENV)
1260 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1261 struct inode *newip, struct dentry *newdp)
1264 cred_t *credp = crref();
1265 const char *oldname = olddp->d_name.name;
1266 const char *newname = newdp->d_name.name;
1268 #if defined(AFS_LINUX26_ENV)
1271 /* Remove old and new entries from name hash. New one will change below.
1272 * While it's optimal to catch failures and re-insert newdp into hash,
1273 * it's also error prone and in that case we're already dealing with error
1274 * cases. Let another lookup put things right, if need be.
1276 #if defined(AFS_LINUX26_ENV)
1277 if (!d_unhashed(olddp))
1279 if (!d_unhashed(newdp))
1282 if (!list_empty(&olddp->d_hash))
1284 if (!list_empty(&newdp->d_hash))
1288 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1292 /* update time so it doesn't expire immediately */
1293 newdp->d_time = jiffies;
1294 d_move(olddp, newdp);
1297 #if defined(AFS_LINUX26_ENV)
1306 /* afs_linux_ireadlink
1307 * Internal readlink which can return link contents to user or kernel space.
1308 * Note that the buffer is NOT supposed to be null-terminated.
1311 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1314 cred_t *credp = crref();
1318 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1319 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1323 return maxlen - tuio.uio_resid;
1328 #if !defined(AFS_LINUX24_ENV)
1329 /* afs_linux_readlink
1330 * Fill target (which is in user space) with contents of symlink.
1333 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1336 struct inode *ip = dp->d_inode;
1339 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1345 /* afs_linux_follow_link
1346 * a file system dependent link following routine.
1349 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1350 unsigned int follow)
1358 name = osi_Alloc(PATH_MAX + 1);
1362 return ERR_PTR(-EIO);
1365 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1370 res = ERR_PTR(code);
1373 res = lookup_dentry(name, basep, follow);
1377 osi_Free(name, PATH_MAX + 1);
1383 /* afs_linux_readpage
1384 * all reads come through here. A strategy-like read call.
1387 afs_linux_readpage(struct file *fp, struct page *pp)
1390 cred_t *credp = crref();
1391 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1393 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1395 ulong address = afs_linux_page_address(pp);
1396 afs_offs_t offset = pageoff(pp);
1400 struct inode *ip = FILE_INODE(fp);
1401 int cnt = page_count(pp);
1402 struct vcache *avc = ITOAFS(ip);
1405 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1409 atomic_add(1, &pp->count);
1410 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1411 clear_bit(PG_error, &pp->flags);
1414 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1416 #ifdef AFS_LINUX24_ENV
1420 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 */
1421 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1422 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1423 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1426 #ifdef AFS_LINUX24_ENV
1431 if (tuio.uio_resid) /* zero remainder of page */
1432 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1434 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1435 flush_dcache_page(pp);
1436 SetPageUptodate(pp);
1438 set_bit(PG_uptodate, &pp->flags);
1442 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1446 clear_bit(PG_locked, &pp->flags);
1451 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1453 struct vrequest treq;
1456 code = afs_InitReq(&treq, credp);
1457 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1458 tdc = afs_FindDCache(avc, offset);
1460 if (!(tdc->mflags & DFNextStarted))
1461 afs_PrefetchChunk(avc, tdc, credp, &treq);
1464 ReleaseWriteLock(&avc->lock);
1473 #if defined(AFS_LINUX24_ENV)
1475 afs_linux_writepage(struct page *pp)
1477 struct address_space *mapping = pp->mapping;
1478 struct inode *inode;
1479 unsigned long end_index;
1480 unsigned offset = PAGE_CACHE_SIZE;
1483 inode = (struct inode *)mapping->host;
1484 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1487 if (pp->index < end_index)
1489 /* things got complicated... */
1490 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1491 /* OK, are we completely out? */
1492 if (pp->index >= end_index + 1 || !offset)
1496 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1498 SetPageUptodate(pp);
1500 if (status == offset)
1507 /* afs_linux_permission
1508 * Check access rights - returns error if can't check or permission denied.
1511 afs_linux_permission(struct inode *ip, int mode)
1514 cred_t *credp = crref();
1518 if (mode & MAY_EXEC)
1520 if (mode & MAY_READ)
1522 if (mode & MAY_WRITE)
1524 code = afs_access(ITOAFS(ip), tmp, credp);
1532 #if defined(AFS_LINUX24_ENV)
1534 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1535 unsigned long offset, unsigned int count)
1537 struct vcache *vcp = ITOAFS(ip);
1546 buffer = kmap(pp) + offset;
1547 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1550 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1551 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1552 ICL_TYPE_INT32, 99999);
1554 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1556 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1561 && afs_stats_cmperf.cacheCurrDirtyChunks >
1562 afs_stats_cmperf.cacheMaxDirtyChunks) {
1563 struct vrequest treq;
1565 ObtainWriteLock(&vcp->lock, 533);
1566 if (!afs_InitReq(&treq, credp))
1567 code = afs_DoPartialWrite(vcp, &treq);
1568 ReleaseWriteLock(&vcp->lock);
1570 code = code ? -code : count - tuio.uio_resid;
1572 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1573 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1574 ICL_TYPE_INT32, code);
1583 afs_linux_updatepage(struct file *file, struct page *page,
1584 unsigned long offset, unsigned int count)
1586 struct dentry *dentry = file->f_dentry;
1588 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1591 /* afs_linux_updatepage
1592 * What one would have thought was writepage - write dirty page to file.
1593 * Called from generic_file_write. buffer is still in user space. pagep
1594 * has been filled in with old data if we're updating less than a page.
1597 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1598 unsigned int count, int sync)
1600 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1601 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1607 set_bit(PG_locked, &pp->flags);
1611 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1612 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1613 ICL_TYPE_INT32, 99999);
1614 setup_uio(&tuio, &iovec, page_addr + offset,
1615 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1618 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1622 code = code ? -code : count - tuio.uio_resid;
1623 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1624 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1625 ICL_TYPE_INT32, code);
1630 clear_bit(PG_locked, &pp->flags);
1635 #if defined(AFS_LINUX24_ENV)
1637 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1644 code = afs_linux_updatepage(file, page, offset, to - offset);
1653 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1660 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1663 struct inode_operations afs_file_iops = {
1664 #if defined(AFS_LINUX26_ENV)
1665 .permission = afs_linux_permission,
1666 .getattr = afs_linux_getattr,
1667 .setattr = afs_notify_change,
1668 #elif defined(AFS_LINUX24_ENV)
1669 .permission = afs_linux_permission,
1670 .revalidate = afs_linux_revalidate,
1671 .setattr = afs_notify_change,
1673 .default_file_ops = &afs_file_fops,
1674 .readpage = afs_linux_readpage,
1675 .revalidate = afs_linux_revalidate,
1676 .updatepage = afs_linux_updatepage,
1680 #if defined(AFS_LINUX24_ENV)
1681 struct address_space_operations afs_file_aops = {
1682 .readpage = afs_linux_readpage,
1683 .writepage = afs_linux_writepage,
1684 .commit_write = afs_linux_commit_write,
1685 .prepare_write = afs_linux_prepare_write,
1690 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1691 * by what sort of operation is allowed.....
1694 struct inode_operations afs_dir_iops = {
1695 #if !defined(AFS_LINUX24_ENV)
1696 .default_file_ops = &afs_dir_fops,
1698 .setattr = afs_notify_change,
1700 .create = afs_linux_create,
1701 .lookup = afs_linux_lookup,
1702 .link = afs_linux_link,
1703 .unlink = afs_linux_unlink,
1704 .symlink = afs_linux_symlink,
1705 .mkdir = afs_linux_mkdir,
1706 .rmdir = afs_linux_rmdir,
1707 .rename = afs_linux_rename,
1708 #if defined(AFS_LINUX26_ENV)
1709 .getattr = afs_linux_getattr,
1711 .revalidate = afs_linux_revalidate,
1713 .permission = afs_linux_permission,
1716 /* We really need a separate symlink set of ops, since do_follow_link()
1717 * determines if it _is_ a link by checking if the follow_link op is set.
1719 #if defined(AFS_LINUX24_ENV)
1721 afs_symlink_filler(struct file *file, struct page *page)
1723 struct inode *ip = (struct inode *)page->mapping->host;
1724 char *p = (char *)kmap(page);
1729 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1734 p[code] = '\0'; /* null terminate? */
1737 SetPageUptodate(page);
1751 struct address_space_operations afs_symlink_aops = {
1752 .readpage = afs_symlink_filler
1756 struct inode_operations afs_symlink_iops = {
1757 #if defined(AFS_LINUX24_ENV)
1758 .readlink = page_readlink,
1759 .follow_link = page_follow_link,
1760 .setattr = afs_notify_change,
1762 .readlink = afs_linux_readlink,
1763 .follow_link = afs_linux_follow_link,
1764 .permission = afs_linux_permission,
1765 .revalidate = afs_linux_revalidate,