ZIL: Call brt_pending_add() replaying TX_CLONE_RANGE
[zfs.git] / module / zfs / zfs_ioctl.c
blob2738385e260b16df9a425a907bed355f866c7257
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Portions Copyright 2011 Martin Matuska
25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek
27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
30 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
37 * Copyright 2017 RackTop Systems.
38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
39 * Copyright (c) 2019 Datto Inc.
40 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
41 * Copyright (c) 2019, 2021, Klara Inc.
42 * Copyright (c) 2019, Allan Jude
46 * ZFS ioctls.
48 * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
49 * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
51 * There are two ways that we handle ioctls: the legacy way where almost
52 * all of the logic is in the ioctl callback, and the new way where most
53 * of the marshalling is handled in the common entry point, zfsdev_ioctl().
55 * Non-legacy ioctls should be registered by calling
56 * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked
57 * from userland by lzc_ioctl().
59 * The registration arguments are as follows:
61 * const char *name
62 * The name of the ioctl. This is used for history logging. If the
63 * ioctl returns successfully (the callback returns 0), and allow_log
64 * is true, then a history log entry will be recorded with the input &
65 * output nvlists. The log entry can be printed with "zpool history -i".
67 * zfs_ioc_t ioc
68 * The ioctl request number, which userland will pass to ioctl(2).
69 * We want newer versions of libzfs and libzfs_core to run against
70 * existing zfs kernel modules (i.e. a deferred reboot after an update).
71 * Therefore the ioctl numbers cannot change from release to release.
73 * zfs_secpolicy_func_t *secpolicy
74 * This function will be called before the zfs_ioc_func_t, to
75 * determine if this operation is permitted. It should return EPERM
76 * on failure, and 0 on success. Checks include determining if the
77 * dataset is visible in this zone, and if the user has either all
78 * zfs privileges in the zone (SYS_MOUNT), or has been granted permission
79 * to do this operation on this dataset with "zfs allow".
81 * zfs_ioc_namecheck_t namecheck
82 * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool
83 * name, a dataset name, or nothing. If the name is not well-formed,
84 * the ioctl will fail and the callback will not be called.
85 * Therefore, the callback can assume that the name is well-formed
86 * (e.g. is null-terminated, doesn't have more than one '@' character,
87 * doesn't have invalid characters).
89 * zfs_ioc_poolcheck_t pool_check
90 * This specifies requirements on the pool state. If the pool does
91 * not meet them (is suspended or is readonly), the ioctl will fail
92 * and the callback will not be called. If any checks are specified
93 * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME.
94 * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED |
95 * POOL_CHECK_READONLY).
97 * zfs_ioc_key_t *nvl_keys
98 * The list of expected/allowable innvl input keys. This list is used
99 * to validate the nvlist input to the ioctl.
101 * boolean_t smush_outnvlist
102 * If smush_outnvlist is true, then the output is presumed to be a
103 * list of errors, and it will be "smushed" down to fit into the
104 * caller's buffer, by removing some entries and replacing them with a
105 * single "N_MORE_ERRORS" entry indicating how many were removed. See
106 * nvlist_smush() for details. If smush_outnvlist is false, and the
107 * outnvlist does not fit into the userland-provided buffer, then the
108 * ioctl will fail with ENOMEM.
110 * zfs_ioc_func_t *func
111 * The callback function that will perform the operation.
113 * The callback should return 0 on success, or an error number on
114 * failure. If the function fails, the userland ioctl will return -1,
115 * and errno will be set to the callback's return value. The callback
116 * will be called with the following arguments:
118 * const char *name
119 * The name of the pool or dataset to operate on, from
120 * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the
121 * expected type (pool, dataset, or none).
123 * nvlist_t *innvl
124 * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or
125 * NULL if no input nvlist was provided. Changes to this nvlist are
126 * ignored. If the input nvlist could not be deserialized, the
127 * ioctl will fail and the callback will not be called.
129 * nvlist_t *outnvl
130 * The output nvlist, initially empty. The callback can fill it in,
131 * and it will be returned to userland by serializing it into
132 * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization
133 * fails (e.g. because the caller didn't supply a large enough
134 * buffer), then the overall ioctl will fail. See the
135 * 'smush_nvlist' argument above for additional behaviors.
137 * There are two typical uses of the output nvlist:
138 * - To return state, e.g. property values. In this case,
139 * smush_outnvlist should be false. If the buffer was not large
140 * enough, the caller will reallocate a larger buffer and try
141 * the ioctl again.
143 * - To return multiple errors from an ioctl which makes on-disk
144 * changes. In this case, smush_outnvlist should be true.
145 * Ioctls which make on-disk modifications should generally not
146 * use the outnvl if they succeed, because the caller can not
147 * distinguish between the operation failing, and
148 * deserialization failing.
150 * IOCTL Interface Errors
152 * The following ioctl input errors can be returned:
153 * ZFS_ERR_IOC_CMD_UNAVAIL the ioctl number is not supported by kernel
154 * ZFS_ERR_IOC_ARG_UNAVAIL an input argument is not supported by kernel
155 * ZFS_ERR_IOC_ARG_REQUIRED a required input argument is missing
156 * ZFS_ERR_IOC_ARG_BADTYPE an input argument has an invalid type
159 #include <sys/types.h>
160 #include <sys/param.h>
161 #include <sys/errno.h>
162 #include <sys/uio_impl.h>
163 #include <sys/file.h>
164 #include <sys/kmem.h>
165 #include <sys/cmn_err.h>
166 #include <sys/stat.h>
167 #include <sys/zfs_ioctl.h>
168 #include <sys/zfs_quota.h>
169 #include <sys/zfs_vfsops.h>
170 #include <sys/zfs_znode.h>
171 #include <sys/zap.h>
172 #include <sys/spa.h>
173 #include <sys/spa_impl.h>
174 #include <sys/vdev.h>
175 #include <sys/vdev_impl.h>
176 #include <sys/dmu.h>
177 #include <sys/dsl_dir.h>
178 #include <sys/dsl_dataset.h>
179 #include <sys/dsl_prop.h>
180 #include <sys/dsl_deleg.h>
181 #include <sys/dmu_objset.h>
182 #include <sys/dmu_impl.h>
183 #include <sys/dmu_redact.h>
184 #include <sys/dmu_tx.h>
185 #include <sys/sunddi.h>
186 #include <sys/policy.h>
187 #include <sys/zone.h>
188 #include <sys/nvpair.h>
189 #include <sys/pathname.h>
190 #include <sys/fs/zfs.h>
191 #include <sys/zfs_ctldir.h>
192 #include <sys/zfs_dir.h>
193 #include <sys/zfs_onexit.h>
194 #include <sys/zvol.h>
195 #include <sys/dsl_scan.h>
196 #include <sys/fm/util.h>
197 #include <sys/dsl_crypt.h>
198 #include <sys/rrwlock.h>
199 #include <sys/zfs_file.h>
201 #include <sys/dmu_recv.h>
202 #include <sys/dmu_send.h>
203 #include <sys/dmu_recv.h>
204 #include <sys/dsl_destroy.h>
205 #include <sys/dsl_bookmark.h>
206 #include <sys/dsl_userhold.h>
207 #include <sys/zfeature.h>
208 #include <sys/zcp.h>
209 #include <sys/zio_checksum.h>
210 #include <sys/vdev_removal.h>
211 #include <sys/vdev_impl.h>
212 #include <sys/vdev_initialize.h>
213 #include <sys/vdev_trim.h>
215 #include "zfs_namecheck.h"
216 #include "zfs_prop.h"
217 #include "zfs_deleg.h"
218 #include "zfs_comutil.h"
220 #include <sys/lua/lua.h>
221 #include <sys/lua/lauxlib.h>
222 #include <sys/zfs_ioctl_impl.h>
224 kmutex_t zfsdev_state_lock;
225 static zfsdev_state_t zfsdev_state_listhead;
228 * Limit maximum nvlist size. We don't want users passing in insane values
229 * for zc->zc_nvlist_src_size, since we will need to allocate that much memory.
230 * Defaults to 0=auto which is handled by platform code.
232 uint64_t zfs_max_nvlist_src_size = 0;
235 * When logging the output nvlist of an ioctl in the on-disk history, limit
236 * the logged size to this many bytes. This must be less than DMU_MAX_ACCESS.
237 * This applies primarily to zfs_ioc_channel_program().
239 static uint64_t zfs_history_output_max = 1024 * 1024;
241 uint_t zfs_fsyncer_key;
242 uint_t zfs_allow_log_key;
244 /* DATA_TYPE_ANY is used when zkey_type can vary. */
245 #define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
247 typedef struct zfs_ioc_vec {
248 zfs_ioc_legacy_func_t *zvec_legacy_func;
249 zfs_ioc_func_t *zvec_func;
250 zfs_secpolicy_func_t *zvec_secpolicy;
251 zfs_ioc_namecheck_t zvec_namecheck;
252 boolean_t zvec_allow_log;
253 zfs_ioc_poolcheck_t zvec_pool_check;
254 boolean_t zvec_smush_outnvlist;
255 const char *zvec_name;
256 const zfs_ioc_key_t *zvec_nvl_keys;
257 size_t zvec_nvl_key_count;
258 } zfs_ioc_vec_t;
260 /* This array is indexed by zfs_userquota_prop_t */
261 static const char *userquota_perms[] = {
262 ZFS_DELEG_PERM_USERUSED,
263 ZFS_DELEG_PERM_USERQUOTA,
264 ZFS_DELEG_PERM_GROUPUSED,
265 ZFS_DELEG_PERM_GROUPQUOTA,
266 ZFS_DELEG_PERM_USEROBJUSED,
267 ZFS_DELEG_PERM_USEROBJQUOTA,
268 ZFS_DELEG_PERM_GROUPOBJUSED,
269 ZFS_DELEG_PERM_GROUPOBJQUOTA,
270 ZFS_DELEG_PERM_PROJECTUSED,
271 ZFS_DELEG_PERM_PROJECTQUOTA,
272 ZFS_DELEG_PERM_PROJECTOBJUSED,
273 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
276 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
277 static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
278 static int zfs_check_settable(const char *name, nvpair_t *property,
279 cred_t *cr);
280 static int zfs_check_clearable(const char *dataset, nvlist_t *props,
281 nvlist_t **errors);
282 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
283 boolean_t *);
284 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
285 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
287 static void
288 history_str_free(char *buf)
290 kmem_free(buf, HIS_MAX_RECORD_LEN);
293 static char *
294 history_str_get(zfs_cmd_t *zc)
296 char *buf;
298 if (zc->zc_history == 0)
299 return (NULL);
301 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
302 if (copyinstr((void *)(uintptr_t)zc->zc_history,
303 buf, HIS_MAX_RECORD_LEN, NULL) != 0) {
304 history_str_free(buf);
305 return (NULL);
308 buf[HIS_MAX_RECORD_LEN -1] = '\0';
310 return (buf);
314 * Return non-zero if the spa version is less than requested version.
316 static int
317 zfs_earlier_version(const char *name, int version)
319 spa_t *spa;
321 if (spa_open(name, &spa, FTAG) == 0) {
322 if (spa_version(spa) < version) {
323 spa_close(spa, FTAG);
324 return (1);
326 spa_close(spa, FTAG);
328 return (0);
332 * Return TRUE if the ZPL version is less than requested version.
334 static boolean_t
335 zpl_earlier_version(const char *name, int version)
337 objset_t *os;
338 boolean_t rc = B_TRUE;
340 if (dmu_objset_hold(name, FTAG, &os) == 0) {
341 uint64_t zplversion;
343 if (dmu_objset_type(os) != DMU_OST_ZFS) {
344 dmu_objset_rele(os, FTAG);
345 return (B_TRUE);
347 /* XXX reading from non-owned objset */
348 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
349 rc = zplversion < version;
350 dmu_objset_rele(os, FTAG);
352 return (rc);
355 static void
356 zfs_log_history(zfs_cmd_t *zc)
358 spa_t *spa;
359 char *buf;
361 if ((buf = history_str_get(zc)) == NULL)
362 return;
364 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
365 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
366 (void) spa_history_log(spa, buf);
367 spa_close(spa, FTAG);
369 history_str_free(buf);
373 * Policy for top-level read operations (list pools). Requires no privileges,
374 * and can be used in the local zone, as there is no associated dataset.
376 static int
377 zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
379 (void) zc, (void) innvl, (void) cr;
380 return (0);
384 * Policy for dataset read operations (list children, get statistics). Requires
385 * no privileges, but must be visible in the local zone.
387 static int
388 zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
390 (void) innvl, (void) cr;
391 if (INGLOBALZONE(curproc) ||
392 zone_dataset_visible(zc->zc_name, NULL))
393 return (0);
395 return (SET_ERROR(ENOENT));
398 static int
399 zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
401 int writable = 1;
404 * The dataset must be visible by this zone -- check this first
405 * so they don't see EPERM on something they shouldn't know about.
407 if (!INGLOBALZONE(curproc) &&
408 !zone_dataset_visible(dataset, &writable))
409 return (SET_ERROR(ENOENT));
411 if (INGLOBALZONE(curproc)) {
413 * If the fs is zoned, only root can access it from the
414 * global zone.
416 if (secpolicy_zfs(cr) && zoned)
417 return (SET_ERROR(EPERM));
418 } else {
420 * If we are in a local zone, the 'zoned' property must be set.
422 if (!zoned)
423 return (SET_ERROR(EPERM));
425 /* must be writable by this zone */
426 if (!writable)
427 return (SET_ERROR(EPERM));
429 return (0);
432 static int
433 zfs_dozonecheck(const char *dataset, cred_t *cr)
435 uint64_t zoned;
437 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED),
438 &zoned, NULL))
439 return (SET_ERROR(ENOENT));
441 return (zfs_dozonecheck_impl(dataset, zoned, cr));
444 static int
445 zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
447 uint64_t zoned;
449 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned))
450 return (SET_ERROR(ENOENT));
452 return (zfs_dozonecheck_impl(dataset, zoned, cr));
455 static int
456 zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
457 const char *perm, cred_t *cr)
459 int error;
461 error = zfs_dozonecheck_ds(name, ds, cr);
462 if (error == 0) {
463 error = secpolicy_zfs(cr);
464 if (error != 0)
465 error = dsl_deleg_access_impl(ds, perm, cr);
467 return (error);
470 static int
471 zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
473 int error;
474 dsl_dataset_t *ds;
475 dsl_pool_t *dp;
478 * First do a quick check for root in the global zone, which
479 * is allowed to do all write_perms. This ensures that zfs_ioc_*
480 * will get to handle nonexistent datasets.
482 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
483 return (0);
485 error = dsl_pool_hold(name, FTAG, &dp);
486 if (error != 0)
487 return (error);
489 error = dsl_dataset_hold(dp, name, FTAG, &ds);
490 if (error != 0) {
491 dsl_pool_rele(dp, FTAG);
492 return (error);
495 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
497 dsl_dataset_rele(ds, FTAG);
498 dsl_pool_rele(dp, FTAG);
499 return (error);
503 * Policy for setting the security label property.
505 * Returns 0 for success, non-zero for access and other errors.
507 static int
508 zfs_set_slabel_policy(const char *name, const char *strval, cred_t *cr)
510 #ifdef HAVE_MLSLABEL
511 char ds_hexsl[MAXNAMELEN];
512 bslabel_t ds_sl, new_sl;
513 boolean_t new_default = FALSE;
514 uint64_t zoned;
515 int needed_priv = -1;
516 int error;
518 /* First get the existing dataset label. */
519 error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
520 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
521 if (error != 0)
522 return (SET_ERROR(EPERM));
524 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
525 new_default = TRUE;
527 /* The label must be translatable */
528 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
529 return (SET_ERROR(EINVAL));
532 * In a non-global zone, disallow attempts to set a label that
533 * doesn't match that of the zone; otherwise no other checks
534 * are needed.
536 if (!INGLOBALZONE(curproc)) {
537 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
538 return (SET_ERROR(EPERM));
539 return (0);
543 * For global-zone datasets (i.e., those whose zoned property is
544 * "off", verify that the specified new label is valid for the
545 * global zone.
547 if (dsl_prop_get_integer(name,
548 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
549 return (SET_ERROR(EPERM));
550 if (!zoned) {
551 if (zfs_check_global_label(name, strval) != 0)
552 return (SET_ERROR(EPERM));
556 * If the existing dataset label is nondefault, check if the
557 * dataset is mounted (label cannot be changed while mounted).
558 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
559 * mounted (or isn't a dataset, doesn't exist, ...).
561 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
562 objset_t *os;
563 static const char *setsl_tag = "setsl_tag";
566 * Try to own the dataset; abort if there is any error,
567 * (e.g., already mounted, in use, or other error).
569 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
570 setsl_tag, &os);
571 if (error != 0)
572 return (SET_ERROR(EPERM));
574 dmu_objset_disown(os, B_TRUE, setsl_tag);
576 if (new_default) {
577 needed_priv = PRIV_FILE_DOWNGRADE_SL;
578 goto out_check;
581 if (hexstr_to_label(strval, &new_sl) != 0)
582 return (SET_ERROR(EPERM));
584 if (blstrictdom(&ds_sl, &new_sl))
585 needed_priv = PRIV_FILE_DOWNGRADE_SL;
586 else if (blstrictdom(&new_sl, &ds_sl))
587 needed_priv = PRIV_FILE_UPGRADE_SL;
588 } else {
589 /* dataset currently has a default label */
590 if (!new_default)
591 needed_priv = PRIV_FILE_UPGRADE_SL;
594 out_check:
595 if (needed_priv != -1)
596 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
597 return (0);
598 #else
599 return (SET_ERROR(ENOTSUP));
600 #endif /* HAVE_MLSLABEL */
603 static int
604 zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
605 cred_t *cr)
607 const char *strval;
610 * Check permissions for special properties.
612 switch (prop) {
613 default:
614 break;
615 case ZFS_PROP_ZONED:
617 * Disallow setting of 'zoned' from within a local zone.
619 if (!INGLOBALZONE(curproc))
620 return (SET_ERROR(EPERM));
621 break;
623 case ZFS_PROP_QUOTA:
624 case ZFS_PROP_FILESYSTEM_LIMIT:
625 case ZFS_PROP_SNAPSHOT_LIMIT:
626 if (!INGLOBALZONE(curproc)) {
627 uint64_t zoned;
628 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
630 * Unprivileged users are allowed to modify the
631 * limit on things *under* (ie. contained by)
632 * the thing they own.
634 if (dsl_prop_get_integer(dsname,
635 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint))
636 return (SET_ERROR(EPERM));
637 if (!zoned || strlen(dsname) <= strlen(setpoint))
638 return (SET_ERROR(EPERM));
640 break;
642 case ZFS_PROP_MLSLABEL:
643 if (!is_system_labeled())
644 return (SET_ERROR(EPERM));
646 if (nvpair_value_string(propval, &strval) == 0) {
647 int err;
649 err = zfs_set_slabel_policy(dsname, strval, CRED());
650 if (err != 0)
651 return (err);
653 break;
656 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
659 static int
660 zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
663 * permission to set permissions will be evaluated later in
664 * dsl_deleg_can_allow()
666 (void) innvl;
667 return (zfs_dozonecheck(zc->zc_name, cr));
670 static int
671 zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
673 (void) innvl;
674 return (zfs_secpolicy_write_perms(zc->zc_name,
675 ZFS_DELEG_PERM_ROLLBACK, cr));
678 static int
679 zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
681 (void) innvl;
682 dsl_pool_t *dp;
683 dsl_dataset_t *ds;
684 const char *cp;
685 int error;
688 * Generate the current snapshot name from the given objsetid, then
689 * use that name for the secpolicy/zone checks.
691 cp = strchr(zc->zc_name, '@');
692 if (cp == NULL)
693 return (SET_ERROR(EINVAL));
694 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
695 if (error != 0)
696 return (error);
698 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
699 if (error != 0) {
700 dsl_pool_rele(dp, FTAG);
701 return (error);
704 dsl_dataset_name(ds, zc->zc_name);
706 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
707 ZFS_DELEG_PERM_SEND, cr);
708 dsl_dataset_rele(ds, FTAG);
709 dsl_pool_rele(dp, FTAG);
711 return (error);
714 static int
715 zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
717 (void) innvl;
718 return (zfs_secpolicy_write_perms(zc->zc_name,
719 ZFS_DELEG_PERM_SEND, cr));
722 static int
723 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
725 (void) zc, (void) innvl, (void) cr;
726 return (SET_ERROR(ENOTSUP));
729 static int
730 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
732 (void) zc, (void) innvl, (void) cr;
733 return (SET_ERROR(ENOTSUP));
736 static int
737 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
739 char *cp;
742 * Remove the @bla or /bla from the end of the name to get the parent.
744 (void) strlcpy(parent, datasetname, parentsize);
745 cp = strrchr(parent, '@');
746 if (cp != NULL) {
747 cp[0] = '\0';
748 } else {
749 cp = strrchr(parent, '/');
750 if (cp == NULL)
751 return (SET_ERROR(ENOENT));
752 cp[0] = '\0';
755 return (0);
759 zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
761 int error;
763 if ((error = zfs_secpolicy_write_perms(name,
764 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
765 return (error);
767 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
770 static int
771 zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
773 (void) innvl;
774 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
778 * Destroying snapshots with delegated permissions requires
779 * descendant mount and destroy permissions.
781 static int
782 zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
784 (void) zc;
785 nvlist_t *snaps;
786 nvpair_t *pair, *nextpair;
787 int error = 0;
789 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
791 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
792 pair = nextpair) {
793 nextpair = nvlist_next_nvpair(snaps, pair);
794 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
795 if (error == ENOENT) {
797 * Ignore any snapshots that don't exist (we consider
798 * them "already destroyed"). Remove the name from the
799 * nvl here in case the snapshot is created between
800 * now and when we try to destroy it (in which case
801 * we don't want to destroy it since we haven't
802 * checked for permission).
804 fnvlist_remove_nvpair(snaps, pair);
805 error = 0;
807 if (error != 0)
808 break;
811 return (error);
815 zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
817 char parentname[ZFS_MAX_DATASET_NAME_LEN];
818 int error;
820 if ((error = zfs_secpolicy_write_perms(from,
821 ZFS_DELEG_PERM_RENAME, cr)) != 0)
822 return (error);
824 if ((error = zfs_secpolicy_write_perms(from,
825 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
826 return (error);
828 if ((error = zfs_get_parent(to, parentname,
829 sizeof (parentname))) != 0)
830 return (error);
832 if ((error = zfs_secpolicy_write_perms(parentname,
833 ZFS_DELEG_PERM_CREATE, cr)) != 0)
834 return (error);
836 if ((error = zfs_secpolicy_write_perms(parentname,
837 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
838 return (error);
840 return (error);
843 static int
844 zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
846 (void) innvl;
847 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
850 static int
851 zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
853 (void) innvl;
854 dsl_pool_t *dp;
855 dsl_dataset_t *clone;
856 int error;
858 error = zfs_secpolicy_write_perms(zc->zc_name,
859 ZFS_DELEG_PERM_PROMOTE, cr);
860 if (error != 0)
861 return (error);
863 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
864 if (error != 0)
865 return (error);
867 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
869 if (error == 0) {
870 char parentname[ZFS_MAX_DATASET_NAME_LEN];
871 dsl_dataset_t *origin = NULL;
872 dsl_dir_t *dd;
873 dd = clone->ds_dir;
875 error = dsl_dataset_hold_obj(dd->dd_pool,
876 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
877 if (error != 0) {
878 dsl_dataset_rele(clone, FTAG);
879 dsl_pool_rele(dp, FTAG);
880 return (error);
883 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
884 ZFS_DELEG_PERM_MOUNT, cr);
886 dsl_dataset_name(origin, parentname);
887 if (error == 0) {
888 error = zfs_secpolicy_write_perms_ds(parentname, origin,
889 ZFS_DELEG_PERM_PROMOTE, cr);
891 dsl_dataset_rele(clone, FTAG);
892 dsl_dataset_rele(origin, FTAG);
894 dsl_pool_rele(dp, FTAG);
895 return (error);
898 static int
899 zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
901 (void) innvl;
902 int error;
904 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
905 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
906 return (error);
908 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
909 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
910 return (error);
912 return (zfs_secpolicy_write_perms(zc->zc_name,
913 ZFS_DELEG_PERM_CREATE, cr));
917 zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
919 return (zfs_secpolicy_write_perms(name,
920 ZFS_DELEG_PERM_SNAPSHOT, cr));
924 * Check for permission to create each snapshot in the nvlist.
926 static int
927 zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
929 (void) zc;
930 nvlist_t *snaps;
931 int error = 0;
932 nvpair_t *pair;
934 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
936 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
937 pair = nvlist_next_nvpair(snaps, pair)) {
938 char *name = (char *)nvpair_name(pair);
939 char *atp = strchr(name, '@');
941 if (atp == NULL) {
942 error = SET_ERROR(EINVAL);
943 break;
945 *atp = '\0';
946 error = zfs_secpolicy_snapshot_perms(name, cr);
947 *atp = '@';
948 if (error != 0)
949 break;
951 return (error);
955 * Check for permission to create each bookmark in the nvlist.
957 static int
958 zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
960 (void) zc;
961 int error = 0;
963 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
964 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
965 char *name = (char *)nvpair_name(pair);
966 char *hashp = strchr(name, '#');
968 if (hashp == NULL) {
969 error = SET_ERROR(EINVAL);
970 break;
972 *hashp = '\0';
973 error = zfs_secpolicy_write_perms(name,
974 ZFS_DELEG_PERM_BOOKMARK, cr);
975 *hashp = '#';
976 if (error != 0)
977 break;
979 return (error);
982 static int
983 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
985 (void) zc;
986 nvpair_t *pair, *nextpair;
987 int error = 0;
989 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
990 pair = nextpair) {
991 char *name = (char *)nvpair_name(pair);
992 char *hashp = strchr(name, '#');
993 nextpair = nvlist_next_nvpair(innvl, pair);
995 if (hashp == NULL) {
996 error = SET_ERROR(EINVAL);
997 break;
1000 *hashp = '\0';
1001 error = zfs_secpolicy_write_perms(name,
1002 ZFS_DELEG_PERM_DESTROY, cr);
1003 *hashp = '#';
1004 if (error == ENOENT) {
1006 * Ignore any filesystems that don't exist (we consider
1007 * their bookmarks "already destroyed"). Remove
1008 * the name from the nvl here in case the filesystem
1009 * is created between now and when we try to destroy
1010 * the bookmark (in which case we don't want to
1011 * destroy it since we haven't checked for permission).
1013 fnvlist_remove_nvpair(innvl, pair);
1014 error = 0;
1016 if (error != 0)
1017 break;
1020 return (error);
1023 static int
1024 zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1026 (void) zc, (void) innvl, (void) cr;
1028 * Even root must have a proper TSD so that we know what pool
1029 * to log to.
1031 if (tsd_get(zfs_allow_log_key) == NULL)
1032 return (SET_ERROR(EPERM));
1033 return (0);
1036 static int
1037 zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1039 char parentname[ZFS_MAX_DATASET_NAME_LEN];
1040 int error;
1041 const char *origin;
1043 if ((error = zfs_get_parent(zc->zc_name, parentname,
1044 sizeof (parentname))) != 0)
1045 return (error);
1047 if (nvlist_lookup_string(innvl, "origin", &origin) == 0 &&
1048 (error = zfs_secpolicy_write_perms(origin,
1049 ZFS_DELEG_PERM_CLONE, cr)) != 0)
1050 return (error);
1052 if ((error = zfs_secpolicy_write_perms(parentname,
1053 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1054 return (error);
1056 return (zfs_secpolicy_write_perms(parentname,
1057 ZFS_DELEG_PERM_MOUNT, cr));
1061 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1062 * SYS_CONFIG privilege, which is not available in a local zone.
1065 zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1067 (void) zc, (void) innvl;
1069 if (secpolicy_sys_config(cr, B_FALSE) != 0)
1070 return (SET_ERROR(EPERM));
1072 return (0);
1076 * Policy for object to name lookups.
1078 static int
1079 zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1081 (void) innvl;
1082 int error;
1084 if (secpolicy_sys_config(cr, B_FALSE) == 0)
1085 return (0);
1087 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1088 return (error);
1092 * Policy for fault injection. Requires all privileges.
1094 static int
1095 zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1097 (void) zc, (void) innvl;
1098 return (secpolicy_zinject(cr));
1101 static int
1102 zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1104 (void) innvl;
1105 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1107 if (prop == ZPROP_USERPROP) {
1108 if (!zfs_prop_user(zc->zc_value))
1109 return (SET_ERROR(EINVAL));
1110 return (zfs_secpolicy_write_perms(zc->zc_name,
1111 ZFS_DELEG_PERM_USERPROP, cr));
1112 } else {
1113 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1114 NULL, cr));
1118 static int
1119 zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1121 int err = zfs_secpolicy_read(zc, innvl, cr);
1122 if (err)
1123 return (err);
1125 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1126 return (SET_ERROR(EINVAL));
1128 if (zc->zc_value[0] == 0) {
1130 * They are asking about a posix uid/gid. If it's
1131 * themself, allow it.
1133 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1134 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1135 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1136 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
1137 if (zc->zc_guid == crgetuid(cr))
1138 return (0);
1139 } else if (zc->zc_objset_type == ZFS_PROP_GROUPUSED ||
1140 zc->zc_objset_type == ZFS_PROP_GROUPQUOTA ||
1141 zc->zc_objset_type == ZFS_PROP_GROUPOBJUSED ||
1142 zc->zc_objset_type == ZFS_PROP_GROUPOBJQUOTA) {
1143 if (groupmember(zc->zc_guid, cr))
1144 return (0);
1146 /* else is for project quota/used */
1149 return (zfs_secpolicy_write_perms(zc->zc_name,
1150 userquota_perms[zc->zc_objset_type], cr));
1153 static int
1154 zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1156 int err = zfs_secpolicy_read(zc, innvl, cr);
1157 if (err)
1158 return (err);
1160 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
1161 return (SET_ERROR(EINVAL));
1163 return (zfs_secpolicy_write_perms(zc->zc_name,
1164 userquota_perms[zc->zc_objset_type], cr));
1167 static int
1168 zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1170 (void) innvl;
1171 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1172 NULL, cr));
1175 static int
1176 zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1178 (void) zc;
1179 nvpair_t *pair;
1180 nvlist_t *holds;
1181 int error;
1183 holds = fnvlist_lookup_nvlist(innvl, "holds");
1185 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1186 pair = nvlist_next_nvpair(holds, pair)) {
1187 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1188 error = dmu_fsname(nvpair_name(pair), fsname);
1189 if (error != 0)
1190 return (error);
1191 error = zfs_secpolicy_write_perms(fsname,
1192 ZFS_DELEG_PERM_HOLD, cr);
1193 if (error != 0)
1194 return (error);
1196 return (0);
1199 static int
1200 zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1202 (void) zc;
1203 nvpair_t *pair;
1204 int error;
1206 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1207 pair = nvlist_next_nvpair(innvl, pair)) {
1208 char fsname[ZFS_MAX_DATASET_NAME_LEN];
1209 error = dmu_fsname(nvpair_name(pair), fsname);
1210 if (error != 0)
1211 return (error);
1212 error = zfs_secpolicy_write_perms(fsname,
1213 ZFS_DELEG_PERM_RELEASE, cr);
1214 if (error != 0)
1215 return (error);
1217 return (0);
1221 * Policy for allowing temporary snapshots to be taken or released
1223 static int
1224 zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1227 * A temporary snapshot is the same as a snapshot,
1228 * hold, destroy and release all rolled into one.
1229 * Delegated diff alone is sufficient that we allow this.
1231 int error;
1233 if (zfs_secpolicy_write_perms(zc->zc_name,
1234 ZFS_DELEG_PERM_DIFF, cr) == 0)
1235 return (0);
1237 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
1239 if (innvl != NULL) {
1240 if (error == 0)
1241 error = zfs_secpolicy_hold(zc, innvl, cr);
1242 if (error == 0)
1243 error = zfs_secpolicy_release(zc, innvl, cr);
1244 if (error == 0)
1245 error = zfs_secpolicy_destroy(zc, innvl, cr);
1247 return (error);
1250 static int
1251 zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1253 return (zfs_secpolicy_write_perms(zc->zc_name,
1254 ZFS_DELEG_PERM_LOAD_KEY, cr));
1257 static int
1258 zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1260 return (zfs_secpolicy_write_perms(zc->zc_name,
1261 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1265 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1267 static int
1268 get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
1270 char *packed;
1271 int error;
1272 nvlist_t *list = NULL;
1275 * Read in and unpack the user-supplied nvlist.
1277 if (size == 0)
1278 return (SET_ERROR(EINVAL));
1280 packed = vmem_alloc(size, KM_SLEEP);
1282 if (ddi_copyin((void *)(uintptr_t)nvl, packed, size, iflag) != 0) {
1283 vmem_free(packed, size);
1284 return (SET_ERROR(EFAULT));
1287 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
1288 vmem_free(packed, size);
1289 return (error);
1292 vmem_free(packed, size);
1294 *nvp = list;
1295 return (0);
1299 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1300 * Entries will be removed from the end of the nvlist, and one int32 entry
1301 * named "N_MORE_ERRORS" will be added indicating how many entries were
1302 * removed.
1304 static int
1305 nvlist_smush(nvlist_t *errors, size_t max)
1307 size_t size;
1309 size = fnvlist_size(errors);
1311 if (size > max) {
1312 nvpair_t *more_errors;
1313 int n = 0;
1315 if (max < 1024)
1316 return (SET_ERROR(ENOMEM));
1318 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1319 more_errors = nvlist_prev_nvpair(errors, NULL);
1321 do {
1322 nvpair_t *pair = nvlist_prev_nvpair(errors,
1323 more_errors);
1324 fnvlist_remove_nvpair(errors, pair);
1325 n++;
1326 size = fnvlist_size(errors);
1327 } while (size > max);
1329 fnvlist_remove_nvpair(errors, more_errors);
1330 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1331 ASSERT3U(fnvlist_size(errors), <=, max);
1334 return (0);
1337 static int
1338 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1340 char *packed = NULL;
1341 int error = 0;
1342 size_t size;
1344 size = fnvlist_size(nvl);
1346 if (size > zc->zc_nvlist_dst_size) {
1347 error = SET_ERROR(ENOMEM);
1348 } else {
1349 packed = fnvlist_pack(nvl, &size);
1350 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1351 size, zc->zc_iflags) != 0)
1352 error = SET_ERROR(EFAULT);
1353 fnvlist_pack_free(packed, size);
1356 zc->zc_nvlist_dst_size = size;
1357 zc->zc_nvlist_dst_filled = B_TRUE;
1358 return (error);
1362 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1364 int error = 0;
1365 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1366 return (SET_ERROR(EINVAL));
1369 mutex_enter(&os->os_user_ptr_lock);
1370 *zfvp = dmu_objset_get_user(os);
1371 /* bump s_active only when non-zero to prevent umount race */
1372 error = zfs_vfs_ref(zfvp);
1373 mutex_exit(&os->os_user_ptr_lock);
1374 return (error);
1378 getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1380 objset_t *os;
1381 int error;
1383 error = dmu_objset_hold(dsname, FTAG, &os);
1384 if (error != 0)
1385 return (error);
1387 error = getzfsvfs_impl(os, zfvp);
1388 dmu_objset_rele(os, FTAG);
1389 return (error);
1393 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
1394 * case its z_sb will be NULL, and it will be opened as the owner.
1395 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1396 * which prevents all inode ops from running.
1398 static int
1399 zfsvfs_hold(const char *name, const void *tag, zfsvfs_t **zfvp,
1400 boolean_t writer)
1402 int error = 0;
1404 if (getzfsvfs(name, zfvp) != 0)
1405 error = zfsvfs_create(name, B_FALSE, zfvp);
1406 if (error == 0) {
1407 if (writer)
1408 ZFS_TEARDOWN_ENTER_WRITE(*zfvp, tag);
1409 else
1410 ZFS_TEARDOWN_ENTER_READ(*zfvp, tag);
1411 if ((*zfvp)->z_unmounted) {
1413 * XXX we could probably try again, since the unmounting
1414 * thread should be just about to disassociate the
1415 * objset from the zfsvfs.
1417 ZFS_TEARDOWN_EXIT(*zfvp, tag);
1418 return (SET_ERROR(EBUSY));
1421 return (error);
1424 static void
1425 zfsvfs_rele(zfsvfs_t *zfsvfs, const void *tag)
1427 ZFS_TEARDOWN_EXIT(zfsvfs, tag);
1429 if (zfs_vfs_held(zfsvfs)) {
1430 zfs_vfs_rele(zfsvfs);
1431 } else {
1432 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
1433 zfsvfs_free(zfsvfs);
1437 static int
1438 zfs_ioc_pool_create(zfs_cmd_t *zc)
1440 int error;
1441 nvlist_t *config, *props = NULL;
1442 nvlist_t *rootprops = NULL;
1443 nvlist_t *zplprops = NULL;
1444 dsl_crypto_params_t *dcp = NULL;
1445 const char *spa_name = zc->zc_name;
1446 boolean_t unload_wkey = B_TRUE;
1448 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1449 zc->zc_iflags, &config)))
1450 return (error);
1452 if (zc->zc_nvlist_src_size != 0 && (error =
1453 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1454 zc->zc_iflags, &props))) {
1455 nvlist_free(config);
1456 return (error);
1459 if (props) {
1460 nvlist_t *nvl = NULL;
1461 nvlist_t *hidden_args = NULL;
1462 uint64_t version = SPA_VERSION;
1463 const char *tname;
1465 (void) nvlist_lookup_uint64(props,
1466 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1467 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1468 error = SET_ERROR(EINVAL);
1469 goto pool_props_bad;
1471 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1472 if (nvl) {
1473 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
1474 if (error != 0)
1475 goto pool_props_bad;
1476 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1479 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1480 &hidden_args);
1481 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1482 rootprops, hidden_args, &dcp);
1483 if (error != 0)
1484 goto pool_props_bad;
1485 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1487 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1488 error = zfs_fill_zplprops_root(version, rootprops,
1489 zplprops, NULL);
1490 if (error != 0)
1491 goto pool_props_bad;
1493 if (nvlist_lookup_string(props,
1494 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1495 spa_name = tname;
1498 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
1501 * Set the remaining root properties
1503 if (!error && (error = zfs_set_prop_nvlist(spa_name,
1504 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
1505 (void) spa_destroy(spa_name);
1506 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1509 pool_props_bad:
1510 nvlist_free(rootprops);
1511 nvlist_free(zplprops);
1512 nvlist_free(config);
1513 nvlist_free(props);
1514 dsl_crypto_params_free(dcp, unload_wkey && !!error);
1516 return (error);
1519 static int
1520 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1522 int error;
1523 zfs_log_history(zc);
1524 error = spa_destroy(zc->zc_name);
1526 return (error);
1529 static int
1530 zfs_ioc_pool_import(zfs_cmd_t *zc)
1532 nvlist_t *config, *props = NULL;
1533 uint64_t guid;
1534 int error;
1536 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1537 zc->zc_iflags, &config)) != 0)
1538 return (error);
1540 if (zc->zc_nvlist_src_size != 0 && (error =
1541 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1542 zc->zc_iflags, &props))) {
1543 nvlist_free(config);
1544 return (error);
1547 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1548 guid != zc->zc_guid)
1549 error = SET_ERROR(EINVAL);
1550 else
1551 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1553 if (zc->zc_nvlist_dst != 0) {
1554 int err;
1556 if ((err = put_nvlist(zc, config)) != 0)
1557 error = err;
1560 nvlist_free(config);
1561 nvlist_free(props);
1563 return (error);
1566 static int
1567 zfs_ioc_pool_export(zfs_cmd_t *zc)
1569 int error;
1570 boolean_t force = (boolean_t)zc->zc_cookie;
1571 boolean_t hardforce = (boolean_t)zc->zc_guid;
1573 zfs_log_history(zc);
1574 error = spa_export(zc->zc_name, NULL, force, hardforce);
1576 return (error);
1579 static int
1580 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1582 nvlist_t *configs;
1583 int error;
1585 error = spa_all_configs(&zc->zc_cookie, &configs);
1586 if (error)
1587 return (error);
1589 error = put_nvlist(zc, configs);
1591 nvlist_free(configs);
1593 return (error);
1597 * inputs:
1598 * zc_name name of the pool
1600 * outputs:
1601 * zc_cookie real errno
1602 * zc_nvlist_dst config nvlist
1603 * zc_nvlist_dst_size size of config nvlist
1605 static int
1606 zfs_ioc_pool_stats(zfs_cmd_t *zc)
1608 nvlist_t *config;
1609 int error;
1610 int ret = 0;
1612 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1613 sizeof (zc->zc_value));
1615 if (config != NULL) {
1616 ret = put_nvlist(zc, config);
1617 nvlist_free(config);
1620 * The config may be present even if 'error' is non-zero.
1621 * In this case we return success, and preserve the real errno
1622 * in 'zc_cookie'.
1624 zc->zc_cookie = error;
1625 } else {
1626 ret = error;
1629 return (ret);
1633 * Try to import the given pool, returning pool stats as appropriate so that
1634 * user land knows which devices are available and overall pool health.
1636 static int
1637 zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1639 nvlist_t *tryconfig, *config = NULL;
1640 int error;
1642 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1643 zc->zc_iflags, &tryconfig)) != 0)
1644 return (error);
1646 config = spa_tryimport(tryconfig);
1648 nvlist_free(tryconfig);
1650 if (config == NULL)
1651 return (SET_ERROR(EINVAL));
1653 error = put_nvlist(zc, config);
1654 nvlist_free(config);
1656 return (error);
1660 * inputs:
1661 * zc_name name of the pool
1662 * zc_cookie scan func (pool_scan_func_t)
1663 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
1665 static int
1666 zfs_ioc_pool_scan(zfs_cmd_t *zc)
1668 spa_t *spa;
1669 int error;
1671 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1672 return (SET_ERROR(EINVAL));
1674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1675 return (error);
1677 if (zc->zc_flags == POOL_SCRUB_PAUSE)
1678 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1679 else if (zc->zc_cookie == POOL_SCAN_NONE)
1680 error = spa_scan_stop(spa);
1681 else
1682 error = spa_scan(spa, zc->zc_cookie);
1684 spa_close(spa, FTAG);
1686 return (error);
1690 * inputs:
1691 * poolname name of the pool
1692 * scan_type scan func (pool_scan_func_t)
1693 * scan_command scrub pause/resume flag (pool_scrub_cmd_t)
1695 static const zfs_ioc_key_t zfs_keys_pool_scrub[] = {
1696 {"scan_type", DATA_TYPE_UINT64, 0},
1697 {"scan_command", DATA_TYPE_UINT64, 0},
1700 static int
1701 zfs_ioc_pool_scrub(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
1703 spa_t *spa;
1704 int error;
1705 uint64_t scan_type, scan_cmd;
1707 if (nvlist_lookup_uint64(innvl, "scan_type", &scan_type) != 0)
1708 return (SET_ERROR(EINVAL));
1709 if (nvlist_lookup_uint64(innvl, "scan_command", &scan_cmd) != 0)
1710 return (SET_ERROR(EINVAL));
1712 if (scan_cmd >= POOL_SCRUB_FLAGS_END)
1713 return (SET_ERROR(EINVAL));
1715 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
1716 return (error);
1718 if (scan_cmd == POOL_SCRUB_PAUSE) {
1719 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1720 } else if (scan_type == POOL_SCAN_NONE) {
1721 error = spa_scan_stop(spa);
1722 } else {
1723 error = spa_scan(spa, scan_type);
1726 spa_close(spa, FTAG);
1727 return (error);
1730 static int
1731 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1733 spa_t *spa;
1734 int error;
1736 error = spa_open(zc->zc_name, &spa, FTAG);
1737 if (error == 0) {
1738 spa_freeze(spa);
1739 spa_close(spa, FTAG);
1741 return (error);
1744 static int
1745 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1747 spa_t *spa;
1748 int error;
1750 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1751 return (error);
1753 if (zc->zc_cookie < spa_version(spa) ||
1754 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
1755 spa_close(spa, FTAG);
1756 return (SET_ERROR(EINVAL));
1759 spa_upgrade(spa, zc->zc_cookie);
1760 spa_close(spa, FTAG);
1762 return (error);
1765 static int
1766 zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1768 spa_t *spa;
1769 char *hist_buf;
1770 uint64_t size;
1771 int error;
1773 if ((size = zc->zc_history_len) == 0)
1774 return (SET_ERROR(EINVAL));
1776 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1777 return (error);
1779 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1780 spa_close(spa, FTAG);
1781 return (SET_ERROR(ENOTSUP));
1784 hist_buf = vmem_alloc(size, KM_SLEEP);
1785 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1786 &zc->zc_history_len, hist_buf)) == 0) {
1787 error = ddi_copyout(hist_buf,
1788 (void *)(uintptr_t)zc->zc_history,
1789 zc->zc_history_len, zc->zc_iflags);
1792 spa_close(spa, FTAG);
1793 vmem_free(hist_buf, size);
1794 return (error);
1797 static int
1798 zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1800 spa_t *spa;
1801 int error;
1803 error = spa_open(zc->zc_name, &spa, FTAG);
1804 if (error == 0) {
1805 error = spa_change_guid(spa);
1806 spa_close(spa, FTAG);
1808 return (error);
1811 static int
1812 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1814 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
1818 * inputs:
1819 * zc_name name of filesystem
1820 * zc_obj object to find
1822 * outputs:
1823 * zc_value name of object
1825 static int
1826 zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1828 objset_t *os;
1829 int error;
1831 /* XXX reading from objset not owned */
1832 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1833 FTAG, &os)) != 0)
1834 return (error);
1835 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1836 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1837 return (SET_ERROR(EINVAL));
1839 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
1840 sizeof (zc->zc_value));
1841 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1843 return (error);
1847 * inputs:
1848 * zc_name name of filesystem
1849 * zc_obj object to find
1851 * outputs:
1852 * zc_stat stats on object
1853 * zc_value path to object
1855 static int
1856 zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1858 objset_t *os;
1859 int error;
1861 /* XXX reading from objset not owned */
1862 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1863 FTAG, &os)) != 0)
1864 return (error);
1865 if (dmu_objset_type(os) != DMU_OST_ZFS) {
1866 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1867 return (SET_ERROR(EINVAL));
1869 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1870 sizeof (zc->zc_value));
1871 dmu_objset_rele_flags(os, B_TRUE, FTAG);
1873 return (error);
1876 static int
1877 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1879 spa_t *spa;
1880 int error;
1881 nvlist_t *config;
1883 error = spa_open(zc->zc_name, &spa, FTAG);
1884 if (error != 0)
1885 return (error);
1887 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1888 zc->zc_iflags, &config);
1889 if (error == 0) {
1890 error = spa_vdev_add(spa, config);
1891 nvlist_free(config);
1893 spa_close(spa, FTAG);
1894 return (error);
1898 * inputs:
1899 * zc_name name of the pool
1900 * zc_guid guid of vdev to remove
1901 * zc_cookie cancel removal
1903 static int
1904 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1906 spa_t *spa;
1907 int error;
1909 error = spa_open(zc->zc_name, &spa, FTAG);
1910 if (error != 0)
1911 return (error);
1912 if (zc->zc_cookie != 0) {
1913 error = spa_vdev_remove_cancel(spa);
1914 } else {
1915 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1917 spa_close(spa, FTAG);
1918 return (error);
1921 static int
1922 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1924 spa_t *spa;
1925 int error;
1926 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1928 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1929 return (error);
1930 switch (zc->zc_cookie) {
1931 case VDEV_STATE_ONLINE:
1932 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1933 break;
1935 case VDEV_STATE_OFFLINE:
1936 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1937 break;
1939 case VDEV_STATE_FAULTED:
1940 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1941 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1942 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
1943 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1945 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1946 break;
1948 case VDEV_STATE_DEGRADED:
1949 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1950 zc->zc_obj != VDEV_AUX_EXTERNAL)
1951 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1953 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1954 break;
1956 case VDEV_STATE_REMOVED:
1957 error = vdev_remove_wanted(spa, zc->zc_guid);
1958 break;
1960 default:
1961 error = SET_ERROR(EINVAL);
1963 zc->zc_cookie = newstate;
1964 spa_close(spa, FTAG);
1965 return (error);
1968 static int
1969 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1971 spa_t *spa;
1972 nvlist_t *config;
1973 int replacing = zc->zc_cookie;
1974 int rebuild = zc->zc_simple;
1975 int error;
1977 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1978 return (error);
1980 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1981 zc->zc_iflags, &config)) == 0) {
1982 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing,
1983 rebuild);
1984 nvlist_free(config);
1987 spa_close(spa, FTAG);
1988 return (error);
1991 static int
1992 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1994 spa_t *spa;
1995 int error;
1997 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1998 return (error);
2000 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2002 spa_close(spa, FTAG);
2003 return (error);
2006 static int
2007 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2009 spa_t *spa;
2010 nvlist_t *config, *props = NULL;
2011 int error;
2012 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2014 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2015 return (error);
2017 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2018 zc->zc_iflags, &config))) {
2019 spa_close(spa, FTAG);
2020 return (error);
2023 if (zc->zc_nvlist_src_size != 0 && (error =
2024 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2025 zc->zc_iflags, &props))) {
2026 spa_close(spa, FTAG);
2027 nvlist_free(config);
2028 return (error);
2031 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2033 spa_close(spa, FTAG);
2035 nvlist_free(config);
2036 nvlist_free(props);
2038 return (error);
2041 static int
2042 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2044 spa_t *spa;
2045 const char *path = zc->zc_value;
2046 uint64_t guid = zc->zc_guid;
2047 int error;
2049 error = spa_open(zc->zc_name, &spa, FTAG);
2050 if (error != 0)
2051 return (error);
2053 error = spa_vdev_setpath(spa, guid, path);
2054 spa_close(spa, FTAG);
2055 return (error);
2058 static int
2059 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2061 spa_t *spa;
2062 const char *fru = zc->zc_value;
2063 uint64_t guid = zc->zc_guid;
2064 int error;
2066 error = spa_open(zc->zc_name, &spa, FTAG);
2067 if (error != 0)
2068 return (error);
2070 error = spa_vdev_setfru(spa, guid, fru);
2071 spa_close(spa, FTAG);
2072 return (error);
2075 static int
2076 zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
2078 int error = 0;
2079 nvlist_t *nv;
2081 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2083 if (!zc->zc_simple && zc->zc_nvlist_dst != 0 &&
2084 (error = dsl_prop_get_all(os, &nv)) == 0) {
2085 dmu_objset_stats(os, nv);
2087 * NB: zvol_get_stats() will read the objset contents,
2088 * which we aren't supposed to do with a
2089 * DS_MODE_USER hold, because it could be
2090 * inconsistent. So this is a bit of a workaround...
2091 * XXX reading without owning
2093 if (!zc->zc_objset_stats.dds_inconsistent &&
2094 dmu_objset_type(os) == DMU_OST_ZVOL) {
2095 error = zvol_get_stats(os, nv);
2096 if (error == EIO) {
2097 nvlist_free(nv);
2098 return (error);
2100 VERIFY0(error);
2102 if (error == 0)
2103 error = put_nvlist(zc, nv);
2104 nvlist_free(nv);
2107 return (error);
2111 * inputs:
2112 * zc_name name of filesystem
2113 * zc_nvlist_dst_size size of buffer for property nvlist
2115 * outputs:
2116 * zc_objset_stats stats
2117 * zc_nvlist_dst property nvlist
2118 * zc_nvlist_dst_size size of property nvlist
2120 static int
2121 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2123 objset_t *os;
2124 int error;
2126 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2127 if (error == 0) {
2128 error = zfs_ioc_objset_stats_impl(zc, os);
2129 dmu_objset_rele(os, FTAG);
2132 return (error);
2136 * inputs:
2137 * zc_name name of filesystem
2138 * zc_nvlist_dst_size size of buffer for property nvlist
2140 * outputs:
2141 * zc_nvlist_dst received property nvlist
2142 * zc_nvlist_dst_size size of received property nvlist
2144 * Gets received properties (distinct from local properties on or after
2145 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2146 * local property values.
2148 static int
2149 zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
2151 int error = 0;
2152 nvlist_t *nv;
2155 * Without this check, we would return local property values if the
2156 * caller has not already received properties on or after
2157 * SPA_VERSION_RECVD_PROPS.
2159 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2160 return (SET_ERROR(ENOTSUP));
2162 if (zc->zc_nvlist_dst != 0 &&
2163 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
2164 error = put_nvlist(zc, nv);
2165 nvlist_free(nv);
2168 return (error);
2171 static int
2172 nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2174 uint64_t value;
2175 int error;
2178 * zfs_get_zplprop() will either find a value or give us
2179 * the default value (if there is one).
2181 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2182 return (error);
2183 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2184 return (0);
2188 * inputs:
2189 * zc_name name of filesystem
2190 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2192 * outputs:
2193 * zc_nvlist_dst zpl property nvlist
2194 * zc_nvlist_dst_size size of zpl property nvlist
2196 static int
2197 zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2199 objset_t *os;
2200 int err;
2202 /* XXX reading without owning */
2203 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
2204 return (err);
2206 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2209 * NB: nvl_add_zplprop() will read the objset contents,
2210 * which we aren't supposed to do with a DS_MODE_USER
2211 * hold, because it could be inconsistent.
2213 if (zc->zc_nvlist_dst != 0 &&
2214 !zc->zc_objset_stats.dds_inconsistent &&
2215 dmu_objset_type(os) == DMU_OST_ZFS) {
2216 nvlist_t *nv;
2218 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2219 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2220 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2221 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2222 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2223 err = put_nvlist(zc, nv);
2224 nvlist_free(nv);
2225 } else {
2226 err = SET_ERROR(ENOENT);
2228 dmu_objset_rele(os, FTAG);
2229 return (err);
2233 * inputs:
2234 * zc_name name of filesystem
2235 * zc_cookie zap cursor
2236 * zc_nvlist_dst_size size of buffer for property nvlist
2238 * outputs:
2239 * zc_name name of next filesystem
2240 * zc_cookie zap cursor
2241 * zc_objset_stats stats
2242 * zc_nvlist_dst property nvlist
2243 * zc_nvlist_dst_size size of property nvlist
2245 static int
2246 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2248 objset_t *os;
2249 int error;
2250 char *p;
2251 size_t orig_len = strlen(zc->zc_name);
2253 top:
2254 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
2255 if (error == ENOENT)
2256 error = SET_ERROR(ESRCH);
2257 return (error);
2260 p = strrchr(zc->zc_name, '/');
2261 if (p == NULL || p[1] != '\0')
2262 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2263 p = zc->zc_name + strlen(zc->zc_name);
2265 do {
2266 error = dmu_dir_list_next(os,
2267 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2268 NULL, &zc->zc_cookie);
2269 if (error == ENOENT)
2270 error = SET_ERROR(ESRCH);
2271 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
2272 dmu_objset_rele(os, FTAG);
2275 * If it's an internal dataset (ie. with a '$' in its name),
2276 * don't try to get stats for it, otherwise we'll return ENOENT.
2278 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
2279 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
2280 if (error == ENOENT) {
2281 /* We lost a race with destroy, get the next one. */
2282 zc->zc_name[orig_len] = '\0';
2283 goto top;
2286 return (error);
2290 * inputs:
2291 * zc_name name of filesystem
2292 * zc_cookie zap cursor
2293 * zc_nvlist_src iteration range nvlist
2294 * zc_nvlist_src_size size of iteration range nvlist
2296 * outputs:
2297 * zc_name name of next snapshot
2298 * zc_objset_stats stats
2299 * zc_nvlist_dst property nvlist
2300 * zc_nvlist_dst_size size of property nvlist
2302 static int
2303 zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2305 int error;
2306 objset_t *os, *ossnap;
2307 dsl_dataset_t *ds;
2308 uint64_t min_txg = 0, max_txg = 0;
2310 if (zc->zc_nvlist_src_size != 0) {
2311 nvlist_t *props = NULL;
2312 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2313 zc->zc_iflags, &props);
2314 if (error != 0)
2315 return (error);
2316 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2317 &min_txg);
2318 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2319 &max_txg);
2320 nvlist_free(props);
2323 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2324 if (error != 0) {
2325 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
2329 * A dataset name of maximum length cannot have any snapshots,
2330 * so exit immediately.
2332 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2333 ZFS_MAX_DATASET_NAME_LEN) {
2334 dmu_objset_rele(os, FTAG);
2335 return (SET_ERROR(ESRCH));
2338 while (error == 0) {
2339 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2340 error = SET_ERROR(EINTR);
2341 break;
2344 error = dmu_snapshot_list_next(os,
2345 sizeof (zc->zc_name) - strlen(zc->zc_name),
2346 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2347 &zc->zc_cookie, NULL);
2348 if (error == ENOENT) {
2349 error = SET_ERROR(ESRCH);
2350 break;
2351 } else if (error != 0) {
2352 break;
2355 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2356 FTAG, &ds);
2357 if (error != 0)
2358 break;
2360 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2361 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
2362 dsl_dataset_rele(ds, FTAG);
2363 /* undo snapshot name append */
2364 *(strchr(zc->zc_name, '@') + 1) = '\0';
2365 /* skip snapshot */
2366 continue;
2369 if (zc->zc_simple) {
2370 dsl_dataset_fast_stat(ds, &zc->zc_objset_stats);
2371 dsl_dataset_rele(ds, FTAG);
2372 break;
2375 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2376 dsl_dataset_rele(ds, FTAG);
2377 break;
2379 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2380 dsl_dataset_rele(ds, FTAG);
2381 break;
2383 dsl_dataset_rele(ds, FTAG);
2384 break;
2387 dmu_objset_rele(os, FTAG);
2388 /* if we failed, undo the @ that we tacked on to zc_name */
2389 if (error != 0)
2390 *strchr(zc->zc_name, '@') = '\0';
2391 return (error);
2394 static int
2395 zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
2397 const char *propname = nvpair_name(pair);
2398 uint64_t *valary;
2399 unsigned int vallen;
2400 const char *dash, *domain;
2401 zfs_userquota_prop_t type;
2402 uint64_t rid;
2403 uint64_t quota;
2404 zfsvfs_t *zfsvfs;
2405 int err;
2407 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2408 nvlist_t *attrs;
2409 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2410 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2411 &pair) != 0)
2412 return (SET_ERROR(EINVAL));
2416 * A correctly constructed propname is encoded as
2417 * userquota@<rid>-<domain>.
2419 if ((dash = strchr(propname, '-')) == NULL ||
2420 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2421 vallen != 3)
2422 return (SET_ERROR(EINVAL));
2424 domain = dash + 1;
2425 type = valary[0];
2426 rid = valary[1];
2427 quota = valary[2];
2429 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
2430 if (err == 0) {
2431 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
2432 zfsvfs_rele(zfsvfs, FTAG);
2435 return (err);
2439 * If the named property is one that has a special function to set its value,
2440 * return 0 on success and a positive error code on failure; otherwise if it is
2441 * not one of the special properties handled by this function, return -1.
2443 * XXX: It would be better for callers of the property interface if we handled
2444 * these special cases in dsl_prop.c (in the dsl layer).
2446 static int
2447 zfs_prop_set_special(const char *dsname, zprop_source_t source,
2448 nvpair_t *pair)
2450 const char *propname = nvpair_name(pair);
2451 zfs_prop_t prop = zfs_name_to_prop(propname);
2452 uint64_t intval = 0;
2453 const char *strval = NULL;
2454 int err = -1;
2456 if (prop == ZPROP_USERPROP) {
2457 if (zfs_prop_userquota(propname))
2458 return (zfs_prop_set_userquota(dsname, pair));
2459 return (-1);
2462 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2463 nvlist_t *attrs;
2464 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2465 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2466 &pair) == 0);
2469 /* all special properties are numeric except for keylocation */
2470 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2471 strval = fnvpair_value_string(pair);
2472 } else {
2473 intval = fnvpair_value_uint64(pair);
2476 switch (prop) {
2477 case ZFS_PROP_QUOTA:
2478 err = dsl_dir_set_quota(dsname, source, intval);
2479 break;
2480 case ZFS_PROP_REFQUOTA:
2481 err = dsl_dataset_set_refquota(dsname, source, intval);
2482 break;
2483 case ZFS_PROP_FILESYSTEM_LIMIT:
2484 case ZFS_PROP_SNAPSHOT_LIMIT:
2485 if (intval == UINT64_MAX) {
2486 /* clearing the limit, just do it */
2487 err = 0;
2488 } else {
2489 err = dsl_dir_activate_fs_ss_limit(dsname);
2492 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2493 * default path to set the value in the nvlist.
2495 if (err == 0)
2496 err = -1;
2497 break;
2498 case ZFS_PROP_KEYLOCATION:
2499 err = dsl_crypto_can_set_keylocation(dsname, strval);
2502 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2503 * default path to set the value in the nvlist.
2505 if (err == 0)
2506 err = -1;
2507 break;
2508 case ZFS_PROP_RESERVATION:
2509 err = dsl_dir_set_reservation(dsname, source, intval);
2510 break;
2511 case ZFS_PROP_REFRESERVATION:
2512 err = dsl_dataset_set_refreservation(dsname, source, intval);
2513 break;
2514 case ZFS_PROP_COMPRESSION:
2515 err = dsl_dataset_set_compression(dsname, source, intval);
2517 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2518 * default path to set the value in the nvlist.
2520 if (err == 0)
2521 err = -1;
2522 break;
2523 case ZFS_PROP_VOLSIZE:
2524 err = zvol_set_volsize(dsname, intval);
2525 break;
2526 case ZFS_PROP_SNAPDEV:
2527 err = zvol_set_snapdev(dsname, source, intval);
2528 break;
2529 case ZFS_PROP_VOLMODE:
2530 err = zvol_set_volmode(dsname, source, intval);
2531 break;
2532 case ZFS_PROP_VERSION:
2534 zfsvfs_t *zfsvfs;
2536 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
2537 break;
2539 err = zfs_set_version(zfsvfs, intval);
2540 zfsvfs_rele(zfsvfs, FTAG);
2542 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2543 zfs_cmd_t *zc;
2545 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
2546 (void) strlcpy(zc->zc_name, dsname,
2547 sizeof (zc->zc_name));
2548 (void) zfs_ioc_userspace_upgrade(zc);
2549 (void) zfs_ioc_id_quota_upgrade(zc);
2550 kmem_free(zc, sizeof (zfs_cmd_t));
2552 break;
2554 default:
2555 err = -1;
2558 return (err);
2561 static boolean_t
2562 zfs_is_namespace_prop(zfs_prop_t prop)
2564 switch (prop) {
2566 case ZFS_PROP_ATIME:
2567 case ZFS_PROP_RELATIME:
2568 case ZFS_PROP_DEVICES:
2569 case ZFS_PROP_EXEC:
2570 case ZFS_PROP_SETUID:
2571 case ZFS_PROP_READONLY:
2572 case ZFS_PROP_XATTR:
2573 case ZFS_PROP_NBMAND:
2574 return (B_TRUE);
2576 default:
2577 return (B_FALSE);
2582 * This function is best effort. If it fails to set any of the given properties,
2583 * it continues to set as many as it can and returns the last error
2584 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2585 * with the list of names of all the properties that failed along with the
2586 * corresponding error numbers.
2588 * If every property is set successfully, zero is returned and errlist is not
2589 * modified.
2592 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2593 nvlist_t *errlist)
2595 nvpair_t *pair;
2596 nvpair_t *propval;
2597 int rv = 0;
2598 int err;
2599 uint64_t intval;
2600 const char *strval;
2601 boolean_t should_update_mount_cache = B_FALSE;
2603 nvlist_t *genericnvl = fnvlist_alloc();
2604 nvlist_t *retrynvl = fnvlist_alloc();
2605 retry:
2606 pair = NULL;
2607 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2608 const char *propname = nvpair_name(pair);
2609 zfs_prop_t prop = zfs_name_to_prop(propname);
2610 err = 0;
2612 /* decode the property value */
2613 propval = pair;
2614 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2615 nvlist_t *attrs;
2616 attrs = fnvpair_value_nvlist(pair);
2617 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2618 &propval) != 0)
2619 err = SET_ERROR(EINVAL);
2622 /* Validate value type */
2623 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2624 /* inherited properties are expected to be booleans */
2625 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2626 err = SET_ERROR(EINVAL);
2627 } else if (err == 0 && prop == ZPROP_USERPROP) {
2628 if (zfs_prop_user(propname)) {
2629 if (nvpair_type(propval) != DATA_TYPE_STRING)
2630 err = SET_ERROR(EINVAL);
2631 } else if (zfs_prop_userquota(propname)) {
2632 if (nvpair_type(propval) !=
2633 DATA_TYPE_UINT64_ARRAY)
2634 err = SET_ERROR(EINVAL);
2635 } else {
2636 err = SET_ERROR(EINVAL);
2638 } else if (err == 0) {
2639 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2640 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2641 err = SET_ERROR(EINVAL);
2642 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
2643 const char *unused;
2645 intval = fnvpair_value_uint64(propval);
2647 switch (zfs_prop_get_type(prop)) {
2648 case PROP_TYPE_NUMBER:
2649 break;
2650 case PROP_TYPE_STRING:
2651 err = SET_ERROR(EINVAL);
2652 break;
2653 case PROP_TYPE_INDEX:
2654 if (zfs_prop_index_to_string(prop,
2655 intval, &unused) != 0)
2656 err =
2657 SET_ERROR(ZFS_ERR_BADPROP);
2658 break;
2659 default:
2660 cmn_err(CE_PANIC,
2661 "unknown property type");
2663 } else {
2664 err = SET_ERROR(EINVAL);
2668 /* Validate permissions */
2669 if (err == 0)
2670 err = zfs_check_settable(dsname, pair, CRED());
2672 if (err == 0) {
2673 if (source == ZPROP_SRC_INHERITED)
2674 err = -1; /* does not need special handling */
2675 else
2676 err = zfs_prop_set_special(dsname, source,
2677 pair);
2678 if (err == -1) {
2680 * For better performance we build up a list of
2681 * properties to set in a single transaction.
2683 err = nvlist_add_nvpair(genericnvl, pair);
2684 } else if (err != 0 && nvl != retrynvl) {
2686 * This may be a spurious error caused by
2687 * receiving quota and reservation out of order.
2688 * Try again in a second pass.
2690 err = nvlist_add_nvpair(retrynvl, pair);
2694 if (err != 0) {
2695 if (errlist != NULL)
2696 fnvlist_add_int32(errlist, propname, err);
2697 rv = err;
2700 if (zfs_is_namespace_prop(prop))
2701 should_update_mount_cache = B_TRUE;
2704 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2705 nvl = retrynvl;
2706 goto retry;
2709 if (nvlist_empty(genericnvl))
2710 goto out;
2713 * Try to set them all in one batch.
2715 err = dsl_props_set(dsname, source, genericnvl);
2716 if (err == 0)
2717 goto out;
2720 * If batching fails, we still want to set as many properties as we
2721 * can, so try setting them individually.
2723 pair = NULL;
2724 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2725 const char *propname = nvpair_name(pair);
2727 propval = pair;
2728 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2729 nvlist_t *attrs;
2730 attrs = fnvpair_value_nvlist(pair);
2731 propval = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
2734 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2735 strval = fnvpair_value_string(propval);
2736 err = dsl_prop_set_string(dsname, propname,
2737 source, strval);
2738 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2739 err = dsl_prop_inherit(dsname, propname, source);
2740 } else {
2741 intval = fnvpair_value_uint64(propval);
2742 err = dsl_prop_set_int(dsname, propname, source,
2743 intval);
2746 if (err != 0) {
2747 if (errlist != NULL) {
2748 fnvlist_add_int32(errlist, propname, err);
2750 rv = err;
2754 out:
2755 if (should_update_mount_cache)
2756 zfs_ioctl_update_mount_cache(dsname);
2758 nvlist_free(genericnvl);
2759 nvlist_free(retrynvl);
2761 return (rv);
2765 * Check that all the properties are valid user properties.
2767 static int
2768 zfs_check_userprops(nvlist_t *nvl)
2770 nvpair_t *pair = NULL;
2772 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2773 const char *propname = nvpair_name(pair);
2775 if (!zfs_prop_user(propname) ||
2776 nvpair_type(pair) != DATA_TYPE_STRING)
2777 return (SET_ERROR(EINVAL));
2779 if (strlen(propname) >= ZAP_MAXNAMELEN)
2780 return (SET_ERROR(ENAMETOOLONG));
2782 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2783 return (SET_ERROR(E2BIG));
2785 return (0);
2788 static void
2789 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2791 nvpair_t *pair;
2793 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2795 pair = NULL;
2796 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2797 if (nvlist_exists(skipped, nvpair_name(pair)))
2798 continue;
2800 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2804 static int
2805 clear_received_props(const char *dsname, nvlist_t *props,
2806 nvlist_t *skipped)
2808 int err = 0;
2809 nvlist_t *cleared_props = NULL;
2810 props_skip(props, skipped, &cleared_props);
2811 if (!nvlist_empty(cleared_props)) {
2813 * Acts on local properties until the dataset has received
2814 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2816 zprop_source_t flags = (ZPROP_SRC_NONE |
2817 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2818 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2820 nvlist_free(cleared_props);
2821 return (err);
2825 * inputs:
2826 * zc_name name of filesystem
2827 * zc_value name of property to set
2828 * zc_nvlist_src{_size} nvlist of properties to apply
2829 * zc_cookie received properties flag
2831 * outputs:
2832 * zc_nvlist_dst{_size} error for each unapplied received property
2834 static int
2835 zfs_ioc_set_prop(zfs_cmd_t *zc)
2837 nvlist_t *nvl;
2838 boolean_t received = zc->zc_cookie;
2839 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2840 ZPROP_SRC_LOCAL);
2841 nvlist_t *errors;
2842 int error;
2844 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2845 zc->zc_iflags, &nvl)) != 0)
2846 return (error);
2848 if (received) {
2849 nvlist_t *origprops;
2851 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2852 (void) clear_received_props(zc->zc_name,
2853 origprops, nvl);
2854 nvlist_free(origprops);
2857 error = dsl_prop_set_hasrecvd(zc->zc_name);
2860 errors = fnvlist_alloc();
2861 if (error == 0)
2862 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2864 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
2865 (void) put_nvlist(zc, errors);
2868 nvlist_free(errors);
2869 nvlist_free(nvl);
2870 return (error);
2874 * inputs:
2875 * zc_name name of filesystem
2876 * zc_value name of property to inherit
2877 * zc_cookie revert to received value if TRUE
2879 * outputs: none
2881 static int
2882 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2884 const char *propname = zc->zc_value;
2885 zfs_prop_t prop = zfs_name_to_prop(propname);
2886 boolean_t received = zc->zc_cookie;
2887 zprop_source_t source = (received
2888 ? ZPROP_SRC_NONE /* revert to received value, if any */
2889 : ZPROP_SRC_INHERITED); /* explicitly inherit */
2890 nvlist_t *dummy;
2891 nvpair_t *pair;
2892 zprop_type_t type;
2893 int err;
2895 if (!received) {
2897 * Only check this in the non-received case. We want to allow
2898 * 'inherit -S' to revert non-inheritable properties like quota
2899 * and reservation to the received or default values even though
2900 * they are not considered inheritable.
2902 if (prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop))
2903 return (SET_ERROR(EINVAL));
2906 if (prop == ZPROP_USERPROP) {
2907 if (!zfs_prop_user(propname))
2908 return (SET_ERROR(EINVAL));
2910 type = PROP_TYPE_STRING;
2911 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2912 return (SET_ERROR(EINVAL));
2913 } else {
2914 type = zfs_prop_get_type(prop);
2918 * zfs_prop_set_special() expects properties in the form of an
2919 * nvpair with type info.
2921 dummy = fnvlist_alloc();
2923 switch (type) {
2924 case PROP_TYPE_STRING:
2925 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2926 break;
2927 case PROP_TYPE_NUMBER:
2928 case PROP_TYPE_INDEX:
2929 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2930 break;
2931 default:
2932 err = SET_ERROR(EINVAL);
2933 goto errout;
2936 pair = nvlist_next_nvpair(dummy, NULL);
2937 if (pair == NULL) {
2938 err = SET_ERROR(EINVAL);
2939 } else {
2940 err = zfs_prop_set_special(zc->zc_name, source, pair);
2941 if (err == -1) /* property is not "special", needs handling */
2942 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2943 source);
2946 errout:
2947 nvlist_free(dummy);
2948 return (err);
2951 static int
2952 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2954 nvlist_t *props;
2955 spa_t *spa;
2956 int error;
2957 nvpair_t *pair;
2959 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2960 zc->zc_iflags, &props)))
2961 return (error);
2964 * If the only property is the configfile, then just do a spa_lookup()
2965 * to handle the faulted case.
2967 pair = nvlist_next_nvpair(props, NULL);
2968 if (pair != NULL && strcmp(nvpair_name(pair),
2969 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2970 nvlist_next_nvpair(props, pair) == NULL) {
2971 mutex_enter(&spa_namespace_lock);
2972 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2973 spa_configfile_set(spa, props, B_FALSE);
2974 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE);
2976 mutex_exit(&spa_namespace_lock);
2977 if (spa != NULL) {
2978 nvlist_free(props);
2979 return (0);
2983 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2984 nvlist_free(props);
2985 return (error);
2988 error = spa_prop_set(spa, props);
2990 nvlist_free(props);
2991 spa_close(spa, FTAG);
2993 return (error);
2996 static int
2997 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2999 spa_t *spa;
3000 int error;
3001 nvlist_t *nvp = NULL;
3003 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3005 * If the pool is faulted, there may be properties we can still
3006 * get (such as altroot and cachefile), so attempt to get them
3007 * anyway.
3009 mutex_enter(&spa_namespace_lock);
3010 if ((spa = spa_lookup(zc->zc_name)) != NULL)
3011 error = spa_prop_get(spa, &nvp);
3012 mutex_exit(&spa_namespace_lock);
3013 } else {
3014 error = spa_prop_get(spa, &nvp);
3015 spa_close(spa, FTAG);
3018 if (error == 0 && zc->zc_nvlist_dst != 0)
3019 error = put_nvlist(zc, nvp);
3020 else
3021 error = SET_ERROR(EFAULT);
3023 nvlist_free(nvp);
3024 return (error);
3028 * innvl: {
3029 * "vdevprops_set_vdev" -> guid
3030 * "vdevprops_set_props" -> { prop -> value }
3033 * outnvl: propname -> error code (int32)
3035 static const zfs_ioc_key_t zfs_keys_vdev_set_props[] = {
3036 {ZPOOL_VDEV_PROPS_SET_VDEV, DATA_TYPE_UINT64, 0},
3037 {ZPOOL_VDEV_PROPS_SET_PROPS, DATA_TYPE_NVLIST, 0}
3040 static int
3041 zfs_ioc_vdev_set_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3043 spa_t *spa;
3044 int error;
3045 vdev_t *vd;
3046 uint64_t vdev_guid;
3048 /* Early validation */
3049 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
3050 &vdev_guid) != 0)
3051 return (SET_ERROR(EINVAL));
3053 if (outnvl == NULL)
3054 return (SET_ERROR(EINVAL));
3056 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3057 return (error);
3059 ASSERT(spa_writeable(spa));
3061 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3062 spa_close(spa, FTAG);
3063 return (SET_ERROR(ENOENT));
3066 error = vdev_prop_set(vd, innvl, outnvl);
3068 spa_close(spa, FTAG);
3070 return (error);
3074 * innvl: {
3075 * "vdevprops_get_vdev" -> guid
3076 * (optional) "vdevprops_get_props" -> { propname -> propid }
3079 * outnvl: propname -> value
3081 static const zfs_ioc_key_t zfs_keys_vdev_get_props[] = {
3082 {ZPOOL_VDEV_PROPS_GET_VDEV, DATA_TYPE_UINT64, 0},
3083 {ZPOOL_VDEV_PROPS_GET_PROPS, DATA_TYPE_NVLIST, ZK_OPTIONAL}
3086 static int
3087 zfs_ioc_vdev_get_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3089 spa_t *spa;
3090 int error;
3091 vdev_t *vd;
3092 uint64_t vdev_guid;
3094 /* Early validation */
3095 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
3096 &vdev_guid) != 0)
3097 return (SET_ERROR(EINVAL));
3099 if (outnvl == NULL)
3100 return (SET_ERROR(EINVAL));
3102 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3103 return (error);
3105 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3106 spa_close(spa, FTAG);
3107 return (SET_ERROR(ENOENT));
3110 error = vdev_prop_get(vd, innvl, outnvl);
3112 spa_close(spa, FTAG);
3114 return (error);
3118 * inputs:
3119 * zc_name name of filesystem
3120 * zc_nvlist_src{_size} nvlist of delegated permissions
3121 * zc_perm_action allow/unallow flag
3123 * outputs: none
3125 static int
3126 zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3128 int error;
3129 nvlist_t *fsaclnv = NULL;
3131 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3132 zc->zc_iflags, &fsaclnv)) != 0)
3133 return (error);
3136 * Verify nvlist is constructed correctly
3138 if (zfs_deleg_verify_nvlist(fsaclnv) != 0) {
3139 nvlist_free(fsaclnv);
3140 return (SET_ERROR(EINVAL));
3144 * If we don't have PRIV_SYS_MOUNT, then validate
3145 * that user is allowed to hand out each permission in
3146 * the nvlist(s)
3149 error = secpolicy_zfs(CRED());
3150 if (error != 0) {
3151 if (zc->zc_perm_action == B_FALSE) {
3152 error = dsl_deleg_can_allow(zc->zc_name,
3153 fsaclnv, CRED());
3154 } else {
3155 error = dsl_deleg_can_unallow(zc->zc_name,
3156 fsaclnv, CRED());
3160 if (error == 0)
3161 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3163 nvlist_free(fsaclnv);
3164 return (error);
3168 * inputs:
3169 * zc_name name of filesystem
3171 * outputs:
3172 * zc_nvlist_src{_size} nvlist of delegated permissions
3174 static int
3175 zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3177 nvlist_t *nvp;
3178 int error;
3180 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3181 error = put_nvlist(zc, nvp);
3182 nvlist_free(nvp);
3185 return (error);
3188 static void
3189 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3191 zfs_creat_t *zct = arg;
3193 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3196 #define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3199 * inputs:
3200 * os parent objset pointer (NULL if root fs)
3201 * fuids_ok fuids allowed in this version of the spa?
3202 * sa_ok SAs allowed in this version of the spa?
3203 * createprops list of properties requested by creator
3205 * outputs:
3206 * zplprops values for the zplprops we attach to the master node object
3207 * is_ci true if requested file system will be purely case-insensitive
3209 * Determine the settings for utf8only, normalization and
3210 * casesensitivity. Specific values may have been requested by the
3211 * creator and/or we can inherit values from the parent dataset. If
3212 * the file system is of too early a vintage, a creator can not
3213 * request settings for these properties, even if the requested
3214 * setting is the default value. We don't actually want to create dsl
3215 * properties for these, so remove them from the source nvlist after
3216 * processing.
3218 static int
3219 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
3220 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3221 nvlist_t *zplprops, boolean_t *is_ci)
3223 uint64_t sense = ZFS_PROP_UNDEFINED;
3224 uint64_t norm = ZFS_PROP_UNDEFINED;
3225 uint64_t u8 = ZFS_PROP_UNDEFINED;
3226 int error;
3228 ASSERT(zplprops != NULL);
3230 /* parent dataset must be a filesystem */
3231 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
3232 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
3235 * Pull out creator prop choices, if any.
3237 if (createprops) {
3238 (void) nvlist_lookup_uint64(createprops,
3239 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
3240 (void) nvlist_lookup_uint64(createprops,
3241 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3242 (void) nvlist_remove_all(createprops,
3243 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3244 (void) nvlist_lookup_uint64(createprops,
3245 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3246 (void) nvlist_remove_all(createprops,
3247 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3248 (void) nvlist_lookup_uint64(createprops,
3249 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3250 (void) nvlist_remove_all(createprops,
3251 zfs_prop_to_name(ZFS_PROP_CASE));
3255 * If the zpl version requested is whacky or the file system
3256 * or pool is version is too "young" to support normalization
3257 * and the creator tried to set a value for one of the props,
3258 * error out.
3260 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3261 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3262 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3263 (zplver < ZPL_VERSION_NORMALIZATION &&
3264 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3265 sense != ZFS_PROP_UNDEFINED)))
3266 return (SET_ERROR(ENOTSUP));
3269 * Put the version in the zplprops
3271 VERIFY(nvlist_add_uint64(zplprops,
3272 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3274 if (norm == ZFS_PROP_UNDEFINED &&
3275 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3276 return (error);
3277 VERIFY(nvlist_add_uint64(zplprops,
3278 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3281 * If we're normalizing, names must always be valid UTF-8 strings.
3283 if (norm)
3284 u8 = 1;
3285 if (u8 == ZFS_PROP_UNDEFINED &&
3286 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3287 return (error);
3288 VERIFY(nvlist_add_uint64(zplprops,
3289 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3291 if (sense == ZFS_PROP_UNDEFINED &&
3292 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3293 return (error);
3294 VERIFY(nvlist_add_uint64(zplprops,
3295 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3297 if (is_ci)
3298 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3300 return (0);
3303 static int
3304 zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3305 nvlist_t *zplprops, boolean_t *is_ci)
3307 boolean_t fuids_ok, sa_ok;
3308 uint64_t zplver = ZPL_VERSION;
3309 objset_t *os = NULL;
3310 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3311 spa_t *spa;
3312 uint64_t spa_vers;
3313 int error;
3315 zfs_get_parent(dataset, parentname, sizeof (parentname));
3317 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3318 return (error);
3320 spa_vers = spa_version(spa);
3321 spa_close(spa, FTAG);
3323 zplver = zfs_zpl_version_map(spa_vers);
3324 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3325 sa_ok = (zplver >= ZPL_VERSION_SA);
3328 * Open parent object set so we can inherit zplprop values.
3330 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
3331 return (error);
3333 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
3334 zplprops, is_ci);
3335 dmu_objset_rele(os, FTAG);
3336 return (error);
3339 static int
3340 zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3341 nvlist_t *zplprops, boolean_t *is_ci)
3343 boolean_t fuids_ok;
3344 boolean_t sa_ok;
3345 uint64_t zplver = ZPL_VERSION;
3346 int error;
3348 zplver = zfs_zpl_version_map(spa_vers);
3349 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3350 sa_ok = (zplver >= ZPL_VERSION_SA);
3352 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3353 createprops, zplprops, is_ci);
3354 return (error);
3358 * innvl: {
3359 * "type" -> dmu_objset_type_t (int32)
3360 * (optional) "props" -> { prop -> value }
3361 * (optional) "hidden_args" -> { "wkeydata" -> value }
3362 * raw uint8_t array of encryption wrapping key data (32 bytes)
3365 * outnvl: propname -> error code (int32)
3368 static const zfs_ioc_key_t zfs_keys_create[] = {
3369 {"type", DATA_TYPE_INT32, 0},
3370 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3371 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3374 static int
3375 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3377 int error = 0;
3378 zfs_creat_t zct = { 0 };
3379 nvlist_t *nvprops = NULL;
3380 nvlist_t *hidden_args = NULL;
3381 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3382 dmu_objset_type_t type;
3383 boolean_t is_insensitive = B_FALSE;
3384 dsl_crypto_params_t *dcp = NULL;
3386 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
3387 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3388 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
3390 switch (type) {
3391 case DMU_OST_ZFS:
3392 cbfunc = zfs_create_cb;
3393 break;
3395 case DMU_OST_ZVOL:
3396 cbfunc = zvol_create_cb;
3397 break;
3399 default:
3400 cbfunc = NULL;
3401 break;
3403 if (strchr(fsname, '@') ||
3404 strchr(fsname, '%'))
3405 return (SET_ERROR(EINVAL));
3407 zct.zct_props = nvprops;
3409 if (cbfunc == NULL)
3410 return (SET_ERROR(EINVAL));
3412 if (type == DMU_OST_ZVOL) {
3413 uint64_t volsize, volblocksize;
3415 if (nvprops == NULL)
3416 return (SET_ERROR(EINVAL));
3417 if (nvlist_lookup_uint64(nvprops,
3418 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3419 return (SET_ERROR(EINVAL));
3421 if ((error = nvlist_lookup_uint64(nvprops,
3422 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3423 &volblocksize)) != 0 && error != ENOENT)
3424 return (SET_ERROR(EINVAL));
3426 if (error != 0)
3427 volblocksize = zfs_prop_default_numeric(
3428 ZFS_PROP_VOLBLOCKSIZE);
3430 if ((error = zvol_check_volblocksize(fsname,
3431 volblocksize)) != 0 ||
3432 (error = zvol_check_volsize(volsize,
3433 volblocksize)) != 0)
3434 return (error);
3435 } else if (type == DMU_OST_ZFS) {
3436 int error;
3439 * We have to have normalization and
3440 * case-folding flags correct when we do the
3441 * file system creation, so go figure them out
3442 * now.
3444 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3445 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3446 error = zfs_fill_zplprops(fsname, nvprops,
3447 zct.zct_zplprops, &is_insensitive);
3448 if (error != 0) {
3449 nvlist_free(zct.zct_zplprops);
3450 return (error);
3454 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3455 hidden_args, &dcp);
3456 if (error != 0) {
3457 nvlist_free(zct.zct_zplprops);
3458 return (error);
3461 error = dmu_objset_create(fsname, type,
3462 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3464 nvlist_free(zct.zct_zplprops);
3465 dsl_crypto_params_free(dcp, !!error);
3468 * It would be nice to do this atomically.
3470 if (error == 0) {
3471 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3472 nvprops, outnvl);
3473 if (error != 0) {
3474 spa_t *spa;
3475 int error2;
3478 * Volumes will return EBUSY and cannot be destroyed
3479 * until all asynchronous minor handling (e.g. from
3480 * setting the volmode property) has completed. Wait for
3481 * the spa_zvol_taskq to drain then retry.
3483 error2 = dsl_destroy_head(fsname);
3484 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3485 error2 = spa_open(fsname, &spa, FTAG);
3486 if (error2 == 0) {
3487 taskq_wait(spa->spa_zvol_taskq);
3488 spa_close(spa, FTAG);
3490 error2 = dsl_destroy_head(fsname);
3494 return (error);
3498 * innvl: {
3499 * "origin" -> name of origin snapshot
3500 * (optional) "props" -> { prop -> value }
3501 * (optional) "hidden_args" -> { "wkeydata" -> value }
3502 * raw uint8_t array of encryption wrapping key data (32 bytes)
3505 * outputs:
3506 * outnvl: propname -> error code (int32)
3508 static const zfs_ioc_key_t zfs_keys_clone[] = {
3509 {"origin", DATA_TYPE_STRING, 0},
3510 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3511 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3514 static int
3515 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3517 int error = 0;
3518 nvlist_t *nvprops = NULL;
3519 const char *origin_name;
3521 origin_name = fnvlist_lookup_string(innvl, "origin");
3522 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3524 if (strchr(fsname, '@') ||
3525 strchr(fsname, '%'))
3526 return (SET_ERROR(EINVAL));
3528 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3529 return (SET_ERROR(EINVAL));
3531 error = dmu_objset_clone(fsname, origin_name);
3534 * It would be nice to do this atomically.
3536 if (error == 0) {
3537 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3538 nvprops, outnvl);
3539 if (error != 0)
3540 (void) dsl_destroy_head(fsname);
3542 return (error);
3545 static const zfs_ioc_key_t zfs_keys_remap[] = {
3546 /* no nvl keys */
3549 static int
3550 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3552 /* This IOCTL is no longer supported. */
3553 (void) fsname, (void) innvl, (void) outnvl;
3554 return (0);
3558 * innvl: {
3559 * "snaps" -> { snapshot1, snapshot2 }
3560 * (optional) "props" -> { prop -> value (string) }
3563 * outnvl: snapshot -> error code (int32)
3565 static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3566 {"snaps", DATA_TYPE_NVLIST, 0},
3567 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3570 static int
3571 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3573 nvlist_t *snaps;
3574 nvlist_t *props = NULL;
3575 int error, poollen;
3576 nvpair_t *pair;
3578 (void) nvlist_lookup_nvlist(innvl, "props", &props);
3579 if (!nvlist_empty(props) &&
3580 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3581 return (SET_ERROR(ENOTSUP));
3582 if ((error = zfs_check_userprops(props)) != 0)
3583 return (error);
3585 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3586 poollen = strlen(poolname);
3587 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3588 pair = nvlist_next_nvpair(snaps, pair)) {
3589 const char *name = nvpair_name(pair);
3590 char *cp = strchr(name, '@');
3593 * The snap name must contain an @, and the part after it must
3594 * contain only valid characters.
3596 if (cp == NULL ||
3597 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3598 return (SET_ERROR(EINVAL));
3601 * The snap must be in the specified pool.
3603 if (strncmp(name, poolname, poollen) != 0 ||
3604 (name[poollen] != '/' && name[poollen] != '@'))
3605 return (SET_ERROR(EXDEV));
3608 * Check for permission to set the properties on the fs.
3610 if (!nvlist_empty(props)) {
3611 *cp = '\0';
3612 error = zfs_secpolicy_write_perms(name,
3613 ZFS_DELEG_PERM_USERPROP, CRED());
3614 *cp = '@';
3615 if (error != 0)
3616 return (error);
3619 /* This must be the only snap of this fs. */
3620 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3621 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3622 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3623 == 0) {
3624 return (SET_ERROR(EXDEV));
3629 error = dsl_dataset_snapshot(snaps, props, outnvl);
3631 return (error);
3635 * innvl: "message" -> string
3637 static const zfs_ioc_key_t zfs_keys_log_history[] = {
3638 {"message", DATA_TYPE_STRING, 0},
3641 static int
3642 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3644 (void) unused, (void) outnvl;
3645 const char *message;
3646 char *poolname;
3647 spa_t *spa;
3648 int error;
3651 * The poolname in the ioctl is not set, we get it from the TSD,
3652 * which was set at the end of the last successful ioctl that allows
3653 * logging. The secpolicy func already checked that it is set.
3654 * Only one log ioctl is allowed after each successful ioctl, so
3655 * we clear the TSD here.
3657 poolname = tsd_get(zfs_allow_log_key);
3658 if (poolname == NULL)
3659 return (SET_ERROR(EINVAL));
3660 (void) tsd_set(zfs_allow_log_key, NULL);
3661 error = spa_open(poolname, &spa, FTAG);
3662 kmem_strfree(poolname);
3663 if (error != 0)
3664 return (error);
3666 message = fnvlist_lookup_string(innvl, "message");
3668 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3669 spa_close(spa, FTAG);
3670 return (SET_ERROR(ENOTSUP));
3673 error = spa_history_log(spa, message);
3674 spa_close(spa, FTAG);
3675 return (error);
3679 * This ioctl is used to set the bootenv configuration on the current
3680 * pool. This configuration is stored in the second padding area of the label,
3681 * and it is used by the bootloader(s) to store the bootloader and/or system
3682 * specific data.
3683 * The data is stored as nvlist data stream, and is protected by
3684 * an embedded checksum.
3685 * The version can have two possible values:
3686 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3687 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
3689 static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
3690 {"version", DATA_TYPE_UINT64, 0},
3691 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
3694 static int
3695 zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3697 int error;
3698 spa_t *spa;
3700 if ((error = spa_open(name, &spa, FTAG)) != 0)
3701 return (error);
3702 spa_vdev_state_enter(spa, SCL_ALL);
3703 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
3704 (void) spa_vdev_state_exit(spa, NULL, 0);
3705 spa_close(spa, FTAG);
3706 return (error);
3709 static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3710 /* no nvl keys */
3713 static int
3714 zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3716 spa_t *spa;
3717 int error;
3719 if ((error = spa_open(name, &spa, FTAG)) != 0)
3720 return (error);
3721 spa_vdev_state_enter(spa, SCL_ALL);
3722 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3723 (void) spa_vdev_state_exit(spa, NULL, 0);
3724 spa_close(spa, FTAG);
3725 return (error);
3729 * The dp_config_rwlock must not be held when calling this, because the
3730 * unmount may need to write out data.
3732 * This function is best-effort. Callers must deal gracefully if it
3733 * remains mounted (or is remounted after this call).
3735 * Returns 0 if the argument is not a snapshot, or it is not currently a
3736 * filesystem, or we were able to unmount it. Returns error code otherwise.
3738 void
3739 zfs_unmount_snap(const char *snapname)
3741 if (strchr(snapname, '@') == NULL)
3742 return;
3744 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE);
3747 static int
3748 zfs_unmount_snap_cb(const char *snapname, void *arg)
3750 (void) arg;
3751 zfs_unmount_snap(snapname);
3752 return (0);
3756 * When a clone is destroyed, its origin may also need to be destroyed,
3757 * in which case it must be unmounted. This routine will do that unmount
3758 * if necessary.
3760 void
3761 zfs_destroy_unmount_origin(const char *fsname)
3763 int error;
3764 objset_t *os;
3765 dsl_dataset_t *ds;
3767 error = dmu_objset_hold(fsname, FTAG, &os);
3768 if (error != 0)
3769 return;
3770 ds = dmu_objset_ds(os);
3771 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3772 char originname[ZFS_MAX_DATASET_NAME_LEN];
3773 dsl_dataset_name(ds->ds_prev, originname);
3774 dmu_objset_rele(os, FTAG);
3775 zfs_unmount_snap(originname);
3776 } else {
3777 dmu_objset_rele(os, FTAG);
3782 * innvl: {
3783 * "snaps" -> { snapshot1, snapshot2 }
3784 * (optional boolean) "defer"
3787 * outnvl: snapshot -> error code (int32)
3789 static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3790 {"snaps", DATA_TYPE_NVLIST, 0},
3791 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
3794 static int
3795 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3797 int poollen;
3798 nvlist_t *snaps;
3799 nvpair_t *pair;
3800 boolean_t defer;
3801 spa_t *spa;
3803 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
3804 defer = nvlist_exists(innvl, "defer");
3806 poollen = strlen(poolname);
3807 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3808 pair = nvlist_next_nvpair(snaps, pair)) {
3809 const char *name = nvpair_name(pair);
3812 * The snap must be in the specified pool to prevent the
3813 * invalid removal of zvol minors below.
3815 if (strncmp(name, poolname, poollen) != 0 ||
3816 (name[poollen] != '/' && name[poollen] != '@'))
3817 return (SET_ERROR(EXDEV));
3819 zfs_unmount_snap(nvpair_name(pair));
3820 if (spa_open(name, &spa, FTAG) == 0) {
3821 zvol_remove_minors(spa, name, B_TRUE);
3822 spa_close(spa, FTAG);
3826 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3830 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>.
3831 * All bookmarks and snapshots must be in the same pool.
3832 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail.
3834 * innvl: {
3835 * new_bookmark1 -> existing_snapshot,
3836 * new_bookmark2 -> existing_bookmark,
3839 * outnvl: bookmark -> error code (int32)
3842 static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3843 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3846 static int
3847 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3849 (void) poolname;
3850 return (dsl_bookmark_create(innvl, outnvl));
3854 * innvl: {
3855 * property 1, property 2, ...
3858 * outnvl: {
3859 * bookmark name 1 -> { property 1, property 2, ... },
3860 * bookmark name 2 -> { property 1, property 2, ... }
3864 static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3865 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3868 static int
3869 zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3871 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3875 * innvl is not used.
3877 * outnvl: {
3878 * property 1, property 2, ...
3882 static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
3883 /* no nvl keys */
3886 static int
3887 zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
3888 nvlist_t *outnvl)
3890 (void) innvl;
3891 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3892 char *bmname;
3894 bmname = strchr(bookmark, '#');
3895 if (bmname == NULL)
3896 return (SET_ERROR(EINVAL));
3897 bmname++;
3899 (void) strlcpy(fsname, bookmark, sizeof (fsname));
3900 *(strchr(fsname, '#')) = '\0';
3902 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
3906 * innvl: {
3907 * bookmark name 1, bookmark name 2
3910 * outnvl: bookmark -> error code (int32)
3913 static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3914 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3917 static int
3918 zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3919 nvlist_t *outnvl)
3921 int error, poollen;
3923 poollen = strlen(poolname);
3924 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3925 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3926 const char *name = nvpair_name(pair);
3927 const char *cp = strchr(name, '#');
3930 * The bookmark name must contain an #, and the part after it
3931 * must contain only valid characters.
3933 if (cp == NULL ||
3934 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3935 return (SET_ERROR(EINVAL));
3938 * The bookmark must be in the specified pool.
3940 if (strncmp(name, poolname, poollen) != 0 ||
3941 (name[poollen] != '/' && name[poollen] != '#'))
3942 return (SET_ERROR(EXDEV));
3945 error = dsl_bookmark_destroy(innvl, outnvl);
3946 return (error);
3949 static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3950 {"program", DATA_TYPE_STRING, 0},
3951 {"arg", DATA_TYPE_ANY, 0},
3952 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3953 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3954 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3957 static int
3958 zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3959 nvlist_t *outnvl)
3961 const char *program;
3962 uint64_t instrlimit, memlimit;
3963 boolean_t sync_flag;
3964 nvpair_t *nvarg = NULL;
3966 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
3967 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3968 sync_flag = B_TRUE;
3970 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3971 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3973 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3974 memlimit = ZCP_DEFAULT_MEMLIMIT;
3976 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
3978 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3979 return (SET_ERROR(EINVAL));
3980 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3981 return (SET_ERROR(EINVAL));
3983 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3984 nvarg, outnvl));
3988 * innvl: unused
3989 * outnvl: empty
3991 static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3992 /* no nvl keys */
3995 static int
3996 zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3998 (void) innvl, (void) outnvl;
3999 return (spa_checkpoint(poolname));
4003 * innvl: unused
4004 * outnvl: empty
4006 static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
4007 /* no nvl keys */
4010 static int
4011 zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
4012 nvlist_t *outnvl)
4014 (void) innvl, (void) outnvl;
4015 return (spa_checkpoint_discard(poolname));
4019 * inputs:
4020 * zc_name name of dataset to destroy
4021 * zc_defer_destroy mark for deferred destroy
4023 * outputs: none
4025 static int
4026 zfs_ioc_destroy(zfs_cmd_t *zc)
4028 objset_t *os;
4029 dmu_objset_type_t ost;
4030 int err;
4032 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4033 if (err != 0)
4034 return (err);
4035 ost = dmu_objset_type(os);
4036 dmu_objset_rele(os, FTAG);
4038 if (ost == DMU_OST_ZFS)
4039 zfs_unmount_snap(zc->zc_name);
4041 if (strchr(zc->zc_name, '@')) {
4042 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
4043 } else {
4044 err = dsl_destroy_head(zc->zc_name);
4045 if (err == EEXIST) {
4047 * It is possible that the given DS may have
4048 * hidden child (%recv) datasets - "leftovers"
4049 * resulting from the previously interrupted
4050 * 'zfs receive'.
4052 * 6 extra bytes for /%recv
4054 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
4056 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
4057 zc->zc_name, recv_clone_name) >=
4058 sizeof (namebuf))
4059 return (SET_ERROR(EINVAL));
4062 * Try to remove the hidden child (%recv) and after
4063 * that try to remove the target dataset.
4064 * If the hidden child (%recv) does not exist
4065 * the original error (EEXIST) will be returned
4067 err = dsl_destroy_head(namebuf);
4068 if (err == 0)
4069 err = dsl_destroy_head(zc->zc_name);
4070 else if (err == ENOENT)
4071 err = SET_ERROR(EEXIST);
4075 return (err);
4079 * innvl: {
4080 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
4081 * "initialize_vdevs": { -> guids to initialize (nvlist)
4082 * "vdev_path_1": vdev_guid_1, (uint64),
4083 * "vdev_path_2": vdev_guid_2, (uint64),
4084 * ...
4085 * },
4088 * outnvl: {
4089 * "initialize_vdevs": { -> initialization errors (nvlist)
4090 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4091 * "vdev_path_2": errno, ... (uint64)
4092 * ...
4096 * EINVAL is returned for an unknown commands or if any of the provided vdev
4097 * guids have be specified with a type other than uint64.
4099 static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
4100 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
4101 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4104 static int
4105 zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4107 uint64_t cmd_type;
4108 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4109 &cmd_type) != 0) {
4110 return (SET_ERROR(EINVAL));
4113 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
4114 cmd_type == POOL_INITIALIZE_START ||
4115 cmd_type == POOL_INITIALIZE_SUSPEND ||
4116 cmd_type == POOL_INITIALIZE_UNINIT)) {
4117 return (SET_ERROR(EINVAL));
4120 nvlist_t *vdev_guids;
4121 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4122 &vdev_guids) != 0) {
4123 return (SET_ERROR(EINVAL));
4126 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4127 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4128 uint64_t vdev_guid;
4129 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4130 return (SET_ERROR(EINVAL));
4134 spa_t *spa;
4135 int error = spa_open(poolname, &spa, FTAG);
4136 if (error != 0)
4137 return (error);
4139 nvlist_t *vdev_errlist = fnvlist_alloc();
4140 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4141 vdev_errlist);
4143 if (fnvlist_size(vdev_errlist) > 0) {
4144 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4145 vdev_errlist);
4147 fnvlist_free(vdev_errlist);
4149 spa_close(spa, FTAG);
4150 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4154 * innvl: {
4155 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4156 * "trim_vdevs": { -> guids to TRIM (nvlist)
4157 * "vdev_path_1": vdev_guid_1, (uint64),
4158 * "vdev_path_2": vdev_guid_2, (uint64),
4159 * ...
4160 * },
4161 * "trim_rate" -> Target TRIM rate in bytes/sec.
4162 * "trim_secure" -> Set to request a secure TRIM.
4165 * outnvl: {
4166 * "trim_vdevs": { -> TRIM errors (nvlist)
4167 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4168 * "vdev_path_2": errno, ... (uint64)
4169 * ...
4173 * EINVAL is returned for an unknown commands or if any of the provided vdev
4174 * guids have be specified with a type other than uint64.
4176 static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4177 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4178 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4179 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4180 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4183 static int
4184 zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4186 uint64_t cmd_type;
4187 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4188 return (SET_ERROR(EINVAL));
4190 if (!(cmd_type == POOL_TRIM_CANCEL ||
4191 cmd_type == POOL_TRIM_START ||
4192 cmd_type == POOL_TRIM_SUSPEND)) {
4193 return (SET_ERROR(EINVAL));
4196 nvlist_t *vdev_guids;
4197 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4198 return (SET_ERROR(EINVAL));
4200 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4201 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4202 uint64_t vdev_guid;
4203 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4204 return (SET_ERROR(EINVAL));
4208 /* Optional, defaults to maximum rate when not provided */
4209 uint64_t rate;
4210 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4211 rate = 0;
4213 /* Optional, defaults to standard TRIM when not provided */
4214 boolean_t secure;
4215 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4216 &secure) != 0) {
4217 secure = B_FALSE;
4220 spa_t *spa;
4221 int error = spa_open(poolname, &spa, FTAG);
4222 if (error != 0)
4223 return (error);
4225 nvlist_t *vdev_errlist = fnvlist_alloc();
4226 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4227 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4229 if (fnvlist_size(vdev_errlist) > 0)
4230 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4232 fnvlist_free(vdev_errlist);
4234 spa_close(spa, FTAG);
4235 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
4239 * This ioctl waits for activity of a particular type to complete. If there is
4240 * no activity of that type in progress, it returns immediately, and the
4241 * returned value "waited" is false. If there is activity in progress, and no
4242 * tag is passed in, the ioctl blocks until all activity of that type is
4243 * complete, and then returns with "waited" set to true.
4245 * If a tag is provided, it identifies a particular instance of an activity to
4246 * wait for. Currently, this is only valid for use with 'initialize', because
4247 * that is the only activity for which there can be multiple instances running
4248 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4249 * the vdev on which to wait.
4251 * If a thread waiting in the ioctl receives a signal, the call will return
4252 * immediately, and the return value will be EINTR.
4254 * innvl: {
4255 * "wait_activity" -> int32_t
4256 * (optional) "wait_tag" -> uint64_t
4259 * outnvl: "waited" -> boolean_t
4261 static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4262 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4263 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4266 static int
4267 zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4269 int32_t activity;
4270 uint64_t tag;
4271 boolean_t waited;
4272 int error;
4274 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4275 return (EINVAL);
4277 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4278 error = spa_wait_tag(name, activity, tag, &waited);
4279 else
4280 error = spa_wait(name, activity, &waited);
4282 if (error == 0)
4283 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4285 return (error);
4289 * This ioctl waits for activity of a particular type to complete. If there is
4290 * no activity of that type in progress, it returns immediately, and the
4291 * returned value "waited" is false. If there is activity in progress, and no
4292 * tag is passed in, the ioctl blocks until all activity of that type is
4293 * complete, and then returns with "waited" set to true.
4295 * If a thread waiting in the ioctl receives a signal, the call will return
4296 * immediately, and the return value will be EINTR.
4298 * innvl: {
4299 * "wait_activity" -> int32_t
4302 * outnvl: "waited" -> boolean_t
4304 static const zfs_ioc_key_t zfs_keys_fs_wait[] = {
4305 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4308 static int
4309 zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4311 int32_t activity;
4312 boolean_t waited = B_FALSE;
4313 int error;
4314 dsl_pool_t *dp;
4315 dsl_dir_t *dd;
4316 dsl_dataset_t *ds;
4318 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0)
4319 return (SET_ERROR(EINVAL));
4321 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0)
4322 return (SET_ERROR(EINVAL));
4324 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
4325 return (error);
4327 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
4328 dsl_pool_rele(dp, FTAG);
4329 return (error);
4332 dd = ds->ds_dir;
4333 mutex_enter(&dd->dd_activity_lock);
4334 dd->dd_activity_waiters++;
4337 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t
4338 * aren't evicted while we're waiting. Normally this is prevented by
4339 * holding the pool, but we can't do that while we're waiting since
4340 * that would prevent TXGs from syncing out. Some of the functionality
4341 * of long-holds (e.g. preventing deletion) is unnecessary for this
4342 * case, since we would cancel the waiters before proceeding with a
4343 * deletion. An alternative mechanism for keeping the dataset around
4344 * could be developed but this is simpler.
4346 dsl_dataset_long_hold(ds, FTAG);
4347 dsl_pool_rele(dp, FTAG);
4349 error = dsl_dir_wait(dd, ds, activity, &waited);
4351 dsl_dataset_long_rele(ds, FTAG);
4352 dd->dd_activity_waiters--;
4353 if (dd->dd_activity_waiters == 0)
4354 cv_signal(&dd->dd_activity_cv);
4355 mutex_exit(&dd->dd_activity_lock);
4357 dsl_dataset_rele(ds, FTAG);
4359 if (error == 0)
4360 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited);
4362 return (error);
4366 * fsname is name of dataset to rollback (to most recent snapshot)
4368 * innvl may contain name of expected target snapshot
4370 * outnvl: "target" -> name of most recent snapshot
4373 static const zfs_ioc_key_t zfs_keys_rollback[] = {
4374 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4377 static int
4378 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4380 zfsvfs_t *zfsvfs;
4381 zvol_state_handle_t *zv;
4382 const char *target = NULL;
4383 int error;
4385 (void) nvlist_lookup_string(innvl, "target", &target);
4386 if (target != NULL) {
4387 const char *cp = strchr(target, '@');
4390 * The snap name must contain an @, and the part after it must
4391 * contain only valid characters.
4393 if (cp == NULL ||
4394 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4395 return (SET_ERROR(EINVAL));
4398 if (getzfsvfs(fsname, &zfsvfs) == 0) {
4399 dsl_dataset_t *ds;
4401 ds = dmu_objset_ds(zfsvfs->z_os);
4402 error = zfs_suspend_fs(zfsvfs);
4403 if (error == 0) {
4404 int resume_err;
4406 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4407 outnvl);
4408 resume_err = zfs_resume_fs(zfsvfs, ds);
4409 error = error ? error : resume_err;
4411 zfs_vfs_rele(zfsvfs);
4412 } else if ((zv = zvol_suspend(fsname)) != NULL) {
4413 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4414 outnvl);
4415 zvol_resume(zv);
4416 } else {
4417 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4419 return (error);
4422 static int
4423 recursive_unmount(const char *fsname, void *arg)
4425 const char *snapname = arg;
4426 char *fullname;
4428 fullname = kmem_asprintf("%s@%s", fsname, snapname);
4429 zfs_unmount_snap(fullname);
4430 kmem_strfree(fullname);
4432 return (0);
4437 * snapname is the snapshot to redact.
4438 * innvl: {
4439 * "bookname" -> (string)
4440 * shortname of the redaction bookmark to generate
4441 * "snapnv" -> (nvlist, values ignored)
4442 * snapshots to redact snapname with respect to
4445 * outnvl is unused
4448 static const zfs_ioc_key_t zfs_keys_redact[] = {
4449 {"bookname", DATA_TYPE_STRING, 0},
4450 {"snapnv", DATA_TYPE_NVLIST, 0},
4453 static int
4454 zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4456 (void) outnvl;
4457 nvlist_t *redactnvl = NULL;
4458 const char *redactbook = NULL;
4460 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4461 return (SET_ERROR(EINVAL));
4462 if (fnvlist_num_pairs(redactnvl) == 0)
4463 return (SET_ERROR(ENXIO));
4464 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4465 return (SET_ERROR(EINVAL));
4467 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4471 * inputs:
4472 * zc_name old name of dataset
4473 * zc_value new name of dataset
4474 * zc_cookie recursive flag (only valid for snapshots)
4476 * outputs: none
4478 static int
4479 zfs_ioc_rename(zfs_cmd_t *zc)
4481 objset_t *os;
4482 dmu_objset_type_t ost;
4483 boolean_t recursive = zc->zc_cookie & 1;
4484 boolean_t nounmount = !!(zc->zc_cookie & 2);
4485 char *at;
4486 int err;
4488 /* "zfs rename" from and to ...%recv datasets should both fail */
4489 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4490 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4491 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4492 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4493 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
4494 return (SET_ERROR(EINVAL));
4496 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4497 if (err != 0)
4498 return (err);
4499 ost = dmu_objset_type(os);
4500 dmu_objset_rele(os, FTAG);
4502 at = strchr(zc->zc_name, '@');
4503 if (at != NULL) {
4504 /* snaps must be in same fs */
4505 int error;
4507 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4508 return (SET_ERROR(EXDEV));
4509 *at = '\0';
4510 if (ost == DMU_OST_ZFS && !nounmount) {
4511 error = dmu_objset_find(zc->zc_name,
4512 recursive_unmount, at + 1,
4513 recursive ? DS_FIND_CHILDREN : 0);
4514 if (error != 0) {
4515 *at = '@';
4516 return (error);
4519 error = dsl_dataset_rename_snapshot(zc->zc_name,
4520 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4521 *at = '@';
4523 return (error);
4524 } else {
4525 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
4529 static int
4530 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4532 const char *propname = nvpair_name(pair);
4533 boolean_t issnap = (strchr(dsname, '@') != NULL);
4534 zfs_prop_t prop = zfs_name_to_prop(propname);
4535 uint64_t intval, compval;
4536 int err;
4538 if (prop == ZPROP_USERPROP) {
4539 if (zfs_prop_user(propname)) {
4540 if ((err = zfs_secpolicy_write_perms(dsname,
4541 ZFS_DELEG_PERM_USERPROP, cr)))
4542 return (err);
4543 return (0);
4546 if (!issnap && zfs_prop_userquota(propname)) {
4547 const char *perm = NULL;
4548 const char *uq_prefix =
4549 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4550 const char *gq_prefix =
4551 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
4552 const char *uiq_prefix =
4553 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4554 const char *giq_prefix =
4555 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
4556 const char *pq_prefix =
4557 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4558 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4559 ZFS_PROP_PROJECTOBJQUOTA];
4561 if (strncmp(propname, uq_prefix,
4562 strlen(uq_prefix)) == 0) {
4563 perm = ZFS_DELEG_PERM_USERQUOTA;
4564 } else if (strncmp(propname, uiq_prefix,
4565 strlen(uiq_prefix)) == 0) {
4566 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
4567 } else if (strncmp(propname, gq_prefix,
4568 strlen(gq_prefix)) == 0) {
4569 perm = ZFS_DELEG_PERM_GROUPQUOTA;
4570 } else if (strncmp(propname, giq_prefix,
4571 strlen(giq_prefix)) == 0) {
4572 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
4573 } else if (strncmp(propname, pq_prefix,
4574 strlen(pq_prefix)) == 0) {
4575 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4576 } else if (strncmp(propname, piq_prefix,
4577 strlen(piq_prefix)) == 0) {
4578 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
4579 } else {
4580 /* {USER|GROUP|PROJECT}USED are read-only */
4581 return (SET_ERROR(EINVAL));
4584 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
4585 return (err);
4586 return (0);
4589 return (SET_ERROR(EINVAL));
4592 if (issnap)
4593 return (SET_ERROR(EINVAL));
4595 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4597 * dsl_prop_get_all_impl() returns properties in this
4598 * format.
4600 nvlist_t *attrs;
4601 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4602 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4603 &pair) == 0);
4607 * Check that this value is valid for this pool version
4609 switch (prop) {
4610 case ZFS_PROP_COMPRESSION:
4612 * If the user specified gzip compression, make sure
4613 * the SPA supports it. We ignore any errors here since
4614 * we'll catch them later.
4616 if (nvpair_value_uint64(pair, &intval) == 0) {
4617 compval = ZIO_COMPRESS_ALGO(intval);
4618 if (compval >= ZIO_COMPRESS_GZIP_1 &&
4619 compval <= ZIO_COMPRESS_GZIP_9 &&
4620 zfs_earlier_version(dsname,
4621 SPA_VERSION_GZIP_COMPRESSION)) {
4622 return (SET_ERROR(ENOTSUP));
4625 if (compval == ZIO_COMPRESS_ZLE &&
4626 zfs_earlier_version(dsname,
4627 SPA_VERSION_ZLE_COMPRESSION))
4628 return (SET_ERROR(ENOTSUP));
4630 if (compval == ZIO_COMPRESS_LZ4) {
4631 spa_t *spa;
4633 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4634 return (err);
4636 if (!spa_feature_is_enabled(spa,
4637 SPA_FEATURE_LZ4_COMPRESS)) {
4638 spa_close(spa, FTAG);
4639 return (SET_ERROR(ENOTSUP));
4641 spa_close(spa, FTAG);
4644 if (compval == ZIO_COMPRESS_ZSTD) {
4645 spa_t *spa;
4647 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4648 return (err);
4650 if (!spa_feature_is_enabled(spa,
4651 SPA_FEATURE_ZSTD_COMPRESS)) {
4652 spa_close(spa, FTAG);
4653 return (SET_ERROR(ENOTSUP));
4655 spa_close(spa, FTAG);
4658 break;
4660 case ZFS_PROP_COPIES:
4661 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
4662 return (SET_ERROR(ENOTSUP));
4663 break;
4665 case ZFS_PROP_VOLBLOCKSIZE:
4666 case ZFS_PROP_RECORDSIZE:
4667 /* Record sizes above 128k need the feature to be enabled */
4668 if (nvpair_value_uint64(pair, &intval) == 0 &&
4669 intval > SPA_OLD_MAXBLOCKSIZE) {
4670 spa_t *spa;
4673 * We don't allow setting the property above 1MB,
4674 * unless the tunable has been changed.
4676 if (intval > zfs_max_recordsize ||
4677 intval > SPA_MAXBLOCKSIZE)
4678 return (SET_ERROR(ERANGE));
4680 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4681 return (err);
4683 if (!spa_feature_is_enabled(spa,
4684 SPA_FEATURE_LARGE_BLOCKS)) {
4685 spa_close(spa, FTAG);
4686 return (SET_ERROR(ENOTSUP));
4688 spa_close(spa, FTAG);
4690 break;
4692 case ZFS_PROP_DNODESIZE:
4693 /* Dnode sizes above 512 need the feature to be enabled */
4694 if (nvpair_value_uint64(pair, &intval) == 0 &&
4695 intval != ZFS_DNSIZE_LEGACY) {
4696 spa_t *spa;
4698 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4699 return (err);
4701 if (!spa_feature_is_enabled(spa,
4702 SPA_FEATURE_LARGE_DNODE)) {
4703 spa_close(spa, FTAG);
4704 return (SET_ERROR(ENOTSUP));
4706 spa_close(spa, FTAG);
4708 break;
4710 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4712 * This property could require the allocation classes
4713 * feature to be active for setting, however we allow
4714 * it so that tests of settable properties succeed.
4715 * The CLI will issue a warning in this case.
4717 break;
4719 case ZFS_PROP_SHARESMB:
4720 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4721 return (SET_ERROR(ENOTSUP));
4722 break;
4724 case ZFS_PROP_ACLINHERIT:
4725 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4726 nvpair_value_uint64(pair, &intval) == 0) {
4727 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4728 zfs_earlier_version(dsname,
4729 SPA_VERSION_PASSTHROUGH_X))
4730 return (SET_ERROR(ENOTSUP));
4732 break;
4733 case ZFS_PROP_CHECKSUM:
4734 case ZFS_PROP_DEDUP:
4736 spa_feature_t feature;
4737 spa_t *spa;
4738 int err;
4740 /* dedup feature version checks */
4741 if (prop == ZFS_PROP_DEDUP &&
4742 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4743 return (SET_ERROR(ENOTSUP));
4745 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4746 nvpair_value_uint64(pair, &intval) == 0) {
4747 /* check prop value is enabled in features */
4748 feature = zio_checksum_to_feature(
4749 intval & ZIO_CHECKSUM_MASK);
4750 if (feature == SPA_FEATURE_NONE)
4751 break;
4753 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4754 return (err);
4756 if (!spa_feature_is_enabled(spa, feature)) {
4757 spa_close(spa, FTAG);
4758 return (SET_ERROR(ENOTSUP));
4760 spa_close(spa, FTAG);
4762 break;
4765 default:
4766 break;
4769 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4773 * Removes properties from the given props list that fail permission checks
4774 * needed to clear them and to restore them in case of a receive error. For each
4775 * property, make sure we have both set and inherit permissions.
4777 * Returns the first error encountered if any permission checks fail. If the
4778 * caller provides a non-NULL errlist, it also gives the complete list of names
4779 * of all the properties that failed a permission check along with the
4780 * corresponding error numbers. The caller is responsible for freeing the
4781 * returned errlist.
4783 * If every property checks out successfully, zero is returned and the list
4784 * pointed at by errlist is NULL.
4786 static int
4787 zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist)
4789 zfs_cmd_t *zc;
4790 nvpair_t *pair, *next_pair;
4791 nvlist_t *errors;
4792 int err, rv = 0;
4794 if (props == NULL)
4795 return (0);
4797 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4799 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
4800 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
4801 pair = nvlist_next_nvpair(props, NULL);
4802 while (pair != NULL) {
4803 next_pair = nvlist_next_nvpair(props, pair);
4805 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4806 sizeof (zc->zc_value));
4807 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
4808 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
4809 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4810 VERIFY(nvlist_add_int32(errors,
4811 zc->zc_value, err) == 0);
4813 pair = next_pair;
4815 kmem_free(zc, sizeof (zfs_cmd_t));
4817 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4818 nvlist_free(errors);
4819 errors = NULL;
4820 } else {
4821 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4824 if (errlist == NULL)
4825 nvlist_free(errors);
4826 else
4827 *errlist = errors;
4829 return (rv);
4832 static boolean_t
4833 propval_equals(nvpair_t *p1, nvpair_t *p2)
4835 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4836 /* dsl_prop_get_all_impl() format */
4837 nvlist_t *attrs;
4838 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4839 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4840 &p1) == 0);
4843 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4844 nvlist_t *attrs;
4845 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4846 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4847 &p2) == 0);
4850 if (nvpair_type(p1) != nvpair_type(p2))
4851 return (B_FALSE);
4853 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4854 const char *valstr1, *valstr2;
4856 VERIFY(nvpair_value_string(p1, &valstr1) == 0);
4857 VERIFY(nvpair_value_string(p2, &valstr2) == 0);
4858 return (strcmp(valstr1, valstr2) == 0);
4859 } else {
4860 uint64_t intval1, intval2;
4862 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4863 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4864 return (intval1 == intval2);
4869 * Remove properties from props if they are not going to change (as determined
4870 * by comparison with origprops). Remove them from origprops as well, since we
4871 * do not need to clear or restore properties that won't change.
4873 static void
4874 props_reduce(nvlist_t *props, nvlist_t *origprops)
4876 nvpair_t *pair, *next_pair;
4878 if (origprops == NULL)
4879 return; /* all props need to be received */
4881 pair = nvlist_next_nvpair(props, NULL);
4882 while (pair != NULL) {
4883 const char *propname = nvpair_name(pair);
4884 nvpair_t *match;
4886 next_pair = nvlist_next_nvpair(props, pair);
4888 if ((nvlist_lookup_nvpair(origprops, propname,
4889 &match) != 0) || !propval_equals(pair, match))
4890 goto next; /* need to set received value */
4892 /* don't clear the existing received value */
4893 (void) nvlist_remove_nvpair(origprops, match);
4894 /* don't bother receiving the property */
4895 (void) nvlist_remove_nvpair(props, pair);
4896 next:
4897 pair = next_pair;
4902 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4903 * For example, refquota cannot be set until after the receipt of a dataset,
4904 * because in replication streams, an older/earlier snapshot may exceed the
4905 * refquota. We want to receive the older/earlier snapshot, but setting
4906 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4907 * the older/earlier snapshot from being received (with EDQUOT).
4909 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4911 * libzfs will need to be judicious handling errors encountered by props
4912 * extracted by this function.
4914 static nvlist_t *
4915 extract_delay_props(nvlist_t *props)
4917 nvlist_t *delayprops;
4918 nvpair_t *nvp, *tmp;
4919 static const zfs_prop_t delayable[] = {
4920 ZFS_PROP_REFQUOTA,
4921 ZFS_PROP_KEYLOCATION,
4923 * Setting ZFS_PROP_SHARESMB requires the objset type to be
4924 * known, which is not possible prior to receipt of raw sends.
4926 ZFS_PROP_SHARESMB,
4929 int i;
4931 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4933 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4934 nvp = nvlist_next_nvpair(props, nvp)) {
4936 * strcmp() is safe because zfs_prop_to_name() always returns
4937 * a bounded string.
4939 for (i = 0; delayable[i] != 0; i++) {
4940 if (strcmp(zfs_prop_to_name(delayable[i]),
4941 nvpair_name(nvp)) == 0) {
4942 break;
4945 if (delayable[i] != 0) {
4946 tmp = nvlist_prev_nvpair(props, nvp);
4947 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4948 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4949 nvp = tmp;
4953 if (nvlist_empty(delayprops)) {
4954 nvlist_free(delayprops);
4955 delayprops = NULL;
4957 return (delayprops);
4960 static void
4961 zfs_allow_log_destroy(void *arg)
4963 char *poolname = arg;
4965 if (poolname != NULL)
4966 kmem_strfree(poolname);
4969 #ifdef ZFS_DEBUG
4970 static boolean_t zfs_ioc_recv_inject_err;
4971 #endif
4974 * nvlist 'errors' is always allocated. It will contain descriptions of
4975 * encountered errors, if any. It's the callers responsibility to free.
4977 static int
4978 zfs_ioc_recv_impl(char *tofs, char *tosnap, const char *origin,
4979 nvlist_t *recvprops, nvlist_t *localprops, nvlist_t *hidden_args,
4980 boolean_t force, boolean_t heal, boolean_t resumable, int input_fd,
4981 dmu_replay_record_t *begin_record, uint64_t *read_bytes,
4982 uint64_t *errflags, nvlist_t **errors)
4984 dmu_recv_cookie_t drc;
4985 int error = 0;
4986 int props_error = 0;
4987 offset_t off, noff;
4988 nvlist_t *local_delayprops = NULL;
4989 nvlist_t *recv_delayprops = NULL;
4990 nvlist_t *inherited_delayprops = NULL;
4991 nvlist_t *origprops = NULL; /* existing properties */
4992 nvlist_t *origrecvd = NULL; /* existing received properties */
4993 boolean_t first_recvd_props = B_FALSE;
4994 boolean_t tofs_was_redacted;
4995 zfs_file_t *input_fp;
4997 *read_bytes = 0;
4998 *errflags = 0;
4999 *errors = fnvlist_alloc();
5000 off = 0;
5002 if ((input_fp = zfs_file_get(input_fd)) == NULL)
5003 return (SET_ERROR(EBADF));
5005 noff = off = zfs_file_off(input_fp);
5006 error = dmu_recv_begin(tofs, tosnap, begin_record, force, heal,
5007 resumable, localprops, hidden_args, origin, &drc, input_fp,
5008 &off);
5009 if (error != 0)
5010 goto out;
5011 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
5014 * Set properties before we receive the stream so that they are applied
5015 * to the new data. Note that we must call dmu_recv_stream() if
5016 * dmu_recv_begin() succeeds.
5018 if (recvprops != NULL && !drc.drc_newfs) {
5019 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
5020 SPA_VERSION_RECVD_PROPS &&
5021 !dsl_prop_get_hasrecvd(tofs))
5022 first_recvd_props = B_TRUE;
5025 * If new received properties are supplied, they are to
5026 * completely replace the existing received properties,
5027 * so stash away the existing ones.
5029 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
5030 nvlist_t *errlist = NULL;
5032 * Don't bother writing a property if its value won't
5033 * change (and avoid the unnecessary security checks).
5035 * The first receive after SPA_VERSION_RECVD_PROPS is a
5036 * special case where we blow away all local properties
5037 * regardless.
5039 if (!first_recvd_props)
5040 props_reduce(recvprops, origrecvd);
5041 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
5042 (void) nvlist_merge(*errors, errlist, 0);
5043 nvlist_free(errlist);
5045 if (clear_received_props(tofs, origrecvd,
5046 first_recvd_props ? NULL : recvprops) != 0)
5047 *errflags |= ZPROP_ERR_NOCLEAR;
5048 } else {
5049 *errflags |= ZPROP_ERR_NOCLEAR;
5054 * Stash away existing properties so we can restore them on error unless
5055 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
5056 * case "origrecvd" will take care of that.
5058 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
5059 objset_t *os;
5060 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
5061 if (dsl_prop_get_all(os, &origprops) != 0) {
5062 *errflags |= ZPROP_ERR_NOCLEAR;
5064 dmu_objset_rele(os, FTAG);
5065 } else {
5066 *errflags |= ZPROP_ERR_NOCLEAR;
5070 if (recvprops != NULL) {
5071 props_error = dsl_prop_set_hasrecvd(tofs);
5073 if (props_error == 0) {
5074 recv_delayprops = extract_delay_props(recvprops);
5075 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5076 recvprops, *errors);
5080 if (localprops != NULL) {
5081 nvlist_t *oprops = fnvlist_alloc();
5082 nvlist_t *xprops = fnvlist_alloc();
5083 nvpair_t *nvp = NULL;
5085 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5086 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
5087 /* -x property */
5088 const char *name = nvpair_name(nvp);
5089 zfs_prop_t prop = zfs_name_to_prop(name);
5090 if (prop != ZPROP_USERPROP) {
5091 if (!zfs_prop_inheritable(prop))
5092 continue;
5093 } else if (!zfs_prop_user(name))
5094 continue;
5095 fnvlist_add_boolean(xprops, name);
5096 } else {
5097 /* -o property=value */
5098 fnvlist_add_nvpair(oprops, nvp);
5102 local_delayprops = extract_delay_props(oprops);
5103 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5104 oprops, *errors);
5105 inherited_delayprops = extract_delay_props(xprops);
5106 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5107 xprops, *errors);
5109 nvlist_free(oprops);
5110 nvlist_free(xprops);
5113 error = dmu_recv_stream(&drc, &off);
5115 if (error == 0) {
5116 zfsvfs_t *zfsvfs = NULL;
5117 zvol_state_handle_t *zv = NULL;
5119 if (getzfsvfs(tofs, &zfsvfs) == 0) {
5120 /* online recv */
5121 dsl_dataset_t *ds;
5122 int end_err;
5123 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
5124 begin_record->drr_u.drr_begin.
5125 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
5127 ds = dmu_objset_ds(zfsvfs->z_os);
5128 error = zfs_suspend_fs(zfsvfs);
5130 * If the suspend fails, then the recv_end will
5131 * likely also fail, and clean up after itself.
5133 end_err = dmu_recv_end(&drc, zfsvfs);
5135 * If the dataset was not redacted, but we received a
5136 * redacted stream onto it, we need to unmount the
5137 * dataset. Otherwise, resume the filesystem.
5139 if (error == 0 && !drc.drc_newfs &&
5140 stream_is_redacted && !tofs_was_redacted) {
5141 error = zfs_end_fs(zfsvfs, ds);
5142 } else if (error == 0) {
5143 error = zfs_resume_fs(zfsvfs, ds);
5145 error = error ? error : end_err;
5146 zfs_vfs_rele(zfsvfs);
5147 } else if ((zv = zvol_suspend(tofs)) != NULL) {
5148 error = dmu_recv_end(&drc, zvol_tag(zv));
5149 zvol_resume(zv);
5150 } else {
5151 error = dmu_recv_end(&drc, NULL);
5154 /* Set delayed properties now, after we're done receiving. */
5155 if (recv_delayprops != NULL && error == 0) {
5156 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
5157 recv_delayprops, *errors);
5159 if (local_delayprops != NULL && error == 0) {
5160 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5161 local_delayprops, *errors);
5163 if (inherited_delayprops != NULL && error == 0) {
5164 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5165 inherited_delayprops, *errors);
5170 * Merge delayed props back in with initial props, in case
5171 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
5172 * we have to make sure clear_received_props() includes
5173 * the delayed properties).
5175 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
5176 * using ASSERT() will be just like a VERIFY.
5178 if (recv_delayprops != NULL) {
5179 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
5180 nvlist_free(recv_delayprops);
5182 if (local_delayprops != NULL) {
5183 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
5184 nvlist_free(local_delayprops);
5186 if (inherited_delayprops != NULL) {
5187 ASSERT(nvlist_merge(localprops, inherited_delayprops, 0) == 0);
5188 nvlist_free(inherited_delayprops);
5190 *read_bytes = off - noff;
5192 #ifdef ZFS_DEBUG
5193 if (zfs_ioc_recv_inject_err) {
5194 zfs_ioc_recv_inject_err = B_FALSE;
5195 error = 1;
5197 #endif
5200 * On error, restore the original props.
5202 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
5203 if (clear_received_props(tofs, recvprops, NULL) != 0) {
5205 * We failed to clear the received properties.
5206 * Since we may have left a $recvd value on the
5207 * system, we can't clear the $hasrecvd flag.
5209 *errflags |= ZPROP_ERR_NORESTORE;
5210 } else if (first_recvd_props) {
5211 dsl_prop_unset_hasrecvd(tofs);
5214 if (origrecvd == NULL && !drc.drc_newfs) {
5215 /* We failed to stash the original properties. */
5216 *errflags |= ZPROP_ERR_NORESTORE;
5220 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5221 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5222 * explicitly if we're restoring local properties cleared in the
5223 * first new-style receive.
5225 if (origrecvd != NULL &&
5226 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5227 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5228 origrecvd, NULL) != 0) {
5230 * We stashed the original properties but failed to
5231 * restore them.
5233 *errflags |= ZPROP_ERR_NORESTORE;
5236 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5237 !first_recvd_props) {
5238 nvlist_t *setprops;
5239 nvlist_t *inheritprops;
5240 nvpair_t *nvp;
5242 if (origprops == NULL) {
5243 /* We failed to stash the original properties. */
5244 *errflags |= ZPROP_ERR_NORESTORE;
5245 goto out;
5248 /* Restore original props */
5249 setprops = fnvlist_alloc();
5250 inheritprops = fnvlist_alloc();
5251 nvp = NULL;
5252 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5253 const char *name = nvpair_name(nvp);
5254 const char *source;
5255 nvlist_t *attrs;
5257 if (!nvlist_exists(origprops, name)) {
5259 * Property was not present or was explicitly
5260 * inherited before the receive, restore this.
5262 fnvlist_add_boolean(inheritprops, name);
5263 continue;
5265 attrs = fnvlist_lookup_nvlist(origprops, name);
5266 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5268 /* Skip received properties */
5269 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5270 continue;
5272 if (strcmp(source, tofs) == 0) {
5273 /* Property was locally set */
5274 fnvlist_add_nvlist(setprops, name, attrs);
5275 } else {
5276 /* Property was implicitly inherited */
5277 fnvlist_add_boolean(inheritprops, name);
5281 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5282 NULL) != 0)
5283 *errflags |= ZPROP_ERR_NORESTORE;
5284 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5285 NULL) != 0)
5286 *errflags |= ZPROP_ERR_NORESTORE;
5288 nvlist_free(setprops);
5289 nvlist_free(inheritprops);
5291 out:
5292 zfs_file_put(input_fp);
5293 nvlist_free(origrecvd);
5294 nvlist_free(origprops);
5296 if (error == 0)
5297 error = props_error;
5299 return (error);
5303 * inputs:
5304 * zc_name name of containing filesystem (unused)
5305 * zc_nvlist_src{_size} nvlist of properties to apply
5306 * zc_nvlist_conf{_size} nvlist of properties to exclude
5307 * (DATA_TYPE_BOOLEAN) and override (everything else)
5308 * zc_value name of snapshot to create
5309 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5310 * zc_cookie file descriptor to recv from
5311 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5312 * zc_guid force flag
5314 * outputs:
5315 * zc_cookie number of bytes read
5316 * zc_obj zprop_errflags_t
5317 * zc_nvlist_dst{_size} error for each unapplied received property
5319 static int
5320 zfs_ioc_recv(zfs_cmd_t *zc)
5322 dmu_replay_record_t begin_record;
5323 nvlist_t *errors = NULL;
5324 nvlist_t *recvdprops = NULL;
5325 nvlist_t *localprops = NULL;
5326 const char *origin = NULL;
5327 char *tosnap;
5328 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5329 int error = 0;
5331 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5332 strchr(zc->zc_value, '@') == NULL ||
5333 strchr(zc->zc_value, '%'))
5334 return (SET_ERROR(EINVAL));
5336 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
5337 tosnap = strchr(tofs, '@');
5338 *tosnap++ = '\0';
5340 if (zc->zc_nvlist_src != 0 &&
5341 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5342 zc->zc_iflags, &recvdprops)) != 0)
5343 return (error);
5345 if (zc->zc_nvlist_conf != 0 &&
5346 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5347 zc->zc_iflags, &localprops)) != 0)
5348 return (error);
5350 if (zc->zc_string[0])
5351 origin = zc->zc_string;
5353 begin_record.drr_type = DRR_BEGIN;
5354 begin_record.drr_payloadlen = 0;
5355 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5357 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
5358 NULL, zc->zc_guid, B_FALSE, B_FALSE, zc->zc_cookie, &begin_record,
5359 &zc->zc_cookie, &zc->zc_obj, &errors);
5360 nvlist_free(recvdprops);
5361 nvlist_free(localprops);
5364 * Now that all props, initial and delayed, are set, report the prop
5365 * errors to the caller.
5367 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5368 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5369 put_nvlist(zc, errors) != 0)) {
5371 * Caller made zc->zc_nvlist_dst less than the minimum expected
5372 * size or supplied an invalid address.
5374 error = SET_ERROR(EINVAL);
5377 nvlist_free(errors);
5379 return (error);
5383 * innvl: {
5384 * "snapname" -> full name of the snapshot to create
5385 * (optional) "props" -> received properties to set (nvlist)
5386 * (optional) "localprops" -> override and exclude properties (nvlist)
5387 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5388 * "begin_record" -> non-byteswapped dmu_replay_record_t
5389 * "input_fd" -> file descriptor to read stream from (int32)
5390 * (optional) "force" -> force flag (value ignored)
5391 * (optional) "heal" -> use send stream to heal data corruption
5392 * (optional) "resumable" -> resumable flag (value ignored)
5393 * (optional) "cleanup_fd" -> unused
5394 * (optional) "action_handle" -> unused
5395 * (optional) "hidden_args" -> { "wkeydata" -> value }
5398 * outnvl: {
5399 * "read_bytes" -> number of bytes read
5400 * "error_flags" -> zprop_errflags_t
5401 * "errors" -> error for each unapplied received property (nvlist)
5404 static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5405 {"snapname", DATA_TYPE_STRING, 0},
5406 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5407 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5408 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5409 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5410 {"input_fd", DATA_TYPE_INT32, 0},
5411 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5412 {"heal", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5413 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5414 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5415 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5416 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5419 static int
5420 zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5422 dmu_replay_record_t *begin_record;
5423 uint_t begin_record_size;
5424 nvlist_t *errors = NULL;
5425 nvlist_t *recvprops = NULL;
5426 nvlist_t *localprops = NULL;
5427 nvlist_t *hidden_args = NULL;
5428 const char *snapname;
5429 const char *origin = NULL;
5430 char *tosnap;
5431 char tofs[ZFS_MAX_DATASET_NAME_LEN];
5432 boolean_t force;
5433 boolean_t heal;
5434 boolean_t resumable;
5435 uint64_t read_bytes = 0;
5436 uint64_t errflags = 0;
5437 int input_fd = -1;
5438 int error;
5440 snapname = fnvlist_lookup_string(innvl, "snapname");
5442 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5443 strchr(snapname, '@') == NULL ||
5444 strchr(snapname, '%'))
5445 return (SET_ERROR(EINVAL));
5447 (void) strlcpy(tofs, snapname, sizeof (tofs));
5448 tosnap = strchr(tofs, '@');
5449 *tosnap++ = '\0';
5451 error = nvlist_lookup_string(innvl, "origin", &origin);
5452 if (error && error != ENOENT)
5453 return (error);
5455 error = nvlist_lookup_byte_array(innvl, "begin_record",
5456 (uchar_t **)&begin_record, &begin_record_size);
5457 if (error != 0 || begin_record_size != sizeof (*begin_record))
5458 return (SET_ERROR(EINVAL));
5460 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
5462 force = nvlist_exists(innvl, "force");
5463 heal = nvlist_exists(innvl, "heal");
5464 resumable = nvlist_exists(innvl, "resumable");
5466 /* we still use "props" here for backwards compatibility */
5467 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
5468 if (error && error != ENOENT)
5469 return (error);
5471 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5472 if (error && error != ENOENT)
5473 return (error);
5475 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5476 if (error && error != ENOENT)
5477 return (error);
5479 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
5480 hidden_args, force, heal, resumable, input_fd, begin_record,
5481 &read_bytes, &errflags, &errors);
5483 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5484 fnvlist_add_uint64(outnvl, "error_flags", errflags);
5485 fnvlist_add_nvlist(outnvl, "errors", errors);
5487 nvlist_free(errors);
5488 nvlist_free(recvprops);
5489 nvlist_free(localprops);
5491 return (error);
5494 typedef struct dump_bytes_io {
5495 zfs_file_t *dbi_fp;
5496 caddr_t dbi_buf;
5497 int dbi_len;
5498 int dbi_err;
5499 } dump_bytes_io_t;
5501 static void
5502 dump_bytes_cb(void *arg)
5504 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
5505 zfs_file_t *fp;
5506 caddr_t buf;
5508 fp = dbi->dbi_fp;
5509 buf = dbi->dbi_buf;
5511 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL);
5514 static int
5515 dump_bytes(objset_t *os, void *buf, int len, void *arg)
5517 dump_bytes_io_t dbi;
5519 dbi.dbi_fp = arg;
5520 dbi.dbi_buf = buf;
5521 dbi.dbi_len = len;
5523 #if defined(HAVE_LARGE_STACKS)
5524 dump_bytes_cb(&dbi);
5525 #else
5527 * The vn_rdwr() call is performed in a taskq to ensure that there is
5528 * always enough stack space to write safely to the target filesystem.
5529 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5530 * them and they are used in vdev_file.c for a similar purpose.
5532 spa_taskq_dispatch_sync(dmu_objset_spa(os), ZIO_TYPE_FREE,
5533 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
5534 #endif /* HAVE_LARGE_STACKS */
5536 return (dbi.dbi_err);
5540 * inputs:
5541 * zc_name name of snapshot to send
5542 * zc_cookie file descriptor to send stream to
5543 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5544 * zc_sendobj objsetid of snapshot to send
5545 * zc_fromobj objsetid of incremental fromsnap (may be zero)
5546 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5547 * output size in zc_objset_type.
5548 * zc_flags lzc_send_flags
5550 * outputs:
5551 * zc_objset_type estimated size, if zc_guid is set
5553 * NOTE: This is no longer the preferred interface, any new functionality
5554 * should be added to zfs_ioc_send_new() instead.
5556 static int
5557 zfs_ioc_send(zfs_cmd_t *zc)
5559 int error;
5560 offset_t off;
5561 boolean_t estimate = (zc->zc_guid != 0);
5562 boolean_t embedok = (zc->zc_flags & 0x1);
5563 boolean_t large_block_ok = (zc->zc_flags & 0x2);
5564 boolean_t compressok = (zc->zc_flags & 0x4);
5565 boolean_t rawok = (zc->zc_flags & 0x8);
5566 boolean_t savedok = (zc->zc_flags & 0x10);
5568 if (zc->zc_obj != 0) {
5569 dsl_pool_t *dp;
5570 dsl_dataset_t *tosnap;
5572 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5573 if (error != 0)
5574 return (error);
5576 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5577 if (error != 0) {
5578 dsl_pool_rele(dp, FTAG);
5579 return (error);
5582 if (dsl_dir_is_clone(tosnap->ds_dir))
5583 zc->zc_fromobj =
5584 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
5585 dsl_dataset_rele(tosnap, FTAG);
5586 dsl_pool_rele(dp, FTAG);
5589 if (estimate) {
5590 dsl_pool_t *dp;
5591 dsl_dataset_t *tosnap;
5592 dsl_dataset_t *fromsnap = NULL;
5594 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5595 if (error != 0)
5596 return (error);
5598 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5599 FTAG, &tosnap);
5600 if (error != 0) {
5601 dsl_pool_rele(dp, FTAG);
5602 return (error);
5605 if (zc->zc_fromobj != 0) {
5606 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5607 FTAG, &fromsnap);
5608 if (error != 0) {
5609 dsl_dataset_rele(tosnap, FTAG);
5610 dsl_pool_rele(dp, FTAG);
5611 return (error);
5615 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
5616 compressok || rawok, savedok, &zc->zc_objset_type);
5618 if (fromsnap != NULL)
5619 dsl_dataset_rele(fromsnap, FTAG);
5620 dsl_dataset_rele(tosnap, FTAG);
5621 dsl_pool_rele(dp, FTAG);
5622 } else {
5623 zfs_file_t *fp;
5624 dmu_send_outparams_t out = {0};
5626 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
5627 return (SET_ERROR(EBADF));
5629 off = zfs_file_off(fp);
5630 out.dso_outfunc = dump_bytes;
5631 out.dso_arg = fp;
5632 out.dso_dryrun = B_FALSE;
5633 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5634 zc->zc_fromobj, embedok, large_block_ok, compressok,
5635 rawok, savedok, zc->zc_cookie, &off, &out);
5637 zfs_file_put(fp);
5639 return (error);
5643 * inputs:
5644 * zc_name name of snapshot on which to report progress
5645 * zc_cookie file descriptor of send stream
5647 * outputs:
5648 * zc_cookie number of bytes written in send stream thus far
5649 * zc_objset_type logical size of data traversed by send thus far
5651 static int
5652 zfs_ioc_send_progress(zfs_cmd_t *zc)
5654 dsl_pool_t *dp;
5655 dsl_dataset_t *ds;
5656 dmu_sendstatus_t *dsp = NULL;
5657 int error;
5659 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5660 if (error != 0)
5661 return (error);
5663 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5664 if (error != 0) {
5665 dsl_pool_rele(dp, FTAG);
5666 return (error);
5669 mutex_enter(&ds->ds_sendstream_lock);
5672 * Iterate over all the send streams currently active on this dataset.
5673 * If there's one which matches the specified file descriptor _and_ the
5674 * stream was started by the current process, return the progress of
5675 * that stream.
5678 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5679 dsp = list_next(&ds->ds_sendstreams, dsp)) {
5680 if (dsp->dss_outfd == zc->zc_cookie &&
5681 zfs_proc_is_caller(dsp->dss_proc))
5682 break;
5685 if (dsp != NULL) {
5686 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
5687 0, 0);
5688 /* This is the closest thing we have to atomic_read_64. */
5689 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
5690 } else {
5691 error = SET_ERROR(ENOENT);
5694 mutex_exit(&ds->ds_sendstream_lock);
5695 dsl_dataset_rele(ds, FTAG);
5696 dsl_pool_rele(dp, FTAG);
5697 return (error);
5700 static int
5701 zfs_ioc_inject_fault(zfs_cmd_t *zc)
5703 int id, error;
5705 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5706 &zc->zc_inject_record);
5708 if (error == 0)
5709 zc->zc_guid = (uint64_t)id;
5711 return (error);
5714 static int
5715 zfs_ioc_clear_fault(zfs_cmd_t *zc)
5717 return (zio_clear_fault((int)zc->zc_guid));
5720 static int
5721 zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5723 int id = (int)zc->zc_guid;
5724 int error;
5726 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5727 &zc->zc_inject_record);
5729 zc->zc_guid = id;
5731 return (error);
5734 static int
5735 zfs_ioc_error_log(zfs_cmd_t *zc)
5737 spa_t *spa;
5738 int error;
5740 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5741 return (error);
5743 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5744 &zc->zc_nvlist_dst_size);
5746 spa_close(spa, FTAG);
5748 return (error);
5751 static int
5752 zfs_ioc_clear(zfs_cmd_t *zc)
5754 spa_t *spa;
5755 vdev_t *vd;
5756 int error;
5759 * On zpool clear we also fix up missing slogs
5761 mutex_enter(&spa_namespace_lock);
5762 spa = spa_lookup(zc->zc_name);
5763 if (spa == NULL) {
5764 mutex_exit(&spa_namespace_lock);
5765 return (SET_ERROR(EIO));
5767 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
5768 /* we need to let spa_open/spa_load clear the chains */
5769 spa_set_log_state(spa, SPA_LOG_CLEAR);
5771 spa->spa_last_open_failed = 0;
5772 mutex_exit(&spa_namespace_lock);
5774 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5775 error = spa_open(zc->zc_name, &spa, FTAG);
5776 } else {
5777 nvlist_t *policy;
5778 nvlist_t *config = NULL;
5780 if (zc->zc_nvlist_src == 0)
5781 return (SET_ERROR(EINVAL));
5783 if ((error = get_nvlist(zc->zc_nvlist_src,
5784 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5785 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5786 policy, &config);
5787 if (config != NULL) {
5788 int err;
5790 if ((err = put_nvlist(zc, config)) != 0)
5791 error = err;
5792 nvlist_free(config);
5794 nvlist_free(policy);
5798 if (error != 0)
5799 return (error);
5802 * If multihost is enabled, resuming I/O is unsafe as another
5803 * host may have imported the pool.
5805 if (spa_multihost(spa) && spa_suspended(spa))
5806 return (SET_ERROR(EINVAL));
5808 spa_vdev_state_enter(spa, SCL_NONE);
5810 if (zc->zc_guid == 0) {
5811 vd = NULL;
5812 } else {
5813 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
5814 if (vd == NULL) {
5815 error = SET_ERROR(ENODEV);
5816 (void) spa_vdev_state_exit(spa, NULL, error);
5817 spa_close(spa, FTAG);
5818 return (error);
5822 vdev_clear(spa, vd);
5824 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5825 NULL : spa->spa_root_vdev, 0);
5828 * Resume any suspended I/Os.
5830 if (zio_resume(spa) != 0)
5831 error = SET_ERROR(EIO);
5833 spa_close(spa, FTAG);
5835 return (error);
5839 * Reopen all the vdevs associated with the pool.
5841 * innvl: {
5842 * "scrub_restart" -> when true and scrub is running, allow to restart
5843 * scrub as the side effect of the reopen (boolean).
5846 * outnvl is unused
5848 static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
5849 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
5852 static int
5853 zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
5855 (void) outnvl;
5856 spa_t *spa;
5857 int error;
5858 boolean_t rc, scrub_restart = B_TRUE;
5860 if (innvl) {
5861 error = nvlist_lookup_boolean_value(innvl,
5862 "scrub_restart", &rc);
5863 if (error == 0)
5864 scrub_restart = rc;
5867 error = spa_open(pool, &spa, FTAG);
5868 if (error != 0)
5869 return (error);
5871 spa_vdev_state_enter(spa, SCL_NONE);
5874 * If the scrub_restart flag is B_FALSE and a scrub is already
5875 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5876 * we don't restart the scrub as a side effect of the reopen.
5877 * Otherwise, let vdev_open() decided if a resilver is required.
5880 spa->spa_scrub_reopen = (!scrub_restart &&
5881 dsl_scan_scrubbing(spa->spa_dsl_pool));
5882 vdev_reopen(spa->spa_root_vdev);
5883 spa->spa_scrub_reopen = B_FALSE;
5885 (void) spa_vdev_state_exit(spa, NULL, 0);
5886 spa_close(spa, FTAG);
5887 return (0);
5891 * inputs:
5892 * zc_name name of filesystem
5894 * outputs:
5895 * zc_string name of conflicting snapshot, if there is one
5897 static int
5898 zfs_ioc_promote(zfs_cmd_t *zc)
5900 dsl_pool_t *dp;
5901 dsl_dataset_t *ds, *ods;
5902 char origin[ZFS_MAX_DATASET_NAME_LEN];
5903 char *cp;
5904 int error;
5906 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5907 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5908 strchr(zc->zc_name, '%'))
5909 return (SET_ERROR(EINVAL));
5911 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5912 if (error != 0)
5913 return (error);
5915 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5916 if (error != 0) {
5917 dsl_pool_rele(dp, FTAG);
5918 return (error);
5921 if (!dsl_dir_is_clone(ds->ds_dir)) {
5922 dsl_dataset_rele(ds, FTAG);
5923 dsl_pool_rele(dp, FTAG);
5924 return (SET_ERROR(EINVAL));
5927 error = dsl_dataset_hold_obj(dp,
5928 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5929 if (error != 0) {
5930 dsl_dataset_rele(ds, FTAG);
5931 dsl_pool_rele(dp, FTAG);
5932 return (error);
5935 dsl_dataset_name(ods, origin);
5936 dsl_dataset_rele(ods, FTAG);
5937 dsl_dataset_rele(ds, FTAG);
5938 dsl_pool_rele(dp, FTAG);
5941 * We don't need to unmount *all* the origin fs's snapshots, but
5942 * it's easier.
5944 cp = strchr(origin, '@');
5945 if (cp)
5946 *cp = '\0';
5947 (void) dmu_objset_find(origin,
5948 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5949 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
5953 * Retrieve a single {user|group|project}{used|quota}@... property.
5955 * inputs:
5956 * zc_name name of filesystem
5957 * zc_objset_type zfs_userquota_prop_t
5958 * zc_value domain name (eg. "S-1-234-567-89")
5959 * zc_guid RID/UID/GID
5961 * outputs:
5962 * zc_cookie property value
5964 static int
5965 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5967 zfsvfs_t *zfsvfs;
5968 int error;
5970 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
5971 return (SET_ERROR(EINVAL));
5973 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
5974 if (error != 0)
5975 return (error);
5977 error = zfs_userspace_one(zfsvfs,
5978 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
5979 zfsvfs_rele(zfsvfs, FTAG);
5981 return (error);
5985 * inputs:
5986 * zc_name name of filesystem
5987 * zc_cookie zap cursor
5988 * zc_objset_type zfs_userquota_prop_t
5989 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5991 * outputs:
5992 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5993 * zc_cookie zap cursor
5995 static int
5996 zfs_ioc_userspace_many(zfs_cmd_t *zc)
5998 zfsvfs_t *zfsvfs;
5999 int bufsize = zc->zc_nvlist_dst_size;
6001 if (bufsize <= 0)
6002 return (SET_ERROR(ENOMEM));
6004 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
6005 if (error != 0)
6006 return (error);
6008 void *buf = vmem_alloc(bufsize, KM_SLEEP);
6010 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
6011 buf, &zc->zc_nvlist_dst_size);
6013 if (error == 0) {
6014 error = xcopyout(buf,
6015 (void *)(uintptr_t)zc->zc_nvlist_dst,
6016 zc->zc_nvlist_dst_size);
6018 vmem_free(buf, bufsize);
6019 zfsvfs_rele(zfsvfs, FTAG);
6021 return (error);
6025 * inputs:
6026 * zc_name name of filesystem
6028 * outputs:
6029 * none
6031 static int
6032 zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
6034 int error = 0;
6035 zfsvfs_t *zfsvfs;
6037 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
6038 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
6040 * If userused is not enabled, it may be because the
6041 * objset needs to be closed & reopened (to grow the
6042 * objset_phys_t). Suspend/resume the fs will do that.
6044 dsl_dataset_t *ds, *newds;
6046 ds = dmu_objset_ds(zfsvfs->z_os);
6047 error = zfs_suspend_fs(zfsvfs);
6048 if (error == 0) {
6049 dmu_objset_refresh_ownership(ds, &newds,
6050 B_TRUE, zfsvfs);
6051 error = zfs_resume_fs(zfsvfs, newds);
6054 if (error == 0) {
6055 mutex_enter(&zfsvfs->z_os->os_upgrade_lock);
6056 if (zfsvfs->z_os->os_upgrade_id == 0) {
6057 /* clear potential error code and retry */
6058 zfsvfs->z_os->os_upgrade_status = 0;
6059 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6061 dsl_pool_config_enter(
6062 dmu_objset_pool(zfsvfs->z_os), FTAG);
6063 dmu_objset_userspace_upgrade(zfsvfs->z_os);
6064 dsl_pool_config_exit(
6065 dmu_objset_pool(zfsvfs->z_os), FTAG);
6066 } else {
6067 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6070 taskq_wait_id(zfsvfs->z_os->os_spa->spa_upgrade_taskq,
6071 zfsvfs->z_os->os_upgrade_id);
6072 error = zfsvfs->z_os->os_upgrade_status;
6074 zfs_vfs_rele(zfsvfs);
6075 } else {
6076 objset_t *os;
6078 /* XXX kind of reading contents without owning */
6079 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6080 if (error != 0)
6081 return (error);
6083 mutex_enter(&os->os_upgrade_lock);
6084 if (os->os_upgrade_id == 0) {
6085 /* clear potential error code and retry */
6086 os->os_upgrade_status = 0;
6087 mutex_exit(&os->os_upgrade_lock);
6089 dmu_objset_userspace_upgrade(os);
6090 } else {
6091 mutex_exit(&os->os_upgrade_lock);
6094 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6096 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6097 error = os->os_upgrade_status;
6099 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT,
6100 FTAG);
6102 return (error);
6106 * inputs:
6107 * zc_name name of filesystem
6109 * outputs:
6110 * none
6112 static int
6113 zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
6115 objset_t *os;
6116 int error;
6118 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
6119 if (error != 0)
6120 return (error);
6122 if (dmu_objset_userobjspace_upgradable(os) ||
6123 dmu_objset_projectquota_upgradable(os)) {
6124 mutex_enter(&os->os_upgrade_lock);
6125 if (os->os_upgrade_id == 0) {
6126 /* clear potential error code and retry */
6127 os->os_upgrade_status = 0;
6128 mutex_exit(&os->os_upgrade_lock);
6130 dmu_objset_id_quota_upgrade(os);
6131 } else {
6132 mutex_exit(&os->os_upgrade_lock);
6135 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6137 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6138 error = os->os_upgrade_status;
6139 } else {
6140 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6143 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
6145 return (error);
6148 static int
6149 zfs_ioc_share(zfs_cmd_t *zc)
6151 return (SET_ERROR(ENOSYS));
6155 * inputs:
6156 * zc_name name of containing filesystem
6157 * zc_obj object # beyond which we want next in-use object #
6159 * outputs:
6160 * zc_obj next in-use object #
6162 static int
6163 zfs_ioc_next_obj(zfs_cmd_t *zc)
6165 objset_t *os = NULL;
6166 int error;
6168 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
6169 if (error != 0)
6170 return (error);
6172 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
6174 dmu_objset_rele(os, FTAG);
6175 return (error);
6179 * inputs:
6180 * zc_name name of filesystem
6181 * zc_value prefix name for snapshot
6182 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
6184 * outputs:
6185 * zc_value short name of new snapshot
6187 static int
6188 zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
6190 char *snap_name;
6191 char *hold_name;
6192 minor_t minor;
6194 zfs_file_t *fp = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
6195 if (fp == NULL)
6196 return (SET_ERROR(EBADF));
6198 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
6199 (u_longlong_t)ddi_get_lbolt64());
6200 hold_name = kmem_asprintf("%%%s", zc->zc_value);
6202 int error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
6203 hold_name);
6204 if (error == 0)
6205 (void) strlcpy(zc->zc_value, snap_name,
6206 sizeof (zc->zc_value));
6207 kmem_strfree(snap_name);
6208 kmem_strfree(hold_name);
6209 zfs_onexit_fd_rele(fp);
6210 return (error);
6214 * inputs:
6215 * zc_name name of "to" snapshot
6216 * zc_value name of "from" snapshot
6217 * zc_cookie file descriptor to write diff data on
6219 * outputs:
6220 * dmu_diff_record_t's to the file descriptor
6222 static int
6223 zfs_ioc_diff(zfs_cmd_t *zc)
6225 zfs_file_t *fp;
6226 offset_t off;
6227 int error;
6229 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
6230 return (SET_ERROR(EBADF));
6232 off = zfs_file_off(fp);
6233 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
6235 zfs_file_put(fp);
6237 return (error);
6240 static int
6241 zfs_ioc_smb_acl(zfs_cmd_t *zc)
6243 return (SET_ERROR(ENOTSUP));
6247 * innvl: {
6248 * "holds" -> { snapname -> holdname (string), ... }
6249 * (optional) "cleanup_fd" -> fd (int32)
6252 * outnvl: {
6253 * snapname -> error value (int32)
6254 * ...
6257 static const zfs_ioc_key_t zfs_keys_hold[] = {
6258 {"holds", DATA_TYPE_NVLIST, 0},
6259 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6262 static int
6263 zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
6265 (void) pool;
6266 nvpair_t *pair;
6267 nvlist_t *holds;
6268 int cleanup_fd = -1;
6269 int error;
6270 minor_t minor = 0;
6271 zfs_file_t *fp = NULL;
6273 holds = fnvlist_lookup_nvlist(args, "holds");
6275 /* make sure the user didn't pass us any invalid (empty) tags */
6276 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6277 pair = nvlist_next_nvpair(holds, pair)) {
6278 const char *htag;
6280 error = nvpair_value_string(pair, &htag);
6281 if (error != 0)
6282 return (SET_ERROR(error));
6284 if (strlen(htag) == 0)
6285 return (SET_ERROR(EINVAL));
6288 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
6289 fp = zfs_onexit_fd_hold(cleanup_fd, &minor);
6290 if (fp == NULL)
6291 return (SET_ERROR(EBADF));
6294 error = dsl_dataset_user_hold(holds, minor, errlist);
6295 if (fp != NULL) {
6296 ASSERT3U(minor, !=, 0);
6297 zfs_onexit_fd_rele(fp);
6299 return (SET_ERROR(error));
6303 * innvl is not used.
6305 * outnvl: {
6306 * holdname -> time added (uint64 seconds since epoch)
6307 * ...
6310 static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6311 /* no nvl keys */
6314 static int
6315 zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
6317 (void) args;
6318 return (dsl_dataset_get_holds(snapname, outnvl));
6322 * innvl: {
6323 * snapname -> { holdname, ... }
6324 * ...
6327 * outnvl: {
6328 * snapname -> error value (int32)
6329 * ...
6332 static const zfs_ioc_key_t zfs_keys_release[] = {
6333 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6336 static int
6337 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
6339 (void) pool;
6340 return (dsl_dataset_user_release(holds, errlist));
6344 * inputs:
6345 * zc_guid flags (ZEVENT_NONBLOCK)
6346 * zc_cleanup_fd zevent file descriptor
6348 * outputs:
6349 * zc_nvlist_dst next nvlist event
6350 * zc_cookie dropped events since last get
6352 static int
6353 zfs_ioc_events_next(zfs_cmd_t *zc)
6355 zfs_zevent_t *ze;
6356 nvlist_t *event = NULL;
6357 minor_t minor;
6358 uint64_t dropped = 0;
6359 int error;
6361 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6362 if (fp == NULL)
6363 return (SET_ERROR(EBADF));
6365 do {
6366 error = zfs_zevent_next(ze, &event,
6367 &zc->zc_nvlist_dst_size, &dropped);
6368 if (event != NULL) {
6369 zc->zc_cookie = dropped;
6370 error = put_nvlist(zc, event);
6371 nvlist_free(event);
6374 if (zc->zc_guid & ZEVENT_NONBLOCK)
6375 break;
6377 if ((error == 0) || (error != ENOENT))
6378 break;
6380 error = zfs_zevent_wait(ze);
6381 if (error != 0)
6382 break;
6383 } while (1);
6385 zfs_zevent_fd_rele(fp);
6387 return (error);
6391 * outputs:
6392 * zc_cookie cleared events count
6394 static int
6395 zfs_ioc_events_clear(zfs_cmd_t *zc)
6397 uint_t count;
6399 zfs_zevent_drain_all(&count);
6400 zc->zc_cookie = count;
6402 return (0);
6406 * inputs:
6407 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6408 * zc_cleanup zevent file descriptor
6410 static int
6411 zfs_ioc_events_seek(zfs_cmd_t *zc)
6413 zfs_zevent_t *ze;
6414 minor_t minor;
6415 int error;
6417 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6418 if (fp == NULL)
6419 return (SET_ERROR(EBADF));
6421 error = zfs_zevent_seek(ze, zc->zc_guid);
6422 zfs_zevent_fd_rele(fp);
6424 return (error);
6428 * inputs:
6429 * zc_name name of later filesystem or snapshot
6430 * zc_value full name of old snapshot or bookmark
6432 * outputs:
6433 * zc_cookie space in bytes
6434 * zc_objset_type compressed space in bytes
6435 * zc_perm_action uncompressed space in bytes
6437 static int
6438 zfs_ioc_space_written(zfs_cmd_t *zc)
6440 int error;
6441 dsl_pool_t *dp;
6442 dsl_dataset_t *new;
6444 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
6445 if (error != 0)
6446 return (error);
6447 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6448 if (error != 0) {
6449 dsl_pool_rele(dp, FTAG);
6450 return (error);
6452 if (strchr(zc->zc_value, '#') != NULL) {
6453 zfs_bookmark_phys_t bmp;
6454 error = dsl_bookmark_lookup(dp, zc->zc_value,
6455 new, &bmp);
6456 if (error == 0) {
6457 error = dsl_dataset_space_written_bookmark(&bmp, new,
6458 &zc->zc_cookie,
6459 &zc->zc_objset_type, &zc->zc_perm_action);
6461 } else {
6462 dsl_dataset_t *old;
6463 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
6465 if (error == 0) {
6466 error = dsl_dataset_space_written(old, new,
6467 &zc->zc_cookie,
6468 &zc->zc_objset_type, &zc->zc_perm_action);
6469 dsl_dataset_rele(old, FTAG);
6472 dsl_dataset_rele(new, FTAG);
6473 dsl_pool_rele(dp, FTAG);
6474 return (error);
6478 * innvl: {
6479 * "firstsnap" -> snapshot name
6482 * outnvl: {
6483 * "used" -> space in bytes
6484 * "compressed" -> compressed space in bytes
6485 * "uncompressed" -> uncompressed space in bytes
6488 static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6489 {"firstsnap", DATA_TYPE_STRING, 0},
6492 static int
6493 zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
6495 int error;
6496 dsl_pool_t *dp;
6497 dsl_dataset_t *new, *old;
6498 const char *firstsnap;
6499 uint64_t used, comp, uncomp;
6501 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6503 error = dsl_pool_hold(lastsnap, FTAG, &dp);
6504 if (error != 0)
6505 return (error);
6507 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
6508 if (error == 0 && !new->ds_is_snapshot) {
6509 dsl_dataset_rele(new, FTAG);
6510 error = SET_ERROR(EINVAL);
6512 if (error != 0) {
6513 dsl_pool_rele(dp, FTAG);
6514 return (error);
6516 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
6517 if (error == 0 && !old->ds_is_snapshot) {
6518 dsl_dataset_rele(old, FTAG);
6519 error = SET_ERROR(EINVAL);
6521 if (error != 0) {
6522 dsl_dataset_rele(new, FTAG);
6523 dsl_pool_rele(dp, FTAG);
6524 return (error);
6527 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6528 dsl_dataset_rele(old, FTAG);
6529 dsl_dataset_rele(new, FTAG);
6530 dsl_pool_rele(dp, FTAG);
6531 fnvlist_add_uint64(outnvl, "used", used);
6532 fnvlist_add_uint64(outnvl, "compressed", comp);
6533 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6534 return (error);
6538 * innvl: {
6539 * "fd" -> file descriptor to write stream to (int32)
6540 * (optional) "fromsnap" -> full snap name to send an incremental from
6541 * (optional) "largeblockok" -> (value ignored)
6542 * indicates that blocks > 128KB are permitted
6543 * (optional) "embedok" -> (value ignored)
6544 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6545 * (optional) "compressok" -> (value ignored)
6546 * presence indicates compressed DRR_WRITE records are permitted
6547 * (optional) "rawok" -> (value ignored)
6548 * presence indicates raw encrypted records should be used.
6549 * (optional) "savedok" -> (value ignored)
6550 * presence indicates we should send a partially received snapshot
6551 * (optional) "resume_object" and "resume_offset" -> (uint64)
6552 * if present, resume send stream from specified object and offset.
6553 * (optional) "redactbook" -> (string)
6554 * if present, use this bookmark's redaction list to generate a redacted
6555 * send stream
6558 * outnvl is unused
6560 static const zfs_ioc_key_t zfs_keys_send_new[] = {
6561 {"fd", DATA_TYPE_INT32, 0},
6562 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6563 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6564 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6565 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6566 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6567 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6568 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6569 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6570 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6573 static int
6574 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6576 (void) outnvl;
6577 int error;
6578 offset_t off;
6579 const char *fromname = NULL;
6580 int fd;
6581 zfs_file_t *fp;
6582 boolean_t largeblockok;
6583 boolean_t embedok;
6584 boolean_t compressok;
6585 boolean_t rawok;
6586 boolean_t savedok;
6587 uint64_t resumeobj = 0;
6588 uint64_t resumeoff = 0;
6589 const char *redactbook = NULL;
6591 fd = fnvlist_lookup_int32(innvl, "fd");
6593 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6595 largeblockok = nvlist_exists(innvl, "largeblockok");
6596 embedok = nvlist_exists(innvl, "embedok");
6597 compressok = nvlist_exists(innvl, "compressok");
6598 rawok = nvlist_exists(innvl, "rawok");
6599 savedok = nvlist_exists(innvl, "savedok");
6601 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6602 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6604 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6606 if ((fp = zfs_file_get(fd)) == NULL)
6607 return (SET_ERROR(EBADF));
6609 off = zfs_file_off(fp);
6611 dmu_send_outparams_t out = {0};
6612 out.dso_outfunc = dump_bytes;
6613 out.dso_arg = fp;
6614 out.dso_dryrun = B_FALSE;
6615 error = dmu_send(snapname, fromname, embedok, largeblockok,
6616 compressok, rawok, savedok, resumeobj, resumeoff,
6617 redactbook, fd, &off, &out);
6619 zfs_file_put(fp);
6620 return (error);
6623 static int
6624 send_space_sum(objset_t *os, void *buf, int len, void *arg)
6626 (void) os, (void) buf;
6627 uint64_t *size = arg;
6629 *size += len;
6630 return (0);
6634 * Determine approximately how large a zfs send stream will be -- the number
6635 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6637 * innvl: {
6638 * (optional) "from" -> full snap or bookmark name to send an incremental
6639 * from
6640 * (optional) "largeblockok" -> (value ignored)
6641 * indicates that blocks > 128KB are permitted
6642 * (optional) "embedok" -> (value ignored)
6643 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6644 * (optional) "compressok" -> (value ignored)
6645 * presence indicates compressed DRR_WRITE records are permitted
6646 * (optional) "rawok" -> (value ignored)
6647 * presence indicates raw encrypted records should be used.
6648 * (optional) "resume_object" and "resume_offset" -> (uint64)
6649 * if present, resume send stream from specified object and offset.
6650 * (optional) "fd" -> file descriptor to use as a cookie for progress
6651 * tracking (int32)
6654 * outnvl: {
6655 * "space" -> bytes of space (uint64)
6658 static const zfs_ioc_key_t zfs_keys_send_space[] = {
6659 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6660 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6661 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6662 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6663 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6664 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6665 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6666 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
6667 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6668 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6669 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
6672 static int
6673 zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6675 dsl_pool_t *dp;
6676 dsl_dataset_t *tosnap;
6677 dsl_dataset_t *fromsnap = NULL;
6678 int error;
6679 const char *fromname = NULL;
6680 const char *redactlist_book = NULL;
6681 boolean_t largeblockok;
6682 boolean_t embedok;
6683 boolean_t compressok;
6684 boolean_t rawok;
6685 boolean_t savedok;
6686 uint64_t space = 0;
6687 boolean_t full_estimate = B_FALSE;
6688 uint64_t resumeobj = 0;
6689 uint64_t resumeoff = 0;
6690 uint64_t resume_bytes = 0;
6691 int32_t fd = -1;
6692 zfs_bookmark_phys_t zbm = {0};
6694 error = dsl_pool_hold(snapname, FTAG, &dp);
6695 if (error != 0)
6696 return (error);
6698 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6699 if (error != 0) {
6700 dsl_pool_rele(dp, FTAG);
6701 return (error);
6703 (void) nvlist_lookup_int32(innvl, "fd", &fd);
6705 largeblockok = nvlist_exists(innvl, "largeblockok");
6706 embedok = nvlist_exists(innvl, "embedok");
6707 compressok = nvlist_exists(innvl, "compressok");
6708 rawok = nvlist_exists(innvl, "rawok");
6709 savedok = nvlist_exists(innvl, "savedok");
6710 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
6711 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
6712 &redactlist_book) == 0);
6714 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6715 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6716 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
6718 if (altbook) {
6719 full_estimate = B_TRUE;
6720 } else if (from) {
6721 if (strchr(fromname, '#')) {
6722 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
6725 * dsl_bookmark_lookup() will fail with EXDEV if
6726 * the from-bookmark and tosnap are at the same txg.
6727 * However, it's valid to do a send (and therefore,
6728 * a send estimate) from and to the same time point,
6729 * if the bookmark is redacted (the incremental send
6730 * can change what's redacted on the target). In
6731 * this case, dsl_bookmark_lookup() fills in zbm
6732 * but returns EXDEV. Ignore this error.
6734 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
6735 zbm.zbm_guid ==
6736 dsl_dataset_phys(tosnap)->ds_guid)
6737 error = 0;
6739 if (error != 0) {
6740 dsl_dataset_rele(tosnap, FTAG);
6741 dsl_pool_rele(dp, FTAG);
6742 return (error);
6744 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
6745 ZBM_FLAG_HAS_FBN)) {
6746 full_estimate = B_TRUE;
6748 } else if (strchr(fromname, '@')) {
6749 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
6750 if (error != 0) {
6751 dsl_dataset_rele(tosnap, FTAG);
6752 dsl_pool_rele(dp, FTAG);
6753 return (error);
6756 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
6757 full_estimate = B_TRUE;
6758 dsl_dataset_rele(fromsnap, FTAG);
6760 } else {
6762 * from is not properly formatted as a snapshot or
6763 * bookmark
6765 dsl_dataset_rele(tosnap, FTAG);
6766 dsl_pool_rele(dp, FTAG);
6767 return (SET_ERROR(EINVAL));
6771 if (full_estimate) {
6772 dmu_send_outparams_t out = {0};
6773 offset_t off = 0;
6774 out.dso_outfunc = send_space_sum;
6775 out.dso_arg = &space;
6776 out.dso_dryrun = B_TRUE;
6778 * We have to release these holds so dmu_send can take them. It
6779 * will do all the error checking we need.
6781 dsl_dataset_rele(tosnap, FTAG);
6782 dsl_pool_rele(dp, FTAG);
6783 error = dmu_send(snapname, fromname, embedok, largeblockok,
6784 compressok, rawok, savedok, resumeobj, resumeoff,
6785 redactlist_book, fd, &off, &out);
6786 } else {
6787 error = dmu_send_estimate_fast(tosnap, fromsnap,
6788 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
6789 compressok || rawok, savedok, &space);
6790 space -= resume_bytes;
6791 if (fromsnap != NULL)
6792 dsl_dataset_rele(fromsnap, FTAG);
6793 dsl_dataset_rele(tosnap, FTAG);
6794 dsl_pool_rele(dp, FTAG);
6797 fnvlist_add_uint64(outnvl, "space", space);
6799 return (error);
6803 * Sync the currently open TXG to disk for the specified pool.
6804 * This is somewhat similar to 'zfs_sync()'.
6805 * For cases that do not result in error this ioctl will wait for
6806 * the currently open TXG to commit before returning back to the caller.
6808 * innvl: {
6809 * "force" -> when true, force uberblock update even if there is no dirty data.
6810 * In addition this will cause the vdev configuration to be written
6811 * out including updating the zpool cache file. (boolean_t)
6814 * onvl is unused
6816 static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6817 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6820 static int
6821 zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6823 (void) onvl;
6824 int err;
6825 boolean_t rc, force = B_FALSE;
6826 spa_t *spa;
6828 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6829 return (err);
6831 if (innvl) {
6832 err = nvlist_lookup_boolean_value(innvl, "force", &rc);
6833 if (err == 0)
6834 force = rc;
6837 if (force) {
6838 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6839 vdev_config_dirty(spa->spa_root_vdev);
6840 spa_config_exit(spa, SCL_CONFIG, FTAG);
6842 txg_wait_synced(spa_get_dsl(spa), 0);
6844 spa_close(spa, FTAG);
6846 return (0);
6850 * Load a user's wrapping key into the kernel.
6851 * innvl: {
6852 * "hidden_args" -> { "wkeydata" -> value }
6853 * raw uint8_t array of encryption wrapping key data (32 bytes)
6854 * (optional) "noop" -> (value ignored)
6855 * presence indicated key should only be verified, not loaded
6858 static const zfs_ioc_key_t zfs_keys_load_key[] = {
6859 {"hidden_args", DATA_TYPE_NVLIST, 0},
6860 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6863 static int
6864 zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6866 (void) outnvl;
6867 int ret;
6868 dsl_crypto_params_t *dcp = NULL;
6869 nvlist_t *hidden_args;
6870 boolean_t noop = nvlist_exists(innvl, "noop");
6872 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6873 ret = SET_ERROR(EINVAL);
6874 goto error;
6877 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
6879 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6880 hidden_args, &dcp);
6881 if (ret != 0)
6882 goto error;
6884 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6885 if (ret != 0)
6886 goto error;
6888 dsl_crypto_params_free(dcp, noop);
6890 return (0);
6892 error:
6893 dsl_crypto_params_free(dcp, B_TRUE);
6894 return (ret);
6898 * Unload a user's wrapping key from the kernel.
6899 * Both innvl and outnvl are unused.
6901 static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6902 /* no nvl keys */
6905 static int
6906 zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6908 (void) innvl, (void) outnvl;
6909 int ret = 0;
6911 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6912 ret = (SET_ERROR(EINVAL));
6913 goto out;
6916 ret = spa_keystore_unload_wkey(dsname);
6917 if (ret != 0)
6918 goto out;
6920 out:
6921 return (ret);
6925 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
6926 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
6927 * here to change how the key is derived in userspace.
6929 * innvl: {
6930 * "hidden_args" (optional) -> { "wkeydata" -> value }
6931 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6932 * "props" (optional) -> { prop -> value }
6935 * outnvl is unused
6937 static const zfs_ioc_key_t zfs_keys_change_key[] = {
6938 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6939 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6940 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6943 static int
6944 zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6946 (void) outnvl;
6947 int ret;
6948 uint64_t cmd = DCP_CMD_NONE;
6949 dsl_crypto_params_t *dcp = NULL;
6950 nvlist_t *args = NULL, *hidden_args = NULL;
6952 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6953 ret = (SET_ERROR(EINVAL));
6954 goto error;
6957 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6958 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6959 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6961 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6962 if (ret != 0)
6963 goto error;
6965 ret = spa_keystore_change_key(dsname, dcp);
6966 if (ret != 0)
6967 goto error;
6969 dsl_crypto_params_free(dcp, B_FALSE);
6971 return (0);
6973 error:
6974 dsl_crypto_params_free(dcp, B_TRUE);
6975 return (ret);
6978 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6980 static void
6981 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6982 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6983 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6985 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6987 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6988 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6989 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6990 ASSERT3P(vec->zvec_func, ==, NULL);
6992 vec->zvec_legacy_func = func;
6993 vec->zvec_secpolicy = secpolicy;
6994 vec->zvec_namecheck = namecheck;
6995 vec->zvec_allow_log = log_history;
6996 vec->zvec_pool_check = pool_check;
7000 * See the block comment at the beginning of this file for details on
7001 * each argument to this function.
7003 void
7004 zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
7005 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
7006 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
7007 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
7009 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
7011 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
7012 ASSERT3U(ioc, <, ZFS_IOC_LAST);
7013 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
7014 ASSERT3P(vec->zvec_func, ==, NULL);
7016 /* if we are logging, the name must be valid */
7017 ASSERT(!allow_log || namecheck != NO_NAME);
7019 vec->zvec_name = name;
7020 vec->zvec_func = func;
7021 vec->zvec_secpolicy = secpolicy;
7022 vec->zvec_namecheck = namecheck;
7023 vec->zvec_pool_check = pool_check;
7024 vec->zvec_smush_outnvlist = smush_outnvlist;
7025 vec->zvec_allow_log = allow_log;
7026 vec->zvec_nvl_keys = nvl_keys;
7027 vec->zvec_nvl_key_count = num_keys;
7030 static void
7031 zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7032 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
7033 zfs_ioc_poolcheck_t pool_check)
7035 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7036 POOL_NAME, log_history, pool_check);
7039 void
7040 zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7041 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
7043 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7044 DATASET_NAME, B_FALSE, pool_check);
7047 static void
7048 zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7050 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
7051 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7054 static void
7055 zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7056 zfs_secpolicy_func_t *secpolicy)
7058 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7059 NO_NAME, B_FALSE, POOL_CHECK_NONE);
7062 static void
7063 zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
7064 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
7066 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7067 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
7070 static void
7071 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7073 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
7074 zfs_secpolicy_read);
7077 static void
7078 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7079 zfs_secpolicy_func_t *secpolicy)
7081 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7082 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7085 static void
7086 zfs_ioctl_init(void)
7088 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
7089 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
7090 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7091 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
7093 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
7094 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
7095 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7096 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
7098 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
7099 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
7100 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7101 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
7103 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
7104 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
7105 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7106 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
7108 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
7109 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
7110 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7111 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
7113 zfs_ioctl_register("create", ZFS_IOC_CREATE,
7114 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
7115 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7116 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
7118 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
7119 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
7120 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7121 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
7123 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
7124 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
7125 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7126 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
7128 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
7129 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
7130 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7131 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
7133 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
7134 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
7135 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7136 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
7137 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
7138 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
7139 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7140 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
7142 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
7143 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
7144 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7145 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
7147 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
7148 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
7149 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7150 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
7152 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
7153 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
7154 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7155 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
7157 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
7158 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
7159 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7160 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
7162 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
7163 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
7164 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
7165 ARRAY_SIZE(zfs_keys_get_bookmark_props));
7167 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
7168 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
7169 POOL_NAME,
7170 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7171 zfs_keys_destroy_bookmarks,
7172 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
7174 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
7175 zfs_ioc_recv_new, zfs_secpolicy_recv, DATASET_NAME,
7176 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7177 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
7178 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
7179 zfs_ioc_load_key, zfs_secpolicy_load_key,
7180 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7181 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
7182 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
7183 zfs_ioc_unload_key, zfs_secpolicy_load_key,
7184 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7185 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
7186 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
7187 zfs_ioc_change_key, zfs_secpolicy_change_key,
7188 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
7189 B_TRUE, B_TRUE, zfs_keys_change_key,
7190 ARRAY_SIZE(zfs_keys_change_key));
7192 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
7193 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
7194 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7195 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
7196 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
7197 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
7198 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
7200 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
7201 zfs_ioc_channel_program, zfs_secpolicy_config,
7202 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
7203 B_TRUE, zfs_keys_channel_program,
7204 ARRAY_SIZE(zfs_keys_channel_program));
7206 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
7207 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
7208 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7209 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
7211 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
7212 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
7213 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7214 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
7216 zfs_ioctl_register("zpool_discard_checkpoint",
7217 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
7218 zfs_secpolicy_config, POOL_NAME,
7219 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7220 zfs_keys_pool_discard_checkpoint,
7221 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
7223 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
7224 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
7225 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7226 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
7228 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
7229 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
7230 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7231 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
7233 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
7234 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
7235 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7236 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
7238 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS,
7239 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME,
7240 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7241 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait));
7243 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
7244 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
7245 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7246 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
7248 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
7249 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
7250 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
7251 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
7253 zfs_ioctl_register("zpool_vdev_get_props", ZFS_IOC_VDEV_GET_PROPS,
7254 zfs_ioc_vdev_get_props, zfs_secpolicy_read, POOL_NAME,
7255 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_vdev_get_props,
7256 ARRAY_SIZE(zfs_keys_vdev_get_props));
7258 zfs_ioctl_register("zpool_vdev_set_props", ZFS_IOC_VDEV_SET_PROPS,
7259 zfs_ioc_vdev_set_props, zfs_secpolicy_config, POOL_NAME,
7260 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7261 zfs_keys_vdev_set_props, ARRAY_SIZE(zfs_keys_vdev_set_props));
7263 zfs_ioctl_register("scrub", ZFS_IOC_POOL_SCRUB,
7264 zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME,
7265 POOL_CHECK_NONE, B_TRUE, B_TRUE,
7266 zfs_keys_pool_scrub, ARRAY_SIZE(zfs_keys_pool_scrub));
7268 /* IOCTLS that use the legacy function signature */
7270 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7271 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7273 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7274 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7275 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7276 zfs_ioc_pool_scan);
7277 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7278 zfs_ioc_pool_upgrade);
7279 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7280 zfs_ioc_vdev_add);
7281 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7282 zfs_ioc_vdev_remove);
7283 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7284 zfs_ioc_vdev_set_state);
7285 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7286 zfs_ioc_vdev_attach);
7287 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7288 zfs_ioc_vdev_detach);
7289 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7290 zfs_ioc_vdev_setpath);
7291 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7292 zfs_ioc_vdev_setfru);
7293 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7294 zfs_ioc_pool_set_props);
7295 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7296 zfs_ioc_vdev_split);
7297 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7298 zfs_ioc_pool_reguid);
7300 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7301 zfs_ioc_pool_configs, zfs_secpolicy_none);
7302 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7303 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7304 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7305 zfs_ioc_inject_fault, zfs_secpolicy_inject);
7306 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7307 zfs_ioc_clear_fault, zfs_secpolicy_inject);
7308 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
7309 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
7312 * pool destroy, and export don't log the history as part of
7313 * zfsdev_ioctl, but rather zfs_ioc_pool_export
7314 * does the logging of those commands.
7316 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
7317 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7318 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
7319 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7321 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7322 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7323 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
7324 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7326 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7327 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7328 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7329 zfs_ioc_dsobj_to_dsname,
7330 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7331 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7332 zfs_ioc_pool_get_history,
7333 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7335 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7336 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7338 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
7339 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
7341 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7342 zfs_ioc_space_written);
7343 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7344 zfs_ioc_objset_recvd_props);
7345 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7346 zfs_ioc_next_obj);
7347 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7348 zfs_ioc_get_fsacl);
7349 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7350 zfs_ioc_objset_stats);
7351 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7352 zfs_ioc_objset_zplprops);
7353 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7354 zfs_ioc_dataset_list_next);
7355 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7356 zfs_ioc_snapshot_list_next);
7357 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7358 zfs_ioc_send_progress);
7360 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7361 zfs_ioc_diff, zfs_secpolicy_diff);
7362 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7363 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7364 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7365 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7366 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7367 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7368 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7369 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7370 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7371 zfs_ioc_send, zfs_secpolicy_send);
7373 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7374 zfs_secpolicy_none);
7375 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7376 zfs_secpolicy_destroy);
7377 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7378 zfs_secpolicy_rename);
7379 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7380 zfs_secpolicy_recv);
7381 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7382 zfs_secpolicy_promote);
7383 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7384 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7385 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7386 zfs_secpolicy_set_fsacl);
7388 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7389 zfs_secpolicy_share, POOL_CHECK_NONE);
7390 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7391 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7392 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7393 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7394 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7395 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7396 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7397 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7399 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7400 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7401 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7402 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7403 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7404 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7406 zfs_ioctl_init_os();
7410 * Verify that for non-legacy ioctls the input nvlist
7411 * pairs match against the expected input.
7413 * Possible errors are:
7414 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7415 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7416 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7418 static int
7419 zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7421 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7422 boolean_t required_keys_found = B_FALSE;
7425 * examine each input pair
7427 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7428 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7429 const char *name = nvpair_name(pair);
7430 data_type_t type = nvpair_type(pair);
7431 boolean_t identified = B_FALSE;
7434 * check pair against the documented names and type
7436 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7437 /* if not a wild card name, check for an exact match */
7438 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7439 strcmp(nvl_keys[k].zkey_name, name) != 0)
7440 continue;
7442 identified = B_TRUE;
7444 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7445 nvl_keys[k].zkey_type != type) {
7446 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7449 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7450 continue;
7452 required_keys_found = B_TRUE;
7453 break;
7456 /* allow an 'optional' key, everything else is invalid */
7457 if (!identified &&
7458 (strcmp(name, "optional") != 0 ||
7459 type != DATA_TYPE_NVLIST)) {
7460 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7464 /* verify that all required keys were found */
7465 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7466 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7467 continue;
7469 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
7470 /* at least one non-optional key is expected here */
7471 if (!required_keys_found)
7472 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7473 continue;
7476 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7477 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7480 return (0);
7483 static int
7484 pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7485 zfs_ioc_poolcheck_t check)
7487 spa_t *spa;
7488 int error;
7490 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7491 type == ENTITY_NAME);
7493 if (check & POOL_CHECK_NONE)
7494 return (0);
7496 error = spa_open(name, &spa, FTAG);
7497 if (error == 0) {
7498 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
7499 error = SET_ERROR(EAGAIN);
7500 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
7501 error = SET_ERROR(EROFS);
7502 spa_close(spa, FTAG);
7504 return (error);
7508 zfsdev_getminor(zfs_file_t *fp, minor_t *minorp)
7510 zfsdev_state_t *zs, *fpd;
7512 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7514 fpd = zfs_file_private(fp);
7515 if (fpd == NULL)
7516 return (SET_ERROR(EBADF));
7518 mutex_enter(&zfsdev_state_lock);
7520 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7522 if (zs->zs_minor == -1)
7523 continue;
7525 if (fpd == zs) {
7526 *minorp = fpd->zs_minor;
7527 mutex_exit(&zfsdev_state_lock);
7528 return (0);
7532 mutex_exit(&zfsdev_state_lock);
7534 return (SET_ERROR(EBADF));
7537 void *
7538 zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
7540 zfsdev_state_t *zs;
7542 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7543 if (zs->zs_minor == minor) {
7544 membar_consumer();
7545 switch (which) {
7546 case ZST_ONEXIT:
7547 return (zs->zs_onexit);
7548 case ZST_ZEVENT:
7549 return (zs->zs_zevent);
7550 case ZST_ALL:
7551 return (zs);
7556 return (NULL);
7560 * Find a free minor number. The zfsdev_state_list is expected to
7561 * be short since it is only a list of currently open file handles.
7563 static minor_t
7564 zfsdev_minor_alloc(void)
7566 static minor_t last_minor = 0;
7567 minor_t m;
7569 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7571 for (m = last_minor + 1; m != last_minor; m++) {
7572 if (m > ZFSDEV_MAX_MINOR)
7573 m = 1;
7574 if (zfsdev_get_state(m, ZST_ALL) == NULL) {
7575 last_minor = m;
7576 return (m);
7580 return (0);
7584 zfsdev_state_init(void *priv)
7586 zfsdev_state_t *zs, *zsprev = NULL;
7587 minor_t minor;
7588 boolean_t newzs = B_FALSE;
7590 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7592 minor = zfsdev_minor_alloc();
7593 if (minor == 0)
7594 return (SET_ERROR(ENXIO));
7596 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zs->zs_next) {
7597 if (zs->zs_minor == -1)
7598 break;
7599 zsprev = zs;
7602 if (!zs) {
7603 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7604 newzs = B_TRUE;
7607 zfsdev_private_set_state(priv, zs);
7609 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7610 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7613 * In order to provide for lock-free concurrent read access
7614 * to the minor list in zfsdev_get_state(), new entries
7615 * must be completely written before linking them into the
7616 * list whereas existing entries are already linked; the last
7617 * operation must be updating zs_minor (from -1 to the new
7618 * value).
7620 if (newzs) {
7621 zs->zs_minor = minor;
7622 membar_producer();
7623 zsprev->zs_next = zs;
7624 } else {
7625 membar_producer();
7626 zs->zs_minor = minor;
7629 return (0);
7632 void
7633 zfsdev_state_destroy(void *priv)
7635 zfsdev_state_t *zs = zfsdev_private_get_state(priv);
7637 ASSERT(zs != NULL);
7638 ASSERT3S(zs->zs_minor, >, 0);
7641 * The last reference to this zfsdev file descriptor is being dropped.
7642 * We don't have to worry about lookup grabbing this state object, and
7643 * zfsdev_state_init() will not try to reuse this object until it is
7644 * invalidated by setting zs_minor to -1. Invalidation must be done
7645 * last, with a memory barrier to ensure ordering. This lets us avoid
7646 * taking the global zfsdev state lock around destruction.
7648 zfs_onexit_destroy(zs->zs_onexit);
7649 zfs_zevent_destroy(zs->zs_zevent);
7650 zs->zs_onexit = NULL;
7651 zs->zs_zevent = NULL;
7652 membar_producer();
7653 zs->zs_minor = -1;
7656 long
7657 zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
7659 int error, cmd;
7660 const zfs_ioc_vec_t *vec;
7661 char *saved_poolname = NULL;
7662 uint64_t max_nvlist_src_size;
7663 size_t saved_poolname_len = 0;
7664 nvlist_t *innvl = NULL;
7665 fstrans_cookie_t cookie;
7666 hrtime_t start_time = gethrtime();
7668 cmd = vecnum;
7669 error = 0;
7670 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
7671 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7673 vec = &zfs_ioc_vec[vecnum];
7676 * The registered ioctl list may be sparse, verify that either
7677 * a normal or legacy handler are registered.
7679 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
7680 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7682 zc->zc_iflags = flag & FKIOCTL;
7683 max_nvlist_src_size = zfs_max_nvlist_src_size_os();
7684 if (zc->zc_nvlist_src_size > max_nvlist_src_size) {
7686 * Make sure the user doesn't pass in an insane value for
7687 * zc_nvlist_src_size. We have to check, since we will end
7688 * up allocating that much memory inside of get_nvlist(). This
7689 * prevents a nefarious user from allocating tons of kernel
7690 * memory.
7692 * Also, we return EINVAL instead of ENOMEM here. The reason
7693 * being that returning ENOMEM from an ioctl() has a special
7694 * connotation; that the user's size value is too small and
7695 * needs to be expanded to hold the nvlist. See
7696 * zcmd_expand_dst_nvlist() for details.
7698 error = SET_ERROR(EINVAL); /* User's size too big */
7700 } else if (zc->zc_nvlist_src_size != 0) {
7701 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7702 zc->zc_iflags, &innvl);
7703 if (error != 0)
7704 goto out;
7708 * Ensure that all pool/dataset names are valid before we pass down to
7709 * the lower layers.
7711 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7712 switch (vec->zvec_namecheck) {
7713 case POOL_NAME:
7714 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
7715 error = SET_ERROR(EINVAL);
7716 else
7717 error = pool_status_check(zc->zc_name,
7718 vec->zvec_namecheck, vec->zvec_pool_check);
7719 break;
7721 case DATASET_NAME:
7722 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
7723 error = SET_ERROR(EINVAL);
7724 else
7725 error = pool_status_check(zc->zc_name,
7726 vec->zvec_namecheck, vec->zvec_pool_check);
7727 break;
7729 case ENTITY_NAME:
7730 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7731 error = SET_ERROR(EINVAL);
7732 } else {
7733 error = pool_status_check(zc->zc_name,
7734 vec->zvec_namecheck, vec->zvec_pool_check);
7736 break;
7738 case NO_NAME:
7739 break;
7742 * Ensure that all input pairs are valid before we pass them down
7743 * to the lower layers.
7745 * The vectored functions can use fnvlist_lookup_{type} for any
7746 * required pairs since zfs_check_input_nvpairs() confirmed that
7747 * they exist and are of the correct type.
7749 if (error == 0 && vec->zvec_func != NULL) {
7750 error = zfs_check_input_nvpairs(innvl, vec);
7751 if (error != 0)
7752 goto out;
7755 if (error == 0) {
7756 cookie = spl_fstrans_mark();
7757 error = vec->zvec_secpolicy(zc, innvl, CRED());
7758 spl_fstrans_unmark(cookie);
7761 if (error != 0)
7762 goto out;
7764 /* legacy ioctls can modify zc_name */
7766 * Can't use kmem_strdup() as we might truncate the string and
7767 * kmem_strfree() would then free with incorrect size.
7769 saved_poolname_len = strlen(zc->zc_name) + 1;
7770 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP);
7772 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len);
7773 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
7775 if (vec->zvec_func != NULL) {
7776 nvlist_t *outnvl;
7777 int puterror = 0;
7778 spa_t *spa;
7779 nvlist_t *lognv = NULL;
7781 ASSERT(vec->zvec_legacy_func == NULL);
7784 * Add the innvl to the lognv before calling the func,
7785 * in case the func changes the innvl.
7787 if (vec->zvec_allow_log) {
7788 lognv = fnvlist_alloc();
7789 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7790 vec->zvec_name);
7791 if (!nvlist_empty(innvl)) {
7792 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7793 innvl);
7797 outnvl = fnvlist_alloc();
7798 cookie = spl_fstrans_mark();
7799 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
7800 spl_fstrans_unmark(cookie);
7803 * Some commands can partially execute, modify state, and still
7804 * return an error. In these cases, attempt to record what
7805 * was modified.
7807 if ((error == 0 ||
7808 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7809 vec->zvec_allow_log &&
7810 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7811 if (!nvlist_empty(outnvl)) {
7812 size_t out_size = fnvlist_size(outnvl);
7813 if (out_size > zfs_history_output_max) {
7814 fnvlist_add_int64(lognv,
7815 ZPOOL_HIST_OUTPUT_SIZE, out_size);
7816 } else {
7817 fnvlist_add_nvlist(lognv,
7818 ZPOOL_HIST_OUTPUT_NVL, outnvl);
7821 if (error != 0) {
7822 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7823 error);
7825 fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS,
7826 gethrtime() - start_time);
7827 (void) spa_history_log_nvl(spa, lognv);
7828 spa_close(spa, FTAG);
7830 fnvlist_free(lognv);
7832 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7833 int smusherror = 0;
7834 if (vec->zvec_smush_outnvlist) {
7835 smusherror = nvlist_smush(outnvl,
7836 zc->zc_nvlist_dst_size);
7838 if (smusherror == 0)
7839 puterror = put_nvlist(zc, outnvl);
7842 if (puterror != 0)
7843 error = puterror;
7845 nvlist_free(outnvl);
7846 } else {
7847 cookie = spl_fstrans_mark();
7848 error = vec->zvec_legacy_func(zc);
7849 spl_fstrans_unmark(cookie);
7852 out:
7853 nvlist_free(innvl);
7854 if (error == 0 && vec->zvec_allow_log) {
7855 char *s = tsd_get(zfs_allow_log_key);
7856 if (s != NULL)
7857 kmem_strfree(s);
7858 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname));
7860 if (saved_poolname != NULL)
7861 kmem_free(saved_poolname, saved_poolname_len);
7863 return (error);
7867 zfs_kmod_init(void)
7869 int error;
7871 if ((error = zvol_init()) != 0)
7872 return (error);
7874 spa_init(SPA_MODE_READ | SPA_MODE_WRITE);
7875 zfs_init();
7877 zfs_ioctl_init();
7879 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
7880 zfsdev_state_listhead.zs_minor = -1;
7882 if ((error = zfsdev_attach()) != 0)
7883 goto out;
7885 tsd_create(&zfs_fsyncer_key, NULL);
7886 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7887 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7889 return (0);
7890 out:
7891 zfs_fini();
7892 spa_fini();
7893 zvol_fini();
7895 return (error);
7898 void
7899 zfs_kmod_fini(void)
7901 zfsdev_state_t *zs, *zsnext = NULL;
7903 zfsdev_detach();
7905 mutex_destroy(&zfsdev_state_lock);
7907 for (zs = &zfsdev_state_listhead; zs != NULL; zs = zsnext) {
7908 zsnext = zs->zs_next;
7909 if (zs->zs_onexit)
7910 zfs_onexit_destroy(zs->zs_onexit);
7911 if (zs->zs_zevent)
7912 zfs_zevent_destroy(zs->zs_zevent);
7913 if (zs != &zfsdev_state_listhead)
7914 kmem_free(zs, sizeof (zfsdev_state_t));
7917 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */
7918 zfs_fini();
7919 spa_fini();
7920 zvol_fini();
7922 tsd_destroy(&zfs_fsyncer_key);
7923 tsd_destroy(&rrw_tsd_key);
7924 tsd_destroy(&zfs_allow_log_key);
7927 ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, U64, ZMOD_RW,
7928 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls");
7930 ZFS_MODULE_PARAM(zfs, zfs_, history_output_max, U64, ZMOD_RW,
7931 "Maximum size in bytes of ZFS ioctl output that will be logged");