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_INT32, (int)*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 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
83 code = generic_file_read(fp, buf, count, offp);
87 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
88 ICL_TYPE_INT32, (int)*offp,
89 ICL_TYPE_INT32, count,
90 ICL_TYPE_INT32, code);
98 /* Now we have integrated VM for writes as well as reads. generic_file_write
99 * also takes care of re-positioning the pointer if file is open in append
100 * mode. Call fake open/close to ensure we do writes of core dumps.
102 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
107 struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
108 struct vrequest treq;
109 cred_t *credp = crref();
113 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
114 ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
115 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
118 /* get a validated vcache entry */
119 code = (ssize_t)afs_InitReq(&treq, credp);
121 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
123 ObtainWriteLock(&vcp->lock, 529);
125 ReleaseWriteLock(&vcp->lock);
130 code = generic_file_write(fp, buf, count, offp);
134 ObtainWriteLock(&vcp->lock, 530);
135 vcp->m.Date = osi_Time(); /* set modification time */
136 afs_FakeClose(vcp, credp);
138 code2 = afs_DoPartialWrite(vcp, &treq);
139 if (code2 && code >=0)
140 code = (ssize_t) -code2;
141 ReleaseWriteLock(&vcp->lock);
143 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
144 ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
145 ICL_TYPE_INT32, code);
152 /* This is a complete rewrite of afs_readdir, since we can make use of
153 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
154 * handling and use of bulkstats will need to be reflected here as well.
156 static int afs_linux_readdir(struct file *fp,
157 void *dirbuf, filldir_t filldir)
159 extern struct DirEntry * afs_dir_GetBlob();
160 struct vcache *avc = (struct vcache*)FILE_INODE(fp);
161 struct vrequest treq;
162 register struct dcache *tdc;
170 cred_t *credp = crref();
173 AFS_STATCNT(afs_readdir);
175 code = afs_InitReq(&treq, credp);
182 /* update the cache entry */
184 code = afs_VerifyVCache(avc, &treq);
190 /* get a reference to the entire directory */
191 tdc = afs_GetDCache(avc, 0, &treq, &origOffset, &len, 1);
196 ObtainReadLock(&avc->lock);
198 * Make sure that the data in the cache is current. There are two
199 * cases we need to worry about:
200 * 1. The cache data is being fetched by another process.
201 * 2. The cache data is no longer valid
203 while ((avc->states & CStatd)
204 && (tdc->flags & DFFetching)
205 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
206 tdc->flags |= DFWaiting;
207 ReleaseReadLock(&avc->lock);
208 afs_osi_Sleep(&tdc->validPos);
209 ObtainReadLock(&avc->lock);
211 if (!(avc->states & CStatd)
212 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
213 ReleaseReadLock(&avc->lock);
218 /* Fill in until we get an error or we're done. This implementation
219 * takes an offset in units of blobs, rather than bytes.
222 offset = (int)fp->f_pos;
224 dirpos = BlobScan(&tdc->f.inode, offset);
228 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
232 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
233 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
234 len = strlen(de->name);
236 /* filldir returns -EINVAL when the buffer is full. */
237 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
239 unsigned int type=DT_UNKNOWN;
240 struct VenusFid afid;
243 afid.Cell=avc->fid.Cell;
244 afid.Fid.Volume=avc->fid.Fid.Volume;
245 afid.Fid.Vnode=ntohl(de->fid.vnode);
246 afid.Fid.Unique=ntohl(de->fid.vunique);
247 if ((avc->states & CForeign) == 0 &&
248 (ntohl(de->fid.vnode) & 1)) {
250 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
253 } else if (((tvc->states) & (CStatd|CTruth))) {
254 /* CTruth will be set if the object has
259 else if (vtype == VREG)
261 /* Don't do this until we're sure it can't be a mtpt */
262 /* else if (vtype == VLNK)
264 /* what other types does AFS support? */
266 /* clean up from afs_FindVCache */
267 afs_PutVCache(tvc, WRITE_LOCK);
269 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
272 code = (*filldir)(dirbuf, de->name, len, offset, ino);
277 offset = dirpos + 1 + ((len+16)>>5);
279 /* If filldir didn't fill in the last one this is still pointing to that
282 fp->f_pos = (loff_t)offset;
285 ReleaseReadLock(&avc->lock);
291 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
294 /* in afs_pioctl.c */
295 extern int afs_xioctl(struct inode *ip, struct file *fp,
296 unsigned int com, unsigned long arg);
299 /* We need to detect unmap's after close. To do that, we need our own
300 * vm_operations_struct's. And we need to set them up for both the
301 * private and shared mappings. The fun part is that these are all static
302 * so we'll have to initialize on the fly!
304 static struct vm_operations_struct afs_private_mmap_ops;
305 static int afs_private_mmap_ops_inited = 0;
306 static struct vm_operations_struct afs_shared_mmap_ops;
307 static int afs_shared_mmap_ops_inited = 0;
309 void afs_linux_vma_close(struct vm_area_struct *vmap)
317 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
322 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
323 ICL_TYPE_POINTER, vcp,
324 ICL_TYPE_INT32, vcp->mapcnt,
325 ICL_TYPE_INT32, vcp->opens,
326 ICL_TYPE_INT32, vcp->execsOrWriters);
327 ObtainWriteLock(&vcp->lock, 532);
330 ReleaseWriteLock(&vcp->lock);
333 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
334 /* only decrement the execsOrWriters flag if this is not a writable
336 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
337 vcp->execsOrWriters--;
339 vcp->states &= ~CMAPPED;
344 ReleaseWriteLock(&vcp->lock);
351 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
353 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
354 cred_t *credp = crref();
355 struct vrequest treq;
359 #if defined(AFS_LINUX24_ENV)
360 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
361 ICL_TYPE_POINTER, vmap->vm_start,
362 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
364 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
365 ICL_TYPE_POINTER, vmap->vm_start,
366 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
367 ICL_TYPE_INT32, vmap->vm_offset);
370 /* get a validated vcache entry */
371 code = afs_InitReq(&treq, credp);
373 code = afs_VerifyVCache(vcp, &treq);
379 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
382 code = generic_file_mmap(fp, vmap);
387 ObtainWriteLock(&vcp->lock,531);
388 /* Set out vma ops so we catch the close. The following test should be
389 * the same as used in generic_file_mmap.
391 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
392 if (!afs_shared_mmap_ops_inited) {
393 afs_shared_mmap_ops_inited = 1;
394 afs_shared_mmap_ops = *vmap->vm_ops;
395 afs_shared_mmap_ops.close = afs_linux_vma_close;
397 vmap->vm_ops = &afs_shared_mmap_ops;
400 if (!afs_private_mmap_ops_inited) {
401 afs_private_mmap_ops_inited = 1;
402 afs_private_mmap_ops = *vmap->vm_ops;
403 afs_private_mmap_ops.close = afs_linux_vma_close;
405 vmap->vm_ops = &afs_private_mmap_ops;
409 /* Add an open reference on the first mapping. */
410 if (vcp->mapcnt == 0) {
411 vcp->execsOrWriters++;
413 vcp->states |= CMAPPED;
415 ReleaseWriteLock(&vcp->lock);
424 int afs_linux_open(struct inode *ip, struct file *fp)
427 cred_t *credp = crref();
430 #ifdef AFS_LINUX24_ENV
433 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
434 #ifdef AFS_LINUX24_ENV
443 /* afs_Close is called from release, since release is used to handle all
444 * file closings. In addition afs_linux_flush is called from sys_close to
445 * handle flushing the data back to the server. The kicker is that we could
446 * ignore flush completely if only sys_close took it's return value from
447 * fput. See afs_linux_flush for notes on interactions between release and
450 static int afs_linux_release(struct inode *ip, struct file *fp)
453 cred_t *credp = crref();
454 struct vcache *vcp = (struct vcache*)ip;
457 #ifdef AFS_LINUX24_ENV
461 vcp->flushcnt--; /* protected by AFS global lock. */
464 code = afs_close(vcp, fp->f_flags, credp);
466 #ifdef AFS_LINUX24_ENV
475 #if defined(AFS_LINUX24_ENV)
476 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
478 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
482 struct inode *ip = FILE_INODE(fp);
483 cred_t *credp = crref();
486 #ifdef AFS_LINUX24_ENV
489 code = afs_fsync((struct vcache*)ip, credp);
490 #ifdef AFS_LINUX24_ENV
500 /* No support for async i/o */
501 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
503 /* I don't think it will, at least not as can be detected here. */
504 int afs_linux_check_media_change(kdev_t dev);
506 /* Revalidate media and file system. */
507 int afs_linux_file_revalidate(kdev_t dev);
510 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
513 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
514 cred_t *credp = crref();
515 #ifdef AFS_LINUX24_ENV
516 struct flock64 flock;
521 /* Convert to a lock format afs_lockctl understands. */
522 memset((char*)&flock, 0, sizeof(flock));
523 flock.l_type = flp->fl_type;
524 flock.l_pid = flp->fl_pid;
526 flock.l_start = flp->fl_start;
527 flock.l_len = flp->fl_end - flp->fl_start;
530 code = afs_lockctl(vcp, &flock, cmd, credp);
538 * flush is called from sys_close. We could ignore it, but sys_close return
539 * code comes from flush, not release. We need to use release to keep
540 * the vcache open count correct. Note that flush is called before release
541 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
542 * races and also avoid calling afs_close twice when closing the file.
543 * If we merely checked for opens > 0 in afs_linux_release, then if an
544 * new open occurred when storing back the file, afs_linux_release would
545 * incorrectly close the file and decrement the opens count. Calling afs_close
546 * on the just flushed file is wasteful, since the background daemon will
547 * execute the code that finally decides there is nothing to do.
549 int afs_linux_flush(struct file *fp)
551 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
555 /* Only do this on the last close of the file pointer. */
556 #if defined(AFS_LINUX24_ENV)
557 if (atomic_read(&fp->f_count) > 1)
566 code = afs_close(vcp, fp->f_flags, credp);
567 vcp->flushcnt++; /* protected by AFS global lock. */
574 /* Not allowed to directly read a directory. */
575 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
582 #if defined(AFS_LINUX24_ENV)
583 struct file_operations afs_dir_fops = {
584 read: generic_read_dir,
585 readdir: afs_linux_readdir,
587 open: afs_linux_open,
588 release: afs_linux_release,
591 struct file_operations afs_dir_fops = {
592 NULL, /* afs_linux_lseek */
594 NULL, /* afs_linux_write */
596 NULL, /* afs_linux_select */
597 afs_xioctl, /* close enough to use the ported AFS one */
598 NULL, /* afs_linux_mmap */
600 NULL, /* afs_linux_flush */
603 NULL, /* afs_linux_fasync */
604 NULL, /* afs_linux_check_media_change */
605 NULL, /* afs_linux_file_revalidate */
610 #if defined(AFS_LINUX24_ENV)
611 struct file_operations afs_file_fops = {
612 read: afs_linux_read,
613 write: afs_linux_write,
615 mmap: afs_linux_mmap,
616 open: afs_linux_open,
617 flush: afs_linux_flush,
618 release: afs_linux_release,
619 fsync: afs_linux_fsync,
620 lock: afs_linux_lock,
623 struct file_operations afs_file_fops = {
624 NULL, /* afs_linux_lseek */
627 NULL, /* afs_linux_readdir */
628 NULL, /* afs_linux_select */
629 afs_xioctl, /* close enough to use the ported AFS one */
635 NULL, /* afs_linux_fasync */
636 NULL, /* afs_linux_check_media_change */
637 NULL, /* afs_linux_file_revalidate */
643 /**********************************************************************
644 * AFS Linux dentry operations
645 **********************************************************************/
647 /* afs_linux_revalidate
648 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
650 static int afs_linux_revalidate(struct dentry *dp)
654 struct vrequest treq;
655 struct vcache *vcp = (struct vcache*)dp->d_inode;
658 #ifdef AFS_LINUX24_ENV
662 /* Make this a fast path (no crref), since it's called so often. */
663 if (vcp->states & CStatd) {
664 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
665 check_bad_parent(dp); /* check and correct mvid */
667 #ifdef AFS_LINUX24_ENV
675 code = afs_InitReq(&treq, credp);
677 code = afs_VerifyVCache(vcp, &treq);
679 #ifdef AFS_LINUX24_ENV
688 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
689 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
691 static int afs_linux_dentry_revalidate(struct dentry *dp)
696 struct vrequest treq;
697 struct inode *ip = (struct inode *)dp->d_inode;
699 unsigned long timeout = 3*HZ; /* 3 seconds */
701 if (!(flags & LOOKUP_CONTINUE)) {
702 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
708 if (time_after(jiffies, dp->d_time + timeout))
718 /* afs_dentry_iput */
719 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
724 #if defined(AFS_LINUX24_ENV)
725 struct dentry_operations afs_dentry_operations = {
726 d_revalidate: afs_linux_dentry_revalidate,
727 d_iput: afs_dentry_iput,
729 struct dentry_operations *afs_dops = &afs_dentry_operations;
731 struct dentry_operations afs_dentry_operations = {
732 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
734 NULL, /* d_compare */
735 NULL, /* d_delete(struct dentry *) */
736 NULL, /* d_release(struct dentry *) */
737 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
739 struct dentry_operations *afs_dops = &afs_dentry_operations;
742 /**********************************************************************
743 * AFS Linux inode operations
744 **********************************************************************/
748 * Merely need to set enough of vattr to get us through the create. Note
749 * that the higher level code (open_namei) will take care of any tuncation
750 * explicitly. Exclusive open is also taken care of in open_namei.
752 * name is in kernel space at this point.
754 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
757 cred_t *credp = crref();
760 const char *name = dp->d_name.name;
764 vattr.va_mode = mode;
767 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
768 (struct vcache**)&ip, credp);
771 vattr2inode(ip, &vattr);
772 /* Reset ops if symlink or directory. */
773 #if defined(AFS_LINUX24_ENV)
774 if (S_ISREG(ip->i_mode)) {
775 ip->i_op = &afs_file_iops;
776 ip->i_fop = &afs_file_fops;
777 ip->i_data.a_ops = &afs_file_aops;
778 } else if (S_ISDIR(ip->i_mode)) {
779 ip->i_op = &afs_dir_iops;
780 ip->i_fop = &afs_dir_fops;
781 } else if (S_ISLNK(ip->i_mode)) {
782 ip->i_op = &afs_symlink_iops;
783 ip->i_data.a_ops = &afs_symlink_aops;
784 ip->i_mapping = &ip->i_data;
786 printk("afs_linux_create: FIXME\n");
788 if (S_ISDIR(ip->i_mode))
789 ip->i_op = &afs_dir_iops;
790 else if (S_ISLNK(ip->i_mode))
791 ip->i_op = &afs_symlink_iops;
795 dp->d_time = jiffies;
796 d_instantiate(dp, ip);
804 /* afs_linux_lookup */
805 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
806 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
808 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
812 cred_t *credp = crref();
813 struct vcache *vcp=NULL;
814 const char *comp = dp->d_name.name;
816 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
819 struct inode *ip = (struct inode*)vcp;
820 /* Reset ops if symlink or directory. */
821 #if defined(AFS_LINUX24_ENV)
822 if (S_ISREG(ip->i_mode)) {
823 ip->i_op = &afs_file_iops;
824 ip->i_fop = &afs_file_fops;
825 ip->i_data.a_ops = &afs_file_aops;
826 } else if (S_ISDIR(ip->i_mode)) {
827 ip->i_op = &afs_dir_iops;
828 ip->i_fop = &afs_dir_fops;
829 } else if (S_ISLNK(ip->i_mode)) {
830 ip->i_op = &afs_symlink_iops;
831 ip->i_data.a_ops = &afs_symlink_aops;
832 ip->i_mapping = &ip->i_data;
834 printk("afs_linux_lookup: FIXME\n");
836 if (S_ISDIR(ip->i_mode))
837 ip->i_op = &afs_dir_iops;
838 else if (S_ISLNK(ip->i_mode))
839 ip->i_op = &afs_symlink_iops;
842 dp->d_time = jiffies;
844 d_add(dp, (struct inode*)vcp);
849 /* It's ok for the file to not be found. That's noted by the caller by
850 * seeing that the dp->d_inode field is NULL.
852 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
856 return ERR_PTR(-code);
864 int afs_linux_link(struct dentry *olddp, struct inode *dip,
865 struct dentry *newdp)
868 cred_t *credp = crref();
869 const char *name = newdp->d_name.name;
870 struct inode *oldip = olddp->d_inode;
872 /* If afs_link returned the vnode, we could instantiate the
873 * dentry. Since it's not, we drop this one and do a new lookup.
878 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
885 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
888 cred_t *credp = crref();
889 const char *name = dp->d_name.name;
892 if (!list_empty(&dp->d_hash)) {
894 /* Install a definite non-existence if we're the only user. */
895 #if defined(AFS_LINUX24_ENV)
896 if (atomic_read(&dp->d_count) == 1)
898 if (dp->d_count == 1)
904 code = afs_remove((struct vcache*)dip, name, credp);
909 dp->d_time = jiffies;
910 d_add(dp, NULL); /* means definitely does _not_ exist */
918 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
922 cred_t *credp = crref();
924 const char *name = dp->d_name.name;
926 /* If afs_symlink returned the vnode, we could instantiate the
927 * dentry. Since it's not, we drop this one and do a new lookup.
933 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
939 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
942 cred_t *credp = crref();
943 struct vcache *tvcp = NULL;
945 const char *name = dp->d_name.name;
949 vattr.va_mask = ATTR_MODE;
950 vattr.va_mode = mode;
951 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
954 tvcp->v.v_op = &afs_dir_iops;
955 #if defined(AFS_LINUX24_ENV)
956 tvcp->v.v_fop = &afs_dir_fops;
959 dp->d_time = jiffies;
960 d_instantiate(dp, (struct inode*)tvcp);
967 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
970 cred_t *credp = crref();
971 const char *name = dp->d_name.name;
974 code = afs_rmdir((struct vcache*)dip, name, credp);
976 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
977 * that failed because a directory is not empty. So, we map
978 * EEXIST to ENOTEMPTY on linux.
980 if (code == EEXIST) {
995 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
996 struct inode *newip, struct dentry *newdp)
999 cred_t *credp = crref();
1000 const char *oldname = olddp->d_name.name;
1001 const char *newname = newdp->d_name.name;
1003 /* Remove old and new entries from name hash. New one will change below.
1004 * While it's optimal to catch failures and re-insert newdp into hash,
1005 * it's also error prone and in that case we're already dealing with error
1006 * cases. Let another lookup put things right, if need be.
1008 if (!list_empty(&olddp->d_hash)) {
1011 if (!list_empty(&newdp->d_hash)) {
1015 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1020 /* update time so it doesn't expire immediately */
1021 newdp->d_time = jiffies;
1022 d_move(olddp, newdp);
1030 /* afs_linux_ireadlink
1031 * Internal readlink which can return link contents to user or kernel space.
1032 * Note that the buffer is NOT supposed to be null-terminated.
1034 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1038 cred_t *credp = crref();
1042 setup_uio(&tuio, &iov, target, 0, maxlen, UIO_READ, seg);
1043 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1047 return maxlen - tuio.uio_resid;
1052 #if !defined(AFS_LINUX24_ENV)
1053 /* afs_linux_readlink
1054 * Fill target (which is in user space) with contents of symlink.
1056 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1059 struct inode *ip = dp->d_inode;
1062 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1068 /* afs_linux_follow_link
1069 * a file system dependent link following routine.
1071 struct dentry * afs_linux_follow_link(struct dentry *dp,
1072 struct dentry *basep,
1073 unsigned int follow)
1080 name = osi_Alloc(PATH_MAX+1);
1084 return ERR_PTR(-EIO);
1087 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1092 res = ERR_PTR(code);
1096 res = lookup_dentry(name, basep, follow);
1100 osi_Free(name, PATH_MAX+1);
1106 /* afs_linux_readpage
1107 * all reads come through here. A strategy-like read call.
1109 int afs_linux_readpage(struct file *fp, struct page *pp)
1112 cred_t *credp = crref();
1113 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1115 loff_t offset = pp->index << PAGE_CACHE_SHIFT;
1117 ulong address = afs_linux_page_address(pp);
1118 loff_t offset = pageoff(pp);
1122 struct inode *ip = FILE_INODE(fp);
1123 int cnt = atomic_read(&pp->count);
1126 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1127 ICL_TYPE_POINTER, ip,
1128 ICL_TYPE_POINTER, pp,
1129 ICL_TYPE_INT32, cnt,
1130 ICL_TYPE_INT32, 99999); /* not a possible code value */
1132 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1138 atomic_add(1, &pp->count);
1139 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1140 clear_bit(PG_error, &pp->flags);
1143 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1144 UIO_READ, AFS_UIOSYS);
1145 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1146 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1151 if (tuio.uio_resid) /* zero remainder of page */
1152 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1154 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1155 flush_dcache_page(pp);
1156 SetPageUptodate(pp);
1158 set_bit(PG_uptodate, &pp->flags);
1162 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1166 clear_bit(PG_locked, &pp->flags);
1172 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1173 ICL_TYPE_POINTER, ip,
1174 ICL_TYPE_POINTER, pp,
1175 ICL_TYPE_INT32, cnt,
1176 ICL_TYPE_INT32, code);
1181 #if defined(AFS_LINUX24_ENV)
1182 int afs_linux_writepage(struct page *pp)
1184 struct address_space *mapping = pp->mapping;
1185 struct inode *inode;
1186 unsigned long end_index;
1187 unsigned offset = PAGE_CACHE_SIZE;
1190 inode = (struct inode *) mapping->host;
1191 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1194 if (pp->index < end_index)
1196 /* things got complicated... */
1197 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1198 /* OK, are we completely out? */
1199 if (pp->index >= end_index+1 || !offset)
1203 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1205 SetPageUptodate(pp);
1207 if (status == offset)
1215 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1216 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1218 /* afs_linux_truncate
1219 * Handles discarding disk blocks if this were a device. ext2 indicates we
1220 * may need to zero partial last pages of memory mapped files.
1222 void afs_linux_truncate(struct inode *ip)
1227 /* afs_linux_permission
1228 * Check access rights - returns error if can't check or permission denied.
1230 int afs_linux_permission(struct inode *ip, int mode)
1233 cred_t *credp = crref();
1237 if (mode & MAY_EXEC) tmp |= VEXEC;
1238 if (mode & MAY_READ) tmp |= VREAD;
1239 if (mode & MAY_WRITE) tmp |= VWRITE;
1240 code = afs_access((struct vcache*)ip, tmp, credp);
1249 /* msdos sector mapping hack for memory mapping. */
1250 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1253 #if defined(AFS_LINUX24_ENV)
1254 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1255 unsigned long offset,
1258 struct vcache *vcp = (struct vcache *) ip;
1267 buffer = kmap(pp) + offset;
1268 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1271 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1272 ICL_TYPE_POINTER, pp,
1273 ICL_TYPE_INT32, atomic_read(&pp->count),
1274 ICL_TYPE_INT32, 99999);
1275 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1277 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1281 code = code ? -code : count - tuio.uio_resid;
1282 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1283 ICL_TYPE_POINTER, pp,
1284 ICL_TYPE_INT32, atomic_read(&pp->count),
1285 ICL_TYPE_INT32, code);
1294 afs_linux_updatepage(struct file *file, struct page *page,
1295 unsigned long offset, unsigned int count)
1297 struct dentry *dentry = file->f_dentry;
1299 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1302 /* afs_linux_updatepage
1303 * What one would have thought was writepage - write dirty page to file.
1304 * Called from generic_file_write. buffer is still in user space. pagep
1305 * has been filled in with old data if we're updating less than a page.
1307 int afs_linux_updatepage(struct file *fp, struct page *pp,
1308 unsigned long offset,
1309 unsigned int count, int sync)
1311 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1312 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1318 set_bit(PG_locked, &pp->flags);
1322 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1323 ICL_TYPE_POINTER, pp,
1324 ICL_TYPE_INT32, atomic_read(&pp->count),
1325 ICL_TYPE_INT32, 99999);
1326 setup_uio(&tuio, &iovec, page_addr + offset, pageoff(pp) + offset, count,
1327 UIO_WRITE, AFS_UIOSYS);
1329 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1333 code = code ? -code : count - tuio.uio_resid;
1334 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1335 ICL_TYPE_POINTER, pp,
1336 ICL_TYPE_INT32, atomic_read(&pp->count),
1337 ICL_TYPE_INT32, code);
1342 clear_bit(PG_locked, &pp->flags);
1347 #if defined(AFS_LINUX24_ENV)
1348 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1354 code = afs_linux_updatepage(file, page, offset, to-offset);
1362 static int afs_linux_prepare_write(struct file *file, struct page *page,
1363 unsigned from, unsigned to)
1369 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1372 #if defined(AFS_LINUX24_ENV)
1373 struct inode_operations afs_file_iops = {
1374 revalidate: afs_linux_revalidate,
1375 setattr: afs_notify_change,
1376 permission: afs_linux_permission,
1378 struct address_space_operations afs_file_aops = {
1379 readpage: afs_linux_readpage,
1380 writepage: afs_linux_writepage,
1381 commit_write: afs_linux_commit_write,
1382 prepare_write: afs_linux_prepare_write,
1385 struct inode_operations *afs_ops = &afs_file_iops;
1387 struct inode_operations afs_iops = {
1388 &afs_file_fops, /* file operations */
1389 NULL, /* afs_linux_create */
1390 NULL, /* afs_linux_lookup */
1391 NULL, /* afs_linux_link */
1392 NULL, /* afs_linux_unlink */
1393 NULL, /* afs_linux_symlink */
1394 NULL, /* afs_linux_mkdir */
1395 NULL, /* afs_linux_rmdir */
1396 NULL, /* afs_linux_mknod */
1397 NULL, /* afs_linux_rename */
1398 NULL, /* afs_linux_readlink */
1399 NULL, /* afs_linux_follow_link */
1401 NULL, /* afs_linux_writepage */
1402 NULL, /* afs_linux_bmap */
1403 NULL, /* afs_linux_truncate */
1404 afs_linux_permission,
1405 NULL, /* afs_linux_smap */
1406 afs_linux_updatepage,
1407 afs_linux_revalidate,
1410 struct inode_operations *afs_ops = &afs_iops;
1413 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1414 * by what sort of operation is allowed.....
1416 #if defined(AFS_LINUX24_ENV)
1417 struct inode_operations afs_dir_iops = {
1418 create: afs_linux_create,
1419 lookup: afs_linux_lookup,
1420 link: afs_linux_link,
1421 unlink: afs_linux_unlink,
1422 symlink: afs_linux_symlink,
1423 mkdir: afs_linux_mkdir,
1424 rmdir: afs_linux_rmdir,
1425 rename: afs_linux_rename,
1426 revalidate: afs_linux_revalidate,
1427 setattr: afs_notify_change,
1428 permission: afs_linux_permission,
1431 struct inode_operations afs_dir_iops = {
1432 &afs_dir_fops, /* file operations for directories */
1440 NULL, /* afs_linux_mknod */
1442 NULL, /* afs_linux_readlink */
1443 NULL, /* afs_linux_follow_link */
1444 NULL, /* afs_linux_readpage */
1445 NULL, /* afs_linux_writepage */
1446 NULL, /* afs_linux_bmap */
1447 NULL, /* afs_linux_truncate */
1448 afs_linux_permission,
1449 NULL, /* afs_linux_smap */
1450 NULL, /* afs_linux_updatepage */
1451 afs_linux_revalidate,
1455 /* We really need a separate symlink set of ops, since do_follow_link()
1456 * determines if it _is_ a link by checking if the follow_link op is set.
1458 #if defined(AFS_LINUX24_ENV)
1459 static int afs_symlink_filler(struct file *file, struct page *page)
1461 struct inode *ip = (struct inode *) page->mapping->host;
1462 char *p = (char *)kmap(page);
1467 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1471 p[code] = '\0'; /* null terminate? */
1475 SetPageUptodate(page);
1490 struct address_space_operations afs_symlink_aops = {
1491 readpage: afs_symlink_filler
1494 struct inode_operations afs_symlink_iops = {
1495 readlink: page_readlink,
1496 follow_link: page_follow_link,
1497 setattr: afs_notify_change,
1500 struct inode_operations afs_symlink_iops = {
1501 NULL, /* file operations */
1509 NULL, /* afs_linux_mknod */
1512 afs_linux_follow_link,
1513 NULL, /* readpage */
1514 NULL, /* afs_linux_writepage */
1515 NULL, /* afs_linux_bmap */
1516 NULL, /* afs_linux_truncate */
1517 afs_linux_permission, /* tho the code appears to indicate not used? */
1518 NULL, /* afs_linux_smap */
1519 NULL, /* updatepage */
1520 afs_linux_revalidate, /* tho the code appears to indicate not used? */