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, 2017 by Delphix. All rights reserved.
24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
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 * - Programatic 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 #define _IN_LIBZFS_CORE_
77 #include <libzfs_core.h>
85 #include <sys/nvpair.h>
86 #include <sys/param.h>
87 #include <sys/types.h>
89 #include <sys/zfs_ioctl.h>
90 #include "libzfs_core_compat.h"
91 #include "libzfs_compat.h"
94 extern int zfs_ioctl_version;
98 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
99 static int g_refcount;
102 libzfs_core_init(void)
104 (void) pthread_mutex_lock(&g_lock);
105 if (g_refcount == 0) {
106 g_fd = open("/dev/zfs", O_RDWR);
108 (void) pthread_mutex_unlock(&g_lock);
113 (void) pthread_mutex_unlock(&g_lock);
119 libzfs_core_fini(void)
121 (void) pthread_mutex_lock(&g_lock);
122 ASSERT3S(g_refcount, >, 0);
127 if (g_refcount == 0 && g_fd != -1) {
131 (void) pthread_mutex_unlock(&g_lock);
135 lzc_ioctl(zfs_ioc_t ioc, const char *name,
136 nvlist_t *source, nvlist_t **resultp)
138 zfs_cmd_t zc = { 0 };
146 ASSERT3S(g_refcount, >, 0);
147 VERIFY3S(g_fd, !=, -1);
149 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
152 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
153 zfs_ioctl_version = get_zfs_ioctl_version();
155 if (zfs_ioctl_version < ZFS_IOCVER_LZC) {
157 error = lzc_compat_pre(&zc, &ioc, &source);
163 packed = fnvlist_pack(source, &size);
164 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
165 zc.zc_nvlist_src_size = size;
167 if (resultp != NULL) {
169 if (ioc == ZFS_IOC_CHANNEL_PROGRAM) {
170 zc.zc_nvlist_dst_size = fnvlist_lookup_uint64(source,
173 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
175 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
176 malloc(zc.zc_nvlist_dst_size);
178 if (zc.zc_nvlist_dst == NULL) {
180 if (zc.zc_nvlist_dst == 0) {
187 while (ioctl(g_fd, ioc, &zc) != 0) {
189 * If ioctl exited with ENOMEM, we retry the ioctl after
190 * increasing the size of the destination nvlist.
192 * Channel programs that exit with ENOMEM ran over the
193 * lua memory sandbox; they should not be retried.
195 if (errno == ENOMEM && resultp != NULL &&
196 ioc != ZFS_IOC_CHANNEL_PROGRAM) {
197 free((void *)(uintptr_t)zc.zc_nvlist_dst);
198 zc.zc_nvlist_dst_size *= 2;
199 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
200 malloc(zc.zc_nvlist_dst_size);
202 if (zc.zc_nvlist_dst == NULL) {
204 if (zc.zc_nvlist_dst == 0) {
216 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
217 lzc_compat_post(&zc, ioc);
219 if (zc.zc_nvlist_dst_filled) {
220 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
221 zc.zc_nvlist_dst_size);
224 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
225 lzc_compat_outnvl(&zc, ioc, resultp);
229 if (zfs_ioctl_version < ZFS_IOCVER_LZC) {
230 if (source != oldsource)
235 fnvlist_pack_free(packed, size);
236 free((void *)(uintptr_t)zc.zc_nvlist_dst);
241 lzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props)
244 nvlist_t *args = fnvlist_alloc();
245 fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
247 fnvlist_add_nvlist(args, "props", props);
248 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
254 lzc_clone(const char *fsname, const char *origin,
258 nvlist_t *args = fnvlist_alloc();
259 fnvlist_add_string(args, "origin", origin);
261 fnvlist_add_nvlist(args, "props", props);
262 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
268 lzc_promote(const char *fsname, char *snapnamebuf, int snapnamelen)
271 * The promote ioctl is still legacy, so we need to construct our
272 * own zfs_cmd_t rather than using lzc_ioctl().
274 zfs_cmd_t zc = { 0 };
276 ASSERT3S(g_refcount, >, 0);
277 VERIFY3S(g_fd, !=, -1);
279 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
280 if (ioctl(g_fd, ZFS_IOC_PROMOTE, &zc) != 0) {
282 if (error == EEXIST && snapnamebuf != NULL)
283 (void) strlcpy(snapnamebuf, zc.zc_string, snapnamelen);
290 lzc_remap(const char *fsname)
293 nvlist_t *args = fnvlist_alloc();
294 error = lzc_ioctl(ZFS_IOC_REMAP, fsname, args, NULL);
302 * The keys in the snaps nvlist are the snapshots to be created.
303 * They must all be in the same pool.
305 * The props nvlist is properties to set. Currently only user properties
306 * are supported. { user:prop_name -> string value }
308 * The returned results nvlist will have an entry for each snapshot that failed.
309 * The value will be the (int32) error code.
311 * The return value will be 0 if all snapshots were created, otherwise it will
312 * be the errno of a (unspecified) snapshot that failed.
315 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
320 char pool[ZFS_MAX_DATASET_NAME_LEN];
324 /* determine the pool name */
325 elem = nvlist_next_nvpair(snaps, NULL);
328 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
329 pool[strcspn(pool, "/@")] = '\0';
331 args = fnvlist_alloc();
332 fnvlist_add_nvlist(args, "snaps", snaps);
334 fnvlist_add_nvlist(args, "props", props);
336 error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
343 * Destroys snapshots.
345 * The keys in the snaps nvlist are the snapshots to be destroyed.
346 * They must all be in the same pool.
348 * Snapshots that do not exist will be silently ignored.
350 * If 'defer' is not set, and a snapshot has user holds or clones, the
351 * destroy operation will fail and none of the snapshots will be
354 * If 'defer' is set, and a snapshot has user holds or clones, it will be
355 * marked for deferred destruction, and will be destroyed when the last hold
356 * or clone is removed/destroyed.
358 * The return value will be 0 if all snapshots were destroyed (or marked for
359 * later destruction if 'defer' is set) or didn't exist to begin with.
361 * Otherwise the return value will be the errno of a (unspecified) snapshot
362 * that failed, no snapshots will be destroyed, and the errlist will have an
363 * entry for each snapshot that failed. The value in the errlist will be
364 * the (int32) error code.
367 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
372 char pool[ZFS_MAX_DATASET_NAME_LEN];
374 /* determine the pool name */
375 elem = nvlist_next_nvpair(snaps, NULL);
378 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
379 pool[strcspn(pool, "/@")] = '\0';
381 args = fnvlist_alloc();
382 fnvlist_add_nvlist(args, "snaps", snaps);
384 fnvlist_add_boolean(args, "defer");
386 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
393 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
399 char fs[ZFS_MAX_DATASET_NAME_LEN];
402 /* determine the fs name */
403 (void) strlcpy(fs, firstsnap, sizeof (fs));
404 atp = strchr(fs, '@');
409 args = fnvlist_alloc();
410 fnvlist_add_string(args, "firstsnap", firstsnap);
412 err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
415 *usedp = fnvlist_lookup_uint64(result, "used");
416 fnvlist_free(result);
422 lzc_exists(const char *dataset)
425 * The objset_stats ioctl is still legacy, so we need to construct our
426 * own zfs_cmd_t rather than using lzc_ioctl().
428 zfs_cmd_t zc = { 0 };
430 ASSERT3S(g_refcount, >, 0);
431 VERIFY3S(g_fd, !=, -1);
433 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
434 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
438 * Create "user holds" on snapshots. If there is a hold on a snapshot,
439 * the snapshot can not be destroyed. (However, it can be marked for deletion
440 * by lzc_destroy_snaps(defer=B_TRUE).)
442 * The keys in the nvlist are snapshot names.
443 * The snapshots must all be in the same pool.
444 * The value is the name of the hold (string type).
446 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
447 * In this case, when the cleanup_fd is closed (including on process
448 * termination), the holds will be released. If the system is shut down
449 * uncleanly, the holds will be released when the pool is next opened
452 * Holds for snapshots which don't exist will be skipped and have an entry
453 * added to errlist, but will not cause an overall failure.
455 * The return value will be 0 if all holds, for snapshots that existed,
456 * were succesfully created.
458 * Otherwise the return value will be the errno of a (unspecified) hold that
459 * failed and no holds will be created.
461 * In all cases the errlist will have an entry for each hold that failed
462 * (name = snapshot), with its value being the error code (int32).
465 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
467 char pool[ZFS_MAX_DATASET_NAME_LEN];
472 /* determine the pool name */
473 elem = nvlist_next_nvpair(holds, NULL);
476 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
477 pool[strcspn(pool, "/@")] = '\0';
479 args = fnvlist_alloc();
480 fnvlist_add_nvlist(args, "holds", holds);
481 if (cleanup_fd != -1)
482 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
484 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
490 * Release "user holds" on snapshots. If the snapshot has been marked for
491 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
492 * any clones, and all the user holds are removed, then the snapshot will be
495 * The keys in the nvlist are snapshot names.
496 * The snapshots must all be in the same pool.
497 * The value is a nvlist whose keys are the holds to remove.
499 * Holds which failed to release because they didn't exist will have an entry
500 * added to errlist, but will not cause an overall failure.
502 * The return value will be 0 if the nvl holds was empty or all holds that
503 * existed, were successfully removed.
505 * Otherwise the return value will be the errno of a (unspecified) hold that
506 * failed to release and no holds will be released.
508 * In all cases the errlist will have an entry for each hold that failed to
512 lzc_release(nvlist_t *holds, nvlist_t **errlist)
514 char pool[ZFS_MAX_DATASET_NAME_LEN];
517 /* determine the pool name */
518 elem = nvlist_next_nvpair(holds, NULL);
521 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
522 pool[strcspn(pool, "/@")] = '\0';
524 return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
528 * Retrieve list of user holds on the specified snapshot.
530 * On success, *holdsp will be set to a nvlist which the caller must free.
531 * The keys are the names of the holds, and the value is the creation time
532 * of the hold (uint64) in seconds since the epoch.
535 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
538 nvlist_t *innvl = fnvlist_alloc();
539 error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
545 * Generate a zfs send stream for the specified snapshot and write it to
546 * the specified file descriptor.
548 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
550 * If "from" is NULL, a full (non-incremental) stream will be sent.
551 * If "from" is non-NULL, it must be the full name of a snapshot or
552 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
553 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
554 * bookmark must represent an earlier point in the history of "snapname").
555 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
556 * or it can be the origin of "snapname"'s filesystem, or an earlier
557 * snapshot in the origin, etc.
559 * "fd" is the file descriptor to write the send stream to.
561 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
562 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
563 * records with drr_blksz > 128K.
565 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
566 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
567 * which the receiving system must support (as indicated by support
568 * for the "embedded_data" feature).
571 lzc_send(const char *snapname, const char *from, int fd,
572 enum lzc_send_flags flags)
574 return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
578 lzc_send_resume(const char *snapname, const char *from, int fd,
579 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
584 args = fnvlist_alloc();
585 fnvlist_add_int32(args, "fd", fd);
587 fnvlist_add_string(args, "fromsnap", from);
588 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
589 fnvlist_add_boolean(args, "largeblockok");
590 if (flags & LZC_SEND_FLAG_EMBED_DATA)
591 fnvlist_add_boolean(args, "embedok");
592 if (flags & LZC_SEND_FLAG_COMPRESS)
593 fnvlist_add_boolean(args, "compressok");
594 if (resumeobj != 0 || resumeoff != 0) {
595 fnvlist_add_uint64(args, "resume_object", resumeobj);
596 fnvlist_add_uint64(args, "resume_offset", resumeoff);
598 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
604 * "from" can be NULL, a snapshot, or a bookmark.
606 * If from is NULL, a full (non-incremental) stream will be estimated. This
607 * is calculated very efficiently.
609 * If from is a snapshot, lzc_send_space uses the deadlists attached to
610 * each snapshot to efficiently estimate the stream size.
612 * If from is a bookmark, the indirect blocks in the destination snapshot
613 * are traversed, looking for blocks with a birth time since the creation TXG of
614 * the snapshot this bookmark was created from. This will result in
615 * significantly more I/O and be less efficient than a send space estimation on
616 * an equivalent snapshot.
619 lzc_send_space(const char *snapname, const char *from,
620 enum lzc_send_flags flags, uint64_t *spacep)
626 args = fnvlist_alloc();
628 fnvlist_add_string(args, "from", from);
629 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
630 fnvlist_add_boolean(args, "largeblockok");
631 if (flags & LZC_SEND_FLAG_EMBED_DATA)
632 fnvlist_add_boolean(args, "embedok");
633 if (flags & LZC_SEND_FLAG_COMPRESS)
634 fnvlist_add_boolean(args, "compressok");
635 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
638 *spacep = fnvlist_lookup_uint64(result, "space");
644 recv_read(int fd, void *buf, int ilen)
651 rv = read(fd, cp, len);
656 if (rv < 0 || len != 0)
663 recv_impl(const char *snapname, nvlist_t *props, const char *origin,
664 boolean_t force, boolean_t resumable, int fd,
665 const dmu_replay_record_t *begin_record)
668 * The receive ioctl is still legacy, so we need to construct our own
669 * zfs_cmd_t rather than using zfsc_ioctl().
671 zfs_cmd_t zc = { 0 };
677 ASSERT3S(g_refcount, >, 0);
678 VERIFY3S(g_fd, !=, -1);
680 /* zc_name is name of containing filesystem */
681 (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
682 atp = strchr(zc.zc_name, '@');
687 /* if the fs does not exist, try its parent. */
688 if (!lzc_exists(zc.zc_name)) {
689 char *slashp = strrchr(zc.zc_name, '/');
696 /* zc_value is full name of the snapshot to create */
697 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
700 /* zc_nvlist_src is props to set */
701 packed = fnvlist_pack(props, &size);
702 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
703 zc.zc_nvlist_src_size = size;
706 /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
708 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
710 /* zc_begin_record is non-byteswapped BEGIN record */
711 if (begin_record == NULL) {
712 error = recv_read(fd, &zc.zc_begin_record,
713 sizeof (zc.zc_begin_record));
717 zc.zc_begin_record = *begin_record;
720 /* zc_cookie is fd to read from */
723 /* zc guid is force flag */
726 zc.zc_resumable = resumable;
728 /* zc_cleanup_fd is unused */
729 zc.zc_cleanup_fd = -1;
731 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
737 fnvlist_pack_free(packed, size);
738 free((void*)(uintptr_t)zc.zc_nvlist_dst);
743 * The simplest receive case: receive from the specified fd, creating the
744 * specified snapshot. Apply the specified properties as "received" properties
745 * (which can be overridden by locally-set properties). If the stream is a
746 * clone, its origin snapshot must be specified by 'origin'. The 'force'
747 * flag will cause the target filesystem to be rolled back or destroyed if
748 * necessary to receive.
750 * Return 0 on success or an errno on failure.
752 * Note: this interface does not work on dedup'd streams
753 * (those with DMU_BACKUP_FEATURE_DEDUP).
756 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
757 boolean_t force, int fd)
759 return (recv_impl(snapname, props, origin, force, B_FALSE, fd, NULL));
763 * Like lzc_receive, but if the receive fails due to premature stream
764 * termination, the intermediate state will be preserved on disk. In this
765 * case, ECKSUM will be returned. The receive may subsequently be resumed
766 * with a resuming send stream generated by lzc_send_resume().
769 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
770 boolean_t force, int fd)
772 return (recv_impl(snapname, props, origin, force, B_TRUE, fd, NULL));
776 * Like lzc_receive, but allows the caller to read the begin record and then to
777 * pass it in. That could be useful if the caller wants to derive, for example,
778 * the snapname or the origin parameters based on the information contained in
780 * The begin record must be in its original form as read from the stream,
781 * in other words, it should not be byteswapped.
783 * The 'resumable' parameter allows to obtain the same behavior as with
784 * lzc_receive_resumable.
787 lzc_receive_with_header(const char *snapname, nvlist_t *props,
788 const char *origin, boolean_t force, boolean_t resumable, int fd,
789 const dmu_replay_record_t *begin_record)
791 if (begin_record == NULL)
793 return (recv_impl(snapname, props, origin, force, resumable, fd,
798 * Roll back this filesystem or volume to its most recent snapshot.
799 * If snapnamebuf is not NULL, it will be filled in with the name
800 * of the most recent snapshot.
801 * Note that the latest snapshot may change if a new one is concurrently
802 * created or the current one is destroyed. lzc_rollback_to can be used
803 * to roll back to a specific latest snapshot.
805 * Return 0 on success or an errno on failure.
808 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
814 args = fnvlist_alloc();
815 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
817 if (err == 0 && snapnamebuf != NULL) {
818 const char *snapname = fnvlist_lookup_string(result, "target");
819 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
827 * Roll back this filesystem or volume to the specified snapshot,
830 * Return 0 on success or an errno on failure.
833 lzc_rollback_to(const char *fsname, const char *snapname)
839 args = fnvlist_alloc();
840 fnvlist_add_string(args, "target", snapname);
841 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
850 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
851 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
852 * snapshots must be in the same pool.
854 * The returned results nvlist will have an entry for each bookmark that failed.
855 * The value will be the (int32) error code.
857 * The return value will be 0 if all bookmarks were created, otherwise it will
858 * be the errno of a (undetermined) bookmarks that failed.
861 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
865 char pool[ZFS_MAX_DATASET_NAME_LEN];
867 /* determine the pool name */
868 elem = nvlist_next_nvpair(bookmarks, NULL);
871 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
872 pool[strcspn(pool, "/#")] = '\0';
874 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
880 * Retrieve bookmarks.
882 * Retrieve the list of bookmarks for the given file system. The props
883 * parameter is an nvlist of property names (with no values) that will be
884 * returned for each bookmark.
886 * The following are valid properties on bookmarks, all of which are numbers
887 * (represented as uint64 in the nvlist)
889 * "guid" - globally unique identifier of the snapshot it refers to
890 * "createtxg" - txg when the snapshot it refers to was created
891 * "creation" - timestamp when the snapshot it refers to was created
893 * The format of the returned nvlist as follows:
894 * <short name of bookmark> -> {
895 * <name of property> -> {
901 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
903 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
907 * Destroys bookmarks.
909 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
910 * They must all be in the same pool. Bookmarks are specified as
913 * Bookmarks that do not exist will be silently ignored.
915 * The return value will be 0 if all bookmarks that existed were destroyed.
917 * Otherwise the return value will be the errno of a (undetermined) bookmark
918 * that failed, no bookmarks will be destroyed, and the errlist will have an
919 * entry for each bookmarks that failed. The value in the errlist will be
920 * the (int32) error code.
923 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
927 char pool[ZFS_MAX_DATASET_NAME_LEN];
929 /* determine the pool name */
930 elem = nvlist_next_nvpair(bmarks, NULL);
933 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
934 pool[strcspn(pool, "/#")] = '\0';
936 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
942 lzc_channel_program_impl(const char *pool, const char *program, boolean_t sync,
943 uint64_t instrlimit, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
948 args = fnvlist_alloc();
949 fnvlist_add_string(args, ZCP_ARG_PROGRAM, program);
950 fnvlist_add_nvlist(args, ZCP_ARG_ARGLIST, argnvl);
951 fnvlist_add_boolean_value(args, ZCP_ARG_SYNC, sync);
952 fnvlist_add_uint64(args, ZCP_ARG_INSTRLIMIT, instrlimit);
953 fnvlist_add_uint64(args, ZCP_ARG_MEMLIMIT, memlimit);
954 error = lzc_ioctl(ZFS_IOC_CHANNEL_PROGRAM, pool, args, outnvl);
961 * Executes a channel program.
963 * If this function returns 0 the channel program was successfully loaded and
964 * ran without failing. Note that individual commands the channel program ran
965 * may have failed and the channel program is responsible for reporting such
966 * errors through outnvl if they are important.
968 * This method may also return:
970 * EINVAL The program contains syntax errors, or an invalid memory or time
971 * limit was given. No part of the channel program was executed.
972 * If caused by syntax errors, 'outnvl' contains information about the
975 * EDOM The program was executed, but encountered a runtime error, such as
976 * calling a function with incorrect arguments, invoking the error()
977 * function directly, failing an assert() command, etc. Some portion
978 * of the channel program may have executed and committed changes.
979 * Information about the failure can be found in 'outnvl'.
981 * ENOMEM The program fully executed, but the output buffer was not large
982 * enough to store the returned value. No output is returned through
985 * ENOSPC The program was terminated because it exceeded its memory usage
986 * limit. Some portion of the channel program may have executed and
987 * committed changes to disk. No output is returned through 'outnvl'.
989 * ETIMEDOUT The program was terminated because it exceeded its Lua instruction
990 * limit. Some portion of the channel program may have executed and
991 * committed changes to disk. No output is returned through 'outnvl'.
994 lzc_channel_program(const char *pool, const char *program, uint64_t instrlimit,
995 uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
997 return (lzc_channel_program_impl(pool, program, B_TRUE, instrlimit,
998 memlimit, argnvl, outnvl));
1002 * Executes a read-only channel program.
1004 * A read-only channel program works programmatically the same way as a
1005 * normal channel program executed with lzc_channel_program(). The only
1006 * difference is it runs exclusively in open-context and therefore can
1007 * return faster. The downside to that, is that the program cannot change
1008 * on-disk state by calling functions from the zfs.sync submodule.
1010 * The return values of this function (and their meaning) are exactly the
1011 * same as the ones described in lzc_channel_program().
1014 lzc_channel_program_nosync(const char *pool, const char *program,
1015 uint64_t timeout, uint64_t memlimit, nvlist_t *argnvl, nvlist_t **outnvl)
1017 return (lzc_channel_program_impl(pool, program, B_FALSE, timeout,
1018 memlimit, argnvl, outnvl));