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.
30 * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
31 * It has the following characteristics:
33 * - Thread Safe. libzfs_core is accessible concurrently from multiple
34 * threads. This is accomplished primarily by avoiding global data
35 * (e.g. caching). Since it's thread-safe, there is no reason for a
36 * process to have multiple libzfs "instances". Therefore, we store
37 * our few pieces of data (e.g. the file descriptor) in global
38 * variables. The fd is reference-counted so that the libzfs_core
39 * library can be "initialized" multiple times (e.g. by different
40 * consumers within the same process).
42 * - Committed Interface. The libzfs_core interface will be committed,
43 * therefore consumers can compile against it and be confident that
44 * their code will continue to work on future releases of this code.
45 * Currently, the interface is Evolving (not Committed), but we intend
46 * to commit to it once it is more complete and we determine that it
47 * meets the needs of all consumers.
49 * - Programmatic Error Handling. libzfs_core communicates errors with
50 * defined error numbers, and doesn't print anything to stdout/stderr.
52 * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
53 * to/from the kernel ioctls. There is generally a 1:1 correspondence
54 * between libzfs_core functions and ioctls to /dev/zfs.
56 * - Clear Atomicity. Because libzfs_core functions are generally 1:1
57 * with kernel ioctls, and kernel ioctls are general atomic, each
58 * libzfs_core function is atomic. For example, creating multiple
59 * snapshots with a single call to lzc_snapshot() is atomic -- it
60 * can't fail with only some of the requested snapshots created, even
61 * in the event of power loss or system crash.
63 * - Continued libzfs Support. Some higher-level operations (e.g.
64 * support for "zfs send -R") are too complicated to fit the scope of
65 * libzfs_core. This functionality will continue to live in libzfs.
66 * Where appropriate, libzfs will use the underlying atomic operations
67 * of libzfs_core. For example, libzfs may implement "zfs send -R |
68 * zfs receive" by using individual "send one snapshot", rename,
69 * destroy, and "receive one snapshot" operations in libzfs_core.
70 * /sbin/zfs and /zbin/zpool will link with both libzfs and
71 * libzfs_core. Other consumers should aim to use only libzfs_core,
72 * since that will be the supported, stable interface going forwards.
75 #include <libzfs_core.h>
83 #include <sys/nvpair.h>
84 #include <sys/param.h>
85 #include <sys/types.h>
87 #include <sys/zfs_ioctl.h>
90 static pthread_mutex_t g_lock
= PTHREAD_MUTEX_INITIALIZER
;
91 static int g_refcount
;
94 libzfs_core_init(void)
96 (void) pthread_mutex_lock(&g_lock
);
97 if (g_refcount
== 0) {
98 g_fd
= open("/dev/zfs", O_RDWR
);
100 (void) pthread_mutex_unlock(&g_lock
);
105 (void) pthread_mutex_unlock(&g_lock
);
110 libzfs_core_fini(void)
112 (void) pthread_mutex_lock(&g_lock
);
113 ASSERT3S(g_refcount
, >, 0);
118 if (g_refcount
== 0 && g_fd
!= -1) {
122 (void) pthread_mutex_unlock(&g_lock
);
126 lzc_ioctl(zfs_ioc_t ioc
, const char *name
,
127 nvlist_t
*source
, nvlist_t
**resultp
)
129 zfs_cmd_t zc
= {"\0"};
134 ASSERT3S(g_refcount
, >, 0);
135 VERIFY3S(g_fd
, !=, -1);
138 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
140 if (source
!= NULL
) {
141 packed
= fnvlist_pack(source
, &size
);
142 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
143 zc
.zc_nvlist_src_size
= size
;
146 if (resultp
!= NULL
) {
148 zc
.zc_nvlist_dst_size
= MAX(size
* 2, 128 * 1024);
149 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
150 malloc(zc
.zc_nvlist_dst_size
);
151 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
157 while (ioctl(g_fd
, ioc
, &zc
) != 0) {
158 if (errno
== ENOMEM
&& resultp
!= NULL
) {
159 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
160 zc
.zc_nvlist_dst_size
*= 2;
161 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
162 malloc(zc
.zc_nvlist_dst_size
);
163 if (zc
.zc_nvlist_dst
== (uint64_t)0) {
172 if (zc
.zc_nvlist_dst_filled
) {
173 *resultp
= fnvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
174 zc
.zc_nvlist_dst_size
);
178 fnvlist_pack_free(packed
, size
);
179 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
184 lzc_create(const char *fsname
, enum lzc_dataset_type type
, nvlist_t
*props
)
187 nvlist_t
*args
= fnvlist_alloc();
188 fnvlist_add_int32(args
, "type", (dmu_objset_type_t
)type
);
190 fnvlist_add_nvlist(args
, "props", props
);
191 error
= lzc_ioctl(ZFS_IOC_CREATE
, fsname
, args
, NULL
);
197 lzc_clone(const char *fsname
, const char *origin
,
201 nvlist_t
*args
= fnvlist_alloc();
202 fnvlist_add_string(args
, "origin", origin
);
204 fnvlist_add_nvlist(args
, "props", props
);
205 error
= lzc_ioctl(ZFS_IOC_CLONE
, fsname
, args
, NULL
);
211 lzc_promote(const char *fsname
, char *snapnamebuf
, int snapnamelen
)
214 * The promote ioctl is still legacy, so we need to construct our
215 * own zfs_cmd_t rather than using lzc_ioctl().
217 zfs_cmd_t zc
= { "\0" };
219 ASSERT3S(g_refcount
, >, 0);
220 VERIFY3S(g_fd
, !=, -1);
222 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_name
));
223 if (ioctl(g_fd
, ZFS_IOC_PROMOTE
, &zc
) != 0) {
225 if (error
== EEXIST
&& snapnamebuf
!= NULL
)
226 (void) strlcpy(snapnamebuf
, zc
.zc_string
, snapnamelen
);
235 * The keys in the snaps nvlist are the snapshots to be created.
236 * They must all be in the same pool.
238 * The props nvlist is properties to set. Currently only user properties
239 * are supported. { user:prop_name -> string value }
241 * The returned results nvlist will have an entry for each snapshot that failed.
242 * The value will be the (int32) error code.
244 * The return value will be 0 if all snapshots were created, otherwise it will
245 * be the errno of a (unspecified) snapshot that failed.
248 lzc_snapshot(nvlist_t
*snaps
, nvlist_t
*props
, nvlist_t
**errlist
)
253 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
257 /* determine the pool name */
258 elem
= nvlist_next_nvpair(snaps
, NULL
);
261 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
262 pool
[strcspn(pool
, "/@")] = '\0';
264 args
= fnvlist_alloc();
265 fnvlist_add_nvlist(args
, "snaps", snaps
);
267 fnvlist_add_nvlist(args
, "props", props
);
269 error
= lzc_ioctl(ZFS_IOC_SNAPSHOT
, pool
, args
, errlist
);
276 * Destroys snapshots.
278 * The keys in the snaps nvlist are the snapshots to be destroyed.
279 * They must all be in the same pool.
281 * Snapshots that do not exist will be silently ignored.
283 * If 'defer' is not set, and a snapshot has user holds or clones, the
284 * destroy operation will fail and none of the snapshots will be
287 * If 'defer' is set, and a snapshot has user holds or clones, it will be
288 * marked for deferred destruction, and will be destroyed when the last hold
289 * or clone is removed/destroyed.
291 * The return value will be 0 if all snapshots were destroyed (or marked for
292 * later destruction if 'defer' is set) or didn't exist to begin with.
294 * Otherwise the return value will be the errno of a (unspecified) snapshot
295 * that failed, no snapshots will be destroyed, and the errlist will have an
296 * entry for each snapshot that failed. The value in the errlist will be
297 * the (int32) error code.
300 lzc_destroy_snaps(nvlist_t
*snaps
, boolean_t defer
, nvlist_t
**errlist
)
305 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
307 /* determine the pool name */
308 elem
= nvlist_next_nvpair(snaps
, NULL
);
311 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
312 pool
[strcspn(pool
, "/@")] = '\0';
314 args
= fnvlist_alloc();
315 fnvlist_add_nvlist(args
, "snaps", snaps
);
317 fnvlist_add_boolean(args
, "defer");
319 error
= lzc_ioctl(ZFS_IOC_DESTROY_SNAPS
, pool
, args
, errlist
);
326 lzc_snaprange_space(const char *firstsnap
, const char *lastsnap
,
332 char fs
[ZFS_MAX_DATASET_NAME_LEN
];
335 /* determine the fs name */
336 (void) strlcpy(fs
, firstsnap
, sizeof (fs
));
337 atp
= strchr(fs
, '@');
342 args
= fnvlist_alloc();
343 fnvlist_add_string(args
, "firstsnap", firstsnap
);
345 err
= lzc_ioctl(ZFS_IOC_SPACE_SNAPS
, lastsnap
, args
, &result
);
348 *usedp
= fnvlist_lookup_uint64(result
, "used");
349 fnvlist_free(result
);
355 lzc_exists(const char *dataset
)
358 * The objset_stats ioctl is still legacy, so we need to construct our
359 * own zfs_cmd_t rather than using lzc_ioctl().
361 zfs_cmd_t zc
= {"\0"};
363 ASSERT3S(g_refcount
, >, 0);
364 VERIFY3S(g_fd
, !=, -1);
366 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
367 return (ioctl(g_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) == 0);
372 * It was added to preserve the function signature in case it is
373 * needed in the future.
377 lzc_sync(const char *pool_name
, nvlist_t
*innvl
, nvlist_t
**outnvl
)
379 return (lzc_ioctl(ZFS_IOC_POOL_SYNC
, pool_name
, innvl
, NULL
));
383 * Create "user holds" on snapshots. If there is a hold on a snapshot,
384 * the snapshot can not be destroyed. (However, it can be marked for deletion
385 * by lzc_destroy_snaps(defer=B_TRUE).)
387 * The keys in the nvlist are snapshot names.
388 * The snapshots must all be in the same pool.
389 * The value is the name of the hold (string type).
391 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
392 * In this case, when the cleanup_fd is closed (including on process
393 * termination), the holds will be released. If the system is shut down
394 * uncleanly, the holds will be released when the pool is next opened
397 * Holds for snapshots which don't exist will be skipped and have an entry
398 * added to errlist, but will not cause an overall failure.
400 * The return value will be 0 if all holds, for snapshots that existed,
401 * were successfully created.
403 * Otherwise the return value will be the errno of a (unspecified) hold that
404 * failed and no holds will be created.
406 * In all cases the errlist will have an entry for each hold that failed
407 * (name = snapshot), with its value being the error code (int32).
410 lzc_hold(nvlist_t
*holds
, int cleanup_fd
, nvlist_t
**errlist
)
412 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
417 /* determine the pool name */
418 elem
= nvlist_next_nvpair(holds
, NULL
);
421 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
422 pool
[strcspn(pool
, "/@")] = '\0';
424 args
= fnvlist_alloc();
425 fnvlist_add_nvlist(args
, "holds", holds
);
426 if (cleanup_fd
!= -1)
427 fnvlist_add_int32(args
, "cleanup_fd", cleanup_fd
);
429 error
= lzc_ioctl(ZFS_IOC_HOLD
, pool
, args
, errlist
);
435 * Release "user holds" on snapshots. If the snapshot has been marked for
436 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
437 * any clones, and all the user holds are removed, then the snapshot will be
440 * The keys in the nvlist are snapshot names.
441 * The snapshots must all be in the same pool.
442 * The value is an nvlist whose keys are the holds to remove.
444 * Holds which failed to release because they didn't exist will have an entry
445 * added to errlist, but will not cause an overall failure.
447 * The return value will be 0 if the nvl holds was empty or all holds that
448 * existed, were successfully removed.
450 * Otherwise the return value will be the errno of a (unspecified) hold that
451 * failed to release and no holds will be released.
453 * In all cases the errlist will have an entry for each hold that failed to
457 lzc_release(nvlist_t
*holds
, nvlist_t
**errlist
)
459 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
462 /* determine the pool name */
463 elem
= nvlist_next_nvpair(holds
, NULL
);
466 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
467 pool
[strcspn(pool
, "/@")] = '\0';
469 return (lzc_ioctl(ZFS_IOC_RELEASE
, pool
, holds
, errlist
));
473 * Retrieve list of user holds on the specified snapshot.
475 * On success, *holdsp will be set to an nvlist which the caller must free.
476 * The keys are the names of the holds, and the value is the creation time
477 * of the hold (uint64) in seconds since the epoch.
480 lzc_get_holds(const char *snapname
, nvlist_t
**holdsp
)
482 return (lzc_ioctl(ZFS_IOC_GET_HOLDS
, snapname
, NULL
, holdsp
));
486 * Generate a zfs send stream for the specified snapshot and write it to
487 * the specified file descriptor.
489 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
491 * If "from" is NULL, a full (non-incremental) stream will be sent.
492 * If "from" is non-NULL, it must be the full name of a snapshot or
493 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
494 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
495 * bookmark must represent an earlier point in the history of "snapname").
496 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
497 * or it can be the origin of "snapname"'s filesystem, or an earlier
498 * snapshot in the origin, etc.
500 * "fd" is the file descriptor to write the send stream to.
502 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
503 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
504 * records with drr_blksz > 128K.
506 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
507 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
508 * which the receiving system must support (as indicated by support
509 * for the "embedded_data" feature).
512 lzc_send(const char *snapname
, const char *from
, int fd
,
513 enum lzc_send_flags flags
)
515 return (lzc_send_resume(snapname
, from
, fd
, flags
, 0, 0));
519 lzc_send_resume(const char *snapname
, const char *from
, int fd
,
520 enum lzc_send_flags flags
, uint64_t resumeobj
, uint64_t resumeoff
)
525 args
= fnvlist_alloc();
526 fnvlist_add_int32(args
, "fd", fd
);
528 fnvlist_add_string(args
, "fromsnap", from
);
529 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
530 fnvlist_add_boolean(args
, "largeblockok");
531 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
532 fnvlist_add_boolean(args
, "embedok");
533 if (flags
& LZC_SEND_FLAG_COMPRESS
)
534 fnvlist_add_boolean(args
, "compressok");
535 if (resumeobj
!= 0 || resumeoff
!= 0) {
536 fnvlist_add_uint64(args
, "resume_object", resumeobj
);
537 fnvlist_add_uint64(args
, "resume_offset", resumeoff
);
539 err
= lzc_ioctl(ZFS_IOC_SEND_NEW
, snapname
, args
, NULL
);
545 * "from" can be NULL, a snapshot, or a bookmark.
547 * If from is NULL, a full (non-incremental) stream will be estimated. This
548 * is calculated very efficiently.
550 * If from is a snapshot, lzc_send_space uses the deadlists attached to
551 * each snapshot to efficiently estimate the stream size.
553 * If from is a bookmark, the indirect blocks in the destination snapshot
554 * are traversed, looking for blocks with a birth time since the creation TXG of
555 * the snapshot this bookmark was created from. This will result in
556 * significantly more I/O and be less efficient than a send space estimation on
557 * an equivalent snapshot.
560 lzc_send_space(const char *snapname
, const char *from
,
561 enum lzc_send_flags flags
, uint64_t *spacep
)
567 args
= fnvlist_alloc();
569 fnvlist_add_string(args
, "from", from
);
570 if (flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
571 fnvlist_add_boolean(args
, "largeblockok");
572 if (flags
& LZC_SEND_FLAG_EMBED_DATA
)
573 fnvlist_add_boolean(args
, "embedok");
574 if (flags
& LZC_SEND_FLAG_COMPRESS
)
575 fnvlist_add_boolean(args
, "compressok");
576 err
= lzc_ioctl(ZFS_IOC_SEND_SPACE
, snapname
, args
, &result
);
579 *spacep
= fnvlist_lookup_uint64(result
, "space");
585 recv_read(int fd
, void *buf
, int ilen
)
592 rv
= read(fd
, cp
, len
);
597 if (rv
< 0 || len
!= 0)
604 * Linux adds ZFS_IOC_RECV_NEW for resumable streams and preserves the legacy
605 * ZFS_IOC_RECV user/kernel interface. The new interface supports all stream
606 * options but is currently only used for resumable streams. This way updated
607 * user space utilities will interoperate with older kernel modules.
609 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
612 recv_impl(const char *snapname
, nvlist_t
*recvdprops
, nvlist_t
*localprops
,
613 const char *origin
, boolean_t force
, boolean_t resumable
, int input_fd
,
614 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
615 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
618 dmu_replay_record_t drr
;
619 char fsname
[MAXPATHLEN
];
623 ASSERT3S(g_refcount
, >, 0);
624 VERIFY3S(g_fd
, !=, -1);
626 /* Set 'fsname' to the name of containing filesystem */
627 (void) strlcpy(fsname
, snapname
, sizeof (fsname
));
628 atp
= strchr(fsname
, '@');
633 /* If the fs does not exist, try its parent. */
634 if (!lzc_exists(fsname
)) {
635 char *slashp
= strrchr(fsname
, '/');
642 * The begin_record is normally a non-byteswapped BEGIN record.
643 * For resumable streams it may be set to any non-byteswapped
644 * dmu_replay_record_t.
646 if (begin_record
== NULL
) {
647 error
= recv_read(input_fd
, &drr
, sizeof (drr
));
655 nvlist_t
*outnvl
= NULL
;
656 nvlist_t
*innvl
= fnvlist_alloc();
658 fnvlist_add_string(innvl
, "snapname", snapname
);
660 if (recvdprops
!= NULL
)
661 fnvlist_add_nvlist(innvl
, "props", recvdprops
);
663 if (localprops
!= NULL
)
664 fnvlist_add_nvlist(innvl
, "localprops", localprops
);
666 if (origin
!= NULL
&& strlen(origin
))
667 fnvlist_add_string(innvl
, "origin", origin
);
669 fnvlist_add_byte_array(innvl
, "begin_record",
670 (uchar_t
*)&drr
, sizeof (drr
));
672 fnvlist_add_int32(innvl
, "input_fd", input_fd
);
675 fnvlist_add_boolean(innvl
, "force");
678 fnvlist_add_boolean(innvl
, "resumable");
681 fnvlist_add_int32(innvl
, "cleanup_fd", cleanup_fd
);
683 if (action_handle
!= NULL
)
684 fnvlist_add_uint64(innvl
, "action_handle",
687 error
= lzc_ioctl(ZFS_IOC_RECV_NEW
, fsname
, innvl
, &outnvl
);
689 if (error
== 0 && read_bytes
!= NULL
)
690 error
= nvlist_lookup_uint64(outnvl
, "read_bytes",
693 if (error
== 0 && errflags
!= NULL
)
694 error
= nvlist_lookup_uint64(outnvl
, "error_flags",
697 if (error
== 0 && action_handle
!= NULL
)
698 error
= nvlist_lookup_uint64(outnvl
, "action_handle",
701 if (error
== 0 && errors
!= NULL
) {
703 error
= nvlist_lookup_nvlist(outnvl
, "errors", &nvl
);
705 *errors
= fnvlist_dup(nvl
);
709 fnvlist_free(outnvl
);
711 zfs_cmd_t zc
= {"\0"};
715 ASSERT3S(g_refcount
, >, 0);
717 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_value
));
718 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
720 if (recvdprops
!= NULL
) {
721 packed
= fnvlist_pack(recvdprops
, &size
);
722 zc
.zc_nvlist_src
= (uint64_t)(uintptr_t)packed
;
723 zc
.zc_nvlist_src_size
= size
;
726 if (localprops
!= NULL
) {
727 packed
= fnvlist_pack(localprops
, &size
);
728 zc
.zc_nvlist_conf
= (uint64_t)(uintptr_t)packed
;
729 zc
.zc_nvlist_conf_size
= size
;
733 (void) strlcpy(zc
.zc_string
, origin
,
734 sizeof (zc
.zc_string
));
736 ASSERT3S(drr
.drr_type
, ==, DRR_BEGIN
);
737 zc
.zc_begin_record
= drr
.drr_u
.drr_begin
;
739 zc
.zc_cookie
= input_fd
;
740 zc
.zc_cleanup_fd
= -1;
741 zc
.zc_action_handle
= 0;
744 zc
.zc_cleanup_fd
= cleanup_fd
;
746 if (action_handle
!= NULL
)
747 zc
.zc_action_handle
= *action_handle
;
749 zc
.zc_nvlist_dst_size
= 128 * 1024;
750 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)
751 malloc(zc
.zc_nvlist_dst_size
);
753 error
= ioctl(g_fd
, ZFS_IOC_RECV
, &zc
);
757 if (read_bytes
!= NULL
)
758 *read_bytes
= zc
.zc_cookie
;
760 if (errflags
!= NULL
)
761 *errflags
= zc
.zc_obj
;
763 if (action_handle
!= NULL
)
764 *action_handle
= zc
.zc_action_handle
;
767 VERIFY0(nvlist_unpack(
768 (void *)(uintptr_t)zc
.zc_nvlist_dst
,
769 zc
.zc_nvlist_dst_size
, errors
, KM_SLEEP
));
773 fnvlist_pack_free(packed
, size
);
774 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
781 * The simplest receive case: receive from the specified fd, creating the
782 * specified snapshot. Apply the specified properties as "received" properties
783 * (which can be overridden by locally-set properties). If the stream is a
784 * clone, its origin snapshot must be specified by 'origin'. The 'force'
785 * flag will cause the target filesystem to be rolled back or destroyed if
786 * necessary to receive.
788 * Return 0 on success or an errno on failure.
790 * Note: this interface does not work on dedup'd streams
791 * (those with DMU_BACKUP_FEATURE_DEDUP).
794 lzc_receive(const char *snapname
, nvlist_t
*props
, const char *origin
,
795 boolean_t force
, int fd
)
797 return (recv_impl(snapname
, props
, NULL
, origin
, force
, B_FALSE
, fd
,
798 NULL
, -1, NULL
, NULL
, NULL
, NULL
));
802 * Like lzc_receive, but if the receive fails due to premature stream
803 * termination, the intermediate state will be preserved on disk. In this
804 * case, ECKSUM will be returned. The receive may subsequently be resumed
805 * with a resuming send stream generated by lzc_send_resume().
808 lzc_receive_resumable(const char *snapname
, nvlist_t
*props
, const char *origin
,
809 boolean_t force
, int fd
)
811 return (recv_impl(snapname
, props
, NULL
, origin
, force
, B_TRUE
, fd
,
812 NULL
, -1, NULL
, NULL
, NULL
, NULL
));
816 * Like lzc_receive, but allows the caller to read the begin record and then to
817 * pass it in. That could be useful if the caller wants to derive, for example,
818 * the snapname or the origin parameters based on the information contained in
820 * The begin record must be in its original form as read from the stream,
821 * in other words, it should not be byteswapped.
823 * The 'resumable' parameter allows to obtain the same behavior as with
824 * lzc_receive_resumable.
827 lzc_receive_with_header(const char *snapname
, nvlist_t
*props
,
828 const char *origin
, boolean_t force
, boolean_t resumable
, int fd
,
829 const dmu_replay_record_t
*begin_record
)
831 if (begin_record
== NULL
)
833 return (recv_impl(snapname
, props
, NULL
, origin
, force
, resumable
, fd
,
834 begin_record
, -1, NULL
, NULL
, NULL
, NULL
));
838 * Like lzc_receive, but allows the caller to pass all supported arguments
839 * and retrieve all values returned. The only additional input parameter
840 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
842 * The following parameters all provide return values. Several may be set
843 * in the failure case and will contain additional information.
845 * The 'read_bytes' value will be set to the total number of bytes read.
847 * The 'errflags' value will contain zprop_errflags_t flags which are
848 * used to describe any failures.
850 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
851 * It should be set to zero on first call and will contain an updated handle
852 * on success, it should be passed in subsequent calls.
854 * The 'errors' nvlist contains an entry for each unapplied received
855 * property. Callers are responsible for freeing this nvlist.
857 int lzc_receive_one(const char *snapname
, nvlist_t
*props
,
858 const char *origin
, boolean_t force
, boolean_t resumable
, int input_fd
,
859 const dmu_replay_record_t
*begin_record
, int cleanup_fd
,
860 uint64_t *read_bytes
, uint64_t *errflags
, uint64_t *action_handle
,
863 return (recv_impl(snapname
, props
, NULL
, origin
, force
, resumable
,
864 input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
865 action_handle
, errors
));
869 * Like lzc_receive_one, but allows the caller to pass an additional 'cmdprops'
872 * The 'cmdprops' nvlist contains both override ('zfs receive -o') and
873 * exclude ('zfs receive -x') properties. Callers are responsible for freeing
876 int lzc_receive_with_cmdprops(const char *snapname
, nvlist_t
*props
,
877 nvlist_t
*cmdprops
, const char *origin
, boolean_t force
,
878 boolean_t resumable
, int input_fd
, const dmu_replay_record_t
*begin_record
,
879 int cleanup_fd
, uint64_t *read_bytes
, uint64_t *errflags
,
880 uint64_t *action_handle
, nvlist_t
**errors
)
882 return (recv_impl(snapname
, props
, cmdprops
, origin
, force
, resumable
,
883 input_fd
, begin_record
, cleanup_fd
, read_bytes
, errflags
,
884 action_handle
, errors
));
888 * Roll back this filesystem or volume to its most recent snapshot.
889 * If snapnamebuf is not NULL, it will be filled in with the name
890 * of the most recent snapshot.
891 * Note that the latest snapshot may change if a new one is concurrently
892 * created or the current one is destroyed. lzc_rollback_to can be used
893 * to roll back to a specific latest snapshot.
895 * Return 0 on success or an errno on failure.
898 lzc_rollback(const char *fsname
, char *snapnamebuf
, int snapnamelen
)
904 args
= fnvlist_alloc();
905 err
= lzc_ioctl(ZFS_IOC_ROLLBACK
, fsname
, args
, &result
);
907 if (err
== 0 && snapnamebuf
!= NULL
) {
908 const char *snapname
= fnvlist_lookup_string(result
, "target");
909 (void) strlcpy(snapnamebuf
, snapname
, snapnamelen
);
917 * Roll back this filesystem or volume to the specified snapshot,
920 * Return 0 on success or an errno on failure.
923 lzc_rollback_to(const char *fsname
, const char *snapname
)
929 args
= fnvlist_alloc();
930 fnvlist_add_string(args
, "target", snapname
);
931 err
= lzc_ioctl(ZFS_IOC_ROLLBACK
, fsname
, args
, &result
);
940 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
941 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
942 * snapshots must be in the same pool.
944 * The returned results nvlist will have an entry for each bookmark that failed.
945 * The value will be the (int32) error code.
947 * The return value will be 0 if all bookmarks were created, otherwise it will
948 * be the errno of a (undetermined) bookmarks that failed.
951 lzc_bookmark(nvlist_t
*bookmarks
, nvlist_t
**errlist
)
955 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
957 /* determine the pool name */
958 elem
= nvlist_next_nvpair(bookmarks
, NULL
);
961 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
962 pool
[strcspn(pool
, "/#")] = '\0';
964 error
= lzc_ioctl(ZFS_IOC_BOOKMARK
, pool
, bookmarks
, errlist
);
970 * Retrieve bookmarks.
972 * Retrieve the list of bookmarks for the given file system. The props
973 * parameter is an nvlist of property names (with no values) that will be
974 * returned for each bookmark.
976 * The following are valid properties on bookmarks, all of which are numbers
977 * (represented as uint64 in the nvlist)
979 * "guid" - globally unique identifier of the snapshot it refers to
980 * "createtxg" - txg when the snapshot it refers to was created
981 * "creation" - timestamp when the snapshot it refers to was created
983 * The format of the returned nvlist as follows:
984 * <short name of bookmark> -> {
985 * <name of property> -> {
991 lzc_get_bookmarks(const char *fsname
, nvlist_t
*props
, nvlist_t
**bmarks
)
993 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS
, fsname
, props
, bmarks
));
997 * Destroys bookmarks.
999 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
1000 * They must all be in the same pool. Bookmarks are specified as
1003 * Bookmarks that do not exist will be silently ignored.
1005 * The return value will be 0 if all bookmarks that existed were destroyed.
1007 * Otherwise the return value will be the errno of a (undetermined) bookmark
1008 * that failed, no bookmarks will be destroyed, and the errlist will have an
1009 * entry for each bookmarks that failed. The value in the errlist will be
1010 * the (int32) error code.
1013 lzc_destroy_bookmarks(nvlist_t
*bmarks
, nvlist_t
**errlist
)
1017 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
1019 /* determine the pool name */
1020 elem
= nvlist_next_nvpair(bmarks
, NULL
);
1023 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
1024 pool
[strcspn(pool
, "/#")] = '\0';
1026 error
= lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS
, pool
, bmarks
, errlist
);