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_tx.h>
180 #include <sys/sunddi.h>
181 #include <sys/policy.h>
182 #include <sys/zone.h>
183 #include <sys/nvpair.h>
184 #include <sys/pathname.h>
186 #include <sys/fs/zfs.h>
187 #include <sys/zfs_ctldir.h>
188 #include <sys/zfs_dir.h>
189 #include <sys/zfs_onexit.h>
190 #include <sys/zvol.h>
191 #include <sys/dsl_scan.h>
192 #include <sys/fm/util.h>
193 #include <sys/dsl_crypt.h>
195 #include <sys/dmu_recv.h>
196 #include <sys/dmu_send.h>
197 #include <sys/dsl_destroy.h>
198 #include <sys/dsl_bookmark.h>
199 #include <sys/dsl_userhold.h>
200 #include <sys/zfeature.h>
202 #include <sys/zio_checksum.h>
203 #include <sys/vdev_removal.h>
204 #include <sys/zfs_sysfs.h>
205 #include <sys/vdev_impl.h>
206 #include <sys/vdev_initialize.h>
207 #include <sys/vdev_trim.h>
209 #include <linux/miscdevice.h>
210 #include <linux/slab.h>
212 #include "zfs_namecheck.h"
213 #include "zfs_prop.h"
214 #include "zfs_deleg.h"
215 #include "zfs_comutil.h"
217 #include <sys/lua/lua.h>
218 #include <sys/lua/lauxlib.h>
221 * Limit maximum nvlist size. We don't want users passing in insane values
222 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
224 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
226 kmutex_t zfsdev_state_lock;
227 zfsdev_state_t *zfsdev_state_list;
229 extern void zfs_init(void);
230 extern void zfs_fini(void);
232 uint_t zfs_fsyncer_key;
233 extern uint_t rrw_tsd_key;
234 static uint_t zfs_allow_log_key;
236 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
237 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
238 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
241 * IOC Keys are used to document and validate user->kernel interface inputs.
242 * See zfs_keys_recv_new for an example declaration. Any key name that is not
243 * listed will be rejected as input.
245 * The keyname 'optional' is always allowed, and must be an nvlist if present.
246 * Arguments which older kernels can safely ignore can be placed under the
249 * When adding new keys to an existing ioc for new functionality, consider:
250 * - adding an entry into zfs_sysfs.c zfs_features[] list
251 * - updating the libzfs_input_check.c test utility
253 * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
254 * for the expected name (bookmark, snapshot, property, etc) but there
255 * is no validation in the preflight zfs_check_input_nvpairs() check.
258 ZK_OPTIONAL = 1 << 0, /* pair is optional */
259 ZK_WILDCARDLIST = 1 << 1, /* one or more unspecified key names */
262 /* DATA_TYPE_ANY is used when zkey_type can vary. */
263 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
265 typedef struct zfs_ioc_key {
266 const char *zkey_name;
267 data_type_t zkey_type;
268 ioc_key_flag_t zkey_flags;
275 } zfs_ioc_namecheck_t;
278 POOL_CHECK_NONE = 1 << 0,
279 POOL_CHECK_SUSPENDED = 1 << 1,
280 POOL_CHECK_READONLY = 1 << 2,
281 } zfs_ioc_poolcheck_t;
283 typedef struct zfs_ioc_vec {
284 zfs_ioc_legacy_func_t *zvec_legacy_func;
285 zfs_ioc_func_t *zvec_func;
286 zfs_secpolicy_func_t *zvec_secpolicy;
287 zfs_ioc_namecheck_t zvec_namecheck;
288 boolean_t zvec_allow_log;
289 zfs_ioc_poolcheck_t zvec_pool_check;
290 boolean_t zvec_smush_outnvlist;
291 const char *zvec_name;
292 const zfs_ioc_key_t *zvec_nvl_keys;
293 size_t zvec_nvl_key_count;
296 /* This array is indexed by zfs_userquota_prop_t */
297 static const char *userquota_perms[] = {
298 ZFS_DELEG_PERM_USERUSED,
299 ZFS_DELEG_PERM_USERQUOTA,
300 ZFS_DELEG_PERM_GROUPUSED,
301 ZFS_DELEG_PERM_GROUPQUOTA,
302 ZFS_DELEG_PERM_USEROBJUSED,
303 ZFS_DELEG_PERM_USEROBJQUOTA,
304 ZFS_DELEG_PERM_GROUPOBJUSED,
305 ZFS_DELEG_PERM_GROUPOBJQUOTA,
306 ZFS_DELEG_PERM_PROJECTUSED,
307 ZFS_DELEG_PERM_PROJECTQUOTA,
308 ZFS_DELEG_PERM_PROJECTOBJUSED,
309 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
312 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
313 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
314 static int zfs_check_settable(const char *name, nvpair_t *property,
316 static int zfs_check_clearable(char *dataset, nvlist_t *props,
318 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
320 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
321 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
324 history_str_free(char *buf)
326 kmem_free(buf, HIS_MAX_RECORD_LEN);
330 history_str_get(zfs_cmd_t *zc)
334 if (zc->zc_history == 0)
337 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
338 if (copyinstr((void *)(uintptr_t)zc->zc_history,
339 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
340 history_str_free(buf);
344 buf[HIS_MAX_RECORD_LEN -1] = '\0';
350 * Check to see if the named dataset is currently defined as bootable
353 zfs_is_bootfs(const char *name)
357 if (dmu_objset_hold(name, FTAG, &os) == 0) {
359 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
360 dmu_objset_rele(os, FTAG);
367 * Return non-zero if the spa version is less than requested version.
370 zfs_earlier_version(const char *name, int version)
374 if (spa_open(name, &spa, FTAG) == 0) {
375 if (spa_version(spa) < version) {
376 spa_close(spa, FTAG);
379 spa_close(spa, FTAG);
385 * Return TRUE if the ZPL version is less than requested version.
388 zpl_earlier_version(const char *name, int version)
391 boolean_t rc = B_TRUE;
393 if (dmu_objset_hold(name, FTAG, &os) == 0) {
396 if (dmu_objset_type(os) != DMU_OST_ZFS) {
397 dmu_objset_rele(os, FTAG);
400 /* XXX reading from non-owned objset */
401 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
402 rc = zplversion < version;
403 dmu_objset_rele(os, FTAG);
409 zfs_log_history(zfs_cmd_t *zc)
414 if ((buf = history_str_get(zc)) == NULL)
417 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
418 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
419 (void) spa_history_log(spa, buf);
420 spa_close(spa, FTAG);
422 history_str_free(buf);
426 * Policy for top-level read operations (list pools). Requires no privileges,
427 * and can be used in the local zone, as there is no associated dataset.
431 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
437 * Policy for dataset read operations (list children, get statistics). Requires
438 * no privileges, but must be visible in the local zone.
442 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
444 if (INGLOBALZONE(curproc) ||
445 zone_dataset_visible(zc->zc_name, NULL))
448 return (SET_ERROR(ENOENT));
452 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
457 * The dataset must be visible by this zone -- check this first
458 * so they don't see EPERM on something they shouldn't know about.
460 if (!INGLOBALZONE(curproc) &&
461 !zone_dataset_visible(dataset, &writable))
462 return (SET_ERROR(ENOENT));
464 if (INGLOBALZONE(curproc)) {
466 * If the fs is zoned, only root can access it from the
469 if (secpolicy_zfs(cr) && zoned)
470 return (SET_ERROR(EPERM));
473 * If we are in a local zone, the 'zoned' property must be set.
476 return (SET_ERROR(EPERM));
478 /* must be writable by this zone */
480 return (SET_ERROR(EPERM));
486 zfs_dozonecheck(const char *dataset, cred_t *cr)
490 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
491 return (SET_ERROR(ENOENT));
493 return (zfs_dozonecheck_impl(dataset, zoned, cr));
497 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
501 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
502 return (SET_ERROR(ENOENT));
504 return (zfs_dozonecheck_impl(dataset, zoned, cr));
508 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
509 const char *perm, cred_t *cr)
513 error = zfs_dozonecheck_ds(name, ds, cr);
515 error = secpolicy_zfs(cr);
517 error = dsl_deleg_access_impl(ds, perm, cr);
523 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
530 * First do a quick check for root in the global zone, which
531 * is allowed to do all write_perms. This ensures that zfs_ioc_*
532 * will get to handle nonexistent datasets.
534 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
537 error = dsl_pool_hold(name, FTAG, &dp);
541 error = dsl_dataset_hold(dp, name, FTAG, &ds);
543 dsl_pool_rele(dp, FTAG);
547 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
549 dsl_dataset_rele(ds, FTAG);
550 dsl_pool_rele(dp, FTAG);
555 * Policy for setting the security label property.
557 * Returns 0 for success, non-zero for access and other errors.
560 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
563 char ds_hexsl[MAXNAMELEN];
564 bslabel_t ds_sl, new_sl;
565 boolean_t new_default = FALSE;
567 int needed_priv = -1;
570 /* First get the existing dataset label. */
571 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
572 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
574 return (SET_ERROR(EPERM));
576 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
579 /* The label must be translatable */
580 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
581 return (SET_ERROR(EINVAL));
584 * In a non-global zone, disallow attempts to set a label that
585 * doesn't match that of the zone; otherwise no other checks
588 if (!INGLOBALZONE(curproc)) {
589 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
590 return (SET_ERROR(EPERM));
595 * For global-zone datasets (i.e., those whose zoned property is
596 * "off", verify that the specified new label is valid for the
599 if (dsl_prop_get_integer(name,
600 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
601 return (SET_ERROR(EPERM));
603 if (zfs_check_global_label(name, strval) != 0)
604 return (SET_ERROR(EPERM));
608 * If the existing dataset label is nondefault, check if the
609 * dataset is mounted (label cannot be changed while mounted).
610 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
611 * mounted (or isn't a dataset, doesn't exist, ...).
613 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
615 static char *setsl_tag = "setsl_tag";
618 * Try to own the dataset; abort if there is any error,
619 * (e.g., already mounted, in use, or other error).
621 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
624 return (SET_ERROR(EPERM));
626 dmu_objset_disown(os, B_TRUE, setsl_tag);
629 needed_priv = PRIV_FILE_DOWNGRADE_SL;
633 if (hexstr_to_label(strval, &new_sl) != 0)
634 return (SET_ERROR(EPERM));
636 if (blstrictdom(&ds_sl, &new_sl))
637 needed_priv = PRIV_FILE_DOWNGRADE_SL;
638 else if (blstrictdom(&new_sl, &ds_sl))
639 needed_priv = PRIV_FILE_UPGRADE_SL;
641 /* dataset currently has a default label */
643 needed_priv = PRIV_FILE_UPGRADE_SL;
647 if (needed_priv != -1)
648 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
651 return (SET_ERROR(ENOTSUP));
652 #endif /* HAVE_MLSLABEL */
656 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
662 * Check permissions for special properties.
669 * Disallow setting of 'zoned' from within a local zone.
671 if (!INGLOBALZONE(curproc))
672 return (SET_ERROR(EPERM));
676 case ZFS_PROP_FILESYSTEM_LIMIT:
677 case ZFS_PROP_SNAPSHOT_LIMIT:
678 if (!INGLOBALZONE(curproc)) {
680 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
682 * Unprivileged users are allowed to modify the
683 * limit on things *under* (ie. contained by)
684 * the thing they own.
686 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
688 return (SET_ERROR(EPERM));
689 if (!zoned || strlen(dsname) <= strlen(setpoint))
690 return (SET_ERROR(EPERM));
694 case ZFS_PROP_MLSLABEL:
695 if (!is_system_labeled())
696 return (SET_ERROR(EPERM));
698 if (nvpair_value_string(propval, &strval) == 0) {
701 err = zfs_set_slabel_policy(dsname, strval, CRED());
708 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
713 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
717 error = zfs_dozonecheck(zc->zc_name, cr);
722 * permission to set permissions will be evaluated later in
723 * dsl_deleg_can_allow()
730 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
732 return (zfs_secpolicy_write_perms(zc->zc_name,
733 ZFS_DELEG_PERM_ROLLBACK, cr));
738 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
746 * Generate the current snapshot name from the given objsetid, then
747 * use that name for the secpolicy/zone checks.
749 cp = strchr(zc->zc_name, '@');
751 return (SET_ERROR(EINVAL));
752 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
756 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
758 dsl_pool_rele(dp, FTAG);
762 dsl_dataset_name(ds, zc->zc_name);
764 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
765 ZFS_DELEG_PERM_SEND, cr);
766 dsl_dataset_rele(ds, FTAG);
767 dsl_pool_rele(dp, FTAG);
774 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
776 return (zfs_secpolicy_write_perms(zc->zc_name,
777 ZFS_DELEG_PERM_SEND, cr));
781 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
783 return (SET_ERROR(ENOTSUP));
787 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
789 return (SET_ERROR(ENOTSUP));
793 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
798 * Remove the @bla or /bla from the end of the name to get the parent.
800 (void) strncpy(parent, datasetname, parentsize);
801 cp = strrchr(parent, '@');
805 cp = strrchr(parent, '/');
807 return (SET_ERROR(ENOENT));
815 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
819 if ((error = zfs_secpolicy_write_perms(name,
820 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
823 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
828 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
830 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
834 * Destroying snapshots with delegated permissions requires
835 * descendant mount and destroy permissions.
839 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
842 nvpair_t *pair, *nextpair;
845 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
847 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
849 nextpair = nvlist_next_nvpair(snaps, pair);
850 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
851 if (error == ENOENT) {
853 * Ignore any snapshots that don't exist (we consider
854 * them "already destroyed"). Remove the name from the
855 * nvl here in case the snapshot is created between
856 * now and when we try to destroy it (in which case
857 * we don't want to destroy it since we haven't
858 * checked for permission).
860 fnvlist_remove_nvpair(snaps, pair);
871 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
873 char parentname[ZFS_MAX_DATASET_NAME_LEN];
876 if ((error = zfs_secpolicy_write_perms(from,
877 ZFS_DELEG_PERM_RENAME, cr)) != 0)
880 if ((error = zfs_secpolicy_write_perms(from,
881 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
884 if ((error = zfs_get_parent(to, parentname,
885 sizeof (parentname))) != 0)
888 if ((error = zfs_secpolicy_write_perms(parentname,
889 ZFS_DELEG_PERM_CREATE, cr)) != 0)
892 if ((error = zfs_secpolicy_write_perms(parentname,
893 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
901 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
903 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
908 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
911 dsl_dataset_t *clone;
914 error = zfs_secpolicy_write_perms(zc->zc_name,
915 ZFS_DELEG_PERM_PROMOTE, cr);
919 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
923 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
926 char parentname[ZFS_MAX_DATASET_NAME_LEN];
927 dsl_dataset_t *origin = NULL;
931 error = dsl_dataset_hold_obj(dd->dd_pool,
932 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
934 dsl_dataset_rele(clone, FTAG);
935 dsl_pool_rele(dp, FTAG);
939 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
940 ZFS_DELEG_PERM_MOUNT, cr);
942 dsl_dataset_name(origin, parentname);
944 error = zfs_secpolicy_write_perms_ds(parentname, origin,
945 ZFS_DELEG_PERM_PROMOTE, cr);
947 dsl_dataset_rele(clone, FTAG);
948 dsl_dataset_rele(origin, FTAG);
950 dsl_pool_rele(dp, FTAG);
956 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
960 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
961 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
964 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
965 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
968 return (zfs_secpolicy_write_perms(zc->zc_name,
969 ZFS_DELEG_PERM_CREATE, cr));
974 zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
976 return (zfs_secpolicy_recv(zc, innvl, cr));
980 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
982 return (zfs_secpolicy_write_perms(name,
983 ZFS_DELEG_PERM_SNAPSHOT, cr));
987 * Check for permission to create each snapshot in the nvlist.
991 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
997 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
999 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1000 pair = nvlist_next_nvpair(snaps, pair)) {
1001 char *name = nvpair_name(pair);
1002 char *atp = strchr(name, '@');
1005 error = SET_ERROR(EINVAL);
1009 error = zfs_secpolicy_snapshot_perms(name, cr);
1018 * Check for permission to create each bookmark in the nvlist.
1022 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1026 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1027 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1028 char *name = nvpair_name(pair);
1029 char *hashp = strchr(name, '#');
1031 if (hashp == NULL) {
1032 error = SET_ERROR(EINVAL);
1036 error = zfs_secpolicy_write_perms(name,
1037 ZFS_DELEG_PERM_BOOKMARK, cr);
1047 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1049 return (zfs_secpolicy_write_perms(zc->zc_name,
1050 ZFS_DELEG_PERM_REMAP, cr));
1055 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1057 nvpair_t *pair, *nextpair;
1060 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1062 char *name = nvpair_name(pair);
1063 char *hashp = strchr(name, '#');
1064 nextpair = nvlist_next_nvpair(innvl, pair);
1066 if (hashp == NULL) {
1067 error = SET_ERROR(EINVAL);
1072 error = zfs_secpolicy_write_perms(name,
1073 ZFS_DELEG_PERM_DESTROY, cr);
1075 if (error == ENOENT) {
1077 * Ignore any filesystems that don't exist (we consider
1078 * their bookmarks "already destroyed"). Remove
1079 * the name from the nvl here in case the filesystem
1080 * is created between now and when we try to destroy
1081 * the bookmark (in which case we don't want to
1082 * destroy it since we haven't checked for permission).
1084 fnvlist_remove_nvpair(innvl, pair);
1096 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1099 * Even root must have a proper TSD so that we know what pool
1102 if (tsd_get(zfs_allow_log_key) == NULL)
1103 return (SET_ERROR(EPERM));
1108 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1110 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1114 if ((error = zfs_get_parent(zc->zc_name, parentname,
1115 sizeof (parentname))) != 0)
1118 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1119 (error = zfs_secpolicy_write_perms(origin,
1120 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1123 if ((error = zfs_secpolicy_write_perms(parentname,
1124 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1127 return (zfs_secpolicy_write_perms(parentname,
1128 ZFS_DELEG_PERM_MOUNT, cr));
1132 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1133 * SYS_CONFIG privilege, which is not available in a local zone.
1137 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1139 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1140 return (SET_ERROR(EPERM));
1146 * Policy for object to name lookups.
1150 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1154 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1157 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1162 * Policy for fault injection. Requires all privileges.
1166 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1168 return (secpolicy_zinject(cr));
1173 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1175 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1177 if (prop == ZPROP_INVAL) {
1178 if (!zfs_prop_user(zc->zc_value))
1179 return (SET_ERROR(EINVAL));
1180 return (zfs_secpolicy_write_perms(zc->zc_name,
1181 ZFS_DELEG_PERM_USERPROP, cr));
1183 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1189 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1191 int err = zfs_secpolicy_read(zc, innvl, cr);
1195 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1196 return (SET_ERROR(EINVAL));
1198 if (zc->zc_value[0] == 0) {
1200 * They are asking about a posix uid/gid. If it's
1201 * themself, allow it.
1203 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1204 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1205 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1206 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1207 if (zc->zc_guid == crgetuid(cr))
1209 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1210 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1211 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1212 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1213 if (groupmember(zc->zc_guid, cr))
1216 /* else is for project quota/used */
1219 return (zfs_secpolicy_write_perms(zc->zc_name,
1220 userquota_perms[zc->zc_objset_type], cr));
1224 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1226 int err = zfs_secpolicy_read(zc, innvl, cr);
1230 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1231 return (SET_ERROR(EINVAL));
1233 return (zfs_secpolicy_write_perms(zc->zc_name,
1234 userquota_perms[zc->zc_objset_type], cr));
1239 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1241 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1247 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1253 holds = fnvlist_lookup_nvlist(innvl, "holds");
1255 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1256 pair = nvlist_next_nvpair(holds, pair)) {
1257 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1258 error = dmu_fsname(nvpair_name(pair), fsname);
1261 error = zfs_secpolicy_write_perms(fsname,
1262 ZFS_DELEG_PERM_HOLD, cr);
1271 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1276 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1277 pair = nvlist_next_nvpair(innvl, pair)) {
1278 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1279 error = dmu_fsname(nvpair_name(pair), fsname);
1282 error = zfs_secpolicy_write_perms(fsname,
1283 ZFS_DELEG_PERM_RELEASE, cr);
1291 * Policy for allowing temporary snapshots to be taken or released
1294 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1297 * A temporary snapshot is the same as a snapshot,
1298 * hold, destroy and release all rolled into one.
1299 * Delegated diff alone is sufficient that we allow this.
1303 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1304 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1307 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1309 if (innvl != NULL) {
1311 error = zfs_secpolicy_hold(zc, innvl, cr);
1313 error = zfs_secpolicy_release(zc, innvl, cr);
1315 error = zfs_secpolicy_destroy(zc, innvl, cr);
1321 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1323 return (zfs_secpolicy_write_perms(zc->zc_name,
1324 ZFS_DELEG_PERM_LOAD_KEY, cr));
1328 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1330 return (zfs_secpolicy_write_perms(zc->zc_name,
1331 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1335 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1338 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1342 nvlist_t *list = NULL;
1345 * Read in and unpack the user-supplied nvlist.
1348 return (SET_ERROR(EINVAL));
1350 packed = vmem_alloc(size, KM_SLEEP);
1352 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1354 vmem_free(packed, size);
1355 return (SET_ERROR(EFAULT));
1358 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1359 vmem_free(packed, size);
1363 vmem_free(packed, size);
1370 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1371 * Entries will be removed from the end of the nvlist, and one int32 entry
1372 * named "N_MORE_ERRORS" will be added indicating how many entries were
1376 nvlist_smush(nvlist_t *errors, size_t max)
1380 size = fnvlist_size(errors);
1383 nvpair_t *more_errors;
1387 return (SET_ERROR(ENOMEM));
1389 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1390 more_errors = nvlist_prev_nvpair(errors, NULL);
1393 nvpair_t *pair = nvlist_prev_nvpair(errors,
1395 fnvlist_remove_nvpair(errors, pair);
1397 size = fnvlist_size(errors);
1398 } while (size > max);
1400 fnvlist_remove_nvpair(errors, more_errors);
1401 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1402 ASSERT3U(fnvlist_size(errors), <=, max);
1409 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1411 char *packed = NULL;
1415 size = fnvlist_size(nvl);
1417 if (size > zc->zc_nvlist_dst_size) {
1418 error = SET_ERROR(ENOMEM);
1420 packed = fnvlist_pack(nvl, &size);
1421 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1422 size, zc->zc_iflags) != 0)
1423 error = SET_ERROR(EFAULT);
1424 fnvlist_pack_free(packed, size);
1427 zc->zc_nvlist_dst_size = size;
1428 zc->zc_nvlist_dst_filled = B_TRUE;
1433 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1436 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1437 return (SET_ERROR(EINVAL));
1440 mutex_enter(&os->os_user_ptr_lock);
1441 *zfvp = dmu_objset_get_user(os);
1442 /* bump s_active only when non-zero to prevent umount race */
1443 if (*zfvp == NULL || (*zfvp)->z_sb == NULL ||
1444 !atomic_inc_not_zero(&((*zfvp)->z_sb->s_active))) {
1445 error = SET_ERROR(ESRCH);
1447 mutex_exit(&os->os_user_ptr_lock);
1452 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1457 error = dmu_objset_hold(dsname, FTAG, &os);
1461 error = getzfsvfs_impl(os, zfvp);
1462 dmu_objset_rele(os, FTAG);
1467 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1468 * case its z_sb will be NULL, and it will be opened as the owner.
1469 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1470 * which prevents all inode ops from running.
1473 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1477 if (getzfsvfs(name, zfvp) != 0)
1478 error = zfsvfs_create(name, B_FALSE, zfvp);
1480 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1482 if ((*zfvp)->z_unmounted) {
1484 * XXX we could probably try again, since the unmounting
1485 * thread should be just about to disassociate the
1486 * objset from the zfsvfs.
1488 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1489 return (SET_ERROR(EBUSY));
1496 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1498 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1501 deactivate_super(zfsvfs->z_sb);
1503 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1504 zfsvfs_free(zfsvfs);
1509 zfs_ioc_pool_create(zfs_cmd_t *zc)
1512 nvlist_t *config, *props = NULL;
1513 nvlist_t *rootprops = NULL;
1514 nvlist_t *zplprops = NULL;
1515 dsl_crypto_params_t *dcp = NULL;
1516 char *spa_name = zc->zc_name;
1517 boolean_t unload_wkey = B_TRUE;
1519 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1520 zc->zc_iflags, &config)))
1523 if (zc->zc_nvlist_src_size != 0 && (error =
1524 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1525 zc->zc_iflags, &props))) {
1526 nvlist_free(config);
1531 nvlist_t *nvl = NULL;
1532 nvlist_t *hidden_args = NULL;
1533 uint64_t version = SPA_VERSION;
1536 (void) nvlist_lookup_uint64(props,
1537 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1538 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1539 error = SET_ERROR(EINVAL);
1540 goto pool_props_bad;
1542 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1544 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1546 goto pool_props_bad;
1547 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1550 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1552 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1553 rootprops, hidden_args, &dcp);
1555 goto pool_props_bad;
1556 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1558 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1559 error = zfs_fill_zplprops_root(version, rootprops,
1562 goto pool_props_bad;
1564 if (nvlist_lookup_string(props,
1565 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1569 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1572 * Set the remaining root properties
1574 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1575 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1576 (void) spa_destroy(spa_name);
1577 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1581 nvlist_free(rootprops);
1582 nvlist_free(zplprops);
1583 nvlist_free(config);
1585 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1591 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1594 zfs_log_history(zc);
1595 error = spa_destroy(zc->zc_name);
1601 zfs_ioc_pool_import(zfs_cmd_t *zc)
1603 nvlist_t *config, *props = NULL;
1607 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1608 zc->zc_iflags, &config)) != 0)
1611 if (zc->zc_nvlist_src_size != 0 && (error =
1612 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1613 zc->zc_iflags, &props))) {
1614 nvlist_free(config);
1618 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1619 guid != zc->zc_guid)
1620 error = SET_ERROR(EINVAL);
1622 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1624 if (zc->zc_nvlist_dst != 0) {
1627 if ((err = put_nvlist(zc, config)) != 0)
1631 nvlist_free(config);
1638 zfs_ioc_pool_export(zfs_cmd_t *zc)
1641 boolean_t force = (boolean_t)zc->zc_cookie;
1642 boolean_t hardforce = (boolean_t)zc->zc_guid;
1644 zfs_log_history(zc);
1645 error = spa_export(zc->zc_name, NULL, force, hardforce);
1651 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1656 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1657 return (SET_ERROR(EEXIST));
1659 error = put_nvlist(zc, configs);
1661 nvlist_free(configs);
1668 * zc_name name of the pool
1671 * zc_cookie real errno
1672 * zc_nvlist_dst config nvlist
1673 * zc_nvlist_dst_size size of config nvlist
1676 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1682 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1683 sizeof (zc->zc_value));
1685 if (config != NULL) {
1686 ret = put_nvlist(zc, config);
1687 nvlist_free(config);
1690 * The config may be present even if 'error' is non-zero.
1691 * In this case we return success, and preserve the real errno
1694 zc->zc_cookie = error;
1703 * Try to import the given pool, returning pool stats as appropriate so that
1704 * user land knows which devices are available and overall pool health.
1707 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1709 nvlist_t *tryconfig, *config = NULL;
1712 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1713 zc->zc_iflags, &tryconfig)) != 0)
1716 config = spa_tryimport(tryconfig);
1718 nvlist_free(tryconfig);
1721 return (SET_ERROR(EINVAL));
1723 error = put_nvlist(zc, config);
1724 nvlist_free(config);
1731 * zc_name name of the pool
1732 * zc_cookie scan func (pool_scan_func_t)
1733 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1736 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1741 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1742 return (SET_ERROR(EINVAL));
1744 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1747 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1748 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1749 else if (zc->zc_cookie == POOL_SCAN_NONE)
1750 error = spa_scan_stop(spa);
1752 error = spa_scan(spa, zc->zc_cookie);
1754 spa_close(spa, FTAG);
1760 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1765 error = spa_open(zc->zc_name, &spa, FTAG);
1768 spa_close(spa, FTAG);
1774 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1779 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1782 if (zc->zc_cookie < spa_version(spa) ||
1783 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1784 spa_close(spa, FTAG);
1785 return (SET_ERROR(EINVAL));
1788 spa_upgrade(spa, zc->zc_cookie);
1789 spa_close(spa, FTAG);
1795 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1802 if ((size = zc->zc_history_len) == 0)
1803 return (SET_ERROR(EINVAL));
1805 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1808 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1809 spa_close(spa, FTAG);
1810 return (SET_ERROR(ENOTSUP));
1813 hist_buf = vmem_alloc(size, KM_SLEEP);
1814 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1815 &zc->zc_history_len, hist_buf)) == 0) {
1816 error = ddi_copyout(hist_buf,
1817 (void *)(uintptr_t)zc->zc_history,
1818 zc->zc_history_len, zc->zc_iflags);
1821 spa_close(spa, FTAG);
1822 vmem_free(hist_buf, size);
1827 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1832 error = spa_open(zc->zc_name, &spa, FTAG);
1834 error = spa_change_guid(spa);
1835 spa_close(spa, FTAG);
1841 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1843 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1848 * zc_name name of filesystem
1849 * zc_obj object to find
1852 * zc_value name of object
1855 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1860 /* XXX reading from objset not owned */
1861 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1864 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1865 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1866 return (SET_ERROR(EINVAL));
1868 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1869 sizeof (zc->zc_value));
1870 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1877 * zc_name name of filesystem
1878 * zc_obj object to find
1881 * zc_stat stats on object
1882 * zc_value path to object
1885 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1890 /* XXX reading from objset not owned */
1891 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1894 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1895 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1896 return (SET_ERROR(EINVAL));
1898 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1899 sizeof (zc->zc_value));
1900 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1906 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1912 error = spa_open(zc->zc_name, &spa, FTAG);
1916 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1917 zc->zc_iflags, &config);
1919 error = spa_vdev_add(spa, config);
1920 nvlist_free(config);
1922 spa_close(spa, FTAG);
1928 * zc_name name of the pool
1929 * zc_guid guid of vdev to remove
1930 * zc_cookie cancel removal
1933 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1938 error = spa_open(zc->zc_name, &spa, FTAG);
1941 if (zc->zc_cookie != 0) {
1942 error = spa_vdev_remove_cancel(spa);
1944 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1946 spa_close(spa, FTAG);
1951 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1955 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1957 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1959 switch (zc->zc_cookie) {
1960 case VDEV_STATE_ONLINE:
1961 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1964 case VDEV_STATE_OFFLINE:
1965 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1968 case VDEV_STATE_FAULTED:
1969 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1970 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1971 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1972 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1974 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1977 case VDEV_STATE_DEGRADED:
1978 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1979 zc->zc_obj != VDEV_AUX_EXTERNAL)
1980 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1982 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1986 error = SET_ERROR(EINVAL);
1988 zc->zc_cookie = newstate;
1989 spa_close(spa, FTAG);
1994 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1997 int replacing = zc->zc_cookie;
2001 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2004 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2005 zc->zc_iflags, &config)) == 0) {
2006 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2007 nvlist_free(config);
2010 spa_close(spa, FTAG);
2015 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2020 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2023 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2025 spa_close(spa, FTAG);
2030 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2033 nvlist_t *config, *props = NULL;
2035 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2037 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2040 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2041 zc->zc_iflags, &config))) {
2042 spa_close(spa, FTAG);
2046 if (zc->zc_nvlist_src_size != 0 && (error =
2047 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2048 zc->zc_iflags, &props))) {
2049 spa_close(spa, FTAG);
2050 nvlist_free(config);
2054 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2056 spa_close(spa, FTAG);
2058 nvlist_free(config);
2065 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2068 char *path = zc->zc_value;
2069 uint64_t guid = zc->zc_guid;
2072 error = spa_open(zc->zc_name, &spa, FTAG);
2076 error = spa_vdev_setpath(spa, guid, path);
2077 spa_close(spa, FTAG);
2082 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2085 char *fru = zc->zc_value;
2086 uint64_t guid = zc->zc_guid;
2089 error = spa_open(zc->zc_name, &spa, FTAG);
2093 error = spa_vdev_setfru(spa, guid, fru);
2094 spa_close(spa, FTAG);
2099 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2104 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2106 if (zc->zc_nvlist_dst != 0 &&
2107 (error = dsl_prop_get_all(os, &nv)) == 0) {
2108 dmu_objset_stats(os, nv);
2110 * NB: zvol_get_stats() will read the objset contents,
2111 * which we aren't supposed to do with a
2112 * DS_MODE_USER hold, because it could be
2113 * inconsistent. So this is a bit of a workaround...
2114 * XXX reading with out owning
2116 if (!zc->zc_objset_stats.dds_inconsistent &&
2117 dmu_objset_type(os) == DMU_OST_ZVOL) {
2118 error = zvol_get_stats(os, nv);
2126 error = put_nvlist(zc, nv);
2135 * zc_name name of filesystem
2136 * zc_nvlist_dst_size size of buffer for property nvlist
2139 * zc_objset_stats stats
2140 * zc_nvlist_dst property nvlist
2141 * zc_nvlist_dst_size size of property nvlist
2144 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2149 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2151 error = zfs_ioc_objset_stats_impl(zc, os);
2152 dmu_objset_rele(os, FTAG);
2160 * zc_name name of filesystem
2161 * zc_nvlist_dst_size size of buffer for property nvlist
2164 * zc_nvlist_dst received property nvlist
2165 * zc_nvlist_dst_size size of received property nvlist
2167 * Gets received properties (distinct from local properties on or after
2168 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2169 * local property values.
2172 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2178 * Without this check, we would return local property values if the
2179 * caller has not already received properties on or after
2180 * SPA_VERSION_RECVD_PROPS.
2182 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2183 return (SET_ERROR(ENOTSUP));
2185 if (zc->zc_nvlist_dst != 0 &&
2186 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2187 error = put_nvlist(zc, nv);
2195 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2201 * zfs_get_zplprop() will either find a value or give us
2202 * the default value (if there is one).
2204 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2206 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2212 * zc_name name of filesystem
2213 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2216 * zc_nvlist_dst zpl property nvlist
2217 * zc_nvlist_dst_size size of zpl property nvlist
2220 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2225 /* XXX reading without owning */
2226 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2229 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2232 * NB: nvl_add_zplprop() will read the objset contents,
2233 * which we aren't supposed to do with a DS_MODE_USER
2234 * hold, because it could be inconsistent.
2236 if (zc->zc_nvlist_dst != 0 &&
2237 !zc->zc_objset_stats.dds_inconsistent &&
2238 dmu_objset_type(os) == DMU_OST_ZFS) {
2241 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2242 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2243 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2244 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2245 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2246 err = put_nvlist(zc, nv);
2249 err = SET_ERROR(ENOENT);
2251 dmu_objset_rele(os, FTAG);
2257 * zc_name name of filesystem
2258 * zc_cookie zap cursor
2259 * zc_nvlist_dst_size size of buffer for property nvlist
2262 * zc_name name of next filesystem
2263 * zc_cookie zap cursor
2264 * zc_objset_stats stats
2265 * zc_nvlist_dst property nvlist
2266 * zc_nvlist_dst_size size of property nvlist
2269 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2274 size_t orig_len = strlen(zc->zc_name);
2277 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2278 if (error == ENOENT)
2279 error = SET_ERROR(ESRCH);
2283 p = strrchr(zc->zc_name, '/');
2284 if (p == NULL || p[1] != '\0')
2285 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2286 p = zc->zc_name + strlen(zc->zc_name);
2289 error = dmu_dir_list_next(os,
2290 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2291 NULL, &zc->zc_cookie);
2292 if (error == ENOENT)
2293 error = SET_ERROR(ESRCH);
2294 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2295 dmu_objset_rele(os, FTAG);
2298 * If it's an internal dataset (ie. with a '$' in its name),
2299 * don't try to get stats for it, otherwise we'll return ENOENT.
2301 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2302 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2303 if (error == ENOENT) {
2304 /* We lost a race with destroy, get the next one. */
2305 zc->zc_name[orig_len] = '\0';
2314 * zc_name name of filesystem
2315 * zc_cookie zap cursor
2316 * zc_nvlist_src iteration range nvlist
2317 * zc_nvlist_src_size size of iteration range nvlist
2320 * zc_name name of next snapshot
2321 * zc_objset_stats stats
2322 * zc_nvlist_dst property nvlist
2323 * zc_nvlist_dst_size size of property nvlist
2326 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2329 objset_t *os, *ossnap;
2331 uint64_t min_txg = 0, max_txg = 0;
2333 if (zc->zc_nvlist_src_size != 0) {
2334 nvlist_t *props = NULL;
2335 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2336 zc->zc_iflags, &props);
2339 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2341 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2346 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2348 return (error == ENOENT ? ESRCH : error);
2352 * A dataset name of maximum length cannot have any snapshots,
2353 * so exit immediately.
2355 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2356 ZFS_MAX_DATASET_NAME_LEN) {
2357 dmu_objset_rele(os, FTAG);
2358 return (SET_ERROR(ESRCH));
2361 while (error == 0) {
2362 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2363 error = SET_ERROR(EINTR);
2367 error = dmu_snapshot_list_next(os,
2368 sizeof (zc->zc_name) - strlen(zc->zc_name),
2369 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2370 &zc->zc_cookie, NULL);
2371 if (error == ENOENT) {
2372 error = SET_ERROR(ESRCH);
2374 } else if (error != 0) {
2378 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2383 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2384 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2385 dsl_dataset_rele(ds, FTAG);
2386 /* undo snapshot name append */
2387 *(strchr(zc->zc_name, '@') + 1) = '\0';
2392 if (zc->zc_simple) {
2393 dsl_dataset_rele(ds, FTAG);
2397 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2398 dsl_dataset_rele(ds, FTAG);
2401 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2402 dsl_dataset_rele(ds, FTAG);
2405 dsl_dataset_rele(ds, FTAG);
2409 dmu_objset_rele(os, FTAG);
2410 /* if we failed, undo the @ that we tacked on to zc_name */
2412 *strchr(zc->zc_name, '@') = '\0';
2417 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2419 const char *propname = nvpair_name(pair);
2421 unsigned int vallen;
2424 zfs_userquota_prop_t type;
2430 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2432 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2433 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2435 return (SET_ERROR(EINVAL));
2439 * A correctly constructed propname is encoded as
2440 * userquota@<rid>-<domain>.
2442 if ((dash = strchr(propname, '-')) == NULL ||
2443 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2445 return (SET_ERROR(EINVAL));
2452 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2454 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2455 zfsvfs_rele(zfsvfs, FTAG);
2462 * If the named property is one that has a special function to set its value,
2463 * return 0 on success and a positive error code on failure; otherwise if it is
2464 * not one of the special properties handled by this function, return -1.
2466 * XXX: It would be better for callers of the property interface if we handled
2467 * these special cases in dsl_prop.c (in the dsl layer).
2470 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2473 const char *propname = nvpair_name(pair);
2474 zfs_prop_t prop = zfs_name_to_prop(propname);
2475 uint64_t intval = 0;
2476 char *strval = NULL;
2479 if (prop == ZPROP_INVAL) {
2480 if (zfs_prop_userquota(propname))
2481 return (zfs_prop_set_userquota(dsname, pair));
2485 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2487 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2488 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2492 /* all special properties are numeric except for keylocation */
2493 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2494 strval = fnvpair_value_string(pair);
2496 intval = fnvpair_value_uint64(pair);
2500 case ZFS_PROP_QUOTA:
2501 err = dsl_dir_set_quota(dsname, source, intval);
2503 case ZFS_PROP_REFQUOTA:
2504 err = dsl_dataset_set_refquota(dsname, source, intval);
2506 case ZFS_PROP_FILESYSTEM_LIMIT:
2507 case ZFS_PROP_SNAPSHOT_LIMIT:
2508 if (intval == UINT64_MAX) {
2509 /* clearing the limit, just do it */
2512 err = dsl_dir_activate_fs_ss_limit(dsname);
2515 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2516 * default path to set the value in the nvlist.
2521 case ZFS_PROP_KEYLOCATION:
2522 err = dsl_crypto_can_set_keylocation(dsname, strval);
2525 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2526 * default path to set the value in the nvlist.
2531 case ZFS_PROP_RESERVATION:
2532 err = dsl_dir_set_reservation(dsname, source, intval);
2534 case ZFS_PROP_REFRESERVATION:
2535 err = dsl_dataset_set_refreservation(dsname, source, intval);
2537 case ZFS_PROP_VOLSIZE:
2538 err = zvol_set_volsize(dsname, intval);
2540 case ZFS_PROP_SNAPDEV:
2541 err = zvol_set_snapdev(dsname, source, intval);
2543 case ZFS_PROP_VOLMODE:
2544 err = zvol_set_volmode(dsname, source, intval);
2546 case ZFS_PROP_VERSION:
2550 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2553 err = zfs_set_version(zfsvfs, intval);
2554 zfsvfs_rele(zfsvfs, FTAG);
2556 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2559 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2560 (void) strcpy(zc->zc_name, dsname);
2561 (void) zfs_ioc_userspace_upgrade(zc);
2562 (void) zfs_ioc_id_quota_upgrade(zc);
2563 kmem_free(zc, sizeof (zfs_cmd_t));
2575 * This function is best effort. If it fails to set any of the given properties,
2576 * it continues to set as many as it can and returns the last error
2577 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2578 * with the list of names of all the properties that failed along with the
2579 * corresponding error numbers.
2581 * If every property is set successfully, zero is returned and errlist is not
2585 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2594 nvlist_t *genericnvl = fnvlist_alloc();
2595 nvlist_t *retrynvl = fnvlist_alloc();
2598 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2599 const char *propname = nvpair_name(pair);
2600 zfs_prop_t prop = zfs_name_to_prop(propname);
2603 /* decode the property value */
2605 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2607 attrs = fnvpair_value_nvlist(pair);
2608 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2610 err = SET_ERROR(EINVAL);
2613 /* Validate value type */
2614 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2615 /* inherited properties are expected to be booleans */
2616 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2617 err = SET_ERROR(EINVAL);
2618 } else if (err == 0 && prop == ZPROP_INVAL) {
2619 if (zfs_prop_user(propname)) {
2620 if (nvpair_type(propval) != DATA_TYPE_STRING)
2621 err = SET_ERROR(EINVAL);
2622 } else if (zfs_prop_userquota(propname)) {
2623 if (nvpair_type(propval) !=
2624 DATA_TYPE_UINT64_ARRAY)
2625 err = SET_ERROR(EINVAL);
2627 err = SET_ERROR(EINVAL);
2629 } else if (err == 0) {
2630 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2631 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2632 err = SET_ERROR(EINVAL);
2633 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2636 intval = fnvpair_value_uint64(propval);
2638 switch (zfs_prop_get_type(prop)) {
2639 case PROP_TYPE_NUMBER:
2641 case PROP_TYPE_STRING:
2642 err = SET_ERROR(EINVAL);
2644 case PROP_TYPE_INDEX:
2645 if (zfs_prop_index_to_string(prop,
2646 intval, &unused) != 0)
2647 err = SET_ERROR(EINVAL);
2651 "unknown property type");
2654 err = SET_ERROR(EINVAL);
2658 /* Validate permissions */
2660 err = zfs_check_settable(dsname, pair, CRED());
2663 if (source == ZPROP_SRC_INHERITED)
2664 err = -1; /* does not need special handling */
2666 err = zfs_prop_set_special(dsname, source,
2670 * For better performance we build up a list of
2671 * properties to set in a single transaction.
2673 err = nvlist_add_nvpair(genericnvl, pair);
2674 } else if (err != 0 && nvl != retrynvl) {
2676 * This may be a spurious error caused by
2677 * receiving quota and reservation out of order.
2678 * Try again in a second pass.
2680 err = nvlist_add_nvpair(retrynvl, pair);
2685 if (errlist != NULL)
2686 fnvlist_add_int32(errlist, propname, err);
2691 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2696 if (!nvlist_empty(genericnvl) &&
2697 dsl_props_set(dsname, source, genericnvl) != 0) {
2699 * If this fails, we still want to set as many properties as we
2700 * can, so try setting them individually.
2703 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2704 const char *propname = nvpair_name(pair);
2708 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2710 attrs = fnvpair_value_nvlist(pair);
2711 propval = fnvlist_lookup_nvpair(attrs,
2715 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2716 strval = fnvpair_value_string(propval);
2717 err = dsl_prop_set_string(dsname, propname,
2719 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2720 err = dsl_prop_inherit(dsname, propname,
2723 intval = fnvpair_value_uint64(propval);
2724 err = dsl_prop_set_int(dsname, propname, source,
2729 if (errlist != NULL) {
2730 fnvlist_add_int32(errlist, propname,
2737 nvlist_free(genericnvl);
2738 nvlist_free(retrynvl);
2744 * Check that all the properties are valid user properties.
2747 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2749 nvpair_t *pair = NULL;
2752 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2753 const char *propname = nvpair_name(pair);
2755 if (!zfs_prop_user(propname) ||
2756 nvpair_type(pair) != DATA_TYPE_STRING)
2757 return (SET_ERROR(EINVAL));
2759 if ((error = zfs_secpolicy_write_perms(fsname,
2760 ZFS_DELEG_PERM_USERPROP, CRED())))
2763 if (strlen(propname) >= ZAP_MAXNAMELEN)
2764 return (SET_ERROR(ENAMETOOLONG));
2766 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2767 return (SET_ERROR(E2BIG));
2773 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2777 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2780 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2781 if (nvlist_exists(skipped, nvpair_name(pair)))
2784 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2789 clear_received_props(const char *dsname, nvlist_t *props,
2793 nvlist_t *cleared_props = NULL;
2794 props_skip(props, skipped, &cleared_props);
2795 if (!nvlist_empty(cleared_props)) {
2797 * Acts on local properties until the dataset has received
2798 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2800 zprop_source_t flags = (ZPROP_SRC_NONE |
2801 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2802 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2804 nvlist_free(cleared_props);
2810 * zc_name name of filesystem
2811 * zc_value name of property to set
2812 * zc_nvlist_src{_size} nvlist of properties to apply
2813 * zc_cookie received properties flag
2816 * zc_nvlist_dst{_size} error for each unapplied received property
2819 zfs_ioc_set_prop(zfs_cmd_t *zc)
2822 boolean_t received = zc->zc_cookie;
2823 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2828 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2829 zc->zc_iflags, &nvl)) != 0)
2833 nvlist_t *origprops;
2835 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2836 (void) clear_received_props(zc->zc_name,
2838 nvlist_free(origprops);
2841 error = dsl_prop_set_hasrecvd(zc->zc_name);
2844 errors = fnvlist_alloc();
2846 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2848 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2849 (void) put_nvlist(zc, errors);
2852 nvlist_free(errors);
2859 * zc_name name of filesystem
2860 * zc_value name of property to inherit
2861 * zc_cookie revert to received value if TRUE
2866 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2868 const char *propname = zc->zc_value;
2869 zfs_prop_t prop = zfs_name_to_prop(propname);
2870 boolean_t received = zc->zc_cookie;
2871 zprop_source_t source = (received
2872 ? ZPROP_SRC_NONE /* revert to received value, if any */
2873 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2881 * Only check this in the non-received case. We want to allow
2882 * 'inherit -S' to revert non-inheritable properties like quota
2883 * and reservation to the received or default values even though
2884 * they are not considered inheritable.
2886 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2887 return (SET_ERROR(EINVAL));
2890 if (prop == ZPROP_INVAL) {
2891 if (!zfs_prop_user(propname))
2892 return (SET_ERROR(EINVAL));
2894 type = PROP_TYPE_STRING;
2895 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2896 return (SET_ERROR(EINVAL));
2898 type = zfs_prop_get_type(prop);
2902 * zfs_prop_set_special() expects properties in the form of an
2903 * nvpair with type info.
2905 dummy = fnvlist_alloc();
2908 case PROP_TYPE_STRING:
2909 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2911 case PROP_TYPE_NUMBER:
2912 case PROP_TYPE_INDEX:
2913 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2916 err = SET_ERROR(EINVAL);
2920 pair = nvlist_next_nvpair(dummy, NULL);
2922 err = SET_ERROR(EINVAL);
2924 err = zfs_prop_set_special(zc->zc_name, source, pair);
2925 if (err == -1) /* property is not "special", needs handling */
2926 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2936 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2943 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2944 zc->zc_iflags, &props)))
2948 * If the only property is the configfile, then just do a spa_lookup()
2949 * to handle the faulted case.
2951 pair = nvlist_next_nvpair(props, NULL);
2952 if (pair != NULL && strcmp(nvpair_name(pair),
2953 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2954 nvlist_next_nvpair(props, pair) == NULL) {
2955 mutex_enter(&spa_namespace_lock);
2956 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2957 spa_configfile_set(spa, props, B_FALSE);
2958 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2960 mutex_exit(&spa_namespace_lock);
2967 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2972 error = spa_prop_set(spa, props);
2975 spa_close(spa, FTAG);
2981 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2985 nvlist_t *nvp = NULL;
2987 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2989 * If the pool is faulted, there may be properties we can still
2990 * get (such as altroot and cachefile), so attempt to get them
2993 mutex_enter(&spa_namespace_lock);
2994 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2995 error = spa_prop_get(spa, &nvp);
2996 mutex_exit(&spa_namespace_lock);
2998 error = spa_prop_get(spa, &nvp);
2999 spa_close(spa, FTAG);
3002 if (error == 0 && zc->zc_nvlist_dst != 0)
3003 error = put_nvlist(zc, nvp);
3005 error = SET_ERROR(EFAULT);
3013 * zc_name name of filesystem
3014 * zc_nvlist_src{_size} nvlist of delegated permissions
3015 * zc_perm_action allow/unallow flag
3020 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3023 nvlist_t *fsaclnv = NULL;
3025 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3026 zc->zc_iflags, &fsaclnv)) != 0)
3030 * Verify nvlist is constructed correctly
3032 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3033 nvlist_free(fsaclnv);
3034 return (SET_ERROR(EINVAL));
3038 * If we don't have PRIV_SYS_MOUNT, then validate
3039 * that user is allowed to hand out each permission in
3043 error = secpolicy_zfs(CRED());
3045 if (zc->zc_perm_action == B_FALSE) {
3046 error = dsl_deleg_can_allow(zc->zc_name,
3049 error = dsl_deleg_can_unallow(zc->zc_name,
3055 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3057 nvlist_free(fsaclnv);
3063 * zc_name name of filesystem
3066 * zc_nvlist_src{_size} nvlist of delegated permissions
3069 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3074 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3075 error = put_nvlist(zc, nvp);
3084 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3086 zfs_creat_t *zct = arg;
3088 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3091 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3095 * os parent objset pointer (NULL if root fs)
3096 * fuids_ok fuids allowed in this version of the spa?
3097 * sa_ok SAs allowed in this version of the spa?
3098 * createprops list of properties requested by creator
3101 * zplprops values for the zplprops we attach to the master node object
3102 * is_ci true if requested file system will be purely case-insensitive
3104 * Determine the settings for utf8only, normalization and
3105 * casesensitivity. Specific values may have been requested by the
3106 * creator and/or we can inherit values from the parent dataset. If
3107 * the file system is of too early a vintage, a creator can not
3108 * request settings for these properties, even if the requested
3109 * setting is the default value. We don't actually want to create dsl
3110 * properties for these, so remove them from the source nvlist after
3114 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3115 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3116 nvlist_t *zplprops, boolean_t *is_ci)
3118 uint64_t sense = ZFS_PROP_UNDEFINED;
3119 uint64_t norm = ZFS_PROP_UNDEFINED;
3120 uint64_t u8 = ZFS_PROP_UNDEFINED;
3123 ASSERT(zplprops != NULL);
3125 /* parent dataset must be a filesystem */
3126 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3127 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3130 * Pull out creator prop choices, if any.
3133 (void) nvlist_lookup_uint64(createprops,
3134 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3135 (void) nvlist_lookup_uint64(createprops,
3136 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3137 (void) nvlist_remove_all(createprops,
3138 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3139 (void) nvlist_lookup_uint64(createprops,
3140 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3141 (void) nvlist_remove_all(createprops,
3142 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3143 (void) nvlist_lookup_uint64(createprops,
3144 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3145 (void) nvlist_remove_all(createprops,
3146 zfs_prop_to_name(ZFS_PROP_CASE));
3150 * If the zpl version requested is whacky or the file system
3151 * or pool is version is too "young" to support normalization
3152 * and the creator tried to set a value for one of the props,
3155 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3156 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3157 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3158 (zplver < ZPL_VERSION_NORMALIZATION &&
3159 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3160 sense != ZFS_PROP_UNDEFINED)))
3161 return (SET_ERROR(ENOTSUP));
3164 * Put the version in the zplprops
3166 VERIFY(nvlist_add_uint64(zplprops,
3167 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3169 if (norm == ZFS_PROP_UNDEFINED &&
3170 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3172 VERIFY(nvlist_add_uint64(zplprops,
3173 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3176 * If we're normalizing, names must always be valid UTF-8 strings.
3180 if (u8 == ZFS_PROP_UNDEFINED &&
3181 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3183 VERIFY(nvlist_add_uint64(zplprops,
3184 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3186 if (sense == ZFS_PROP_UNDEFINED &&
3187 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3189 VERIFY(nvlist_add_uint64(zplprops,
3190 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3193 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3199 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3200 nvlist_t *zplprops, boolean_t *is_ci)
3202 boolean_t fuids_ok, sa_ok;
3203 uint64_t zplver = ZPL_VERSION;
3204 objset_t *os = NULL;
3205 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3210 zfs_get_parent(dataset, parentname, sizeof (parentname));
3212 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3215 spa_vers = spa_version(spa);
3216 spa_close(spa, FTAG);
3218 zplver = zfs_zpl_version_map(spa_vers);
3219 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3220 sa_ok = (zplver >= ZPL_VERSION_SA);
3223 * Open parent object set so we can inherit zplprop values.
3225 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3228 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3230 dmu_objset_rele(os, FTAG);
3235 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3236 nvlist_t *zplprops, boolean_t *is_ci)
3240 uint64_t zplver = ZPL_VERSION;
3243 zplver = zfs_zpl_version_map(spa_vers);
3244 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3245 sa_ok = (zplver >= ZPL_VERSION_SA);
3247 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3248 createprops, zplprops, is_ci);
3254 * "type" -> dmu_objset_type_t (int32)
3255 * (optional) "props" -> { prop -> value }
3256 * (optional) "hidden_args" -> { "wkeydata" -> value }
3257 * raw uint8_t array of encryption wrapping key data (32 bytes)
3260 * outnvl: propname -> error code (int32)
3263 static const zfs_ioc_key_t zfs_keys_create[] = {
3264 {"type", DATA_TYPE_INT32, 0},
3265 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3266 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3270 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3273 zfs_creat_t zct = { 0 };
3274 nvlist_t *nvprops = NULL;
3275 nvlist_t *hidden_args = NULL;
3276 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3277 dmu_objset_type_t type;
3278 boolean_t is_insensitive = B_FALSE;
3279 dsl_crypto_params_t *dcp = NULL;
3281 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3282 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3283 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3287 cbfunc = zfs_create_cb;
3291 cbfunc = zvol_create_cb;
3298 if (strchr(fsname, '@') ||
3299 strchr(fsname, '%'))
3300 return (SET_ERROR(EINVAL));
3302 zct.zct_props = nvprops;
3305 return (SET_ERROR(EINVAL));
3307 if (type == DMU_OST_ZVOL) {
3308 uint64_t volsize, volblocksize;
3310 if (nvprops == NULL)
3311 return (SET_ERROR(EINVAL));
3312 if (nvlist_lookup_uint64(nvprops,
3313 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3314 return (SET_ERROR(EINVAL));
3316 if ((error = nvlist_lookup_uint64(nvprops,
3317 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3318 &volblocksize)) != 0 && error != ENOENT)
3319 return (SET_ERROR(EINVAL));
3322 volblocksize = zfs_prop_default_numeric(
3323 ZFS_PROP_VOLBLOCKSIZE);
3325 if ((error = zvol_check_volblocksize(fsname,
3326 volblocksize)) != 0 ||
3327 (error = zvol_check_volsize(volsize,
3328 volblocksize)) != 0)
3330 } else if (type == DMU_OST_ZFS) {
3334 * We have to have normalization and
3335 * case-folding flags correct when we do the
3336 * file system creation, so go figure them out
3339 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3340 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3341 error = zfs_fill_zplprops(fsname, nvprops,
3342 zct.zct_zplprops, &is_insensitive);
3344 nvlist_free(zct.zct_zplprops);
3349 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3352 nvlist_free(zct.zct_zplprops);
3356 error = dmu_objset_create(fsname, type,
3357 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3359 nvlist_free(zct.zct_zplprops);
3360 dsl_crypto_params_free(dcp, !!error);
3363 * It would be nice to do this atomically.
3366 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3373 * Volumes will return EBUSY and cannot be destroyed
3374 * until all asynchronous minor handling has completed.
3375 * Wait for the spa_zvol_taskq to drain then retry.
3377 error2 = dsl_destroy_head(fsname);
3378 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3379 error2 = spa_open(fsname, &spa, FTAG);
3381 taskq_wait(spa->spa_zvol_taskq);
3382 spa_close(spa, FTAG);
3384 error2 = dsl_destroy_head(fsname);
3393 * "origin" -> name of origin snapshot
3394 * (optional) "props" -> { prop -> value }
3395 * (optional) "hidden_args" -> { "wkeydata" -> value }
3396 * raw uint8_t array of encryption wrapping key data (32 bytes)
3400 * outnvl: propname -> error code (int32)
3402 static const zfs_ioc_key_t zfs_keys_clone[] = {
3403 {"origin", DATA_TYPE_STRING, 0},
3404 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3405 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3409 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3412 nvlist_t *nvprops = NULL;
3415 origin_name = fnvlist_lookup_string(innvl, "origin");
3416 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3418 if (strchr(fsname, '@') ||
3419 strchr(fsname, '%'))
3420 return (SET_ERROR(EINVAL));
3422 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3423 return (SET_ERROR(EINVAL));
3425 error = dmu_objset_clone(fsname, origin_name);
3428 * It would be nice to do this atomically.
3431 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3434 (void) dsl_destroy_head(fsname);
3439 static const zfs_ioc_key_t zfs_keys_remap[] = {
3445 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3447 if (strchr(fsname, '@') ||
3448 strchr(fsname, '%'))
3449 return (SET_ERROR(EINVAL));
3451 return (dmu_objset_remap_indirects(fsname));
3456 * "snaps" -> { snapshot1, snapshot2 }
3457 * (optional) "props" -> { prop -> value (string) }
3460 * outnvl: snapshot -> error code (int32)
3462 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3463 {"snaps", DATA_TYPE_NVLIST, 0},
3464 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3468 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3471 nvlist_t *props = NULL;
3475 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3476 if ((error = zfs_check_userprops(poolname, props)) != 0)
3479 if (!nvlist_empty(props) &&
3480 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3481 return (SET_ERROR(ENOTSUP));
3483 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3484 poollen = strlen(poolname);
3485 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3486 pair = nvlist_next_nvpair(snaps, pair)) {
3487 const char *name = nvpair_name(pair);
3488 const char *cp = strchr(name, '@');
3491 * The snap name must contain an @, and the part after it must
3492 * contain only valid characters.
3495 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3496 return (SET_ERROR(EINVAL));
3499 * The snap must be in the specified pool.
3501 if (strncmp(name, poolname, poollen) != 0 ||
3502 (name[poollen] != '/' && name[poollen] != '@'))
3503 return (SET_ERROR(EXDEV));
3505 /* This must be the only snap of this fs. */
3506 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3507 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3508 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3510 return (SET_ERROR(EXDEV));
3515 error = dsl_dataset_snapshot(snaps, props, outnvl);
3521 * innvl: "message" -> string
3523 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3524 {"message", DATA_TYPE_STRING, 0},
3529 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3537 * The poolname in the ioctl is not set, we get it from the TSD,
3538 * which was set at the end of the last successful ioctl that allows
3539 * logging. The secpolicy func already checked that it is set.
3540 * Only one log ioctl is allowed after each successful ioctl, so
3541 * we clear the TSD here.
3543 poolname = tsd_get(zfs_allow_log_key);
3544 if (poolname == NULL)
3545 return (SET_ERROR(EINVAL));
3546 (void) tsd_set(zfs_allow_log_key, NULL);
3547 error = spa_open(poolname, &spa, FTAG);
3552 message = fnvlist_lookup_string(innvl, "message");
3554 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3555 spa_close(spa, FTAG);
3556 return (SET_ERROR(ENOTSUP));
3559 error = spa_history_log(spa, message);
3560 spa_close(spa, FTAG);
3565 * The dp_config_rwlock must not be held when calling this, because the
3566 * unmount may need to write out data.
3568 * This function is best-effort. Callers must deal gracefully if it
3569 * remains mounted (or is remounted after this call).
3571 * Returns 0 if the argument is not a snapshot, or it is not currently a
3572 * filesystem, or we were able to unmount it. Returns error code otherwise.
3575 zfs_unmount_snap(const char *snapname)
3577 if (strchr(snapname, '@') == NULL)
3580 (void) zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
3585 zfs_unmount_snap_cb(const char *snapname, void *arg)
3587 zfs_unmount_snap(snapname);
3592 * When a clone is destroyed, its origin may also need to be destroyed,
3593 * in which case it must be unmounted. This routine will do that unmount
3597 zfs_destroy_unmount_origin(const char *fsname)
3603 error = dmu_objset_hold(fsname, FTAG, &os);
3606 ds = dmu_objset_ds(os);
3607 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3608 char originname[ZFS_MAX_DATASET_NAME_LEN];
3609 dsl_dataset_name(ds->ds_prev, originname);
3610 dmu_objset_rele(os, FTAG);
3611 zfs_unmount_snap(originname);
3613 dmu_objset_rele(os, FTAG);
3619 * "snaps" -> { snapshot1, snapshot2 }
3620 * (optional boolean) "defer"
3623 * outnvl: snapshot -> error code (int32)
3625 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3626 {"snaps", DATA_TYPE_NVLIST, 0},
3627 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3632 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3638 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3639 defer = nvlist_exists(innvl, "defer");
3641 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3642 pair = nvlist_next_nvpair(snaps, pair)) {
3643 zfs_unmount_snap(nvpair_name(pair));
3646 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3650 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3651 * All bookmarks must be in the same pool.
3654 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3657 * outnvl: bookmark -> error code (int32)
3660 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3661 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3666 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3668 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3669 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3673 * Verify the snapshot argument.
3675 if (nvpair_value_string(pair, &snap_name) != 0)
3676 return (SET_ERROR(EINVAL));
3679 /* Verify that the keys (bookmarks) are unique */
3680 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3681 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3682 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3683 return (SET_ERROR(EINVAL));
3687 return (dsl_bookmark_create(innvl, outnvl));
3692 * property 1, property 2, ...
3696 * bookmark name 1 -> { property 1, property 2, ... },
3697 * bookmark name 2 -> { property 1, property 2, ... }
3701 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3702 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3706 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3708 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3713 * bookmark name 1, bookmark name 2
3716 * outnvl: bookmark -> error code (int32)
3719 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3720 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3724 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3729 poollen = strlen(poolname);
3730 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3731 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3732 const char *name = nvpair_name(pair);
3733 const char *cp = strchr(name, '#');
3736 * The bookmark name must contain an #, and the part after it
3737 * must contain only valid characters.
3740 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3741 return (SET_ERROR(EINVAL));
3744 * The bookmark must be in the specified pool.
3746 if (strncmp(name, poolname, poollen) != 0 ||
3747 (name[poollen] != '/' && name[poollen] != '#'))
3748 return (SET_ERROR(EXDEV));
3751 error = dsl_bookmark_destroy(innvl, outnvl);
3755 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3756 {"program", DATA_TYPE_STRING, 0},
3757 {"arg", DATA_TYPE_ANY, 0},
3758 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3759 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3760 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3764 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3768 uint64_t instrlimit, memlimit;
3769 boolean_t sync_flag;
3770 nvpair_t *nvarg = NULL;
3772 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3773 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3776 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3777 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3779 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3780 memlimit = ZCP_DEFAULT_MEMLIMIT;
3782 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3784 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3786 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3789 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3797 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3803 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3805 return (spa_checkpoint(poolname));
3812 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3818 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3821 return (spa_checkpoint_discard(poolname));
3826 * zc_name name of dataset to destroy
3827 * zc_defer_destroy mark for deferred destroy
3832 zfs_ioc_destroy(zfs_cmd_t *zc)
3835 dmu_objset_type_t ost;
3838 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3841 ost = dmu_objset_type(os);
3842 dmu_objset_rele(os, FTAG);
3844 if (ost == DMU_OST_ZFS)
3845 zfs_unmount_snap(zc->zc_name);
3847 if (strchr(zc->zc_name, '@')) {
3848 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3850 err = dsl_destroy_head(zc->zc_name);
3851 if (err == EEXIST) {
3853 * It is possible that the given DS may have
3854 * hidden child (%recv) datasets - "leftovers"
3855 * resulting from the previously interrupted
3858 * 6 extra bytes for /%recv
3860 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3862 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3863 zc->zc_name, recv_clone_name) >=
3865 return (SET_ERROR(EINVAL));
3868 * Try to remove the hidden child (%recv) and after
3869 * that try to remove the target dataset.
3870 * If the hidden child (%recv) does not exist
3871 * the original error (EEXIST) will be returned
3873 err = dsl_destroy_head(namebuf);
3875 err = dsl_destroy_head(zc->zc_name);
3876 else if (err == ENOENT)
3877 err = SET_ERROR(EEXIST);
3886 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
3887 * "initialize_vdevs": { -> guids to initialize (nvlist)
3888 * "vdev_path_1": vdev_guid_1, (uint64),
3889 * "vdev_path_2": vdev_guid_2, (uint64),
3895 * "initialize_vdevs": { -> initialization errors (nvlist)
3896 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3897 * "vdev_path_2": errno, ... (uint64)
3902 * EINVAL is returned for an unknown commands or if any of the provided vdev
3903 * guids have be specified with a type other than uint64.
3905 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
3906 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
3907 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
3911 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3914 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
3916 return (SET_ERROR(EINVAL));
3919 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
3920 cmd_type == POOL_INITIALIZE_START ||
3921 cmd_type == POOL_INITIALIZE_SUSPEND)) {
3922 return (SET_ERROR(EINVAL));
3925 nvlist_t *vdev_guids;
3926 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
3927 &vdev_guids) != 0) {
3928 return (SET_ERROR(EINVAL));
3931 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
3932 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
3934 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
3935 return (SET_ERROR(EINVAL));
3940 int error = spa_open(poolname, &spa, FTAG);
3944 nvlist_t *vdev_errlist = fnvlist_alloc();
3945 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
3948 if (fnvlist_size(vdev_errlist) > 0) {
3949 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
3952 fnvlist_free(vdev_errlist);
3954 spa_close(spa, FTAG);
3955 return (total_errors > 0 ? EINVAL : 0);
3960 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
3961 * "trim_vdevs": { -> guids to TRIM (nvlist)
3962 * "vdev_path_1": vdev_guid_1, (uint64),
3963 * "vdev_path_2": vdev_guid_2, (uint64),
3966 * "trim_rate" -> Target TRIM rate in bytes/sec.
3967 * "trim_secure" -> Set to request a secure TRIM.
3971 * "trim_vdevs": { -> TRIM errors (nvlist)
3972 * "vdev_path_1": errno, see function body for possible errnos (uint64)
3973 * "vdev_path_2": errno, ... (uint64)
3978 * EINVAL is returned for an unknown commands or if any of the provided vdev
3979 * guids have be specified with a type other than uint64.
3981 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
3982 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
3983 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
3984 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
3985 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3989 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3992 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
3993 return (SET_ERROR(EINVAL));
3995 if (!(cmd_type == POOL_TRIM_CANCEL ||
3996 cmd_type == POOL_TRIM_START ||
3997 cmd_type == POOL_TRIM_SUSPEND)) {
3998 return (SET_ERROR(EINVAL));
4001 nvlist_t *vdev_guids;
4002 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4003 return (SET_ERROR(EINVAL));
4005 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4006 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4008 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4009 return (SET_ERROR(EINVAL));
4013 /* Optional, defaults to maximum rate when not provided */
4015 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4018 /* Optional, defaults to standard TRIM when not provided */
4020 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4026 int error = spa_open(poolname, &spa, FTAG);
4030 nvlist_t *vdev_errlist = fnvlist_alloc();
4031 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4032 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4034 if (fnvlist_size(vdev_errlist) > 0)
4035 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4037 fnvlist_free(vdev_errlist);
4039 spa_close(spa, FTAG);
4040 return (total_errors > 0 ? EINVAL : 0);
4044 * fsname is name of dataset to rollback (to most recent snapshot)
4046 * innvl may contain name of expected target snapshot
4048 * outnvl: "target" -> name of most recent snapshot
4051 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4052 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4057 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4061 char *target = NULL;
4064 (void) nvlist_lookup_string(innvl, "target", &target);
4065 if (target != NULL) {
4066 const char *cp = strchr(target, '@');
4069 * The snap name must contain an @, and the part after it must
4070 * contain only valid characters.
4073 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4074 return (SET_ERROR(EINVAL));
4077 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4080 ds = dmu_objset_ds(zfsvfs->z_os);
4081 error = zfs_suspend_fs(zfsvfs);
4085 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4087 resume_err = zfs_resume_fs(zfsvfs, ds);
4088 error = error ? error : resume_err;
4090 deactivate_super(zfsvfs->z_sb);
4091 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4092 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4096 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4102 recursive_unmount(const char *fsname, void *arg)
4104 const char *snapname = arg;
4107 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4108 zfs_unmount_snap(fullname);
4116 * zc_name old name of dataset
4117 * zc_value new name of dataset
4118 * zc_cookie recursive flag (only valid for snapshots)
4123 zfs_ioc_rename(zfs_cmd_t *zc)
4126 dmu_objset_type_t ost;
4127 boolean_t recursive = zc->zc_cookie & 1;
4131 /* "zfs rename" from and to ...%recv datasets should both fail */
4132 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4133 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4134 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4135 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4136 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4137 return (SET_ERROR(EINVAL));
4139 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4142 ost = dmu_objset_type(os);
4143 dmu_objset_rele(os, FTAG);
4145 at = strchr(zc->zc_name, '@');
4147 /* snaps must be in same fs */
4150 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4151 return (SET_ERROR(EXDEV));
4153 if (ost == DMU_OST_ZFS) {
4154 error = dmu_objset_find(zc->zc_name,
4155 recursive_unmount, at + 1,
4156 recursive ? DS_FIND_CHILDREN : 0);
4162 error = dsl_dataset_rename_snapshot(zc->zc_name,
4163 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4168 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4173 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4175 const char *propname = nvpair_name(pair);
4176 boolean_t issnap = (strchr(dsname, '@') != NULL);
4177 zfs_prop_t prop = zfs_name_to_prop(propname);
4181 if (prop == ZPROP_INVAL) {
4182 if (zfs_prop_user(propname)) {
4183 if ((err = zfs_secpolicy_write_perms(dsname,
4184 ZFS_DELEG_PERM_USERPROP, cr)))
4189 if (!issnap && zfs_prop_userquota(propname)) {
4190 const char *perm = NULL;
4191 const char *uq_prefix =
4192 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4193 const char *gq_prefix =
4194 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4195 const char *uiq_prefix =
4196 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4197 const char *giq_prefix =
4198 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4199 const char *pq_prefix =
4200 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4201 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4202 ZFS_PROP_PROJECTOBJQUOTA];
4204 if (strncmp(propname, uq_prefix,
4205 strlen(uq_prefix)) == 0) {
4206 perm = ZFS_DELEG_PERM_USERQUOTA;
4207 } else if (strncmp(propname, uiq_prefix,
4208 strlen(uiq_prefix)) == 0) {
4209 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4210 } else if (strncmp(propname, gq_prefix,
4211 strlen(gq_prefix)) == 0) {
4212 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4213 } else if (strncmp(propname, giq_prefix,
4214 strlen(giq_prefix)) == 0) {
4215 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4216 } else if (strncmp(propname, pq_prefix,
4217 strlen(pq_prefix)) == 0) {
4218 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4219 } else if (strncmp(propname, piq_prefix,
4220 strlen(piq_prefix)) == 0) {
4221 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4223 /* {USER|GROUP|PROJECT}USED are read-only */
4224 return (SET_ERROR(EINVAL));
4227 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4232 return (SET_ERROR(EINVAL));
4236 return (SET_ERROR(EINVAL));
4238 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4240 * dsl_prop_get_all_impl() returns properties in this
4244 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4245 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4250 * Check that this value is valid for this pool version
4253 case ZFS_PROP_COMPRESSION:
4255 * If the user specified gzip compression, make sure
4256 * the SPA supports it. We ignore any errors here since
4257 * we'll catch them later.
4259 if (nvpair_value_uint64(pair, &intval) == 0) {
4260 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4261 intval <= ZIO_COMPRESS_GZIP_9 &&
4262 zfs_earlier_version(dsname,
4263 SPA_VERSION_GZIP_COMPRESSION)) {
4264 return (SET_ERROR(ENOTSUP));
4267 if (intval == ZIO_COMPRESS_ZLE &&
4268 zfs_earlier_version(dsname,
4269 SPA_VERSION_ZLE_COMPRESSION))
4270 return (SET_ERROR(ENOTSUP));
4272 if (intval == ZIO_COMPRESS_LZ4) {
4275 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4278 if (!spa_feature_is_enabled(spa,
4279 SPA_FEATURE_LZ4_COMPRESS)) {
4280 spa_close(spa, FTAG);
4281 return (SET_ERROR(ENOTSUP));
4283 spa_close(spa, FTAG);
4287 * If this is a bootable dataset then
4288 * verify that the compression algorithm
4289 * is supported for booting. We must return
4290 * something other than ENOTSUP since it
4291 * implies a downrev pool version.
4293 if (zfs_is_bootfs(dsname) &&
4294 !BOOTFS_COMPRESS_VALID(intval)) {
4295 return (SET_ERROR(ERANGE));
4300 case ZFS_PROP_COPIES:
4301 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4302 return (SET_ERROR(ENOTSUP));
4305 case ZFS_PROP_VOLBLOCKSIZE:
4306 case ZFS_PROP_RECORDSIZE:
4307 /* Record sizes above 128k need the feature to be enabled */
4308 if (nvpair_value_uint64(pair, &intval) == 0 &&
4309 intval > SPA_OLD_MAXBLOCKSIZE) {
4313 * We don't allow setting the property above 1MB,
4314 * unless the tunable has been changed.
4316 if (intval > zfs_max_recordsize ||
4317 intval > SPA_MAXBLOCKSIZE)
4318 return (SET_ERROR(ERANGE));
4320 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4323 if (!spa_feature_is_enabled(spa,
4324 SPA_FEATURE_LARGE_BLOCKS)) {
4325 spa_close(spa, FTAG);
4326 return (SET_ERROR(ENOTSUP));
4328 spa_close(spa, FTAG);
4332 case ZFS_PROP_DNODESIZE:
4333 /* Dnode sizes above 512 need the feature to be enabled */
4334 if (nvpair_value_uint64(pair, &intval) == 0 &&
4335 intval != ZFS_DNSIZE_LEGACY) {
4339 * If this is a bootable dataset then
4340 * we don't allow large (>512B) dnodes,
4341 * because GRUB doesn't support them.
4343 if (zfs_is_bootfs(dsname) &&
4344 intval != ZFS_DNSIZE_LEGACY) {
4345 return (SET_ERROR(EDOM));
4348 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4351 if (!spa_feature_is_enabled(spa,
4352 SPA_FEATURE_LARGE_DNODE)) {
4353 spa_close(spa, FTAG);
4354 return (SET_ERROR(ENOTSUP));
4356 spa_close(spa, FTAG);
4360 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4362 * This property could require the allocation classes
4363 * feature to be active for setting, however we allow
4364 * it so that tests of settable properties succeed.
4365 * The CLI will issue a warning in this case.
4369 case ZFS_PROP_SHARESMB:
4370 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4371 return (SET_ERROR(ENOTSUP));
4374 case ZFS_PROP_ACLINHERIT:
4375 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4376 nvpair_value_uint64(pair, &intval) == 0) {
4377 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4378 zfs_earlier_version(dsname,
4379 SPA_VERSION_PASSTHROUGH_X))
4380 return (SET_ERROR(ENOTSUP));
4383 case ZFS_PROP_CHECKSUM:
4384 case ZFS_PROP_DEDUP:
4386 spa_feature_t feature;
4390 /* dedup feature version checks */
4391 if (prop == ZFS_PROP_DEDUP &&
4392 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4393 return (SET_ERROR(ENOTSUP));
4395 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4396 nvpair_value_uint64(pair, &intval) == 0) {
4397 /* check prop value is enabled in features */
4398 feature = zio_checksum_to_feature(
4399 intval & ZIO_CHECKSUM_MASK);
4400 if (feature == SPA_FEATURE_NONE)
4403 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4406 if (!spa_feature_is_enabled(spa, feature)) {
4407 spa_close(spa, FTAG);
4408 return (SET_ERROR(ENOTSUP));
4410 spa_close(spa, FTAG);
4419 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4423 * Removes properties from the given props list that fail permission checks
4424 * needed to clear them and to restore them in case of a receive error. For each
4425 * property, make sure we have both set and inherit permissions.
4427 * Returns the first error encountered if any permission checks fail. If the
4428 * caller provides a non-NULL errlist, it also gives the complete list of names
4429 * of all the properties that failed a permission check along with the
4430 * corresponding error numbers. The caller is responsible for freeing the
4433 * If every property checks out successfully, zero is returned and the list
4434 * pointed at by errlist is NULL.
4437 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4440 nvpair_t *pair, *next_pair;
4447 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4449 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4450 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4451 pair = nvlist_next_nvpair(props, NULL);
4452 while (pair != NULL) {
4453 next_pair = nvlist_next_nvpair(props, pair);
4455 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4456 sizeof (zc->zc_value));
4457 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4458 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4459 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4460 VERIFY(nvlist_add_int32(errors,
4461 zc->zc_value, err) == 0);
4465 kmem_free(zc, sizeof (zfs_cmd_t));
4467 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4468 nvlist_free(errors);
4471 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4474 if (errlist == NULL)
4475 nvlist_free(errors);
4483 propval_equals(nvpair_t *p1, nvpair_t *p2)
4485 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4486 /* dsl_prop_get_all_impl() format */
4488 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4489 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4493 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4495 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4496 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4500 if (nvpair_type(p1) != nvpair_type(p2))
4503 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4504 char *valstr1, *valstr2;
4506 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4507 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4508 return (strcmp(valstr1, valstr2) == 0);
4510 uint64_t intval1, intval2;
4512 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4513 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4514 return (intval1 == intval2);
4519 * Remove properties from props if they are not going to change (as determined
4520 * by comparison with origprops). Remove them from origprops as well, since we
4521 * do not need to clear or restore properties that won't change.
4524 props_reduce(nvlist_t *props, nvlist_t *origprops)
4526 nvpair_t *pair, *next_pair;
4528 if (origprops == NULL)
4529 return; /* all props need to be received */
4531 pair = nvlist_next_nvpair(props, NULL);
4532 while (pair != NULL) {
4533 const char *propname = nvpair_name(pair);
4536 next_pair = nvlist_next_nvpair(props, pair);
4538 if ((nvlist_lookup_nvpair(origprops, propname,
4539 &match) != 0) || !propval_equals(pair, match))
4540 goto next; /* need to set received value */
4542 /* don't clear the existing received value */
4543 (void) nvlist_remove_nvpair(origprops, match);
4544 /* don't bother receiving the property */
4545 (void) nvlist_remove_nvpair(props, pair);
4552 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4553 * For example, refquota cannot be set until after the receipt of a dataset,
4554 * because in replication streams, an older/earlier snapshot may exceed the
4555 * refquota. We want to receive the older/earlier snapshot, but setting
4556 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4557 * the older/earlier snapshot from being received (with EDQUOT).
4559 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4561 * libzfs will need to be judicious handling errors encountered by props
4562 * extracted by this function.
4565 extract_delay_props(nvlist_t *props)
4567 nvlist_t *delayprops;
4568 nvpair_t *nvp, *tmp;
4569 static const zfs_prop_t delayable[] = {
4571 ZFS_PROP_KEYLOCATION,
4576 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4578 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4579 nvp = nvlist_next_nvpair(props, nvp)) {
4581 * strcmp() is safe because zfs_prop_to_name() always returns
4584 for (i = 0; delayable[i] != 0; i++) {
4585 if (strcmp(zfs_prop_to_name(delayable[i]),
4586 nvpair_name(nvp)) == 0) {
4590 if (delayable[i] != 0) {
4591 tmp = nvlist_prev_nvpair(props, nvp);
4592 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4593 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4598 if (nvlist_empty(delayprops)) {
4599 nvlist_free(delayprops);
4602 return (delayprops);
4606 static boolean_t zfs_ioc_recv_inject_err;
4610 * nvlist 'errors' is always allocated. It will contain descriptions of
4611 * encountered errors, if any. It's the callers responsibility to free.
4614 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4615 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4616 boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4617 int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4618 uint64_t *action_handle, nvlist_t **errors)
4620 dmu_recv_cookie_t drc;
4622 int props_error = 0;
4624 nvlist_t *local_delayprops = NULL;
4625 nvlist_t *recv_delayprops = NULL;
4626 nvlist_t *origprops = NULL; /* existing properties */
4627 nvlist_t *origrecvd = NULL; /* existing received properties */
4628 boolean_t first_recvd_props = B_FALSE;
4633 *errors = fnvlist_alloc();
4635 input_fp = getf(input_fd);
4636 if (input_fp == NULL)
4637 return (SET_ERROR(EBADF));
4639 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4640 resumable, localprops, hidden_args, origin, &drc);
4645 * Set properties before we receive the stream so that they are applied
4646 * to the new data. Note that we must call dmu_recv_stream() if
4647 * dmu_recv_begin() succeeds.
4649 if (recvprops != NULL && !drc.drc_newfs) {
4650 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4651 SPA_VERSION_RECVD_PROPS &&
4652 !dsl_prop_get_hasrecvd(tofs))
4653 first_recvd_props = B_TRUE;
4656 * If new received properties are supplied, they are to
4657 * completely replace the existing received properties,
4658 * so stash away the existing ones.
4660 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4661 nvlist_t *errlist = NULL;
4663 * Don't bother writing a property if its value won't
4664 * change (and avoid the unnecessary security checks).
4666 * The first receive after SPA_VERSION_RECVD_PROPS is a
4667 * special case where we blow away all local properties
4670 if (!first_recvd_props)
4671 props_reduce(recvprops, origrecvd);
4672 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4673 (void) nvlist_merge(*errors, errlist, 0);
4674 nvlist_free(errlist);
4676 if (clear_received_props(tofs, origrecvd,
4677 first_recvd_props ? NULL : recvprops) != 0)
4678 *errflags |= ZPROP_ERR_NOCLEAR;
4680 *errflags |= ZPROP_ERR_NOCLEAR;
4685 * Stash away existing properties so we can restore them on error unless
4686 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4687 * case "origrecvd" will take care of that.
4689 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4691 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4692 if (dsl_prop_get_all(os, &origprops) != 0) {
4693 *errflags |= ZPROP_ERR_NOCLEAR;
4695 dmu_objset_rele(os, FTAG);
4697 *errflags |= ZPROP_ERR_NOCLEAR;
4701 if (recvprops != NULL) {
4702 props_error = dsl_prop_set_hasrecvd(tofs);
4704 if (props_error == 0) {
4705 recv_delayprops = extract_delay_props(recvprops);
4706 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4707 recvprops, *errors);
4711 if (localprops != NULL) {
4712 nvlist_t *oprops = fnvlist_alloc();
4713 nvlist_t *xprops = fnvlist_alloc();
4714 nvpair_t *nvp = NULL;
4716 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4717 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4719 const char *name = nvpair_name(nvp);
4720 zfs_prop_t prop = zfs_name_to_prop(name);
4721 if (prop != ZPROP_INVAL) {
4722 if (!zfs_prop_inheritable(prop))
4724 } else if (!zfs_prop_user(name))
4726 fnvlist_add_boolean(xprops, name);
4728 /* -o property=value */
4729 fnvlist_add_nvpair(oprops, nvp);
4733 local_delayprops = extract_delay_props(oprops);
4734 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4736 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4739 nvlist_free(oprops);
4740 nvlist_free(xprops);
4743 off = input_fp->f_offset;
4744 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4748 zfsvfs_t *zfsvfs = NULL;
4749 zvol_state_t *zv = NULL;
4751 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4756 ds = dmu_objset_ds(zfsvfs->z_os);
4757 error = zfs_suspend_fs(zfsvfs);
4759 * If the suspend fails, then the recv_end will
4760 * likely also fail, and clean up after itself.
4762 end_err = dmu_recv_end(&drc, zfsvfs);
4764 error = zfs_resume_fs(zfsvfs, ds);
4765 error = error ? error : end_err;
4766 deactivate_super(zfsvfs->z_sb);
4767 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4768 error = dmu_recv_end(&drc, zvol_tag(zv));
4771 error = dmu_recv_end(&drc, NULL);
4774 /* Set delayed properties now, after we're done receiving. */
4775 if (recv_delayprops != NULL && error == 0) {
4776 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4777 recv_delayprops, *errors);
4779 if (local_delayprops != NULL && error == 0) {
4780 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4781 local_delayprops, *errors);
4786 * Merge delayed props back in with initial props, in case
4787 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4788 * we have to make sure clear_received_props() includes
4789 * the delayed properties).
4791 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4792 * using ASSERT() will be just like a VERIFY.
4794 if (recv_delayprops != NULL) {
4795 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4796 nvlist_free(recv_delayprops);
4798 if (local_delayprops != NULL) {
4799 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4800 nvlist_free(local_delayprops);
4803 *read_bytes = off - input_fp->f_offset;
4804 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4805 input_fp->f_offset = off;
4808 if (zfs_ioc_recv_inject_err) {
4809 zfs_ioc_recv_inject_err = B_FALSE;
4815 * On error, restore the original props.
4817 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4818 if (clear_received_props(tofs, recvprops, NULL) != 0) {
4820 * We failed to clear the received properties.
4821 * Since we may have left a $recvd value on the
4822 * system, we can't clear the $hasrecvd flag.
4824 *errflags |= ZPROP_ERR_NORESTORE;
4825 } else if (first_recvd_props) {
4826 dsl_prop_unset_hasrecvd(tofs);
4829 if (origrecvd == NULL && !drc.drc_newfs) {
4830 /* We failed to stash the original properties. */
4831 *errflags |= ZPROP_ERR_NORESTORE;
4835 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4836 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4837 * explicitly if we're restoring local properties cleared in the
4838 * first new-style receive.
4840 if (origrecvd != NULL &&
4841 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4842 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4843 origrecvd, NULL) != 0) {
4845 * We stashed the original properties but failed to
4848 *errflags |= ZPROP_ERR_NORESTORE;
4851 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4852 !first_recvd_props) {
4854 nvlist_t *inheritprops;
4857 if (origprops == NULL) {
4858 /* We failed to stash the original properties. */
4859 *errflags |= ZPROP_ERR_NORESTORE;
4863 /* Restore original props */
4864 setprops = fnvlist_alloc();
4865 inheritprops = fnvlist_alloc();
4867 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4868 const char *name = nvpair_name(nvp);
4872 if (!nvlist_exists(origprops, name)) {
4874 * Property was not present or was explicitly
4875 * inherited before the receive, restore this.
4877 fnvlist_add_boolean(inheritprops, name);
4880 attrs = fnvlist_lookup_nvlist(origprops, name);
4881 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4883 /* Skip received properties */
4884 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4887 if (strcmp(source, tofs) == 0) {
4888 /* Property was locally set */
4889 fnvlist_add_nvlist(setprops, name, attrs);
4891 /* Property was implicitly inherited */
4892 fnvlist_add_boolean(inheritprops, name);
4896 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
4898 *errflags |= ZPROP_ERR_NORESTORE;
4899 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
4901 *errflags |= ZPROP_ERR_NORESTORE;
4903 nvlist_free(setprops);
4904 nvlist_free(inheritprops);
4908 nvlist_free(origrecvd);
4909 nvlist_free(origprops);
4912 error = props_error;
4919 * zc_name name of containing filesystem (unused)
4920 * zc_nvlist_src{_size} nvlist of properties to apply
4921 * zc_nvlist_conf{_size} nvlist of properties to exclude
4922 * (DATA_TYPE_BOOLEAN) and override (everything else)
4923 * zc_value name of snapshot to create
4924 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4925 * zc_cookie file descriptor to recv from
4926 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4927 * zc_guid force flag
4928 * zc_cleanup_fd cleanup-on-exit file descriptor
4929 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4932 * zc_cookie number of bytes read
4933 * zc_obj zprop_errflags_t
4934 * zc_action_handle handle for this guid/ds mapping
4935 * zc_nvlist_dst{_size} error for each unapplied received property
4938 zfs_ioc_recv(zfs_cmd_t *zc)
4940 dmu_replay_record_t begin_record;
4941 nvlist_t *errors = NULL;
4942 nvlist_t *recvdprops = NULL;
4943 nvlist_t *localprops = NULL;
4944 char *origin = NULL;
4946 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4949 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4950 strchr(zc->zc_value, '@') == NULL ||
4951 strchr(zc->zc_value, '%'))
4952 return (SET_ERROR(EINVAL));
4954 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
4955 tosnap = strchr(tofs, '@');
4958 if (zc->zc_nvlist_src != 0 &&
4959 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4960 zc->zc_iflags, &recvdprops)) != 0)
4963 if (zc->zc_nvlist_conf != 0 &&
4964 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
4965 zc->zc_iflags, &localprops)) != 0)
4968 if (zc->zc_string[0])
4969 origin = zc->zc_string;
4971 begin_record.drr_type = DRR_BEGIN;
4972 begin_record.drr_payloadlen = 0;
4973 begin_record.drr_u.drr_begin = zc->zc_begin_record;
4975 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
4976 NULL, zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
4977 zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
4978 &zc->zc_action_handle, &errors);
4979 nvlist_free(recvdprops);
4980 nvlist_free(localprops);
4983 * Now that all props, initial and delayed, are set, report the prop
4984 * errors to the caller.
4986 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
4987 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4988 put_nvlist(zc, errors) != 0)) {
4990 * Caller made zc->zc_nvlist_dst less than the minimum expected
4991 * size or supplied an invalid address.
4993 error = SET_ERROR(EINVAL);
4996 nvlist_free(errors);
5003 * "snapname" -> full name of the snapshot to create
5004 * (optional) "props" -> received properties to set (nvlist)
5005 * (optional) "localprops" -> override and exclude properties (nvlist)
5006 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5007 * "begin_record" -> non-byteswapped dmu_replay_record_t
5008 * "input_fd" -> file descriptor to read stream from (int32)
5009 * (optional) "force" -> force flag (value ignored)
5010 * (optional) "resumable" -> resumable flag (value ignored)
5011 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
5012 * (optional) "action_handle" -> handle for this guid/ds mapping
5013 * (optional) "hidden_args" -> { "wkeydata" -> value }
5017 * "read_bytes" -> number of bytes read
5018 * "error_flags" -> zprop_errflags_t
5019 * "action_handle" -> handle for this guid/ds mapping
5020 * "errors" -> error for each unapplied received property (nvlist)
5023 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5024 {"snapname", DATA_TYPE_STRING, 0},
5025 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5026 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5027 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5028 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5029 {"input_fd", DATA_TYPE_INT32, 0},
5030 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5031 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5032 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5033 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5034 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5038 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5040 dmu_replay_record_t *begin_record;
5041 uint_t begin_record_size;
5042 nvlist_t *errors = NULL;
5043 nvlist_t *recvprops = NULL;
5044 nvlist_t *localprops = NULL;
5045 nvlist_t *hidden_args = NULL;
5047 char *origin = NULL;
5049 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5051 boolean_t resumable;
5052 uint64_t action_handle = 0;
5053 uint64_t read_bytes = 0;
5054 uint64_t errflags = 0;
5056 int cleanup_fd = -1;
5059 snapname = fnvlist_lookup_string(innvl, "snapname");
5061 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5062 strchr(snapname, '@') == NULL ||
5063 strchr(snapname, '%'))
5064 return (SET_ERROR(EINVAL));
5066 (void) strcpy(tofs, snapname);
5067 tosnap = strchr(tofs, '@');
5070 error = nvlist_lookup_string(innvl, "origin", &origin);
5071 if (error && error != ENOENT)
5074 error = nvlist_lookup_byte_array(innvl, "begin_record",
5075 (uchar_t **)&begin_record, &begin_record_size);
5076 if (error != 0 || begin_record_size != sizeof (*begin_record))
5077 return (SET_ERROR(EINVAL));
5079 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5081 force = nvlist_exists(innvl, "force");
5082 resumable = nvlist_exists(innvl, "resumable");
5084 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
5085 if (error && error != ENOENT)
5088 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
5089 if (error && error != ENOENT)
5092 /* we still use "props" here for backwards compatibility */
5093 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5094 if (error && error != ENOENT)
5097 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5098 if (error && error != ENOENT)
5101 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5102 if (error && error != ENOENT)
5105 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5106 hidden_args, force, resumable, input_fd, begin_record, cleanup_fd,
5107 &read_bytes, &errflags, &action_handle, &errors);
5109 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5110 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5111 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
5112 fnvlist_add_nvlist(outnvl, "errors", errors);
5114 nvlist_free(errors);
5115 nvlist_free(recvprops);
5116 nvlist_free(localprops);
5123 * zc_name name of snapshot to send
5124 * zc_cookie file descriptor to send stream to
5125 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5126 * zc_sendobj objsetid of snapshot to send
5127 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5128 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5129 * output size in zc_objset_type.
5130 * zc_flags lzc_send_flags
5133 * zc_objset_type estimated size, if zc_guid is set
5135 * NOTE: This is no longer the preferred interface, any new functionality
5136 * should be added to zfs_ioc_send_new() instead.
5139 zfs_ioc_send(zfs_cmd_t *zc)
5143 boolean_t estimate = (zc->zc_guid != 0);
5144 boolean_t embedok = (zc->zc_flags & 0x1);
5145 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5146 boolean_t compressok = (zc->zc_flags & 0x4);
5147 boolean_t rawok = (zc->zc_flags & 0x8);
5149 if (zc->zc_obj != 0) {
5151 dsl_dataset_t *tosnap;
5153 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5157 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5159 dsl_pool_rele(dp, FTAG);
5163 if (dsl_dir_is_clone(tosnap->ds_dir))
5165 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5166 dsl_dataset_rele(tosnap, FTAG);
5167 dsl_pool_rele(dp, FTAG);
5172 dsl_dataset_t *tosnap;
5173 dsl_dataset_t *fromsnap = NULL;
5175 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5179 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5182 dsl_pool_rele(dp, FTAG);
5186 if (zc->zc_fromobj != 0) {
5187 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5190 dsl_dataset_rele(tosnap, FTAG);
5191 dsl_pool_rele(dp, FTAG);
5196 error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
5197 &zc->zc_objset_type);
5199 if (fromsnap != NULL)
5200 dsl_dataset_rele(fromsnap, FTAG);
5201 dsl_dataset_rele(tosnap, FTAG);
5202 dsl_pool_rele(dp, FTAG);
5204 file_t *fp = getf(zc->zc_cookie);
5206 return (SET_ERROR(EBADF));
5209 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5210 zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
5211 zc->zc_cookie, fp->f_vnode, &off);
5213 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5215 releasef(zc->zc_cookie);
5222 * zc_name name of snapshot on which to report progress
5223 * zc_cookie file descriptor of send stream
5226 * zc_cookie number of bytes written in send stream thus far
5229 zfs_ioc_send_progress(zfs_cmd_t *zc)
5233 dmu_sendarg_t *dsp = NULL;
5236 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5240 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5242 dsl_pool_rele(dp, FTAG);
5246 mutex_enter(&ds->ds_sendstream_lock);
5249 * Iterate over all the send streams currently active on this dataset.
5250 * If there's one which matches the specified file descriptor _and_ the
5251 * stream was started by the current process, return the progress of
5255 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5256 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5257 if (dsp->dsa_outfd == zc->zc_cookie &&
5258 dsp->dsa_proc->group_leader == curproc->group_leader)
5263 zc->zc_cookie = *(dsp->dsa_off);
5265 error = SET_ERROR(ENOENT);
5267 mutex_exit(&ds->ds_sendstream_lock);
5268 dsl_dataset_rele(ds, FTAG);
5269 dsl_pool_rele(dp, FTAG);
5274 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5278 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5279 &zc->zc_inject_record);
5282 zc->zc_guid = (uint64_t)id;
5288 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5290 return (zio_clear_fault((int)zc->zc_guid));
5294 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5296 int id = (int)zc->zc_guid;
5299 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5300 &zc->zc_inject_record);
5308 zfs_ioc_error_log(zfs_cmd_t *zc)
5312 size_t count = (size_t)zc->zc_nvlist_dst_size;
5314 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5317 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5320 zc->zc_nvlist_dst_size = count;
5322 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5324 spa_close(spa, FTAG);
5330 zfs_ioc_clear(zfs_cmd_t *zc)
5337 * On zpool clear we also fix up missing slogs
5339 mutex_enter(&spa_namespace_lock);
5340 spa = spa_lookup(zc->zc_name);
5342 mutex_exit(&spa_namespace_lock);
5343 return (SET_ERROR(EIO));
5345 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5346 /* we need to let spa_open/spa_load clear the chains */
5347 spa_set_log_state(spa, SPA_LOG_CLEAR);
5349 spa->spa_last_open_failed = 0;
5350 mutex_exit(&spa_namespace_lock);
5352 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5353 error = spa_open(zc->zc_name, &spa, FTAG);
5356 nvlist_t *config = NULL;
5358 if (zc->zc_nvlist_src == 0)
5359 return (SET_ERROR(EINVAL));
5361 if ((error = get_nvlist(zc->zc_nvlist_src,
5362 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5363 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5365 if (config != NULL) {
5368 if ((err = put_nvlist(zc, config)) != 0)
5370 nvlist_free(config);
5372 nvlist_free(policy);
5380 * If multihost is enabled, resuming I/O is unsafe as another
5381 * host may have imported the pool.
5383 if (spa_multihost(spa) && spa_suspended(spa))
5384 return (SET_ERROR(EINVAL));
5386 spa_vdev_state_enter(spa, SCL_NONE);
5388 if (zc->zc_guid == 0) {
5391 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5393 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5394 spa_close(spa, FTAG);
5395 return (SET_ERROR(ENODEV));
5399 vdev_clear(spa, vd);
5401 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5402 NULL : spa->spa_root_vdev, 0);
5405 * Resume any suspended I/Os.
5407 if (zio_resume(spa) != 0)
5408 error = SET_ERROR(EIO);
5410 spa_close(spa, FTAG);
5416 * Reopen all the vdevs associated with the pool.
5419 * "scrub_restart" -> when true and scrub is running, allow to restart
5420 * scrub as the side effect of the reopen (boolean).
5425 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5426 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, 0},
5431 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5435 boolean_t scrub_restart = B_TRUE;
5438 scrub_restart = fnvlist_lookup_boolean_value(innvl,
5442 error = spa_open(pool, &spa, FTAG);
5446 spa_vdev_state_enter(spa, SCL_NONE);
5449 * If the scrub_restart flag is B_FALSE and a scrub is already
5450 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5451 * we don't restart the scrub as a side effect of the reopen.
5452 * Otherwise, let vdev_open() decided if a resilver is required.
5455 spa->spa_scrub_reopen = (!scrub_restart &&
5456 dsl_scan_scrubbing(spa->spa_dsl_pool));
5457 vdev_reopen(spa->spa_root_vdev);
5458 spa->spa_scrub_reopen = B_FALSE;
5460 (void) spa_vdev_state_exit(spa, NULL, 0);
5461 spa_close(spa, FTAG);
5467 * zc_name name of filesystem
5470 * zc_string name of conflicting snapshot, if there is one
5473 zfs_ioc_promote(zfs_cmd_t *zc)
5476 dsl_dataset_t *ds, *ods;
5477 char origin[ZFS_MAX_DATASET_NAME_LEN];
5481 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5482 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5483 strchr(zc->zc_name, '%'))
5484 return (SET_ERROR(EINVAL));
5486 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5490 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5492 dsl_pool_rele(dp, FTAG);
5496 if (!dsl_dir_is_clone(ds->ds_dir)) {
5497 dsl_dataset_rele(ds, FTAG);
5498 dsl_pool_rele(dp, FTAG);
5499 return (SET_ERROR(EINVAL));
5502 error = dsl_dataset_hold_obj(dp,
5503 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5505 dsl_dataset_rele(ds, FTAG);
5506 dsl_pool_rele(dp, FTAG);
5510 dsl_dataset_name(ods, origin);
5511 dsl_dataset_rele(ods, FTAG);
5512 dsl_dataset_rele(ds, FTAG);
5513 dsl_pool_rele(dp, FTAG);
5516 * We don't need to unmount *all* the origin fs's snapshots, but
5519 cp = strchr(origin, '@');
5522 (void) dmu_objset_find(origin,
5523 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5524 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5528 * Retrieve a single {user|group|project}{used|quota}@... property.
5531 * zc_name name of filesystem
5532 * zc_objset_type zfs_userquota_prop_t
5533 * zc_value domain name (eg. "S-1-234-567-89")
5534 * zc_guid RID/UID/GID
5537 * zc_cookie property value
5540 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5545 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5546 return (SET_ERROR(EINVAL));
5548 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5552 error = zfs_userspace_one(zfsvfs,
5553 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5554 zfsvfs_rele(zfsvfs, FTAG);
5561 * zc_name name of filesystem
5562 * zc_cookie zap cursor
5563 * zc_objset_type zfs_userquota_prop_t
5564 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5567 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5568 * zc_cookie zap cursor
5571 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5574 int bufsize = zc->zc_nvlist_dst_size;
5577 return (SET_ERROR(ENOMEM));
5579 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5583 void *buf = vmem_alloc(bufsize, KM_SLEEP);
5585 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5586 buf, &zc->zc_nvlist_dst_size);
5589 error = xcopyout(buf,
5590 (void *)(uintptr_t)zc->zc_nvlist_dst,
5591 zc->zc_nvlist_dst_size);
5593 vmem_free(buf, bufsize);
5594 zfsvfs_rele(zfsvfs, FTAG);
5601 * zc_name name of filesystem
5607 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5613 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5614 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5616 * If userused is not enabled, it may be because the
5617 * objset needs to be closed & reopened (to grow the
5618 * objset_phys_t). Suspend/resume the fs will do that.
5620 dsl_dataset_t *ds, *newds;
5622 ds = dmu_objset_ds(zfsvfs->z_os);
5623 error = zfs_suspend_fs(zfsvfs);
5625 dmu_objset_refresh_ownership(ds, &newds,
5627 error = zfs_resume_fs(zfsvfs, newds);
5631 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5632 deactivate_super(zfsvfs->z_sb);
5634 /* XXX kind of reading contents without owning */
5635 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5639 error = dmu_objset_userspace_upgrade(os);
5640 dmu_objset_rele_flags(os, B_TRUE, FTAG);
5648 * zc_name name of filesystem
5654 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5659 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5663 if (dmu_objset_userobjspace_upgradable(os) ||
5664 dmu_objset_projectquota_upgradable(os)) {
5665 mutex_enter(&os->os_upgrade_lock);
5666 if (os->os_upgrade_id == 0) {
5667 /* clear potential error code and retry */
5668 os->os_upgrade_status = 0;
5669 mutex_exit(&os->os_upgrade_lock);
5671 dmu_objset_id_quota_upgrade(os);
5673 mutex_exit(&os->os_upgrade_lock);
5676 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5678 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5679 error = os->os_upgrade_status;
5681 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5684 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
5690 zfs_ioc_share(zfs_cmd_t *zc)
5692 return (SET_ERROR(ENOSYS));
5695 ace_t full_access[] = {
5696 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5701 * zc_name name of containing filesystem
5702 * zc_obj object # beyond which we want next in-use object #
5705 * zc_obj next in-use object #
5708 zfs_ioc_next_obj(zfs_cmd_t *zc)
5710 objset_t *os = NULL;
5713 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5717 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
5719 dmu_objset_rele(os, FTAG);
5725 * zc_name name of filesystem
5726 * zc_value prefix name for snapshot
5727 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5730 * zc_value short name of new snapshot
5733 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5740 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5744 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5745 (u_longlong_t)ddi_get_lbolt64());
5746 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5748 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5751 (void) strlcpy(zc->zc_value, snap_name,
5752 sizeof (zc->zc_value));
5755 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5761 * zc_name name of "to" snapshot
5762 * zc_value name of "from" snapshot
5763 * zc_cookie file descriptor to write diff data on
5766 * dmu_diff_record_t's to the file descriptor
5769 zfs_ioc_diff(zfs_cmd_t *zc)
5775 fp = getf(zc->zc_cookie);
5777 return (SET_ERROR(EBADF));
5781 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5783 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5785 releasef(zc->zc_cookie);
5791 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5793 return (SET_ERROR(ENOTSUP));
5798 * "holds" -> { snapname -> holdname (string), ... }
5799 * (optional) "cleanup_fd" -> fd (int32)
5803 * snapname -> error value (int32)
5807 static const zfs_ioc_key_t zfs_keys_hold[] = {
5808 {"holds", DATA_TYPE_NVLIST, 0},
5809 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5814 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5818 int cleanup_fd = -1;
5822 holds = fnvlist_lookup_nvlist(args, "holds");
5824 /* make sure the user didn't pass us any invalid (empty) tags */
5825 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5826 pair = nvlist_next_nvpair(holds, pair)) {
5829 error = nvpair_value_string(pair, &htag);
5831 return (SET_ERROR(error));
5833 if (strlen(htag) == 0)
5834 return (SET_ERROR(EINVAL));
5837 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5838 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5843 error = dsl_dataset_user_hold(holds, minor, errlist);
5845 zfs_onexit_fd_rele(cleanup_fd);
5850 * innvl is not used.
5853 * holdname -> time added (uint64 seconds since epoch)
5857 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
5863 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5865 return (dsl_dataset_get_holds(snapname, outnvl));
5870 * snapname -> { holdname, ... }
5875 * snapname -> error value (int32)
5879 static const zfs_ioc_key_t zfs_keys_release[] = {
5880 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
5885 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5887 return (dsl_dataset_user_release(holds, errlist));
5892 * zc_guid flags (ZEVENT_NONBLOCK)
5893 * zc_cleanup_fd zevent file descriptor
5896 * zc_nvlist_dst next nvlist event
5897 * zc_cookie dropped events since last get
5900 zfs_ioc_events_next(zfs_cmd_t *zc)
5903 nvlist_t *event = NULL;
5905 uint64_t dropped = 0;
5908 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5913 error = zfs_zevent_next(ze, &event,
5914 &zc->zc_nvlist_dst_size, &dropped);
5915 if (event != NULL) {
5916 zc->zc_cookie = dropped;
5917 error = put_nvlist(zc, event);
5921 if (zc->zc_guid & ZEVENT_NONBLOCK)
5924 if ((error == 0) || (error != ENOENT))
5927 error = zfs_zevent_wait(ze);
5932 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5939 * zc_cookie cleared events count
5942 zfs_ioc_events_clear(zfs_cmd_t *zc)
5946 zfs_zevent_drain_all(&count);
5947 zc->zc_cookie = count;
5954 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5955 * zc_cleanup zevent file descriptor
5958 zfs_ioc_events_seek(zfs_cmd_t *zc)
5964 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5968 error = zfs_zevent_seek(ze, zc->zc_guid);
5969 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5976 * zc_name name of new filesystem or snapshot
5977 * zc_value full name of old snapshot
5980 * zc_cookie space in bytes
5981 * zc_objset_type compressed space in bytes
5982 * zc_perm_action uncompressed space in bytes
5985 zfs_ioc_space_written(zfs_cmd_t *zc)
5989 dsl_dataset_t *new, *old;
5991 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5994 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5996 dsl_pool_rele(dp, FTAG);
5999 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6001 dsl_dataset_rele(new, FTAG);
6002 dsl_pool_rele(dp, FTAG);
6006 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
6007 &zc->zc_objset_type, &zc->zc_perm_action);
6008 dsl_dataset_rele(old, FTAG);
6009 dsl_dataset_rele(new, FTAG);
6010 dsl_pool_rele(dp, FTAG);
6016 * "firstsnap" -> snapshot name
6020 * "used" -> space in bytes
6021 * "compressed" -> compressed space in bytes
6022 * "uncompressed" -> uncompressed space in bytes
6025 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6026 {"firstsnap", DATA_TYPE_STRING, 0},
6030 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6034 dsl_dataset_t *new, *old;
6036 uint64_t used, comp, uncomp;
6038 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6040 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6044 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6045 if (error == 0 && !new->ds_is_snapshot) {
6046 dsl_dataset_rele(new, FTAG);
6047 error = SET_ERROR(EINVAL);
6050 dsl_pool_rele(dp, FTAG);
6053 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6054 if (error == 0 && !old->ds_is_snapshot) {
6055 dsl_dataset_rele(old, FTAG);
6056 error = SET_ERROR(EINVAL);
6059 dsl_dataset_rele(new, FTAG);
6060 dsl_pool_rele(dp, FTAG);
6064 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6065 dsl_dataset_rele(old, FTAG);
6066 dsl_dataset_rele(new, FTAG);
6067 dsl_pool_rele(dp, FTAG);
6068 fnvlist_add_uint64(outnvl, "used", used);
6069 fnvlist_add_uint64(outnvl, "compressed", comp);
6070 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6076 * "fd" -> file descriptor to write stream to (int32)
6077 * (optional) "fromsnap" -> full snap name to send an incremental from
6078 * (optional) "largeblockok" -> (value ignored)
6079 * indicates that blocks > 128KB are permitted
6080 * (optional) "embedok" -> (value ignored)
6081 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6082 * (optional) "compressok" -> (value ignored)
6083 * presence indicates compressed DRR_WRITE records are permitted
6084 * (optional) "rawok" -> (value ignored)
6085 * presence indicates raw encrypted records should be used.
6086 * (optional) "resume_object" and "resume_offset" -> (uint64)
6087 * if present, resume send stream from specified object and offset.
6092 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6093 {"fd", DATA_TYPE_INT32, 0},
6094 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6095 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6096 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6097 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6098 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6099 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6100 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6105 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6109 char *fromname = NULL;
6112 boolean_t largeblockok;
6114 boolean_t compressok;
6116 uint64_t resumeobj = 0;
6117 uint64_t resumeoff = 0;
6119 fd = fnvlist_lookup_int32(innvl, "fd");
6121 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6123 largeblockok = nvlist_exists(innvl, "largeblockok");
6124 embedok = nvlist_exists(innvl, "embedok");
6125 compressok = nvlist_exists(innvl, "compressok");
6126 rawok = nvlist_exists(innvl, "rawok");
6128 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6129 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6131 if ((fp = getf(fd)) == NULL)
6132 return (SET_ERROR(EBADF));
6135 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6136 rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
6138 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
6146 * Determine approximately how large a zfs send stream will be -- the number
6147 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6150 * (optional) "from" -> full snap or bookmark name to send an incremental
6152 * (optional) "largeblockok" -> (value ignored)
6153 * indicates that blocks > 128KB are permitted
6154 * (optional) "embedok" -> (value ignored)
6155 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6156 * (optional) "compressok" -> (value ignored)
6157 * presence indicates compressed DRR_WRITE records are permitted
6158 * (optional) "rawok" -> (value ignored)
6159 * presence indicates raw encrypted records should be used.
6163 * "space" -> bytes of space (uint64)
6166 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6167 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6168 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6169 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6170 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6171 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6172 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6176 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6179 dsl_dataset_t *tosnap;
6182 boolean_t compressok;
6186 error = dsl_pool_hold(snapname, FTAG, &dp);
6190 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6192 dsl_pool_rele(dp, FTAG);
6196 compressok = nvlist_exists(innvl, "compressok");
6197 rawok = nvlist_exists(innvl, "rawok");
6199 error = nvlist_lookup_string(innvl, "from", &fromname);
6201 if (strchr(fromname, '@') != NULL) {
6203 * If from is a snapshot, hold it and use the more
6204 * efficient dmu_send_estimate to estimate send space
6205 * size using deadlists.
6207 dsl_dataset_t *fromsnap;
6208 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6211 error = dmu_send_estimate(tosnap, fromsnap,
6212 compressok || rawok, &space);
6213 dsl_dataset_rele(fromsnap, FTAG);
6214 } else if (strchr(fromname, '#') != NULL) {
6216 * If from is a bookmark, fetch the creation TXG of the
6217 * snapshot it was created from and use that to find
6218 * blocks that were born after it.
6220 zfs_bookmark_phys_t frombm;
6222 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6226 error = dmu_send_estimate_from_txg(tosnap,
6227 frombm.zbm_creation_txg, compressok || rawok,
6231 * from is not properly formatted as a snapshot or
6234 error = SET_ERROR(EINVAL);
6239 * If estimating the size of a full send, use dmu_send_estimate.
6241 error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6245 fnvlist_add_uint64(outnvl, "space", space);
6248 dsl_dataset_rele(tosnap, FTAG);
6249 dsl_pool_rele(dp, FTAG);
6254 * Sync the currently open TXG to disk for the specified pool.
6255 * This is somewhat similar to 'zfs_sync()'.
6256 * For cases that do not result in error this ioctl will wait for
6257 * the currently open TXG to commit before returning back to the caller.
6260 * "force" -> when true, force uberblock update even if there is no dirty data.
6261 * In addition this will cause the vdev configuration to be written
6262 * out including updating the zpool cache file. (boolean_t)
6267 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6268 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6273 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6276 boolean_t force = B_FALSE;
6279 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6283 force = fnvlist_lookup_boolean_value(innvl, "force");
6286 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6287 vdev_config_dirty(spa->spa_root_vdev);
6288 spa_config_exit(spa, SCL_CONFIG, FTAG);
6290 txg_wait_synced(spa_get_dsl(spa), 0);
6292 spa_close(spa, FTAG);
6298 * Load a user's wrapping key into the kernel.
6300 * "hidden_args" -> { "wkeydata" -> value }
6301 * raw uint8_t array of encryption wrapping key data (32 bytes)
6302 * (optional) "noop" -> (value ignored)
6303 * presence indicated key should only be verified, not loaded
6306 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6307 {"hidden_args", DATA_TYPE_NVLIST, 0},
6308 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6313 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6316 dsl_crypto_params_t *dcp = NULL;
6317 nvlist_t *hidden_args;
6318 boolean_t noop = nvlist_exists(innvl, "noop");
6320 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6321 ret = SET_ERROR(EINVAL);
6325 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6327 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6332 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6336 dsl_crypto_params_free(dcp, noop);
6341 dsl_crypto_params_free(dcp, B_TRUE);
6346 * Unload a user's wrapping key from the kernel.
6347 * Both innvl and outnvl are unused.
6349 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6355 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6359 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6360 ret = (SET_ERROR(EINVAL));
6364 ret = spa_keystore_unload_wkey(dsname);
6373 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6374 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6375 * here to change how the key is derived in userspace.
6378 * "hidden_args" (optional) -> { "wkeydata" -> value }
6379 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6380 * "props" (optional) -> { prop -> value }
6385 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6386 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6387 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6388 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6393 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6396 uint64_t cmd = DCP_CMD_NONE;
6397 dsl_crypto_params_t *dcp = NULL;
6398 nvlist_t *args = NULL, *hidden_args = NULL;
6400 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6401 ret = (SET_ERROR(EINVAL));
6405 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6406 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6407 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6409 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6413 ret = spa_keystore_change_key(dsname, dcp);
6417 dsl_crypto_params_free(dcp, B_FALSE);
6422 dsl_crypto_params_free(dcp, B_TRUE);
6426 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6429 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6430 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6431 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6433 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6435 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6436 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6437 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6438 ASSERT3P(vec->zvec_func, ==, NULL);
6440 vec->zvec_legacy_func = func;
6441 vec->zvec_secpolicy = secpolicy;
6442 vec->zvec_namecheck = namecheck;
6443 vec->zvec_allow_log = log_history;
6444 vec->zvec_pool_check = pool_check;
6448 * See the block comment at the beginning of this file for details on
6449 * each argument to this function.
6452 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6453 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6454 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6455 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6457 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6459 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6460 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6461 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6462 ASSERT3P(vec->zvec_func, ==, NULL);
6464 /* if we are logging, the name must be valid */
6465 ASSERT(!allow_log || namecheck != NO_NAME);
6467 vec->zvec_name = name;
6468 vec->zvec_func = func;
6469 vec->zvec_secpolicy = secpolicy;
6470 vec->zvec_namecheck = namecheck;
6471 vec->zvec_pool_check = pool_check;
6472 vec->zvec_smush_outnvlist = smush_outnvlist;
6473 vec->zvec_allow_log = allow_log;
6474 vec->zvec_nvl_keys = nvl_keys;
6475 vec->zvec_nvl_key_count = num_keys;
6479 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6480 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6481 zfs_ioc_poolcheck_t pool_check)
6483 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6484 POOL_NAME, log_history, pool_check);
6488 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6489 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6491 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6492 DATASET_NAME, B_FALSE, pool_check);
6496 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6498 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6499 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6503 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6504 zfs_secpolicy_func_t *secpolicy)
6506 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6507 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6511 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6512 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6514 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6515 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6519 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6521 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6522 zfs_secpolicy_read);
6526 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6527 zfs_secpolicy_func_t *secpolicy)
6529 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6530 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6534 zfs_ioctl_init(void)
6536 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6537 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6538 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6539 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6541 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6542 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6543 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6544 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6546 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6547 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6548 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6549 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6551 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6552 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6553 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6554 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6556 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6557 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6558 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6559 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6561 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6562 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6563 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6564 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6566 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6567 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6568 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6569 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6571 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6572 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6573 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6574 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6576 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6577 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6578 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6579 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6581 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6582 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6583 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6584 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6585 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6586 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6587 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6588 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6590 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6591 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6592 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6593 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6595 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6596 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6597 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6598 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6600 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6601 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6602 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6603 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6605 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6606 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6607 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6608 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6610 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6611 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6613 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6614 zfs_keys_destroy_bookmarks,
6615 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6617 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6618 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
6619 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6620 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
6621 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6622 zfs_ioc_load_key, zfs_secpolicy_load_key,
6623 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6624 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
6625 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6626 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6627 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
6628 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
6629 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6630 zfs_ioc_change_key, zfs_secpolicy_change_key,
6631 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6632 B_TRUE, B_TRUE, zfs_keys_change_key,
6633 ARRAY_SIZE(zfs_keys_change_key));
6635 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6636 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6637 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6638 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6639 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6640 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
6641 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
6643 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6644 zfs_ioc_channel_program, zfs_secpolicy_config,
6645 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6646 B_TRUE, zfs_keys_channel_program,
6647 ARRAY_SIZE(zfs_keys_channel_program));
6649 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6650 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6651 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6652 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6654 zfs_ioctl_register("zpool_discard_checkpoint",
6655 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6656 zfs_secpolicy_config, POOL_NAME,
6657 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6658 zfs_keys_pool_discard_checkpoint,
6659 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
6661 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6662 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6663 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6664 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
6666 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
6667 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
6668 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6669 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
6671 /* IOCTLS that use the legacy function signature */
6673 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6674 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6676 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6677 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6678 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6680 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6681 zfs_ioc_pool_upgrade);
6682 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6684 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6685 zfs_ioc_vdev_remove);
6686 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6687 zfs_ioc_vdev_set_state);
6688 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6689 zfs_ioc_vdev_attach);
6690 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6691 zfs_ioc_vdev_detach);
6692 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6693 zfs_ioc_vdev_setpath);
6694 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6695 zfs_ioc_vdev_setfru);
6696 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6697 zfs_ioc_pool_set_props);
6698 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6699 zfs_ioc_vdev_split);
6700 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6701 zfs_ioc_pool_reguid);
6703 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6704 zfs_ioc_pool_configs, zfs_secpolicy_none);
6705 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6706 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6707 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6708 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6709 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6710 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6711 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6712 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6715 * pool destroy, and export don't log the history as part of
6716 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6717 * does the logging of those commands.
6719 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6720 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6721 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6722 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6724 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6725 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6726 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6727 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6729 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6730 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6731 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6732 zfs_ioc_dsobj_to_dsname,
6733 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6734 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6735 zfs_ioc_pool_get_history,
6736 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6738 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6739 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6741 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6742 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6744 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6745 zfs_ioc_space_written);
6746 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6747 zfs_ioc_objset_recvd_props);
6748 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6750 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6752 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6753 zfs_ioc_objset_stats);
6754 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6755 zfs_ioc_objset_zplprops);
6756 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6757 zfs_ioc_dataset_list_next);
6758 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6759 zfs_ioc_snapshot_list_next);
6760 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6761 zfs_ioc_send_progress);
6763 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6764 zfs_ioc_diff, zfs_secpolicy_diff);
6765 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6766 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6767 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6768 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6769 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6770 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6771 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6772 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6773 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6774 zfs_ioc_send, zfs_secpolicy_send);
6776 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6777 zfs_secpolicy_none);
6778 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6779 zfs_secpolicy_destroy);
6780 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6781 zfs_secpolicy_rename);
6782 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6783 zfs_secpolicy_recv);
6784 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6785 zfs_secpolicy_promote);
6786 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6787 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6788 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6789 zfs_secpolicy_set_fsacl);
6791 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6792 zfs_secpolicy_share, POOL_CHECK_NONE);
6793 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6794 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6795 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6796 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6797 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6798 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6799 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6800 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6805 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
6806 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6807 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
6808 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6809 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
6810 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6814 * Verify that for non-legacy ioctls the input nvlist
6815 * pairs match against the expected input.
6817 * Possible errors are:
6818 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
6819 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
6820 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
6823 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
6825 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
6826 boolean_t required_keys_found = B_FALSE;
6829 * examine each input pair
6831 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
6832 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
6833 char *name = nvpair_name(pair);
6834 data_type_t type = nvpair_type(pair);
6835 boolean_t identified = B_FALSE;
6838 * check pair against the documented names and type
6840 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6841 /* if not a wild card name, check for an exact match */
6842 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
6843 strcmp(nvl_keys[k].zkey_name, name) != 0)
6846 identified = B_TRUE;
6848 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
6849 nvl_keys[k].zkey_type != type) {
6850 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
6853 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6856 required_keys_found = B_TRUE;
6860 /* allow an 'optional' key, everything else is invalid */
6862 (strcmp(name, "optional") != 0 ||
6863 type != DATA_TYPE_NVLIST)) {
6864 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
6868 /* verify that all required keys were found */
6869 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6870 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6873 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
6874 /* at least one non-optionial key is expected here */
6875 if (!required_keys_found)
6876 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6880 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
6881 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6888 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6889 zfs_ioc_poolcheck_t check)
6894 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6896 if (check & POOL_CHECK_NONE)
6899 error = spa_open(name, &spa, FTAG);
6901 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6902 error = SET_ERROR(EAGAIN);
6903 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6904 error = SET_ERROR(EROFS);
6905 spa_close(spa, FTAG);
6911 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
6915 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6916 if (zs->zs_minor == minor) {
6920 return (zs->zs_onexit);
6922 return (zs->zs_zevent);
6933 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
6937 ptr = zfsdev_get_state_impl(minor, which);
6943 zfsdev_getminor(struct file *filp, minor_t *minorp)
6945 zfsdev_state_t *zs, *fpd;
6947 ASSERT(filp != NULL);
6948 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
6950 fpd = filp->private_data;
6952 return (SET_ERROR(EBADF));
6954 mutex_enter(&zfsdev_state_lock);
6956 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6958 if (zs->zs_minor == -1)
6962 *minorp = fpd->zs_minor;
6963 mutex_exit(&zfsdev_state_lock);
6968 mutex_exit(&zfsdev_state_lock);
6970 return (SET_ERROR(EBADF));
6974 * Find a free minor number. The zfsdev_state_list is expected to
6975 * be short since it is only a list of currently open file handles.
6978 zfsdev_minor_alloc(void)
6980 static minor_t last_minor = 0;
6983 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6985 for (m = last_minor + 1; m != last_minor; m++) {
6986 if (m > ZFSDEV_MAX_MINOR)
6988 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
6998 zfsdev_state_init(struct file *filp)
7000 zfsdev_state_t *zs, *zsprev = NULL;
7002 boolean_t newzs = B_FALSE;
7004 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7006 minor = zfsdev_minor_alloc();
7008 return (SET_ERROR(ENXIO));
7010 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7011 if (zs->zs_minor == -1)
7017 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7022 filp->private_data = zs;
7024 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7025 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7029 * In order to provide for lock-free concurrent read access
7030 * to the minor list in zfsdev_get_state_impl(), new entries
7031 * must be completely written before linking them into the
7032 * list whereas existing entries are already linked; the last
7033 * operation must be updating zs_minor (from -1 to the new
7037 zs->zs_minor = minor;
7039 zsprev->zs_next = zs;
7042 zs->zs_minor = minor;
7049 zfsdev_state_destroy(struct file *filp)
7053 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7054 ASSERT(filp->private_data != NULL);
7056 zs = filp->private_data;
7058 zfs_onexit_destroy(zs->zs_onexit);
7059 zfs_zevent_destroy(zs->zs_zevent);
7065 zfsdev_open(struct inode *ino, struct file *filp)
7069 mutex_enter(&zfsdev_state_lock);
7070 error = zfsdev_state_init(filp);
7071 mutex_exit(&zfsdev_state_lock);
7077 zfsdev_release(struct inode *ino, struct file *filp)
7081 mutex_enter(&zfsdev_state_lock);
7082 error = zfsdev_state_destroy(filp);
7083 mutex_exit(&zfsdev_state_lock);
7089 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
7093 int error, rc, flag = 0;
7094 const zfs_ioc_vec_t *vec;
7095 char *saved_poolname = NULL;
7096 nvlist_t *innvl = NULL;
7097 fstrans_cookie_t cookie;
7099 vecnum = cmd - ZFS_IOC_FIRST;
7100 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7101 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7102 vec = &zfs_ioc_vec[vecnum];
7105 * The registered ioctl list may be sparse, verify that either
7106 * a normal or legacy handler are registered.
7108 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7109 return (-SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7111 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
7113 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
7115 error = SET_ERROR(EFAULT);
7119 zc->zc_iflags = flag & FKIOCTL;
7120 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
7122 * Make sure the user doesn't pass in an insane value for
7123 * zc_nvlist_src_size. We have to check, since we will end
7124 * up allocating that much memory inside of get_nvlist(). This
7125 * prevents a nefarious user from allocating tons of kernel
7128 * Also, we return EINVAL instead of ENOMEM here. The reason
7129 * being that returning ENOMEM from an ioctl() has a special
7130 * connotation; that the user's size value is too small and
7131 * needs to be expanded to hold the nvlist. See
7132 * zcmd_expand_dst_nvlist() for details.
7134 error = SET_ERROR(EINVAL); /* User's size too big */
7136 } else if (zc->zc_nvlist_src_size != 0) {
7137 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7138 zc->zc_iflags, &innvl);
7144 * Ensure that all pool/dataset names are valid before we pass down to
7147 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7148 switch (vec->zvec_namecheck) {
7150 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7151 error = SET_ERROR(EINVAL);
7153 error = pool_status_check(zc->zc_name,
7154 vec->zvec_namecheck, vec->zvec_pool_check);
7158 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7159 error = SET_ERROR(EINVAL);
7161 error = pool_status_check(zc->zc_name,
7162 vec->zvec_namecheck, vec->zvec_pool_check);
7170 * Ensure that all input pairs are valid before we pass them down
7171 * to the lower layers.
7173 * The vectored functions can use fnvlist_lookup_{type} for any
7174 * required pairs since zfs_check_input_nvpairs() confirmed that
7175 * they exist and are of the correct type.
7177 if (error == 0 && vec->zvec_func != NULL) {
7178 error = zfs_check_input_nvpairs(innvl, vec);
7184 cookie = spl_fstrans_mark();
7185 error = vec->zvec_secpolicy(zc, innvl, CRED());
7186 spl_fstrans_unmark(cookie);
7192 /* legacy ioctls can modify zc_name */
7193 saved_poolname = strdup(zc->zc_name);
7194 if (saved_poolname == NULL) {
7195 error = SET_ERROR(ENOMEM);
7198 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7201 if (vec->zvec_func != NULL) {
7205 nvlist_t *lognv = NULL;
7207 ASSERT(vec->zvec_legacy_func == NULL);
7210 * Add the innvl to the lognv before calling the func,
7211 * in case the func changes the innvl.
7213 if (vec->zvec_allow_log) {
7214 lognv = fnvlist_alloc();
7215 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7217 if (!nvlist_empty(innvl)) {
7218 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7223 outnvl = fnvlist_alloc();
7224 cookie = spl_fstrans_mark();
7225 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7226 spl_fstrans_unmark(cookie);
7229 * Some commands can partially execute, modify state, and still
7230 * return an error. In these cases, attempt to record what
7234 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7235 vec->zvec_allow_log &&
7236 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7237 if (!nvlist_empty(outnvl)) {
7238 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7242 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7245 (void) spa_history_log_nvl(spa, lognv);
7246 spa_close(spa, FTAG);
7248 fnvlist_free(lognv);
7250 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7252 if (vec->zvec_smush_outnvlist) {
7253 smusherror = nvlist_smush(outnvl,
7254 zc->zc_nvlist_dst_size);
7256 if (smusherror == 0)
7257 puterror = put_nvlist(zc, outnvl);
7263 nvlist_free(outnvl);
7265 cookie = spl_fstrans_mark();
7266 error = vec->zvec_legacy_func(zc);
7267 spl_fstrans_unmark(cookie);
7272 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
7273 if (error == 0 && rc != 0)
7274 error = SET_ERROR(EFAULT);
7275 if (error == 0 && vec->zvec_allow_log) {
7276 char *s = tsd_get(zfs_allow_log_key);
7279 (void) tsd_set(zfs_allow_log_key, saved_poolname);
7281 if (saved_poolname != NULL)
7282 strfree(saved_poolname);
7285 kmem_free(zc, sizeof (zfs_cmd_t));
7289 #ifdef CONFIG_COMPAT
7291 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
7293 return (zfsdev_ioctl(filp, cmd, arg));
7296 #define zfsdev_compat_ioctl NULL
7299 static const struct file_operations zfsdev_fops = {
7300 .open = zfsdev_open,
7301 .release = zfsdev_release,
7302 .unlocked_ioctl = zfsdev_ioctl,
7303 .compat_ioctl = zfsdev_compat_ioctl,
7304 .owner = THIS_MODULE,
7307 static struct miscdevice zfs_misc = {
7308 .minor = ZFS_DEVICE_MINOR,
7310 .fops = &zfsdev_fops,
7313 MODULE_ALIAS_MISCDEV(ZFS_DEVICE_MINOR);
7314 MODULE_ALIAS("devname:zfs");
7321 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7322 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7323 zfsdev_state_list->zs_minor = -1;
7325 error = misc_register(&zfs_misc);
7326 if (error == -EBUSY) {
7328 * Fallback to dynamic minor allocation in the event of a
7329 * collision with a reserved minor in linux/miscdevice.h.
7330 * In this case the kernel modules must be manually loaded.
7332 printk(KERN_INFO "ZFS: misc_register() with static minor %d "
7333 "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
7334 ZFS_DEVICE_MINOR, error);
7336 zfs_misc.minor = MISC_DYNAMIC_MINOR;
7337 error = misc_register(&zfs_misc);
7341 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
7349 zfsdev_state_t *zs, *zsprev = NULL;
7351 misc_deregister(&zfs_misc);
7352 mutex_destroy(&zfsdev_state_lock);
7354 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7356 kmem_free(zsprev, sizeof (zfsdev_state_t));
7360 kmem_free(zsprev, sizeof (zfsdev_state_t));
7364 zfs_allow_log_destroy(void *arg)
7366 char *poolname = arg;
7368 if (poolname != NULL)
7373 #define ZFS_DEBUG_STR " (DEBUG mode)"
7375 #define ZFS_DEBUG_STR ""
7383 if ((error = -zvol_init()) != 0)
7386 spa_init(FREAD | FWRITE);
7392 if ((error = zfs_attach()) != 0)
7395 tsd_create(&zfs_fsyncer_key, NULL);
7396 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7397 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7399 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
7400 "ZFS pool version %s, ZFS filesystem version %s\n",
7401 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
7402 SPA_VERSION_STRING, ZPL_VERSION_STRING);
7403 #ifndef CONFIG_FS_POSIX_ACL
7404 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
7405 #endif /* CONFIG_FS_POSIX_ACL */
7414 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
7415 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
7416 ZFS_DEBUG_STR, error);
7430 tsd_destroy(&zfs_fsyncer_key);
7431 tsd_destroy(&rrw_tsd_key);
7432 tsd_destroy(&zfs_allow_log_key);
7434 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
7435 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
7438 #if defined(_KERNEL)
7442 MODULE_DESCRIPTION("ZFS");
7443 MODULE_AUTHOR(ZFS_META_AUTHOR);
7444 MODULE_LICENSE(ZFS_META_LICENSE);
7445 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);