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 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
25 * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26 * Copyright 2014 Xin Li <delphij@FreeBSD.org>. All rights reserved.
27 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
28 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright 2017 RackTop Systems.
36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright (c) 2019 Datto Inc.
43 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
44 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
46 * There are two ways that we handle ioctls: the legacy way where almost
47 * all of the logic is in the ioctl callback, and the new way where most
48 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
50 * Non-legacy ioctls should be registered by calling
51 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
52 * from userland by lzc_ioctl().
54 * The registration arguments are as follows:
57 * The name of the ioctl. This is used for history logging. If the
58 * ioctl returns successfully (the callback returns 0), and allow_log
59 * is true, then a history log entry will be recorded with the input &
60 * output nvlists. The log entry can be printed with "zpool history -i".
63 * The ioctl request number, which userland will pass to ioctl(2).
64 * We want newer versions of libzfs and libzfs_core to run against
65 * existing zfs kernel modules (i.e. a deferred reboot after an update).
66 * Therefore the ioctl numbers cannot change from release to release.
68 * zfs_secpolicy_func_t *secpolicy
69 * This function will be called before the zfs_ioc_func_t, to
70 * determine if this operation is permitted. It should return EPERM
71 * on failure, and 0 on success. Checks include determining if the
72 * dataset is visible in this zone, and if the user has either all
73 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
74 * to do this operation on this dataset with "zfs allow".
76 * zfs_ioc_namecheck_t namecheck
77 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
78 * name, a dataset name, or nothing. If the name is not well-formed,
79 * the ioctl will fail and the callback will not be called.
80 * Therefore, the callback can assume that the name is well-formed
81 * (e.g. is null-terminated, doesn't have more than one '@' character,
82 * doesn't have invalid characters).
84 * zfs_ioc_poolcheck_t pool_check
85 * This specifies requirements on the pool state. If the pool does
86 * not meet them (is suspended or is readonly), the ioctl will fail
87 * and the callback will not be called. If any checks are specified
88 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
89 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
90 * POOL_CHECK_READONLY).
92 * zfs_ioc_key_t *nvl_keys
93 * The list of expected/allowable innvl input keys. This list is used
94 * to validate the nvlist input to the ioctl.
96 * boolean_t smush_outnvlist
97 * If smush_outnvlist is true, then the output is presumed to be a
98 * list of errors, and it will be "smushed" down to fit into the
99 * caller's buffer, by removing some entries and replacing them with a
100 * single "N_MORE_ERRORS" entry indicating how many were removed. See
101 * nvlist_smush() for details. If smush_outnvlist is false, and the
102 * outnvlist does not fit into the userland-provided buffer, then the
103 * ioctl will fail with ENOMEM.
105 * zfs_ioc_func_t *func
106 * The callback function that will perform the operation.
108 * The callback should return 0 on success, or an error number on
109 * failure. If the function fails, the userland ioctl will return -1,
110 * and errno will be set to the callback's return value. The callback
111 * will be called with the following arguments:
114 * The name of the pool or dataset to operate on, from
115 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
116 * expected type (pool, dataset, or none).
119 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
120 * NULL if no input nvlist was provided. Changes to this nvlist are
121 * ignored. If the input nvlist could not be deserialized, the
122 * ioctl will fail and the callback will not be called.
125 * The output nvlist, initially empty. The callback can fill it in,
126 * and it will be returned to userland by serializing it into
127 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
128 * fails (e.g. because the caller didn't supply a large enough
129 * buffer), then the overall ioctl will fail. See the
130 * 'smush_nvlist' argument above for additional behaviors.
132 * There are two typical uses of the output nvlist:
133 * - To return state, e.g. property values. In this case,
134 * smush_outnvlist should be false. If the buffer was not large
135 * enough, the caller will reallocate a larger buffer and try
138 * - To return multiple errors from an ioctl which makes on-disk
139 * changes. In this case, smush_outnvlist should be true.
140 * Ioctls which make on-disk modifications should generally not
141 * use the outnvl if they succeed, because the caller can not
142 * distinguish between the operation failing, and
143 * deserialization failing.
146 * IOCTL Interface Errors
148 * The following ioctl input errors can be returned:
149 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
150 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
151 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
152 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
156 #include "opt_kstack_pages.h"
159 #include <sys/types.h>
160 #include <sys/param.h>
161 #include <sys/systm.h>
162 #include <sys/conf.h>
163 #include <sys/kernel.h>
164 #include <sys/lock.h>
165 #include <sys/malloc.h>
166 #include <sys/mutex.h>
167 #include <sys/proc.h>
168 #include <sys/errno.h>
171 #include <sys/file.h>
172 #include <sys/kmem.h>
173 #include <sys/conf.h>
174 #include <sys/cmn_err.h>
175 #include <sys/stat.h>
176 #include <sys/zfs_ioctl.h>
177 #include <sys/zfs_vfsops.h>
178 #include <sys/zfs_znode.h>
181 #include <sys/spa_impl.h>
182 #include <sys/vdev.h>
184 #include <sys/dsl_dir.h>
185 #include <sys/dsl_dataset.h>
186 #include <sys/dsl_prop.h>
187 #include <sys/dsl_deleg.h>
188 #include <sys/dmu_objset.h>
189 #include <sys/dmu_impl.h>
190 #include <sys/dmu_tx.h>
191 #include <sys/sunddi.h>
192 #include <sys/policy.h>
193 #include <sys/zone.h>
194 #include <sys/nvpair.h>
195 #include <sys/mount.h>
196 #include <sys/taskqueue.h>
198 #include <sys/varargs.h>
199 #include <sys/fs/zfs.h>
200 #include <sys/zfs_ctldir.h>
201 #include <sys/zfs_dir.h>
202 #include <sys/zfs_onexit.h>
203 #include <sys/zvol.h>
204 #include <sys/dsl_scan.h>
205 #include <sys/dmu_objset.h>
206 #include <sys/dmu_send.h>
207 #include <sys/dsl_destroy.h>
208 #include <sys/dsl_bookmark.h>
209 #include <sys/dsl_userhold.h>
210 #include <sys/zfeature.h>
212 #include <sys/zio_checksum.h>
213 #include <sys/vdev_removal.h>
214 #include <sys/vdev_impl.h>
215 #include <sys/vdev_initialize.h>
217 #include "zfs_namecheck.h"
218 #include "zfs_prop.h"
219 #include "zfs_deleg.h"
220 #include "zfs_comutil.h"
221 #include "zfs_ioctl_compat.h"
227 #define ARRAY_SIZE(x) nitems(x)
230 static struct cdev *zfsdev;
232 extern void zfs_init(void);
233 extern void zfs_fini(void);
235 uint_t zfs_fsyncer_key;
236 extern uint_t rrw_tsd_key;
237 static uint_t zfs_allow_log_key;
238 extern uint_t zfs_geom_probe_vdev_key;
240 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
241 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
242 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
245 * IOC Keys are used to document and validate user->kernel interface inputs.
246 * See zfs_keys_recv_new for an example declaration. Any key name that is not
247 * listed will be rejected as input.
249 * The keyname 'optional' is always allowed, and must be an nvlist if present.
250 * Arguments which older kernels can safely ignore can be placed under the
253 * When adding new keys to an existing ioc for new functionality, consider:
254 * - adding an entry into zfs_sysfs.c zfs_features[] list
255 * - updating the libzfs_input_check.c test utility
257 * Note: in the ZK_WILDCARDLIST case, the name serves as documentation
258 * for the expected name (bookmark, snapshot, property, etc) but there
259 * is no validation in the preflight zfs_check_input_nvpairs() check.
262 ZK_OPTIONAL = 1 << 0, /* pair is optional */
263 ZK_WILDCARDLIST = 1 << 1, /* one or more unspecified key names */
266 /* DATA_TYPE_ANY is used when zkey_type can vary. */
267 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
269 typedef struct zfs_ioc_key {
270 const char *zkey_name;
271 data_type_t zkey_type;
272 ioc_key_flag_t zkey_flags;
280 } zfs_ioc_namecheck_t;
283 POOL_CHECK_NONE = 1 << 0,
284 POOL_CHECK_SUSPENDED = 1 << 1,
285 POOL_CHECK_READONLY = 1 << 2,
286 } zfs_ioc_poolcheck_t;
288 typedef struct zfs_ioc_vec {
289 zfs_ioc_legacy_func_t *zvec_legacy_func;
290 zfs_ioc_func_t *zvec_func;
291 zfs_secpolicy_func_t *zvec_secpolicy;
292 zfs_ioc_namecheck_t zvec_namecheck;
293 boolean_t zvec_allow_log;
294 zfs_ioc_poolcheck_t zvec_pool_check;
295 boolean_t zvec_smush_outnvlist;
296 const char *zvec_name;
297 const zfs_ioc_key_t *zvec_nvl_keys;
298 size_t zvec_nvl_key_count;
301 /* This array is indexed by zfs_userquota_prop_t */
302 static const char *userquota_perms[] = {
303 ZFS_DELEG_PERM_USERUSED,
304 ZFS_DELEG_PERM_USERQUOTA,
305 ZFS_DELEG_PERM_GROUPUSED,
306 ZFS_DELEG_PERM_GROUPQUOTA,
309 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
310 static int zfs_check_settable(const char *name, nvpair_t *property,
312 static int zfs_check_clearable(char *dataset, nvlist_t *props,
314 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
316 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
317 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
319 static void zfsdev_close(void *data);
321 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
323 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
325 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
332 * Get rid of annoying "../common/" prefix to filename.
334 newfile = strrchr(file, '/');
335 if (newfile != NULL) {
336 newfile = newfile + 1; /* Get rid of leading / */
342 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
346 * To get this data, use the zfs-dprintf probe as so:
347 * dtrace -q -n 'zfs-dprintf \
348 * /stringof(arg0) == "dbuf.c"/ \
349 * {printf("%s: %s", stringof(arg1), stringof(arg3))}'
351 * arg1 = function name
355 DTRACE_PROBE4(zfs__dprintf,
356 char *, newfile, char *, func, int, line, char *, buf);
360 history_str_free(char *buf)
362 kmem_free(buf, HIS_MAX_RECORD_LEN);
366 history_str_get(zfs_cmd_t *zc)
370 if (zc->zc_history == 0)
373 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
374 if (copyinstr((void *)(uintptr_t)zc->zc_history,
375 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
376 history_str_free(buf);
380 buf[HIS_MAX_RECORD_LEN -1] = '\0';
386 * Check to see if the named dataset is currently defined as bootable
389 zfs_is_bootfs(const char *name)
393 if (dmu_objset_hold(name, FTAG, &os) == 0) {
395 ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os)));
396 dmu_objset_rele(os, FTAG);
403 * Return non-zero if the spa version is less than requested version.
406 zfs_earlier_version(const char *name, int version)
410 if (spa_open(name, &spa, FTAG) == 0) {
411 if (spa_version(spa) < version) {
412 spa_close(spa, FTAG);
415 spa_close(spa, FTAG);
421 * Return TRUE if the ZPL version is less than requested version.
424 zpl_earlier_version(const char *name, int version)
427 boolean_t rc = B_TRUE;
429 if (dmu_objset_hold(name, FTAG, &os) == 0) {
432 if (dmu_objset_type(os) != DMU_OST_ZFS) {
433 dmu_objset_rele(os, FTAG);
436 /* XXX reading from non-owned objset */
437 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
438 rc = zplversion < version;
439 dmu_objset_rele(os, FTAG);
445 zfs_log_history(zfs_cmd_t *zc)
450 if ((buf = history_str_get(zc)) == NULL)
453 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
454 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
455 (void) spa_history_log(spa, buf);
456 spa_close(spa, FTAG);
458 history_str_free(buf);
462 * Policy for top-level read operations (list pools). Requires no privileges,
463 * and can be used in the local zone, as there is no associated dataset.
467 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
473 * Policy for dataset read operations (list children, get statistics). Requires
474 * no privileges, but must be visible in the local zone.
478 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
480 if (INGLOBALZONE(curthread) ||
481 zone_dataset_visible(zc->zc_name, NULL))
484 return (SET_ERROR(ENOENT));
488 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
493 * The dataset must be visible by this zone -- check this first
494 * so they don't see EPERM on something they shouldn't know about.
496 if (!INGLOBALZONE(curthread) &&
497 !zone_dataset_visible(dataset, &writable))
498 return (SET_ERROR(ENOENT));
500 if (INGLOBALZONE(curthread)) {
502 * If the fs is zoned, only root can access it from the
505 if (secpolicy_zfs(cr) && zoned)
506 return (SET_ERROR(EPERM));
509 * If we are in a local zone, the 'zoned' property must be set.
512 return (SET_ERROR(EPERM));
514 /* must be writable by this zone */
516 return (SET_ERROR(EPERM));
522 zfs_dozonecheck(const char *dataset, cred_t *cr)
526 if (dsl_prop_get_integer(dataset, "jailed", &zoned, NULL))
527 return (SET_ERROR(ENOENT));
529 return (zfs_dozonecheck_impl(dataset, zoned, cr));
533 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
537 if (dsl_prop_get_int_ds(ds, "jailed", &zoned))
538 return (SET_ERROR(ENOENT));
540 return (zfs_dozonecheck_impl(dataset, zoned, cr));
544 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
545 const char *perm, cred_t *cr)
549 error = zfs_dozonecheck_ds(name, ds, cr);
551 error = secpolicy_zfs(cr);
553 error = dsl_deleg_access_impl(ds, perm, cr);
559 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
566 * First do a quick check for root in the global zone, which
567 * is allowed to do all write_perms. This ensures that zfs_ioc_*
568 * will get to handle nonexistent datasets.
570 if (INGLOBALZONE(curthread) && secpolicy_zfs(cr) == 0)
573 error = dsl_pool_hold(name, FTAG, &dp);
577 error = dsl_dataset_hold(dp, name, FTAG, &ds);
579 dsl_pool_rele(dp, FTAG);
583 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
585 dsl_dataset_rele(ds, FTAG);
586 dsl_pool_rele(dp, FTAG);
592 * Policy for setting the security label property.
594 * Returns 0 for success, non-zero for access and other errors.
597 zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr)
599 char ds_hexsl[MAXNAMELEN];
600 bslabel_t ds_sl, new_sl;
601 boolean_t new_default = FALSE;
603 int needed_priv = -1;
606 /* First get the existing dataset label. */
607 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
608 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
610 return (SET_ERROR(EPERM));
612 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
615 /* The label must be translatable */
616 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
617 return (SET_ERROR(EINVAL));
620 * In a non-global zone, disallow attempts to set a label that
621 * doesn't match that of the zone; otherwise no other checks
624 if (!INGLOBALZONE(curproc)) {
625 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
626 return (SET_ERROR(EPERM));
631 * For global-zone datasets (i.e., those whose zoned property is
632 * "off", verify that the specified new label is valid for the
635 if (dsl_prop_get_integer(name,
636 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
637 return (SET_ERROR(EPERM));
639 if (zfs_check_global_label(name, strval) != 0)
640 return (SET_ERROR(EPERM));
644 * If the existing dataset label is nondefault, check if the
645 * dataset is mounted (label cannot be changed while mounted).
646 * Get the zfsvfs; if there isn't one, then the dataset isn't
647 * mounted (or isn't a dataset, doesn't exist, ...).
649 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
651 static char *setsl_tag = "setsl_tag";
654 * Try to own the dataset; abort if there is any error,
655 * (e.g., already mounted, in use, or other error).
657 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE,
660 return (SET_ERROR(EPERM));
662 dmu_objset_disown(os, setsl_tag);
665 needed_priv = PRIV_FILE_DOWNGRADE_SL;
669 if (hexstr_to_label(strval, &new_sl) != 0)
670 return (SET_ERROR(EPERM));
672 if (blstrictdom(&ds_sl, &new_sl))
673 needed_priv = PRIV_FILE_DOWNGRADE_SL;
674 else if (blstrictdom(&new_sl, &ds_sl))
675 needed_priv = PRIV_FILE_UPGRADE_SL;
677 /* dataset currently has a default label */
679 needed_priv = PRIV_FILE_UPGRADE_SL;
683 if (needed_priv != -1)
684 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
687 #endif /* SECLABEL */
690 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
696 * Check permissions for special properties.
701 * Disallow setting of 'zoned' from within a local zone.
703 if (!INGLOBALZONE(curthread))
704 return (SET_ERROR(EPERM));
708 case ZFS_PROP_FILESYSTEM_LIMIT:
709 case ZFS_PROP_SNAPSHOT_LIMIT:
710 if (!INGLOBALZONE(curthread)) {
712 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
714 * Unprivileged users are allowed to modify the
715 * limit on things *under* (ie. contained by)
716 * the thing they own.
718 if (dsl_prop_get_integer(dsname, "jailed", &zoned,
720 return (SET_ERROR(EPERM));
721 if (!zoned || strlen(dsname) <= strlen(setpoint))
722 return (SET_ERROR(EPERM));
726 case ZFS_PROP_MLSLABEL:
728 if (!is_system_labeled())
729 return (SET_ERROR(EPERM));
731 if (nvpair_value_string(propval, &strval) == 0) {
734 err = zfs_set_slabel_policy(dsname, strval, CRED());
744 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
749 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
753 error = zfs_dozonecheck(zc->zc_name, cr);
758 * permission to set permissions will be evaluated later in
759 * dsl_deleg_can_allow()
766 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
768 return (zfs_secpolicy_write_perms(zc->zc_name,
769 ZFS_DELEG_PERM_ROLLBACK, cr));
774 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
782 * Generate the current snapshot name from the given objsetid, then
783 * use that name for the secpolicy/zone checks.
785 cp = strchr(zc->zc_name, '@');
787 return (SET_ERROR(EINVAL));
788 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
792 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
794 dsl_pool_rele(dp, FTAG);
798 dsl_dataset_name(ds, zc->zc_name);
800 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
801 ZFS_DELEG_PERM_SEND, cr);
802 dsl_dataset_rele(ds, FTAG);
803 dsl_pool_rele(dp, FTAG);
810 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
812 return (zfs_secpolicy_write_perms(zc->zc_name,
813 ZFS_DELEG_PERM_SEND, cr));
818 zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
823 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
824 NO_FOLLOW, NULL, &vp)) != 0)
827 /* Now make sure mntpnt and dataset are ZFS */
829 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
830 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
831 zc->zc_name) != 0)) {
833 return (SET_ERROR(EPERM));
837 return (dsl_deleg_access(zc->zc_name,
838 ZFS_DELEG_PERM_SHARE, cr));
842 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
844 if (!INGLOBALZONE(curthread))
845 return (SET_ERROR(EPERM));
847 if (secpolicy_nfs(cr) == 0) {
850 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
855 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
857 if (!INGLOBALZONE(curthread))
858 return (SET_ERROR(EPERM));
860 if (secpolicy_smb(cr) == 0) {
863 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
868 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
873 * Remove the @bla or /bla from the end of the name to get the parent.
875 (void) strncpy(parent, datasetname, parentsize);
876 cp = strrchr(parent, '@');
880 cp = strrchr(parent, '/');
882 return (SET_ERROR(ENOENT));
890 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
894 if ((error = zfs_secpolicy_write_perms(name,
895 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
898 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
903 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
905 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
909 * Destroying snapshots with delegated permissions requires
910 * descendant mount and destroy permissions.
914 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
917 nvpair_t *pair, *nextpair;
920 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
922 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
924 nextpair = nvlist_next_nvpair(snaps, pair);
925 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
926 if (error == ENOENT) {
928 * Ignore any snapshots that don't exist (we consider
929 * them "already destroyed"). Remove the name from the
930 * nvl here in case the snapshot is created between
931 * now and when we try to destroy it (in which case
932 * we don't want to destroy it since we haven't
933 * checked for permission).
935 fnvlist_remove_nvpair(snaps, pair);
946 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
948 char parentname[ZFS_MAX_DATASET_NAME_LEN];
951 if ((error = zfs_secpolicy_write_perms(from,
952 ZFS_DELEG_PERM_RENAME, cr)) != 0)
955 if ((error = zfs_secpolicy_write_perms(from,
956 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
959 if ((error = zfs_get_parent(to, parentname,
960 sizeof (parentname))) != 0)
963 if ((error = zfs_secpolicy_write_perms(parentname,
964 ZFS_DELEG_PERM_CREATE, cr)) != 0)
967 if ((error = zfs_secpolicy_write_perms(parentname,
968 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
976 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
982 if ((pound = strchr(zc->zc_name, '#')) != NULL) {
984 error = zfs_secpolicy_write_perms(zc->zc_name,
985 ZFS_DELEG_PERM_RENAME, cr);
987 error = zfs_secpolicy_write_perms(zc->zc_name,
988 ZFS_DELEG_PERM_BOOKMARK, cr);
994 if ((zc->zc_cookie & 1) != 0) {
996 * This is recursive rename, so the starting snapshot might
997 * not exist. Check file system or volume permission instead.
999 at = strchr(zc->zc_name, '@');
1005 error = zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr);
1015 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1018 dsl_dataset_t *clone;
1021 error = zfs_secpolicy_write_perms(zc->zc_name,
1022 ZFS_DELEG_PERM_PROMOTE, cr);
1026 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
1030 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
1033 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1034 dsl_dataset_t *origin = NULL;
1038 error = dsl_dataset_hold_obj(dd->dd_pool,
1039 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
1041 dsl_dataset_rele(clone, FTAG);
1042 dsl_pool_rele(dp, FTAG);
1046 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
1047 ZFS_DELEG_PERM_MOUNT, cr);
1049 dsl_dataset_name(origin, parentname);
1051 error = zfs_secpolicy_write_perms_ds(parentname, origin,
1052 ZFS_DELEG_PERM_PROMOTE, cr);
1054 dsl_dataset_rele(clone, FTAG);
1055 dsl_dataset_rele(origin, FTAG);
1057 dsl_pool_rele(dp, FTAG);
1063 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1067 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1068 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
1071 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1072 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
1075 return (zfs_secpolicy_write_perms(zc->zc_name,
1076 ZFS_DELEG_PERM_CREATE, cr));
1080 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
1082 return (zfs_secpolicy_write_perms(name,
1083 ZFS_DELEG_PERM_SNAPSHOT, cr));
1087 * Check for permission to create each snapshot in the nvlist.
1091 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1097 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
1099 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1100 pair = nvlist_next_nvpair(snaps, pair)) {
1101 char *name = nvpair_name(pair);
1102 char *atp = strchr(name, '@');
1105 error = SET_ERROR(EINVAL);
1109 error = zfs_secpolicy_snapshot_perms(name, cr);
1118 * Check for permission to create each bookmark in the nvlist.
1122 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1126 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
1127 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1128 char *name = nvpair_name(pair);
1129 char *hashp = strchr(name, '#');
1131 if (hashp == NULL) {
1132 error = SET_ERROR(EINVAL);
1136 error = zfs_secpolicy_write_perms(name,
1137 ZFS_DELEG_PERM_BOOKMARK, cr);
1147 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1149 return (zfs_secpolicy_write_perms(zc->zc_name,
1150 ZFS_DELEG_PERM_REMAP, cr));
1155 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1157 nvpair_t *pair, *nextpair;
1160 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1162 char *name = nvpair_name(pair);
1163 char *hashp = strchr(name, '#');
1164 nextpair = nvlist_next_nvpair(innvl, pair);
1166 if (hashp == NULL) {
1167 error = SET_ERROR(EINVAL);
1172 error = zfs_secpolicy_write_perms(name,
1173 ZFS_DELEG_PERM_DESTROY, cr);
1175 if (error == ENOENT) {
1177 * Ignore any filesystems that don't exist (we consider
1178 * their bookmarks "already destroyed"). Remove
1179 * the name from the nvl here in case the filesystem
1180 * is created between now and when we try to destroy
1181 * the bookmark (in which case we don't want to
1182 * destroy it since we haven't checked for permission).
1184 fnvlist_remove_nvpair(innvl, pair);
1196 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1199 * Even root must have a proper TSD so that we know what pool
1202 if (tsd_get(zfs_allow_log_key) == NULL)
1203 return (SET_ERROR(EPERM));
1208 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1210 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1214 if ((error = zfs_get_parent(zc->zc_name, parentname,
1215 sizeof (parentname))) != 0)
1218 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1219 (error = zfs_secpolicy_write_perms(origin,
1220 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1223 if ((error = zfs_secpolicy_write_perms(parentname,
1224 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1227 return (zfs_secpolicy_write_perms(parentname,
1228 ZFS_DELEG_PERM_MOUNT, cr));
1232 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1233 * SYS_CONFIG privilege, which is not available in a local zone.
1237 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1239 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1240 return (SET_ERROR(EPERM));
1246 * Policy for object to name lookups.
1250 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1254 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1257 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1262 * Policy for fault injection. Requires all privileges.
1266 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1268 return (secpolicy_zinject(cr));
1273 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1275 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1277 if (prop == ZPROP_INVAL) {
1278 if (!zfs_prop_user(zc->zc_value))
1279 return (SET_ERROR(EINVAL));
1280 return (zfs_secpolicy_write_perms(zc->zc_name,
1281 ZFS_DELEG_PERM_USERPROP, cr));
1283 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1289 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1291 int err = zfs_secpolicy_read(zc, innvl, cr);
1295 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1296 return (SET_ERROR(EINVAL));
1298 if (zc->zc_value[0] == 0) {
1300 * They are asking about a posix uid/gid. If it's
1301 * themself, allow it.
1303 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1304 zc->zc_objset_type == ZFS_PROP_USERQUOTA) {
1305 if (zc->zc_guid == crgetuid(cr))
1308 if (groupmember(zc->zc_guid, cr))
1313 return (zfs_secpolicy_write_perms(zc->zc_name,
1314 userquota_perms[zc->zc_objset_type], cr));
1318 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1320 int err = zfs_secpolicy_read(zc, innvl, cr);
1324 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1325 return (SET_ERROR(EINVAL));
1327 return (zfs_secpolicy_write_perms(zc->zc_name,
1328 userquota_perms[zc->zc_objset_type], cr));
1333 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1335 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1341 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1347 holds = fnvlist_lookup_nvlist(innvl, "holds");
1349 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1350 pair = nvlist_next_nvpair(holds, pair)) {
1351 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1352 error = dmu_fsname(nvpair_name(pair), fsname);
1355 error = zfs_secpolicy_write_perms(fsname,
1356 ZFS_DELEG_PERM_HOLD, cr);
1365 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1370 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1371 pair = nvlist_next_nvpair(innvl, pair)) {
1372 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1373 error = dmu_fsname(nvpair_name(pair), fsname);
1376 error = zfs_secpolicy_write_perms(fsname,
1377 ZFS_DELEG_PERM_RELEASE, cr);
1385 * Policy for allowing temporary snapshots to be taken or released
1388 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1391 * A temporary snapshot is the same as a snapshot,
1392 * hold, destroy and release all rolled into one.
1393 * Delegated diff alone is sufficient that we allow this.
1397 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1398 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1401 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1402 if (innvl != NULL) {
1404 error = zfs_secpolicy_hold(zc, innvl, cr);
1406 error = zfs_secpolicy_release(zc, innvl, cr);
1408 error = zfs_secpolicy_destroy(zc, innvl, cr);
1414 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1417 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1421 nvlist_t *list = NULL;
1424 * Read in and unpack the user-supplied nvlist.
1427 return (SET_ERROR(EINVAL));
1429 packed = kmem_alloc(size, KM_SLEEP);
1431 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1433 kmem_free(packed, size);
1434 return (SET_ERROR(EFAULT));
1437 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1438 kmem_free(packed, size);
1442 kmem_free(packed, size);
1449 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1450 * Entries will be removed from the end of the nvlist, and one int32 entry
1451 * named "N_MORE_ERRORS" will be added indicating how many entries were
1455 nvlist_smush(nvlist_t *errors, size_t max)
1459 size = fnvlist_size(errors);
1462 nvpair_t *more_errors;
1466 return (SET_ERROR(ENOMEM));
1468 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1469 more_errors = nvlist_prev_nvpair(errors, NULL);
1472 nvpair_t *pair = nvlist_prev_nvpair(errors,
1474 fnvlist_remove_nvpair(errors, pair);
1476 size = fnvlist_size(errors);
1477 } while (size > max);
1479 fnvlist_remove_nvpair(errors, more_errors);
1480 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1481 ASSERT3U(fnvlist_size(errors), <=, max);
1488 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1490 char *packed = NULL;
1494 size = fnvlist_size(nvl);
1496 if (size > zc->zc_nvlist_dst_size) {
1498 * Solaris returns ENOMEM here, because even if an error is
1499 * returned from an ioctl(2), new zc_nvlist_dst_size will be
1500 * passed to the userland. This is not the case for FreeBSD.
1501 * We need to return 0, so the kernel will copy the
1502 * zc_nvlist_dst_size back and the userland can discover that a
1503 * bigger buffer is needed.
1507 packed = fnvlist_pack(nvl, &size);
1508 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1509 size, zc->zc_iflags) != 0)
1510 error = SET_ERROR(EFAULT);
1511 fnvlist_pack_free(packed, size);
1514 zc->zc_nvlist_dst_size = size;
1515 zc->zc_nvlist_dst_filled = B_TRUE;
1520 getzfsvfs_impl(objset_t *os, vfs_t **vfsp)
1525 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1526 return (SET_ERROR(EINVAL));
1529 mutex_enter(&os->os_user_ptr_lock);
1530 zfvp = dmu_objset_get_user(os);
1532 *vfsp = zfvp->z_vfs;
1533 vfs_ref(zfvp->z_vfs);
1535 error = SET_ERROR(ESRCH);
1537 mutex_exit(&os->os_user_ptr_lock);
1542 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1548 error = dmu_objset_hold(dsname, FTAG, &os);
1551 error = getzfsvfs_impl(os, &vfsp);
1552 dmu_objset_rele(os, FTAG);
1556 error = vfs_busy(vfsp, 0);
1560 error = SET_ERROR(ESRCH);
1562 *zfvp = vfsp->vfs_data;
1568 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1569 * case its z_vfs will be NULL, and it will be opened as the owner.
1570 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1571 * which prevents all vnode ops from running.
1574 zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer)
1578 if (getzfsvfs(name, zfvp) != 0)
1579 error = zfsvfs_create(name, zfvp);
1581 rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER :
1584 if ((*zfvp)->z_unmounted) {
1586 * XXX we could probably try again, since the unmounting
1587 * thread should be just about to disassociate the
1588 * objset from the zfsvfs.
1590 rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1591 return (SET_ERROR(EBUSY));
1595 * vfs_busy() ensures that the filesystem is not and
1596 * can not be unmounted.
1598 ASSERT(!(*zfvp)->z_unmounted);
1605 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1607 rrm_exit(&zfsvfs->z_teardown_lock, tag);
1609 if (zfsvfs->z_vfs) {
1611 VFS_RELE(zfsvfs->z_vfs);
1613 vfs_unbusy(zfsvfs->z_vfs);
1616 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1617 zfsvfs_free(zfsvfs);
1622 zfs_ioc_pool_create(zfs_cmd_t *zc)
1625 nvlist_t *config, *props = NULL;
1626 nvlist_t *rootprops = NULL;
1627 nvlist_t *zplprops = NULL;
1628 char *spa_name = zc->zc_name;
1630 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1631 zc->zc_iflags, &config))
1634 if (zc->zc_nvlist_src_size != 0 && (error =
1635 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1636 zc->zc_iflags, &props))) {
1637 nvlist_free(config);
1642 nvlist_t *nvl = NULL;
1643 uint64_t version = SPA_VERSION;
1646 (void) nvlist_lookup_uint64(props,
1647 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1648 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1649 error = SET_ERROR(EINVAL);
1650 goto pool_props_bad;
1652 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1654 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1656 nvlist_free(config);
1660 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1662 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1663 error = zfs_fill_zplprops_root(version, rootprops,
1666 goto pool_props_bad;
1668 if (nvlist_lookup_string(props,
1669 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1673 error = spa_create(zc->zc_name, config, props, zplprops);
1676 * Set the remaining root properties
1678 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1679 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1680 (void) spa_destroy(spa_name);
1683 nvlist_free(rootprops);
1684 nvlist_free(zplprops);
1685 nvlist_free(config);
1692 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1695 zfs_log_history(zc);
1696 error = spa_destroy(zc->zc_name);
1699 zvol_remove_minors(zc->zc_name);
1705 zfs_ioc_pool_import(zfs_cmd_t *zc)
1707 nvlist_t *config, *props = NULL;
1711 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1712 zc->zc_iflags, &config)) != 0)
1715 if (zc->zc_nvlist_src_size != 0 && (error =
1716 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1717 zc->zc_iflags, &props))) {
1718 nvlist_free(config);
1722 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1723 guid != zc->zc_guid)
1724 error = SET_ERROR(EINVAL);
1726 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1728 if (zc->zc_nvlist_dst != 0) {
1731 if ((err = put_nvlist(zc, config)) != 0)
1735 nvlist_free(config);
1743 zfs_ioc_pool_export(zfs_cmd_t *zc)
1746 boolean_t force = (boolean_t)zc->zc_cookie;
1747 boolean_t hardforce = (boolean_t)zc->zc_guid;
1749 zfs_log_history(zc);
1750 error = spa_export(zc->zc_name, NULL, force, hardforce);
1753 zvol_remove_minors(zc->zc_name);
1759 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1764 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1765 return (SET_ERROR(EEXIST));
1767 error = put_nvlist(zc, configs);
1769 nvlist_free(configs);
1776 * zc_name name of the pool
1779 * zc_cookie real errno
1780 * zc_nvlist_dst config nvlist
1781 * zc_nvlist_dst_size size of config nvlist
1784 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1790 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1791 sizeof (zc->zc_value));
1793 if (config != NULL) {
1794 ret = put_nvlist(zc, config);
1795 nvlist_free(config);
1798 * The config may be present even if 'error' is non-zero.
1799 * In this case we return success, and preserve the real errno
1802 zc->zc_cookie = error;
1811 * Try to import the given pool, returning pool stats as appropriate so that
1812 * user land knows which devices are available and overall pool health.
1815 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1817 nvlist_t *tryconfig, *config;
1820 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1821 zc->zc_iflags, &tryconfig)) != 0)
1824 config = spa_tryimport(tryconfig);
1826 nvlist_free(tryconfig);
1829 return (SET_ERROR(EINVAL));
1831 error = put_nvlist(zc, config);
1832 nvlist_free(config);
1839 * zc_name name of the pool
1840 * zc_cookie scan func (pool_scan_func_t)
1841 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1844 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1849 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1852 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1853 return (SET_ERROR(EINVAL));
1855 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1856 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1857 else if (zc->zc_cookie == POOL_SCAN_NONE)
1858 error = spa_scan_stop(spa);
1860 error = spa_scan(spa, zc->zc_cookie);
1862 spa_close(spa, FTAG);
1868 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1873 error = spa_open(zc->zc_name, &spa, FTAG);
1876 spa_close(spa, FTAG);
1882 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1887 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1890 if (zc->zc_cookie < spa_version(spa) ||
1891 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1892 spa_close(spa, FTAG);
1893 return (SET_ERROR(EINVAL));
1896 spa_upgrade(spa, zc->zc_cookie);
1897 spa_close(spa, FTAG);
1903 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1910 if ((size = zc->zc_history_len) == 0)
1911 return (SET_ERROR(EINVAL));
1913 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1916 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1917 spa_close(spa, FTAG);
1918 return (SET_ERROR(ENOTSUP));
1921 hist_buf = kmem_alloc(size, KM_SLEEP);
1922 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1923 &zc->zc_history_len, hist_buf)) == 0) {
1924 error = ddi_copyout(hist_buf,
1925 (void *)(uintptr_t)zc->zc_history,
1926 zc->zc_history_len, zc->zc_iflags);
1929 spa_close(spa, FTAG);
1930 kmem_free(hist_buf, size);
1935 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1940 error = spa_open(zc->zc_name, &spa, FTAG);
1942 error = spa_change_guid(spa);
1943 spa_close(spa, FTAG);
1949 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1951 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1956 * zc_name name of filesystem
1957 * zc_obj object to find
1960 * zc_value name of object
1963 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1968 /* XXX reading from objset not owned */
1969 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1971 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1972 dmu_objset_rele(os, FTAG);
1973 return (SET_ERROR(EINVAL));
1975 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1976 sizeof (zc->zc_value));
1977 dmu_objset_rele(os, FTAG);
1984 * zc_name name of filesystem
1985 * zc_obj object to find
1988 * zc_stat stats on object
1989 * zc_value path to object
1992 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1997 /* XXX reading from objset not owned */
1998 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
2000 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2001 dmu_objset_rele(os, FTAG);
2002 return (SET_ERROR(EINVAL));
2004 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
2005 sizeof (zc->zc_value));
2006 dmu_objset_rele(os, FTAG);
2012 zfs_ioc_vdev_add(zfs_cmd_t *zc)
2016 nvlist_t *config, **l2cache, **spares;
2017 uint_t nl2cache = 0, nspares = 0;
2019 error = spa_open(zc->zc_name, &spa, FTAG);
2023 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2024 zc->zc_iflags, &config);
2025 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
2026 &l2cache, &nl2cache);
2028 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
2033 * A root pool with concatenated devices is not supported.
2034 * Thus, can not add a device to a root pool.
2036 * Intent log device can not be added to a rootpool because
2037 * during mountroot, zil is replayed, a seperated log device
2038 * can not be accessed during the mountroot time.
2040 * l2cache and spare devices are ok to be added to a rootpool.
2042 if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
2043 nvlist_free(config);
2044 spa_close(spa, FTAG);
2045 return (SET_ERROR(EDOM));
2047 #endif /* illumos */
2050 error = spa_vdev_add(spa, config);
2051 nvlist_free(config);
2053 spa_close(spa, FTAG);
2059 * zc_name name of the pool
2060 * zc_guid guid of vdev to remove
2061 * zc_cookie cancel removal
2064 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
2069 error = spa_open(zc->zc_name, &spa, FTAG);
2072 if (zc->zc_cookie != 0) {
2073 error = spa_vdev_remove_cancel(spa);
2075 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
2077 spa_close(spa, FTAG);
2082 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
2086 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2088 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2090 switch (zc->zc_cookie) {
2091 case VDEV_STATE_ONLINE:
2092 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
2095 case VDEV_STATE_OFFLINE:
2096 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
2099 case VDEV_STATE_FAULTED:
2100 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2101 zc->zc_obj != VDEV_AUX_EXTERNAL)
2102 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2104 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
2107 case VDEV_STATE_DEGRADED:
2108 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2109 zc->zc_obj != VDEV_AUX_EXTERNAL)
2110 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2112 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
2116 error = SET_ERROR(EINVAL);
2118 zc->zc_cookie = newstate;
2119 spa_close(spa, FTAG);
2124 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2127 int replacing = zc->zc_cookie;
2131 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2134 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2135 zc->zc_iflags, &config)) == 0) {
2136 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2137 nvlist_free(config);
2140 spa_close(spa, FTAG);
2145 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2150 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2153 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2155 spa_close(spa, FTAG);
2160 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2163 nvlist_t *config, *props = NULL;
2165 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2167 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2170 if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2171 zc->zc_iflags, &config)) {
2172 spa_close(spa, FTAG);
2176 if (zc->zc_nvlist_src_size != 0 && (error =
2177 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2178 zc->zc_iflags, &props))) {
2179 spa_close(spa, FTAG);
2180 nvlist_free(config);
2184 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2186 spa_close(spa, FTAG);
2188 nvlist_free(config);
2195 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2198 char *path = zc->zc_value;
2199 uint64_t guid = zc->zc_guid;
2202 error = spa_open(zc->zc_name, &spa, FTAG);
2206 error = spa_vdev_setpath(spa, guid, path);
2207 spa_close(spa, FTAG);
2212 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2215 char *fru = zc->zc_value;
2216 uint64_t guid = zc->zc_guid;
2219 error = spa_open(zc->zc_name, &spa, FTAG);
2223 error = spa_vdev_setfru(spa, guid, fru);
2224 spa_close(spa, FTAG);
2229 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2234 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2236 if (zc->zc_nvlist_dst != 0 &&
2237 (error = dsl_prop_get_all(os, &nv)) == 0) {
2238 dmu_objset_stats(os, nv);
2240 * NB: zvol_get_stats() will read the objset contents,
2241 * which we aren't supposed to do with a
2242 * DS_MODE_USER hold, because it could be
2243 * inconsistent. So this is a bit of a workaround...
2244 * XXX reading with out owning
2246 if (!zc->zc_objset_stats.dds_inconsistent &&
2247 dmu_objset_type(os) == DMU_OST_ZVOL) {
2248 error = zvol_get_stats(os, nv);
2253 error = put_nvlist(zc, nv);
2262 * zc_name name of filesystem
2263 * zc_nvlist_dst_size size of buffer for property nvlist
2266 * zc_objset_stats stats
2267 * zc_nvlist_dst property nvlist
2268 * zc_nvlist_dst_size size of property nvlist
2271 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2276 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2278 error = zfs_ioc_objset_stats_impl(zc, os);
2279 dmu_objset_rele(os, FTAG);
2282 if (error == ENOMEM)
2289 * zc_name name of filesystem
2290 * zc_nvlist_dst_size size of buffer for property nvlist
2293 * zc_nvlist_dst received property nvlist
2294 * zc_nvlist_dst_size size of received property nvlist
2296 * Gets received properties (distinct from local properties on or after
2297 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2298 * local property values.
2301 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2307 * Without this check, we would return local property values if the
2308 * caller has not already received properties on or after
2309 * SPA_VERSION_RECVD_PROPS.
2311 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2312 return (SET_ERROR(ENOTSUP));
2314 if (zc->zc_nvlist_dst != 0 &&
2315 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2316 error = put_nvlist(zc, nv);
2324 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2330 * zfs_get_zplprop() will either find a value or give us
2331 * the default value (if there is one).
2333 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2335 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2341 * zc_name name of filesystem
2342 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2345 * zc_nvlist_dst zpl property nvlist
2346 * zc_nvlist_dst_size size of zpl property nvlist
2349 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2354 /* XXX reading without owning */
2355 if (err = dmu_objset_hold(zc->zc_name, FTAG, &os))
2358 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2361 * NB: nvl_add_zplprop() will read the objset contents,
2362 * which we aren't supposed to do with a DS_MODE_USER
2363 * hold, because it could be inconsistent.
2365 if (zc->zc_nvlist_dst != 0 &&
2366 !zc->zc_objset_stats.dds_inconsistent &&
2367 dmu_objset_type(os) == DMU_OST_ZFS) {
2370 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2371 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2372 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2373 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2374 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2375 err = put_nvlist(zc, nv);
2378 err = SET_ERROR(ENOENT);
2380 dmu_objset_rele(os, FTAG);
2385 dataset_name_hidden(const char *name)
2388 * Skip over datasets that are not visible in this zone,
2389 * internal datasets (which have a $ in their name), and
2390 * temporary datasets (which have a % in their name).
2392 if (strchr(name, '$') != NULL)
2394 if (strchr(name, '%') != NULL)
2396 if (!INGLOBALZONE(curthread) && !zone_dataset_visible(name, NULL))
2403 * zc_name name of filesystem
2404 * zc_cookie zap cursor
2405 * zc_nvlist_src iteration range nvlist
2406 * zc_nvlist_src_size size of iteration range nvlist
2409 * zc_name name of next filesystem
2410 * zc_cookie zap cursor
2411 * zc_objset_stats stats
2412 * zc_nvlist_dst property nvlist
2413 * zc_nvlist_dst_size size of property nvlist
2416 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2421 size_t orig_len = strlen(zc->zc_name);
2424 if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) {
2425 if (error == ENOENT)
2426 error = SET_ERROR(ESRCH);
2430 p = strrchr(zc->zc_name, '/');
2431 if (p == NULL || p[1] != '\0')
2432 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2433 p = zc->zc_name + strlen(zc->zc_name);
2436 error = dmu_dir_list_next(os,
2437 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2438 NULL, &zc->zc_cookie);
2439 if (error == ENOENT)
2440 error = SET_ERROR(ESRCH);
2441 } while (error == 0 && dataset_name_hidden(zc->zc_name));
2442 dmu_objset_rele(os, FTAG);
2445 * If it's an internal dataset (ie. with a '$' in its name),
2446 * don't try to get stats for it, otherwise we'll return ENOENT.
2448 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2449 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2450 if (error == ENOENT) {
2451 /* We lost a race with destroy, get the next one. */
2452 zc->zc_name[orig_len] = '\0';
2461 * zc_name name of filesystem
2462 * zc_cookie zap cursor
2463 * zc_nvlist_dst_size size of buffer for property nvlist
2464 * zc_simple when set, only name is requested
2467 * zc_name name of next snapshot
2468 * zc_objset_stats stats
2469 * zc_nvlist_dst property nvlist
2470 * zc_nvlist_dst_size size of property nvlist
2473 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2476 objset_t *os, *ossnap;
2478 uint64_t min_txg = 0, max_txg = 0;
2480 if (zc->zc_nvlist_src_size != 0) {
2481 nvlist_t *props = NULL;
2482 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2483 zc->zc_iflags, &props);
2486 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2488 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2493 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2495 return (error == ENOENT ? ESRCH : error);
2499 * A dataset name of maximum length cannot have any snapshots,
2500 * so exit immediately.
2502 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2503 ZFS_MAX_DATASET_NAME_LEN) {
2504 dmu_objset_rele(os, FTAG);
2505 return (SET_ERROR(ESRCH));
2508 while (error == 0) {
2509 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2510 error = SET_ERROR(EINTR);
2514 error = dmu_snapshot_list_next(os,
2515 sizeof (zc->zc_name) - strlen(zc->zc_name),
2516 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2517 &zc->zc_cookie, NULL);
2518 if (error == ENOENT) {
2519 error = SET_ERROR(ESRCH);
2521 } else if (error != 0) {
2525 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2530 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2531 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2532 dsl_dataset_rele(ds, FTAG);
2533 /* undo snapshot name append */
2534 *(strchr(zc->zc_name, '@') + 1) = '\0';
2539 if (zc->zc_simple) {
2540 dsl_dataset_rele(ds, FTAG);
2544 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2545 dsl_dataset_rele(ds, FTAG);
2548 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2549 dsl_dataset_rele(ds, FTAG);
2552 dsl_dataset_rele(ds, FTAG);
2556 dmu_objset_rele(os, FTAG);
2557 /* if we failed, undo the @ that we tacked on to zc_name */
2559 *strchr(zc->zc_name, '@') = '\0';
2564 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2566 const char *propname = nvpair_name(pair);
2568 unsigned int vallen;
2571 zfs_userquota_prop_t type;
2577 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2579 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2580 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2582 return (SET_ERROR(EINVAL));
2586 * A correctly constructed propname is encoded as
2587 * userquota@<rid>-<domain>.
2589 if ((dash = strchr(propname, '-')) == NULL ||
2590 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2592 return (SET_ERROR(EINVAL));
2599 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2601 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2602 zfsvfs_rele(zfsvfs, FTAG);
2609 * If the named property is one that has a special function to set its value,
2610 * return 0 on success and a positive error code on failure; otherwise if it is
2611 * not one of the special properties handled by this function, return -1.
2613 * XXX: It would be better for callers of the property interface if we handled
2614 * these special cases in dsl_prop.c (in the dsl layer).
2617 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2620 const char *propname = nvpair_name(pair);
2621 zfs_prop_t prop = zfs_name_to_prop(propname);
2625 if (prop == ZPROP_INVAL) {
2626 if (zfs_prop_userquota(propname))
2627 return (zfs_prop_set_userquota(dsname, pair));
2631 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2633 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2634 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2638 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING)
2641 VERIFY(0 == nvpair_value_uint64(pair, &intval));
2644 case ZFS_PROP_QUOTA:
2645 err = dsl_dir_set_quota(dsname, source, intval);
2647 case ZFS_PROP_REFQUOTA:
2648 err = dsl_dataset_set_refquota(dsname, source, intval);
2650 case ZFS_PROP_FILESYSTEM_LIMIT:
2651 case ZFS_PROP_SNAPSHOT_LIMIT:
2652 if (intval == UINT64_MAX) {
2653 /* clearing the limit, just do it */
2656 err = dsl_dir_activate_fs_ss_limit(dsname);
2659 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2660 * default path to set the value in the nvlist.
2665 case ZFS_PROP_RESERVATION:
2666 err = dsl_dir_set_reservation(dsname, source, intval);
2668 case ZFS_PROP_REFRESERVATION:
2669 err = dsl_dataset_set_refreservation(dsname, source, intval);
2671 case ZFS_PROP_VOLSIZE:
2672 err = zvol_set_volsize(dsname, intval);
2674 case ZFS_PROP_VERSION:
2678 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2681 err = zfs_set_version(zfsvfs, intval);
2682 zfsvfs_rele(zfsvfs, FTAG);
2684 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2687 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2688 (void) strcpy(zc->zc_name, dsname);
2689 (void) zfs_ioc_userspace_upgrade(zc);
2690 kmem_free(zc, sizeof (zfs_cmd_t));
2702 * This function is best effort. If it fails to set any of the given properties,
2703 * it continues to set as many as it can and returns the last error
2704 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2705 * with the list of names of all the properties that failed along with the
2706 * corresponding error numbers.
2708 * If every property is set successfully, zero is returned and errlist is not
2712 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2720 nvlist_t *genericnvl = fnvlist_alloc();
2721 nvlist_t *retrynvl = fnvlist_alloc();
2725 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2726 const char *propname = nvpair_name(pair);
2727 zfs_prop_t prop = zfs_name_to_prop(propname);
2730 /* decode the property value */
2732 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2734 attrs = fnvpair_value_nvlist(pair);
2735 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2737 err = SET_ERROR(EINVAL);
2740 /* Validate value type */
2741 if (err == 0 && prop == ZPROP_INVAL) {
2742 if (zfs_prop_user(propname)) {
2743 if (nvpair_type(propval) != DATA_TYPE_STRING)
2744 err = SET_ERROR(EINVAL);
2745 } else if (zfs_prop_userquota(propname)) {
2746 if (nvpair_type(propval) !=
2747 DATA_TYPE_UINT64_ARRAY)
2748 err = SET_ERROR(EINVAL);
2750 err = SET_ERROR(EINVAL);
2752 } else if (err == 0) {
2753 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2754 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2755 err = SET_ERROR(EINVAL);
2756 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2759 intval = fnvpair_value_uint64(propval);
2761 switch (zfs_prop_get_type(prop)) {
2762 case PROP_TYPE_NUMBER:
2764 case PROP_TYPE_STRING:
2765 err = SET_ERROR(EINVAL);
2767 case PROP_TYPE_INDEX:
2768 if (zfs_prop_index_to_string(prop,
2769 intval, &unused) != 0)
2770 err = SET_ERROR(EINVAL);
2774 "unknown property type");
2777 err = SET_ERROR(EINVAL);
2781 /* Validate permissions */
2783 err = zfs_check_settable(dsname, pair, CRED());
2786 err = zfs_prop_set_special(dsname, source, pair);
2789 * For better performance we build up a list of
2790 * properties to set in a single transaction.
2792 err = nvlist_add_nvpair(genericnvl, pair);
2793 } else if (err != 0 && nvl != retrynvl) {
2795 * This may be a spurious error caused by
2796 * receiving quota and reservation out of order.
2797 * Try again in a second pass.
2799 err = nvlist_add_nvpair(retrynvl, pair);
2804 if (errlist != NULL)
2805 fnvlist_add_int32(errlist, propname, err);
2810 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2815 if (!nvlist_empty(genericnvl) &&
2816 dsl_props_set(dsname, source, genericnvl) != 0) {
2818 * If this fails, we still want to set as many properties as we
2819 * can, so try setting them individually.
2822 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2823 const char *propname = nvpair_name(pair);
2827 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2829 attrs = fnvpair_value_nvlist(pair);
2830 propval = fnvlist_lookup_nvpair(attrs,
2834 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2835 strval = fnvpair_value_string(propval);
2836 err = dsl_prop_set_string(dsname, propname,
2839 intval = fnvpair_value_uint64(propval);
2840 err = dsl_prop_set_int(dsname, propname, source,
2845 if (errlist != NULL) {
2846 fnvlist_add_int32(errlist, propname,
2853 nvlist_free(genericnvl);
2854 nvlist_free(retrynvl);
2860 * Check that all the properties are valid user properties.
2863 zfs_check_userprops(nvlist_t *nvl)
2865 nvpair_t *pair = NULL;
2867 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2868 const char *propname = nvpair_name(pair);
2870 if (!zfs_prop_user(propname) ||
2871 nvpair_type(pair) != DATA_TYPE_STRING)
2872 return (SET_ERROR(EINVAL));
2874 if (strlen(propname) >= ZAP_MAXNAMELEN)
2875 return (SET_ERROR(ENAMETOOLONG));
2877 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2884 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2888 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2891 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2892 if (nvlist_exists(skipped, nvpair_name(pair)))
2895 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2900 clear_received_props(const char *dsname, nvlist_t *props,
2904 nvlist_t *cleared_props = NULL;
2905 props_skip(props, skipped, &cleared_props);
2906 if (!nvlist_empty(cleared_props)) {
2908 * Acts on local properties until the dataset has received
2909 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2911 zprop_source_t flags = (ZPROP_SRC_NONE |
2912 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2913 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2915 nvlist_free(cleared_props);
2921 * zc_name name of filesystem
2922 * zc_value name of property to set
2923 * zc_nvlist_src{_size} nvlist of properties to apply
2924 * zc_cookie received properties flag
2927 * zc_nvlist_dst{_size} error for each unapplied received property
2930 zfs_ioc_set_prop(zfs_cmd_t *zc)
2933 boolean_t received = zc->zc_cookie;
2934 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2939 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2940 zc->zc_iflags, &nvl)) != 0)
2944 nvlist_t *origprops;
2946 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2947 (void) clear_received_props(zc->zc_name,
2949 nvlist_free(origprops);
2952 error = dsl_prop_set_hasrecvd(zc->zc_name);
2955 errors = fnvlist_alloc();
2957 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2959 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2960 (void) put_nvlist(zc, errors);
2963 nvlist_free(errors);
2970 * zc_name name of filesystem
2971 * zc_value name of property to inherit
2972 * zc_cookie revert to received value if TRUE
2977 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2979 const char *propname = zc->zc_value;
2980 zfs_prop_t prop = zfs_name_to_prop(propname);
2981 boolean_t received = zc->zc_cookie;
2982 zprop_source_t source = (received
2983 ? ZPROP_SRC_NONE /* revert to received value, if any */
2984 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2993 * zfs_prop_set_special() expects properties in the form of an
2994 * nvpair with type info.
2996 if (prop == ZPROP_INVAL) {
2997 if (!zfs_prop_user(propname))
2998 return (SET_ERROR(EINVAL));
3000 type = PROP_TYPE_STRING;
3001 } else if (prop == ZFS_PROP_VOLSIZE ||
3002 prop == ZFS_PROP_VERSION) {
3003 return (SET_ERROR(EINVAL));
3005 type = zfs_prop_get_type(prop);
3008 VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3011 case PROP_TYPE_STRING:
3012 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
3014 case PROP_TYPE_NUMBER:
3015 case PROP_TYPE_INDEX:
3016 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
3020 return (SET_ERROR(EINVAL));
3023 pair = nvlist_next_nvpair(dummy, NULL);
3024 err = zfs_prop_set_special(zc->zc_name, source, pair);
3027 return (err); /* special property already handled */
3030 * Only check this in the non-received case. We want to allow
3031 * 'inherit -S' to revert non-inheritable properties like quota
3032 * and reservation to the received or default values even though
3033 * they are not considered inheritable.
3035 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
3036 return (SET_ERROR(EINVAL));
3039 /* property name has been validated by zfs_secpolicy_inherit_prop() */
3040 return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
3044 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
3051 if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3052 zc->zc_iflags, &props))
3056 * If the only property is the configfile, then just do a spa_lookup()
3057 * to handle the faulted case.
3059 pair = nvlist_next_nvpair(props, NULL);
3060 if (pair != NULL && strcmp(nvpair_name(pair),
3061 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
3062 nvlist_next_nvpair(props, pair) == NULL) {
3063 mutex_enter(&spa_namespace_lock);
3064 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
3065 spa_configfile_set(spa, props, B_FALSE);
3066 spa_write_cachefile(spa, B_FALSE, B_TRUE);
3068 mutex_exit(&spa_namespace_lock);
3075 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3080 error = spa_prop_set(spa, props);
3083 spa_close(spa, FTAG);
3089 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
3093 nvlist_t *nvp = NULL;
3095 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3097 * If the pool is faulted, there may be properties we can still
3098 * get (such as altroot and cachefile), so attempt to get them
3101 mutex_enter(&spa_namespace_lock);
3102 if ((spa = spa_lookup(zc->zc_name)) != NULL)
3103 error = spa_prop_get(spa, &nvp);
3104 mutex_exit(&spa_namespace_lock);
3106 error = spa_prop_get(spa, &nvp);
3107 spa_close(spa, FTAG);
3110 if (error == 0 && zc->zc_nvlist_dst != 0)
3111 error = put_nvlist(zc, nvp);
3113 error = SET_ERROR(EFAULT);
3121 * zc_name name of filesystem
3122 * zc_nvlist_src{_size} nvlist of delegated permissions
3123 * zc_perm_action allow/unallow flag
3128 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3131 nvlist_t *fsaclnv = NULL;
3133 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3134 zc->zc_iflags, &fsaclnv)) != 0)
3138 * Verify nvlist is constructed correctly
3140 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3141 nvlist_free(fsaclnv);
3142 return (SET_ERROR(EINVAL));
3146 * If we don't have PRIV_SYS_MOUNT, then validate
3147 * that user is allowed to hand out each permission in
3151 error = secpolicy_zfs(CRED());
3153 if (zc->zc_perm_action == B_FALSE) {
3154 error = dsl_deleg_can_allow(zc->zc_name,
3157 error = dsl_deleg_can_unallow(zc->zc_name,
3163 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3165 nvlist_free(fsaclnv);
3171 * zc_name name of filesystem
3174 * zc_nvlist_src{_size} nvlist of delegated permissions
3177 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3182 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3183 error = put_nvlist(zc, nvp);
3192 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3194 zfs_creat_t *zct = arg;
3196 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3199 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3203 * os parent objset pointer (NULL if root fs)
3204 * fuids_ok fuids allowed in this version of the spa?
3205 * sa_ok SAs allowed in this version of the spa?
3206 * createprops list of properties requested by creator
3209 * zplprops values for the zplprops we attach to the master node object
3210 * is_ci true if requested file system will be purely case-insensitive
3212 * Determine the settings for utf8only, normalization and
3213 * casesensitivity. Specific values may have been requested by the
3214 * creator and/or we can inherit values from the parent dataset. If
3215 * the file system is of too early a vintage, a creator can not
3216 * request settings for these properties, even if the requested
3217 * setting is the default value. We don't actually want to create dsl
3218 * properties for these, so remove them from the source nvlist after
3222 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3223 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3224 nvlist_t *zplprops, boolean_t *is_ci)
3226 uint64_t sense = ZFS_PROP_UNDEFINED;
3227 uint64_t norm = ZFS_PROP_UNDEFINED;
3228 uint64_t u8 = ZFS_PROP_UNDEFINED;
3230 ASSERT(zplprops != NULL);
3232 /* parent dataset must be a filesystem */
3233 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3234 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3237 * Pull out creator prop choices, if any.
3240 (void) nvlist_lookup_uint64(createprops,
3241 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3242 (void) nvlist_lookup_uint64(createprops,
3243 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3244 (void) nvlist_remove_all(createprops,
3245 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3246 (void) nvlist_lookup_uint64(createprops,
3247 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3248 (void) nvlist_remove_all(createprops,
3249 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3250 (void) nvlist_lookup_uint64(createprops,
3251 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3252 (void) nvlist_remove_all(createprops,
3253 zfs_prop_to_name(ZFS_PROP_CASE));
3257 * If the zpl version requested is whacky or the file system
3258 * or pool is version is too "young" to support normalization
3259 * and the creator tried to set a value for one of the props,
3262 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3263 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3264 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3265 (zplver < ZPL_VERSION_NORMALIZATION &&
3266 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3267 sense != ZFS_PROP_UNDEFINED)))
3268 return (SET_ERROR(ENOTSUP));
3271 * Put the version in the zplprops
3273 VERIFY(nvlist_add_uint64(zplprops,
3274 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3276 if (norm == ZFS_PROP_UNDEFINED)
3277 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3278 VERIFY(nvlist_add_uint64(zplprops,
3279 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3282 * If we're normalizing, names must always be valid UTF-8 strings.
3286 if (u8 == ZFS_PROP_UNDEFINED)
3287 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3288 VERIFY(nvlist_add_uint64(zplprops,
3289 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3291 if (sense == ZFS_PROP_UNDEFINED)
3292 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3293 VERIFY(nvlist_add_uint64(zplprops,
3294 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3297 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3303 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3304 nvlist_t *zplprops, boolean_t *is_ci)
3306 boolean_t fuids_ok, sa_ok;
3307 uint64_t zplver = ZPL_VERSION;
3308 objset_t *os = NULL;
3309 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3314 zfs_get_parent(dataset, parentname, sizeof (parentname));
3316 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3319 spa_vers = spa_version(spa);
3320 spa_close(spa, FTAG);
3322 zplver = zfs_zpl_version_map(spa_vers);
3323 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3324 sa_ok = (zplver >= ZPL_VERSION_SA);
3327 * Open parent object set so we can inherit zplprop values.
3329 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3332 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3334 dmu_objset_rele(os, FTAG);
3339 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3340 nvlist_t *zplprops, boolean_t *is_ci)
3344 uint64_t zplver = ZPL_VERSION;
3347 zplver = zfs_zpl_version_map(spa_vers);
3348 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3349 sa_ok = (zplver >= ZPL_VERSION_SA);
3351 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3352 createprops, zplprops, is_ci);
3358 * "type" -> dmu_objset_type_t (int32)
3359 * (optional) "props" -> { prop -> value }
3362 * outnvl: propname -> error code (int32)
3365 static const zfs_ioc_key_t zfs_keys_create[] = {
3366 {"type", DATA_TYPE_INT32, 0},
3367 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3368 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3372 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3375 zfs_creat_t zct = { 0 };
3376 nvlist_t *nvprops = NULL;
3377 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3378 dmu_objset_type_t type;
3379 boolean_t is_insensitive = B_FALSE;
3381 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3382 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3386 cbfunc = zfs_create_cb;
3390 cbfunc = zvol_create_cb;
3397 if (strchr(fsname, '@') ||
3398 strchr(fsname, '%'))
3399 return (SET_ERROR(EINVAL));
3401 zct.zct_props = nvprops;
3404 return (SET_ERROR(EINVAL));
3406 if (type == DMU_OST_ZVOL) {
3407 uint64_t volsize, volblocksize;
3409 if (nvprops == NULL)
3410 return (SET_ERROR(EINVAL));
3411 if (nvlist_lookup_uint64(nvprops,
3412 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3413 return (SET_ERROR(EINVAL));
3415 if ((error = nvlist_lookup_uint64(nvprops,
3416 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3417 &volblocksize)) != 0 && error != ENOENT)
3418 return (SET_ERROR(EINVAL));
3421 volblocksize = zfs_prop_default_numeric(
3422 ZFS_PROP_VOLBLOCKSIZE);
3424 if ((error = zvol_check_volblocksize(
3425 volblocksize)) != 0 ||
3426 (error = zvol_check_volsize(volsize,
3427 volblocksize)) != 0)
3429 } else if (type == DMU_OST_ZFS) {
3433 * We have to have normalization and
3434 * case-folding flags correct when we do the
3435 * file system creation, so go figure them out
3438 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3439 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3440 error = zfs_fill_zplprops(fsname, nvprops,
3441 zct.zct_zplprops, &is_insensitive);
3443 nvlist_free(zct.zct_zplprops);
3448 error = dmu_objset_create(fsname, type,
3449 is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3450 nvlist_free(zct.zct_zplprops);
3453 * It would be nice to do this atomically.
3456 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3458 #if defined(__FreeBSD__) && defined(_KERNEL)
3460 * Wait for ZVOL operations to settle down before destroying.
3465 if (spa_open(fsname, &spa, FTAG) == 0) {
3466 taskqueue_drain_all(
3467 spa->spa_zvol_taskq->tq_queue);
3468 spa_close(spa, FTAG);
3473 (void) dsl_destroy_head(fsname);
3480 * "origin" -> name of origin snapshot
3481 * (optional) "props" -> { prop -> value }
3484 * outnvl: propname -> error code (int32)
3486 static const zfs_ioc_key_t zfs_keys_clone[] = {
3487 {"origin", DATA_TYPE_STRING, 0},
3488 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3489 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3493 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3496 nvlist_t *nvprops = NULL;
3499 origin_name = fnvlist_lookup_string(innvl, "origin");
3500 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3502 if (strchr(fsname, '@') ||
3503 strchr(fsname, '%'))
3504 return (SET_ERROR(EINVAL));
3506 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3507 return (SET_ERROR(EINVAL));
3508 error = dmu_objset_clone(fsname, origin_name);
3513 * It would be nice to do this atomically.
3516 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3519 (void) dsl_destroy_head(fsname);
3524 static const zfs_ioc_key_t zfs_keys_remap[] = {
3530 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3532 if (strchr(fsname, '@') ||
3533 strchr(fsname, '%'))
3534 return (SET_ERROR(EINVAL));
3536 return (dmu_objset_remap_indirects(fsname));
3541 * "snaps" -> { snapshot1, snapshot2 }
3542 * (optional) "props" -> { prop -> value (string) }
3545 * outnvl: snapshot -> error code (int32)
3547 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3548 {"snaps", DATA_TYPE_NVLIST, 0},
3549 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3553 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3556 nvlist_t *props = NULL;
3560 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3561 if (!nvlist_empty(props) &&
3562 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3563 return (SET_ERROR(ENOTSUP));
3564 if ((error = zfs_check_userprops(props)) != 0)
3567 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3568 poollen = strlen(poolname);
3569 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3570 pair = nvlist_next_nvpair(snaps, pair)) {
3571 const char *name = nvpair_name(pair);
3572 char *cp = strchr(name, '@');
3575 * The snap name must contain an @, and the part after it must
3576 * contain only valid characters.
3579 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3580 return (SET_ERROR(EINVAL));
3583 * The snap must be in the specified pool.
3585 if (strncmp(name, poolname, poollen) != 0 ||
3586 (name[poollen] != '/' && name[poollen] != '@'))
3587 return (SET_ERROR(EXDEV));
3590 * Check for permission to set the properties on the fs.
3592 if (!nvlist_empty(props)) {
3594 error = zfs_secpolicy_write_perms(name,
3595 ZFS_DELEG_PERM_USERPROP, CRED());
3601 /* This must be the only snap of this fs. */
3602 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3603 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3604 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3606 return (SET_ERROR(EXDEV));
3611 error = dsl_dataset_snapshot(snaps, props, outnvl);
3616 * innvl: "message" -> string
3618 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3619 {"message", DATA_TYPE_STRING, 0},
3624 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3632 * The poolname in the ioctl is not set, we get it from the TSD,
3633 * which was set at the end of the last successful ioctl that allows
3634 * logging. The secpolicy func already checked that it is set.
3635 * Only one log ioctl is allowed after each successful ioctl, so
3636 * we clear the TSD here.
3638 poolname = tsd_get(zfs_allow_log_key);
3639 (void) tsd_set(zfs_allow_log_key, NULL);
3640 error = spa_open(poolname, &spa, FTAG);
3645 message = fnvlist_lookup_string(innvl, "message");
3647 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3648 spa_close(spa, FTAG);
3649 return (SET_ERROR(ENOTSUP));
3652 error = spa_history_log(spa, message);
3653 spa_close(spa, FTAG);
3658 * This ioctl is used to set the bootenv configuration on the current
3659 * pool. This configuration is stored in the second padding area of the label,
3660 * and it is used by the GRUB bootloader used on Linux to store the contents
3661 * of the grubenv file. The file is stored as raw ASCII, and is protected by
3662 * an embedded checksum. By default, GRUB will check if the boot filesystem
3663 * supports storing the environment data in a special location, and if so,
3664 * will invoke filesystem specific logic to retrieve it. This can be overriden
3665 * by a variable, should the user so desire.
3668 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3669 {"envmap", DATA_TYPE_STRING, 0},
3673 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3679 envmap = fnvlist_lookup_string(innvl, "envmap");
3680 if ((error = spa_open(name, &spa, FTAG)) != 0)
3682 spa_vdev_state_enter(spa, SCL_ALL);
3683 error = vdev_label_write_bootenv(spa->spa_root_vdev, envmap);
3684 (void) spa_vdev_state_exit(spa, NULL, 0);
3685 spa_close(spa, FTAG);
3689 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3695 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3700 if ((error = spa_open(name, &spa, FTAG)) != 0)
3702 spa_vdev_state_enter(spa, SCL_ALL);
3703 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3704 (void) spa_vdev_state_exit(spa, NULL, 0);
3705 spa_close(spa, FTAG);
3710 static const zfs_ioc_key_t zfs_keys_nextboot[] = {
3711 {"command", DATA_TYPE_STRING, 0},
3712 {ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 0},
3713 {ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 0}
3717 zfs_ioc_nextboot(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3719 char name[MAXNAMELEN];
3727 if (nvlist_lookup_uint64(innvl,
3728 ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
3730 if (nvlist_lookup_uint64(innvl,
3731 ZPOOL_CONFIG_GUID, &vdev_guid) != 0)
3733 command = fnvlist_lookup_string(innvl, "command");
3735 mutex_enter(&spa_namespace_lock);
3736 spa = spa_by_guid(pool_guid, vdev_guid);
3738 strcpy(name, spa_name(spa));
3739 mutex_exit(&spa_namespace_lock);
3743 if ((error = spa_open(name, &spa, FTAG)) != 0)
3745 spa_vdev_state_enter(spa, SCL_ALL);
3746 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
3748 (void) spa_vdev_state_exit(spa, NULL, ENXIO);
3749 spa_close(spa, FTAG);
3752 error = vdev_label_write_pad2(vd, command, strlen(command));
3753 (void) spa_vdev_state_exit(spa, NULL, 0);
3754 txg_wait_synced(spa->spa_dsl_pool, 0);
3755 spa_close(spa, FTAG);
3761 * The dp_config_rwlock must not be held when calling this, because the
3762 * unmount may need to write out data.
3764 * This function is best-effort. Callers must deal gracefully if it
3765 * remains mounted (or is remounted after this call).
3767 * Returns 0 if the argument is not a snapshot, or it is not currently a
3768 * filesystem, or we were able to unmount it. Returns error code otherwise.
3771 zfs_unmount_snap(const char *snapname)
3774 zfsvfs_t *zfsvfs = NULL;
3776 if (strchr(snapname, '@') == NULL)
3779 int err = getzfsvfs(snapname, &zfsvfs);
3781 ASSERT3P(zfsvfs, ==, NULL);
3784 vfsp = zfsvfs->z_vfs;
3786 ASSERT(!dsl_pool_config_held(dmu_objset_pool(zfsvfs->z_os)));
3789 err = vn_vfswlock(vfsp->vfs_vnodecovered);
3796 * Always force the unmount for snapshots.
3799 (void) dounmount(vfsp, MS_FORCE, kcred);
3803 (void) dounmount(vfsp, MS_FORCE, curthread);
3809 zfs_unmount_snap_cb(const char *snapname, void *arg)
3811 zfs_unmount_snap(snapname);
3816 * When a clone is destroyed, its origin may also need to be destroyed,
3817 * in which case it must be unmounted. This routine will do that unmount
3821 zfs_destroy_unmount_origin(const char *fsname)
3827 error = dmu_objset_hold(fsname, FTAG, &os);
3830 ds = dmu_objset_ds(os);
3831 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3832 char originname[ZFS_MAX_DATASET_NAME_LEN];
3833 dsl_dataset_name(ds->ds_prev, originname);
3834 dmu_objset_rele(os, FTAG);
3835 zfs_unmount_snap(originname);
3837 dmu_objset_rele(os, FTAG);
3843 * "snaps" -> { snapshot1, snapshot2 }
3844 * (optional boolean) "defer"
3847 * outnvl: snapshot -> error code (int32)
3850 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3851 {"snaps", DATA_TYPE_NVLIST, 0},
3852 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3857 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3864 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3865 defer = nvlist_exists(innvl, "defer");
3867 poollen = strlen(poolname);
3868 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3869 pair = nvlist_next_nvpair(snaps, pair)) {
3870 const char *name = nvpair_name(pair);
3873 * The snap must be in the specified pool to prevent the
3874 * invalid removal of zvol minors below.
3876 if (strncmp(name, poolname, poollen) != 0 ||
3877 (name[poollen] != '/' && name[poollen] != '@'))
3878 return (SET_ERROR(EXDEV));
3880 zfs_unmount_snap(nvpair_name(pair));
3883 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3887 * Create bookmarks. Bookmark names are of the form <fs>#<bmark>.
3888 * All bookmarks must be in the same pool.
3891 * bookmark1 -> snapshot1, bookmark2 -> snapshot2
3894 * outnvl: bookmark -> error code (int32)
3897 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3898 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3903 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3905 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3906 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3910 * Verify the snapshot argument.
3912 if (nvpair_value_string(pair, &snap_name) != 0)
3913 return (SET_ERROR(EINVAL));
3916 /* Verify that the keys (bookmarks) are unique */
3917 for (nvpair_t *pair2 = nvlist_next_nvpair(innvl, pair);
3918 pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) {
3919 if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0)
3920 return (SET_ERROR(EINVAL));
3924 return (dsl_bookmark_create(innvl, outnvl));
3929 * property 1, property 2, ...
3933 * bookmark name 1 -> { property 1, property 2, ... },
3934 * bookmark name 2 -> { property 1, property 2, ... }
3938 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3939 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3943 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3945 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3950 * bookmark name 1, bookmark name 2
3953 * outnvl: bookmark -> error code (int32)
3956 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3957 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3961 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3966 poollen = strlen(poolname);
3967 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3968 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3969 const char *name = nvpair_name(pair);
3970 const char *cp = strchr(name, '#');
3973 * The bookmark name must contain an #, and the part after it
3974 * must contain only valid characters.
3977 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3978 return (SET_ERROR(EINVAL));
3981 * The bookmark must be in the specified pool.
3983 if (strncmp(name, poolname, poollen) != 0 ||
3984 (name[poollen] != '/' && name[poollen] != '#'))
3985 return (SET_ERROR(EXDEV));
3988 error = dsl_bookmark_destroy(innvl, outnvl);
3992 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3993 {"program", DATA_TYPE_STRING, 0},
3994 {"arg", DATA_TYPE_ANY, 0},
3995 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3996 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3997 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
4001 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
4005 uint64_t instrlimit, memlimit;
4006 boolean_t sync_flag;
4007 nvpair_t *nvarg = NULL;
4009 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
4010 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
4013 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
4014 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
4016 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
4017 memlimit = ZCP_DEFAULT_MEMLIMIT;
4019 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
4021 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
4023 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
4026 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
4034 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
4040 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4042 return (spa_checkpoint(poolname));
4049 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
4055 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
4058 return (spa_checkpoint_discard(poolname));
4063 * zc_name name of dataset to destroy
4064 * zc_defer_destroy mark for deferred destroy
4069 zfs_ioc_destroy(zfs_cmd_t *zc)
4072 dmu_objset_type_t ost;
4075 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4078 ost = dmu_objset_type(os);
4079 dmu_objset_rele(os, FTAG);
4081 if (ost == DMU_OST_ZFS)
4082 zfs_unmount_snap(zc->zc_name);
4084 if (strchr(zc->zc_name, '@'))
4085 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
4087 err = dsl_destroy_head(zc->zc_name);
4089 if (ost == DMU_OST_ZVOL && err == 0)
4090 (void) zvol_remove_minor(zc->zc_name);
4098 * guid 1, guid 2, ...
4100 * func: POOL_INITIALIZE_{CANCEL|DO|SUSPEND}
4104 * [func: EINVAL (if provided command type didn't make sense)],
4106 * guid1: errno, (see function body for possible errnos)
4112 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
4113 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
4114 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4118 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4123 error = spa_open(poolname, &spa, FTAG);
4128 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4130 spa_close(spa, FTAG);
4131 return (SET_ERROR(EINVAL));
4133 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4134 cmd_type == POOL_INITIALIZE_DO ||
4135 cmd_type == POOL_INITIALIZE_SUSPEND)) {
4136 spa_close(spa, FTAG);
4137 return (SET_ERROR(EINVAL));
4140 nvlist_t *vdev_guids;
4141 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4142 &vdev_guids) != 0) {
4143 spa_close(spa, FTAG);
4144 return (SET_ERROR(EINVAL));
4147 nvlist_t *vdev_errlist = fnvlist_alloc();
4148 int total_errors = 0;
4150 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4151 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4152 uint64_t vdev_guid = fnvpair_value_uint64(pair);
4154 error = spa_vdev_initialize(spa, vdev_guid, cmd_type);
4156 char guid_as_str[MAXNAMELEN];
4158 (void) snprintf(guid_as_str, sizeof (guid_as_str),
4159 "%llu", (unsigned long long)vdev_guid);
4160 fnvlist_add_int64(vdev_errlist, guid_as_str, error);
4164 if (fnvlist_size(vdev_errlist) > 0) {
4165 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4168 fnvlist_free(vdev_errlist);
4170 spa_close(spa, FTAG);
4171 return (total_errors > 0 ? EINVAL : 0);
4175 * fsname is name of dataset to rollback (to most recent snapshot)
4177 * innvl may contain name of expected target snapshot
4179 * outnvl: "target" -> name of most recent snapshot
4182 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4183 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4188 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4191 char *target = NULL;
4194 (void) nvlist_lookup_string(innvl, "target", &target);
4195 if (target != NULL) {
4196 const char *cp = strchr(target, '@');
4199 * The snap name must contain an @, and the part after it must
4200 * contain only valid characters.
4203 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4204 return (SET_ERROR(EINVAL));
4207 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4210 ds = dmu_objset_ds(zfsvfs->z_os);
4211 error = zfs_suspend_fs(zfsvfs);
4215 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4217 resume_err = zfs_resume_fs(zfsvfs, ds);
4218 error = error ? error : resume_err;
4221 VFS_RELE(zfsvfs->z_vfs);
4223 vfs_unbusy(zfsvfs->z_vfs);
4226 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4232 recursive_unmount(const char *fsname, void *arg)
4234 const char *snapname = arg;
4235 char fullname[ZFS_MAX_DATASET_NAME_LEN];
4237 (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
4238 zfs_unmount_snap(fullname);
4245 * zc_name old name of dataset or bookmark
4246 * zc_value new name of dataset or bookmark
4247 * zc_cookie recursive flag (only valid for snapshots)
4252 zfs_ioc_rename(zfs_cmd_t *zc)
4255 dmu_objset_type_t ost;
4256 boolean_t recursive = zc->zc_cookie & 1;
4258 boolean_t allow_mounted = B_TRUE;
4262 allow_mounted = (zc->zc_cookie & 2) != 0;
4265 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4266 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4268 pos = strchr(zc->zc_name, '#');
4270 /* Bookmarks must be in same fs. */
4271 pos2 = strchr(zc->zc_value, '#');
4273 return (SET_ERROR(EINVAL));
4275 /* Recursive flag is not supported yet. */
4277 return (SET_ERROR(ENOTSUP));
4281 if (strcmp(zc->zc_name, zc->zc_value) == 0) {
4282 err = dsl_bookmark_rename(zc->zc_name,
4285 err = SET_ERROR(EXDEV);
4292 /* "zfs rename" from and to ...%recv datasets should both fail */
4293 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4294 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4295 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4296 return (SET_ERROR(EINVAL));
4298 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4301 ost = dmu_objset_type(os);
4302 dmu_objset_rele(os, FTAG);
4304 pos = strchr(zc->zc_name, '@');
4306 /* Snapshots must be in same fs. */
4307 pos2 = strchr(zc->zc_value, '@');
4309 return (SET_ERROR(EINVAL));
4312 if (strcmp(zc->zc_name, zc->zc_value) != 0) {
4313 err = SET_ERROR(EXDEV);
4315 if (ost == DMU_OST_ZFS && !allow_mounted) {
4316 err = dmu_objset_find(zc->zc_name,
4317 recursive_unmount, pos + 1,
4318 recursive ? DS_FIND_CHILDREN : 0);
4321 err = dsl_dataset_rename_snapshot(zc->zc_name,
4322 pos + 1, pos2 + 1, recursive);
4330 if (ost == DMU_OST_ZVOL)
4331 (void) zvol_remove_minor(zc->zc_name);
4333 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4338 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4340 const char *propname = nvpair_name(pair);
4341 boolean_t issnap = (strchr(dsname, '@') != NULL);
4342 zfs_prop_t prop = zfs_name_to_prop(propname);
4346 if (prop == ZPROP_INVAL) {
4347 if (zfs_prop_user(propname)) {
4348 if (err = zfs_secpolicy_write_perms(dsname,
4349 ZFS_DELEG_PERM_USERPROP, cr))
4354 if (!issnap && zfs_prop_userquota(propname)) {
4355 const char *perm = NULL;
4356 const char *uq_prefix =
4357 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4358 const char *gq_prefix =
4359 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4361 if (strncmp(propname, uq_prefix,
4362 strlen(uq_prefix)) == 0) {
4363 perm = ZFS_DELEG_PERM_USERQUOTA;
4364 } else if (strncmp(propname, gq_prefix,
4365 strlen(gq_prefix)) == 0) {
4366 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4368 /* USERUSED and GROUPUSED are read-only */
4369 return (SET_ERROR(EINVAL));
4372 if (err = zfs_secpolicy_write_perms(dsname, perm, cr))
4377 return (SET_ERROR(EINVAL));
4381 return (SET_ERROR(EINVAL));
4383 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4385 * dsl_prop_get_all_impl() returns properties in this
4389 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4390 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4395 * Check that this value is valid for this pool version
4398 case ZFS_PROP_COMPRESSION:
4400 * If the user specified gzip compression, make sure
4401 * the SPA supports it. We ignore any errors here since
4402 * we'll catch them later.
4404 if (nvpair_value_uint64(pair, &intval) == 0) {
4405 if (intval >= ZIO_COMPRESS_GZIP_1 &&
4406 intval <= ZIO_COMPRESS_GZIP_9 &&
4407 zfs_earlier_version(dsname,
4408 SPA_VERSION_GZIP_COMPRESSION)) {
4409 return (SET_ERROR(ENOTSUP));
4412 if (intval == ZIO_COMPRESS_ZLE &&
4413 zfs_earlier_version(dsname,
4414 SPA_VERSION_ZLE_COMPRESSION))
4415 return (SET_ERROR(ENOTSUP));
4417 if (intval == ZIO_COMPRESS_LZ4) {
4420 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4423 if (!spa_feature_is_enabled(spa,
4424 SPA_FEATURE_LZ4_COMPRESS)) {
4425 spa_close(spa, FTAG);
4426 return (SET_ERROR(ENOTSUP));
4428 spa_close(spa, FTAG);
4432 * If this is a bootable dataset then
4433 * verify that the compression algorithm
4434 * is supported for booting. We must return
4435 * something other than ENOTSUP since it
4436 * implies a downrev pool version.
4438 if (zfs_is_bootfs(dsname) &&
4439 !BOOTFS_COMPRESS_VALID(intval)) {
4440 return (SET_ERROR(ERANGE));
4445 case ZFS_PROP_COPIES:
4446 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4447 return (SET_ERROR(ENOTSUP));
4450 case ZFS_PROP_RECORDSIZE:
4451 /* Record sizes above 128k need the feature to be enabled */
4452 if (nvpair_value_uint64(pair, &intval) == 0 &&
4453 intval > SPA_OLD_MAXBLOCKSIZE) {
4457 * We don't allow setting the property above 1MB,
4458 * unless the tunable has been changed.
4460 if (intval > zfs_max_recordsize ||
4461 intval > SPA_MAXBLOCKSIZE)
4462 return (SET_ERROR(ERANGE));
4464 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4467 if (!spa_feature_is_enabled(spa,
4468 SPA_FEATURE_LARGE_BLOCKS)) {
4469 spa_close(spa, FTAG);
4470 return (SET_ERROR(ENOTSUP));
4472 spa_close(spa, FTAG);
4476 case ZFS_PROP_DNODESIZE:
4477 /* Dnode sizes above 512 need the feature to be enabled */
4478 if (nvpair_value_uint64(pair, &intval) == 0 &&
4479 intval != ZFS_DNSIZE_LEGACY) {
4482 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4485 if (!spa_feature_is_enabled(spa,
4486 SPA_FEATURE_LARGE_DNODE)) {
4487 spa_close(spa, FTAG);
4488 return (SET_ERROR(ENOTSUP));
4490 spa_close(spa, FTAG);
4494 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4496 * This property could require the allocation classes
4497 * feature to be active for setting, however we allow
4498 * it so that tests of settable properties succeed.
4499 * The CLI will issue a warning in this case.
4503 case ZFS_PROP_SHARESMB:
4504 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4505 return (SET_ERROR(ENOTSUP));
4508 case ZFS_PROP_ACLINHERIT:
4509 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4510 nvpair_value_uint64(pair, &intval) == 0) {
4511 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4512 zfs_earlier_version(dsname,
4513 SPA_VERSION_PASSTHROUGH_X))
4514 return (SET_ERROR(ENOTSUP));
4518 case ZFS_PROP_CHECKSUM:
4519 case ZFS_PROP_DEDUP:
4521 spa_feature_t feature;
4524 /* dedup feature version checks */
4525 if (prop == ZFS_PROP_DEDUP &&
4526 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4527 return (SET_ERROR(ENOTSUP));
4529 if (nvpair_value_uint64(pair, &intval) != 0)
4530 return (SET_ERROR(EINVAL));
4532 /* check prop value is enabled in features */
4533 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4534 if (feature == SPA_FEATURE_NONE)
4537 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4540 if (!spa_feature_is_enabled(spa, feature)) {
4541 spa_close(spa, FTAG);
4542 return (SET_ERROR(ENOTSUP));
4544 spa_close(spa, FTAG);
4549 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4553 * Checks for a race condition to make sure we don't increment a feature flag
4557 zfs_prop_activate_feature_check(void *arg, dmu_tx_t *tx)
4559 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4560 spa_feature_t *featurep = arg;
4562 if (!spa_feature_is_active(spa, *featurep))
4565 return (SET_ERROR(EBUSY));
4569 * The callback invoked on feature activation in the sync task caused by
4570 * zfs_prop_activate_feature.
4573 zfs_prop_activate_feature_sync(void *arg, dmu_tx_t *tx)
4575 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
4576 spa_feature_t *featurep = arg;
4578 spa_feature_incr(spa, *featurep, tx);
4582 * Activates a feature on a pool in response to a property setting. This
4583 * creates a new sync task which modifies the pool to reflect the feature
4587 zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature)
4591 /* EBUSY here indicates that the feature is already active */
4592 err = dsl_sync_task(spa_name(spa),
4593 zfs_prop_activate_feature_check, zfs_prop_activate_feature_sync,
4594 &feature, 2, ZFS_SPACE_CHECK_RESERVED);
4596 if (err != 0 && err != EBUSY)
4603 * Removes properties from the given props list that fail permission checks
4604 * needed to clear them and to restore them in case of a receive error. For each
4605 * property, make sure we have both set and inherit permissions.
4607 * Returns the first error encountered if any permission checks fail. If the
4608 * caller provides a non-NULL errlist, it also gives the complete list of names
4609 * of all the properties that failed a permission check along with the
4610 * corresponding error numbers. The caller is responsible for freeing the
4613 * If every property checks out successfully, zero is returned and the list
4614 * pointed at by errlist is NULL.
4617 zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist)
4620 nvpair_t *pair, *next_pair;
4627 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4629 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4630 (void) strcpy(zc->zc_name, dataset);
4631 pair = nvlist_next_nvpair(props, NULL);
4632 while (pair != NULL) {
4633 next_pair = nvlist_next_nvpair(props, pair);
4635 (void) strcpy(zc->zc_value, nvpair_name(pair));
4636 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4637 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4638 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4639 VERIFY(nvlist_add_int32(errors,
4640 zc->zc_value, err) == 0);
4644 kmem_free(zc, sizeof (zfs_cmd_t));
4646 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4647 nvlist_free(errors);
4650 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4653 if (errlist == NULL)
4654 nvlist_free(errors);
4662 propval_equals(nvpair_t *p1, nvpair_t *p2)
4664 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4665 /* dsl_prop_get_all_impl() format */
4667 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4668 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4672 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4674 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4675 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4679 if (nvpair_type(p1) != nvpair_type(p2))
4682 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4683 char *valstr1, *valstr2;
4685 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4686 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4687 return (strcmp(valstr1, valstr2) == 0);
4689 uint64_t intval1, intval2;
4691 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4692 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4693 return (intval1 == intval2);
4698 * Remove properties from props if they are not going to change (as determined
4699 * by comparison with origprops). Remove them from origprops as well, since we
4700 * do not need to clear or restore properties that won't change.
4703 props_reduce(nvlist_t *props, nvlist_t *origprops)
4705 nvpair_t *pair, *next_pair;
4707 if (origprops == NULL)
4708 return; /* all props need to be received */
4710 pair = nvlist_next_nvpair(props, NULL);
4711 while (pair != NULL) {
4712 const char *propname = nvpair_name(pair);
4715 next_pair = nvlist_next_nvpair(props, pair);
4717 if ((nvlist_lookup_nvpair(origprops, propname,
4718 &match) != 0) || !propval_equals(pair, match))
4719 goto next; /* need to set received value */
4721 /* don't clear the existing received value */
4722 (void) nvlist_remove_nvpair(origprops, match);
4723 /* don't bother receiving the property */
4724 (void) nvlist_remove_nvpair(props, pair);
4731 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4732 * For example, refquota cannot be set until after the receipt of a dataset,
4733 * because in replication streams, an older/earlier snapshot may exceed the
4734 * refquota. We want to receive the older/earlier snapshot, but setting
4735 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4736 * the older/earlier snapshot from being received (with EDQUOT).
4738 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4740 * libzfs will need to be judicious handling errors encountered by props
4741 * extracted by this function.
4744 extract_delay_props(nvlist_t *props)
4746 nvlist_t *delayprops;
4747 nvpair_t *nvp, *tmp;
4748 static const zfs_prop_t delayable[] = { ZFS_PROP_REFQUOTA, 0 };
4751 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4753 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4754 nvp = nvlist_next_nvpair(props, nvp)) {
4756 * strcmp() is safe because zfs_prop_to_name() always returns
4759 for (i = 0; delayable[i] != 0; i++) {
4760 if (strcmp(zfs_prop_to_name(delayable[i]),
4761 nvpair_name(nvp)) == 0) {
4765 if (delayable[i] != 0) {
4766 tmp = nvlist_prev_nvpair(props, nvp);
4767 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4768 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4773 if (nvlist_empty(delayprops)) {
4774 nvlist_free(delayprops);
4777 return (delayprops);
4781 static boolean_t zfs_ioc_recv_inject_err;
4786 * zc_name name of containing filesystem
4787 * zc_nvlist_src{_size} nvlist of properties to apply
4788 * zc_value name of snapshot to create
4789 * zc_string name of clone origin (if DRR_FLAG_CLONE)
4790 * zc_cookie file descriptor to recv from
4791 * zc_begin_record the BEGIN record of the stream (not byteswapped)
4792 * zc_guid force flag
4793 * zc_cleanup_fd cleanup-on-exit file descriptor
4794 * zc_action_handle handle for this guid/ds mapping (or zero on first call)
4795 * zc_resumable if data is incomplete assume sender will resume
4798 * zc_cookie number of bytes read
4799 * zc_nvlist_dst{_size} error for each unapplied received property
4800 * zc_obj zprop_errflags_t
4801 * zc_action_handle handle for this guid/ds mapping
4804 zfs_ioc_recv(zfs_cmd_t *zc)
4807 dmu_recv_cookie_t drc;
4808 boolean_t force = (boolean_t)zc->zc_guid;
4811 int props_error = 0;
4814 nvlist_t *props = NULL; /* sent properties */
4815 nvlist_t *origprops = NULL; /* existing properties */
4816 nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4817 char *origin = NULL;
4819 char tofs[ZFS_MAX_DATASET_NAME_LEN];
4820 boolean_t first_recvd_props = B_FALSE;
4822 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4823 strchr(zc->zc_value, '@') == NULL ||
4824 strchr(zc->zc_value, '%'))
4825 return (SET_ERROR(EINVAL));
4827 (void) strcpy(tofs, zc->zc_value);
4828 tosnap = strchr(tofs, '@');
4831 if (zc->zc_nvlist_src != 0 &&
4832 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4833 zc->zc_iflags, &props)) != 0)
4840 fget_read(curthread, fd, &cap_pread_rights, &fp);
4844 return (SET_ERROR(EBADF));
4847 errors = fnvlist_alloc();
4849 if (zc->zc_string[0])
4850 origin = zc->zc_string;
4852 error = dmu_recv_begin(tofs, tosnap,
4853 &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4858 * Set properties before we receive the stream so that they are applied
4859 * to the new data. Note that we must call dmu_recv_stream() if
4860 * dmu_recv_begin() succeeds.
4862 if (props != NULL && !drc.drc_newfs) {
4863 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4864 SPA_VERSION_RECVD_PROPS &&
4865 !dsl_prop_get_hasrecvd(tofs))
4866 first_recvd_props = B_TRUE;
4869 * If new received properties are supplied, they are to
4870 * completely replace the existing received properties, so stash
4871 * away the existing ones.
4873 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4874 nvlist_t *errlist = NULL;
4876 * Don't bother writing a property if its value won't
4877 * change (and avoid the unnecessary security checks).
4879 * The first receive after SPA_VERSION_RECVD_PROPS is a
4880 * special case where we blow away all local properties
4883 if (!first_recvd_props)
4884 props_reduce(props, origprops);
4885 if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4886 (void) nvlist_merge(errors, errlist, 0);
4887 nvlist_free(errlist);
4889 if (clear_received_props(tofs, origprops,
4890 first_recvd_props ? NULL : props) != 0)
4891 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4893 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4897 if (props != NULL) {
4898 props_error = dsl_prop_set_hasrecvd(tofs);
4900 if (props_error == 0) {
4901 delayprops = extract_delay_props(props);
4902 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4908 error = dmu_recv_stream(&drc, fp, &off, zc->zc_cleanup_fd,
4909 &zc->zc_action_handle);
4912 zfsvfs_t *zfsvfs = NULL;
4914 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4919 ds = dmu_objset_ds(zfsvfs->z_os);
4920 error = zfs_suspend_fs(zfsvfs);
4922 * If the suspend fails, then the recv_end will
4923 * likely also fail, and clean up after itself.
4925 end_err = dmu_recv_end(&drc, zfsvfs);
4927 error = zfs_resume_fs(zfsvfs, ds);
4928 error = error ? error : end_err;
4930 VFS_RELE(zfsvfs->z_vfs);
4932 vfs_unbusy(zfsvfs->z_vfs);
4935 error = dmu_recv_end(&drc, NULL);
4938 /* Set delayed properties now, after we're done receiving. */
4939 if (delayprops != NULL && error == 0) {
4940 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4941 delayprops, errors);
4945 if (delayprops != NULL) {
4947 * Merge delayed props back in with initial props, in case
4948 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4949 * we have to make sure clear_received_props() includes
4950 * the delayed properties).
4952 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4953 * using ASSERT() will be just like a VERIFY.
4955 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4956 nvlist_free(delayprops);
4960 * Now that all props, initial and delayed, are set, report the prop
4961 * errors to the caller.
4963 if (zc->zc_nvlist_dst_size != 0 &&
4964 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4965 put_nvlist(zc, errors) != 0)) {
4967 * Caller made zc->zc_nvlist_dst less than the minimum expected
4968 * size or supplied an invalid address.
4970 props_error = SET_ERROR(EINVAL);
4973 zc->zc_cookie = off - fp->f_offset;
4974 if (off >= 0 && off <= MAXOFFSET_T)
4978 if (zfs_ioc_recv_inject_err) {
4979 zfs_ioc_recv_inject_err = B_FALSE;
4985 * On error, restore the original props.
4987 if (error != 0 && props != NULL && !drc.drc_newfs) {
4988 if (clear_received_props(tofs, props, NULL) != 0) {
4990 * We failed to clear the received properties.
4991 * Since we may have left a $recvd value on the
4992 * system, we can't clear the $hasrecvd flag.
4994 zc->zc_obj |= ZPROP_ERR_NORESTORE;
4995 } else if (first_recvd_props) {
4996 dsl_prop_unset_hasrecvd(tofs);
4999 if (origprops == NULL && !drc.drc_newfs) {
5000 /* We failed to stash the original properties. */
5001 zc->zc_obj |= ZPROP_ERR_NORESTORE;
5005 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5006 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5007 * explictly if we're restoring local properties cleared in the
5008 * first new-style receive.
5010 if (origprops != NULL &&
5011 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5012 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5013 origprops, NULL) != 0) {
5015 * We stashed the original properties but failed to
5018 zc->zc_obj |= ZPROP_ERR_NORESTORE;
5023 nvlist_free(origprops);
5024 nvlist_free(errors);
5028 error = props_error;
5035 * zc_name name of snapshot to send
5036 * zc_cookie file descriptor to send stream to
5037 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5038 * zc_sendobj objsetid of snapshot to send
5039 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5040 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5041 * output size in zc_objset_type.
5042 * zc_flags lzc_send_flags
5045 * zc_objset_type estimated size, if zc_guid is set
5047 * NOTE: This is no longer the preferred interface, any new functionality
5048 * should be added to zfs_ioc_send_new() instead.
5051 zfs_ioc_send(zfs_cmd_t *zc)
5055 boolean_t estimate = (zc->zc_guid != 0);
5056 boolean_t embedok = (zc->zc_flags & 0x1);
5057 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5058 boolean_t compressok = (zc->zc_flags & 0x4);
5060 if (zc->zc_obj != 0) {
5062 dsl_dataset_t *tosnap;
5064 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5068 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5070 dsl_pool_rele(dp, FTAG);
5074 if (dsl_dir_is_clone(tosnap->ds_dir))
5076 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5077 dsl_dataset_rele(tosnap, FTAG);
5078 dsl_pool_rele(dp, FTAG);
5083 dsl_dataset_t *tosnap;
5084 dsl_dataset_t *fromsnap = NULL;
5086 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5090 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5092 dsl_pool_rele(dp, FTAG);
5096 if (zc->zc_fromobj != 0) {
5097 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5100 dsl_dataset_rele(tosnap, FTAG);
5101 dsl_pool_rele(dp, FTAG);
5106 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5107 &zc->zc_objset_type);
5109 if (fromsnap != NULL)
5110 dsl_dataset_rele(fromsnap, FTAG);
5111 dsl_dataset_rele(tosnap, FTAG);
5112 dsl_pool_rele(dp, FTAG);
5117 fp = getf(zc->zc_cookie);
5119 fget_write(curthread, zc->zc_cookie, &cap_write_rights, &fp);
5122 return (SET_ERROR(EBADF));
5125 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5126 zc->zc_fromobj, embedok, large_block_ok, compressok,
5128 zc->zc_cookie, fp->f_vnode, &off);
5130 zc->zc_cookie, fp, &off);
5133 if (off >= 0 && off <= MAXOFFSET_T)
5135 releasef(zc->zc_cookie);
5142 * zc_name name of snapshot on which to report progress
5143 * zc_cookie file descriptor of send stream
5146 * zc_cookie number of bytes written in send stream thus far
5149 zfs_ioc_send_progress(zfs_cmd_t *zc)
5153 dmu_sendarg_t *dsp = NULL;
5156 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5160 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5162 dsl_pool_rele(dp, FTAG);
5166 mutex_enter(&ds->ds_sendstream_lock);
5169 * Iterate over all the send streams currently active on this dataset.
5170 * If there's one which matches the specified file descriptor _and_ the
5171 * stream was started by the current process, return the progress of
5174 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5175 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5176 if (dsp->dsa_outfd == zc->zc_cookie &&
5177 dsp->dsa_proc == curproc)
5182 zc->zc_cookie = *(dsp->dsa_off);
5184 error = SET_ERROR(ENOENT);
5186 mutex_exit(&ds->ds_sendstream_lock);
5187 dsl_dataset_rele(ds, FTAG);
5188 dsl_pool_rele(dp, FTAG);
5193 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5197 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5198 &zc->zc_inject_record);
5201 zc->zc_guid = (uint64_t)id;
5207 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5209 return (zio_clear_fault((int)zc->zc_guid));
5213 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5215 int id = (int)zc->zc_guid;
5218 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5219 &zc->zc_inject_record);
5227 zfs_ioc_error_log(zfs_cmd_t *zc)
5231 size_t count = (size_t)zc->zc_nvlist_dst_size;
5233 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5236 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5239 zc->zc_nvlist_dst_size = count;
5241 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5243 spa_close(spa, FTAG);
5249 zfs_ioc_clear(zfs_cmd_t *zc)
5256 * On zpool clear we also fix up missing slogs
5258 mutex_enter(&spa_namespace_lock);
5259 spa = spa_lookup(zc->zc_name);
5261 mutex_exit(&spa_namespace_lock);
5262 return (SET_ERROR(EIO));
5264 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5265 /* we need to let spa_open/spa_load clear the chains */
5266 spa_set_log_state(spa, SPA_LOG_CLEAR);
5268 spa->spa_last_open_failed = 0;
5269 mutex_exit(&spa_namespace_lock);
5271 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5272 error = spa_open(zc->zc_name, &spa, FTAG);
5275 nvlist_t *config = NULL;
5277 if (zc->zc_nvlist_src == 0)
5278 return (SET_ERROR(EINVAL));
5280 if ((error = get_nvlist(zc->zc_nvlist_src,
5281 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5282 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5284 if (config != NULL) {
5287 if ((err = put_nvlist(zc, config)) != 0)
5289 nvlist_free(config);
5291 nvlist_free(policy);
5299 * If multihost is enabled, resuming I/O is unsafe as another
5300 * host may have imported the pool.
5302 if (spa_multihost(spa) && spa_suspended(spa))
5303 return (SET_ERROR(EINVAL));
5305 spa_vdev_state_enter(spa, SCL_NONE);
5307 if (zc->zc_guid == 0) {
5310 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5312 (void) spa_vdev_state_exit(spa, NULL, ENODEV);
5313 spa_close(spa, FTAG);
5314 return (SET_ERROR(ENODEV));
5318 vdev_clear(spa, vd);
5320 (void) spa_vdev_state_exit(spa, NULL, 0);
5323 * Resume any suspended I/Os.
5325 if (zio_resume(spa) != 0)
5326 error = SET_ERROR(EIO);
5328 spa_close(spa, FTAG);
5334 * Reopen all the vdevs associated with the pool.
5337 * "scrub_restart" -> when true and scrub is running, allow to restart
5338 * scrub as the side effect of the reopen (boolean).
5343 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5344 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
5348 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5352 boolean_t scrub_restart = B_TRUE;
5355 scrub_restart = fnvlist_lookup_boolean_value(innvl,
5359 error = spa_open(pool, &spa, FTAG);
5363 spa_vdev_state_enter(spa, SCL_NONE);
5366 * If a resilver is already in progress then set the
5367 * spa_scrub_reopen flag to B_TRUE so that we don't restart
5368 * the scan as a side effect of the reopen. Otherwise, let
5369 * vdev_open() decided if a resilver is required.
5371 spa->spa_scrub_reopen = (!scrub_restart &&
5372 dsl_scan_resilvering(spa->spa_dsl_pool));
5373 vdev_reopen(spa->spa_root_vdev);
5374 spa->spa_scrub_reopen = B_FALSE;
5376 (void) spa_vdev_state_exit(spa, NULL, 0);
5377 spa_close(spa, FTAG);
5383 * zc_name name of filesystem
5386 * zc_string name of conflicting snapshot, if there is one
5389 zfs_ioc_promote(zfs_cmd_t *zc)
5392 dsl_dataset_t *ds, *ods;
5393 char origin[ZFS_MAX_DATASET_NAME_LEN];
5397 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5398 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5399 strchr(zc->zc_name, '%'))
5400 return (SET_ERROR(EINVAL));
5402 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5406 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5408 dsl_pool_rele(dp, FTAG);
5412 if (!dsl_dir_is_clone(ds->ds_dir)) {
5413 dsl_dataset_rele(ds, FTAG);
5414 dsl_pool_rele(dp, FTAG);
5415 return (SET_ERROR(EINVAL));
5418 error = dsl_dataset_hold_obj(dp,
5419 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5421 dsl_dataset_rele(ds, FTAG);
5422 dsl_pool_rele(dp, FTAG);
5426 dsl_dataset_name(ods, origin);
5427 dsl_dataset_rele(ods, FTAG);
5428 dsl_dataset_rele(ds, FTAG);
5429 dsl_pool_rele(dp, FTAG);
5432 * We don't need to unmount *all* the origin fs's snapshots, but
5435 cp = strchr(origin, '@');
5438 (void) dmu_objset_find(origin,
5439 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5440 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5444 * Retrieve a single {user|group}{used|quota}@... property.
5447 * zc_name name of filesystem
5448 * zc_objset_type zfs_userquota_prop_t
5449 * zc_value domain name (eg. "S-1-234-567-89")
5450 * zc_guid RID/UID/GID
5453 * zc_cookie property value
5456 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5461 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5462 return (SET_ERROR(EINVAL));
5464 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5468 error = zfs_userspace_one(zfsvfs,
5469 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5470 zfsvfs_rele(zfsvfs, FTAG);
5477 * zc_name name of filesystem
5478 * zc_cookie zap cursor
5479 * zc_objset_type zfs_userquota_prop_t
5480 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5483 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5484 * zc_cookie zap cursor
5487 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5490 int bufsize = zc->zc_nvlist_dst_size;
5493 return (SET_ERROR(ENOMEM));
5495 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5499 void *buf = kmem_alloc(bufsize, KM_SLEEP);
5501 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
5502 buf, &zc->zc_nvlist_dst_size);
5505 error = ddi_copyout(buf,
5506 (void *)(uintptr_t)zc->zc_nvlist_dst,
5507 zc->zc_nvlist_dst_size, zc->zc_iflags);
5509 kmem_free(buf, bufsize);
5510 zfsvfs_rele(zfsvfs, FTAG);
5517 * zc_name name of filesystem
5523 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5529 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
5530 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
5532 * If userused is not enabled, it may be because the
5533 * objset needs to be closed & reopened (to grow the
5534 * objset_phys_t). Suspend/resume the fs will do that.
5536 dsl_dataset_t *ds, *newds;
5538 ds = dmu_objset_ds(zfsvfs->z_os);
5539 error = zfs_suspend_fs(zfsvfs);
5541 dmu_objset_refresh_ownership(ds, &newds,
5543 error = zfs_resume_fs(zfsvfs, newds);
5547 error = dmu_objset_userspace_upgrade(zfsvfs->z_os);
5549 VFS_RELE(zfsvfs->z_vfs);
5551 vfs_unbusy(zfsvfs->z_vfs);
5554 /* XXX kind of reading contents without owning */
5555 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5559 error = dmu_objset_userspace_upgrade(os);
5560 dmu_objset_rele(os, FTAG);
5568 * We don't want to have a hard dependency
5569 * against some special symbols in sharefs
5570 * nfs, and smbsrv. Determine them if needed when
5571 * the first file system is shared.
5572 * Neither sharefs, nfs or smbsrv are unloadable modules.
5574 int (*znfsexport_fs)(void *arg);
5575 int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t);
5576 int (*zsmbexport_fs)(void *arg, boolean_t add_share);
5578 int zfs_nfsshare_inited;
5579 int zfs_smbshare_inited;
5581 ddi_modhandle_t nfs_mod;
5582 ddi_modhandle_t sharefs_mod;
5583 ddi_modhandle_t smbsrv_mod;
5584 #endif /* illumos */
5585 kmutex_t zfs_share_lock;
5593 ASSERT(MUTEX_HELD(&zfs_share_lock));
5594 /* Both NFS and SMB shares also require sharetab support. */
5595 if (sharefs_mod == NULL && ((sharefs_mod =
5596 ddi_modopen("fs/sharefs",
5597 KRTLD_MODE_FIRST, &error)) == NULL)) {
5598 return (SET_ERROR(ENOSYS));
5600 if (zshare_fs == NULL && ((zshare_fs =
5601 (int (*)(enum sharefs_sys_op, share_t *, uint32_t))
5602 ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) {
5603 return (SET_ERROR(ENOSYS));
5607 #endif /* illumos */
5610 zfs_ioc_share(zfs_cmd_t *zc)
5616 switch (zc->zc_share.z_sharetype) {
5618 case ZFS_UNSHARE_NFS:
5619 if (zfs_nfsshare_inited == 0) {
5620 mutex_enter(&zfs_share_lock);
5621 if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs",
5622 KRTLD_MODE_FIRST, &error)) == NULL)) {
5623 mutex_exit(&zfs_share_lock);
5624 return (SET_ERROR(ENOSYS));
5626 if (znfsexport_fs == NULL &&
5627 ((znfsexport_fs = (int (*)(void *))
5629 "nfs_export", &error)) == NULL)) {
5630 mutex_exit(&zfs_share_lock);
5631 return (SET_ERROR(ENOSYS));
5633 error = zfs_init_sharefs();
5635 mutex_exit(&zfs_share_lock);
5636 return (SET_ERROR(ENOSYS));
5638 zfs_nfsshare_inited = 1;
5639 mutex_exit(&zfs_share_lock);
5643 case ZFS_UNSHARE_SMB:
5644 if (zfs_smbshare_inited == 0) {
5645 mutex_enter(&zfs_share_lock);
5646 if (smbsrv_mod == NULL && ((smbsrv_mod =
5647 ddi_modopen("drv/smbsrv",
5648 KRTLD_MODE_FIRST, &error)) == NULL)) {
5649 mutex_exit(&zfs_share_lock);
5650 return (SET_ERROR(ENOSYS));
5652 if (zsmbexport_fs == NULL && ((zsmbexport_fs =
5653 (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod,
5654 "smb_server_share", &error)) == NULL)) {
5655 mutex_exit(&zfs_share_lock);
5656 return (SET_ERROR(ENOSYS));
5658 error = zfs_init_sharefs();
5660 mutex_exit(&zfs_share_lock);
5661 return (SET_ERROR(ENOSYS));
5663 zfs_smbshare_inited = 1;
5664 mutex_exit(&zfs_share_lock);
5668 return (SET_ERROR(EINVAL));
5671 switch (zc->zc_share.z_sharetype) {
5673 case ZFS_UNSHARE_NFS:
5675 znfsexport_fs((void *)
5676 (uintptr_t)zc->zc_share.z_exportdata))
5680 case ZFS_UNSHARE_SMB:
5681 if (error = zsmbexport_fs((void *)
5682 (uintptr_t)zc->zc_share.z_exportdata,
5683 zc->zc_share.z_sharetype == ZFS_SHARE_SMB ?
5690 opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS ||
5691 zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ?
5692 SHAREFS_ADD : SHAREFS_REMOVE;
5695 * Add or remove share from sharetab
5697 error = zshare_fs(opcode,
5698 (void *)(uintptr_t)zc->zc_share.z_sharedata,
5699 zc->zc_share.z_sharemax);
5703 #else /* !illumos */
5705 #endif /* illumos */
5708 ace_t full_access[] = {
5709 {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0}
5714 * zc_name name of containing filesystem
5715 * zc_obj object # beyond which we want next in-use object #
5718 * zc_obj next in-use object #
5721 zfs_ioc_next_obj(zfs_cmd_t *zc)
5723 objset_t *os = NULL;
5726 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
5730 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
5732 dmu_objset_rele(os, FTAG);
5738 * zc_name name of filesystem
5739 * zc_value prefix name for snapshot
5740 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
5743 * zc_value short name of new snapshot
5746 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
5753 error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
5757 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
5758 (u_longlong_t)ddi_get_lbolt64());
5759 hold_name = kmem_asprintf("%%%s", zc->zc_value);
5761 error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
5764 (void) strcpy(zc->zc_value, snap_name);
5767 zfs_onexit_fd_rele(zc->zc_cleanup_fd);
5773 * zc_name name of "to" snapshot
5774 * zc_value name of "from" snapshot
5775 * zc_cookie file descriptor to write diff data on
5778 * dmu_diff_record_t's to the file descriptor
5781 zfs_ioc_diff(zfs_cmd_t *zc)
5788 fp = getf(zc->zc_cookie);
5790 fget_write(curthread, zc->zc_cookie, &cap_write_rights, &fp);
5793 return (SET_ERROR(EBADF));
5798 error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off);
5800 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
5803 if (off >= 0 && off <= MAXOFFSET_T)
5805 releasef(zc->zc_cookie);
5812 * Remove all ACL files in shares dir
5815 zfs_smb_acl_purge(znode_t *dzp)
5818 zap_attribute_t zap;
5819 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5822 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5823 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5824 zap_cursor_advance(&zc)) {
5825 if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred,
5829 zap_cursor_fini(&zc);
5832 #endif /* illumos */
5835 zfs_ioc_smb_acl(zfs_cmd_t *zc)
5840 vnode_t *resourcevp = NULL;
5849 if ((error = lookupname(zc->zc_value, UIO_SYSSPACE,
5850 NO_FOLLOW, NULL, &vp)) != 0)
5853 /* Now make sure mntpnt and dataset are ZFS */
5855 if (strcmp(vp->v_vfsp->mnt_stat.f_fstypename, "zfs") != 0 ||
5856 (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
5857 zc->zc_name) != 0)) {
5859 return (SET_ERROR(EINVAL));
5863 zfsvfs = dzp->z_zfsvfs;
5867 * Create share dir if its missing.
5869 mutex_enter(&zfsvfs->z_lock);
5870 if (zfsvfs->z_shares_dir == 0) {
5873 tx = dmu_tx_create(zfsvfs->z_os);
5874 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE,
5876 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5877 error = dmu_tx_assign(tx, TXG_WAIT);
5881 error = zfs_create_share_dir(zfsvfs, tx);
5885 mutex_exit(&zfsvfs->z_lock);
5891 mutex_exit(&zfsvfs->z_lock);
5893 ASSERT(zfsvfs->z_shares_dir);
5894 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) {
5900 switch (zc->zc_cookie) {
5901 case ZFS_SMB_ACL_ADD:
5902 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
5903 vattr.va_type = VREG;
5904 vattr.va_mode = S_IFREG|0777;
5908 vsec.vsa_mask = VSA_ACE;
5909 vsec.vsa_aclentp = &full_access;
5910 vsec.vsa_aclentsz = sizeof (full_access);
5911 vsec.vsa_aclcnt = 1;
5913 error = VOP_CREATE(ZTOV(sharedir), zc->zc_string,
5914 &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec);
5916 VN_RELE(resourcevp);
5919 case ZFS_SMB_ACL_REMOVE:
5920 error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred,
5924 case ZFS_SMB_ACL_RENAME:
5925 if ((error = get_nvlist(zc->zc_nvlist_src,
5926 zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) {
5928 VN_RELE(ZTOV(sharedir));
5932 if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) ||
5933 nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET,
5936 VN_RELE(ZTOV(sharedir));
5938 nvlist_free(nvlist);
5941 error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target,
5943 nvlist_free(nvlist);
5946 case ZFS_SMB_ACL_PURGE:
5947 error = zfs_smb_acl_purge(sharedir);
5951 error = SET_ERROR(EINVAL);
5956 VN_RELE(ZTOV(sharedir));
5961 #else /* !illumos */
5962 return (EOPNOTSUPP);
5963 #endif /* illumos */
5968 * "holds" -> { snapname -> holdname (string), ... }
5969 * (optional) "cleanup_fd" -> fd (int32)
5973 * snapname -> error value (int32)
5977 static const zfs_ioc_key_t zfs_keys_hold[] = {
5978 {"holds", DATA_TYPE_NVLIST, 0},
5979 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5984 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
5988 int cleanup_fd = -1;
5992 holds = fnvlist_lookup_nvlist(args, "holds");
5994 /* make sure the user didn't pass us any invalid (empty) tags */
5995 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
5996 pair = nvlist_next_nvpair(holds, pair)) {
5999 error = nvpair_value_string(pair, &htag);
6001 return (SET_ERROR(error));
6003 if (strlen(htag) == 0)
6004 return (SET_ERROR(EINVAL));
6007 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6008 error = zfs_onexit_fd_hold(cleanup_fd, &minor);
6013 error = dsl_dataset_user_hold(holds, minor, errlist);
6015 zfs_onexit_fd_rele(cleanup_fd);
6020 * innvl is not used.
6023 * holdname -> time added (uint64 seconds since epoch)
6027 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6033 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6035 return (dsl_dataset_get_holds(snapname, outnvl));
6040 * snapname -> { holdname, ... }
6045 * snapname -> error value (int32)
6049 static const zfs_ioc_key_t zfs_keys_release[] = {
6050 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6055 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6057 return (dsl_dataset_user_release(holds, errlist));
6062 * zc_name name of new filesystem or snapshot
6063 * zc_value full name of old snapshot
6066 * zc_cookie space in bytes
6067 * zc_objset_type compressed space in bytes
6068 * zc_perm_action uncompressed space in bytes
6071 zfs_ioc_space_written(zfs_cmd_t *zc)
6075 dsl_dataset_t *new, *old;
6077 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6080 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6082 dsl_pool_rele(dp, FTAG);
6085 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6087 dsl_dataset_rele(new, FTAG);
6088 dsl_pool_rele(dp, FTAG);
6092 error = dsl_dataset_space_written(old, new, &zc->zc_cookie,
6093 &zc->zc_objset_type, &zc->zc_perm_action);
6094 dsl_dataset_rele(old, FTAG);
6095 dsl_dataset_rele(new, FTAG);
6096 dsl_pool_rele(dp, FTAG);
6102 * "firstsnap" -> snapshot name
6106 * "used" -> space in bytes
6107 * "compressed" -> compressed space in bytes
6108 * "uncompressed" -> uncompressed space in bytes
6111 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6112 {"firstsnap", DATA_TYPE_STRING, 0},
6116 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6120 dsl_dataset_t *new, *old;
6122 uint64_t used, comp, uncomp;
6124 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6126 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6130 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6131 if (error == 0 && !new->ds_is_snapshot) {
6132 dsl_dataset_rele(new, FTAG);
6133 error = SET_ERROR(EINVAL);
6136 dsl_pool_rele(dp, FTAG);
6139 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6140 if (error == 0 && !old->ds_is_snapshot) {
6141 dsl_dataset_rele(old, FTAG);
6142 error = SET_ERROR(EINVAL);
6145 dsl_dataset_rele(new, FTAG);
6146 dsl_pool_rele(dp, FTAG);
6150 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6151 dsl_dataset_rele(old, FTAG);
6152 dsl_dataset_rele(new, FTAG);
6153 dsl_pool_rele(dp, FTAG);
6154 fnvlist_add_uint64(outnvl, "used", used);
6155 fnvlist_add_uint64(outnvl, "compressed", comp);
6156 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6161 zfs_ioc_jail(zfs_cmd_t *zc)
6164 return (zone_dataset_attach(curthread->td_ucred, zc->zc_name,
6165 (int)zc->zc_jailid));
6169 zfs_ioc_unjail(zfs_cmd_t *zc)
6172 return (zone_dataset_detach(curthread->td_ucred, zc->zc_name,
6173 (int)zc->zc_jailid));
6178 * "fd" -> file descriptor to write stream to (int32)
6179 * (optional) "fromsnap" -> full snap name to send an incremental from
6180 * (optional) "largeblockok" -> (value ignored)
6181 * indicates that blocks > 128KB are permitted
6182 * (optional) "embedok" -> (value ignored)
6183 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6184 * (optional) "compressok" -> (value ignored)
6185 * presence indicates compressed DRR_WRITE records are permitted
6186 * (optional) "resume_object" and "resume_offset" -> (uint64)
6187 * if present, resume send stream from specified object and offset.
6192 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6193 {"fd", DATA_TYPE_INT32, 0},
6194 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6195 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6196 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6197 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6198 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6199 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6200 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6205 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6210 char *fromname = NULL;
6212 boolean_t largeblockok;
6214 boolean_t compressok;
6215 uint64_t resumeobj = 0;
6216 uint64_t resumeoff = 0;
6218 fd = fnvlist_lookup_int32(innvl, "fd");
6220 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6222 largeblockok = nvlist_exists(innvl, "largeblockok");
6223 embedok = nvlist_exists(innvl, "embedok");
6224 compressok = nvlist_exists(innvl, "compressok");
6226 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6227 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6230 file_t *fp = getf(fd);
6232 fget_write(curthread, fd, &cap_write_rights, &fp);
6235 return (SET_ERROR(EBADF));
6238 error = dmu_send(snapname, fromname, embedok, largeblockok, compressok,
6240 fd, resumeobj, resumeoff, fp->f_vnode, &off);
6242 fd, resumeobj, resumeoff, fp, &off);
6246 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
6257 * Determine approximately how large a zfs send stream will be -- the number
6258 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6261 * (optional) "from" -> full snap or bookmark name to send an incremental
6263 * (optional) "largeblockok" -> (value ignored)
6264 * indicates that blocks > 128KB are permitted
6265 * (optional) "embedok" -> (value ignored)
6266 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6267 * (optional) "compressok" -> (value ignored)
6268 * presence indicates compressed DRR_WRITE records are permitted
6272 * "space" -> bytes of space (uint64)
6275 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6276 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6277 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6278 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6279 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6280 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6281 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6285 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6288 dsl_dataset_t *tosnap;
6291 boolean_t compressok;
6294 error = dsl_pool_hold(snapname, FTAG, &dp);
6298 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6300 dsl_pool_rele(dp, FTAG);
6304 compressok = nvlist_exists(innvl, "compressok");
6306 error = nvlist_lookup_string(innvl, "from", &fromname);
6308 if (strchr(fromname, '@') != NULL) {
6310 * If from is a snapshot, hold it and use the more
6311 * efficient dmu_send_estimate to estimate send space
6312 * size using deadlists.
6314 dsl_dataset_t *fromsnap;
6315 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6318 error = dmu_send_estimate(tosnap, fromsnap, compressok,
6320 dsl_dataset_rele(fromsnap, FTAG);
6321 } else if (strchr(fromname, '#') != NULL) {
6323 * If from is a bookmark, fetch the creation TXG of the
6324 * snapshot it was created from and use that to find
6325 * blocks that were born after it.
6327 zfs_bookmark_phys_t frombm;
6329 error = dsl_bookmark_lookup(dp, fromname, tosnap,
6333 error = dmu_send_estimate_from_txg(tosnap,
6334 frombm.zbm_creation_txg, compressok, &space);
6337 * from is not properly formatted as a snapshot or
6340 error = SET_ERROR(EINVAL);
6345 * If estimating the size of a full send, use dmu_send_estimate.
6347 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
6350 fnvlist_add_uint64(outnvl, "space", space);
6353 dsl_dataset_rele(tosnap, FTAG);
6354 dsl_pool_rele(dp, FTAG);
6359 * Sync the currently open TXG to disk for the specified pool.
6360 * This is somewhat similar to 'zfs_sync()'.
6361 * For cases that do not result in error this ioctl will wait for
6362 * the currently open TXG to commit before returning back to the caller.
6365 * "force" -> when true, force uberblock update even if there is no dirty data.
6366 * In addition this will cause the vdev configuration to be written
6367 * out including updating the zpool cache file. (boolean_t)
6372 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6373 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6378 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6384 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6387 force = fnvlist_lookup_boolean_value(innvl, "force");
6389 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6390 vdev_config_dirty(spa->spa_root_vdev);
6391 spa_config_exit(spa, SCL_CONFIG, FTAG);
6393 txg_wait_synced(spa_get_dsl(spa), 0);
6395 spa_close(spa, FTAG);
6400 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6403 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6404 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6405 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6407 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6409 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6410 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6411 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6412 ASSERT3P(vec->zvec_func, ==, NULL);
6414 vec->zvec_legacy_func = func;
6415 vec->zvec_secpolicy = secpolicy;
6416 vec->zvec_namecheck = namecheck;
6417 vec->zvec_allow_log = log_history;
6418 vec->zvec_pool_check = pool_check;
6422 * See the block comment at the beginning of this file for details on
6423 * each argument to this function.
6426 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6427 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6428 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
6429 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6431 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6433 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6434 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6435 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6436 ASSERT3P(vec->zvec_func, ==, NULL);
6438 /* if we are logging, the name must be valid */
6439 ASSERT(!allow_log || namecheck != NO_NAME);
6441 vec->zvec_name = name;
6442 vec->zvec_func = func;
6443 vec->zvec_secpolicy = secpolicy;
6444 vec->zvec_namecheck = namecheck;
6445 vec->zvec_pool_check = pool_check;
6446 vec->zvec_smush_outnvlist = smush_outnvlist;
6447 vec->zvec_allow_log = allow_log;
6448 vec->zvec_nvl_keys = nvl_keys;
6449 vec->zvec_nvl_key_count = num_keys;
6453 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6454 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6455 zfs_ioc_poolcheck_t pool_check)
6457 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6458 POOL_NAME, log_history, pool_check);
6462 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6463 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
6465 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6466 DATASET_NAME, B_FALSE, pool_check);
6470 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6472 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
6473 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6477 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6478 zfs_secpolicy_func_t *secpolicy)
6480 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6481 NO_NAME, B_FALSE, POOL_CHECK_NONE);
6485 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
6486 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
6488 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6489 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6493 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6495 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6496 zfs_secpolicy_read);
6500 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6501 zfs_secpolicy_func_t *secpolicy)
6503 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6504 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6508 zfs_ioctl_init(void)
6510 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
6511 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
6512 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6513 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6515 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
6516 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
6517 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6518 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6520 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
6521 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
6522 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6523 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6525 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
6526 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
6527 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6528 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6530 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
6531 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
6532 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6533 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6535 zfs_ioctl_register("create", ZFS_IOC_CREATE,
6536 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
6537 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6538 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6540 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
6541 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
6542 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6543 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6545 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
6546 zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
6547 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6548 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
6550 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
6551 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
6552 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6553 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6555 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
6556 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
6557 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6558 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
6559 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
6560 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
6561 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6562 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
6564 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
6565 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
6566 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6567 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
6569 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
6570 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
6571 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6572 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
6574 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
6575 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
6576 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6577 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
6579 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
6580 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
6581 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
6582 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
6584 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
6585 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
6587 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6588 zfs_keys_destroy_bookmarks,
6589 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
6591 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
6592 zfs_ioc_channel_program, zfs_secpolicy_config,
6593 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
6594 B_TRUE, zfs_keys_channel_program,
6595 ARRAY_SIZE(zfs_keys_channel_program));
6597 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
6598 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
6599 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6600 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
6602 zfs_ioctl_register("zpool_discard_checkpoint",
6603 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
6604 zfs_secpolicy_config, POOL_NAME,
6605 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6606 zfs_keys_pool_discard_checkpoint,
6607 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
6609 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
6610 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
6611 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
6612 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
6614 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
6615 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
6616 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
6617 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
6618 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
6619 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
6620 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
6622 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
6623 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
6624 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
6625 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
6627 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
6628 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
6629 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
6630 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
6632 /* IOCTLS that use the legacy function signature */
6634 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
6635 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
6637 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
6638 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6639 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
6641 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
6642 zfs_ioc_pool_upgrade);
6643 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
6645 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
6646 zfs_ioc_vdev_remove);
6647 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
6648 zfs_ioc_vdev_set_state);
6649 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
6650 zfs_ioc_vdev_attach);
6651 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
6652 zfs_ioc_vdev_detach);
6653 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
6654 zfs_ioc_vdev_setpath);
6655 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
6656 zfs_ioc_vdev_setfru);
6657 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
6658 zfs_ioc_pool_set_props);
6659 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
6660 zfs_ioc_vdev_split);
6661 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
6662 zfs_ioc_pool_reguid);
6664 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
6665 zfs_ioc_pool_configs, zfs_secpolicy_none);
6666 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
6667 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
6668 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
6669 zfs_ioc_inject_fault, zfs_secpolicy_inject);
6670 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
6671 zfs_ioc_clear_fault, zfs_secpolicy_inject);
6672 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
6673 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
6676 * pool destroy, and export don't log the history as part of
6677 * zfsdev_ioctl, but rather zfs_ioc_pool_export
6678 * does the logging of those commands.
6680 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
6681 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6682 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
6683 zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
6685 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
6686 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6687 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
6688 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
6690 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
6691 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_NONE);
6692 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
6693 zfs_ioc_dsobj_to_dsname,
6694 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_NONE);
6695 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
6696 zfs_ioc_pool_get_history,
6697 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6699 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
6700 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
6702 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
6703 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6705 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
6706 zfs_ioc_space_written);
6707 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
6708 zfs_ioc_objset_recvd_props);
6709 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
6711 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
6713 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
6714 zfs_ioc_objset_stats);
6715 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
6716 zfs_ioc_objset_zplprops);
6717 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
6718 zfs_ioc_dataset_list_next);
6719 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
6720 zfs_ioc_snapshot_list_next);
6721 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
6722 zfs_ioc_send_progress);
6724 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
6725 zfs_ioc_diff, zfs_secpolicy_diff);
6726 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
6727 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
6728 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
6729 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
6730 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
6731 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
6732 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
6733 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
6734 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
6735 zfs_ioc_send, zfs_secpolicy_send);
6737 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
6738 zfs_secpolicy_none);
6739 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
6740 zfs_secpolicy_destroy);
6741 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
6742 zfs_secpolicy_recv);
6743 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
6744 zfs_secpolicy_promote);
6745 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
6746 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
6747 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
6748 zfs_secpolicy_set_fsacl);
6751 * Not using zfs_ioctl_register_dataset_modify as DATASET_NAME check
6752 * won't allow a bookmark name.
6754 zfs_ioctl_register_legacy(ZFS_IOC_RENAME, zfs_ioc_rename,
6755 zfs_secpolicy_rename, ENTITY_NAME, B_TRUE,
6756 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6758 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
6759 zfs_secpolicy_share, POOL_CHECK_NONE);
6760 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
6761 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
6762 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
6763 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
6764 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6765 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
6766 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
6767 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6770 zfs_ioctl_register_dataset_nolog(ZFS_IOC_JAIL, zfs_ioc_jail,
6771 zfs_secpolicy_config, POOL_CHECK_NONE);
6772 zfs_ioctl_register_dataset_nolog(ZFS_IOC_UNJAIL, zfs_ioc_unjail,
6773 zfs_secpolicy_config, POOL_CHECK_NONE);
6774 zfs_ioctl_register("fbsd_nextboot", ZFS_IOC_NEXTBOOT,
6775 zfs_ioc_nextboot, zfs_secpolicy_config, NO_NAME,
6776 POOL_CHECK_NONE, B_FALSE, B_FALSE,
6777 zfs_keys_nextboot, ARRAY_SIZE(zfs_keys_nextboot));
6782 * Verify that for non-legacy ioctls the input nvlist
6783 * pairs match against the expected input.
6785 * Possible errors are:
6786 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
6787 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
6788 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
6791 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
6793 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
6794 boolean_t required_keys_found = B_FALSE;
6797 * examine each input pair
6799 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
6800 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
6801 char *name = nvpair_name(pair);
6802 data_type_t type = nvpair_type(pair);
6803 boolean_t identified = B_FALSE;
6806 * check pair against the documented names and type
6808 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6809 /* if not a wild card name, check for an exact match */
6810 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
6811 strcmp(nvl_keys[k].zkey_name, name) != 0)
6814 identified = B_TRUE;
6816 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
6817 nvl_keys[k].zkey_type != type) {
6818 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
6821 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6824 required_keys_found = B_TRUE;
6828 /* allow an 'optional' key, everything else is invalid */
6830 (strcmp(name, "optional") != 0 ||
6831 type != DATA_TYPE_NVLIST)) {
6832 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
6836 /* verify that all required keys were found */
6837 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
6838 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
6841 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
6842 /* at least one non-optionial key is expected here */
6843 if (!required_keys_found)
6844 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6848 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
6849 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
6856 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
6857 zfs_ioc_poolcheck_t check)
6862 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
6863 type == ENTITY_NAME);
6865 if (check & POOL_CHECK_NONE)
6868 error = spa_open(name, &spa, FTAG);
6870 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
6871 error = SET_ERROR(EAGAIN);
6872 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
6873 error = SET_ERROR(EROFS);
6874 spa_close(spa, FTAG);
6880 * Find a free minor number.
6883 zfsdev_minor_alloc(void)
6885 static minor_t last_minor;
6888 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6890 for (m = last_minor + 1; m != last_minor; m++) {
6891 if (m > ZFSDEV_MAX_MINOR)
6893 if (ddi_get_soft_state(zfsdev_state, m) == NULL) {
6903 zfs_ctldev_init(struct cdev *devp)
6906 zfs_soft_state_t *zs;
6908 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6910 minor = zfsdev_minor_alloc();
6912 return (SET_ERROR(ENXIO));
6914 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS)
6915 return (SET_ERROR(EAGAIN));
6917 devfs_set_cdevpriv((void *)(uintptr_t)minor, zfsdev_close);
6919 zs = ddi_get_soft_state(zfsdev_state, minor);
6920 zs->zss_type = ZSST_CTLDEV;
6921 zfs_onexit_init((zfs_onexit_t **)&zs->zss_data);
6927 zfs_ctldev_destroy(zfs_onexit_t *zo, minor_t minor)
6929 ASSERT(MUTEX_HELD(&spa_namespace_lock));
6931 zfs_onexit_destroy(zo);
6932 ddi_soft_state_free(zfsdev_state, minor);
6936 zfsdev_get_soft_state(minor_t minor, enum zfs_soft_state_type which)
6938 zfs_soft_state_t *zp;
6940 zp = ddi_get_soft_state(zfsdev_state, minor);
6941 if (zp == NULL || zp->zss_type != which)
6944 return (zp->zss_data);
6948 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
6953 if (getminor(*devp) != 0)
6954 return (zvol_open(devp, flag, otyp, cr));
6957 /* This is the control device. Allocate a new minor if requested. */
6959 mutex_enter(&spa_namespace_lock);
6960 error = zfs_ctldev_init(devp);
6961 mutex_exit(&spa_namespace_lock);
6968 zfsdev_close(void *data)
6971 minor_t minor = (minor_t)(uintptr_t)data;
6976 mutex_enter(&spa_namespace_lock);
6977 zo = zfsdev_get_soft_state(minor, ZSST_CTLDEV);
6979 mutex_exit(&spa_namespace_lock);
6982 zfs_ctldev_destroy(zo, minor);
6983 mutex_exit(&spa_namespace_lock);
6987 zfsdev_ioctl(struct cdev *dev, u_long zcmd, caddr_t arg, int flag,
6994 minor_t minor = getminor(dev);
6996 zfs_iocparm_t *zc_iocparm;
6997 int cflag, cmd, oldvecnum;
6998 boolean_t newioc, compat;
6999 void *compat_zc = NULL;
7000 cred_t *cr = td->td_ucred;
7002 const zfs_ioc_vec_t *vec;
7003 char *saved_poolname = NULL;
7004 nvlist_t *innvl = NULL;
7006 cflag = ZFS_CMD_COMPAT_NONE;
7008 newioc = B_TRUE; /* "new" style (zfs_iocparm_t) ioctl */
7010 len = IOCPARM_LEN(zcmd);
7011 vecnum = cmd = zcmd & 0xff;
7014 * Check if we are talking to supported older binaries
7015 * and translate zfs_cmd if necessary
7017 if (len != sizeof(zfs_iocparm_t)) {
7024 case sizeof(zfs_cmd_zcmd_t):
7025 cflag = ZFS_CMD_COMPAT_LZC;
7027 case sizeof(zfs_cmd_deadman_t):
7028 cflag = ZFS_CMD_COMPAT_DEADMAN;
7030 case sizeof(zfs_cmd_v28_t):
7031 cflag = ZFS_CMD_COMPAT_V28;
7033 case sizeof(zfs_cmd_v15_t):
7034 if (cmd >= sizeof(zfs_ioctl_v15_to_v28) /
7035 sizeof(zfs_ioctl_v15_to_v28[0]))
7038 cflag = ZFS_CMD_COMPAT_V15;
7039 vecnum = zfs_ioctl_v15_to_v28[cmd];
7042 * Return without further handling
7043 * if the command is blacklisted.
7045 if (vecnum == ZFS_IOC_COMPAT_PASS)
7047 else if (vecnum == ZFS_IOC_COMPAT_FAIL)
7056 vecnum = cmd - ZFS_IOC_FIRST;
7057 ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip));
7060 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7061 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7062 vec = &zfs_ioc_vec[vecnum];
7064 zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
7067 error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag);
7069 error = SET_ERROR(EFAULT);
7072 #else /* !illumos */
7073 bzero(zc, sizeof(zfs_cmd_t));
7076 zc_iocparm = (void *)arg;
7078 switch (zc_iocparm->zfs_ioctl_version) {
7079 case ZFS_IOCVER_CURRENT:
7080 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_t)) {
7081 error = SET_ERROR(EINVAL);
7085 case ZFS_IOCVER_INLANES:
7086 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_inlanes_t)) {
7087 error = SET_ERROR(EFAULT);
7091 cflag = ZFS_CMD_COMPAT_INLANES;
7093 case ZFS_IOCVER_RESUME:
7094 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_resume_t)) {
7095 error = SET_ERROR(EFAULT);
7099 cflag = ZFS_CMD_COMPAT_RESUME;
7101 case ZFS_IOCVER_EDBP:
7102 if (zc_iocparm->zfs_cmd_size != sizeof(zfs_cmd_edbp_t)) {
7103 error = SET_ERROR(EFAULT);
7107 cflag = ZFS_CMD_COMPAT_EDBP;
7109 case ZFS_IOCVER_ZCMD:
7110 if (zc_iocparm->zfs_cmd_size > sizeof(zfs_cmd_t) ||
7111 zc_iocparm->zfs_cmd_size < sizeof(zfs_cmd_zcmd_t)) {
7112 error = SET_ERROR(EFAULT);
7116 cflag = ZFS_CMD_COMPAT_ZCMD;
7119 error = SET_ERROR(EINVAL);
7125 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
7126 compat_zc = kmem_zalloc(sizeof(zfs_cmd_t), KM_SLEEP);
7127 bzero(compat_zc, sizeof(zfs_cmd_t));
7129 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
7130 compat_zc, zc_iocparm->zfs_cmd_size, flag);
7132 error = SET_ERROR(EFAULT);
7136 error = ddi_copyin((void *)(uintptr_t)zc_iocparm->zfs_cmd,
7137 zc, zc_iocparm->zfs_cmd_size, flag);
7139 error = SET_ERROR(EFAULT);
7147 ASSERT(compat_zc != NULL);
7148 zfs_cmd_compat_get(zc, compat_zc, cflag);
7150 ASSERT(compat_zc == NULL);
7151 zfs_cmd_compat_get(zc, arg, cflag);
7154 error = zfs_ioctl_compat_pre(zc, &vecnum, cflag);
7157 if (oldvecnum != vecnum)
7158 vec = &zfs_ioc_vec[vecnum];
7160 #endif /* !illumos */
7162 zc->zc_iflags = flag & FKIOCTL;
7163 if (zc->zc_nvlist_src_size != 0) {
7164 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7165 zc->zc_iflags, &innvl);
7170 /* rewrite innvl for backwards compatibility */
7172 innvl = zfs_ioctl_compat_innvl(zc, innvl, vecnum, cflag);
7175 * Ensure that all pool/dataset names are valid before we pass down to
7178 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7179 switch (vec->zvec_namecheck) {
7181 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7182 error = SET_ERROR(EINVAL);
7184 error = pool_status_check(zc->zc_name,
7185 vec->zvec_namecheck, vec->zvec_pool_check);
7189 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7190 error = SET_ERROR(EINVAL);
7192 error = pool_status_check(zc->zc_name,
7193 vec->zvec_namecheck, vec->zvec_pool_check);
7197 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7198 error = SET_ERROR(EINVAL);
7200 error = pool_status_check(zc->zc_name,
7201 vec->zvec_namecheck, vec->zvec_pool_check);
7210 * Ensure that all input pairs are valid before we pass them down
7211 * to the lower layers.
7213 * The vectored functions can use fnvlist_lookup_{type} for any
7214 * required pairs since zfs_check_input_nvpairs() confirmed that
7215 * they exist and are of the correct type.
7217 if (error == 0 && vec->zvec_func != NULL) {
7218 error = zfs_check_input_nvpairs(innvl, vec);
7224 error = vec->zvec_secpolicy(zc, innvl, cr);
7229 /* legacy ioctls can modify zc_name */
7230 len = strcspn(zc->zc_name, "/@#") + 1;
7231 saved_poolname = kmem_alloc(len, KM_SLEEP);
7232 (void) strlcpy(saved_poolname, zc->zc_name, len);
7234 if (vec->zvec_func != NULL) {
7238 nvlist_t *lognv = NULL;
7240 ASSERT(vec->zvec_legacy_func == NULL);
7243 * Add the innvl to the lognv before calling the func,
7244 * in case the func changes the innvl.
7246 if (vec->zvec_allow_log) {
7247 lognv = fnvlist_alloc();
7248 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7250 if (!nvlist_empty(innvl)) {
7251 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7256 outnvl = fnvlist_alloc();
7257 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7260 * Some commands can partially execute, modify state, and still
7261 * return an error. In these cases, attempt to record what
7265 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7266 vec->zvec_allow_log &&
7267 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7268 if (!nvlist_empty(outnvl)) {
7269 fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL,
7273 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7276 (void) spa_history_log_nvl(spa, lognv);
7277 spa_close(spa, FTAG);
7279 fnvlist_free(lognv);
7281 /* rewrite outnvl for backwards compatibility */
7283 outnvl = zfs_ioctl_compat_outnvl(zc, outnvl, vecnum,
7286 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7288 if (vec->zvec_smush_outnvlist) {
7289 smusherror = nvlist_smush(outnvl,
7290 zc->zc_nvlist_dst_size);
7292 if (smusherror == 0)
7293 puterror = put_nvlist(zc, outnvl);
7299 nvlist_free(outnvl);
7301 error = vec->zvec_legacy_func(zc);
7307 #if defined(__FreeBSD__) && defined(_KERNEL)
7309 * Wait for ZVOL changes to get applied.
7310 * NB: taskqueue_drain_all() does less than taskq_wait(),
7311 * but enough for what we want.
7312 * And there is no equivalent illumos API.
7317 if (spa_open(saved_poolname, &spa, FTAG) == 0) {
7318 taskqueue_drain_all(
7319 spa->spa_zvol_taskq->tq_queue);
7320 spa_close(spa, FTAG);
7326 rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag);
7327 if (error == 0 && rc != 0)
7328 error = SET_ERROR(EFAULT);
7331 zfs_ioctl_compat_post(zc, cmd, cflag);
7333 ASSERT(compat_zc != NULL);
7334 ASSERT(sizeof(zfs_cmd_t) >= zc_iocparm->zfs_cmd_size);
7336 zfs_cmd_compat_put(zc, compat_zc, vecnum, cflag);
7337 rc = ddi_copyout(compat_zc,
7338 (void *)(uintptr_t)zc_iocparm->zfs_cmd,
7339 zc_iocparm->zfs_cmd_size, flag);
7340 if (error == 0 && rc != 0)
7341 error = SET_ERROR(EFAULT);
7342 kmem_free(compat_zc, sizeof (zfs_cmd_t));
7344 zfs_cmd_compat_put(zc, arg, vecnum, cflag);
7349 rc = ddi_copyout(zc, (void *)(uintptr_t)zc_iocparm->zfs_cmd,
7350 sizeof (zfs_cmd_t), flag);
7351 if (error == 0 && rc != 0)
7352 error = SET_ERROR(EFAULT);
7355 if (error == 0 && vec->zvec_allow_log) {
7356 char *s = tsd_get(zfs_allow_log_key);
7359 (void) tsd_set(zfs_allow_log_key, saved_poolname);
7361 if (saved_poolname != NULL)
7362 strfree(saved_poolname);
7365 kmem_free(zc, sizeof (zfs_cmd_t));
7371 zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7373 if (cmd != DDI_ATTACH)
7374 return (DDI_FAILURE);
7376 if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0,
7377 DDI_PSEUDO, 0) == DDI_FAILURE)
7378 return (DDI_FAILURE);
7382 ddi_report_dev(dip);
7384 return (DDI_SUCCESS);
7388 zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7390 if (spa_busy() || zfs_busy() || zvol_busy())
7391 return (DDI_FAILURE);
7393 if (cmd != DDI_DETACH)
7394 return (DDI_FAILURE);
7398 ddi_prop_remove_all(dip);
7399 ddi_remove_minor_node(dip, NULL);
7401 return (DDI_SUCCESS);
7406 zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7409 case DDI_INFO_DEVT2DEVINFO:
7411 return (DDI_SUCCESS);
7413 case DDI_INFO_DEVT2INSTANCE:
7414 *result = (void *)0;
7415 return (DDI_SUCCESS);
7418 return (DDI_FAILURE);
7420 #endif /* illumos */
7423 * OK, so this is a little weird.
7425 * /dev/zfs is the control node, i.e. minor 0.
7426 * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0.
7428 * /dev/zfs has basically nothing to do except serve up ioctls,
7429 * so most of the standard driver entry points are in zvol.c.
7432 static struct cb_ops zfs_cb_ops = {
7433 zfsdev_open, /* open */
7434 zfsdev_close, /* close */
7435 zvol_strategy, /* strategy */
7437 zvol_dump, /* dump */
7438 zvol_read, /* read */
7439 zvol_write, /* write */
7440 zfsdev_ioctl, /* ioctl */
7444 nochpoll, /* poll */
7445 ddi_prop_op, /* prop_op */
7446 NULL, /* streamtab */
7447 D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */
7448 CB_REV, /* version */
7449 nodev, /* async read */
7450 nodev, /* async write */
7453 static struct dev_ops zfs_dev_ops = {
7454 DEVO_REV, /* version */
7456 zfs_info, /* info */
7457 nulldev, /* identify */
7458 nulldev, /* probe */
7459 zfs_attach, /* attach */
7460 zfs_detach, /* detach */
7462 &zfs_cb_ops, /* driver operations */
7463 NULL, /* no bus operations */
7465 ddi_quiesce_not_needed, /* quiesce */
7468 static struct modldrv zfs_modldrv = {
7474 static struct modlinkage modlinkage = {
7476 (void *)&zfs_modlfs,
7477 (void *)&zfs_modldrv,
7480 #endif /* illumos */
7482 static struct cdevsw zfs_cdevsw = {
7483 .d_version = D_VERSION,
7484 .d_open = zfsdev_open,
7485 .d_ioctl = zfsdev_ioctl,
7486 .d_name = ZFS_DEV_NAME
7490 zfs_allow_log_destroy(void *arg)
7492 char *poolname = arg;
7499 zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
7507 destroy_dev(zfsdev);
7510 static struct root_hold_token *zfs_root_token;
7518 spa_init(FREAD | FWRITE);
7523 if ((error = mod_install(&modlinkage)) != 0) {
7530 tsd_create(&zfs_fsyncer_key, NULL);
7531 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7532 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7534 error = ldi_ident_from_mod(&modlinkage, &zfs_li);
7536 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7546 if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
7547 return (SET_ERROR(EBUSY));
7549 if ((error = mod_remove(&modlinkage)) != 0)
7555 if (zfs_nfsshare_inited)
7556 (void) ddi_modclose(nfs_mod);
7557 if (zfs_smbshare_inited)
7558 (void) ddi_modclose(smbsrv_mod);
7559 if (zfs_nfsshare_inited || zfs_smbshare_inited)
7560 (void) ddi_modclose(sharefs_mod);
7562 tsd_destroy(&zfs_fsyncer_key);
7563 ldi_ident_release(zfs_li);
7565 mutex_destroy(&zfs_share_lock);
7571 _info(struct modinfo *modinfop)
7573 return (mod_info(&modlinkage, modinfop));
7575 #endif /* illumos */
7577 static int zfs__init(void);
7578 static int zfs__fini(void);
7579 static void zfs_shutdown(void *, int);
7581 static eventhandler_tag zfs_shutdown_event_tag;
7584 #define ZFS_MIN_KSTACK_PAGES 4
7592 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
7593 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
7594 "overflow panic!\nPlease consider adding "
7595 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
7596 ZFS_MIN_KSTACK_PAGES);
7599 zfs_root_token = root_mount_hold("ZFS");
7601 mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7603 spa_init(FREAD | FWRITE);
7608 tsd_create(&zfs_fsyncer_key, NULL);
7609 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7610 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7611 tsd_create(&zfs_geom_probe_vdev_key, NULL);
7613 printf("ZFS storage pool version: features support (" SPA_VERSION_STRING ")\n");
7614 root_mount_rel(zfs_root_token);
7624 if (spa_busy() || zfs_busy() || zvol_busy() ||
7625 zio_injection_enabled) {
7634 tsd_destroy(&zfs_fsyncer_key);
7635 tsd_destroy(&rrw_tsd_key);
7636 tsd_destroy(&zfs_allow_log_key);
7638 mutex_destroy(&zfs_share_lock);
7644 zfs_shutdown(void *arg __unused, int howto __unused)
7648 * ZFS fini routines can not properly work in a panic-ed system.
7650 if (!KERNEL_PANICKED())
7656 zfs_modevent(module_t mod, int type, void *unused __unused)
7664 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
7665 shutdown_post_sync, zfs_shutdown, NULL,
7666 SHUTDOWN_PRI_FIRST);
7670 if (err == 0 && zfs_shutdown_event_tag != NULL)
7671 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
7672 zfs_shutdown_event_tag);
7679 return (EOPNOTSUPP);
7682 static moduledata_t zfs_mod = {
7687 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_VFS, SI_ORDER_ANY);
7688 MODULE_VERSION(zfsctrl, 1);
7689 MODULE_DEPEND(zfsctrl, opensolaris, 1, 1, 1);
7690 MODULE_DEPEND(zfsctrl, xdr, 1, 1, 1);
7691 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);
7692 MODULE_DEPEND(zfsctrl, zlib, 1, 1, 1);