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.
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.
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]
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 * Copyright (c) 2017 Datto Inc.
26 * Copyright 2017 RackTop Systems.
27 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
31 * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
32 * It has the following characteristics:
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).
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.
50 * - Programmatic Error Handling. libzfs_core communicates errors with
51 * defined error numbers, and doesn't print anything to stdout/stderr.
53 * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
54 * to/from the kernel ioctls. There is generally a 1:1 correspondence
55 * between libzfs_core functions and ioctls to /dev/zfs.
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.
64 * - Continued libzfs Support. Some higher-level operations (e.g.
65 * support for "zfs send -R") are too complicated to fit the scope of
66 * libzfs_core. This functionality will continue to live in libzfs.
67 * Where appropriate, libzfs will use the underlying atomic operations
68 * of libzfs_core. For example, libzfs may implement "zfs send -R |
69 * zfs receive" by using individual "send one snapshot", rename,
70 * destroy, and "receive one snapshot" operations in libzfs_core.
71 * /sbin/zfs and /zbin/zpool will link with both libzfs and
72 * libzfs_core. Other consumers should aim to use only libzfs_core,
73 * since that will be the supported, stable interface going forwards.
76 #include <libzfs_core.h>
84 #include <sys/nvpair.h>
85 #include <sys/param.h>
86 #include <sys/types.h>
88 #include <sys/zfs_ioctl.h>
91 static pthread_mutex_t g_lock
= PTHREAD_MUTEX_INITIALIZER
;
92 static int g_refcount
;
95 libzfs_core_init(void)
97 (void) pthread_mutex_lock(&g_lock
);
98 if (g_refcount
== 0) {
99 g_fd
= open("/dev/zfs", O_RDWR
);
101 (void) pthread_mutex_unlock(&g_lock
);
106 (void) pthread_mutex_unlock(&g_lock
);
111 libzfs_core_fini(void)
113 (void) pthread_mutex_lock(&g_lock
);
114 ASSERT3S(g_refcount
, >, 0);
119 if (g_refcount
== 0 && g_fd
!= -1) {
123 (void) pthread_mutex_unlock(&g_lock
);
127 lzc_ioctl(zfs_ioc_t ioc
, const char *name
,
128 nvlist_t
*source
, nvlist_t
**resultp
)
130 zfs_cmd_t zc
= {"\0"};
135 ASSERT3S(g_refcount
, >, 0);
136 VERIFY3S(g_fd
, !=, -1);
139 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
141 if (source
!= NULL
) {
142 packed
= fnvlist_pack(source
, &size
);
143 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
144 zc
.zc_nvlist_src_size
= size
;
147 if (resultp
!= NULL
) {
149 zc
.zc_nvlist_dst_size
= MAX(size
* 2, 128 * 1024);
150 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
151 malloc(zc
.zc_nvlist_dst_size
);
152 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
158 while (ioctl(g_fd
, ioc
, &zc
) != 0) {
159 if (errno
== ENOMEM
&& resultp
!= NULL
) {
160 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
161 zc
.zc_nvlist_dst_size
*= 2;
162 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
163 malloc(zc
.zc_nvlist_dst_size
);
164 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
173 if (zc
.zc_nvlist_dst_filled
) {
174 *resultp
= fnvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
175 zc
.zc_nvlist_dst_size
);
180 fnvlist_pack_free(packed
, size
);
181 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
186 lzc_create(const char *fsname
, enum lzc_dataset_type type
, nvlist_t
*props
,
187 uint8_t *wkeydata
, uint_t wkeylen
)
190 nvlist_t
*hidden_args
= NULL
;
191 nvlist_t
*args
= fnvlist_alloc();
193 fnvlist_add_int32(args
, "type", (dmu_objset_type_t
)type
);
195 fnvlist_add_nvlist(args
, "props", props
);
197 if (wkeydata
!= NULL
) {
198 hidden_args
= fnvlist_alloc();
199 fnvlist_add_uint8_array(hidden_args
, "wkeydata", wkeydata
,
201 fnvlist_add_nvlist(args
, ZPOOL_HIDDEN_ARGS
, hidden_args
);
204 error
= lzc_ioctl(ZFS_IOC_CREATE
, fsname
, args
, NULL
);
205 nvlist_free(hidden_args
);
211 lzc_clone(const char *fsname
, const char *origin
, nvlist_t
*props
)
214 nvlist_t
*hidden_args
= NULL
;
215 nvlist_t
*args
= fnvlist_alloc();
217 fnvlist_add_string(args
, "origin", origin
);
219 fnvlist_add_nvlist(args
, "props", props
);
220 error
= lzc_ioctl(ZFS_IOC_CLONE
, fsname
, args
, NULL
);
221 nvlist_free(hidden_args
);
227 lzc_promote(const char *fsname
, char *snapnamebuf
, int snapnamelen
)
230 * The promote ioctl is still legacy, so we need to construct our
231 * own zfs_cmd_t rather than using lzc_ioctl().
233 zfs_cmd_t zc
= { "\0" };
235 ASSERT3S(g_refcount
, >, 0);
236 VERIFY3S(g_fd
, !=, -1);
238 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_name
));
239 if (ioctl(g_fd
, ZFS_IOC_PROMOTE
, &zc
) != 0) {
241 if (error
== EEXIST
&& snapnamebuf
!= NULL
)
242 (void) strlcpy(snapnamebuf
, zc
.zc_string
, snapnamelen
);
251 * The keys in the snaps nvlist are the snapshots to be created.
252 * They must all be in the same pool.
254 * The props nvlist is properties to set. Currently only user properties
255 * are supported. { user:prop_name -> string value }
257 * The returned results nvlist will have an entry for each snapshot that failed.
258 * The value will be the (int32) error code.
260 * The return value will be 0 if all snapshots were created, otherwise it will
261 * be the errno of a (unspecified) snapshot that failed.
264 lzc_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
**errlist
)
269 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
273 /* determine the pool name */
274 elem
= nvlist_next_nvpair(snaps
, NULL
);
277 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
278 pool
[strcspn(pool
, "/@")] = '\0';
280 args
= fnvlist_alloc();
281 fnvlist_add_nvlist(args
, "snaps", snaps
);
283 fnvlist_add_nvlist(args
, "props", props
);
285 error
= lzc_ioctl(ZFS_IOC_SNAPSHOT
, pool
, args
, errlist
);
292 * Destroys snapshots.
294 * The keys in the snaps nvlist are the snapshots to be destroyed.
295 * They must all be in the same pool.
297 * Snapshots that do not exist will be silently ignored.
299 * If 'defer' is not set, and a snapshot has user holds or clones, the
300 * destroy operation will fail and none of the snapshots will be
303 * If 'defer' is set, and a snapshot has user holds or clones, it will be
304 * marked for deferred destruction, and will be destroyed when the last hold
305 * or clone is removed/destroyed.
307 * The return value will be 0 if all snapshots were destroyed (or marked for
308 * later destruction if 'defer' is set) or didn't exist to begin with.
310 * Otherwise the return value will be the errno of a (unspecified) snapshot
311 * that failed, no snapshots will be destroyed, and the errlist will have an
312 * entry for each snapshot that failed. The value in the errlist will be
313 * the (int32) error code.
316 lzc_destroy_snaps(nvlist_t
*snaps
, boolean_t defer
, nvlist_t
**errlist
)
321 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
323 /* determine the pool name */
324 elem
= nvlist_next_nvpair(snaps
, NULL
);
327 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
328 pool
[strcspn(pool
, "/@")] = '\0';
330 args
= fnvlist_alloc();
331 fnvlist_add_nvlist(args
, "snaps", snaps
);
333 fnvlist_add_boolean(args
, "defer");
335 error
= lzc_ioctl(ZFS_IOC_DESTROY_SNAPS
, pool
, args
, errlist
);
342 lzc_snaprange_space(const char *firstsnap
, const char *lastsnap
,
348 char fs
[ZFS_MAX_DATASET_NAME_LEN
];
351 /* determine the fs name */
352 (void) strlcpy(fs
, firstsnap
, sizeof (fs
));
353 atp
= strchr(fs
, '@');
358 args
= fnvlist_alloc();
359 fnvlist_add_string(args
, "firstsnap", firstsnap
);
361 err
= lzc_ioctl(ZFS_IOC_SPACE_SNAPS
, lastsnap
, args
, &result
);
364 *usedp
= fnvlist_lookup_uint64(result
, "used");
365 fnvlist_free(result
);
371 lzc_exists(const char *dataset
)
374 * The objset_stats ioctl is still legacy, so we need to construct our
375 * own zfs_cmd_t rather than using lzc_ioctl().
377 zfs_cmd_t zc
= {"\0"};
379 ASSERT3S(g_refcount
, >, 0);
380 VERIFY3S(g_fd
, !=, -1);
382 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
383 return (ioctl(g_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) == 0);
388 * It was added to preserve the function signature in case it is
389 * needed in the future.
393 lzc_sync(const char *pool_name
, nvlist_t
*innvl
, nvlist_t
**outnvl
)
395 return (lzc_ioctl(ZFS_IOC_POOL_SYNC
, pool_name
, innvl
, NULL
));
399 * Create "user holds" on snapshots. If there is a hold on a snapshot,
400 * the snapshot can not be destroyed. (However, it can be marked for deletion
401 * by lzc_destroy_snaps(defer=B_TRUE).)
403 * The keys in the nvlist are snapshot names.
404 * The snapshots must all be in the same pool.
405 * The value is the name of the hold (string type).
407 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
408 * In this case, when the cleanup_fd is closed (including on process
409 * termination), the holds will be released. If the system is shut down
410 * uncleanly, the holds will be released when the pool is next opened
413 * Holds for snapshots which don't exist will be skipped and have an entry
414 * added to errlist, but will not cause an overall failure.
416 * The return value will be 0 if all holds, for snapshots that existed,
417 * were successfully created.
419 * Otherwise the return value will be the errno of a (unspecified) hold that
420 * failed and no holds will be created.
422 * In all cases the errlist will have an entry for each hold that failed
423 * (name = snapshot), with its value being the error code (int32).
426 lzc_hold(nvlist_t
*holds
, int cleanup_fd
, nvlist_t
**errlist
)
428 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
433 /* determine the pool name */
434 elem
= nvlist_next_nvpair(holds
, NULL
);
437 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
438 pool
[strcspn(pool
, "/@")] = '\0';
440 args
= fnvlist_alloc();
441 fnvlist_add_nvlist(args
, "holds", holds
);
442 if (cleanup_fd
!= -1)
443 fnvlist_add_int32(args
, "cleanup_fd", cleanup_fd
);
445 error
= lzc_ioctl(ZFS_IOC_HOLD
, pool
, args
, errlist
);
451 * Release "user holds" on snapshots. If the snapshot has been marked for
452 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
453 * any clones, and all the user holds are removed, then the snapshot will be
456 * The keys in the nvlist are snapshot names.
457 * The snapshots must all be in the same pool.
458 * The value is an nvlist whose keys are the holds to remove.
460 * Holds which failed to release because they didn't exist will have an entry
461 * added to errlist, but will not cause an overall failure.
463 * The return value will be 0 if the nvl holds was empty or all holds that
464 * existed, were successfully removed.
466 * Otherwise the return value will be the errno of a (unspecified) hold that
467 * failed to release and no holds will be released.
469 * In all cases the errlist will have an entry for each hold that failed to
473 lzc_release(nvlist_t
*holds
, nvlist_t
**errlist
)
475 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
478 /* determine the pool name */
479 elem
= nvlist_next_nvpair(holds
, NULL
);
482 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
483 pool
[strcspn(pool
, "/@")] = '\0';
485 return (lzc_ioctl(ZFS_IOC_RELEASE
, pool
, holds
, errlist
));
489 * Retrieve list of user holds on the specified snapshot.
491 * On success, *holdsp will be set to an nvlist which the caller must free.
492 * The keys are the names of the holds, and the value is the creation time
493 * of the hold (uint64) in seconds since the epoch.
496 lzc_get_holds(const char *snapname
, nvlist_t
**holdsp
)
498 return (lzc_ioctl(ZFS_IOC_GET_HOLDS
, snapname
, NULL
, holdsp
));
502 * Generate a zfs send stream for the specified snapshot and write it to
503 * the specified file descriptor.
505 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
507 * If "from" is NULL, a full (non-incremental) stream will be sent.
508 * If "from" is non-NULL, it must be the full name of a snapshot or
509 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
510 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
511 * bookmark must represent an earlier point in the history of "snapname").
512 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
513 * or it can be the origin of "snapname"'s filesystem, or an earlier
514 * snapshot in the origin, etc.
516 * "fd" is the file descriptor to write the send stream to.
518 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
519 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
520 * records with drr_blksz > 128K.
522 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
523 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
524 * which the receiving system must support (as indicated by support
525 * for the "embedded_data" feature).
528 lzc_send(const char *snapname
, const char *from
, int fd
,
529 enum lzc_send_flags flags
)
531 return (lzc_send_resume(snapname
, from
, fd
, flags
, 0, 0));
535 lzc_send_resume(const char *snapname
, const char *from
, int fd
,
536 enum lzc_send_flags flags
, uint64_t resumeobj
, uint64_t resumeoff
)
541 args
= fnvlist_alloc();
542 fnvlist_add_int32(args
, "fd", fd
);
544 fnvlist_add_string(args
, "fromsnap", from
);
545 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
546 fnvlist_add_boolean(args
, "largeblockok");
547 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
548 fnvlist_add_boolean(args
, "embedok");
549 if (flags
& LZC_SEND_FLAG_COMPRESS
)
550 fnvlist_add_boolean(args
, "compressok");
551 if (flags
& LZC_SEND_FLAG_RAW
)
552 fnvlist_add_boolean(args
, "rawok");
553 if (resumeobj
!= 0 || resumeoff
!= 0) {
554 fnvlist_add_uint64(args
, "resume_object", resumeobj
);
555 fnvlist_add_uint64(args
, "resume_offset", resumeoff
);
557 err
= lzc_ioctl(ZFS_IOC_SEND_NEW
, snapname
, args
, NULL
);
563 * "from" can be NULL, a snapshot, or a bookmark.
565 * If from is NULL, a full (non-incremental) stream will be estimated. This
566 * is calculated very efficiently.
568 * If from is a snapshot, lzc_send_space uses the deadlists attached to
569 * each snapshot to efficiently estimate the stream size.
571 * If from is a bookmark, the indirect blocks in the destination snapshot
572 * are traversed, looking for blocks with a birth time since the creation TXG of
573 * the snapshot this bookmark was created from. This will result in
574 * significantly more I/O and be less efficient than a send space estimation on
575 * an equivalent snapshot.
578 lzc_send_space(const char *snapname
, const char *from
,
579 enum lzc_send_flags flags
, uint64_t *spacep
)
585 args
= fnvlist_alloc();
587 fnvlist_add_string(args
, "from", from
);
588 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
589 fnvlist_add_boolean(args
, "largeblockok");
590 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
591 fnvlist_add_boolean(args
, "embedok");
592 if (flags
& LZC_SEND_FLAG_COMPRESS
)
593 fnvlist_add_boolean(args
, "compressok");
594 if (flags
& LZC_SEND_FLAG_RAW
)
595 fnvlist_add_boolean(args
, "rawok");
596 err
= lzc_ioctl(ZFS_IOC_SEND_SPACE
, snapname
, args
, &result
);
599 *spacep
= fnvlist_lookup_uint64(result
, "space");
605 recv_read(int fd
, void *buf
, int ilen
)
612 rv
= read(fd
, cp
, len
);
617 if (rv
< 0 || len
!= 0)
624 * Linux adds ZFS_IOC_RECV_NEW for resumable and raw streams and preserves the
625 * legacy ZFS_IOC_RECV user/kernel interface. The new interface supports all
626 * stream options but is currently only used for resumable streams. This way
627 * updated user space utilities will interoperate with older kernel modules.
629 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
632 recv_impl(const char *snapname
, nvlist_t
*recvdprops
, nvlist_t
*localprops
,
633 const char *origin
, boolean_t force
, boolean_t resumable
, boolean_t raw
,
634 int input_fd
, const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
635 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
638 dmu_replay_record_t drr
;
639 char fsname
[MAXPATHLEN
];
643 ASSERT3S(g_refcount
, >, 0);
644 VERIFY3S(g_fd
, !=, -1);
646 /* Set 'fsname' to the name of containing filesystem */
647 (void) strlcpy(fsname
, snapname
, sizeof (fsname
));
648 atp
= strchr(fsname
, '@');
653 /* If the fs does not exist, try its parent. */
654 if (!lzc_exists(fsname
)) {
655 char *slashp
= strrchr(fsname
, '/');
662 * The begin_record is normally a non-byteswapped BEGIN record.
663 * For resumable streams it may be set to any non-byteswapped
664 * dmu_replay_record_t.
666 if (begin_record
== NULL
) {
667 error
= recv_read(input_fd
, &drr
, sizeof (drr
));
674 if (resumable
|| raw
) {
675 nvlist_t
*outnvl
= NULL
;
676 nvlist_t
*innvl
= fnvlist_alloc();
678 fnvlist_add_string(innvl
, "snapname", snapname
);
680 if (recvdprops
!= NULL
)
681 fnvlist_add_nvlist(innvl
, "props", recvdprops
);
683 if (localprops
!= NULL
)
684 fnvlist_add_nvlist(innvl
, "localprops", localprops
);
686 if (origin
!= NULL
&& strlen(origin
))
687 fnvlist_add_string(innvl
, "origin", origin
);
689 fnvlist_add_byte_array(innvl
, "begin_record",
690 (uchar_t
*)&drr
, sizeof (drr
));
692 fnvlist_add_int32(innvl
, "input_fd", input_fd
);
695 fnvlist_add_boolean(innvl
, "force");
698 fnvlist_add_boolean(innvl
, "resumable");
701 fnvlist_add_int32(innvl
, "cleanup_fd", cleanup_fd
);
703 if (action_handle
!= NULL
)
704 fnvlist_add_uint64(innvl
, "action_handle",
707 error
= lzc_ioctl(ZFS_IOC_RECV_NEW
, fsname
, innvl
, &outnvl
);
709 if (error
== 0 && read_bytes
!= NULL
)
710 error
= nvlist_lookup_uint64(outnvl
, "read_bytes",
713 if (error
== 0 && errflags
!= NULL
)
714 error
= nvlist_lookup_uint64(outnvl
, "error_flags",
717 if (error
== 0 && action_handle
!= NULL
)
718 error
= nvlist_lookup_uint64(outnvl
, "action_handle",
721 if (error
== 0 && errors
!= NULL
) {
723 error
= nvlist_lookup_nvlist(outnvl
, "errors", &nvl
);
725 *errors
= fnvlist_dup(nvl
);
729 fnvlist_free(outnvl
);
731 zfs_cmd_t zc
= {"\0"};
735 ASSERT3S(g_refcount
, >, 0);
737 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_value
));
738 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
740 if (recvdprops
!= NULL
) {
741 packed
= fnvlist_pack(recvdprops
, &size
);
742 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
743 zc
.zc_nvlist_src_size
= size
;
746 if (localprops
!= NULL
) {
747 packed
= fnvlist_pack(localprops
, &size
);
748 zc
.zc_nvlist_conf
= (uint64_t)(uintptr_t)packed
;
749 zc
.zc_nvlist_conf_size
= size
;
753 (void) strlcpy(zc
.zc_string
, origin
,
754 sizeof (zc
.zc_string
));
756 ASSERT3S(drr
.drr_type
, ==, DRR_BEGIN
);
757 zc
.zc_begin_record
= drr
.drr_u
.drr_begin
;
759 zc
.zc_cookie
= input_fd
;
760 zc
.zc_cleanup_fd
= -1;
761 zc
.zc_action_handle
= 0;
764 zc
.zc_cleanup_fd
= cleanup_fd
;
766 if (action_handle
!= NULL
)
767 zc
.zc_action_handle
= *action_handle
;
769 zc
.zc_nvlist_dst_size
= 128 * 1024;
770 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
771 malloc(zc
.zc_nvlist_dst_size
);
773 error
= ioctl(g_fd
, ZFS_IOC_RECV
, &zc
);
777 if (read_bytes
!= NULL
)
778 *read_bytes
= zc
.zc_cookie
;
780 if (errflags
!= NULL
)
781 *errflags
= zc
.zc_obj
;
783 if (action_handle
!= NULL
)
784 *action_handle
= zc
.zc_action_handle
;
787 VERIFY0(nvlist_unpack(
788 (void *)(uintptr_t)zc
.zc_nvlist_dst
,
789 zc
.zc_nvlist_dst_size
, errors
, KM_SLEEP
));
793 fnvlist_pack_free(packed
, size
);
794 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
801 * The simplest receive case: receive from the specified fd, creating the
802 * specified snapshot. Apply the specified properties as "received" properties
803 * (which can be overridden by locally-set properties). If the stream is a
804 * clone, its origin snapshot must be specified by 'origin'. The 'force'
805 * flag will cause the target filesystem to be rolled back or destroyed if
806 * necessary to receive.
808 * Return 0 on success or an errno on failure.
810 * Note: this interface does not work on dedup'd streams
811 * (those with DMU_BACKUP_FEATURE_DEDUP).
814 lzc_receive(const char *snapname
, nvlist_t
*props
, const char *origin
,
815 boolean_t force
, boolean_t raw
, int fd
)
817 return (recv_impl(snapname
, props
, NULL
, origin
, force
, B_FALSE
, raw
,
818 fd
, NULL
, -1, NULL
, NULL
, NULL
, NULL
));
822 * Like lzc_receive, but if the receive fails due to premature stream
823 * termination, the intermediate state will be preserved on disk. In this
824 * case, ECKSUM will be returned. The receive may subsequently be resumed
825 * with a resuming send stream generated by lzc_send_resume().
828 lzc_receive_resumable(const char *snapname
, nvlist_t
*props
, const char *origin
,
829 boolean_t force
, boolean_t raw
, int fd
)
831 return (recv_impl(snapname
, props
, NULL
, origin
, force
, B_TRUE
, raw
,
832 fd
, NULL
, -1, NULL
, NULL
, NULL
, NULL
));
836 * Like lzc_receive, but allows the caller to read the begin record and then to
837 * pass it in. That could be useful if the caller wants to derive, for example,
838 * the snapname or the origin parameters based on the information contained in
840 * The begin record must be in its original form as read from the stream,
841 * in other words, it should not be byteswapped.
843 * The 'resumable' parameter allows to obtain the same behavior as with
844 * lzc_receive_resumable.
847 lzc_receive_with_header(const char *snapname
, nvlist_t
*props
,
848 const char *origin
, boolean_t force
, boolean_t resumable
, boolean_t raw
,
849 int fd
, const dmu_replay_record_t
*begin_record
)
851 if (begin_record
== NULL
)
854 return (recv_impl(snapname
, props
, NULL
, origin
, force
, resumable
, raw
,
855 fd
, begin_record
, -1, NULL
, NULL
, NULL
, NULL
));
859 * Like lzc_receive, but allows the caller to pass all supported arguments
860 * and retrieve all values returned. The only additional input parameter
861 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
863 * The following parameters all provide return values. Several may be set
864 * in the failure case and will contain additional information.
866 * The 'read_bytes' value will be set to the total number of bytes read.
868 * The 'errflags' value will contain zprop_errflags_t flags which are
869 * used to describe any failures.
871 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
872 * It should be set to zero on first call and will contain an updated handle
873 * on success, it should be passed in subsequent calls.
875 * The 'errors' nvlist contains an entry for each unapplied received
876 * property. Callers are responsible for freeing this nvlist.
878 int lzc_receive_one(const char *snapname
, nvlist_t
*props
,
879 const char *origin
, boolean_t force
, boolean_t resumable
, boolean_t raw
,
880 int input_fd
, const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
881 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
884 return (recv_impl(snapname
, props
, NULL
, origin
, force
, resumable
,
885 raw
, input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
886 action_handle
, errors
));
890 * Like lzc_receive_one, but allows the caller to pass an additional 'cmdprops'
893 * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
894 * exclude ('zfs receive -x') properties. Callers are responsible for freeing
897 int lzc_receive_with_cmdprops(const char *snapname
, nvlist_t
*props
,
898 nvlist_t
*cmdprops
, const char *origin
, boolean_t force
,
899 boolean_t resumable
, boolean_t raw
, int input_fd
,
900 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
901 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
904 return (recv_impl(snapname
, props
, cmdprops
, origin
, force
, resumable
,
905 raw
, input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
906 action_handle
, errors
));
910 * Roll back this filesystem or volume to its most recent snapshot.
911 * If snapnamebuf is not NULL, it will be filled in with the name
912 * of the most recent snapshot.
913 * Note that the latest snapshot may change if a new one is concurrently
914 * created or the current one is destroyed. lzc_rollback_to can be used
915 * to roll back to a specific latest snapshot.
917 * Return 0 on success or an errno on failure.
920 lzc_rollback(const char *fsname
, char *snapnamebuf
, int snapnamelen
)
926 args
= fnvlist_alloc();
927 err
= lzc_ioctl(ZFS_IOC_ROLLBACK
, fsname
, args
, &result
);
929 if (err
== 0 && snapnamebuf
!= NULL
) {
930 const char *snapname
= fnvlist_lookup_string(result
, "target");
931 (void) strlcpy(snapnamebuf
, snapname
, snapnamelen
);
939 * Roll back this filesystem or volume to the specified snapshot,
942 * Return 0 on success or an errno on failure.
945 lzc_rollback_to(const char *fsname
, const char *snapname
)
951 args
= fnvlist_alloc();
952 fnvlist_add_string(args
, "target", snapname
);
953 err
= lzc_ioctl(ZFS_IOC_ROLLBACK
, fsname
, args
, &result
);
962 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
963 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
964 * snapshots must be in the same pool.
966 * The returned results nvlist will have an entry for each bookmark that failed.
967 * The value will be the (int32) error code.
969 * The return value will be 0 if all bookmarks were created, otherwise it will
970 * be the errno of a (undetermined) bookmarks that failed.
973 lzc_bookmark(nvlist_t
*bookmarks
, nvlist_t
**errlist
)
977 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
979 /* determine the pool name */
980 elem
= nvlist_next_nvpair(bookmarks
, NULL
);
983 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
984 pool
[strcspn(pool
, "/#")] = '\0';
986 error
= lzc_ioctl(ZFS_IOC_BOOKMARK
, pool
, bookmarks
, errlist
);
992 * Retrieve bookmarks.
994 * Retrieve the list of bookmarks for the given file system. The props
995 * parameter is an nvlist of property names (with no values) that will be
996 * returned for each bookmark.
998 * The following are valid properties on bookmarks, all of which are numbers
999 * (represented as uint64 in the nvlist)
1001 * "guid" - globally unique identifier of the snapshot it refers to
1002 * "createtxg" - txg when the snapshot it refers to was created
1003 * "creation" - timestamp when the snapshot it refers to was created
1005 * The format of the returned nvlist as follows:
1006 * <short name of bookmark> -> {
1007 * <name of property> -> {
1013 lzc_get_bookmarks(const char *fsname
, nvlist_t
*props
, nvlist_t
**bmarks
)
1015 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS
, fsname
, props
, bmarks
));
1019 * Destroys bookmarks.
1021 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
1022 * They must all be in the same pool. Bookmarks are specified as
1025 * Bookmarks that do not exist will be silently ignored.
1027 * The return value will be 0 if all bookmarks that existed were destroyed.
1029 * Otherwise the return value will be the errno of a (undetermined) bookmark
1030 * that failed, no bookmarks will be destroyed, and the errlist will have an
1031 * entry for each bookmarks that failed. The value in the errlist will be
1032 * the (int32) error code.
1035 lzc_destroy_bookmarks(nvlist_t
*bmarks
, nvlist_t
**errlist
)
1039 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
1041 /* determine the pool name */
1042 elem
= nvlist_next_nvpair(bmarks
, NULL
);
1045 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
1046 pool
[strcspn(pool
, "/#")] = '\0';
1048 error
= lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS
, pool
, bmarks
, errlist
);
1054 * Performs key management functions
1056 * crypto_cmd should be a value from zfs_ioc_crypto_cmd_t. If the command
1057 * specifies to load or change a wrapping key, the key should be specified in
1058 * the hidden_args nvlist so that it is not logged
1061 lzc_load_key(const char *fsname
, boolean_t noop
, uint8_t *wkeydata
,
1066 nvlist_t
*hidden_args
;
1068 if (wkeydata
== NULL
)
1071 ioc_args
= fnvlist_alloc();
1072 hidden_args
= fnvlist_alloc();
1073 fnvlist_add_uint8_array(hidden_args
, "wkeydata", wkeydata
, wkeylen
);
1074 fnvlist_add_nvlist(ioc_args
, ZPOOL_HIDDEN_ARGS
, hidden_args
);
1076 fnvlist_add_boolean(ioc_args
, "noop");
1077 error
= lzc_ioctl(ZFS_IOC_LOAD_KEY
, fsname
, ioc_args
, NULL
);
1078 nvlist_free(hidden_args
);
1079 nvlist_free(ioc_args
);
1085 lzc_unload_key(const char *fsname
)
1087 return (lzc_ioctl(ZFS_IOC_UNLOAD_KEY
, fsname
, NULL
, NULL
));
1091 lzc_change_key(const char *fsname
, uint64_t crypt_cmd
, nvlist_t
*props
,
1092 uint8_t *wkeydata
, uint_t wkeylen
)
1095 nvlist_t
*ioc_args
= fnvlist_alloc();
1096 nvlist_t
*hidden_args
= NULL
;
1098 fnvlist_add_uint64(ioc_args
, "crypt_cmd", crypt_cmd
);
1100 if (wkeydata
!= NULL
) {
1101 hidden_args
= fnvlist_alloc();
1102 fnvlist_add_uint8_array(hidden_args
, "wkeydata", wkeydata
,
1104 fnvlist_add_nvlist(ioc_args
, ZPOOL_HIDDEN_ARGS
, hidden_args
);
1108 fnvlist_add_nvlist(ioc_args
, "props", props
);
1110 error
= lzc_ioctl(ZFS_IOC_CHANGE_KEY
, fsname
, ioc_args
, NULL
);
1111 nvlist_free(hidden_args
);
1112 nvlist_free(ioc_args
);
1117 lzc_reopen(const char *pool_name
, boolean_t scrub_restart
)
1119 nvlist_t
*args
= fnvlist_alloc();
1122 fnvlist_add_boolean_value(args
, "scrub_restart", scrub_restart
);
1124 error
= lzc_ioctl(ZFS_IOC_POOL_REOPEN
, pool_name
, args
, NULL
);