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