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.
29 * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
30 * It has the following characteristics:
32 * - Thread Safe. libzfs_core is accessible concurrently from multiple
33 * threads. This is accomplished primarily by avoiding global data
34 * (e.g. caching). Since it's thread-safe, there is no reason for a
35 * process to have multiple libzfs "instances". Therefore, we store
36 * our few pieces of data (e.g. the file descriptor) in global
37 * variables. The fd is reference-counted so that the libzfs_core
38 * library can be "initialized" multiple times (e.g. by different
39 * consumers within the same process).
41 * - Committed Interface. The libzfs_core interface will be committed,
42 * therefore consumers can compile against it and be confident that
43 * their code will continue to work on future releases of this code.
44 * Currently, the interface is Evolving (not Committed), but we intend
45 * to commit to it once it is more complete and we determine that it
46 * meets the needs of all consumers.
48 * - Programmatic Error Handling. libzfs_core communicates errors with
49 * defined error numbers, and doesn't print anything to stdout/stderr.
51 * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
52 * to/from the kernel ioctls. There is generally a 1:1 correspondence
53 * between libzfs_core functions and ioctls to /dev/zfs.
55 * - Clear Atomicity. Because libzfs_core functions are generally 1:1
56 * with kernel ioctls, and kernel ioctls are general atomic, each
57 * libzfs_core function is atomic. For example, creating multiple
58 * snapshots with a single call to lzc_snapshot() is atomic -- it
59 * can't fail with only some of the requested snapshots created, even
60 * in the event of power loss or system crash.
62 * - Continued libzfs Support. Some higher-level operations (e.g.
63 * support for "zfs send -R") are too complicated to fit the scope of
64 * libzfs_core. This functionality will continue to live in libzfs.
65 * Where appropriate, libzfs will use the underlying atomic operations
66 * of libzfs_core. For example, libzfs may implement "zfs send -R |
67 * zfs receive" by using individual "send one snapshot", rename,
68 * destroy, and "receive one snapshot" operations in libzfs_core.
69 * /sbin/zfs and /zbin/zpool will link with both libzfs and
70 * libzfs_core. Other consumers should aim to use only libzfs_core,
71 * since that will be the supported, stable interface going forwards.
74 #include <libzfs_core.h>
82 #include <sys/nvpair.h>
83 #include <sys/param.h>
84 #include <sys/types.h>
86 #include <sys/zfs_ioctl.h>
89 static pthread_mutex_t g_lock
= PTHREAD_MUTEX_INITIALIZER
;
90 static int g_refcount
;
93 libzfs_core_init(void)
95 (void) pthread_mutex_lock(&g_lock
);
96 if (g_refcount
== 0) {
97 g_fd
= open("/dev/zfs", O_RDWR
);
99 (void) pthread_mutex_unlock(&g_lock
);
104 (void) pthread_mutex_unlock(&g_lock
);
109 libzfs_core_fini(void)
111 (void) pthread_mutex_lock(&g_lock
);
112 ASSERT3S(g_refcount
, >, 0);
117 if (g_refcount
== 0 && g_fd
!= -1) {
121 (void) pthread_mutex_unlock(&g_lock
);
125 lzc_ioctl(zfs_ioc_t ioc
, const char *name
,
126 nvlist_t
*source
, nvlist_t
**resultp
)
128 zfs_cmd_t zc
= {"\0"};
133 ASSERT3S(g_refcount
, >, 0);
134 VERIFY3S(g_fd
, !=, -1);
137 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
139 if (source
!= NULL
) {
140 packed
= fnvlist_pack(source
, &size
);
141 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
142 zc
.zc_nvlist_src_size
= size
;
145 if (resultp
!= NULL
) {
147 zc
.zc_nvlist_dst_size
= MAX(size
* 2, 128 * 1024);
148 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
149 malloc(zc
.zc_nvlist_dst_size
);
150 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
156 while (ioctl(g_fd
, ioc
, &zc
) != 0) {
157 if (errno
== ENOMEM
&& resultp
!= NULL
) {
158 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
159 zc
.zc_nvlist_dst_size
*= 2;
160 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
161 malloc(zc
.zc_nvlist_dst_size
);
162 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
171 if (zc
.zc_nvlist_dst_filled
) {
172 *resultp
= fnvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
173 zc
.zc_nvlist_dst_size
);
177 fnvlist_pack_free(packed
, size
);
178 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
183 lzc_create(const char *fsname
, enum lzc_dataset_type type
, nvlist_t
*props
)
186 nvlist_t
*args
= fnvlist_alloc();
187 fnvlist_add_int32(args
, "type", (dmu_objset_type_t
)type
);
189 fnvlist_add_nvlist(args
, "props", props
);
190 error
= lzc_ioctl(ZFS_IOC_CREATE
, fsname
, args
, NULL
);
196 lzc_clone(const char *fsname
, const char *origin
,
200 nvlist_t
*args
= fnvlist_alloc();
201 fnvlist_add_string(args
, "origin", origin
);
203 fnvlist_add_nvlist(args
, "props", props
);
204 error
= lzc_ioctl(ZFS_IOC_CLONE
, fsname
, args
, NULL
);
212 * The keys in the snaps nvlist are the snapshots to be created.
213 * They must all be in the same pool.
215 * The props nvlist is properties to set. Currently only user properties
216 * are supported. { user:prop_name -> string value }
218 * The returned results nvlist will have an entry for each snapshot that failed.
219 * The value will be the (int32) error code.
221 * The return value will be 0 if all snapshots were created, otherwise it will
222 * be the errno of a (unspecified) snapshot that failed.
225 lzc_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
**errlist
)
230 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
234 /* determine the pool name */
235 elem
= nvlist_next_nvpair(snaps
, NULL
);
238 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
239 pool
[strcspn(pool
, "/@")] = '\0';
241 args
= fnvlist_alloc();
242 fnvlist_add_nvlist(args
, "snaps", snaps
);
244 fnvlist_add_nvlist(args
, "props", props
);
246 error
= lzc_ioctl(ZFS_IOC_SNAPSHOT
, pool
, args
, errlist
);
253 * Destroys snapshots.
255 * The keys in the snaps nvlist are the snapshots to be destroyed.
256 * They must all be in the same pool.
258 * Snapshots that do not exist will be silently ignored.
260 * If 'defer' is not set, and a snapshot has user holds or clones, the
261 * destroy operation will fail and none of the snapshots will be
264 * If 'defer' is set, and a snapshot has user holds or clones, it will be
265 * marked for deferred destruction, and will be destroyed when the last hold
266 * or clone is removed/destroyed.
268 * The return value will be 0 if all snapshots were destroyed (or marked for
269 * later destruction if 'defer' is set) or didn't exist to begin with.
271 * Otherwise the return value will be the errno of a (unspecified) snapshot
272 * that failed, no snapshots will be destroyed, and the errlist will have an
273 * entry for each snapshot that failed. The value in the errlist will be
274 * the (int32) error code.
277 lzc_destroy_snaps(nvlist_t
*snaps
, boolean_t defer
, nvlist_t
**errlist
)
282 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
284 /* determine the pool name */
285 elem
= nvlist_next_nvpair(snaps
, NULL
);
288 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
289 pool
[strcspn(pool
, "/@")] = '\0';
291 args
= fnvlist_alloc();
292 fnvlist_add_nvlist(args
, "snaps", snaps
);
294 fnvlist_add_boolean(args
, "defer");
296 error
= lzc_ioctl(ZFS_IOC_DESTROY_SNAPS
, pool
, args
, errlist
);
303 lzc_snaprange_space(const char *firstsnap
, const char *lastsnap
,
309 char fs
[ZFS_MAX_DATASET_NAME_LEN
];
312 /* determine the fs name */
313 (void) strlcpy(fs
, firstsnap
, sizeof (fs
));
314 atp
= strchr(fs
, '@');
319 args
= fnvlist_alloc();
320 fnvlist_add_string(args
, "firstsnap", firstsnap
);
322 err
= lzc_ioctl(ZFS_IOC_SPACE_SNAPS
, lastsnap
, args
, &result
);
325 *usedp
= fnvlist_lookup_uint64(result
, "used");
326 fnvlist_free(result
);
332 lzc_exists(const char *dataset
)
335 * The objset_stats ioctl is still legacy, so we need to construct our
336 * own zfs_cmd_t rather than using zfsc_ioctl().
338 zfs_cmd_t zc
= {"\0"};
340 ASSERT3S(g_refcount
, >, 0);
341 VERIFY3S(g_fd
, !=, -1);
343 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
344 return (ioctl(g_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) == 0);
349 * It was added to preserve the function signature in case it is
350 * needed in the future.
354 lzc_sync(const char *pool_name
, nvlist_t
*innvl
, nvlist_t
**outnvl
)
356 return (lzc_ioctl(ZFS_IOC_POOL_SYNC
, pool_name
, innvl
, NULL
));
360 * Create "user holds" on snapshots. If there is a hold on a snapshot,
361 * the snapshot can not be destroyed. (However, it can be marked for deletion
362 * by lzc_destroy_snaps(defer=B_TRUE).)
364 * The keys in the nvlist are snapshot names.
365 * The snapshots must all be in the same pool.
366 * The value is the name of the hold (string type).
368 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
369 * In this case, when the cleanup_fd is closed (including on process
370 * termination), the holds will be released. If the system is shut down
371 * uncleanly, the holds will be released when the pool is next opened
374 * Holds for snapshots which don't exist will be skipped and have an entry
375 * added to errlist, but will not cause an overall failure.
377 * The return value will be 0 if all holds, for snapshots that existed,
378 * were successfully created.
380 * Otherwise the return value will be the errno of a (unspecified) hold that
381 * failed and no holds will be created.
383 * In all cases the errlist will have an entry for each hold that failed
384 * (name = snapshot), with its value being the error code (int32).
387 lzc_hold(nvlist_t
*holds
, int cleanup_fd
, nvlist_t
**errlist
)
389 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
394 /* determine the pool name */
395 elem
= nvlist_next_nvpair(holds
, NULL
);
398 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
399 pool
[strcspn(pool
, "/@")] = '\0';
401 args
= fnvlist_alloc();
402 fnvlist_add_nvlist(args
, "holds", holds
);
403 if (cleanup_fd
!= -1)
404 fnvlist_add_int32(args
, "cleanup_fd", cleanup_fd
);
406 error
= lzc_ioctl(ZFS_IOC_HOLD
, pool
, args
, errlist
);
412 * Release "user holds" on snapshots. If the snapshot has been marked for
413 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
414 * any clones, and all the user holds are removed, then the snapshot will be
417 * The keys in the nvlist are snapshot names.
418 * The snapshots must all be in the same pool.
419 * The value is an nvlist whose keys are the holds to remove.
421 * Holds which failed to release because they didn't exist will have an entry
422 * added to errlist, but will not cause an overall failure.
424 * The return value will be 0 if the nvl holds was empty or all holds that
425 * existed, were successfully removed.
427 * Otherwise the return value will be the errno of a (unspecified) hold that
428 * failed to release and no holds will be released.
430 * In all cases the errlist will have an entry for each hold that failed to
434 lzc_release(nvlist_t
*holds
, nvlist_t
**errlist
)
436 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
439 /* determine the pool name */
440 elem
= nvlist_next_nvpair(holds
, NULL
);
443 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
444 pool
[strcspn(pool
, "/@")] = '\0';
446 return (lzc_ioctl(ZFS_IOC_RELEASE
, pool
, holds
, errlist
));
450 * Retrieve list of user holds on the specified snapshot.
452 * On success, *holdsp will be set to an nvlist which the caller must free.
453 * The keys are the names of the holds, and the value is the creation time
454 * of the hold (uint64) in seconds since the epoch.
457 lzc_get_holds(const char *snapname
, nvlist_t
**holdsp
)
459 return (lzc_ioctl(ZFS_IOC_GET_HOLDS
, snapname
, NULL
, holdsp
));
463 * Generate a zfs send stream for the specified snapshot and write it to
464 * the specified file descriptor.
466 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
468 * If "from" is NULL, a full (non-incremental) stream will be sent.
469 * If "from" is non-NULL, it must be the full name of a snapshot or
470 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
471 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
472 * bookmark must represent an earlier point in the history of "snapname").
473 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
474 * or it can be the origin of "snapname"'s filesystem, or an earlier
475 * snapshot in the origin, etc.
477 * "fd" is the file descriptor to write the send stream to.
479 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
480 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
481 * records with drr_blksz > 128K.
483 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
484 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
485 * which the receiving system must support (as indicated by support
486 * for the "embedded_data" feature).
489 lzc_send(const char *snapname
, const char *from
, int fd
,
490 enum lzc_send_flags flags
)
492 return (lzc_send_resume(snapname
, from
, fd
, flags
, 0, 0));
496 lzc_send_resume(const char *snapname
, const char *from
, int fd
,
497 enum lzc_send_flags flags
, uint64_t resumeobj
, uint64_t resumeoff
)
502 args
= fnvlist_alloc();
503 fnvlist_add_int32(args
, "fd", fd
);
505 fnvlist_add_string(args
, "fromsnap", from
);
506 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
507 fnvlist_add_boolean(args
, "largeblockok");
508 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
509 fnvlist_add_boolean(args
, "embedok");
510 if (flags
& LZC_SEND_FLAG_COMPRESS
)
511 fnvlist_add_boolean(args
, "compressok");
512 if (resumeobj
!= 0 || resumeoff
!= 0) {
513 fnvlist_add_uint64(args
, "resume_object", resumeobj
);
514 fnvlist_add_uint64(args
, "resume_offset", resumeoff
);
516 err
= lzc_ioctl(ZFS_IOC_SEND_NEW
, snapname
, args
, NULL
);
522 * "from" can be NULL, a snapshot, or a bookmark.
524 * If from is NULL, a full (non-incremental) stream will be estimated. This
525 * is calculated very efficiently.
527 * If from is a snapshot, lzc_send_space uses the deadlists attached to
528 * each snapshot to efficiently estimate the stream size.
530 * If from is a bookmark, the indirect blocks in the destination snapshot
531 * are traversed, looking for blocks with a birth time since the creation TXG of
532 * the snapshot this bookmark was created from. This will result in
533 * significantly more I/O and be less efficient than a send space estimation on
534 * an equivalent snapshot.
537 lzc_send_space(const char *snapname
, const char *from
,
538 enum lzc_send_flags flags
, uint64_t *spacep
)
544 args
= fnvlist_alloc();
546 fnvlist_add_string(args
, "from", from
);
547 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
548 fnvlist_add_boolean(args
, "largeblockok");
549 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
550 fnvlist_add_boolean(args
, "embedok");
551 if (flags
& LZC_SEND_FLAG_COMPRESS
)
552 fnvlist_add_boolean(args
, "compressok");
553 err
= lzc_ioctl(ZFS_IOC_SEND_SPACE
, snapname
, args
, &result
);
556 *spacep
= fnvlist_lookup_uint64(result
, "space");
562 recv_read(int fd
, void *buf
, int ilen
)
569 rv
= read(fd
, cp
, len
);
574 if (rv
< 0 || len
!= 0)
581 * Linux adds ZFS_IOC_RECV_NEW for resumable streams and preserves the legacy
582 * ZFS_IOC_RECV user/kernel interface. The new interface supports all stream
583 * options but is currently only used for resumable streams. This way updated
584 * user space utilities will interoperate with older kernel modules.
586 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
589 recv_impl(const char *snapname
, nvlist_t
*recvdprops
, nvlist_t
*localprops
,
590 const char *origin
, boolean_t force
, boolean_t resumable
, int input_fd
,
591 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
592 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
595 dmu_replay_record_t drr
;
596 char fsname
[MAXPATHLEN
];
600 ASSERT3S(g_refcount
, >, 0);
601 VERIFY3S(g_fd
, !=, -1);
603 /* Set 'fsname' to the name of containing filesystem */
604 (void) strlcpy(fsname
, snapname
, sizeof (fsname
));
605 atp
= strchr(fsname
, '@');
610 /* If the fs does not exist, try its parent. */
611 if (!lzc_exists(fsname
)) {
612 char *slashp
= strrchr(fsname
, '/');
619 * The begin_record is normally a non-byteswapped BEGIN record.
620 * For resumable streams it may be set to any non-byteswapped
621 * dmu_replay_record_t.
623 if (begin_record
== NULL
) {
624 error
= recv_read(input_fd
, &drr
, sizeof (drr
));
632 nvlist_t
*outnvl
= NULL
;
633 nvlist_t
*innvl
= fnvlist_alloc();
635 fnvlist_add_string(innvl
, "snapname", snapname
);
637 if (recvdprops
!= NULL
)
638 fnvlist_add_nvlist(innvl
, "props", recvdprops
);
640 if (localprops
!= NULL
)
641 fnvlist_add_nvlist(innvl
, "localprops", localprops
);
643 if (origin
!= NULL
&& strlen(origin
))
644 fnvlist_add_string(innvl
, "origin", origin
);
646 fnvlist_add_byte_array(innvl
, "begin_record",
647 (uchar_t
*)&drr
, sizeof (drr
));
649 fnvlist_add_int32(innvl
, "input_fd", input_fd
);
652 fnvlist_add_boolean(innvl
, "force");
655 fnvlist_add_boolean(innvl
, "resumable");
658 fnvlist_add_int32(innvl
, "cleanup_fd", cleanup_fd
);
660 if (action_handle
!= NULL
)
661 fnvlist_add_uint64(innvl
, "action_handle",
664 error
= lzc_ioctl(ZFS_IOC_RECV_NEW
, fsname
, innvl
, &outnvl
);
666 if (error
== 0 && read_bytes
!= NULL
)
667 error
= nvlist_lookup_uint64(outnvl
, "read_bytes",
670 if (error
== 0 && errflags
!= NULL
)
671 error
= nvlist_lookup_uint64(outnvl
, "error_flags",
674 if (error
== 0 && action_handle
!= NULL
)
675 error
= nvlist_lookup_uint64(outnvl
, "action_handle",
678 if (error
== 0 && errors
!= NULL
) {
680 error
= nvlist_lookup_nvlist(outnvl
, "errors", &nvl
);
682 *errors
= fnvlist_dup(nvl
);
686 fnvlist_free(outnvl
);
688 zfs_cmd_t zc
= {"\0"};
692 ASSERT3S(g_refcount
, >, 0);
694 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_value
));
695 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
697 if (recvdprops
!= NULL
) {
698 packed
= fnvlist_pack(recvdprops
, &size
);
699 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
700 zc
.zc_nvlist_src_size
= size
;
703 if (localprops
!= NULL
) {
704 packed
= fnvlist_pack(localprops
, &size
);
705 zc
.zc_nvlist_conf
= (uint64_t)(uintptr_t)packed
;
706 zc
.zc_nvlist_conf_size
= size
;
710 (void) strlcpy(zc
.zc_string
, origin
,
711 sizeof (zc
.zc_string
));
713 ASSERT3S(drr
.drr_type
, ==, DRR_BEGIN
);
714 zc
.zc_begin_record
= drr
.drr_u
.drr_begin
;
716 zc
.zc_cookie
= input_fd
;
717 zc
.zc_cleanup_fd
= -1;
718 zc
.zc_action_handle
= 0;
721 zc
.zc_cleanup_fd
= cleanup_fd
;
723 if (action_handle
!= NULL
)
724 zc
.zc_action_handle
= *action_handle
;
726 zc
.zc_nvlist_dst_size
= 128 * 1024;
727 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
728 malloc(zc
.zc_nvlist_dst_size
);
730 error
= ioctl(g_fd
, ZFS_IOC_RECV
, &zc
);
734 if (read_bytes
!= NULL
)
735 *read_bytes
= zc
.zc_cookie
;
737 if (errflags
!= NULL
)
738 *errflags
= zc
.zc_obj
;
740 if (action_handle
!= NULL
)
741 *action_handle
= zc
.zc_action_handle
;
744 VERIFY0(nvlist_unpack(
745 (void *)(uintptr_t)zc
.zc_nvlist_dst
,
746 zc
.zc_nvlist_dst_size
, errors
, KM_SLEEP
));
750 fnvlist_pack_free(packed
, size
);
751 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
758 * The simplest receive case: receive from the specified fd, creating the
759 * specified snapshot. Apply the specified properties as "received" properties
760 * (which can be overridden by locally-set properties). If the stream is a
761 * clone, its origin snapshot must be specified by 'origin'. The 'force'
762 * flag will cause the target filesystem to be rolled back or destroyed if
763 * necessary to receive.
765 * Return 0 on success or an errno on failure.
767 * Note: this interface does not work on dedup'd streams
768 * (those with DMU_BACKUP_FEATURE_DEDUP).
771 lzc_receive(const char *snapname
, nvlist_t
*props
, const char *origin
,
772 boolean_t force
, int fd
)
774 return (recv_impl(snapname
, props
, NULL
, origin
, force
, B_FALSE
, fd
,
775 NULL
, -1, NULL
, NULL
, NULL
, NULL
));
779 * Like lzc_receive, but if the receive fails due to premature stream
780 * termination, the intermediate state will be preserved on disk. In this
781 * case, ECKSUM will be returned. The receive may subsequently be resumed
782 * with a resuming send stream generated by lzc_send_resume().
785 lzc_receive_resumable(const char *snapname
, nvlist_t
*props
, const char *origin
,
786 boolean_t force
, int fd
)
788 return (recv_impl(snapname
, props
, NULL
, origin
, force
, B_TRUE
, fd
,
789 NULL
, -1, NULL
, NULL
, NULL
, NULL
));
793 * Like lzc_receive, but allows the caller to read the begin record and then to
794 * pass it in. That could be useful if the caller wants to derive, for example,
795 * the snapname or the origin parameters based on the information contained in
797 * The begin record must be in its original form as read from the stream,
798 * in other words, it should not be byteswapped.
800 * The 'resumable' parameter allows to obtain the same behavior as with
801 * lzc_receive_resumable.
804 lzc_receive_with_header(const char *snapname
, nvlist_t
*props
,
805 const char *origin
, boolean_t force
, boolean_t resumable
, int fd
,
806 const dmu_replay_record_t
*begin_record
)
808 if (begin_record
== NULL
)
810 return (recv_impl(snapname
, props
, NULL
, origin
, force
, resumable
, fd
,
811 begin_record
, -1, NULL
, NULL
, NULL
, NULL
));
815 * Like lzc_receive, but allows the caller to pass all supported arguments
816 * and retrieve all values returned. The only additional input parameter
817 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
819 * The following parameters all provide return values. Several may be set
820 * in the failure case and will contain additional information.
822 * The 'read_bytes' value will be set to the total number of bytes read.
824 * The 'errflags' value will contain zprop_errflags_t flags which are
825 * used to describe any failures.
827 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
828 * It should be set to zero on first call and will contain an updated handle
829 * on success, it should be passed in subsequent calls.
831 * The 'errors' nvlist contains an entry for each unapplied received
832 * property. Callers are responsible for freeing this nvlist.
834 int lzc_receive_one(const char *snapname
, nvlist_t
*props
,
835 const char *origin
, boolean_t force
, boolean_t resumable
, int input_fd
,
836 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
837 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
840 return (recv_impl(snapname
, props
, NULL
, origin
, force
, resumable
,
841 input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
842 action_handle
, errors
));
846 * Like lzc_receive_one, but allows the caller to pass an additional 'cmdprops'
849 * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
850 * exclude ('zfs receive -x') properties. Callers are responsible for freeing
853 int lzc_receive_with_cmdprops(const char *snapname
, nvlist_t
*props
,
854 nvlist_t
*cmdprops
, const char *origin
, boolean_t force
,
855 boolean_t resumable
, int input_fd
, const dmu_replay_record_t
*begin_record
,
856 int cleanup_fd
, uint64_t *read_bytes
, uint64_t *errflags
,
857 uint64_t *action_handle
, nvlist_t
**errors
)
859 return (recv_impl(snapname
, props
, cmdprops
, origin
, force
, resumable
,
860 input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
861 action_handle
, errors
));
865 * Roll back this filesystem or volume to its most recent snapshot.
866 * If snapnamebuf is not NULL, it will be filled in with the name
867 * of the most recent snapshot.
869 * Return 0 on success or an errno on failure.
872 lzc_rollback(const char *fsname
, char *snapnamebuf
, int snapnamelen
)
878 args
= fnvlist_alloc();
879 err
= lzc_ioctl(ZFS_IOC_ROLLBACK
, fsname
, args
, &result
);
881 if (err
== 0 && snapnamebuf
!= NULL
) {
882 const char *snapname
= fnvlist_lookup_string(result
, "target");
883 (void) strlcpy(snapnamebuf
, snapname
, snapnamelen
);
893 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
894 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
895 * snapshots must be in the same pool.
897 * The returned results nvlist will have an entry for each bookmark that failed.
898 * The value will be the (int32) error code.
900 * The return value will be 0 if all bookmarks were created, otherwise it will
901 * be the errno of a (undetermined) bookmarks that failed.
904 lzc_bookmark(nvlist_t
*bookmarks
, nvlist_t
**errlist
)
908 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
910 /* determine the pool name */
911 elem
= nvlist_next_nvpair(bookmarks
, NULL
);
914 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
915 pool
[strcspn(pool
, "/#")] = '\0';
917 error
= lzc_ioctl(ZFS_IOC_BOOKMARK
, pool
, bookmarks
, errlist
);
923 * Retrieve bookmarks.
925 * Retrieve the list of bookmarks for the given file system. The props
926 * parameter is an nvlist of property names (with no values) that will be
927 * returned for each bookmark.
929 * The following are valid properties on bookmarks, all of which are numbers
930 * (represented as uint64 in the nvlist)
932 * "guid" - globally unique identifier of the snapshot it refers to
933 * "createtxg" - txg when the snapshot it refers to was created
934 * "creation" - timestamp when the snapshot it refers to was created
936 * The format of the returned nvlist as follows:
937 * <short name of bookmark> -> {
938 * <name of property> -> {
944 lzc_get_bookmarks(const char *fsname
, nvlist_t
*props
, nvlist_t
**bmarks
)
946 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS
, fsname
, props
, bmarks
));
950 * Destroys bookmarks.
952 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
953 * They must all be in the same pool. Bookmarks are specified as
956 * Bookmarks that do not exist will be silently ignored.
958 * The return value will be 0 if all bookmarks that existed were destroyed.
960 * Otherwise the return value will be the errno of a (undetermined) bookmark
961 * that failed, no bookmarks will be destroyed, and the errlist will have an
962 * entry for each bookmarks that failed. The value in the errlist will be
963 * the (int32) error code.
966 lzc_destroy_bookmarks(nvlist_t
*bmarks
, nvlist_t
**errlist
)
970 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
972 /* determine the pool name */
973 elem
= nvlist_next_nvpair(bmarks
, NULL
);
976 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
977 pool
[strcspn(pool
, "/#")] = '\0';
979 error
= lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS
, pool
, bmarks
, errlist
);