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);
115 (void) pthread_mutex_unlock(&g_lock);
119 lzc_ioctl(zfs_ioc_t ioc, const char *name,
120 nvlist_t *source, nvlist_t **resultp)
122 zfs_cmd_t zc = {"\0"};
127 ASSERT3S(g_refcount, >, 0);
129 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
131 packed = fnvlist_pack(source, &size);
132 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
133 zc.zc_nvlist_src_size = size;
135 if (resultp != NULL) {
137 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
138 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
139 malloc(zc.zc_nvlist_dst_size);
140 if (zc.zc_nvlist_dst == (uint64_t)0) {
146 while (ioctl(g_fd, ioc, &zc) != 0) {
147 if (errno == ENOMEM && resultp != NULL) {
148 free((void *)(uintptr_t)zc.zc_nvlist_dst);
149 zc.zc_nvlist_dst_size *= 2;
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) {
161 if (zc.zc_nvlist_dst_filled) {
162 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
163 zc.zc_nvlist_dst_size);
167 fnvlist_pack_free(packed, size);
168 free((void *)(uintptr_t)zc.zc_nvlist_dst);
173 lzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
176 nvlist_t *args = fnvlist_alloc();
177 fnvlist_add_int32(args, "type", type);
179 fnvlist_add_nvlist(args, "props", props);
180 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
186 lzc_clone(const char *fsname, const char *origin,
190 nvlist_t *args = fnvlist_alloc();
191 fnvlist_add_string(args, "origin", origin);
193 fnvlist_add_nvlist(args, "props", props);
194 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
202 * The keys in the snaps nvlist are the snapshots to be created.
203 * They must all be in the same pool.
205 * The props nvlist is properties to set. Currently only user properties
206 * are supported. { user:prop_name -> string value }
208 * The returned results nvlist will have an entry for each snapshot that failed.
209 * The value will be the (int32) error code.
211 * The return value will be 0 if all snapshots were created, otherwise it will
212 * be the errno of a (unspecified) snapshot that failed.
215 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
220 char pool[ZFS_MAX_DATASET_NAME_LEN];
224 /* determine the pool name */
225 elem = nvlist_next_nvpair(snaps, NULL);
228 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
229 pool[strcspn(pool, "/@")] = '\0';
231 args = fnvlist_alloc();
232 fnvlist_add_nvlist(args, "snaps", snaps);
234 fnvlist_add_nvlist(args, "props", props);
236 error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
243 * Destroys snapshots.
245 * The keys in the snaps nvlist are the snapshots to be destroyed.
246 * They must all be in the same pool.
248 * Snapshots that do not exist will be silently ignored.
250 * If 'defer' is not set, and a snapshot has user holds or clones, the
251 * destroy operation will fail and none of the snapshots will be
254 * If 'defer' is set, and a snapshot has user holds or clones, it will be
255 * marked for deferred destruction, and will be destroyed when the last hold
256 * or clone is removed/destroyed.
258 * The return value will be 0 if all snapshots were destroyed (or marked for
259 * later destruction if 'defer' is set) or didn't exist to begin with.
261 * Otherwise the return value will be the errno of a (unspecified) snapshot
262 * that failed, no snapshots will be destroyed, and the errlist will have an
263 * entry for each snapshot that failed. The value in the errlist will be
264 * the (int32) error code.
267 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
272 char pool[ZFS_MAX_DATASET_NAME_LEN];
274 /* determine the pool name */
275 elem = nvlist_next_nvpair(snaps, NULL);
278 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
279 pool[strcspn(pool, "/@")] = '\0';
281 args = fnvlist_alloc();
282 fnvlist_add_nvlist(args, "snaps", snaps);
284 fnvlist_add_boolean(args, "defer");
286 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
293 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
299 char fs[ZFS_MAX_DATASET_NAME_LEN];
302 /* determine the fs name */
303 (void) strlcpy(fs, firstsnap, sizeof (fs));
304 atp = strchr(fs, '@');
309 args = fnvlist_alloc();
310 fnvlist_add_string(args, "firstsnap", firstsnap);
312 err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
315 *usedp = fnvlist_lookup_uint64(result, "used");
316 fnvlist_free(result);
322 lzc_exists(const char *dataset)
325 * The objset_stats ioctl is still legacy, so we need to construct our
326 * own zfs_cmd_t rather than using zfsc_ioctl().
328 zfs_cmd_t zc = {"\0"};
330 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
331 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
335 * Create "user holds" on snapshots. If there is a hold on a snapshot,
336 * the snapshot can not be destroyed. (However, it can be marked for deletion
337 * by lzc_destroy_snaps(defer=B_TRUE).)
339 * The keys in the nvlist are snapshot names.
340 * The snapshots must all be in the same pool.
341 * The value is the name of the hold (string type).
343 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
344 * In this case, when the cleanup_fd is closed (including on process
345 * termination), the holds will be released. If the system is shut down
346 * uncleanly, the holds will be released when the pool is next opened
349 * Holds for snapshots which don't exist will be skipped and have an entry
350 * added to errlist, but will not cause an overall failure.
352 * The return value will be 0 if all holds, for snapshots that existed,
353 * were successfully created.
355 * Otherwise the return value will be the errno of a (unspecified) hold that
356 * failed and no holds will be created.
358 * In all cases the errlist will have an entry for each hold that failed
359 * (name = snapshot), with its value being the error code (int32).
362 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
364 char pool[ZFS_MAX_DATASET_NAME_LEN];
369 /* determine the pool name */
370 elem = nvlist_next_nvpair(holds, NULL);
373 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
374 pool[strcspn(pool, "/@")] = '\0';
376 args = fnvlist_alloc();
377 fnvlist_add_nvlist(args, "holds", holds);
378 if (cleanup_fd != -1)
379 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
381 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
387 * Release "user holds" on snapshots. If the snapshot has been marked for
388 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
389 * any clones, and all the user holds are removed, then the snapshot will be
392 * The keys in the nvlist are snapshot names.
393 * The snapshots must all be in the same pool.
394 * The value is an nvlist whose keys are the holds to remove.
396 * Holds which failed to release because they didn't exist will have an entry
397 * added to errlist, but will not cause an overall failure.
399 * The return value will be 0 if the nvl holds was empty or all holds that
400 * existed, were successfully removed.
402 * Otherwise the return value will be the errno of a (unspecified) hold that
403 * failed to release and no holds will be released.
405 * In all cases the errlist will have an entry for each hold that failed to
409 lzc_release(nvlist_t *holds, nvlist_t **errlist)
411 char pool[ZFS_MAX_DATASET_NAME_LEN];
414 /* determine the pool name */
415 elem = nvlist_next_nvpair(holds, NULL);
418 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
419 pool[strcspn(pool, "/@")] = '\0';
421 return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
425 * Retrieve list of user holds on the specified snapshot.
427 * On success, *holdsp will be set to an nvlist which the caller must free.
428 * The keys are the names of the holds, and the value is the creation time
429 * of the hold (uint64) in seconds since the epoch.
432 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
435 nvlist_t *innvl = fnvlist_alloc();
436 error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
442 * Generate a zfs send stream for the specified snapshot and write it to
443 * the specified file descriptor.
445 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
447 * If "from" is NULL, a full (non-incremental) stream will be sent.
448 * If "from" is non-NULL, it must be the full name of a snapshot or
449 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
450 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
451 * bookmark must represent an earlier point in the history of "snapname").
452 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
453 * or it can be the origin of "snapname"'s filesystem, or an earlier
454 * snapshot in the origin, etc.
456 * "fd" is the file descriptor to write the send stream to.
458 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
459 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
460 * records with drr_blksz > 128K.
462 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
463 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
464 * which the receiving system must support (as indicated by support
465 * for the "embedded_data" feature).
468 lzc_send(const char *snapname, const char *from, int fd,
469 enum lzc_send_flags flags)
471 return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
475 lzc_send_resume(const char *snapname, const char *from, int fd,
476 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
481 args = fnvlist_alloc();
482 fnvlist_add_int32(args, "fd", fd);
484 fnvlist_add_string(args, "fromsnap", from);
485 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
486 fnvlist_add_boolean(args, "largeblockok");
487 if (flags & LZC_SEND_FLAG_COMPRESS)
488 fnvlist_add_boolean(args, "compressok");
489 if (flags & LZC_SEND_FLAG_EMBED_DATA)
490 fnvlist_add_boolean(args, "embedok");
491 if (resumeobj != 0 || resumeoff != 0) {
492 fnvlist_add_uint64(args, "resume_object", resumeobj);
493 fnvlist_add_uint64(args, "resume_offset", resumeoff);
495 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
501 * "from" can be NULL, a snapshot, or a bookmark.
503 * If from is NULL, a full (non-incremental) stream will be estimated. This
504 * is calculated very efficiently.
506 * If from is a snapshot, lzc_send_space uses the deadlists attached to
507 * each snapshot to efficiently estimate the stream size.
509 * If from is a bookmark, the indirect blocks in the destination snapshot
510 * are traversed, looking for blocks with a birth time since the creation TXG of
511 * the snapshot this bookmark was created from. This will result in
512 * significantly more I/O and be less efficient than a send space estimation on
513 * an equivalent snapshot.
516 lzc_send_space(const char *snapname, const char *from,
517 enum lzc_send_flags flags, uint64_t *spacep)
523 args = fnvlist_alloc();
525 fnvlist_add_string(args, "from", from);
526 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
527 fnvlist_add_boolean(args, "largeblockok");
528 if (flags & LZC_SEND_FLAG_EMBED_DATA)
529 fnvlist_add_boolean(args, "embedok");
530 if (flags & LZC_SEND_FLAG_COMPRESS)
531 fnvlist_add_boolean(args, "compressok");
532 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
535 *spacep = fnvlist_lookup_uint64(result, "space");
541 recv_read(int fd, void *buf, int ilen)
548 rv = read(fd, cp, len);
553 if (rv < 0 || len != 0)
560 * Linux adds ZFS_IOC_RECV_NEW for resumable streams and preserves the legacy
561 * ZFS_IOC_RECV user/kernel interface. The new interface supports all stream
562 * options but is currently only used for resumable streams. This way updated
563 * user space utilities will interoperate with older kernel modules.
565 * Non-Linux OpenZFS platforms have opted to modify the legacy interface.
568 recv_impl(const char *snapname, nvlist_t *props, const char *origin,
569 boolean_t force, boolean_t resumable, int input_fd,
570 const dmu_replay_record_t *begin_record, int cleanup_fd,
571 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
574 dmu_replay_record_t drr;
575 char fsname[MAXPATHLEN];
579 /* Set 'fsname' to the name of containing filesystem */
580 (void) strlcpy(fsname, snapname, sizeof (fsname));
581 atp = strchr(fsname, '@');
586 /* If the fs does not exist, try its parent. */
587 if (!lzc_exists(fsname)) {
588 char *slashp = strrchr(fsname, '/');
595 * The begin_record is normally a non-byteswapped BEGIN record.
596 * For resumable streams it may be set to any non-byteswapped
597 * dmu_replay_record_t.
599 if (begin_record == NULL) {
600 error = recv_read(input_fd, &drr, sizeof (drr));
608 nvlist_t *outnvl = NULL;
609 nvlist_t *innvl = fnvlist_alloc();
611 fnvlist_add_string(innvl, "snapname", snapname);
614 fnvlist_add_nvlist(innvl, "props", props);
616 if (origin != NULL && strlen(origin))
617 fnvlist_add_string(innvl, "origin", origin);
619 fnvlist_add_byte_array(innvl, "begin_record",
620 (uchar_t *)&drr, sizeof (drr));
622 fnvlist_add_int32(innvl, "input_fd", input_fd);
625 fnvlist_add_boolean(innvl, "force");
628 fnvlist_add_boolean(innvl, "resumable");
631 fnvlist_add_int32(innvl, "cleanup_fd", cleanup_fd);
633 if (action_handle != NULL)
634 fnvlist_add_uint64(innvl, "action_handle",
637 error = lzc_ioctl(ZFS_IOC_RECV_NEW, fsname, innvl, &outnvl);
639 if (error == 0 && read_bytes != NULL)
640 error = nvlist_lookup_uint64(outnvl, "read_bytes",
643 if (error == 0 && errflags != NULL)
644 error = nvlist_lookup_uint64(outnvl, "error_flags",
647 if (error == 0 && action_handle != NULL)
648 error = nvlist_lookup_uint64(outnvl, "action_handle",
651 if (error == 0 && errors != NULL) {
653 error = nvlist_lookup_nvlist(outnvl, "errors", &nvl);
655 *errors = fnvlist_dup(nvl);
659 fnvlist_free(outnvl);
661 zfs_cmd_t zc = {"\0"};
665 ASSERT3S(g_refcount, >, 0);
667 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_value));
668 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
671 packed = fnvlist_pack(props, &size);
672 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
673 zc.zc_nvlist_src_size = size;
677 (void) strlcpy(zc.zc_string, origin,
678 sizeof (zc.zc_string));
680 ASSERT3S(drr.drr_type, ==, DRR_BEGIN);
681 zc.zc_begin_record = drr.drr_u.drr_begin;
683 zc.zc_cookie = input_fd;
684 zc.zc_cleanup_fd = -1;
685 zc.zc_action_handle = 0;
688 zc.zc_cleanup_fd = cleanup_fd;
690 if (action_handle != NULL)
691 zc.zc_action_handle = *action_handle;
693 zc.zc_nvlist_dst_size = 128 * 1024;
694 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
695 malloc(zc.zc_nvlist_dst_size);
697 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
701 if (read_bytes != NULL)
702 *read_bytes = zc.zc_cookie;
704 if (errflags != NULL)
705 *errflags = zc.zc_obj;
707 if (action_handle != NULL)
708 *action_handle = zc.zc_action_handle;
711 VERIFY0(nvlist_unpack(
712 (void *)(uintptr_t)zc.zc_nvlist_dst,
713 zc.zc_nvlist_dst_size, errors, KM_SLEEP));
717 fnvlist_pack_free(packed, size);
718 free((void *)(uintptr_t)zc.zc_nvlist_dst);
725 * The simplest receive case: receive from the specified fd, creating the
726 * specified snapshot. Apply the specified properties as "received" properties
727 * (which can be overridden by locally-set properties). If the stream is a
728 * clone, its origin snapshot must be specified by 'origin'. The 'force'
729 * flag will cause the target filesystem to be rolled back or destroyed if
730 * necessary to receive.
732 * Return 0 on success or an errno on failure.
734 * Note: this interface does not work on dedup'd streams
735 * (those with DMU_BACKUP_FEATURE_DEDUP).
738 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
739 boolean_t force, int fd)
741 return (recv_impl(snapname, props, origin, force, B_FALSE, fd,
742 NULL, -1, NULL, NULL, NULL, NULL));
746 * Like lzc_receive, but if the receive fails due to premature stream
747 * termination, the intermediate state will be preserved on disk. In this
748 * case, ECKSUM will be returned. The receive may subsequently be resumed
749 * with a resuming send stream generated by lzc_send_resume().
752 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
753 boolean_t force, int fd)
755 return (recv_impl(snapname, props, origin, force, B_TRUE, fd,
756 NULL, -1, NULL, NULL, NULL, NULL));
760 * Like lzc_receive, but allows the caller to read the begin record and then to
761 * pass it in. That could be useful if the caller wants to derive, for example,
762 * the snapname or the origin parameters based on the information contained in
764 * The begin record must be in its original form as read from the stream,
765 * in other words, it should not be byteswapped.
767 * The 'resumable' parameter allows to obtain the same behavior as with
768 * lzc_receive_resumable.
771 lzc_receive_with_header(const char *snapname, nvlist_t *props,
772 const char *origin, boolean_t force, boolean_t resumable, int fd,
773 const dmu_replay_record_t *begin_record)
775 if (begin_record == NULL)
777 return (recv_impl(snapname, props, origin, force, resumable, fd,
778 begin_record, -1, NULL, NULL, NULL, NULL));
782 * Like lzc_receive, but allows the caller to pass all supported arguments
783 * and retrieve all values returned. The only additional input parameter
784 * is 'cleanup_fd' which is used to set a cleanup-on-exit file descriptor.
786 * The following parameters all provide return values. Several may be set
787 * in the failure case and will contain additional information.
789 * The 'read_bytes' value will be set to the total number of bytes read.
791 * The 'errflags' value will contain zprop_errflags_t flags which are
792 * used to describe any failures.
794 * The 'action_handle' is used to pass the handle for this guid/ds mapping.
795 * It should be set to zero on first call and will contain an updated handle
796 * on success, it should be passed in subsequent calls.
798 * The 'errors' nvlist contains an entry for each unapplied received
799 * property. Callers are responsible for freeing this nvlist.
801 int lzc_receive_one(const char *snapname, nvlist_t *props,
802 const char *origin, boolean_t force, boolean_t resumable, int input_fd,
803 const dmu_replay_record_t *begin_record, int cleanup_fd,
804 uint64_t *read_bytes, uint64_t *errflags, uint64_t *action_handle,
807 return (recv_impl(snapname, props, origin, force, resumable,
808 input_fd, begin_record, cleanup_fd, read_bytes, errflags,
809 action_handle, errors));
813 * Roll back this filesystem or volume to its most recent snapshot.
814 * If snapnamebuf is not NULL, it will be filled in with the name
815 * of the most recent snapshot.
817 * Return 0 on success or an errno on failure.
820 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
826 args = fnvlist_alloc();
827 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
829 if (err == 0 && snapnamebuf != NULL) {
830 const char *snapname = fnvlist_lookup_string(result, "target");
831 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
839 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
840 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
841 * snapshots must be in the same pool.
843 * The returned results nvlist will have an entry for each bookmark that failed.
844 * The value will be the (int32) error code.
846 * The return value will be 0 if all bookmarks were created, otherwise it will
847 * be the errno of a (undetermined) bookmarks that failed.
850 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
854 char pool[ZFS_MAX_DATASET_NAME_LEN];
856 /* determine the pool name */
857 elem = nvlist_next_nvpair(bookmarks, NULL);
860 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
861 pool[strcspn(pool, "/#")] = '\0';
863 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
869 * Retrieve bookmarks.
871 * Retrieve the list of bookmarks for the given file system. The props
872 * parameter is an nvlist of property names (with no values) that will be
873 * returned for each bookmark.
875 * The following are valid properties on bookmarks, all of which are numbers
876 * (represented as uint64 in the nvlist)
878 * "guid" - globally unique identifier of the snapshot it refers to
879 * "createtxg" - txg when the snapshot it refers to was created
880 * "creation" - timestamp when the snapshot it refers to was created
882 * The format of the returned nvlist as follows:
883 * <short name of bookmark> -> {
884 * <name of property> -> {
890 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
892 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
896 * Destroys bookmarks.
898 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
899 * They must all be in the same pool. Bookmarks are specified as
902 * Bookmarks that do not exist will be silently ignored.
904 * The return value will be 0 if all bookmarks that existed were destroyed.
906 * Otherwise the return value will be the errno of a (undetermined) bookmark
907 * that failed, no bookmarks will be destroyed, and the errlist will have an
908 * entry for each bookmarks that failed. The value in the errlist will be
909 * the (int32) error code.
912 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
916 char pool[ZFS_MAX_DATASET_NAME_LEN];
918 /* determine the pool name */
919 elem = nvlist_next_nvpair(bmarks, NULL);
922 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
923 pool[strcspn(pool, "/#")] = '\0';
925 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);