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 https://opensource.org/licenses/CDDL-1.0.
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 * Copyright (c) 2012 Pawel Jakub Dawidek
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, 2021, 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>
162 #include <sys/uio_impl.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 static zfsdev_state_t zfsdev_state_listhead;
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 uint64_t zfs_max_nvlist_src_size = 0;
235 * When logging the output nvlist of an ioctl in the on-disk history, limit
236 * the logged size to this many bytes. This must be less than DMU_MAX_ACCESS.
237 * This applies primarily to zfs_ioc_channel_program().
239 static uint64_t zfs_history_output_max = 1024 * 1024;
241 uint_t zfs_allow_log_key;
243 /* DATA_TYPE_ANY is used when zkey_type can vary. */
244 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
246 typedef struct zfs_ioc_vec {
247 zfs_ioc_legacy_func_t *zvec_legacy_func;
248 zfs_ioc_func_t *zvec_func;
249 zfs_secpolicy_func_t *zvec_secpolicy;
250 zfs_ioc_namecheck_t zvec_namecheck;
251 boolean_t zvec_allow_log;
252 zfs_ioc_poolcheck_t zvec_pool_check;
253 boolean_t zvec_smush_outnvlist;
254 const char *zvec_name;
255 const zfs_ioc_key_t *zvec_nvl_keys;
256 size_t zvec_nvl_key_count;
259 /* This array is indexed by zfs_userquota_prop_t */
260 static const char *userquota_perms[] = {
261 ZFS_DELEG_PERM_USERUSED,
262 ZFS_DELEG_PERM_USERQUOTA,
263 ZFS_DELEG_PERM_GROUPUSED,
264 ZFS_DELEG_PERM_GROUPQUOTA,
265 ZFS_DELEG_PERM_USEROBJUSED,
266 ZFS_DELEG_PERM_USEROBJQUOTA,
267 ZFS_DELEG_PERM_GROUPOBJUSED,
268 ZFS_DELEG_PERM_GROUPOBJQUOTA,
269 ZFS_DELEG_PERM_PROJECTUSED,
270 ZFS_DELEG_PERM_PROJECTQUOTA,
271 ZFS_DELEG_PERM_PROJECTOBJUSED,
272 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
275 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
276 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
277 static int zfs_check_settable(const char *name, nvpair_t *property,
279 static int zfs_check_clearable(const char *dataset, nvlist_t *props,
281 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
283 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
284 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
287 history_str_free(char *buf)
289 kmem_free(buf, HIS_MAX_RECORD_LEN);
293 history_str_get(zfs_cmd_t *zc)
297 if (zc->zc_history == 0)
300 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
301 if (copyinstr((void *)(uintptr_t)zc->zc_history,
302 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
303 history_str_free(buf);
307 buf[HIS_MAX_RECORD_LEN -1] = '\0';
313 * Return non-zero if the spa version is less than requested version.
316 zfs_earlier_version(const char *name, int version)
320 if (spa_open(name, &spa, FTAG) == 0) {
321 if (spa_version(spa) < version) {
322 spa_close(spa, FTAG);
325 spa_close(spa, FTAG);
331 * Return TRUE if the ZPL version is less than requested version.
334 zpl_earlier_version(const char *name, int version)
337 boolean_t rc = B_TRUE;
339 if (dmu_objset_hold(name, FTAG, &os) == 0) {
342 if (dmu_objset_type(os) != DMU_OST_ZFS) {
343 dmu_objset_rele(os, FTAG);
346 /* XXX reading from non-owned objset */
347 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
348 rc = zplversion < version;
349 dmu_objset_rele(os, FTAG);
355 zfs_log_history(zfs_cmd_t *zc)
360 if ((buf = history_str_get(zc)) == NULL)
363 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
364 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
365 (void) spa_history_log(spa, buf);
366 spa_close(spa, FTAG);
368 history_str_free(buf);
372 * Policy for top-level read operations (list pools). Requires no privileges,
373 * and can be used in the local zone, as there is no associated dataset.
376 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
378 (void) zc, (void) innvl, (void) cr;
383 * Policy for dataset read operations (list children, get statistics). Requires
384 * no privileges, but must be visible in the local zone.
387 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
389 (void) innvl, (void) cr;
390 if (INGLOBALZONE(curproc) ||
391 zone_dataset_visible(zc->zc_name, NULL))
394 return (SET_ERROR(ENOENT));
398 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
403 * The dataset must be visible by this zone -- check this first
404 * so they don't see EPERM on something they shouldn't know about.
406 if (!INGLOBALZONE(curproc) &&
407 !zone_dataset_visible(dataset, &writable))
408 return (SET_ERROR(ENOENT));
410 if (INGLOBALZONE(curproc)) {
412 * If the fs is zoned, only root can access it from the
415 if (secpolicy_zfs(cr) && zoned)
416 return (SET_ERROR(EPERM));
419 * If we are in a local zone, the 'zoned' property must be set.
422 return (SET_ERROR(EPERM));
424 /* must be writable by this zone */
426 return (SET_ERROR(EPERM));
432 zfs_dozonecheck(const char *dataset, cred_t *cr)
436 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED),
438 return (SET_ERROR(ENOENT));
440 return (zfs_dozonecheck_impl(dataset, zoned, cr));
444 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
448 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned))
449 return (SET_ERROR(ENOENT));
451 return (zfs_dozonecheck_impl(dataset, zoned, cr));
455 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
456 const char *perm, cred_t *cr)
460 error = zfs_dozonecheck_ds(name, ds, cr);
462 error = secpolicy_zfs(cr);
464 error = dsl_deleg_access_impl(ds, perm, cr);
470 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
477 * First do a quick check for root in the global zone, which
478 * is allowed to do all write_perms. This ensures that zfs_ioc_*
479 * will get to handle nonexistent datasets.
481 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
484 error = dsl_pool_hold(name, FTAG, &dp);
488 error = dsl_dataset_hold(dp, name, FTAG, &ds);
490 dsl_pool_rele(dp, FTAG);
494 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
496 dsl_dataset_rele(ds, FTAG);
497 dsl_pool_rele(dp, FTAG);
502 * Policy for setting the security label property.
504 * Returns 0 for success, non-zero for access and other errors.
507 zfs_set_slabel_policy(const char *name, const char *strval, cred_t *cr)
510 char ds_hexsl[MAXNAMELEN];
511 bslabel_t ds_sl, new_sl;
512 boolean_t new_default = FALSE;
514 int needed_priv = -1;
517 /* First get the existing dataset label. */
518 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
519 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
521 return (SET_ERROR(EPERM));
523 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
526 /* The label must be translatable */
527 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
528 return (SET_ERROR(EINVAL));
531 * In a non-global zone, disallow attempts to set a label that
532 * doesn't match that of the zone; otherwise no other checks
535 if (!INGLOBALZONE(curproc)) {
536 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
537 return (SET_ERROR(EPERM));
542 * For global-zone datasets (i.e., those whose zoned property is
543 * "off", verify that the specified new label is valid for the
546 if (dsl_prop_get_integer(name,
547 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
548 return (SET_ERROR(EPERM));
550 if (zfs_check_global_label(name, strval) != 0)
551 return (SET_ERROR(EPERM));
555 * If the existing dataset label is nondefault, check if the
556 * dataset is mounted (label cannot be changed while mounted).
557 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
558 * mounted (or isn't a dataset, doesn't exist, ...).
560 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
562 static const char *setsl_tag = "setsl_tag";
565 * Try to own the dataset; abort if there is any error,
566 * (e.g., already mounted, in use, or other error).
568 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
571 return (SET_ERROR(EPERM));
573 dmu_objset_disown(os, B_TRUE, setsl_tag);
576 needed_priv = PRIV_FILE_DOWNGRADE_SL;
580 if (hexstr_to_label(strval, &new_sl) != 0)
581 return (SET_ERROR(EPERM));
583 if (blstrictdom(&ds_sl, &new_sl))
584 needed_priv = PRIV_FILE_DOWNGRADE_SL;
585 else if (blstrictdom(&new_sl, &ds_sl))
586 needed_priv = PRIV_FILE_UPGRADE_SL;
588 /* dataset currently has a default label */
590 needed_priv = PRIV_FILE_UPGRADE_SL;
594 if (needed_priv != -1)
595 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
598 return (SET_ERROR(ENOTSUP));
599 #endif /* HAVE_MLSLABEL */
603 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
609 * Check permissions for special properties.
616 * Disallow setting of 'zoned' from within a local zone.
618 if (!INGLOBALZONE(curproc))
619 return (SET_ERROR(EPERM));
623 case ZFS_PROP_FILESYSTEM_LIMIT:
624 case ZFS_PROP_SNAPSHOT_LIMIT:
625 if (!INGLOBALZONE(curproc)) {
627 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
629 * Unprivileged users are allowed to modify the
630 * limit on things *under* (ie. contained by)
631 * the thing they own.
633 if (dsl_prop_get_integer(dsname,
634 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint))
635 return (SET_ERROR(EPERM));
636 if (!zoned || strlen(dsname) <= strlen(setpoint))
637 return (SET_ERROR(EPERM));
641 case ZFS_PROP_MLSLABEL:
642 if (!is_system_labeled())
643 return (SET_ERROR(EPERM));
645 if (nvpair_value_string(propval, &strval) == 0) {
648 err = zfs_set_slabel_policy(dsname, strval, CRED());
655 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
659 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
662 * permission to set permissions will be evaluated later in
663 * dsl_deleg_can_allow()
666 return (zfs_dozonecheck(zc->zc_name, cr));
670 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));
678 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);
714 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 (void) zc, (void) innvl, (void) cr;
725 return (SET_ERROR(ENOTSUP));
729 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
731 (void) zc, (void) innvl, (void) cr;
732 return (SET_ERROR(ENOTSUP));
736 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
741 * Remove the @bla or /bla from the end of the name to get the parent.
743 (void) strlcpy(parent, datasetname, parentsize);
744 cp = strrchr(parent, '@');
748 cp = strrchr(parent, '/');
750 return (SET_ERROR(ENOENT));
758 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
762 if ((error = zfs_secpolicy_write_perms(name,
763 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
766 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
770 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
773 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
777 * Destroying snapshots with delegated permissions requires
778 * descendant mount and destroy permissions.
781 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
785 nvpair_t *pair, *nextpair;
788 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
790 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
792 nextpair = nvlist_next_nvpair(snaps, pair);
793 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
794 if (error == ENOENT) {
796 * Ignore any snapshots that don't exist (we consider
797 * them "already destroyed"). Remove the name from the
798 * nvl here in case the snapshot is created between
799 * now and when we try to destroy it (in which case
800 * we don't want to destroy it since we haven't
801 * checked for permission).
803 fnvlist_remove_nvpair(snaps, pair);
814 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
816 char parentname[ZFS_MAX_DATASET_NAME_LEN];
819 if ((error = zfs_secpolicy_write_perms(from,
820 ZFS_DELEG_PERM_RENAME, cr)) != 0)
823 if ((error = zfs_secpolicy_write_perms(from,
824 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
827 if ((error = zfs_get_parent(to, parentname,
828 sizeof (parentname))) != 0)
831 if ((error = zfs_secpolicy_write_perms(parentname,
832 ZFS_DELEG_PERM_CREATE, cr)) != 0)
835 if ((error = zfs_secpolicy_write_perms(parentname,
836 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
843 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
846 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
850 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
854 dsl_dataset_t *clone;
857 error = zfs_secpolicy_write_perms(zc->zc_name,
858 ZFS_DELEG_PERM_PROMOTE, cr);
862 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
866 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
869 char parentname[ZFS_MAX_DATASET_NAME_LEN];
870 dsl_dataset_t *origin = NULL;
874 error = dsl_dataset_hold_obj(dd->dd_pool,
875 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
877 dsl_dataset_rele(clone, FTAG);
878 dsl_pool_rele(dp, FTAG);
882 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
883 ZFS_DELEG_PERM_MOUNT, cr);
885 dsl_dataset_name(origin, parentname);
887 error = zfs_secpolicy_write_perms_ds(parentname, origin,
888 ZFS_DELEG_PERM_PROMOTE, cr);
890 dsl_dataset_rele(clone, FTAG);
891 dsl_dataset_rele(origin, FTAG);
893 dsl_pool_rele(dp, FTAG);
898 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
903 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
904 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
907 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
908 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
911 return (zfs_secpolicy_write_perms(zc->zc_name,
912 ZFS_DELEG_PERM_CREATE, cr));
916 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
918 return (zfs_secpolicy_write_perms(name,
919 ZFS_DELEG_PERM_SNAPSHOT, cr));
923 * Check for permission to create each snapshot in the nvlist.
926 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
933 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
935 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
936 pair = nvlist_next_nvpair(snaps, pair)) {
937 char *name = (char *)nvpair_name(pair);
938 char *atp = strchr(name, '@');
941 error = SET_ERROR(EINVAL);
945 error = zfs_secpolicy_snapshot_perms(name, cr);
954 * Check for permission to create each bookmark in the nvlist.
957 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
962 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
963 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
964 char *name = (char *)nvpair_name(pair);
965 char *hashp = strchr(name, '#');
968 error = SET_ERROR(EINVAL);
972 error = zfs_secpolicy_write_perms(name,
973 ZFS_DELEG_PERM_BOOKMARK, cr);
982 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
985 nvpair_t *pair, *nextpair;
988 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
990 char *name = (char *)nvpair_name(pair);
991 char *hashp = strchr(name, '#');
992 nextpair = nvlist_next_nvpair(innvl, pair);
995 error = SET_ERROR(EINVAL);
1000 error = zfs_secpolicy_write_perms(name,
1001 ZFS_DELEG_PERM_DESTROY, cr);
1003 if (error == ENOENT) {
1005 * Ignore any filesystems that don't exist (we consider
1006 * their bookmarks "already destroyed"). Remove
1007 * the name from the nvl here in case the filesystem
1008 * is created between now and when we try to destroy
1009 * the bookmark (in which case we don't want to
1010 * destroy it since we haven't checked for permission).
1012 fnvlist_remove_nvpair(innvl, pair);
1023 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1025 (void) zc, (void) innvl, (void) cr;
1027 * Even root must have a proper TSD so that we know what pool
1030 if (tsd_get(zfs_allow_log_key) == NULL)
1031 return (SET_ERROR(EPERM));
1036 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1038 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1042 if ((error = zfs_get_parent(zc->zc_name, parentname,
1043 sizeof (parentname))) != 0)
1046 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1047 (error = zfs_secpolicy_write_perms(origin,
1048 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1051 if ((error = zfs_secpolicy_write_perms(parentname,
1052 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1055 return (zfs_secpolicy_write_perms(parentname,
1056 ZFS_DELEG_PERM_MOUNT, cr));
1060 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1061 * SYS_CONFIG privilege, which is not available in a local zone.
1064 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1066 (void) zc, (void) innvl;
1068 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1069 return (SET_ERROR(EPERM));
1075 * Policy for object to name lookups.
1078 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1083 if (secpolicy_sys_config(cr, B_FALSE) == 0)
1086 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1091 * Policy for fault injection. Requires all privileges.
1094 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1096 (void) zc, (void) innvl;
1097 return (secpolicy_zinject(cr));
1101 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1104 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1106 if (prop == ZPROP_USERPROP) {
1107 if (!zfs_prop_user(zc->zc_value))
1108 return (SET_ERROR(EINVAL));
1109 return (zfs_secpolicy_write_perms(zc->zc_name,
1110 ZFS_DELEG_PERM_USERPROP, cr));
1112 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1118 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1120 int err = zfs_secpolicy_read(zc, innvl, cr);
1124 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1125 return (SET_ERROR(EINVAL));
1127 if (zc->zc_value[0] == 0) {
1129 * They are asking about a posix uid/gid. If it's
1130 * themself, allow it.
1132 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1133 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1134 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1135 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1136 if (zc->zc_guid == crgetuid(cr))
1138 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1139 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1140 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1141 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1142 if (groupmember(zc->zc_guid, cr))
1145 /* else is for project quota/used */
1148 return (zfs_secpolicy_write_perms(zc->zc_name,
1149 userquota_perms[zc->zc_objset_type], cr));
1153 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1155 int err = zfs_secpolicy_read(zc, innvl, cr);
1159 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1160 return (SET_ERROR(EINVAL));
1162 return (zfs_secpolicy_write_perms(zc->zc_name,
1163 userquota_perms[zc->zc_objset_type], cr));
1167 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1170 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1175 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1182 holds = fnvlist_lookup_nvlist(innvl, "holds");
1184 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1185 pair = nvlist_next_nvpair(holds, pair)) {
1186 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1187 error = dmu_fsname(nvpair_name(pair), fsname);
1190 error = zfs_secpolicy_write_perms(fsname,
1191 ZFS_DELEG_PERM_HOLD, cr);
1199 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1205 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1206 pair = nvlist_next_nvpair(innvl, pair)) {
1207 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1208 error = dmu_fsname(nvpair_name(pair), fsname);
1211 error = zfs_secpolicy_write_perms(fsname,
1212 ZFS_DELEG_PERM_RELEASE, cr);
1220 * Policy for allowing temporary snapshots to be taken or released
1223 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1226 * A temporary snapshot is the same as a snapshot,
1227 * hold, destroy and release all rolled into one.
1228 * Delegated diff alone is sufficient that we allow this.
1232 if (zfs_secpolicy_write_perms(zc->zc_name,
1233 ZFS_DELEG_PERM_DIFF, cr) == 0)
1236 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1238 if (innvl != NULL) {
1240 error = zfs_secpolicy_hold(zc, innvl, cr);
1242 error = zfs_secpolicy_release(zc, innvl, cr);
1244 error = zfs_secpolicy_destroy(zc, innvl, cr);
1250 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1252 return (zfs_secpolicy_write_perms(zc->zc_name,
1253 ZFS_DELEG_PERM_LOAD_KEY, cr));
1257 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1259 return (zfs_secpolicy_write_perms(zc->zc_name,
1260 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1264 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1267 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1271 nvlist_t *list = NULL;
1274 * Read in and unpack the user-supplied nvlist.
1277 return (SET_ERROR(EINVAL));
1279 packed = vmem_alloc(size, KM_SLEEP);
1281 if (ddi_copyin((void *)(uintptr_t)nvl, packed, size, iflag) != 0) {
1282 vmem_free(packed, size);
1283 return (SET_ERROR(EFAULT));
1286 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1287 vmem_free(packed, size);
1291 vmem_free(packed, size);
1298 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1299 * Entries will be removed from the end of the nvlist, and one int32 entry
1300 * named "N_MORE_ERRORS" will be added indicating how many entries were
1304 nvlist_smush(nvlist_t *errors, size_t max)
1308 size = fnvlist_size(errors);
1311 nvpair_t *more_errors;
1315 return (SET_ERROR(ENOMEM));
1317 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1318 more_errors = nvlist_prev_nvpair(errors, NULL);
1321 nvpair_t *pair = nvlist_prev_nvpair(errors,
1323 fnvlist_remove_nvpair(errors, pair);
1325 size = fnvlist_size(errors);
1326 } while (size > max);
1328 fnvlist_remove_nvpair(errors, more_errors);
1329 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1330 ASSERT3U(fnvlist_size(errors), <=, max);
1337 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1339 char *packed = NULL;
1343 size = fnvlist_size(nvl);
1345 if (size > zc->zc_nvlist_dst_size) {
1346 error = SET_ERROR(ENOMEM);
1348 packed = fnvlist_pack(nvl, &size);
1349 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1350 size, zc->zc_iflags) != 0)
1351 error = SET_ERROR(EFAULT);
1352 fnvlist_pack_free(packed, size);
1355 zc->zc_nvlist_dst_size = size;
1356 zc->zc_nvlist_dst_filled = B_TRUE;
1361 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1364 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1365 return (SET_ERROR(EINVAL));
1368 mutex_enter(&os->os_user_ptr_lock);
1369 *zfvp = dmu_objset_get_user(os);
1370 /* bump s_active only when non-zero to prevent umount race */
1371 error = zfs_vfs_ref(zfvp);
1372 mutex_exit(&os->os_user_ptr_lock);
1377 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1382 error = dmu_objset_hold(dsname, FTAG, &os);
1386 error = getzfsvfs_impl(os, zfvp);
1387 dmu_objset_rele(os, FTAG);
1392 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1393 * case its z_sb will be NULL, and it will be opened as the owner.
1394 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1395 * which prevents all inode ops from running.
1398 zfsvfs_hold(const char *name, const void *tag, zfsvfs_t **zfvp,
1403 if (getzfsvfs(name, zfvp) != 0)
1404 error = zfsvfs_create(name, B_FALSE, zfvp);
1407 ZFS_TEARDOWN_ENTER_WRITE(*zfvp, tag);
1409 ZFS_TEARDOWN_ENTER_READ(*zfvp, tag);
1410 if ((*zfvp)->z_unmounted) {
1412 * XXX we could probably try again, since the unmounting
1413 * thread should be just about to disassociate the
1414 * objset from the zfsvfs.
1416 ZFS_TEARDOWN_EXIT(*zfvp, tag);
1417 return (SET_ERROR(EBUSY));
1424 zfsvfs_rele(zfsvfs_t *zfsvfs, const void *tag)
1426 ZFS_TEARDOWN_EXIT(zfsvfs, tag);
1428 if (zfs_vfs_held(zfsvfs)) {
1429 zfs_vfs_rele(zfsvfs);
1431 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1432 zfsvfs_free(zfsvfs);
1437 zfs_ioc_pool_create(zfs_cmd_t *zc)
1440 nvlist_t *config, *props = NULL;
1441 nvlist_t *rootprops = NULL;
1442 nvlist_t *zplprops = NULL;
1443 dsl_crypto_params_t *dcp = NULL;
1444 const char *spa_name = zc->zc_name;
1445 boolean_t unload_wkey = B_TRUE;
1447 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1448 zc->zc_iflags, &config)))
1451 if (zc->zc_nvlist_src_size != 0 && (error =
1452 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1453 zc->zc_iflags, &props))) {
1454 nvlist_free(config);
1459 nvlist_t *nvl = NULL;
1460 nvlist_t *hidden_args = NULL;
1461 uint64_t version = SPA_VERSION;
1464 (void) nvlist_lookup_uint64(props,
1465 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1466 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1467 error = SET_ERROR(EINVAL);
1468 goto pool_props_bad;
1470 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1472 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1474 goto pool_props_bad;
1475 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1478 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1480 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1481 rootprops, hidden_args, &dcp);
1483 goto pool_props_bad;
1484 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1486 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1487 error = zfs_fill_zplprops_root(version, rootprops,
1490 goto pool_props_bad;
1492 if (nvlist_lookup_string(props,
1493 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1497 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1500 * Set the remaining root properties
1502 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1503 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1504 (void) spa_destroy(spa_name);
1505 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1509 nvlist_free(rootprops);
1510 nvlist_free(zplprops);
1511 nvlist_free(config);
1513 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1519 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1522 zfs_log_history(zc);
1523 error = spa_destroy(zc->zc_name);
1529 zfs_ioc_pool_import(zfs_cmd_t *zc)
1531 nvlist_t *config, *props = NULL;
1535 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1536 zc->zc_iflags, &config)) != 0)
1539 if (zc->zc_nvlist_src_size != 0 && (error =
1540 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1541 zc->zc_iflags, &props))) {
1542 nvlist_free(config);
1546 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1547 guid != zc->zc_guid)
1548 error = SET_ERROR(EINVAL);
1550 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1552 if (zc->zc_nvlist_dst != 0) {
1555 if ((err = put_nvlist(zc, config)) != 0)
1559 nvlist_free(config);
1566 zfs_ioc_pool_export(zfs_cmd_t *zc)
1569 boolean_t force = (boolean_t)zc->zc_cookie;
1570 boolean_t hardforce = (boolean_t)zc->zc_guid;
1572 zfs_log_history(zc);
1573 error = spa_export(zc->zc_name, NULL, force, hardforce);
1579 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1584 error = spa_all_configs(&zc->zc_cookie, &configs);
1588 error = put_nvlist(zc, configs);
1590 nvlist_free(configs);
1597 * zc_name name of the pool
1600 * zc_cookie real errno
1601 * zc_nvlist_dst config nvlist
1602 * zc_nvlist_dst_size size of config nvlist
1605 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1611 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1612 sizeof (zc->zc_value));
1614 if (config != NULL) {
1615 ret = put_nvlist(zc, config);
1616 nvlist_free(config);
1619 * The config may be present even if 'error' is non-zero.
1620 * In this case we return success, and preserve the real errno
1623 zc->zc_cookie = error;
1632 * Try to import the given pool, returning pool stats as appropriate so that
1633 * user land knows which devices are available and overall pool health.
1636 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1638 nvlist_t *tryconfig, *config = NULL;
1641 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1642 zc->zc_iflags, &tryconfig)) != 0)
1645 config = spa_tryimport(tryconfig);
1647 nvlist_free(tryconfig);
1650 return (SET_ERROR(EINVAL));
1652 error = put_nvlist(zc, config);
1653 nvlist_free(config);
1660 * zc_name name of the pool
1661 * zc_cookie scan func (pool_scan_func_t)
1662 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1665 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1670 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1671 return (SET_ERROR(EINVAL));
1673 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1676 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1677 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1678 else if (zc->zc_cookie == POOL_SCAN_NONE)
1679 error = spa_scan_stop(spa);
1681 error = spa_scan(spa, zc->zc_cookie);
1683 spa_close(spa, FTAG);
1690 * poolname name of the pool
1691 * scan_type scan func (pool_scan_func_t)
1692 * scan_command scrub pause/resume flag (pool_scrub_cmd_t)
1694 static const zfs_ioc_key_t zfs_keys_pool_scrub[] = {
1695 {"scan_type", DATA_TYPE_UINT64, 0},
1696 {"scan_command", DATA_TYPE_UINT64, 0},
1700 zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
1704 uint64_t scan_type, scan_cmd;
1706 if (nvlist_lookup_uint64(innvl, "scan_type", &scan_type) != 0)
1707 return (SET_ERROR(EINVAL));
1708 if (nvlist_lookup_uint64(innvl, "scan_command", &scan_cmd) != 0)
1709 return (SET_ERROR(EINVAL));
1711 if (scan_cmd >= POOL_SCRUB_FLAGS_END)
1712 return (SET_ERROR(EINVAL));
1714 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
1717 if (scan_cmd == POOL_SCRUB_PAUSE) {
1718 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1719 } else if (scan_type == POOL_SCAN_NONE) {
1720 error = spa_scan_stop(spa);
1722 error = spa_scan(spa, scan_type);
1725 spa_close(spa, FTAG);
1730 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1735 error = spa_open(zc->zc_name, &spa, FTAG);
1738 spa_close(spa, FTAG);
1744 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1749 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1752 if (zc->zc_cookie < spa_version(spa) ||
1753 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1754 spa_close(spa, FTAG);
1755 return (SET_ERROR(EINVAL));
1758 spa_upgrade(spa, zc->zc_cookie);
1759 spa_close(spa, FTAG);
1765 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1772 if ((size = zc->zc_history_len) == 0)
1773 return (SET_ERROR(EINVAL));
1775 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1778 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1779 spa_close(spa, FTAG);
1780 return (SET_ERROR(ENOTSUP));
1783 hist_buf = vmem_alloc(size, KM_SLEEP);
1784 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1785 &zc->zc_history_len, hist_buf)) == 0) {
1786 error = ddi_copyout(hist_buf,
1787 (void *)(uintptr_t)zc->zc_history,
1788 zc->zc_history_len, zc->zc_iflags);
1791 spa_close(spa, FTAG);
1792 vmem_free(hist_buf, size);
1797 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1802 error = spa_open(zc->zc_name, &spa, FTAG);
1804 error = spa_change_guid(spa);
1805 spa_close(spa, FTAG);
1811 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1813 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1818 * zc_name name of filesystem
1819 * zc_obj object to find
1822 * zc_value name of object
1825 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1830 /* XXX reading from objset not owned */
1831 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1834 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1835 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1836 return (SET_ERROR(EINVAL));
1838 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1839 sizeof (zc->zc_value));
1840 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1847 * zc_name name of filesystem
1848 * zc_obj object to find
1851 * zc_stat stats on object
1852 * zc_value path to object
1855 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1860 /* XXX reading from objset not owned */
1861 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1864 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1865 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1866 return (SET_ERROR(EINVAL));
1868 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1869 sizeof (zc->zc_value));
1870 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1876 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1882 error = spa_open(zc->zc_name, &spa, FTAG);
1886 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1887 zc->zc_iflags, &config);
1889 error = spa_vdev_add(spa, config);
1890 nvlist_free(config);
1892 spa_close(spa, FTAG);
1898 * zc_name name of the pool
1899 * zc_guid guid of vdev to remove
1900 * zc_cookie cancel removal
1903 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1908 error = spa_open(zc->zc_name, &spa, FTAG);
1911 if (zc->zc_cookie != 0) {
1912 error = spa_vdev_remove_cancel(spa);
1914 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1916 spa_close(spa, FTAG);
1921 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1925 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1927 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1929 switch (zc->zc_cookie) {
1930 case VDEV_STATE_ONLINE:
1931 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1934 case VDEV_STATE_OFFLINE:
1935 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1938 case VDEV_STATE_FAULTED:
1939 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1940 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1941 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1942 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1944 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1947 case VDEV_STATE_DEGRADED:
1948 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1949 zc->zc_obj != VDEV_AUX_EXTERNAL)
1950 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1952 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1955 case VDEV_STATE_REMOVED:
1956 error = vdev_remove_wanted(spa, zc->zc_guid);
1960 error = SET_ERROR(EINVAL);
1962 zc->zc_cookie = newstate;
1963 spa_close(spa, FTAG);
1968 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1972 int replacing = zc->zc_cookie;
1973 int rebuild = zc->zc_simple;
1976 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1979 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1980 zc->zc_iflags, &config)) == 0) {
1981 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing,
1983 nvlist_free(config);
1986 spa_close(spa, FTAG);
1991 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1996 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1999 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2001 spa_close(spa, FTAG);
2006 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2009 nvlist_t *config, *props = NULL;
2011 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2013 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2016 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2017 zc->zc_iflags, &config))) {
2018 spa_close(spa, FTAG);
2022 if (zc->zc_nvlist_src_size != 0 && (error =
2023 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2024 zc->zc_iflags, &props))) {
2025 spa_close(spa, FTAG);
2026 nvlist_free(config);
2030 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2032 spa_close(spa, FTAG);
2034 nvlist_free(config);
2041 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2044 const char *path = zc->zc_value;
2045 uint64_t guid = zc->zc_guid;
2048 error = spa_open(zc->zc_name, &spa, FTAG);
2052 error = spa_vdev_setpath(spa, guid, path);
2053 spa_close(spa, FTAG);
2058 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2061 const char *fru = zc->zc_value;
2062 uint64_t guid = zc->zc_guid;
2065 error = spa_open(zc->zc_name, &spa, FTAG);
2069 error = spa_vdev_setfru(spa, guid, fru);
2070 spa_close(spa, FTAG);
2075 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2080 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2082 if (!zc->zc_simple && zc->zc_nvlist_dst != 0 &&
2083 (error = dsl_prop_get_all(os, &nv)) == 0) {
2084 dmu_objset_stats(os, nv);
2086 * NB: zvol_get_stats() will read the objset contents,
2087 * which we aren't supposed to do with a
2088 * DS_MODE_USER hold, because it could be
2089 * inconsistent. So this is a bit of a workaround...
2090 * XXX reading without owning
2092 if (!zc->zc_objset_stats.dds_inconsistent &&
2093 dmu_objset_type(os) == DMU_OST_ZVOL) {
2094 error = zvol_get_stats(os, nv);
2102 error = put_nvlist(zc, nv);
2111 * zc_name name of filesystem
2112 * zc_nvlist_dst_size size of buffer for property nvlist
2115 * zc_objset_stats stats
2116 * zc_nvlist_dst property nvlist
2117 * zc_nvlist_dst_size size of property nvlist
2120 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2125 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2127 error = zfs_ioc_objset_stats_impl(zc, os);
2128 dmu_objset_rele(os, FTAG);
2136 * zc_name name of filesystem
2137 * zc_nvlist_dst_size size of buffer for property nvlist
2140 * zc_nvlist_dst received property nvlist
2141 * zc_nvlist_dst_size size of received property nvlist
2143 * Gets received properties (distinct from local properties on or after
2144 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2145 * local property values.
2148 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2154 * Without this check, we would return local property values if the
2155 * caller has not already received properties on or after
2156 * SPA_VERSION_RECVD_PROPS.
2158 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2159 return (SET_ERROR(ENOTSUP));
2161 if (zc->zc_nvlist_dst != 0 &&
2162 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2163 error = put_nvlist(zc, nv);
2171 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2177 * zfs_get_zplprop() will either find a value or give us
2178 * the default value (if there is one).
2180 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2182 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2188 * zc_name name of filesystem
2189 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2192 * zc_nvlist_dst zpl property nvlist
2193 * zc_nvlist_dst_size size of zpl property nvlist
2196 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2201 /* XXX reading without owning */
2202 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2205 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2208 * NB: nvl_add_zplprop() will read the objset contents,
2209 * which we aren't supposed to do with a DS_MODE_USER
2210 * hold, because it could be inconsistent.
2212 if (zc->zc_nvlist_dst != 0 &&
2213 !zc->zc_objset_stats.dds_inconsistent &&
2214 dmu_objset_type(os) == DMU_OST_ZFS) {
2217 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2218 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2219 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2220 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2221 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2222 err = put_nvlist(zc, nv);
2225 err = SET_ERROR(ENOENT);
2227 dmu_objset_rele(os, FTAG);
2233 * zc_name name of filesystem
2234 * zc_cookie zap cursor
2235 * zc_nvlist_dst_size size of buffer for property nvlist
2238 * zc_name name of next filesystem
2239 * zc_cookie zap cursor
2240 * zc_objset_stats stats
2241 * zc_nvlist_dst property nvlist
2242 * zc_nvlist_dst_size size of property nvlist
2245 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2250 size_t orig_len = strlen(zc->zc_name);
2253 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2254 if (error == ENOENT)
2255 error = SET_ERROR(ESRCH);
2259 p = strrchr(zc->zc_name, '/');
2260 if (p == NULL || p[1] != '\0')
2261 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2262 p = zc->zc_name + strlen(zc->zc_name);
2265 error = dmu_dir_list_next(os,
2266 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2267 NULL, &zc->zc_cookie);
2268 if (error == ENOENT)
2269 error = SET_ERROR(ESRCH);
2270 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2271 dmu_objset_rele(os, FTAG);
2274 * If it's an internal dataset (ie. with a '$' in its name),
2275 * don't try to get stats for it, otherwise we'll return ENOENT.
2277 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2278 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2279 if (error == ENOENT) {
2280 /* We lost a race with destroy, get the next one. */
2281 zc->zc_name[orig_len] = '\0';
2290 * zc_name name of filesystem
2291 * zc_cookie zap cursor
2292 * zc_nvlist_src iteration range nvlist
2293 * zc_nvlist_src_size size of iteration range nvlist
2296 * zc_name name of next snapshot
2297 * zc_objset_stats stats
2298 * zc_nvlist_dst property nvlist
2299 * zc_nvlist_dst_size size of property nvlist
2302 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2305 objset_t *os, *ossnap;
2307 uint64_t min_txg = 0, max_txg = 0;
2309 if (zc->zc_nvlist_src_size != 0) {
2310 nvlist_t *props = NULL;
2311 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2312 zc->zc_iflags, &props);
2315 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2317 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2322 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2324 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
2328 * A dataset name of maximum length cannot have any snapshots,
2329 * so exit immediately.
2331 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2332 ZFS_MAX_DATASET_NAME_LEN) {
2333 dmu_objset_rele(os, FTAG);
2334 return (SET_ERROR(ESRCH));
2337 while (error == 0) {
2338 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2339 error = SET_ERROR(EINTR);
2343 error = dmu_snapshot_list_next(os,
2344 sizeof (zc->zc_name) - strlen(zc->zc_name),
2345 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2346 &zc->zc_cookie, NULL);
2347 if (error == ENOENT) {
2348 error = SET_ERROR(ESRCH);
2350 } else if (error != 0) {
2354 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2359 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2360 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2361 dsl_dataset_rele(ds, FTAG);
2362 /* undo snapshot name append */
2363 *(strchr(zc->zc_name, '@') + 1) = '\0';
2368 if (zc->zc_simple) {
2369 dsl_dataset_fast_stat(ds, &zc->zc_objset_stats);
2370 dsl_dataset_rele(ds, FTAG);
2374 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2375 dsl_dataset_rele(ds, FTAG);
2378 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2379 dsl_dataset_rele(ds, FTAG);
2382 dsl_dataset_rele(ds, FTAG);
2386 dmu_objset_rele(os, FTAG);
2387 /* if we failed, undo the @ that we tacked on to zc_name */
2389 *strchr(zc->zc_name, '@') = '\0';
2394 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2396 const char *propname = nvpair_name(pair);
2398 unsigned int vallen;
2399 const char *dash, *domain;
2400 zfs_userquota_prop_t type;
2406 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2408 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2409 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2411 return (SET_ERROR(EINVAL));
2415 * A correctly constructed propname is encoded as
2416 * userquota@<rid>-<domain>.
2418 if ((dash = strchr(propname, '-')) == NULL ||
2419 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2421 return (SET_ERROR(EINVAL));
2428 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2430 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2431 zfsvfs_rele(zfsvfs, FTAG);
2438 * If the named property is one that has a special function to set its value,
2439 * return 0 on success and a positive error code on failure; otherwise if it is
2440 * not one of the special properties handled by this function, return -1.
2442 * XXX: It would be better for callers of the property interface if we handled
2443 * these special cases in dsl_prop.c (in the dsl layer).
2446 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2449 const char *propname = nvpair_name(pair);
2450 zfs_prop_t prop = zfs_name_to_prop(propname);
2451 uint64_t intval = 0;
2452 const char *strval = NULL;
2455 if (prop == ZPROP_USERPROP) {
2456 if (zfs_prop_userquota(propname))
2457 return (zfs_prop_set_userquota(dsname, pair));
2461 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2463 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2464 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2468 /* all special properties are numeric except for keylocation */
2469 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2470 strval = fnvpair_value_string(pair);
2472 intval = fnvpair_value_uint64(pair);
2476 case ZFS_PROP_QUOTA:
2477 err = dsl_dir_set_quota(dsname, source, intval);
2479 case ZFS_PROP_REFQUOTA:
2480 err = dsl_dataset_set_refquota(dsname, source, intval);
2482 case ZFS_PROP_FILESYSTEM_LIMIT:
2483 case ZFS_PROP_SNAPSHOT_LIMIT:
2484 if (intval == UINT64_MAX) {
2485 /* clearing the limit, just do it */
2488 err = dsl_dir_activate_fs_ss_limit(dsname);
2491 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2492 * default path to set the value in the nvlist.
2497 case ZFS_PROP_KEYLOCATION:
2498 err = dsl_crypto_can_set_keylocation(dsname, strval);
2501 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2502 * default path to set the value in the nvlist.
2507 case ZFS_PROP_RESERVATION:
2508 err = dsl_dir_set_reservation(dsname, source, intval);
2510 case ZFS_PROP_REFRESERVATION:
2511 err = dsl_dataset_set_refreservation(dsname, source, intval);
2513 case ZFS_PROP_COMPRESSION:
2514 err = dsl_dataset_set_compression(dsname, source, intval);
2516 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2517 * default path to set the value in the nvlist.
2522 case ZFS_PROP_VOLSIZE:
2523 err = zvol_set_volsize(dsname, intval);
2525 case ZFS_PROP_VOLTHREADING:
2526 err = zvol_set_volthreading(dsname, intval);
2528 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2529 * default path to set the value in the nvlist.
2534 case ZFS_PROP_SNAPDEV:
2535 case ZFS_PROP_VOLMODE:
2536 err = zvol_set_common(dsname, prop, source, intval);
2538 case ZFS_PROP_READONLY:
2539 err = zvol_set_ro(dsname, intval);
2541 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2542 * default path to set the value in the nvlist.
2547 case ZFS_PROP_VERSION:
2551 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2554 err = zfs_set_version(zfsvfs, intval);
2555 zfsvfs_rele(zfsvfs, FTAG);
2557 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2560 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2561 (void) strlcpy(zc->zc_name, dsname,
2562 sizeof (zc->zc_name));
2563 (void) zfs_ioc_userspace_upgrade(zc);
2564 (void) zfs_ioc_id_quota_upgrade(zc);
2565 kmem_free(zc, sizeof (zfs_cmd_t));
2577 zfs_is_namespace_prop(zfs_prop_t prop)
2581 case ZFS_PROP_ATIME:
2582 case ZFS_PROP_RELATIME:
2583 case ZFS_PROP_DEVICES:
2585 case ZFS_PROP_SETUID:
2586 case ZFS_PROP_READONLY:
2587 case ZFS_PROP_XATTR:
2588 case ZFS_PROP_NBMAND:
2597 * This function is best effort. If it fails to set any of the given properties,
2598 * it continues to set as many as it can and returns the last error
2599 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2600 * with the list of names of all the properties that failed along with the
2601 * corresponding error numbers.
2603 * If every property is set successfully, zero is returned and errlist is not
2607 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2616 boolean_t should_update_mount_cache = B_FALSE;
2618 nvlist_t *genericnvl = fnvlist_alloc();
2619 nvlist_t *retrynvl = fnvlist_alloc();
2622 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2623 const char *propname = nvpair_name(pair);
2624 zfs_prop_t prop = zfs_name_to_prop(propname);
2627 /* decode the property value */
2629 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2631 attrs = fnvpair_value_nvlist(pair);
2632 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2634 err = SET_ERROR(EINVAL);
2637 /* Validate value type */
2638 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2639 /* inherited properties are expected to be booleans */
2640 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2641 err = SET_ERROR(EINVAL);
2642 } else if (err == 0 && prop == ZPROP_USERPROP) {
2643 if (zfs_prop_user(propname)) {
2644 if (nvpair_type(propval) != DATA_TYPE_STRING)
2645 err = SET_ERROR(EINVAL);
2646 } else if (zfs_prop_userquota(propname)) {
2647 if (nvpair_type(propval) !=
2648 DATA_TYPE_UINT64_ARRAY)
2649 err = SET_ERROR(EINVAL);
2651 err = SET_ERROR(EINVAL);
2653 } else if (err == 0) {
2654 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2655 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2656 err = SET_ERROR(EINVAL);
2657 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2660 intval = fnvpair_value_uint64(propval);
2662 switch (zfs_prop_get_type(prop)) {
2663 case PROP_TYPE_NUMBER:
2665 case PROP_TYPE_STRING:
2666 err = SET_ERROR(EINVAL);
2668 case PROP_TYPE_INDEX:
2669 if (zfs_prop_index_to_string(prop,
2670 intval, &unused) != 0)
2672 SET_ERROR(ZFS_ERR_BADPROP);
2676 "unknown property type");
2679 err = SET_ERROR(EINVAL);
2683 /* Validate permissions */
2685 err = zfs_check_settable(dsname, pair, CRED());
2688 if (source == ZPROP_SRC_INHERITED)
2689 err = -1; /* does not need special handling */
2691 err = zfs_prop_set_special(dsname, source,
2695 * For better performance we build up a list of
2696 * properties to set in a single transaction.
2698 err = nvlist_add_nvpair(genericnvl, pair);
2699 } else if (err != 0 && nvl != retrynvl) {
2701 * This may be a spurious error caused by
2702 * receiving quota and reservation out of order.
2703 * Try again in a second pass.
2705 err = nvlist_add_nvpair(retrynvl, pair);
2710 if (errlist != NULL)
2711 fnvlist_add_int32(errlist, propname, err);
2715 if (zfs_is_namespace_prop(prop))
2716 should_update_mount_cache = B_TRUE;
2719 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2724 if (nvlist_empty(genericnvl))
2728 * Try to set them all in one batch.
2730 err = dsl_props_set(dsname, source, genericnvl);
2735 * If batching fails, we still want to set as many properties as we
2736 * can, so try setting them individually.
2739 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2740 const char *propname = nvpair_name(pair);
2743 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2745 attrs = fnvpair_value_nvlist(pair);
2746 propval = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
2749 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2750 strval = fnvpair_value_string(propval);
2751 err = dsl_prop_set_string(dsname, propname,
2753 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2754 err = dsl_prop_inherit(dsname, propname, source);
2756 intval = fnvpair_value_uint64(propval);
2757 err = dsl_prop_set_int(dsname, propname, source,
2762 if (errlist != NULL) {
2763 fnvlist_add_int32(errlist, propname, err);
2770 if (should_update_mount_cache)
2771 zfs_ioctl_update_mount_cache(dsname);
2773 nvlist_free(genericnvl);
2774 nvlist_free(retrynvl);
2780 * Check that all the properties are valid user properties.
2783 zfs_check_userprops(nvlist_t *nvl)
2785 nvpair_t *pair = NULL;
2787 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2788 const char *propname = nvpair_name(pair);
2790 if (!zfs_prop_user(propname) ||
2791 nvpair_type(pair) != DATA_TYPE_STRING)
2792 return (SET_ERROR(EINVAL));
2794 if (strlen(propname) >= ZAP_MAXNAMELEN)
2795 return (SET_ERROR(ENAMETOOLONG));
2797 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2798 return (SET_ERROR(E2BIG));
2804 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2808 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2811 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2812 if (nvlist_exists(skipped, nvpair_name(pair)))
2815 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2820 clear_received_props(const char *dsname, nvlist_t *props,
2824 nvlist_t *cleared_props = NULL;
2825 props_skip(props, skipped, &cleared_props);
2826 if (!nvlist_empty(cleared_props)) {
2828 * Acts on local properties until the dataset has received
2829 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2831 zprop_source_t flags = (ZPROP_SRC_NONE |
2832 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2833 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2835 nvlist_free(cleared_props);
2841 * zc_name name of filesystem
2842 * zc_value name of property to set
2843 * zc_nvlist_src{_size} nvlist of properties to apply
2844 * zc_cookie received properties flag
2847 * zc_nvlist_dst{_size} error for each unapplied received property
2850 zfs_ioc_set_prop(zfs_cmd_t *zc)
2853 boolean_t received = zc->zc_cookie;
2854 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2859 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2860 zc->zc_iflags, &nvl)) != 0)
2864 nvlist_t *origprops;
2866 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2867 (void) clear_received_props(zc->zc_name,
2869 nvlist_free(origprops);
2872 error = dsl_prop_set_hasrecvd(zc->zc_name);
2875 errors = fnvlist_alloc();
2877 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2879 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2880 (void) put_nvlist(zc, errors);
2883 nvlist_free(errors);
2890 * zc_name name of filesystem
2891 * zc_value name of property to inherit
2892 * zc_cookie revert to received value if TRUE
2897 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2899 const char *propname = zc->zc_value;
2900 zfs_prop_t prop = zfs_name_to_prop(propname);
2901 boolean_t received = zc->zc_cookie;
2902 zprop_source_t source = (received
2903 ? ZPROP_SRC_NONE /* revert to received value, if any */
2904 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2912 * Only check this in the non-received case. We want to allow
2913 * 'inherit -S' to revert non-inheritable properties like quota
2914 * and reservation to the received or default values even though
2915 * they are not considered inheritable.
2917 if (prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop))
2918 return (SET_ERROR(EINVAL));
2921 if (prop == ZPROP_USERPROP) {
2922 if (!zfs_prop_user(propname))
2923 return (SET_ERROR(EINVAL));
2925 type = PROP_TYPE_STRING;
2926 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2927 return (SET_ERROR(EINVAL));
2929 type = zfs_prop_get_type(prop);
2933 * zfs_prop_set_special() expects properties in the form of an
2934 * nvpair with type info.
2936 dummy = fnvlist_alloc();
2939 case PROP_TYPE_STRING:
2940 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2942 case PROP_TYPE_NUMBER:
2943 case PROP_TYPE_INDEX:
2944 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2947 err = SET_ERROR(EINVAL);
2951 pair = nvlist_next_nvpair(dummy, NULL);
2953 err = SET_ERROR(EINVAL);
2955 err = zfs_prop_set_special(zc->zc_name, source, pair);
2956 if (err == -1) /* property is not "special", needs handling */
2957 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2967 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2974 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2975 zc->zc_iflags, &props)))
2979 * If the only property is the configfile, then just do a spa_lookup()
2980 * to handle the faulted case.
2982 pair = nvlist_next_nvpair(props, NULL);
2983 if (pair != NULL && strcmp(nvpair_name(pair),
2984 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2985 nvlist_next_nvpair(props, pair) == NULL) {
2986 mutex_enter(&spa_namespace_lock);
2987 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2988 spa_configfile_set(spa, props, B_FALSE);
2989 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
2991 mutex_exit(&spa_namespace_lock);
2998 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3003 error = spa_prop_set(spa, props);
3006 spa_close(spa, FTAG);
3012 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
3016 nvlist_t *nvp = NULL;
3018 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3020 * If the pool is faulted, there may be properties we can still
3021 * get (such as altroot and cachefile), so attempt to get them
3024 mutex_enter(&spa_namespace_lock);
3025 if ((spa = spa_lookup(zc->zc_name)) != NULL)
3026 error = spa_prop_get(spa, &nvp);
3027 mutex_exit(&spa_namespace_lock);
3029 error = spa_prop_get(spa, &nvp);
3030 spa_close(spa, FTAG);
3033 if (error == 0 && zc->zc_nvlist_dst != 0)
3034 error = put_nvlist(zc, nvp);
3036 error = SET_ERROR(EFAULT);
3044 * "vdevprops_set_vdev" -> guid
3045 * "vdevprops_set_props" -> { prop -> value }
3048 * outnvl: propname -> error code (int32)
3050 static const zfs_ioc_key_t zfs_keys_vdev_set_props[] = {
3051 {ZPOOL_VDEV_PROPS_SET_VDEV, DATA_TYPE_UINT64, 0},
3052 {ZPOOL_VDEV_PROPS_SET_PROPS, DATA_TYPE_NVLIST, 0}
3056 zfs_ioc_vdev_set_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3063 /* Early validation */
3064 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
3066 return (SET_ERROR(EINVAL));
3069 return (SET_ERROR(EINVAL));
3071 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3074 ASSERT(spa_writeable(spa));
3076 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3077 spa_close(spa, FTAG);
3078 return (SET_ERROR(ENOENT));
3081 error = vdev_prop_set(vd, innvl, outnvl);
3083 spa_close(spa, FTAG);
3090 * "vdevprops_get_vdev" -> guid
3091 * (optional) "vdevprops_get_props" -> { propname -> propid }
3094 * outnvl: propname -> value
3096 static const zfs_ioc_key_t zfs_keys_vdev_get_props[] = {
3097 {ZPOOL_VDEV_PROPS_GET_VDEV, DATA_TYPE_UINT64, 0},
3098 {ZPOOL_VDEV_PROPS_GET_PROPS, DATA_TYPE_NVLIST, ZK_OPTIONAL}
3102 zfs_ioc_vdev_get_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3109 /* Early validation */
3110 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
3112 return (SET_ERROR(EINVAL));
3115 return (SET_ERROR(EINVAL));
3117 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3120 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3121 spa_close(spa, FTAG);
3122 return (SET_ERROR(ENOENT));
3125 error = vdev_prop_get(vd, innvl, outnvl);
3127 spa_close(spa, FTAG);
3134 * zc_name name of filesystem
3135 * zc_nvlist_src{_size} nvlist of delegated permissions
3136 * zc_perm_action allow/unallow flag
3141 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3144 nvlist_t *fsaclnv = NULL;
3146 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3147 zc->zc_iflags, &fsaclnv)) != 0)
3151 * Verify nvlist is constructed correctly
3153 if (zfs_deleg_verify_nvlist(fsaclnv) != 0) {
3154 nvlist_free(fsaclnv);
3155 return (SET_ERROR(EINVAL));
3159 * If we don't have PRIV_SYS_MOUNT, then validate
3160 * that user is allowed to hand out each permission in
3164 error = secpolicy_zfs(CRED());
3166 if (zc->zc_perm_action == B_FALSE) {
3167 error = dsl_deleg_can_allow(zc->zc_name,
3170 error = dsl_deleg_can_unallow(zc->zc_name,
3176 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3178 nvlist_free(fsaclnv);
3184 * zc_name name of filesystem
3187 * zc_nvlist_src{_size} nvlist of delegated permissions
3190 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3195 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3196 error = put_nvlist(zc, nvp);
3204 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3206 zfs_creat_t *zct = arg;
3208 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3211 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3215 * os parent objset pointer (NULL if root fs)
3216 * fuids_ok fuids allowed in this version of the spa?
3217 * sa_ok SAs allowed in this version of the spa?
3218 * createprops list of properties requested by creator
3221 * zplprops values for the zplprops we attach to the master node object
3222 * is_ci true if requested file system will be purely case-insensitive
3224 * Determine the settings for utf8only, normalization and
3225 * casesensitivity. Specific values may have been requested by the
3226 * creator and/or we can inherit values from the parent dataset. If
3227 * the file system is of too early a vintage, a creator can not
3228 * request settings for these properties, even if the requested
3229 * setting is the default value. We don't actually want to create dsl
3230 * properties for these, so remove them from the source nvlist after
3234 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3235 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3236 nvlist_t *zplprops, boolean_t *is_ci)
3238 uint64_t sense = ZFS_PROP_UNDEFINED;
3239 uint64_t norm = ZFS_PROP_UNDEFINED;
3240 uint64_t u8 = ZFS_PROP_UNDEFINED;
3243 ASSERT(zplprops != NULL);
3245 /* parent dataset must be a filesystem */
3246 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3247 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3250 * Pull out creator prop choices, if any.
3253 (void) nvlist_lookup_uint64(createprops,
3254 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3255 (void) nvlist_lookup_uint64(createprops,
3256 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3257 (void) nvlist_remove_all(createprops,
3258 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3259 (void) nvlist_lookup_uint64(createprops,
3260 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3261 (void) nvlist_remove_all(createprops,
3262 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3263 (void) nvlist_lookup_uint64(createprops,
3264 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3265 (void) nvlist_remove_all(createprops,
3266 zfs_prop_to_name(ZFS_PROP_CASE));
3270 * If the zpl version requested is whacky or the file system
3271 * or pool is version is too "young" to support normalization
3272 * and the creator tried to set a value for one of the props,
3275 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3276 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3277 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3278 (zplver < ZPL_VERSION_NORMALIZATION &&
3279 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3280 sense != ZFS_PROP_UNDEFINED)))
3281 return (SET_ERROR(ENOTSUP));
3284 * Put the version in the zplprops
3286 VERIFY(nvlist_add_uint64(zplprops,
3287 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3289 if (norm == ZFS_PROP_UNDEFINED &&
3290 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3292 VERIFY(nvlist_add_uint64(zplprops,
3293 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3296 * If we're normalizing, names must always be valid UTF-8 strings.
3300 if (u8 == ZFS_PROP_UNDEFINED &&
3301 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3303 VERIFY(nvlist_add_uint64(zplprops,
3304 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3306 if (sense == ZFS_PROP_UNDEFINED &&
3307 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3309 VERIFY(nvlist_add_uint64(zplprops,
3310 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3313 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3319 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3320 nvlist_t *zplprops, boolean_t *is_ci)
3322 boolean_t fuids_ok, sa_ok;
3323 uint64_t zplver = ZPL_VERSION;
3324 objset_t *os = NULL;
3325 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3330 zfs_get_parent(dataset, parentname, sizeof (parentname));
3332 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3335 spa_vers = spa_version(spa);
3336 spa_close(spa, FTAG);
3338 zplver = zfs_zpl_version_map(spa_vers);
3339 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3340 sa_ok = (zplver >= ZPL_VERSION_SA);
3343 * Open parent object set so we can inherit zplprop values.
3345 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3348 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3350 dmu_objset_rele(os, FTAG);
3355 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3356 nvlist_t *zplprops, boolean_t *is_ci)
3360 uint64_t zplver = ZPL_VERSION;
3363 zplver = zfs_zpl_version_map(spa_vers);
3364 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3365 sa_ok = (zplver >= ZPL_VERSION_SA);
3367 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3368 createprops, zplprops, is_ci);
3374 * "type" -> dmu_objset_type_t (int32)
3375 * (optional) "props" -> { prop -> value }
3376 * (optional) "hidden_args" -> { "wkeydata" -> value }
3377 * raw uint8_t array of encryption wrapping key data (32 bytes)
3380 * outnvl: propname -> error code (int32)
3383 static const zfs_ioc_key_t zfs_keys_create[] = {
3384 {"type", DATA_TYPE_INT32, 0},
3385 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3386 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3390 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3393 zfs_creat_t zct = { 0 };
3394 nvlist_t *nvprops = NULL;
3395 nvlist_t *hidden_args = NULL;
3396 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3397 dmu_objset_type_t type;
3398 boolean_t is_insensitive = B_FALSE;
3399 dsl_crypto_params_t *dcp = NULL;
3401 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3402 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3403 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3407 cbfunc = zfs_create_cb;
3411 cbfunc = zvol_create_cb;
3418 if (strchr(fsname, '@') ||
3419 strchr(fsname, '%'))
3420 return (SET_ERROR(EINVAL));
3422 zct.zct_props = nvprops;
3425 return (SET_ERROR(EINVAL));
3427 if (type == DMU_OST_ZVOL) {
3428 uint64_t volsize, volblocksize;
3430 if (nvprops == NULL)
3431 return (SET_ERROR(EINVAL));
3432 if (nvlist_lookup_uint64(nvprops,
3433 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3434 return (SET_ERROR(EINVAL));
3436 if ((error = nvlist_lookup_uint64(nvprops,
3437 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3438 &volblocksize)) != 0 && error != ENOENT)
3439 return (SET_ERROR(EINVAL));
3442 volblocksize = zfs_prop_default_numeric(
3443 ZFS_PROP_VOLBLOCKSIZE);
3445 if ((error = zvol_check_volblocksize(fsname,
3446 volblocksize)) != 0 ||
3447 (error = zvol_check_volsize(volsize,
3448 volblocksize)) != 0)
3450 } else if (type == DMU_OST_ZFS) {
3454 * We have to have normalization and
3455 * case-folding flags correct when we do the
3456 * file system creation, so go figure them out
3459 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3460 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3461 error = zfs_fill_zplprops(fsname, nvprops,
3462 zct.zct_zplprops, &is_insensitive);
3464 nvlist_free(zct.zct_zplprops);
3469 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3472 nvlist_free(zct.zct_zplprops);
3476 error = dmu_objset_create(fsname, type,
3477 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3479 nvlist_free(zct.zct_zplprops);
3480 dsl_crypto_params_free(dcp, !!error);
3483 * It would be nice to do this atomically.
3486 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3493 * Volumes will return EBUSY and cannot be destroyed
3494 * until all asynchronous minor handling (e.g. from
3495 * setting the volmode property) has completed. Wait for
3496 * the spa_zvol_taskq to drain then retry.
3498 error2 = dsl_destroy_head(fsname);
3499 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3500 error2 = spa_open(fsname, &spa, FTAG);
3502 taskq_wait(spa->spa_zvol_taskq);
3503 spa_close(spa, FTAG);
3505 error2 = dsl_destroy_head(fsname);
3514 * "origin" -> name of origin snapshot
3515 * (optional) "props" -> { prop -> value }
3516 * (optional) "hidden_args" -> { "wkeydata" -> value }
3517 * raw uint8_t array of encryption wrapping key data (32 bytes)
3521 * outnvl: propname -> error code (int32)
3523 static const zfs_ioc_key_t zfs_keys_clone[] = {
3524 {"origin", DATA_TYPE_STRING, 0},
3525 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3526 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3530 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3533 nvlist_t *nvprops = NULL;
3534 const char *origin_name;
3536 origin_name = fnvlist_lookup_string(innvl, "origin");
3537 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3539 if (strchr(fsname, '@') ||
3540 strchr(fsname, '%'))
3541 return (SET_ERROR(EINVAL));
3543 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3544 return (SET_ERROR(EINVAL));
3546 error = dmu_objset_clone(fsname, origin_name);
3549 * It would be nice to do this atomically.
3552 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3555 (void) dsl_destroy_head(fsname);
3560 static const zfs_ioc_key_t zfs_keys_remap[] = {
3565 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3567 /* This IOCTL is no longer supported. */
3568 (void) fsname, (void) innvl, (void) outnvl;
3574 * "snaps" -> { snapshot1, snapshot2 }
3575 * (optional) "props" -> { prop -> value (string) }
3578 * outnvl: snapshot -> error code (int32)
3580 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3581 {"snaps", DATA_TYPE_NVLIST, 0},
3582 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3586 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3589 nvlist_t *props = NULL;
3593 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3594 if (!nvlist_empty(props) &&
3595 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3596 return (SET_ERROR(ENOTSUP));
3597 if ((error = zfs_check_userprops(props)) != 0)
3600 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3601 poollen = strlen(poolname);
3602 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3603 pair = nvlist_next_nvpair(snaps, pair)) {
3604 const char *name = nvpair_name(pair);
3605 char *cp = strchr(name, '@');
3608 * The snap name must contain an @, and the part after it must
3609 * contain only valid characters.
3612 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3613 return (SET_ERROR(EINVAL));
3616 * The snap must be in the specified pool.
3618 if (strncmp(name, poolname, poollen) != 0 ||
3619 (name[poollen] != '/' && name[poollen] != '@'))
3620 return (SET_ERROR(EXDEV));
3623 * Check for permission to set the properties on the fs.
3625 if (!nvlist_empty(props)) {
3627 error = zfs_secpolicy_write_perms(name,
3628 ZFS_DELEG_PERM_USERPROP, CRED());
3634 /* This must be the only snap of this fs. */
3635 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3636 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3637 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3639 return (SET_ERROR(EXDEV));
3644 error = dsl_dataset_snapshot(snaps, props, outnvl);
3650 * innvl: "message" -> string
3652 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3653 {"message", DATA_TYPE_STRING, 0},
3657 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3659 (void) unused, (void) outnvl;
3660 const char *message;
3666 * The poolname in the ioctl is not set, we get it from the TSD,
3667 * which was set at the end of the last successful ioctl that allows
3668 * logging. The secpolicy func already checked that it is set.
3669 * Only one log ioctl is allowed after each successful ioctl, so
3670 * we clear the TSD here.
3672 poolname = tsd_get(zfs_allow_log_key);
3673 if (poolname == NULL)
3674 return (SET_ERROR(EINVAL));
3675 (void) tsd_set(zfs_allow_log_key, NULL);
3676 error = spa_open(poolname, &spa, FTAG);
3677 kmem_strfree(poolname);
3681 message = fnvlist_lookup_string(innvl, "message");
3683 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3684 spa_close(spa, FTAG);
3685 return (SET_ERROR(ENOTSUP));
3688 error = spa_history_log(spa, message);
3689 spa_close(spa, FTAG);
3694 * This ioctl is used to set the bootenv configuration on the current
3695 * pool. This configuration is stored in the second padding area of the label,
3696 * and it is used by the bootloader(s) to store the bootloader and/or system
3698 * The data is stored as nvlist data stream, and is protected by
3699 * an embedded checksum.
3700 * The version can have two possible values:
3701 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3702 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3704 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3705 {"version", DATA_TYPE_UINT64, 0},
3706 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3710 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3715 if ((error = spa_open(name, &spa, FTAG)) != 0)
3717 spa_vdev_state_enter(spa, SCL_ALL);
3718 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3719 (void) spa_vdev_state_exit(spa, NULL, 0);
3720 spa_close(spa, FTAG);
3724 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3729 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3734 if ((error = spa_open(name, &spa, FTAG)) != 0)
3736 spa_vdev_state_enter(spa, SCL_ALL);
3737 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3738 (void) spa_vdev_state_exit(spa, NULL, 0);
3739 spa_close(spa, FTAG);
3744 * The dp_config_rwlock must not be held when calling this, because the
3745 * unmount may need to write out data.
3747 * This function is best-effort. Callers must deal gracefully if it
3748 * remains mounted (or is remounted after this call).
3750 * Returns 0 if the argument is not a snapshot, or it is not currently a
3751 * filesystem, or we were able to unmount it. Returns error code otherwise.
3754 zfs_unmount_snap(const char *snapname)
3756 if (strchr(snapname, '@') == NULL)
3759 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE);
3763 zfs_unmount_snap_cb(const char *snapname, void *arg)
3766 zfs_unmount_snap(snapname);
3771 * When a clone is destroyed, its origin may also need to be destroyed,
3772 * in which case it must be unmounted. This routine will do that unmount
3776 zfs_destroy_unmount_origin(const char *fsname)
3782 error = dmu_objset_hold(fsname, FTAG, &os);
3785 ds = dmu_objset_ds(os);
3786 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3787 char originname[ZFS_MAX_DATASET_NAME_LEN];
3788 dsl_dataset_name(ds->ds_prev, originname);
3789 dmu_objset_rele(os, FTAG);
3790 zfs_unmount_snap(originname);
3792 dmu_objset_rele(os, FTAG);
3798 * "snaps" -> { snapshot1, snapshot2 }
3799 * (optional boolean) "defer"
3802 * outnvl: snapshot -> error code (int32)
3804 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3805 {"snaps", DATA_TYPE_NVLIST, 0},
3806 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3810 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3818 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3819 defer = nvlist_exists(innvl, "defer");
3821 poollen = strlen(poolname);
3822 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3823 pair = nvlist_next_nvpair(snaps, pair)) {
3824 const char *name = nvpair_name(pair);
3827 * The snap must be in the specified pool to prevent the
3828 * invalid removal of zvol minors below.
3830 if (strncmp(name, poolname, poollen) != 0 ||
3831 (name[poollen] != '/' && name[poollen] != '@'))
3832 return (SET_ERROR(EXDEV));
3834 zfs_unmount_snap(nvpair_name(pair));
3835 if (spa_open(name, &spa, FTAG) == 0) {
3836 zvol_remove_minors(spa, name, B_TRUE);
3837 spa_close(spa, FTAG);
3841 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3845 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>.
3846 * All bookmarks and snapshots must be in the same pool.
3847 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail.
3850 * new_bookmark1 -> existing_snapshot,
3851 * new_bookmark2 -> existing_bookmark,
3854 * outnvl: bookmark -> error code (int32)
3857 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3858 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3862 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3865 return (dsl_bookmark_create(innvl, outnvl));
3870 * property 1, property 2, ...
3874 * bookmark name 1 -> { property 1, property 2, ... },
3875 * bookmark name 2 -> { property 1, property 2, ... }
3879 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3880 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3884 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3886 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3890 * innvl is not used.
3893 * property 1, property 2, ...
3897 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
3902 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
3906 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3909 bmname = strchr(bookmark, '#');
3911 return (SET_ERROR(EINVAL));
3914 (void) strlcpy(fsname, bookmark, sizeof (fsname));
3915 *(strchr(fsname, '#')) = '\0';
3917 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
3922 * bookmark name 1, bookmark name 2
3925 * outnvl: bookmark -> error code (int32)
3928 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3929 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3933 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3938 poollen = strlen(poolname);
3939 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3940 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3941 const char *name = nvpair_name(pair);
3942 const char *cp = strchr(name, '#');
3945 * The bookmark name must contain an #, and the part after it
3946 * must contain only valid characters.
3949 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3950 return (SET_ERROR(EINVAL));
3953 * The bookmark must be in the specified pool.
3955 if (strncmp(name, poolname, poollen) != 0 ||
3956 (name[poollen] != '/' && name[poollen] != '#'))
3957 return (SET_ERROR(EXDEV));
3960 error = dsl_bookmark_destroy(innvl, outnvl);
3964 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3965 {"program", DATA_TYPE_STRING, 0},
3966 {"arg", DATA_TYPE_ANY, 0},
3967 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3968 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3969 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3973 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3976 const char *program;
3977 uint64_t instrlimit, memlimit;
3978 boolean_t sync_flag;
3979 nvpair_t *nvarg = NULL;
3981 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3982 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3985 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3986 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3988 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3989 memlimit = ZCP_DEFAULT_MEMLIMIT;
3991 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3993 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3994 return (SET_ERROR(EINVAL));
3995 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3996 return (SET_ERROR(EINVAL));
3998 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
4006 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
4011 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4013 (void) innvl, (void) outnvl;
4014 return (spa_checkpoint(poolname));
4021 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
4026 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
4029 (void) innvl, (void) outnvl;
4030 return (spa_checkpoint_discard(poolname));
4035 * zc_name name of dataset to destroy
4036 * zc_defer_destroy mark for deferred destroy
4041 zfs_ioc_destroy(zfs_cmd_t *zc)
4044 dmu_objset_type_t ost;
4047 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4050 ost = dmu_objset_type(os);
4051 dmu_objset_rele(os, FTAG);
4053 if (ost == DMU_OST_ZFS)
4054 zfs_unmount_snap(zc->zc_name);
4056 if (strchr(zc->zc_name, '@')) {
4057 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
4059 err = dsl_destroy_head(zc->zc_name);
4060 if (err == EEXIST) {
4062 * It is possible that the given DS may have
4063 * hidden child (%recv) datasets - "leftovers"
4064 * resulting from the previously interrupted
4067 * 6 extra bytes for /%recv
4069 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
4071 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
4072 zc->zc_name, recv_clone_name) >=
4074 return (SET_ERROR(EINVAL));
4077 * Try to remove the hidden child (%recv) and after
4078 * that try to remove the target dataset.
4079 * If the hidden child (%recv) does not exist
4080 * the original error (EEXIST) will be returned
4082 err = dsl_destroy_head(namebuf);
4084 err = dsl_destroy_head(zc->zc_name);
4085 else if (err == ENOENT)
4086 err = SET_ERROR(EEXIST);
4095 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
4096 * "initialize_vdevs": { -> guids to initialize (nvlist)
4097 * "vdev_path_1": vdev_guid_1, (uint64),
4098 * "vdev_path_2": vdev_guid_2, (uint64),
4104 * "initialize_vdevs": { -> initialization errors (nvlist)
4105 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4106 * "vdev_path_2": errno, ... (uint64)
4111 * EINVAL is returned for an unknown commands or if any of the provided vdev
4112 * guids have be specified with a type other than uint64.
4114 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
4115 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
4116 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4120 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4123 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4125 return (SET_ERROR(EINVAL));
4128 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4129 cmd_type == POOL_INITIALIZE_START ||
4130 cmd_type == POOL_INITIALIZE_SUSPEND ||
4131 cmd_type == POOL_INITIALIZE_UNINIT)) {
4132 return (SET_ERROR(EINVAL));
4135 nvlist_t *vdev_guids;
4136 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4137 &vdev_guids) != 0) {
4138 return (SET_ERROR(EINVAL));
4141 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4142 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4144 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4145 return (SET_ERROR(EINVAL));
4150 int error = spa_open(poolname, &spa, FTAG);
4154 nvlist_t *vdev_errlist = fnvlist_alloc();
4155 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4158 if (fnvlist_size(vdev_errlist) > 0) {
4159 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4162 fnvlist_free(vdev_errlist);
4164 spa_close(spa, FTAG);
4165 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4170 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4171 * "trim_vdevs": { -> guids to TRIM (nvlist)
4172 * "vdev_path_1": vdev_guid_1, (uint64),
4173 * "vdev_path_2": vdev_guid_2, (uint64),
4176 * "trim_rate" -> Target TRIM rate in bytes/sec.
4177 * "trim_secure" -> Set to request a secure TRIM.
4181 * "trim_vdevs": { -> TRIM errors (nvlist)
4182 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4183 * "vdev_path_2": errno, ... (uint64)
4188 * EINVAL is returned for an unknown commands or if any of the provided vdev
4189 * guids have be specified with a type other than uint64.
4191 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4192 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4193 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4194 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4195 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4199 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4202 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4203 return (SET_ERROR(EINVAL));
4205 if (!(cmd_type == POOL_TRIM_CANCEL ||
4206 cmd_type == POOL_TRIM_START ||
4207 cmd_type == POOL_TRIM_SUSPEND)) {
4208 return (SET_ERROR(EINVAL));
4211 nvlist_t *vdev_guids;
4212 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4213 return (SET_ERROR(EINVAL));
4215 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4216 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4218 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4219 return (SET_ERROR(EINVAL));
4223 /* Optional, defaults to maximum rate when not provided */
4225 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4228 /* Optional, defaults to standard TRIM when not provided */
4230 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4236 int error = spa_open(poolname, &spa, FTAG);
4240 nvlist_t *vdev_errlist = fnvlist_alloc();
4241 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4242 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4244 if (fnvlist_size(vdev_errlist) > 0)
4245 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4247 fnvlist_free(vdev_errlist);
4249 spa_close(spa, FTAG);
4250 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4254 * This ioctl waits for activity of a particular type to complete. If there is
4255 * no activity of that type in progress, it returns immediately, and the
4256 * returned value "waited" is false. If there is activity in progress, and no
4257 * tag is passed in, the ioctl blocks until all activity of that type is
4258 * complete, and then returns with "waited" set to true.
4260 * If a tag is provided, it identifies a particular instance of an activity to
4261 * wait for. Currently, this is only valid for use with 'initialize', because
4262 * that is the only activity for which there can be multiple instances running
4263 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4264 * the vdev on which to wait.
4266 * If a thread waiting in the ioctl receives a signal, the call will return
4267 * immediately, and the return value will be EINTR.
4270 * "wait_activity" -> int32_t
4271 * (optional) "wait_tag" -> uint64_t
4274 * outnvl: "waited" -> boolean_t
4276 static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4277 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4278 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4282 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4289 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4292 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4293 error = spa_wait_tag(name, activity, tag, &waited);
4295 error = spa_wait(name, activity, &waited);
4298 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4304 * This ioctl waits for activity of a particular type to complete. If there is
4305 * no activity of that type in progress, it returns immediately, and the
4306 * returned value "waited" is false. If there is activity in progress, and no
4307 * tag is passed in, the ioctl blocks until all activity of that type is
4308 * complete, and then returns with "waited" set to true.
4310 * If a thread waiting in the ioctl receives a signal, the call will return
4311 * immediately, and the return value will be EINTR.
4314 * "wait_activity" -> int32_t
4317 * outnvl: "waited" -> boolean_t
4319 static const zfs_ioc_key_t zfs_keys_fs_wait[] = {
4320 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4324 zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4327 boolean_t waited = B_FALSE;
4333 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0)
4334 return (SET_ERROR(EINVAL));
4336 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0)
4337 return (SET_ERROR(EINVAL));
4339 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
4342 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
4343 dsl_pool_rele(dp, FTAG);
4348 mutex_enter(&dd->dd_activity_lock);
4349 dd->dd_activity_waiters++;
4352 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t
4353 * aren't evicted while we're waiting. Normally this is prevented by
4354 * holding the pool, but we can't do that while we're waiting since
4355 * that would prevent TXGs from syncing out. Some of the functionality
4356 * of long-holds (e.g. preventing deletion) is unnecessary for this
4357 * case, since we would cancel the waiters before proceeding with a
4358 * deletion. An alternative mechanism for keeping the dataset around
4359 * could be developed but this is simpler.
4361 dsl_dataset_long_hold(ds, FTAG);
4362 dsl_pool_rele(dp, FTAG);
4364 error = dsl_dir_wait(dd, ds, activity, &waited);
4366 dsl_dataset_long_rele(ds, FTAG);
4367 dd->dd_activity_waiters--;
4368 if (dd->dd_activity_waiters == 0)
4369 cv_signal(&dd->dd_activity_cv);
4370 mutex_exit(&dd->dd_activity_lock);
4372 dsl_dataset_rele(ds, FTAG);
4375 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited);
4381 * fsname is name of dataset to rollback (to most recent snapshot)
4383 * innvl may contain name of expected target snapshot
4385 * outnvl: "target" -> name of most recent snapshot
4388 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4389 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4393 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4396 zvol_state_handle_t *zv;
4397 const char *target = NULL;
4400 (void) nvlist_lookup_string(innvl, "target", &target);
4401 if (target != NULL) {
4402 const char *cp = strchr(target, '@');
4405 * The snap name must contain an @, and the part after it must
4406 * contain only valid characters.
4409 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4410 return (SET_ERROR(EINVAL));
4413 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4416 ds = dmu_objset_ds(zfsvfs->z_os);
4417 error = zfs_suspend_fs(zfsvfs);
4421 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4423 resume_err = zfs_resume_fs(zfsvfs, ds);
4424 error = error ? error : resume_err;
4426 zfs_vfs_rele(zfsvfs);
4427 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4428 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4432 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4438 recursive_unmount(const char *fsname, void *arg)
4440 const char *snapname = arg;
4443 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4444 zfs_unmount_snap(fullname);
4445 kmem_strfree(fullname);
4452 * snapname is the snapshot to redact.
4454 * "bookname" -> (string)
4455 * shortname of the redaction bookmark to generate
4456 * "snapnv" -> (nvlist, values ignored)
4457 * snapshots to redact snapname with respect to
4463 static const zfs_ioc_key_t zfs_keys_redact[] = {
4464 {"bookname", DATA_TYPE_STRING, 0},
4465 {"snapnv", DATA_TYPE_NVLIST, 0},
4469 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4472 nvlist_t *redactnvl = NULL;
4473 const char *redactbook = NULL;
4475 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4476 return (SET_ERROR(EINVAL));
4477 if (fnvlist_num_pairs(redactnvl) == 0)
4478 return (SET_ERROR(ENXIO));
4479 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4480 return (SET_ERROR(EINVAL));
4482 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4487 * zc_name old name of dataset
4488 * zc_value new name of dataset
4489 * zc_cookie recursive flag (only valid for snapshots)
4494 zfs_ioc_rename(zfs_cmd_t *zc)
4497 dmu_objset_type_t ost;
4498 boolean_t recursive = zc->zc_cookie & 1;
4499 boolean_t nounmount = !!(zc->zc_cookie & 2);
4503 /* "zfs rename" from and to ...%recv datasets should both fail */
4504 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4505 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4506 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4507 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4508 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4509 return (SET_ERROR(EINVAL));
4511 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4514 ost = dmu_objset_type(os);
4515 dmu_objset_rele(os, FTAG);
4517 at = strchr(zc->zc_name, '@');
4519 /* snaps must be in same fs */
4522 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4523 return (SET_ERROR(EXDEV));
4525 if (ost == DMU_OST_ZFS && !nounmount) {
4526 error = dmu_objset_find(zc->zc_name,
4527 recursive_unmount, at + 1,
4528 recursive ? DS_FIND_CHILDREN : 0);
4534 error = dsl_dataset_rename_snapshot(zc->zc_name,
4535 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4540 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4545 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4547 const char *propname = nvpair_name(pair);
4548 boolean_t issnap = (strchr(dsname, '@') != NULL);
4549 zfs_prop_t prop = zfs_name_to_prop(propname);
4550 uint64_t intval, compval;
4553 if (prop == ZPROP_USERPROP) {
4554 if (zfs_prop_user(propname)) {
4555 if ((err = zfs_secpolicy_write_perms(dsname,
4556 ZFS_DELEG_PERM_USERPROP, cr)))
4561 if (!issnap && zfs_prop_userquota(propname)) {
4562 const char *perm = NULL;
4563 const char *uq_prefix =
4564 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4565 const char *gq_prefix =
4566 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4567 const char *uiq_prefix =
4568 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4569 const char *giq_prefix =
4570 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4571 const char *pq_prefix =
4572 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4573 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4574 ZFS_PROP_PROJECTOBJQUOTA];
4576 if (strncmp(propname, uq_prefix,
4577 strlen(uq_prefix)) == 0) {
4578 perm = ZFS_DELEG_PERM_USERQUOTA;
4579 } else if (strncmp(propname, uiq_prefix,
4580 strlen(uiq_prefix)) == 0) {
4581 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4582 } else if (strncmp(propname, gq_prefix,
4583 strlen(gq_prefix)) == 0) {
4584 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4585 } else if (strncmp(propname, giq_prefix,
4586 strlen(giq_prefix)) == 0) {
4587 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4588 } else if (strncmp(propname, pq_prefix,
4589 strlen(pq_prefix)) == 0) {
4590 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4591 } else if (strncmp(propname, piq_prefix,
4592 strlen(piq_prefix)) == 0) {
4593 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4595 /* {USER|GROUP|PROJECT}USED are read-only */
4596 return (SET_ERROR(EINVAL));
4599 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4604 return (SET_ERROR(EINVAL));
4608 return (SET_ERROR(EINVAL));
4610 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4612 * dsl_prop_get_all_impl() returns properties in this
4616 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4617 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4622 * Check that this value is valid for this pool version
4625 case ZFS_PROP_COMPRESSION:
4627 * If the user specified gzip compression, make sure
4628 * the SPA supports it. We ignore any errors here since
4629 * we'll catch them later.
4631 if (nvpair_value_uint64(pair, &intval) == 0) {
4632 compval = ZIO_COMPRESS_ALGO(intval);
4633 if (compval >= ZIO_COMPRESS_GZIP_1 &&
4634 compval <= ZIO_COMPRESS_GZIP_9 &&
4635 zfs_earlier_version(dsname,
4636 SPA_VERSION_GZIP_COMPRESSION)) {
4637 return (SET_ERROR(ENOTSUP));
4640 if (compval == ZIO_COMPRESS_ZLE &&
4641 zfs_earlier_version(dsname,
4642 SPA_VERSION_ZLE_COMPRESSION))
4643 return (SET_ERROR(ENOTSUP));
4645 if (compval == ZIO_COMPRESS_LZ4) {
4648 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4651 if (!spa_feature_is_enabled(spa,
4652 SPA_FEATURE_LZ4_COMPRESS)) {
4653 spa_close(spa, FTAG);
4654 return (SET_ERROR(ENOTSUP));
4656 spa_close(spa, FTAG);
4659 if (compval == ZIO_COMPRESS_ZSTD) {
4662 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4665 if (!spa_feature_is_enabled(spa,
4666 SPA_FEATURE_ZSTD_COMPRESS)) {
4667 spa_close(spa, FTAG);
4668 return (SET_ERROR(ENOTSUP));
4670 spa_close(spa, FTAG);
4675 case ZFS_PROP_COPIES:
4676 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4677 return (SET_ERROR(ENOTSUP));
4680 case ZFS_PROP_VOLBLOCKSIZE:
4681 case ZFS_PROP_RECORDSIZE:
4682 /* Record sizes above 128k need the feature to be enabled */
4683 if (nvpair_value_uint64(pair, &intval) == 0 &&
4684 intval > SPA_OLD_MAXBLOCKSIZE) {
4688 * We don't allow setting the property above 1MB,
4689 * unless the tunable has been changed.
4691 if (intval > zfs_max_recordsize ||
4692 intval > SPA_MAXBLOCKSIZE)
4693 return (SET_ERROR(ERANGE));
4695 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4698 if (!spa_feature_is_enabled(spa,
4699 SPA_FEATURE_LARGE_BLOCKS)) {
4700 spa_close(spa, FTAG);
4701 return (SET_ERROR(ENOTSUP));
4703 spa_close(spa, FTAG);
4707 case ZFS_PROP_DNODESIZE:
4708 /* Dnode sizes above 512 need the feature to be enabled */
4709 if (nvpair_value_uint64(pair, &intval) == 0 &&
4710 intval != ZFS_DNSIZE_LEGACY) {
4713 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4716 if (!spa_feature_is_enabled(spa,
4717 SPA_FEATURE_LARGE_DNODE)) {
4718 spa_close(spa, FTAG);
4719 return (SET_ERROR(ENOTSUP));
4721 spa_close(spa, FTAG);
4725 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4727 * This property could require the allocation classes
4728 * feature to be active for setting, however we allow
4729 * it so that tests of settable properties succeed.
4730 * The CLI will issue a warning in this case.
4734 case ZFS_PROP_SHARESMB:
4735 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4736 return (SET_ERROR(ENOTSUP));
4739 case ZFS_PROP_ACLINHERIT:
4740 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4741 nvpair_value_uint64(pair, &intval) == 0) {
4742 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4743 zfs_earlier_version(dsname,
4744 SPA_VERSION_PASSTHROUGH_X))
4745 return (SET_ERROR(ENOTSUP));
4748 case ZFS_PROP_CHECKSUM:
4749 case ZFS_PROP_DEDUP:
4751 spa_feature_t feature;
4755 /* dedup feature version checks */
4756 if (prop == ZFS_PROP_DEDUP &&
4757 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4758 return (SET_ERROR(ENOTSUP));
4760 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4761 nvpair_value_uint64(pair, &intval) == 0) {
4762 /* check prop value is enabled in features */
4763 feature = zio_checksum_to_feature(
4764 intval & ZIO_CHECKSUM_MASK);
4765 if (feature == SPA_FEATURE_NONE)
4768 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4771 if (!spa_feature_is_enabled(spa, feature)) {
4772 spa_close(spa, FTAG);
4773 return (SET_ERROR(ENOTSUP));
4775 spa_close(spa, FTAG);
4784 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4788 * Removes properties from the given props list that fail permission checks
4789 * needed to clear them and to restore them in case of a receive error. For each
4790 * property, make sure we have both set and inherit permissions.
4792 * Returns the first error encountered if any permission checks fail. If the
4793 * caller provides a non-NULL errlist, it also gives the complete list of names
4794 * of all the properties that failed a permission check along with the
4795 * corresponding error numbers. The caller is responsible for freeing the
4798 * If every property checks out successfully, zero is returned and the list
4799 * pointed at by errlist is NULL.
4802 zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist)
4805 nvpair_t *pair, *next_pair;
4812 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4814 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4815 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4816 pair = nvlist_next_nvpair(props, NULL);
4817 while (pair != NULL) {
4818 next_pair = nvlist_next_nvpair(props, pair);
4820 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4821 sizeof (zc->zc_value));
4822 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4823 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4824 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4825 VERIFY(nvlist_add_int32(errors,
4826 zc->zc_value, err) == 0);
4830 kmem_free(zc, sizeof (zfs_cmd_t));
4832 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4833 nvlist_free(errors);
4836 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4839 if (errlist == NULL)
4840 nvlist_free(errors);
4848 propval_equals(nvpair_t *p1, nvpair_t *p2)
4850 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4851 /* dsl_prop_get_all_impl() format */
4853 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4854 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4858 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4860 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4861 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4865 if (nvpair_type(p1) != nvpair_type(p2))
4868 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4869 const char *valstr1, *valstr2;
4871 VERIFY(nvpair_value_string(p1, &valstr1) == 0);
4872 VERIFY(nvpair_value_string(p2, &valstr2) == 0);
4873 return (strcmp(valstr1, valstr2) == 0);
4875 uint64_t intval1, intval2;
4877 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4878 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4879 return (intval1 == intval2);
4884 * Remove properties from props if they are not going to change (as determined
4885 * by comparison with origprops). Remove them from origprops as well, since we
4886 * do not need to clear or restore properties that won't change.
4889 props_reduce(nvlist_t *props, nvlist_t *origprops)
4891 nvpair_t *pair, *next_pair;
4893 if (origprops == NULL)
4894 return; /* all props need to be received */
4896 pair = nvlist_next_nvpair(props, NULL);
4897 while (pair != NULL) {
4898 const char *propname = nvpair_name(pair);
4901 next_pair = nvlist_next_nvpair(props, pair);
4903 if ((nvlist_lookup_nvpair(origprops, propname,
4904 &match) != 0) || !propval_equals(pair, match))
4905 goto next; /* need to set received value */
4907 /* don't clear the existing received value */
4908 (void) nvlist_remove_nvpair(origprops, match);
4909 /* don't bother receiving the property */
4910 (void) nvlist_remove_nvpair(props, pair);
4917 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4918 * For example, refquota cannot be set until after the receipt of a dataset,
4919 * because in replication streams, an older/earlier snapshot may exceed the
4920 * refquota. We want to receive the older/earlier snapshot, but setting
4921 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4922 * the older/earlier snapshot from being received (with EDQUOT).
4924 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4926 * libzfs will need to be judicious handling errors encountered by props
4927 * extracted by this function.
4930 extract_delay_props(nvlist_t *props)
4932 nvlist_t *delayprops;
4933 nvpair_t *nvp, *tmp;
4934 static const zfs_prop_t delayable[] = {
4936 ZFS_PROP_KEYLOCATION,
4938 * Setting ZFS_PROP_SHARESMB requires the objset type to be
4939 * known, which is not possible prior to receipt of raw sends.
4946 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4948 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4949 nvp = nvlist_next_nvpair(props, nvp)) {
4951 * strcmp() is safe because zfs_prop_to_name() always returns
4954 for (i = 0; delayable[i] != 0; i++) {
4955 if (strcmp(zfs_prop_to_name(delayable[i]),
4956 nvpair_name(nvp)) == 0) {
4960 if (delayable[i] != 0) {
4961 tmp = nvlist_prev_nvpair(props, nvp);
4962 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4963 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4968 if (nvlist_empty(delayprops)) {
4969 nvlist_free(delayprops);
4972 return (delayprops);
4976 zfs_allow_log_destroy(void *arg)
4978 char *poolname = arg;
4980 if (poolname != NULL)
4981 kmem_strfree(poolname);
4985 static boolean_t zfs_ioc_recv_inject_err;
4989 * nvlist 'errors' is always allocated. It will contain descriptions of
4990 * encountered errors, if any. It's the callers responsibility to free.
4993 zfs_ioc_recv_impl(char *tofs, char *tosnap, const char *origin,
4994 nvlist_t *recvprops, nvlist_t *localprops, nvlist_t *hidden_args,
4995 boolean_t force, boolean_t heal, boolean_t resumable, int input_fd,
4996 dmu_replay_record_t *begin_record, uint64_t *read_bytes,
4997 uint64_t *errflags, nvlist_t **errors)
4999 dmu_recv_cookie_t drc;
5001 int props_error = 0;
5003 nvlist_t *local_delayprops = NULL;
5004 nvlist_t *recv_delayprops = NULL;
5005 nvlist_t *inherited_delayprops = NULL;
5006 nvlist_t *origprops = NULL; /* existing properties */
5007 nvlist_t *origrecvd = NULL; /* existing received properties */
5008 boolean_t first_recvd_props = B_FALSE;
5009 boolean_t tofs_was_redacted;
5010 zfs_file_t *input_fp;
5014 *errors = fnvlist_alloc();
5017 if ((input_fp = zfs_file_get(input_fd)) == NULL)
5018 return (SET_ERROR(EBADF));
5020 noff = off = zfs_file_off(input_fp);
5021 error = dmu_recv_begin(tofs, tosnap, begin_record, force, heal,
5022 resumable, localprops, hidden_args, origin, &drc, input_fp,
5026 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
5029 * Set properties before we receive the stream so that they are applied
5030 * to the new data. Note that we must call dmu_recv_stream() if
5031 * dmu_recv_begin() succeeds.
5033 if (recvprops != NULL && !drc.drc_newfs) {
5034 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
5035 SPA_VERSION_RECVD_PROPS &&
5036 !dsl_prop_get_hasrecvd(tofs))
5037 first_recvd_props = B_TRUE;
5040 * If new received properties are supplied, they are to
5041 * completely replace the existing received properties,
5042 * so stash away the existing ones.
5044 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
5045 nvlist_t *errlist = NULL;
5047 * Don't bother writing a property if its value won't
5048 * change (and avoid the unnecessary security checks).
5050 * The first receive after SPA_VERSION_RECVD_PROPS is a
5051 * special case where we blow away all local properties
5054 if (!first_recvd_props)
5055 props_reduce(recvprops, origrecvd);
5056 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
5057 (void) nvlist_merge(*errors, errlist, 0);
5058 nvlist_free(errlist);
5060 if (clear_received_props(tofs, origrecvd,
5061 first_recvd_props ? NULL : recvprops) != 0)
5062 *errflags |= ZPROP_ERR_NOCLEAR;
5064 *errflags |= ZPROP_ERR_NOCLEAR;
5069 * Stash away existing properties so we can restore them on error unless
5070 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
5071 * case "origrecvd" will take care of that.
5073 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
5075 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
5076 if (dsl_prop_get_all(os, &origprops) != 0) {
5077 *errflags |= ZPROP_ERR_NOCLEAR;
5079 dmu_objset_rele(os, FTAG);
5081 *errflags |= ZPROP_ERR_NOCLEAR;
5085 if (recvprops != NULL) {
5086 props_error = dsl_prop_set_hasrecvd(tofs);
5088 if (props_error == 0) {
5089 recv_delayprops = extract_delay_props(recvprops);
5090 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5091 recvprops, *errors);
5095 if (localprops != NULL) {
5096 nvlist_t *oprops = fnvlist_alloc();
5097 nvlist_t *xprops = fnvlist_alloc();
5098 nvpair_t *nvp = NULL;
5100 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5101 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
5103 const char *name = nvpair_name(nvp);
5104 zfs_prop_t prop = zfs_name_to_prop(name);
5105 if (prop != ZPROP_USERPROP) {
5106 if (!zfs_prop_inheritable(prop))
5108 } else if (!zfs_prop_user(name))
5110 fnvlist_add_boolean(xprops, name);
5112 /* -o property=value */
5113 fnvlist_add_nvpair(oprops, nvp);
5117 local_delayprops = extract_delay_props(oprops);
5118 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5120 inherited_delayprops = extract_delay_props(xprops);
5121 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5124 nvlist_free(oprops);
5125 nvlist_free(xprops);
5128 error = dmu_recv_stream(&drc, &off);
5131 zfsvfs_t *zfsvfs = NULL;
5132 zvol_state_handle_t *zv = NULL;
5134 if (getzfsvfs(tofs, &zfsvfs) == 0) {
5138 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
5139 begin_record->drr_u.drr_begin.
5140 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
5142 ds = dmu_objset_ds(zfsvfs->z_os);
5143 error = zfs_suspend_fs(zfsvfs);
5145 * If the suspend fails, then the recv_end will
5146 * likely also fail, and clean up after itself.
5148 end_err = dmu_recv_end(&drc, zfsvfs);
5150 * If the dataset was not redacted, but we received a
5151 * redacted stream onto it, we need to unmount the
5152 * dataset. Otherwise, resume the filesystem.
5154 if (error == 0 && !drc.drc_newfs &&
5155 stream_is_redacted && !tofs_was_redacted) {
5156 error = zfs_end_fs(zfsvfs, ds);
5157 } else if (error == 0) {
5158 error = zfs_resume_fs(zfsvfs, ds);
5160 error = error ? error : end_err;
5161 zfs_vfs_rele(zfsvfs);
5162 } else if ((zv = zvol_suspend(tofs)) != NULL) {
5163 error = dmu_recv_end(&drc, zvol_tag(zv));
5166 error = dmu_recv_end(&drc, NULL);
5169 /* Set delayed properties now, after we're done receiving. */
5170 if (recv_delayprops != NULL && error == 0) {
5171 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5172 recv_delayprops, *errors);
5174 if (local_delayprops != NULL && error == 0) {
5175 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5176 local_delayprops, *errors);
5178 if (inherited_delayprops != NULL && error == 0) {
5179 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5180 inherited_delayprops, *errors);
5185 * Merge delayed props back in with initial props, in case
5186 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
5187 * we have to make sure clear_received_props() includes
5188 * the delayed properties).
5190 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
5191 * using ASSERT() will be just like a VERIFY.
5193 if (recv_delayprops != NULL) {
5194 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
5195 nvlist_free(recv_delayprops);
5197 if (local_delayprops != NULL) {
5198 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
5199 nvlist_free(local_delayprops);
5201 if (inherited_delayprops != NULL) {
5202 ASSERT(nvlist_merge(localprops, inherited_delayprops, 0) == 0);
5203 nvlist_free(inherited_delayprops);
5205 *read_bytes = off - noff;
5208 if (zfs_ioc_recv_inject_err) {
5209 zfs_ioc_recv_inject_err = B_FALSE;
5215 * On error, restore the original props.
5217 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
5218 if (clear_received_props(tofs, recvprops, NULL) != 0) {
5220 * We failed to clear the received properties.
5221 * Since we may have left a $recvd value on the
5222 * system, we can't clear the $hasrecvd flag.
5224 *errflags |= ZPROP_ERR_NORESTORE;
5225 } else if (first_recvd_props) {
5226 dsl_prop_unset_hasrecvd(tofs);
5229 if (origrecvd == NULL && !drc.drc_newfs) {
5230 /* We failed to stash the original properties. */
5231 *errflags |= ZPROP_ERR_NORESTORE;
5235 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5236 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5237 * explicitly if we're restoring local properties cleared in the
5238 * first new-style receive.
5240 if (origrecvd != NULL &&
5241 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5242 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5243 origrecvd, NULL) != 0) {
5245 * We stashed the original properties but failed to
5248 *errflags |= ZPROP_ERR_NORESTORE;
5251 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5252 !first_recvd_props) {
5254 nvlist_t *inheritprops;
5257 if (origprops == NULL) {
5258 /* We failed to stash the original properties. */
5259 *errflags |= ZPROP_ERR_NORESTORE;
5263 /* Restore original props */
5264 setprops = fnvlist_alloc();
5265 inheritprops = fnvlist_alloc();
5267 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5268 const char *name = nvpair_name(nvp);
5272 if (!nvlist_exists(origprops, name)) {
5274 * Property was not present or was explicitly
5275 * inherited before the receive, restore this.
5277 fnvlist_add_boolean(inheritprops, name);
5280 attrs = fnvlist_lookup_nvlist(origprops, name);
5281 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5283 /* Skip received properties */
5284 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5287 if (strcmp(source, tofs) == 0) {
5288 /* Property was locally set */
5289 fnvlist_add_nvlist(setprops, name, attrs);
5291 /* Property was implicitly inherited */
5292 fnvlist_add_boolean(inheritprops, name);
5296 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5298 *errflags |= ZPROP_ERR_NORESTORE;
5299 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5301 *errflags |= ZPROP_ERR_NORESTORE;
5303 nvlist_free(setprops);
5304 nvlist_free(inheritprops);
5307 zfs_file_put(input_fp);
5308 nvlist_free(origrecvd);
5309 nvlist_free(origprops);
5312 error = props_error;
5319 * zc_name name of containing filesystem (unused)
5320 * zc_nvlist_src{_size} nvlist of properties to apply
5321 * zc_nvlist_conf{_size} nvlist of properties to exclude
5322 * (DATA_TYPE_BOOLEAN) and override (everything else)
5323 * zc_value name of snapshot to create
5324 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5325 * zc_cookie file descriptor to recv from
5326 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5327 * zc_guid force flag
5330 * zc_cookie number of bytes read
5331 * zc_obj zprop_errflags_t
5332 * zc_nvlist_dst{_size} error for each unapplied received property
5335 zfs_ioc_recv(zfs_cmd_t *zc)
5337 dmu_replay_record_t begin_record;
5338 nvlist_t *errors = NULL;
5339 nvlist_t *recvdprops = NULL;
5340 nvlist_t *localprops = NULL;
5341 const char *origin = NULL;
5343 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5346 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5347 strchr(zc->zc_value, '@') == NULL ||
5348 strchr(zc->zc_value, '%'))
5349 return (SET_ERROR(EINVAL));
5351 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5352 tosnap = strchr(tofs, '@');
5355 if (zc->zc_nvlist_src != 0 &&
5356 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5357 zc->zc_iflags, &recvdprops)) != 0)
5360 if (zc->zc_nvlist_conf != 0 &&
5361 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5362 zc->zc_iflags, &localprops)) != 0)
5365 if (zc->zc_string[0])
5366 origin = zc->zc_string;
5368 begin_record.drr_type = DRR_BEGIN;
5369 begin_record.drr_payloadlen = 0;
5370 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5372 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5373 NULL, zc->zc_guid, B_FALSE, B_FALSE, zc->zc_cookie, &begin_record,
5374 &zc->zc_cookie, &zc->zc_obj, &errors);
5375 nvlist_free(recvdprops);
5376 nvlist_free(localprops);
5379 * Now that all props, initial and delayed, are set, report the prop
5380 * errors to the caller.
5382 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5383 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5384 put_nvlist(zc, errors) != 0)) {
5386 * Caller made zc->zc_nvlist_dst less than the minimum expected
5387 * size or supplied an invalid address.
5389 error = SET_ERROR(EINVAL);
5392 nvlist_free(errors);
5399 * "snapname" -> full name of the snapshot to create
5400 * (optional) "props" -> received properties to set (nvlist)
5401 * (optional) "localprops" -> override and exclude properties (nvlist)
5402 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5403 * "begin_record" -> non-byteswapped dmu_replay_record_t
5404 * "input_fd" -> file descriptor to read stream from (int32)
5405 * (optional) "force" -> force flag (value ignored)
5406 * (optional) "heal" -> use send stream to heal data corruption
5407 * (optional) "resumable" -> resumable flag (value ignored)
5408 * (optional) "cleanup_fd" -> unused
5409 * (optional) "action_handle" -> unused
5410 * (optional) "hidden_args" -> { "wkeydata" -> value }
5414 * "read_bytes" -> number of bytes read
5415 * "error_flags" -> zprop_errflags_t
5416 * "errors" -> error for each unapplied received property (nvlist)
5419 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5420 {"snapname", DATA_TYPE_STRING, 0},
5421 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5422 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5423 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5424 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5425 {"input_fd", DATA_TYPE_INT32, 0},
5426 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5427 {"heal", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5428 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5429 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5430 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5431 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5435 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5437 dmu_replay_record_t *begin_record;
5438 uint_t begin_record_size;
5439 nvlist_t *errors = NULL;
5440 nvlist_t *recvprops = NULL;
5441 nvlist_t *localprops = NULL;
5442 nvlist_t *hidden_args = NULL;
5443 const char *snapname;
5444 const char *origin = NULL;
5446 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5449 boolean_t resumable;
5450 uint64_t read_bytes = 0;
5451 uint64_t errflags = 0;
5455 snapname = fnvlist_lookup_string(innvl, "snapname");
5457 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5458 strchr(snapname, '@') == NULL ||
5459 strchr(snapname, '%'))
5460 return (SET_ERROR(EINVAL));
5462 (void) strlcpy(tofs, snapname, sizeof (tofs));
5463 tosnap = strchr(tofs, '@');
5466 error = nvlist_lookup_string(innvl, "origin", &origin);
5467 if (error && error != ENOENT)
5470 error = nvlist_lookup_byte_array(innvl, "begin_record",
5471 (uchar_t **)&begin_record, &begin_record_size);
5472 if (error != 0 || begin_record_size != sizeof (*begin_record))
5473 return (SET_ERROR(EINVAL));
5475 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5477 force = nvlist_exists(innvl, "force");
5478 heal = nvlist_exists(innvl, "heal");
5479 resumable = nvlist_exists(innvl, "resumable");
5481 /* we still use "props" here for backwards compatibility */
5482 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5483 if (error && error != ENOENT)
5486 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5487 if (error && error != ENOENT)
5490 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5491 if (error && error != ENOENT)
5494 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5495 hidden_args, force, heal, resumable, input_fd, begin_record,
5496 &read_bytes, &errflags, &errors);
5498 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5499 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5500 fnvlist_add_nvlist(outnvl, "errors", errors);
5502 nvlist_free(errors);
5503 nvlist_free(recvprops);
5504 nvlist_free(localprops);
5509 typedef struct dump_bytes_io {
5517 dump_bytes_cb(void *arg)
5519 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
5526 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL);
5530 dump_bytes(objset_t *os, void *buf, int len, void *arg)
5532 dump_bytes_io_t dbi;
5538 #if defined(HAVE_LARGE_STACKS)
5539 dump_bytes_cb(&dbi);
5542 * The vn_rdwr() call is performed in a taskq to ensure that there is
5543 * always enough stack space to write safely to the target filesystem.
5544 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5545 * them and they are used in vdev_file.c for a similar purpose.
5547 spa_taskq_dispatch_sync(dmu_objset_spa(os), ZIO_TYPE_FREE,
5548 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
5549 #endif /* HAVE_LARGE_STACKS */
5551 return (dbi.dbi_err);
5556 * zc_name name of snapshot to send
5557 * zc_cookie file descriptor to send stream to
5558 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5559 * zc_sendobj objsetid of snapshot to send
5560 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5561 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5562 * output size in zc_objset_type.
5563 * zc_flags lzc_send_flags
5566 * zc_objset_type estimated size, if zc_guid is set
5568 * NOTE: This is no longer the preferred interface, any new functionality
5569 * should be added to zfs_ioc_send_new() instead.
5572 zfs_ioc_send(zfs_cmd_t *zc)
5576 boolean_t estimate = (zc->zc_guid != 0);
5577 boolean_t embedok = (zc->zc_flags & 0x1);
5578 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5579 boolean_t compressok = (zc->zc_flags & 0x4);
5580 boolean_t rawok = (zc->zc_flags & 0x8);
5581 boolean_t savedok = (zc->zc_flags & 0x10);
5583 if (zc->zc_obj != 0) {
5585 dsl_dataset_t *tosnap;
5587 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5591 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5593 dsl_pool_rele(dp, FTAG);
5597 if (dsl_dir_is_clone(tosnap->ds_dir))
5599 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5600 dsl_dataset_rele(tosnap, FTAG);
5601 dsl_pool_rele(dp, FTAG);
5606 dsl_dataset_t *tosnap;
5607 dsl_dataset_t *fromsnap = NULL;
5609 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5613 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5616 dsl_pool_rele(dp, FTAG);
5620 if (zc->zc_fromobj != 0) {
5621 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5624 dsl_dataset_rele(tosnap, FTAG);
5625 dsl_pool_rele(dp, FTAG);
5630 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
5631 compressok || rawok, savedok, &zc->zc_objset_type);
5633 if (fromsnap != NULL)
5634 dsl_dataset_rele(fromsnap, FTAG);
5635 dsl_dataset_rele(tosnap, FTAG);
5636 dsl_pool_rele(dp, FTAG);
5639 dmu_send_outparams_t out = {0};
5641 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
5642 return (SET_ERROR(EBADF));
5644 off = zfs_file_off(fp);
5645 out.dso_outfunc = dump_bytes;
5647 out.dso_dryrun = B_FALSE;
5648 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5649 zc->zc_fromobj, embedok, large_block_ok, compressok,
5650 rawok, savedok, zc->zc_cookie, &off, &out);
5659 * zc_name name of snapshot on which to report progress
5660 * zc_cookie file descriptor of send stream
5663 * zc_cookie number of bytes written in send stream thus far
5664 * zc_objset_type logical size of data traversed by send thus far
5667 zfs_ioc_send_progress(zfs_cmd_t *zc)
5671 dmu_sendstatus_t *dsp = NULL;
5674 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5678 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5680 dsl_pool_rele(dp, FTAG);
5684 mutex_enter(&ds->ds_sendstream_lock);
5687 * Iterate over all the send streams currently active on this dataset.
5688 * If there's one which matches the specified file descriptor _and_ the
5689 * stream was started by the current process, return the progress of
5693 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5694 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5695 if (dsp->dss_outfd == zc->zc_cookie &&
5696 zfs_proc_is_caller(dsp->dss_proc))
5701 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
5703 /* This is the closest thing we have to atomic_read_64. */
5704 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
5706 error = SET_ERROR(ENOENT);
5709 mutex_exit(&ds->ds_sendstream_lock);
5710 dsl_dataset_rele(ds, FTAG);
5711 dsl_pool_rele(dp, FTAG);
5716 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5720 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5721 &zc->zc_inject_record);
5724 zc->zc_guid = (uint64_t)id;
5730 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5732 return (zio_clear_fault((int)zc->zc_guid));
5736 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5738 int id = (int)zc->zc_guid;
5741 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5742 &zc->zc_inject_record);
5750 zfs_ioc_error_log(zfs_cmd_t *zc)
5755 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5758 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5759 &zc->zc_nvlist_dst_size);
5761 spa_close(spa, FTAG);
5767 zfs_ioc_clear(zfs_cmd_t *zc)
5774 * On zpool clear we also fix up missing slogs
5776 mutex_enter(&spa_namespace_lock);
5777 spa = spa_lookup(zc->zc_name);
5779 mutex_exit(&spa_namespace_lock);
5780 return (SET_ERROR(EIO));
5782 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5783 /* we need to let spa_open/spa_load clear the chains */
5784 spa_set_log_state(spa, SPA_LOG_CLEAR);
5786 spa->spa_last_open_failed = 0;
5787 mutex_exit(&spa_namespace_lock);
5789 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5790 error = spa_open(zc->zc_name, &spa, FTAG);
5793 nvlist_t *config = NULL;
5795 if (zc->zc_nvlist_src == 0)
5796 return (SET_ERROR(EINVAL));
5798 if ((error = get_nvlist(zc->zc_nvlist_src,
5799 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5800 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5802 if (config != NULL) {
5805 if ((err = put_nvlist(zc, config)) != 0)
5807 nvlist_free(config);
5809 nvlist_free(policy);
5817 * If multihost is enabled, resuming I/O is unsafe as another
5818 * host may have imported the pool.
5820 if (spa_multihost(spa) && spa_suspended(spa))
5821 return (SET_ERROR(EINVAL));
5823 spa_vdev_state_enter(spa, SCL_NONE);
5825 if (zc->zc_guid == 0) {
5828 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5830 error = SET_ERROR(ENODEV);
5831 (void) spa_vdev_state_exit(spa, NULL, error);
5832 spa_close(spa, FTAG);
5837 vdev_clear(spa, vd);
5839 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5840 NULL : spa->spa_root_vdev, 0);
5843 * Resume any suspended I/Os.
5845 if (zio_resume(spa) != 0)
5846 error = SET_ERROR(EIO);
5848 spa_close(spa, FTAG);
5854 * Reopen all the vdevs associated with the pool.
5857 * "scrub_restart" -> when true and scrub is running, allow to restart
5858 * scrub as the side effect of the reopen (boolean).
5863 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5864 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
5868 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5873 boolean_t rc, scrub_restart = B_TRUE;
5876 error = nvlist_lookup_boolean_value(innvl,
5877 "scrub_restart", &rc);
5882 error = spa_open(pool, &spa, FTAG);
5886 spa_vdev_state_enter(spa, SCL_NONE);
5889 * If the scrub_restart flag is B_FALSE and a scrub is already
5890 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5891 * we don't restart the scrub as a side effect of the reopen.
5892 * Otherwise, let vdev_open() decided if a resilver is required.
5895 spa->spa_scrub_reopen = (!scrub_restart &&
5896 dsl_scan_scrubbing(spa->spa_dsl_pool));
5897 vdev_reopen(spa->spa_root_vdev);
5898 spa->spa_scrub_reopen = B_FALSE;
5900 (void) spa_vdev_state_exit(spa, NULL, 0);
5901 spa_close(spa, FTAG);
5907 * zc_name name of filesystem
5910 * zc_string name of conflicting snapshot, if there is one
5913 zfs_ioc_promote(zfs_cmd_t *zc)
5916 dsl_dataset_t *ds, *ods;
5917 char origin[ZFS_MAX_DATASET_NAME_LEN];
5921 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5922 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5923 strchr(zc->zc_name, '%'))
5924 return (SET_ERROR(EINVAL));
5926 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5930 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5932 dsl_pool_rele(dp, FTAG);
5936 if (!dsl_dir_is_clone(ds->ds_dir)) {
5937 dsl_dataset_rele(ds, FTAG);
5938 dsl_pool_rele(dp, FTAG);
5939 return (SET_ERROR(EINVAL));
5942 error = dsl_dataset_hold_obj(dp,
5943 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5945 dsl_dataset_rele(ds, FTAG);
5946 dsl_pool_rele(dp, FTAG);
5950 dsl_dataset_name(ods, origin);
5951 dsl_dataset_rele(ods, FTAG);
5952 dsl_dataset_rele(ds, FTAG);
5953 dsl_pool_rele(dp, FTAG);
5956 * We don't need to unmount *all* the origin fs's snapshots, but
5959 cp = strchr(origin, '@');
5962 (void) dmu_objset_find(origin,
5963 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5964 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5968 * Retrieve a single {user|group|project}{used|quota}@... property.
5971 * zc_name name of filesystem
5972 * zc_objset_type zfs_userquota_prop_t
5973 * zc_value domain name (eg. "S-1-234-567-89")
5974 * zc_guid RID/UID/GID
5977 * zc_cookie property value
5980 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5985 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5986 return (SET_ERROR(EINVAL));
5988 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5992 error = zfs_userspace_one(zfsvfs,
5993 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5994 zfsvfs_rele(zfsvfs, FTAG);
6001 * zc_name name of filesystem
6002 * zc_cookie zap cursor
6003 * zc_objset_type zfs_userquota_prop_t
6004 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
6007 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
6008 * zc_cookie zap cursor
6011 zfs_ioc_userspace_many(zfs_cmd_t *zc)
6014 int bufsize = zc->zc_nvlist_dst_size;
6017 return (SET_ERROR(ENOMEM));
6019 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
6023 void *buf = vmem_alloc(bufsize, KM_SLEEP);
6025 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
6026 buf, &zc->zc_nvlist_dst_size);
6029 error = xcopyout(buf,
6030 (void *)(uintptr_t)zc->zc_nvlist_dst,
6031 zc->zc_nvlist_dst_size);
6033 vmem_free(buf, bufsize);
6034 zfsvfs_rele(zfsvfs, FTAG);
6041 * zc_name name of filesystem
6047 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
6052 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
6053 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
6055 * If userused is not enabled, it may be because the
6056 * objset needs to be closed & reopened (to grow the
6057 * objset_phys_t). Suspend/resume the fs will do that.
6059 dsl_dataset_t *ds, *newds;
6061 ds = dmu_objset_ds(zfsvfs->z_os);
6062 error = zfs_suspend_fs(zfsvfs);
6064 dmu_objset_refresh_ownership(ds, &newds,
6066 error = zfs_resume_fs(zfsvfs, newds);
6070 mutex_enter(&zfsvfs->z_os->os_upgrade_lock);
6071 if (zfsvfs->z_os->os_upgrade_id == 0) {
6072 /* clear potential error code and retry */
6073 zfsvfs->z_os->os_upgrade_status = 0;
6074 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6076 dsl_pool_config_enter(
6077 dmu_objset_pool(zfsvfs->z_os), FTAG);
6078 dmu_objset_userspace_upgrade(zfsvfs->z_os);
6079 dsl_pool_config_exit(
6080 dmu_objset_pool(zfsvfs->z_os), FTAG);
6082 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6085 taskq_wait_id(zfsvfs->z_os->os_spa->spa_upgrade_taskq,
6086 zfsvfs->z_os->os_upgrade_id);
6087 error = zfsvfs->z_os->os_upgrade_status;
6089 zfs_vfs_rele(zfsvfs);
6093 /* XXX kind of reading contents without owning */
6094 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6098 mutex_enter(&os->os_upgrade_lock);
6099 if (os->os_upgrade_id == 0) {
6100 /* clear potential error code and retry */
6101 os->os_upgrade_status = 0;
6102 mutex_exit(&os->os_upgrade_lock);
6104 dmu_objset_userspace_upgrade(os);
6106 mutex_exit(&os->os_upgrade_lock);
6109 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6111 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6112 error = os->os_upgrade_status;
6114 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT,
6122 * zc_name name of filesystem
6128 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
6133 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6137 if (dmu_objset_userobjspace_upgradable(os) ||
6138 dmu_objset_projectquota_upgradable(os)) {
6139 mutex_enter(&os->os_upgrade_lock);
6140 if (os->os_upgrade_id == 0) {
6141 /* clear potential error code and retry */
6142 os->os_upgrade_status = 0;
6143 mutex_exit(&os->os_upgrade_lock);
6145 dmu_objset_id_quota_upgrade(os);
6147 mutex_exit(&os->os_upgrade_lock);
6150 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6152 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6153 error = os->os_upgrade_status;
6155 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6158 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
6164 zfs_ioc_share(zfs_cmd_t *zc)
6166 return (SET_ERROR(ENOSYS));
6171 * zc_name name of containing filesystem
6172 * zc_obj object # beyond which we want next in-use object #
6175 * zc_obj next in-use object #
6178 zfs_ioc_next_obj(zfs_cmd_t *zc)
6180 objset_t *os = NULL;
6183 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
6187 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
6189 dmu_objset_rele(os, FTAG);
6195 * zc_name name of filesystem
6196 * zc_value prefix name for snapshot
6197 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
6200 * zc_value short name of new snapshot
6203 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
6209 zfs_file_t *fp = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
6211 return (SET_ERROR(EBADF));
6213 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
6214 (u_longlong_t)ddi_get_lbolt64());
6215 hold_name = kmem_asprintf("%%%s", zc->zc_value);
6217 int error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
6220 (void) strlcpy(zc->zc_value, snap_name,
6221 sizeof (zc->zc_value));
6222 kmem_strfree(snap_name);
6223 kmem_strfree(hold_name);
6224 zfs_onexit_fd_rele(fp);
6230 * zc_name name of "to" snapshot
6231 * zc_value name of "from" snapshot
6232 * zc_cookie file descriptor to write diff data on
6235 * dmu_diff_record_t's to the file descriptor
6238 zfs_ioc_diff(zfs_cmd_t *zc)
6244 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
6245 return (SET_ERROR(EBADF));
6247 off = zfs_file_off(fp);
6248 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
6256 zfs_ioc_smb_acl(zfs_cmd_t *zc)
6258 return (SET_ERROR(ENOTSUP));
6263 * "holds" -> { snapname -> holdname (string), ... }
6264 * (optional) "cleanup_fd" -> fd (int32)
6268 * snapname -> error value (int32)
6272 static const zfs_ioc_key_t zfs_keys_hold[] = {
6273 {"holds", DATA_TYPE_NVLIST, 0},
6274 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6278 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6283 int cleanup_fd = -1;
6286 zfs_file_t *fp = NULL;
6288 holds = fnvlist_lookup_nvlist(args, "holds");
6290 /* make sure the user didn't pass us any invalid (empty) tags */
6291 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6292 pair = nvlist_next_nvpair(holds, pair)) {
6295 error = nvpair_value_string(pair, &htag);
6297 return (SET_ERROR(error));
6299 if (strlen(htag) == 0)
6300 return (SET_ERROR(EINVAL));
6303 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6304 fp = zfs_onexit_fd_hold(cleanup_fd, &minor);
6306 return (SET_ERROR(EBADF));
6309 error = dsl_dataset_user_hold(holds, minor, errlist);
6311 ASSERT3U(minor, !=, 0);
6312 zfs_onexit_fd_rele(fp);
6314 return (SET_ERROR(error));
6318 * innvl is not used.
6321 * holdname -> time added (uint64 seconds since epoch)
6325 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6330 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6333 return (dsl_dataset_get_holds(snapname, outnvl));
6338 * snapname -> { holdname, ... }
6343 * snapname -> error value (int32)
6347 static const zfs_ioc_key_t zfs_keys_release[] = {
6348 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6352 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6355 return (dsl_dataset_user_release(holds, errlist));
6360 * zc_guid flags (ZEVENT_NONBLOCK)
6361 * zc_cleanup_fd zevent file descriptor
6364 * zc_nvlist_dst next nvlist event
6365 * zc_cookie dropped events since last get
6368 zfs_ioc_events_next(zfs_cmd_t *zc)
6371 nvlist_t *event = NULL;
6373 uint64_t dropped = 0;
6376 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6378 return (SET_ERROR(EBADF));
6381 error = zfs_zevent_next(ze, &event,
6382 &zc->zc_nvlist_dst_size, &dropped);
6383 if (event != NULL) {
6384 zc->zc_cookie = dropped;
6385 error = put_nvlist(zc, event);
6389 if (zc->zc_guid & ZEVENT_NONBLOCK)
6392 if ((error == 0) || (error != ENOENT))
6395 error = zfs_zevent_wait(ze);
6400 zfs_zevent_fd_rele(fp);
6407 * zc_cookie cleared events count
6410 zfs_ioc_events_clear(zfs_cmd_t *zc)
6414 zfs_zevent_drain_all(&count);
6415 zc->zc_cookie = count;
6422 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6423 * zc_cleanup zevent file descriptor
6426 zfs_ioc_events_seek(zfs_cmd_t *zc)
6432 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6434 return (SET_ERROR(EBADF));
6436 error = zfs_zevent_seek(ze, zc->zc_guid);
6437 zfs_zevent_fd_rele(fp);
6444 * zc_name name of later filesystem or snapshot
6445 * zc_value full name of old snapshot or bookmark
6448 * zc_cookie space in bytes
6449 * zc_objset_type compressed space in bytes
6450 * zc_perm_action uncompressed space in bytes
6453 zfs_ioc_space_written(zfs_cmd_t *zc)
6459 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6462 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6464 dsl_pool_rele(dp, FTAG);
6467 if (strchr(zc->zc_value, '#') != NULL) {
6468 zfs_bookmark_phys_t bmp;
6469 error = dsl_bookmark_lookup(dp, zc->zc_value,
6472 error = dsl_dataset_space_written_bookmark(&bmp, new,
6474 &zc->zc_objset_type, &zc->zc_perm_action);
6478 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6481 error = dsl_dataset_space_written(old, new,
6483 &zc->zc_objset_type, &zc->zc_perm_action);
6484 dsl_dataset_rele(old, FTAG);
6487 dsl_dataset_rele(new, FTAG);
6488 dsl_pool_rele(dp, FTAG);
6494 * "firstsnap" -> snapshot name
6498 * "used" -> space in bytes
6499 * "compressed" -> compressed space in bytes
6500 * "uncompressed" -> uncompressed space in bytes
6503 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6504 {"firstsnap", DATA_TYPE_STRING, 0},
6508 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6512 dsl_dataset_t *new, *old;
6513 const char *firstsnap;
6514 uint64_t used, comp, uncomp;
6516 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6518 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6522 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6523 if (error == 0 && !new->ds_is_snapshot) {
6524 dsl_dataset_rele(new, FTAG);
6525 error = SET_ERROR(EINVAL);
6528 dsl_pool_rele(dp, FTAG);
6531 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6532 if (error == 0 && !old->ds_is_snapshot) {
6533 dsl_dataset_rele(old, FTAG);
6534 error = SET_ERROR(EINVAL);
6537 dsl_dataset_rele(new, FTAG);
6538 dsl_pool_rele(dp, FTAG);
6542 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6543 dsl_dataset_rele(old, FTAG);
6544 dsl_dataset_rele(new, FTAG);
6545 dsl_pool_rele(dp, FTAG);
6546 fnvlist_add_uint64(outnvl, "used", used);
6547 fnvlist_add_uint64(outnvl, "compressed", comp);
6548 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6554 * "fd" -> file descriptor to write stream to (int32)
6555 * (optional) "fromsnap" -> full snap name to send an incremental from
6556 * (optional) "largeblockok" -> (value ignored)
6557 * indicates that blocks > 128KB are permitted
6558 * (optional) "embedok" -> (value ignored)
6559 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6560 * (optional) "compressok" -> (value ignored)
6561 * presence indicates compressed DRR_WRITE records are permitted
6562 * (optional) "rawok" -> (value ignored)
6563 * presence indicates raw encrypted records should be used.
6564 * (optional) "savedok" -> (value ignored)
6565 * presence indicates we should send a partially received snapshot
6566 * (optional) "resume_object" and "resume_offset" -> (uint64)
6567 * if present, resume send stream from specified object and offset.
6568 * (optional) "redactbook" -> (string)
6569 * if present, use this bookmark's redaction list to generate a redacted
6575 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6576 {"fd", DATA_TYPE_INT32, 0},
6577 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6578 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6579 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6580 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6581 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6582 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6583 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6584 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6585 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6589 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6594 const char *fromname = NULL;
6597 boolean_t largeblockok;
6599 boolean_t compressok;
6602 uint64_t resumeobj = 0;
6603 uint64_t resumeoff = 0;
6604 const char *redactbook = NULL;
6606 fd = fnvlist_lookup_int32(innvl, "fd");
6608 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6610 largeblockok = nvlist_exists(innvl, "largeblockok");
6611 embedok = nvlist_exists(innvl, "embedok");
6612 compressok = nvlist_exists(innvl, "compressok");
6613 rawok = nvlist_exists(innvl, "rawok");
6614 savedok = nvlist_exists(innvl, "savedok");
6616 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6617 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6619 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6621 if ((fp = zfs_file_get(fd)) == NULL)
6622 return (SET_ERROR(EBADF));
6624 off = zfs_file_off(fp);
6626 dmu_send_outparams_t out = {0};
6627 out.dso_outfunc = dump_bytes;
6629 out.dso_dryrun = B_FALSE;
6630 error = dmu_send(snapname, fromname, embedok, largeblockok,
6631 compressok, rawok, savedok, resumeobj, resumeoff,
6632 redactbook, fd, &off, &out);
6639 send_space_sum(objset_t *os, void *buf, int len, void *arg)
6641 (void) os, (void) buf;
6642 uint64_t *size = arg;
6649 * Determine approximately how large a zfs send stream will be -- the number
6650 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6653 * (optional) "from" -> full snap or bookmark name to send an incremental
6655 * (optional) "largeblockok" -> (value ignored)
6656 * indicates that blocks > 128KB are permitted
6657 * (optional) "embedok" -> (value ignored)
6658 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6659 * (optional) "compressok" -> (value ignored)
6660 * presence indicates compressed DRR_WRITE records are permitted
6661 * (optional) "rawok" -> (value ignored)
6662 * presence indicates raw encrypted records should be used.
6663 * (optional) "resume_object" and "resume_offset" -> (uint64)
6664 * if present, resume send stream from specified object and offset.
6665 * (optional) "fd" -> file descriptor to use as a cookie for progress
6670 * "space" -> bytes of space (uint64)
6673 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6674 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6675 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6676 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6677 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6678 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6679 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6680 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6681 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6682 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6683 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6684 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
6688 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6691 dsl_dataset_t *tosnap;
6692 dsl_dataset_t *fromsnap = NULL;
6694 const char *fromname = NULL;
6695 const char *redactlist_book = NULL;
6696 boolean_t largeblockok;
6698 boolean_t compressok;
6702 boolean_t full_estimate = B_FALSE;
6703 uint64_t resumeobj = 0;
6704 uint64_t resumeoff = 0;
6705 uint64_t resume_bytes = 0;
6707 zfs_bookmark_phys_t zbm = {0};
6709 error = dsl_pool_hold(snapname, FTAG, &dp);
6713 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6715 dsl_pool_rele(dp, FTAG);
6718 (void) nvlist_lookup_int32(innvl, "fd", &fd);
6720 largeblockok = nvlist_exists(innvl, "largeblockok");
6721 embedok = nvlist_exists(innvl, "embedok");
6722 compressok = nvlist_exists(innvl, "compressok");
6723 rawok = nvlist_exists(innvl, "rawok");
6724 savedok = nvlist_exists(innvl, "savedok");
6725 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
6726 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
6727 &redactlist_book) == 0);
6729 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6730 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6731 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
6734 full_estimate = B_TRUE;
6736 if (strchr(fromname, '#')) {
6737 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
6740 * dsl_bookmark_lookup() will fail with EXDEV if
6741 * the from-bookmark and tosnap are at the same txg.
6742 * However, it's valid to do a send (and therefore,
6743 * a send estimate) from and to the same time point,
6744 * if the bookmark is redacted (the incremental send
6745 * can change what's redacted on the target). In
6746 * this case, dsl_bookmark_lookup() fills in zbm
6747 * but returns EXDEV. Ignore this error.
6749 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
6751 dsl_dataset_phys(tosnap)->ds_guid)
6755 dsl_dataset_rele(tosnap, FTAG);
6756 dsl_pool_rele(dp, FTAG);
6759 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
6760 ZBM_FLAG_HAS_FBN)) {
6761 full_estimate = B_TRUE;
6763 } else if (strchr(fromname, '@')) {
6764 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6766 dsl_dataset_rele(tosnap, FTAG);
6767 dsl_pool_rele(dp, FTAG);
6771 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
6772 full_estimate = B_TRUE;
6773 dsl_dataset_rele(fromsnap, FTAG);
6777 * from is not properly formatted as a snapshot or
6780 dsl_dataset_rele(tosnap, FTAG);
6781 dsl_pool_rele(dp, FTAG);
6782 return (SET_ERROR(EINVAL));
6786 if (full_estimate) {
6787 dmu_send_outparams_t out = {0};
6789 out.dso_outfunc = send_space_sum;
6790 out.dso_arg = &space;
6791 out.dso_dryrun = B_TRUE;
6793 * We have to release these holds so dmu_send can take them. It
6794 * will do all the error checking we need.
6796 dsl_dataset_rele(tosnap, FTAG);
6797 dsl_pool_rele(dp, FTAG);
6798 error = dmu_send(snapname, fromname, embedok, largeblockok,
6799 compressok, rawok, savedok, resumeobj, resumeoff,
6800 redactlist_book, fd, &off, &out);
6802 error = dmu_send_estimate_fast(tosnap, fromsnap,
6803 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
6804 compressok || rawok, savedok, &space);
6805 space -= resume_bytes;
6806 if (fromsnap != NULL)
6807 dsl_dataset_rele(fromsnap, FTAG);
6808 dsl_dataset_rele(tosnap, FTAG);
6809 dsl_pool_rele(dp, FTAG);
6812 fnvlist_add_uint64(outnvl, "space", space);
6818 * Sync the currently open TXG to disk for the specified pool.
6819 * This is somewhat similar to 'zfs_sync()'.
6820 * For cases that do not result in error this ioctl will wait for
6821 * the currently open TXG to commit before returning back to the caller.
6824 * "force" -> when true, force uberblock update even if there is no dirty data.
6825 * In addition this will cause the vdev configuration to be written
6826 * out including updating the zpool cache file. (boolean_t)
6831 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6832 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6836 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6840 boolean_t rc, force = B_FALSE;
6843 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6847 err = nvlist_lookup_boolean_value(innvl, "force", &rc);
6853 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6854 vdev_config_dirty(spa->spa_root_vdev);
6855 spa_config_exit(spa, SCL_CONFIG, FTAG);
6857 txg_wait_synced(spa_get_dsl(spa), 0);
6859 spa_close(spa, FTAG);
6865 * Load a user's wrapping key into the kernel.
6867 * "hidden_args" -> { "wkeydata" -> value }
6868 * raw uint8_t array of encryption wrapping key data (32 bytes)
6869 * (optional) "noop" -> (value ignored)
6870 * presence indicated key should only be verified, not loaded
6873 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6874 {"hidden_args", DATA_TYPE_NVLIST, 0},
6875 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6879 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6883 dsl_crypto_params_t *dcp = NULL;
6884 nvlist_t *hidden_args;
6885 boolean_t noop = nvlist_exists(innvl, "noop");
6887 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6888 ret = SET_ERROR(EINVAL);
6892 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6894 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6899 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6903 dsl_crypto_params_free(dcp, noop);
6908 dsl_crypto_params_free(dcp, B_TRUE);
6913 * Unload a user's wrapping key from the kernel.
6914 * Both innvl and outnvl are unused.
6916 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6921 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6923 (void) innvl, (void) outnvl;
6926 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6927 ret = (SET_ERROR(EINVAL));
6931 ret = spa_keystore_unload_wkey(dsname);
6940 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6941 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6942 * here to change how the key is derived in userspace.
6945 * "hidden_args" (optional) -> { "wkeydata" -> value }
6946 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6947 * "props" (optional) -> { prop -> value }
6952 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6953 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6954 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6955 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6959 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6963 uint64_t cmd = DCP_CMD_NONE;
6964 dsl_crypto_params_t *dcp = NULL;
6965 nvlist_t *args = NULL, *hidden_args = NULL;
6967 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6968 ret = (SET_ERROR(EINVAL));
6972 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6973 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6974 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6976 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6980 ret = spa_keystore_change_key(dsname, dcp);
6984 dsl_crypto_params_free(dcp, B_FALSE);
6989 dsl_crypto_params_free(dcp, B_TRUE);
6993 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6996 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6997 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6998 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
7000 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
7002 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
7003 ASSERT3U(ioc, <, ZFS_IOC_LAST);
7004 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
7005 ASSERT3P(vec->zvec_func, ==, NULL);
7007 vec->zvec_legacy_func = func;
7008 vec->zvec_secpolicy = secpolicy;
7009 vec->zvec_namecheck = namecheck;
7010 vec->zvec_allow_log = log_history;
7011 vec->zvec_pool_check = pool_check;
7015 * See the block comment at the beginning of this file for details on
7016 * each argument to this function.
7019 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
7020 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
7021 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
7022 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
7024 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
7026 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
7027 ASSERT3U(ioc, <, ZFS_IOC_LAST);
7028 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
7029 ASSERT3P(vec->zvec_func, ==, NULL);
7031 /* if we are logging, the name must be valid */
7032 ASSERT(!allow_log || namecheck != NO_NAME);
7034 vec->zvec_name = name;
7035 vec->zvec_func = func;
7036 vec->zvec_secpolicy = secpolicy;
7037 vec->zvec_namecheck = namecheck;
7038 vec->zvec_pool_check = pool_check;
7039 vec->zvec_smush_outnvlist = smush_outnvlist;
7040 vec->zvec_allow_log = allow_log;
7041 vec->zvec_nvl_keys = nvl_keys;
7042 vec->zvec_nvl_key_count = num_keys;
7046 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7047 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
7048 zfs_ioc_poolcheck_t pool_check)
7050 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7051 POOL_NAME, log_history, pool_check);
7055 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7056 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
7058 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7059 DATASET_NAME, B_FALSE, pool_check);
7063 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7065 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
7066 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7070 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7071 zfs_secpolicy_func_t *secpolicy)
7073 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7074 NO_NAME, B_FALSE, POOL_CHECK_NONE);
7078 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
7079 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
7081 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7082 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
7086 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7088 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
7089 zfs_secpolicy_read);
7093 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7094 zfs_secpolicy_func_t *secpolicy)
7096 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7097 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7101 zfs_ioctl_init(void)
7103 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
7104 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
7105 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7106 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
7108 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
7109 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
7110 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7111 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
7113 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
7114 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
7115 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7116 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
7118 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
7119 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
7120 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7121 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
7123 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
7124 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
7125 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7126 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
7128 zfs_ioctl_register("create", ZFS_IOC_CREATE,
7129 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
7130 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7131 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
7133 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
7134 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
7135 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7136 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
7138 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
7139 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
7140 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7141 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
7143 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
7144 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
7145 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7146 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
7148 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
7149 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
7150 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7151 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
7152 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
7153 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
7154 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7155 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
7157 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
7158 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
7159 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7160 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
7162 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
7163 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
7164 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7165 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
7167 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
7168 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
7169 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7170 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
7172 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
7173 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
7174 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7175 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
7177 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
7178 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
7179 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
7180 ARRAY_SIZE(zfs_keys_get_bookmark_props));
7182 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
7183 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
7185 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7186 zfs_keys_destroy_bookmarks,
7187 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
7189 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
7190 zfs_ioc_recv_new, zfs_secpolicy_recv, DATASET_NAME,
7191 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7192 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
7193 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
7194 zfs_ioc_load_key, zfs_secpolicy_load_key,
7195 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7196 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
7197 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
7198 zfs_ioc_unload_key, zfs_secpolicy_load_key,
7199 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7200 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
7201 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
7202 zfs_ioc_change_key, zfs_secpolicy_change_key,
7203 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
7204 B_TRUE, B_TRUE, zfs_keys_change_key,
7205 ARRAY_SIZE(zfs_keys_change_key));
7207 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
7208 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
7209 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7210 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
7211 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
7212 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
7213 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
7215 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
7216 zfs_ioc_channel_program, zfs_secpolicy_config,
7217 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
7218 B_TRUE, zfs_keys_channel_program,
7219 ARRAY_SIZE(zfs_keys_channel_program));
7221 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
7222 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
7223 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7224 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
7226 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
7227 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
7228 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7229 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
7231 zfs_ioctl_register("zpool_discard_checkpoint",
7232 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
7233 zfs_secpolicy_config, POOL_NAME,
7234 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7235 zfs_keys_pool_discard_checkpoint,
7236 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
7238 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
7239 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
7240 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7241 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
7243 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
7244 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
7245 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7246 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
7248 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
7249 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
7250 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7251 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
7253 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS,
7254 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME,
7255 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7256 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait));
7258 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
7259 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
7260 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7261 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
7263 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
7264 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
7265 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
7266 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
7268 zfs_ioctl_register("zpool_vdev_get_props", ZFS_IOC_VDEV_GET_PROPS,
7269 zfs_ioc_vdev_get_props, zfs_secpolicy_read, POOL_NAME,
7270 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_vdev_get_props,
7271 ARRAY_SIZE(zfs_keys_vdev_get_props));
7273 zfs_ioctl_register("zpool_vdev_set_props", ZFS_IOC_VDEV_SET_PROPS,
7274 zfs_ioc_vdev_set_props, zfs_secpolicy_config, POOL_NAME,
7275 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7276 zfs_keys_vdev_set_props, ARRAY_SIZE(zfs_keys_vdev_set_props));
7278 zfs_ioctl_register("scrub", ZFS_IOC_POOL_SCRUB,
7279 zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME,
7280 POOL_CHECK_NONE, B_TRUE, B_TRUE,
7281 zfs_keys_pool_scrub, ARRAY_SIZE(zfs_keys_pool_scrub));
7283 /* IOCTLS that use the legacy function signature */
7285 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7286 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7288 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7289 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7290 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7292 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7293 zfs_ioc_pool_upgrade);
7294 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7296 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7297 zfs_ioc_vdev_remove);
7298 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7299 zfs_ioc_vdev_set_state);
7300 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7301 zfs_ioc_vdev_attach);
7302 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7303 zfs_ioc_vdev_detach);
7304 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7305 zfs_ioc_vdev_setpath);
7306 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7307 zfs_ioc_vdev_setfru);
7308 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7309 zfs_ioc_pool_set_props);
7310 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7311 zfs_ioc_vdev_split);
7312 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7313 zfs_ioc_pool_reguid);
7315 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7316 zfs_ioc_pool_configs, zfs_secpolicy_none);
7317 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7318 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7319 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7320 zfs_ioc_inject_fault, zfs_secpolicy_inject);
7321 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7322 zfs_ioc_clear_fault, zfs_secpolicy_inject);
7323 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
7324 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
7327 * pool destroy, and export don't log the history as part of
7328 * zfsdev_ioctl, but rather zfs_ioc_pool_export
7329 * does the logging of those commands.
7331 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
7332 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7333 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
7334 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7336 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7337 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7338 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
7339 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7341 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7342 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7343 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7344 zfs_ioc_dsobj_to_dsname,
7345 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7346 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7347 zfs_ioc_pool_get_history,
7348 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7350 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7351 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7353 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
7354 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
7356 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7357 zfs_ioc_space_written);
7358 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7359 zfs_ioc_objset_recvd_props);
7360 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7362 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7364 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7365 zfs_ioc_objset_stats);
7366 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7367 zfs_ioc_objset_zplprops);
7368 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7369 zfs_ioc_dataset_list_next);
7370 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7371 zfs_ioc_snapshot_list_next);
7372 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7373 zfs_ioc_send_progress);
7375 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7376 zfs_ioc_diff, zfs_secpolicy_diff);
7377 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7378 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7379 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7380 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7381 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7382 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7383 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7384 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7385 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7386 zfs_ioc_send, zfs_secpolicy_send);
7388 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7389 zfs_secpolicy_none);
7390 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7391 zfs_secpolicy_destroy);
7392 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7393 zfs_secpolicy_rename);
7394 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7395 zfs_secpolicy_recv);
7396 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7397 zfs_secpolicy_promote);
7398 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7399 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7400 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7401 zfs_secpolicy_set_fsacl);
7403 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7404 zfs_secpolicy_share, POOL_CHECK_NONE);
7405 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7406 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7407 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7408 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7409 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7410 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7411 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7412 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7414 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7415 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7416 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7417 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7418 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7419 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7421 zfs_ioctl_init_os();
7425 * Verify that for non-legacy ioctls the input nvlist
7426 * pairs match against the expected input.
7428 * Possible errors are:
7429 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7430 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7431 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7434 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7436 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7437 boolean_t required_keys_found = B_FALSE;
7440 * examine each input pair
7442 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7443 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7444 const char *name = nvpair_name(pair);
7445 data_type_t type = nvpair_type(pair);
7446 boolean_t identified = B_FALSE;
7449 * check pair against the documented names and type
7451 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7452 /* if not a wild card name, check for an exact match */
7453 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7454 strcmp(nvl_keys[k].zkey_name, name) != 0)
7457 identified = B_TRUE;
7459 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7460 nvl_keys[k].zkey_type != type) {
7461 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7464 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7467 required_keys_found = B_TRUE;
7471 /* allow an 'optional' key, everything else is invalid */
7473 (strcmp(name, "optional") != 0 ||
7474 type != DATA_TYPE_NVLIST)) {
7475 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7479 /* verify that all required keys were found */
7480 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7481 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7484 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7485 /* at least one non-optional key is expected here */
7486 if (!required_keys_found)
7487 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7491 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7492 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7499 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7500 zfs_ioc_poolcheck_t check)
7505 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7506 type == ENTITY_NAME);
7508 if (check & POOL_CHECK_NONE)
7511 error = spa_open(name, &spa, FTAG);
7513 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7514 error = SET_ERROR(EAGAIN);
7515 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7516 error = SET_ERROR(EROFS);
7517 spa_close(spa, FTAG);
7523 zfsdev_getminor(zfs_file_t *fp, minor_t *minorp)
7525 zfsdev_state_t *zs, *fpd;
7527 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7529 fpd = zfs_file_private(fp);
7531 return (SET_ERROR(EBADF));
7533 mutex_enter(&zfsdev_state_lock);
7535 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7537 if (zs->zs_minor == -1)
7541 *minorp = fpd->zs_minor;
7542 mutex_exit(&zfsdev_state_lock);
7547 mutex_exit(&zfsdev_state_lock);
7549 return (SET_ERROR(EBADF));
7553 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
7557 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7558 if (zs->zs_minor == minor) {
7562 return (zs->zs_onexit);
7564 return (zs->zs_zevent);
7575 * Find a free minor number. The zfsdev_state_list is expected to
7576 * be short since it is only a list of currently open file handles.
7579 zfsdev_minor_alloc(void)
7581 static minor_t last_minor = 0;
7584 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7586 for (m = last_minor + 1; m != last_minor; m++) {
7587 if (m > ZFSDEV_MAX_MINOR)
7589 if (zfsdev_get_state(m, ZST_ALL) == NULL) {
7599 zfsdev_state_init(void *priv)
7601 zfsdev_state_t *zs, *zsprev = NULL;
7603 boolean_t newzs = B_FALSE;
7605 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7607 minor = zfsdev_minor_alloc();
7609 return (SET_ERROR(ENXIO));
7611 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7612 if (zs->zs_minor == -1)
7618 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7622 zfsdev_private_set_state(priv, zs);
7624 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7625 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7628 * In order to provide for lock-free concurrent read access
7629 * to the minor list in zfsdev_get_state(), new entries
7630 * must be completely written before linking them into the
7631 * list whereas existing entries are already linked; the last
7632 * operation must be updating zs_minor (from -1 to the new
7636 zs->zs_minor = minor;
7638 zsprev->zs_next = zs;
7641 zs->zs_minor = minor;
7648 zfsdev_state_destroy(void *priv)
7650 zfsdev_state_t *zs = zfsdev_private_get_state(priv);
7653 ASSERT3S(zs->zs_minor, >, 0);
7656 * The last reference to this zfsdev file descriptor is being dropped.
7657 * We don't have to worry about lookup grabbing this state object, and
7658 * zfsdev_state_init() will not try to reuse this object until it is
7659 * invalidated by setting zs_minor to -1. Invalidation must be done
7660 * last, with a memory barrier to ensure ordering. This lets us avoid
7661 * taking the global zfsdev state lock around destruction.
7663 zfs_onexit_destroy(zs->zs_onexit);
7664 zfs_zevent_destroy(zs->zs_zevent);
7665 zs->zs_onexit = NULL;
7666 zs->zs_zevent = NULL;
7672 zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
7675 const zfs_ioc_vec_t *vec;
7676 char *saved_poolname = NULL;
7677 uint64_t max_nvlist_src_size;
7678 size_t saved_poolname_len = 0;
7679 nvlist_t *innvl = NULL;
7680 fstrans_cookie_t cookie;
7681 hrtime_t start_time = gethrtime();
7685 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7686 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7688 vec = &zfs_ioc_vec[vecnum];
7691 * The registered ioctl list may be sparse, verify that either
7692 * a normal or legacy handler are registered.
7694 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7695 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7697 zc->zc_iflags = flag & FKIOCTL;
7698 max_nvlist_src_size = zfs_max_nvlist_src_size_os();
7699 if (zc->zc_nvlist_src_size > max_nvlist_src_size) {
7701 * Make sure the user doesn't pass in an insane value for
7702 * zc_nvlist_src_size. We have to check, since we will end
7703 * up allocating that much memory inside of get_nvlist(). This
7704 * prevents a nefarious user from allocating tons of kernel
7707 * Also, we return EINVAL instead of ENOMEM here. The reason
7708 * being that returning ENOMEM from an ioctl() has a special
7709 * connotation; that the user's size value is too small and
7710 * needs to be expanded to hold the nvlist. See
7711 * zcmd_expand_dst_nvlist() for details.
7713 error = SET_ERROR(EINVAL); /* User's size too big */
7715 } else if (zc->zc_nvlist_src_size != 0) {
7716 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7717 zc->zc_iflags, &innvl);
7723 * Ensure that all pool/dataset names are valid before we pass down to
7726 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7727 switch (vec->zvec_namecheck) {
7729 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7730 error = SET_ERROR(EINVAL);
7732 error = pool_status_check(zc->zc_name,
7733 vec->zvec_namecheck, vec->zvec_pool_check);
7737 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7738 error = SET_ERROR(EINVAL);
7740 error = pool_status_check(zc->zc_name,
7741 vec->zvec_namecheck, vec->zvec_pool_check);
7745 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7746 error = SET_ERROR(EINVAL);
7748 error = pool_status_check(zc->zc_name,
7749 vec->zvec_namecheck, vec->zvec_pool_check);
7757 * Ensure that all input pairs are valid before we pass them down
7758 * to the lower layers.
7760 * The vectored functions can use fnvlist_lookup_{type} for any
7761 * required pairs since zfs_check_input_nvpairs() confirmed that
7762 * they exist and are of the correct type.
7764 if (error == 0 && vec->zvec_func != NULL) {
7765 error = zfs_check_input_nvpairs(innvl, vec);
7771 cookie = spl_fstrans_mark();
7772 error = vec->zvec_secpolicy(zc, innvl, CRED());
7773 spl_fstrans_unmark(cookie);
7779 /* legacy ioctls can modify zc_name */
7781 * Can't use kmem_strdup() as we might truncate the string and
7782 * kmem_strfree() would then free with incorrect size.
7784 saved_poolname_len = strlen(zc->zc_name) + 1;
7785 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP);
7787 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len);
7788 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7790 if (vec->zvec_func != NULL) {
7794 nvlist_t *lognv = NULL;
7796 ASSERT(vec->zvec_legacy_func == NULL);
7799 * Add the innvl to the lognv before calling the func,
7800 * in case the func changes the innvl.
7802 if (vec->zvec_allow_log) {
7803 lognv = fnvlist_alloc();
7804 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7806 if (!nvlist_empty(innvl)) {
7807 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7812 outnvl = fnvlist_alloc();
7813 cookie = spl_fstrans_mark();
7814 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7815 spl_fstrans_unmark(cookie);
7818 * Some commands can partially execute, modify state, and still
7819 * return an error. In these cases, attempt to record what
7823 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7824 vec->zvec_allow_log &&
7825 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7826 if (!nvlist_empty(outnvl)) {
7827 size_t out_size = fnvlist_size(outnvl);
7828 if (out_size > zfs_history_output_max) {
7829 fnvlist_add_int64(lognv,
7830 ZPOOL_HIST_OUTPUT_SIZE, out_size);
7832 fnvlist_add_nvlist(lognv,
7833 ZPOOL_HIST_OUTPUT_NVL, outnvl);
7837 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7840 fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS,
7841 gethrtime() - start_time);
7842 (void) spa_history_log_nvl(spa, lognv);
7843 spa_close(spa, FTAG);
7845 fnvlist_free(lognv);
7847 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7849 if (vec->zvec_smush_outnvlist) {
7850 smusherror = nvlist_smush(outnvl,
7851 zc->zc_nvlist_dst_size);
7853 if (smusherror == 0)
7854 puterror = put_nvlist(zc, outnvl);
7860 nvlist_free(outnvl);
7862 cookie = spl_fstrans_mark();
7863 error = vec->zvec_legacy_func(zc);
7864 spl_fstrans_unmark(cookie);
7869 if (error == 0 && vec->zvec_allow_log) {
7870 char *s = tsd_get(zfs_allow_log_key);
7873 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname));
7875 if (saved_poolname != NULL)
7876 kmem_free(saved_poolname, saved_poolname_len);
7886 if ((error = zvol_init()) != 0)
7889 spa_init(SPA_MODE_READ | SPA_MODE_WRITE);
7894 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7895 zfsdev_state_listhead.zs_minor = -1;
7897 if ((error = zfsdev_attach()) != 0)
7900 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7901 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7915 zfsdev_state_t *zs, *zsnext = NULL;
7919 mutex_destroy(&zfsdev_state_lock);
7921 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zsnext) {
7922 zsnext = zs->zs_next;
7924 zfs_onexit_destroy(zs->zs_onexit);
7926 zfs_zevent_destroy(zs->zs_zevent);
7927 if (zs != &zfsdev_state_listhead)
7928 kmem_free(zs, sizeof (zfsdev_state_t));
7931 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */
7936 tsd_destroy(&rrw_tsd_key);
7937 tsd_destroy(&zfs_allow_log_key);
7940 ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, U64, ZMOD_RW,
7941 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls");
7943 ZFS_MODULE_PARAM(zfs, zfs_, history_output_max, U64, ZMOD_RW,
7944 "Maximum size in bytes of ZFS ioctl output that will be logged");