2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Linux specific vnodeops. Also includes the glue routines required to call
12 * AFS vnodeops. The "NOTUSED" #define is used to indicate routines and
13 * calling sequences present in an ops table that we don't actually use.
14 * They are present solely for documentation purposes.
16 * So far the only truly scary part is that Linux relies on the inode cache
17 * to be up to date. Don't you dare break a callback and expect an fstat
18 * to give you meaningful information. This appears to be fixed in the 2.1
19 * development kernels. As it is we can fix this now by intercepting the
23 #include <afsconfig.h>
24 #include "../afs/param.h"
28 #include "../afs/sysincludes.h"
29 #include "../afs/afsincludes.h"
30 #include "../afs/afs_stats.h"
32 #include "../h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "../h/smp_lock.h"
38 #define pageoff(pp) pgoff2loff((pp)->index)
40 #define pageoff(pp) pp->offset
43 extern struct vcache *afs_globalVp;
45 extern struct dentry_operations *afs_dops;
46 #if defined(AFS_LINUX24_ENV)
47 extern struct inode_operations afs_file_iops;
48 extern struct address_space_operations afs_file_aops;
49 struct address_space_operations afs_symlink_aops;
51 extern struct inode_operations afs_dir_iops;
52 extern struct inode_operations afs_symlink_iops;
56 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
59 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
63 struct vcache *vcp = (struct vcache*)fp->f_dentry->d_inode;
64 cred_t *credp = crref();
68 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
69 ICL_TYPE_OFFSET, offp,
70 ICL_TYPE_INT32, count,
71 ICL_TYPE_INT32, 99999);
73 /* get a validated vcache entry */
74 code = afs_InitReq(&treq, credp);
76 code = afs_VerifyVCache(vcp, &treq);
81 #ifdef AFS_64BIT_CLIENT
82 if (*offp + count > afs_vmMappingEnd) {
85 afs_size_t oldOffset = *offp;
88 if (*offp < afs_vmMappingEnd) {
89 /* special case of a buffer crossing the VM mapping end */
90 afs_int32 tcount = afs_vmMappingEnd - *offp;
92 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
94 code = generic_file_read(fp, buf, tcount, offp);
101 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
102 UIO_READ, AFS_UIOSYS);
103 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
104 xfered += count - tuio.uio_resid;
107 *offp += count - tuio.uio_resid;
114 #endif /* AFS_64BIT_CLIENT */
115 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
117 code = generic_file_read(fp, buf, count, offp);
119 #ifdef AFS_64BIT_CLIENT
121 #endif /* AFS_64BIT_CLIENT */
124 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
125 ICL_TYPE_OFFSET, offp,
126 ICL_TYPE_INT32, count,
127 ICL_TYPE_INT32, code);
135 /* Now we have integrated VM for writes as well as reads. generic_file_write
136 * also takes care of re-positioning the pointer if file is open in append
137 * mode. Call fake open/close to ensure we do writes of core dumps.
139 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
144 struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
145 struct vrequest treq;
146 cred_t *credp = crref();
150 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
151 ICL_TYPE_OFFSET, offp,
152 ICL_TYPE_INT32, count,
153 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
156 /* get a validated vcache entry */
157 code = (ssize_t)afs_InitReq(&treq, credp);
159 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
161 ObtainWriteLock(&vcp->lock, 529);
163 ReleaseWriteLock(&vcp->lock);
167 #ifdef AFS_64BIT_CLIENT
168 if (*offp + count > afs_vmMappingEnd) {
171 afs_size_t oldOffset = *offp;
172 afs_int32 xfered = 0;
174 if (*offp < afs_vmMappingEnd) {
175 /* special case of a buffer crossing the VM mapping end */
176 afs_int32 tcount = afs_vmMappingEnd - *offp;
179 code = generic_file_write(fp, buf, tcount, offp);
181 if (code != tcount) {
186 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
187 UIO_WRITE, AFS_UIOSYS);
188 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
189 xfered += count - tuio.uio_resid;
192 *offp += count - tuio.uio_resid;
194 /* Purge dirty chunks of file if there are too many dirty chunks.
195 * Inside the write loop, we only do this at a chunk boundary.
196 * Clean up partial chunk if necessary at end of loop.
198 if (AFS_CHUNKBASE(tuio.afsio_offset) != AFS_CHUNKBASE(oldOffset)) {
199 ObtainWriteLock(&vcp->lock,402);
200 code = afs_DoPartialWrite(vcp, &treq);
201 vcp->states |= CDirty;
202 ReleaseWriteLock(&vcp->lock);
206 ObtainWriteLock(&vcp->lock,400);
207 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
209 if (*offp > vcp->m.Length) {
210 vcp->m.Length = *offp;
212 ReleaseWriteLock(&vcp->lock);
216 #endif /* AFS_64BIT_CLIENT */
218 code = generic_file_write(fp, buf, count, offp);
220 #ifdef AFS_64BIT_CLIENT
222 #endif /* AFS_64BIT_CLIENT */
225 ObtainWriteLock(&vcp->lock, 530);
226 vcp->m.Date = osi_Time(); /* set modification time */
227 afs_FakeClose(vcp, credp);
229 code2 = afs_DoPartialWrite(vcp, &treq);
230 if (code2 && code >=0)
231 code = (ssize_t) -code2;
232 ReleaseWriteLock(&vcp->lock);
234 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
235 ICL_TYPE_OFFSET, offp,
236 ICL_TYPE_INT32, count,
237 ICL_TYPE_INT32, code);
244 /* This is a complete rewrite of afs_readdir, since we can make use of
245 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
246 * handling and use of bulkstats will need to be reflected here as well.
248 static int afs_linux_readdir(struct file *fp,
249 void *dirbuf, filldir_t filldir)
251 extern struct DirEntry * afs_dir_GetBlob();
252 struct vcache *avc = (struct vcache*)FILE_INODE(fp);
253 struct vrequest treq;
254 register struct dcache *tdc;
261 afs_size_t origOffset, tlen;
262 cred_t *credp = crref();
265 AFS_STATCNT(afs_readdir);
267 code = afs_InitReq(&treq, credp);
274 /* update the cache entry */
276 code = afs_VerifyVCache(avc, &treq);
282 /* get a reference to the entire directory */
283 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
289 ObtainReadLock(&avc->lock);
290 ObtainReadLock(&tdc->lock);
292 * Make sure that the data in the cache is current. There are two
293 * cases we need to worry about:
294 * 1. The cache data is being fetched by another process.
295 * 2. The cache data is no longer valid
297 while ((avc->states & CStatd)
298 && (tdc->dflags & DFFetching)
299 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
300 ReleaseReadLock(&tdc->lock);
301 ReleaseReadLock(&avc->lock);
302 afs_osi_Sleep(&tdc->validPos);
303 ObtainReadLock(&avc->lock);
304 ObtainReadLock(&tdc->lock);
306 if (!(avc->states & CStatd)
307 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
308 ReleaseReadLock(&tdc->lock);
309 ReleaseReadLock(&avc->lock);
314 /* Fill in until we get an error or we're done. This implementation
315 * takes an offset in units of blobs, rather than bytes.
318 offset = (int)fp->f_pos;
320 dirpos = BlobScan(&tdc->f.inode, offset);
324 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
328 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
329 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
330 len = strlen(de->name);
332 /* filldir returns -EINVAL when the buffer is full. */
333 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
335 unsigned int type=DT_UNKNOWN;
336 struct VenusFid afid;
339 afid.Cell=avc->fid.Cell;
340 afid.Fid.Volume=avc->fid.Fid.Volume;
341 afid.Fid.Vnode=ntohl(de->fid.vnode);
342 afid.Fid.Unique=ntohl(de->fid.vunique);
343 if ((avc->states & CForeign) == 0 &&
344 (ntohl(de->fid.vnode) & 1)) {
345 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
348 } else if (((tvc->states) & (CStatd|CTruth))) {
349 /* CTruth will be set if the object has
354 else if (vtype == VREG)
356 /* Don't do this until we're sure it can't be a mtpt */
357 /* else if (vtype == VLNK)
359 /* what other types does AFS support? */
361 /* clean up from afs_FindVCache */
362 afs_PutVCache(tvc, WRITE_LOCK);
364 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
367 code = (*filldir)(dirbuf, de->name, len, offset, ino);
372 offset = dirpos + 1 + ((len+16)>>5);
374 /* If filldir didn't fill in the last one this is still pointing to that
377 fp->f_pos = (loff_t)offset;
379 ReleaseReadLock(&tdc->lock);
381 ReleaseReadLock(&avc->lock);
387 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
390 /* in afs_pioctl.c */
391 extern int afs_xioctl(struct inode *ip, struct file *fp,
392 unsigned int com, unsigned long arg);
395 /* We need to detect unmap's after close. To do that, we need our own
396 * vm_operations_struct's. And we need to set them up for both the
397 * private and shared mappings. The fun part is that these are all static
398 * so we'll have to initialize on the fly!
400 static struct vm_operations_struct afs_private_mmap_ops;
401 static int afs_private_mmap_ops_inited = 0;
402 static struct vm_operations_struct afs_shared_mmap_ops;
403 static int afs_shared_mmap_ops_inited = 0;
405 void afs_linux_vma_close(struct vm_area_struct *vmap)
413 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
418 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
419 ICL_TYPE_POINTER, vcp,
420 ICL_TYPE_INT32, vcp->mapcnt,
421 ICL_TYPE_INT32, vcp->opens,
422 ICL_TYPE_INT32, vcp->execsOrWriters);
423 ObtainWriteLock(&vcp->lock, 532);
426 ReleaseWriteLock(&vcp->lock);
429 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
430 /* only decrement the execsOrWriters flag if this is not a writable
432 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
433 vcp->execsOrWriters--;
435 vcp->states &= ~CMAPPED;
440 ReleaseWriteLock(&vcp->lock);
447 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
449 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
450 cred_t *credp = crref();
451 struct vrequest treq;
455 #if defined(AFS_LINUX24_ENV)
456 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
457 ICL_TYPE_POINTER, vmap->vm_start,
458 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
460 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
461 ICL_TYPE_POINTER, vmap->vm_start,
462 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
463 ICL_TYPE_INT32, vmap->vm_offset);
466 /* get a validated vcache entry */
467 code = afs_InitReq(&treq, credp);
469 code = afs_VerifyVCache(vcp, &treq);
475 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
478 code = generic_file_mmap(fp, vmap);
483 ObtainWriteLock(&vcp->lock,531);
484 /* Set out vma ops so we catch the close. The following test should be
485 * the same as used in generic_file_mmap.
487 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
488 if (!afs_shared_mmap_ops_inited) {
489 afs_shared_mmap_ops_inited = 1;
490 afs_shared_mmap_ops = *vmap->vm_ops;
491 afs_shared_mmap_ops.close = afs_linux_vma_close;
493 vmap->vm_ops = &afs_shared_mmap_ops;
496 if (!afs_private_mmap_ops_inited) {
497 afs_private_mmap_ops_inited = 1;
498 afs_private_mmap_ops = *vmap->vm_ops;
499 afs_private_mmap_ops.close = afs_linux_vma_close;
501 vmap->vm_ops = &afs_private_mmap_ops;
505 /* Add an open reference on the first mapping. */
506 if (vcp->mapcnt == 0) {
507 vcp->execsOrWriters++;
509 vcp->states |= CMAPPED;
511 ReleaseWriteLock(&vcp->lock);
520 int afs_linux_open(struct inode *ip, struct file *fp)
523 cred_t *credp = crref();
526 #ifdef AFS_LINUX24_ENV
529 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
530 #ifdef AFS_LINUX24_ENV
539 /* afs_Close is called from release, since release is used to handle all
540 * file closings. In addition afs_linux_flush is called from sys_close to
541 * handle flushing the data back to the server. The kicker is that we could
542 * ignore flush completely if only sys_close took it's return value from
543 * fput. See afs_linux_flush for notes on interactions between release and
546 static int afs_linux_release(struct inode *ip, struct file *fp)
549 cred_t *credp = crref();
550 struct vcache *vcp = (struct vcache*)ip;
553 #ifdef AFS_LINUX24_ENV
557 vcp->flushcnt--; /* protected by AFS global lock. */
560 code = afs_close(vcp, fp->f_flags, credp);
562 #ifdef AFS_LINUX24_ENV
571 #if defined(AFS_LINUX24_ENV)
572 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
574 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
578 struct inode *ip = FILE_INODE(fp);
579 cred_t *credp = crref();
582 #ifdef AFS_LINUX24_ENV
585 code = afs_fsync((struct vcache*)ip, credp);
586 #ifdef AFS_LINUX24_ENV
596 /* No support for async i/o */
597 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
599 /* I don't think it will, at least not as can be detected here. */
600 int afs_linux_check_media_change(kdev_t dev);
602 /* Revalidate media and file system. */
603 int afs_linux_file_revalidate(kdev_t dev);
606 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
609 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
610 cred_t *credp = crref();
611 #ifdef AFS_LINUX24_ENV
612 struct flock64 flock;
617 /* Convert to a lock format afs_lockctl understands. */
618 memset((char*)&flock, 0, sizeof(flock));
619 flock.l_type = flp->fl_type;
620 flock.l_pid = flp->fl_pid;
622 flock.l_start = flp->fl_start;
623 flock.l_len = flp->fl_end - flp->fl_start;
626 code = afs_lockctl(vcp, &flock, cmd, credp);
634 * flush is called from sys_close. We could ignore it, but sys_close return
635 * code comes from flush, not release. We need to use release to keep
636 * the vcache open count correct. Note that flush is called before release
637 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
638 * races and also avoid calling afs_close twice when closing the file.
639 * If we merely checked for opens > 0 in afs_linux_release, then if an
640 * new open occurred when storing back the file, afs_linux_release would
641 * incorrectly close the file and decrement the opens count. Calling afs_close
642 * on the just flushed file is wasteful, since the background daemon will
643 * execute the code that finally decides there is nothing to do.
645 int afs_linux_flush(struct file *fp)
647 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
651 /* Only do this on the last close of the file pointer. */
652 #if defined(AFS_LINUX24_ENV)
653 if (atomic_read(&fp->f_count) > 1)
662 code = afs_close(vcp, fp->f_flags, credp);
663 vcp->flushcnt++; /* protected by AFS global lock. */
670 /* Not allowed to directly read a directory. */
671 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
678 #if defined(AFS_LINUX24_ENV)
679 struct file_operations afs_dir_fops = {
680 read: generic_read_dir,
681 readdir: afs_linux_readdir,
683 open: afs_linux_open,
684 release: afs_linux_release,
687 struct file_operations afs_dir_fops = {
688 NULL, /* afs_linux_lseek */
690 NULL, /* afs_linux_write */
692 NULL, /* afs_linux_select */
693 afs_xioctl, /* close enough to use the ported AFS one */
694 NULL, /* afs_linux_mmap */
696 NULL, /* afs_linux_flush */
699 NULL, /* afs_linux_fasync */
700 NULL, /* afs_linux_check_media_change */
701 NULL, /* afs_linux_file_revalidate */
706 #if defined(AFS_LINUX24_ENV)
707 struct file_operations afs_file_fops = {
708 read: afs_linux_read,
709 write: afs_linux_write,
711 mmap: afs_linux_mmap,
712 open: afs_linux_open,
713 flush: afs_linux_flush,
714 release: afs_linux_release,
715 fsync: afs_linux_fsync,
716 lock: afs_linux_lock,
719 struct file_operations afs_file_fops = {
720 NULL, /* afs_linux_lseek */
723 NULL, /* afs_linux_readdir */
724 NULL, /* afs_linux_select */
725 afs_xioctl, /* close enough to use the ported AFS one */
731 NULL, /* afs_linux_fasync */
732 NULL, /* afs_linux_check_media_change */
733 NULL, /* afs_linux_file_revalidate */
739 /**********************************************************************
740 * AFS Linux dentry operations
741 **********************************************************************/
743 /* afs_linux_revalidate
744 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
746 static int afs_linux_revalidate(struct dentry *dp)
750 struct vrequest treq;
751 struct vcache *vcp = (struct vcache*)dp->d_inode;
754 #ifdef AFS_LINUX24_ENV
758 /* Make this a fast path (no crref), since it's called so often. */
759 if (vcp->states & CStatd) {
760 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
761 check_bad_parent(dp); /* check and correct mvid */
763 #ifdef AFS_LINUX24_ENV
771 code = afs_InitReq(&treq, credp);
773 code = afs_VerifyVCache(vcp, &treq);
775 #ifdef AFS_LINUX24_ENV
784 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
785 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
787 static int afs_linux_dentry_revalidate(struct dentry *dp)
792 struct vrequest treq;
793 struct inode *ip = (struct inode *)dp->d_inode;
795 unsigned long timeout = 3*HZ; /* 3 seconds */
797 if (!(flags & LOOKUP_CONTINUE)) {
798 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
804 if (time_after(jiffies, dp->d_time + timeout))
814 /* afs_dentry_iput */
815 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
820 #if defined(AFS_LINUX24_ENV)
821 struct dentry_operations afs_dentry_operations = {
822 d_revalidate: afs_linux_dentry_revalidate,
823 d_iput: afs_dentry_iput,
825 struct dentry_operations *afs_dops = &afs_dentry_operations;
827 struct dentry_operations afs_dentry_operations = {
828 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
830 NULL, /* d_compare */
831 NULL, /* d_delete(struct dentry *) */
832 NULL, /* d_release(struct dentry *) */
833 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
835 struct dentry_operations *afs_dops = &afs_dentry_operations;
838 /**********************************************************************
839 * AFS Linux inode operations
840 **********************************************************************/
844 * Merely need to set enough of vattr to get us through the create. Note
845 * that the higher level code (open_namei) will take care of any tuncation
846 * explicitly. Exclusive open is also taken care of in open_namei.
848 * name is in kernel space at this point.
850 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
853 cred_t *credp = crref();
856 const char *name = dp->d_name.name;
860 vattr.va_mode = mode;
863 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
864 (struct vcache**)&ip, credp);
867 vattr2inode(ip, &vattr);
868 /* Reset ops if symlink or directory. */
869 #if defined(AFS_LINUX24_ENV)
870 if (S_ISREG(ip->i_mode)) {
871 ip->i_op = &afs_file_iops;
872 ip->i_fop = &afs_file_fops;
873 ip->i_data.a_ops = &afs_file_aops;
874 } else if (S_ISDIR(ip->i_mode)) {
875 ip->i_op = &afs_dir_iops;
876 ip->i_fop = &afs_dir_fops;
877 } else if (S_ISLNK(ip->i_mode)) {
878 ip->i_op = &afs_symlink_iops;
879 ip->i_data.a_ops = &afs_symlink_aops;
880 ip->i_mapping = &ip->i_data;
882 printk("afs_linux_create: FIXME\n");
884 if (S_ISDIR(ip->i_mode))
885 ip->i_op = &afs_dir_iops;
886 else if (S_ISLNK(ip->i_mode))
887 ip->i_op = &afs_symlink_iops;
891 dp->d_time = jiffies;
892 d_instantiate(dp, ip);
900 /* afs_linux_lookup */
901 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
902 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
904 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
908 cred_t *credp = crref();
909 struct vcache *vcp=NULL;
910 const char *comp = dp->d_name.name;
912 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
915 struct inode *ip = (struct inode*)vcp;
916 /* Reset ops if symlink or directory. */
917 #if defined(AFS_LINUX24_ENV)
918 if (S_ISREG(ip->i_mode)) {
919 ip->i_op = &afs_file_iops;
920 ip->i_fop = &afs_file_fops;
921 ip->i_data.a_ops = &afs_file_aops;
922 } else if (S_ISDIR(ip->i_mode)) {
923 ip->i_op = &afs_dir_iops;
924 ip->i_fop = &afs_dir_fops;
925 } else if (S_ISLNK(ip->i_mode)) {
926 ip->i_op = &afs_symlink_iops;
927 ip->i_data.a_ops = &afs_symlink_aops;
928 ip->i_mapping = &ip->i_data;
930 printk("afs_linux_lookup: FIXME\n");
932 if (S_ISDIR(ip->i_mode))
933 ip->i_op = &afs_dir_iops;
934 else if (S_ISLNK(ip->i_mode))
935 ip->i_op = &afs_symlink_iops;
938 dp->d_time = jiffies;
940 d_add(dp, (struct inode*)vcp);
945 /* It's ok for the file to not be found. That's noted by the caller by
946 * seeing that the dp->d_inode field is NULL.
948 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
952 return ERR_PTR(-code);
960 int afs_linux_link(struct dentry *olddp, struct inode *dip,
961 struct dentry *newdp)
964 cred_t *credp = crref();
965 const char *name = newdp->d_name.name;
966 struct inode *oldip = olddp->d_inode;
968 /* If afs_link returned the vnode, we could instantiate the
969 * dentry. Since it's not, we drop this one and do a new lookup.
974 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
981 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
984 cred_t *credp = crref();
985 const char *name = dp->d_name.name;
988 if (!list_empty(&dp->d_hash)) {
990 /* Install a definite non-existence if we're the only user. */
991 #if defined(AFS_LINUX24_ENV)
992 if (atomic_read(&dp->d_count) == 1)
994 if (dp->d_count == 1)
1000 code = afs_remove((struct vcache*)dip, name, credp);
1005 dp->d_time = jiffies;
1006 d_add(dp, NULL); /* means definitely does _not_ exist */
1014 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1018 cred_t *credp = crref();
1020 const char *name = dp->d_name.name;
1022 /* If afs_symlink returned the vnode, we could instantiate the
1023 * dentry. Since it's not, we drop this one and do a new lookup.
1029 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
1035 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1038 cred_t *credp = crref();
1039 struct vcache *tvcp = NULL;
1041 const char *name = dp->d_name.name;
1045 vattr.va_mask = ATTR_MODE;
1046 vattr.va_mode = mode;
1047 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
1050 tvcp->v.v_op = &afs_dir_iops;
1051 #if defined(AFS_LINUX24_ENV)
1052 tvcp->v.v_fop = &afs_dir_fops;
1054 dp->d_op = afs_dops;
1055 dp->d_time = jiffies;
1056 d_instantiate(dp, (struct inode*)tvcp);
1063 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1066 cred_t *credp = crref();
1067 const char *name = dp->d_name.name;
1070 code = afs_rmdir((struct vcache*)dip, name, credp);
1072 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1073 * that failed because a directory is not empty. So, we map
1074 * EEXIST to ENOTEMPTY on linux.
1076 if (code == EEXIST) {
1091 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1092 struct inode *newip, struct dentry *newdp)
1095 cred_t *credp = crref();
1096 const char *oldname = olddp->d_name.name;
1097 const char *newname = newdp->d_name.name;
1099 /* Remove old and new entries from name hash. New one will change below.
1100 * While it's optimal to catch failures and re-insert newdp into hash,
1101 * it's also error prone and in that case we're already dealing with error
1102 * cases. Let another lookup put things right, if need be.
1104 if (!list_empty(&olddp->d_hash)) {
1107 if (!list_empty(&newdp->d_hash)) {
1111 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1116 /* update time so it doesn't expire immediately */
1117 newdp->d_time = jiffies;
1118 d_move(olddp, newdp);
1126 /* afs_linux_ireadlink
1127 * Internal readlink which can return link contents to user or kernel space.
1128 * Note that the buffer is NOT supposed to be null-terminated.
1130 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1134 cred_t *credp = crref();
1138 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1139 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1143 return maxlen - tuio.uio_resid;
1148 #if !defined(AFS_LINUX24_ENV)
1149 /* afs_linux_readlink
1150 * Fill target (which is in user space) with contents of symlink.
1152 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1155 struct inode *ip = dp->d_inode;
1158 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1164 /* afs_linux_follow_link
1165 * a file system dependent link following routine.
1167 struct dentry * afs_linux_follow_link(struct dentry *dp,
1168 struct dentry *basep,
1169 unsigned int follow)
1177 name = osi_Alloc(PATH_MAX+1);
1181 return ERR_PTR(-EIO);
1184 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1189 res = ERR_PTR(code);
1193 res = lookup_dentry(name, basep, follow);
1197 osi_Free(name, PATH_MAX+1);
1203 /* afs_linux_readpage
1204 * all reads come through here. A strategy-like read call.
1206 int afs_linux_readpage(struct file *fp, struct page *pp)
1209 cred_t *credp = crref();
1210 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1212 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1214 ulong address = afs_linux_page_address(pp);
1215 afs_offs_t offset = pageoff(pp);
1219 struct inode *ip = FILE_INODE(fp);
1220 int cnt = atomic_read(&pp->count);
1223 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1224 ICL_TYPE_POINTER, ip,
1225 ICL_TYPE_POINTER, pp,
1226 ICL_TYPE_INT32, cnt,
1227 ICL_TYPE_INT32, 99999); /* not a possible code value */
1228 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1234 atomic_add(1, &pp->count);
1235 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1236 clear_bit(PG_error, &pp->flags);
1239 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1240 UIO_READ, AFS_UIOSYS);
1241 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1242 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1247 if (tuio.uio_resid) /* zero remainder of page */
1248 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1250 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1251 flush_dcache_page(pp);
1252 SetPageUptodate(pp);
1254 set_bit(PG_uptodate, &pp->flags);
1258 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1262 clear_bit(PG_locked, &pp->flags);
1268 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1269 ICL_TYPE_POINTER, ip,
1270 ICL_TYPE_POINTER, pp,
1271 ICL_TYPE_INT32, cnt,
1272 ICL_TYPE_INT32, code);
1277 #if defined(AFS_LINUX24_ENV)
1278 int afs_linux_writepage(struct page *pp)
1280 struct address_space *mapping = pp->mapping;
1281 struct inode *inode;
1282 unsigned long end_index;
1283 unsigned offset = PAGE_CACHE_SIZE;
1286 inode = (struct inode *) mapping->host;
1287 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1290 if (pp->index < end_index)
1292 /* things got complicated... */
1293 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1294 /* OK, are we completely out? */
1295 if (pp->index >= end_index+1 || !offset)
1299 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1301 SetPageUptodate(pp);
1303 if (status == offset)
1311 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1312 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1314 /* afs_linux_truncate
1315 * Handles discarding disk blocks if this were a device. ext2 indicates we
1316 * may need to zero partial last pages of memory mapped files.
1318 void afs_linux_truncate(struct inode *ip)
1323 /* afs_linux_permission
1324 * Check access rights - returns error if can't check or permission denied.
1326 int afs_linux_permission(struct inode *ip, int mode)
1329 cred_t *credp = crref();
1333 if (mode & MAY_EXEC) tmp |= VEXEC;
1334 if (mode & MAY_READ) tmp |= VREAD;
1335 if (mode & MAY_WRITE) tmp |= VWRITE;
1336 code = afs_access((struct vcache*)ip, tmp, credp);
1345 /* msdos sector mapping hack for memory mapping. */
1346 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1349 #if defined(AFS_LINUX24_ENV)
1350 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1351 unsigned long offset,
1354 struct vcache *vcp = (struct vcache *) ip;
1363 buffer = kmap(pp) + offset;
1364 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1367 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1368 ICL_TYPE_POINTER, pp,
1369 ICL_TYPE_INT32, atomic_read(&pp->count),
1370 ICL_TYPE_INT32, 99999);
1371 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1373 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1377 code = code ? -code : count - tuio.uio_resid;
1378 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1379 ICL_TYPE_POINTER, pp,
1380 ICL_TYPE_INT32, atomic_read(&pp->count),
1381 ICL_TYPE_INT32, code);
1390 afs_linux_updatepage(struct file *file, struct page *page,
1391 unsigned long offset, unsigned int count)
1393 struct dentry *dentry = file->f_dentry;
1395 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1398 /* afs_linux_updatepage
1399 * What one would have thought was writepage - write dirty page to file.
1400 * Called from generic_file_write. buffer is still in user space. pagep
1401 * has been filled in with old data if we're updating less than a page.
1403 int afs_linux_updatepage(struct file *fp, struct page *pp,
1404 unsigned long offset,
1405 unsigned int count, int sync)
1407 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1408 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1414 set_bit(PG_locked, &pp->flags);
1418 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1419 ICL_TYPE_POINTER, pp,
1420 ICL_TYPE_INT32, atomic_read(&pp->count),
1421 ICL_TYPE_INT32, 99999);
1422 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1423 count, UIO_WRITE, AFS_UIOSYS);
1425 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1429 code = code ? -code : count - tuio.uio_resid;
1430 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1431 ICL_TYPE_POINTER, pp,
1432 ICL_TYPE_INT32, atomic_read(&pp->count),
1433 ICL_TYPE_INT32, code);
1438 clear_bit(PG_locked, &pp->flags);
1443 #if defined(AFS_LINUX24_ENV)
1444 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1450 code = afs_linux_updatepage(file, page, offset, to-offset);
1458 static int afs_linux_prepare_write(struct file *file, struct page *page,
1459 unsigned from, unsigned to)
1465 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1468 #if defined(AFS_LINUX24_ENV)
1469 struct inode_operations afs_file_iops = {
1470 revalidate: afs_linux_revalidate,
1471 setattr: afs_notify_change,
1472 permission: afs_linux_permission,
1474 struct address_space_operations afs_file_aops = {
1475 readpage: afs_linux_readpage,
1476 writepage: afs_linux_writepage,
1477 commit_write: afs_linux_commit_write,
1478 prepare_write: afs_linux_prepare_write,
1481 struct inode_operations *afs_ops = &afs_file_iops;
1483 struct inode_operations afs_iops = {
1484 &afs_file_fops, /* file operations */
1485 NULL, /* afs_linux_create */
1486 NULL, /* afs_linux_lookup */
1487 NULL, /* afs_linux_link */
1488 NULL, /* afs_linux_unlink */
1489 NULL, /* afs_linux_symlink */
1490 NULL, /* afs_linux_mkdir */
1491 NULL, /* afs_linux_rmdir */
1492 NULL, /* afs_linux_mknod */
1493 NULL, /* afs_linux_rename */
1494 NULL, /* afs_linux_readlink */
1495 NULL, /* afs_linux_follow_link */
1497 NULL, /* afs_linux_writepage */
1498 NULL, /* afs_linux_bmap */
1499 NULL, /* afs_linux_truncate */
1500 afs_linux_permission,
1501 NULL, /* afs_linux_smap */
1502 afs_linux_updatepage,
1503 afs_linux_revalidate,
1506 struct inode_operations *afs_ops = &afs_iops;
1509 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1510 * by what sort of operation is allowed.....
1512 #if defined(AFS_LINUX24_ENV)
1513 struct inode_operations afs_dir_iops = {
1514 create: afs_linux_create,
1515 lookup: afs_linux_lookup,
1516 link: afs_linux_link,
1517 unlink: afs_linux_unlink,
1518 symlink: afs_linux_symlink,
1519 mkdir: afs_linux_mkdir,
1520 rmdir: afs_linux_rmdir,
1521 rename: afs_linux_rename,
1522 revalidate: afs_linux_revalidate,
1523 setattr: afs_notify_change,
1524 permission: afs_linux_permission,
1527 struct inode_operations afs_dir_iops = {
1528 &afs_dir_fops, /* file operations for directories */
1536 NULL, /* afs_linux_mknod */
1538 NULL, /* afs_linux_readlink */
1539 NULL, /* afs_linux_follow_link */
1540 NULL, /* afs_linux_readpage */
1541 NULL, /* afs_linux_writepage */
1542 NULL, /* afs_linux_bmap */
1543 NULL, /* afs_linux_truncate */
1544 afs_linux_permission,
1545 NULL, /* afs_linux_smap */
1546 NULL, /* afs_linux_updatepage */
1547 afs_linux_revalidate,
1551 /* We really need a separate symlink set of ops, since do_follow_link()
1552 * determines if it _is_ a link by checking if the follow_link op is set.
1554 #if defined(AFS_LINUX24_ENV)
1555 static int afs_symlink_filler(struct file *file, struct page *page)
1557 struct inode *ip = (struct inode *) page->mapping->host;
1558 char *p = (char *)kmap(page);
1563 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1567 p[code] = '\0'; /* null terminate? */
1571 SetPageUptodate(page);
1586 struct address_space_operations afs_symlink_aops = {
1587 readpage: afs_symlink_filler
1590 struct inode_operations afs_symlink_iops = {
1591 readlink: page_readlink,
1592 follow_link: page_follow_link,
1593 setattr: afs_notify_change,
1596 struct inode_operations afs_symlink_iops = {
1597 NULL, /* file operations */
1605 NULL, /* afs_linux_mknod */
1608 afs_linux_follow_link,
1609 NULL, /* readpage */
1610 NULL, /* afs_linux_writepage */
1611 NULL, /* afs_linux_bmap */
1612 NULL, /* afs_linux_truncate */
1613 afs_linux_permission, /* tho the code appears to indicate not used? */
1614 NULL, /* afs_linux_smap */
1615 NULL, /* updatepage */
1616 afs_linux_revalidate, /* tho the code appears to indicate not used? */