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 "../afs/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;
46 extern struct dentry_operations *afs_dops;
47 #if defined(AFS_LINUX24_ENV)
48 extern struct inode_operations afs_file_iops;
49 extern struct address_space_operations afs_file_aops;
50 struct address_space_operations afs_symlink_aops;
52 extern struct inode_operations afs_dir_iops;
53 extern struct inode_operations afs_symlink_iops;
57 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
60 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
64 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
65 cred_t *credp = crref();
69 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
70 ICL_TYPE_OFFSET, offp,
71 ICL_TYPE_INT32, count,
72 ICL_TYPE_INT32, 99999);
74 /* get a validated vcache entry */
75 code = afs_InitReq(&treq, credp);
77 code = afs_VerifyVCache(vcp, &treq);
82 #ifdef AFS_64BIT_CLIENT
83 if (*offp + count > afs_vmMappingEnd) {
86 afs_size_t oldOffset = *offp;
89 if (*offp < afs_vmMappingEnd) {
90 /* special case of a buffer crossing the VM mapping end */
91 afs_int32 tcount = afs_vmMappingEnd - *offp;
93 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
95 code = generic_file_read(fp, buf, tcount, offp);
102 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
103 UIO_READ, AFS_UIOSYS);
104 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
105 xfered += count - tuio.uio_resid;
108 *offp += count - tuio.uio_resid;
115 #endif /* AFS_64BIT_CLIENT */
116 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
118 code = generic_file_read(fp, buf, count, offp);
120 #ifdef AFS_64BIT_CLIENT
122 #endif /* AFS_64BIT_CLIENT */
125 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
126 ICL_TYPE_OFFSET, offp,
127 ICL_TYPE_INT32, count,
128 ICL_TYPE_INT32, code);
136 /* Now we have integrated VM for writes as well as reads. generic_file_write
137 * also takes care of re-positioning the pointer if file is open in append
138 * mode. Call fake open/close to ensure we do writes of core dumps.
140 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
145 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
146 struct vrequest treq;
147 cred_t *credp = crref();
151 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
152 ICL_TYPE_OFFSET, offp,
153 ICL_TYPE_INT32, count,
154 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
157 /* get a validated vcache entry */
158 code = (ssize_t)afs_InitReq(&treq, credp);
160 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
162 ObtainWriteLock(&vcp->lock, 529);
164 ReleaseWriteLock(&vcp->lock);
168 #ifdef AFS_64BIT_CLIENT
169 if (*offp + count > afs_vmMappingEnd) {
172 afs_size_t oldOffset = *offp;
173 afs_int32 xfered = 0;
175 if (*offp < afs_vmMappingEnd) {
176 /* special case of a buffer crossing the VM mapping end */
177 afs_int32 tcount = afs_vmMappingEnd - *offp;
180 code = generic_file_write(fp, buf, tcount, offp);
182 if (code != tcount) {
187 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
188 UIO_WRITE, AFS_UIOSYS);
189 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
190 xfered += count - tuio.uio_resid;
193 *offp += count - tuio.uio_resid;
195 /* Purge dirty chunks of file if there are too many dirty chunks.
196 * Inside the write loop, we only do this at a chunk boundary.
197 * Clean up partial chunk if necessary at end of loop.
199 if (AFS_CHUNKBASE(tuio.afsio_offset) != AFS_CHUNKBASE(oldOffset)) {
200 ObtainWriteLock(&vcp->lock,402);
201 code = afs_DoPartialWrite(vcp, &treq);
202 vcp->states |= CDirty;
203 ReleaseWriteLock(&vcp->lock);
207 ObtainWriteLock(&vcp->lock,400);
208 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
210 if (*offp > vcp->m.Length) {
211 vcp->m.Length = *offp;
213 ReleaseWriteLock(&vcp->lock);
217 #endif /* AFS_64BIT_CLIENT */
219 code = generic_file_write(fp, buf, count, offp);
221 #ifdef AFS_64BIT_CLIENT
223 #endif /* AFS_64BIT_CLIENT */
226 ObtainWriteLock(&vcp->lock, 530);
227 vcp->m.Date = osi_Time(); /* set modification time */
228 afs_FakeClose(vcp, credp);
230 code2 = afs_DoPartialWrite(vcp, &treq);
231 if (code2 && code >=0)
232 code = (ssize_t) -code2;
233 ReleaseWriteLock(&vcp->lock);
235 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
236 ICL_TYPE_OFFSET, offp,
237 ICL_TYPE_INT32, count,
238 ICL_TYPE_INT32, code);
245 /* This is a complete rewrite of afs_readdir, since we can make use of
246 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
247 * handling and use of bulkstats will need to be reflected here as well.
249 static int afs_linux_readdir(struct file *fp,
250 void *dirbuf, filldir_t filldir)
252 extern struct DirEntry * afs_dir_GetBlob();
253 struct vcache *avc = ITOAFS(FILE_INODE(fp));
254 struct vrequest treq;
255 register struct dcache *tdc;
262 afs_size_t origOffset, tlen;
263 cred_t *credp = crref();
264 struct afs_fakestat_state fakestat;
267 AFS_STATCNT(afs_readdir);
269 code = afs_InitReq(&treq, credp);
276 afs_InitFakeStat(&fakestat);
277 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
279 afs_PutFakeStat(&fakestat);
284 /* update the cache entry */
286 code = afs_VerifyVCache(avc, &treq);
288 afs_PutFakeStat(&fakestat);
293 /* get a reference to the entire directory */
294 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
297 afs_PutFakeStat(&fakestat);
301 ObtainReadLock(&avc->lock);
302 ObtainReadLock(&tdc->lock);
304 * Make sure that the data in the cache is current. There are two
305 * cases we need to worry about:
306 * 1. The cache data is being fetched by another process.
307 * 2. The cache data is no longer valid
309 while ((avc->states & CStatd)
310 && (tdc->dflags & DFFetching)
311 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
312 ReleaseReadLock(&tdc->lock);
313 ReleaseReadLock(&avc->lock);
314 afs_osi_Sleep(&tdc->validPos);
315 ObtainReadLock(&avc->lock);
316 ObtainReadLock(&tdc->lock);
318 if (!(avc->states & CStatd)
319 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
320 ReleaseReadLock(&tdc->lock);
321 ReleaseReadLock(&avc->lock);
326 /* Fill in until we get an error or we're done. This implementation
327 * takes an offset in units of blobs, rather than bytes.
330 offset = (int)fp->f_pos;
332 dirpos = BlobScan(&tdc->f.inode, offset);
336 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
340 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
341 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
342 len = strlen(de->name);
344 /* filldir returns -EINVAL when the buffer is full. */
345 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
347 unsigned int type=DT_UNKNOWN;
348 struct VenusFid afid;
351 afid.Cell=avc->fid.Cell;
352 afid.Fid.Volume=avc->fid.Fid.Volume;
353 afid.Fid.Vnode=ntohl(de->fid.vnode);
354 afid.Fid.Unique=ntohl(de->fid.vunique);
355 if ((avc->states & CForeign) == 0 &&
356 (ntohl(de->fid.vnode) & 1)) {
358 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
361 } else if (((tvc->states) & (CStatd|CTruth))) {
362 /* CTruth will be set if the object has
367 else if (vtype == VREG)
369 /* Don't do this until we're sure it can't be a mtpt */
370 /* else if (vtype == VLNK)
372 /* what other types does AFS support? */
374 /* clean up from afs_FindVCache */
375 afs_PutVCache(tvc, WRITE_LOCK);
377 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
380 code = (*filldir)(dirbuf, de->name, len, offset, ino);
385 offset = dirpos + 1 + ((len+16)>>5);
387 /* If filldir didn't fill in the last one this is still pointing to that
390 fp->f_pos = (loff_t)offset;
392 ReleaseReadLock(&tdc->lock);
394 ReleaseReadLock(&avc->lock);
395 afs_PutFakeStat(&fakestat);
401 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
404 /* in afs_pioctl.c */
405 extern int afs_xioctl(struct inode *ip, struct file *fp,
406 unsigned int com, unsigned long arg);
409 /* We need to detect unmap's after close. To do that, we need our own
410 * vm_operations_struct's. And we need to set them up for both the
411 * private and shared mappings. The fun part is that these are all static
412 * so we'll have to initialize on the fly!
414 static struct vm_operations_struct afs_private_mmap_ops;
415 static int afs_private_mmap_ops_inited = 0;
416 static struct vm_operations_struct afs_shared_mmap_ops;
417 static int afs_shared_mmap_ops_inited = 0;
419 void afs_linux_vma_close(struct vm_area_struct *vmap)
427 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
432 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
433 ICL_TYPE_POINTER, vcp,
434 ICL_TYPE_INT32, vcp->mapcnt,
435 ICL_TYPE_INT32, vcp->opens,
436 ICL_TYPE_INT32, vcp->execsOrWriters);
437 ObtainWriteLock(&vcp->lock, 532);
440 ReleaseWriteLock(&vcp->lock);
443 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
444 /* only decrement the execsOrWriters flag if this is not a writable
446 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
447 vcp->execsOrWriters--;
449 vcp->states &= ~CMAPPED;
454 ReleaseWriteLock(&vcp->lock);
461 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
463 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
464 cred_t *credp = crref();
465 struct vrequest treq;
469 #if defined(AFS_LINUX24_ENV)
470 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
471 ICL_TYPE_POINTER, vmap->vm_start,
472 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
474 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
475 ICL_TYPE_POINTER, vmap->vm_start,
476 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
477 ICL_TYPE_INT32, vmap->vm_offset);
480 /* get a validated vcache entry */
481 code = afs_InitReq(&treq, credp);
483 code = afs_VerifyVCache(vcp, &treq);
489 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
492 code = generic_file_mmap(fp, vmap);
497 ObtainWriteLock(&vcp->lock,531);
498 /* Set out vma ops so we catch the close. The following test should be
499 * the same as used in generic_file_mmap.
501 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
502 if (!afs_shared_mmap_ops_inited) {
503 afs_shared_mmap_ops_inited = 1;
504 afs_shared_mmap_ops = *vmap->vm_ops;
505 afs_shared_mmap_ops.close = afs_linux_vma_close;
507 vmap->vm_ops = &afs_shared_mmap_ops;
510 if (!afs_private_mmap_ops_inited) {
511 afs_private_mmap_ops_inited = 1;
512 afs_private_mmap_ops = *vmap->vm_ops;
513 afs_private_mmap_ops.close = afs_linux_vma_close;
515 vmap->vm_ops = &afs_private_mmap_ops;
519 /* Add an open reference on the first mapping. */
520 if (vcp->mapcnt == 0) {
521 vcp->execsOrWriters++;
523 vcp->states |= CMAPPED;
525 ReleaseWriteLock(&vcp->lock);
534 int afs_linux_open(struct inode *ip, struct file *fp)
537 cred_t *credp = crref();
540 #ifdef AFS_LINUX24_ENV
543 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
544 #ifdef AFS_LINUX24_ENV
553 /* afs_Close is called from release, since release is used to handle all
554 * file closings. In addition afs_linux_flush is called from sys_close to
555 * handle flushing the data back to the server. The kicker is that we could
556 * ignore flush completely if only sys_close took it's return value from
557 * fput. See afs_linux_flush for notes on interactions between release and
560 static int afs_linux_release(struct inode *ip, struct file *fp)
563 cred_t *credp = crref();
564 struct vcache *vcp = ITOAFS(ip);
567 #ifdef AFS_LINUX24_ENV
571 vcp->flushcnt--; /* protected by AFS global lock. */
574 code = afs_close(vcp, fp->f_flags, credp);
576 #ifdef AFS_LINUX24_ENV
585 #if defined(AFS_LINUX24_ENV)
586 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
588 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
592 struct inode *ip = FILE_INODE(fp);
593 cred_t *credp = crref();
596 #ifdef AFS_LINUX24_ENV
599 code = afs_fsync(ITOAFS(ip), credp);
600 #ifdef AFS_LINUX24_ENV
610 /* No support for async i/o */
611 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
613 /* I don't think it will, at least not as can be detected here. */
614 int afs_linux_check_media_change(kdev_t dev);
616 /* Revalidate media and file system. */
617 int afs_linux_file_revalidate(kdev_t dev);
620 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
623 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
624 cred_t *credp = crref();
625 #ifdef AFS_LINUX24_ENV
626 struct flock64 flock;
631 /* Convert to a lock format afs_lockctl understands. */
632 memset((char*)&flock, 0, sizeof(flock));
633 flock.l_type = flp->fl_type;
634 flock.l_pid = flp->fl_pid;
636 flock.l_start = flp->fl_start;
637 flock.l_len = flp->fl_end - flp->fl_start;
640 code = afs_lockctl(vcp, &flock, cmd, credp);
648 * flush is called from sys_close. We could ignore it, but sys_close return
649 * code comes from flush, not release. We need to use release to keep
650 * the vcache open count correct. Note that flush is called before release
651 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
652 * races and also avoid calling afs_close twice when closing the file.
653 * If we merely checked for opens > 0 in afs_linux_release, then if an
654 * new open occurred when storing back the file, afs_linux_release would
655 * incorrectly close the file and decrement the opens count. Calling afs_close
656 * on the just flushed file is wasteful, since the background daemon will
657 * execute the code that finally decides there is nothing to do.
659 int afs_linux_flush(struct file *fp)
661 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
665 /* Only do this on the last close of the file pointer. */
666 #if defined(AFS_LINUX24_ENV)
667 if (atomic_read(&fp->f_count) > 1)
676 code = afs_close(vcp, fp->f_flags, credp);
677 vcp->flushcnt++; /* protected by AFS global lock. */
684 /* Not allowed to directly read a directory. */
685 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
692 #if defined(AFS_LINUX24_ENV)
693 struct file_operations afs_dir_fops = {
694 read: generic_read_dir,
695 readdir: afs_linux_readdir,
697 open: afs_linux_open,
698 release: afs_linux_release,
701 struct file_operations afs_dir_fops = {
702 NULL, /* afs_linux_lseek */
704 NULL, /* afs_linux_write */
706 NULL, /* afs_linux_select */
707 afs_xioctl, /* close enough to use the ported AFS one */
708 NULL, /* afs_linux_mmap */
710 NULL, /* afs_linux_flush */
713 NULL, /* afs_linux_fasync */
714 NULL, /* afs_linux_check_media_change */
715 NULL, /* afs_linux_file_revalidate */
720 #if defined(AFS_LINUX24_ENV)
721 struct file_operations afs_file_fops = {
722 read: afs_linux_read,
723 write: afs_linux_write,
725 mmap: afs_linux_mmap,
726 open: afs_linux_open,
727 flush: afs_linux_flush,
728 release: afs_linux_release,
729 fsync: afs_linux_fsync,
730 lock: afs_linux_lock,
733 struct file_operations afs_file_fops = {
734 NULL, /* afs_linux_lseek */
737 NULL, /* afs_linux_readdir */
738 NULL, /* afs_linux_select */
739 afs_xioctl, /* close enough to use the ported AFS one */
745 NULL, /* afs_linux_fasync */
746 NULL, /* afs_linux_check_media_change */
747 NULL, /* afs_linux_file_revalidate */
753 /**********************************************************************
754 * AFS Linux dentry operations
755 **********************************************************************/
757 /* afs_linux_revalidate
758 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
760 static int afs_linux_revalidate(struct dentry *dp)
764 struct vrequest treq;
765 struct vcache *vcp = ITOAFS(dp->d_inode);
768 #ifdef AFS_LINUX24_ENV
772 /* Make this a fast path (no crref), since it's called so often. */
773 if (vcp->states & CStatd) {
774 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
775 check_bad_parent(dp); /* check and correct mvid */
777 #ifdef AFS_LINUX24_ENV
785 code = afs_InitReq(&treq, credp);
787 code = afs_VerifyVCache(vcp, &treq);
789 #ifdef AFS_LINUX24_ENV
799 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
800 * In kernels 2.2.10 and above, we are passed an additional flags var which
801 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
802 * we are advised to follow the entry if it is a link or to make sure that
803 * it is a directory. But since the kernel itself checks these possibilities
804 * later on, we shouldn't have to do it until later. Perhaps in the future..
806 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
807 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
809 static int afs_linux_dentry_revalidate(struct dentry *dp)
813 cred_t *credp = crref();
814 struct vrequest treq;
815 struct vcache *lookupvcp = NULL;
816 int code, bad_dentry = 1;
817 struct sysname_info sysState;
818 struct vcache *vcp = ITOAFS(dp->d_inode);
819 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
823 sysState.allocked = 0;
825 /* If it's a negative dentry, then there's nothing to do. */
826 if (!vcp || !parentvcp)
829 /* If it is the AFS root, then there's no chance it needs
831 if (vcp == afs_globalVp) {
836 if (code = afs_InitReq(&treq, credp))
839 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
840 name = sysState.name;
842 /* First try looking up the DNLC */
843 if (lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK)) {
844 /* Verify that the dentry does not point to an old inode */
845 if (vcp != lookupvcp)
847 /* Check and correct mvid */
848 if (*name != '/' && vcp->mvstat == 2)
849 check_bad_parent(dp);
855 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
856 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
858 /* Verify that the dentry does not point to an old inode */
859 if (vcp != lookupvcp)
867 afs_PutVCache(lookupvcp, WRITE_LOCK);
868 if (sysState.allocked)
869 osi_FreeLargeSpace(name);
875 shrink_dcache_parent(dp);
883 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
884 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
886 static int afs_linux_dentry_revalidate(struct dentry *dp)
891 struct vrequest treq;
892 struct inode *ip = AFSTOI(dp->d_inode);
894 unsigned long timeout = 3*HZ; /* 3 seconds */
897 printk("negative dentry: %s\n", dp->d_name.name);
899 if (!(flags & LOOKUP_CONTINUE)) {
900 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
906 if (time_after(jiffies, dp->d_time + timeout))
917 /* afs_dentry_iput */
918 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
923 static int afs_dentry_delete(struct dentry *dp)
925 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
926 return 1; /* bad inode? */
928 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
929 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
930 ICL_TYPE_STRING, dp->d_name.name);
934 #if defined(AFS_LINUX24_ENV)
935 struct dentry_operations afs_dentry_operations = {
936 d_revalidate: afs_linux_dentry_revalidate,
937 d_iput: afs_dentry_iput,
938 d_delete: afs_dentry_delete,
940 struct dentry_operations *afs_dops = &afs_dentry_operations;
942 struct dentry_operations afs_dentry_operations = {
943 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
945 NULL, /* d_compare */
946 NULL, /* d_delete(struct dentry *) */
947 NULL, /* d_release(struct dentry *) */
948 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
950 struct dentry_operations *afs_dops = &afs_dentry_operations;
953 /**********************************************************************
954 * AFS Linux inode operations
955 **********************************************************************/
959 * Merely need to set enough of vattr to get us through the create. Note
960 * that the higher level code (open_namei) will take care of any tuncation
961 * explicitly. Exclusive open is also taken care of in open_namei.
963 * name is in kernel space at this point.
965 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
968 cred_t *credp = crref();
971 const char *name = dp->d_name.name;
975 vattr.va_mode = mode;
978 code = afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
979 (struct vcache**)&ip, credp);
982 vattr2inode(ip, &vattr);
983 /* Reset ops if symlink or directory. */
984 #if defined(AFS_LINUX24_ENV)
985 if (S_ISREG(ip->i_mode)) {
986 ip->i_op = &afs_file_iops;
987 ip->i_fop = &afs_file_fops;
988 ip->i_data.a_ops = &afs_file_aops;
989 } else if (S_ISDIR(ip->i_mode)) {
990 ip->i_op = &afs_dir_iops;
991 ip->i_fop = &afs_dir_fops;
992 } else if (S_ISLNK(ip->i_mode)) {
993 ip->i_op = &afs_symlink_iops;
994 ip->i_data.a_ops = &afs_symlink_aops;
995 ip->i_mapping = &ip->i_data;
997 printk("afs_linux_create: FIXME\n");
999 if (S_ISDIR(ip->i_mode))
1000 ip->i_op = &afs_dir_iops;
1001 else if (S_ISLNK(ip->i_mode))
1002 ip->i_op = &afs_symlink_iops;
1005 dp->d_op = afs_dops;
1006 dp->d_time = jiffies;
1007 d_instantiate(dp, ip);
1015 /* afs_linux_lookup */
1016 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1017 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
1019 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
1023 cred_t *credp = crref();
1024 struct vcache *vcp=NULL;
1025 const char *comp = dp->d_name.name;
1027 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1030 struct inode *ip = AFSTOI(vcp);
1031 /* Reset ops if symlink or directory. */
1032 #if defined(AFS_LINUX24_ENV)
1033 if (S_ISREG(ip->i_mode)) {
1034 ip->i_op = &afs_file_iops;
1035 ip->i_fop = &afs_file_fops;
1036 ip->i_data.a_ops = &afs_file_aops;
1037 } else if (S_ISDIR(ip->i_mode)) {
1038 ip->i_op = &afs_dir_iops;
1039 ip->i_fop = &afs_dir_fops;
1040 } else if (S_ISLNK(ip->i_mode)) {
1041 ip->i_op = &afs_symlink_iops;
1042 ip->i_data.a_ops = &afs_symlink_aops;
1043 ip->i_mapping = &ip->i_data;
1045 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);
1047 if (S_ISDIR(ip->i_mode))
1048 ip->i_op = &afs_dir_iops;
1049 else if (S_ISLNK(ip->i_mode))
1050 ip->i_op = &afs_symlink_iops;
1053 dp->d_time = jiffies;
1054 dp->d_op = afs_dops;
1055 d_add(dp, AFSTOI(vcp));
1060 /* It's ok for the file to not be found. That's noted by the caller by
1061 * seeing that the dp->d_inode field is NULL.
1063 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1067 return ERR_PTR(-code);
1075 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1076 struct dentry *newdp)
1079 cred_t *credp = crref();
1080 const char *name = newdp->d_name.name;
1081 struct inode *oldip = olddp->d_inode;
1083 /* If afs_link returned the vnode, we could instantiate the
1084 * dentry. Since it's not, we drop this one and do a new lookup.
1089 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1096 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1099 cred_t *credp = crref();
1100 const char *name = dp->d_name.name;
1103 code = afs_remove(ITOAFS(dip), name, credp);
1112 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1116 cred_t *credp = crref();
1118 const char *name = dp->d_name.name;
1120 /* If afs_symlink returned the vnode, we could instantiate the
1121 * dentry. Since it's not, we drop this one and do a new lookup.
1127 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1133 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1136 cred_t *credp = crref();
1137 struct vcache *tvcp = NULL;
1139 const char *name = dp->d_name.name;
1143 vattr.va_mask = ATTR_MODE;
1144 vattr.va_mode = mode;
1145 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1148 tvcp->v.v_op = &afs_dir_iops;
1149 #if defined(AFS_LINUX24_ENV)
1150 tvcp->v.v_fop = &afs_dir_fops;
1152 dp->d_op = afs_dops;
1153 dp->d_time = jiffies;
1154 d_instantiate(dp, AFSTOI(tvcp));
1162 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1165 cred_t *credp = crref();
1166 const char *name = dp->d_name.name;
1169 code = afs_rmdir(ITOAFS(dip), name, credp);
1171 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1172 * that failed because a directory is not empty. So, we map
1173 * EEXIST to ENOTEMPTY on linux.
1175 if (code == EEXIST) {
1190 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1191 struct inode *newip, struct dentry *newdp)
1194 cred_t *credp = crref();
1195 const char *oldname = olddp->d_name.name;
1196 const char *newname = newdp->d_name.name;
1198 /* Remove old and new entries from name hash. New one will change below.
1199 * While it's optimal to catch failures and re-insert newdp into hash,
1200 * it's also error prone and in that case we're already dealing with error
1201 * cases. Let another lookup put things right, if need be.
1203 if (!list_empty(&olddp->d_hash)) {
1206 if (!list_empty(&newdp->d_hash)) {
1210 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip),
1215 /* update time so it doesn't expire immediately */
1216 newdp->d_time = jiffies;
1217 d_move(olddp, newdp);
1225 /* afs_linux_ireadlink
1226 * Internal readlink which can return link contents to user or kernel space.
1227 * Note that the buffer is NOT supposed to be null-terminated.
1229 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1233 cred_t *credp = crref();
1237 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1238 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1242 return maxlen - tuio.uio_resid;
1247 #if !defined(AFS_LINUX24_ENV)
1248 /* afs_linux_readlink
1249 * Fill target (which is in user space) with contents of symlink.
1251 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1254 struct inode *ip = dp->d_inode;
1257 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1263 /* afs_linux_follow_link
1264 * a file system dependent link following routine.
1266 struct dentry * afs_linux_follow_link(struct dentry *dp,
1267 struct dentry *basep,
1268 unsigned int follow)
1276 name = osi_Alloc(PATH_MAX+1);
1280 return ERR_PTR(-EIO);
1283 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1288 res = ERR_PTR(code);
1292 res = lookup_dentry(name, basep, follow);
1296 osi_Free(name, PATH_MAX+1);
1302 /* afs_linux_readpage
1303 * all reads come through here. A strategy-like read call.
1305 int afs_linux_readpage(struct file *fp, struct page *pp)
1308 cred_t *credp = crref();
1309 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1311 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1313 ulong address = afs_linux_page_address(pp);
1314 afs_offs_t offset = pageoff(pp);
1318 struct inode *ip = FILE_INODE(fp);
1319 int cnt = atomic_read(&pp->count);
1322 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1323 ICL_TYPE_POINTER, ip,
1324 ICL_TYPE_POINTER, pp,
1325 ICL_TYPE_INT32, cnt,
1326 ICL_TYPE_INT32, 99999); /* not a possible code value */
1327 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1333 atomic_add(1, &pp->count);
1334 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1335 clear_bit(PG_error, &pp->flags);
1338 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1339 UIO_READ, AFS_UIOSYS);
1340 code = afs_rdwr(ITOAFS(ip), &tuio, UIO_READ, 0, credp);
1341 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1346 if (tuio.uio_resid) /* zero remainder of page */
1347 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1349 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1350 flush_dcache_page(pp);
1351 SetPageUptodate(pp);
1353 set_bit(PG_uptodate, &pp->flags);
1357 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1361 clear_bit(PG_locked, &pp->flags);
1367 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1368 ICL_TYPE_POINTER, ip,
1369 ICL_TYPE_POINTER, pp,
1370 ICL_TYPE_INT32, cnt,
1371 ICL_TYPE_INT32, code);
1376 #if defined(AFS_LINUX24_ENV)
1377 int afs_linux_writepage(struct page *pp)
1379 struct address_space *mapping = pp->mapping;
1380 struct inode *inode;
1381 unsigned long end_index;
1382 unsigned offset = PAGE_CACHE_SIZE;
1385 inode = (struct inode *) mapping->host;
1386 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1389 if (pp->index < end_index)
1391 /* things got complicated... */
1392 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1393 /* OK, are we completely out? */
1394 if (pp->index >= end_index+1 || !offset)
1398 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1400 SetPageUptodate(pp);
1402 if (status == offset)
1410 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1411 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1413 /* afs_linux_truncate
1414 * Handles discarding disk blocks if this were a device. ext2 indicates we
1415 * may need to zero partial last pages of memory mapped files.
1417 void afs_linux_truncate(struct inode *ip)
1422 /* afs_linux_permission
1423 * Check access rights - returns error if can't check or permission denied.
1425 int afs_linux_permission(struct inode *ip, int mode)
1428 cred_t *credp = crref();
1432 if (mode & MAY_EXEC) tmp |= VEXEC;
1433 if (mode & MAY_READ) tmp |= VREAD;
1434 if (mode & MAY_WRITE) tmp |= VWRITE;
1435 code = afs_access(ITOAFS(ip), tmp, credp);
1444 /* msdos sector mapping hack for memory mapping. */
1445 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1448 #if defined(AFS_LINUX24_ENV)
1449 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1450 unsigned long offset,
1453 struct vcache *vcp = ITOAFS(ip);
1462 buffer = kmap(pp) + offset;
1463 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1466 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1467 ICL_TYPE_POINTER, pp,
1468 ICL_TYPE_INT32, atomic_read(&pp->count),
1469 ICL_TYPE_INT32, 99999);
1470 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1472 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1476 code = code ? -code : count - tuio.uio_resid;
1477 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1478 ICL_TYPE_POINTER, pp,
1479 ICL_TYPE_INT32, atomic_read(&pp->count),
1480 ICL_TYPE_INT32, code);
1489 afs_linux_updatepage(struct file *file, struct page *page,
1490 unsigned long offset, unsigned int count)
1492 struct dentry *dentry = file->f_dentry;
1494 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1497 /* afs_linux_updatepage
1498 * What one would have thought was writepage - write dirty page to file.
1499 * Called from generic_file_write. buffer is still in user space. pagep
1500 * has been filled in with old data if we're updating less than a page.
1502 int afs_linux_updatepage(struct file *fp, struct page *pp,
1503 unsigned long offset,
1504 unsigned int count, int sync)
1506 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1507 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1513 set_bit(PG_locked, &pp->flags);
1517 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1518 ICL_TYPE_POINTER, pp,
1519 ICL_TYPE_INT32, atomic_read(&pp->count),
1520 ICL_TYPE_INT32, 99999);
1521 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1522 count, UIO_WRITE, AFS_UIOSYS);
1524 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1528 code = code ? -code : count - tuio.uio_resid;
1529 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1530 ICL_TYPE_POINTER, pp,
1531 ICL_TYPE_INT32, atomic_read(&pp->count),
1532 ICL_TYPE_INT32, code);
1537 clear_bit(PG_locked, &pp->flags);
1542 #if defined(AFS_LINUX24_ENV)
1543 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1549 code = afs_linux_updatepage(file, page, offset, to-offset);
1557 static int afs_linux_prepare_write(struct file *file, struct page *page,
1558 unsigned from, unsigned to)
1564 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1567 #if defined(AFS_LINUX24_ENV)
1568 struct inode_operations afs_file_iops = {
1569 revalidate: afs_linux_revalidate,
1570 setattr: afs_notify_change,
1571 permission: afs_linux_permission,
1573 struct address_space_operations afs_file_aops = {
1574 readpage: afs_linux_readpage,
1575 writepage: afs_linux_writepage,
1576 commit_write: afs_linux_commit_write,
1577 prepare_write: afs_linux_prepare_write,
1580 struct inode_operations *afs_ops = &afs_file_iops;
1582 struct inode_operations afs_iops = {
1583 &afs_file_fops, /* file operations */
1584 NULL, /* afs_linux_create */
1585 NULL, /* afs_linux_lookup */
1586 NULL, /* afs_linux_link */
1587 NULL, /* afs_linux_unlink */
1588 NULL, /* afs_linux_symlink */
1589 NULL, /* afs_linux_mkdir */
1590 NULL, /* afs_linux_rmdir */
1591 NULL, /* afs_linux_mknod */
1592 NULL, /* afs_linux_rename */
1593 NULL, /* afs_linux_readlink */
1594 NULL, /* afs_linux_follow_link */
1596 NULL, /* afs_linux_writepage */
1597 NULL, /* afs_linux_bmap */
1598 NULL, /* afs_linux_truncate */
1599 afs_linux_permission,
1600 NULL, /* afs_linux_smap */
1601 afs_linux_updatepage,
1602 afs_linux_revalidate,
1605 struct inode_operations *afs_ops = &afs_iops;
1608 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1609 * by what sort of operation is allowed.....
1611 #if defined(AFS_LINUX24_ENV)
1612 struct inode_operations afs_dir_iops = {
1613 create: afs_linux_create,
1614 lookup: afs_linux_lookup,
1615 link: afs_linux_link,
1616 unlink: afs_linux_unlink,
1617 symlink: afs_linux_symlink,
1618 mkdir: afs_linux_mkdir,
1619 rmdir: afs_linux_rmdir,
1620 rename: afs_linux_rename,
1621 revalidate: afs_linux_revalidate,
1622 setattr: afs_notify_change,
1623 permission: afs_linux_permission,
1626 struct inode_operations afs_dir_iops = {
1627 &afs_dir_fops, /* file operations for directories */
1635 NULL, /* afs_linux_mknod */
1637 NULL, /* afs_linux_readlink */
1638 NULL, /* afs_linux_follow_link */
1639 NULL, /* afs_linux_readpage */
1640 NULL, /* afs_linux_writepage */
1641 NULL, /* afs_linux_bmap */
1642 NULL, /* afs_linux_truncate */
1643 afs_linux_permission,
1644 NULL, /* afs_linux_smap */
1645 NULL, /* afs_linux_updatepage */
1646 afs_linux_revalidate,
1650 /* We really need a separate symlink set of ops, since do_follow_link()
1651 * determines if it _is_ a link by checking if the follow_link op is set.
1653 #if defined(AFS_LINUX24_ENV)
1654 static int afs_symlink_filler(struct file *file, struct page *page)
1656 struct inode *ip = (struct inode *) page->mapping->host;
1657 char *p = (char *)kmap(page);
1662 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1666 p[code] = '\0'; /* null terminate? */
1670 SetPageUptodate(page);
1685 struct address_space_operations afs_symlink_aops = {
1686 readpage: afs_symlink_filler
1689 struct inode_operations afs_symlink_iops = {
1690 readlink: page_readlink,
1691 follow_link: page_follow_link,
1692 setattr: afs_notify_change,
1695 struct inode_operations afs_symlink_iops = {
1696 NULL, /* file operations */
1704 NULL, /* afs_linux_mknod */
1707 afs_linux_follow_link,
1708 NULL, /* readpage */
1709 NULL, /* afs_linux_writepage */
1710 NULL, /* afs_linux_bmap */
1711 NULL, /* afs_linux_truncate */
1712 afs_linux_permission, /* tho the code appears to indicate not used? */
1713 NULL, /* afs_linux_smap */
1714 NULL, /* updatepage */
1715 afs_linux_revalidate, /* tho the code appears to indicate not used? */