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