4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * CDDL HEADER START
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The contents of this file are subject to the terms of the
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Common Development and Distribution License (the "License").
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * You may not use this file except in compliance with the License.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * or http://www.opensolaris.org/os/licensing.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * See the License for the specific language governing permissions
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * and limitations under the License.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * When distributing Covered Code, include this CDDL HEADER in each
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If applicable, add the following below this CDDL HEADER, with the
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * fields enclosed by brackets "[]" replaced with your own identifying
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * CDDL HEADER END
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * LibZFS_Core (lzc) is intended to replace most functionality in libzfs.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * It has the following characteristics:
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * - Thread Safe. libzfs_core is accessible concurrently from multiple
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * threads. This is accomplished primarily by avoiding global data
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * (e.g. caching). Since it's thread-safe, there is no reason for a
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * process to have multiple libzfs "instances". Therefore, we store
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * our few pieces of data (e.g. the file descriptor) in global
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * variables. The fd is reference-counted so that the libzfs_core
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * library can be "initialized" multiple times (e.g. by different
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * consumers within the same process).
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * - Committed Interface. The libzfs_core interface will be committed,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * therefore consumers can compile against it and be confident that
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * their code will continue to work on future releases of this code.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Currently, the interface is Evolving (not Committed), but we intend
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * to commit to it once it is more complete and we determine that it
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * meets the needs of all consumers.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * - Programatic Error Handling. libzfs_core communicates errors with
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * defined error numbers, and doesn't print anything to stdout/stderr.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * - Thin Layer. libzfs_core is a thin layer, marshaling arguments
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * to/from the kernel ioctls. There is generally a 1:1 correspondence
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * between libzfs_core functions and ioctls to /dev/zfs.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * - Clear Atomicity. Because libzfs_core functions are generally 1:1
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * with kernel ioctls, and kernel ioctls are general atomic, each
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * libzfs_core function is atomic. For example, creating multiple
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * snapshots with a single call to lzc_snapshot() is atomic -- it
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * can't fail with only some of the requested snapshots created, even
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * in the event of power loss or system crash.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * - Continued libzfs Support. Some higher-level operations (e.g.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * support for "zfs send -R") are too complicated to fit the scope of
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * libzfs_core. This functionality will continue to live in libzfs.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Where appropriate, libzfs will use the underlying atomic operations
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * of libzfs_core. For example, libzfs may implement "zfs send -R |
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * zfs receive" by using individual "send one snapshot", rename,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * destroy, and "receive one snapshot" operations in libzfs_core.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * /sbin/zfs and /zbin/zpool will link with both libzfs and
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * libzfs_core. Other consumers should aim to use only libzfs_core,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * since that will be the supported, stable interface going forwards.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <libzfs_core.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <ctype.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <unistd.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <stdlib.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <string.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <errno.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <fcntl.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <pthread.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <sys/nvpair.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <sys/param.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <sys/types.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <sys/stat.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens#include <sys/zfs_ioctl.h>
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensstatic int g_fd;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensstatic pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensstatic int g_refcount;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslibzfs_core_init(void)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) pthread_mutex_lock(&g_lock);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (g_refcount == 0) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens g_fd = open("/dev/zfs", O_RDWR);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (g_fd < 0) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) pthread_mutex_unlock(&g_lock);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (errno);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens g_refcount++;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) pthread_mutex_unlock(&g_lock);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensvoid
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslibzfs_core_fini(void)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) pthread_mutex_lock(&g_lock);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens ASSERT3S(g_refcount, >, 0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens g_refcount--;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (g_refcount == 0)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) close(g_fd);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) pthread_mutex_unlock(&g_lock);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensstatic int
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_ioctl(zfs_ioc_t ioc, const char *name,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *source, nvlist_t **resultp)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zfs_cmd_t zc = { 0 };
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error = 0;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char *packed;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens size_t size;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens ASSERT3S(g_refcount, >, 0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens packed = fnvlist_pack(source, &size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_src_size = size;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (resultp != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *resultp = NULL;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_dst_size = MAX(size * 2, 128 * 1024);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens malloc(zc.zc_nvlist_dst_size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (zc.zc_nvlist_dst == NULL) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = ENOMEM;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens goto out;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens while (ioctl(g_fd, ioc, &zc) != 0) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (errno == ENOMEM && resultp != NULL) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens free((void *)(uintptr_t)zc.zc_nvlist_dst);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_dst_size *= 2;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_dst = (uint64_t)(uintptr_t)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens malloc(zc.zc_nvlist_dst_size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (zc.zc_nvlist_dst == NULL) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = ENOMEM;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens goto out;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens } else {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = errno;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens break;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (zc.zc_nvlist_dst_filled) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *resultp = fnvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_dst_size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensout:
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_pack_free(packed, size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens free((void *)(uintptr_t)zc.zc_nvlist_dst);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (error);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
26455f9efcf9b1e44937d4d86d1ce37b006f25a9Andriy Gaponlzc_create(const char *fsname, enum lzc_dataset_type type, nvlist_t *props)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args = fnvlist_alloc();
26455f9efcf9b1e44937d4d86d1ce37b006f25a9Andriy Gapon fnvlist_add_int32(args, "type", (dmu_objset_type_t)type);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (props != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_nvlist(args, "props", props);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = lzc_ioctl(ZFS_IOC_CREATE, fsname, args, NULL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (error);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_clone(const char *fsname, const char *origin,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *props)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args = fnvlist_alloc();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(args, "origin", origin);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (props != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_nvlist(args, "props", props);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = lzc_ioctl(ZFS_IOC_CLONE, fsname, args, NULL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (error);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Creates snapshots.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The keys in the snaps nvlist are the snapshots to be created.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * They must all be in the same pool.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The props nvlist is properties to set. Currently only user properties
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * are supported. { user:prop_name -> string value }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The returned results nvlist will have an entry for each snapshot that failed.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The value will be the (int32) error code.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The return value will be 0 if all snapshots were created, otherwise it will
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * be the errno of a (unspecified) snapshot that failed.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t **errlist)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvpair_t *elem;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *errlist = NULL;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* determine the pool name */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens elem = nvlist_next_nvpair(snaps, NULL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (elem == NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens pool[strcspn(pool, "/@")] = '\0';
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens args = fnvlist_alloc();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_nvlist(args, "snaps", snaps);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (props != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_nvlist(args, "props", props);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = lzc_ioctl(ZFS_IOC_SNAPSHOT, pool, args, errlist);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (error);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Destroys snapshots.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The keys in the snaps nvlist are the snapshots to be destroyed.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * They must all be in the same pool.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Snapshots that do not exist will be silently ignored.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If 'defer' is not set, and a snapshot has user holds or clones, the
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * destroy operation will fail and none of the snapshots will be
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * destroyed.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If 'defer' is set, and a snapshot has user holds or clones, it will be
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * marked for deferred destruction, and will be destroyed when the last hold
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * or clone is removed/destroyed.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The return value will be 0 if all snapshots were destroyed (or marked for
bb6e70758d0c30c09f148026d6e686e21cfc8d18Matthew Ahrens * later destruction if 'defer' is set) or didn't exist to begin with.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Otherwise the return value will be the errno of a (unspecified) snapshot
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * that failed, no snapshots will be destroyed, and the errlist will have an
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * entry for each snapshot that failed. The value in the errlist will be
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * the (int32) error code.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_destroy_snaps(nvlist_t *snaps, boolean_t defer, nvlist_t **errlist)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvpair_t *elem;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* determine the pool name */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens elem = nvlist_next_nvpair(snaps, NULL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (elem == NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens pool[strcspn(pool, "/@")] = '\0';
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens args = fnvlist_alloc();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_nvlist(args, "snaps", snaps);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (defer)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_boolean(args, "defer");
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = lzc_ioctl(ZFS_IOC_DESTROY_SNAPS, pool, args, errlist);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (error);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_snaprange_space(const char *firstsnap, const char *lastsnap,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens uint64_t *usedp)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *result;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int err;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char fs[ZFS_MAX_DATASET_NAME_LEN];
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char *atp;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* determine the fs name */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(fs, firstsnap, sizeof (fs));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens atp = strchr(fs, '@');
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (atp == NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (EINVAL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *atp = '\0';
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens args = fnvlist_alloc();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(args, "firstsnap", firstsnap);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens err = lzc_ioctl(ZFS_IOC_SPACE_SNAPS, lastsnap, args, &result);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (err == 0)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *usedp = fnvlist_lookup_uint64(result, "used");
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_free(result);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (err);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensboolean_t
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_exists(const char *dataset)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The objset_stats ioctl is still legacy, so we need to construct our
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * own zfs_cmd_t rather than using zfsc_ioctl().
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zfs_cmd_t zc = { 0 };
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (ioctl(g_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Create "user holds" on snapshots. If there is a hold on a snapshot,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * the snapshot can not be destroyed. (However, it can be marked for deletion
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * by lzc_destroy_snaps(defer=B_TRUE).)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The keys in the nvlist are snapshot names.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The snapshots must all be in the same pool.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The value is the name of the hold (string type).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If cleanup_fd is not -1, it must be the result of open("/dev/zfs", O_EXCL).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * In this case, when the cleanup_fd is closed (including on process
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * termination), the holds will be released. If the system is shut down
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * uncleanly, the holds will be released when the pool is next opened
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * or imported.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Holds for snapshots which don't exist will be skipped and have an entry
bb6e70758d0c30c09f148026d6e686e21cfc8d18Matthew Ahrens * added to errlist, but will not cause an overall failure.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
bb6e70758d0c30c09f148026d6e686e21cfc8d18Matthew Ahrens * The return value will be 0 if all holds, for snapshots that existed,
bb6e70758d0c30c09f148026d6e686e21cfc8d18Matthew Ahrens * were succesfully created.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Otherwise the return value will be the errno of a (unspecified) hold that
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * failed and no holds will be created.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * In all cases the errlist will have an entry for each hold that failed
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * (name = snapshot), with its value being the error code (int32).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenslzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *args;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *elem;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* determine the pool name */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens elem = nvlist_next_nvpair(holds, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (elem == NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pool[strcspn(pool, "/@")] = '\0';
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens args = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_nvlist(args, "holds", holds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (cleanup_fd != -1)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_add_int32(args, "cleanup_fd", cleanup_fd);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = lzc_ioctl(ZFS_IOC_HOLD, pool, args, errlist);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_free(args);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Release "user holds" on snapshots. If the snapshot has been marked for
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * deferred destroy (by lzc_destroy_snaps(defer=B_TRUE)), it does not have
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * any clones, and all the user holds are removed, then the snapshot will be
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * destroyed.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The keys in the nvlist are snapshot names.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The snapshots must all be in the same pool.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The value is a nvlist whose keys are the holds to remove.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Holds which failed to release because they didn't exist will have an entry
bb6e70758d0c30c09f148026d6e686e21cfc8d18Matthew Ahrens * added to errlist, but will not cause an overall failure.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * The return value will be 0 if the nvl holds was empty or all holds that
bb6e70758d0c30c09f148026d6e686e21cfc8d18Matthew Ahrens * existed, were successfully removed.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Otherwise the return value will be the errno of a (unspecified) hold that
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * failed to release and no holds will be released.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland *
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * In all cases the errlist will have an entry for each hold that failed to
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * to release.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenslzc_release(nvlist_t *holds, nvlist_t **errlist)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvpair_t *elem;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* determine the pool name */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens elem = nvlist_next_nvpair(holds, NULL);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (elem == NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens pool[strcspn(pool, "/@")] = '\0';
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (lzc_ioctl(ZFS_IOC_RELEASE, pool, holds, errlist));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Retrieve list of user holds on the specified snapshot.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * On success, *holdsp will be set to a nvlist which the caller must free.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The keys are the names of the holds, and the value is the creation time
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * of the hold (uint64) in seconds since the epoch.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenslzc_get_holds(const char *snapname, nvlist_t **holdsp)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens nvlist_t *innvl = fnvlist_alloc();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = lzc_ioctl(ZFS_IOC_GET_HOLDS, snapname, innvl, holdsp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens fnvlist_free(innvl);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Generate a zfs send stream for the specified snapshot and write it to
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * the specified file descriptor.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * If "from" is NULL, a full (non-incremental) stream will be sent.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * If "from" is non-NULL, it must be the full name of a snapshot or
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * bookmark must represent an earlier point in the history of "snapname").
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * or it can be the origin of "snapname"'s filesystem, or an earlier
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * snapshot in the origin, etc.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "fd" is the file descriptor to write the send stream to.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens *
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * records with drr_blksz > 128K.
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens *
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * which the receiving system must support (as indicated by support
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * for the "embedded_data" feature).
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrenslzc_send(const char *snapname, const char *from, int fd,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens enum lzc_send_flags flags)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensint
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenslzc_send_resume(const char *snapname, const char *from, int fd,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens enum lzc_send_flags flags, uint64_t resumeobj, uint64_t resumeoff)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int err;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens args = fnvlist_alloc();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_int32(args, "fd", fd);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (from != NULL)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fnvlist_add_string(args, "fromsnap", from);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens fnvlist_add_boolean(args, "largeblockok");
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (flags & LZC_SEND_FLAG_EMBED_DATA)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens fnvlist_add_boolean(args, "embedok");
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (flags & LZC_SEND_FLAG_COMPRESS)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel fnvlist_add_boolean(args, "compressok");
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resumeobj != 0 || resumeoff != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(args, "resume_object", resumeobj);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(args, "resume_offset", resumeoff);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens err = lzc_ioctl(ZFS_IOC_SEND_NEW, snapname, args, NULL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (err);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * "from" can be NULL, a snapshot, or a bookmark.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman *
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * If from is NULL, a full (non-incremental) stream will be estimated. This
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * is calculated very efficiently.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman *
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * If from is a snapshot, lzc_send_space uses the deadlists attached to
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * each snapshot to efficiently estimate the stream size.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman *
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * If from is a bookmark, the indirect blocks in the destination snapshot
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * are traversed, looking for blocks with a birth time since the creation TXG of
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * the snapshot this bookmark was created from. This will result in
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * significantly more I/O and be less efficient than a send space estimation on
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * an equivalent snapshot.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmellzc_send_space(const char *snapname, const char *from,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel enum lzc_send_flags flags, uint64_t *spacep)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *result;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int err;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens args = fnvlist_alloc();
643da460c8ca583e39ce053081754e24087f84c8Max Grossman if (from != NULL)
643da460c8ca583e39ce053081754e24087f84c8Max Grossman fnvlist_add_string(args, "from", from);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel fnvlist_add_boolean(args, "largeblockok");
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (flags & LZC_SEND_FLAG_EMBED_DATA)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel fnvlist_add_boolean(args, "embedok");
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (flags & LZC_SEND_FLAG_COMPRESS)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel fnvlist_add_boolean(args, "compressok");
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens err = lzc_ioctl(ZFS_IOC_SEND_SPACE, snapname, args, &result);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(args);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (err == 0)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *spacep = fnvlist_lookup_uint64(result, "space");
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_free(result);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (err);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensstatic int
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensrecv_read(int fd, void *buf, int ilen)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char *cp = buf;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int rv;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int len = ilen;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens do {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens rv = read(fd, cp, len);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens cp += rv;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens len -= rv;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens } while (rv > 0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (rv < 0 || len != 0)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (EIO);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic int
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gaponrecv_impl(const char *snapname, nvlist_t *props, const char *origin,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon boolean_t force, boolean_t resumable, int fd,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon const dmu_replay_record_t *begin_record)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The receive ioctl is still legacy, so we need to construct our own
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * zfs_cmd_t rather than using zfsc_ioctl().
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zfs_cmd_t zc = { 0 };
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char *atp;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char *packed = NULL;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens size_t size;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens ASSERT3S(g_refcount, >, 0);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_name is name of containing filesystem */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(zc.zc_name, snapname, sizeof (zc.zc_name));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens atp = strchr(zc.zc_name, '@');
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (atp == NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (EINVAL);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *atp = '\0';
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* if the fs does not exist, try its parent. */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (!lzc_exists(zc.zc_name)) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char *slashp = strrchr(zc.zc_name, '/');
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (slashp == NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (ENOENT);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *slashp = '\0';
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_value is full name of the snapshot to create */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (props != NULL) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_nvlist_src is props to set */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens packed = fnvlist_pack(props, &size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_nvlist_src_size = size;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (origin != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens (void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_begin_record is non-byteswapped BEGIN record */
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon if (begin_record == NULL) {
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon error = recv_read(fd, &zc.zc_begin_record,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon sizeof (zc.zc_begin_record));
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon if (error != 0)
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon goto out;
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon } else {
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon zc.zc_begin_record = *begin_record;
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon }
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_cookie is fd to read from */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_cookie = fd;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc guid is force flag */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_guid = force;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zc.zc_resumable = resumable;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /* zc_cleanup_fd is unused */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_cleanup_fd = -1;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (error != 0)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = errno;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensout:
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (packed != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_pack_free(packed, size);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens free((void*)(uintptr_t)zc.zc_nvlist_dst);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (error);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens/*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * The simplest receive case: receive from the specified fd, creating the
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * specified snapshot. Apply the specified properties as "received" properties
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * (which can be overridden by locally-set properties). If the stream is a
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * clone, its origin snapshot must be specified by 'origin'. The 'force'
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * flag will cause the target filesystem to be rolled back or destroyed if
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * necessary to receive.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens *
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Return 0 on success or an errno on failure.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens *
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note: this interface does not work on dedup'd streams
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * (those with DMU_BACKUP_FEATURE_DEDUP).
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensint
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenslzc_receive(const char *snapname, nvlist_t *props, const char *origin,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t force, int fd)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon return (recv_impl(snapname, props, origin, force, B_FALSE, fd, NULL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens/*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Like lzc_receive, but if the receive fails due to premature stream
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * termination, the intermediate state will be preserved on disk. In this
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * case, ECKSUM will be returned. The receive may subsequently be resumed
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * with a resuming send stream generated by lzc_send_resume().
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensint
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenslzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t force, int fd)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon return (recv_impl(snapname, props, origin, force, B_TRUE, fd, NULL));
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon}
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon/*
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * Like lzc_receive, but allows the caller to read the begin record and then to
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * pass it in. That could be useful if the caller wants to derive, for example,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * the snapname or the origin parameters based on the information contained in
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * the begin record.
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * The begin record must be in its original form as read from the stream,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * in other words, it should not be byteswapped.
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon *
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * The 'resumable' parameter allows to obtain the same behavior as with
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon * lzc_receive_resumable.
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon */
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gaponint
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gaponlzc_receive_with_header(const char *snapname, nvlist_t *props,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon const char *origin, boolean_t force, boolean_t resumable, int fd,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon const dmu_replay_record_t *begin_record)
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon{
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon if (begin_record == NULL)
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon return (EINVAL);
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon return (recv_impl(snapname, props, origin, force, resumable, fd,
620f322510b2d6433f7f6af60fa52380c07756adAndriy Gapon begin_record));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens/*
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * Roll back this filesystem or volume to its most recent snapshot.
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * If snapnamebuf is not NULL, it will be filled in with the name
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * of the most recent snapshot.
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens *
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens * Return 0 on success or an errno on failure.
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens */
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrensint
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrenslzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens{
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens nvlist_t *args;
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens nvlist_t *result;
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens int err;
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens args = fnvlist_alloc();
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens nvlist_free(args);
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens if (err == 0 && snapnamebuf != NULL) {
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens const char *snapname = fnvlist_lookup_string(result, "target");
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens (void) strlcpy(snapnamebuf, snapname, snapnamelen);
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens }
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens return (err);
a7027df17fad220a20367b9d1eb251bc6300d203Matthew Ahrens}
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens/*
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Creates bookmarks.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * snapshots must be in the same pool.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The returned results nvlist will have an entry for each bookmark that failed.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The value will be the (int32) error code.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The return value will be 0 if all bookmarks were created, otherwise it will
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * be the errno of a (undetermined) bookmarks that failed.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrensint
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrenslzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens{
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens nvpair_t *elem;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens int error;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens /* determine the pool name */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens elem = nvlist_next_nvpair(bookmarks, NULL);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (elem == NULL)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (0);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens pool[strcspn(pool, "/#")] = '\0';
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (error);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens}
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens/*
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Retrieve bookmarks.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Retrieve the list of bookmarks for the given file system. The props
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * parameter is an nvlist of property names (with no values) that will be
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * returned for each bookmark.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The following are valid properties on bookmarks, all of which are numbers
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * (represented as uint64 in the nvlist)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "guid" - globally unique identifier of the snapshot it refers to
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "createtxg" - txg when the snapshot it refers to was created
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "creation" - timestamp when the snapshot it refers to was created
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The format of the returned nvlist as follows:
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * <short name of bookmark> -> {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * <name of property> -> {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * "value" -> uint64
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrensint
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrenslzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens{
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens}
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens/*
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Destroys bookmarks.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The keys in the bmarks nvlist are the bookmarks to be destroyed.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * They must all be in the same pool. Bookmarks are specified as
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * <fs>#<bmark>.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Bookmarks that do not exist will be silently ignored.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * The return value will be 0 if all bookmarks that existed were destroyed.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens *
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * Otherwise the return value will be the errno of a (undetermined) bookmark
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * that failed, no bookmarks will be destroyed, and the errlist will have an
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * entry for each bookmarks that failed. The value in the errlist will be
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * the (int32) error code.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrensint
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrenslzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens{
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens nvpair_t *elem;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens int error;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN];
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens /* determine the pool name */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens elem = nvlist_next_nvpair(bmarks, NULL);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (elem == NULL)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (0);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens pool[strcspn(pool, "/#")] = '\0';
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (error);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens}