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 * Portions Copyright 2011 Martin Matuska
24 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/errno.h>
32 #include <sys/modctl.h>
37 #include <sys/cmn_err.h>
39 #include <sys/zfs_ioctl.h>
40 #include <sys/zfs_vfsops.h>
41 #include <sys/zfs_znode.h>
44 #include <sys/spa_impl.h>
46 #include <sys/priv_impl.h>
48 #include <sys/dsl_dir.h>
49 #include <sys/dsl_dataset.h>
50 #include <sys/dsl_prop.h>
51 #include <sys/dsl_deleg.h>
52 #include <sys/dmu_objset.h>
54 #include <sys/sunddi.h>
55 #include <sys/sunldi.h>
56 #include <sys/policy.h>
58 #include <sys/nvpair.h>
59 #include <sys/pathname.h>
60 #include <sys/mount.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/zfs_ctldir.h>
64 #include <sys/zfs_dir.h>
65 #include <sys/zfs_onexit.h>
67 #include <sys/dsl_scan.h>
68 #include <sharefs/share.h>
69 #include <sys/dmu_objset.h>
70 #include <sys/fm/util.h>
72 #include <linux/miscdevice.h>
74 #include "zfs_namecheck.h"
76 #include "zfs_deleg.h"
77 #include "zfs_comutil.h"
79 kmutex_t zfsdev_state_lock;
80 list_t zfsdev_state_list;
82 extern void zfs_init(void);
83 extern void zfs_fini(void);
85 typedef int zfs_ioc_func_t(zfs_cmd_t *);
86 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
92 } zfs_ioc_namecheck_t;
95 POOL_CHECK_NONE = 1 << 0,
96 POOL_CHECK_SUSPENDED = 1 << 1,
97 POOL_CHECK_READONLY = 1 << 2
98 } zfs_ioc_poolcheck_t;
100 typedef struct zfs_ioc_vec {
101 zfs_ioc_func_t *zvec_func;
102 zfs_secpolicy_func_t *zvec_secpolicy;
103 zfs_ioc_namecheck_t zvec_namecheck;
104 boolean_t zvec_his_log;
105 zfs_ioc_poolcheck_t zvec_pool_check;
108 /* This array is indexed by zfs_userquota_prop_t */
109 static const char *userquota_perms[] = {
110 ZFS_DELEG_PERM_USERUSED,
111 ZFS_DELEG_PERM_USERQUOTA,
112 ZFS_DELEG_PERM_GROUPUSED,
113 ZFS_DELEG_PERM_GROUPQUOTA,
116 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
117 static int zfs_check_settable(const char *name, nvpair_t *property,
119 static int zfs_check_clearable(char *dataset, nvlist_t *props,
121 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
123 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
126 history_str_free(char *buf)
128 kmem_free(buf, HIS_MAX_RECORD_LEN);
132 history_str_get(zfs_cmd_t *zc)
136 if (zc->zc_history == 0)
139 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP | KM_NODEBUG);
140 if (copyinstr((void *)(uintptr_t)zc->zc_history,
141 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
142 history_str_free(buf);
146 buf[HIS_MAX_RECORD_LEN -1] = '\0';
152 * Check to see if the named dataset is currently defined as bootable
155 zfs_is_bootfs(const char *name)
159 if (dmu_objset_hold(name, FTAG, &os) == 0) {
161 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
162 dmu_objset_rele(os, FTAG);
169 * zfs_earlier_version
171 * Return non-zero if the spa version is less than requested version.
174 zfs_earlier_version(const char *name, int version)
178 if (spa_open(name, &spa, FTAG) == 0) {
179 if (spa_version(spa) < version) {
180 spa_close(spa, FTAG);
183 spa_close(spa, FTAG);
189 * zpl_earlier_version
191 * Return TRUE if the ZPL version is less than requested version.
194 zpl_earlier_version(const char *name, int version)
197 boolean_t rc = B_TRUE;
199 if (dmu_objset_hold(name, FTAG, &os) == 0) {
202 if (dmu_objset_type(os) != DMU_OST_ZFS) {
203 dmu_objset_rele(os, FTAG);
206 /* XXX reading from non-owned objset */
207 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
208 rc = zplversion < version;
209 dmu_objset_rele(os, FTAG);
215 zfs_log_history(zfs_cmd_t *zc)
220 if ((buf = history_str_get(zc)) == NULL)
223 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
224 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
225 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
226 spa_close(spa, FTAG);
228 history_str_free(buf);
232 * Policy for top-level read operations (list pools). Requires no privileges,
233 * and can be used in the local zone, as there is no associated dataset.
237 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
243 * Policy for dataset read operations (list children, get statistics). Requires
244 * no privileges, but must be visible in the local zone.
248 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
250 if (INGLOBALZONE(curproc) ||
251 zone_dataset_visible(zc->zc_name, NULL))
258 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
263 * The dataset must be visible by this zone -- check this first
264 * so they don't see EPERM on something they shouldn't know about.
266 if (!INGLOBALZONE(curproc) &&
267 !zone_dataset_visible(dataset, &writable))
270 if (INGLOBALZONE(curproc)) {
272 * If the fs is zoned, only root can access it from the
275 if (secpolicy_zfs(cr) && zoned)
279 * If we are in a local zone, the 'zoned' property must be set.
284 /* must be writable by this zone */
292 zfs_dozonecheck(const char *dataset, cred_t *cr)
296 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
299 return (zfs_dozonecheck_impl(dataset, zoned, cr));
303 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
307 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
308 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
309 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
312 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
314 return (zfs_dozonecheck_impl(dataset, zoned, cr));
318 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
322 error = zfs_dozonecheck(name, cr);
324 error = secpolicy_zfs(cr);
326 error = dsl_deleg_access(name, perm, cr);
332 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
333 const char *perm, cred_t *cr)
337 error = zfs_dozonecheck_ds(name, ds, cr);
339 error = secpolicy_zfs(cr);
341 error = dsl_deleg_access_impl(ds, perm, cr);
347 * Policy for setting the security label property.
349 * Returns 0 for success, non-zero for access and other errors.
352 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
355 char ds_hexsl[MAXNAMELEN];
356 bslabel_t ds_sl, new_sl;
357 boolean_t new_default = FALSE;
359 int needed_priv = -1;
362 /* First get the existing dataset label. */
363 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
364 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
368 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
371 /* The label must be translatable */
372 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
376 * In a non-global zone, disallow attempts to set a label that
377 * doesn't match that of the zone; otherwise no other checks
380 if (!INGLOBALZONE(curproc)) {
381 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
387 * For global-zone datasets (i.e., those whose zoned property is
388 * "off", verify that the specified new label is valid for the
391 if (dsl_prop_get_integer(name,
392 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
395 if (zfs_check_global_label(name, strval) != 0)
400 * If the existing dataset label is nondefault, check if the
401 * dataset is mounted (label cannot be changed while mounted).
402 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
403 * mounted (or isn't a dataset, doesn't exist, ...).
405 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
407 static char *setsl_tag = "setsl_tag";
410 * Try to own the dataset; abort if there is any error,
411 * (e.g., already mounted, in use, or other error).
413 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
418 dmu_objset_disown(os, setsl_tag);
421 needed_priv = PRIV_FILE_DOWNGRADE_SL;
425 if (hexstr_to_label(strval, &new_sl) != 0)
428 if (blstrictdom(&ds_sl, &new_sl))
429 needed_priv = PRIV_FILE_DOWNGRADE_SL;
430 else if (blstrictdom(&new_sl, &ds_sl))
431 needed_priv = PRIV_FILE_UPGRADE_SL;
433 /* dataset currently has a default label */
435 needed_priv = PRIV_FILE_UPGRADE_SL;
439 if (needed_priv != -1)
440 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
444 #endif /* HAVE_MLSLABEL */
448 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
454 * Check permissions for special properties.
461 * Disallow setting of 'zoned' from within a local zone.
463 if (!INGLOBALZONE(curproc))
468 if (!INGLOBALZONE(curproc)) {
470 char setpoint[MAXNAMELEN];
472 * Unprivileged users are allowed to modify the
473 * quota on things *under* (ie. contained by)
474 * the thing they own.
476 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
479 if (!zoned || strlen(dsname) <= strlen(setpoint))
484 case ZFS_PROP_MLSLABEL:
485 if (!is_system_labeled())
488 if (nvpair_value_string(propval, &strval) == 0) {
491 err = zfs_set_slabel_policy(dsname, strval, CRED());
498 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
502 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
506 error = zfs_dozonecheck(zc->zc_name, cr);
511 * permission to set permissions will be evaluated later in
512 * dsl_deleg_can_allow()
518 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
520 return (zfs_secpolicy_write_perms(zc->zc_name,
521 ZFS_DELEG_PERM_ROLLBACK, cr));
525 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
534 * Generate the current snapshot name from the given objsetid, then
535 * use that name for the secpolicy/zone checks.
537 cp = strchr(zc->zc_name, '@');
540 error = spa_open(zc->zc_name, &spa, FTAG);
544 dp = spa_get_dsl(spa);
545 rw_enter(&dp->dp_config_rwlock, RW_READER);
546 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
547 rw_exit(&dp->dp_config_rwlock);
548 spa_close(spa, FTAG);
552 dsl_dataset_name(ds, zc->zc_name);
554 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
555 ZFS_DELEG_PERM_SEND, cr);
556 dsl_dataset_rele(ds, FTAG);
561 #ifdef HAVE_SMB_SHARE
563 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
568 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
569 NO_FOLLOW, NULL, &vp)) != 0)
572 /* Now make sure mntpnt and dataset are ZFS */
574 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
575 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
576 zc->zc_name) != 0)) {
582 return (dsl_deleg_access(zc->zc_name,
583 ZFS_DELEG_PERM_SHARE, cr));
585 #endif /* HAVE_SMB_SHARE */
588 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
590 #ifdef HAVE_SMB_SHARE
591 if (!INGLOBALZONE(curproc))
594 if (secpolicy_nfs(cr) == 0) {
597 return (zfs_secpolicy_deleg_share(zc, cr));
601 #endif /* HAVE_SMB_SHARE */
605 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
607 #ifdef HAVE_SMB_SHARE
608 if (!INGLOBALZONE(curproc))
611 if (secpolicy_smb(cr) == 0) {
614 return (zfs_secpolicy_deleg_share(zc, cr));
618 #endif /* HAVE_SMB_SHARE */
622 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
627 * Remove the @bla or /bla from the end of the name to get the parent.
629 (void) strncpy(parent, datasetname, parentsize);
630 cp = strrchr(parent, '@');
634 cp = strrchr(parent, '/');
644 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
648 if ((error = zfs_secpolicy_write_perms(name,
649 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
652 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
656 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
658 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
662 * Destroying snapshots with delegated permissions requires
663 * descendent mount and destroy permissions.
664 * Reassemble the full filesystem@snap name so dsl_deleg_access()
665 * can do the correct permission check.
667 * Since this routine is used when doing a recursive destroy of snapshots
668 * and destroying snapshots requires descendent permissions, a successfull
669 * check of the top level snapshot applies to snapshots of all descendent
672 * The target snapshot may not exist when doing a recursive destroy.
673 * In this case fallback to permissions of the parent dataset.
676 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr)
681 dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value);
683 error = zfs_secpolicy_destroy_perms(dsname, cr);
685 error = zfs_secpolicy_destroy_perms(zc->zc_name, cr);
692 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
694 char parentname[MAXNAMELEN];
697 if ((error = zfs_secpolicy_write_perms(from,
698 ZFS_DELEG_PERM_RENAME, cr)) != 0)
701 if ((error = zfs_secpolicy_write_perms(from,
702 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
705 if ((error = zfs_get_parent(to, parentname,
706 sizeof (parentname))) != 0)
709 if ((error = zfs_secpolicy_write_perms(parentname,
710 ZFS_DELEG_PERM_CREATE, cr)) != 0)
713 if ((error = zfs_secpolicy_write_perms(parentname,
714 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
721 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
723 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
727 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
729 char parentname[MAXNAMELEN];
733 error = zfs_secpolicy_write_perms(zc->zc_name,
734 ZFS_DELEG_PERM_PROMOTE, cr);
738 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
741 dsl_dataset_t *pclone = NULL;
743 dd = clone->os_dsl_dataset->ds_dir;
745 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
746 error = dsl_dataset_hold_obj(dd->dd_pool,
747 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
748 rw_exit(&dd->dd_pool->dp_config_rwlock);
750 dmu_objset_rele(clone, FTAG);
754 error = zfs_secpolicy_write_perms(zc->zc_name,
755 ZFS_DELEG_PERM_MOUNT, cr);
757 dsl_dataset_name(pclone, parentname);
758 dmu_objset_rele(clone, FTAG);
759 dsl_dataset_rele(pclone, FTAG);
761 error = zfs_secpolicy_write_perms(parentname,
762 ZFS_DELEG_PERM_PROMOTE, cr);
768 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
772 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
773 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
776 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
777 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
780 return (zfs_secpolicy_write_perms(zc->zc_name,
781 ZFS_DELEG_PERM_CREATE, cr));
785 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
787 return (zfs_secpolicy_write_perms(name,
788 ZFS_DELEG_PERM_SNAPSHOT, cr));
792 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
795 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
799 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
801 char parentname[MAXNAMELEN];
804 if ((error = zfs_get_parent(zc->zc_name, parentname,
805 sizeof (parentname))) != 0)
808 if (zc->zc_value[0] != '\0') {
809 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
810 ZFS_DELEG_PERM_CLONE, cr)) != 0)
814 if ((error = zfs_secpolicy_write_perms(parentname,
815 ZFS_DELEG_PERM_CREATE, cr)) != 0)
818 error = zfs_secpolicy_write_perms(parentname,
819 ZFS_DELEG_PERM_MOUNT, cr);
825 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
826 * SYS_CONFIG privilege, which is not available in a local zone.
830 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
832 if (secpolicy_sys_config(cr, B_FALSE) != 0)
839 * Policy for object to name lookups.
843 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
847 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
850 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
855 * Policy for fault injection. Requires all privileges.
859 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
861 return (secpolicy_zinject(cr));
865 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
867 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
869 if (prop == ZPROP_INVAL) {
870 if (!zfs_prop_user(zc->zc_value))
872 return (zfs_secpolicy_write_perms(zc->zc_name,
873 ZFS_DELEG_PERM_USERPROP, cr));
875 return (zfs_secpolicy_setprop(zc->zc_name, prop,
881 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
883 int err = zfs_secpolicy_read(zc, cr);
887 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
890 if (zc->zc_value[0] == 0) {
892 * They are asking about a posix uid/gid. If it's
893 * themself, allow it.
895 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
896 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
897 if (zc->zc_guid == crgetuid(cr))
900 if (groupmember(zc->zc_guid, cr))
905 return (zfs_secpolicy_write_perms(zc->zc_name,
906 userquota_perms[zc->zc_objset_type], cr));
910 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
912 int err = zfs_secpolicy_read(zc, cr);
916 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
919 return (zfs_secpolicy_write_perms(zc->zc_name,
920 userquota_perms[zc->zc_objset_type], cr));
924 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
926 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
931 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
933 return (zfs_secpolicy_write_perms(zc->zc_name,
934 ZFS_DELEG_PERM_HOLD, cr));
938 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
940 return (zfs_secpolicy_write_perms(zc->zc_name,
941 ZFS_DELEG_PERM_RELEASE, cr));
945 * Policy for allowing temporary snapshots to be taken or released
948 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
951 * A temporary snapshot is the same as a snapshot,
952 * hold, destroy and release all rolled into one.
953 * Delegated diff alone is sufficient that we allow this.
957 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
958 ZFS_DELEG_PERM_DIFF, cr)) == 0)
961 error = zfs_secpolicy_snapshot(zc, cr);
963 error = zfs_secpolicy_hold(zc, cr);
965 error = zfs_secpolicy_release(zc, cr);
967 error = zfs_secpolicy_destroy(zc, cr);
972 * Returns the nvlist as specified by the user in the zfs_cmd_t.
975 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
979 nvlist_t *list = NULL;
982 * Read in and unpack the user-supplied nvlist.
987 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
989 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
991 kmem_free(packed, size);
995 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
996 kmem_free(packed, size);
1000 kmem_free(packed, size);
1007 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1011 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1013 if (size > zc->zc_nvlist_dst_size) {
1014 nvpair_t *more_errors;
1017 if (zc->zc_nvlist_dst_size < 1024)
1020 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1021 more_errors = nvlist_prev_nvpair(*errors, NULL);
1024 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1026 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1028 VERIFY(nvlist_size(*errors, &size,
1029 NV_ENCODE_NATIVE) == 0);
1030 } while (size > zc->zc_nvlist_dst_size);
1032 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1033 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1034 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1035 ASSERT(size <= zc->zc_nvlist_dst_size);
1042 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1044 char *packed = NULL;
1048 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1050 if (size > zc->zc_nvlist_dst_size) {
1053 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1054 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1056 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1057 size, zc->zc_iflags) != 0)
1059 kmem_free(packed, size);
1062 zc->zc_nvlist_dst_size = size;
1067 get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
1072 error = dmu_objset_hold(dsname, FTAG, &os);
1075 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1076 dmu_objset_rele(os, FTAG);
1080 mutex_enter(&os->os_user_ptr_lock);
1081 *zsbp = dmu_objset_get_user(os);
1082 if (*zsbp && (*zsbp)->z_sb) {
1083 atomic_inc(&((*zsbp)->z_sb->s_active));
1087 mutex_exit(&os->os_user_ptr_lock);
1088 dmu_objset_rele(os, FTAG);
1093 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1094 * case its z_sb will be NULL, and it will be opened as the owner.
1097 zfs_sb_hold(const char *name, void *tag, zfs_sb_t **zsbp, boolean_t writer)
1101 if (get_zfs_sb(name, zsbp) != 0)
1102 error = zfs_sb_create(name, zsbp);
1104 rrw_enter(&(*zsbp)->z_teardown_lock, (writer) ? RW_WRITER :
1106 if ((*zsbp)->z_unmounted) {
1108 * XXX we could probably try again, since the unmounting
1109 * thread should be just about to disassociate the
1110 * objset from the zfsvfs.
1112 rrw_exit(&(*zsbp)->z_teardown_lock, tag);
1120 zfs_sb_rele(zfs_sb_t *zsb, void *tag)
1122 rrw_exit(&zsb->z_teardown_lock, tag);
1125 deactivate_super(zsb->z_sb);
1127 dmu_objset_disown(zsb->z_os, zsb);
1133 zfs_ioc_pool_create(zfs_cmd_t *zc)
1136 nvlist_t *config, *props = NULL;
1137 nvlist_t *rootprops = NULL;
1138 nvlist_t *zplprops = NULL;
1141 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1142 zc->zc_iflags, &config)))
1145 if (zc->zc_nvlist_src_size != 0 && (error =
1146 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1147 zc->zc_iflags, &props))) {
1148 nvlist_free(config);
1153 nvlist_t *nvl = NULL;
1154 uint64_t version = SPA_VERSION;
1156 (void) nvlist_lookup_uint64(props,
1157 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1158 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1160 goto pool_props_bad;
1162 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1164 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1166 nvlist_free(config);
1170 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1172 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1173 error = zfs_fill_zplprops_root(version, rootprops,
1176 goto pool_props_bad;
1179 buf = history_str_get(zc);
1181 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1184 * Set the remaining root properties
1186 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1187 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1188 (void) spa_destroy(zc->zc_name);
1191 history_str_free(buf);
1194 nvlist_free(rootprops);
1195 nvlist_free(zplprops);
1196 nvlist_free(config);
1203 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1206 zfs_log_history(zc);
1207 error = spa_destroy(zc->zc_name);
1209 zvol_remove_minors(zc->zc_name);
1214 zfs_ioc_pool_import(zfs_cmd_t *zc)
1216 nvlist_t *config, *props = NULL;
1220 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1221 zc->zc_iflags, &config)) != 0)
1224 if (zc->zc_nvlist_src_size != 0 && (error =
1225 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1226 zc->zc_iflags, &props))) {
1227 nvlist_free(config);
1231 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1232 guid != zc->zc_guid)
1235 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1237 if (zc->zc_nvlist_dst != 0) {
1240 if ((err = put_nvlist(zc, config)) != 0)
1245 zvol_create_minors(zc->zc_name);
1247 nvlist_free(config);
1256 zfs_ioc_pool_export(zfs_cmd_t *zc)
1259 boolean_t force = (boolean_t)zc->zc_cookie;
1260 boolean_t hardforce = (boolean_t)zc->zc_guid;
1262 zfs_log_history(zc);
1263 error = spa_export(zc->zc_name, NULL, force, hardforce);
1265 zvol_remove_minors(zc->zc_name);
1270 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1275 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1278 error = put_nvlist(zc, configs);
1280 nvlist_free(configs);
1286 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1292 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1293 sizeof (zc->zc_value));
1295 if (config != NULL) {
1296 ret = put_nvlist(zc, config);
1297 nvlist_free(config);
1300 * The config may be present even if 'error' is non-zero.
1301 * In this case we return success, and preserve the real errno
1304 zc->zc_cookie = error;
1313 * Try to import the given pool, returning pool stats as appropriate so that
1314 * user land knows which devices are available and overall pool health.
1317 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1319 nvlist_t *tryconfig, *config;
1322 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1323 zc->zc_iflags, &tryconfig)) != 0)
1326 config = spa_tryimport(tryconfig);
1328 nvlist_free(tryconfig);
1333 error = put_nvlist(zc, config);
1334 nvlist_free(config);
1341 * zc_name name of the pool
1342 * zc_cookie scan func (pool_scan_func_t)
1345 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1350 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1353 if (zc->zc_cookie == POOL_SCAN_NONE)
1354 error = spa_scan_stop(spa);
1356 error = spa_scan(spa, zc->zc_cookie);
1358 spa_close(spa, FTAG);
1364 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1369 error = spa_open(zc->zc_name, &spa, FTAG);
1372 spa_close(spa, FTAG);
1378 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1383 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1386 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1387 spa_close(spa, FTAG);
1391 spa_upgrade(spa, zc->zc_cookie);
1392 spa_close(spa, FTAG);
1398 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1405 if ((size = zc->zc_history_len) == 0)
1408 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1411 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1412 spa_close(spa, FTAG);
1416 hist_buf = vmem_alloc(size, KM_SLEEP);
1417 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1418 &zc->zc_history_len, hist_buf)) == 0) {
1419 error = ddi_copyout(hist_buf,
1420 (void *)(uintptr_t)zc->zc_history,
1421 zc->zc_history_len, zc->zc_iflags);
1424 spa_close(spa, FTAG);
1425 vmem_free(hist_buf, size);
1430 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1434 if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1442 * zc_name name of filesystem
1443 * zc_obj object to find
1446 * zc_value name of object
1449 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1454 /* XXX reading from objset not owned */
1455 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1457 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1458 dmu_objset_rele(os, FTAG);
1461 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1462 sizeof (zc->zc_value));
1463 dmu_objset_rele(os, FTAG);
1470 * zc_name name of filesystem
1471 * zc_obj object to find
1474 * zc_stat stats on object
1475 * zc_value path to object
1478 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1483 /* XXX reading from objset not owned */
1484 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1486 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1487 dmu_objset_rele(os, FTAG);
1490 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1491 sizeof (zc->zc_value));
1492 dmu_objset_rele(os, FTAG);
1498 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1502 nvlist_t *config, **l2cache, **spares;
1503 uint_t nl2cache = 0, nspares = 0;
1505 error = spa_open(zc->zc_name, &spa, FTAG);
1509 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1510 zc->zc_iflags, &config);
1511 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1512 &l2cache, &nl2cache);
1514 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1518 * A root pool with concatenated devices is not supported.
1519 * Thus, can not add a device to a root pool.
1521 * Intent log device can not be added to a rootpool because
1522 * during mountroot, zil is replayed, a seperated log device
1523 * can not be accessed during the mountroot time.
1525 * l2cache and spare devices are ok to be added to a rootpool.
1527 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1528 nvlist_free(config);
1529 spa_close(spa, FTAG);
1534 error = spa_vdev_add(spa, config);
1535 nvlist_free(config);
1537 spa_close(spa, FTAG);
1543 * zc_name name of the pool
1544 * zc_nvlist_conf nvlist of devices to remove
1545 * zc_cookie to stop the remove?
1548 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1553 error = spa_open(zc->zc_name, &spa, FTAG);
1556 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1557 spa_close(spa, FTAG);
1562 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1566 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1568 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1570 switch (zc->zc_cookie) {
1571 case VDEV_STATE_ONLINE:
1572 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1575 case VDEV_STATE_OFFLINE:
1576 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1579 case VDEV_STATE_FAULTED:
1580 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1581 zc->zc_obj != VDEV_AUX_EXTERNAL)
1582 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1584 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1587 case VDEV_STATE_DEGRADED:
1588 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1589 zc->zc_obj != VDEV_AUX_EXTERNAL)
1590 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1592 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1598 zc->zc_cookie = newstate;
1599 spa_close(spa, FTAG);
1604 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1607 int replacing = zc->zc_cookie;
1611 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1614 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1615 zc->zc_iflags, &config)) == 0) {
1616 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1617 nvlist_free(config);
1620 spa_close(spa, FTAG);
1625 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1630 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1633 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1635 spa_close(spa, FTAG);
1640 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1643 nvlist_t *config, *props = NULL;
1645 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1647 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1650 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1651 zc->zc_iflags, &config))) {
1652 spa_close(spa, FTAG);
1656 if (zc->zc_nvlist_src_size != 0 && (error =
1657 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1658 zc->zc_iflags, &props))) {
1659 spa_close(spa, FTAG);
1660 nvlist_free(config);
1664 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1666 spa_close(spa, FTAG);
1668 nvlist_free(config);
1675 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1678 char *path = zc->zc_value;
1679 uint64_t guid = zc->zc_guid;
1682 error = spa_open(zc->zc_name, &spa, FTAG);
1686 error = spa_vdev_setpath(spa, guid, path);
1687 spa_close(spa, FTAG);
1692 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1695 char *fru = zc->zc_value;
1696 uint64_t guid = zc->zc_guid;
1699 error = spa_open(zc->zc_name, &spa, FTAG);
1703 error = spa_vdev_setfru(spa, guid, fru);
1704 spa_close(spa, FTAG);
1709 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1714 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1716 if (zc->zc_nvlist_dst != 0 &&
1717 (error = dsl_prop_get_all(os, &nv)) == 0) {
1718 dmu_objset_stats(os, nv);
1720 * NB: zvol_get_stats() will read the objset contents,
1721 * which we aren't supposed to do with a
1722 * DS_MODE_USER hold, because it could be
1723 * inconsistent. So this is a bit of a workaround...
1724 * XXX reading with out owning
1726 if (!zc->zc_objset_stats.dds_inconsistent) {
1727 if (dmu_objset_type(os) == DMU_OST_ZVOL)
1728 error = zvol_get_stats(os, nv);
1731 error = put_nvlist(zc, nv);
1740 * zc_name name of filesystem
1741 * zc_nvlist_dst_size size of buffer for property nvlist
1744 * zc_objset_stats stats
1745 * zc_nvlist_dst property nvlist
1746 * zc_nvlist_dst_size size of property nvlist
1749 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1751 objset_t *os = NULL;
1754 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1757 error = zfs_ioc_objset_stats_impl(zc, os);
1759 dmu_objset_rele(os, FTAG);
1766 * zc_name name of filesystem
1767 * zc_nvlist_dst_size size of buffer for property nvlist
1770 * zc_nvlist_dst received property nvlist
1771 * zc_nvlist_dst_size size of received property nvlist
1773 * Gets received properties (distinct from local properties on or after
1774 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1775 * local property values.
1778 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1780 objset_t *os = NULL;
1784 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1788 * Without this check, we would return local property values if the
1789 * caller has not already received properties on or after
1790 * SPA_VERSION_RECVD_PROPS.
1792 if (!dsl_prop_get_hasrecvd(os)) {
1793 dmu_objset_rele(os, FTAG);
1797 if (zc->zc_nvlist_dst != 0 &&
1798 (error = dsl_prop_get_received(os, &nv)) == 0) {
1799 error = put_nvlist(zc, nv);
1803 dmu_objset_rele(os, FTAG);
1808 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1814 * zfs_get_zplprop() will either find a value or give us
1815 * the default value (if there is one).
1817 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1819 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1825 * zc_name name of filesystem
1826 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1829 * zc_nvlist_dst zpl property nvlist
1830 * zc_nvlist_dst_size size of zpl property nvlist
1833 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1838 /* XXX reading without owning */
1839 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1842 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1845 * NB: nvl_add_zplprop() will read the objset contents,
1846 * which we aren't supposed to do with a DS_MODE_USER
1847 * hold, because it could be inconsistent.
1849 if (zc->zc_nvlist_dst != 0 &&
1850 !zc->zc_objset_stats.dds_inconsistent &&
1851 dmu_objset_type(os) == DMU_OST_ZFS) {
1854 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1855 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1856 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1857 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1858 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1859 err = put_nvlist(zc, nv);
1864 dmu_objset_rele(os, FTAG);
1869 dataset_name_hidden(const char *name)
1872 * Skip over datasets that are not visible in this zone,
1873 * internal datasets (which have a $ in their name), and
1874 * temporary datasets (which have a % in their name).
1876 if (strchr(name, '$') != NULL)
1878 if (strchr(name, '%') != NULL)
1880 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1887 * zc_name name of filesystem
1888 * zc_cookie zap cursor
1889 * zc_nvlist_dst_size size of buffer for property nvlist
1892 * zc_name name of next filesystem
1893 * zc_cookie zap cursor
1894 * zc_objset_stats stats
1895 * zc_nvlist_dst property nvlist
1896 * zc_nvlist_dst_size size of property nvlist
1899 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1904 size_t orig_len = strlen(zc->zc_name);
1907 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1908 if (error == ENOENT)
1913 p = strrchr(zc->zc_name, '/');
1914 if (p == NULL || p[1] != '\0')
1915 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1916 p = zc->zc_name + strlen(zc->zc_name);
1919 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1920 * but is not declared void because its called by dmu_objset_find().
1922 if (zc->zc_cookie == 0) {
1923 uint64_t cookie = 0;
1924 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1926 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
1927 if (!dataset_name_hidden(zc->zc_name))
1928 (void) dmu_objset_prefetch(zc->zc_name, NULL);
1933 error = dmu_dir_list_next(os,
1934 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1935 NULL, &zc->zc_cookie);
1936 if (error == ENOENT)
1938 } while (error == 0 && dataset_name_hidden(zc->zc_name) &&
1939 !(zc->zc_iflags & FKIOCTL));
1940 dmu_objset_rele(os, FTAG);
1943 * If it's an internal dataset (ie. with a '$' in its name),
1944 * don't try to get stats for it, otherwise we'll return ENOENT.
1946 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1947 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1948 if (error == ENOENT) {
1949 /* We lost a race with destroy, get the next one. */
1950 zc->zc_name[orig_len] = '\0';
1959 * zc_name name of filesystem
1960 * zc_cookie zap cursor
1961 * zc_nvlist_dst_size size of buffer for property nvlist
1964 * zc_name name of next snapshot
1965 * zc_objset_stats stats
1966 * zc_nvlist_dst property nvlist
1967 * zc_nvlist_dst_size size of property nvlist
1970 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
1976 if (zc->zc_cookie == 0)
1977 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
1978 NULL, DS_FIND_SNAPSHOTS);
1980 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
1982 return (error == ENOENT ? ESRCH : error);
1985 * A dataset name of maximum length cannot have any snapshots,
1986 * so exit immediately.
1988 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
1989 dmu_objset_rele(os, FTAG);
1993 error = dmu_snapshot_list_next(os,
1994 sizeof (zc->zc_name) - strlen(zc->zc_name),
1995 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2000 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2003 * Since we probably don't have a hold on this snapshot,
2004 * it's possible that the objsetid could have been destroyed
2005 * and reused for a new objset. It's OK if this happens during
2006 * a zfs send operation, since the new createtxg will be
2007 * beyond the range we're interested in.
2009 rw_enter(&dp->dp_config_rwlock, RW_READER);
2010 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2011 rw_exit(&dp->dp_config_rwlock);
2013 if (error == ENOENT) {
2014 /* Racing with destroy, get the next one. */
2015 *strchr(zc->zc_name, '@') = '\0';
2016 dmu_objset_rele(os, FTAG);
2022 error = dmu_objset_from_ds(ds, &ossnap);
2024 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2025 dsl_dataset_rele(ds, FTAG);
2027 } else if (error == ENOENT) {
2031 dmu_objset_rele(os, FTAG);
2032 /* if we failed, undo the @ that we tacked on to zc_name */
2034 *strchr(zc->zc_name, '@') = '\0';
2039 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2041 const char *propname = nvpair_name(pair);
2043 unsigned int vallen;
2046 zfs_userquota_prop_t type;
2052 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2054 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2055 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2061 * A correctly constructed propname is encoded as
2062 * userquota@<rid>-<domain>.
2064 if ((dash = strchr(propname, '-')) == NULL ||
2065 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2074 err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
2076 err = zfs_set_userquota(zsb, type, domain, rid, quota);
2077 zfs_sb_rele(zsb, FTAG);
2084 * If the named property is one that has a special function to set its value,
2085 * return 0 on success and a positive error code on failure; otherwise if it is
2086 * not one of the special properties handled by this function, return -1.
2088 * XXX: It would be better for callers of the property interface if we handled
2089 * these special cases in dsl_prop.c (in the dsl layer).
2092 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2095 const char *propname = nvpair_name(pair);
2096 zfs_prop_t prop = zfs_name_to_prop(propname);
2100 if (prop == ZPROP_INVAL) {
2101 if (zfs_prop_userquota(propname))
2102 return (zfs_prop_set_userquota(dsname, pair));
2106 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2108 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2109 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2113 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2116 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2119 case ZFS_PROP_QUOTA:
2120 err = dsl_dir_set_quota(dsname, source, intval);
2122 case ZFS_PROP_REFQUOTA:
2123 err = dsl_dataset_set_quota(dsname, source, intval);
2125 case ZFS_PROP_RESERVATION:
2126 err = dsl_dir_set_reservation(dsname, source, intval);
2128 case ZFS_PROP_REFRESERVATION:
2129 err = dsl_dataset_set_reservation(dsname, source, intval);
2131 case ZFS_PROP_VOLSIZE:
2132 err = zvol_set_volsize(dsname, intval);
2134 case ZFS_PROP_VERSION:
2138 if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
2141 err = zfs_set_version(zsb, intval);
2142 zfs_sb_rele(zsb, FTAG);
2144 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2147 zc = kmem_zalloc(sizeof (zfs_cmd_t),
2148 KM_SLEEP | KM_NODEBUG);
2149 (void) strcpy(zc->zc_name, dsname);
2150 (void) zfs_ioc_userspace_upgrade(zc);
2151 kmem_free(zc, sizeof (zfs_cmd_t));
2164 * This function is best effort. If it fails to set any of the given properties,
2165 * it continues to set as many as it can and returns the first error
2166 * encountered. If the caller provides a non-NULL errlist, it also gives the
2167 * complete list of names of all the properties it failed to set along with the
2168 * corresponding error numbers. The caller is responsible for freeing the
2171 * If every property is set successfully, zero is returned and the list pointed
2172 * at by errlist is NULL.
2175 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2183 nvlist_t *genericnvl;
2187 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2188 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2189 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2193 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2194 const char *propname = nvpair_name(pair);
2195 zfs_prop_t prop = zfs_name_to_prop(propname);
2198 /* decode the property value */
2200 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2202 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2203 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2208 /* Validate value type */
2209 if (err == 0 && prop == ZPROP_INVAL) {
2210 if (zfs_prop_user(propname)) {
2211 if (nvpair_type(propval) != DATA_TYPE_STRING)
2213 } else if (zfs_prop_userquota(propname)) {
2214 if (nvpair_type(propval) !=
2215 DATA_TYPE_UINT64_ARRAY)
2218 } else if (err == 0) {
2219 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2220 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2222 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2225 VERIFY(nvpair_value_uint64(propval,
2228 switch (zfs_prop_get_type(prop)) {
2229 case PROP_TYPE_NUMBER:
2231 case PROP_TYPE_STRING:
2234 case PROP_TYPE_INDEX:
2235 if (zfs_prop_index_to_string(prop,
2236 intval, &unused) != 0)
2241 "unknown property type");
2248 /* Validate permissions */
2250 err = zfs_check_settable(dsname, pair, CRED());
2253 err = zfs_prop_set_special(dsname, source, pair);
2256 * For better performance we build up a list of
2257 * properties to set in a single transaction.
2259 err = nvlist_add_nvpair(genericnvl, pair);
2260 } else if (err != 0 && nvl != retrynvl) {
2262 * This may be a spurious error caused by
2263 * receiving quota and reservation out of order.
2264 * Try again in a second pass.
2266 err = nvlist_add_nvpair(retrynvl, pair);
2271 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2274 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2279 if (!nvlist_empty(genericnvl) &&
2280 dsl_props_set(dsname, source, genericnvl) != 0) {
2282 * If this fails, we still want to set as many properties as we
2283 * can, so try setting them individually.
2286 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2287 const char *propname = nvpair_name(pair);
2291 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2293 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2294 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2298 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2299 VERIFY(nvpair_value_string(propval,
2301 err = dsl_prop_set(dsname, propname, source, 1,
2302 strlen(strval) + 1, strval);
2304 VERIFY(nvpair_value_uint64(propval,
2306 err = dsl_prop_set(dsname, propname, source, 8,
2311 VERIFY(nvlist_add_int32(errors, propname,
2316 nvlist_free(genericnvl);
2317 nvlist_free(retrynvl);
2319 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2320 nvlist_free(errors);
2323 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2326 if (errlist == NULL)
2327 nvlist_free(errors);
2335 * Check that all the properties are valid user properties.
2338 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2340 nvpair_t *pair = NULL;
2343 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2344 const char *propname = nvpair_name(pair);
2347 if (!zfs_prop_user(propname) ||
2348 nvpair_type(pair) != DATA_TYPE_STRING)
2351 if ((error = zfs_secpolicy_write_perms(fsname,
2352 ZFS_DELEG_PERM_USERPROP, CRED())))
2355 if (strlen(propname) >= ZAP_MAXNAMELEN)
2356 return (ENAMETOOLONG);
2358 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2359 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2366 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2370 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2373 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2374 if (nvlist_exists(skipped, nvpair_name(pair)))
2377 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2382 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2386 nvlist_t *cleared_props = NULL;
2387 props_skip(props, skipped, &cleared_props);
2388 if (!nvlist_empty(cleared_props)) {
2390 * Acts on local properties until the dataset has received
2391 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2393 zprop_source_t flags = (ZPROP_SRC_NONE |
2394 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2395 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2397 nvlist_free(cleared_props);
2403 * zc_name name of filesystem
2404 * zc_value name of property to set
2405 * zc_nvlist_src{_size} nvlist of properties to apply
2406 * zc_cookie received properties flag
2409 * zc_nvlist_dst{_size} error for each unapplied received property
2412 zfs_ioc_set_prop(zfs_cmd_t *zc)
2415 boolean_t received = zc->zc_cookie;
2416 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2418 nvlist_t *errors = NULL;
2421 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2422 zc->zc_iflags, &nvl)) != 0)
2426 nvlist_t *origprops;
2429 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2430 if (dsl_prop_get_received(os, &origprops) == 0) {
2431 (void) clear_received_props(os,
2432 zc->zc_name, origprops, nvl);
2433 nvlist_free(origprops);
2436 dsl_prop_set_hasrecvd(os);
2437 dmu_objset_rele(os, FTAG);
2441 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2443 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2444 (void) put_nvlist(zc, errors);
2447 nvlist_free(errors);
2454 * zc_name name of filesystem
2455 * zc_value name of property to inherit
2456 * zc_cookie revert to received value if TRUE
2461 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2463 const char *propname = zc->zc_value;
2464 zfs_prop_t prop = zfs_name_to_prop(propname);
2465 boolean_t received = zc->zc_cookie;
2466 zprop_source_t source = (received
2467 ? ZPROP_SRC_NONE /* revert to received value, if any */
2468 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2477 * zfs_prop_set_special() expects properties in the form of an
2478 * nvpair with type info.
2480 if (prop == ZPROP_INVAL) {
2481 if (!zfs_prop_user(propname))
2484 type = PROP_TYPE_STRING;
2485 } else if (prop == ZFS_PROP_VOLSIZE ||
2486 prop == ZFS_PROP_VERSION) {
2489 type = zfs_prop_get_type(prop);
2492 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2495 case PROP_TYPE_STRING:
2496 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2498 case PROP_TYPE_NUMBER:
2499 case PROP_TYPE_INDEX:
2500 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2507 pair = nvlist_next_nvpair(dummy, NULL);
2508 err = zfs_prop_set_special(zc->zc_name, source, pair);
2511 return (err); /* special property already handled */
2514 * Only check this in the non-received case. We want to allow
2515 * 'inherit -S' to revert non-inheritable properties like quota
2516 * and reservation to the received or default values even though
2517 * they are not considered inheritable.
2519 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2523 /* the property name has been validated by zfs_secpolicy_inherit() */
2524 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2528 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2535 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2536 zc->zc_iflags, &props)))
2540 * If the only property is the configfile, then just do a spa_lookup()
2541 * to handle the faulted case.
2543 pair = nvlist_next_nvpair(props, NULL);
2544 if (pair != NULL && strcmp(nvpair_name(pair),
2545 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2546 nvlist_next_nvpair(props, pair) == NULL) {
2547 mutex_enter(&spa_namespace_lock);
2548 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2549 spa_configfile_set(spa, props, B_FALSE);
2550 spa_config_sync(spa, B_FALSE, B_TRUE);
2552 mutex_exit(&spa_namespace_lock);
2559 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2564 error = spa_prop_set(spa, props);
2567 spa_close(spa, FTAG);
2573 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2577 nvlist_t *nvp = NULL;
2579 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2581 * If the pool is faulted, there may be properties we can still
2582 * get (such as altroot and cachefile), so attempt to get them
2585 mutex_enter(&spa_namespace_lock);
2586 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2587 error = spa_prop_get(spa, &nvp);
2588 mutex_exit(&spa_namespace_lock);
2590 error = spa_prop_get(spa, &nvp);
2591 spa_close(spa, FTAG);
2594 if (error == 0 && zc->zc_nvlist_dst != 0)
2595 error = put_nvlist(zc, nvp);
2605 * zc_name name of volume
2610 zfs_ioc_create_minor(zfs_cmd_t *zc)
2612 return (zvol_create_minor(zc->zc_name));
2617 * zc_name name of volume
2622 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2624 return (zvol_remove_minor(zc->zc_name));
2629 * zc_name name of filesystem
2630 * zc_nvlist_src{_size} nvlist of delegated permissions
2631 * zc_perm_action allow/unallow flag
2636 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2639 nvlist_t *fsaclnv = NULL;
2641 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2642 zc->zc_iflags, &fsaclnv)) != 0)
2646 * Verify nvlist is constructed correctly
2648 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2649 nvlist_free(fsaclnv);
2654 * If we don't have PRIV_SYS_MOUNT, then validate
2655 * that user is allowed to hand out each permission in
2659 error = secpolicy_zfs(CRED());
2661 if (zc->zc_perm_action == B_FALSE) {
2662 error = dsl_deleg_can_allow(zc->zc_name,
2665 error = dsl_deleg_can_unallow(zc->zc_name,
2671 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2673 nvlist_free(fsaclnv);
2679 * zc_name name of filesystem
2682 * zc_nvlist_src{_size} nvlist of delegated permissions
2685 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2690 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2691 error = put_nvlist(zc, nvp);
2700 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2702 zfs_creat_t *zct = arg;
2704 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2707 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2711 * createprops list of properties requested by creator
2712 * default_zplver zpl version to use if unspecified in createprops
2713 * fuids_ok fuids allowed in this version of the spa?
2714 * os parent objset pointer (NULL if root fs)
2717 * zplprops values for the zplprops we attach to the master node object
2718 * is_ci true if requested file system will be purely case-insensitive
2720 * Determine the settings for utf8only, normalization and
2721 * casesensitivity. Specific values may have been requested by the
2722 * creator and/or we can inherit values from the parent dataset. If
2723 * the file system is of too early a vintage, a creator can not
2724 * request settings for these properties, even if the requested
2725 * setting is the default value. We don't actually want to create dsl
2726 * properties for these, so remove them from the source nvlist after
2730 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2731 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2732 nvlist_t *zplprops, boolean_t *is_ci)
2734 uint64_t sense = ZFS_PROP_UNDEFINED;
2735 uint64_t norm = ZFS_PROP_UNDEFINED;
2736 uint64_t u8 = ZFS_PROP_UNDEFINED;
2739 ASSERT(zplprops != NULL);
2742 * Pull out creator prop choices, if any.
2745 (void) nvlist_lookup_uint64(createprops,
2746 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2747 (void) nvlist_lookup_uint64(createprops,
2748 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2749 (void) nvlist_remove_all(createprops,
2750 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2751 (void) nvlist_lookup_uint64(createprops,
2752 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2753 (void) nvlist_remove_all(createprops,
2754 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2755 (void) nvlist_lookup_uint64(createprops,
2756 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2757 (void) nvlist_remove_all(createprops,
2758 zfs_prop_to_name(ZFS_PROP_CASE));
2762 * If the zpl version requested is whacky or the file system
2763 * or pool is version is too "young" to support normalization
2764 * and the creator tried to set a value for one of the props,
2767 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2768 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2769 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2770 (zplver < ZPL_VERSION_NORMALIZATION &&
2771 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2772 sense != ZFS_PROP_UNDEFINED)))
2776 * Put the version in the zplprops
2778 VERIFY(nvlist_add_uint64(zplprops,
2779 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2781 if (norm == ZFS_PROP_UNDEFINED &&
2782 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
2784 VERIFY(nvlist_add_uint64(zplprops,
2785 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2788 * If we're normalizing, names must always be valid UTF-8 strings.
2792 if (u8 == ZFS_PROP_UNDEFINED &&
2793 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
2795 VERIFY(nvlist_add_uint64(zplprops,
2796 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2798 if (sense == ZFS_PROP_UNDEFINED &&
2799 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
2801 VERIFY(nvlist_add_uint64(zplprops,
2802 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2805 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2811 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2812 nvlist_t *zplprops, boolean_t *is_ci)
2814 boolean_t fuids_ok, sa_ok;
2815 uint64_t zplver = ZPL_VERSION;
2816 objset_t *os = NULL;
2817 char parentname[MAXNAMELEN];
2823 (void) strlcpy(parentname, dataset, sizeof (parentname));
2824 cp = strrchr(parentname, '/');
2828 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2831 spa_vers = spa_version(spa);
2832 spa_close(spa, FTAG);
2834 zplver = zfs_zpl_version_map(spa_vers);
2835 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2836 sa_ok = (zplver >= ZPL_VERSION_SA);
2839 * Open parent object set so we can inherit zplprop values.
2841 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2844 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2846 dmu_objset_rele(os, FTAG);
2851 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2852 nvlist_t *zplprops, boolean_t *is_ci)
2856 uint64_t zplver = ZPL_VERSION;
2859 zplver = zfs_zpl_version_map(spa_vers);
2860 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2861 sa_ok = (zplver >= ZPL_VERSION_SA);
2863 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2864 createprops, zplprops, is_ci);
2870 * zc_objset_type type of objset to create (fs vs zvol)
2871 * zc_name name of new objset
2872 * zc_value name of snapshot to clone from (may be empty)
2873 * zc_nvlist_src{_size} nvlist of properties to apply
2878 zfs_ioc_create(zfs_cmd_t *zc)
2883 nvlist_t *nvprops = NULL;
2884 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2885 dmu_objset_type_t type = zc->zc_objset_type;
2890 cbfunc = zfs_create_cb;
2894 cbfunc = zvol_create_cb;
2901 if (strchr(zc->zc_name, '@') ||
2902 strchr(zc->zc_name, '%'))
2905 if (zc->zc_nvlist_src != 0 &&
2906 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2907 zc->zc_iflags, &nvprops)) != 0)
2910 zct.zct_zplprops = NULL;
2911 zct.zct_props = nvprops;
2913 if (zc->zc_value[0] != '\0') {
2915 * We're creating a clone of an existing snapshot.
2917 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2918 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2919 nvlist_free(nvprops);
2923 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2925 nvlist_free(nvprops);
2929 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2930 dmu_objset_rele(clone, FTAG);
2932 nvlist_free(nvprops);
2936 boolean_t is_insensitive = B_FALSE;
2938 if (cbfunc == NULL) {
2939 nvlist_free(nvprops);
2943 if (type == DMU_OST_ZVOL) {
2944 uint64_t volsize, volblocksize;
2946 if (nvprops == NULL ||
2947 nvlist_lookup_uint64(nvprops,
2948 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2950 nvlist_free(nvprops);
2954 if ((error = nvlist_lookup_uint64(nvprops,
2955 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2956 &volblocksize)) != 0 && error != ENOENT) {
2957 nvlist_free(nvprops);
2962 volblocksize = zfs_prop_default_numeric(
2963 ZFS_PROP_VOLBLOCKSIZE);
2965 if ((error = zvol_check_volblocksize(
2966 volblocksize)) != 0 ||
2967 (error = zvol_check_volsize(volsize,
2968 volblocksize)) != 0) {
2969 nvlist_free(nvprops);
2972 } else if (type == DMU_OST_ZFS) {
2976 * We have to have normalization and
2977 * case-folding flags correct when we do the
2978 * file system creation, so go figure them out
2981 VERIFY(nvlist_alloc(&zct.zct_zplprops,
2982 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2983 error = zfs_fill_zplprops(zc->zc_name, nvprops,
2984 zct.zct_zplprops, &is_insensitive);
2986 nvlist_free(nvprops);
2987 nvlist_free(zct.zct_zplprops);
2991 error = dmu_objset_create(zc->zc_name, type,
2992 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
2993 nvlist_free(zct.zct_zplprops);
2997 * It would be nice to do this atomically.
3000 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3003 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3005 nvlist_free(nvprops);
3011 * zc_name name of filesystem
3012 * zc_value short name of snapshot
3013 * zc_cookie recursive flag
3014 * zc_nvlist_src[_size] property list
3017 * zc_value short snapname (i.e. part after the '@')
3020 zfs_ioc_snapshot(zfs_cmd_t *zc)
3022 nvlist_t *nvprops = NULL;
3024 boolean_t recursive = zc->zc_cookie;
3026 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3029 if (zc->zc_nvlist_src != 0 &&
3030 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3031 zc->zc_iflags, &nvprops)) != 0)
3034 error = zfs_check_userprops(zc->zc_name, nvprops);
3038 if (!nvlist_empty(nvprops) &&
3039 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3044 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3045 nvprops, recursive, B_FALSE, -1);
3048 nvlist_free(nvprops);
3054 * name dataset name, or when 'arg == NULL' the full snapshot name
3055 * arg short snapshot name (i.e. part after the '@')
3058 zfs_unmount_snap(const char *name, void *arg)
3060 zfs_sb_t *zsb = NULL;
3068 dsname = strdup(name);
3069 snapname = strdup(arg);
3071 ptr = strchr(name, '@');
3073 dsname = strdup(name);
3074 dsname[ptr - name] = '\0';
3075 snapname = strdup(ptr + 1);
3081 fullname = kmem_asprintf("%s@%s", dsname, snapname);
3083 error = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
3085 error = zfsctl_unmount_snapshot(zsb, fullname, MNT_FORCE);
3086 zfs_sb_rele(zsb, FTAG);
3088 /* Allow ENOENT for consistency with upstream */
3089 if (error == ENOENT)
3102 * zc_name name of filesystem
3103 * zc_value short name of snapshot
3104 * zc_defer_destroy mark for deferred destroy
3109 zfs_ioc_destroy_snaps(zfs_cmd_t *zc)
3113 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3115 err = dmu_objset_find(zc->zc_name,
3116 zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN);
3119 return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value,
3120 zc->zc_defer_destroy));
3125 * zc_name name of dataset to destroy
3126 * zc_objset_type type of objset
3127 * zc_defer_destroy mark for deferred destroy
3132 zfs_ioc_destroy(zfs_cmd_t *zc)
3135 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3136 err = zfs_unmount_snap(zc->zc_name, NULL);
3141 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3142 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3143 (void) zvol_remove_minor(zc->zc_name);
3149 * zc_name name of dataset to rollback (to most recent snapshot)
3154 zfs_ioc_rollback(zfs_cmd_t *zc)
3156 dsl_dataset_t *ds, *clone;
3161 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3165 /* must not be a snapshot */
3166 if (dsl_dataset_is_snapshot(ds)) {
3167 dsl_dataset_rele(ds, FTAG);
3171 /* must have a most recent snapshot */
3172 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3173 dsl_dataset_rele(ds, FTAG);
3178 * Create clone of most recent snapshot.
3180 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3181 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3185 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3192 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
3193 error = zfs_suspend_fs(zsb);
3197 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3198 error = dsl_dataset_clone_swap(clone, ds,
3200 dsl_dataset_disown(ds, FTAG);
3205 resume_err = zfs_resume_fs(zsb, zc->zc_name);
3206 error = error ? error : resume_err;
3208 deactivate_super(zsb->z_sb);
3210 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3211 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3212 dsl_dataset_disown(ds, FTAG);
3220 * Destroy clone (which also closes it).
3222 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3225 strfree(clone_name);
3227 dsl_dataset_rele(ds, FTAG);
3233 * zc_name old name of dataset
3234 * zc_value new name of dataset
3235 * zc_cookie recursive flag (only valid for snapshots)
3240 zfs_ioc_rename(zfs_cmd_t *zc)
3242 boolean_t recursive = zc->zc_cookie & 1;
3245 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3246 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3247 strchr(zc->zc_value, '%'))
3251 * Unmount snapshot unless we're doing a recursive rename,
3252 * in which case the dataset code figures out which snapshots
3255 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3256 zc->zc_objset_type == DMU_OST_ZFS) {
3257 err = zfs_unmount_snap(zc->zc_name, NULL);
3262 err = dmu_objset_rename(zc->zc_name, zc->zc_value, recursive);
3263 if ((err == 0) && (zc->zc_objset_type == DMU_OST_ZVOL)) {
3264 (void) zvol_remove_minor(zc->zc_name);
3265 (void) zvol_create_minor(zc->zc_value);
3272 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3274 const char *propname = nvpair_name(pair);
3275 boolean_t issnap = (strchr(dsname, '@') != NULL);
3276 zfs_prop_t prop = zfs_name_to_prop(propname);
3280 if (prop == ZPROP_INVAL) {
3281 if (zfs_prop_user(propname)) {
3282 if ((err = zfs_secpolicy_write_perms(dsname,
3283 ZFS_DELEG_PERM_USERPROP, cr)))
3288 if (!issnap && zfs_prop_userquota(propname)) {
3289 const char *perm = NULL;
3290 const char *uq_prefix =
3291 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3292 const char *gq_prefix =
3293 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3295 if (strncmp(propname, uq_prefix,
3296 strlen(uq_prefix)) == 0) {
3297 perm = ZFS_DELEG_PERM_USERQUOTA;
3298 } else if (strncmp(propname, gq_prefix,
3299 strlen(gq_prefix)) == 0) {
3300 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3302 /* USERUSED and GROUPUSED are read-only */
3306 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3317 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3319 * dsl_prop_get_all_impl() returns properties in this
3323 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3324 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3329 * Check that this value is valid for this pool version
3332 case ZFS_PROP_COMPRESSION:
3334 * If the user specified gzip compression, make sure
3335 * the SPA supports it. We ignore any errors here since
3336 * we'll catch them later.
3338 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3339 nvpair_value_uint64(pair, &intval) == 0) {
3340 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3341 intval <= ZIO_COMPRESS_GZIP_9 &&
3342 zfs_earlier_version(dsname,
3343 SPA_VERSION_GZIP_COMPRESSION)) {
3347 if (intval == ZIO_COMPRESS_ZLE &&
3348 zfs_earlier_version(dsname,
3349 SPA_VERSION_ZLE_COMPRESSION))
3353 * If this is a bootable dataset then
3354 * verify that the compression algorithm
3355 * is supported for booting. We must return
3356 * something other than ENOTSUP since it
3357 * implies a downrev pool version.
3359 if (zfs_is_bootfs(dsname) &&
3360 !BOOTFS_COMPRESS_VALID(intval)) {
3366 case ZFS_PROP_COPIES:
3367 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3371 case ZFS_PROP_DEDUP:
3372 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3376 case ZFS_PROP_SHARESMB:
3377 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3381 case ZFS_PROP_ACLINHERIT:
3382 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3383 nvpair_value_uint64(pair, &intval) == 0) {
3384 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3385 zfs_earlier_version(dsname,
3386 SPA_VERSION_PASSTHROUGH_X))
3394 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3398 * Removes properties from the given props list that fail permission checks
3399 * needed to clear them and to restore them in case of a receive error. For each
3400 * property, make sure we have both set and inherit permissions.
3402 * Returns the first error encountered if any permission checks fail. If the
3403 * caller provides a non-NULL errlist, it also gives the complete list of names
3404 * of all the properties that failed a permission check along with the
3405 * corresponding error numbers. The caller is responsible for freeing the
3408 * If every property checks out successfully, zero is returned and the list
3409 * pointed at by errlist is NULL.
3412 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3415 nvpair_t *pair, *next_pair;
3422 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3424 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
3425 (void) strcpy(zc->zc_name, dataset);
3426 pair = nvlist_next_nvpair(props, NULL);
3427 while (pair != NULL) {
3428 next_pair = nvlist_next_nvpair(props, pair);
3430 (void) strcpy(zc->zc_value, nvpair_name(pair));
3431 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3432 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3433 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3434 VERIFY(nvlist_add_int32(errors,
3435 zc->zc_value, err) == 0);
3439 kmem_free(zc, sizeof (zfs_cmd_t));
3441 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3442 nvlist_free(errors);
3445 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3448 if (errlist == NULL)
3449 nvlist_free(errors);
3457 propval_equals(nvpair_t *p1, nvpair_t *p2)
3459 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3460 /* dsl_prop_get_all_impl() format */
3462 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3463 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3467 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3469 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3470 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3474 if (nvpair_type(p1) != nvpair_type(p2))
3477 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3478 char *valstr1, *valstr2;
3480 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3481 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3482 return (strcmp(valstr1, valstr2) == 0);
3484 uint64_t intval1, intval2;
3486 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3487 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3488 return (intval1 == intval2);
3493 * Remove properties from props if they are not going to change (as determined
3494 * by comparison with origprops). Remove them from origprops as well, since we
3495 * do not need to clear or restore properties that won't change.
3498 props_reduce(nvlist_t *props, nvlist_t *origprops)
3500 nvpair_t *pair, *next_pair;
3502 if (origprops == NULL)
3503 return; /* all props need to be received */
3505 pair = nvlist_next_nvpair(props, NULL);
3506 while (pair != NULL) {
3507 const char *propname = nvpair_name(pair);
3510 next_pair = nvlist_next_nvpair(props, pair);
3512 if ((nvlist_lookup_nvpair(origprops, propname,
3513 &match) != 0) || !propval_equals(pair, match))
3514 goto next; /* need to set received value */
3516 /* don't clear the existing received value */
3517 (void) nvlist_remove_nvpair(origprops, match);
3518 /* don't bother receiving the property */
3519 (void) nvlist_remove_nvpair(props, pair);
3526 static boolean_t zfs_ioc_recv_inject_err;
3531 * zc_name name of containing filesystem
3532 * zc_nvlist_src{_size} nvlist of properties to apply
3533 * zc_value name of snapshot to create
3534 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3535 * zc_cookie file descriptor to recv from
3536 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3537 * zc_guid force flag
3538 * zc_cleanup_fd cleanup-on-exit file descriptor
3539 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3542 * zc_cookie number of bytes read
3543 * zc_nvlist_dst{_size} error for each unapplied received property
3544 * zc_obj zprop_errflags_t
3545 * zc_action_handle handle for this guid/ds mapping
3548 zfs_ioc_recv(zfs_cmd_t *zc)
3552 dmu_recv_cookie_t drc;
3553 boolean_t force = (boolean_t)zc->zc_guid;
3556 int props_error = 0;
3559 nvlist_t *props = NULL; /* sent properties */
3560 nvlist_t *origprops = NULL; /* existing properties */
3561 objset_t *origin = NULL;
3563 char tofs[ZFS_MAXNAMELEN];
3564 boolean_t first_recvd_props = B_FALSE;
3566 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3567 strchr(zc->zc_value, '@') == NULL ||
3568 strchr(zc->zc_value, '%'))
3571 (void) strcpy(tofs, zc->zc_value);
3572 tosnap = strchr(tofs, '@');
3575 if (zc->zc_nvlist_src != 0 &&
3576 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3577 zc->zc_iflags, &props)) != 0)
3587 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3589 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3590 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3591 !dsl_prop_get_hasrecvd(os)) {
3592 first_recvd_props = B_TRUE;
3596 * If new received properties are supplied, they are to
3597 * completely replace the existing received properties, so stash
3598 * away the existing ones.
3600 if (dsl_prop_get_received(os, &origprops) == 0) {
3601 nvlist_t *errlist = NULL;
3603 * Don't bother writing a property if its value won't
3604 * change (and avoid the unnecessary security checks).
3606 * The first receive after SPA_VERSION_RECVD_PROPS is a
3607 * special case where we blow away all local properties
3610 if (!first_recvd_props)
3611 props_reduce(props, origprops);
3612 if (zfs_check_clearable(tofs, origprops,
3614 (void) nvlist_merge(errors, errlist, 0);
3615 nvlist_free(errlist);
3618 dmu_objset_rele(os, FTAG);
3621 if (zc->zc_string[0]) {
3622 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3627 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3628 &zc->zc_begin_record, force, origin, &drc);
3630 dmu_objset_rele(origin, FTAG);
3635 * Set properties before we receive the stream so that they are applied
3636 * to the new data. Note that we must call dmu_recv_stream() if
3637 * dmu_recv_begin() succeeds.
3642 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3643 if (drc.drc_newfs) {
3644 if (spa_version(os->os_spa) >=
3645 SPA_VERSION_RECVD_PROPS)
3646 first_recvd_props = B_TRUE;
3647 } else if (origprops != NULL) {
3648 if (clear_received_props(os, tofs, origprops,
3649 first_recvd_props ? NULL : props) != 0)
3650 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3652 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3654 dsl_prop_set_hasrecvd(os);
3655 } else if (!drc.drc_newfs) {
3656 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3659 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3661 (void) nvlist_merge(errors, errlist, 0);
3662 nvlist_free(errlist);
3665 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3667 * Caller made zc->zc_nvlist_dst less than the minimum expected
3668 * size or supplied an invalid address.
3670 props_error = EINVAL;
3674 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3675 &zc->zc_action_handle);
3678 zfs_sb_t *zsb = NULL;
3680 if (get_zfs_sb(tofs, &zsb) == 0) {
3684 error = zfs_suspend_fs(zsb);
3686 * If the suspend fails, then the recv_end will
3687 * likely also fail, and clean up after itself.
3689 end_err = dmu_recv_end(&drc);
3691 error = zfs_resume_fs(zsb, tofs);
3692 error = error ? error : end_err;
3693 deactivate_super(zsb->z_sb);
3695 error = dmu_recv_end(&drc);
3699 zc->zc_cookie = off - fp->f_offset;
3700 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3704 if (zfs_ioc_recv_inject_err) {
3705 zfs_ioc_recv_inject_err = B_FALSE;
3710 * On error, restore the original props.
3712 if (error && props) {
3713 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3714 if (clear_received_props(os, tofs, props, NULL) != 0) {
3716 * We failed to clear the received properties.
3717 * Since we may have left a $recvd value on the
3718 * system, we can't clear the $hasrecvd flag.
3720 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3721 } else if (first_recvd_props) {
3722 dsl_prop_unset_hasrecvd(os);
3724 dmu_objset_rele(os, FTAG);
3725 } else if (!drc.drc_newfs) {
3726 /* We failed to clear the received properties. */
3727 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3730 if (origprops == NULL && !drc.drc_newfs) {
3731 /* We failed to stash the original properties. */
3732 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3736 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3737 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3738 * explictly if we're restoring local properties cleared in the
3739 * first new-style receive.
3741 if (origprops != NULL &&
3742 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3743 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3744 origprops, NULL) != 0) {
3746 * We stashed the original properties but failed to
3749 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3754 nvlist_free(origprops);
3755 nvlist_free(errors);
3759 error = props_error;
3766 * zc_name name of snapshot to send
3767 * zc_cookie file descriptor to send stream to
3768 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3769 * zc_sendobj objsetid of snapshot to send
3770 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3775 zfs_ioc_send(zfs_cmd_t *zc)
3777 objset_t *fromsnap = NULL;
3783 dsl_dataset_t *dsfrom = NULL;
3787 error = spa_open(zc->zc_name, &spa, FTAG);
3791 dp = spa_get_dsl(spa);
3792 rw_enter(&dp->dp_config_rwlock, RW_READER);
3793 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3794 rw_exit(&dp->dp_config_rwlock);
3796 spa_close(spa, FTAG);
3800 error = dmu_objset_from_ds(ds, &tosnap);
3802 dsl_dataset_rele(ds, FTAG);
3803 spa_close(spa, FTAG);
3807 if (zc->zc_fromobj != 0) {
3808 rw_enter(&dp->dp_config_rwlock, RW_READER);
3809 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3810 rw_exit(&dp->dp_config_rwlock);
3811 spa_close(spa, FTAG);
3813 dsl_dataset_rele(ds, FTAG);
3816 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3818 dsl_dataset_rele(dsfrom, FTAG);
3819 dsl_dataset_rele(ds, FTAG);
3823 spa_close(spa, FTAG);
3826 fp = getf(zc->zc_cookie);
3828 dsl_dataset_rele(ds, FTAG);
3830 dsl_dataset_rele(dsfrom, FTAG);
3835 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off);
3837 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3839 releasef(zc->zc_cookie);
3841 dsl_dataset_rele(dsfrom, FTAG);
3842 dsl_dataset_rele(ds, FTAG);
3847 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3851 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3852 &zc->zc_inject_record);
3855 zc->zc_guid = (uint64_t)id;
3861 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3863 return (zio_clear_fault((int)zc->zc_guid));
3867 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3869 int id = (int)zc->zc_guid;
3872 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3873 &zc->zc_inject_record);
3881 zfs_ioc_error_log(zfs_cmd_t *zc)
3885 size_t count = (size_t)zc->zc_nvlist_dst_size;
3887 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3890 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3893 zc->zc_nvlist_dst_size = count;
3895 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3897 spa_close(spa, FTAG);
3903 zfs_ioc_clear(zfs_cmd_t *zc)
3910 * On zpool clear we also fix up missing slogs
3912 mutex_enter(&spa_namespace_lock);
3913 spa = spa_lookup(zc->zc_name);
3915 mutex_exit(&spa_namespace_lock);
3918 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3919 /* we need to let spa_open/spa_load clear the chains */
3920 spa_set_log_state(spa, SPA_LOG_CLEAR);
3922 spa->spa_last_open_failed = 0;
3923 mutex_exit(&spa_namespace_lock);
3925 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3926 error = spa_open(zc->zc_name, &spa, FTAG);
3929 nvlist_t *config = NULL;
3931 if (zc->zc_nvlist_src == 0)
3934 if ((error = get_nvlist(zc->zc_nvlist_src,
3935 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3936 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3938 if (config != NULL) {
3941 if ((err = put_nvlist(zc, config)) != 0)
3943 nvlist_free(config);
3945 nvlist_free(policy);
3952 spa_vdev_state_enter(spa, SCL_NONE);
3954 if (zc->zc_guid == 0) {
3957 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
3959 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
3960 spa_close(spa, FTAG);
3965 vdev_clear(spa, vd);
3967 (void) spa_vdev_state_exit(spa, NULL, 0);
3970 * Resume any suspended I/Os.
3972 if (zio_resume(spa) != 0)
3975 spa_close(spa, FTAG);
3982 * zc_name name of filesystem
3983 * zc_value name of origin snapshot
3986 * zc_string name of conflicting snapshot, if there is one
3989 zfs_ioc_promote(zfs_cmd_t *zc)
3994 * We don't need to unmount *all* the origin fs's snapshots, but
3997 cp = strchr(zc->zc_value, '@');
4000 (void) dmu_objset_find(zc->zc_value,
4001 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4002 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4006 * Retrieve a single {user|group}{used|quota}@... property.
4009 * zc_name name of filesystem
4010 * zc_objset_type zfs_userquota_prop_t
4011 * zc_value domain name (eg. "S-1-234-567-89")
4012 * zc_guid RID/UID/GID
4015 * zc_cookie property value
4018 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4023 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4026 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4030 error = zfs_userspace_one(zsb,
4031 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4032 zfs_sb_rele(zsb, FTAG);
4039 * zc_name name of filesystem
4040 * zc_cookie zap cursor
4041 * zc_objset_type zfs_userquota_prop_t
4042 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4045 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4046 * zc_cookie zap cursor
4049 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4052 int bufsize = zc->zc_nvlist_dst_size;
4059 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4063 buf = vmem_alloc(bufsize, KM_SLEEP);
4065 error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
4066 buf, &zc->zc_nvlist_dst_size);
4069 error = xcopyout(buf,
4070 (void *)(uintptr_t)zc->zc_nvlist_dst,
4071 zc->zc_nvlist_dst_size);
4073 vmem_free(buf, bufsize);
4074 zfs_sb_rele(zsb, FTAG);
4081 * zc_name name of filesystem
4087 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4093 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4094 if (!dmu_objset_userused_enabled(zsb->z_os)) {
4096 * If userused is not enabled, it may be because the
4097 * objset needs to be closed & reopened (to grow the
4098 * objset_phys_t). Suspend/resume the fs will do that.
4100 error = zfs_suspend_fs(zsb);
4102 error = zfs_resume_fs(zsb, zc->zc_name);
4105 error = dmu_objset_userspace_upgrade(zsb->z_os);
4106 deactivate_super(zsb->z_sb);
4108 /* XXX kind of reading contents without owning */
4109 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4113 error = dmu_objset_userspace_upgrade(os);
4114 dmu_objset_rele(os, FTAG);
4121 zfs_ioc_share(zfs_cmd_t *zc)
4126 ace_t full_access[] = {
4127 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4132 * zc_name name of containing filesystem
4133 * zc_obj object # beyond which we want next in-use object #
4136 * zc_obj next in-use object #
4139 zfs_ioc_next_obj(zfs_cmd_t *zc)
4141 objset_t *os = NULL;
4144 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4148 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4149 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4151 dmu_objset_rele(os, FTAG);
4157 * zc_name name of filesystem
4158 * zc_value prefix name for snapshot
4159 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4164 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4169 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4170 (u_longlong_t)ddi_get_lbolt64());
4172 if (strlen(snap_name) >= MAXNAMELEN) {
4177 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4178 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4184 (void) strcpy(zc->zc_value, snap_name);
4191 * zc_name name of "to" snapshot
4192 * zc_value name of "from" snapshot
4193 * zc_cookie file descriptor to write diff data on
4196 * dmu_diff_record_t's to the file descriptor
4199 zfs_ioc_diff(zfs_cmd_t *zc)
4207 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4211 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4213 dmu_objset_rele(tosnap, FTAG);
4217 fp = getf(zc->zc_cookie);
4219 dmu_objset_rele(fromsnap, FTAG);
4220 dmu_objset_rele(tosnap, FTAG);
4226 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4228 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4230 releasef(zc->zc_cookie);
4232 dmu_objset_rele(fromsnap, FTAG);
4233 dmu_objset_rele(tosnap, FTAG);
4238 * Remove all ACL files in shares dir
4240 #ifdef HAVE_SMB_SHARE
4242 zfs_smb_acl_purge(znode_t *dzp)
4245 zap_attribute_t zap;
4246 zfs_sb_t *zsb = ZTOZSB(dzp);
4249 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
4250 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4251 zap_cursor_advance(&zc)) {
4252 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4256 zap_cursor_fini(&zc);
4259 #endif /* HAVE_SMB_SHARE */
4262 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4264 #ifdef HAVE_SMB_SHARE
4267 vnode_t *resourcevp = NULL;
4276 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4277 NO_FOLLOW, NULL, &vp)) != 0)
4280 /* Now make sure mntpnt and dataset are ZFS */
4282 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4283 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4284 zc->zc_name) != 0)) {
4294 * Create share dir if its missing.
4296 mutex_enter(&zsb->z_lock);
4297 if (zsb->z_shares_dir == 0) {
4300 tx = dmu_tx_create(zsb->z_os);
4301 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4303 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4304 error = dmu_tx_assign(tx, TXG_WAIT);
4308 error = zfs_create_share_dir(zsb, tx);
4312 mutex_exit(&zsb->z_lock);
4318 mutex_exit(&zsb->z_lock);
4320 ASSERT(zsb->z_shares_dir);
4321 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
4327 switch (zc->zc_cookie) {
4328 case ZFS_SMB_ACL_ADD:
4329 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4330 vattr.va_mode = S_IFREG|0777;
4334 vsec.vsa_mask = VSA_ACE;
4335 vsec.vsa_aclentp = &full_access;
4336 vsec.vsa_aclentsz = sizeof (full_access);
4337 vsec.vsa_aclcnt = 1;
4339 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4340 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4342 VN_RELE(resourcevp);
4345 case ZFS_SMB_ACL_REMOVE:
4346 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4350 case ZFS_SMB_ACL_RENAME:
4351 if ((error = get_nvlist(zc->zc_nvlist_src,
4352 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4357 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4358 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4361 VN_RELE(ZTOV(sharedir));
4363 nvlist_free(nvlist);
4366 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4368 nvlist_free(nvlist);
4371 case ZFS_SMB_ACL_PURGE:
4372 error = zfs_smb_acl_purge(sharedir);
4381 VN_RELE(ZTOV(sharedir));
4388 #endif /* HAVE_SMB_SHARE */
4393 * zc_name name of filesystem
4394 * zc_value short name of snap
4395 * zc_string user-supplied tag for this hold
4396 * zc_cookie recursive flag
4397 * zc_temphold set if hold is temporary
4398 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4399 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4400 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4405 zfs_ioc_hold(zfs_cmd_t *zc)
4407 boolean_t recursive = zc->zc_cookie;
4414 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4417 if (zc->zc_sendobj == 0) {
4418 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4419 zc->zc_string, recursive, zc->zc_temphold,
4420 zc->zc_cleanup_fd));
4426 error = spa_open(zc->zc_name, &spa, FTAG);
4430 dp = spa_get_dsl(spa);
4431 rw_enter(&dp->dp_config_rwlock, RW_READER);
4432 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4433 rw_exit(&dp->dp_config_rwlock);
4434 spa_close(spa, FTAG);
4439 * Until we have a hold on this snapshot, it's possible that
4440 * zc_sendobj could've been destroyed and reused as part
4441 * of a later txg. Make sure we're looking at the right object.
4443 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4444 dsl_dataset_rele(ds, FTAG);
4448 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4449 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4451 dsl_dataset_rele(ds, FTAG);
4456 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4460 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4463 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4465 dsl_dataset_rele(ds, FTAG);
4472 * zc_name name of dataset from which we're releasing a user hold
4473 * zc_value short name of snap
4474 * zc_string user-supplied tag for this hold
4475 * zc_cookie recursive flag
4480 zfs_ioc_release(zfs_cmd_t *zc)
4482 boolean_t recursive = zc->zc_cookie;
4484 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4487 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4488 zc->zc_string, recursive));
4493 * zc_name name of filesystem
4496 * zc_nvlist_src{_size} nvlist of snapshot holds
4499 zfs_ioc_get_holds(zfs_cmd_t *zc)
4504 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4505 error = put_nvlist(zc, nvp);
4514 * zc_guid flags (ZEVENT_NONBLOCK)
4517 * zc_nvlist_dst next nvlist event
4518 * zc_cookie dropped events since last get
4519 * zc_cleanup_fd cleanup-on-exit file descriptor
4522 zfs_ioc_events_next(zfs_cmd_t *zc)
4525 nvlist_t *event = NULL;
4527 uint64_t dropped = 0;
4530 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4535 error = zfs_zevent_next(ze, &event,
4536 &zc->zc_nvlist_dst_size, &dropped);
4537 if (event != NULL) {
4538 zc->zc_cookie = dropped;
4539 error = put_nvlist(zc, event);
4543 if (zc->zc_guid & ZEVENT_NONBLOCK)
4546 if ((error == 0) || (error != ENOENT))
4549 error = zfs_zevent_wait(ze);
4554 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4561 * zc_cookie cleared events count
4564 zfs_ioc_events_clear(zfs_cmd_t *zc)
4568 zfs_zevent_drain_all(&count);
4569 zc->zc_cookie = count;
4575 * pool create, destroy, and export don't log the history as part of
4576 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4577 * do the logging of those commands.
4579 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4580 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4582 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4584 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4586 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4588 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4590 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4592 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4594 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4595 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4596 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4597 POOL_CHECK_READONLY },
4598 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4599 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4600 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4602 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4603 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4604 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4605 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4606 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4607 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4608 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4609 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4610 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4611 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4612 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4613 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4614 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4615 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4616 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4617 POOL_CHECK_SUSPENDED },
4618 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4620 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4621 POOL_CHECK_SUSPENDED },
4622 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4623 POOL_CHECK_SUSPENDED },
4624 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4625 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4626 { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4628 { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4630 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4631 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4632 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4633 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4634 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4635 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4636 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4637 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4638 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4639 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4640 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE,
4642 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4644 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4646 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4648 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4650 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4652 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4653 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4654 { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME,
4655 B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4656 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4657 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4658 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4660 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4661 POOL_CHECK_SUSPENDED },
4662 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4663 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4664 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4666 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4667 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4668 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4670 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4672 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4673 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4674 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4676 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4677 B_FALSE, POOL_CHECK_NONE },
4678 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4679 B_FALSE, POOL_CHECK_NONE },
4680 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4681 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4682 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4683 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4684 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4685 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4686 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4687 POOL_CHECK_SUSPENDED },
4688 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4690 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4691 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4692 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4694 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4696 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4697 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4698 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4699 POOL_CHECK_SUSPENDED },
4700 { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4702 { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
4707 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4708 zfs_ioc_poolcheck_t check)
4713 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4715 if (check & POOL_CHECK_NONE)
4718 error = spa_open(name, &spa, FTAG);
4720 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4722 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4724 spa_close(spa, FTAG);
4730 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4734 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4736 for (zs = list_head(&zfsdev_state_list); zs != NULL;
4737 zs = list_next(&zfsdev_state_list, zs)) {
4738 if (zs->zs_minor == minor) {
4740 case ZST_ONEXIT: return (zs->zs_onexit);
4741 case ZST_ZEVENT: return (zs->zs_zevent);
4742 case ZST_ALL: return (zs);
4751 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4755 mutex_enter(&zfsdev_state_lock);
4756 ptr = zfsdev_get_state_impl(minor, which);
4757 mutex_exit(&zfsdev_state_lock);
4763 zfsdev_getminor(struct file *filp)
4765 ASSERT(filp != NULL);
4766 ASSERT(filp->private_data != NULL);
4768 return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4772 * Find a free minor number. The zfsdev_state_list is expected to
4773 * be short since it is only a list of currently open file handles.
4776 zfsdev_minor_alloc(void)
4778 static minor_t last_minor = 0;
4781 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4783 for (m = last_minor + 1; m != last_minor; m++) {
4784 if (m > ZFSDEV_MAX_MINOR)
4786 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
4796 zfsdev_state_init(struct file *filp)
4801 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4803 minor = zfsdev_minor_alloc();
4807 zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
4812 zs->zs_minor = minor;
4813 filp->private_data = zs;
4815 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
4816 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
4818 list_insert_tail(&zfsdev_state_list, zs);
4824 zfsdev_state_destroy(struct file *filp)
4828 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4829 ASSERT(filp->private_data != NULL);
4831 zs = filp->private_data;
4832 zfs_onexit_destroy(zs->zs_onexit);
4833 zfs_zevent_destroy(zs->zs_zevent);
4835 list_remove(&zfsdev_state_list, zs);
4836 kmem_free(zs, sizeof(zfsdev_state_t));
4842 zfsdev_open(struct inode *ino, struct file *filp)
4846 mutex_enter(&zfsdev_state_lock);
4847 error = zfsdev_state_init(filp);
4848 mutex_exit(&zfsdev_state_lock);
4854 zfsdev_release(struct inode *ino, struct file *filp)
4858 mutex_enter(&zfsdev_state_lock);
4859 error = zfsdev_state_destroy(filp);
4860 mutex_exit(&zfsdev_state_lock);
4866 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
4870 int error, rc, flag = 0;
4872 vec = cmd - ZFS_IOC;
4873 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
4876 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
4878 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
4882 if ((error == 0) && !(flag & FKIOCTL))
4883 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, CRED());
4886 * Ensure that all pool/dataset names are valid before we pass down to
4890 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4891 zc->zc_iflags = flag & FKIOCTL;
4892 switch (zfs_ioc_vec[vec].zvec_namecheck) {
4894 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
4896 error = pool_status_check(zc->zc_name,
4897 zfs_ioc_vec[vec].zvec_namecheck,
4898 zfs_ioc_vec[vec].zvec_pool_check);
4902 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
4904 error = pool_status_check(zc->zc_name,
4905 zfs_ioc_vec[vec].zvec_namecheck,
4906 zfs_ioc_vec[vec].zvec_pool_check);
4915 error = zfs_ioc_vec[vec].zvec_func(zc);
4917 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
4921 if (zfs_ioc_vec[vec].zvec_his_log)
4922 zfs_log_history(zc);
4925 kmem_free(zc, sizeof (zfs_cmd_t));
4929 #ifdef CONFIG_COMPAT
4931 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
4933 return zfsdev_ioctl(filp, cmd, arg);
4936 #define zfsdev_compat_ioctl NULL
4939 static const struct file_operations zfsdev_fops = {
4940 .open = zfsdev_open,
4941 .release = zfsdev_release,
4942 .unlocked_ioctl = zfsdev_ioctl,
4943 .compat_ioctl = zfsdev_compat_ioctl,
4944 .owner = THIS_MODULE,
4947 static struct miscdevice zfs_misc = {
4948 .minor = MISC_DYNAMIC_MINOR,
4950 .fops = &zfsdev_fops,
4958 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
4959 list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
4960 offsetof(zfsdev_state_t, zs_next));
4962 error = misc_register(&zfs_misc);
4964 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
4976 error = misc_deregister(&zfs_misc);
4978 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
4980 mutex_destroy(&zfsdev_state_lock);
4981 list_destroy(&zfsdev_state_list);
4984 uint_t zfs_fsyncer_key;
4985 extern uint_t rrw_tsd_key;
4988 #define ZFS_DEBUG_STR " (DEBUG mode)"
4990 #define ZFS_DEBUG_STR ""
4998 spa_init(FREAD | FWRITE);
5001 if ((error = zvol_init()) != 0)
5004 if ((error = zfs_attach()) != 0)
5007 tsd_create(&zfs_fsyncer_key, NULL);
5008 tsd_create(&rrw_tsd_key, NULL);
5010 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
5011 "ZFS pool version %s, ZFS filesystem version %s\n",
5012 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
5013 SPA_VERSION_STRING, ZPL_VERSION_STRING);
5022 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
5023 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
5024 ZFS_DEBUG_STR, error);
5037 tsd_destroy(&zfs_fsyncer_key);
5038 tsd_destroy(&rrw_tsd_key);
5040 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
5041 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
5047 spl_module_init(_init);
5048 spl_module_exit(_fini);
5050 MODULE_DESCRIPTION("ZFS");
5051 MODULE_AUTHOR(ZFS_META_AUTHOR);
5052 MODULE_LICENSE(ZFS_META_LICENSE);
5053 #endif /* HAVE_SPL */