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"
28 #include "afs/sysincludes.h"
29 #include "afsincludes.h"
30 #include "afs/afs_stats.h"
31 #include "afs/afs_osidnlc.h"
33 #include "h/pagemap.h"
34 #if defined(AFS_LINUX24_ENV)
35 #include "h/smp_lock.h"
39 #define pageoff(pp) pgoff2loff((pp)->index)
41 #define pageoff(pp) pp->offset
44 extern struct vcache *afs_globalVp;
45 extern afs_rwlock_t afs_xvcache;
47 extern struct dentry_operations *afs_dops;
48 #if defined(AFS_LINUX24_ENV)
49 extern struct inode_operations afs_file_iops;
50 extern struct address_space_operations afs_file_aops;
51 struct address_space_operations afs_symlink_aops;
53 extern struct inode_operations afs_dir_iops;
54 extern struct inode_operations afs_symlink_iops;
58 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
61 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
65 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
66 cred_t *credp = crref();
70 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
71 ICL_TYPE_OFFSET, offp,
72 ICL_TYPE_INT32, count,
73 ICL_TYPE_INT32, 99999);
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, vcp,
109 ICL_TYPE_OFFSET, offp,
111 ICL_TYPE_INT32, code);
113 *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,
132 ICL_TYPE_INT32, count,
133 ICL_TYPE_INT32, code);
141 /* Now we have integrated VM for writes as well as reads. generic_file_write
142 * also takes care of re-positioning the pointer if file is open in append
143 * mode. Call fake open/close to ensure we do writes of core dumps.
145 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
150 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
151 struct vrequest treq;
152 cred_t *credp = crref();
157 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
158 ICL_TYPE_OFFSET, offp,
159 ICL_TYPE_INT32, count,
160 ICL_TYPE_INT32, (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) != AFS_CHUNKBASE(oldOffset)) {
210 ObtainWriteLock(&vcp->lock,402);
211 code = afs_DoPartialWrite(vcp, &treq);
212 vcp->states |= CDirty;
213 ReleaseWriteLock(&vcp->lock);
218 ObtainWriteLock(&vcp->lock,400);
219 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
221 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
222 vcp->m.Length = toffs;
224 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,
248 ICL_TYPE_INT32, count,
249 ICL_TYPE_INT32, code);
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.
260 static int afs_linux_readdir(struct file *fp,
261 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 &&
367 (ntohl(de->fid.vnode) & 1)) {
369 } else if ((tvc=afs_FindVCache(&afid,0,0))) {
372 } else if (((tvc->states) & (CStatd|CTruth))) {
373 /* CTruth will be set if the object has
378 else if (vtype == VREG)
380 /* Don't do this until we're sure it can't be a mtpt */
381 /* else if (vtype == VLNK)
383 /* what other types does AFS support? */
385 /* clean up from afs_FindVCache */
388 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
391 code = (*filldir)(dirbuf, de->name, len, offset, ino);
396 offset = dirpos + 1 + ((len+16)>>5);
398 /* If filldir didn't fill in the last one this is still pointing to that
401 fp->f_pos = (loff_t)offset;
403 ReleaseReadLock(&tdc->lock);
405 ReleaseReadLock(&avc->lock);
406 afs_PutFakeStat(&fakestat);
412 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
415 /* in afs_pioctl.c */
416 extern int afs_xioctl(struct inode *ip, struct file *fp,
417 unsigned int com, unsigned long arg);
420 /* We need to detect unmap's after close. To do that, we need our own
421 * vm_operations_struct's. And we need to set them up for both the
422 * private and shared mappings. The fun part is that these are all static
423 * so we'll have to initialize on the fly!
425 static struct vm_operations_struct afs_private_mmap_ops;
426 static int afs_private_mmap_ops_inited = 0;
427 static struct vm_operations_struct afs_shared_mmap_ops;
428 static int afs_shared_mmap_ops_inited = 0;
430 void afs_linux_vma_close(struct vm_area_struct *vmap)
438 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
443 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
444 ICL_TYPE_POINTER, vcp,
445 ICL_TYPE_INT32, vcp->mapcnt,
446 ICL_TYPE_INT32, vcp->opens,
447 ICL_TYPE_INT32, vcp->execsOrWriters);
448 ObtainWriteLock(&vcp->lock, 532);
451 ReleaseWriteLock(&vcp->lock);
454 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
455 /* only decrement the execsOrWriters flag if this is not a writable
457 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
458 vcp->execsOrWriters--;
460 vcp->states &= ~CMAPPED;
465 ReleaseWriteLock(&vcp->lock);
472 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
474 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
475 cred_t *credp = crref();
476 struct vrequest treq;
480 #if defined(AFS_LINUX24_ENV)
481 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
482 ICL_TYPE_POINTER, vmap->vm_start,
483 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
485 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
486 ICL_TYPE_POINTER, vmap->vm_start,
487 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
488 ICL_TYPE_INT32, vmap->vm_offset);
491 /* get a validated vcache entry */
492 code = afs_InitReq(&treq, credp);
494 code = afs_VerifyVCache(vcp, &treq);
500 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
503 code = generic_file_mmap(fp, vmap);
508 ObtainWriteLock(&vcp->lock,531);
509 /* Set out vma ops so we catch the close. The following test should be
510 * the same as used in generic_file_mmap.
512 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
513 if (!afs_shared_mmap_ops_inited) {
514 afs_shared_mmap_ops_inited = 1;
515 afs_shared_mmap_ops = *vmap->vm_ops;
516 afs_shared_mmap_ops.close = afs_linux_vma_close;
518 vmap->vm_ops = &afs_shared_mmap_ops;
521 if (!afs_private_mmap_ops_inited) {
522 afs_private_mmap_ops_inited = 1;
523 afs_private_mmap_ops = *vmap->vm_ops;
524 afs_private_mmap_ops.close = afs_linux_vma_close;
526 vmap->vm_ops = &afs_private_mmap_ops;
530 /* Add an open reference on the first mapping. */
531 if (vcp->mapcnt == 0) {
532 vcp->execsOrWriters++;
534 vcp->states |= CMAPPED;
536 ReleaseWriteLock(&vcp->lock);
545 int afs_linux_open(struct inode *ip, struct file *fp)
548 cred_t *credp = crref();
551 #ifdef AFS_LINUX24_ENV
554 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
555 #ifdef AFS_LINUX24_ENV
564 /* afs_Close is called from release, since release is used to handle all
565 * file closings. In addition afs_linux_flush is called from sys_close to
566 * handle flushing the data back to the server. The kicker is that we could
567 * ignore flush completely if only sys_close took it's return value from
568 * fput. See afs_linux_flush for notes on interactions between release and
571 static int afs_linux_release(struct inode *ip, struct file *fp)
574 cred_t *credp = crref();
575 struct vcache *vcp = ITOAFS(ip);
578 #ifdef AFS_LINUX24_ENV
582 vcp->flushcnt--; /* protected by AFS global lock. */
585 code = afs_close(vcp, fp->f_flags, credp);
587 #ifdef AFS_LINUX24_ENV
596 #if defined(AFS_LINUX24_ENV)
597 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
599 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
603 struct inode *ip = FILE_INODE(fp);
604 cred_t *credp = crref();
607 #ifdef AFS_LINUX24_ENV
610 code = afs_fsync(ITOAFS(ip), credp);
611 #ifdef AFS_LINUX24_ENV
621 /* No support for async i/o */
622 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
624 /* I don't think it will, at least not as can be detected here. */
625 int afs_linux_check_media_change(kdev_t dev);
627 /* Revalidate media and file system. */
628 int afs_linux_file_revalidate(kdev_t dev);
631 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
634 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
635 cred_t *credp = crref();
636 #ifdef AFS_LINUX24_ENV
637 struct flock64 flock;
642 /* Convert to a lock format afs_lockctl understands. */
643 memset((char*)&flock, 0, sizeof(flock));
644 flock.l_type = flp->fl_type;
645 flock.l_pid = flp->fl_pid;
647 flock.l_start = flp->fl_start;
648 flock.l_len = flp->fl_end - flp->fl_start;
650 /* Safe because there are no large files, yet */
651 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
652 if (cmd == F_GETLK64)
654 else if (cmd == F_SETLK64)
656 else if (cmd == F_SETLKW64)
658 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
661 code = afs_lockctl(vcp, &flock, cmd, credp);
664 /* Convert flock back to Linux's file_lock */
665 flp->fl_type = flock.l_type;
666 flp->fl_pid = flock.l_pid;
667 flp->fl_start = flock.l_start;
668 flp->fl_end = flock.l_start + flock.l_len;
676 * flush is called from sys_close. We could ignore it, but sys_close return
677 * code comes from flush, not release. We need to use release to keep
678 * the vcache open count correct. Note that flush is called before release
679 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
680 * races and also avoid calling afs_close twice when closing the file.
681 * If we merely checked for opens > 0 in afs_linux_release, then if an
682 * new open occurred when storing back the file, afs_linux_release would
683 * incorrectly close the file and decrement the opens count. Calling afs_close
684 * on the just flushed file is wasteful, since the background daemon will
685 * execute the code that finally decides there is nothing to do.
687 int afs_linux_flush(struct file *fp)
689 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
693 /* Only do this on the last close of the file pointer. */
694 #if defined(AFS_LINUX24_ENV)
695 if (atomic_read(&fp->f_count) > 1)
704 code = afs_close(vcp, fp->f_flags, credp);
705 vcp->flushcnt++; /* protected by AFS global lock. */
712 /* Not allowed to directly read a directory. */
713 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
720 #if defined(AFS_LINUX24_ENV)
721 struct file_operations afs_dir_fops = {
722 read: generic_read_dir,
723 readdir: afs_linux_readdir,
725 open: afs_linux_open,
726 release: afs_linux_release,
729 struct file_operations afs_dir_fops = {
730 NULL, /* afs_linux_lseek */
732 NULL, /* afs_linux_write */
734 NULL, /* afs_linux_select */
735 afs_xioctl, /* close enough to use the ported AFS one */
736 NULL, /* afs_linux_mmap */
738 NULL, /* afs_linux_flush */
741 NULL, /* afs_linux_fasync */
742 NULL, /* afs_linux_check_media_change */
743 NULL, /* afs_linux_file_revalidate */
748 #if defined(AFS_LINUX24_ENV)
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,
761 struct file_operations afs_file_fops = {
762 NULL, /* afs_linux_lseek */
765 NULL, /* afs_linux_readdir */
766 NULL, /* afs_linux_select */
767 afs_xioctl, /* close enough to use the ported AFS one */
773 NULL, /* afs_linux_fasync */
774 NULL, /* afs_linux_check_media_change */
775 NULL, /* afs_linux_file_revalidate */
781 /**********************************************************************
782 * AFS Linux dentry operations
783 **********************************************************************/
785 /* afs_linux_revalidate
786 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
788 static int afs_linux_revalidate(struct dentry *dp)
792 struct vrequest treq;
793 struct vcache *vcp = ITOAFS(dp->d_inode);
794 struct vcache *rootvp = NULL;
798 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid &&
799 (vcp->states & CMValid) && (vcp->states & CStatd)) {
800 ObtainSharedLock(&afs_xvcache, 680);
801 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
802 ReleaseSharedLock(&afs_xvcache);
805 #ifdef AFS_LINUX24_ENV
809 /* Make this a fast path (no crref), since it's called so often. */
810 if (vcp->states & CStatd) {
811 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
812 check_bad_parent(dp); /* check and correct mvid */
814 vcache2fakeinode(rootvp, vcp);
817 #ifdef AFS_LINUX24_ENV
820 if (rootvp) afs_PutVCache(rootvp);
826 code = afs_InitReq(&treq, credp);
828 code = afs_VerifyVCache(vcp, &treq);
830 #ifdef AFS_LINUX24_ENV
840 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
841 * In kernels 2.2.10 and above, we are passed an additional flags var which
842 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
843 * we are advised to follow the entry if it is a link or to make sure that
844 * it is a directory. But since the kernel itself checks these possibilities
845 * later on, we shouldn't have to do it until later. Perhaps in the future..
847 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
848 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
850 static int afs_linux_dentry_revalidate(struct dentry *dp)
854 cred_t *credp = crref();
855 struct vrequest treq;
856 struct vcache *lookupvcp = NULL;
857 int code, bad_dentry = 1;
858 struct sysname_info sysState;
859 struct vcache *vcp = ITOAFS(dp->d_inode);
860 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
865 sysState.allocked = 0;
867 /* If it's a negative dentry, then there's nothing to do. */
868 if (!vcp || !parentvcp)
871 /* If it is the AFS root, then there's no chance it needs
873 if (vcp == afs_globalVp) {
878 if ((code = afs_InitReq(&treq, credp)))
881 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
882 name = sysState.name;
884 /* First try looking up the DNLC */
885 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
886 /* Verify that the dentry does not point to an old inode */
887 if (vcp != lookupvcp)
889 /* Check and correct mvid */
890 if (*name != '/' && vcp->mvstat == 2)
891 check_bad_parent(dp);
897 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
898 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
900 /* Verify that the dentry does not point to an old inode */
901 if (vcp != lookupvcp)
909 afs_PutVCache(lookupvcp);
910 if (sysState.allocked)
911 osi_FreeLargeSpace(name);
917 shrink_dcache_parent(dp);
926 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
927 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
929 static int afs_linux_dentry_revalidate(struct dentry *dp)
934 struct vrequest treq;
935 struct inode *ip = AFSTOI(dp->d_inode);
937 unsigned long timeout = 3*HZ; /* 3 seconds */
940 printk("negative dentry: %s\n", dp->d_name.name);
942 if (!(flags & LOOKUP_CONTINUE)) {
943 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
949 if (time_after(jiffies, dp->d_time + timeout))
960 /* afs_dentry_iput */
961 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
963 if (ICL_SETACTIVE(afs_iclSetp)) {
965 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT,
966 ICL_TYPE_POINTER, ip,
967 ICL_TYPE_STRING, dp->d_parent->d_name.name,
968 ICL_TYPE_STRING, dp->d_name.name);
975 static int afs_dentry_delete(struct dentry *dp)
977 if (ICL_SETACTIVE(afs_iclSetp)) {
979 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
980 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
981 ICL_TYPE_STRING, dp->d_name.name);
985 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
986 return 1; /* bad inode? */
991 #if defined(AFS_LINUX24_ENV)
992 struct dentry_operations afs_dentry_operations = {
993 d_revalidate: afs_linux_dentry_revalidate,
994 d_iput: afs_dentry_iput,
995 d_delete: afs_dentry_delete,
997 struct dentry_operations *afs_dops = &afs_dentry_operations;
999 struct dentry_operations afs_dentry_operations = {
1000 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
1002 NULL, /* d_compare */
1003 afs_dentry_delete, /* d_delete(struct dentry *) */
1004 NULL, /* d_release(struct dentry *) */
1005 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
1007 struct dentry_operations *afs_dops = &afs_dentry_operations;
1010 /**********************************************************************
1011 * AFS Linux inode operations
1012 **********************************************************************/
1016 * Merely need to set enough of vattr to get us through the create. Note
1017 * that the higher level code (open_namei) will take care of any tuncation
1018 * explicitly. Exclusive open is also taken care of in open_namei.
1020 * name is in kernel space at this point.
1022 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1025 cred_t *credp = crref();
1028 const char *name = dp->d_name.name;
1032 vattr.va_mode = mode;
1035 code = afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
1036 (struct vcache**)&ip, credp);
1039 vattr2inode(ip, &vattr);
1040 /* Reset ops if symlink or directory. */
1041 #if defined(AFS_LINUX24_ENV)
1042 if (S_ISREG(ip->i_mode)) {
1043 ip->i_op = &afs_file_iops;
1044 ip->i_fop = &afs_file_fops;
1045 ip->i_data.a_ops = &afs_file_aops;
1046 } else if (S_ISDIR(ip->i_mode)) {
1047 ip->i_op = &afs_dir_iops;
1048 ip->i_fop = &afs_dir_fops;
1049 } else if (S_ISLNK(ip->i_mode)) {
1050 ip->i_op = &afs_symlink_iops;
1051 ip->i_data.a_ops = &afs_symlink_aops;
1052 ip->i_mapping = &ip->i_data;
1054 printk("afs_linux_create: FIXME\n");
1056 if (S_ISDIR(ip->i_mode))
1057 ip->i_op = &afs_dir_iops;
1058 else if (S_ISLNK(ip->i_mode))
1059 ip->i_op = &afs_symlink_iops;
1062 dp->d_op = afs_dops;
1063 dp->d_time = jiffies;
1064 d_instantiate(dp, ip);
1072 /* afs_linux_lookup */
1073 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1074 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
1076 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
1080 cred_t *credp = crref();
1081 struct vcache *vcp=NULL;
1082 const char *comp = dp->d_name.name;
1084 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1087 struct inode *ip = AFSTOI(vcp);
1088 /* Reset ops if symlink or directory. */
1089 #if defined(AFS_LINUX24_ENV)
1090 if (S_ISREG(ip->i_mode)) {
1091 ip->i_op = &afs_file_iops;
1092 ip->i_fop = &afs_file_fops;
1093 ip->i_data.a_ops = &afs_file_aops;
1094 } else if (S_ISDIR(ip->i_mode)) {
1095 ip->i_op = &afs_dir_iops;
1096 ip->i_fop = &afs_dir_fops;
1097 } else if (S_ISLNK(ip->i_mode)) {
1098 ip->i_op = &afs_symlink_iops;
1099 ip->i_data.a_ops = &afs_symlink_aops;
1100 ip->i_mapping = &ip->i_data;
1102 printk("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n", ip->i_mode, dp->d_name.name, code);
1104 if (S_ISDIR(ip->i_mode))
1105 ip->i_op = &afs_dir_iops;
1106 else if (S_ISLNK(ip->i_mode))
1107 ip->i_op = &afs_symlink_iops;
1110 dp->d_time = jiffies;
1111 dp->d_op = afs_dops;
1112 d_add(dp, AFSTOI(vcp));
1117 /* It's ok for the file to not be found. That's noted by the caller by
1118 * seeing that the dp->d_inode field is NULL.
1120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1124 return ERR_PTR(-code);
1132 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1133 struct dentry *newdp)
1136 cred_t *credp = crref();
1137 const char *name = newdp->d_name.name;
1138 struct inode *oldip = olddp->d_inode;
1140 /* If afs_link returned the vnode, we could instantiate the
1141 * dentry. Since it's not, we drop this one and do a new lookup.
1146 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1153 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1156 cred_t *credp = crref();
1157 const char *name = dp->d_name.name;
1160 code = afs_remove(ITOAFS(dip), name, credp);
1169 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1173 cred_t *credp = crref();
1175 const char *name = dp->d_name.name;
1177 /* If afs_symlink returned the vnode, we could instantiate the
1178 * dentry. Since it's not, we drop this one and do a new lookup.
1184 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1190 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1193 cred_t *credp = crref();
1194 struct vcache *tvcp = NULL;
1196 const char *name = dp->d_name.name;
1200 vattr.va_mask = ATTR_MODE;
1201 vattr.va_mode = mode;
1202 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1205 tvcp->v.v_op = &afs_dir_iops;
1206 #if defined(AFS_LINUX24_ENV)
1207 tvcp->v.v_fop = &afs_dir_fops;
1209 dp->d_op = afs_dops;
1210 dp->d_time = jiffies;
1211 d_instantiate(dp, AFSTOI(tvcp));
1219 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1222 cred_t *credp = crref();
1223 const char *name = dp->d_name.name;
1226 code = afs_rmdir(ITOAFS(dip), name, credp);
1228 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1229 * that failed because a directory is not empty. So, we map
1230 * EEXIST to ENOTEMPTY on linux.
1232 if (code == EEXIST) {
1247 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1248 struct inode *newip, struct dentry *newdp)
1251 cred_t *credp = crref();
1252 const char *oldname = olddp->d_name.name;
1253 const char *newname = newdp->d_name.name;
1255 /* Remove old and new entries from name hash. New one will change below.
1256 * While it's optimal to catch failures and re-insert newdp into hash,
1257 * it's also error prone and in that case we're already dealing with error
1258 * cases. Let another lookup put things right, if need be.
1260 if (!list_empty(&olddp->d_hash)) {
1263 if (!list_empty(&newdp->d_hash)) {
1267 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip),
1272 /* update time so it doesn't expire immediately */
1273 newdp->d_time = jiffies;
1274 d_move(olddp, newdp);
1282 /* afs_linux_ireadlink
1283 * Internal readlink which can return link contents to user or kernel space.
1284 * Note that the buffer is NOT supposed to be null-terminated.
1286 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1290 cred_t *credp = crref();
1294 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1295 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1299 return maxlen - tuio.uio_resid;
1304 #if !defined(AFS_LINUX24_ENV)
1305 /* afs_linux_readlink
1306 * Fill target (which is in user space) with contents of symlink.
1308 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1311 struct inode *ip = dp->d_inode;
1314 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1320 /* afs_linux_follow_link
1321 * a file system dependent link following routine.
1323 struct dentry * afs_linux_follow_link(struct dentry *dp,
1324 struct dentry *basep,
1325 unsigned int follow)
1333 name = osi_Alloc(PATH_MAX+1);
1337 return ERR_PTR(-EIO);
1340 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1345 res = ERR_PTR(code);
1349 res = lookup_dentry(name, basep, follow);
1353 osi_Free(name, PATH_MAX+1);
1359 /* afs_linux_readpage
1360 * all reads come through here. A strategy-like read call.
1362 int afs_linux_readpage(struct file *fp, struct page *pp)
1365 cred_t *credp = crref();
1366 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1368 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1370 ulong address = afs_linux_page_address(pp);
1371 afs_offs_t offset = pageoff(pp);
1375 struct inode *ip = FILE_INODE(fp);
1376 int cnt = atomic_read(&pp->count);
1377 struct vcache *avc = ITOAFS(ip);
1380 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1381 ICL_TYPE_POINTER, ip,
1382 ICL_TYPE_POINTER, pp,
1383 ICL_TYPE_INT32, cnt,
1384 ICL_TYPE_INT32, 99999); /* not a possible code value */
1385 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1391 atomic_add(1, &pp->count);
1392 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1393 clear_bit(PG_error, &pp->flags);
1396 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1397 UIO_READ, AFS_UIOSYS);
1398 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1399 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1404 if (tuio.uio_resid) /* zero remainder of page */
1405 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1407 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1408 flush_dcache_page(pp);
1409 SetPageUptodate(pp);
1411 set_bit(PG_uptodate, &pp->flags);
1415 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1419 clear_bit(PG_locked, &pp->flags);
1424 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1426 struct vrequest treq;
1428 code = afs_InitReq(&treq, credp);
1429 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1430 tdc = afs_FindDCache(avc, offset);
1432 if (!(tdc->mflags & DFNextStarted))
1433 afs_PrefetchChunk(avc, tdc, credp, &treq);
1436 ReleaseWriteLock(&avc->lock);
1441 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1442 ICL_TYPE_POINTER, ip,
1443 ICL_TYPE_POINTER, pp,
1444 ICL_TYPE_INT32, cnt,
1445 ICL_TYPE_INT32, code);
1450 #if defined(AFS_LINUX24_ENV)
1451 int afs_linux_writepage(struct page *pp)
1453 struct address_space *mapping = pp->mapping;
1454 struct inode *inode;
1455 unsigned long end_index;
1456 unsigned offset = PAGE_CACHE_SIZE;
1459 inode = (struct inode *) mapping->host;
1460 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1463 if (pp->index < end_index)
1465 /* things got complicated... */
1466 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1467 /* OK, are we completely out? */
1468 if (pp->index >= end_index+1 || !offset)
1472 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1474 SetPageUptodate(pp);
1476 if (status == offset)
1484 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1485 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1487 /* afs_linux_truncate
1488 * Handles discarding disk blocks if this were a device. ext2 indicates we
1489 * may need to zero partial last pages of memory mapped files.
1491 void afs_linux_truncate(struct inode *ip)
1496 /* afs_linux_permission
1497 * Check access rights - returns error if can't check or permission denied.
1499 int afs_linux_permission(struct inode *ip, int mode)
1502 cred_t *credp = crref();
1506 if (mode & MAY_EXEC) tmp |= VEXEC;
1507 if (mode & MAY_READ) tmp |= VREAD;
1508 if (mode & MAY_WRITE) tmp |= VWRITE;
1509 code = afs_access(ITOAFS(ip), tmp, credp);
1518 /* msdos sector mapping hack for memory mapping. */
1519 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1522 #if defined(AFS_LINUX24_ENV)
1523 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1524 unsigned long offset,
1527 struct vcache *vcp = ITOAFS(ip);
1536 buffer = kmap(pp) + offset;
1537 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1540 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1541 ICL_TYPE_POINTER, pp,
1542 ICL_TYPE_INT32, atomic_read(&pp->count),
1543 ICL_TYPE_INT32, 99999);
1545 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1547 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1551 if (!code && afs_stats_cmperf.cacheCurrDirtyChunks >
1552 afs_stats_cmperf.cacheMaxDirtyChunks) {
1553 struct vrequest treq;
1555 ObtainWriteLock(&vcp->lock, 533);
1556 if (!afs_InitReq(&treq, credp))
1557 code = afs_DoPartialWrite(vcp, &treq);
1558 ReleaseWriteLock(&vcp->lock);
1560 code = code ? -code : count - tuio.uio_resid;
1562 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1563 ICL_TYPE_POINTER, pp,
1564 ICL_TYPE_INT32, atomic_read(&pp->count),
1565 ICL_TYPE_INT32, code);
1574 afs_linux_updatepage(struct file *file, struct page *page,
1575 unsigned long offset, unsigned int count)
1577 struct dentry *dentry = file->f_dentry;
1579 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1582 /* afs_linux_updatepage
1583 * What one would have thought was writepage - write dirty page to file.
1584 * Called from generic_file_write. buffer is still in user space. pagep
1585 * has been filled in with old data if we're updating less than a page.
1587 int afs_linux_updatepage(struct file *fp, struct page *pp,
1588 unsigned long offset,
1589 unsigned int count, int sync)
1591 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1592 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1598 set_bit(PG_locked, &pp->flags);
1602 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1603 ICL_TYPE_POINTER, pp,
1604 ICL_TYPE_INT32, atomic_read(&pp->count),
1605 ICL_TYPE_INT32, 99999);
1606 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1607 count, UIO_WRITE, AFS_UIOSYS);
1609 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1613 code = code ? -code : count - tuio.uio_resid;
1614 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1615 ICL_TYPE_POINTER, pp,
1616 ICL_TYPE_INT32, atomic_read(&pp->count),
1617 ICL_TYPE_INT32, code);
1622 clear_bit(PG_locked, &pp->flags);
1627 #if defined(AFS_LINUX24_ENV)
1628 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1634 code = afs_linux_updatepage(file, page, offset, to-offset);
1642 static int afs_linux_prepare_write(struct file *file, struct page *page,
1643 unsigned from, unsigned to)
1649 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1652 #if defined(AFS_LINUX24_ENV)
1653 struct inode_operations afs_file_iops = {
1654 revalidate: afs_linux_revalidate,
1655 setattr: afs_notify_change,
1656 permission: afs_linux_permission,
1658 struct address_space_operations afs_file_aops = {
1659 readpage: afs_linux_readpage,
1660 writepage: afs_linux_writepage,
1661 commit_write: afs_linux_commit_write,
1662 prepare_write: afs_linux_prepare_write,
1665 struct inode_operations *afs_ops = &afs_file_iops;
1667 struct inode_operations afs_iops = {
1668 &afs_file_fops, /* file operations */
1669 NULL, /* afs_linux_create */
1670 NULL, /* afs_linux_lookup */
1671 NULL, /* afs_linux_link */
1672 NULL, /* afs_linux_unlink */
1673 NULL, /* afs_linux_symlink */
1674 NULL, /* afs_linux_mkdir */
1675 NULL, /* afs_linux_rmdir */
1676 NULL, /* afs_linux_mknod */
1677 NULL, /* afs_linux_rename */
1678 NULL, /* afs_linux_readlink */
1679 NULL, /* afs_linux_follow_link */
1681 NULL, /* afs_linux_writepage */
1682 NULL, /* afs_linux_bmap */
1683 NULL, /* afs_linux_truncate */
1684 afs_linux_permission,
1685 NULL, /* afs_linux_smap */
1686 afs_linux_updatepage,
1687 afs_linux_revalidate,
1690 struct inode_operations *afs_ops = &afs_iops;
1693 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1694 * by what sort of operation is allowed.....
1696 #if defined(AFS_LINUX24_ENV)
1697 struct inode_operations afs_dir_iops = {
1698 create: afs_linux_create,
1699 lookup: afs_linux_lookup,
1700 link: afs_linux_link,
1701 unlink: afs_linux_unlink,
1702 symlink: afs_linux_symlink,
1703 mkdir: afs_linux_mkdir,
1704 rmdir: afs_linux_rmdir,
1705 rename: afs_linux_rename,
1706 revalidate: afs_linux_revalidate,
1707 setattr: afs_notify_change,
1708 permission: afs_linux_permission,
1711 struct inode_operations afs_dir_iops = {
1712 &afs_dir_fops, /* file operations for directories */
1720 NULL, /* afs_linux_mknod */
1722 NULL, /* afs_linux_readlink */
1723 NULL, /* afs_linux_follow_link */
1724 NULL, /* afs_linux_readpage */
1725 NULL, /* afs_linux_writepage */
1726 NULL, /* afs_linux_bmap */
1727 NULL, /* afs_linux_truncate */
1728 afs_linux_permission,
1729 NULL, /* afs_linux_smap */
1730 NULL, /* afs_linux_updatepage */
1731 afs_linux_revalidate,
1735 /* We really need a separate symlink set of ops, since do_follow_link()
1736 * determines if it _is_ a link by checking if the follow_link op is set.
1738 #if defined(AFS_LINUX24_ENV)
1739 static int afs_symlink_filler(struct file *file, struct page *page)
1741 struct inode *ip = (struct inode *) page->mapping->host;
1742 char *p = (char *)kmap(page);
1747 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1751 p[code] = '\0'; /* null terminate? */
1755 SetPageUptodate(page);
1770 struct address_space_operations afs_symlink_aops = {
1771 readpage: afs_symlink_filler
1774 struct inode_operations afs_symlink_iops = {
1775 readlink: page_readlink,
1776 follow_link: page_follow_link,
1777 setattr: afs_notify_change,
1780 struct inode_operations afs_symlink_iops = {
1781 NULL, /* file operations */
1789 NULL, /* afs_linux_mknod */
1792 afs_linux_follow_link,
1793 NULL, /* readpage */
1794 NULL, /* afs_linux_writepage */
1795 NULL, /* afs_linux_bmap */
1796 NULL, /* afs_linux_truncate */
1797 afs_linux_permission, /* tho the code appears to indicate not used? */
1798 NULL, /* afs_linux_smap */
1799 NULL, /* updatepage */
1800 afs_linux_revalidate, /* tho the code appears to indicate not used? */