libzfs_core.c revision 3b2aab18808792cbd248a12f1edf139b89833c13
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 by Delphix. 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
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew 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 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 *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The return value will be 0 if all holds were created. Otherwise the return
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * value will be the errno of a (unspecified) hold that failed, no holds will
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * be created, and the errlist will have an entry for each hold that
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * failed (name = snapshot). The value in the errlist will be the error
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * 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 *
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The return value will be 0 if all holds were removed.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Otherwise the return value will be the errno of a (unspecified) release
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * that failed, no holds will be released, and the errlist will have an
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * entry for each snapshot that has failed releases (name = snapshot).
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * The value in the errlist will be the error code (int32) of a failed 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 * If fromsnap is NULL, a full (non-incremental) stream will be sent.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_send(const char *snapname, const char *fromsnap, int fd)
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 (fromsnap != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(args, "fromsnap", fromsnap);
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 * If fromsnap is NULL, a full (non-incremental) stream will be estimated.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_send_space(const char *snapname, const char *fromsnap, 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 (fromsnap != NULL)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens fnvlist_add_string(args, "fromsnap", fromsnap);
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 Ahrens/*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * The simplest receive case: receive from the specified fd, creating the
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * specified snapshot. Apply the specified properties a "received" properties
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * (which can be overridden by locally-set properties). If the stream is a
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * clone, its origin snapshot must be specified by 'origin'. The 'force'
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * flag will cause the target filesystem to be rolled back or destroyed if
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * necessary to receive.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Return 0 on success or an errno on failure.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens *
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * Note: this interface does not work on dedup'd streams
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * (those with DMU_BACKUP_FEATURE_DEDUP).
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrensint
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrenslzc_receive(const char *snapname, nvlist_t *props, const char *origin,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens boolean_t force, int fd)
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 dmu_replay_record_t drr;
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 */
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens error = recv_read(fd, &drr, sizeof (drr));
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens if (error != 0)
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens goto out;
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens zc.zc_begin_record = drr.drr_u.drr_begin;
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
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}