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