1 #include <sys/sysmacros.h>
6 EXPORT_SYMBOL(rootdir
);
8 static kmem_cache_t
*vn_cache
;
9 static kmem_cache_t
*vn_file_cache
;
11 static spinlock_t vn_file_lock
= SPIN_LOCK_UNLOCKED
;
12 static LIST_HEAD(vn_file_list
);
15 vn_get_sol_type(umode_t mode
)
42 } /* vn_get_sol_type() */
49 vp
= kmem_cache_alloc(vn_cache
, flag
);
57 EXPORT_SYMBOL(vn_alloc
);
62 kmem_cache_free(vn_cache
, vp
);
64 EXPORT_SYMBOL(vn_free
);
67 vn_open(const char *path
, uio_seg_t seg
, int flags
, int mode
,
68 vnode_t
**vpp
, int x1
, void *x2
)
72 int rc
, saved_umask
, flags_rw
;
75 BUG_ON(seg
!= UIO_SYSSPACE
);
79 if (!(flags
& FCREAT
) && (flags
& FWRITE
))
82 flags_rw
= flags
& (FWRITE
| FREAD
);
83 flags
&= ~(FWRITE
| FREAD
);
85 case FWRITE
: flags
|= O_WRONLY
;
86 case FREAD
: flags
|= O_RDONLY
;
87 case (FWRITE
| FREAD
): flags
|= O_RDWR
;
91 saved_umask
= xchg(¤t
->fs
->umask
, 0);
93 fp
= filp_open(path
, flags
, mode
);
96 (void)xchg(¤t
->fs
->umask
, saved_umask
);
101 rc
= vfs_getattr(fp
->f_vfsmnt
, fp
->f_dentry
, &stat
);
107 vp
= vn_alloc(KM_SLEEP
);
113 mutex_enter(&vp
->v_lock
);
114 vp
->v_type
= vn_get_sol_type(stat
.mode
);
117 mutex_exit(&vp
->v_lock
);
121 EXPORT_SYMBOL(vn_open
);
124 vn_openat(const char *path
, uio_seg_t seg
, int flags
, int mode
,
125 vnode_t
**vpp
, int x1
, void *x2
, vnode_t
*vp
, int fd
)
130 BUG_ON(vp
!= rootdir
);
132 realpath
= kmalloc(strlen(path
) + 2, GFP_KERNEL
);
136 sprintf(realpath
, "/%s", path
);
137 rc
= vn_open(realpath
, seg
, flags
, mode
, vpp
, x1
, x2
);
143 EXPORT_SYMBOL(vn_openat
);
146 vn_rdwr(uio_rw_t uio
, vnode_t
*vp
, void *addr
, ssize_t len
, offset_t off
,
147 uio_seg_t seg
, int x1
, rlim64_t x2
, void *x3
, ssize_t
*residp
)
150 mm_segment_t saved_fs
;
154 BUG_ON(!(uio
== UIO_WRITE
|| uio
== UIO_READ
));
157 BUG_ON(seg
!= UIO_SYSSPACE
);
159 BUG_ON(x2
!= RLIM64_INFINITY
);
164 /* Writable user data segment must be briefly increased for this
165 * process so we can use the user space read call paths to write
166 * in to memory allocated by the kernel. */
171 rc
= vfs_write(fp
, addr
, len
, &offset
);
173 rc
= vfs_read(fp
, addr
, len
, &offset
);
189 EXPORT_SYMBOL(vn_rdwr
);
192 vn_close(vnode_t
*vp
, int flags
, int x1
, int x2
, void *x3
, void *x4
)
199 rc
= filp_close(vp
->v_file
, 0);
204 EXPORT_SYMBOL(vn_close
);
206 static struct dentry
*lookup_hash(struct nameidata
*nd
)
208 return __lookup_hash(&nd
->last
, nd
->dentry
, nd
);
209 } /* lookup_hash() */
211 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
213 vn_remove(const char *path
, uio_seg_t seg
, int flags
)
215 struct dentry
*dentry
;
217 struct inode
*inode
= NULL
;
220 BUG_ON(seg
!= UIO_SYSSPACE
);
221 BUG_ON(flags
!= RMFILE
);
223 rc
= path_lookup(path
, LOOKUP_PARENT
, &nd
);
228 if (nd
.last_type
!= LAST_NORM
)
231 mutex_lock_nested(&nd
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
232 dentry
= lookup_hash(&nd
);
233 rc
= PTR_ERR(dentry
);
234 if (!IS_ERR(dentry
)) {
235 /* Why not before? Because we want correct rc value */
236 if (nd
.last
.name
[nd
.last
.len
])
238 inode
= dentry
->d_inode
;
240 atomic_inc(&inode
->i_count
);
241 rc
= vfs_unlink(nd
.dentry
->d_inode
, dentry
);
245 mutex_unlock(&nd
.dentry
->d_inode
->i_mutex
);
247 iput(inode
); /* truncate the inode here */
254 rc
= !dentry
->d_inode
? -ENOENT
:
255 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
258 EXPORT_SYMBOL(vn_remove
);
260 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
262 vn_rename(const char *oldname
, const char *newname
, int x1
)
264 struct dentry
* old_dir
, * new_dir
;
265 struct dentry
* old_dentry
, *new_dentry
;
266 struct dentry
* trap
;
267 struct nameidata oldnd
, newnd
;
270 rc
= path_lookup(oldname
, LOOKUP_PARENT
, &oldnd
);
274 rc
= path_lookup(newname
, LOOKUP_PARENT
, &newnd
);
279 if (oldnd
.mnt
!= newnd
.mnt
)
282 old_dir
= oldnd
.dentry
;
284 if (oldnd
.last_type
!= LAST_NORM
)
287 new_dir
= newnd
.dentry
;
288 if (newnd
.last_type
!= LAST_NORM
)
291 trap
= lock_rename(new_dir
, old_dir
);
293 old_dentry
= lookup_hash(&oldnd
);
295 rc
= PTR_ERR(old_dentry
);
296 if (IS_ERR(old_dentry
))
299 /* source must exist */
301 if (!old_dentry
->d_inode
)
304 /* unless the source is a directory trailing slashes give -ENOTDIR */
305 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
307 if (oldnd
.last
.name
[oldnd
.last
.len
])
309 if (newnd
.last
.name
[newnd
.last
.len
])
313 /* source should not be ancestor of target */
315 if (old_dentry
== trap
)
318 new_dentry
= lookup_hash(&newnd
);
319 rc
= PTR_ERR(new_dentry
);
320 if (IS_ERR(new_dentry
))
323 /* target should not be an ancestor of source */
325 if (new_dentry
== trap
)
328 rc
= vfs_rename(old_dir
->d_inode
, old_dentry
,
329 new_dir
->d_inode
, new_dentry
);
335 unlock_rename(new_dir
, old_dir
);
337 path_release(&newnd
);
339 path_release(&oldnd
);
343 EXPORT_SYMBOL(vn_rename
);
346 vn_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, void *x3
, void *x4
)
358 rc
= vfs_getattr(fp
->f_vfsmnt
, fp
->f_dentry
, &stat
);
362 vap
->va_type
= vn_get_sol_type(stat
.mode
);
363 vap
->va_mode
= stat
.mode
;
364 vap
->va_uid
= stat
.uid
;
365 vap
->va_gid
= stat
.gid
;
367 vap
->va_nodeid
= stat
.ino
;
368 vap
->va_nlink
= stat
.nlink
;
369 vap
->va_size
= stat
.size
;
370 vap
->va_blocksize
= stat
.blksize
;
371 vap
->va_atime
.tv_sec
= stat
.atime
.tv_sec
;
372 vap
->va_atime
.tv_usec
= stat
.atime
.tv_nsec
/ NSEC_PER_USEC
;
373 vap
->va_mtime
.tv_sec
= stat
.mtime
.tv_sec
;
374 vap
->va_mtime
.tv_usec
= stat
.mtime
.tv_nsec
/ NSEC_PER_USEC
;
375 vap
->va_ctime
.tv_sec
= stat
.ctime
.tv_sec
;
376 vap
->va_ctime
.tv_usec
= stat
.ctime
.tv_nsec
/ NSEC_PER_USEC
;
377 vap
->va_rdev
= stat
.rdev
;
378 vap
->va_blocks
= stat
.blocks
;
382 EXPORT_SYMBOL(vn_getattr
);
384 int vn_fsync(vnode_t
*vp
, int flags
, void *x3
, void *x4
)
394 return file_fsync(vp
->v_file
, vp
->v_file
->f_dentry
, datasync
);
396 EXPORT_SYMBOL(vn_fsync
);
398 /* Function must be called while holding the vn_file_lock */
404 BUG_ON(!spin_is_locked(&vn_file_lock
));
406 list_for_each_entry(fp
, &vn_file_list
, f_list
) {
407 if (fd
== fp
->f_fd
) {
408 BUG_ON(atomic_read(&fp
->f_ref
) == 0);
424 /* Already open just take an extra reference */
425 spin_lock(&vn_file_lock
);
429 atomic_inc(&fp
->f_ref
);
430 spin_unlock(&vn_file_lock
);
431 printk("found file\n");
435 spin_unlock(&vn_file_lock
);
437 /* File was not yet opened create the object and setup */
438 fp
= kmem_cache_alloc(vn_file_cache
, 0);
442 mutex_enter(&fp
->f_lock
);
446 atomic_inc(&fp
->f_ref
);
452 vp
= vn_alloc(KM_SLEEP
);
456 if (vfs_getattr(lfp
->f_vfsmnt
, lfp
->f_dentry
, &stat
))
459 mutex_enter(&vp
->v_lock
);
460 vp
->v_type
= vn_get_sol_type(stat
.mode
);
462 mutex_exit(&vp
->v_lock
);
467 /* Put it on the tracking list */
468 spin_lock(&vn_file_lock
);
469 list_add(&fp
->f_list
, &vn_file_list
);
470 spin_unlock(&vn_file_lock
);
472 mutex_exit(&fp
->f_lock
);
476 printk("out_vnode\n");
479 printk("out_fget\n");
482 printk("out_mutex\n");
483 mutex_exit(&fp
->f_lock
);
484 kmem_cache_free(vn_file_cache
, fp
);
491 static void releasef_locked(file_t
*fp
)
493 BUG_ON(fp
->f_file
== NULL
);
494 BUG_ON(fp
->f_vnode
== NULL
);
496 /* Unlinked from list, no refs, safe to free outside mutex */
498 vn_free(fp
->f_vnode
);
500 kmem_cache_free(vn_file_cache
, fp
);
508 spin_lock(&vn_file_lock
);
511 atomic_dec(&fp
->f_ref
);
512 if (atomic_read(&fp
->f_ref
) > 0) {
513 spin_unlock(&vn_file_lock
);
517 list_del(&fp
->f_list
);
520 spin_unlock(&vn_file_lock
);
524 EXPORT_SYMBOL(releasef
);
527 vn_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
529 struct vnode
*vp
= buf
;
531 mutex_init(&vp
->v_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
534 } /* vn_cache_constructor() */
537 vn_cache_destructor(void *buf
, void *cdrarg
)
539 struct vnode
*vp
= buf
;
541 mutex_destroy(&vp
->v_lock
);
542 } /* vn_cache_destructor() */
545 vn_file_cache_constructor(void *buf
, void *cdrarg
, int kmflags
)
549 atomic_set(&fp
->f_ref
, 0);
550 mutex_init(&fp
->f_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
553 } /* file_cache_constructor() */
556 vn_file_cache_destructor(void *buf
, void *cdrarg
)
560 mutex_destroy(&fp
->f_lock
);
561 } /* vn_file_cache_destructor() */
566 vn_cache
= kmem_cache_create("spl_vn_cache", sizeof(struct vnode
), 64,
567 vn_cache_constructor
,
569 NULL
, NULL
, NULL
, 0);
571 vn_file_cache
= kmem_cache_create("spl_vn_file_cache",
573 vn_file_cache_constructor
,
574 vn_file_cache_destructor
,
575 NULL
, NULL
, NULL
, 0);
582 file_t
*fp
, *next_fp
;
585 spin_lock(&vn_file_lock
);
587 list_for_each_entry_safe(fp
, next_fp
, &vn_file_list
, f_list
) {
588 list_del(&fp
->f_list
);
593 rc
= kmem_cache_destroy(vn_file_cache
);
595 printk("Warning leaked vn_file_cache objects\n");
597 vn_file_cache
= NULL
;
598 spin_unlock(&vn_file_lock
);
601 printk("Warning: %d files leaked\n", leaked
);
603 rc
= kmem_cache_destroy(vn_cache
);
605 printk("Warning leaked vn_cache objects\n");