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) 2013 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 * - Programatic 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 #define _IN_LIBZFS_CORE_
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>
88 #include "libzfs_core_compat.h"
89 #include "libzfs_compat.h"
92 extern int zfs_ioctl_version;
96 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
97 static int g_refcount;
100 libzfs_core_init(void)
102 (void) pthread_mutex_lock(&g_lock);
103 if (g_refcount == 0) {
104 g_fd = open("/dev/zfs", O_RDWR);
106 (void) pthread_mutex_unlock(&g_lock);
111 (void) pthread_mutex_unlock(&g_lock);
117 libzfs_core_fini(void)
119 (void) pthread_mutex_lock(&g_lock);
120 ASSERT3S(g_refcount, >, 0);
124 (void) pthread_mutex_unlock(&g_lock);
128 lzc_ioctl(zfs_ioc_t ioc, const char *name,
129 nvlist_t *source, nvlist_t **resultp)
131 zfs_cmd_t zc = { 0 };
139 ASSERT3S(g_refcount, >, 0);
141 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
144 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
145 zfs_ioctl_version = get_zfs_ioctl_version();
147 if (zfs_ioctl_version < ZFS_IOCVER_LZC) {
149 error = lzc_compat_pre(&zc, &ioc, &source);
155 packed = fnvlist_pack(source, &size);
156 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
157 zc.zc_nvlist_src_size = size;
159 if (resultp != NULL) {
161 zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
162 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
163 malloc(zc.zc_nvlist_dst_size);
165 if (zc.zc_nvlist_dst == NULL) {
167 if (zc.zc_nvlist_dst == 0) {
174 while (ioctl(g_fd, ioc, &zc) != 0) {
175 if (errno == ENOMEM && resultp != NULL) {
176 free((void *)(uintptr_t)zc.zc_nvlist_dst);
177 zc.zc_nvlist_dst_size *= 2;
178 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
179 malloc(zc.zc_nvlist_dst_size);
181 if (zc.zc_nvlist_dst == NULL) {
183 if (zc.zc_nvlist_dst == 0) {
195 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
196 lzc_compat_post(&zc, ioc);
198 if (zc.zc_nvlist_dst_filled) {
199 *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
200 zc.zc_nvlist_dst_size);
203 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
204 lzc_compat_outnvl(&zc, ioc, resultp);
208 if (zfs_ioctl_version < ZFS_IOCVER_LZC) {
209 if (source != oldsource)
214 fnvlist_pack_free(packed, size);
215 free((void *)(uintptr_t)zc.zc_nvlist_dst);
220 lzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
223 nvlist_t *args = fnvlist_alloc();
224 fnvlist_add_int32(args, "type", type);
226 fnvlist_add_nvlist(args, "props", props);
227 error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
233 lzc_clone(const char *fsname, const char *origin,
237 nvlist_t *args = fnvlist_alloc();
238 fnvlist_add_string(args, "origin", origin);
240 fnvlist_add_nvlist(args, "props", props);
241 error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
249 * The keys in the snaps nvlist are the snapshots to be created.
250 * They must all be in the same pool.
252 * The props nvlist is properties to set. Currently only user properties
253 * are supported. { user:prop_name -> string value }
255 * The returned results nvlist will have an entry for each snapshot that failed.
256 * The value will be the (int32) error code.
258 * The return value will be 0 if all snapshots were created, otherwise it will
259 * be the errno of a (unspecified) snapshot that failed.
262 lzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
267 char pool[MAXNAMELEN];
271 /* determine the pool name */
272 elem = nvlist_next_nvpair(snaps, NULL);
275 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
276 pool[strcspn(pool, "/@")] = '\0';
278 args = fnvlist_alloc();
279 fnvlist_add_nvlist(args, "snaps", snaps);
281 fnvlist_add_nvlist(args, "props", props);
283 error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
290 * Destroys snapshots.
292 * The keys in the snaps nvlist are the snapshots to be destroyed.
293 * They must all be in the same pool.
295 * Snapshots that do not exist will be silently ignored.
297 * If 'defer' is not set, and a snapshot has user holds or clones, the
298 * destroy operation will fail and none of the snapshots will be
301 * If 'defer' is set, and a snapshot has user holds or clones, it will be
302 * marked for deferred destruction, and will be destroyed when the last hold
303 * or clone is removed/destroyed.
305 * The return value will be 0 if all snapshots were destroyed (or marked for
306 * later destruction if 'defer' is set) or didn't exist to begin with.
308 * Otherwise the return value will be the errno of a (unspecified) snapshot
309 * that failed, no snapshots will be destroyed, and the errlist will have an
310 * entry for each snapshot that failed. The value in the errlist will be
311 * the (int32) error code.
314 lzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
319 char pool[MAXNAMELEN];
321 /* determine the pool name */
322 elem = nvlist_next_nvpair(snaps, NULL);
325 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
326 pool[strcspn(pool, "/@")] = '\0';
328 args = fnvlist_alloc();
329 fnvlist_add_nvlist(args, "snaps", snaps);
331 fnvlist_add_boolean(args, "defer");
333 error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
340 lzc_snaprange_space(const char *firstsnap, const char *lastsnap,
349 /* determine the fs name */
350 (void) strlcpy(fs, firstsnap, sizeof (fs));
351 atp = strchr(fs, '@');
356 args = fnvlist_alloc();
357 fnvlist_add_string(args, "firstsnap", firstsnap);
359 err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
362 *usedp = fnvlist_lookup_uint64(result, "used");
363 fnvlist_free(result);
369 lzc_exists(const char *dataset)
372 * The objset_stats ioctl is still legacy, so we need to construct our
373 * own zfs_cmd_t rather than using zfsc_ioctl().
375 zfs_cmd_t zc = { 0 };
377 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
378 return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
382 * Create "user holds" on snapshots. If there is a hold on a snapshot,
383 * the snapshot can not be destroyed. (However, it can be marked for deletion
384 * by lzc_destroy_snaps(defer=B_TRUE).)
386 * The keys in the nvlist are snapshot names.
387 * The snapshots must all be in the same pool.
388 * The value is the name of the hold (string type).
390 * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
391 * In this case, when the cleanup_fd is closed (including on process
392 * termination), the holds will be released. If the system is shut down
393 * uncleanly, the holds will be released when the pool is next opened
396 * Holds for snapshots which don't exist will be skipped and have an entry
397 * added to errlist, but will not cause an overall failure.
399 * The return value will be 0 if all holds, for snapshots that existed,
400 * were succesfully created.
402 * Otherwise the return value will be the errno of a (unspecified) hold that
403 * failed and no holds will be created.
405 * In all cases the errlist will have an entry for each hold that failed
406 * (name = snapshot), with its value being the error code (int32).
409 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
411 char pool[MAXNAMELEN];
416 /* determine the pool name */
417 elem = nvlist_next_nvpair(holds, NULL);
420 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
421 pool[strcspn(pool, "/@")] = '\0';
423 args = fnvlist_alloc();
424 fnvlist_add_nvlist(args, "holds", holds);
425 if (cleanup_fd != -1)
426 fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
428 error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
434 * Release "user holds" on snapshots. If the snapshot has been marked for
435 * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
436 * any clones, and all the user holds are removed, then the snapshot will be
439 * The keys in the nvlist are snapshot names.
440 * The snapshots must all be in the same pool.
441 * The value is a nvlist whose keys are the holds to remove.
443 * Holds which failed to release because they didn't exist will have an entry
444 * added to errlist, but will not cause an overall failure.
446 * The return value will be 0 if the nvl holds was empty or all holds that
447 * existed, were successfully removed.
449 * Otherwise the return value will be the errno of a (unspecified) hold that
450 * failed to release and no holds will be released.
452 * In all cases the errlist will have an entry for each hold that failed to
456 lzc_release(nvlist_t *holds, nvlist_t **errlist)
458 char pool[MAXNAMELEN];
461 /* determine the pool name */
462 elem = nvlist_next_nvpair(holds, NULL);
465 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
466 pool[strcspn(pool, "/@")] = '\0';
468 return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
472 * Retrieve list of user holds on the specified snapshot.
474 * On success, *holdsp will be set to a nvlist which the caller must free.
475 * The keys are the names of the holds, and the value is the creation time
476 * of the hold (uint64) in seconds since the epoch.
479 lzc_get_holds(const char *snapname, nvlist_t **holdsp)
482 nvlist_t *innvl = fnvlist_alloc();
483 error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
489 * Generate a zfs send stream for the specified snapshot and write it to
490 * the specified file descriptor.
492 * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
494 * If "from" is NULL, a full (non-incremental) stream will be sent.
495 * If "from" is non-NULL, it must be the full name of a snapshot or
496 * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
497 * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
498 * bookmark must represent an earlier point in the history of "snapname").
499 * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
500 * or it can be the origin of "snapname"'s filesystem, or an earlier
501 * snapshot in the origin, etc.
503 * "fd" is the file descriptor to write the send stream to.
505 * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
506 * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
507 * records with drr_blksz > 128K.
509 * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
510 * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
511 * which the receiving system must support (as indicated by support
512 * for the "embedded_data" feature).
515 lzc_send(const char *snapname, const char *from, int fd,
516 enum lzc_send_flags flags)
521 args = fnvlist_alloc();
522 fnvlist_add_int32(args, "fd", fd);
524 fnvlist_add_string(args, "fromsnap", from);
525 if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
526 fnvlist_add_boolean(args, "largeblockok");
527 if (flags & LZC_SEND_FLAG_EMBED_DATA)
528 fnvlist_add_boolean(args, "embedok");
529 err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
535 * If fromsnap is NULL, a full (non-incremental) stream will be estimated.
538 lzc_send_space(const char *snapname, const char *fromsnap, uint64_t *spacep)
544 args = fnvlist_alloc();
545 if (fromsnap != NULL)
546 fnvlist_add_string(args, "fromsnap", fromsnap);
547 err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
550 *spacep = fnvlist_lookup_uint64(result, "space");
556 recv_read(int fd, void *buf, int ilen)
563 rv = read(fd, cp, len);
568 if (rv < 0 || len != 0)
575 * The simplest receive case: receive from the specified fd, creating the
576 * specified snapshot. Apply the specified properties a "received" properties
577 * (which can be overridden by locally-set properties). If the stream is a
578 * clone, its origin snapshot must be specified by 'origin'. The 'force'
579 * flag will cause the target filesystem to be rolled back or destroyed if
580 * necessary to receive.
582 * Return 0 on success or an errno on failure.
584 * Note: this interface does not work on dedup'd streams
585 * (those with DMU_BACKUP_FEATURE_DEDUP).
588 lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
589 boolean_t force, int fd)
592 * The receive ioctl is still legacy, so we need to construct our own
593 * zfs_cmd_t rather than using zfsc_ioctl().
595 zfs_cmd_t zc = { 0 };
599 dmu_replay_record_t drr;
602 ASSERT3S(g_refcount, >, 0);
604 /* zc_name is name of containing filesystem */
605 (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
606 atp = strchr(zc.zc_name, '@');
611 /* if the fs does not exist, try its parent. */
612 if (!lzc_exists(zc.zc_name)) {
613 char *slashp = strrchr(zc.zc_name, '/');
620 /* zc_value is full name of the snapshot to create */
621 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
624 /* zc_nvlist_src is props to set */
625 packed = fnvlist_pack(props, &size);
626 zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
627 zc.zc_nvlist_src_size = size;
630 /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
632 (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
634 /* zc_begin_record is non-byteswapped BEGIN record */
635 error = recv_read(fd, &drr, sizeof (drr));
638 zc.zc_begin_record = drr.drr_u.drr_begin;
640 /* zc_cookie is fd to read from */
643 /* zc guid is force flag */
646 /* zc_cleanup_fd is unused */
647 zc.zc_cleanup_fd = -1;
649 error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
655 fnvlist_pack_free(packed, size);
656 free((void*)(uintptr_t)zc.zc_nvlist_dst);
661 * Roll back this filesystem or volume to its most recent snapshot.
662 * If snapnamebuf is not NULL, it will be filled in with the name
663 * of the most recent snapshot.
665 * Return 0 on success or an errno on failure.
668 lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
674 args = fnvlist_alloc();
675 err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
677 if (err == 0 && snapnamebuf != NULL) {
678 const char *snapname = fnvlist_lookup_string(result, "target");
679 (void) strlcpy(snapnamebuf, snapname, snapnamelen);
687 * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
688 * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
689 * snapshots must be in the same pool.
691 * The returned results nvlist will have an entry for each bookmark that failed.
692 * The value will be the (int32) error code.
694 * The return value will be 0 if all bookmarks were created, otherwise it will
695 * be the errno of a (undetermined) bookmarks that failed.
698 lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
702 char pool[MAXNAMELEN];
704 /* determine the pool name */
705 elem = nvlist_next_nvpair(bookmarks, NULL);
708 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
709 pool[strcspn(pool, "/#")] = '\0';
711 error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
717 * Retrieve bookmarks.
719 * Retrieve the list of bookmarks for the given file system. The props
720 * parameter is an nvlist of property names (with no values) that will be
721 * returned for each bookmark.
723 * The following are valid properties on bookmarks, all of which are numbers
724 * (represented as uint64 in the nvlist)
726 * "guid" - globally unique identifier of the snapshot it refers to
727 * "createtxg" - txg when the snapshot it refers to was created
728 * "creation" - timestamp when the snapshot it refers to was created
730 * The format of the returned nvlist as follows:
731 * <short name of bookmark> -> {
732 * <name of property> -> {
738 lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
740 return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
744 * Destroys bookmarks.
746 * The keys in the bmarks nvlist are the bookmarks to be destroyed.
747 * They must all be in the same pool. Bookmarks are specified as
750 * Bookmarks that do not exist will be silently ignored.
752 * The return value will be 0 if all bookmarks that existed were destroyed.
754 * Otherwise the return value will be the errno of a (undetermined) bookmark
755 * that failed, no bookmarks will be destroyed, and the errlist will have an
756 * entry for each bookmarks that failed. The value in the errlist will be
757 * the (int32) error code.
760 lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
764 char pool[MAXNAMELEN];
766 /* determine the pool name */
767 elem = nvlist_next_nvpair(bmarks, NULL);
770 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
771 pool[strcspn(pool, "/#")] = '\0';
773 error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);