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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright 2017 RackTop Systems.
38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
39 * Copyright (c) 2019 Datto Inc.
40 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
41 * Copyright (c) 2019, Klara Inc.
42 * Copyright (c) 2019, Allan Jude
48 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
49 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
51 * There are two ways that we handle ioctls: the legacy way where almost
52 * all of the logic is in the ioctl callback, and the new way where most
53 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
55 * Non-legacy ioctls should be registered by calling
56 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
57 * from userland by lzc_ioctl().
59 * The registration arguments are as follows:
62 * The name of the ioctl. This is used for history logging. If the
63 * ioctl returns successfully (the callback returns 0), and allow_log
64 * is true, then a history log entry will be recorded with the input &
65 * output nvlists. The log entry can be printed with "zpool history -i".
68 * The ioctl request number, which userland will pass to ioctl(2).
69 * We want newer versions of libzfs and libzfs_core to run against
70 * existing zfs kernel modules (i.e. a deferred reboot after an update).
71 * Therefore the ioctl numbers cannot change from release to release.
73 * zfs_secpolicy_func_t *secpolicy
74 * This function will be called before the zfs_ioc_func_t, to
75 * determine if this operation is permitted. It should return EPERM
76 * on failure, and 0 on success. Checks include determining if the
77 * dataset is visible in this zone, and if the user has either all
78 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
79 * to do this operation on this dataset with "zfs allow".
81 * zfs_ioc_namecheck_t namecheck
82 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
83 * name, a dataset name, or nothing. If the name is not well-formed,
84 * the ioctl will fail and the callback will not be called.
85 * Therefore, the callback can assume that the name is well-formed
86 * (e.g. is null-terminated, doesn't have more than one '@' character,
87 * doesn't have invalid characters).
89 * zfs_ioc_poolcheck_t pool_check
90 * This specifies requirements on the pool state. If the pool does
91 * not meet them (is suspended or is readonly), the ioctl will fail
92 * and the callback will not be called. If any checks are specified
93 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
94 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
95 * POOL_CHECK_READONLY).
97 * zfs_ioc_key_t *nvl_keys
98 * The list of expected/allowable innvl input keys. This list is used
99 * to validate the nvlist input to the ioctl.
101 * boolean_t smush_outnvlist
102 * If smush_outnvlist is true, then the output is presumed to be a
103 * list of errors, and it will be "smushed" down to fit into the
104 * caller's buffer, by removing some entries and replacing them with a
105 * single "N_MORE_ERRORS" entry indicating how many were removed. See
106 * nvlist_smush() for details. If smush_outnvlist is false, and the
107 * outnvlist does not fit into the userland-provided buffer, then the
108 * ioctl will fail with ENOMEM.
110 * zfs_ioc_func_t *func
111 * The callback function that will perform the operation.
113 * The callback should return 0 on success, or an error number on
114 * failure. If the function fails, the userland ioctl will return -1,
115 * and errno will be set to the callback's return value. The callback
116 * will be called with the following arguments:
119 * The name of the pool or dataset to operate on, from
120 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
121 * expected type (pool, dataset, or none).
124 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
125 * NULL if no input nvlist was provided. Changes to this nvlist are
126 * ignored. If the input nvlist could not be deserialized, the
127 * ioctl will fail and the callback will not be called.
130 * The output nvlist, initially empty. The callback can fill it in,
131 * and it will be returned to userland by serializing it into
132 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
133 * fails (e.g. because the caller didn't supply a large enough
134 * buffer), then the overall ioctl will fail. See the
135 * 'smush_nvlist' argument above for additional behaviors.
137 * There are two typical uses of the output nvlist:
138 * - To return state, e.g. property values. In this case,
139 * smush_outnvlist should be false. If the buffer was not large
140 * enough, the caller will reallocate a larger buffer and try
143 * - To return multiple errors from an ioctl which makes on-disk
144 * changes. In this case, smush_outnvlist should be true.
145 * Ioctls which make on-disk modifications should generally not
146 * use the outnvl if they succeed, because the caller can not
147 * distinguish between the operation failing, and
148 * deserialization failing.
150 * IOCTL Interface Errors
152 * The following ioctl input errors can be returned:
153 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
154 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
155 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
156 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
159 #include <sys/types.h>
160 #include <sys/param.h>
161 #include <sys/errno.h>
163 #include <sys/file.h>
164 #include <sys/kmem.h>
165 #include <sys/cmn_err.h>
166 #include <sys/stat.h>
167 #include <sys/zfs_ioctl.h>
168 #include <sys/zfs_quota.h>
169 #include <sys/zfs_vfsops.h>
170 #include <sys/zfs_znode.h>
173 #include <sys/spa_impl.h>
174 #include <sys/vdev.h>
175 #include <sys/vdev_impl.h>
177 #include <sys/dsl_dir.h>
178 #include <sys/dsl_dataset.h>
179 #include <sys/dsl_prop.h>
180 #include <sys/dsl_deleg.h>
181 #include <sys/dmu_objset.h>
182 #include <sys/dmu_impl.h>
183 #include <sys/dmu_redact.h>
184 #include <sys/dmu_tx.h>
185 #include <sys/sunddi.h>
186 #include <sys/policy.h>
187 #include <sys/zone.h>
188 #include <sys/nvpair.h>
189 #include <sys/pathname.h>
190 #include <sys/fs/zfs.h>
191 #include <sys/zfs_ctldir.h>
192 #include <sys/zfs_dir.h>
193 #include <sys/zfs_onexit.h>
194 #include <sys/zvol.h>
195 #include <sys/dsl_scan.h>
196 #include <sys/fm/util.h>
197 #include <sys/dsl_crypt.h>
198 #include <sys/rrwlock.h>
199 #include <sys/zfs_file.h>
201 #include <sys/dmu_recv.h>
202 #include <sys/dmu_send.h>
203 #include <sys/dmu_recv.h>
204 #include <sys/dsl_destroy.h>
205 #include <sys/dsl_bookmark.h>
206 #include <sys/dsl_userhold.h>
207 #include <sys/zfeature.h>
209 #include <sys/zio_checksum.h>
210 #include <sys/vdev_removal.h>
211 #include <sys/vdev_impl.h>
212 #include <sys/vdev_initialize.h>
213 #include <sys/vdev_trim.h>
215 #include "zfs_namecheck.h"
216 #include "zfs_prop.h"
217 #include "zfs_deleg.h"
218 #include "zfs_comutil.h"
220 #include <sys/lua/lua.h>
221 #include <sys/lua/lauxlib.h>
222 #include <sys/zfs_ioctl_impl.h>
224 kmutex_t zfsdev_state_lock;
225 zfsdev_state_t *zfsdev_state_list;
228 * Limit maximum nvlist size. We don't want users passing in insane values
229 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
230 * Defaults to 0=auto which is handled by platform code.
232 unsigned long zfs_max_nvlist_src_size = 0;
234 uint_t zfs_fsyncer_key;
235 uint_t zfs_allow_log_key;
237 /* DATA_TYPE_ANY is used when zkey_type can vary. */
238 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
240 typedef struct zfs_ioc_vec {
241 zfs_ioc_legacy_func_t *zvec_legacy_func;
242 zfs_ioc_func_t *zvec_func;
243 zfs_secpolicy_func_t *zvec_secpolicy;
244 zfs_ioc_namecheck_t zvec_namecheck;
245 boolean_t zvec_allow_log;
246 zfs_ioc_poolcheck_t zvec_pool_check;
247 boolean_t zvec_smush_outnvlist;
248 const char *zvec_name;
249 const zfs_ioc_key_t *zvec_nvl_keys;
250 size_t zvec_nvl_key_count;
253 /* This array is indexed by zfs_userquota_prop_t */
254 static const char *userquota_perms[] = {
255 ZFS_DELEG_PERM_USERUSED,
256 ZFS_DELEG_PERM_USERQUOTA,
257 ZFS_DELEG_PERM_GROUPUSED,
258 ZFS_DELEG_PERM_GROUPQUOTA,
259 ZFS_DELEG_PERM_USEROBJUSED,
260 ZFS_DELEG_PERM_USEROBJQUOTA,
261 ZFS_DELEG_PERM_GROUPOBJUSED,
262 ZFS_DELEG_PERM_GROUPOBJQUOTA,
263 ZFS_DELEG_PERM_PROJECTUSED,
264 ZFS_DELEG_PERM_PROJECTQUOTA,
265 ZFS_DELEG_PERM_PROJECTOBJUSED,
266 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
269 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
270 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
271 static int zfs_check_settable(const char *name, nvpair_t *property,
273 static int zfs_check_clearable(const char *dataset, nvlist_t *props,
275 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
277 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
278 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
281 history_str_free(char *buf)
283 kmem_free(buf, HIS_MAX_RECORD_LEN);
287 history_str_get(zfs_cmd_t *zc)
291 if (zc->zc_history == 0)
294 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
295 if (copyinstr((void *)(uintptr_t)zc->zc_history,
296 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
297 history_str_free(buf);
301 buf[HIS_MAX_RECORD_LEN -1] = '\0';
307 * Return non-zero if the spa version is less than requested version.
310 zfs_earlier_version(const char *name, int version)
314 if (spa_open(name, &spa, FTAG) == 0) {
315 if (spa_version(spa) < version) {
316 spa_close(spa, FTAG);
319 spa_close(spa, FTAG);
325 * Return TRUE if the ZPL version is less than requested version.
328 zpl_earlier_version(const char *name, int version)
331 boolean_t rc = B_TRUE;
333 if (dmu_objset_hold(name, FTAG, &os) == 0) {
336 if (dmu_objset_type(os) != DMU_OST_ZFS) {
337 dmu_objset_rele(os, FTAG);
340 /* XXX reading from non-owned objset */
341 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
342 rc = zplversion < version;
343 dmu_objset_rele(os, FTAG);
349 zfs_log_history(zfs_cmd_t *zc)
354 if ((buf = history_str_get(zc)) == NULL)
357 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
358 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
359 (void) spa_history_log(spa, buf);
360 spa_close(spa, FTAG);
362 history_str_free(buf);
366 * Policy for top-level read operations (list pools). Requires no privileges,
367 * and can be used in the local zone, as there is no associated dataset.
371 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
377 * Policy for dataset read operations (list children, get statistics). Requires
378 * no privileges, but must be visible in the local zone.
382 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
384 if (INGLOBALZONE(curproc) ||
385 zone_dataset_visible(zc->zc_name, NULL))
388 return (SET_ERROR(ENOENT));
392 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
397 * The dataset must be visible by this zone -- check this first
398 * so they don't see EPERM on something they shouldn't know about.
400 if (!INGLOBALZONE(curproc) &&
401 !zone_dataset_visible(dataset, &writable))
402 return (SET_ERROR(ENOENT));
404 if (INGLOBALZONE(curproc)) {
406 * If the fs is zoned, only root can access it from the
409 if (secpolicy_zfs(cr) && zoned)
410 return (SET_ERROR(EPERM));
413 * If we are in a local zone, the 'zoned' property must be set.
416 return (SET_ERROR(EPERM));
418 /* must be writable by this zone */
420 return (SET_ERROR(EPERM));
426 zfs_dozonecheck(const char *dataset, cred_t *cr)
430 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED),
432 return (SET_ERROR(ENOENT));
434 return (zfs_dozonecheck_impl(dataset, zoned, cr));
438 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
442 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned))
443 return (SET_ERROR(ENOENT));
445 return (zfs_dozonecheck_impl(dataset, zoned, cr));
449 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
450 const char *perm, cred_t *cr)
454 error = zfs_dozonecheck_ds(name, ds, cr);
456 error = secpolicy_zfs(cr);
458 error = dsl_deleg_access_impl(ds, perm, cr);
464 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
471 * First do a quick check for root in the global zone, which
472 * is allowed to do all write_perms. This ensures that zfs_ioc_*
473 * will get to handle nonexistent datasets.
475 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
478 error = dsl_pool_hold(name, FTAG, &dp);
482 error = dsl_dataset_hold(dp, name, FTAG, &ds);
484 dsl_pool_rele(dp, FTAG);
488 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
490 dsl_dataset_rele(ds, FTAG);
491 dsl_pool_rele(dp, FTAG);
496 * Policy for setting the security label property.
498 * Returns 0 for success, non-zero for access and other errors.
501 zfs_set_slabel_policy(const char *name, const char *strval, cred_t *cr)
504 char ds_hexsl[MAXNAMELEN];
505 bslabel_t ds_sl, new_sl;
506 boolean_t new_default = FALSE;
508 int needed_priv = -1;
511 /* First get the existing dataset label. */
512 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
513 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
515 return (SET_ERROR(EPERM));
517 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
520 /* The label must be translatable */
521 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
522 return (SET_ERROR(EINVAL));
525 * In a non-global zone, disallow attempts to set a label that
526 * doesn't match that of the zone; otherwise no other checks
529 if (!INGLOBALZONE(curproc)) {
530 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
531 return (SET_ERROR(EPERM));
536 * For global-zone datasets (i.e., those whose zoned property is
537 * "off", verify that the specified new label is valid for the
540 if (dsl_prop_get_integer(name,
541 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
542 return (SET_ERROR(EPERM));
544 if (zfs_check_global_label(name, strval) != 0)
545 return (SET_ERROR(EPERM));
549 * If the existing dataset label is nondefault, check if the
550 * dataset is mounted (label cannot be changed while mounted).
551 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
552 * mounted (or isn't a dataset, doesn't exist, ...).
554 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
556 static const char *setsl_tag = "setsl_tag";
559 * Try to own the dataset; abort if there is any error,
560 * (e.g., already mounted, in use, or other error).
562 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
565 return (SET_ERROR(EPERM));
567 dmu_objset_disown(os, B_TRUE, setsl_tag);
570 needed_priv = PRIV_FILE_DOWNGRADE_SL;
574 if (hexstr_to_label(strval, &new_sl) != 0)
575 return (SET_ERROR(EPERM));
577 if (blstrictdom(&ds_sl, &new_sl))
578 needed_priv = PRIV_FILE_DOWNGRADE_SL;
579 else if (blstrictdom(&new_sl, &ds_sl))
580 needed_priv = PRIV_FILE_UPGRADE_SL;
582 /* dataset currently has a default label */
584 needed_priv = PRIV_FILE_UPGRADE_SL;
588 if (needed_priv != -1)
589 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
592 return (SET_ERROR(ENOTSUP));
593 #endif /* HAVE_MLSLABEL */
597 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
603 * Check permissions for special properties.
610 * Disallow setting of 'zoned' from within a local zone.
612 if (!INGLOBALZONE(curproc))
613 return (SET_ERROR(EPERM));
617 case ZFS_PROP_FILESYSTEM_LIMIT:
618 case ZFS_PROP_SNAPSHOT_LIMIT:
619 if (!INGLOBALZONE(curproc)) {
621 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
623 * Unprivileged users are allowed to modify the
624 * limit on things *under* (ie. contained by)
625 * the thing they own.
627 if (dsl_prop_get_integer(dsname,
628 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint))
629 return (SET_ERROR(EPERM));
630 if (!zoned || strlen(dsname) <= strlen(setpoint))
631 return (SET_ERROR(EPERM));
635 case ZFS_PROP_MLSLABEL:
636 if (!is_system_labeled())
637 return (SET_ERROR(EPERM));
639 if (nvpair_value_string(propval, &strval) == 0) {
642 err = zfs_set_slabel_policy(dsname, strval, CRED());
649 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
654 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
658 error = zfs_dozonecheck(zc->zc_name, cr);
663 * permission to set permissions will be evaluated later in
664 * dsl_deleg_can_allow()
671 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
673 return (zfs_secpolicy_write_perms(zc->zc_name,
674 ZFS_DELEG_PERM_ROLLBACK, cr));
679 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
687 * Generate the current snapshot name from the given objsetid, then
688 * use that name for the secpolicy/zone checks.
690 cp = strchr(zc->zc_name, '@');
692 return (SET_ERROR(EINVAL));
693 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
697 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
699 dsl_pool_rele(dp, FTAG);
703 dsl_dataset_name(ds, zc->zc_name);
705 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
706 ZFS_DELEG_PERM_SEND, cr);
707 dsl_dataset_rele(ds, FTAG);
708 dsl_pool_rele(dp, FTAG);
715 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
717 return (zfs_secpolicy_write_perms(zc->zc_name,
718 ZFS_DELEG_PERM_SEND, cr));
722 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
724 return (SET_ERROR(ENOTSUP));
728 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
730 return (SET_ERROR(ENOTSUP));
734 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
739 * Remove the @bla or /bla from the end of the name to get the parent.
741 (void) strncpy(parent, datasetname, parentsize);
742 cp = strrchr(parent, '@');
746 cp = strrchr(parent, '/');
748 return (SET_ERROR(ENOENT));
756 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
760 if ((error = zfs_secpolicy_write_perms(name,
761 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
764 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
769 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
771 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
775 * Destroying snapshots with delegated permissions requires
776 * descendant mount and destroy permissions.
780 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
783 nvpair_t *pair, *nextpair;
786 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
788 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
790 nextpair = nvlist_next_nvpair(snaps, pair);
791 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
792 if (error == ENOENT) {
794 * Ignore any snapshots that don't exist (we consider
795 * them "already destroyed"). Remove the name from the
796 * nvl here in case the snapshot is created between
797 * now and when we try to destroy it (in which case
798 * we don't want to destroy it since we haven't
799 * checked for permission).
801 fnvlist_remove_nvpair(snaps, pair);
812 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
814 char parentname[ZFS_MAX_DATASET_NAME_LEN];
817 if ((error = zfs_secpolicy_write_perms(from,
818 ZFS_DELEG_PERM_RENAME, cr)) != 0)
821 if ((error = zfs_secpolicy_write_perms(from,
822 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
825 if ((error = zfs_get_parent(to, parentname,
826 sizeof (parentname))) != 0)
829 if ((error = zfs_secpolicy_write_perms(parentname,
830 ZFS_DELEG_PERM_CREATE, cr)) != 0)
833 if ((error = zfs_secpolicy_write_perms(parentname,
834 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
842 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
844 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
849 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
852 dsl_dataset_t *clone;
855 error = zfs_secpolicy_write_perms(zc->zc_name,
856 ZFS_DELEG_PERM_PROMOTE, cr);
860 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
864 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
867 char parentname[ZFS_MAX_DATASET_NAME_LEN];
868 dsl_dataset_t *origin = NULL;
872 error = dsl_dataset_hold_obj(dd->dd_pool,
873 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
875 dsl_dataset_rele(clone, FTAG);
876 dsl_pool_rele(dp, FTAG);
880 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
881 ZFS_DELEG_PERM_MOUNT, cr);
883 dsl_dataset_name(origin, parentname);
885 error = zfs_secpolicy_write_perms_ds(parentname, origin,
886 ZFS_DELEG_PERM_PROMOTE, cr);
888 dsl_dataset_rele(clone, FTAG);
889 dsl_dataset_rele(origin, FTAG);
891 dsl_pool_rele(dp, FTAG);
897 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
901 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
902 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
905 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
906 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
909 return (zfs_secpolicy_write_perms(zc->zc_name,
910 ZFS_DELEG_PERM_CREATE, cr));
915 zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
917 return (zfs_secpolicy_recv(zc, innvl, cr));
921 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
923 return (zfs_secpolicy_write_perms(name,
924 ZFS_DELEG_PERM_SNAPSHOT, cr));
928 * Check for permission to create each snapshot in the nvlist.
932 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
938 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
940 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
941 pair = nvlist_next_nvpair(snaps, pair)) {
942 char *name = nvpair_name(pair);
943 char *atp = strchr(name, '@');
946 error = SET_ERROR(EINVAL);
950 error = zfs_secpolicy_snapshot_perms(name, cr);
959 * Check for permission to create each bookmark in the nvlist.
963 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
967 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
968 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
969 char *name = nvpair_name(pair);
970 char *hashp = strchr(name, '#');
973 error = SET_ERROR(EINVAL);
977 error = zfs_secpolicy_write_perms(name,
978 ZFS_DELEG_PERM_BOOKMARK, cr);
988 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
990 nvpair_t *pair, *nextpair;
993 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
995 char *name = nvpair_name(pair);
996 char *hashp = strchr(name, '#');
997 nextpair = nvlist_next_nvpair(innvl, pair);
1000 error = SET_ERROR(EINVAL);
1005 error = zfs_secpolicy_write_perms(name,
1006 ZFS_DELEG_PERM_DESTROY, cr);
1008 if (error == ENOENT) {
1010 * Ignore any filesystems that don't exist (we consider
1011 * their bookmarks "already destroyed"). Remove
1012 * the name from the nvl here in case the filesystem
1013 * is created between now and when we try to destroy
1014 * the bookmark (in which case we don't want to
1015 * destroy it since we haven't checked for permission).
1017 fnvlist_remove_nvpair(innvl, pair);
1029 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1032 * Even root must have a proper TSD so that we know what pool
1035 if (tsd_get(zfs_allow_log_key) == NULL)
1036 return (SET_ERROR(EPERM));
1041 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1043 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1047 if ((error = zfs_get_parent(zc->zc_name, parentname,
1048 sizeof (parentname))) != 0)
1051 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1052 (error = zfs_secpolicy_write_perms(origin,
1053 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1056 if ((error = zfs_secpolicy_write_perms(parentname,
1057 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1060 return (zfs_secpolicy_write_perms(parentname,
1061 ZFS_DELEG_PERM_MOUNT, cr));
1065 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1066 * SYS_CONFIG privilege, which is not available in a local zone.
1070 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1072 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1073 return (SET_ERROR(EPERM));
1079 * Policy for object to name lookups.
1083 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1087 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1090 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1095 * Policy for fault injection. Requires all privileges.
1099 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1101 return (secpolicy_zinject(cr));
1106 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1108 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1110 if (prop == ZPROP_INVAL) {
1111 if (!zfs_prop_user(zc->zc_value))
1112 return (SET_ERROR(EINVAL));
1113 return (zfs_secpolicy_write_perms(zc->zc_name,
1114 ZFS_DELEG_PERM_USERPROP, cr));
1116 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1122 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1124 int err = zfs_secpolicy_read(zc, innvl, cr);
1128 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1129 return (SET_ERROR(EINVAL));
1131 if (zc->zc_value[0] == 0) {
1133 * They are asking about a posix uid/gid. If it's
1134 * themself, allow it.
1136 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1137 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1138 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1139 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1140 if (zc->zc_guid == crgetuid(cr))
1142 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1143 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1144 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1145 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1146 if (groupmember(zc->zc_guid, cr))
1149 /* else is for project quota/used */
1152 return (zfs_secpolicy_write_perms(zc->zc_name,
1153 userquota_perms[zc->zc_objset_type], cr));
1157 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1159 int err = zfs_secpolicy_read(zc, innvl, cr);
1163 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1164 return (SET_ERROR(EINVAL));
1166 return (zfs_secpolicy_write_perms(zc->zc_name,
1167 userquota_perms[zc->zc_objset_type], cr));
1172 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1174 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1180 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1186 holds = fnvlist_lookup_nvlist(innvl, "holds");
1188 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1189 pair = nvlist_next_nvpair(holds, pair)) {
1190 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1191 error = dmu_fsname(nvpair_name(pair), fsname);
1194 error = zfs_secpolicy_write_perms(fsname,
1195 ZFS_DELEG_PERM_HOLD, cr);
1204 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1209 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1210 pair = nvlist_next_nvpair(innvl, pair)) {
1211 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1212 error = dmu_fsname(nvpair_name(pair), fsname);
1215 error = zfs_secpolicy_write_perms(fsname,
1216 ZFS_DELEG_PERM_RELEASE, cr);
1224 * Policy for allowing temporary snapshots to be taken or released
1227 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1230 * A temporary snapshot is the same as a snapshot,
1231 * hold, destroy and release all rolled into one.
1232 * Delegated diff alone is sufficient that we allow this.
1236 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1237 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1240 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1242 if (innvl != NULL) {
1244 error = zfs_secpolicy_hold(zc, innvl, cr);
1246 error = zfs_secpolicy_release(zc, innvl, cr);
1248 error = zfs_secpolicy_destroy(zc, innvl, cr);
1254 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1256 return (zfs_secpolicy_write_perms(zc->zc_name,
1257 ZFS_DELEG_PERM_LOAD_KEY, cr));
1261 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1263 return (zfs_secpolicy_write_perms(zc->zc_name,
1264 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1268 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1271 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1275 nvlist_t *list = NULL;
1278 * Read in and unpack the user-supplied nvlist.
1281 return (SET_ERROR(EINVAL));
1283 packed = vmem_alloc(size, KM_SLEEP);
1285 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1287 vmem_free(packed, size);
1288 return (SET_ERROR(EFAULT));
1291 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1292 vmem_free(packed, size);
1296 vmem_free(packed, size);
1303 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1304 * Entries will be removed from the end of the nvlist, and one int32 entry
1305 * named "N_MORE_ERRORS" will be added indicating how many entries were
1309 nvlist_smush(nvlist_t *errors, size_t max)
1313 size = fnvlist_size(errors);
1316 nvpair_t *more_errors;
1320 return (SET_ERROR(ENOMEM));
1322 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1323 more_errors = nvlist_prev_nvpair(errors, NULL);
1326 nvpair_t *pair = nvlist_prev_nvpair(errors,
1328 fnvlist_remove_nvpair(errors, pair);
1330 size = fnvlist_size(errors);
1331 } while (size > max);
1333 fnvlist_remove_nvpair(errors, more_errors);
1334 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1335 ASSERT3U(fnvlist_size(errors), <=, max);
1342 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1344 char *packed = NULL;
1348 size = fnvlist_size(nvl);
1350 if (size > zc->zc_nvlist_dst_size) {
1351 error = SET_ERROR(ENOMEM);
1353 packed = fnvlist_pack(nvl, &size);
1354 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1355 size, zc->zc_iflags) != 0)
1356 error = SET_ERROR(EFAULT);
1357 fnvlist_pack_free(packed, size);
1360 zc->zc_nvlist_dst_size = size;
1361 zc->zc_nvlist_dst_filled = B_TRUE;
1366 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1369 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1370 return (SET_ERROR(EINVAL));
1373 mutex_enter(&os->os_user_ptr_lock);
1374 *zfvp = dmu_objset_get_user(os);
1375 /* bump s_active only when non-zero to prevent umount race */
1376 error = zfs_vfs_ref(zfvp);
1377 mutex_exit(&os->os_user_ptr_lock);
1382 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1387 error = dmu_objset_hold(dsname, FTAG, &os);
1391 error = getzfsvfs_impl(os, zfvp);
1392 dmu_objset_rele(os, FTAG);
1397 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1398 * case its z_sb will be NULL, and it will be opened as the owner.
1399 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1400 * which prevents all inode ops from running.
1403 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1407 if (getzfsvfs(name, zfvp) != 0)
1408 error = zfsvfs_create(name, B_FALSE, zfvp);
1410 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1412 if ((*zfvp)->z_unmounted) {
1414 * XXX we could probably try again, since the unmounting
1415 * thread should be just about to disassociate the
1416 * objset from the zfsvfs.
1418 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1419 return (SET_ERROR(EBUSY));
1426 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1428 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1430 if (zfs_vfs_held(zfsvfs)) {
1431 zfs_vfs_rele(zfsvfs);
1433 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1434 zfsvfs_free(zfsvfs);
1439 zfs_ioc_pool_create(zfs_cmd_t *zc)
1442 nvlist_t *config, *props = NULL;
1443 nvlist_t *rootprops = NULL;
1444 nvlist_t *zplprops = NULL;
1445 dsl_crypto_params_t *dcp = NULL;
1446 const char *spa_name = zc->zc_name;
1447 boolean_t unload_wkey = B_TRUE;
1449 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1450 zc->zc_iflags, &config)))
1453 if (zc->zc_nvlist_src_size != 0 && (error =
1454 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1455 zc->zc_iflags, &props))) {
1456 nvlist_free(config);
1461 nvlist_t *nvl = NULL;
1462 nvlist_t *hidden_args = NULL;
1463 uint64_t version = SPA_VERSION;
1466 (void) nvlist_lookup_uint64(props,
1467 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1468 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1469 error = SET_ERROR(EINVAL);
1470 goto pool_props_bad;
1472 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1474 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1476 goto pool_props_bad;
1477 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1480 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1482 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1483 rootprops, hidden_args, &dcp);
1485 goto pool_props_bad;
1486 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1488 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1489 error = zfs_fill_zplprops_root(version, rootprops,
1492 goto pool_props_bad;
1494 if (nvlist_lookup_string(props,
1495 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1499 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1502 * Set the remaining root properties
1504 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1505 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1506 (void) spa_destroy(spa_name);
1507 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1511 nvlist_free(rootprops);
1512 nvlist_free(zplprops);
1513 nvlist_free(config);
1515 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1521 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1524 zfs_log_history(zc);
1525 error = spa_destroy(zc->zc_name);
1531 zfs_ioc_pool_import(zfs_cmd_t *zc)
1533 nvlist_t *config, *props = NULL;
1537 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1538 zc->zc_iflags, &config)) != 0)
1541 if (zc->zc_nvlist_src_size != 0 && (error =
1542 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1543 zc->zc_iflags, &props))) {
1544 nvlist_free(config);
1548 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1549 guid != zc->zc_guid)
1550 error = SET_ERROR(EINVAL);
1552 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1554 if (zc->zc_nvlist_dst != 0) {
1557 if ((err = put_nvlist(zc, config)) != 0)
1561 nvlist_free(config);
1568 zfs_ioc_pool_export(zfs_cmd_t *zc)
1571 boolean_t force = (boolean_t)zc->zc_cookie;
1572 boolean_t hardforce = (boolean_t)zc->zc_guid;
1574 zfs_log_history(zc);
1575 error = spa_export(zc->zc_name, NULL, force, hardforce);
1581 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1586 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1587 return (SET_ERROR(EEXIST));
1589 error = put_nvlist(zc, configs);
1591 nvlist_free(configs);
1598 * zc_name name of the pool
1601 * zc_cookie real errno
1602 * zc_nvlist_dst config nvlist
1603 * zc_nvlist_dst_size size of config nvlist
1606 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1612 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1613 sizeof (zc->zc_value));
1615 if (config != NULL) {
1616 ret = put_nvlist(zc, config);
1617 nvlist_free(config);
1620 * The config may be present even if 'error' is non-zero.
1621 * In this case we return success, and preserve the real errno
1624 zc->zc_cookie = error;
1633 * Try to import the given pool, returning pool stats as appropriate so that
1634 * user land knows which devices are available and overall pool health.
1637 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1639 nvlist_t *tryconfig, *config = NULL;
1642 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1643 zc->zc_iflags, &tryconfig)) != 0)
1646 config = spa_tryimport(tryconfig);
1648 nvlist_free(tryconfig);
1651 return (SET_ERROR(EINVAL));
1653 error = put_nvlist(zc, config);
1654 nvlist_free(config);
1661 * zc_name name of the pool
1662 * zc_cookie scan func (pool_scan_func_t)
1663 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1666 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1671 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1672 return (SET_ERROR(EINVAL));
1674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1677 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1678 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1679 else if (zc->zc_cookie == POOL_SCAN_NONE)
1680 error = spa_scan_stop(spa);
1682 error = spa_scan(spa, zc->zc_cookie);
1684 spa_close(spa, FTAG);
1690 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1695 error = spa_open(zc->zc_name, &spa, FTAG);
1698 spa_close(spa, FTAG);
1704 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1709 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1712 if (zc->zc_cookie < spa_version(spa) ||
1713 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1714 spa_close(spa, FTAG);
1715 return (SET_ERROR(EINVAL));
1718 spa_upgrade(spa, zc->zc_cookie);
1719 spa_close(spa, FTAG);
1725 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1732 if ((size = zc->zc_history_len) == 0)
1733 return (SET_ERROR(EINVAL));
1735 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1738 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1739 spa_close(spa, FTAG);
1740 return (SET_ERROR(ENOTSUP));
1743 hist_buf = vmem_alloc(size, KM_SLEEP);
1744 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1745 &zc->zc_history_len, hist_buf)) == 0) {
1746 error = ddi_copyout(hist_buf,
1747 (void *)(uintptr_t)zc->zc_history,
1748 zc->zc_history_len, zc->zc_iflags);
1751 spa_close(spa, FTAG);
1752 vmem_free(hist_buf, size);
1757 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1762 error = spa_open(zc->zc_name, &spa, FTAG);
1764 error = spa_change_guid(spa);
1765 spa_close(spa, FTAG);
1771 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1773 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1778 * zc_name name of filesystem
1779 * zc_obj object to find
1782 * zc_value name of object
1785 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1790 /* XXX reading from objset not owned */
1791 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1794 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1795 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1796 return (SET_ERROR(EINVAL));
1798 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1799 sizeof (zc->zc_value));
1800 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1807 * zc_name name of filesystem
1808 * zc_obj object to find
1811 * zc_stat stats on object
1812 * zc_value path to object
1815 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1820 /* XXX reading from objset not owned */
1821 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1824 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1825 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1826 return (SET_ERROR(EINVAL));
1828 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1829 sizeof (zc->zc_value));
1830 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1836 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1842 error = spa_open(zc->zc_name, &spa, FTAG);
1846 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1847 zc->zc_iflags, &config);
1849 error = spa_vdev_add(spa, config);
1850 nvlist_free(config);
1852 spa_close(spa, FTAG);
1858 * zc_name name of the pool
1859 * zc_guid guid of vdev to remove
1860 * zc_cookie cancel removal
1863 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1868 error = spa_open(zc->zc_name, &spa, FTAG);
1871 if (zc->zc_cookie != 0) {
1872 error = spa_vdev_remove_cancel(spa);
1874 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1876 spa_close(spa, FTAG);
1881 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1885 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1887 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1889 switch (zc->zc_cookie) {
1890 case VDEV_STATE_ONLINE:
1891 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1894 case VDEV_STATE_OFFLINE:
1895 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1898 case VDEV_STATE_FAULTED:
1899 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1900 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1901 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1902 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1904 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1907 case VDEV_STATE_DEGRADED:
1908 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1909 zc->zc_obj != VDEV_AUX_EXTERNAL)
1910 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1912 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1916 error = SET_ERROR(EINVAL);
1918 zc->zc_cookie = newstate;
1919 spa_close(spa, FTAG);
1924 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1928 int replacing = zc->zc_cookie;
1929 int rebuild = zc->zc_simple;
1932 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1935 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1936 zc->zc_iflags, &config)) == 0) {
1937 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing,
1939 nvlist_free(config);
1942 spa_close(spa, FTAG);
1947 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1952 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1955 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1957 spa_close(spa, FTAG);
1962 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1965 nvlist_t *config, *props = NULL;
1967 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1969 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1972 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1973 zc->zc_iflags, &config))) {
1974 spa_close(spa, FTAG);
1978 if (zc->zc_nvlist_src_size != 0 && (error =
1979 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1980 zc->zc_iflags, &props))) {
1981 spa_close(spa, FTAG);
1982 nvlist_free(config);
1986 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1988 spa_close(spa, FTAG);
1990 nvlist_free(config);
1997 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2000 const char *path = zc->zc_value;
2001 uint64_t guid = zc->zc_guid;
2004 error = spa_open(zc->zc_name, &spa, FTAG);
2008 error = spa_vdev_setpath(spa, guid, path);
2009 spa_close(spa, FTAG);
2014 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2017 const char *fru = zc->zc_value;
2018 uint64_t guid = zc->zc_guid;
2021 error = spa_open(zc->zc_name, &spa, FTAG);
2025 error = spa_vdev_setfru(spa, guid, fru);
2026 spa_close(spa, FTAG);
2031 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2036 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2038 if (zc->zc_nvlist_dst != 0 &&
2039 (error = dsl_prop_get_all(os, &nv)) == 0) {
2040 dmu_objset_stats(os, nv);
2042 * NB: zvol_get_stats() will read the objset contents,
2043 * which we aren't supposed to do with a
2044 * DS_MODE_USER hold, because it could be
2045 * inconsistent. So this is a bit of a workaround...
2046 * XXX reading without owning
2048 if (!zc->zc_objset_stats.dds_inconsistent &&
2049 dmu_objset_type(os) == DMU_OST_ZVOL) {
2050 error = zvol_get_stats(os, nv);
2058 error = put_nvlist(zc, nv);
2067 * zc_name name of filesystem
2068 * zc_nvlist_dst_size size of buffer for property nvlist
2071 * zc_objset_stats stats
2072 * zc_nvlist_dst property nvlist
2073 * zc_nvlist_dst_size size of property nvlist
2076 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2081 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2083 error = zfs_ioc_objset_stats_impl(zc, os);
2084 dmu_objset_rele(os, FTAG);
2092 * zc_name name of filesystem
2093 * zc_nvlist_dst_size size of buffer for property nvlist
2096 * zc_nvlist_dst received property nvlist
2097 * zc_nvlist_dst_size size of received property nvlist
2099 * Gets received properties (distinct from local properties on or after
2100 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2101 * local property values.
2104 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2110 * Without this check, we would return local property values if the
2111 * caller has not already received properties on or after
2112 * SPA_VERSION_RECVD_PROPS.
2114 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2115 return (SET_ERROR(ENOTSUP));
2117 if (zc->zc_nvlist_dst != 0 &&
2118 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2119 error = put_nvlist(zc, nv);
2127 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2133 * zfs_get_zplprop() will either find a value or give us
2134 * the default value (if there is one).
2136 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2138 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2144 * zc_name name of filesystem
2145 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2148 * zc_nvlist_dst zpl property nvlist
2149 * zc_nvlist_dst_size size of zpl property nvlist
2152 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2157 /* XXX reading without owning */
2158 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2161 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2164 * NB: nvl_add_zplprop() will read the objset contents,
2165 * which we aren't supposed to do with a DS_MODE_USER
2166 * hold, because it could be inconsistent.
2168 if (zc->zc_nvlist_dst != 0 &&
2169 !zc->zc_objset_stats.dds_inconsistent &&
2170 dmu_objset_type(os) == DMU_OST_ZFS) {
2173 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2174 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2175 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2176 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2177 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2178 err = put_nvlist(zc, nv);
2181 err = SET_ERROR(ENOENT);
2183 dmu_objset_rele(os, FTAG);
2189 * zc_name name of filesystem
2190 * zc_cookie zap cursor
2191 * zc_nvlist_dst_size size of buffer for property nvlist
2194 * zc_name name of next filesystem
2195 * zc_cookie zap cursor
2196 * zc_objset_stats stats
2197 * zc_nvlist_dst property nvlist
2198 * zc_nvlist_dst_size size of property nvlist
2201 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2206 size_t orig_len = strlen(zc->zc_name);
2209 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2210 if (error == ENOENT)
2211 error = SET_ERROR(ESRCH);
2215 p = strrchr(zc->zc_name, '/');
2216 if (p == NULL || p[1] != '\0')
2217 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2218 p = zc->zc_name + strlen(zc->zc_name);
2221 error = dmu_dir_list_next(os,
2222 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2223 NULL, &zc->zc_cookie);
2224 if (error == ENOENT)
2225 error = SET_ERROR(ESRCH);
2226 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2227 dmu_objset_rele(os, FTAG);
2230 * If it's an internal dataset (ie. with a '$' in its name),
2231 * don't try to get stats for it, otherwise we'll return ENOENT.
2233 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2234 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2235 if (error == ENOENT) {
2236 /* We lost a race with destroy, get the next one. */
2237 zc->zc_name[orig_len] = '\0';
2246 * zc_name name of filesystem
2247 * zc_cookie zap cursor
2248 * zc_nvlist_src iteration range nvlist
2249 * zc_nvlist_src_size size of iteration range nvlist
2252 * zc_name name of next snapshot
2253 * zc_objset_stats stats
2254 * zc_nvlist_dst property nvlist
2255 * zc_nvlist_dst_size size of property nvlist
2258 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2261 objset_t *os, *ossnap;
2263 uint64_t min_txg = 0, max_txg = 0;
2265 if (zc->zc_nvlist_src_size != 0) {
2266 nvlist_t *props = NULL;
2267 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2268 zc->zc_iflags, &props);
2271 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2273 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2278 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2280 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
2284 * A dataset name of maximum length cannot have any snapshots,
2285 * so exit immediately.
2287 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2288 ZFS_MAX_DATASET_NAME_LEN) {
2289 dmu_objset_rele(os, FTAG);
2290 return (SET_ERROR(ESRCH));
2293 while (error == 0) {
2294 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2295 error = SET_ERROR(EINTR);
2299 error = dmu_snapshot_list_next(os,
2300 sizeof (zc->zc_name) - strlen(zc->zc_name),
2301 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2302 &zc->zc_cookie, NULL);
2303 if (error == ENOENT) {
2304 error = SET_ERROR(ESRCH);
2306 } else if (error != 0) {
2310 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2315 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2316 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2317 dsl_dataset_rele(ds, FTAG);
2318 /* undo snapshot name append */
2319 *(strchr(zc->zc_name, '@') + 1) = '\0';
2324 if (zc->zc_simple) {
2325 dsl_dataset_rele(ds, FTAG);
2329 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2330 dsl_dataset_rele(ds, FTAG);
2333 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2334 dsl_dataset_rele(ds, FTAG);
2337 dsl_dataset_rele(ds, FTAG);
2341 dmu_objset_rele(os, FTAG);
2342 /* if we failed, undo the @ that we tacked on to zc_name */
2344 *strchr(zc->zc_name, '@') = '\0';
2349 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2351 const char *propname = nvpair_name(pair);
2353 unsigned int vallen;
2354 const char *dash, *domain;
2355 zfs_userquota_prop_t type;
2361 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2363 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2364 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2366 return (SET_ERROR(EINVAL));
2370 * A correctly constructed propname is encoded as
2371 * userquota@<rid>-<domain>.
2373 if ((dash = strchr(propname, '-')) == NULL ||
2374 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2376 return (SET_ERROR(EINVAL));
2383 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2385 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2386 zfsvfs_rele(zfsvfs, FTAG);
2393 * If the named property is one that has a special function to set its value,
2394 * return 0 on success and a positive error code on failure; otherwise if it is
2395 * not one of the special properties handled by this function, return -1.
2397 * XXX: It would be better for callers of the property interface if we handled
2398 * these special cases in dsl_prop.c (in the dsl layer).
2401 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2404 const char *propname = nvpair_name(pair);
2405 zfs_prop_t prop = zfs_name_to_prop(propname);
2406 uint64_t intval = 0;
2407 const char *strval = NULL;
2410 if (prop == ZPROP_INVAL) {
2411 if (zfs_prop_userquota(propname))
2412 return (zfs_prop_set_userquota(dsname, pair));
2416 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2418 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2419 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2423 /* all special properties are numeric except for keylocation */
2424 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2425 strval = fnvpair_value_string(pair);
2427 intval = fnvpair_value_uint64(pair);
2431 case ZFS_PROP_QUOTA:
2432 err = dsl_dir_set_quota(dsname, source, intval);
2434 case ZFS_PROP_REFQUOTA:
2435 err = dsl_dataset_set_refquota(dsname, source, intval);
2437 case ZFS_PROP_FILESYSTEM_LIMIT:
2438 case ZFS_PROP_SNAPSHOT_LIMIT:
2439 if (intval == UINT64_MAX) {
2440 /* clearing the limit, just do it */
2443 err = dsl_dir_activate_fs_ss_limit(dsname);
2446 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2447 * default path to set the value in the nvlist.
2452 case ZFS_PROP_KEYLOCATION:
2453 err = dsl_crypto_can_set_keylocation(dsname, strval);
2456 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2457 * default path to set the value in the nvlist.
2462 case ZFS_PROP_RESERVATION:
2463 err = dsl_dir_set_reservation(dsname, source, intval);
2465 case ZFS_PROP_REFRESERVATION:
2466 err = dsl_dataset_set_refreservation(dsname, source, intval);
2468 case ZFS_PROP_COMPRESSION:
2469 err = dsl_dataset_set_compression(dsname, source, intval);
2471 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2472 * default path to set the value in the nvlist.
2477 case ZFS_PROP_VOLSIZE:
2478 err = zvol_set_volsize(dsname, intval);
2480 case ZFS_PROP_SNAPDEV:
2481 err = zvol_set_snapdev(dsname, source, intval);
2483 case ZFS_PROP_VOLMODE:
2484 err = zvol_set_volmode(dsname, source, intval);
2486 case ZFS_PROP_VERSION:
2490 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2493 err = zfs_set_version(zfsvfs, intval);
2494 zfsvfs_rele(zfsvfs, FTAG);
2496 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2499 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2500 (void) strlcpy(zc->zc_name, dsname,
2501 sizeof (zc->zc_name));
2502 (void) zfs_ioc_userspace_upgrade(zc);
2503 (void) zfs_ioc_id_quota_upgrade(zc);
2504 kmem_free(zc, sizeof (zfs_cmd_t));
2516 * This function is best effort. If it fails to set any of the given properties,
2517 * it continues to set as many as it can and returns the last error
2518 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2519 * with the list of names of all the properties that failed along with the
2520 * corresponding error numbers.
2522 * If every property is set successfully, zero is returned and errlist is not
2526 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2535 nvlist_t *genericnvl = fnvlist_alloc();
2536 nvlist_t *retrynvl = fnvlist_alloc();
2539 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2540 const char *propname = nvpair_name(pair);
2541 zfs_prop_t prop = zfs_name_to_prop(propname);
2544 /* decode the property value */
2546 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2548 attrs = fnvpair_value_nvlist(pair);
2549 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2551 err = SET_ERROR(EINVAL);
2554 /* Validate value type */
2555 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2556 /* inherited properties are expected to be booleans */
2557 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2558 err = SET_ERROR(EINVAL);
2559 } else if (err == 0 && prop == ZPROP_INVAL) {
2560 if (zfs_prop_user(propname)) {
2561 if (nvpair_type(propval) != DATA_TYPE_STRING)
2562 err = SET_ERROR(EINVAL);
2563 } else if (zfs_prop_userquota(propname)) {
2564 if (nvpair_type(propval) !=
2565 DATA_TYPE_UINT64_ARRAY)
2566 err = SET_ERROR(EINVAL);
2568 err = SET_ERROR(EINVAL);
2570 } else if (err == 0) {
2571 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2572 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2573 err = SET_ERROR(EINVAL);
2574 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2577 intval = fnvpair_value_uint64(propval);
2579 switch (zfs_prop_get_type(prop)) {
2580 case PROP_TYPE_NUMBER:
2582 case PROP_TYPE_STRING:
2583 err = SET_ERROR(EINVAL);
2585 case PROP_TYPE_INDEX:
2586 if (zfs_prop_index_to_string(prop,
2587 intval, &unused) != 0)
2589 SET_ERROR(ZFS_ERR_BADPROP);
2593 "unknown property type");
2596 err = SET_ERROR(EINVAL);
2600 /* Validate permissions */
2602 err = zfs_check_settable(dsname, pair, CRED());
2605 if (source == ZPROP_SRC_INHERITED)
2606 err = -1; /* does not need special handling */
2608 err = zfs_prop_set_special(dsname, source,
2612 * For better performance we build up a list of
2613 * properties to set in a single transaction.
2615 err = nvlist_add_nvpair(genericnvl, pair);
2616 } else if (err != 0 && nvl != retrynvl) {
2618 * This may be a spurious error caused by
2619 * receiving quota and reservation out of order.
2620 * Try again in a second pass.
2622 err = nvlist_add_nvpair(retrynvl, pair);
2627 if (errlist != NULL)
2628 fnvlist_add_int32(errlist, propname, err);
2633 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2638 if (!nvlist_empty(genericnvl) &&
2639 dsl_props_set(dsname, source, genericnvl) != 0) {
2641 * If this fails, we still want to set as many properties as we
2642 * can, so try setting them individually.
2645 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2646 const char *propname = nvpair_name(pair);
2650 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2652 attrs = fnvpair_value_nvlist(pair);
2653 propval = fnvlist_lookup_nvpair(attrs,
2657 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2658 strval = fnvpair_value_string(propval);
2659 err = dsl_prop_set_string(dsname, propname,
2661 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2662 err = dsl_prop_inherit(dsname, propname,
2665 intval = fnvpair_value_uint64(propval);
2666 err = dsl_prop_set_int(dsname, propname, source,
2671 if (errlist != NULL) {
2672 fnvlist_add_int32(errlist, propname,
2679 nvlist_free(genericnvl);
2680 nvlist_free(retrynvl);
2686 * Check that all the properties are valid user properties.
2689 zfs_check_userprops(nvlist_t *nvl)
2691 nvpair_t *pair = NULL;
2693 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2694 const char *propname = nvpair_name(pair);
2696 if (!zfs_prop_user(propname) ||
2697 nvpair_type(pair) != DATA_TYPE_STRING)
2698 return (SET_ERROR(EINVAL));
2700 if (strlen(propname) >= ZAP_MAXNAMELEN)
2701 return (SET_ERROR(ENAMETOOLONG));
2703 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2704 return (SET_ERROR(E2BIG));
2710 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2714 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2717 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2718 if (nvlist_exists(skipped, nvpair_name(pair)))
2721 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2726 clear_received_props(const char *dsname, nvlist_t *props,
2730 nvlist_t *cleared_props = NULL;
2731 props_skip(props, skipped, &cleared_props);
2732 if (!nvlist_empty(cleared_props)) {
2734 * Acts on local properties until the dataset has received
2735 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2737 zprop_source_t flags = (ZPROP_SRC_NONE |
2738 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2739 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2741 nvlist_free(cleared_props);
2747 * zc_name name of filesystem
2748 * zc_value name of property to set
2749 * zc_nvlist_src{_size} nvlist of properties to apply
2750 * zc_cookie received properties flag
2753 * zc_nvlist_dst{_size} error for each unapplied received property
2756 zfs_ioc_set_prop(zfs_cmd_t *zc)
2759 boolean_t received = zc->zc_cookie;
2760 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2765 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2766 zc->zc_iflags, &nvl)) != 0)
2770 nvlist_t *origprops;
2772 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2773 (void) clear_received_props(zc->zc_name,
2775 nvlist_free(origprops);
2778 error = dsl_prop_set_hasrecvd(zc->zc_name);
2781 errors = fnvlist_alloc();
2783 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2785 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2786 (void) put_nvlist(zc, errors);
2789 nvlist_free(errors);
2796 * zc_name name of filesystem
2797 * zc_value name of property to inherit
2798 * zc_cookie revert to received value if TRUE
2803 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2805 const char *propname = zc->zc_value;
2806 zfs_prop_t prop = zfs_name_to_prop(propname);
2807 boolean_t received = zc->zc_cookie;
2808 zprop_source_t source = (received
2809 ? ZPROP_SRC_NONE /* revert to received value, if any */
2810 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2818 * Only check this in the non-received case. We want to allow
2819 * 'inherit -S' to revert non-inheritable properties like quota
2820 * and reservation to the received or default values even though
2821 * they are not considered inheritable.
2823 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2824 return (SET_ERROR(EINVAL));
2827 if (prop == ZPROP_INVAL) {
2828 if (!zfs_prop_user(propname))
2829 return (SET_ERROR(EINVAL));
2831 type = PROP_TYPE_STRING;
2832 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2833 return (SET_ERROR(EINVAL));
2835 type = zfs_prop_get_type(prop);
2839 * zfs_prop_set_special() expects properties in the form of an
2840 * nvpair with type info.
2842 dummy = fnvlist_alloc();
2845 case PROP_TYPE_STRING:
2846 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2848 case PROP_TYPE_NUMBER:
2849 case PROP_TYPE_INDEX:
2850 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2853 err = SET_ERROR(EINVAL);
2857 pair = nvlist_next_nvpair(dummy, NULL);
2859 err = SET_ERROR(EINVAL);
2861 err = zfs_prop_set_special(zc->zc_name, source, pair);
2862 if (err == -1) /* property is not "special", needs handling */
2863 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2873 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2880 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2881 zc->zc_iflags, &props)))
2885 * If the only property is the configfile, then just do a spa_lookup()
2886 * to handle the faulted case.
2888 pair = nvlist_next_nvpair(props, NULL);
2889 if (pair != NULL && strcmp(nvpair_name(pair),
2890 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2891 nvlist_next_nvpair(props, pair) == NULL) {
2892 mutex_enter(&spa_namespace_lock);
2893 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2894 spa_configfile_set(spa, props, B_FALSE);
2895 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2897 mutex_exit(&spa_namespace_lock);
2904 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2909 error = spa_prop_set(spa, props);
2912 spa_close(spa, FTAG);
2918 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2922 nvlist_t *nvp = NULL;
2924 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2926 * If the pool is faulted, there may be properties we can still
2927 * get (such as altroot and cachefile), so attempt to get them
2930 mutex_enter(&spa_namespace_lock);
2931 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2932 error = spa_prop_get(spa, &nvp);
2933 mutex_exit(&spa_namespace_lock);
2935 error = spa_prop_get(spa, &nvp);
2936 spa_close(spa, FTAG);
2939 if (error == 0 && zc->zc_nvlist_dst != 0)
2940 error = put_nvlist(zc, nvp);
2942 error = SET_ERROR(EFAULT);
2950 * zc_name name of filesystem
2951 * zc_nvlist_src{_size} nvlist of delegated permissions
2952 * zc_perm_action allow/unallow flag
2957 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2960 nvlist_t *fsaclnv = NULL;
2962 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2963 zc->zc_iflags, &fsaclnv)) != 0)
2967 * Verify nvlist is constructed correctly
2969 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2970 nvlist_free(fsaclnv);
2971 return (SET_ERROR(EINVAL));
2975 * If we don't have PRIV_SYS_MOUNT, then validate
2976 * that user is allowed to hand out each permission in
2980 error = secpolicy_zfs(CRED());
2982 if (zc->zc_perm_action == B_FALSE) {
2983 error = dsl_deleg_can_allow(zc->zc_name,
2986 error = dsl_deleg_can_unallow(zc->zc_name,
2992 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
2994 nvlist_free(fsaclnv);
3000 * zc_name name of filesystem
3003 * zc_nvlist_src{_size} nvlist of delegated permissions
3006 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3011 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3012 error = put_nvlist(zc, nvp);
3021 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3023 zfs_creat_t *zct = arg;
3025 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3028 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3032 * os parent objset pointer (NULL if root fs)
3033 * fuids_ok fuids allowed in this version of the spa?
3034 * sa_ok SAs allowed in this version of the spa?
3035 * createprops list of properties requested by creator
3038 * zplprops values for the zplprops we attach to the master node object
3039 * is_ci true if requested file system will be purely case-insensitive
3041 * Determine the settings for utf8only, normalization and
3042 * casesensitivity. Specific values may have been requested by the
3043 * creator and/or we can inherit values from the parent dataset. If
3044 * the file system is of too early a vintage, a creator can not
3045 * request settings for these properties, even if the requested
3046 * setting is the default value. We don't actually want to create dsl
3047 * properties for these, so remove them from the source nvlist after
3051 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3052 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3053 nvlist_t *zplprops, boolean_t *is_ci)
3055 uint64_t sense = ZFS_PROP_UNDEFINED;
3056 uint64_t norm = ZFS_PROP_UNDEFINED;
3057 uint64_t u8 = ZFS_PROP_UNDEFINED;
3060 ASSERT(zplprops != NULL);
3062 /* parent dataset must be a filesystem */
3063 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3064 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3067 * Pull out creator prop choices, if any.
3070 (void) nvlist_lookup_uint64(createprops,
3071 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3072 (void) nvlist_lookup_uint64(createprops,
3073 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3074 (void) nvlist_remove_all(createprops,
3075 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3076 (void) nvlist_lookup_uint64(createprops,
3077 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3078 (void) nvlist_remove_all(createprops,
3079 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3080 (void) nvlist_lookup_uint64(createprops,
3081 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3082 (void) nvlist_remove_all(createprops,
3083 zfs_prop_to_name(ZFS_PROP_CASE));
3087 * If the zpl version requested is whacky or the file system
3088 * or pool is version is too "young" to support normalization
3089 * and the creator tried to set a value for one of the props,
3092 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3093 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3094 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3095 (zplver < ZPL_VERSION_NORMALIZATION &&
3096 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3097 sense != ZFS_PROP_UNDEFINED)))
3098 return (SET_ERROR(ENOTSUP));
3101 * Put the version in the zplprops
3103 VERIFY(nvlist_add_uint64(zplprops,
3104 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3106 if (norm == ZFS_PROP_UNDEFINED &&
3107 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3109 VERIFY(nvlist_add_uint64(zplprops,
3110 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3113 * If we're normalizing, names must always be valid UTF-8 strings.
3117 if (u8 == ZFS_PROP_UNDEFINED &&
3118 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3120 VERIFY(nvlist_add_uint64(zplprops,
3121 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3123 if (sense == ZFS_PROP_UNDEFINED &&
3124 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3126 VERIFY(nvlist_add_uint64(zplprops,
3127 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3130 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3136 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3137 nvlist_t *zplprops, boolean_t *is_ci)
3139 boolean_t fuids_ok, sa_ok;
3140 uint64_t zplver = ZPL_VERSION;
3141 objset_t *os = NULL;
3142 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3147 zfs_get_parent(dataset, parentname, sizeof (parentname));
3149 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3152 spa_vers = spa_version(spa);
3153 spa_close(spa, FTAG);
3155 zplver = zfs_zpl_version_map(spa_vers);
3156 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3157 sa_ok = (zplver >= ZPL_VERSION_SA);
3160 * Open parent object set so we can inherit zplprop values.
3162 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3165 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3167 dmu_objset_rele(os, FTAG);
3172 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3173 nvlist_t *zplprops, boolean_t *is_ci)
3177 uint64_t zplver = ZPL_VERSION;
3180 zplver = zfs_zpl_version_map(spa_vers);
3181 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3182 sa_ok = (zplver >= ZPL_VERSION_SA);
3184 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3185 createprops, zplprops, is_ci);
3191 * "type" -> dmu_objset_type_t (int32)
3192 * (optional) "props" -> { prop -> value }
3193 * (optional) "hidden_args" -> { "wkeydata" -> value }
3194 * raw uint8_t array of encryption wrapping key data (32 bytes)
3197 * outnvl: propname -> error code (int32)
3200 static const zfs_ioc_key_t zfs_keys_create[] = {
3201 {"type", DATA_TYPE_INT32, 0},
3202 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3203 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3207 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3210 zfs_creat_t zct = { 0 };
3211 nvlist_t *nvprops = NULL;
3212 nvlist_t *hidden_args = NULL;
3213 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3214 dmu_objset_type_t type;
3215 boolean_t is_insensitive = B_FALSE;
3216 dsl_crypto_params_t *dcp = NULL;
3218 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3219 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3220 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3224 cbfunc = zfs_create_cb;
3228 cbfunc = zvol_create_cb;
3235 if (strchr(fsname, '@') ||
3236 strchr(fsname, '%'))
3237 return (SET_ERROR(EINVAL));
3239 zct.zct_props = nvprops;
3242 return (SET_ERROR(EINVAL));
3244 if (type == DMU_OST_ZVOL) {
3245 uint64_t volsize, volblocksize;
3247 if (nvprops == NULL)
3248 return (SET_ERROR(EINVAL));
3249 if (nvlist_lookup_uint64(nvprops,
3250 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3251 return (SET_ERROR(EINVAL));
3253 if ((error = nvlist_lookup_uint64(nvprops,
3254 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3255 &volblocksize)) != 0 && error != ENOENT)
3256 return (SET_ERROR(EINVAL));
3259 volblocksize = zfs_prop_default_numeric(
3260 ZFS_PROP_VOLBLOCKSIZE);
3262 if ((error = zvol_check_volblocksize(fsname,
3263 volblocksize)) != 0 ||
3264 (error = zvol_check_volsize(volsize,
3265 volblocksize)) != 0)
3267 } else if (type == DMU_OST_ZFS) {
3271 * We have to have normalization and
3272 * case-folding flags correct when we do the
3273 * file system creation, so go figure them out
3276 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3277 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3278 error = zfs_fill_zplprops(fsname, nvprops,
3279 zct.zct_zplprops, &is_insensitive);
3281 nvlist_free(zct.zct_zplprops);
3286 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3289 nvlist_free(zct.zct_zplprops);
3293 error = dmu_objset_create(fsname, type,
3294 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3296 nvlist_free(zct.zct_zplprops);
3297 dsl_crypto_params_free(dcp, !!error);
3300 * It would be nice to do this atomically.
3303 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3310 * Volumes will return EBUSY and cannot be destroyed
3311 * until all asynchronous minor handling (e.g. from
3312 * setting the volmode property) has completed. Wait for
3313 * the spa_zvol_taskq to drain then retry.
3315 error2 = dsl_destroy_head(fsname);
3316 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3317 error2 = spa_open(fsname, &spa, FTAG);
3319 taskq_wait(spa->spa_zvol_taskq);
3320 spa_close(spa, FTAG);
3322 error2 = dsl_destroy_head(fsname);
3331 * "origin" -> name of origin snapshot
3332 * (optional) "props" -> { prop -> value }
3333 * (optional) "hidden_args" -> { "wkeydata" -> value }
3334 * raw uint8_t array of encryption wrapping key data (32 bytes)
3338 * outnvl: propname -> error code (int32)
3340 static const zfs_ioc_key_t zfs_keys_clone[] = {
3341 {"origin", DATA_TYPE_STRING, 0},
3342 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3343 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3347 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3350 nvlist_t *nvprops = NULL;
3351 const char *origin_name;
3353 origin_name = fnvlist_lookup_string(innvl, "origin");
3354 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3356 if (strchr(fsname, '@') ||
3357 strchr(fsname, '%'))
3358 return (SET_ERROR(EINVAL));
3360 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3361 return (SET_ERROR(EINVAL));
3363 error = dmu_objset_clone(fsname, origin_name);
3366 * It would be nice to do this atomically.
3369 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3372 (void) dsl_destroy_head(fsname);
3377 static const zfs_ioc_key_t zfs_keys_remap[] = {
3383 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3385 /* This IOCTL is no longer supported. */
3391 * "snaps" -> { snapshot1, snapshot2 }
3392 * (optional) "props" -> { prop -> value (string) }
3395 * outnvl: snapshot -> error code (int32)
3397 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3398 {"snaps", DATA_TYPE_NVLIST, 0},
3399 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3403 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3406 nvlist_t *props = NULL;
3410 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3411 if (!nvlist_empty(props) &&
3412 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3413 return (SET_ERROR(ENOTSUP));
3414 if ((error = zfs_check_userprops(props)) != 0)
3417 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3418 poollen = strlen(poolname);
3419 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3420 pair = nvlist_next_nvpair(snaps, pair)) {
3421 const char *name = nvpair_name(pair);
3422 char *cp = strchr(name, '@');
3425 * The snap name must contain an @, and the part after it must
3426 * contain only valid characters.
3429 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3430 return (SET_ERROR(EINVAL));
3433 * The snap must be in the specified pool.
3435 if (strncmp(name, poolname, poollen) != 0 ||
3436 (name[poollen] != '/' && name[poollen] != '@'))
3437 return (SET_ERROR(EXDEV));
3440 * Check for permission to set the properties on the fs.
3442 if (!nvlist_empty(props)) {
3444 error = zfs_secpolicy_write_perms(name,
3445 ZFS_DELEG_PERM_USERPROP, CRED());
3451 /* This must be the only snap of this fs. */
3452 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3453 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3454 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3456 return (SET_ERROR(EXDEV));
3461 error = dsl_dataset_snapshot(snaps, props, outnvl);
3467 * innvl: "message" -> string
3469 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3470 {"message", DATA_TYPE_STRING, 0},
3475 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3477 const char *message;
3483 * The poolname in the ioctl is not set, we get it from the TSD,
3484 * which was set at the end of the last successful ioctl that allows
3485 * logging. The secpolicy func already checked that it is set.
3486 * Only one log ioctl is allowed after each successful ioctl, so
3487 * we clear the TSD here.
3489 poolname = tsd_get(zfs_allow_log_key);
3490 if (poolname == NULL)
3491 return (SET_ERROR(EINVAL));
3492 (void) tsd_set(zfs_allow_log_key, NULL);
3493 error = spa_open(poolname, &spa, FTAG);
3494 kmem_strfree(poolname);
3498 message = fnvlist_lookup_string(innvl, "message");
3500 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3501 spa_close(spa, FTAG);
3502 return (SET_ERROR(ENOTSUP));
3505 error = spa_history_log(spa, message);
3506 spa_close(spa, FTAG);
3511 * This ioctl is used to set the bootenv configuration on the current
3512 * pool. This configuration is stored in the second padding area of the label,
3513 * and it is used by the bootloader(s) to store the bootloader and/or system
3515 * The data is stored as nvlist data stream, and is protected by
3516 * an embedded checksum.
3517 * The version can have two possible values:
3518 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3519 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3521 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3522 {"version", DATA_TYPE_UINT64, 0},
3523 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3527 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3532 if ((error = spa_open(name, &spa, FTAG)) != 0)
3534 spa_vdev_state_enter(spa, SCL_ALL);
3535 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3536 (void) spa_vdev_state_exit(spa, NULL, 0);
3537 spa_close(spa, FTAG);
3541 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3546 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3551 if ((error = spa_open(name, &spa, FTAG)) != 0)
3553 spa_vdev_state_enter(spa, SCL_ALL);
3554 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3555 (void) spa_vdev_state_exit(spa, NULL, 0);
3556 spa_close(spa, FTAG);
3561 * The dp_config_rwlock must not be held when calling this, because the
3562 * unmount may need to write out data.
3564 * This function is best-effort. Callers must deal gracefully if it
3565 * remains mounted (or is remounted after this call).
3567 * Returns 0 if the argument is not a snapshot, or it is not currently a
3568 * filesystem, or we were able to unmount it. Returns error code otherwise.
3571 zfs_unmount_snap(const char *snapname)
3573 if (strchr(snapname, '@') == NULL)
3576 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE);
3581 zfs_unmount_snap_cb(const char *snapname, void *arg)
3583 zfs_unmount_snap(snapname);
3588 * When a clone is destroyed, its origin may also need to be destroyed,
3589 * in which case it must be unmounted. This routine will do that unmount
3593 zfs_destroy_unmount_origin(const char *fsname)
3599 error = dmu_objset_hold(fsname, FTAG, &os);
3602 ds = dmu_objset_ds(os);
3603 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3604 char originname[ZFS_MAX_DATASET_NAME_LEN];
3605 dsl_dataset_name(ds->ds_prev, originname);
3606 dmu_objset_rele(os, FTAG);
3607 zfs_unmount_snap(originname);
3609 dmu_objset_rele(os, FTAG);
3615 * "snaps" -> { snapshot1, snapshot2 }
3616 * (optional boolean) "defer"
3619 * outnvl: snapshot -> error code (int32)
3621 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3622 {"snaps", DATA_TYPE_NVLIST, 0},
3623 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3628 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3636 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3637 defer = nvlist_exists(innvl, "defer");
3639 poollen = strlen(poolname);
3640 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3641 pair = nvlist_next_nvpair(snaps, pair)) {
3642 const char *name = nvpair_name(pair);
3645 * The snap must be in the specified pool to prevent the
3646 * invalid removal of zvol minors below.
3648 if (strncmp(name, poolname, poollen) != 0 ||
3649 (name[poollen] != '/' && name[poollen] != '@'))
3650 return (SET_ERROR(EXDEV));
3652 zfs_unmount_snap(nvpair_name(pair));
3653 if (spa_open(name, &spa, FTAG) == 0) {
3654 zvol_remove_minors(spa, name, B_TRUE);
3655 spa_close(spa, FTAG);
3659 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3663 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>.
3664 * All bookmarks and snapshots must be in the same pool.
3665 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail.
3668 * new_bookmark1 -> existing_snapshot,
3669 * new_bookmark2 -> existing_bookmark,
3672 * outnvl: bookmark -> error code (int32)
3675 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3676 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3681 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3683 return (dsl_bookmark_create(innvl, outnvl));
3688 * property 1, property 2, ...
3692 * bookmark name 1 -> { property 1, property 2, ... },
3693 * bookmark name 2 -> { property 1, property 2, ... }
3697 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3698 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3702 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3704 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3708 * innvl is not used.
3711 * property 1, property 2, ...
3715 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
3721 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
3724 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3727 bmname = strchr(bookmark, '#');
3729 return (SET_ERROR(EINVAL));
3732 (void) strlcpy(fsname, bookmark, sizeof (fsname));
3733 *(strchr(fsname, '#')) = '\0';
3735 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
3740 * bookmark name 1, bookmark name 2
3743 * outnvl: bookmark -> error code (int32)
3746 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3747 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3751 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3756 poollen = strlen(poolname);
3757 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3758 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3759 const char *name = nvpair_name(pair);
3760 const char *cp = strchr(name, '#');
3763 * The bookmark name must contain an #, and the part after it
3764 * must contain only valid characters.
3767 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3768 return (SET_ERROR(EINVAL));
3771 * The bookmark must be in the specified pool.
3773 if (strncmp(name, poolname, poollen) != 0 ||
3774 (name[poollen] != '/' && name[poollen] != '#'))
3775 return (SET_ERROR(EXDEV));
3778 error = dsl_bookmark_destroy(innvl, outnvl);
3782 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3783 {"program", DATA_TYPE_STRING, 0},
3784 {"arg", DATA_TYPE_ANY, 0},
3785 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3786 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3787 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3791 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3795 uint64_t instrlimit, memlimit;
3796 boolean_t sync_flag;
3797 nvpair_t *nvarg = NULL;
3799 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3800 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3803 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3804 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3806 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3807 memlimit = ZCP_DEFAULT_MEMLIMIT;
3809 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3811 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3812 return (SET_ERROR(EINVAL));
3813 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3814 return (SET_ERROR(EINVAL));
3816 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3824 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3830 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3832 return (spa_checkpoint(poolname));
3839 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3845 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3848 return (spa_checkpoint_discard(poolname));
3853 * zc_name name of dataset to destroy
3854 * zc_defer_destroy mark for deferred destroy
3859 zfs_ioc_destroy(zfs_cmd_t *zc)
3862 dmu_objset_type_t ost;
3865 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3868 ost = dmu_objset_type(os);
3869 dmu_objset_rele(os, FTAG);
3871 if (ost == DMU_OST_ZFS)
3872 zfs_unmount_snap(zc->zc_name);
3874 if (strchr(zc->zc_name, '@')) {
3875 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3877 err = dsl_destroy_head(zc->zc_name);
3878 if (err == EEXIST) {
3880 * It is possible that the given DS may have
3881 * hidden child (%recv) datasets - "leftovers"
3882 * resulting from the previously interrupted
3885 * 6 extra bytes for /%recv
3887 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3889 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3890 zc->zc_name, recv_clone_name) >=
3892 return (SET_ERROR(EINVAL));
3895 * Try to remove the hidden child (%recv) and after
3896 * that try to remove the target dataset.
3897 * If the hidden child (%recv) does not exist
3898 * the original error (EEXIST) will be returned
3900 err = dsl_destroy_head(namebuf);
3902 err = dsl_destroy_head(zc->zc_name);
3903 else if (err == ENOENT)
3904 err = SET_ERROR(EEXIST);
3913 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
3914 * "initialize_vdevs": { -> guids to initialize (nvlist)
3915 * "vdev_path_1": vdev_guid_1, (uint64),
3916 * "vdev_path_2": vdev_guid_2, (uint64),
3922 * "initialize_vdevs": { -> initialization errors (nvlist)
3923 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3924 * "vdev_path_2": errno, ... (uint64)
3929 * EINVAL is returned for an unknown commands or if any of the provided vdev
3930 * guids have be specified with a type other than uint64.
3932 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
3933 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
3934 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
3938 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3941 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3943 return (SET_ERROR(EINVAL));
3946 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3947 cmd_type == POOL_INITIALIZE_START ||
3948 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3949 return (SET_ERROR(EINVAL));
3952 nvlist_t *vdev_guids;
3953 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3954 &vdev_guids) != 0) {
3955 return (SET_ERROR(EINVAL));
3958 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3959 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3961 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
3962 return (SET_ERROR(EINVAL));
3967 int error = spa_open(poolname, &spa, FTAG);
3971 nvlist_t *vdev_errlist = fnvlist_alloc();
3972 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
3975 if (fnvlist_size(vdev_errlist) > 0) {
3976 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3979 fnvlist_free(vdev_errlist);
3981 spa_close(spa, FTAG);
3982 return (total_errors > 0 ? EINVAL : 0);
3987 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
3988 * "trim_vdevs": { -> guids to TRIM (nvlist)
3989 * "vdev_path_1": vdev_guid_1, (uint64),
3990 * "vdev_path_2": vdev_guid_2, (uint64),
3993 * "trim_rate" -> Target TRIM rate in bytes/sec.
3994 * "trim_secure" -> Set to request a secure TRIM.
3998 * "trim_vdevs": { -> TRIM errors (nvlist)
3999 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4000 * "vdev_path_2": errno, ... (uint64)
4005 * EINVAL is returned for an unknown commands or if any of the provided vdev
4006 * guids have be specified with a type other than uint64.
4008 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4009 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4010 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4011 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4012 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4016 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4019 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4020 return (SET_ERROR(EINVAL));
4022 if (!(cmd_type == POOL_TRIM_CANCEL ||
4023 cmd_type == POOL_TRIM_START ||
4024 cmd_type == POOL_TRIM_SUSPEND)) {
4025 return (SET_ERROR(EINVAL));
4028 nvlist_t *vdev_guids;
4029 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4030 return (SET_ERROR(EINVAL));
4032 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4033 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4035 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4036 return (SET_ERROR(EINVAL));
4040 /* Optional, defaults to maximum rate when not provided */
4042 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4045 /* Optional, defaults to standard TRIM when not provided */
4047 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4053 int error = spa_open(poolname, &spa, FTAG);
4057 nvlist_t *vdev_errlist = fnvlist_alloc();
4058 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4059 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4061 if (fnvlist_size(vdev_errlist) > 0)
4062 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4064 fnvlist_free(vdev_errlist);
4066 spa_close(spa, FTAG);
4067 return (total_errors > 0 ? EINVAL : 0);
4071 * This ioctl waits for activity of a particular type to complete. If there is
4072 * no activity of that type in progress, it returns immediately, and the
4073 * returned value "waited" is false. If there is activity in progress, and no
4074 * tag is passed in, the ioctl blocks until all activity of that type is
4075 * complete, and then returns with "waited" set to true.
4077 * If a tag is provided, it identifies a particular instance of an activity to
4078 * wait for. Currently, this is only valid for use with 'initialize', because
4079 * that is the only activity for which there can be multiple instances running
4080 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4081 * the vdev on which to wait.
4083 * If a thread waiting in the ioctl receives a signal, the call will return
4084 * immediately, and the return value will be EINTR.
4087 * "wait_activity" -> int32_t
4088 * (optional) "wait_tag" -> uint64_t
4091 * outnvl: "waited" -> boolean_t
4093 static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4094 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4095 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4099 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4106 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4109 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4110 error = spa_wait_tag(name, activity, tag, &waited);
4112 error = spa_wait(name, activity, &waited);
4115 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4121 * This ioctl waits for activity of a particular type to complete. If there is
4122 * no activity of that type in progress, it returns immediately, and the
4123 * returned value "waited" is false. If there is activity in progress, and no
4124 * tag is passed in, the ioctl blocks until all activity of that type is
4125 * complete, and then returns with "waited" set to true.
4127 * If a thread waiting in the ioctl receives a signal, the call will return
4128 * immediately, and the return value will be EINTR.
4131 * "wait_activity" -> int32_t
4134 * outnvl: "waited" -> boolean_t
4136 static const zfs_ioc_key_t zfs_keys_fs_wait[] = {
4137 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4141 zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4144 boolean_t waited = B_FALSE;
4150 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0)
4151 return (SET_ERROR(EINVAL));
4153 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0)
4154 return (SET_ERROR(EINVAL));
4156 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
4159 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
4160 dsl_pool_rele(dp, FTAG);
4165 mutex_enter(&dd->dd_activity_lock);
4166 dd->dd_activity_waiters++;
4169 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t
4170 * aren't evicted while we're waiting. Normally this is prevented by
4171 * holding the pool, but we can't do that while we're waiting since
4172 * that would prevent TXGs from syncing out. Some of the functionality
4173 * of long-holds (e.g. preventing deletion) is unnecessary for this
4174 * case, since we would cancel the waiters before proceeding with a
4175 * deletion. An alternative mechanism for keeping the dataset around
4176 * could be developed but this is simpler.
4178 dsl_dataset_long_hold(ds, FTAG);
4179 dsl_pool_rele(dp, FTAG);
4181 error = dsl_dir_wait(dd, ds, activity, &waited);
4183 dsl_dataset_long_rele(ds, FTAG);
4184 dd->dd_activity_waiters--;
4185 if (dd->dd_activity_waiters == 0)
4186 cv_signal(&dd->dd_activity_cv);
4187 mutex_exit(&dd->dd_activity_lock);
4189 dsl_dataset_rele(ds, FTAG);
4192 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited);
4198 * fsname is name of dataset to rollback (to most recent snapshot)
4200 * innvl may contain name of expected target snapshot
4202 * outnvl: "target" -> name of most recent snapshot
4205 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4206 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4211 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4214 zvol_state_handle_t *zv;
4215 char *target = NULL;
4218 (void) nvlist_lookup_string(innvl, "target", &target);
4219 if (target != NULL) {
4220 const char *cp = strchr(target, '@');
4223 * The snap name must contain an @, and the part after it must
4224 * contain only valid characters.
4227 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4228 return (SET_ERROR(EINVAL));
4231 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4234 ds = dmu_objset_ds(zfsvfs->z_os);
4235 error = zfs_suspend_fs(zfsvfs);
4239 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4241 resume_err = zfs_resume_fs(zfsvfs, ds);
4242 error = error ? error : resume_err;
4244 zfs_vfs_rele(zfsvfs);
4245 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4246 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4250 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4256 recursive_unmount(const char *fsname, void *arg)
4258 const char *snapname = arg;
4261 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4262 zfs_unmount_snap(fullname);
4263 kmem_strfree(fullname);
4270 * snapname is the snapshot to redact.
4272 * "bookname" -> (string)
4273 * shortname of the redaction bookmark to generate
4274 * "snapnv" -> (nvlist, values ignored)
4275 * snapshots to redact snapname with respect to
4282 static const zfs_ioc_key_t zfs_keys_redact[] = {
4283 {"bookname", DATA_TYPE_STRING, 0},
4284 {"snapnv", DATA_TYPE_NVLIST, 0},
4287 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4289 nvlist_t *redactnvl = NULL;
4290 char *redactbook = NULL;
4292 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4293 return (SET_ERROR(EINVAL));
4294 if (fnvlist_num_pairs(redactnvl) == 0)
4295 return (SET_ERROR(ENXIO));
4296 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4297 return (SET_ERROR(EINVAL));
4299 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4304 * zc_name old name of dataset
4305 * zc_value new name of dataset
4306 * zc_cookie recursive flag (only valid for snapshots)
4311 zfs_ioc_rename(zfs_cmd_t *zc)
4314 dmu_objset_type_t ost;
4315 boolean_t recursive = zc->zc_cookie & 1;
4316 boolean_t nounmount = !!(zc->zc_cookie & 2);
4320 /* "zfs rename" from and to ...%recv datasets should both fail */
4321 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4322 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4323 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4324 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4325 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4326 return (SET_ERROR(EINVAL));
4328 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4331 ost = dmu_objset_type(os);
4332 dmu_objset_rele(os, FTAG);
4334 at = strchr(zc->zc_name, '@');
4336 /* snaps must be in same fs */
4339 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4340 return (SET_ERROR(EXDEV));
4342 if (ost == DMU_OST_ZFS && !nounmount) {
4343 error = dmu_objset_find(zc->zc_name,
4344 recursive_unmount, at + 1,
4345 recursive ? DS_FIND_CHILDREN : 0);
4351 error = dsl_dataset_rename_snapshot(zc->zc_name,
4352 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4357 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4362 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4364 const char *propname = nvpair_name(pair);
4365 boolean_t issnap = (strchr(dsname, '@') != NULL);
4366 zfs_prop_t prop = zfs_name_to_prop(propname);
4367 uint64_t intval, compval;
4370 if (prop == ZPROP_INVAL) {
4371 if (zfs_prop_user(propname)) {
4372 if ((err = zfs_secpolicy_write_perms(dsname,
4373 ZFS_DELEG_PERM_USERPROP, cr)))
4378 if (!issnap && zfs_prop_userquota(propname)) {
4379 const char *perm = NULL;
4380 const char *uq_prefix =
4381 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4382 const char *gq_prefix =
4383 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4384 const char *uiq_prefix =
4385 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4386 const char *giq_prefix =
4387 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4388 const char *pq_prefix =
4389 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4390 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4391 ZFS_PROP_PROJECTOBJQUOTA];
4393 if (strncmp(propname, uq_prefix,
4394 strlen(uq_prefix)) == 0) {
4395 perm = ZFS_DELEG_PERM_USERQUOTA;
4396 } else if (strncmp(propname, uiq_prefix,
4397 strlen(uiq_prefix)) == 0) {
4398 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4399 } else if (strncmp(propname, gq_prefix,
4400 strlen(gq_prefix)) == 0) {
4401 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4402 } else if (strncmp(propname, giq_prefix,
4403 strlen(giq_prefix)) == 0) {
4404 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4405 } else if (strncmp(propname, pq_prefix,
4406 strlen(pq_prefix)) == 0) {
4407 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4408 } else if (strncmp(propname, piq_prefix,
4409 strlen(piq_prefix)) == 0) {
4410 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4412 /* {USER|GROUP|PROJECT}USED are read-only */
4413 return (SET_ERROR(EINVAL));
4416 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4421 return (SET_ERROR(EINVAL));
4425 return (SET_ERROR(EINVAL));
4427 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4429 * dsl_prop_get_all_impl() returns properties in this
4433 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4434 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4439 * Check that this value is valid for this pool version
4442 case ZFS_PROP_COMPRESSION:
4444 * If the user specified gzip compression, make sure
4445 * the SPA supports it. We ignore any errors here since
4446 * we'll catch them later.
4448 if (nvpair_value_uint64(pair, &intval) == 0) {
4449 compval = ZIO_COMPRESS_ALGO(intval);
4450 if (compval >= ZIO_COMPRESS_GZIP_1 &&
4451 compval <= ZIO_COMPRESS_GZIP_9 &&
4452 zfs_earlier_version(dsname,
4453 SPA_VERSION_GZIP_COMPRESSION)) {
4454 return (SET_ERROR(ENOTSUP));
4457 if (compval == ZIO_COMPRESS_ZLE &&
4458 zfs_earlier_version(dsname,
4459 SPA_VERSION_ZLE_COMPRESSION))
4460 return (SET_ERROR(ENOTSUP));
4462 if (compval == ZIO_COMPRESS_LZ4) {
4465 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4468 if (!spa_feature_is_enabled(spa,
4469 SPA_FEATURE_LZ4_COMPRESS)) {
4470 spa_close(spa, FTAG);
4471 return (SET_ERROR(ENOTSUP));
4473 spa_close(spa, FTAG);
4476 if (compval == ZIO_COMPRESS_ZSTD) {
4479 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4482 if (!spa_feature_is_enabled(spa,
4483 SPA_FEATURE_ZSTD_COMPRESS)) {
4484 spa_close(spa, FTAG);
4485 return (SET_ERROR(ENOTSUP));
4487 spa_close(spa, FTAG);
4492 case ZFS_PROP_COPIES:
4493 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4494 return (SET_ERROR(ENOTSUP));
4497 case ZFS_PROP_VOLBLOCKSIZE:
4498 case ZFS_PROP_RECORDSIZE:
4499 /* Record sizes above 128k need the feature to be enabled */
4500 if (nvpair_value_uint64(pair, &intval) == 0 &&
4501 intval > SPA_OLD_MAXBLOCKSIZE) {
4505 * We don't allow setting the property above 1MB,
4506 * unless the tunable has been changed.
4508 if (intval > zfs_max_recordsize ||
4509 intval > SPA_MAXBLOCKSIZE)
4510 return (SET_ERROR(ERANGE));
4512 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4515 if (!spa_feature_is_enabled(spa,
4516 SPA_FEATURE_LARGE_BLOCKS)) {
4517 spa_close(spa, FTAG);
4518 return (SET_ERROR(ENOTSUP));
4520 spa_close(spa, FTAG);
4524 case ZFS_PROP_DNODESIZE:
4525 /* Dnode sizes above 512 need the feature to be enabled */
4526 if (nvpair_value_uint64(pair, &intval) == 0 &&
4527 intval != ZFS_DNSIZE_LEGACY) {
4530 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4533 if (!spa_feature_is_enabled(spa,
4534 SPA_FEATURE_LARGE_DNODE)) {
4535 spa_close(spa, FTAG);
4536 return (SET_ERROR(ENOTSUP));
4538 spa_close(spa, FTAG);
4542 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4544 * This property could require the allocation classes
4545 * feature to be active for setting, however we allow
4546 * it so that tests of settable properties succeed.
4547 * The CLI will issue a warning in this case.
4551 case ZFS_PROP_SHARESMB:
4552 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4553 return (SET_ERROR(ENOTSUP));
4556 case ZFS_PROP_ACLINHERIT:
4557 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4558 nvpair_value_uint64(pair, &intval) == 0) {
4559 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4560 zfs_earlier_version(dsname,
4561 SPA_VERSION_PASSTHROUGH_X))
4562 return (SET_ERROR(ENOTSUP));
4565 case ZFS_PROP_CHECKSUM:
4566 case ZFS_PROP_DEDUP:
4568 spa_feature_t feature;
4572 /* dedup feature version checks */
4573 if (prop == ZFS_PROP_DEDUP &&
4574 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4575 return (SET_ERROR(ENOTSUP));
4577 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4578 nvpair_value_uint64(pair, &intval) == 0) {
4579 /* check prop value is enabled in features */
4580 feature = zio_checksum_to_feature(
4581 intval & ZIO_CHECKSUM_MASK);
4582 if (feature == SPA_FEATURE_NONE)
4585 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4588 if (!spa_feature_is_enabled(spa, feature)) {
4589 spa_close(spa, FTAG);
4590 return (SET_ERROR(ENOTSUP));
4592 spa_close(spa, FTAG);
4601 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4605 * Removes properties from the given props list that fail permission checks
4606 * needed to clear them and to restore them in case of a receive error. For each
4607 * property, make sure we have both set and inherit permissions.
4609 * Returns the first error encountered if any permission checks fail. If the
4610 * caller provides a non-NULL errlist, it also gives the complete list of names
4611 * of all the properties that failed a permission check along with the
4612 * corresponding error numbers. The caller is responsible for freeing the
4615 * If every property checks out successfully, zero is returned and the list
4616 * pointed at by errlist is NULL.
4619 zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist)
4622 nvpair_t *pair, *next_pair;
4629 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4631 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4632 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4633 pair = nvlist_next_nvpair(props, NULL);
4634 while (pair != NULL) {
4635 next_pair = nvlist_next_nvpair(props, pair);
4637 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4638 sizeof (zc->zc_value));
4639 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4640 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4641 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4642 VERIFY(nvlist_add_int32(errors,
4643 zc->zc_value, err) == 0);
4647 kmem_free(zc, sizeof (zfs_cmd_t));
4649 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4650 nvlist_free(errors);
4653 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4656 if (errlist == NULL)
4657 nvlist_free(errors);
4665 propval_equals(nvpair_t *p1, nvpair_t *p2)
4667 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4668 /* dsl_prop_get_all_impl() format */
4670 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4671 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4675 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4677 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4678 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4682 if (nvpair_type(p1) != nvpair_type(p2))
4685 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4686 char *valstr1, *valstr2;
4688 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4689 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4690 return (strcmp(valstr1, valstr2) == 0);
4692 uint64_t intval1, intval2;
4694 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4695 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4696 return (intval1 == intval2);
4701 * Remove properties from props if they are not going to change (as determined
4702 * by comparison with origprops). Remove them from origprops as well, since we
4703 * do not need to clear or restore properties that won't change.
4706 props_reduce(nvlist_t *props, nvlist_t *origprops)
4708 nvpair_t *pair, *next_pair;
4710 if (origprops == NULL)
4711 return; /* all props need to be received */
4713 pair = nvlist_next_nvpair(props, NULL);
4714 while (pair != NULL) {
4715 const char *propname = nvpair_name(pair);
4718 next_pair = nvlist_next_nvpair(props, pair);
4720 if ((nvlist_lookup_nvpair(origprops, propname,
4721 &match) != 0) || !propval_equals(pair, match))
4722 goto next; /* need to set received value */
4724 /* don't clear the existing received value */
4725 (void) nvlist_remove_nvpair(origprops, match);
4726 /* don't bother receiving the property */
4727 (void) nvlist_remove_nvpair(props, pair);
4734 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4735 * For example, refquota cannot be set until after the receipt of a dataset,
4736 * because in replication streams, an older/earlier snapshot may exceed the
4737 * refquota. We want to receive the older/earlier snapshot, but setting
4738 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4739 * the older/earlier snapshot from being received (with EDQUOT).
4741 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4743 * libzfs will need to be judicious handling errors encountered by props
4744 * extracted by this function.
4747 extract_delay_props(nvlist_t *props)
4749 nvlist_t *delayprops;
4750 nvpair_t *nvp, *tmp;
4751 static const zfs_prop_t delayable[] = {
4753 ZFS_PROP_KEYLOCATION,
4758 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4760 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4761 nvp = nvlist_next_nvpair(props, nvp)) {
4763 * strcmp() is safe because zfs_prop_to_name() always returns
4766 for (i = 0; delayable[i] != 0; i++) {
4767 if (strcmp(zfs_prop_to_name(delayable[i]),
4768 nvpair_name(nvp)) == 0) {
4772 if (delayable[i] != 0) {
4773 tmp = nvlist_prev_nvpair(props, nvp);
4774 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4775 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4780 if (nvlist_empty(delayprops)) {
4781 nvlist_free(delayprops);
4784 return (delayprops);
4788 zfs_allow_log_destroy(void *arg)
4790 char *poolname = arg;
4792 if (poolname != NULL)
4793 kmem_strfree(poolname);
4797 static boolean_t zfs_ioc_recv_inject_err;
4801 * nvlist 'errors' is always allocated. It will contain descriptions of
4802 * encountered errors, if any. It's the callers responsibility to free.
4805 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4806 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4807 boolean_t resumable, int input_fd,
4808 dmu_replay_record_t *begin_record, uint64_t *read_bytes,
4809 uint64_t *errflags, nvlist_t **errors)
4811 dmu_recv_cookie_t drc;
4813 int props_error = 0;
4815 nvlist_t *local_delayprops = NULL;
4816 nvlist_t *recv_delayprops = NULL;
4817 nvlist_t *origprops = NULL; /* existing properties */
4818 nvlist_t *origrecvd = NULL; /* existing received properties */
4819 boolean_t first_recvd_props = B_FALSE;
4820 boolean_t tofs_was_redacted;
4821 zfs_file_t *input_fp;
4825 *errors = fnvlist_alloc();
4828 if ((error = zfs_file_get(input_fd, &input_fp)))
4831 noff = off = zfs_file_off(input_fp);
4832 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4833 resumable, localprops, hidden_args, origin, &drc, input_fp,
4837 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
4840 * Set properties before we receive the stream so that they are applied
4841 * to the new data. Note that we must call dmu_recv_stream() if
4842 * dmu_recv_begin() succeeds.
4844 if (recvprops != NULL && !drc.drc_newfs) {
4845 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4846 SPA_VERSION_RECVD_PROPS &&
4847 !dsl_prop_get_hasrecvd(tofs))
4848 first_recvd_props = B_TRUE;
4851 * If new received properties are supplied, they are to
4852 * completely replace the existing received properties,
4853 * so stash away the existing ones.
4855 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4856 nvlist_t *errlist = NULL;
4858 * Don't bother writing a property if its value won't
4859 * change (and avoid the unnecessary security checks).
4861 * The first receive after SPA_VERSION_RECVD_PROPS is a
4862 * special case where we blow away all local properties
4865 if (!first_recvd_props)
4866 props_reduce(recvprops, origrecvd);
4867 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4868 (void) nvlist_merge(*errors, errlist, 0);
4869 nvlist_free(errlist);
4871 if (clear_received_props(tofs, origrecvd,
4872 first_recvd_props ? NULL : recvprops) != 0)
4873 *errflags |= ZPROP_ERR_NOCLEAR;
4875 *errflags |= ZPROP_ERR_NOCLEAR;
4880 * Stash away existing properties so we can restore them on error unless
4881 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4882 * case "origrecvd" will take care of that.
4884 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4886 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4887 if (dsl_prop_get_all(os, &origprops) != 0) {
4888 *errflags |= ZPROP_ERR_NOCLEAR;
4890 dmu_objset_rele(os, FTAG);
4892 *errflags |= ZPROP_ERR_NOCLEAR;
4896 if (recvprops != NULL) {
4897 props_error = dsl_prop_set_hasrecvd(tofs);
4899 if (props_error == 0) {
4900 recv_delayprops = extract_delay_props(recvprops);
4901 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4902 recvprops, *errors);
4906 if (localprops != NULL) {
4907 nvlist_t *oprops = fnvlist_alloc();
4908 nvlist_t *xprops = fnvlist_alloc();
4909 nvpair_t *nvp = NULL;
4911 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4912 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4914 const char *name = nvpair_name(nvp);
4915 zfs_prop_t prop = zfs_name_to_prop(name);
4916 if (prop != ZPROP_INVAL) {
4917 if (!zfs_prop_inheritable(prop))
4919 } else if (!zfs_prop_user(name))
4921 fnvlist_add_boolean(xprops, name);
4923 /* -o property=value */
4924 fnvlist_add_nvpair(oprops, nvp);
4928 local_delayprops = extract_delay_props(oprops);
4929 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4931 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4934 nvlist_free(oprops);
4935 nvlist_free(xprops);
4938 error = dmu_recv_stream(&drc, &off);
4941 zfsvfs_t *zfsvfs = NULL;
4942 zvol_state_handle_t *zv = NULL;
4944 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4948 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
4949 begin_record->drr_u.drr_begin.
4950 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
4952 ds = dmu_objset_ds(zfsvfs->z_os);
4953 error = zfs_suspend_fs(zfsvfs);
4955 * If the suspend fails, then the recv_end will
4956 * likely also fail, and clean up after itself.
4958 end_err = dmu_recv_end(&drc, zfsvfs);
4960 * If the dataset was not redacted, but we received a
4961 * redacted stream onto it, we need to unmount the
4962 * dataset. Otherwise, resume the filesystem.
4964 if (error == 0 && !drc.drc_newfs &&
4965 stream_is_redacted && !tofs_was_redacted) {
4966 error = zfs_end_fs(zfsvfs, ds);
4967 } else if (error == 0) {
4968 error = zfs_resume_fs(zfsvfs, ds);
4970 error = error ? error : end_err;
4971 zfs_vfs_rele(zfsvfs);
4972 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4973 error = dmu_recv_end(&drc, zvol_tag(zv));
4976 error = dmu_recv_end(&drc, NULL);
4979 /* Set delayed properties now, after we're done receiving. */
4980 if (recv_delayprops != NULL && error == 0) {
4981 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4982 recv_delayprops, *errors);
4984 if (local_delayprops != NULL && error == 0) {
4985 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4986 local_delayprops, *errors);
4991 * Merge delayed props back in with initial props, in case
4992 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4993 * we have to make sure clear_received_props() includes
4994 * the delayed properties).
4996 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4997 * using ASSERT() will be just like a VERIFY.
4999 if (recv_delayprops != NULL) {
5000 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
5001 nvlist_free(recv_delayprops);
5003 if (local_delayprops != NULL) {
5004 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
5005 nvlist_free(local_delayprops);
5007 *read_bytes = off - noff;
5010 if (zfs_ioc_recv_inject_err) {
5011 zfs_ioc_recv_inject_err = B_FALSE;
5017 * On error, restore the original props.
5019 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
5020 if (clear_received_props(tofs, recvprops, NULL) != 0) {
5022 * We failed to clear the received properties.
5023 * Since we may have left a $recvd value on the
5024 * system, we can't clear the $hasrecvd flag.
5026 *errflags |= ZPROP_ERR_NORESTORE;
5027 } else if (first_recvd_props) {
5028 dsl_prop_unset_hasrecvd(tofs);
5031 if (origrecvd == NULL && !drc.drc_newfs) {
5032 /* We failed to stash the original properties. */
5033 *errflags |= ZPROP_ERR_NORESTORE;
5037 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5038 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5039 * explicitly if we're restoring local properties cleared in the
5040 * first new-style receive.
5042 if (origrecvd != NULL &&
5043 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5044 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5045 origrecvd, NULL) != 0) {
5047 * We stashed the original properties but failed to
5050 *errflags |= ZPROP_ERR_NORESTORE;
5053 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5054 !first_recvd_props) {
5056 nvlist_t *inheritprops;
5059 if (origprops == NULL) {
5060 /* We failed to stash the original properties. */
5061 *errflags |= ZPROP_ERR_NORESTORE;
5065 /* Restore original props */
5066 setprops = fnvlist_alloc();
5067 inheritprops = fnvlist_alloc();
5069 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5070 const char *name = nvpair_name(nvp);
5074 if (!nvlist_exists(origprops, name)) {
5076 * Property was not present or was explicitly
5077 * inherited before the receive, restore this.
5079 fnvlist_add_boolean(inheritprops, name);
5082 attrs = fnvlist_lookup_nvlist(origprops, name);
5083 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5085 /* Skip received properties */
5086 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5089 if (strcmp(source, tofs) == 0) {
5090 /* Property was locally set */
5091 fnvlist_add_nvlist(setprops, name, attrs);
5093 /* Property was implicitly inherited */
5094 fnvlist_add_boolean(inheritprops, name);
5098 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5100 *errflags |= ZPROP_ERR_NORESTORE;
5101 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5103 *errflags |= ZPROP_ERR_NORESTORE;
5105 nvlist_free(setprops);
5106 nvlist_free(inheritprops);
5109 zfs_file_put(input_fd);
5110 nvlist_free(origrecvd);
5111 nvlist_free(origprops);
5114 error = props_error;
5121 * zc_name name of containing filesystem (unused)
5122 * zc_nvlist_src{_size} nvlist of properties to apply
5123 * zc_nvlist_conf{_size} nvlist of properties to exclude
5124 * (DATA_TYPE_BOOLEAN) and override (everything else)
5125 * zc_value name of snapshot to create
5126 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5127 * zc_cookie file descriptor to recv from
5128 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5129 * zc_guid force flag
5132 * zc_cookie number of bytes read
5133 * zc_obj zprop_errflags_t
5134 * zc_nvlist_dst{_size} error for each unapplied received property
5137 zfs_ioc_recv(zfs_cmd_t *zc)
5139 dmu_replay_record_t begin_record;
5140 nvlist_t *errors = NULL;
5141 nvlist_t *recvdprops = NULL;
5142 nvlist_t *localprops = NULL;
5143 char *origin = NULL;
5145 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5148 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5149 strchr(zc->zc_value, '@') == NULL ||
5150 strchr(zc->zc_value, '%'))
5151 return (SET_ERROR(EINVAL));
5153 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5154 tosnap = strchr(tofs, '@');
5157 if (zc->zc_nvlist_src != 0 &&
5158 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5159 zc->zc_iflags, &recvdprops)) != 0)
5162 if (zc->zc_nvlist_conf != 0 &&
5163 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5164 zc->zc_iflags, &localprops)) != 0)
5167 if (zc->zc_string[0])
5168 origin = zc->zc_string;
5170 begin_record.drr_type = DRR_BEGIN;
5171 begin_record.drr_payloadlen = 0;
5172 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5174 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5175 NULL, zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
5176 &zc->zc_cookie, &zc->zc_obj, &errors);
5177 nvlist_free(recvdprops);
5178 nvlist_free(localprops);
5181 * Now that all props, initial and delayed, are set, report the prop
5182 * errors to the caller.
5184 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5185 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5186 put_nvlist(zc, errors) != 0)) {
5188 * Caller made zc->zc_nvlist_dst less than the minimum expected
5189 * size or supplied an invalid address.
5191 error = SET_ERROR(EINVAL);
5194 nvlist_free(errors);
5201 * "snapname" -> full name of the snapshot to create
5202 * (optional) "props" -> received properties to set (nvlist)
5203 * (optional) "localprops" -> override and exclude properties (nvlist)
5204 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5205 * "begin_record" -> non-byteswapped dmu_replay_record_t
5206 * "input_fd" -> file descriptor to read stream from (int32)
5207 * (optional) "force" -> force flag (value ignored)
5208 * (optional) "resumable" -> resumable flag (value ignored)
5209 * (optional) "cleanup_fd" -> unused
5210 * (optional) "action_handle" -> unused
5211 * (optional) "hidden_args" -> { "wkeydata" -> value }
5215 * "read_bytes" -> number of bytes read
5216 * "error_flags" -> zprop_errflags_t
5217 * "errors" -> error for each unapplied received property (nvlist)
5220 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5221 {"snapname", DATA_TYPE_STRING, 0},
5222 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5223 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5224 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5225 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5226 {"input_fd", DATA_TYPE_INT32, 0},
5227 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5228 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5229 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5230 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5231 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5235 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5237 dmu_replay_record_t *begin_record;
5238 uint_t begin_record_size;
5239 nvlist_t *errors = NULL;
5240 nvlist_t *recvprops = NULL;
5241 nvlist_t *localprops = NULL;
5242 nvlist_t *hidden_args = NULL;
5244 char *origin = NULL;
5246 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5248 boolean_t resumable;
5249 uint64_t read_bytes = 0;
5250 uint64_t errflags = 0;
5254 snapname = fnvlist_lookup_string(innvl, "snapname");
5256 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5257 strchr(snapname, '@') == NULL ||
5258 strchr(snapname, '%'))
5259 return (SET_ERROR(EINVAL));
5261 (void) strlcpy(tofs, snapname, sizeof (tofs));
5262 tosnap = strchr(tofs, '@');
5265 error = nvlist_lookup_string(innvl, "origin", &origin);
5266 if (error && error != ENOENT)
5269 error = nvlist_lookup_byte_array(innvl, "begin_record",
5270 (uchar_t **)&begin_record, &begin_record_size);
5271 if (error != 0 || begin_record_size != sizeof (*begin_record))
5272 return (SET_ERROR(EINVAL));
5274 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5276 force = nvlist_exists(innvl, "force");
5277 resumable = nvlist_exists(innvl, "resumable");
5279 /* we still use "props" here for backwards compatibility */
5280 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5281 if (error && error != ENOENT)
5284 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5285 if (error && error != ENOENT)
5288 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5289 if (error && error != ENOENT)
5292 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5293 hidden_args, force, resumable, input_fd, begin_record,
5294 &read_bytes, &errflags, &errors);
5296 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5297 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5298 fnvlist_add_nvlist(outnvl, "errors", errors);
5300 nvlist_free(errors);
5301 nvlist_free(recvprops);
5302 nvlist_free(localprops);
5307 typedef struct dump_bytes_io {
5315 dump_bytes_cb(void *arg)
5317 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
5324 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL);
5328 dump_bytes(objset_t *os, void *buf, int len, void *arg)
5330 dump_bytes_io_t dbi;
5336 #if defined(HAVE_LARGE_STACKS)
5337 dump_bytes_cb(&dbi);
5340 * The vn_rdwr() call is performed in a taskq to ensure that there is
5341 * always enough stack space to write safely to the target filesystem.
5342 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5343 * them and they are used in vdev_file.c for a similar purpose.
5345 spa_taskq_dispatch_sync(dmu_objset_spa(os), ZIO_TYPE_FREE,
5346 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
5347 #endif /* HAVE_LARGE_STACKS */
5349 return (dbi.dbi_err);
5354 * zc_name name of snapshot to send
5355 * zc_cookie file descriptor to send stream to
5356 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5357 * zc_sendobj objsetid of snapshot to send
5358 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5359 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5360 * output size in zc_objset_type.
5361 * zc_flags lzc_send_flags
5364 * zc_objset_type estimated size, if zc_guid is set
5366 * NOTE: This is no longer the preferred interface, any new functionality
5367 * should be added to zfs_ioc_send_new() instead.
5370 zfs_ioc_send(zfs_cmd_t *zc)
5374 boolean_t estimate = (zc->zc_guid != 0);
5375 boolean_t embedok = (zc->zc_flags & 0x1);
5376 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5377 boolean_t compressok = (zc->zc_flags & 0x4);
5378 boolean_t rawok = (zc->zc_flags & 0x8);
5379 boolean_t savedok = (zc->zc_flags & 0x10);
5381 if (zc->zc_obj != 0) {
5383 dsl_dataset_t *tosnap;
5385 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5389 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5391 dsl_pool_rele(dp, FTAG);
5395 if (dsl_dir_is_clone(tosnap->ds_dir))
5397 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5398 dsl_dataset_rele(tosnap, FTAG);
5399 dsl_pool_rele(dp, FTAG);
5404 dsl_dataset_t *tosnap;
5405 dsl_dataset_t *fromsnap = NULL;
5407 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5411 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5414 dsl_pool_rele(dp, FTAG);
5418 if (zc->zc_fromobj != 0) {
5419 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5422 dsl_dataset_rele(tosnap, FTAG);
5423 dsl_pool_rele(dp, FTAG);
5428 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
5429 compressok || rawok, savedok, &zc->zc_objset_type);
5431 if (fromsnap != NULL)
5432 dsl_dataset_rele(fromsnap, FTAG);
5433 dsl_dataset_rele(tosnap, FTAG);
5434 dsl_pool_rele(dp, FTAG);
5437 dmu_send_outparams_t out = {0};
5439 if ((error = zfs_file_get(zc->zc_cookie, &fp)))
5442 off = zfs_file_off(fp);
5443 out.dso_outfunc = dump_bytes;
5445 out.dso_dryrun = B_FALSE;
5446 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5447 zc->zc_fromobj, embedok, large_block_ok, compressok,
5448 rawok, savedok, zc->zc_cookie, &off, &out);
5450 zfs_file_put(zc->zc_cookie);
5457 * zc_name name of snapshot on which to report progress
5458 * zc_cookie file descriptor of send stream
5461 * zc_cookie number of bytes written in send stream thus far
5462 * zc_objset_type logical size of data traversed by send thus far
5465 zfs_ioc_send_progress(zfs_cmd_t *zc)
5469 dmu_sendstatus_t *dsp = NULL;
5472 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5476 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5478 dsl_pool_rele(dp, FTAG);
5482 mutex_enter(&ds->ds_sendstream_lock);
5485 * Iterate over all the send streams currently active on this dataset.
5486 * If there's one which matches the specified file descriptor _and_ the
5487 * stream was started by the current process, return the progress of
5491 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5492 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5493 if (dsp->dss_outfd == zc->zc_cookie &&
5494 zfs_proc_is_caller(dsp->dss_proc))
5499 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
5501 /* This is the closest thing we have to atomic_read_64. */
5502 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
5504 error = SET_ERROR(ENOENT);
5507 mutex_exit(&ds->ds_sendstream_lock);
5508 dsl_dataset_rele(ds, FTAG);
5509 dsl_pool_rele(dp, FTAG);
5514 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5518 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5519 &zc->zc_inject_record);
5522 zc->zc_guid = (uint64_t)id;
5528 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5530 return (zio_clear_fault((int)zc->zc_guid));
5534 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5536 int id = (int)zc->zc_guid;
5539 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5540 &zc->zc_inject_record);
5548 zfs_ioc_error_log(zfs_cmd_t *zc)
5552 size_t count = (size_t)zc->zc_nvlist_dst_size;
5554 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5557 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5560 zc->zc_nvlist_dst_size = count;
5562 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5564 spa_close(spa, FTAG);
5570 zfs_ioc_clear(zfs_cmd_t *zc)
5577 * On zpool clear we also fix up missing slogs
5579 mutex_enter(&spa_namespace_lock);
5580 spa = spa_lookup(zc->zc_name);
5582 mutex_exit(&spa_namespace_lock);
5583 return (SET_ERROR(EIO));
5585 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5586 /* we need to let spa_open/spa_load clear the chains */
5587 spa_set_log_state(spa, SPA_LOG_CLEAR);
5589 spa->spa_last_open_failed = 0;
5590 mutex_exit(&spa_namespace_lock);
5592 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5593 error = spa_open(zc->zc_name, &spa, FTAG);
5596 nvlist_t *config = NULL;
5598 if (zc->zc_nvlist_src == 0)
5599 return (SET_ERROR(EINVAL));
5601 if ((error = get_nvlist(zc->zc_nvlist_src,
5602 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5603 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5605 if (config != NULL) {
5608 if ((err = put_nvlist(zc, config)) != 0)
5610 nvlist_free(config);
5612 nvlist_free(policy);
5620 * If multihost is enabled, resuming I/O is unsafe as another
5621 * host may have imported the pool.
5623 if (spa_multihost(spa) && spa_suspended(spa))
5624 return (SET_ERROR(EINVAL));
5626 spa_vdev_state_enter(spa, SCL_NONE);
5628 if (zc->zc_guid == 0) {
5631 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5633 error = SET_ERROR(ENODEV);
5634 (void) spa_vdev_state_exit(spa, NULL, error);
5635 spa_close(spa, FTAG);
5640 vdev_clear(spa, vd);
5642 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5643 NULL : spa->spa_root_vdev, 0);
5646 * Resume any suspended I/Os.
5648 if (zio_resume(spa) != 0)
5649 error = SET_ERROR(EIO);
5651 spa_close(spa, FTAG);
5657 * Reopen all the vdevs associated with the pool.
5660 * "scrub_restart" -> when true and scrub is running, allow to restart
5661 * scrub as the side effect of the reopen (boolean).
5666 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5667 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
5672 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5676 boolean_t rc, scrub_restart = B_TRUE;
5679 error = nvlist_lookup_boolean_value(innvl,
5680 "scrub_restart", &rc);
5685 error = spa_open(pool, &spa, FTAG);
5689 spa_vdev_state_enter(spa, SCL_NONE);
5692 * If the scrub_restart flag is B_FALSE and a scrub is already
5693 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5694 * we don't restart the scrub as a side effect of the reopen.
5695 * Otherwise, let vdev_open() decided if a resilver is required.
5698 spa->spa_scrub_reopen = (!scrub_restart &&
5699 dsl_scan_scrubbing(spa->spa_dsl_pool));
5700 vdev_reopen(spa->spa_root_vdev);
5701 spa->spa_scrub_reopen = B_FALSE;
5703 (void) spa_vdev_state_exit(spa, NULL, 0);
5704 spa_close(spa, FTAG);
5710 * zc_name name of filesystem
5713 * zc_string name of conflicting snapshot, if there is one
5716 zfs_ioc_promote(zfs_cmd_t *zc)
5719 dsl_dataset_t *ds, *ods;
5720 char origin[ZFS_MAX_DATASET_NAME_LEN];
5724 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5725 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5726 strchr(zc->zc_name, '%'))
5727 return (SET_ERROR(EINVAL));
5729 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5733 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5735 dsl_pool_rele(dp, FTAG);
5739 if (!dsl_dir_is_clone(ds->ds_dir)) {
5740 dsl_dataset_rele(ds, FTAG);
5741 dsl_pool_rele(dp, FTAG);
5742 return (SET_ERROR(EINVAL));
5745 error = dsl_dataset_hold_obj(dp,
5746 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5748 dsl_dataset_rele(ds, FTAG);
5749 dsl_pool_rele(dp, FTAG);
5753 dsl_dataset_name(ods, origin);
5754 dsl_dataset_rele(ods, FTAG);
5755 dsl_dataset_rele(ds, FTAG);
5756 dsl_pool_rele(dp, FTAG);
5759 * We don't need to unmount *all* the origin fs's snapshots, but
5762 cp = strchr(origin, '@');
5765 (void) dmu_objset_find(origin,
5766 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5767 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5771 * Retrieve a single {user|group|project}{used|quota}@... property.
5774 * zc_name name of filesystem
5775 * zc_objset_type zfs_userquota_prop_t
5776 * zc_value domain name (eg. "S-1-234-567-89")
5777 * zc_guid RID/UID/GID
5780 * zc_cookie property value
5783 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5788 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5789 return (SET_ERROR(EINVAL));
5791 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5795 error = zfs_userspace_one(zfsvfs,
5796 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5797 zfsvfs_rele(zfsvfs, FTAG);
5804 * zc_name name of filesystem
5805 * zc_cookie zap cursor
5806 * zc_objset_type zfs_userquota_prop_t
5807 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5810 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5811 * zc_cookie zap cursor
5814 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5817 int bufsize = zc->zc_nvlist_dst_size;
5820 return (SET_ERROR(ENOMEM));
5822 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5826 void *buf = vmem_alloc(bufsize, KM_SLEEP);
5828 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5829 buf, &zc->zc_nvlist_dst_size);
5832 error = xcopyout(buf,
5833 (void *)(uintptr_t)zc->zc_nvlist_dst,
5834 zc->zc_nvlist_dst_size);
5836 vmem_free(buf, bufsize);
5837 zfsvfs_rele(zfsvfs, FTAG);
5844 * zc_name name of filesystem
5850 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5856 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5857 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5859 * If userused is not enabled, it may be because the
5860 * objset needs to be closed & reopened (to grow the
5861 * objset_phys_t). Suspend/resume the fs will do that.
5863 dsl_dataset_t *ds, *newds;
5865 ds = dmu_objset_ds(zfsvfs->z_os);
5866 error = zfs_suspend_fs(zfsvfs);
5868 dmu_objset_refresh_ownership(ds, &newds,
5870 error = zfs_resume_fs(zfsvfs, newds);
5874 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5875 zfs_vfs_rele(zfsvfs);
5877 /* XXX kind of reading contents without owning */
5878 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5882 error = dmu_objset_userspace_upgrade(os);
5883 dmu_objset_rele_flags(os, B_TRUE, FTAG);
5891 * zc_name name of filesystem
5897 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5902 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5906 if (dmu_objset_userobjspace_upgradable(os) ||
5907 dmu_objset_projectquota_upgradable(os)) {
5908 mutex_enter(&os->os_upgrade_lock);
5909 if (os->os_upgrade_id == 0) {
5910 /* clear potential error code and retry */
5911 os->os_upgrade_status = 0;
5912 mutex_exit(&os->os_upgrade_lock);
5914 dmu_objset_id_quota_upgrade(os);
5916 mutex_exit(&os->os_upgrade_lock);
5919 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5921 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5922 error = os->os_upgrade_status;
5924 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5927 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
5933 zfs_ioc_share(zfs_cmd_t *zc)
5935 return (SET_ERROR(ENOSYS));
5938 ace_t full_access[] = {
5939 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5944 * zc_name name of containing filesystem
5945 * zc_obj object # beyond which we want next in-use object #
5948 * zc_obj next in-use object #
5951 zfs_ioc_next_obj(zfs_cmd_t *zc)
5953 objset_t *os = NULL;
5956 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5960 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
5962 dmu_objset_rele(os, FTAG);
5968 * zc_name name of filesystem
5969 * zc_value prefix name for snapshot
5970 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5973 * zc_value short name of new snapshot
5976 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5983 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5987 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5988 (u_longlong_t)ddi_get_lbolt64());
5989 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5991 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5994 (void) strlcpy(zc->zc_value, snap_name,
5995 sizeof (zc->zc_value));
5996 kmem_strfree(snap_name);
5997 kmem_strfree(hold_name);
5998 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
6004 * zc_name name of "to" snapshot
6005 * zc_value name of "from" snapshot
6006 * zc_cookie file descriptor to write diff data on
6009 * dmu_diff_record_t's to the file descriptor
6012 zfs_ioc_diff(zfs_cmd_t *zc)
6018 if ((error = zfs_file_get(zc->zc_cookie, &fp)))
6021 off = zfs_file_off(fp);
6022 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
6024 zfs_file_put(zc->zc_cookie);
6030 zfs_ioc_smb_acl(zfs_cmd_t *zc)
6032 return (SET_ERROR(ENOTSUP));
6037 * "holds" -> { snapname -> holdname (string), ... }
6038 * (optional) "cleanup_fd" -> fd (int32)
6042 * snapname -> error value (int32)
6046 static const zfs_ioc_key_t zfs_keys_hold[] = {
6047 {"holds", DATA_TYPE_NVLIST, 0},
6048 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6053 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6057 int cleanup_fd = -1;
6061 holds = fnvlist_lookup_nvlist(args, "holds");
6063 /* make sure the user didn't pass us any invalid (empty) tags */
6064 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6065 pair = nvlist_next_nvpair(holds, pair)) {
6068 error = nvpair_value_string(pair, &htag);
6070 return (SET_ERROR(error));
6072 if (strlen(htag) == 0)
6073 return (SET_ERROR(EINVAL));
6076 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6077 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
6079 return (SET_ERROR(error));
6082 error = dsl_dataset_user_hold(holds, minor, errlist);
6084 zfs_onexit_fd_rele(cleanup_fd);
6085 return (SET_ERROR(error));
6089 * innvl is not used.
6092 * holdname -> time added (uint64 seconds since epoch)
6096 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6102 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6104 return (dsl_dataset_get_holds(snapname, outnvl));
6109 * snapname -> { holdname, ... }
6114 * snapname -> error value (int32)
6118 static const zfs_ioc_key_t zfs_keys_release[] = {
6119 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6124 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6126 return (dsl_dataset_user_release(holds, errlist));
6131 * zc_guid flags (ZEVENT_NONBLOCK)
6132 * zc_cleanup_fd zevent file descriptor
6135 * zc_nvlist_dst next nvlist event
6136 * zc_cookie dropped events since last get
6139 zfs_ioc_events_next(zfs_cmd_t *zc)
6142 nvlist_t *event = NULL;
6144 uint64_t dropped = 0;
6147 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6152 error = zfs_zevent_next(ze, &event,
6153 &zc->zc_nvlist_dst_size, &dropped);
6154 if (event != NULL) {
6155 zc->zc_cookie = dropped;
6156 error = put_nvlist(zc, event);
6160 if (zc->zc_guid & ZEVENT_NONBLOCK)
6163 if ((error == 0) || (error != ENOENT))
6166 error = zfs_zevent_wait(ze);
6171 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
6178 * zc_cookie cleared events count
6181 zfs_ioc_events_clear(zfs_cmd_t *zc)
6185 zfs_zevent_drain_all(&count);
6186 zc->zc_cookie = count;
6193 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6194 * zc_cleanup zevent file descriptor
6197 zfs_ioc_events_seek(zfs_cmd_t *zc)
6203 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6207 error = zfs_zevent_seek(ze, zc->zc_guid);
6208 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
6215 * zc_name name of later filesystem or snapshot
6216 * zc_value full name of old snapshot or bookmark
6219 * zc_cookie space in bytes
6220 * zc_objset_type compressed space in bytes
6221 * zc_perm_action uncompressed space in bytes
6224 zfs_ioc_space_written(zfs_cmd_t *zc)
6230 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6233 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6235 dsl_pool_rele(dp, FTAG);
6238 if (strchr(zc->zc_value, '#') != NULL) {
6239 zfs_bookmark_phys_t bmp;
6240 error = dsl_bookmark_lookup(dp, zc->zc_value,
6243 error = dsl_dataset_space_written_bookmark(&bmp, new,
6245 &zc->zc_objset_type, &zc->zc_perm_action);
6249 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6252 error = dsl_dataset_space_written(old, new,
6254 &zc->zc_objset_type, &zc->zc_perm_action);
6255 dsl_dataset_rele(old, FTAG);
6258 dsl_dataset_rele(new, FTAG);
6259 dsl_pool_rele(dp, FTAG);
6265 * "firstsnap" -> snapshot name
6269 * "used" -> space in bytes
6270 * "compressed" -> compressed space in bytes
6271 * "uncompressed" -> uncompressed space in bytes
6274 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6275 {"firstsnap", DATA_TYPE_STRING, 0},
6279 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6283 dsl_dataset_t *new, *old;
6285 uint64_t used, comp, uncomp;
6287 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6289 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6293 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6294 if (error == 0 && !new->ds_is_snapshot) {
6295 dsl_dataset_rele(new, FTAG);
6296 error = SET_ERROR(EINVAL);
6299 dsl_pool_rele(dp, FTAG);
6302 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6303 if (error == 0 && !old->ds_is_snapshot) {
6304 dsl_dataset_rele(old, FTAG);
6305 error = SET_ERROR(EINVAL);
6308 dsl_dataset_rele(new, FTAG);
6309 dsl_pool_rele(dp, FTAG);
6313 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6314 dsl_dataset_rele(old, FTAG);
6315 dsl_dataset_rele(new, FTAG);
6316 dsl_pool_rele(dp, FTAG);
6317 fnvlist_add_uint64(outnvl, "used", used);
6318 fnvlist_add_uint64(outnvl, "compressed", comp);
6319 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6325 * "fd" -> file descriptor to write stream to (int32)
6326 * (optional) "fromsnap" -> full snap name to send an incremental from
6327 * (optional) "largeblockok" -> (value ignored)
6328 * indicates that blocks > 128KB are permitted
6329 * (optional) "embedok" -> (value ignored)
6330 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6331 * (optional) "compressok" -> (value ignored)
6332 * presence indicates compressed DRR_WRITE records are permitted
6333 * (optional) "rawok" -> (value ignored)
6334 * presence indicates raw encrypted records should be used.
6335 * (optional) "savedok" -> (value ignored)
6336 * presence indicates we should send a partially received snapshot
6337 * (optional) "resume_object" and "resume_offset" -> (uint64)
6338 * if present, resume send stream from specified object and offset.
6339 * (optional) "redactbook" -> (string)
6340 * if present, use this bookmark's redaction list to generate a redacted
6346 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6347 {"fd", DATA_TYPE_INT32, 0},
6348 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6349 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6350 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6351 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6352 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6353 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6354 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6355 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6356 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6361 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6365 char *fromname = NULL;
6368 boolean_t largeblockok;
6370 boolean_t compressok;
6373 uint64_t resumeobj = 0;
6374 uint64_t resumeoff = 0;
6375 char *redactbook = NULL;
6377 fd = fnvlist_lookup_int32(innvl, "fd");
6379 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6381 largeblockok = nvlist_exists(innvl, "largeblockok");
6382 embedok = nvlist_exists(innvl, "embedok");
6383 compressok = nvlist_exists(innvl, "compressok");
6384 rawok = nvlist_exists(innvl, "rawok");
6385 savedok = nvlist_exists(innvl, "savedok");
6387 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6388 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6390 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6392 if ((error = zfs_file_get(fd, &fp)))
6395 off = zfs_file_off(fp);
6397 dmu_send_outparams_t out = {0};
6398 out.dso_outfunc = dump_bytes;
6400 out.dso_dryrun = B_FALSE;
6401 error = dmu_send(snapname, fromname, embedok, largeblockok,
6402 compressok, rawok, savedok, resumeobj, resumeoff,
6403 redactbook, fd, &off, &out);
6411 send_space_sum(objset_t *os, void *buf, int len, void *arg)
6413 uint64_t *size = arg;
6419 * Determine approximately how large a zfs send stream will be -- the number
6420 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6423 * (optional) "from" -> full snap or bookmark name to send an incremental
6425 * (optional) "largeblockok" -> (value ignored)
6426 * indicates that blocks > 128KB are permitted
6427 * (optional) "embedok" -> (value ignored)
6428 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6429 * (optional) "compressok" -> (value ignored)
6430 * presence indicates compressed DRR_WRITE records are permitted
6431 * (optional) "rawok" -> (value ignored)
6432 * presence indicates raw encrypted records should be used.
6433 * (optional) "resume_object" and "resume_offset" -> (uint64)
6434 * if present, resume send stream from specified object and offset.
6435 * (optional) "fd" -> file descriptor to use as a cookie for progress
6440 * "space" -> bytes of space (uint64)
6443 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6444 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6445 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6446 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6447 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6448 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6449 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6450 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6451 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6452 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6453 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6454 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
6458 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6461 dsl_dataset_t *tosnap;
6462 dsl_dataset_t *fromsnap = NULL;
6464 char *fromname = NULL;
6465 char *redactlist_book = NULL;
6466 boolean_t largeblockok;
6468 boolean_t compressok;
6472 boolean_t full_estimate = B_FALSE;
6473 uint64_t resumeobj = 0;
6474 uint64_t resumeoff = 0;
6475 uint64_t resume_bytes = 0;
6477 zfs_bookmark_phys_t zbm = {0};
6479 error = dsl_pool_hold(snapname, FTAG, &dp);
6483 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6485 dsl_pool_rele(dp, FTAG);
6488 (void) nvlist_lookup_int32(innvl, "fd", &fd);
6490 largeblockok = nvlist_exists(innvl, "largeblockok");
6491 embedok = nvlist_exists(innvl, "embedok");
6492 compressok = nvlist_exists(innvl, "compressok");
6493 rawok = nvlist_exists(innvl, "rawok");
6494 savedok = nvlist_exists(innvl, "savedok");
6495 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
6496 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
6497 &redactlist_book) == 0);
6499 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6500 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6501 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
6504 full_estimate = B_TRUE;
6506 if (strchr(fromname, '#')) {
6507 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
6510 * dsl_bookmark_lookup() will fail with EXDEV if
6511 * the from-bookmark and tosnap are at the same txg.
6512 * However, it's valid to do a send (and therefore,
6513 * a send estimate) from and to the same time point,
6514 * if the bookmark is redacted (the incremental send
6515 * can change what's redacted on the target). In
6516 * this case, dsl_bookmark_lookup() fills in zbm
6517 * but returns EXDEV. Ignore this error.
6519 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
6521 dsl_dataset_phys(tosnap)->ds_guid)
6525 dsl_dataset_rele(tosnap, FTAG);
6526 dsl_pool_rele(dp, FTAG);
6529 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
6530 ZBM_FLAG_HAS_FBN)) {
6531 full_estimate = B_TRUE;
6533 } else if (strchr(fromname, '@')) {
6534 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6536 dsl_dataset_rele(tosnap, FTAG);
6537 dsl_pool_rele(dp, FTAG);
6541 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
6542 full_estimate = B_TRUE;
6543 dsl_dataset_rele(fromsnap, FTAG);
6547 * from is not properly formatted as a snapshot or
6550 dsl_dataset_rele(tosnap, FTAG);
6551 dsl_pool_rele(dp, FTAG);
6552 return (SET_ERROR(EINVAL));
6556 if (full_estimate) {
6557 dmu_send_outparams_t out = {0};
6559 out.dso_outfunc = send_space_sum;
6560 out.dso_arg = &space;
6561 out.dso_dryrun = B_TRUE;
6563 * We have to release these holds so dmu_send can take them. It
6564 * will do all the error checking we need.
6566 dsl_dataset_rele(tosnap, FTAG);
6567 dsl_pool_rele(dp, FTAG);
6568 error = dmu_send(snapname, fromname, embedok, largeblockok,
6569 compressok, rawok, savedok, resumeobj, resumeoff,
6570 redactlist_book, fd, &off, &out);
6572 error = dmu_send_estimate_fast(tosnap, fromsnap,
6573 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
6574 compressok || rawok, savedok, &space);
6575 space -= resume_bytes;
6576 if (fromsnap != NULL)
6577 dsl_dataset_rele(fromsnap, FTAG);
6578 dsl_dataset_rele(tosnap, FTAG);
6579 dsl_pool_rele(dp, FTAG);
6582 fnvlist_add_uint64(outnvl, "space", space);
6588 * Sync the currently open TXG to disk for the specified pool.
6589 * This is somewhat similar to 'zfs_sync()'.
6590 * For cases that do not result in error this ioctl will wait for
6591 * the currently open TXG to commit before returning back to the caller.
6594 * "force" -> when true, force uberblock update even if there is no dirty data.
6595 * In addition this will cause the vdev configuration to be written
6596 * out including updating the zpool cache file. (boolean_t)
6601 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6602 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6607 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6610 boolean_t force = B_FALSE;
6613 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6617 force = fnvlist_lookup_boolean_value(innvl, "force");
6620 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6621 vdev_config_dirty(spa->spa_root_vdev);
6622 spa_config_exit(spa, SCL_CONFIG, FTAG);
6624 txg_wait_synced(spa_get_dsl(spa), 0);
6626 spa_close(spa, FTAG);
6632 * Load a user's wrapping key into the kernel.
6634 * "hidden_args" -> { "wkeydata" -> value }
6635 * raw uint8_t array of encryption wrapping key data (32 bytes)
6636 * (optional) "noop" -> (value ignored)
6637 * presence indicated key should only be verified, not loaded
6640 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6641 {"hidden_args", DATA_TYPE_NVLIST, 0},
6642 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6647 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6650 dsl_crypto_params_t *dcp = NULL;
6651 nvlist_t *hidden_args;
6652 boolean_t noop = nvlist_exists(innvl, "noop");
6654 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6655 ret = SET_ERROR(EINVAL);
6659 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6661 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6666 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6670 dsl_crypto_params_free(dcp, noop);
6675 dsl_crypto_params_free(dcp, B_TRUE);
6680 * Unload a user's wrapping key from the kernel.
6681 * Both innvl and outnvl are unused.
6683 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6689 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6693 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6694 ret = (SET_ERROR(EINVAL));
6698 ret = spa_keystore_unload_wkey(dsname);
6707 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6708 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6709 * here to change how the key is derived in userspace.
6712 * "hidden_args" (optional) -> { "wkeydata" -> value }
6713 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6714 * "props" (optional) -> { prop -> value }
6719 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6720 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6721 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6722 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6727 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6730 uint64_t cmd = DCP_CMD_NONE;
6731 dsl_crypto_params_t *dcp = NULL;
6732 nvlist_t *args = NULL, *hidden_args = NULL;
6734 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6735 ret = (SET_ERROR(EINVAL));
6739 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6740 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6741 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6743 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6747 ret = spa_keystore_change_key(dsname, dcp);
6751 dsl_crypto_params_free(dcp, B_FALSE);
6756 dsl_crypto_params_free(dcp, B_TRUE);
6760 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6763 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6764 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6765 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6767 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6769 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6770 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6771 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6772 ASSERT3P(vec->zvec_func, ==, NULL);
6774 vec->zvec_legacy_func = func;
6775 vec->zvec_secpolicy = secpolicy;
6776 vec->zvec_namecheck = namecheck;
6777 vec->zvec_allow_log = log_history;
6778 vec->zvec_pool_check = pool_check;
6782 * See the block comment at the beginning of this file for details on
6783 * each argument to this function.
6786 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6787 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6788 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6789 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6791 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6793 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6794 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6795 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6796 ASSERT3P(vec->zvec_func, ==, NULL);
6798 /* if we are logging, the name must be valid */
6799 ASSERT(!allow_log || namecheck != NO_NAME);
6801 vec->zvec_name = name;
6802 vec->zvec_func = func;
6803 vec->zvec_secpolicy = secpolicy;
6804 vec->zvec_namecheck = namecheck;
6805 vec->zvec_pool_check = pool_check;
6806 vec->zvec_smush_outnvlist = smush_outnvlist;
6807 vec->zvec_allow_log = allow_log;
6808 vec->zvec_nvl_keys = nvl_keys;
6809 vec->zvec_nvl_key_count = num_keys;
6813 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6814 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6815 zfs_ioc_poolcheck_t pool_check)
6817 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6818 POOL_NAME, log_history, pool_check);
6822 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6823 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6825 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6826 DATASET_NAME, B_FALSE, pool_check);
6830 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6832 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6833 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6837 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6838 zfs_secpolicy_func_t *secpolicy)
6840 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6841 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6845 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6846 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6848 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6849 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6853 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6855 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6856 zfs_secpolicy_read);
6860 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6861 zfs_secpolicy_func_t *secpolicy)
6863 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6864 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6868 zfs_ioctl_init(void)
6870 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6871 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6872 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6873 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6875 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6876 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6877 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6878 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6880 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6881 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6882 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6883 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6885 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6886 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6887 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6888 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6890 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6891 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6892 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6893 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6895 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6896 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6897 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6898 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6900 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6901 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6902 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6903 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6905 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6906 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
6907 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6908 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6910 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6911 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6912 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6913 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6915 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6916 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6917 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6918 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6919 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6920 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6921 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6922 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6924 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6925 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6926 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6927 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6929 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6930 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6931 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6932 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6934 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6935 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6936 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6937 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6939 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6940 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6941 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6942 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6944 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
6945 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
6946 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
6947 ARRAY_SIZE(zfs_keys_get_bookmark_props));
6949 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6950 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6952 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6953 zfs_keys_destroy_bookmarks,
6954 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6956 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6957 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
6958 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6959 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
6960 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6961 zfs_ioc_load_key, zfs_secpolicy_load_key,
6962 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6963 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
6964 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6965 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6966 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6967 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
6968 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6969 zfs_ioc_change_key, zfs_secpolicy_change_key,
6970 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6971 B_TRUE, B_TRUE, zfs_keys_change_key,
6972 ARRAY_SIZE(zfs_keys_change_key));
6974 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6975 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6976 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6977 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6978 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6979 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
6980 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
6982 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6983 zfs_ioc_channel_program, zfs_secpolicy_config,
6984 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6985 B_TRUE, zfs_keys_channel_program,
6986 ARRAY_SIZE(zfs_keys_channel_program));
6988 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
6989 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
6990 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6991 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
6993 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6994 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6995 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6996 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6998 zfs_ioctl_register("zpool_discard_checkpoint",
6999 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
7000 zfs_secpolicy_config, POOL_NAME,
7001 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7002 zfs_keys_pool_discard_checkpoint,
7003 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
7005 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
7006 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
7007 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7008 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
7010 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
7011 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
7012 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7013 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
7015 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
7016 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
7017 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7018 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
7020 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS,
7021 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME,
7022 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7023 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait));
7025 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
7026 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
7027 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7028 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
7030 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
7031 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
7032 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
7033 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
7035 /* IOCTLS that use the legacy function signature */
7037 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7038 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7040 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7041 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7042 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7044 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7045 zfs_ioc_pool_upgrade);
7046 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7048 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7049 zfs_ioc_vdev_remove);
7050 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7051 zfs_ioc_vdev_set_state);
7052 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7053 zfs_ioc_vdev_attach);
7054 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7055 zfs_ioc_vdev_detach);
7056 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7057 zfs_ioc_vdev_setpath);
7058 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7059 zfs_ioc_vdev_setfru);
7060 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7061 zfs_ioc_pool_set_props);
7062 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7063 zfs_ioc_vdev_split);
7064 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7065 zfs_ioc_pool_reguid);
7067 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7068 zfs_ioc_pool_configs, zfs_secpolicy_none);
7069 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7070 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7071 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7072 zfs_ioc_inject_fault, zfs_secpolicy_inject);
7073 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7074 zfs_ioc_clear_fault, zfs_secpolicy_inject);
7075 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
7076 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
7079 * pool destroy, and export don't log the history as part of
7080 * zfsdev_ioctl, but rather zfs_ioc_pool_export
7081 * does the logging of those commands.
7083 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
7084 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7085 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
7086 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7088 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7089 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7090 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
7091 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7093 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7094 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7095 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7096 zfs_ioc_dsobj_to_dsname,
7097 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7098 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7099 zfs_ioc_pool_get_history,
7100 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7102 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7103 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7105 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
7106 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
7108 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7109 zfs_ioc_space_written);
7110 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7111 zfs_ioc_objset_recvd_props);
7112 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7114 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7116 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7117 zfs_ioc_objset_stats);
7118 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7119 zfs_ioc_objset_zplprops);
7120 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7121 zfs_ioc_dataset_list_next);
7122 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7123 zfs_ioc_snapshot_list_next);
7124 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7125 zfs_ioc_send_progress);
7127 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7128 zfs_ioc_diff, zfs_secpolicy_diff);
7129 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7130 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7131 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7132 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7133 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7134 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7135 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7136 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7137 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7138 zfs_ioc_send, zfs_secpolicy_send);
7140 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7141 zfs_secpolicy_none);
7142 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7143 zfs_secpolicy_destroy);
7144 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7145 zfs_secpolicy_rename);
7146 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7147 zfs_secpolicy_recv);
7148 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7149 zfs_secpolicy_promote);
7150 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7151 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7152 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7153 zfs_secpolicy_set_fsacl);
7155 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7156 zfs_secpolicy_share, POOL_CHECK_NONE);
7157 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7158 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7159 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7160 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7161 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7162 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7163 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7164 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7166 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7167 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7168 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7169 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7170 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7171 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7173 zfs_ioctl_init_os();
7177 * Verify that for non-legacy ioctls the input nvlist
7178 * pairs match against the expected input.
7180 * Possible errors are:
7181 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7182 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7183 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7186 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7188 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7189 boolean_t required_keys_found = B_FALSE;
7192 * examine each input pair
7194 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7195 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7196 char *name = nvpair_name(pair);
7197 data_type_t type = nvpair_type(pair);
7198 boolean_t identified = B_FALSE;
7201 * check pair against the documented names and type
7203 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7204 /* if not a wild card name, check for an exact match */
7205 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7206 strcmp(nvl_keys[k].zkey_name, name) != 0)
7209 identified = B_TRUE;
7211 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7212 nvl_keys[k].zkey_type != type) {
7213 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7216 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7219 required_keys_found = B_TRUE;
7223 /* allow an 'optional' key, everything else is invalid */
7225 (strcmp(name, "optional") != 0 ||
7226 type != DATA_TYPE_NVLIST)) {
7227 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7231 /* verify that all required keys were found */
7232 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7233 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7236 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7237 /* at least one non-optional key is expected here */
7238 if (!required_keys_found)
7239 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7243 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7244 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7251 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7252 zfs_ioc_poolcheck_t check)
7257 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7258 type == ENTITY_NAME);
7260 if (check & POOL_CHECK_NONE)
7263 error = spa_open(name, &spa, FTAG);
7265 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7266 error = SET_ERROR(EAGAIN);
7267 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7268 error = SET_ERROR(EROFS);
7269 spa_close(spa, FTAG);
7275 zfsdev_getminor(int fd, minor_t *minorp)
7277 zfsdev_state_t *zs, *fpd;
7281 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7283 if ((rc = zfs_file_get(fd, &fp)))
7286 fpd = zfs_file_private(fp);
7288 return (SET_ERROR(EBADF));
7290 mutex_enter(&zfsdev_state_lock);
7292 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7294 if (zs->zs_minor == -1)
7298 *minorp = fpd->zs_minor;
7299 mutex_exit(&zfsdev_state_lock);
7304 mutex_exit(&zfsdev_state_lock);
7306 return (SET_ERROR(EBADF));
7310 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
7314 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7315 if (zs->zs_minor == minor) {
7319 return (zs->zs_onexit);
7321 return (zs->zs_zevent);
7332 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
7336 ptr = zfsdev_get_state_impl(minor, which);
7342 * Find a free minor number. The zfsdev_state_list is expected to
7343 * be short since it is only a list of currently open file handles.
7346 zfsdev_minor_alloc(void)
7348 static minor_t last_minor = 0;
7351 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7353 for (m = last_minor + 1; m != last_minor; m++) {
7354 if (m > ZFSDEV_MAX_MINOR)
7356 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
7366 zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
7369 const zfs_ioc_vec_t *vec;
7370 char *saved_poolname = NULL;
7371 uint64_t max_nvlist_src_size;
7372 size_t saved_poolname_len = 0;
7373 nvlist_t *innvl = NULL;
7374 fstrans_cookie_t cookie;
7378 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7379 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7381 vec = &zfs_ioc_vec[vecnum];
7384 * The registered ioctl list may be sparse, verify that either
7385 * a normal or legacy handler are registered.
7387 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7388 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7390 zc->zc_iflags = flag & FKIOCTL;
7391 max_nvlist_src_size = zfs_max_nvlist_src_size_os();
7392 if (zc->zc_nvlist_src_size > max_nvlist_src_size) {
7394 * Make sure the user doesn't pass in an insane value for
7395 * zc_nvlist_src_size. We have to check, since we will end
7396 * up allocating that much memory inside of get_nvlist(). This
7397 * prevents a nefarious user from allocating tons of kernel
7400 * Also, we return EINVAL instead of ENOMEM here. The reason
7401 * being that returning ENOMEM from an ioctl() has a special
7402 * connotation; that the user's size value is too small and
7403 * needs to be expanded to hold the nvlist. See
7404 * zcmd_expand_dst_nvlist() for details.
7406 error = SET_ERROR(EINVAL); /* User's size too big */
7408 } else if (zc->zc_nvlist_src_size != 0) {
7409 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7410 zc->zc_iflags, &innvl);
7416 * Ensure that all pool/dataset names are valid before we pass down to
7419 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7420 switch (vec->zvec_namecheck) {
7422 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7423 error = SET_ERROR(EINVAL);
7425 error = pool_status_check(zc->zc_name,
7426 vec->zvec_namecheck, vec->zvec_pool_check);
7430 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7431 error = SET_ERROR(EINVAL);
7433 error = pool_status_check(zc->zc_name,
7434 vec->zvec_namecheck, vec->zvec_pool_check);
7438 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7439 error = SET_ERROR(EINVAL);
7441 error = pool_status_check(zc->zc_name,
7442 vec->zvec_namecheck, vec->zvec_pool_check);
7450 * Ensure that all input pairs are valid before we pass them down
7451 * to the lower layers.
7453 * The vectored functions can use fnvlist_lookup_{type} for any
7454 * required pairs since zfs_check_input_nvpairs() confirmed that
7455 * they exist and are of the correct type.
7457 if (error == 0 && vec->zvec_func != NULL) {
7458 error = zfs_check_input_nvpairs(innvl, vec);
7464 cookie = spl_fstrans_mark();
7465 error = vec->zvec_secpolicy(zc, innvl, CRED());
7466 spl_fstrans_unmark(cookie);
7472 /* legacy ioctls can modify zc_name */
7474 * Can't use kmem_strdup() as we might truncate the string and
7475 * kmem_strfree() would then free with incorrect size.
7477 saved_poolname_len = strlen(zc->zc_name) + 1;
7478 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP);
7480 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len);
7481 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7483 if (vec->zvec_func != NULL) {
7487 nvlist_t *lognv = NULL;
7489 ASSERT(vec->zvec_legacy_func == NULL);
7492 * Add the innvl to the lognv before calling the func,
7493 * in case the func changes the innvl.
7495 if (vec->zvec_allow_log) {
7496 lognv = fnvlist_alloc();
7497 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7499 if (!nvlist_empty(innvl)) {
7500 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7505 outnvl = fnvlist_alloc();
7506 cookie = spl_fstrans_mark();
7507 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7508 spl_fstrans_unmark(cookie);
7511 * Some commands can partially execute, modify state, and still
7512 * return an error. In these cases, attempt to record what
7516 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7517 vec->zvec_allow_log &&
7518 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7519 if (!nvlist_empty(outnvl)) {
7520 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7524 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7527 (void) spa_history_log_nvl(spa, lognv);
7528 spa_close(spa, FTAG);
7530 fnvlist_free(lognv);
7532 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7534 if (vec->zvec_smush_outnvlist) {
7535 smusherror = nvlist_smush(outnvl,
7536 zc->zc_nvlist_dst_size);
7538 if (smusherror == 0)
7539 puterror = put_nvlist(zc, outnvl);
7545 nvlist_free(outnvl);
7547 cookie = spl_fstrans_mark();
7548 error = vec->zvec_legacy_func(zc);
7549 spl_fstrans_unmark(cookie);
7554 if (error == 0 && vec->zvec_allow_log) {
7555 char *s = tsd_get(zfs_allow_log_key);
7558 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname));
7560 if (saved_poolname != NULL)
7561 kmem_free(saved_poolname, saved_poolname_len);
7571 if ((error = zvol_init()) != 0)
7574 spa_init(SPA_MODE_READ | SPA_MODE_WRITE);
7579 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7580 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7581 zfsdev_state_list->zs_minor = -1;
7583 if ((error = zfsdev_attach()) != 0)
7586 tsd_create(&zfs_fsyncer_key, NULL);
7587 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7588 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7602 zfsdev_state_t *zs, *zsnext = NULL;
7606 mutex_destroy(&zfsdev_state_lock);
7608 for (zs = zfsdev_state_list; zs != NULL; zs = zsnext) {
7609 zsnext = zs->zs_next;
7611 zfs_onexit_destroy(zs->zs_onexit);
7613 zfs_zevent_destroy(zs->zs_zevent);
7614 kmem_free(zs, sizeof (zfsdev_state_t));
7617 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */
7622 tsd_destroy(&zfs_fsyncer_key);
7623 tsd_destroy(&rrw_tsd_key);
7624 tsd_destroy(&zfs_allow_log_key);
7628 ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, ULONG, ZMOD_RW,
7629 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls");