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 "../afs/param.h"
24 #include "../afs/sysincludes.h"
25 #include "../afs/afsincludes.h"
26 #include "../afs/afs_stats.h"
28 #include "../h/pagemap.h"
29 #if defined(AFS_LINUX24_ENV)
30 #include "../h/smp_lock.h"
34 #define pageoff(pp) pgoff2loff((pp)->index)
36 #define pageoff(pp) pp->offset
39 extern struct vcache *afs_globalVp;
41 extern struct dentry_operations *afs_dops;
42 #if defined(AFS_LINUX24_ENV)
43 extern struct inode_operations afs_file_iops;
44 extern struct address_space_operations afs_file_aops;
45 struct address_space_operations afs_symlink_aops;
47 extern struct inode_operations afs_dir_iops;
48 extern struct inode_operations afs_symlink_iops;
52 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
55 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
59 struct vcache *vcp = (struct vcache*)fp->f_dentry->d_inode;
60 cred_t *credp = crref();
64 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
65 ICL_TYPE_INT32, (int)*offp,
66 ICL_TYPE_INT32, count,
67 ICL_TYPE_INT32, 99999);
69 /* get a validated vcache entry */
70 code = afs_InitReq(&treq, credp);
72 code = afs_VerifyVCache(vcp, &treq);
77 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
79 code = generic_file_read(fp, buf, count, offp);
83 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
84 ICL_TYPE_INT32, (int)*offp,
85 ICL_TYPE_INT32, count,
86 ICL_TYPE_INT32, code);
94 /* Now we have integrated VM for writes as well as reads. generic_file_write
95 * also takes care of re-positioning the pointer if file is open in append
96 * mode. Call fake open/close to ensure we do writes of core dumps.
98 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
103 struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
104 struct vrequest treq;
105 cred_t *credp = crref();
109 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
110 ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
111 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
114 /* get a validated vcache entry */
115 code = (ssize_t)afs_InitReq(&treq, credp);
117 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
119 ObtainWriteLock(&vcp->lock, 529);
121 ReleaseWriteLock(&vcp->lock);
126 code = generic_file_write(fp, buf, count, offp);
130 ObtainWriteLock(&vcp->lock, 530);
131 vcp->m.Date = osi_Time(); /* set modification time */
132 afs_FakeClose(vcp, credp);
134 code2 = afs_DoPartialWrite(vcp, &treq);
135 if (code2 && code >=0)
136 code = (ssize_t) -code2;
137 ReleaseWriteLock(&vcp->lock);
139 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
140 ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
141 ICL_TYPE_INT32, code);
148 /* This is a complete rewrite of afs_readdir, since we can make use of
149 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
150 * handling and use of bulkstats will need to be reflected here as well.
152 static int afs_linux_readdir(struct file *fp,
153 void *dirbuf, filldir_t filldir)
155 extern struct DirEntry * afs_dir_GetBlob();
156 struct vcache *avc = (struct vcache*)FILE_INODE(fp);
157 struct vrequest treq;
158 register struct dcache *tdc;
166 cred_t *credp = crref();
169 AFS_STATCNT(afs_readdir);
171 code = afs_InitReq(&treq, credp);
178 /* update the cache entry */
180 code = afs_VerifyVCache(avc, &treq);
186 /* get a reference to the entire directory */
187 tdc = afs_GetDCache(avc, 0, &treq, &origOffset, &len, 1);
192 ObtainReadLock(&avc->lock);
194 * Make sure that the data in the cache is current. There are two
195 * cases we need to worry about:
196 * 1. The cache data is being fetched by another process.
197 * 2. The cache data is no longer valid
199 while ((avc->states & CStatd)
200 && (tdc->flags & DFFetching)
201 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
202 tdc->flags |= DFWaiting;
203 ReleaseReadLock(&avc->lock);
204 afs_osi_Sleep(&tdc->validPos);
205 ObtainReadLock(&avc->lock);
207 if (!(avc->states & CStatd)
208 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
209 ReleaseReadLock(&avc->lock);
214 /* Fill in until we get an error or we're done. This implementation
215 * takes an offset in units of blobs, rather than bytes.
218 offset = (int)fp->f_pos;
220 dirpos = BlobScan(&tdc->f.inode, offset);
224 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
228 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
229 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
230 len = strlen(de->name);
232 /* filldir returns -EINVAL when the buffer is full. */
233 #ifdef AFS_LINUX24_ENV
234 code = (*filldir)(dirbuf, de->name, len, offset, ino, DT_DIR);
236 code = (*filldir)(dirbuf, de->name, len, offset, ino);
241 offset = dirpos + 1 + ((len+16)>>5);
243 /* If filldir didn't fill in the last one this is still pointing to that
246 fp->f_pos = (loff_t)offset;
249 ReleaseReadLock(&avc->lock);
255 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
258 /* in afs_pioctl.c */
259 extern int afs_xioctl(struct inode *ip, struct file *fp,
260 unsigned int com, unsigned long arg);
263 /* We need to detect unmap's after close. To do that, we need our own
264 * vm_operations_struct's. And we need to set them up for both the
265 * private and shared mappings. The fun part is that these are all static
266 * so we'll have to initialize on the fly!
268 static struct vm_operations_struct afs_private_mmap_ops;
269 static int afs_private_mmap_ops_inited = 0;
270 static struct vm_operations_struct afs_shared_mmap_ops;
271 static int afs_shared_mmap_ops_inited = 0;
273 void afs_linux_vma_close(struct vm_area_struct *vmap)
281 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
286 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
287 ICL_TYPE_POINTER, vcp,
288 ICL_TYPE_INT32, vcp->mapcnt,
289 ICL_TYPE_INT32, vcp->opens,
290 ICL_TYPE_INT32, vcp->execsOrWriters);
291 ObtainWriteLock(&vcp->lock, 532);
294 ReleaseWriteLock(&vcp->lock);
297 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
298 /* only decrement the execsOrWriters flag if this is not a writable
300 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
301 vcp->execsOrWriters--;
303 vcp->states &= ~CMAPPED;
308 ReleaseWriteLock(&vcp->lock);
315 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
317 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
318 cred_t *credp = crref();
319 struct vrequest treq;
323 #if defined(AFS_LINUX24_ENV)
324 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
325 ICL_TYPE_POINTER, vmap->vm_start,
326 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
328 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
329 ICL_TYPE_POINTER, vmap->vm_start,
330 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
331 ICL_TYPE_INT32, vmap->vm_offset);
334 /* get a validated vcache entry */
335 code = afs_InitReq(&treq, credp);
337 code = afs_VerifyVCache(vcp, &treq);
343 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
346 code = generic_file_mmap(fp, vmap);
351 ObtainWriteLock(&vcp->lock,531);
352 /* Set out vma ops so we catch the close. The following test should be
353 * the same as used in generic_file_mmap.
355 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
356 if (!afs_shared_mmap_ops_inited) {
357 afs_shared_mmap_ops_inited = 1;
358 afs_shared_mmap_ops = *vmap->vm_ops;
359 afs_shared_mmap_ops.close = afs_linux_vma_close;
361 vmap->vm_ops = &afs_shared_mmap_ops;
364 if (!afs_private_mmap_ops_inited) {
365 afs_private_mmap_ops_inited = 1;
366 afs_private_mmap_ops = *vmap->vm_ops;
367 afs_private_mmap_ops.close = afs_linux_vma_close;
369 vmap->vm_ops = &afs_private_mmap_ops;
373 /* Add an open reference on the first mapping. */
374 if (vcp->mapcnt == 0) {
375 vcp->execsOrWriters++;
377 vcp->states |= CMAPPED;
379 ReleaseWriteLock(&vcp->lock);
388 int afs_linux_open(struct inode *ip, struct file *fp)
391 cred_t *credp = crref();
394 #ifdef AFS_LINUX24_ENV
397 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
398 #ifdef AFS_LINUX24_ENV
407 /* afs_Close is called from release, since release is used to handle all
408 * file closings. In addition afs_linux_flush is called from sys_close to
409 * handle flushing the data back to the server. The kicker is that we could
410 * ignore flush completely if only sys_close took it's return value from
411 * fput. See afs_linux_flush for notes on interactions between release and
414 static int afs_linux_release(struct inode *ip, struct file *fp)
417 cred_t *credp = crref();
418 struct vcache *vcp = (struct vcache*)ip;
421 #ifdef AFS_LINUX24_ENV
425 vcp->flushcnt--; /* protected by AFS global lock. */
428 code = afs_close(vcp, fp->f_flags, credp);
430 #ifdef AFS_LINUX24_ENV
439 #if defined(AFS_LINUX24_ENV)
440 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
442 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
446 struct inode *ip = FILE_INODE(fp);
447 cred_t *credp = crref();
450 #ifdef AFS_LINUX24_ENV
453 code = afs_fsync((struct vcache*)ip, credp);
454 #ifdef AFS_LINUX24_ENV
464 /* No support for async i/o */
465 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
467 /* I don't think it will, at least not as can be detected here. */
468 int afs_linux_check_media_change(kdev_t dev);
470 /* Revalidate media and file system. */
471 int afs_linux_file_revalidate(kdev_t dev);
474 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
477 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
478 cred_t *credp = crref();
481 /* Convert to a lock format afs_lockctl understands. */
482 memset((char*)&flock, 0, sizeof(flock));
483 flock.l_type = flp->fl_type;
484 flock.l_pid = flp->fl_pid;
486 flock.l_start = flp->fl_start;
487 flock.l_len = flp->fl_end - flp->fl_start;
490 code = afs_lockctl(vcp, &flock, cmd, credp);
498 * flush is called from sys_close. We could ignore it, but sys_close return
499 * code comes from flush, not release. We need to use release to keep
500 * the vcache open count correct. Note that flush is called before release
501 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
502 * races and also avoid calling afs_close twice when closing the file.
503 * If we merely checked for opens > 0 in afs_linux_release, then if an
504 * new open occurred when storing back the file, afs_linux_release would
505 * incorrectly close the file and decrement the opens count. Calling afs_close
506 * on the just flushed file is wasteful, since the background daemon will
507 * execute the code that finally decides there is nothing to do.
509 int afs_linux_flush(struct file *fp)
511 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
515 /* Only do this on the last close of the file pointer. */
516 #if defined(AFS_LINUX24_ENV)
517 if (atomic_read(&fp->f_count) > 1)
526 code = afs_close(vcp, fp->f_flags, credp);
527 vcp->flushcnt++; /* protected by AFS global lock. */
534 /* Not allowed to directly read a directory. */
535 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
542 #if defined(AFS_LINUX24_ENV)
543 struct file_operations afs_dir_fops = {
544 read: generic_read_dir,
545 readdir: afs_linux_readdir,
547 open: afs_linux_open,
548 release: afs_linux_release,
551 struct file_operations afs_dir_fops = {
552 NULL, /* afs_linux_lseek */
554 NULL, /* afs_linux_write */
556 NULL, /* afs_linux_select */
557 afs_xioctl, /* close enough to use the ported AFS one */
558 NULL, /* afs_linux_mmap */
560 NULL, /* afs_linux_flush */
563 NULL, /* afs_linux_fasync */
564 NULL, /* afs_linux_check_media_change */
565 NULL, /* afs_linux_file_revalidate */
570 #if defined(AFS_LINUX24_ENV)
571 struct file_operations afs_file_fops = {
572 read: afs_linux_read,
573 write: afs_linux_write,
575 mmap: afs_linux_mmap,
576 open: afs_linux_open,
577 flush: afs_linux_flush,
578 release: afs_linux_release,
579 fsync: afs_linux_fsync,
580 lock: afs_linux_lock,
583 struct file_operations afs_file_fops = {
584 NULL, /* afs_linux_lseek */
587 NULL, /* afs_linux_readdir */
588 NULL, /* afs_linux_select */
589 afs_xioctl, /* close enough to use the ported AFS one */
595 NULL, /* afs_linux_fasync */
596 NULL, /* afs_linux_check_media_change */
597 NULL, /* afs_linux_file_revalidate */
603 /**********************************************************************
604 * AFS Linux dentry operations
605 **********************************************************************/
607 /* afs_linux_revalidate
608 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
610 static int afs_linux_revalidate(struct dentry *dp)
614 struct vrequest treq;
615 struct vcache *vcp = (struct vcache*)dp->d_inode;
618 #ifdef AFS_LINUX24_ENV
622 /* If it's a negative dentry, then there's nothing to do. */
624 #ifdef AFS_LINUX24_ENV
631 /* Make this a fast path (no crref), since it's called so often. */
632 if (vcp->states & CStatd) {
633 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
634 check_bad_parent(dp); /* check and correct mvid */
636 #ifdef AFS_LINUX24_ENV
644 code = afs_InitReq(&treq, credp);
646 code = afs_VerifyVCache(vcp, &treq);
648 #ifdef AFS_LINUX24_ENV
657 /* Validate a dentry. Return 0 if unchanged, 1 if VFS layer should re-evaluate.
658 * In kernels 2.2.10 and above, we are passed an additional flags var which
659 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
660 * we are advised to follow the entry if it is a link or to make sure that
661 * it is a directory. But since the kernel itself checks these possibilities
662 * later on, we shouldn't have to do it until later. Perhaps in the future..
664 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
665 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
667 static int afs_linux_dentry_revalidate(struct dentry *dp)
672 struct vrequest treq;
673 struct vcache *vcp = (struct vcache*)dp->d_inode;
676 #ifdef AFS_LINUX24_ENV
680 /* If it's a negative dentry, then there's nothing to do. */
682 #ifdef AFS_LINUX24_ENV
689 /* Make this a fast path (no crref), since it's called so often. */
690 if (vcp->states & CStatd) {
691 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
692 check_bad_parent(dp); /* check and correct mvid */
694 #ifdef AFS_LINUX24_ENV
702 code = afs_InitReq(&treq, credp);
704 code = afs_VerifyVCache(vcp, &treq);
706 #ifdef AFS_LINUX24_ENV
715 /* afs_dentry_iput */
716 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
721 #if defined(AFS_LINUX24_ENV)
722 struct dentry_operations afs_dentry_operations = {
723 d_revalidate: afs_linux_dentry_revalidate,
724 d_iput: afs_dentry_iput,
726 struct dentry_operations *afs_dops = &afs_dentry_operations;
728 struct dentry_operations afs_dentry_operations = {
729 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
731 NULL, /* d_compare */
732 NULL, /* d_delete(struct dentry *) */
733 NULL, /* d_release(struct dentry *) */
734 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
736 struct dentry_operations *afs_dops = &afs_dentry_operations;
739 /**********************************************************************
740 * AFS Linux inode operations
741 **********************************************************************/
745 * Merely need to set enough of vattr to get us through the create. Note
746 * that the higher level code (open_namei) will take care of any tuncation
747 * explicitly. Exclusive open is also taken care of in open_namei.
749 * name is in kernel space at this point.
751 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
754 cred_t *credp = crref();
757 const char *name = dp->d_name.name;
761 vattr.va_mode = mode;
764 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
765 (struct vcache**)&ip, credp);
768 vattr2inode(ip, &vattr);
769 /* Reset ops if symlink or directory. */
770 #if defined(AFS_LINUX24_ENV)
771 if (S_ISREG(ip->i_mode)) {
772 ip->i_op = &afs_file_iops;
773 ip->i_fop = &afs_file_fops;
774 ip->i_data.a_ops = &afs_file_aops;
775 } else if (S_ISDIR(ip->i_mode)) {
776 ip->i_op = &afs_dir_iops;
777 ip->i_fop = &afs_dir_fops;
778 } else if (S_ISLNK(ip->i_mode)) {
779 ip->i_op = &afs_symlink_iops;
780 ip->i_data.a_ops = &afs_symlink_aops;
781 ip->i_mapping = &ip->i_data;
783 printk("afs_linux_create: FIXME\n");
785 if (S_ISDIR(ip->i_mode))
786 ip->i_op = &afs_dir_iops;
787 else if (S_ISLNK(ip->i_mode))
788 ip->i_op = &afs_symlink_iops;
792 d_instantiate(dp, ip);
800 /* afs_linux_lookup */
801 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
802 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
804 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
808 cred_t *credp = crref();
809 struct vcache *vcp=NULL;
810 const char *comp = dp->d_name.name;
812 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
815 struct inode *ip = (struct inode*)vcp;
816 /* Reset ops if symlink or directory. */
817 #if defined(AFS_LINUX24_ENV)
818 if (S_ISREG(ip->i_mode)) {
819 ip->i_op = &afs_file_iops;
820 ip->i_fop = &afs_file_fops;
821 ip->i_data.a_ops = &afs_file_aops;
822 } else if (S_ISDIR(ip->i_mode)) {
823 ip->i_op = &afs_dir_iops;
824 ip->i_fop = &afs_dir_fops;
825 } else if (S_ISLNK(ip->i_mode)) {
826 ip->i_op = &afs_symlink_iops;
827 ip->i_data.a_ops = &afs_symlink_aops;
828 ip->i_mapping = &ip->i_data;
830 printk("afs_linux_lookup: FIXME\n");
832 if (S_ISDIR(ip->i_mode))
833 ip->i_op = &afs_dir_iops;
834 else if (S_ISLNK(ip->i_mode))
835 ip->i_op = &afs_symlink_iops;
839 d_add(dp, (struct inode*)vcp);
844 /* It's ok for the file to not be found. That's noted by the caller by
845 * seeing that the dp->d_inode field is NULL.
847 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
851 return ERR_PTR(-code);
859 int afs_linux_link(struct dentry *olddp, struct inode *dip,
860 struct dentry *newdp)
863 cred_t *credp = crref();
864 const char *name = newdp->d_name.name;
865 struct inode *oldip = olddp->d_inode;
867 /* If afs_link returned the vnode, we could instantiate the
868 * dentry. Since it's not, we drop this one and do a new lookup.
873 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
880 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
883 cred_t *credp = crref();
884 const char *name = dp->d_name.name;
887 if (!list_empty(&dp->d_hash)) {
889 /* Install a definite non-existence if we're the only user. */
890 #if defined(AFS_LINUX24_ENV)
891 if (atomic_read(&dp->d_count) == 1)
893 if (dp->d_count == 1)
899 code = afs_remove((struct vcache*)dip, name, credp);
904 d_add(dp, NULL); /* means definitely does _not_ exist */
911 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
915 cred_t *credp = crref();
917 const char *name = dp->d_name.name;
919 /* If afs_symlink returned the vnode, we could instantiate the
920 * dentry. Since it's not, we drop this one and do a new lookup.
926 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
932 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
935 cred_t *credp = crref();
936 struct vcache *tvcp = NULL;
938 const char *name = dp->d_name.name;
942 vattr.va_mask = ATTR_MODE;
943 vattr.va_mode = mode;
944 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
947 tvcp->v.v_op = &afs_dir_iops;
948 #if defined(AFS_LINUX24_ENV)
949 tvcp->v.v_fop = &afs_dir_fops;
952 d_instantiate(dp, (struct inode*)tvcp);
959 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
962 cred_t *credp = crref();
963 const char *name = dp->d_name.name;
966 code = afs_rmdir((struct vcache*)dip, name, credp);
968 /* Linux likes to see ENOTDIR returned from an rmdir() syscall
969 * that failed because a directory is not empty. So, we map
970 * EEXIST to ENOTDIR on linux.
972 if (code == EEXIST) {
987 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
988 struct inode *newip, struct dentry *newdp)
991 cred_t *credp = crref();
992 const char *oldname = olddp->d_name.name;
993 const char *newname = newdp->d_name.name;
995 /* Remove old and new entries from name hash. New one will change below.
996 * While it's optimal to catch failures and re-insert newdp into hash,
997 * it's also error prone and in that case we're already dealing with error
998 * cases. Let another lookup put things right, if need be.
1000 if (!list_empty(&olddp->d_hash)) {
1003 if (!list_empty(&newdp->d_hash)) {
1007 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1012 d_move(olddp, newdp);
1019 /* afs_linux_ireadlink
1020 * Internal readlink which can return link contents to user or kernel space.
1021 * Note that the buffer is NOT supposed to be null-terminated.
1023 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1027 cred_t *credp = crref();
1031 setup_uio(&tuio, &iov, target, 0, maxlen, UIO_READ, seg);
1032 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1036 return maxlen - tuio.uio_resid;
1041 #if !defined(AFS_LINUX24_ENV)
1042 /* afs_linux_readlink
1043 * Fill target (which is in user space) with contents of symlink.
1045 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1048 struct inode *ip = dp->d_inode;
1051 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1057 /* afs_linux_follow_link
1058 * a file system dependent link following routine.
1060 struct dentry * afs_linux_follow_link(struct dentry *dp,
1061 struct dentry *basep,
1062 unsigned int follow)
1069 name = osi_Alloc(PATH_MAX+1);
1073 return ERR_PTR(-EIO);
1076 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1081 res = ERR_PTR(code);
1085 res = lookup_dentry(name, basep, follow);
1089 osi_Free(name, PATH_MAX+1);
1095 /* afs_linux_readpage
1096 * all reads come through here. A strategy-like read call.
1098 int afs_linux_readpage(struct file *fp, struct page *pp)
1101 cred_t *credp = crref();
1102 ulong address = afs_linux_page_address(pp);
1105 struct inode *ip = FILE_INODE(fp);
1106 int cnt = atomic_read(&pp->count);
1109 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1110 ICL_TYPE_POINTER, ip,
1111 ICL_TYPE_POINTER, pp,
1112 ICL_TYPE_INT32, cnt,
1113 ICL_TYPE_INT32, 99999); /* not a possible code value */
1114 atomic_add(1, &pp->count);
1115 #if defined(AFS_LINUX24_ENV)
1118 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1119 clear_bit(PG_error, &pp->flags);
1122 #if defined(AFS_LINUX24_ENV)
1123 setup_uio(&tuio, &iovec, (char*)address, pp->index << PAGE_CACHE_SHIFT,
1124 PAGESIZE, UIO_READ, AFS_UIOSYS);
1126 setup_uio(&tuio, &iovec, (char*)address, pageoff(pp), PAGESIZE,
1127 UIO_READ, AFS_UIOSYS);
1129 #if defined(AFS_LINUX24_ENV)
1132 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1133 #if defined(AFS_LINUX24_ENV)
1138 if (tuio.uio_resid) /* zero remainder of page */
1139 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1141 #if defined(AFS_LINUX24_ENV)
1143 flush_dcache_page(pp);
1145 SetPageUptodate(pp);
1147 set_bit(PG_uptodate, &pp->flags);
1151 #if defined(AFS_LINUX24_ENV)
1154 clear_bit(PG_locked, &pp->flags);
1160 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1161 ICL_TYPE_POINTER, ip,
1162 ICL_TYPE_POINTER, pp,
1163 ICL_TYPE_INT32, cnt,
1164 ICL_TYPE_INT32, code);
1169 #if defined(AFS_LINUX24_ENV)
1170 int afs_linux_writepage(struct page *pp)
1172 struct address_space *mapping = pp->mapping;
1173 struct inode *inode;
1174 unsigned long end_index;
1175 unsigned offset = PAGE_CACHE_SIZE;
1178 inode = (struct inode *) mapping->host;
1179 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1182 if (pp->index < end_index)
1184 /* things got complicated... */
1185 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1186 /* OK, are we completely out? */
1187 if (pp->index >= end_index+1 || !offset)
1191 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1193 SetPageUptodate(pp);
1196 if (status == offset)
1204 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1205 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1207 /* afs_linux_truncate
1208 * Handles discarding disk blocks if this were a device. ext2 indicates we
1209 * may need to zero partial last pages of memory mapped files.
1211 void afs_linux_truncate(struct inode *ip)
1216 /* afs_linux_permission
1217 * Check access rights - returns error if can't check or permission denied.
1219 int afs_linux_permission(struct inode *ip, int mode)
1222 cred_t *credp = crref();
1226 if (mode & MAY_EXEC) tmp |= VEXEC;
1227 if (mode & MAY_READ) tmp |= VREAD;
1228 if (mode & MAY_WRITE) tmp |= VWRITE;
1229 code = afs_access((struct vcache*)ip, tmp, credp);
1238 /* msdos sector mapping hack for memory mapping. */
1239 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1242 #if defined(AFS_LINUX24_ENV)
1243 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1244 unsigned long offset,
1247 struct vcache *vcp = (struct vcache *) ip;
1248 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1256 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1257 ICL_TYPE_POINTER, pp,
1258 ICL_TYPE_INT32, atomic_read(&pp->count),
1259 ICL_TYPE_INT32, 99999);
1260 setup_uio(&tuio, &iovec, page_addr + offset,
1261 (pp->index << PAGE_CACHE_SHIFT) + offset, count,
1262 UIO_WRITE, AFS_UIOSYS);
1264 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1268 code = code ? -code : count - tuio.uio_resid;
1269 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1270 ICL_TYPE_POINTER, pp,
1271 ICL_TYPE_INT32, atomic_read(&pp->count),
1272 ICL_TYPE_INT32, code);
1280 afs_linux_updatepage(struct file *file, struct page *page,
1281 unsigned long offset, unsigned int count)
1283 struct dentry *dentry = file->f_dentry;
1285 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1288 /* afs_linux_updatepage
1289 * What one would have thought was writepage - write dirty page to file.
1290 * Called from generic_file_write. buffer is still in user space. pagep
1291 * has been filled in with old data if we're updating less than a page.
1293 int afs_linux_updatepage(struct file *fp, struct page *pp,
1294 unsigned long offset,
1295 unsigned int count, int sync)
1297 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1298 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1304 set_bit(PG_locked, &pp->flags);
1308 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1309 ICL_TYPE_POINTER, pp,
1310 ICL_TYPE_INT32, atomic_read(&pp->count),
1311 ICL_TYPE_INT32, 99999);
1312 setup_uio(&tuio, &iovec, page_addr + offset, pageoff(pp) + offset, count,
1313 UIO_WRITE, AFS_UIOSYS);
1315 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1319 code = code ? -code : count - tuio.uio_resid;
1320 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1321 ICL_TYPE_POINTER, pp,
1322 ICL_TYPE_INT32, atomic_read(&pp->count),
1323 ICL_TYPE_INT32, code);
1328 clear_bit(PG_locked, &pp->flags);
1333 #if defined(AFS_LINUX24_ENV)
1334 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1340 status = afs_linux_updatepage(file, page, offset, to-offset);
1348 static int afs_linux_prepare_write(struct file *file, struct page *page,
1349 unsigned from, unsigned to)
1355 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1358 #if defined(AFS_LINUX24_ENV)
1359 struct inode_operations afs_file_iops = {
1360 revalidate: afs_linux_revalidate,
1361 setattr: afs_notify_change,
1362 permission: afs_linux_permission,
1364 struct address_space_operations afs_file_aops = {
1365 readpage: afs_linux_readpage,
1366 writepage: afs_linux_writepage,
1367 commit_write: afs_linux_commit_write,
1368 prepare_write: afs_linux_prepare_write,
1371 struct inode_operations *afs_ops = &afs_file_iops;
1373 struct inode_operations afs_iops = {
1374 &afs_file_fops, /* file operations */
1375 NULL, /* afs_linux_create */
1376 NULL, /* afs_linux_lookup */
1377 NULL, /* afs_linux_link */
1378 NULL, /* afs_linux_unlink */
1379 NULL, /* afs_linux_symlink */
1380 NULL, /* afs_linux_mkdir */
1381 NULL, /* afs_linux_rmdir */
1382 NULL, /* afs_linux_mknod */
1383 NULL, /* afs_linux_rename */
1384 NULL, /* afs_linux_readlink */
1385 NULL, /* afs_linux_follow_link */
1387 NULL, /* afs_linux_writepage */
1388 NULL, /* afs_linux_bmap */
1389 NULL, /* afs_linux_truncate */
1390 afs_linux_permission,
1391 NULL, /* afs_linux_smap */
1392 afs_linux_updatepage,
1393 afs_linux_revalidate,
1396 struct inode_operations *afs_ops = &afs_iops;
1399 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1400 * by what sort of operation is allowed.....
1402 #if defined(AFS_LINUX24_ENV)
1403 struct inode_operations afs_dir_iops = {
1404 create: afs_linux_create,
1405 lookup: afs_linux_lookup,
1406 link: afs_linux_link,
1407 unlink: afs_linux_unlink,
1408 symlink: afs_linux_symlink,
1409 mkdir: afs_linux_mkdir,
1410 rmdir: afs_linux_rmdir,
1411 rename: afs_linux_rename,
1412 revalidate: afs_linux_revalidate,
1413 setattr: afs_notify_change,
1414 permission: afs_linux_permission,
1417 struct inode_operations afs_dir_iops = {
1418 &afs_dir_fops, /* file operations for directories */
1426 NULL, /* afs_linux_mknod */
1428 NULL, /* afs_linux_readlink */
1429 NULL, /* afs_linux_follow_link */
1430 NULL, /* afs_linux_readpage */
1431 NULL, /* afs_linux_writepage */
1432 NULL, /* afs_linux_bmap */
1433 NULL, /* afs_linux_truncate */
1434 afs_linux_permission,
1435 NULL, /* afs_linux_smap */
1436 NULL, /* afs_linux_updatepage */
1437 afs_linux_revalidate,
1441 /* We really need a separate symlink set of ops, since do_follow_link()
1442 * determines if it _is_ a link by checking if the follow_link op is set.
1444 #if defined(AFS_LINUX24_ENV)
1445 static int afs_symlink_filler(struct file *file, struct page *page)
1447 struct inode *ip = (struct inode *) page->mapping->host;
1448 char *p = (char *)kmap(page);
1453 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1459 p[code] = '\0'; /* null terminate? */
1460 SetPageUptodate(page);
1472 struct address_space_operations afs_symlink_aops = {
1473 readpage: afs_symlink_filler
1476 struct inode_operations afs_symlink_iops = {
1477 readlink: page_readlink,
1478 follow_link: page_follow_link,
1479 setattr: afs_notify_change,
1482 struct inode_operations afs_symlink_iops = {
1483 NULL, /* file operations */
1491 NULL, /* afs_linux_mknod */
1494 afs_linux_follow_link,
1495 NULL, /* readpage */
1496 NULL, /* afs_linux_writepage */
1497 NULL, /* afs_linux_bmap */
1498 NULL, /* afs_linux_truncate */
1499 afs_linux_permission, /* tho the code appears to indicate not used? */
1500 NULL, /* afs_linux_smap */
1501 NULL, /* updatepage */
1502 afs_linux_revalidate, /* tho the code appears to indicate not used? */