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"
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;
45 extern struct dentry_operations *afs_dops;
46 #if defined(AFS_LINUX24_ENV)
47 extern struct inode_operations afs_file_iops;
48 extern struct address_space_operations afs_file_aops;
49 struct address_space_operations afs_symlink_aops;
51 extern struct inode_operations afs_dir_iops;
52 extern struct inode_operations afs_symlink_iops;
56 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
59 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
63 struct vcache *vcp = (struct vcache*)fp->f_dentry->d_inode;
64 cred_t *credp = crref();
68 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
69 ICL_TYPE_OFFSET, offp,
70 ICL_TYPE_INT32, count,
71 ICL_TYPE_INT32, 99999);
73 /* get a validated vcache entry */
74 code = afs_InitReq(&treq, credp);
76 code = afs_VerifyVCache(vcp, &treq);
81 #ifdef AFS_64BIT_CLIENT
82 if (*offp + count > afs_vmMappingEnd) {
85 afs_size_t oldOffset = *offp;
88 if (*offp < afs_vmMappingEnd) {
89 /* special case of a buffer crossing the VM mapping end */
90 afs_int32 tcount = afs_vmMappingEnd - *offp;
92 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
94 code = generic_file_read(fp, buf, tcount, offp);
101 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
102 UIO_READ, AFS_UIOSYS);
103 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
104 xfered += count - tuio.uio_resid;
107 *offp += count - tuio.uio_resid;
114 #endif /* AFS_64BIT_CLIENT */
115 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
117 code = generic_file_read(fp, buf, count, offp);
119 #ifdef AFS_64BIT_CLIENT
121 #endif /* AFS_64BIT_CLIENT */
124 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
125 ICL_TYPE_OFFSET, offp,
126 ICL_TYPE_INT32, count,
127 ICL_TYPE_INT32, code);
135 /* Now we have integrated VM for writes as well as reads. generic_file_write
136 * also takes care of re-positioning the pointer if file is open in append
137 * mode. Call fake open/close to ensure we do writes of core dumps.
139 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
144 struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
145 struct vrequest treq;
146 cred_t *credp = crref();
150 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
151 ICL_TYPE_OFFSET, offp,
152 ICL_TYPE_INT32, count,
153 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
156 /* get a validated vcache entry */
157 code = (ssize_t)afs_InitReq(&treq, credp);
159 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
161 ObtainWriteLock(&vcp->lock, 529);
163 ReleaseWriteLock(&vcp->lock);
167 #ifdef AFS_64BIT_CLIENT
168 if (*offp + count > afs_vmMappingEnd) {
171 afs_size_t oldOffset = *offp;
172 afs_int32 xfered = 0;
174 if (*offp < afs_vmMappingEnd) {
175 /* special case of a buffer crossing the VM mapping end */
176 afs_int32 tcount = afs_vmMappingEnd - *offp;
179 code = generic_file_write(fp, buf, tcount, offp);
181 if (code != tcount) {
186 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
187 UIO_WRITE, AFS_UIOSYS);
188 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
189 xfered += count - tuio.uio_resid;
192 *offp += count - tuio.uio_resid;
194 /* Purge dirty chunks of file if there are too many dirty chunks.
195 * Inside the write loop, we only do this at a chunk boundary.
196 * Clean up partial chunk if necessary at end of loop.
198 if (AFS_CHUNKBASE(tuio.afsio_offset) != AFS_CHUNKBASE(oldOffset)) {
199 ObtainWriteLock(&vcp->lock,402);
200 code = afs_DoPartialWrite(vcp, &treq);
201 vcp->states |= CDirty;
202 ReleaseWriteLock(&vcp->lock);
206 ObtainWriteLock(&vcp->lock,400);
207 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
209 if (*offp > vcp->m.Length) {
210 vcp->m.Length = *offp;
212 ReleaseWriteLock(&vcp->lock);
216 #endif /* AFS_64BIT_CLIENT */
218 code = generic_file_write(fp, buf, count, offp);
220 #ifdef AFS_64BIT_CLIENT
222 #endif /* AFS_64BIT_CLIENT */
225 ObtainWriteLock(&vcp->lock, 530);
226 vcp->m.Date = osi_Time(); /* set modification time */
227 afs_FakeClose(vcp, credp);
229 code2 = afs_DoPartialWrite(vcp, &treq);
230 if (code2 && code >=0)
231 code = (ssize_t) -code2;
232 ReleaseWriteLock(&vcp->lock);
234 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
235 ICL_TYPE_OFFSET, offp,
236 ICL_TYPE_INT32, count,
237 ICL_TYPE_INT32, code);
244 /* This is a complete rewrite of afs_readdir, since we can make use of
245 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
246 * handling and use of bulkstats will need to be reflected here as well.
248 static int afs_linux_readdir(struct file *fp,
249 void *dirbuf, filldir_t filldir)
251 extern struct DirEntry * afs_dir_GetBlob();
252 struct vcache *avc = (struct vcache*)FILE_INODE(fp);
253 struct vrequest treq;
254 register struct dcache *tdc;
261 afs_size_t origOffset, tlen;
262 cred_t *credp = crref();
265 AFS_STATCNT(afs_readdir);
267 code = afs_InitReq(&treq, credp);
274 /* update the cache entry */
276 code = afs_VerifyVCache(avc, &treq);
282 /* get a reference to the entire directory */
283 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
289 ObtainReadLock(&avc->lock);
291 * Make sure that the data in the cache is current. There are two
292 * cases we need to worry about:
293 * 1. The cache data is being fetched by another process.
294 * 2. The cache data is no longer valid
296 while ((avc->states & CStatd)
297 && (tdc->flags & DFFetching)
298 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
299 tdc->flags |= DFWaiting;
300 ReleaseReadLock(&avc->lock);
301 afs_osi_Sleep(&tdc->validPos);
302 ObtainReadLock(&avc->lock);
304 if (!(avc->states & CStatd)
305 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
306 ReleaseReadLock(&avc->lock);
311 /* Fill in until we get an error or we're done. This implementation
312 * takes an offset in units of blobs, rather than bytes.
315 offset = (int)fp->f_pos;
317 dirpos = BlobScan(&tdc->f.inode, offset);
321 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
325 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
326 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
327 len = strlen(de->name);
329 /* filldir returns -EINVAL when the buffer is full. */
330 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
332 unsigned int type=DT_UNKNOWN;
333 struct VenusFid afid;
336 afid.Cell=avc->fid.Cell;
337 afid.Fid.Volume=avc->fid.Fid.Volume;
338 afid.Fid.Vnode=ntohl(de->fid.vnode);
339 afid.Fid.Unique=ntohl(de->fid.vunique);
340 if ((avc->states & CForeign) == 0 &&
341 (ntohl(de->fid.vnode) & 1)) {
342 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
345 } else if (((tvc->states) & (CStatd|CTruth))) {
346 /* CTruth will be set if the object has
351 else if (vtype == VREG)
353 /* Don't do this until we're sure it can't be a mtpt */
354 /* else if (vtype == VLNK)
356 /* what other types does AFS support? */
358 /* clean up from afs_FindVCache */
359 afs_PutVCache(tvc, WRITE_LOCK);
361 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
364 code = (*filldir)(dirbuf, de->name, len, offset, ino);
369 offset = dirpos + 1 + ((len+16)>>5);
371 /* If filldir didn't fill in the last one this is still pointing to that
374 fp->f_pos = (loff_t)offset;
377 ReleaseReadLock(&avc->lock);
383 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
386 /* in afs_pioctl.c */
387 extern int afs_xioctl(struct inode *ip, struct file *fp,
388 unsigned int com, unsigned long arg);
391 /* We need to detect unmap's after close. To do that, we need our own
392 * vm_operations_struct's. And we need to set them up for both the
393 * private and shared mappings. The fun part is that these are all static
394 * so we'll have to initialize on the fly!
396 static struct vm_operations_struct afs_private_mmap_ops;
397 static int afs_private_mmap_ops_inited = 0;
398 static struct vm_operations_struct afs_shared_mmap_ops;
399 static int afs_shared_mmap_ops_inited = 0;
401 void afs_linux_vma_close(struct vm_area_struct *vmap)
409 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
414 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
415 ICL_TYPE_POINTER, vcp,
416 ICL_TYPE_INT32, vcp->mapcnt,
417 ICL_TYPE_INT32, vcp->opens,
418 ICL_TYPE_INT32, vcp->execsOrWriters);
419 ObtainWriteLock(&vcp->lock, 532);
422 ReleaseWriteLock(&vcp->lock);
425 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
426 /* only decrement the execsOrWriters flag if this is not a writable
428 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
429 vcp->execsOrWriters--;
431 vcp->states &= ~CMAPPED;
436 ReleaseWriteLock(&vcp->lock);
443 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
445 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
446 cred_t *credp = crref();
447 struct vrequest treq;
451 #if defined(AFS_LINUX24_ENV)
452 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
453 ICL_TYPE_POINTER, vmap->vm_start,
454 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
456 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
457 ICL_TYPE_POINTER, vmap->vm_start,
458 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
459 ICL_TYPE_INT32, vmap->vm_offset);
462 /* get a validated vcache entry */
463 code = afs_InitReq(&treq, credp);
465 code = afs_VerifyVCache(vcp, &treq);
471 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
474 code = generic_file_mmap(fp, vmap);
479 ObtainWriteLock(&vcp->lock,531);
480 /* Set out vma ops so we catch the close. The following test should be
481 * the same as used in generic_file_mmap.
483 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
484 if (!afs_shared_mmap_ops_inited) {
485 afs_shared_mmap_ops_inited = 1;
486 afs_shared_mmap_ops = *vmap->vm_ops;
487 afs_shared_mmap_ops.close = afs_linux_vma_close;
489 vmap->vm_ops = &afs_shared_mmap_ops;
492 if (!afs_private_mmap_ops_inited) {
493 afs_private_mmap_ops_inited = 1;
494 afs_private_mmap_ops = *vmap->vm_ops;
495 afs_private_mmap_ops.close = afs_linux_vma_close;
497 vmap->vm_ops = &afs_private_mmap_ops;
501 /* Add an open reference on the first mapping. */
502 if (vcp->mapcnt == 0) {
503 vcp->execsOrWriters++;
505 vcp->states |= CMAPPED;
507 ReleaseWriteLock(&vcp->lock);
516 int afs_linux_open(struct inode *ip, struct file *fp)
519 cred_t *credp = crref();
522 #ifdef AFS_LINUX24_ENV
525 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
526 #ifdef AFS_LINUX24_ENV
535 /* afs_Close is called from release, since release is used to handle all
536 * file closings. In addition afs_linux_flush is called from sys_close to
537 * handle flushing the data back to the server. The kicker is that we could
538 * ignore flush completely if only sys_close took it's return value from
539 * fput. See afs_linux_flush for notes on interactions between release and
542 static int afs_linux_release(struct inode *ip, struct file *fp)
545 cred_t *credp = crref();
546 struct vcache *vcp = (struct vcache*)ip;
549 #ifdef AFS_LINUX24_ENV
553 vcp->flushcnt--; /* protected by AFS global lock. */
556 code = afs_close(vcp, fp->f_flags, credp);
558 #ifdef AFS_LINUX24_ENV
567 #if defined(AFS_LINUX24_ENV)
568 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
570 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
574 struct inode *ip = FILE_INODE(fp);
575 cred_t *credp = crref();
578 #ifdef AFS_LINUX24_ENV
581 code = afs_fsync((struct vcache*)ip, credp);
582 #ifdef AFS_LINUX24_ENV
592 /* No support for async i/o */
593 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
595 /* I don't think it will, at least not as can be detected here. */
596 int afs_linux_check_media_change(kdev_t dev);
598 /* Revalidate media and file system. */
599 int afs_linux_file_revalidate(kdev_t dev);
602 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
605 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
606 cred_t *credp = crref();
607 #ifdef AFS_LINUX24_ENV
608 struct flock64 flock;
613 /* Convert to a lock format afs_lockctl understands. */
614 memset((char*)&flock, 0, sizeof(flock));
615 flock.l_type = flp->fl_type;
616 flock.l_pid = flp->fl_pid;
618 flock.l_start = flp->fl_start;
619 flock.l_len = flp->fl_end - flp->fl_start;
622 code = afs_lockctl(vcp, &flock, cmd, credp);
630 * flush is called from sys_close. We could ignore it, but sys_close return
631 * code comes from flush, not release. We need to use release to keep
632 * the vcache open count correct. Note that flush is called before release
633 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
634 * races and also avoid calling afs_close twice when closing the file.
635 * If we merely checked for opens > 0 in afs_linux_release, then if an
636 * new open occurred when storing back the file, afs_linux_release would
637 * incorrectly close the file and decrement the opens count. Calling afs_close
638 * on the just flushed file is wasteful, since the background daemon will
639 * execute the code that finally decides there is nothing to do.
641 int afs_linux_flush(struct file *fp)
643 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
647 /* Only do this on the last close of the file pointer. */
648 #if defined(AFS_LINUX24_ENV)
649 if (atomic_read(&fp->f_count) > 1)
658 code = afs_close(vcp, fp->f_flags, credp);
659 vcp->flushcnt++; /* protected by AFS global lock. */
666 /* Not allowed to directly read a directory. */
667 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
674 #if defined(AFS_LINUX24_ENV)
675 struct file_operations afs_dir_fops = {
676 read: generic_read_dir,
677 readdir: afs_linux_readdir,
679 open: afs_linux_open,
680 release: afs_linux_release,
683 struct file_operations afs_dir_fops = {
684 NULL, /* afs_linux_lseek */
686 NULL, /* afs_linux_write */
688 NULL, /* afs_linux_select */
689 afs_xioctl, /* close enough to use the ported AFS one */
690 NULL, /* afs_linux_mmap */
692 NULL, /* afs_linux_flush */
695 NULL, /* afs_linux_fasync */
696 NULL, /* afs_linux_check_media_change */
697 NULL, /* afs_linux_file_revalidate */
702 #if defined(AFS_LINUX24_ENV)
703 struct file_operations afs_file_fops = {
704 read: afs_linux_read,
705 write: afs_linux_write,
707 mmap: afs_linux_mmap,
708 open: afs_linux_open,
709 flush: afs_linux_flush,
710 release: afs_linux_release,
711 fsync: afs_linux_fsync,
712 lock: afs_linux_lock,
715 struct file_operations afs_file_fops = {
716 NULL, /* afs_linux_lseek */
719 NULL, /* afs_linux_readdir */
720 NULL, /* afs_linux_select */
721 afs_xioctl, /* close enough to use the ported AFS one */
727 NULL, /* afs_linux_fasync */
728 NULL, /* afs_linux_check_media_change */
729 NULL, /* afs_linux_file_revalidate */
735 /**********************************************************************
736 * AFS Linux dentry operations
737 **********************************************************************/
739 /* afs_linux_revalidate
740 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
742 static int afs_linux_revalidate(struct dentry *dp)
746 struct vrequest treq;
747 struct vcache *vcp = (struct vcache*)dp->d_inode;
750 #ifdef AFS_LINUX24_ENV
754 /* Make this a fast path (no crref), since it's called so often. */
755 if (vcp->states & CStatd) {
756 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
757 check_bad_parent(dp); /* check and correct mvid */
759 #ifdef AFS_LINUX24_ENV
767 code = afs_InitReq(&treq, credp);
769 code = afs_VerifyVCache(vcp, &treq);
771 #ifdef AFS_LINUX24_ENV
780 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
781 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
783 static int afs_linux_dentry_revalidate(struct dentry *dp)
788 struct vrequest treq;
789 struct inode *ip = (struct inode *)dp->d_inode;
791 unsigned long timeout = 3*HZ; /* 3 seconds */
793 if (!(flags & LOOKUP_CONTINUE)) {
794 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
800 if (time_after(jiffies, dp->d_time + timeout))
810 /* afs_dentry_iput */
811 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
816 #if defined(AFS_LINUX24_ENV)
817 struct dentry_operations afs_dentry_operations = {
818 d_revalidate: afs_linux_dentry_revalidate,
819 d_iput: afs_dentry_iput,
821 struct dentry_operations *afs_dops = &afs_dentry_operations;
823 struct dentry_operations afs_dentry_operations = {
824 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
826 NULL, /* d_compare */
827 NULL, /* d_delete(struct dentry *) */
828 NULL, /* d_release(struct dentry *) */
829 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
831 struct dentry_operations *afs_dops = &afs_dentry_operations;
834 /**********************************************************************
835 * AFS Linux inode operations
836 **********************************************************************/
840 * Merely need to set enough of vattr to get us through the create. Note
841 * that the higher level code (open_namei) will take care of any tuncation
842 * explicitly. Exclusive open is also taken care of in open_namei.
844 * name is in kernel space at this point.
846 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
849 cred_t *credp = crref();
852 const char *name = dp->d_name.name;
856 vattr.va_mode = mode;
859 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
860 (struct vcache**)&ip, credp);
863 vattr2inode(ip, &vattr);
864 /* Reset ops if symlink or directory. */
865 #if defined(AFS_LINUX24_ENV)
866 if (S_ISREG(ip->i_mode)) {
867 ip->i_op = &afs_file_iops;
868 ip->i_fop = &afs_file_fops;
869 ip->i_data.a_ops = &afs_file_aops;
870 } else if (S_ISDIR(ip->i_mode)) {
871 ip->i_op = &afs_dir_iops;
872 ip->i_fop = &afs_dir_fops;
873 } else if (S_ISLNK(ip->i_mode)) {
874 ip->i_op = &afs_symlink_iops;
875 ip->i_data.a_ops = &afs_symlink_aops;
876 ip->i_mapping = &ip->i_data;
878 printk("afs_linux_create: FIXME\n");
880 if (S_ISDIR(ip->i_mode))
881 ip->i_op = &afs_dir_iops;
882 else if (S_ISLNK(ip->i_mode))
883 ip->i_op = &afs_symlink_iops;
887 dp->d_time = jiffies;
888 d_instantiate(dp, ip);
896 /* afs_linux_lookup */
897 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
898 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
900 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
904 cred_t *credp = crref();
905 struct vcache *vcp=NULL;
906 const char *comp = dp->d_name.name;
908 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
911 struct inode *ip = (struct inode*)vcp;
912 /* Reset ops if symlink or directory. */
913 #if defined(AFS_LINUX24_ENV)
914 if (S_ISREG(ip->i_mode)) {
915 ip->i_op = &afs_file_iops;
916 ip->i_fop = &afs_file_fops;
917 ip->i_data.a_ops = &afs_file_aops;
918 } else if (S_ISDIR(ip->i_mode)) {
919 ip->i_op = &afs_dir_iops;
920 ip->i_fop = &afs_dir_fops;
921 } else if (S_ISLNK(ip->i_mode)) {
922 ip->i_op = &afs_symlink_iops;
923 ip->i_data.a_ops = &afs_symlink_aops;
924 ip->i_mapping = &ip->i_data;
926 printk("afs_linux_lookup: FIXME\n");
928 if (S_ISDIR(ip->i_mode))
929 ip->i_op = &afs_dir_iops;
930 else if (S_ISLNK(ip->i_mode))
931 ip->i_op = &afs_symlink_iops;
934 dp->d_time = jiffies;
936 d_add(dp, (struct inode*)vcp);
941 /* It's ok for the file to not be found. That's noted by the caller by
942 * seeing that the dp->d_inode field is NULL.
944 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
948 return ERR_PTR(-code);
956 int afs_linux_link(struct dentry *olddp, struct inode *dip,
957 struct dentry *newdp)
960 cred_t *credp = crref();
961 const char *name = newdp->d_name.name;
962 struct inode *oldip = olddp->d_inode;
964 /* If afs_link returned the vnode, we could instantiate the
965 * dentry. Since it's not, we drop this one and do a new lookup.
970 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
977 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
980 cred_t *credp = crref();
981 const char *name = dp->d_name.name;
984 if (!list_empty(&dp->d_hash)) {
986 /* Install a definite non-existence if we're the only user. */
987 #if defined(AFS_LINUX24_ENV)
988 if (atomic_read(&dp->d_count) == 1)
990 if (dp->d_count == 1)
996 code = afs_remove((struct vcache*)dip, name, credp);
1001 dp->d_time = jiffies;
1002 d_add(dp, NULL); /* means definitely does _not_ exist */
1010 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1014 cred_t *credp = crref();
1016 const char *name = dp->d_name.name;
1018 /* If afs_symlink returned the vnode, we could instantiate the
1019 * dentry. Since it's not, we drop this one and do a new lookup.
1025 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
1031 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1034 cred_t *credp = crref();
1035 struct vcache *tvcp = NULL;
1037 const char *name = dp->d_name.name;
1041 vattr.va_mask = ATTR_MODE;
1042 vattr.va_mode = mode;
1043 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
1046 tvcp->v.v_op = &afs_dir_iops;
1047 #if defined(AFS_LINUX24_ENV)
1048 tvcp->v.v_fop = &afs_dir_fops;
1050 dp->d_op = afs_dops;
1051 dp->d_time = jiffies;
1052 d_instantiate(dp, (struct inode*)tvcp);
1059 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1062 cred_t *credp = crref();
1063 const char *name = dp->d_name.name;
1066 code = afs_rmdir((struct vcache*)dip, name, credp);
1068 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1069 * that failed because a directory is not empty. So, we map
1070 * EEXIST to ENOTEMPTY on linux.
1072 if (code == EEXIST) {
1087 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1088 struct inode *newip, struct dentry *newdp)
1091 cred_t *credp = crref();
1092 const char *oldname = olddp->d_name.name;
1093 const char *newname = newdp->d_name.name;
1095 /* Remove old and new entries from name hash. New one will change below.
1096 * While it's optimal to catch failures and re-insert newdp into hash,
1097 * it's also error prone and in that case we're already dealing with error
1098 * cases. Let another lookup put things right, if need be.
1100 if (!list_empty(&olddp->d_hash)) {
1103 if (!list_empty(&newdp->d_hash)) {
1107 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1112 /* update time so it doesn't expire immediately */
1113 newdp->d_time = jiffies;
1114 d_move(olddp, newdp);
1122 /* afs_linux_ireadlink
1123 * Internal readlink which can return link contents to user or kernel space.
1124 * Note that the buffer is NOT supposed to be null-terminated.
1126 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1130 cred_t *credp = crref();
1134 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1135 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1139 return maxlen - tuio.uio_resid;
1144 #if !defined(AFS_LINUX24_ENV)
1145 /* afs_linux_readlink
1146 * Fill target (which is in user space) with contents of symlink.
1148 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1151 struct inode *ip = dp->d_inode;
1154 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1160 /* afs_linux_follow_link
1161 * a file system dependent link following routine.
1163 struct dentry * afs_linux_follow_link(struct dentry *dp,
1164 struct dentry *basep,
1165 unsigned int follow)
1173 name = osi_Alloc(PATH_MAX+1);
1177 return ERR_PTR(-EIO);
1180 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1185 res = ERR_PTR(code);
1189 res = lookup_dentry(name, basep, follow);
1193 osi_Free(name, PATH_MAX+1);
1199 /* afs_linux_readpage
1200 * all reads come through here. A strategy-like read call.
1202 int afs_linux_readpage(struct file *fp, struct page *pp)
1205 cred_t *credp = crref();
1206 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1208 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1210 ulong address = afs_linux_page_address(pp);
1211 afs_offs_t offset = pageoff(pp);
1215 struct inode *ip = FILE_INODE(fp);
1216 int cnt = atomic_read(&pp->count);
1219 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1220 ICL_TYPE_POINTER, ip,
1221 ICL_TYPE_POINTER, pp,
1222 ICL_TYPE_INT32, cnt,
1223 ICL_TYPE_INT32, 99999); /* not a possible code value */
1224 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1230 atomic_add(1, &pp->count);
1231 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1232 clear_bit(PG_error, &pp->flags);
1235 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1236 UIO_READ, AFS_UIOSYS);
1237 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1243 if (tuio.uio_resid) /* zero remainder of page */
1244 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1246 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1247 flush_dcache_page(pp);
1248 SetPageUptodate(pp);
1250 set_bit(PG_uptodate, &pp->flags);
1254 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1258 clear_bit(PG_locked, &pp->flags);
1264 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1265 ICL_TYPE_POINTER, ip,
1266 ICL_TYPE_POINTER, pp,
1267 ICL_TYPE_INT32, cnt,
1268 ICL_TYPE_INT32, code);
1273 #if defined(AFS_LINUX24_ENV)
1274 int afs_linux_writepage(struct page *pp)
1276 struct address_space *mapping = pp->mapping;
1277 struct inode *inode;
1278 unsigned long end_index;
1279 unsigned offset = PAGE_CACHE_SIZE;
1282 inode = (struct inode *) mapping->host;
1283 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1286 if (pp->index < end_index)
1288 /* things got complicated... */
1289 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1290 /* OK, are we completely out? */
1291 if (pp->index >= end_index+1 || !offset)
1295 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1297 SetPageUptodate(pp);
1299 if (status == offset)
1307 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1308 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1310 /* afs_linux_truncate
1311 * Handles discarding disk blocks if this were a device. ext2 indicates we
1312 * may need to zero partial last pages of memory mapped files.
1314 void afs_linux_truncate(struct inode *ip)
1319 /* afs_linux_permission
1320 * Check access rights - returns error if can't check or permission denied.
1322 int afs_linux_permission(struct inode *ip, int mode)
1325 cred_t *credp = crref();
1329 if (mode & MAY_EXEC) tmp |= VEXEC;
1330 if (mode & MAY_READ) tmp |= VREAD;
1331 if (mode & MAY_WRITE) tmp |= VWRITE;
1332 code = afs_access((struct vcache*)ip, tmp, credp);
1341 /* msdos sector mapping hack for memory mapping. */
1342 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1345 #if defined(AFS_LINUX24_ENV)
1346 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1347 unsigned long offset,
1350 struct vcache *vcp = (struct vcache *) ip;
1359 buffer = kmap(pp) + offset;
1360 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1363 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1364 ICL_TYPE_POINTER, pp,
1365 ICL_TYPE_INT32, atomic_read(&pp->count),
1366 ICL_TYPE_INT32, 99999);
1367 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1369 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1373 code = code ? -code : count - tuio.uio_resid;
1374 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1375 ICL_TYPE_POINTER, pp,
1376 ICL_TYPE_INT32, atomic_read(&pp->count),
1377 ICL_TYPE_INT32, code);
1386 afs_linux_updatepage(struct file *file, struct page *page,
1387 unsigned long offset, unsigned int count)
1389 struct dentry *dentry = file->f_dentry;
1391 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1394 /* afs_linux_updatepage
1395 * What one would have thought was writepage - write dirty page to file.
1396 * Called from generic_file_write. buffer is still in user space. pagep
1397 * has been filled in with old data if we're updating less than a page.
1399 int afs_linux_updatepage(struct file *fp, struct page *pp,
1400 unsigned long offset,
1401 unsigned int count, int sync)
1403 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1404 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1410 set_bit(PG_locked, &pp->flags);
1414 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1415 ICL_TYPE_POINTER, pp,
1416 ICL_TYPE_INT32, atomic_read(&pp->count),
1417 ICL_TYPE_INT32, 99999);
1418 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1419 count, UIO_WRITE, AFS_UIOSYS);
1421 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1425 code = code ? -code : count - tuio.uio_resid;
1426 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1427 ICL_TYPE_POINTER, pp,
1428 ICL_TYPE_INT32, atomic_read(&pp->count),
1429 ICL_TYPE_INT32, code);
1434 clear_bit(PG_locked, &pp->flags);
1439 #if defined(AFS_LINUX24_ENV)
1440 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1446 code = afs_linux_updatepage(file, page, offset, to-offset);
1454 static int afs_linux_prepare_write(struct file *file, struct page *page,
1455 unsigned from, unsigned to)
1461 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1464 #if defined(AFS_LINUX24_ENV)
1465 struct inode_operations afs_file_iops = {
1466 revalidate: afs_linux_revalidate,
1467 setattr: afs_notify_change,
1468 permission: afs_linux_permission,
1470 struct address_space_operations afs_file_aops = {
1471 readpage: afs_linux_readpage,
1472 writepage: afs_linux_writepage,
1473 commit_write: afs_linux_commit_write,
1474 prepare_write: afs_linux_prepare_write,
1477 struct inode_operations *afs_ops = &afs_file_iops;
1479 struct inode_operations afs_iops = {
1480 &afs_file_fops, /* file operations */
1481 NULL, /* afs_linux_create */
1482 NULL, /* afs_linux_lookup */
1483 NULL, /* afs_linux_link */
1484 NULL, /* afs_linux_unlink */
1485 NULL, /* afs_linux_symlink */
1486 NULL, /* afs_linux_mkdir */
1487 NULL, /* afs_linux_rmdir */
1488 NULL, /* afs_linux_mknod */
1489 NULL, /* afs_linux_rename */
1490 NULL, /* afs_linux_readlink */
1491 NULL, /* afs_linux_follow_link */
1493 NULL, /* afs_linux_writepage */
1494 NULL, /* afs_linux_bmap */
1495 NULL, /* afs_linux_truncate */
1496 afs_linux_permission,
1497 NULL, /* afs_linux_smap */
1498 afs_linux_updatepage,
1499 afs_linux_revalidate,
1502 struct inode_operations *afs_ops = &afs_iops;
1505 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1506 * by what sort of operation is allowed.....
1508 #if defined(AFS_LINUX24_ENV)
1509 struct inode_operations afs_dir_iops = {
1510 create: afs_linux_create,
1511 lookup: afs_linux_lookup,
1512 link: afs_linux_link,
1513 unlink: afs_linux_unlink,
1514 symlink: afs_linux_symlink,
1515 mkdir: afs_linux_mkdir,
1516 rmdir: afs_linux_rmdir,
1517 rename: afs_linux_rename,
1518 revalidate: afs_linux_revalidate,
1519 setattr: afs_notify_change,
1520 permission: afs_linux_permission,
1523 struct inode_operations afs_dir_iops = {
1524 &afs_dir_fops, /* file operations for directories */
1532 NULL, /* afs_linux_mknod */
1534 NULL, /* afs_linux_readlink */
1535 NULL, /* afs_linux_follow_link */
1536 NULL, /* afs_linux_readpage */
1537 NULL, /* afs_linux_writepage */
1538 NULL, /* afs_linux_bmap */
1539 NULL, /* afs_linux_truncate */
1540 afs_linux_permission,
1541 NULL, /* afs_linux_smap */
1542 NULL, /* afs_linux_updatepage */
1543 afs_linux_revalidate,
1547 /* We really need a separate symlink set of ops, since do_follow_link()
1548 * determines if it _is_ a link by checking if the follow_link op is set.
1550 #if defined(AFS_LINUX24_ENV)
1551 static int afs_symlink_filler(struct file *file, struct page *page)
1553 struct inode *ip = (struct inode *) page->mapping->host;
1554 char *p = (char *)kmap(page);
1559 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1563 p[code] = '\0'; /* null terminate? */
1567 SetPageUptodate(page);
1582 struct address_space_operations afs_symlink_aops = {
1583 readpage: afs_symlink_filler
1586 struct inode_operations afs_symlink_iops = {
1587 readlink: page_readlink,
1588 follow_link: page_follow_link,
1589 setattr: afs_notify_change,
1592 struct inode_operations afs_symlink_iops = {
1593 NULL, /* file operations */
1601 NULL, /* afs_linux_mknod */
1604 afs_linux_follow_link,
1605 NULL, /* readpage */
1606 NULL, /* afs_linux_writepage */
1607 NULL, /* afs_linux_bmap */
1608 NULL, /* afs_linux_truncate */
1609 afs_linux_permission, /* tho the code appears to indicate not used? */
1610 NULL, /* afs_linux_smap */
1611 NULL, /* updatepage */
1612 afs_linux_revalidate, /* tho the code appears to indicate not used? */