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
235 unsigned int type=DT_UNKNOWN;
236 struct VenusFid afid;
239 afid.Cell=avc->fid.Cell;
240 afid.Fid.Volume=avc->fid.Fid.Volume;
241 afid.Fid.Vnode=ntohl(de->fid.vnode);
242 afid.Fid.Unique=ntohl(de->fid.vunique);
243 if ((avc->states & CForeign) == 0 &&
244 (ntohl(de->fid.vnode) & 1)) {
246 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
249 } else if (((tvc->states) & (CStatd|CTruth))) {
250 /* CTruth will be set if the object has
255 else if (vtype == VREG)
257 /* Don't do this until we're sure it can't be a mtpt */
258 /* else if (vtype == VLNK)
260 /* what other types does AFS support? */
263 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
266 code = (*filldir)(dirbuf, de->name, len, offset, ino);
271 offset = dirpos + 1 + ((len+16)>>5);
273 /* If filldir didn't fill in the last one this is still pointing to that
276 fp->f_pos = (loff_t)offset;
279 ReleaseReadLock(&avc->lock);
285 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
288 /* in afs_pioctl.c */
289 extern int afs_xioctl(struct inode *ip, struct file *fp,
290 unsigned int com, unsigned long arg);
293 /* We need to detect unmap's after close. To do that, we need our own
294 * vm_operations_struct's. And we need to set them up for both the
295 * private and shared mappings. The fun part is that these are all static
296 * so we'll have to initialize on the fly!
298 static struct vm_operations_struct afs_private_mmap_ops;
299 static int afs_private_mmap_ops_inited = 0;
300 static struct vm_operations_struct afs_shared_mmap_ops;
301 static int afs_shared_mmap_ops_inited = 0;
303 void afs_linux_vma_close(struct vm_area_struct *vmap)
311 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
316 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
317 ICL_TYPE_POINTER, vcp,
318 ICL_TYPE_INT32, vcp->mapcnt,
319 ICL_TYPE_INT32, vcp->opens,
320 ICL_TYPE_INT32, vcp->execsOrWriters);
321 ObtainWriteLock(&vcp->lock, 532);
324 ReleaseWriteLock(&vcp->lock);
327 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
328 /* only decrement the execsOrWriters flag if this is not a writable
330 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
331 vcp->execsOrWriters--;
333 vcp->states &= ~CMAPPED;
338 ReleaseWriteLock(&vcp->lock);
345 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
347 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
348 cred_t *credp = crref();
349 struct vrequest treq;
353 #if defined(AFS_LINUX24_ENV)
354 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
355 ICL_TYPE_POINTER, vmap->vm_start,
356 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
358 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
359 ICL_TYPE_POINTER, vmap->vm_start,
360 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
361 ICL_TYPE_INT32, vmap->vm_offset);
364 /* get a validated vcache entry */
365 code = afs_InitReq(&treq, credp);
367 code = afs_VerifyVCache(vcp, &treq);
373 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
376 code = generic_file_mmap(fp, vmap);
381 ObtainWriteLock(&vcp->lock,531);
382 /* Set out vma ops so we catch the close. The following test should be
383 * the same as used in generic_file_mmap.
385 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
386 if (!afs_shared_mmap_ops_inited) {
387 afs_shared_mmap_ops_inited = 1;
388 afs_shared_mmap_ops = *vmap->vm_ops;
389 afs_shared_mmap_ops.close = afs_linux_vma_close;
391 vmap->vm_ops = &afs_shared_mmap_ops;
394 if (!afs_private_mmap_ops_inited) {
395 afs_private_mmap_ops_inited = 1;
396 afs_private_mmap_ops = *vmap->vm_ops;
397 afs_private_mmap_ops.close = afs_linux_vma_close;
399 vmap->vm_ops = &afs_private_mmap_ops;
403 /* Add an open reference on the first mapping. */
404 if (vcp->mapcnt == 0) {
405 vcp->execsOrWriters++;
407 vcp->states |= CMAPPED;
409 ReleaseWriteLock(&vcp->lock);
418 int afs_linux_open(struct inode *ip, struct file *fp)
421 cred_t *credp = crref();
424 #ifdef AFS_LINUX24_ENV
427 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
428 #ifdef AFS_LINUX24_ENV
437 /* afs_Close is called from release, since release is used to handle all
438 * file closings. In addition afs_linux_flush is called from sys_close to
439 * handle flushing the data back to the server. The kicker is that we could
440 * ignore flush completely if only sys_close took it's return value from
441 * fput. See afs_linux_flush for notes on interactions between release and
444 static int afs_linux_release(struct inode *ip, struct file *fp)
447 cred_t *credp = crref();
448 struct vcache *vcp = (struct vcache*)ip;
451 #ifdef AFS_LINUX24_ENV
455 vcp->flushcnt--; /* protected by AFS global lock. */
458 code = afs_close(vcp, fp->f_flags, credp);
460 #ifdef AFS_LINUX24_ENV
469 #if defined(AFS_LINUX24_ENV)
470 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
472 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
476 struct inode *ip = FILE_INODE(fp);
477 cred_t *credp = crref();
480 #ifdef AFS_LINUX24_ENV
483 code = afs_fsync((struct vcache*)ip, credp);
484 #ifdef AFS_LINUX24_ENV
494 /* No support for async i/o */
495 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
497 /* I don't think it will, at least not as can be detected here. */
498 int afs_linux_check_media_change(kdev_t dev);
500 /* Revalidate media and file system. */
501 int afs_linux_file_revalidate(kdev_t dev);
504 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
507 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
508 cred_t *credp = crref();
511 /* Convert to a lock format afs_lockctl understands. */
512 memset((char*)&flock, 0, sizeof(flock));
513 flock.l_type = flp->fl_type;
514 flock.l_pid = flp->fl_pid;
516 flock.l_start = flp->fl_start;
517 flock.l_len = flp->fl_end - flp->fl_start;
520 code = afs_lockctl(vcp, &flock, cmd, credp);
528 * flush is called from sys_close. We could ignore it, but sys_close return
529 * code comes from flush, not release. We need to use release to keep
530 * the vcache open count correct. Note that flush is called before release
531 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
532 * races and also avoid calling afs_close twice when closing the file.
533 * If we merely checked for opens > 0 in afs_linux_release, then if an
534 * new open occurred when storing back the file, afs_linux_release would
535 * incorrectly close the file and decrement the opens count. Calling afs_close
536 * on the just flushed file is wasteful, since the background daemon will
537 * execute the code that finally decides there is nothing to do.
539 int afs_linux_flush(struct file *fp)
541 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
545 /* Only do this on the last close of the file pointer. */
546 #if defined(AFS_LINUX24_ENV)
547 if (atomic_read(&fp->f_count) > 1)
556 code = afs_close(vcp, fp->f_flags, credp);
557 vcp->flushcnt++; /* protected by AFS global lock. */
564 /* Not allowed to directly read a directory. */
565 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
572 #if defined(AFS_LINUX24_ENV)
573 struct file_operations afs_dir_fops = {
574 read: generic_read_dir,
575 readdir: afs_linux_readdir,
577 open: afs_linux_open,
578 release: afs_linux_release,
581 struct file_operations afs_dir_fops = {
582 NULL, /* afs_linux_lseek */
584 NULL, /* afs_linux_write */
586 NULL, /* afs_linux_select */
587 afs_xioctl, /* close enough to use the ported AFS one */
588 NULL, /* afs_linux_mmap */
590 NULL, /* afs_linux_flush */
593 NULL, /* afs_linux_fasync */
594 NULL, /* afs_linux_check_media_change */
595 NULL, /* afs_linux_file_revalidate */
600 #if defined(AFS_LINUX24_ENV)
601 struct file_operations afs_file_fops = {
602 read: afs_linux_read,
603 write: afs_linux_write,
605 mmap: afs_linux_mmap,
606 open: afs_linux_open,
607 flush: afs_linux_flush,
608 release: afs_linux_release,
609 fsync: afs_linux_fsync,
610 lock: afs_linux_lock,
613 struct file_operations afs_file_fops = {
614 NULL, /* afs_linux_lseek */
617 NULL, /* afs_linux_readdir */
618 NULL, /* afs_linux_select */
619 afs_xioctl, /* close enough to use the ported AFS one */
625 NULL, /* afs_linux_fasync */
626 NULL, /* afs_linux_check_media_change */
627 NULL, /* afs_linux_file_revalidate */
633 /**********************************************************************
634 * AFS Linux dentry operations
635 **********************************************************************/
637 /* afs_linux_revalidate
638 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
640 static int afs_linux_revalidate(struct dentry *dp)
644 struct vrequest treq;
645 struct vcache *vcp = (struct vcache*)dp->d_inode;
648 #ifdef AFS_LINUX24_ENV
652 /* Make this a fast path (no crref), since it's called so often. */
653 if (vcp->states & CStatd) {
654 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
655 check_bad_parent(dp); /* check and correct mvid */
657 #ifdef AFS_LINUX24_ENV
665 code = afs_InitReq(&treq, credp);
667 code = afs_VerifyVCache(vcp, &treq);
669 #ifdef AFS_LINUX24_ENV
678 /* Validate a dentry. Return 0 if unchanged, 1 if VFS layer should re-evaluate.
679 * In kernels 2.2.10 and above, we are passed an additional flags var which
680 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
681 * we are advised to follow the entry if it is a link or to make sure that
682 * it is a directory. But since the kernel itself checks these possibilities
683 * later on, we shouldn't have to do it until later. Perhaps in the future..
685 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
686 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
688 static int afs_linux_dentry_revalidate(struct dentry *dp)
693 struct vrequest treq;
694 struct vcache *vcp = (struct vcache*)dp->d_inode;
695 struct vcache *dvcp = (struct vcache*)dp->d_parent->d_inode;
699 #ifdef AFS_LINUX24_ENV
703 /* Make this a fast path (no crref), since it's called so often. */
704 if ((dvcp->states & CStatd)) {
705 if (dp->d_time != hgetlo(dvcp->m.DataVersion))
710 code = afs_InitReq(&treq, credp);
712 code = afs_VerifyVCache(dvcp, &treq);
716 if (dp->d_time != hgetlo(dvcp->m.DataVersion))
721 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
722 check_bad_parent(dp); /* check and correct mvid */
723 /*vcache2inode(vcp);*/
725 #ifdef AFS_LINUX24_ENV
734 #ifdef AFS_LINUX24_ENV
741 /* afs_dentry_iput */
742 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
747 #if defined(AFS_LINUX24_ENV)
748 struct dentry_operations afs_dentry_operations = {
749 d_revalidate: afs_linux_dentry_revalidate,
750 d_iput: afs_dentry_iput,
752 struct dentry_operations *afs_dops = &afs_dentry_operations;
754 struct dentry_operations afs_dentry_operations = {
755 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
757 NULL, /* d_compare */
758 NULL, /* d_delete(struct dentry *) */
759 NULL, /* d_release(struct dentry *) */
760 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
762 struct dentry_operations *afs_dops = &afs_dentry_operations;
765 /**********************************************************************
766 * AFS Linux inode operations
767 **********************************************************************/
771 * Merely need to set enough of vattr to get us through the create. Note
772 * that the higher level code (open_namei) will take care of any tuncation
773 * explicitly. Exclusive open is also taken care of in open_namei.
775 * name is in kernel space at this point.
777 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
780 cred_t *credp = crref();
783 const char *name = dp->d_name.name;
787 vattr.va_mode = mode;
790 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
791 (struct vcache**)&ip, credp);
794 vattr2inode(ip, &vattr);
795 /* Reset ops if symlink or directory. */
796 #if defined(AFS_LINUX24_ENV)
797 if (S_ISREG(ip->i_mode)) {
798 ip->i_op = &afs_file_iops;
799 ip->i_fop = &afs_file_fops;
800 ip->i_data.a_ops = &afs_file_aops;
801 } else if (S_ISDIR(ip->i_mode)) {
802 ip->i_op = &afs_dir_iops;
803 ip->i_fop = &afs_dir_fops;
804 } else if (S_ISLNK(ip->i_mode)) {
805 ip->i_op = &afs_symlink_iops;
806 ip->i_data.a_ops = &afs_symlink_aops;
807 ip->i_mapping = &ip->i_data;
809 printk("afs_linux_create: FIXME\n");
811 if (S_ISDIR(ip->i_mode))
812 ip->i_op = &afs_dir_iops;
813 else if (S_ISLNK(ip->i_mode))
814 ip->i_op = &afs_symlink_iops;
818 /* This DV is probably wrong, unfortunately, Perhaps we should
819 VerifyVCache the directory */
820 dp->d_time=hgetlo(((struct vcache *)dip)->m.DataVersion);
821 d_instantiate(dp, ip);
829 /* afs_linux_lookup */
830 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
831 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
833 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
837 cred_t *credp = crref();
838 struct vcache *vcp=NULL;
839 const char *comp = dp->d_name.name;
841 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
844 struct inode *ip = (struct inode*)vcp;
845 /* Reset ops if symlink or directory. */
846 #if defined(AFS_LINUX24_ENV)
847 if (S_ISREG(ip->i_mode)) {
848 ip->i_op = &afs_file_iops;
849 ip->i_fop = &afs_file_fops;
850 ip->i_data.a_ops = &afs_file_aops;
851 } else if (S_ISDIR(ip->i_mode)) {
852 ip->i_op = &afs_dir_iops;
853 ip->i_fop = &afs_dir_fops;
854 } else if (S_ISLNK(ip->i_mode)) {
855 ip->i_op = &afs_symlink_iops;
856 ip->i_data.a_ops = &afs_symlink_aops;
857 ip->i_mapping = &ip->i_data;
859 printk("afs_linux_lookup: FIXME\n");
861 if (S_ISDIR(ip->i_mode))
862 ip->i_op = &afs_dir_iops;
863 else if (S_ISLNK(ip->i_mode))
864 ip->i_op = &afs_symlink_iops;
867 /* directory ought to be stat'd here.... */
868 dp->d_time=hgetlo(((struct vcache *)dip)->m.DataVersion);
870 d_add(dp, (struct inode*)vcp);
875 /* It's ok for the file to not be found. That's noted by the caller by
876 * seeing that the dp->d_inode field is NULL.
878 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
882 return ERR_PTR(-code);
890 int afs_linux_link(struct dentry *olddp, struct inode *dip,
891 struct dentry *newdp)
894 cred_t *credp = crref();
895 const char *name = newdp->d_name.name;
896 struct inode *oldip = olddp->d_inode;
898 /* If afs_link returned the vnode, we could instantiate the
899 * dentry. Since it's not, we drop this one and do a new lookup.
904 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
911 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
914 cred_t *credp = crref();
915 const char *name = dp->d_name.name;
918 if (!list_empty(&dp->d_hash)) {
920 /* Install a definite non-existence if we're the only user. */
921 #if defined(AFS_LINUX24_ENV)
922 if (atomic_read(&dp->d_count) == 1)
924 if (dp->d_count == 1)
930 code = afs_remove((struct vcache*)dip, name, credp);
935 /* This DV is probably wrong, unfortunately, Perhaps we should
936 VerifyVCache the directory */
937 dp->d_time=hgetlo(((struct vcache *)dip)->m.DataVersion);
938 d_add(dp, NULL); /* means definitely does _not_ exist */
946 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
950 cred_t *credp = crref();
952 const char *name = dp->d_name.name;
954 /* If afs_symlink returned the vnode, we could instantiate the
955 * dentry. Since it's not, we drop this one and do a new lookup.
961 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
967 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
970 cred_t *credp = crref();
971 struct vcache *tvcp = NULL;
973 const char *name = dp->d_name.name;
977 vattr.va_mask = ATTR_MODE;
978 vattr.va_mode = mode;
979 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
982 tvcp->v.v_op = &afs_dir_iops;
983 #if defined(AFS_LINUX24_ENV)
984 tvcp->v.v_fop = &afs_dir_fops;
987 /* This DV is probably wrong, unfortunately, Perhaps we should
988 VerifyVCache the directory */
989 dp->d_time=hgetlo(((struct vcache *)dip)->m.DataVersion);
990 d_instantiate(dp, (struct inode*)tvcp);
997 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1000 cred_t *credp = crref();
1001 const char *name = dp->d_name.name;
1004 code = afs_rmdir((struct vcache*)dip, name, credp);
1006 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1007 * that failed because a directory is not empty. So, we map
1008 * EEXIST to ENOTEMPTY on linux.
1010 if (code == EEXIST) {
1025 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1026 struct inode *newip, struct dentry *newdp)
1029 cred_t *credp = crref();
1030 const char *oldname = olddp->d_name.name;
1031 const char *newname = newdp->d_name.name;
1033 /* Remove old and new entries from name hash. New one will change below.
1034 * While it's optimal to catch failures and re-insert newdp into hash,
1035 * it's also error prone and in that case we're already dealing with error
1036 * cases. Let another lookup put things right, if need be.
1038 if (!list_empty(&olddp->d_hash)) {
1041 if (!list_empty(&newdp->d_hash)) {
1045 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1050 /* update time so it doesn't expire immediately */
1051 /* This DV is probably wrong, unfortunately, Perhaps we should
1052 VerifyVCache the directory */
1053 newdp->d_time=hgetlo(((struct vcache *)newdp->d_parent->d_inode)->m.DataVersion);
1054 d_move(olddp, newdp);
1062 /* afs_linux_ireadlink
1063 * Internal readlink which can return link contents to user or kernel space.
1064 * Note that the buffer is NOT supposed to be null-terminated.
1066 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1070 cred_t *credp = crref();
1074 setup_uio(&tuio, &iov, target, 0, maxlen, UIO_READ, seg);
1075 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1079 return maxlen - tuio.uio_resid;
1084 #if !defined(AFS_LINUX24_ENV)
1085 /* afs_linux_readlink
1086 * Fill target (which is in user space) with contents of symlink.
1088 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1091 struct inode *ip = dp->d_inode;
1094 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1100 /* afs_linux_follow_link
1101 * a file system dependent link following routine.
1103 struct dentry * afs_linux_follow_link(struct dentry *dp,
1104 struct dentry *basep,
1105 unsigned int follow)
1112 name = osi_Alloc(PATH_MAX+1);
1116 return ERR_PTR(-EIO);
1119 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1124 res = ERR_PTR(code);
1128 res = lookup_dentry(name, basep, follow);
1132 osi_Free(name, PATH_MAX+1);
1138 /* afs_linux_readpage
1139 * all reads come through here. A strategy-like read call.
1141 int afs_linux_readpage(struct file *fp, struct page *pp)
1144 cred_t *credp = crref();
1145 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1147 loff_t offset = pp->index << PAGE_CACHE_SHIFT;
1149 ulong address = afs_linux_page_address(pp);
1150 loff_t offset = pageoff(pp);
1154 struct inode *ip = FILE_INODE(fp);
1155 int cnt = atomic_read(&pp->count);
1158 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1159 ICL_TYPE_POINTER, ip,
1160 ICL_TYPE_POINTER, pp,
1161 ICL_TYPE_INT32, cnt,
1162 ICL_TYPE_INT32, 99999); /* not a possible code value */
1164 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1170 atomic_add(1, &pp->count);
1171 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1172 clear_bit(PG_error, &pp->flags);
1175 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1176 UIO_READ, AFS_UIOSYS);
1177 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1178 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1183 if (tuio.uio_resid) /* zero remainder of page */
1184 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1186 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1187 flush_dcache_page(pp);
1188 SetPageUptodate(pp);
1190 set_bit(PG_uptodate, &pp->flags);
1194 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1198 clear_bit(PG_locked, &pp->flags);
1204 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1205 ICL_TYPE_POINTER, ip,
1206 ICL_TYPE_POINTER, pp,
1207 ICL_TYPE_INT32, cnt,
1208 ICL_TYPE_INT32, code);
1213 #if defined(AFS_LINUX24_ENV)
1214 int afs_linux_writepage(struct page *pp)
1216 struct address_space *mapping = pp->mapping;
1217 struct inode *inode;
1218 unsigned long end_index;
1219 unsigned offset = PAGE_CACHE_SIZE;
1222 inode = (struct inode *) mapping->host;
1223 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1226 if (pp->index < end_index)
1228 /* things got complicated... */
1229 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1230 /* OK, are we completely out? */
1231 if (pp->index >= end_index+1 || !offset)
1235 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1237 SetPageUptodate(pp);
1239 if (status == offset)
1247 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1248 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1250 /* afs_linux_truncate
1251 * Handles discarding disk blocks if this were a device. ext2 indicates we
1252 * may need to zero partial last pages of memory mapped files.
1254 void afs_linux_truncate(struct inode *ip)
1259 /* afs_linux_permission
1260 * Check access rights - returns error if can't check or permission denied.
1262 int afs_linux_permission(struct inode *ip, int mode)
1265 cred_t *credp = crref();
1269 if (mode & MAY_EXEC) tmp |= VEXEC;
1270 if (mode & MAY_READ) tmp |= VREAD;
1271 if (mode & MAY_WRITE) tmp |= VWRITE;
1272 code = afs_access((struct vcache*)ip, tmp, credp);
1281 /* msdos sector mapping hack for memory mapping. */
1282 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1285 #if defined(AFS_LINUX24_ENV)
1286 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1287 unsigned long offset,
1290 struct vcache *vcp = (struct vcache *) ip;
1299 buffer = kmap(pp) + offset;
1300 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1303 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1304 ICL_TYPE_POINTER, pp,
1305 ICL_TYPE_INT32, atomic_read(&pp->count),
1306 ICL_TYPE_INT32, 99999);
1307 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1309 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1313 code = code ? -code : count - tuio.uio_resid;
1314 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1315 ICL_TYPE_POINTER, pp,
1316 ICL_TYPE_INT32, atomic_read(&pp->count),
1317 ICL_TYPE_INT32, code);
1326 afs_linux_updatepage(struct file *file, struct page *page,
1327 unsigned long offset, unsigned int count)
1329 struct dentry *dentry = file->f_dentry;
1331 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1334 /* afs_linux_updatepage
1335 * What one would have thought was writepage - write dirty page to file.
1336 * Called from generic_file_write. buffer is still in user space. pagep
1337 * has been filled in with old data if we're updating less than a page.
1339 int afs_linux_updatepage(struct file *fp, struct page *pp,
1340 unsigned long offset,
1341 unsigned int count, int sync)
1343 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1344 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1350 set_bit(PG_locked, &pp->flags);
1354 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1355 ICL_TYPE_POINTER, pp,
1356 ICL_TYPE_INT32, atomic_read(&pp->count),
1357 ICL_TYPE_INT32, 99999);
1358 setup_uio(&tuio, &iovec, page_addr + offset, pp->offset + offset, count,
1359 UIO_WRITE, AFS_UIOSYS);
1361 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1365 code = code ? -code : count - tuio.uio_resid;
1366 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1367 ICL_TYPE_POINTER, pp,
1368 ICL_TYPE_INT32, atomic_read(&pp->count),
1369 ICL_TYPE_INT32, code);
1374 clear_bit(PG_locked, &pp->flags);
1379 #if defined(AFS_LINUX24_ENV)
1380 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1386 code = afs_linux_updatepage(file, page, offset, to-offset);
1394 static int afs_linux_prepare_write(struct file *file, struct page *page,
1395 unsigned from, unsigned to)
1401 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1404 #if defined(AFS_LINUX24_ENV)
1405 struct inode_operations afs_file_iops = {
1406 revalidate: afs_linux_revalidate,
1407 setattr: afs_notify_change,
1408 permission: afs_linux_permission,
1410 struct address_space_operations afs_file_aops = {
1411 readpage: afs_linux_readpage,
1412 writepage: afs_linux_writepage,
1413 commit_write: afs_linux_commit_write,
1414 prepare_write: afs_linux_prepare_write,
1417 struct inode_operations *afs_ops = &afs_file_iops;
1419 struct inode_operations afs_iops = {
1420 &afs_file_fops, /* file operations */
1421 NULL, /* afs_linux_create */
1422 NULL, /* afs_linux_lookup */
1423 NULL, /* afs_linux_link */
1424 NULL, /* afs_linux_unlink */
1425 NULL, /* afs_linux_symlink */
1426 NULL, /* afs_linux_mkdir */
1427 NULL, /* afs_linux_rmdir */
1428 NULL, /* afs_linux_mknod */
1429 NULL, /* afs_linux_rename */
1430 NULL, /* afs_linux_readlink */
1431 NULL, /* afs_linux_follow_link */
1433 NULL, /* afs_linux_writepage */
1434 NULL, /* afs_linux_bmap */
1435 NULL, /* afs_linux_truncate */
1436 afs_linux_permission,
1437 NULL, /* afs_linux_smap */
1438 afs_linux_updatepage,
1439 afs_linux_revalidate,
1442 struct inode_operations *afs_ops = &afs_iops;
1445 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1446 * by what sort of operation is allowed.....
1448 #if defined(AFS_LINUX24_ENV)
1449 struct inode_operations afs_dir_iops = {
1450 create: afs_linux_create,
1451 lookup: afs_linux_lookup,
1452 link: afs_linux_link,
1453 unlink: afs_linux_unlink,
1454 symlink: afs_linux_symlink,
1455 mkdir: afs_linux_mkdir,
1456 rmdir: afs_linux_rmdir,
1457 rename: afs_linux_rename,
1458 revalidate: afs_linux_revalidate,
1459 setattr: afs_notify_change,
1460 permission: afs_linux_permission,
1463 struct inode_operations afs_dir_iops = {
1464 &afs_dir_fops, /* file operations for directories */
1472 NULL, /* afs_linux_mknod */
1474 NULL, /* afs_linux_readlink */
1475 NULL, /* afs_linux_follow_link */
1476 NULL, /* afs_linux_readpage */
1477 NULL, /* afs_linux_writepage */
1478 NULL, /* afs_linux_bmap */
1479 NULL, /* afs_linux_truncate */
1480 afs_linux_permission,
1481 NULL, /* afs_linux_smap */
1482 NULL, /* afs_linux_updatepage */
1483 afs_linux_revalidate,
1487 /* We really need a separate symlink set of ops, since do_follow_link()
1488 * determines if it _is_ a link by checking if the follow_link op is set.
1490 #if defined(AFS_LINUX24_ENV)
1491 static int afs_symlink_filler(struct file *file, struct page *page)
1493 struct inode *ip = (struct inode *) page->mapping->host;
1494 char *p = (char *)kmap(page);
1499 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1503 p[code] = '\0'; /* null terminate? */
1507 SetPageUptodate(page);
1522 struct address_space_operations afs_symlink_aops = {
1523 readpage: afs_symlink_filler
1526 struct inode_operations afs_symlink_iops = {
1527 readlink: page_readlink,
1528 follow_link: page_follow_link,
1529 setattr: afs_notify_change,
1532 struct inode_operations afs_symlink_iops = {
1533 NULL, /* file operations */
1541 NULL, /* afs_linux_mknod */
1544 afs_linux_follow_link,
1545 NULL, /* readpage */
1546 NULL, /* afs_linux_writepage */
1547 NULL, /* afs_linux_bmap */
1548 NULL, /* afs_linux_truncate */
1549 afs_linux_permission, /* tho the code appears to indicate not used? */
1550 NULL, /* afs_linux_smap */
1551 NULL, /* updatepage */
1552 afs_linux_revalidate, /* tho the code appears to indicate not used? */