4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/sysmacros.h>
35 #include <sys/vnode.h>
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
39 #include <sys/cmn_err.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/zfs_dir.h>
43 #include <sys/fs/zfs.h>
45 #include <sys/dsl_prop.h>
46 #include <sys/dsl_dataset.h>
49 #include <sys/varargs.h>
50 #include <sys/policy.h>
51 #include <sys/atomic.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_ctldir.h>
54 #include <sys/sunddi.h>
57 struct mtx zfs_debug_mtx;
58 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
59 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
60 int zfs_debug_level = 0;
61 TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level);
62 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0,
65 static int zfs_mount(vfs_t *vfsp, kthread_t *td);
66 static int zfs_umount(vfs_t *vfsp, int fflag, kthread_t *td);
67 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp, kthread_t *td);
68 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp, kthread_t *td);
69 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
70 static int zfs_sync(vfs_t *vfsp, int waitfor, kthread_t *td);
71 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp);
72 static void zfs_objset_close(zfsvfs_t *zfsvfs);
73 static void zfs_freevfs(vfs_t *vfsp);
75 static struct vfsops zfs_vfsops = {
76 .vfs_mount = zfs_mount,
77 .vfs_unmount = zfs_umount,
79 .vfs_statfs = zfs_statfs,
82 .vfs_fhtovp = zfs_fhtovp,
85 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL);
88 * We need to keep a count of active fs's.
89 * This is necessary to prevent our module
90 * from being unloaded after a umount -f
92 static uint32_t zfs_active_fs_count = 0;
96 zfs_sync(vfs_t *vfsp, int waitfor, kthread_t *td)
100 * Data integrity is job one. We don't want a compromised kernel
101 * writing to the storage pool, so we never sync during panic.
108 * Sync a specific filesystem.
110 zfsvfs_t *zfsvfs = vfsp->vfs_data;
113 error = vfs_stdsync(vfsp, waitfor, td);
118 if (zfsvfs->z_log != NULL)
119 zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
121 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
125 * Sync all ZFS filesystems. This is what happens when you
126 * run sync(1M). Unlike other filesystems, ZFS honors the
127 * request by waiting for all pools to commit all dirty data.
136 atime_changed_cb(void *arg, uint64_t newval)
138 zfsvfs_t *zfsvfs = arg;
140 if (newval == TRUE) {
141 zfsvfs->z_atime = TRUE;
142 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
143 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
144 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
146 zfsvfs->z_atime = FALSE;
147 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
148 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
149 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
154 xattr_changed_cb(void *arg, uint64_t newval)
156 zfsvfs_t *zfsvfs = arg;
158 if (newval == TRUE) {
159 /* XXX locking on vfs_flag? */
161 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
163 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
164 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
166 /* XXX locking on vfs_flag? */
168 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
170 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
171 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
176 blksz_changed_cb(void *arg, uint64_t newval)
178 zfsvfs_t *zfsvfs = arg;
180 if (newval < SPA_MINBLOCKSIZE ||
181 newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
182 newval = SPA_MAXBLOCKSIZE;
184 zfsvfs->z_max_blksz = newval;
185 zfsvfs->z_vfs->vfs_bsize = newval;
189 readonly_changed_cb(void *arg, uint64_t newval)
191 zfsvfs_t *zfsvfs = arg;
194 /* XXX locking on vfs_flag? */
195 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
196 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
197 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
199 /* XXX locking on vfs_flag? */
200 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
201 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
202 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
207 setuid_changed_cb(void *arg, uint64_t newval)
209 zfsvfs_t *zfsvfs = arg;
211 if (newval == FALSE) {
212 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
213 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
214 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
216 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
217 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
218 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
223 exec_changed_cb(void *arg, uint64_t newval)
225 zfsvfs_t *zfsvfs = arg;
227 if (newval == FALSE) {
228 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
229 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
230 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
232 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
233 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
234 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
239 snapdir_changed_cb(void *arg, uint64_t newval)
241 zfsvfs_t *zfsvfs = arg;
243 zfsvfs->z_show_ctldir = newval;
247 acl_mode_changed_cb(void *arg, uint64_t newval)
249 zfsvfs_t *zfsvfs = arg;
251 zfsvfs->z_acl_mode = newval;
255 acl_inherit_changed_cb(void *arg, uint64_t newval)
257 zfsvfs_t *zfsvfs = arg;
259 zfsvfs->z_acl_inherit = newval;
263 zfs_refresh_properties(vfs_t *vfsp)
265 zfsvfs_t *zfsvfs = vfsp->vfs_data;
268 * Remount operations default to "rw" unless "ro" is explicitly
271 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
272 readonly_changed_cb(zfsvfs, B_TRUE);
274 if (!dmu_objset_is_snapshot(zfsvfs->z_os))
275 readonly_changed_cb(zfsvfs, B_FALSE);
276 else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL))
280 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
281 setuid_changed_cb(zfsvfs, B_FALSE);
283 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))
284 setuid_changed_cb(zfsvfs, B_FALSE);
285 else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL))
286 setuid_changed_cb(zfsvfs, B_TRUE);
289 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL))
290 exec_changed_cb(zfsvfs, B_FALSE);
291 else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL))
292 exec_changed_cb(zfsvfs, B_TRUE);
294 if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL))
295 atime_changed_cb(zfsvfs, B_TRUE);
296 else if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
297 atime_changed_cb(zfsvfs, B_FALSE);
299 if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL))
300 xattr_changed_cb(zfsvfs, B_TRUE);
301 else if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL))
302 xattr_changed_cb(zfsvfs, B_FALSE);
308 zfs_register_callbacks(vfs_t *vfsp)
310 struct dsl_dataset *ds = NULL;
312 zfsvfs_t *zfsvfs = NULL;
313 int readonly, do_readonly = FALSE;
314 int setuid, do_setuid = FALSE;
315 int exec, do_exec = FALSE;
316 int xattr, do_xattr = FALSE;
320 zfsvfs = vfsp->vfs_data;
325 * The act of registering our callbacks will destroy any mount
326 * options we may have. In order to enable temporary overrides
327 * of mount options, we stash away the current values and
328 * restore them after we register the callbacks.
330 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
332 do_readonly = B_TRUE;
333 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
335 do_readonly = B_TRUE;
337 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
341 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
344 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
349 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
352 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
356 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
359 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
365 * Register property callbacks.
367 * It would probably be fine to just check for i/o error from
368 * the first prop_register(), but I guess I like to go
371 ds = dmu_objset_ds(os);
372 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
373 error = error ? error : dsl_prop_register(ds,
374 "xattr", xattr_changed_cb, zfsvfs);
375 error = error ? error : dsl_prop_register(ds,
376 "recordsize", blksz_changed_cb, zfsvfs);
377 error = error ? error : dsl_prop_register(ds,
378 "readonly", readonly_changed_cb, zfsvfs);
379 error = error ? error : dsl_prop_register(ds,
380 "setuid", setuid_changed_cb, zfsvfs);
381 error = error ? error : dsl_prop_register(ds,
382 "exec", exec_changed_cb, zfsvfs);
383 error = error ? error : dsl_prop_register(ds,
384 "snapdir", snapdir_changed_cb, zfsvfs);
385 error = error ? error : dsl_prop_register(ds,
386 "aclmode", acl_mode_changed_cb, zfsvfs);
387 error = error ? error : dsl_prop_register(ds,
388 "aclinherit", acl_inherit_changed_cb, zfsvfs);
393 * Invoke our callbacks to restore temporary mount options.
396 readonly_changed_cb(zfsvfs, readonly);
398 setuid_changed_cb(zfsvfs, setuid);
400 exec_changed_cb(zfsvfs, exec);
402 xattr_changed_cb(zfsvfs, xattr);
408 * We may attempt to unregister some callbacks that are not
409 * registered, but this is OK; it will simply return ENOMSG,
410 * which we will ignore.
412 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
413 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
414 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
415 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
416 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
417 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
418 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
419 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
420 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
427 zfs_domount(vfs_t *vfsp, char *osname, kthread_t *td)
429 cred_t *cr = td->td_ucred;
430 uint64_t recordsize, readonly;
440 * Initialize the zfs-specific filesystem structure.
441 * Should probably make this a kmem cache, shuffle fields,
442 * and just bzero up to z_hold_mtx[].
444 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
445 zfsvfs->z_vfs = vfsp;
446 zfsvfs->z_parent = zfsvfs;
447 zfsvfs->z_assign = TXG_NOWAIT;
448 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
449 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
451 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
452 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
453 offsetof(znode_t, z_link_node));
454 rw_init(&zfsvfs->z_um_lock, NULL, RW_DEFAULT, NULL);
456 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
459 zfsvfs->z_vfs->vfs_bsize = recordsize;
461 vfsp->vfs_data = zfsvfs;
462 vfsp->mnt_flag |= MNT_LOCAL;
463 vfsp->mnt_kern_flag |= MNTK_MPSAFE;
464 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
466 if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL))
470 mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
472 mode = DS_MODE_PRIMARY;
474 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os);
475 if (error == EROFS) {
476 mode = DS_MODE_PRIMARY | DS_MODE_READONLY;
477 error = dmu_objset_open(osname, DMU_OST_ZFS, mode,
484 if (error = zfs_init_fs(zfsvfs, &zp, cr))
487 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
490 ASSERT(mode & DS_MODE_READONLY);
491 atime_changed_cb(zfsvfs, B_FALSE);
492 readonly_changed_cb(zfsvfs, B_TRUE);
493 if (error = dsl_prop_get_integer(osname, "xattr", &xattr, NULL))
495 xattr_changed_cb(zfsvfs, xattr);
496 zfsvfs->z_issnap = B_TRUE;
498 error = zfs_register_callbacks(vfsp);
502 zfs_unlinked_drain(zfsvfs);
505 * Parse and replay the intent log.
507 zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign,
511 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
514 vfs_mountedfrom(vfsp, osname);
516 if (!zfsvfs->z_issnap)
517 zfsctl_create(zfsvfs);
521 dmu_objset_close(zfsvfs->z_os);
522 rw_destroy(&zfsvfs->z_um_lock);
523 mutex_destroy(&zfsvfs->z_znodes_lock);
524 kmem_free(zfsvfs, sizeof (zfsvfs_t));
526 atomic_add_32(&zfs_active_fs_count, 1);
534 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
536 objset_t *os = zfsvfs->z_os;
537 struct dsl_dataset *ds;
540 * Unregister properties.
542 if (!dmu_objset_is_snapshot(os)) {
543 ds = dmu_objset_ds(os);
544 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
547 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
550 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
553 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
556 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
559 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
562 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
565 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
568 VERIFY(dsl_prop_unregister(ds, "aclinherit",
569 acl_inherit_changed_cb, zfsvfs) == 0);
575 zfs_mount(vfs_t *vfsp, kthread_t *td)
581 * When doing a remount, we simply refresh our temporary properties
582 * according to those options set in the current VFS options.
584 if (vfsp->vfs_flag & MS_REMOUNT)
585 return (zfs_refresh_properties(vfsp));
587 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&from, NULL))
591 error = zfs_domount(vfsp, from, td);
597 zfs_statfs(vfs_t *vfsp, struct statfs *statp, kthread_t *td)
599 zfsvfs_t *zfsvfs = vfsp->vfs_data;
600 uint64_t refdbytes, availbytes, usedobjs, availobjs;
602 statp->f_version = STATFS_VERSION;
606 dmu_objset_space(zfsvfs->z_os,
607 &refdbytes, &availbytes, &usedobjs, &availobjs);
610 * The underlying storage pool actually uses multiple block sizes.
611 * We report the fragsize as the smallest block size we support,
612 * and we report our blocksize as the filesystem's maximum blocksize.
614 statp->f_bsize = zfsvfs->z_vfs->vfs_bsize;
615 statp->f_iosize = zfsvfs->z_vfs->vfs_bsize;
618 * The following report "total" blocks of various kinds in the
619 * file system, but reported in terms of f_frsize - the
623 statp->f_blocks = (refdbytes + availbytes) / statp->f_bsize;
624 statp->f_bfree = availbytes / statp->f_bsize;
625 statp->f_bavail = statp->f_bfree; /* no root reservation */
628 * statvfs() should really be called statufs(), because it assumes
629 * static metadata. ZFS doesn't preallocate files, so the best
630 * we can do is report the max that could possibly fit in f_files,
631 * and that minus the number actually used in f_ffree.
632 * For f_ffree, report the smaller of the number of object available
633 * and the number of blocks (each object will take at least a block).
635 statp->f_ffree = MIN(availobjs, statp->f_bfree);
636 statp->f_files = statp->f_ffree + usedobjs;
639 * We're a zfs filesystem.
641 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
643 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
644 sizeof(statp->f_mntfromname));
645 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
646 sizeof(statp->f_mntonname));
648 statp->f_namemax = ZFS_MAXNAMELEN;
655 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp, kthread_t *td)
657 zfsvfs_t *zfsvfs = vfsp->vfs_data;
663 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
666 error = vn_lock(*vpp, flags, td);
667 (*vpp)->v_vflag |= VV_ROOT;
676 zfs_umount(vfs_t *vfsp, int fflag, kthread_t *td)
678 zfsvfs_t *zfsvfs = vfsp->vfs_data;
679 cred_t *cr = td->td_ucred;
682 if ((ret = secpolicy_fs_unmount(cr, vfsp)) != 0)
685 (void) dnlc_purge_vfsp(vfsp, 0);
688 * Unmount any snapshots mounted under .zfs before unmounting the
691 if (zfsvfs->z_ctldir != NULL) {
692 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
694 ret = vflush(vfsp, 0, 0, td);
695 ASSERT(ret == EBUSY);
696 if (!(fflag & MS_FORCE)) {
697 if (zfsvfs->z_ctldir->v_count > 1)
699 ASSERT(zfsvfs->z_ctldir->v_count == 1);
701 zfsctl_destroy(zfsvfs);
702 ASSERT(zfsvfs->z_ctldir == NULL);
706 * Flush all the files.
708 ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
710 if (!zfsvfs->z_issnap) {
711 zfsctl_create(zfsvfs);
712 ASSERT(zfsvfs->z_ctldir != NULL);
717 if (fflag & MS_FORCE) {
719 vfsp->mnt_kern_flag |= MNTK_UNMOUNTF;
721 zfsvfs->z_unmounted1 = B_TRUE;
724 * Wait for all zfs threads to leave zfs.
725 * Grabbing a rwlock as reader in all vops and
726 * as writer here doesn't work because it too easy to get
727 * multiple reader enters as zfs can re-enter itself.
728 * This can lead to deadlock if there is an intervening
729 * rw_enter as writer.
730 * So a file system threads ref count (z_op_cnt) is used.
731 * A polling loop on z_op_cnt may seem inefficient, but
732 * - this saves all threads on exit from having to grab a
733 * mutex in order to cv_signal
734 * - only occurs on forced unmount in the rare case when
735 * there are outstanding threads within the file system.
737 while (zfsvfs->z_op_cnt) {
742 zfs_objset_close(zfsvfs);
750 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
752 zfsvfs_t *zfsvfs = vfsp->vfs_data;
757 err = zfs_zget(zfsvfs, ino, &zp);
758 if (err == 0 && zp->z_unlinked) {
766 vn_lock(*vpp, flags, curthread);
773 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, vnode_t **vpp)
775 kthread_t *td = curthread;
776 zfsvfs_t *zfsvfs = vfsp->vfs_data;
779 uint64_t fid_gen = 0;
788 if (fidp->fid_len == LONG_FID_LEN) {
789 zfid_long_t *zlfid = (zfid_long_t *)fidp;
790 uint64_t objsetid = 0;
793 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
794 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
796 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
797 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
801 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
807 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
808 zfid_short_t *zfid = (zfid_short_t *)fidp;
810 for (i = 0; i < sizeof (zfid->zf_object); i++)
811 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
813 for (i = 0; i < sizeof (zfid->zf_gen); i++)
814 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
820 /* A zero fid_gen means we are in the .zfs control directories */
822 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
823 *vpp = zfsvfs->z_ctldir;
824 ASSERT(*vpp != NULL);
825 if (object == ZFSCTL_INO_SNAPDIR) {
826 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
827 0, NULL, NULL) == 0);
833 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
837 gen_mask = -1ULL >> (64 - 8 * i);
839 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
840 if (err = zfs_zget(zfsvfs, object, &zp)) {
844 zp_gen = zp->z_phys->zp_gen & gen_mask;
847 if (zp->z_unlinked || zp_gen != fid_gen) {
848 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
856 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
857 vnode_create_vobject(*vpp, zp->z_phys->zp_size, td);
863 zfs_objset_close(zfsvfs_t *zfsvfs)
865 znode_t *zp, *nextzp;
866 objset_t *os = zfsvfs->z_os;
869 * For forced unmount, at this point all vops except zfs_inactive
870 * are erroring EIO. We need to now suspend zfs_inactive threads
871 * while we are freeing dbufs before switching zfs_inactive
872 * to use behaviour without a objset.
874 rw_enter(&zfsvfs->z_um_lock, RW_WRITER);
877 * Release all holds on dbufs
878 * Note, although we have stopped all other vop threads and
879 * zfs_inactive(), the dmu can callback via znode_pageout_func()
880 * which can zfs_znode_free() the znode.
881 * So we lock z_all_znodes; search the list for a held
882 * dbuf; drop the lock (we know zp can't disappear if we hold
883 * a dbuf lock; then regrab the lock and restart.
885 mutex_enter(&zfsvfs->z_znodes_lock);
886 for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) {
887 nextzp = list_next(&zfsvfs->z_all_znodes, zp);
888 if (zp->z_dbuf_held) {
889 /* dbufs should only be held when force unmounting */
891 mutex_exit(&zfsvfs->z_znodes_lock);
892 dmu_buf_rele(zp->z_dbuf, NULL);
894 mutex_enter(&zfsvfs->z_znodes_lock);
895 nextzp = list_head(&zfsvfs->z_all_znodes);
898 mutex_exit(&zfsvfs->z_znodes_lock);
901 * Unregister properties.
903 if (!dmu_objset_is_snapshot(os))
904 zfs_unregister_callbacks(zfsvfs);
907 * Switch zfs_inactive to behaviour without an objset.
908 * It just tosses cached pages and frees the znode & vnode.
909 * Then re-enable zfs_inactive threads in that new behaviour.
911 zfsvfs->z_unmounted2 = B_TRUE;
912 rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */
915 * Close the zil. Can't close the zil while zfs_inactive
916 * threads are blocked as zil_close can call zfs_inactive.
919 zil_close(zfsvfs->z_log);
920 zfsvfs->z_log = NULL;
924 * Evict all dbufs so that cached znodes will be freed
926 if (dmu_objset_evict_dbufs(os, 1)) {
927 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
928 (void) dmu_objset_evict_dbufs(os, 0);
932 * Finally close the objset
934 dmu_objset_close(os);
938 zfs_freevfs(vfs_t *vfsp)
940 zfsvfs_t *zfsvfs = vfsp->vfs_data;
943 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
944 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
945 rw_destroy(&zfsvfs->z_um_lock);
946 mutex_destroy(&zfsvfs->z_znodes_lock);
947 kmem_free(zfsvfs, sizeof (zfsvfs_t));
949 atomic_add_32(&zfs_active_fs_count, -1);
953 static int desiredvnodes_backup;
957 zfs_vnodes_adjust(void)
962 desiredvnodes_backup = desiredvnodes;
965 * We calculate newdesiredvnodes the same way it is done in
966 * vntblinit(). If it is equal to desiredvnodes, it means that
967 * it wasn't tuned by the administrator and we can tune it down.
969 val = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
970 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
971 if (desiredvnodes == val)
972 desiredvnodes = (3 * desiredvnodes) / 4;
977 zfs_vnodes_adjust_back(void)
981 desiredvnodes = desiredvnodes_backup;
989 printf("ZFS filesystem version " ZFS_VERSION_STRING "\n");
992 * Initialize .zfs directory structures
997 * Initialize znode cache, vnode ops, etc...
1002 * Reduce number of vnodes. Originally number of vnodes is calculated
1003 * with UFS inode in mind. We reduce it here, because it's too big for
1006 zfs_vnodes_adjust();
1014 zfs_vnodes_adjust_back();
1020 return (zfs_active_fs_count != 0);