xref: /illumos-gate/usr/src/lib/libzfs_core/common/libzfs_core.c (revision 85f4cb87104c72587029a6e0f1663332c85ba118)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
24  * Copyright (c) 2013 Steven Hartland. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  * Copyright 2017 RackTop Systems.
27  * Copyright (c) 2017 Datto Inc.
28  */
29 
30 /*
31  * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
32  * It has the following characteristics:
33  *
34  *  - Thread Safe.  libzfs_core is accessible concurrently from multiple
35  *  threads.  This is accomplished primarily by avoiding global data
36  *  (e.g. caching).  Since it's thread-safe, there is no reason for a
37  *  process to have multiple libzfs "instances".  Therefore, we store
38  *  our few pieces of data (e.g. the file descriptor) in global
39  *  variables.  The fd is reference-counted so that the libzfs_core
40  *  library can be "initialized" multiple times (e.g. by different
41  *  consumers within the same process).
42  *
43  *  - Committed Interface.  The libzfs_core interface will be committed,
44  *  therefore consumers can compile against it and be confident that
45  *  their code will continue to work on future releases of this code.
46  *  Currently, the interface is Evolving (not Committed), but we intend
47  *  to commit to it once it is more complete and we determine that it
48  *  meets the needs of all consumers.
49  *
50  *  - Programatic Error Handling.  libzfs_core communicates errors with
51  *  defined error numbers, and doesn't print anything to stdout/stderr.
52  *
53  *  - Thin Layer.  libzfs_core is a thin layer, marshaling arguments
54  *  to/from the kernel ioctls.  There is generally a 1:1 correspondence
55  *  between libzfs_core functions and ioctls to /dev/zfs.
56  *
57  *  - Clear Atomicity.  Because libzfs_core functions are generally 1:1
58  *  with kernel ioctls, and kernel ioctls are general atomic, each
59  *  libzfs_core function is atomic.  For example, creating multiple
60  *  snapshots with a single call to lzc_snapshot() is atomic -- it
61  *  can't fail with only some of the requested snapshots created, even
62  *  in the event of power loss or system crash.
63  *
64  *  - Continued libzfs Support.  Some higher-level operations (e.g.
65  *  support for "zfs send -R") are too complicated to fit the scope of
66  *  libzfs_core.  This functionality will continue to live in libzfs.
67  *  Where appropriate, libzfs will use the underlying atomic operations
68  *  of libzfs_core.  For example, libzfs may implement "zfs send -R |
69  *  zfs receive" by using individual "send one snapshot", rename,
70  *  destroy, and "receive one snapshot" operations in libzfs_core.
71  *  /sbin/zfs and /zbin/zpool will link with both libzfs and
72  *  libzfs_core.  Other consumers should aim to use only libzfs_core,
73  *  since that will be the supported, stable interface going forwards.
74  */
75 
76 #include <libzfs_core.h>
77 #include <ctype.h>
78 #include <unistd.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #ifdef ZFS_DEBUG
82 #include <stdio.h>
83 #endif
84 #include <errno.h>
85 #include <fcntl.h>
86 #include <pthread.h>
87 #include <sys/nvpair.h>
88 #include <sys/param.h>
89 #include <sys/types.h>
90 #include <sys/stat.h>
91 #include <sys/zfs_ioctl.h>
92 
93 static int g_fd = -1;
94 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
95 static int g_refcount;
96 
97 #ifdef ZFS_DEBUG
98 static zfs_ioc_t fail_ioc_cmd;
99 static zfs_errno_t fail_ioc_err;
100 
101 static void
102 libzfs_core_debug_ioc(void)
103 {
104 	/*
105 	 * To test running newer user space binaries with kernel's
106 	 * that don't yet support an ioctl or a new ioctl arg we
107 	 * provide an override to intentionally fail an ioctl.
108 	 *
109 	 * USAGE:
110 	 * The override variable, ZFS_IOC_TEST, is of the form "cmd:err"
111 	 *
112 	 * For example, to fail a ZFS_IOC_POOL_CHECKPOINT with a
113 	 * ZFS_ERR_IOC_CMD_UNAVAIL, the string would be "0x5a4d:1029"
114 	 *
115 	 * $ sudo sh -c "ZFS_IOC_TEST=0x5a4d:1029 zpool checkpoint tank"
116 	 * cannot checkpoint 'tank': the loaded zfs module does not support
117 	 * this operation. A reboot may be required to enable this operation.
118 	 */
119 	if (fail_ioc_cmd == 0) {
120 		char *ioc_test = getenv("ZFS_IOC_TEST");
121 		unsigned int ioc_num = 0, ioc_err = 0;
122 
123 		if (ioc_test != NULL &&
124 		    sscanf(ioc_test, "%i:%i", &ioc_num, &ioc_err) == 2 &&
125 		    ioc_num < ZFS_IOC_LAST)  {
126 			fail_ioc_cmd = ioc_num;
127 			fail_ioc_err = ioc_err;
128 		}
129 	}
130 }
131 #endif
132 
133 int
134 libzfs_core_init(void)
135 {
136 	(void) pthread_mutex_lock(&g_lock);
137 	if (g_refcount == 0) {
138 		g_fd = open("/dev/zfs", O_RDWR);
139 		if (g_fd < 0) {
140 			(void) pthread_mutex_unlock(&g_lock);
141 			return (errno);
142 		}
143 	}
144 	g_refcount++;
145 
146 #ifdef ZFS_DEBUG
147 	libzfs_core_debug_ioc();
148 #endif
149 	(void) pthread_mutex_unlock(&g_lock);
150 	return (0);
151 }
152 
153 void
154 libzfs_core_fini(void)
155 {
156 	(void) pthread_mutex_lock(&g_lock);
157 	ASSERT3S(g_refcount, >, 0);
158 
159 	if (g_refcount > 0)
160 		g_refcount--;
161 
162 	if (g_refcount == 0 && g_fd != -1) {
163 		(void) close(g_fd);
164 		g_fd = -1;
165 	}
166 	(void) pthread_mutex_unlock(&g_lock);
167 }
168 
169 static int
170 lzc_ioctl(zfs_ioc_t ioc, const char *name,
171     nvlist_t *source, nvlist_t **resultp)
172 {
173 	zfs_cmd_t zc = { 0 };
174 	int error = 0;
175 	char *packed = NULL;
176 	size_t size = 0;
177 
178 	ASSERT3S(g_refcount, >, 0);
179 	VERIFY3S(g_fd, !=, -1);
180 
181 #ifdef ZFS_DEBUG
182 	if (ioc == fail_ioc_cmd)
183 		return (fail_ioc_err);
184 #endif
185 
186 	if (name != NULL)
187 		(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
188 
189 	if (source != NULL) {
190 		packed = fnvlist_pack(source, &size);
191 		zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
192 		zc.zc_nvlist_src_size = size;
193 	}
194 
195 	if (resultp != NULL) {
196 		*resultp = NULL;
197 		if (ioc == ZFS_IOC_CHANNEL_PROGRAM) {
198 			zc.zc_nvlist_dst_size = fnvlist_lookup_uint64(source,
199 			    ZCP_ARG_MEMLIMIT);
200 		} else {
201 			zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
202 		}
203 		zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
204 		    malloc(zc.zc_nvlist_dst_size);
205 		if (zc.zc_nvlist_dst == 0) {
206 			error = ENOMEM;
207 			goto out;
208 		}
209 	}
210 
211 	while (ioctl(g_fd, ioc, &zc) != 0) {
212 		/*
213 		 * If ioctl exited with ENOMEM, we retry the ioctl after
214 		 * increasing the size of the destination nvlist.
215 		 *
216 		 * Channel programs that exit with ENOMEM ran over the
217 		 * lua memory sandbox; they should not be retried.
218 		 */
219 		if (errno == ENOMEM && resultp != NULL &&
220 		    ioc != ZFS_IOC_CHANNEL_PROGRAM) {
221 			free((void *)(uintptr_t)zc.zc_nvlist_dst);
222 			zc.zc_nvlist_dst_size *= 2;
223 			zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
224 			    malloc(zc.zc_nvlist_dst_size);
225 			if (zc.zc_nvlist_dst == 0) {
226 				error = ENOMEM;
227 				goto out;
228 			}
229 		} else {
230 			error = errno;
231 			break;
232 		}
233 	}
234 	if (zc.zc_nvlist_dst_filled) {
235 		*resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
236 		    zc.zc_nvlist_dst_size);
237 	}
238 
239 out:
240 	if (packed != NULL)
241 		fnvlist_pack_free(packed, size);
242 	free((void *)(uintptr_t)zc.zc_nvlist_dst);
243 	return (error);
244 }
245 
246 int
247 lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props,
248     uint8_t *wkeydata, uint_t wkeylen)
249 {
250 	int error;
251 	nvlist_t *hidden_args = NULL;
252 	nvlist_t *args = fnvlist_alloc();
253 
254 	fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
255 	if (props != NULL)
256 		fnvlist_add_nvlist(args, "props", props);
257 
258 	if (wkeydata != NULL) {
259 		hidden_args = fnvlist_alloc();
260 		fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
261 		    wkeylen);
262 		fnvlist_add_nvlist(args, ZPOOL_HIDDEN_ARGS, hidden_args);
263 	}
264 
265 	error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
266 	nvlist_free(hidden_args);
267 	nvlist_free(args);
268 	return (error);
269 }
270 
271 int
272 lzc_clone(const char *fsname, const char *origin, nvlist_t *props)
273 {
274 	int error;
275 	nvlist_t *hidden_args = NULL;
276 	nvlist_t *args = fnvlist_alloc();
277 
278 	fnvlist_add_string(args, "origin", origin);
279 	if (props != NULL)
280 		fnvlist_add_nvlist(args, "props", props);
281 	error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
282 	nvlist_free(hidden_args);
283 	nvlist_free(args);
284 	return (error);
285 }
286 
287 int
288 lzc_promote(const char *fsname, char *snapnamebuf, int snapnamelen)
289 {
290 	/*
291 	 * The promote ioctl is still legacy, so we need to construct our
292 	 * own zfs_cmd_t rather than using lzc_ioctl().
293 	 */
294 	zfs_cmd_t zc = { 0 };
295 
296 	ASSERT3S(g_refcount, >, 0);
297 	VERIFY3S(g_fd, !=, -1);
298 
299 	(void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
300 	if (ioctl(g_fd, ZFS_IOC_PROMOTE, &zc) != 0) {
301 		int error = errno;
302 		if (error == EEXIST && snapnamebuf != NULL)
303 			(void) strlcpy(snapnamebuf, zc.zc_string, snapnamelen);
304 		return (error);
305 	}
306 	return (0);
307 }
308 
309 int
310 lzc_remap(const char *fsname)
311 {
312 	int error;
313 	nvlist_t *args = fnvlist_alloc();
314 	error = lzc_ioctl(ZFS_IOC_REMAP, fsname, args, NULL);
315 	nvlist_free(args);
316 	return (error);
317 }
318 
319 int
320 lzc_rename(const char *source, const char *target)
321 {
322 	zfs_cmd_t zc = { 0 };
323 	int error;
324 
325 	ASSERT3S(g_refcount, >, 0);
326 	VERIFY3S(g_fd, !=, -1);
327 
328 	(void) strlcpy(zc.zc_name, source, sizeof (zc.zc_name));
329 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
330 	error = ioctl(g_fd, ZFS_IOC_RENAME, &zc);
331 	if (error != 0)
332 		error = errno;
333 	return (error);
334 }
335 
336 int
337 lzc_destroy(const char *fsname)
338 {
339 	int error;
340 
341 	nvlist_t *args = fnvlist_alloc();
342 	error = lzc_ioctl(ZFS_IOC_DESTROY, fsname, args, NULL);
343 	nvlist_free(args);
344 	return (error);
345 }
346 
347 /*
348  * Creates snapshots.
349  *
350  * The keys in the snaps nvlist are the snapshots to be created.
351  * They must all be in the same pool.
352  *
353  * The props nvlist is properties to set.  Currently only user properties
354  * are supported.  { user:prop_name -> string value }
355  *
356  * The returned results nvlist will have an entry for each snapshot that failed.
357  * The value will be the (int32) error code.
358  *
359  * The return value will be 0 if all snapshots were created, otherwise it will
360  * be the errno of a (unspecified) snapshot that failed.
361  */
362 int
363 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
364 {
365 	nvpair_t *elem;
366 	nvlist_t *args;
367 	int error;
368 	char pool[ZFS_MAX_DATASET_NAME_LEN];
369 
370 	*errlist = NULL;
371 
372 	/* determine the pool name */
373 	elem = nvlist_next_nvpair(snaps, NULL);
374 	if (elem == NULL)
375 		return (0);
376 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
377 	pool[strcspn(pool, "/@")] = '\0';
378 
379 	args = fnvlist_alloc();
380 	fnvlist_add_nvlist(args, "snaps", snaps);
381 	if (props != NULL)
382 		fnvlist_add_nvlist(args, "props", props);
383 
384 	error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
385 	nvlist_free(args);
386 
387 	return (error);
388 }
389 
390 /*
391  * Destroys snapshots.
392  *
393  * The keys in the snaps nvlist are the snapshots to be destroyed.
394  * They must all be in the same pool.
395  *
396  * Snapshots that do not exist will be silently ignored.
397  *
398  * If 'defer' is not set, and a snapshot has user holds or clones, the
399  * destroy operation will fail and none of the snapshots will be
400  * destroyed.
401  *
402  * If 'defer' is set, and a snapshot has user holds or clones, it will be
403  * marked for deferred destruction, and will be destroyed when the last hold
404  * or clone is removed/destroyed.
405  *
406  * The return value will be 0 if all snapshots were destroyed (or marked for
407  * later destruction if 'defer' is set) or didn't exist to begin with.
408  *
409  * Otherwise the return value will be the errno of a (unspecified) snapshot
410  * that failed, no snapshots will be destroyed, and the errlist will have an
411  * entry for each snapshot that failed.  The value in the errlist will be
412  * the (int32) error code.
413  */
414 int
415 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
416 {
417 	nvpair_t *elem;
418 	nvlist_t *args;
419 	int error;
420 	char pool[ZFS_MAX_DATASET_NAME_LEN];
421 
422 	/* determine the pool name */
423 	elem = nvlist_next_nvpair(snaps, NULL);
424 	if (elem == NULL)
425 		return (0);
426 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
427 	pool[strcspn(pool, "/@")] = '\0';
428 
429 	args = fnvlist_alloc();
430 	fnvlist_add_nvlist(args, "snaps", snaps);
431 	if (defer)
432 		fnvlist_add_boolean(args, "defer");
433 
434 	error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
435 	nvlist_free(args);
436 
437 	return (error);
438 }
439 
440 int
441 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
442     uint64_t *usedp)
443 {
444 	nvlist_t *args;
445 	nvlist_t *result;
446 	int err;
447 	char fs[ZFS_MAX_DATASET_NAME_LEN];
448 	char *atp;
449 
450 	/* determine the fs name */
451 	(void) strlcpy(fs, firstsnap, sizeof (fs));
452 	atp = strchr(fs, '@');
453 	if (atp == NULL)
454 		return (EINVAL);
455 	*atp = '\0';
456 
457 	args = fnvlist_alloc();
458 	fnvlist_add_string(args, "firstsnap", firstsnap);
459 
460 	err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
461 	nvlist_free(args);
462 	if (err == 0)
463 		*usedp = fnvlist_lookup_uint64(result, "used");
464 	fnvlist_free(result);
465 
466 	return (err);
467 }
468 
469 boolean_t
470 lzc_exists(const char *dataset)
471 {
472 	/*
473 	 * The objset_stats ioctl is still legacy, so we need to construct our
474 	 * own zfs_cmd_t rather than using lzc_ioctl().
475 	 */
476 	zfs_cmd_t zc = { 0 };
477 
478 	ASSERT3S(g_refcount, >, 0);
479 	VERIFY3S(g_fd, !=, -1);
480 
481 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
482 	return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
483 }
484 
485 /*
486  * outnvl is unused.
487  * It was added to preserve the function signature in case it is
488  * needed in the future.
489  */
490 /*ARGSUSED*/
491 int
492 lzc_sync(const char *pool_name, nvlist_t *innvl, nvlist_t **outnvl)
493 {
494 	return (lzc_ioctl(ZFS_IOC_POOL_SYNC, pool_name, innvl, NULL));
495 }
496 
497 /*
498  * Create "user holds" on snapshots.  If there is a hold on a snapshot,
499  * the snapshot can not be destroyed.  (However, it can be marked for deletion
500  * by lzc_destroy_snaps(defer=B_TRUE).)
501  *
502  * The keys in the nvlist are snapshot names.
503  * The snapshots must all be in the same pool.
504  * The value is the name of the hold (string type).
505  *
506  * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
507  * In this case, when the cleanup_fd is closed (including on process
508  * termination), the holds will be released.  If the system is shut down
509  * uncleanly, the holds will be released when the pool is next opened
510  * or imported.
511  *
512  * Holds for snapshots which don't exist will be skipped and have an entry
513  * added to errlist, but will not cause an overall failure.
514  *
515  * The return value will be 0 if all holds, for snapshots that existed,
516  * were succesfully created.
517  *
518  * Otherwise the return value will be the errno of a (unspecified) hold that
519  * failed and no holds will be created.
520  *
521  * In all cases the errlist will have an entry for each hold that failed
522  * (name = snapshot), with its value being the error code (int32).
523  */
524 int
525 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
526 {
527 	char pool[ZFS_MAX_DATASET_NAME_LEN];
528 	nvlist_t *args;
529 	nvpair_t *elem;
530 	int error;
531 
532 	/* determine the pool name */
533 	elem = nvlist_next_nvpair(holds, NULL);
534 	if (elem == NULL)
535 		return (0);
536 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
537 	pool[strcspn(pool, "/@")] = '\0';
538 
539 	args = fnvlist_alloc();
540 	fnvlist_add_nvlist(args, "holds", holds);
541 	if (cleanup_fd != -1)
542 		fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
543 
544 	error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
545 	nvlist_free(args);
546 	return (error);
547 }
548 
549 /*
550  * Release "user holds" on snapshots.  If the snapshot has been marked for
551  * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
552  * any clones, and all the user holds are removed, then the snapshot will be
553  * destroyed.
554  *
555  * The keys in the nvlist are snapshot names.
556  * The snapshots must all be in the same pool.
557  * The value is a nvlist whose keys are the holds to remove.
558  *
559  * Holds which failed to release because they didn't exist will have an entry
560  * added to errlist, but will not cause an overall failure.
561  *
562  * The return value will be 0 if the nvl holds was empty or all holds that
563  * existed, were successfully removed.
564  *
565  * Otherwise the return value will be the errno of a (unspecified) hold that
566  * failed to release and no holds will be released.
567  *
568  * In all cases the errlist will have an entry for each hold that failed to
569  * to release.
570  */
571 int
572 lzc_release(nvlist_t *holds, nvlist_t **errlist)
573 {
574 	char pool[ZFS_MAX_DATASET_NAME_LEN];
575 	nvpair_t *elem;
576 
577 	/* determine the pool name */
578 	elem = nvlist_next_nvpair(holds, NULL);
579 	if (elem == NULL)
580 		return (0);
581 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
582 	pool[strcspn(pool, "/@")] = '\0';
583 
584 	return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
585 }
586 
587 /*
588  * Retrieve list of user holds on the specified snapshot.
589  *
590  * On success, *holdsp will be set to a nvlist which the caller must free.
591  * The keys are the names of the holds, and the value is the creation time
592  * of the hold (uint64) in seconds since the epoch.
593  */
594 int
595 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
596 {
597 	return (lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, NULL, holdsp));
598 }
599 
600 /*
601  * Generate a zfs send stream for the specified snapshot and write it to
602  * the specified file descriptor.
603  *
604  * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
605  *
606  * If "from" is NULL, a full (non-incremental) stream will be sent.
607  * If "from" is non-NULL, it must be the full name of a snapshot or
608  * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
609  * "pool/fs#earlier_bmark").  If non-NULL, the specified snapshot or
610  * bookmark must represent an earlier point in the history of "snapname").
611  * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
612  * or it can be the origin of "snapname"'s filesystem, or an earlier
613  * snapshot in the origin, etc.
614  *
615  * "fd" is the file descriptor to write the send stream to.
616  *
617  * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
618  * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
619  * records with drr_blksz > 128K.
620  *
621  * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
622  * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
623  * which the receiving system must support (as indicated by support
624  * for the "embedded_data" feature).
625  */
626 int
627 lzc_send(const char *snapname, const char *from, int fd,
628     enum lzc_send_flags flags)
629 {
630 	return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
631 }
632 
633 int
634 lzc_send_resume(const char *snapname, const char *from, int fd,
635     enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
636 {
637 	nvlist_t *args;
638 	int err;
639 
640 	args = fnvlist_alloc();
641 	fnvlist_add_int32(args, "fd", fd);
642 	if (from != NULL)
643 		fnvlist_add_string(args, "fromsnap", from);
644 	if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
645 		fnvlist_add_boolean(args, "largeblockok");
646 	if (flags & LZC_SEND_FLAG_EMBED_DATA)
647 		fnvlist_add_boolean(args, "embedok");
648 	if (flags & LZC_SEND_FLAG_COMPRESS)
649 		fnvlist_add_boolean(args, "compressok");
650 	if (flags & LZC_SEND_FLAG_RAW)
651 		fnvlist_add_boolean(args, "rawok");
652 	if (resumeobj != 0 || resumeoff != 0) {
653 		fnvlist_add_uint64(args, "resume_object", resumeobj);
654 		fnvlist_add_uint64(args, "resume_offset", resumeoff);
655 	}
656 	err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
657 	nvlist_free(args);
658 	return (err);
659 }
660 
661 /*
662  * "from" can be NULL, a snapshot, or a bookmark.
663  *
664  * If from is NULL, a full (non-incremental) stream will be estimated.  This
665  * is calculated very efficiently.
666  *
667  * If from is a snapshot, lzc_send_space uses the deadlists attached to
668  * each snapshot to efficiently estimate the stream size.
669  *
670  * If from is a bookmark, the indirect blocks in the destination snapshot
671  * are traversed, looking for blocks with a birth time since the creation TXG of
672  * the snapshot this bookmark was created from.  This will result in
673  * significantly more I/O and be less efficient than a send space estimation on
674  * an equivalent snapshot.
675  */
676 int
677 lzc_send_space(const char *snapname, const char *from,
678     enum lzc_send_flags flags, uint64_t *spacep)
679 {
680 	nvlist_t *args;
681 	nvlist_t *result;
682 	int err;
683 
684 	args = fnvlist_alloc();
685 	if (from != NULL)
686 		fnvlist_add_string(args, "from", from);
687 	if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
688 		fnvlist_add_boolean(args, "largeblockok");
689 	if (flags & LZC_SEND_FLAG_EMBED_DATA)
690 		fnvlist_add_boolean(args, "embedok");
691 	if (flags & LZC_SEND_FLAG_COMPRESS)
692 		fnvlist_add_boolean(args, "compressok");
693 	err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
694 	nvlist_free(args);
695 	if (err == 0)
696 		*spacep = fnvlist_lookup_uint64(result, "space");
697 	nvlist_free(result);
698 	return (err);
699 }
700 
701 static int
702 recv_read(int fd, void *buf, int ilen)
703 {
704 	char *cp = buf;
705 	int rv;
706 	int len = ilen;
707 
708 	do {
709 		rv = read(fd, cp, len);
710 		cp += rv;
711 		len -= rv;
712 	} while (rv > 0);
713 
714 	if (rv < 0 || len != 0)
715 		return (EIO);
716 
717 	return (0);
718 }
719 
720 static int
721 recv_impl(const char *snapname, nvlist_t *recvdprops,  nvlist_t *localprops,
722     uint8_t *wkeydata, uint_t wkeylen, const char *origin, boolean_t force,
723     boolean_t resumable, boolean_t raw, int input_fd,
724     const dmu_replay_record_t *begin_record, int cleanup_fd,
725     uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
726     nvlist_t **errors)
727 {
728 
729 	/*
730 	 * The receive ioctl is still legacy, so we need to construct our own
731 	 * zfs_cmd_t rather than using zfsc_ioctl().
732 	 */
733 	zfs_cmd_t zc = { 0 };
734 	char *packed = NULL;
735 	size_t size;
736 
737 	dmu_replay_record_t drr;
738 	char fsname[MAXPATHLEN];
739 	char *atp;
740 	int error;
741 
742 	ASSERT3S(g_refcount, >, 0);
743 	VERIFY3S(g_fd, !=, -1);
744 
745 	/* Set 'fsname' to the name of containing filesystem */
746 	(void) strlcpy(fsname, snapname, sizeof (fsname));
747 	atp = strchr(fsname, '@');
748 	if (atp == NULL)
749 		return (EINVAL);
750 	*atp = '\0';
751 
752 	/* if the fs does not exist, try its parent. */
753 	if (!lzc_exists(fsname)) {
754 		char *slashp = strrchr(fsname, '/');
755 		if (slashp == NULL)
756 			return (ENOENT);
757 		*slashp = '\0';
758 	}
759 
760 	/*
761 	 * The begin_record is normally a non-byteswapped BEGIN record.
762 	 * For resumable streams it may be set to any non-byteswapped
763 	 * dmu_replay_record_t.
764 	 */
765 	if (begin_record == NULL) {
766 		error = recv_read(input_fd, &drr, sizeof (drr));
767 		if (error != 0)
768 			return (error);
769 	} else {
770 		drr = *begin_record;
771 	}
772 
773 	(void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
774 	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
775 
776 	if (recvdprops != NULL) {
777 		packed = fnvlist_pack(recvdprops, &size);
778 		zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
779 		zc.zc_nvlist_src_size = size;
780 	}
781 
782 	if (localprops != NULL) {
783 		packed = fnvlist_pack(localprops, &size);
784 		zc.zc_nvlist_conf = (uint64_t)(uintptr_t)packed;
785 		zc.zc_nvlist_conf_size = size;
786 	}
787 
788 	/* Use zc_history_ members for hidden args */
789 	if (wkeydata != NULL) {
790 		nvlist_t *hidden_args = fnvlist_alloc();
791 		fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
792 		    wkeylen);
793 		packed = fnvlist_pack(hidden_args, &size);
794 		zc.zc_history_offset = (uint64_t)(uintptr_t)packed;
795 		zc.zc_history_len = size;
796 	}
797 
798 	if (origin != NULL)
799 		(void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
800 
801 	ASSERT3S(drr.drr_type, ==, DRR_BEGIN);
802 	zc.zc_begin_record = drr;
803 	zc.zc_guid = force;
804 	zc.zc_cookie = input_fd;
805 	zc.zc_cleanup_fd = -1;
806 	zc.zc_action_handle = 0;
807 	zc.zc_resumable = resumable;
808 
809 	if (cleanup_fd >= 0)
810 		zc.zc_cleanup_fd = cleanup_fd;
811 
812 	if (action_handle != NULL)
813 		zc.zc_action_handle = *action_handle;
814 
815 	zc.zc_nvlist_dst_size = 128 * 1024;
816 	zc.zc_nvlist_dst = (uint64_t)(uintptr_t)malloc(zc.zc_nvlist_dst_size);
817 
818 	error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
819 	if (error != 0) {
820 		error = errno;
821 	} else {
822 		if (read_bytes != NULL)
823 			*read_bytes = zc.zc_cookie;
824 
825 		if (errflags != NULL)
826 			*errflags = zc.zc_obj;
827 
828 		if (action_handle != NULL)
829 			*action_handle = zc.zc_action_handle;
830 
831 		if (errors != NULL)
832 			VERIFY0(nvlist_unpack(
833 			    (void *)(uintptr_t)zc.zc_nvlist_dst,
834 			    zc.zc_nvlist_dst_size, errors, KM_SLEEP));
835 	}
836 
837 	if (packed != NULL)
838 		fnvlist_pack_free(packed, size);
839 	free((void*)(uintptr_t)zc.zc_nvlist_dst);
840 
841 	return (error);
842 }
843 
844 /*
845  * The simplest receive case: receive from the specified fd, creating the
846  * specified snapshot.  Apply the specified properties as "received" properties
847  * (which can be overridden by locally-set properties).  If the stream is a
848  * clone, its origin snapshot must be specified by 'origin'.  The 'force'
849  * flag will cause the target filesystem to be rolled back or destroyed if
850  * necessary to receive.
851  *
852  * Return 0 on success or an errno on failure.
853  *
854  * Note: this interface does not work on dedup'd streams
855  * (those with DMU_BACKUP_FEATURE_DEDUP).
856  */
857 int
858 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
859     boolean_t raw, boolean_t force, int fd)
860 {
861 	return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
862 	    B_FALSE, raw, fd, NULL, -1, NULL, NULL, NULL, NULL));
863 }
864 
865 /*
866  * Like lzc_receive, but if the receive fails due to premature stream
867  * termination, the intermediate state will be preserved on disk.  In this
868  * case, ECKSUM will be returned.  The receive may subsequently be resumed
869  * with a resuming send stream generated by lzc_send_resume().
870  */
871 int
872 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
873     boolean_t force, boolean_t raw, int fd)
874 {
875 	return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
876 	    B_TRUE, raw, fd, NULL, -1, NULL, NULL, NULL, NULL));
877 }
878 
879 /*
880  * Like lzc_receive, but allows the caller to read the begin record and then to
881  * pass it in.  That could be useful if the caller wants to derive, for example,
882  * the snapname or the origin parameters based on the information contained in
883  * the begin record.
884  * The begin record must be in its original form as read from the stream,
885  * in other words, it should not be byteswapped.
886  *
887  * The 'resumable' parameter allows to obtain the same behavior as with
888  * lzc_receive_resumable.
889  */
890 int
891 lzc_receive_with_header(const char *snapname, nvlist_t *props,
892     const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
893     int fd, const dmu_replay_record_t *begin_record)
894 {
895 	if (begin_record == NULL)
896 		return (EINVAL);
897 
898 	return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
899 	    resumable, raw, fd, begin_record, -1, NULL, NULL, NULL, NULL));
900 }
901 
902 /*
903  * Allows the caller to pass an additional 'cmdprops' argument.
904  *
905  * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
906  * exclude ('zfs receive -x') properties. Callers are responsible for freeing
907  * this nvlist
908  */
909 int lzc_receive_with_cmdprops(const char *snapname, nvlist_t *props,
910     nvlist_t *cmdprops, uint8_t *wkeydata, uint_t wkeylen, const char *origin,
911     boolean_t force, boolean_t resumable, boolean_t raw, int input_fd,
912     const dmu_replay_record_t *begin_record, int cleanup_fd,
913     uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
914     nvlist_t **errors)
915 {
916 	return (recv_impl(snapname, props, cmdprops, wkeydata, wkeylen, origin,
917 	    force, resumable, raw, input_fd, begin_record, cleanup_fd,
918 	    read_bytes, errflags, action_handle, errors));
919 }
920 
921 /*
922  * Roll back this filesystem or volume to its most recent snapshot.
923  * If snapnamebuf is not NULL, it will be filled in with the name
924  * of the most recent snapshot.
925  * Note that the latest snapshot may change if a new one is concurrently
926  * created or the current one is destroyed.  lzc_rollback_to can be used
927  * to roll back to a specific latest snapshot.
928  *
929  * Return 0 on success or an errno on failure.
930  */
931 int
932 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
933 {
934 	nvlist_t *args;
935 	nvlist_t *result;
936 	int err;
937 
938 	args = fnvlist_alloc();
939 	err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
940 	nvlist_free(args);
941 	if (err == 0 && snapnamebuf != NULL) {
942 		const char *snapname = fnvlist_lookup_string(result, "target");
943 		(void) strlcpy(snapnamebuf, snapname, snapnamelen);
944 	}
945 	nvlist_free(result);
946 
947 	return (err);
948 }
949 
950 /*
951  * Roll back this filesystem or volume to the specified snapshot,
952  * if possible.
953  *
954  * Return 0 on success or an errno on failure.
955  */
956 int
957 lzc_rollback_to(const char *fsname, const char *snapname)
958 {
959 	nvlist_t *args;
960 	nvlist_t *result;
961 	int err;
962 
963 	args = fnvlist_alloc();
964 	fnvlist_add_string(args, "target", snapname);
965 	err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
966 	nvlist_free(args);
967 	nvlist_free(result);
968 	return (err);
969 }
970 
971 /*
972  * Creates bookmarks.
973  *
974  * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
975  * the name of the snapshot (e.g. "pool/fs@snap").  All the bookmarks and
976  * snapshots must be in the same pool.
977  *
978  * The returned results nvlist will have an entry for each bookmark that failed.
979  * The value will be the (int32) error code.
980  *
981  * The return value will be 0 if all bookmarks were created, otherwise it will
982  * be the errno of a (undetermined) bookmarks that failed.
983  */
984 int
985 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
986 {
987 	nvpair_t *elem;
988 	int error;
989 	char pool[ZFS_MAX_DATASET_NAME_LEN];
990 
991 	/* determine the pool name */
992 	elem = nvlist_next_nvpair(bookmarks, NULL);
993 	if (elem == NULL)
994 		return (0);
995 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
996 	pool[strcspn(pool, "/#")] = '\0';
997 
998 	error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
999 
1000 	return (error);
1001 }
1002 
1003 /*
1004  * Retrieve bookmarks.
1005  *
1006  * Retrieve the list of bookmarks for the given file system. The props
1007  * parameter is an nvlist of property names (with no values) that will be
1008  * returned for each bookmark.
1009  *
1010  * The following are valid properties on bookmarks, all of which are numbers
1011  * (represented as uint64 in the nvlist)
1012  *
1013  * "guid" - globally unique identifier of the snapshot it refers to
1014  * "createtxg" - txg when the snapshot it refers to was created
1015  * "creation" - timestamp when the snapshot it refers to was created
1016  * "ivsetguid" - IVset guid for identifying encrypted snapshots
1017  *
1018  * The format of the returned nvlist as follows:
1019  * <short name of bookmark> -> {
1020  *     <name of property> -> {
1021  *         "value" -> uint64
1022  *     }
1023  *  }
1024  */
1025 int
1026 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
1027 {
1028 	return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
1029 }
1030 
1031 /*
1032  * Destroys bookmarks.
1033  *
1034  * The keys in the bmarks nvlist are the bookmarks to be destroyed.
1035  * They must all be in the same pool.  Bookmarks are specified as
1036  * <fs>#<bmark>.
1037  *
1038  * Bookmarks that do not exist will be silently ignored.
1039  *
1040  * The return value will be 0 if all bookmarks that existed were destroyed.
1041  *
1042  * Otherwise the return value will be the errno of a (undetermined) bookmark
1043  * that failed, no bookmarks will be destroyed, and the errlist will have an
1044  * entry for each bookmarks that failed.  The value in the errlist will be
1045  * the (int32) error code.
1046  */
1047 int
1048 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
1049 {
1050 	nvpair_t *elem;
1051 	int error;
1052 	char pool[ZFS_MAX_DATASET_NAME_LEN];
1053 
1054 	/* determine the pool name */
1055 	elem = nvlist_next_nvpair(bmarks, NULL);
1056 	if (elem == NULL)
1057 		return (0);
1058 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
1059 	pool[strcspn(pool, "/#")] = '\0';
1060 
1061 	error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
1062 
1063 	return (error);
1064 }
1065 
1066 static int
1067 lzc_channel_program_impl(const char *pool, const char *program, boolean_t sync,
1068     uint64_t instrlimit, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1069 {
1070 	int error;
1071 	nvlist_t *args;
1072 
1073 	args = fnvlist_alloc();
1074 	fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
1075 	fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
1076 	fnvlist_add_boolean_value(args, ZCP_ARG_SYNC, sync);
1077 	fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
1078 	fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
1079 	error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
1080 	fnvlist_free(args);
1081 
1082 	return (error);
1083 }
1084 
1085 /*
1086  * Executes a channel program.
1087  *
1088  * If this function returns 0 the channel program was successfully loaded and
1089  * ran without failing. Note that individual commands the channel program ran
1090  * may have failed and the channel program is responsible for reporting such
1091  * errors through outnvl if they are important.
1092  *
1093  * This method may also return:
1094  *
1095  * EINVAL   The program contains syntax errors, or an invalid memory or time
1096  *          limit was given. No part of the channel program was executed.
1097  *          If caused by syntax errors, 'outnvl' contains information about the
1098  *          errors.
1099  *
1100  * ECHRNG   The program was executed, but encountered a runtime error, such as
1101  *          calling a function with incorrect arguments, invoking the error()
1102  *          function directly, failing an assert() command, etc. Some portion
1103  *          of the channel program may have executed and committed changes.
1104  *          Information about the failure can be found in 'outnvl'.
1105  *
1106  * ENOMEM   The program fully executed, but the output buffer was not large
1107  *          enough to store the returned value. No output is returned through
1108  *          'outnvl'.
1109  *
1110  * ENOSPC   The program was terminated because it exceeded its memory usage
1111  *          limit. Some portion of the channel program may have executed and
1112  *          committed changes to disk. No output is returned through 'outnvl'.
1113  *
1114  * ETIME    The program was terminated because it exceeded its Lua instruction
1115  *          limit. Some portion of the channel program may have executed and
1116  *          committed changes to disk. No output is returned through 'outnvl'.
1117  */
1118 int
1119 lzc_channel_program(const char *pool, const char *program, uint64_t instrlimit,
1120     uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1121 {
1122 	return (lzc_channel_program_impl(pool, program, B_TRUE, instrlimit,
1123 	    memlimit, argnvl, outnvl));
1124 }
1125 
1126 /*
1127  * Creates a checkpoint for the specified pool.
1128  *
1129  * If this function returns 0 the pool was successfully checkpointed.
1130  *
1131  * This method may also return:
1132  *
1133  * ZFS_ERR_CHECKPOINT_EXISTS
1134  *	The pool already has a checkpoint. A pools can only have one
1135  *	checkpoint at most, at any given time.
1136  *
1137  * ZFS_ERR_DISCARDING_CHECKPOINT
1138  *	ZFS is in the middle of discarding a checkpoint for this pool.
1139  *	The pool can be checkpointed again once the discard is done.
1140  *
1141  * ZFS_DEVRM_IN_PROGRESS
1142  *	A vdev is currently being removed. The pool cannot be
1143  *	checkpointed until the device removal is done.
1144  *
1145  * ZFS_VDEV_TOO_BIG
1146  *	One or more top-level vdevs exceed the maximum vdev size
1147  *	supported for this feature.
1148  */
1149 int
1150 lzc_pool_checkpoint(const char *pool)
1151 {
1152 	int error;
1153 
1154 	nvlist_t *result = NULL;
1155 	nvlist_t *args = fnvlist_alloc();
1156 
1157 	error = lzc_ioctl(ZFS_IOC_POOL_CHECKPOINT, pool, args, &result);
1158 
1159 	fnvlist_free(args);
1160 	fnvlist_free(result);
1161 
1162 	return (error);
1163 }
1164 
1165 /*
1166  * Discard the checkpoint from the specified pool.
1167  *
1168  * If this function returns 0 the checkpoint was successfully discarded.
1169  *
1170  * This method may also return:
1171  *
1172  * ZFS_ERR_NO_CHECKPOINT
1173  *	The pool does not have a checkpoint.
1174  *
1175  * ZFS_ERR_DISCARDING_CHECKPOINT
1176  *	ZFS is already in the middle of discarding the checkpoint.
1177  */
1178 int
1179 lzc_pool_checkpoint_discard(const char *pool)
1180 {
1181 	int error;
1182 
1183 	nvlist_t *result = NULL;
1184 	nvlist_t *args = fnvlist_alloc();
1185 
1186 	error = lzc_ioctl(ZFS_IOC_POOL_DISCARD_CHECKPOINT, pool, args, &result);
1187 
1188 	fnvlist_free(args);
1189 	fnvlist_free(result);
1190 
1191 	return (error);
1192 }
1193 
1194 /*
1195  * Executes a read-only channel program.
1196  *
1197  * A read-only channel program works programmatically the same way as a
1198  * normal channel program executed with lzc_channel_program(). The only
1199  * difference is it runs exclusively in open-context and therefore can
1200  * return faster. The downside to that, is that the program cannot change
1201  * on-disk state by calling functions from the zfs.sync submodule.
1202  *
1203  * The return values of this function (and their meaning) are exactly the
1204  * same as the ones described in lzc_channel_program().
1205  */
1206 int
1207 lzc_channel_program_nosync(const char *pool, const char *program,
1208     uint64_t timeout, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1209 {
1210 	return (lzc_channel_program_impl(pool, program, B_FALSE, timeout,
1211 	    memlimit, argnvl, outnvl));
1212 }
1213 
1214 /*
1215  * Changes initializing state.
1216  *
1217  * vdevs should be a list of (<key>, guid) where guid is a uint64 vdev GUID.
1218  * The key is ignored.
1219  *
1220  * If there are errors related to vdev arguments, per-vdev errors are returned
1221  * in an nvlist with the key "vdevs". Each error is a (guid, errno) pair where
1222  * guid is stringified with PRIu64, and errno is one of the following as
1223  * an int64_t:
1224  *	- ENODEV if the device was not found
1225  *	- EINVAL if the devices is not a leaf or is not concrete (e.g. missing)
1226  *	- EROFS if the device is not writeable
1227  *	- EBUSY start requested but the device is already being either
1228  *	        initialized or trimmed
1229  *	- ESRCH cancel/suspend requested but device is not being initialized
1230  *
1231  * If the errlist is empty, then return value will be:
1232  *	- EINVAL if one or more arguments was invalid
1233  *	- Other spa_open failures
1234  *	- 0 if the operation succeeded
1235  */
1236 int
1237 lzc_initialize(const char *poolname, pool_initialize_func_t cmd_type,
1238     nvlist_t *vdevs, nvlist_t **errlist)
1239 {
1240 	int error;
1241 
1242 	nvlist_t *args = fnvlist_alloc();
1243 	fnvlist_add_uint64(args, ZPOOL_INITIALIZE_COMMAND, (uint64_t)cmd_type);
1244 	fnvlist_add_nvlist(args, ZPOOL_INITIALIZE_VDEVS, vdevs);
1245 
1246 	error = lzc_ioctl(ZFS_IOC_POOL_INITIALIZE, poolname, args, errlist);
1247 
1248 	fnvlist_free(args);
1249 
1250 	return (error);
1251 }
1252 
1253 /*
1254  * Changes TRIM state.
1255  *
1256  * vdevs should be a list of (<key>, guid) where guid is a uint64 vdev GUID.
1257  * The key is ignored.
1258  *
1259  * If there are errors related to vdev arguments, per-vdev errors are returned
1260  * in an nvlist with the key "vdevs". Each error is a (guid, errno) pair where
1261  * guid is stringified with PRIu64, and errno is one of the following as
1262  * an int64_t:
1263  *	- ENODEV if the device was not found
1264  *	- EINVAL if the devices is not a leaf or is not concrete (e.g. missing)
1265  *	- EROFS if the device is not writeable
1266  *	- EBUSY start requested but the device is already being either trimmed
1267  *	        or initialized
1268  *	- ESRCH cancel/suspend requested but device is not being initialized
1269  *	- EOPNOTSUPP if the device does not support TRIM (or secure TRIM)
1270  *
1271  * If the errlist is empty, then return value will be:
1272  *	- EINVAL if one or more arguments was invalid
1273  *	- Other spa_open failures
1274  *	- 0 if the operation succeeded
1275  */
1276 int
1277 lzc_trim(const char *poolname, pool_trim_func_t cmd_type, uint64_t rate,
1278     boolean_t secure, nvlist_t *vdevs, nvlist_t **errlist)
1279 {
1280 	int error;
1281 
1282 	nvlist_t *args = fnvlist_alloc();
1283 	fnvlist_add_uint64(args, ZPOOL_TRIM_COMMAND, (uint64_t)cmd_type);
1284 	fnvlist_add_nvlist(args, ZPOOL_TRIM_VDEVS, vdevs);
1285 	fnvlist_add_uint64(args, ZPOOL_TRIM_RATE, rate);
1286 	fnvlist_add_boolean_value(args, ZPOOL_TRIM_SECURE, secure);
1287 
1288 	error = lzc_ioctl(ZFS_IOC_POOL_TRIM, poolname, args, errlist);
1289 
1290 	fnvlist_free(args);
1291 
1292 	return (error);
1293 }
1294 
1295 /*
1296  * Performs key management functions
1297  *
1298  * crypto_cmd should be a value from zfs_ioc_crypto_cmd_t. If the command
1299  * specifies to load or change a wrapping key, the key should be specified in
1300  * the hidden_args nvlist so that it is not logged
1301  */
1302 int
1303 lzc_load_key(const char *fsname, boolean_t noop, uint8_t *wkeydata,
1304     uint_t wkeylen)
1305 {
1306 	int error;
1307 	nvlist_t *ioc_args;
1308 	nvlist_t *hidden_args;
1309 
1310 	if (wkeydata == NULL)
1311 		return (EINVAL);
1312 
1313 	ioc_args = fnvlist_alloc();
1314 	hidden_args = fnvlist_alloc();
1315 	fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata, wkeylen);
1316 	fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1317 	if (noop)
1318 		fnvlist_add_boolean(ioc_args, "noop");
1319 	error = lzc_ioctl(ZFS_IOC_LOAD_KEY, fsname, ioc_args, NULL);
1320 	nvlist_free(hidden_args);
1321 	nvlist_free(ioc_args);
1322 
1323 	return (error);
1324 }
1325 
1326 int
1327 lzc_unload_key(const char *fsname)
1328 {
1329 	return (lzc_ioctl(ZFS_IOC_UNLOAD_KEY, fsname, NULL, NULL));
1330 }
1331 
1332 int
1333 lzc_change_key(const char *fsname, uint64_t crypt_cmd, nvlist_t *props,
1334     uint8_t *wkeydata, uint_t wkeylen)
1335 {
1336 	int error;
1337 	nvlist_t *ioc_args = fnvlist_alloc();
1338 	nvlist_t *hidden_args = NULL;
1339 
1340 	fnvlist_add_uint64(ioc_args, "crypt_cmd", crypt_cmd);
1341 
1342 	if (wkeydata != NULL) {
1343 		hidden_args = fnvlist_alloc();
1344 		fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
1345 		    wkeylen);
1346 		fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1347 	}
1348 
1349 	if (props != NULL)
1350 		fnvlist_add_nvlist(ioc_args, "props", props);
1351 
1352 	error = lzc_ioctl(ZFS_IOC_CHANGE_KEY, fsname, ioc_args, NULL);
1353 	nvlist_free(hidden_args);
1354 	nvlist_free(ioc_args);
1355 	return (error);
1356 }
1357