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