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
12 * AFS vnodeops. The "NOTUSED" #define is used to indicate routines and
13 * calling sequences present in an ops table that we don't actually use.
14 * They are present solely for documentation purposes.
16 * So far the only truly scary part is that Linux relies on the inode cache
17 * to be up to date. Don't you dare break a callback and expect an fstat
18 * to give you meaningful information. This appears to be fixed in the 2.1
19 * development kernels. As it is we can fix this now by intercepting the
23 #include <afsconfig.h>
24 #include "afs/param.h"
29 #include "afs/sysincludes.h"
30 #include "afsincludes.h"
31 #include "afs/afs_stats.h"
32 #include "afs/afs_osidnlc.h"
34 #include "h/pagemap.h"
35 #if defined(AFS_LINUX24_ENV)
36 #include "h/smp_lock.h"
40 #define pageoff(pp) pgoff2loff((pp)->index)
42 #define pageoff(pp) pp->offset
45 extern struct vcache *afs_globalVp;
46 extern afs_rwlock_t afs_xvcache;
48 extern struct dentry_operations *afs_dops;
49 #if defined(AFS_LINUX24_ENV)
50 extern struct inode_operations afs_file_iops;
51 extern struct address_space_operations afs_file_aops;
52 struct address_space_operations afs_symlink_aops;
54 extern struct inode_operations afs_dir_iops;
55 extern struct inode_operations afs_symlink_iops;
60 afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int)
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) {
90 afs_size_t oldOffset = *offp;
93 if (*offp < afs_vmMappingEnd) {
94 /* special case of a buffer crossing the VM mapping end */
95 afs_int32 tcount = afs_vmMappingEnd - *offp;
97 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
99 code = generic_file_read(fp, buf, tcount, offp);
101 if (code != tcount) {
106 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
107 UIO_READ, AFS_UIOSYS);
108 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
109 xfered += count - tuio.uio_resid;
111 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
112 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
113 ICL_TYPE_INT32, code);
115 *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);
229 #endif /* AFS_64BIT_CLIENT */
231 code = generic_file_write(fp, buf, count, offp);
233 #ifdef AFS_64BIT_CLIENT
235 #endif /* AFS_64BIT_CLIENT */
238 ObtainWriteLock(&vcp->lock, 530);
239 vcp->m.Date = osi_Time(); /* set modification time */
240 afs_FakeClose(vcp, credp);
242 code2 = afs_DoPartialWrite(vcp, &treq);
243 if (code2 && code >= 0)
244 code = (ssize_t) - code2;
245 ReleaseWriteLock(&vcp->lock);
247 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
248 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
256 /* This is a complete rewrite of afs_readdir, since we can make use of
257 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
258 * handling and use of bulkstats will need to be reflected here as well.
261 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
263 extern struct DirEntry *afs_dir_GetBlob();
264 struct vcache *avc = ITOAFS(FILE_INODE(fp));
265 struct vrequest treq;
266 register struct dcache *tdc;
273 afs_size_t origOffset, tlen;
274 cred_t *credp = crref();
275 struct afs_fakestat_state fakestat;
278 AFS_STATCNT(afs_readdir);
280 code = afs_InitReq(&treq, credp);
287 afs_InitFakeStat(&fakestat);
288 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
290 afs_PutFakeStat(&fakestat);
295 /* update the cache entry */
297 code = afs_VerifyVCache(avc, &treq);
299 afs_PutFakeStat(&fakestat);
304 /* get a reference to the entire directory */
305 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
308 afs_PutFakeStat(&fakestat);
312 ObtainReadLock(&avc->lock);
313 ObtainReadLock(&tdc->lock);
315 * Make sure that the data in the cache is current. There are two
316 * cases we need to worry about:
317 * 1. The cache data is being fetched by another process.
318 * 2. The cache data is no longer valid
320 while ((avc->states & CStatd)
321 && (tdc->dflags & DFFetching)
322 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
323 ReleaseReadLock(&tdc->lock);
324 ReleaseReadLock(&avc->lock);
325 afs_osi_Sleep(&tdc->validPos);
326 ObtainReadLock(&avc->lock);
327 ObtainReadLock(&tdc->lock);
329 if (!(avc->states & CStatd)
330 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
331 ReleaseReadLock(&tdc->lock);
332 ReleaseReadLock(&avc->lock);
337 /* Fill in until we get an error or we're done. This implementation
338 * takes an offset in units of blobs, rather than bytes.
341 offset = (int)fp->f_pos;
343 dirpos = BlobScan(&tdc->f.inode, offset);
347 de = afs_dir_GetBlob(&tdc->f.inode, dirpos);
351 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
352 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
354 len = strlen(de->name);
356 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %x, dirpos %d)\n",
357 &tdc->f.inode, dirpos);
360 ReleaseReadLock(&avc->lock);
361 afs_PutFakeStat(&fakestat);
365 /* filldir returns -EINVAL when the buffer is full. */
366 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
368 unsigned int type = DT_UNKNOWN;
369 struct VenusFid afid;
372 afid.Cell = avc->fid.Cell;
373 afid.Fid.Volume = avc->fid.Fid.Volume;
374 afid.Fid.Vnode = ntohl(de->fid.vnode);
375 afid.Fid.Unique = ntohl(de->fid.vunique);
376 if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
378 } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
381 } else if (((tvc->states) & (CStatd | CTruth))) {
382 /* CTruth will be set if the object has
387 else if (vtype == VREG)
389 /* Don't do this until we're sure it can't be a mtpt */
390 /* else if (vtype == VLNK)
392 /* what other types does AFS support? */
394 /* clean up from afs_FindVCache */
397 code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
400 code = (*filldir) (dirbuf, de->name, len, offset, ino);
405 offset = dirpos + 1 + ((len + 16) >> 5);
407 /* If filldir didn't fill in the last one this is still pointing to that
410 fp->f_pos = (loff_t) offset;
412 ReleaseReadLock(&tdc->lock);
414 ReleaseReadLock(&avc->lock);
415 afs_PutFakeStat(&fakestat);
421 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
424 /* in afs_pioctl.c */
425 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
429 /* We need to detect unmap's after close. To do that, we need our own
430 * vm_operations_struct's. And we need to set them up for both the
431 * private and shared mappings. The fun part is that these are all static
432 * so we'll have to initialize on the fly!
434 static struct vm_operations_struct afs_private_mmap_ops;
435 static int afs_private_mmap_ops_inited = 0;
436 static struct vm_operations_struct afs_shared_mmap_ops;
437 static int afs_shared_mmap_ops_inited = 0;
440 afs_linux_vma_close(struct vm_area_struct *vmap)
449 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
454 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
455 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
456 ICL_TYPE_INT32, vcp->execsOrWriters);
457 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
458 ObtainWriteLock(&vcp->lock, 532);
461 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
462 MyPidxx, &vcp, &vmap);
466 ReleaseWriteLock(&vcp->lock);
468 if (need_unlock && vcp->execsOrWriters < 2) {
470 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
471 /* only decrement the execsOrWriters flag if this is not a
473 if (!(vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
474 vcp->execsOrWriters--;
475 vcp->states &= ~CMAPPED;
477 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
478 vcp->execsOrWriters--;
479 /* If we did not have the lock */
482 if (!vcp->execsOrWriters)
483 vcp->execsOrWriters = 1;
488 ReleaseWriteLock(&vcp->lock);
496 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
498 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
499 cred_t *credp = crref();
500 struct vrequest treq;
504 #if defined(AFS_LINUX24_ENV)
505 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
506 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
507 vmap->vm_end - vmap->vm_start);
509 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
510 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
511 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
515 /* get a validated vcache entry */
516 code = afs_InitReq(&treq, credp);
518 code = afs_VerifyVCache(vcp, &treq);
524 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
527 code = generic_file_mmap(fp, vmap);
532 ObtainWriteLock(&vcp->lock, 531);
533 /* Set out vma ops so we catch the close. The following test should be
534 * the same as used in generic_file_mmap.
536 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
537 if (!afs_shared_mmap_ops_inited) {
538 afs_shared_mmap_ops_inited = 1;
539 afs_shared_mmap_ops = *vmap->vm_ops;
540 afs_shared_mmap_ops.close = afs_linux_vma_close;
542 vmap->vm_ops = &afs_shared_mmap_ops;
544 if (!afs_private_mmap_ops_inited) {
545 afs_private_mmap_ops_inited = 1;
546 afs_private_mmap_ops = *vmap->vm_ops;
547 afs_private_mmap_ops.close = afs_linux_vma_close;
549 vmap->vm_ops = &afs_private_mmap_ops;
553 /* Add an open reference on the first mapping. */
554 if (vcp->mapcnt == 0) {
555 vcp->execsOrWriters++;
557 vcp->states |= CMAPPED;
559 ReleaseWriteLock(&vcp->lock);
569 afs_linux_open(struct inode *ip, struct file *fp)
572 cred_t *credp = crref();
575 #ifdef AFS_LINUX24_ENV
578 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
579 #ifdef AFS_LINUX24_ENV
588 /* afs_Close is called from release, since release is used to handle all
589 * file closings. In addition afs_linux_flush is called from sys_close to
590 * handle flushing the data back to the server. The kicker is that we could
591 * ignore flush completely if only sys_close took it's return value from
592 * fput. See afs_linux_flush for notes on interactions between release and
596 afs_linux_release(struct inode *ip, struct file *fp)
599 cred_t *credp = crref();
600 struct vcache *vcp = ITOAFS(ip);
603 #ifdef AFS_LINUX24_ENV
607 vcp->flushcnt--; /* protected by AFS global lock. */
609 code = afs_close(vcp, fp->f_flags, credp);
611 #ifdef AFS_LINUX24_ENV
620 #if defined(AFS_LINUX24_ENV)
622 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
625 afs_linux_fsync(struct file *fp, struct dentry *dp)
629 struct inode *ip = FILE_INODE(fp);
630 cred_t *credp = crref();
633 #ifdef AFS_LINUX24_ENV
636 code = afs_fsync(ITOAFS(ip), credp);
637 #ifdef AFS_LINUX24_ENV
647 /* No support for async i/o */
648 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
650 /* I don't think it will, at least not as can be detected here. */
651 int afs_linux_check_media_change(kdev_t dev);
653 /* Revalidate media and file system. */
654 int afs_linux_file_revalidate(kdev_t dev);
658 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
661 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
662 cred_t *credp = crref();
663 #ifdef AFS_LINUX24_ENV
664 struct flock64 flock;
669 /* Convert to a lock format afs_lockctl understands. */
670 memset((char *)&flock, 0, sizeof(flock));
671 flock.l_type = flp->fl_type;
672 flock.l_pid = flp->fl_pid;
674 flock.l_start = flp->fl_start;
675 flock.l_len = flp->fl_end - flp->fl_start;
677 /* Safe because there are no large files, yet */
678 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
679 if (cmd == F_GETLK64)
681 else if (cmd == F_SETLK64)
683 else if (cmd == F_SETLKW64)
685 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
688 code = afs_lockctl(vcp, &flock, cmd, credp);
691 /* Convert flock back to Linux's file_lock */
692 flp->fl_type = flock.l_type;
693 flp->fl_pid = flock.l_pid;
694 flp->fl_start = flock.l_start;
695 flp->fl_end = flock.l_start + flock.l_len;
703 * flush is called from sys_close. We could ignore it, but sys_close return
704 * code comes from flush, not release. We need to use release to keep
705 * the vcache open count correct. Note that flush is called before release
706 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
707 * races and also avoid calling afs_close twice when closing the file.
708 * If we merely checked for opens > 0 in afs_linux_release, then if an
709 * new open occurred when storing back the file, afs_linux_release would
710 * incorrectly close the file and decrement the opens count. Calling afs_close
711 * on the just flushed file is wasteful, since the background daemon will
712 * execute the code that finally decides there is nothing to do.
715 afs_linux_flush(struct file *fp)
717 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
721 /* Only do this on the last close of the file pointer. */
722 #if defined(AFS_LINUX24_ENV)
723 if (atomic_read(&fp->f_count) > 1)
732 code = afs_close(vcp, fp->f_flags, credp);
733 vcp->flushcnt++; /* protected by AFS global lock. */
740 /* Not allowed to directly read a directory. */
742 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
749 #if defined(AFS_LINUX24_ENV)
750 struct file_operations afs_dir_fops = {
751 read:generic_read_dir,
752 readdir:afs_linux_readdir,
755 release:afs_linux_release,
758 struct file_operations afs_dir_fops = {
759 NULL, /* afs_linux_lseek */
761 NULL, /* afs_linux_write */
763 NULL, /* afs_linux_select */
764 afs_xioctl, /* close enough to use the ported AFS one */
765 NULL, /* afs_linux_mmap */
767 NULL, /* afs_linux_flush */
770 NULL, /* afs_linux_fasync */
771 NULL, /* afs_linux_check_media_change */
772 NULL, /* afs_linux_file_revalidate */
777 #if defined(AFS_LINUX24_ENV)
778 struct file_operations afs_file_fops = {
780 write:afs_linux_write,
784 flush:afs_linux_flush,
785 release:afs_linux_release,
786 fsync:afs_linux_fsync,
790 struct file_operations afs_file_fops = {
791 NULL, /* afs_linux_lseek */
794 NULL, /* afs_linux_readdir */
795 NULL, /* afs_linux_select */
796 afs_xioctl, /* close enough to use the ported AFS one */
802 NULL, /* afs_linux_fasync */
803 NULL, /* afs_linux_check_media_change */
804 NULL, /* afs_linux_file_revalidate */
810 /**********************************************************************
811 * AFS Linux dentry operations
812 **********************************************************************/
814 /* afs_linux_revalidate
815 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
818 afs_linux_revalidate(struct dentry *dp)
822 struct vrequest treq;
823 struct vcache *vcp = ITOAFS(dp->d_inode);
824 struct vcache *rootvp = NULL;
828 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
829 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
830 ObtainSharedLock(&afs_xvcache, 680);
831 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
832 ReleaseSharedLock(&afs_xvcache);
834 #ifdef AFS_LINUX24_ENV
838 /* Make this a fast path (no crref), since it's called so often. */
839 if (vcp->states & CStatd) {
840 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
841 check_bad_parent(dp); /* check and correct mvid */
843 vcache2fakeinode(rootvp, vcp);
846 #ifdef AFS_LINUX24_ENV
850 afs_PutVCache(rootvp);
856 code = afs_InitReq(&treq, credp);
858 code = afs_VerifyVCache(vcp, &treq);
860 #ifdef AFS_LINUX24_ENV
870 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
871 * In kernels 2.2.10 and above, we are passed an additional flags var which
872 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
873 * we are advised to follow the entry if it is a link or to make sure that
874 * it is a directory. But since the kernel itself checks these possibilities
875 * later on, we shouldn't have to do it until later. Perhaps in the future..
877 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
879 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
882 afs_linux_dentry_revalidate(struct dentry *dp)
886 cred_t *credp = crref();
887 struct vrequest treq;
888 struct vcache *lookupvcp = NULL;
889 int code, bad_dentry = 1;
890 struct sysname_info sysState;
891 struct vcache *vcp = ITOAFS(dp->d_inode);
892 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
897 sysState.allocked = 0;
899 /* If it's a negative dentry, then there's nothing to do. */
900 if (!vcp || !parentvcp)
903 /* If it is the AFS root, then there's no chance it needs
905 if (vcp == afs_globalVp) {
910 if ((code = afs_InitReq(&treq, credp)))
913 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
914 name = sysState.name;
916 /* First try looking up the DNLC */
917 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
918 /* Verify that the dentry does not point to an old inode */
919 if (vcp != lookupvcp)
921 /* Check and correct mvid */
922 if (*name != '/' && vcp->mvstat == 2)
923 check_bad_parent(dp);
929 /* A DNLC lookup failure cannot be trusted. Try a real lookup.
930 Make sure to try the real name and not the @sys expansion;
931 afs_lookup will expand @sys itself. */
933 code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
935 /* Verify that the dentry does not point to an old inode */
936 if (vcp != lookupvcp)
944 afs_PutVCache(lookupvcp);
945 if (sysState.allocked)
946 osi_FreeLargeSpace(name);
952 shrink_dcache_parent(dp);
960 /* afs_dentry_iput */
962 afs_dentry_iput(struct dentry *dp, struct inode *ip)
964 if (ICL_SETACTIVE(afs_iclSetp)) {
966 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
967 ICL_TYPE_STRING, dp->d_parent->d_name.name,
968 ICL_TYPE_STRING, dp->d_name.name);
976 afs_dentry_delete(struct dentry *dp)
978 if (ICL_SETACTIVE(afs_iclSetp)) {
980 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
981 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
982 ICL_TYPE_STRING, dp->d_name.name);
986 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
987 return 1; /* bad inode? */
992 #if defined(AFS_LINUX24_ENV)
993 struct dentry_operations afs_dentry_operations = {
994 d_revalidate:afs_linux_dentry_revalidate,
995 d_iput:afs_dentry_iput,
996 d_delete:afs_dentry_delete,
998 struct dentry_operations *afs_dops = &afs_dentry_operations;
1000 struct dentry_operations afs_dentry_operations = {
1001 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
1003 NULL, /* d_compare */
1004 afs_dentry_delete, /* d_delete(struct dentry *) */
1005 NULL, /* d_release(struct dentry *) */
1006 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
1008 struct dentry_operations *afs_dops = &afs_dentry_operations;
1011 /**********************************************************************
1012 * AFS Linux inode operations
1013 **********************************************************************/
1017 * Merely need to set enough of vattr to get us through the create. Note
1018 * that the higher level code (open_namei) will take care of any tuncation
1019 * explicitly. Exclusive open is also taken care of in open_namei.
1021 * name is in kernel space at this point.
1024 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1027 cred_t *credp = crref();
1030 const char *name = dp->d_name.name;
1034 vattr.va_mode = mode;
1038 afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
1039 (struct vcache **)&ip, credp);
1042 vattr2inode(ip, &vattr);
1043 /* Reset ops if symlink or directory. */
1044 #if defined(AFS_LINUX24_ENV)
1045 if (S_ISREG(ip->i_mode)) {
1046 ip->i_op = &afs_file_iops;
1047 ip->i_fop = &afs_file_fops;
1048 ip->i_data.a_ops = &afs_file_aops;
1049 } else if (S_ISDIR(ip->i_mode)) {
1050 ip->i_op = &afs_dir_iops;
1051 ip->i_fop = &afs_dir_fops;
1052 } else if (S_ISLNK(ip->i_mode)) {
1053 ip->i_op = &afs_symlink_iops;
1054 ip->i_data.a_ops = &afs_symlink_aops;
1055 ip->i_mapping = &ip->i_data;
1057 printk("afs_linux_create: FIXME\n");
1059 if (S_ISDIR(ip->i_mode))
1060 ip->i_op = &afs_dir_iops;
1061 else if (S_ISLNK(ip->i_mode))
1062 ip->i_op = &afs_symlink_iops;
1065 dp->d_op = afs_dops;
1066 dp->d_time = jiffies;
1067 d_instantiate(dp, ip);
1075 /* afs_linux_lookup */
1076 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1078 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1081 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1085 cred_t *credp = crref();
1086 struct vcache *vcp = NULL;
1087 const char *comp = dp->d_name.name;
1089 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1092 struct inode *ip = AFSTOI(vcp);
1093 /* Reset ops if symlink or directory. */
1094 #if defined(AFS_LINUX24_ENV)
1095 if (S_ISREG(ip->i_mode)) {
1096 ip->i_op = &afs_file_iops;
1097 ip->i_fop = &afs_file_fops;
1098 ip->i_data.a_ops = &afs_file_aops;
1099 } else if (S_ISDIR(ip->i_mode)) {
1100 ip->i_op = &afs_dir_iops;
1101 ip->i_fop = &afs_dir_fops;
1102 } else if (S_ISLNK(ip->i_mode)) {
1103 ip->i_op = &afs_symlink_iops;
1104 ip->i_data.a_ops = &afs_symlink_aops;
1105 ip->i_mapping = &ip->i_data;
1108 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1109 ip->i_mode, dp->d_name.name, code);
1111 if (S_ISDIR(ip->i_mode))
1112 ip->i_op = &afs_dir_iops;
1113 else if (S_ISLNK(ip->i_mode))
1114 ip->i_op = &afs_symlink_iops;
1117 dp->d_time = jiffies;
1118 dp->d_op = afs_dops;
1119 d_add(dp, AFSTOI(vcp));
1124 /* It's ok for the file to not be found. That's noted by the caller by
1125 * seeing that the dp->d_inode field is NULL.
1127 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1131 return ERR_PTR(-code);
1140 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1143 cred_t *credp = crref();
1144 const char *name = newdp->d_name.name;
1145 struct inode *oldip = olddp->d_inode;
1147 /* If afs_link returned the vnode, we could instantiate the
1148 * dentry. Since it's not, we drop this one and do a new lookup.
1153 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1161 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1164 cred_t *credp = crref();
1165 const char *name = dp->d_name.name;
1168 code = afs_remove(ITOAFS(dip), name, credp);
1178 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1181 cred_t *credp = crref();
1183 const char *name = dp->d_name.name;
1185 /* If afs_symlink returned the vnode, we could instantiate the
1186 * dentry. Since it's not, we drop this one and do a new lookup.
1192 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1199 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1202 cred_t *credp = crref();
1203 struct vcache *tvcp = NULL;
1205 const char *name = dp->d_name.name;
1209 vattr.va_mask = ATTR_MODE;
1210 vattr.va_mode = mode;
1211 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1214 tvcp->v.v_op = &afs_dir_iops;
1215 #if defined(AFS_LINUX24_ENV)
1216 tvcp->v.v_fop = &afs_dir_fops;
1218 dp->d_op = afs_dops;
1219 dp->d_time = jiffies;
1220 d_instantiate(dp, AFSTOI(tvcp));
1229 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1232 cred_t *credp = crref();
1233 const char *name = dp->d_name.name;
1236 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) {
1258 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1259 struct inode *newip, struct dentry *newdp)
1262 cred_t *credp = crref();
1263 const char *oldname = olddp->d_name.name;
1264 const char *newname = newdp->d_name.name;
1266 /* Remove old and new entries from name hash. New one will change below.
1267 * While it's optimal to catch failures and re-insert newdp into hash,
1268 * it's also error prone and in that case we're already dealing with error
1269 * cases. Let another lookup put things right, if need be.
1271 if (!list_empty(&olddp->d_hash)) {
1274 if (!list_empty(&newdp->d_hash)) {
1278 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1282 /* update time so it doesn't expire immediately */
1283 newdp->d_time = jiffies;
1284 d_move(olddp, newdp);
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 = atomic_read(&pp->count);
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)
1489 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1491 afs_linux_bmap(struct inode *ip, int)
1496 /* afs_linux_truncate
1497 * Handles discarding disk blocks if this were a device. ext2 indicates we
1498 * may need to zero partial last pages of memory mapped files.
1501 afs_linux_truncate(struct inode *ip)
1506 /* afs_linux_permission
1507 * Check access rights - returns error if can't check or permission denied.
1510 afs_linux_permission(struct inode *ip, int mode)
1513 cred_t *credp = crref();
1517 if (mode & MAY_EXEC)
1519 if (mode & MAY_READ)
1521 if (mode & MAY_WRITE)
1523 code = afs_access(ITOAFS(ip), tmp, credp);
1532 /* msdos sector mapping hack for memory mapping. */
1534 afs_linux_smap(struct inode *ip, int)
1540 #if defined(AFS_LINUX24_ENV)
1542 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1543 unsigned long offset, unsigned int count)
1545 struct vcache *vcp = ITOAFS(ip);
1554 buffer = kmap(pp) + offset;
1555 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1558 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1559 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1560 ICL_TYPE_INT32, 99999);
1562 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1564 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1569 && afs_stats_cmperf.cacheCurrDirtyChunks >
1570 afs_stats_cmperf.cacheMaxDirtyChunks) {
1571 struct vrequest treq;
1573 ObtainWriteLock(&vcp->lock, 533);
1574 if (!afs_InitReq(&treq, credp))
1575 code = afs_DoPartialWrite(vcp, &treq);
1576 ReleaseWriteLock(&vcp->lock);
1578 code = code ? -code : count - tuio.uio_resid;
1580 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1581 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1582 ICL_TYPE_INT32, code);
1591 afs_linux_updatepage(struct file *file, struct page *page,
1592 unsigned long offset, unsigned int count)
1594 struct dentry *dentry = file->f_dentry;
1596 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1599 /* afs_linux_updatepage
1600 * What one would have thought was writepage - write dirty page to file.
1601 * Called from generic_file_write. buffer is still in user space. pagep
1602 * has been filled in with old data if we're updating less than a page.
1605 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1606 unsigned int count, int sync)
1608 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1609 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1615 set_bit(PG_locked, &pp->flags);
1619 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1620 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1621 ICL_TYPE_INT32, 99999);
1622 setup_uio(&tuio, &iovec, page_addr + offset,
1623 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1626 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1630 code = code ? -code : count - tuio.uio_resid;
1631 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1632 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1633 ICL_TYPE_INT32, code);
1638 clear_bit(PG_locked, &pp->flags);
1643 #if defined(AFS_LINUX24_ENV)
1645 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1652 code = afs_linux_updatepage(file, page, offset, to - offset);
1661 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1668 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1671 #if defined(AFS_LINUX24_ENV)
1672 struct inode_operations afs_file_iops = {
1673 revalidate:afs_linux_revalidate,
1674 setattr:afs_notify_change,
1675 permission:afs_linux_permission,
1677 struct address_space_operations afs_file_aops = {
1678 readpage:afs_linux_readpage,
1679 writepage:afs_linux_writepage,
1680 commit_write:afs_linux_commit_write,
1681 prepare_write:afs_linux_prepare_write,
1684 struct inode_operations *afs_ops = &afs_file_iops;
1686 struct inode_operations afs_iops = {
1687 &afs_file_fops, /* file operations */
1688 NULL, /* afs_linux_create */
1689 NULL, /* afs_linux_lookup */
1690 NULL, /* afs_linux_link */
1691 NULL, /* afs_linux_unlink */
1692 NULL, /* afs_linux_symlink */
1693 NULL, /* afs_linux_mkdir */
1694 NULL, /* afs_linux_rmdir */
1695 NULL, /* afs_linux_mknod */
1696 NULL, /* afs_linux_rename */
1697 NULL, /* afs_linux_readlink */
1698 NULL, /* afs_linux_follow_link */
1700 NULL, /* afs_linux_writepage */
1701 NULL, /* afs_linux_bmap */
1702 NULL, /* afs_linux_truncate */
1703 afs_linux_permission,
1704 NULL, /* afs_linux_smap */
1705 afs_linux_updatepage,
1706 afs_linux_revalidate,
1709 struct inode_operations *afs_ops = &afs_iops;
1712 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1713 * by what sort of operation is allowed.....
1715 #if defined(AFS_LINUX24_ENV)
1716 struct inode_operations afs_dir_iops = {
1717 create:afs_linux_create,
1718 lookup:afs_linux_lookup,
1719 link:afs_linux_link,
1720 unlink:afs_linux_unlink,
1721 symlink:afs_linux_symlink,
1722 mkdir:afs_linux_mkdir,
1723 rmdir:afs_linux_rmdir,
1724 rename:afs_linux_rename,
1725 revalidate:afs_linux_revalidate,
1726 setattr:afs_notify_change,
1727 permission:afs_linux_permission,
1730 struct inode_operations afs_dir_iops = {
1731 &afs_dir_fops, /* file operations for directories */
1739 NULL, /* afs_linux_mknod */
1741 NULL, /* afs_linux_readlink */
1742 NULL, /* afs_linux_follow_link */
1743 NULL, /* afs_linux_readpage */
1744 NULL, /* afs_linux_writepage */
1745 NULL, /* afs_linux_bmap */
1746 NULL, /* afs_linux_truncate */
1747 afs_linux_permission,
1748 NULL, /* afs_linux_smap */
1749 NULL, /* afs_linux_updatepage */
1750 afs_linux_revalidate,
1754 /* We really need a separate symlink set of ops, since do_follow_link()
1755 * determines if it _is_ a link by checking if the follow_link op is set.
1757 #if defined(AFS_LINUX24_ENV)
1759 afs_symlink_filler(struct file *file, struct page *page)
1761 struct inode *ip = (struct inode *)page->mapping->host;
1762 char *p = (char *)kmap(page);
1767 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1771 p[code] = '\0'; /* null terminate? */
1775 SetPageUptodate(page);
1790 struct address_space_operations afs_symlink_aops = {
1791 readpage:afs_symlink_filler
1794 struct inode_operations afs_symlink_iops = {
1795 readlink:page_readlink,
1796 follow_link:page_follow_link,
1797 setattr:afs_notify_change,
1800 struct inode_operations afs_symlink_iops = {
1801 NULL, /* file operations */
1809 NULL, /* afs_linux_mknod */
1812 afs_linux_follow_link,
1813 NULL, /* readpage */
1814 NULL, /* afs_linux_writepage */
1815 NULL, /* afs_linux_bmap */
1816 NULL, /* afs_linux_truncate */
1817 afs_linux_permission, /* tho the code appears to indicate not used? */
1818 NULL, /* afs_linux_smap */
1819 NULL, /* updatepage */
1820 afs_linux_revalidate, /* tho the code appears to indicate not used? */