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) 2014 Integros [integros.com]
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 * - Programatic 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 #define _IN_LIBZFS_CORE_
76 #include <libzfs_core.h>
84 #include <sys/nvpair.h>
85 #include <sys/param.h>
86 #include <sys/types.h>
88 #include <sys/zfs_ioctl.h>
89 #include "libzfs_core_compat.h"
90 #include "libzfs_compat.h"
93 extern int zfs_ioctl_version;
97 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
98 static int g_refcount;
101 libzfs_core_init(void)
103 (void) pthread_mutex_lock(&g_lock);
104 if (g_refcount == 0) {
105 g_fd = open("/dev/zfs", O_RDWR);
107 (void) pthread_mutex_unlock(&g_lock);
112 (void) pthread_mutex_unlock(&g_lock);
118 libzfs_core_fini(void)
120 (void) pthread_mutex_lock(&g_lock);
121 ASSERT3S(g_refcount, >, 0);
125 (void) pthread_mutex_unlock(&g_lock);
129 lzc_ioctl(zfs_ioc_t ioc, const char *name,
130 nvlist_t *source, nvlist_t **resultp)
132 zfs_cmd_t zc = { 0 };
140 ASSERT3S(g_refcount, >, 0);
142 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
145 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
146 zfs_ioctl_version = get_zfs_ioctl_version();
148 if (zfs_ioctl_version < ZFS_IOCVER_LZC) {
150 error = lzc_compat_pre(&zc, &ioc, &source);
156 packed = fnvlist_pack(source, &size);
157 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
158 zc.zc_nvlist_src_size = size;
160 if (resultp != NULL) {
162 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
163 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
164 malloc(zc.zc_nvlist_dst_size);
166 if (zc.zc_nvlist_dst == NULL) {
168 if (zc.zc_nvlist_dst == 0) {
175 while (ioctl(g_fd, ioc, &zc) != 0) {
176 if (errno == ENOMEM && resultp != NULL) {
177 free((void *)(uintptr_t)zc.zc_nvlist_dst);
178 zc.zc_nvlist_dst_size *= 2;
179 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
180 malloc(zc.zc_nvlist_dst_size);
182 if (zc.zc_nvlist_dst == NULL) {
184 if (zc.zc_nvlist_dst == 0) {
196 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
197 lzc_compat_post(&zc, ioc);
199 if (zc.zc_nvlist_dst_filled) {
200 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
201 zc.zc_nvlist_dst_size);
204 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
205 lzc_compat_outnvl(&zc, ioc, resultp);
209 if (zfs_ioctl_version < ZFS_IOCVER_LZC) {
210 if (source != oldsource)
215 fnvlist_pack_free(packed, size);
216 free((void *)(uintptr_t)zc.zc_nvlist_dst);
221 lzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
224 nvlist_t *args = fnvlist_alloc();
225 fnvlist_add_int32(args, "type", type);
227 fnvlist_add_nvlist(args, "props", props);
228 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
234 lzc_clone(const char *fsname, const char *origin,
238 nvlist_t *args = fnvlist_alloc();
239 fnvlist_add_string(args, "origin", origin);
241 fnvlist_add_nvlist(args, "props", props);
242 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
250 * The keys in the snaps nvlist are the snapshots to be created.
251 * They must all be in the same pool.
253 * The props nvlist is properties to set. Currently only user properties
254 * are supported. { user:prop_name -> string value }
256 * The returned results nvlist will have an entry for each snapshot that failed.
257 * The value will be the (int32) error code.
259 * The return value will be 0 if all snapshots were created, otherwise it will
260 * be the errno of a (unspecified) snapshot that failed.
263 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
268 char pool[MAXNAMELEN];
272 /* determine the pool name */
273 elem = nvlist_next_nvpair(snaps, NULL);
276 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
277 pool[strcspn(pool, "/@")] = '\0';
279 args = fnvlist_alloc();
280 fnvlist_add_nvlist(args, "snaps", snaps);
282 fnvlist_add_nvlist(args, "props", props);
284 error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
291 * Destroys snapshots.
293 * The keys in the snaps nvlist are the snapshots to be destroyed.
294 * They must all be in the same pool.
296 * Snapshots that do not exist will be silently ignored.
298 * If 'defer' is not set, and a snapshot has user holds or clones, the
299 * destroy operation will fail and none of the snapshots will be
302 * If 'defer' is set, and a snapshot has user holds or clones, it will be
303 * marked for deferred destruction, and will be destroyed when the last hold
304 * or clone is removed/destroyed.
306 * The return value will be 0 if all snapshots were destroyed (or marked for
307 * later destruction if 'defer' is set) or didn't exist to begin with.
309 * Otherwise the return value will be the errno of a (unspecified) snapshot
310 * that failed, no snapshots will be destroyed, and the errlist will have an
311 * entry for each snapshot that failed. The value in the errlist will be
312 * the (int32) error code.
315 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
320 char pool[MAXNAMELEN];
322 /* determine the pool name */
323 elem = nvlist_next_nvpair(snaps, NULL);
326 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
327 pool[strcspn(pool, "/@")] = '\0';
329 args = fnvlist_alloc();
330 fnvlist_add_nvlist(args, "snaps", snaps);
332 fnvlist_add_boolean(args, "defer");
334 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
341 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
350 /* determine the fs name */
351 (void) strlcpy(fs, firstsnap, sizeof (fs));
352 atp = strchr(fs, '@');
357 args = fnvlist_alloc();
358 fnvlist_add_string(args, "firstsnap", firstsnap);
360 err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
363 *usedp = fnvlist_lookup_uint64(result, "used");
364 fnvlist_free(result);
370 lzc_exists(const char *dataset)
373 * The objset_stats ioctl is still legacy, so we need to construct our
374 * own zfs_cmd_t rather than using zfsc_ioctl().
376 zfs_cmd_t zc = { 0 };
378 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
379 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
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 succesfully 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[MAXNAMELEN];
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 a 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[MAXNAMELEN];
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 a 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)
483 nvlist_t *innvl = fnvlist_alloc();
484 error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
490 * Generate a zfs send stream for the specified snapshot and write it to
491 * the specified file descriptor.
493 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
495 * If "from" is NULL, a full (non-incremental) stream will be sent.
496 * If "from" is non-NULL, it must be the full name of a snapshot or
497 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
498 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
499 * bookmark must represent an earlier point in the history of "snapname").
500 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
501 * or it can be the origin of "snapname"'s filesystem, or an earlier
502 * snapshot in the origin, etc.
504 * "fd" is the file descriptor to write the send stream to.
506 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
507 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
508 * records with drr_blksz > 128K.
510 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
511 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
512 * which the receiving system must support (as indicated by support
513 * for the "embedded_data" feature).
516 lzc_send(const char *snapname, const char *from, int fd,
517 enum lzc_send_flags flags)
519 return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
523 lzc_send_resume(const char *snapname, const char *from, int fd,
524 enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
529 args = fnvlist_alloc();
530 fnvlist_add_int32(args, "fd", fd);
532 fnvlist_add_string(args, "fromsnap", from);
533 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
534 fnvlist_add_boolean(args, "largeblockok");
535 if (flags & LZC_SEND_FLAG_EMBED_DATA)
536 fnvlist_add_boolean(args, "embedok");
537 if (resumeobj != 0 || resumeoff != 0) {
538 fnvlist_add_uint64(args, "resume_object", resumeobj);
539 fnvlist_add_uint64(args, "resume_offset", resumeoff);
541 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
547 * "from" can be NULL, a snapshot, or a bookmark.
549 * If from is NULL, a full (non-incremental) stream will be estimated. This
550 * is calculated very efficiently.
552 * If from is a snapshot, lzc_send_space uses the deadlists attached to
553 * each snapshot to efficiently estimate the stream size.
555 * If from is a bookmark, the indirect blocks in the destination snapshot
556 * are traversed, looking for blocks with a birth time since the creation TXG of
557 * the snapshot this bookmark was created from. This will result in
558 * significantly more I/O and be less efficient than a send space estimation on
559 * an equivalent snapshot.
562 lzc_send_space(const char *snapname, const char *from, uint64_t *spacep)
568 args = fnvlist_alloc();
570 fnvlist_add_string(args, "from", from);
571 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
574 *spacep = fnvlist_lookup_uint64(result, "space");
580 recv_read(int fd, void *buf, int ilen)
587 rv = read(fd, cp, len);
592 if (rv < 0 || len != 0)
599 lzc_receive_impl(const char *snapname, nvlist_t *props, const char *origin,
600 boolean_t force, boolean_t resumable, int fd)
603 * The receive ioctl is still legacy, so we need to construct our own
604 * zfs_cmd_t rather than using zfsc_ioctl().
606 zfs_cmd_t zc = { 0 };
612 ASSERT3S(g_refcount, >, 0);
614 /* zc_name is name of containing filesystem */
615 (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
616 atp = strchr(zc.zc_name, '@');
621 /* if the fs does not exist, try its parent. */
622 if (!lzc_exists(zc.zc_name)) {
623 char *slashp = strrchr(zc.zc_name, '/');
630 /* zc_value is full name of the snapshot to create */
631 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
634 /* zc_nvlist_src is props to set */
635 packed = fnvlist_pack(props, &size);
636 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
637 zc.zc_nvlist_src_size = size;
640 /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
642 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
644 /* zc_begin_record is non-byteswapped BEGIN record */
645 error = recv_read(fd, &zc.zc_begin_record, sizeof (zc.zc_begin_record));
649 /* zc_cookie is fd to read from */
652 /* zc guid is force flag */
655 zc.zc_resumable = resumable;
657 /* zc_cleanup_fd is unused */
658 zc.zc_cleanup_fd = -1;
660 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
666 fnvlist_pack_free(packed, size);
667 free((void*)(uintptr_t)zc.zc_nvlist_dst);
672 * The simplest receive case: receive from the specified fd, creating the
673 * specified snapshot. Apply the specified properties as "received" properties
674 * (which can be overridden by locally-set properties). If the stream is a
675 * clone, its origin snapshot must be specified by 'origin'. The 'force'
676 * flag will cause the target filesystem to be rolled back or destroyed if
677 * necessary to receive.
679 * Return 0 on success or an errno on failure.
681 * Note: this interface does not work on dedup'd streams
682 * (those with DMU_BACKUP_FEATURE_DEDUP).
685 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
686 boolean_t force, int fd)
688 return (lzc_receive_impl(snapname, props, origin, force, B_FALSE, fd));
692 * Like lzc_receive, but if the receive fails due to premature stream
693 * termination, the intermediate state will be preserved on disk. In this
694 * case, ECKSUM will be returned. The receive may subsequently be resumed
695 * with a resuming send stream generated by lzc_send_resume().
698 lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
699 boolean_t force, int fd)
701 return (lzc_receive_impl(snapname, props, origin, force, B_TRUE, fd));
705 * Roll back this filesystem or volume to its most recent snapshot.
706 * If snapnamebuf is not NULL, it will be filled in with the name
707 * of the most recent snapshot.
709 * Return 0 on success or an errno on failure.
712 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
718 args = fnvlist_alloc();
719 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
721 if (err == 0 && snapnamebuf != NULL) {
722 const char *snapname = fnvlist_lookup_string(result, "target");
723 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
731 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
732 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
733 * snapshots must be in the same pool.
735 * The returned results nvlist will have an entry for each bookmark that failed.
736 * The value will be the (int32) error code.
738 * The return value will be 0 if all bookmarks were created, otherwise it will
739 * be the errno of a (undetermined) bookmarks that failed.
742 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
746 char pool[MAXNAMELEN];
748 /* determine the pool name */
749 elem = nvlist_next_nvpair(bookmarks, NULL);
752 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
753 pool[strcspn(pool, "/#")] = '\0';
755 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
761 * Retrieve bookmarks.
763 * Retrieve the list of bookmarks for the given file system. The props
764 * parameter is an nvlist of property names (with no values) that will be
765 * returned for each bookmark.
767 * The following are valid properties on bookmarks, all of which are numbers
768 * (represented as uint64 in the nvlist)
770 * "guid" - globally unique identifier of the snapshot it refers to
771 * "createtxg" - txg when the snapshot it refers to was created
772 * "creation" - timestamp when the snapshot it refers to was created
774 * The format of the returned nvlist as follows:
775 * <short name of bookmark> -> {
776 * <name of property> -> {
782 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
784 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
788 * Destroys bookmarks.
790 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
791 * They must all be in the same pool. Bookmarks are specified as
794 * Bookmarks that do not exist will be silently ignored.
796 * The return value will be 0 if all bookmarks that existed were destroyed.
798 * Otherwise the return value will be the errno of a (undetermined) bookmark
799 * that failed, no bookmarks will be destroyed, and the errlist will have an
800 * entry for each bookmarks that failed. The value in the errlist will be
801 * the (int32) error code.
804 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
808 char pool[MAXNAMELEN];
810 /* determine the pool name */
811 elem = nvlist_next_nvpair(bmarks, NULL);
814 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
815 pool[strcspn(pool, "/#")] = '\0';
817 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);