4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2018 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.
45 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
46 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
48 * There are two ways that we handle ioctls: the legacy way where almost
49 * all of the logic is in the ioctl callback, and the new way where most
50 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
52 * Non-legacy ioctls should be registered by calling
53 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
54 * from userland by lzc_ioctl().
56 * The registration arguments are as follows:
59 * The name of the ioctl. This is used for history logging. If the
60 * ioctl returns successfully (the callback returns 0), and allow_log
61 * is true, then a history log entry will be recorded with the input &
62 * output nvlists. The log entry can be printed with "zpool history -i".
65 * The ioctl request number, which userland will pass to ioctl(2).
66 * We want newer versions of libzfs and libzfs_core to run against
67 * existing zfs kernel modules (i.e. a deferred reboot after an update).
68 * Therefore the ioctl numbers cannot change from release to release.
70 * zfs_secpolicy_func_t *secpolicy
71 * This function will be called before the zfs_ioc_func_t, to
72 * determine if this operation is permitted. It should return EPERM
73 * on failure, and 0 on success. Checks include determining if the
74 * dataset is visible in this zone, and if the user has either all
75 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
76 * to do this operation on this dataset with "zfs allow".
78 * zfs_ioc_namecheck_t namecheck
79 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
80 * name, a dataset name, or nothing. If the name is not well-formed,
81 * the ioctl will fail and the callback will not be called.
82 * Therefore, the callback can assume that the name is well-formed
83 * (e.g. is null-terminated, doesn't have more than one '@' character,
84 * doesn't have invalid characters).
86 * zfs_ioc_poolcheck_t pool_check
87 * This specifies requirements on the pool state. If the pool does
88 * not meet them (is suspended or is readonly), the ioctl will fail
89 * and the callback will not be called. If any checks are specified
90 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
91 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
92 * POOL_CHECK_READONLY).
94 * zfs_ioc_key_t *nvl_keys
95 * The list of expected/allowable innvl input keys. This list is used
96 * to validate the nvlist input to the ioctl.
98 * boolean_t smush_outnvlist
99 * If smush_outnvlist is true, then the output is presumed to be a
100 * list of errors, and it will be "smushed" down to fit into the
101 * caller's buffer, by removing some entries and replacing them with a
102 * single "N_MORE_ERRORS" entry indicating how many were removed. See
103 * nvlist_smush() for details. If smush_outnvlist is false, and the
104 * outnvlist does not fit into the userland-provided buffer, then the
105 * ioctl will fail with ENOMEM.
107 * zfs_ioc_func_t *func
108 * The callback function that will perform the operation.
110 * The callback should return 0 on success, or an error number on
111 * failure. If the function fails, the userland ioctl will return -1,
112 * and errno will be set to the callback's return value. The callback
113 * will be called with the following arguments:
116 * The name of the pool or dataset to operate on, from
117 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
118 * expected type (pool, dataset, or none).
121 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
122 * NULL if no input nvlist was provided. Changes to this nvlist are
123 * ignored. If the input nvlist could not be deserialized, the
124 * ioctl will fail and the callback will not be called.
127 * The output nvlist, initially empty. The callback can fill it in,
128 * and it will be returned to userland by serializing it into
129 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
130 * fails (e.g. because the caller didn't supply a large enough
131 * buffer), then the overall ioctl will fail. See the
132 * 'smush_nvlist' argument above for additional behaviors.
134 * There are two typical uses of the output nvlist:
135 * - To return state, e.g. property values. In this case,
136 * smush_outnvlist should be false. If the buffer was not large
137 * enough, the caller will reallocate a larger buffer and try
140 * - To return multiple errors from an ioctl which makes on-disk
141 * changes. In this case, smush_outnvlist should be true.
142 * Ioctls which make on-disk modifications should generally not
143 * use the outnvl if they succeed, because the caller can not
144 * distinguish between the operation failing, and
145 * deserialization failing.
147 * IOCTL Interface Errors
149 * The following ioctl input errors can be returned:
150 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
151 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
152 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
153 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
156 #include <sys/types.h>
157 #include <sys/param.h>
158 #include <sys/errno.h>
160 #include <sys/file.h>
161 #include <sys/kmem.h>
162 #include <sys/cmn_err.h>
163 #include <sys/stat.h>
164 #include <sys/zfs_ioctl.h>
165 #include <sys/zfs_vfsops.h>
166 #include <sys/zfs_znode.h>
169 #include <sys/spa_impl.h>
170 #include <sys/vdev.h>
171 #include <sys/vdev_impl.h>
173 #include <sys/dsl_dir.h>
174 #include <sys/dsl_dataset.h>
175 #include <sys/dsl_prop.h>
176 #include <sys/dsl_deleg.h>
177 #include <sys/dmu_objset.h>
178 #include <sys/dmu_impl.h>
179 #include <sys/dmu_redact.h>
180 #include <sys/dmu_tx.h>
181 #include <sys/sunddi.h>
182 #include <sys/policy.h>
183 #include <sys/zone.h>
184 #include <sys/nvpair.h>
185 #include <sys/pathname.h>
187 #include <sys/fs/zfs.h>
188 #include <sys/zfs_ctldir.h>
189 #include <sys/zfs_dir.h>
190 #include <sys/zfs_onexit.h>
191 #include <sys/zvol.h>
192 #include <sys/dsl_scan.h>
193 #include <sys/fm/util.h>
194 #include <sys/dsl_crypt.h>
196 #include <sys/dmu_recv.h>
197 #include <sys/dmu_send.h>
198 #include <sys/dmu_recv.h>
199 #include <sys/dsl_destroy.h>
200 #include <sys/dsl_bookmark.h>
201 #include <sys/dsl_userhold.h>
202 #include <sys/zfeature.h>
204 #include <sys/zio_checksum.h>
205 #include <sys/vdev_removal.h>
206 #include <sys/zfs_sysfs.h>
207 #include <sys/vdev_impl.h>
208 #include <sys/vdev_initialize.h>
209 #include <sys/vdev_trim.h>
211 #include <linux/miscdevice.h>
212 #include <linux/slab.h>
214 #include "zfs_namecheck.h"
215 #include "zfs_prop.h"
216 #include "zfs_deleg.h"
217 #include "zfs_comutil.h"
219 #include <sys/lua/lua.h>
220 #include <sys/lua/lauxlib.h>
223 * Limit maximum nvlist size. We don't want users passing in insane values
224 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
226 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
228 kmutex_t zfsdev_state_lock;
229 zfsdev_state_t *zfsdev_state_list;
231 extern void zfs_init(void);
232 extern void zfs_fini(void);
234 uint_t zfs_fsyncer_key;
235 extern uint_t rrw_tsd_key;
236 static uint_t zfs_allow_log_key;
238 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
239 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
240 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
243 * IOC Keys are used to document and validate user->kernel interface inputs.
244 * See zfs_keys_recv_new for an example declaration. Any key name that is not
245 * listed will be rejected as input.
247 * The keyname 'optional' is always allowed, and must be an nvlist if present.
248 * Arguments which older kernels can safely ignore can be placed under the
251 * When adding new keys to an existing ioc for new functionality, consider:
252 * - adding an entry into zfs_sysfs.c zfs_features[] list
253 * - updating the libzfs_input_check.c test utility
255 * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
256 * for the expected name (bookmark, snapshot, property, etc) but there
257 * is no validation in the preflight zfs_check_input_nvpairs() check.
260 ZK_OPTIONAL = 1 << 0, /* pair is optional */
261 ZK_WILDCARDLIST = 1 << 1, /* one or more unspecified key names */
264 /* DATA_TYPE_ANY is used when zkey_type can vary. */
265 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
267 typedef struct zfs_ioc_key {
268 const char *zkey_name;
269 data_type_t zkey_type;
270 ioc_key_flag_t zkey_flags;
278 } zfs_ioc_namecheck_t;
281 POOL_CHECK_NONE = 1 << 0,
282 POOL_CHECK_SUSPENDED = 1 << 1,
283 POOL_CHECK_READONLY = 1 << 2,
284 } zfs_ioc_poolcheck_t;
286 typedef struct zfs_ioc_vec {
287 zfs_ioc_legacy_func_t *zvec_legacy_func;
288 zfs_ioc_func_t *zvec_func;
289 zfs_secpolicy_func_t *zvec_secpolicy;
290 zfs_ioc_namecheck_t zvec_namecheck;
291 boolean_t zvec_allow_log;
292 zfs_ioc_poolcheck_t zvec_pool_check;
293 boolean_t zvec_smush_outnvlist;
294 const char *zvec_name;
295 const zfs_ioc_key_t *zvec_nvl_keys;
296 size_t zvec_nvl_key_count;
299 /* This array is indexed by zfs_userquota_prop_t */
300 static const char *userquota_perms[] = {
301 ZFS_DELEG_PERM_USERUSED,
302 ZFS_DELEG_PERM_USERQUOTA,
303 ZFS_DELEG_PERM_GROUPUSED,
304 ZFS_DELEG_PERM_GROUPQUOTA,
305 ZFS_DELEG_PERM_USEROBJUSED,
306 ZFS_DELEG_PERM_USEROBJQUOTA,
307 ZFS_DELEG_PERM_GROUPOBJUSED,
308 ZFS_DELEG_PERM_GROUPOBJQUOTA,
309 ZFS_DELEG_PERM_PROJECTUSED,
310 ZFS_DELEG_PERM_PROJECTQUOTA,
311 ZFS_DELEG_PERM_PROJECTOBJUSED,
312 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
315 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
316 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
317 static int zfs_check_settable(const char *name, nvpair_t *property,
319 static int zfs_check_clearable(char *dataset, nvlist_t *props,
321 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
323 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
324 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
327 history_str_free(char *buf)
329 kmem_free(buf, HIS_MAX_RECORD_LEN);
333 history_str_get(zfs_cmd_t *zc)
337 if (zc->zc_history == 0)
340 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
341 if (copyinstr((void *)(uintptr_t)zc->zc_history,
342 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
343 history_str_free(buf);
347 buf[HIS_MAX_RECORD_LEN -1] = '\0';
353 * Check to see if the named dataset is currently defined as bootable
356 zfs_is_bootfs(const char *name)
360 if (dmu_objset_hold(name, FTAG, &os) == 0) {
362 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
363 dmu_objset_rele(os, FTAG);
370 * Return non-zero if the spa version is less than requested version.
373 zfs_earlier_version(const char *name, int version)
377 if (spa_open(name, &spa, FTAG) == 0) {
378 if (spa_version(spa) < version) {
379 spa_close(spa, FTAG);
382 spa_close(spa, FTAG);
388 * Return TRUE if the ZPL version is less than requested version.
391 zpl_earlier_version(const char *name, int version)
394 boolean_t rc = B_TRUE;
396 if (dmu_objset_hold(name, FTAG, &os) == 0) {
399 if (dmu_objset_type(os) != DMU_OST_ZFS) {
400 dmu_objset_rele(os, FTAG);
403 /* XXX reading from non-owned objset */
404 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
405 rc = zplversion < version;
406 dmu_objset_rele(os, FTAG);
412 zfs_log_history(zfs_cmd_t *zc)
417 if ((buf = history_str_get(zc)) == NULL)
420 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
421 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
422 (void) spa_history_log(spa, buf);
423 spa_close(spa, FTAG);
425 history_str_free(buf);
429 * Policy for top-level read operations (list pools). Requires no privileges,
430 * and can be used in the local zone, as there is no associated dataset.
434 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
440 * Policy for dataset read operations (list children, get statistics). Requires
441 * no privileges, but must be visible in the local zone.
445 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
447 if (INGLOBALZONE(curproc) ||
448 zone_dataset_visible(zc->zc_name, NULL))
451 return (SET_ERROR(ENOENT));
455 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
460 * The dataset must be visible by this zone -- check this first
461 * so they don't see EPERM on something they shouldn't know about.
463 if (!INGLOBALZONE(curproc) &&
464 !zone_dataset_visible(dataset, &writable))
465 return (SET_ERROR(ENOENT));
467 if (INGLOBALZONE(curproc)) {
469 * If the fs is zoned, only root can access it from the
472 if (secpolicy_zfs(cr) && zoned)
473 return (SET_ERROR(EPERM));
476 * If we are in a local zone, the 'zoned' property must be set.
479 return (SET_ERROR(EPERM));
481 /* must be writable by this zone */
483 return (SET_ERROR(EPERM));
489 zfs_dozonecheck(const char *dataset, cred_t *cr)
493 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
494 return (SET_ERROR(ENOENT));
496 return (zfs_dozonecheck_impl(dataset, zoned, cr));
500 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
504 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
505 return (SET_ERROR(ENOENT));
507 return (zfs_dozonecheck_impl(dataset, zoned, cr));
511 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
512 const char *perm, cred_t *cr)
516 error = zfs_dozonecheck_ds(name, ds, cr);
518 error = secpolicy_zfs(cr);
520 error = dsl_deleg_access_impl(ds, perm, cr);
526 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
533 * First do a quick check for root in the global zone, which
534 * is allowed to do all write_perms. This ensures that zfs_ioc_*
535 * will get to handle nonexistent datasets.
537 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
540 error = dsl_pool_hold(name, FTAG, &dp);
544 error = dsl_dataset_hold(dp, name, FTAG, &ds);
546 dsl_pool_rele(dp, FTAG);
550 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
552 dsl_dataset_rele(ds, FTAG);
553 dsl_pool_rele(dp, FTAG);
558 * Policy for setting the security label property.
560 * Returns 0 for success, non-zero for access and other errors.
563 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
566 char ds_hexsl[MAXNAMELEN];
567 bslabel_t ds_sl, new_sl;
568 boolean_t new_default = FALSE;
570 int needed_priv = -1;
573 /* First get the existing dataset label. */
574 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
575 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
577 return (SET_ERROR(EPERM));
579 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
582 /* The label must be translatable */
583 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
584 return (SET_ERROR(EINVAL));
587 * In a non-global zone, disallow attempts to set a label that
588 * doesn't match that of the zone; otherwise no other checks
591 if (!INGLOBALZONE(curproc)) {
592 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
593 return (SET_ERROR(EPERM));
598 * For global-zone datasets (i.e., those whose zoned property is
599 * "off", verify that the specified new label is valid for the
602 if (dsl_prop_get_integer(name,
603 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
604 return (SET_ERROR(EPERM));
606 if (zfs_check_global_label(name, strval) != 0)
607 return (SET_ERROR(EPERM));
611 * If the existing dataset label is nondefault, check if the
612 * dataset is mounted (label cannot be changed while mounted).
613 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
614 * mounted (or isn't a dataset, doesn't exist, ...).
616 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
618 static char *setsl_tag = "setsl_tag";
621 * Try to own the dataset; abort if there is any error,
622 * (e.g., already mounted, in use, or other error).
624 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
627 return (SET_ERROR(EPERM));
629 dmu_objset_disown(os, B_TRUE, setsl_tag);
632 needed_priv = PRIV_FILE_DOWNGRADE_SL;
636 if (hexstr_to_label(strval, &new_sl) != 0)
637 return (SET_ERROR(EPERM));
639 if (blstrictdom(&ds_sl, &new_sl))
640 needed_priv = PRIV_FILE_DOWNGRADE_SL;
641 else if (blstrictdom(&new_sl, &ds_sl))
642 needed_priv = PRIV_FILE_UPGRADE_SL;
644 /* dataset currently has a default label */
646 needed_priv = PRIV_FILE_UPGRADE_SL;
650 if (needed_priv != -1)
651 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
654 return (SET_ERROR(ENOTSUP));
655 #endif /* HAVE_MLSLABEL */
659 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
665 * Check permissions for special properties.
672 * Disallow setting of 'zoned' from within a local zone.
674 if (!INGLOBALZONE(curproc))
675 return (SET_ERROR(EPERM));
679 case ZFS_PROP_FILESYSTEM_LIMIT:
680 case ZFS_PROP_SNAPSHOT_LIMIT:
681 if (!INGLOBALZONE(curproc)) {
683 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
685 * Unprivileged users are allowed to modify the
686 * limit on things *under* (ie. contained by)
687 * the thing they own.
689 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
691 return (SET_ERROR(EPERM));
692 if (!zoned || strlen(dsname) <= strlen(setpoint))
693 return (SET_ERROR(EPERM));
697 case ZFS_PROP_MLSLABEL:
698 if (!is_system_labeled())
699 return (SET_ERROR(EPERM));
701 if (nvpair_value_string(propval, &strval) == 0) {
704 err = zfs_set_slabel_policy(dsname, strval, CRED());
711 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
716 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
720 error = zfs_dozonecheck(zc->zc_name, cr);
725 * permission to set permissions will be evaluated later in
726 * dsl_deleg_can_allow()
733 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
735 return (zfs_secpolicy_write_perms(zc->zc_name,
736 ZFS_DELEG_PERM_ROLLBACK, cr));
741 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
749 * Generate the current snapshot name from the given objsetid, then
750 * use that name for the secpolicy/zone checks.
752 cp = strchr(zc->zc_name, '@');
754 return (SET_ERROR(EINVAL));
755 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
759 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
761 dsl_pool_rele(dp, FTAG);
765 dsl_dataset_name(ds, zc->zc_name);
767 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
768 ZFS_DELEG_PERM_SEND, cr);
769 dsl_dataset_rele(ds, FTAG);
770 dsl_pool_rele(dp, FTAG);
777 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
779 return (zfs_secpolicy_write_perms(zc->zc_name,
780 ZFS_DELEG_PERM_SEND, cr));
784 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
786 return (SET_ERROR(ENOTSUP));
790 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
792 return (SET_ERROR(ENOTSUP));
796 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
801 * Remove the @bla or /bla from the end of the name to get the parent.
803 (void) strncpy(parent, datasetname, parentsize);
804 cp = strrchr(parent, '@');
808 cp = strrchr(parent, '/');
810 return (SET_ERROR(ENOENT));
818 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
822 if ((error = zfs_secpolicy_write_perms(name,
823 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
826 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
831 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
833 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
837 * Destroying snapshots with delegated permissions requires
838 * descendant mount and destroy permissions.
842 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
845 nvpair_t *pair, *nextpair;
848 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
850 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
852 nextpair = nvlist_next_nvpair(snaps, pair);
853 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
854 if (error == ENOENT) {
856 * Ignore any snapshots that don't exist (we consider
857 * them "already destroyed"). Remove the name from the
858 * nvl here in case the snapshot is created between
859 * now and when we try to destroy it (in which case
860 * we don't want to destroy it since we haven't
861 * checked for permission).
863 fnvlist_remove_nvpair(snaps, pair);
874 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
876 char parentname[ZFS_MAX_DATASET_NAME_LEN];
879 if ((error = zfs_secpolicy_write_perms(from,
880 ZFS_DELEG_PERM_RENAME, cr)) != 0)
883 if ((error = zfs_secpolicy_write_perms(from,
884 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
887 if ((error = zfs_get_parent(to, parentname,
888 sizeof (parentname))) != 0)
891 if ((error = zfs_secpolicy_write_perms(parentname,
892 ZFS_DELEG_PERM_CREATE, cr)) != 0)
895 if ((error = zfs_secpolicy_write_perms(parentname,
896 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
904 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
906 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
911 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
914 dsl_dataset_t *clone;
917 error = zfs_secpolicy_write_perms(zc->zc_name,
918 ZFS_DELEG_PERM_PROMOTE, cr);
922 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
926 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
929 char parentname[ZFS_MAX_DATASET_NAME_LEN];
930 dsl_dataset_t *origin = NULL;
934 error = dsl_dataset_hold_obj(dd->dd_pool,
935 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
937 dsl_dataset_rele(clone, FTAG);
938 dsl_pool_rele(dp, FTAG);
942 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
943 ZFS_DELEG_PERM_MOUNT, cr);
945 dsl_dataset_name(origin, parentname);
947 error = zfs_secpolicy_write_perms_ds(parentname, origin,
948 ZFS_DELEG_PERM_PROMOTE, cr);
950 dsl_dataset_rele(clone, FTAG);
951 dsl_dataset_rele(origin, FTAG);
953 dsl_pool_rele(dp, FTAG);
959 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
963 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
964 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
967 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
968 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
971 return (zfs_secpolicy_write_perms(zc->zc_name,
972 ZFS_DELEG_PERM_CREATE, cr));
977 zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
979 return (zfs_secpolicy_recv(zc, innvl, cr));
983 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
985 return (zfs_secpolicy_write_perms(name,
986 ZFS_DELEG_PERM_SNAPSHOT, cr));
990 * Check for permission to create each snapshot in the nvlist.
994 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1000 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
1002 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1003 pair = nvlist_next_nvpair(snaps, pair)) {
1004 char *name = nvpair_name(pair);
1005 char *atp = strchr(name, '@');
1008 error = SET_ERROR(EINVAL);
1012 error = zfs_secpolicy_snapshot_perms(name, cr);
1021 * Check for permission to create each bookmark in the nvlist.
1025 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1029 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1030 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1031 char *name = nvpair_name(pair);
1032 char *hashp = strchr(name, '#');
1034 if (hashp == NULL) {
1035 error = SET_ERROR(EINVAL);
1039 error = zfs_secpolicy_write_perms(name,
1040 ZFS_DELEG_PERM_BOOKMARK, cr);
1050 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1052 nvpair_t *pair, *nextpair;
1055 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1057 char *name = nvpair_name(pair);
1058 char *hashp = strchr(name, '#');
1059 nextpair = nvlist_next_nvpair(innvl, pair);
1061 if (hashp == NULL) {
1062 error = SET_ERROR(EINVAL);
1067 error = zfs_secpolicy_write_perms(name,
1068 ZFS_DELEG_PERM_DESTROY, cr);
1070 if (error == ENOENT) {
1072 * Ignore any filesystems that don't exist (we consider
1073 * their bookmarks "already destroyed"). Remove
1074 * the name from the nvl here in case the filesystem
1075 * is created between now and when we try to destroy
1076 * the bookmark (in which case we don't want to
1077 * destroy it since we haven't checked for permission).
1079 fnvlist_remove_nvpair(innvl, pair);
1091 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1094 * Even root must have a proper TSD so that we know what pool
1097 if (tsd_get(zfs_allow_log_key) == NULL)
1098 return (SET_ERROR(EPERM));
1103 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1105 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1109 if ((error = zfs_get_parent(zc->zc_name, parentname,
1110 sizeof (parentname))) != 0)
1113 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1114 (error = zfs_secpolicy_write_perms(origin,
1115 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1118 if ((error = zfs_secpolicy_write_perms(parentname,
1119 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1122 return (zfs_secpolicy_write_perms(parentname,
1123 ZFS_DELEG_PERM_MOUNT, cr));
1127 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1128 * SYS_CONFIG privilege, which is not available in a local zone.
1132 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1134 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1135 return (SET_ERROR(EPERM));
1141 * Policy for object to name lookups.
1145 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1149 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1152 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1157 * Policy for fault injection. Requires all privileges.
1161 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1163 return (secpolicy_zinject(cr));
1168 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1170 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1172 if (prop == ZPROP_INVAL) {
1173 if (!zfs_prop_user(zc->zc_value))
1174 return (SET_ERROR(EINVAL));
1175 return (zfs_secpolicy_write_perms(zc->zc_name,
1176 ZFS_DELEG_PERM_USERPROP, cr));
1178 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1184 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1186 int err = zfs_secpolicy_read(zc, innvl, cr);
1190 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1191 return (SET_ERROR(EINVAL));
1193 if (zc->zc_value[0] == 0) {
1195 * They are asking about a posix uid/gid. If it's
1196 * themself, allow it.
1198 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1199 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1200 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1201 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1202 if (zc->zc_guid == crgetuid(cr))
1204 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1205 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1206 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1207 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1208 if (groupmember(zc->zc_guid, cr))
1211 /* else is for project quota/used */
1214 return (zfs_secpolicy_write_perms(zc->zc_name,
1215 userquota_perms[zc->zc_objset_type], cr));
1219 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1221 int err = zfs_secpolicy_read(zc, innvl, cr);
1225 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1226 return (SET_ERROR(EINVAL));
1228 return (zfs_secpolicy_write_perms(zc->zc_name,
1229 userquota_perms[zc->zc_objset_type], cr));
1234 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1236 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1242 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1248 holds = fnvlist_lookup_nvlist(innvl, "holds");
1250 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1251 pair = nvlist_next_nvpair(holds, pair)) {
1252 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1253 error = dmu_fsname(nvpair_name(pair), fsname);
1256 error = zfs_secpolicy_write_perms(fsname,
1257 ZFS_DELEG_PERM_HOLD, cr);
1266 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1271 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1272 pair = nvlist_next_nvpair(innvl, pair)) {
1273 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1274 error = dmu_fsname(nvpair_name(pair), fsname);
1277 error = zfs_secpolicy_write_perms(fsname,
1278 ZFS_DELEG_PERM_RELEASE, cr);
1286 * Policy for allowing temporary snapshots to be taken or released
1289 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1292 * A temporary snapshot is the same as a snapshot,
1293 * hold, destroy and release all rolled into one.
1294 * Delegated diff alone is sufficient that we allow this.
1298 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1299 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1302 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1304 if (innvl != NULL) {
1306 error = zfs_secpolicy_hold(zc, innvl, cr);
1308 error = zfs_secpolicy_release(zc, innvl, cr);
1310 error = zfs_secpolicy_destroy(zc, innvl, cr);
1316 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1318 return (zfs_secpolicy_write_perms(zc->zc_name,
1319 ZFS_DELEG_PERM_LOAD_KEY, cr));
1323 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1325 return (zfs_secpolicy_write_perms(zc->zc_name,
1326 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1330 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1333 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1337 nvlist_t *list = NULL;
1340 * Read in and unpack the user-supplied nvlist.
1343 return (SET_ERROR(EINVAL));
1345 packed = vmem_alloc(size, KM_SLEEP);
1347 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1349 vmem_free(packed, size);
1350 return (SET_ERROR(EFAULT));
1353 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1354 vmem_free(packed, size);
1358 vmem_free(packed, size);
1365 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1366 * Entries will be removed from the end of the nvlist, and one int32 entry
1367 * named "N_MORE_ERRORS" will be added indicating how many entries were
1371 nvlist_smush(nvlist_t *errors, size_t max)
1375 size = fnvlist_size(errors);
1378 nvpair_t *more_errors;
1382 return (SET_ERROR(ENOMEM));
1384 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1385 more_errors = nvlist_prev_nvpair(errors, NULL);
1388 nvpair_t *pair = nvlist_prev_nvpair(errors,
1390 fnvlist_remove_nvpair(errors, pair);
1392 size = fnvlist_size(errors);
1393 } while (size > max);
1395 fnvlist_remove_nvpair(errors, more_errors);
1396 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1397 ASSERT3U(fnvlist_size(errors), <=, max);
1404 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1406 char *packed = NULL;
1410 size = fnvlist_size(nvl);
1412 if (size > zc->zc_nvlist_dst_size) {
1413 error = SET_ERROR(ENOMEM);
1415 packed = fnvlist_pack(nvl, &size);
1416 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1417 size, zc->zc_iflags) != 0)
1418 error = SET_ERROR(EFAULT);
1419 fnvlist_pack_free(packed, size);
1422 zc->zc_nvlist_dst_size = size;
1423 zc->zc_nvlist_dst_filled = B_TRUE;
1428 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1431 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1432 return (SET_ERROR(EINVAL));
1435 mutex_enter(&os->os_user_ptr_lock);
1436 *zfvp = dmu_objset_get_user(os);
1437 /* bump s_active only when non-zero to prevent umount race */
1438 if (*zfvp == NULL || (*zfvp)->z_sb == NULL ||
1439 !atomic_inc_not_zero(&((*zfvp)->z_sb->s_active))) {
1440 error = SET_ERROR(ESRCH);
1442 mutex_exit(&os->os_user_ptr_lock);
1447 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1452 error = dmu_objset_hold(dsname, FTAG, &os);
1456 error = getzfsvfs_impl(os, zfvp);
1457 dmu_objset_rele(os, FTAG);
1462 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1463 * case its z_sb will be NULL, and it will be opened as the owner.
1464 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1465 * which prevents all inode ops from running.
1468 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1472 if (getzfsvfs(name, zfvp) != 0)
1473 error = zfsvfs_create(name, B_FALSE, zfvp);
1475 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1477 if ((*zfvp)->z_unmounted) {
1479 * XXX we could probably try again, since the unmounting
1480 * thread should be just about to disassociate the
1481 * objset from the zfsvfs.
1483 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1484 return (SET_ERROR(EBUSY));
1491 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1493 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1496 deactivate_super(zfsvfs->z_sb);
1498 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1499 zfsvfs_free(zfsvfs);
1504 zfs_ioc_pool_create(zfs_cmd_t *zc)
1507 nvlist_t *config, *props = NULL;
1508 nvlist_t *rootprops = NULL;
1509 nvlist_t *zplprops = NULL;
1510 dsl_crypto_params_t *dcp = NULL;
1511 char *spa_name = zc->zc_name;
1512 boolean_t unload_wkey = B_TRUE;
1514 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1515 zc->zc_iflags, &config)))
1518 if (zc->zc_nvlist_src_size != 0 && (error =
1519 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1520 zc->zc_iflags, &props))) {
1521 nvlist_free(config);
1526 nvlist_t *nvl = NULL;
1527 nvlist_t *hidden_args = NULL;
1528 uint64_t version = SPA_VERSION;
1531 (void) nvlist_lookup_uint64(props,
1532 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1533 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1534 error = SET_ERROR(EINVAL);
1535 goto pool_props_bad;
1537 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1539 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1541 goto pool_props_bad;
1542 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1545 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1547 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1548 rootprops, hidden_args, &dcp);
1550 goto pool_props_bad;
1551 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1553 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1554 error = zfs_fill_zplprops_root(version, rootprops,
1557 goto pool_props_bad;
1559 if (nvlist_lookup_string(props,
1560 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1564 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1567 * Set the remaining root properties
1569 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1570 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1571 (void) spa_destroy(spa_name);
1572 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1576 nvlist_free(rootprops);
1577 nvlist_free(zplprops);
1578 nvlist_free(config);
1580 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1586 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1589 zfs_log_history(zc);
1590 error = spa_destroy(zc->zc_name);
1596 zfs_ioc_pool_import(zfs_cmd_t *zc)
1598 nvlist_t *config, *props = NULL;
1602 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1603 zc->zc_iflags, &config)) != 0)
1606 if (zc->zc_nvlist_src_size != 0 && (error =
1607 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1608 zc->zc_iflags, &props))) {
1609 nvlist_free(config);
1613 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1614 guid != zc->zc_guid)
1615 error = SET_ERROR(EINVAL);
1617 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1619 if (zc->zc_nvlist_dst != 0) {
1622 if ((err = put_nvlist(zc, config)) != 0)
1626 nvlist_free(config);
1633 zfs_ioc_pool_export(zfs_cmd_t *zc)
1636 boolean_t force = (boolean_t)zc->zc_cookie;
1637 boolean_t hardforce = (boolean_t)zc->zc_guid;
1639 zfs_log_history(zc);
1640 error = spa_export(zc->zc_name, NULL, force, hardforce);
1646 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1651 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1652 return (SET_ERROR(EEXIST));
1654 error = put_nvlist(zc, configs);
1656 nvlist_free(configs);
1663 * zc_name name of the pool
1666 * zc_cookie real errno
1667 * zc_nvlist_dst config nvlist
1668 * zc_nvlist_dst_size size of config nvlist
1671 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1677 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1678 sizeof (zc->zc_value));
1680 if (config != NULL) {
1681 ret = put_nvlist(zc, config);
1682 nvlist_free(config);
1685 * The config may be present even if 'error' is non-zero.
1686 * In this case we return success, and preserve the real errno
1689 zc->zc_cookie = error;
1698 * Try to import the given pool, returning pool stats as appropriate so that
1699 * user land knows which devices are available and overall pool health.
1702 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1704 nvlist_t *tryconfig, *config = NULL;
1707 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1708 zc->zc_iflags, &tryconfig)) != 0)
1711 config = spa_tryimport(tryconfig);
1713 nvlist_free(tryconfig);
1716 return (SET_ERROR(EINVAL));
1718 error = put_nvlist(zc, config);
1719 nvlist_free(config);
1726 * zc_name name of the pool
1727 * zc_cookie scan func (pool_scan_func_t)
1728 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1731 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1736 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1737 return (SET_ERROR(EINVAL));
1739 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1742 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1743 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1744 else if (zc->zc_cookie == POOL_SCAN_NONE)
1745 error = spa_scan_stop(spa);
1747 error = spa_scan(spa, zc->zc_cookie);
1749 spa_close(spa, FTAG);
1755 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1760 error = spa_open(zc->zc_name, &spa, FTAG);
1763 spa_close(spa, FTAG);
1769 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1774 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1777 if (zc->zc_cookie < spa_version(spa) ||
1778 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1779 spa_close(spa, FTAG);
1780 return (SET_ERROR(EINVAL));
1783 spa_upgrade(spa, zc->zc_cookie);
1784 spa_close(spa, FTAG);
1790 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1797 if ((size = zc->zc_history_len) == 0)
1798 return (SET_ERROR(EINVAL));
1800 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1803 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1804 spa_close(spa, FTAG);
1805 return (SET_ERROR(ENOTSUP));
1808 hist_buf = vmem_alloc(size, KM_SLEEP);
1809 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1810 &zc->zc_history_len, hist_buf)) == 0) {
1811 error = ddi_copyout(hist_buf,
1812 (void *)(uintptr_t)zc->zc_history,
1813 zc->zc_history_len, zc->zc_iflags);
1816 spa_close(spa, FTAG);
1817 vmem_free(hist_buf, size);
1822 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1827 error = spa_open(zc->zc_name, &spa, FTAG);
1829 error = spa_change_guid(spa);
1830 spa_close(spa, FTAG);
1836 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1838 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1843 * zc_name name of filesystem
1844 * zc_obj object to find
1847 * zc_value name of object
1850 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1855 /* XXX reading from objset not owned */
1856 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1859 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1860 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1861 return (SET_ERROR(EINVAL));
1863 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1864 sizeof (zc->zc_value));
1865 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1872 * zc_name name of filesystem
1873 * zc_obj object to find
1876 * zc_stat stats on object
1877 * zc_value path to object
1880 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1885 /* XXX reading from objset not owned */
1886 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1889 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1890 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1891 return (SET_ERROR(EINVAL));
1893 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1894 sizeof (zc->zc_value));
1895 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1901 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1907 error = spa_open(zc->zc_name, &spa, FTAG);
1911 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1912 zc->zc_iflags, &config);
1914 error = spa_vdev_add(spa, config);
1915 nvlist_free(config);
1917 spa_close(spa, FTAG);
1923 * zc_name name of the pool
1924 * zc_guid guid of vdev to remove
1925 * zc_cookie cancel removal
1928 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1933 error = spa_open(zc->zc_name, &spa, FTAG);
1936 if (zc->zc_cookie != 0) {
1937 error = spa_vdev_remove_cancel(spa);
1939 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1941 spa_close(spa, FTAG);
1946 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1950 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1952 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1954 switch (zc->zc_cookie) {
1955 case VDEV_STATE_ONLINE:
1956 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1959 case VDEV_STATE_OFFLINE:
1960 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1963 case VDEV_STATE_FAULTED:
1964 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1965 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1966 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1967 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1969 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1972 case VDEV_STATE_DEGRADED:
1973 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1974 zc->zc_obj != VDEV_AUX_EXTERNAL)
1975 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1977 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1981 error = SET_ERROR(EINVAL);
1983 zc->zc_cookie = newstate;
1984 spa_close(spa, FTAG);
1989 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1992 int replacing = zc->zc_cookie;
1996 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1999 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2000 zc->zc_iflags, &config)) == 0) {
2001 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2002 nvlist_free(config);
2005 spa_close(spa, FTAG);
2010 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2015 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2018 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2020 spa_close(spa, FTAG);
2025 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2028 nvlist_t *config, *props = NULL;
2030 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2032 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2035 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2036 zc->zc_iflags, &config))) {
2037 spa_close(spa, FTAG);
2041 if (zc->zc_nvlist_src_size != 0 && (error =
2042 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2043 zc->zc_iflags, &props))) {
2044 spa_close(spa, FTAG);
2045 nvlist_free(config);
2049 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2051 spa_close(spa, FTAG);
2053 nvlist_free(config);
2060 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2063 char *path = zc->zc_value;
2064 uint64_t guid = zc->zc_guid;
2067 error = spa_open(zc->zc_name, &spa, FTAG);
2071 error = spa_vdev_setpath(spa, guid, path);
2072 spa_close(spa, FTAG);
2077 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2080 char *fru = zc->zc_value;
2081 uint64_t guid = zc->zc_guid;
2084 error = spa_open(zc->zc_name, &spa, FTAG);
2088 error = spa_vdev_setfru(spa, guid, fru);
2089 spa_close(spa, FTAG);
2094 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2099 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2101 if (zc->zc_nvlist_dst != 0 &&
2102 (error = dsl_prop_get_all(os, &nv)) == 0) {
2103 dmu_objset_stats(os, nv);
2105 * NB: zvol_get_stats() will read the objset contents,
2106 * which we aren't supposed to do with a
2107 * DS_MODE_USER hold, because it could be
2108 * inconsistent. So this is a bit of a workaround...
2109 * XXX reading with out owning
2111 if (!zc->zc_objset_stats.dds_inconsistent &&
2112 dmu_objset_type(os) == DMU_OST_ZVOL) {
2113 error = zvol_get_stats(os, nv);
2121 error = put_nvlist(zc, nv);
2130 * zc_name name of filesystem
2131 * zc_nvlist_dst_size size of buffer for property nvlist
2134 * zc_objset_stats stats
2135 * zc_nvlist_dst property nvlist
2136 * zc_nvlist_dst_size size of property nvlist
2139 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2144 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2146 error = zfs_ioc_objset_stats_impl(zc, os);
2147 dmu_objset_rele(os, FTAG);
2155 * zc_name name of filesystem
2156 * zc_nvlist_dst_size size of buffer for property nvlist
2159 * zc_nvlist_dst received property nvlist
2160 * zc_nvlist_dst_size size of received property nvlist
2162 * Gets received properties (distinct from local properties on or after
2163 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2164 * local property values.
2167 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2173 * Without this check, we would return local property values if the
2174 * caller has not already received properties on or after
2175 * SPA_VERSION_RECVD_PROPS.
2177 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2178 return (SET_ERROR(ENOTSUP));
2180 if (zc->zc_nvlist_dst != 0 &&
2181 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2182 error = put_nvlist(zc, nv);
2190 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2196 * zfs_get_zplprop() will either find a value or give us
2197 * the default value (if there is one).
2199 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2201 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2207 * zc_name name of filesystem
2208 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2211 * zc_nvlist_dst zpl property nvlist
2212 * zc_nvlist_dst_size size of zpl property nvlist
2215 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2220 /* XXX reading without owning */
2221 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2224 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2227 * NB: nvl_add_zplprop() will read the objset contents,
2228 * which we aren't supposed to do with a DS_MODE_USER
2229 * hold, because it could be inconsistent.
2231 if (zc->zc_nvlist_dst != 0 &&
2232 !zc->zc_objset_stats.dds_inconsistent &&
2233 dmu_objset_type(os) == DMU_OST_ZFS) {
2236 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2237 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2238 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2239 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2240 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2241 err = put_nvlist(zc, nv);
2244 err = SET_ERROR(ENOENT);
2246 dmu_objset_rele(os, FTAG);
2252 * zc_name name of filesystem
2253 * zc_cookie zap cursor
2254 * zc_nvlist_dst_size size of buffer for property nvlist
2257 * zc_name name of next filesystem
2258 * zc_cookie zap cursor
2259 * zc_objset_stats stats
2260 * zc_nvlist_dst property nvlist
2261 * zc_nvlist_dst_size size of property nvlist
2264 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2269 size_t orig_len = strlen(zc->zc_name);
2272 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2273 if (error == ENOENT)
2274 error = SET_ERROR(ESRCH);
2278 p = strrchr(zc->zc_name, '/');
2279 if (p == NULL || p[1] != '\0')
2280 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2281 p = zc->zc_name + strlen(zc->zc_name);
2284 error = dmu_dir_list_next(os,
2285 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2286 NULL, &zc->zc_cookie);
2287 if (error == ENOENT)
2288 error = SET_ERROR(ESRCH);
2289 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2290 dmu_objset_rele(os, FTAG);
2293 * If it's an internal dataset (ie. with a '$' in its name),
2294 * don't try to get stats for it, otherwise we'll return ENOENT.
2296 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2297 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2298 if (error == ENOENT) {
2299 /* We lost a race with destroy, get the next one. */
2300 zc->zc_name[orig_len] = '\0';
2309 * zc_name name of filesystem
2310 * zc_cookie zap cursor
2311 * zc_nvlist_src iteration range nvlist
2312 * zc_nvlist_src_size size of iteration range nvlist
2315 * zc_name name of next snapshot
2316 * zc_objset_stats stats
2317 * zc_nvlist_dst property nvlist
2318 * zc_nvlist_dst_size size of property nvlist
2321 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2324 objset_t *os, *ossnap;
2326 uint64_t min_txg = 0, max_txg = 0;
2328 if (zc->zc_nvlist_src_size != 0) {
2329 nvlist_t *props = NULL;
2330 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2331 zc->zc_iflags, &props);
2334 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2336 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2341 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2343 return (error == ENOENT ? ESRCH : error);
2347 * A dataset name of maximum length cannot have any snapshots,
2348 * so exit immediately.
2350 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2351 ZFS_MAX_DATASET_NAME_LEN) {
2352 dmu_objset_rele(os, FTAG);
2353 return (SET_ERROR(ESRCH));
2356 while (error == 0) {
2357 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2358 error = SET_ERROR(EINTR);
2362 error = dmu_snapshot_list_next(os,
2363 sizeof (zc->zc_name) - strlen(zc->zc_name),
2364 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2365 &zc->zc_cookie, NULL);
2366 if (error == ENOENT) {
2367 error = SET_ERROR(ESRCH);
2369 } else if (error != 0) {
2373 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2378 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2379 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2380 dsl_dataset_rele(ds, FTAG);
2381 /* undo snapshot name append */
2382 *(strchr(zc->zc_name, '@') + 1) = '\0';
2387 if (zc->zc_simple) {
2388 dsl_dataset_rele(ds, FTAG);
2392 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2393 dsl_dataset_rele(ds, FTAG);
2396 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2397 dsl_dataset_rele(ds, FTAG);
2400 dsl_dataset_rele(ds, FTAG);
2404 dmu_objset_rele(os, FTAG);
2405 /* if we failed, undo the @ that we tacked on to zc_name */
2407 *strchr(zc->zc_name, '@') = '\0';
2412 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2414 const char *propname = nvpair_name(pair);
2416 unsigned int vallen;
2419 zfs_userquota_prop_t type;
2425 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2427 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2428 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2430 return (SET_ERROR(EINVAL));
2434 * A correctly constructed propname is encoded as
2435 * userquota@<rid>-<domain>.
2437 if ((dash = strchr(propname, '-')) == NULL ||
2438 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2440 return (SET_ERROR(EINVAL));
2447 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2449 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2450 zfsvfs_rele(zfsvfs, FTAG);
2457 * If the named property is one that has a special function to set its value,
2458 * return 0 on success and a positive error code on failure; otherwise if it is
2459 * not one of the special properties handled by this function, return -1.
2461 * XXX: It would be better for callers of the property interface if we handled
2462 * these special cases in dsl_prop.c (in the dsl layer).
2465 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2468 const char *propname = nvpair_name(pair);
2469 zfs_prop_t prop = zfs_name_to_prop(propname);
2470 uint64_t intval = 0;
2471 char *strval = NULL;
2474 if (prop == ZPROP_INVAL) {
2475 if (zfs_prop_userquota(propname))
2476 return (zfs_prop_set_userquota(dsname, pair));
2480 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2482 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2483 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2487 /* all special properties are numeric except for keylocation */
2488 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2489 strval = fnvpair_value_string(pair);
2491 intval = fnvpair_value_uint64(pair);
2495 case ZFS_PROP_QUOTA:
2496 err = dsl_dir_set_quota(dsname, source, intval);
2498 case ZFS_PROP_REFQUOTA:
2499 err = dsl_dataset_set_refquota(dsname, source, intval);
2501 case ZFS_PROP_FILESYSTEM_LIMIT:
2502 case ZFS_PROP_SNAPSHOT_LIMIT:
2503 if (intval == UINT64_MAX) {
2504 /* clearing the limit, just do it */
2507 err = dsl_dir_activate_fs_ss_limit(dsname);
2510 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2511 * default path to set the value in the nvlist.
2516 case ZFS_PROP_KEYLOCATION:
2517 err = dsl_crypto_can_set_keylocation(dsname, strval);
2520 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2521 * default path to set the value in the nvlist.
2526 case ZFS_PROP_RESERVATION:
2527 err = dsl_dir_set_reservation(dsname, source, intval);
2529 case ZFS_PROP_REFRESERVATION:
2530 err = dsl_dataset_set_refreservation(dsname, source, intval);
2532 case ZFS_PROP_VOLSIZE:
2533 err = zvol_set_volsize(dsname, intval);
2535 case ZFS_PROP_SNAPDEV:
2536 err = zvol_set_snapdev(dsname, source, intval);
2538 case ZFS_PROP_VOLMODE:
2539 err = zvol_set_volmode(dsname, source, intval);
2541 case ZFS_PROP_VERSION:
2545 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2548 err = zfs_set_version(zfsvfs, intval);
2549 zfsvfs_rele(zfsvfs, FTAG);
2551 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2554 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2555 (void) strcpy(zc->zc_name, dsname);
2556 (void) zfs_ioc_userspace_upgrade(zc);
2557 (void) zfs_ioc_id_quota_upgrade(zc);
2558 kmem_free(zc, sizeof (zfs_cmd_t));
2570 * This function is best effort. If it fails to set any of the given properties,
2571 * it continues to set as many as it can and returns the last error
2572 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2573 * with the list of names of all the properties that failed along with the
2574 * corresponding error numbers.
2576 * If every property is set successfully, zero is returned and errlist is not
2580 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2589 nvlist_t *genericnvl = fnvlist_alloc();
2590 nvlist_t *retrynvl = fnvlist_alloc();
2593 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2594 const char *propname = nvpair_name(pair);
2595 zfs_prop_t prop = zfs_name_to_prop(propname);
2598 /* decode the property value */
2600 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2602 attrs = fnvpair_value_nvlist(pair);
2603 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2605 err = SET_ERROR(EINVAL);
2608 /* Validate value type */
2609 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2610 /* inherited properties are expected to be booleans */
2611 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2612 err = SET_ERROR(EINVAL);
2613 } else if (err == 0 && prop == ZPROP_INVAL) {
2614 if (zfs_prop_user(propname)) {
2615 if (nvpair_type(propval) != DATA_TYPE_STRING)
2616 err = SET_ERROR(EINVAL);
2617 } else if (zfs_prop_userquota(propname)) {
2618 if (nvpair_type(propval) !=
2619 DATA_TYPE_UINT64_ARRAY)
2620 err = SET_ERROR(EINVAL);
2622 err = SET_ERROR(EINVAL);
2624 } else if (err == 0) {
2625 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2626 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2627 err = SET_ERROR(EINVAL);
2628 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2631 intval = fnvpair_value_uint64(propval);
2633 switch (zfs_prop_get_type(prop)) {
2634 case PROP_TYPE_NUMBER:
2636 case PROP_TYPE_STRING:
2637 err = SET_ERROR(EINVAL);
2639 case PROP_TYPE_INDEX:
2640 if (zfs_prop_index_to_string(prop,
2641 intval, &unused) != 0)
2642 err = SET_ERROR(EINVAL);
2646 "unknown property type");
2649 err = SET_ERROR(EINVAL);
2653 /* Validate permissions */
2655 err = zfs_check_settable(dsname, pair, CRED());
2658 if (source == ZPROP_SRC_INHERITED)
2659 err = -1; /* does not need special handling */
2661 err = zfs_prop_set_special(dsname, source,
2665 * For better performance we build up a list of
2666 * properties to set in a single transaction.
2668 err = nvlist_add_nvpair(genericnvl, pair);
2669 } else if (err != 0 && nvl != retrynvl) {
2671 * This may be a spurious error caused by
2672 * receiving quota and reservation out of order.
2673 * Try again in a second pass.
2675 err = nvlist_add_nvpair(retrynvl, pair);
2680 if (errlist != NULL)
2681 fnvlist_add_int32(errlist, propname, err);
2686 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2691 if (!nvlist_empty(genericnvl) &&
2692 dsl_props_set(dsname, source, genericnvl) != 0) {
2694 * If this fails, we still want to set as many properties as we
2695 * can, so try setting them individually.
2698 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2699 const char *propname = nvpair_name(pair);
2703 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2705 attrs = fnvpair_value_nvlist(pair);
2706 propval = fnvlist_lookup_nvpair(attrs,
2710 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2711 strval = fnvpair_value_string(propval);
2712 err = dsl_prop_set_string(dsname, propname,
2714 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2715 err = dsl_prop_inherit(dsname, propname,
2718 intval = fnvpair_value_uint64(propval);
2719 err = dsl_prop_set_int(dsname, propname, source,
2724 if (errlist != NULL) {
2725 fnvlist_add_int32(errlist, propname,
2732 nvlist_free(genericnvl);
2733 nvlist_free(retrynvl);
2739 * Check that all the properties are valid user properties.
2742 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2744 nvpair_t *pair = NULL;
2747 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2748 const char *propname = nvpair_name(pair);
2750 if (!zfs_prop_user(propname) ||
2751 nvpair_type(pair) != DATA_TYPE_STRING)
2752 return (SET_ERROR(EINVAL));
2754 if ((error = zfs_secpolicy_write_perms(fsname,
2755 ZFS_DELEG_PERM_USERPROP, CRED())))
2758 if (strlen(propname) >= ZAP_MAXNAMELEN)
2759 return (SET_ERROR(ENAMETOOLONG));
2761 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2762 return (SET_ERROR(E2BIG));
2768 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2772 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2775 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2776 if (nvlist_exists(skipped, nvpair_name(pair)))
2779 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2784 clear_received_props(const char *dsname, nvlist_t *props,
2788 nvlist_t *cleared_props = NULL;
2789 props_skip(props, skipped, &cleared_props);
2790 if (!nvlist_empty(cleared_props)) {
2792 * Acts on local properties until the dataset has received
2793 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2795 zprop_source_t flags = (ZPROP_SRC_NONE |
2796 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2797 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2799 nvlist_free(cleared_props);
2805 * zc_name name of filesystem
2806 * zc_value name of property to set
2807 * zc_nvlist_src{_size} nvlist of properties to apply
2808 * zc_cookie received properties flag
2811 * zc_nvlist_dst{_size} error for each unapplied received property
2814 zfs_ioc_set_prop(zfs_cmd_t *zc)
2817 boolean_t received = zc->zc_cookie;
2818 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2823 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2824 zc->zc_iflags, &nvl)) != 0)
2828 nvlist_t *origprops;
2830 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2831 (void) clear_received_props(zc->zc_name,
2833 nvlist_free(origprops);
2836 error = dsl_prop_set_hasrecvd(zc->zc_name);
2839 errors = fnvlist_alloc();
2841 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2843 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2844 (void) put_nvlist(zc, errors);
2847 nvlist_free(errors);
2854 * zc_name name of filesystem
2855 * zc_value name of property to inherit
2856 * zc_cookie revert to received value if TRUE
2861 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2863 const char *propname = zc->zc_value;
2864 zfs_prop_t prop = zfs_name_to_prop(propname);
2865 boolean_t received = zc->zc_cookie;
2866 zprop_source_t source = (received
2867 ? ZPROP_SRC_NONE /* revert to received value, if any */
2868 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2876 * Only check this in the non-received case. We want to allow
2877 * 'inherit -S' to revert non-inheritable properties like quota
2878 * and reservation to the received or default values even though
2879 * they are not considered inheritable.
2881 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2882 return (SET_ERROR(EINVAL));
2885 if (prop == ZPROP_INVAL) {
2886 if (!zfs_prop_user(propname))
2887 return (SET_ERROR(EINVAL));
2889 type = PROP_TYPE_STRING;
2890 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2891 return (SET_ERROR(EINVAL));
2893 type = zfs_prop_get_type(prop);
2897 * zfs_prop_set_special() expects properties in the form of an
2898 * nvpair with type info.
2900 dummy = fnvlist_alloc();
2903 case PROP_TYPE_STRING:
2904 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2906 case PROP_TYPE_NUMBER:
2907 case PROP_TYPE_INDEX:
2908 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2911 err = SET_ERROR(EINVAL);
2915 pair = nvlist_next_nvpair(dummy, NULL);
2917 err = SET_ERROR(EINVAL);
2919 err = zfs_prop_set_special(zc->zc_name, source, pair);
2920 if (err == -1) /* property is not "special", needs handling */
2921 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2931 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2938 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2939 zc->zc_iflags, &props)))
2943 * If the only property is the configfile, then just do a spa_lookup()
2944 * to handle the faulted case.
2946 pair = nvlist_next_nvpair(props, NULL);
2947 if (pair != NULL && strcmp(nvpair_name(pair),
2948 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2949 nvlist_next_nvpair(props, pair) == NULL) {
2950 mutex_enter(&spa_namespace_lock);
2951 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2952 spa_configfile_set(spa, props, B_FALSE);
2953 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2955 mutex_exit(&spa_namespace_lock);
2962 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2967 error = spa_prop_set(spa, props);
2970 spa_close(spa, FTAG);
2976 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2980 nvlist_t *nvp = NULL;
2982 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2984 * If the pool is faulted, there may be properties we can still
2985 * get (such as altroot and cachefile), so attempt to get them
2988 mutex_enter(&spa_namespace_lock);
2989 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2990 error = spa_prop_get(spa, &nvp);
2991 mutex_exit(&spa_namespace_lock);
2993 error = spa_prop_get(spa, &nvp);
2994 spa_close(spa, FTAG);
2997 if (error == 0 && zc->zc_nvlist_dst != 0)
2998 error = put_nvlist(zc, nvp);
3000 error = SET_ERROR(EFAULT);
3008 * zc_name name of filesystem
3009 * zc_nvlist_src{_size} nvlist of delegated permissions
3010 * zc_perm_action allow/unallow flag
3015 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3018 nvlist_t *fsaclnv = NULL;
3020 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3021 zc->zc_iflags, &fsaclnv)) != 0)
3025 * Verify nvlist is constructed correctly
3027 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3028 nvlist_free(fsaclnv);
3029 return (SET_ERROR(EINVAL));
3033 * If we don't have PRIV_SYS_MOUNT, then validate
3034 * that user is allowed to hand out each permission in
3038 error = secpolicy_zfs(CRED());
3040 if (zc->zc_perm_action == B_FALSE) {
3041 error = dsl_deleg_can_allow(zc->zc_name,
3044 error = dsl_deleg_can_unallow(zc->zc_name,
3050 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3052 nvlist_free(fsaclnv);
3058 * zc_name name of filesystem
3061 * zc_nvlist_src{_size} nvlist of delegated permissions
3064 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3069 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3070 error = put_nvlist(zc, nvp);
3079 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3081 zfs_creat_t *zct = arg;
3083 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3086 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3090 * os parent objset pointer (NULL if root fs)
3091 * fuids_ok fuids allowed in this version of the spa?
3092 * sa_ok SAs allowed in this version of the spa?
3093 * createprops list of properties requested by creator
3096 * zplprops values for the zplprops we attach to the master node object
3097 * is_ci true if requested file system will be purely case-insensitive
3099 * Determine the settings for utf8only, normalization and
3100 * casesensitivity. Specific values may have been requested by the
3101 * creator and/or we can inherit values from the parent dataset. If
3102 * the file system is of too early a vintage, a creator can not
3103 * request settings for these properties, even if the requested
3104 * setting is the default value. We don't actually want to create dsl
3105 * properties for these, so remove them from the source nvlist after
3109 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3110 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3111 nvlist_t *zplprops, boolean_t *is_ci)
3113 uint64_t sense = ZFS_PROP_UNDEFINED;
3114 uint64_t norm = ZFS_PROP_UNDEFINED;
3115 uint64_t u8 = ZFS_PROP_UNDEFINED;
3118 ASSERT(zplprops != NULL);
3120 /* parent dataset must be a filesystem */
3121 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3122 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3125 * Pull out creator prop choices, if any.
3128 (void) nvlist_lookup_uint64(createprops,
3129 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3130 (void) nvlist_lookup_uint64(createprops,
3131 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3132 (void) nvlist_remove_all(createprops,
3133 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3134 (void) nvlist_lookup_uint64(createprops,
3135 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3136 (void) nvlist_remove_all(createprops,
3137 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3138 (void) nvlist_lookup_uint64(createprops,
3139 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3140 (void) nvlist_remove_all(createprops,
3141 zfs_prop_to_name(ZFS_PROP_CASE));
3145 * If the zpl version requested is whacky or the file system
3146 * or pool is version is too "young" to support normalization
3147 * and the creator tried to set a value for one of the props,
3150 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3151 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3152 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3153 (zplver < ZPL_VERSION_NORMALIZATION &&
3154 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3155 sense != ZFS_PROP_UNDEFINED)))
3156 return (SET_ERROR(ENOTSUP));
3159 * Put the version in the zplprops
3161 VERIFY(nvlist_add_uint64(zplprops,
3162 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3164 if (norm == ZFS_PROP_UNDEFINED &&
3165 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3167 VERIFY(nvlist_add_uint64(zplprops,
3168 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3171 * If we're normalizing, names must always be valid UTF-8 strings.
3175 if (u8 == ZFS_PROP_UNDEFINED &&
3176 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3178 VERIFY(nvlist_add_uint64(zplprops,
3179 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3181 if (sense == ZFS_PROP_UNDEFINED &&
3182 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3184 VERIFY(nvlist_add_uint64(zplprops,
3185 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3188 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3194 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3195 nvlist_t *zplprops, boolean_t *is_ci)
3197 boolean_t fuids_ok, sa_ok;
3198 uint64_t zplver = ZPL_VERSION;
3199 objset_t *os = NULL;
3200 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3205 zfs_get_parent(dataset, parentname, sizeof (parentname));
3207 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3210 spa_vers = spa_version(spa);
3211 spa_close(spa, FTAG);
3213 zplver = zfs_zpl_version_map(spa_vers);
3214 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3215 sa_ok = (zplver >= ZPL_VERSION_SA);
3218 * Open parent object set so we can inherit zplprop values.
3220 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3223 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3225 dmu_objset_rele(os, FTAG);
3230 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3231 nvlist_t *zplprops, boolean_t *is_ci)
3235 uint64_t zplver = ZPL_VERSION;
3238 zplver = zfs_zpl_version_map(spa_vers);
3239 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3240 sa_ok = (zplver >= ZPL_VERSION_SA);
3242 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3243 createprops, zplprops, is_ci);
3249 * "type" -> dmu_objset_type_t (int32)
3250 * (optional) "props" -> { prop -> value }
3251 * (optional) "hidden_args" -> { "wkeydata" -> value }
3252 * raw uint8_t array of encryption wrapping key data (32 bytes)
3255 * outnvl: propname -> error code (int32)
3258 static const zfs_ioc_key_t zfs_keys_create[] = {
3259 {"type", DATA_TYPE_INT32, 0},
3260 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3261 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3265 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3268 zfs_creat_t zct = { 0 };
3269 nvlist_t *nvprops = NULL;
3270 nvlist_t *hidden_args = NULL;
3271 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3272 dmu_objset_type_t type;
3273 boolean_t is_insensitive = B_FALSE;
3274 dsl_crypto_params_t *dcp = NULL;
3276 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3277 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3278 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3282 cbfunc = zfs_create_cb;
3286 cbfunc = zvol_create_cb;
3293 if (strchr(fsname, '@') ||
3294 strchr(fsname, '%'))
3295 return (SET_ERROR(EINVAL));
3297 zct.zct_props = nvprops;
3300 return (SET_ERROR(EINVAL));
3302 if (type == DMU_OST_ZVOL) {
3303 uint64_t volsize, volblocksize;
3305 if (nvprops == NULL)
3306 return (SET_ERROR(EINVAL));
3307 if (nvlist_lookup_uint64(nvprops,
3308 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3309 return (SET_ERROR(EINVAL));
3311 if ((error = nvlist_lookup_uint64(nvprops,
3312 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3313 &volblocksize)) != 0 && error != ENOENT)
3314 return (SET_ERROR(EINVAL));
3317 volblocksize = zfs_prop_default_numeric(
3318 ZFS_PROP_VOLBLOCKSIZE);
3320 if ((error = zvol_check_volblocksize(fsname,
3321 volblocksize)) != 0 ||
3322 (error = zvol_check_volsize(volsize,
3323 volblocksize)) != 0)
3325 } else if (type == DMU_OST_ZFS) {
3329 * We have to have normalization and
3330 * case-folding flags correct when we do the
3331 * file system creation, so go figure them out
3334 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3335 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3336 error = zfs_fill_zplprops(fsname, nvprops,
3337 zct.zct_zplprops, &is_insensitive);
3339 nvlist_free(zct.zct_zplprops);
3344 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3347 nvlist_free(zct.zct_zplprops);
3351 error = dmu_objset_create(fsname, type,
3352 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3354 nvlist_free(zct.zct_zplprops);
3355 dsl_crypto_params_free(dcp, !!error);
3358 * It would be nice to do this atomically.
3361 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3368 * Volumes will return EBUSY and cannot be destroyed
3369 * until all asynchronous minor handling has completed.
3370 * Wait for the spa_zvol_taskq to drain then retry.
3372 error2 = dsl_destroy_head(fsname);
3373 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3374 error2 = spa_open(fsname, &spa, FTAG);
3376 taskq_wait(spa->spa_zvol_taskq);
3377 spa_close(spa, FTAG);
3379 error2 = dsl_destroy_head(fsname);
3388 * "origin" -> name of origin snapshot
3389 * (optional) "props" -> { prop -> value }
3390 * (optional) "hidden_args" -> { "wkeydata" -> value }
3391 * raw uint8_t array of encryption wrapping key data (32 bytes)
3395 * outnvl: propname -> error code (int32)
3397 static const zfs_ioc_key_t zfs_keys_clone[] = {
3398 {"origin", DATA_TYPE_STRING, 0},
3399 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3400 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3404 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3407 nvlist_t *nvprops = NULL;
3410 origin_name = fnvlist_lookup_string(innvl, "origin");
3411 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3413 if (strchr(fsname, '@') ||
3414 strchr(fsname, '%'))
3415 return (SET_ERROR(EINVAL));
3417 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3418 return (SET_ERROR(EINVAL));
3420 error = dmu_objset_clone(fsname, origin_name);
3423 * It would be nice to do this atomically.
3426 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3429 (void) dsl_destroy_head(fsname);
3434 static const zfs_ioc_key_t zfs_keys_remap[] = {
3440 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3442 /* This IOCTL is no longer supported. */
3448 * "snaps" -> { snapshot1, snapshot2 }
3449 * (optional) "props" -> { prop -> value (string) }
3452 * outnvl: snapshot -> error code (int32)
3454 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3455 {"snaps", DATA_TYPE_NVLIST, 0},
3456 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3460 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3463 nvlist_t *props = NULL;
3467 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3468 if ((error = zfs_check_userprops(poolname, props)) != 0)
3471 if (!nvlist_empty(props) &&
3472 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3473 return (SET_ERROR(ENOTSUP));
3475 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3476 poollen = strlen(poolname);
3477 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3478 pair = nvlist_next_nvpair(snaps, pair)) {
3479 const char *name = nvpair_name(pair);
3480 const char *cp = strchr(name, '@');
3483 * The snap name must contain an @, and the part after it must
3484 * contain only valid characters.
3487 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3488 return (SET_ERROR(EINVAL));
3491 * The snap must be in the specified pool.
3493 if (strncmp(name, poolname, poollen) != 0 ||
3494 (name[poollen] != '/' && name[poollen] != '@'))
3495 return (SET_ERROR(EXDEV));
3497 /* This must be the only snap of this fs. */
3498 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3499 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3500 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3502 return (SET_ERROR(EXDEV));
3507 error = dsl_dataset_snapshot(snaps, props, outnvl);
3513 * innvl: "message" -> string
3515 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3516 {"message", DATA_TYPE_STRING, 0},
3521 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3529 * The poolname in the ioctl is not set, we get it from the TSD,
3530 * which was set at the end of the last successful ioctl that allows
3531 * logging. The secpolicy func already checked that it is set.
3532 * Only one log ioctl is allowed after each successful ioctl, so
3533 * we clear the TSD here.
3535 poolname = tsd_get(zfs_allow_log_key);
3536 if (poolname == NULL)
3537 return (SET_ERROR(EINVAL));
3538 (void) tsd_set(zfs_allow_log_key, NULL);
3539 error = spa_open(poolname, &spa, FTAG);
3544 message = fnvlist_lookup_string(innvl, "message");
3546 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3547 spa_close(spa, FTAG);
3548 return (SET_ERROR(ENOTSUP));
3551 error = spa_history_log(spa, message);
3552 spa_close(spa, FTAG);
3557 * The dp_config_rwlock must not be held when calling this, because the
3558 * unmount may need to write out data.
3560 * This function is best-effort. Callers must deal gracefully if it
3561 * remains mounted (or is remounted after this call).
3563 * Returns 0 if the argument is not a snapshot, or it is not currently a
3564 * filesystem, or we were able to unmount it. Returns error code otherwise.
3567 zfs_unmount_snap(const char *snapname)
3569 if (strchr(snapname, '@') == NULL)
3572 (void) zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
3577 zfs_unmount_snap_cb(const char *snapname, void *arg)
3579 zfs_unmount_snap(snapname);
3584 * When a clone is destroyed, its origin may also need to be destroyed,
3585 * in which case it must be unmounted. This routine will do that unmount
3589 zfs_destroy_unmount_origin(const char *fsname)
3595 error = dmu_objset_hold(fsname, FTAG, &os);
3598 ds = dmu_objset_ds(os);
3599 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3600 char originname[ZFS_MAX_DATASET_NAME_LEN];
3601 dsl_dataset_name(ds->ds_prev, originname);
3602 dmu_objset_rele(os, FTAG);
3603 zfs_unmount_snap(originname);
3605 dmu_objset_rele(os, FTAG);
3611 * "snaps" -> { snapshot1, snapshot2 }
3612 * (optional boolean) "defer"
3615 * outnvl: snapshot -> error code (int32)
3617 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3618 {"snaps", DATA_TYPE_NVLIST, 0},
3619 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3624 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3630 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3631 defer = nvlist_exists(innvl, "defer");
3633 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3634 pair = nvlist_next_nvpair(snaps, pair)) {
3635 zfs_unmount_snap(nvpair_name(pair));
3638 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3642 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3643 * All bookmarks must be in the same pool.
3646 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3649 * outnvl: bookmark -> error code (int32)
3652 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3653 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3658 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3660 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3661 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3665 * Verify the snapshot argument.
3667 if (nvpair_value_string(pair, &snap_name) != 0)
3668 return (SET_ERROR(EINVAL));
3671 /* Verify that the keys (bookmarks) are unique */
3672 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3673 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3674 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3675 return (SET_ERROR(EINVAL));
3679 return (dsl_bookmark_create(innvl, outnvl));
3684 * property 1, property 2, ...
3688 * bookmark name 1 -> { property 1, property 2, ... },
3689 * bookmark name 2 -> { property 1, property 2, ... }
3693 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3694 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3698 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3700 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3704 * innvl is not used.
3707 * property 1, property 2, ...
3711 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
3717 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
3720 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3723 bmname = strchr(bookmark, '#');
3725 return (SET_ERROR(EINVAL));
3728 (void) strlcpy(fsname, bookmark, sizeof (fsname));
3729 *(strchr(fsname, '#')) = '\0';
3731 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
3736 * bookmark name 1, bookmark name 2
3739 * outnvl: bookmark -> error code (int32)
3742 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3743 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3747 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3752 poollen = strlen(poolname);
3753 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3754 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3755 const char *name = nvpair_name(pair);
3756 const char *cp = strchr(name, '#');
3759 * The bookmark name must contain an #, and the part after it
3760 * must contain only valid characters.
3763 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3764 return (SET_ERROR(EINVAL));
3767 * The bookmark must be in the specified pool.
3769 if (strncmp(name, poolname, poollen) != 0 ||
3770 (name[poollen] != '/' && name[poollen] != '#'))
3771 return (SET_ERROR(EXDEV));
3774 error = dsl_bookmark_destroy(innvl, outnvl);
3778 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3779 {"program", DATA_TYPE_STRING, 0},
3780 {"arg", DATA_TYPE_ANY, 0},
3781 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3782 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3783 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3787 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3791 uint64_t instrlimit, memlimit;
3792 boolean_t sync_flag;
3793 nvpair_t *nvarg = NULL;
3795 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3796 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3799 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3800 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3802 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3803 memlimit = ZCP_DEFAULT_MEMLIMIT;
3805 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3807 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3809 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3812 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3820 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3826 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3828 return (spa_checkpoint(poolname));
3835 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3841 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3844 return (spa_checkpoint_discard(poolname));
3849 * zc_name name of dataset to destroy
3850 * zc_defer_destroy mark for deferred destroy
3855 zfs_ioc_destroy(zfs_cmd_t *zc)
3858 dmu_objset_type_t ost;
3861 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3864 ost = dmu_objset_type(os);
3865 dmu_objset_rele(os, FTAG);
3867 if (ost == DMU_OST_ZFS)
3868 zfs_unmount_snap(zc->zc_name);
3870 if (strchr(zc->zc_name, '@')) {
3871 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3873 err = dsl_destroy_head(zc->zc_name);
3874 if (err == EEXIST) {
3876 * It is possible that the given DS may have
3877 * hidden child (%recv) datasets - "leftovers"
3878 * resulting from the previously interrupted
3881 * 6 extra bytes for /%recv
3883 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3885 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3886 zc->zc_name, recv_clone_name) >=
3888 return (SET_ERROR(EINVAL));
3891 * Try to remove the hidden child (%recv) and after
3892 * that try to remove the target dataset.
3893 * If the hidden child (%recv) does not exist
3894 * the original error (EEXIST) will be returned
3896 err = dsl_destroy_head(namebuf);
3898 err = dsl_destroy_head(zc->zc_name);
3899 else if (err == ENOENT)
3900 err = SET_ERROR(EEXIST);
3909 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
3910 * "initialize_vdevs": { -> guids to initialize (nvlist)
3911 * "vdev_path_1": vdev_guid_1, (uint64),
3912 * "vdev_path_2": vdev_guid_2, (uint64),
3918 * "initialize_vdevs": { -> initialization errors (nvlist)
3919 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3920 * "vdev_path_2": errno, ... (uint64)
3925 * EINVAL is returned for an unknown commands or if any of the provided vdev
3926 * guids have be specified with a type other than uint64.
3928 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
3929 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
3930 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
3934 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3937 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3939 return (SET_ERROR(EINVAL));
3942 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3943 cmd_type == POOL_INITIALIZE_START ||
3944 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3945 return (SET_ERROR(EINVAL));
3948 nvlist_t *vdev_guids;
3949 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3950 &vdev_guids) != 0) {
3951 return (SET_ERROR(EINVAL));
3954 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3955 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3957 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
3958 return (SET_ERROR(EINVAL));
3963 int error = spa_open(poolname, &spa, FTAG);
3967 nvlist_t *vdev_errlist = fnvlist_alloc();
3968 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
3971 if (fnvlist_size(vdev_errlist) > 0) {
3972 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3975 fnvlist_free(vdev_errlist);
3977 spa_close(spa, FTAG);
3978 return (total_errors > 0 ? EINVAL : 0);
3983 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
3984 * "trim_vdevs": { -> guids to TRIM (nvlist)
3985 * "vdev_path_1": vdev_guid_1, (uint64),
3986 * "vdev_path_2": vdev_guid_2, (uint64),
3989 * "trim_rate" -> Target TRIM rate in bytes/sec.
3990 * "trim_secure" -> Set to request a secure TRIM.
3994 * "trim_vdevs": { -> TRIM errors (nvlist)
3995 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3996 * "vdev_path_2": errno, ... (uint64)
4001 * EINVAL is returned for an unknown commands or if any of the provided vdev
4002 * guids have be specified with a type other than uint64.
4004 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4005 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4006 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4007 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4008 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4012 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4015 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4016 return (SET_ERROR(EINVAL));
4018 if (!(cmd_type == POOL_TRIM_CANCEL ||
4019 cmd_type == POOL_TRIM_START ||
4020 cmd_type == POOL_TRIM_SUSPEND)) {
4021 return (SET_ERROR(EINVAL));
4024 nvlist_t *vdev_guids;
4025 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4026 return (SET_ERROR(EINVAL));
4028 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4029 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4031 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4032 return (SET_ERROR(EINVAL));
4036 /* Optional, defaults to maximum rate when not provided */
4038 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4041 /* Optional, defaults to standard TRIM when not provided */
4043 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4049 int error = spa_open(poolname, &spa, FTAG);
4053 nvlist_t *vdev_errlist = fnvlist_alloc();
4054 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4055 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4057 if (fnvlist_size(vdev_errlist) > 0)
4058 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4060 fnvlist_free(vdev_errlist);
4062 spa_close(spa, FTAG);
4063 return (total_errors > 0 ? EINVAL : 0);
4067 * fsname is name of dataset to rollback (to most recent snapshot)
4069 * innvl may contain name of expected target snapshot
4071 * outnvl: "target" -> name of most recent snapshot
4074 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4075 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4080 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4084 char *target = NULL;
4087 (void) nvlist_lookup_string(innvl, "target", &target);
4088 if (target != NULL) {
4089 const char *cp = strchr(target, '@');
4092 * The snap name must contain an @, and the part after it must
4093 * contain only valid characters.
4096 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4097 return (SET_ERROR(EINVAL));
4100 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4103 ds = dmu_objset_ds(zfsvfs->z_os);
4104 error = zfs_suspend_fs(zfsvfs);
4108 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4110 resume_err = zfs_resume_fs(zfsvfs, ds);
4111 error = error ? error : resume_err;
4113 deactivate_super(zfsvfs->z_sb);
4114 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4115 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4119 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4125 recursive_unmount(const char *fsname, void *arg)
4127 const char *snapname = arg;
4130 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4131 zfs_unmount_snap(fullname);
4139 * snapname is the snapshot to redact.
4141 * "bookname" -> (string)
4142 * name of the redaction bookmark to generate
4143 * "snapnv" -> (nvlist, values ignored)
4144 * snapshots to redact snapname with respect to
4151 static const zfs_ioc_key_t zfs_keys_redact[] = {
4152 {"bookname", DATA_TYPE_STRING, 0},
4153 {"snapnv", DATA_TYPE_NVLIST, 0},
4156 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4158 nvlist_t *redactnvl = NULL;
4159 char *redactbook = NULL;
4161 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4162 return (SET_ERROR(EINVAL));
4163 if (fnvlist_num_pairs(redactnvl) == 0)
4164 return (SET_ERROR(ENXIO));
4165 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4166 return (SET_ERROR(EINVAL));
4168 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4173 * zc_name old name of dataset
4174 * zc_value new name of dataset
4175 * zc_cookie recursive flag (only valid for snapshots)
4180 zfs_ioc_rename(zfs_cmd_t *zc)
4183 dmu_objset_type_t ost;
4184 boolean_t recursive = zc->zc_cookie & 1;
4188 /* "zfs rename" from and to ...%recv datasets should both fail */
4189 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4190 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4191 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4192 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4193 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4194 return (SET_ERROR(EINVAL));
4196 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4199 ost = dmu_objset_type(os);
4200 dmu_objset_rele(os, FTAG);
4202 at = strchr(zc->zc_name, '@');
4204 /* snaps must be in same fs */
4207 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4208 return (SET_ERROR(EXDEV));
4210 if (ost == DMU_OST_ZFS) {
4211 error = dmu_objset_find(zc->zc_name,
4212 recursive_unmount, at + 1,
4213 recursive ? DS_FIND_CHILDREN : 0);
4219 error = dsl_dataset_rename_snapshot(zc->zc_name,
4220 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4225 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4230 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4232 const char *propname = nvpair_name(pair);
4233 boolean_t issnap = (strchr(dsname, '@') != NULL);
4234 zfs_prop_t prop = zfs_name_to_prop(propname);
4238 if (prop == ZPROP_INVAL) {
4239 if (zfs_prop_user(propname)) {
4240 if ((err = zfs_secpolicy_write_perms(dsname,
4241 ZFS_DELEG_PERM_USERPROP, cr)))
4246 if (!issnap && zfs_prop_userquota(propname)) {
4247 const char *perm = NULL;
4248 const char *uq_prefix =
4249 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4250 const char *gq_prefix =
4251 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4252 const char *uiq_prefix =
4253 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4254 const char *giq_prefix =
4255 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4256 const char *pq_prefix =
4257 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4258 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4259 ZFS_PROP_PROJECTOBJQUOTA];
4261 if (strncmp(propname, uq_prefix,
4262 strlen(uq_prefix)) == 0) {
4263 perm = ZFS_DELEG_PERM_USERQUOTA;
4264 } else if (strncmp(propname, uiq_prefix,
4265 strlen(uiq_prefix)) == 0) {
4266 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4267 } else if (strncmp(propname, gq_prefix,
4268 strlen(gq_prefix)) == 0) {
4269 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4270 } else if (strncmp(propname, giq_prefix,
4271 strlen(giq_prefix)) == 0) {
4272 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4273 } else if (strncmp(propname, pq_prefix,
4274 strlen(pq_prefix)) == 0) {
4275 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4276 } else if (strncmp(propname, piq_prefix,
4277 strlen(piq_prefix)) == 0) {
4278 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4280 /* {USER|GROUP|PROJECT}USED are read-only */
4281 return (SET_ERROR(EINVAL));
4284 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4289 return (SET_ERROR(EINVAL));
4293 return (SET_ERROR(EINVAL));
4295 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4297 * dsl_prop_get_all_impl() returns properties in this
4301 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4302 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4307 * Check that this value is valid for this pool version
4310 case ZFS_PROP_COMPRESSION:
4312 * If the user specified gzip compression, make sure
4313 * the SPA supports it. We ignore any errors here since
4314 * we'll catch them later.
4316 if (nvpair_value_uint64(pair, &intval) == 0) {
4317 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4318 intval <= ZIO_COMPRESS_GZIP_9 &&
4319 zfs_earlier_version(dsname,
4320 SPA_VERSION_GZIP_COMPRESSION)) {
4321 return (SET_ERROR(ENOTSUP));
4324 if (intval == ZIO_COMPRESS_ZLE &&
4325 zfs_earlier_version(dsname,
4326 SPA_VERSION_ZLE_COMPRESSION))
4327 return (SET_ERROR(ENOTSUP));
4329 if (intval == ZIO_COMPRESS_LZ4) {
4332 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4335 if (!spa_feature_is_enabled(spa,
4336 SPA_FEATURE_LZ4_COMPRESS)) {
4337 spa_close(spa, FTAG);
4338 return (SET_ERROR(ENOTSUP));
4340 spa_close(spa, FTAG);
4344 * If this is a bootable dataset then
4345 * verify that the compression algorithm
4346 * is supported for booting. We must return
4347 * something other than ENOTSUP since it
4348 * implies a downrev pool version.
4350 if (zfs_is_bootfs(dsname) &&
4351 !BOOTFS_COMPRESS_VALID(intval)) {
4352 return (SET_ERROR(ERANGE));
4357 case ZFS_PROP_COPIES:
4358 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4359 return (SET_ERROR(ENOTSUP));
4362 case ZFS_PROP_VOLBLOCKSIZE:
4363 case ZFS_PROP_RECORDSIZE:
4364 /* Record sizes above 128k need the feature to be enabled */
4365 if (nvpair_value_uint64(pair, &intval) == 0 &&
4366 intval > SPA_OLD_MAXBLOCKSIZE) {
4370 * We don't allow setting the property above 1MB,
4371 * unless the tunable has been changed.
4373 if (intval > zfs_max_recordsize ||
4374 intval > SPA_MAXBLOCKSIZE)
4375 return (SET_ERROR(ERANGE));
4377 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4380 if (!spa_feature_is_enabled(spa,
4381 SPA_FEATURE_LARGE_BLOCKS)) {
4382 spa_close(spa, FTAG);
4383 return (SET_ERROR(ENOTSUP));
4385 spa_close(spa, FTAG);
4389 case ZFS_PROP_DNODESIZE:
4390 /* Dnode sizes above 512 need the feature to be enabled */
4391 if (nvpair_value_uint64(pair, &intval) == 0 &&
4392 intval != ZFS_DNSIZE_LEGACY) {
4396 * If this is a bootable dataset then
4397 * we don't allow large (>512B) dnodes,
4398 * because GRUB doesn't support them.
4400 if (zfs_is_bootfs(dsname) &&
4401 intval != ZFS_DNSIZE_LEGACY) {
4402 return (SET_ERROR(EDOM));
4405 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4408 if (!spa_feature_is_enabled(spa,
4409 SPA_FEATURE_LARGE_DNODE)) {
4410 spa_close(spa, FTAG);
4411 return (SET_ERROR(ENOTSUP));
4413 spa_close(spa, FTAG);
4417 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4419 * This property could require the allocation classes
4420 * feature to be active for setting, however we allow
4421 * it so that tests of settable properties succeed.
4422 * The CLI will issue a warning in this case.
4426 case ZFS_PROP_SHARESMB:
4427 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4428 return (SET_ERROR(ENOTSUP));
4431 case ZFS_PROP_ACLINHERIT:
4432 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4433 nvpair_value_uint64(pair, &intval) == 0) {
4434 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4435 zfs_earlier_version(dsname,
4436 SPA_VERSION_PASSTHROUGH_X))
4437 return (SET_ERROR(ENOTSUP));
4440 case ZFS_PROP_CHECKSUM:
4441 case ZFS_PROP_DEDUP:
4443 spa_feature_t feature;
4447 /* dedup feature version checks */
4448 if (prop == ZFS_PROP_DEDUP &&
4449 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4450 return (SET_ERROR(ENOTSUP));
4452 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4453 nvpair_value_uint64(pair, &intval) == 0) {
4454 /* check prop value is enabled in features */
4455 feature = zio_checksum_to_feature(
4456 intval & ZIO_CHECKSUM_MASK);
4457 if (feature == SPA_FEATURE_NONE)
4460 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4463 if (!spa_feature_is_enabled(spa, feature)) {
4464 spa_close(spa, FTAG);
4465 return (SET_ERROR(ENOTSUP));
4467 spa_close(spa, FTAG);
4476 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4480 * Removes properties from the given props list that fail permission checks
4481 * needed to clear them and to restore them in case of a receive error. For each
4482 * property, make sure we have both set and inherit permissions.
4484 * Returns the first error encountered if any permission checks fail. If the
4485 * caller provides a non-NULL errlist, it also gives the complete list of names
4486 * of all the properties that failed a permission check along with the
4487 * corresponding error numbers. The caller is responsible for freeing the
4490 * If every property checks out successfully, zero is returned and the list
4491 * pointed at by errlist is NULL.
4494 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4497 nvpair_t *pair, *next_pair;
4504 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4506 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4507 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4508 pair = nvlist_next_nvpair(props, NULL);
4509 while (pair != NULL) {
4510 next_pair = nvlist_next_nvpair(props, pair);
4512 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4513 sizeof (zc->zc_value));
4514 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4515 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4516 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4517 VERIFY(nvlist_add_int32(errors,
4518 zc->zc_value, err) == 0);
4522 kmem_free(zc, sizeof (zfs_cmd_t));
4524 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4525 nvlist_free(errors);
4528 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4531 if (errlist == NULL)
4532 nvlist_free(errors);
4540 propval_equals(nvpair_t *p1, nvpair_t *p2)
4542 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4543 /* dsl_prop_get_all_impl() format */
4545 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4546 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4550 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4552 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4553 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4557 if (nvpair_type(p1) != nvpair_type(p2))
4560 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4561 char *valstr1, *valstr2;
4563 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4564 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4565 return (strcmp(valstr1, valstr2) == 0);
4567 uint64_t intval1, intval2;
4569 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4570 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4571 return (intval1 == intval2);
4576 * Remove properties from props if they are not going to change (as determined
4577 * by comparison with origprops). Remove them from origprops as well, since we
4578 * do not need to clear or restore properties that won't change.
4581 props_reduce(nvlist_t *props, nvlist_t *origprops)
4583 nvpair_t *pair, *next_pair;
4585 if (origprops == NULL)
4586 return; /* all props need to be received */
4588 pair = nvlist_next_nvpair(props, NULL);
4589 while (pair != NULL) {
4590 const char *propname = nvpair_name(pair);
4593 next_pair = nvlist_next_nvpair(props, pair);
4595 if ((nvlist_lookup_nvpair(origprops, propname,
4596 &match) != 0) || !propval_equals(pair, match))
4597 goto next; /* need to set received value */
4599 /* don't clear the existing received value */
4600 (void) nvlist_remove_nvpair(origprops, match);
4601 /* don't bother receiving the property */
4602 (void) nvlist_remove_nvpair(props, pair);
4609 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4610 * For example, refquota cannot be set until after the receipt of a dataset,
4611 * because in replication streams, an older/earlier snapshot may exceed the
4612 * refquota. We want to receive the older/earlier snapshot, but setting
4613 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4614 * the older/earlier snapshot from being received (with EDQUOT).
4616 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4618 * libzfs will need to be judicious handling errors encountered by props
4619 * extracted by this function.
4622 extract_delay_props(nvlist_t *props)
4624 nvlist_t *delayprops;
4625 nvpair_t *nvp, *tmp;
4626 static const zfs_prop_t delayable[] = {
4628 ZFS_PROP_KEYLOCATION,
4633 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4635 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4636 nvp = nvlist_next_nvpair(props, nvp)) {
4638 * strcmp() is safe because zfs_prop_to_name() always returns
4641 for (i = 0; delayable[i] != 0; i++) {
4642 if (strcmp(zfs_prop_to_name(delayable[i]),
4643 nvpair_name(nvp)) == 0) {
4647 if (delayable[i] != 0) {
4648 tmp = nvlist_prev_nvpair(props, nvp);
4649 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4650 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4655 if (nvlist_empty(delayprops)) {
4656 nvlist_free(delayprops);
4659 return (delayprops);
4663 static boolean_t zfs_ioc_recv_inject_err;
4667 * nvlist 'errors' is always allocated. It will contain descriptions of
4668 * encountered errors, if any. It's the callers responsibility to free.
4671 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4672 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4673 boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4674 int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4675 uint64_t *action_handle, nvlist_t **errors)
4677 dmu_recv_cookie_t drc;
4679 int props_error = 0;
4681 nvlist_t *local_delayprops = NULL;
4682 nvlist_t *recv_delayprops = NULL;
4683 nvlist_t *origprops = NULL; /* existing properties */
4684 nvlist_t *origrecvd = NULL; /* existing received properties */
4685 boolean_t first_recvd_props = B_FALSE;
4686 boolean_t tofs_was_redacted;
4691 *errors = fnvlist_alloc();
4693 input_fp = getf(input_fd);
4694 if (input_fp == NULL)
4695 return (SET_ERROR(EBADF));
4697 off = input_fp->f_offset;
4698 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4699 resumable, localprops, hidden_args, origin, &drc, input_fp->f_vnode,
4703 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
4706 * Set properties before we receive the stream so that they are applied
4707 * to the new data. Note that we must call dmu_recv_stream() if
4708 * dmu_recv_begin() succeeds.
4710 if (recvprops != NULL && !drc.drc_newfs) {
4711 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4712 SPA_VERSION_RECVD_PROPS &&
4713 !dsl_prop_get_hasrecvd(tofs))
4714 first_recvd_props = B_TRUE;
4717 * If new received properties are supplied, they are to
4718 * completely replace the existing received properties,
4719 * so stash away the existing ones.
4721 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4722 nvlist_t *errlist = NULL;
4724 * Don't bother writing a property if its value won't
4725 * change (and avoid the unnecessary security checks).
4727 * The first receive after SPA_VERSION_RECVD_PROPS is a
4728 * special case where we blow away all local properties
4731 if (!first_recvd_props)
4732 props_reduce(recvprops, origrecvd);
4733 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4734 (void) nvlist_merge(*errors, errlist, 0);
4735 nvlist_free(errlist);
4737 if (clear_received_props(tofs, origrecvd,
4738 first_recvd_props ? NULL : recvprops) != 0)
4739 *errflags |= ZPROP_ERR_NOCLEAR;
4741 *errflags |= ZPROP_ERR_NOCLEAR;
4746 * Stash away existing properties so we can restore them on error unless
4747 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4748 * case "origrecvd" will take care of that.
4750 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4752 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4753 if (dsl_prop_get_all(os, &origprops) != 0) {
4754 *errflags |= ZPROP_ERR_NOCLEAR;
4756 dmu_objset_rele(os, FTAG);
4758 *errflags |= ZPROP_ERR_NOCLEAR;
4762 if (recvprops != NULL) {
4763 props_error = dsl_prop_set_hasrecvd(tofs);
4765 if (props_error == 0) {
4766 recv_delayprops = extract_delay_props(recvprops);
4767 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4768 recvprops, *errors);
4772 if (localprops != NULL) {
4773 nvlist_t *oprops = fnvlist_alloc();
4774 nvlist_t *xprops = fnvlist_alloc();
4775 nvpair_t *nvp = NULL;
4777 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4778 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4780 const char *name = nvpair_name(nvp);
4781 zfs_prop_t prop = zfs_name_to_prop(name);
4782 if (prop != ZPROP_INVAL) {
4783 if (!zfs_prop_inheritable(prop))
4785 } else if (!zfs_prop_user(name))
4787 fnvlist_add_boolean(xprops, name);
4789 /* -o property=value */
4790 fnvlist_add_nvpair(oprops, nvp);
4794 local_delayprops = extract_delay_props(oprops);
4795 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4797 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4800 nvlist_free(oprops);
4801 nvlist_free(xprops);
4804 error = dmu_recv_stream(&drc, cleanup_fd, action_handle, &off);
4807 zfsvfs_t *zfsvfs = NULL;
4808 zvol_state_t *zv = NULL;
4810 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4814 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
4815 begin_record->drr_u.drr_begin.
4816 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
4818 ds = dmu_objset_ds(zfsvfs->z_os);
4819 error = zfs_suspend_fs(zfsvfs);
4821 * If the suspend fails, then the recv_end will
4822 * likely also fail, and clean up after itself.
4824 end_err = dmu_recv_end(&drc, zfsvfs);
4826 * If the dataset was not redacted, but we received a
4827 * redacted stream onto it, we need to unmount the
4828 * dataset. Otherwise, resume the filesystem.
4830 if (error == 0 && !drc.drc_newfs &&
4831 stream_is_redacted && !tofs_was_redacted) {
4832 error = zfs_end_fs(zfsvfs, ds);
4833 } else if (error == 0) {
4834 error = zfs_resume_fs(zfsvfs, ds);
4836 error = error ? error : end_err;
4837 deactivate_super(zfsvfs->z_sb);
4838 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4839 error = dmu_recv_end(&drc, zvol_tag(zv));
4842 error = dmu_recv_end(&drc, NULL);
4845 /* Set delayed properties now, after we're done receiving. */
4846 if (recv_delayprops != NULL && error == 0) {
4847 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4848 recv_delayprops, *errors);
4850 if (local_delayprops != NULL && error == 0) {
4851 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4852 local_delayprops, *errors);
4857 * Merge delayed props back in with initial props, in case
4858 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4859 * we have to make sure clear_received_props() includes
4860 * the delayed properties).
4862 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4863 * using ASSERT() will be just like a VERIFY.
4865 if (recv_delayprops != NULL) {
4866 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4867 nvlist_free(recv_delayprops);
4869 if (local_delayprops != NULL) {
4870 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4871 nvlist_free(local_delayprops);
4874 *read_bytes = off - input_fp->f_offset;
4875 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4876 input_fp->f_offset = off;
4879 if (zfs_ioc_recv_inject_err) {
4880 zfs_ioc_recv_inject_err = B_FALSE;
4886 * On error, restore the original props.
4888 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4889 if (clear_received_props(tofs, recvprops, NULL) != 0) {
4891 * We failed to clear the received properties.
4892 * Since we may have left a $recvd value on the
4893 * system, we can't clear the $hasrecvd flag.
4895 *errflags |= ZPROP_ERR_NORESTORE;
4896 } else if (first_recvd_props) {
4897 dsl_prop_unset_hasrecvd(tofs);
4900 if (origrecvd == NULL && !drc.drc_newfs) {
4901 /* We failed to stash the original properties. */
4902 *errflags |= ZPROP_ERR_NORESTORE;
4906 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4907 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4908 * explicitly if we're restoring local properties cleared in the
4909 * first new-style receive.
4911 if (origrecvd != NULL &&
4912 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4913 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4914 origrecvd, NULL) != 0) {
4916 * We stashed the original properties but failed to
4919 *errflags |= ZPROP_ERR_NORESTORE;
4922 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4923 !first_recvd_props) {
4925 nvlist_t *inheritprops;
4928 if (origprops == NULL) {
4929 /* We failed to stash the original properties. */
4930 *errflags |= ZPROP_ERR_NORESTORE;
4934 /* Restore original props */
4935 setprops = fnvlist_alloc();
4936 inheritprops = fnvlist_alloc();
4938 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4939 const char *name = nvpair_name(nvp);
4943 if (!nvlist_exists(origprops, name)) {
4945 * Property was not present or was explicitly
4946 * inherited before the receive, restore this.
4948 fnvlist_add_boolean(inheritprops, name);
4951 attrs = fnvlist_lookup_nvlist(origprops, name);
4952 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4954 /* Skip received properties */
4955 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4958 if (strcmp(source, tofs) == 0) {
4959 /* Property was locally set */
4960 fnvlist_add_nvlist(setprops, name, attrs);
4962 /* Property was implicitly inherited */
4963 fnvlist_add_boolean(inheritprops, name);
4967 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
4969 *errflags |= ZPROP_ERR_NORESTORE;
4970 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
4972 *errflags |= ZPROP_ERR_NORESTORE;
4974 nvlist_free(setprops);
4975 nvlist_free(inheritprops);
4979 nvlist_free(origrecvd);
4980 nvlist_free(origprops);
4983 error = props_error;
4990 * zc_name name of containing filesystem (unused)
4991 * zc_nvlist_src{_size} nvlist of properties to apply
4992 * zc_nvlist_conf{_size} nvlist of properties to exclude
4993 * (DATA_TYPE_BOOLEAN) and override (everything else)
4994 * zc_value name of snapshot to create
4995 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4996 * zc_cookie file descriptor to recv from
4997 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4998 * zc_guid force flag
4999 * zc_cleanup_fd cleanup-on-exit file descriptor
5000 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
5003 * zc_cookie number of bytes read
5004 * zc_obj zprop_errflags_t
5005 * zc_action_handle handle for this guid/ds mapping
5006 * zc_nvlist_dst{_size} error for each unapplied received property
5009 zfs_ioc_recv(zfs_cmd_t *zc)
5011 dmu_replay_record_t begin_record;
5012 nvlist_t *errors = NULL;
5013 nvlist_t *recvdprops = NULL;
5014 nvlist_t *localprops = NULL;
5015 char *origin = NULL;
5017 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5020 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5021 strchr(zc->zc_value, '@') == NULL ||
5022 strchr(zc->zc_value, '%'))
5023 return (SET_ERROR(EINVAL));
5025 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5026 tosnap = strchr(tofs, '@');
5029 if (zc->zc_nvlist_src != 0 &&
5030 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5031 zc->zc_iflags, &recvdprops)) != 0)
5034 if (zc->zc_nvlist_conf != 0 &&
5035 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5036 zc->zc_iflags, &localprops)) != 0)
5039 if (zc->zc_string[0])
5040 origin = zc->zc_string;
5042 begin_record.drr_type = DRR_BEGIN;
5043 begin_record.drr_payloadlen = 0;
5044 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5046 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5047 NULL, zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
5048 zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
5049 &zc->zc_action_handle, &errors);
5050 nvlist_free(recvdprops);
5051 nvlist_free(localprops);
5054 * Now that all props, initial and delayed, are set, report the prop
5055 * errors to the caller.
5057 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5058 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5059 put_nvlist(zc, errors) != 0)) {
5061 * Caller made zc->zc_nvlist_dst less than the minimum expected
5062 * size or supplied an invalid address.
5064 error = SET_ERROR(EINVAL);
5067 nvlist_free(errors);
5074 * "snapname" -> full name of the snapshot to create
5075 * (optional) "props" -> received properties to set (nvlist)
5076 * (optional) "localprops" -> override and exclude properties (nvlist)
5077 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5078 * "begin_record" -> non-byteswapped dmu_replay_record_t
5079 * "input_fd" -> file descriptor to read stream from (int32)
5080 * (optional) "force" -> force flag (value ignored)
5081 * (optional) "resumable" -> resumable flag (value ignored)
5082 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
5083 * (optional) "action_handle" -> handle for this guid/ds mapping
5084 * (optional) "hidden_args" -> { "wkeydata" -> value }
5088 * "read_bytes" -> number of bytes read
5089 * "error_flags" -> zprop_errflags_t
5090 * "action_handle" -> handle for this guid/ds mapping
5091 * "errors" -> error for each unapplied received property (nvlist)
5094 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5095 {"snapname", DATA_TYPE_STRING, 0},
5096 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5097 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5098 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5099 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5100 {"input_fd", DATA_TYPE_INT32, 0},
5101 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5102 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5103 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5104 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5105 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5109 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5111 dmu_replay_record_t *begin_record;
5112 uint_t begin_record_size;
5113 nvlist_t *errors = NULL;
5114 nvlist_t *recvprops = NULL;
5115 nvlist_t *localprops = NULL;
5116 nvlist_t *hidden_args = NULL;
5118 char *origin = NULL;
5120 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5122 boolean_t resumable;
5123 uint64_t action_handle = 0;
5124 uint64_t read_bytes = 0;
5125 uint64_t errflags = 0;
5127 int cleanup_fd = -1;
5130 snapname = fnvlist_lookup_string(innvl, "snapname");
5132 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5133 strchr(snapname, '@') == NULL ||
5134 strchr(snapname, '%'))
5135 return (SET_ERROR(EINVAL));
5137 (void) strcpy(tofs, snapname);
5138 tosnap = strchr(tofs, '@');
5141 error = nvlist_lookup_string(innvl, "origin", &origin);
5142 if (error && error != ENOENT)
5145 error = nvlist_lookup_byte_array(innvl, "begin_record",
5146 (uchar_t **)&begin_record, &begin_record_size);
5147 if (error != 0 || begin_record_size != sizeof (*begin_record))
5148 return (SET_ERROR(EINVAL));
5150 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5152 force = nvlist_exists(innvl, "force");
5153 resumable = nvlist_exists(innvl, "resumable");
5155 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
5156 if (error && error != ENOENT)
5159 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
5160 if (error && error != ENOENT)
5163 /* we still use "props" here for backwards compatibility */
5164 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5165 if (error && error != ENOENT)
5168 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5169 if (error && error != ENOENT)
5172 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5173 if (error && error != ENOENT)
5176 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5177 hidden_args, force, resumable, input_fd, begin_record, cleanup_fd,
5178 &read_bytes, &errflags, &action_handle, &errors);
5180 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5181 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5182 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
5183 fnvlist_add_nvlist(outnvl, "errors", errors);
5185 nvlist_free(errors);
5186 nvlist_free(recvprops);
5187 nvlist_free(localprops);
5192 typedef struct dump_bytes_io {
5200 dump_bytes_cb(void *arg)
5202 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
5203 ssize_t resid; /* have to get resid to get detailed errno */
5205 dbi->dbi_err = vn_rdwr(UIO_WRITE, dbi->dbi_vp,
5206 (caddr_t)dbi->dbi_buf, dbi->dbi_len,
5207 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
5211 dump_bytes(objset_t *os, void *buf, int len, void *arg)
5213 dump_bytes_io_t dbi;
5219 #if defined(HAVE_LARGE_STACKS)
5220 dump_bytes_cb(&dbi);
5223 * The vn_rdwr() call is performed in a taskq to ensure that there is
5224 * always enough stack space to write safely to the target filesystem.
5225 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5226 * them and they are used in vdev_file.c for a similar purpose.
5228 spa_taskq_dispatch_sync(dmu_objset_spa(os), ZIO_TYPE_FREE,
5229 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
5230 #endif /* HAVE_LARGE_STACKS */
5232 return (dbi.dbi_err);
5237 * zc_name name of snapshot to send
5238 * zc_cookie file descriptor to send stream to
5239 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5240 * zc_sendobj objsetid of snapshot to send
5241 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5242 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5243 * output size in zc_objset_type.
5244 * zc_flags lzc_send_flags
5247 * zc_objset_type estimated size, if zc_guid is set
5249 * NOTE: This is no longer the preferred interface, any new functionality
5250 * should be added to zfs_ioc_send_new() instead.
5253 zfs_ioc_send(zfs_cmd_t *zc)
5257 boolean_t estimate = (zc->zc_guid != 0);
5258 boolean_t embedok = (zc->zc_flags & 0x1);
5259 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5260 boolean_t compressok = (zc->zc_flags & 0x4);
5261 boolean_t rawok = (zc->zc_flags & 0x8);
5263 if (zc->zc_obj != 0) {
5265 dsl_dataset_t *tosnap;
5267 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5271 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5273 dsl_pool_rele(dp, FTAG);
5277 if (dsl_dir_is_clone(tosnap->ds_dir))
5279 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5280 dsl_dataset_rele(tosnap, FTAG);
5281 dsl_pool_rele(dp, FTAG);
5286 dsl_dataset_t *tosnap;
5287 dsl_dataset_t *fromsnap = NULL;
5289 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5293 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5296 dsl_pool_rele(dp, FTAG);
5300 if (zc->zc_fromobj != 0) {
5301 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5304 dsl_dataset_rele(tosnap, FTAG);
5305 dsl_pool_rele(dp, FTAG);
5310 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
5311 compressok || rawok, &zc->zc_objset_type);
5313 if (fromsnap != NULL)
5314 dsl_dataset_rele(fromsnap, FTAG);
5315 dsl_dataset_rele(tosnap, FTAG);
5316 dsl_pool_rele(dp, FTAG);
5318 file_t *fp = getf(zc->zc_cookie);
5320 return (SET_ERROR(EBADF));
5323 dmu_send_outparams_t out = {0};
5324 out.dso_outfunc = dump_bytes;
5325 out.dso_arg = fp->f_vnode;
5326 out.dso_dryrun = B_FALSE;
5327 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5328 zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
5329 zc->zc_cookie, &off, &out);
5331 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5333 releasef(zc->zc_cookie);
5340 * zc_name name of snapshot on which to report progress
5341 * zc_cookie file descriptor of send stream
5344 * zc_cookie number of bytes written in send stream thus far
5345 * zc_objset_type logical size of data traversed by send thus far
5348 zfs_ioc_send_progress(zfs_cmd_t *zc)
5352 dmu_sendstatus_t *dsp = NULL;
5355 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5359 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5361 dsl_pool_rele(dp, FTAG);
5365 mutex_enter(&ds->ds_sendstream_lock);
5368 * Iterate over all the send streams currently active on this dataset.
5369 * If there's one which matches the specified file descriptor _and_ the
5370 * stream was started by the current process, return the progress of
5374 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5375 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5376 if (dsp->dss_outfd == zc->zc_cookie &&
5377 dsp->dss_proc == curproc)
5382 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
5384 /* This is the closest thing we have to atomic_read_64. */
5385 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
5387 error = SET_ERROR(ENOENT);
5390 mutex_exit(&ds->ds_sendstream_lock);
5391 dsl_dataset_rele(ds, FTAG);
5392 dsl_pool_rele(dp, FTAG);
5397 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5401 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5402 &zc->zc_inject_record);
5405 zc->zc_guid = (uint64_t)id;
5411 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5413 return (zio_clear_fault((int)zc->zc_guid));
5417 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5419 int id = (int)zc->zc_guid;
5422 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5423 &zc->zc_inject_record);
5431 zfs_ioc_error_log(zfs_cmd_t *zc)
5435 size_t count = (size_t)zc->zc_nvlist_dst_size;
5437 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5440 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5443 zc->zc_nvlist_dst_size = count;
5445 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5447 spa_close(spa, FTAG);
5453 zfs_ioc_clear(zfs_cmd_t *zc)
5460 * On zpool clear we also fix up missing slogs
5462 mutex_enter(&spa_namespace_lock);
5463 spa = spa_lookup(zc->zc_name);
5465 mutex_exit(&spa_namespace_lock);
5466 return (SET_ERROR(EIO));
5468 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5469 /* we need to let spa_open/spa_load clear the chains */
5470 spa_set_log_state(spa, SPA_LOG_CLEAR);
5472 spa->spa_last_open_failed = 0;
5473 mutex_exit(&spa_namespace_lock);
5475 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5476 error = spa_open(zc->zc_name, &spa, FTAG);
5479 nvlist_t *config = NULL;
5481 if (zc->zc_nvlist_src == 0)
5482 return (SET_ERROR(EINVAL));
5484 if ((error = get_nvlist(zc->zc_nvlist_src,
5485 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5486 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5488 if (config != NULL) {
5491 if ((err = put_nvlist(zc, config)) != 0)
5493 nvlist_free(config);
5495 nvlist_free(policy);
5503 * If multihost is enabled, resuming I/O is unsafe as another
5504 * host may have imported the pool.
5506 if (spa_multihost(spa) && spa_suspended(spa))
5507 return (SET_ERROR(EINVAL));
5509 spa_vdev_state_enter(spa, SCL_NONE);
5511 if (zc->zc_guid == 0) {
5514 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5516 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5517 spa_close(spa, FTAG);
5518 return (SET_ERROR(ENODEV));
5522 vdev_clear(spa, vd);
5524 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5525 NULL : spa->spa_root_vdev, 0);
5528 * Resume any suspended I/Os.
5530 if (zio_resume(spa) != 0)
5531 error = SET_ERROR(EIO);
5533 spa_close(spa, FTAG);
5539 * Reopen all the vdevs associated with the pool.
5542 * "scrub_restart" -> when true and scrub is running, allow to restart
5543 * scrub as the side effect of the reopen (boolean).
5548 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5549 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, 0},
5554 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5558 boolean_t scrub_restart = B_TRUE;
5561 scrub_restart = fnvlist_lookup_boolean_value(innvl,
5565 error = spa_open(pool, &spa, FTAG);
5569 spa_vdev_state_enter(spa, SCL_NONE);
5572 * If the scrub_restart flag is B_FALSE and a scrub is already
5573 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5574 * we don't restart the scrub as a side effect of the reopen.
5575 * Otherwise, let vdev_open() decided if a resilver is required.
5578 spa->spa_scrub_reopen = (!scrub_restart &&
5579 dsl_scan_scrubbing(spa->spa_dsl_pool));
5580 vdev_reopen(spa->spa_root_vdev);
5581 spa->spa_scrub_reopen = B_FALSE;
5583 (void) spa_vdev_state_exit(spa, NULL, 0);
5584 spa_close(spa, FTAG);
5590 * zc_name name of filesystem
5593 * zc_string name of conflicting snapshot, if there is one
5596 zfs_ioc_promote(zfs_cmd_t *zc)
5599 dsl_dataset_t *ds, *ods;
5600 char origin[ZFS_MAX_DATASET_NAME_LEN];
5604 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5605 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5606 strchr(zc->zc_name, '%'))
5607 return (SET_ERROR(EINVAL));
5609 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5613 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5615 dsl_pool_rele(dp, FTAG);
5619 if (!dsl_dir_is_clone(ds->ds_dir)) {
5620 dsl_dataset_rele(ds, FTAG);
5621 dsl_pool_rele(dp, FTAG);
5622 return (SET_ERROR(EINVAL));
5625 error = dsl_dataset_hold_obj(dp,
5626 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5628 dsl_dataset_rele(ds, FTAG);
5629 dsl_pool_rele(dp, FTAG);
5633 dsl_dataset_name(ods, origin);
5634 dsl_dataset_rele(ods, FTAG);
5635 dsl_dataset_rele(ds, FTAG);
5636 dsl_pool_rele(dp, FTAG);
5639 * We don't need to unmount *all* the origin fs's snapshots, but
5642 cp = strchr(origin, '@');
5645 (void) dmu_objset_find(origin,
5646 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5647 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5651 * Retrieve a single {user|group|project}{used|quota}@... property.
5654 * zc_name name of filesystem
5655 * zc_objset_type zfs_userquota_prop_t
5656 * zc_value domain name (eg. "S-1-234-567-89")
5657 * zc_guid RID/UID/GID
5660 * zc_cookie property value
5663 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5668 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5669 return (SET_ERROR(EINVAL));
5671 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5675 error = zfs_userspace_one(zfsvfs,
5676 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5677 zfsvfs_rele(zfsvfs, FTAG);
5684 * zc_name name of filesystem
5685 * zc_cookie zap cursor
5686 * zc_objset_type zfs_userquota_prop_t
5687 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5690 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5691 * zc_cookie zap cursor
5694 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5697 int bufsize = zc->zc_nvlist_dst_size;
5700 return (SET_ERROR(ENOMEM));
5702 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5706 void *buf = vmem_alloc(bufsize, KM_SLEEP);
5708 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5709 buf, &zc->zc_nvlist_dst_size);
5712 error = xcopyout(buf,
5713 (void *)(uintptr_t)zc->zc_nvlist_dst,
5714 zc->zc_nvlist_dst_size);
5716 vmem_free(buf, bufsize);
5717 zfsvfs_rele(zfsvfs, FTAG);
5724 * zc_name name of filesystem
5730 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5736 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5737 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5739 * If userused is not enabled, it may be because the
5740 * objset needs to be closed & reopened (to grow the
5741 * objset_phys_t). Suspend/resume the fs will do that.
5743 dsl_dataset_t *ds, *newds;
5745 ds = dmu_objset_ds(zfsvfs->z_os);
5746 error = zfs_suspend_fs(zfsvfs);
5748 dmu_objset_refresh_ownership(ds, &newds,
5750 error = zfs_resume_fs(zfsvfs, newds);
5754 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5755 deactivate_super(zfsvfs->z_sb);
5757 /* XXX kind of reading contents without owning */
5758 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5762 error = dmu_objset_userspace_upgrade(os);
5763 dmu_objset_rele_flags(os, B_TRUE, FTAG);
5771 * zc_name name of filesystem
5777 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5782 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5786 if (dmu_objset_userobjspace_upgradable(os) ||
5787 dmu_objset_projectquota_upgradable(os)) {
5788 mutex_enter(&os->os_upgrade_lock);
5789 if (os->os_upgrade_id == 0) {
5790 /* clear potential error code and retry */
5791 os->os_upgrade_status = 0;
5792 mutex_exit(&os->os_upgrade_lock);
5794 dmu_objset_id_quota_upgrade(os);
5796 mutex_exit(&os->os_upgrade_lock);
5799 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5801 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5802 error = os->os_upgrade_status;
5804 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5807 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
5813 zfs_ioc_share(zfs_cmd_t *zc)
5815 return (SET_ERROR(ENOSYS));
5818 ace_t full_access[] = {
5819 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5824 * zc_name name of containing filesystem
5825 * zc_obj object # beyond which we want next in-use object #
5828 * zc_obj next in-use object #
5831 zfs_ioc_next_obj(zfs_cmd_t *zc)
5833 objset_t *os = NULL;
5836 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5840 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
5842 dmu_objset_rele(os, FTAG);
5848 * zc_name name of filesystem
5849 * zc_value prefix name for snapshot
5850 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5853 * zc_value short name of new snapshot
5856 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5863 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5867 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5868 (u_longlong_t)ddi_get_lbolt64());
5869 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5871 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5874 (void) strlcpy(zc->zc_value, snap_name,
5875 sizeof (zc->zc_value));
5878 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5884 * zc_name name of "to" snapshot
5885 * zc_value name of "from" snapshot
5886 * zc_cookie file descriptor to write diff data on
5889 * dmu_diff_record_t's to the file descriptor
5892 zfs_ioc_diff(zfs_cmd_t *zc)
5898 fp = getf(zc->zc_cookie);
5900 return (SET_ERROR(EBADF));
5904 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5906 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5908 releasef(zc->zc_cookie);
5914 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5916 return (SET_ERROR(ENOTSUP));
5921 * "holds" -> { snapname -> holdname (string), ... }
5922 * (optional) "cleanup_fd" -> fd (int32)
5926 * snapname -> error value (int32)
5930 static const zfs_ioc_key_t zfs_keys_hold[] = {
5931 {"holds", DATA_TYPE_NVLIST, 0},
5932 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5937 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5941 int cleanup_fd = -1;
5945 holds = fnvlist_lookup_nvlist(args, "holds");
5947 /* make sure the user didn't pass us any invalid (empty) tags */
5948 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5949 pair = nvlist_next_nvpair(holds, pair)) {
5952 error = nvpair_value_string(pair, &htag);
5954 return (SET_ERROR(error));
5956 if (strlen(htag) == 0)
5957 return (SET_ERROR(EINVAL));
5960 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5961 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5966 error = dsl_dataset_user_hold(holds, minor, errlist);
5968 zfs_onexit_fd_rele(cleanup_fd);
5973 * innvl is not used.
5976 * holdname -> time added (uint64 seconds since epoch)
5980 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
5986 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5988 return (dsl_dataset_get_holds(snapname, outnvl));
5993 * snapname -> { holdname, ... }
5998 * snapname -> error value (int32)
6002 static const zfs_ioc_key_t zfs_keys_release[] = {
6003 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6008 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6010 return (dsl_dataset_user_release(holds, errlist));
6015 * zc_guid flags (ZEVENT_NONBLOCK)
6016 * zc_cleanup_fd zevent file descriptor
6019 * zc_nvlist_dst next nvlist event
6020 * zc_cookie dropped events since last get
6023 zfs_ioc_events_next(zfs_cmd_t *zc)
6026 nvlist_t *event = NULL;
6028 uint64_t dropped = 0;
6031 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6036 error = zfs_zevent_next(ze, &event,
6037 &zc->zc_nvlist_dst_size, &dropped);
6038 if (event != NULL) {
6039 zc->zc_cookie = dropped;
6040 error = put_nvlist(zc, event);
6044 if (zc->zc_guid & ZEVENT_NONBLOCK)
6047 if ((error == 0) || (error != ENOENT))
6050 error = zfs_zevent_wait(ze);
6055 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
6062 * zc_cookie cleared events count
6065 zfs_ioc_events_clear(zfs_cmd_t *zc)
6069 zfs_zevent_drain_all(&count);
6070 zc->zc_cookie = count;
6077 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6078 * zc_cleanup zevent file descriptor
6081 zfs_ioc_events_seek(zfs_cmd_t *zc)
6087 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6091 error = zfs_zevent_seek(ze, zc->zc_guid);
6092 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
6099 * zc_name name of later filesystem or snapshot
6100 * zc_value full name of old snapshot or bookmark
6103 * zc_cookie space in bytes
6104 * zc_objset_type compressed space in bytes
6105 * zc_perm_action uncompressed space in bytes
6108 zfs_ioc_space_written(zfs_cmd_t *zc)
6114 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6117 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6119 dsl_pool_rele(dp, FTAG);
6122 if (strchr(zc->zc_value, '#') != NULL) {
6123 zfs_bookmark_phys_t bmp;
6124 error = dsl_bookmark_lookup(dp, zc->zc_value,
6127 error = dsl_dataset_space_written_bookmark(&bmp, new,
6129 &zc->zc_objset_type, &zc->zc_perm_action);
6133 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6136 error = dsl_dataset_space_written(old, new,
6138 &zc->zc_objset_type, &zc->zc_perm_action);
6139 dsl_dataset_rele(old, FTAG);
6142 dsl_dataset_rele(new, FTAG);
6143 dsl_pool_rele(dp, FTAG);
6149 * "firstsnap" -> snapshot name
6153 * "used" -> space in bytes
6154 * "compressed" -> compressed space in bytes
6155 * "uncompressed" -> uncompressed space in bytes
6158 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6159 {"firstsnap", DATA_TYPE_STRING, 0},
6163 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6167 dsl_dataset_t *new, *old;
6169 uint64_t used, comp, uncomp;
6171 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6173 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6177 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6178 if (error == 0 && !new->ds_is_snapshot) {
6179 dsl_dataset_rele(new, FTAG);
6180 error = SET_ERROR(EINVAL);
6183 dsl_pool_rele(dp, FTAG);
6186 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6187 if (error == 0 && !old->ds_is_snapshot) {
6188 dsl_dataset_rele(old, FTAG);
6189 error = SET_ERROR(EINVAL);
6192 dsl_dataset_rele(new, FTAG);
6193 dsl_pool_rele(dp, FTAG);
6197 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6198 dsl_dataset_rele(old, FTAG);
6199 dsl_dataset_rele(new, FTAG);
6200 dsl_pool_rele(dp, FTAG);
6201 fnvlist_add_uint64(outnvl, "used", used);
6202 fnvlist_add_uint64(outnvl, "compressed", comp);
6203 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6209 * "fd" -> file descriptor to write stream to (int32)
6210 * (optional) "fromsnap" -> full snap name to send an incremental from
6211 * (optional) "largeblockok" -> (value ignored)
6212 * indicates that blocks > 128KB are permitted
6213 * (optional) "embedok" -> (value ignored)
6214 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6215 * (optional) "compressok" -> (value ignored)
6216 * presence indicates compressed DRR_WRITE records are permitted
6217 * (optional) "rawok" -> (value ignored)
6218 * presence indicates raw encrypted records should be used.
6219 * (optional) "resume_object" and "resume_offset" -> (uint64)
6220 * if present, resume send stream from specified object and offset.
6221 * (optional) "redactbook" -> (string)
6222 * if present, use this bookmark's redaction list to generate a redacted
6228 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6229 {"fd", DATA_TYPE_INT32, 0},
6230 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6231 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6232 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6233 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6234 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6235 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6236 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6237 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6242 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6246 char *fromname = NULL;
6249 boolean_t largeblockok;
6251 boolean_t compressok;
6253 uint64_t resumeobj = 0;
6254 uint64_t resumeoff = 0;
6255 char *redactbook = NULL;
6257 fd = fnvlist_lookup_int32(innvl, "fd");
6259 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6261 largeblockok = nvlist_exists(innvl, "largeblockok");
6262 embedok = nvlist_exists(innvl, "embedok");
6263 compressok = nvlist_exists(innvl, "compressok");
6264 rawok = nvlist_exists(innvl, "rawok");
6266 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6267 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6269 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6271 if ((fp = getf(fd)) == NULL)
6272 return (SET_ERROR(EBADF));
6275 dmu_send_outparams_t out = {0};
6276 out.dso_outfunc = dump_bytes;
6277 out.dso_arg = fp->f_vnode;
6278 out.dso_dryrun = B_FALSE;
6279 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6280 rawok, resumeobj, resumeoff, redactbook, fd, &off, &out);
6282 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
6291 send_space_sum(objset_t *os, void *buf, int len, void *arg)
6293 uint64_t *size = arg;
6299 * Determine approximately how large a zfs send stream will be -- the number
6300 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6303 * (optional) "from" -> full snap or bookmark name to send an incremental
6305 * (optional) "largeblockok" -> (value ignored)
6306 * indicates that blocks > 128KB are permitted
6307 * (optional) "embedok" -> (value ignored)
6308 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6309 * (optional) "compressok" -> (value ignored)
6310 * presence indicates compressed DRR_WRITE records are permitted
6311 * (optional) "rawok" -> (value ignored)
6312 * presence indicates raw encrypted records should be used.
6313 * (optional) "fd" -> file descriptor to use as a cookie for progress
6318 * "space" -> bytes of space (uint64)
6321 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6322 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6323 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6324 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6325 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6326 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6327 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6328 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6329 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6330 {"resumeobj", DATA_TYPE_UINT64, ZK_OPTIONAL},
6331 {"resumeoff", DATA_TYPE_UINT64, ZK_OPTIONAL},
6332 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
6336 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6339 dsl_dataset_t *tosnap;
6340 dsl_dataset_t *fromsnap = NULL;
6342 char *fromname = NULL;
6343 char *redactlist_book = NULL;
6344 boolean_t largeblockok;
6346 boolean_t compressok;
6349 boolean_t full_estimate = B_FALSE;
6350 uint64_t resumeobj = 0;
6351 uint64_t resumeoff = 0;
6352 uint64_t resume_bytes = 0;
6354 zfs_bookmark_phys_t zbm = {0};
6356 error = dsl_pool_hold(snapname, FTAG, &dp);
6360 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6362 dsl_pool_rele(dp, FTAG);
6365 (void) nvlist_lookup_int32(innvl, "fd", &fd);
6367 largeblockok = nvlist_exists(innvl, "largeblockok");
6368 embedok = nvlist_exists(innvl, "embedok");
6369 compressok = nvlist_exists(innvl, "compressok");
6370 rawok = nvlist_exists(innvl, "rawok");
6371 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
6372 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
6373 &redactlist_book) == 0);
6375 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6376 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6377 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
6380 full_estimate = B_TRUE;
6382 if (strchr(fromname, '#')) {
6383 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
6386 * dsl_bookmark_lookup() will fail with EXDEV if
6387 * the from-bookmark and tosnap are at the same txg.
6388 * However, it's valid to do a send (and therefore,
6389 * a send estimate) from and to the same time point,
6390 * if the bookmark is redacted (the incremental send
6391 * can change what's redacted on the target). In
6392 * this case, dsl_bookmark_lookup() fills in zbm
6393 * but returns EXDEV. Ignore this error.
6395 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
6397 dsl_dataset_phys(tosnap)->ds_guid)
6401 dsl_dataset_rele(tosnap, FTAG);
6402 dsl_pool_rele(dp, FTAG);
6405 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
6406 ZBM_FLAG_HAS_FBN)) {
6407 full_estimate = B_TRUE;
6409 } else if (strchr(fromname, '@')) {
6410 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6412 dsl_dataset_rele(tosnap, FTAG);
6413 dsl_pool_rele(dp, FTAG);
6417 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
6418 full_estimate = B_TRUE;
6419 dsl_dataset_rele(fromsnap, FTAG);
6423 * from is not properly formatted as a snapshot or
6426 dsl_dataset_rele(tosnap, FTAG);
6427 dsl_pool_rele(dp, FTAG);
6428 return (SET_ERROR(EINVAL));
6432 if (full_estimate) {
6433 dmu_send_outparams_t out = {0};
6435 out.dso_outfunc = send_space_sum;
6436 out.dso_arg = &space;
6437 out.dso_dryrun = B_TRUE;
6439 * We have to release these holds so dmu_send can take them. It
6440 * will do all the error checking we need.
6442 dsl_dataset_rele(tosnap, FTAG);
6443 dsl_pool_rele(dp, FTAG);
6444 error = dmu_send(snapname, fromname, embedok, largeblockok,
6445 compressok, rawok, resumeobj, resumeoff, redactlist_book,
6448 error = dmu_send_estimate_fast(tosnap, fromsnap,
6449 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
6450 compressok || rawok, &space);
6451 space -= resume_bytes;
6452 if (fromsnap != NULL)
6453 dsl_dataset_rele(fromsnap, FTAG);
6454 dsl_dataset_rele(tosnap, FTAG);
6455 dsl_pool_rele(dp, FTAG);
6458 fnvlist_add_uint64(outnvl, "space", space);
6464 * Sync the currently open TXG to disk for the specified pool.
6465 * This is somewhat similar to 'zfs_sync()'.
6466 * For cases that do not result in error this ioctl will wait for
6467 * the currently open TXG to commit before returning back to the caller.
6470 * "force" -> when true, force uberblock update even if there is no dirty data.
6471 * In addition this will cause the vdev configuration to be written
6472 * out including updating the zpool cache file. (boolean_t)
6477 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6478 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6483 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6486 boolean_t force = B_FALSE;
6489 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6493 force = fnvlist_lookup_boolean_value(innvl, "force");
6496 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6497 vdev_config_dirty(spa->spa_root_vdev);
6498 spa_config_exit(spa, SCL_CONFIG, FTAG);
6500 txg_wait_synced(spa_get_dsl(spa), 0);
6502 spa_close(spa, FTAG);
6508 * Load a user's wrapping key into the kernel.
6510 * "hidden_args" -> { "wkeydata" -> value }
6511 * raw uint8_t array of encryption wrapping key data (32 bytes)
6512 * (optional) "noop" -> (value ignored)
6513 * presence indicated key should only be verified, not loaded
6516 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6517 {"hidden_args", DATA_TYPE_NVLIST, 0},
6518 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6523 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6526 dsl_crypto_params_t *dcp = NULL;
6527 nvlist_t *hidden_args;
6528 boolean_t noop = nvlist_exists(innvl, "noop");
6530 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6531 ret = SET_ERROR(EINVAL);
6535 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6537 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6542 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6546 dsl_crypto_params_free(dcp, noop);
6551 dsl_crypto_params_free(dcp, B_TRUE);
6556 * Unload a user's wrapping key from the kernel.
6557 * Both innvl and outnvl are unused.
6559 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6565 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6569 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6570 ret = (SET_ERROR(EINVAL));
6574 ret = spa_keystore_unload_wkey(dsname);
6583 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6584 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6585 * here to change how the key is derived in userspace.
6588 * "hidden_args" (optional) -> { "wkeydata" -> value }
6589 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6590 * "props" (optional) -> { prop -> value }
6595 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6596 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6597 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6598 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6603 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6606 uint64_t cmd = DCP_CMD_NONE;
6607 dsl_crypto_params_t *dcp = NULL;
6608 nvlist_t *args = NULL, *hidden_args = NULL;
6610 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6611 ret = (SET_ERROR(EINVAL));
6615 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6616 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6617 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6619 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6623 ret = spa_keystore_change_key(dsname, dcp);
6627 dsl_crypto_params_free(dcp, B_FALSE);
6632 dsl_crypto_params_free(dcp, B_TRUE);
6636 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6639 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6640 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6641 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6643 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6645 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6646 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6647 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6648 ASSERT3P(vec->zvec_func, ==, NULL);
6650 vec->zvec_legacy_func = func;
6651 vec->zvec_secpolicy = secpolicy;
6652 vec->zvec_namecheck = namecheck;
6653 vec->zvec_allow_log = log_history;
6654 vec->zvec_pool_check = pool_check;
6658 * See the block comment at the beginning of this file for details on
6659 * each argument to this function.
6662 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6663 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6664 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6665 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6667 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6669 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6670 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6671 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6672 ASSERT3P(vec->zvec_func, ==, NULL);
6674 /* if we are logging, the name must be valid */
6675 ASSERT(!allow_log || namecheck != NO_NAME);
6677 vec->zvec_name = name;
6678 vec->zvec_func = func;
6679 vec->zvec_secpolicy = secpolicy;
6680 vec->zvec_namecheck = namecheck;
6681 vec->zvec_pool_check = pool_check;
6682 vec->zvec_smush_outnvlist = smush_outnvlist;
6683 vec->zvec_allow_log = allow_log;
6684 vec->zvec_nvl_keys = nvl_keys;
6685 vec->zvec_nvl_key_count = num_keys;
6689 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6690 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6691 zfs_ioc_poolcheck_t pool_check)
6693 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6694 POOL_NAME, log_history, pool_check);
6698 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6699 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6701 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6702 DATASET_NAME, B_FALSE, pool_check);
6706 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6708 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6709 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6713 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6714 zfs_secpolicy_func_t *secpolicy)
6716 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6717 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6721 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6722 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6724 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6725 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6729 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6731 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6732 zfs_secpolicy_read);
6736 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6737 zfs_secpolicy_func_t *secpolicy)
6739 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6740 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6744 zfs_ioctl_init(void)
6746 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6747 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6748 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6749 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6751 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6752 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6753 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6754 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6756 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6757 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6758 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6759 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6761 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6762 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6763 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6764 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6766 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6767 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6768 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6769 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6771 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6772 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6773 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6774 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6776 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6777 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6778 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6779 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6781 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6782 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
6783 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6784 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6786 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6787 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6788 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6789 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6791 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6792 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6793 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6794 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6795 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6796 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6797 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6798 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6800 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6801 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6802 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6803 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6805 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6806 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6807 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6808 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6810 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6811 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6812 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6813 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6815 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6816 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6817 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6818 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6820 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
6821 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
6822 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
6823 ARRAY_SIZE(zfs_keys_get_bookmark_props));
6825 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6826 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6828 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6829 zfs_keys_destroy_bookmarks,
6830 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6832 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6833 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
6834 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6835 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
6836 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6837 zfs_ioc_load_key, zfs_secpolicy_load_key,
6838 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6839 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
6840 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6841 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6842 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6843 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
6844 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6845 zfs_ioc_change_key, zfs_secpolicy_change_key,
6846 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6847 B_TRUE, B_TRUE, zfs_keys_change_key,
6848 ARRAY_SIZE(zfs_keys_change_key));
6850 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6851 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6852 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6853 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6854 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6855 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
6856 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
6858 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6859 zfs_ioc_channel_program, zfs_secpolicy_config,
6860 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6861 B_TRUE, zfs_keys_channel_program,
6862 ARRAY_SIZE(zfs_keys_channel_program));
6864 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
6865 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
6866 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6867 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
6869 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6870 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6871 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6872 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6874 zfs_ioctl_register("zpool_discard_checkpoint",
6875 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6876 zfs_secpolicy_config, POOL_NAME,
6877 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6878 zfs_keys_pool_discard_checkpoint,
6879 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
6881 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6882 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6883 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6884 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
6886 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
6887 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
6888 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6889 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
6891 /* IOCTLS that use the legacy function signature */
6893 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6894 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6896 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6897 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6898 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6900 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6901 zfs_ioc_pool_upgrade);
6902 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6904 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6905 zfs_ioc_vdev_remove);
6906 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6907 zfs_ioc_vdev_set_state);
6908 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6909 zfs_ioc_vdev_attach);
6910 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6911 zfs_ioc_vdev_detach);
6912 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6913 zfs_ioc_vdev_setpath);
6914 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6915 zfs_ioc_vdev_setfru);
6916 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6917 zfs_ioc_pool_set_props);
6918 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6919 zfs_ioc_vdev_split);
6920 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6921 zfs_ioc_pool_reguid);
6923 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6924 zfs_ioc_pool_configs, zfs_secpolicy_none);
6925 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6926 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6927 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6928 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6929 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6930 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6931 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6932 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6935 * pool destroy, and export don't log the history as part of
6936 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6937 * does the logging of those commands.
6939 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6940 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6941 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6942 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6944 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6945 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6946 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6947 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6949 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6950 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6951 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6952 zfs_ioc_dsobj_to_dsname,
6953 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6954 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6955 zfs_ioc_pool_get_history,
6956 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6958 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6959 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6961 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6962 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6964 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6965 zfs_ioc_space_written);
6966 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6967 zfs_ioc_objset_recvd_props);
6968 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6970 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6972 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6973 zfs_ioc_objset_stats);
6974 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6975 zfs_ioc_objset_zplprops);
6976 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6977 zfs_ioc_dataset_list_next);
6978 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6979 zfs_ioc_snapshot_list_next);
6980 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6981 zfs_ioc_send_progress);
6983 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6984 zfs_ioc_diff, zfs_secpolicy_diff);
6985 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6986 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6987 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6988 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6989 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6990 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6991 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6992 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6993 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6994 zfs_ioc_send, zfs_secpolicy_send);
6996 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6997 zfs_secpolicy_none);
6998 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6999 zfs_secpolicy_destroy);
7000 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7001 zfs_secpolicy_rename);
7002 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7003 zfs_secpolicy_recv);
7004 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7005 zfs_secpolicy_promote);
7006 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7007 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7008 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7009 zfs_secpolicy_set_fsacl);
7011 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7012 zfs_secpolicy_share, POOL_CHECK_NONE);
7013 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7014 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7015 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7016 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7017 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7018 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7019 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7020 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7025 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7026 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7027 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7028 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7029 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7030 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7034 * Verify that for non-legacy ioctls the input nvlist
7035 * pairs match against the expected input.
7037 * Possible errors are:
7038 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7039 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7040 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7043 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7045 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7046 boolean_t required_keys_found = B_FALSE;
7049 * examine each input pair
7051 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7052 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7053 char *name = nvpair_name(pair);
7054 data_type_t type = nvpair_type(pair);
7055 boolean_t identified = B_FALSE;
7058 * check pair against the documented names and type
7060 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7061 /* if not a wild card name, check for an exact match */
7062 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7063 strcmp(nvl_keys[k].zkey_name, name) != 0)
7066 identified = B_TRUE;
7068 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7069 nvl_keys[k].zkey_type != type) {
7070 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7073 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7076 required_keys_found = B_TRUE;
7080 /* allow an 'optional' key, everything else is invalid */
7082 (strcmp(name, "optional") != 0 ||
7083 type != DATA_TYPE_NVLIST)) {
7084 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7088 /* verify that all required keys were found */
7089 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7090 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7093 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7094 /* at least one non-optionial key is expected here */
7095 if (!required_keys_found)
7096 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7100 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7101 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7108 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7109 zfs_ioc_poolcheck_t check)
7114 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7115 type == ENTITY_NAME);
7117 if (check & POOL_CHECK_NONE)
7120 error = spa_open(name, &spa, FTAG);
7122 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7123 error = SET_ERROR(EAGAIN);
7124 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7125 error = SET_ERROR(EROFS);
7126 spa_close(spa, FTAG);
7132 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
7136 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7137 if (zs->zs_minor == minor) {
7141 return (zs->zs_onexit);
7143 return (zs->zs_zevent);
7154 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
7158 ptr = zfsdev_get_state_impl(minor, which);
7164 zfsdev_getminor(struct file *filp, minor_t *minorp)
7166 zfsdev_state_t *zs, *fpd;
7168 ASSERT(filp != NULL);
7169 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7171 fpd = filp->private_data;
7173 return (SET_ERROR(EBADF));
7175 mutex_enter(&zfsdev_state_lock);
7177 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7179 if (zs->zs_minor == -1)
7183 *minorp = fpd->zs_minor;
7184 mutex_exit(&zfsdev_state_lock);
7189 mutex_exit(&zfsdev_state_lock);
7191 return (SET_ERROR(EBADF));
7195 * Find a free minor number. The zfsdev_state_list is expected to
7196 * be short since it is only a list of currently open file handles.
7199 zfsdev_minor_alloc(void)
7201 static minor_t last_minor = 0;
7204 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7206 for (m = last_minor + 1; m != last_minor; m++) {
7207 if (m > ZFSDEV_MAX_MINOR)
7209 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
7219 zfsdev_state_init(struct file *filp)
7221 zfsdev_state_t *zs, *zsprev = NULL;
7223 boolean_t newzs = B_FALSE;
7225 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7227 minor = zfsdev_minor_alloc();
7229 return (SET_ERROR(ENXIO));
7231 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7232 if (zs->zs_minor == -1)
7238 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7243 filp->private_data = zs;
7245 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7246 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7250 * In order to provide for lock-free concurrent read access
7251 * to the minor list in zfsdev_get_state_impl(), new entries
7252 * must be completely written before linking them into the
7253 * list whereas existing entries are already linked; the last
7254 * operation must be updating zs_minor (from -1 to the new
7258 zs->zs_minor = minor;
7260 zsprev->zs_next = zs;
7263 zs->zs_minor = minor;
7270 zfsdev_state_destroy(struct file *filp)
7274 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7275 ASSERT(filp->private_data != NULL);
7277 zs = filp->private_data;
7279 zfs_onexit_destroy(zs->zs_onexit);
7280 zfs_zevent_destroy(zs->zs_zevent);
7286 zfsdev_open(struct inode *ino, struct file *filp)
7290 mutex_enter(&zfsdev_state_lock);
7291 error = zfsdev_state_init(filp);
7292 mutex_exit(&zfsdev_state_lock);
7298 zfsdev_release(struct inode *ino, struct file *filp)
7302 mutex_enter(&zfsdev_state_lock);
7303 error = zfsdev_state_destroy(filp);
7304 mutex_exit(&zfsdev_state_lock);
7310 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
7314 int error, rc, flag = 0;
7315 const zfs_ioc_vec_t *vec;
7316 char *saved_poolname = NULL;
7317 nvlist_t *innvl = NULL;
7318 fstrans_cookie_t cookie;
7320 vecnum = cmd - ZFS_IOC_FIRST;
7321 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7322 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7323 vec = &zfs_ioc_vec[vecnum];
7326 * The registered ioctl list may be sparse, verify that either
7327 * a normal or legacy handler are registered.
7329 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7330 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7332 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
7334 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
7336 error = SET_ERROR(EFAULT);
7340 zc->zc_iflags = flag & FKIOCTL;
7341 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
7343 * Make sure the user doesn't pass in an insane value for
7344 * zc_nvlist_src_size. We have to check, since we will end
7345 * up allocating that much memory inside of get_nvlist(). This
7346 * prevents a nefarious user from allocating tons of kernel
7349 * Also, we return EINVAL instead of ENOMEM here. The reason
7350 * being that returning ENOMEM from an ioctl() has a special
7351 * connotation; that the user's size value is too small and
7352 * needs to be expanded to hold the nvlist. See
7353 * zcmd_expand_dst_nvlist() for details.
7355 error = SET_ERROR(EINVAL); /* User's size too big */
7357 } else if (zc->zc_nvlist_src_size != 0) {
7358 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7359 zc->zc_iflags, &innvl);
7365 * Ensure that all pool/dataset names are valid before we pass down to
7368 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7369 switch (vec->zvec_namecheck) {
7371 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7372 error = SET_ERROR(EINVAL);
7374 error = pool_status_check(zc->zc_name,
7375 vec->zvec_namecheck, vec->zvec_pool_check);
7379 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7380 error = SET_ERROR(EINVAL);
7382 error = pool_status_check(zc->zc_name,
7383 vec->zvec_namecheck, vec->zvec_pool_check);
7387 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7388 error = SET_ERROR(EINVAL);
7390 error = pool_status_check(zc->zc_name,
7391 vec->zvec_namecheck, vec->zvec_pool_check);
7399 * Ensure that all input pairs are valid before we pass them down
7400 * to the lower layers.
7402 * The vectored functions can use fnvlist_lookup_{type} for any
7403 * required pairs since zfs_check_input_nvpairs() confirmed that
7404 * they exist and are of the correct type.
7406 if (error == 0 && vec->zvec_func != NULL) {
7407 error = zfs_check_input_nvpairs(innvl, vec);
7413 cookie = spl_fstrans_mark();
7414 error = vec->zvec_secpolicy(zc, innvl, CRED());
7415 spl_fstrans_unmark(cookie);
7421 /* legacy ioctls can modify zc_name */
7422 saved_poolname = strdup(zc->zc_name);
7423 if (saved_poolname == NULL) {
7424 error = SET_ERROR(ENOMEM);
7427 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7430 if (vec->zvec_func != NULL) {
7434 nvlist_t *lognv = NULL;
7436 ASSERT(vec->zvec_legacy_func == NULL);
7439 * Add the innvl to the lognv before calling the func,
7440 * in case the func changes the innvl.
7442 if (vec->zvec_allow_log) {
7443 lognv = fnvlist_alloc();
7444 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7446 if (!nvlist_empty(innvl)) {
7447 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7452 outnvl = fnvlist_alloc();
7453 cookie = spl_fstrans_mark();
7454 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7455 spl_fstrans_unmark(cookie);
7458 * Some commands can partially execute, modify state, and still
7459 * return an error. In these cases, attempt to record what
7463 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7464 vec->zvec_allow_log &&
7465 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7466 if (!nvlist_empty(outnvl)) {
7467 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7471 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7474 (void) spa_history_log_nvl(spa, lognv);
7475 spa_close(spa, FTAG);
7477 fnvlist_free(lognv);
7479 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7481 if (vec->zvec_smush_outnvlist) {
7482 smusherror = nvlist_smush(outnvl,
7483 zc->zc_nvlist_dst_size);
7485 if (smusherror == 0)
7486 puterror = put_nvlist(zc, outnvl);
7492 nvlist_free(outnvl);
7494 cookie = spl_fstrans_mark();
7495 error = vec->zvec_legacy_func(zc);
7496 spl_fstrans_unmark(cookie);
7501 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
7502 if (error == 0 && rc != 0)
7503 error = SET_ERROR(EFAULT);
7504 if (error == 0 && vec->zvec_allow_log) {
7505 char *s = tsd_get(zfs_allow_log_key);
7508 (void) tsd_set(zfs_allow_log_key, saved_poolname);
7510 if (saved_poolname != NULL)
7511 strfree(saved_poolname);
7514 kmem_free(zc, sizeof (zfs_cmd_t));
7518 #ifdef CONFIG_COMPAT
7520 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
7522 return (zfsdev_ioctl(filp, cmd, arg));
7525 #define zfsdev_compat_ioctl NULL
7528 static const struct file_operations zfsdev_fops = {
7529 .open = zfsdev_open,
7530 .release = zfsdev_release,
7531 .unlocked_ioctl = zfsdev_ioctl,
7532 .compat_ioctl = zfsdev_compat_ioctl,
7533 .owner = THIS_MODULE,
7536 static struct miscdevice zfs_misc = {
7537 .minor = ZFS_DEVICE_MINOR,
7539 .fops = &zfsdev_fops,
7542 MODULE_ALIAS_MISCDEV(ZFS_DEVICE_MINOR);
7543 MODULE_ALIAS("devname:zfs");
7550 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7551 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7552 zfsdev_state_list->zs_minor = -1;
7554 error = misc_register(&zfs_misc);
7555 if (error == -EBUSY) {
7557 * Fallback to dynamic minor allocation in the event of a
7558 * collision with a reserved minor in linux/miscdevice.h.
7559 * In this case the kernel modules must be manually loaded.
7561 printk(KERN_INFO "ZFS: misc_register() with static minor %d "
7562 "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
7563 ZFS_DEVICE_MINOR, error);
7565 zfs_misc.minor = MISC_DYNAMIC_MINOR;
7566 error = misc_register(&zfs_misc);
7570 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
7578 zfsdev_state_t *zs, *zsprev = NULL;
7580 misc_deregister(&zfs_misc);
7581 mutex_destroy(&zfsdev_state_lock);
7583 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7585 kmem_free(zsprev, sizeof (zfsdev_state_t));
7589 kmem_free(zsprev, sizeof (zfsdev_state_t));
7593 zfs_allow_log_destroy(void *arg)
7595 char *poolname = arg;
7597 if (poolname != NULL)
7602 #define ZFS_DEBUG_STR " (DEBUG mode)"
7604 #define ZFS_DEBUG_STR ""
7612 if ((error = -zvol_init()) != 0)
7615 spa_init(FREAD | FWRITE);
7621 if ((error = zfs_attach()) != 0)
7624 tsd_create(&zfs_fsyncer_key, NULL);
7625 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7626 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7628 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
7629 "ZFS pool version %s, ZFS filesystem version %s\n",
7630 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
7631 SPA_VERSION_STRING, ZPL_VERSION_STRING);
7632 #ifndef CONFIG_FS_POSIX_ACL
7633 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
7634 #endif /* CONFIG_FS_POSIX_ACL */
7643 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
7644 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
7645 ZFS_DEBUG_STR, error);
7659 tsd_destroy(&zfs_fsyncer_key);
7660 tsd_destroy(&rrw_tsd_key);
7661 tsd_destroy(&zfs_allow_log_key);
7663 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
7664 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
7667 #if defined(_KERNEL)
7671 MODULE_DESCRIPTION("ZFS");
7672 MODULE_AUTHOR(ZFS_META_AUTHOR);
7673 MODULE_LICENSE(ZFS_META_LICENSE);
7674 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);