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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
24 * All rights reserved.
25 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
30 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysmacros.h>
39 #include <sys/vnode.h>
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
43 #include <sys/cmn_err.h>
44 #include <sys/zfs_znode.h>
45 #include <sys/zfs_dir.h>
47 #include <sys/fs/zfs.h>
49 #include <sys/dsl_prop.h>
50 #include <sys/dsl_dataset.h>
51 #include <sys/dsl_deleg.h>
55 #include <sys/sa_impl.h>
56 #include <sys/varargs.h>
57 #include <sys/policy.h>
58 #include <sys/atomic.h>
59 #include <sys/zfs_ioctl.h>
60 #include <sys/zfs_ctldir.h>
61 #include <sys/zfs_fuid.h>
62 #include <sys/sunddi.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/spa_boot.h>
67 #include <ufs/ufs/quota.h>
68 #include <sys/rmlock.h>
70 #include "zfs_comutil.h"
72 struct mtx zfs_debug_mtx;
73 MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF);
75 SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system");
78 SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0,
79 "File system owner can perform privileged operation on his file systems");
82 SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RWTUN, &zfs_debug_level, 0,
85 SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions");
86 static int zfs_version_acl = ZFS_ACL_VERSION;
87 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0,
89 static int zfs_version_spa = SPA_VERSION;
90 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0,
92 static int zfs_version_zpl = ZPL_VERSION;
93 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0,
96 static int zfs_root_setvnode(zfsvfs_t *zfsvfs);
97 static void zfs_root_dropvnode(zfsvfs_t *zfsvfs);
99 static int zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg);
100 static int zfs_mount(vfs_t *vfsp);
101 static int zfs_umount(vfs_t *vfsp, int fflag);
102 static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp);
103 static int zfs_statfs(vfs_t *vfsp, struct statfs *statp);
104 static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp);
105 static int zfs_sync(vfs_t *vfsp, int waitfor);
106 static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
107 struct ucred **credanonp, int *numsecflavors, int **secflavors);
108 static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp);
109 static void zfs_objset_close(zfsvfs_t *zfsvfs);
110 static void zfs_freevfs(vfs_t *vfsp);
112 struct vfsops zfs_vfsops = {
113 .vfs_mount = zfs_mount,
114 .vfs_unmount = zfs_umount,
115 .vfs_root = zfs_root,
116 .vfs_statfs = zfs_statfs,
117 .vfs_vget = zfs_vget,
118 .vfs_sync = zfs_sync,
119 .vfs_checkexp = zfs_checkexp,
120 .vfs_fhtovp = zfs_fhtovp,
121 .vfs_quotactl = zfs_quotactl,
124 VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN);
127 * We need to keep a count of active fs's.
128 * This is necessary to prevent our module
129 * from being unloaded after a umount -f
131 static uint32_t zfs_active_fs_count = 0;
134 zfs_getquota(zfsvfs_t *zfsvfs, uid_t id, int isgroup, struct dqblk64 *dqp)
139 uint64_t usedobj, quotaobj;
140 uint64_t quota, used = 0;
143 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
144 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
146 if (quotaobj == 0 || zfsvfs->z_replay) {
150 (void)sprintf(buf, "%llx", (longlong_t)id);
151 if ((error = zap_lookup(zfsvfs->z_os, quotaobj,
152 buf, sizeof(quota), 1, "a)) != 0) {
153 dprintf("%s(%d): quotaobj lookup failed\n", __FUNCTION__, __LINE__);
157 * quota(8) uses bsoftlimit as "quoota", and hardlimit as "limit".
158 * So we set them to be the same.
160 dqp->dqb_bsoftlimit = dqp->dqb_bhardlimit = btodb(quota);
161 error = zap_lookup(zfsvfs->z_os, usedobj, buf, sizeof(used), 1, &used);
162 if (error && error != ENOENT) {
163 dprintf("%s(%d): usedobj failed; %d\n", __FUNCTION__, __LINE__, error);
166 dqp->dqb_curblocks = btodb(used);
167 dqp->dqb_ihardlimit = dqp->dqb_isoftlimit = 0;
170 * Setting this to 0 causes FreeBSD quota(8) to print
171 * the number of days since the epoch, which isn't
172 * particularly useful.
174 dqp->dqb_btime = dqp->dqb_itime = now.tv_sec;
180 zfs_quotactl(vfs_t *vfsp, int cmds, uid_t id, void *arg)
182 zfsvfs_t *zfsvfs = vfsp->vfs_data;
184 int cmd, type, error = 0;
187 zfs_userquota_prop_t quota_type;
188 struct dqblk64 dqblk = { 0 };
191 cmd = cmds >> SUBCMDSHIFT;
192 type = cmds & SUBCMDMASK;
198 id = td->td_ucred->cr_ruid;
201 id = td->td_ucred->cr_rgid;
205 if (cmd == Q_QUOTAON || cmd == Q_QUOTAOFF)
213 * ZFS_PROP_USERQUOTA,
214 * ZFS_PROP_GROUPUSED,
215 * ZFS_PROP_GROUPQUOTA
220 if (type == USRQUOTA)
221 quota_type = ZFS_PROP_USERQUOTA;
222 else if (type == GRPQUOTA)
223 quota_type = ZFS_PROP_GROUPQUOTA;
229 if (type == USRQUOTA)
230 quota_type = ZFS_PROP_USERUSED;
231 else if (type == GRPQUOTA)
232 quota_type = ZFS_PROP_GROUPUSED;
239 * Depending on the cmd, we may need to get
240 * the ruid and domain (see fuidstr_to_sid?),
241 * the fuid (how?), or other information.
242 * Create fuid using zfs_fuid_create(zfsvfs, id,
243 * ZFS_OWNER or ZFS_GROUP, cr, &fuidp)?
244 * I think I can use just the id?
246 * Look at zfs_fuid_overquota() to look up a quota.
247 * zap_lookup(something, quotaobj, fuidstring, sizeof(long long), 1, "a)
249 * See zfs_set_userquota() to set a quota.
251 if ((u_int)type >= MAXQUOTAS) {
259 error = copyout(&bitsize, arg, sizeof(int));
262 // As far as I can tell, you can't turn quotas on or off on zfs
271 error = copyin(&dqblk, arg, sizeof(dqblk));
273 error = zfs_set_userquota(zfsvfs, quota_type,
274 "", id, dbtob(dqblk.dqb_bhardlimit));
277 error = zfs_getquota(zfsvfs, id, type == GRPQUOTA, &dqblk);
279 error = copyout(&dqblk, arg, sizeof(dqblk));
292 zfs_sync(vfs_t *vfsp, int waitfor)
296 * Data integrity is job one. We don't want a compromised kernel
297 * writing to the storage pool, so we never sync during panic.
303 * Ignore the system syncher. ZFS already commits async data
304 * at zfs_txg_timeout intervals.
306 if (waitfor == MNT_LAZY)
311 * Sync a specific filesystem.
313 zfsvfs_t *zfsvfs = vfsp->vfs_data;
317 error = vfs_stdsync(vfsp, waitfor);
322 dp = dmu_objset_pool(zfsvfs->z_os);
325 * If the system is shutting down, then skip any
326 * filesystems which may exist on a suspended pool.
328 if (sys_shutdown && spa_suspended(dp->dp_spa)) {
333 if (zfsvfs->z_log != NULL)
334 zil_commit(zfsvfs->z_log, 0);
339 * Sync all ZFS filesystems. This is what happens when you
340 * run sync(1M). Unlike other filesystems, ZFS honors the
341 * request by waiting for all pools to commit all dirty data.
349 #ifndef __FreeBSD_kernel__
351 zfs_create_unique_device(dev_t *dev)
356 ASSERT3U(zfs_minor, <=, MAXMIN32);
357 minor_t start = zfs_minor;
359 mutex_enter(&zfs_dev_mtx);
360 if (zfs_minor >= MAXMIN32) {
362 * If we're still using the real major
363 * keep out of /dev/zfs and /dev/zvol minor
364 * number space. If we're using a getudev()'ed
365 * major number, we can use all of its minors.
367 if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
368 zfs_minor = ZFS_MIN_MINOR;
374 *dev = makedevice(zfs_major, zfs_minor);
375 mutex_exit(&zfs_dev_mtx);
376 } while (vfs_devismounted(*dev) && zfs_minor != start);
377 if (zfs_minor == start) {
379 * We are using all ~262,000 minor numbers for the
380 * current major number. Create a new major number.
382 if ((new_major = getudev()) == (major_t)-1) {
384 "zfs_mount: Can't get unique major "
388 mutex_enter(&zfs_dev_mtx);
389 zfs_major = new_major;
392 mutex_exit(&zfs_dev_mtx);
396 /* CONSTANTCONDITION */
401 #endif /* !__FreeBSD_kernel__ */
404 atime_changed_cb(void *arg, uint64_t newval)
406 zfsvfs_t *zfsvfs = arg;
408 if (newval == TRUE) {
409 zfsvfs->z_atime = TRUE;
410 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME;
411 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
412 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
414 zfsvfs->z_atime = FALSE;
415 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME;
416 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
417 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
422 xattr_changed_cb(void *arg, uint64_t newval)
424 zfsvfs_t *zfsvfs = arg;
426 if (newval == TRUE) {
427 /* XXX locking on vfs_flag? */
429 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
431 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
432 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
434 /* XXX locking on vfs_flag? */
436 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
438 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
439 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
444 blksz_changed_cb(void *arg, uint64_t newval)
446 zfsvfs_t *zfsvfs = arg;
447 ASSERT3U(newval, <=, spa_maxblocksize(dmu_objset_spa(zfsvfs->z_os)));
448 ASSERT3U(newval, >=, SPA_MINBLOCKSIZE);
449 ASSERT(ISP2(newval));
451 zfsvfs->z_max_blksz = newval;
452 zfsvfs->z_vfs->mnt_stat.f_iosize = newval;
456 readonly_changed_cb(void *arg, uint64_t newval)
458 zfsvfs_t *zfsvfs = arg;
461 /* XXX locking on vfs_flag? */
462 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
463 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
464 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
466 /* XXX locking on vfs_flag? */
467 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
468 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
469 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
474 setuid_changed_cb(void *arg, uint64_t newval)
476 zfsvfs_t *zfsvfs = arg;
478 if (newval == FALSE) {
479 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
480 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
481 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
483 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
484 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
485 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
490 exec_changed_cb(void *arg, uint64_t newval)
492 zfsvfs_t *zfsvfs = arg;
494 if (newval == FALSE) {
495 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
496 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
497 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
499 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
500 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
501 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
506 * The nbmand mount option can be changed at mount time.
507 * We can't allow it to be toggled on live file systems or incorrect
508 * behavior may be seen from cifs clients
510 * This property isn't registered via dsl_prop_register(), but this callback
511 * will be called when a file system is first mounted
514 nbmand_changed_cb(void *arg, uint64_t newval)
516 zfsvfs_t *zfsvfs = arg;
517 if (newval == FALSE) {
518 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
519 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
521 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
522 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
527 snapdir_changed_cb(void *arg, uint64_t newval)
529 zfsvfs_t *zfsvfs = arg;
531 zfsvfs->z_show_ctldir = newval;
535 vscan_changed_cb(void *arg, uint64_t newval)
537 zfsvfs_t *zfsvfs = arg;
539 zfsvfs->z_vscan = newval;
543 acl_mode_changed_cb(void *arg, uint64_t newval)
545 zfsvfs_t *zfsvfs = arg;
547 zfsvfs->z_acl_mode = newval;
551 acl_inherit_changed_cb(void *arg, uint64_t newval)
553 zfsvfs_t *zfsvfs = arg;
555 zfsvfs->z_acl_inherit = newval;
559 zfs_register_callbacks(vfs_t *vfsp)
561 struct dsl_dataset *ds = NULL;
563 zfsvfs_t *zfsvfs = NULL;
565 boolean_t readonly = B_FALSE;
566 boolean_t do_readonly = B_FALSE;
567 boolean_t setuid = B_FALSE;
568 boolean_t do_setuid = B_FALSE;
569 boolean_t exec = B_FALSE;
570 boolean_t do_exec = B_FALSE;
572 boolean_t devices = B_FALSE;
573 boolean_t do_devices = B_FALSE;
575 boolean_t xattr = B_FALSE;
576 boolean_t do_xattr = B_FALSE;
577 boolean_t atime = B_FALSE;
578 boolean_t do_atime = B_FALSE;
582 zfsvfs = vfsp->vfs_data;
587 * This function can be called for a snapshot when we update snapshot's
588 * mount point, which isn't really supported.
590 if (dmu_objset_is_snapshot(os))
594 * The act of registering our callbacks will destroy any mount
595 * options we may have. In order to enable temporary overrides
596 * of mount options, we stash away the current values and
597 * restore them after we register the callbacks.
599 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
600 !spa_writeable(dmu_objset_spa(os))) {
602 do_readonly = B_TRUE;
603 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
605 do_readonly = B_TRUE;
607 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
611 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
614 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
619 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
622 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
626 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
629 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
633 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
636 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
642 * We need to enter pool configuration here, so that we can use
643 * dsl_prop_get_int_ds() to handle the special nbmand property below.
644 * dsl_prop_get_integer() can not be used, because it has to acquire
645 * spa_namespace_lock and we can not do that because we already hold
646 * z_teardown_lock. The problem is that spa_write_cachefile() is called
647 * with spa_namespace_lock held and the function calls ZFS vnode
648 * operations to write the cache file and thus z_teardown_lock is
649 * acquired after spa_namespace_lock.
651 ds = dmu_objset_ds(os);
652 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
655 * nbmand is a special property. It can only be changed at
658 * This is weird, but it is documented to only be changeable
661 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
663 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
665 } else if (error = dsl_prop_get_int_ds(ds, "nbmand", &nbmand) != 0) {
666 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
671 * Register property callbacks.
673 * It would probably be fine to just check for i/o error from
674 * the first prop_register(), but I guess I like to go
677 error = dsl_prop_register(ds,
678 zfs_prop_to_name(ZFS_PROP_ATIME), atime_changed_cb, zfsvfs);
679 error = error ? error : dsl_prop_register(ds,
680 zfs_prop_to_name(ZFS_PROP_XATTR), xattr_changed_cb, zfsvfs);
681 error = error ? error : dsl_prop_register(ds,
682 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), blksz_changed_cb, zfsvfs);
683 error = error ? error : dsl_prop_register(ds,
684 zfs_prop_to_name(ZFS_PROP_READONLY), readonly_changed_cb, zfsvfs);
686 error = error ? error : dsl_prop_register(ds,
687 zfs_prop_to_name(ZFS_PROP_DEVICES), devices_changed_cb, zfsvfs);
689 error = error ? error : dsl_prop_register(ds,
690 zfs_prop_to_name(ZFS_PROP_SETUID), setuid_changed_cb, zfsvfs);
691 error = error ? error : dsl_prop_register(ds,
692 zfs_prop_to_name(ZFS_PROP_EXEC), exec_changed_cb, zfsvfs);
693 error = error ? error : dsl_prop_register(ds,
694 zfs_prop_to_name(ZFS_PROP_SNAPDIR), snapdir_changed_cb, zfsvfs);
695 error = error ? error : dsl_prop_register(ds,
696 zfs_prop_to_name(ZFS_PROP_ACLMODE), acl_mode_changed_cb, zfsvfs);
697 error = error ? error : dsl_prop_register(ds,
698 zfs_prop_to_name(ZFS_PROP_ACLINHERIT), acl_inherit_changed_cb,
700 error = error ? error : dsl_prop_register(ds,
701 zfs_prop_to_name(ZFS_PROP_VSCAN), vscan_changed_cb, zfsvfs);
702 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
707 * Invoke our callbacks to restore temporary mount options.
710 readonly_changed_cb(zfsvfs, readonly);
712 setuid_changed_cb(zfsvfs, setuid);
714 exec_changed_cb(zfsvfs, exec);
716 xattr_changed_cb(zfsvfs, xattr);
718 atime_changed_cb(zfsvfs, atime);
720 nbmand_changed_cb(zfsvfs, nbmand);
725 dsl_prop_unregister_all(ds, zfsvfs);
730 zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
731 uint64_t *userp, uint64_t *groupp)
734 * Is it a valid type of object to track?
736 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
737 return (SET_ERROR(ENOENT));
740 * If we have a NULL data pointer
741 * then assume the id's aren't changing and
742 * return EEXIST to the dmu to let it know to
746 return (SET_ERROR(EEXIST));
748 if (bonustype == DMU_OT_ZNODE) {
749 znode_phys_t *znp = data;
750 *userp = znp->zp_uid;
751 *groupp = znp->zp_gid;
754 sa_hdr_phys_t *sap = data;
755 sa_hdr_phys_t sa = *sap;
756 boolean_t swap = B_FALSE;
758 ASSERT(bonustype == DMU_OT_SA);
760 if (sa.sa_magic == 0) {
762 * This should only happen for newly created
763 * files that haven't had the znode data filled
770 if (sa.sa_magic == BSWAP_32(SA_MAGIC)) {
771 sa.sa_magic = SA_MAGIC;
772 sa.sa_layout_info = BSWAP_16(sa.sa_layout_info);
775 VERIFY3U(sa.sa_magic, ==, SA_MAGIC);
778 hdrsize = sa_hdrsize(&sa);
779 VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t));
780 *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
782 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
785 *userp = BSWAP_64(*userp);
786 *groupp = BSWAP_64(*groupp);
793 fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
794 char *domainbuf, int buflen, uid_t *ridp)
799 fuid = zfs_strtonum(fuidstr, NULL);
801 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
803 (void) strlcpy(domainbuf, domain, buflen);
806 *ridp = FUID_RID(fuid);
810 zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
813 case ZFS_PROP_USERUSED:
814 return (DMU_USERUSED_OBJECT);
815 case ZFS_PROP_GROUPUSED:
816 return (DMU_GROUPUSED_OBJECT);
817 case ZFS_PROP_USERQUOTA:
818 return (zfsvfs->z_userquota_obj);
819 case ZFS_PROP_GROUPQUOTA:
820 return (zfsvfs->z_groupquota_obj);
826 zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
827 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
832 zfs_useracct_t *buf = vbuf;
835 if (!dmu_objset_userspace_present(zfsvfs->z_os))
836 return (SET_ERROR(ENOTSUP));
838 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
844 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
845 (error = zap_cursor_retrieve(&zc, &za)) == 0;
846 zap_cursor_advance(&zc)) {
847 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
851 fuidstr_to_sid(zfsvfs, za.za_name,
852 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
854 buf->zu_space = za.za_first_integer;
860 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
861 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
862 *cookiep = zap_cursor_serialize(&zc);
863 zap_cursor_fini(&zc);
868 * buf must be big enough (eg, 32 bytes)
871 id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
872 char *buf, boolean_t addok)
877 if (domain && domain[0]) {
878 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
880 return (SET_ERROR(ENOENT));
882 fuid = FUID_ENCODE(domainid, rid);
883 (void) sprintf(buf, "%llx", (longlong_t)fuid);
888 zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
889 const char *domain, uint64_t rid, uint64_t *valp)
897 if (!dmu_objset_userspace_present(zfsvfs->z_os))
898 return (SET_ERROR(ENOTSUP));
900 obj = zfs_userquota_prop_to_obj(zfsvfs, type);
904 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
908 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
915 zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
916 const char *domain, uint64_t rid, uint64_t quota)
922 boolean_t fuid_dirtied;
924 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
925 return (SET_ERROR(EINVAL));
927 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
928 return (SET_ERROR(ENOTSUP));
930 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
931 &zfsvfs->z_groupquota_obj;
933 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
936 fuid_dirtied = zfsvfs->z_fuid_dirty;
938 tx = dmu_tx_create(zfsvfs->z_os);
939 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
941 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
942 zfs_userquota_prop_prefixes[type]);
945 zfs_fuid_txhold(zfsvfs, tx);
946 err = dmu_tx_assign(tx, TXG_WAIT);
952 mutex_enter(&zfsvfs->z_lock);
954 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
956 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
957 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
959 mutex_exit(&zfsvfs->z_lock);
962 err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
966 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx);
970 zfs_fuid_sync(zfsvfs, tx);
976 zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
979 uint64_t used, quota, usedobj, quotaobj;
982 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
983 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
985 if (quotaobj == 0 || zfsvfs->z_replay)
988 (void) sprintf(buf, "%llx", (longlong_t)fuid);
989 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a);
993 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
996 return (used >= quota);
1000 zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
1005 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
1007 fuid = isgroup ? zp->z_gid : zp->z_uid;
1009 if (quotaobj == 0 || zfsvfs->z_replay)
1012 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
1016 * Associate this zfsvfs with the given objset, which must be owned.
1017 * This will cache a bunch of on-disk state from the objset in the
1021 zfsvfs_init(zfsvfs_t *zfsvfs, objset_t *os)
1026 zfsvfs->z_max_blksz = SPA_OLD_MAXBLOCKSIZE;
1027 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
1030 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
1033 if (zfsvfs->z_version >
1034 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
1035 (void) printf("Can't mount a version %lld file system "
1036 "on a version %lld pool\n. Pool must be upgraded to mount "
1037 "this file system.", (u_longlong_t)zfsvfs->z_version,
1038 (u_longlong_t)spa_version(dmu_objset_spa(os)));
1039 return (SET_ERROR(ENOTSUP));
1041 error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &val);
1044 zfsvfs->z_norm = (int)val;
1046 error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &val);
1049 zfsvfs->z_utf8 = (val != 0);
1051 error = zfs_get_zplprop(os, ZFS_PROP_CASE, &val);
1054 zfsvfs->z_case = (uint_t)val;
1057 * Fold case on file systems that are always or sometimes case
1060 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
1061 zfsvfs->z_case == ZFS_CASE_MIXED)
1062 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
1064 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1065 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1067 uint64_t sa_obj = 0;
1068 if (zfsvfs->z_use_sa) {
1069 /* should either have both of these objects or none */
1070 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
1076 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1077 &zfsvfs->z_attr_table);
1081 if (zfsvfs->z_version >= ZPL_VERSION_SA)
1082 sa_register_update_callback(os, zfs_sa_upgrade);
1084 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
1088 ASSERT(zfsvfs->z_root != 0);
1090 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
1091 &zfsvfs->z_unlinkedobj);
1095 error = zap_lookup(os, MASTER_NODE_OBJ,
1096 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
1097 8, 1, &zfsvfs->z_userquota_obj);
1098 if (error == ENOENT)
1099 zfsvfs->z_userquota_obj = 0;
1100 else if (error != 0)
1103 error = zap_lookup(os, MASTER_NODE_OBJ,
1104 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
1105 8, 1, &zfsvfs->z_groupquota_obj);
1106 if (error == ENOENT)
1107 zfsvfs->z_groupquota_obj = 0;
1108 else if (error != 0)
1111 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
1112 &zfsvfs->z_fuid_obj);
1113 if (error == ENOENT)
1114 zfsvfs->z_fuid_obj = 0;
1115 else if (error != 0)
1118 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
1119 &zfsvfs->z_shares_dir);
1120 if (error == ENOENT)
1121 zfsvfs->z_shares_dir = 0;
1122 else if (error != 0)
1126 * Only use the name cache if we are looking for a
1127 * name on a file system that does not require normalization
1128 * or case folding. We can also look there if we happen to be
1129 * on a non-normalizing, mixed sensitivity file system IF we
1130 * are looking for the exact name (which is always the case on
1133 zfsvfs->z_use_namecache = !zfsvfs->z_norm ||
1134 ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
1135 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER));
1140 #if defined(__FreeBSD__)
1141 taskq_t *zfsvfs_taskq;
1144 zfsvfs_task_unlinked_drain(void *context, int pending __unused)
1147 zfs_unlinked_drain((zfsvfs_t *)context);
1152 zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
1159 * XXX: Fix struct statfs so this isn't necessary!
1161 * The 'osname' is used as the filesystem's special node, which means
1162 * it must fit in statfs.f_mntfromname, or else it can't be
1163 * enumerated, so libzfs_mnttab_find() returns NULL, which causes
1164 * 'zfs unmount' to think it's not mounted when it is.
1166 if (strlen(osname) >= MNAMELEN)
1167 return (SET_ERROR(ENAMETOOLONG));
1169 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
1172 * We claim to always be readonly so we can open snapshots;
1173 * other ZPL code will prevent us from writing to snapshots.
1176 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
1178 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1182 error = zfsvfs_create_impl(zfvp, zfsvfs, os);
1184 dmu_objset_disown(os, zfsvfs);
1191 zfsvfs_create_impl(zfsvfs_t **zfvp, zfsvfs_t *zfsvfs, objset_t *os)
1195 zfsvfs->z_vfs = NULL;
1196 zfsvfs->z_parent = zfsvfs;
1198 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1199 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
1200 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
1201 offsetof(znode_t, z_link_node));
1202 #if defined(__FreeBSD__)
1203 TASK_INIT(&zfsvfs->z_unlinked_drain_task, 0,
1204 zfsvfs_task_unlinked_drain, zfsvfs);
1207 rrm_init(&zfsvfs->z_teardown_lock, B_TRUE);
1209 rrm_init(&zfsvfs->z_teardown_lock, B_FALSE);
1211 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
1212 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
1213 for (int i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1214 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1216 rm_init(&zfsvfs->z_rootvnodelock, "zfs root vnode lock");
1218 error = zfsvfs_init(zfsvfs, os);
1221 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1230 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
1234 error = zfs_register_callbacks(zfsvfs->z_vfs);
1238 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
1241 * If we are not mounting (ie: online recv), then we don't
1242 * have to worry about replaying the log as we blocked all
1243 * operations out since we closed the ZIL.
1249 * During replay we remove the read only flag to
1250 * allow replays to succeed.
1252 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
1254 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
1256 zfs_unlinked_drain(zfsvfs);
1259 * Parse and replay the intent log.
1261 * Because of ziltest, this must be done after
1262 * zfs_unlinked_drain(). (Further note: ziltest
1263 * doesn't use readonly mounts, where
1264 * zfs_unlinked_drain() isn't called.) This is because
1265 * ziltest causes spa_sync() to think it's committed,
1266 * but actually it is not, so the intent log contains
1267 * many txg's worth of changes.
1269 * In particular, if object N is in the unlinked set in
1270 * the last txg to actually sync, then it could be
1271 * actually freed in a later txg and then reallocated
1272 * in a yet later txg. This would write a "create
1273 * object N" record to the intent log. Normally, this
1274 * would be fine because the spa_sync() would have
1275 * written out the fact that object N is free, before
1276 * we could write the "create object N" intent log
1279 * But when we are in ziltest mode, we advance the "open
1280 * txg" without actually spa_sync()-ing the changes to
1281 * disk. So we would see that object N is still
1282 * allocated and in the unlinked set, and there is an
1283 * intent log record saying to allocate it.
1285 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
1286 if (zil_replay_disable) {
1287 zil_destroy(zfsvfs->z_log, B_FALSE);
1289 zfsvfs->z_replay = B_TRUE;
1290 zil_replay(zfsvfs->z_os, zfsvfs,
1292 zfsvfs->z_replay = B_FALSE;
1295 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
1299 * Set the objset user_ptr to track its zfsvfs.
1301 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1302 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1303 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1308 extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
1311 zfsvfs_free(zfsvfs_t *zfsvfs)
1316 * This is a barrier to prevent the filesystem from going away in
1317 * zfs_znode_move() until we can safely ensure that the filesystem is
1318 * not unmounted. We consider the filesystem valid before the barrier
1319 * and invalid after the barrier.
1321 rw_enter(&zfsvfs_lock, RW_READER);
1322 rw_exit(&zfsvfs_lock);
1324 rm_destroy(&zfsvfs->z_rootvnodelock);
1326 zfs_fuid_destroy(zfsvfs);
1328 mutex_destroy(&zfsvfs->z_znodes_lock);
1329 mutex_destroy(&zfsvfs->z_lock);
1330 list_destroy(&zfsvfs->z_all_znodes);
1331 rrm_destroy(&zfsvfs->z_teardown_lock);
1332 rw_destroy(&zfsvfs->z_teardown_inactive_lock);
1333 rw_destroy(&zfsvfs->z_fuid_lock);
1334 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1335 mutex_destroy(&zfsvfs->z_hold_mtx[i]);
1336 kmem_free(zfsvfs, sizeof (zfsvfs_t));
1340 zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
1342 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
1343 if (zfsvfs->z_vfs) {
1344 if (zfsvfs->z_use_fuids) {
1345 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1346 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1347 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1348 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1349 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1350 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1352 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
1353 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
1354 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
1355 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
1356 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
1357 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
1360 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
1364 zfs_domount(vfs_t *vfsp, char *osname)
1366 uint64_t recordsize, fsid_guid;
1374 error = zfsvfs_create(osname, &zfsvfs);
1377 zfsvfs->z_vfs = vfsp;
1380 /* Initialize the generic filesystem structure. */
1381 vfsp->vfs_bcount = 0;
1382 vfsp->vfs_data = NULL;
1384 if (zfs_create_unique_device(&mount_dev) == -1) {
1385 error = SET_ERROR(ENODEV);
1388 ASSERT(vfs_devismounted(mount_dev) == 0);
1391 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
1394 zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE;
1395 zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize;
1397 vfsp->vfs_data = zfsvfs;
1398 vfsp->mnt_flag |= MNT_LOCAL;
1399 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED;
1400 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES;
1401 vfsp->mnt_kern_flag |= MNTK_EXTENDED_SHARED;
1402 vfsp->mnt_kern_flag |= MNTK_NO_IOPF; /* vn_io_fault can be used */
1405 * The fsid is 64 bits, composed of an 8-bit fs type, which
1406 * separates our fsid from any other filesystem types, and a
1407 * 56-bit objset unique ID. The objset unique ID is unique to
1408 * all objsets open on this system, provided by unique_create().
1409 * The 8-bit fs type must be put in the low bits of fsid[1]
1410 * because that's where other Solaris filesystems put it.
1412 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
1413 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
1414 vfsp->vfs_fsid.val[0] = fsid_guid;
1415 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
1416 vfsp->mnt_vfc->vfc_typenum & 0xFF;
1419 * Set features for file system.
1421 zfs_set_fuid_feature(zfsvfs);
1422 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
1423 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1424 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1425 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
1426 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
1427 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
1428 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
1430 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
1432 if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
1435 atime_changed_cb(zfsvfs, B_FALSE);
1436 readonly_changed_cb(zfsvfs, B_TRUE);
1437 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
1439 xattr_changed_cb(zfsvfs, pval);
1440 zfsvfs->z_issnap = B_TRUE;
1441 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
1443 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
1444 dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
1445 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
1447 error = zfsvfs_setup(zfsvfs, B_TRUE);
1450 vfs_mountedfrom(vfsp, osname);
1452 if (!zfsvfs->z_issnap)
1453 zfsctl_create(zfsvfs);
1456 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1457 zfsvfs_free(zfsvfs);
1459 atomic_inc_32(&zfs_active_fs_count);
1466 zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
1468 objset_t *os = zfsvfs->z_os;
1470 if (!dmu_objset_is_snapshot(os))
1471 dsl_prop_unregister_all(dmu_objset_ds(os), zfsvfs);
1476 * Convert a decimal digit string to a uint64_t integer.
1479 str_to_uint64(char *str, uint64_t *objnum)
1484 if (*str < '0' || *str > '9')
1485 return (SET_ERROR(EINVAL));
1487 num = num*10 + *str++ - '0';
1495 * The boot path passed from the boot loader is in the form of
1496 * "rootpool-name/root-filesystem-object-number'. Convert this
1497 * string to a dataset name: "rootpool-name/root-filesystem-name".
1500 zfs_parse_bootfs(char *bpath, char *outpath)
1506 if (*bpath == 0 || *bpath == '/')
1507 return (SET_ERROR(EINVAL));
1509 (void) strcpy(outpath, bpath);
1511 slashp = strchr(bpath, '/');
1513 /* if no '/', just return the pool name */
1514 if (slashp == NULL) {
1518 /* if not a number, just return the root dataset name */
1519 if (str_to_uint64(slashp+1, &objnum)) {
1524 error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
1531 * Check that the hex label string is appropriate for the dataset being
1532 * mounted into the global_zone proper.
1534 * Return an error if the hex label string is not default or
1535 * admin_low/admin_high. For admin_low labels, the corresponding
1536 * dataset must be readonly.
1539 zfs_check_global_label(const char *dsname, const char *hexsl)
1541 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1543 if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
1545 if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
1546 /* must be readonly */
1549 if (dsl_prop_get_integer(dsname,
1550 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
1551 return (SET_ERROR(EACCES));
1552 return (rdonly ? 0 : EACCES);
1554 return (SET_ERROR(EACCES));
1558 * Determine whether the mount is allowed according to MAC check.
1559 * by comparing (where appropriate) label of the dataset against
1560 * the label of the zone being mounted into. If the dataset has
1561 * no label, create one.
1563 * Returns 0 if access allowed, error otherwise (e.g. EACCES)
1566 zfs_mount_label_policy(vfs_t *vfsp, char *osname)
1569 zone_t *mntzone = NULL;
1570 ts_label_t *mnt_tsl;
1573 char ds_hexsl[MAXNAMELEN];
1575 retv = EACCES; /* assume the worst */
1578 * Start by getting the dataset label if it exists.
1580 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1581 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
1583 return (SET_ERROR(EACCES));
1586 * If labeling is NOT enabled, then disallow the mount of datasets
1587 * which have a non-default label already. No other label checks
1590 if (!is_system_labeled()) {
1591 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
1593 return (SET_ERROR(EACCES));
1597 * Get the label of the mountpoint. If mounting into the global
1598 * zone (i.e. mountpoint is not within an active zone and the
1599 * zoned property is off), the label must be default or
1600 * admin_low/admin_high only; no other checks are needed.
1602 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
1603 if (mntzone->zone_id == GLOBAL_ZONEID) {
1608 if (dsl_prop_get_integer(osname,
1609 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
1610 return (SET_ERROR(EACCES));
1612 return (zfs_check_global_label(osname, ds_hexsl));
1615 * This is the case of a zone dataset being mounted
1616 * initially, before the zone has been fully created;
1617 * allow this mount into global zone.
1622 mnt_tsl = mntzone->zone_slabel;
1623 ASSERT(mnt_tsl != NULL);
1624 label_hold(mnt_tsl);
1625 mnt_sl = label2bslabel(mnt_tsl);
1627 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
1629 * The dataset doesn't have a real label, so fabricate one.
1633 if (l_to_str_internal(mnt_sl, &str) == 0 &&
1634 dsl_prop_set_string(osname,
1635 zfs_prop_to_name(ZFS_PROP_MLSLABEL),
1636 ZPROP_SRC_LOCAL, str) == 0)
1639 kmem_free(str, strlen(str) + 1);
1640 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
1642 * Now compare labels to complete the MAC check. If the
1643 * labels are equal then allow access. If the mountpoint
1644 * label dominates the dataset label, allow readonly access.
1645 * Otherwise, access is denied.
1647 if (blequal(mnt_sl, &ds_sl))
1649 else if (bldominates(mnt_sl, &ds_sl)) {
1650 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
1655 label_rele(mnt_tsl);
1659 #endif /* SECLABEL */
1661 #ifdef OPENSOLARIS_MOUNTROOT
1663 zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
1666 static int zfsrootdone = 0;
1667 zfsvfs_t *zfsvfs = NULL;
1676 * The filesystem that we mount as root is defined in the
1677 * boot property "zfs-bootfs" with a format of
1678 * "poolname/root-dataset-objnum".
1680 if (why == ROOT_INIT) {
1682 return (SET_ERROR(EBUSY));
1684 * the process of doing a spa_load will require the
1685 * clock to be set before we could (for example) do
1686 * something better by looking at the timestamp on
1687 * an uberblock, so just set it to -1.
1691 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
1692 cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
1694 return (SET_ERROR(EINVAL));
1696 zfs_devid = spa_get_bootprop("diskdevid");
1697 error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
1699 spa_free_bootprop(zfs_devid);
1701 spa_free_bootprop(zfs_bootfs);
1702 cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
1706 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
1707 spa_free_bootprop(zfs_bootfs);
1708 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
1713 spa_free_bootprop(zfs_bootfs);
1715 if (error = vfs_lock(vfsp))
1718 if (error = zfs_domount(vfsp, rootfs.bo_name)) {
1719 cmn_err(CE_NOTE, "zfs_domount: error %d", error);
1723 zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
1725 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
1726 cmn_err(CE_NOTE, "zfs_zget: error %d", error);
1731 mutex_enter(&vp->v_lock);
1732 vp->v_flag |= VROOT;
1733 mutex_exit(&vp->v_lock);
1737 * Leave rootvp held. The root file system is never unmounted.
1740 vfs_add((struct vnode *)0, vfsp,
1741 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1745 } else if (why == ROOT_REMOUNT) {
1746 readonly_changed_cb(vfsp->vfs_data, B_FALSE);
1747 vfsp->vfs_flag |= VFS_REMOUNT;
1749 /* refresh mount options */
1750 zfs_unregister_callbacks(vfsp->vfs_data);
1751 return (zfs_register_callbacks(vfsp));
1753 } else if (why == ROOT_UNMOUNT) {
1754 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
1755 (void) zfs_sync(vfsp, 0, 0);
1760 * if "why" is equal to anything else other than ROOT_INIT,
1761 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
1763 return (SET_ERROR(ENOTSUP));
1765 #endif /* OPENSOLARIS_MOUNTROOT */
1768 getpoolname(const char *osname, char *poolname)
1772 p = strchr(osname, '/');
1774 if (strlen(osname) >= MAXNAMELEN)
1775 return (ENAMETOOLONG);
1776 (void) strcpy(poolname, osname);
1778 if (p - osname >= MAXNAMELEN)
1779 return (ENAMETOOLONG);
1780 (void) strncpy(poolname, osname, p - osname);
1781 poolname[p - osname] = '\0';
1788 zfs_mount(vfs_t *vfsp)
1790 kthread_t *td = curthread;
1791 vnode_t *mvp = vfsp->mnt_vnodecovered;
1792 cred_t *cr = td->td_ucred;
1798 if (mvp->v_type != VDIR)
1799 return (SET_ERROR(ENOTDIR));
1801 mutex_enter(&mvp->v_lock);
1802 if ((uap->flags & MS_REMOUNT) == 0 &&
1803 (uap->flags & MS_OVERLAY) == 0 &&
1804 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
1805 mutex_exit(&mvp->v_lock);
1806 return (SET_ERROR(EBUSY));
1808 mutex_exit(&mvp->v_lock);
1811 * ZFS does not support passing unparsed data in via MS_DATA.
1812 * Users should use the MS_OPTIONSTR interface; this means
1813 * that all option parsing is already done and the options struct
1814 * can be interrogated.
1816 if ((uap->flags & MS_DATA) && uap->datalen > 0)
1817 return (SET_ERROR(EINVAL));
1820 * Get the objset name (the "special" mount argument).
1822 if (error = pn_get(uap->spec, fromspace, &spn))
1825 osname = spn.pn_path;
1826 #else /* !illumos */
1827 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL))
1828 return (SET_ERROR(EINVAL));
1831 * If full-owner-access is enabled and delegated administration is
1832 * turned on, we must set nosuid.
1834 if (zfs_super_owner &&
1835 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) {
1836 secpolicy_fs_mount_clearopts(cr, vfsp);
1838 #endif /* illumos */
1841 * Check for mount privilege?
1843 * If we don't have privilege then see if
1844 * we have local permission to allow it
1846 error = secpolicy_fs_mount(cr, mvp, vfsp);
1848 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0)
1851 if (!(vfsp->vfs_flag & MS_REMOUNT)) {
1855 * Make sure user is the owner of the mount point
1856 * or has sufficient privileges.
1859 vattr.va_mask = AT_UID;
1861 vn_lock(mvp, LK_SHARED | LK_RETRY);
1862 if (VOP_GETATTR(mvp, &vattr, cr)) {
1867 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 &&
1868 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) {
1875 secpolicy_fs_mount_clearopts(cr, vfsp);
1879 * Refuse to mount a filesystem if we are in a local zone and the
1880 * dataset is not visible.
1882 if (!INGLOBALZONE(curthread) &&
1883 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
1884 error = SET_ERROR(EPERM);
1889 error = zfs_mount_label_policy(vfsp, osname);
1894 vfsp->vfs_flag |= MNT_NFS4ACLS;
1897 * When doing a remount, we simply refresh our temporary properties
1898 * according to those options set in the current VFS options.
1900 if (vfsp->vfs_flag & MS_REMOUNT) {
1901 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1904 * Refresh mount options with z_teardown_lock blocking I/O while
1905 * the filesystem is in an inconsistent state.
1906 * The lock also serializes this code with filesystem
1907 * manipulations between entry to zfs_suspend_fs() and return
1908 * from zfs_resume_fs().
1910 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
1911 zfs_unregister_callbacks(zfsvfs);
1912 error = zfs_register_callbacks(vfsp);
1913 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
1917 /* Initial root mount: try hard to import the requested root pool. */
1918 if ((vfsp->vfs_flag & MNT_ROOTFS) != 0 &&
1919 (vfsp->vfs_flag & MNT_UPDATE) == 0) {
1920 char pname[MAXNAMELEN];
1922 error = getpoolname(osname, pname);
1924 error = spa_import_rootpool(pname);
1929 error = zfs_domount(vfsp, osname);
1933 zfs_root_setvnode((zfsvfs_t *)vfsp->vfs_data);
1937 * Add an extra VFS_HOLD on our parent vfs so that it can't
1938 * disappear due to a forced unmount.
1940 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
1941 VFS_HOLD(mvp->v_vfsp);
1949 zfs_statfs(vfs_t *vfsp, struct statfs *statp)
1951 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1952 uint64_t refdbytes, availbytes, usedobjs, availobjs;
1954 statp->f_version = STATFS_VERSION;
1958 dmu_objset_space(zfsvfs->z_os,
1959 &refdbytes, &availbytes, &usedobjs, &availobjs);
1962 * The underlying storage pool actually uses multiple block sizes.
1963 * We report the fragsize as the smallest block size we support,
1964 * and we report our blocksize as the filesystem's maximum blocksize.
1966 statp->f_bsize = SPA_MINBLOCKSIZE;
1967 statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize;
1970 * The following report "total" blocks of various kinds in the
1971 * file system, but reported in terms of f_frsize - the
1975 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
1976 statp->f_bfree = availbytes / statp->f_bsize;
1977 statp->f_bavail = statp->f_bfree; /* no root reservation */
1980 * statvfs() should really be called statufs(), because it assumes
1981 * static metadata. ZFS doesn't preallocate files, so the best
1982 * we can do is report the max that could possibly fit in f_files,
1983 * and that minus the number actually used in f_ffree.
1984 * For f_ffree, report the smaller of the number of object available
1985 * and the number of blocks (each object will take at least a block).
1987 statp->f_ffree = MIN(availobjs, statp->f_bfree);
1988 statp->f_files = statp->f_ffree + usedobjs;
1991 * We're a zfs filesystem.
1993 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename));
1995 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname,
1996 sizeof(statp->f_mntfromname));
1997 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname,
1998 sizeof(statp->f_mntonname));
2000 statp->f_namemax = MAXNAMELEN - 1;
2007 zfs_root_setvnode(zfsvfs_t *zfsvfs)
2013 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
2015 panic("could not zfs_zget for root vnode");
2018 rm_wlock(&zfsvfs->z_rootvnodelock);
2019 if (zfsvfs->z_rootvnode != NULL)
2020 panic("zfs mount point already has a root vnode: %p\n",
2021 zfsvfs->z_rootvnode);
2022 zfsvfs->z_rootvnode = ZTOV(rootzp);
2023 rm_wunlock(&zfsvfs->z_rootvnodelock);
2028 zfs_root_putvnode(zfsvfs_t *zfsvfs)
2032 rm_wlock(&zfsvfs->z_rootvnodelock);
2033 vp = zfsvfs->z_rootvnode;
2034 zfsvfs->z_rootvnode = NULL;
2035 rm_wunlock(&zfsvfs->z_rootvnodelock);
2041 zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp)
2043 struct rm_priotracker tracker;
2044 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2048 rm_rlock(&zfsvfs->z_rootvnodelock, &tracker);
2049 *vpp = zfsvfs->z_rootvnode;
2050 if (*vpp != NULL && (((*vpp)->v_iflag & VI_DOOMED) == 0)) {
2052 rm_runlock(&zfsvfs->z_rootvnodelock, &tracker);
2055 rm_runlock(&zfsvfs->z_rootvnodelock, &tracker);
2058 * We found the vnode but did not like it.
2062 zfs_root_putvnode(zfsvfs);
2066 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
2068 *vpp = ZTOV(rootzp);
2074 error = vn_lock(*vpp, flags);
2084 * Teardown the zfsvfs::z_os.
2086 * Note, if 'unmounting' is FALSE, we return with the 'z_teardown_lock'
2087 * and 'z_teardown_inactive_lock' held.
2090 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
2094 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2098 * We purge the parent filesystem's vfsp as the parent
2099 * filesystem and all of its snapshots have their vnode's
2100 * v_vfsp set to the parent's filesystem's vfsp. Note,
2101 * 'z_parent' is self referential for non-snapshots.
2103 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2104 #ifdef FREEBSD_NAMECACHE
2105 cache_purgevfs(zfsvfs->z_parent->z_vfs, true);
2110 * Close the zil. NB: Can't close the zil while zfs_inactive
2111 * threads are blocked as zil_close can call zfs_inactive.
2113 if (zfsvfs->z_log) {
2114 zil_close(zfsvfs->z_log);
2115 zfsvfs->z_log = NULL;
2118 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
2121 * If we are not unmounting (ie: online recv) and someone already
2122 * unmounted this file system while we were doing the switcheroo,
2123 * or a reopen of z_os failed then just bail out now.
2125 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
2126 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2127 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2128 return (SET_ERROR(EIO));
2132 * At this point there are no vops active, and any new vops will
2133 * fail with EIO since we have z_teardown_lock for writer (only
2134 * relavent for forced unmount).
2136 * Release all holds on dbufs.
2138 mutex_enter(&zfsvfs->z_znodes_lock);
2139 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
2140 zp = list_next(&zfsvfs->z_all_znodes, zp))
2142 ASSERT(ZTOV(zp)->v_count >= 0);
2143 zfs_znode_dmu_fini(zp);
2145 mutex_exit(&zfsvfs->z_znodes_lock);
2148 * If we are unmounting, set the unmounted flag and let new vops
2149 * unblock. zfs_inactive will have the unmounted behavior, and all
2150 * other vops will fail with EIO.
2153 zfsvfs->z_unmounted = B_TRUE;
2154 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2155 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2159 * z_os will be NULL if there was an error in attempting to reopen
2160 * zfsvfs, so just return as the properties had already been
2161 * unregistered and cached data had been evicted before.
2163 if (zfsvfs->z_os == NULL)
2167 * Unregister properties.
2169 zfs_unregister_callbacks(zfsvfs);
2174 if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) &&
2175 !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
2176 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
2177 dmu_objset_evict_dbufs(zfsvfs->z_os);
2184 zfs_umount(vfs_t *vfsp, int fflag)
2186 kthread_t *td = curthread;
2187 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2189 cred_t *cr = td->td_ucred;
2192 zfs_root_putvnode(zfsvfs);
2194 ret = secpolicy_fs_unmount(cr, vfsp);
2196 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
2197 ZFS_DELEG_PERM_MOUNT, cr))
2202 * We purge the parent filesystem's vfsp as the parent filesystem
2203 * and all of its snapshots have their vnode's v_vfsp set to the
2204 * parent's filesystem's vfsp. Note, 'z_parent' is self
2205 * referential for non-snapshots.
2207 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
2210 * Unmount any snapshots mounted under .zfs before unmounting the
2213 if (zfsvfs->z_ctldir != NULL) {
2214 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0)
2218 if (fflag & MS_FORCE) {
2220 * Mark file system as unmounted before calling
2221 * vflush(FORCECLOSE). This way we ensure no future vnops
2222 * will be called and risk operating on DOOMED vnodes.
2224 rrm_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
2225 zfsvfs->z_unmounted = B_TRUE;
2226 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2230 * Flush all the files.
2232 ret = vflush(vfsp, 0, (fflag & MS_FORCE) ? FORCECLOSE : 0, td);
2237 if (!(fflag & MS_FORCE)) {
2239 * Check the number of active vnodes in the file system.
2240 * Our count is maintained in the vfs structure, but the
2241 * number is off by 1 to indicate a hold on the vfs
2244 * The '.zfs' directory maintains a reference of its
2245 * own, and any active references underneath are
2246 * reflected in the vnode count.
2248 if (zfsvfs->z_ctldir == NULL) {
2249 if (vfsp->vfs_count > 1)
2250 return (SET_ERROR(EBUSY));
2252 if (vfsp->vfs_count > 2 ||
2253 zfsvfs->z_ctldir->v_count > 1)
2254 return (SET_ERROR(EBUSY));
2259 while (taskqueue_cancel(zfsvfs_taskq->tq_queue,
2260 &zfsvfs->z_unlinked_drain_task, NULL) != 0)
2261 taskqueue_drain(zfsvfs_taskq->tq_queue,
2262 &zfsvfs->z_unlinked_drain_task);
2264 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
2268 * z_os will be NULL if there was an error in
2269 * attempting to reopen zfsvfs.
2273 * Unset the objset user_ptr.
2275 mutex_enter(&os->os_user_ptr_lock);
2276 dmu_objset_set_user(os, NULL);
2277 mutex_exit(&os->os_user_ptr_lock);
2280 * Finally release the objset
2282 dmu_objset_disown(os, zfsvfs);
2286 * We can now safely destroy the '.zfs' directory node.
2288 if (zfsvfs->z_ctldir != NULL)
2289 zfsctl_destroy(zfsvfs);
2296 zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp)
2298 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2303 * zfs_zget() can't operate on virtual entries like .zfs/ or
2304 * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP.
2305 * This will make NFS to switch to LOOKUP instead of using VGET.
2307 if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR ||
2308 (zfsvfs->z_shares_dir != 0 && ino == zfsvfs->z_shares_dir))
2309 return (EOPNOTSUPP);
2312 err = zfs_zget(zfsvfs, ino, &zp);
2313 if (err == 0 && zp->z_unlinked) {
2321 err = vn_lock(*vpp, flags);
2328 zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp,
2329 struct ucred **credanonp, int *numsecflavors, int **secflavors)
2331 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2334 * If this is regular file system vfsp is the same as
2335 * zfsvfs->z_parent->z_vfs, but if it is snapshot,
2336 * zfsvfs->z_parent->z_vfs represents parent file system
2337 * which we have to use here, because only this file system
2338 * has mnt_export configured.
2340 return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp,
2341 credanonp, numsecflavors, secflavors));
2344 CTASSERT(SHORT_FID_LEN <= sizeof(struct fid));
2345 CTASSERT(LONG_FID_LEN <= sizeof(struct fid));
2348 zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp)
2350 struct componentname cn;
2351 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2354 uint64_t object = 0;
2355 uint64_t fid_gen = 0;
2365 * On FreeBSD we can get snapshot's mount point or its parent file
2366 * system mount point depending if snapshot is already mounted or not.
2368 if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) {
2369 zfid_long_t *zlfid = (zfid_long_t *)fidp;
2370 uint64_t objsetid = 0;
2371 uint64_t setgen = 0;
2373 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
2374 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
2376 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
2377 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
2381 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
2383 return (SET_ERROR(EINVAL));
2387 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
2388 zfid_short_t *zfid = (zfid_short_t *)fidp;
2390 for (i = 0; i < sizeof (zfid->zf_object); i++)
2391 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
2393 for (i = 0; i < sizeof (zfid->zf_gen); i++)
2394 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
2397 return (SET_ERROR(EINVAL));
2401 * A zero fid_gen means we are in .zfs or the .zfs/snapshot
2402 * directory tree. If the object == zfsvfs->z_shares_dir, then
2403 * we are in the .zfs/shares directory tree.
2405 if ((fid_gen == 0 &&
2406 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) ||
2407 (zfsvfs->z_shares_dir != 0 && object == zfsvfs->z_shares_dir)) {
2409 VERIFY0(zfsctl_root(zfsvfs, LK_SHARED, &dvp));
2410 if (object == ZFSCTL_INO_SNAPDIR) {
2411 cn.cn_nameptr = "snapshot";
2412 cn.cn_namelen = strlen(cn.cn_nameptr);
2413 cn.cn_nameiop = LOOKUP;
2414 cn.cn_flags = ISLASTCN | LOCKLEAF;
2415 cn.cn_lkflags = flags;
2416 VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
2418 } else if (object == zfsvfs->z_shares_dir) {
2420 * XXX This branch must not be taken,
2421 * if it is, then the lookup below will
2424 cn.cn_nameptr = "shares";
2425 cn.cn_namelen = strlen(cn.cn_nameptr);
2426 cn.cn_nameiop = LOOKUP;
2427 cn.cn_flags = ISLASTCN;
2428 cn.cn_lkflags = flags;
2429 VERIFY0(VOP_LOOKUP(dvp, vpp, &cn));
2437 gen_mask = -1ULL >> (64 - 8 * i);
2439 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
2440 if (err = zfs_zget(zfsvfs, object, &zp)) {
2444 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
2446 zp_gen = zp_gen & gen_mask;
2449 if (zp->z_unlinked || zp_gen != fid_gen) {
2450 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
2453 return (SET_ERROR(EINVAL));
2458 err = vn_lock(*vpp, flags);
2460 vnode_create_vobject(*vpp, zp->z_size, curthread);
2467 * Block out VOPs and close zfsvfs_t::z_os
2469 * Note, if successful, then we return with the 'z_teardown_lock' and
2470 * 'z_teardown_inactive_lock' write held. We leave ownership of the underlying
2471 * dataset and objset intact so that they can be atomically handed off during
2472 * a subsequent rollback or recv operation and the resume thereafter.
2475 zfs_suspend_fs(zfsvfs_t *zfsvfs)
2479 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
2486 * Rebuild SA and release VOPs. Note that ownership of the underlying dataset
2487 * is an invariant across any of the operations that can be performed while the
2488 * filesystem was suspended. Whether it succeeded or failed, the preconditions
2489 * are the same: the relevant objset and associated dataset are owned by
2490 * zfsvfs, held, and long held on entry.
2493 zfs_resume_fs(zfsvfs_t *zfsvfs, dsl_dataset_t *ds)
2498 ASSERT(RRM_WRITE_HELD(&zfsvfs->z_teardown_lock));
2499 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
2502 * We already own this, so just update the objset_t, as the one we
2503 * had before may have been evicted.
2506 VERIFY3P(ds->ds_owner, ==, zfsvfs);
2507 VERIFY(dsl_dataset_long_held(ds));
2508 VERIFY0(dmu_objset_from_ds(ds, &os));
2510 err = zfsvfs_init(zfsvfs, os);
2514 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
2516 zfs_set_fuid_feature(zfsvfs);
2519 * Attempt to re-establish all the active znodes with
2520 * their dbufs. If a zfs_rezget() fails, then we'll let
2521 * any potential callers discover that via ZFS_ENTER_VERIFY_VP
2522 * when they try to use their znode.
2524 mutex_enter(&zfsvfs->z_znodes_lock);
2525 for (zp = list_head(&zfsvfs->z_all_znodes); zp;
2526 zp = list_next(&zfsvfs->z_all_znodes, zp)) {
2527 (void) zfs_rezget(zp);
2529 mutex_exit(&zfsvfs->z_znodes_lock);
2532 /* release the VOPs */
2533 rw_exit(&zfsvfs->z_teardown_inactive_lock);
2534 rrm_exit(&zfsvfs->z_teardown_lock, FTAG);
2538 * Since we couldn't setup the sa framework, try to force
2539 * unmount this file system.
2541 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) {
2542 vfs_ref(zfsvfs->z_vfs);
2543 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread);
2550 zfs_freevfs(vfs_t *vfsp)
2552 zfsvfs_t *zfsvfs = vfsp->vfs_data;
2556 * If this is a snapshot, we have an extra VFS_HOLD on our parent
2557 * from zfs_mount(). Release it here. If we came through
2558 * zfs_mountroot() instead, we didn't grab an extra hold, so
2559 * skip the VFS_RELE for rootvfs.
2561 if (zfsvfs->z_issnap && (vfsp != rootvfs))
2562 VFS_RELE(zfsvfs->z_parent->z_vfs);
2565 zfsvfs_free(zfsvfs);
2567 atomic_dec_32(&zfs_active_fs_count);
2571 static int desiredvnodes_backup;
2575 zfs_vnodes_adjust(void)
2578 int newdesiredvnodes;
2580 desiredvnodes_backup = desiredvnodes;
2583 * We calculate newdesiredvnodes the same way it is done in
2584 * vntblinit(). If it is equal to desiredvnodes, it means that
2585 * it wasn't tuned by the administrator and we can tune it down.
2587 newdesiredvnodes = min(maxproc + vm_cnt.v_page_count / 4, 2 *
2588 vm_kmem_size / (5 * (sizeof(struct vm_object) +
2589 sizeof(struct vnode))));
2590 if (newdesiredvnodes == desiredvnodes)
2591 desiredvnodes = (3 * newdesiredvnodes) / 4;
2596 zfs_vnodes_adjust_back(void)
2600 desiredvnodes = desiredvnodes_backup;
2608 printf("ZFS filesystem version: " ZPL_VERSION_STRING "\n");
2611 * Initialize .zfs directory structures
2616 * Initialize znode cache, vnode ops, etc...
2621 * Reduce number of vnodes. Originally number of vnodes is calculated
2622 * with UFS inode in mind. We reduce it here, because it's too big for
2625 zfs_vnodes_adjust();
2627 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
2628 #if defined(__FreeBSD__)
2629 zfsvfs_taskq = taskq_create("zfsvfs", 1, minclsyspri, 0, 0, 0);
2636 #if defined(__FreeBSD__)
2637 taskq_destroy(zfsvfs_taskq);
2641 zfs_vnodes_adjust_back();
2647 return (zfs_active_fs_count != 0);
2651 zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
2654 objset_t *os = zfsvfs->z_os;
2657 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
2658 return (SET_ERROR(EINVAL));
2660 if (newvers < zfsvfs->z_version)
2661 return (SET_ERROR(EINVAL));
2663 if (zfs_spa_version_map(newvers) >
2664 spa_version(dmu_objset_spa(zfsvfs->z_os)))
2665 return (SET_ERROR(ENOTSUP));
2667 tx = dmu_tx_create(os);
2668 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
2669 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2670 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
2672 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2674 error = dmu_tx_assign(tx, TXG_WAIT);
2680 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
2681 8, 1, &newvers, tx);
2688 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
2691 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
2693 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
2694 DMU_OT_NONE, 0, tx);
2696 error = zap_add(os, MASTER_NODE_OBJ,
2697 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
2700 VERIFY(0 == sa_set_sa_object(os, sa_obj));
2701 sa_register_update_callback(os, zfs_sa_upgrade);
2704 spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
2705 "from %llu to %llu", zfsvfs->z_version, newvers);
2709 zfsvfs->z_version = newvers;
2710 os->os_version = newvers;
2712 zfs_set_fuid_feature(zfsvfs);
2718 * Read a property stored within the master node.
2721 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
2723 uint64_t *cached_copy = NULL;
2726 * Figure out where in the objset_t the cached copy would live, if it
2727 * is available for the requested property.
2731 case ZFS_PROP_VERSION:
2732 cached_copy = &os->os_version;
2734 case ZFS_PROP_NORMALIZE:
2735 cached_copy = &os->os_normalization;
2737 case ZFS_PROP_UTF8ONLY:
2738 cached_copy = &os->os_utf8only;
2741 cached_copy = &os->os_casesensitivity;
2747 if (cached_copy != NULL && *cached_copy != OBJSET_PROP_UNINITIALIZED) {
2748 *value = *cached_copy;
2753 * If the property wasn't cached, look up the file system's value for
2754 * the property. For the version property, we look up a slightly
2759 if (prop == ZFS_PROP_VERSION) {
2760 pname = ZPL_VERSION_STR;
2762 pname = zfs_prop_to_name(prop);
2766 ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);
2767 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
2770 if (error == ENOENT) {
2771 /* No value set, use the default value */
2773 case ZFS_PROP_VERSION:
2774 *value = ZPL_VERSION;
2776 case ZFS_PROP_NORMALIZE:
2777 case ZFS_PROP_UTF8ONLY:
2781 *value = ZFS_CASE_SENSITIVE;
2790 * If one of the methods for getting the property value above worked,
2791 * copy it into the objset_t's cache.
2793 if (error == 0 && cached_copy != NULL) {
2794 *cached_copy = *value;
2801 * Return true if the coresponding vfs's unmounted flag is set.
2802 * Otherwise return false.
2803 * If this function returns true we know VFS unmount has been initiated.
2806 zfs_get_vfs_flag_unmounted(objset_t *os)
2809 boolean_t unmounted = B_FALSE;
2811 ASSERT(dmu_objset_type(os) == DMU_OST_ZFS);
2813 mutex_enter(&os->os_user_ptr_lock);
2814 zfvp = dmu_objset_get_user(os);
2815 if (zfvp != NULL && zfvp->z_vfs != NULL &&
2816 (zfvp->z_vfs->mnt_kern_flag & MNTK_UNMOUNT))
2818 mutex_exit(&os->os_user_ptr_lock);
2825 zfsvfs_update_fromname(const char *oldname, const char *newname)
2827 char tmpbuf[MAXPATHLEN];
2832 oldlen = strlen(oldname);
2834 mtx_lock(&mountlist_mtx);
2835 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2836 fromname = mp->mnt_stat.f_mntfromname;
2837 if (strcmp(fromname, oldname) == 0) {
2838 (void)strlcpy(fromname, newname,
2839 sizeof(mp->mnt_stat.f_mntfromname));
2842 if (strncmp(fromname, oldname, oldlen) == 0 &&
2843 (fromname[oldlen] == '/' || fromname[oldlen] == '@')) {
2844 (void)snprintf(tmpbuf, sizeof(tmpbuf), "%s%s",
2845 newname, fromname + oldlen);
2846 (void)strlcpy(fromname, tmpbuf,
2847 sizeof(mp->mnt_stat.f_mntfromname));
2851 mtx_unlock(&mountlist_mtx);