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 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright 2017 RackTop Systems.
38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
39 * Copyright (c) 2019 Datto Inc.
40 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
41 * Copyright (c) 2019, 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_list;
228 * Limit maximum nvlist size. We don't want users passing in insane values
229 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
230 * Defaults to 0=auto which is handled by platform code.
232 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_fsyncer_key;
242 uint_t zfs_allow_log_key;
244 /* DATA_TYPE_ANY is used when zkey_type can vary. */
245 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
247 typedef struct zfs_ioc_vec {
248 zfs_ioc_legacy_func_t *zvec_legacy_func;
249 zfs_ioc_func_t *zvec_func;
250 zfs_secpolicy_func_t *zvec_secpolicy;
251 zfs_ioc_namecheck_t zvec_namecheck;
252 boolean_t zvec_allow_log;
253 zfs_ioc_poolcheck_t zvec_pool_check;
254 boolean_t zvec_smush_outnvlist;
255 const char *zvec_name;
256 const zfs_ioc_key_t *zvec_nvl_keys;
257 size_t zvec_nvl_key_count;
260 /* This array is indexed by zfs_userquota_prop_t */
261 static const char *userquota_perms[] = {
262 ZFS_DELEG_PERM_USERUSED,
263 ZFS_DELEG_PERM_USERQUOTA,
264 ZFS_DELEG_PERM_GROUPUSED,
265 ZFS_DELEG_PERM_GROUPQUOTA,
266 ZFS_DELEG_PERM_USEROBJUSED,
267 ZFS_DELEG_PERM_USEROBJQUOTA,
268 ZFS_DELEG_PERM_GROUPOBJUSED,
269 ZFS_DELEG_PERM_GROUPOBJQUOTA,
270 ZFS_DELEG_PERM_PROJECTUSED,
271 ZFS_DELEG_PERM_PROJECTQUOTA,
272 ZFS_DELEG_PERM_PROJECTOBJUSED,
273 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
276 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
277 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
278 static int zfs_check_settable(const char *name, nvpair_t *property,
280 static int zfs_check_clearable(const char *dataset, nvlist_t *props,
282 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
284 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
285 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
288 history_str_free(char *buf)
290 kmem_free(buf, HIS_MAX_RECORD_LEN);
294 history_str_get(zfs_cmd_t *zc)
298 if (zc->zc_history == 0)
301 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
302 if (copyinstr((void *)(uintptr_t)zc->zc_history,
303 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
304 history_str_free(buf);
308 buf[HIS_MAX_RECORD_LEN -1] = '\0';
314 * Return non-zero if the spa version is less than requested version.
317 zfs_earlier_version(const char *name, int version)
321 if (spa_open(name, &spa, FTAG) == 0) {
322 if (spa_version(spa) < version) {
323 spa_close(spa, FTAG);
326 spa_close(spa, FTAG);
332 * Return TRUE if the ZPL version is less than requested version.
335 zpl_earlier_version(const char *name, int version)
338 boolean_t rc = B_TRUE;
340 if (dmu_objset_hold(name, FTAG, &os) == 0) {
343 if (dmu_objset_type(os) != DMU_OST_ZFS) {
344 dmu_objset_rele(os, FTAG);
347 /* XXX reading from non-owned objset */
348 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
349 rc = zplversion < version;
350 dmu_objset_rele(os, FTAG);
356 zfs_log_history(zfs_cmd_t *zc)
361 if ((buf = history_str_get(zc)) == NULL)
364 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
365 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
366 (void) spa_history_log(spa, buf);
367 spa_close(spa, FTAG);
369 history_str_free(buf);
373 * Policy for top-level read operations (list pools). Requires no privileges,
374 * and can be used in the local zone, as there is no associated dataset.
377 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
379 (void) zc, (void) innvl, (void) cr;
384 * Policy for dataset read operations (list children, get statistics). Requires
385 * no privileges, but must be visible in the local zone.
388 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
390 (void) innvl, (void) cr;
391 if (INGLOBALZONE(curproc) ||
392 zone_dataset_visible(zc->zc_name, NULL))
395 return (SET_ERROR(ENOENT));
399 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
404 * The dataset must be visible by this zone -- check this first
405 * so they don't see EPERM on something they shouldn't know about.
407 if (!INGLOBALZONE(curproc) &&
408 !zone_dataset_visible(dataset, &writable))
409 return (SET_ERROR(ENOENT));
411 if (INGLOBALZONE(curproc)) {
413 * If the fs is zoned, only root can access it from the
416 if (secpolicy_zfs(cr) && zoned)
417 return (SET_ERROR(EPERM));
420 * If we are in a local zone, the 'zoned' property must be set.
423 return (SET_ERROR(EPERM));
425 /* must be writable by this zone */
427 return (SET_ERROR(EPERM));
433 zfs_dozonecheck(const char *dataset, cred_t *cr)
437 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED),
439 return (SET_ERROR(ENOENT));
441 return (zfs_dozonecheck_impl(dataset, zoned, cr));
445 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
449 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned))
450 return (SET_ERROR(ENOENT));
452 return (zfs_dozonecheck_impl(dataset, zoned, cr));
456 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
457 const char *perm, cred_t *cr)
461 error = zfs_dozonecheck_ds(name, ds, cr);
463 error = secpolicy_zfs(cr);
465 error = dsl_deleg_access_impl(ds, perm, cr);
471 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
478 * First do a quick check for root in the global zone, which
479 * is allowed to do all write_perms. This ensures that zfs_ioc_*
480 * will get to handle nonexistent datasets.
482 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
485 error = dsl_pool_hold(name, FTAG, &dp);
489 error = dsl_dataset_hold(dp, name, FTAG, &ds);
491 dsl_pool_rele(dp, FTAG);
495 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
497 dsl_dataset_rele(ds, FTAG);
498 dsl_pool_rele(dp, FTAG);
503 * Policy for setting the security label property.
505 * Returns 0 for success, non-zero for access and other errors.
508 zfs_set_slabel_policy(const char *name, const char *strval, cred_t *cr)
511 char ds_hexsl[MAXNAMELEN];
512 bslabel_t ds_sl, new_sl;
513 boolean_t new_default = FALSE;
515 int needed_priv = -1;
518 /* First get the existing dataset label. */
519 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
520 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
522 return (SET_ERROR(EPERM));
524 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
527 /* The label must be translatable */
528 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
529 return (SET_ERROR(EINVAL));
532 * In a non-global zone, disallow attempts to set a label that
533 * doesn't match that of the zone; otherwise no other checks
536 if (!INGLOBALZONE(curproc)) {
537 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
538 return (SET_ERROR(EPERM));
543 * For global-zone datasets (i.e., those whose zoned property is
544 * "off", verify that the specified new label is valid for the
547 if (dsl_prop_get_integer(name,
548 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
549 return (SET_ERROR(EPERM));
551 if (zfs_check_global_label(name, strval) != 0)
552 return (SET_ERROR(EPERM));
556 * If the existing dataset label is nondefault, check if the
557 * dataset is mounted (label cannot be changed while mounted).
558 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
559 * mounted (or isn't a dataset, doesn't exist, ...).
561 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
563 static const char *setsl_tag = "setsl_tag";
566 * Try to own the dataset; abort if there is any error,
567 * (e.g., already mounted, in use, or other error).
569 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
572 return (SET_ERROR(EPERM));
574 dmu_objset_disown(os, B_TRUE, setsl_tag);
577 needed_priv = PRIV_FILE_DOWNGRADE_SL;
581 if (hexstr_to_label(strval, &new_sl) != 0)
582 return (SET_ERROR(EPERM));
584 if (blstrictdom(&ds_sl, &new_sl))
585 needed_priv = PRIV_FILE_DOWNGRADE_SL;
586 else if (blstrictdom(&new_sl, &ds_sl))
587 needed_priv = PRIV_FILE_UPGRADE_SL;
589 /* dataset currently has a default label */
591 needed_priv = PRIV_FILE_UPGRADE_SL;
595 if (needed_priv != -1)
596 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
599 return (SET_ERROR(ENOTSUP));
600 #endif /* HAVE_MLSLABEL */
604 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
610 * Check permissions for special properties.
617 * Disallow setting of 'zoned' from within a local zone.
619 if (!INGLOBALZONE(curproc))
620 return (SET_ERROR(EPERM));
624 case ZFS_PROP_FILESYSTEM_LIMIT:
625 case ZFS_PROP_SNAPSHOT_LIMIT:
626 if (!INGLOBALZONE(curproc)) {
628 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
630 * Unprivileged users are allowed to modify the
631 * limit on things *under* (ie. contained by)
632 * the thing they own.
634 if (dsl_prop_get_integer(dsname,
635 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint))
636 return (SET_ERROR(EPERM));
637 if (!zoned || strlen(dsname) <= strlen(setpoint))
638 return (SET_ERROR(EPERM));
642 case ZFS_PROP_MLSLABEL:
643 if (!is_system_labeled())
644 return (SET_ERROR(EPERM));
646 if (nvpair_value_string(propval, &strval) == 0) {
649 err = zfs_set_slabel_policy(dsname, strval, CRED());
656 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
660 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
663 * permission to set permissions will be evaluated later in
664 * dsl_deleg_can_allow()
667 return (zfs_dozonecheck(zc->zc_name, cr));
671 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
674 return (zfs_secpolicy_write_perms(zc->zc_name,
675 ZFS_DELEG_PERM_ROLLBACK, cr));
679 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
688 * Generate the current snapshot name from the given objsetid, then
689 * use that name for the secpolicy/zone checks.
691 cp = strchr(zc->zc_name, '@');
693 return (SET_ERROR(EINVAL));
694 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
698 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
700 dsl_pool_rele(dp, FTAG);
704 dsl_dataset_name(ds, zc->zc_name);
706 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
707 ZFS_DELEG_PERM_SEND, cr);
708 dsl_dataset_rele(ds, FTAG);
709 dsl_pool_rele(dp, FTAG);
715 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
718 return (zfs_secpolicy_write_perms(zc->zc_name,
719 ZFS_DELEG_PERM_SEND, cr));
723 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
725 (void) zc, (void) innvl, (void) cr;
726 return (SET_ERROR(ENOTSUP));
730 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
732 (void) zc, (void) innvl, (void) cr;
733 return (SET_ERROR(ENOTSUP));
737 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
742 * Remove the @bla or /bla from the end of the name to get the parent.
744 (void) strlcpy(parent, datasetname, parentsize);
745 cp = strrchr(parent, '@');
749 cp = strrchr(parent, '/');
751 return (SET_ERROR(ENOENT));
759 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
763 if ((error = zfs_secpolicy_write_perms(name,
764 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
767 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
771 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
774 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
778 * Destroying snapshots with delegated permissions requires
779 * descendant mount and destroy permissions.
782 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
786 nvpair_t *pair, *nextpair;
789 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
791 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
793 nextpair = nvlist_next_nvpair(snaps, pair);
794 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
795 if (error == ENOENT) {
797 * Ignore any snapshots that don't exist (we consider
798 * them "already destroyed"). Remove the name from the
799 * nvl here in case the snapshot is created between
800 * now and when we try to destroy it (in which case
801 * we don't want to destroy it since we haven't
802 * checked for permission).
804 fnvlist_remove_nvpair(snaps, pair);
815 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
817 char parentname[ZFS_MAX_DATASET_NAME_LEN];
820 if ((error = zfs_secpolicy_write_perms(from,
821 ZFS_DELEG_PERM_RENAME, cr)) != 0)
824 if ((error = zfs_secpolicy_write_perms(from,
825 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
828 if ((error = zfs_get_parent(to, parentname,
829 sizeof (parentname))) != 0)
832 if ((error = zfs_secpolicy_write_perms(parentname,
833 ZFS_DELEG_PERM_CREATE, cr)) != 0)
836 if ((error = zfs_secpolicy_write_perms(parentname,
837 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
844 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
847 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
851 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
855 dsl_dataset_t *clone;
858 error = zfs_secpolicy_write_perms(zc->zc_name,
859 ZFS_DELEG_PERM_PROMOTE, cr);
863 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
867 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
870 char parentname[ZFS_MAX_DATASET_NAME_LEN];
871 dsl_dataset_t *origin = NULL;
875 error = dsl_dataset_hold_obj(dd->dd_pool,
876 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
878 dsl_dataset_rele(clone, FTAG);
879 dsl_pool_rele(dp, FTAG);
883 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
884 ZFS_DELEG_PERM_MOUNT, cr);
886 dsl_dataset_name(origin, parentname);
888 error = zfs_secpolicy_write_perms_ds(parentname, origin,
889 ZFS_DELEG_PERM_PROMOTE, cr);
891 dsl_dataset_rele(clone, FTAG);
892 dsl_dataset_rele(origin, FTAG);
894 dsl_pool_rele(dp, FTAG);
899 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
904 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
905 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
908 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
909 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
912 return (zfs_secpolicy_write_perms(zc->zc_name,
913 ZFS_DELEG_PERM_CREATE, cr));
917 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
919 return (zfs_secpolicy_write_perms(name,
920 ZFS_DELEG_PERM_SNAPSHOT, cr));
924 * Check for permission to create each snapshot in the nvlist.
927 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
934 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
936 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
937 pair = nvlist_next_nvpair(snaps, pair)) {
938 char *name = nvpair_name(pair);
939 char *atp = strchr(name, '@');
942 error = SET_ERROR(EINVAL);
946 error = zfs_secpolicy_snapshot_perms(name, cr);
955 * Check for permission to create each bookmark in the nvlist.
958 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
963 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
964 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
965 char *name = nvpair_name(pair);
966 char *hashp = strchr(name, '#');
969 error = SET_ERROR(EINVAL);
973 error = zfs_secpolicy_write_perms(name,
974 ZFS_DELEG_PERM_BOOKMARK, cr);
983 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
986 nvpair_t *pair, *nextpair;
989 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
991 char *name = nvpair_name(pair);
992 char *hashp = strchr(name, '#');
993 nextpair = nvlist_next_nvpair(innvl, pair);
996 error = SET_ERROR(EINVAL);
1001 error = zfs_secpolicy_write_perms(name,
1002 ZFS_DELEG_PERM_DESTROY, cr);
1004 if (error == ENOENT) {
1006 * Ignore any filesystems that don't exist (we consider
1007 * their bookmarks "already destroyed"). Remove
1008 * the name from the nvl here in case the filesystem
1009 * is created between now and when we try to destroy
1010 * the bookmark (in which case we don't want to
1011 * destroy it since we haven't checked for permission).
1013 fnvlist_remove_nvpair(innvl, pair);
1024 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1026 (void) zc, (void) innvl, (void) cr;
1028 * Even root must have a proper TSD so that we know what pool
1031 if (tsd_get(zfs_allow_log_key) == NULL)
1032 return (SET_ERROR(EPERM));
1037 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1039 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1043 if ((error = zfs_get_parent(zc->zc_name, parentname,
1044 sizeof (parentname))) != 0)
1047 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1048 (error = zfs_secpolicy_write_perms(origin,
1049 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1052 if ((error = zfs_secpolicy_write_perms(parentname,
1053 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1056 return (zfs_secpolicy_write_perms(parentname,
1057 ZFS_DELEG_PERM_MOUNT, cr));
1061 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1062 * SYS_CONFIG privilege, which is not available in a local zone.
1065 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1067 (void) zc, (void) innvl;
1069 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1070 return (SET_ERROR(EPERM));
1076 * Policy for object to name lookups.
1079 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1084 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1087 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1092 * Policy for fault injection. Requires all privileges.
1095 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1097 (void) zc, (void) innvl;
1098 return (secpolicy_zinject(cr));
1102 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1105 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1107 if (prop == ZPROP_USERPROP) {
1108 if (!zfs_prop_user(zc->zc_value))
1109 return (SET_ERROR(EINVAL));
1110 return (zfs_secpolicy_write_perms(zc->zc_name,
1111 ZFS_DELEG_PERM_USERPROP, cr));
1113 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1119 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1121 int err = zfs_secpolicy_read(zc, innvl, cr);
1125 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1126 return (SET_ERROR(EINVAL));
1128 if (zc->zc_value[0] == 0) {
1130 * They are asking about a posix uid/gid. If it's
1131 * themself, allow it.
1133 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1134 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1135 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1136 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1137 if (zc->zc_guid == crgetuid(cr))
1139 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1140 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1141 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1142 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1143 if (groupmember(zc->zc_guid, cr))
1146 /* else is for project quota/used */
1149 return (zfs_secpolicy_write_perms(zc->zc_name,
1150 userquota_perms[zc->zc_objset_type], cr));
1154 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1156 int err = zfs_secpolicy_read(zc, innvl, cr);
1160 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1161 return (SET_ERROR(EINVAL));
1163 return (zfs_secpolicy_write_perms(zc->zc_name,
1164 userquota_perms[zc->zc_objset_type], cr));
1168 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1171 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1176 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1183 holds = fnvlist_lookup_nvlist(innvl, "holds");
1185 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1186 pair = nvlist_next_nvpair(holds, pair)) {
1187 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1188 error = dmu_fsname(nvpair_name(pair), fsname);
1191 error = zfs_secpolicy_write_perms(fsname,
1192 ZFS_DELEG_PERM_HOLD, cr);
1200 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1206 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1207 pair = nvlist_next_nvpair(innvl, pair)) {
1208 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1209 error = dmu_fsname(nvpair_name(pair), fsname);
1212 error = zfs_secpolicy_write_perms(fsname,
1213 ZFS_DELEG_PERM_RELEASE, cr);
1221 * Policy for allowing temporary snapshots to be taken or released
1224 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1227 * A temporary snapshot is the same as a snapshot,
1228 * hold, destroy and release all rolled into one.
1229 * Delegated diff alone is sufficient that we allow this.
1233 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1234 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1237 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1239 if (innvl != NULL) {
1241 error = zfs_secpolicy_hold(zc, innvl, cr);
1243 error = zfs_secpolicy_release(zc, innvl, cr);
1245 error = zfs_secpolicy_destroy(zc, innvl, cr);
1251 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1253 return (zfs_secpolicy_write_perms(zc->zc_name,
1254 ZFS_DELEG_PERM_LOAD_KEY, cr));
1258 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1260 return (zfs_secpolicy_write_perms(zc->zc_name,
1261 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1265 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1268 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1272 nvlist_t *list = NULL;
1275 * Read in and unpack the user-supplied nvlist.
1278 return (SET_ERROR(EINVAL));
1280 packed = vmem_alloc(size, KM_SLEEP);
1282 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1284 vmem_free(packed, size);
1285 return (SET_ERROR(EFAULT));
1288 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1289 vmem_free(packed, size);
1293 vmem_free(packed, size);
1300 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1301 * Entries will be removed from the end of the nvlist, and one int32 entry
1302 * named "N_MORE_ERRORS" will be added indicating how many entries were
1306 nvlist_smush(nvlist_t *errors, size_t max)
1310 size = fnvlist_size(errors);
1313 nvpair_t *more_errors;
1317 return (SET_ERROR(ENOMEM));
1319 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1320 more_errors = nvlist_prev_nvpair(errors, NULL);
1323 nvpair_t *pair = nvlist_prev_nvpair(errors,
1325 fnvlist_remove_nvpair(errors, pair);
1327 size = fnvlist_size(errors);
1328 } while (size > max);
1330 fnvlist_remove_nvpair(errors, more_errors);
1331 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1332 ASSERT3U(fnvlist_size(errors), <=, max);
1339 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1341 char *packed = NULL;
1345 size = fnvlist_size(nvl);
1347 if (size > zc->zc_nvlist_dst_size) {
1348 error = SET_ERROR(ENOMEM);
1350 packed = fnvlist_pack(nvl, &size);
1351 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1352 size, zc->zc_iflags) != 0)
1353 error = SET_ERROR(EFAULT);
1354 fnvlist_pack_free(packed, size);
1357 zc->zc_nvlist_dst_size = size;
1358 zc->zc_nvlist_dst_filled = B_TRUE;
1363 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1366 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1367 return (SET_ERROR(EINVAL));
1370 mutex_enter(&os->os_user_ptr_lock);
1371 *zfvp = dmu_objset_get_user(os);
1372 /* bump s_active only when non-zero to prevent umount race */
1373 error = zfs_vfs_ref(zfvp);
1374 mutex_exit(&os->os_user_ptr_lock);
1379 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1384 error = dmu_objset_hold(dsname, FTAG, &os);
1388 error = getzfsvfs_impl(os, zfvp);
1389 dmu_objset_rele(os, FTAG);
1394 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1395 * case its z_sb will be NULL, and it will be opened as the owner.
1396 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1397 * which prevents all inode ops from running.
1400 zfsvfs_hold(const char *name, const void *tag, zfsvfs_t **zfvp,
1405 if (getzfsvfs(name, zfvp) != 0)
1406 error = zfsvfs_create(name, B_FALSE, zfvp);
1409 ZFS_TEARDOWN_ENTER_WRITE(*zfvp, tag);
1411 ZFS_TEARDOWN_ENTER_READ(*zfvp, tag);
1412 if ((*zfvp)->z_unmounted) {
1414 * XXX we could probably try again, since the unmounting
1415 * thread should be just about to disassociate the
1416 * objset from the zfsvfs.
1418 ZFS_TEARDOWN_EXIT(*zfvp, tag);
1419 return (SET_ERROR(EBUSY));
1426 zfsvfs_rele(zfsvfs_t *zfsvfs, const void *tag)
1428 ZFS_TEARDOWN_EXIT(zfsvfs, tag);
1430 if (zfs_vfs_held(zfsvfs)) {
1431 zfs_vfs_rele(zfsvfs);
1433 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1434 zfsvfs_free(zfsvfs);
1439 zfs_ioc_pool_create(zfs_cmd_t *zc)
1442 nvlist_t *config, *props = NULL;
1443 nvlist_t *rootprops = NULL;
1444 nvlist_t *zplprops = NULL;
1445 dsl_crypto_params_t *dcp = NULL;
1446 const char *spa_name = zc->zc_name;
1447 boolean_t unload_wkey = B_TRUE;
1449 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1450 zc->zc_iflags, &config)))
1453 if (zc->zc_nvlist_src_size != 0 && (error =
1454 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1455 zc->zc_iflags, &props))) {
1456 nvlist_free(config);
1461 nvlist_t *nvl = NULL;
1462 nvlist_t *hidden_args = NULL;
1463 uint64_t version = SPA_VERSION;
1466 (void) nvlist_lookup_uint64(props,
1467 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1468 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1469 error = SET_ERROR(EINVAL);
1470 goto pool_props_bad;
1472 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1474 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1476 goto pool_props_bad;
1477 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1480 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1482 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1483 rootprops, hidden_args, &dcp);
1485 goto pool_props_bad;
1486 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1488 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1489 error = zfs_fill_zplprops_root(version, rootprops,
1492 goto pool_props_bad;
1494 if (nvlist_lookup_string(props,
1495 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1499 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1502 * Set the remaining root properties
1504 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1505 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1506 (void) spa_destroy(spa_name);
1507 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1511 nvlist_free(rootprops);
1512 nvlist_free(zplprops);
1513 nvlist_free(config);
1515 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1521 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1524 zfs_log_history(zc);
1525 error = spa_destroy(zc->zc_name);
1531 zfs_ioc_pool_import(zfs_cmd_t *zc)
1533 nvlist_t *config, *props = NULL;
1537 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1538 zc->zc_iflags, &config)) != 0)
1541 if (zc->zc_nvlist_src_size != 0 && (error =
1542 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1543 zc->zc_iflags, &props))) {
1544 nvlist_free(config);
1548 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1549 guid != zc->zc_guid)
1550 error = SET_ERROR(EINVAL);
1552 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1554 if (zc->zc_nvlist_dst != 0) {
1557 if ((err = put_nvlist(zc, config)) != 0)
1561 nvlist_free(config);
1568 zfs_ioc_pool_export(zfs_cmd_t *zc)
1571 boolean_t force = (boolean_t)zc->zc_cookie;
1572 boolean_t hardforce = (boolean_t)zc->zc_guid;
1574 zfs_log_history(zc);
1575 error = spa_export(zc->zc_name, NULL, force, hardforce);
1581 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1586 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1587 return (SET_ERROR(EEXIST));
1589 error = put_nvlist(zc, configs);
1591 nvlist_free(configs);
1598 * zc_name name of the pool
1601 * zc_cookie real errno
1602 * zc_nvlist_dst config nvlist
1603 * zc_nvlist_dst_size size of config nvlist
1606 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1612 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1613 sizeof (zc->zc_value));
1615 if (config != NULL) {
1616 ret = put_nvlist(zc, config);
1617 nvlist_free(config);
1620 * The config may be present even if 'error' is non-zero.
1621 * In this case we return success, and preserve the real errno
1624 zc->zc_cookie = error;
1633 * Try to import the given pool, returning pool stats as appropriate so that
1634 * user land knows which devices are available and overall pool health.
1637 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1639 nvlist_t *tryconfig, *config = NULL;
1642 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1643 zc->zc_iflags, &tryconfig)) != 0)
1646 config = spa_tryimport(tryconfig);
1648 nvlist_free(tryconfig);
1651 return (SET_ERROR(EINVAL));
1653 error = put_nvlist(zc, config);
1654 nvlist_free(config);
1661 * zc_name name of the pool
1662 * zc_cookie scan func (pool_scan_func_t)
1663 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1666 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1671 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1672 return (SET_ERROR(EINVAL));
1674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1677 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1678 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1679 else if (zc->zc_cookie == POOL_SCAN_NONE)
1680 error = spa_scan_stop(spa);
1682 error = spa_scan(spa, zc->zc_cookie);
1684 spa_close(spa, FTAG);
1690 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1695 error = spa_open(zc->zc_name, &spa, FTAG);
1698 spa_close(spa, FTAG);
1704 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1709 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1712 if (zc->zc_cookie < spa_version(spa) ||
1713 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1714 spa_close(spa, FTAG);
1715 return (SET_ERROR(EINVAL));
1718 spa_upgrade(spa, zc->zc_cookie);
1719 spa_close(spa, FTAG);
1725 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1732 if ((size = zc->zc_history_len) == 0)
1733 return (SET_ERROR(EINVAL));
1735 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1738 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1739 spa_close(spa, FTAG);
1740 return (SET_ERROR(ENOTSUP));
1743 hist_buf = vmem_alloc(size, KM_SLEEP);
1744 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1745 &zc->zc_history_len, hist_buf)) == 0) {
1746 error = ddi_copyout(hist_buf,
1747 (void *)(uintptr_t)zc->zc_history,
1748 zc->zc_history_len, zc->zc_iflags);
1751 spa_close(spa, FTAG);
1752 vmem_free(hist_buf, size);
1757 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1762 error = spa_open(zc->zc_name, &spa, FTAG);
1764 error = spa_change_guid(spa);
1765 spa_close(spa, FTAG);
1771 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1773 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1778 * zc_name name of filesystem
1779 * zc_obj object to find
1782 * zc_value name of object
1785 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1790 /* XXX reading from objset not owned */
1791 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1794 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1795 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1796 return (SET_ERROR(EINVAL));
1798 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1799 sizeof (zc->zc_value));
1800 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1807 * zc_name name of filesystem
1808 * zc_obj object to find
1811 * zc_stat stats on object
1812 * zc_value path to object
1815 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1820 /* XXX reading from objset not owned */
1821 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1824 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1825 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1826 return (SET_ERROR(EINVAL));
1828 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1829 sizeof (zc->zc_value));
1830 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1836 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1842 error = spa_open(zc->zc_name, &spa, FTAG);
1846 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1847 zc->zc_iflags, &config);
1849 error = spa_vdev_add(spa, config);
1850 nvlist_free(config);
1852 spa_close(spa, FTAG);
1858 * zc_name name of the pool
1859 * zc_guid guid of vdev to remove
1860 * zc_cookie cancel removal
1863 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1868 error = spa_open(zc->zc_name, &spa, FTAG);
1871 if (zc->zc_cookie != 0) {
1872 error = spa_vdev_remove_cancel(spa);
1874 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1876 spa_close(spa, FTAG);
1881 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1885 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1887 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1889 switch (zc->zc_cookie) {
1890 case VDEV_STATE_ONLINE:
1891 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1894 case VDEV_STATE_OFFLINE:
1895 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1898 case VDEV_STATE_FAULTED:
1899 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1900 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1901 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1902 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1904 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1907 case VDEV_STATE_DEGRADED:
1908 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1909 zc->zc_obj != VDEV_AUX_EXTERNAL)
1910 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1912 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1915 case VDEV_STATE_REMOVED:
1916 error = vdev_remove_wanted(spa, zc->zc_guid);
1920 error = SET_ERROR(EINVAL);
1922 zc->zc_cookie = newstate;
1923 spa_close(spa, FTAG);
1928 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1932 int replacing = zc->zc_cookie;
1933 int rebuild = zc->zc_simple;
1936 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1939 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1940 zc->zc_iflags, &config)) == 0) {
1941 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing,
1943 nvlist_free(config);
1946 spa_close(spa, FTAG);
1951 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1956 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1959 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
1961 spa_close(spa, FTAG);
1966 zfs_ioc_vdev_split(zfs_cmd_t *zc)
1969 nvlist_t *config, *props = NULL;
1971 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1973 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1976 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1977 zc->zc_iflags, &config))) {
1978 spa_close(spa, FTAG);
1982 if (zc->zc_nvlist_src_size != 0 && (error =
1983 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1984 zc->zc_iflags, &props))) {
1985 spa_close(spa, FTAG);
1986 nvlist_free(config);
1990 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1992 spa_close(spa, FTAG);
1994 nvlist_free(config);
2001 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2004 const char *path = zc->zc_value;
2005 uint64_t guid = zc->zc_guid;
2008 error = spa_open(zc->zc_name, &spa, FTAG);
2012 error = spa_vdev_setpath(spa, guid, path);
2013 spa_close(spa, FTAG);
2018 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2021 const char *fru = zc->zc_value;
2022 uint64_t guid = zc->zc_guid;
2025 error = spa_open(zc->zc_name, &spa, FTAG);
2029 error = spa_vdev_setfru(spa, guid, fru);
2030 spa_close(spa, FTAG);
2035 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2040 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2042 if (!zc->zc_simple && zc->zc_nvlist_dst != 0 &&
2043 (error = dsl_prop_get_all(os, &nv)) == 0) {
2044 dmu_objset_stats(os, nv);
2046 * NB: zvol_get_stats() will read the objset contents,
2047 * which we aren't supposed to do with a
2048 * DS_MODE_USER hold, because it could be
2049 * inconsistent. So this is a bit of a workaround...
2050 * XXX reading without owning
2052 if (!zc->zc_objset_stats.dds_inconsistent &&
2053 dmu_objset_type(os) == DMU_OST_ZVOL) {
2054 error = zvol_get_stats(os, nv);
2062 error = put_nvlist(zc, nv);
2071 * zc_name name of filesystem
2072 * zc_nvlist_dst_size size of buffer for property nvlist
2075 * zc_objset_stats stats
2076 * zc_nvlist_dst property nvlist
2077 * zc_nvlist_dst_size size of property nvlist
2080 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2085 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2087 error = zfs_ioc_objset_stats_impl(zc, os);
2088 dmu_objset_rele(os, FTAG);
2096 * zc_name name of filesystem
2097 * zc_nvlist_dst_size size of buffer for property nvlist
2100 * zc_nvlist_dst received property nvlist
2101 * zc_nvlist_dst_size size of received property nvlist
2103 * Gets received properties (distinct from local properties on or after
2104 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2105 * local property values.
2108 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2114 * Without this check, we would return local property values if the
2115 * caller has not already received properties on or after
2116 * SPA_VERSION_RECVD_PROPS.
2118 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2119 return (SET_ERROR(ENOTSUP));
2121 if (zc->zc_nvlist_dst != 0 &&
2122 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2123 error = put_nvlist(zc, nv);
2131 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2137 * zfs_get_zplprop() will either find a value or give us
2138 * the default value (if there is one).
2140 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2142 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2148 * zc_name name of filesystem
2149 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2152 * zc_nvlist_dst zpl property nvlist
2153 * zc_nvlist_dst_size size of zpl property nvlist
2156 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2161 /* XXX reading without owning */
2162 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2165 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2168 * NB: nvl_add_zplprop() will read the objset contents,
2169 * which we aren't supposed to do with a DS_MODE_USER
2170 * hold, because it could be inconsistent.
2172 if (zc->zc_nvlist_dst != 0 &&
2173 !zc->zc_objset_stats.dds_inconsistent &&
2174 dmu_objset_type(os) == DMU_OST_ZFS) {
2177 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2178 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2179 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2180 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2181 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2182 err = put_nvlist(zc, nv);
2185 err = SET_ERROR(ENOENT);
2187 dmu_objset_rele(os, FTAG);
2193 * zc_name name of filesystem
2194 * zc_cookie zap cursor
2195 * zc_nvlist_dst_size size of buffer for property nvlist
2198 * zc_name name of next filesystem
2199 * zc_cookie zap cursor
2200 * zc_objset_stats stats
2201 * zc_nvlist_dst property nvlist
2202 * zc_nvlist_dst_size size of property nvlist
2205 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2210 size_t orig_len = strlen(zc->zc_name);
2213 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2214 if (error == ENOENT)
2215 error = SET_ERROR(ESRCH);
2219 p = strrchr(zc->zc_name, '/');
2220 if (p == NULL || p[1] != '\0')
2221 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2222 p = zc->zc_name + strlen(zc->zc_name);
2225 error = dmu_dir_list_next(os,
2226 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2227 NULL, &zc->zc_cookie);
2228 if (error == ENOENT)
2229 error = SET_ERROR(ESRCH);
2230 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2231 dmu_objset_rele(os, FTAG);
2234 * If it's an internal dataset (ie. with a '$' in its name),
2235 * don't try to get stats for it, otherwise we'll return ENOENT.
2237 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2238 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2239 if (error == ENOENT) {
2240 /* We lost a race with destroy, get the next one. */
2241 zc->zc_name[orig_len] = '\0';
2250 * zc_name name of filesystem
2251 * zc_cookie zap cursor
2252 * zc_nvlist_src iteration range nvlist
2253 * zc_nvlist_src_size size of iteration range nvlist
2256 * zc_name name of next snapshot
2257 * zc_objset_stats stats
2258 * zc_nvlist_dst property nvlist
2259 * zc_nvlist_dst_size size of property nvlist
2262 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2265 objset_t *os, *ossnap;
2267 uint64_t min_txg = 0, max_txg = 0;
2269 if (zc->zc_nvlist_src_size != 0) {
2270 nvlist_t *props = NULL;
2271 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2272 zc->zc_iflags, &props);
2275 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2277 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2282 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2284 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
2288 * A dataset name of maximum length cannot have any snapshots,
2289 * so exit immediately.
2291 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2292 ZFS_MAX_DATASET_NAME_LEN) {
2293 dmu_objset_rele(os, FTAG);
2294 return (SET_ERROR(ESRCH));
2297 while (error == 0) {
2298 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2299 error = SET_ERROR(EINTR);
2303 error = dmu_snapshot_list_next(os,
2304 sizeof (zc->zc_name) - strlen(zc->zc_name),
2305 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2306 &zc->zc_cookie, NULL);
2307 if (error == ENOENT) {
2308 error = SET_ERROR(ESRCH);
2310 } else if (error != 0) {
2314 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2319 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2320 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2321 dsl_dataset_rele(ds, FTAG);
2322 /* undo snapshot name append */
2323 *(strchr(zc->zc_name, '@') + 1) = '\0';
2328 if (zc->zc_simple) {
2329 dsl_dataset_fast_stat(ds, &zc->zc_objset_stats);
2330 dsl_dataset_rele(ds, FTAG);
2334 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2335 dsl_dataset_rele(ds, FTAG);
2338 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2339 dsl_dataset_rele(ds, FTAG);
2342 dsl_dataset_rele(ds, FTAG);
2346 dmu_objset_rele(os, FTAG);
2347 /* if we failed, undo the @ that we tacked on to zc_name */
2349 *strchr(zc->zc_name, '@') = '\0';
2354 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2356 const char *propname = nvpair_name(pair);
2358 unsigned int vallen;
2359 const char *dash, *domain;
2360 zfs_userquota_prop_t type;
2366 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2368 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2369 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2371 return (SET_ERROR(EINVAL));
2375 * A correctly constructed propname is encoded as
2376 * userquota@<rid>-<domain>.
2378 if ((dash = strchr(propname, '-')) == NULL ||
2379 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2381 return (SET_ERROR(EINVAL));
2388 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2390 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2391 zfsvfs_rele(zfsvfs, FTAG);
2398 * If the named property is one that has a special function to set its value,
2399 * return 0 on success and a positive error code on failure; otherwise if it is
2400 * not one of the special properties handled by this function, return -1.
2402 * XXX: It would be better for callers of the property interface if we handled
2403 * these special cases in dsl_prop.c (in the dsl layer).
2406 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2409 const char *propname = nvpair_name(pair);
2410 zfs_prop_t prop = zfs_name_to_prop(propname);
2411 uint64_t intval = 0;
2412 const char *strval = NULL;
2415 if (prop == ZPROP_USERPROP) {
2416 if (zfs_prop_userquota(propname))
2417 return (zfs_prop_set_userquota(dsname, pair));
2421 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2423 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2424 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2428 /* all special properties are numeric except for keylocation */
2429 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2430 strval = fnvpair_value_string(pair);
2432 intval = fnvpair_value_uint64(pair);
2436 case ZFS_PROP_QUOTA:
2437 err = dsl_dir_set_quota(dsname, source, intval);
2439 case ZFS_PROP_REFQUOTA:
2440 err = dsl_dataset_set_refquota(dsname, source, intval);
2442 case ZFS_PROP_FILESYSTEM_LIMIT:
2443 case ZFS_PROP_SNAPSHOT_LIMIT:
2444 if (intval == UINT64_MAX) {
2445 /* clearing the limit, just do it */
2448 err = dsl_dir_activate_fs_ss_limit(dsname);
2451 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2452 * default path to set the value in the nvlist.
2457 case ZFS_PROP_KEYLOCATION:
2458 err = dsl_crypto_can_set_keylocation(dsname, strval);
2461 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2462 * default path to set the value in the nvlist.
2467 case ZFS_PROP_RESERVATION:
2468 err = dsl_dir_set_reservation(dsname, source, intval);
2470 case ZFS_PROP_REFRESERVATION:
2471 err = dsl_dataset_set_refreservation(dsname, source, intval);
2473 case ZFS_PROP_COMPRESSION:
2474 err = dsl_dataset_set_compression(dsname, source, intval);
2476 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2477 * default path to set the value in the nvlist.
2482 case ZFS_PROP_VOLSIZE:
2483 err = zvol_set_volsize(dsname, intval);
2485 case ZFS_PROP_SNAPDEV:
2486 err = zvol_set_snapdev(dsname, source, intval);
2488 case ZFS_PROP_VOLMODE:
2489 err = zvol_set_volmode(dsname, source, intval);
2491 case ZFS_PROP_VERSION:
2495 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2498 err = zfs_set_version(zfsvfs, intval);
2499 zfsvfs_rele(zfsvfs, FTAG);
2501 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2504 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2505 (void) strlcpy(zc->zc_name, dsname,
2506 sizeof (zc->zc_name));
2507 (void) zfs_ioc_userspace_upgrade(zc);
2508 (void) zfs_ioc_id_quota_upgrade(zc);
2509 kmem_free(zc, sizeof (zfs_cmd_t));
2521 zfs_is_namespace_prop(zfs_prop_t prop)
2525 case ZFS_PROP_ATIME:
2526 case ZFS_PROP_RELATIME:
2527 case ZFS_PROP_DEVICES:
2529 case ZFS_PROP_SETUID:
2530 case ZFS_PROP_READONLY:
2531 case ZFS_PROP_XATTR:
2532 case ZFS_PROP_NBMAND:
2541 * This function is best effort. If it fails to set any of the given properties,
2542 * it continues to set as many as it can and returns the last error
2543 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2544 * with the list of names of all the properties that failed along with the
2545 * corresponding error numbers.
2547 * If every property is set successfully, zero is returned and errlist is not
2551 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2560 boolean_t should_update_mount_cache = B_FALSE;
2562 nvlist_t *genericnvl = fnvlist_alloc();
2563 nvlist_t *retrynvl = fnvlist_alloc();
2566 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2567 const char *propname = nvpair_name(pair);
2568 zfs_prop_t prop = zfs_name_to_prop(propname);
2571 /* decode the property value */
2573 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2575 attrs = fnvpair_value_nvlist(pair);
2576 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2578 err = SET_ERROR(EINVAL);
2581 /* Validate value type */
2582 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2583 /* inherited properties are expected to be booleans */
2584 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2585 err = SET_ERROR(EINVAL);
2586 } else if (err == 0 && prop == ZPROP_USERPROP) {
2587 if (zfs_prop_user(propname)) {
2588 if (nvpair_type(propval) != DATA_TYPE_STRING)
2589 err = SET_ERROR(EINVAL);
2590 } else if (zfs_prop_userquota(propname)) {
2591 if (nvpair_type(propval) !=
2592 DATA_TYPE_UINT64_ARRAY)
2593 err = SET_ERROR(EINVAL);
2595 err = SET_ERROR(EINVAL);
2597 } else if (err == 0) {
2598 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2599 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2600 err = SET_ERROR(EINVAL);
2601 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2604 intval = fnvpair_value_uint64(propval);
2606 switch (zfs_prop_get_type(prop)) {
2607 case PROP_TYPE_NUMBER:
2609 case PROP_TYPE_STRING:
2610 err = SET_ERROR(EINVAL);
2612 case PROP_TYPE_INDEX:
2613 if (zfs_prop_index_to_string(prop,
2614 intval, &unused) != 0)
2616 SET_ERROR(ZFS_ERR_BADPROP);
2620 "unknown property type");
2623 err = SET_ERROR(EINVAL);
2627 /* Validate permissions */
2629 err = zfs_check_settable(dsname, pair, CRED());
2632 if (source == ZPROP_SRC_INHERITED)
2633 err = -1; /* does not need special handling */
2635 err = zfs_prop_set_special(dsname, source,
2639 * For better performance we build up a list of
2640 * properties to set in a single transaction.
2642 err = nvlist_add_nvpair(genericnvl, pair);
2643 } else if (err != 0 && nvl != retrynvl) {
2645 * This may be a spurious error caused by
2646 * receiving quota and reservation out of order.
2647 * Try again in a second pass.
2649 err = nvlist_add_nvpair(retrynvl, pair);
2654 if (errlist != NULL)
2655 fnvlist_add_int32(errlist, propname, err);
2659 if (zfs_is_namespace_prop(prop))
2660 should_update_mount_cache = B_TRUE;
2663 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2668 if (nvlist_empty(genericnvl))
2672 * Try to set them all in one batch.
2674 err = dsl_props_set(dsname, source, genericnvl);
2679 * If batching fails, we still want to set as many properties as we
2680 * can, so try setting them individually.
2683 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2684 const char *propname = nvpair_name(pair);
2688 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2690 attrs = fnvpair_value_nvlist(pair);
2691 propval = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
2694 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2695 strval = fnvpair_value_string(propval);
2696 err = dsl_prop_set_string(dsname, propname,
2698 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2699 err = dsl_prop_inherit(dsname, propname, source);
2701 intval = fnvpair_value_uint64(propval);
2702 err = dsl_prop_set_int(dsname, propname, source,
2707 if (errlist != NULL) {
2708 fnvlist_add_int32(errlist, propname, err);
2715 if (should_update_mount_cache)
2716 zfs_ioctl_update_mount_cache(dsname);
2718 nvlist_free(genericnvl);
2719 nvlist_free(retrynvl);
2725 * Check that all the properties are valid user properties.
2728 zfs_check_userprops(nvlist_t *nvl)
2730 nvpair_t *pair = NULL;
2732 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2733 const char *propname = nvpair_name(pair);
2735 if (!zfs_prop_user(propname) ||
2736 nvpair_type(pair) != DATA_TYPE_STRING)
2737 return (SET_ERROR(EINVAL));
2739 if (strlen(propname) >= ZAP_MAXNAMELEN)
2740 return (SET_ERROR(ENAMETOOLONG));
2742 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2743 return (SET_ERROR(E2BIG));
2749 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2753 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2756 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2757 if (nvlist_exists(skipped, nvpair_name(pair)))
2760 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2765 clear_received_props(const char *dsname, nvlist_t *props,
2769 nvlist_t *cleared_props = NULL;
2770 props_skip(props, skipped, &cleared_props);
2771 if (!nvlist_empty(cleared_props)) {
2773 * Acts on local properties until the dataset has received
2774 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2776 zprop_source_t flags = (ZPROP_SRC_NONE |
2777 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2778 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2780 nvlist_free(cleared_props);
2786 * zc_name name of filesystem
2787 * zc_value name of property to set
2788 * zc_nvlist_src{_size} nvlist of properties to apply
2789 * zc_cookie received properties flag
2792 * zc_nvlist_dst{_size} error for each unapplied received property
2795 zfs_ioc_set_prop(zfs_cmd_t *zc)
2798 boolean_t received = zc->zc_cookie;
2799 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2804 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2805 zc->zc_iflags, &nvl)) != 0)
2809 nvlist_t *origprops;
2811 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2812 (void) clear_received_props(zc->zc_name,
2814 nvlist_free(origprops);
2817 error = dsl_prop_set_hasrecvd(zc->zc_name);
2820 errors = fnvlist_alloc();
2822 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2824 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2825 (void) put_nvlist(zc, errors);
2828 nvlist_free(errors);
2835 * zc_name name of filesystem
2836 * zc_value name of property to inherit
2837 * zc_cookie revert to received value if TRUE
2842 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2844 const char *propname = zc->zc_value;
2845 zfs_prop_t prop = zfs_name_to_prop(propname);
2846 boolean_t received = zc->zc_cookie;
2847 zprop_source_t source = (received
2848 ? ZPROP_SRC_NONE /* revert to received value, if any */
2849 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2857 * Only check this in the non-received case. We want to allow
2858 * 'inherit -S' to revert non-inheritable properties like quota
2859 * and reservation to the received or default values even though
2860 * they are not considered inheritable.
2862 if (prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop))
2863 return (SET_ERROR(EINVAL));
2866 if (prop == ZPROP_USERPROP) {
2867 if (!zfs_prop_user(propname))
2868 return (SET_ERROR(EINVAL));
2870 type = PROP_TYPE_STRING;
2871 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2872 return (SET_ERROR(EINVAL));
2874 type = zfs_prop_get_type(prop);
2878 * zfs_prop_set_special() expects properties in the form of an
2879 * nvpair with type info.
2881 dummy = fnvlist_alloc();
2884 case PROP_TYPE_STRING:
2885 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2887 case PROP_TYPE_NUMBER:
2888 case PROP_TYPE_INDEX:
2889 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2892 err = SET_ERROR(EINVAL);
2896 pair = nvlist_next_nvpair(dummy, NULL);
2898 err = SET_ERROR(EINVAL);
2900 err = zfs_prop_set_special(zc->zc_name, source, pair);
2901 if (err == -1) /* property is not "special", needs handling */
2902 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2912 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2919 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2920 zc->zc_iflags, &props)))
2924 * If the only property is the configfile, then just do a spa_lookup()
2925 * to handle the faulted case.
2927 pair = nvlist_next_nvpair(props, NULL);
2928 if (pair != NULL && strcmp(nvpair_name(pair),
2929 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2930 nvlist_next_nvpair(props, pair) == NULL) {
2931 mutex_enter(&spa_namespace_lock);
2932 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2933 spa_configfile_set(spa, props, B_FALSE);
2934 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
2936 mutex_exit(&spa_namespace_lock);
2943 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2948 error = spa_prop_set(spa, props);
2951 spa_close(spa, FTAG);
2957 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2961 nvlist_t *nvp = NULL;
2963 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2965 * If the pool is faulted, there may be properties we can still
2966 * get (such as altroot and cachefile), so attempt to get them
2969 mutex_enter(&spa_namespace_lock);
2970 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2971 error = spa_prop_get(spa, &nvp);
2972 mutex_exit(&spa_namespace_lock);
2974 error = spa_prop_get(spa, &nvp);
2975 spa_close(spa, FTAG);
2978 if (error == 0 && zc->zc_nvlist_dst != 0)
2979 error = put_nvlist(zc, nvp);
2981 error = SET_ERROR(EFAULT);
2989 * "vdevprops_set_vdev" -> guid
2990 * "vdevprops_set_props" -> { prop -> value }
2993 * outnvl: propname -> error code (int32)
2995 static const zfs_ioc_key_t zfs_keys_vdev_set_props[] = {
2996 {ZPOOL_VDEV_PROPS_SET_VDEV, DATA_TYPE_UINT64, 0},
2997 {ZPOOL_VDEV_PROPS_SET_PROPS, DATA_TYPE_NVLIST, 0}
3001 zfs_ioc_vdev_set_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3008 /* Early validation */
3009 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
3011 return (SET_ERROR(EINVAL));
3014 return (SET_ERROR(EINVAL));
3016 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3019 ASSERT(spa_writeable(spa));
3021 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3022 spa_close(spa, FTAG);
3023 return (SET_ERROR(ENOENT));
3026 error = vdev_prop_set(vd, innvl, outnvl);
3028 spa_close(spa, FTAG);
3035 * "vdevprops_get_vdev" -> guid
3036 * (optional) "vdevprops_get_props" -> { propname -> propid }
3039 * outnvl: propname -> value
3041 static const zfs_ioc_key_t zfs_keys_vdev_get_props[] = {
3042 {ZPOOL_VDEV_PROPS_GET_VDEV, DATA_TYPE_UINT64, 0},
3043 {ZPOOL_VDEV_PROPS_GET_PROPS, DATA_TYPE_NVLIST, ZK_OPTIONAL}
3047 zfs_ioc_vdev_get_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3054 /* Early validation */
3055 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
3057 return (SET_ERROR(EINVAL));
3060 return (SET_ERROR(EINVAL));
3062 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3065 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3066 spa_close(spa, FTAG);
3067 return (SET_ERROR(ENOENT));
3070 error = vdev_prop_get(vd, innvl, outnvl);
3072 spa_close(spa, FTAG);
3079 * zc_name name of filesystem
3080 * zc_nvlist_src{_size} nvlist of delegated permissions
3081 * zc_perm_action allow/unallow flag
3086 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3089 nvlist_t *fsaclnv = NULL;
3091 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3092 zc->zc_iflags, &fsaclnv)) != 0)
3096 * Verify nvlist is constructed correctly
3098 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3099 nvlist_free(fsaclnv);
3100 return (SET_ERROR(EINVAL));
3104 * If we don't have PRIV_SYS_MOUNT, then validate
3105 * that user is allowed to hand out each permission in
3109 error = secpolicy_zfs(CRED());
3111 if (zc->zc_perm_action == B_FALSE) {
3112 error = dsl_deleg_can_allow(zc->zc_name,
3115 error = dsl_deleg_can_unallow(zc->zc_name,
3121 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3123 nvlist_free(fsaclnv);
3129 * zc_name name of filesystem
3132 * zc_nvlist_src{_size} nvlist of delegated permissions
3135 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3140 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3141 error = put_nvlist(zc, nvp);
3149 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3151 zfs_creat_t *zct = arg;
3153 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3156 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3160 * os parent objset pointer (NULL if root fs)
3161 * fuids_ok fuids allowed in this version of the spa?
3162 * sa_ok SAs allowed in this version of the spa?
3163 * createprops list of properties requested by creator
3166 * zplprops values for the zplprops we attach to the master node object
3167 * is_ci true if requested file system will be purely case-insensitive
3169 * Determine the settings for utf8only, normalization and
3170 * casesensitivity. Specific values may have been requested by the
3171 * creator and/or we can inherit values from the parent dataset. If
3172 * the file system is of too early a vintage, a creator can not
3173 * request settings for these properties, even if the requested
3174 * setting is the default value. We don't actually want to create dsl
3175 * properties for these, so remove them from the source nvlist after
3179 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3180 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3181 nvlist_t *zplprops, boolean_t *is_ci)
3183 uint64_t sense = ZFS_PROP_UNDEFINED;
3184 uint64_t norm = ZFS_PROP_UNDEFINED;
3185 uint64_t u8 = ZFS_PROP_UNDEFINED;
3188 ASSERT(zplprops != NULL);
3190 /* parent dataset must be a filesystem */
3191 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3192 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3195 * Pull out creator prop choices, if any.
3198 (void) nvlist_lookup_uint64(createprops,
3199 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3200 (void) nvlist_lookup_uint64(createprops,
3201 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3202 (void) nvlist_remove_all(createprops,
3203 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3204 (void) nvlist_lookup_uint64(createprops,
3205 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3206 (void) nvlist_remove_all(createprops,
3207 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3208 (void) nvlist_lookup_uint64(createprops,
3209 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3210 (void) nvlist_remove_all(createprops,
3211 zfs_prop_to_name(ZFS_PROP_CASE));
3215 * If the zpl version requested is whacky or the file system
3216 * or pool is version is too "young" to support normalization
3217 * and the creator tried to set a value for one of the props,
3220 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3221 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3222 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3223 (zplver < ZPL_VERSION_NORMALIZATION &&
3224 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3225 sense != ZFS_PROP_UNDEFINED)))
3226 return (SET_ERROR(ENOTSUP));
3229 * Put the version in the zplprops
3231 VERIFY(nvlist_add_uint64(zplprops,
3232 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3234 if (norm == ZFS_PROP_UNDEFINED &&
3235 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3237 VERIFY(nvlist_add_uint64(zplprops,
3238 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3241 * If we're normalizing, names must always be valid UTF-8 strings.
3245 if (u8 == ZFS_PROP_UNDEFINED &&
3246 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3248 VERIFY(nvlist_add_uint64(zplprops,
3249 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3251 if (sense == ZFS_PROP_UNDEFINED &&
3252 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3254 VERIFY(nvlist_add_uint64(zplprops,
3255 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3258 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3264 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3265 nvlist_t *zplprops, boolean_t *is_ci)
3267 boolean_t fuids_ok, sa_ok;
3268 uint64_t zplver = ZPL_VERSION;
3269 objset_t *os = NULL;
3270 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3275 zfs_get_parent(dataset, parentname, sizeof (parentname));
3277 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3280 spa_vers = spa_version(spa);
3281 spa_close(spa, FTAG);
3283 zplver = zfs_zpl_version_map(spa_vers);
3284 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3285 sa_ok = (zplver >= ZPL_VERSION_SA);
3288 * Open parent object set so we can inherit zplprop values.
3290 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3293 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3295 dmu_objset_rele(os, FTAG);
3300 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3301 nvlist_t *zplprops, boolean_t *is_ci)
3305 uint64_t zplver = ZPL_VERSION;
3308 zplver = zfs_zpl_version_map(spa_vers);
3309 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3310 sa_ok = (zplver >= ZPL_VERSION_SA);
3312 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3313 createprops, zplprops, is_ci);
3319 * "type" -> dmu_objset_type_t (int32)
3320 * (optional) "props" -> { prop -> value }
3321 * (optional) "hidden_args" -> { "wkeydata" -> value }
3322 * raw uint8_t array of encryption wrapping key data (32 bytes)
3325 * outnvl: propname -> error code (int32)
3328 static const zfs_ioc_key_t zfs_keys_create[] = {
3329 {"type", DATA_TYPE_INT32, 0},
3330 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3331 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3335 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3338 zfs_creat_t zct = { 0 };
3339 nvlist_t *nvprops = NULL;
3340 nvlist_t *hidden_args = NULL;
3341 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3342 dmu_objset_type_t type;
3343 boolean_t is_insensitive = B_FALSE;
3344 dsl_crypto_params_t *dcp = NULL;
3346 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3347 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3348 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3352 cbfunc = zfs_create_cb;
3356 cbfunc = zvol_create_cb;
3363 if (strchr(fsname, '@') ||
3364 strchr(fsname, '%'))
3365 return (SET_ERROR(EINVAL));
3367 zct.zct_props = nvprops;
3370 return (SET_ERROR(EINVAL));
3372 if (type == DMU_OST_ZVOL) {
3373 uint64_t volsize, volblocksize;
3375 if (nvprops == NULL)
3376 return (SET_ERROR(EINVAL));
3377 if (nvlist_lookup_uint64(nvprops,
3378 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3379 return (SET_ERROR(EINVAL));
3381 if ((error = nvlist_lookup_uint64(nvprops,
3382 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3383 &volblocksize)) != 0 && error != ENOENT)
3384 return (SET_ERROR(EINVAL));
3387 volblocksize = zfs_prop_default_numeric(
3388 ZFS_PROP_VOLBLOCKSIZE);
3390 if ((error = zvol_check_volblocksize(fsname,
3391 volblocksize)) != 0 ||
3392 (error = zvol_check_volsize(volsize,
3393 volblocksize)) != 0)
3395 } else if (type == DMU_OST_ZFS) {
3399 * We have to have normalization and
3400 * case-folding flags correct when we do the
3401 * file system creation, so go figure them out
3404 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3405 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3406 error = zfs_fill_zplprops(fsname, nvprops,
3407 zct.zct_zplprops, &is_insensitive);
3409 nvlist_free(zct.zct_zplprops);
3414 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3417 nvlist_free(zct.zct_zplprops);
3421 error = dmu_objset_create(fsname, type,
3422 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3424 nvlist_free(zct.zct_zplprops);
3425 dsl_crypto_params_free(dcp, !!error);
3428 * It would be nice to do this atomically.
3431 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3438 * Volumes will return EBUSY and cannot be destroyed
3439 * until all asynchronous minor handling (e.g. from
3440 * setting the volmode property) has completed. Wait for
3441 * the spa_zvol_taskq to drain then retry.
3443 error2 = dsl_destroy_head(fsname);
3444 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3445 error2 = spa_open(fsname, &spa, FTAG);
3447 taskq_wait(spa->spa_zvol_taskq);
3448 spa_close(spa, FTAG);
3450 error2 = dsl_destroy_head(fsname);
3459 * "origin" -> name of origin snapshot
3460 * (optional) "props" -> { prop -> value }
3461 * (optional) "hidden_args" -> { "wkeydata" -> value }
3462 * raw uint8_t array of encryption wrapping key data (32 bytes)
3466 * outnvl: propname -> error code (int32)
3468 static const zfs_ioc_key_t zfs_keys_clone[] = {
3469 {"origin", DATA_TYPE_STRING, 0},
3470 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3471 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3475 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3478 nvlist_t *nvprops = NULL;
3479 const char *origin_name;
3481 origin_name = fnvlist_lookup_string(innvl, "origin");
3482 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3484 if (strchr(fsname, '@') ||
3485 strchr(fsname, '%'))
3486 return (SET_ERROR(EINVAL));
3488 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3489 return (SET_ERROR(EINVAL));
3491 error = dmu_objset_clone(fsname, origin_name);
3494 * It would be nice to do this atomically.
3497 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3500 (void) dsl_destroy_head(fsname);
3505 static const zfs_ioc_key_t zfs_keys_remap[] = {
3510 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3512 /* This IOCTL is no longer supported. */
3513 (void) fsname, (void) innvl, (void) outnvl;
3519 * "snaps" -> { snapshot1, snapshot2 }
3520 * (optional) "props" -> { prop -> value (string) }
3523 * outnvl: snapshot -> error code (int32)
3525 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3526 {"snaps", DATA_TYPE_NVLIST, 0},
3527 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3531 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3534 nvlist_t *props = NULL;
3538 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3539 if (!nvlist_empty(props) &&
3540 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3541 return (SET_ERROR(ENOTSUP));
3542 if ((error = zfs_check_userprops(props)) != 0)
3545 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3546 poollen = strlen(poolname);
3547 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3548 pair = nvlist_next_nvpair(snaps, pair)) {
3549 const char *name = nvpair_name(pair);
3550 char *cp = strchr(name, '@');
3553 * The snap name must contain an @, and the part after it must
3554 * contain only valid characters.
3557 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3558 return (SET_ERROR(EINVAL));
3561 * The snap must be in the specified pool.
3563 if (strncmp(name, poolname, poollen) != 0 ||
3564 (name[poollen] != '/' && name[poollen] != '@'))
3565 return (SET_ERROR(EXDEV));
3568 * Check for permission to set the properties on the fs.
3570 if (!nvlist_empty(props)) {
3572 error = zfs_secpolicy_write_perms(name,
3573 ZFS_DELEG_PERM_USERPROP, CRED());
3579 /* This must be the only snap of this fs. */
3580 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3581 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3582 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3584 return (SET_ERROR(EXDEV));
3589 error = dsl_dataset_snapshot(snaps, props, outnvl);
3595 * innvl: "message" -> string
3597 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3598 {"message", DATA_TYPE_STRING, 0},
3602 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3604 (void) unused, (void) outnvl;
3605 const char *message;
3611 * The poolname in the ioctl is not set, we get it from the TSD,
3612 * which was set at the end of the last successful ioctl that allows
3613 * logging. The secpolicy func already checked that it is set.
3614 * Only one log ioctl is allowed after each successful ioctl, so
3615 * we clear the TSD here.
3617 poolname = tsd_get(zfs_allow_log_key);
3618 if (poolname == NULL)
3619 return (SET_ERROR(EINVAL));
3620 (void) tsd_set(zfs_allow_log_key, NULL);
3621 error = spa_open(poolname, &spa, FTAG);
3622 kmem_strfree(poolname);
3626 message = fnvlist_lookup_string(innvl, "message");
3628 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3629 spa_close(spa, FTAG);
3630 return (SET_ERROR(ENOTSUP));
3633 error = spa_history_log(spa, message);
3634 spa_close(spa, FTAG);
3639 * This ioctl is used to set the bootenv configuration on the current
3640 * pool. This configuration is stored in the second padding area of the label,
3641 * and it is used by the bootloader(s) to store the bootloader and/or system
3643 * The data is stored as nvlist data stream, and is protected by
3644 * an embedded checksum.
3645 * The version can have two possible values:
3646 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3647 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3649 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3650 {"version", DATA_TYPE_UINT64, 0},
3651 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3655 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3660 if ((error = spa_open(name, &spa, FTAG)) != 0)
3662 spa_vdev_state_enter(spa, SCL_ALL);
3663 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3664 (void) spa_vdev_state_exit(spa, NULL, 0);
3665 spa_close(spa, FTAG);
3669 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3674 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3679 if ((error = spa_open(name, &spa, FTAG)) != 0)
3681 spa_vdev_state_enter(spa, SCL_ALL);
3682 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3683 (void) spa_vdev_state_exit(spa, NULL, 0);
3684 spa_close(spa, FTAG);
3689 * The dp_config_rwlock must not be held when calling this, because the
3690 * unmount may need to write out data.
3692 * This function is best-effort. Callers must deal gracefully if it
3693 * remains mounted (or is remounted after this call).
3695 * Returns 0 if the argument is not a snapshot, or it is not currently a
3696 * filesystem, or we were able to unmount it. Returns error code otherwise.
3699 zfs_unmount_snap(const char *snapname)
3701 if (strchr(snapname, '@') == NULL)
3704 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE);
3708 zfs_unmount_snap_cb(const char *snapname, void *arg)
3711 zfs_unmount_snap(snapname);
3716 * When a clone is destroyed, its origin may also need to be destroyed,
3717 * in which case it must be unmounted. This routine will do that unmount
3721 zfs_destroy_unmount_origin(const char *fsname)
3727 error = dmu_objset_hold(fsname, FTAG, &os);
3730 ds = dmu_objset_ds(os);
3731 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3732 char originname[ZFS_MAX_DATASET_NAME_LEN];
3733 dsl_dataset_name(ds->ds_prev, originname);
3734 dmu_objset_rele(os, FTAG);
3735 zfs_unmount_snap(originname);
3737 dmu_objset_rele(os, FTAG);
3743 * "snaps" -> { snapshot1, snapshot2 }
3744 * (optional boolean) "defer"
3747 * outnvl: snapshot -> error code (int32)
3749 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3750 {"snaps", DATA_TYPE_NVLIST, 0},
3751 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3755 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3763 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3764 defer = nvlist_exists(innvl, "defer");
3766 poollen = strlen(poolname);
3767 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3768 pair = nvlist_next_nvpair(snaps, pair)) {
3769 const char *name = nvpair_name(pair);
3772 * The snap must be in the specified pool to prevent the
3773 * invalid removal of zvol minors below.
3775 if (strncmp(name, poolname, poollen) != 0 ||
3776 (name[poollen] != '/' && name[poollen] != '@'))
3777 return (SET_ERROR(EXDEV));
3779 zfs_unmount_snap(nvpair_name(pair));
3780 if (spa_open(name, &spa, FTAG) == 0) {
3781 zvol_remove_minors(spa, name, B_TRUE);
3782 spa_close(spa, FTAG);
3786 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3790 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>.
3791 * All bookmarks and snapshots must be in the same pool.
3792 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail.
3795 * new_bookmark1 -> existing_snapshot,
3796 * new_bookmark2 -> existing_bookmark,
3799 * outnvl: bookmark -> error code (int32)
3802 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3803 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3807 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3810 return (dsl_bookmark_create(innvl, outnvl));
3815 * property 1, property 2, ...
3819 * bookmark name 1 -> { property 1, property 2, ... },
3820 * bookmark name 2 -> { property 1, property 2, ... }
3824 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3825 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3829 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3831 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3835 * innvl is not used.
3838 * property 1, property 2, ...
3842 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
3847 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
3851 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3854 bmname = strchr(bookmark, '#');
3856 return (SET_ERROR(EINVAL));
3859 (void) strlcpy(fsname, bookmark, sizeof (fsname));
3860 *(strchr(fsname, '#')) = '\0';
3862 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
3867 * bookmark name 1, bookmark name 2
3870 * outnvl: bookmark -> error code (int32)
3873 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3874 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3878 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3883 poollen = strlen(poolname);
3884 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3885 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3886 const char *name = nvpair_name(pair);
3887 const char *cp = strchr(name, '#');
3890 * The bookmark name must contain an #, and the part after it
3891 * must contain only valid characters.
3894 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3895 return (SET_ERROR(EINVAL));
3898 * The bookmark must be in the specified pool.
3900 if (strncmp(name, poolname, poollen) != 0 ||
3901 (name[poollen] != '/' && name[poollen] != '#'))
3902 return (SET_ERROR(EXDEV));
3905 error = dsl_bookmark_destroy(innvl, outnvl);
3909 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3910 {"program", DATA_TYPE_STRING, 0},
3911 {"arg", DATA_TYPE_ANY, 0},
3912 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3913 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3914 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3918 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3922 uint64_t instrlimit, memlimit;
3923 boolean_t sync_flag;
3924 nvpair_t *nvarg = NULL;
3926 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3927 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3930 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3931 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3933 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3934 memlimit = ZCP_DEFAULT_MEMLIMIT;
3936 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3938 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3939 return (SET_ERROR(EINVAL));
3940 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3941 return (SET_ERROR(EINVAL));
3943 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3951 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3956 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3958 (void) innvl, (void) outnvl;
3959 return (spa_checkpoint(poolname));
3966 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3971 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3974 (void) innvl, (void) outnvl;
3975 return (spa_checkpoint_discard(poolname));
3980 * zc_name name of dataset to destroy
3981 * zc_defer_destroy mark for deferred destroy
3986 zfs_ioc_destroy(zfs_cmd_t *zc)
3989 dmu_objset_type_t ost;
3992 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3995 ost = dmu_objset_type(os);
3996 dmu_objset_rele(os, FTAG);
3998 if (ost == DMU_OST_ZFS)
3999 zfs_unmount_snap(zc->zc_name);
4001 if (strchr(zc->zc_name, '@')) {
4002 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
4004 err = dsl_destroy_head(zc->zc_name);
4005 if (err == EEXIST) {
4007 * It is possible that the given DS may have
4008 * hidden child (%recv) datasets - "leftovers"
4009 * resulting from the previously interrupted
4012 * 6 extra bytes for /%recv
4014 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
4016 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
4017 zc->zc_name, recv_clone_name) >=
4019 return (SET_ERROR(EINVAL));
4022 * Try to remove the hidden child (%recv) and after
4023 * that try to remove the target dataset.
4024 * If the hidden child (%recv) does not exist
4025 * the original error (EEXIST) will be returned
4027 err = dsl_destroy_head(namebuf);
4029 err = dsl_destroy_head(zc->zc_name);
4030 else if (err == ENOENT)
4031 err = SET_ERROR(EEXIST);
4040 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
4041 * "initialize_vdevs": { -> guids to initialize (nvlist)
4042 * "vdev_path_1": vdev_guid_1, (uint64),
4043 * "vdev_path_2": vdev_guid_2, (uint64),
4049 * "initialize_vdevs": { -> initialization errors (nvlist)
4050 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4051 * "vdev_path_2": errno, ... (uint64)
4056 * EINVAL is returned for an unknown commands or if any of the provided vdev
4057 * guids have be specified with a type other than uint64.
4059 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
4060 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
4061 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4065 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4068 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4070 return (SET_ERROR(EINVAL));
4073 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4074 cmd_type == POOL_INITIALIZE_START ||
4075 cmd_type == POOL_INITIALIZE_SUSPEND)) {
4076 return (SET_ERROR(EINVAL));
4079 nvlist_t *vdev_guids;
4080 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4081 &vdev_guids) != 0) {
4082 return (SET_ERROR(EINVAL));
4085 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4086 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4088 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4089 return (SET_ERROR(EINVAL));
4094 int error = spa_open(poolname, &spa, FTAG);
4098 nvlist_t *vdev_errlist = fnvlist_alloc();
4099 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4102 if (fnvlist_size(vdev_errlist) > 0) {
4103 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4106 fnvlist_free(vdev_errlist);
4108 spa_close(spa, FTAG);
4109 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4114 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4115 * "trim_vdevs": { -> guids to TRIM (nvlist)
4116 * "vdev_path_1": vdev_guid_1, (uint64),
4117 * "vdev_path_2": vdev_guid_2, (uint64),
4120 * "trim_rate" -> Target TRIM rate in bytes/sec.
4121 * "trim_secure" -> Set to request a secure TRIM.
4125 * "trim_vdevs": { -> TRIM errors (nvlist)
4126 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4127 * "vdev_path_2": errno, ... (uint64)
4132 * EINVAL is returned for an unknown commands or if any of the provided vdev
4133 * guids have be specified with a type other than uint64.
4135 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4136 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4137 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4138 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4139 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4143 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4146 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4147 return (SET_ERROR(EINVAL));
4149 if (!(cmd_type == POOL_TRIM_CANCEL ||
4150 cmd_type == POOL_TRIM_START ||
4151 cmd_type == POOL_TRIM_SUSPEND)) {
4152 return (SET_ERROR(EINVAL));
4155 nvlist_t *vdev_guids;
4156 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4157 return (SET_ERROR(EINVAL));
4159 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4160 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4162 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4163 return (SET_ERROR(EINVAL));
4167 /* Optional, defaults to maximum rate when not provided */
4169 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4172 /* Optional, defaults to standard TRIM when not provided */
4174 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4180 int error = spa_open(poolname, &spa, FTAG);
4184 nvlist_t *vdev_errlist = fnvlist_alloc();
4185 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4186 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4188 if (fnvlist_size(vdev_errlist) > 0)
4189 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4191 fnvlist_free(vdev_errlist);
4193 spa_close(spa, FTAG);
4194 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4198 * This ioctl waits for activity of a particular type to complete. If there is
4199 * no activity of that type in progress, it returns immediately, and the
4200 * returned value "waited" is false. If there is activity in progress, and no
4201 * tag is passed in, the ioctl blocks until all activity of that type is
4202 * complete, and then returns with "waited" set to true.
4204 * If a tag is provided, it identifies a particular instance of an activity to
4205 * wait for. Currently, this is only valid for use with 'initialize', because
4206 * that is the only activity for which there can be multiple instances running
4207 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4208 * the vdev on which to wait.
4210 * If a thread waiting in the ioctl receives a signal, the call will return
4211 * immediately, and the return value will be EINTR.
4214 * "wait_activity" -> int32_t
4215 * (optional) "wait_tag" -> uint64_t
4218 * outnvl: "waited" -> boolean_t
4220 static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4221 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4222 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4226 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4233 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4236 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4237 error = spa_wait_tag(name, activity, tag, &waited);
4239 error = spa_wait(name, activity, &waited);
4242 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4248 * This ioctl waits for activity of a particular type to complete. If there is
4249 * no activity of that type in progress, it returns immediately, and the
4250 * returned value "waited" is false. If there is activity in progress, and no
4251 * tag is passed in, the ioctl blocks until all activity of that type is
4252 * complete, and then returns with "waited" set to true.
4254 * If a thread waiting in the ioctl receives a signal, the call will return
4255 * immediately, and the return value will be EINTR.
4258 * "wait_activity" -> int32_t
4261 * outnvl: "waited" -> boolean_t
4263 static const zfs_ioc_key_t zfs_keys_fs_wait[] = {
4264 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4268 zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4271 boolean_t waited = B_FALSE;
4277 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0)
4278 return (SET_ERROR(EINVAL));
4280 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0)
4281 return (SET_ERROR(EINVAL));
4283 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
4286 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
4287 dsl_pool_rele(dp, FTAG);
4292 mutex_enter(&dd->dd_activity_lock);
4293 dd->dd_activity_waiters++;
4296 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t
4297 * aren't evicted while we're waiting. Normally this is prevented by
4298 * holding the pool, but we can't do that while we're waiting since
4299 * that would prevent TXGs from syncing out. Some of the functionality
4300 * of long-holds (e.g. preventing deletion) is unnecessary for this
4301 * case, since we would cancel the waiters before proceeding with a
4302 * deletion. An alternative mechanism for keeping the dataset around
4303 * could be developed but this is simpler.
4305 dsl_dataset_long_hold(ds, FTAG);
4306 dsl_pool_rele(dp, FTAG);
4308 error = dsl_dir_wait(dd, ds, activity, &waited);
4310 dsl_dataset_long_rele(ds, FTAG);
4311 dd->dd_activity_waiters--;
4312 if (dd->dd_activity_waiters == 0)
4313 cv_signal(&dd->dd_activity_cv);
4314 mutex_exit(&dd->dd_activity_lock);
4316 dsl_dataset_rele(ds, FTAG);
4319 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited);
4325 * fsname is name of dataset to rollback (to most recent snapshot)
4327 * innvl may contain name of expected target snapshot
4329 * outnvl: "target" -> name of most recent snapshot
4332 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4333 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4337 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4340 zvol_state_handle_t *zv;
4341 char *target = NULL;
4344 (void) nvlist_lookup_string(innvl, "target", &target);
4345 if (target != NULL) {
4346 const char *cp = strchr(target, '@');
4349 * The snap name must contain an @, and the part after it must
4350 * contain only valid characters.
4353 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4354 return (SET_ERROR(EINVAL));
4357 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4360 ds = dmu_objset_ds(zfsvfs->z_os);
4361 error = zfs_suspend_fs(zfsvfs);
4365 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4367 resume_err = zfs_resume_fs(zfsvfs, ds);
4368 error = error ? error : resume_err;
4370 zfs_vfs_rele(zfsvfs);
4371 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4372 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4376 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4382 recursive_unmount(const char *fsname, void *arg)
4384 const char *snapname = arg;
4387 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4388 zfs_unmount_snap(fullname);
4389 kmem_strfree(fullname);
4396 * snapname is the snapshot to redact.
4398 * "bookname" -> (string)
4399 * shortname of the redaction bookmark to generate
4400 * "snapnv" -> (nvlist, values ignored)
4401 * snapshots to redact snapname with respect to
4407 static const zfs_ioc_key_t zfs_keys_redact[] = {
4408 {"bookname", DATA_TYPE_STRING, 0},
4409 {"snapnv", DATA_TYPE_NVLIST, 0},
4413 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4416 nvlist_t *redactnvl = NULL;
4417 char *redactbook = NULL;
4419 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4420 return (SET_ERROR(EINVAL));
4421 if (fnvlist_num_pairs(redactnvl) == 0)
4422 return (SET_ERROR(ENXIO));
4423 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4424 return (SET_ERROR(EINVAL));
4426 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4431 * zc_name old name of dataset
4432 * zc_value new name of dataset
4433 * zc_cookie recursive flag (only valid for snapshots)
4438 zfs_ioc_rename(zfs_cmd_t *zc)
4441 dmu_objset_type_t ost;
4442 boolean_t recursive = zc->zc_cookie & 1;
4443 boolean_t nounmount = !!(zc->zc_cookie & 2);
4447 /* "zfs rename" from and to ...%recv datasets should both fail */
4448 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4449 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4450 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4451 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4452 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4453 return (SET_ERROR(EINVAL));
4455 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4458 ost = dmu_objset_type(os);
4459 dmu_objset_rele(os, FTAG);
4461 at = strchr(zc->zc_name, '@');
4463 /* snaps must be in same fs */
4466 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4467 return (SET_ERROR(EXDEV));
4469 if (ost == DMU_OST_ZFS && !nounmount) {
4470 error = dmu_objset_find(zc->zc_name,
4471 recursive_unmount, at + 1,
4472 recursive ? DS_FIND_CHILDREN : 0);
4478 error = dsl_dataset_rename_snapshot(zc->zc_name,
4479 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4484 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4489 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4491 const char *propname = nvpair_name(pair);
4492 boolean_t issnap = (strchr(dsname, '@') != NULL);
4493 zfs_prop_t prop = zfs_name_to_prop(propname);
4494 uint64_t intval, compval;
4497 if (prop == ZPROP_USERPROP) {
4498 if (zfs_prop_user(propname)) {
4499 if ((err = zfs_secpolicy_write_perms(dsname,
4500 ZFS_DELEG_PERM_USERPROP, cr)))
4505 if (!issnap && zfs_prop_userquota(propname)) {
4506 const char *perm = NULL;
4507 const char *uq_prefix =
4508 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4509 const char *gq_prefix =
4510 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4511 const char *uiq_prefix =
4512 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4513 const char *giq_prefix =
4514 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4515 const char *pq_prefix =
4516 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4517 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4518 ZFS_PROP_PROJECTOBJQUOTA];
4520 if (strncmp(propname, uq_prefix,
4521 strlen(uq_prefix)) == 0) {
4522 perm = ZFS_DELEG_PERM_USERQUOTA;
4523 } else if (strncmp(propname, uiq_prefix,
4524 strlen(uiq_prefix)) == 0) {
4525 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4526 } else if (strncmp(propname, gq_prefix,
4527 strlen(gq_prefix)) == 0) {
4528 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4529 } else if (strncmp(propname, giq_prefix,
4530 strlen(giq_prefix)) == 0) {
4531 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4532 } else if (strncmp(propname, pq_prefix,
4533 strlen(pq_prefix)) == 0) {
4534 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4535 } else if (strncmp(propname, piq_prefix,
4536 strlen(piq_prefix)) == 0) {
4537 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4539 /* {USER|GROUP|PROJECT}USED are read-only */
4540 return (SET_ERROR(EINVAL));
4543 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4548 return (SET_ERROR(EINVAL));
4552 return (SET_ERROR(EINVAL));
4554 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4556 * dsl_prop_get_all_impl() returns properties in this
4560 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4561 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4566 * Check that this value is valid for this pool version
4569 case ZFS_PROP_COMPRESSION:
4571 * If the user specified gzip compression, make sure
4572 * the SPA supports it. We ignore any errors here since
4573 * we'll catch them later.
4575 if (nvpair_value_uint64(pair, &intval) == 0) {
4576 compval = ZIO_COMPRESS_ALGO(intval);
4577 if (compval >= ZIO_COMPRESS_GZIP_1 &&
4578 compval <= ZIO_COMPRESS_GZIP_9 &&
4579 zfs_earlier_version(dsname,
4580 SPA_VERSION_GZIP_COMPRESSION)) {
4581 return (SET_ERROR(ENOTSUP));
4584 if (compval == ZIO_COMPRESS_ZLE &&
4585 zfs_earlier_version(dsname,
4586 SPA_VERSION_ZLE_COMPRESSION))
4587 return (SET_ERROR(ENOTSUP));
4589 if (compval == ZIO_COMPRESS_LZ4) {
4592 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4595 if (!spa_feature_is_enabled(spa,
4596 SPA_FEATURE_LZ4_COMPRESS)) {
4597 spa_close(spa, FTAG);
4598 return (SET_ERROR(ENOTSUP));
4600 spa_close(spa, FTAG);
4603 if (compval == ZIO_COMPRESS_ZSTD) {
4606 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4609 if (!spa_feature_is_enabled(spa,
4610 SPA_FEATURE_ZSTD_COMPRESS)) {
4611 spa_close(spa, FTAG);
4612 return (SET_ERROR(ENOTSUP));
4614 spa_close(spa, FTAG);
4619 case ZFS_PROP_COPIES:
4620 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4621 return (SET_ERROR(ENOTSUP));
4624 case ZFS_PROP_VOLBLOCKSIZE:
4625 case ZFS_PROP_RECORDSIZE:
4626 /* Record sizes above 128k need the feature to be enabled */
4627 if (nvpair_value_uint64(pair, &intval) == 0 &&
4628 intval > SPA_OLD_MAXBLOCKSIZE) {
4632 * We don't allow setting the property above 1MB,
4633 * unless the tunable has been changed.
4635 if (intval > zfs_max_recordsize ||
4636 intval > SPA_MAXBLOCKSIZE)
4637 return (SET_ERROR(ERANGE));
4639 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4642 if (!spa_feature_is_enabled(spa,
4643 SPA_FEATURE_LARGE_BLOCKS)) {
4644 spa_close(spa, FTAG);
4645 return (SET_ERROR(ENOTSUP));
4647 spa_close(spa, FTAG);
4651 case ZFS_PROP_DNODESIZE:
4652 /* Dnode sizes above 512 need the feature to be enabled */
4653 if (nvpair_value_uint64(pair, &intval) == 0 &&
4654 intval != ZFS_DNSIZE_LEGACY) {
4657 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4660 if (!spa_feature_is_enabled(spa,
4661 SPA_FEATURE_LARGE_DNODE)) {
4662 spa_close(spa, FTAG);
4663 return (SET_ERROR(ENOTSUP));
4665 spa_close(spa, FTAG);
4669 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4671 * This property could require the allocation classes
4672 * feature to be active for setting, however we allow
4673 * it so that tests of settable properties succeed.
4674 * The CLI will issue a warning in this case.
4678 case ZFS_PROP_SHARESMB:
4679 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4680 return (SET_ERROR(ENOTSUP));
4683 case ZFS_PROP_ACLINHERIT:
4684 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4685 nvpair_value_uint64(pair, &intval) == 0) {
4686 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4687 zfs_earlier_version(dsname,
4688 SPA_VERSION_PASSTHROUGH_X))
4689 return (SET_ERROR(ENOTSUP));
4692 case ZFS_PROP_CHECKSUM:
4693 case ZFS_PROP_DEDUP:
4695 spa_feature_t feature;
4699 /* dedup feature version checks */
4700 if (prop == ZFS_PROP_DEDUP &&
4701 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4702 return (SET_ERROR(ENOTSUP));
4704 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4705 nvpair_value_uint64(pair, &intval) == 0) {
4706 /* check prop value is enabled in features */
4707 feature = zio_checksum_to_feature(
4708 intval & ZIO_CHECKSUM_MASK);
4709 if (feature == SPA_FEATURE_NONE)
4712 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4715 if (!spa_feature_is_enabled(spa, feature)) {
4716 spa_close(spa, FTAG);
4717 return (SET_ERROR(ENOTSUP));
4719 spa_close(spa, FTAG);
4728 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4732 * Removes properties from the given props list that fail permission checks
4733 * needed to clear them and to restore them in case of a receive error. For each
4734 * property, make sure we have both set and inherit permissions.
4736 * Returns the first error encountered if any permission checks fail. If the
4737 * caller provides a non-NULL errlist, it also gives the complete list of names
4738 * of all the properties that failed a permission check along with the
4739 * corresponding error numbers. The caller is responsible for freeing the
4742 * If every property checks out successfully, zero is returned and the list
4743 * pointed at by errlist is NULL.
4746 zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist)
4749 nvpair_t *pair, *next_pair;
4756 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4758 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4759 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4760 pair = nvlist_next_nvpair(props, NULL);
4761 while (pair != NULL) {
4762 next_pair = nvlist_next_nvpair(props, pair);
4764 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4765 sizeof (zc->zc_value));
4766 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4767 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4768 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4769 VERIFY(nvlist_add_int32(errors,
4770 zc->zc_value, err) == 0);
4774 kmem_free(zc, sizeof (zfs_cmd_t));
4776 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4777 nvlist_free(errors);
4780 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4783 if (errlist == NULL)
4784 nvlist_free(errors);
4792 propval_equals(nvpair_t *p1, nvpair_t *p2)
4794 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4795 /* dsl_prop_get_all_impl() format */
4797 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4798 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4802 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4804 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4805 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4809 if (nvpair_type(p1) != nvpair_type(p2))
4812 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4813 char *valstr1, *valstr2;
4815 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4816 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4817 return (strcmp(valstr1, valstr2) == 0);
4819 uint64_t intval1, intval2;
4821 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4822 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4823 return (intval1 == intval2);
4828 * Remove properties from props if they are not going to change (as determined
4829 * by comparison with origprops). Remove them from origprops as well, since we
4830 * do not need to clear or restore properties that won't change.
4833 props_reduce(nvlist_t *props, nvlist_t *origprops)
4835 nvpair_t *pair, *next_pair;
4837 if (origprops == NULL)
4838 return; /* all props need to be received */
4840 pair = nvlist_next_nvpair(props, NULL);
4841 while (pair != NULL) {
4842 const char *propname = nvpair_name(pair);
4845 next_pair = nvlist_next_nvpair(props, pair);
4847 if ((nvlist_lookup_nvpair(origprops, propname,
4848 &match) != 0) || !propval_equals(pair, match))
4849 goto next; /* need to set received value */
4851 /* don't clear the existing received value */
4852 (void) nvlist_remove_nvpair(origprops, match);
4853 /* don't bother receiving the property */
4854 (void) nvlist_remove_nvpair(props, pair);
4861 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4862 * For example, refquota cannot be set until after the receipt of a dataset,
4863 * because in replication streams, an older/earlier snapshot may exceed the
4864 * refquota. We want to receive the older/earlier snapshot, but setting
4865 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4866 * the older/earlier snapshot from being received (with EDQUOT).
4868 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4870 * libzfs will need to be judicious handling errors encountered by props
4871 * extracted by this function.
4874 extract_delay_props(nvlist_t *props)
4876 nvlist_t *delayprops;
4877 nvpair_t *nvp, *tmp;
4878 static const zfs_prop_t delayable[] = {
4880 ZFS_PROP_KEYLOCATION,
4882 * Setting ZFS_PROP_SHARESMB requires the objset type to be
4883 * known, which is not possible prior to receipt of raw sends.
4890 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4892 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4893 nvp = nvlist_next_nvpair(props, nvp)) {
4895 * strcmp() is safe because zfs_prop_to_name() always returns
4898 for (i = 0; delayable[i] != 0; i++) {
4899 if (strcmp(zfs_prop_to_name(delayable[i]),
4900 nvpair_name(nvp)) == 0) {
4904 if (delayable[i] != 0) {
4905 tmp = nvlist_prev_nvpair(props, nvp);
4906 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4907 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4912 if (nvlist_empty(delayprops)) {
4913 nvlist_free(delayprops);
4916 return (delayprops);
4920 zfs_allow_log_destroy(void *arg)
4922 char *poolname = arg;
4924 if (poolname != NULL)
4925 kmem_strfree(poolname);
4929 static boolean_t zfs_ioc_recv_inject_err;
4933 * nvlist 'errors' is always allocated. It will contain descriptions of
4934 * encountered errors, if any. It's the callers responsibility to free.
4937 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4938 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4939 boolean_t heal, boolean_t resumable, int input_fd,
4940 dmu_replay_record_t *begin_record, uint64_t *read_bytes,
4941 uint64_t *errflags, nvlist_t **errors)
4943 dmu_recv_cookie_t drc;
4945 int props_error = 0;
4947 nvlist_t *local_delayprops = NULL;
4948 nvlist_t *recv_delayprops = NULL;
4949 nvlist_t *inherited_delayprops = NULL;
4950 nvlist_t *origprops = NULL; /* existing properties */
4951 nvlist_t *origrecvd = NULL; /* existing received properties */
4952 boolean_t first_recvd_props = B_FALSE;
4953 boolean_t tofs_was_redacted;
4954 zfs_file_t *input_fp;
4958 *errors = fnvlist_alloc();
4961 if ((input_fp = zfs_file_get(input_fd)) == NULL)
4962 return (SET_ERROR(EBADF));
4964 noff = off = zfs_file_off(input_fp);
4965 error = dmu_recv_begin(tofs, tosnap, begin_record, force, heal,
4966 resumable, localprops, hidden_args, origin, &drc, input_fp,
4970 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
4973 * Set properties before we receive the stream so that they are applied
4974 * to the new data. Note that we must call dmu_recv_stream() if
4975 * dmu_recv_begin() succeeds.
4977 if (recvprops != NULL && !drc.drc_newfs) {
4978 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4979 SPA_VERSION_RECVD_PROPS &&
4980 !dsl_prop_get_hasrecvd(tofs))
4981 first_recvd_props = B_TRUE;
4984 * If new received properties are supplied, they are to
4985 * completely replace the existing received properties,
4986 * so stash away the existing ones.
4988 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4989 nvlist_t *errlist = NULL;
4991 * Don't bother writing a property if its value won't
4992 * change (and avoid the unnecessary security checks).
4994 * The first receive after SPA_VERSION_RECVD_PROPS is a
4995 * special case where we blow away all local properties
4998 if (!first_recvd_props)
4999 props_reduce(recvprops, origrecvd);
5000 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
5001 (void) nvlist_merge(*errors, errlist, 0);
5002 nvlist_free(errlist);
5004 if (clear_received_props(tofs, origrecvd,
5005 first_recvd_props ? NULL : recvprops) != 0)
5006 *errflags |= ZPROP_ERR_NOCLEAR;
5008 *errflags |= ZPROP_ERR_NOCLEAR;
5013 * Stash away existing properties so we can restore them on error unless
5014 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
5015 * case "origrecvd" will take care of that.
5017 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
5019 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
5020 if (dsl_prop_get_all(os, &origprops) != 0) {
5021 *errflags |= ZPROP_ERR_NOCLEAR;
5023 dmu_objset_rele(os, FTAG);
5025 *errflags |= ZPROP_ERR_NOCLEAR;
5029 if (recvprops != NULL) {
5030 props_error = dsl_prop_set_hasrecvd(tofs);
5032 if (props_error == 0) {
5033 recv_delayprops = extract_delay_props(recvprops);
5034 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5035 recvprops, *errors);
5039 if (localprops != NULL) {
5040 nvlist_t *oprops = fnvlist_alloc();
5041 nvlist_t *xprops = fnvlist_alloc();
5042 nvpair_t *nvp = NULL;
5044 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5045 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
5047 const char *name = nvpair_name(nvp);
5048 zfs_prop_t prop = zfs_name_to_prop(name);
5049 if (prop != ZPROP_USERPROP) {
5050 if (!zfs_prop_inheritable(prop))
5052 } else if (!zfs_prop_user(name))
5054 fnvlist_add_boolean(xprops, name);
5056 /* -o property=value */
5057 fnvlist_add_nvpair(oprops, nvp);
5061 local_delayprops = extract_delay_props(oprops);
5062 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5064 inherited_delayprops = extract_delay_props(xprops);
5065 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5068 nvlist_free(oprops);
5069 nvlist_free(xprops);
5072 error = dmu_recv_stream(&drc, &off);
5075 zfsvfs_t *zfsvfs = NULL;
5076 zvol_state_handle_t *zv = NULL;
5078 if (getzfsvfs(tofs, &zfsvfs) == 0) {
5082 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
5083 begin_record->drr_u.drr_begin.
5084 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
5086 ds = dmu_objset_ds(zfsvfs->z_os);
5087 error = zfs_suspend_fs(zfsvfs);
5089 * If the suspend fails, then the recv_end will
5090 * likely also fail, and clean up after itself.
5092 end_err = dmu_recv_end(&drc, zfsvfs);
5094 * If the dataset was not redacted, but we received a
5095 * redacted stream onto it, we need to unmount the
5096 * dataset. Otherwise, resume the filesystem.
5098 if (error == 0 && !drc.drc_newfs &&
5099 stream_is_redacted && !tofs_was_redacted) {
5100 error = zfs_end_fs(zfsvfs, ds);
5101 } else if (error == 0) {
5102 error = zfs_resume_fs(zfsvfs, ds);
5104 error = error ? error : end_err;
5105 zfs_vfs_rele(zfsvfs);
5106 } else if ((zv = zvol_suspend(tofs)) != NULL) {
5107 error = dmu_recv_end(&drc, zvol_tag(zv));
5110 error = dmu_recv_end(&drc, NULL);
5113 /* Set delayed properties now, after we're done receiving. */
5114 if (recv_delayprops != NULL && error == 0) {
5115 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5116 recv_delayprops, *errors);
5118 if (local_delayprops != NULL && error == 0) {
5119 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5120 local_delayprops, *errors);
5122 if (inherited_delayprops != NULL && error == 0) {
5123 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5124 inherited_delayprops, *errors);
5129 * Merge delayed props back in with initial props, in case
5130 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
5131 * we have to make sure clear_received_props() includes
5132 * the delayed properties).
5134 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
5135 * using ASSERT() will be just like a VERIFY.
5137 if (recv_delayprops != NULL) {
5138 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
5139 nvlist_free(recv_delayprops);
5141 if (local_delayprops != NULL) {
5142 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
5143 nvlist_free(local_delayprops);
5145 if (inherited_delayprops != NULL) {
5146 ASSERT(nvlist_merge(localprops, inherited_delayprops, 0) == 0);
5147 nvlist_free(inherited_delayprops);
5149 *read_bytes = off - noff;
5152 if (zfs_ioc_recv_inject_err) {
5153 zfs_ioc_recv_inject_err = B_FALSE;
5159 * On error, restore the original props.
5161 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
5162 if (clear_received_props(tofs, recvprops, NULL) != 0) {
5164 * We failed to clear the received properties.
5165 * Since we may have left a $recvd value on the
5166 * system, we can't clear the $hasrecvd flag.
5168 *errflags |= ZPROP_ERR_NORESTORE;
5169 } else if (first_recvd_props) {
5170 dsl_prop_unset_hasrecvd(tofs);
5173 if (origrecvd == NULL && !drc.drc_newfs) {
5174 /* We failed to stash the original properties. */
5175 *errflags |= ZPROP_ERR_NORESTORE;
5179 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5180 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5181 * explicitly if we're restoring local properties cleared in the
5182 * first new-style receive.
5184 if (origrecvd != NULL &&
5185 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5186 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5187 origrecvd, NULL) != 0) {
5189 * We stashed the original properties but failed to
5192 *errflags |= ZPROP_ERR_NORESTORE;
5195 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5196 !first_recvd_props) {
5198 nvlist_t *inheritprops;
5201 if (origprops == NULL) {
5202 /* We failed to stash the original properties. */
5203 *errflags |= ZPROP_ERR_NORESTORE;
5207 /* Restore original props */
5208 setprops = fnvlist_alloc();
5209 inheritprops = fnvlist_alloc();
5211 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5212 const char *name = nvpair_name(nvp);
5216 if (!nvlist_exists(origprops, name)) {
5218 * Property was not present or was explicitly
5219 * inherited before the receive, restore this.
5221 fnvlist_add_boolean(inheritprops, name);
5224 attrs = fnvlist_lookup_nvlist(origprops, name);
5225 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5227 /* Skip received properties */
5228 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5231 if (strcmp(source, tofs) == 0) {
5232 /* Property was locally set */
5233 fnvlist_add_nvlist(setprops, name, attrs);
5235 /* Property was implicitly inherited */
5236 fnvlist_add_boolean(inheritprops, name);
5240 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5242 *errflags |= ZPROP_ERR_NORESTORE;
5243 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5245 *errflags |= ZPROP_ERR_NORESTORE;
5247 nvlist_free(setprops);
5248 nvlist_free(inheritprops);
5251 zfs_file_put(input_fp);
5252 nvlist_free(origrecvd);
5253 nvlist_free(origprops);
5256 error = props_error;
5263 * zc_name name of containing filesystem (unused)
5264 * zc_nvlist_src{_size} nvlist of properties to apply
5265 * zc_nvlist_conf{_size} nvlist of properties to exclude
5266 * (DATA_TYPE_BOOLEAN) and override (everything else)
5267 * zc_value name of snapshot to create
5268 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5269 * zc_cookie file descriptor to recv from
5270 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5271 * zc_guid force flag
5274 * zc_cookie number of bytes read
5275 * zc_obj zprop_errflags_t
5276 * zc_nvlist_dst{_size} error for each unapplied received property
5279 zfs_ioc_recv(zfs_cmd_t *zc)
5281 dmu_replay_record_t begin_record;
5282 nvlist_t *errors = NULL;
5283 nvlist_t *recvdprops = NULL;
5284 nvlist_t *localprops = NULL;
5285 char *origin = NULL;
5287 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5290 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5291 strchr(zc->zc_value, '@') == NULL ||
5292 strchr(zc->zc_value, '%'))
5293 return (SET_ERROR(EINVAL));
5295 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5296 tosnap = strchr(tofs, '@');
5299 if (zc->zc_nvlist_src != 0 &&
5300 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5301 zc->zc_iflags, &recvdprops)) != 0)
5304 if (zc->zc_nvlist_conf != 0 &&
5305 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5306 zc->zc_iflags, &localprops)) != 0)
5309 if (zc->zc_string[0])
5310 origin = zc->zc_string;
5312 begin_record.drr_type = DRR_BEGIN;
5313 begin_record.drr_payloadlen = 0;
5314 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5316 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5317 NULL, zc->zc_guid, B_FALSE, B_FALSE, zc->zc_cookie, &begin_record,
5318 &zc->zc_cookie, &zc->zc_obj, &errors);
5319 nvlist_free(recvdprops);
5320 nvlist_free(localprops);
5323 * Now that all props, initial and delayed, are set, report the prop
5324 * errors to the caller.
5326 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5327 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5328 put_nvlist(zc, errors) != 0)) {
5330 * Caller made zc->zc_nvlist_dst less than the minimum expected
5331 * size or supplied an invalid address.
5333 error = SET_ERROR(EINVAL);
5336 nvlist_free(errors);
5343 * "snapname" -> full name of the snapshot to create
5344 * (optional) "props" -> received properties to set (nvlist)
5345 * (optional) "localprops" -> override and exclude properties (nvlist)
5346 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5347 * "begin_record" -> non-byteswapped dmu_replay_record_t
5348 * "input_fd" -> file descriptor to read stream from (int32)
5349 * (optional) "force" -> force flag (value ignored)
5350 * (optional) "heal" -> use send stream to heal data corruption
5351 * (optional) "resumable" -> resumable flag (value ignored)
5352 * (optional) "cleanup_fd" -> unused
5353 * (optional) "action_handle" -> unused
5354 * (optional) "hidden_args" -> { "wkeydata" -> value }
5358 * "read_bytes" -> number of bytes read
5359 * "error_flags" -> zprop_errflags_t
5360 * "errors" -> error for each unapplied received property (nvlist)
5363 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5364 {"snapname", DATA_TYPE_STRING, 0},
5365 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5366 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5367 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5368 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5369 {"input_fd", DATA_TYPE_INT32, 0},
5370 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5371 {"heal", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5372 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5373 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5374 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5375 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5379 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5381 dmu_replay_record_t *begin_record;
5382 uint_t begin_record_size;
5383 nvlist_t *errors = NULL;
5384 nvlist_t *recvprops = NULL;
5385 nvlist_t *localprops = NULL;
5386 nvlist_t *hidden_args = NULL;
5388 char *origin = NULL;
5390 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5393 boolean_t resumable;
5394 uint64_t read_bytes = 0;
5395 uint64_t errflags = 0;
5399 snapname = fnvlist_lookup_string(innvl, "snapname");
5401 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5402 strchr(snapname, '@') == NULL ||
5403 strchr(snapname, '%'))
5404 return (SET_ERROR(EINVAL));
5406 (void) strlcpy(tofs, snapname, sizeof (tofs));
5407 tosnap = strchr(tofs, '@');
5410 error = nvlist_lookup_string(innvl, "origin", &origin);
5411 if (error && error != ENOENT)
5414 error = nvlist_lookup_byte_array(innvl, "begin_record",
5415 (uchar_t **)&begin_record, &begin_record_size);
5416 if (error != 0 || begin_record_size != sizeof (*begin_record))
5417 return (SET_ERROR(EINVAL));
5419 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5421 force = nvlist_exists(innvl, "force");
5422 heal = nvlist_exists(innvl, "heal");
5423 resumable = nvlist_exists(innvl, "resumable");
5425 /* we still use "props" here for backwards compatibility */
5426 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5427 if (error && error != ENOENT)
5430 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5431 if (error && error != ENOENT)
5434 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5435 if (error && error != ENOENT)
5438 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5439 hidden_args, force, heal, resumable, input_fd, begin_record,
5440 &read_bytes, &errflags, &errors);
5442 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5443 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5444 fnvlist_add_nvlist(outnvl, "errors", errors);
5446 nvlist_free(errors);
5447 nvlist_free(recvprops);
5448 nvlist_free(localprops);
5453 typedef struct dump_bytes_io {
5461 dump_bytes_cb(void *arg)
5463 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
5470 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL);
5474 dump_bytes(objset_t *os, void *buf, int len, void *arg)
5476 dump_bytes_io_t dbi;
5482 #if defined(HAVE_LARGE_STACKS)
5483 dump_bytes_cb(&dbi);
5486 * The vn_rdwr() call is performed in a taskq to ensure that there is
5487 * always enough stack space to write safely to the target filesystem.
5488 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5489 * them and they are used in vdev_file.c for a similar purpose.
5491 spa_taskq_dispatch_sync(dmu_objset_spa(os), ZIO_TYPE_FREE,
5492 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
5493 #endif /* HAVE_LARGE_STACKS */
5495 return (dbi.dbi_err);
5500 * zc_name name of snapshot to send
5501 * zc_cookie file descriptor to send stream to
5502 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5503 * zc_sendobj objsetid of snapshot to send
5504 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5505 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5506 * output size in zc_objset_type.
5507 * zc_flags lzc_send_flags
5510 * zc_objset_type estimated size, if zc_guid is set
5512 * NOTE: This is no longer the preferred interface, any new functionality
5513 * should be added to zfs_ioc_send_new() instead.
5516 zfs_ioc_send(zfs_cmd_t *zc)
5520 boolean_t estimate = (zc->zc_guid != 0);
5521 boolean_t embedok = (zc->zc_flags & 0x1);
5522 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5523 boolean_t compressok = (zc->zc_flags & 0x4);
5524 boolean_t rawok = (zc->zc_flags & 0x8);
5525 boolean_t savedok = (zc->zc_flags & 0x10);
5527 if (zc->zc_obj != 0) {
5529 dsl_dataset_t *tosnap;
5531 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5535 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5537 dsl_pool_rele(dp, FTAG);
5541 if (dsl_dir_is_clone(tosnap->ds_dir))
5543 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5544 dsl_dataset_rele(tosnap, FTAG);
5545 dsl_pool_rele(dp, FTAG);
5550 dsl_dataset_t *tosnap;
5551 dsl_dataset_t *fromsnap = NULL;
5553 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5557 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5560 dsl_pool_rele(dp, FTAG);
5564 if (zc->zc_fromobj != 0) {
5565 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5568 dsl_dataset_rele(tosnap, FTAG);
5569 dsl_pool_rele(dp, FTAG);
5574 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
5575 compressok || rawok, savedok, &zc->zc_objset_type);
5577 if (fromsnap != NULL)
5578 dsl_dataset_rele(fromsnap, FTAG);
5579 dsl_dataset_rele(tosnap, FTAG);
5580 dsl_pool_rele(dp, FTAG);
5583 dmu_send_outparams_t out = {0};
5585 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
5586 return (SET_ERROR(EBADF));
5588 off = zfs_file_off(fp);
5589 out.dso_outfunc = dump_bytes;
5591 out.dso_dryrun = B_FALSE;
5592 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5593 zc->zc_fromobj, embedok, large_block_ok, compressok,
5594 rawok, savedok, zc->zc_cookie, &off, &out);
5603 * zc_name name of snapshot on which to report progress
5604 * zc_cookie file descriptor of send stream
5607 * zc_cookie number of bytes written in send stream thus far
5608 * zc_objset_type logical size of data traversed by send thus far
5611 zfs_ioc_send_progress(zfs_cmd_t *zc)
5615 dmu_sendstatus_t *dsp = NULL;
5618 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5622 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5624 dsl_pool_rele(dp, FTAG);
5628 mutex_enter(&ds->ds_sendstream_lock);
5631 * Iterate over all the send streams currently active on this dataset.
5632 * If there's one which matches the specified file descriptor _and_ the
5633 * stream was started by the current process, return the progress of
5637 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5638 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5639 if (dsp->dss_outfd == zc->zc_cookie &&
5640 zfs_proc_is_caller(dsp->dss_proc))
5645 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
5647 /* This is the closest thing we have to atomic_read_64. */
5648 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
5650 error = SET_ERROR(ENOENT);
5653 mutex_exit(&ds->ds_sendstream_lock);
5654 dsl_dataset_rele(ds, FTAG);
5655 dsl_pool_rele(dp, FTAG);
5660 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5664 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5665 &zc->zc_inject_record);
5668 zc->zc_guid = (uint64_t)id;
5674 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5676 return (zio_clear_fault((int)zc->zc_guid));
5680 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5682 int id = (int)zc->zc_guid;
5685 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5686 &zc->zc_inject_record);
5694 zfs_ioc_error_log(zfs_cmd_t *zc)
5698 uint64_t count = zc->zc_nvlist_dst_size;
5700 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5703 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5706 zc->zc_nvlist_dst_size = count;
5708 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5710 spa_close(spa, FTAG);
5716 zfs_ioc_clear(zfs_cmd_t *zc)
5723 * On zpool clear we also fix up missing slogs
5725 mutex_enter(&spa_namespace_lock);
5726 spa = spa_lookup(zc->zc_name);
5728 mutex_exit(&spa_namespace_lock);
5729 return (SET_ERROR(EIO));
5731 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5732 /* we need to let spa_open/spa_load clear the chains */
5733 spa_set_log_state(spa, SPA_LOG_CLEAR);
5735 spa->spa_last_open_failed = 0;
5736 mutex_exit(&spa_namespace_lock);
5738 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5739 error = spa_open(zc->zc_name, &spa, FTAG);
5742 nvlist_t *config = NULL;
5744 if (zc->zc_nvlist_src == 0)
5745 return (SET_ERROR(EINVAL));
5747 if ((error = get_nvlist(zc->zc_nvlist_src,
5748 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5749 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5751 if (config != NULL) {
5754 if ((err = put_nvlist(zc, config)) != 0)
5756 nvlist_free(config);
5758 nvlist_free(policy);
5766 * If multihost is enabled, resuming I/O is unsafe as another
5767 * host may have imported the pool.
5769 if (spa_multihost(spa) && spa_suspended(spa))
5770 return (SET_ERROR(EINVAL));
5772 spa_vdev_state_enter(spa, SCL_NONE);
5774 if (zc->zc_guid == 0) {
5777 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5779 error = SET_ERROR(ENODEV);
5780 (void) spa_vdev_state_exit(spa, NULL, error);
5781 spa_close(spa, FTAG);
5786 vdev_clear(spa, vd);
5788 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5789 NULL : spa->spa_root_vdev, 0);
5792 * Resume any suspended I/Os.
5794 if (zio_resume(spa) != 0)
5795 error = SET_ERROR(EIO);
5797 spa_close(spa, FTAG);
5803 * Reopen all the vdevs associated with the pool.
5806 * "scrub_restart" -> when true and scrub is running, allow to restart
5807 * scrub as the side effect of the reopen (boolean).
5812 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5813 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
5817 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5822 boolean_t rc, scrub_restart = B_TRUE;
5825 error = nvlist_lookup_boolean_value(innvl,
5826 "scrub_restart", &rc);
5831 error = spa_open(pool, &spa, FTAG);
5835 spa_vdev_state_enter(spa, SCL_NONE);
5838 * If the scrub_restart flag is B_FALSE and a scrub is already
5839 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5840 * we don't restart the scrub as a side effect of the reopen.
5841 * Otherwise, let vdev_open() decided if a resilver is required.
5844 spa->spa_scrub_reopen = (!scrub_restart &&
5845 dsl_scan_scrubbing(spa->spa_dsl_pool));
5846 vdev_reopen(spa->spa_root_vdev);
5847 spa->spa_scrub_reopen = B_FALSE;
5849 (void) spa_vdev_state_exit(spa, NULL, 0);
5850 spa_close(spa, FTAG);
5856 * zc_name name of filesystem
5859 * zc_string name of conflicting snapshot, if there is one
5862 zfs_ioc_promote(zfs_cmd_t *zc)
5865 dsl_dataset_t *ds, *ods;
5866 char origin[ZFS_MAX_DATASET_NAME_LEN];
5870 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5871 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5872 strchr(zc->zc_name, '%'))
5873 return (SET_ERROR(EINVAL));
5875 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5879 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5881 dsl_pool_rele(dp, FTAG);
5885 if (!dsl_dir_is_clone(ds->ds_dir)) {
5886 dsl_dataset_rele(ds, FTAG);
5887 dsl_pool_rele(dp, FTAG);
5888 return (SET_ERROR(EINVAL));
5891 error = dsl_dataset_hold_obj(dp,
5892 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5894 dsl_dataset_rele(ds, FTAG);
5895 dsl_pool_rele(dp, FTAG);
5899 dsl_dataset_name(ods, origin);
5900 dsl_dataset_rele(ods, FTAG);
5901 dsl_dataset_rele(ds, FTAG);
5902 dsl_pool_rele(dp, FTAG);
5905 * We don't need to unmount *all* the origin fs's snapshots, but
5908 cp = strchr(origin, '@');
5911 (void) dmu_objset_find(origin,
5912 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5913 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5917 * Retrieve a single {user|group|project}{used|quota}@... property.
5920 * zc_name name of filesystem
5921 * zc_objset_type zfs_userquota_prop_t
5922 * zc_value domain name (eg. "S-1-234-567-89")
5923 * zc_guid RID/UID/GID
5926 * zc_cookie property value
5929 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5934 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5935 return (SET_ERROR(EINVAL));
5937 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5941 error = zfs_userspace_one(zfsvfs,
5942 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5943 zfsvfs_rele(zfsvfs, FTAG);
5950 * zc_name name of filesystem
5951 * zc_cookie zap cursor
5952 * zc_objset_type zfs_userquota_prop_t
5953 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5956 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5957 * zc_cookie zap cursor
5960 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5963 int bufsize = zc->zc_nvlist_dst_size;
5966 return (SET_ERROR(ENOMEM));
5968 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5972 void *buf = vmem_alloc(bufsize, KM_SLEEP);
5974 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5975 buf, &zc->zc_nvlist_dst_size);
5978 error = xcopyout(buf,
5979 (void *)(uintptr_t)zc->zc_nvlist_dst,
5980 zc->zc_nvlist_dst_size);
5982 vmem_free(buf, bufsize);
5983 zfsvfs_rele(zfsvfs, FTAG);
5990 * zc_name name of filesystem
5996 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
6001 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
6002 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
6004 * If userused is not enabled, it may be because the
6005 * objset needs to be closed & reopened (to grow the
6006 * objset_phys_t). Suspend/resume the fs will do that.
6008 dsl_dataset_t *ds, *newds;
6010 ds = dmu_objset_ds(zfsvfs->z_os);
6011 error = zfs_suspend_fs(zfsvfs);
6013 dmu_objset_refresh_ownership(ds, &newds,
6015 error = zfs_resume_fs(zfsvfs, newds);
6019 mutex_enter(&zfsvfs->z_os->os_upgrade_lock);
6020 if (zfsvfs->z_os->os_upgrade_id == 0) {
6021 /* clear potential error code and retry */
6022 zfsvfs->z_os->os_upgrade_status = 0;
6023 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6025 dsl_pool_config_enter(
6026 dmu_objset_pool(zfsvfs->z_os), FTAG);
6027 dmu_objset_userspace_upgrade(zfsvfs->z_os);
6028 dsl_pool_config_exit(
6029 dmu_objset_pool(zfsvfs->z_os), FTAG);
6031 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6034 taskq_wait_id(zfsvfs->z_os->os_spa->spa_upgrade_taskq,
6035 zfsvfs->z_os->os_upgrade_id);
6036 error = zfsvfs->z_os->os_upgrade_status;
6038 zfs_vfs_rele(zfsvfs);
6042 /* XXX kind of reading contents without owning */
6043 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6047 mutex_enter(&os->os_upgrade_lock);
6048 if (os->os_upgrade_id == 0) {
6049 /* clear potential error code and retry */
6050 os->os_upgrade_status = 0;
6051 mutex_exit(&os->os_upgrade_lock);
6053 dmu_objset_userspace_upgrade(os);
6055 mutex_exit(&os->os_upgrade_lock);
6058 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6060 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6061 error = os->os_upgrade_status;
6063 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT,
6071 * zc_name name of filesystem
6077 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
6082 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6086 if (dmu_objset_userobjspace_upgradable(os) ||
6087 dmu_objset_projectquota_upgradable(os)) {
6088 mutex_enter(&os->os_upgrade_lock);
6089 if (os->os_upgrade_id == 0) {
6090 /* clear potential error code and retry */
6091 os->os_upgrade_status = 0;
6092 mutex_exit(&os->os_upgrade_lock);
6094 dmu_objset_id_quota_upgrade(os);
6096 mutex_exit(&os->os_upgrade_lock);
6099 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6101 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6102 error = os->os_upgrade_status;
6104 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6107 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
6113 zfs_ioc_share(zfs_cmd_t *zc)
6115 return (SET_ERROR(ENOSYS));
6120 * zc_name name of containing filesystem
6121 * zc_obj object # beyond which we want next in-use object #
6124 * zc_obj next in-use object #
6127 zfs_ioc_next_obj(zfs_cmd_t *zc)
6129 objset_t *os = NULL;
6132 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
6136 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
6138 dmu_objset_rele(os, FTAG);
6144 * zc_name name of filesystem
6145 * zc_value prefix name for snapshot
6146 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
6149 * zc_value short name of new snapshot
6152 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
6158 zfs_file_t *fp = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
6160 return (SET_ERROR(EBADF));
6162 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
6163 (u_longlong_t)ddi_get_lbolt64());
6164 hold_name = kmem_asprintf("%%%s", zc->zc_value);
6166 int error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
6169 (void) strlcpy(zc->zc_value, snap_name,
6170 sizeof (zc->zc_value));
6171 kmem_strfree(snap_name);
6172 kmem_strfree(hold_name);
6173 zfs_onexit_fd_rele(fp);
6179 * zc_name name of "to" snapshot
6180 * zc_value name of "from" snapshot
6181 * zc_cookie file descriptor to write diff data on
6184 * dmu_diff_record_t's to the file descriptor
6187 zfs_ioc_diff(zfs_cmd_t *zc)
6193 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
6194 return (SET_ERROR(EBADF));
6196 off = zfs_file_off(fp);
6197 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
6205 zfs_ioc_smb_acl(zfs_cmd_t *zc)
6207 return (SET_ERROR(ENOTSUP));
6212 * "holds" -> { snapname -> holdname (string), ... }
6213 * (optional) "cleanup_fd" -> fd (int32)
6217 * snapname -> error value (int32)
6221 static const zfs_ioc_key_t zfs_keys_hold[] = {
6222 {"holds", DATA_TYPE_NVLIST, 0},
6223 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6227 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6232 int cleanup_fd = -1;
6235 zfs_file_t *fp = NULL;
6237 holds = fnvlist_lookup_nvlist(args, "holds");
6239 /* make sure the user didn't pass us any invalid (empty) tags */
6240 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6241 pair = nvlist_next_nvpair(holds, pair)) {
6244 error = nvpair_value_string(pair, &htag);
6246 return (SET_ERROR(error));
6248 if (strlen(htag) == 0)
6249 return (SET_ERROR(EINVAL));
6252 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6253 fp = zfs_onexit_fd_hold(cleanup_fd, &minor);
6255 return (SET_ERROR(EBADF));
6258 error = dsl_dataset_user_hold(holds, minor, errlist);
6260 ASSERT3U(minor, !=, 0);
6261 zfs_onexit_fd_rele(fp);
6263 return (SET_ERROR(error));
6267 * innvl is not used.
6270 * holdname -> time added (uint64 seconds since epoch)
6274 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6279 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6282 return (dsl_dataset_get_holds(snapname, outnvl));
6287 * snapname -> { holdname, ... }
6292 * snapname -> error value (int32)
6296 static const zfs_ioc_key_t zfs_keys_release[] = {
6297 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6301 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6304 return (dsl_dataset_user_release(holds, errlist));
6309 * zc_guid flags (ZEVENT_NONBLOCK)
6310 * zc_cleanup_fd zevent file descriptor
6313 * zc_nvlist_dst next nvlist event
6314 * zc_cookie dropped events since last get
6317 zfs_ioc_events_next(zfs_cmd_t *zc)
6320 nvlist_t *event = NULL;
6322 uint64_t dropped = 0;
6325 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6327 return (SET_ERROR(EBADF));
6330 error = zfs_zevent_next(ze, &event,
6331 &zc->zc_nvlist_dst_size, &dropped);
6332 if (event != NULL) {
6333 zc->zc_cookie = dropped;
6334 error = put_nvlist(zc, event);
6338 if (zc->zc_guid & ZEVENT_NONBLOCK)
6341 if ((error == 0) || (error != ENOENT))
6344 error = zfs_zevent_wait(ze);
6349 zfs_zevent_fd_rele(fp);
6356 * zc_cookie cleared events count
6359 zfs_ioc_events_clear(zfs_cmd_t *zc)
6363 zfs_zevent_drain_all(&count);
6364 zc->zc_cookie = count;
6371 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6372 * zc_cleanup zevent file descriptor
6375 zfs_ioc_events_seek(zfs_cmd_t *zc)
6381 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6383 return (SET_ERROR(EBADF));
6385 error = zfs_zevent_seek(ze, zc->zc_guid);
6386 zfs_zevent_fd_rele(fp);
6393 * zc_name name of later filesystem or snapshot
6394 * zc_value full name of old snapshot or bookmark
6397 * zc_cookie space in bytes
6398 * zc_objset_type compressed space in bytes
6399 * zc_perm_action uncompressed space in bytes
6402 zfs_ioc_space_written(zfs_cmd_t *zc)
6408 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6411 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6413 dsl_pool_rele(dp, FTAG);
6416 if (strchr(zc->zc_value, '#') != NULL) {
6417 zfs_bookmark_phys_t bmp;
6418 error = dsl_bookmark_lookup(dp, zc->zc_value,
6421 error = dsl_dataset_space_written_bookmark(&bmp, new,
6423 &zc->zc_objset_type, &zc->zc_perm_action);
6427 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6430 error = dsl_dataset_space_written(old, new,
6432 &zc->zc_objset_type, &zc->zc_perm_action);
6433 dsl_dataset_rele(old, FTAG);
6436 dsl_dataset_rele(new, FTAG);
6437 dsl_pool_rele(dp, FTAG);
6443 * "firstsnap" -> snapshot name
6447 * "used" -> space in bytes
6448 * "compressed" -> compressed space in bytes
6449 * "uncompressed" -> uncompressed space in bytes
6452 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6453 {"firstsnap", DATA_TYPE_STRING, 0},
6457 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6461 dsl_dataset_t *new, *old;
6463 uint64_t used, comp, uncomp;
6465 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6467 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6471 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6472 if (error == 0 && !new->ds_is_snapshot) {
6473 dsl_dataset_rele(new, FTAG);
6474 error = SET_ERROR(EINVAL);
6477 dsl_pool_rele(dp, FTAG);
6480 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6481 if (error == 0 && !old->ds_is_snapshot) {
6482 dsl_dataset_rele(old, FTAG);
6483 error = SET_ERROR(EINVAL);
6486 dsl_dataset_rele(new, FTAG);
6487 dsl_pool_rele(dp, FTAG);
6491 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6492 dsl_dataset_rele(old, FTAG);
6493 dsl_dataset_rele(new, FTAG);
6494 dsl_pool_rele(dp, FTAG);
6495 fnvlist_add_uint64(outnvl, "used", used);
6496 fnvlist_add_uint64(outnvl, "compressed", comp);
6497 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6503 * "fd" -> file descriptor to write stream to (int32)
6504 * (optional) "fromsnap" -> full snap name to send an incremental from
6505 * (optional) "largeblockok" -> (value ignored)
6506 * indicates that blocks > 128KB are permitted
6507 * (optional) "embedok" -> (value ignored)
6508 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6509 * (optional) "compressok" -> (value ignored)
6510 * presence indicates compressed DRR_WRITE records are permitted
6511 * (optional) "rawok" -> (value ignored)
6512 * presence indicates raw encrypted records should be used.
6513 * (optional) "savedok" -> (value ignored)
6514 * presence indicates we should send a partially received snapshot
6515 * (optional) "resume_object" and "resume_offset" -> (uint64)
6516 * if present, resume send stream from specified object and offset.
6517 * (optional) "redactbook" -> (string)
6518 * if present, use this bookmark's redaction list to generate a redacted
6524 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6525 {"fd", DATA_TYPE_INT32, 0},
6526 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6527 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6528 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6529 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6530 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6531 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6532 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6533 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6534 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6538 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6543 char *fromname = NULL;
6546 boolean_t largeblockok;
6548 boolean_t compressok;
6551 uint64_t resumeobj = 0;
6552 uint64_t resumeoff = 0;
6553 char *redactbook = NULL;
6555 fd = fnvlist_lookup_int32(innvl, "fd");
6557 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6559 largeblockok = nvlist_exists(innvl, "largeblockok");
6560 embedok = nvlist_exists(innvl, "embedok");
6561 compressok = nvlist_exists(innvl, "compressok");
6562 rawok = nvlist_exists(innvl, "rawok");
6563 savedok = nvlist_exists(innvl, "savedok");
6565 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6566 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6568 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6570 if ((fp = zfs_file_get(fd)) == NULL)
6571 return (SET_ERROR(EBADF));
6573 off = zfs_file_off(fp);
6575 dmu_send_outparams_t out = {0};
6576 out.dso_outfunc = dump_bytes;
6578 out.dso_dryrun = B_FALSE;
6579 error = dmu_send(snapname, fromname, embedok, largeblockok,
6580 compressok, rawok, savedok, resumeobj, resumeoff,
6581 redactbook, fd, &off, &out);
6588 send_space_sum(objset_t *os, void *buf, int len, void *arg)
6590 (void) os, (void) buf;
6591 uint64_t *size = arg;
6598 * Determine approximately how large a zfs send stream will be -- the number
6599 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6602 * (optional) "from" -> full snap or bookmark name to send an incremental
6604 * (optional) "largeblockok" -> (value ignored)
6605 * indicates that blocks > 128KB are permitted
6606 * (optional) "embedok" -> (value ignored)
6607 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6608 * (optional) "compressok" -> (value ignored)
6609 * presence indicates compressed DRR_WRITE records are permitted
6610 * (optional) "rawok" -> (value ignored)
6611 * presence indicates raw encrypted records should be used.
6612 * (optional) "resume_object" and "resume_offset" -> (uint64)
6613 * if present, resume send stream from specified object and offset.
6614 * (optional) "fd" -> file descriptor to use as a cookie for progress
6619 * "space" -> bytes of space (uint64)
6622 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6623 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6624 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6625 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6626 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6627 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6628 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6629 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6630 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6631 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6632 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6633 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
6637 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6640 dsl_dataset_t *tosnap;
6641 dsl_dataset_t *fromsnap = NULL;
6643 char *fromname = NULL;
6644 char *redactlist_book = NULL;
6645 boolean_t largeblockok;
6647 boolean_t compressok;
6651 boolean_t full_estimate = B_FALSE;
6652 uint64_t resumeobj = 0;
6653 uint64_t resumeoff = 0;
6654 uint64_t resume_bytes = 0;
6656 zfs_bookmark_phys_t zbm = {0};
6658 error = dsl_pool_hold(snapname, FTAG, &dp);
6662 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6664 dsl_pool_rele(dp, FTAG);
6667 (void) nvlist_lookup_int32(innvl, "fd", &fd);
6669 largeblockok = nvlist_exists(innvl, "largeblockok");
6670 embedok = nvlist_exists(innvl, "embedok");
6671 compressok = nvlist_exists(innvl, "compressok");
6672 rawok = nvlist_exists(innvl, "rawok");
6673 savedok = nvlist_exists(innvl, "savedok");
6674 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
6675 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
6676 &redactlist_book) == 0);
6678 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6679 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6680 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
6683 full_estimate = B_TRUE;
6685 if (strchr(fromname, '#')) {
6686 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
6689 * dsl_bookmark_lookup() will fail with EXDEV if
6690 * the from-bookmark and tosnap are at the same txg.
6691 * However, it's valid to do a send (and therefore,
6692 * a send estimate) from and to the same time point,
6693 * if the bookmark is redacted (the incremental send
6694 * can change what's redacted on the target). In
6695 * this case, dsl_bookmark_lookup() fills in zbm
6696 * but returns EXDEV. Ignore this error.
6698 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
6700 dsl_dataset_phys(tosnap)->ds_guid)
6704 dsl_dataset_rele(tosnap, FTAG);
6705 dsl_pool_rele(dp, FTAG);
6708 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
6709 ZBM_FLAG_HAS_FBN)) {
6710 full_estimate = B_TRUE;
6712 } else if (strchr(fromname, '@')) {
6713 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6715 dsl_dataset_rele(tosnap, FTAG);
6716 dsl_pool_rele(dp, FTAG);
6720 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
6721 full_estimate = B_TRUE;
6722 dsl_dataset_rele(fromsnap, FTAG);
6726 * from is not properly formatted as a snapshot or
6729 dsl_dataset_rele(tosnap, FTAG);
6730 dsl_pool_rele(dp, FTAG);
6731 return (SET_ERROR(EINVAL));
6735 if (full_estimate) {
6736 dmu_send_outparams_t out = {0};
6738 out.dso_outfunc = send_space_sum;
6739 out.dso_arg = &space;
6740 out.dso_dryrun = B_TRUE;
6742 * We have to release these holds so dmu_send can take them. It
6743 * will do all the error checking we need.
6745 dsl_dataset_rele(tosnap, FTAG);
6746 dsl_pool_rele(dp, FTAG);
6747 error = dmu_send(snapname, fromname, embedok, largeblockok,
6748 compressok, rawok, savedok, resumeobj, resumeoff,
6749 redactlist_book, fd, &off, &out);
6751 error = dmu_send_estimate_fast(tosnap, fromsnap,
6752 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
6753 compressok || rawok, savedok, &space);
6754 space -= resume_bytes;
6755 if (fromsnap != NULL)
6756 dsl_dataset_rele(fromsnap, FTAG);
6757 dsl_dataset_rele(tosnap, FTAG);
6758 dsl_pool_rele(dp, FTAG);
6761 fnvlist_add_uint64(outnvl, "space", space);
6767 * Sync the currently open TXG to disk for the specified pool.
6768 * This is somewhat similar to 'zfs_sync()'.
6769 * For cases that do not result in error this ioctl will wait for
6770 * the currently open TXG to commit before returning back to the caller.
6773 * "force" -> when true, force uberblock update even if there is no dirty data.
6774 * In addition this will cause the vdev configuration to be written
6775 * out including updating the zpool cache file. (boolean_t)
6780 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6781 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6785 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6789 boolean_t rc, force = B_FALSE;
6792 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6796 err = nvlist_lookup_boolean_value(innvl, "force", &rc);
6802 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6803 vdev_config_dirty(spa->spa_root_vdev);
6804 spa_config_exit(spa, SCL_CONFIG, FTAG);
6806 txg_wait_synced(spa_get_dsl(spa), 0);
6808 spa_close(spa, FTAG);
6814 * Load a user's wrapping key into the kernel.
6816 * "hidden_args" -> { "wkeydata" -> value }
6817 * raw uint8_t array of encryption wrapping key data (32 bytes)
6818 * (optional) "noop" -> (value ignored)
6819 * presence indicated key should only be verified, not loaded
6822 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6823 {"hidden_args", DATA_TYPE_NVLIST, 0},
6824 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6828 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6832 dsl_crypto_params_t *dcp = NULL;
6833 nvlist_t *hidden_args;
6834 boolean_t noop = nvlist_exists(innvl, "noop");
6836 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6837 ret = SET_ERROR(EINVAL);
6841 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6843 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6848 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6852 dsl_crypto_params_free(dcp, noop);
6857 dsl_crypto_params_free(dcp, B_TRUE);
6862 * Unload a user's wrapping key from the kernel.
6863 * Both innvl and outnvl are unused.
6865 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6870 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6872 (void) innvl, (void) outnvl;
6875 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6876 ret = (SET_ERROR(EINVAL));
6880 ret = spa_keystore_unload_wkey(dsname);
6889 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6890 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6891 * here to change how the key is derived in userspace.
6894 * "hidden_args" (optional) -> { "wkeydata" -> value }
6895 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6896 * "props" (optional) -> { prop -> value }
6901 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6902 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6903 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6904 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6908 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6912 uint64_t cmd = DCP_CMD_NONE;
6913 dsl_crypto_params_t *dcp = NULL;
6914 nvlist_t *args = NULL, *hidden_args = NULL;
6916 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6917 ret = (SET_ERROR(EINVAL));
6921 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6922 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6923 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6925 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6929 ret = spa_keystore_change_key(dsname, dcp);
6933 dsl_crypto_params_free(dcp, B_FALSE);
6938 dsl_crypto_params_free(dcp, B_TRUE);
6942 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6945 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6946 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6947 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6949 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6951 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6952 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6953 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6954 ASSERT3P(vec->zvec_func, ==, NULL);
6956 vec->zvec_legacy_func = func;
6957 vec->zvec_secpolicy = secpolicy;
6958 vec->zvec_namecheck = namecheck;
6959 vec->zvec_allow_log = log_history;
6960 vec->zvec_pool_check = pool_check;
6964 * See the block comment at the beginning of this file for details on
6965 * each argument to this function.
6968 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6969 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6970 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6971 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6973 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6975 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6976 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6977 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6978 ASSERT3P(vec->zvec_func, ==, NULL);
6980 /* if we are logging, the name must be valid */
6981 ASSERT(!allow_log || namecheck != NO_NAME);
6983 vec->zvec_name = name;
6984 vec->zvec_func = func;
6985 vec->zvec_secpolicy = secpolicy;
6986 vec->zvec_namecheck = namecheck;
6987 vec->zvec_pool_check = pool_check;
6988 vec->zvec_smush_outnvlist = smush_outnvlist;
6989 vec->zvec_allow_log = allow_log;
6990 vec->zvec_nvl_keys = nvl_keys;
6991 vec->zvec_nvl_key_count = num_keys;
6995 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6996 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6997 zfs_ioc_poolcheck_t pool_check)
6999 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7000 POOL_NAME, log_history, pool_check);
7004 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7005 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
7007 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7008 DATASET_NAME, B_FALSE, pool_check);
7012 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7014 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
7015 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7019 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7020 zfs_secpolicy_func_t *secpolicy)
7022 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7023 NO_NAME, B_FALSE, POOL_CHECK_NONE);
7027 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
7028 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
7030 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7031 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
7035 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7037 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
7038 zfs_secpolicy_read);
7042 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7043 zfs_secpolicy_func_t *secpolicy)
7045 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7046 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7050 zfs_ioctl_init(void)
7052 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
7053 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
7054 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7055 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
7057 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
7058 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
7059 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7060 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
7062 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
7063 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
7064 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7065 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
7067 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
7068 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
7069 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7070 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
7072 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
7073 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
7074 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7075 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
7077 zfs_ioctl_register("create", ZFS_IOC_CREATE,
7078 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
7079 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7080 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
7082 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
7083 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
7084 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7085 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
7087 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
7088 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
7089 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7090 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
7092 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
7093 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
7094 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7095 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
7097 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
7098 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
7099 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7100 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
7101 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
7102 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
7103 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7104 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
7106 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
7107 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
7108 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7109 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
7111 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
7112 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
7113 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7114 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
7116 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
7117 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
7118 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7119 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
7121 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
7122 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
7123 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7124 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
7126 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
7127 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
7128 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
7129 ARRAY_SIZE(zfs_keys_get_bookmark_props));
7131 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
7132 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
7134 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7135 zfs_keys_destroy_bookmarks,
7136 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
7138 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
7139 zfs_ioc_recv_new, zfs_secpolicy_recv, DATASET_NAME,
7140 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7141 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
7142 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
7143 zfs_ioc_load_key, zfs_secpolicy_load_key,
7144 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7145 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
7146 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
7147 zfs_ioc_unload_key, zfs_secpolicy_load_key,
7148 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7149 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
7150 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
7151 zfs_ioc_change_key, zfs_secpolicy_change_key,
7152 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
7153 B_TRUE, B_TRUE, zfs_keys_change_key,
7154 ARRAY_SIZE(zfs_keys_change_key));
7156 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
7157 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
7158 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7159 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
7160 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
7161 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
7162 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
7164 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
7165 zfs_ioc_channel_program, zfs_secpolicy_config,
7166 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
7167 B_TRUE, zfs_keys_channel_program,
7168 ARRAY_SIZE(zfs_keys_channel_program));
7170 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
7171 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
7172 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7173 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
7175 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
7176 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
7177 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7178 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
7180 zfs_ioctl_register("zpool_discard_checkpoint",
7181 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
7182 zfs_secpolicy_config, POOL_NAME,
7183 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7184 zfs_keys_pool_discard_checkpoint,
7185 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
7187 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
7188 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
7189 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7190 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
7192 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
7193 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
7194 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7195 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
7197 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
7198 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
7199 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7200 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
7202 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS,
7203 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME,
7204 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7205 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait));
7207 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
7208 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
7209 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7210 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
7212 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
7213 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
7214 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
7215 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
7217 zfs_ioctl_register("zpool_vdev_get_props", ZFS_IOC_VDEV_GET_PROPS,
7218 zfs_ioc_vdev_get_props, zfs_secpolicy_read, POOL_NAME,
7219 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_vdev_get_props,
7220 ARRAY_SIZE(zfs_keys_vdev_get_props));
7222 zfs_ioctl_register("zpool_vdev_set_props", ZFS_IOC_VDEV_SET_PROPS,
7223 zfs_ioc_vdev_set_props, zfs_secpolicy_config, POOL_NAME,
7224 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7225 zfs_keys_vdev_set_props, ARRAY_SIZE(zfs_keys_vdev_set_props));
7227 /* IOCTLS that use the legacy function signature */
7229 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7230 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7232 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7233 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7234 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7236 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7237 zfs_ioc_pool_upgrade);
7238 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7240 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7241 zfs_ioc_vdev_remove);
7242 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7243 zfs_ioc_vdev_set_state);
7244 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7245 zfs_ioc_vdev_attach);
7246 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7247 zfs_ioc_vdev_detach);
7248 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7249 zfs_ioc_vdev_setpath);
7250 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7251 zfs_ioc_vdev_setfru);
7252 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7253 zfs_ioc_pool_set_props);
7254 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7255 zfs_ioc_vdev_split);
7256 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7257 zfs_ioc_pool_reguid);
7259 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7260 zfs_ioc_pool_configs, zfs_secpolicy_none);
7261 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7262 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7263 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7264 zfs_ioc_inject_fault, zfs_secpolicy_inject);
7265 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7266 zfs_ioc_clear_fault, zfs_secpolicy_inject);
7267 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
7268 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
7271 * pool destroy, and export don't log the history as part of
7272 * zfsdev_ioctl, but rather zfs_ioc_pool_export
7273 * does the logging of those commands.
7275 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
7276 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7277 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
7278 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7280 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7281 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7282 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
7283 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7285 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7286 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7287 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7288 zfs_ioc_dsobj_to_dsname,
7289 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7290 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7291 zfs_ioc_pool_get_history,
7292 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7294 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7295 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7297 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
7298 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
7300 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7301 zfs_ioc_space_written);
7302 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7303 zfs_ioc_objset_recvd_props);
7304 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7306 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7308 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7309 zfs_ioc_objset_stats);
7310 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7311 zfs_ioc_objset_zplprops);
7312 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7313 zfs_ioc_dataset_list_next);
7314 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7315 zfs_ioc_snapshot_list_next);
7316 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7317 zfs_ioc_send_progress);
7319 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7320 zfs_ioc_diff, zfs_secpolicy_diff);
7321 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7322 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7323 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7324 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7325 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7326 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7327 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7328 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7329 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7330 zfs_ioc_send, zfs_secpolicy_send);
7332 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7333 zfs_secpolicy_none);
7334 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7335 zfs_secpolicy_destroy);
7336 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7337 zfs_secpolicy_rename);
7338 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7339 zfs_secpolicy_recv);
7340 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7341 zfs_secpolicy_promote);
7342 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7343 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7344 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7345 zfs_secpolicy_set_fsacl);
7347 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7348 zfs_secpolicy_share, POOL_CHECK_NONE);
7349 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7350 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7351 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7352 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7353 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7354 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7355 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7356 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7358 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7359 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7360 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7361 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7362 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7363 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7365 zfs_ioctl_init_os();
7369 * Verify that for non-legacy ioctls the input nvlist
7370 * pairs match against the expected input.
7372 * Possible errors are:
7373 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7374 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7375 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7378 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7380 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7381 boolean_t required_keys_found = B_FALSE;
7384 * examine each input pair
7386 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7387 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7388 char *name = nvpair_name(pair);
7389 data_type_t type = nvpair_type(pair);
7390 boolean_t identified = B_FALSE;
7393 * check pair against the documented names and type
7395 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7396 /* if not a wild card name, check for an exact match */
7397 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7398 strcmp(nvl_keys[k].zkey_name, name) != 0)
7401 identified = B_TRUE;
7403 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7404 nvl_keys[k].zkey_type != type) {
7405 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7408 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7411 required_keys_found = B_TRUE;
7415 /* allow an 'optional' key, everything else is invalid */
7417 (strcmp(name, "optional") != 0 ||
7418 type != DATA_TYPE_NVLIST)) {
7419 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7423 /* verify that all required keys were found */
7424 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7425 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7428 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7429 /* at least one non-optional key is expected here */
7430 if (!required_keys_found)
7431 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7435 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7436 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7443 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7444 zfs_ioc_poolcheck_t check)
7449 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7450 type == ENTITY_NAME);
7452 if (check & POOL_CHECK_NONE)
7455 error = spa_open(name, &spa, FTAG);
7457 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7458 error = SET_ERROR(EAGAIN);
7459 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7460 error = SET_ERROR(EROFS);
7461 spa_close(spa, FTAG);
7467 zfsdev_getminor(zfs_file_t *fp, minor_t *minorp)
7469 zfsdev_state_t *zs, *fpd;
7471 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7473 fpd = zfs_file_private(fp);
7475 return (SET_ERROR(EBADF));
7477 mutex_enter(&zfsdev_state_lock);
7479 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7481 if (zs->zs_minor == -1)
7485 *minorp = fpd->zs_minor;
7486 mutex_exit(&zfsdev_state_lock);
7491 mutex_exit(&zfsdev_state_lock);
7493 return (SET_ERROR(EBADF));
7497 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
7501 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7502 if (zs->zs_minor == minor) {
7506 return (zs->zs_onexit);
7508 return (zs->zs_zevent);
7519 * Find a free minor number. The zfsdev_state_list is expected to
7520 * be short since it is only a list of currently open file handles.
7523 zfsdev_minor_alloc(void)
7525 static minor_t last_minor = 0;
7528 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7530 for (m = last_minor + 1; m != last_minor; m++) {
7531 if (m > ZFSDEV_MAX_MINOR)
7533 if (zfsdev_get_state(m, ZST_ALL) == NULL) {
7543 zfsdev_state_init(void *priv)
7545 zfsdev_state_t *zs, *zsprev = NULL;
7547 boolean_t newzs = B_FALSE;
7549 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7551 minor = zfsdev_minor_alloc();
7553 return (SET_ERROR(ENXIO));
7555 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7556 if (zs->zs_minor == -1)
7562 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7566 zfsdev_private_set_state(priv, zs);
7568 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7569 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7572 * In order to provide for lock-free concurrent read access
7573 * to the minor list in zfsdev_get_state(), new entries
7574 * must be completely written before linking them into the
7575 * list whereas existing entries are already linked; the last
7576 * operation must be updating zs_minor (from -1 to the new
7580 zs->zs_minor = minor;
7582 zsprev->zs_next = zs;
7585 zs->zs_minor = minor;
7592 zfsdev_state_destroy(void *priv)
7594 zfsdev_state_t *zs = zfsdev_private_get_state(priv);
7597 ASSERT3S(zs->zs_minor, >, 0);
7600 * The last reference to this zfsdev file descriptor is being dropped.
7601 * We don't have to worry about lookup grabbing this state object, and
7602 * zfsdev_state_init() will not try to reuse this object until it is
7603 * invalidated by setting zs_minor to -1. Invalidation must be done
7604 * last, with a memory barrier to ensure ordering. This lets us avoid
7605 * taking the global zfsdev state lock around destruction.
7607 zfs_onexit_destroy(zs->zs_onexit);
7608 zfs_zevent_destroy(zs->zs_zevent);
7609 zs->zs_onexit = NULL;
7610 zs->zs_zevent = NULL;
7616 zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
7619 const zfs_ioc_vec_t *vec;
7620 char *saved_poolname = NULL;
7621 uint64_t max_nvlist_src_size;
7622 size_t saved_poolname_len = 0;
7623 nvlist_t *innvl = NULL;
7624 fstrans_cookie_t cookie;
7625 hrtime_t start_time = gethrtime();
7629 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7630 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7632 vec = &zfs_ioc_vec[vecnum];
7635 * The registered ioctl list may be sparse, verify that either
7636 * a normal or legacy handler are registered.
7638 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7639 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7641 zc->zc_iflags = flag & FKIOCTL;
7642 max_nvlist_src_size = zfs_max_nvlist_src_size_os();
7643 if (zc->zc_nvlist_src_size > max_nvlist_src_size) {
7645 * Make sure the user doesn't pass in an insane value for
7646 * zc_nvlist_src_size. We have to check, since we will end
7647 * up allocating that much memory inside of get_nvlist(). This
7648 * prevents a nefarious user from allocating tons of kernel
7651 * Also, we return EINVAL instead of ENOMEM here. The reason
7652 * being that returning ENOMEM from an ioctl() has a special
7653 * connotation; that the user's size value is too small and
7654 * needs to be expanded to hold the nvlist. See
7655 * zcmd_expand_dst_nvlist() for details.
7657 error = SET_ERROR(EINVAL); /* User's size too big */
7659 } else if (zc->zc_nvlist_src_size != 0) {
7660 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7661 zc->zc_iflags, &innvl);
7667 * Ensure that all pool/dataset names are valid before we pass down to
7670 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7671 switch (vec->zvec_namecheck) {
7673 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7674 error = SET_ERROR(EINVAL);
7676 error = pool_status_check(zc->zc_name,
7677 vec->zvec_namecheck, vec->zvec_pool_check);
7681 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7682 error = SET_ERROR(EINVAL);
7684 error = pool_status_check(zc->zc_name,
7685 vec->zvec_namecheck, vec->zvec_pool_check);
7689 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7690 error = SET_ERROR(EINVAL);
7692 error = pool_status_check(zc->zc_name,
7693 vec->zvec_namecheck, vec->zvec_pool_check);
7701 * Ensure that all input pairs are valid before we pass them down
7702 * to the lower layers.
7704 * The vectored functions can use fnvlist_lookup_{type} for any
7705 * required pairs since zfs_check_input_nvpairs() confirmed that
7706 * they exist and are of the correct type.
7708 if (error == 0 && vec->zvec_func != NULL) {
7709 error = zfs_check_input_nvpairs(innvl, vec);
7715 cookie = spl_fstrans_mark();
7716 error = vec->zvec_secpolicy(zc, innvl, CRED());
7717 spl_fstrans_unmark(cookie);
7723 /* legacy ioctls can modify zc_name */
7725 * Can't use kmem_strdup() as we might truncate the string and
7726 * kmem_strfree() would then free with incorrect size.
7728 saved_poolname_len = strlen(zc->zc_name) + 1;
7729 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP);
7731 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len);
7732 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7734 if (vec->zvec_func != NULL) {
7738 nvlist_t *lognv = NULL;
7740 ASSERT(vec->zvec_legacy_func == NULL);
7743 * Add the innvl to the lognv before calling the func,
7744 * in case the func changes the innvl.
7746 if (vec->zvec_allow_log) {
7747 lognv = fnvlist_alloc();
7748 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7750 if (!nvlist_empty(innvl)) {
7751 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7756 outnvl = fnvlist_alloc();
7757 cookie = spl_fstrans_mark();
7758 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7759 spl_fstrans_unmark(cookie);
7762 * Some commands can partially execute, modify state, and still
7763 * return an error. In these cases, attempt to record what
7767 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7768 vec->zvec_allow_log &&
7769 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7770 if (!nvlist_empty(outnvl)) {
7771 size_t out_size = fnvlist_size(outnvl);
7772 if (out_size > zfs_history_output_max) {
7773 fnvlist_add_int64(lognv,
7774 ZPOOL_HIST_OUTPUT_SIZE, out_size);
7776 fnvlist_add_nvlist(lognv,
7777 ZPOOL_HIST_OUTPUT_NVL, outnvl);
7781 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7784 fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS,
7785 gethrtime() - start_time);
7786 (void) spa_history_log_nvl(spa, lognv);
7787 spa_close(spa, FTAG);
7789 fnvlist_free(lognv);
7791 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7793 if (vec->zvec_smush_outnvlist) {
7794 smusherror = nvlist_smush(outnvl,
7795 zc->zc_nvlist_dst_size);
7797 if (smusherror == 0)
7798 puterror = put_nvlist(zc, outnvl);
7804 nvlist_free(outnvl);
7806 cookie = spl_fstrans_mark();
7807 error = vec->zvec_legacy_func(zc);
7808 spl_fstrans_unmark(cookie);
7813 if (error == 0 && vec->zvec_allow_log) {
7814 char *s = tsd_get(zfs_allow_log_key);
7817 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname));
7819 if (saved_poolname != NULL)
7820 kmem_free(saved_poolname, saved_poolname_len);
7830 if ((error = zvol_init()) != 0)
7833 spa_init(SPA_MODE_READ | SPA_MODE_WRITE);
7838 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7839 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7840 zfsdev_state_list->zs_minor = -1;
7842 if ((error = zfsdev_attach()) != 0)
7845 tsd_create(&zfs_fsyncer_key, NULL);
7846 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7847 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7861 zfsdev_state_t *zs, *zsnext = NULL;
7865 mutex_destroy(&zfsdev_state_lock);
7867 for (zs = zfsdev_state_list; zs != NULL; zs = zsnext) {
7868 zsnext = zs->zs_next;
7870 zfs_onexit_destroy(zs->zs_onexit);
7872 zfs_zevent_destroy(zs->zs_zevent);
7873 kmem_free(zs, sizeof (zfsdev_state_t));
7876 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */
7881 tsd_destroy(&zfs_fsyncer_key);
7882 tsd_destroy(&rrw_tsd_key);
7883 tsd_destroy(&zfs_allow_log_key);
7886 ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, U64, ZMOD_RW,
7887 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls");
7889 ZFS_MODULE_PARAM(zfs, zfs_, history_output_max, U64, ZMOD_RW,
7890 "Maximum size in bytes of ZFS ioctl output that will be logged");