libzfs_core.c revision 620f322510b2d6433f7f6af60fa52380c07756ad
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/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
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
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_create(const char *fsname, dmu_objset_type_t type, nvlist_t *props)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens int error;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens nvlist_t *args = fnvlist_alloc();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_int32(args, "type", 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;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char pool[MAXNAMELEN];
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;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char pool[MAXNAMELEN];
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;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens char fs[MAXNAMELEN];
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{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char pool[MAXNAMELEN];
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{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens char pool[MAXNAMELEN];
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/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Generate a zfs send stream for the specified snapshot and write it to
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * the specified file descriptor.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * "snapname" is the full name of the snapshot to send (e.g. "pool/fs@snap")
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If "from" is NULL, a full (non-incremental) stream will be sent.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If "from" is non-NULL, it must be the full name of a snapshot or
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * bookmark to send an incremental from (e.g. "pool/fs@earlier_snap" or
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * "pool/fs#earlier_bmark"). If non-NULL, the specified snapshot or
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * bookmark must represent an earlier point in the history of "snapname").
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * It can be an earlier snapshot in the same filesystem or zvol as "snapname",
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * or it can be the origin of "snapname"'s filesystem, or an earlier
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * snapshot in the origin, etc.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * "fd" is the file descriptor to write the send stream to.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If "flags" contains LZC_SEND_FLAG_LARGE_BLOCK, the stream is permitted
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * to contain DRR_WRITE records with drr_length > 128K, and DRR_OBJECT
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * records with drr_blksz > 128K.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If "flags" contains LZC_SEND_FLAG_EMBED_DATA, the stream is permitted
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * to contain DRR_WRITE_EMBEDDED records with drr_etype==BP_EMBEDDED_TYPE_DATA,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * which the receiving system must support (as indicated by support
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * for the "embedded_data" feature).
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_send(const char *snapname, const char *from, int fd,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens enum lzc_send_flags flags)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens{
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens return (lzc_send_resume(snapname, from, fd, flags, 0, 0));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens}
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_send_resume(const char *snapname, const char *from, int fd,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew 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);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (from != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(args, "fromsnap", from);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (flags & LZC_SEND_FLAG_LARGE_BLOCK)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_boolean(args, "largeblockok");
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (flags & LZC_SEND_FLAG_EMBED_DATA)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_boolean(args, "embedok");
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (resumeobj != 0 || resumeoff != 0) {
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_uint64(args, "resume_object", resumeobj);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_uint64(args, "resume_offset", resumeoff);
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew 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/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * "from" can be NULL, a snapshot, or a bookmark.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If from is NULL, a full (non-incremental) stream will be estimated. This
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * is calculated very efficiently.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If from is a snapshot, lzc_send_space uses the deadlists attached to
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * each snapshot to efficiently estimate the stream size.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * If from is a bookmark, the indirect blocks in the destination snapshot
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * are traversed, looking for blocks with a birth time since the creation TXG of
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * the snapshot this bookmark was created from. This will result in
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * significantly more I/O and be less efficient than a send space estimation on
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * an equivalent snapshot.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_send_space(const char *snapname, const char *from, 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();
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (from != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(args, "from", from);
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
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensstatic int
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensrecv_impl(const char *snapname, nvlist_t *props, const char *origin,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens boolean_t force, boolean_t resumable, int fd,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens 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 */
(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
if (props != NULL) {
/* zc_nvlist_src is props to set */
packed = fnvlist_pack(props, &size);
zc.zc_nvlist_src = (uint64_t)(uintptr_t)packed;
zc.zc_nvlist_src_size = size;
}
/* zc_string is name of clone origin (if DRR_FLAG_CLONE) */
if (origin != NULL)
(void) strlcpy(zc.zc_string, origin, sizeof (zc.zc_string));
/* zc_begin_record is non-byteswapped BEGIN record */
if (begin_record == NULL) {
error = recv_read(fd, &zc.zc_begin_record,
sizeof (zc.zc_begin_record));
if (error != 0)
goto out;
} else {
zc.zc_begin_record = *begin_record;
}
/* zc_cookie is fd to read from */
zc.zc_cookie = fd;
/* zc guid is force flag */
zc.zc_guid = force;
zc.zc_resumable = resumable;
/* zc_cleanup_fd is unused */
zc.zc_cleanup_fd = -1;
error = ioctl(g_fd, ZFS_IOC_RECV, &zc);
if (error != 0)
error = errno;
out:
if (packed != NULL)
fnvlist_pack_free(packed, size);
free((void*)(uintptr_t)zc.zc_nvlist_dst);
return (error);
}
/*
* The simplest receive case: receive from the specified fd, creating the
* specified snapshot. Apply the specified properties as "received" properties
* (which can be overridden by locally-set properties). If the stream is a
* clone, its origin snapshot must be specified by 'origin'. The 'force'
* flag will cause the target filesystem to be rolled back or destroyed if
* necessary to receive.
*
* Return 0 on success or an errno on failure.
*
* Note: this interface does not work on dedup'd streams
* (those with DMU_BACKUP_FEATURE_DEDUP).
*/
int
lzc_receive(const char *snapname, nvlist_t *props, const char *origin,
boolean_t force, int fd)
{
return (recv_impl(snapname, props, origin, force, B_FALSE, fd, NULL));
}
/*
* Like lzc_receive, but if the receive fails due to premature stream
* termination, the intermediate state will be preserved on disk. In this
* case, ECKSUM will be returned. The receive may subsequently be resumed
* with a resuming send stream generated by lzc_send_resume().
*/
int
lzc_receive_resumable(const char *snapname, nvlist_t *props, const char *origin,
boolean_t force, int fd)
{
return (recv_impl(snapname, props, origin, force, B_TRUE, fd, NULL));
}
/*
* Like lzc_receive, but allows the caller to read the begin record and then to
* pass it in. That could be useful if the caller wants to derive, for example,
* the snapname or the origin parameters based on the information contained in
* the begin record.
* The begin record must be in its original form as read from the stream,
* in other words, it should not be byteswapped.
*
* The 'resumable' parameter allows to obtain the same behavior as with
* lzc_receive_resumable.
*/
int
lzc_receive_with_header(const char *snapname, nvlist_t *props,
const char *origin, boolean_t force, boolean_t resumable, int fd,
const dmu_replay_record_t *begin_record)
{
if (begin_record == NULL)
return (EINVAL);
return (recv_impl(snapname, props, origin, force, resumable, fd,
begin_record));
}
/*
* Roll back this filesystem or volume to its most recent snapshot.
* If snapnamebuf is not NULL, it will be filled in with the name
* of the most recent snapshot.
*
* Return 0 on success or an errno on failure.
*/
int
lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
{
nvlist_t *args;
nvlist_t *result;
int err;
args = fnvlist_alloc();
err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
nvlist_free(args);
if (err == 0 && snapnamebuf != NULL) {
const char *snapname = fnvlist_lookup_string(result, "target");
(void) strlcpy(snapnamebuf, snapname, snapnamelen);
}
return (err);
}
/*
* Creates bookmarks.
*
* The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to
* the name of the snapshot (e.g. "pool/fs@snap"). All the bookmarks and
* snapshots must be in the same pool.
*
* The returned results nvlist will have an entry for each bookmark that failed.
* The value will be the (int32) error code.
*
* The return value will be 0 if all bookmarks were created, otherwise it will
* be the errno of a (undetermined) bookmarks that failed.
*/
int
lzc_bookmark(nvlist_t *bookmarks, nvlist_t **errlist)
{
nvpair_t *elem;
int error;
char pool[MAXNAMELEN];
/* determine the pool name */
elem = nvlist_next_nvpair(bookmarks, NULL);
if (elem == NULL)
return (0);
(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
pool[strcspn(pool, "/#")] = '\0';
error = lzc_ioctl(ZFS_IOC_BOOKMARK, pool, bookmarks, errlist);
return (error);
}
/*
* Retrieve bookmarks.
*
* Retrieve the list of bookmarks for the given file system. The props
* parameter is an nvlist of property names (with no values) that will be
* returned for each bookmark.
*
* The following are valid properties on bookmarks, all of which are numbers
* (represented as uint64 in the nvlist)
*
* "guid" - globally unique identifier of the snapshot it refers to
* "createtxg" - txg when the snapshot it refers to was created
* "creation" - timestamp when the snapshot it refers to was created
*
* The format of the returned nvlist as follows:
* <short name of bookmark> -> {
* <name of property> -> {
* "value" -> uint64
* }
* }
*/
int
lzc_get_bookmarks(const char *fsname, nvlist_t *props, nvlist_t **bmarks)
{
return (lzc_ioctl(ZFS_IOC_GET_BOOKMARKS, fsname, props, bmarks));
}
/*
* Destroys bookmarks.
*
* The keys in the bmarks nvlist are the bookmarks to be destroyed.
* They must all be in the same pool. Bookmarks are specified as
* <fs>#<bmark>.
*
* Bookmarks that do not exist will be silently ignored.
*
* The return value will be 0 if all bookmarks that existed were destroyed.
*
* Otherwise the return value will be the errno of a (undetermined) bookmark
* that failed, no bookmarks will be destroyed, and the errlist will have an
* entry for each bookmarks that failed. The value in the errlist will be
* the (int32) error code.
*/
int
lzc_destroy_bookmarks(nvlist_t *bmarks, nvlist_t **errlist)
{
nvpair_t *elem;
int error;
char pool[MAXNAMELEN];
/* determine the pool name */
elem = nvlist_next_nvpair(bmarks, NULL);
if (elem == NULL)
return (0);
(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
pool[strcspn(pool, "/#")] = '\0';
error = lzc_ioctl(ZFS_IOC_DESTROY_BOOKMARKS, pool, bmarks, errlist);
return (error);
}