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"
31 #include "../afs/afs_osidnlc.h"
33 #include "../h/pagemap.h"
34 #if defined(AFS_LINUX24_ENV)
35 #include "../h/smp_lock.h"
39 #define pageoff(pp) pgoff2loff((pp)->index)
41 #define pageoff(pp) pp->offset
44 extern struct vcache *afs_globalVp;
46 extern struct dentry_operations *afs_dops;
47 #if defined(AFS_LINUX24_ENV)
48 extern struct inode_operations afs_file_iops;
49 extern struct address_space_operations afs_file_aops;
50 struct address_space_operations afs_symlink_aops;
52 extern struct inode_operations afs_dir_iops;
53 extern struct inode_operations afs_symlink_iops;
57 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
60 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
64 struct vcache *vcp = (struct vcache*)fp->f_dentry->d_inode;
65 cred_t *credp = crref();
69 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
70 ICL_TYPE_OFFSET, offp,
71 ICL_TYPE_INT32, count,
72 ICL_TYPE_INT32, 99999);
74 /* get a validated vcache entry */
75 code = afs_InitReq(&treq, credp);
77 code = afs_VerifyVCache(vcp, &treq);
82 #ifdef AFS_64BIT_CLIENT
83 if (*offp + count > afs_vmMappingEnd) {
86 afs_size_t oldOffset = *offp;
89 if (*offp < afs_vmMappingEnd) {
90 /* special case of a buffer crossing the VM mapping end */
91 afs_int32 tcount = afs_vmMappingEnd - *offp;
93 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
95 code = generic_file_read(fp, buf, tcount, offp);
102 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
103 UIO_READ, AFS_UIOSYS);
104 code = afs_read(vcp, &tuio, credp, 0, 0, 0);
105 xfered += count - tuio.uio_resid;
108 *offp += count - tuio.uio_resid;
115 #endif /* AFS_64BIT_CLIENT */
116 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
118 code = generic_file_read(fp, buf, count, offp);
120 #ifdef AFS_64BIT_CLIENT
122 #endif /* AFS_64BIT_CLIENT */
125 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
126 ICL_TYPE_OFFSET, offp,
127 ICL_TYPE_INT32, count,
128 ICL_TYPE_INT32, code);
136 /* Now we have integrated VM for writes as well as reads. generic_file_write
137 * also takes care of re-positioning the pointer if file is open in append
138 * mode. Call fake open/close to ensure we do writes of core dumps.
140 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
145 struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
146 struct vrequest treq;
147 cred_t *credp = crref();
151 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
152 ICL_TYPE_OFFSET, offp,
153 ICL_TYPE_INT32, count,
154 ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
157 /* get a validated vcache entry */
158 code = (ssize_t)afs_InitReq(&treq, credp);
160 code = (ssize_t)afs_VerifyVCache(vcp, &treq);
162 ObtainWriteLock(&vcp->lock, 529);
164 ReleaseWriteLock(&vcp->lock);
168 #ifdef AFS_64BIT_CLIENT
169 if (*offp + count > afs_vmMappingEnd) {
172 afs_size_t oldOffset = *offp;
173 afs_int32 xfered = 0;
175 if (*offp < afs_vmMappingEnd) {
176 /* special case of a buffer crossing the VM mapping end */
177 afs_int32 tcount = afs_vmMappingEnd - *offp;
180 code = generic_file_write(fp, buf, tcount, offp);
182 if (code != tcount) {
187 setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) *offp, count,
188 UIO_WRITE, AFS_UIOSYS);
189 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
190 xfered += count - tuio.uio_resid;
193 *offp += count - tuio.uio_resid;
195 /* Purge dirty chunks of file if there are too many dirty chunks.
196 * Inside the write loop, we only do this at a chunk boundary.
197 * Clean up partial chunk if necessary at end of loop.
199 if (AFS_CHUNKBASE(tuio.afsio_offset) != AFS_CHUNKBASE(oldOffset)) {
200 ObtainWriteLock(&vcp->lock,402);
201 code = afs_DoPartialWrite(vcp, &treq);
202 vcp->states |= CDirty;
203 ReleaseWriteLock(&vcp->lock);
207 ObtainWriteLock(&vcp->lock,400);
208 vcp->m.Date = osi_Time(); /* Set file date (for ranlib) */
210 if (*offp > vcp->m.Length) {
211 vcp->m.Length = *offp;
213 ReleaseWriteLock(&vcp->lock);
217 #endif /* AFS_64BIT_CLIENT */
219 code = generic_file_write(fp, buf, count, offp);
221 #ifdef AFS_64BIT_CLIENT
223 #endif /* AFS_64BIT_CLIENT */
226 ObtainWriteLock(&vcp->lock, 530);
227 vcp->m.Date = osi_Time(); /* set modification time */
228 afs_FakeClose(vcp, credp);
230 code2 = afs_DoPartialWrite(vcp, &treq);
231 if (code2 && code >=0)
232 code = (ssize_t) -code2;
233 ReleaseWriteLock(&vcp->lock);
235 afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
236 ICL_TYPE_OFFSET, offp,
237 ICL_TYPE_INT32, count,
238 ICL_TYPE_INT32, code);
245 /* This is a complete rewrite of afs_readdir, since we can make use of
246 * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
247 * handling and use of bulkstats will need to be reflected here as well.
249 static int afs_linux_readdir(struct file *fp,
250 void *dirbuf, filldir_t filldir)
252 extern struct DirEntry * afs_dir_GetBlob();
253 struct vcache *avc = (struct vcache*)FILE_INODE(fp);
254 struct vrequest treq;
255 register struct dcache *tdc;
262 afs_size_t origOffset, tlen;
263 cred_t *credp = crref();
266 AFS_STATCNT(afs_readdir);
268 code = afs_InitReq(&treq, credp);
275 /* update the cache entry */
277 code = afs_VerifyVCache(avc, &treq);
283 /* get a reference to the entire directory */
284 tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
290 ObtainReadLock(&avc->lock);
291 ObtainReadLock(&tdc->lock);
293 * Make sure that the data in the cache is current. There are two
294 * cases we need to worry about:
295 * 1. The cache data is being fetched by another process.
296 * 2. The cache data is no longer valid
298 while ((avc->states & CStatd)
299 && (tdc->dflags & DFFetching)
300 && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
301 ReleaseReadLock(&tdc->lock);
302 ReleaseReadLock(&avc->lock);
303 afs_osi_Sleep(&tdc->validPos);
304 ObtainReadLock(&avc->lock);
305 ObtainReadLock(&tdc->lock);
307 if (!(avc->states & CStatd)
308 || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
309 ReleaseReadLock(&tdc->lock);
310 ReleaseReadLock(&avc->lock);
315 /* Fill in until we get an error or we're done. This implementation
316 * takes an offset in units of blobs, rather than bytes.
319 offset = (int)fp->f_pos;
321 dirpos = BlobScan(&tdc->f.inode, offset);
325 de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
329 ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
330 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
331 len = strlen(de->name);
333 /* filldir returns -EINVAL when the buffer is full. */
334 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
336 unsigned int type=DT_UNKNOWN;
337 struct VenusFid afid;
340 afid.Cell=avc->fid.Cell;
341 afid.Fid.Volume=avc->fid.Fid.Volume;
342 afid.Fid.Vnode=ntohl(de->fid.vnode);
343 afid.Fid.Unique=ntohl(de->fid.vunique);
344 if ((avc->states & CForeign) == 0 &&
345 (ntohl(de->fid.vnode) & 1)) {
346 } else if ((tvc=afs_FindVCache(&afid,0,0,0,0))) {
349 } else if (((tvc->states) & (CStatd|CTruth))) {
350 /* CTruth will be set if the object has
355 else if (vtype == VREG)
357 /* Don't do this until we're sure it can't be a mtpt */
358 /* else if (vtype == VLNK)
360 /* what other types does AFS support? */
362 /* clean up from afs_FindVCache */
363 afs_PutVCache(tvc, WRITE_LOCK);
365 code = (*filldir)(dirbuf, de->name, len, offset, ino, type);
368 code = (*filldir)(dirbuf, de->name, len, offset, ino);
373 offset = dirpos + 1 + ((len+16)>>5);
375 /* If filldir didn't fill in the last one this is still pointing to that
378 fp->f_pos = (loff_t)offset;
380 ReleaseReadLock(&tdc->lock);
382 ReleaseReadLock(&avc->lock);
388 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
391 /* in afs_pioctl.c */
392 extern int afs_xioctl(struct inode *ip, struct file *fp,
393 unsigned int com, unsigned long arg);
396 /* We need to detect unmap's after close. To do that, we need our own
397 * vm_operations_struct's. And we need to set them up for both the
398 * private and shared mappings. The fun part is that these are all static
399 * so we'll have to initialize on the fly!
401 static struct vm_operations_struct afs_private_mmap_ops;
402 static int afs_private_mmap_ops_inited = 0;
403 static struct vm_operations_struct afs_shared_mmap_ops;
404 static int afs_shared_mmap_ops_inited = 0;
406 void afs_linux_vma_close(struct vm_area_struct *vmap)
414 vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
419 afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
420 ICL_TYPE_POINTER, vcp,
421 ICL_TYPE_INT32, vcp->mapcnt,
422 ICL_TYPE_INT32, vcp->opens,
423 ICL_TYPE_INT32, vcp->execsOrWriters);
424 ObtainWriteLock(&vcp->lock, 532);
427 ReleaseWriteLock(&vcp->lock);
430 (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
431 /* only decrement the execsOrWriters flag if this is not a writable
433 if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
434 vcp->execsOrWriters--;
436 vcp->states &= ~CMAPPED;
441 ReleaseWriteLock(&vcp->lock);
448 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
450 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
451 cred_t *credp = crref();
452 struct vrequest treq;
456 #if defined(AFS_LINUX24_ENV)
457 afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
458 ICL_TYPE_POINTER, vmap->vm_start,
459 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start);
461 afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
462 ICL_TYPE_POINTER, vmap->vm_start,
463 ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
464 ICL_TYPE_INT32, vmap->vm_offset);
467 /* get a validated vcache entry */
468 code = afs_InitReq(&treq, credp);
470 code = afs_VerifyVCache(vcp, &treq);
476 osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
479 code = generic_file_mmap(fp, vmap);
484 ObtainWriteLock(&vcp->lock,531);
485 /* Set out vma ops so we catch the close. The following test should be
486 * the same as used in generic_file_mmap.
488 if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
489 if (!afs_shared_mmap_ops_inited) {
490 afs_shared_mmap_ops_inited = 1;
491 afs_shared_mmap_ops = *vmap->vm_ops;
492 afs_shared_mmap_ops.close = afs_linux_vma_close;
494 vmap->vm_ops = &afs_shared_mmap_ops;
497 if (!afs_private_mmap_ops_inited) {
498 afs_private_mmap_ops_inited = 1;
499 afs_private_mmap_ops = *vmap->vm_ops;
500 afs_private_mmap_ops.close = afs_linux_vma_close;
502 vmap->vm_ops = &afs_private_mmap_ops;
506 /* Add an open reference on the first mapping. */
507 if (vcp->mapcnt == 0) {
508 vcp->execsOrWriters++;
510 vcp->states |= CMAPPED;
512 ReleaseWriteLock(&vcp->lock);
521 int afs_linux_open(struct inode *ip, struct file *fp)
524 cred_t *credp = crref();
527 #ifdef AFS_LINUX24_ENV
530 code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
531 #ifdef AFS_LINUX24_ENV
540 /* afs_Close is called from release, since release is used to handle all
541 * file closings. In addition afs_linux_flush is called from sys_close to
542 * handle flushing the data back to the server. The kicker is that we could
543 * ignore flush completely if only sys_close took it's return value from
544 * fput. See afs_linux_flush for notes on interactions between release and
547 static int afs_linux_release(struct inode *ip, struct file *fp)
550 cred_t *credp = crref();
551 struct vcache *vcp = (struct vcache*)ip;
554 #ifdef AFS_LINUX24_ENV
558 vcp->flushcnt--; /* protected by AFS global lock. */
561 code = afs_close(vcp, fp->f_flags, credp);
563 #ifdef AFS_LINUX24_ENV
572 #if defined(AFS_LINUX24_ENV)
573 static int afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
575 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
579 struct inode *ip = FILE_INODE(fp);
580 cred_t *credp = crref();
583 #ifdef AFS_LINUX24_ENV
586 code = afs_fsync((struct vcache*)ip, credp);
587 #ifdef AFS_LINUX24_ENV
597 /* No support for async i/o */
598 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
600 /* I don't think it will, at least not as can be detected here. */
601 int afs_linux_check_media_change(kdev_t dev);
603 /* Revalidate media and file system. */
604 int afs_linux_file_revalidate(kdev_t dev);
607 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
610 struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
611 cred_t *credp = crref();
612 #ifdef AFS_LINUX24_ENV
613 struct flock64 flock;
618 /* Convert to a lock format afs_lockctl understands. */
619 memset((char*)&flock, 0, sizeof(flock));
620 flock.l_type = flp->fl_type;
621 flock.l_pid = flp->fl_pid;
623 flock.l_start = flp->fl_start;
624 flock.l_len = flp->fl_end - flp->fl_start;
627 code = afs_lockctl(vcp, &flock, cmd, credp);
635 * flush is called from sys_close. We could ignore it, but sys_close return
636 * code comes from flush, not release. We need to use release to keep
637 * the vcache open count correct. Note that flush is called before release
638 * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
639 * races and also avoid calling afs_close twice when closing the file.
640 * If we merely checked for opens > 0 in afs_linux_release, then if an
641 * new open occurred when storing back the file, afs_linux_release would
642 * incorrectly close the file and decrement the opens count. Calling afs_close
643 * on the just flushed file is wasteful, since the background daemon will
644 * execute the code that finally decides there is nothing to do.
646 int afs_linux_flush(struct file *fp)
648 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
652 /* Only do this on the last close of the file pointer. */
653 #if defined(AFS_LINUX24_ENV)
654 if (atomic_read(&fp->f_count) > 1)
663 code = afs_close(vcp, fp->f_flags, credp);
664 vcp->flushcnt++; /* protected by AFS global lock. */
671 /* Not allowed to directly read a directory. */
672 ssize_t afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
679 #if defined(AFS_LINUX24_ENV)
680 struct file_operations afs_dir_fops = {
681 read: generic_read_dir,
682 readdir: afs_linux_readdir,
684 open: afs_linux_open,
685 release: afs_linux_release,
688 struct file_operations afs_dir_fops = {
689 NULL, /* afs_linux_lseek */
691 NULL, /* afs_linux_write */
693 NULL, /* afs_linux_select */
694 afs_xioctl, /* close enough to use the ported AFS one */
695 NULL, /* afs_linux_mmap */
697 NULL, /* afs_linux_flush */
700 NULL, /* afs_linux_fasync */
701 NULL, /* afs_linux_check_media_change */
702 NULL, /* afs_linux_file_revalidate */
707 #if defined(AFS_LINUX24_ENV)
708 struct file_operations afs_file_fops = {
709 read: afs_linux_read,
710 write: afs_linux_write,
712 mmap: afs_linux_mmap,
713 open: afs_linux_open,
714 flush: afs_linux_flush,
715 release: afs_linux_release,
716 fsync: afs_linux_fsync,
717 lock: afs_linux_lock,
720 struct file_operations afs_file_fops = {
721 NULL, /* afs_linux_lseek */
724 NULL, /* afs_linux_readdir */
725 NULL, /* afs_linux_select */
726 afs_xioctl, /* close enough to use the ported AFS one */
732 NULL, /* afs_linux_fasync */
733 NULL, /* afs_linux_check_media_change */
734 NULL, /* afs_linux_file_revalidate */
740 /**********************************************************************
741 * AFS Linux dentry operations
742 **********************************************************************/
744 /* afs_linux_revalidate
745 * Ensure vcache is stat'd before use. Return 0 if entry is valid.
747 static int afs_linux_revalidate(struct dentry *dp)
751 struct vrequest treq;
752 struct vcache *vcp = (struct vcache*)dp->d_inode;
755 #ifdef AFS_LINUX24_ENV
759 /* Make this a fast path (no crref), since it's called so often. */
760 if (vcp->states & CStatd) {
761 if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
762 check_bad_parent(dp); /* check and correct mvid */
764 #ifdef AFS_LINUX24_ENV
772 code = afs_InitReq(&treq, credp);
774 code = afs_VerifyVCache(vcp, &treq);
776 #ifdef AFS_LINUX24_ENV
786 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
787 * In kernels 2.2.10 and above, we are passed an additional flags var which
788 * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
789 * we are advised to follow the entry if it is a link or to make sure that
790 * it is a directory. But since the kernel itself checks these possibilities
791 * later on, we shouldn't have to do it until later. Perhaps in the future..
793 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
794 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
796 static int afs_linux_dentry_revalidate(struct dentry *dp)
800 cred_t *credp = crref();
801 struct vrequest treq;
802 struct vcache *lookupvcp = NULL;
803 int code, bad_dentry = 1;
804 struct sysname_info sysState;
805 struct vcache *vcp = (struct vcache*) dp->d_inode;
806 struct vcache *parentvcp = (struct vcache*) dp->d_parent->d_inode;
810 /* If it's a negative dentry, then there's nothing to do. */
811 if (!vcp || !parentvcp)
814 if (code = afs_InitReq(&treq, credp))
817 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
818 name = sysState.name;
820 /* First try looking up the DNLC */
821 if (lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK)) {
822 /* Verify that the dentry does not point to an old inode */
823 if (vcp != lookupvcp)
825 /* Check and correct mvid */
826 if (*name != '/' && vcp->mvstat == 2)
827 check_bad_parent(dp);
833 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
834 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
836 /* Verify that the dentry does not point to an old inode */
837 if (vcp != lookupvcp)
845 afs_PutVCache(lookupvcp, WRITE_LOCK);
846 if (sysState.allocked)
847 osi_FreeLargeSpace(name);
853 shrink_dcache_parent(dp);
861 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
862 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
864 static int afs_linux_dentry_revalidate(struct dentry *dp)
869 struct vrequest treq;
870 struct inode *ip = (struct inode *)dp->d_inode;
872 unsigned long timeout = 3*HZ; /* 3 seconds */
875 printk("negative dentry: %s\n", dp->d_name.name);
877 if (!(flags & LOOKUP_CONTINUE)) {
878 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
884 if (time_after(jiffies, dp->d_time + timeout))
895 /* afs_dentry_iput */
896 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
901 #if defined(AFS_LINUX24_ENV)
902 struct dentry_operations afs_dentry_operations = {
903 d_revalidate: afs_linux_dentry_revalidate,
904 d_iput: afs_dentry_iput,
906 struct dentry_operations *afs_dops = &afs_dentry_operations;
908 struct dentry_operations afs_dentry_operations = {
909 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
911 NULL, /* d_compare */
912 NULL, /* d_delete(struct dentry *) */
913 NULL, /* d_release(struct dentry *) */
914 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
916 struct dentry_operations *afs_dops = &afs_dentry_operations;
919 /**********************************************************************
920 * AFS Linux inode operations
921 **********************************************************************/
925 * Merely need to set enough of vattr to get us through the create. Note
926 * that the higher level code (open_namei) will take care of any tuncation
927 * explicitly. Exclusive open is also taken care of in open_namei.
929 * name is in kernel space at this point.
931 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
934 cred_t *credp = crref();
937 const char *name = dp->d_name.name;
941 vattr.va_mode = mode;
944 code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
945 (struct vcache**)&ip, credp);
948 vattr2inode(ip, &vattr);
949 /* Reset ops if symlink or directory. */
950 #if defined(AFS_LINUX24_ENV)
951 if (S_ISREG(ip->i_mode)) {
952 ip->i_op = &afs_file_iops;
953 ip->i_fop = &afs_file_fops;
954 ip->i_data.a_ops = &afs_file_aops;
955 } else if (S_ISDIR(ip->i_mode)) {
956 ip->i_op = &afs_dir_iops;
957 ip->i_fop = &afs_dir_fops;
958 } else if (S_ISLNK(ip->i_mode)) {
959 ip->i_op = &afs_symlink_iops;
960 ip->i_data.a_ops = &afs_symlink_aops;
961 ip->i_mapping = &ip->i_data;
963 printk("afs_linux_create: FIXME\n");
965 if (S_ISDIR(ip->i_mode))
966 ip->i_op = &afs_dir_iops;
967 else if (S_ISLNK(ip->i_mode))
968 ip->i_op = &afs_symlink_iops;
972 dp->d_time = jiffies;
973 d_instantiate(dp, ip);
981 /* afs_linux_lookup */
982 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
983 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
985 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
989 cred_t *credp = crref();
990 struct vcache *vcp=NULL;
991 const char *comp = dp->d_name.name;
993 code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
996 struct inode *ip = (struct inode*)vcp;
997 /* Reset ops if symlink or directory. */
998 #if defined(AFS_LINUX24_ENV)
999 if (S_ISREG(ip->i_mode)) {
1000 ip->i_op = &afs_file_iops;
1001 ip->i_fop = &afs_file_fops;
1002 ip->i_data.a_ops = &afs_file_aops;
1003 } else if (S_ISDIR(ip->i_mode)) {
1004 ip->i_op = &afs_dir_iops;
1005 ip->i_fop = &afs_dir_fops;
1006 } else if (S_ISLNK(ip->i_mode)) {
1007 ip->i_op = &afs_symlink_iops;
1008 ip->i_data.a_ops = &afs_symlink_aops;
1009 ip->i_mapping = &ip->i_data;
1011 printk("afs_linux_lookup: ip->i_mode 0x%x dp->d_name.name %s code %d\n", ip->i_mode, dp->d_name.name, code);
1013 if (S_ISDIR(ip->i_mode))
1014 ip->i_op = &afs_dir_iops;
1015 else if (S_ISLNK(ip->i_mode))
1016 ip->i_op = &afs_symlink_iops;
1018 dp->d_time = jiffies;
1019 dp->d_op = afs_dops;
1020 d_add(dp, (struct inode*)vcp);
1026 /* It's ok for the file to not be found. That's noted by the caller by
1027 * seeing that the dp->d_inode field is NULL.
1029 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1033 return ERR_PTR(-code);
1041 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1042 struct dentry *newdp)
1045 cred_t *credp = crref();
1046 const char *name = newdp->d_name.name;
1047 struct inode *oldip = olddp->d_inode;
1049 /* If afs_link returned the vnode, we could instantiate the
1050 * dentry. Since it's not, we drop this one and do a new lookup.
1055 code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
1062 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1065 cred_t *credp = crref();
1066 const char *name = dp->d_name.name;
1070 code = afs_remove((struct vcache*)dip, name, credp);
1079 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1083 cred_t *credp = crref();
1085 const char *name = dp->d_name.name;
1087 /* If afs_symlink returned the vnode, we could instantiate the
1088 * dentry. Since it's not, we drop this one and do a new lookup.
1094 code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
1100 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1103 cred_t *credp = crref();
1104 struct vcache *tvcp = NULL;
1106 const char *name = dp->d_name.name;
1110 vattr.va_mask = ATTR_MODE;
1111 vattr.va_mode = mode;
1112 code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
1115 tvcp->v.v_op = &afs_dir_iops;
1116 #if defined(AFS_LINUX24_ENV)
1117 tvcp->v.v_fop = &afs_dir_fops;
1119 dp->d_op = afs_dops;
1120 dp->d_time = jiffies;
1121 d_instantiate(dp, (struct inode*)tvcp);
1129 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1132 cred_t *credp = crref();
1133 const char *name = dp->d_name.name;
1136 code = afs_rmdir((struct vcache*)dip, name, credp);
1138 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1139 * that failed because a directory is not empty. So, we map
1140 * EEXIST to ENOTEMPTY on linux.
1142 if (code == EEXIST) {
1157 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1158 struct inode *newip, struct dentry *newdp)
1161 cred_t *credp = crref();
1162 const char *oldname = olddp->d_name.name;
1163 const char *newname = newdp->d_name.name;
1165 /* Remove old and new entries from name hash. New one will change below.
1166 * While it's optimal to catch failures and re-insert newdp into hash,
1167 * it's also error prone and in that case we're already dealing with error
1168 * cases. Let another lookup put things right, if need be.
1170 if (!list_empty(&olddp->d_hash)) {
1173 if (!list_empty(&newdp->d_hash)) {
1177 code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
1182 /* update time so it doesn't expire immediately */
1183 newdp->d_time = jiffies;
1184 d_move(olddp, newdp);
1192 /* afs_linux_ireadlink
1193 * Internal readlink which can return link contents to user or kernel space.
1194 * Note that the buffer is NOT supposed to be null-terminated.
1196 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1200 cred_t *credp = crref();
1204 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1205 code = afs_readlink((struct vcache*)ip, &tuio, credp);
1209 return maxlen - tuio.uio_resid;
1214 #if !defined(AFS_LINUX24_ENV)
1215 /* afs_linux_readlink
1216 * Fill target (which is in user space) with contents of symlink.
1218 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1221 struct inode *ip = dp->d_inode;
1224 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1230 /* afs_linux_follow_link
1231 * a file system dependent link following routine.
1233 struct dentry * afs_linux_follow_link(struct dentry *dp,
1234 struct dentry *basep,
1235 unsigned int follow)
1243 name = osi_Alloc(PATH_MAX+1);
1247 return ERR_PTR(-EIO);
1250 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1255 res = ERR_PTR(code);
1259 res = lookup_dentry(name, basep, follow);
1263 osi_Free(name, PATH_MAX+1);
1269 /* afs_linux_readpage
1270 * all reads come through here. A strategy-like read call.
1272 int afs_linux_readpage(struct file *fp, struct page *pp)
1275 cred_t *credp = crref();
1276 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1278 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1280 ulong address = afs_linux_page_address(pp);
1281 afs_offs_t offset = pageoff(pp);
1285 struct inode *ip = FILE_INODE(fp);
1286 int cnt = atomic_read(&pp->count);
1289 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1290 ICL_TYPE_POINTER, ip,
1291 ICL_TYPE_POINTER, pp,
1292 ICL_TYPE_INT32, cnt,
1293 ICL_TYPE_INT32, 99999); /* not a possible code value */
1294 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1300 atomic_add(1, &pp->count);
1301 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1302 clear_bit(PG_error, &pp->flags);
1305 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1306 UIO_READ, AFS_UIOSYS);
1307 code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
1308 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1313 if (tuio.uio_resid) /* zero remainder of page */
1314 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1316 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1317 flush_dcache_page(pp);
1318 SetPageUptodate(pp);
1320 set_bit(PG_uptodate, &pp->flags);
1324 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1328 clear_bit(PG_locked, &pp->flags);
1334 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1335 ICL_TYPE_POINTER, ip,
1336 ICL_TYPE_POINTER, pp,
1337 ICL_TYPE_INT32, cnt,
1338 ICL_TYPE_INT32, code);
1343 #if defined(AFS_LINUX24_ENV)
1344 int afs_linux_writepage(struct page *pp)
1346 struct address_space *mapping = pp->mapping;
1347 struct inode *inode;
1348 unsigned long end_index;
1349 unsigned offset = PAGE_CACHE_SIZE;
1352 inode = (struct inode *) mapping->host;
1353 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1356 if (pp->index < end_index)
1358 /* things got complicated... */
1359 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1360 /* OK, are we completely out? */
1361 if (pp->index >= end_index+1 || !offset)
1365 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1367 SetPageUptodate(pp);
1369 if (status == offset)
1377 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1378 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1380 /* afs_linux_truncate
1381 * Handles discarding disk blocks if this were a device. ext2 indicates we
1382 * may need to zero partial last pages of memory mapped files.
1384 void afs_linux_truncate(struct inode *ip)
1389 /* afs_linux_permission
1390 * Check access rights - returns error if can't check or permission denied.
1392 int afs_linux_permission(struct inode *ip, int mode)
1395 cred_t *credp = crref();
1399 if (mode & MAY_EXEC) tmp |= VEXEC;
1400 if (mode & MAY_READ) tmp |= VREAD;
1401 if (mode & MAY_WRITE) tmp |= VWRITE;
1402 code = afs_access((struct vcache*)ip, tmp, credp);
1411 /* msdos sector mapping hack for memory mapping. */
1412 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1415 #if defined(AFS_LINUX24_ENV)
1416 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1417 unsigned long offset,
1420 struct vcache *vcp = (struct vcache *) ip;
1429 buffer = kmap(pp) + offset;
1430 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1433 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1434 ICL_TYPE_POINTER, pp,
1435 ICL_TYPE_INT32, atomic_read(&pp->count),
1436 ICL_TYPE_INT32, 99999);
1437 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1439 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1443 code = code ? -code : count - tuio.uio_resid;
1444 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1445 ICL_TYPE_POINTER, pp,
1446 ICL_TYPE_INT32, atomic_read(&pp->count),
1447 ICL_TYPE_INT32, code);
1456 afs_linux_updatepage(struct file *file, struct page *page,
1457 unsigned long offset, unsigned int count)
1459 struct dentry *dentry = file->f_dentry;
1461 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1464 /* afs_linux_updatepage
1465 * What one would have thought was writepage - write dirty page to file.
1466 * Called from generic_file_write. buffer is still in user space. pagep
1467 * has been filled in with old data if we're updating less than a page.
1469 int afs_linux_updatepage(struct file *fp, struct page *pp,
1470 unsigned long offset,
1471 unsigned int count, int sync)
1473 struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1474 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1480 set_bit(PG_locked, &pp->flags);
1484 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1485 ICL_TYPE_POINTER, pp,
1486 ICL_TYPE_INT32, atomic_read(&pp->count),
1487 ICL_TYPE_INT32, 99999);
1488 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1489 count, UIO_WRITE, AFS_UIOSYS);
1491 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1495 code = code ? -code : count - tuio.uio_resid;
1496 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1497 ICL_TYPE_POINTER, pp,
1498 ICL_TYPE_INT32, atomic_read(&pp->count),
1499 ICL_TYPE_INT32, code);
1504 clear_bit(PG_locked, &pp->flags);
1509 #if defined(AFS_LINUX24_ENV)
1510 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1516 code = afs_linux_updatepage(file, page, offset, to-offset);
1524 static int afs_linux_prepare_write(struct file *file, struct page *page,
1525 unsigned from, unsigned to)
1531 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1534 #if defined(AFS_LINUX24_ENV)
1535 struct inode_operations afs_file_iops = {
1536 revalidate: afs_linux_revalidate,
1537 setattr: afs_notify_change,
1538 permission: afs_linux_permission,
1540 struct address_space_operations afs_file_aops = {
1541 readpage: afs_linux_readpage,
1542 writepage: afs_linux_writepage,
1543 commit_write: afs_linux_commit_write,
1544 prepare_write: afs_linux_prepare_write,
1547 struct inode_operations *afs_ops = &afs_file_iops;
1549 struct inode_operations afs_iops = {
1550 &afs_file_fops, /* file operations */
1551 NULL, /* afs_linux_create */
1552 NULL, /* afs_linux_lookup */
1553 NULL, /* afs_linux_link */
1554 NULL, /* afs_linux_unlink */
1555 NULL, /* afs_linux_symlink */
1556 NULL, /* afs_linux_mkdir */
1557 NULL, /* afs_linux_rmdir */
1558 NULL, /* afs_linux_mknod */
1559 NULL, /* afs_linux_rename */
1560 NULL, /* afs_linux_readlink */
1561 NULL, /* afs_linux_follow_link */
1563 NULL, /* afs_linux_writepage */
1564 NULL, /* afs_linux_bmap */
1565 NULL, /* afs_linux_truncate */
1566 afs_linux_permission,
1567 NULL, /* afs_linux_smap */
1568 afs_linux_updatepage,
1569 afs_linux_revalidate,
1572 struct inode_operations *afs_ops = &afs_iops;
1575 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1576 * by what sort of operation is allowed.....
1578 #if defined(AFS_LINUX24_ENV)
1579 struct inode_operations afs_dir_iops = {
1580 create: afs_linux_create,
1581 lookup: afs_linux_lookup,
1582 link: afs_linux_link,
1583 unlink: afs_linux_unlink,
1584 symlink: afs_linux_symlink,
1585 mkdir: afs_linux_mkdir,
1586 rmdir: afs_linux_rmdir,
1587 rename: afs_linux_rename,
1588 revalidate: afs_linux_revalidate,
1589 setattr: afs_notify_change,
1590 permission: afs_linux_permission,
1593 struct inode_operations afs_dir_iops = {
1594 &afs_dir_fops, /* file operations for directories */
1602 NULL, /* afs_linux_mknod */
1604 NULL, /* afs_linux_readlink */
1605 NULL, /* afs_linux_follow_link */
1606 NULL, /* afs_linux_readpage */
1607 NULL, /* afs_linux_writepage */
1608 NULL, /* afs_linux_bmap */
1609 NULL, /* afs_linux_truncate */
1610 afs_linux_permission,
1611 NULL, /* afs_linux_smap */
1612 NULL, /* afs_linux_updatepage */
1613 afs_linux_revalidate,
1617 /* We really need a separate symlink set of ops, since do_follow_link()
1618 * determines if it _is_ a link by checking if the follow_link op is set.
1620 #if defined(AFS_LINUX24_ENV)
1621 static int afs_symlink_filler(struct file *file, struct page *page)
1623 struct inode *ip = (struct inode *) page->mapping->host;
1624 char *p = (char *)kmap(page);
1629 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1633 p[code] = '\0'; /* null terminate? */
1637 SetPageUptodate(page);
1652 struct address_space_operations afs_symlink_aops = {
1653 readpage: afs_symlink_filler
1656 struct inode_operations afs_symlink_iops = {
1657 readlink: page_readlink,
1658 follow_link: page_follow_link,
1659 setattr: afs_notify_change,
1662 struct inode_operations afs_symlink_iops = {
1663 NULL, /* file operations */
1671 NULL, /* afs_linux_mknod */
1674 afs_linux_follow_link,
1675 NULL, /* readpage */
1676 NULL, /* afs_linux_writepage */
1677 NULL, /* afs_linux_bmap */
1678 NULL, /* afs_linux_truncate */
1679 afs_linux_permission, /* tho the code appears to indicate not used? */
1680 NULL, /* afs_linux_smap */
1681 NULL, /* updatepage */
1682 afs_linux_revalidate, /* tho the code appears to indicate not used? */