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 = ITOAFS(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 = ITOAFS(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 = ITOAFS(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 = ITOAFS(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 = ITOAFS(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 = ITOAFS(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(ITOAFS(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 = ITOAFS(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 = ITOAFS(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 = ITOAFS(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 = ITOAFS(dp->d_inode);
806 struct vcache *parentvcp = ITOAFS(dp->d_parent->d_inode);
810 sysState.allocked = 0;
812 /* If it's a negative dentry, then there's nothing to do. */
813 if (!vcp || !parentvcp)
816 if (code = afs_InitReq(&treq, credp))
819 Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
820 name = sysState.name;
822 /* First try looking up the DNLC */
823 if (lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK)) {
824 /* Verify that the dentry does not point to an old inode */
825 if (vcp != lookupvcp)
827 /* Check and correct mvid */
828 if (*name != '/' && vcp->mvstat == 2)
829 check_bad_parent(dp);
835 /* A DNLC lookup failure cannot be trusted. Try a real lookup */
836 code = afs_lookup(parentvcp, name, &lookupvcp, credp);
838 /* Verify that the dentry does not point to an old inode */
839 if (vcp != lookupvcp)
847 afs_PutVCache(lookupvcp, WRITE_LOCK);
848 if (sysState.allocked)
849 osi_FreeLargeSpace(name);
855 shrink_dcache_parent(dp);
863 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
864 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
866 static int afs_linux_dentry_revalidate(struct dentry *dp)
871 struct vrequest treq;
872 struct inode *ip = AFSTOI(dp->d_inode);
874 unsigned long timeout = 3*HZ; /* 3 seconds */
877 printk("negative dentry: %s\n", dp->d_name.name);
879 if (!(flags & LOOKUP_CONTINUE)) {
880 long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
886 if (time_after(jiffies, dp->d_time + timeout))
897 /* afs_dentry_iput */
898 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
903 #if defined(AFS_LINUX24_ENV)
904 struct dentry_operations afs_dentry_operations = {
905 d_revalidate: afs_linux_dentry_revalidate,
906 d_iput: afs_dentry_iput,
908 struct dentry_operations *afs_dops = &afs_dentry_operations;
910 struct dentry_operations afs_dentry_operations = {
911 afs_linux_dentry_revalidate, /* d_validate(struct dentry *) */
913 NULL, /* d_compare */
914 NULL, /* d_delete(struct dentry *) */
915 NULL, /* d_release(struct dentry *) */
916 afs_dentry_iput /* d_iput(struct dentry *, struct inode *) */
918 struct dentry_operations *afs_dops = &afs_dentry_operations;
921 /**********************************************************************
922 * AFS Linux inode operations
923 **********************************************************************/
927 * Merely need to set enough of vattr to get us through the create. Note
928 * that the higher level code (open_namei) will take care of any tuncation
929 * explicitly. Exclusive open is also taken care of in open_namei.
931 * name is in kernel space at this point.
933 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
936 cred_t *credp = crref();
939 const char *name = dp->d_name.name;
943 vattr.va_mode = mode;
946 code = afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
947 (struct vcache**)&ip, credp);
950 vattr2inode(ip, &vattr);
951 /* Reset ops if symlink or directory. */
952 #if defined(AFS_LINUX24_ENV)
953 if (S_ISREG(ip->i_mode)) {
954 ip->i_op = &afs_file_iops;
955 ip->i_fop = &afs_file_fops;
956 ip->i_data.a_ops = &afs_file_aops;
957 } else if (S_ISDIR(ip->i_mode)) {
958 ip->i_op = &afs_dir_iops;
959 ip->i_fop = &afs_dir_fops;
960 } else if (S_ISLNK(ip->i_mode)) {
961 ip->i_op = &afs_symlink_iops;
962 ip->i_data.a_ops = &afs_symlink_aops;
963 ip->i_mapping = &ip->i_data;
965 printk("afs_linux_create: FIXME\n");
967 if (S_ISDIR(ip->i_mode))
968 ip->i_op = &afs_dir_iops;
969 else if (S_ISLNK(ip->i_mode))
970 ip->i_op = &afs_symlink_iops;
974 dp->d_time = jiffies;
975 d_instantiate(dp, ip);
983 /* afs_linux_lookup */
984 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
985 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
987 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
991 cred_t *credp = crref();
992 struct vcache *vcp=NULL;
993 const char *comp = dp->d_name.name;
995 code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
998 struct inode *ip = AFSTOI(vcp);
999 /* Reset ops if symlink or directory. */
1000 #if defined(AFS_LINUX24_ENV)
1001 if (S_ISREG(ip->i_mode)) {
1002 ip->i_op = &afs_file_iops;
1003 ip->i_fop = &afs_file_fops;
1004 ip->i_data.a_ops = &afs_file_aops;
1005 } else if (S_ISDIR(ip->i_mode)) {
1006 ip->i_op = &afs_dir_iops;
1007 ip->i_fop = &afs_dir_fops;
1008 } else if (S_ISLNK(ip->i_mode)) {
1009 ip->i_op = &afs_symlink_iops;
1010 ip->i_data.a_ops = &afs_symlink_aops;
1011 ip->i_mapping = &ip->i_data;
1013 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);
1015 if (S_ISDIR(ip->i_mode))
1016 ip->i_op = &afs_dir_iops;
1017 else if (S_ISLNK(ip->i_mode))
1018 ip->i_op = &afs_symlink_iops;
1021 dp->d_time = jiffies;
1022 dp->d_op = afs_dops;
1023 d_add(dp, AFSTOI(vcp));
1028 /* It's ok for the file to not be found. That's noted by the caller by
1029 * seeing that the dp->d_inode field is NULL.
1031 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1035 return ERR_PTR(-code);
1043 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1044 struct dentry *newdp)
1047 cred_t *credp = crref();
1048 const char *name = newdp->d_name.name;
1049 struct inode *oldip = olddp->d_inode;
1051 /* If afs_link returned the vnode, we could instantiate the
1052 * dentry. Since it's not, we drop this one and do a new lookup.
1057 code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1064 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1067 cred_t *credp = crref();
1068 const char *name = dp->d_name.name;
1072 code = afs_remove(ITOAFS(dip), name, credp);
1081 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1085 cred_t *credp = crref();
1087 const char *name = dp->d_name.name;
1089 /* If afs_symlink returned the vnode, we could instantiate the
1090 * dentry. Since it's not, we drop this one and do a new lookup.
1096 code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1102 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1105 cred_t *credp = crref();
1106 struct vcache *tvcp = NULL;
1108 const char *name = dp->d_name.name;
1112 vattr.va_mask = ATTR_MODE;
1113 vattr.va_mode = mode;
1114 code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1117 tvcp->v.v_op = &afs_dir_iops;
1118 #if defined(AFS_LINUX24_ENV)
1119 tvcp->v.v_fop = &afs_dir_fops;
1121 dp->d_op = afs_dops;
1122 dp->d_time = jiffies;
1123 d_instantiate(dp, AFSTOI(tvcp));
1131 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1134 cred_t *credp = crref();
1135 const char *name = dp->d_name.name;
1138 code = afs_rmdir(ITOAFS(dip), name, credp);
1140 /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1141 * that failed because a directory is not empty. So, we map
1142 * EEXIST to ENOTEMPTY on linux.
1144 if (code == EEXIST) {
1159 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1160 struct inode *newip, struct dentry *newdp)
1163 cred_t *credp = crref();
1164 const char *oldname = olddp->d_name.name;
1165 const char *newname = newdp->d_name.name;
1167 /* Remove old and new entries from name hash. New one will change below.
1168 * While it's optimal to catch failures and re-insert newdp into hash,
1169 * it's also error prone and in that case we're already dealing with error
1170 * cases. Let another lookup put things right, if need be.
1172 if (!list_empty(&olddp->d_hash)) {
1175 if (!list_empty(&newdp->d_hash)) {
1179 code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip),
1184 /* update time so it doesn't expire immediately */
1185 newdp->d_time = jiffies;
1186 d_move(olddp, newdp);
1194 /* afs_linux_ireadlink
1195 * Internal readlink which can return link contents to user or kernel space.
1196 * Note that the buffer is NOT supposed to be null-terminated.
1198 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1202 cred_t *credp = crref();
1206 setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1207 code = afs_readlink(ITOAFS(ip), &tuio, credp);
1211 return maxlen - tuio.uio_resid;
1216 #if !defined(AFS_LINUX24_ENV)
1217 /* afs_linux_readlink
1218 * Fill target (which is in user space) with contents of symlink.
1220 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1223 struct inode *ip = dp->d_inode;
1226 code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1232 /* afs_linux_follow_link
1233 * a file system dependent link following routine.
1235 struct dentry * afs_linux_follow_link(struct dentry *dp,
1236 struct dentry *basep,
1237 unsigned int follow)
1245 name = osi_Alloc(PATH_MAX+1);
1249 return ERR_PTR(-EIO);
1252 code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1257 res = ERR_PTR(code);
1261 res = lookup_dentry(name, basep, follow);
1265 osi_Free(name, PATH_MAX+1);
1271 /* afs_linux_readpage
1272 * all reads come through here. A strategy-like read call.
1274 int afs_linux_readpage(struct file *fp, struct page *pp)
1277 cred_t *credp = crref();
1278 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1280 afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1282 ulong address = afs_linux_page_address(pp);
1283 afs_offs_t offset = pageoff(pp);
1287 struct inode *ip = FILE_INODE(fp);
1288 int cnt = atomic_read(&pp->count);
1291 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1292 ICL_TYPE_POINTER, ip,
1293 ICL_TYPE_POINTER, pp,
1294 ICL_TYPE_INT32, cnt,
1295 ICL_TYPE_INT32, 99999); /* not a possible code value */
1296 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1302 atomic_add(1, &pp->count);
1303 set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1304 clear_bit(PG_error, &pp->flags);
1307 setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1308 UIO_READ, AFS_UIOSYS);
1309 code = afs_rdwr(ITOAFS(ip), &tuio, UIO_READ, 0, credp);
1310 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1315 if (tuio.uio_resid) /* zero remainder of page */
1316 memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1319 flush_dcache_page(pp);
1320 SetPageUptodate(pp);
1322 set_bit(PG_uptodate, &pp->flags);
1326 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1330 clear_bit(PG_locked, &pp->flags);
1336 afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1337 ICL_TYPE_POINTER, ip,
1338 ICL_TYPE_POINTER, pp,
1339 ICL_TYPE_INT32, cnt,
1340 ICL_TYPE_INT32, code);
1345 #if defined(AFS_LINUX24_ENV)
1346 int afs_linux_writepage(struct page *pp)
1348 struct address_space *mapping = pp->mapping;
1349 struct inode *inode;
1350 unsigned long end_index;
1351 unsigned offset = PAGE_CACHE_SIZE;
1354 inode = (struct inode *) mapping->host;
1355 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1358 if (pp->index < end_index)
1360 /* things got complicated... */
1361 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1362 /* OK, are we completely out? */
1363 if (pp->index >= end_index+1 || !offset)
1367 status = afs_linux_writepage_sync(inode, pp, 0, offset);
1369 SetPageUptodate(pp);
1371 if (status == offset)
1379 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1380 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1382 /* afs_linux_truncate
1383 * Handles discarding disk blocks if this were a device. ext2 indicates we
1384 * may need to zero partial last pages of memory mapped files.
1386 void afs_linux_truncate(struct inode *ip)
1391 /* afs_linux_permission
1392 * Check access rights - returns error if can't check or permission denied.
1394 int afs_linux_permission(struct inode *ip, int mode)
1397 cred_t *credp = crref();
1401 if (mode & MAY_EXEC) tmp |= VEXEC;
1402 if (mode & MAY_READ) tmp |= VREAD;
1403 if (mode & MAY_WRITE) tmp |= VWRITE;
1404 code = afs_access(ITOAFS(ip), tmp, credp);
1413 /* msdos sector mapping hack for memory mapping. */
1414 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1417 #if defined(AFS_LINUX24_ENV)
1418 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1419 unsigned long offset,
1422 struct vcache *vcp = ITOAFS(ip);
1431 buffer = kmap(pp) + offset;
1432 base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1435 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1436 ICL_TYPE_POINTER, pp,
1437 ICL_TYPE_INT32, atomic_read(&pp->count),
1438 ICL_TYPE_INT32, 99999);
1439 setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1441 code = afs_write(vcp, &tuio, f_flags, credp, 0);
1445 code = code ? -code : count - tuio.uio_resid;
1446 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1447 ICL_TYPE_POINTER, pp,
1448 ICL_TYPE_INT32, atomic_read(&pp->count),
1449 ICL_TYPE_INT32, code);
1458 afs_linux_updatepage(struct file *file, struct page *page,
1459 unsigned long offset, unsigned int count)
1461 struct dentry *dentry = file->f_dentry;
1463 return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1466 /* afs_linux_updatepage
1467 * What one would have thought was writepage - write dirty page to file.
1468 * Called from generic_file_write. buffer is still in user space. pagep
1469 * has been filled in with old data if we're updating less than a page.
1471 int afs_linux_updatepage(struct file *fp, struct page *pp,
1472 unsigned long offset,
1473 unsigned int count, int sync)
1475 struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1476 u8 *page_addr = (u8*) afs_linux_page_address(pp);
1482 set_bit(PG_locked, &pp->flags);
1486 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1487 ICL_TYPE_POINTER, pp,
1488 ICL_TYPE_INT32, atomic_read(&pp->count),
1489 ICL_TYPE_INT32, 99999);
1490 setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1491 count, UIO_WRITE, AFS_UIOSYS);
1493 code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1497 code = code ? -code : count - tuio.uio_resid;
1498 afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1499 ICL_TYPE_POINTER, pp,
1500 ICL_TYPE_INT32, atomic_read(&pp->count),
1501 ICL_TYPE_INT32, code);
1506 clear_bit(PG_locked, &pp->flags);
1511 #if defined(AFS_LINUX24_ENV)
1512 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1518 code = afs_linux_updatepage(file, page, offset, to-offset);
1526 static int afs_linux_prepare_write(struct file *file, struct page *page,
1527 unsigned from, unsigned to)
1533 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1536 #if defined(AFS_LINUX24_ENV)
1537 struct inode_operations afs_file_iops = {
1538 revalidate: afs_linux_revalidate,
1539 setattr: afs_notify_change,
1540 permission: afs_linux_permission,
1542 struct address_space_operations afs_file_aops = {
1543 readpage: afs_linux_readpage,
1544 writepage: afs_linux_writepage,
1545 commit_write: afs_linux_commit_write,
1546 prepare_write: afs_linux_prepare_write,
1549 struct inode_operations *afs_ops = &afs_file_iops;
1551 struct inode_operations afs_iops = {
1552 &afs_file_fops, /* file operations */
1553 NULL, /* afs_linux_create */
1554 NULL, /* afs_linux_lookup */
1555 NULL, /* afs_linux_link */
1556 NULL, /* afs_linux_unlink */
1557 NULL, /* afs_linux_symlink */
1558 NULL, /* afs_linux_mkdir */
1559 NULL, /* afs_linux_rmdir */
1560 NULL, /* afs_linux_mknod */
1561 NULL, /* afs_linux_rename */
1562 NULL, /* afs_linux_readlink */
1563 NULL, /* afs_linux_follow_link */
1565 NULL, /* afs_linux_writepage */
1566 NULL, /* afs_linux_bmap */
1567 NULL, /* afs_linux_truncate */
1568 afs_linux_permission,
1569 NULL, /* afs_linux_smap */
1570 afs_linux_updatepage,
1571 afs_linux_revalidate,
1574 struct inode_operations *afs_ops = &afs_iops;
1577 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1578 * by what sort of operation is allowed.....
1580 #if defined(AFS_LINUX24_ENV)
1581 struct inode_operations afs_dir_iops = {
1582 create: afs_linux_create,
1583 lookup: afs_linux_lookup,
1584 link: afs_linux_link,
1585 unlink: afs_linux_unlink,
1586 symlink: afs_linux_symlink,
1587 mkdir: afs_linux_mkdir,
1588 rmdir: afs_linux_rmdir,
1589 rename: afs_linux_rename,
1590 revalidate: afs_linux_revalidate,
1591 setattr: afs_notify_change,
1592 permission: afs_linux_permission,
1595 struct inode_operations afs_dir_iops = {
1596 &afs_dir_fops, /* file operations for directories */
1604 NULL, /* afs_linux_mknod */
1606 NULL, /* afs_linux_readlink */
1607 NULL, /* afs_linux_follow_link */
1608 NULL, /* afs_linux_readpage */
1609 NULL, /* afs_linux_writepage */
1610 NULL, /* afs_linux_bmap */
1611 NULL, /* afs_linux_truncate */
1612 afs_linux_permission,
1613 NULL, /* afs_linux_smap */
1614 NULL, /* afs_linux_updatepage */
1615 afs_linux_revalidate,
1619 /* We really need a separate symlink set of ops, since do_follow_link()
1620 * determines if it _is_ a link by checking if the follow_link op is set.
1622 #if defined(AFS_LINUX24_ENV)
1623 static int afs_symlink_filler(struct file *file, struct page *page)
1625 struct inode *ip = (struct inode *) page->mapping->host;
1626 char *p = (char *)kmap(page);
1631 code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1635 p[code] = '\0'; /* null terminate? */
1639 SetPageUptodate(page);
1654 struct address_space_operations afs_symlink_aops = {
1655 readpage: afs_symlink_filler
1658 struct inode_operations afs_symlink_iops = {
1659 readlink: page_readlink,
1660 follow_link: page_follow_link,
1661 setattr: afs_notify_change,
1664 struct inode_operations afs_symlink_iops = {
1665 NULL, /* file operations */
1673 NULL, /* afs_linux_mknod */
1676 afs_linux_follow_link,
1677 NULL, /* readpage */
1678 NULL, /* afs_linux_writepage */
1679 NULL, /* afs_linux_bmap */
1680 NULL, /* afs_linux_truncate */
1681 afs_linux_permission, /* tho the code appears to indicate not used? */
1682 NULL, /* afs_linux_smap */
1683 NULL, /* updatepage */
1684 afs_linux_revalidate, /* tho the code appears to indicate not used? */