]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzfs_core/libzfs_core.c
OpenZFS 7614, 9064 - zfs device evacuation/removal
[mirror_zfs.git] / lib / libzfs_core / libzfs_core.c
CommitLineData
6f1ffb06
MA
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/*
234c91c5 23 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
95fd54a1 24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
bec1067d 25 * Copyright (c) 2017 Datto Inc.
d12f91fd 26 * Copyright 2017 RackTop Systems.
d3f2cd7e 27 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
6f1ffb06
MA
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 *
b8fce77b 50 * - Programmatic Error Handling. libzfs_core communicates errors with
6f1ffb06
MA
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
e2454897 90static int g_fd = -1;
6f1ffb06
MA
91static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
92static int g_refcount;
93
94int
95libzfs_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
110void
111libzfs_core_fini(void)
112{
113 (void) pthread_mutex_lock(&g_lock);
114 ASSERT3S(g_refcount, >, 0);
e2454897
GM
115
116 if (g_refcount > 0)
117 g_refcount--;
118
119 if (g_refcount == 0 && g_fd != -1) {
6f1ffb06 120 (void) close(g_fd);
e2454897
GM
121 g_fd = -1;
122 }
6f1ffb06
MA
123 (void) pthread_mutex_unlock(&g_lock);
124}
125
126static int
127lzc_ioctl(zfs_ioc_t ioc, const char *name,
128 nvlist_t *source, nvlist_t **resultp)
129{
13fe0198 130 zfs_cmd_t zc = {"\0"};
6f1ffb06 131 int error = 0;
bec1067d
AP
132 char *packed = NULL;
133 size_t size = 0;
6f1ffb06
MA
134
135 ASSERT3S(g_refcount, >, 0);
e2454897 136 VERIFY3S(g_fd, !=, -1);
6f1ffb06 137
bec1067d
AP
138 if (name != NULL)
139 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
6f1ffb06 140
bec1067d
AP
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 }
6f1ffb06
MA
146
147 if (resultp != NULL) {
13fe0198 148 *resultp = NULL;
234c91c5
CW
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 }
6f1ffb06
MA
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) {
d99a0153
CW
164 /*
165 * If ioctl exited with ENOMEM, we retry the ioctl after
166 * increasing the size of the destination nvlist.
167 *
234c91c5 168 * Channel programs that exit with ENOMEM ran over the
d99a0153
CW
169 * lua memory sandbox; they should not be retried.
170 */
171 if (errno == ENOMEM && resultp != NULL &&
172 ioc != ZFS_IOC_CHANNEL_PROGRAM) {
6f1ffb06
MA
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);
6f1ffb06
MA
189 }
190
191out:
b5256303
TC
192 if (packed != NULL)
193 fnvlist_pack_free(packed, size);
6f1ffb06
MA
194 free((void *)(uintptr_t)zc.zc_nvlist_dst);
195 return (error);
196}
197
198int
b5256303
TC
199lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props,
200 uint8_t *wkeydata, uint_t wkeylen)
6f1ffb06
MA
201{
202 int error;
b5256303 203 nvlist_t *hidden_args = NULL;
6f1ffb06 204 nvlist_t *args = fnvlist_alloc();
b5256303 205
e67a7ffb 206 fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
6f1ffb06
MA
207 if (props != NULL)
208 fnvlist_add_nvlist(args, "props", props);
b5256303
TC
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
6f1ffb06 217 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
b5256303 218 nvlist_free(hidden_args);
6f1ffb06
MA
219 nvlist_free(args);
220 return (error);
221}
222
223int
b5256303 224lzc_clone(const char *fsname, const char *origin, nvlist_t *props)
6f1ffb06
MA
225{
226 int error;
b5256303 227 nvlist_t *hidden_args = NULL;
6f1ffb06 228 nvlist_t *args = fnvlist_alloc();
b5256303 229
6f1ffb06
MA
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);
b5256303 234 nvlist_free(hidden_args);
6f1ffb06
MA
235 nvlist_free(args);
236 return (error);
237}
238
d12f91fd
GDN
239int
240lzc_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
a1d477c2
MA
261int
262lzc_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
6f1ffb06
MA
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
13fe0198 284 * be the errno of a (unspecified) snapshot that failed.
6f1ffb06
MA
285 */
286int
287lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
288{
289 nvpair_t *elem;
290 nvlist_t *args;
291 int error;
eca7b760 292 char pool[ZFS_MAX_DATASET_NAME_LEN];
6f1ffb06
MA
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
1a077756 331 * later destruction if 'defer' is set) or didn't exist to begin with.
6f1ffb06 332 *
13fe0198 333 * Otherwise the return value will be the errno of a (unspecified) snapshot
6f1ffb06
MA
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 */
338int
339lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
340{
341 nvpair_t *elem;
342 nvlist_t *args;
343 int error;
eca7b760 344 char pool[ZFS_MAX_DATASET_NAME_LEN];
6f1ffb06
MA
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);
6f1ffb06
MA
362}
363
364int
365lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
366 uint64_t *usedp)
367{
368 nvlist_t *args;
369 nvlist_t *result;
370 int err;
eca7b760 371 char fs[ZFS_MAX_DATASET_NAME_LEN];
6f1ffb06
MA
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
393boolean_t
394lzc_exists(const char *dataset)
395{
396 /*
397 * The objset_stats ioctl is still legacy, so we need to construct our
d12f91fd 398 * own zfs_cmd_t rather than using lzc_ioctl().
6f1ffb06 399 */
13fe0198 400 zfs_cmd_t zc = {"\0"};
6f1ffb06 401
e2454897
GM
402 ASSERT3S(g_refcount, >, 0);
403 VERIFY3S(g_fd, !=, -1);
404
6f1ffb06
MA
405 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
406 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
407}
408
bec1067d
AP
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*/
415int
416lzc_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
13fe0198
MA
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 *
95fd54a1 436 * Holds for snapshots which don't exist will be skipped and have an entry
1a077756 437 * added to errlist, but will not cause an overall failure.
95fd54a1 438 *
1a077756 439 * The return value will be 0 if all holds, for snapshots that existed,
b8fce77b 440 * were successfully created.
95fd54a1
SH
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).
13fe0198
MA
447 */
448int
449lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
450{
eca7b760 451 char pool[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
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.
d5884c34 481 * The value is an nvlist whose keys are the holds to remove.
13fe0198 482 *
95fd54a1 483 * Holds which failed to release because they didn't exist will have an entry
1a077756 484 * added to errlist, but will not cause an overall failure.
95fd54a1
SH
485 *
486 * The return value will be 0 if the nvl holds was empty or all holds that
1a077756 487 * existed, were successfully removed.
95fd54a1
SH
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.
13fe0198
MA
494 */
495int
496lzc_release(nvlist_t *holds, nvlist_t **errlist)
497{
eca7b760 498 char pool[ZFS_MAX_DATASET_NAME_LEN];
13fe0198
MA
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 *
d5884c34 514 * On success, *holdsp will be set to an nvlist which the caller must free.
13fe0198
MA
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 */
518int
519lzc_get_holds(const char *snapname, nvlist_t **holdsp)
520{
bec1067d 521 return (lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, NULL, holdsp));
13fe0198
MA
522}
523
6f1ffb06 524/*
9b67f605
MA
525 * Generate a zfs send stream for the specified snapshot and write it to
526 * the specified file descriptor.
da536844
MA
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.
9b67f605 540 *
f1512ee6
MA
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 *
9b67f605
MA
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).
6f1ffb06
MA
549 */
550int
9b67f605
MA
551lzc_send(const char *snapname, const char *from, int fd,
552 enum lzc_send_flags flags)
47dfff3b
MA
553{
554 return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
555}
556
557int
558lzc_send_resume(const char *snapname, const char *from, int fd,
559 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
6f1ffb06
MA
560{
561 nvlist_t *args;
562 int err;
563
564 args = fnvlist_alloc();
565 fnvlist_add_int32(args, "fd", fd);
da536844
MA
566 if (from != NULL)
567 fnvlist_add_string(args, "fromsnap", from);
f1512ee6
MA
568 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
569 fnvlist_add_boolean(args, "largeblockok");
9b67f605
MA
570 if (flags & LZC_SEND_FLAG_EMBED_DATA)
571 fnvlist_add_boolean(args, "embedok");
a7004725
DK
572 if (flags & LZC_SEND_FLAG_COMPRESS)
573 fnvlist_add_boolean(args, "compressok");
b5256303
TC
574 if (flags & LZC_SEND_FLAG_RAW)
575 fnvlist_add_boolean(args, "rawok");
47dfff3b
MA
576 if (resumeobj != 0 || resumeoff != 0) {
577 fnvlist_add_uint64(args, "resume_object", resumeobj);
578 fnvlist_add_uint64(args, "resume_offset", resumeoff);
579 }
6f1ffb06
MA
580 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
581 nvlist_free(args);
582 return (err);
583}
584
585/*
5dc8b736
MG
586 * "from" can be NULL, a snapshot, or a bookmark.
587 *
588 * If from is NULL, a full (non-incremental) stream will be estimated. This
589 * is calculated very efficiently.
590 *
591 * If from is a snapshot, lzc_send_space uses the deadlists attached to
592 * each snapshot to efficiently estimate the stream size.
593 *
594 * If from is a bookmark, the indirect blocks in the destination snapshot
595 * are traversed, looking for blocks with a birth time since the creation TXG of
596 * the snapshot this bookmark was created from. This will result in
597 * significantly more I/O and be less efficient than a send space estimation on
598 * an equivalent snapshot.
6f1ffb06
MA
599 */
600int
2aa34383
DK
601lzc_send_space(const char *snapname, const char *from,
602 enum lzc_send_flags flags, uint64_t *spacep)
6f1ffb06
MA
603{
604 nvlist_t *args;
605 nvlist_t *result;
606 int err;
607
608 args = fnvlist_alloc();
5dc8b736
MG
609 if (from != NULL)
610 fnvlist_add_string(args, "from", from);
2aa34383
DK
611 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
612 fnvlist_add_boolean(args, "largeblockok");
613 if (flags & LZC_SEND_FLAG_EMBED_DATA)
614 fnvlist_add_boolean(args, "embedok");
615 if (flags & LZC_SEND_FLAG_COMPRESS)
616 fnvlist_add_boolean(args, "compressok");
cf7684bc 617 if (flags & LZC_SEND_FLAG_RAW)
618 fnvlist_add_boolean(args, "rawok");
6f1ffb06
MA
619 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
620 nvlist_free(args);
621 if (err == 0)
622 *spacep = fnvlist_lookup_uint64(result, "space");
623 nvlist_free(result);
624 return (err);
625}
626
627static int
628recv_read(int fd, void *buf, int ilen)
629{
630 char *cp = buf;
631 int rv;
632 int len = ilen;
633
634 do {
635 rv = read(fd, cp, len);
636 cp += rv;
637 len -= rv;
638 } while (rv > 0);
639
640 if (rv < 0 || len != 0)
641 return (EIO);
642
643 return (0);
644}
645
43e52edd 646/*
b5256303
TC
647 * Linux adds ZFS_IOC_RECV_NEW for resumable and raw streams and preserves the
648 * legacy ZFS_IOC_RECV user/kernel interface. The new interface supports all
649 * stream options but is currently only used for resumable streams. This way
650 * updated user space utilities will interoperate with older kernel modules.
43e52edd
BB
651 *
652 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
653 */
47dfff3b 654static int
a3eeab2d 655recv_impl(const char *snapname, nvlist_t *recvdprops, nvlist_t *localprops,
b5256303
TC
656 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
657 int input_fd, const dmu_replay_record_t *begin_record, int cleanup_fd,
43e52edd
BB
658 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
659 nvlist_t **errors)
6f1ffb06 660{
43e52edd
BB
661 dmu_replay_record_t drr;
662 char fsname[MAXPATHLEN];
6f1ffb06 663 char *atp;
6f1ffb06
MA
664 int error;
665
e2454897
GM
666 ASSERT3S(g_refcount, >, 0);
667 VERIFY3S(g_fd, !=, -1);
668
43e52edd
BB
669 /* Set 'fsname' to the name of containing filesystem */
670 (void) strlcpy(fsname, snapname, sizeof (fsname));
671 atp = strchr(fsname, '@');
6f1ffb06
MA
672 if (atp == NULL)
673 return (EINVAL);
674 *atp = '\0';
675
43e52edd
BB
676 /* If the fs does not exist, try its parent. */
677 if (!lzc_exists(fsname)) {
678 char *slashp = strrchr(fsname, '/');
6f1ffb06
MA
679 if (slashp == NULL)
680 return (ENOENT);
681 *slashp = '\0';
43e52edd 682 }
6f1ffb06 683
43e52edd
BB
684 /*
685 * The begin_record is normally a non-byteswapped BEGIN record.
686 * For resumable streams it may be set to any non-byteswapped
687 * dmu_replay_record_t.
688 */
689 if (begin_record == NULL) {
690 error = recv_read(input_fd, &drr, sizeof (drr));
691 if (error != 0)
692 return (error);
693 } else {
694 drr = *begin_record;
6f1ffb06
MA
695 }
696
b5256303 697 if (resumable || raw) {
43e52edd
BB
698 nvlist_t *outnvl = NULL;
699 nvlist_t *innvl = fnvlist_alloc();
6f1ffb06 700
43e52edd 701 fnvlist_add_string(innvl, "snapname", snapname);
6f1ffb06 702
a3eeab2d 703 if (recvdprops != NULL)
704 fnvlist_add_nvlist(innvl, "props", recvdprops);
705
706 if (localprops != NULL)
707 fnvlist_add_nvlist(innvl, "localprops", localprops);
6f1ffb06 708
43e52edd
BB
709 if (origin != NULL && strlen(origin))
710 fnvlist_add_string(innvl, "origin", origin);
711
712 fnvlist_add_byte_array(innvl, "begin_record",
02730c33 713 (uchar_t *)&drr, sizeof (drr));
43e52edd
BB
714
715 fnvlist_add_int32(innvl, "input_fd", input_fd);
716
717 if (force)
718 fnvlist_add_boolean(innvl, "force");
719
720 if (resumable)
721 fnvlist_add_boolean(innvl, "resumable");
722
723 if (cleanup_fd >= 0)
724 fnvlist_add_int32(innvl, "cleanup_fd", cleanup_fd);
725
726 if (action_handle != NULL)
727 fnvlist_add_uint64(innvl, "action_handle",
728 *action_handle);
729
730 error = lzc_ioctl(ZFS_IOC_RECV_NEW, fsname, innvl, &outnvl);
731
732 if (error == 0 && read_bytes != NULL)
733 error = nvlist_lookup_uint64(outnvl, "read_bytes",
734 read_bytes);
735
736 if (error == 0 && errflags != NULL)
737 error = nvlist_lookup_uint64(outnvl, "error_flags",
738 errflags);
739
740 if (error == 0 && action_handle != NULL)
741 error = nvlist_lookup_uint64(outnvl, "action_handle",
742 action_handle);
743
744 if (error == 0 && errors != NULL) {
745 nvlist_t *nvl;
746 error = nvlist_lookup_nvlist(outnvl, "errors", &nvl);
747 if (error == 0)
748 *errors = fnvlist_dup(nvl);
749 }
750
751 fnvlist_free(innvl);
752 fnvlist_free(outnvl);
fd41e935 753 } else {
43e52edd
BB
754 zfs_cmd_t zc = {"\0"};
755 char *packed = NULL;
756 size_t size;
6f1ffb06 757
43e52edd 758 ASSERT3S(g_refcount, >, 0);
6f1ffb06 759
43e52edd
BB
760 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_value));
761 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
6f1ffb06 762
a3eeab2d 763 if (recvdprops != NULL) {
764 packed = fnvlist_pack(recvdprops, &size);
43e52edd
BB
765 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
766 zc.zc_nvlist_src_size = size;
767 }
47dfff3b 768
a3eeab2d 769 if (localprops != NULL) {
770 packed = fnvlist_pack(localprops, &size);
771 zc.zc_nvlist_conf = (uint64_t)(uintptr_t)packed;
772 zc.zc_nvlist_conf_size = size;
773 }
774
43e52edd
BB
775 if (origin != NULL)
776 (void) strlcpy(zc.zc_string, origin,
777 sizeof (zc.zc_string));
6f1ffb06 778
43e52edd
BB
779 ASSERT3S(drr.drr_type, ==, DRR_BEGIN);
780 zc.zc_begin_record = drr.drr_u.drr_begin;
781 zc.zc_guid = force;
782 zc.zc_cookie = input_fd;
783 zc.zc_cleanup_fd = -1;
784 zc.zc_action_handle = 0;
785
786 if (cleanup_fd >= 0)
787 zc.zc_cleanup_fd = cleanup_fd;
788
789 if (action_handle != NULL)
790 zc.zc_action_handle = *action_handle;
791
792 zc.zc_nvlist_dst_size = 128 * 1024;
793 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
794 malloc(zc.zc_nvlist_dst_size);
795
796 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
797 if (error != 0) {
798 error = errno;
799 } else {
800 if (read_bytes != NULL)
801 *read_bytes = zc.zc_cookie;
802
803 if (errflags != NULL)
804 *errflags = zc.zc_obj;
805
806 if (action_handle != NULL)
807 *action_handle = zc.zc_action_handle;
808
809 if (errors != NULL)
810 VERIFY0(nvlist_unpack(
811 (void *)(uintptr_t)zc.zc_nvlist_dst,
812 zc.zc_nvlist_dst_size, errors, KM_SLEEP));
813 }
814
815 if (packed != NULL)
816 fnvlist_pack_free(packed, size);
817 free((void *)(uintptr_t)zc.zc_nvlist_dst);
818 }
6f1ffb06 819
6f1ffb06
MA
820 return (error);
821}
46ba1e59 822
47dfff3b
MA
823/*
824 * The simplest receive case: receive from the specified fd, creating the
825 * specified snapshot. Apply the specified properties as "received" properties
826 * (which can be overridden by locally-set properties). If the stream is a
827 * clone, its origin snapshot must be specified by 'origin'. The 'force'
828 * flag will cause the target filesystem to be rolled back or destroyed if
829 * necessary to receive.
830 *
831 * Return 0 on success or an errno on failure.
832 *
833 * Note: this interface does not work on dedup'd streams
834 * (those with DMU_BACKUP_FEATURE_DEDUP).
835 */
836int
837lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
b5256303 838 boolean_t force, boolean_t raw, int fd)
47dfff3b 839{
b5256303
TC
840 return (recv_impl(snapname, props, NULL, origin, force, B_FALSE, raw,
841 fd, NULL, -1, NULL, NULL, NULL, NULL));
47dfff3b
MA
842}
843
844/*
845 * Like lzc_receive, but if the receive fails due to premature stream
846 * termination, the intermediate state will be preserved on disk. In this
847 * case, ECKSUM will be returned. The receive may subsequently be resumed
848 * with a resuming send stream generated by lzc_send_resume().
849 */
850int
851lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
b5256303 852 boolean_t force, boolean_t raw, int fd)
47dfff3b 853{
b5256303
TC
854 return (recv_impl(snapname, props, NULL, origin, force, B_TRUE, raw,
855 fd, NULL, -1, NULL, NULL, NULL, NULL));
fd41e935
BB
856}
857
858/*
859 * Like lzc_receive, but allows the caller to read the begin record and then to
860 * pass it in. That could be useful if the caller wants to derive, for example,
861 * the snapname or the origin parameters based on the information contained in
862 * the begin record.
863 * The begin record must be in its original form as read from the stream,
864 * in other words, it should not be byteswapped.
865 *
866 * The 'resumable' parameter allows to obtain the same behavior as with
867 * lzc_receive_resumable.
868 */
869int
870lzc_receive_with_header(const char *snapname, nvlist_t *props,
b5256303
TC
871 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
872 int fd, const dmu_replay_record_t *begin_record)
fd41e935
BB
873{
874 if (begin_record == NULL)
875 return (EINVAL);
b5256303
TC
876
877 return (recv_impl(snapname, props, NULL, origin, force, resumable, raw,
878 fd, begin_record, -1, NULL, NULL, NULL, NULL));
43e52edd
BB
879}
880
881/*
882 * Like lzc_receive, but allows the caller to pass all supported arguments
883 * and retrieve all values returned. The only additional input parameter
884 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
885 *
886 * The following parameters all provide return values. Several may be set
887 * in the failure case and will contain additional information.
888 *
889 * The 'read_bytes' value will be set to the total number of bytes read.
890 *
891 * The 'errflags' value will contain zprop_errflags_t flags which are
892 * used to describe any failures.
893 *
894 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
895 * It should be set to zero on first call and will contain an updated handle
896 * on success, it should be passed in subsequent calls.
897 *
898 * The 'errors' nvlist contains an entry for each unapplied received
899 * property. Callers are responsible for freeing this nvlist.
900 */
901int lzc_receive_one(const char *snapname, nvlist_t *props,
b5256303
TC
902 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
903 int input_fd, const dmu_replay_record_t *begin_record, int cleanup_fd,
43e52edd
BB
904 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
905 nvlist_t **errors)
906{
a3eeab2d 907 return (recv_impl(snapname, props, NULL, origin, force, resumable,
b5256303 908 raw, input_fd, begin_record, cleanup_fd, read_bytes, errflags,
a3eeab2d 909 action_handle, errors));
910}
911
912/*
913 * Like lzc_receive_one, but allows the caller to pass an additional 'cmdprops'
914 * argument.
915 *
916 * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
917 * exclude ('zfs receive -x') properties. Callers are responsible for freeing
918 * this nvlist
919 */
920int lzc_receive_with_cmdprops(const char *snapname, nvlist_t *props,
921 nvlist_t *cmdprops, const char *origin, boolean_t force,
b5256303
TC
922 boolean_t resumable, boolean_t raw, int input_fd,
923 const dmu_replay_record_t *begin_record, int cleanup_fd,
924 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
925 nvlist_t **errors)
a3eeab2d 926{
927 return (recv_impl(snapname, props, cmdprops, origin, force, resumable,
b5256303 928 raw, input_fd, begin_record, cleanup_fd, read_bytes, errflags,
43e52edd 929 action_handle, errors));
47dfff3b
MA
930}
931
46ba1e59
MA
932/*
933 * Roll back this filesystem or volume to its most recent snapshot.
934 * If snapnamebuf is not NULL, it will be filled in with the name
935 * of the most recent snapshot.
8ca78ab0
AG
936 * Note that the latest snapshot may change if a new one is concurrently
937 * created or the current one is destroyed. lzc_rollback_to can be used
938 * to roll back to a specific latest snapshot.
46ba1e59
MA
939 *
940 * Return 0 on success or an errno on failure.
941 */
942int
943lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
944{
945 nvlist_t *args;
946 nvlist_t *result;
947 int err;
948
949 args = fnvlist_alloc();
950 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
951 nvlist_free(args);
952 if (err == 0 && snapnamebuf != NULL) {
953 const char *snapname = fnvlist_lookup_string(result, "target");
954 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
955 }
bb7ffdaf
GM
956 nvlist_free(result);
957
46ba1e59
MA
958 return (err);
959}
da536844 960
8ca78ab0
AG
961/*
962 * Roll back this filesystem or volume to the specified snapshot,
963 * if possible.
964 *
965 * Return 0 on success or an errno on failure.
966 */
967int
968lzc_rollback_to(const char *fsname, const char *snapname)
969{
970 nvlist_t *args;
971 nvlist_t *result;
972 int err;
973
974 args = fnvlist_alloc();
975 fnvlist_add_string(args, "target", snapname);
976 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
977 nvlist_free(args);
978 nvlist_free(result);
979 return (err);
980}
981
da536844
MA
982/*
983 * Creates bookmarks.
984 *
985 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
986 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
987 * snapshots must be in the same pool.
988 *
989 * The returned results nvlist will have an entry for each bookmark that failed.
990 * The value will be the (int32) error code.
991 *
992 * The return value will be 0 if all bookmarks were created, otherwise it will
993 * be the errno of a (undetermined) bookmarks that failed.
994 */
995int
996lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
997{
998 nvpair_t *elem;
999 int error;
eca7b760 1000 char pool[ZFS_MAX_DATASET_NAME_LEN];
da536844
MA
1001
1002 /* determine the pool name */
1003 elem = nvlist_next_nvpair(bookmarks, NULL);
1004 if (elem == NULL)
1005 return (0);
1006 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
1007 pool[strcspn(pool, "/#")] = '\0';
1008
1009 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
1010
1011 return (error);
1012}
1013
1014/*
1015 * Retrieve bookmarks.
1016 *
1017 * Retrieve the list of bookmarks for the given file system. The props
1018 * parameter is an nvlist of property names (with no values) that will be
1019 * returned for each bookmark.
1020 *
1021 * The following are valid properties on bookmarks, all of which are numbers
1022 * (represented as uint64 in the nvlist)
1023 *
1024 * "guid" - globally unique identifier of the snapshot it refers to
1025 * "createtxg" - txg when the snapshot it refers to was created
1026 * "creation" - timestamp when the snapshot it refers to was created
1027 *
1028 * The format of the returned nvlist as follows:
1029 * <short name of bookmark> -> {
1030 * <name of property> -> {
1031 * "value" -> uint64
1032 * }
1033 * }
1034 */
1035int
1036lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
1037{
1038 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
1039}
1040
1041/*
1042 * Destroys bookmarks.
1043 *
1044 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
1045 * They must all be in the same pool. Bookmarks are specified as
1046 * <fs>#<bmark>.
1047 *
1048 * Bookmarks that do not exist will be silently ignored.
1049 *
1050 * The return value will be 0 if all bookmarks that existed were destroyed.
1051 *
1052 * Otherwise the return value will be the errno of a (undetermined) bookmark
1053 * that failed, no bookmarks will be destroyed, and the errlist will have an
1054 * entry for each bookmarks that failed. The value in the errlist will be
1055 * the (int32) error code.
1056 */
1057int
1058lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
1059{
1060 nvpair_t *elem;
1061 int error;
eca7b760 1062 char pool[ZFS_MAX_DATASET_NAME_LEN];
da536844
MA
1063
1064 /* determine the pool name */
1065 elem = nvlist_next_nvpair(bmarks, NULL);
1066 if (elem == NULL)
1067 return (0);
1068 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
1069 pool[strcspn(pool, "/#")] = '\0';
1070
1071 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
1072
1073 return (error);
1074}
b5256303 1075
5b72a38d
SD
1076static int
1077lzc_channel_program_impl(const char *pool, const char *program, boolean_t sync,
1078 uint64_t instrlimit, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1079{
1080 int error;
1081 nvlist_t *args;
1082
1083 args = fnvlist_alloc();
1084 fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
1085 fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
1086 fnvlist_add_boolean_value(args, ZCP_ARG_SYNC, sync);
1087 fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
1088 fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
1089 error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
1090 fnvlist_free(args);
1091
1092 return (error);
1093}
1094
d99a0153
CW
1095/*
1096 * Executes a channel program.
1097 *
1098 * If this function returns 0 the channel program was successfully loaded and
1099 * ran without failing. Note that individual commands the channel program ran
1100 * may have failed and the channel program is responsible for reporting such
1101 * errors through outnvl if they are important.
1102 *
1103 * This method may also return:
1104 *
1105 * EINVAL The program contains syntax errors, or an invalid memory or time
1106 * limit was given. No part of the channel program was executed.
1107 * If caused by syntax errors, 'outnvl' contains information about the
1108 * errors.
1109 *
1110 * ECHRNG The program was executed, but encountered a runtime error, such as
1111 * calling a function with incorrect arguments, invoking the error()
1112 * function directly, failing an assert() command, etc. Some portion
1113 * of the channel program may have executed and committed changes.
1114 * Information about the failure can be found in 'outnvl'.
1115 *
1116 * ENOMEM The program fully executed, but the output buffer was not large
1117 * enough to store the returned value. No output is returned through
1118 * 'outnvl'.
1119 *
1120 * ENOSPC The program was terminated because it exceeded its memory usage
1121 * limit. Some portion of the channel program may have executed and
1122 * committed changes to disk. No output is returned through 'outnvl'.
1123 *
1124 * ETIME The program was terminated because it exceeded its Lua instruction
1125 * limit. Some portion of the channel program may have executed and
1126 * committed changes to disk. No output is returned through 'outnvl'.
1127 */
1128int
1129lzc_channel_program(const char *pool, const char *program, uint64_t instrlimit,
1130 uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1131{
5b72a38d
SD
1132 return (lzc_channel_program_impl(pool, program, B_TRUE, instrlimit,
1133 memlimit, argnvl, outnvl));
1134}
d99a0153 1135
5b72a38d
SD
1136/*
1137 * Executes a read-only channel program.
1138 *
1139 * A read-only channel program works programmatically the same way as a
1140 * normal channel program executed with lzc_channel_program(). The only
1141 * difference is it runs exclusively in open-context and therefore can
1142 * return faster. The downside to that, is that the program cannot change
1143 * on-disk state by calling functions from the zfs.sync submodule.
1144 *
1145 * The return values of this function (and their meaning) are exactly the
1146 * same as the ones described in lzc_channel_program().
1147 */
1148int
1149lzc_channel_program_nosync(const char *pool, const char *program,
1150 uint64_t timeout, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1151{
1152 return (lzc_channel_program_impl(pool, program, B_FALSE, timeout,
1153 memlimit, argnvl, outnvl));
d99a0153
CW
1154}
1155
b5256303
TC
1156/*
1157 * Performs key management functions
1158 *
1159 * crypto_cmd should be a value from zfs_ioc_crypto_cmd_t. If the command
1160 * specifies to load or change a wrapping key, the key should be specified in
1161 * the hidden_args nvlist so that it is not logged
1162 */
1163int
1164lzc_load_key(const char *fsname, boolean_t noop, uint8_t *wkeydata,
1165 uint_t wkeylen)
1166{
1167 int error;
1168 nvlist_t *ioc_args;
1169 nvlist_t *hidden_args;
1170
1171 if (wkeydata == NULL)
1172 return (EINVAL);
1173
1174 ioc_args = fnvlist_alloc();
1175 hidden_args = fnvlist_alloc();
1176 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata, wkeylen);
1177 fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1178 if (noop)
1179 fnvlist_add_boolean(ioc_args, "noop");
1180 error = lzc_ioctl(ZFS_IOC_LOAD_KEY, fsname, ioc_args, NULL);
1181 nvlist_free(hidden_args);
1182 nvlist_free(ioc_args);
1183
1184 return (error);
1185}
1186
1187int
1188lzc_unload_key(const char *fsname)
1189{
1190 return (lzc_ioctl(ZFS_IOC_UNLOAD_KEY, fsname, NULL, NULL));
1191}
1192
1193int
1194lzc_change_key(const char *fsname, uint64_t crypt_cmd, nvlist_t *props,
1195 uint8_t *wkeydata, uint_t wkeylen)
1196{
1197 int error;
1198 nvlist_t *ioc_args = fnvlist_alloc();
1199 nvlist_t *hidden_args = NULL;
1200
1201 fnvlist_add_uint64(ioc_args, "crypt_cmd", crypt_cmd);
1202
1203 if (wkeydata != NULL) {
1204 hidden_args = fnvlist_alloc();
1205 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
1206 wkeylen);
1207 fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1208 }
1209
1210 if (props != NULL)
1211 fnvlist_add_nvlist(ioc_args, "props", props);
1212
1213 error = lzc_ioctl(ZFS_IOC_CHANGE_KEY, fsname, ioc_args, NULL);
1214 nvlist_free(hidden_args);
1215 nvlist_free(ioc_args);
d99a0153 1216
b5256303
TC
1217 return (error);
1218}
d3f2cd7e
AB
1219
1220int
1221lzc_reopen(const char *pool_name, boolean_t scrub_restart)
1222{
1223 nvlist_t *args = fnvlist_alloc();
1224 int error;
1225
1226 fnvlist_add_boolean_value(args, "scrub_restart", scrub_restart);
1227
1228 error = lzc_ioctl(ZFS_IOC_POOL_REOPEN, pool_name, args, NULL);
1229 nvlist_free(args);
1230 return (error);
1231}