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 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 by Delphix. All rights reserved.
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/errno.h>
35 #include <sys/modctl.h>
40 #include <sys/cmn_err.h>
42 #include <sys/zfs_ioctl.h>
43 #include <sys/zfs_vfsops.h>
44 #include <sys/zfs_znode.h>
47 #include <sys/spa_impl.h>
49 #include <sys/priv_impl.h>
51 #include <sys/dsl_dir.h>
52 #include <sys/dsl_dataset.h>
53 #include <sys/dsl_prop.h>
54 #include <sys/dsl_deleg.h>
55 #include <sys/dmu_objset.h>
57 #include <sys/sunddi.h>
58 #include <sys/sunldi.h>
59 #include <sys/policy.h>
61 #include <sys/nvpair.h>
62 #include <sys/pathname.h>
63 #include <sys/mount.h>
65 #include <sys/fs/zfs.h>
66 #include <sys/zfs_ctldir.h>
67 #include <sys/zfs_dir.h>
68 #include <sys/zfs_onexit.h>
70 #include <sys/dsl_scan.h>
71 #include <sharefs/share.h>
72 #include <sys/dmu_objset.h>
73 #include <sys/fm/util.h>
75 #include <linux/miscdevice.h>
77 #include "zfs_namecheck.h"
79 #include "zfs_deleg.h"
80 #include "zfs_comutil.h"
82 kmutex_t zfsdev_state_lock;
83 list_t zfsdev_state_list;
85 extern void zfs_init(void);
86 extern void zfs_fini(void);
88 typedef int zfs_ioc_func_t(zfs_cmd_t *);
89 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *);
95 } zfs_ioc_namecheck_t;
98 POOL_CHECK_NONE = 1 << 0,
99 POOL_CHECK_SUSPENDED = 1 << 1,
100 POOL_CHECK_READONLY = 1 << 2
101 } zfs_ioc_poolcheck_t;
103 typedef struct zfs_ioc_vec {
104 zfs_ioc_func_t *zvec_func;
105 zfs_secpolicy_func_t *zvec_secpolicy;
106 zfs_ioc_namecheck_t zvec_namecheck;
107 boolean_t zvec_his_log;
108 zfs_ioc_poolcheck_t zvec_pool_check;
111 /* This array is indexed by zfs_userquota_prop_t */
112 static const char *userquota_perms[] = {
113 ZFS_DELEG_PERM_USERUSED,
114 ZFS_DELEG_PERM_USERQUOTA,
115 ZFS_DELEG_PERM_GROUPUSED,
116 ZFS_DELEG_PERM_GROUPQUOTA,
119 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
120 static int zfs_check_settable(const char *name, nvpair_t *property,
122 static int zfs_check_clearable(char *dataset, nvlist_t *props,
124 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
126 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **);
129 history_str_free(char *buf)
131 kmem_free(buf, HIS_MAX_RECORD_LEN);
135 history_str_get(zfs_cmd_t *zc)
139 if (zc->zc_history == 0)
142 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP | KM_NODEBUG);
143 if (copyinstr((void *)(uintptr_t)zc->zc_history,
144 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
145 history_str_free(buf);
149 buf[HIS_MAX_RECORD_LEN -1] = '\0';
155 * Check to see if the named dataset is currently defined as bootable
158 zfs_is_bootfs(const char *name)
162 if (dmu_objset_hold(name, FTAG, &os) == 0) {
164 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
165 dmu_objset_rele(os, FTAG);
172 * zfs_earlier_version
174 * Return non-zero if the spa version is less than requested version.
177 zfs_earlier_version(const char *name, int version)
181 if (spa_open(name, &spa, FTAG) == 0) {
182 if (spa_version(spa) < version) {
183 spa_close(spa, FTAG);
186 spa_close(spa, FTAG);
192 * zpl_earlier_version
194 * Return TRUE if the ZPL version is less than requested version.
197 zpl_earlier_version(const char *name, int version)
200 boolean_t rc = B_TRUE;
202 if (dmu_objset_hold(name, FTAG, &os) == 0) {
205 if (dmu_objset_type(os) != DMU_OST_ZFS) {
206 dmu_objset_rele(os, FTAG);
209 /* XXX reading from non-owned objset */
210 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
211 rc = zplversion < version;
212 dmu_objset_rele(os, FTAG);
218 zfs_log_history(zfs_cmd_t *zc)
223 if ((buf = history_str_get(zc)) == NULL)
226 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
227 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
228 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL);
229 spa_close(spa, FTAG);
231 history_str_free(buf);
235 * Policy for top-level read operations (list pools). Requires no privileges,
236 * and can be used in the local zone, as there is no associated dataset.
240 zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr)
246 * Policy for dataset read operations (list children, get statistics). Requires
247 * no privileges, but must be visible in the local zone.
251 zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr)
253 if (INGLOBALZONE(curproc) ||
254 zone_dataset_visible(zc->zc_name, NULL))
261 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
266 * The dataset must be visible by this zone -- check this first
267 * so they don't see EPERM on something they shouldn't know about.
269 if (!INGLOBALZONE(curproc) &&
270 !zone_dataset_visible(dataset, &writable))
273 if (INGLOBALZONE(curproc)) {
275 * If the fs is zoned, only root can access it from the
278 if (secpolicy_zfs(cr) && zoned)
282 * If we are in a local zone, the 'zoned' property must be set.
287 /* must be writable by this zone */
295 zfs_dozonecheck(const char *dataset, cred_t *cr)
299 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
302 return (zfs_dozonecheck_impl(dataset, zoned, cr));
306 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
310 rw_enter(&ds->ds_dir->dd_pool->dp_config_rwlock, RW_READER);
311 if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL)) {
312 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
315 rw_exit(&ds->ds_dir->dd_pool->dp_config_rwlock);
317 return (zfs_dozonecheck_impl(dataset, zoned, cr));
321 * If name ends in a '@', then require recursive permissions.
324 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
327 boolean_t descendent = B_FALSE;
331 at = strchr(name, '@');
332 if (at != NULL && at[1] == '\0') {
337 error = dsl_dataset_hold(name, FTAG, &ds);
343 error = zfs_dozonecheck_ds(name, ds, cr);
345 error = secpolicy_zfs(cr);
347 error = dsl_deleg_access_impl(ds, descendent, perm, cr);
350 dsl_dataset_rele(ds, FTAG);
355 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
356 const char *perm, cred_t *cr)
360 error = zfs_dozonecheck_ds(name, ds, cr);
362 error = secpolicy_zfs(cr);
364 error = dsl_deleg_access_impl(ds, B_FALSE, perm, cr);
370 * Policy for setting the security label property.
372 * Returns 0 for success, non-zero for access and other errors.
375 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
378 char ds_hexsl[MAXNAMELEN];
379 bslabel_t ds_sl, new_sl;
380 boolean_t new_default = FALSE;
382 int needed_priv = -1;
385 /* First get the existing dataset label. */
386 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
387 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
391 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
394 /* The label must be translatable */
395 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
399 * In a non-global zone, disallow attempts to set a label that
400 * doesn't match that of the zone; otherwise no other checks
403 if (!INGLOBALZONE(curproc)) {
404 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
410 * For global-zone datasets (i.e., those whose zoned property is
411 * "off", verify that the specified new label is valid for the
414 if (dsl_prop_get_integer(name,
415 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
418 if (zfs_check_global_label(name, strval) != 0)
423 * If the existing dataset label is nondefault, check if the
424 * dataset is mounted (label cannot be changed while mounted).
425 * Get the zfs_sb_t; if there isn't one, then the dataset isn't
426 * mounted (or isn't a dataset, doesn't exist, ...).
428 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
430 static char *setsl_tag = "setsl_tag";
433 * Try to own the dataset; abort if there is any error,
434 * (e.g., already mounted, in use, or other error).
436 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
441 dmu_objset_disown(os, setsl_tag);
444 needed_priv = PRIV_FILE_DOWNGRADE_SL;
448 if (hexstr_to_label(strval, &new_sl) != 0)
451 if (blstrictdom(&ds_sl, &new_sl))
452 needed_priv = PRIV_FILE_DOWNGRADE_SL;
453 else if (blstrictdom(&new_sl, &ds_sl))
454 needed_priv = PRIV_FILE_UPGRADE_SL;
456 /* dataset currently has a default label */
458 needed_priv = PRIV_FILE_UPGRADE_SL;
462 if (needed_priv != -1)
463 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
467 #endif /* HAVE_MLSLABEL */
471 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
477 * Check permissions for special properties.
484 * Disallow setting of 'zoned' from within a local zone.
486 if (!INGLOBALZONE(curproc))
491 if (!INGLOBALZONE(curproc)) {
493 char setpoint[MAXNAMELEN];
495 * Unprivileged users are allowed to modify the
496 * quota on things *under* (ie. contained by)
497 * the thing they own.
499 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
502 if (!zoned || strlen(dsname) <= strlen(setpoint))
507 case ZFS_PROP_MLSLABEL:
508 if (!is_system_labeled())
511 if (nvpair_value_string(propval, &strval) == 0) {
514 err = zfs_set_slabel_policy(dsname, strval, CRED());
521 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
525 zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr)
529 error = zfs_dozonecheck(zc->zc_name, cr);
534 * permission to set permissions will be evaluated later in
535 * dsl_deleg_can_allow()
541 zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr)
543 return (zfs_secpolicy_write_perms(zc->zc_name,
544 ZFS_DELEG_PERM_ROLLBACK, cr));
548 zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr)
557 * Generate the current snapshot name from the given objsetid, then
558 * use that name for the secpolicy/zone checks.
560 cp = strchr(zc->zc_name, '@');
563 error = spa_open(zc->zc_name, &spa, FTAG);
567 dp = spa_get_dsl(spa);
568 rw_enter(&dp->dp_config_rwlock, RW_READER);
569 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
570 rw_exit(&dp->dp_config_rwlock);
571 spa_close(spa, FTAG);
575 dsl_dataset_name(ds, zc->zc_name);
577 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
578 ZFS_DELEG_PERM_SEND, cr);
579 dsl_dataset_rele(ds, FTAG);
584 #ifdef HAVE_SMB_SHARE
586 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr)
591 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
592 NO_FOLLOW, NULL, &vp)) != 0)
595 /* Now make sure mntpnt and dataset are ZFS */
597 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
598 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
599 zc->zc_name) != 0)) {
605 return (dsl_deleg_access(zc->zc_name,
606 ZFS_DELEG_PERM_SHARE, cr));
608 #endif /* HAVE_SMB_SHARE */
611 zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr)
613 #ifdef HAVE_SMB_SHARE
614 if (!INGLOBALZONE(curproc))
617 if (secpolicy_nfs(cr) == 0) {
620 return (zfs_secpolicy_deleg_share(zc, cr));
624 #endif /* HAVE_SMB_SHARE */
628 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr)
630 #ifdef HAVE_SMB_SHARE
631 if (!INGLOBALZONE(curproc))
634 if (secpolicy_smb(cr) == 0) {
637 return (zfs_secpolicy_deleg_share(zc, cr));
641 #endif /* HAVE_SMB_SHARE */
645 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
650 * Remove the @bla or /bla from the end of the name to get the parent.
652 (void) strncpy(parent, datasetname, parentsize);
653 cp = strrchr(parent, '@');
657 cp = strrchr(parent, '/');
667 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
671 if ((error = zfs_secpolicy_write_perms(name,
672 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
675 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
679 zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr)
681 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
685 * Destroying snapshots with delegated permissions requires
686 * descendent mount and destroy permissions.
689 zfs_secpolicy_destroy_recursive(zfs_cmd_t *zc, cred_t *cr)
694 dsname = kmem_asprintf("%s@", zc->zc_name);
696 error = zfs_secpolicy_destroy_perms(dsname, cr);
698 error = zfs_secpolicy_destroy_perms(zc->zc_name, cr);
705 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
707 char parentname[MAXNAMELEN];
710 if ((error = zfs_secpolicy_write_perms(from,
711 ZFS_DELEG_PERM_RENAME, cr)) != 0)
714 if ((error = zfs_secpolicy_write_perms(from,
715 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
718 if ((error = zfs_get_parent(to, parentname,
719 sizeof (parentname))) != 0)
722 if ((error = zfs_secpolicy_write_perms(parentname,
723 ZFS_DELEG_PERM_CREATE, cr)) != 0)
726 if ((error = zfs_secpolicy_write_perms(parentname,
727 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
734 zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr)
736 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
740 zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr)
742 char parentname[MAXNAMELEN];
746 error = zfs_secpolicy_write_perms(zc->zc_name,
747 ZFS_DELEG_PERM_PROMOTE, cr);
751 error = dmu_objset_hold(zc->zc_name, FTAG, &clone);
754 dsl_dataset_t *pclone = NULL;
756 dd = clone->os_dsl_dataset->ds_dir;
758 rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
759 error = dsl_dataset_hold_obj(dd->dd_pool,
760 dd->dd_phys->dd_origin_obj, FTAG, &pclone);
761 rw_exit(&dd->dd_pool->dp_config_rwlock);
763 dmu_objset_rele(clone, FTAG);
767 error = zfs_secpolicy_write_perms(zc->zc_name,
768 ZFS_DELEG_PERM_MOUNT, cr);
770 dsl_dataset_name(pclone, parentname);
771 dmu_objset_rele(clone, FTAG);
772 dsl_dataset_rele(pclone, FTAG);
774 error = zfs_secpolicy_write_perms(parentname,
775 ZFS_DELEG_PERM_PROMOTE, cr);
781 zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr)
785 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
786 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
789 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
790 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
793 return (zfs_secpolicy_write_perms(zc->zc_name,
794 ZFS_DELEG_PERM_CREATE, cr));
798 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
800 return (zfs_secpolicy_write_perms(name,
801 ZFS_DELEG_PERM_SNAPSHOT, cr));
805 zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr)
808 return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr));
812 zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr)
814 char parentname[MAXNAMELEN];
817 if ((error = zfs_get_parent(zc->zc_name, parentname,
818 sizeof (parentname))) != 0)
821 if (zc->zc_value[0] != '\0') {
822 if ((error = zfs_secpolicy_write_perms(zc->zc_value,
823 ZFS_DELEG_PERM_CLONE, cr)) != 0)
827 if ((error = zfs_secpolicy_write_perms(parentname,
828 ZFS_DELEG_PERM_CREATE, cr)) != 0)
831 error = zfs_secpolicy_write_perms(parentname,
832 ZFS_DELEG_PERM_MOUNT, cr);
838 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
839 * SYS_CONFIG privilege, which is not available in a local zone.
843 zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr)
845 if (secpolicy_sys_config(cr, B_FALSE) != 0)
852 * Policy for object to name lookups.
856 zfs_secpolicy_diff(zfs_cmd_t *zc, cred_t *cr)
860 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
863 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
868 * Policy for fault injection. Requires all privileges.
872 zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr)
874 return (secpolicy_zinject(cr));
878 zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr)
880 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
882 if (prop == ZPROP_INVAL) {
883 if (!zfs_prop_user(zc->zc_value))
885 return (zfs_secpolicy_write_perms(zc->zc_name,
886 ZFS_DELEG_PERM_USERPROP, cr));
888 return (zfs_secpolicy_setprop(zc->zc_name, prop,
894 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr)
896 int err = zfs_secpolicy_read(zc, cr);
900 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
903 if (zc->zc_value[0] == 0) {
905 * They are asking about a posix uid/gid. If it's
906 * themself, allow it.
908 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
909 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
910 if (zc->zc_guid == crgetuid(cr))
913 if (groupmember(zc->zc_guid, cr))
918 return (zfs_secpolicy_write_perms(zc->zc_name,
919 userquota_perms[zc->zc_objset_type], cr));
923 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr)
925 int err = zfs_secpolicy_read(zc, cr);
929 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
932 return (zfs_secpolicy_write_perms(zc->zc_name,
933 userquota_perms[zc->zc_objset_type], cr));
937 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr)
939 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
944 zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr)
946 return (zfs_secpolicy_write_perms(zc->zc_name,
947 ZFS_DELEG_PERM_HOLD, cr));
951 zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr)
953 return (zfs_secpolicy_write_perms(zc->zc_name,
954 ZFS_DELEG_PERM_RELEASE, cr));
958 * Policy for allowing temporary snapshots to be taken or released
961 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, cred_t *cr)
964 * A temporary snapshot is the same as a snapshot,
965 * hold, destroy and release all rolled into one.
966 * Delegated diff alone is sufficient that we allow this.
970 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
971 ZFS_DELEG_PERM_DIFF, cr)) == 0)
974 error = zfs_secpolicy_snapshot(zc, cr);
976 error = zfs_secpolicy_hold(zc, cr);
978 error = zfs_secpolicy_release(zc, cr);
980 error = zfs_secpolicy_destroy(zc, cr);
985 * Returns the nvlist as specified by the user in the zfs_cmd_t.
988 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
992 nvlist_t *list = NULL;
995 * Read in and unpack the user-supplied nvlist.
1000 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1002 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1004 kmem_free(packed, size);
1008 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1009 kmem_free(packed, size);
1013 kmem_free(packed, size);
1020 fit_error_list(zfs_cmd_t *zc, nvlist_t **errors)
1024 VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1026 if (size > zc->zc_nvlist_dst_size) {
1027 nvpair_t *more_errors;
1030 if (zc->zc_nvlist_dst_size < 1024)
1033 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0);
1034 more_errors = nvlist_prev_nvpair(*errors, NULL);
1037 nvpair_t *pair = nvlist_prev_nvpair(*errors,
1039 VERIFY(nvlist_remove_nvpair(*errors, pair) == 0);
1041 VERIFY(nvlist_size(*errors, &size,
1042 NV_ENCODE_NATIVE) == 0);
1043 } while (size > zc->zc_nvlist_dst_size);
1045 VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0);
1046 VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0);
1047 ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0);
1048 ASSERT(size <= zc->zc_nvlist_dst_size);
1055 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1057 char *packed = NULL;
1061 VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0);
1063 if (size > zc->zc_nvlist_dst_size) {
1066 packed = kmem_alloc(size, KM_SLEEP | KM_NODEBUG);
1067 VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE,
1069 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1070 size, zc->zc_iflags) != 0)
1072 kmem_free(packed, size);
1075 zc->zc_nvlist_dst_size = size;
1080 get_zfs_sb(const char *dsname, zfs_sb_t **zsbp)
1085 error = dmu_objset_hold(dsname, FTAG, &os);
1088 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1089 dmu_objset_rele(os, FTAG);
1093 mutex_enter(&os->os_user_ptr_lock);
1094 *zsbp = dmu_objset_get_user(os);
1095 if (*zsbp && (*zsbp)->z_sb) {
1096 atomic_inc(&((*zsbp)->z_sb->s_active));
1100 mutex_exit(&os->os_user_ptr_lock);
1101 dmu_objset_rele(os, FTAG);
1106 * Find a zfs_sb_t for a mounted filesystem, or create our own, in which
1107 * case its z_sb will be NULL, and it will be opened as the owner.
1110 zfs_sb_hold(const char *name, void *tag, zfs_sb_t **zsbp, boolean_t writer)
1114 if (get_zfs_sb(name, zsbp) != 0)
1115 error = zfs_sb_create(name, zsbp);
1117 rrw_enter(&(*zsbp)->z_teardown_lock, (writer) ? RW_WRITER :
1119 if ((*zsbp)->z_unmounted) {
1121 * XXX we could probably try again, since the unmounting
1122 * thread should be just about to disassociate the
1123 * objset from the zfsvfs.
1125 rrw_exit(&(*zsbp)->z_teardown_lock, tag);
1133 zfs_sb_rele(zfs_sb_t *zsb, void *tag)
1135 rrw_exit(&zsb->z_teardown_lock, tag);
1138 deactivate_super(zsb->z_sb);
1140 dmu_objset_disown(zsb->z_os, zsb);
1146 zfs_ioc_pool_create(zfs_cmd_t *zc)
1149 nvlist_t *config, *props = NULL;
1150 nvlist_t *rootprops = NULL;
1151 nvlist_t *zplprops = NULL;
1154 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1155 zc->zc_iflags, &config)))
1158 if (zc->zc_nvlist_src_size != 0 && (error =
1159 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1160 zc->zc_iflags, &props))) {
1161 nvlist_free(config);
1166 nvlist_t *nvl = NULL;
1167 uint64_t version = SPA_VERSION;
1169 (void) nvlist_lookup_uint64(props,
1170 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1171 if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) {
1173 goto pool_props_bad;
1175 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1177 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1179 nvlist_free(config);
1183 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1185 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1186 error = zfs_fill_zplprops_root(version, rootprops,
1189 goto pool_props_bad;
1192 buf = history_str_get(zc);
1194 error = spa_create(zc->zc_name, config, props, buf, zplprops);
1197 * Set the remaining root properties
1199 if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1200 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1201 (void) spa_destroy(zc->zc_name);
1204 history_str_free(buf);
1207 nvlist_free(rootprops);
1208 nvlist_free(zplprops);
1209 nvlist_free(config);
1216 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1219 zfs_log_history(zc);
1220 error = spa_destroy(zc->zc_name);
1222 zvol_remove_minors(zc->zc_name);
1227 zfs_ioc_pool_import(zfs_cmd_t *zc)
1229 nvlist_t *config, *props = NULL;
1233 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1234 zc->zc_iflags, &config)) != 0)
1237 if (zc->zc_nvlist_src_size != 0 && (error =
1238 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1239 zc->zc_iflags, &props))) {
1240 nvlist_free(config);
1244 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1245 guid != zc->zc_guid)
1248 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1250 if (zc->zc_nvlist_dst != 0) {
1253 if ((err = put_nvlist(zc, config)) != 0)
1258 zvol_create_minors(zc->zc_name);
1260 nvlist_free(config);
1269 zfs_ioc_pool_export(zfs_cmd_t *zc)
1272 boolean_t force = (boolean_t)zc->zc_cookie;
1273 boolean_t hardforce = (boolean_t)zc->zc_guid;
1275 zfs_log_history(zc);
1276 error = spa_export(zc->zc_name, NULL, force, hardforce);
1278 zvol_remove_minors(zc->zc_name);
1283 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1288 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1291 error = put_nvlist(zc, configs);
1293 nvlist_free(configs);
1299 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1305 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1306 sizeof (zc->zc_value));
1308 if (config != NULL) {
1309 ret = put_nvlist(zc, config);
1310 nvlist_free(config);
1313 * The config may be present even if 'error' is non-zero.
1314 * In this case we return success, and preserve the real errno
1317 zc->zc_cookie = error;
1326 * Try to import the given pool, returning pool stats as appropriate so that
1327 * user land knows which devices are available and overall pool health.
1330 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1332 nvlist_t *tryconfig, *config;
1335 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1336 zc->zc_iflags, &tryconfig)) != 0)
1339 config = spa_tryimport(tryconfig);
1341 nvlist_free(tryconfig);
1346 error = put_nvlist(zc, config);
1347 nvlist_free(config);
1354 * zc_name name of the pool
1355 * zc_cookie scan func (pool_scan_func_t)
1358 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1363 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1366 if (zc->zc_cookie == POOL_SCAN_NONE)
1367 error = spa_scan_stop(spa);
1369 error = spa_scan(spa, zc->zc_cookie);
1371 spa_close(spa, FTAG);
1377 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1382 error = spa_open(zc->zc_name, &spa, FTAG);
1385 spa_close(spa, FTAG);
1391 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1396 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1399 if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) {
1400 spa_close(spa, FTAG);
1404 spa_upgrade(spa, zc->zc_cookie);
1405 spa_close(spa, FTAG);
1411 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1418 if ((size = zc->zc_history_len) == 0)
1421 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1424 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1425 spa_close(spa, FTAG);
1429 hist_buf = vmem_alloc(size, KM_SLEEP);
1430 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1431 &zc->zc_history_len, hist_buf)) == 0) {
1432 error = ddi_copyout(hist_buf,
1433 (void *)(uintptr_t)zc->zc_history,
1434 zc->zc_history_len, zc->zc_iflags);
1437 spa_close(spa, FTAG);
1438 vmem_free(hist_buf, size);
1443 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1448 error = spa_open(zc->zc_name, &spa, FTAG);
1450 error = spa_change_guid(spa);
1451 spa_close(spa, FTAG);
1457 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1461 if ((error = dsl_dsobj_to_dsname(zc->zc_name,zc->zc_obj,zc->zc_value)))
1469 * zc_name name of filesystem
1470 * zc_obj object to find
1473 * zc_value name of object
1476 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1481 /* XXX reading from objset not owned */
1482 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1484 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1485 dmu_objset_rele(os, FTAG);
1488 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1489 sizeof (zc->zc_value));
1490 dmu_objset_rele(os, FTAG);
1497 * zc_name name of filesystem
1498 * zc_obj object to find
1501 * zc_stat stats on object
1502 * zc_value path to object
1505 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1510 /* XXX reading from objset not owned */
1511 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1513 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1514 dmu_objset_rele(os, FTAG);
1517 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1518 sizeof (zc->zc_value));
1519 dmu_objset_rele(os, FTAG);
1525 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1529 nvlist_t *config, **l2cache, **spares;
1530 uint_t nl2cache = 0, nspares = 0;
1532 error = spa_open(zc->zc_name, &spa, FTAG);
1536 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1537 zc->zc_iflags, &config);
1538 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1539 &l2cache, &nl2cache);
1541 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1545 * A root pool with concatenated devices is not supported.
1546 * Thus, can not add a device to a root pool.
1548 * Intent log device can not be added to a rootpool because
1549 * during mountroot, zil is replayed, a seperated log device
1550 * can not be accessed during the mountroot time.
1552 * l2cache and spare devices are ok to be added to a rootpool.
1554 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1555 nvlist_free(config);
1556 spa_close(spa, FTAG);
1561 error = spa_vdev_add(spa, config);
1562 nvlist_free(config);
1564 spa_close(spa, FTAG);
1570 * zc_name name of the pool
1571 * zc_nvlist_conf nvlist of devices to remove
1572 * zc_cookie to stop the remove?
1575 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1580 error = spa_open(zc->zc_name, &spa, FTAG);
1583 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1584 spa_close(spa, FTAG);
1589 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1593 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1595 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1597 switch (zc->zc_cookie) {
1598 case VDEV_STATE_ONLINE:
1599 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1602 case VDEV_STATE_OFFLINE:
1603 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1606 case VDEV_STATE_FAULTED:
1607 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1608 zc->zc_obj != VDEV_AUX_EXTERNAL)
1609 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1611 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1614 case VDEV_STATE_DEGRADED:
1615 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1616 zc->zc_obj != VDEV_AUX_EXTERNAL)
1617 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1619 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1625 zc->zc_cookie = newstate;
1626 spa_close(spa, FTAG);
1631 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1634 int replacing = zc->zc_cookie;
1638 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1641 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1642 zc->zc_iflags, &config)) == 0) {
1643 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1644 nvlist_free(config);
1647 spa_close(spa, FTAG);
1652 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1657 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1660 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1662 spa_close(spa, FTAG);
1667 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1670 nvlist_t *config, *props = NULL;
1672 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1677 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1678 zc->zc_iflags, &config))) {
1679 spa_close(spa, FTAG);
1683 if (zc->zc_nvlist_src_size != 0 && (error =
1684 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1685 zc->zc_iflags, &props))) {
1686 spa_close(spa, FTAG);
1687 nvlist_free(config);
1691 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1693 spa_close(spa, FTAG);
1695 nvlist_free(config);
1702 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1705 char *path = zc->zc_value;
1706 uint64_t guid = zc->zc_guid;
1709 error = spa_open(zc->zc_name, &spa, FTAG);
1713 error = spa_vdev_setpath(spa, guid, path);
1714 spa_close(spa, FTAG);
1719 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
1722 char *fru = zc->zc_value;
1723 uint64_t guid = zc->zc_guid;
1726 error = spa_open(zc->zc_name, &spa, FTAG);
1730 error = spa_vdev_setfru(spa, guid, fru);
1731 spa_close(spa, FTAG);
1736 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
1741 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1743 if (zc->zc_nvlist_dst != 0 &&
1744 (error = dsl_prop_get_all(os, &nv)) == 0) {
1745 dmu_objset_stats(os, nv);
1747 * NB: zvol_get_stats() will read the objset contents,
1748 * which we aren't supposed to do with a
1749 * DS_MODE_USER hold, because it could be
1750 * inconsistent. So this is a bit of a workaround...
1751 * XXX reading with out owning
1753 if (!zc->zc_objset_stats.dds_inconsistent &&
1754 dmu_objset_type(os) == DMU_OST_ZVOL) {
1755 error = zvol_get_stats(os, nv);
1758 VERIFY3S(error, ==, 0);
1761 error = put_nvlist(zc, nv);
1770 * zc_name name of filesystem
1771 * zc_nvlist_dst_size size of buffer for property nvlist
1774 * zc_objset_stats stats
1775 * zc_nvlist_dst property nvlist
1776 * zc_nvlist_dst_size size of property nvlist
1779 zfs_ioc_objset_stats(zfs_cmd_t *zc)
1781 objset_t *os = NULL;
1784 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1787 error = zfs_ioc_objset_stats_impl(zc, os);
1789 dmu_objset_rele(os, FTAG);
1796 * zc_name name of filesystem
1797 * zc_nvlist_dst_size size of buffer for property nvlist
1800 * zc_nvlist_dst received property nvlist
1801 * zc_nvlist_dst_size size of received property nvlist
1803 * Gets received properties (distinct from local properties on or after
1804 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
1805 * local property values.
1808 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
1810 objset_t *os = NULL;
1814 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1818 * Without this check, we would return local property values if the
1819 * caller has not already received properties on or after
1820 * SPA_VERSION_RECVD_PROPS.
1822 if (!dsl_prop_get_hasrecvd(os)) {
1823 dmu_objset_rele(os, FTAG);
1827 if (zc->zc_nvlist_dst != 0 &&
1828 (error = dsl_prop_get_received(os, &nv)) == 0) {
1829 error = put_nvlist(zc, nv);
1833 dmu_objset_rele(os, FTAG);
1838 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
1844 * zfs_get_zplprop() will either find a value or give us
1845 * the default value (if there is one).
1847 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
1849 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
1855 * zc_name name of filesystem
1856 * zc_nvlist_dst_size size of buffer for zpl property nvlist
1859 * zc_nvlist_dst zpl property nvlist
1860 * zc_nvlist_dst_size size of zpl property nvlist
1863 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
1868 /* XXX reading without owning */
1869 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
1872 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
1875 * NB: nvl_add_zplprop() will read the objset contents,
1876 * which we aren't supposed to do with a DS_MODE_USER
1877 * hold, because it could be inconsistent.
1879 if (zc->zc_nvlist_dst != 0 &&
1880 !zc->zc_objset_stats.dds_inconsistent &&
1881 dmu_objset_type(os) == DMU_OST_ZFS) {
1884 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1885 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
1886 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
1887 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
1888 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
1889 err = put_nvlist(zc, nv);
1894 dmu_objset_rele(os, FTAG);
1899 dataset_name_hidden(const char *name)
1902 * Skip over datasets that are not visible in this zone,
1903 * internal datasets (which have a $ in their name), and
1904 * temporary datasets (which have a % in their name).
1906 if (strchr(name, '$') != NULL)
1908 if (strchr(name, '%') != NULL)
1910 if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
1917 * zc_name name of filesystem
1918 * zc_cookie zap cursor
1919 * zc_nvlist_dst_size size of buffer for property nvlist
1922 * zc_name name of next filesystem
1923 * zc_cookie zap cursor
1924 * zc_objset_stats stats
1925 * zc_nvlist_dst property nvlist
1926 * zc_nvlist_dst_size size of property nvlist
1929 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
1934 size_t orig_len = strlen(zc->zc_name);
1937 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
1938 if (error == ENOENT)
1943 p = strrchr(zc->zc_name, '/');
1944 if (p == NULL || p[1] != '\0')
1945 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
1946 p = zc->zc_name + strlen(zc->zc_name);
1949 * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0
1950 * but is not declared void because its called by dmu_objset_find().
1952 if (zc->zc_cookie == 0) {
1953 uint64_t cookie = 0;
1954 int len = sizeof (zc->zc_name) - (p - zc->zc_name);
1956 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
1957 if (!dataset_name_hidden(zc->zc_name))
1958 (void) dmu_objset_prefetch(zc->zc_name, NULL);
1963 error = dmu_dir_list_next(os,
1964 sizeof (zc->zc_name) - (p - zc->zc_name), p,
1965 NULL, &zc->zc_cookie);
1966 if (error == ENOENT)
1968 } while (error == 0 && dataset_name_hidden(zc->zc_name));
1969 dmu_objset_rele(os, FTAG);
1972 * If it's an internal dataset (ie. with a '$' in its name),
1973 * don't try to get stats for it, otherwise we'll return ENOENT.
1975 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
1976 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
1977 if (error == ENOENT) {
1978 /* We lost a race with destroy, get the next one. */
1979 zc->zc_name[orig_len] = '\0';
1988 * zc_name name of filesystem
1989 * zc_cookie zap cursor
1990 * zc_nvlist_dst_size size of buffer for property nvlist
1993 * zc_name name of next snapshot
1994 * zc_objset_stats stats
1995 * zc_nvlist_dst property nvlist
1996 * zc_nvlist_dst_size size of property nvlist
1999 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2005 if (zc->zc_cookie == 0 && !zc->zc_simple)
2006 (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch,
2007 NULL, DS_FIND_SNAPSHOTS);
2009 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2011 return (error == ENOENT ? ESRCH : error);
2014 * A dataset name of maximum length cannot have any snapshots,
2015 * so exit immediately.
2017 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) {
2018 dmu_objset_rele(os, FTAG);
2022 error = dmu_snapshot_list_next(os,
2023 sizeof (zc->zc_name) - strlen(zc->zc_name),
2024 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2027 if (error == 0 && !zc->zc_simple) {
2029 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2032 * Since we probably don't have a hold on this snapshot,
2033 * it's possible that the objsetid could have been destroyed
2034 * and reused for a new objset. It's OK if this happens during
2035 * a zfs send operation, since the new createtxg will be
2036 * beyond the range we're interested in.
2038 rw_enter(&dp->dp_config_rwlock, RW_READER);
2039 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2040 rw_exit(&dp->dp_config_rwlock);
2042 if (error == ENOENT) {
2043 /* Racing with destroy, get the next one. */
2044 *strchr(zc->zc_name, '@') = '\0';
2045 dmu_objset_rele(os, FTAG);
2051 error = dmu_objset_from_ds(ds, &ossnap);
2053 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2054 dsl_dataset_rele(ds, FTAG);
2056 } else if (error == ENOENT) {
2060 dmu_objset_rele(os, FTAG);
2061 /* if we failed, undo the @ that we tacked on to zc_name */
2063 *strchr(zc->zc_name, '@') = '\0';
2068 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2070 const char *propname = nvpair_name(pair);
2072 unsigned int vallen;
2075 zfs_userquota_prop_t type;
2081 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2083 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2084 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2090 * A correctly constructed propname is encoded as
2091 * userquota@<rid>-<domain>.
2093 if ((dash = strchr(propname, '-')) == NULL ||
2094 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2103 err = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
2105 err = zfs_set_userquota(zsb, type, domain, rid, quota);
2106 zfs_sb_rele(zsb, FTAG);
2113 * If the named property is one that has a special function to set its value,
2114 * return 0 on success and a positive error code on failure; otherwise if it is
2115 * not one of the special properties handled by this function, return -1.
2117 * XXX: It would be better for callers of the property interface if we handled
2118 * these special cases in dsl_prop.c (in the dsl layer).
2121 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2124 const char *propname = nvpair_name(pair);
2125 zfs_prop_t prop = zfs_name_to_prop(propname);
2129 if (prop == ZPROP_INVAL) {
2130 if (zfs_prop_userquota(propname))
2131 return (zfs_prop_set_userquota(dsname, pair));
2135 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2137 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2138 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2142 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2145 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2148 case ZFS_PROP_QUOTA:
2149 err = dsl_dir_set_quota(dsname, source, intval);
2151 case ZFS_PROP_REFQUOTA:
2152 err = dsl_dataset_set_quota(dsname, source, intval);
2154 case ZFS_PROP_RESERVATION:
2155 err = dsl_dir_set_reservation(dsname, source, intval);
2157 case ZFS_PROP_REFRESERVATION:
2158 err = dsl_dataset_set_reservation(dsname, source, intval);
2160 case ZFS_PROP_VOLSIZE:
2161 err = zvol_set_volsize(dsname, intval);
2163 case ZFS_PROP_VERSION:
2167 if ((err = zfs_sb_hold(dsname, FTAG, &zsb, B_TRUE)) != 0)
2170 err = zfs_set_version(zsb, intval);
2171 zfs_sb_rele(zsb, FTAG);
2173 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2176 zc = kmem_zalloc(sizeof (zfs_cmd_t),
2177 KM_SLEEP | KM_NODEBUG);
2178 (void) strcpy(zc->zc_name, dsname);
2179 (void) zfs_ioc_userspace_upgrade(zc);
2180 kmem_free(zc, sizeof (zfs_cmd_t));
2193 * This function is best effort. If it fails to set any of the given properties,
2194 * it continues to set as many as it can and returns the first error
2195 * encountered. If the caller provides a non-NULL errlist, it also gives the
2196 * complete list of names of all the properties it failed to set along with the
2197 * corresponding error numbers. The caller is responsible for freeing the
2200 * If every property is set successfully, zero is returned and the list pointed
2201 * at by errlist is NULL.
2204 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2212 nvlist_t *genericnvl;
2216 VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2217 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2218 VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2222 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2223 const char *propname = nvpair_name(pair);
2224 zfs_prop_t prop = zfs_name_to_prop(propname);
2227 /* decode the property value */
2229 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2231 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2232 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2237 /* Validate value type */
2238 if (err == 0 && prop == ZPROP_INVAL) {
2239 if (zfs_prop_user(propname)) {
2240 if (nvpair_type(propval) != DATA_TYPE_STRING)
2242 } else if (zfs_prop_userquota(propname)) {
2243 if (nvpair_type(propval) !=
2244 DATA_TYPE_UINT64_ARRAY)
2249 } else if (err == 0) {
2250 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2251 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2253 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2256 VERIFY(nvpair_value_uint64(propval,
2259 switch (zfs_prop_get_type(prop)) {
2260 case PROP_TYPE_NUMBER:
2262 case PROP_TYPE_STRING:
2265 case PROP_TYPE_INDEX:
2266 if (zfs_prop_index_to_string(prop,
2267 intval, &unused) != 0)
2272 "unknown property type");
2279 /* Validate permissions */
2281 err = zfs_check_settable(dsname, pair, CRED());
2284 err = zfs_prop_set_special(dsname, source, pair);
2287 * For better performance we build up a list of
2288 * properties to set in a single transaction.
2290 err = nvlist_add_nvpair(genericnvl, pair);
2291 } else if (err != 0 && nvl != retrynvl) {
2293 * This may be a spurious error caused by
2294 * receiving quota and reservation out of order.
2295 * Try again in a second pass.
2297 err = nvlist_add_nvpair(retrynvl, pair);
2302 VERIFY(nvlist_add_int32(errors, propname, err) == 0);
2305 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2310 if (!nvlist_empty(genericnvl) &&
2311 dsl_props_set(dsname, source, genericnvl) != 0) {
2313 * If this fails, we still want to set as many properties as we
2314 * can, so try setting them individually.
2317 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2318 const char *propname = nvpair_name(pair);
2322 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2324 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2325 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2329 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2330 VERIFY(nvpair_value_string(propval,
2332 err = dsl_prop_set(dsname, propname, source, 1,
2333 strlen(strval) + 1, strval);
2335 VERIFY(nvpair_value_uint64(propval,
2337 err = dsl_prop_set(dsname, propname, source, 8,
2342 VERIFY(nvlist_add_int32(errors, propname,
2347 nvlist_free(genericnvl);
2348 nvlist_free(retrynvl);
2350 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
2351 nvlist_free(errors);
2354 VERIFY(nvpair_value_int32(pair, &rv) == 0);
2357 if (errlist == NULL)
2358 nvlist_free(errors);
2366 * Check that all the properties are valid user properties.
2369 zfs_check_userprops(char *fsname, nvlist_t *nvl)
2371 nvpair_t *pair = NULL;
2374 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2375 const char *propname = nvpair_name(pair);
2378 if (!zfs_prop_user(propname) ||
2379 nvpair_type(pair) != DATA_TYPE_STRING)
2382 if ((error = zfs_secpolicy_write_perms(fsname,
2383 ZFS_DELEG_PERM_USERPROP, CRED())))
2386 if (strlen(propname) >= ZAP_MAXNAMELEN)
2387 return (ENAMETOOLONG);
2389 VERIFY(nvpair_value_string(pair, &valstr) == 0);
2390 if (strlen(valstr) >= ZAP_MAXVALUELEN)
2397 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2401 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2404 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2405 if (nvlist_exists(skipped, nvpair_name(pair)))
2408 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2413 clear_received_props(objset_t *os, const char *fs, nvlist_t *props,
2417 nvlist_t *cleared_props = NULL;
2418 props_skip(props, skipped, &cleared_props);
2419 if (!nvlist_empty(cleared_props)) {
2421 * Acts on local properties until the dataset has received
2422 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2424 zprop_source_t flags = (ZPROP_SRC_NONE |
2425 (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0));
2426 err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL);
2428 nvlist_free(cleared_props);
2434 * zc_name name of filesystem
2435 * zc_value name of property to set
2436 * zc_nvlist_src{_size} nvlist of properties to apply
2437 * zc_cookie received properties flag
2440 * zc_nvlist_dst{_size} error for each unapplied received property
2443 zfs_ioc_set_prop(zfs_cmd_t *zc)
2446 boolean_t received = zc->zc_cookie;
2447 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2449 nvlist_t *errors = NULL;
2452 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2453 zc->zc_iflags, &nvl)) != 0)
2457 nvlist_t *origprops;
2460 if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) {
2461 if (dsl_prop_get_received(os, &origprops) == 0) {
2462 (void) clear_received_props(os,
2463 zc->zc_name, origprops, nvl);
2464 nvlist_free(origprops);
2467 dsl_prop_set_hasrecvd(os);
2468 dmu_objset_rele(os, FTAG);
2472 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors);
2474 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2475 (void) put_nvlist(zc, errors);
2478 nvlist_free(errors);
2485 * zc_name name of filesystem
2486 * zc_value name of property to inherit
2487 * zc_cookie revert to received value if TRUE
2492 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2494 const char *propname = zc->zc_value;
2495 zfs_prop_t prop = zfs_name_to_prop(propname);
2496 boolean_t received = zc->zc_cookie;
2497 zprop_source_t source = (received
2498 ? ZPROP_SRC_NONE /* revert to received value, if any */
2499 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2508 * zfs_prop_set_special() expects properties in the form of an
2509 * nvpair with type info.
2511 if (prop == ZPROP_INVAL) {
2512 if (!zfs_prop_user(propname))
2515 type = PROP_TYPE_STRING;
2516 } else if (prop == ZFS_PROP_VOLSIZE ||
2517 prop == ZFS_PROP_VERSION) {
2520 type = zfs_prop_get_type(prop);
2523 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2526 case PROP_TYPE_STRING:
2527 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2529 case PROP_TYPE_NUMBER:
2530 case PROP_TYPE_INDEX:
2531 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2538 pair = nvlist_next_nvpair(dummy, NULL);
2539 err = zfs_prop_set_special(zc->zc_name, source, pair);
2542 return (err); /* special property already handled */
2545 * Only check this in the non-received case. We want to allow
2546 * 'inherit -S' to revert non-inheritable properties like quota
2547 * and reservation to the received or default values even though
2548 * they are not considered inheritable.
2550 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2554 /* the property name has been validated by zfs_secpolicy_inherit() */
2555 return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL));
2559 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2566 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2567 zc->zc_iflags, &props)))
2571 * If the only property is the configfile, then just do a spa_lookup()
2572 * to handle the faulted case.
2574 pair = nvlist_next_nvpair(props, NULL);
2575 if (pair != NULL && strcmp(nvpair_name(pair),
2576 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2577 nvlist_next_nvpair(props, pair) == NULL) {
2578 mutex_enter(&spa_namespace_lock);
2579 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2580 spa_configfile_set(spa, props, B_FALSE);
2581 spa_config_sync(spa, B_FALSE, B_TRUE);
2583 mutex_exit(&spa_namespace_lock);
2590 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2595 error = spa_prop_set(spa, props);
2598 spa_close(spa, FTAG);
2604 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2608 nvlist_t *nvp = NULL;
2610 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2612 * If the pool is faulted, there may be properties we can still
2613 * get (such as altroot and cachefile), so attempt to get them
2616 mutex_enter(&spa_namespace_lock);
2617 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2618 error = spa_prop_get(spa, &nvp);
2619 mutex_exit(&spa_namespace_lock);
2621 error = spa_prop_get(spa, &nvp);
2622 spa_close(spa, FTAG);
2625 if (error == 0 && zc->zc_nvlist_dst != 0)
2626 error = put_nvlist(zc, nvp);
2636 * zc_name name of volume
2641 zfs_ioc_create_minor(zfs_cmd_t *zc)
2643 return (zvol_create_minor(zc->zc_name));
2648 * zc_name name of volume
2653 zfs_ioc_remove_minor(zfs_cmd_t *zc)
2655 return (zvol_remove_minor(zc->zc_name));
2660 * zc_name name of filesystem
2661 * zc_nvlist_src{_size} nvlist of delegated permissions
2662 * zc_perm_action allow/unallow flag
2667 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2670 nvlist_t *fsaclnv = NULL;
2672 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2673 zc->zc_iflags, &fsaclnv)) != 0)
2677 * Verify nvlist is constructed correctly
2679 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2680 nvlist_free(fsaclnv);
2685 * If we don't have PRIV_SYS_MOUNT, then validate
2686 * that user is allowed to hand out each permission in
2690 error = secpolicy_zfs(CRED());
2692 if (zc->zc_perm_action == B_FALSE) {
2693 error = dsl_deleg_can_allow(zc->zc_name,
2696 error = dsl_deleg_can_unallow(zc->zc_name,
2702 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2704 nvlist_free(fsaclnv);
2710 * zc_name name of filesystem
2713 * zc_nvlist_src{_size} nvlist of delegated permissions
2716 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
2721 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
2722 error = put_nvlist(zc, nvp);
2731 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
2733 zfs_creat_t *zct = arg;
2735 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
2738 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
2742 * createprops list of properties requested by creator
2743 * default_zplver zpl version to use if unspecified in createprops
2744 * fuids_ok fuids allowed in this version of the spa?
2745 * os parent objset pointer (NULL if root fs)
2748 * zplprops values for the zplprops we attach to the master node object
2749 * is_ci true if requested file system will be purely case-insensitive
2751 * Determine the settings for utf8only, normalization and
2752 * casesensitivity. Specific values may have been requested by the
2753 * creator and/or we can inherit values from the parent dataset. If
2754 * the file system is of too early a vintage, a creator can not
2755 * request settings for these properties, even if the requested
2756 * setting is the default value. We don't actually want to create dsl
2757 * properties for these, so remove them from the source nvlist after
2761 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
2762 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
2763 nvlist_t *zplprops, boolean_t *is_ci)
2765 uint64_t sense = ZFS_PROP_UNDEFINED;
2766 uint64_t norm = ZFS_PROP_UNDEFINED;
2767 uint64_t u8 = ZFS_PROP_UNDEFINED;
2770 ASSERT(zplprops != NULL);
2773 * Pull out creator prop choices, if any.
2776 (void) nvlist_lookup_uint64(createprops,
2777 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
2778 (void) nvlist_lookup_uint64(createprops,
2779 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
2780 (void) nvlist_remove_all(createprops,
2781 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
2782 (void) nvlist_lookup_uint64(createprops,
2783 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
2784 (void) nvlist_remove_all(createprops,
2785 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
2786 (void) nvlist_lookup_uint64(createprops,
2787 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
2788 (void) nvlist_remove_all(createprops,
2789 zfs_prop_to_name(ZFS_PROP_CASE));
2793 * If the zpl version requested is whacky or the file system
2794 * or pool is version is too "young" to support normalization
2795 * and the creator tried to set a value for one of the props,
2798 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
2799 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
2800 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
2801 (zplver < ZPL_VERSION_NORMALIZATION &&
2802 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
2803 sense != ZFS_PROP_UNDEFINED)))
2807 * Put the version in the zplprops
2809 VERIFY(nvlist_add_uint64(zplprops,
2810 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
2812 if (norm == ZFS_PROP_UNDEFINED &&
2813 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
2815 VERIFY(nvlist_add_uint64(zplprops,
2816 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
2819 * If we're normalizing, names must always be valid UTF-8 strings.
2823 if (u8 == ZFS_PROP_UNDEFINED &&
2824 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
2826 VERIFY(nvlist_add_uint64(zplprops,
2827 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
2829 if (sense == ZFS_PROP_UNDEFINED &&
2830 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
2832 VERIFY(nvlist_add_uint64(zplprops,
2833 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
2836 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
2842 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
2843 nvlist_t *zplprops, boolean_t *is_ci)
2845 boolean_t fuids_ok, sa_ok;
2846 uint64_t zplver = ZPL_VERSION;
2847 objset_t *os = NULL;
2848 char parentname[MAXNAMELEN];
2854 (void) strlcpy(parentname, dataset, sizeof (parentname));
2855 cp = strrchr(parentname, '/');
2859 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
2862 spa_vers = spa_version(spa);
2863 spa_close(spa, FTAG);
2865 zplver = zfs_zpl_version_map(spa_vers);
2866 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2867 sa_ok = (zplver >= ZPL_VERSION_SA);
2870 * Open parent object set so we can inherit zplprop values.
2872 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
2875 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
2877 dmu_objset_rele(os, FTAG);
2882 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
2883 nvlist_t *zplprops, boolean_t *is_ci)
2887 uint64_t zplver = ZPL_VERSION;
2890 zplver = zfs_zpl_version_map(spa_vers);
2891 fuids_ok = (zplver >= ZPL_VERSION_FUID);
2892 sa_ok = (zplver >= ZPL_VERSION_SA);
2894 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
2895 createprops, zplprops, is_ci);
2901 * zc_objset_type type of objset to create (fs vs zvol)
2902 * zc_name name of new objset
2903 * zc_value name of snapshot to clone from (may be empty)
2904 * zc_nvlist_src{_size} nvlist of properties to apply
2909 zfs_ioc_create(zfs_cmd_t *zc)
2914 nvlist_t *nvprops = NULL;
2915 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
2916 dmu_objset_type_t type = zc->zc_objset_type;
2921 cbfunc = zfs_create_cb;
2925 cbfunc = zvol_create_cb;
2932 if (strchr(zc->zc_name, '@') ||
2933 strchr(zc->zc_name, '%'))
2936 if (zc->zc_nvlist_src != 0 &&
2937 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2938 zc->zc_iflags, &nvprops)) != 0)
2941 zct.zct_zplprops = NULL;
2942 zct.zct_props = nvprops;
2944 if (zc->zc_value[0] != '\0') {
2946 * We're creating a clone of an existing snapshot.
2948 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
2949 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) {
2950 nvlist_free(nvprops);
2954 error = dmu_objset_hold(zc->zc_value, FTAG, &clone);
2956 nvlist_free(nvprops);
2960 error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0);
2961 dmu_objset_rele(clone, FTAG);
2963 nvlist_free(nvprops);
2967 boolean_t is_insensitive = B_FALSE;
2969 if (cbfunc == NULL) {
2970 nvlist_free(nvprops);
2974 if (type == DMU_OST_ZVOL) {
2975 uint64_t volsize, volblocksize;
2977 if (nvprops == NULL ||
2978 nvlist_lookup_uint64(nvprops,
2979 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2981 nvlist_free(nvprops);
2985 if ((error = nvlist_lookup_uint64(nvprops,
2986 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2987 &volblocksize)) != 0 && error != ENOENT) {
2988 nvlist_free(nvprops);
2993 volblocksize = zfs_prop_default_numeric(
2994 ZFS_PROP_VOLBLOCKSIZE);
2996 if ((error = zvol_check_volblocksize(
2997 volblocksize)) != 0 ||
2998 (error = zvol_check_volsize(volsize,
2999 volblocksize)) != 0) {
3000 nvlist_free(nvprops);
3003 } else if (type == DMU_OST_ZFS) {
3007 * We have to have normalization and
3008 * case-folding flags correct when we do the
3009 * file system creation, so go figure them out
3012 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3013 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3014 error = zfs_fill_zplprops(zc->zc_name, nvprops,
3015 zct.zct_zplprops, &is_insensitive);
3017 nvlist_free(nvprops);
3018 nvlist_free(zct.zct_zplprops);
3022 error = dmu_objset_create(zc->zc_name, type,
3023 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3024 nvlist_free(zct.zct_zplprops);
3028 * It would be nice to do this atomically.
3031 error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL,
3034 (void) dmu_objset_destroy(zc->zc_name, B_FALSE);
3036 nvlist_free(nvprops);
3042 * zc_name name of filesystem
3043 * zc_value short name of snapshot
3044 * zc_cookie recursive flag
3045 * zc_nvlist_src[_size] property list
3048 * zc_value short snapname (i.e. part after the '@')
3051 zfs_ioc_snapshot(zfs_cmd_t *zc)
3053 nvlist_t *nvprops = NULL;
3055 boolean_t recursive = zc->zc_cookie;
3057 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
3060 if (zc->zc_nvlist_src != 0 &&
3061 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3062 zc->zc_iflags, &nvprops)) != 0)
3065 error = zfs_check_userprops(zc->zc_name, nvprops);
3069 if (!nvlist_empty(nvprops) &&
3070 zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) {
3075 error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, NULL,
3076 nvprops, recursive, B_FALSE, -1);
3079 nvlist_free(nvprops);
3085 * name dataset name, or when 'arg == NULL' the full snapshot name
3086 * arg short snapshot name (i.e. part after the '@')
3089 zfs_unmount_snap(const char *name, void *arg)
3091 zfs_sb_t *zsb = NULL;
3099 dsname = strdup(name);
3100 snapname = strdup(arg);
3102 ptr = strchr(name, '@');
3104 dsname = strdup(name);
3105 dsname[ptr - name] = '\0';
3106 snapname = strdup(ptr + 1);
3112 fullname = kmem_asprintf("%s@%s", dsname, snapname);
3114 error = zfs_sb_hold(dsname, FTAG, &zsb, B_FALSE);
3116 error = zfsctl_unmount_snapshot(zsb, fullname, MNT_FORCE);
3117 zfs_sb_rele(zsb, FTAG);
3119 /* Allow ENOENT for consistency with upstream */
3120 if (error == ENOENT)
3133 * zc_name name of filesystem, snaps must be under it
3134 * zc_nvlist_src[_size] full names of snapshots to destroy
3135 * zc_defer_destroy mark for deferred destroy
3138 * zc_name on failure, name of failed snapshot
3141 zfs_ioc_destroy_snaps_nvl(zfs_cmd_t *zc)
3147 if ((err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3148 zc->zc_iflags, &nvl)) != 0)
3151 len = strlen(zc->zc_name);
3152 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
3153 pair = nvlist_next_nvpair(nvl, pair)) {
3154 const char *name = nvpair_name(pair);
3156 * The snap name must be underneath the zc_name. This ensures
3157 * that our permission checks were legitimate.
3159 if (strncmp(zc->zc_name, name, len) != 0 ||
3160 (name[len] != '@' && name[len] != '/')) {
3165 (void) zfs_unmount_snap(name, NULL);
3166 (void) zvol_remove_minor(name);
3169 err = dmu_snapshots_destroy_nvl(nvl, zc->zc_defer_destroy,
3177 * zc_name name of dataset to destroy
3178 * zc_objset_type type of objset
3179 * zc_defer_destroy mark for deferred destroy
3184 zfs_ioc_destroy(zfs_cmd_t *zc)
3187 if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) {
3188 err = zfs_unmount_snap(zc->zc_name, NULL);
3193 err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy);
3194 if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3195 (void) zvol_remove_minor(zc->zc_name);
3201 * zc_name name of dataset to rollback (to most recent snapshot)
3206 zfs_ioc_rollback(zfs_cmd_t *zc)
3208 dsl_dataset_t *ds, *clone;
3213 error = dsl_dataset_hold(zc->zc_name, FTAG, &ds);
3217 /* must not be a snapshot */
3218 if (dsl_dataset_is_snapshot(ds)) {
3219 dsl_dataset_rele(ds, FTAG);
3223 /* must have a most recent snapshot */
3224 if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
3225 dsl_dataset_rele(ds, FTAG);
3230 * Create clone of most recent snapshot.
3232 clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name);
3233 error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT);
3237 error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone);
3244 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
3245 error = zfs_suspend_fs(zsb);
3249 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3250 error = dsl_dataset_clone_swap(clone, ds,
3252 dsl_dataset_disown(ds, FTAG);
3257 resume_err = zfs_resume_fs(zsb, zc->zc_name);
3258 error = error ? error : resume_err;
3260 deactivate_super(zsb->z_sb);
3262 if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) {
3263 error = dsl_dataset_clone_swap(clone, ds, B_TRUE);
3264 dsl_dataset_disown(ds, FTAG);
3272 * Destroy clone (which also closes it).
3274 (void) dsl_dataset_destroy(clone, FTAG, B_FALSE);
3277 strfree(clone_name);
3279 dsl_dataset_rele(ds, FTAG);
3285 * zc_name old name of dataset
3286 * zc_value new name of dataset
3287 * zc_cookie recursive flag (only valid for snapshots)
3292 zfs_ioc_rename(zfs_cmd_t *zc)
3294 boolean_t recursive = zc->zc_cookie & 1;
3297 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3298 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3299 strchr(zc->zc_value, '%'))
3303 * Unmount snapshot unless we're doing a recursive rename,
3304 * in which case the dataset code figures out which snapshots
3307 if (!recursive && strchr(zc->zc_name, '@') != NULL &&
3308 zc->zc_objset_type == DMU_OST_ZFS) {
3309 err = zfs_unmount_snap(zc->zc_name, NULL);
3314 err = dmu_objset_rename(zc->zc_name, zc->zc_value, recursive);
3315 if ((err == 0) && (zc->zc_objset_type == DMU_OST_ZVOL)) {
3316 (void) zvol_remove_minor(zc->zc_name);
3317 (void) zvol_create_minor(zc->zc_value);
3324 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3326 const char *propname = nvpair_name(pair);
3327 boolean_t issnap = (strchr(dsname, '@') != NULL);
3328 zfs_prop_t prop = zfs_name_to_prop(propname);
3332 if (prop == ZPROP_INVAL) {
3333 if (zfs_prop_user(propname)) {
3334 if ((err = zfs_secpolicy_write_perms(dsname,
3335 ZFS_DELEG_PERM_USERPROP, cr)))
3340 if (!issnap && zfs_prop_userquota(propname)) {
3341 const char *perm = NULL;
3342 const char *uq_prefix =
3343 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3344 const char *gq_prefix =
3345 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3347 if (strncmp(propname, uq_prefix,
3348 strlen(uq_prefix)) == 0) {
3349 perm = ZFS_DELEG_PERM_USERQUOTA;
3350 } else if (strncmp(propname, gq_prefix,
3351 strlen(gq_prefix)) == 0) {
3352 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3354 /* USERUSED and GROUPUSED are read-only */
3358 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3369 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3371 * dsl_prop_get_all_impl() returns properties in this
3375 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3376 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3381 * Check that this value is valid for this pool version
3384 case ZFS_PROP_COMPRESSION:
3386 * If the user specified gzip compression, make sure
3387 * the SPA supports it. We ignore any errors here since
3388 * we'll catch them later.
3390 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3391 nvpair_value_uint64(pair, &intval) == 0) {
3392 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3393 intval <= ZIO_COMPRESS_GZIP_9 &&
3394 zfs_earlier_version(dsname,
3395 SPA_VERSION_GZIP_COMPRESSION)) {
3399 if (intval == ZIO_COMPRESS_ZLE &&
3400 zfs_earlier_version(dsname,
3401 SPA_VERSION_ZLE_COMPRESSION))
3405 * If this is a bootable dataset then
3406 * verify that the compression algorithm
3407 * is supported for booting. We must return
3408 * something other than ENOTSUP since it
3409 * implies a downrev pool version.
3411 if (zfs_is_bootfs(dsname) &&
3412 !BOOTFS_COMPRESS_VALID(intval)) {
3418 case ZFS_PROP_COPIES:
3419 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
3423 case ZFS_PROP_DEDUP:
3424 if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3428 case ZFS_PROP_SHARESMB:
3429 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3433 case ZFS_PROP_ACLINHERIT:
3434 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3435 nvpair_value_uint64(pair, &intval) == 0) {
3436 if (intval == ZFS_ACL_PASSTHROUGH_X &&
3437 zfs_earlier_version(dsname,
3438 SPA_VERSION_PASSTHROUGH_X))
3446 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
3450 * Removes properties from the given props list that fail permission checks
3451 * needed to clear them and to restore them in case of a receive error. For each
3452 * property, make sure we have both set and inherit permissions.
3454 * Returns the first error encountered if any permission checks fail. If the
3455 * caller provides a non-NULL errlist, it also gives the complete list of names
3456 * of all the properties that failed a permission check along with the
3457 * corresponding error numbers. The caller is responsible for freeing the
3460 * If every property checks out successfully, zero is returned and the list
3461 * pointed at by errlist is NULL.
3464 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
3467 nvpair_t *pair, *next_pair;
3474 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3476 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
3477 (void) strcpy(zc->zc_name, dataset);
3478 pair = nvlist_next_nvpair(props, NULL);
3479 while (pair != NULL) {
3480 next_pair = nvlist_next_nvpair(props, pair);
3482 (void) strcpy(zc->zc_value, nvpair_name(pair));
3483 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
3484 (err = zfs_secpolicy_inherit(zc, CRED())) != 0) {
3485 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
3486 VERIFY(nvlist_add_int32(errors,
3487 zc->zc_value, err) == 0);
3491 kmem_free(zc, sizeof (zfs_cmd_t));
3493 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
3494 nvlist_free(errors);
3497 VERIFY(nvpair_value_int32(pair, &rv) == 0);
3500 if (errlist == NULL)
3501 nvlist_free(errors);
3509 propval_equals(nvpair_t *p1, nvpair_t *p2)
3511 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
3512 /* dsl_prop_get_all_impl() format */
3514 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
3515 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3519 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
3521 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
3522 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3526 if (nvpair_type(p1) != nvpair_type(p2))
3529 if (nvpair_type(p1) == DATA_TYPE_STRING) {
3530 char *valstr1, *valstr2;
3532 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
3533 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
3534 return (strcmp(valstr1, valstr2) == 0);
3536 uint64_t intval1, intval2;
3538 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
3539 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
3540 return (intval1 == intval2);
3545 * Remove properties from props if they are not going to change (as determined
3546 * by comparison with origprops). Remove them from origprops as well, since we
3547 * do not need to clear or restore properties that won't change.
3550 props_reduce(nvlist_t *props, nvlist_t *origprops)
3552 nvpair_t *pair, *next_pair;
3554 if (origprops == NULL)
3555 return; /* all props need to be received */
3557 pair = nvlist_next_nvpair(props, NULL);
3558 while (pair != NULL) {
3559 const char *propname = nvpair_name(pair);
3562 next_pair = nvlist_next_nvpair(props, pair);
3564 if ((nvlist_lookup_nvpair(origprops, propname,
3565 &match) != 0) || !propval_equals(pair, match))
3566 goto next; /* need to set received value */
3568 /* don't clear the existing received value */
3569 (void) nvlist_remove_nvpair(origprops, match);
3570 /* don't bother receiving the property */
3571 (void) nvlist_remove_nvpair(props, pair);
3578 static boolean_t zfs_ioc_recv_inject_err;
3583 * zc_name name of containing filesystem
3584 * zc_nvlist_src{_size} nvlist of properties to apply
3585 * zc_value name of snapshot to create
3586 * zc_string name of clone origin (if DRR_FLAG_CLONE)
3587 * zc_cookie file descriptor to recv from
3588 * zc_begin_record the BEGIN record of the stream (not byteswapped)
3589 * zc_guid force flag
3590 * zc_cleanup_fd cleanup-on-exit file descriptor
3591 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
3594 * zc_cookie number of bytes read
3595 * zc_nvlist_dst{_size} error for each unapplied received property
3596 * zc_obj zprop_errflags_t
3597 * zc_action_handle handle for this guid/ds mapping
3600 zfs_ioc_recv(zfs_cmd_t *zc)
3604 dmu_recv_cookie_t drc;
3605 boolean_t force = (boolean_t)zc->zc_guid;
3608 int props_error = 0;
3611 nvlist_t *props = NULL; /* sent properties */
3612 nvlist_t *origprops = NULL; /* existing properties */
3613 objset_t *origin = NULL;
3615 char tofs[ZFS_MAXNAMELEN];
3616 boolean_t first_recvd_props = B_FALSE;
3618 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3619 strchr(zc->zc_value, '@') == NULL ||
3620 strchr(zc->zc_value, '%'))
3623 (void) strcpy(tofs, zc->zc_value);
3624 tosnap = strchr(tofs, '@');
3627 if (zc->zc_nvlist_src != 0 &&
3628 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3629 zc->zc_iflags, &props)) != 0)
3639 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3641 if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) {
3642 if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) &&
3643 !dsl_prop_get_hasrecvd(os)) {
3644 first_recvd_props = B_TRUE;
3648 * If new received properties are supplied, they are to
3649 * completely replace the existing received properties, so stash
3650 * away the existing ones.
3652 if (dsl_prop_get_received(os, &origprops) == 0) {
3653 nvlist_t *errlist = NULL;
3655 * Don't bother writing a property if its value won't
3656 * change (and avoid the unnecessary security checks).
3658 * The first receive after SPA_VERSION_RECVD_PROPS is a
3659 * special case where we blow away all local properties
3662 if (!first_recvd_props)
3663 props_reduce(props, origprops);
3664 if (zfs_check_clearable(tofs, origprops,
3666 (void) nvlist_merge(errors, errlist, 0);
3667 nvlist_free(errlist);
3670 dmu_objset_rele(os, FTAG);
3673 if (zc->zc_string[0]) {
3674 error = dmu_objset_hold(zc->zc_string, FTAG, &origin);
3679 error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds,
3680 &zc->zc_begin_record, force, origin, &drc);
3682 dmu_objset_rele(origin, FTAG);
3687 * Set properties before we receive the stream so that they are applied
3688 * to the new data. Note that we must call dmu_recv_stream() if
3689 * dmu_recv_begin() succeeds.
3694 if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) {
3695 if (drc.drc_newfs) {
3696 if (spa_version(os->os_spa) >=
3697 SPA_VERSION_RECVD_PROPS)
3698 first_recvd_props = B_TRUE;
3699 } else if (origprops != NULL) {
3700 if (clear_received_props(os, tofs, origprops,
3701 first_recvd_props ? NULL : props) != 0)
3702 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3704 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3706 dsl_prop_set_hasrecvd(os);
3707 } else if (!drc.drc_newfs) {
3708 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
3711 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
3713 (void) nvlist_merge(errors, errlist, 0);
3714 nvlist_free(errlist);
3717 if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) {
3719 * Caller made zc->zc_nvlist_dst less than the minimum expected
3720 * size or supplied an invalid address.
3722 props_error = EINVAL;
3726 error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
3727 &zc->zc_action_handle);
3730 zfs_sb_t *zsb = NULL;
3732 if (get_zfs_sb(tofs, &zsb) == 0) {
3736 error = zfs_suspend_fs(zsb);
3738 * If the suspend fails, then the recv_end will
3739 * likely also fail, and clean up after itself.
3741 end_err = dmu_recv_end(&drc);
3743 error = zfs_resume_fs(zsb, tofs);
3744 error = error ? error : end_err;
3745 deactivate_super(zsb->z_sb);
3747 error = dmu_recv_end(&drc);
3751 zc->zc_cookie = off - fp->f_offset;
3752 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3756 if (zfs_ioc_recv_inject_err) {
3757 zfs_ioc_recv_inject_err = B_FALSE;
3762 * On error, restore the original props.
3764 if (error && props) {
3765 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
3766 if (clear_received_props(os, tofs, props, NULL) != 0) {
3768 * We failed to clear the received properties.
3769 * Since we may have left a $recvd value on the
3770 * system, we can't clear the $hasrecvd flag.
3772 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3773 } else if (first_recvd_props) {
3774 dsl_prop_unset_hasrecvd(os);
3776 dmu_objset_rele(os, FTAG);
3777 } else if (!drc.drc_newfs) {
3778 /* We failed to clear the received properties. */
3779 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3782 if (origprops == NULL && !drc.drc_newfs) {
3783 /* We failed to stash the original properties. */
3784 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3788 * dsl_props_set() will not convert RECEIVED to LOCAL on or
3789 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
3790 * explictly if we're restoring local properties cleared in the
3791 * first new-style receive.
3793 if (origprops != NULL &&
3794 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
3795 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
3796 origprops, NULL) != 0) {
3798 * We stashed the original properties but failed to
3801 zc->zc_obj |= ZPROP_ERR_NORESTORE;
3806 nvlist_free(origprops);
3807 nvlist_free(errors);
3811 error = props_error;
3818 * zc_name name of snapshot to send
3819 * zc_cookie file descriptor to send stream to
3820 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
3821 * zc_sendobj objsetid of snapshot to send
3822 * zc_fromobj objsetid of incremental fromsnap (may be zero)
3823 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
3824 * output size in zc_objset_type.
3829 zfs_ioc_send(zfs_cmd_t *zc)
3831 objset_t *fromsnap = NULL;
3836 dsl_dataset_t *dsfrom = NULL;
3839 boolean_t estimate = (zc->zc_guid != 0);
3841 error = spa_open(zc->zc_name, &spa, FTAG);
3845 dp = spa_get_dsl(spa);
3846 rw_enter(&dp->dp_config_rwlock, RW_READER);
3847 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
3848 rw_exit(&dp->dp_config_rwlock);
3850 spa_close(spa, FTAG);
3854 error = dmu_objset_from_ds(ds, &tosnap);
3856 dsl_dataset_rele(ds, FTAG);
3857 spa_close(spa, FTAG);
3861 if (zc->zc_fromobj != 0) {
3862 rw_enter(&dp->dp_config_rwlock, RW_READER);
3863 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, FTAG, &dsfrom);
3864 rw_exit(&dp->dp_config_rwlock);
3865 spa_close(spa, FTAG);
3867 dsl_dataset_rele(ds, FTAG);
3870 error = dmu_objset_from_ds(dsfrom, &fromsnap);
3872 dsl_dataset_rele(dsfrom, FTAG);
3873 dsl_dataset_rele(ds, FTAG);
3877 spa_close(spa, FTAG);
3881 error = dmu_send_estimate(tosnap, fromsnap, zc->zc_obj,
3882 &zc->zc_objset_type);
3884 file_t *fp = getf(zc->zc_cookie);
3886 dsl_dataset_rele(ds, FTAG);
3888 dsl_dataset_rele(dsfrom, FTAG);
3893 error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj,
3896 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
3898 releasef(zc->zc_cookie);
3901 dsl_dataset_rele(dsfrom, FTAG);
3902 dsl_dataset_rele(ds, FTAG);
3907 zfs_ioc_inject_fault(zfs_cmd_t *zc)
3911 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
3912 &zc->zc_inject_record);
3915 zc->zc_guid = (uint64_t)id;
3921 zfs_ioc_clear_fault(zfs_cmd_t *zc)
3923 return (zio_clear_fault((int)zc->zc_guid));
3927 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
3929 int id = (int)zc->zc_guid;
3932 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
3933 &zc->zc_inject_record);
3941 zfs_ioc_error_log(zfs_cmd_t *zc)
3945 size_t count = (size_t)zc->zc_nvlist_dst_size;
3947 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
3950 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
3953 zc->zc_nvlist_dst_size = count;
3955 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
3957 spa_close(spa, FTAG);
3963 zfs_ioc_clear(zfs_cmd_t *zc)
3970 * On zpool clear we also fix up missing slogs
3972 mutex_enter(&spa_namespace_lock);
3973 spa = spa_lookup(zc->zc_name);
3975 mutex_exit(&spa_namespace_lock);
3978 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
3979 /* we need to let spa_open/spa_load clear the chains */
3980 spa_set_log_state(spa, SPA_LOG_CLEAR);
3982 spa->spa_last_open_failed = 0;
3983 mutex_exit(&spa_namespace_lock);
3985 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
3986 error = spa_open(zc->zc_name, &spa, FTAG);
3989 nvlist_t *config = NULL;
3991 if (zc->zc_nvlist_src == 0)
3994 if ((error = get_nvlist(zc->zc_nvlist_src,
3995 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
3996 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
3998 if (config != NULL) {
4001 if ((err = put_nvlist(zc, config)) != 0)
4003 nvlist_free(config);
4005 nvlist_free(policy);
4012 spa_vdev_state_enter(spa, SCL_NONE);
4014 if (zc->zc_guid == 0) {
4017 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
4019 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
4020 spa_close(spa, FTAG);
4025 vdev_clear(spa, vd);
4027 (void) spa_vdev_state_exit(spa, NULL, 0);
4030 * Resume any suspended I/Os.
4032 if (zio_resume(spa) != 0)
4035 spa_close(spa, FTAG);
4041 zfs_ioc_pool_reopen(zfs_cmd_t *zc)
4046 error = spa_open(zc->zc_name, &spa, FTAG);
4050 spa_vdev_state_enter(spa, SCL_NONE);
4051 vdev_reopen(spa->spa_root_vdev);
4052 (void) spa_vdev_state_exit(spa, NULL, 0);
4053 spa_close(spa, FTAG);
4058 * zc_name name of filesystem
4059 * zc_value name of origin snapshot
4062 * zc_string name of conflicting snapshot, if there is one
4065 zfs_ioc_promote(zfs_cmd_t *zc)
4070 * We don't need to unmount *all* the origin fs's snapshots, but
4073 cp = strchr(zc->zc_value, '@');
4076 (void) dmu_objset_find(zc->zc_value,
4077 zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS);
4078 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4082 * Retrieve a single {user|group}{used|quota}@... property.
4085 * zc_name name of filesystem
4086 * zc_objset_type zfs_userquota_prop_t
4087 * zc_value domain name (eg. "S-1-234-567-89")
4088 * zc_guid RID/UID/GID
4091 * zc_cookie property value
4094 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4099 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
4102 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4106 error = zfs_userspace_one(zsb,
4107 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
4108 zfs_sb_rele(zsb, FTAG);
4115 * zc_name name of filesystem
4116 * zc_cookie zap cursor
4117 * zc_objset_type zfs_userquota_prop_t
4118 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
4121 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
4122 * zc_cookie zap cursor
4125 zfs_ioc_userspace_many(zfs_cmd_t *zc)
4128 int bufsize = zc->zc_nvlist_dst_size;
4135 error = zfs_sb_hold(zc->zc_name, FTAG, &zsb, B_FALSE);
4139 buf = vmem_alloc(bufsize, KM_SLEEP);
4141 error = zfs_userspace_many(zsb, zc->zc_objset_type, &zc->zc_cookie,
4142 buf, &zc->zc_nvlist_dst_size);
4145 error = xcopyout(buf,
4146 (void *)(uintptr_t)zc->zc_nvlist_dst,
4147 zc->zc_nvlist_dst_size);
4149 vmem_free(buf, bufsize);
4150 zfs_sb_rele(zsb, FTAG);
4157 * zc_name name of filesystem
4163 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
4169 if (get_zfs_sb(zc->zc_name, &zsb) == 0) {
4170 if (!dmu_objset_userused_enabled(zsb->z_os)) {
4172 * If userused is not enabled, it may be because the
4173 * objset needs to be closed & reopened (to grow the
4174 * objset_phys_t). Suspend/resume the fs will do that.
4176 error = zfs_suspend_fs(zsb);
4178 error = zfs_resume_fs(zsb, zc->zc_name);
4181 error = dmu_objset_userspace_upgrade(zsb->z_os);
4182 deactivate_super(zsb->z_sb);
4184 /* XXX kind of reading contents without owning */
4185 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4189 error = dmu_objset_userspace_upgrade(os);
4190 dmu_objset_rele(os, FTAG);
4197 zfs_ioc_share(zfs_cmd_t *zc)
4202 ace_t full_access[] = {
4203 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
4208 * zc_name name of containing filesystem
4209 * zc_obj object # beyond which we want next in-use object #
4212 * zc_obj next in-use object #
4215 zfs_ioc_next_obj(zfs_cmd_t *zc)
4217 objset_t *os = NULL;
4220 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
4224 error = dmu_object_next(os, &zc->zc_obj, B_FALSE,
4225 os->os_dsl_dataset->ds_phys->ds_prev_snap_txg);
4227 dmu_objset_rele(os, FTAG);
4233 * zc_name name of filesystem
4234 * zc_value prefix name for snapshot
4235 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4240 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
4245 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
4246 (u_longlong_t)ddi_get_lbolt64());
4248 if (strlen(snap_name) >= MAXNAMELEN) {
4253 error = dmu_objset_snapshot(zc->zc_name, snap_name, snap_name,
4254 NULL, B_FALSE, B_TRUE, zc->zc_cleanup_fd);
4260 (void) strcpy(zc->zc_value, snap_name);
4267 * zc_name name of "to" snapshot
4268 * zc_value name of "from" snapshot
4269 * zc_cookie file descriptor to write diff data on
4272 * dmu_diff_record_t's to the file descriptor
4275 zfs_ioc_diff(zfs_cmd_t *zc)
4283 error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap);
4287 error = dmu_objset_hold(zc->zc_value, FTAG, &fromsnap);
4289 dmu_objset_rele(tosnap, FTAG);
4293 fp = getf(zc->zc_cookie);
4295 dmu_objset_rele(fromsnap, FTAG);
4296 dmu_objset_rele(tosnap, FTAG);
4302 error = dmu_diff(tosnap, fromsnap, fp->f_vnode, &off);
4304 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4306 releasef(zc->zc_cookie);
4308 dmu_objset_rele(fromsnap, FTAG);
4309 dmu_objset_rele(tosnap, FTAG);
4314 * Remove all ACL files in shares dir
4316 #ifdef HAVE_SMB_SHARE
4318 zfs_smb_acl_purge(znode_t *dzp)
4321 zap_attribute_t zap;
4322 zfs_sb_t *zsb = ZTOZSB(dzp);
4325 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
4326 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
4327 zap_cursor_advance(&zc)) {
4328 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
4332 zap_cursor_fini(&zc);
4335 #endif /* HAVE_SMB_SHARE */
4338 zfs_ioc_smb_acl(zfs_cmd_t *zc)
4340 #ifdef HAVE_SMB_SHARE
4343 vnode_t *resourcevp = NULL;
4352 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
4353 NO_FOLLOW, NULL, &vp)) != 0)
4356 /* Now make sure mntpnt and dataset are ZFS */
4358 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
4359 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
4360 zc->zc_name) != 0)) {
4370 * Create share dir if its missing.
4372 mutex_enter(&zsb->z_lock);
4373 if (zsb->z_shares_dir == 0) {
4376 tx = dmu_tx_create(zsb->z_os);
4377 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
4379 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
4380 error = dmu_tx_assign(tx, TXG_WAIT);
4384 error = zfs_create_share_dir(zsb, tx);
4388 mutex_exit(&zsb->z_lock);
4394 mutex_exit(&zsb->z_lock);
4396 ASSERT(zsb->z_shares_dir);
4397 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &sharedir)) != 0) {
4403 switch (zc->zc_cookie) {
4404 case ZFS_SMB_ACL_ADD:
4405 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
4406 vattr.va_mode = S_IFREG|0777;
4410 vsec.vsa_mask = VSA_ACE;
4411 vsec.vsa_aclentp = &full_access;
4412 vsec.vsa_aclentsz = sizeof (full_access);
4413 vsec.vsa_aclcnt = 1;
4415 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
4416 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
4418 VN_RELE(resourcevp);
4421 case ZFS_SMB_ACL_REMOVE:
4422 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
4426 case ZFS_SMB_ACL_RENAME:
4427 if ((error = get_nvlist(zc->zc_nvlist_src,
4428 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
4433 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
4434 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
4437 VN_RELE(ZTOV(sharedir));
4439 nvlist_free(nvlist);
4442 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
4444 nvlist_free(nvlist);
4447 case ZFS_SMB_ACL_PURGE:
4448 error = zfs_smb_acl_purge(sharedir);
4457 VN_RELE(ZTOV(sharedir));
4464 #endif /* HAVE_SMB_SHARE */
4469 * zc_name name of filesystem
4470 * zc_value short name of snap
4471 * zc_string user-supplied tag for this hold
4472 * zc_cookie recursive flag
4473 * zc_temphold set if hold is temporary
4474 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
4475 * zc_sendobj if non-zero, the objid for zc_name@zc_value
4476 * zc_createtxg if zc_sendobj is non-zero, snap must have zc_createtxg
4481 zfs_ioc_hold(zfs_cmd_t *zc)
4483 boolean_t recursive = zc->zc_cookie;
4490 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4493 if (zc->zc_sendobj == 0) {
4494 return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value,
4495 zc->zc_string, recursive, zc->zc_temphold,
4496 zc->zc_cleanup_fd));
4502 error = spa_open(zc->zc_name, &spa, FTAG);
4506 dp = spa_get_dsl(spa);
4507 rw_enter(&dp->dp_config_rwlock, RW_READER);
4508 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
4509 rw_exit(&dp->dp_config_rwlock);
4510 spa_close(spa, FTAG);
4515 * Until we have a hold on this snapshot, it's possible that
4516 * zc_sendobj could've been destroyed and reused as part
4517 * of a later txg. Make sure we're looking at the right object.
4519 if (zc->zc_createtxg != ds->ds_phys->ds_creation_txg) {
4520 dsl_dataset_rele(ds, FTAG);
4524 if (zc->zc_cleanup_fd != -1 && zc->zc_temphold) {
4525 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
4527 dsl_dataset_rele(ds, FTAG);
4532 error = dsl_dataset_user_hold_for_send(ds, zc->zc_string,
4536 dsl_register_onexit_hold_cleanup(ds, zc->zc_string,
4539 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
4541 dsl_dataset_rele(ds, FTAG);
4548 * zc_name name of dataset from which we're releasing a user hold
4549 * zc_value short name of snap
4550 * zc_string user-supplied tag for this hold
4551 * zc_cookie recursive flag
4556 zfs_ioc_release(zfs_cmd_t *zc)
4558 boolean_t recursive = zc->zc_cookie;
4560 if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0)
4563 return (dsl_dataset_user_release(zc->zc_name, zc->zc_value,
4564 zc->zc_string, recursive));
4569 * zc_name name of filesystem
4572 * zc_nvlist_src{_size} nvlist of snapshot holds
4575 zfs_ioc_get_holds(zfs_cmd_t *zc)
4580 if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) {
4581 error = put_nvlist(zc, nvp);
4590 * zc_guid flags (ZEVENT_NONBLOCK)
4593 * zc_nvlist_dst next nvlist event
4594 * zc_cookie dropped events since last get
4595 * zc_cleanup_fd cleanup-on-exit file descriptor
4598 zfs_ioc_events_next(zfs_cmd_t *zc)
4601 nvlist_t *event = NULL;
4603 uint64_t dropped = 0;
4606 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
4611 error = zfs_zevent_next(ze, &event,
4612 &zc->zc_nvlist_dst_size, &dropped);
4613 if (event != NULL) {
4614 zc->zc_cookie = dropped;
4615 error = put_nvlist(zc, event);
4619 if (zc->zc_guid & ZEVENT_NONBLOCK)
4622 if ((error == 0) || (error != ENOENT))
4625 error = zfs_zevent_wait(ze);
4630 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
4637 * zc_cookie cleared events count
4640 zfs_ioc_events_clear(zfs_cmd_t *zc)
4644 zfs_zevent_drain_all(&count);
4645 zc->zc_cookie = count;
4652 * zc_name name of new filesystem or snapshot
4653 * zc_value full name of old snapshot
4656 * zc_cookie space in bytes
4657 * zc_objset_type compressed space in bytes
4658 * zc_perm_action uncompressed space in bytes
4661 zfs_ioc_space_written(zfs_cmd_t *zc)
4664 dsl_dataset_t *new, *old;
4666 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4669 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4671 dsl_dataset_rele(new, FTAG);
4675 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
4676 &zc->zc_objset_type, &zc->zc_perm_action);
4677 dsl_dataset_rele(old, FTAG);
4678 dsl_dataset_rele(new, FTAG);
4684 * zc_name full name of last snapshot
4685 * zc_value full name of first snapshot
4688 * zc_cookie space in bytes
4689 * zc_objset_type compressed space in bytes
4690 * zc_perm_action uncompressed space in bytes
4693 zfs_ioc_space_snaps(zfs_cmd_t *zc)
4696 dsl_dataset_t *new, *old;
4698 error = dsl_dataset_hold(zc->zc_name, FTAG, &new);
4701 error = dsl_dataset_hold(zc->zc_value, FTAG, &old);
4703 dsl_dataset_rele(new, FTAG);
4707 error = dsl_dataset_space_wouldfree(old, new, &zc->zc_cookie,
4708 &zc->zc_objset_type, &zc->zc_perm_action);
4709 dsl_dataset_rele(old, FTAG);
4710 dsl_dataset_rele(new, FTAG);
4715 * pool create, destroy, and export don't log the history as part of
4716 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export
4717 * do the logging of those commands.
4719 static zfs_ioc_vec_t zfs_ioc_vec[] = {
4720 { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4722 { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4724 { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4726 { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4728 { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE,
4730 { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4732 { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE,
4734 { zfs_ioc_pool_scan, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4735 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4736 { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE,
4737 POOL_CHECK_READONLY },
4738 { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4739 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4740 { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4742 { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4743 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4744 { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4745 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4746 { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4747 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4748 { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4749 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4750 { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4751 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4752 { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4753 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4754 { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE,
4755 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4756 { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4757 POOL_CHECK_SUSPENDED },
4758 { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4760 { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4761 POOL_CHECK_SUSPENDED },
4762 { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4763 POOL_CHECK_SUSPENDED },
4764 { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE,
4765 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4766 { zfs_ioc_create_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4768 { zfs_ioc_remove_minor, zfs_secpolicy_config, DATASET_NAME, B_FALSE,
4770 { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE,
4771 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4772 { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE,
4773 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4774 { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE,
4775 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4776 { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE,
4777 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4778 { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE,
4779 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4780 { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_FALSE,
4782 { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4784 { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4786 { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE,
4788 { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE,
4790 { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4792 { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE,
4793 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4794 { zfs_ioc_destroy_snaps_nvl, zfs_secpolicy_destroy_recursive,
4795 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4796 { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE,
4797 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4798 { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_diff, POOL_NAME, B_FALSE,
4800 { zfs_ioc_obj_to_path, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4801 POOL_CHECK_SUSPENDED },
4802 { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4803 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4804 { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE,
4806 { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE,
4807 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4808 { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4810 { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE,
4812 { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE,
4813 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4814 { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE,
4816 { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, DATASET_NAME,
4817 B_FALSE, POOL_CHECK_NONE },
4818 { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, DATASET_NAME,
4819 B_FALSE, POOL_CHECK_NONE },
4820 { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
4821 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4822 { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE,
4823 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4824 { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE,
4825 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4826 { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4827 POOL_CHECK_SUSPENDED },
4828 { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4830 { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4831 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4832 { zfs_ioc_next_obj, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4834 { zfs_ioc_diff, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4836 { zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, DATASET_NAME,
4837 B_FALSE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4838 { zfs_ioc_obj_to_stats, zfs_secpolicy_diff, DATASET_NAME, B_FALSE,
4839 POOL_CHECK_SUSPENDED },
4840 { zfs_ioc_events_next, zfs_secpolicy_config, NO_NAME, B_FALSE,
4842 { zfs_ioc_events_clear, zfs_secpolicy_config, NO_NAME, B_FALSE,
4844 { zfs_ioc_pool_reguid, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4845 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY },
4846 { zfs_ioc_space_written, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4847 POOL_CHECK_SUSPENDED },
4848 { zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, B_FALSE,
4849 POOL_CHECK_SUSPENDED },
4850 { zfs_ioc_pool_reopen, zfs_secpolicy_config, POOL_NAME, B_TRUE,
4851 POOL_CHECK_SUSPENDED },
4855 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
4856 zfs_ioc_poolcheck_t check)
4861 ASSERT(type == POOL_NAME || type == DATASET_NAME);
4863 if (check & POOL_CHECK_NONE)
4866 error = spa_open(name, &spa, FTAG);
4868 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
4870 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
4872 spa_close(spa, FTAG);
4878 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
4882 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4884 for (zs = list_head(&zfsdev_state_list); zs != NULL;
4885 zs = list_next(&zfsdev_state_list, zs)) {
4886 if (zs->zs_minor == minor) {
4888 case ZST_ONEXIT: return (zs->zs_onexit);
4889 case ZST_ZEVENT: return (zs->zs_zevent);
4890 case ZST_ALL: return (zs);
4899 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
4903 mutex_enter(&zfsdev_state_lock);
4904 ptr = zfsdev_get_state_impl(minor, which);
4905 mutex_exit(&zfsdev_state_lock);
4911 zfsdev_getminor(struct file *filp)
4913 ASSERT(filp != NULL);
4914 ASSERT(filp->private_data != NULL);
4916 return (((zfsdev_state_t *)filp->private_data)->zs_minor);
4920 * Find a free minor number. The zfsdev_state_list is expected to
4921 * be short since it is only a list of currently open file handles.
4924 zfsdev_minor_alloc(void)
4926 static minor_t last_minor = 0;
4929 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4931 for (m = last_minor + 1; m != last_minor; m++) {
4932 if (m > ZFSDEV_MAX_MINOR)
4934 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
4944 zfsdev_state_init(struct file *filp)
4949 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4951 minor = zfsdev_minor_alloc();
4955 zs = kmem_zalloc( sizeof(zfsdev_state_t), KM_SLEEP);
4960 zs->zs_minor = minor;
4961 filp->private_data = zs;
4963 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
4964 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
4966 list_insert_tail(&zfsdev_state_list, zs);
4972 zfsdev_state_destroy(struct file *filp)
4976 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
4977 ASSERT(filp->private_data != NULL);
4979 zs = filp->private_data;
4980 zfs_onexit_destroy(zs->zs_onexit);
4981 zfs_zevent_destroy(zs->zs_zevent);
4983 list_remove(&zfsdev_state_list, zs);
4984 kmem_free(zs, sizeof(zfsdev_state_t));
4990 zfsdev_open(struct inode *ino, struct file *filp)
4994 mutex_enter(&zfsdev_state_lock);
4995 error = zfsdev_state_init(filp);
4996 mutex_exit(&zfsdev_state_lock);
5002 zfsdev_release(struct inode *ino, struct file *filp)
5006 mutex_enter(&zfsdev_state_lock);
5007 error = zfsdev_state_destroy(filp);
5008 mutex_exit(&zfsdev_state_lock);
5014 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5018 int error, rc, flag = 0;
5020 vec = cmd - ZFS_IOC;
5021 if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
5024 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP | KM_NODEBUG);
5026 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
5030 if ((error == 0) && !(flag & FKIOCTL))
5031 error = zfs_ioc_vec[vec].zvec_secpolicy(zc, CRED());
5034 * Ensure that all pool/dataset names are valid before we pass down to
5038 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5039 zc->zc_iflags = flag & FKIOCTL;
5040 switch (zfs_ioc_vec[vec].zvec_namecheck) {
5042 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
5044 error = pool_status_check(zc->zc_name,
5045 zfs_ioc_vec[vec].zvec_namecheck,
5046 zfs_ioc_vec[vec].zvec_pool_check);
5050 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
5052 error = pool_status_check(zc->zc_name,
5053 zfs_ioc_vec[vec].zvec_namecheck,
5054 zfs_ioc_vec[vec].zvec_pool_check);
5063 error = zfs_ioc_vec[vec].zvec_func(zc);
5065 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
5069 if (zfs_ioc_vec[vec].zvec_his_log)
5070 zfs_log_history(zc);
5073 kmem_free(zc, sizeof (zfs_cmd_t));
5077 #ifdef CONFIG_COMPAT
5079 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
5081 return zfsdev_ioctl(filp, cmd, arg);
5084 #define zfsdev_compat_ioctl NULL
5087 static const struct file_operations zfsdev_fops = {
5088 .open = zfsdev_open,
5089 .release = zfsdev_release,
5090 .unlocked_ioctl = zfsdev_ioctl,
5091 .compat_ioctl = zfsdev_compat_ioctl,
5092 .owner = THIS_MODULE,
5095 static struct miscdevice zfs_misc = {
5096 .minor = MISC_DYNAMIC_MINOR,
5098 .fops = &zfsdev_fops,
5106 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
5107 list_create(&zfsdev_state_list, sizeof (zfsdev_state_t),
5108 offsetof(zfsdev_state_t, zs_next));
5110 error = misc_register(&zfs_misc);
5112 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
5124 error = misc_deregister(&zfs_misc);
5126 printk(KERN_INFO "ZFS: misc_deregister() failed %d\n", error);
5128 mutex_destroy(&zfsdev_state_lock);
5129 list_destroy(&zfsdev_state_list);
5132 uint_t zfs_fsyncer_key;
5133 extern uint_t rrw_tsd_key;
5136 #define ZFS_DEBUG_STR " (DEBUG mode)"
5138 #define ZFS_DEBUG_STR ""
5146 spa_init(FREAD | FWRITE);
5149 if ((error = zvol_init()) != 0)
5152 if ((error = zfs_attach()) != 0)
5155 tsd_create(&zfs_fsyncer_key, NULL);
5156 tsd_create(&rrw_tsd_key, NULL);
5158 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
5159 "ZFS pool version %s, ZFS filesystem version %s\n",
5160 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
5161 SPA_VERSION_STRING, ZPL_VERSION_STRING);
5170 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
5171 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
5172 ZFS_DEBUG_STR, error);
5185 tsd_destroy(&zfs_fsyncer_key);
5186 tsd_destroy(&rrw_tsd_key);
5188 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
5189 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
5195 spl_module_init(_init);
5196 spl_module_exit(_fini);
5198 MODULE_DESCRIPTION("ZFS");
5199 MODULE_AUTHOR(ZFS_META_AUTHOR);
5200 MODULE_LICENSE(ZFS_META_LICENSE);
5201 #endif /* HAVE_SPL */