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.
28 * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
29 * It has the following characteristics:
31 * - Thread Safe. libzfs_core is accessible concurrently from multiple
32 * threads. This is accomplished primarily by avoiding global data
33 * (e.g. caching). Since it's thread-safe, there is no reason for a
34 * process to have multiple libzfs "instances". Therefore, we store
35 * our few pieces of data (e.g. the file descriptor) in global
36 * variables. The fd is reference-counted so that the libzfs_core
37 * library can be "initialized" multiple times (e.g. by different
38 * consumers within the same process).
40 * - Committed Interface. The libzfs_core interface will be committed,
41 * therefore consumers can compile against it and be confident that
42 * their code will continue to work on future releases of this code.
43 * Currently, the interface is Evolving (not Committed), but we intend
44 * to commit to it once it is more complete and we determine that it
45 * meets the needs of all consumers.
47 * - Programmatic Error Handling. libzfs_core communicates errors with
48 * defined error numbers, and doesn't print anything to stdout/stderr.
50 * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
51 * to/from the kernel ioctls. There is generally a 1:1 correspondence
52 * between libzfs_core functions and ioctls to /dev/zfs.
54 * - Clear Atomicity. Because libzfs_core functions are generally 1:1
55 * with kernel ioctls, and kernel ioctls are general atomic, each
56 * libzfs_core function is atomic. For example, creating multiple
57 * snapshots with a single call to lzc_snapshot() is atomic -- it
58 * can't fail with only some of the requested snapshots created, even
59 * in the event of power loss or system crash.
61 * - Continued libzfs Support. Some higher-level operations (e.g.
62 * support for "zfs send -R") are too complicated to fit the scope of
63 * libzfs_core. This functionality will continue to live in libzfs.
64 * Where appropriate, libzfs will use the underlying atomic operations
65 * of libzfs_core. For example, libzfs may implement "zfs send -R |
66 * zfs receive" by using individual "send one snapshot", rename,
67 * destroy, and "receive one snapshot" operations in libzfs_core.
68 * /sbin/zfs and /zbin/zpool will link with both libzfs and
69 * libzfs_core. Other consumers should aim to use only libzfs_core,
70 * since that will be the supported, stable interface going forwards.
73 #include <libzfs_core.h>
81 #include <sys/nvpair.h>
82 #include <sys/param.h>
83 #include <sys/types.h>
85 #include <sys/zfs_ioctl.h>
88 static pthread_mutex_t g_lock
= PTHREAD_MUTEX_INITIALIZER
;
89 static int g_refcount
;
92 libzfs_core_init(void)
94 (void) pthread_mutex_lock(&g_lock
);
95 if (g_refcount
== 0) {
96 g_fd
= open("/dev/zfs", O_RDWR
);
98 (void) pthread_mutex_unlock(&g_lock
);
103 (void) pthread_mutex_unlock(&g_lock
);
108 libzfs_core_fini(void)
110 (void) pthread_mutex_lock(&g_lock
);
111 ASSERT3S(g_refcount
, >, 0);
116 if (g_refcount
== 0 && g_fd
!= -1) {
120 (void) pthread_mutex_unlock(&g_lock
);
124 lzc_ioctl(zfs_ioc_t ioc
, const char *name
,
125 nvlist_t
*source
, nvlist_t
**resultp
)
127 zfs_cmd_t zc
= {"\0"};
132 ASSERT3S(g_refcount
, >, 0);
133 VERIFY3S(g_fd
, !=, -1);
135 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
137 packed
= fnvlist_pack(source
, &size
);
138 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
139 zc
.zc_nvlist_src_size
= size
;
141 if (resultp
!= NULL
) {
143 zc
.zc_nvlist_dst_size
= MAX(size
* 2, 128 * 1024);
144 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
145 malloc(zc
.zc_nvlist_dst_size
);
146 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
152 while (ioctl(g_fd
, ioc
, &zc
) != 0) {
153 if (errno
== ENOMEM
&& resultp
!= NULL
) {
154 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
155 zc
.zc_nvlist_dst_size
*= 2;
156 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
157 malloc(zc
.zc_nvlist_dst_size
);
158 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
167 if (zc
.zc_nvlist_dst_filled
) {
168 *resultp
= fnvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
169 zc
.zc_nvlist_dst_size
);
173 fnvlist_pack_free(packed
, size
);
174 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
179 lzc_create(const char *fsname
, enum lzc_dataset_type type
, nvlist_t
*props
)
182 nvlist_t
*args
= fnvlist_alloc();
183 fnvlist_add_int32(args
, "type", (dmu_objset_type_t
)type
);
185 fnvlist_add_nvlist(args
, "props", props
);
186 error
= lzc_ioctl(ZFS_IOC_CREATE
, fsname
, args
, NULL
);
192 lzc_clone(const char *fsname
, const char *origin
,
196 nvlist_t
*args
= fnvlist_alloc();
197 fnvlist_add_string(args
, "origin", origin
);
199 fnvlist_add_nvlist(args
, "props", props
);
200 error
= lzc_ioctl(ZFS_IOC_CLONE
, fsname
, args
, NULL
);
208 * The keys in the snaps nvlist are the snapshots to be created.
209 * They must all be in the same pool.
211 * The props nvlist is properties to set. Currently only user properties
212 * are supported. { user:prop_name -> string value }
214 * The returned results nvlist will have an entry for each snapshot that failed.
215 * The value will be the (int32) error code.
217 * The return value will be 0 if all snapshots were created, otherwise it will
218 * be the errno of a (unspecified) snapshot that failed.
221 lzc_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
**errlist
)
226 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
230 /* determine the pool name */
231 elem
= nvlist_next_nvpair(snaps
, NULL
);
234 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
235 pool
[strcspn(pool
, "/@")] = '\0';
237 args
= fnvlist_alloc();
238 fnvlist_add_nvlist(args
, "snaps", snaps
);
240 fnvlist_add_nvlist(args
, "props", props
);
242 error
= lzc_ioctl(ZFS_IOC_SNAPSHOT
, pool
, args
, errlist
);
249 * Destroys snapshots.
251 * The keys in the snaps nvlist are the snapshots to be destroyed.
252 * They must all be in the same pool.
254 * Snapshots that do not exist will be silently ignored.
256 * If 'defer' is not set, and a snapshot has user holds or clones, the
257 * destroy operation will fail and none of the snapshots will be
260 * If 'defer' is set, and a snapshot has user holds or clones, it will be
261 * marked for deferred destruction, and will be destroyed when the last hold
262 * or clone is removed/destroyed.
264 * The return value will be 0 if all snapshots were destroyed (or marked for
265 * later destruction if 'defer' is set) or didn't exist to begin with.
267 * Otherwise the return value will be the errno of a (unspecified) snapshot
268 * that failed, no snapshots will be destroyed, and the errlist will have an
269 * entry for each snapshot that failed. The value in the errlist will be
270 * the (int32) error code.
273 lzc_destroy_snaps(nvlist_t
*snaps
, boolean_t defer
, nvlist_t
**errlist
)
278 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
280 /* determine the pool name */
281 elem
= nvlist_next_nvpair(snaps
, NULL
);
284 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
285 pool
[strcspn(pool
, "/@")] = '\0';
287 args
= fnvlist_alloc();
288 fnvlist_add_nvlist(args
, "snaps", snaps
);
290 fnvlist_add_boolean(args
, "defer");
292 error
= lzc_ioctl(ZFS_IOC_DESTROY_SNAPS
, pool
, args
, errlist
);
299 lzc_snaprange_space(const char *firstsnap
, const char *lastsnap
,
305 char fs
[ZFS_MAX_DATASET_NAME_LEN
];
308 /* determine the fs name */
309 (void) strlcpy(fs
, firstsnap
, sizeof (fs
));
310 atp
= strchr(fs
, '@');
315 args
= fnvlist_alloc();
316 fnvlist_add_string(args
, "firstsnap", firstsnap
);
318 err
= lzc_ioctl(ZFS_IOC_SPACE_SNAPS
, lastsnap
, args
, &result
);
321 *usedp
= fnvlist_lookup_uint64(result
, "used");
322 fnvlist_free(result
);
328 lzc_exists(const char *dataset
)
331 * The objset_stats ioctl is still legacy, so we need to construct our
332 * own zfs_cmd_t rather than using zfsc_ioctl().
334 zfs_cmd_t zc
= {"\0"};
336 ASSERT3S(g_refcount
, >, 0);
337 VERIFY3S(g_fd
, !=, -1);
339 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
340 return (ioctl(g_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) == 0);
344 * Create "user holds" on snapshots. If there is a hold on a snapshot,
345 * the snapshot can not be destroyed. (However, it can be marked for deletion
346 * by lzc_destroy_snaps(defer=B_TRUE).)
348 * The keys in the nvlist are snapshot names.
349 * The snapshots must all be in the same pool.
350 * The value is the name of the hold (string type).
352 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
353 * In this case, when the cleanup_fd is closed (including on process
354 * termination), the holds will be released. If the system is shut down
355 * uncleanly, the holds will be released when the pool is next opened
358 * Holds for snapshots which don't exist will be skipped and have an entry
359 * added to errlist, but will not cause an overall failure.
361 * The return value will be 0 if all holds, for snapshots that existed,
362 * were successfully created.
364 * Otherwise the return value will be the errno of a (unspecified) hold that
365 * failed and no holds will be created.
367 * In all cases the errlist will have an entry for each hold that failed
368 * (name = snapshot), with its value being the error code (int32).
371 lzc_hold(nvlist_t
*holds
, int cleanup_fd
, nvlist_t
**errlist
)
373 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
378 /* determine the pool name */
379 elem
= nvlist_next_nvpair(holds
, NULL
);
382 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
383 pool
[strcspn(pool
, "/@")] = '\0';
385 args
= fnvlist_alloc();
386 fnvlist_add_nvlist(args
, "holds", holds
);
387 if (cleanup_fd
!= -1)
388 fnvlist_add_int32(args
, "cleanup_fd", cleanup_fd
);
390 error
= lzc_ioctl(ZFS_IOC_HOLD
, pool
, args
, errlist
);
396 * Release "user holds" on snapshots. If the snapshot has been marked for
397 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
398 * any clones, and all the user holds are removed, then the snapshot will be
401 * The keys in the nvlist are snapshot names.
402 * The snapshots must all be in the same pool.
403 * The value is an nvlist whose keys are the holds to remove.
405 * Holds which failed to release because they didn't exist will have an entry
406 * added to errlist, but will not cause an overall failure.
408 * The return value will be 0 if the nvl holds was empty or all holds that
409 * existed, were successfully removed.
411 * Otherwise the return value will be the errno of a (unspecified) hold that
412 * failed to release and no holds will be released.
414 * In all cases the errlist will have an entry for each hold that failed to
418 lzc_release(nvlist_t
*holds
, nvlist_t
**errlist
)
420 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
423 /* determine the pool name */
424 elem
= nvlist_next_nvpair(holds
, NULL
);
427 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
428 pool
[strcspn(pool
, "/@")] = '\0';
430 return (lzc_ioctl(ZFS_IOC_RELEASE
, pool
, holds
, errlist
));
434 * Retrieve list of user holds on the specified snapshot.
436 * On success, *holdsp will be set to an nvlist which the caller must free.
437 * The keys are the names of the holds, and the value is the creation time
438 * of the hold (uint64) in seconds since the epoch.
441 lzc_get_holds(const char *snapname
, nvlist_t
**holdsp
)
444 nvlist_t
*innvl
= fnvlist_alloc();
445 error
= lzc_ioctl(ZFS_IOC_GET_HOLDS
, snapname
, innvl
, holdsp
);
451 * Generate a zfs send stream for the specified snapshot and write it to
452 * the specified file descriptor.
454 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
456 * If "from" is NULL, a full (non-incremental) stream will be sent.
457 * If "from" is non-NULL, it must be the full name of a snapshot or
458 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
459 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
460 * bookmark must represent an earlier point in the history of "snapname").
461 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
462 * or it can be the origin of "snapname"'s filesystem, or an earlier
463 * snapshot in the origin, etc.
465 * "fd" is the file descriptor to write the send stream to.
467 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
468 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
469 * records with drr_blksz > 128K.
471 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
472 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
473 * which the receiving system must support (as indicated by support
474 * for the "embedded_data" feature).
477 lzc_send(const char *snapname
, const char *from
, int fd
,
478 enum lzc_send_flags flags
)
480 return (lzc_send_resume(snapname
, from
, fd
, flags
, 0, 0));
484 lzc_send_resume(const char *snapname
, const char *from
, int fd
,
485 enum lzc_send_flags flags
, uint64_t resumeobj
, uint64_t resumeoff
)
490 args
= fnvlist_alloc();
491 fnvlist_add_int32(args
, "fd", fd
);
493 fnvlist_add_string(args
, "fromsnap", from
);
494 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
495 fnvlist_add_boolean(args
, "largeblockok");
496 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
497 fnvlist_add_boolean(args
, "embedok");
498 if (flags
& LZC_SEND_FLAG_COMPRESS
)
499 fnvlist_add_boolean(args
, "compressok");
500 if (resumeobj
!= 0 || resumeoff
!= 0) {
501 fnvlist_add_uint64(args
, "resume_object", resumeobj
);
502 fnvlist_add_uint64(args
, "resume_offset", resumeoff
);
504 err
= lzc_ioctl(ZFS_IOC_SEND_NEW
, snapname
, args
, NULL
);
510 * "from" can be NULL, a snapshot, or a bookmark.
512 * If from is NULL, a full (non-incremental) stream will be estimated. This
513 * is calculated very efficiently.
515 * If from is a snapshot, lzc_send_space uses the deadlists attached to
516 * each snapshot to efficiently estimate the stream size.
518 * If from is a bookmark, the indirect blocks in the destination snapshot
519 * are traversed, looking for blocks with a birth time since the creation TXG of
520 * the snapshot this bookmark was created from. This will result in
521 * significantly more I/O and be less efficient than a send space estimation on
522 * an equivalent snapshot.
525 lzc_send_space(const char *snapname
, const char *from
,
526 enum lzc_send_flags flags
, uint64_t *spacep
)
532 args
= fnvlist_alloc();
534 fnvlist_add_string(args
, "from", from
);
535 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
536 fnvlist_add_boolean(args
, "largeblockok");
537 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
538 fnvlist_add_boolean(args
, "embedok");
539 if (flags
& LZC_SEND_FLAG_COMPRESS
)
540 fnvlist_add_boolean(args
, "compressok");
541 err
= lzc_ioctl(ZFS_IOC_SEND_SPACE
, snapname
, args
, &result
);
544 *spacep
= fnvlist_lookup_uint64(result
, "space");
550 recv_read(int fd
, void *buf
, int ilen
)
557 rv
= read(fd
, cp
, len
);
562 if (rv
< 0 || len
!= 0)
569 * Linux adds ZFS_IOC_RECV_NEW for resumable streams and preserves the legacy
570 * ZFS_IOC_RECV user/kernel interface. The new interface supports all stream
571 * options but is currently only used for resumable streams. This way updated
572 * user space utilities will interoperate with older kernel modules.
574 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
577 recv_impl(const char *snapname
, nvlist_t
*props
, const char *origin
,
578 boolean_t force
, boolean_t resumable
, int input_fd
,
579 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
580 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
583 dmu_replay_record_t drr
;
584 char fsname
[MAXPATHLEN
];
588 ASSERT3S(g_refcount
, >, 0);
589 VERIFY3S(g_fd
, !=, -1);
591 /* Set 'fsname' to the name of containing filesystem */
592 (void) strlcpy(fsname
, snapname
, sizeof (fsname
));
593 atp
= strchr(fsname
, '@');
598 /* If the fs does not exist, try its parent. */
599 if (!lzc_exists(fsname
)) {
600 char *slashp
= strrchr(fsname
, '/');
607 * The begin_record is normally a non-byteswapped BEGIN record.
608 * For resumable streams it may be set to any non-byteswapped
609 * dmu_replay_record_t.
611 if (begin_record
== NULL
) {
612 error
= recv_read(input_fd
, &drr
, sizeof (drr
));
620 nvlist_t
*outnvl
= NULL
;
621 nvlist_t
*innvl
= fnvlist_alloc();
623 fnvlist_add_string(innvl
, "snapname", snapname
);
626 fnvlist_add_nvlist(innvl
, "props", props
);
628 if (origin
!= NULL
&& strlen(origin
))
629 fnvlist_add_string(innvl
, "origin", origin
);
631 fnvlist_add_byte_array(innvl
, "begin_record",
632 (uchar_t
*)&drr
, sizeof (drr
));
634 fnvlist_add_int32(innvl
, "input_fd", input_fd
);
637 fnvlist_add_boolean(innvl
, "force");
640 fnvlist_add_boolean(innvl
, "resumable");
643 fnvlist_add_int32(innvl
, "cleanup_fd", cleanup_fd
);
645 if (action_handle
!= NULL
)
646 fnvlist_add_uint64(innvl
, "action_handle",
649 error
= lzc_ioctl(ZFS_IOC_RECV_NEW
, fsname
, innvl
, &outnvl
);
651 if (error
== 0 && read_bytes
!= NULL
)
652 error
= nvlist_lookup_uint64(outnvl
, "read_bytes",
655 if (error
== 0 && errflags
!= NULL
)
656 error
= nvlist_lookup_uint64(outnvl
, "error_flags",
659 if (error
== 0 && action_handle
!= NULL
)
660 error
= nvlist_lookup_uint64(outnvl
, "action_handle",
663 if (error
== 0 && errors
!= NULL
) {
665 error
= nvlist_lookup_nvlist(outnvl
, "errors", &nvl
);
667 *errors
= fnvlist_dup(nvl
);
671 fnvlist_free(outnvl
);
673 zfs_cmd_t zc
= {"\0"};
677 ASSERT3S(g_refcount
, >, 0);
679 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_value
));
680 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
683 packed
= fnvlist_pack(props
, &size
);
684 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
685 zc
.zc_nvlist_src_size
= size
;
689 (void) strlcpy(zc
.zc_string
, origin
,
690 sizeof (zc
.zc_string
));
692 ASSERT3S(drr
.drr_type
, ==, DRR_BEGIN
);
693 zc
.zc_begin_record
= drr
.drr_u
.drr_begin
;
695 zc
.zc_cookie
= input_fd
;
696 zc
.zc_cleanup_fd
= -1;
697 zc
.zc_action_handle
= 0;
700 zc
.zc_cleanup_fd
= cleanup_fd
;
702 if (action_handle
!= NULL
)
703 zc
.zc_action_handle
= *action_handle
;
705 zc
.zc_nvlist_dst_size
= 128 * 1024;
706 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
707 malloc(zc
.zc_nvlist_dst_size
);
709 error
= ioctl(g_fd
, ZFS_IOC_RECV
, &zc
);
713 if (read_bytes
!= NULL
)
714 *read_bytes
= zc
.zc_cookie
;
716 if (errflags
!= NULL
)
717 *errflags
= zc
.zc_obj
;
719 if (action_handle
!= NULL
)
720 *action_handle
= zc
.zc_action_handle
;
723 VERIFY0(nvlist_unpack(
724 (void *)(uintptr_t)zc
.zc_nvlist_dst
,
725 zc
.zc_nvlist_dst_size
, errors
, KM_SLEEP
));
729 fnvlist_pack_free(packed
, size
);
730 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
737 * The simplest receive case: receive from the specified fd, creating the
738 * specified snapshot. Apply the specified properties as "received" properties
739 * (which can be overridden by locally-set properties). If the stream is a
740 * clone, its origin snapshot must be specified by 'origin'. The 'force'
741 * flag will cause the target filesystem to be rolled back or destroyed if
742 * necessary to receive.
744 * Return 0 on success or an errno on failure.
746 * Note: this interface does not work on dedup'd streams
747 * (those with DMU_BACKUP_FEATURE_DEDUP).
750 lzc_receive(const char *snapname
, nvlist_t
*props
, const char *origin
,
751 boolean_t force
, int fd
)
753 return (recv_impl(snapname
, props
, origin
, force
, B_FALSE
, fd
,
754 NULL
, -1, NULL
, NULL
, NULL
, NULL
));
758 * Like lzc_receive, but if the receive fails due to premature stream
759 * termination, the intermediate state will be preserved on disk. In this
760 * case, ECKSUM will be returned. The receive may subsequently be resumed
761 * with a resuming send stream generated by lzc_send_resume().
764 lzc_receive_resumable(const char *snapname
, nvlist_t
*props
, const char *origin
,
765 boolean_t force
, int fd
)
767 return (recv_impl(snapname
, props
, origin
, force
, B_TRUE
, fd
,
768 NULL
, -1, NULL
, NULL
, NULL
, NULL
));
772 * Like lzc_receive, but allows the caller to read the begin record and then to
773 * pass it in. That could be useful if the caller wants to derive, for example,
774 * the snapname or the origin parameters based on the information contained in
776 * The begin record must be in its original form as read from the stream,
777 * in other words, it should not be byteswapped.
779 * The 'resumable' parameter allows to obtain the same behavior as with
780 * lzc_receive_resumable.
783 lzc_receive_with_header(const char *snapname
, nvlist_t
*props
,
784 const char *origin
, boolean_t force
, boolean_t resumable
, int fd
,
785 const dmu_replay_record_t
*begin_record
)
787 if (begin_record
== NULL
)
789 return (recv_impl(snapname
, props
, origin
, force
, resumable
, fd
,
790 begin_record
, -1, NULL
, NULL
, NULL
, NULL
));
794 * Like lzc_receive, but allows the caller to pass all supported arguments
795 * and retrieve all values returned. The only additional input parameter
796 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
798 * The following parameters all provide return values. Several may be set
799 * in the failure case and will contain additional information.
801 * The 'read_bytes' value will be set to the total number of bytes read.
803 * The 'errflags' value will contain zprop_errflags_t flags which are
804 * used to describe any failures.
806 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
807 * It should be set to zero on first call and will contain an updated handle
808 * on success, it should be passed in subsequent calls.
810 * The 'errors' nvlist contains an entry for each unapplied received
811 * property. Callers are responsible for freeing this nvlist.
813 int lzc_receive_one(const char *snapname
, nvlist_t
*props
,
814 const char *origin
, boolean_t force
, boolean_t resumable
, int input_fd
,
815 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
816 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
819 return (recv_impl(snapname
, props
, origin
, force
, resumable
,
820 input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
821 action_handle
, errors
));
825 * Roll back this filesystem or volume to its most recent snapshot.
826 * If snapnamebuf is not NULL, it will be filled in with the name
827 * of the most recent snapshot.
829 * Return 0 on success or an errno on failure.
832 lzc_rollback(const char *fsname
, char *snapnamebuf
, int snapnamelen
)
838 args
= fnvlist_alloc();
839 err
= lzc_ioctl(ZFS_IOC_ROLLBACK
, fsname
, args
, &result
);
841 if (err
== 0 && snapnamebuf
!= NULL
) {
842 const char *snapname
= fnvlist_lookup_string(result
, "target");
843 (void) strlcpy(snapnamebuf
, snapname
, snapnamelen
);
853 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
854 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
855 * snapshots must be in the same pool.
857 * The returned results nvlist will have an entry for each bookmark that failed.
858 * The value will be the (int32) error code.
860 * The return value will be 0 if all bookmarks were created, otherwise it will
861 * be the errno of a (undetermined) bookmarks that failed.
864 lzc_bookmark(nvlist_t
*bookmarks
, nvlist_t
**errlist
)
868 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
870 /* determine the pool name */
871 elem
= nvlist_next_nvpair(bookmarks
, NULL
);
874 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
875 pool
[strcspn(pool
, "/#")] = '\0';
877 error
= lzc_ioctl(ZFS_IOC_BOOKMARK
, pool
, bookmarks
, errlist
);
883 * Retrieve bookmarks.
885 * Retrieve the list of bookmarks for the given file system. The props
886 * parameter is an nvlist of property names (with no values) that will be
887 * returned for each bookmark.
889 * The following are valid properties on bookmarks, all of which are numbers
890 * (represented as uint64 in the nvlist)
892 * "guid" - globally unique identifier of the snapshot it refers to
893 * "createtxg" - txg when the snapshot it refers to was created
894 * "creation" - timestamp when the snapshot it refers to was created
896 * The format of the returned nvlist as follows:
897 * <short name of bookmark> -> {
898 * <name of property> -> {
904 lzc_get_bookmarks(const char *fsname
, nvlist_t
*props
, nvlist_t
**bmarks
)
906 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS
, fsname
, props
, bmarks
));
910 * Destroys bookmarks.
912 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
913 * They must all be in the same pool. Bookmarks are specified as
916 * Bookmarks that do not exist will be silently ignored.
918 * The return value will be 0 if all bookmarks that existed were destroyed.
920 * Otherwise the return value will be the errno of a (undetermined) bookmark
921 * that failed, no bookmarks will be destroyed, and the errlist will have an
922 * entry for each bookmarks that failed. The value in the errlist will be
923 * the (int32) error code.
926 lzc_destroy_bookmarks(nvlist_t
*bmarks
, nvlist_t
**errlist
)
930 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
932 /* determine the pool name */
933 elem
= nvlist_next_nvpair(bmarks
, NULL
);
936 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
937 pool
[strcspn(pool
, "/#")] = '\0';
939 error
= lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS
, pool
, bmarks
, errlist
);