]> git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-vnode.c
Split <sys/debug.h> header
[mirror_spl-debian.git] / module / spl / spl-vnode.c
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Vnode Implementation.
25 \*****************************************************************************/
26
27 #include <sys/vnode.h>
28 #include <spl-debug.h>
29
30 #ifdef DEBUG_SUBSYSTEM
31 #undef DEBUG_SUBSYSTEM
32 #endif
33
34 #define DEBUG_SUBSYSTEM S_VNODE
35
36 vnode_t *rootdir = (vnode_t *)0xabcd1234;
37 EXPORT_SYMBOL(rootdir);
38
39 static spl_kmem_cache_t *vn_cache;
40 static spl_kmem_cache_t *vn_file_cache;
41
42 static spinlock_t vn_file_lock = SPIN_LOCK_UNLOCKED;
43 static LIST_HEAD(vn_file_list);
44
45 static vtype_t
46 vn_get_sol_type(umode_t mode)
47 {
48 if (S_ISREG(mode))
49 return VREG;
50
51 if (S_ISDIR(mode))
52 return VDIR;
53
54 if (S_ISCHR(mode))
55 return VCHR;
56
57 if (S_ISBLK(mode))
58 return VBLK;
59
60 if (S_ISFIFO(mode))
61 return VFIFO;
62
63 if (S_ISLNK(mode))
64 return VLNK;
65
66 if (S_ISSOCK(mode))
67 return VSOCK;
68
69 if (S_ISCHR(mode))
70 return VCHR;
71
72 return VNON;
73 } /* vn_get_sol_type() */
74
75 vnode_t *
76 vn_alloc(int flag)
77 {
78 vnode_t *vp;
79 ENTRY;
80
81 vp = kmem_cache_alloc(vn_cache, flag);
82 if (vp != NULL) {
83 vp->v_file = NULL;
84 vp->v_type = 0;
85 }
86
87 RETURN(vp);
88 } /* vn_alloc() */
89 EXPORT_SYMBOL(vn_alloc);
90
91 void
92 vn_free(vnode_t *vp)
93 {
94 ENTRY;
95 kmem_cache_free(vn_cache, vp);
96 EXIT;
97 } /* vn_free() */
98 EXPORT_SYMBOL(vn_free);
99
100 int
101 vn_open(const char *path, uio_seg_t seg, int flags, int mode,
102 vnode_t **vpp, int x1, void *x2)
103 {
104 struct file *fp;
105 struct kstat stat;
106 int rc, saved_umask = 0;
107 vnode_t *vp;
108 ENTRY;
109
110 ASSERT(flags & (FWRITE | FREAD));
111 ASSERT(seg == UIO_SYSSPACE);
112 ASSERT(vpp);
113 *vpp = NULL;
114
115 if (!(flags & FCREAT) && (flags & FWRITE))
116 flags |= FEXCL;
117
118 /* Note for filp_open() the two low bits must be remapped to mean:
119 * 01 - read-only -> 00 read-only
120 * 10 - write-only -> 01 write-only
121 * 11 - read-write -> 10 read-write
122 */
123 flags--;
124
125 if (flags & FCREAT)
126 saved_umask = xchg(&current->fs->umask, 0);
127
128 fp = filp_open(path, flags, mode);
129
130 if (flags & FCREAT)
131 (void)xchg(&current->fs->umask, saved_umask);
132
133 if (IS_ERR(fp))
134 RETURN(-PTR_ERR(fp));
135
136 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
137 if (rc) {
138 filp_close(fp, 0);
139 RETURN(-rc);
140 }
141
142 vp = vn_alloc(KM_SLEEP);
143 if (!vp) {
144 filp_close(fp, 0);
145 RETURN(ENOMEM);
146 }
147
148 mutex_enter(&vp->v_lock);
149 vp->v_type = vn_get_sol_type(stat.mode);
150 vp->v_file = fp;
151 *vpp = vp;
152 mutex_exit(&vp->v_lock);
153
154 RETURN(0);
155 } /* vn_open() */
156 EXPORT_SYMBOL(vn_open);
157
158 int
159 vn_openat(const char *path, uio_seg_t seg, int flags, int mode,
160 vnode_t **vpp, int x1, void *x2, vnode_t *vp, int fd)
161 {
162 char *realpath;
163 int len, rc;
164 ENTRY;
165
166 ASSERT(vp == rootdir);
167
168 len = strlen(path) + 2;
169 realpath = kmalloc(len, GFP_KERNEL);
170 if (!realpath)
171 RETURN(ENOMEM);
172
173 (void)snprintf(realpath, len, "/%s", path);
174 rc = vn_open(realpath, seg, flags, mode, vpp, x1, x2);
175 kfree(realpath);
176
177 RETURN(rc);
178 } /* vn_openat() */
179 EXPORT_SYMBOL(vn_openat);
180
181 int
182 vn_rdwr(uio_rw_t uio, vnode_t *vp, void *addr, ssize_t len, offset_t off,
183 uio_seg_t seg, int ioflag, rlim64_t x2, void *x3, ssize_t *residp)
184 {
185 loff_t offset;
186 mm_segment_t saved_fs;
187 struct file *fp;
188 int rc;
189 ENTRY;
190
191 ASSERT(uio == UIO_WRITE || uio == UIO_READ);
192 ASSERT(vp);
193 ASSERT(vp->v_file);
194 ASSERT(seg == UIO_SYSSPACE);
195 ASSERT((ioflag & ~FAPPEND) == 0);
196 ASSERT(x2 == RLIM64_INFINITY);
197
198 fp = vp->v_file;
199
200 offset = off;
201 if (ioflag & FAPPEND)
202 offset = fp->f_pos;
203
204 /* Writable user data segment must be briefly increased for this
205 * process so we can use the user space read call paths to write
206 * in to memory allocated by the kernel. */
207 saved_fs = get_fs();
208 set_fs(get_ds());
209
210 if (uio & UIO_WRITE)
211 rc = vfs_write(fp, addr, len, &offset);
212 else
213 rc = vfs_read(fp, addr, len, &offset);
214
215 set_fs(saved_fs);
216
217 if (rc < 0)
218 RETURN(-rc);
219
220 if (residp) {
221 *residp = len - rc;
222 } else {
223 if (rc != len)
224 RETURN(EIO);
225 }
226
227 RETURN(0);
228 } /* vn_rdwr() */
229 EXPORT_SYMBOL(vn_rdwr);
230
231 int
232 vn_close(vnode_t *vp, int flags, int x1, int x2, void *x3, void *x4)
233 {
234 int rc;
235 ENTRY;
236
237 ASSERT(vp);
238 ASSERT(vp->v_file);
239
240 rc = filp_close(vp->v_file, 0);
241 vn_free(vp);
242
243 RETURN(-rc);
244 } /* vn_close() */
245 EXPORT_SYMBOL(vn_close);
246
247 /* vn_seek() does not actually seek it only performs bounds checking on the
248 * proposed seek. We perform minimal checking and allow vn_rdwr() to catch
249 * anything more serious. */
250 int
251 vn_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
252 {
253 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
254 }
255 EXPORT_SYMBOL(vn_seek);
256
257 static struct dentry *
258 vn_lookup_hash(struct nameidata *nd)
259 {
260 return lookup_one_len(nd->last.name, nd->nd_dentry, nd->last.len);
261 } /* lookup_hash() */
262
263 static void
264 vn_path_release(struct nameidata *nd)
265 {
266 dput(nd->nd_dentry);
267 mntput(nd->nd_mnt);
268 }
269
270 /* Modified do_unlinkat() from linux/fs/namei.c, only uses exported symbols */
271 int
272 vn_remove(const char *path, uio_seg_t seg, int flags)
273 {
274 struct dentry *dentry;
275 struct nameidata nd;
276 struct inode *inode = NULL;
277 int rc = 0;
278 ENTRY;
279
280 ASSERT(seg == UIO_SYSSPACE);
281 ASSERT(flags == RMFILE);
282
283 rc = path_lookup(path, LOOKUP_PARENT, &nd);
284 if (rc)
285 GOTO(exit, rc);
286
287 rc = -EISDIR;
288 if (nd.last_type != LAST_NORM)
289 GOTO(exit1, rc);
290
291 #ifdef HAVE_INODE_I_MUTEX
292 mutex_lock_nested(&nd.nd_dentry->d_inode->i_mutex, I_MUTEX_PARENT);
293 #else
294 down(&nd.nd_dentry->d_inode->i_sem);
295 #endif /* HAVE_INODE_I_MUTEX */
296 dentry = vn_lookup_hash(&nd);
297 rc = PTR_ERR(dentry);
298 if (!IS_ERR(dentry)) {
299 /* Why not before? Because we want correct rc value */
300 if (nd.last.name[nd.last.len])
301 GOTO(slashes, rc);
302
303 inode = dentry->d_inode;
304 if (inode)
305 atomic_inc(&inode->i_count);
306 #ifdef HAVE_2ARGS_VFS_UNLINK
307 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry);
308 #else
309 rc = vfs_unlink(nd.nd_dentry->d_inode, dentry, nd.nd_mnt);
310 #endif /* HAVE_2ARGS_VFS_UNLINK */
311 exit2:
312 dput(dentry);
313 }
314 #ifdef HAVE_INODE_I_MUTEX
315 mutex_unlock(&nd.nd_dentry->d_inode->i_mutex);
316 #else
317 up(&nd.nd_dentry->d_inode->i_sem);
318 #endif /* HAVE_INODE_I_MUTEX */
319 if (inode)
320 iput(inode); /* truncate the inode here */
321 exit1:
322 vn_path_release(&nd);
323 exit:
324 RETURN(-rc);
325
326 slashes:
327 rc = !dentry->d_inode ? -ENOENT :
328 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
329 GOTO(exit2, rc);
330 } /* vn_remove() */
331 EXPORT_SYMBOL(vn_remove);
332
333 /* Modified do_rename() from linux/fs/namei.c, only uses exported symbols */
334 int
335 vn_rename(const char *oldname, const char *newname, int x1)
336 {
337 struct dentry *old_dir, *new_dir;
338 struct dentry *old_dentry, *new_dentry;
339 struct dentry *trap;
340 struct nameidata oldnd, newnd;
341 int rc = 0;
342 ENTRY;
343
344 rc = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
345 if (rc)
346 GOTO(exit, rc);
347
348 rc = path_lookup(newname, LOOKUP_PARENT, &newnd);
349 if (rc)
350 GOTO(exit1, rc);
351
352 rc = -EXDEV;
353 if (oldnd.nd_mnt != newnd.nd_mnt)
354 GOTO(exit2, rc);
355
356 old_dir = oldnd.nd_dentry;
357 rc = -EBUSY;
358 if (oldnd.last_type != LAST_NORM)
359 GOTO(exit2, rc);
360
361 new_dir = newnd.nd_dentry;
362 if (newnd.last_type != LAST_NORM)
363 GOTO(exit2, rc);
364
365 trap = lock_rename(new_dir, old_dir);
366
367 old_dentry = vn_lookup_hash(&oldnd);
368
369 rc = PTR_ERR(old_dentry);
370 if (IS_ERR(old_dentry))
371 GOTO(exit3, rc);
372
373 /* source must exist */
374 rc = -ENOENT;
375 if (!old_dentry->d_inode)
376 GOTO(exit4, rc);
377
378 /* unless the source is a directory trailing slashes give -ENOTDIR */
379 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
380 rc = -ENOTDIR;
381 if (oldnd.last.name[oldnd.last.len])
382 GOTO(exit4, rc);
383 if (newnd.last.name[newnd.last.len])
384 GOTO(exit4, rc);
385 }
386
387 /* source should not be ancestor of target */
388 rc = -EINVAL;
389 if (old_dentry == trap)
390 GOTO(exit4, rc);
391
392 new_dentry = vn_lookup_hash(&newnd);
393 rc = PTR_ERR(new_dentry);
394 if (IS_ERR(new_dentry))
395 GOTO(exit4, rc);
396
397 /* target should not be an ancestor of source */
398 rc = -ENOTEMPTY;
399 if (new_dentry == trap)
400 GOTO(exit5, rc);
401
402 #ifdef HAVE_4ARGS_VFS_RENAME
403 rc = vfs_rename(old_dir->d_inode, old_dentry,
404 new_dir->d_inode, new_dentry);
405 #else
406 rc = vfs_rename(old_dir->d_inode, old_dentry, oldnd.nd_mnt,
407 new_dir->d_inode, new_dentry, newnd.nd_mnt);
408 #endif /* HAVE_4ARGS_VFS_RENAME */
409 exit5:
410 dput(new_dentry);
411 exit4:
412 dput(old_dentry);
413 exit3:
414 unlock_rename(new_dir, old_dir);
415 exit2:
416 vn_path_release(&newnd);
417 exit1:
418 vn_path_release(&oldnd);
419 exit:
420 RETURN(-rc);
421 }
422 EXPORT_SYMBOL(vn_rename);
423
424 int
425 vn_getattr(vnode_t *vp, vattr_t *vap, int flags, void *x3, void *x4)
426 {
427 struct file *fp;
428 struct kstat stat;
429 int rc;
430 ENTRY;
431
432 ASSERT(vp);
433 ASSERT(vp->v_file);
434 ASSERT(vap);
435
436 fp = vp->v_file;
437
438 rc = vfs_getattr(fp->f_vfsmnt, fp->f_dentry, &stat);
439 if (rc)
440 RETURN(-rc);
441
442 vap->va_type = vn_get_sol_type(stat.mode);
443 vap->va_mode = stat.mode;
444 vap->va_uid = stat.uid;
445 vap->va_gid = stat.gid;
446 vap->va_fsid = 0;
447 vap->va_nodeid = stat.ino;
448 vap->va_nlink = stat.nlink;
449 vap->va_size = stat.size;
450 vap->va_blocksize = stat.blksize;
451 vap->va_atime.tv_sec = stat.atime.tv_sec;
452 vap->va_atime.tv_usec = stat.atime.tv_nsec / NSEC_PER_USEC;
453 vap->va_mtime.tv_sec = stat.mtime.tv_sec;
454 vap->va_mtime.tv_usec = stat.mtime.tv_nsec / NSEC_PER_USEC;
455 vap->va_ctime.tv_sec = stat.ctime.tv_sec;
456 vap->va_ctime.tv_usec = stat.ctime.tv_nsec / NSEC_PER_USEC;
457 vap->va_rdev = stat.rdev;
458 vap->va_blocks = stat.blocks;
459
460 RETURN(0);
461 }
462 EXPORT_SYMBOL(vn_getattr);
463
464 int vn_fsync(vnode_t *vp, int flags, void *x3, void *x4)
465 {
466 int datasync = 0;
467 ENTRY;
468
469 ASSERT(vp);
470 ASSERT(vp->v_file);
471
472 if (flags & FDSYNC)
473 datasync = 1;
474
475 RETURN(-spl_filp_fsync(vp->v_file, datasync));
476 } /* vn_fsync() */
477 EXPORT_SYMBOL(vn_fsync);
478
479 /* Function must be called while holding the vn_file_lock */
480 static file_t *
481 file_find(int fd)
482 {
483 file_t *fp;
484
485 ASSERT(spin_is_locked(&vn_file_lock));
486
487 list_for_each_entry(fp, &vn_file_list, f_list) {
488 if (fd == fp->f_fd) {
489 ASSERT(atomic_read(&fp->f_ref) != 0);
490 return fp;
491 }
492 }
493
494 return NULL;
495 } /* file_find() */
496
497 file_t *
498 vn_getf(int fd)
499 {
500 struct kstat stat;
501 struct file *lfp;
502 file_t *fp;
503 vnode_t *vp;
504 int rc = 0;
505 ENTRY;
506
507 /* Already open just take an extra reference */
508 spin_lock(&vn_file_lock);
509
510 fp = file_find(fd);
511 if (fp) {
512 atomic_inc(&fp->f_ref);
513 spin_unlock(&vn_file_lock);
514 RETURN(fp);
515 }
516
517 spin_unlock(&vn_file_lock);
518
519 /* File was not yet opened create the object and setup */
520 fp = kmem_cache_alloc(vn_file_cache, KM_SLEEP);
521 if (fp == NULL)
522 GOTO(out, rc);
523
524 mutex_enter(&fp->f_lock);
525
526 fp->f_fd = fd;
527 fp->f_offset = 0;
528 atomic_inc(&fp->f_ref);
529
530 lfp = fget(fd);
531 if (lfp == NULL)
532 GOTO(out_mutex, rc);
533
534 vp = vn_alloc(KM_SLEEP);
535 if (vp == NULL)
536 GOTO(out_fget, rc);
537
538 if (vfs_getattr(lfp->f_vfsmnt, lfp->f_dentry, &stat))
539 GOTO(out_vnode, rc);
540
541 mutex_enter(&vp->v_lock);
542 vp->v_type = vn_get_sol_type(stat.mode);
543 vp->v_file = lfp;
544 mutex_exit(&vp->v_lock);
545
546 fp->f_vnode = vp;
547 fp->f_file = lfp;
548
549 /* Put it on the tracking list */
550 spin_lock(&vn_file_lock);
551 list_add(&fp->f_list, &vn_file_list);
552 spin_unlock(&vn_file_lock);
553
554 mutex_exit(&fp->f_lock);
555 RETURN(fp);
556
557 out_vnode:
558 vn_free(vp);
559 out_fget:
560 fput(lfp);
561 out_mutex:
562 mutex_exit(&fp->f_lock);
563 kmem_cache_free(vn_file_cache, fp);
564 out:
565 RETURN(NULL);
566 } /* getf() */
567 EXPORT_SYMBOL(getf);
568
569 static void releasef_locked(file_t *fp)
570 {
571 ASSERT(fp->f_file);
572 ASSERT(fp->f_vnode);
573
574 /* Unlinked from list, no refs, safe to free outside mutex */
575 fput(fp->f_file);
576 vn_free(fp->f_vnode);
577
578 kmem_cache_free(vn_file_cache, fp);
579 }
580
581 void
582 vn_releasef(int fd)
583 {
584 file_t *fp;
585 ENTRY;
586
587 spin_lock(&vn_file_lock);
588 fp = file_find(fd);
589 if (fp) {
590 atomic_dec(&fp->f_ref);
591 if (atomic_read(&fp->f_ref) > 0) {
592 spin_unlock(&vn_file_lock);
593 EXIT;
594 return;
595 }
596
597 list_del(&fp->f_list);
598 releasef_locked(fp);
599 }
600 spin_unlock(&vn_file_lock);
601
602 EXIT;
603 return;
604 } /* releasef() */
605 EXPORT_SYMBOL(releasef);
606
607 #ifndef HAVE_SET_FS_PWD
608 # ifdef HAVE_2ARGS_SET_FS_PWD
609 /* Used from 2.6.25 - 2.6.31+ */
610 void
611 set_fs_pwd(struct fs_struct *fs, struct path *path)
612 {
613 struct path old_pwd;
614
615 write_lock(&fs->lock);
616 old_pwd = fs->pwd;
617 fs->pwd = *path;
618 path_get(path);
619 write_unlock(&fs->lock);
620
621 if (old_pwd.dentry)
622 path_put(&old_pwd);
623 }
624 # else
625 /* Used from 2.6.11 - 2.6.24 */
626 void
627 set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt, struct dentry *dentry)
628 {
629 struct dentry *old_pwd;
630 struct vfsmount *old_pwdmnt;
631
632 write_lock(&fs->lock);
633 old_pwd = fs->pwd;
634 old_pwdmnt = fs->pwdmnt;
635 fs->pwdmnt = mntget(mnt);
636 fs->pwd = dget(dentry);
637 write_unlock(&fs->lock);
638
639 if (old_pwd) {
640 dput(old_pwd);
641 mntput(old_pwdmnt);
642 }
643 }
644 # endif /* HAVE_2ARGS_SET_FS_PWD */
645 #endif /* HAVE_SET_FS_PWD */
646
647 int
648 vn_set_pwd(const char *filename)
649 {
650 #if defined(HAVE_2ARGS_SET_FS_PWD) && defined(HAVE_USER_PATH_DIR)
651 struct path path;
652 #else
653 struct nameidata nd;
654 #endif /* HAVE_2ARGS_SET_FS_PWD */
655 mm_segment_t saved_fs;
656 int rc;
657 ENTRY;
658
659 /*
660 * user_path_dir() and __user_walk() both expect 'filename' to be
661 * a user space address so we must briefly increase the data segment
662 * size to ensure strncpy_from_user() does not fail with -EFAULT.
663 */
664 saved_fs = get_fs();
665 set_fs(get_ds());
666
667 #ifdef HAVE_2ARGS_SET_FS_PWD
668 # ifdef HAVE_USER_PATH_DIR
669 rc = user_path_dir(filename, &path);
670 if (rc)
671 GOTO(out, rc);
672
673 rc = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
674 if (rc)
675 GOTO(dput_and_out, rc);
676
677 set_fs_pwd(current->fs, &path);
678
679 dput_and_out:
680 path_put(&path);
681 # else
682 rc = __user_walk(filename,
683 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
684 if (rc)
685 GOTO(out, rc);
686
687 rc = vfs_permission(&nd, MAY_EXEC);
688 if (rc)
689 GOTO(dput_and_out, rc);
690
691 set_fs_pwd(current->fs, &nd.path);
692
693 dput_and_out:
694 path_put(&nd.path);
695 # endif /* HAVE_USER_PATH_DIR */
696 #else
697 rc = __user_walk(filename,
698 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
699 if (rc)
700 GOTO(out, rc);
701
702 rc = vfs_permission(&nd, MAY_EXEC);
703 if (rc)
704 GOTO(dput_and_out, rc);
705
706 set_fs_pwd(current->fs, nd.nd_mnt, nd.nd_dentry);
707
708 dput_and_out:
709 vn_path_release(&nd);
710 #endif /* HAVE_2ARGS_SET_FS_PWD */
711 out:
712 set_fs(saved_fs);
713
714 RETURN(-rc);
715 } /* vn_set_pwd() */
716 EXPORT_SYMBOL(vn_set_pwd);
717
718 static int
719 vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
720 {
721 struct vnode *vp = buf;
722
723 mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
724
725 return (0);
726 } /* vn_cache_constructor() */
727
728 static void
729 vn_cache_destructor(void *buf, void *cdrarg)
730 {
731 struct vnode *vp = buf;
732
733 mutex_destroy(&vp->v_lock);
734 } /* vn_cache_destructor() */
735
736 static int
737 vn_file_cache_constructor(void *buf, void *cdrarg, int kmflags)
738 {
739 file_t *fp = buf;
740
741 atomic_set(&fp->f_ref, 0);
742 mutex_init(&fp->f_lock, NULL, MUTEX_DEFAULT, NULL);
743 INIT_LIST_HEAD(&fp->f_list);
744
745 return (0);
746 } /* file_cache_constructor() */
747
748 static void
749 vn_file_cache_destructor(void *buf, void *cdrarg)
750 {
751 file_t *fp = buf;
752
753 mutex_destroy(&fp->f_lock);
754 } /* vn_file_cache_destructor() */
755
756 int
757 vn_init(void)
758 {
759 ENTRY;
760 vn_cache = kmem_cache_create("spl_vn_cache",
761 sizeof(struct vnode), 64,
762 vn_cache_constructor,
763 vn_cache_destructor,
764 NULL, NULL, NULL, 0);
765
766 vn_file_cache = kmem_cache_create("spl_vn_file_cache",
767 sizeof(file_t), 64,
768 vn_file_cache_constructor,
769 vn_file_cache_destructor,
770 NULL, NULL, NULL, 0);
771 RETURN(0);
772 } /* vn_init() */
773
774 void
775 vn_fini(void)
776 {
777 file_t *fp, *next_fp;
778 int leaked = 0;
779 ENTRY;
780
781 spin_lock(&vn_file_lock);
782
783 list_for_each_entry_safe(fp, next_fp, &vn_file_list, f_list) {
784 list_del(&fp->f_list);
785 releasef_locked(fp);
786 leaked++;
787 }
788
789 kmem_cache_destroy(vn_file_cache);
790 vn_file_cache = NULL;
791 spin_unlock(&vn_file_lock);
792
793 if (leaked > 0)
794 CWARN("Warning %d files leaked\n", leaked);
795
796 kmem_cache_destroy(vn_cache);
797
798 EXIT;
799 return;
800 } /* vn_fini() */