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
14 * So far the only truly scary part is that Linux relies on the inode cache
15 * to be up to date. Don't you dare break a callback and expect an fstat
16 * to give you meaningful information. This appears to be fixed in the 2.1
17 * development kernels. As it is we can fix this now by intercepting the
21 #include <afsconfig.h>
22 #include "afs/param.h"
27 #include "afs/sysincludes.h"
28 #include "afsincludes.h"
29 #include "afs/afs_stats.h"
30 #include "afs/afs_osidnlc.h"
32 #include "h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "h/smp_lock.h"
38 #define pageoff(pp) pgoff2loff((pp)->index)
40 #define pageoff(pp) pp->offset
43 extern struct vcache *afs_globalVp;
44 extern afs_rwlock_t afs_xvcache;
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 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
60 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
61 cred_t *credp = crref();
65 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
66 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
69 /* get a validated vcache entry */
70 code = afs_InitReq(&treq, credp);
72 code = afs_VerifyVCache(vcp, &treq);
77 #ifdef AFS_64BIT_CLIENT
78 if (*offp + count > afs_vmMappingEnd) {
81 afs_size_t oldOffset = *offp;
84 if (*offp < afs_vmMappingEnd) {
85 /* special case of a buffer crossing the VM mapping end */
86 afs_int32 tcount = afs_vmMappingEnd - *offp;
88 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
90 code = generic_file_read(fp, buf, tcount, offp);
97 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
98 UIO_READ, AFS_UIOSYS);
99 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
100 xfered += count - tuio.uio_resid;
102 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
103 vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
104 ICL_TYPE_INT32, code);
106 *offp += count - tuio.uio_resid;
113 #endif /* AFS_64BIT_CLIENT */
114 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
116 code = generic_file_read(fp, buf, count, offp);
118 #ifdef AFS_64BIT_CLIENT
120 #endif /* AFS_64BIT_CLIENT */
123 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
124 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
133 /* Now we have integrated VM for writes as well as reads. generic_file_write
134 * also takes care of re-positioning the pointer if file is open in append
135 * mode. Call fake open/close to ensure we do writes of core dumps.
138 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
142 struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
143 struct vrequest treq;
144 cred_t *credp = crref();
149 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
150 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
151 (fp->f_flags & O_APPEND) ? 99998 : 99999);
154 /* get a validated vcache entry */
155 code = (ssize_t) afs_InitReq(&treq, credp);
157 code = (ssize_t) afs_VerifyVCache(vcp, &treq);
159 ObtainWriteLock(&vcp->lock, 529);
161 ReleaseWriteLock(&vcp->lock);
165 #ifdef AFS_64BIT_CLIENT
167 if (fp->f_flags & O_APPEND)
168 toffs += vcp->m.Length;
169 if (toffs + count > afs_vmMappingEnd) {
172 afs_size_t oldOffset = *offp;
173 afs_int32 xfered = 0;
175 if (toffs < 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) {
188 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
189 UIO_WRITE, AFS_UIOSYS);
190 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
191 xfered += count - tuio.uio_resid;
194 *offp += count - tuio.uio_resid;
196 /* Purge dirty chunks of file if there are too many dirty chunks.
197 * Inside the write loop, we only do this at a chunk boundary.
198 * Clean up partial chunk if necessary at end of loop.
200 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
201 AFS_CHUNKBASE(oldOffset)) {
202 ObtainWriteLock(&vcp->lock, 402);
203 code = afs_DoPartialWrite(vcp, &treq);
204 vcp->states |= CDirty;
205 ReleaseWriteLock(&vcp->lock);
210 ObtainWriteLock(&vcp->lock, 400);
211 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
213 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
214 vcp->m.Length = toffs;
216 ReleaseWriteLock(&vcp->lock);
220 #endif /* AFS_64BIT_CLIENT */
222 code = generic_file_write(fp, buf, count, offp);
224 #ifdef AFS_64BIT_CLIENT
226 #endif /* AFS_64BIT_CLIENT */
229 ObtainWriteLock(&vcp->lock, 530);
230 vcp->m.Date = osi_Time(); /* set modification time */
231 afs_FakeClose(vcp, credp);
233 code2 = afs_DoPartialWrite(vcp, &treq);
234 if (code2 && code >= 0)
235 code = (ssize_t) - code2;
236 ReleaseWriteLock(&vcp->lock);
238 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
239 ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
247 /* This is a complete rewrite of afs_readdir, since we can make use of
248 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
249 * handling and use of bulkstats will need to be reflected here as well.
252 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
254 extern struct DirEntry *afs_dir_GetBlob();
255 struct vcache *avc = ITOAFS(FILE_INODE(fp));
256 struct vrequest treq;
257 register struct dcache *tdc;
264 afs_size_t origOffset, tlen;
265 cred_t *credp = crref();
266 struct afs_fakestat_state fakestat;
269 AFS_STATCNT(afs_readdir);
271 code = afs_InitReq(&treq, credp);
278 afs_InitFakeStat(&fakestat);
279 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
281 afs_PutFakeStat(&fakestat);
286 /* update the cache entry */
288 code = afs_VerifyVCache(avc, &treq);
290 afs_PutFakeStat(&fakestat);
295 /* get a reference to the entire directory */
296 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
299 afs_PutFakeStat(&fakestat);
303 ObtainReadLock(&avc->lock);
304 ObtainReadLock(&tdc->lock);
306 * Make sure that the data in the cache is current. There are two
307 * cases we need to worry about:
308 * 1. The cache data is being fetched by another process.
309 * 2. The cache data is no longer valid
311 while ((avc->states & CStatd)
312 && (tdc->dflags & DFFetching)
313 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
314 ReleaseReadLock(&tdc->lock);
315 ReleaseReadLock(&avc->lock);
316 afs_osi_Sleep(&tdc->validPos);
317 ObtainReadLock(&avc->lock);
318 ObtainReadLock(&tdc->lock);
320 if (!(avc->states & CStatd)
321 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
322 ReleaseReadLock(&tdc->lock);
323 ReleaseReadLock(&avc->lock);
328 /* Fill in until we get an error or we're done. This implementation
329 * takes an offset in units of blobs, rather than bytes.
332 offset = (int)fp->f_pos;
334 dirpos = BlobScan(&tdc->f.inode, offset);
338 de = afs_dir_GetBlob(&tdc->f.inode, dirpos);
342 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
343 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
345 len = strlen(de->name);
347 printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %x, dirpos %d)\n",
348 &tdc->f.inode, dirpos);
351 ReleaseReadLock(&avc->lock);
352 afs_PutFakeStat(&fakestat);
356 /* filldir returns -EINVAL when the buffer is full. */
357 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
359 unsigned int type = DT_UNKNOWN;
360 struct VenusFid afid;
363 afid.Cell = avc->fid.Cell;
364 afid.Fid.Volume = avc->fid.Fid.Volume;
365 afid.Fid.Vnode = ntohl(de->fid.vnode);
366 afid.Fid.Unique = ntohl(de->fid.vunique);
367 if ((avc->states & CForeign) == 0 && (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 /* in afs_pioctl.c */
413 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
417 /* We need to detect unmap's after close. To do that, we need our own
418 * vm_operations_struct's. And we need to set them up for both the
419 * private and shared mappings. The fun part is that these are all static
420 * so we'll have to initialize on the fly!
422 static struct vm_operations_struct afs_private_mmap_ops;
423 static int afs_private_mmap_ops_inited = 0;
424 static struct vm_operations_struct afs_shared_mmap_ops;
425 static int afs_shared_mmap_ops_inited = 0;
428 afs_linux_vma_close(struct vm_area_struct *vmap)
437 vcp = ITOAFS(FILE_INODE(vmap->vm_file));
442 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
443 ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
444 ICL_TYPE_INT32, vcp->execsOrWriters);
445 if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
446 ObtainWriteLock(&vcp->lock, 532);
449 printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
450 MyPidxx, &vcp, &vmap);
454 ReleaseWriteLock(&vcp->lock);
456 if (need_unlock && vcp->execsOrWriters < 2) {
458 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
459 /* only decrement the execsOrWriters flag if this is not a
461 if (!(vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
462 vcp->execsOrWriters--;
463 vcp->states &= ~CMAPPED;
465 } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
466 vcp->execsOrWriters--;
467 /* If we did not have the lock */
470 if (!vcp->execsOrWriters)
471 vcp->execsOrWriters = 1;
476 ReleaseWriteLock(&vcp->lock);
484 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
486 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
487 cred_t *credp = crref();
488 struct vrequest treq;
492 #if defined(AFS_LINUX24_ENV)
493 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
494 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
495 vmap->vm_end - vmap->vm_start);
497 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
498 ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
499 vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
503 /* get a validated vcache entry */
504 code = afs_InitReq(&treq, credp);
506 code = afs_VerifyVCache(vcp, &treq);
512 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
515 code = generic_file_mmap(fp, vmap);
520 ObtainWriteLock(&vcp->lock, 531);
521 /* Set out vma ops so we catch the close. The following test should be
522 * the same as used in generic_file_mmap.
524 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
525 if (!afs_shared_mmap_ops_inited) {
526 afs_shared_mmap_ops_inited = 1;
527 afs_shared_mmap_ops = *vmap->vm_ops;
528 afs_shared_mmap_ops.close = afs_linux_vma_close;
530 vmap->vm_ops = &afs_shared_mmap_ops;
532 if (!afs_private_mmap_ops_inited) {
533 afs_private_mmap_ops_inited = 1;
534 afs_private_mmap_ops = *vmap->vm_ops;
535 afs_private_mmap_ops.close = afs_linux_vma_close;
537 vmap->vm_ops = &afs_private_mmap_ops;
541 /* Add an open reference on the first mapping. */
542 if (vcp->mapcnt == 0) {
543 vcp->execsOrWriters++;
545 vcp->states |= CMAPPED;
547 ReleaseWriteLock(&vcp->lock);
557 afs_linux_open(struct inode *ip, struct file *fp)
560 cred_t *credp = crref();
563 #ifdef AFS_LINUX24_ENV
566 code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
567 #ifdef AFS_LINUX24_ENV
576 /* afs_Close is called from release, since release is used to handle all
577 * file closings. In addition afs_linux_flush is called from sys_close to
578 * handle flushing the data back to the server. The kicker is that we could
579 * ignore flush completely if only sys_close took it's return value from
580 * fput. See afs_linux_flush for notes on interactions between release and
584 afs_linux_release(struct inode *ip, struct file *fp)
587 cred_t *credp = crref();
588 struct vcache *vcp = ITOAFS(ip);
591 #ifdef AFS_LINUX24_ENV
595 vcp->flushcnt--; /* protected by AFS global lock. */
597 code = afs_close(vcp, fp->f_flags, credp);
599 #ifdef AFS_LINUX24_ENV
608 #if defined(AFS_LINUX24_ENV)
610 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
613 afs_linux_fsync(struct file *fp, struct dentry *dp)
617 struct inode *ip = FILE_INODE(fp);
618 cred_t *credp = crref();
621 #ifdef AFS_LINUX24_ENV
624 code = afs_fsync(ITOAFS(ip), credp);
625 #ifdef AFS_LINUX24_ENV
636 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
639 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
640 cred_t *credp = crref();
641 #ifdef AFS_LINUX24_ENV
642 struct flock64 flock;
647 /* Convert to a lock format afs_lockctl understands. */
648 memset((char *)&flock, 0, sizeof(flock));
649 flock.l_type = flp->fl_type;
650 flock.l_pid = flp->fl_pid;
652 flock.l_start = flp->fl_start;
653 flock.l_len = flp->fl_end - flp->fl_start;
655 /* Safe because there are no large files, yet */
656 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
657 if (cmd == F_GETLK64)
659 else if (cmd == F_SETLK64)
661 else if (cmd == F_SETLKW64)
663 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
666 code = afs_lockctl(vcp, &flock, cmd, credp);
669 /* Convert flock back to Linux's file_lock */
670 flp->fl_type = flock.l_type;
671 flp->fl_pid = flock.l_pid;
672 flp->fl_start = flock.l_start;
673 flp->fl_end = flock.l_start + flock.l_len;
681 * flush is called from sys_close. We could ignore it, but sys_close return
682 * code comes from flush, not release. We need to use release to keep
683 * the vcache open count correct. Note that flush is called before release
684 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
685 * races and also avoid calling afs_close twice when closing the file.
686 * If we merely checked for opens > 0 in afs_linux_release, then if an
687 * new open occurred when storing back the file, afs_linux_release would
688 * incorrectly close the file and decrement the opens count. Calling afs_close
689 * on the just flushed file is wasteful, since the background daemon will
690 * execute the code that finally decides there is nothing to do.
693 afs_linux_flush(struct file *fp)
695 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
699 /* Only do this on the last close of the file pointer. */
700 #if defined(AFS_LINUX24_ENV)
701 if (atomic_read(&fp->f_count) > 1)
710 code = afs_close(vcp, fp->f_flags, credp);
711 vcp->flushcnt++; /* protected by AFS global lock. */
718 #if !defined(AFS_LINUX24_ENV)
719 /* Not allowed to directly read a directory. */
721 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
729 struct file_operations afs_dir_fops = {
730 #if !defined(AFS_LINUX24_ENV)
731 .read = afs_linux_dir_read,
732 .lock = afs_linux_lock,
733 .fsync = afs_linux_fsync,
735 .read = generic_read_dir,
737 .readdir = afs_linux_readdir,
739 .open = afs_linux_open,
740 .release = afs_linux_release,
743 struct file_operations afs_file_fops = {
744 .read = afs_linux_read,
745 .write = afs_linux_write,
747 .mmap = afs_linux_mmap,
748 .open = afs_linux_open,
749 .flush = afs_linux_flush,
750 .release = afs_linux_release,
751 .fsync = afs_linux_fsync,
752 .lock = afs_linux_lock,
756 /**********************************************************************
757 * AFS Linux dentry operations
758 **********************************************************************/
760 /* afs_linux_revalidate
761 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
764 afs_linux_revalidate(struct dentry *dp)
768 struct vrequest treq;
769 struct vcache *vcp = ITOAFS(dp->d_inode);
770 struct vcache *rootvp = NULL;
774 if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
775 && (vcp->states & CMValid) && (vcp->states & CStatd)) {
776 ObtainSharedLock(&afs_xvcache, 680);
777 rootvp = afs_FindVCache(vcp->mvid, 0, 0);
778 ReleaseSharedLock(&afs_xvcache);
780 #ifdef AFS_LINUX24_ENV
784 /* Make this a fast path (no crref), since it's called so often. */
785 if (vcp->states & CStatd) {
786 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
787 check_bad_parent(dp); /* check and correct mvid */
789 vcache2fakeinode(rootvp, vcp);
792 #ifdef AFS_LINUX24_ENV
796 afs_PutVCache(rootvp);
802 code = afs_InitReq(&treq, credp);
804 code = afs_VerifyVCache(vcp, &treq);
806 #ifdef AFS_LINUX24_ENV
816 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
817 * In kernels 2.2.10 and above, we are passed an additional flags var which
818 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
819 * we are advised to follow the entry if it is a link or to make sure that
820 * it is a directory. But since the kernel itself checks these possibilities
821 * later on, we shouldn't have to do it until later. Perhaps in the future..
823 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
825 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
828 afs_linux_dentry_revalidate(struct dentry *dp)
832 cred_t *credp = crref();
833 struct vrequest treq;
834 struct vcache *lookupvcp = NULL;
835 int code, bad_dentry = 1;
836 struct sysname_info sysState;
837 struct vcache *vcp = ITOAFS(dp->d_inode);
838 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
843 sysState.allocked = 0;
845 /* If it's a negative dentry, then there's nothing to do. */
846 if (!vcp || !parentvcp)
849 /* If it is the AFS root, then there's no chance it needs
851 if (vcp == afs_globalVp) {
856 if ((code = afs_InitReq(&treq, credp)))
859 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
860 name = sysState.name;
862 /* First try looking up the DNLC */
863 if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
864 /* Verify that the dentry does not point to an old inode */
865 if (vcp != lookupvcp)
867 /* Check and correct mvid */
868 if (*name != '/' && vcp->mvstat == 2)
869 check_bad_parent(dp);
875 /* A DNLC lookup failure cannot be trusted. Try a real lookup.
876 Make sure to try the real name and not the @sys expansion;
877 afs_lookup will expand @sys itself. */
879 code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
881 /* Verify that the dentry does not point to an old inode */
882 if (vcp != lookupvcp)
890 afs_PutVCache(lookupvcp);
891 if (sysState.allocked)
892 osi_FreeLargeSpace(name);
898 shrink_dcache_parent(dp);
906 /* afs_dentry_iput */
908 afs_dentry_iput(struct dentry *dp, struct inode *ip)
910 if (ICL_SETACTIVE(afs_iclSetp)) {
912 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
913 ICL_TYPE_STRING, dp->d_parent->d_name.name,
914 ICL_TYPE_STRING, dp->d_name.name);
922 afs_dentry_delete(struct dentry *dp)
924 if (ICL_SETACTIVE(afs_iclSetp)) {
926 afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
927 dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
928 ICL_TYPE_STRING, dp->d_name.name);
932 if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
933 return 1; /* bad inode? */
938 struct dentry_operations afs_dentry_operations = {
939 .d_revalidate = afs_linux_dentry_revalidate,
940 .d_iput = afs_dentry_iput,
941 .d_delete = afs_dentry_delete,
943 struct dentry_operations *afs_dops = &afs_dentry_operations;
945 /**********************************************************************
946 * AFS Linux inode operations
947 **********************************************************************/
951 * Merely need to set enough of vattr to get us through the create. Note
952 * that the higher level code (open_namei) will take care of any tuncation
953 * explicitly. Exclusive open is also taken care of in open_namei.
955 * name is in kernel space at this point.
958 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
961 cred_t *credp = crref();
964 const char *name = dp->d_name.name;
968 vattr.va_mode = mode;
972 afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
973 (struct vcache **)&ip, credp);
976 vattr2inode(ip, &vattr);
977 /* Reset ops if symlink or directory. */
978 #if defined(AFS_LINUX24_ENV)
979 if (S_ISREG(ip->i_mode)) {
980 ip->i_op = &afs_file_iops;
981 ip->i_fop = &afs_file_fops;
982 ip->i_data.a_ops = &afs_file_aops;
983 } else if (S_ISDIR(ip->i_mode)) {
984 ip->i_op = &afs_dir_iops;
985 ip->i_fop = &afs_dir_fops;
986 } else if (S_ISLNK(ip->i_mode)) {
987 ip->i_op = &afs_symlink_iops;
988 ip->i_data.a_ops = &afs_symlink_aops;
989 ip->i_mapping = &ip->i_data;
991 printk("afs_linux_create: FIXME\n");
993 if (S_ISDIR(ip->i_mode))
994 ip->i_op = &afs_dir_iops;
995 else if (S_ISLNK(ip->i_mode))
996 ip->i_op = &afs_symlink_iops;
1000 dp->d_time = jiffies;
1001 d_instantiate(dp, ip);
1009 /* afs_linux_lookup */
1010 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1012 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1015 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1019 cred_t *credp = crref();
1020 struct vcache *vcp = NULL;
1021 const char *comp = dp->d_name.name;
1023 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1026 struct inode *ip = AFSTOI(vcp);
1027 /* Reset ops if symlink or directory. */
1028 #if defined(AFS_LINUX24_ENV)
1029 if (S_ISREG(ip->i_mode)) {
1030 ip->i_op = &afs_file_iops;
1031 ip->i_fop = &afs_file_fops;
1032 ip->i_data.a_ops = &afs_file_aops;
1033 } else if (S_ISDIR(ip->i_mode)) {
1034 ip->i_op = &afs_dir_iops;
1035 ip->i_fop = &afs_dir_fops;
1036 } else if (S_ISLNK(ip->i_mode)) {
1037 ip->i_op = &afs_symlink_iops;
1038 ip->i_data.a_ops = &afs_symlink_aops;
1039 ip->i_mapping = &ip->i_data;
1042 ("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n",
1043 ip->i_mode, dp->d_name.name, code);
1045 if (S_ISDIR(ip->i_mode))
1046 ip->i_op = &afs_dir_iops;
1047 else if (S_ISLNK(ip->i_mode))
1048 ip->i_op = &afs_symlink_iops;
1051 dp->d_time = jiffies;
1052 dp->d_op = afs_dops;
1053 d_add(dp, AFSTOI(vcp));
1058 /* It's ok for the file to not be found. That's noted by the caller by
1059 * seeing that the dp->d_inode field is NULL.
1061 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1065 return ERR_PTR(-code);
1074 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1077 cred_t *credp = crref();
1078 const char *name = newdp->d_name.name;
1079 struct inode *oldip = olddp->d_inode;
1081 /* If afs_link returned the vnode, we could instantiate the
1082 * dentry. Since it's not, we drop this one and do a new lookup.
1087 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1095 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1098 cred_t *credp = crref();
1099 const char *name = dp->d_name.name;
1102 code = afs_remove(ITOAFS(dip), name, credp);
1112 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1115 cred_t *credp = crref();
1117 const char *name = dp->d_name.name;
1119 /* If afs_symlink returned the vnode, we could instantiate the
1120 * dentry. Since it's not, we drop this one and do a new lookup.
1126 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1133 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));
1163 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1166 cred_t *credp = crref();
1167 const char *name = dp->d_name.name;
1170 code = afs_rmdir(ITOAFS(dip), name, credp);
1172 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1173 * that failed because a directory is not empty. So, we map
1174 * EEXIST to ENOTEMPTY on linux.
1176 if (code == EEXIST) {
1192 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1193 struct inode *newip, struct dentry *newdp)
1196 cred_t *credp = crref();
1197 const char *oldname = olddp->d_name.name;
1198 const char *newname = newdp->d_name.name;
1200 /* Remove old and new entries from name hash. New one will change below.
1201 * While it's optimal to catch failures and re-insert newdp into hash,
1202 * it's also error prone and in that case we're already dealing with error
1203 * cases. Let another lookup put things right, if need be.
1205 if (!list_empty(&olddp->d_hash)) {
1208 if (!list_empty(&newdp->d_hash)) {
1212 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1216 /* update time so it doesn't expire immediately */
1217 newdp->d_time = jiffies;
1218 d_move(olddp, newdp);
1226 /* afs_linux_ireadlink
1227 * Internal readlink which can return link contents to user or kernel space.
1228 * Note that the buffer is NOT supposed to be null-terminated.
1231 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1234 cred_t *credp = crref();
1238 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1239 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1243 return maxlen - tuio.uio_resid;
1248 #if !defined(AFS_LINUX24_ENV)
1249 /* afs_linux_readlink
1250 * Fill target (which is in user space) with contents of symlink.
1253 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1256 struct inode *ip = dp->d_inode;
1259 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1265 /* afs_linux_follow_link
1266 * a file system dependent link following routine.
1269 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1270 unsigned int follow)
1278 name = osi_Alloc(PATH_MAX + 1);
1282 return ERR_PTR(-EIO);
1285 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1290 res = ERR_PTR(code);
1293 res = lookup_dentry(name, basep, follow);
1297 osi_Free(name, PATH_MAX + 1);
1303 /* afs_linux_readpage
1304 * all reads come through here. A strategy-like read call.
1307 afs_linux_readpage(struct file *fp, struct page *pp)
1310 cred_t *credp = crref();
1311 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1313 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1315 ulong address = afs_linux_page_address(pp);
1316 afs_offs_t offset = pageoff(pp);
1320 struct inode *ip = FILE_INODE(fp);
1321 int cnt = atomic_read(&pp->count);
1322 struct vcache *avc = ITOAFS(ip);
1325 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip, ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32, 99999); /* not a possible code value */
1326 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1332 atomic_add(1, &pp->count);
1333 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1334 clear_bit(PG_error, &pp->flags);
1337 setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1339 code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1340 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1345 if (tuio.uio_resid) /* zero remainder of page */
1346 memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1348 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1349 flush_dcache_page(pp);
1350 SetPageUptodate(pp);
1352 set_bit(PG_uptodate, &pp->flags);
1355 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1359 clear_bit(PG_locked, &pp->flags);
1364 if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1366 struct vrequest treq;
1368 code = afs_InitReq(&treq, credp);
1369 if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1370 tdc = afs_FindDCache(avc, offset);
1372 if (!(tdc->mflags & DFNextStarted))
1373 afs_PrefetchChunk(avc, tdc, credp, &treq);
1376 ReleaseWriteLock(&avc->lock);
1381 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1382 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1388 #if defined(AFS_LINUX24_ENV)
1390 afs_linux_writepage(struct page *pp)
1392 struct address_space *mapping = pp->mapping;
1393 struct inode *inode;
1394 unsigned long end_index;
1395 unsigned offset = PAGE_CACHE_SIZE;
1398 inode = (struct inode *)mapping->host;
1399 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1402 if (pp->index < end_index)
1404 /* things got complicated... */
1405 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1406 /* OK, are we completely out? */
1407 if (pp->index >= end_index + 1 || !offset)
1411 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1413 SetPageUptodate(pp);
1415 if (status == offset)
1422 /* afs_linux_permission
1423 * Check access rights - returns error if can't check or permission denied.
1426 afs_linux_permission(struct inode *ip, int mode)
1429 cred_t *credp = crref();
1433 if (mode & MAY_EXEC)
1435 if (mode & MAY_READ)
1437 if (mode & MAY_WRITE)
1439 code = afs_access(ITOAFS(ip), tmp, credp);
1447 #if defined(AFS_LINUX24_ENV)
1449 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1450 unsigned long offset, unsigned int count)
1452 struct vcache *vcp = ITOAFS(ip);
1461 buffer = kmap(pp) + offset;
1462 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1465 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1466 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1467 ICL_TYPE_INT32, 99999);
1469 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1471 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1476 && afs_stats_cmperf.cacheCurrDirtyChunks >
1477 afs_stats_cmperf.cacheMaxDirtyChunks) {
1478 struct vrequest treq;
1480 ObtainWriteLock(&vcp->lock, 533);
1481 if (!afs_InitReq(&treq, credp))
1482 code = afs_DoPartialWrite(vcp, &treq);
1483 ReleaseWriteLock(&vcp->lock);
1485 code = code ? -code : count - tuio.uio_resid;
1487 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1488 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1489 ICL_TYPE_INT32, code);
1498 afs_linux_updatepage(struct file *file, struct page *page,
1499 unsigned long offset, unsigned int count)
1501 struct dentry *dentry = file->f_dentry;
1503 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1506 /* afs_linux_updatepage
1507 * What one would have thought was writepage - write dirty page to file.
1508 * Called from generic_file_write. buffer is still in user space. pagep
1509 * has been filled in with old data if we're updating less than a page.
1512 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1513 unsigned int count, int sync)
1515 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1516 u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1522 set_bit(PG_locked, &pp->flags);
1526 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1527 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1528 ICL_TYPE_INT32, 99999);
1529 setup_uio(&tuio, &iovec, page_addr + offset,
1530 (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1533 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1537 code = code ? -code : count - tuio.uio_resid;
1538 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1539 ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, atomic_read(&pp->count),
1540 ICL_TYPE_INT32, code);
1545 clear_bit(PG_locked, &pp->flags);
1550 #if defined(AFS_LINUX24_ENV)
1552 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1559 code = afs_linux_updatepage(file, page, offset, to - offset);
1568 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1575 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1578 struct inode_operations afs_file_iops = {
1579 #if defined(AFS_LINUX24_ENV)
1580 .permission = afs_linux_permission,
1581 .revalidate = afs_linux_revalidate,
1582 .setattr = afs_notify_change,
1584 .default_file_ops = &afs_file_fops,
1585 .readpage = afs_linux_readpage,
1586 .revalidate = afs_linux_revalidate,
1587 .updatepage = afs_linux_updatepage,
1591 #if defined(AFS_LINUX24_ENV)
1592 struct address_space_operations afs_file_aops = {
1593 .readpage = afs_linux_readpage,
1594 .writepage = afs_linux_writepage,
1595 .commit_write = afs_linux_commit_write,
1596 .prepare_write = afs_linux_prepare_write,
1601 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1602 * by what sort of operation is allowed.....
1605 struct inode_operations afs_dir_iops = {
1606 #if !defined(AFS_LINUX24_ENV)
1607 .default_file_ops = &afs_dir_fops,
1609 .setattr = afs_notify_change,
1611 .create = afs_linux_create,
1612 .lookup = afs_linux_lookup,
1613 .link = afs_linux_link,
1614 .unlink = afs_linux_unlink,
1615 .symlink = afs_linux_symlink,
1616 .mkdir = afs_linux_mkdir,
1617 .rmdir = afs_linux_rmdir,
1618 .rename = afs_linux_rename,
1619 .revalidate = afs_linux_revalidate,
1620 .permission = afs_linux_permission,
1623 /* We really need a separate symlink set of ops, since do_follow_link()
1624 * determines if it _is_ a link by checking if the follow_link op is set.
1626 #if defined(AFS_LINUX24_ENV)
1628 afs_symlink_filler(struct file *file, struct page *page)
1630 struct inode *ip = (struct inode *)page->mapping->host;
1631 char *p = (char *)kmap(page);
1636 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1640 p[code] = '\0'; /* null terminate? */
1644 SetPageUptodate(page);
1659 struct address_space_operations afs_symlink_aops = {
1660 .readpage = afs_symlink_filler
1664 struct inode_operations afs_symlink_iops = {
1665 #if defined(AFS_LINUX24_ENV)
1666 .readlink = page_readlink,
1667 .follow_link = page_follow_link,
1668 .setattr = afs_notify_change,
1670 .readlink = afs_linux_readlink,
1671 .follow_link = afs_linux_follow_link,
1672 .permission = afs_linux_permission,
1673 .revalidate = afs_linux_revalidate,