1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 * Copyright 2017 RackTop Systems.
27 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
28 * Copyright (c) 2019, 2020 by Christian Schwarz. All rights reserved.
29 * Copyright (c) 2019 Datto Inc.
30 */
31
32 /*
33 * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
34 * It has the following characteristics:
35 *
36 * - Thread Safe. libzfs_core is accessible concurrently from multiple
37 * threads. This is accomplished primarily by avoiding global data
38 * (e.g. caching). Since it's thread-safe, there is no reason for a
39 * process to have multiple libzfs "instances". Therefore, we store
40 * our few pieces of data (e.g. the file descriptor) in global
41 * variables. The fd is reference-counted so that the libzfs_core
42 * library can be "initialized" multiple times (e.g. by different
43 * consumers within the same process).
44 *
45 * - Committed Interface. The libzfs_core interface will be committed,
46 * therefore consumers can compile against it and be confident that
47 * their code will continue to work on future releases of this code.
48 * Currently, the interface is Evolving (not Committed), but we intend
49 * to commit to it once it is more complete and we determine that it
50 * meets the needs of all consumers.
51 *
52 * - Programmatic Error Handling. libzfs_core communicates errors with
53 * defined error numbers, and doesn't print anything to stdout/stderr.
54 *
55 * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
56 * to/from the kernel ioctls. There is generally a 1:1 correspondence
57 * between libzfs_core functions and ioctls to ZFS_DEV.
58 *
59 * - Clear Atomicity. Because libzfs_core functions are generally 1:1
60 * with kernel ioctls, and kernel ioctls are general atomic, each
61 * libzfs_core function is atomic. For example, creating multiple
62 * snapshots with a single call to lzc_snapshot() is atomic -- it
63 * can't fail with only some of the requested snapshots created, even
64 * in the event of power loss or system crash.
65 *
66 * - Continued libzfs Support. Some higher-level operations (e.g.
67 * support for "zfs send -R") are too complicated to fit the scope of
68 * libzfs_core. This functionality will continue to live in libzfs.
69 * Where appropriate, libzfs will use the underlying atomic operations
70 * of libzfs_core. For example, libzfs may implement "zfs send -R |
71 * zfs receive" by using individual "send one snapshot", rename,
72 * destroy, and "receive one snapshot" operations in libzfs_core.
73 * /sbin/zfs and /sbin/zpool will link with both libzfs and
74 * libzfs_core. Other consumers should aim to use only libzfs_core,
75 * since that will be the supported, stable interface going forwards.
76 */
77
78 #include <libzfs_core.h>
79 #include <ctype.h>
80 #include <unistd.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #ifdef ZFS_DEBUG
84 #include <stdio.h>
85 #endif
86 #include <errno.h>
87 #include <fcntl.h>
88 #include <pthread.h>
89 #include <libzutil.h>
90 #include <sys/nvpair.h>
91 #include <sys/param.h>
92 #include <sys/types.h>
93 #include <sys/stat.h>
94 #include <sys/zfs_ioctl.h>
95 #if __FreeBSD__
96 #define BIG_PIPE_SIZE (64 * 1024) /* From sys/pipe.h */
97 #endif
98
99 static int g_fd = -1;
100 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
101 static int g_refcount;
102
103 #ifdef ZFS_DEBUG
104 static zfs_ioc_t fail_ioc_cmd = ZFS_IOC_LAST;
105 static zfs_errno_t fail_ioc_err;
106
107 static void
libzfs_core_debug_ioc(void)108 libzfs_core_debug_ioc(void)
109 {
110 /*
111 * To test running newer user space binaries with kernel's
112 * that don't yet support an ioctl or a new ioctl arg we
113 * provide an override to intentionally fail an ioctl.
114 *
115 * USAGE:
116 * The override variable, ZFS_IOC_TEST, is of the form "cmd:err"
117 *
118 * For example, to fail a ZFS_IOC_POOL_CHECKPOINT with a
119 * ZFS_ERR_IOC_CMD_UNAVAIL, the string would be "0x5a4d:1029"
120 *
121 * $ sudo sh -c "ZFS_IOC_TEST=0x5a4d:1029 zpool checkpoint tank"
122 * cannot checkpoint 'tank': the loaded zfs module does not support
123 * this operation. A reboot may be required to enable this operation.
124 */
125 if (fail_ioc_cmd == ZFS_IOC_LAST) {
126 char *ioc_test = getenv("ZFS_IOC_TEST");
127 unsigned int ioc_num = 0, ioc_err = 0;
128
129 if (ioc_test != NULL &&
130 sscanf(ioc_test, "%i:%i", &ioc_num, &ioc_err) == 2 &&
131 ioc_num < ZFS_IOC_LAST) {
132 fail_ioc_cmd = ioc_num;
133 fail_ioc_err = ioc_err;
134 }
135 }
136 }
137 #endif
138
139 int
libzfs_core_init(void)140 libzfs_core_init(void)
141 {
142 (void) pthread_mutex_lock(&g_lock);
143 if (g_refcount == 0) {
144 g_fd = open(ZFS_DEV, O_RDWR|O_CLOEXEC);
145 if (g_fd < 0) {
146 (void) pthread_mutex_unlock(&g_lock);
147 return (errno);
148 }
149 }
150 g_refcount++;
151
152 #ifdef ZFS_DEBUG
153 libzfs_core_debug_ioc();
154 #endif
155 (void) pthread_mutex_unlock(&g_lock);
156 return (0);
157 }
158
159 void
libzfs_core_fini(void)160 libzfs_core_fini(void)
161 {
162 (void) pthread_mutex_lock(&g_lock);
163 ASSERT3S(g_refcount, >, 0);
164
165 g_refcount--;
166
167 if (g_refcount == 0 && g_fd != -1) {
168 (void) close(g_fd);
169 g_fd = -1;
170 }
171 (void) pthread_mutex_unlock(&g_lock);
172 }
173
174 static int
lzc_ioctl(zfs_ioc_t ioc,const char * name,nvlist_t * source,nvlist_t ** resultp)175 lzc_ioctl(zfs_ioc_t ioc, const char *name,
176 nvlist_t *source, nvlist_t **resultp)
177 {
178 zfs_cmd_t zc = {"\0"};
179 int error = 0;
180 char *packed = NULL;
181 size_t size = 0;
182
183 ASSERT3S(g_refcount, >, 0);
184 VERIFY3S(g_fd, !=, -1);
185
186 #ifdef ZFS_DEBUG
187 if (ioc == fail_ioc_cmd)
188 return (fail_ioc_err);
189 #endif
190
191 if (name != NULL)
192 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
193
194 if (source != NULL) {
195 packed = fnvlist_pack(source, &size);
196 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
197 zc.zc_nvlist_src_size = size;
198 }
199
200 if (resultp != NULL) {
201 *resultp = NULL;
202 if (ioc == ZFS_IOC_CHANNEL_PROGRAM) {
203 zc.zc_nvlist_dst_size = fnvlist_lookup_uint64(source,
204 ZCP_ARG_MEMLIMIT);
205 } else {
206 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
207 }
208 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
209 malloc(zc.zc_nvlist_dst_size);
210 if (zc.zc_nvlist_dst == (uint64_t)0) {
211 error = ENOMEM;
212 goto out;
213 }
214 }
215
216 while (lzc_ioctl_fd(g_fd, ioc, &zc) != 0) {
217 /*
218 * If ioctl exited with ENOMEM, we retry the ioctl after
219 * increasing the size of the destination nvlist.
220 *
221 * Channel programs that exit with ENOMEM ran over the
222 * lua memory sandbox; they should not be retried.
223 */
224 if (errno == ENOMEM && resultp != NULL &&
225 ioc != ZFS_IOC_CHANNEL_PROGRAM) {
226 free((void *)(uintptr_t)zc.zc_nvlist_dst);
227 zc.zc_nvlist_dst_size *= 2;
228 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
229 malloc(zc.zc_nvlist_dst_size);
230 if (zc.zc_nvlist_dst == (uint64_t)0) {
231 error = ENOMEM;
232 goto out;
233 }
234 } else {
235 error = errno;
236 break;
237 }
238 }
239 if (zc.zc_nvlist_dst_filled && resultp != NULL) {
240 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
241 zc.zc_nvlist_dst_size);
242 }
243
244 out:
245 if (packed != NULL)
246 fnvlist_pack_free(packed, size);
247 free((void *)(uintptr_t)zc.zc_nvlist_dst);
248 return (error);
249 }
250
251 int
lzc_scrub(zfs_ioc_t ioc,const char * name,nvlist_t * source,nvlist_t ** resultp)252 lzc_scrub(zfs_ioc_t ioc, const char *name,
253 nvlist_t *source, nvlist_t **resultp)
254 {
255 return (lzc_ioctl(ioc, name, source, resultp));
256 }
257
258 int
lzc_create(const char * fsname,enum lzc_dataset_type type,nvlist_t * props,uint8_t * wkeydata,uint_t wkeylen)259 lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props,
260 uint8_t *wkeydata, uint_t wkeylen)
261 {
262 int error;
263 nvlist_t *hidden_args = NULL;
264 nvlist_t *args = fnvlist_alloc();
265
266 fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
267 if (props != NULL)
268 fnvlist_add_nvlist(args, "props", props);
269
270 if (wkeydata != NULL) {
271 hidden_args = fnvlist_alloc();
272 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
273 wkeylen);
274 fnvlist_add_nvlist(args, ZPOOL_HIDDEN_ARGS, hidden_args);
275 }
276
277 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
278 nvlist_free(hidden_args);
279 nvlist_free(args);
280 return (error);
281 }
282
283 int
lzc_clone(const char * fsname,const char * origin,nvlist_t * props)284 lzc_clone(const char *fsname, const char *origin, nvlist_t *props)
285 {
286 int error;
287 nvlist_t *hidden_args = NULL;
288 nvlist_t *args = fnvlist_alloc();
289
290 fnvlist_add_string(args, "origin", origin);
291 if (props != NULL)
292 fnvlist_add_nvlist(args, "props", props);
293 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
294 nvlist_free(hidden_args);
295 nvlist_free(args);
296 return (error);
297 }
298
299 int
lzc_promote(const char * fsname,char * snapnamebuf,int snapnamelen)300 lzc_promote(const char *fsname, char *snapnamebuf, int snapnamelen)
301 {
302 /*
303 * The promote ioctl is still legacy, so we need to construct our
304 * own zfs_cmd_t rather than using lzc_ioctl().
305 */
306 zfs_cmd_t zc = {"\0"};
307
308 ASSERT3S(g_refcount, >, 0);
309 VERIFY3S(g_fd, !=, -1);
310
311 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
312 if (lzc_ioctl_fd(g_fd, ZFS_IOC_PROMOTE, &zc) != 0) {
313 int error = errno;
314 if (error == EEXIST && snapnamebuf != NULL)
315 (void) strlcpy(snapnamebuf, zc.zc_string, snapnamelen);
316 return (error);
317 }
318 return (0);
319 }
320
321 int
lzc_rename(const char * source,const char * target)322 lzc_rename(const char *source, const char *target)
323 {
324 zfs_cmd_t zc = {"\0"};
325 int error;
326
327 ASSERT3S(g_refcount, >, 0);
328 VERIFY3S(g_fd, !=, -1);
329 (void) strlcpy(zc.zc_name, source, sizeof (zc.zc_name));
330 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
331 error = lzc_ioctl_fd(g_fd, ZFS_IOC_RENAME, &zc);
332 if (error != 0)
333 error = errno;
334 return (error);
335 }
336
337 int
lzc_destroy(const char * fsname)338 lzc_destroy(const char *fsname)
339 {
340 int error;
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
lzc_snapshot(nvlist_t * snaps,nvlist_t * props,nvlist_t ** errlist)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
lzc_destroy_snaps(nvlist_t * snaps,boolean_t defer,nvlist_t ** errlist)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
lzc_snaprange_space(const char * firstsnap,const char * lastsnap,uint64_t * usedp)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
lzc_exists(const char * dataset)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 (lzc_ioctl_fd(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 int
lzc_sync(const char * pool_name,nvlist_t * innvl,nvlist_t ** outnvl)491 lzc_sync(const char *pool_name, nvlist_t *innvl, nvlist_t **outnvl)
492 {
493 (void) outnvl;
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(ZFS_DEV, 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 successfully 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
lzc_hold(nvlist_t * holds,int cleanup_fd,nvlist_t ** errlist)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 an 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
lzc_release(nvlist_t * holds,nvlist_t ** errlist)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 an 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
lzc_get_holds(const char * snapname,nvlist_t ** holdsp)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 int
lzc_get_props(const char * poolname,nvlist_t ** props)601 lzc_get_props(const char *poolname, nvlist_t **props)
602 {
603 return (lzc_ioctl(ZFS_IOC_POOL_GET_PROPS, poolname, NULL, props));
604 }
605
606 static unsigned int
max_pipe_buffer(int infd)607 max_pipe_buffer(int infd)
608 {
609 #if __linux__
610 static unsigned int max;
611 if (max == 0) {
612 max = 1048576; /* fs/pipe.c default */
613
614 FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "re");
615 if (procf != NULL) {
616 if (fscanf(procf, "%u", &max) <= 0) {
617 /* ignore error: max untouched if parse fails */
618 }
619 fclose(procf);
620 }
621 }
622
623 unsigned int cur = fcntl(infd, F_GETPIPE_SZ);
624 /*
625 * Sadly, Linux has an unfixed deadlock if you do SETPIPE_SZ on a pipe
626 * with data in it.
627 * cf. #13232, https://bugzilla.kernel.org/show_bug.cgi?id=212295
628 *
629 * And since the problem is in waking up the writer, there's nothing
630 * we can do about it from here.
631 *
632 * So if people want to, they can set this, but they
633 * may regret it...
634 */
635 if (getenv("ZFS_SET_PIPE_MAX") == NULL)
636 return (cur);
637 if (cur < max && fcntl(infd, F_SETPIPE_SZ, max) != -1)
638 cur = max;
639 return (cur);
640 #else
641 /* FreeBSD automatically resizes */
642 (void) infd;
643 return (BIG_PIPE_SIZE);
644 #endif
645 }
646
647 #if __linux__
648 struct send_worker_ctx {
649 int from; /* read end of pipe, with send data; closed on exit */
650 int to; /* original arbitrary output fd; mustn't be a pipe */
651 };
652
653 static void *
send_worker(void * arg)654 send_worker(void *arg)
655 {
656 struct send_worker_ctx *ctx = arg;
657 unsigned int bufsiz = max_pipe_buffer(ctx->from);
658 ssize_t rd;
659
660 for (;;) {
661 rd = splice(ctx->from, NULL, ctx->to, NULL, bufsiz,
662 SPLICE_F_MOVE | SPLICE_F_MORE);
663 if ((rd == -1 && errno != EINTR) || rd == 0)
664 break;
665 }
666 int err = (rd == -1) ? errno : 0;
667 close(ctx->from);
668 return ((void *)(uintptr_t)err);
669 }
670 #endif
671
672 /*
673 * Since Linux 5.10, 4d03e3cc59828c82ee89ea6e27a2f3cdf95aaadf
674 * ("fs: don't allow kernel reads and writes without iter ops"),
675 * ZFS_IOC_SEND* will EINVAL when writing to /dev/null, /dev/zero, &c.
676 *
677 * This wrapper transparently executes func() with a pipe
678 * by spawning a thread to copy from that pipe to the original output
679 * in the background.
680 *
681 * Returns the error from func(), if nonzero,
682 * otherwise the error from the thread.
683 *
684 * No-op if orig_fd is -1, already a pipe (but the buffer size is bumped),
685 * and on not-Linux; as such, it is safe to wrap/call wrapped functions
686 * in a wrapped context.
687 */
688 int
lzc_send_wrapper(int (* func)(int,void *),int orig_fd,void * data)689 lzc_send_wrapper(int (*func)(int, void *), int orig_fd, void *data)
690 {
691 #if __linux__
692 struct stat sb;
693 if (orig_fd != -1 && fstat(orig_fd, &sb) == -1)
694 return (errno);
695 if (orig_fd == -1 || S_ISFIFO(sb.st_mode)) {
696 if (orig_fd != -1)
697 (void) max_pipe_buffer(orig_fd);
698 return (func(orig_fd, data));
699 }
700 if ((fcntl(orig_fd, F_GETFL) & O_ACCMODE) == O_RDONLY)
701 return (errno = EBADF);
702
703 int rw[2];
704 if (pipe2(rw, O_CLOEXEC) == -1)
705 return (errno);
706
707 int err;
708 pthread_t send_thread;
709 struct send_worker_ctx ctx = {.from = rw[0], .to = orig_fd};
710 if ((err = pthread_create(&send_thread, NULL, send_worker, &ctx))
711 != 0) {
712 close(rw[0]);
713 close(rw[1]);
714 return (errno = err);
715 }
716
717 err = func(rw[1], data);
718
719 void *send_err;
720 close(rw[1]);
721 pthread_join(send_thread, &send_err);
722 if (err == 0 && send_err != 0)
723 errno = err = (uintptr_t)send_err;
724
725 return (err);
726 #else
727 return (func(orig_fd, data));
728 #endif
729 }
730
731 /*
732 * Generate a zfs send stream for the specified snapshot and write it to
733 * the specified file descriptor.
734 *
735 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
736 *
737 * If "from" is NULL, a full (non-incremental) stream will be sent.
738 * If "from" is non-NULL, it must be the full name of a snapshot or
739 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
740 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
741 * bookmark must represent an earlier point in the history of "snapname").
742 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
743 * or it can be the origin of "snapname"'s filesystem, or an earlier
744 * snapshot in the origin, etc.
745 *
746 * "fd" is the file descriptor to write the send stream to.
747 *
748 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
749 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
750 * records with drr_blksz > 128K.
751 *
752 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
753 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
754 * which the receiving system must support (as indicated by support
755 * for the "embedded_data" feature).
756 *
757 * If "flags" contains LZC_SEND_FLAG_COMPRESS, the stream is generated by using
758 * compressed WRITE records for blocks which are compressed on disk and in
759 * memory. If the lz4_compress feature is active on the sending system, then
760 * the receiving system must have that feature enabled as well.
761 *
762 * If "flags" contains LZC_SEND_FLAG_RAW, the stream is generated, for encrypted
763 * datasets, by sending data exactly as it exists on disk. This allows backups
764 * to be taken even if encryption keys are not currently loaded.
765 */
766 int
lzc_send(const char * snapname,const char * from,int fd,enum lzc_send_flags flags)767 lzc_send(const char *snapname, const char *from, int fd,
768 enum lzc_send_flags flags)
769 {
770 return (lzc_send_resume_redacted(snapname, from, fd, flags, 0, 0,
771 NULL));
772 }
773
774 int
lzc_send_redacted(const char * snapname,const char * from,int fd,enum lzc_send_flags flags,const char * redactbook)775 lzc_send_redacted(const char *snapname, const char *from, int fd,
776 enum lzc_send_flags flags, const char *redactbook)
777 {
778 return (lzc_send_resume_redacted(snapname, from, fd, flags, 0, 0,
779 redactbook));
780 }
781
782 int
lzc_send_resume(const char * snapname,const char * from,int fd,enum lzc_send_flags flags,uint64_t resumeobj,uint64_t resumeoff)783 lzc_send_resume(const char *snapname, const char *from, int fd,
784 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
785 {
786 return (lzc_send_resume_redacted(snapname, from, fd, flags, resumeobj,
787 resumeoff, NULL));
788 }
789
790 /*
791 * snapname: The name of the "tosnap", or the snapshot whose contents we are
792 * sending.
793 * from: The name of the "fromsnap", or the incremental source.
794 * fd: File descriptor to write the stream to.
795 * flags: flags that determine features to be used by the stream.
796 * resumeobj: Object to resume from, for resuming send
797 * resumeoff: Offset to resume from, for resuming send.
798 * redactnv: nvlist of string -> boolean(ignored) containing the names of all
799 * the snapshots that we should redact with respect to.
800 * redactbook: Name of the redaction bookmark to create.
801 *
802 * Pre-wrapped.
803 */
804 static int
lzc_send_resume_redacted_cb_impl(const char * snapname,const char * from,int fd,enum lzc_send_flags flags,uint64_t resumeobj,uint64_t resumeoff,const char * redactbook)805 lzc_send_resume_redacted_cb_impl(const char *snapname, const char *from, int fd,
806 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff,
807 const char *redactbook)
808 {
809 nvlist_t *args;
810 int err;
811
812 args = fnvlist_alloc();
813 fnvlist_add_int32(args, "fd", fd);
814 if (from != NULL)
815 fnvlist_add_string(args, "fromsnap", from);
816 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
817 fnvlist_add_boolean(args, "largeblockok");
818 if (flags & LZC_SEND_FLAG_EMBED_DATA)
819 fnvlist_add_boolean(args, "embedok");
820 if (flags & LZC_SEND_FLAG_COMPRESS)
821 fnvlist_add_boolean(args, "compressok");
822 if (flags & LZC_SEND_FLAG_RAW)
823 fnvlist_add_boolean(args, "rawok");
824 if (flags & LZC_SEND_FLAG_SAVED)
825 fnvlist_add_boolean(args, "savedok");
826 if (resumeobj != 0 || resumeoff != 0) {
827 fnvlist_add_uint64(args, "resume_object", resumeobj);
828 fnvlist_add_uint64(args, "resume_offset", resumeoff);
829 }
830 if (redactbook != NULL)
831 fnvlist_add_string(args, "redactbook", redactbook);
832
833 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
834 nvlist_free(args);
835 return (err);
836 }
837
838 struct lzc_send_resume_redacted {
839 const char *snapname;
840 const char *from;
841 enum lzc_send_flags flags;
842 uint64_t resumeobj;
843 uint64_t resumeoff;
844 const char *redactbook;
845 };
846
847 static int
lzc_send_resume_redacted_cb(int fd,void * arg)848 lzc_send_resume_redacted_cb(int fd, void *arg)
849 {
850 struct lzc_send_resume_redacted *zsrr = arg;
851 return (lzc_send_resume_redacted_cb_impl(zsrr->snapname, zsrr->from,
852 fd, zsrr->flags, zsrr->resumeobj, zsrr->resumeoff,
853 zsrr->redactbook));
854 }
855
856 int
lzc_send_resume_redacted(const char * snapname,const char * from,int fd,enum lzc_send_flags flags,uint64_t resumeobj,uint64_t resumeoff,const char * redactbook)857 lzc_send_resume_redacted(const char *snapname, const char *from, int fd,
858 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff,
859 const char *redactbook)
860 {
861 struct lzc_send_resume_redacted zsrr = {
862 .snapname = snapname,
863 .from = from,
864 .flags = flags,
865 .resumeobj = resumeobj,
866 .resumeoff = resumeoff,
867 .redactbook = redactbook,
868 };
869 return (lzc_send_wrapper(lzc_send_resume_redacted_cb, fd, &zsrr));
870 }
871
872 /*
873 * "from" can be NULL, a snapshot, or a bookmark.
874 *
875 * If from is NULL, a full (non-incremental) stream will be estimated. This
876 * is calculated very efficiently.
877 *
878 * If from is a snapshot, lzc_send_space uses the deadlists attached to
879 * each snapshot to efficiently estimate the stream size.
880 *
881 * If from is a bookmark, the indirect blocks in the destination snapshot
882 * are traversed, looking for blocks with a birth time since the creation TXG of
883 * the snapshot this bookmark was created from. This will result in
884 * significantly more I/O and be less efficient than a send space estimation on
885 * an equivalent snapshot. This process is also used if redact_snaps is
886 * non-null.
887 *
888 * Pre-wrapped.
889 */
890 static int
lzc_send_space_resume_redacted_cb_impl(const char * snapname,const char * from,enum lzc_send_flags flags,uint64_t resumeobj,uint64_t resumeoff,uint64_t resume_bytes,const char * redactbook,int fd,uint64_t * spacep)891 lzc_send_space_resume_redacted_cb_impl(const char *snapname, const char *from,
892 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff,
893 uint64_t resume_bytes, const char *redactbook, int fd, uint64_t *spacep)
894 {
895 nvlist_t *args;
896 nvlist_t *result;
897 int err;
898
899 args = fnvlist_alloc();
900 if (from != NULL)
901 fnvlist_add_string(args, "from", from);
902 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
903 fnvlist_add_boolean(args, "largeblockok");
904 if (flags & LZC_SEND_FLAG_EMBED_DATA)
905 fnvlist_add_boolean(args, "embedok");
906 if (flags & LZC_SEND_FLAG_COMPRESS)
907 fnvlist_add_boolean(args, "compressok");
908 if (flags & LZC_SEND_FLAG_RAW)
909 fnvlist_add_boolean(args, "rawok");
910 if (resumeobj != 0 || resumeoff != 0) {
911 fnvlist_add_uint64(args, "resume_object", resumeobj);
912 fnvlist_add_uint64(args, "resume_offset", resumeoff);
913 fnvlist_add_uint64(args, "bytes", resume_bytes);
914 }
915 if (redactbook != NULL)
916 fnvlist_add_string(args, "redactbook", redactbook);
917 if (fd != -1)
918 fnvlist_add_int32(args, "fd", fd);
919
920 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
921 nvlist_free(args);
922 if (err == 0)
923 *spacep = fnvlist_lookup_uint64(result, "space");
924 nvlist_free(result);
925 return (err);
926 }
927
928 struct lzc_send_space_resume_redacted {
929 const char *snapname;
930 const char *from;
931 enum lzc_send_flags flags;
932 uint64_t resumeobj;
933 uint64_t resumeoff;
934 uint64_t resume_bytes;
935 const char *redactbook;
936 uint64_t *spacep;
937 };
938
939 static int
lzc_send_space_resume_redacted_cb(int fd,void * arg)940 lzc_send_space_resume_redacted_cb(int fd, void *arg)
941 {
942 struct lzc_send_space_resume_redacted *zssrr = arg;
943 return (lzc_send_space_resume_redacted_cb_impl(zssrr->snapname,
944 zssrr->from, zssrr->flags, zssrr->resumeobj, zssrr->resumeoff,
945 zssrr->resume_bytes, zssrr->redactbook, fd, zssrr->spacep));
946 }
947
948 int
lzc_send_space_resume_redacted(const char * snapname,const char * from,enum lzc_send_flags flags,uint64_t resumeobj,uint64_t resumeoff,uint64_t resume_bytes,const char * redactbook,int fd,uint64_t * spacep)949 lzc_send_space_resume_redacted(const char *snapname, const char *from,
950 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff,
951 uint64_t resume_bytes, const char *redactbook, int fd, uint64_t *spacep)
952 {
953 struct lzc_send_space_resume_redacted zssrr = {
954 .snapname = snapname,
955 .from = from,
956 .flags = flags,
957 .resumeobj = resumeobj,
958 .resumeoff = resumeoff,
959 .resume_bytes = resume_bytes,
960 .redactbook = redactbook,
961 .spacep = spacep,
962 };
963 return (lzc_send_wrapper(lzc_send_space_resume_redacted_cb,
964 fd, &zssrr));
965 }
966
967 int
lzc_send_space(const char * snapname,const char * from,enum lzc_send_flags flags,uint64_t * spacep)968 lzc_send_space(const char *snapname, const char *from,
969 enum lzc_send_flags flags, uint64_t *spacep)
970 {
971 return (lzc_send_space_resume_redacted(snapname, from, flags, 0, 0, 0,
972 NULL, -1, spacep));
973 }
974
975 static int
recv_read(int fd,void * buf,int ilen)976 recv_read(int fd, void *buf, int ilen)
977 {
978 char *cp = buf;
979 int rv;
980 int len = ilen;
981
982 do {
983 rv = read(fd, cp, len);
984 cp += rv;
985 len -= rv;
986 } while (rv > 0);
987
988 if (rv < 0 || len != 0)
989 return (EIO);
990
991 return (0);
992 }
993
994 /*
995 * Linux adds ZFS_IOC_RECV_NEW for resumable and raw streams and preserves the
996 * legacy ZFS_IOC_RECV user/kernel interface. The new interface supports all
997 * stream options but is currently only used for resumable streams. This way
998 * updated user space utilities will interoperate with older kernel modules.
999 *
1000 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
1001 */
1002 static int
recv_impl(const char * snapname,nvlist_t * recvdprops,nvlist_t * localprops,uint8_t * wkeydata,uint_t wkeylen,const char * origin,boolean_t force,boolean_t heal,boolean_t resumable,boolean_t raw,int input_fd,const dmu_replay_record_t * begin_record,uint64_t * read_bytes,uint64_t * errflags,nvlist_t ** errors)1003 recv_impl(const char *snapname, nvlist_t *recvdprops, nvlist_t *localprops,
1004 uint8_t *wkeydata, uint_t wkeylen, const char *origin, boolean_t force,
1005 boolean_t heal, boolean_t resumable, boolean_t raw, int input_fd,
1006 const dmu_replay_record_t *begin_record, uint64_t *read_bytes,
1007 uint64_t *errflags, nvlist_t **errors)
1008 {
1009 dmu_replay_record_t drr;
1010 char fsname[MAXPATHLEN];
1011 char *atp;
1012 int error;
1013 boolean_t payload = B_FALSE;
1014
1015 ASSERT3S(g_refcount, >, 0);
1016 VERIFY3S(g_fd, !=, -1);
1017
1018 /* Set 'fsname' to the name of containing filesystem */
1019 (void) strlcpy(fsname, snapname, sizeof (fsname));
1020 atp = strchr(fsname, '@');
1021 if (atp == NULL)
1022 return (EINVAL);
1023 *atp = '\0';
1024
1025 /* If the fs does not exist, try its parent. */
1026 if (!lzc_exists(fsname)) {
1027 char *slashp = strrchr(fsname, '/');
1028 if (slashp == NULL)
1029 return (ENOENT);
1030 *slashp = '\0';
1031 }
1032
1033 /*
1034 * It is not uncommon for gigabytes to be processed by zfs receive.
1035 * Speculatively increase the buffer size if supported by the platform.
1036 */
1037 struct stat sb;
1038 if (fstat(input_fd, &sb) == -1)
1039 return (errno);
1040 if (S_ISFIFO(sb.st_mode))
1041 (void) max_pipe_buffer(input_fd);
1042
1043 /*
1044 * The begin_record is normally a non-byteswapped BEGIN record.
1045 * For resumable streams it may be set to any non-byteswapped
1046 * dmu_replay_record_t.
1047 */
1048 if (begin_record == NULL) {
1049 error = recv_read(input_fd, &drr, sizeof (drr));
1050 if (error != 0)
1051 return (error);
1052 } else {
1053 drr = *begin_record;
1054 payload = (begin_record->drr_payloadlen != 0);
1055 }
1056
1057 /*
1058 * All receives with a payload should use the new interface.
1059 */
1060 if (resumable || heal || raw || wkeydata != NULL || payload) {
1061 nvlist_t *outnvl = NULL;
1062 nvlist_t *innvl = fnvlist_alloc();
1063
1064 fnvlist_add_string(innvl, "snapname", snapname);
1065
1066 if (recvdprops != NULL)
1067 fnvlist_add_nvlist(innvl, "props", recvdprops);
1068
1069 if (localprops != NULL)
1070 fnvlist_add_nvlist(innvl, "localprops", localprops);
1071
1072 if (wkeydata != NULL) {
1073 /*
1074 * wkeydata must be placed in the special
1075 * ZPOOL_HIDDEN_ARGS nvlist so that it
1076 * will not be printed to the zpool history.
1077 */
1078 nvlist_t *hidden_args = fnvlist_alloc();
1079 fnvlist_add_uint8_array(hidden_args, "wkeydata",
1080 wkeydata, wkeylen);
1081 fnvlist_add_nvlist(innvl, ZPOOL_HIDDEN_ARGS,
1082 hidden_args);
1083 nvlist_free(hidden_args);
1084 }
1085
1086 if (origin != NULL && strlen(origin))
1087 fnvlist_add_string(innvl, "origin", origin);
1088
1089 fnvlist_add_byte_array(innvl, "begin_record",
1090 (uchar_t *)&drr, sizeof (drr));
1091
1092 fnvlist_add_int32(innvl, "input_fd", input_fd);
1093
1094 if (force)
1095 fnvlist_add_boolean(innvl, "force");
1096
1097 if (resumable)
1098 fnvlist_add_boolean(innvl, "resumable");
1099
1100 if (heal)
1101 fnvlist_add_boolean(innvl, "heal");
1102
1103 error = lzc_ioctl(ZFS_IOC_RECV_NEW, fsname, innvl, &outnvl);
1104
1105 if (error == 0 && read_bytes != NULL)
1106 error = nvlist_lookup_uint64(outnvl, "read_bytes",
1107 read_bytes);
1108
1109 if (error == 0 && errflags != NULL)
1110 error = nvlist_lookup_uint64(outnvl, "error_flags",
1111 errflags);
1112
1113 if (error == 0 && errors != NULL) {
1114 nvlist_t *nvl;
1115 error = nvlist_lookup_nvlist(outnvl, "errors", &nvl);
1116 if (error == 0)
1117 *errors = fnvlist_dup(nvl);
1118 }
1119
1120 fnvlist_free(innvl);
1121 fnvlist_free(outnvl);
1122 } else {
1123 zfs_cmd_t zc = {"\0"};
1124 char *rp_packed = NULL;
1125 char *lp_packed = NULL;
1126 size_t size;
1127
1128 ASSERT3S(g_refcount, >, 0);
1129
1130 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
1131 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
1132
1133 if (recvdprops != NULL) {
1134 rp_packed = fnvlist_pack(recvdprops, &size);
1135 zc.zc_nvlist_src = (uint64_t)(uintptr_t)rp_packed;
1136 zc.zc_nvlist_src_size = size;
1137 }
1138
1139 if (localprops != NULL) {
1140 lp_packed = fnvlist_pack(localprops, &size);
1141 zc.zc_nvlist_conf = (uint64_t)(uintptr_t)lp_packed;
1142 zc.zc_nvlist_conf_size = size;
1143 }
1144
1145 if (origin != NULL)
1146 (void) strlcpy(zc.zc_string, origin,
1147 sizeof (zc.zc_string));
1148
1149 ASSERT3S(drr.drr_type, ==, DRR_BEGIN);
1150 zc.zc_begin_record = drr.drr_u.drr_begin;
1151 zc.zc_guid = force;
1152 zc.zc_cookie = input_fd;
1153 zc.zc_cleanup_fd = -1;
1154 zc.zc_action_handle = 0;
1155
1156 zc.zc_nvlist_dst_size = 128 * 1024;
1157 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
1158 malloc(zc.zc_nvlist_dst_size);
1159
1160 error = lzc_ioctl_fd(g_fd, ZFS_IOC_RECV, &zc);
1161 if (error != 0) {
1162 error = errno;
1163 } else {
1164 if (read_bytes != NULL)
1165 *read_bytes = zc.zc_cookie;
1166
1167 if (errflags != NULL)
1168 *errflags = zc.zc_obj;
1169
1170 if (errors != NULL)
1171 VERIFY0(nvlist_unpack(
1172 (void *)(uintptr_t)zc.zc_nvlist_dst,
1173 zc.zc_nvlist_dst_size, errors, KM_SLEEP));
1174 }
1175
1176 if (rp_packed != NULL)
1177 fnvlist_pack_free(rp_packed, size);
1178 if (lp_packed != NULL)
1179 fnvlist_pack_free(lp_packed, size);
1180 free((void *)(uintptr_t)zc.zc_nvlist_dst);
1181 }
1182
1183 return (error);
1184 }
1185
1186 /*
1187 * The simplest receive case: receive from the specified fd, creating the
1188 * specified snapshot. Apply the specified properties as "received" properties
1189 * (which can be overridden by locally-set properties). If the stream is a
1190 * clone, its origin snapshot must be specified by 'origin'. The 'force'
1191 * flag will cause the target filesystem to be rolled back or destroyed if
1192 * necessary to receive.
1193 *
1194 * Return 0 on success or an errno on failure.
1195 *
1196 * Note: this interface does not work on dedup'd streams
1197 * (those with DMU_BACKUP_FEATURE_DEDUP).
1198 */
1199 int
lzc_receive(const char * snapname,nvlist_t * props,const char * origin,boolean_t force,boolean_t raw,int fd)1200 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
1201 boolean_t force, boolean_t raw, int fd)
1202 {
1203 return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
1204 B_FALSE, B_FALSE, raw, fd, NULL, NULL, NULL, NULL));
1205 }
1206
1207 /*
1208 * Like lzc_receive, but if the receive fails due to premature stream
1209 * termination, the intermediate state will be preserved on disk. In this
1210 * case, ECKSUM will be returned. The receive may subsequently be resumed
1211 * with a resuming send stream generated by lzc_send_resume().
1212 */
1213 int
lzc_receive_resumable(const char * snapname,nvlist_t * props,const char * origin,boolean_t force,boolean_t raw,int fd)1214 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
1215 boolean_t force, boolean_t raw, int fd)
1216 {
1217 return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
1218 B_FALSE, B_TRUE, raw, fd, NULL, NULL, NULL, NULL));
1219 }
1220
1221 /*
1222 * Like lzc_receive, but allows the caller to read the begin record and then to
1223 * pass it in. That could be useful if the caller wants to derive, for example,
1224 * the snapname or the origin parameters based on the information contained in
1225 * the begin record.
1226 * The begin record must be in its original form as read from the stream,
1227 * in other words, it should not be byteswapped.
1228 *
1229 * The 'resumable' parameter allows to obtain the same behavior as with
1230 * lzc_receive_resumable.
1231 */
1232 int
lzc_receive_with_header(const char * snapname,nvlist_t * props,const char * origin,boolean_t force,boolean_t resumable,boolean_t raw,int fd,const dmu_replay_record_t * begin_record)1233 lzc_receive_with_header(const char *snapname, nvlist_t *props,
1234 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
1235 int fd, const dmu_replay_record_t *begin_record)
1236 {
1237 if (begin_record == NULL)
1238 return (EINVAL);
1239
1240 return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
1241 B_FALSE, resumable, raw, fd, begin_record, NULL, NULL, NULL));
1242 }
1243
1244 /*
1245 * Like lzc_receive, but allows the caller to pass all supported arguments
1246 * and retrieve all values returned. The only additional input parameter
1247 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
1248 *
1249 * The following parameters all provide return values. Several may be set
1250 * in the failure case and will contain additional information.
1251 *
1252 * The 'read_bytes' value will be set to the total number of bytes read.
1253 *
1254 * The 'errflags' value will contain zprop_errflags_t flags which are
1255 * used to describe any failures.
1256 *
1257 * The 'action_handle' and 'cleanup_fd' are no longer used, and are ignored.
1258 *
1259 * The 'errors' nvlist contains an entry for each unapplied received
1260 * property. Callers are responsible for freeing this nvlist.
1261 */
1262 int
lzc_receive_one(const char * snapname,nvlist_t * props,const char * origin,boolean_t force,boolean_t resumable,boolean_t raw,int input_fd,const dmu_replay_record_t * begin_record,int cleanup_fd,uint64_t * read_bytes,uint64_t * errflags,uint64_t * action_handle,nvlist_t ** errors)1263 lzc_receive_one(const char *snapname, nvlist_t *props,
1264 const char *origin, boolean_t force, boolean_t resumable, boolean_t raw,
1265 int input_fd, const dmu_replay_record_t *begin_record, int cleanup_fd,
1266 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
1267 nvlist_t **errors)
1268 {
1269 (void) action_handle, (void) cleanup_fd;
1270 return (recv_impl(snapname, props, NULL, NULL, 0, origin, force,
1271 B_FALSE, resumable, raw, input_fd, begin_record,
1272 read_bytes, errflags, errors));
1273 }
1274
1275 /*
1276 * Like lzc_receive_one, but allows the caller to pass an additional 'cmdprops'
1277 * argument.
1278 *
1279 * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
1280 * exclude ('zfs receive -x') properties. Callers are responsible for freeing
1281 * this nvlist
1282 */
1283 int
lzc_receive_with_cmdprops(const char * snapname,nvlist_t * props,nvlist_t * cmdprops,uint8_t * wkeydata,uint_t wkeylen,const char * origin,boolean_t force,boolean_t resumable,boolean_t raw,int input_fd,const dmu_replay_record_t * begin_record,int cleanup_fd,uint64_t * read_bytes,uint64_t * errflags,uint64_t * action_handle,nvlist_t ** errors)1284 lzc_receive_with_cmdprops(const char *snapname, nvlist_t *props,
1285 nvlist_t *cmdprops, uint8_t *wkeydata, uint_t wkeylen, const char *origin,
1286 boolean_t force, boolean_t resumable, boolean_t raw, int input_fd,
1287 const dmu_replay_record_t *begin_record, int cleanup_fd,
1288 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
1289 nvlist_t **errors)
1290 {
1291 (void) action_handle, (void) cleanup_fd;
1292 return (recv_impl(snapname, props, cmdprops, wkeydata, wkeylen, origin,
1293 force, B_FALSE, resumable, raw, input_fd, begin_record,
1294 read_bytes, errflags, errors));
1295 }
1296
1297 /*
1298 * Like lzc_receive_with_cmdprops, but allows the caller to pass an additional
1299 * 'heal' argument.
1300 *
1301 * The heal arguments tells us to heal the provided snapshot using the provided
1302 * send stream
1303 */
lzc_receive_with_heal(const char * snapname,nvlist_t * props,nvlist_t * cmdprops,uint8_t * wkeydata,uint_t wkeylen,const char * origin,boolean_t force,boolean_t heal,boolean_t resumable,boolean_t raw,int input_fd,const dmu_replay_record_t * begin_record,int cleanup_fd,uint64_t * read_bytes,uint64_t * errflags,uint64_t * action_handle,nvlist_t ** errors)1304 int lzc_receive_with_heal(const char *snapname, nvlist_t *props,
1305 nvlist_t *cmdprops, uint8_t *wkeydata, uint_t wkeylen, const char *origin,
1306 boolean_t force, boolean_t heal, boolean_t resumable, boolean_t raw,
1307 int input_fd, const dmu_replay_record_t *begin_record, int cleanup_fd,
1308 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
1309 nvlist_t **errors)
1310 {
1311 (void) action_handle, (void) cleanup_fd;
1312 return (recv_impl(snapname, props, cmdprops, wkeydata, wkeylen, origin,
1313 force, heal, resumable, raw, input_fd, begin_record,
1314 read_bytes, errflags, errors));
1315 }
1316
1317 /*
1318 * Roll back this filesystem or volume to its most recent snapshot.
1319 * If snapnamebuf is not NULL, it will be filled in with the name
1320 * of the most recent snapshot.
1321 * Note that the latest snapshot may change if a new one is concurrently
1322 * created or the current one is destroyed. lzc_rollback_to can be used
1323 * to roll back to a specific latest snapshot.
1324 *
1325 * Return 0 on success or an errno on failure.
1326 */
1327 int
lzc_rollback(const char * fsname,char * snapnamebuf,int snapnamelen)1328 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
1329 {
1330 nvlist_t *args;
1331 nvlist_t *result;
1332 int err;
1333
1334 args = fnvlist_alloc();
1335 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
1336 nvlist_free(args);
1337 if (err == 0 && snapnamebuf != NULL) {
1338 const char *snapname = fnvlist_lookup_string(result, "target");
1339 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
1340 }
1341 nvlist_free(result);
1342
1343 return (err);
1344 }
1345
1346 /*
1347 * Roll back this filesystem or volume to the specified snapshot,
1348 * if possible.
1349 *
1350 * Return 0 on success or an errno on failure.
1351 */
1352 int
lzc_rollback_to(const char * fsname,const char * snapname)1353 lzc_rollback_to(const char *fsname, const char *snapname)
1354 {
1355 nvlist_t *args;
1356 nvlist_t *result;
1357 int err;
1358
1359 args = fnvlist_alloc();
1360 fnvlist_add_string(args, "target", snapname);
1361 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
1362 nvlist_free(args);
1363 nvlist_free(result);
1364 return (err);
1365 }
1366
1367 /*
1368 * Creates new bookmarks from existing snapshot or bookmark.
1369 *
1370 * The bookmarks nvlist maps from the full name of the new bookmark to
1371 * the full name of the source snapshot or bookmark.
1372 * All the bookmarks and snapshots must be in the same pool.
1373 * The new bookmarks names must be unique.
1374 * => see function dsl_bookmark_create_nvl_validate
1375 *
1376 * The returned results nvlist will have an entry for each bookmark that failed.
1377 * The value will be the (int32) error code.
1378 *
1379 * The return value will be 0 if all bookmarks were created, otherwise it will
1380 * be the errno of a (undetermined) bookmarks that failed.
1381 */
1382 int
lzc_bookmark(nvlist_t * bookmarks,nvlist_t ** errlist)1383 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
1384 {
1385 nvpair_t *elem;
1386 int error;
1387 char pool[ZFS_MAX_DATASET_NAME_LEN];
1388
1389 /* determine pool name from first bookmark */
1390 elem = nvlist_next_nvpair(bookmarks, NULL);
1391 if (elem == NULL)
1392 return (0);
1393 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
1394 pool[strcspn(pool, "/#")] = '\0';
1395
1396 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
1397
1398 return (error);
1399 }
1400
1401 /*
1402 * Retrieve bookmarks.
1403 *
1404 * Retrieve the list of bookmarks for the given file system. The props
1405 * parameter is an nvlist of property names (with no values) that will be
1406 * returned for each bookmark.
1407 *
1408 * The following are valid properties on bookmarks, most of which are numbers
1409 * (represented as uint64 in the nvlist), except redact_snaps, which is a
1410 * uint64 array, and redact_complete, which is a boolean
1411 *
1412 * "guid" - globally unique identifier of the snapshot it refers to
1413 * "createtxg" - txg when the snapshot it refers to was created
1414 * "creation" - timestamp when the snapshot it refers to was created
1415 * "ivsetguid" - IVset guid for identifying encrypted snapshots
1416 * "redact_snaps" - list of guids of the redaction snapshots for the specified
1417 * bookmark. If the bookmark is not a redaction bookmark, the nvlist will
1418 * not contain an entry for this value. If it is redacted with respect to
1419 * no snapshots, it will contain value -> NULL uint64 array
1420 * "redact_complete" - boolean value; true if the redaction bookmark is
1421 * complete, false otherwise.
1422 *
1423 * The format of the returned nvlist as follows:
1424 * <short name of bookmark> -> {
1425 * <name of property> -> {
1426 * "value" -> uint64
1427 * }
1428 * ...
1429 * "redact_snaps" -> {
1430 * "value" -> uint64 array
1431 * }
1432 * "redact_complete" -> {
1433 * "value" -> boolean value
1434 * }
1435 * }
1436 */
1437 int
lzc_get_bookmarks(const char * fsname,nvlist_t * props,nvlist_t ** bmarks)1438 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
1439 {
1440 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
1441 }
1442
1443 /*
1444 * Get bookmark properties.
1445 *
1446 * Given a bookmark's full name, retrieve all properties for the bookmark.
1447 *
1448 * The format of the returned property list is as follows:
1449 * {
1450 * <name of property> -> {
1451 * "value" -> uint64
1452 * }
1453 * ...
1454 * "redact_snaps" -> {
1455 * "value" -> uint64 array
1456 * }
1457 */
1458 int
lzc_get_bookmark_props(const char * bookmark,nvlist_t ** props)1459 lzc_get_bookmark_props(const char *bookmark, nvlist_t **props)
1460 {
1461 int error;
1462
1463 nvlist_t *innvl = fnvlist_alloc();
1464 error = lzc_ioctl(ZFS_IOC_GET_BOOKMARK_PROPS, bookmark, innvl, props);
1465 fnvlist_free(innvl);
1466
1467 return (error);
1468 }
1469
1470 /*
1471 * Destroys bookmarks.
1472 *
1473 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
1474 * They must all be in the same pool. Bookmarks are specified as
1475 * <fs>#<bmark>.
1476 *
1477 * Bookmarks that do not exist will be silently ignored.
1478 *
1479 * The return value will be 0 if all bookmarks that existed were destroyed.
1480 *
1481 * Otherwise the return value will be the errno of a (undetermined) bookmark
1482 * that failed, no bookmarks will be destroyed, and the errlist will have an
1483 * entry for each bookmarks that failed. The value in the errlist will be
1484 * the (int32) error code.
1485 */
1486 int
lzc_destroy_bookmarks(nvlist_t * bmarks,nvlist_t ** errlist)1487 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
1488 {
1489 nvpair_t *elem;
1490 int error;
1491 char pool[ZFS_MAX_DATASET_NAME_LEN];
1492
1493 /* determine the pool name */
1494 elem = nvlist_next_nvpair(bmarks, NULL);
1495 if (elem == NULL)
1496 return (0);
1497 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
1498 pool[strcspn(pool, "/#")] = '\0';
1499
1500 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
1501
1502 return (error);
1503 }
1504
1505 static int
lzc_channel_program_impl(const char * pool,const char * program,boolean_t sync,uint64_t instrlimit,uint64_t memlimit,nvlist_t * argnvl,nvlist_t ** outnvl)1506 lzc_channel_program_impl(const char *pool, const char *program, boolean_t sync,
1507 uint64_t instrlimit, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1508 {
1509 int error;
1510 nvlist_t *args;
1511
1512 args = fnvlist_alloc();
1513 fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
1514 fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
1515 fnvlist_add_boolean_value(args, ZCP_ARG_SYNC, sync);
1516 fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
1517 fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
1518 error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
1519 fnvlist_free(args);
1520
1521 return (error);
1522 }
1523
1524 /*
1525 * Executes a channel program.
1526 *
1527 * If this function returns 0 the channel program was successfully loaded and
1528 * ran without failing. Note that individual commands the channel program ran
1529 * may have failed and the channel program is responsible for reporting such
1530 * errors through outnvl if they are important.
1531 *
1532 * This method may also return:
1533 *
1534 * EINVAL The program contains syntax errors, or an invalid memory or time
1535 * limit was given. No part of the channel program was executed.
1536 * If caused by syntax errors, 'outnvl' contains information about the
1537 * errors.
1538 *
1539 * ECHRNG The program was executed, but encountered a runtime error, such as
1540 * calling a function with incorrect arguments, invoking the error()
1541 * function directly, failing an assert() command, etc. Some portion
1542 * of the channel program may have executed and committed changes.
1543 * Information about the failure can be found in 'outnvl'.
1544 *
1545 * ENOMEM The program fully executed, but the output buffer was not large
1546 * enough to store the returned value. No output is returned through
1547 * 'outnvl'.
1548 *
1549 * ENOSPC The program was terminated because it exceeded its memory usage
1550 * limit. Some portion of the channel program may have executed and
1551 * committed changes to disk. No output is returned through 'outnvl'.
1552 *
1553 * ETIME The program was terminated because it exceeded its Lua instruction
1554 * limit. Some portion of the channel program may have executed and
1555 * committed changes to disk. No output is returned through 'outnvl'.
1556 */
1557 int
lzc_channel_program(const char * pool,const char * program,uint64_t instrlimit,uint64_t memlimit,nvlist_t * argnvl,nvlist_t ** outnvl)1558 lzc_channel_program(const char *pool, const char *program, uint64_t instrlimit,
1559 uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1560 {
1561 return (lzc_channel_program_impl(pool, program, B_TRUE, instrlimit,
1562 memlimit, argnvl, outnvl));
1563 }
1564
1565 /*
1566 * Creates a checkpoint for the specified pool.
1567 *
1568 * If this function returns 0 the pool was successfully checkpointed.
1569 *
1570 * This method may also return:
1571 *
1572 * ZFS_ERR_CHECKPOINT_EXISTS
1573 * The pool already has a checkpoint. A pools can only have one
1574 * checkpoint at most, at any given time.
1575 *
1576 * ZFS_ERR_DISCARDING_CHECKPOINT
1577 * ZFS is in the middle of discarding a checkpoint for this pool.
1578 * The pool can be checkpointed again once the discard is done.
1579 *
1580 * ZFS_DEVRM_IN_PROGRESS
1581 * A vdev is currently being removed. The pool cannot be
1582 * checkpointed until the device removal is done.
1583 *
1584 * ZFS_VDEV_TOO_BIG
1585 * One or more top-level vdevs exceed the maximum vdev size
1586 * supported for this feature.
1587 */
1588 int
lzc_pool_checkpoint(const char * pool)1589 lzc_pool_checkpoint(const char *pool)
1590 {
1591 int error;
1592
1593 nvlist_t *result = NULL;
1594 nvlist_t *args = fnvlist_alloc();
1595
1596 error = lzc_ioctl(ZFS_IOC_POOL_CHECKPOINT, pool, args, &result);
1597
1598 fnvlist_free(args);
1599 fnvlist_free(result);
1600
1601 return (error);
1602 }
1603
1604 /*
1605 * Discard the checkpoint from the specified pool.
1606 *
1607 * If this function returns 0 the checkpoint was successfully discarded.
1608 *
1609 * This method may also return:
1610 *
1611 * ZFS_ERR_NO_CHECKPOINT
1612 * The pool does not have a checkpoint.
1613 *
1614 * ZFS_ERR_DISCARDING_CHECKPOINT
1615 * ZFS is already in the middle of discarding the checkpoint.
1616 */
1617 int
lzc_pool_checkpoint_discard(const char * pool)1618 lzc_pool_checkpoint_discard(const char *pool)
1619 {
1620 int error;
1621
1622 nvlist_t *result = NULL;
1623 nvlist_t *args = fnvlist_alloc();
1624
1625 error = lzc_ioctl(ZFS_IOC_POOL_DISCARD_CHECKPOINT, pool, args, &result);
1626
1627 fnvlist_free(args);
1628 fnvlist_free(result);
1629
1630 return (error);
1631 }
1632
1633 /*
1634 * Load the requested data type for the specified pool.
1635 */
1636 int
lzc_pool_prefetch(const char * pool,zpool_prefetch_type_t type)1637 lzc_pool_prefetch(const char *pool, zpool_prefetch_type_t type)
1638 {
1639 int error;
1640 nvlist_t *result = NULL;
1641 nvlist_t *args = fnvlist_alloc();
1642
1643 fnvlist_add_int32(args, ZPOOL_PREFETCH_TYPE, type);
1644
1645 error = lzc_ioctl(ZFS_IOC_POOL_PREFETCH, pool, args, &result);
1646
1647 fnvlist_free(args);
1648 fnvlist_free(result);
1649
1650 return (error);
1651 }
1652
1653 /*
1654 * Executes a read-only channel program.
1655 *
1656 * A read-only channel program works programmatically the same way as a
1657 * normal channel program executed with lzc_channel_program(). The only
1658 * difference is it runs exclusively in open-context and therefore can
1659 * return faster. The downside to that, is that the program cannot change
1660 * on-disk state by calling functions from the zfs.sync submodule.
1661 *
1662 * The return values of this function (and their meaning) are exactly the
1663 * same as the ones described in lzc_channel_program().
1664 */
1665 int
lzc_channel_program_nosync(const char * pool,const char * program,uint64_t timeout,uint64_t memlimit,nvlist_t * argnvl,nvlist_t ** outnvl)1666 lzc_channel_program_nosync(const char *pool, const char *program,
1667 uint64_t timeout, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1668 {
1669 return (lzc_channel_program_impl(pool, program, B_FALSE, timeout,
1670 memlimit, argnvl, outnvl));
1671 }
1672
1673 int
lzc_get_vdev_prop(const char * poolname,nvlist_t * innvl,nvlist_t ** outnvl)1674 lzc_get_vdev_prop(const char *poolname, nvlist_t *innvl, nvlist_t **outnvl)
1675 {
1676 return (lzc_ioctl(ZFS_IOC_VDEV_GET_PROPS, poolname, innvl, outnvl));
1677 }
1678
1679 int
lzc_set_vdev_prop(const char * poolname,nvlist_t * innvl,nvlist_t ** outnvl)1680 lzc_set_vdev_prop(const char *poolname, nvlist_t *innvl, nvlist_t **outnvl)
1681 {
1682 return (lzc_ioctl(ZFS_IOC_VDEV_SET_PROPS, poolname, innvl, outnvl));
1683 }
1684
1685 /*
1686 * Performs key management functions
1687 *
1688 * crypto_cmd should be a value from dcp_cmd_t. If the command specifies to
1689 * load or change a wrapping key, the key should be specified in the
1690 * hidden_args nvlist so that it is not logged.
1691 */
1692 int
lzc_load_key(const char * fsname,boolean_t noop,uint8_t * wkeydata,uint_t wkeylen)1693 lzc_load_key(const char *fsname, boolean_t noop, uint8_t *wkeydata,
1694 uint_t wkeylen)
1695 {
1696 int error;
1697 nvlist_t *ioc_args;
1698 nvlist_t *hidden_args;
1699
1700 if (wkeydata == NULL)
1701 return (EINVAL);
1702
1703 ioc_args = fnvlist_alloc();
1704 hidden_args = fnvlist_alloc();
1705 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata, wkeylen);
1706 fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1707 if (noop)
1708 fnvlist_add_boolean(ioc_args, "noop");
1709 error = lzc_ioctl(ZFS_IOC_LOAD_KEY, fsname, ioc_args, NULL);
1710 nvlist_free(hidden_args);
1711 nvlist_free(ioc_args);
1712
1713 return (error);
1714 }
1715
1716 int
lzc_unload_key(const char * fsname)1717 lzc_unload_key(const char *fsname)
1718 {
1719 return (lzc_ioctl(ZFS_IOC_UNLOAD_KEY, fsname, NULL, NULL));
1720 }
1721
1722 int
lzc_change_key(const char * fsname,uint64_t crypt_cmd,nvlist_t * props,uint8_t * wkeydata,uint_t wkeylen)1723 lzc_change_key(const char *fsname, uint64_t crypt_cmd, nvlist_t *props,
1724 uint8_t *wkeydata, uint_t wkeylen)
1725 {
1726 int error;
1727 nvlist_t *ioc_args = fnvlist_alloc();
1728 nvlist_t *hidden_args = NULL;
1729
1730 fnvlist_add_uint64(ioc_args, "crypt_cmd", crypt_cmd);
1731
1732 if (wkeydata != NULL) {
1733 hidden_args = fnvlist_alloc();
1734 fnvlist_add_uint8_array(hidden_args, "wkeydata", wkeydata,
1735 wkeylen);
1736 fnvlist_add_nvlist(ioc_args, ZPOOL_HIDDEN_ARGS, hidden_args);
1737 }
1738
1739 if (props != NULL)
1740 fnvlist_add_nvlist(ioc_args, "props", props);
1741
1742 error = lzc_ioctl(ZFS_IOC_CHANGE_KEY, fsname, ioc_args, NULL);
1743 nvlist_free(hidden_args);
1744 nvlist_free(ioc_args);
1745
1746 return (error);
1747 }
1748
1749 int
lzc_reopen(const char * pool_name,boolean_t scrub_restart)1750 lzc_reopen(const char *pool_name, boolean_t scrub_restart)
1751 {
1752 nvlist_t *args = fnvlist_alloc();
1753 int error;
1754
1755 fnvlist_add_boolean_value(args, "scrub_restart", scrub_restart);
1756
1757 error = lzc_ioctl(ZFS_IOC_POOL_REOPEN, pool_name, args, NULL);
1758 nvlist_free(args);
1759 return (error);
1760 }
1761
1762 /*
1763 * Changes initializing state.
1764 *
1765 * vdevs should be a list of (<key>, guid) where guid is a uint64 vdev GUID.
1766 * The key is ignored.
1767 *
1768 * If there are errors related to vdev arguments, per-vdev errors are returned
1769 * in an nvlist with the key "vdevs". Each error is a (guid, errno) pair where
1770 * guid is stringified with PRIu64, and errno is one of the following as
1771 * an int64_t:
1772 * - ENODEV if the device was not found
1773 * - EINVAL if the devices is not a leaf or is not concrete (e.g. missing)
1774 * - EROFS if the device is not writeable
1775 * - EBUSY start requested but the device is already being either
1776 * initialized or trimmed
1777 * - ESRCH cancel/suspend requested but device is not being initialized
1778 *
1779 * If the errlist is empty, then return value will be:
1780 * - EINVAL if one or more arguments was invalid
1781 * - Other spa_open failures
1782 * - 0 if the operation succeeded
1783 */
1784 int
lzc_initialize(const char * poolname,pool_initialize_func_t cmd_type,nvlist_t * vdevs,nvlist_t ** errlist)1785 lzc_initialize(const char *poolname, pool_initialize_func_t cmd_type,
1786 nvlist_t *vdevs, nvlist_t **errlist)
1787 {
1788 int error;
1789
1790 nvlist_t *args = fnvlist_alloc();
1791 fnvlist_add_uint64(args, ZPOOL_INITIALIZE_COMMAND, (uint64_t)cmd_type);
1792 fnvlist_add_nvlist(args, ZPOOL_INITIALIZE_VDEVS, vdevs);
1793
1794 error = lzc_ioctl(ZFS_IOC_POOL_INITIALIZE, poolname, args, errlist);
1795
1796 fnvlist_free(args);
1797
1798 return (error);
1799 }
1800
1801 /*
1802 * Changes TRIM state.
1803 *
1804 * vdevs should be a list of (<key>, guid) where guid is a uint64 vdev GUID.
1805 * The key is ignored.
1806 *
1807 * If there are errors related to vdev arguments, per-vdev errors are returned
1808 * in an nvlist with the key "vdevs". Each error is a (guid, errno) pair where
1809 * guid is stringified with PRIu64, and errno is one of the following as
1810 * an int64_t:
1811 * - ENODEV if the device was not found
1812 * - EINVAL if the devices is not a leaf or is not concrete (e.g. missing)
1813 * - EROFS if the device is not writeable
1814 * - EBUSY start requested but the device is already being either trimmed
1815 * or initialized
1816 * - ESRCH cancel/suspend requested but device is not being initialized
1817 * - EOPNOTSUPP if the device does not support TRIM (or secure TRIM)
1818 *
1819 * If the errlist is empty, then return value will be:
1820 * - EINVAL if one or more arguments was invalid
1821 * - Other spa_open failures
1822 * - 0 if the operation succeeded
1823 */
1824 int
lzc_trim(const char * poolname,pool_trim_func_t cmd_type,uint64_t rate,boolean_t secure,nvlist_t * vdevs,nvlist_t ** errlist)1825 lzc_trim(const char *poolname, pool_trim_func_t cmd_type, uint64_t rate,
1826 boolean_t secure, nvlist_t *vdevs, nvlist_t **errlist)
1827 {
1828 int error;
1829
1830 nvlist_t *args = fnvlist_alloc();
1831 fnvlist_add_uint64(args, ZPOOL_TRIM_COMMAND, (uint64_t)cmd_type);
1832 fnvlist_add_nvlist(args, ZPOOL_TRIM_VDEVS, vdevs);
1833 fnvlist_add_uint64(args, ZPOOL_TRIM_RATE, rate);
1834 fnvlist_add_boolean_value(args, ZPOOL_TRIM_SECURE, secure);
1835
1836 error = lzc_ioctl(ZFS_IOC_POOL_TRIM, poolname, args, errlist);
1837
1838 fnvlist_free(args);
1839
1840 return (error);
1841 }
1842
1843 /*
1844 * Create a redaction bookmark named bookname by redacting snapshot with respect
1845 * to all the snapshots in snapnv.
1846 */
1847 int
lzc_redact(const char * snapshot,const char * bookname,nvlist_t * snapnv)1848 lzc_redact(const char *snapshot, const char *bookname, nvlist_t *snapnv)
1849 {
1850 nvlist_t *args = fnvlist_alloc();
1851 fnvlist_add_string(args, "bookname", bookname);
1852 fnvlist_add_nvlist(args, "snapnv", snapnv);
1853 int error = lzc_ioctl(ZFS_IOC_REDACT, snapshot, args, NULL);
1854 fnvlist_free(args);
1855 return (error);
1856 }
1857
1858 static int
wait_common(const char * pool,zpool_wait_activity_t activity,boolean_t use_tag,uint64_t tag,boolean_t * waited)1859 wait_common(const char *pool, zpool_wait_activity_t activity, boolean_t use_tag,
1860 uint64_t tag, boolean_t *waited)
1861 {
1862 nvlist_t *args = fnvlist_alloc();
1863 nvlist_t *result = NULL;
1864
1865 fnvlist_add_int32(args, ZPOOL_WAIT_ACTIVITY, activity);
1866 if (use_tag)
1867 fnvlist_add_uint64(args, ZPOOL_WAIT_TAG, tag);
1868
1869 int error = lzc_ioctl(ZFS_IOC_WAIT, pool, args, &result);
1870
1871 if (error == 0 && waited != NULL)
1872 *waited = fnvlist_lookup_boolean_value(result,
1873 ZPOOL_WAIT_WAITED);
1874
1875 fnvlist_free(args);
1876 fnvlist_free(result);
1877
1878 return (error);
1879 }
1880
1881 int
lzc_wait(const char * pool,zpool_wait_activity_t activity,boolean_t * waited)1882 lzc_wait(const char *pool, zpool_wait_activity_t activity, boolean_t *waited)
1883 {
1884 return (wait_common(pool, activity, B_FALSE, 0, waited));
1885 }
1886
1887 int
lzc_wait_tag(const char * pool,zpool_wait_activity_t activity,uint64_t tag,boolean_t * waited)1888 lzc_wait_tag(const char *pool, zpool_wait_activity_t activity, uint64_t tag,
1889 boolean_t *waited)
1890 {
1891 return (wait_common(pool, activity, B_TRUE, tag, waited));
1892 }
1893
1894 int
lzc_wait_fs(const char * fs,zfs_wait_activity_t activity,boolean_t * waited)1895 lzc_wait_fs(const char *fs, zfs_wait_activity_t activity, boolean_t *waited)
1896 {
1897 nvlist_t *args = fnvlist_alloc();
1898 nvlist_t *result = NULL;
1899
1900 fnvlist_add_int32(args, ZFS_WAIT_ACTIVITY, activity);
1901
1902 int error = lzc_ioctl(ZFS_IOC_WAIT_FS, fs, args, &result);
1903
1904 if (error == 0 && waited != NULL)
1905 *waited = fnvlist_lookup_boolean_value(result,
1906 ZFS_WAIT_WAITED);
1907
1908 fnvlist_free(args);
1909 fnvlist_free(result);
1910
1911 return (error);
1912 }
1913
1914 /*
1915 * Set the bootenv contents for the given pool.
1916 */
1917 int
lzc_set_bootenv(const char * pool,const nvlist_t * env)1918 lzc_set_bootenv(const char *pool, const nvlist_t *env)
1919 {
1920 return (lzc_ioctl(ZFS_IOC_SET_BOOTENV, pool, (nvlist_t *)env, NULL));
1921 }
1922
1923 /*
1924 * Get the contents of the bootenv of the given pool.
1925 */
1926 int
lzc_get_bootenv(const char * pool,nvlist_t ** outnvl)1927 lzc_get_bootenv(const char *pool, nvlist_t **outnvl)
1928 {
1929 return (lzc_ioctl(ZFS_IOC_GET_BOOTENV, pool, NULL, outnvl));
1930 }
1931
1932 /*
1933 * Prune the specified amount from the pool's dedup table.
1934 */
1935 int
lzc_ddt_prune(const char * pool,zpool_ddt_prune_unit_t unit,uint64_t amount)1936 lzc_ddt_prune(const char *pool, zpool_ddt_prune_unit_t unit, uint64_t amount)
1937 {
1938 int error;
1939
1940 nvlist_t *result = NULL;
1941 nvlist_t *args = fnvlist_alloc();
1942
1943 fnvlist_add_int32(args, DDT_PRUNE_UNIT, unit);
1944 fnvlist_add_uint64(args, DDT_PRUNE_AMOUNT, amount);
1945
1946 error = lzc_ioctl(ZFS_IOC_DDT_PRUNE, pool, args, &result);
1947
1948 fnvlist_free(args);
1949 fnvlist_free(result);
1950
1951 return (error);
1952 }
1953