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 ..... */
353 len = strlen(de->name);
355 /* filldir returns -EINVAL when the buffer is full. */
356 #if (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);
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);
405 afs_PutFakeStat(&fakestat);
411 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
414 /* in afs_pioctl.c */
415 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
419 /* We need to detect unmap's after close. To do that, we need our own
420 * vm_operations_struct's. And we need to set them up for both the
421 * private and shared mappings. The fun part is that these are all static
422 * so we'll have to initialize on the fly!
424 static struct vm_operations_struct afs_private_mmap_ops;
425 static int afs_private_mmap_ops_inited = 0;
426 static struct vm_operations_struct afs_shared_mmap_ops;
427 static int afs_shared_mmap_ops_inited = 0;
430 afs_linux_vma_close(struct vm_area_struct *vmap)
439 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
444 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
445 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
446 ICL_TYPE_INT32, vcp->execsOrWriters);
447 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
448 ObtainWriteLock(&vcp->lock, 532);
451 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
452 MyPidxx, &vcp, &vmap);
456 ReleaseWriteLock(&vcp->lock);
458 if (need_unlock && vcp->execsOrWriters < 2) {
460 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
461 /* only decrement the execsOrWriters flag if this is not a
463 if (!(vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
464 vcp->execsOrWriters--;
465 vcp->states &= ~CMAPPED;
467 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
468 vcp->execsOrWriters--;
469 /* If we did not have the lock */
472 if (!vcp->execsOrWriters)
473 vcp->execsOrWriters = 1;
478 ReleaseWriteLock(&vcp->lock);
486 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
488 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
489 cred_t *credp = crref();
490 struct vrequest treq;
494 #if defined(AFS_LINUX24_ENV)
495 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
496 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
497 vmap->vm_end - vmap->vm_start);
499 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
500 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
501 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
505 /* get a validated vcache entry */
506 code = afs_InitReq(&treq, credp);
508 code = afs_VerifyVCache(vcp, &treq);
514 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
517 code = generic_file_mmap(fp, vmap);
522 ObtainWriteLock(&vcp->lock, 531);
523 /* Set out vma ops so we catch the close. The following test should be
524 * the same as used in generic_file_mmap.
526 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
527 if (!afs_shared_mmap_ops_inited) {
528 afs_shared_mmap_ops_inited = 1;
529 afs_shared_mmap_ops = *vmap->vm_ops;
530 afs_shared_mmap_ops.close = afs_linux_vma_close;
532 vmap->vm_ops = &afs_shared_mmap_ops;
534 if (!afs_private_mmap_ops_inited) {
535 afs_private_mmap_ops_inited = 1;
536 afs_private_mmap_ops = *vmap->vm_ops;
537 afs_private_mmap_ops.close = afs_linux_vma_close;
539 vmap->vm_ops = &afs_private_mmap_ops;
543 /* Add an open reference on the first mapping. */
544 if (vcp->mapcnt == 0) {
545 vcp->execsOrWriters++;
547 vcp->states |= CMAPPED;
549 ReleaseWriteLock(&vcp->lock);
559 afs_linux_open(struct inode *ip, struct file *fp)
562 cred_t *credp = crref();
565 #ifdef AFS_LINUX24_ENV
568 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
569 #ifdef AFS_LINUX24_ENV
578 /* afs_Close is called from release, since release is used to handle all
579 * file closings. In addition afs_linux_flush is called from sys_close to
580 * handle flushing the data back to the server. The kicker is that we could
581 * ignore flush completely if only sys_close took it's return value from
582 * fput. See afs_linux_flush for notes on interactions between release and
586 afs_linux_release(struct inode *ip, struct file *fp)
589 cred_t *credp = crref();
590 struct vcache *vcp = ITOAFS(ip);
593 #ifdef AFS_LINUX24_ENV
597 vcp->flushcnt--; /* protected by AFS global lock. */
599 code = afs_close(vcp, fp->f_flags, credp);
601 #ifdef AFS_LINUX24_ENV
610 #if defined(AFS_LINUX24_ENV)
612 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
615 afs_linux_fsync(struct file *fp, struct dentry *dp)
619 struct inode *ip = FILE_INODE(fp);
620 cred_t *credp = crref();
623 #ifdef AFS_LINUX24_ENV
626 code = afs_fsync(ITOAFS(ip), credp);
627 #ifdef AFS_LINUX24_ENV
637 /* No support for async i/o */
638 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
640 /* I don't think it will, at least not as can be detected here. */
641 int afs_linux_check_media_change(kdev_t dev);
643 /* Revalidate media and file system. */
644 int afs_linux_file_revalidate(kdev_t dev);
648 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
651 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
652 cred_t *credp = crref();
653 #ifdef AFS_LINUX24_ENV
654 struct flock64 flock;
659 /* Convert to a lock format afs_lockctl understands. */
660 memset((char *)&flock, 0, sizeof(flock));
661 flock.l_type = flp->fl_type;
662 flock.l_pid = flp->fl_pid;
664 flock.l_start = flp->fl_start;
665 flock.l_len = flp->fl_end - flp->fl_start;
667 /* Safe because there are no large files, yet */
668 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
669 if (cmd == F_GETLK64)
671 else if (cmd == F_SETLK64)
673 else if (cmd == F_SETLKW64)
675 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
678 code = afs_lockctl(vcp, &flock, cmd, credp);
681 /* Convert flock back to Linux's file_lock */
682 flp->fl_type = flock.l_type;
683 flp->fl_pid = flock.l_pid;
684 flp->fl_start = flock.l_start;
685 flp->fl_end = flock.l_start + flock.l_len;
693 * flush is called from sys_close. We could ignore it, but sys_close return
694 * code comes from flush, not release. We need to use release to keep
695 * the vcache open count correct. Note that flush is called before release
696 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
697 * races and also avoid calling afs_close twice when closing the file.
698 * If we merely checked for opens > 0 in afs_linux_release, then if an
699 * new open occurred when storing back the file, afs_linux_release would
700 * incorrectly close the file and decrement the opens count. Calling afs_close
701 * on the just flushed file is wasteful, since the background daemon will
702 * execute the code that finally decides there is nothing to do.
705 afs_linux_flush(struct file *fp)
707 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
711 /* Only do this on the last close of the file pointer. */
712 #if defined(AFS_LINUX24_ENV)
713 if (atomic_read(&fp->f_count) > 1)
722 code = afs_close(vcp, fp->f_flags, credp);
723 vcp->flushcnt++; /* protected by AFS global lock. */
730 /* Not allowed to directly read a directory. */
732 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
739 #if defined(AFS_LINUX24_ENV)
740 struct file_operations afs_dir_fops = {
741 read:generic_read_dir,
742 readdir:afs_linux_readdir,
745 release:afs_linux_release,
748 struct file_operations afs_dir_fops = {
749 NULL, /* afs_linux_lseek */
751 NULL, /* afs_linux_write */
753 NULL, /* afs_linux_select */
754 afs_xioctl, /* close enough to use the ported AFS one */
755 NULL, /* afs_linux_mmap */
757 NULL, /* afs_linux_flush */
760 NULL, /* afs_linux_fasync */
761 NULL, /* afs_linux_check_media_change */
762 NULL, /* afs_linux_file_revalidate */
767 #if defined(AFS_LINUX24_ENV)
768 struct file_operations afs_file_fops = {
770 write:afs_linux_write,
774 flush:afs_linux_flush,
775 release:afs_linux_release,
776 fsync:afs_linux_fsync,
780 struct file_operations afs_file_fops = {
781 NULL, /* afs_linux_lseek */
784 NULL, /* afs_linux_readdir */
785 NULL, /* afs_linux_select */
786 afs_xioctl, /* close enough to use the ported AFS one */
792 NULL, /* afs_linux_fasync */
793 NULL, /* afs_linux_check_media_change */
794 NULL, /* afs_linux_file_revalidate */
800 /**********************************************************************
801 * AFS Linux dentry operations
802 **********************************************************************/
804 /* afs_linux_revalidate
805 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
808 afs_linux_revalidate(struct dentry *dp)
812 struct vrequest treq;
813 struct vcache *vcp = ITOAFS(dp->d_inode);
814 struct vcache *rootvp = NULL;
818 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
819 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
820 ObtainSharedLock(&afs_xvcache, 680);
821 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
822 ReleaseSharedLock(&afs_xvcache);
824 #ifdef AFS_LINUX24_ENV
828 /* Make this a fast path (no crref), since it's called so often. */
829 if (vcp->states & CStatd) {
830 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
831 check_bad_parent(dp); /* check and correct mvid */
833 vcache2fakeinode(rootvp, vcp);
836 #ifdef AFS_LINUX24_ENV
840 afs_PutVCache(rootvp);
846 code = afs_InitReq(&treq, credp);
848 code = afs_VerifyVCache(vcp, &treq);
850 #ifdef AFS_LINUX24_ENV
860 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
861 * In kernels 2.2.10 and above, we are passed an additional flags var which
862 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
863 * we are advised to follow the entry if it is a link or to make sure that
864 * it is a directory. But since the kernel itself checks these possibilities
865 * later on, we shouldn't have to do it until later. Perhaps in the future..
867 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
869 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
872 afs_linux_dentry_revalidate(struct dentry *dp)
876 cred_t *credp = crref();
877 struct vrequest treq;
878 struct vcache *lookupvcp = NULL;
879 int code, bad_dentry = 1;
880 struct sysname_info sysState;
881 struct vcache *vcp = ITOAFS(dp->d_inode);
882 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
887 sysState.allocked = 0;
889 /* If it's a negative dentry, then there's nothing to do. */
890 if (!vcp || !parentvcp)
893 /* If it is the AFS root, then there's no chance it needs
895 if (vcp == afs_globalVp) {
900 if ((code = afs_InitReq(&treq, credp)))
903 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
904 name = sysState.name;
906 /* First try looking up the DNLC */
907 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
908 /* Verify that the dentry does not point to an old inode */
909 if (vcp != lookupvcp)
911 /* Check and correct mvid */
912 if (*name != '/' && vcp->mvstat == 2)
913 check_bad_parent(dp);
919 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
920 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
922 /* Verify that the dentry does not point to an old inode */
923 if (vcp != lookupvcp)
931 afs_PutVCache(lookupvcp);
932 if (sysState.allocked)
933 osi_FreeLargeSpace(name);
939 shrink_dcache_parent(dp);
948 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
950 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
953 afs_linux_dentry_revalidate(struct dentry *dp)
958 struct vrequest treq;
959 struct inode *ip = AFSTOI(dp->d_inode);
961 unsigned long timeout = 3 * HZ; /* 3 seconds */
964 printk("negative dentry: %s\n", dp->d_name.name);
966 if (!(flags & LOOKUP_CONTINUE)) {
967 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
973 if (time_after(jiffies, dp->d_time + timeout))
984 /* afs_dentry_iput */
986 afs_dentry_iput(struct dentry *dp, struct inode *ip)
988 if (ICL_SETACTIVE(afs_iclSetp)) {
990 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
991 ICL_TYPE_STRING, dp->d_parent->d_name.name,
992 ICL_TYPE_STRING, dp->d_name.name);
1000 afs_dentry_delete(struct dentry *dp)
1002 if (ICL_SETACTIVE(afs_iclSetp)) {
1004 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
1005 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
1006 ICL_TYPE_STRING, dp->d_name.name);
1010 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
1011 return 1; /* bad inode? */
1016 #if defined(AFS_LINUX24_ENV)
1017 struct dentry_operations afs_dentry_operations = {
1018 d_revalidate:afs_linux_dentry_revalidate,
1019 d_iput:afs_dentry_iput,
1020 d_delete:afs_dentry_delete,
1022 struct dentry_operations *afs_dops = &afs_dentry_operations;
1024 struct dentry_operations afs_dentry_operations = {
1025 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
1027 NULL, /* d_compare */
1028 afs_dentry_delete, /* d_delete(struct dentry *) */
1029 NULL, /* d_release(struct dentry *) */
1030 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
1032 struct dentry_operations *afs_dops = &afs_dentry_operations;
1035 /**********************************************************************
1036 * AFS Linux inode operations
1037 **********************************************************************/
1041 * Merely need to set enough of vattr to get us through the create. Note
1042 * that the higher level code (open_namei) will take care of any tuncation
1043 * explicitly. Exclusive open is also taken care of in open_namei.
1045 * name is in kernel space at this point.
1048 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1051 cred_t *credp = crref();
1054 const char *name = dp->d_name.name;
1058 vattr.va_mode = mode;
1062 afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
1063 (struct vcache **)&ip, credp);
1066 vattr2inode(ip, &vattr);
1067 /* Reset ops if symlink or directory. */
1068 #if defined(AFS_LINUX24_ENV)
1069 if (S_ISREG(ip->i_mode)) {
1070 ip->i_op = &afs_file_iops;
1071 ip->i_fop = &afs_file_fops;
1072 ip->i_data.a_ops = &afs_file_aops;
1073 } else if (S_ISDIR(ip->i_mode)) {
1074 ip->i_op = &afs_dir_iops;
1075 ip->i_fop = &afs_dir_fops;
1076 } else if (S_ISLNK(ip->i_mode)) {
1077 ip->i_op = &afs_symlink_iops;
1078 ip->i_data.a_ops = &afs_symlink_aops;
1079 ip->i_mapping = &ip->i_data;
1081 printk("afs_linux_create: FIXME\n");
1083 if (S_ISDIR(ip->i_mode))
1084 ip->i_op = &afs_dir_iops;
1085 else if (S_ISLNK(ip->i_mode))
1086 ip->i_op = &afs_symlink_iops;
1089 dp->d_op = afs_dops;
1090 dp->d_time = jiffies;
1091 d_instantiate(dp, ip);
1099 /* afs_linux_lookup */
1100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1102 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1105 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1109 cred_t *credp = crref();
1110 struct vcache *vcp = NULL;
1111 const char *comp = dp->d_name.name;
1113 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1116 struct inode *ip = AFSTOI(vcp);
1117 /* Reset ops if symlink or directory. */
1118 #if defined(AFS_LINUX24_ENV)
1119 if (S_ISREG(ip->i_mode)) {
1120 ip->i_op = &afs_file_iops;
1121 ip->i_fop = &afs_file_fops;
1122 ip->i_data.a_ops = &afs_file_aops;
1123 } else if (S_ISDIR(ip->i_mode)) {
1124 ip->i_op = &afs_dir_iops;
1125 ip->i_fop = &afs_dir_fops;
1126 } else if (S_ISLNK(ip->i_mode)) {
1127 ip->i_op = &afs_symlink_iops;
1128 ip->i_data.a_ops = &afs_symlink_aops;
1129 ip->i_mapping = &ip->i_data;
1132 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1133 ip->i_mode, dp->d_name.name, code);
1135 if (S_ISDIR(ip->i_mode))
1136 ip->i_op = &afs_dir_iops;
1137 else if (S_ISLNK(ip->i_mode))
1138 ip->i_op = &afs_symlink_iops;
1141 dp->d_time = jiffies;
1142 dp->d_op = afs_dops;
1143 d_add(dp, AFSTOI(vcp));
1148 /* It's ok for the file to not be found. That's noted by the caller by
1149 * seeing that the dp->d_inode field is NULL.
1151 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1155 return ERR_PTR(-code);
1164 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1167 cred_t *credp = crref();
1168 const char *name = newdp->d_name.name;
1169 struct inode *oldip = olddp->d_inode;
1171 /* If afs_link returned the vnode, we could instantiate the
1172 * dentry. Since it's not, we drop this one and do a new lookup.
1177 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1185 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1188 cred_t *credp = crref();
1189 const char *name = dp->d_name.name;
1192 code = afs_remove(ITOAFS(dip), name, credp);
1202 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1205 cred_t *credp = crref();
1207 const char *name = dp->d_name.name;
1209 /* If afs_symlink returned the vnode, we could instantiate the
1210 * dentry. Since it's not, we drop this one and do a new lookup.
1216 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1223 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1226 cred_t *credp = crref();
1227 struct vcache *tvcp = NULL;
1229 const char *name = dp->d_name.name;
1233 vattr.va_mask = ATTR_MODE;
1234 vattr.va_mode = mode;
1235 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1238 tvcp->v.v_op = &afs_dir_iops;
1239 #if defined(AFS_LINUX24_ENV)
1240 tvcp->v.v_fop = &afs_dir_fops;
1242 dp->d_op = afs_dops;
1243 dp->d_time = jiffies;
1244 d_instantiate(dp, AFSTOI(tvcp));
1253 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1256 cred_t *credp = crref();
1257 const char *name = dp->d_name.name;
1260 code = afs_rmdir(ITOAFS(dip), name, credp);
1262 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1263 * that failed because a directory is not empty. So, we map
1264 * EEXIST to ENOTEMPTY on linux.
1266 if (code == EEXIST) {
1282 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1283 struct inode *newip, struct dentry *newdp)
1286 cred_t *credp = crref();
1287 const char *oldname = olddp->d_name.name;
1288 const char *newname = newdp->d_name.name;
1290 /* Remove old and new entries from name hash. New one will change below.
1291 * While it's optimal to catch failures and re-insert newdp into hash,
1292 * it's also error prone and in that case we're already dealing with error
1293 * cases. Let another lookup put things right, if need be.
1295 if (!list_empty(&olddp->d_hash)) {
1298 if (!list_empty(&newdp->d_hash)) {
1302 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1306 /* update time so it doesn't expire immediately */
1307 newdp->d_time = jiffies;
1308 d_move(olddp, newdp);
1316 /* afs_linux_ireadlink
1317 * Internal readlink which can return link contents to user or kernel space.
1318 * Note that the buffer is NOT supposed to be null-terminated.
1321 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1324 cred_t *credp = crref();
1328 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1329 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1333 return maxlen - tuio.uio_resid;
1338 #if !defined(AFS_LINUX24_ENV)
1339 /* afs_linux_readlink
1340 * Fill target (which is in user space) with contents of symlink.
1343 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1346 struct inode *ip = dp->d_inode;
1349 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1355 /* afs_linux_follow_link
1356 * a file system dependent link following routine.
1359 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1360 unsigned int follow)
1368 name = osi_Alloc(PATH_MAX + 1);
1372 return ERR_PTR(-EIO);
1375 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1380 res = ERR_PTR(code);
1383 res = lookup_dentry(name, basep, follow);
1387 osi_Free(name, PATH_MAX + 1);
1393 /* afs_linux_readpage
1394 * all reads come through here. A strategy-like read call.
1397 afs_linux_readpage(struct file *fp, struct page *pp)
1400 cred_t *credp = crref();
1401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1403 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1405 ulong address = afs_linux_page_address(pp);
1406 afs_offs_t offset = pageoff(pp);
1410 struct inode *ip = FILE_INODE(fp);
1411 int cnt = atomic_read(&pp->count);
1412 struct vcache *avc = ITOAFS(ip);
1415 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 */
1416 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1422 atomic_add(1, &pp->count);
1423 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1424 clear_bit(PG_error, &pp->flags);
1427 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1429 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1430 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1435 if (tuio.uio_resid) /* zero remainder of page */
1436 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1438 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1439 flush_dcache_page(pp);
1440 SetPageUptodate(pp);
1442 set_bit(PG_uptodate, &pp->flags);
1445 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1449 clear_bit(PG_locked, &pp->flags);
1454 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1456 struct vrequest treq;
1458 code = afs_InitReq(&treq, credp);
1459 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1460 tdc = afs_FindDCache(avc, offset);
1462 if (!(tdc->mflags & DFNextStarted))
1463 afs_PrefetchChunk(avc, tdc, credp, &treq);
1466 ReleaseWriteLock(&avc->lock);
1471 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1472 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1478 #if defined(AFS_LINUX24_ENV)
1480 afs_linux_writepage(struct page *pp)
1482 struct address_space *mapping = pp->mapping;
1483 struct inode *inode;
1484 unsigned long end_index;
1485 unsigned offset = PAGE_CACHE_SIZE;
1488 inode = (struct inode *)mapping->host;
1489 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1492 if (pp->index < end_index)
1494 /* things got complicated... */
1495 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1496 /* OK, are we completely out? */
1497 if (pp->index >= end_index + 1 || !offset)
1501 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1503 SetPageUptodate(pp);
1505 if (status == offset)
1513 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1515 afs_linux_bmap(struct inode *ip, int)
1520 /* afs_linux_truncate
1521 * Handles discarding disk blocks if this were a device. ext2 indicates we
1522 * may need to zero partial last pages of memory mapped files.
1525 afs_linux_truncate(struct inode *ip)
1530 /* afs_linux_permission
1531 * Check access rights - returns error if can't check or permission denied.
1534 afs_linux_permission(struct inode *ip, int mode)
1537 cred_t *credp = crref();
1541 if (mode & MAY_EXEC)
1543 if (mode & MAY_READ)
1545 if (mode & MAY_WRITE)
1547 code = afs_access(ITOAFS(ip), tmp, credp);
1556 /* msdos sector mapping hack for memory mapping. */
1558 afs_linux_smap(struct inode *ip, int)
1564 #if defined(AFS_LINUX24_ENV)
1566 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1567 unsigned long offset, unsigned int count)
1569 struct vcache *vcp = ITOAFS(ip);
1578 buffer = kmap(pp) + offset;
1579 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1582 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1583 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1584 ICL_TYPE_INT32, 99999);
1586 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1588 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1593 && afs_stats_cmperf.cacheCurrDirtyChunks >
1594 afs_stats_cmperf.cacheMaxDirtyChunks) {
1595 struct vrequest treq;
1597 ObtainWriteLock(&vcp->lock, 533);
1598 if (!afs_InitReq(&treq, credp))
1599 code = afs_DoPartialWrite(vcp, &treq);
1600 ReleaseWriteLock(&vcp->lock);
1602 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, atomic_read(&pp->count),
1606 ICL_TYPE_INT32, code);
1615 afs_linux_updatepage(struct file *file, struct page *page,
1616 unsigned long offset, unsigned int count)
1618 struct dentry *dentry = file->f_dentry;
1620 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1623 /* afs_linux_updatepage
1624 * What one would have thought was writepage - write dirty page to file.
1625 * Called from generic_file_write. buffer is still in user space. pagep
1626 * has been filled in with old data if we're updating less than a page.
1629 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1630 unsigned int count, int sync)
1632 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1633 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1639 set_bit(PG_locked, &pp->flags);
1643 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1644 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1645 ICL_TYPE_INT32, 99999);
1646 setup_uio(&tuio, &iovec, page_addr + offset,
1647 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1650 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1654 code = code ? -code : count - tuio.uio_resid;
1655 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1656 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1657 ICL_TYPE_INT32, code);
1662 clear_bit(PG_locked, &pp->flags);
1667 #if defined(AFS_LINUX24_ENV)
1669 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1676 code = afs_linux_updatepage(file, page, offset, to - offset);
1685 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1692 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1695 #if defined(AFS_LINUX24_ENV)
1696 struct inode_operations afs_file_iops = {
1697 revalidate:afs_linux_revalidate,
1698 setattr:afs_notify_change,
1699 permission:afs_linux_permission,
1701 struct address_space_operations afs_file_aops = {
1702 readpage:afs_linux_readpage,
1703 writepage:afs_linux_writepage,
1704 commit_write:afs_linux_commit_write,
1705 prepare_write:afs_linux_prepare_write,
1708 struct inode_operations *afs_ops = &afs_file_iops;
1710 struct inode_operations afs_iops = {
1711 &afs_file_fops, /* file operations */
1712 NULL, /* afs_linux_create */
1713 NULL, /* afs_linux_lookup */
1714 NULL, /* afs_linux_link */
1715 NULL, /* afs_linux_unlink */
1716 NULL, /* afs_linux_symlink */
1717 NULL, /* afs_linux_mkdir */
1718 NULL, /* afs_linux_rmdir */
1719 NULL, /* afs_linux_mknod */
1720 NULL, /* afs_linux_rename */
1721 NULL, /* afs_linux_readlink */
1722 NULL, /* afs_linux_follow_link */
1724 NULL, /* afs_linux_writepage */
1725 NULL, /* afs_linux_bmap */
1726 NULL, /* afs_linux_truncate */
1727 afs_linux_permission,
1728 NULL, /* afs_linux_smap */
1729 afs_linux_updatepage,
1730 afs_linux_revalidate,
1733 struct inode_operations *afs_ops = &afs_iops;
1736 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1737 * by what sort of operation is allowed.....
1739 #if defined(AFS_LINUX24_ENV)
1740 struct inode_operations afs_dir_iops = {
1741 create:afs_linux_create,
1742 lookup:afs_linux_lookup,
1743 link:afs_linux_link,
1744 unlink:afs_linux_unlink,
1745 symlink:afs_linux_symlink,
1746 mkdir:afs_linux_mkdir,
1747 rmdir:afs_linux_rmdir,
1748 rename:afs_linux_rename,
1749 revalidate:afs_linux_revalidate,
1750 setattr:afs_notify_change,
1751 permission:afs_linux_permission,
1754 struct inode_operations afs_dir_iops = {
1755 &afs_dir_fops, /* file operations for directories */
1763 NULL, /* afs_linux_mknod */
1765 NULL, /* afs_linux_readlink */
1766 NULL, /* afs_linux_follow_link */
1767 NULL, /* afs_linux_readpage */
1768 NULL, /* afs_linux_writepage */
1769 NULL, /* afs_linux_bmap */
1770 NULL, /* afs_linux_truncate */
1771 afs_linux_permission,
1772 NULL, /* afs_linux_smap */
1773 NULL, /* afs_linux_updatepage */
1774 afs_linux_revalidate,
1778 /* We really need a separate symlink set of ops, since do_follow_link()
1779 * determines if it _is_ a link by checking if the follow_link op is set.
1781 #if defined(AFS_LINUX24_ENV)
1783 afs_symlink_filler(struct file *file, struct page *page)
1785 struct inode *ip = (struct inode *)page->mapping->host;
1786 char *p = (char *)kmap(page);
1791 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1795 p[code] = '\0'; /* null terminate? */
1799 SetPageUptodate(page);
1814 struct address_space_operations afs_symlink_aops = {
1815 readpage:afs_symlink_filler
1818 struct inode_operations afs_symlink_iops = {
1819 readlink:page_readlink,
1820 follow_link:page_follow_link,
1821 setattr:afs_notify_change,
1824 struct inode_operations afs_symlink_iops = {
1825 NULL, /* file operations */
1833 NULL, /* afs_linux_mknod */
1836 afs_linux_follow_link,
1837 NULL, /* readpage */
1838 NULL, /* afs_linux_writepage */
1839 NULL, /* afs_linux_bmap */
1840 NULL, /* afs_linux_truncate */
1841 afs_linux_permission, /* tho the code appears to indicate not used? */
1842 NULL, /* afs_linux_smap */
1843 NULL, /* updatepage */
1844 afs_linux_revalidate, /* tho the code appears to indicate not used? */