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, 2017 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) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright (c) 2017 Datto Inc. All rights reserved.
38 * Copyright 2017 RackTop Systems.
39 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
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 * The ioctl numbers can change from release to release, because
67 * the caller (libzfs) must be matched to the kernel.
69 * zfs_secpolicy_func_t *secpolicy
70 * This function will be called before the zfs_ioc_func_t, to
71 * determine if this operation is permitted. It should return EPERM
72 * on failure, and 0 on success. Checks include determining if the
73 * dataset is visible in this zone, and if the user has either all
74 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
75 * to do this operation on this dataset with "zfs allow".
77 * zfs_ioc_namecheck_t namecheck
78 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
79 * name, a dataset name, or nothing. If the name is not well-formed,
80 * the ioctl will fail and the callback will not be called.
81 * Therefore, the callback can assume that the name is well-formed
82 * (e.g. is null-terminated, doesn't have more than one '@' character,
83 * doesn't have invalid characters).
85 * zfs_ioc_poolcheck_t pool_check
86 * This specifies requirements on the pool state. If the pool does
87 * not meet them (is suspended or is readonly), the ioctl will fail
88 * and the callback will not be called. If any checks are specified
89 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
90 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
91 * POOL_CHECK_READONLY).
93 * boolean_t smush_outnvlist
94 * If smush_outnvlist is true, then the output is presumed to be a
95 * list of errors, and it will be "smushed" down to fit into the
96 * caller's buffer, by removing some entries and replacing them with a
97 * single "N_MORE_ERRORS" entry indicating how many were removed. See
98 * nvlist_smush() for details. If smush_outnvlist is false, and the
99 * outnvlist does not fit into the userland-provided buffer, then the
100 * ioctl will fail with ENOMEM.
102 * zfs_ioc_func_t *func
103 * The callback function that will perform the operation.
105 * The callback should return 0 on success, or an error number on
106 * failure. If the function fails, the userland ioctl will return -1,
107 * and errno will be set to the callback's return value. The callback
108 * will be called with the following arguments:
111 * The name of the pool or dataset to operate on, from
112 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
113 * expected type (pool, dataset, or none).
116 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
117 * NULL if no input nvlist was provided. Changes to this nvlist are
118 * ignored. If the input nvlist could not be deserialized, the
119 * ioctl will fail and the callback will not be called.
122 * The output nvlist, initially empty. The callback can fill it in,
123 * and it will be returned to userland by serializing it into
124 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
125 * fails (e.g. because the caller didn't supply a large enough
126 * buffer), then the overall ioctl will fail. See the
127 * 'smush_nvlist' argument above for additional behaviors.
129 * There are two typical uses of the output nvlist:
130 * - To return state, e.g. property values. In this case,
131 * smush_outnvlist should be false. If the buffer was not large
132 * enough, the caller will reallocate a larger buffer and try
135 * - To return multiple errors from an ioctl which makes on-disk
136 * changes. In this case, smush_outnvlist should be true.
137 * Ioctls which make on-disk modifications should generally not
138 * use the outnvl if they succeed, because the caller can not
139 * distinguish between the operation failing, and
140 * deserialization failing.
143 #include <sys/types.h>
144 #include <sys/param.h>
145 #include <sys/errno.h>
147 #include <sys/file.h>
148 #include <sys/kmem.h>
149 #include <sys/cmn_err.h>
150 #include <sys/stat.h>
151 #include <sys/zfs_ioctl.h>
152 #include <sys/zfs_vfsops.h>
153 #include <sys/zfs_znode.h>
156 #include <sys/spa_impl.h>
157 #include <sys/vdev.h>
158 #include <sys/vdev_impl.h>
160 #include <sys/dsl_dir.h>
161 #include <sys/dsl_dataset.h>
162 #include <sys/dsl_prop.h>
163 #include <sys/dsl_deleg.h>
164 #include <sys/dmu_objset.h>
165 #include <sys/dmu_impl.h>
166 #include <sys/dmu_tx.h>
167 #include <sys/sunddi.h>
168 #include <sys/policy.h>
169 #include <sys/zone.h>
170 #include <sys/nvpair.h>
171 #include <sys/pathname.h>
173 #include <sys/fs/zfs.h>
174 #include <sys/zfs_ctldir.h>
175 #include <sys/zfs_dir.h>
176 #include <sys/zfs_onexit.h>
177 #include <sys/zvol.h>
178 #include <sys/dsl_scan.h>
179 #include <sys/fm/util.h>
180 #include <sys/dsl_crypt.h>
182 #include <sys/dmu_send.h>
183 #include <sys/dsl_destroy.h>
184 #include <sys/dsl_bookmark.h>
185 #include <sys/dsl_userhold.h>
186 #include <sys/zfeature.h>
188 #include <sys/zio_checksum.h>
189 #include <sys/vdev_removal.h>
190 #include <sys/zfs_sysfs.h>
192 #include <linux/miscdevice.h>
193 #include <linux/slab.h>
195 #include "zfs_namecheck.h"
196 #include "zfs_prop.h"
197 #include "zfs_deleg.h"
198 #include "zfs_comutil.h"
200 #include <sys/lua/lua.h>
201 #include <sys/lua/lauxlib.h>
204 * Limit maximum nvlist size. We don't want users passing in insane values
205 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
207 #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE
209 kmutex_t zfsdev_state_lock;
210 zfsdev_state_t *zfsdev_state_list;
212 extern void zfs_init(void);
213 extern void zfs_fini(void);
215 uint_t zfs_fsyncer_key;
216 extern uint_t rrw_tsd_key;
217 static uint_t zfs_allow_log_key;
219 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
220 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
221 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
227 } zfs_ioc_namecheck_t;
230 POOL_CHECK_NONE = 1 << 0,
231 POOL_CHECK_SUSPENDED = 1 << 1,
232 POOL_CHECK_READONLY = 1 << 2,
233 } zfs_ioc_poolcheck_t;
235 typedef struct zfs_ioc_vec {
236 zfs_ioc_legacy_func_t *zvec_legacy_func;
237 zfs_ioc_func_t *zvec_func;
238 zfs_secpolicy_func_t *zvec_secpolicy;
239 zfs_ioc_namecheck_t zvec_namecheck;
240 boolean_t zvec_allow_log;
241 zfs_ioc_poolcheck_t zvec_pool_check;
242 boolean_t zvec_smush_outnvlist;
243 const char *zvec_name;
246 /* This array is indexed by zfs_userquota_prop_t */
247 static const char *userquota_perms[] = {
248 ZFS_DELEG_PERM_USERUSED,
249 ZFS_DELEG_PERM_USERQUOTA,
250 ZFS_DELEG_PERM_GROUPUSED,
251 ZFS_DELEG_PERM_GROUPQUOTA,
252 ZFS_DELEG_PERM_USEROBJUSED,
253 ZFS_DELEG_PERM_USEROBJQUOTA,
254 ZFS_DELEG_PERM_GROUPOBJUSED,
255 ZFS_DELEG_PERM_GROUPOBJQUOTA,
256 ZFS_DELEG_PERM_PROJECTUSED,
257 ZFS_DELEG_PERM_PROJECTQUOTA,
258 ZFS_DELEG_PERM_PROJECTOBJUSED,
259 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
262 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
263 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
264 static int zfs_check_settable(const char *name, nvpair_t *property,
266 static int zfs_check_clearable(char *dataset, nvlist_t *props,
268 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
270 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
271 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
274 history_str_free(char *buf)
276 kmem_free(buf, HIS_MAX_RECORD_LEN);
280 history_str_get(zfs_cmd_t *zc)
284 if (zc->zc_history == 0)
287 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
288 if (copyinstr((void *)(uintptr_t)zc->zc_history,
289 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
290 history_str_free(buf);
294 buf[HIS_MAX_RECORD_LEN -1] = '\0';
300 * Check to see if the named dataset is currently defined as bootable
303 zfs_is_bootfs(const char *name)
307 if (dmu_objset_hold(name, FTAG, &os) == 0) {
309 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
310 dmu_objset_rele(os, FTAG);
317 * Return non-zero if the spa version is less than requested version.
320 zfs_earlier_version(const char *name, int version)
324 if (spa_open(name, &spa, FTAG) == 0) {
325 if (spa_version(spa) < version) {
326 spa_close(spa, FTAG);
329 spa_close(spa, FTAG);
335 * Return TRUE if the ZPL version is less than requested version.
338 zpl_earlier_version(const char *name, int version)
341 boolean_t rc = B_TRUE;
343 if (dmu_objset_hold(name, FTAG, &os) == 0) {
346 if (dmu_objset_type(os) != DMU_OST_ZFS) {
347 dmu_objset_rele(os, FTAG);
350 /* XXX reading from non-owned objset */
351 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
352 rc = zplversion < version;
353 dmu_objset_rele(os, FTAG);
359 zfs_log_history(zfs_cmd_t *zc)
364 if ((buf = history_str_get(zc)) == NULL)
367 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
368 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
369 (void) spa_history_log(spa, buf);
370 spa_close(spa, FTAG);
372 history_str_free(buf);
376 * Policy for top-level read operations (list pools). Requires no privileges,
377 * and can be used in the local zone, as there is no associated dataset.
381 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
387 * Policy for dataset read operations (list children, get statistics). Requires
388 * no privileges, but must be visible in the local zone.
392 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
394 if (INGLOBALZONE(curproc) ||
395 zone_dataset_visible(zc->zc_name, NULL))
398 return (SET_ERROR(ENOENT));
402 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
407 * The dataset must be visible by this zone -- check this first
408 * so they don't see EPERM on something they shouldn't know about.
410 if (!INGLOBALZONE(curproc) &&
411 !zone_dataset_visible(dataset, &writable))
412 return (SET_ERROR(ENOENT));
414 if (INGLOBALZONE(curproc)) {
416 * If the fs is zoned, only root can access it from the
419 if (secpolicy_zfs(cr) && zoned)
420 return (SET_ERROR(EPERM));
423 * If we are in a local zone, the 'zoned' property must be set.
426 return (SET_ERROR(EPERM));
428 /* must be writable by this zone */
430 return (SET_ERROR(EPERM));
436 zfs_dozonecheck(const char *dataset, cred_t *cr)
440 if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL))
441 return (SET_ERROR(ENOENT));
443 return (zfs_dozonecheck_impl(dataset, zoned, cr));
447 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
451 if (dsl_prop_get_int_ds(ds, "zoned", &zoned))
452 return (SET_ERROR(ENOENT));
454 return (zfs_dozonecheck_impl(dataset, zoned, cr));
458 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
459 const char *perm, cred_t *cr)
463 error = zfs_dozonecheck_ds(name, ds, cr);
465 error = secpolicy_zfs(cr);
467 error = dsl_deleg_access_impl(ds, perm, cr);
473 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
480 * First do a quick check for root in the global zone, which
481 * is allowed to do all write_perms. This ensures that zfs_ioc_*
482 * will get to handle nonexistent datasets.
484 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
487 error = dsl_pool_hold(name, FTAG, &dp);
491 error = dsl_dataset_hold(dp, name, FTAG, &ds);
493 dsl_pool_rele(dp, FTAG);
497 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
499 dsl_dataset_rele(ds, FTAG);
500 dsl_pool_rele(dp, FTAG);
505 * Policy for setting the security label property.
507 * Returns 0 for success, non-zero for access and other errors.
510 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
513 char ds_hexsl[MAXNAMELEN];
514 bslabel_t ds_sl, new_sl;
515 boolean_t new_default = FALSE;
517 int needed_priv = -1;
520 /* First get the existing dataset label. */
521 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
522 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
524 return (SET_ERROR(EPERM));
526 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
529 /* The label must be translatable */
530 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
531 return (SET_ERROR(EINVAL));
534 * In a non-global zone, disallow attempts to set a label that
535 * doesn't match that of the zone; otherwise no other checks
538 if (!INGLOBALZONE(curproc)) {
539 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
540 return (SET_ERROR(EPERM));
545 * For global-zone datasets (i.e., those whose zoned property is
546 * "off", verify that the specified new label is valid for the
549 if (dsl_prop_get_integer(name,
550 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
551 return (SET_ERROR(EPERM));
553 if (zfs_check_global_label(name, strval) != 0)
554 return (SET_ERROR(EPERM));
558 * If the existing dataset label is nondefault, check if the
559 * dataset is mounted (label cannot be changed while mounted).
560 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
561 * mounted (or isn't a dataset, doesn't exist, ...).
563 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
565 static char *setsl_tag = "setsl_tag";
568 * Try to own the dataset; abort if there is any error,
569 * (e.g., already mounted, in use, or other error).
571 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
574 return (SET_ERROR(EPERM));
576 dmu_objset_disown(os, B_TRUE, setsl_tag);
579 needed_priv = PRIV_FILE_DOWNGRADE_SL;
583 if (hexstr_to_label(strval, &new_sl) != 0)
584 return (SET_ERROR(EPERM));
586 if (blstrictdom(&ds_sl, &new_sl))
587 needed_priv = PRIV_FILE_DOWNGRADE_SL;
588 else if (blstrictdom(&new_sl, &ds_sl))
589 needed_priv = PRIV_FILE_UPGRADE_SL;
591 /* dataset currently has a default label */
593 needed_priv = PRIV_FILE_UPGRADE_SL;
597 if (needed_priv != -1)
598 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
601 return (SET_ERROR(ENOTSUP));
602 #endif /* HAVE_MLSLABEL */
606 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
612 * Check permissions for special properties.
619 * Disallow setting of 'zoned' from within a local zone.
621 if (!INGLOBALZONE(curproc))
622 return (SET_ERROR(EPERM));
626 case ZFS_PROP_FILESYSTEM_LIMIT:
627 case ZFS_PROP_SNAPSHOT_LIMIT:
628 if (!INGLOBALZONE(curproc)) {
630 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
632 * Unprivileged users are allowed to modify the
633 * limit on things *under* (ie. contained by)
634 * the thing they own.
636 if (dsl_prop_get_integer(dsname, "zoned", &zoned,
638 return (SET_ERROR(EPERM));
639 if (!zoned || strlen(dsname) <= strlen(setpoint))
640 return (SET_ERROR(EPERM));
644 case ZFS_PROP_MLSLABEL:
645 if (!is_system_labeled())
646 return (SET_ERROR(EPERM));
648 if (nvpair_value_string(propval, &strval) == 0) {
651 err = zfs_set_slabel_policy(dsname, strval, CRED());
658 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
663 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
667 error = zfs_dozonecheck(zc->zc_name, cr);
672 * permission to set permissions will be evaluated later in
673 * dsl_deleg_can_allow()
680 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
682 return (zfs_secpolicy_write_perms(zc->zc_name,
683 ZFS_DELEG_PERM_ROLLBACK, cr));
688 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
696 * Generate the current snapshot name from the given objsetid, then
697 * use that name for the secpolicy/zone checks.
699 cp = strchr(zc->zc_name, '@');
701 return (SET_ERROR(EINVAL));
702 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
706 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
708 dsl_pool_rele(dp, FTAG);
712 dsl_dataset_name(ds, zc->zc_name);
714 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
715 ZFS_DELEG_PERM_SEND, cr);
716 dsl_dataset_rele(ds, FTAG);
717 dsl_pool_rele(dp, FTAG);
724 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
726 return (zfs_secpolicy_write_perms(zc->zc_name,
727 ZFS_DELEG_PERM_SEND, cr));
730 #ifdef HAVE_SMB_SHARE
733 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
738 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
739 NO_FOLLOW, NULL, &vp)) != 0)
742 /* Now make sure mntpnt and dataset are ZFS */
744 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
745 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
746 zc->zc_name) != 0)) {
748 return (SET_ERROR(EPERM));
752 return (dsl_deleg_access(zc->zc_name,
753 ZFS_DELEG_PERM_SHARE, cr));
755 #endif /* HAVE_SMB_SHARE */
758 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
760 #ifdef HAVE_SMB_SHARE
761 if (!INGLOBALZONE(curproc))
762 return (SET_ERROR(EPERM));
764 if (secpolicy_nfs(cr) == 0) {
767 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
770 return (SET_ERROR(ENOTSUP));
771 #endif /* HAVE_SMB_SHARE */
775 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
777 #ifdef HAVE_SMB_SHARE
778 if (!INGLOBALZONE(curproc))
779 return (SET_ERROR(EPERM));
781 if (secpolicy_smb(cr) == 0) {
784 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
787 return (SET_ERROR(ENOTSUP));
788 #endif /* HAVE_SMB_SHARE */
792 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
797 * Remove the @bla or /bla from the end of the name to get the parent.
799 (void) strncpy(parent, datasetname, parentsize);
800 cp = strrchr(parent, '@');
804 cp = strrchr(parent, '/');
806 return (SET_ERROR(ENOENT));
814 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
818 if ((error = zfs_secpolicy_write_perms(name,
819 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
822 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
827 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
829 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
833 * Destroying snapshots with delegated permissions requires
834 * descendant mount and destroy permissions.
838 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
841 nvpair_t *pair, *nextpair;
844 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
845 return (SET_ERROR(EINVAL));
846 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
848 nextpair = nvlist_next_nvpair(snaps, pair);
849 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
850 if (error == ENOENT) {
852 * Ignore any snapshots that don't exist (we consider
853 * them "already destroyed"). Remove the name from the
854 * nvl here in case the snapshot is created between
855 * now and when we try to destroy it (in which case
856 * we don't want to destroy it since we haven't
857 * checked for permission).
859 fnvlist_remove_nvpair(snaps, pair);
870 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
872 char parentname[ZFS_MAX_DATASET_NAME_LEN];
875 if ((error = zfs_secpolicy_write_perms(from,
876 ZFS_DELEG_PERM_RENAME, cr)) != 0)
879 if ((error = zfs_secpolicy_write_perms(from,
880 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
883 if ((error = zfs_get_parent(to, parentname,
884 sizeof (parentname))) != 0)
887 if ((error = zfs_secpolicy_write_perms(parentname,
888 ZFS_DELEG_PERM_CREATE, cr)) != 0)
891 if ((error = zfs_secpolicy_write_perms(parentname,
892 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
900 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
902 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
907 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
910 dsl_dataset_t *clone;
913 error = zfs_secpolicy_write_perms(zc->zc_name,
914 ZFS_DELEG_PERM_PROMOTE, cr);
918 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
922 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
925 char parentname[ZFS_MAX_DATASET_NAME_LEN];
926 dsl_dataset_t *origin = NULL;
930 error = dsl_dataset_hold_obj(dd->dd_pool,
931 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
933 dsl_dataset_rele(clone, FTAG);
934 dsl_pool_rele(dp, FTAG);
938 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
939 ZFS_DELEG_PERM_MOUNT, cr);
941 dsl_dataset_name(origin, parentname);
943 error = zfs_secpolicy_write_perms_ds(parentname, origin,
944 ZFS_DELEG_PERM_PROMOTE, cr);
946 dsl_dataset_rele(clone, FTAG);
947 dsl_dataset_rele(origin, FTAG);
949 dsl_pool_rele(dp, FTAG);
955 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
959 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
960 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
963 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
964 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
967 return (zfs_secpolicy_write_perms(zc->zc_name,
968 ZFS_DELEG_PERM_CREATE, cr));
973 zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
975 return (zfs_secpolicy_recv(zc, innvl, cr));
979 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
981 return (zfs_secpolicy_write_perms(name,
982 ZFS_DELEG_PERM_SNAPSHOT, cr));
986 * Check for permission to create each snapshot in the nvlist.
990 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
996 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
997 return (SET_ERROR(EINVAL));
998 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
999 pair = nvlist_next_nvpair(snaps, pair)) {
1000 char *name = nvpair_name(pair);
1001 char *atp = strchr(name, '@');
1004 error = SET_ERROR(EINVAL);
1008 error = zfs_secpolicy_snapshot_perms(name, cr);
1017 * Check for permission to create each snapshot in the nvlist.
1021 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1025 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1026 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1027 char *name = nvpair_name(pair);
1028 char *hashp = strchr(name, '#');
1030 if (hashp == NULL) {
1031 error = SET_ERROR(EINVAL);
1035 error = zfs_secpolicy_write_perms(name,
1036 ZFS_DELEG_PERM_BOOKMARK, cr);
1046 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1048 return (zfs_secpolicy_write_perms(zc->zc_name,
1049 ZFS_DELEG_PERM_REMAP, cr));
1054 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1056 nvpair_t *pair, *nextpair;
1059 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1061 char *name = nvpair_name(pair);
1062 char *hashp = strchr(name, '#');
1063 nextpair = nvlist_next_nvpair(innvl, pair);
1065 if (hashp == NULL) {
1066 error = SET_ERROR(EINVAL);
1071 error = zfs_secpolicy_write_perms(name,
1072 ZFS_DELEG_PERM_DESTROY, cr);
1074 if (error == ENOENT) {
1076 * Ignore any filesystems that don't exist (we consider
1077 * their bookmarks "already destroyed"). Remove
1078 * the name from the nvl here in case the filesystem
1079 * is created between now and when we try to destroy
1080 * the bookmark (in which case we don't want to
1081 * destroy it since we haven't checked for permission).
1083 fnvlist_remove_nvpair(innvl, pair);
1095 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1098 * Even root must have a proper TSD so that we know what pool
1101 if (tsd_get(zfs_allow_log_key) == NULL)
1102 return (SET_ERROR(EPERM));
1107 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1109 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1113 if ((error = zfs_get_parent(zc->zc_name, parentname,
1114 sizeof (parentname))) != 0)
1117 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1118 (error = zfs_secpolicy_write_perms(origin,
1119 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1122 if ((error = zfs_secpolicy_write_perms(parentname,
1123 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1126 return (zfs_secpolicy_write_perms(parentname,
1127 ZFS_DELEG_PERM_MOUNT, cr));
1131 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1132 * SYS_CONFIG privilege, which is not available in a local zone.
1136 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1138 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1139 return (SET_ERROR(EPERM));
1145 * Policy for object to name lookups.
1149 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1153 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1156 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1161 * Policy for fault injection. Requires all privileges.
1165 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1167 return (secpolicy_zinject(cr));
1172 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1174 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1176 if (prop == ZPROP_INVAL) {
1177 if (!zfs_prop_user(zc->zc_value))
1178 return (SET_ERROR(EINVAL));
1179 return (zfs_secpolicy_write_perms(zc->zc_name,
1180 ZFS_DELEG_PERM_USERPROP, cr));
1182 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1188 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1190 int err = zfs_secpolicy_read(zc, innvl, cr);
1194 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1195 return (SET_ERROR(EINVAL));
1197 if (zc->zc_value[0] == 0) {
1199 * They are asking about a posix uid/gid. If it's
1200 * themself, allow it.
1202 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1203 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1204 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1205 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1206 if (zc->zc_guid == crgetuid(cr))
1208 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1209 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1210 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1211 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1212 if (groupmember(zc->zc_guid, cr))
1215 /* else is for project quota/used */
1218 return (zfs_secpolicy_write_perms(zc->zc_name,
1219 userquota_perms[zc->zc_objset_type], cr));
1223 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1225 int err = zfs_secpolicy_read(zc, innvl, cr);
1229 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1230 return (SET_ERROR(EINVAL));
1232 return (zfs_secpolicy_write_perms(zc->zc_name,
1233 userquota_perms[zc->zc_objset_type], cr));
1238 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1240 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1246 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1252 error = nvlist_lookup_nvlist(innvl, "holds", &holds);
1254 return (SET_ERROR(EINVAL));
1256 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1257 pair = nvlist_next_nvpair(holds, pair)) {
1258 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1259 error = dmu_fsname(nvpair_name(pair), fsname);
1262 error = zfs_secpolicy_write_perms(fsname,
1263 ZFS_DELEG_PERM_HOLD, cr);
1272 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1277 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1278 pair = nvlist_next_nvpair(innvl, pair)) {
1279 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1280 error = dmu_fsname(nvpair_name(pair), fsname);
1283 error = zfs_secpolicy_write_perms(fsname,
1284 ZFS_DELEG_PERM_RELEASE, cr);
1292 * Policy for allowing temporary snapshots to be taken or released
1295 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1298 * A temporary snapshot is the same as a snapshot,
1299 * hold, destroy and release all rolled into one.
1300 * Delegated diff alone is sufficient that we allow this.
1304 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1305 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1308 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1310 error = zfs_secpolicy_hold(zc, innvl, cr);
1312 error = zfs_secpolicy_release(zc, innvl, cr);
1314 error = zfs_secpolicy_destroy(zc, innvl, cr);
1319 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1321 return (zfs_secpolicy_write_perms(zc->zc_name,
1322 ZFS_DELEG_PERM_LOAD_KEY, cr));
1326 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1328 return (zfs_secpolicy_write_perms(zc->zc_name,
1329 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1333 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1336 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1340 nvlist_t *list = NULL;
1343 * Read in and unpack the user-supplied nvlist.
1346 return (SET_ERROR(EINVAL));
1348 packed = vmem_alloc(size, KM_SLEEP);
1350 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1352 vmem_free(packed, size);
1353 return (SET_ERROR(EFAULT));
1356 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1357 vmem_free(packed, size);
1361 vmem_free(packed, size);
1368 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1369 * Entries will be removed from the end of the nvlist, and one int32 entry
1370 * named "N_MORE_ERRORS" will be added indicating how many entries were
1374 nvlist_smush(nvlist_t *errors, size_t max)
1378 size = fnvlist_size(errors);
1381 nvpair_t *more_errors;
1385 return (SET_ERROR(ENOMEM));
1387 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1388 more_errors = nvlist_prev_nvpair(errors, NULL);
1391 nvpair_t *pair = nvlist_prev_nvpair(errors,
1393 fnvlist_remove_nvpair(errors, pair);
1395 size = fnvlist_size(errors);
1396 } while (size > max);
1398 fnvlist_remove_nvpair(errors, more_errors);
1399 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1400 ASSERT3U(fnvlist_size(errors), <=, max);
1407 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1409 char *packed = NULL;
1413 size = fnvlist_size(nvl);
1415 if (size > zc->zc_nvlist_dst_size) {
1416 error = SET_ERROR(ENOMEM);
1418 packed = fnvlist_pack(nvl, &size);
1419 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1420 size, zc->zc_iflags) != 0)
1421 error = SET_ERROR(EFAULT);
1422 fnvlist_pack_free(packed, size);
1425 zc->zc_nvlist_dst_size = size;
1426 zc->zc_nvlist_dst_filled = B_TRUE;
1431 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1434 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1435 return (SET_ERROR(EINVAL));
1438 mutex_enter(&os->os_user_ptr_lock);
1439 *zfvp = dmu_objset_get_user(os);
1440 /* bump s_active only when non-zero to prevent umount race */
1441 if (*zfvp == NULL || (*zfvp)->z_sb == NULL ||
1442 !atomic_inc_not_zero(&((*zfvp)->z_sb->s_active))) {
1443 error = SET_ERROR(ESRCH);
1445 mutex_exit(&os->os_user_ptr_lock);
1450 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1455 error = dmu_objset_hold(dsname, FTAG, &os);
1459 error = getzfsvfs_impl(os, zfvp);
1460 dmu_objset_rele(os, FTAG);
1465 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1466 * case its z_sb will be NULL, and it will be opened as the owner.
1467 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1468 * which prevents all inode ops from running.
1471 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1475 if (getzfsvfs(name, zfvp) != 0)
1476 error = zfsvfs_create(name, B_FALSE, zfvp);
1478 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1480 if ((*zfvp)->z_unmounted) {
1482 * XXX we could probably try again, since the unmounting
1483 * thread should be just about to disassociate the
1484 * objset from the zfsvfs.
1486 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1487 return (SET_ERROR(EBUSY));
1494 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1496 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1499 deactivate_super(zfsvfs->z_sb);
1501 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1502 zfsvfs_free(zfsvfs);
1507 zfs_ioc_pool_create(zfs_cmd_t *zc)
1510 nvlist_t *config, *props = NULL;
1511 nvlist_t *rootprops = NULL;
1512 nvlist_t *zplprops = NULL;
1513 dsl_crypto_params_t *dcp = NULL;
1514 char *spa_name = zc->zc_name;
1516 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1517 zc->zc_iflags, &config)))
1520 if (zc->zc_nvlist_src_size != 0 && (error =
1521 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1522 zc->zc_iflags, &props))) {
1523 nvlist_free(config);
1528 nvlist_t *nvl = NULL;
1529 nvlist_t *hidden_args = NULL;
1530 uint64_t version = SPA_VERSION;
1533 (void) nvlist_lookup_uint64(props,
1534 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1535 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1536 error = SET_ERROR(EINVAL);
1537 goto pool_props_bad;
1539 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1541 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1543 nvlist_free(config);
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 nvlist_free(config);
1559 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1561 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1562 error = zfs_fill_zplprops_root(version, rootprops,
1565 goto pool_props_bad;
1567 if (nvlist_lookup_string(props,
1568 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1572 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1575 * Set the remaining root properties
1577 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1578 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1579 (void) spa_destroy(spa_name);
1582 nvlist_free(rootprops);
1583 nvlist_free(zplprops);
1584 nvlist_free(config);
1586 dsl_crypto_params_free(dcp, !!error);
1592 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1595 zfs_log_history(zc);
1596 error = spa_destroy(zc->zc_name);
1602 zfs_ioc_pool_import(zfs_cmd_t *zc)
1604 nvlist_t *config, *props = NULL;
1608 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1609 zc->zc_iflags, &config)) != 0)
1612 if (zc->zc_nvlist_src_size != 0 && (error =
1613 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1614 zc->zc_iflags, &props))) {
1615 nvlist_free(config);
1619 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1620 guid != zc->zc_guid)
1621 error = SET_ERROR(EINVAL);
1623 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1625 if (zc->zc_nvlist_dst != 0) {
1628 if ((err = put_nvlist(zc, config)) != 0)
1632 nvlist_free(config);
1639 zfs_ioc_pool_export(zfs_cmd_t *zc)
1642 boolean_t force = (boolean_t)zc->zc_cookie;
1643 boolean_t hardforce = (boolean_t)zc->zc_guid;
1645 zfs_log_history(zc);
1646 error = spa_export(zc->zc_name, NULL, force, hardforce);
1652 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1657 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1658 return (SET_ERROR(EEXIST));
1660 error = put_nvlist(zc, configs);
1662 nvlist_free(configs);
1669 * zc_name name of the pool
1672 * zc_cookie real errno
1673 * zc_nvlist_dst config nvlist
1674 * zc_nvlist_dst_size size of config nvlist
1677 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1683 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1684 sizeof (zc->zc_value));
1686 if (config != NULL) {
1687 ret = put_nvlist(zc, config);
1688 nvlist_free(config);
1691 * The config may be present even if 'error' is non-zero.
1692 * In this case we return success, and preserve the real errno
1695 zc->zc_cookie = error;
1704 * Try to import the given pool, returning pool stats as appropriate so that
1705 * user land knows which devices are available and overall pool health.
1708 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1710 nvlist_t *tryconfig, *config = NULL;
1713 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1714 zc->zc_iflags, &tryconfig)) != 0)
1717 config = spa_tryimport(tryconfig);
1719 nvlist_free(tryconfig);
1722 return (SET_ERROR(EINVAL));
1724 error = put_nvlist(zc, config);
1725 nvlist_free(config);
1732 * zc_name name of the pool
1733 * zc_cookie scan func (pool_scan_func_t)
1734 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1737 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1742 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1743 return (SET_ERROR(EINVAL));
1745 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1748 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1749 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1750 else if (zc->zc_cookie == POOL_SCAN_NONE)
1751 error = spa_scan_stop(spa);
1753 error = spa_scan(spa, zc->zc_cookie);
1755 spa_close(spa, FTAG);
1761 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1766 error = spa_open(zc->zc_name, &spa, FTAG);
1769 spa_close(spa, FTAG);
1775 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1780 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1783 if (zc->zc_cookie < spa_version(spa) ||
1784 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1785 spa_close(spa, FTAG);
1786 return (SET_ERROR(EINVAL));
1789 spa_upgrade(spa, zc->zc_cookie);
1790 spa_close(spa, FTAG);
1796 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1803 if ((size = zc->zc_history_len) == 0)
1804 return (SET_ERROR(EINVAL));
1806 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1809 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1810 spa_close(spa, FTAG);
1811 return (SET_ERROR(ENOTSUP));
1814 hist_buf = vmem_alloc(size, KM_SLEEP);
1815 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1816 &zc->zc_history_len, hist_buf)) == 0) {
1817 error = ddi_copyout(hist_buf,
1818 (void *)(uintptr_t)zc->zc_history,
1819 zc->zc_history_len, zc->zc_iflags);
1822 spa_close(spa, FTAG);
1823 vmem_free(hist_buf, size);
1828 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1833 error = spa_open(zc->zc_name, &spa, FTAG);
1835 error = spa_change_guid(spa);
1836 spa_close(spa, FTAG);
1842 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1844 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1849 * zc_name name of filesystem
1850 * zc_obj object to find
1853 * zc_value name of object
1856 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1861 /* XXX reading from objset not owned */
1862 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1865 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1866 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1867 return (SET_ERROR(EINVAL));
1869 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1870 sizeof (zc->zc_value));
1871 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1878 * zc_name name of filesystem
1879 * zc_obj object to find
1882 * zc_stat stats on object
1883 * zc_value path to object
1886 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1891 /* XXX reading from objset not owned */
1892 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1895 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1896 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1897 return (SET_ERROR(EINVAL));
1899 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1900 sizeof (zc->zc_value));
1901 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1907 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1913 error = spa_open(zc->zc_name, &spa, FTAG);
1917 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1918 zc->zc_iflags, &config);
1920 error = spa_vdev_add(spa, config);
1921 nvlist_free(config);
1923 spa_close(spa, FTAG);
1929 * zc_name name of the pool
1930 * zc_guid guid of vdev to remove
1931 * zc_cookie cancel removal
1934 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1939 error = spa_open(zc->zc_name, &spa, FTAG);
1942 if (zc->zc_cookie != 0) {
1943 error = spa_vdev_remove_cancel(spa);
1945 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1947 spa_close(spa, FTAG);
1952 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1956 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1958 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1960 switch (zc->zc_cookie) {
1961 case VDEV_STATE_ONLINE:
1962 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1965 case VDEV_STATE_OFFLINE:
1966 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1969 case VDEV_STATE_FAULTED:
1970 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1971 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1972 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1973 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1975 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1978 case VDEV_STATE_DEGRADED:
1979 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1980 zc->zc_obj != VDEV_AUX_EXTERNAL)
1981 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1983 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1987 error = SET_ERROR(EINVAL);
1989 zc->zc_cookie = newstate;
1990 spa_close(spa, FTAG);
1995 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1998 int replacing = zc->zc_cookie;
2002 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2005 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2006 zc->zc_iflags, &config)) == 0) {
2007 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2008 nvlist_free(config);
2011 spa_close(spa, FTAG);
2016 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2021 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2024 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2026 spa_close(spa, FTAG);
2031 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2034 nvlist_t *config, *props = NULL;
2036 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2038 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2041 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2042 zc->zc_iflags, &config))) {
2043 spa_close(spa, FTAG);
2047 if (zc->zc_nvlist_src_size != 0 && (error =
2048 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2049 zc->zc_iflags, &props))) {
2050 spa_close(spa, FTAG);
2051 nvlist_free(config);
2055 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2057 spa_close(spa, FTAG);
2059 nvlist_free(config);
2066 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2069 char *path = zc->zc_value;
2070 uint64_t guid = zc->zc_guid;
2073 error = spa_open(zc->zc_name, &spa, FTAG);
2077 error = spa_vdev_setpath(spa, guid, path);
2078 spa_close(spa, FTAG);
2083 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2086 char *fru = zc->zc_value;
2087 uint64_t guid = zc->zc_guid;
2090 error = spa_open(zc->zc_name, &spa, FTAG);
2094 error = spa_vdev_setfru(spa, guid, fru);
2095 spa_close(spa, FTAG);
2100 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2105 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2107 if (zc->zc_nvlist_dst != 0 &&
2108 (error = dsl_prop_get_all(os, &nv)) == 0) {
2109 dmu_objset_stats(os, nv);
2111 * NB: zvol_get_stats() will read the objset contents,
2112 * which we aren't supposed to do with a
2113 * DS_MODE_USER hold, because it could be
2114 * inconsistent. So this is a bit of a workaround...
2115 * XXX reading with out owning
2117 if (!zc->zc_objset_stats.dds_inconsistent &&
2118 dmu_objset_type(os) == DMU_OST_ZVOL) {
2119 error = zvol_get_stats(os, nv);
2127 error = put_nvlist(zc, nv);
2136 * zc_name name of filesystem
2137 * zc_nvlist_dst_size size of buffer for property nvlist
2140 * zc_objset_stats stats
2141 * zc_nvlist_dst property nvlist
2142 * zc_nvlist_dst_size size of property nvlist
2145 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2150 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2152 error = zfs_ioc_objset_stats_impl(zc, os);
2153 dmu_objset_rele(os, FTAG);
2161 * zc_name name of filesystem
2162 * zc_nvlist_dst_size size of buffer for property nvlist
2165 * zc_nvlist_dst received property nvlist
2166 * zc_nvlist_dst_size size of received property nvlist
2168 * Gets received properties (distinct from local properties on or after
2169 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2170 * local property values.
2173 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2179 * Without this check, we would return local property values if the
2180 * caller has not already received properties on or after
2181 * SPA_VERSION_RECVD_PROPS.
2183 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2184 return (SET_ERROR(ENOTSUP));
2186 if (zc->zc_nvlist_dst != 0 &&
2187 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2188 error = put_nvlist(zc, nv);
2196 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2202 * zfs_get_zplprop() will either find a value or give us
2203 * the default value (if there is one).
2205 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2207 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2213 * zc_name name of filesystem
2214 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2217 * zc_nvlist_dst zpl property nvlist
2218 * zc_nvlist_dst_size size of zpl property nvlist
2221 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2226 /* XXX reading without owning */
2227 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2230 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2233 * NB: nvl_add_zplprop() will read the objset contents,
2234 * which we aren't supposed to do with a DS_MODE_USER
2235 * hold, because it could be inconsistent.
2237 if (zc->zc_nvlist_dst != 0 &&
2238 !zc->zc_objset_stats.dds_inconsistent &&
2239 dmu_objset_type(os) == DMU_OST_ZFS) {
2242 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2243 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2244 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2245 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2246 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2247 err = put_nvlist(zc, nv);
2250 err = SET_ERROR(ENOENT);
2252 dmu_objset_rele(os, FTAG);
2258 * zc_name name of filesystem
2259 * zc_cookie zap cursor
2260 * zc_nvlist_dst_size size of buffer for property nvlist
2263 * zc_name name of next filesystem
2264 * zc_cookie zap cursor
2265 * zc_objset_stats stats
2266 * zc_nvlist_dst property nvlist
2267 * zc_nvlist_dst_size size of property nvlist
2270 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2275 size_t orig_len = strlen(zc->zc_name);
2278 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2279 if (error == ENOENT)
2280 error = SET_ERROR(ESRCH);
2284 p = strrchr(zc->zc_name, '/');
2285 if (p == NULL || p[1] != '\0')
2286 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2287 p = zc->zc_name + strlen(zc->zc_name);
2290 error = dmu_dir_list_next(os,
2291 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2292 NULL, &zc->zc_cookie);
2293 if (error == ENOENT)
2294 error = SET_ERROR(ESRCH);
2295 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2296 dmu_objset_rele(os, FTAG);
2299 * If it's an internal dataset (ie. with a '$' in its name),
2300 * don't try to get stats for it, otherwise we'll return ENOENT.
2302 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2303 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2304 if (error == ENOENT) {
2305 /* We lost a race with destroy, get the next one. */
2306 zc->zc_name[orig_len] = '\0';
2315 * zc_name name of filesystem
2316 * zc_cookie zap cursor
2317 * zc_nvlist_dst_size size of buffer for property 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)
2331 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2333 return (error == ENOENT ? ESRCH : error);
2337 * A dataset name of maximum length cannot have any snapshots,
2338 * so exit immediately.
2340 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2341 ZFS_MAX_DATASET_NAME_LEN) {
2342 dmu_objset_rele(os, FTAG);
2343 return (SET_ERROR(ESRCH));
2346 error = dmu_snapshot_list_next(os,
2347 sizeof (zc->zc_name) - strlen(zc->zc_name),
2348 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie,
2351 if (error == 0 && !zc->zc_simple) {
2353 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
2355 error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds);
2359 error = dmu_objset_from_ds(ds, &ossnap);
2361 error = zfs_ioc_objset_stats_impl(zc, ossnap);
2362 dsl_dataset_rele(ds, FTAG);
2364 } else if (error == ENOENT) {
2365 error = SET_ERROR(ESRCH);
2368 dmu_objset_rele(os, FTAG);
2369 /* if we failed, undo the @ that we tacked on to zc_name */
2371 *strchr(zc->zc_name, '@') = '\0';
2376 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2378 const char *propname = nvpair_name(pair);
2380 unsigned int vallen;
2383 zfs_userquota_prop_t type;
2389 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2391 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2392 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2394 return (SET_ERROR(EINVAL));
2398 * A correctly constructed propname is encoded as
2399 * userquota@<rid>-<domain>.
2401 if ((dash = strchr(propname, '-')) == NULL ||
2402 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2404 return (SET_ERROR(EINVAL));
2411 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2413 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2414 zfsvfs_rele(zfsvfs, FTAG);
2421 * If the named property is one that has a special function to set its value,
2422 * return 0 on success and a positive error code on failure; otherwise if it is
2423 * not one of the special properties handled by this function, return -1.
2425 * XXX: It would be better for callers of the property interface if we handled
2426 * these special cases in dsl_prop.c (in the dsl layer).
2429 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2432 const char *propname = nvpair_name(pair);
2433 zfs_prop_t prop = zfs_name_to_prop(propname);
2434 uint64_t intval = 0;
2435 char *strval = NULL;
2438 if (prop == ZPROP_INVAL) {
2439 if (zfs_prop_userquota(propname))
2440 return (zfs_prop_set_userquota(dsname, pair));
2444 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2446 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2447 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2451 /* all special properties are numeric except for keylocation */
2452 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2453 strval = fnvpair_value_string(pair);
2455 intval = fnvpair_value_uint64(pair);
2459 case ZFS_PROP_QUOTA:
2460 err = dsl_dir_set_quota(dsname, source, intval);
2462 case ZFS_PROP_REFQUOTA:
2463 err = dsl_dataset_set_refquota(dsname, source, intval);
2465 case ZFS_PROP_FILESYSTEM_LIMIT:
2466 case ZFS_PROP_SNAPSHOT_LIMIT:
2467 if (intval == UINT64_MAX) {
2468 /* clearing the limit, just do it */
2471 err = dsl_dir_activate_fs_ss_limit(dsname);
2474 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2475 * default path to set the value in the nvlist.
2480 case ZFS_PROP_KEYLOCATION:
2481 err = dsl_crypto_can_set_keylocation(dsname, strval);
2484 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2485 * default path to set the value in the nvlist.
2490 case ZFS_PROP_RESERVATION:
2491 err = dsl_dir_set_reservation(dsname, source, intval);
2493 case ZFS_PROP_REFRESERVATION:
2494 err = dsl_dataset_set_refreservation(dsname, source, intval);
2496 case ZFS_PROP_VOLSIZE:
2497 err = zvol_set_volsize(dsname, intval);
2499 case ZFS_PROP_SNAPDEV:
2500 err = zvol_set_snapdev(dsname, source, intval);
2502 case ZFS_PROP_VOLMODE:
2503 err = zvol_set_volmode(dsname, source, intval);
2505 case ZFS_PROP_VERSION:
2509 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2512 err = zfs_set_version(zfsvfs, intval);
2513 zfsvfs_rele(zfsvfs, FTAG);
2515 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2518 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2519 (void) strcpy(zc->zc_name, dsname);
2520 (void) zfs_ioc_userspace_upgrade(zc);
2521 (void) zfs_ioc_id_quota_upgrade(zc);
2522 kmem_free(zc, sizeof (zfs_cmd_t));
2534 * This function is best effort. If it fails to set any of the given properties,
2535 * it continues to set as many as it can and returns the last error
2536 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2537 * with the list of names of all the properties that failed along with the
2538 * corresponding error numbers.
2540 * If every property is set successfully, zero is returned and errlist is not
2544 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2553 nvlist_t *genericnvl = fnvlist_alloc();
2554 nvlist_t *retrynvl = fnvlist_alloc();
2557 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2558 const char *propname = nvpair_name(pair);
2559 zfs_prop_t prop = zfs_name_to_prop(propname);
2562 /* decode the property value */
2564 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2566 attrs = fnvpair_value_nvlist(pair);
2567 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2569 err = SET_ERROR(EINVAL);
2572 /* Validate value type */
2573 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2574 /* inherited properties are expected to be booleans */
2575 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2576 err = SET_ERROR(EINVAL);
2577 } else if (err == 0 && prop == ZPROP_INVAL) {
2578 if (zfs_prop_user(propname)) {
2579 if (nvpair_type(propval) != DATA_TYPE_STRING)
2580 err = SET_ERROR(EINVAL);
2581 } else if (zfs_prop_userquota(propname)) {
2582 if (nvpair_type(propval) !=
2583 DATA_TYPE_UINT64_ARRAY)
2584 err = SET_ERROR(EINVAL);
2586 err = SET_ERROR(EINVAL);
2588 } else if (err == 0) {
2589 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2590 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2591 err = SET_ERROR(EINVAL);
2592 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2595 intval = fnvpair_value_uint64(propval);
2597 switch (zfs_prop_get_type(prop)) {
2598 case PROP_TYPE_NUMBER:
2600 case PROP_TYPE_STRING:
2601 err = SET_ERROR(EINVAL);
2603 case PROP_TYPE_INDEX:
2604 if (zfs_prop_index_to_string(prop,
2605 intval, &unused) != 0)
2606 err = SET_ERROR(EINVAL);
2610 "unknown property type");
2613 err = SET_ERROR(EINVAL);
2617 /* Validate permissions */
2619 err = zfs_check_settable(dsname, pair, CRED());
2622 if (source == ZPROP_SRC_INHERITED)
2623 err = -1; /* does not need special handling */
2625 err = zfs_prop_set_special(dsname, source,
2629 * For better performance we build up a list of
2630 * properties to set in a single transaction.
2632 err = nvlist_add_nvpair(genericnvl, pair);
2633 } else if (err != 0 && nvl != retrynvl) {
2635 * This may be a spurious error caused by
2636 * receiving quota and reservation out of order.
2637 * Try again in a second pass.
2639 err = nvlist_add_nvpair(retrynvl, pair);
2644 if (errlist != NULL)
2645 fnvlist_add_int32(errlist, propname, err);
2650 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2655 if (!nvlist_empty(genericnvl) &&
2656 dsl_props_set(dsname, source, genericnvl) != 0) {
2658 * If this fails, we still want to set as many properties as we
2659 * can, so try setting them individually.
2662 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2663 const char *propname = nvpair_name(pair);
2667 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2669 attrs = fnvpair_value_nvlist(pair);
2670 propval = fnvlist_lookup_nvpair(attrs,
2674 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2675 strval = fnvpair_value_string(propval);
2676 err = dsl_prop_set_string(dsname, propname,
2678 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2679 err = dsl_prop_inherit(dsname, propname,
2682 intval = fnvpair_value_uint64(propval);
2683 err = dsl_prop_set_int(dsname, propname, source,
2688 if (errlist != NULL) {
2689 fnvlist_add_int32(errlist, propname,
2696 nvlist_free(genericnvl);
2697 nvlist_free(retrynvl);
2703 * Check that all the properties are valid user properties.
2706 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2708 nvpair_t *pair = NULL;
2711 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2712 const char *propname = nvpair_name(pair);
2714 if (!zfs_prop_user(propname) ||
2715 nvpair_type(pair) != DATA_TYPE_STRING)
2716 return (SET_ERROR(EINVAL));
2718 if ((error = zfs_secpolicy_write_perms(fsname,
2719 ZFS_DELEG_PERM_USERPROP, CRED())))
2722 if (strlen(propname) >= ZAP_MAXNAMELEN)
2723 return (SET_ERROR(ENAMETOOLONG));
2725 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2726 return (SET_ERROR(E2BIG));
2732 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2736 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2739 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2740 if (nvlist_exists(skipped, nvpair_name(pair)))
2743 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2748 clear_received_props(const char *dsname, nvlist_t *props,
2752 nvlist_t *cleared_props = NULL;
2753 props_skip(props, skipped, &cleared_props);
2754 if (!nvlist_empty(cleared_props)) {
2756 * Acts on local properties until the dataset has received
2757 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2759 zprop_source_t flags = (ZPROP_SRC_NONE |
2760 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2761 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2763 nvlist_free(cleared_props);
2769 * zc_name name of filesystem
2770 * zc_value name of property to set
2771 * zc_nvlist_src{_size} nvlist of properties to apply
2772 * zc_cookie received properties flag
2775 * zc_nvlist_dst{_size} error for each unapplied received property
2778 zfs_ioc_set_prop(zfs_cmd_t *zc)
2781 boolean_t received = zc->zc_cookie;
2782 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2787 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2788 zc->zc_iflags, &nvl)) != 0)
2792 nvlist_t *origprops;
2794 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2795 (void) clear_received_props(zc->zc_name,
2797 nvlist_free(origprops);
2800 error = dsl_prop_set_hasrecvd(zc->zc_name);
2803 errors = fnvlist_alloc();
2805 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2807 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2808 (void) put_nvlist(zc, errors);
2811 nvlist_free(errors);
2818 * zc_name name of filesystem
2819 * zc_value name of property to inherit
2820 * zc_cookie revert to received value if TRUE
2825 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2827 const char *propname = zc->zc_value;
2828 zfs_prop_t prop = zfs_name_to_prop(propname);
2829 boolean_t received = zc->zc_cookie;
2830 zprop_source_t source = (received
2831 ? ZPROP_SRC_NONE /* revert to received value, if any */
2832 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2840 * Only check this in the non-received case. We want to allow
2841 * 'inherit -S' to revert non-inheritable properties like quota
2842 * and reservation to the received or default values even though
2843 * they are not considered inheritable.
2845 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2846 return (SET_ERROR(EINVAL));
2849 if (prop == ZPROP_INVAL) {
2850 if (!zfs_prop_user(propname))
2851 return (SET_ERROR(EINVAL));
2853 type = PROP_TYPE_STRING;
2854 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2855 return (SET_ERROR(EINVAL));
2857 type = zfs_prop_get_type(prop);
2861 * zfs_prop_set_special() expects properties in the form of an
2862 * nvpair with type info.
2864 dummy = fnvlist_alloc();
2867 case PROP_TYPE_STRING:
2868 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2870 case PROP_TYPE_NUMBER:
2871 case PROP_TYPE_INDEX:
2872 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2875 err = SET_ERROR(EINVAL);
2879 pair = nvlist_next_nvpair(dummy, NULL);
2881 err = SET_ERROR(EINVAL);
2883 err = zfs_prop_set_special(zc->zc_name, source, pair);
2884 if (err == -1) /* property is not "special", needs handling */
2885 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2895 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2902 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2903 zc->zc_iflags, &props)))
2907 * If the only property is the configfile, then just do a spa_lookup()
2908 * to handle the faulted case.
2910 pair = nvlist_next_nvpair(props, NULL);
2911 if (pair != NULL && strcmp(nvpair_name(pair),
2912 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2913 nvlist_next_nvpair(props, pair) == NULL) {
2914 mutex_enter(&spa_namespace_lock);
2915 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2916 spa_configfile_set(spa, props, B_FALSE);
2917 spa_write_cachefile(spa, B_FALSE, B_TRUE);
2919 mutex_exit(&spa_namespace_lock);
2926 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2931 error = spa_prop_set(spa, props);
2934 spa_close(spa, FTAG);
2940 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2944 nvlist_t *nvp = NULL;
2946 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2948 * If the pool is faulted, there may be properties we can still
2949 * get (such as altroot and cachefile), so attempt to get them
2952 mutex_enter(&spa_namespace_lock);
2953 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2954 error = spa_prop_get(spa, &nvp);
2955 mutex_exit(&spa_namespace_lock);
2957 error = spa_prop_get(spa, &nvp);
2958 spa_close(spa, FTAG);
2961 if (error == 0 && zc->zc_nvlist_dst != 0)
2962 error = put_nvlist(zc, nvp);
2964 error = SET_ERROR(EFAULT);
2972 * zc_name name of filesystem
2973 * zc_nvlist_src{_size} nvlist of delegated permissions
2974 * zc_perm_action allow/unallow flag
2979 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
2982 nvlist_t *fsaclnv = NULL;
2984 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2985 zc->zc_iflags, &fsaclnv)) != 0)
2989 * Verify nvlist is constructed correctly
2991 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
2992 nvlist_free(fsaclnv);
2993 return (SET_ERROR(EINVAL));
2997 * If we don't have PRIV_SYS_MOUNT, then validate
2998 * that user is allowed to hand out each permission in
3002 error = secpolicy_zfs(CRED());
3004 if (zc->zc_perm_action == B_FALSE) {
3005 error = dsl_deleg_can_allow(zc->zc_name,
3008 error = dsl_deleg_can_unallow(zc->zc_name,
3014 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3016 nvlist_free(fsaclnv);
3022 * zc_name name of filesystem
3025 * zc_nvlist_src{_size} nvlist of delegated permissions
3028 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3033 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3034 error = put_nvlist(zc, nvp);
3043 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3045 zfs_creat_t *zct = arg;
3047 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3050 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3054 * os parent objset pointer (NULL if root fs)
3055 * fuids_ok fuids allowed in this version of the spa?
3056 * sa_ok SAs allowed in this version of the spa?
3057 * createprops list of properties requested by creator
3060 * zplprops values for the zplprops we attach to the master node object
3061 * is_ci true if requested file system will be purely case-insensitive
3063 * Determine the settings for utf8only, normalization and
3064 * casesensitivity. Specific values may have been requested by the
3065 * creator and/or we can inherit values from the parent dataset. If
3066 * the file system is of too early a vintage, a creator can not
3067 * request settings for these properties, even if the requested
3068 * setting is the default value. We don't actually want to create dsl
3069 * properties for these, so remove them from the source nvlist after
3073 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3074 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3075 nvlist_t *zplprops, boolean_t *is_ci)
3077 uint64_t sense = ZFS_PROP_UNDEFINED;
3078 uint64_t norm = ZFS_PROP_UNDEFINED;
3079 uint64_t u8 = ZFS_PROP_UNDEFINED;
3082 ASSERT(zplprops != NULL);
3084 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3085 return (SET_ERROR(EINVAL));
3088 * Pull out creator prop choices, if any.
3091 (void) nvlist_lookup_uint64(createprops,
3092 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3093 (void) nvlist_lookup_uint64(createprops,
3094 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3095 (void) nvlist_remove_all(createprops,
3096 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3097 (void) nvlist_lookup_uint64(createprops,
3098 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3099 (void) nvlist_remove_all(createprops,
3100 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3101 (void) nvlist_lookup_uint64(createprops,
3102 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3103 (void) nvlist_remove_all(createprops,
3104 zfs_prop_to_name(ZFS_PROP_CASE));
3108 * If the zpl version requested is whacky or the file system
3109 * or pool is version is too "young" to support normalization
3110 * and the creator tried to set a value for one of the props,
3113 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3114 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3115 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3116 (zplver < ZPL_VERSION_NORMALIZATION &&
3117 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3118 sense != ZFS_PROP_UNDEFINED)))
3119 return (SET_ERROR(ENOTSUP));
3122 * Put the version in the zplprops
3124 VERIFY(nvlist_add_uint64(zplprops,
3125 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3127 if (norm == ZFS_PROP_UNDEFINED &&
3128 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3130 VERIFY(nvlist_add_uint64(zplprops,
3131 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3134 * If we're normalizing, names must always be valid UTF-8 strings.
3138 if (u8 == ZFS_PROP_UNDEFINED &&
3139 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3141 VERIFY(nvlist_add_uint64(zplprops,
3142 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3144 if (sense == ZFS_PROP_UNDEFINED &&
3145 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3147 VERIFY(nvlist_add_uint64(zplprops,
3148 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3151 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3157 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3158 nvlist_t *zplprops, boolean_t *is_ci)
3160 boolean_t fuids_ok, sa_ok;
3161 uint64_t zplver = ZPL_VERSION;
3162 objset_t *os = NULL;
3163 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3169 (void) strlcpy(parentname, dataset, sizeof (parentname));
3170 cp = strrchr(parentname, '/');
3174 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3177 spa_vers = spa_version(spa);
3178 spa_close(spa, FTAG);
3180 zplver = zfs_zpl_version_map(spa_vers);
3181 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3182 sa_ok = (zplver >= ZPL_VERSION_SA);
3185 * Open parent object set so we can inherit zplprop values.
3187 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3190 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3192 dmu_objset_rele(os, FTAG);
3197 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3198 nvlist_t *zplprops, boolean_t *is_ci)
3202 uint64_t zplver = ZPL_VERSION;
3205 zplver = zfs_zpl_version_map(spa_vers);
3206 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3207 sa_ok = (zplver >= ZPL_VERSION_SA);
3209 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3210 createprops, zplprops, is_ci);
3216 * "type" -> dmu_objset_type_t (int32)
3217 * (optional) "props" -> { prop -> value }
3218 * (optional) "hidden_args" -> { "wkeydata" -> value }
3219 * raw uint8_t array of encryption wrapping key data (32 bytes)
3222 * outnvl: propname -> error code (int32)
3225 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3228 zfs_creat_t zct = { 0 };
3229 nvlist_t *nvprops = NULL;
3230 nvlist_t *hidden_args = NULL;
3231 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3233 dmu_objset_type_t type;
3234 boolean_t is_insensitive = B_FALSE;
3235 dsl_crypto_params_t *dcp = NULL;
3237 if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3238 return (SET_ERROR(EINVAL));
3240 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3241 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3245 cbfunc = zfs_create_cb;
3249 cbfunc = zvol_create_cb;
3256 if (strchr(fsname, '@') ||
3257 strchr(fsname, '%'))
3258 return (SET_ERROR(EINVAL));
3260 zct.zct_props = nvprops;
3263 return (SET_ERROR(EINVAL));
3265 if (type == DMU_OST_ZVOL) {
3266 uint64_t volsize, volblocksize;
3268 if (nvprops == NULL)
3269 return (SET_ERROR(EINVAL));
3270 if (nvlist_lookup_uint64(nvprops,
3271 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3272 return (SET_ERROR(EINVAL));
3274 if ((error = nvlist_lookup_uint64(nvprops,
3275 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3276 &volblocksize)) != 0 && error != ENOENT)
3277 return (SET_ERROR(EINVAL));
3280 volblocksize = zfs_prop_default_numeric(
3281 ZFS_PROP_VOLBLOCKSIZE);
3283 if ((error = zvol_check_volblocksize(fsname,
3284 volblocksize)) != 0 ||
3285 (error = zvol_check_volsize(volsize,
3286 volblocksize)) != 0)
3288 } else if (type == DMU_OST_ZFS) {
3292 * We have to have normalization and
3293 * case-folding flags correct when we do the
3294 * file system creation, so go figure them out
3297 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3298 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3299 error = zfs_fill_zplprops(fsname, nvprops,
3300 zct.zct_zplprops, &is_insensitive);
3302 nvlist_free(zct.zct_zplprops);
3307 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3310 nvlist_free(zct.zct_zplprops);
3314 error = dmu_objset_create(fsname, type,
3315 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3317 nvlist_free(zct.zct_zplprops);
3318 dsl_crypto_params_free(dcp, !!error);
3321 * It would be nice to do this atomically.
3324 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3331 * Volumes will return EBUSY and cannot be destroyed
3332 * until all asynchronous minor handling has completed.
3333 * Wait for the spa_zvol_taskq to drain then retry.
3335 error2 = dsl_destroy_head(fsname);
3336 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3337 error2 = spa_open(fsname, &spa, FTAG);
3339 taskq_wait(spa->spa_zvol_taskq);
3340 spa_close(spa, FTAG);
3342 error2 = dsl_destroy_head(fsname);
3351 * "origin" -> name of origin snapshot
3352 * (optional) "props" -> { prop -> value }
3353 * (optional) "hidden_args" -> { "wkeydata" -> value }
3354 * raw uint8_t array of encryption wrapping key data (32 bytes)
3358 * outnvl: propname -> error code (int32)
3361 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3364 nvlist_t *nvprops = NULL;
3367 if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3368 return (SET_ERROR(EINVAL));
3369 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3371 if (strchr(fsname, '@') ||
3372 strchr(fsname, '%'))
3373 return (SET_ERROR(EINVAL));
3375 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3376 return (SET_ERROR(EINVAL));
3378 error = dmu_objset_clone(fsname, origin_name);
3381 * It would be nice to do this atomically.
3384 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3387 (void) dsl_destroy_head(fsname);
3394 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3396 if (strchr(fsname, '@') ||
3397 strchr(fsname, '%'))
3398 return (SET_ERROR(EINVAL));
3400 return (dmu_objset_remap_indirects(fsname));
3405 * "snaps" -> { snapshot1, snapshot2 }
3406 * (optional) "props" -> { prop -> value (string) }
3409 * outnvl: snapshot -> error code (int32)
3412 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3415 nvlist_t *props = NULL;
3419 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3420 if ((error = zfs_check_userprops(poolname, props)) != 0)
3423 if (!nvlist_empty(props) &&
3424 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3425 return (SET_ERROR(ENOTSUP));
3427 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3428 return (SET_ERROR(EINVAL));
3429 poollen = strlen(poolname);
3430 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3431 pair = nvlist_next_nvpair(snaps, pair)) {
3432 const char *name = nvpair_name(pair);
3433 const char *cp = strchr(name, '@');
3436 * The snap name must contain an @, and the part after it must
3437 * contain only valid characters.
3440 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3441 return (SET_ERROR(EINVAL));
3444 * The snap must be in the specified pool.
3446 if (strncmp(name, poolname, poollen) != 0 ||
3447 (name[poollen] != '/' && name[poollen] != '@'))
3448 return (SET_ERROR(EXDEV));
3450 /* This must be the only snap of this fs. */
3451 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3452 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3453 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3455 return (SET_ERROR(EXDEV));
3460 error = dsl_dataset_snapshot(snaps, props, outnvl);
3466 * innvl: "message" -> string
3470 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3478 * The poolname in the ioctl is not set, we get it from the TSD,
3479 * which was set at the end of the last successful ioctl that allows
3480 * logging. The secpolicy func already checked that it is set.
3481 * Only one log ioctl is allowed after each successful ioctl, so
3482 * we clear the TSD here.
3484 poolname = tsd_get(zfs_allow_log_key);
3485 if (poolname == NULL)
3486 return (SET_ERROR(EINVAL));
3487 (void) tsd_set(zfs_allow_log_key, NULL);
3488 error = spa_open(poolname, &spa, FTAG);
3493 if (nvlist_lookup_string(innvl, "message", &message) != 0) {
3494 spa_close(spa, FTAG);
3495 return (SET_ERROR(EINVAL));
3498 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3499 spa_close(spa, FTAG);
3500 return (SET_ERROR(ENOTSUP));
3503 error = spa_history_log(spa, message);
3504 spa_close(spa, FTAG);
3509 * The dp_config_rwlock must not be held when calling this, because the
3510 * unmount may need to write out data.
3512 * This function is best-effort. Callers must deal gracefully if it
3513 * remains mounted (or is remounted after this call).
3515 * Returns 0 if the argument is not a snapshot, or it is not currently a
3516 * filesystem, or we were able to unmount it. Returns error code otherwise.
3519 zfs_unmount_snap(const char *snapname)
3521 if (strchr(snapname, '@') == NULL)
3524 (void) zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE);
3529 zfs_unmount_snap_cb(const char *snapname, void *arg)
3531 zfs_unmount_snap(snapname);
3536 * When a clone is destroyed, its origin may also need to be destroyed,
3537 * in which case it must be unmounted. This routine will do that unmount
3541 zfs_destroy_unmount_origin(const char *fsname)
3547 error = dmu_objset_hold(fsname, FTAG, &os);
3550 ds = dmu_objset_ds(os);
3551 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3552 char originname[ZFS_MAX_DATASET_NAME_LEN];
3553 dsl_dataset_name(ds->ds_prev, originname);
3554 dmu_objset_rele(os, FTAG);
3555 zfs_unmount_snap(originname);
3557 dmu_objset_rele(os, FTAG);
3563 * "snaps" -> { snapshot1, snapshot2 }
3564 * (optional boolean) "defer"
3567 * outnvl: snapshot -> error code (int32)
3571 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3577 if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3578 return (SET_ERROR(EINVAL));
3579 defer = nvlist_exists(innvl, "defer");
3581 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3582 pair = nvlist_next_nvpair(snaps, pair)) {
3583 zfs_unmount_snap(nvpair_name(pair));
3586 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3590 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3591 * All bookmarks must be in the same pool.
3594 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3597 * outnvl: bookmark -> error code (int32)
3602 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3604 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3605 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3609 * Verify the snapshot argument.
3611 if (nvpair_value_string(pair, &snap_name) != 0)
3612 return (SET_ERROR(EINVAL));
3615 /* Verify that the keys (bookmarks) are unique */
3616 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3617 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3618 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3619 return (SET_ERROR(EINVAL));
3623 return (dsl_bookmark_create(innvl, outnvl));
3628 * property 1, property 2, ...
3632 * bookmark name 1 -> { property 1, property 2, ... },
3633 * bookmark name 2 -> { property 1, property 2, ... }
3638 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3640 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3645 * bookmark name 1, bookmark name 2
3648 * outnvl: bookmark -> error code (int32)
3652 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3657 poollen = strlen(poolname);
3658 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3659 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3660 const char *name = nvpair_name(pair);
3661 const char *cp = strchr(name, '#');
3664 * The bookmark name must contain an #, and the part after it
3665 * must contain only valid characters.
3668 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3669 return (SET_ERROR(EINVAL));
3672 * The bookmark must be in the specified pool.
3674 if (strncmp(name, poolname, poollen) != 0 ||
3675 (name[poollen] != '/' && name[poollen] != '#'))
3676 return (SET_ERROR(EXDEV));
3679 error = dsl_bookmark_destroy(innvl, outnvl);
3684 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3688 uint64_t instrlimit, memlimit;
3689 boolean_t sync_flag;
3690 nvpair_t *nvarg = NULL;
3692 if (0 != nvlist_lookup_string(innvl, ZCP_ARG_PROGRAM, &program)) {
3695 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3698 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3699 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3701 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3702 memlimit = ZCP_DEFAULT_MEMLIMIT;
3704 if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3708 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3710 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3713 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3723 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3725 return (spa_checkpoint(poolname));
3734 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3737 return (spa_checkpoint_discard(poolname));
3742 * zc_name name of dataset to destroy
3743 * zc_objset_type type of objset
3744 * zc_defer_destroy mark for deferred destroy
3749 zfs_ioc_destroy(zfs_cmd_t *zc)
3753 if (zc->zc_objset_type == DMU_OST_ZFS)
3754 zfs_unmount_snap(zc->zc_name);
3756 if (strchr(zc->zc_name, '@')) {
3757 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3759 err = dsl_destroy_head(zc->zc_name);
3760 if (err == EEXIST) {
3762 * It is possible that the given DS may have
3763 * hidden child (%recv) datasets - "leftovers"
3764 * resulting from the previously interrupted
3767 * 6 extra bytes for /%recv
3769 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
3771 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
3772 zc->zc_name, recv_clone_name) >=
3774 return (SET_ERROR(EINVAL));
3777 * Try to remove the hidden child (%recv) and after
3778 * that try to remove the target dataset.
3779 * If the hidden child (%recv) does not exist
3780 * the original error (EEXIST) will be returned
3782 err = dsl_destroy_head(namebuf);
3784 err = dsl_destroy_head(zc->zc_name);
3785 else if (err == ENOENT)
3786 err = SET_ERROR(EEXIST);
3794 * fsname is name of dataset to rollback (to most recent snapshot)
3796 * innvl may contain name of expected target snapshot
3798 * outnvl: "target" -> name of most recent snapshot
3803 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3807 char *target = NULL;
3810 (void) nvlist_lookup_string(innvl, "target", &target);
3811 if (target != NULL) {
3812 const char *cp = strchr(target, '@');
3815 * The snap name must contain an @, and the part after it must
3816 * contain only valid characters.
3819 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3820 return (SET_ERROR(EINVAL));
3823 if (getzfsvfs(fsname, &zfsvfs) == 0) {
3826 ds = dmu_objset_ds(zfsvfs->z_os);
3827 error = zfs_suspend_fs(zfsvfs);
3831 error = dsl_dataset_rollback(fsname, target, zfsvfs,
3833 resume_err = zfs_resume_fs(zfsvfs, ds);
3834 error = error ? error : resume_err;
3836 deactivate_super(zfsvfs->z_sb);
3837 } else if ((zv = zvol_suspend(fsname)) != NULL) {
3838 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
3842 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3848 recursive_unmount(const char *fsname, void *arg)
3850 const char *snapname = arg;
3853 fullname = kmem_asprintf("%s@%s", fsname, snapname);
3854 zfs_unmount_snap(fullname);
3862 * zc_name old name of dataset
3863 * zc_value new name of dataset
3864 * zc_cookie recursive flag (only valid for snapshots)
3869 zfs_ioc_rename(zfs_cmd_t *zc)
3871 boolean_t recursive = zc->zc_cookie & 1;
3874 /* "zfs rename" from and to ...%recv datasets should both fail */
3875 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3876 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3877 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3878 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3879 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3880 return (SET_ERROR(EINVAL));
3882 at = strchr(zc->zc_name, '@');
3884 /* snaps must be in same fs */
3887 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3888 return (SET_ERROR(EXDEV));
3890 if (zc->zc_objset_type == DMU_OST_ZFS) {
3891 error = dmu_objset_find(zc->zc_name,
3892 recursive_unmount, at + 1,
3893 recursive ? DS_FIND_CHILDREN : 0);
3899 error = dsl_dataset_rename_snapshot(zc->zc_name,
3900 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3905 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3910 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3912 const char *propname = nvpair_name(pair);
3913 boolean_t issnap = (strchr(dsname, '@') != NULL);
3914 zfs_prop_t prop = zfs_name_to_prop(propname);
3918 if (prop == ZPROP_INVAL) {
3919 if (zfs_prop_user(propname)) {
3920 if ((err = zfs_secpolicy_write_perms(dsname,
3921 ZFS_DELEG_PERM_USERPROP, cr)))
3926 if (!issnap && zfs_prop_userquota(propname)) {
3927 const char *perm = NULL;
3928 const char *uq_prefix =
3929 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
3930 const char *gq_prefix =
3931 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
3932 const char *uiq_prefix =
3933 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
3934 const char *giq_prefix =
3935 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
3936 const char *pq_prefix =
3937 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
3938 const char *piq_prefix = zfs_userquota_prop_prefixes[\
3939 ZFS_PROP_PROJECTOBJQUOTA];
3941 if (strncmp(propname, uq_prefix,
3942 strlen(uq_prefix)) == 0) {
3943 perm = ZFS_DELEG_PERM_USERQUOTA;
3944 } else if (strncmp(propname, uiq_prefix,
3945 strlen(uiq_prefix)) == 0) {
3946 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
3947 } else if (strncmp(propname, gq_prefix,
3948 strlen(gq_prefix)) == 0) {
3949 perm = ZFS_DELEG_PERM_GROUPQUOTA;
3950 } else if (strncmp(propname, giq_prefix,
3951 strlen(giq_prefix)) == 0) {
3952 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
3953 } else if (strncmp(propname, pq_prefix,
3954 strlen(pq_prefix)) == 0) {
3955 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
3956 } else if (strncmp(propname, piq_prefix,
3957 strlen(piq_prefix)) == 0) {
3958 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
3960 /* {USER|GROUP|PROJECT}USED are read-only */
3961 return (SET_ERROR(EINVAL));
3964 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
3969 return (SET_ERROR(EINVAL));
3973 return (SET_ERROR(EINVAL));
3975 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
3977 * dsl_prop_get_all_impl() returns properties in this
3981 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
3982 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
3987 * Check that this value is valid for this pool version
3990 case ZFS_PROP_COMPRESSION:
3992 * If the user specified gzip compression, make sure
3993 * the SPA supports it. We ignore any errors here since
3994 * we'll catch them later.
3996 if (nvpair_value_uint64(pair, &intval) == 0) {
3997 if (intval >= ZIO_COMPRESS_GZIP_1 &&
3998 intval <= ZIO_COMPRESS_GZIP_9 &&
3999 zfs_earlier_version(dsname,
4000 SPA_VERSION_GZIP_COMPRESSION)) {
4001 return (SET_ERROR(ENOTSUP));
4004 if (intval == ZIO_COMPRESS_ZLE &&
4005 zfs_earlier_version(dsname,
4006 SPA_VERSION_ZLE_COMPRESSION))
4007 return (SET_ERROR(ENOTSUP));
4009 if (intval == ZIO_COMPRESS_LZ4) {
4012 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4015 if (!spa_feature_is_enabled(spa,
4016 SPA_FEATURE_LZ4_COMPRESS)) {
4017 spa_close(spa, FTAG);
4018 return (SET_ERROR(ENOTSUP));
4020 spa_close(spa, FTAG);
4024 * If this is a bootable dataset then
4025 * verify that the compression algorithm
4026 * is supported for booting. We must return
4027 * something other than ENOTSUP since it
4028 * implies a downrev pool version.
4030 if (zfs_is_bootfs(dsname) &&
4031 !BOOTFS_COMPRESS_VALID(intval)) {
4032 return (SET_ERROR(ERANGE));
4037 case ZFS_PROP_COPIES:
4038 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4039 return (SET_ERROR(ENOTSUP));
4042 case ZFS_PROP_VOLBLOCKSIZE:
4043 case ZFS_PROP_RECORDSIZE:
4044 /* Record sizes above 128k need the feature to be enabled */
4045 if (nvpair_value_uint64(pair, &intval) == 0 &&
4046 intval > SPA_OLD_MAXBLOCKSIZE) {
4050 * We don't allow setting the property above 1MB,
4051 * unless the tunable has been changed.
4053 if (intval > zfs_max_recordsize ||
4054 intval > SPA_MAXBLOCKSIZE)
4055 return (SET_ERROR(ERANGE));
4057 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4060 if (!spa_feature_is_enabled(spa,
4061 SPA_FEATURE_LARGE_BLOCKS)) {
4062 spa_close(spa, FTAG);
4063 return (SET_ERROR(ENOTSUP));
4065 spa_close(spa, FTAG);
4069 case ZFS_PROP_DNODESIZE:
4070 /* Dnode sizes above 512 need the feature to be enabled */
4071 if (nvpair_value_uint64(pair, &intval) == 0 &&
4072 intval != ZFS_DNSIZE_LEGACY) {
4076 * If this is a bootable dataset then
4077 * we don't allow large (>512B) dnodes,
4078 * because GRUB doesn't support them.
4080 if (zfs_is_bootfs(dsname) &&
4081 intval != ZFS_DNSIZE_LEGACY) {
4082 return (SET_ERROR(EDOM));
4085 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4088 if (!spa_feature_is_enabled(spa,
4089 SPA_FEATURE_LARGE_DNODE)) {
4090 spa_close(spa, FTAG);
4091 return (SET_ERROR(ENOTSUP));
4093 spa_close(spa, FTAG);
4097 case ZFS_PROP_SHARESMB:
4098 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4099 return (SET_ERROR(ENOTSUP));
4102 case ZFS_PROP_ACLINHERIT:
4103 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4104 nvpair_value_uint64(pair, &intval) == 0) {
4105 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4106 zfs_earlier_version(dsname,
4107 SPA_VERSION_PASSTHROUGH_X))
4108 return (SET_ERROR(ENOTSUP));
4111 case ZFS_PROP_CHECKSUM:
4112 case ZFS_PROP_DEDUP:
4114 spa_feature_t feature;
4118 /* dedup feature version checks */
4119 if (prop == ZFS_PROP_DEDUP &&
4120 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4121 return (SET_ERROR(ENOTSUP));
4123 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4124 nvpair_value_uint64(pair, &intval) == 0) {
4125 /* check prop value is enabled in features */
4126 feature = zio_checksum_to_feature(
4127 intval & ZIO_CHECKSUM_MASK);
4128 if (feature == SPA_FEATURE_NONE)
4131 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4134 if (!spa_feature_is_enabled(spa, feature)) {
4135 spa_close(spa, FTAG);
4136 return (SET_ERROR(ENOTSUP));
4138 spa_close(spa, FTAG);
4147 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4151 * Removes properties from the given props list that fail permission checks
4152 * needed to clear them and to restore them in case of a receive error. For each
4153 * property, make sure we have both set and inherit permissions.
4155 * Returns the first error encountered if any permission checks fail. If the
4156 * caller provides a non-NULL errlist, it also gives the complete list of names
4157 * of all the properties that failed a permission check along with the
4158 * corresponding error numbers. The caller is responsible for freeing the
4161 * If every property checks out successfully, zero is returned and the list
4162 * pointed at by errlist is NULL.
4165 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4168 nvpair_t *pair, *next_pair;
4175 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4177 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4178 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4179 pair = nvlist_next_nvpair(props, NULL);
4180 while (pair != NULL) {
4181 next_pair = nvlist_next_nvpair(props, pair);
4183 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4184 sizeof (zc->zc_value));
4185 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4186 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4187 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4188 VERIFY(nvlist_add_int32(errors,
4189 zc->zc_value, err) == 0);
4193 kmem_free(zc, sizeof (zfs_cmd_t));
4195 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4196 nvlist_free(errors);
4199 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4202 if (errlist == NULL)
4203 nvlist_free(errors);
4211 propval_equals(nvpair_t *p1, nvpair_t *p2)
4213 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4214 /* dsl_prop_get_all_impl() format */
4216 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4217 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4221 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4223 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4224 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4228 if (nvpair_type(p1) != nvpair_type(p2))
4231 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4232 char *valstr1, *valstr2;
4234 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4235 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4236 return (strcmp(valstr1, valstr2) == 0);
4238 uint64_t intval1, intval2;
4240 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4241 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4242 return (intval1 == intval2);
4247 * Remove properties from props if they are not going to change (as determined
4248 * by comparison with origprops). Remove them from origprops as well, since we
4249 * do not need to clear or restore properties that won't change.
4252 props_reduce(nvlist_t *props, nvlist_t *origprops)
4254 nvpair_t *pair, *next_pair;
4256 if (origprops == NULL)
4257 return; /* all props need to be received */
4259 pair = nvlist_next_nvpair(props, NULL);
4260 while (pair != NULL) {
4261 const char *propname = nvpair_name(pair);
4264 next_pair = nvlist_next_nvpair(props, pair);
4266 if ((nvlist_lookup_nvpair(origprops, propname,
4267 &match) != 0) || !propval_equals(pair, match))
4268 goto next; /* need to set received value */
4270 /* don't clear the existing received value */
4271 (void) nvlist_remove_nvpair(origprops, match);
4272 /* don't bother receiving the property */
4273 (void) nvlist_remove_nvpair(props, pair);
4280 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4281 * For example, refquota cannot be set until after the receipt of a dataset,
4282 * because in replication streams, an older/earlier snapshot may exceed the
4283 * refquota. We want to receive the older/earlier snapshot, but setting
4284 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4285 * the older/earlier snapshot from being received (with EDQUOT).
4287 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4289 * libzfs will need to be judicious handling errors encountered by props
4290 * extracted by this function.
4293 extract_delay_props(nvlist_t *props)
4295 nvlist_t *delayprops;
4296 nvpair_t *nvp, *tmp;
4297 static const zfs_prop_t delayable[] = {
4299 ZFS_PROP_KEYLOCATION,
4304 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4306 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4307 nvp = nvlist_next_nvpair(props, nvp)) {
4309 * strcmp() is safe because zfs_prop_to_name() always returns
4312 for (i = 0; delayable[i] != 0; i++) {
4313 if (strcmp(zfs_prop_to_name(delayable[i]),
4314 nvpair_name(nvp)) == 0) {
4318 if (delayable[i] != 0) {
4319 tmp = nvlist_prev_nvpair(props, nvp);
4320 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4321 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4326 if (nvlist_empty(delayprops)) {
4327 nvlist_free(delayprops);
4330 return (delayprops);
4334 static boolean_t zfs_ioc_recv_inject_err;
4338 * nvlist 'errors' is always allocated. It will contain descriptions of
4339 * encountered errors, if any. It's the callers responsibility to free.
4342 zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
4343 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
4344 boolean_t resumable, int input_fd, dmu_replay_record_t *begin_record,
4345 int cleanup_fd, uint64_t *read_bytes, uint64_t *errflags,
4346 uint64_t *action_handle, nvlist_t **errors)
4348 dmu_recv_cookie_t drc;
4350 int props_error = 0;
4352 nvlist_t *local_delayprops = NULL;
4353 nvlist_t *recv_delayprops = NULL;
4354 nvlist_t *origprops = NULL; /* existing properties */
4355 nvlist_t *origrecvd = NULL; /* existing received properties */
4356 boolean_t first_recvd_props = B_FALSE;
4361 *errors = fnvlist_alloc();
4363 input_fp = getf(input_fd);
4364 if (input_fp == NULL)
4365 return (SET_ERROR(EBADF));
4367 error = dmu_recv_begin(tofs, tosnap, begin_record, force,
4368 resumable, localprops, hidden_args, origin, &drc);
4373 * Set properties before we receive the stream so that they are applied
4374 * to the new data. Note that we must call dmu_recv_stream() if
4375 * dmu_recv_begin() succeeds.
4377 if (recvprops != NULL && !drc.drc_newfs) {
4378 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4379 SPA_VERSION_RECVD_PROPS &&
4380 !dsl_prop_get_hasrecvd(tofs))
4381 first_recvd_props = B_TRUE;
4384 * If new received properties are supplied, they are to
4385 * completely replace the existing received properties,
4386 * so stash away the existing ones.
4388 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
4389 nvlist_t *errlist = NULL;
4391 * Don't bother writing a property if its value won't
4392 * change (and avoid the unnecessary security checks).
4394 * The first receive after SPA_VERSION_RECVD_PROPS is a
4395 * special case where we blow away all local properties
4398 if (!first_recvd_props)
4399 props_reduce(recvprops, origrecvd);
4400 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
4401 (void) nvlist_merge(*errors, errlist, 0);
4402 nvlist_free(errlist);
4404 if (clear_received_props(tofs, origrecvd,
4405 first_recvd_props ? NULL : recvprops) != 0)
4406 *errflags |= ZPROP_ERR_NOCLEAR;
4408 *errflags |= ZPROP_ERR_NOCLEAR;
4413 * Stash away existing properties so we can restore them on error unless
4414 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
4415 * case "origrecvd" will take care of that.
4417 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
4419 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
4420 if (dsl_prop_get_all(os, &origprops) != 0) {
4421 *errflags |= ZPROP_ERR_NOCLEAR;
4423 dmu_objset_rele(os, FTAG);
4425 *errflags |= ZPROP_ERR_NOCLEAR;
4429 if (recvprops != NULL) {
4430 props_error = dsl_prop_set_hasrecvd(tofs);
4432 if (props_error == 0) {
4433 recv_delayprops = extract_delay_props(recvprops);
4434 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4435 recvprops, *errors);
4439 if (localprops != NULL) {
4440 nvlist_t *oprops = fnvlist_alloc();
4441 nvlist_t *xprops = fnvlist_alloc();
4442 nvpair_t *nvp = NULL;
4444 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4445 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
4447 const char *name = nvpair_name(nvp);
4448 zfs_prop_t prop = zfs_name_to_prop(name);
4449 if (prop != ZPROP_INVAL) {
4450 if (!zfs_prop_inheritable(prop))
4452 } else if (!zfs_prop_user(name))
4454 fnvlist_add_boolean(xprops, name);
4456 /* -o property=value */
4457 fnvlist_add_nvpair(oprops, nvp);
4461 local_delayprops = extract_delay_props(oprops);
4462 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4464 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
4467 nvlist_free(oprops);
4468 nvlist_free(xprops);
4471 off = input_fp->f_offset;
4472 error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd,
4476 zfsvfs_t *zfsvfs = NULL;
4477 zvol_state_t *zv = NULL;
4479 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4484 ds = dmu_objset_ds(zfsvfs->z_os);
4485 error = zfs_suspend_fs(zfsvfs);
4487 * If the suspend fails, then the recv_end will
4488 * likely also fail, and clean up after itself.
4490 end_err = dmu_recv_end(&drc, zfsvfs);
4492 error = zfs_resume_fs(zfsvfs, ds);
4493 error = error ? error : end_err;
4494 deactivate_super(zfsvfs->z_sb);
4495 } else if ((zv = zvol_suspend(tofs)) != NULL) {
4496 error = dmu_recv_end(&drc, zvol_tag(zv));
4499 error = dmu_recv_end(&drc, NULL);
4502 /* Set delayed properties now, after we're done receiving. */
4503 if (recv_delayprops != NULL && error == 0) {
4504 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4505 recv_delayprops, *errors);
4507 if (local_delayprops != NULL && error == 0) {
4508 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
4509 local_delayprops, *errors);
4514 * Merge delayed props back in with initial props, in case
4515 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4516 * we have to make sure clear_received_props() includes
4517 * the delayed properties).
4519 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4520 * using ASSERT() will be just like a VERIFY.
4522 if (recv_delayprops != NULL) {
4523 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
4524 nvlist_free(recv_delayprops);
4526 if (local_delayprops != NULL) {
4527 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
4528 nvlist_free(local_delayprops);
4531 *read_bytes = off - input_fp->f_offset;
4532 if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0)
4533 input_fp->f_offset = off;
4536 if (zfs_ioc_recv_inject_err) {
4537 zfs_ioc_recv_inject_err = B_FALSE;
4543 * On error, restore the original props.
4545 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
4546 if (clear_received_props(tofs, recvprops, NULL) != 0) {
4548 * We failed to clear the received properties.
4549 * Since we may have left a $recvd value on the
4550 * system, we can't clear the $hasrecvd flag.
4552 *errflags |= ZPROP_ERR_NORESTORE;
4553 } else if (first_recvd_props) {
4554 dsl_prop_unset_hasrecvd(tofs);
4557 if (origrecvd == NULL && !drc.drc_newfs) {
4558 /* We failed to stash the original properties. */
4559 *errflags |= ZPROP_ERR_NORESTORE;
4563 * dsl_props_set() will not convert RECEIVED to LOCAL on or
4564 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4565 * explicitly if we're restoring local properties cleared in the
4566 * first new-style receive.
4568 if (origrecvd != NULL &&
4569 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4570 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4571 origrecvd, NULL) != 0) {
4573 * We stashed the original properties but failed to
4576 *errflags |= ZPROP_ERR_NORESTORE;
4579 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
4580 !first_recvd_props) {
4582 nvlist_t *inheritprops;
4585 if (origprops == NULL) {
4586 /* We failed to stash the original properties. */
4587 *errflags |= ZPROP_ERR_NORESTORE;
4591 /* Restore original props */
4592 setprops = fnvlist_alloc();
4593 inheritprops = fnvlist_alloc();
4595 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
4596 const char *name = nvpair_name(nvp);
4600 if (!nvlist_exists(origprops, name)) {
4602 * Property was not present or was explicitly
4603 * inherited before the receive, restore this.
4605 fnvlist_add_boolean(inheritprops, name);
4608 attrs = fnvlist_lookup_nvlist(origprops, name);
4609 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
4611 /* Skip received properties */
4612 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
4615 if (strcmp(source, tofs) == 0) {
4616 /* Property was locally set */
4617 fnvlist_add_nvlist(setprops, name, attrs);
4619 /* Property was implicitly inherited */
4620 fnvlist_add_boolean(inheritprops, name);
4624 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
4626 *errflags |= ZPROP_ERR_NORESTORE;
4627 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
4629 *errflags |= ZPROP_ERR_NORESTORE;
4631 nvlist_free(setprops);
4632 nvlist_free(inheritprops);
4636 nvlist_free(origrecvd);
4637 nvlist_free(origprops);
4640 error = props_error;
4647 * zc_name name of containing filesystem (unused)
4648 * zc_nvlist_src{_size} nvlist of properties to apply
4649 * zc_nvlist_conf{_size} nvlist of properties to exclude
4650 * (DATA_TYPE_BOOLEAN) and override (everything else)
4651 * zc_value name of snapshot to create
4652 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4653 * zc_cookie file descriptor to recv from
4654 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4655 * zc_guid force flag
4656 * zc_cleanup_fd cleanup-on-exit file descriptor
4657 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4660 * zc_cookie number of bytes read
4661 * zc_obj zprop_errflags_t
4662 * zc_action_handle handle for this guid/ds mapping
4663 * zc_nvlist_dst{_size} error for each unapplied received property
4666 zfs_ioc_recv(zfs_cmd_t *zc)
4668 dmu_replay_record_t begin_record;
4669 nvlist_t *errors = NULL;
4670 nvlist_t *recvdprops = NULL;
4671 nvlist_t *localprops = NULL;
4672 char *origin = NULL;
4674 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4677 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4678 strchr(zc->zc_value, '@') == NULL ||
4679 strchr(zc->zc_value, '%'))
4680 return (SET_ERROR(EINVAL));
4682 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
4683 tosnap = strchr(tofs, '@');
4686 if (zc->zc_nvlist_src != 0 &&
4687 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4688 zc->zc_iflags, &recvdprops)) != 0)
4691 if (zc->zc_nvlist_conf != 0 &&
4692 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
4693 zc->zc_iflags, &localprops)) != 0)
4696 if (zc->zc_string[0])
4697 origin = zc->zc_string;
4699 begin_record.drr_type = DRR_BEGIN;
4700 begin_record.drr_payloadlen = 0;
4701 begin_record.drr_u.drr_begin = zc->zc_begin_record;
4703 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
4704 NULL, zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record,
4705 zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj,
4706 &zc->zc_action_handle, &errors);
4707 nvlist_free(recvdprops);
4708 nvlist_free(localprops);
4711 * Now that all props, initial and delayed, are set, report the prop
4712 * errors to the caller.
4714 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
4715 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4716 put_nvlist(zc, errors) != 0)) {
4718 * Caller made zc->zc_nvlist_dst less than the minimum expected
4719 * size or supplied an invalid address.
4721 error = SET_ERROR(EINVAL);
4724 nvlist_free(errors);
4731 * "snapname" -> full name of the snapshot to create
4732 * (optional) "props" -> received properties to set (nvlist)
4733 * (optional) "localprops" -> override and exclude properties (nvlist)
4734 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
4735 * "begin_record" -> non-byteswapped dmu_replay_record_t
4736 * "input_fd" -> file descriptor to read stream from (int32)
4737 * (optional) "force" -> force flag (value ignored)
4738 * (optional) "resumable" -> resumable flag (value ignored)
4739 * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor
4740 * (optional) "action_handle" -> handle for this guid/ds mapping
4744 * "read_bytes" -> number of bytes read
4745 * "error_flags" -> zprop_errflags_t
4746 * "action_handle" -> handle for this guid/ds mapping
4747 * "errors" -> error for each unapplied received property (nvlist)
4751 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4753 dmu_replay_record_t *begin_record;
4754 uint_t begin_record_size;
4755 nvlist_t *errors = NULL;
4756 nvlist_t *recvprops = NULL;
4757 nvlist_t *localprops = NULL;
4758 nvlist_t *hidden_args = NULL;
4759 char *snapname = NULL;
4760 char *origin = NULL;
4762 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4764 boolean_t resumable;
4765 uint64_t action_handle = 0;
4766 uint64_t read_bytes = 0;
4767 uint64_t errflags = 0;
4769 int cleanup_fd = -1;
4772 error = nvlist_lookup_string(innvl, "snapname", &snapname);
4774 return (SET_ERROR(EINVAL));
4776 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
4777 strchr(snapname, '@') == NULL ||
4778 strchr(snapname, '%'))
4779 return (SET_ERROR(EINVAL));
4781 (void) strcpy(tofs, snapname);
4782 tosnap = strchr(tofs, '@');
4785 error = nvlist_lookup_string(innvl, "origin", &origin);
4786 if (error && error != ENOENT)
4789 error = nvlist_lookup_byte_array(innvl, "begin_record",
4790 (uchar_t **)&begin_record, &begin_record_size);
4791 if (error != 0 || begin_record_size != sizeof (*begin_record))
4792 return (SET_ERROR(EINVAL));
4794 error = nvlist_lookup_int32(innvl, "input_fd", &input_fd);
4796 return (SET_ERROR(EINVAL));
4798 force = nvlist_exists(innvl, "force");
4799 resumable = nvlist_exists(innvl, "resumable");
4801 error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd);
4802 if (error && error != ENOENT)
4805 error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle);
4806 if (error && error != ENOENT)
4809 /* we still use "props" here for backwards compatibility */
4810 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
4811 if (error && error != ENOENT)
4814 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
4815 if (error && error != ENOENT)
4818 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
4819 if (error && error != ENOENT)
4822 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
4823 hidden_args, force, resumable, input_fd, begin_record, cleanup_fd,
4824 &read_bytes, &errflags, &action_handle, &errors);
4826 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
4827 fnvlist_add_uint64(outnvl, "error_flags", errflags);
4828 fnvlist_add_uint64(outnvl, "action_handle", action_handle);
4829 fnvlist_add_nvlist(outnvl, "errors", errors);
4831 nvlist_free(errors);
4832 nvlist_free(recvprops);
4833 nvlist_free(localprops);
4840 * zc_name name of snapshot to send
4841 * zc_cookie file descriptor to send stream to
4842 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
4843 * zc_sendobj objsetid of snapshot to send
4844 * zc_fromobj objsetid of incremental fromsnap (may be zero)
4845 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
4846 * output size in zc_objset_type.
4847 * zc_flags lzc_send_flags
4850 * zc_objset_type estimated size, if zc_guid is set
4852 * NOTE: This is no longer the preferred interface, any new functionality
4853 * should be added to zfs_ioc_send_new() instead.
4856 zfs_ioc_send(zfs_cmd_t *zc)
4860 boolean_t estimate = (zc->zc_guid != 0);
4861 boolean_t embedok = (zc->zc_flags & 0x1);
4862 boolean_t large_block_ok = (zc->zc_flags & 0x2);
4863 boolean_t compressok = (zc->zc_flags & 0x4);
4864 boolean_t rawok = (zc->zc_flags & 0x8);
4866 if (zc->zc_obj != 0) {
4868 dsl_dataset_t *tosnap;
4870 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4874 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
4876 dsl_pool_rele(dp, FTAG);
4880 if (dsl_dir_is_clone(tosnap->ds_dir))
4882 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
4883 dsl_dataset_rele(tosnap, FTAG);
4884 dsl_pool_rele(dp, FTAG);
4889 dsl_dataset_t *tosnap;
4890 dsl_dataset_t *fromsnap = NULL;
4892 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4896 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
4899 dsl_pool_rele(dp, FTAG);
4903 if (zc->zc_fromobj != 0) {
4904 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4907 dsl_dataset_rele(tosnap, FTAG);
4908 dsl_pool_rele(dp, FTAG);
4913 error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok,
4914 &zc->zc_objset_type);
4916 if (fromsnap != NULL)
4917 dsl_dataset_rele(fromsnap, FTAG);
4918 dsl_dataset_rele(tosnap, FTAG);
4919 dsl_pool_rele(dp, FTAG);
4921 file_t *fp = getf(zc->zc_cookie);
4923 return (SET_ERROR(EBADF));
4926 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4927 zc->zc_fromobj, embedok, large_block_ok, compressok, rawok,
4928 zc->zc_cookie, fp->f_vnode, &off);
4930 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4932 releasef(zc->zc_cookie);
4939 * zc_name name of snapshot on which to report progress
4940 * zc_cookie file descriptor of send stream
4943 * zc_cookie number of bytes written in send stream thus far
4946 zfs_ioc_send_progress(zfs_cmd_t *zc)
4950 dmu_sendarg_t *dsp = NULL;
4953 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4957 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4959 dsl_pool_rele(dp, FTAG);
4963 mutex_enter(&ds->ds_sendstream_lock);
4966 * Iterate over all the send streams currently active on this dataset.
4967 * If there's one which matches the specified file descriptor _and_ the
4968 * stream was started by the current process, return the progress of
4972 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
4973 dsp = list_next(&ds->ds_sendstreams, dsp)) {
4974 if (dsp->dsa_outfd == zc->zc_cookie &&
4975 dsp->dsa_proc->group_leader == curproc->group_leader)
4980 zc->zc_cookie = *(dsp->dsa_off);
4982 error = SET_ERROR(ENOENT);
4984 mutex_exit(&ds->ds_sendstream_lock);
4985 dsl_dataset_rele(ds, FTAG);
4986 dsl_pool_rele(dp, FTAG);
4991 zfs_ioc_inject_fault(zfs_cmd_t *zc)
4995 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
4996 &zc->zc_inject_record);
4999 zc->zc_guid = (uint64_t)id;
5005 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5007 return (zio_clear_fault((int)zc->zc_guid));
5011 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5013 int id = (int)zc->zc_guid;
5016 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5017 &zc->zc_inject_record);
5025 zfs_ioc_error_log(zfs_cmd_t *zc)
5029 size_t count = (size_t)zc->zc_nvlist_dst_size;
5031 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5034 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5037 zc->zc_nvlist_dst_size = count;
5039 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5041 spa_close(spa, FTAG);
5047 zfs_ioc_clear(zfs_cmd_t *zc)
5054 * On zpool clear we also fix up missing slogs
5056 mutex_enter(&spa_namespace_lock);
5057 spa = spa_lookup(zc->zc_name);
5059 mutex_exit(&spa_namespace_lock);
5060 return (SET_ERROR(EIO));
5062 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5063 /* we need to let spa_open/spa_load clear the chains */
5064 spa_set_log_state(spa, SPA_LOG_CLEAR);
5066 spa->spa_last_open_failed = 0;
5067 mutex_exit(&spa_namespace_lock);
5069 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5070 error = spa_open(zc->zc_name, &spa, FTAG);
5073 nvlist_t *config = NULL;
5075 if (zc->zc_nvlist_src == 0)
5076 return (SET_ERROR(EINVAL));
5078 if ((error = get_nvlist(zc->zc_nvlist_src,
5079 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5080 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5082 if (config != NULL) {
5085 if ((err = put_nvlist(zc, config)) != 0)
5087 nvlist_free(config);
5089 nvlist_free(policy);
5096 spa_vdev_state_enter(spa, SCL_NONE);
5098 if (zc->zc_guid == 0) {
5101 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5103 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5104 spa_close(spa, FTAG);
5105 return (SET_ERROR(ENODEV));
5109 vdev_clear(spa, vd);
5111 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5112 NULL : spa->spa_root_vdev, 0);
5115 * Resume any suspended I/Os.
5117 if (zio_resume(spa) != 0)
5118 error = SET_ERROR(EIO);
5120 spa_close(spa, FTAG);
5126 * Reopen all the vdevs associated with the pool.
5129 * "scrub_restart" -> when true and scrub is running, allow to restart
5130 * scrub as the side effect of the reopen (boolean).
5137 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5141 boolean_t scrub_restart = B_TRUE;
5144 if (nvlist_lookup_boolean_value(innvl, "scrub_restart",
5145 &scrub_restart) != 0) {
5146 return (SET_ERROR(EINVAL));
5150 error = spa_open(pool, &spa, FTAG);
5154 spa_vdev_state_enter(spa, SCL_NONE);
5157 * If the scrub_restart flag is B_FALSE and a scrub is already
5158 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5159 * we don't restart the scrub as a side effect of the reopen.
5160 * Otherwise, let vdev_open() decided if a resilver is required.
5163 spa->spa_scrub_reopen = (!scrub_restart &&
5164 dsl_scan_scrubbing(spa->spa_dsl_pool));
5165 vdev_reopen(spa->spa_root_vdev);
5166 spa->spa_scrub_reopen = B_FALSE;
5168 (void) spa_vdev_state_exit(spa, NULL, 0);
5169 spa_close(spa, FTAG);
5175 * zc_name name of filesystem
5178 * zc_string name of conflicting snapshot, if there is one
5181 zfs_ioc_promote(zfs_cmd_t *zc)
5184 dsl_dataset_t *ds, *ods;
5185 char origin[ZFS_MAX_DATASET_NAME_LEN];
5189 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5190 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5191 strchr(zc->zc_name, '%'))
5192 return (SET_ERROR(EINVAL));
5194 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5198 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5200 dsl_pool_rele(dp, FTAG);
5204 if (!dsl_dir_is_clone(ds->ds_dir)) {
5205 dsl_dataset_rele(ds, FTAG);
5206 dsl_pool_rele(dp, FTAG);
5207 return (SET_ERROR(EINVAL));
5210 error = dsl_dataset_hold_obj(dp,
5211 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5213 dsl_dataset_rele(ds, FTAG);
5214 dsl_pool_rele(dp, FTAG);
5218 dsl_dataset_name(ods, origin);
5219 dsl_dataset_rele(ods, FTAG);
5220 dsl_dataset_rele(ds, FTAG);
5221 dsl_pool_rele(dp, FTAG);
5224 * We don't need to unmount *all* the origin fs's snapshots, but
5227 cp = strchr(origin, '@');
5230 (void) dmu_objset_find(origin,
5231 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5232 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5236 * Retrieve a single {user|group|project}{used|quota}@... property.
5239 * zc_name name of filesystem
5240 * zc_objset_type zfs_userquota_prop_t
5241 * zc_value domain name (eg. "S-1-234-567-89")
5242 * zc_guid RID/UID/GID
5245 * zc_cookie property value
5248 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5253 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5254 return (SET_ERROR(EINVAL));
5256 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5260 error = zfs_userspace_one(zfsvfs,
5261 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5262 zfsvfs_rele(zfsvfs, FTAG);
5269 * zc_name name of filesystem
5270 * zc_cookie zap cursor
5271 * zc_objset_type zfs_userquota_prop_t
5272 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5275 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5276 * zc_cookie zap cursor
5279 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5282 int bufsize = zc->zc_nvlist_dst_size;
5285 return (SET_ERROR(ENOMEM));
5287 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5291 void *buf = vmem_alloc(bufsize, KM_SLEEP);
5293 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5294 buf, &zc->zc_nvlist_dst_size);
5297 error = xcopyout(buf,
5298 (void *)(uintptr_t)zc->zc_nvlist_dst,
5299 zc->zc_nvlist_dst_size);
5301 vmem_free(buf, bufsize);
5302 zfsvfs_rele(zfsvfs, FTAG);
5309 * zc_name name of filesystem
5315 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5321 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5322 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5324 * If userused is not enabled, it may be because the
5325 * objset needs to be closed & reopened (to grow the
5326 * objset_phys_t). Suspend/resume the fs will do that.
5328 dsl_dataset_t *ds, *newds;
5330 ds = dmu_objset_ds(zfsvfs->z_os);
5331 error = zfs_suspend_fs(zfsvfs);
5333 dmu_objset_refresh_ownership(ds, &newds,
5335 error = zfs_resume_fs(zfsvfs, newds);
5339 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5340 deactivate_super(zfsvfs->z_sb);
5342 /* XXX kind of reading contents without owning */
5343 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5347 error = dmu_objset_userspace_upgrade(os);
5348 dmu_objset_rele_flags(os, B_TRUE, FTAG);
5356 * zc_name name of filesystem
5362 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
5367 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
5371 if (dmu_objset_userobjspace_upgradable(os) ||
5372 dmu_objset_projectquota_upgradable(os)) {
5373 mutex_enter(&os->os_upgrade_lock);
5374 if (os->os_upgrade_id == 0) {
5375 /* clear potential error code and retry */
5376 os->os_upgrade_status = 0;
5377 mutex_exit(&os->os_upgrade_lock);
5379 dmu_objset_id_quota_upgrade(os);
5381 mutex_exit(&os->os_upgrade_lock);
5384 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5386 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
5387 error = os->os_upgrade_status;
5389 dsl_pool_rele(dmu_objset_pool(os), FTAG);
5392 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
5398 zfs_ioc_share(zfs_cmd_t *zc)
5400 return (SET_ERROR(ENOSYS));
5403 ace_t full_access[] = {
5404 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5409 * zc_name name of containing filesystem
5410 * zc_obj object # beyond which we want next in-use object #
5413 * zc_obj next in-use object #
5416 zfs_ioc_next_obj(zfs_cmd_t *zc)
5418 objset_t *os = NULL;
5421 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5425 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
5427 dmu_objset_rele(os, FTAG);
5433 * zc_name name of filesystem
5434 * zc_value prefix name for snapshot
5435 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5438 * zc_value short name of new snapshot
5441 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5448 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5452 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5453 (u_longlong_t)ddi_get_lbolt64());
5454 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5456 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5459 (void) strlcpy(zc->zc_value, snap_name,
5460 sizeof (zc->zc_value));
5463 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5469 * zc_name name of "to" snapshot
5470 * zc_value name of "from" snapshot
5471 * zc_cookie file descriptor to write diff data on
5474 * dmu_diff_record_t's to the file descriptor
5477 zfs_ioc_diff(zfs_cmd_t *zc)
5483 fp = getf(zc->zc_cookie);
5485 return (SET_ERROR(EBADF));
5489 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5491 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5493 releasef(zc->zc_cookie);
5499 * Remove all ACL files in shares dir
5501 #ifdef HAVE_SMB_SHARE
5503 zfs_smb_acl_purge(znode_t *dzp)
5506 zap_attribute_t zap;
5507 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
5510 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5511 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5512 zap_cursor_advance(&zc)) {
5513 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5517 zap_cursor_fini(&zc);
5520 #endif /* HAVE_SMB_SHARE */
5523 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5525 #ifdef HAVE_SMB_SHARE
5528 vnode_t *resourcevp = NULL;
5537 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5538 NO_FOLLOW, NULL, &vp)) != 0)
5541 /* Now make sure mntpnt and dataset are ZFS */
5543 if (vp->v_vfsp->vfs_fstype != zfsfstype ||
5544 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5545 zc->zc_name) != 0)) {
5547 return (SET_ERROR(EINVAL));
5551 zfsvfs = ZTOZSB(dzp);
5555 * Create share dir if its missing.
5557 mutex_enter(&zfsvfs->z_lock);
5558 if (zfsvfs->z_shares_dir == 0) {
5561 tx = dmu_tx_create(zfsvfs->z_os);
5562 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5564 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5565 error = dmu_tx_assign(tx, TXG_WAIT);
5569 error = zfs_create_share_dir(zfsvfs, tx);
5573 mutex_exit(&zfsvfs->z_lock);
5579 mutex_exit(&zfsvfs->z_lock);
5581 ASSERT(zfsvfs->z_shares_dir);
5582 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5588 switch (zc->zc_cookie) {
5589 case ZFS_SMB_ACL_ADD:
5590 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5591 vattr.va_mode = S_IFREG|0777;
5595 vsec.vsa_mask = VSA_ACE;
5596 vsec.vsa_aclentp = &full_access;
5597 vsec.vsa_aclentsz = sizeof (full_access);
5598 vsec.vsa_aclcnt = 1;
5600 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5601 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5603 VN_RELE(resourcevp);
5606 case ZFS_SMB_ACL_REMOVE:
5607 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5611 case ZFS_SMB_ACL_RENAME:
5612 if ((error = get_nvlist(zc->zc_nvlist_src,
5613 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5615 VN_RELE(ZTOV(sharedir));
5619 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5620 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5623 VN_RELE(ZTOV(sharedir));
5625 nvlist_free(nvlist);
5628 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5630 nvlist_free(nvlist);
5633 case ZFS_SMB_ACL_PURGE:
5634 error = zfs_smb_acl_purge(sharedir);
5638 error = SET_ERROR(EINVAL);
5643 VN_RELE(ZTOV(sharedir));
5649 return (SET_ERROR(ENOTSUP));
5650 #endif /* HAVE_SMB_SHARE */
5655 * "holds" -> { snapname -> holdname (string), ... }
5656 * (optional) "cleanup_fd" -> fd (int32)
5660 * snapname -> error value (int32)
5666 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5670 int cleanup_fd = -1;
5674 error = nvlist_lookup_nvlist(args, "holds", &holds);
5676 return (SET_ERROR(EINVAL));
5678 /* make sure the user didn't pass us any invalid (empty) tags */
5679 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5680 pair = nvlist_next_nvpair(holds, pair)) {
5683 error = nvpair_value_string(pair, &htag);
5685 return (SET_ERROR(error));
5687 if (strlen(htag) == 0)
5688 return (SET_ERROR(EINVAL));
5691 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
5692 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
5697 error = dsl_dataset_user_hold(holds, minor, errlist);
5699 zfs_onexit_fd_rele(cleanup_fd);
5704 * innvl is not used.
5707 * holdname -> time added (uint64 seconds since epoch)
5713 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
5715 ASSERT3P(args, ==, NULL);
5716 return (dsl_dataset_get_holds(snapname, outnvl));
5721 * snapname -> { holdname, ... }
5726 * snapname -> error value (int32)
5732 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
5734 return (dsl_dataset_user_release(holds, errlist));
5739 * zc_guid flags (ZEVENT_NONBLOCK)
5740 * zc_cleanup_fd zevent file descriptor
5743 * zc_nvlist_dst next nvlist event
5744 * zc_cookie dropped events since last get
5747 zfs_ioc_events_next(zfs_cmd_t *zc)
5750 nvlist_t *event = NULL;
5752 uint64_t dropped = 0;
5755 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5760 error = zfs_zevent_next(ze, &event,
5761 &zc->zc_nvlist_dst_size, &dropped);
5762 if (event != NULL) {
5763 zc->zc_cookie = dropped;
5764 error = put_nvlist(zc, event);
5768 if (zc->zc_guid & ZEVENT_NONBLOCK)
5771 if ((error == 0) || (error != ENOENT))
5774 error = zfs_zevent_wait(ze);
5779 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5786 * zc_cookie cleared events count
5789 zfs_ioc_events_clear(zfs_cmd_t *zc)
5793 zfs_zevent_drain_all(&count);
5794 zc->zc_cookie = count;
5801 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
5802 * zc_cleanup zevent file descriptor
5805 zfs_ioc_events_seek(zfs_cmd_t *zc)
5811 error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
5815 error = zfs_zevent_seek(ze, zc->zc_guid);
5816 zfs_zevent_fd_rele(zc->zc_cleanup_fd);
5823 * zc_name name of new filesystem or snapshot
5824 * zc_value full name of old snapshot
5827 * zc_cookie space in bytes
5828 * zc_objset_type compressed space in bytes
5829 * zc_perm_action uncompressed space in bytes
5832 zfs_ioc_space_written(zfs_cmd_t *zc)
5836 dsl_dataset_t *new, *old;
5838 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5841 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
5843 dsl_pool_rele(dp, FTAG);
5846 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
5848 dsl_dataset_rele(new, FTAG);
5849 dsl_pool_rele(dp, FTAG);
5853 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
5854 &zc->zc_objset_type, &zc->zc_perm_action);
5855 dsl_dataset_rele(old, FTAG);
5856 dsl_dataset_rele(new, FTAG);
5857 dsl_pool_rele(dp, FTAG);
5863 * "firstsnap" -> snapshot name
5867 * "used" -> space in bytes
5868 * "compressed" -> compressed space in bytes
5869 * "uncompressed" -> uncompressed space in bytes
5873 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
5877 dsl_dataset_t *new, *old;
5879 uint64_t used, comp, uncomp;
5881 if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0)
5882 return (SET_ERROR(EINVAL));
5884 error = dsl_pool_hold(lastsnap, FTAG, &dp);
5888 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
5889 if (error == 0 && !new->ds_is_snapshot) {
5890 dsl_dataset_rele(new, FTAG);
5891 error = SET_ERROR(EINVAL);
5894 dsl_pool_rele(dp, FTAG);
5897 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
5898 if (error == 0 && !old->ds_is_snapshot) {
5899 dsl_dataset_rele(old, FTAG);
5900 error = SET_ERROR(EINVAL);
5903 dsl_dataset_rele(new, FTAG);
5904 dsl_pool_rele(dp, FTAG);
5908 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5909 dsl_dataset_rele(old, FTAG);
5910 dsl_dataset_rele(new, FTAG);
5911 dsl_pool_rele(dp, FTAG);
5912 fnvlist_add_uint64(outnvl, "used", used);
5913 fnvlist_add_uint64(outnvl, "compressed", comp);
5914 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5920 * "fd" -> file descriptor to write stream to (int32)
5921 * (optional) "fromsnap" -> full snap name to send an incremental from
5922 * (optional) "largeblockok" -> (value ignored)
5923 * indicates that blocks > 128KB are permitted
5924 * (optional) "embedok" -> (value ignored)
5925 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5926 * (optional) "compressok" -> (value ignored)
5927 * presence indicates compressed DRR_WRITE records are permitted
5928 * (optional) "rawok" -> (value ignored)
5929 * presence indicates raw encrypted records should be used.
5930 * (optional) "resume_object" and "resume_offset" -> (uint64)
5931 * if present, resume send stream from specified object and offset.
5938 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5942 char *fromname = NULL;
5945 boolean_t largeblockok;
5947 boolean_t compressok;
5949 uint64_t resumeobj = 0;
5950 uint64_t resumeoff = 0;
5952 error = nvlist_lookup_int32(innvl, "fd", &fd);
5954 return (SET_ERROR(EINVAL));
5956 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
5958 largeblockok = nvlist_exists(innvl, "largeblockok");
5959 embedok = nvlist_exists(innvl, "embedok");
5960 compressok = nvlist_exists(innvl, "compressok");
5961 rawok = nvlist_exists(innvl, "rawok");
5963 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
5964 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
5966 if ((fp = getf(fd)) == NULL)
5967 return (SET_ERROR(EBADF));
5970 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
5971 rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off);
5973 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5981 * Determine approximately how large a zfs send stream will be -- the number
5982 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
5985 * (optional) "from" -> full snap or bookmark name to send an incremental
5987 * (optional) "largeblockok" -> (value ignored)
5988 * indicates that blocks > 128KB are permitted
5989 * (optional) "embedok" -> (value ignored)
5990 * presence indicates DRR_WRITE_EMBEDDED records are permitted
5991 * (optional) "compressok" -> (value ignored)
5992 * presence indicates compressed DRR_WRITE records are permitted
5993 * (optional) "rawok" -> (value ignored)
5994 * presence indicates raw encrypted records should be used.
5998 * "space" -> bytes of space (uint64)
6002 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6005 dsl_dataset_t *tosnap;
6008 boolean_t compressok;
6012 error = dsl_pool_hold(snapname, FTAG, &dp);
6016 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6018 dsl_pool_rele(dp, FTAG);
6022 compressok = nvlist_exists(innvl, "compressok");
6023 rawok = nvlist_exists(innvl, "rawok");
6025 error = nvlist_lookup_string(innvl, "from", &fromname);
6027 if (strchr(fromname, '@') != NULL) {
6029 * If from is a snapshot, hold it and use the more
6030 * efficient dmu_send_estimate to estimate send space
6031 * size using deadlists.
6033 dsl_dataset_t *fromsnap;
6034 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6037 error = dmu_send_estimate(tosnap, fromsnap,
6038 compressok || rawok, &space);
6039 dsl_dataset_rele(fromsnap, FTAG);
6040 } else if (strchr(fromname, '#') != NULL) {
6042 * If from is a bookmark, fetch the creation TXG of the
6043 * snapshot it was created from and use that to find
6044 * blocks that were born after it.
6046 zfs_bookmark_phys_t frombm;
6048 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6052 error = dmu_send_estimate_from_txg(tosnap,
6053 frombm.zbm_creation_txg, compressok || rawok,
6057 * from is not properly formatted as a snapshot or
6060 error = SET_ERROR(EINVAL);
6065 * If estimating the size of a full send, use dmu_send_estimate.
6067 error = dmu_send_estimate(tosnap, NULL, compressok || rawok,
6071 fnvlist_add_uint64(outnvl, "space", space);
6074 dsl_dataset_rele(tosnap, FTAG);
6075 dsl_pool_rele(dp, FTAG);
6080 * Sync the currently open TXG to disk for the specified pool.
6081 * This is somewhat similar to 'zfs_sync()'.
6082 * For cases that do not result in error this ioctl will wait for
6083 * the currently open TXG to commit before returning back to the caller.
6086 * "force" -> when true, force uberblock update even if there is no dirty data.
6087 * In addition this will cause the vdev configuration to be written
6088 * out including updating the zpool cache file. (boolean_t)
6095 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6098 boolean_t force = B_FALSE;
6101 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6105 if (nvlist_lookup_boolean_value(innvl, "force", &force) != 0) {
6106 err = SET_ERROR(EINVAL);
6112 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6113 vdev_config_dirty(spa->spa_root_vdev);
6114 spa_config_exit(spa, SCL_CONFIG, FTAG);
6116 txg_wait_synced(spa_get_dsl(spa), 0);
6118 spa_close(spa, FTAG);
6124 * Load a user's wrapping key into the kernel.
6126 * "hidden_args" -> { "wkeydata" -> value }
6127 * raw uint8_t array of encryption wrapping key data (32 bytes)
6128 * (optional) "noop" -> (value ignored)
6129 * presence indicated key should only be verified, not loaded
6134 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6137 dsl_crypto_params_t *dcp = NULL;
6138 nvlist_t *hidden_args;
6139 boolean_t noop = nvlist_exists(innvl, "noop");
6141 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6142 ret = SET_ERROR(EINVAL);
6146 ret = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6148 ret = SET_ERROR(EINVAL);
6152 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6157 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6161 dsl_crypto_params_free(dcp, noop);
6166 dsl_crypto_params_free(dcp, B_TRUE);
6171 * Unload a user's wrapping key from the kernel.
6172 * Both innvl and outnvl are unused.
6176 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6180 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6181 ret = (SET_ERROR(EINVAL));
6185 ret = spa_keystore_unload_wkey(dsname);
6194 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6195 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6196 * here to change how the key is derived in userspace.
6199 * "hidden_args" (optional) -> { "wkeydata" -> value }
6200 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6201 * "props" (optional) -> { prop -> value }
6208 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6211 uint64_t cmd = DCP_CMD_NONE;
6212 dsl_crypto_params_t *dcp = NULL;
6213 nvlist_t *args = NULL, *hidden_args = NULL;
6215 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6216 ret = (SET_ERROR(EINVAL));
6220 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6221 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6222 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6224 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6228 ret = spa_keystore_change_key(dsname, dcp);
6232 dsl_crypto_params_free(dcp, B_FALSE);
6237 dsl_crypto_params_free(dcp, B_TRUE);
6241 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6244 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6245 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6246 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6248 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6250 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6251 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6252 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6253 ASSERT3P(vec->zvec_func, ==, NULL);
6255 vec->zvec_legacy_func = func;
6256 vec->zvec_secpolicy = secpolicy;
6257 vec->zvec_namecheck = namecheck;
6258 vec->zvec_allow_log = log_history;
6259 vec->zvec_pool_check = pool_check;
6263 * See the block comment at the beginning of this file for details on
6264 * each argument to this function.
6267 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6268 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6269 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6270 boolean_t allow_log)
6272 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6274 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6275 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6276 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6277 ASSERT3P(vec->zvec_func, ==, NULL);
6279 /* if we are logging, the name must be valid */
6280 ASSERT(!allow_log || namecheck != NO_NAME);
6282 vec->zvec_name = name;
6283 vec->zvec_func = func;
6284 vec->zvec_secpolicy = secpolicy;
6285 vec->zvec_namecheck = namecheck;
6286 vec->zvec_pool_check = pool_check;
6287 vec->zvec_smush_outnvlist = smush_outnvlist;
6288 vec->zvec_allow_log = allow_log;
6292 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6293 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6294 zfs_ioc_poolcheck_t pool_check)
6296 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6297 POOL_NAME, log_history, pool_check);
6301 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6302 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6304 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6305 DATASET_NAME, B_FALSE, pool_check);
6309 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6311 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6312 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6316 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6317 zfs_secpolicy_func_t *secpolicy)
6319 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6320 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6324 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6325 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6327 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6328 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6332 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6334 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6335 zfs_secpolicy_read);
6339 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6340 zfs_secpolicy_func_t *secpolicy)
6342 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6343 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6347 zfs_ioctl_init(void)
6349 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6350 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6351 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6353 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6354 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6355 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
6357 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6358 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6359 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6361 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6362 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6363 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6365 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6366 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6367 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6369 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6370 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6371 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6373 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6374 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6375 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6377 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6378 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6379 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6381 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6382 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6383 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6385 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6386 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6387 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6388 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6389 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6390 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6392 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6393 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6394 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6396 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6397 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6398 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
6400 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6401 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6402 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6404 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6405 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6406 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
6408 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6409 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6411 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6413 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
6414 zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME,
6415 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6416 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
6417 zfs_ioc_load_key, zfs_secpolicy_load_key,
6418 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
6419 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
6420 zfs_ioc_unload_key, zfs_secpolicy_load_key,
6421 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE);
6422 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
6423 zfs_ioc_change_key, zfs_secpolicy_change_key,
6424 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
6427 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6428 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6429 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
6430 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6431 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
6434 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6435 zfs_ioc_channel_program, zfs_secpolicy_config,
6436 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6439 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6440 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6441 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6443 zfs_ioctl_register("zpool_discard_checkpoint",
6444 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6445 zfs_secpolicy_config, POOL_NAME,
6446 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
6448 /* IOCTLS that use the legacy function signature */
6450 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6451 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6453 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6454 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6455 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6457 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6458 zfs_ioc_pool_upgrade);
6459 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6461 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6462 zfs_ioc_vdev_remove);
6463 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6464 zfs_ioc_vdev_set_state);
6465 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6466 zfs_ioc_vdev_attach);
6467 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6468 zfs_ioc_vdev_detach);
6469 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6470 zfs_ioc_vdev_setpath);
6471 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6472 zfs_ioc_vdev_setfru);
6473 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6474 zfs_ioc_pool_set_props);
6475 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6476 zfs_ioc_vdev_split);
6477 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6478 zfs_ioc_pool_reguid);
6480 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6481 zfs_ioc_pool_configs, zfs_secpolicy_none);
6482 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6483 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6484 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6485 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6486 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6487 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6488 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6489 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6492 * pool destroy, and export don't log the history as part of
6493 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6494 * does the logging of those commands.
6496 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6497 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6498 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6499 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6501 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6502 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6503 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6504 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6506 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6507 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
6508 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6509 zfs_ioc_dsobj_to_dsname,
6510 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
6511 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6512 zfs_ioc_pool_get_history,
6513 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6515 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6516 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6518 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6519 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6521 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6522 zfs_ioc_space_written);
6523 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6524 zfs_ioc_objset_recvd_props);
6525 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6527 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6529 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6530 zfs_ioc_objset_stats);
6531 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6532 zfs_ioc_objset_zplprops);
6533 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6534 zfs_ioc_dataset_list_next);
6535 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6536 zfs_ioc_snapshot_list_next);
6537 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6538 zfs_ioc_send_progress);
6540 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6541 zfs_ioc_diff, zfs_secpolicy_diff);
6542 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6543 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6544 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6545 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6546 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6547 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6548 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6549 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6550 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6551 zfs_ioc_send, zfs_secpolicy_send);
6553 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6554 zfs_secpolicy_none);
6555 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6556 zfs_secpolicy_destroy);
6557 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
6558 zfs_secpolicy_rename);
6559 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6560 zfs_secpolicy_recv);
6561 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6562 zfs_secpolicy_promote);
6563 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6564 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6565 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6566 zfs_secpolicy_set_fsacl);
6568 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6569 zfs_secpolicy_share, POOL_CHECK_NONE);
6570 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6571 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6572 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6573 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6574 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6575 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6576 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6577 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6582 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
6583 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6584 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
6585 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6586 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
6587 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
6591 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6592 zfs_ioc_poolcheck_t check)
6597 ASSERT(type == POOL_NAME || type == DATASET_NAME);
6599 if (check & POOL_CHECK_NONE)
6602 error = spa_open(name, &spa, FTAG);
6604 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6605 error = SET_ERROR(EAGAIN);
6606 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6607 error = SET_ERROR(EROFS);
6608 spa_close(spa, FTAG);
6614 zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which)
6618 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6619 if (zs->zs_minor == minor) {
6623 return (zs->zs_onexit);
6625 return (zs->zs_zevent);
6636 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
6640 ptr = zfsdev_get_state_impl(minor, which);
6646 zfsdev_getminor(struct file *filp, minor_t *minorp)
6648 zfsdev_state_t *zs, *fpd;
6650 ASSERT(filp != NULL);
6651 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
6653 fpd = filp->private_data;
6655 return (SET_ERROR(EBADF));
6657 mutex_enter(&zfsdev_state_lock);
6659 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6661 if (zs->zs_minor == -1)
6665 *minorp = fpd->zs_minor;
6666 mutex_exit(&zfsdev_state_lock);
6671 mutex_exit(&zfsdev_state_lock);
6673 return (SET_ERROR(EBADF));
6677 * Find a free minor number. The zfsdev_state_list is expected to
6678 * be short since it is only a list of currently open file handles.
6681 zfsdev_minor_alloc(void)
6683 static minor_t last_minor = 0;
6686 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6688 for (m = last_minor + 1; m != last_minor; m++) {
6689 if (m > ZFSDEV_MAX_MINOR)
6691 if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) {
6701 zfsdev_state_init(struct file *filp)
6703 zfsdev_state_t *zs, *zsprev = NULL;
6705 boolean_t newzs = B_FALSE;
6707 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6709 minor = zfsdev_minor_alloc();
6711 return (SET_ERROR(ENXIO));
6713 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
6714 if (zs->zs_minor == -1)
6720 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
6725 filp->private_data = zs;
6727 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
6728 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
6732 * In order to provide for lock-free concurrent read access
6733 * to the minor list in zfsdev_get_state_impl(), new entries
6734 * must be completely written before linking them into the
6735 * list whereas existing entries are already linked; the last
6736 * operation must be updating zs_minor (from -1 to the new
6740 zs->zs_minor = minor;
6742 zsprev->zs_next = zs;
6745 zs->zs_minor = minor;
6752 zfsdev_state_destroy(struct file *filp)
6756 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
6757 ASSERT(filp->private_data != NULL);
6759 zs = filp->private_data;
6761 zfs_onexit_destroy(zs->zs_onexit);
6762 zfs_zevent_destroy(zs->zs_zevent);
6768 zfsdev_open(struct inode *ino, struct file *filp)
6772 mutex_enter(&zfsdev_state_lock);
6773 error = zfsdev_state_init(filp);
6774 mutex_exit(&zfsdev_state_lock);
6780 zfsdev_release(struct inode *ino, struct file *filp)
6784 mutex_enter(&zfsdev_state_lock);
6785 error = zfsdev_state_destroy(filp);
6786 mutex_exit(&zfsdev_state_lock);
6792 zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
6796 int error, rc, flag = 0;
6797 const zfs_ioc_vec_t *vec;
6798 char *saved_poolname = NULL;
6799 nvlist_t *innvl = NULL;
6800 fstrans_cookie_t cookie;
6802 vecnum = cmd - ZFS_IOC_FIRST;
6803 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
6804 return (-SET_ERROR(EINVAL));
6805 vec = &zfs_ioc_vec[vecnum];
6808 * The registered ioctl list may be sparse, verify that either
6809 * a normal or legacy handler are registered.
6811 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
6812 return (-SET_ERROR(EINVAL));
6814 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
6816 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
6818 error = SET_ERROR(EFAULT);
6822 zc->zc_iflags = flag & FKIOCTL;
6823 if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) {
6825 * Make sure the user doesn't pass in an insane value for
6826 * zc_nvlist_src_size. We have to check, since we will end
6827 * up allocating that much memory inside of get_nvlist(). This
6828 * prevents a nefarious user from allocating tons of kernel
6831 * Also, we return EINVAL instead of ENOMEM here. The reason
6832 * being that returning ENOMEM from an ioctl() has a special
6833 * connotation; that the user's size value is too small and
6834 * needs to be expanded to hold the nvlist. See
6835 * zcmd_expand_dst_nvlist() for details.
6837 error = SET_ERROR(EINVAL); /* User's size too big */
6839 } else if (zc->zc_nvlist_src_size != 0) {
6840 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6841 zc->zc_iflags, &innvl);
6847 * Ensure that all pool/dataset names are valid before we pass down to
6850 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
6851 switch (vec->zvec_namecheck) {
6853 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
6854 error = SET_ERROR(EINVAL);
6856 error = pool_status_check(zc->zc_name,
6857 vec->zvec_namecheck, vec->zvec_pool_check);
6861 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
6862 error = SET_ERROR(EINVAL);
6864 error = pool_status_check(zc->zc_name,
6865 vec->zvec_namecheck, vec->zvec_pool_check);
6874 cookie = spl_fstrans_mark();
6875 error = vec->zvec_secpolicy(zc, innvl, CRED());
6876 spl_fstrans_unmark(cookie);
6882 /* legacy ioctls can modify zc_name */
6883 saved_poolname = strdup(zc->zc_name);
6884 if (saved_poolname == NULL) {
6885 error = SET_ERROR(ENOMEM);
6888 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
6891 if (vec->zvec_func != NULL) {
6895 nvlist_t *lognv = NULL;
6897 ASSERT(vec->zvec_legacy_func == NULL);
6900 * Add the innvl to the lognv before calling the func,
6901 * in case the func changes the innvl.
6903 if (vec->zvec_allow_log) {
6904 lognv = fnvlist_alloc();
6905 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
6907 if (!nvlist_empty(innvl)) {
6908 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
6913 outnvl = fnvlist_alloc();
6914 cookie = spl_fstrans_mark();
6915 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
6916 spl_fstrans_unmark(cookie);
6919 * Some commands can partially execute, modify state, and still
6920 * return an error. In these cases, attempt to record what
6924 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
6925 vec->zvec_allow_log &&
6926 spa_open(zc->zc_name, &spa, FTAG) == 0) {
6927 if (!nvlist_empty(outnvl)) {
6928 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
6932 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
6935 (void) spa_history_log_nvl(spa, lognv);
6936 spa_close(spa, FTAG);
6938 fnvlist_free(lognv);
6940 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
6942 if (vec->zvec_smush_outnvlist) {
6943 smusherror = nvlist_smush(outnvl,
6944 zc->zc_nvlist_dst_size);
6946 if (smusherror == 0)
6947 puterror = put_nvlist(zc, outnvl);
6953 nvlist_free(outnvl);
6955 cookie = spl_fstrans_mark();
6956 error = vec->zvec_legacy_func(zc);
6957 spl_fstrans_unmark(cookie);
6962 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
6963 if (error == 0 && rc != 0)
6964 error = SET_ERROR(EFAULT);
6965 if (error == 0 && vec->zvec_allow_log) {
6966 char *s = tsd_get(zfs_allow_log_key);
6969 (void) tsd_set(zfs_allow_log_key, saved_poolname);
6971 if (saved_poolname != NULL)
6972 strfree(saved_poolname);
6975 kmem_free(zc, sizeof (zfs_cmd_t));
6979 #ifdef CONFIG_COMPAT
6981 zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg)
6983 return (zfsdev_ioctl(filp, cmd, arg));
6986 #define zfsdev_compat_ioctl NULL
6989 static const struct file_operations zfsdev_fops = {
6990 .open = zfsdev_open,
6991 .release = zfsdev_release,
6992 .unlocked_ioctl = zfsdev_ioctl,
6993 .compat_ioctl = zfsdev_compat_ioctl,
6994 .owner = THIS_MODULE,
6997 static struct miscdevice zfs_misc = {
7000 .fops = &zfsdev_fops,
7003 MODULE_ALIAS_MISCDEV(ZFS_MINOR);
7004 MODULE_ALIAS("devname:zfs");
7011 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7012 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7013 zfsdev_state_list->zs_minor = -1;
7015 error = misc_register(&zfs_misc);
7016 if (error == -EBUSY) {
7018 * Fallback to dynamic minor allocation in the event of a
7019 * collision with a reserved minor in linux/miscdevice.h.
7020 * In this case the kernel modules must be manually loaded.
7022 printk(KERN_INFO "ZFS: misc_register() with static minor %d "
7023 "failed %d, retrying with MISC_DYNAMIC_MINOR\n",
7026 zfs_misc.minor = MISC_DYNAMIC_MINOR;
7027 error = misc_register(&zfs_misc);
7031 printk(KERN_INFO "ZFS: misc_register() failed %d\n", error);
7039 zfsdev_state_t *zs, *zsprev = NULL;
7041 misc_deregister(&zfs_misc);
7042 mutex_destroy(&zfsdev_state_lock);
7044 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7046 kmem_free(zsprev, sizeof (zfsdev_state_t));
7050 kmem_free(zsprev, sizeof (zfsdev_state_t));
7054 zfs_allow_log_destroy(void *arg)
7056 char *poolname = arg;
7058 if (poolname != NULL)
7063 #define ZFS_DEBUG_STR " (DEBUG mode)"
7065 #define ZFS_DEBUG_STR ""
7073 error = -vn_set_pwd("/");
7076 "ZFS: Warning unable to set pwd to '/': %d\n", error);
7080 if ((error = -zvol_init()) != 0)
7083 spa_init(FREAD | FWRITE);
7089 if ((error = zfs_attach()) != 0)
7092 tsd_create(&zfs_fsyncer_key, NULL);
7093 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7094 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7096 printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, "
7097 "ZFS pool version %s, ZFS filesystem version %s\n",
7098 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR,
7099 SPA_VERSION_STRING, ZPL_VERSION_STRING);
7100 #ifndef CONFIG_FS_POSIX_ACL
7101 printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n");
7102 #endif /* CONFIG_FS_POSIX_ACL */
7111 printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s"
7112 ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE,
7113 ZFS_DEBUG_STR, error);
7127 tsd_destroy(&zfs_fsyncer_key);
7128 tsd_destroy(&rrw_tsd_key);
7129 tsd_destroy(&zfs_allow_log_key);
7131 printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n",
7132 ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR);
7135 #if defined(_KERNEL)
7139 MODULE_DESCRIPTION("ZFS");
7140 MODULE_AUTHOR(ZFS_META_AUTHOR);
7141 MODULE_LICENSE(ZFS_META_LICENSE);
7142 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);