]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_ioctl.c
Cleanup: Switch to strlcpy from strncpy
[mirror_zfs.git] / module / zfs / zfs_ioctl.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
9ae529ec 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
b129c659 24 * Portions Copyright 2011 Martin Matuska
671c9354 25 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
0cee2406 26 * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net>
005e27e3 27 * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
1b87e0f5 28 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
788eb90c 29 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
196bee4c 30 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
9759c60f 31 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
95fd54a1 32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
e550644f
BB
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2016 Toomas Soome <tsoome@me.com>
a0bd735a 35 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
d8d418ff 36 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
d12f91fd 37 * Copyright 2017 RackTop Systems.
d3f2cd7e 38 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
4c0883fb 39 * Copyright (c) 2019 Datto Inc.
a73f361f 40 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
2a673e76 41 * Copyright (c) 2019, 2021, Klara Inc.
10b3c7f5 42 * Copyright (c) 2019, Allan Jude
6f1ffb06
MA
43 */
44
45/*
46 * ZFS ioctls.
47 *
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.
50 *
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().
54 *
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().
58 *
59 * The registration arguments are as follows:
60 *
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".
66 *
67 * zfs_ioc_t ioc
68 * The ioctl request number, which userland will pass to ioctl(2).
b83a0e2d
DB
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.
6f1ffb06
MA
72 *
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".
80 *
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).
88 *
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).
96 *
b83a0e2d
DB
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.
100 *
6f1ffb06
MA
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.
109 *
110 * zfs_ioc_func_t *func
111 * The callback function that will perform the operation.
112 *
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:
117 *
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).
122 *
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.
128 *
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.
136 *
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.
142 *
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.
b83a0e2d
DB
149 *
150 * IOCTL Interface Errors
151 *
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
3541dc6d 157 */
34dc7c2f 158
34dc7c2f
BB
159#include <sys/types.h>
160#include <sys/param.h>
161#include <sys/errno.h>
c0801bf3 162#include <sys/uio_impl.h>
34dc7c2f
BB
163#include <sys/file.h>
164#include <sys/kmem.h>
34dc7c2f
BB
165#include <sys/cmn_err.h>
166#include <sys/stat.h>
167#include <sys/zfs_ioctl.h>
957c7aa2 168#include <sys/zfs_quota.h>
428870ff 169#include <sys/zfs_vfsops.h>
34dc7c2f
BB
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>
4a283c7f 175#include <sys/vdev_impl.h>
34dc7c2f
BB
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>
37abac6d 182#include <sys/dmu_impl.h>
30af21b0 183#include <sys/dmu_redact.h>
13fe0198 184#include <sys/dmu_tx.h>
34dc7c2f 185#include <sys/sunddi.h>
34dc7c2f
BB
186#include <sys/policy.h>
187#include <sys/zone.h>
188#include <sys/nvpair.h>
189#include <sys/pathname.h>
34dc7c2f 190#include <sys/fs/zfs.h>
ebe7e575 191#include <sys/zfs_ctldir.h>
34dc7c2f 192#include <sys/zfs_dir.h>
572e2857 193#include <sys/zfs_onexit.h>
34dc7c2f 194#include <sys/zvol.h>
428870ff 195#include <sys/dsl_scan.h>
325f0235 196#include <sys/fm/util.h>
b5256303 197#include <sys/dsl_crypt.h>
7bb0c294 198#include <sys/rrwlock.h>
da92d5cb 199#include <sys/zfs_file.h>
325f0235 200
03916905 201#include <sys/dmu_recv.h>
13fe0198 202#include <sys/dmu_send.h>
30af21b0 203#include <sys/dmu_recv.h>
13fe0198 204#include <sys/dsl_destroy.h>
da536844 205#include <sys/dsl_bookmark.h>
13fe0198 206#include <sys/dsl_userhold.h>
9759c60f 207#include <sys/zfeature.h>
d99a0153 208#include <sys/zcp.h>
3c67d83a 209#include <sys/zio_checksum.h>
a1d477c2 210#include <sys/vdev_removal.h>
619f0976
GW
211#include <sys/vdev_impl.h>
212#include <sys/vdev_initialize.h>
1b939560 213#include <sys/vdev_trim.h>
9759c60f 214
34dc7c2f
BB
215#include "zfs_namecheck.h"
216#include "zfs_prop.h"
217#include "zfs_deleg.h"
428870ff 218#include "zfs_comutil.h"
34dc7c2f 219
d99a0153
CW
220#include <sys/lua/lua.h>
221#include <sys/lua/lauxlib.h>
7bb0c294 222#include <sys/zfs_ioctl_impl.h>
f74b821a 223
325f0235 224kmutex_t zfsdev_state_lock;
18168da7 225static zfsdev_state_t *zfsdev_state_list;
34dc7c2f 226
b83a0e2d 227/*
7bb0c294
MM
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.
009cc8e8 230 * Defaults to 0=auto which is handled by platform code.
b83a0e2d 231 */
009cc8e8 232unsigned long zfs_max_nvlist_src_size = 0;
7bb0c294 233
d66aab7c
MA
234/*
235 * When logging the output nvlist of an ioctl in the on-disk history, limit
bf169e9f 236 * the logged size to this many bytes. This must be less than DMU_MAX_ACCESS.
d66aab7c
MA
237 * This applies primarily to zfs_ioc_channel_program().
238 */
18168da7 239static unsigned long zfs_history_output_max = 1024 * 1024;
d66aab7c 240
7bb0c294
MM
241uint_t zfs_fsyncer_key;
242uint_t zfs_allow_log_key;
b83a0e2d
DB
243
244/* DATA_TYPE_ANY is used when zkey_type can vary. */
245#define DATA_TYPE_ANY DATA_TYPE_UNKNOWN
246
34dc7c2f 247typedef struct zfs_ioc_vec {
6f1ffb06 248 zfs_ioc_legacy_func_t *zvec_legacy_func;
34dc7c2f
BB
249 zfs_ioc_func_t *zvec_func;
250 zfs_secpolicy_func_t *zvec_secpolicy;
9babb374 251 zfs_ioc_namecheck_t zvec_namecheck;
6f1ffb06 252 boolean_t zvec_allow_log;
572e2857 253 zfs_ioc_poolcheck_t zvec_pool_check;
6f1ffb06
MA
254 boolean_t zvec_smush_outnvlist;
255 const char *zvec_name;
b83a0e2d
DB
256 const zfs_ioc_key_t *zvec_nvl_keys;
257 size_t zvec_nvl_key_count;
34dc7c2f
BB
258} zfs_ioc_vec_t;
259
9babb374
BB
260/* This array is indexed by zfs_userquota_prop_t */
261static const char *userquota_perms[] = {
262 ZFS_DELEG_PERM_USERUSED,
263 ZFS_DELEG_PERM_USERQUOTA,
264 ZFS_DELEG_PERM_GROUPUSED,
265 ZFS_DELEG_PERM_GROUPQUOTA,
1de321e6
JX
266 ZFS_DELEG_PERM_USEROBJUSED,
267 ZFS_DELEG_PERM_USEROBJQUOTA,
268 ZFS_DELEG_PERM_GROUPOBJUSED,
269 ZFS_DELEG_PERM_GROUPOBJQUOTA,
9c5167d1
NF
270 ZFS_DELEG_PERM_PROJECTUSED,
271 ZFS_DELEG_PERM_PROJECTQUOTA,
272 ZFS_DELEG_PERM_PROJECTOBJUSED,
273 ZFS_DELEG_PERM_PROJECTOBJQUOTA,
9babb374
BB
274};
275
276static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
9c5167d1 277static int zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc);
428870ff
BB
278static int zfs_check_settable(const char *name, nvpair_t *property,
279 cred_t *cr);
4d55ea81 280static int zfs_check_clearable(const char *dataset, nvlist_t *props,
428870ff 281 nvlist_t **errors);
b128c09f
BB
282static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
283 boolean_t *);
6f1ffb06
MA
284int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
285static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
b128c09f 286
34dc7c2f
BB
287static void
288history_str_free(char *buf)
289{
290 kmem_free(buf, HIS_MAX_RECORD_LEN);
291}
292
293static char *
294history_str_get(zfs_cmd_t *zc)
295{
296 char *buf;
297
b8864a23 298 if (zc->zc_history == 0)
34dc7c2f
BB
299 return (NULL);
300
efcd79a8 301 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP);
34dc7c2f
BB
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);
306 }
307
308 buf[HIS_MAX_RECORD_LEN -1] = '\0';
309
310 return (buf);
311}
312
b128c09f 313/*
d3cc8b15 314 * Return non-zero if the spa version is less than requested version.
34dc7c2f
BB
315 */
316static int
b128c09f 317zfs_earlier_version(const char *name, int version)
34dc7c2f 318{
34dc7c2f
BB
319 spa_t *spa;
320
321 if (spa_open(name, &spa, FTAG) == 0) {
322 if (spa_version(spa) < version) {
323 spa_close(spa, FTAG);
324 return (1);
325 }
326 spa_close(spa, FTAG);
327 }
328 return (0);
329}
330
331/*
b128c09f 332 * Return TRUE if the ZPL version is less than requested version.
34dc7c2f 333 */
b128c09f
BB
334static boolean_t
335zpl_earlier_version(const char *name, int version)
34dc7c2f
BB
336{
337 objset_t *os;
b128c09f 338 boolean_t rc = B_TRUE;
34dc7c2f 339
428870ff 340 if (dmu_objset_hold(name, FTAG, &os) == 0) {
b128c09f 341 uint64_t zplversion;
34dc7c2f 342
428870ff
BB
343 if (dmu_objset_type(os) != DMU_OST_ZFS) {
344 dmu_objset_rele(os, FTAG);
345 return (B_TRUE);
346 }
347 /* XXX reading from non-owned objset */
b128c09f
BB
348 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0)
349 rc = zplversion < version;
428870ff 350 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
351 }
352 return (rc);
353}
354
355static void
356zfs_log_history(zfs_cmd_t *zc)
357{
358 spa_t *spa;
359 char *buf;
360
361 if ((buf = history_str_get(zc)) == NULL)
362 return;
363
364 if (spa_open(zc->zc_name, &spa, FTAG) == 0) {
365 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY)
6f1ffb06 366 (void) spa_history_log(spa, buf);
34dc7c2f
BB
367 spa_close(spa, FTAG);
368 }
369 history_str_free(buf);
370}
371
372/*
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.
375 */
34dc7c2f 376static int
6f1ffb06 377zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 378{
ef70eff1 379 (void) zc, (void) innvl, (void) cr;
34dc7c2f
BB
380 return (0);
381}
382
383/*
384 * Policy for dataset read operations (list children, get statistics). Requires
385 * no privileges, but must be visible in the local zone.
386 */
34dc7c2f 387static int
6f1ffb06 388zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 389{
ef70eff1 390 (void) innvl, (void) cr;
34dc7c2f
BB
391 if (INGLOBALZONE(curproc) ||
392 zone_dataset_visible(zc->zc_name, NULL))
393 return (0);
394
2e528b49 395 return (SET_ERROR(ENOENT));
34dc7c2f
BB
396}
397
398static int
572e2857 399zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr)
34dc7c2f 400{
34dc7c2f
BB
401 int writable = 1;
402
403 /*
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.
406 */
407 if (!INGLOBALZONE(curproc) &&
408 !zone_dataset_visible(dataset, &writable))
2e528b49 409 return (SET_ERROR(ENOENT));
34dc7c2f 410
34dc7c2f
BB
411 if (INGLOBALZONE(curproc)) {
412 /*
413 * If the fs is zoned, only root can access it from the
414 * global zone.
415 */
416 if (secpolicy_zfs(cr) && zoned)
2e528b49 417 return (SET_ERROR(EPERM));
34dc7c2f
BB
418 } else {
419 /*
420 * If we are in a local zone, the 'zoned' property must be set.
421 */
422 if (!zoned)
2e528b49 423 return (SET_ERROR(EPERM));
34dc7c2f
BB
424
425 /* must be writable by this zone */
426 if (!writable)
2e528b49 427 return (SET_ERROR(EPERM));
34dc7c2f
BB
428 }
429 return (0);
430}
431
572e2857
BB
432static int
433zfs_dozonecheck(const char *dataset, cred_t *cr)
434{
435 uint64_t zoned;
436
7bb0c294
MM
437 if (dsl_prop_get_integer(dataset, zfs_prop_to_name(ZFS_PROP_ZONED),
438 &zoned, NULL))
2e528b49 439 return (SET_ERROR(ENOENT));
572e2857
BB
440
441 return (zfs_dozonecheck_impl(dataset, zoned, cr));
442}
443
444static int
445zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr)
446{
447 uint64_t zoned;
448
7bb0c294 449 if (dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_ZONED), &zoned))
2e528b49 450 return (SET_ERROR(ENOENT));
572e2857
BB
451
452 return (zfs_dozonecheck_impl(dataset, zoned, cr));
453}
454
6f1ffb06 455static int
13fe0198
MA
456zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds,
457 const char *perm, cred_t *cr)
34dc7c2f
BB
458{
459 int error;
460
330d06f9 461 error = zfs_dozonecheck_ds(name, ds, cr);
34dc7c2f
BB
462 if (error == 0) {
463 error = secpolicy_zfs(cr);
13fe0198 464 if (error != 0)
6f1ffb06 465 error = dsl_deleg_access_impl(ds, perm, cr);
34dc7c2f
BB
466 }
467 return (error);
468}
469
6f1ffb06 470static int
13fe0198 471zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr)
572e2857
BB
472{
473 int error;
13fe0198
MA
474 dsl_dataset_t *ds;
475 dsl_pool_t *dp;
572e2857 476
e88551d5
GM
477 /*
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.
481 */
482 if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0)
483 return (0);
484
13fe0198
MA
485 error = dsl_pool_hold(name, FTAG, &dp);
486 if (error != 0)
487 return (error);
488
489 error = dsl_dataset_hold(dp, name, FTAG, &ds);
490 if (error != 0) {
491 dsl_pool_rele(dp, FTAG);
492 return (error);
572e2857 493 }
13fe0198
MA
494
495 error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr);
496
497 dsl_dataset_rele(ds, FTAG);
498 dsl_pool_rele(dp, FTAG);
572e2857
BB
499 return (error);
500}
501
428870ff
BB
502/*
503 * Policy for setting the security label property.
504 *
505 * Returns 0 for success, non-zero for access and other errors.
506 */
34dc7c2f 507static int
4d55ea81 508zfs_set_slabel_policy(const char *name, const char *strval, cred_t *cr)
34dc7c2f 509{
d2c15e84 510#ifdef HAVE_MLSLABEL
428870ff
BB
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;
517
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);
13fe0198 521 if (error != 0)
2e528b49 522 return (SET_ERROR(EPERM));
428870ff
BB
523
524 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
525 new_default = TRUE;
526
527 /* The label must be translatable */
528 if (!new_default && (hexstr_to_label(strval, &new_sl) != 0))
2e528b49 529 return (SET_ERROR(EINVAL));
428870ff
BB
530
531 /*
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.
535 */
536 if (!INGLOBALZONE(curproc)) {
537 if (new_default || !blequal(&new_sl, CR_SL(CRED())))
2e528b49 538 return (SET_ERROR(EPERM));
428870ff
BB
539 return (0);
540 }
541
542 /*
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.
546 */
547 if (dsl_prop_get_integer(name,
548 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
2e528b49 549 return (SET_ERROR(EPERM));
428870ff
BB
550 if (!zoned) {
551 if (zfs_check_global_label(name, strval) != 0)
2e528b49 552 return (SET_ERROR(EPERM));
428870ff
BB
553 }
554
555 /*
556 * If the existing dataset label is nondefault, check if the
557 * dataset is mounted (label cannot be changed while mounted).
0037b49e 558 * Get the zfsvfs_t; if there isn't one, then the dataset isn't
428870ff
BB
559 * mounted (or isn't a dataset, doesn't exist, ...).
560 */
561 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) {
562 objset_t *os;
4d55ea81 563 static const char *setsl_tag = "setsl_tag";
428870ff
BB
564
565 /*
566 * Try to own the dataset; abort if there is any error,
567 * (e.g., already mounted, in use, or other error).
568 */
b5256303 569 error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE,
428870ff 570 setsl_tag, &os);
13fe0198 571 if (error != 0)
2e528b49 572 return (SET_ERROR(EPERM));
428870ff 573
b5256303 574 dmu_objset_disown(os, B_TRUE, setsl_tag);
428870ff
BB
575
576 if (new_default) {
577 needed_priv = PRIV_FILE_DOWNGRADE_SL;
578 goto out_check;
579 }
580
581 if (hexstr_to_label(strval, &new_sl) != 0)
2e528b49 582 return (SET_ERROR(EPERM));
428870ff
BB
583
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;
592 }
593
594out_check:
595 if (needed_priv != -1)
596 return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL));
597 return (0);
d2c15e84 598#else
ecb2b7dc 599 return (SET_ERROR(ENOTSUP));
d2c15e84 600#endif /* HAVE_MLSLABEL */
428870ff
BB
601}
602
603static int
604zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval,
605 cred_t *cr)
606{
607 char *strval;
608
34dc7c2f
BB
609 /*
610 * Check permissions for special properties.
611 */
612 switch (prop) {
e75c13c3
BB
613 default:
614 break;
34dc7c2f
BB
615 case ZFS_PROP_ZONED:
616 /*
617 * Disallow setting of 'zoned' from within a local zone.
618 */
619 if (!INGLOBALZONE(curproc))
2e528b49 620 return (SET_ERROR(EPERM));
34dc7c2f
BB
621 break;
622
623 case ZFS_PROP_QUOTA:
788eb90c
JJ
624 case ZFS_PROP_FILESYSTEM_LIMIT:
625 case ZFS_PROP_SNAPSHOT_LIMIT:
34dc7c2f
BB
626 if (!INGLOBALZONE(curproc)) {
627 uint64_t zoned;
eca7b760 628 char setpoint[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
629 /*
630 * Unprivileged users are allowed to modify the
788eb90c 631 * limit on things *under* (ie. contained by)
34dc7c2f
BB
632 * the thing they own.
633 */
7bb0c294
MM
634 if (dsl_prop_get_integer(dsname,
635 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, setpoint))
2e528b49 636 return (SET_ERROR(EPERM));
428870ff 637 if (!zoned || strlen(dsname) <= strlen(setpoint))
2e528b49 638 return (SET_ERROR(EPERM));
34dc7c2f
BB
639 }
640 break;
428870ff
BB
641
642 case ZFS_PROP_MLSLABEL:
643 if (!is_system_labeled())
2e528b49 644 return (SET_ERROR(EPERM));
428870ff
BB
645
646 if (nvpair_value_string(propval, &strval) == 0) {
647 int err;
648
649 err = zfs_set_slabel_policy(dsname, strval, CRED());
650 if (err != 0)
651 return (err);
652 }
653 break;
34dc7c2f
BB
654 }
655
428870ff 656 return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr));
34dc7c2f
BB
657}
658
6f1ffb06
MA
659static int
660zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 661{
34dc7c2f
BB
662 /*
663 * permission to set permissions will be evaluated later in
664 * dsl_deleg_can_allow()
665 */
ef70eff1
AZ
666 (void) innvl;
667 return (zfs_dozonecheck(zc->zc_name, cr));
34dc7c2f
BB
668}
669
6f1ffb06
MA
670static int
671zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 672{
ef70eff1 673 (void) innvl;
428870ff
BB
674 return (zfs_secpolicy_write_perms(zc->zc_name,
675 ZFS_DELEG_PERM_ROLLBACK, cr));
34dc7c2f
BB
676}
677
6f1ffb06
MA
678static int
679zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 680{
ef70eff1 681 (void) innvl;
572e2857
BB
682 dsl_pool_t *dp;
683 dsl_dataset_t *ds;
4d55ea81 684 const char *cp;
572e2857
BB
685 int error;
686
687 /*
688 * Generate the current snapshot name from the given objsetid, then
689 * use that name for the secpolicy/zone checks.
690 */
691 cp = strchr(zc->zc_name, '@');
692 if (cp == NULL)
2e528b49 693 return (SET_ERROR(EINVAL));
13fe0198
MA
694 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
695 if (error != 0)
572e2857
BB
696 return (error);
697
572e2857 698 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds);
13fe0198
MA
699 if (error != 0) {
700 dsl_pool_rele(dp, FTAG);
572e2857 701 return (error);
13fe0198 702 }
572e2857
BB
703
704 dsl_dataset_name(ds, zc->zc_name);
705
706 error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds,
707 ZFS_DELEG_PERM_SEND, cr);
708 dsl_dataset_rele(ds, FTAG);
13fe0198 709 dsl_pool_rele(dp, FTAG);
572e2857
BB
710
711 return (error);
34dc7c2f
BB
712}
713
6f1ffb06
MA
714static int
715zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
716{
ef70eff1 717 (void) innvl;
6f1ffb06
MA
718 return (zfs_secpolicy_write_perms(zc->zc_name,
719 ZFS_DELEG_PERM_SEND, cr));
720}
721
65c7cc49 722static int
6f1ffb06 723zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 724{
ef70eff1 725 (void) zc, (void) innvl, (void) cr;
2e528b49 726 return (SET_ERROR(ENOTSUP));
9babb374 727}
34dc7c2f 728
65c7cc49 729static int
6f1ffb06 730zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 731{
ef70eff1 732 (void) zc, (void) innvl, (void) cr;
2e528b49 733 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
734}
735
736static int
737zfs_get_parent(const char *datasetname, char *parent, int parentsize)
738{
739 char *cp;
740
741 /*
742 * Remove the @bla or /bla from the end of the name to get the parent.
743 */
7584fbe8 744 (void) strlcpy(parent, datasetname, parentsize);
34dc7c2f
BB
745 cp = strrchr(parent, '@');
746 if (cp != NULL) {
747 cp[0] = '\0';
748 } else {
749 cp = strrchr(parent, '/');
750 if (cp == NULL)
2e528b49 751 return (SET_ERROR(ENOENT));
34dc7c2f
BB
752 cp[0] = '\0';
753 }
754
755 return (0);
756}
757
758int
759zfs_secpolicy_destroy_perms(const char *name, cred_t *cr)
760{
761 int error;
762
763 if ((error = zfs_secpolicy_write_perms(name,
764 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
765 return (error);
766
767 return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr));
768}
769
770static int
6f1ffb06 771zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 772{
ef70eff1 773 (void) innvl;
34dc7c2f
BB
774 return (zfs_secpolicy_destroy_perms(zc->zc_name, cr));
775}
776
777/*
428870ff 778 * Destroying snapshots with delegated permissions requires
6f1ffb06 779 * descendant mount and destroy permissions.
34dc7c2f 780 */
34dc7c2f 781static int
6f1ffb06 782zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 783{
ef70eff1 784 (void) zc;
6f1ffb06
MA
785 nvlist_t *snaps;
786 nvpair_t *pair, *nextpair;
787 int error = 0;
428870ff 788
b83a0e2d
DB
789 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
790
6f1ffb06
MA
791 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
792 pair = nextpair) {
6f1ffb06 793 nextpair = nvlist_next_nvpair(snaps, pair);
da536844
MA
794 error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr);
795 if (error == ENOENT) {
6f1ffb06
MA
796 /*
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).
803 */
804 fnvlist_remove_nvpair(snaps, pair);
805 error = 0;
6f1ffb06 806 }
6f1ffb06
MA
807 if (error != 0)
808 break;
809 }
428870ff 810
428870ff 811 return (error);
34dc7c2f
BB
812}
813
814int
815zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr)
816{
eca7b760 817 char parentname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
818 int error;
819
820 if ((error = zfs_secpolicy_write_perms(from,
821 ZFS_DELEG_PERM_RENAME, cr)) != 0)
822 return (error);
823
824 if ((error = zfs_secpolicy_write_perms(from,
825 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
826 return (error);
827
828 if ((error = zfs_get_parent(to, parentname,
829 sizeof (parentname))) != 0)
830 return (error);
831
832 if ((error = zfs_secpolicy_write_perms(parentname,
833 ZFS_DELEG_PERM_CREATE, cr)) != 0)
834 return (error);
835
836 if ((error = zfs_secpolicy_write_perms(parentname,
837 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
838 return (error);
839
840 return (error);
841}
842
843static int
6f1ffb06 844zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 845{
ef70eff1 846 (void) innvl;
34dc7c2f
BB
847 return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr));
848}
849
850static int
6f1ffb06 851zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 852{
ef70eff1 853 (void) innvl;
13fe0198
MA
854 dsl_pool_t *dp;
855 dsl_dataset_t *clone;
34dc7c2f
BB
856 int error;
857
858 error = zfs_secpolicy_write_perms(zc->zc_name,
859 ZFS_DELEG_PERM_PROMOTE, cr);
13fe0198
MA
860 if (error != 0)
861 return (error);
862
863 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
864 if (error != 0)
34dc7c2f
BB
865 return (error);
866
13fe0198 867 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone);
34dc7c2f
BB
868
869 if (error == 0) {
eca7b760 870 char parentname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198 871 dsl_dataset_t *origin = NULL;
34dc7c2f 872 dsl_dir_t *dd;
13fe0198 873 dd = clone->ds_dir;
34dc7c2f 874
b128c09f 875 error = dsl_dataset_hold_obj(dd->dd_pool,
d683ddbb 876 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin);
13fe0198
MA
877 if (error != 0) {
878 dsl_dataset_rele(clone, FTAG);
879 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
880 return (error);
881 }
882
13fe0198 883 error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone,
34dc7c2f
BB
884 ZFS_DELEG_PERM_MOUNT, cr);
885
13fe0198
MA
886 dsl_dataset_name(origin, parentname);
887 if (error == 0) {
888 error = zfs_secpolicy_write_perms_ds(parentname, origin,
34dc7c2f 889 ZFS_DELEG_PERM_PROMOTE, cr);
13fe0198
MA
890 }
891 dsl_dataset_rele(clone, FTAG);
892 dsl_dataset_rele(origin, FTAG);
34dc7c2f 893 }
13fe0198 894 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
895 return (error);
896}
897
898static int
6f1ffb06 899zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 900{
ef70eff1 901 (void) innvl;
34dc7c2f
BB
902 int error;
903
904 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
905 ZFS_DELEG_PERM_RECEIVE, cr)) != 0)
906 return (error);
907
908 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
909 ZFS_DELEG_PERM_MOUNT, cr)) != 0)
910 return (error);
911
912 return (zfs_secpolicy_write_perms(zc->zc_name,
913 ZFS_DELEG_PERM_CREATE, cr));
914}
915
916int
917zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr)
918{
428870ff
BB
919 return (zfs_secpolicy_write_perms(name,
920 ZFS_DELEG_PERM_SNAPSHOT, cr));
34dc7c2f
BB
921}
922
6f1ffb06
MA
923/*
924 * Check for permission to create each snapshot in the nvlist.
925 */
34dc7c2f 926static int
6f1ffb06 927zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 928{
ef70eff1 929 (void) zc;
6f1ffb06
MA
930 nvlist_t *snaps;
931 int error = 0;
932 nvpair_t *pair;
933
b83a0e2d
DB
934 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
935
6f1ffb06
MA
936 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
937 pair = nvlist_next_nvpair(snaps, pair)) {
938 char *name = nvpair_name(pair);
939 char *atp = strchr(name, '@');
34dc7c2f 940
6f1ffb06 941 if (atp == NULL) {
2e528b49 942 error = SET_ERROR(EINVAL);
6f1ffb06
MA
943 break;
944 }
945 *atp = '\0';
946 error = zfs_secpolicy_snapshot_perms(name, cr);
947 *atp = '@';
948 if (error != 0)
949 break;
950 }
951 return (error);
952}
953
da536844 954/*
b83a0e2d 955 * Check for permission to create each bookmark in the nvlist.
da536844 956 */
da536844
MA
957static int
958zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
959{
ef70eff1 960 (void) zc;
da536844 961 int error = 0;
da536844 962
1c27024e 963 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
da536844
MA
964 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
965 char *name = nvpair_name(pair);
966 char *hashp = strchr(name, '#');
967
968 if (hashp == NULL) {
969 error = SET_ERROR(EINVAL);
970 break;
971 }
972 *hashp = '\0';
973 error = zfs_secpolicy_write_perms(name,
974 ZFS_DELEG_PERM_BOOKMARK, cr);
975 *hashp = '#';
976 if (error != 0)
977 break;
978 }
979 return (error);
980}
981
da536844
MA
982static int
983zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
984{
ef70eff1 985 (void) zc;
da536844
MA
986 nvpair_t *pair, *nextpair;
987 int error = 0;
988
989 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
990 pair = nextpair) {
991 char *name = nvpair_name(pair);
992 char *hashp = strchr(name, '#');
993 nextpair = nvlist_next_nvpair(innvl, pair);
994
995 if (hashp == NULL) {
996 error = SET_ERROR(EINVAL);
997 break;
998 }
999
1000 *hashp = '\0';
1001 error = zfs_secpolicy_write_perms(name,
1002 ZFS_DELEG_PERM_DESTROY, cr);
1003 *hashp = '#';
1004 if (error == ENOENT) {
1005 /*
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).
1012 */
1013 fnvlist_remove_nvpair(innvl, pair);
1014 error = 0;
1015 }
1016 if (error != 0)
1017 break;
1018 }
1019
1020 return (error);
1021}
1022
6f1ffb06
MA
1023static int
1024zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1025{
ef70eff1 1026 (void) zc, (void) innvl, (void) cr;
6f1ffb06
MA
1027 /*
1028 * Even root must have a proper TSD so that we know what pool
1029 * to log to.
1030 */
1031 if (tsd_get(zfs_allow_log_key) == NULL)
2e528b49 1032 return (SET_ERROR(EPERM));
6f1ffb06 1033 return (0);
34dc7c2f
BB
1034}
1035
1036static int
6f1ffb06 1037zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 1038{
eca7b760 1039 char parentname[ZFS_MAX_DATASET_NAME_LEN];
428870ff 1040 int error;
6f1ffb06 1041 char *origin;
34dc7c2f
BB
1042
1043 if ((error = zfs_get_parent(zc->zc_name, parentname,
1044 sizeof (parentname))) != 0)
1045 return (error);
1046
6f1ffb06
MA
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);
34dc7c2f
BB
1051
1052 if ((error = zfs_secpolicy_write_perms(parentname,
1053 ZFS_DELEG_PERM_CREATE, cr)) != 0)
1054 return (error);
1055
6f1ffb06
MA
1056 return (zfs_secpolicy_write_perms(parentname,
1057 ZFS_DELEG_PERM_MOUNT, cr));
34dc7c2f
BB
1058}
1059
34dc7c2f
BB
1060/*
1061 * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires
1062 * SYS_CONFIG privilege, which is not available in a local zone.
1063 */
7bb0c294 1064int
6f1ffb06 1065zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 1066{
ef70eff1
AZ
1067 (void) zc, (void) innvl;
1068
34dc7c2f 1069 if (secpolicy_sys_config(cr, B_FALSE) != 0)
2e528b49 1070 return (SET_ERROR(EPERM));
34dc7c2f
BB
1071
1072 return (0);
1073}
1074
572e2857
BB
1075/*
1076 * Policy for object to name lookups.
1077 */
572e2857 1078static int
6f1ffb06 1079zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
572e2857 1080{
ef70eff1 1081 (void) innvl;
572e2857
BB
1082 int error;
1083
1084 if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1085 return (0);
1086
1087 error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
1088 return (error);
1089}
1090
34dc7c2f
BB
1091/*
1092 * Policy for fault injection. Requires all privileges.
1093 */
34dc7c2f 1094static int
6f1ffb06 1095zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 1096{
ef70eff1 1097 (void) zc, (void) innvl;
34dc7c2f
BB
1098 return (secpolicy_zinject(cr));
1099}
1100
1101static int
6f1ffb06 1102zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
34dc7c2f 1103{
ef70eff1 1104 (void) innvl;
34dc7c2f
BB
1105 zfs_prop_t prop = zfs_name_to_prop(zc->zc_value);
1106
4ff7a8fa 1107 if (prop == ZPROP_USERPROP) {
34dc7c2f 1108 if (!zfs_prop_user(zc->zc_value))
2e528b49 1109 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1110 return (zfs_secpolicy_write_perms(zc->zc_name,
1111 ZFS_DELEG_PERM_USERPROP, cr));
1112 } else {
428870ff
BB
1113 return (zfs_secpolicy_setprop(zc->zc_name, prop,
1114 NULL, cr));
34dc7c2f
BB
1115 }
1116}
1117
9babb374 1118static int
6f1ffb06 1119zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 1120{
6f1ffb06 1121 int err = zfs_secpolicy_read(zc, innvl, cr);
9babb374
BB
1122 if (err)
1123 return (err);
1124
1125 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 1126 return (SET_ERROR(EINVAL));
9babb374
BB
1127
1128 if (zc->zc_value[0] == 0) {
1129 /*
1130 * They are asking about a posix uid/gid. If it's
1131 * themself, allow it.
1132 */
1133 if (zc->zc_objset_type == ZFS_PROP_USERUSED ||
1de321e6
JX
1134 zc->zc_objset_type == ZFS_PROP_USERQUOTA ||
1135 zc->zc_objset_type == ZFS_PROP_USEROBJUSED ||
1136 zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) {
9babb374
BB
1137 if (zc->zc_guid == crgetuid(cr))
1138 return (0);
9c5167d1
NF
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) {
9babb374
BB
1143 if (groupmember(zc->zc_guid, cr))
1144 return (0);
1145 }
9c5167d1 1146 /* else is for project quota/used */
9babb374
BB
1147 }
1148
1149 return (zfs_secpolicy_write_perms(zc->zc_name,
1150 userquota_perms[zc->zc_objset_type], cr));
1151}
1152
1153static int
6f1ffb06 1154zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 1155{
6f1ffb06 1156 int err = zfs_secpolicy_read(zc, innvl, cr);
9babb374
BB
1157 if (err)
1158 return (err);
1159
1160 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 1161 return (SET_ERROR(EINVAL));
9babb374
BB
1162
1163 return (zfs_secpolicy_write_perms(zc->zc_name,
1164 userquota_perms[zc->zc_objset_type], cr));
1165}
1166
1167static int
6f1ffb06 1168zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
9babb374 1169{
ef70eff1 1170 (void) innvl;
428870ff
BB
1171 return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION,
1172 NULL, cr));
9babb374
BB
1173}
1174
45d1cae3 1175static int
6f1ffb06 1176zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
45d1cae3 1177{
ef70eff1 1178 (void) zc;
13fe0198
MA
1179 nvpair_t *pair;
1180 nvlist_t *holds;
1181 int error;
1182
b83a0e2d 1183 holds = fnvlist_lookup_nvlist(innvl, "holds");
13fe0198
MA
1184
1185 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
1186 pair = nvlist_next_nvpair(holds, pair)) {
eca7b760 1187 char fsname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
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);
1195 }
1196 return (0);
45d1cae3
BB
1197}
1198
1199static int
6f1ffb06 1200zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
45d1cae3 1201{
ef70eff1 1202 (void) zc;
13fe0198
MA
1203 nvpair_t *pair;
1204 int error;
1205
1206 for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1207 pair = nvlist_next_nvpair(innvl, pair)) {
eca7b760 1208 char fsname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
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);
1216 }
1217 return (0);
45d1cae3
BB
1218}
1219
572e2857
BB
1220/*
1221 * Policy for allowing temporary snapshots to be taken or released
1222 */
1223static int
6f1ffb06 1224zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
572e2857
BB
1225{
1226 /*
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.
1230 */
1231 int error;
1232
1233 if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1234 ZFS_DELEG_PERM_DIFF, cr)) == 0)
1235 return (0);
1236
6f1ffb06 1237 error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
b83a0e2d
DB
1238
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);
1246 }
572e2857
BB
1247 return (error);
1248}
1249
b5256303
TC
1250static int
1251zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1252{
1253 return (zfs_secpolicy_write_perms(zc->zc_name,
1254 ZFS_DELEG_PERM_LOAD_KEY, cr));
1255}
1256
1257static int
1258zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1259{
1260 return (zfs_secpolicy_write_perms(zc->zc_name,
1261 ZFS_DELEG_PERM_CHANGE_KEY, cr));
1262}
1263
34dc7c2f
BB
1264/*
1265 * Returns the nvlist as specified by the user in the zfs_cmd_t.
1266 */
1267static int
9babb374 1268get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
34dc7c2f
BB
1269{
1270 char *packed;
1271 int error;
1272 nvlist_t *list = NULL;
1273
1274 /*
1275 * Read in and unpack the user-supplied nvlist.
1276 */
1277 if (size == 0)
2e528b49 1278 return (SET_ERROR(EINVAL));
34dc7c2f 1279
77aef6f6 1280 packed = vmem_alloc(size, KM_SLEEP);
34dc7c2f 1281
9babb374
BB
1282 if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1283 iflag)) != 0) {
77aef6f6 1284 vmem_free(packed, size);
0de7c552 1285 return (SET_ERROR(EFAULT));
34dc7c2f
BB
1286 }
1287
1288 if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) {
77aef6f6 1289 vmem_free(packed, size);
34dc7c2f
BB
1290 return (error);
1291 }
1292
77aef6f6 1293 vmem_free(packed, size);
34dc7c2f
BB
1294
1295 *nvp = list;
1296 return (0);
1297}
1298
6f1ffb06
MA
1299/*
1300 * Reduce the size of this nvlist until it can be serialized in 'max' bytes.
1301 * Entries will be removed from the end of the nvlist, and one int32 entry
1302 * named "N_MORE_ERRORS" will be added indicating how many entries were
1303 * removed.
1304 */
428870ff 1305static int
6f1ffb06 1306nvlist_smush(nvlist_t *errors, size_t max)
428870ff
BB
1307{
1308 size_t size;
1309
6f1ffb06 1310 size = fnvlist_size(errors);
428870ff 1311
6f1ffb06 1312 if (size > max) {
428870ff
BB
1313 nvpair_t *more_errors;
1314 int n = 0;
1315
6f1ffb06 1316 if (max < 1024)
2e528b49 1317 return (SET_ERROR(ENOMEM));
428870ff 1318
6f1ffb06
MA
1319 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1320 more_errors = nvlist_prev_nvpair(errors, NULL);
428870ff
BB
1321
1322 do {
6f1ffb06 1323 nvpair_t *pair = nvlist_prev_nvpair(errors,
428870ff 1324 more_errors);
6f1ffb06 1325 fnvlist_remove_nvpair(errors, pair);
428870ff 1326 n++;
6f1ffb06
MA
1327 size = fnvlist_size(errors);
1328 } while (size > max);
428870ff 1329
6f1ffb06
MA
1330 fnvlist_remove_nvpair(errors, more_errors);
1331 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1332 ASSERT3U(fnvlist_size(errors), <=, max);
428870ff
BB
1333 }
1334
1335 return (0);
1336}
1337
34dc7c2f
BB
1338static int
1339put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1340{
1341 char *packed = NULL;
428870ff 1342 int error = 0;
34dc7c2f 1343 size_t size;
34dc7c2f 1344
6f1ffb06 1345 size = fnvlist_size(nvl);
34dc7c2f
BB
1346
1347 if (size > zc->zc_nvlist_dst_size) {
2e528b49 1348 error = SET_ERROR(ENOMEM);
34dc7c2f 1349 } else {
6f1ffb06 1350 packed = fnvlist_pack(nvl, &size);
428870ff
BB
1351 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1352 size, zc->zc_iflags) != 0)
2e528b49 1353 error = SET_ERROR(EFAULT);
6f1ffb06 1354 fnvlist_pack_free(packed, size);
34dc7c2f
BB
1355 }
1356
1357 zc->zc_nvlist_dst_size = size;
6f1ffb06 1358 zc->zc_nvlist_dst_filled = B_TRUE;
34dc7c2f
BB
1359 return (error);
1360}
1361
d99a0153
CW
1362int
1363getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
9babb374 1364{
d99a0153 1365 int error = 0;
428870ff 1366 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2e528b49 1367 return (SET_ERROR(EINVAL));
428870ff 1368 }
9babb374 1369
428870ff 1370 mutex_enter(&os->os_user_ptr_lock);
0037b49e 1371 *zfvp = dmu_objset_get_user(os);
061460df 1372 /* bump s_active only when non-zero to prevent umount race */
7bb0c294 1373 error = zfs_vfs_ref(zfvp);
428870ff 1374 mutex_exit(&os->os_user_ptr_lock);
d99a0153
CW
1375 return (error);
1376}
1377
8d103d88 1378int
d99a0153
CW
1379getzfsvfs(const char *dsname, zfsvfs_t **zfvp)
1380{
1381 objset_t *os;
1382 int error;
1383
1384 error = dmu_objset_hold(dsname, FTAG, &os);
1385 if (error != 0)
1386 return (error);
1387
1388 error = getzfsvfs_impl(os, zfvp);
428870ff 1389 dmu_objset_rele(os, FTAG);
9babb374
BB
1390 return (error);
1391}
1392
1393/*
0037b49e 1394 * Find a zfsvfs_t for a mounted filesystem, or create our own, in which
2cf7f52b 1395 * case its z_sb will be NULL, and it will be opened as the owner.
9ae529ec
CS
1396 * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER,
1397 * which prevents all inode ops from running.
9babb374
BB
1398 */
1399static int
dd66857d
AZ
1400zfsvfs_hold(const char *name, const void *tag, zfsvfs_t **zfvp,
1401 boolean_t writer)
9babb374
BB
1402{
1403 int error = 0;
9babb374 1404
f298b24d 1405 if (getzfsvfs(name, zfvp) != 0)
163a8c28 1406 error = zfsvfs_create(name, B_FALSE, zfvp);
9babb374 1407 if (error == 0) {
5ebe425a
MG
1408 if (writer)
1409 ZFS_TEARDOWN_ENTER_WRITE(*zfvp, tag);
1410 else
1411 ZFS_TEARDOWN_ENTER_READ(*zfvp, tag);
0037b49e 1412 if ((*zfvp)->z_unmounted) {
9babb374
BB
1413 /*
1414 * XXX we could probably try again, since the unmounting
1415 * thread should be just about to disassociate the
0037b49e 1416 * objset from the zfsvfs.
9babb374 1417 */
5ebe425a 1418 ZFS_TEARDOWN_EXIT(*zfvp, tag);
2e528b49 1419 return (SET_ERROR(EBUSY));
9babb374
BB
1420 }
1421 }
1422 return (error);
1423}
1424
1425static void
dd66857d 1426zfsvfs_rele(zfsvfs_t *zfsvfs, const void *tag)
9babb374 1427{
5ebe425a 1428 ZFS_TEARDOWN_EXIT(zfsvfs, tag);
9babb374 1429
362ae8d1
MM
1430 if (zfs_vfs_held(zfsvfs)) {
1431 zfs_vfs_rele(zfsvfs);
9babb374 1432 } else {
b5256303 1433 dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs);
f298b24d 1434 zfsvfs_free(zfsvfs);
9babb374
BB
1435 }
1436}
1437
34dc7c2f
BB
1438static int
1439zfs_ioc_pool_create(zfs_cmd_t *zc)
1440{
1441 int error;
1442 nvlist_t *config, *props = NULL;
b128c09f
BB
1443 nvlist_t *rootprops = NULL;
1444 nvlist_t *zplprops = NULL;
b5256303 1445 dsl_crypto_params_t *dcp = NULL;
4d55ea81 1446 const char *spa_name = zc->zc_name;
ab1a9705 1447 boolean_t unload_wkey = B_TRUE;
34dc7c2f 1448
c65aa5b2
BB
1449 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1450 zc->zc_iflags, &config)))
34dc7c2f
BB
1451 return (error);
1452
1453 if (zc->zc_nvlist_src_size != 0 && (error =
9babb374
BB
1454 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1455 zc->zc_iflags, &props))) {
34dc7c2f
BB
1456 nvlist_free(config);
1457 return (error);
1458 }
1459
b128c09f
BB
1460 if (props) {
1461 nvlist_t *nvl = NULL;
b5256303 1462 nvlist_t *hidden_args = NULL;
b128c09f 1463 uint64_t version = SPA_VERSION;
4ceb8dd6 1464 char *tname;
b128c09f
BB
1465
1466 (void) nvlist_lookup_uint64(props,
1467 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
9ae529ec 1468 if (!SPA_VERSION_IS_SUPPORTED(version)) {
2e528b49 1469 error = SET_ERROR(EINVAL);
b128c09f
BB
1470 goto pool_props_bad;
1471 }
1472 (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl);
1473 if (nvl) {
1474 error = nvlist_dup(nvl, &rootprops, KM_SLEEP);
ab1a9705 1475 if (error != 0)
1476 goto pool_props_bad;
b128c09f
BB
1477 (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1478 }
b5256303
TC
1479
1480 (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS,
1481 &hidden_args);
1482 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1483 rootprops, hidden_args, &dcp);
ab1a9705 1484 if (error != 0)
1485 goto pool_props_bad;
b5256303
TC
1486 (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS);
1487
b128c09f
BB
1488 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1489 error = zfs_fill_zplprops_root(version, rootprops,
1490 zplprops, NULL);
13fe0198 1491 if (error != 0)
b128c09f 1492 goto pool_props_bad;
4ceb8dd6 1493
1494 if (nvlist_lookup_string(props,
1495 zpool_prop_to_name(ZPOOL_PROP_TNAME), &tname) == 0)
1496 spa_name = tname;
b128c09f
BB
1497 }
1498
b5256303 1499 error = spa_create(zc->zc_name, config, props, zplprops, dcp);
b128c09f
BB
1500
1501 /*
1502 * Set the remaining root properties
1503 */
4ceb8dd6 1504 if (!error && (error = zfs_set_prop_nvlist(spa_name,
ab1a9705 1505 ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) {
4ceb8dd6 1506 (void) spa_destroy(spa_name);
ab1a9705 1507 unload_wkey = B_FALSE; /* spa_destroy() unloads wrapping keys */
1508 }
34dc7c2f 1509
b128c09f
BB
1510pool_props_bad:
1511 nvlist_free(rootprops);
1512 nvlist_free(zplprops);
34dc7c2f 1513 nvlist_free(config);
b128c09f 1514 nvlist_free(props);
ab1a9705 1515 dsl_crypto_params_free(dcp, unload_wkey && !!error);
34dc7c2f
BB
1516
1517 return (error);
1518}
1519
1520static int
1521zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1522{
1523 int error;
1524 zfs_log_history(zc);
1525 error = spa_destroy(zc->zc_name);
a0bd735a 1526
34dc7c2f
BB
1527 return (error);
1528}
1529
1530static int
1531zfs_ioc_pool_import(zfs_cmd_t *zc)
1532{
34dc7c2f
BB
1533 nvlist_t *config, *props = NULL;
1534 uint64_t guid;
428870ff 1535 int error;
34dc7c2f
BB
1536
1537 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1538 zc->zc_iflags, &config)) != 0)
34dc7c2f
BB
1539 return (error);
1540
1541 if (zc->zc_nvlist_src_size != 0 && (error =
9babb374
BB
1542 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1543 zc->zc_iflags, &props))) {
34dc7c2f
BB
1544 nvlist_free(config);
1545 return (error);
1546 }
1547
1548 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1549 guid != zc->zc_guid)
2e528b49 1550 error = SET_ERROR(EINVAL);
34dc7c2f 1551 else
572e2857 1552 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
34dc7c2f 1553
572e2857
BB
1554 if (zc->zc_nvlist_dst != 0) {
1555 int err;
1556
1557 if ((err = put_nvlist(zc, config)) != 0)
1558 error = err;
1559 }
428870ff 1560
34dc7c2f 1561 nvlist_free(config);
8a5fc748 1562 nvlist_free(props);
34dc7c2f
BB
1563
1564 return (error);
1565}
1566
1567static int
1568zfs_ioc_pool_export(zfs_cmd_t *zc)
1569{
1570 int error;
b128c09f 1571 boolean_t force = (boolean_t)zc->zc_cookie;
fb5f0bc8 1572 boolean_t hardforce = (boolean_t)zc->zc_guid;
b128c09f 1573
34dc7c2f 1574 zfs_log_history(zc);
fb5f0bc8 1575 error = spa_export(zc->zc_name, NULL, force, hardforce);
a0bd735a 1576
34dc7c2f
BB
1577 return (error);
1578}
1579
1580static int
1581zfs_ioc_pool_configs(zfs_cmd_t *zc)
1582{
1583 nvlist_t *configs;
1584 int error;
1585
1586 if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
2e528b49 1587 return (SET_ERROR(EEXIST));
34dc7c2f
BB
1588
1589 error = put_nvlist(zc, configs);
1590
1591 nvlist_free(configs);
1592
1593 return (error);
1594}
1595
9ae529ec
CS
1596/*
1597 * inputs:
1598 * zc_name name of the pool
1599 *
1600 * outputs:
1601 * zc_cookie real errno
1602 * zc_nvlist_dst config nvlist
1603 * zc_nvlist_dst_size size of config nvlist
1604 */
34dc7c2f
BB
1605static int
1606zfs_ioc_pool_stats(zfs_cmd_t *zc)
1607{
1608 nvlist_t *config;
1609 int error;
1610 int ret = 0;
1611
1612 error = spa_get_stats(zc->zc_name, &config, zc->zc_value,
1613 sizeof (zc->zc_value));
1614
1615 if (config != NULL) {
1616 ret = put_nvlist(zc, config);
1617 nvlist_free(config);
1618
1619 /*
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'.
1623 */
1624 zc->zc_cookie = error;
1625 } else {
1626 ret = error;
1627 }
1628
1629 return (ret);
1630}
1631
1632/*
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.
1635 */
1636static int
1637zfs_ioc_pool_tryimport(zfs_cmd_t *zc)
1638{
379ca9cf 1639 nvlist_t *tryconfig, *config = NULL;
34dc7c2f
BB
1640 int error;
1641
1642 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1643 zc->zc_iflags, &tryconfig)) != 0)
34dc7c2f
BB
1644 return (error);
1645
1646 config = spa_tryimport(tryconfig);
1647
1648 nvlist_free(tryconfig);
1649
1650 if (config == NULL)
2e528b49 1651 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1652
1653 error = put_nvlist(zc, config);
1654 nvlist_free(config);
1655
1656 return (error);
1657}
1658
428870ff
BB
1659/*
1660 * inputs:
1661 * zc_name name of the pool
1662 * zc_cookie scan func (pool_scan_func_t)
0ea05c64 1663 * zc_flags scrub pause/resume flag (pool_scrub_cmd_t)
428870ff 1664 */
34dc7c2f 1665static int
428870ff 1666zfs_ioc_pool_scan(zfs_cmd_t *zc)
34dc7c2f
BB
1667{
1668 spa_t *spa;
1669 int error;
1670
0ea05c64
AP
1671 if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1672 return (SET_ERROR(EINVAL));
1673
1724eb62 1674 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1675 return (error);
1676
0ea05c64
AP
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)
428870ff
BB
1680 error = spa_scan_stop(spa);
1681 else
1682 error = spa_scan(spa, zc->zc_cookie);
34dc7c2f
BB
1683
1684 spa_close(spa, FTAG);
1685
1686 return (error);
1687}
1688
1689static int
1690zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1691{
1692 spa_t *spa;
1693 int error;
1694
1695 error = spa_open(zc->zc_name, &spa, FTAG);
1696 if (error == 0) {
1697 spa_freeze(spa);
1698 spa_close(spa, FTAG);
1699 }
1700 return (error);
1701}
1702
1703static int
1704zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1705{
1706 spa_t *spa;
1707 int error;
1708
1709 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1710 return (error);
1711
9ae529ec
CS
1712 if (zc->zc_cookie < spa_version(spa) ||
1713 !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) {
34dc7c2f 1714 spa_close(spa, FTAG);
2e528b49 1715 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1716 }
1717
1718 spa_upgrade(spa, zc->zc_cookie);
1719 spa_close(spa, FTAG);
1720
1721 return (error);
1722}
1723
1724static int
1725zfs_ioc_pool_get_history(zfs_cmd_t *zc)
1726{
1727 spa_t *spa;
1728 char *hist_buf;
1729 uint64_t size;
1730 int error;
1731
1732 if ((size = zc->zc_history_len) == 0)
2e528b49 1733 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1734
1735 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1736 return (error);
1737
1738 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
1739 spa_close(spa, FTAG);
2e528b49 1740 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
1741 }
1742
34b84cb8 1743 hist_buf = vmem_alloc(size, KM_SLEEP);
34dc7c2f
BB
1744 if ((error = spa_history_get(spa, &zc->zc_history_offset,
1745 &zc->zc_history_len, hist_buf)) == 0) {
9babb374
BB
1746 error = ddi_copyout(hist_buf,
1747 (void *)(uintptr_t)zc->zc_history,
1748 zc->zc_history_len, zc->zc_iflags);
34dc7c2f
BB
1749 }
1750
1751 spa_close(spa, FTAG);
34b84cb8 1752 vmem_free(hist_buf, size);
34dc7c2f
BB
1753 return (error);
1754}
1755
3541dc6d
GA
1756static int
1757zfs_ioc_pool_reguid(zfs_cmd_t *zc)
1758{
1759 spa_t *spa;
1760 int error;
1761
1762 error = spa_open(zc->zc_name, &spa, FTAG);
1763 if (error == 0) {
1764 error = spa_change_guid(spa);
1765 spa_close(spa, FTAG);
1766 }
1767 return (error);
1768}
1769
34dc7c2f
BB
1770static int
1771zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc)
1772{
13fe0198 1773 return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value));
34dc7c2f
BB
1774}
1775
428870ff
BB
1776/*
1777 * inputs:
1778 * zc_name name of filesystem
1779 * zc_obj object to find
1780 *
1781 * outputs:
1782 * zc_value name of object
1783 */
34dc7c2f
BB
1784static int
1785zfs_ioc_obj_to_path(zfs_cmd_t *zc)
1786{
428870ff 1787 objset_t *os;
34dc7c2f
BB
1788 int error;
1789
428870ff 1790 /* XXX reading from objset not owned */
b5256303
TC
1791 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1792 FTAG, &os)) != 0)
34dc7c2f 1793 return (error);
428870ff 1794 if (dmu_objset_type(os) != DMU_OST_ZFS) {
b5256303 1795 dmu_objset_rele_flags(os, B_TRUE, FTAG);
2e528b49 1796 return (SET_ERROR(EINVAL));
428870ff
BB
1797 }
1798 error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value,
34dc7c2f 1799 sizeof (zc->zc_value));
b5256303 1800 dmu_objset_rele_flags(os, B_TRUE, FTAG);
34dc7c2f
BB
1801
1802 return (error);
1803}
1804
572e2857
BB
1805/*
1806 * inputs:
1807 * zc_name name of filesystem
1808 * zc_obj object to find
1809 *
1810 * outputs:
1811 * zc_stat stats on object
1812 * zc_value path to object
1813 */
1814static int
1815zfs_ioc_obj_to_stats(zfs_cmd_t *zc)
1816{
1817 objset_t *os;
1818 int error;
1819
1820 /* XXX reading from objset not owned */
b5256303
TC
1821 if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE,
1822 FTAG, &os)) != 0)
572e2857
BB
1823 return (error);
1824 if (dmu_objset_type(os) != DMU_OST_ZFS) {
b5256303 1825 dmu_objset_rele_flags(os, B_TRUE, FTAG);
2e528b49 1826 return (SET_ERROR(EINVAL));
572e2857
BB
1827 }
1828 error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1829 sizeof (zc->zc_value));
b5256303 1830 dmu_objset_rele_flags(os, B_TRUE, FTAG);
572e2857
BB
1831
1832 return (error);
1833}
1834
34dc7c2f
BB
1835static int
1836zfs_ioc_vdev_add(zfs_cmd_t *zc)
1837{
1838 spa_t *spa;
1839 int error;
64ad2b26 1840 nvlist_t *config;
34dc7c2f
BB
1841
1842 error = spa_open(zc->zc_name, &spa, FTAG);
1843 if (error != 0)
1844 return (error);
1845
1846 error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1847 zc->zc_iflags, &config);
34dc7c2f
BB
1848 if (error == 0) {
1849 error = spa_vdev_add(spa, config);
1850 nvlist_free(config);
1851 }
1852 spa_close(spa, FTAG);
1853 return (error);
1854}
1855
428870ff
BB
1856/*
1857 * inputs:
1858 * zc_name name of the pool
a1d477c2
MA
1859 * zc_guid guid of vdev to remove
1860 * zc_cookie cancel removal
428870ff 1861 */
34dc7c2f
BB
1862static int
1863zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1864{
1865 spa_t *spa;
1866 int error;
1867
1868 error = spa_open(zc->zc_name, &spa, FTAG);
1869 if (error != 0)
1870 return (error);
a1d477c2
MA
1871 if (zc->zc_cookie != 0) {
1872 error = spa_vdev_remove_cancel(spa);
1873 } else {
1874 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1875 }
34dc7c2f
BB
1876 spa_close(spa, FTAG);
1877 return (error);
1878}
1879
1880static int
1881zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1882{
1883 spa_t *spa;
1884 int error;
1885 vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1886
1887 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1888 return (error);
1889 switch (zc->zc_cookie) {
1890 case VDEV_STATE_ONLINE:
1891 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1892 break;
1893
1894 case VDEV_STATE_OFFLINE:
1895 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1896 break;
1897
1898 case VDEV_STATE_FAULTED:
428870ff 1899 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
4a283c7f
TH
1900 zc->zc_obj != VDEV_AUX_EXTERNAL &&
1901 zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST)
428870ff
BB
1902 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1903
1904 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
34dc7c2f
BB
1905 break;
1906
1907 case VDEV_STATE_DEGRADED:
428870ff
BB
1908 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1909 zc->zc_obj != VDEV_AUX_EXTERNAL)
1910 zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1911
1912 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
34dc7c2f
BB
1913 break;
1914
1915 default:
2e528b49 1916 error = SET_ERROR(EINVAL);
34dc7c2f
BB
1917 }
1918 zc->zc_cookie = newstate;
1919 spa_close(spa, FTAG);
1920 return (error);
1921}
1922
1923static int
1924zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1925{
1926 spa_t *spa;
34dc7c2f 1927 nvlist_t *config;
9a49d3f3
BB
1928 int replacing = zc->zc_cookie;
1929 int rebuild = zc->zc_simple;
34dc7c2f
BB
1930 int error;
1931
1932 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1933 return (error);
1934
1935 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
9babb374 1936 zc->zc_iflags, &config)) == 0) {
9a49d3f3
BB
1937 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing,
1938 rebuild);
34dc7c2f
BB
1939 nvlist_free(config);
1940 }
1941
1942 spa_close(spa, FTAG);
1943 return (error);
1944}
1945
1946static int
1947zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1948{
1949 spa_t *spa;
1950 int error;
1951
1952 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1953 return (error);
1954
fb5f0bc8 1955 error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
34dc7c2f
BB
1956
1957 spa_close(spa, FTAG);
1958 return (error);
1959}
1960
428870ff
BB
1961static int
1962zfs_ioc_vdev_split(zfs_cmd_t *zc)
1963{
1964 spa_t *spa;
1965 nvlist_t *config, *props = NULL;
1966 int error;
1967 boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
1968
1969 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1970 return (error);
1971
c65aa5b2
BB
1972 if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1973 zc->zc_iflags, &config))) {
428870ff
BB
1974 spa_close(spa, FTAG);
1975 return (error);
1976 }
1977
1978 if (zc->zc_nvlist_src_size != 0 && (error =
1979 get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1980 zc->zc_iflags, &props))) {
1981 spa_close(spa, FTAG);
1982 nvlist_free(config);
1983 return (error);
1984 }
1985
1986 error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
1987
1988 spa_close(spa, FTAG);
1989
1990 nvlist_free(config);
1991 nvlist_free(props);
1992
1993 return (error);
1994}
1995
34dc7c2f
BB
1996static int
1997zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
1998{
1999 spa_t *spa;
4d55ea81 2000 const char *path = zc->zc_value;
34dc7c2f
BB
2001 uint64_t guid = zc->zc_guid;
2002 int error;
2003
2004 error = spa_open(zc->zc_name, &spa, FTAG);
2005 if (error != 0)
2006 return (error);
2007
2008 error = spa_vdev_setpath(spa, guid, path);
2009 spa_close(spa, FTAG);
2010 return (error);
2011}
2012
9babb374
BB
2013static int
2014zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2015{
2016 spa_t *spa;
4d55ea81 2017 const char *fru = zc->zc_value;
9babb374
BB
2018 uint64_t guid = zc->zc_guid;
2019 int error;
2020
2021 error = spa_open(zc->zc_name, &spa, FTAG);
2022 if (error != 0)
2023 return (error);
2024
2025 error = spa_vdev_setfru(spa, guid, fru);
2026 spa_close(spa, FTAG);
2027 return (error);
2028}
2029
34dc7c2f 2030static int
572e2857 2031zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os)
34dc7c2f 2032{
572e2857 2033 int error = 0;
34dc7c2f
BB
2034 nvlist_t *nv;
2035
34dc7c2f
BB
2036 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2037
399b9819 2038 if (zc->zc_nvlist_dst != 0 &&
428870ff 2039 (error = dsl_prop_get_all(os, &nv)) == 0) {
34dc7c2f
BB
2040 dmu_objset_stats(os, nv);
2041 /*
2042 * NB: zvol_get_stats() will read the objset contents,
2043 * which we aren't supposed to do with a
b128c09f 2044 * DS_MODE_USER hold, because it could be
34dc7c2f 2045 * inconsistent. So this is a bit of a workaround...
e1cfd73f 2046 * XXX reading without owning
34dc7c2f 2047 */
330d06f9
MA
2048 if (!zc->zc_objset_stats.dds_inconsistent &&
2049 dmu_objset_type(os) == DMU_OST_ZVOL) {
2050 error = zvol_get_stats(os, nv);
6d421005
B
2051 if (error == EIO) {
2052 nvlist_free(nv);
330d06f9 2053 return (error);
6d421005 2054 }
c99c9001 2055 VERIFY0(error);
34dc7c2f 2056 }
8a8f5c6b
BB
2057 if (error == 0)
2058 error = put_nvlist(zc, nv);
34dc7c2f
BB
2059 nvlist_free(nv);
2060 }
2061
572e2857
BB
2062 return (error);
2063}
2064
2065/*
2066 * inputs:
2067 * zc_name name of filesystem
2068 * zc_nvlist_dst_size size of buffer for property nvlist
2069 *
2070 * outputs:
2071 * zc_objset_stats stats
2072 * zc_nvlist_dst property nvlist
2073 * zc_nvlist_dst_size size of property nvlist
2074 */
2075static int
2076zfs_ioc_objset_stats(zfs_cmd_t *zc)
2077{
13fe0198 2078 objset_t *os;
572e2857
BB
2079 int error;
2080
13fe0198
MA
2081 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2082 if (error == 0) {
2083 error = zfs_ioc_objset_stats_impl(zc, os);
2084 dmu_objset_rele(os, FTAG);
2085 }
572e2857 2086
428870ff
BB
2087 return (error);
2088}
2089
2090/*
2091 * inputs:
2092 * zc_name name of filesystem
2093 * zc_nvlist_dst_size size of buffer for property nvlist
2094 *
2095 * outputs:
2096 * zc_nvlist_dst received property nvlist
2097 * zc_nvlist_dst_size size of received property nvlist
2098 *
2099 * Gets received properties (distinct from local properties on or after
2100 * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from
2101 * local property values.
2102 */
2103static int
26685276 2104zfs_ioc_objset_recvd_props(zfs_cmd_t *zc)
428870ff 2105{
13fe0198 2106 int error = 0;
428870ff
BB
2107 nvlist_t *nv;
2108
428870ff
BB
2109 /*
2110 * Without this check, we would return local property values if the
2111 * caller has not already received properties on or after
2112 * SPA_VERSION_RECVD_PROPS.
2113 */
13fe0198 2114 if (!dsl_prop_get_hasrecvd(zc->zc_name))
2e528b49 2115 return (SET_ERROR(ENOTSUP));
428870ff
BB
2116
2117 if (zc->zc_nvlist_dst != 0 &&
13fe0198 2118 (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) {
428870ff
BB
2119 error = put_nvlist(zc, nv);
2120 nvlist_free(nv);
2121 }
2122
34dc7c2f
BB
2123 return (error);
2124}
2125
2126static int
2127nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop)
2128{
2129 uint64_t value;
2130 int error;
2131
2132 /*
2133 * zfs_get_zplprop() will either find a value or give us
2134 * the default value (if there is one).
2135 */
2136 if ((error = zfs_get_zplprop(os, prop, &value)) != 0)
2137 return (error);
2138 VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0);
2139 return (0);
2140}
2141
2142/*
2143 * inputs:
2144 * zc_name name of filesystem
2145 * zc_nvlist_dst_size size of buffer for zpl property nvlist
2146 *
2147 * outputs:
2148 * zc_nvlist_dst zpl property nvlist
2149 * zc_nvlist_dst_size size of zpl property nvlist
2150 */
2151static int
2152zfs_ioc_objset_zplprops(zfs_cmd_t *zc)
2153{
2154 objset_t *os;
2155 int err;
2156
428870ff 2157 /* XXX reading without owning */
c65aa5b2 2158 if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os)))
34dc7c2f
BB
2159 return (err);
2160
2161 dmu_objset_fast_stat(os, &zc->zc_objset_stats);
2162
2163 /*
2164 * NB: nvl_add_zplprop() will read the objset contents,
b128c09f
BB
2165 * which we aren't supposed to do with a DS_MODE_USER
2166 * hold, because it could be inconsistent.
34dc7c2f 2167 */
b8864a23 2168 if (zc->zc_nvlist_dst != 0 &&
34dc7c2f
BB
2169 !zc->zc_objset_stats.dds_inconsistent &&
2170 dmu_objset_type(os) == DMU_OST_ZFS) {
2171 nvlist_t *nv;
2172
2173 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2174 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2175 (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2176 (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2177 (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2178 err = put_nvlist(zc, nv);
2179 nvlist_free(nv);
2180 } else {
2e528b49 2181 err = SET_ERROR(ENOENT);
34dc7c2f 2182 }
428870ff 2183 dmu_objset_rele(os, FTAG);
34dc7c2f
BB
2184 return (err);
2185}
2186
2187/*
2188 * inputs:
2189 * zc_name name of filesystem
2190 * zc_cookie zap cursor
2191 * zc_nvlist_dst_size size of buffer for property nvlist
2192 *
2193 * outputs:
2194 * zc_name name of next filesystem
9babb374 2195 * zc_cookie zap cursor
34dc7c2f
BB
2196 * zc_objset_stats stats
2197 * zc_nvlist_dst property nvlist
2198 * zc_nvlist_dst_size size of property nvlist
34dc7c2f
BB
2199 */
2200static int
2201zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2202{
2203 objset_t *os;
2204 int error;
2205 char *p;
428870ff 2206 size_t orig_len = strlen(zc->zc_name);
34dc7c2f 2207
428870ff 2208top:
c65aa5b2 2209 if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) {
34dc7c2f 2210 if (error == ENOENT)
2e528b49 2211 error = SET_ERROR(ESRCH);
34dc7c2f
BB
2212 return (error);
2213 }
2214
2215 p = strrchr(zc->zc_name, '/');
2216 if (p == NULL || p[1] != '\0')
2217 (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name));
2218 p = zc->zc_name + strlen(zc->zc_name);
2219
2220 do {
2221 error = dmu_dir_list_next(os,
2222 sizeof (zc->zc_name) - (p - zc->zc_name), p,
2223 NULL, &zc->zc_cookie);
2224 if (error == ENOENT)
2e528b49 2225 error = SET_ERROR(ESRCH);
2e5dc449 2226 } while (error == 0 && zfs_dataset_name_hidden(zc->zc_name));
428870ff 2227 dmu_objset_rele(os, FTAG);
34dc7c2f 2228
428870ff
BB
2229 /*
2230 * If it's an internal dataset (ie. with a '$' in its name),
2231 * don't try to get stats for it, otherwise we'll return ENOENT.
2232 */
2233 if (error == 0 && strchr(zc->zc_name, '$') == NULL) {
34dc7c2f 2234 error = zfs_ioc_objset_stats(zc); /* fill in the stats */
428870ff
BB
2235 if (error == ENOENT) {
2236 /* We lost a race with destroy, get the next one. */
2237 zc->zc_name[orig_len] = '\0';
2238 goto top;
2239 }
2240 }
34dc7c2f
BB
2241 return (error);
2242}
2243
2244/*
2245 * inputs:
2246 * zc_name name of filesystem
2247 * zc_cookie zap cursor
4c0883fb
AP
2248 * zc_nvlist_src iteration range nvlist
2249 * zc_nvlist_src_size size of iteration range nvlist
34dc7c2f
BB
2250 *
2251 * outputs:
2252 * zc_name name of next snapshot
2253 * zc_objset_stats stats
2254 * zc_nvlist_dst property nvlist
2255 * zc_nvlist_dst_size size of property nvlist
34dc7c2f
BB
2256 */
2257static int
2258zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
2259{
34dc7c2f 2260 int error;
4c0883fb
AP
2261 objset_t *os, *ossnap;
2262 dsl_dataset_t *ds;
2263 uint64_t min_txg = 0, max_txg = 0;
2264
2265 if (zc->zc_nvlist_src_size != 0) {
2266 nvlist_t *props = NULL;
2267 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2268 zc->zc_iflags, &props);
2269 if (error != 0)
2270 return (error);
2271 (void) nvlist_lookup_uint64(props, SNAP_ITER_MIN_TXG,
2272 &min_txg);
2273 (void) nvlist_lookup_uint64(props, SNAP_ITER_MAX_TXG,
2274 &max_txg);
2275 nvlist_free(props);
2276 }
34dc7c2f 2277
428870ff 2278 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
13fe0198 2279 if (error != 0) {
28caa74b 2280 return (error == ENOENT ? SET_ERROR(ESRCH) : error);
13fe0198 2281 }
34dc7c2f
BB
2282
2283 /*
2284 * A dataset name of maximum length cannot have any snapshots,
2285 * so exit immediately.
2286 */
eca7b760
IK
2287 if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >=
2288 ZFS_MAX_DATASET_NAME_LEN) {
428870ff 2289 dmu_objset_rele(os, FTAG);
2e528b49 2290 return (SET_ERROR(ESRCH));
34dc7c2f
BB
2291 }
2292
4c0883fb
AP
2293 while (error == 0) {
2294 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2295 error = SET_ERROR(EINTR);
2296 break;
2297 }
572e2857 2298
4c0883fb
AP
2299 error = dmu_snapshot_list_next(os,
2300 sizeof (zc->zc_name) - strlen(zc->zc_name),
2301 zc->zc_name + strlen(zc->zc_name), &zc->zc_obj,
2302 &zc->zc_cookie, NULL);
2303 if (error == ENOENT) {
2304 error = SET_ERROR(ESRCH);
2305 break;
2306 } else if (error != 0) {
2307 break;
2308 }
572e2857 2309
4c0883fb
AP
2310 error = dsl_dataset_hold_obj(dmu_objset_pool(os), zc->zc_obj,
2311 FTAG, &ds);
2312 if (error != 0)
2313 break;
572e2857 2314
4c0883fb
AP
2315 if ((min_txg != 0 && dsl_get_creationtxg(ds) < min_txg) ||
2316 (max_txg != 0 && dsl_get_creationtxg(ds) > max_txg)) {
572e2857 2317 dsl_dataset_rele(ds, FTAG);
4c0883fb
AP
2318 /* undo snapshot name append */
2319 *(strchr(zc->zc_name, '@') + 1) = '\0';
2320 /* skip snapshot */
2321 continue;
428870ff 2322 }
4c0883fb
AP
2323
2324 if (zc->zc_simple) {
3a1ce491
AH
2325 zc->zc_objset_stats.dds_creation_txg =
2326 dsl_get_creationtxg(ds);
4c0883fb
AP
2327 dsl_dataset_rele(ds, FTAG);
2328 break;
2329 }
2330
2331 if ((error = dmu_objset_from_ds(ds, &ossnap)) != 0) {
2332 dsl_dataset_rele(ds, FTAG);
2333 break;
2334 }
2335 if ((error = zfs_ioc_objset_stats_impl(zc, ossnap)) != 0) {
2336 dsl_dataset_rele(ds, FTAG);
2337 break;
2338 }
2339 dsl_dataset_rele(ds, FTAG);
2340 break;
428870ff 2341 }
34dc7c2f 2342
572e2857 2343 dmu_objset_rele(os, FTAG);
34dc7c2f 2344 /* if we failed, undo the @ that we tacked on to zc_name */
13fe0198 2345 if (error != 0)
34dc7c2f 2346 *strchr(zc->zc_name, '@') = '\0';
34dc7c2f
BB
2347 return (error);
2348}
2349
428870ff
BB
2350static int
2351zfs_prop_set_userquota(const char *dsname, nvpair_t *pair)
34dc7c2f 2352{
428870ff
BB
2353 const char *propname = nvpair_name(pair);
2354 uint64_t *valary;
2355 unsigned int vallen;
4d55ea81 2356 const char *dash, *domain;
428870ff
BB
2357 zfs_userquota_prop_t type;
2358 uint64_t rid;
2359 uint64_t quota;
0037b49e 2360 zfsvfs_t *zfsvfs;
428870ff
BB
2361 int err;
2362
2363 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2364 nvlist_t *attrs;
2365 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2366 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2367 &pair) != 0)
2e528b49 2368 return (SET_ERROR(EINVAL));
428870ff 2369 }
34dc7c2f
BB
2370
2371 /*
428870ff
BB
2372 * A correctly constructed propname is encoded as
2373 * userquota@<rid>-<domain>.
34dc7c2f 2374 */
428870ff
BB
2375 if ((dash = strchr(propname, '-')) == NULL ||
2376 nvpair_value_uint64_array(pair, &valary, &vallen) != 0 ||
2377 vallen != 3)
2e528b49 2378 return (SET_ERROR(EINVAL));
34dc7c2f 2379
428870ff
BB
2380 domain = dash + 1;
2381 type = valary[0];
2382 rid = valary[1];
2383 quota = valary[2];
34dc7c2f 2384
f298b24d 2385 err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE);
428870ff 2386 if (err == 0) {
0037b49e 2387 err = zfs_set_userquota(zfsvfs, type, domain, rid, quota);
f298b24d 2388 zfsvfs_rele(zfsvfs, FTAG);
428870ff 2389 }
9babb374 2390
428870ff
BB
2391 return (err);
2392}
34dc7c2f 2393
428870ff
BB
2394/*
2395 * If the named property is one that has a special function to set its value,
2396 * return 0 on success and a positive error code on failure; otherwise if it is
2397 * not one of the special properties handled by this function, return -1.
2398 *
2399 * XXX: It would be better for callers of the property interface if we handled
2400 * these special cases in dsl_prop.c (in the dsl layer).
2401 */
2402static int
2403zfs_prop_set_special(const char *dsname, zprop_source_t source,
2404 nvpair_t *pair)
2405{
2406 const char *propname = nvpair_name(pair);
2407 zfs_prop_t prop = zfs_name_to_prop(propname);
b5256303 2408 uint64_t intval = 0;
4d55ea81 2409 const char *strval = NULL;
f1512ee6 2410 int err = -1;
9babb374 2411
4ff7a8fa 2412 if (prop == ZPROP_USERPROP) {
428870ff
BB
2413 if (zfs_prop_userquota(propname))
2414 return (zfs_prop_set_userquota(dsname, pair));
2415 return (-1);
2416 }
34dc7c2f 2417
428870ff
BB
2418 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2419 nvlist_t *attrs;
2420 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
2421 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2422 &pair) == 0);
2423 }
b128c09f 2424
b5256303
TC
2425 /* all special properties are numeric except for keylocation */
2426 if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
2427 strval = fnvpair_value_string(pair);
2428 } else {
2429 intval = fnvpair_value_uint64(pair);
2430 }
34dc7c2f 2431
428870ff
BB
2432 switch (prop) {
2433 case ZFS_PROP_QUOTA:
2434 err = dsl_dir_set_quota(dsname, source, intval);
2435 break;
2436 case ZFS_PROP_REFQUOTA:
13fe0198 2437 err = dsl_dataset_set_refquota(dsname, source, intval);
428870ff 2438 break;
788eb90c
JJ
2439 case ZFS_PROP_FILESYSTEM_LIMIT:
2440 case ZFS_PROP_SNAPSHOT_LIMIT:
2441 if (intval == UINT64_MAX) {
2442 /* clearing the limit, just do it */
2443 err = 0;
2444 } else {
2445 err = dsl_dir_activate_fs_ss_limit(dsname);
2446 }
b5256303
TC
2447 /*
2448 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2449 * default path to set the value in the nvlist.
2450 */
2451 if (err == 0)
2452 err = -1;
2453 break;
2454 case ZFS_PROP_KEYLOCATION:
2455 err = dsl_crypto_can_set_keylocation(dsname, strval);
2456
788eb90c
JJ
2457 /*
2458 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2459 * default path to set the value in the nvlist.
2460 */
2461 if (err == 0)
2462 err = -1;
2463 break;
428870ff
BB
2464 case ZFS_PROP_RESERVATION:
2465 err = dsl_dir_set_reservation(dsname, source, intval);
2466 break;
2467 case ZFS_PROP_REFRESERVATION:
13fe0198 2468 err = dsl_dataset_set_refreservation(dsname, source, intval);
428870ff 2469 break;
10b3c7f5
MN
2470 case ZFS_PROP_COMPRESSION:
2471 err = dsl_dataset_set_compression(dsname, source, intval);
2472 /*
2473 * Set err to -1 to force the zfs_set_prop_nvlist code down the
2474 * default path to set the value in the nvlist.
2475 */
2476 if (err == 0)
2477 err = -1;
2478 break;
428870ff 2479 case ZFS_PROP_VOLSIZE:
60101509 2480 err = zvol_set_volsize(dsname, intval);
428870ff 2481 break;
0b4d1b58 2482 case ZFS_PROP_SNAPDEV:
a0bd735a 2483 err = zvol_set_snapdev(dsname, source, intval);
0b4d1b58 2484 break;
cf8738d8 2485 case ZFS_PROP_VOLMODE:
2486 err = zvol_set_volmode(dsname, source, intval);
2487 break;
428870ff
BB
2488 case ZFS_PROP_VERSION:
2489 {
0037b49e 2490 zfsvfs_t *zfsvfs;
428870ff 2491
f298b24d 2492 if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0)
34dc7c2f 2493 break;
b128c09f 2494
0037b49e 2495 err = zfs_set_version(zfsvfs, intval);
f298b24d 2496 zfsvfs_rele(zfsvfs, FTAG);
34dc7c2f 2497
428870ff
BB
2498 if (err == 0 && intval >= ZPL_VERSION_USERSPACE) {
2499 zfs_cmd_t *zc;
34dc7c2f 2500
efcd79a8 2501 zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
c9e319fa
JL
2502 (void) strlcpy(zc->zc_name, dsname,
2503 sizeof (zc->zc_name));
428870ff 2504 (void) zfs_ioc_userspace_upgrade(zc);
9c5167d1 2505 (void) zfs_ioc_id_quota_upgrade(zc);
428870ff 2506 kmem_free(zc, sizeof (zfs_cmd_t));
34dc7c2f 2507 }
428870ff
BB
2508 break;
2509 }
428870ff
BB
2510 default:
2511 err = -1;
2512 }
34dc7c2f 2513
428870ff
BB
2514 return (err);
2515}
34dc7c2f 2516
75b4cbf6
AS
2517static boolean_t
2518zfs_is_namespace_prop(zfs_prop_t prop)
2519{
2520 switch (prop) {
2521
2522 case ZFS_PROP_ATIME:
2523 case ZFS_PROP_RELATIME:
2524 case ZFS_PROP_DEVICES:
2525 case ZFS_PROP_EXEC:
2526 case ZFS_PROP_SETUID:
2527 case ZFS_PROP_READONLY:
2528 case ZFS_PROP_XATTR:
2529 case ZFS_PROP_NBMAND:
2530 return (B_TRUE);
2531
2532 default:
2533 return (B_FALSE);
2534 }
2535}
2536
428870ff
BB
2537/*
2538 * This function is best effort. If it fails to set any of the given properties,
6f1ffb06
MA
2539 * it continues to set as many as it can and returns the last error
2540 * encountered. If the caller provides a non-NULL errlist, it will be filled in
2541 * with the list of names of all the properties that failed along with the
2542 * corresponding error numbers.
428870ff 2543 *
6f1ffb06
MA
2544 * If every property is set successfully, zero is returned and errlist is not
2545 * modified.
428870ff
BB
2546 */
2547int
2548zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
6f1ffb06 2549 nvlist_t *errlist)
428870ff
BB
2550{
2551 nvpair_t *pair;
2552 nvpair_t *propval;
2553 int rv = 0;
2f14adac 2554 int err;
428870ff 2555 uint64_t intval;
4d55ea81 2556 const char *strval;
75b4cbf6 2557 boolean_t should_update_mount_cache = B_FALSE;
34dc7c2f 2558
6f1ffb06
MA
2559 nvlist_t *genericnvl = fnvlist_alloc();
2560 nvlist_t *retrynvl = fnvlist_alloc();
428870ff
BB
2561retry:
2562 pair = NULL;
2563 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2564 const char *propname = nvpair_name(pair);
2565 zfs_prop_t prop = zfs_name_to_prop(propname);
2f14adac 2566 err = 0;
428870ff
BB
2567
2568 /* decode the property value */
2569 propval = pair;
2570 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2571 nvlist_t *attrs;
6f1ffb06 2572 attrs = fnvpair_value_nvlist(pair);
428870ff
BB
2573 if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2574 &propval) != 0)
2e528b49 2575 err = SET_ERROR(EINVAL);
9babb374 2576 }
34dc7c2f 2577
428870ff 2578 /* Validate value type */
a3eeab2d 2579 if (err == 0 && source == ZPROP_SRC_INHERITED) {
2580 /* inherited properties are expected to be booleans */
2581 if (nvpair_type(propval) != DATA_TYPE_BOOLEAN)
2582 err = SET_ERROR(EINVAL);
4ff7a8fa 2583 } else if (err == 0 && prop == ZPROP_USERPROP) {
428870ff
BB
2584 if (zfs_prop_user(propname)) {
2585 if (nvpair_type(propval) != DATA_TYPE_STRING)
2e528b49 2586 err = SET_ERROR(EINVAL);
428870ff
BB
2587 } else if (zfs_prop_userquota(propname)) {
2588 if (nvpair_type(propval) !=
2589 DATA_TYPE_UINT64_ARRAY)
2e528b49 2590 err = SET_ERROR(EINVAL);
330d06f9 2591 } else {
2e528b49 2592 err = SET_ERROR(EINVAL);
428870ff
BB
2593 }
2594 } else if (err == 0) {
2595 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2596 if (zfs_prop_get_type(prop) != PROP_TYPE_STRING)
2e528b49 2597 err = SET_ERROR(EINVAL);
428870ff 2598 } else if (nvpair_type(propval) == DATA_TYPE_UINT64) {
34dc7c2f
BB
2599 const char *unused;
2600
6f1ffb06 2601 intval = fnvpair_value_uint64(propval);
34dc7c2f
BB
2602
2603 switch (zfs_prop_get_type(prop)) {
2604 case PROP_TYPE_NUMBER:
2605 break;
2606 case PROP_TYPE_STRING:
2e528b49 2607 err = SET_ERROR(EINVAL);
428870ff 2608 break;
34dc7c2f
BB
2609 case PROP_TYPE_INDEX:
2610 if (zfs_prop_index_to_string(prop,
428870ff 2611 intval, &unused) != 0)
8fb79fdd
AJ
2612 err =
2613 SET_ERROR(ZFS_ERR_BADPROP);
34dc7c2f
BB
2614 break;
2615 default:
2616 cmn_err(CE_PANIC,
2617 "unknown property type");
34dc7c2f 2618 }
34dc7c2f 2619 } else {
2e528b49 2620 err = SET_ERROR(EINVAL);
34dc7c2f 2621 }
34dc7c2f 2622 }
428870ff
BB
2623
2624 /* Validate permissions */
2625 if (err == 0)
2626 err = zfs_check_settable(dsname, pair, CRED());
2627
2628 if (err == 0) {
a3eeab2d 2629 if (source == ZPROP_SRC_INHERITED)
2630 err = -1; /* does not need special handling */
2631 else
2632 err = zfs_prop_set_special(dsname, source,
2633 pair);
428870ff
BB
2634 if (err == -1) {
2635 /*
2636 * For better performance we build up a list of
2637 * properties to set in a single transaction.
2638 */
2639 err = nvlist_add_nvpair(genericnvl, pair);
2640 } else if (err != 0 && nvl != retrynvl) {
2641 /*
2642 * This may be a spurious error caused by
2643 * receiving quota and reservation out of order.
2644 * Try again in a second pass.
2645 */
2646 err = nvlist_add_nvpair(retrynvl, pair);
2647 }
2648 }
2649
6f1ffb06
MA
2650 if (err != 0) {
2651 if (errlist != NULL)
2652 fnvlist_add_int32(errlist, propname, err);
2653 rv = err;
2654 }
75b4cbf6
AS
2655
2656 if (zfs_is_namespace_prop(prop))
2657 should_update_mount_cache = B_TRUE;
34dc7c2f
BB
2658 }
2659
428870ff
BB
2660 if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2661 nvl = retrynvl;
2662 goto retry;
2663 }
2664
2f14adac
CS
2665 if (nvlist_empty(genericnvl))
2666 goto out;
428870ff 2667
2f14adac
CS
2668 /*
2669 * Try to set them all in one batch.
2670 */
2671 err = dsl_props_set(dsname, source, genericnvl);
2672 if (err == 0)
2673 goto out;
428870ff 2674
2f14adac
CS
2675 /*
2676 * If batching fails, we still want to set as many properties as we
2677 * can, so try setting them individually.
2678 */
2679 pair = NULL;
2680 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2681 const char *propname = nvpair_name(pair);
2682 err = 0;
2683
2684 propval = pair;
2685 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2686 nvlist_t *attrs;
2687 attrs = fnvpair_value_nvlist(pair);
2688 propval = fnvlist_lookup_nvpair(attrs, ZPROP_VALUE);
2689 }
2690
2691 if (nvpair_type(propval) == DATA_TYPE_STRING) {
2692 strval = fnvpair_value_string(propval);
2693 err = dsl_prop_set_string(dsname, propname,
2694 source, strval);
2695 } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) {
2696 err = dsl_prop_inherit(dsname, propname, source);
2697 } else {
2698 intval = fnvpair_value_uint64(propval);
2699 err = dsl_prop_set_int(dsname, propname, source,
2700 intval);
2701 }
2702
2703 if (err != 0) {
2704 if (errlist != NULL) {
2705 fnvlist_add_int32(errlist, propname, err);
428870ff 2706 }
2f14adac 2707 rv = err;
428870ff 2708 }
9babb374 2709 }
2f14adac
CS
2710
2711out:
75b4cbf6
AS
2712 if (should_update_mount_cache)
2713 zfs_ioctl_update_mount_cache(dsname);
2714
9babb374 2715 nvlist_free(genericnvl);
428870ff
BB
2716 nvlist_free(retrynvl);
2717
428870ff 2718 return (rv);
9babb374
BB
2719}
2720
2721/*
2722 * Check that all the properties are valid user properties.
2723 */
2724static int
e6203d28 2725zfs_check_userprops(nvlist_t *nvl)
9babb374 2726{
428870ff 2727 nvpair_t *pair = NULL;
9babb374 2728
428870ff
BB
2729 while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2730 const char *propname = nvpair_name(pair);
9babb374
BB
2731
2732 if (!zfs_prop_user(propname) ||
428870ff 2733 nvpair_type(pair) != DATA_TYPE_STRING)
2e528b49 2734 return (SET_ERROR(EINVAL));
9babb374 2735
9babb374 2736 if (strlen(propname) >= ZAP_MAXNAMELEN)
2e528b49 2737 return (SET_ERROR(ENAMETOOLONG));
9babb374 2738
da536844 2739 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2e528b49 2740 return (SET_ERROR(E2BIG));
9babb374 2741 }
34dc7c2f
BB
2742 return (0);
2743}
2744
428870ff
BB
2745static void
2746props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2747{
2748 nvpair_t *pair;
2749
2750 VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2751
2752 pair = NULL;
2753 while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2754 if (nvlist_exists(skipped, nvpair_name(pair)))
2755 continue;
2756
2757 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2758 }
2759}
2760
2761static int
13fe0198 2762clear_received_props(const char *dsname, nvlist_t *props,
428870ff
BB
2763 nvlist_t *skipped)
2764{
2765 int err = 0;
2766 nvlist_t *cleared_props = NULL;
2767 props_skip(props, skipped, &cleared_props);
2768 if (!nvlist_empty(cleared_props)) {
2769 /*
2770 * Acts on local properties until the dataset has received
2771 * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2772 */
2773 zprop_source_t flags = (ZPROP_SRC_NONE |
13fe0198
MA
2774 (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2775 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
428870ff
BB
2776 }
2777 nvlist_free(cleared_props);
2778 return (err);
2779}
2780
34dc7c2f
BB
2781/*
2782 * inputs:
2783 * zc_name name of filesystem
9babb374 2784 * zc_value name of property to set
34dc7c2f 2785 * zc_nvlist_src{_size} nvlist of properties to apply
428870ff 2786 * zc_cookie received properties flag
34dc7c2f 2787 *
428870ff
BB
2788 * outputs:
2789 * zc_nvlist_dst{_size} error for each unapplied received property
34dc7c2f
BB
2790 */
2791static int
2792zfs_ioc_set_prop(zfs_cmd_t *zc)
2793{
2794 nvlist_t *nvl;
428870ff
BB
2795 boolean_t received = zc->zc_cookie;
2796 zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2797 ZPROP_SRC_LOCAL);
6f1ffb06 2798 nvlist_t *errors;
34dc7c2f
BB
2799 int error;
2800
2801 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9babb374 2802 zc->zc_iflags, &nvl)) != 0)
34dc7c2f
BB
2803 return (error);
2804
428870ff 2805 if (received) {
b128c09f 2806 nvlist_t *origprops;
b128c09f 2807
13fe0198
MA
2808 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2809 (void) clear_received_props(zc->zc_name,
2810 origprops, nvl);
2811 nvlist_free(origprops);
428870ff 2812 }
13fe0198
MA
2813
2814 error = dsl_prop_set_hasrecvd(zc->zc_name);
b128c09f
BB
2815 }
2816
6f1ffb06 2817 errors = fnvlist_alloc();
13fe0198
MA
2818 if (error == 0)
2819 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
34dc7c2f 2820
b8864a23 2821 if (zc->zc_nvlist_dst != 0 && errors != NULL) {
428870ff
BB
2822 (void) put_nvlist(zc, errors);
2823 }
2824
2825 nvlist_free(errors);
34dc7c2f
BB
2826 nvlist_free(nvl);
2827 return (error);
2828}
2829
2830/*
2831 * inputs:
2832 * zc_name name of filesystem
2833 * zc_value name of property to inherit
428870ff 2834 * zc_cookie revert to received value if TRUE
34dc7c2f
BB
2835 *
2836 * outputs: none
2837 */
2838static int
2839zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2840{
428870ff
BB
2841 const char *propname = zc->zc_value;
2842 zfs_prop_t prop = zfs_name_to_prop(propname);
2843 boolean_t received = zc->zc_cookie;
2844 zprop_source_t source = (received
2845 ? ZPROP_SRC_NONE /* revert to received value, if any */
2846 : ZPROP_SRC_INHERITED); /* explicitly inherit */
92aceb2a 2847 nvlist_t *dummy;
2848 nvpair_t *pair;
2849 zprop_type_t type;
2850 int err;
428870ff 2851
92aceb2a 2852 if (!received) {
261c013f
BB
2853 /*
2854 * Only check this in the non-received case. We want to allow
2855 * 'inherit -S' to revert non-inheritable properties like quota
2856 * and reservation to the received or default values even though
2857 * they are not considered inheritable.
2858 */
4ff7a8fa 2859 if (prop != ZPROP_USERPROP && !zfs_prop_inheritable(prop))
261c013f 2860 return (SET_ERROR(EINVAL));
959f56b9 2861 }
2862
4ff7a8fa 2863 if (prop == ZPROP_USERPROP) {
92aceb2a 2864 if (!zfs_prop_user(propname))
2865 return (SET_ERROR(EINVAL));
2866
2867 type = PROP_TYPE_STRING;
2868 } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) {
2869 return (SET_ERROR(EINVAL));
2870 } else {
2871 type = zfs_prop_get_type(prop);
2872 }
2873
2874 /*
2875 * zfs_prop_set_special() expects properties in the form of an
2876 * nvpair with type info.
2877 */
2878 dummy = fnvlist_alloc();
2879
2880 switch (type) {
2881 case PROP_TYPE_STRING:
2882 VERIFY(0 == nvlist_add_string(dummy, propname, ""));
2883 break;
2884 case PROP_TYPE_NUMBER:
2885 case PROP_TYPE_INDEX:
2886 VERIFY(0 == nvlist_add_uint64(dummy, propname, 0));
2887 break;
2888 default:
2889 err = SET_ERROR(EINVAL);
2890 goto errout;
2891 }
2892
2893 pair = nvlist_next_nvpair(dummy, NULL);
2894 if (pair == NULL) {
2895 err = SET_ERROR(EINVAL);
2896 } else {
2897 err = zfs_prop_set_special(zc->zc_name, source, pair);
2898 if (err == -1) /* property is not "special", needs handling */
2899 err = dsl_prop_inherit(zc->zc_name, zc->zc_value,
2900 source);
2901 }
2902
2903errout:
2904 nvlist_free(dummy);
2905 return (err);
34dc7c2f
BB
2906}
2907
2908static int
2909zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2910{
2911 nvlist_t *props;
2912 spa_t *spa;
2913 int error;
428870ff 2914 nvpair_t *pair;
34dc7c2f 2915
c65aa5b2
BB
2916 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2917 zc->zc_iflags, &props)))
34dc7c2f
BB
2918 return (error);
2919
d164b209
BB
2920 /*
2921 * If the only property is the configfile, then just do a spa_lookup()
2922 * to handle the faulted case.
2923 */
428870ff
BB
2924 pair = nvlist_next_nvpair(props, NULL);
2925 if (pair != NULL && strcmp(nvpair_name(pair),
d164b209 2926 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
428870ff 2927 nvlist_next_nvpair(props, pair) == NULL) {
d164b209
BB
2928 mutex_enter(&spa_namespace_lock);
2929 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2930 spa_configfile_set(spa, props, B_FALSE);
a1d477c2 2931 spa_write_cachefile(spa, B_FALSE, B_TRUE);
d164b209
BB
2932 }
2933 mutex_exit(&spa_namespace_lock);
428870ff
BB
2934 if (spa != NULL) {
2935 nvlist_free(props);
d164b209 2936 return (0);
428870ff 2937 }
d164b209
BB
2938 }
2939
34dc7c2f
BB
2940 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2941 nvlist_free(props);
2942 return (error);
2943 }
2944
2945 error = spa_prop_set(spa, props);
2946
2947 nvlist_free(props);
2948 spa_close(spa, FTAG);
2949
2950 return (error);
2951}
2952
2953static int
2954zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2955{
2956 spa_t *spa;
2957 int error;
2958 nvlist_t *nvp = NULL;
2959
d164b209
BB
2960 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2961 /*
2962 * If the pool is faulted, there may be properties we can still
2963 * get (such as altroot and cachefile), so attempt to get them
2964 * anyway.
2965 */
2966 mutex_enter(&spa_namespace_lock);
2967 if ((spa = spa_lookup(zc->zc_name)) != NULL)
2968 error = spa_prop_get(spa, &nvp);
2969 mutex_exit(&spa_namespace_lock);
2970 } else {
2971 error = spa_prop_get(spa, &nvp);
2972 spa_close(spa, FTAG);
2973 }
34dc7c2f 2974
b8864a23 2975 if (error == 0 && zc->zc_nvlist_dst != 0)
34dc7c2f
BB
2976 error = put_nvlist(zc, nvp);
2977 else
2e528b49 2978 error = SET_ERROR(EFAULT);
34dc7c2f 2979
d164b209 2980 nvlist_free(nvp);
34dc7c2f
BB
2981 return (error);
2982}
2983
2a673e76
AJ
2984/*
2985 * innvl: {
2986 * "vdevprops_set_vdev" -> guid
2987 * "vdevprops_set_props" -> { prop -> value }
2988 * }
2989 *
2990 * outnvl: propname -> error code (int32)
2991 */
2992static const zfs_ioc_key_t zfs_keys_vdev_set_props[] = {
2993 {ZPOOL_VDEV_PROPS_SET_VDEV, DATA_TYPE_UINT64, 0},
2994 {ZPOOL_VDEV_PROPS_SET_PROPS, DATA_TYPE_NVLIST, 0}
2995};
2996
2997static int
2998zfs_ioc_vdev_set_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
2999{
3000 spa_t *spa;
3001 int error;
3002 vdev_t *vd;
3003 uint64_t vdev_guid;
3004
3005 /* Early validation */
3006 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
3007 &vdev_guid) != 0)
3008 return (SET_ERROR(EINVAL));
3009
3010 if (outnvl == NULL)
3011 return (SET_ERROR(EINVAL));
3012
3013 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3014 return (error);
3015
3016 ASSERT(spa_writeable(spa));
3017
3018 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3019 spa_close(spa, FTAG);
3020 return (SET_ERROR(ENOENT));
3021 }
3022
3023 error = vdev_prop_set(vd, innvl, outnvl);
3024
3025 spa_close(spa, FTAG);
3026
3027 return (error);
3028}
3029
3030/*
3031 * innvl: {
3032 * "vdevprops_get_vdev" -> guid
3033 * (optional) "vdevprops_get_props" -> { propname -> propid }
3034 * }
3035 *
3036 * outnvl: propname -> value
3037 */
3038static const zfs_ioc_key_t zfs_keys_vdev_get_props[] = {
3039 {ZPOOL_VDEV_PROPS_GET_VDEV, DATA_TYPE_UINT64, 0},
3040 {ZPOOL_VDEV_PROPS_GET_PROPS, DATA_TYPE_NVLIST, ZK_OPTIONAL}
3041};
3042
3043static int
3044zfs_ioc_vdev_get_props(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3045{
3046 spa_t *spa;
3047 int error;
3048 vdev_t *vd;
3049 uint64_t vdev_guid;
3050
3051 /* Early validation */
3052 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
3053 &vdev_guid) != 0)
3054 return (SET_ERROR(EINVAL));
3055
3056 if (outnvl == NULL)
3057 return (SET_ERROR(EINVAL));
3058
3059 if ((error = spa_open(poolname, &spa, FTAG)) != 0)
3060 return (error);
3061
3062 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL) {
3063 spa_close(spa, FTAG);
3064 return (SET_ERROR(ENOENT));
3065 }
3066
3067 error = vdev_prop_get(vd, innvl, outnvl);
3068
3069 spa_close(spa, FTAG);
3070
3071 return (error);
3072}
3073
34dc7c2f
BB
3074/*
3075 * inputs:
3076 * zc_name name of filesystem
3077 * zc_nvlist_src{_size} nvlist of delegated permissions
3078 * zc_perm_action allow/unallow flag
3079 *
3080 * outputs: none
3081 */
3082static int
3083zfs_ioc_set_fsacl(zfs_cmd_t *zc)
3084{
3085 int error;
3086 nvlist_t *fsaclnv = NULL;
3087
3088 if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
9babb374 3089 zc->zc_iflags, &fsaclnv)) != 0)
34dc7c2f
BB
3090 return (error);
3091
3092 /*
3093 * Verify nvlist is constructed correctly
3094 */
3095 if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3096 nvlist_free(fsaclnv);
2e528b49 3097 return (SET_ERROR(EINVAL));
34dc7c2f
BB
3098 }
3099
3100 /*
3101 * If we don't have PRIV_SYS_MOUNT, then validate
3102 * that user is allowed to hand out each permission in
3103 * the nvlist(s)
3104 */
3105
3106 error = secpolicy_zfs(CRED());
13fe0198 3107 if (error != 0) {
34dc7c2f
BB
3108 if (zc->zc_perm_action == B_FALSE) {
3109 error = dsl_deleg_can_allow(zc->zc_name,
3110 fsaclnv, CRED());
3111 } else {
3112 error = dsl_deleg_can_unallow(zc->zc_name,
3113 fsaclnv, CRED());
3114 }
3115 }
3116
3117 if (error == 0)
3118 error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action);
3119
3120 nvlist_free(fsaclnv);
3121 return (error);
3122}
3123
3124/*
3125 * inputs:
3126 * zc_name name of filesystem
3127 *
3128 * outputs:
3129 * zc_nvlist_src{_size} nvlist of delegated permissions
3130 */
3131static int
3132zfs_ioc_get_fsacl(zfs_cmd_t *zc)
3133{
3134 nvlist_t *nvp;
3135 int error;
3136
3137 if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) {
3138 error = put_nvlist(zc, nvp);
3139 nvlist_free(nvp);
3140 }
3141
3142 return (error);
3143}
3144
34dc7c2f
BB
3145static void
3146zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3147{
3148 zfs_creat_t *zct = arg;
3149
3150 zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3151}
3152
3153#define ZFS_PROP_UNDEFINED ((uint64_t)-1)
3154
3155/*
3156 * inputs:
b128c09f 3157 * os parent objset pointer (NULL if root fs)
d3cc8b15
WA
3158 * fuids_ok fuids allowed in this version of the spa?
3159 * sa_ok SAs allowed in this version of the spa?
3160 * createprops list of properties requested by creator
34dc7c2f
BB
3161 *
3162 * outputs:
3163 * zplprops values for the zplprops we attach to the master node object
b128c09f 3164 * is_ci true if requested file system will be purely case-insensitive
34dc7c2f
BB
3165 *
3166 * Determine the settings for utf8only, normalization and
3167 * casesensitivity. Specific values may have been requested by the
3168 * creator and/or we can inherit values from the parent dataset. If
3169 * the file system is of too early a vintage, a creator can not
3170 * request settings for these properties, even if the requested
3171 * setting is the default value. We don't actually want to create dsl
3172 * properties for these, so remove them from the source nvlist after
3173 * processing.
3174 */
3175static int
9babb374 3176zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
428870ff
BB
3177 boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops,
3178 nvlist_t *zplprops, boolean_t *is_ci)
34dc7c2f 3179{
34dc7c2f
BB
3180 uint64_t sense = ZFS_PROP_UNDEFINED;
3181 uint64_t norm = ZFS_PROP_UNDEFINED;
3182 uint64_t u8 = ZFS_PROP_UNDEFINED;
b129c659 3183 int error;
34dc7c2f
BB
3184
3185 ASSERT(zplprops != NULL);
3186
d8d418ff 3187 /* parent dataset must be a filesystem */
87a275d9 3188 if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS)
d8d418ff 3189 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
87a275d9 3190
34dc7c2f
BB
3191 /*
3192 * Pull out creator prop choices, if any.
3193 */
3194 if (createprops) {
b128c09f
BB
3195 (void) nvlist_lookup_uint64(createprops,
3196 zfs_prop_to_name(ZFS_PROP_VERSION), &zplver);
34dc7c2f
BB
3197 (void) nvlist_lookup_uint64(createprops,
3198 zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm);
3199 (void) nvlist_remove_all(createprops,
3200 zfs_prop_to_name(ZFS_PROP_NORMALIZE));
3201 (void) nvlist_lookup_uint64(createprops,
3202 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8);
3203 (void) nvlist_remove_all(createprops,
3204 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
3205 (void) nvlist_lookup_uint64(createprops,
3206 zfs_prop_to_name(ZFS_PROP_CASE), &sense);
3207 (void) nvlist_remove_all(createprops,
3208 zfs_prop_to_name(ZFS_PROP_CASE));
3209 }
3210
3211 /*
b128c09f
BB
3212 * If the zpl version requested is whacky or the file system
3213 * or pool is version is too "young" to support normalization
3214 * and the creator tried to set a value for one of the props,
3215 * error out.
34dc7c2f 3216 */
b128c09f
BB
3217 if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3218 (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
428870ff 3219 (zplver >= ZPL_VERSION_SA && !sa_ok) ||
b128c09f 3220 (zplver < ZPL_VERSION_NORMALIZATION &&
34dc7c2f 3221 (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
b128c09f 3222 sense != ZFS_PROP_UNDEFINED)))
2e528b49 3223 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
3224
3225 /*
3226 * Put the version in the zplprops
3227 */
3228 VERIFY(nvlist_add_uint64(zplprops,
3229 zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3230
b129c659
MM
3231 if (norm == ZFS_PROP_UNDEFINED &&
3232 (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0)
3233 return (error);
34dc7c2f
BB
3234 VERIFY(nvlist_add_uint64(zplprops,
3235 zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3236
3237 /*
3238 * If we're normalizing, names must always be valid UTF-8 strings.
3239 */
3240 if (norm)
3241 u8 = 1;
b129c659
MM
3242 if (u8 == ZFS_PROP_UNDEFINED &&
3243 (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0)
3244 return (error);
34dc7c2f
BB
3245 VERIFY(nvlist_add_uint64(zplprops,
3246 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3247
b129c659
MM
3248 if (sense == ZFS_PROP_UNDEFINED &&
3249 (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0)
3250 return (error);
34dc7c2f
BB
3251 VERIFY(nvlist_add_uint64(zplprops,
3252 zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3253
3254 if (is_ci)
3255 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3256
34dc7c2f
BB
3257 return (0);
3258}
3259
b128c09f
BB
3260static int
3261zfs_fill_zplprops(const char *dataset, nvlist_t *createprops,
3262 nvlist_t *zplprops, boolean_t *is_ci)
3263{
428870ff 3264 boolean_t fuids_ok, sa_ok;
b128c09f
BB
3265 uint64_t zplver = ZPL_VERSION;
3266 objset_t *os = NULL;
eca7b760 3267 char parentname[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
3268 spa_t *spa;
3269 uint64_t spa_vers;
b128c09f
BB
3270 int error;
3271
d8d418ff 3272 zfs_get_parent(dataset, parentname, sizeof (parentname));
b128c09f 3273
428870ff
BB
3274 if ((error = spa_open(dataset, &spa, FTAG)) != 0)
3275 return (error);
3276
3277 spa_vers = spa_version(spa);
3278 spa_close(spa, FTAG);
3279
3280 zplver = zfs_zpl_version_map(spa_vers);
3281 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3282 sa_ok = (zplver >= ZPL_VERSION_SA);
b128c09f
BB
3283
3284 /*
3285 * Open parent object set so we can inherit zplprop values.
3286 */
428870ff 3287 if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0)
b128c09f
BB
3288 return (error);
3289
428870ff 3290 error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops,
b128c09f 3291 zplprops, is_ci);
428870ff 3292 dmu_objset_rele(os, FTAG);
b128c09f
BB
3293 return (error);
3294}
3295
3296static int
3297zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops,
3298 nvlist_t *zplprops, boolean_t *is_ci)
3299{
428870ff
BB
3300 boolean_t fuids_ok;
3301 boolean_t sa_ok;
b128c09f
BB
3302 uint64_t zplver = ZPL_VERSION;
3303 int error;
3304
428870ff
BB
3305 zplver = zfs_zpl_version_map(spa_vers);
3306 fuids_ok = (zplver >= ZPL_VERSION_FUID);
3307 sa_ok = (zplver >= ZPL_VERSION_SA);
b128c09f 3308
428870ff
BB
3309 error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok,
3310 createprops, zplprops, is_ci);
b128c09f
BB
3311 return (error);
3312}
3313
34dc7c2f 3314/*
6f1ffb06
MA
3315 * innvl: {
3316 * "type" -> dmu_objset_type_t (int32)
3317 * (optional) "props" -> { prop -> value }
b5256303
TC
3318 * (optional) "hidden_args" -> { "wkeydata" -> value }
3319 * raw uint8_t array of encryption wrapping key data (32 bytes)
6f1ffb06 3320 * }
34dc7c2f 3321 *
6f1ffb06 3322 * outnvl: propname -> error code (int32)
34dc7c2f 3323 */
b83a0e2d
DB
3324
3325static const zfs_ioc_key_t zfs_keys_create[] = {
3326 {"type", DATA_TYPE_INT32, 0},
3327 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3328 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3329};
3330
34dc7c2f 3331static int
6f1ffb06 3332zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3333{
34dc7c2f 3334 int error = 0;
6f1ffb06 3335 zfs_creat_t zct = { 0 };
34dc7c2f 3336 nvlist_t *nvprops = NULL;
b5256303 3337 nvlist_t *hidden_args = NULL;
34dc7c2f 3338 void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
6f1ffb06
MA
3339 dmu_objset_type_t type;
3340 boolean_t is_insensitive = B_FALSE;
b5256303 3341 dsl_crypto_params_t *dcp = NULL;
34dc7c2f 3342
b83a0e2d 3343 type = (dmu_objset_type_t)fnvlist_lookup_int32(innvl, "type");
6f1ffb06 3344 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
b5256303 3345 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
34dc7c2f 3346
6f1ffb06 3347 switch (type) {
34dc7c2f
BB
3348 case DMU_OST_ZFS:
3349 cbfunc = zfs_create_cb;
3350 break;
3351
3352 case DMU_OST_ZVOL:
3353 cbfunc = zvol_create_cb;
3354 break;
3355
3356 default:
3357 cbfunc = NULL;
3358 break;
3359 }
6f1ffb06
MA
3360 if (strchr(fsname, '@') ||
3361 strchr(fsname, '%'))
2e528b49 3362 return (SET_ERROR(EINVAL));
34dc7c2f 3363
34dc7c2f
BB
3364 zct.zct_props = nvprops;
3365
6f1ffb06 3366 if (cbfunc == NULL)
2e528b49 3367 return (SET_ERROR(EINVAL));
34dc7c2f 3368
6f1ffb06
MA
3369 if (type == DMU_OST_ZVOL) {
3370 uint64_t volsize, volblocksize;
34dc7c2f 3371
6f1ffb06 3372 if (nvprops == NULL)
2e528b49 3373 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3374 if (nvlist_lookup_uint64(nvprops,
3375 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
2e528b49 3376 return (SET_ERROR(EINVAL));
34dc7c2f 3377
6f1ffb06
MA
3378 if ((error = nvlist_lookup_uint64(nvprops,
3379 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3380 &volblocksize)) != 0 && error != ENOENT)
2e528b49 3381 return (SET_ERROR(EINVAL));
34dc7c2f 3382
6f1ffb06
MA
3383 if (error != 0)
3384 volblocksize = zfs_prop_default_numeric(
3385 ZFS_PROP_VOLBLOCKSIZE);
34dc7c2f 3386
4cb7b9c5 3387 if ((error = zvol_check_volblocksize(fsname,
6f1ffb06
MA
3388 volblocksize)) != 0 ||
3389 (error = zvol_check_volsize(volsize,
3390 volblocksize)) != 0)
3391 return (error);
3392 } else if (type == DMU_OST_ZFS) {
3393 int error;
34dc7c2f 3394
6f1ffb06
MA
3395 /*
3396 * We have to have normalization and
3397 * case-folding flags correct when we do the
3398 * file system creation, so go figure them out
3399 * now.
3400 */
3401 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3402 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3403 error = zfs_fill_zplprops(fsname, nvprops,
3404 zct.zct_zplprops, &is_insensitive);
3405 if (error != 0) {
3406 nvlist_free(zct.zct_zplprops);
3407 return (error);
34dc7c2f 3408 }
34dc7c2f
BB
3409 }
3410
b5256303
TC
3411 error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops,
3412 hidden_args, &dcp);
3413 if (error != 0) {
3414 nvlist_free(zct.zct_zplprops);
3415 return (error);
3416 }
3417
6f1ffb06 3418 error = dmu_objset_create(fsname, type,
b5256303
TC
3419 is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct);
3420
6f1ffb06 3421 nvlist_free(zct.zct_zplprops);
b5256303 3422 dsl_crypto_params_free(dcp, !!error);
6f1ffb06 3423
34dc7c2f
BB
3424 /*
3425 * It would be nice to do this atomically.
3426 */
3427 if (error == 0) {
6f1ffb06
MA
3428 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3429 nvprops, outnvl);
f74b821a
BB
3430 if (error != 0) {
3431 spa_t *spa;
3432 int error2;
3433
3434 /*
3435 * Volumes will return EBUSY and cannot be destroyed
ec213971
MA
3436 * until all asynchronous minor handling (e.g. from
3437 * setting the volmode property) has completed. Wait for
3438 * the spa_zvol_taskq to drain then retry.
f74b821a
BB
3439 */
3440 error2 = dsl_destroy_head(fsname);
3441 while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) {
3442 error2 = spa_open(fsname, &spa, FTAG);
3443 if (error2 == 0) {
3444 taskq_wait(spa->spa_zvol_taskq);
3445 spa_close(spa, FTAG);
3446 }
3447 error2 = dsl_destroy_head(fsname);
3448 }
3449 }
34dc7c2f 3450 }
34dc7c2f
BB
3451 return (error);
3452}
3453
3454/*
6f1ffb06
MA
3455 * innvl: {
3456 * "origin" -> name of origin snapshot
3457 * (optional) "props" -> { prop -> value }
b5256303
TC
3458 * (optional) "hidden_args" -> { "wkeydata" -> value }
3459 * raw uint8_t array of encryption wrapping key data (32 bytes)
6f1ffb06 3460 * }
34dc7c2f 3461 *
428870ff 3462 * outputs:
6f1ffb06 3463 * outnvl: propname -> error code (int32)
34dc7c2f 3464 */
b83a0e2d
DB
3465static const zfs_ioc_key_t zfs_keys_clone[] = {
3466 {"origin", DATA_TYPE_STRING, 0},
3467 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3468 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3469};
3470
34dc7c2f 3471static int
6f1ffb06 3472zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3473{
6f1ffb06 3474 int error = 0;
b128c09f 3475 nvlist_t *nvprops = NULL;
4d55ea81 3476 const char *origin_name;
b128c09f 3477
b83a0e2d 3478 origin_name = fnvlist_lookup_string(innvl, "origin");
6f1ffb06 3479 (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
b128c09f 3480
6f1ffb06
MA
3481 if (strchr(fsname, '@') ||
3482 strchr(fsname, '%'))
2e528b49 3483 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3484
3485 if (dataset_namecheck(origin_name, NULL, NULL) != 0)
2e528b49 3486 return (SET_ERROR(EINVAL));
b5256303 3487
13fe0198 3488 error = dmu_objset_clone(fsname, origin_name);
b128c09f 3489
6f1ffb06
MA
3490 /*
3491 * It would be nice to do this atomically.
3492 */
3493 if (error == 0) {
3494 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3495 nvprops, outnvl);
3496 if (error != 0)
13fe0198 3497 (void) dsl_destroy_head(fsname);
b128c09f 3498 }
6f1ffb06
MA
3499 return (error);
3500}
9babb374 3501
b83a0e2d
DB
3502static const zfs_ioc_key_t zfs_keys_remap[] = {
3503 /* no nvl keys */
3504};
3505
a1d477c2
MA
3506static int
3507zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3508{
59ec30a3 3509 /* This IOCTL is no longer supported. */
ef70eff1 3510 (void) fsname, (void) innvl, (void) outnvl;
59ec30a3 3511 return (0);
a1d477c2
MA
3512}
3513
6f1ffb06
MA
3514/*
3515 * innvl: {
3516 * "snaps" -> { snapshot1, snapshot2 }
3517 * (optional) "props" -> { prop -> value (string) }
3518 * }
3519 *
3520 * outnvl: snapshot -> error code (int32)
6f1ffb06 3521 */
b83a0e2d
DB
3522static const zfs_ioc_key_t zfs_keys_snapshot[] = {
3523 {"snaps", DATA_TYPE_NVLIST, 0},
3524 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
3525};
3526
6f1ffb06
MA
3527static int
3528zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3529{
3530 nvlist_t *snaps;
3531 nvlist_t *props = NULL;
3532 int error, poollen;
1c27024e 3533 nvpair_t *pair;
9babb374 3534
6f1ffb06 3535 (void) nvlist_lookup_nvlist(innvl, "props", &props);
6f1ffb06
MA
3536 if (!nvlist_empty(props) &&
3537 zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
2e528b49 3538 return (SET_ERROR(ENOTSUP));
e6203d28
AG
3539 if ((error = zfs_check_userprops(props)) != 0)
3540 return (error);
6f1ffb06 3541
b83a0e2d 3542 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
6f1ffb06
MA
3543 poollen = strlen(poolname);
3544 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3545 pair = nvlist_next_nvpair(snaps, pair)) {
3546 const char *name = nvpair_name(pair);
e6203d28 3547 char *cp = strchr(name, '@');
6f1ffb06
MA
3548
3549 /*
3550 * The snap name must contain an @, and the part after it must
3551 * contain only valid characters.
3552 */
da536844
MA
3553 if (cp == NULL ||
3554 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
2e528b49 3555 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3556
3557 /*
3558 * The snap must be in the specified pool.
3559 */
3560 if (strncmp(name, poolname, poollen) != 0 ||
3561 (name[poollen] != '/' && name[poollen] != '@'))
2e528b49 3562 return (SET_ERROR(EXDEV));
6f1ffb06 3563
e6203d28
AG
3564 /*
3565 * Check for permission to set the properties on the fs.
3566 */
3567 if (!nvlist_empty(props)) {
3568 *cp = '\0';
3569 error = zfs_secpolicy_write_perms(name,
3570 ZFS_DELEG_PERM_USERPROP, CRED());
3571 *cp = '@';
3572 if (error != 0)
3573 return (error);
3574 }
3575
6f1ffb06 3576 /* This must be the only snap of this fs. */
1c27024e 3577 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
6f1ffb06
MA
3578 pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3579 if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3580 == 0) {
2e528b49 3581 return (SET_ERROR(EXDEV));
6f1ffb06
MA
3582 }
3583 }
3584 }
3585
13fe0198 3586 error = dsl_dataset_snapshot(snaps, props, outnvl);
ba6a2402 3587
6f1ffb06
MA
3588 return (error);
3589}
3590
3591/*
3592 * innvl: "message" -> string
3593 */
b83a0e2d
DB
3594static const zfs_ioc_key_t zfs_keys_log_history[] = {
3595 {"message", DATA_TYPE_STRING, 0},
3596};
3597
6f1ffb06
MA
3598static int
3599zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3600{
ef70eff1 3601 (void) unused, (void) outnvl;
4d55ea81
RM
3602 const char *message;
3603 char *poolname;
6f1ffb06
MA
3604 spa_t *spa;
3605 int error;
6f1ffb06
MA
3606
3607 /*
3608 * The poolname in the ioctl is not set, we get it from the TSD,
3609 * which was set at the end of the last successful ioctl that allows
3610 * logging. The secpolicy func already checked that it is set.
3611 * Only one log ioctl is allowed after each successful ioctl, so
3612 * we clear the TSD here.
3613 */
3614 poolname = tsd_get(zfs_allow_log_key);
9f3d1407 3615 if (poolname == NULL)
3616 return (SET_ERROR(EINVAL));
6f1ffb06
MA
3617 (void) tsd_set(zfs_allow_log_key, NULL);
3618 error = spa_open(poolname, &spa, FTAG);
e4f5fa12 3619 kmem_strfree(poolname);
6f1ffb06
MA
3620 if (error != 0)
3621 return (error);
3622
b83a0e2d 3623 message = fnvlist_lookup_string(innvl, "message");
6f1ffb06
MA
3624
3625 if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) {
3626 spa_close(spa, FTAG);
2e528b49 3627 return (SET_ERROR(ENOTSUP));
6f1ffb06
MA
3628 }
3629
3630 error = spa_history_log(spa, message);
3631 spa_close(spa, FTAG);
b128c09f 3632 return (error);
34dc7c2f
BB
3633}
3634
108a454a
PD
3635/*
3636 * This ioctl is used to set the bootenv configuration on the current
3637 * pool. This configuration is stored in the second padding area of the label,
1db9e6e4
TS
3638 * and it is used by the bootloader(s) to store the bootloader and/or system
3639 * specific data.
3640 * The data is stored as nvlist data stream, and is protected by
3641 * an embedded checksum.
3642 * The version can have two possible values:
3643 * VB_RAW: nvlist should have key GRUB_ENVMAP, value DATA_TYPE_STRING.
3644 * VB_NVLIST: nvlist with arbitrary <key, value> pairs.
108a454a 3645 */
108a454a 3646static const zfs_ioc_key_t zfs_keys_set_bootenv[] = {
1db9e6e4
TS
3647 {"version", DATA_TYPE_UINT64, 0},
3648 {"<keys>", DATA_TYPE_ANY, ZK_OPTIONAL | ZK_WILDCARDLIST},
108a454a
PD
3649};
3650
3651static int
3652zfs_ioc_set_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3653{
108a454a
PD
3654 int error;
3655 spa_t *spa;
3656
108a454a
PD
3657 if ((error = spa_open(name, &spa, FTAG)) != 0)
3658 return (error);
3659 spa_vdev_state_enter(spa, SCL_ALL);
1db9e6e4 3660 error = vdev_label_write_bootenv(spa->spa_root_vdev, innvl);
108a454a
PD
3661 (void) spa_vdev_state_exit(spa, NULL, 0);
3662 spa_close(spa, FTAG);
3663 return (error);
3664}
3665
3666static const zfs_ioc_key_t zfs_keys_get_bootenv[] = {
3667 /* no nvl keys */
3668};
3669
108a454a
PD
3670static int
3671zfs_ioc_get_bootenv(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
3672{
3673 spa_t *spa;
3674 int error;
3675
3676 if ((error = spa_open(name, &spa, FTAG)) != 0)
3677 return (error);
3678 spa_vdev_state_enter(spa, SCL_ALL);
3679 error = vdev_label_read_bootenv(spa->spa_root_vdev, outnvl);
3680 (void) spa_vdev_state_exit(spa, NULL, 0);
3681 spa_close(spa, FTAG);
3682 return (error);
3683}
3684
ebe7e575 3685/*
13fe0198
MA
3686 * The dp_config_rwlock must not be held when calling this, because the
3687 * unmount may need to write out data.
3688 *
3689 * This function is best-effort. Callers must deal gracefully if it
3690 * remains mounted (or is remounted after this call).
d09f25dc 3691 *
278bee93
BB
3692 * Returns 0 if the argument is not a snapshot, or it is not currently a
3693 * filesystem, or we were able to unmount it. Returns error code otherwise.
ebe7e575 3694 */
8d103d88 3695void
13fe0198 3696zfs_unmount_snap(const char *snapname)
34dc7c2f 3697{
278bee93 3698 if (strchr(snapname, '@') == NULL)
8d103d88 3699 return;
ebe7e575 3700
4d55ea81 3701 (void) zfsctl_snapshot_unmount(snapname, MNT_FORCE);
13fe0198
MA
3702}
3703
13fe0198
MA
3704static int
3705zfs_unmount_snap_cb(const char *snapname, void *arg)
3706{
ef70eff1 3707 (void) arg;
8d103d88
SD
3708 zfs_unmount_snap(snapname);
3709 return (0);
13fe0198
MA
3710}
3711
3712/*
3713 * When a clone is destroyed, its origin may also need to be destroyed,
3714 * in which case it must be unmounted. This routine will do that unmount
3715 * if necessary.
3716 */
3717void
3718zfs_destroy_unmount_origin(const char *fsname)
3719{
3720 int error;
3721 objset_t *os;
3722 dsl_dataset_t *ds;
3723
3724 error = dmu_objset_hold(fsname, FTAG, &os);
3725 if (error != 0)
3726 return;
3727 ds = dmu_objset_ds(os);
3728 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
eca7b760 3729 char originname[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
3730 dsl_dataset_name(ds->ds_prev, originname);
3731 dmu_objset_rele(os, FTAG);
8d103d88 3732 zfs_unmount_snap(originname);
13fe0198
MA
3733 } else {
3734 dmu_objset_rele(os, FTAG);
3735 }
34dc7c2f
BB
3736}
3737
3738/*
6f1ffb06
MA
3739 * innvl: {
3740 * "snaps" -> { snapshot1, snapshot2 }
3741 * (optional boolean) "defer"
3742 * }
34dc7c2f 3743 *
6f1ffb06 3744 * outnvl: snapshot -> error code (int32)
34dc7c2f 3745 */
b83a0e2d
DB
3746static const zfs_ioc_key_t zfs_keys_destroy_snaps[] = {
3747 {"snaps", DATA_TYPE_NVLIST, 0},
7bb0c294 3748 {"defer", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
b83a0e2d
DB
3749};
3750
34dc7c2f 3751static int
6f1ffb06 3752zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 3753{
7bb0c294 3754 int poollen;
6f1ffb06 3755 nvlist_t *snaps;
330d06f9 3756 nvpair_t *pair;
6f1ffb06 3757 boolean_t defer;
7bb0c294 3758 spa_t *spa;
34dc7c2f 3759
b83a0e2d 3760 snaps = fnvlist_lookup_nvlist(innvl, "snaps");
6f1ffb06 3761 defer = nvlist_exists(innvl, "defer");
330d06f9 3762
7bb0c294 3763 poollen = strlen(poolname);
6f1ffb06
MA
3764 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3765 pair = nvlist_next_nvpair(snaps, pair)) {
7bb0c294
MM
3766 const char *name = nvpair_name(pair);
3767
3768 /*
3769 * The snap must be in the specified pool to prevent the
3770 * invalid removal of zvol minors below.
3771 */
3772 if (strncmp(name, poolname, poollen) != 0 ||
3773 (name[poollen] != '/' && name[poollen] != '@'))
3774 return (SET_ERROR(EXDEV));
3775
8d103d88 3776 zfs_unmount_snap(nvpair_name(pair));
7bb0c294
MM
3777 if (spa_open(name, &spa, FTAG) == 0) {
3778 zvol_remove_minors(spa, name, B_TRUE);
3779 spa_close(spa, FTAG);
3780 }
da536844
MA
3781 }
3782
3783 return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3784}
3785
3786/*
a73f361f
CS
3787 * Create bookmarks. The bookmark names are of the form <fs>#<bmark>.
3788 * All bookmarks and snapshots must be in the same pool.
3789 * dsl_bookmark_create_nvl_validate describes the nvlist schema in more detail.
da536844
MA
3790 *
3791 * innvl: {
a73f361f
CS
3792 * new_bookmark1 -> existing_snapshot,
3793 * new_bookmark2 -> existing_bookmark,
da536844
MA
3794 * }
3795 *
3796 * outnvl: bookmark -> error code (int32)
3797 *
3798 */
b83a0e2d
DB
3799static const zfs_ioc_key_t zfs_keys_bookmark[] = {
3800 {"<bookmark>...", DATA_TYPE_STRING, ZK_WILDCARDLIST},
3801};
3802
da536844
MA
3803static int
3804zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3805{
ef70eff1 3806 (void) poolname;
da536844
MA
3807 return (dsl_bookmark_create(innvl, outnvl));
3808}
3809
3810/*
3811 * innvl: {
3812 * property 1, property 2, ...
3813 * }
3814 *
3815 * outnvl: {
3816 * bookmark name 1 -> { property 1, property 2, ... },
3817 * bookmark name 2 -> { property 1, property 2, ... }
3818 * }
3819 *
3820 */
b83a0e2d
DB
3821static const zfs_ioc_key_t zfs_keys_get_bookmarks[] = {
3822 {"<property>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST | ZK_OPTIONAL},
3823};
3824
da536844
MA
3825static int
3826zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3827{
3828 return (dsl_get_bookmarks(fsname, innvl, outnvl));
3829}
3830
30af21b0
PD
3831/*
3832 * innvl is not used.
3833 *
3834 * outnvl: {
3835 * property 1, property 2, ...
3836 * }
3837 *
3838 */
3839static const zfs_ioc_key_t zfs_keys_get_bookmark_props[] = {
3840 /* no nvl keys */
3841};
3842
30af21b0
PD
3843static int
3844zfs_ioc_get_bookmark_props(const char *bookmark, nvlist_t *innvl,
3845 nvlist_t *outnvl)
3846{
ef70eff1 3847 (void) innvl;
30af21b0
PD
3848 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3849 char *bmname;
3850
3851 bmname = strchr(bookmark, '#');
3852 if (bmname == NULL)
3853 return (SET_ERROR(EINVAL));
3854 bmname++;
3855
3856 (void) strlcpy(fsname, bookmark, sizeof (fsname));
3857 *(strchr(fsname, '#')) = '\0';
3858
3859 return (dsl_get_bookmark_props(fsname, bmname, outnvl));
3860}
3861
da536844
MA
3862/*
3863 * innvl: {
3864 * bookmark name 1, bookmark name 2
3865 * }
3866 *
3867 * outnvl: bookmark -> error code (int32)
3868 *
3869 */
b83a0e2d
DB
3870static const zfs_ioc_key_t zfs_keys_destroy_bookmarks[] = {
3871 {"<bookmark>...", DATA_TYPE_BOOLEAN, ZK_WILDCARDLIST},
3872};
3873
da536844
MA
3874static int
3875zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl,
3876 nvlist_t *outnvl)
3877{
3878 int error, poollen;
da536844
MA
3879
3880 poollen = strlen(poolname);
1c27024e 3881 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
da536844 3882 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
330d06f9 3883 const char *name = nvpair_name(pair);
da536844 3884 const char *cp = strchr(name, '#');
6f1ffb06 3885
330d06f9 3886 /*
da536844
MA
3887 * The bookmark name must contain an #, and the part after it
3888 * must contain only valid characters.
3889 */
3890 if (cp == NULL ||
3891 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3892 return (SET_ERROR(EINVAL));
3893
3894 /*
3895 * The bookmark must be in the specified pool.
330d06f9 3896 */
6f1ffb06 3897 if (strncmp(name, poolname, poollen) != 0 ||
da536844 3898 (name[poollen] != '/' && name[poollen] != '#'))
2e528b49 3899 return (SET_ERROR(EXDEV));
330d06f9
MA
3900 }
3901
da536844
MA
3902 error = dsl_bookmark_destroy(innvl, outnvl);
3903 return (error);
34dc7c2f
BB
3904}
3905
b83a0e2d
DB
3906static const zfs_ioc_key_t zfs_keys_channel_program[] = {
3907 {"program", DATA_TYPE_STRING, 0},
3908 {"arg", DATA_TYPE_ANY, 0},
3909 {"sync", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
3910 {"instrlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3911 {"memlimit", DATA_TYPE_UINT64, ZK_OPTIONAL},
3912};
3913
d99a0153
CW
3914static int
3915zfs_ioc_channel_program(const char *poolname, nvlist_t *innvl,
3916 nvlist_t *outnvl)
3917{
3918 char *program;
3919 uint64_t instrlimit, memlimit;
5b72a38d 3920 boolean_t sync_flag;
d99a0153
CW
3921 nvpair_t *nvarg = NULL;
3922
b83a0e2d 3923 program = fnvlist_lookup_string(innvl, ZCP_ARG_PROGRAM);
5b72a38d
SD
3924 if (0 != nvlist_lookup_boolean_value(innvl, ZCP_ARG_SYNC, &sync_flag)) {
3925 sync_flag = B_TRUE;
3926 }
d99a0153
CW
3927 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_INSTRLIMIT, &instrlimit)) {
3928 instrlimit = ZCP_DEFAULT_INSTRLIMIT;
3929 }
3930 if (0 != nvlist_lookup_uint64(innvl, ZCP_ARG_MEMLIMIT, &memlimit)) {
3931 memlimit = ZCP_DEFAULT_MEMLIMIT;
3932 }
b83a0e2d 3933 nvarg = fnvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST);
d99a0153
CW
3934
3935 if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
7bb0c294 3936 return (SET_ERROR(EINVAL));
234c91c5 3937 if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
7bb0c294 3938 return (SET_ERROR(EINVAL));
d99a0153 3939
5b72a38d 3940 return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
d99a0153
CW
3941 nvarg, outnvl));
3942}
3943
d2734cce
SD
3944/*
3945 * innvl: unused
3946 * outnvl: empty
3947 */
b83a0e2d
DB
3948static const zfs_ioc_key_t zfs_keys_pool_checkpoint[] = {
3949 /* no nvl keys */
3950};
3951
d2734cce
SD
3952static int
3953zfs_ioc_pool_checkpoint(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3954{
ef70eff1 3955 (void) innvl, (void) outnvl;
d2734cce
SD
3956 return (spa_checkpoint(poolname));
3957}
3958
3959/*
3960 * innvl: unused
3961 * outnvl: empty
3962 */
b83a0e2d
DB
3963static const zfs_ioc_key_t zfs_keys_pool_discard_checkpoint[] = {
3964 /* no nvl keys */
3965};
3966
d2734cce
SD
3967static int
3968zfs_ioc_pool_discard_checkpoint(const char *poolname, nvlist_t *innvl,
3969 nvlist_t *outnvl)
3970{
ef70eff1 3971 (void) innvl, (void) outnvl;
d2734cce
SD
3972 return (spa_checkpoint_discard(poolname));
3973}
3974
34dc7c2f
BB
3975/*
3976 * inputs:
3977 * zc_name name of dataset to destroy
45d1cae3 3978 * zc_defer_destroy mark for deferred destroy
34dc7c2f
BB
3979 *
3980 * outputs: none
3981 */
3982static int
3983zfs_ioc_destroy(zfs_cmd_t *zc)
3984{
dc1c630b
AG
3985 objset_t *os;
3986 dmu_objset_type_t ost;
428870ff 3987 int err;
d09f25dc 3988
dc1c630b
AG
3989 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
3990 if (err != 0)
3991 return (err);
3992 ost = dmu_objset_type(os);
3993 dmu_objset_rele(os, FTAG);
3994
3995 if (ost == DMU_OST_ZFS)
8d103d88 3996 zfs_unmount_snap(zc->zc_name);
34dc7c2f 3997
1b87e0f5 3998 if (strchr(zc->zc_name, '@')) {
13fe0198 3999 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
1b87e0f5 4000 } else {
13fe0198 4001 err = dsl_destroy_head(zc->zc_name);
1b87e0f5
RS
4002 if (err == EEXIST) {
4003 /*
4004 * It is possible that the given DS may have
4005 * hidden child (%recv) datasets - "leftovers"
4006 * resulting from the previously interrupted
4007 * 'zfs receive'.
4008 *
4009 * 6 extra bytes for /%recv
4010 */
4011 char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6];
4012
682ce104
TH
4013 if (snprintf(namebuf, sizeof (namebuf), "%s/%s",
4014 zc->zc_name, recv_clone_name) >=
4015 sizeof (namebuf))
4016 return (SET_ERROR(EINVAL));
1b87e0f5
RS
4017
4018 /*
4019 * Try to remove the hidden child (%recv) and after
4020 * that try to remove the target dataset.
4021 * If the hidden child (%recv) does not exist
4022 * the original error (EEXIST) will be returned
4023 */
4024 err = dsl_destroy_head(namebuf);
4025 if (err == 0)
4026 err = dsl_destroy_head(zc->zc_name);
4027 else if (err == ENOENT)
ecb2b7dc 4028 err = SET_ERROR(EEXIST);
1b87e0f5
RS
4029 }
4030 }
a0bd735a 4031
428870ff 4032 return (err);
34dc7c2f
BB
4033}
4034
619f0976
GW
4035/*
4036 * innvl: {
1b939560 4037 * "initialize_command" -> POOL_INITIALIZE_{CANCEL|START|SUSPEND} (uint64)
c10d37dd
GW
4038 * "initialize_vdevs": { -> guids to initialize (nvlist)
4039 * "vdev_path_1": vdev_guid_1, (uint64),
4040 * "vdev_path_2": vdev_guid_2, (uint64),
4041 * ...
619f0976 4042 * },
619f0976
GW
4043 * }
4044 *
4045 * outnvl: {
c10d37dd
GW
4046 * "initialize_vdevs": { -> initialization errors (nvlist)
4047 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4048 * "vdev_path_2": errno, ... (uint64)
619f0976 4049 * ...
c10d37dd 4050 * }
619f0976
GW
4051 * }
4052 *
c10d37dd
GW
4053 * EINVAL is returned for an unknown commands or if any of the provided vdev
4054 * guids have be specified with a type other than uint64.
619f0976
GW
4055 */
4056static const zfs_ioc_key_t zfs_keys_pool_initialize[] = {
c10d37dd 4057 {ZPOOL_INITIALIZE_COMMAND, DATA_TYPE_UINT64, 0},
619f0976
GW
4058 {ZPOOL_INITIALIZE_VDEVS, DATA_TYPE_NVLIST, 0}
4059};
4060
4061static int
4062zfs_ioc_pool_initialize(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4063{
619f0976
GW
4064 uint64_t cmd_type;
4065 if (nvlist_lookup_uint64(innvl, ZPOOL_INITIALIZE_COMMAND,
4066 &cmd_type) != 0) {
619f0976
GW
4067 return (SET_ERROR(EINVAL));
4068 }
c10d37dd 4069
619f0976 4070 if (!(cmd_type == POOL_INITIALIZE_CANCEL ||
1b939560 4071 cmd_type == POOL_INITIALIZE_START ||
619f0976 4072 cmd_type == POOL_INITIALIZE_SUSPEND)) {
619f0976
GW
4073 return (SET_ERROR(EINVAL));
4074 }
4075
4076 nvlist_t *vdev_guids;
4077 if (nvlist_lookup_nvlist(innvl, ZPOOL_INITIALIZE_VDEVS,
4078 &vdev_guids) != 0) {
619f0976
GW
4079 return (SET_ERROR(EINVAL));
4080 }
4081
619f0976
GW
4082 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4083 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
c10d37dd
GW
4084 uint64_t vdev_guid;
4085 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4086 return (SET_ERROR(EINVAL));
619f0976
GW
4087 }
4088 }
c10d37dd
GW
4089
4090 spa_t *spa;
4091 int error = spa_open(poolname, &spa, FTAG);
4092 if (error != 0)
4093 return (error);
4094
4095 nvlist_t *vdev_errlist = fnvlist_alloc();
4096 int total_errors = spa_vdev_initialize(spa, vdev_guids, cmd_type,
4097 vdev_errlist);
4098
619f0976
GW
4099 if (fnvlist_size(vdev_errlist) > 0) {
4100 fnvlist_add_nvlist(outnvl, ZPOOL_INITIALIZE_VDEVS,
4101 vdev_errlist);
4102 }
4103 fnvlist_free(vdev_errlist);
4104
4105 spa_close(spa, FTAG);
51568629 4106 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
619f0976
GW
4107}
4108
1b939560
BB
4109/*
4110 * innvl: {
4111 * "trim_command" -> POOL_TRIM_{CANCEL|START|SUSPEND} (uint64)
4112 * "trim_vdevs": { -> guids to TRIM (nvlist)
4113 * "vdev_path_1": vdev_guid_1, (uint64),
4114 * "vdev_path_2": vdev_guid_2, (uint64),
4115 * ...
4116 * },
4117 * "trim_rate" -> Target TRIM rate in bytes/sec.
4118 * "trim_secure" -> Set to request a secure TRIM.
4119 * }
4120 *
4121 * outnvl: {
4122 * "trim_vdevs": { -> TRIM errors (nvlist)
4123 * "vdev_path_1": errno, see function body for possible errnos (uint64)
4124 * "vdev_path_2": errno, ... (uint64)
4125 * ...
4126 * }
4127 * }
4128 *
4129 * EINVAL is returned for an unknown commands or if any of the provided vdev
4130 * guids have be specified with a type other than uint64.
4131 */
4132static const zfs_ioc_key_t zfs_keys_pool_trim[] = {
4133 {ZPOOL_TRIM_COMMAND, DATA_TYPE_UINT64, 0},
4134 {ZPOOL_TRIM_VDEVS, DATA_TYPE_NVLIST, 0},
4135 {ZPOOL_TRIM_RATE, DATA_TYPE_UINT64, ZK_OPTIONAL},
4136 {ZPOOL_TRIM_SECURE, DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
4137};
4138
4139static int
4140zfs_ioc_pool_trim(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4141{
4142 uint64_t cmd_type;
4143 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_COMMAND, &cmd_type) != 0)
4144 return (SET_ERROR(EINVAL));
4145
4146 if (!(cmd_type == POOL_TRIM_CANCEL ||
4147 cmd_type == POOL_TRIM_START ||
4148 cmd_type == POOL_TRIM_SUSPEND)) {
4149 return (SET_ERROR(EINVAL));
4150 }
4151
4152 nvlist_t *vdev_guids;
4153 if (nvlist_lookup_nvlist(innvl, ZPOOL_TRIM_VDEVS, &vdev_guids) != 0)
4154 return (SET_ERROR(EINVAL));
4155
4156 for (nvpair_t *pair = nvlist_next_nvpair(vdev_guids, NULL);
4157 pair != NULL; pair = nvlist_next_nvpair(vdev_guids, pair)) {
4158 uint64_t vdev_guid;
4159 if (nvpair_value_uint64(pair, &vdev_guid) != 0) {
4160 return (SET_ERROR(EINVAL));
4161 }
4162 }
4163
4164 /* Optional, defaults to maximum rate when not provided */
4165 uint64_t rate;
4166 if (nvlist_lookup_uint64(innvl, ZPOOL_TRIM_RATE, &rate) != 0)
4167 rate = 0;
4168
4169 /* Optional, defaults to standard TRIM when not provided */
4170 boolean_t secure;
4171 if (nvlist_lookup_boolean_value(innvl, ZPOOL_TRIM_SECURE,
4172 &secure) != 0) {
4173 secure = B_FALSE;
4174 }
4175
4176 spa_t *spa;
4177 int error = spa_open(poolname, &spa, FTAG);
4178 if (error != 0)
4179 return (error);
4180
4181 nvlist_t *vdev_errlist = fnvlist_alloc();
4182 int total_errors = spa_vdev_trim(spa, vdev_guids, cmd_type,
4183 rate, !!zfs_trim_metaslab_skip, secure, vdev_errlist);
4184
4185 if (fnvlist_size(vdev_errlist) > 0)
4186 fnvlist_add_nvlist(outnvl, ZPOOL_TRIM_VDEVS, vdev_errlist);
4187
4188 fnvlist_free(vdev_errlist);
4189
4190 spa_close(spa, FTAG);
51568629 4191 return (total_errors > 0 ? SET_ERROR(EINVAL) : 0);
1b939560
BB
4192}
4193
e60e158e
JG
4194/*
4195 * This ioctl waits for activity of a particular type to complete. If there is
4196 * no activity of that type in progress, it returns immediately, and the
4197 * returned value "waited" is false. If there is activity in progress, and no
4198 * tag is passed in, the ioctl blocks until all activity of that type is
4199 * complete, and then returns with "waited" set to true.
4200 *
4201 * If a tag is provided, it identifies a particular instance of an activity to
4202 * wait for. Currently, this is only valid for use with 'initialize', because
4203 * that is the only activity for which there can be multiple instances running
4204 * concurrently. In the case of 'initialize', the tag corresponds to the guid of
4205 * the vdev on which to wait.
4206 *
4207 * If a thread waiting in the ioctl receives a signal, the call will return
4208 * immediately, and the return value will be EINTR.
4209 *
4210 * innvl: {
4211 * "wait_activity" -> int32_t
4212 * (optional) "wait_tag" -> uint64_t
4213 * }
4214 *
4215 * outnvl: "waited" -> boolean_t
4216 */
4217static const zfs_ioc_key_t zfs_keys_pool_wait[] = {
4218 {ZPOOL_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4219 {ZPOOL_WAIT_TAG, DATA_TYPE_UINT64, ZK_OPTIONAL},
4220};
4221
4222static int
4223zfs_ioc_wait(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4224{
4225 int32_t activity;
4226 uint64_t tag;
4227 boolean_t waited;
4228 int error;
4229
4230 if (nvlist_lookup_int32(innvl, ZPOOL_WAIT_ACTIVITY, &activity) != 0)
4231 return (EINVAL);
4232
4233 if (nvlist_lookup_uint64(innvl, ZPOOL_WAIT_TAG, &tag) == 0)
4234 error = spa_wait_tag(name, activity, tag, &waited);
4235 else
4236 error = spa_wait(name, activity, &waited);
4237
4238 if (error == 0)
4239 fnvlist_add_boolean_value(outnvl, ZPOOL_WAIT_WAITED, waited);
4240
4241 return (error);
4242}
4243
5a42ef04
PD
4244/*
4245 * This ioctl waits for activity of a particular type to complete. If there is
4246 * no activity of that type in progress, it returns immediately, and the
4247 * returned value "waited" is false. If there is activity in progress, and no
4248 * tag is passed in, the ioctl blocks until all activity of that type is
4249 * complete, and then returns with "waited" set to true.
4250 *
4251 * If a thread waiting in the ioctl receives a signal, the call will return
4252 * immediately, and the return value will be EINTR.
4253 *
4254 * innvl: {
4255 * "wait_activity" -> int32_t
4256 * }
4257 *
4258 * outnvl: "waited" -> boolean_t
4259 */
4260static const zfs_ioc_key_t zfs_keys_fs_wait[] = {
4261 {ZFS_WAIT_ACTIVITY, DATA_TYPE_INT32, 0},
4262};
4263
4264static int
4265zfs_ioc_wait_fs(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
4266{
4267 int32_t activity;
4268 boolean_t waited = B_FALSE;
4269 int error;
4270 dsl_pool_t *dp;
4271 dsl_dir_t *dd;
4272 dsl_dataset_t *ds;
4273
4274 if (nvlist_lookup_int32(innvl, ZFS_WAIT_ACTIVITY, &activity) != 0)
4275 return (SET_ERROR(EINVAL));
4276
4277 if (activity >= ZFS_WAIT_NUM_ACTIVITIES || activity < 0)
4278 return (SET_ERROR(EINVAL));
4279
4280 if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
4281 return (error);
4282
4283 if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
4284 dsl_pool_rele(dp, FTAG);
4285 return (error);
4286 }
4287
4288 dd = ds->ds_dir;
4289 mutex_enter(&dd->dd_activity_lock);
4290 dd->dd_activity_waiters++;
4291
4292 /*
4293 * We get a long-hold here so that the dsl_dataset_t and dsl_dir_t
4294 * aren't evicted while we're waiting. Normally this is prevented by
4295 * holding the pool, but we can't do that while we're waiting since
4296 * that would prevent TXGs from syncing out. Some of the functionality
4297 * of long-holds (e.g. preventing deletion) is unnecessary for this
4298 * case, since we would cancel the waiters before proceeding with a
4299 * deletion. An alternative mechanism for keeping the dataset around
4300 * could be developed but this is simpler.
4301 */
4302 dsl_dataset_long_hold(ds, FTAG);
4303 dsl_pool_rele(dp, FTAG);
4304
4305 error = dsl_dir_wait(dd, ds, activity, &waited);
4306
4307 dsl_dataset_long_rele(ds, FTAG);
4308 dd->dd_activity_waiters--;
4309 if (dd->dd_activity_waiters == 0)
4310 cv_signal(&dd->dd_activity_cv);
4311 mutex_exit(&dd->dd_activity_lock);
4312
4313 dsl_dataset_rele(ds, FTAG);
4314
4315 if (error == 0)
4316 fnvlist_add_boolean_value(outnvl, ZFS_WAIT_WAITED, waited);
4317
4318 return (error);
4319}
4320
34dc7c2f 4321/*
46ba1e59 4322 * fsname is name of dataset to rollback (to most recent snapshot)
34dc7c2f 4323 *
8ca78ab0 4324 * innvl may contain name of expected target snapshot
46ba1e59
MA
4325 *
4326 * outnvl: "target" -> name of most recent snapshot
4327 * }
34dc7c2f 4328 */
b83a0e2d
DB
4329static const zfs_ioc_key_t zfs_keys_rollback[] = {
4330 {"target", DATA_TYPE_STRING, ZK_OPTIONAL},
4331};
4332
34dc7c2f 4333static int
8ca78ab0 4334zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
34dc7c2f 4335{
0037b49e 4336 zfsvfs_t *zfsvfs;
5df7e9d8 4337 zvol_state_handle_t *zv;
8ca78ab0 4338 char *target = NULL;
13fe0198 4339 int error;
34dc7c2f 4340
8ca78ab0
AG
4341 (void) nvlist_lookup_string(innvl, "target", &target);
4342 if (target != NULL) {
13342832 4343 const char *cp = strchr(target, '@');
8ca78ab0 4344
13342832
AG
4345 /*
4346 * The snap name must contain an @, and the part after it must
4347 * contain only valid characters.
4348 */
4349 if (cp == NULL ||
4350 zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
8ca78ab0
AG
4351 return (SET_ERROR(EINVAL));
4352 }
4353
f298b24d 4354 if (getzfsvfs(fsname, &zfsvfs) == 0) {
ec923db2
GM
4355 dsl_dataset_t *ds;
4356
0037b49e
BB
4357 ds = dmu_objset_ds(zfsvfs->z_os);
4358 error = zfs_suspend_fs(zfsvfs);
34dc7c2f
BB
4359 if (error == 0) {
4360 int resume_err;
4361
8ca78ab0
AG
4362 error = dsl_dataset_rollback(fsname, target, zfsvfs,
4363 outnvl);
0037b49e 4364 resume_err = zfs_resume_fs(zfsvfs, ds);
34dc7c2f 4365 error = error ? error : resume_err;
34dc7c2f 4366 }
362ae8d1 4367 zfs_vfs_rele(zfsvfs);
040dab99 4368 } else if ((zv = zvol_suspend(fsname)) != NULL) {
8ca78ab0
AG
4369 error = dsl_dataset_rollback(fsname, target, zvol_tag(zv),
4370 outnvl);
040dab99 4371 zvol_resume(zv);
34dc7c2f 4372 } else {
8ca78ab0 4373 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
34dc7c2f 4374 }
13fe0198
MA
4375 return (error);
4376}
34dc7c2f 4377
13fe0198
MA
4378static int
4379recursive_unmount(const char *fsname, void *arg)
4380{
4381 const char *snapname = arg;
4382 char *fullname;
428870ff 4383
13fe0198 4384 fullname = kmem_asprintf("%s@%s", fsname, snapname);
8d103d88 4385 zfs_unmount_snap(fullname);
e4f5fa12 4386 kmem_strfree(fullname);
00fcdee1 4387
8d103d88 4388 return (0);
34dc7c2f
BB
4389}
4390
30af21b0
PD
4391/*
4392 *
4393 * snapname is the snapshot to redact.
4394 * innvl: {
4395 * "bookname" -> (string)
a73f361f 4396 * shortname of the redaction bookmark to generate
30af21b0
PD
4397 * "snapnv" -> (nvlist, values ignored)
4398 * snapshots to redact snapname with respect to
4399 * }
4400 *
4401 * outnvl is unused
4402 */
4403
30af21b0
PD
4404static const zfs_ioc_key_t zfs_keys_redact[] = {
4405 {"bookname", DATA_TYPE_STRING, 0},
4406 {"snapnv", DATA_TYPE_NVLIST, 0},
4407};
ef70eff1 4408
30af21b0
PD
4409static int
4410zfs_ioc_redact(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
4411{
ef70eff1 4412 (void) outnvl;
30af21b0
PD
4413 nvlist_t *redactnvl = NULL;
4414 char *redactbook = NULL;
4415
4416 if (nvlist_lookup_nvlist(innvl, "snapnv", &redactnvl) != 0)
4417 return (SET_ERROR(EINVAL));
4418 if (fnvlist_num_pairs(redactnvl) == 0)
4419 return (SET_ERROR(ENXIO));
4420 if (nvlist_lookup_string(innvl, "bookname", &redactbook) != 0)
4421 return (SET_ERROR(EINVAL));
4422
4423 return (dmu_redact_snap(snapname, redactnvl, redactbook));
4424}
4425
34dc7c2f
BB
4426/*
4427 * inputs:
4428 * zc_name old name of dataset
4429 * zc_value new name of dataset
4430 * zc_cookie recursive flag (only valid for snapshots)
4431 *
4432 * outputs: none
4433 */
4434static int
4435zfs_ioc_rename(zfs_cmd_t *zc)
4436{
dc1c630b
AG
4437 objset_t *os;
4438 dmu_objset_type_t ost;
34dc7c2f 4439 boolean_t recursive = zc->zc_cookie & 1;
7b4e2723 4440 boolean_t nounmount = !!(zc->zc_cookie & 2);
13fe0198 4441 char *at;
dc1c630b 4442 int err;
34dc7c2f 4443
650258d7 4444 /* "zfs rename" from and to ...%recv datasets should both fail */
4445 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
34dc7c2f 4446 zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
650258d7 4447 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4448 dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4449 strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
2e528b49 4450 return (SET_ERROR(EINVAL));
34dc7c2f 4451
dc1c630b
AG
4452 err = dmu_objset_hold(zc->zc_name, FTAG, &os);
4453 if (err != 0)
4454 return (err);
4455 ost = dmu_objset_type(os);
4456 dmu_objset_rele(os, FTAG);
4457
13fe0198
MA
4458 at = strchr(zc->zc_name, '@');
4459 if (at != NULL) {
4460 /* snaps must be in same fs */
9554185d
SH
4461 int error;
4462
13fe0198 4463 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
2e528b49 4464 return (SET_ERROR(EXDEV));
13fe0198 4465 *at = '\0';
7b4e2723 4466 if (ost == DMU_OST_ZFS && !nounmount) {
9554185d 4467 error = dmu_objset_find(zc->zc_name,
13fe0198
MA
4468 recursive_unmount, at + 1,
4469 recursive ? DS_FIND_CHILDREN : 0);
9554185d
SH
4470 if (error != 0) {
4471 *at = '@';
13fe0198 4472 return (error);
9554185d 4473 }
13fe0198 4474 }
9554185d
SH
4475 error = dsl_dataset_rename_snapshot(zc->zc_name,
4476 at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4477 *at = '@';
4478
4479 return (error);
13fe0198 4480 } else {
ba6a2402 4481 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
95c73795 4482 }
34dc7c2f
BB
4483}
4484
428870ff
BB
4485static int
4486zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4487{
4488 const char *propname = nvpair_name(pair);
4489 boolean_t issnap = (strchr(dsname, '@') != NULL);
4490 zfs_prop_t prop = zfs_name_to_prop(propname);
10b3c7f5 4491 uint64_t intval, compval;
428870ff
BB
4492 int err;
4493
4ff7a8fa 4494 if (prop == ZPROP_USERPROP) {
428870ff 4495 if (zfs_prop_user(propname)) {
c65aa5b2
BB
4496 if ((err = zfs_secpolicy_write_perms(dsname,
4497 ZFS_DELEG_PERM_USERPROP, cr)))
428870ff
BB
4498 return (err);
4499 return (0);
4500 }
4501
4502 if (!issnap && zfs_prop_userquota(propname)) {
4503 const char *perm = NULL;
4504 const char *uq_prefix =
4505 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA];
4506 const char *gq_prefix =
4507 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA];
1de321e6
JX
4508 const char *uiq_prefix =
4509 zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA];
4510 const char *giq_prefix =
4511 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA];
9c5167d1
NF
4512 const char *pq_prefix =
4513 zfs_userquota_prop_prefixes[ZFS_PROP_PROJECTQUOTA];
4514 const char *piq_prefix = zfs_userquota_prop_prefixes[\
4515 ZFS_PROP_PROJECTOBJQUOTA];
428870ff
BB
4516
4517 if (strncmp(propname, uq_prefix,
4518 strlen(uq_prefix)) == 0) {
4519 perm = ZFS_DELEG_PERM_USERQUOTA;
1de321e6
JX
4520 } else if (strncmp(propname, uiq_prefix,
4521 strlen(uiq_prefix)) == 0) {
4522 perm = ZFS_DELEG_PERM_USEROBJQUOTA;
428870ff
BB
4523 } else if (strncmp(propname, gq_prefix,
4524 strlen(gq_prefix)) == 0) {
4525 perm = ZFS_DELEG_PERM_GROUPQUOTA;
1de321e6
JX
4526 } else if (strncmp(propname, giq_prefix,
4527 strlen(giq_prefix)) == 0) {
4528 perm = ZFS_DELEG_PERM_GROUPOBJQUOTA;
9c5167d1
NF
4529 } else if (strncmp(propname, pq_prefix,
4530 strlen(pq_prefix)) == 0) {
4531 perm = ZFS_DELEG_PERM_PROJECTQUOTA;
4532 } else if (strncmp(propname, piq_prefix,
4533 strlen(piq_prefix)) == 0) {
4534 perm = ZFS_DELEG_PERM_PROJECTOBJQUOTA;
428870ff 4535 } else {
9c5167d1 4536 /* {USER|GROUP|PROJECT}USED are read-only */
2e528b49 4537 return (SET_ERROR(EINVAL));
428870ff
BB
4538 }
4539
c65aa5b2 4540 if ((err = zfs_secpolicy_write_perms(dsname, perm, cr)))
428870ff
BB
4541 return (err);
4542 return (0);
4543 }
4544
2e528b49 4545 return (SET_ERROR(EINVAL));
428870ff
BB
4546 }
4547
4548 if (issnap)
2e528b49 4549 return (SET_ERROR(EINVAL));
428870ff
BB
4550
4551 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
4552 /*
4553 * dsl_prop_get_all_impl() returns properties in this
4554 * format.
4555 */
4556 nvlist_t *attrs;
4557 VERIFY(nvpair_value_nvlist(pair, &attrs) == 0);
4558 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4559 &pair) == 0);
4560 }
4561
4562 /*
4563 * Check that this value is valid for this pool version
4564 */
4565 switch (prop) {
4566 case ZFS_PROP_COMPRESSION:
4567 /*
4568 * If the user specified gzip compression, make sure
4569 * the SPA supports it. We ignore any errors here since
4570 * we'll catch them later.
4571 */
f1512ee6 4572 if (nvpair_value_uint64(pair, &intval) == 0) {
10b3c7f5
MN
4573 compval = ZIO_COMPRESS_ALGO(intval);
4574 if (compval >= ZIO_COMPRESS_GZIP_1 &&
4575 compval <= ZIO_COMPRESS_GZIP_9 &&
428870ff
BB
4576 zfs_earlier_version(dsname,
4577 SPA_VERSION_GZIP_COMPRESSION)) {
2e528b49 4578 return (SET_ERROR(ENOTSUP));
428870ff
BB
4579 }
4580
10b3c7f5 4581 if (compval == ZIO_COMPRESS_ZLE &&
428870ff
BB
4582 zfs_earlier_version(dsname,
4583 SPA_VERSION_ZLE_COMPRESSION))
2e528b49 4584 return (SET_ERROR(ENOTSUP));
428870ff 4585
10b3c7f5 4586 if (compval == ZIO_COMPRESS_LZ4) {
9759c60f
ED
4587 spa_t *spa;
4588
4589 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4590 return (err);
4591
fa86b5db
MA
4592 if (!spa_feature_is_enabled(spa,
4593 SPA_FEATURE_LZ4_COMPRESS)) {
9759c60f 4594 spa_close(spa, FTAG);
2e528b49 4595 return (SET_ERROR(ENOTSUP));
9759c60f
ED
4596 }
4597 spa_close(spa, FTAG);
4598 }
10b3c7f5
MN
4599
4600 if (compval == ZIO_COMPRESS_ZSTD) {
4601 spa_t *spa;
4602
4603 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4604 return (err);
4605
4606 if (!spa_feature_is_enabled(spa,
4607 SPA_FEATURE_ZSTD_COMPRESS)) {
4608 spa_close(spa, FTAG);
4609 return (SET_ERROR(ENOTSUP));
4610 }
4611 spa_close(spa, FTAG);
4612 }
428870ff
BB
4613 }
4614 break;
4615
4616 case ZFS_PROP_COPIES:
4617 if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS))
2e528b49 4618 return (SET_ERROR(ENOTSUP));
428870ff
BB
4619 break;
4620
4cb7b9c5 4621 case ZFS_PROP_VOLBLOCKSIZE:
f1512ee6
MA
4622 case ZFS_PROP_RECORDSIZE:
4623 /* Record sizes above 128k need the feature to be enabled */
4624 if (nvpair_value_uint64(pair, &intval) == 0 &&
4625 intval > SPA_OLD_MAXBLOCKSIZE) {
4626 spa_t *spa;
4627
f1512ee6
MA
4628 /*
4629 * We don't allow setting the property above 1MB,
4630 * unless the tunable has been changed.
4631 */
4632 if (intval > zfs_max_recordsize ||
4633 intval > SPA_MAXBLOCKSIZE)
4b2a3e0c 4634 return (SET_ERROR(ERANGE));
f1512ee6
MA
4635
4636 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4637 return (err);
4638
4639 if (!spa_feature_is_enabled(spa,
4640 SPA_FEATURE_LARGE_BLOCKS)) {
4641 spa_close(spa, FTAG);
4642 return (SET_ERROR(ENOTSUP));
4643 }
4644 spa_close(spa, FTAG);
4645 }
4646 break;
4647
50c957f7
NB
4648 case ZFS_PROP_DNODESIZE:
4649 /* Dnode sizes above 512 need the feature to be enabled */
4650 if (nvpair_value_uint64(pair, &intval) == 0 &&
4651 intval != ZFS_DNSIZE_LEGACY) {
4652 spa_t *spa;
4653
50c957f7
NB
4654 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4655 return (err);
4656
4657 if (!spa_feature_is_enabled(spa,
4658 SPA_FEATURE_LARGE_DNODE)) {
4659 spa_close(spa, FTAG);
4660 return (SET_ERROR(ENOTSUP));
4661 }
4662 spa_close(spa, FTAG);
4663 }
4664 break;
4665
cc99f275
DB
4666 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
4667 /*
4668 * This property could require the allocation classes
4669 * feature to be active for setting, however we allow
4670 * it so that tests of settable properties succeed.
4671 * The CLI will issue a warning in this case.
4672 */
4673 break;
4674
428870ff
BB
4675 case ZFS_PROP_SHARESMB:
4676 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
2e528b49 4677 return (SET_ERROR(ENOTSUP));
428870ff
BB
4678 break;
4679
4680 case ZFS_PROP_ACLINHERIT:
4681 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4682 nvpair_value_uint64(pair, &intval) == 0) {
4683 if (intval == ZFS_ACL_PASSTHROUGH_X &&
4684 zfs_earlier_version(dsname,
4685 SPA_VERSION_PASSTHROUGH_X))
2e528b49 4686 return (SET_ERROR(ENOTSUP));
428870ff
BB
4687 }
4688 break;
3c67d83a
TH
4689 case ZFS_PROP_CHECKSUM:
4690 case ZFS_PROP_DEDUP:
4691 {
4692 spa_feature_t feature;
4693 spa_t *spa;
3c67d83a
TH
4694 int err;
4695
4696 /* dedup feature version checks */
4697 if (prop == ZFS_PROP_DEDUP &&
4698 zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4699 return (SET_ERROR(ENOTSUP));
4700
c8c30836 4701 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4702 nvpair_value_uint64(pair, &intval) == 0) {
4703 /* check prop value is enabled in features */
4704 feature = zio_checksum_to_feature(
4705 intval & ZIO_CHECKSUM_MASK);
4706 if (feature == SPA_FEATURE_NONE)
4707 break;
3c67d83a 4708
c8c30836 4709 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4710 return (err);
5fadb7fb 4711
c8c30836 4712 if (!spa_feature_is_enabled(spa, feature)) {
4713 spa_close(spa, FTAG);
4714 return (SET_ERROR(ENOTSUP));
4715 }
3c67d83a 4716 spa_close(spa, FTAG);
3c67d83a 4717 }
3c67d83a
TH
4718 break;
4719 }
4720
e75c13c3
BB
4721 default:
4722 break;
428870ff
BB
4723 }
4724
4725 return (zfs_secpolicy_setprop(dsname, prop, pair, CRED()));
4726}
4727
4728/*
4729 * Removes properties from the given props list that fail permission checks
4730 * needed to clear them and to restore them in case of a receive error. For each
4731 * property, make sure we have both set and inherit permissions.
4732 *
4733 * Returns the first error encountered if any permission checks fail. If the
4734 * caller provides a non-NULL errlist, it also gives the complete list of names
4735 * of all the properties that failed a permission check along with the
4736 * corresponding error numbers. The caller is responsible for freeing the
4737 * returned errlist.
4738 *
4739 * If every property checks out successfully, zero is returned and the list
4740 * pointed at by errlist is NULL.
4741 */
4742static int
4d55ea81 4743zfs_check_clearable(const char *dataset, nvlist_t *props, nvlist_t **errlist)
b128c09f
BB
4744{
4745 zfs_cmd_t *zc;
428870ff
BB
4746 nvpair_t *pair, *next_pair;
4747 nvlist_t *errors;
4748 int err, rv = 0;
b128c09f
BB
4749
4750 if (props == NULL)
428870ff
BB
4751 return (0);
4752
4753 VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4754
efcd79a8 4755 zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP);
680eada9 4756 (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name));
428870ff
BB
4757 pair = nvlist_next_nvpair(props, NULL);
4758 while (pair != NULL) {
4759 next_pair = nvlist_next_nvpair(props, pair);
4760
680eada9 4761 (void) strlcpy(zc->zc_value, nvpair_name(pair),
4762 sizeof (zc->zc_value));
428870ff 4763 if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 ||
6f1ffb06 4764 (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) {
428870ff
BB
4765 VERIFY(nvlist_remove_nvpair(props, pair) == 0);
4766 VERIFY(nvlist_add_int32(errors,
4767 zc->zc_value, err) == 0);
4768 }
4769 pair = next_pair;
b128c09f
BB
4770 }
4771 kmem_free(zc, sizeof (zfs_cmd_t));
428870ff
BB
4772
4773 if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) {
4774 nvlist_free(errors);
4775 errors = NULL;
4776 } else {
4777 VERIFY(nvpair_value_int32(pair, &rv) == 0);
4778 }
4779
4780 if (errlist == NULL)
4781 nvlist_free(errors);
4782 else
4783 *errlist = errors;
4784
4785 return (rv);
4786}
4787
4788static boolean_t
4789propval_equals(nvpair_t *p1, nvpair_t *p2)
4790{
4791 if (nvpair_type(p1) == DATA_TYPE_NVLIST) {
4792 /* dsl_prop_get_all_impl() format */
4793 nvlist_t *attrs;
4794 VERIFY(nvpair_value_nvlist(p1, &attrs) == 0);
4795 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4796 &p1) == 0);
4797 }
4798
4799 if (nvpair_type(p2) == DATA_TYPE_NVLIST) {
4800 nvlist_t *attrs;
4801 VERIFY(nvpair_value_nvlist(p2, &attrs) == 0);
4802 VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
4803 &p2) == 0);
4804 }
4805
4806 if (nvpair_type(p1) != nvpair_type(p2))
4807 return (B_FALSE);
4808
4809 if (nvpair_type(p1) == DATA_TYPE_STRING) {
4810 char *valstr1, *valstr2;
4811
4812 VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0);
4813 VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0);
4814 return (strcmp(valstr1, valstr2) == 0);
4815 } else {
4816 uint64_t intval1, intval2;
4817
4818 VERIFY(nvpair_value_uint64(p1, &intval1) == 0);
4819 VERIFY(nvpair_value_uint64(p2, &intval2) == 0);
4820 return (intval1 == intval2);
4821 }
b128c09f
BB
4822}
4823
428870ff
BB
4824/*
4825 * Remove properties from props if they are not going to change (as determined
4826 * by comparison with origprops). Remove them from origprops as well, since we
4827 * do not need to clear or restore properties that won't change.
4828 */
4829static void
4830props_reduce(nvlist_t *props, nvlist_t *origprops)
4831{
4832 nvpair_t *pair, *next_pair;
4833
4834 if (origprops == NULL)
4835 return; /* all props need to be received */
4836
4837 pair = nvlist_next_nvpair(props, NULL);
4838 while (pair != NULL) {
4839 const char *propname = nvpair_name(pair);
4840 nvpair_t *match;
4841
4842 next_pair = nvlist_next_nvpair(props, pair);
4843
4844 if ((nvlist_lookup_nvpair(origprops, propname,
4845 &match) != 0) || !propval_equals(pair, match))
4846 goto next; /* need to set received value */
4847
4848 /* don't clear the existing received value */
4849 (void) nvlist_remove_nvpair(origprops, match);
4850 /* don't bother receiving the property */
4851 (void) nvlist_remove_nvpair(props, pair);
4852next:
4853 pair = next_pair;
4854 }
4855}
4856
671c9354
DM
4857/*
4858 * Extract properties that cannot be set PRIOR to the receipt of a dataset.
4859 * For example, refquota cannot be set until after the receipt of a dataset,
4860 * because in replication streams, an older/earlier snapshot may exceed the
4861 * refquota. We want to receive the older/earlier snapshot, but setting
4862 * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent
4863 * the older/earlier snapshot from being received (with EDQUOT).
4864 *
4865 * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario.
4866 *
4867 * libzfs will need to be judicious handling errors encountered by props
4868 * extracted by this function.
4869 */
4870static nvlist_t *
4871extract_delay_props(nvlist_t *props)
4872{
4873 nvlist_t *delayprops;
4874 nvpair_t *nvp, *tmp;
b5256303
TC
4875 static const zfs_prop_t delayable[] = {
4876 ZFS_PROP_REFQUOTA,
4877 ZFS_PROP_KEYLOCATION,
c50b3f14
AH
4878 /*
4879 * Setting ZFS_PROP_SHARESMB requires the objset type to be
4880 * known, which is not possible prior to receipt of raw sends.
4881 */
4882 ZFS_PROP_SHARESMB,
b5256303
TC
4883 0
4884 };
671c9354
DM
4885 int i;
4886
4887 VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4888
4889 for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL;
4890 nvp = nvlist_next_nvpair(props, nvp)) {
4891 /*
4892 * strcmp() is safe because zfs_prop_to_name() always returns
4893 * a bounded string.
4894 */
4895 for (i = 0; delayable[i] != 0; i++) {
4896 if (strcmp(zfs_prop_to_name(delayable[i]),
4897 nvpair_name(nvp)) == 0) {
4898 break;
4899 }
4900 }
4901 if (delayable[i] != 0) {
4902 tmp = nvlist_prev_nvpair(props, nvp);
4903 VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4904 VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4905 nvp = tmp;
4906 }
4907 }
4908
4909 if (nvlist_empty(delayprops)) {
4910 nvlist_free(delayprops);
4911 delayprops = NULL;
4912 }
4913 return (delayprops);
4914}
4915
7bb0c294
MM
4916static void
4917zfs_allow_log_destroy(void *arg)
4918{
4919 char *poolname = arg;
4920
4921 if (poolname != NULL)
e4f5fa12 4922 kmem_strfree(poolname);
7bb0c294
MM
4923}
4924
6d8da841 4925#ifdef ZFS_DEBUG
428870ff
BB
4926static boolean_t zfs_ioc_recv_inject_err;
4927#endif
4928
34dc7c2f 4929/*
1bf3bf0e
GN
4930 * nvlist 'errors' is always allocated. It will contain descriptions of
4931 * encountered errors, if any. It's the callers responsibility to free.
34dc7c2f
BB
4932 */
4933static int
a3eeab2d 4934zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops,
d9c460a0 4935 nvlist_t *localprops, nvlist_t *hidden_args, boolean_t force,
e8cf3a4f 4936 boolean_t heal, boolean_t resumable, int input_fd,
196bee4c
MA
4937 dmu_replay_record_t *begin_record, uint64_t *read_bytes,
4938 uint64_t *errflags, nvlist_t **errors)
34dc7c2f 4939{
34dc7c2f 4940 dmu_recv_cookie_t drc;
428870ff
BB
4941 int error = 0;
4942 int props_error = 0;
da92d5cb 4943 offset_t off, noff;
d9c460a0
TC
4944 nvlist_t *local_delayprops = NULL;
4945 nvlist_t *recv_delayprops = NULL;
c50b3f14 4946 nvlist_t *inherited_delayprops = NULL;
43e52edd 4947 nvlist_t *origprops = NULL; /* existing properties */
a3eeab2d 4948 nvlist_t *origrecvd = NULL; /* existing received properties */
428870ff 4949 boolean_t first_recvd_props = B_FALSE;
30af21b0 4950 boolean_t tofs_was_redacted;
da92d5cb 4951 zfs_file_t *input_fp;
34dc7c2f 4952
1bf3bf0e
GN
4953 *read_bytes = 0;
4954 *errflags = 0;
4955 *errors = fnvlist_alloc();
da92d5cb 4956 off = 0;
1bf3bf0e 4957
958826be
GW
4958 if ((input_fp = zfs_file_get(input_fd)) == NULL)
4959 return (SET_ERROR(EBADF));
13fe0198 4960
da92d5cb 4961 noff = off = zfs_file_off(input_fp);
e8cf3a4f 4962 error = dmu_recv_begin(tofs, tosnap, begin_record, force, heal,
da92d5cb 4963 resumable, localprops, hidden_args, origin, &drc, input_fp,
30af21b0 4964 &off);
13fe0198
MA
4965 if (error != 0)
4966 goto out;
30af21b0 4967 tofs_was_redacted = dsl_get_redacted(drc.drc_ds);
13fe0198
MA
4968
4969 /*
4970 * Set properties before we receive the stream so that they are applied
4971 * to the new data. Note that we must call dmu_recv_stream() if
4972 * dmu_recv_begin() succeeds.
4973 */
a3eeab2d 4974 if (recvprops != NULL && !drc.drc_newfs) {
13fe0198
MA
4975 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4976 SPA_VERSION_RECVD_PROPS &&
4977 !dsl_prop_get_hasrecvd(tofs))
428870ff 4978 first_recvd_props = B_TRUE;
428870ff 4979
b128c09f 4980 /*
428870ff 4981 * If new received properties are supplied, they are to
d9c460a0
TC
4982 * completely replace the existing received properties,
4983 * so stash away the existing ones.
b128c09f 4984 */
a3eeab2d 4985 if (dsl_prop_get_received(tofs, &origrecvd) == 0) {
428870ff
BB
4986 nvlist_t *errlist = NULL;
4987 /*
4988 * Don't bother writing a property if its value won't
4989 * change (and avoid the unnecessary security checks).
4990 *
4991 * The first receive after SPA_VERSION_RECVD_PROPS is a
4992 * special case where we blow away all local properties
4993 * regardless.
4994 */
4995 if (!first_recvd_props)
a3eeab2d 4996 props_reduce(recvprops, origrecvd);
4997 if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0)
43e52edd 4998 (void) nvlist_merge(*errors, errlist, 0);
428870ff 4999 nvlist_free(errlist);
b128c09f 5000
a3eeab2d 5001 if (clear_received_props(tofs, origrecvd,
5002 first_recvd_props ? NULL : recvprops) != 0)
5003 *errflags |= ZPROP_ERR_NOCLEAR;
5004 } else {
5005 *errflags |= ZPROP_ERR_NOCLEAR;
5006 }
5007 }
5008
5009 /*
5010 * Stash away existing properties so we can restore them on error unless
5011 * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which
5012 * case "origrecvd" will take care of that.
5013 */
5014 if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) {
5015 objset_t *os;
5016 if (dmu_objset_hold(tofs, FTAG, &os) == 0) {
5017 if (dsl_prop_get_all(os, &origprops) != 0) {
43e52edd 5018 *errflags |= ZPROP_ERR_NOCLEAR;
a3eeab2d 5019 }
5020 dmu_objset_rele(os, FTAG);
13fe0198 5021 } else {
43e52edd 5022 *errflags |= ZPROP_ERR_NOCLEAR;
428870ff 5023 }
13fe0198
MA
5024 }
5025
a3eeab2d 5026 if (recvprops != NULL) {
13fe0198 5027 props_error = dsl_prop_set_hasrecvd(tofs);
428870ff 5028
13fe0198 5029 if (props_error == 0) {
d9c460a0 5030 recv_delayprops = extract_delay_props(recvprops);
13fe0198 5031 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
a3eeab2d 5032 recvprops, *errors);
13fe0198 5033 }
428870ff
BB
5034 }
5035
a3eeab2d 5036 if (localprops != NULL) {
5037 nvlist_t *oprops = fnvlist_alloc();
5038 nvlist_t *xprops = fnvlist_alloc();
5039 nvpair_t *nvp = NULL;
5040
5041 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5042 if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) {
5043 /* -x property */
5044 const char *name = nvpair_name(nvp);
5045 zfs_prop_t prop = zfs_name_to_prop(name);
4ff7a8fa 5046 if (prop != ZPROP_USERPROP) {
a3eeab2d 5047 if (!zfs_prop_inheritable(prop))
5048 continue;
5049 } else if (!zfs_prop_user(name))
5050 continue;
5051 fnvlist_add_boolean(xprops, name);
5052 } else {
5053 /* -o property=value */
5054 fnvlist_add_nvpair(oprops, nvp);
5055 }
5056 }
d9c460a0
TC
5057
5058 local_delayprops = extract_delay_props(oprops);
a3eeab2d 5059 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5060 oprops, *errors);
c50b3f14 5061 inherited_delayprops = extract_delay_props(xprops);
a3eeab2d 5062 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5063 xprops, *errors);
5064
5065 nvlist_free(oprops);
5066 nvlist_free(xprops);
5067 }
5068
196bee4c 5069 error = dmu_recv_stream(&drc, &off);
34dc7c2f 5070
45d1cae3 5071 if (error == 0) {
0037b49e 5072 zfsvfs_t *zfsvfs = NULL;
5df7e9d8 5073 zvol_state_handle_t *zv = NULL;
b128c09f 5074
f298b24d 5075 if (getzfsvfs(tofs, &zfsvfs) == 0) {
45d1cae3 5076 /* online recv */
ec923db2 5077 dsl_dataset_t *ds;
45d1cae3 5078 int end_err;
30af21b0
PD
5079 boolean_t stream_is_redacted = DMU_GET_FEATUREFLAGS(
5080 begin_record->drr_u.drr_begin.
5081 drr_versioninfo) & DMU_BACKUP_FEATURE_REDACTED;
b128c09f 5082
0037b49e
BB
5083 ds = dmu_objset_ds(zfsvfs->z_os);
5084 error = zfs_suspend_fs(zfsvfs);
45d1cae3
BB
5085 /*
5086 * If the suspend fails, then the recv_end will
5087 * likely also fail, and clean up after itself.
5088 */
0037b49e 5089 end_err = dmu_recv_end(&drc, zfsvfs);
30af21b0
PD
5090 /*
5091 * If the dataset was not redacted, but we received a
5092 * redacted stream onto it, we need to unmount the
5093 * dataset. Otherwise, resume the filesystem.
5094 */
5095 if (error == 0 && !drc.drc_newfs &&
5096 stream_is_redacted && !tofs_was_redacted) {
5097 error = zfs_end_fs(zfsvfs, ds);
5098 } else if (error == 0) {
0037b49e 5099 error = zfs_resume_fs(zfsvfs, ds);
30af21b0 5100 }
45d1cae3 5101 error = error ? error : end_err;
362ae8d1 5102 zfs_vfs_rele(zfsvfs);
040dab99
CC
5103 } else if ((zv = zvol_suspend(tofs)) != NULL) {
5104 error = dmu_recv_end(&drc, zvol_tag(zv));
5105 zvol_resume(zv);
b128c09f 5106 } else {
831baf06 5107 error = dmu_recv_end(&drc, NULL);
34dc7c2f 5108 }
671c9354
DM
5109
5110 /* Set delayed properties now, after we're done receiving. */
d9c460a0 5111 if (recv_delayprops != NULL && error == 0) {
671c9354 5112 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
d9c460a0
TC
5113 recv_delayprops, *errors);
5114 }
5115 if (local_delayprops != NULL && error == 0) {
5116 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL,
5117 local_delayprops, *errors);
671c9354 5118 }
c50b3f14
AH
5119 if (inherited_delayprops != NULL && error == 0) {
5120 (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED,
5121 inherited_delayprops, *errors);
5122 }
671c9354
DM
5123 }
5124
d9c460a0
TC
5125 /*
5126 * Merge delayed props back in with initial props, in case
5127 * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
5128 * we have to make sure clear_received_props() includes
5129 * the delayed properties).
5130 *
5131 * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
5132 * using ASSERT() will be just like a VERIFY.
5133 */
5134 if (recv_delayprops != NULL) {
5135 ASSERT(nvlist_merge(recvprops, recv_delayprops, 0) == 0);
5136 nvlist_free(recv_delayprops);
5137 }
5138 if (local_delayprops != NULL) {
5139 ASSERT(nvlist_merge(localprops, local_delayprops, 0) == 0);
5140 nvlist_free(local_delayprops);
671c9354 5141 }
c50b3f14
AH
5142 if (inherited_delayprops != NULL) {
5143 ASSERT(nvlist_merge(localprops, inherited_delayprops, 0) == 0);
5144 nvlist_free(inherited_delayprops);
5145 }
da92d5cb 5146 *read_bytes = off - noff;
34dc7c2f 5147
6d8da841 5148#ifdef ZFS_DEBUG
428870ff
BB
5149 if (zfs_ioc_recv_inject_err) {
5150 zfs_ioc_recv_inject_err = B_FALSE;
5151 error = 1;
5152 }
5153#endif
ba6a2402 5154
b128c09f
BB
5155 /*
5156 * On error, restore the original props.
5157 */
a3eeab2d 5158 if (error != 0 && recvprops != NULL && !drc.drc_newfs) {
5159 if (clear_received_props(tofs, recvprops, NULL) != 0) {
13fe0198
MA
5160 /*
5161 * We failed to clear the received properties.
5162 * Since we may have left a $recvd value on the
5163 * system, we can't clear the $hasrecvd flag.
5164 */
43e52edd 5165 *errflags |= ZPROP_ERR_NORESTORE;
13fe0198
MA
5166 } else if (first_recvd_props) {
5167 dsl_prop_unset_hasrecvd(tofs);
428870ff
BB
5168 }
5169
a3eeab2d 5170 if (origrecvd == NULL && !drc.drc_newfs) {
428870ff 5171 /* We failed to stash the original properties. */
43e52edd 5172 *errflags |= ZPROP_ERR_NORESTORE;
428870ff
BB
5173 }
5174
5175 /*
5176 * dsl_props_set() will not convert RECEIVED to LOCAL on or
5177 * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4e33ba4c 5178 * explicitly if we're restoring local properties cleared in the
428870ff
BB
5179 * first new-style receive.
5180 */
a3eeab2d 5181 if (origrecvd != NULL &&
428870ff
BB
5182 zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5183 ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
a3eeab2d 5184 origrecvd, NULL) != 0) {
428870ff
BB
5185 /*
5186 * We stashed the original properties but failed to
5187 * restore them.
5188 */
43e52edd 5189 *errflags |= ZPROP_ERR_NORESTORE;
428870ff 5190 }
b128c09f 5191 }
a3eeab2d 5192 if (error != 0 && localprops != NULL && !drc.drc_newfs &&
5193 !first_recvd_props) {
5194 nvlist_t *setprops;
5195 nvlist_t *inheritprops;
5196 nvpair_t *nvp;
5197
5198 if (origprops == NULL) {
5199 /* We failed to stash the original properties. */
5200 *errflags |= ZPROP_ERR_NORESTORE;
5201 goto out;
5202 }
5203
5204 /* Restore original props */
5205 setprops = fnvlist_alloc();
5206 inheritprops = fnvlist_alloc();
5207 nvp = NULL;
5208 while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) {
5209 const char *name = nvpair_name(nvp);
5210 const char *source;
5211 nvlist_t *attrs;
5212
5213 if (!nvlist_exists(origprops, name)) {
5214 /*
5215 * Property was not present or was explicitly
5216 * inherited before the receive, restore this.
5217 */
5218 fnvlist_add_boolean(inheritprops, name);
5219 continue;
5220 }
5221 attrs = fnvlist_lookup_nvlist(origprops, name);
5222 source = fnvlist_lookup_string(attrs, ZPROP_SOURCE);
5223
5224 /* Skip received properties */
5225 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0)
5226 continue;
5227
5228 if (strcmp(source, tofs) == 0) {
5229 /* Property was locally set */
5230 fnvlist_add_nvlist(setprops, name, attrs);
5231 } else {
5232 /* Property was implicitly inherited */
5233 fnvlist_add_boolean(inheritprops, name);
5234 }
5235 }
5236
5237 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops,
5238 NULL) != 0)
5239 *errflags |= ZPROP_ERR_NORESTORE;
5240 if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops,
5241 NULL) != 0)
5242 *errflags |= ZPROP_ERR_NORESTORE;
5243
5244 nvlist_free(setprops);
5245 nvlist_free(inheritprops);
5246 }
b128c09f 5247out:
958826be 5248 zfs_file_put(input_fp);
a3eeab2d 5249 nvlist_free(origrecvd);
b128c09f 5250 nvlist_free(origprops);
428870ff
BB
5251
5252 if (error == 0)
5253 error = props_error;
5254
34dc7c2f
BB
5255 return (error);
5256}
5257
43e52edd
BB
5258/*
5259 * inputs:
5260 * zc_name name of containing filesystem (unused)
5261 * zc_nvlist_src{_size} nvlist of properties to apply
a3eeab2d 5262 * zc_nvlist_conf{_size} nvlist of properties to exclude
5263 * (DATA_TYPE_BOOLEAN) and override (everything else)
43e52edd
BB
5264 * zc_value name of snapshot to create
5265 * zc_string name of clone origin (if DRR_FLAG_CLONE)
5266 * zc_cookie file descriptor to recv from
5267 * zc_begin_record the BEGIN record of the stream (not byteswapped)
5268 * zc_guid force flag
43e52edd
BB
5269 *
5270 * outputs:
5271 * zc_cookie number of bytes read
5272 * zc_obj zprop_errflags_t
43e52edd
BB
5273 * zc_nvlist_dst{_size} error for each unapplied received property
5274 */
5275static int
5276zfs_ioc_recv(zfs_cmd_t *zc)
5277{
5278 dmu_replay_record_t begin_record;
5279 nvlist_t *errors = NULL;
a3eeab2d 5280 nvlist_t *recvdprops = NULL;
5281 nvlist_t *localprops = NULL;
43e52edd
BB
5282 char *origin = NULL;
5283 char *tosnap;
eca7b760 5284 char tofs[ZFS_MAX_DATASET_NAME_LEN];
43e52edd
BB
5285 int error = 0;
5286
5287 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5288 strchr(zc->zc_value, '@') == NULL ||
5289 strchr(zc->zc_value, '%'))
5290 return (SET_ERROR(EINVAL));
5291
30f3f2e1 5292 (void) strlcpy(tofs, zc->zc_value, sizeof (tofs));
43e52edd
BB
5293 tosnap = strchr(tofs, '@');
5294 *tosnap++ = '\0';
5295
5296 if (zc->zc_nvlist_src != 0 &&
5297 (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
a3eeab2d 5298 zc->zc_iflags, &recvdprops)) != 0)
5299 return (error);
5300
5301 if (zc->zc_nvlist_conf != 0 &&
5302 (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
5303 zc->zc_iflags, &localprops)) != 0)
43e52edd
BB
5304 return (error);
5305
5306 if (zc->zc_string[0])
5307 origin = zc->zc_string;
5308
5309 begin_record.drr_type = DRR_BEGIN;
5310 begin_record.drr_payloadlen = 0;
5311 begin_record.drr_u.drr_begin = zc->zc_begin_record;
5312
a3eeab2d 5313 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops,
e8cf3a4f 5314 NULL, zc->zc_guid, B_FALSE, B_FALSE, zc->zc_cookie, &begin_record,
196bee4c 5315 &zc->zc_cookie, &zc->zc_obj, &errors);
a3eeab2d 5316 nvlist_free(recvdprops);
5317 nvlist_free(localprops);
43e52edd
BB
5318
5319 /*
5320 * Now that all props, initial and delayed, are set, report the prop
5321 * errors to the caller.
5322 */
5323 if (zc->zc_nvlist_dst_size != 0 && errors != NULL &&
5324 (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5325 put_nvlist(zc, errors) != 0)) {
5326 /*
5327 * Caller made zc->zc_nvlist_dst less than the minimum expected
5328 * size or supplied an invalid address.
5329 */
5330 error = SET_ERROR(EINVAL);
5331 }
5332
5333 nvlist_free(errors);
5334
5335 return (error);
5336}
5337
5338/*
5339 * innvl: {
5340 * "snapname" -> full name of the snapshot to create
a3eeab2d 5341 * (optional) "props" -> received properties to set (nvlist)
5342 * (optional) "localprops" -> override and exclude properties (nvlist)
43e52edd
BB
5343 * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE)
5344 * "begin_record" -> non-byteswapped dmu_replay_record_t
5345 * "input_fd" -> file descriptor to read stream from (int32)
5346 * (optional) "force" -> force flag (value ignored)
e8cf3a4f 5347 * (optional) "heal" -> use send stream to heal data corruption
43e52edd 5348 * (optional) "resumable" -> resumable flag (value ignored)
196bee4c
MA
5349 * (optional) "cleanup_fd" -> unused
5350 * (optional) "action_handle" -> unused
b83a0e2d 5351 * (optional) "hidden_args" -> { "wkeydata" -> value }
43e52edd
BB
5352 * }
5353 *
5354 * outnvl: {
5355 * "read_bytes" -> number of bytes read
5356 * "error_flags" -> zprop_errflags_t
43e52edd
BB
5357 * "errors" -> error for each unapplied received property (nvlist)
5358 * }
5359 */
b83a0e2d
DB
5360static const zfs_ioc_key_t zfs_keys_recv_new[] = {
5361 {"snapname", DATA_TYPE_STRING, 0},
5362 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5363 {"localprops", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5364 {"origin", DATA_TYPE_STRING, ZK_OPTIONAL},
5365 {"begin_record", DATA_TYPE_BYTE_ARRAY, 0},
5366 {"input_fd", DATA_TYPE_INT32, 0},
5367 {"force", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
e8cf3a4f 5368 {"heal", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
b83a0e2d
DB
5369 {"resumable", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
5370 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
5371 {"action_handle", DATA_TYPE_UINT64, ZK_OPTIONAL},
5372 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
5373};
5374
43e52edd
BB
5375static int
5376zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
5377{
5378 dmu_replay_record_t *begin_record;
5379 uint_t begin_record_size;
5380 nvlist_t *errors = NULL;
a3eeab2d 5381 nvlist_t *recvprops = NULL;
5382 nvlist_t *localprops = NULL;
d9c460a0 5383 nvlist_t *hidden_args = NULL;
b83a0e2d 5384 char *snapname;
43e52edd
BB
5385 char *origin = NULL;
5386 char *tosnap;
eca7b760 5387 char tofs[ZFS_MAX_DATASET_NAME_LEN];
43e52edd 5388 boolean_t force;
e8cf3a4f 5389 boolean_t heal;
43e52edd 5390 boolean_t resumable;
43e52edd
BB
5391 uint64_t read_bytes = 0;
5392 uint64_t errflags = 0;
5393 int input_fd = -1;
43e52edd
BB
5394 int error;
5395
b83a0e2d 5396 snapname = fnvlist_lookup_string(innvl, "snapname");
43e52edd
BB
5397
5398 if (dataset_namecheck(snapname, NULL, NULL) != 0 ||
5399 strchr(snapname, '@') == NULL ||
5400 strchr(snapname, '%'))
5401 return (SET_ERROR(EINVAL));
5402
c9e319fa 5403 (void) strlcpy(tofs, snapname, sizeof (tofs));
43e52edd
BB
5404 tosnap = strchr(tofs, '@');
5405 *tosnap++ = '\0';
5406
5407 error = nvlist_lookup_string(innvl, "origin", &origin);
5408 if (error && error != ENOENT)
5409 return (error);
5410
5411 error = nvlist_lookup_byte_array(innvl, "begin_record",
02730c33 5412 (uchar_t **)&begin_record, &begin_record_size);
43e52edd
BB
5413 if (error != 0 || begin_record_size != sizeof (*begin_record))
5414 return (SET_ERROR(EINVAL));
5415
b83a0e2d 5416 input_fd = fnvlist_lookup_int32(innvl, "input_fd");
43e52edd
BB
5417
5418 force = nvlist_exists(innvl, "force");
e8cf3a4f 5419 heal = nvlist_exists(innvl, "heal");
43e52edd
BB
5420 resumable = nvlist_exists(innvl, "resumable");
5421
a3eeab2d 5422 /* we still use "props" here for backwards compatibility */
5423 error = nvlist_lookup_nvlist(innvl, "props", &recvprops);
43e52edd
BB
5424 if (error && error != ENOENT)
5425 return (error);
5426
a3eeab2d 5427 error = nvlist_lookup_nvlist(innvl, "localprops", &localprops);
5428 if (error && error != ENOENT)
5429 return (error);
5430
d9c460a0
TC
5431 error = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
5432 if (error && error != ENOENT)
5433 return (error);
5434
a3eeab2d 5435 error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops,
e8cf3a4f 5436 hidden_args, force, heal, resumable, input_fd, begin_record,
196bee4c 5437 &read_bytes, &errflags, &errors);
43e52edd
BB
5438
5439 fnvlist_add_uint64(outnvl, "read_bytes", read_bytes);
5440 fnvlist_add_uint64(outnvl, "error_flags", errflags);
43e52edd
BB
5441 fnvlist_add_nvlist(outnvl, "errors", errors);
5442
5443 nvlist_free(errors);
a3eeab2d 5444 nvlist_free(recvprops);
5445 nvlist_free(localprops);
43e52edd
BB
5446
5447 return (error);
5448}
5449
30af21b0 5450typedef struct dump_bytes_io {
da92d5cb
MM
5451 zfs_file_t *dbi_fp;
5452 caddr_t dbi_buf;
30af21b0
PD
5453 int dbi_len;
5454 int dbi_err;
5455} dump_bytes_io_t;
5456
5457static void
5458dump_bytes_cb(void *arg)
5459{
5460 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
da92d5cb
MM
5461 zfs_file_t *fp;
5462 caddr_t buf;
5463
5464 fp = dbi->dbi_fp;
5465 buf = dbi->dbi_buf;
30af21b0 5466
da92d5cb 5467 dbi->dbi_err = zfs_file_write(fp, buf, dbi->dbi_len, NULL);
30af21b0
PD
5468}
5469
5470static int
5471dump_bytes(objset_t *os, void *buf, int len, void *arg)
5472{
5473 dump_bytes_io_t dbi;
5474
da92d5cb 5475 dbi.dbi_fp = arg;
30af21b0
PD
5476 dbi.dbi_buf = buf;
5477 dbi.dbi_len = len;
5478
5479#if defined(HAVE_LARGE_STACKS)
5480 dump_bytes_cb(&dbi);
5481#else
5482 /*
5483 * The vn_rdwr() call is performed in a taskq to ensure that there is
5484 * always enough stack space to write safely to the target filesystem.
5485 * The ZIO_TYPE_FREE threads are used because there can be a lot of
5486 * them and they are used in vdev_file.c for a similar purpose.
5487 */
5488 spa_taskq_dispatch_sync(dmu_objset_spa(os), ZIO_TYPE_FREE,
5489 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
5490#endif /* HAVE_LARGE_STACKS */
5491
5492 return (dbi.dbi_err);
5493}
5494
34dc7c2f
BB
5495/*
5496 * inputs:
5497 * zc_name name of snapshot to send
34dc7c2f 5498 * zc_cookie file descriptor to send stream to
572e2857
BB
5499 * zc_obj fromorigin flag (mutually exclusive with zc_fromobj)
5500 * zc_sendobj objsetid of snapshot to send
5501 * zc_fromobj objsetid of incremental fromsnap (may be zero)
330d06f9
MA
5502 * zc_guid if set, estimate size of stream only. zc_cookie is ignored.
5503 * output size in zc_objset_type.
f1512ee6 5504 * zc_flags lzc_send_flags
34dc7c2f 5505 *
da536844
MA
5506 * outputs:
5507 * zc_objset_type estimated size, if zc_guid is set
cf7684bc 5508 *
5509 * NOTE: This is no longer the preferred interface, any new functionality
5510 * should be added to zfs_ioc_send_new() instead.
34dc7c2f
BB
5511 */
5512static int
5513zfs_ioc_send(zfs_cmd_t *zc)
5514{
34dc7c2f
BB
5515 int error;
5516 offset_t off;
330d06f9 5517 boolean_t estimate = (zc->zc_guid != 0);
9b67f605 5518 boolean_t embedok = (zc->zc_flags & 0x1);
f1512ee6 5519 boolean_t large_block_ok = (zc->zc_flags & 0x2);
2aa34383 5520 boolean_t compressok = (zc->zc_flags & 0x4);
b5256303 5521 boolean_t rawok = (zc->zc_flags & 0x8);
ba0ba69e 5522 boolean_t savedok = (zc->zc_flags & 0x10);
34dc7c2f 5523
13fe0198
MA
5524 if (zc->zc_obj != 0) {
5525 dsl_pool_t *dp;
5526 dsl_dataset_t *tosnap;
34dc7c2f 5527
13fe0198
MA
5528 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5529 if (error != 0)
572e2857 5530 return (error);
13fe0198
MA
5531
5532 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap);
5533 if (error != 0) {
5534 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
5535 return (error);
5536 }
13fe0198
MA
5537
5538 if (dsl_dir_is_clone(tosnap->ds_dir))
d683ddbb
JG
5539 zc->zc_fromobj =
5540 dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj;
13fe0198
MA
5541 dsl_dataset_rele(tosnap, FTAG);
5542 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
5543 }
5544
13fe0198
MA
5545 if (estimate) {
5546 dsl_pool_t *dp;
5547 dsl_dataset_t *tosnap;
5548 dsl_dataset_t *fromsnap = NULL;
5549
5550 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5551 if (error != 0)
5552 return (error);
6f1ffb06 5553
b5256303
TC
5554 error = dsl_dataset_hold_obj(dp, zc->zc_sendobj,
5555 FTAG, &tosnap);
13fe0198
MA
5556 if (error != 0) {
5557 dsl_pool_rele(dp, FTAG);
5558 return (error);
6f1ffb06
MA
5559 }
5560
13fe0198
MA
5561 if (zc->zc_fromobj != 0) {
5562 error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5563 FTAG, &fromsnap);
5564 if (error != 0) {
5565 dsl_dataset_rele(tosnap, FTAG);
5566 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
5567 return (error);
5568 }
5569 }
34dc7c2f 5570
30af21b0 5571 error = dmu_send_estimate_fast(tosnap, fromsnap, NULL,
ba0ba69e 5572 compressok || rawok, savedok, &zc->zc_objset_type);
13fe0198
MA
5573
5574 if (fromsnap != NULL)
5575 dsl_dataset_rele(fromsnap, FTAG);
5576 dsl_dataset_rele(tosnap, FTAG);
5577 dsl_pool_rele(dp, FTAG);
330d06f9 5578 } else {
da92d5cb 5579 zfs_file_t *fp;
30af21b0 5580 dmu_send_outparams_t out = {0};
da92d5cb 5581
958826be
GW
5582 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
5583 return (SET_ERROR(EBADF));
da92d5cb
MM
5584
5585 off = zfs_file_off(fp);
30af21b0 5586 out.dso_outfunc = dump_bytes;
da92d5cb 5587 out.dso_arg = fp;
30af21b0 5588 out.dso_dryrun = B_FALSE;
13fe0198 5589 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
ba0ba69e
TC
5590 zc->zc_fromobj, embedok, large_block_ok, compressok,
5591 rawok, savedok, zc->zc_cookie, &off, &out);
34dc7c2f 5592
958826be 5593 zfs_file_put(fp);
330d06f9 5594 }
34dc7c2f
BB
5595 return (error);
5596}
5597
37abac6d
BP
5598/*
5599 * inputs:
30af21b0
PD
5600 * zc_name name of snapshot on which to report progress
5601 * zc_cookie file descriptor of send stream
37abac6d
BP
5602 *
5603 * outputs:
30af21b0
PD
5604 * zc_cookie number of bytes written in send stream thus far
5605 * zc_objset_type logical size of data traversed by send thus far
37abac6d
BP
5606 */
5607static int
5608zfs_ioc_send_progress(zfs_cmd_t *zc)
5609{
13fe0198 5610 dsl_pool_t *dp;
37abac6d 5611 dsl_dataset_t *ds;
30af21b0 5612 dmu_sendstatus_t *dsp = NULL;
37abac6d
BP
5613 int error;
5614
13fe0198
MA
5615 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5616 if (error != 0)
37abac6d
BP
5617 return (error);
5618
13fe0198
MA
5619 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5620 if (error != 0) {
5621 dsl_pool_rele(dp, FTAG);
5622 return (error);
5623 }
5624
37abac6d
BP
5625 mutex_enter(&ds->ds_sendstream_lock);
5626
5627 /*
5628 * Iterate over all the send streams currently active on this dataset.
5629 * If there's one which matches the specified file descriptor _and_ the
5630 * stream was started by the current process, return the progress of
5631 * that stream.
5632 */
5633
5634 for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL;
5635 dsp = list_next(&ds->ds_sendstreams, dsp)) {
30af21b0 5636 if (dsp->dss_outfd == zc->zc_cookie &&
1f043c8b 5637 zfs_proc_is_caller(dsp->dss_proc))
37abac6d
BP
5638 break;
5639 }
5640
30af21b0
PD
5641 if (dsp != NULL) {
5642 zc->zc_cookie = atomic_cas_64((volatile uint64_t *)dsp->dss_off,
5643 0, 0);
5644 /* This is the closest thing we have to atomic_read_64. */
5645 zc->zc_objset_type = atomic_cas_64(&dsp->dss_blocks, 0, 0);
5646 } else {
2e528b49 5647 error = SET_ERROR(ENOENT);
30af21b0 5648 }
37abac6d
BP
5649
5650 mutex_exit(&ds->ds_sendstream_lock);
5651 dsl_dataset_rele(ds, FTAG);
13fe0198 5652 dsl_pool_rele(dp, FTAG);
37abac6d
BP
5653 return (error);
5654}
5655
34dc7c2f
BB
5656static int
5657zfs_ioc_inject_fault(zfs_cmd_t *zc)
5658{
5659 int id, error;
5660
5661 error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id,
5662 &zc->zc_inject_record);
5663
5664 if (error == 0)
5665 zc->zc_guid = (uint64_t)id;
5666
5667 return (error);
5668}
5669
5670static int
5671zfs_ioc_clear_fault(zfs_cmd_t *zc)
5672{
5673 return (zio_clear_fault((int)zc->zc_guid));
5674}
5675
5676static int
5677zfs_ioc_inject_list_next(zfs_cmd_t *zc)
5678{
5679 int id = (int)zc->zc_guid;
5680 int error;
5681
5682 error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name),
5683 &zc->zc_inject_record);
5684
5685 zc->zc_guid = id;
5686
5687 return (error);
5688}
5689
5690static int
5691zfs_ioc_error_log(zfs_cmd_t *zc)
5692{
5693 spa_t *spa;
5694 int error;
0409d332 5695 uint64_t count = zc->zc_nvlist_dst_size;
34dc7c2f
BB
5696
5697 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
5698 return (error);
5699
5700 error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst,
5701 &count);
5702 if (error == 0)
5703 zc->zc_nvlist_dst_size = count;
5704 else
5705 zc->zc_nvlist_dst_size = spa_get_errlog_size(spa);
5706
5707 spa_close(spa, FTAG);
5708
5709 return (error);
5710}
5711
5712static int
5713zfs_ioc_clear(zfs_cmd_t *zc)
5714{
5715 spa_t *spa;
5716 vdev_t *vd;
34dc7c2f
BB
5717 int error;
5718
34dc7c2f 5719 /*
b128c09f 5720 * On zpool clear we also fix up missing slogs
34dc7c2f 5721 */
b128c09f
BB
5722 mutex_enter(&spa_namespace_lock);
5723 spa = spa_lookup(zc->zc_name);
5724 if (spa == NULL) {
5725 mutex_exit(&spa_namespace_lock);
2e528b49 5726 return (SET_ERROR(EIO));
b128c09f 5727 }
428870ff 5728 if (spa_get_log_state(spa) == SPA_LOG_MISSING) {
b128c09f 5729 /* we need to let spa_open/spa_load clear the chains */
428870ff 5730 spa_set_log_state(spa, SPA_LOG_CLEAR);
34dc7c2f 5731 }
428870ff 5732 spa->spa_last_open_failed = 0;
b128c09f 5733 mutex_exit(&spa_namespace_lock);
34dc7c2f 5734
428870ff
BB
5735 if (zc->zc_cookie & ZPOOL_NO_REWIND) {
5736 error = spa_open(zc->zc_name, &spa, FTAG);
5737 } else {
5738 nvlist_t *policy;
5739 nvlist_t *config = NULL;
5740
b8864a23 5741 if (zc->zc_nvlist_src == 0)
2e528b49 5742 return (SET_ERROR(EINVAL));
428870ff
BB
5743
5744 if ((error = get_nvlist(zc->zc_nvlist_src,
5745 zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) {
5746 error = spa_open_rewind(zc->zc_name, &spa, FTAG,
5747 policy, &config);
5748 if (config != NULL) {
572e2857
BB
5749 int err;
5750
5751 if ((err = put_nvlist(zc, config)) != 0)
5752 error = err;
428870ff
BB
5753 nvlist_free(config);
5754 }
5755 nvlist_free(policy);
5756 }
5757 }
5758
13fe0198 5759 if (error != 0)
b128c09f
BB
5760 return (error);
5761
8133679f
OF
5762 /*
5763 * If multihost is enabled, resuming I/O is unsafe as another
5764 * host may have imported the pool.
5765 */
5766 if (spa_multihost(spa) && spa_suspended(spa))
5767 return (SET_ERROR(EINVAL));
5768
428870ff 5769 spa_vdev_state_enter(spa, SCL_NONE);
34dc7c2f
BB
5770
5771 if (zc->zc_guid == 0) {
5772 vd = NULL;
b128c09f
BB
5773 } else {
5774 vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE);
34dc7c2f 5775 if (vd == NULL) {
28caa74b
MM
5776 error = SET_ERROR(ENODEV);
5777 (void) spa_vdev_state_exit(spa, NULL, error);
34dc7c2f 5778 spa_close(spa, FTAG);
28caa74b 5779 return (error);
34dc7c2f
BB
5780 }
5781 }
5782
b128c09f
BB
5783 vdev_clear(spa, vd);
5784
3f759c0c
BB
5785 (void) spa_vdev_state_exit(spa, spa_suspended(spa) ?
5786 NULL : spa->spa_root_vdev, 0);
34dc7c2f 5787
b128c09f
BB
5788 /*
5789 * Resume any suspended I/Os.
5790 */
9babb374 5791 if (zio_resume(spa) != 0)
2e528b49 5792 error = SET_ERROR(EIO);
34dc7c2f
BB
5793
5794 spa_close(spa, FTAG);
5795
9babb374 5796 return (error);
34dc7c2f
BB
5797}
5798
d3f2cd7e
AB
5799/*
5800 * Reopen all the vdevs associated with the pool.
5801 *
5802 * innvl: {
5803 * "scrub_restart" -> when true and scrub is running, allow to restart
5804 * scrub as the side effect of the reopen (boolean).
5805 * }
5806 *
5807 * outnvl is unused
5808 */
b83a0e2d 5809static const zfs_ioc_key_t zfs_keys_pool_reopen[] = {
7bb0c294 5810 {"scrub_restart", DATA_TYPE_BOOLEAN_VALUE, ZK_OPTIONAL},
b83a0e2d
DB
5811};
5812
1bd201e7 5813static int
d3f2cd7e 5814zfs_ioc_pool_reopen(const char *pool, nvlist_t *innvl, nvlist_t *outnvl)
1bd201e7 5815{
ef70eff1 5816 (void) outnvl;
1bd201e7
CS
5817 spa_t *spa;
5818 int error;
7bb0c294 5819 boolean_t rc, scrub_restart = B_TRUE;
1bd201e7 5820
d3f2cd7e 5821 if (innvl) {
7bb0c294
MM
5822 error = nvlist_lookup_boolean_value(innvl,
5823 "scrub_restart", &rc);
5824 if (error == 0)
5825 scrub_restart = rc;
d3f2cd7e
AB
5826 }
5827
5828 error = spa_open(pool, &spa, FTAG);
13fe0198 5829 if (error != 0)
1bd201e7
CS
5830 return (error);
5831
5832 spa_vdev_state_enter(spa, SCL_NONE);
65947351
GW
5833
5834 /*
d3f2cd7e
AB
5835 * If the scrub_restart flag is B_FALSE and a scrub is already
5836 * in progress then set spa_scrub_reopen flag to B_TRUE so that
5837 * we don't restart the scrub as a side effect of the reopen.
5838 * Otherwise, let vdev_open() decided if a resilver is required.
65947351 5839 */
d3f2cd7e
AB
5840
5841 spa->spa_scrub_reopen = (!scrub_restart &&
5842 dsl_scan_scrubbing(spa->spa_dsl_pool));
1bd201e7 5843 vdev_reopen(spa->spa_root_vdev);
65947351
GW
5844 spa->spa_scrub_reopen = B_FALSE;
5845
1bd201e7
CS
5846 (void) spa_vdev_state_exit(spa, NULL, 0);
5847 spa_close(spa, FTAG);
5848 return (0);
5849}
d3f2cd7e 5850
34dc7c2f
BB
5851/*
5852 * inputs:
5853 * zc_name name of filesystem
34dc7c2f 5854 *
428870ff
BB
5855 * outputs:
5856 * zc_string name of conflicting snapshot, if there is one
34dc7c2f
BB
5857 */
5858static int
5859zfs_ioc_promote(zfs_cmd_t *zc)
5860{
d12f91fd
GDN
5861 dsl_pool_t *dp;
5862 dsl_dataset_t *ds, *ods;
5863 char origin[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 5864 char *cp;
d12f91fd
GDN
5865 int error;
5866
650258d7 5867 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
5868 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
5869 strchr(zc->zc_name, '%'))
5870 return (SET_ERROR(EINVAL));
5871
d12f91fd
GDN
5872 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5873 if (error != 0)
5874 return (error);
5875
5876 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5877 if (error != 0) {
5878 dsl_pool_rele(dp, FTAG);
5879 return (error);
5880 }
5881
5882 if (!dsl_dir_is_clone(ds->ds_dir)) {
5883 dsl_dataset_rele(ds, FTAG);
5884 dsl_pool_rele(dp, FTAG);
5885 return (SET_ERROR(EINVAL));
5886 }
5887
5888 error = dsl_dataset_hold_obj(dp,
5889 dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5890 if (error != 0) {
5891 dsl_dataset_rele(ds, FTAG);
5892 dsl_pool_rele(dp, FTAG);
5893 return (error);
5894 }
5895
5896 dsl_dataset_name(ods, origin);
5897 dsl_dataset_rele(ods, FTAG);
5898 dsl_dataset_rele(ds, FTAG);
5899 dsl_pool_rele(dp, FTAG);
34dc7c2f
BB
5900
5901 /*
5902 * We don't need to unmount *all* the origin fs's snapshots, but
5903 * it's easier.
5904 */
d12f91fd 5905 cp = strchr(origin, '@');
34dc7c2f
BB
5906 if (cp)
5907 *cp = '\0';
d12f91fd 5908 (void) dmu_objset_find(origin,
13fe0198 5909 zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
428870ff 5910 return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
34dc7c2f
BB
5911}
5912
9babb374 5913/*
9c5167d1 5914 * Retrieve a single {user|group|project}{used|quota}@... property.
9babb374
BB
5915 *
5916 * inputs:
5917 * zc_name name of filesystem
5918 * zc_objset_type zfs_userquota_prop_t
5919 * zc_value domain name (eg. "S-1-234-567-89")
5920 * zc_guid RID/UID/GID
5921 *
5922 * outputs:
5923 * zc_cookie property value
5924 */
5925static int
5926zfs_ioc_userspace_one(zfs_cmd_t *zc)
5927{
0037b49e 5928 zfsvfs_t *zfsvfs;
9babb374
BB
5929 int error;
5930
5931 if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS)
2e528b49 5932 return (SET_ERROR(EINVAL));
9babb374 5933
f298b24d 5934 error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
13fe0198 5935 if (error != 0)
9babb374
BB
5936 return (error);
5937
0037b49e 5938 error = zfs_userspace_one(zfsvfs,
9babb374 5939 zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie);
f298b24d 5940 zfsvfs_rele(zfsvfs, FTAG);
9babb374
BB
5941
5942 return (error);
5943}
5944
5945/*
5946 * inputs:
5947 * zc_name name of filesystem
5948 * zc_cookie zap cursor
5949 * zc_objset_type zfs_userquota_prop_t
5950 * zc_nvlist_dst[_size] buffer to fill (not really an nvlist)
5951 *
5952 * outputs:
5953 * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t)
5954 * zc_cookie zap cursor
5955 */
5956static int
5957zfs_ioc_userspace_many(zfs_cmd_t *zc)
5958{
0037b49e 5959 zfsvfs_t *zfsvfs;
428870ff
BB
5960 int bufsize = zc->zc_nvlist_dst_size;
5961
5962 if (bufsize <= 0)
2e528b49 5963 return (SET_ERROR(ENOMEM));
9babb374 5964
1c27024e 5965 int error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE);
13fe0198 5966 if (error != 0)
9babb374
BB
5967 return (error);
5968
1c27024e 5969 void *buf = vmem_alloc(bufsize, KM_SLEEP);
9babb374 5970
0037b49e 5971 error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie,
9babb374
BB
5972 buf, &zc->zc_nvlist_dst_size);
5973
5974 if (error == 0) {
5975 error = xcopyout(buf,
5976 (void *)(uintptr_t)zc->zc_nvlist_dst,
5977 zc->zc_nvlist_dst_size);
5978 }
2b8cad61 5979 vmem_free(buf, bufsize);
f298b24d 5980 zfsvfs_rele(zfsvfs, FTAG);
9babb374
BB
5981
5982 return (error);
5983}
5984
5985/*
5986 * inputs:
5987 * zc_name name of filesystem
5988 *
5989 * outputs:
5990 * none
5991 */
5992static int
5993zfs_ioc_userspace_upgrade(zfs_cmd_t *zc)
5994{
428870ff 5995 int error = 0;
0037b49e 5996 zfsvfs_t *zfsvfs;
9babb374 5997
f298b24d 5998 if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) {
0037b49e 5999 if (!dmu_objset_userused_enabled(zfsvfs->z_os)) {
9babb374
BB
6000 /*
6001 * If userused is not enabled, it may be because the
6002 * objset needs to be closed & reopened (to grow the
6003 * objset_phys_t). Suspend/resume the fs will do that.
6004 */
5e00213e 6005 dsl_dataset_t *ds, *newds;
ec923db2 6006
0037b49e
BB
6007 ds = dmu_objset_ds(zfsvfs->z_os);
6008 error = zfs_suspend_fs(zfsvfs);
831baf06 6009 if (error == 0) {
5e00213e 6010 dmu_objset_refresh_ownership(ds, &newds,
b5256303 6011 B_TRUE, zfsvfs);
5e00213e 6012 error = zfs_resume_fs(zfsvfs, newds);
831baf06 6013 }
9babb374 6014 }
4072f465 6015 if (error == 0) {
6016 mutex_enter(&zfsvfs->z_os->os_upgrade_lock);
6017 if (zfsvfs->z_os->os_upgrade_id == 0) {
6018 /* clear potential error code and retry */
6019 zfsvfs->z_os->os_upgrade_status = 0;
6020 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6021
6022 dsl_pool_config_enter(
6023 dmu_objset_pool(zfsvfs->z_os), FTAG);
6024 dmu_objset_userspace_upgrade(zfsvfs->z_os);
6025 dsl_pool_config_exit(
6026 dmu_objset_pool(zfsvfs->z_os), FTAG);
6027 } else {
6028 mutex_exit(&zfsvfs->z_os->os_upgrade_lock);
6029 }
6030
6031 taskq_wait_id(zfsvfs->z_os->os_spa->spa_upgrade_taskq,
6032 zfsvfs->z_os->os_upgrade_id);
6033 error = zfsvfs->z_os->os_upgrade_status;
6034 }
362ae8d1 6035 zfs_vfs_rele(zfsvfs);
9babb374 6036 } else {
4072f465 6037 objset_t *os;
6038
428870ff 6039 /* XXX kind of reading contents without owning */
b5256303 6040 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
13fe0198 6041 if (error != 0)
9babb374
BB
6042 return (error);
6043
4072f465 6044 mutex_enter(&os->os_upgrade_lock);
6045 if (os->os_upgrade_id == 0) {
6046 /* clear potential error code and retry */
6047 os->os_upgrade_status = 0;
6048 mutex_exit(&os->os_upgrade_lock);
6049
6050 dmu_objset_userspace_upgrade(os);
6051 } else {
6052 mutex_exit(&os->os_upgrade_lock);
6053 }
9babb374 6054
4072f465 6055 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6056
6057 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6058 error = os->os_upgrade_status;
6059
6060 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT,
6061 FTAG);
6062 }
9babb374
BB
6063 return (error);
6064}
6065
1de321e6
JX
6066/*
6067 * inputs:
6068 * zc_name name of filesystem
6069 *
6070 * outputs:
6071 * none
6072 */
6073static int
9c5167d1 6074zfs_ioc_id_quota_upgrade(zfs_cmd_t *zc)
1de321e6
JX
6075{
6076 objset_t *os;
6077 int error;
6078
b5256303 6079 error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os);
1de321e6
JX
6080 if (error != 0)
6081 return (error);
6082
9c5167d1
NF
6083 if (dmu_objset_userobjspace_upgradable(os) ||
6084 dmu_objset_projectquota_upgradable(os)) {
1de321e6
JX
6085 mutex_enter(&os->os_upgrade_lock);
6086 if (os->os_upgrade_id == 0) {
6087 /* clear potential error code and retry */
6088 os->os_upgrade_status = 0;
6089 mutex_exit(&os->os_upgrade_lock);
6090
9c5167d1 6091 dmu_objset_id_quota_upgrade(os);
1de321e6
JX
6092 } else {
6093 mutex_exit(&os->os_upgrade_lock);
6094 }
6095
c0daec32
AB
6096 dsl_pool_rele(dmu_objset_pool(os), FTAG);
6097
1de321e6
JX
6098 taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id);
6099 error = os->os_upgrade_status;
c0daec32
AB
6100 } else {
6101 dsl_pool_rele(dmu_objset_pool(os), FTAG);
1de321e6
JX
6102 }
6103
b5256303 6104 dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG);
1de321e6
JX
6105
6106 return (error);
6107}
6108
34dc7c2f
BB
6109static int
6110zfs_ioc_share(zfs_cmd_t *zc)
6111{
2e528b49 6112 return (SET_ERROR(ENOSYS));
34dc7c2f
BB
6113}
6114
572e2857
BB
6115/*
6116 * inputs:
6117 * zc_name name of containing filesystem
6118 * zc_obj object # beyond which we want next in-use object #
6119 *
6120 * outputs:
6121 * zc_obj next in-use object #
6122 */
6123static int
6124zfs_ioc_next_obj(zfs_cmd_t *zc)
6125{
6126 objset_t *os = NULL;
6127 int error;
6128
6129 error = dmu_objset_hold(zc->zc_name, FTAG, &os);
13fe0198 6130 if (error != 0)
572e2857
BB
6131 return (error);
6132
7290cd3c 6133 error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0);
572e2857
BB
6134
6135 dmu_objset_rele(os, FTAG);
6136 return (error);
6137}
6138
6139/*
6140 * inputs:
6141 * zc_name name of filesystem
6142 * zc_value prefix name for snapshot
6143 * zc_cleanup_fd cleanup-on-exit file descriptor for calling process
6144 *
6145 * outputs:
6f1ffb06 6146 * zc_value short name of new snapshot
572e2857
BB
6147 */
6148static int
6149zfs_ioc_tmp_snapshot(zfs_cmd_t *zc)
6150{
6151 char *snap_name;
13fe0198 6152 char *hold_name;
13fe0198 6153 minor_t minor;
572e2857 6154
958826be
GW
6155 zfs_file_t *fp = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor);
6156 if (fp == NULL)
6157 return (SET_ERROR(EBADF));
572e2857 6158
13fe0198
MA
6159 snap_name = kmem_asprintf("%s-%016llx", zc->zc_value,
6160 (u_longlong_t)ddi_get_lbolt64());
6161 hold_name = kmem_asprintf("%%%s", zc->zc_value);
6162
958826be 6163 int error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor,
13fe0198
MA
6164 hold_name);
6165 if (error == 0)
680eada9 6166 (void) strlcpy(zc->zc_value, snap_name,
6167 sizeof (zc->zc_value));
e4f5fa12
MM
6168 kmem_strfree(snap_name);
6169 kmem_strfree(hold_name);
958826be 6170 zfs_onexit_fd_rele(fp);
13fe0198 6171 return (error);
572e2857
BB
6172}
6173
6174/*
6175 * inputs:
6176 * zc_name name of "to" snapshot
6177 * zc_value name of "from" snapshot
6178 * zc_cookie file descriptor to write diff data on
6179 *
6180 * outputs:
6181 * dmu_diff_record_t's to the file descriptor
6182 */
6183static int
6184zfs_ioc_diff(zfs_cmd_t *zc)
6185{
da92d5cb 6186 zfs_file_t *fp;
572e2857
BB
6187 offset_t off;
6188 int error;
6189
958826be
GW
6190 if ((fp = zfs_file_get(zc->zc_cookie)) == NULL)
6191 return (SET_ERROR(EBADF));
572e2857 6192
da92d5cb
MM
6193 off = zfs_file_off(fp);
6194 error = dmu_diff(zc->zc_name, zc->zc_value, fp, &off);
572e2857 6195
958826be 6196 zfs_file_put(fp);
572e2857 6197
572e2857
BB
6198 return (error);
6199}
6200
9babb374
BB
6201static int
6202zfs_ioc_smb_acl(zfs_cmd_t *zc)
6203{
2e528b49 6204 return (SET_ERROR(ENOTSUP));
9babb374
BB
6205}
6206
45d1cae3 6207/*
13fe0198
MA
6208 * innvl: {
6209 * "holds" -> { snapname -> holdname (string), ... }
6210 * (optional) "cleanup_fd" -> fd (int32)
6211 * }
45d1cae3 6212 *
13fe0198
MA
6213 * outnvl: {
6214 * snapname -> error value (int32)
6215 * ...
6216 * }
45d1cae3 6217 */
b83a0e2d
DB
6218static const zfs_ioc_key_t zfs_keys_hold[] = {
6219 {"holds", DATA_TYPE_NVLIST, 0},
6220 {"cleanup_fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6221};
6222
45d1cae3 6223static int
13fe0198 6224zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist)
45d1cae3 6225{
ef70eff1 6226 (void) pool;
fc581e05 6227 nvpair_t *pair;
13fe0198
MA
6228 nvlist_t *holds;
6229 int cleanup_fd = -1;
572e2857
BB
6230 int error;
6231 minor_t minor = 0;
958826be 6232 zfs_file_t *fp = NULL;
45d1cae3 6233
b83a0e2d 6234 holds = fnvlist_lookup_nvlist(args, "holds");
572e2857 6235
fc581e05
JJS
6236 /* make sure the user didn't pass us any invalid (empty) tags */
6237 for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
6238 pair = nvlist_next_nvpair(holds, pair)) {
6239 char *htag;
6240
6241 error = nvpair_value_string(pair, &htag);
6242 if (error != 0)
6243 return (SET_ERROR(error));
6244
6245 if (strlen(htag) == 0)
6246 return (SET_ERROR(EINVAL));
6247 }
6248
13fe0198 6249 if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) {
958826be
GW
6250 fp = zfs_onexit_fd_hold(cleanup_fd, &minor);
6251 if (fp == NULL)
6252 return (SET_ERROR(EBADF));
572e2857 6253 }
572e2857 6254
13fe0198 6255 error = dsl_dataset_user_hold(holds, minor, errlist);
958826be
GW
6256 if (fp != NULL) {
6257 ASSERT3U(minor, !=, 0);
6258 zfs_onexit_fd_rele(fp);
6259 }
7bb0c294 6260 return (SET_ERROR(error));
45d1cae3
BB
6261}
6262
6263/*
13fe0198 6264 * innvl is not used.
45d1cae3 6265 *
13fe0198
MA
6266 * outnvl: {
6267 * holdname -> time added (uint64 seconds since epoch)
6268 * ...
6269 * }
45d1cae3 6270 */
b83a0e2d
DB
6271static const zfs_ioc_key_t zfs_keys_get_holds[] = {
6272 /* no nvl keys */
6273};
6274
45d1cae3 6275static int
13fe0198 6276zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl)
45d1cae3 6277{
ef70eff1 6278 (void) args;
13fe0198 6279 return (dsl_dataset_get_holds(snapname, outnvl));
45d1cae3
BB
6280}
6281
6282/*
13fe0198
MA
6283 * innvl: {
6284 * snapname -> { holdname, ... }
6285 * ...
6286 * }
45d1cae3 6287 *
13fe0198
MA
6288 * outnvl: {
6289 * snapname -> error value (int32)
6290 * ...
6291 * }
45d1cae3 6292 */
b83a0e2d
DB
6293static const zfs_ioc_key_t zfs_keys_release[] = {
6294 {"<snapname>...", DATA_TYPE_NVLIST, ZK_WILDCARDLIST},
6295};
6296
45d1cae3 6297static int
13fe0198 6298zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
45d1cae3 6299{
ef70eff1 6300 (void) pool;
13fe0198 6301 return (dsl_dataset_user_release(holds, errlist));
45d1cae3
BB
6302}
6303
26685276
BB
6304/*
6305 * inputs:
6306 * zc_guid flags (ZEVENT_NONBLOCK)
9b101a73 6307 * zc_cleanup_fd zevent file descriptor
26685276
BB
6308 *
6309 * outputs:
6310 * zc_nvlist_dst next nvlist event
6311 * zc_cookie dropped events since last get
26685276
BB
6312 */
6313static int
6314zfs_ioc_events_next(zfs_cmd_t *zc)
6315{
6316 zfs_zevent_t *ze;
6317 nvlist_t *event = NULL;
6318 minor_t minor;
6319 uint64_t dropped = 0;
6320 int error;
6321
958826be
GW
6322 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6323 if (fp == NULL)
6324 return (SET_ERROR(EBADF));
26685276
BB
6325
6326 do {
baa40d45 6327 error = zfs_zevent_next(ze, &event,
02730c33 6328 &zc->zc_nvlist_dst_size, &dropped);
26685276
BB
6329 if (event != NULL) {
6330 zc->zc_cookie = dropped;
6331 error = put_nvlist(zc, event);
baa40d45 6332 nvlist_free(event);
26685276
BB
6333 }
6334
6335 if (zc->zc_guid & ZEVENT_NONBLOCK)
6336 break;
6337
6338 if ((error == 0) || (error != ENOENT))
6339 break;
6340
6341 error = zfs_zevent_wait(ze);
13fe0198 6342 if (error != 0)
26685276
BB
6343 break;
6344 } while (1);
6345
958826be 6346 zfs_zevent_fd_rele(fp);
26685276
BB
6347
6348 return (error);
6349}
6350
6351/*
6352 * outputs:
6353 * zc_cookie cleared events count
6354 */
6355static int
6356zfs_ioc_events_clear(zfs_cmd_t *zc)
6357{
6358 int count;
6359
6360 zfs_zevent_drain_all(&count);
6361 zc->zc_cookie = count;
6362
d1d7e268 6363 return (0);
26685276
BB
6364}
6365
75e3ff58
BB
6366/*
6367 * inputs:
6368 * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END
6369 * zc_cleanup zevent file descriptor
6370 */
6371static int
6372zfs_ioc_events_seek(zfs_cmd_t *zc)
6373{
6374 zfs_zevent_t *ze;
6375 minor_t minor;
6376 int error;
6377
958826be
GW
6378 zfs_file_t *fp = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze);
6379 if (fp == NULL)
6380 return (SET_ERROR(EBADF));
75e3ff58
BB
6381
6382 error = zfs_zevent_seek(ze, zc->zc_guid);
958826be 6383 zfs_zevent_fd_rele(fp);
75e3ff58
BB
6384
6385 return (error);
6386}
6387
330d06f9
MA
6388/*
6389 * inputs:
30af21b0
PD
6390 * zc_name name of later filesystem or snapshot
6391 * zc_value full name of old snapshot or bookmark
330d06f9
MA
6392 *
6393 * outputs:
6394 * zc_cookie space in bytes
6395 * zc_objset_type compressed space in bytes
6396 * zc_perm_action uncompressed space in bytes
6397 */
6398static int
6399zfs_ioc_space_written(zfs_cmd_t *zc)
6400{
6401 int error;
13fe0198 6402 dsl_pool_t *dp;
30af21b0 6403 dsl_dataset_t *new;
330d06f9 6404
13fe0198 6405 error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
330d06f9
MA
6406 if (error != 0)
6407 return (error);
13fe0198
MA
6408 error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new);
6409 if (error != 0) {
6410 dsl_pool_rele(dp, FTAG);
6411 return (error);
6412 }
30af21b0
PD
6413 if (strchr(zc->zc_value, '#') != NULL) {
6414 zfs_bookmark_phys_t bmp;
6415 error = dsl_bookmark_lookup(dp, zc->zc_value,
6416 new, &bmp);
6417 if (error == 0) {
6418 error = dsl_dataset_space_written_bookmark(&bmp, new,
6419 &zc->zc_cookie,
6420 &zc->zc_objset_type, &zc->zc_perm_action);
6421 }
6422 } else {
6423 dsl_dataset_t *old;
6424 error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old);
330d06f9 6425
30af21b0
PD
6426 if (error == 0) {
6427 error = dsl_dataset_space_written(old, new,
6428 &zc->zc_cookie,
6429 &zc->zc_objset_type, &zc->zc_perm_action);
6430 dsl_dataset_rele(old, FTAG);
6431 }
6432 }
330d06f9 6433 dsl_dataset_rele(new, FTAG);
13fe0198 6434 dsl_pool_rele(dp, FTAG);
330d06f9
MA
6435 return (error);
6436}
6437
6438/*
6f1ffb06
MA
6439 * innvl: {
6440 * "firstsnap" -> snapshot name
6441 * }
330d06f9 6442 *
6f1ffb06
MA
6443 * outnvl: {
6444 * "used" -> space in bytes
6445 * "compressed" -> compressed space in bytes
6446 * "uncompressed" -> uncompressed space in bytes
6447 * }
330d06f9 6448 */
b83a0e2d
DB
6449static const zfs_ioc_key_t zfs_keys_space_snaps[] = {
6450 {"firstsnap", DATA_TYPE_STRING, 0},
6451};
6452
330d06f9 6453static int
6f1ffb06 6454zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl)
330d06f9
MA
6455{
6456 int error;
13fe0198 6457 dsl_pool_t *dp;
330d06f9 6458 dsl_dataset_t *new, *old;
6f1ffb06
MA
6459 char *firstsnap;
6460 uint64_t used, comp, uncomp;
330d06f9 6461
b83a0e2d 6462 firstsnap = fnvlist_lookup_string(innvl, "firstsnap");
6f1ffb06 6463
13fe0198 6464 error = dsl_pool_hold(lastsnap, FTAG, &dp);
330d06f9
MA
6465 if (error != 0)
6466 return (error);
13fe0198
MA
6467
6468 error = dsl_dataset_hold(dp, lastsnap, FTAG, &new);
71e2fe41
AG
6469 if (error == 0 && !new->ds_is_snapshot) {
6470 dsl_dataset_rele(new, FTAG);
6471 error = SET_ERROR(EINVAL);
6472 }
13fe0198
MA
6473 if (error != 0) {
6474 dsl_pool_rele(dp, FTAG);
6475 return (error);
6476 }
6477 error = dsl_dataset_hold(dp, firstsnap, FTAG, &old);
71e2fe41
AG
6478 if (error == 0 && !old->ds_is_snapshot) {
6479 dsl_dataset_rele(old, FTAG);
6480 error = SET_ERROR(EINVAL);
6481 }
330d06f9
MA
6482 if (error != 0) {
6483 dsl_dataset_rele(new, FTAG);
13fe0198 6484 dsl_pool_rele(dp, FTAG);
330d06f9
MA
6485 return (error);
6486 }
6487
6f1ffb06 6488 error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
330d06f9
MA
6489 dsl_dataset_rele(old, FTAG);
6490 dsl_dataset_rele(new, FTAG);
13fe0198 6491 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
6492 fnvlist_add_uint64(outnvl, "used", used);
6493 fnvlist_add_uint64(outnvl, "compressed", comp);
6494 fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
330d06f9
MA
6495 return (error);
6496}
6497
34dc7c2f 6498/*
6f1ffb06
MA
6499 * innvl: {
6500 * "fd" -> file descriptor to write stream to (int32)
6501 * (optional) "fromsnap" -> full snap name to send an incremental from
f1512ee6
MA
6502 * (optional) "largeblockok" -> (value ignored)
6503 * indicates that blocks > 128KB are permitted
9b67f605
MA
6504 * (optional) "embedok" -> (value ignored)
6505 * presence indicates DRR_WRITE_EMBEDDED records are permitted
2aa34383
DK
6506 * (optional) "compressok" -> (value ignored)
6507 * presence indicates compressed DRR_WRITE records are permitted
b5256303
TC
6508 * (optional) "rawok" -> (value ignored)
6509 * presence indicates raw encrypted records should be used.
ba0ba69e
TC
6510 * (optional) "savedok" -> (value ignored)
6511 * presence indicates we should send a partially received snapshot
47dfff3b
MA
6512 * (optional) "resume_object" and "resume_offset" -> (uint64)
6513 * if present, resume send stream from specified object and offset.
30af21b0
PD
6514 * (optional) "redactbook" -> (string)
6515 * if present, use this bookmark's redaction list to generate a redacted
6516 * send stream
6f1ffb06
MA
6517 * }
6518 *
6519 * outnvl is unused
34dc7c2f 6520 */
b83a0e2d
DB
6521static const zfs_ioc_key_t zfs_keys_send_new[] = {
6522 {"fd", DATA_TYPE_INT32, 0},
6523 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6524 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6525 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6526 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6527 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
ba0ba69e 6528 {"savedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
b83a0e2d
DB
6529 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6530 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
30af21b0 6531 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
b83a0e2d
DB
6532};
6533
6f1ffb06
MA
6534static int
6535zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6536{
ef70eff1 6537 (void) outnvl;
6f1ffb06
MA
6538 int error;
6539 offset_t off;
13fe0198 6540 char *fromname = NULL;
6f1ffb06 6541 int fd;
da92d5cb 6542 zfs_file_t *fp;
f1512ee6 6543 boolean_t largeblockok;
9b67f605 6544 boolean_t embedok;
2aa34383 6545 boolean_t compressok;
b5256303 6546 boolean_t rawok;
ba0ba69e 6547 boolean_t savedok;
47dfff3b
MA
6548 uint64_t resumeobj = 0;
6549 uint64_t resumeoff = 0;
30af21b0 6550 char *redactbook = NULL;
6f1ffb06 6551
b83a0e2d 6552 fd = fnvlist_lookup_int32(innvl, "fd");
6f1ffb06 6553
13fe0198 6554 (void) nvlist_lookup_string(innvl, "fromsnap", &fromname);
6f1ffb06 6555
f1512ee6 6556 largeblockok = nvlist_exists(innvl, "largeblockok");
9b67f605 6557 embedok = nvlist_exists(innvl, "embedok");
2aa34383 6558 compressok = nvlist_exists(innvl, "compressok");
b5256303 6559 rawok = nvlist_exists(innvl, "rawok");
ba0ba69e 6560 savedok = nvlist_exists(innvl, "savedok");
9b67f605 6561
47dfff3b
MA
6562 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6563 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6564
30af21b0
PD
6565 (void) nvlist_lookup_string(innvl, "redactbook", &redactbook);
6566
958826be
GW
6567 if ((fp = zfs_file_get(fd)) == NULL)
6568 return (SET_ERROR(EBADF));
da92d5cb
MM
6569
6570 off = zfs_file_off(fp);
6f1ffb06 6571
30af21b0
PD
6572 dmu_send_outparams_t out = {0};
6573 out.dso_outfunc = dump_bytes;
da92d5cb 6574 out.dso_arg = fp;
30af21b0 6575 out.dso_dryrun = B_FALSE;
ba0ba69e
TC
6576 error = dmu_send(snapname, fromname, embedok, largeblockok,
6577 compressok, rawok, savedok, resumeobj, resumeoff,
6578 redactbook, fd, &off, &out);
6f1ffb06 6579
958826be 6580 zfs_file_put(fp);
6f1ffb06
MA
6581 return (error);
6582}
6583
65c7cc49 6584static int
30af21b0
PD
6585send_space_sum(objset_t *os, void *buf, int len, void *arg)
6586{
ef70eff1 6587 (void) os, (void) buf;
30af21b0 6588 uint64_t *size = arg;
ef70eff1 6589
30af21b0
PD
6590 *size += len;
6591 return (0);
6592}
6593
6f1ffb06
MA
6594/*
6595 * Determine approximately how large a zfs send stream will be -- the number
6596 * of bytes that will be written to the fd supplied to zfs_ioc_send_new().
6597 *
6598 * innvl: {
5dc8b736
MG
6599 * (optional) "from" -> full snap or bookmark name to send an incremental
6600 * from
2aa34383
DK
6601 * (optional) "largeblockok" -> (value ignored)
6602 * indicates that blocks > 128KB are permitted
6603 * (optional) "embedok" -> (value ignored)
6604 * presence indicates DRR_WRITE_EMBEDDED records are permitted
6605 * (optional) "compressok" -> (value ignored)
6606 * presence indicates compressed DRR_WRITE records are permitted
5b525165 6607 * (optional) "rawok" -> (value ignored)
cf7684bc 6608 * presence indicates raw encrypted records should be used.
5b525165
JP
6609 * (optional) "resume_object" and "resume_offset" -> (uint64)
6610 * if present, resume send stream from specified object and offset.
30af21b0
PD
6611 * (optional) "fd" -> file descriptor to use as a cookie for progress
6612 * tracking (int32)
6f1ffb06
MA
6613 * }
6614 *
6615 * outnvl: {
6616 * "space" -> bytes of space (uint64)
6617 * }
6618 */
b83a0e2d
DB
6619static const zfs_ioc_key_t zfs_keys_send_space[] = {
6620 {"from", DATA_TYPE_STRING, ZK_OPTIONAL},
6621 {"fromsnap", DATA_TYPE_STRING, ZK_OPTIONAL},
6622 {"largeblockok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6623 {"embedok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6624 {"compressok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6625 {"rawok", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
30af21b0
PD
6626 {"fd", DATA_TYPE_INT32, ZK_OPTIONAL},
6627 {"redactbook", DATA_TYPE_STRING, ZK_OPTIONAL},
5b525165
JP
6628 {"resume_object", DATA_TYPE_UINT64, ZK_OPTIONAL},
6629 {"resume_offset", DATA_TYPE_UINT64, ZK_OPTIONAL},
6630 {"bytes", DATA_TYPE_UINT64, ZK_OPTIONAL},
b83a0e2d
DB
6631};
6632
6f1ffb06
MA
6633static int
6634zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6635{
13fe0198 6636 dsl_pool_t *dp;
13fe0198 6637 dsl_dataset_t *tosnap;
30af21b0 6638 dsl_dataset_t *fromsnap = NULL;
6f1ffb06 6639 int error;
30af21b0
PD
6640 char *fromname = NULL;
6641 char *redactlist_book = NULL;
6642 boolean_t largeblockok;
6643 boolean_t embedok;
2aa34383 6644 boolean_t compressok;
b5256303 6645 boolean_t rawok;
ba0ba69e 6646 boolean_t savedok;
30af21b0
PD
6647 uint64_t space = 0;
6648 boolean_t full_estimate = B_FALSE;
6649 uint64_t resumeobj = 0;
6650 uint64_t resumeoff = 0;
6651 uint64_t resume_bytes = 0;
6652 int32_t fd = -1;
6653 zfs_bookmark_phys_t zbm = {0};
6f1ffb06 6654
13fe0198
MA
6655 error = dsl_pool_hold(snapname, FTAG, &dp);
6656 if (error != 0)
6f1ffb06
MA
6657 return (error);
6658
13fe0198
MA
6659 error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap);
6660 if (error != 0) {
6661 dsl_pool_rele(dp, FTAG);
6662 return (error);
6663 }
30af21b0 6664 (void) nvlist_lookup_int32(innvl, "fd", &fd);
13fe0198 6665
30af21b0
PD
6666 largeblockok = nvlist_exists(innvl, "largeblockok");
6667 embedok = nvlist_exists(innvl, "embedok");
2aa34383 6668 compressok = nvlist_exists(innvl, "compressok");
b5256303 6669 rawok = nvlist_exists(innvl, "rawok");
ba0ba69e 6670 savedok = nvlist_exists(innvl, "savedok");
30af21b0
PD
6671 boolean_t from = (nvlist_lookup_string(innvl, "from", &fromname) == 0);
6672 boolean_t altbook = (nvlist_lookup_string(innvl, "redactbook",
6673 &redactlist_book) == 0);
6674
6675 (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj);
6676 (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff);
6677 (void) nvlist_lookup_uint64(innvl, "bytes", &resume_bytes);
6678
6679 if (altbook) {
6680 full_estimate = B_TRUE;
6681 } else if (from) {
6682 if (strchr(fromname, '#')) {
6683 error = dsl_bookmark_lookup(dp, fromname, tosnap, &zbm);
2aa34383 6684
5dc8b736 6685 /*
30af21b0
PD
6686 * dsl_bookmark_lookup() will fail with EXDEV if
6687 * the from-bookmark and tosnap are at the same txg.
6688 * However, it's valid to do a send (and therefore,
6689 * a send estimate) from and to the same time point,
6690 * if the bookmark is redacted (the incremental send
6691 * can change what's redacted on the target). In
6692 * this case, dsl_bookmark_lookup() fills in zbm
6693 * but returns EXDEV. Ignore this error.
5dc8b736 6694 */
30af21b0
PD
6695 if (error == EXDEV && zbm.zbm_redaction_obj != 0 &&
6696 zbm.zbm_guid ==
6697 dsl_dataset_phys(tosnap)->ds_guid)
6698 error = 0;
6699
6700 if (error != 0) {
6701 dsl_dataset_rele(tosnap, FTAG);
6702 dsl_pool_rele(dp, FTAG);
6703 return (error);
6704 }
6705 if (zbm.zbm_redaction_obj != 0 || !(zbm.zbm_flags &
6706 ZBM_FLAG_HAS_FBN)) {
6707 full_estimate = B_TRUE;
6708 }
6709 } else if (strchr(fromname, '@')) {
5dc8b736 6710 error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap);
30af21b0
PD
6711 if (error != 0) {
6712 dsl_dataset_rele(tosnap, FTAG);
6713 dsl_pool_rele(dp, FTAG);
6714 return (error);
6715 }
5dc8b736 6716
30af21b0
PD
6717 if (!dsl_dataset_is_before(tosnap, fromsnap, 0)) {
6718 full_estimate = B_TRUE;
6719 dsl_dataset_rele(fromsnap, FTAG);
6720 }
5dc8b736
MG
6721 } else {
6722 /*
6723 * from is not properly formatted as a snapshot or
6724 * bookmark
6725 */
30af21b0
PD
6726 dsl_dataset_rele(tosnap, FTAG);
6727 dsl_pool_rele(dp, FTAG);
6728 return (SET_ERROR(EINVAL));
6f1ffb06 6729 }
30af21b0
PD
6730 }
6731
6732 if (full_estimate) {
6733 dmu_send_outparams_t out = {0};
6734 offset_t off = 0;
6735 out.dso_outfunc = send_space_sum;
6736 out.dso_arg = &space;
6737 out.dso_dryrun = B_TRUE;
d8fdfc2d 6738 /*
30af21b0
PD
6739 * We have to release these holds so dmu_send can take them. It
6740 * will do all the error checking we need.
d8fdfc2d 6741 */
30af21b0
PD
6742 dsl_dataset_rele(tosnap, FTAG);
6743 dsl_pool_rele(dp, FTAG);
6744 error = dmu_send(snapname, fromname, embedok, largeblockok,
ba0ba69e
TC
6745 compressok, rawok, savedok, resumeobj, resumeoff,
6746 redactlist_book, fd, &off, &out);
30af21b0
PD
6747 } else {
6748 error = dmu_send_estimate_fast(tosnap, fromsnap,
6749 (from && strchr(fromname, '#') != NULL ? &zbm : NULL),
ba0ba69e 6750 compressok || rawok, savedok, &space);
30af21b0
PD
6751 space -= resume_bytes;
6752 if (fromsnap != NULL)
6753 dsl_dataset_rele(fromsnap, FTAG);
6754 dsl_dataset_rele(tosnap, FTAG);
6755 dsl_pool_rele(dp, FTAG);
6f1ffb06
MA
6756 }
6757
6f1ffb06
MA
6758 fnvlist_add_uint64(outnvl, "space", space);
6759
6f1ffb06
MA
6760 return (error);
6761}
6762
bec1067d
AP
6763/*
6764 * Sync the currently open TXG to disk for the specified pool.
6765 * This is somewhat similar to 'zfs_sync()'.
6766 * For cases that do not result in error this ioctl will wait for
6767 * the currently open TXG to commit before returning back to the caller.
6768 *
6769 * innvl: {
6770 * "force" -> when true, force uberblock update even if there is no dirty data.
6771 * In addition this will cause the vdev configuration to be written
6772 * out including updating the zpool cache file. (boolean_t)
6773 * }
6774 *
6775 * onvl is unused
6776 */
b83a0e2d
DB
6777static const zfs_ioc_key_t zfs_keys_pool_sync[] = {
6778 {"force", DATA_TYPE_BOOLEAN_VALUE, 0},
6779};
6780
bec1067d
AP
6781static int
6782zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl)
6783{
ef70eff1 6784 (void) onvl;
bec1067d 6785 int err;
edb20ff3 6786 boolean_t rc, force = B_FALSE;
bec1067d
AP
6787 spa_t *spa;
6788
6789 if ((err = spa_open(pool, &spa, FTAG)) != 0)
6790 return (err);
6791
edb20ff3
BB
6792 if (innvl) {
6793 err = nvlist_lookup_boolean_value(innvl, "force", &rc);
6794 if (err == 0)
6795 force = rc;
6796 }
05f85a6a 6797
bec1067d
AP
6798 if (force) {
6799 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER);
6800 vdev_config_dirty(spa->spa_root_vdev);
6801 spa_config_exit(spa, SCL_CONFIG, FTAG);
6802 }
6803 txg_wait_synced(spa_get_dsl(spa), 0);
b83a0e2d 6804
bec1067d
AP
6805 spa_close(spa, FTAG);
6806
edb20ff3 6807 return (0);
bec1067d
AP
6808}
6809
b5256303
TC
6810/*
6811 * Load a user's wrapping key into the kernel.
6812 * innvl: {
6813 * "hidden_args" -> { "wkeydata" -> value }
6814 * raw uint8_t array of encryption wrapping key data (32 bytes)
6815 * (optional) "noop" -> (value ignored)
6816 * presence indicated key should only be verified, not loaded
6817 * }
6818 */
b83a0e2d
DB
6819static const zfs_ioc_key_t zfs_keys_load_key[] = {
6820 {"hidden_args", DATA_TYPE_NVLIST, 0},
6821 {"noop", DATA_TYPE_BOOLEAN, ZK_OPTIONAL},
6822};
6823
b5256303
TC
6824static int
6825zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6826{
ef70eff1 6827 (void) outnvl;
b5256303
TC
6828 int ret;
6829 dsl_crypto_params_t *dcp = NULL;
6830 nvlist_t *hidden_args;
6831 boolean_t noop = nvlist_exists(innvl, "noop");
6832
6833 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6834 ret = SET_ERROR(EINVAL);
6835 goto error;
6836 }
6837
b83a0e2d 6838 hidden_args = fnvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS);
b5256303
TC
6839
6840 ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL,
6841 hidden_args, &dcp);
6842 if (ret != 0)
6843 goto error;
6844
6845 ret = spa_keystore_load_wkey(dsname, dcp, noop);
6846 if (ret != 0)
6847 goto error;
6848
6849 dsl_crypto_params_free(dcp, noop);
6850
6851 return (0);
6852
6853error:
6854 dsl_crypto_params_free(dcp, B_TRUE);
6855 return (ret);
6856}
6857
6858/*
6859 * Unload a user's wrapping key from the kernel.
6860 * Both innvl and outnvl are unused.
6861 */
b83a0e2d
DB
6862static const zfs_ioc_key_t zfs_keys_unload_key[] = {
6863 /* no nvl keys */
6864};
6865
b5256303
TC
6866static int
6867zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6868{
ef70eff1 6869 (void) innvl, (void) outnvl;
b5256303
TC
6870 int ret = 0;
6871
6872 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6873 ret = (SET_ERROR(EINVAL));
6874 goto out;
6875 }
6876
6877 ret = spa_keystore_unload_wkey(dsname);
6878 if (ret != 0)
6879 goto out;
6880
6881out:
6882 return (ret);
6883}
6884
6885/*
6886 * Changes a user's wrapping key used to decrypt a dataset. The keyformat,
ef70eff1 6887 * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified
b5256303
TC
6888 * here to change how the key is derived in userspace.
6889 *
6890 * innvl: {
6891 * "hidden_args" (optional) -> { "wkeydata" -> value }
6892 * raw uint8_t array of new encryption wrapping key data (32 bytes)
6893 * "props" (optional) -> { prop -> value }
6894 * }
6895 *
6896 * outnvl is unused
6897 */
b83a0e2d
DB
6898static const zfs_ioc_key_t zfs_keys_change_key[] = {
6899 {"crypt_cmd", DATA_TYPE_UINT64, ZK_OPTIONAL},
6900 {"hidden_args", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6901 {"props", DATA_TYPE_NVLIST, ZK_OPTIONAL},
6902};
6903
b5256303
TC
6904static int
6905zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl)
6906{
ef70eff1 6907 (void) outnvl;
b5256303
TC
6908 int ret;
6909 uint64_t cmd = DCP_CMD_NONE;
6910 dsl_crypto_params_t *dcp = NULL;
6911 nvlist_t *args = NULL, *hidden_args = NULL;
6912
6913 if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) {
6914 ret = (SET_ERROR(EINVAL));
6915 goto error;
6916 }
6917
6918 (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd);
6919 (void) nvlist_lookup_nvlist(innvl, "props", &args);
6920 (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args);
6921
6922 ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp);
6923 if (ret != 0)
6924 goto error;
6925
6926 ret = spa_keystore_change_key(dsname, dcp);
6927 if (ret != 0)
6928 goto error;
6929
6930 dsl_crypto_params_free(dcp, B_FALSE);
6931
6932 return (0);
6933
6934error:
6935 dsl_crypto_params_free(dcp, B_TRUE);
6936 return (ret);
6937}
6938
6f1ffb06
MA
6939static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6940
6941static void
6942zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6943 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6944 boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6945{
6946 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6947
6948 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6949 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6950 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6951 ASSERT3P(vec->zvec_func, ==, NULL);
6952
6953 vec->zvec_legacy_func = func;
6954 vec->zvec_secpolicy = secpolicy;
6955 vec->zvec_namecheck = namecheck;
6956 vec->zvec_allow_log = log_history;
6957 vec->zvec_pool_check = pool_check;
6958}
6959
6960/*
6961 * See the block comment at the beginning of this file for details on
6962 * each argument to this function.
6963 */
7bb0c294 6964void
6f1ffb06
MA
6965zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func,
6966 zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6967 zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist,
b83a0e2d 6968 boolean_t allow_log, const zfs_ioc_key_t *nvl_keys, size_t num_keys)
6f1ffb06
MA
6969{
6970 zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6971
6972 ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6973 ASSERT3U(ioc, <, ZFS_IOC_LAST);
6974 ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6975 ASSERT3P(vec->zvec_func, ==, NULL);
6976
6977 /* if we are logging, the name must be valid */
6978 ASSERT(!allow_log || namecheck != NO_NAME);
6979
6980 vec->zvec_name = name;
6981 vec->zvec_func = func;
6982 vec->zvec_secpolicy = secpolicy;
6983 vec->zvec_namecheck = namecheck;
6984 vec->zvec_pool_check = pool_check;
6985 vec->zvec_smush_outnvlist = smush_outnvlist;
6986 vec->zvec_allow_log = allow_log;
b83a0e2d
DB
6987 vec->zvec_nvl_keys = nvl_keys;
6988 vec->zvec_nvl_key_count = num_keys;
6f1ffb06
MA
6989}
6990
6991static void
6992zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6993 zfs_secpolicy_func_t *secpolicy, boolean_t log_history,
6994 zfs_ioc_poolcheck_t pool_check)
6995{
6996 zfs_ioctl_register_legacy(ioc, func, secpolicy,
6997 POOL_NAME, log_history, pool_check);
6998}
6999
7bb0c294 7000void
6f1ffb06
MA
7001zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7002 zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check)
7003{
7004 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7005 DATASET_NAME, B_FALSE, pool_check);
7006}
7007
7008static void
7009zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7010{
7011 zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config,
7012 POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7013}
7014
7015static void
7016zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
7017 zfs_secpolicy_func_t *secpolicy)
7018{
7019 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7020 NO_NAME, B_FALSE, POOL_CHECK_NONE);
7021}
7022
7023static void
7024zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc,
7025 zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy)
7026{
7027 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7028 DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
7029}
7030
7031static void
7032zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
7033{
7034 zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
7035 zfs_secpolicy_read);
7036}
7037
7038static void
7039zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
e9aa730c 7040 zfs_secpolicy_func_t *secpolicy)
6f1ffb06
MA
7041{
7042 zfs_ioctl_register_legacy(ioc, func, secpolicy,
7043 DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7044}
7045
65c7cc49 7046static void
6f1ffb06
MA
7047zfs_ioctl_init(void)
7048{
7049 zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
7050 zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
b83a0e2d
DB
7051 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7052 zfs_keys_snapshot, ARRAY_SIZE(zfs_keys_snapshot));
6f1ffb06
MA
7053
7054 zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
7055 zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
b83a0e2d
DB
7056 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7057 zfs_keys_log_history, ARRAY_SIZE(zfs_keys_log_history));
6f1ffb06
MA
7058
7059 zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
7060 zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
7061 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7062 zfs_keys_space_snaps, ARRAY_SIZE(zfs_keys_space_snaps));
6f1ffb06
MA
7063
7064 zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
7065 zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
b83a0e2d
DB
7066 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7067 zfs_keys_send_new, ARRAY_SIZE(zfs_keys_send_new));
6f1ffb06
MA
7068
7069 zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
7070 zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
7071 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7072 zfs_keys_send_space, ARRAY_SIZE(zfs_keys_send_space));
6f1ffb06
MA
7073
7074 zfs_ioctl_register("create", ZFS_IOC_CREATE,
7075 zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
b83a0e2d
DB
7076 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7077 zfs_keys_create, ARRAY_SIZE(zfs_keys_create));
6f1ffb06
MA
7078
7079 zfs_ioctl_register("clone", ZFS_IOC_CLONE,
7080 zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
b83a0e2d
DB
7081 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7082 zfs_keys_clone, ARRAY_SIZE(zfs_keys_clone));
6f1ffb06 7083
a1d477c2 7084 zfs_ioctl_register("remap", ZFS_IOC_REMAP,
59ec30a3 7085 zfs_ioc_remap, zfs_secpolicy_none, DATASET_NAME,
b83a0e2d
DB
7086 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7087 zfs_keys_remap, ARRAY_SIZE(zfs_keys_remap));
a1d477c2 7088
6f1ffb06
MA
7089 zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
7090 zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
b83a0e2d
DB
7091 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7092 zfs_keys_destroy_snaps, ARRAY_SIZE(zfs_keys_destroy_snaps));
6f1ffb06 7093
13fe0198
MA
7094 zfs_ioctl_register("hold", ZFS_IOC_HOLD,
7095 zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
b83a0e2d
DB
7096 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7097 zfs_keys_hold, ARRAY_SIZE(zfs_keys_hold));
13fe0198
MA
7098 zfs_ioctl_register("release", ZFS_IOC_RELEASE,
7099 zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
b83a0e2d
DB
7100 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7101 zfs_keys_release, ARRAY_SIZE(zfs_keys_release));
13fe0198
MA
7102
7103 zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
7104 zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
7105 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7106 zfs_keys_get_holds, ARRAY_SIZE(zfs_keys_get_holds));
13fe0198 7107
46ba1e59
MA
7108 zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
7109 zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
b83a0e2d
DB
7110 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7111 zfs_keys_rollback, ARRAY_SIZE(zfs_keys_rollback));
46ba1e59 7112
da536844
MA
7113 zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
7114 zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
b83a0e2d
DB
7115 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7116 zfs_keys_bookmark, ARRAY_SIZE(zfs_keys_bookmark));
da536844
MA
7117
7118 zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
7119 zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
b83a0e2d
DB
7120 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE,
7121 zfs_keys_get_bookmarks, ARRAY_SIZE(zfs_keys_get_bookmarks));
da536844 7122
30af21b0
PD
7123 zfs_ioctl_register("get_bookmark_props", ZFS_IOC_GET_BOOKMARK_PROPS,
7124 zfs_ioc_get_bookmark_props, zfs_secpolicy_read, ENTITY_NAME,
7125 POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE, zfs_keys_get_bookmark_props,
7126 ARRAY_SIZE(zfs_keys_get_bookmark_props));
7127
da536844
MA
7128 zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
7129 zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
7130 POOL_NAME,
b83a0e2d
DB
7131 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7132 zfs_keys_destroy_bookmarks,
7133 ARRAY_SIZE(zfs_keys_destroy_bookmarks));
da536844 7134
43e52edd 7135 zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW,
ef70eff1 7136 zfs_ioc_recv_new, zfs_secpolicy_recv, DATASET_NAME,
b83a0e2d
DB
7137 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7138 zfs_keys_recv_new, ARRAY_SIZE(zfs_keys_recv_new));
b5256303
TC
7139 zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY,
7140 zfs_ioc_load_key, zfs_secpolicy_load_key,
b83a0e2d
DB
7141 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7142 zfs_keys_load_key, ARRAY_SIZE(zfs_keys_load_key));
b5256303
TC
7143 zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY,
7144 zfs_ioc_unload_key, zfs_secpolicy_load_key,
b83a0e2d
DB
7145 DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE,
7146 zfs_keys_unload_key, ARRAY_SIZE(zfs_keys_unload_key));
b5256303
TC
7147 zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY,
7148 zfs_ioc_change_key, zfs_secpolicy_change_key,
7149 DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY,
b83a0e2d
DB
7150 B_TRUE, B_TRUE, zfs_keys_change_key,
7151 ARRAY_SIZE(zfs_keys_change_key));
43e52edd 7152
bec1067d
AP
7153 zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC,
7154 zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME,
b83a0e2d
DB
7155 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7156 zfs_keys_pool_sync, ARRAY_SIZE(zfs_keys_pool_sync));
d3f2cd7e
AB
7157 zfs_ioctl_register("reopen", ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
7158 zfs_secpolicy_config, POOL_NAME, POOL_CHECK_SUSPENDED, B_TRUE,
b83a0e2d 7159 B_TRUE, zfs_keys_pool_reopen, ARRAY_SIZE(zfs_keys_pool_reopen));
bec1067d 7160
d99a0153
CW
7161 zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
7162 zfs_ioc_channel_program, zfs_secpolicy_config,
7163 POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
b83a0e2d
DB
7164 B_TRUE, zfs_keys_channel_program,
7165 ARRAY_SIZE(zfs_keys_channel_program));
d99a0153 7166
30af21b0
PD
7167 zfs_ioctl_register("redact", ZFS_IOC_REDACT,
7168 zfs_ioc_redact, zfs_secpolicy_config, DATASET_NAME,
7169 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7170 zfs_keys_redact, ARRAY_SIZE(zfs_keys_redact));
7171
d2734cce
SD
7172 zfs_ioctl_register("zpool_checkpoint", ZFS_IOC_POOL_CHECKPOINT,
7173 zfs_ioc_pool_checkpoint, zfs_secpolicy_config, POOL_NAME,
b83a0e2d
DB
7174 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7175 zfs_keys_pool_checkpoint, ARRAY_SIZE(zfs_keys_pool_checkpoint));
d2734cce
SD
7176
7177 zfs_ioctl_register("zpool_discard_checkpoint",
7178 ZFS_IOC_POOL_DISCARD_CHECKPOINT, zfs_ioc_pool_discard_checkpoint,
7179 zfs_secpolicy_config, POOL_NAME,
b83a0e2d
DB
7180 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7181 zfs_keys_pool_discard_checkpoint,
7182 ARRAY_SIZE(zfs_keys_pool_discard_checkpoint));
d2734cce 7183
619f0976
GW
7184 zfs_ioctl_register("initialize", ZFS_IOC_POOL_INITIALIZE,
7185 zfs_ioc_pool_initialize, zfs_secpolicy_config, POOL_NAME,
7186 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7187 zfs_keys_pool_initialize, ARRAY_SIZE(zfs_keys_pool_initialize));
7188
1b939560
BB
7189 zfs_ioctl_register("trim", ZFS_IOC_POOL_TRIM,
7190 zfs_ioc_pool_trim, zfs_secpolicy_config, POOL_NAME,
7191 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE,
7192 zfs_keys_pool_trim, ARRAY_SIZE(zfs_keys_pool_trim));
7193
e60e158e
JG
7194 zfs_ioctl_register("wait", ZFS_IOC_WAIT,
7195 zfs_ioc_wait, zfs_secpolicy_none, POOL_NAME,
7196 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7197 zfs_keys_pool_wait, ARRAY_SIZE(zfs_keys_pool_wait));
7198
5a42ef04
PD
7199 zfs_ioctl_register("wait_fs", ZFS_IOC_WAIT_FS,
7200 zfs_ioc_wait_fs, zfs_secpolicy_none, DATASET_NAME,
7201 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7202 zfs_keys_fs_wait, ARRAY_SIZE(zfs_keys_fs_wait));
7203
108a454a
PD
7204 zfs_ioctl_register("set_bootenv", ZFS_IOC_SET_BOOTENV,
7205 zfs_ioc_set_bootenv, zfs_secpolicy_config, POOL_NAME,
7206 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE,
7207 zfs_keys_set_bootenv, ARRAY_SIZE(zfs_keys_set_bootenv));
7208
7209 zfs_ioctl_register("get_bootenv", ZFS_IOC_GET_BOOTENV,
7210 zfs_ioc_get_bootenv, zfs_secpolicy_none, POOL_NAME,
7211 POOL_CHECK_SUSPENDED, B_FALSE, B_TRUE,
7212 zfs_keys_get_bootenv, ARRAY_SIZE(zfs_keys_get_bootenv));
7213
2a673e76
AJ
7214 zfs_ioctl_register("zpool_vdev_get_props", ZFS_IOC_VDEV_GET_PROPS,
7215 zfs_ioc_vdev_get_props, zfs_secpolicy_read, POOL_NAME,
7216 POOL_CHECK_NONE, B_FALSE, B_FALSE, zfs_keys_vdev_get_props,
7217 ARRAY_SIZE(zfs_keys_vdev_get_props));
7218
7219 zfs_ioctl_register("zpool_vdev_set_props", ZFS_IOC_VDEV_SET_PROPS,
7220 zfs_ioc_vdev_set_props, zfs_secpolicy_config, POOL_NAME,
7221 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE,
7222 zfs_keys_vdev_set_props, ARRAY_SIZE(zfs_keys_vdev_set_props));
7223
6f1ffb06
MA
7224 /* IOCTLS that use the legacy function signature */
7225
7226 zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7227 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7228
7229 zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7230 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7231 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7232 zfs_ioc_pool_scan);
7233 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7234 zfs_ioc_pool_upgrade);
7235 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7236 zfs_ioc_vdev_add);
7237 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7238 zfs_ioc_vdev_remove);
7239 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7240 zfs_ioc_vdev_set_state);
7241 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7242 zfs_ioc_vdev_attach);
7243 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7244 zfs_ioc_vdev_detach);
7245 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7246 zfs_ioc_vdev_setpath);
7247 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7248 zfs_ioc_vdev_setfru);
7249 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7250 zfs_ioc_pool_set_props);
7251 zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7252 zfs_ioc_vdev_split);
7253 zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7254 zfs_ioc_pool_reguid);
7255
7256 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7257 zfs_ioc_pool_configs, zfs_secpolicy_none);
7258 zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7259 zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7260 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7261 zfs_ioc_inject_fault, zfs_secpolicy_inject);
7262 zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7263 zfs_ioc_clear_fault, zfs_secpolicy_inject);
7264 zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
7265 zfs_ioc_inject_list_next, zfs_secpolicy_inject);
7266
7267 /*
7268 * pool destroy, and export don't log the history as part of
7269 * zfsdev_ioctl, but rather zfs_ioc_pool_export
7270 * does the logging of those commands.
7271 */
7272 zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy,
87a63dd7 7273 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6f1ffb06 7274 zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export,
87a63dd7 7275 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
6f1ffb06
MA
7276
7277 zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7278 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7279 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
7280 zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7281
7282 zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7283 zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7284 zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7285 zfs_ioc_dsobj_to_dsname,
7286 zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7287 zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7288 zfs_ioc_pool_get_history,
7289 zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7290
7291 zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7292 zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7293
7294 zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
92e43c17 7295 zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
6f1ffb06
MA
7296
7297 zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7298 zfs_ioc_space_written);
6f1ffb06
MA
7299 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7300 zfs_ioc_objset_recvd_props);
7301 zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7302 zfs_ioc_next_obj);
7303 zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7304 zfs_ioc_get_fsacl);
7305 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7306 zfs_ioc_objset_stats);
7307 zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7308 zfs_ioc_objset_zplprops);
7309 zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7310 zfs_ioc_dataset_list_next);
7311 zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7312 zfs_ioc_snapshot_list_next);
7313 zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7314 zfs_ioc_send_progress);
7315
7316 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
7317 zfs_ioc_diff, zfs_secpolicy_diff);
7318 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS,
7319 zfs_ioc_obj_to_stats, zfs_secpolicy_diff);
7320 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH,
7321 zfs_ioc_obj_to_path, zfs_secpolicy_diff);
7322 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE,
7323 zfs_ioc_userspace_one, zfs_secpolicy_userspace_one);
7324 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY,
7325 zfs_ioc_userspace_many, zfs_secpolicy_userspace_many);
7326 zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND,
7327 zfs_ioc_send, zfs_secpolicy_send);
7328
7329 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop,
7330 zfs_secpolicy_none);
7331 zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
7332 zfs_secpolicy_destroy);
6f1ffb06
MA
7333 zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
7334 zfs_secpolicy_rename);
7335 zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,
7336 zfs_secpolicy_recv);
7337 zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote,
7338 zfs_secpolicy_promote);
6f1ffb06
MA
7339 zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP,
7340 zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop);
7341 zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl,
7342 zfs_secpolicy_set_fsacl);
7343
7344 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share,
7345 zfs_secpolicy_share, POOL_CHECK_NONE);
7346 zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl,
7347 zfs_secpolicy_smb_acl, POOL_CHECK_NONE);
7348 zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE,
7349 zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade,
7350 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7351 zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT,
7352 zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot,
7353 POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
7354
6f1ffb06
MA
7355 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next,
7356 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7357 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear,
7358 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
75e3ff58
BB
7359 zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek,
7360 zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE);
7bb0c294
MM
7361
7362 zfs_ioctl_init_os();
6f1ffb06 7363}
34dc7c2f 7364
b83a0e2d
DB
7365/*
7366 * Verify that for non-legacy ioctls the input nvlist
7367 * pairs match against the expected input.
7368 *
7369 * Possible errors are:
7370 * ZFS_ERR_IOC_ARG_UNAVAIL An unrecognized nvpair was encountered
7371 * ZFS_ERR_IOC_ARG_REQUIRED A required nvpair is missing
7372 * ZFS_ERR_IOC_ARG_BADTYPE Invalid type for nvpair
7373 */
7374static int
7375zfs_check_input_nvpairs(nvlist_t *innvl, const zfs_ioc_vec_t *vec)
7376{
7377 const zfs_ioc_key_t *nvl_keys = vec->zvec_nvl_keys;
7378 boolean_t required_keys_found = B_FALSE;
7379
7380 /*
7381 * examine each input pair
7382 */
7383 for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
7384 pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
7385 char *name = nvpair_name(pair);
7386 data_type_t type = nvpair_type(pair);
7387 boolean_t identified = B_FALSE;
7388
7389 /*
7390 * check pair against the documented names and type
7391 */
7392 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7393 /* if not a wild card name, check for an exact match */
7394 if ((nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) == 0 &&
7395 strcmp(nvl_keys[k].zkey_name, name) != 0)
7396 continue;
7397
7398 identified = B_TRUE;
7399
7400 if (nvl_keys[k].zkey_type != DATA_TYPE_ANY &&
7401 nvl_keys[k].zkey_type != type) {
7402 return (SET_ERROR(ZFS_ERR_IOC_ARG_BADTYPE));
7403 }
7404
7405 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7406 continue;
7407
7408 required_keys_found = B_TRUE;
7409 break;
7410 }
7411
7412 /* allow an 'optional' key, everything else is invalid */
7413 if (!identified &&
7414 (strcmp(name, "optional") != 0 ||
7415 type != DATA_TYPE_NVLIST)) {
7416 return (SET_ERROR(ZFS_ERR_IOC_ARG_UNAVAIL));
7417 }
7418 }
7419
7420 /* verify that all required keys were found */
7421 for (int k = 0; k < vec->zvec_nvl_key_count; k++) {
7422 if (nvl_keys[k].zkey_flags & ZK_OPTIONAL)
7423 continue;
7424
7425 if (nvl_keys[k].zkey_flags & ZK_WILDCARDLIST) {
e1cfd73f 7426 /* at least one non-optional key is expected here */
b83a0e2d
DB
7427 if (!required_keys_found)
7428 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7429 continue;
7430 }
7431
7432 if (!nvlist_exists(innvl, nvl_keys[k].zkey_name))
7433 return (SET_ERROR(ZFS_ERR_IOC_ARG_REQUIRED));
7434 }
7435
7436 return (0);
7437}
7438
65c7cc49 7439static int
572e2857
BB
7440pool_status_check(const char *name, zfs_ioc_namecheck_t type,
7441 zfs_ioc_poolcheck_t check)
9babb374
BB
7442{
7443 spa_t *spa;
7444 int error;
7445
30af21b0
PD
7446 ASSERT(type == POOL_NAME || type == DATASET_NAME ||
7447 type == ENTITY_NAME);
9babb374 7448
572e2857
BB
7449 if (check & POOL_CHECK_NONE)
7450 return (0);
7451
9babb374
BB
7452 error = spa_open(name, &spa, FTAG);
7453 if (error == 0) {
572e2857 7454 if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa))
2e528b49 7455 error = SET_ERROR(EAGAIN);
572e2857 7456 else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa))
2e528b49 7457 error = SET_ERROR(EROFS);
9babb374
BB
7458 spa_close(spa, FTAG);
7459 }
7460 return (error);
7461}
7462
ae9f92f6 7463int
958826be 7464zfsdev_getminor(zfs_file_t *fp, minor_t *minorp)
ae9f92f6
MM
7465{
7466 zfsdev_state_t *zs, *fpd;
ae9f92f6
MM
7467
7468 ASSERT(!MUTEX_HELD(&zfsdev_state_lock));
7469
ae9f92f6
MM
7470 fpd = zfs_file_private(fp);
7471 if (fpd == NULL)
7472 return (SET_ERROR(EBADF));
7473
7474 mutex_enter(&zfsdev_state_lock);
7475
7476 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7477
7478 if (zs->zs_minor == -1)
7479 continue;
7480
7481 if (fpd == zs) {
7482 *minorp = fpd->zs_minor;
7483 mutex_exit(&zfsdev_state_lock);
7484 return (0);
7485 }
7486 }
7487
7488 mutex_exit(&zfsdev_state_lock);
7489
7490 return (SET_ERROR(EBADF));
7491}
7492
1dff5452
RM
7493void *
7494zfsdev_get_state(minor_t minor, enum zfsdev_state_type which)
325f0235
BB
7495{
7496 zfsdev_state_t *zs;
7497
3937ab20 7498 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
325f0235 7499 if (zs->zs_minor == minor) {
cf66e7e5 7500 membar_consumer();
325f0235 7501 switch (which) {
d1d7e268
MK
7502 case ZST_ONEXIT:
7503 return (zs->zs_onexit);
7504 case ZST_ZEVENT:
7505 return (zs->zs_zevent);
7506 case ZST_ALL:
7507 return (zs);
325f0235
BB
7508 }
7509 }
7510 }
7511
d1d7e268 7512 return (NULL);
325f0235
BB
7513}
7514
572e2857 7515/*
325f0235
BB
7516 * Find a free minor number. The zfsdev_state_list is expected to
7517 * be short since it is only a list of currently open file handles.
572e2857 7518 */
a631283b 7519static minor_t
572e2857
BB
7520zfsdev_minor_alloc(void)
7521{
325f0235 7522 static minor_t last_minor = 0;
572e2857
BB
7523 minor_t m;
7524
7525 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7526
7527 for (m = last_minor + 1; m != last_minor; m++) {
7528 if (m > ZFSDEV_MAX_MINOR)
7529 m = 1;
1dff5452 7530 if (zfsdev_get_state(m, ZST_ALL) == NULL) {
572e2857
BB
7531 last_minor = m;
7532 return (m);
7533 }
7534 }
7535
7536 return (0);
7537}
7538
a631283b
RM
7539int
7540zfsdev_state_init(void *priv)
7541{
7542 zfsdev_state_t *zs, *zsprev = NULL;
7543 minor_t minor;
7544 boolean_t newzs = B_FALSE;
7545
7546 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
7547
7548 minor = zfsdev_minor_alloc();
7549 if (minor == 0)
7550 return (SET_ERROR(ENXIO));
7551
7552 for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
7553 if (zs->zs_minor == -1)
7554 break;
7555 zsprev = zs;
7556 }
7557
7558 if (!zs) {
7559 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7560 newzs = B_TRUE;
7561 }
7562
7563 zfsdev_private_set_state(priv, zs);
7564
7565 zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
7566 zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
7567
7568 /*
7569 * In order to provide for lock-free concurrent read access
7570 * to the minor list in zfsdev_get_state(), new entries
7571 * must be completely written before linking them into the
7572 * list whereas existing entries are already linked; the last
7573 * operation must be updating zs_minor (from -1 to the new
7574 * value).
7575 */
7576 if (newzs) {
7577 zs->zs_minor = minor;
7578 membar_producer();
7579 zsprev->zs_next = zs;
7580 } else {
7581 membar_producer();
7582 zs->zs_minor = minor;
7583 }
7584
7585 return (0);
7586}
7587
7588void
7589zfsdev_state_destroy(void *priv)
7590{
7591 zfsdev_state_t *zs = zfsdev_private_get_state(priv);
7592
7593 ASSERT(zs != NULL);
7594 ASSERT3S(zs->zs_minor, >, 0);
7595
7596 /*
7597 * The last reference to this zfsdev file descriptor is being dropped.
7598 * We don't have to worry about lookup grabbing this state object, and
7599 * zfsdev_state_init() will not try to reuse this object until it is
7600 * invalidated by setting zs_minor to -1. Invalidation must be done
7601 * last, with a memory barrier to ensure ordering. This lets us avoid
7602 * taking the global zfsdev state lock around destruction.
7603 */
7604 zfs_onexit_destroy(zs->zs_onexit);
7605 zfs_zevent_destroy(zs->zs_zevent);
7606 zs->zs_onexit = NULL;
7607 zs->zs_zevent = NULL;
7608 membar_producer();
7609 zs->zs_minor = -1;
7610}
7611
7bb0c294 7612long
529246df 7613zfsdev_ioctl_common(uint_t vecnum, zfs_cmd_t *zc, int flag)
34dc7c2f 7614{
529246df 7615 int error, cmd;
6f1ffb06 7616 const zfs_ioc_vec_t *vec;
fb8e608d 7617 char *saved_poolname = NULL;
009cc8e8 7618 uint64_t max_nvlist_src_size;
4458157b 7619 size_t saved_poolname_len = 0;
6f1ffb06 7620 nvlist_t *innvl = NULL;
40d06e3c 7621 fstrans_cookie_t cookie;
2ac90457 7622 hrtime_t start_time = gethrtime();
6f1ffb06 7623
7bb0c294 7624 cmd = vecnum;
51420321 7625 error = 0;
6f1ffb06 7626 if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0]))
51420321
MM
7627 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
7628
6f1ffb06 7629 vec = &zfs_ioc_vec[vecnum];
34dc7c2f 7630
2e0358cb
BB
7631 /*
7632 * The registered ioctl list may be sparse, verify that either
7633 * a normal or legacy handler are registered.
7634 */
7635 if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL)
51420321 7636 return (SET_ERROR(ZFS_ERR_IOC_CMD_UNAVAIL));
34dc7c2f 7637
6f1ffb06 7638 zc->zc_iflags = flag & FKIOCTL;
009cc8e8
RM
7639 max_nvlist_src_size = zfs_max_nvlist_src_size_os();
7640 if (zc->zc_nvlist_src_size > max_nvlist_src_size) {
f74b821a
BB
7641 /*
7642 * Make sure the user doesn't pass in an insane value for
7643 * zc_nvlist_src_size. We have to check, since we will end
7644 * up allocating that much memory inside of get_nvlist(). This
7645 * prevents a nefarious user from allocating tons of kernel
7646 * memory.
7647 *
7648 * Also, we return EINVAL instead of ENOMEM here. The reason
7649 * being that returning ENOMEM from an ioctl() has a special
7650 * connotation; that the user's size value is too small and
7651 * needs to be expanded to hold the nvlist. See
7652 * zcmd_expand_dst_nvlist() for details.
7653 */
7654 error = SET_ERROR(EINVAL); /* User's size too big */
7655
7656 } else if (zc->zc_nvlist_src_size != 0) {
6f1ffb06
MA
7657 error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
7658 zc->zc_iflags, &innvl);
7659 if (error != 0)
7660 goto out;
7661 }
34dc7c2f
BB
7662
7663 /*
7664 * Ensure that all pool/dataset names are valid before we pass down to
7665 * the lower layers.
7666 */
6f1ffb06
MA
7667 zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
7668 switch (vec->zvec_namecheck) {
7669 case POOL_NAME:
7670 if (pool_namecheck(zc->zc_name, NULL, NULL) != 0)
2e528b49 7671 error = SET_ERROR(EINVAL);
6f1ffb06 7672 else
572e2857 7673 error = pool_status_check(zc->zc_name,
6f1ffb06
MA
7674 vec->zvec_namecheck, vec->zvec_pool_check);
7675 break;
34dc7c2f 7676
6f1ffb06
MA
7677 case DATASET_NAME:
7678 if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0)
2e528b49 7679 error = SET_ERROR(EINVAL);
6f1ffb06 7680 else
572e2857 7681 error = pool_status_check(zc->zc_name,
6f1ffb06
MA
7682 vec->zvec_namecheck, vec->zvec_pool_check);
7683 break;
34dc7c2f 7684
30af21b0
PD
7685 case ENTITY_NAME:
7686 if (entity_namecheck(zc->zc_name, NULL, NULL) != 0) {
7687 error = SET_ERROR(EINVAL);
7688 } else {
7689 error = pool_status_check(zc->zc_name,
7690 vec->zvec_namecheck, vec->zvec_pool_check);
7691 }
7692 break;
7693
6f1ffb06
MA
7694 case NO_NAME:
7695 break;
34dc7c2f 7696 }
b83a0e2d
DB
7697 /*
7698 * Ensure that all input pairs are valid before we pass them down
7699 * to the lower layers.
7700 *
7701 * The vectored functions can use fnvlist_lookup_{type} for any
7702 * required pairs since zfs_check_input_nvpairs() confirmed that
7703 * they exist and are of the correct type.
7704 */
7705 if (error == 0 && vec->zvec_func != NULL) {
7706 error = zfs_check_input_nvpairs(innvl, vec);
7707 if (error != 0)
7708 goto out;
7709 }
34dc7c2f 7710
005e27e3 7711 if (error == 0) {
ddab862d 7712 cookie = spl_fstrans_mark();
6f1ffb06 7713 error = vec->zvec_secpolicy(zc, innvl, CRED());
ddab862d
TC
7714 spl_fstrans_unmark(cookie);
7715 }
6f1ffb06
MA
7716
7717 if (error != 0)
7718 goto out;
7719
7720 /* legacy ioctls can modify zc_name */
4458157b
JL
7721 /*
7722 * Can't use kmem_strdup() as we might truncate the string and
7723 * kmem_strfree() would then free with incorrect size.
7724 */
7725 saved_poolname_len = strlen(zc->zc_name) + 1;
7726 saved_poolname = kmem_alloc(saved_poolname_len, KM_SLEEP);
7727
7728 strlcpy(saved_poolname, zc->zc_name, saved_poolname_len);
7729 saved_poolname[strcspn(saved_poolname, "/@#")] = '\0';
6f1ffb06
MA
7730
7731 if (vec->zvec_func != NULL) {
7732 nvlist_t *outnvl;
7733 int puterror = 0;
7734 spa_t *spa;
7735 nvlist_t *lognv = NULL;
7736
7737 ASSERT(vec->zvec_legacy_func == NULL);
7738
7739 /*
7740 * Add the innvl to the lognv before calling the func,
7741 * in case the func changes the innvl.
7742 */
7743 if (vec->zvec_allow_log) {
7744 lognv = fnvlist_alloc();
7745 fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL,
7746 vec->zvec_name);
7747 if (!nvlist_empty(innvl)) {
7748 fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL,
7749 innvl);
7750 }
7751 }
7752
79c76d5b 7753 outnvl = fnvlist_alloc();
40d06e3c 7754 cookie = spl_fstrans_mark();
6f1ffb06 7755 error = vec->zvec_func(zc->zc_name, innvl, outnvl);
40d06e3c 7756 spl_fstrans_unmark(cookie);
6f1ffb06 7757
d99a0153
CW
7758 /*
7759 * Some commands can partially execute, modify state, and still
7760 * return an error. In these cases, attempt to record what
7761 * was modified.
7762 */
7763 if ((error == 0 ||
7764 (cmd == ZFS_IOC_CHANNEL_PROGRAM && error != EINVAL)) &&
7765 vec->zvec_allow_log &&
6f1ffb06
MA
7766 spa_open(zc->zc_name, &spa, FTAG) == 0) {
7767 if (!nvlist_empty(outnvl)) {
d66aab7c
MA
7768 size_t out_size = fnvlist_size(outnvl);
7769 if (out_size > zfs_history_output_max) {
7770 fnvlist_add_int64(lognv,
7771 ZPOOL_HIST_OUTPUT_SIZE, out_size);
7772 } else {
7773 fnvlist_add_nvlist(lognv,
7774 ZPOOL_HIST_OUTPUT_NVL, outnvl);
7775 }
6f1ffb06 7776 }
d99a0153
CW
7777 if (error != 0) {
7778 fnvlist_add_int64(lognv, ZPOOL_HIST_ERRNO,
7779 error);
7780 }
2ac90457
MA
7781 fnvlist_add_int64(lognv, ZPOOL_HIST_ELAPSED_NS,
7782 gethrtime() - start_time);
6f1ffb06
MA
7783 (void) spa_history_log_nvl(spa, lognv);
7784 spa_close(spa, FTAG);
7785 }
7786 fnvlist_free(lognv);
7787
7788 if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) {
7789 int smusherror = 0;
7790 if (vec->zvec_smush_outnvlist) {
7791 smusherror = nvlist_smush(outnvl,
7792 zc->zc_nvlist_dst_size);
7793 }
7794 if (smusherror == 0)
7795 puterror = put_nvlist(zc, outnvl);
7796 }
7797
7798 if (puterror != 0)
7799 error = puterror;
7800
7801 nvlist_free(outnvl);
7802 } else {
40d06e3c 7803 cookie = spl_fstrans_mark();
6f1ffb06 7804 error = vec->zvec_legacy_func(zc);
40d06e3c 7805 spl_fstrans_unmark(cookie);
6f1ffb06
MA
7806 }
7807
7808out:
7809 nvlist_free(innvl);
6f1ffb06
MA
7810 if (error == 0 && vec->zvec_allow_log) {
7811 char *s = tsd_get(zfs_allow_log_key);
7812 if (s != NULL)
e4f5fa12 7813 kmem_strfree(s);
4458157b 7814 (void) tsd_set(zfs_allow_log_key, kmem_strdup(saved_poolname));
34dc7c2f 7815 }
4458157b
JL
7816 if (saved_poolname != NULL)
7817 kmem_free(saved_poolname, saved_poolname_len);
7818
51420321 7819 return (error);
34dc7c2f
BB
7820}
7821
7bb0c294
MM
7822int
7823zfs_kmod_init(void)
34dc7c2f 7824{
7bb0c294 7825 int error;
34dc7c2f 7826
7bb0c294
MM
7827 if ((error = zvol_init()) != 0)
7828 return (error);
34dc7c2f 7829
da92d5cb 7830 spa_init(SPA_MODE_READ | SPA_MODE_WRITE);
7bb0c294 7831 zfs_init();
a6cc9756 7832
7bb0c294 7833 zfs_ioctl_init();
34dc7c2f 7834
325f0235 7835 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
3937ab20
TC
7836 zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
7837 zfsdev_state_list->zs_minor = -1;
34dc7c2f 7838
7bb0c294
MM
7839 if ((error = zfsdev_attach()) != 0)
7840 goto out;
a6cc9756 7841
7bb0c294
MM
7842 tsd_create(&zfs_fsyncer_key, NULL);
7843 tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7844 tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
34dc7c2f 7845
7bb0c294
MM
7846 return (0);
7847out:
7848 zfs_fini();
7849 spa_fini();
7850 zvol_fini();
a6cc9756
BB
7851
7852 return (error);
34dc7c2f
BB
7853}
7854
7bb0c294
MM
7855void
7856zfs_kmod_fini(void)
34dc7c2f 7857{
faa296c7 7858 zfsdev_state_t *zs, *zsnext = NULL;
34dc7c2f 7859
7bb0c294
MM
7860 zfsdev_detach();
7861
325f0235 7862 mutex_destroy(&zfsdev_state_lock);
3937ab20 7863
faa296c7
JL
7864 for (zs = zfsdev_state_list; zs != NULL; zs = zsnext) {
7865 zsnext = zs->zs_next;
7866 if (zs->zs_onexit)
7867 zfs_onexit_destroy(zs->zs_onexit);
7868 if (zs->zs_zevent)
7869 zfs_zevent_destroy(zs->zs_zevent);
7870 kmem_free(zs, sizeof (zfsdev_state_t));
3937ab20 7871 }
325f0235 7872
4f072827 7873 zfs_ereport_taskq_fini(); /* run before zfs_fini() on Linux */
34dc7c2f
BB
7874 zfs_fini();
7875 spa_fini();
a0bd735a 7876 zvol_fini();
46e18b3f 7877
d5446cfc 7878 tsd_destroy(&zfs_fsyncer_key);
3fc050aa 7879 tsd_destroy(&rrw_tsd_key);
6f1ffb06 7880 tsd_destroy(&zfs_allow_log_key);
34dc7c2f 7881}
009cc8e8 7882
009cc8e8 7883ZFS_MODULE_PARAM(zfs, zfs_, max_nvlist_src_size, ULONG, ZMOD_RW,
7ada752a 7884 "Maximum size in bytes allowed for src nvlist passed with ZFS ioctls");
d66aab7c
MA
7885
7886ZFS_MODULE_PARAM(zfs, zfs_, history_output_max, ULONG, ZMOD_RW,
7ada752a 7887 "Maximum size in bytes of ZFS ioctl output that will be logged");