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)) {
357 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
360 } else if (((tvc->states) & (CStatd|CTruth))) {
361 /* CTruth will be set if the object has
366 else if (vtype == VREG)
368 /* Don't do this until we're sure it can't be a mtpt */
369 /* else if (vtype == VLNK)
371 /* what other types does AFS support? */
373 /* clean up from afs_FindVCache */
374 afs_PutVCache(tvc, WRITE_LOCK);
376 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
379 code = (*filldir)(dirbuf, de->name, len, offset, ino);
384 offset = dirpos + 1 + ((len+16)>>5);
386 /* If filldir didn't fill in the last one this is still pointing to that
389 fp->f_pos = (loff_t)offset;
391 ReleaseReadLock(&tdc->lock);
393 ReleaseReadLock(&avc->lock);
394 afs_PutFakeStat(&fakestat);
400 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
403 /* in afs_pioctl.c */
404 extern int afs_xioctl(struct inode *ip, struct file *fp,
405 unsigned int com, unsigned long arg);
408 /* We need to detect unmap's after close. To do that, we need our own
409 * vm_operations_struct's. And we need to set them up for both the
410 * private and shared mappings. The fun part is that these are all static
411 * so we'll have to initialize on the fly!
413 static struct vm_operations_struct afs_private_mmap_ops;
414 static int afs_private_mmap_ops_inited = 0;
415 static struct vm_operations_struct afs_shared_mmap_ops;
416 static int afs_shared_mmap_ops_inited = 0;
418 void afs_linux_vma_close(struct vm_area_struct *vmap)
426 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
431 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
432 ICL_TYPE_POINTER, vcp,
433 ICL_TYPE_INT32, vcp->mapcnt,
434 ICL_TYPE_INT32, vcp->opens,
435 ICL_TYPE_INT32, vcp->execsOrWriters);
436 ObtainWriteLock(&vcp->lock, 532);
439 ReleaseWriteLock(&vcp->lock);
442 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
443 /* only decrement the execsOrWriters flag if this is not a writable
445 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
446 vcp->execsOrWriters--;
448 vcp->states &= ~CMAPPED;
453 ReleaseWriteLock(&vcp->lock);
460 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
462 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
463 cred_t *credp = crref();
464 struct vrequest treq;
468 #if defined(AFS_LINUX24_ENV)
469 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
470 ICL_TYPE_POINTER, vmap->vm_start,
471 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
473 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
474 ICL_TYPE_POINTER, vmap->vm_start,
475 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
476 ICL_TYPE_INT32, vmap->vm_offset);
479 /* get a validated vcache entry */
480 code = afs_InitReq(&treq, credp);
482 code = afs_VerifyVCache(vcp, &treq);
488 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
491 code = generic_file_mmap(fp, vmap);
496 ObtainWriteLock(&vcp->lock,531);
497 /* Set out vma ops so we catch the close. The following test should be
498 * the same as used in generic_file_mmap.
500 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
501 if (!afs_shared_mmap_ops_inited) {
502 afs_shared_mmap_ops_inited = 1;
503 afs_shared_mmap_ops = *vmap->vm_ops;
504 afs_shared_mmap_ops.close = afs_linux_vma_close;
506 vmap->vm_ops = &afs_shared_mmap_ops;
509 if (!afs_private_mmap_ops_inited) {
510 afs_private_mmap_ops_inited = 1;
511 afs_private_mmap_ops = *vmap->vm_ops;
512 afs_private_mmap_ops.close = afs_linux_vma_close;
514 vmap->vm_ops = &afs_private_mmap_ops;
518 /* Add an open reference on the first mapping. */
519 if (vcp->mapcnt == 0) {
520 vcp->execsOrWriters++;
522 vcp->states |= CMAPPED;
524 ReleaseWriteLock(&vcp->lock);
533 int afs_linux_open(struct inode *ip, struct file *fp)
536 cred_t *credp = crref();
539 #ifdef AFS_LINUX24_ENV
542 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
543 #ifdef AFS_LINUX24_ENV
552 /* afs_Close is called from release, since release is used to handle all
553 * file closings. In addition afs_linux_flush is called from sys_close to
554 * handle flushing the data back to the server. The kicker is that we could
555 * ignore flush completely if only sys_close took it's return value from
556 * fput. See afs_linux_flush for notes on interactions between release and
559 static int afs_linux_release(struct inode *ip, struct file *fp)
562 cred_t *credp = crref();
563 struct vcache *vcp = ITOAFS(ip);
566 #ifdef AFS_LINUX24_ENV
570 vcp->flushcnt--; /* protected by AFS global lock. */
573 code = afs_close(vcp, fp->f_flags, credp);
575 #ifdef AFS_LINUX24_ENV
584 #if defined(AFS_LINUX24_ENV)
585 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
587 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
591 struct inode *ip = FILE_INODE(fp);
592 cred_t *credp = crref();
595 #ifdef AFS_LINUX24_ENV
598 code = afs_fsync(ITOAFS(ip), credp);
599 #ifdef AFS_LINUX24_ENV
609 /* No support for async i/o */
610 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
612 /* I don't think it will, at least not as can be detected here. */
613 int afs_linux_check_media_change(kdev_t dev);
615 /* Revalidate media and file system. */
616 int afs_linux_file_revalidate(kdev_t dev);
619 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
622 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
623 cred_t *credp = crref();
624 #ifdef AFS_LINUX24_ENV
625 struct flock64 flock;
630 /* Convert to a lock format afs_lockctl understands. */
631 memset((char*)&flock, 0, sizeof(flock));
632 flock.l_type = flp->fl_type;
633 flock.l_pid = flp->fl_pid;
635 flock.l_start = flp->fl_start;
636 flock.l_len = flp->fl_end - flp->fl_start;
639 code = afs_lockctl(vcp, &flock, cmd, credp);
647 * flush is called from sys_close. We could ignore it, but sys_close return
648 * code comes from flush, not release. We need to use release to keep
649 * the vcache open count correct. Note that flush is called before release
650 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
651 * races and also avoid calling afs_close twice when closing the file.
652 * If we merely checked for opens > 0 in afs_linux_release, then if an
653 * new open occurred when storing back the file, afs_linux_release would
654 * incorrectly close the file and decrement the opens count. Calling afs_close
655 * on the just flushed file is wasteful, since the background daemon will
656 * execute the code that finally decides there is nothing to do.
658 int afs_linux_flush(struct file *fp)
660 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
664 /* Only do this on the last close of the file pointer. */
665 #if defined(AFS_LINUX24_ENV)
666 if (atomic_read(&fp->f_count) > 1)
675 code = afs_close(vcp, fp->f_flags, credp);
676 vcp->flushcnt++; /* protected by AFS global lock. */
683 /* Not allowed to directly read a directory. */
684 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
691 #if defined(AFS_LINUX24_ENV)
692 struct file_operations afs_dir_fops = {
693 read: generic_read_dir,
694 readdir: afs_linux_readdir,
696 open: afs_linux_open,
697 release: afs_linux_release,
700 struct file_operations afs_dir_fops = {
701 NULL, /* afs_linux_lseek */
703 NULL, /* afs_linux_write */
705 NULL, /* afs_linux_select */
706 afs_xioctl, /* close enough to use the ported AFS one */
707 NULL, /* afs_linux_mmap */
709 NULL, /* afs_linux_flush */
712 NULL, /* afs_linux_fasync */
713 NULL, /* afs_linux_check_media_change */
714 NULL, /* afs_linux_file_revalidate */
719 #if defined(AFS_LINUX24_ENV)
720 struct file_operations afs_file_fops = {
721 read: afs_linux_read,
722 write: afs_linux_write,
724 mmap: afs_linux_mmap,
725 open: afs_linux_open,
726 flush: afs_linux_flush,
727 release: afs_linux_release,
728 fsync: afs_linux_fsync,
729 lock: afs_linux_lock,
732 struct file_operations afs_file_fops = {
733 NULL, /* afs_linux_lseek */
736 NULL, /* afs_linux_readdir */
737 NULL, /* afs_linux_select */
738 afs_xioctl, /* close enough to use the ported AFS one */
744 NULL, /* afs_linux_fasync */
745 NULL, /* afs_linux_check_media_change */
746 NULL, /* afs_linux_file_revalidate */
752 /**********************************************************************
753 * AFS Linux dentry operations
754 **********************************************************************/
756 /* afs_linux_revalidate
757 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
759 static int afs_linux_revalidate(struct dentry *dp)
763 struct vrequest treq;
764 struct vcache *vcp = ITOAFS(dp->d_inode);
767 #ifdef AFS_LINUX24_ENV
771 /* Make this a fast path (no crref), since it's called so often. */
772 if (vcp->states & CStatd) {
773 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
774 check_bad_parent(dp); /* check and correct mvid */
776 #ifdef AFS_LINUX24_ENV
784 code = afs_InitReq(&treq, credp);
786 code = afs_VerifyVCache(vcp, &treq);
788 #ifdef AFS_LINUX24_ENV
798 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
799 * In kernels 2.2.10 and above, we are passed an additional flags var which
800 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
801 * we are advised to follow the entry if it is a link or to make sure that
802 * it is a directory. But since the kernel itself checks these possibilities
803 * later on, we shouldn't have to do it until later. Perhaps in the future..
805 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
806 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
808 static int afs_linux_dentry_revalidate(struct dentry *dp)
812 cred_t *credp = crref();
813 struct vrequest treq;
814 struct vcache *lookupvcp = NULL;
815 int code, bad_dentry = 1;
816 struct sysname_info sysState;
817 struct vcache *vcp = ITOAFS(dp->d_inode);
818 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
822 sysState.allocked = 0;
824 /* If it's a negative dentry, then there's nothing to do. */
825 if (!vcp || !parentvcp)
828 /* If it is the AFS root, then there's no chance it needs
830 if (vcp == afs_globalVp) {
835 if (code = afs_InitReq(&treq, credp))
838 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
839 name = sysState.name;
841 /* First try looking up the DNLC */
842 if (lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK)) {
843 /* Verify that the dentry does not point to an old inode */
844 if (vcp != lookupvcp)
846 /* Check and correct mvid */
847 if (*name != '/' && vcp->mvstat == 2)
848 check_bad_parent(dp);
854 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
855 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
857 /* Verify that the dentry does not point to an old inode */
858 if (vcp != lookupvcp)
866 afs_PutVCache(lookupvcp, WRITE_LOCK);
867 if (sysState.allocked)
868 osi_FreeLargeSpace(name);
874 shrink_dcache_parent(dp);
882 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
883 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
885 static int afs_linux_dentry_revalidate(struct dentry *dp)
890 struct vrequest treq;
891 struct inode *ip = AFSTOI(dp->d_inode);
893 unsigned long timeout = 3*HZ; /* 3 seconds */
896 printk("negative dentry: %s\n", dp->d_name.name);
898 if (!(flags & LOOKUP_CONTINUE)) {
899 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
905 if (time_after(jiffies, dp->d_time + timeout))
916 /* afs_dentry_iput */
917 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
922 #if defined(AFS_LINUX24_ENV)
923 struct dentry_operations afs_dentry_operations = {
924 d_revalidate: afs_linux_dentry_revalidate,
925 d_iput: afs_dentry_iput,
927 struct dentry_operations *afs_dops = &afs_dentry_operations;
929 struct dentry_operations afs_dentry_operations = {
930 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
932 NULL, /* d_compare */
933 NULL, /* d_delete(struct dentry *) */
934 NULL, /* d_release(struct dentry *) */
935 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
937 struct dentry_operations *afs_dops = &afs_dentry_operations;
940 /**********************************************************************
941 * AFS Linux inode operations
942 **********************************************************************/
946 * Merely need to set enough of vattr to get us through the create. Note
947 * that the higher level code (open_namei) will take care of any tuncation
948 * explicitly. Exclusive open is also taken care of in open_namei.
950 * name is in kernel space at this point.
952 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
955 cred_t *credp = crref();
958 const char *name = dp->d_name.name;
962 vattr.va_mode = mode;
965 code = afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
966 (struct vcache**)&ip, credp);
969 vattr2inode(ip, &vattr);
970 /* Reset ops if symlink or directory. */
971 #if defined(AFS_LINUX24_ENV)
972 if (S_ISREG(ip->i_mode)) {
973 ip->i_op = &afs_file_iops;
974 ip->i_fop = &afs_file_fops;
975 ip->i_data.a_ops = &afs_file_aops;
976 } else if (S_ISDIR(ip->i_mode)) {
977 ip->i_op = &afs_dir_iops;
978 ip->i_fop = &afs_dir_fops;
979 } else if (S_ISLNK(ip->i_mode)) {
980 ip->i_op = &afs_symlink_iops;
981 ip->i_data.a_ops = &afs_symlink_aops;
982 ip->i_mapping = &ip->i_data;
984 printk("afs_linux_create: FIXME\n");
986 if (S_ISDIR(ip->i_mode))
987 ip->i_op = &afs_dir_iops;
988 else if (S_ISLNK(ip->i_mode))
989 ip->i_op = &afs_symlink_iops;
993 dp->d_time = jiffies;
994 d_instantiate(dp, ip);
1002 /* afs_linux_lookup */
1003 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1004 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
1006 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
1010 cred_t *credp = crref();
1011 struct vcache *vcp=NULL;
1012 const char *comp = dp->d_name.name;
1014 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1017 struct inode *ip = AFSTOI(vcp);
1018 /* Reset ops if symlink or directory. */
1019 #if defined(AFS_LINUX24_ENV)
1020 if (S_ISREG(ip->i_mode)) {
1021 ip->i_op = &afs_file_iops;
1022 ip->i_fop = &afs_file_fops;
1023 ip->i_data.a_ops = &afs_file_aops;
1024 } else if (S_ISDIR(ip->i_mode)) {
1025 ip->i_op = &afs_dir_iops;
1026 ip->i_fop = &afs_dir_fops;
1027 } else if (S_ISLNK(ip->i_mode)) {
1028 ip->i_op = &afs_symlink_iops;
1029 ip->i_data.a_ops = &afs_symlink_aops;
1030 ip->i_mapping = &ip->i_data;
1032 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);
1034 if (S_ISDIR(ip->i_mode))
1035 ip->i_op = &afs_dir_iops;
1036 else if (S_ISLNK(ip->i_mode))
1037 ip->i_op = &afs_symlink_iops;
1040 dp->d_time = jiffies;
1041 dp->d_op = afs_dops;
1042 d_add(dp, AFSTOI(vcp));
1047 /* It's ok for the file to not be found. That's noted by the caller by
1048 * seeing that the dp->d_inode field is NULL.
1050 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1054 return ERR_PTR(-code);
1062 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1063 struct dentry *newdp)
1066 cred_t *credp = crref();
1067 const char *name = newdp->d_name.name;
1068 struct inode *oldip = olddp->d_inode;
1070 /* If afs_link returned the vnode, we could instantiate the
1071 * dentry. Since it's not, we drop this one and do a new lookup.
1076 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1083 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1086 cred_t *credp = crref();
1087 const char *name = dp->d_name.name;
1091 code = afs_remove(ITOAFS(dip), name, credp);
1100 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1104 cred_t *credp = crref();
1106 const char *name = dp->d_name.name;
1108 /* If afs_symlink returned the vnode, we could instantiate the
1109 * dentry. Since it's not, we drop this one and do a new lookup.
1115 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1121 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1124 cred_t *credp = crref();
1125 struct vcache *tvcp = NULL;
1127 const char *name = dp->d_name.name;
1131 vattr.va_mask = ATTR_MODE;
1132 vattr.va_mode = mode;
1133 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1136 tvcp->v.v_op = &afs_dir_iops;
1137 #if defined(AFS_LINUX24_ENV)
1138 tvcp->v.v_fop = &afs_dir_fops;
1140 dp->d_op = afs_dops;
1141 dp->d_time = jiffies;
1142 d_instantiate(dp, AFSTOI(tvcp));
1150 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1153 cred_t *credp = crref();
1154 const char *name = dp->d_name.name;
1157 code = afs_rmdir(ITOAFS(dip), name, credp);
1159 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1160 * that failed because a directory is not empty. So, we map
1161 * EEXIST to ENOTEMPTY on linux.
1163 if (code == EEXIST) {
1178 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1179 struct inode *newip, struct dentry *newdp)
1182 cred_t *credp = crref();
1183 const char *oldname = olddp->d_name.name;
1184 const char *newname = newdp->d_name.name;
1186 /* Remove old and new entries from name hash. New one will change below.
1187 * While it's optimal to catch failures and re-insert newdp into hash,
1188 * it's also error prone and in that case we're already dealing with error
1189 * cases. Let another lookup put things right, if need be.
1191 if (!list_empty(&olddp->d_hash)) {
1194 if (!list_empty(&newdp->d_hash)) {
1198 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip),
1203 /* update time so it doesn't expire immediately */
1204 newdp->d_time = jiffies;
1205 d_move(olddp, newdp);
1213 /* afs_linux_ireadlink
1214 * Internal readlink which can return link contents to user or kernel space.
1215 * Note that the buffer is NOT supposed to be null-terminated.
1217 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1221 cred_t *credp = crref();
1225 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1226 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1230 return maxlen - tuio.uio_resid;
1235 #if !defined(AFS_LINUX24_ENV)
1236 /* afs_linux_readlink
1237 * Fill target (which is in user space) with contents of symlink.
1239 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1242 struct inode *ip = dp->d_inode;
1245 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1251 /* afs_linux_follow_link
1252 * a file system dependent link following routine.
1254 struct dentry * afs_linux_follow_link(struct dentry *dp,
1255 struct dentry *basep,
1256 unsigned int follow)
1264 name = osi_Alloc(PATH_MAX+1);
1268 return ERR_PTR(-EIO);
1271 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1276 res = ERR_PTR(code);
1280 res = lookup_dentry(name, basep, follow);
1284 osi_Free(name, PATH_MAX+1);
1290 /* afs_linux_readpage
1291 * all reads come through here. A strategy-like read call.
1293 int afs_linux_readpage(struct file *fp, struct page *pp)
1296 cred_t *credp = crref();
1297 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1299 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1301 ulong address = afs_linux_page_address(pp);
1302 afs_offs_t offset = pageoff(pp);
1306 struct inode *ip = FILE_INODE(fp);
1307 int cnt = atomic_read(&pp->count);
1310 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1311 ICL_TYPE_POINTER, ip,
1312 ICL_TYPE_POINTER, pp,
1313 ICL_TYPE_INT32, cnt,
1314 ICL_TYPE_INT32, 99999); /* not a possible code value */
1315 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1321 atomic_add(1, &pp->count);
1322 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1323 clear_bit(PG_error, &pp->flags);
1326 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1327 UIO_READ, AFS_UIOSYS);
1328 code = afs_rdwr(ITOAFS(ip), &tuio, UIO_READ, 0, credp);
1329 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1334 if (tuio.uio_resid) /* zero remainder of page */
1335 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1338 flush_dcache_page(pp);
1339 SetPageUptodate(pp);
1341 set_bit(PG_uptodate, &pp->flags);
1345 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1349 clear_bit(PG_locked, &pp->flags);
1355 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1356 ICL_TYPE_POINTER, ip,
1357 ICL_TYPE_POINTER, pp,
1358 ICL_TYPE_INT32, cnt,
1359 ICL_TYPE_INT32, code);
1364 #if defined(AFS_LINUX24_ENV)
1365 int afs_linux_writepage(struct page *pp)
1367 struct address_space *mapping = pp->mapping;
1368 struct inode *inode;
1369 unsigned long end_index;
1370 unsigned offset = PAGE_CACHE_SIZE;
1373 inode = (struct inode *) mapping->host;
1374 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1377 if (pp->index < end_index)
1379 /* things got complicated... */
1380 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1381 /* OK, are we completely out? */
1382 if (pp->index >= end_index+1 || !offset)
1386 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1388 SetPageUptodate(pp);
1390 if (status == offset)
1398 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1399 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1401 /* afs_linux_truncate
1402 * Handles discarding disk blocks if this were a device. ext2 indicates we
1403 * may need to zero partial last pages of memory mapped files.
1405 void afs_linux_truncate(struct inode *ip)
1410 /* afs_linux_permission
1411 * Check access rights - returns error if can't check or permission denied.
1413 int afs_linux_permission(struct inode *ip, int mode)
1416 cred_t *credp = crref();
1420 if (mode & MAY_EXEC) tmp |= VEXEC;
1421 if (mode & MAY_READ) tmp |= VREAD;
1422 if (mode & MAY_WRITE) tmp |= VWRITE;
1423 code = afs_access(ITOAFS(ip), tmp, credp);
1432 /* msdos sector mapping hack for memory mapping. */
1433 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1436 #if defined(AFS_LINUX24_ENV)
1437 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1438 unsigned long offset,
1441 struct vcache *vcp = ITOAFS(ip);
1450 buffer = kmap(pp) + offset;
1451 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1454 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1455 ICL_TYPE_POINTER, pp,
1456 ICL_TYPE_INT32, atomic_read(&pp->count),
1457 ICL_TYPE_INT32, 99999);
1458 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1460 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1464 code = code ? -code : count - tuio.uio_resid;
1465 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1466 ICL_TYPE_POINTER, pp,
1467 ICL_TYPE_INT32, atomic_read(&pp->count),
1468 ICL_TYPE_INT32, code);
1477 afs_linux_updatepage(struct file *file, struct page *page,
1478 unsigned long offset, unsigned int count)
1480 struct dentry *dentry = file->f_dentry;
1482 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1485 /* afs_linux_updatepage
1486 * What one would have thought was writepage - write dirty page to file.
1487 * Called from generic_file_write. buffer is still in user space. pagep
1488 * has been filled in with old data if we're updating less than a page.
1490 int afs_linux_updatepage(struct file *fp, struct page *pp,
1491 unsigned long offset,
1492 unsigned int count, int sync)
1494 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1495 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1501 set_bit(PG_locked, &pp->flags);
1505 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1506 ICL_TYPE_POINTER, pp,
1507 ICL_TYPE_INT32, atomic_read(&pp->count),
1508 ICL_TYPE_INT32, 99999);
1509 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1510 count, UIO_WRITE, AFS_UIOSYS);
1512 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1516 code = code ? -code : count - tuio.uio_resid;
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, code);
1525 clear_bit(PG_locked, &pp->flags);
1530 #if defined(AFS_LINUX24_ENV)
1531 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1537 code = afs_linux_updatepage(file, page, offset, to-offset);
1545 static int afs_linux_prepare_write(struct file *file, struct page *page,
1546 unsigned from, unsigned to)
1552 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1555 #if defined(AFS_LINUX24_ENV)
1556 struct inode_operations afs_file_iops = {
1557 revalidate: afs_linux_revalidate,
1558 setattr: afs_notify_change,
1559 permission: afs_linux_permission,
1561 struct address_space_operations afs_file_aops = {
1562 readpage: afs_linux_readpage,
1563 writepage: afs_linux_writepage,
1564 commit_write: afs_linux_commit_write,
1565 prepare_write: afs_linux_prepare_write,
1568 struct inode_operations *afs_ops = &afs_file_iops;
1570 struct inode_operations afs_iops = {
1571 &afs_file_fops, /* file operations */
1572 NULL, /* afs_linux_create */
1573 NULL, /* afs_linux_lookup */
1574 NULL, /* afs_linux_link */
1575 NULL, /* afs_linux_unlink */
1576 NULL, /* afs_linux_symlink */
1577 NULL, /* afs_linux_mkdir */
1578 NULL, /* afs_linux_rmdir */
1579 NULL, /* afs_linux_mknod */
1580 NULL, /* afs_linux_rename */
1581 NULL, /* afs_linux_readlink */
1582 NULL, /* afs_linux_follow_link */
1584 NULL, /* afs_linux_writepage */
1585 NULL, /* afs_linux_bmap */
1586 NULL, /* afs_linux_truncate */
1587 afs_linux_permission,
1588 NULL, /* afs_linux_smap */
1589 afs_linux_updatepage,
1590 afs_linux_revalidate,
1593 struct inode_operations *afs_ops = &afs_iops;
1596 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1597 * by what sort of operation is allowed.....
1599 #if defined(AFS_LINUX24_ENV)
1600 struct inode_operations afs_dir_iops = {
1601 create: afs_linux_create,
1602 lookup: afs_linux_lookup,
1603 link: afs_linux_link,
1604 unlink: afs_linux_unlink,
1605 symlink: afs_linux_symlink,
1606 mkdir: afs_linux_mkdir,
1607 rmdir: afs_linux_rmdir,
1608 rename: afs_linux_rename,
1609 revalidate: afs_linux_revalidate,
1610 setattr: afs_notify_change,
1611 permission: afs_linux_permission,
1614 struct inode_operations afs_dir_iops = {
1615 &afs_dir_fops, /* file operations for directories */
1623 NULL, /* afs_linux_mknod */
1625 NULL, /* afs_linux_readlink */
1626 NULL, /* afs_linux_follow_link */
1627 NULL, /* afs_linux_readpage */
1628 NULL, /* afs_linux_writepage */
1629 NULL, /* afs_linux_bmap */
1630 NULL, /* afs_linux_truncate */
1631 afs_linux_permission,
1632 NULL, /* afs_linux_smap */
1633 NULL, /* afs_linux_updatepage */
1634 afs_linux_revalidate,
1638 /* We really need a separate symlink set of ops, since do_follow_link()
1639 * determines if it _is_ a link by checking if the follow_link op is set.
1641 #if defined(AFS_LINUX24_ENV)
1642 static int afs_symlink_filler(struct file *file, struct page *page)
1644 struct inode *ip = (struct inode *) page->mapping->host;
1645 char *p = (char *)kmap(page);
1650 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1654 p[code] = '\0'; /* null terminate? */
1658 SetPageUptodate(page);
1673 struct address_space_operations afs_symlink_aops = {
1674 readpage: afs_symlink_filler
1677 struct inode_operations afs_symlink_iops = {
1678 readlink: page_readlink,
1679 follow_link: page_follow_link,
1680 setattr: afs_notify_change,
1683 struct inode_operations afs_symlink_iops = {
1684 NULL, /* file operations */
1692 NULL, /* afs_linux_mknod */
1695 afs_linux_follow_link,
1696 NULL, /* readpage */
1697 NULL, /* afs_linux_writepage */
1698 NULL, /* afs_linux_bmap */
1699 NULL, /* afs_linux_truncate */
1700 afs_linux_permission, /* tho the code appears to indicate not used? */
1701 NULL, /* afs_linux_smap */
1702 NULL, /* updatepage */
1703 afs_linux_revalidate, /* tho the code appears to indicate not used? */