1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy 22eda14cbcSMatt Macy /* 23eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24eda14cbcSMatt Macy * Copyright (c) 2011, 2020 by Delphix. All rights reserved. 25eda14cbcSMatt Macy * Copyright (c) 2012, Joyent, Inc. All rights reserved. 26eda14cbcSMatt Macy * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. 27eda14cbcSMatt Macy * All rights reserved 28eda14cbcSMatt Macy * Copyright (c) 2013 Steven Hartland. All rights reserved. 29eda14cbcSMatt Macy * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. 30eda14cbcSMatt Macy * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 31eda14cbcSMatt Macy * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 32eda14cbcSMatt Macy * Copyright (c) 2019 Datto Inc. 33eda14cbcSMatt Macy */ 34eda14cbcSMatt Macy 35eda14cbcSMatt Macy #include <assert.h> 36eda14cbcSMatt Macy #include <ctype.h> 37eda14cbcSMatt Macy #include <errno.h> 38eda14cbcSMatt Macy #include <libintl.h> 39eda14cbcSMatt Macy #include <stdio.h> 40eda14cbcSMatt Macy #include <stdlib.h> 41da5137abSMartin Matuska #include <string.h> 42eda14cbcSMatt Macy #include <unistd.h> 43eda14cbcSMatt Macy #include <stddef.h> 44eda14cbcSMatt Macy #include <fcntl.h> 45eda14cbcSMatt Macy #include <sys/mount.h> 46eda14cbcSMatt Macy #include <sys/mntent.h> 47eda14cbcSMatt Macy #include <sys/mnttab.h> 48eda14cbcSMatt Macy #include <sys/avl.h> 49eda14cbcSMatt Macy #include <sys/debug.h> 50eda14cbcSMatt Macy #include <sys/stat.h> 51eda14cbcSMatt Macy #include <pthread.h> 52eda14cbcSMatt Macy #include <umem.h> 53eda14cbcSMatt Macy #include <time.h> 54eda14cbcSMatt Macy 55eda14cbcSMatt Macy #include <libzfs.h> 56eda14cbcSMatt Macy #include <libzfs_core.h> 57eda14cbcSMatt Macy #include <libzutil.h> 58eda14cbcSMatt Macy 59eda14cbcSMatt Macy #include "zfs_namecheck.h" 60eda14cbcSMatt Macy #include "zfs_prop.h" 61eda14cbcSMatt Macy #include "zfs_fletcher.h" 62eda14cbcSMatt Macy #include "libzfs_impl.h" 63eda14cbcSMatt Macy #include <cityhash.h> 64eda14cbcSMatt Macy #include <zlib.h> 65eda14cbcSMatt Macy #include <sys/zio_checksum.h> 66eda14cbcSMatt Macy #include <sys/dsl_crypt.h> 67eda14cbcSMatt Macy #include <sys/ddt.h> 68eda14cbcSMatt Macy #include <sys/socket.h> 69eda14cbcSMatt Macy #include <sys/sha2.h> 70eda14cbcSMatt Macy 71eda14cbcSMatt Macy static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *, 72eda14cbcSMatt Macy recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, 73eda14cbcSMatt Macy const char *, nvlist_t *); 74eda14cbcSMatt Macy static int guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent, 75eda14cbcSMatt Macy uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids, 76eda14cbcSMatt Macy uint64_t num_redact_snaps, char *name); 77eda14cbcSMatt Macy static int guid_to_name(libzfs_handle_t *, const char *, 78eda14cbcSMatt Macy uint64_t, boolean_t, char *); 79eda14cbcSMatt Macy 80eda14cbcSMatt Macy typedef struct progress_arg { 81eda14cbcSMatt Macy zfs_handle_t *pa_zhp; 82eda14cbcSMatt Macy int pa_fd; 83eda14cbcSMatt Macy boolean_t pa_parsable; 84eda14cbcSMatt Macy boolean_t pa_estimate; 85eda14cbcSMatt Macy int pa_verbosity; 86eda14cbcSMatt Macy } progress_arg_t; 87eda14cbcSMatt Macy 88eda14cbcSMatt Macy static int 89c03c5b1cSMartin Matuska dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len, 90eda14cbcSMatt Macy zio_cksum_t *zc, int outfd) 91eda14cbcSMatt Macy { 92eda14cbcSMatt Macy ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), 93eda14cbcSMatt Macy ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t)); 94eda14cbcSMatt Macy fletcher_4_incremental_native(drr, 95eda14cbcSMatt Macy offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc); 96eda14cbcSMatt Macy if (drr->drr_type != DRR_BEGIN) { 97eda14cbcSMatt Macy ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u. 98eda14cbcSMatt Macy drr_checksum.drr_checksum)); 99eda14cbcSMatt Macy drr->drr_u.drr_checksum.drr_checksum = *zc; 100eda14cbcSMatt Macy } 101eda14cbcSMatt Macy fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum, 102eda14cbcSMatt Macy sizeof (zio_cksum_t), zc); 103eda14cbcSMatt Macy if (write(outfd, drr, sizeof (*drr)) == -1) 104eda14cbcSMatt Macy return (errno); 105eda14cbcSMatt Macy if (payload_len != 0) { 106eda14cbcSMatt Macy fletcher_4_incremental_native(payload, payload_len, zc); 107eda14cbcSMatt Macy if (write(outfd, payload, payload_len) == -1) 108eda14cbcSMatt Macy return (errno); 109eda14cbcSMatt Macy } 110eda14cbcSMatt Macy return (0); 111eda14cbcSMatt Macy } 112eda14cbcSMatt Macy 113eda14cbcSMatt Macy /* 114eda14cbcSMatt Macy * Routines for dealing with the AVL tree of fs-nvlists 115eda14cbcSMatt Macy */ 116eda14cbcSMatt Macy typedef struct fsavl_node { 117eda14cbcSMatt Macy avl_node_t fn_node; 118eda14cbcSMatt Macy nvlist_t *fn_nvfs; 119eda14cbcSMatt Macy char *fn_snapname; 120eda14cbcSMatt Macy uint64_t fn_guid; 121eda14cbcSMatt Macy } fsavl_node_t; 122eda14cbcSMatt Macy 123eda14cbcSMatt Macy static int 124eda14cbcSMatt Macy fsavl_compare(const void *arg1, const void *arg2) 125eda14cbcSMatt Macy { 126eda14cbcSMatt Macy const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1; 127eda14cbcSMatt Macy const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2; 128eda14cbcSMatt Macy 129eda14cbcSMatt Macy return (TREE_CMP(fn1->fn_guid, fn2->fn_guid)); 130eda14cbcSMatt Macy } 131eda14cbcSMatt Macy 132eda14cbcSMatt Macy /* 133eda14cbcSMatt Macy * Given the GUID of a snapshot, find its containing filesystem and 134eda14cbcSMatt Macy * (optionally) name. 135eda14cbcSMatt Macy */ 136eda14cbcSMatt Macy static nvlist_t * 137eda14cbcSMatt Macy fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname) 138eda14cbcSMatt Macy { 139eda14cbcSMatt Macy fsavl_node_t fn_find; 140eda14cbcSMatt Macy fsavl_node_t *fn; 141eda14cbcSMatt Macy 142eda14cbcSMatt Macy fn_find.fn_guid = snapguid; 143eda14cbcSMatt Macy 144eda14cbcSMatt Macy fn = avl_find(avl, &fn_find, NULL); 145eda14cbcSMatt Macy if (fn) { 146eda14cbcSMatt Macy if (snapname) 147eda14cbcSMatt Macy *snapname = fn->fn_snapname; 148eda14cbcSMatt Macy return (fn->fn_nvfs); 149eda14cbcSMatt Macy } 150eda14cbcSMatt Macy return (NULL); 151eda14cbcSMatt Macy } 152eda14cbcSMatt Macy 153eda14cbcSMatt Macy static void 154eda14cbcSMatt Macy fsavl_destroy(avl_tree_t *avl) 155eda14cbcSMatt Macy { 156eda14cbcSMatt Macy fsavl_node_t *fn; 157eda14cbcSMatt Macy void *cookie; 158eda14cbcSMatt Macy 159eda14cbcSMatt Macy if (avl == NULL) 160eda14cbcSMatt Macy return; 161eda14cbcSMatt Macy 162eda14cbcSMatt Macy cookie = NULL; 163eda14cbcSMatt Macy while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL) 164eda14cbcSMatt Macy free(fn); 165eda14cbcSMatt Macy avl_destroy(avl); 166eda14cbcSMatt Macy free(avl); 167eda14cbcSMatt Macy } 168eda14cbcSMatt Macy 169eda14cbcSMatt Macy /* 170eda14cbcSMatt Macy * Given an nvlist, produce an avl tree of snapshots, ordered by guid 171eda14cbcSMatt Macy */ 172eda14cbcSMatt Macy static avl_tree_t * 173eda14cbcSMatt Macy fsavl_create(nvlist_t *fss) 174eda14cbcSMatt Macy { 175eda14cbcSMatt Macy avl_tree_t *fsavl; 176eda14cbcSMatt Macy nvpair_t *fselem = NULL; 177eda14cbcSMatt Macy 178eda14cbcSMatt Macy if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL) 179eda14cbcSMatt Macy return (NULL); 180eda14cbcSMatt Macy 181eda14cbcSMatt Macy avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t), 182eda14cbcSMatt Macy offsetof(fsavl_node_t, fn_node)); 183eda14cbcSMatt Macy 184eda14cbcSMatt Macy while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) { 185eda14cbcSMatt Macy nvlist_t *nvfs, *snaps; 186eda14cbcSMatt Macy nvpair_t *snapelem = NULL; 187eda14cbcSMatt Macy 188184c1b94SMartin Matuska nvfs = fnvpair_value_nvlist(fselem); 189184c1b94SMartin Matuska snaps = fnvlist_lookup_nvlist(nvfs, "snaps"); 190eda14cbcSMatt Macy 191eda14cbcSMatt Macy while ((snapelem = 192eda14cbcSMatt Macy nvlist_next_nvpair(snaps, snapelem)) != NULL) { 193eda14cbcSMatt Macy fsavl_node_t *fn; 194eda14cbcSMatt Macy 195eda14cbcSMatt Macy if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) { 196eda14cbcSMatt Macy fsavl_destroy(fsavl); 197eda14cbcSMatt Macy return (NULL); 198eda14cbcSMatt Macy } 199eda14cbcSMatt Macy fn->fn_nvfs = nvfs; 200eda14cbcSMatt Macy fn->fn_snapname = nvpair_name(snapelem); 201c03c5b1cSMartin Matuska fn->fn_guid = fnvpair_value_uint64(snapelem); 202eda14cbcSMatt Macy 203eda14cbcSMatt Macy /* 204eda14cbcSMatt Macy * Note: if there are multiple snaps with the 205eda14cbcSMatt Macy * same GUID, we ignore all but one. 206eda14cbcSMatt Macy */ 207c03c5b1cSMartin Matuska avl_index_t where = 0; 208c03c5b1cSMartin Matuska if (avl_find(fsavl, fn, &where) == NULL) 209c03c5b1cSMartin Matuska avl_insert(fsavl, fn, where); 210eda14cbcSMatt Macy else 211eda14cbcSMatt Macy free(fn); 212eda14cbcSMatt Macy } 213eda14cbcSMatt Macy } 214eda14cbcSMatt Macy 215eda14cbcSMatt Macy return (fsavl); 216eda14cbcSMatt Macy } 217eda14cbcSMatt Macy 218eda14cbcSMatt Macy /* 219eda14cbcSMatt Macy * Routines for dealing with the giant nvlist of fs-nvlists, etc. 220eda14cbcSMatt Macy */ 221eda14cbcSMatt Macy typedef struct send_data { 222eda14cbcSMatt Macy /* 223eda14cbcSMatt Macy * assigned inside every recursive call, 224eda14cbcSMatt Macy * restored from *_save on return: 225eda14cbcSMatt Macy * 226eda14cbcSMatt Macy * guid of fromsnap snapshot in parent dataset 227eda14cbcSMatt Macy * txg of fromsnap snapshot in current dataset 228eda14cbcSMatt Macy * txg of tosnap snapshot in current dataset 229eda14cbcSMatt Macy */ 230eda14cbcSMatt Macy 231eda14cbcSMatt Macy uint64_t parent_fromsnap_guid; 232eda14cbcSMatt Macy uint64_t fromsnap_txg; 233eda14cbcSMatt Macy uint64_t tosnap_txg; 234eda14cbcSMatt Macy 235eda14cbcSMatt Macy /* the nvlists get accumulated during depth-first traversal */ 236eda14cbcSMatt Macy nvlist_t *parent_snaps; 237eda14cbcSMatt Macy nvlist_t *fss; 238eda14cbcSMatt Macy nvlist_t *snapprops; 239eda14cbcSMatt Macy nvlist_t *snapholds; /* user holds */ 240eda14cbcSMatt Macy 241eda14cbcSMatt Macy /* send-receive configuration, does not change during traversal */ 242eda14cbcSMatt Macy const char *fsname; 243eda14cbcSMatt Macy const char *fromsnap; 244eda14cbcSMatt Macy const char *tosnap; 245eda14cbcSMatt Macy boolean_t recursive; 246eda14cbcSMatt Macy boolean_t raw; 247eda14cbcSMatt Macy boolean_t doall; 248eda14cbcSMatt Macy boolean_t replicate; 24916038816SMartin Matuska boolean_t skipmissing; 250eda14cbcSMatt Macy boolean_t verbose; 251eda14cbcSMatt Macy boolean_t backup; 252eda14cbcSMatt Macy boolean_t seenfrom; 253eda14cbcSMatt Macy boolean_t seento; 254eda14cbcSMatt Macy boolean_t holds; /* were holds requested with send -h */ 255eda14cbcSMatt Macy boolean_t props; 256eda14cbcSMatt Macy 257eda14cbcSMatt Macy /* 258eda14cbcSMatt Macy * The header nvlist is of the following format: 259eda14cbcSMatt Macy * { 260eda14cbcSMatt Macy * "tosnap" -> string 261eda14cbcSMatt Macy * "fromsnap" -> string (if incremental) 262eda14cbcSMatt Macy * "fss" -> { 263eda14cbcSMatt Macy * id -> { 264eda14cbcSMatt Macy * 265eda14cbcSMatt Macy * "name" -> string (full name; for debugging) 266eda14cbcSMatt Macy * "parentfromsnap" -> number (guid of fromsnap in parent) 267eda14cbcSMatt Macy * 268eda14cbcSMatt Macy * "props" -> { name -> value (only if set here) } 269eda14cbcSMatt Macy * "snaps" -> { name (lastname) -> number (guid) } 270eda14cbcSMatt Macy * "snapprops" -> { name (lastname) -> { name -> value } } 271eda14cbcSMatt Macy * "snapholds" -> { name (lastname) -> { holdname -> crtime } } 272eda14cbcSMatt Macy * 273eda14cbcSMatt Macy * "origin" -> number (guid) (if clone) 274eda14cbcSMatt Macy * "is_encroot" -> boolean 275eda14cbcSMatt Macy * "sent" -> boolean (not on-disk) 276eda14cbcSMatt Macy * } 277eda14cbcSMatt Macy * } 278eda14cbcSMatt Macy * } 279eda14cbcSMatt Macy * 280eda14cbcSMatt Macy */ 281eda14cbcSMatt Macy } send_data_t; 282eda14cbcSMatt Macy 283eda14cbcSMatt Macy static void 284eda14cbcSMatt Macy send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv); 285eda14cbcSMatt Macy 286c03c5b1cSMartin Matuska /* 287c03c5b1cSMartin Matuska * Collect guid, valid props, optionally holds, etc. of a snapshot. 288c03c5b1cSMartin Matuska * This interface is intended for use as a zfs_iter_snapshots_sorted visitor. 289c03c5b1cSMartin Matuska */ 290eda14cbcSMatt Macy static int 291eda14cbcSMatt Macy send_iterate_snap(zfs_handle_t *zhp, void *arg) 292eda14cbcSMatt Macy { 293eda14cbcSMatt Macy send_data_t *sd = arg; 294eda14cbcSMatt Macy uint64_t guid = zhp->zfs_dmustats.dds_guid; 295eda14cbcSMatt Macy uint64_t txg = zhp->zfs_dmustats.dds_creation_txg; 296eda14cbcSMatt Macy boolean_t isfromsnap, istosnap, istosnapwithnofrom; 297c03c5b1cSMartin Matuska char *snapname; 298c03c5b1cSMartin Matuska const char *from = sd->fromsnap; 299c03c5b1cSMartin Matuska const char *to = sd->tosnap; 300eda14cbcSMatt Macy 301c03c5b1cSMartin Matuska snapname = strrchr(zhp->zfs_name, '@'); 302c03c5b1cSMartin Matuska assert(snapname != NULL); 303c03c5b1cSMartin Matuska ++snapname; 304c03c5b1cSMartin Matuska 305c03c5b1cSMartin Matuska isfromsnap = (from != NULL && strcmp(from, snapname) == 0); 306c03c5b1cSMartin Matuska istosnap = (to != NULL && strcmp(to, snapname) == 0); 307c03c5b1cSMartin Matuska istosnapwithnofrom = (istosnap && from == NULL); 308eda14cbcSMatt Macy 309eda14cbcSMatt Macy if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) { 310eda14cbcSMatt Macy if (sd->verbose) { 311eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 312eda14cbcSMatt Macy "skipping snapshot %s because it was created " 313eda14cbcSMatt Macy "after the destination snapshot (%s)\n"), 314c03c5b1cSMartin Matuska zhp->zfs_name, to); 315eda14cbcSMatt Macy } 316eda14cbcSMatt Macy zfs_close(zhp); 317eda14cbcSMatt Macy return (0); 318eda14cbcSMatt Macy } 319eda14cbcSMatt Macy 320184c1b94SMartin Matuska fnvlist_add_uint64(sd->parent_snaps, snapname, guid); 321c03c5b1cSMartin Matuska 322eda14cbcSMatt Macy /* 323eda14cbcSMatt Macy * NB: if there is no fromsnap here (it's a newly created fs in 324eda14cbcSMatt Macy * an incremental replication), we will substitute the tosnap. 325eda14cbcSMatt Macy */ 326c03c5b1cSMartin Matuska if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) 327eda14cbcSMatt Macy sd->parent_fromsnap_guid = guid; 328eda14cbcSMatt Macy 329eda14cbcSMatt Macy if (!sd->recursive) { 330caed7b1cSMartin Matuska /* 331caed7b1cSMartin Matuska * To allow a doall stream to work properly 332caed7b1cSMartin Matuska * with a NULL fromsnap 333caed7b1cSMartin Matuska */ 334c03c5b1cSMartin Matuska if (sd->doall && from == NULL && !sd->seenfrom) 335caed7b1cSMartin Matuska sd->seenfrom = B_TRUE; 336caed7b1cSMartin Matuska 337eda14cbcSMatt Macy if (!sd->seenfrom && isfromsnap) { 338eda14cbcSMatt Macy sd->seenfrom = B_TRUE; 339eda14cbcSMatt Macy zfs_close(zhp); 340eda14cbcSMatt Macy return (0); 341eda14cbcSMatt Macy } 342eda14cbcSMatt Macy 343eda14cbcSMatt Macy if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) { 344eda14cbcSMatt Macy zfs_close(zhp); 345eda14cbcSMatt Macy return (0); 346eda14cbcSMatt Macy } 347eda14cbcSMatt Macy 348eda14cbcSMatt Macy if (istosnap) 349eda14cbcSMatt Macy sd->seento = B_TRUE; 350eda14cbcSMatt Macy } 351eda14cbcSMatt Macy 352c03c5b1cSMartin Matuska nvlist_t *nv = fnvlist_alloc(); 353eda14cbcSMatt Macy send_iterate_prop(zhp, sd->backup, nv); 354184c1b94SMartin Matuska fnvlist_add_nvlist(sd->snapprops, snapname, nv); 355184c1b94SMartin Matuska fnvlist_free(nv); 356c03c5b1cSMartin Matuska 357eda14cbcSMatt Macy if (sd->holds) { 358c03c5b1cSMartin Matuska nvlist_t *holds; 359c03c5b1cSMartin Matuska if (lzc_get_holds(zhp->zfs_name, &holds) == 0) { 360184c1b94SMartin Matuska fnvlist_add_nvlist(sd->snapholds, snapname, holds); 361eda14cbcSMatt Macy fnvlist_free(holds); 362eda14cbcSMatt Macy } 363c03c5b1cSMartin Matuska } 364eda14cbcSMatt Macy 365eda14cbcSMatt Macy zfs_close(zhp); 366eda14cbcSMatt Macy return (0); 367eda14cbcSMatt Macy } 368eda14cbcSMatt Macy 369c03c5b1cSMartin Matuska /* 370c03c5b1cSMartin Matuska * Collect all valid props from the handle snap into an nvlist. 371c03c5b1cSMartin Matuska */ 372eda14cbcSMatt Macy static void 373eda14cbcSMatt Macy send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv) 374eda14cbcSMatt Macy { 375c03c5b1cSMartin Matuska nvlist_t *props; 376eda14cbcSMatt Macy 377eda14cbcSMatt Macy if (received_only) 378eda14cbcSMatt Macy props = zfs_get_recvd_props(zhp); 379eda14cbcSMatt Macy else 380eda14cbcSMatt Macy props = zhp->zfs_props; 381eda14cbcSMatt Macy 382c03c5b1cSMartin Matuska nvpair_t *elem = NULL; 383eda14cbcSMatt Macy while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 384eda14cbcSMatt Macy char *propname = nvpair_name(elem); 385eda14cbcSMatt Macy zfs_prop_t prop = zfs_name_to_prop(propname); 386eda14cbcSMatt Macy 387eda14cbcSMatt Macy if (!zfs_prop_user(propname)) { 388eda14cbcSMatt Macy /* 389eda14cbcSMatt Macy * Realistically, this should never happen. However, 390eda14cbcSMatt Macy * we want the ability to add DSL properties without 391eda14cbcSMatt Macy * needing to make incompatible version changes. We 392eda14cbcSMatt Macy * need to ignore unknown properties to allow older 393eda14cbcSMatt Macy * software to still send datasets containing these 394eda14cbcSMatt Macy * properties, with the unknown properties elided. 395eda14cbcSMatt Macy */ 396eda14cbcSMatt Macy if (prop == ZPROP_INVAL) 397eda14cbcSMatt Macy continue; 398eda14cbcSMatt Macy 399eda14cbcSMatt Macy if (zfs_prop_readonly(prop)) 400eda14cbcSMatt Macy continue; 401eda14cbcSMatt Macy } 402eda14cbcSMatt Macy 403c03c5b1cSMartin Matuska nvlist_t *propnv = fnvpair_value_nvlist(elem); 404c03c5b1cSMartin Matuska 405c03c5b1cSMartin Matuska boolean_t isspacelimit = (prop == ZFS_PROP_QUOTA || 406c03c5b1cSMartin Matuska prop == ZFS_PROP_RESERVATION || 407eda14cbcSMatt Macy prop == ZFS_PROP_REFQUOTA || 408c03c5b1cSMartin Matuska prop == ZFS_PROP_REFRESERVATION); 409c03c5b1cSMartin Matuska if (isspacelimit && zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 410eda14cbcSMatt Macy continue; 411c03c5b1cSMartin Matuska 412c03c5b1cSMartin Matuska char *source; 413c03c5b1cSMartin Matuska if (nvlist_lookup_string(propnv, ZPROP_SOURCE, &source) == 0) { 414c03c5b1cSMartin Matuska if (strcmp(source, zhp->zfs_name) != 0 && 415c03c5b1cSMartin Matuska strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0) 416c03c5b1cSMartin Matuska continue; 417c03c5b1cSMartin Matuska } else { 418eda14cbcSMatt Macy /* 419eda14cbcSMatt Macy * May have no source before SPA_VERSION_RECVD_PROPS, 420eda14cbcSMatt Macy * but is still modifiable. 421eda14cbcSMatt Macy */ 422c03c5b1cSMartin Matuska if (!isspacelimit) 423eda14cbcSMatt Macy continue; 424eda14cbcSMatt Macy } 425eda14cbcSMatt Macy 426eda14cbcSMatt Macy if (zfs_prop_user(propname) || 427eda14cbcSMatt Macy zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 428eda14cbcSMatt Macy char *value; 429184c1b94SMartin Matuska value = fnvlist_lookup_string(propnv, ZPROP_VALUE); 430184c1b94SMartin Matuska fnvlist_add_string(nv, propname, value); 431eda14cbcSMatt Macy } else { 432eda14cbcSMatt Macy uint64_t value; 433184c1b94SMartin Matuska value = fnvlist_lookup_uint64(propnv, ZPROP_VALUE); 434184c1b94SMartin Matuska fnvlist_add_uint64(nv, propname, value); 435eda14cbcSMatt Macy } 436eda14cbcSMatt Macy } 437eda14cbcSMatt Macy } 438eda14cbcSMatt Macy 439eda14cbcSMatt Macy /* 440*271171e0SMartin Matuska * returns snapshot guid 441*271171e0SMartin Matuska * and returns 0 if the snapshot does not exist 442*271171e0SMartin Matuska */ 443*271171e0SMartin Matuska static uint64_t 444*271171e0SMartin Matuska get_snap_guid(libzfs_handle_t *hdl, const char *fs, const char *snap) 445*271171e0SMartin Matuska { 446*271171e0SMartin Matuska char name[MAXPATHLEN + 1]; 447*271171e0SMartin Matuska uint64_t guid = 0; 448*271171e0SMartin Matuska 449*271171e0SMartin Matuska if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0') 450*271171e0SMartin Matuska return (guid); 451*271171e0SMartin Matuska 452*271171e0SMartin Matuska (void) snprintf(name, sizeof (name), "%s@%s", fs, snap); 453*271171e0SMartin Matuska zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT); 454*271171e0SMartin Matuska if (zhp != NULL) { 455*271171e0SMartin Matuska guid = zfs_prop_get_int(zhp, ZFS_PROP_GUID); 456*271171e0SMartin Matuska zfs_close(zhp); 457*271171e0SMartin Matuska } 458*271171e0SMartin Matuska 459*271171e0SMartin Matuska return (guid); 460*271171e0SMartin Matuska } 461*271171e0SMartin Matuska 462*271171e0SMartin Matuska /* 463eda14cbcSMatt Macy * returns snapshot creation txg 464eda14cbcSMatt Macy * and returns 0 if the snapshot does not exist 465eda14cbcSMatt Macy */ 466eda14cbcSMatt Macy static uint64_t 467eda14cbcSMatt Macy get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap) 468eda14cbcSMatt Macy { 469eda14cbcSMatt Macy char name[ZFS_MAX_DATASET_NAME_LEN]; 470eda14cbcSMatt Macy uint64_t txg = 0; 471eda14cbcSMatt Macy 472eda14cbcSMatt Macy if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0') 473eda14cbcSMatt Macy return (txg); 474eda14cbcSMatt Macy 475eda14cbcSMatt Macy (void) snprintf(name, sizeof (name), "%s@%s", fs, snap); 476eda14cbcSMatt Macy if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) { 477eda14cbcSMatt Macy zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT); 478eda14cbcSMatt Macy if (zhp != NULL) { 479eda14cbcSMatt Macy txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG); 480eda14cbcSMatt Macy zfs_close(zhp); 481eda14cbcSMatt Macy } 482eda14cbcSMatt Macy } 483eda14cbcSMatt Macy 484eda14cbcSMatt Macy return (txg); 485eda14cbcSMatt Macy } 486eda14cbcSMatt Macy 487eda14cbcSMatt Macy /* 488c03c5b1cSMartin Matuska * Recursively generate nvlists describing datasets. See comment 489eda14cbcSMatt Macy * for the data structure send_data_t above for description of contents 490eda14cbcSMatt Macy * of the nvlist. 491eda14cbcSMatt Macy */ 492eda14cbcSMatt Macy static int 493eda14cbcSMatt Macy send_iterate_fs(zfs_handle_t *zhp, void *arg) 494eda14cbcSMatt Macy { 495eda14cbcSMatt Macy send_data_t *sd = arg; 496eda14cbcSMatt Macy nvlist_t *nvfs = NULL, *nv = NULL; 497eda14cbcSMatt Macy int rv = 0; 498eda14cbcSMatt Macy uint64_t min_txg = 0, max_txg = 0; 499eda14cbcSMatt Macy uint64_t txg = zhp->zfs_dmustats.dds_creation_txg; 500eda14cbcSMatt Macy uint64_t guid = zhp->zfs_dmustats.dds_guid; 501eda14cbcSMatt Macy uint64_t fromsnap_txg, tosnap_txg; 502eda14cbcSMatt Macy char guidstring[64]; 503eda14cbcSMatt Macy 504c03c5b1cSMartin Matuska /* These fields are restored on return from a recursive call. */ 505c03c5b1cSMartin Matuska uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid; 506c03c5b1cSMartin Matuska uint64_t fromsnap_txg_save = sd->fromsnap_txg; 507c03c5b1cSMartin Matuska uint64_t tosnap_txg_save = sd->tosnap_txg; 508c03c5b1cSMartin Matuska 509eda14cbcSMatt Macy fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap); 510eda14cbcSMatt Macy if (fromsnap_txg != 0) 511eda14cbcSMatt Macy sd->fromsnap_txg = fromsnap_txg; 512eda14cbcSMatt Macy 513eda14cbcSMatt Macy tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap); 514eda14cbcSMatt Macy if (tosnap_txg != 0) 515eda14cbcSMatt Macy sd->tosnap_txg = tosnap_txg; 516eda14cbcSMatt Macy 517eda14cbcSMatt Macy /* 518c03c5b1cSMartin Matuska * On the send side, if the current dataset does not have tosnap, 519eda14cbcSMatt Macy * perform two additional checks: 520eda14cbcSMatt Macy * 521c03c5b1cSMartin Matuska * - Skip sending the current dataset if it was created later than 522c03c5b1cSMartin Matuska * the parent tosnap. 523c03c5b1cSMartin Matuska * - Return error if the current dataset was created earlier than 52416038816SMartin Matuska * the parent tosnap, unless --skip-missing specified. Then 525c03c5b1cSMartin Matuska * just print a warning. 526eda14cbcSMatt Macy */ 527eda14cbcSMatt Macy if (sd->tosnap != NULL && tosnap_txg == 0) { 528eda14cbcSMatt Macy if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) { 529eda14cbcSMatt Macy if (sd->verbose) { 530eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 531eda14cbcSMatt Macy "skipping dataset %s: snapshot %s does " 532eda14cbcSMatt Macy "not exist\n"), zhp->zfs_name, sd->tosnap); 533eda14cbcSMatt Macy } 53416038816SMartin Matuska } else if (sd->skipmissing) { 53516038816SMartin Matuska (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 53616038816SMartin Matuska "WARNING: skipping dataset %s and its children:" 53716038816SMartin Matuska " snapshot %s does not exist\n"), 53816038816SMartin Matuska zhp->zfs_name, sd->tosnap); 539eda14cbcSMatt Macy } else { 540eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 541eda14cbcSMatt Macy "cannot send %s@%s%s: snapshot %s@%s does not " 542eda14cbcSMatt Macy "exist\n"), sd->fsname, sd->tosnap, sd->recursive ? 543eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, " recursively") : "", 544eda14cbcSMatt Macy zhp->zfs_name, sd->tosnap); 545eda14cbcSMatt Macy rv = EZFS_NOENT; 546eda14cbcSMatt Macy } 547eda14cbcSMatt Macy goto out; 548eda14cbcSMatt Macy } 549eda14cbcSMatt Macy 550eda14cbcSMatt Macy nvfs = fnvlist_alloc(); 551eda14cbcSMatt Macy fnvlist_add_string(nvfs, "name", zhp->zfs_name); 552c03c5b1cSMartin Matuska fnvlist_add_uint64(nvfs, "parentfromsnap", sd->parent_fromsnap_guid); 553eda14cbcSMatt Macy 554c03c5b1cSMartin Matuska if (zhp->zfs_dmustats.dds_origin[0] != '\0') { 555eda14cbcSMatt Macy zfs_handle_t *origin = zfs_open(zhp->zfs_hdl, 556eda14cbcSMatt Macy zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 557eda14cbcSMatt Macy if (origin == NULL) { 558eda14cbcSMatt Macy rv = -1; 559eda14cbcSMatt Macy goto out; 560eda14cbcSMatt Macy } 561eda14cbcSMatt Macy fnvlist_add_uint64(nvfs, "origin", 562eda14cbcSMatt Macy origin->zfs_dmustats.dds_guid); 563eda14cbcSMatt Macy zfs_close(origin); 564eda14cbcSMatt Macy } 565eda14cbcSMatt Macy 566c03c5b1cSMartin Matuska /* Iterate over props. */ 567eda14cbcSMatt Macy if (sd->props || sd->backup || sd->recursive) { 568eda14cbcSMatt Macy nv = fnvlist_alloc(); 569eda14cbcSMatt Macy send_iterate_prop(zhp, sd->backup, nv); 570c03c5b1cSMartin Matuska fnvlist_add_nvlist(nvfs, "props", nv); 571eda14cbcSMatt Macy } 572eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) { 573eda14cbcSMatt Macy boolean_t encroot; 574eda14cbcSMatt Macy 575c03c5b1cSMartin Matuska /* Determine if this dataset is an encryption root. */ 576eda14cbcSMatt Macy if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) { 577eda14cbcSMatt Macy rv = -1; 578eda14cbcSMatt Macy goto out; 579eda14cbcSMatt Macy } 580eda14cbcSMatt Macy 581eda14cbcSMatt Macy if (encroot) 582eda14cbcSMatt Macy fnvlist_add_boolean(nvfs, "is_encroot"); 583eda14cbcSMatt Macy 584eda14cbcSMatt Macy /* 585eda14cbcSMatt Macy * Encrypted datasets can only be sent with properties if 586eda14cbcSMatt Macy * the raw flag is specified because the receive side doesn't 587eda14cbcSMatt Macy * currently have a mechanism for recursively asking the user 588eda14cbcSMatt Macy * for new encryption parameters. 589eda14cbcSMatt Macy */ 590eda14cbcSMatt Macy if (!sd->raw) { 591eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 592eda14cbcSMatt Macy "cannot send %s@%s: encrypted dataset %s may not " 593eda14cbcSMatt Macy "be sent with properties without the raw flag\n"), 594eda14cbcSMatt Macy sd->fsname, sd->tosnap, zhp->zfs_name); 595eda14cbcSMatt Macy rv = -1; 596eda14cbcSMatt Macy goto out; 597eda14cbcSMatt Macy } 598eda14cbcSMatt Macy 599eda14cbcSMatt Macy } 600eda14cbcSMatt Macy 601eda14cbcSMatt Macy /* 602c03c5b1cSMartin Matuska * Iterate over snaps, and set sd->parent_fromsnap_guid. 603c03c5b1cSMartin Matuska * 604eda14cbcSMatt Macy * If this is a "doall" send, a replicate send or we're just trying 605eda14cbcSMatt Macy * to gather a list of previous snapshots, iterate through all the 606eda14cbcSMatt Macy * snaps in the txg range. Otherwise just look at the one we're 607eda14cbcSMatt Macy * interested in. 608eda14cbcSMatt Macy */ 609c03c5b1cSMartin Matuska sd->parent_fromsnap_guid = 0; 610c03c5b1cSMartin Matuska sd->parent_snaps = fnvlist_alloc(); 611c03c5b1cSMartin Matuska sd->snapprops = fnvlist_alloc(); 612c03c5b1cSMartin Matuska if (sd->holds) 613c03c5b1cSMartin Matuska sd->snapholds = fnvlist_alloc(); 614eda14cbcSMatt Macy if (sd->doall || sd->replicate || sd->tosnap == NULL) { 615eda14cbcSMatt Macy if (!sd->replicate && fromsnap_txg != 0) 616eda14cbcSMatt Macy min_txg = fromsnap_txg; 617eda14cbcSMatt Macy if (!sd->replicate && tosnap_txg != 0) 618eda14cbcSMatt Macy max_txg = tosnap_txg; 619eda14cbcSMatt Macy (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd, 620eda14cbcSMatt Macy min_txg, max_txg); 621eda14cbcSMatt Macy } else { 622c03c5b1cSMartin Matuska char snapname[MAXPATHLEN]; 623eda14cbcSMatt Macy zfs_handle_t *snap; 624eda14cbcSMatt Macy 625eda14cbcSMatt Macy (void) snprintf(snapname, sizeof (snapname), "%s@%s", 626eda14cbcSMatt Macy zhp->zfs_name, sd->tosnap); 627eda14cbcSMatt Macy if (sd->fromsnap != NULL) 628eda14cbcSMatt Macy sd->seenfrom = B_TRUE; 629c03c5b1cSMartin Matuska snap = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT); 630eda14cbcSMatt Macy if (snap != NULL) 631eda14cbcSMatt Macy (void) send_iterate_snap(snap, sd); 632eda14cbcSMatt Macy } 633eda14cbcSMatt Macy 634eda14cbcSMatt Macy fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps); 635eda14cbcSMatt Macy fnvlist_free(sd->parent_snaps); 636c03c5b1cSMartin Matuska fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops); 637eda14cbcSMatt Macy fnvlist_free(sd->snapprops); 638c03c5b1cSMartin Matuska if (sd->holds) { 639c03c5b1cSMartin Matuska fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds); 640eda14cbcSMatt Macy fnvlist_free(sd->snapholds); 641c03c5b1cSMartin Matuska } 642eda14cbcSMatt Macy 643ac0bf12eSMatt Macy /* Do not allow the size of the properties list to exceed the limit */ 644ac0bf12eSMatt Macy if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) > 645ac0bf12eSMatt Macy zhp->zfs_hdl->libzfs_max_nvlist) { 646ac0bf12eSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 647ac0bf12eSMatt Macy "warning: cannot send %s@%s: the size of the list of " 648ac0bf12eSMatt Macy "snapshots and properties is too large to be received " 649ac0bf12eSMatt Macy "successfully.\n" 650ac0bf12eSMatt Macy "Select a smaller number of snapshots to send.\n"), 651ac0bf12eSMatt Macy zhp->zfs_name, sd->tosnap); 652ac0bf12eSMatt Macy rv = EZFS_NOSPC; 653ac0bf12eSMatt Macy goto out; 654ac0bf12eSMatt Macy } 655c03c5b1cSMartin Matuska /* Add this fs to nvlist. */ 656eda14cbcSMatt Macy (void) snprintf(guidstring, sizeof (guidstring), 657eda14cbcSMatt Macy "0x%llx", (longlong_t)guid); 658eda14cbcSMatt Macy fnvlist_add_nvlist(sd->fss, guidstring, nvfs); 659eda14cbcSMatt Macy 660c03c5b1cSMartin Matuska /* Iterate over children. */ 661eda14cbcSMatt Macy if (sd->recursive) 662eda14cbcSMatt Macy rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd); 663eda14cbcSMatt Macy 664eda14cbcSMatt Macy out: 665c03c5b1cSMartin Matuska /* Restore saved fields. */ 666eda14cbcSMatt Macy sd->parent_fromsnap_guid = parent_fromsnap_guid_save; 667eda14cbcSMatt Macy sd->fromsnap_txg = fromsnap_txg_save; 668eda14cbcSMatt Macy sd->tosnap_txg = tosnap_txg_save; 669c03c5b1cSMartin Matuska 670eda14cbcSMatt Macy fnvlist_free(nv); 671eda14cbcSMatt Macy fnvlist_free(nvfs); 672eda14cbcSMatt Macy 673eda14cbcSMatt Macy zfs_close(zhp); 674eda14cbcSMatt Macy return (rv); 675eda14cbcSMatt Macy } 676eda14cbcSMatt Macy 677eda14cbcSMatt Macy static int 678eda14cbcSMatt Macy gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap, 679eda14cbcSMatt Macy const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall, 68016038816SMartin Matuska boolean_t replicate, boolean_t skipmissing, boolean_t verbose, 68116038816SMartin Matuska boolean_t backup, boolean_t holds, boolean_t props, nvlist_t **nvlp, 68216038816SMartin Matuska avl_tree_t **avlp) 683eda14cbcSMatt Macy { 684eda14cbcSMatt Macy zfs_handle_t *zhp; 685eda14cbcSMatt Macy send_data_t sd = { 0 }; 686eda14cbcSMatt Macy int error; 687eda14cbcSMatt Macy 688eda14cbcSMatt Macy zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 689eda14cbcSMatt Macy if (zhp == NULL) 690eda14cbcSMatt Macy return (EZFS_BADTYPE); 691eda14cbcSMatt Macy 692184c1b94SMartin Matuska sd.fss = fnvlist_alloc(); 693eda14cbcSMatt Macy sd.fsname = fsname; 694eda14cbcSMatt Macy sd.fromsnap = fromsnap; 695eda14cbcSMatt Macy sd.tosnap = tosnap; 696eda14cbcSMatt Macy sd.recursive = recursive; 697eda14cbcSMatt Macy sd.raw = raw; 698eda14cbcSMatt Macy sd.doall = doall; 699eda14cbcSMatt Macy sd.replicate = replicate; 70016038816SMartin Matuska sd.skipmissing = skipmissing; 701eda14cbcSMatt Macy sd.verbose = verbose; 702eda14cbcSMatt Macy sd.backup = backup; 703eda14cbcSMatt Macy sd.holds = holds; 704eda14cbcSMatt Macy sd.props = props; 705eda14cbcSMatt Macy 706eda14cbcSMatt Macy if ((error = send_iterate_fs(zhp, &sd)) != 0) { 707184c1b94SMartin Matuska fnvlist_free(sd.fss); 708eda14cbcSMatt Macy if (avlp != NULL) 709eda14cbcSMatt Macy *avlp = NULL; 710eda14cbcSMatt Macy *nvlp = NULL; 711eda14cbcSMatt Macy return (error); 712eda14cbcSMatt Macy } 713eda14cbcSMatt Macy 714eda14cbcSMatt Macy if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) { 715184c1b94SMartin Matuska fnvlist_free(sd.fss); 716eda14cbcSMatt Macy *nvlp = NULL; 717eda14cbcSMatt Macy return (EZFS_NOMEM); 718eda14cbcSMatt Macy } 719eda14cbcSMatt Macy 720eda14cbcSMatt Macy *nvlp = sd.fss; 721eda14cbcSMatt Macy return (0); 722eda14cbcSMatt Macy } 723eda14cbcSMatt Macy 724eda14cbcSMatt Macy /* 725eda14cbcSMatt Macy * Routines specific to "zfs send" 726eda14cbcSMatt Macy */ 727eda14cbcSMatt Macy typedef struct send_dump_data { 728eda14cbcSMatt Macy /* these are all just the short snapname (the part after the @) */ 729eda14cbcSMatt Macy const char *fromsnap; 730eda14cbcSMatt Macy const char *tosnap; 731eda14cbcSMatt Macy char prevsnap[ZFS_MAX_DATASET_NAME_LEN]; 732eda14cbcSMatt Macy uint64_t prevsnap_obj; 733eda14cbcSMatt Macy boolean_t seenfrom, seento, replicate, doall, fromorigin; 734eda14cbcSMatt Macy boolean_t dryrun, parsable, progress, embed_data, std_out; 735eda14cbcSMatt Macy boolean_t large_block, compress, raw, holds; 736eda14cbcSMatt Macy int outfd; 737eda14cbcSMatt Macy boolean_t err; 738eda14cbcSMatt Macy nvlist_t *fss; 739eda14cbcSMatt Macy nvlist_t *snapholds; 740eda14cbcSMatt Macy avl_tree_t *fsavl; 741eda14cbcSMatt Macy snapfilter_cb_t *filter_cb; 742eda14cbcSMatt Macy void *filter_cb_arg; 743eda14cbcSMatt Macy nvlist_t *debugnv; 744eda14cbcSMatt Macy char holdtag[ZFS_MAX_DATASET_NAME_LEN]; 745eda14cbcSMatt Macy int cleanup_fd; 746eda14cbcSMatt Macy int verbosity; 747eda14cbcSMatt Macy uint64_t size; 748eda14cbcSMatt Macy } send_dump_data_t; 749eda14cbcSMatt Macy 750eda14cbcSMatt Macy static int 751eda14cbcSMatt Macy zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from, 752eda14cbcSMatt Macy enum lzc_send_flags flags, uint64_t *spacep) 753eda14cbcSMatt Macy { 754eda14cbcSMatt Macy assert(snapname != NULL); 755eda14cbcSMatt Macy 756c03c5b1cSMartin Matuska int error = lzc_send_space(snapname, from, flags, spacep); 757c03c5b1cSMartin Matuska if (error == 0) 758c03c5b1cSMartin Matuska return (0); 759c03c5b1cSMartin Matuska 7601f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 761eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 762eda14cbcSMatt Macy "warning: cannot estimate space for '%s'"), snapname); 763eda14cbcSMatt Macy 764c03c5b1cSMartin Matuska libzfs_handle_t *hdl = zhp->zfs_hdl; 765eda14cbcSMatt Macy switch (error) { 766eda14cbcSMatt Macy case EXDEV: 767eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 768eda14cbcSMatt Macy "not an earlier snapshot from the same fs")); 769eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 770eda14cbcSMatt Macy 771eda14cbcSMatt Macy case ENOENT: 772eda14cbcSMatt Macy if (zfs_dataset_exists(hdl, snapname, 773eda14cbcSMatt Macy ZFS_TYPE_SNAPSHOT)) { 774eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 775eda14cbcSMatt Macy "incremental source (%s) does not exist"), 776eda14cbcSMatt Macy snapname); 777eda14cbcSMatt Macy } 778eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOENT, errbuf)); 779eda14cbcSMatt Macy 780eda14cbcSMatt Macy case EDQUOT: 781eda14cbcSMatt Macy case EFBIG: 782eda14cbcSMatt Macy case EIO: 783eda14cbcSMatt Macy case ENOLINK: 784eda14cbcSMatt Macy case ENOSPC: 785eda14cbcSMatt Macy case ENOSTR: 786eda14cbcSMatt Macy case ENXIO: 787eda14cbcSMatt Macy case EPIPE: 788eda14cbcSMatt Macy case ERANGE: 789eda14cbcSMatt Macy case EFAULT: 790eda14cbcSMatt Macy case EROFS: 791eda14cbcSMatt Macy case EINVAL: 79216038816SMartin Matuska zfs_error_aux(hdl, "%s", strerror(error)); 793eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 794eda14cbcSMatt Macy 795eda14cbcSMatt Macy default: 796eda14cbcSMatt Macy return (zfs_standard_error(hdl, error, errbuf)); 797eda14cbcSMatt Macy } 798eda14cbcSMatt Macy } 799eda14cbcSMatt Macy 800eda14cbcSMatt Macy /* 801eda14cbcSMatt Macy * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 802eda14cbcSMatt Macy * NULL) to the file descriptor specified by outfd. 803eda14cbcSMatt Macy */ 804eda14cbcSMatt Macy static int 805eda14cbcSMatt Macy dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj, 806eda14cbcSMatt Macy boolean_t fromorigin, int outfd, enum lzc_send_flags flags, 807eda14cbcSMatt Macy nvlist_t *debugnv) 808eda14cbcSMatt Macy { 809eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 810eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zfs_hdl; 811eda14cbcSMatt Macy nvlist_t *thisdbg; 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 814eda14cbcSMatt Macy assert(fromsnap_obj == 0 || !fromorigin); 815eda14cbcSMatt Macy 816eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 817eda14cbcSMatt Macy zc.zc_cookie = outfd; 818eda14cbcSMatt Macy zc.zc_obj = fromorigin; 819eda14cbcSMatt Macy zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); 820eda14cbcSMatt Macy zc.zc_fromobj = fromsnap_obj; 821eda14cbcSMatt Macy zc.zc_flags = flags; 822eda14cbcSMatt Macy 823c03c5b1cSMartin Matuska if (debugnv != NULL) { 824184c1b94SMartin Matuska thisdbg = fnvlist_alloc(); 825c03c5b1cSMartin Matuska if (fromsnap != NULL && fromsnap[0] != '\0') 826184c1b94SMartin Matuska fnvlist_add_string(thisdbg, "fromsnap", fromsnap); 827eda14cbcSMatt Macy } 828eda14cbcSMatt Macy 829eda14cbcSMatt Macy if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) { 8301f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 831c03c5b1cSMartin Matuska int error = errno; 832eda14cbcSMatt Macy 833c03c5b1cSMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), "%s '%s'", 834c03c5b1cSMartin Matuska dgettext(TEXT_DOMAIN, "warning: cannot send"), 835c03c5b1cSMartin Matuska zhp->zfs_name); 836c03c5b1cSMartin Matuska 837c03c5b1cSMartin Matuska if (debugnv != NULL) { 838c03c5b1cSMartin Matuska fnvlist_add_uint64(thisdbg, "error", error); 839184c1b94SMartin Matuska fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg); 840184c1b94SMartin Matuska fnvlist_free(thisdbg); 841c03c5b1cSMartin Matuska } 842eda14cbcSMatt Macy 843c03c5b1cSMartin Matuska switch (error) { 844eda14cbcSMatt Macy case EXDEV: 845eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 846eda14cbcSMatt Macy "not an earlier snapshot from the same fs")); 847eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 848eda14cbcSMatt Macy 849eda14cbcSMatt Macy case EACCES: 850eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 851eda14cbcSMatt Macy "source key must be loaded")); 852eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 853eda14cbcSMatt Macy 854eda14cbcSMatt Macy case ENOENT: 855eda14cbcSMatt Macy if (zfs_dataset_exists(hdl, zc.zc_name, 856eda14cbcSMatt Macy ZFS_TYPE_SNAPSHOT)) { 857eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 858eda14cbcSMatt Macy "incremental source (@%s) does not exist"), 859eda14cbcSMatt Macy zc.zc_value); 860eda14cbcSMatt Macy } 861eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOENT, errbuf)); 862eda14cbcSMatt Macy 863eda14cbcSMatt Macy case EDQUOT: 864eda14cbcSMatt Macy case EFBIG: 865eda14cbcSMatt Macy case EIO: 866eda14cbcSMatt Macy case ENOLINK: 867eda14cbcSMatt Macy case ENOSPC: 868eda14cbcSMatt Macy case ENOSTR: 869eda14cbcSMatt Macy case ENXIO: 870eda14cbcSMatt Macy case EPIPE: 871eda14cbcSMatt Macy case ERANGE: 872eda14cbcSMatt Macy case EFAULT: 873eda14cbcSMatt Macy case EROFS: 8743ff01b23SMartin Matuska case EINVAL: 87516038816SMartin Matuska zfs_error_aux(hdl, "%s", strerror(errno)); 876eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 877a0b956f5SMartin Matuska case ENOTSUP: 878a0b956f5SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 879a0b956f5SMartin Matuska "large blocks detected but large_blocks feature " 880a0b956f5SMartin Matuska "is inactive; raw send unsupported")); 881a0b956f5SMartin Matuska return (zfs_error(hdl, EZFS_NOTSUP, errbuf)); 882eda14cbcSMatt Macy 883eda14cbcSMatt Macy default: 884eda14cbcSMatt Macy return (zfs_standard_error(hdl, errno, errbuf)); 885eda14cbcSMatt Macy } 886eda14cbcSMatt Macy } 887eda14cbcSMatt Macy 888c03c5b1cSMartin Matuska if (debugnv != NULL) { 889184c1b94SMartin Matuska fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg); 890184c1b94SMartin Matuska fnvlist_free(thisdbg); 891c03c5b1cSMartin Matuska } 892eda14cbcSMatt Macy 893eda14cbcSMatt Macy return (0); 894eda14cbcSMatt Macy } 895eda14cbcSMatt Macy 896eda14cbcSMatt Macy static void 897eda14cbcSMatt Macy gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd) 898eda14cbcSMatt Macy { 899eda14cbcSMatt Macy assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 900eda14cbcSMatt Macy 901eda14cbcSMatt Macy /* 902eda14cbcSMatt Macy * zfs_send() only sets snapholds for sends that need them, 903eda14cbcSMatt Macy * e.g. replication and doall. 904eda14cbcSMatt Macy */ 905eda14cbcSMatt Macy if (sdd->snapholds == NULL) 906eda14cbcSMatt Macy return; 907eda14cbcSMatt Macy 908eda14cbcSMatt Macy fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag); 909eda14cbcSMatt Macy } 910eda14cbcSMatt Macy 911eda14cbcSMatt Macy int 912eda14cbcSMatt Macy zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written, 913eda14cbcSMatt Macy uint64_t *blocks_visited) 914eda14cbcSMatt Macy { 915eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 916eda14cbcSMatt Macy 917c03c5b1cSMartin Matuska if (bytes_written != NULL) 918c03c5b1cSMartin Matuska *bytes_written = 0; 919c03c5b1cSMartin Matuska if (blocks_visited != NULL) 920c03c5b1cSMartin Matuska *blocks_visited = 0; 921eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 922eda14cbcSMatt Macy zc.zc_cookie = fd; 923eda14cbcSMatt Macy if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0) 924eda14cbcSMatt Macy return (errno); 925eda14cbcSMatt Macy if (bytes_written != NULL) 926eda14cbcSMatt Macy *bytes_written = zc.zc_cookie; 927eda14cbcSMatt Macy if (blocks_visited != NULL) 928eda14cbcSMatt Macy *blocks_visited = zc.zc_objset_type; 929eda14cbcSMatt Macy return (0); 930eda14cbcSMatt Macy } 931eda14cbcSMatt Macy 932eda14cbcSMatt Macy static void * 933eda14cbcSMatt Macy send_progress_thread(void *arg) 934eda14cbcSMatt Macy { 935eda14cbcSMatt Macy progress_arg_t *pa = arg; 936eda14cbcSMatt Macy zfs_handle_t *zhp = pa->pa_zhp; 937eda14cbcSMatt Macy uint64_t bytes; 938eda14cbcSMatt Macy uint64_t blocks; 939eda14cbcSMatt Macy char buf[16]; 940eda14cbcSMatt Macy time_t t; 941716fd348SMartin Matuska struct tm tm; 942c03c5b1cSMartin Matuska int err; 943c03c5b1cSMartin Matuska 944c03c5b1cSMartin Matuska if (!pa->pa_parsable) { 945c03c5b1cSMartin Matuska (void) fprintf(stderr, 946c03c5b1cSMartin Matuska "TIME %s %sSNAPSHOT %s\n", 947c03c5b1cSMartin Matuska pa->pa_estimate ? "BYTES" : " SENT", 948c03c5b1cSMartin Matuska pa->pa_verbosity >= 2 ? " BLOCKS " : "", 949c03c5b1cSMartin Matuska zhp->zfs_name); 950c03c5b1cSMartin Matuska } 951eda14cbcSMatt Macy 952eda14cbcSMatt Macy /* 953eda14cbcSMatt Macy * Print the progress from ZFS_IOC_SEND_PROGRESS every second. 954eda14cbcSMatt Macy */ 955eda14cbcSMatt Macy for (;;) { 956eda14cbcSMatt Macy (void) sleep(1); 957eda14cbcSMatt Macy if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes, 958eda14cbcSMatt Macy &blocks)) != 0) { 959eda14cbcSMatt Macy if (err == EINTR || err == ENOENT) 960eda14cbcSMatt Macy return ((void *)0); 961eda14cbcSMatt Macy return ((void *)(uintptr_t)err); 962eda14cbcSMatt Macy } 963eda14cbcSMatt Macy 964eda14cbcSMatt Macy (void) time(&t); 965716fd348SMartin Matuska localtime_r(&t, &tm); 966eda14cbcSMatt Macy 967eda14cbcSMatt Macy if (pa->pa_verbosity >= 2 && pa->pa_parsable) { 968eda14cbcSMatt Macy (void) fprintf(stderr, 969eda14cbcSMatt Macy "%02d:%02d:%02d\t%llu\t%llu\t%s\n", 970716fd348SMartin Matuska tm.tm_hour, tm.tm_min, tm.tm_sec, 971eda14cbcSMatt Macy (u_longlong_t)bytes, (u_longlong_t)blocks, 972eda14cbcSMatt Macy zhp->zfs_name); 973eda14cbcSMatt Macy } else if (pa->pa_verbosity >= 2) { 974eda14cbcSMatt Macy zfs_nicenum(bytes, buf, sizeof (buf)); 975eda14cbcSMatt Macy (void) fprintf(stderr, 976eda14cbcSMatt Macy "%02d:%02d:%02d %5s %8llu %s\n", 977716fd348SMartin Matuska tm.tm_hour, tm.tm_min, tm.tm_sec, 978eda14cbcSMatt Macy buf, (u_longlong_t)blocks, zhp->zfs_name); 979eda14cbcSMatt Macy } else if (pa->pa_parsable) { 980eda14cbcSMatt Macy (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n", 981716fd348SMartin Matuska tm.tm_hour, tm.tm_min, tm.tm_sec, 982eda14cbcSMatt Macy (u_longlong_t)bytes, zhp->zfs_name); 983eda14cbcSMatt Macy } else { 984eda14cbcSMatt Macy zfs_nicebytes(bytes, buf, sizeof (buf)); 985eda14cbcSMatt Macy (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n", 986716fd348SMartin Matuska tm.tm_hour, tm.tm_min, tm.tm_sec, 987eda14cbcSMatt Macy buf, zhp->zfs_name); 988eda14cbcSMatt Macy } 989eda14cbcSMatt Macy } 990eda14cbcSMatt Macy } 991eda14cbcSMatt Macy 992716fd348SMartin Matuska static boolean_t 993716fd348SMartin Matuska send_progress_thread_exit(libzfs_handle_t *hdl, pthread_t ptid) 994716fd348SMartin Matuska { 995716fd348SMartin Matuska void *status = NULL; 996716fd348SMartin Matuska (void) pthread_cancel(ptid); 997716fd348SMartin Matuska (void) pthread_join(ptid, &status); 998716fd348SMartin Matuska int error = (int)(uintptr_t)status; 999716fd348SMartin Matuska if (error != 0 && status != PTHREAD_CANCELED) 1000716fd348SMartin Matuska return (zfs_standard_error(hdl, error, 1001716fd348SMartin Matuska dgettext(TEXT_DOMAIN, "progress thread exited nonzero"))); 1002716fd348SMartin Matuska else 1003716fd348SMartin Matuska return (B_FALSE); 1004716fd348SMartin Matuska } 1005716fd348SMartin Matuska 1006eda14cbcSMatt Macy static void 1007eda14cbcSMatt Macy send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap, 1008eda14cbcSMatt Macy uint64_t size, boolean_t parsable) 1009eda14cbcSMatt Macy { 1010eda14cbcSMatt Macy if (parsable) { 1011eda14cbcSMatt Macy if (fromsnap != NULL) { 1012c03c5b1cSMartin Matuska (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1013c03c5b1cSMartin Matuska "incremental\t%s\t%s"), fromsnap, tosnap); 1014eda14cbcSMatt Macy } else { 1015c03c5b1cSMartin Matuska (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1016c03c5b1cSMartin Matuska "full\t%s"), tosnap); 1017eda14cbcSMatt Macy } 1018c03c5b1cSMartin Matuska (void) fprintf(fout, "\t%llu", (longlong_t)size); 1019eda14cbcSMatt Macy } else { 1020eda14cbcSMatt Macy if (fromsnap != NULL) { 1021eda14cbcSMatt Macy if (strchr(fromsnap, '@') == NULL && 1022eda14cbcSMatt Macy strchr(fromsnap, '#') == NULL) { 1023eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1024c03c5b1cSMartin Matuska "send from @%s to %s"), fromsnap, tosnap); 1025eda14cbcSMatt Macy } else { 1026eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1027c03c5b1cSMartin Matuska "send from %s to %s"), fromsnap, tosnap); 1028eda14cbcSMatt Macy } 1029eda14cbcSMatt Macy } else { 1030eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1031c03c5b1cSMartin Matuska "full send of %s"), tosnap); 1032eda14cbcSMatt Macy } 1033c03c5b1cSMartin Matuska if (size != 0) { 1034eda14cbcSMatt Macy char buf[16]; 1035eda14cbcSMatt Macy zfs_nicebytes(size, buf, sizeof (buf)); 1036eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1037eda14cbcSMatt Macy " estimated size is %s"), buf); 1038eda14cbcSMatt Macy } 1039c03c5b1cSMartin Matuska } 1040eda14cbcSMatt Macy (void) fprintf(fout, "\n"); 1041eda14cbcSMatt Macy } 1042eda14cbcSMatt Macy 1043c03c5b1cSMartin Matuska /* 1044c03c5b1cSMartin Matuska * Send a single filesystem snapshot, updating the send dump data. 1045c03c5b1cSMartin Matuska * This interface is intended for use as a zfs_iter_snapshots_sorted visitor. 1046c03c5b1cSMartin Matuska */ 1047eda14cbcSMatt Macy static int 1048eda14cbcSMatt Macy dump_snapshot(zfs_handle_t *zhp, void *arg) 1049eda14cbcSMatt Macy { 1050eda14cbcSMatt Macy send_dump_data_t *sdd = arg; 1051eda14cbcSMatt Macy progress_arg_t pa = { 0 }; 1052eda14cbcSMatt Macy pthread_t tid; 1053eda14cbcSMatt Macy char *thissnap; 1054eda14cbcSMatt Macy enum lzc_send_flags flags = 0; 1055eda14cbcSMatt Macy int err; 1056eda14cbcSMatt Macy boolean_t isfromsnap, istosnap, fromorigin; 1057eda14cbcSMatt Macy boolean_t exclude = B_FALSE; 1058eda14cbcSMatt Macy FILE *fout = sdd->std_out ? stdout : stderr; 1059eda14cbcSMatt Macy 1060eda14cbcSMatt Macy err = 0; 1061eda14cbcSMatt Macy thissnap = strchr(zhp->zfs_name, '@') + 1; 1062eda14cbcSMatt Macy isfromsnap = (sdd->fromsnap != NULL && 1063eda14cbcSMatt Macy strcmp(sdd->fromsnap, thissnap) == 0); 1064eda14cbcSMatt Macy 1065eda14cbcSMatt Macy if (!sdd->seenfrom && isfromsnap) { 1066eda14cbcSMatt Macy gather_holds(zhp, sdd); 1067eda14cbcSMatt Macy sdd->seenfrom = B_TRUE; 1068c03c5b1cSMartin Matuska (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap)); 1069eda14cbcSMatt Macy sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); 1070eda14cbcSMatt Macy zfs_close(zhp); 1071eda14cbcSMatt Macy return (0); 1072eda14cbcSMatt Macy } 1073eda14cbcSMatt Macy 1074eda14cbcSMatt Macy if (sdd->seento || !sdd->seenfrom) { 1075eda14cbcSMatt Macy zfs_close(zhp); 1076eda14cbcSMatt Macy return (0); 1077eda14cbcSMatt Macy } 1078eda14cbcSMatt Macy 1079eda14cbcSMatt Macy istosnap = (strcmp(sdd->tosnap, thissnap) == 0); 1080eda14cbcSMatt Macy if (istosnap) 1081eda14cbcSMatt Macy sdd->seento = B_TRUE; 1082eda14cbcSMatt Macy 1083eda14cbcSMatt Macy if (sdd->large_block) 1084eda14cbcSMatt Macy flags |= LZC_SEND_FLAG_LARGE_BLOCK; 1085eda14cbcSMatt Macy if (sdd->embed_data) 1086eda14cbcSMatt Macy flags |= LZC_SEND_FLAG_EMBED_DATA; 1087eda14cbcSMatt Macy if (sdd->compress) 1088eda14cbcSMatt Macy flags |= LZC_SEND_FLAG_COMPRESS; 1089eda14cbcSMatt Macy if (sdd->raw) 1090eda14cbcSMatt Macy flags |= LZC_SEND_FLAG_RAW; 1091eda14cbcSMatt Macy 1092eda14cbcSMatt Macy if (!sdd->doall && !isfromsnap && !istosnap) { 1093eda14cbcSMatt Macy if (sdd->replicate) { 1094eda14cbcSMatt Macy char *snapname; 1095eda14cbcSMatt Macy nvlist_t *snapprops; 1096eda14cbcSMatt Macy /* 1097eda14cbcSMatt Macy * Filter out all intermediate snapshots except origin 1098eda14cbcSMatt Macy * snapshots needed to replicate clones. 1099eda14cbcSMatt Macy */ 1100eda14cbcSMatt Macy nvlist_t *nvfs = fsavl_find(sdd->fsavl, 1101eda14cbcSMatt Macy zhp->zfs_dmustats.dds_guid, &snapname); 1102eda14cbcSMatt Macy 11036ba2210eSMartin Matuska if (nvfs != NULL) { 11046ba2210eSMartin Matuska snapprops = fnvlist_lookup_nvlist(nvfs, 11056ba2210eSMartin Matuska "snapprops"); 11066ba2210eSMartin Matuska snapprops = fnvlist_lookup_nvlist(snapprops, 11076ba2210eSMartin Matuska thissnap); 11086ba2210eSMartin Matuska exclude = !nvlist_exists(snapprops, 11096ba2210eSMartin Matuska "is_clone_origin"); 11106ba2210eSMartin Matuska } 1111eda14cbcSMatt Macy } else { 1112eda14cbcSMatt Macy exclude = B_TRUE; 1113eda14cbcSMatt Macy } 1114eda14cbcSMatt Macy } 1115eda14cbcSMatt Macy 1116eda14cbcSMatt Macy /* 1117eda14cbcSMatt Macy * If a filter function exists, call it to determine whether 1118eda14cbcSMatt Macy * this snapshot will be sent. 1119eda14cbcSMatt Macy */ 1120eda14cbcSMatt Macy if (exclude || (sdd->filter_cb != NULL && 1121eda14cbcSMatt Macy sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) { 1122eda14cbcSMatt Macy /* 1123eda14cbcSMatt Macy * This snapshot is filtered out. Don't send it, and don't 1124eda14cbcSMatt Macy * set prevsnap_obj, so it will be as if this snapshot didn't 1125eda14cbcSMatt Macy * exist, and the next accepted snapshot will be sent as 1126eda14cbcSMatt Macy * an incremental from the last accepted one, or as the 1127eda14cbcSMatt Macy * first (and full) snapshot in the case of a replication, 1128eda14cbcSMatt Macy * non-incremental send. 1129eda14cbcSMatt Macy */ 1130eda14cbcSMatt Macy zfs_close(zhp); 1131eda14cbcSMatt Macy return (0); 1132eda14cbcSMatt Macy } 1133eda14cbcSMatt Macy 1134eda14cbcSMatt Macy gather_holds(zhp, sdd); 1135eda14cbcSMatt Macy fromorigin = sdd->prevsnap[0] == '\0' && 1136eda14cbcSMatt Macy (sdd->fromorigin || sdd->replicate); 1137eda14cbcSMatt Macy 1138eda14cbcSMatt Macy if (sdd->verbosity != 0) { 1139eda14cbcSMatt Macy uint64_t size = 0; 1140eda14cbcSMatt Macy char fromds[ZFS_MAX_DATASET_NAME_LEN]; 1141eda14cbcSMatt Macy 1142eda14cbcSMatt Macy if (sdd->prevsnap[0] != '\0') { 1143eda14cbcSMatt Macy (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds)); 1144eda14cbcSMatt Macy *(strchr(fromds, '@') + 1) = '\0'; 1145eda14cbcSMatt Macy (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds)); 1146eda14cbcSMatt Macy } 1147eda14cbcSMatt Macy if (zfs_send_space(zhp, zhp->zfs_name, 1148c03c5b1cSMartin Matuska sdd->prevsnap[0] ? fromds : NULL, flags, &size) == 0) { 1149eda14cbcSMatt Macy send_print_verbose(fout, zhp->zfs_name, 1150eda14cbcSMatt Macy sdd->prevsnap[0] ? sdd->prevsnap : NULL, 1151eda14cbcSMatt Macy size, sdd->parsable); 1152eda14cbcSMatt Macy sdd->size += size; 1153eda14cbcSMatt Macy } 1154c03c5b1cSMartin Matuska } 1155eda14cbcSMatt Macy 1156eda14cbcSMatt Macy if (!sdd->dryrun) { 1157eda14cbcSMatt Macy /* 1158eda14cbcSMatt Macy * If progress reporting is requested, spawn a new thread to 1159eda14cbcSMatt Macy * poll ZFS_IOC_SEND_PROGRESS at a regular interval. 1160eda14cbcSMatt Macy */ 1161eda14cbcSMatt Macy if (sdd->progress) { 1162eda14cbcSMatt Macy pa.pa_zhp = zhp; 1163eda14cbcSMatt Macy pa.pa_fd = sdd->outfd; 1164eda14cbcSMatt Macy pa.pa_parsable = sdd->parsable; 1165eda14cbcSMatt Macy pa.pa_estimate = B_FALSE; 1166eda14cbcSMatt Macy pa.pa_verbosity = sdd->verbosity; 1167eda14cbcSMatt Macy 1168eda14cbcSMatt Macy if ((err = pthread_create(&tid, NULL, 1169eda14cbcSMatt Macy send_progress_thread, &pa)) != 0) { 1170eda14cbcSMatt Macy zfs_close(zhp); 1171eda14cbcSMatt Macy return (err); 1172eda14cbcSMatt Macy } 1173eda14cbcSMatt Macy } 1174eda14cbcSMatt Macy 1175eda14cbcSMatt Macy err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj, 1176eda14cbcSMatt Macy fromorigin, sdd->outfd, flags, sdd->debugnv); 1177eda14cbcSMatt Macy 1178716fd348SMartin Matuska if (sdd->progress && 1179716fd348SMartin Matuska send_progress_thread_exit(zhp->zfs_hdl, tid)) 1180716fd348SMartin Matuska return (-1); 1181eda14cbcSMatt Macy } 1182eda14cbcSMatt Macy 1183eda14cbcSMatt Macy (void) strcpy(sdd->prevsnap, thissnap); 1184eda14cbcSMatt Macy sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); 1185eda14cbcSMatt Macy zfs_close(zhp); 1186eda14cbcSMatt Macy return (err); 1187eda14cbcSMatt Macy } 1188eda14cbcSMatt Macy 1189c03c5b1cSMartin Matuska /* 1190c03c5b1cSMartin Matuska * Send all snapshots for a filesystem, updating the send dump data. 1191c03c5b1cSMartin Matuska */ 1192eda14cbcSMatt Macy static int 1193c03c5b1cSMartin Matuska dump_filesystem(zfs_handle_t *zhp, send_dump_data_t *sdd) 1194eda14cbcSMatt Macy { 1195eda14cbcSMatt Macy int rv = 0; 1196eda14cbcSMatt Macy boolean_t missingfrom = B_FALSE; 1197eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 1198eda14cbcSMatt Macy uint64_t min_txg = 0, max_txg = 0; 1199eda14cbcSMatt Macy 1200c03c5b1cSMartin Matuska /* 1201c03c5b1cSMartin Matuska * Make sure the tosnap exists. 1202c03c5b1cSMartin Matuska */ 1203eda14cbcSMatt Macy (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 1204eda14cbcSMatt Macy zhp->zfs_name, sdd->tosnap); 1205eda14cbcSMatt Macy if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 1206eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1207eda14cbcSMatt Macy "WARNING: could not send %s@%s: does not exist\n"), 1208eda14cbcSMatt Macy zhp->zfs_name, sdd->tosnap); 1209eda14cbcSMatt Macy sdd->err = B_TRUE; 1210eda14cbcSMatt Macy return (0); 1211eda14cbcSMatt Macy } 1212eda14cbcSMatt Macy 1213eda14cbcSMatt Macy /* 1214eda14cbcSMatt Macy * If this fs does not have fromsnap, and we're doing 1215eda14cbcSMatt Macy * recursive, we need to send a full stream from the 1216eda14cbcSMatt Macy * beginning (or an incremental from the origin if this 1217eda14cbcSMatt Macy * is a clone). If we're doing non-recursive, then let 1218eda14cbcSMatt Macy * them get the error. 1219eda14cbcSMatt Macy */ 1220c03c5b1cSMartin Matuska if (sdd->replicate && sdd->fromsnap) { 1221c03c5b1cSMartin Matuska /* 1222c03c5b1cSMartin Matuska * Make sure the fromsnap exists. 1223c03c5b1cSMartin Matuska */ 1224eda14cbcSMatt Macy (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 1225eda14cbcSMatt Macy zhp->zfs_name, sdd->fromsnap); 1226c03c5b1cSMartin Matuska if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) 1227eda14cbcSMatt Macy missingfrom = B_TRUE; 1228eda14cbcSMatt Macy } 1229eda14cbcSMatt Macy 1230c03c5b1cSMartin Matuska sdd->seenfrom = sdd->seento = B_FALSE; 1231c03c5b1cSMartin Matuska sdd->prevsnap[0] = '\0'; 1232eda14cbcSMatt Macy sdd->prevsnap_obj = 0; 1233eda14cbcSMatt Macy if (sdd->fromsnap == NULL || missingfrom) 1234eda14cbcSMatt Macy sdd->seenfrom = B_TRUE; 1235eda14cbcSMatt Macy 1236eda14cbcSMatt Macy /* 1237eda14cbcSMatt Macy * Iterate through all snapshots and process the ones we will be 1238eda14cbcSMatt Macy * sending. If we only have a "from" and "to" snapshot to deal 1239eda14cbcSMatt Macy * with, we can avoid iterating through all the other snapshots. 1240eda14cbcSMatt Macy */ 1241eda14cbcSMatt Macy if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) { 1242c03c5b1cSMartin Matuska if (!sdd->replicate) { 1243c03c5b1cSMartin Matuska if (sdd->fromsnap != NULL) { 1244c03c5b1cSMartin Matuska min_txg = get_snap_txg(zhp->zfs_hdl, 1245c03c5b1cSMartin Matuska zhp->zfs_name, sdd->fromsnap); 1246c03c5b1cSMartin Matuska } 1247c03c5b1cSMartin Matuska if (sdd->tosnap != NULL) { 1248c03c5b1cSMartin Matuska max_txg = get_snap_txg(zhp->zfs_hdl, 1249c03c5b1cSMartin Matuska zhp->zfs_name, sdd->tosnap); 1250c03c5b1cSMartin Matuska } 1251c03c5b1cSMartin Matuska } 1252c03c5b1cSMartin Matuska rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, sdd, 1253eda14cbcSMatt Macy min_txg, max_txg); 1254eda14cbcSMatt Macy } else { 1255eda14cbcSMatt Macy char snapname[MAXPATHLEN] = { 0 }; 1256eda14cbcSMatt Macy zfs_handle_t *snap; 1257eda14cbcSMatt Macy 1258c03c5b1cSMartin Matuska /* Dump fromsnap. */ 1259eda14cbcSMatt Macy if (!sdd->seenfrom) { 1260eda14cbcSMatt Macy (void) snprintf(snapname, sizeof (snapname), 1261eda14cbcSMatt Macy "%s@%s", zhp->zfs_name, sdd->fromsnap); 1262eda14cbcSMatt Macy snap = zfs_open(zhp->zfs_hdl, snapname, 1263eda14cbcSMatt Macy ZFS_TYPE_SNAPSHOT); 1264eda14cbcSMatt Macy if (snap != NULL) 1265eda14cbcSMatt Macy rv = dump_snapshot(snap, sdd); 1266eda14cbcSMatt Macy else 1267eda14cbcSMatt Macy rv = -1; 1268eda14cbcSMatt Macy } 1269eda14cbcSMatt Macy 1270c03c5b1cSMartin Matuska /* Dump tosnap. */ 1271eda14cbcSMatt Macy if (rv == 0) { 1272eda14cbcSMatt Macy (void) snprintf(snapname, sizeof (snapname), 1273eda14cbcSMatt Macy "%s@%s", zhp->zfs_name, sdd->tosnap); 1274eda14cbcSMatt Macy snap = zfs_open(zhp->zfs_hdl, snapname, 1275eda14cbcSMatt Macy ZFS_TYPE_SNAPSHOT); 1276eda14cbcSMatt Macy if (snap != NULL) 1277eda14cbcSMatt Macy rv = dump_snapshot(snap, sdd); 1278eda14cbcSMatt Macy else 1279eda14cbcSMatt Macy rv = -1; 1280eda14cbcSMatt Macy } 1281eda14cbcSMatt Macy } 1282eda14cbcSMatt Macy 1283eda14cbcSMatt Macy if (!sdd->seenfrom) { 1284eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1285eda14cbcSMatt Macy "WARNING: could not send %s@%s:\n" 1286eda14cbcSMatt Macy "incremental source (%s@%s) does not exist\n"), 1287eda14cbcSMatt Macy zhp->zfs_name, sdd->tosnap, 1288eda14cbcSMatt Macy zhp->zfs_name, sdd->fromsnap); 1289eda14cbcSMatt Macy sdd->err = B_TRUE; 1290eda14cbcSMatt Macy } else if (!sdd->seento) { 1291eda14cbcSMatt Macy if (sdd->fromsnap) { 1292eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1293eda14cbcSMatt Macy "WARNING: could not send %s@%s:\n" 1294eda14cbcSMatt Macy "incremental source (%s@%s) " 1295eda14cbcSMatt Macy "is not earlier than it\n"), 1296eda14cbcSMatt Macy zhp->zfs_name, sdd->tosnap, 1297eda14cbcSMatt Macy zhp->zfs_name, sdd->fromsnap); 1298eda14cbcSMatt Macy } else { 1299eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1300eda14cbcSMatt Macy "WARNING: " 1301eda14cbcSMatt Macy "could not send %s@%s: does not exist\n"), 1302eda14cbcSMatt Macy zhp->zfs_name, sdd->tosnap); 1303eda14cbcSMatt Macy } 1304eda14cbcSMatt Macy sdd->err = B_TRUE; 1305eda14cbcSMatt Macy } 1306eda14cbcSMatt Macy 1307eda14cbcSMatt Macy return (rv); 1308eda14cbcSMatt Macy } 1309eda14cbcSMatt Macy 1310c03c5b1cSMartin Matuska /* 1311c03c5b1cSMartin Matuska * Send all snapshots for all filesystems in sdd. 1312c03c5b1cSMartin Matuska */ 1313eda14cbcSMatt Macy static int 1314c03c5b1cSMartin Matuska dump_filesystems(zfs_handle_t *rzhp, send_dump_data_t *sdd) 1315eda14cbcSMatt Macy { 1316eda14cbcSMatt Macy nvpair_t *fspair; 1317eda14cbcSMatt Macy boolean_t needagain, progress; 1318eda14cbcSMatt Macy 1319eda14cbcSMatt Macy if (!sdd->replicate) 1320eda14cbcSMatt Macy return (dump_filesystem(rzhp, sdd)); 1321eda14cbcSMatt Macy 1322eda14cbcSMatt Macy /* Mark the clone origin snapshots. */ 1323eda14cbcSMatt Macy for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1324eda14cbcSMatt Macy fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1325eda14cbcSMatt Macy nvlist_t *nvfs; 1326eda14cbcSMatt Macy uint64_t origin_guid = 0; 1327eda14cbcSMatt Macy 1328184c1b94SMartin Matuska nvfs = fnvpair_value_nvlist(fspair); 1329eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid); 1330eda14cbcSMatt Macy if (origin_guid != 0) { 1331eda14cbcSMatt Macy char *snapname; 1332eda14cbcSMatt Macy nvlist_t *origin_nv = fsavl_find(sdd->fsavl, 1333eda14cbcSMatt Macy origin_guid, &snapname); 1334eda14cbcSMatt Macy if (origin_nv != NULL) { 1335eda14cbcSMatt Macy nvlist_t *snapprops; 1336184c1b94SMartin Matuska snapprops = fnvlist_lookup_nvlist(origin_nv, 1337184c1b94SMartin Matuska "snapprops"); 1338184c1b94SMartin Matuska snapprops = fnvlist_lookup_nvlist(snapprops, 1339184c1b94SMartin Matuska snapname); 1340184c1b94SMartin Matuska fnvlist_add_boolean(snapprops, 1341184c1b94SMartin Matuska "is_clone_origin"); 1342eda14cbcSMatt Macy } 1343eda14cbcSMatt Macy } 1344eda14cbcSMatt Macy } 1345eda14cbcSMatt Macy again: 1346eda14cbcSMatt Macy needagain = progress = B_FALSE; 1347eda14cbcSMatt Macy for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1348eda14cbcSMatt Macy fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1349eda14cbcSMatt Macy nvlist_t *fslist, *parent_nv; 1350eda14cbcSMatt Macy char *fsname; 1351eda14cbcSMatt Macy zfs_handle_t *zhp; 1352eda14cbcSMatt Macy int err; 1353eda14cbcSMatt Macy uint64_t origin_guid = 0; 1354eda14cbcSMatt Macy uint64_t parent_guid = 0; 1355eda14cbcSMatt Macy 1356184c1b94SMartin Matuska fslist = fnvpair_value_nvlist(fspair); 1357eda14cbcSMatt Macy if (nvlist_lookup_boolean(fslist, "sent") == 0) 1358eda14cbcSMatt Macy continue; 1359eda14cbcSMatt Macy 1360184c1b94SMartin Matuska fsname = fnvlist_lookup_string(fslist, "name"); 1361eda14cbcSMatt Macy (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); 1362eda14cbcSMatt Macy (void) nvlist_lookup_uint64(fslist, "parentfromsnap", 1363eda14cbcSMatt Macy &parent_guid); 1364eda14cbcSMatt Macy 1365eda14cbcSMatt Macy if (parent_guid != 0) { 1366eda14cbcSMatt Macy parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL); 1367eda14cbcSMatt Macy if (!nvlist_exists(parent_nv, "sent")) { 1368c03c5b1cSMartin Matuska /* Parent has not been sent; skip this one. */ 1369eda14cbcSMatt Macy needagain = B_TRUE; 1370eda14cbcSMatt Macy continue; 1371eda14cbcSMatt Macy } 1372eda14cbcSMatt Macy } 1373eda14cbcSMatt Macy 1374eda14cbcSMatt Macy if (origin_guid != 0) { 1375eda14cbcSMatt Macy nvlist_t *origin_nv = fsavl_find(sdd->fsavl, 1376eda14cbcSMatt Macy origin_guid, NULL); 1377eda14cbcSMatt Macy if (origin_nv != NULL && 1378eda14cbcSMatt Macy !nvlist_exists(origin_nv, "sent")) { 1379eda14cbcSMatt Macy /* 1380c03c5b1cSMartin Matuska * Origin has not been sent yet; 1381eda14cbcSMatt Macy * skip this clone. 1382eda14cbcSMatt Macy */ 1383eda14cbcSMatt Macy needagain = B_TRUE; 1384eda14cbcSMatt Macy continue; 1385eda14cbcSMatt Macy } 1386eda14cbcSMatt Macy } 1387eda14cbcSMatt Macy 1388eda14cbcSMatt Macy zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); 1389eda14cbcSMatt Macy if (zhp == NULL) 1390eda14cbcSMatt Macy return (-1); 1391eda14cbcSMatt Macy err = dump_filesystem(zhp, sdd); 1392184c1b94SMartin Matuska fnvlist_add_boolean(fslist, "sent"); 1393eda14cbcSMatt Macy progress = B_TRUE; 1394eda14cbcSMatt Macy zfs_close(zhp); 1395eda14cbcSMatt Macy if (err) 1396eda14cbcSMatt Macy return (err); 1397eda14cbcSMatt Macy } 1398eda14cbcSMatt Macy if (needagain) { 1399eda14cbcSMatt Macy assert(progress); 1400eda14cbcSMatt Macy goto again; 1401eda14cbcSMatt Macy } 1402eda14cbcSMatt Macy 1403c03c5b1cSMartin Matuska /* Clean out the sent flags in case we reuse this fss. */ 1404eda14cbcSMatt Macy for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1405eda14cbcSMatt Macy fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1406eda14cbcSMatt Macy nvlist_t *fslist; 1407eda14cbcSMatt Macy 1408184c1b94SMartin Matuska fslist = fnvpair_value_nvlist(fspair); 1409eda14cbcSMatt Macy (void) nvlist_remove_all(fslist, "sent"); 1410eda14cbcSMatt Macy } 1411eda14cbcSMatt Macy 1412eda14cbcSMatt Macy return (0); 1413eda14cbcSMatt Macy } 1414eda14cbcSMatt Macy 1415eda14cbcSMatt Macy nvlist_t * 1416eda14cbcSMatt Macy zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token) 1417eda14cbcSMatt Macy { 1418eda14cbcSMatt Macy unsigned int version; 1419eda14cbcSMatt Macy int nread, i; 1420eda14cbcSMatt Macy unsigned long long checksum, packed_len; 1421eda14cbcSMatt Macy 1422eda14cbcSMatt Macy /* 1423eda14cbcSMatt Macy * Decode token header, which is: 1424eda14cbcSMatt Macy * <token version>-<checksum of payload>-<uncompressed payload length> 1425eda14cbcSMatt Macy * Note that the only supported token version is 1. 1426eda14cbcSMatt Macy */ 1427eda14cbcSMatt Macy nread = sscanf(token, "%u-%llx-%llx-", 1428eda14cbcSMatt Macy &version, &checksum, &packed_len); 1429eda14cbcSMatt Macy if (nread != 3) { 1430eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1431eda14cbcSMatt Macy "resume token is corrupt (invalid format)")); 1432eda14cbcSMatt Macy return (NULL); 1433eda14cbcSMatt Macy } 1434eda14cbcSMatt Macy 1435eda14cbcSMatt Macy if (version != ZFS_SEND_RESUME_TOKEN_VERSION) { 1436eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1437eda14cbcSMatt Macy "resume token is corrupt (invalid version %u)"), 1438eda14cbcSMatt Macy version); 1439eda14cbcSMatt Macy return (NULL); 1440eda14cbcSMatt Macy } 1441eda14cbcSMatt Macy 1442c03c5b1cSMartin Matuska /* Convert hexadecimal representation to binary. */ 1443eda14cbcSMatt Macy token = strrchr(token, '-') + 1; 1444eda14cbcSMatt Macy int len = strlen(token) / 2; 1445eda14cbcSMatt Macy unsigned char *compressed = zfs_alloc(hdl, len); 1446eda14cbcSMatt Macy for (i = 0; i < len; i++) { 1447eda14cbcSMatt Macy nread = sscanf(token + i * 2, "%2hhx", compressed + i); 1448eda14cbcSMatt Macy if (nread != 1) { 1449eda14cbcSMatt Macy free(compressed); 1450eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1451eda14cbcSMatt Macy "resume token is corrupt " 1452eda14cbcSMatt Macy "(payload is not hex-encoded)")); 1453eda14cbcSMatt Macy return (NULL); 1454eda14cbcSMatt Macy } 1455eda14cbcSMatt Macy } 1456eda14cbcSMatt Macy 1457c03c5b1cSMartin Matuska /* Verify checksum. */ 1458eda14cbcSMatt Macy zio_cksum_t cksum; 1459eda14cbcSMatt Macy fletcher_4_native_varsize(compressed, len, &cksum); 1460eda14cbcSMatt Macy if (cksum.zc_word[0] != checksum) { 1461eda14cbcSMatt Macy free(compressed); 1462eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1463eda14cbcSMatt Macy "resume token is corrupt (incorrect checksum)")); 1464eda14cbcSMatt Macy return (NULL); 1465eda14cbcSMatt Macy } 1466eda14cbcSMatt Macy 1467c03c5b1cSMartin Matuska /* Uncompress. */ 1468eda14cbcSMatt Macy void *packed = zfs_alloc(hdl, packed_len); 1469eda14cbcSMatt Macy uLongf packed_len_long = packed_len; 1470eda14cbcSMatt Macy if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK || 1471eda14cbcSMatt Macy packed_len_long != packed_len) { 1472eda14cbcSMatt Macy free(packed); 1473eda14cbcSMatt Macy free(compressed); 1474eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1475eda14cbcSMatt Macy "resume token is corrupt (decompression failed)")); 1476eda14cbcSMatt Macy return (NULL); 1477eda14cbcSMatt Macy } 1478eda14cbcSMatt Macy 1479c03c5b1cSMartin Matuska /* Unpack nvlist. */ 1480eda14cbcSMatt Macy nvlist_t *nv; 1481eda14cbcSMatt Macy int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP); 1482eda14cbcSMatt Macy free(packed); 1483eda14cbcSMatt Macy free(compressed); 1484eda14cbcSMatt Macy if (error != 0) { 1485eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1486eda14cbcSMatt Macy "resume token is corrupt (nvlist_unpack failed)")); 1487eda14cbcSMatt Macy return (NULL); 1488eda14cbcSMatt Macy } 1489eda14cbcSMatt Macy return (nv); 1490eda14cbcSMatt Macy } 1491c03c5b1cSMartin Matuska 1492eda14cbcSMatt Macy static enum lzc_send_flags 1493eda14cbcSMatt Macy lzc_flags_from_sendflags(const sendflags_t *flags) 1494eda14cbcSMatt Macy { 1495eda14cbcSMatt Macy enum lzc_send_flags lzc_flags = 0; 1496c03c5b1cSMartin Matuska 1497eda14cbcSMatt Macy if (flags->largeblock) 1498eda14cbcSMatt Macy lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 1499eda14cbcSMatt Macy if (flags->embed_data) 1500eda14cbcSMatt Macy lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 1501eda14cbcSMatt Macy if (flags->compress) 1502eda14cbcSMatt Macy lzc_flags |= LZC_SEND_FLAG_COMPRESS; 1503eda14cbcSMatt Macy if (flags->raw) 1504eda14cbcSMatt Macy lzc_flags |= LZC_SEND_FLAG_RAW; 1505eda14cbcSMatt Macy if (flags->saved) 1506eda14cbcSMatt Macy lzc_flags |= LZC_SEND_FLAG_SAVED; 1507c03c5b1cSMartin Matuska 1508eda14cbcSMatt Macy return (lzc_flags); 1509eda14cbcSMatt Macy } 1510eda14cbcSMatt Macy 1511eda14cbcSMatt Macy static int 1512eda14cbcSMatt Macy estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags, 1513eda14cbcSMatt Macy uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes, 1514eda14cbcSMatt Macy const char *redactbook, char *errbuf) 1515eda14cbcSMatt Macy { 1516eda14cbcSMatt Macy uint64_t size; 1517eda14cbcSMatt Macy FILE *fout = flags->dryrun ? stdout : stderr; 1518eda14cbcSMatt Macy progress_arg_t pa = { 0 }; 1519eda14cbcSMatt Macy int err = 0; 1520eda14cbcSMatt Macy pthread_t ptid; 1521eda14cbcSMatt Macy 1522eda14cbcSMatt Macy if (flags->progress) { 1523eda14cbcSMatt Macy pa.pa_zhp = zhp; 1524eda14cbcSMatt Macy pa.pa_fd = fd; 1525eda14cbcSMatt Macy pa.pa_parsable = flags->parsable; 1526eda14cbcSMatt Macy pa.pa_estimate = B_TRUE; 1527eda14cbcSMatt Macy pa.pa_verbosity = flags->verbosity; 1528eda14cbcSMatt Macy 1529eda14cbcSMatt Macy err = pthread_create(&ptid, NULL, 1530eda14cbcSMatt Macy send_progress_thread, &pa); 1531eda14cbcSMatt Macy if (err != 0) { 153216038816SMartin Matuska zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno)); 1533eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, 1534eda14cbcSMatt Macy EZFS_THREADCREATEFAILED, errbuf)); 1535eda14cbcSMatt Macy } 1536eda14cbcSMatt Macy } 1537eda14cbcSMatt Macy 1538eda14cbcSMatt Macy err = lzc_send_space_resume_redacted(zhp->zfs_name, from, 1539eda14cbcSMatt Macy lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes, 1540eda14cbcSMatt Macy redactbook, fd, &size); 1541eda14cbcSMatt Macy 1542716fd348SMartin Matuska if (flags->progress && send_progress_thread_exit(zhp->zfs_hdl, ptid)) 1543716fd348SMartin Matuska return (-1); 1544eda14cbcSMatt Macy 1545eda14cbcSMatt Macy if (err != 0) { 154616038816SMartin Matuska zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err)); 1547eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP, 1548eda14cbcSMatt Macy errbuf)); 1549eda14cbcSMatt Macy } 1550eda14cbcSMatt Macy send_print_verbose(fout, zhp->zfs_name, from, size, 1551eda14cbcSMatt Macy flags->parsable); 1552eda14cbcSMatt Macy 1553eda14cbcSMatt Macy if (flags->parsable) { 1554eda14cbcSMatt Macy (void) fprintf(fout, "size\t%llu\n", (longlong_t)size); 1555eda14cbcSMatt Macy } else { 1556eda14cbcSMatt Macy char buf[16]; 1557eda14cbcSMatt Macy zfs_nicenum(size, buf, sizeof (buf)); 1558eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1559eda14cbcSMatt Macy "total estimated size is %s\n"), buf); 1560eda14cbcSMatt Macy } 1561eda14cbcSMatt Macy return (0); 1562eda14cbcSMatt Macy } 1563eda14cbcSMatt Macy 1564eda14cbcSMatt Macy static boolean_t 1565eda14cbcSMatt Macy redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid) 1566eda14cbcSMatt Macy { 1567eda14cbcSMatt Macy for (int i = 0; i < num_snaps; i++) { 1568eda14cbcSMatt Macy if (snaps[i] == guid) 1569eda14cbcSMatt Macy return (B_TRUE); 1570eda14cbcSMatt Macy } 1571eda14cbcSMatt Macy return (B_FALSE); 1572eda14cbcSMatt Macy } 1573eda14cbcSMatt Macy 1574eda14cbcSMatt Macy static boolean_t 1575eda14cbcSMatt Macy redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1, 1576eda14cbcSMatt Macy const uint64_t *snaps2, uint64_t num_snaps2) 1577eda14cbcSMatt Macy { 1578eda14cbcSMatt Macy if (num_snaps1 != num_snaps2) 1579eda14cbcSMatt Macy return (B_FALSE); 1580eda14cbcSMatt Macy for (int i = 0; i < num_snaps1; i++) { 1581eda14cbcSMatt Macy if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i])) 1582eda14cbcSMatt Macy return (B_FALSE); 1583eda14cbcSMatt Macy } 1584eda14cbcSMatt Macy return (B_TRUE); 1585eda14cbcSMatt Macy } 1586eda14cbcSMatt Macy 1587eda14cbcSMatt Macy static int 1588c03c5b1cSMartin Matuska get_bookmarks(const char *path, nvlist_t **bmarksp) 1589eda14cbcSMatt Macy { 1590eda14cbcSMatt Macy nvlist_t *props = fnvlist_alloc(); 1591c03c5b1cSMartin Matuska int error; 1592eda14cbcSMatt Macy 1593eda14cbcSMatt Macy fnvlist_add_boolean(props, "redact_complete"); 1594eda14cbcSMatt Macy fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); 1595c03c5b1cSMartin Matuska error = lzc_get_bookmarks(path, props, bmarksp); 1596184c1b94SMartin Matuska fnvlist_free(props); 1597c03c5b1cSMartin Matuska return (error); 1598eda14cbcSMatt Macy } 1599c03c5b1cSMartin Matuska 1600c03c5b1cSMartin Matuska static nvpair_t * 1601c03c5b1cSMartin Matuska find_redact_pair(nvlist_t *bmarks, const uint64_t *redact_snap_guids, 1602c03c5b1cSMartin Matuska int num_redact_snaps) 1603c03c5b1cSMartin Matuska { 1604eda14cbcSMatt Macy nvpair_t *pair; 1605c03c5b1cSMartin Matuska 1606eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(bmarks, NULL); pair; 1607eda14cbcSMatt Macy pair = nvlist_next_nvpair(bmarks, pair)) { 1608eda14cbcSMatt Macy 1609eda14cbcSMatt Macy nvlist_t *bmark = fnvpair_value_nvlist(pair); 1610eda14cbcSMatt Macy nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, 1611eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); 1612eda14cbcSMatt Macy uint_t len = 0; 1613eda14cbcSMatt Macy uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist, 1614eda14cbcSMatt Macy ZPROP_VALUE, &len); 1615eda14cbcSMatt Macy if (redact_snaps_equal(redact_snap_guids, 1616eda14cbcSMatt Macy num_redact_snaps, bmarksnaps, len)) { 1617eda14cbcSMatt Macy break; 1618eda14cbcSMatt Macy } 1619eda14cbcSMatt Macy } 1620c03c5b1cSMartin Matuska return (pair); 1621c03c5b1cSMartin Matuska } 1622c03c5b1cSMartin Matuska 1623c03c5b1cSMartin Matuska static boolean_t 1624c03c5b1cSMartin Matuska get_redact_complete(nvpair_t *pair) 1625c03c5b1cSMartin Matuska { 1626c03c5b1cSMartin Matuska nvlist_t *bmark = fnvpair_value_nvlist(pair); 1627c03c5b1cSMartin Matuska nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete"); 1628c03c5b1cSMartin Matuska boolean_t complete = fnvlist_lookup_boolean_value(vallist, 1629c03c5b1cSMartin Matuska ZPROP_VALUE); 1630c03c5b1cSMartin Matuska 1631c03c5b1cSMartin Matuska return (complete); 1632c03c5b1cSMartin Matuska } 1633c03c5b1cSMartin Matuska 1634c03c5b1cSMartin Matuska /* 1635c03c5b1cSMartin Matuska * Check that the list of redaction snapshots in the bookmark matches the send 1636c03c5b1cSMartin Matuska * we're resuming, and return whether or not it's complete. 1637c03c5b1cSMartin Matuska * 1638c03c5b1cSMartin Matuska * Note that the caller needs to free the contents of *bookname with free() if 1639c03c5b1cSMartin Matuska * this function returns successfully. 1640c03c5b1cSMartin Matuska */ 1641c03c5b1cSMartin Matuska static int 1642c03c5b1cSMartin Matuska find_redact_book(libzfs_handle_t *hdl, const char *path, 1643c03c5b1cSMartin Matuska const uint64_t *redact_snap_guids, int num_redact_snaps, 1644c03c5b1cSMartin Matuska char **bookname) 1645c03c5b1cSMartin Matuska { 16461f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 1647c03c5b1cSMartin Matuska nvlist_t *bmarks; 1648c03c5b1cSMartin Matuska 1649c03c5b1cSMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1650c03c5b1cSMartin Matuska "cannot resume send")); 1651c03c5b1cSMartin Matuska 1652c03c5b1cSMartin Matuska int error = get_bookmarks(path, &bmarks); 1653c03c5b1cSMartin Matuska if (error != 0) { 1654c03c5b1cSMartin Matuska if (error == ESRCH) { 1655c03c5b1cSMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1656c03c5b1cSMartin Matuska "nonexistent redaction bookmark provided")); 1657c03c5b1cSMartin Matuska } else if (error == ENOENT) { 1658c03c5b1cSMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1659c03c5b1cSMartin Matuska "dataset to be sent no longer exists")); 1660c03c5b1cSMartin Matuska } else { 1661c03c5b1cSMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1662c03c5b1cSMartin Matuska "unknown error: %s"), strerror(error)); 1663c03c5b1cSMartin Matuska } 1664c03c5b1cSMartin Matuska return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1665c03c5b1cSMartin Matuska } 1666c03c5b1cSMartin Matuska nvpair_t *pair = find_redact_pair(bmarks, redact_snap_guids, 1667c03c5b1cSMartin Matuska num_redact_snaps); 1668eda14cbcSMatt Macy if (pair == NULL) { 1669eda14cbcSMatt Macy fnvlist_free(bmarks); 1670eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1671eda14cbcSMatt Macy "no appropriate redaction bookmark exists")); 1672eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1673eda14cbcSMatt Macy } 1674c03c5b1cSMartin Matuska boolean_t complete = get_redact_complete(pair); 1675eda14cbcSMatt Macy if (!complete) { 1676eda14cbcSMatt Macy fnvlist_free(bmarks); 1677eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1678eda14cbcSMatt Macy "incomplete redaction bookmark provided")); 1679eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1680eda14cbcSMatt Macy } 1681c03c5b1cSMartin Matuska *bookname = strndup(nvpair_name(pair), ZFS_MAX_DATASET_NAME_LEN); 1682eda14cbcSMatt Macy ASSERT3P(*bookname, !=, NULL); 1683eda14cbcSMatt Macy fnvlist_free(bmarks); 1684eda14cbcSMatt Macy return (0); 1685eda14cbcSMatt Macy } 1686eda14cbcSMatt Macy 1687c03c5b1cSMartin Matuska static enum lzc_send_flags 1688c03c5b1cSMartin Matuska lzc_flags_from_resume_nvl(nvlist_t *resume_nvl) 1689c03c5b1cSMartin Matuska { 1690c03c5b1cSMartin Matuska enum lzc_send_flags lzc_flags = 0; 1691c03c5b1cSMartin Matuska 1692c03c5b1cSMartin Matuska if (nvlist_exists(resume_nvl, "largeblockok")) 1693c03c5b1cSMartin Matuska lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 1694c03c5b1cSMartin Matuska if (nvlist_exists(resume_nvl, "embedok")) 1695c03c5b1cSMartin Matuska lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 1696c03c5b1cSMartin Matuska if (nvlist_exists(resume_nvl, "compressok")) 1697c03c5b1cSMartin Matuska lzc_flags |= LZC_SEND_FLAG_COMPRESS; 1698c03c5b1cSMartin Matuska if (nvlist_exists(resume_nvl, "rawok")) 1699c03c5b1cSMartin Matuska lzc_flags |= LZC_SEND_FLAG_RAW; 1700c03c5b1cSMartin Matuska if (nvlist_exists(resume_nvl, "savedok")) 1701c03c5b1cSMartin Matuska lzc_flags |= LZC_SEND_FLAG_SAVED; 1702c03c5b1cSMartin Matuska 1703c03c5b1cSMartin Matuska return (lzc_flags); 1704c03c5b1cSMartin Matuska } 1705c03c5b1cSMartin Matuska 1706eda14cbcSMatt Macy static int 1707c03c5b1cSMartin Matuska zfs_send_resume_impl_cb_impl(libzfs_handle_t *hdl, sendflags_t *flags, 1708c03c5b1cSMartin Matuska int outfd, nvlist_t *resume_nvl) 1709eda14cbcSMatt Macy { 17101f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 1711eda14cbcSMatt Macy char *toname; 1712eda14cbcSMatt Macy char *fromname = NULL; 1713eda14cbcSMatt Macy uint64_t resumeobj, resumeoff, toguid, fromguid, bytes; 1714eda14cbcSMatt Macy zfs_handle_t *zhp; 1715eda14cbcSMatt Macy int error = 0; 1716eda14cbcSMatt Macy char name[ZFS_MAX_DATASET_NAME_LEN]; 1717eda14cbcSMatt Macy FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr; 1718eda14cbcSMatt Macy uint64_t *redact_snap_guids = NULL; 1719eda14cbcSMatt Macy int num_redact_snaps = 0; 1720eda14cbcSMatt Macy char *redact_book = NULL; 1721eda14cbcSMatt Macy 1722eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1723eda14cbcSMatt Macy "cannot resume send")); 1724eda14cbcSMatt Macy 1725eda14cbcSMatt Macy if (flags->verbosity != 0) { 1726eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1727eda14cbcSMatt Macy "resume token contents:\n")); 1728eda14cbcSMatt Macy nvlist_print(fout, resume_nvl); 1729eda14cbcSMatt Macy } 1730eda14cbcSMatt Macy 1731eda14cbcSMatt Macy if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 || 1732eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 || 1733eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 || 1734eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 || 1735eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) { 1736eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1737eda14cbcSMatt Macy "resume token is corrupt")); 1738eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_FAULT, errbuf)); 1739eda14cbcSMatt Macy } 1740eda14cbcSMatt Macy fromguid = 0; 1741eda14cbcSMatt Macy (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid); 1742eda14cbcSMatt Macy 1743eda14cbcSMatt Macy if (flags->saved) { 1744eda14cbcSMatt Macy (void) strcpy(name, toname); 1745eda14cbcSMatt Macy } else { 1746eda14cbcSMatt Macy error = guid_to_name(hdl, toname, toguid, B_FALSE, name); 1747eda14cbcSMatt Macy if (error != 0) { 1748eda14cbcSMatt Macy if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) { 1749eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1750eda14cbcSMatt Macy "'%s' is no longer the same snapshot " 1751eda14cbcSMatt Macy "used in the initial send"), toname); 1752eda14cbcSMatt Macy } else { 1753eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1754eda14cbcSMatt Macy "'%s' used in the initial send no " 1755eda14cbcSMatt Macy "longer exists"), toname); 1756eda14cbcSMatt Macy } 1757eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPATH, errbuf)); 1758eda14cbcSMatt Macy } 1759eda14cbcSMatt Macy } 1760eda14cbcSMatt Macy 1761eda14cbcSMatt Macy zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 1762eda14cbcSMatt Macy if (zhp == NULL) { 1763eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1764eda14cbcSMatt Macy "unable to access '%s'"), name); 1765eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPATH, errbuf)); 1766eda14cbcSMatt Macy } 1767eda14cbcSMatt Macy 1768eda14cbcSMatt Macy if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps", 1769eda14cbcSMatt Macy &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) { 1770eda14cbcSMatt Macy num_redact_snaps = -1; 1771eda14cbcSMatt Macy } 1772eda14cbcSMatt Macy 1773eda14cbcSMatt Macy if (fromguid != 0) { 1774eda14cbcSMatt Macy if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE, 1775eda14cbcSMatt Macy redact_snap_guids, num_redact_snaps, name) != 0) { 1776eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1777eda14cbcSMatt Macy "incremental source %#llx no longer exists"), 1778eda14cbcSMatt Macy (longlong_t)fromguid); 1779eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPATH, errbuf)); 1780eda14cbcSMatt Macy } 1781eda14cbcSMatt Macy fromname = name; 1782eda14cbcSMatt Macy } 1783eda14cbcSMatt Macy 1784eda14cbcSMatt Macy redact_snap_guids = NULL; 1785eda14cbcSMatt Macy 1786eda14cbcSMatt Macy if (nvlist_lookup_uint64_array(resume_nvl, 1787eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids, 1788eda14cbcSMatt Macy (uint_t *)&num_redact_snaps) == 0) { 1789eda14cbcSMatt Macy char path[ZFS_MAX_DATASET_NAME_LEN]; 1790eda14cbcSMatt Macy 1791eda14cbcSMatt Macy (void) strlcpy(path, toname, sizeof (path)); 1792eda14cbcSMatt Macy char *at = strchr(path, '@'); 1793eda14cbcSMatt Macy ASSERT3P(at, !=, NULL); 1794eda14cbcSMatt Macy 1795eda14cbcSMatt Macy *at = '\0'; 1796eda14cbcSMatt Macy 1797eda14cbcSMatt Macy if ((error = find_redact_book(hdl, path, redact_snap_guids, 1798eda14cbcSMatt Macy num_redact_snaps, &redact_book)) != 0) { 1799eda14cbcSMatt Macy return (error); 1800eda14cbcSMatt Macy } 1801eda14cbcSMatt Macy } 1802eda14cbcSMatt Macy 1803c03c5b1cSMartin Matuska enum lzc_send_flags lzc_flags = lzc_flags_from_sendflags(flags) | 1804c03c5b1cSMartin Matuska lzc_flags_from_resume_nvl(resume_nvl); 1805c03c5b1cSMartin Matuska 1806eda14cbcSMatt Macy if (flags->verbosity != 0) { 1807eda14cbcSMatt Macy /* 1808eda14cbcSMatt Macy * Some of these may have come from the resume token, set them 1809eda14cbcSMatt Macy * here for size estimate purposes. 1810eda14cbcSMatt Macy */ 1811eda14cbcSMatt Macy sendflags_t tmpflags = *flags; 1812eda14cbcSMatt Macy if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK) 1813eda14cbcSMatt Macy tmpflags.largeblock = B_TRUE; 1814eda14cbcSMatt Macy if (lzc_flags & LZC_SEND_FLAG_COMPRESS) 1815eda14cbcSMatt Macy tmpflags.compress = B_TRUE; 1816eda14cbcSMatt Macy if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA) 1817eda14cbcSMatt Macy tmpflags.embed_data = B_TRUE; 181833b8c039SMartin Matuska if (lzc_flags & LZC_SEND_FLAG_RAW) 181933b8c039SMartin Matuska tmpflags.raw = B_TRUE; 182033b8c039SMartin Matuska if (lzc_flags & LZC_SEND_FLAG_SAVED) 182133b8c039SMartin Matuska tmpflags.saved = B_TRUE; 1822eda14cbcSMatt Macy error = estimate_size(zhp, fromname, outfd, &tmpflags, 1823eda14cbcSMatt Macy resumeobj, resumeoff, bytes, redact_book, errbuf); 1824eda14cbcSMatt Macy } 1825eda14cbcSMatt Macy 1826eda14cbcSMatt Macy if (!flags->dryrun) { 1827eda14cbcSMatt Macy progress_arg_t pa = { 0 }; 1828eda14cbcSMatt Macy pthread_t tid; 1829eda14cbcSMatt Macy /* 1830eda14cbcSMatt Macy * If progress reporting is requested, spawn a new thread to 1831eda14cbcSMatt Macy * poll ZFS_IOC_SEND_PROGRESS at a regular interval. 1832eda14cbcSMatt Macy */ 1833eda14cbcSMatt Macy if (flags->progress) { 1834eda14cbcSMatt Macy pa.pa_zhp = zhp; 1835eda14cbcSMatt Macy pa.pa_fd = outfd; 1836eda14cbcSMatt Macy pa.pa_parsable = flags->parsable; 1837eda14cbcSMatt Macy pa.pa_estimate = B_FALSE; 1838eda14cbcSMatt Macy pa.pa_verbosity = flags->verbosity; 1839eda14cbcSMatt Macy 1840eda14cbcSMatt Macy error = pthread_create(&tid, NULL, 1841eda14cbcSMatt Macy send_progress_thread, &pa); 1842eda14cbcSMatt Macy if (error != 0) { 1843eda14cbcSMatt Macy if (redact_book != NULL) 1844eda14cbcSMatt Macy free(redact_book); 1845eda14cbcSMatt Macy zfs_close(zhp); 1846eda14cbcSMatt Macy return (error); 1847eda14cbcSMatt Macy } 1848eda14cbcSMatt Macy } 1849eda14cbcSMatt Macy 1850eda14cbcSMatt Macy error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd, 1851eda14cbcSMatt Macy lzc_flags, resumeobj, resumeoff, redact_book); 1852eda14cbcSMatt Macy if (redact_book != NULL) 1853eda14cbcSMatt Macy free(redact_book); 1854eda14cbcSMatt Macy 1855716fd348SMartin Matuska if (flags->progress && send_progress_thread_exit(hdl, tid)) 1856716fd348SMartin Matuska return (-1); 1857eda14cbcSMatt Macy 18581f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 1859eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1860eda14cbcSMatt Macy "warning: cannot send '%s'"), zhp->zfs_name); 1861eda14cbcSMatt Macy 1862eda14cbcSMatt Macy zfs_close(zhp); 1863eda14cbcSMatt Macy 1864eda14cbcSMatt Macy switch (error) { 1865eda14cbcSMatt Macy case 0: 1866eda14cbcSMatt Macy return (0); 1867eda14cbcSMatt Macy case EACCES: 1868eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1869eda14cbcSMatt Macy "source key must be loaded")); 1870eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 1871eda14cbcSMatt Macy case ESRCH: 1872eda14cbcSMatt Macy if (lzc_exists(zhp->zfs_name)) { 1873eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1874eda14cbcSMatt Macy "incremental source could not be found")); 1875eda14cbcSMatt Macy } 1876eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOENT, errbuf)); 1877eda14cbcSMatt Macy 1878eda14cbcSMatt Macy case EXDEV: 1879eda14cbcSMatt Macy case ENOENT: 1880eda14cbcSMatt Macy case EDQUOT: 1881eda14cbcSMatt Macy case EFBIG: 1882eda14cbcSMatt Macy case EIO: 1883eda14cbcSMatt Macy case ENOLINK: 1884eda14cbcSMatt Macy case ENOSPC: 1885eda14cbcSMatt Macy case ENOSTR: 1886eda14cbcSMatt Macy case ENXIO: 1887eda14cbcSMatt Macy case EPIPE: 1888eda14cbcSMatt Macy case ERANGE: 1889eda14cbcSMatt Macy case EFAULT: 1890eda14cbcSMatt Macy case EROFS: 189116038816SMartin Matuska zfs_error_aux(hdl, "%s", strerror(errno)); 1892eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 1893eda14cbcSMatt Macy 1894eda14cbcSMatt Macy default: 1895eda14cbcSMatt Macy return (zfs_standard_error(hdl, errno, errbuf)); 1896eda14cbcSMatt Macy } 1897eda14cbcSMatt Macy } else { 1898eda14cbcSMatt Macy if (redact_book != NULL) 1899eda14cbcSMatt Macy free(redact_book); 1900eda14cbcSMatt Macy } 1901eda14cbcSMatt Macy 1902eda14cbcSMatt Macy zfs_close(zhp); 1903eda14cbcSMatt Macy 1904eda14cbcSMatt Macy return (error); 1905eda14cbcSMatt Macy } 1906eda14cbcSMatt Macy 1907c03c5b1cSMartin Matuska struct zfs_send_resume_impl { 1908c03c5b1cSMartin Matuska libzfs_handle_t *hdl; 1909c03c5b1cSMartin Matuska sendflags_t *flags; 1910c03c5b1cSMartin Matuska nvlist_t *resume_nvl; 1911c03c5b1cSMartin Matuska }; 1912c03c5b1cSMartin Matuska 1913c03c5b1cSMartin Matuska static int 1914c03c5b1cSMartin Matuska zfs_send_resume_impl_cb(int outfd, void *arg) 1915c03c5b1cSMartin Matuska { 1916c03c5b1cSMartin Matuska struct zfs_send_resume_impl *zsri = arg; 1917c03c5b1cSMartin Matuska return (zfs_send_resume_impl_cb_impl(zsri->hdl, zsri->flags, outfd, 1918c03c5b1cSMartin Matuska zsri->resume_nvl)); 1919c03c5b1cSMartin Matuska } 1920c03c5b1cSMartin Matuska 1921c03c5b1cSMartin Matuska static int 1922c03c5b1cSMartin Matuska zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd, 1923c03c5b1cSMartin Matuska nvlist_t *resume_nvl) 1924c03c5b1cSMartin Matuska { 1925c03c5b1cSMartin Matuska struct zfs_send_resume_impl zsri = { 1926c03c5b1cSMartin Matuska .hdl = hdl, 1927c03c5b1cSMartin Matuska .flags = flags, 1928c03c5b1cSMartin Matuska .resume_nvl = resume_nvl, 1929c03c5b1cSMartin Matuska }; 1930c03c5b1cSMartin Matuska return (lzc_send_wrapper(zfs_send_resume_impl_cb, outfd, &zsri)); 1931c03c5b1cSMartin Matuska } 1932c03c5b1cSMartin Matuska 1933eda14cbcSMatt Macy int 1934eda14cbcSMatt Macy zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd, 1935eda14cbcSMatt Macy const char *resume_token) 1936eda14cbcSMatt Macy { 1937eda14cbcSMatt Macy int ret; 19381f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 1939eda14cbcSMatt Macy nvlist_t *resume_nvl; 1940eda14cbcSMatt Macy 1941eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1942eda14cbcSMatt Macy "cannot resume send")); 1943eda14cbcSMatt Macy 1944eda14cbcSMatt Macy resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token); 1945eda14cbcSMatt Macy if (resume_nvl == NULL) { 1946eda14cbcSMatt Macy /* 1947eda14cbcSMatt Macy * zfs_error_aux has already been set by 1948eda14cbcSMatt Macy * zfs_send_resume_token_to_nvlist() 1949eda14cbcSMatt Macy */ 1950eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_FAULT, errbuf)); 1951eda14cbcSMatt Macy } 1952eda14cbcSMatt Macy 1953eda14cbcSMatt Macy ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl); 1954184c1b94SMartin Matuska fnvlist_free(resume_nvl); 1955eda14cbcSMatt Macy 1956eda14cbcSMatt Macy return (ret); 1957eda14cbcSMatt Macy } 1958eda14cbcSMatt Macy 1959eda14cbcSMatt Macy int 1960eda14cbcSMatt Macy zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd, 1961eda14cbcSMatt Macy const char *resume_token) 1962eda14cbcSMatt Macy { 1963eda14cbcSMatt Macy int ret; 1964eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zfs_hdl; 1965eda14cbcSMatt Macy nvlist_t *saved_nvl = NULL, *resume_nvl = NULL; 1966eda14cbcSMatt Macy uint64_t saved_guid = 0, resume_guid = 0; 1967eda14cbcSMatt Macy uint64_t obj = 0, off = 0, bytes = 0; 1968eda14cbcSMatt Macy char token_buf[ZFS_MAXPROPLEN]; 19691f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 1970eda14cbcSMatt Macy 1971eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1972eda14cbcSMatt Macy "saved send failed")); 1973eda14cbcSMatt Macy 1974eda14cbcSMatt Macy ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 1975eda14cbcSMatt Macy token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE); 1976eda14cbcSMatt Macy if (ret != 0) 1977eda14cbcSMatt Macy goto out; 1978eda14cbcSMatt Macy 1979eda14cbcSMatt Macy saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf); 1980eda14cbcSMatt Macy if (saved_nvl == NULL) { 1981eda14cbcSMatt Macy /* 1982eda14cbcSMatt Macy * zfs_error_aux has already been set by 1983eda14cbcSMatt Macy * zfs_send_resume_token_to_nvlist() 1984eda14cbcSMatt Macy */ 1985eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_FAULT, errbuf); 1986eda14cbcSMatt Macy goto out; 1987eda14cbcSMatt Macy } 1988eda14cbcSMatt Macy 1989eda14cbcSMatt Macy /* 1990eda14cbcSMatt Macy * If a resume token is provided we use the object and offset 1991eda14cbcSMatt Macy * from that instead of the default, which starts from the 1992eda14cbcSMatt Macy * beginning. 1993eda14cbcSMatt Macy */ 1994eda14cbcSMatt Macy if (resume_token != NULL) { 1995eda14cbcSMatt Macy resume_nvl = zfs_send_resume_token_to_nvlist(hdl, 1996eda14cbcSMatt Macy resume_token); 1997eda14cbcSMatt Macy if (resume_nvl == NULL) { 1998eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_FAULT, errbuf); 1999eda14cbcSMatt Macy goto out; 2000eda14cbcSMatt Macy } 2001eda14cbcSMatt Macy 2002eda14cbcSMatt Macy if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 || 2003eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 || 2004eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 || 2005eda14cbcSMatt Macy nvlist_lookup_uint64(resume_nvl, "toguid", 2006eda14cbcSMatt Macy &resume_guid) != 0) { 2007eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2008eda14cbcSMatt Macy "provided resume token is corrupt")); 2009eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_FAULT, errbuf); 2010eda14cbcSMatt Macy goto out; 2011eda14cbcSMatt Macy } 2012eda14cbcSMatt Macy 2013eda14cbcSMatt Macy if (nvlist_lookup_uint64(saved_nvl, "toguid", 2014eda14cbcSMatt Macy &saved_guid)) { 2015eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2016eda14cbcSMatt Macy "dataset's resume token is corrupt")); 2017eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_FAULT, errbuf); 2018eda14cbcSMatt Macy goto out; 2019eda14cbcSMatt Macy } 2020eda14cbcSMatt Macy 2021eda14cbcSMatt Macy if (resume_guid != saved_guid) { 2022eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2023eda14cbcSMatt Macy "provided resume token does not match dataset")); 2024eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf); 2025eda14cbcSMatt Macy goto out; 2026eda14cbcSMatt Macy } 2027eda14cbcSMatt Macy } 2028eda14cbcSMatt Macy 2029eda14cbcSMatt Macy (void) nvlist_remove_all(saved_nvl, "object"); 2030eda14cbcSMatt Macy fnvlist_add_uint64(saved_nvl, "object", obj); 2031eda14cbcSMatt Macy 2032eda14cbcSMatt Macy (void) nvlist_remove_all(saved_nvl, "offset"); 2033eda14cbcSMatt Macy fnvlist_add_uint64(saved_nvl, "offset", off); 2034eda14cbcSMatt Macy 2035eda14cbcSMatt Macy (void) nvlist_remove_all(saved_nvl, "bytes"); 2036eda14cbcSMatt Macy fnvlist_add_uint64(saved_nvl, "bytes", bytes); 2037eda14cbcSMatt Macy 2038eda14cbcSMatt Macy (void) nvlist_remove_all(saved_nvl, "toname"); 2039eda14cbcSMatt Macy fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name); 2040eda14cbcSMatt Macy 2041eda14cbcSMatt Macy ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl); 2042eda14cbcSMatt Macy 2043eda14cbcSMatt Macy out: 2044184c1b94SMartin Matuska fnvlist_free(saved_nvl); 2045184c1b94SMartin Matuska fnvlist_free(resume_nvl); 2046eda14cbcSMatt Macy return (ret); 2047eda14cbcSMatt Macy } 2048eda14cbcSMatt Macy 2049eda14cbcSMatt Macy /* 2050eda14cbcSMatt Macy * This function informs the target system that the recursive send is complete. 2051eda14cbcSMatt Macy * The record is also expected in the case of a send -p. 2052eda14cbcSMatt Macy */ 2053eda14cbcSMatt Macy static int 2054eda14cbcSMatt Macy send_conclusion_record(int fd, zio_cksum_t *zc) 2055eda14cbcSMatt Macy { 2056eda14cbcSMatt Macy dmu_replay_record_t drr = { 0 }; 2057eda14cbcSMatt Macy drr.drr_type = DRR_END; 2058eda14cbcSMatt Macy if (zc != NULL) 2059eda14cbcSMatt Macy drr.drr_u.drr_end.drr_checksum = *zc; 2060eda14cbcSMatt Macy if (write(fd, &drr, sizeof (drr)) == -1) { 2061eda14cbcSMatt Macy return (errno); 2062eda14cbcSMatt Macy } 2063eda14cbcSMatt Macy return (0); 2064eda14cbcSMatt Macy } 2065eda14cbcSMatt Macy 2066eda14cbcSMatt Macy /* 2067eda14cbcSMatt Macy * This function is responsible for sending the records that contain the 2068eda14cbcSMatt Macy * necessary information for the target system's libzfs to be able to set the 2069eda14cbcSMatt Macy * properties of the filesystem being received, or to be able to prepare for 2070eda14cbcSMatt Macy * a recursive receive. 2071eda14cbcSMatt Macy * 2072eda14cbcSMatt Macy * The "zhp" argument is the handle of the snapshot we are sending 2073eda14cbcSMatt Macy * (the "tosnap"). The "from" argument is the short snapshot name (the part 2074eda14cbcSMatt Macy * after the @) of the incremental source. 2075eda14cbcSMatt Macy */ 2076eda14cbcSMatt Macy static int 2077eda14cbcSMatt Macy send_prelim_records(zfs_handle_t *zhp, const char *from, int fd, 2078eda14cbcSMatt Macy boolean_t gather_props, boolean_t recursive, boolean_t verbose, 207916038816SMartin Matuska boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t skipmissing, 208016038816SMartin Matuska boolean_t backup, boolean_t holds, boolean_t props, boolean_t doall, 2081eda14cbcSMatt Macy nvlist_t **fssp, avl_tree_t **fsavlp) 2082eda14cbcSMatt Macy { 2083eda14cbcSMatt Macy int err = 0; 2084eda14cbcSMatt Macy char *packbuf = NULL; 2085eda14cbcSMatt Macy size_t buflen = 0; 2086eda14cbcSMatt Macy zio_cksum_t zc = { {0} }; 2087eda14cbcSMatt Macy int featureflags = 0; 2088eda14cbcSMatt Macy /* name of filesystem/volume that contains snapshot we are sending */ 2089eda14cbcSMatt Macy char tofs[ZFS_MAX_DATASET_NAME_LEN]; 2090eda14cbcSMatt Macy /* short name of snap we are sending */ 2091a0b956f5SMartin Matuska const char *tosnap = ""; 2092eda14cbcSMatt Macy 20931f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 2094eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2095eda14cbcSMatt Macy "warning: cannot send '%s'"), zhp->zfs_name); 2096eda14cbcSMatt Macy if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp, 2097eda14cbcSMatt Macy ZFS_PROP_VERSION) >= ZPL_VERSION_SA) { 2098eda14cbcSMatt Macy featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; 2099eda14cbcSMatt Macy } 2100eda14cbcSMatt Macy 2101eda14cbcSMatt Macy if (holds) 2102eda14cbcSMatt Macy featureflags |= DMU_BACKUP_FEATURE_HOLDS; 2103eda14cbcSMatt Macy 2104eda14cbcSMatt Macy (void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN); 2105eda14cbcSMatt Macy char *at = strchr(tofs, '@'); 2106eda14cbcSMatt Macy if (at != NULL) { 2107eda14cbcSMatt Macy *at = '\0'; 2108eda14cbcSMatt Macy tosnap = at + 1; 2109eda14cbcSMatt Macy } 2110eda14cbcSMatt Macy 2111eda14cbcSMatt Macy if (gather_props) { 2112eda14cbcSMatt Macy nvlist_t *hdrnv = fnvlist_alloc(); 2113eda14cbcSMatt Macy nvlist_t *fss = NULL; 2114eda14cbcSMatt Macy 2115eda14cbcSMatt Macy if (from != NULL) 2116eda14cbcSMatt Macy fnvlist_add_string(hdrnv, "fromsnap", from); 2117eda14cbcSMatt Macy fnvlist_add_string(hdrnv, "tosnap", tosnap); 2118eda14cbcSMatt Macy if (!recursive) 2119eda14cbcSMatt Macy fnvlist_add_boolean(hdrnv, "not_recursive"); 2120eda14cbcSMatt Macy 2121eda14cbcSMatt Macy if (raw) { 2122184c1b94SMartin Matuska fnvlist_add_boolean(hdrnv, "raw"); 2123eda14cbcSMatt Macy } 2124eda14cbcSMatt Macy 2125eda14cbcSMatt Macy if ((err = gather_nvlist(zhp->zfs_hdl, tofs, 212616038816SMartin Matuska from, tosnap, recursive, raw, doall, replicate, skipmissing, 212716038816SMartin Matuska verbose, backup, holds, props, &fss, fsavlp)) != 0) { 2128eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP, 2129eda14cbcSMatt Macy errbuf)); 2130eda14cbcSMatt Macy } 2131ac0bf12eSMatt Macy /* 2132ac0bf12eSMatt Macy * Do not allow the size of the properties list to exceed 2133ac0bf12eSMatt Macy * the limit 2134ac0bf12eSMatt Macy */ 2135ac0bf12eSMatt Macy if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) > 2136ac0bf12eSMatt Macy zhp->zfs_hdl->libzfs_max_nvlist) { 2137ac0bf12eSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), 2138ac0bf12eSMatt Macy dgettext(TEXT_DOMAIN, "warning: cannot send '%s': " 2139ac0bf12eSMatt Macy "the size of the list of snapshots and properties " 2140ac0bf12eSMatt Macy "is too large to be received successfully.\n" 2141ac0bf12eSMatt Macy "Select a smaller number of snapshots to send.\n"), 2142ac0bf12eSMatt Macy zhp->zfs_name); 2143ac0bf12eSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_NOSPC, 2144ac0bf12eSMatt Macy errbuf)); 2145ac0bf12eSMatt Macy } 2146eda14cbcSMatt Macy fnvlist_add_nvlist(hdrnv, "fss", fss); 2147eda14cbcSMatt Macy VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR, 2148eda14cbcSMatt Macy 0)); 2149eda14cbcSMatt Macy if (fssp != NULL) { 2150eda14cbcSMatt Macy *fssp = fss; 2151eda14cbcSMatt Macy } else { 2152184c1b94SMartin Matuska fnvlist_free(fss); 2153eda14cbcSMatt Macy } 2154184c1b94SMartin Matuska fnvlist_free(hdrnv); 2155eda14cbcSMatt Macy } 2156eda14cbcSMatt Macy 2157eda14cbcSMatt Macy if (!dryrun) { 2158eda14cbcSMatt Macy dmu_replay_record_t drr = { 0 }; 2159eda14cbcSMatt Macy /* write first begin record */ 2160eda14cbcSMatt Macy drr.drr_type = DRR_BEGIN; 2161eda14cbcSMatt Macy drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 2162eda14cbcSMatt Macy DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin. 2163eda14cbcSMatt Macy drr_versioninfo, DMU_COMPOUNDSTREAM); 2164eda14cbcSMatt Macy DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin. 2165eda14cbcSMatt Macy drr_versioninfo, featureflags); 2166eda14cbcSMatt Macy if (snprintf(drr.drr_u.drr_begin.drr_toname, 2167eda14cbcSMatt Macy sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs, 2168eda14cbcSMatt Macy tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) { 2169eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP, 2170eda14cbcSMatt Macy errbuf)); 2171eda14cbcSMatt Macy } 2172eda14cbcSMatt Macy drr.drr_payloadlen = buflen; 2173eda14cbcSMatt Macy 2174eda14cbcSMatt Macy err = dump_record(&drr, packbuf, buflen, &zc, fd); 2175eda14cbcSMatt Macy free(packbuf); 2176eda14cbcSMatt Macy if (err != 0) { 217716038816SMartin Matuska zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err)); 2178eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP, 2179eda14cbcSMatt Macy errbuf)); 2180eda14cbcSMatt Macy } 2181eda14cbcSMatt Macy err = send_conclusion_record(fd, &zc); 2182eda14cbcSMatt Macy if (err != 0) { 218316038816SMartin Matuska zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err)); 2184eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP, 2185eda14cbcSMatt Macy errbuf)); 2186eda14cbcSMatt Macy } 2187eda14cbcSMatt Macy } 2188eda14cbcSMatt Macy return (0); 2189eda14cbcSMatt Macy } 2190eda14cbcSMatt Macy 2191eda14cbcSMatt Macy /* 2192eda14cbcSMatt Macy * Generate a send stream. The "zhp" argument is the filesystem/volume 2193eda14cbcSMatt Macy * that contains the snapshot to send. The "fromsnap" argument is the 2194eda14cbcSMatt Macy * short name (the part after the '@') of the snapshot that is the 2195eda14cbcSMatt Macy * incremental source to send from (if non-NULL). The "tosnap" argument 2196eda14cbcSMatt Macy * is the short name of the snapshot to send. 2197eda14cbcSMatt Macy * 2198eda14cbcSMatt Macy * The content of the send stream is the snapshot identified by 2199eda14cbcSMatt Macy * 'tosnap'. Incremental streams are requested in two ways: 2200eda14cbcSMatt Macy * - from the snapshot identified by "fromsnap" (if non-null) or 2201eda14cbcSMatt Macy * - from the origin of the dataset identified by zhp, which must 2202eda14cbcSMatt Macy * be a clone. In this case, "fromsnap" is null and "fromorigin" 2203eda14cbcSMatt Macy * is TRUE. 2204eda14cbcSMatt Macy * 2205eda14cbcSMatt Macy * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and 2206eda14cbcSMatt Macy * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM) 2207eda14cbcSMatt Macy * if "replicate" is set. If "doall" is set, dump all the intermediate 2208eda14cbcSMatt Macy * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall" 2209eda14cbcSMatt Macy * case too. If "props" is set, send properties. 2210c03c5b1cSMartin Matuska * 2211c03c5b1cSMartin Matuska * Pre-wrapped (cf. lzc_send_wrapper()). 2212eda14cbcSMatt Macy */ 2213c03c5b1cSMartin Matuska static int 2214c03c5b1cSMartin Matuska zfs_send_cb_impl(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 2215eda14cbcSMatt Macy sendflags_t *flags, int outfd, snapfilter_cb_t filter_func, 2216eda14cbcSMatt Macy void *cb_arg, nvlist_t **debugnvp) 2217eda14cbcSMatt Macy { 22181f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 2219eda14cbcSMatt Macy send_dump_data_t sdd = { 0 }; 2220eda14cbcSMatt Macy int err = 0; 2221eda14cbcSMatt Macy nvlist_t *fss = NULL; 2222eda14cbcSMatt Macy avl_tree_t *fsavl = NULL; 2223eda14cbcSMatt Macy static uint64_t holdseq; 2224eda14cbcSMatt Macy int spa_version; 2225eda14cbcSMatt Macy FILE *fout; 2226eda14cbcSMatt Macy 2227eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2228eda14cbcSMatt Macy "cannot send '%s'"), zhp->zfs_name); 2229eda14cbcSMatt Macy 2230eda14cbcSMatt Macy if (fromsnap && fromsnap[0] == '\0') { 2231eda14cbcSMatt Macy zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2232eda14cbcSMatt Macy "zero-length incremental source")); 2233eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 2234eda14cbcSMatt Macy } 2235eda14cbcSMatt Macy 22366ba2210eSMartin Matuska if (fromsnap) { 22376ba2210eSMartin Matuska char full_fromsnap_name[ZFS_MAX_DATASET_NAME_LEN]; 22386ba2210eSMartin Matuska if (snprintf(full_fromsnap_name, sizeof (full_fromsnap_name), 22396ba2210eSMartin Matuska "%s@%s", zhp->zfs_name, fromsnap) >= 22406ba2210eSMartin Matuska sizeof (full_fromsnap_name)) { 22416ba2210eSMartin Matuska err = EINVAL; 22426ba2210eSMartin Matuska goto stderr_out; 22436ba2210eSMartin Matuska } 22446ba2210eSMartin Matuska zfs_handle_t *fromsnapn = zfs_open(zhp->zfs_hdl, 22456ba2210eSMartin Matuska full_fromsnap_name, ZFS_TYPE_SNAPSHOT); 22466ba2210eSMartin Matuska if (fromsnapn == NULL) { 22476ba2210eSMartin Matuska err = -1; 22486ba2210eSMartin Matuska goto err_out; 22496ba2210eSMartin Matuska } 22506ba2210eSMartin Matuska zfs_close(fromsnapn); 22516ba2210eSMartin Matuska } 22526ba2210eSMartin Matuska 2253eda14cbcSMatt Macy if (flags->replicate || flags->doall || flags->props || 2254eda14cbcSMatt Macy flags->holds || flags->backup) { 2255eda14cbcSMatt Macy char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN]; 2256eda14cbcSMatt Macy if (snprintf(full_tosnap_name, sizeof (full_tosnap_name), 2257eda14cbcSMatt Macy "%s@%s", zhp->zfs_name, tosnap) >= 2258eda14cbcSMatt Macy sizeof (full_tosnap_name)) { 2259eda14cbcSMatt Macy err = EINVAL; 2260eda14cbcSMatt Macy goto stderr_out; 2261eda14cbcSMatt Macy } 2262eda14cbcSMatt Macy zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl, 2263eda14cbcSMatt Macy full_tosnap_name, ZFS_TYPE_SNAPSHOT); 2264eda14cbcSMatt Macy if (tosnap == NULL) { 2265eda14cbcSMatt Macy err = -1; 2266eda14cbcSMatt Macy goto err_out; 2267eda14cbcSMatt Macy } 2268eda14cbcSMatt Macy err = send_prelim_records(tosnap, fromsnap, outfd, 2269eda14cbcSMatt Macy flags->replicate || flags->props || flags->holds, 2270eda14cbcSMatt Macy flags->replicate, flags->verbosity > 0, flags->dryrun, 227116038816SMartin Matuska flags->raw, flags->replicate, flags->skipmissing, 227216038816SMartin Matuska flags->backup, flags->holds, flags->props, flags->doall, 227316038816SMartin Matuska &fss, &fsavl); 2274eda14cbcSMatt Macy zfs_close(tosnap); 2275eda14cbcSMatt Macy if (err != 0) 2276eda14cbcSMatt Macy goto err_out; 2277eda14cbcSMatt Macy } 2278eda14cbcSMatt Macy 2279eda14cbcSMatt Macy /* dump each stream */ 2280eda14cbcSMatt Macy sdd.fromsnap = fromsnap; 2281eda14cbcSMatt Macy sdd.tosnap = tosnap; 2282eda14cbcSMatt Macy sdd.outfd = outfd; 2283eda14cbcSMatt Macy sdd.replicate = flags->replicate; 2284eda14cbcSMatt Macy sdd.doall = flags->doall; 2285eda14cbcSMatt Macy sdd.fromorigin = flags->fromorigin; 2286eda14cbcSMatt Macy sdd.fss = fss; 2287eda14cbcSMatt Macy sdd.fsavl = fsavl; 2288eda14cbcSMatt Macy sdd.verbosity = flags->verbosity; 2289eda14cbcSMatt Macy sdd.parsable = flags->parsable; 2290eda14cbcSMatt Macy sdd.progress = flags->progress; 2291eda14cbcSMatt Macy sdd.dryrun = flags->dryrun; 2292eda14cbcSMatt Macy sdd.large_block = flags->largeblock; 2293eda14cbcSMatt Macy sdd.embed_data = flags->embed_data; 2294eda14cbcSMatt Macy sdd.compress = flags->compress; 2295eda14cbcSMatt Macy sdd.raw = flags->raw; 2296eda14cbcSMatt Macy sdd.holds = flags->holds; 2297eda14cbcSMatt Macy sdd.filter_cb = filter_func; 2298eda14cbcSMatt Macy sdd.filter_cb_arg = cb_arg; 2299eda14cbcSMatt Macy if (debugnvp) 2300eda14cbcSMatt Macy sdd.debugnv = *debugnvp; 2301eda14cbcSMatt Macy if (sdd.verbosity != 0 && sdd.dryrun) 2302eda14cbcSMatt Macy sdd.std_out = B_TRUE; 2303eda14cbcSMatt Macy fout = sdd.std_out ? stdout : stderr; 2304eda14cbcSMatt Macy 2305eda14cbcSMatt Macy /* 2306eda14cbcSMatt Macy * Some flags require that we place user holds on the datasets that are 2307eda14cbcSMatt Macy * being sent so they don't get destroyed during the send. We can skip 2308eda14cbcSMatt Macy * this step if the pool is imported read-only since the datasets cannot 2309eda14cbcSMatt Macy * be destroyed. 2310eda14cbcSMatt Macy */ 2311eda14cbcSMatt Macy if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp), 2312eda14cbcSMatt Macy ZPOOL_PROP_READONLY, NULL) && 2313eda14cbcSMatt Macy zfs_spa_version(zhp, &spa_version) == 0 && 2314eda14cbcSMatt Macy spa_version >= SPA_VERSION_USERREFS && 2315eda14cbcSMatt Macy (flags->doall || flags->replicate)) { 2316eda14cbcSMatt Macy ++holdseq; 2317eda14cbcSMatt Macy (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag), 2318eda14cbcSMatt Macy ".send-%d-%llu", getpid(), (u_longlong_t)holdseq); 231916038816SMartin Matuska sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC); 2320eda14cbcSMatt Macy if (sdd.cleanup_fd < 0) { 2321eda14cbcSMatt Macy err = errno; 2322eda14cbcSMatt Macy goto stderr_out; 2323eda14cbcSMatt Macy } 2324eda14cbcSMatt Macy sdd.snapholds = fnvlist_alloc(); 2325eda14cbcSMatt Macy } else { 2326eda14cbcSMatt Macy sdd.cleanup_fd = -1; 2327eda14cbcSMatt Macy sdd.snapholds = NULL; 2328eda14cbcSMatt Macy } 2329eda14cbcSMatt Macy 2330eda14cbcSMatt Macy if (flags->verbosity != 0 || sdd.snapholds != NULL) { 2331eda14cbcSMatt Macy /* 2332eda14cbcSMatt Macy * Do a verbose no-op dry run to get all the verbose output 2333eda14cbcSMatt Macy * or to gather snapshot hold's before generating any data, 2334eda14cbcSMatt Macy * then do a non-verbose real run to generate the streams. 2335eda14cbcSMatt Macy */ 2336eda14cbcSMatt Macy sdd.dryrun = B_TRUE; 2337eda14cbcSMatt Macy err = dump_filesystems(zhp, &sdd); 2338eda14cbcSMatt Macy 2339eda14cbcSMatt Macy if (err != 0) 2340eda14cbcSMatt Macy goto stderr_out; 2341eda14cbcSMatt Macy 2342eda14cbcSMatt Macy if (flags->verbosity != 0) { 2343eda14cbcSMatt Macy if (flags->parsable) { 2344eda14cbcSMatt Macy (void) fprintf(fout, "size\t%llu\n", 2345eda14cbcSMatt Macy (longlong_t)sdd.size); 2346eda14cbcSMatt Macy } else { 2347eda14cbcSMatt Macy char buf[16]; 2348eda14cbcSMatt Macy zfs_nicebytes(sdd.size, buf, sizeof (buf)); 2349eda14cbcSMatt Macy (void) fprintf(fout, dgettext(TEXT_DOMAIN, 2350eda14cbcSMatt Macy "total estimated size is %s\n"), buf); 2351eda14cbcSMatt Macy } 2352eda14cbcSMatt Macy } 2353eda14cbcSMatt Macy 2354eda14cbcSMatt Macy /* Ensure no snaps found is treated as an error. */ 2355eda14cbcSMatt Macy if (!sdd.seento) { 2356eda14cbcSMatt Macy err = ENOENT; 2357eda14cbcSMatt Macy goto err_out; 2358eda14cbcSMatt Macy } 2359eda14cbcSMatt Macy 2360eda14cbcSMatt Macy /* Skip the second run if dryrun was requested. */ 2361eda14cbcSMatt Macy if (flags->dryrun) 2362eda14cbcSMatt Macy goto err_out; 2363eda14cbcSMatt Macy 2364eda14cbcSMatt Macy if (sdd.snapholds != NULL) { 2365eda14cbcSMatt Macy err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds); 2366eda14cbcSMatt Macy if (err != 0) 2367eda14cbcSMatt Macy goto stderr_out; 2368eda14cbcSMatt Macy 2369eda14cbcSMatt Macy fnvlist_free(sdd.snapholds); 2370eda14cbcSMatt Macy sdd.snapholds = NULL; 2371eda14cbcSMatt Macy } 2372eda14cbcSMatt Macy 2373eda14cbcSMatt Macy sdd.dryrun = B_FALSE; 2374eda14cbcSMatt Macy sdd.verbosity = 0; 2375eda14cbcSMatt Macy } 2376eda14cbcSMatt Macy 2377eda14cbcSMatt Macy err = dump_filesystems(zhp, &sdd); 2378eda14cbcSMatt Macy fsavl_destroy(fsavl); 2379184c1b94SMartin Matuska fnvlist_free(fss); 2380eda14cbcSMatt Macy 2381eda14cbcSMatt Macy /* Ensure no snaps found is treated as an error. */ 2382eda14cbcSMatt Macy if (err == 0 && !sdd.seento) 2383eda14cbcSMatt Macy err = ENOENT; 2384eda14cbcSMatt Macy 2385eda14cbcSMatt Macy if (sdd.cleanup_fd != -1) { 2386eda14cbcSMatt Macy VERIFY(0 == close(sdd.cleanup_fd)); 2387eda14cbcSMatt Macy sdd.cleanup_fd = -1; 2388eda14cbcSMatt Macy } 2389eda14cbcSMatt Macy 2390eda14cbcSMatt Macy if (!flags->dryrun && (flags->replicate || flags->doall || 2391eda14cbcSMatt Macy flags->props || flags->backup || flags->holds)) { 2392eda14cbcSMatt Macy /* 2393eda14cbcSMatt Macy * write final end record. NB: want to do this even if 2394eda14cbcSMatt Macy * there was some error, because it might not be totally 2395eda14cbcSMatt Macy * failed. 2396eda14cbcSMatt Macy */ 23971f1e2261SMartin Matuska int err2 = send_conclusion_record(outfd, NULL); 23981f1e2261SMartin Matuska if (err2 != 0) 23991f1e2261SMartin Matuska return (zfs_standard_error(zhp->zfs_hdl, err2, errbuf)); 2400eda14cbcSMatt Macy } 2401eda14cbcSMatt Macy 2402eda14cbcSMatt Macy return (err || sdd.err); 2403eda14cbcSMatt Macy 2404eda14cbcSMatt Macy stderr_out: 2405eda14cbcSMatt Macy err = zfs_standard_error(zhp->zfs_hdl, err, errbuf); 2406eda14cbcSMatt Macy err_out: 2407eda14cbcSMatt Macy fsavl_destroy(fsavl); 2408184c1b94SMartin Matuska fnvlist_free(fss); 2409eda14cbcSMatt Macy fnvlist_free(sdd.snapholds); 2410eda14cbcSMatt Macy 2411eda14cbcSMatt Macy if (sdd.cleanup_fd != -1) 2412eda14cbcSMatt Macy VERIFY(0 == close(sdd.cleanup_fd)); 2413eda14cbcSMatt Macy return (err); 2414eda14cbcSMatt Macy } 2415eda14cbcSMatt Macy 2416c03c5b1cSMartin Matuska struct zfs_send { 2417c03c5b1cSMartin Matuska zfs_handle_t *zhp; 2418c03c5b1cSMartin Matuska const char *fromsnap; 2419c03c5b1cSMartin Matuska const char *tosnap; 2420c03c5b1cSMartin Matuska sendflags_t *flags; 2421c03c5b1cSMartin Matuska snapfilter_cb_t *filter_func; 2422c03c5b1cSMartin Matuska void *cb_arg; 2423c03c5b1cSMartin Matuska nvlist_t **debugnvp; 2424c03c5b1cSMartin Matuska }; 2425c03c5b1cSMartin Matuska 2426c03c5b1cSMartin Matuska static int 2427c03c5b1cSMartin Matuska zfs_send_cb(int outfd, void *arg) 2428c03c5b1cSMartin Matuska { 2429c03c5b1cSMartin Matuska struct zfs_send *zs = arg; 2430c03c5b1cSMartin Matuska return (zfs_send_cb_impl(zs->zhp, zs->fromsnap, zs->tosnap, zs->flags, 2431c03c5b1cSMartin Matuska outfd, zs->filter_func, zs->cb_arg, zs->debugnvp)); 2432c03c5b1cSMartin Matuska } 2433c03c5b1cSMartin Matuska 2434c03c5b1cSMartin Matuska int 2435c03c5b1cSMartin Matuska zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 2436c03c5b1cSMartin Matuska sendflags_t *flags, int outfd, snapfilter_cb_t filter_func, 2437c03c5b1cSMartin Matuska void *cb_arg, nvlist_t **debugnvp) 2438c03c5b1cSMartin Matuska { 2439c03c5b1cSMartin Matuska struct zfs_send arg = { 2440c03c5b1cSMartin Matuska .zhp = zhp, 2441c03c5b1cSMartin Matuska .fromsnap = fromsnap, 2442c03c5b1cSMartin Matuska .tosnap = tosnap, 2443c03c5b1cSMartin Matuska .flags = flags, 2444c03c5b1cSMartin Matuska .filter_func = filter_func, 2445c03c5b1cSMartin Matuska .cb_arg = cb_arg, 2446c03c5b1cSMartin Matuska .debugnvp = debugnvp, 2447c03c5b1cSMartin Matuska }; 2448c03c5b1cSMartin Matuska return (lzc_send_wrapper(zfs_send_cb, outfd, &arg)); 2449c03c5b1cSMartin Matuska } 2450c03c5b1cSMartin Matuska 2451c03c5b1cSMartin Matuska 2452eda14cbcSMatt Macy static zfs_handle_t * 2453eda14cbcSMatt Macy name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname) 2454eda14cbcSMatt Macy { 2455eda14cbcSMatt Macy char dirname[ZFS_MAX_DATASET_NAME_LEN]; 2456eda14cbcSMatt Macy (void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN); 2457eda14cbcSMatt Macy char *c = strchr(dirname, '@'); 2458eda14cbcSMatt Macy if (c != NULL) 2459eda14cbcSMatt Macy *c = '\0'; 2460eda14cbcSMatt Macy return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET)); 2461eda14cbcSMatt Macy } 2462eda14cbcSMatt Macy 2463eda14cbcSMatt Macy /* 2464eda14cbcSMatt Macy * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either 2465eda14cbcSMatt Macy * an earlier snapshot in the same filesystem, or a snapshot before later's 2466eda14cbcSMatt Macy * origin, or it's origin's origin, etc. 2467eda14cbcSMatt Macy */ 2468eda14cbcSMatt Macy static boolean_t 2469eda14cbcSMatt Macy snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later) 2470eda14cbcSMatt Macy { 2471eda14cbcSMatt Macy boolean_t ret; 2472eda14cbcSMatt Macy uint64_t later_txg = 2473eda14cbcSMatt Macy (later->zfs_type == ZFS_TYPE_FILESYSTEM || 2474eda14cbcSMatt Macy later->zfs_type == ZFS_TYPE_VOLUME ? 2475eda14cbcSMatt Macy UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG)); 2476eda14cbcSMatt Macy uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG); 2477eda14cbcSMatt Macy 2478eda14cbcSMatt Macy if (earlier_txg >= later_txg) 2479eda14cbcSMatt Macy return (B_FALSE); 2480eda14cbcSMatt Macy 2481eda14cbcSMatt Macy zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl, 2482eda14cbcSMatt Macy earlier->zfs_name); 2483eda14cbcSMatt Macy zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl, 2484eda14cbcSMatt Macy later->zfs_name); 2485eda14cbcSMatt Macy 2486eda14cbcSMatt Macy if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) { 2487eda14cbcSMatt Macy zfs_close(earlier_dir); 2488eda14cbcSMatt Macy zfs_close(later_dir); 2489eda14cbcSMatt Macy return (B_TRUE); 2490eda14cbcSMatt Macy } 2491eda14cbcSMatt Macy 2492eda14cbcSMatt Macy char clonename[ZFS_MAX_DATASET_NAME_LEN]; 2493eda14cbcSMatt Macy if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename, 2494eda14cbcSMatt Macy ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) { 2495eda14cbcSMatt Macy zfs_close(earlier_dir); 2496eda14cbcSMatt Macy zfs_close(later_dir); 2497eda14cbcSMatt Macy return (B_FALSE); 2498eda14cbcSMatt Macy } 2499eda14cbcSMatt Macy 2500eda14cbcSMatt Macy zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename, 2501eda14cbcSMatt Macy ZFS_TYPE_DATASET); 2502eda14cbcSMatt Macy uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG); 2503eda14cbcSMatt Macy 2504eda14cbcSMatt Macy /* 2505eda14cbcSMatt Macy * If "earlier" is exactly the origin, then 2506eda14cbcSMatt Macy * snapshot_is_before(earlier, origin) will return false (because 2507eda14cbcSMatt Macy * they're the same). 2508eda14cbcSMatt Macy */ 2509eda14cbcSMatt Macy if (origin_txg == earlier_txg && 2510eda14cbcSMatt Macy strcmp(origin->zfs_name, earlier->zfs_name) == 0) { 2511eda14cbcSMatt Macy zfs_close(earlier_dir); 2512eda14cbcSMatt Macy zfs_close(later_dir); 2513eda14cbcSMatt Macy zfs_close(origin); 2514eda14cbcSMatt Macy return (B_TRUE); 2515eda14cbcSMatt Macy } 2516eda14cbcSMatt Macy zfs_close(earlier_dir); 2517eda14cbcSMatt Macy zfs_close(later_dir); 2518eda14cbcSMatt Macy 2519eda14cbcSMatt Macy ret = snapshot_is_before(earlier, origin); 2520eda14cbcSMatt Macy zfs_close(origin); 2521eda14cbcSMatt Macy return (ret); 2522eda14cbcSMatt Macy } 2523eda14cbcSMatt Macy 2524eda14cbcSMatt Macy /* 2525eda14cbcSMatt Macy * The "zhp" argument is the handle of the dataset to send (typically a 2526eda14cbcSMatt Macy * snapshot). The "from" argument is the full name of the snapshot or 2527eda14cbcSMatt Macy * bookmark that is the incremental source. 2528c03c5b1cSMartin Matuska * 2529c03c5b1cSMartin Matuska * Pre-wrapped (cf. lzc_send_wrapper()). 2530eda14cbcSMatt Macy */ 2531c03c5b1cSMartin Matuska static int 2532c03c5b1cSMartin Matuska zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd, 2533c03c5b1cSMartin Matuska sendflags_t *flags, const char *redactbook) 2534eda14cbcSMatt Macy { 2535eda14cbcSMatt Macy int err; 2536eda14cbcSMatt Macy libzfs_handle_t *hdl = zhp->zfs_hdl; 2537eda14cbcSMatt Macy char *name = zhp->zfs_name; 2538eda14cbcSMatt Macy pthread_t ptid; 2539eda14cbcSMatt Macy progress_arg_t pa = { 0 }; 2540eda14cbcSMatt Macy 25411f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 2542eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2543eda14cbcSMatt Macy "warning: cannot send '%s'"), name); 2544eda14cbcSMatt Macy 2545eda14cbcSMatt Macy if (from != NULL && strchr(from, '@')) { 2546eda14cbcSMatt Macy zfs_handle_t *from_zhp = zfs_open(hdl, from, 2547eda14cbcSMatt Macy ZFS_TYPE_DATASET); 2548eda14cbcSMatt Macy if (from_zhp == NULL) 2549eda14cbcSMatt Macy return (-1); 2550eda14cbcSMatt Macy if (!snapshot_is_before(from_zhp, zhp)) { 2551eda14cbcSMatt Macy zfs_close(from_zhp); 2552eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2553eda14cbcSMatt Macy "not an earlier snapshot from the same fs")); 2554eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 2555eda14cbcSMatt Macy } 2556eda14cbcSMatt Macy zfs_close(from_zhp); 2557eda14cbcSMatt Macy } 2558eda14cbcSMatt Macy 2559eda14cbcSMatt Macy if (redactbook != NULL) { 2560eda14cbcSMatt Macy char bookname[ZFS_MAX_DATASET_NAME_LEN]; 2561eda14cbcSMatt Macy nvlist_t *redact_snaps; 2562eda14cbcSMatt Macy zfs_handle_t *book_zhp; 2563eda14cbcSMatt Macy char *at, *pound; 2564eda14cbcSMatt Macy int dsnamelen; 2565eda14cbcSMatt Macy 2566eda14cbcSMatt Macy pound = strchr(redactbook, '#'); 2567eda14cbcSMatt Macy if (pound != NULL) 2568eda14cbcSMatt Macy redactbook = pound + 1; 2569eda14cbcSMatt Macy at = strchr(name, '@'); 2570eda14cbcSMatt Macy if (at == NULL) { 2571eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2572eda14cbcSMatt Macy "cannot do a redacted send to a filesystem")); 2573eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2574eda14cbcSMatt Macy } 2575eda14cbcSMatt Macy dsnamelen = at - name; 2576eda14cbcSMatt Macy if (snprintf(bookname, sizeof (bookname), "%.*s#%s", 2577eda14cbcSMatt Macy dsnamelen, name, redactbook) 2578eda14cbcSMatt Macy >= sizeof (bookname)) { 2579eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2580eda14cbcSMatt Macy "invalid bookmark name")); 2581eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2582eda14cbcSMatt Macy } 2583eda14cbcSMatt Macy book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK); 2584eda14cbcSMatt Macy if (book_zhp == NULL) 2585eda14cbcSMatt Macy return (-1); 2586eda14cbcSMatt Macy if (nvlist_lookup_nvlist(book_zhp->zfs_props, 2587eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), 2588eda14cbcSMatt Macy &redact_snaps) != 0 || redact_snaps == NULL) { 2589eda14cbcSMatt Macy zfs_close(book_zhp); 2590eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2591eda14cbcSMatt Macy "not a redaction bookmark")); 2592eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2593eda14cbcSMatt Macy } 2594eda14cbcSMatt Macy zfs_close(book_zhp); 2595eda14cbcSMatt Macy } 2596eda14cbcSMatt Macy 2597eda14cbcSMatt Macy /* 2598eda14cbcSMatt Macy * Send fs properties 2599eda14cbcSMatt Macy */ 2600eda14cbcSMatt Macy if (flags->props || flags->holds || flags->backup) { 2601eda14cbcSMatt Macy /* 2602eda14cbcSMatt Macy * Note: the header generated by send_prelim_records() 2603eda14cbcSMatt Macy * assumes that the incremental source is in the same 2604eda14cbcSMatt Macy * filesystem/volume as the target (which is a requirement 2605eda14cbcSMatt Macy * when doing "zfs send -R"). But that isn't always the 2606eda14cbcSMatt Macy * case here (e.g. send from snap in origin, or send from 2607eda14cbcSMatt Macy * bookmark). We pass from=NULL, which will omit this 2608eda14cbcSMatt Macy * information from the prelim records; it isn't used 2609eda14cbcSMatt Macy * when receiving this type of stream. 2610eda14cbcSMatt Macy */ 2611eda14cbcSMatt Macy err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE, 2612eda14cbcSMatt Macy flags->verbosity > 0, flags->dryrun, flags->raw, 261316038816SMartin Matuska flags->replicate, B_FALSE, flags->backup, flags->holds, 2614eda14cbcSMatt Macy flags->props, flags->doall, NULL, NULL); 2615eda14cbcSMatt Macy if (err != 0) 2616eda14cbcSMatt Macy return (err); 2617eda14cbcSMatt Macy } 2618eda14cbcSMatt Macy 2619eda14cbcSMatt Macy /* 2620eda14cbcSMatt Macy * Perform size estimate if verbose was specified. 2621eda14cbcSMatt Macy */ 2622eda14cbcSMatt Macy if (flags->verbosity != 0) { 2623eda14cbcSMatt Macy err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook, 2624eda14cbcSMatt Macy errbuf); 2625eda14cbcSMatt Macy if (err != 0) 2626eda14cbcSMatt Macy return (err); 2627eda14cbcSMatt Macy } 2628eda14cbcSMatt Macy 2629eda14cbcSMatt Macy if (flags->dryrun) 2630eda14cbcSMatt Macy return (0); 2631eda14cbcSMatt Macy 2632eda14cbcSMatt Macy /* 2633eda14cbcSMatt Macy * If progress reporting is requested, spawn a new thread to poll 2634eda14cbcSMatt Macy * ZFS_IOC_SEND_PROGRESS at a regular interval. 2635eda14cbcSMatt Macy */ 2636eda14cbcSMatt Macy if (flags->progress) { 2637eda14cbcSMatt Macy pa.pa_zhp = zhp; 2638eda14cbcSMatt Macy pa.pa_fd = fd; 2639eda14cbcSMatt Macy pa.pa_parsable = flags->parsable; 2640eda14cbcSMatt Macy pa.pa_estimate = B_FALSE; 2641eda14cbcSMatt Macy pa.pa_verbosity = flags->verbosity; 2642eda14cbcSMatt Macy 2643eda14cbcSMatt Macy err = pthread_create(&ptid, NULL, 2644eda14cbcSMatt Macy send_progress_thread, &pa); 2645eda14cbcSMatt Macy if (err != 0) { 264616038816SMartin Matuska zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno)); 2647eda14cbcSMatt Macy return (zfs_error(zhp->zfs_hdl, 2648eda14cbcSMatt Macy EZFS_THREADCREATEFAILED, errbuf)); 2649eda14cbcSMatt Macy } 2650eda14cbcSMatt Macy } 2651eda14cbcSMatt Macy 2652eda14cbcSMatt Macy err = lzc_send_redacted(name, from, fd, 2653eda14cbcSMatt Macy lzc_flags_from_sendflags(flags), redactbook); 2654eda14cbcSMatt Macy 2655716fd348SMartin Matuska if (flags->progress && send_progress_thread_exit(hdl, ptid)) 2656716fd348SMartin Matuska return (-1); 2657eda14cbcSMatt Macy 2658e92ffd9bSMartin Matuska if (err == 0 && (flags->props || flags->holds || flags->backup)) { 2659eda14cbcSMatt Macy /* Write the final end record. */ 26603f9d360cSMartin Matuska err = send_conclusion_record(fd, NULL); 2661eda14cbcSMatt Macy if (err != 0) 2662eda14cbcSMatt Macy return (zfs_standard_error(hdl, err, errbuf)); 2663eda14cbcSMatt Macy } 2664eda14cbcSMatt Macy if (err != 0) { 2665eda14cbcSMatt Macy switch (errno) { 2666eda14cbcSMatt Macy case EXDEV: 2667eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2668eda14cbcSMatt Macy "not an earlier snapshot from the same fs")); 2669eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 2670eda14cbcSMatt Macy 2671eda14cbcSMatt Macy case ENOENT: 2672eda14cbcSMatt Macy case ESRCH: 2673eda14cbcSMatt Macy if (lzc_exists(name)) { 2674eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2675eda14cbcSMatt Macy "incremental source (%s) does not exist"), 2676eda14cbcSMatt Macy from); 2677eda14cbcSMatt Macy } 2678eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2679eda14cbcSMatt Macy 2680eda14cbcSMatt Macy case EACCES: 2681eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2682eda14cbcSMatt Macy "dataset key must be loaded")); 2683eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); 2684eda14cbcSMatt Macy 2685eda14cbcSMatt Macy case EBUSY: 2686eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2687eda14cbcSMatt Macy "target is busy; if a filesystem, " 2688eda14cbcSMatt Macy "it must not be mounted")); 2689eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BUSY, errbuf)); 2690eda14cbcSMatt Macy 2691eda14cbcSMatt Macy case EDQUOT: 2692eda14cbcSMatt Macy case EFAULT: 2693eda14cbcSMatt Macy case EFBIG: 2694eda14cbcSMatt Macy case EINVAL: 2695eda14cbcSMatt Macy case EIO: 2696eda14cbcSMatt Macy case ENOLINK: 2697eda14cbcSMatt Macy case ENOSPC: 2698eda14cbcSMatt Macy case ENOSTR: 2699eda14cbcSMatt Macy case ENXIO: 2700eda14cbcSMatt Macy case EPIPE: 2701eda14cbcSMatt Macy case ERANGE: 2702eda14cbcSMatt Macy case EROFS: 270316038816SMartin Matuska zfs_error_aux(hdl, "%s", strerror(errno)); 2704eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 2705a0b956f5SMartin Matuska case ENOTSUP: 2706a0b956f5SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2707a0b956f5SMartin Matuska "large blocks detected but large_blocks feature " 2708a0b956f5SMartin Matuska "is inactive; raw send unsupported")); 2709a0b956f5SMartin Matuska return (zfs_error(hdl, EZFS_NOTSUP, errbuf)); 2710eda14cbcSMatt Macy 2711eda14cbcSMatt Macy default: 2712eda14cbcSMatt Macy return (zfs_standard_error(hdl, errno, errbuf)); 2713eda14cbcSMatt Macy } 2714eda14cbcSMatt Macy } 2715eda14cbcSMatt Macy return (err != 0); 2716eda14cbcSMatt Macy } 2717eda14cbcSMatt Macy 2718c03c5b1cSMartin Matuska struct zfs_send_one { 2719c03c5b1cSMartin Matuska zfs_handle_t *zhp; 2720c03c5b1cSMartin Matuska const char *from; 2721c03c5b1cSMartin Matuska sendflags_t *flags; 2722c03c5b1cSMartin Matuska const char *redactbook; 2723c03c5b1cSMartin Matuska }; 2724c03c5b1cSMartin Matuska 2725c03c5b1cSMartin Matuska static int 2726c03c5b1cSMartin Matuska zfs_send_one_cb(int fd, void *arg) 2727c03c5b1cSMartin Matuska { 2728c03c5b1cSMartin Matuska struct zfs_send_one *zso = arg; 2729c03c5b1cSMartin Matuska return (zfs_send_one_cb_impl(zso->zhp, zso->from, fd, zso->flags, 2730c03c5b1cSMartin Matuska zso->redactbook)); 2731c03c5b1cSMartin Matuska } 2732c03c5b1cSMartin Matuska 2733c03c5b1cSMartin Matuska int 2734c03c5b1cSMartin Matuska zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags, 2735c03c5b1cSMartin Matuska const char *redactbook) 2736c03c5b1cSMartin Matuska { 2737c03c5b1cSMartin Matuska struct zfs_send_one zso = { 2738c03c5b1cSMartin Matuska .zhp = zhp, 2739c03c5b1cSMartin Matuska .from = from, 2740c03c5b1cSMartin Matuska .flags = flags, 2741c03c5b1cSMartin Matuska .redactbook = redactbook, 2742c03c5b1cSMartin Matuska }; 2743c03c5b1cSMartin Matuska return (lzc_send_wrapper(zfs_send_one_cb, fd, &zso)); 2744c03c5b1cSMartin Matuska } 2745c03c5b1cSMartin Matuska 2746eda14cbcSMatt Macy /* 2747eda14cbcSMatt Macy * Routines specific to "zfs recv" 2748eda14cbcSMatt Macy */ 2749eda14cbcSMatt Macy 2750eda14cbcSMatt Macy static int 2751eda14cbcSMatt Macy recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, 2752eda14cbcSMatt Macy boolean_t byteswap, zio_cksum_t *zc) 2753eda14cbcSMatt Macy { 2754eda14cbcSMatt Macy char *cp = buf; 2755eda14cbcSMatt Macy int rv; 2756eda14cbcSMatt Macy int len = ilen; 2757eda14cbcSMatt Macy 2758eda14cbcSMatt Macy do { 2759eda14cbcSMatt Macy rv = read(fd, cp, len); 2760eda14cbcSMatt Macy cp += rv; 2761eda14cbcSMatt Macy len -= rv; 2762eda14cbcSMatt Macy } while (rv > 0); 2763eda14cbcSMatt Macy 2764eda14cbcSMatt Macy if (rv < 0 || len != 0) { 2765eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2766eda14cbcSMatt Macy "failed to read from stream")); 2767eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, 2768eda14cbcSMatt Macy "cannot receive"))); 2769eda14cbcSMatt Macy } 2770eda14cbcSMatt Macy 2771eda14cbcSMatt Macy if (zc) { 2772eda14cbcSMatt Macy if (byteswap) 2773eda14cbcSMatt Macy fletcher_4_incremental_byteswap(buf, ilen, zc); 2774eda14cbcSMatt Macy else 2775eda14cbcSMatt Macy fletcher_4_incremental_native(buf, ilen, zc); 2776eda14cbcSMatt Macy } 2777eda14cbcSMatt Macy return (0); 2778eda14cbcSMatt Macy } 2779eda14cbcSMatt Macy 2780eda14cbcSMatt Macy static int 2781eda14cbcSMatt Macy recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, 2782eda14cbcSMatt Macy boolean_t byteswap, zio_cksum_t *zc) 2783eda14cbcSMatt Macy { 2784eda14cbcSMatt Macy char *buf; 2785eda14cbcSMatt Macy int err; 2786eda14cbcSMatt Macy 2787eda14cbcSMatt Macy buf = zfs_alloc(hdl, len); 2788eda14cbcSMatt Macy 2789ac0bf12eSMatt Macy if (len > hdl->libzfs_max_nvlist) { 2790ac0bf12eSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large")); 27912c48331dSMatt Macy free(buf); 2792ac0bf12eSMatt Macy return (ENOMEM); 2793ac0bf12eSMatt Macy } 2794ac0bf12eSMatt Macy 2795eda14cbcSMatt Macy err = recv_read(hdl, fd, buf, len, byteswap, zc); 2796eda14cbcSMatt Macy if (err != 0) { 2797eda14cbcSMatt Macy free(buf); 2798eda14cbcSMatt Macy return (err); 2799eda14cbcSMatt Macy } 2800eda14cbcSMatt Macy 2801eda14cbcSMatt Macy err = nvlist_unpack(buf, len, nvp, 0); 2802eda14cbcSMatt Macy free(buf); 2803eda14cbcSMatt Macy if (err != 0) { 2804eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 2805eda14cbcSMatt Macy "stream (malformed nvlist)")); 2806eda14cbcSMatt Macy return (EINVAL); 2807eda14cbcSMatt Macy } 2808eda14cbcSMatt Macy return (0); 2809eda14cbcSMatt Macy } 2810eda14cbcSMatt Macy 2811eda14cbcSMatt Macy /* 2812eda14cbcSMatt Macy * Returns the grand origin (origin of origin of origin...) of a given handle. 2813eda14cbcSMatt Macy * If this dataset is not a clone, it simply returns a copy of the original 2814eda14cbcSMatt Macy * handle. 2815eda14cbcSMatt Macy */ 2816eda14cbcSMatt Macy static zfs_handle_t * 2817eda14cbcSMatt Macy recv_open_grand_origin(zfs_handle_t *zhp) 2818eda14cbcSMatt Macy { 2819eda14cbcSMatt Macy char origin[ZFS_MAX_DATASET_NAME_LEN]; 2820eda14cbcSMatt Macy zprop_source_t src; 2821eda14cbcSMatt Macy zfs_handle_t *ozhp = zfs_handle_dup(zhp); 2822eda14cbcSMatt Macy 2823eda14cbcSMatt Macy while (ozhp != NULL) { 2824eda14cbcSMatt Macy if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin, 2825eda14cbcSMatt Macy sizeof (origin), &src, NULL, 0, B_FALSE) != 0) 2826eda14cbcSMatt Macy break; 2827eda14cbcSMatt Macy 2828eda14cbcSMatt Macy (void) zfs_close(ozhp); 2829eda14cbcSMatt Macy ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM); 2830eda14cbcSMatt Macy } 2831eda14cbcSMatt Macy 2832eda14cbcSMatt Macy return (ozhp); 2833eda14cbcSMatt Macy } 2834eda14cbcSMatt Macy 2835eda14cbcSMatt Macy static int 2836eda14cbcSMatt Macy recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname) 2837eda14cbcSMatt Macy { 2838eda14cbcSMatt Macy int err; 2839eda14cbcSMatt Macy zfs_handle_t *ozhp = NULL; 2840eda14cbcSMatt Macy 2841eda14cbcSMatt Macy /* 2842eda14cbcSMatt Macy * Attempt to rename the dataset. If it fails with EACCES we have 2843eda14cbcSMatt Macy * attempted to rename the dataset outside of its encryption root. 2844eda14cbcSMatt Macy * Force the dataset to become an encryption root and try again. 2845eda14cbcSMatt Macy */ 2846eda14cbcSMatt Macy err = lzc_rename(name, newname); 2847eda14cbcSMatt Macy if (err == EACCES) { 2848eda14cbcSMatt Macy ozhp = recv_open_grand_origin(zhp); 2849eda14cbcSMatt Macy if (ozhp == NULL) { 2850eda14cbcSMatt Macy err = ENOENT; 2851eda14cbcSMatt Macy goto out; 2852eda14cbcSMatt Macy } 2853eda14cbcSMatt Macy 2854eda14cbcSMatt Macy err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY, 2855eda14cbcSMatt Macy NULL, NULL, 0); 2856eda14cbcSMatt Macy if (err != 0) 2857eda14cbcSMatt Macy goto out; 2858eda14cbcSMatt Macy 2859eda14cbcSMatt Macy err = lzc_rename(name, newname); 2860eda14cbcSMatt Macy } 2861eda14cbcSMatt Macy 2862eda14cbcSMatt Macy out: 2863eda14cbcSMatt Macy if (ozhp != NULL) 2864eda14cbcSMatt Macy zfs_close(ozhp); 2865eda14cbcSMatt Macy return (err); 2866eda14cbcSMatt Macy } 2867eda14cbcSMatt Macy 2868eda14cbcSMatt Macy static int 2869eda14cbcSMatt Macy recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, 2870eda14cbcSMatt Macy int baselen, char *newname, recvflags_t *flags) 2871eda14cbcSMatt Macy { 2872eda14cbcSMatt Macy static int seq; 2873eda14cbcSMatt Macy int err; 2874eda14cbcSMatt Macy prop_changelist_t *clp = NULL; 2875eda14cbcSMatt Macy zfs_handle_t *zhp = NULL; 2876eda14cbcSMatt Macy 2877eda14cbcSMatt Macy zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 2878eda14cbcSMatt Macy if (zhp == NULL) { 2879eda14cbcSMatt Macy err = -1; 2880eda14cbcSMatt Macy goto out; 2881eda14cbcSMatt Macy } 2882eda14cbcSMatt Macy clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 2883eda14cbcSMatt Macy flags->force ? MS_FORCE : 0); 2884eda14cbcSMatt Macy if (clp == NULL) { 2885eda14cbcSMatt Macy err = -1; 2886eda14cbcSMatt Macy goto out; 2887eda14cbcSMatt Macy } 2888eda14cbcSMatt Macy err = changelist_prefix(clp); 2889eda14cbcSMatt Macy if (err) 2890eda14cbcSMatt Macy goto out; 2891eda14cbcSMatt Macy 2892eda14cbcSMatt Macy if (tryname) { 2893eda14cbcSMatt Macy (void) strcpy(newname, tryname); 2894eda14cbcSMatt Macy if (flags->verbose) { 2895eda14cbcSMatt Macy (void) printf("attempting rename %s to %s\n", 2896eda14cbcSMatt Macy name, newname); 2897eda14cbcSMatt Macy } 2898eda14cbcSMatt Macy err = recv_rename_impl(zhp, name, newname); 2899eda14cbcSMatt Macy if (err == 0) 2900eda14cbcSMatt Macy changelist_rename(clp, name, tryname); 2901eda14cbcSMatt Macy } else { 2902eda14cbcSMatt Macy err = ENOENT; 2903eda14cbcSMatt Macy } 2904eda14cbcSMatt Macy 2905eda14cbcSMatt Macy if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) { 2906eda14cbcSMatt Macy seq++; 2907eda14cbcSMatt Macy 2908eda14cbcSMatt Macy (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN, 2909eda14cbcSMatt Macy "%.*srecv-%u-%u", baselen, name, getpid(), seq); 2910eda14cbcSMatt Macy 2911eda14cbcSMatt Macy if (flags->verbose) { 2912eda14cbcSMatt Macy (void) printf("failed - trying rename %s to %s\n", 2913eda14cbcSMatt Macy name, newname); 2914eda14cbcSMatt Macy } 2915eda14cbcSMatt Macy err = recv_rename_impl(zhp, name, newname); 2916eda14cbcSMatt Macy if (err == 0) 2917eda14cbcSMatt Macy changelist_rename(clp, name, newname); 2918eda14cbcSMatt Macy if (err && flags->verbose) { 2919eda14cbcSMatt Macy (void) printf("failed (%u) - " 2920eda14cbcSMatt Macy "will try again on next pass\n", errno); 2921eda14cbcSMatt Macy } 2922eda14cbcSMatt Macy err = EAGAIN; 2923eda14cbcSMatt Macy } else if (flags->verbose) { 2924eda14cbcSMatt Macy if (err == 0) 2925eda14cbcSMatt Macy (void) printf("success\n"); 2926eda14cbcSMatt Macy else 2927eda14cbcSMatt Macy (void) printf("failed (%u)\n", errno); 2928eda14cbcSMatt Macy } 2929eda14cbcSMatt Macy 2930eda14cbcSMatt Macy (void) changelist_postfix(clp); 2931eda14cbcSMatt Macy 2932eda14cbcSMatt Macy out: 2933eda14cbcSMatt Macy if (clp != NULL) 2934eda14cbcSMatt Macy changelist_free(clp); 2935eda14cbcSMatt Macy if (zhp != NULL) 2936eda14cbcSMatt Macy zfs_close(zhp); 2937eda14cbcSMatt Macy 2938eda14cbcSMatt Macy return (err); 2939eda14cbcSMatt Macy } 2940eda14cbcSMatt Macy 2941eda14cbcSMatt Macy static int 2942eda14cbcSMatt Macy recv_promote(libzfs_handle_t *hdl, const char *fsname, 2943eda14cbcSMatt Macy const char *origin_fsname, recvflags_t *flags) 2944eda14cbcSMatt Macy { 2945eda14cbcSMatt Macy int err; 2946eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 2947eda14cbcSMatt Macy zfs_handle_t *zhp = NULL, *ozhp = NULL; 2948eda14cbcSMatt Macy 2949eda14cbcSMatt Macy if (flags->verbose) 2950eda14cbcSMatt Macy (void) printf("promoting %s\n", fsname); 2951eda14cbcSMatt Macy 2952eda14cbcSMatt Macy (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value)); 2953eda14cbcSMatt Macy (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name)); 2954eda14cbcSMatt Macy 2955eda14cbcSMatt Macy /* 2956eda14cbcSMatt Macy * Attempt to promote the dataset. If it fails with EACCES the 2957eda14cbcSMatt Macy * promotion would cause this dataset to leave its encryption root. 2958eda14cbcSMatt Macy * Force the origin to become an encryption root and try again. 2959eda14cbcSMatt Macy */ 2960eda14cbcSMatt Macy err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 2961eda14cbcSMatt Macy if (err == EACCES) { 2962eda14cbcSMatt Macy zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET); 2963eda14cbcSMatt Macy if (zhp == NULL) { 2964eda14cbcSMatt Macy err = -1; 2965eda14cbcSMatt Macy goto out; 2966eda14cbcSMatt Macy } 2967eda14cbcSMatt Macy 2968eda14cbcSMatt Macy ozhp = recv_open_grand_origin(zhp); 2969eda14cbcSMatt Macy if (ozhp == NULL) { 2970eda14cbcSMatt Macy err = -1; 2971eda14cbcSMatt Macy goto out; 2972eda14cbcSMatt Macy } 2973eda14cbcSMatt Macy 2974eda14cbcSMatt Macy err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY, 2975eda14cbcSMatt Macy NULL, NULL, 0); 2976eda14cbcSMatt Macy if (err != 0) 2977eda14cbcSMatt Macy goto out; 2978eda14cbcSMatt Macy 2979eda14cbcSMatt Macy err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 2980eda14cbcSMatt Macy } 2981eda14cbcSMatt Macy 2982eda14cbcSMatt Macy out: 2983eda14cbcSMatt Macy if (zhp != NULL) 2984eda14cbcSMatt Macy zfs_close(zhp); 2985eda14cbcSMatt Macy if (ozhp != NULL) 2986eda14cbcSMatt Macy zfs_close(ozhp); 2987eda14cbcSMatt Macy 2988eda14cbcSMatt Macy return (err); 2989eda14cbcSMatt Macy } 2990eda14cbcSMatt Macy 2991eda14cbcSMatt Macy static int 2992eda14cbcSMatt Macy recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, 2993eda14cbcSMatt Macy char *newname, recvflags_t *flags) 2994eda14cbcSMatt Macy { 2995eda14cbcSMatt Macy int err = 0; 2996eda14cbcSMatt Macy prop_changelist_t *clp; 2997eda14cbcSMatt Macy zfs_handle_t *zhp; 2998eda14cbcSMatt Macy boolean_t defer = B_FALSE; 2999eda14cbcSMatt Macy int spa_version; 3000eda14cbcSMatt Macy 3001eda14cbcSMatt Macy zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 3002eda14cbcSMatt Macy if (zhp == NULL) 3003eda14cbcSMatt Macy return (-1); 3004c03c5b1cSMartin Matuska zfs_type_t type = zfs_get_type(zhp); 3005c03c5b1cSMartin Matuska if (type == ZFS_TYPE_SNAPSHOT && 3006eda14cbcSMatt Macy zfs_spa_version(zhp, &spa_version) == 0 && 3007eda14cbcSMatt Macy spa_version >= SPA_VERSION_USERREFS) 3008eda14cbcSMatt Macy defer = B_TRUE; 3009c03c5b1cSMartin Matuska clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 3010c03c5b1cSMartin Matuska flags->force ? MS_FORCE : 0); 3011eda14cbcSMatt Macy zfs_close(zhp); 3012eda14cbcSMatt Macy if (clp == NULL) 3013eda14cbcSMatt Macy return (-1); 3014c03c5b1cSMartin Matuska 3015eda14cbcSMatt Macy err = changelist_prefix(clp); 3016eda14cbcSMatt Macy if (err) 3017eda14cbcSMatt Macy return (err); 3018eda14cbcSMatt Macy 3019eda14cbcSMatt Macy if (flags->verbose) 3020eda14cbcSMatt Macy (void) printf("attempting destroy %s\n", name); 3021c03c5b1cSMartin Matuska if (type == ZFS_TYPE_SNAPSHOT) { 3022eda14cbcSMatt Macy nvlist_t *nv = fnvlist_alloc(); 3023eda14cbcSMatt Macy fnvlist_add_boolean(nv, name); 3024eda14cbcSMatt Macy err = lzc_destroy_snaps(nv, defer, NULL); 3025eda14cbcSMatt Macy fnvlist_free(nv); 3026eda14cbcSMatt Macy } else { 3027eda14cbcSMatt Macy err = lzc_destroy(name); 3028eda14cbcSMatt Macy } 3029eda14cbcSMatt Macy if (err == 0) { 3030eda14cbcSMatt Macy if (flags->verbose) 3031eda14cbcSMatt Macy (void) printf("success\n"); 3032eda14cbcSMatt Macy changelist_remove(clp, name); 3033eda14cbcSMatt Macy } 3034eda14cbcSMatt Macy 3035eda14cbcSMatt Macy (void) changelist_postfix(clp); 3036eda14cbcSMatt Macy changelist_free(clp); 3037eda14cbcSMatt Macy 3038eda14cbcSMatt Macy /* 3039eda14cbcSMatt Macy * Deferred destroy might destroy the snapshot or only mark it to be 3040eda14cbcSMatt Macy * destroyed later, and it returns success in either case. 3041eda14cbcSMatt Macy */ 3042eda14cbcSMatt Macy if (err != 0 || (defer && zfs_dataset_exists(hdl, name, 3043eda14cbcSMatt Macy ZFS_TYPE_SNAPSHOT))) { 3044eda14cbcSMatt Macy err = recv_rename(hdl, name, NULL, baselen, newname, flags); 3045eda14cbcSMatt Macy } 3046eda14cbcSMatt Macy 3047eda14cbcSMatt Macy return (err); 3048eda14cbcSMatt Macy } 3049eda14cbcSMatt Macy 3050eda14cbcSMatt Macy typedef struct guid_to_name_data { 3051eda14cbcSMatt Macy uint64_t guid; 3052eda14cbcSMatt Macy boolean_t bookmark_ok; 3053eda14cbcSMatt Macy char *name; 3054eda14cbcSMatt Macy char *skip; 3055eda14cbcSMatt Macy uint64_t *redact_snap_guids; 3056eda14cbcSMatt Macy uint64_t num_redact_snaps; 3057eda14cbcSMatt Macy } guid_to_name_data_t; 3058eda14cbcSMatt Macy 3059eda14cbcSMatt Macy static boolean_t 3060eda14cbcSMatt Macy redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd) 3061eda14cbcSMatt Macy { 3062eda14cbcSMatt Macy uint64_t *bmark_snaps; 3063eda14cbcSMatt Macy uint_t bmark_num_snaps; 3064eda14cbcSMatt Macy nvlist_t *nvl; 3065eda14cbcSMatt Macy if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) 3066eda14cbcSMatt Macy return (B_FALSE); 3067eda14cbcSMatt Macy 3068eda14cbcSMatt Macy nvl = fnvlist_lookup_nvlist(zhp->zfs_props, 3069eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); 3070eda14cbcSMatt Macy bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE, 3071eda14cbcSMatt Macy &bmark_num_snaps); 3072eda14cbcSMatt Macy if (bmark_num_snaps != gtnd->num_redact_snaps) 3073eda14cbcSMatt Macy return (B_FALSE); 3074eda14cbcSMatt Macy int i = 0; 3075eda14cbcSMatt Macy for (; i < bmark_num_snaps; i++) { 3076eda14cbcSMatt Macy int j = 0; 3077eda14cbcSMatt Macy for (; j < bmark_num_snaps; j++) { 3078eda14cbcSMatt Macy if (bmark_snaps[i] == gtnd->redact_snap_guids[j]) 3079eda14cbcSMatt Macy break; 3080eda14cbcSMatt Macy } 3081eda14cbcSMatt Macy if (j == bmark_num_snaps) 3082eda14cbcSMatt Macy break; 3083eda14cbcSMatt Macy } 3084eda14cbcSMatt Macy return (i == bmark_num_snaps); 3085eda14cbcSMatt Macy } 3086eda14cbcSMatt Macy 3087eda14cbcSMatt Macy static int 3088eda14cbcSMatt Macy guid_to_name_cb(zfs_handle_t *zhp, void *arg) 3089eda14cbcSMatt Macy { 3090eda14cbcSMatt Macy guid_to_name_data_t *gtnd = arg; 3091eda14cbcSMatt Macy const char *slash; 3092eda14cbcSMatt Macy int err; 3093eda14cbcSMatt Macy 3094eda14cbcSMatt Macy if (gtnd->skip != NULL && 3095eda14cbcSMatt Macy (slash = strrchr(zhp->zfs_name, '/')) != NULL && 3096eda14cbcSMatt Macy strcmp(slash + 1, gtnd->skip) == 0) { 3097eda14cbcSMatt Macy zfs_close(zhp); 3098eda14cbcSMatt Macy return (0); 3099eda14cbcSMatt Macy } 3100eda14cbcSMatt Macy 3101eda14cbcSMatt Macy if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid && 3102eda14cbcSMatt Macy (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) { 3103eda14cbcSMatt Macy (void) strcpy(gtnd->name, zhp->zfs_name); 3104eda14cbcSMatt Macy zfs_close(zhp); 3105eda14cbcSMatt Macy return (EEXIST); 3106eda14cbcSMatt Macy } 3107eda14cbcSMatt Macy 3108eda14cbcSMatt Macy err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); 3109eda14cbcSMatt Macy if (err != EEXIST && gtnd->bookmark_ok) 3110eda14cbcSMatt Macy err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd); 3111eda14cbcSMatt Macy zfs_close(zhp); 3112eda14cbcSMatt Macy return (err); 3113eda14cbcSMatt Macy } 3114eda14cbcSMatt Macy 3115eda14cbcSMatt Macy /* 3116eda14cbcSMatt Macy * Attempt to find the local dataset associated with this guid. In the case of 3117eda14cbcSMatt Macy * multiple matches, we attempt to find the "best" match by searching 3118eda14cbcSMatt Macy * progressively larger portions of the hierarchy. This allows one to send a 3119eda14cbcSMatt Macy * tree of datasets individually and guarantee that we will find the source 3120eda14cbcSMatt Macy * guid within that hierarchy, even if there are multiple matches elsewhere. 3121eda14cbcSMatt Macy * 3122eda14cbcSMatt Macy * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with 3123eda14cbcSMatt Macy * the specified number of redaction snapshots. If num_redact_snaps isn't 0 or 3124eda14cbcSMatt Macy * -1, then redact_snap_guids will be an array of the guids of the snapshots the 3125eda14cbcSMatt Macy * redaction bookmark was created with. If num_redact_snaps is -1, then we will 3126eda14cbcSMatt Macy * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the 3127eda14cbcSMatt Macy * given guid. Note that a redaction bookmark can be returned if 3128eda14cbcSMatt Macy * num_redact_snaps == -1. 3129eda14cbcSMatt Macy */ 3130eda14cbcSMatt Macy static int 3131eda14cbcSMatt Macy guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent, 3132eda14cbcSMatt Macy uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids, 3133eda14cbcSMatt Macy uint64_t num_redact_snaps, char *name) 3134eda14cbcSMatt Macy { 3135eda14cbcSMatt Macy char pname[ZFS_MAX_DATASET_NAME_LEN]; 3136eda14cbcSMatt Macy guid_to_name_data_t gtnd; 3137eda14cbcSMatt Macy 3138eda14cbcSMatt Macy gtnd.guid = guid; 3139eda14cbcSMatt Macy gtnd.bookmark_ok = bookmark_ok; 3140eda14cbcSMatt Macy gtnd.name = name; 3141eda14cbcSMatt Macy gtnd.skip = NULL; 3142eda14cbcSMatt Macy gtnd.redact_snap_guids = redact_snap_guids; 3143eda14cbcSMatt Macy gtnd.num_redact_snaps = num_redact_snaps; 3144eda14cbcSMatt Macy 3145eda14cbcSMatt Macy /* 3146eda14cbcSMatt Macy * Search progressively larger portions of the hierarchy, starting 3147eda14cbcSMatt Macy * with the filesystem specified by 'parent'. This will 3148eda14cbcSMatt Macy * select the "most local" version of the origin snapshot in the case 3149eda14cbcSMatt Macy * that there are multiple matching snapshots in the system. 3150eda14cbcSMatt Macy */ 3151eda14cbcSMatt Macy (void) strlcpy(pname, parent, sizeof (pname)); 3152eda14cbcSMatt Macy char *cp = strrchr(pname, '@'); 3153eda14cbcSMatt Macy if (cp == NULL) 3154eda14cbcSMatt Macy cp = strchr(pname, '\0'); 3155eda14cbcSMatt Macy for (; cp != NULL; cp = strrchr(pname, '/')) { 3156eda14cbcSMatt Macy /* Chop off the last component and open the parent */ 3157eda14cbcSMatt Macy *cp = '\0'; 3158eda14cbcSMatt Macy zfs_handle_t *zhp = make_dataset_handle(hdl, pname); 3159eda14cbcSMatt Macy 3160eda14cbcSMatt Macy if (zhp == NULL) 3161eda14cbcSMatt Macy continue; 3162eda14cbcSMatt Macy int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd); 3163eda14cbcSMatt Macy if (err != EEXIST) 3164eda14cbcSMatt Macy err = zfs_iter_children(zhp, guid_to_name_cb, >nd); 3165eda14cbcSMatt Macy if (err != EEXIST && bookmark_ok) 3166eda14cbcSMatt Macy err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd); 3167eda14cbcSMatt Macy zfs_close(zhp); 3168eda14cbcSMatt Macy if (err == EEXIST) 3169eda14cbcSMatt Macy return (0); 3170eda14cbcSMatt Macy 3171eda14cbcSMatt Macy /* 3172eda14cbcSMatt Macy * Remember the last portion of the dataset so we skip it next 3173eda14cbcSMatt Macy * time through (as we've already searched that portion of the 3174eda14cbcSMatt Macy * hierarchy). 3175eda14cbcSMatt Macy */ 3176eda14cbcSMatt Macy gtnd.skip = strrchr(pname, '/') + 1; 3177eda14cbcSMatt Macy } 3178eda14cbcSMatt Macy 3179eda14cbcSMatt Macy return (ENOENT); 3180eda14cbcSMatt Macy } 3181eda14cbcSMatt Macy 3182eda14cbcSMatt Macy static int 3183eda14cbcSMatt Macy guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, 3184eda14cbcSMatt Macy boolean_t bookmark_ok, char *name) 3185eda14cbcSMatt Macy { 3186eda14cbcSMatt Macy return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL, 3187eda14cbcSMatt Macy -1, name)); 3188eda14cbcSMatt Macy } 3189eda14cbcSMatt Macy 3190eda14cbcSMatt Macy /* 3191eda14cbcSMatt Macy * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if 3192eda14cbcSMatt Macy * guid1 is after guid2. 3193eda14cbcSMatt Macy */ 3194eda14cbcSMatt Macy static int 3195eda14cbcSMatt Macy created_before(libzfs_handle_t *hdl, avl_tree_t *avl, 3196eda14cbcSMatt Macy uint64_t guid1, uint64_t guid2) 3197eda14cbcSMatt Macy { 3198eda14cbcSMatt Macy nvlist_t *nvfs; 3199eda14cbcSMatt Macy char *fsname = NULL, *snapname = NULL; 3200eda14cbcSMatt Macy char buf[ZFS_MAX_DATASET_NAME_LEN]; 3201eda14cbcSMatt Macy int rv; 3202eda14cbcSMatt Macy zfs_handle_t *guid1hdl, *guid2hdl; 3203eda14cbcSMatt Macy uint64_t create1, create2; 3204eda14cbcSMatt Macy 3205eda14cbcSMatt Macy if (guid2 == 0) 3206eda14cbcSMatt Macy return (0); 3207eda14cbcSMatt Macy if (guid1 == 0) 3208eda14cbcSMatt Macy return (1); 3209eda14cbcSMatt Macy 3210eda14cbcSMatt Macy nvfs = fsavl_find(avl, guid1, &snapname); 3211184c1b94SMartin Matuska fsname = fnvlist_lookup_string(nvfs, "name"); 3212eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 3213eda14cbcSMatt Macy guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 3214eda14cbcSMatt Macy if (guid1hdl == NULL) 3215eda14cbcSMatt Macy return (-1); 3216eda14cbcSMatt Macy 3217eda14cbcSMatt Macy nvfs = fsavl_find(avl, guid2, &snapname); 3218184c1b94SMartin Matuska fsname = fnvlist_lookup_string(nvfs, "name"); 3219eda14cbcSMatt Macy (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 3220eda14cbcSMatt Macy guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 3221eda14cbcSMatt Macy if (guid2hdl == NULL) { 3222eda14cbcSMatt Macy zfs_close(guid1hdl); 3223eda14cbcSMatt Macy return (-1); 3224eda14cbcSMatt Macy } 3225eda14cbcSMatt Macy 3226eda14cbcSMatt Macy create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG); 3227eda14cbcSMatt Macy create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG); 3228eda14cbcSMatt Macy 3229eda14cbcSMatt Macy if (create1 < create2) 3230eda14cbcSMatt Macy rv = -1; 3231eda14cbcSMatt Macy else if (create1 > create2) 3232eda14cbcSMatt Macy rv = +1; 3233eda14cbcSMatt Macy else 3234eda14cbcSMatt Macy rv = 0; 3235eda14cbcSMatt Macy 3236eda14cbcSMatt Macy zfs_close(guid1hdl); 3237eda14cbcSMatt Macy zfs_close(guid2hdl); 3238eda14cbcSMatt Macy 3239eda14cbcSMatt Macy return (rv); 3240eda14cbcSMatt Macy } 3241eda14cbcSMatt Macy 3242eda14cbcSMatt Macy /* 3243eda14cbcSMatt Macy * This function reestablishes the hierarchy of encryption roots after a 3244eda14cbcSMatt Macy * recursive incremental receive has completed. This must be done after the 3245eda14cbcSMatt Macy * second call to recv_incremental_replication() has renamed and promoted all 3246eda14cbcSMatt Macy * sent datasets to their final locations in the dataset hierarchy. 3247eda14cbcSMatt Macy */ 3248eda14cbcSMatt Macy static int 3249eda14cbcSMatt Macy recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs, 3250c03c5b1cSMartin Matuska nvlist_t *stream_nv) 3251eda14cbcSMatt Macy { 3252eda14cbcSMatt Macy int err; 3253eda14cbcSMatt Macy nvpair_t *fselem = NULL; 3254eda14cbcSMatt Macy nvlist_t *stream_fss; 3255eda14cbcSMatt Macy 3256184c1b94SMartin Matuska stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss"); 3257eda14cbcSMatt Macy 3258eda14cbcSMatt Macy while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) { 3259eda14cbcSMatt Macy zfs_handle_t *zhp = NULL; 3260eda14cbcSMatt Macy uint64_t crypt; 3261eda14cbcSMatt Macy nvlist_t *snaps, *props, *stream_nvfs = NULL; 3262eda14cbcSMatt Macy nvpair_t *snapel = NULL; 3263eda14cbcSMatt Macy boolean_t is_encroot, is_clone, stream_encroot; 3264eda14cbcSMatt Macy char *cp; 3265eda14cbcSMatt Macy char *stream_keylocation = NULL; 3266eda14cbcSMatt Macy char keylocation[MAXNAMELEN]; 3267eda14cbcSMatt Macy char fsname[ZFS_MAX_DATASET_NAME_LEN]; 3268eda14cbcSMatt Macy 3269eda14cbcSMatt Macy keylocation[0] = '\0'; 3270184c1b94SMartin Matuska stream_nvfs = fnvpair_value_nvlist(fselem); 3271184c1b94SMartin Matuska snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps"); 3272184c1b94SMartin Matuska props = fnvlist_lookup_nvlist(stream_nvfs, "props"); 3273eda14cbcSMatt Macy stream_encroot = nvlist_exists(stream_nvfs, "is_encroot"); 3274eda14cbcSMatt Macy 3275eda14cbcSMatt Macy /* find a snapshot from the stream that exists locally */ 3276eda14cbcSMatt Macy err = ENOENT; 3277eda14cbcSMatt Macy while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) { 3278eda14cbcSMatt Macy uint64_t guid; 3279eda14cbcSMatt Macy 3280184c1b94SMartin Matuska guid = fnvpair_value_uint64(snapel); 3281eda14cbcSMatt Macy err = guid_to_name(hdl, top_zfs, guid, B_FALSE, 3282eda14cbcSMatt Macy fsname); 3283eda14cbcSMatt Macy if (err == 0) 3284eda14cbcSMatt Macy break; 3285eda14cbcSMatt Macy } 3286eda14cbcSMatt Macy 3287eda14cbcSMatt Macy if (err != 0) 3288eda14cbcSMatt Macy continue; 3289eda14cbcSMatt Macy 3290eda14cbcSMatt Macy cp = strchr(fsname, '@'); 3291eda14cbcSMatt Macy if (cp != NULL) 3292eda14cbcSMatt Macy *cp = '\0'; 3293eda14cbcSMatt Macy 3294eda14cbcSMatt Macy zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET); 3295eda14cbcSMatt Macy if (zhp == NULL) { 3296eda14cbcSMatt Macy err = ENOENT; 3297eda14cbcSMatt Macy goto error; 3298eda14cbcSMatt Macy } 3299eda14cbcSMatt Macy 3300eda14cbcSMatt Macy crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION); 3301eda14cbcSMatt Macy is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0'; 3302eda14cbcSMatt Macy (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL); 3303eda14cbcSMatt Macy 3304eda14cbcSMatt Macy /* we don't need to do anything for unencrypted datasets */ 3305eda14cbcSMatt Macy if (crypt == ZIO_CRYPT_OFF) { 3306eda14cbcSMatt Macy zfs_close(zhp); 3307eda14cbcSMatt Macy continue; 3308eda14cbcSMatt Macy } 3309eda14cbcSMatt Macy 3310eda14cbcSMatt Macy /* 3311eda14cbcSMatt Macy * If the dataset is flagged as an encryption root, was not 3312eda14cbcSMatt Macy * received as a clone and is not currently an encryption root, 3313eda14cbcSMatt Macy * force it to become one. Fixup the keylocation if necessary. 3314eda14cbcSMatt Macy */ 3315eda14cbcSMatt Macy if (stream_encroot) { 3316eda14cbcSMatt Macy if (!is_clone && !is_encroot) { 3317eda14cbcSMatt Macy err = lzc_change_key(fsname, 3318eda14cbcSMatt Macy DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0); 3319eda14cbcSMatt Macy if (err != 0) { 3320eda14cbcSMatt Macy zfs_close(zhp); 3321eda14cbcSMatt Macy goto error; 3322eda14cbcSMatt Macy } 3323eda14cbcSMatt Macy } 3324eda14cbcSMatt Macy 3325184c1b94SMartin Matuska stream_keylocation = fnvlist_lookup_string(props, 3326184c1b94SMartin Matuska zfs_prop_to_name(ZFS_PROP_KEYLOCATION)); 3327eda14cbcSMatt Macy 3328eda14cbcSMatt Macy /* 3329eda14cbcSMatt Macy * Refresh the properties in case the call to 3330eda14cbcSMatt Macy * lzc_change_key() changed the value. 3331eda14cbcSMatt Macy */ 3332eda14cbcSMatt Macy zfs_refresh_properties(zhp); 3333eda14cbcSMatt Macy err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION, 3334eda14cbcSMatt Macy keylocation, sizeof (keylocation), NULL, NULL, 3335eda14cbcSMatt Macy 0, B_TRUE); 3336eda14cbcSMatt Macy if (err != 0) { 3337eda14cbcSMatt Macy zfs_close(zhp); 3338eda14cbcSMatt Macy goto error; 3339eda14cbcSMatt Macy } 3340eda14cbcSMatt Macy 3341eda14cbcSMatt Macy if (strcmp(keylocation, stream_keylocation) != 0) { 3342eda14cbcSMatt Macy err = zfs_prop_set(zhp, 3343eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_KEYLOCATION), 3344eda14cbcSMatt Macy stream_keylocation); 3345eda14cbcSMatt Macy if (err != 0) { 3346eda14cbcSMatt Macy zfs_close(zhp); 3347eda14cbcSMatt Macy goto error; 3348eda14cbcSMatt Macy } 3349eda14cbcSMatt Macy } 3350eda14cbcSMatt Macy } 3351eda14cbcSMatt Macy 3352eda14cbcSMatt Macy /* 3353eda14cbcSMatt Macy * If the dataset is not flagged as an encryption root and is 3354eda14cbcSMatt Macy * currently an encryption root, force it to inherit from its 3355eda14cbcSMatt Macy * parent. The root of a raw send should never be 3356eda14cbcSMatt Macy * force-inherited. 3357eda14cbcSMatt Macy */ 3358eda14cbcSMatt Macy if (!stream_encroot && is_encroot && 3359eda14cbcSMatt Macy strcmp(top_zfs, fsname) != 0) { 3360eda14cbcSMatt Macy err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT, 3361eda14cbcSMatt Macy NULL, NULL, 0); 3362eda14cbcSMatt Macy if (err != 0) { 3363eda14cbcSMatt Macy zfs_close(zhp); 3364eda14cbcSMatt Macy goto error; 3365eda14cbcSMatt Macy } 3366eda14cbcSMatt Macy } 3367eda14cbcSMatt Macy 3368eda14cbcSMatt Macy zfs_close(zhp); 3369eda14cbcSMatt Macy } 3370eda14cbcSMatt Macy 3371eda14cbcSMatt Macy return (0); 3372eda14cbcSMatt Macy 3373eda14cbcSMatt Macy error: 3374eda14cbcSMatt Macy return (err); 3375eda14cbcSMatt Macy } 3376eda14cbcSMatt Macy 3377eda14cbcSMatt Macy static int 3378eda14cbcSMatt Macy recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, 3379eda14cbcSMatt Macy recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl, 3380eda14cbcSMatt Macy nvlist_t *renamed) 3381eda14cbcSMatt Macy { 3382eda14cbcSMatt Macy nvlist_t *local_nv, *deleted = NULL; 3383eda14cbcSMatt Macy avl_tree_t *local_avl; 3384eda14cbcSMatt Macy nvpair_t *fselem, *nextfselem; 3385eda14cbcSMatt Macy char *fromsnap; 3386eda14cbcSMatt Macy char newname[ZFS_MAX_DATASET_NAME_LEN]; 3387eda14cbcSMatt Macy char guidname[32]; 3388eda14cbcSMatt Macy int error; 3389eda14cbcSMatt Macy boolean_t needagain, progress, recursive; 3390eda14cbcSMatt Macy char *s1, *s2; 3391eda14cbcSMatt Macy 3392184c1b94SMartin Matuska fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap"); 3393eda14cbcSMatt Macy 3394eda14cbcSMatt Macy recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 3395eda14cbcSMatt Macy ENOENT); 3396eda14cbcSMatt Macy 3397eda14cbcSMatt Macy if (flags->dryrun) 3398eda14cbcSMatt Macy return (0); 3399eda14cbcSMatt Macy 3400eda14cbcSMatt Macy again: 3401eda14cbcSMatt Macy needagain = progress = B_FALSE; 3402eda14cbcSMatt Macy 3403184c1b94SMartin Matuska deleted = fnvlist_alloc(); 3404eda14cbcSMatt Macy 3405eda14cbcSMatt Macy if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, 340616038816SMartin Matuska recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE, 3407eda14cbcSMatt Macy B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0) 3408eda14cbcSMatt Macy return (error); 3409eda14cbcSMatt Macy 3410eda14cbcSMatt Macy /* 3411eda14cbcSMatt Macy * Process deletes and renames 3412eda14cbcSMatt Macy */ 3413eda14cbcSMatt Macy for (fselem = nvlist_next_nvpair(local_nv, NULL); 3414eda14cbcSMatt Macy fselem; fselem = nextfselem) { 3415eda14cbcSMatt Macy nvlist_t *nvfs, *snaps; 3416eda14cbcSMatt Macy nvlist_t *stream_nvfs = NULL; 3417eda14cbcSMatt Macy nvpair_t *snapelem, *nextsnapelem; 3418eda14cbcSMatt Macy uint64_t fromguid = 0; 3419eda14cbcSMatt Macy uint64_t originguid = 0; 3420eda14cbcSMatt Macy uint64_t stream_originguid = 0; 3421eda14cbcSMatt Macy uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; 3422eda14cbcSMatt Macy char *fsname, *stream_fsname; 3423eda14cbcSMatt Macy 3424eda14cbcSMatt Macy nextfselem = nvlist_next_nvpair(local_nv, fselem); 3425eda14cbcSMatt Macy 3426184c1b94SMartin Matuska nvfs = fnvpair_value_nvlist(fselem); 3427184c1b94SMartin Matuska snaps = fnvlist_lookup_nvlist(nvfs, "snaps"); 3428184c1b94SMartin Matuska fsname = fnvlist_lookup_string(nvfs, "name"); 3429184c1b94SMartin Matuska parent_fromsnap_guid = fnvlist_lookup_uint64(nvfs, 3430184c1b94SMartin Matuska "parentfromsnap"); 3431eda14cbcSMatt Macy (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); 3432eda14cbcSMatt Macy 3433eda14cbcSMatt Macy /* 3434eda14cbcSMatt Macy * First find the stream's fs, so we can check for 3435eda14cbcSMatt Macy * a different origin (due to "zfs promote") 3436eda14cbcSMatt Macy */ 3437eda14cbcSMatt Macy for (snapelem = nvlist_next_nvpair(snaps, NULL); 3438eda14cbcSMatt Macy snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { 3439eda14cbcSMatt Macy uint64_t thisguid; 3440eda14cbcSMatt Macy 3441184c1b94SMartin Matuska thisguid = fnvpair_value_uint64(snapelem); 3442eda14cbcSMatt Macy stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); 3443eda14cbcSMatt Macy 3444eda14cbcSMatt Macy if (stream_nvfs != NULL) 3445eda14cbcSMatt Macy break; 3446eda14cbcSMatt Macy } 3447eda14cbcSMatt Macy 3448eda14cbcSMatt Macy /* check for promote */ 3449eda14cbcSMatt Macy (void) nvlist_lookup_uint64(stream_nvfs, "origin", 3450eda14cbcSMatt Macy &stream_originguid); 3451eda14cbcSMatt Macy if (stream_nvfs && originguid != stream_originguid) { 3452eda14cbcSMatt Macy switch (created_before(hdl, local_avl, 3453eda14cbcSMatt Macy stream_originguid, originguid)) { 3454eda14cbcSMatt Macy case 1: { 3455eda14cbcSMatt Macy /* promote it! */ 3456eda14cbcSMatt Macy nvlist_t *origin_nvfs; 3457eda14cbcSMatt Macy char *origin_fsname; 3458eda14cbcSMatt Macy 3459eda14cbcSMatt Macy origin_nvfs = fsavl_find(local_avl, originguid, 3460eda14cbcSMatt Macy NULL); 3461184c1b94SMartin Matuska origin_fsname = fnvlist_lookup_string( 3462184c1b94SMartin Matuska origin_nvfs, "name"); 3463eda14cbcSMatt Macy error = recv_promote(hdl, fsname, origin_fsname, 3464eda14cbcSMatt Macy flags); 3465eda14cbcSMatt Macy if (error == 0) 3466eda14cbcSMatt Macy progress = B_TRUE; 3467eda14cbcSMatt Macy break; 3468eda14cbcSMatt Macy } 3469eda14cbcSMatt Macy default: 3470eda14cbcSMatt Macy break; 3471eda14cbcSMatt Macy case -1: 3472eda14cbcSMatt Macy fsavl_destroy(local_avl); 3473184c1b94SMartin Matuska fnvlist_free(local_nv); 3474eda14cbcSMatt Macy return (-1); 3475eda14cbcSMatt Macy } 3476eda14cbcSMatt Macy /* 3477eda14cbcSMatt Macy * We had/have the wrong origin, therefore our 3478eda14cbcSMatt Macy * list of snapshots is wrong. Need to handle 3479eda14cbcSMatt Macy * them on the next pass. 3480eda14cbcSMatt Macy */ 3481eda14cbcSMatt Macy needagain = B_TRUE; 3482eda14cbcSMatt Macy continue; 3483eda14cbcSMatt Macy } 3484eda14cbcSMatt Macy 3485eda14cbcSMatt Macy for (snapelem = nvlist_next_nvpair(snaps, NULL); 3486eda14cbcSMatt Macy snapelem; snapelem = nextsnapelem) { 3487eda14cbcSMatt Macy uint64_t thisguid; 3488eda14cbcSMatt Macy char *stream_snapname; 3489eda14cbcSMatt Macy nvlist_t *found, *props; 3490eda14cbcSMatt Macy 3491eda14cbcSMatt Macy nextsnapelem = nvlist_next_nvpair(snaps, snapelem); 3492eda14cbcSMatt Macy 3493184c1b94SMartin Matuska thisguid = fnvpair_value_uint64(snapelem); 3494eda14cbcSMatt Macy found = fsavl_find(stream_avl, thisguid, 3495eda14cbcSMatt Macy &stream_snapname); 3496eda14cbcSMatt Macy 3497eda14cbcSMatt Macy /* check for delete */ 3498eda14cbcSMatt Macy if (found == NULL) { 3499eda14cbcSMatt Macy char name[ZFS_MAX_DATASET_NAME_LEN]; 3500eda14cbcSMatt Macy 3501eda14cbcSMatt Macy if (!flags->force) 3502eda14cbcSMatt Macy continue; 3503eda14cbcSMatt Macy 3504eda14cbcSMatt Macy (void) snprintf(name, sizeof (name), "%s@%s", 3505eda14cbcSMatt Macy fsname, nvpair_name(snapelem)); 3506eda14cbcSMatt Macy 3507eda14cbcSMatt Macy error = recv_destroy(hdl, name, 3508eda14cbcSMatt Macy strlen(fsname)+1, newname, flags); 3509eda14cbcSMatt Macy if (error) 3510eda14cbcSMatt Macy needagain = B_TRUE; 3511eda14cbcSMatt Macy else 3512eda14cbcSMatt Macy progress = B_TRUE; 3513eda14cbcSMatt Macy sprintf(guidname, "%llu", 3514eda14cbcSMatt Macy (u_longlong_t)thisguid); 3515eda14cbcSMatt Macy nvlist_add_boolean(deleted, guidname); 3516eda14cbcSMatt Macy continue; 3517eda14cbcSMatt Macy } 3518eda14cbcSMatt Macy 3519eda14cbcSMatt Macy stream_nvfs = found; 3520eda14cbcSMatt Macy 3521eda14cbcSMatt Macy if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops", 3522eda14cbcSMatt Macy &props) && 0 == nvlist_lookup_nvlist(props, 3523eda14cbcSMatt Macy stream_snapname, &props)) { 3524eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 3525eda14cbcSMatt Macy 3526eda14cbcSMatt Macy zc.zc_cookie = B_TRUE; /* received */ 3527eda14cbcSMatt Macy (void) snprintf(zc.zc_name, sizeof (zc.zc_name), 3528eda14cbcSMatt Macy "%s@%s", fsname, nvpair_name(snapelem)); 3529716fd348SMartin Matuska zcmd_write_src_nvlist(hdl, &zc, props); 3530eda14cbcSMatt Macy (void) zfs_ioctl(hdl, 3531eda14cbcSMatt Macy ZFS_IOC_SET_PROP, &zc); 3532eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 3533eda14cbcSMatt Macy } 3534eda14cbcSMatt Macy 3535eda14cbcSMatt Macy /* check for different snapname */ 3536eda14cbcSMatt Macy if (strcmp(nvpair_name(snapelem), 3537eda14cbcSMatt Macy stream_snapname) != 0) { 3538eda14cbcSMatt Macy char name[ZFS_MAX_DATASET_NAME_LEN]; 3539eda14cbcSMatt Macy char tryname[ZFS_MAX_DATASET_NAME_LEN]; 3540eda14cbcSMatt Macy 3541eda14cbcSMatt Macy (void) snprintf(name, sizeof (name), "%s@%s", 3542eda14cbcSMatt Macy fsname, nvpair_name(snapelem)); 3543eda14cbcSMatt Macy (void) snprintf(tryname, sizeof (name), "%s@%s", 3544eda14cbcSMatt Macy fsname, stream_snapname); 3545eda14cbcSMatt Macy 3546eda14cbcSMatt Macy error = recv_rename(hdl, name, tryname, 3547eda14cbcSMatt Macy strlen(fsname)+1, newname, flags); 3548eda14cbcSMatt Macy if (error) 3549eda14cbcSMatt Macy needagain = B_TRUE; 3550eda14cbcSMatt Macy else 3551eda14cbcSMatt Macy progress = B_TRUE; 3552eda14cbcSMatt Macy } 3553eda14cbcSMatt Macy 3554eda14cbcSMatt Macy if (strcmp(stream_snapname, fromsnap) == 0) 3555eda14cbcSMatt Macy fromguid = thisguid; 3556eda14cbcSMatt Macy } 3557eda14cbcSMatt Macy 3558eda14cbcSMatt Macy /* check for delete */ 3559eda14cbcSMatt Macy if (stream_nvfs == NULL) { 3560eda14cbcSMatt Macy if (!flags->force) 3561eda14cbcSMatt Macy continue; 3562eda14cbcSMatt Macy 3563eda14cbcSMatt Macy error = recv_destroy(hdl, fsname, strlen(tofs)+1, 3564eda14cbcSMatt Macy newname, flags); 3565eda14cbcSMatt Macy if (error) 3566eda14cbcSMatt Macy needagain = B_TRUE; 3567eda14cbcSMatt Macy else 3568eda14cbcSMatt Macy progress = B_TRUE; 3569eda14cbcSMatt Macy sprintf(guidname, "%llu", 3570eda14cbcSMatt Macy (u_longlong_t)parent_fromsnap_guid); 3571eda14cbcSMatt Macy nvlist_add_boolean(deleted, guidname); 3572eda14cbcSMatt Macy continue; 3573eda14cbcSMatt Macy } 3574eda14cbcSMatt Macy 3575eda14cbcSMatt Macy if (fromguid == 0) { 3576eda14cbcSMatt Macy if (flags->verbose) { 3577eda14cbcSMatt Macy (void) printf("local fs %s does not have " 3578eda14cbcSMatt Macy "fromsnap (%s in stream); must have " 3579eda14cbcSMatt Macy "been deleted locally; ignoring\n", 3580eda14cbcSMatt Macy fsname, fromsnap); 3581eda14cbcSMatt Macy } 3582eda14cbcSMatt Macy continue; 3583eda14cbcSMatt Macy } 3584eda14cbcSMatt Macy 3585184c1b94SMartin Matuska stream_fsname = fnvlist_lookup_string(stream_nvfs, "name"); 3586184c1b94SMartin Matuska stream_parent_fromsnap_guid = fnvlist_lookup_uint64( 3587184c1b94SMartin Matuska stream_nvfs, "parentfromsnap"); 3588eda14cbcSMatt Macy 3589eda14cbcSMatt Macy s1 = strrchr(fsname, '/'); 3590eda14cbcSMatt Macy s2 = strrchr(stream_fsname, '/'); 3591eda14cbcSMatt Macy 3592eda14cbcSMatt Macy /* 3593eda14cbcSMatt Macy * Check if we're going to rename based on parent guid change 3594eda14cbcSMatt Macy * and the current parent guid was also deleted. If it was then 3595eda14cbcSMatt Macy * rename will fail and is likely unneeded, so avoid this and 3596eda14cbcSMatt Macy * force an early retry to determine the new 3597eda14cbcSMatt Macy * parent_fromsnap_guid. 3598eda14cbcSMatt Macy */ 3599eda14cbcSMatt Macy if (stream_parent_fromsnap_guid != 0 && 3600eda14cbcSMatt Macy parent_fromsnap_guid != 0 && 3601eda14cbcSMatt Macy stream_parent_fromsnap_guid != parent_fromsnap_guid) { 3602eda14cbcSMatt Macy sprintf(guidname, "%llu", 3603eda14cbcSMatt Macy (u_longlong_t)parent_fromsnap_guid); 3604eda14cbcSMatt Macy if (nvlist_exists(deleted, guidname)) { 3605eda14cbcSMatt Macy progress = B_TRUE; 3606eda14cbcSMatt Macy needagain = B_TRUE; 3607eda14cbcSMatt Macy goto doagain; 3608eda14cbcSMatt Macy } 3609eda14cbcSMatt Macy } 3610eda14cbcSMatt Macy 3611eda14cbcSMatt Macy /* 3612eda14cbcSMatt Macy * Check for rename. If the exact receive path is specified, it 3613eda14cbcSMatt Macy * does not count as a rename, but we still need to check the 3614eda14cbcSMatt Macy * datasets beneath it. 3615eda14cbcSMatt Macy */ 3616eda14cbcSMatt Macy if ((stream_parent_fromsnap_guid != 0 && 3617eda14cbcSMatt Macy parent_fromsnap_guid != 0 && 3618eda14cbcSMatt Macy stream_parent_fromsnap_guid != parent_fromsnap_guid) || 3619eda14cbcSMatt Macy ((flags->isprefix || strcmp(tofs, fsname) != 0) && 3620eda14cbcSMatt Macy (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) { 3621eda14cbcSMatt Macy nvlist_t *parent; 3622eda14cbcSMatt Macy char tryname[ZFS_MAX_DATASET_NAME_LEN]; 3623eda14cbcSMatt Macy 3624eda14cbcSMatt Macy parent = fsavl_find(local_avl, 3625eda14cbcSMatt Macy stream_parent_fromsnap_guid, NULL); 3626eda14cbcSMatt Macy /* 3627eda14cbcSMatt Macy * NB: parent might not be found if we used the 3628eda14cbcSMatt Macy * tosnap for stream_parent_fromsnap_guid, 3629eda14cbcSMatt Macy * because the parent is a newly-created fs; 3630eda14cbcSMatt Macy * we'll be able to rename it after we recv the 3631eda14cbcSMatt Macy * new fs. 3632eda14cbcSMatt Macy */ 3633eda14cbcSMatt Macy if (parent != NULL) { 3634eda14cbcSMatt Macy char *pname; 3635eda14cbcSMatt Macy 3636184c1b94SMartin Matuska pname = fnvlist_lookup_string(parent, "name"); 3637eda14cbcSMatt Macy (void) snprintf(tryname, sizeof (tryname), 3638eda14cbcSMatt Macy "%s%s", pname, strrchr(stream_fsname, '/')); 3639eda14cbcSMatt Macy } else { 3640eda14cbcSMatt Macy tryname[0] = '\0'; 3641eda14cbcSMatt Macy if (flags->verbose) { 3642eda14cbcSMatt Macy (void) printf("local fs %s new parent " 3643eda14cbcSMatt Macy "not found\n", fsname); 3644eda14cbcSMatt Macy } 3645eda14cbcSMatt Macy } 3646eda14cbcSMatt Macy 3647eda14cbcSMatt Macy newname[0] = '\0'; 3648eda14cbcSMatt Macy 3649eda14cbcSMatt Macy error = recv_rename(hdl, fsname, tryname, 3650eda14cbcSMatt Macy strlen(tofs)+1, newname, flags); 3651eda14cbcSMatt Macy 3652eda14cbcSMatt Macy if (renamed != NULL && newname[0] != '\0') { 3653184c1b94SMartin Matuska fnvlist_add_boolean(renamed, newname); 3654eda14cbcSMatt Macy } 3655eda14cbcSMatt Macy 3656eda14cbcSMatt Macy if (error) 3657eda14cbcSMatt Macy needagain = B_TRUE; 3658eda14cbcSMatt Macy else 3659eda14cbcSMatt Macy progress = B_TRUE; 3660eda14cbcSMatt Macy } 3661eda14cbcSMatt Macy } 3662eda14cbcSMatt Macy 3663eda14cbcSMatt Macy doagain: 3664eda14cbcSMatt Macy fsavl_destroy(local_avl); 3665184c1b94SMartin Matuska fnvlist_free(local_nv); 3666184c1b94SMartin Matuska fnvlist_free(deleted); 3667eda14cbcSMatt Macy 3668eda14cbcSMatt Macy if (needagain && progress) { 3669eda14cbcSMatt Macy /* do another pass to fix up temporary names */ 3670eda14cbcSMatt Macy if (flags->verbose) 3671eda14cbcSMatt Macy (void) printf("another pass:\n"); 3672eda14cbcSMatt Macy goto again; 3673eda14cbcSMatt Macy } 3674eda14cbcSMatt Macy 3675eda14cbcSMatt Macy return (needagain || error != 0); 3676eda14cbcSMatt Macy } 3677eda14cbcSMatt Macy 3678eda14cbcSMatt Macy static int 3679eda14cbcSMatt Macy zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, 3680eda14cbcSMatt Macy recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc, 3681eda14cbcSMatt Macy char **top_zfs, nvlist_t *cmdprops) 3682eda14cbcSMatt Macy { 3683eda14cbcSMatt Macy nvlist_t *stream_nv = NULL; 3684eda14cbcSMatt Macy avl_tree_t *stream_avl = NULL; 3685eda14cbcSMatt Macy char *fromsnap = NULL; 3686eda14cbcSMatt Macy char *sendsnap = NULL; 3687eda14cbcSMatt Macy char *cp; 3688eda14cbcSMatt Macy char tofs[ZFS_MAX_DATASET_NAME_LEN]; 3689eda14cbcSMatt Macy char sendfs[ZFS_MAX_DATASET_NAME_LEN]; 36901f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 3691eda14cbcSMatt Macy dmu_replay_record_t drre; 3692eda14cbcSMatt Macy int error; 3693eda14cbcSMatt Macy boolean_t anyerr = B_FALSE; 3694eda14cbcSMatt Macy boolean_t softerr = B_FALSE; 3695eda14cbcSMatt Macy boolean_t recursive, raw; 3696eda14cbcSMatt Macy 3697eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3698eda14cbcSMatt Macy "cannot receive")); 3699eda14cbcSMatt Macy 3700eda14cbcSMatt Macy assert(drr->drr_type == DRR_BEGIN); 3701eda14cbcSMatt Macy assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); 3702eda14cbcSMatt Macy assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) == 3703eda14cbcSMatt Macy DMU_COMPOUNDSTREAM); 3704eda14cbcSMatt Macy 3705eda14cbcSMatt Macy /* 3706eda14cbcSMatt Macy * Read in the nvlist from the stream. 3707eda14cbcSMatt Macy */ 3708eda14cbcSMatt Macy if (drr->drr_payloadlen != 0) { 3709eda14cbcSMatt Macy error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, 3710eda14cbcSMatt Macy &stream_nv, flags->byteswap, zc); 3711eda14cbcSMatt Macy if (error) { 3712eda14cbcSMatt Macy error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3713eda14cbcSMatt Macy goto out; 3714eda14cbcSMatt Macy } 3715eda14cbcSMatt Macy } 3716eda14cbcSMatt Macy 3717eda14cbcSMatt Macy recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 3718eda14cbcSMatt Macy ENOENT); 3719eda14cbcSMatt Macy raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0); 3720eda14cbcSMatt Macy 3721eda14cbcSMatt Macy if (recursive && strchr(destname, '@')) { 3722eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3723eda14cbcSMatt Macy "cannot specify snapshot name for multi-snapshot stream")); 3724eda14cbcSMatt Macy error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3725eda14cbcSMatt Macy goto out; 3726eda14cbcSMatt Macy } 3727eda14cbcSMatt Macy 3728eda14cbcSMatt Macy /* 3729eda14cbcSMatt Macy * Read in the end record and verify checksum. 3730eda14cbcSMatt Macy */ 3731eda14cbcSMatt Macy if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), 3732eda14cbcSMatt Macy flags->byteswap, NULL))) 3733eda14cbcSMatt Macy goto out; 3734eda14cbcSMatt Macy if (flags->byteswap) { 3735eda14cbcSMatt Macy drre.drr_type = BSWAP_32(drre.drr_type); 3736eda14cbcSMatt Macy drre.drr_u.drr_end.drr_checksum.zc_word[0] = 3737eda14cbcSMatt Macy BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); 3738eda14cbcSMatt Macy drre.drr_u.drr_end.drr_checksum.zc_word[1] = 3739eda14cbcSMatt Macy BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); 3740eda14cbcSMatt Macy drre.drr_u.drr_end.drr_checksum.zc_word[2] = 3741eda14cbcSMatt Macy BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); 3742eda14cbcSMatt Macy drre.drr_u.drr_end.drr_checksum.zc_word[3] = 3743eda14cbcSMatt Macy BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); 3744eda14cbcSMatt Macy } 3745eda14cbcSMatt Macy if (drre.drr_type != DRR_END) { 3746eda14cbcSMatt Macy error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3747eda14cbcSMatt Macy goto out; 3748eda14cbcSMatt Macy } 3749eda14cbcSMatt Macy if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { 3750eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3751eda14cbcSMatt Macy "incorrect header checksum")); 3752eda14cbcSMatt Macy error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3753eda14cbcSMatt Macy goto out; 3754eda14cbcSMatt Macy } 3755eda14cbcSMatt Macy 3756eda14cbcSMatt Macy (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); 3757eda14cbcSMatt Macy 3758eda14cbcSMatt Macy if (drr->drr_payloadlen != 0) { 3759eda14cbcSMatt Macy nvlist_t *stream_fss; 3760eda14cbcSMatt Macy 3761184c1b94SMartin Matuska stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss"); 3762eda14cbcSMatt Macy if ((stream_avl = fsavl_create(stream_fss)) == NULL) { 3763eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3764eda14cbcSMatt Macy "couldn't allocate avl tree")); 3765eda14cbcSMatt Macy error = zfs_error(hdl, EZFS_NOMEM, errbuf); 3766eda14cbcSMatt Macy goto out; 3767eda14cbcSMatt Macy } 3768eda14cbcSMatt Macy 3769eda14cbcSMatt Macy if (fromsnap != NULL && recursive) { 3770eda14cbcSMatt Macy nvlist_t *renamed = NULL; 3771eda14cbcSMatt Macy nvpair_t *pair = NULL; 3772eda14cbcSMatt Macy 3773eda14cbcSMatt Macy (void) strlcpy(tofs, destname, sizeof (tofs)); 3774eda14cbcSMatt Macy if (flags->isprefix) { 3775eda14cbcSMatt Macy struct drr_begin *drrb = &drr->drr_u.drr_begin; 3776eda14cbcSMatt Macy int i; 3777eda14cbcSMatt Macy 3778eda14cbcSMatt Macy if (flags->istail) { 3779eda14cbcSMatt Macy cp = strrchr(drrb->drr_toname, '/'); 3780eda14cbcSMatt Macy if (cp == NULL) { 3781eda14cbcSMatt Macy (void) strlcat(tofs, "/", 3782eda14cbcSMatt Macy sizeof (tofs)); 3783eda14cbcSMatt Macy i = 0; 3784eda14cbcSMatt Macy } else { 3785eda14cbcSMatt Macy i = (cp - drrb->drr_toname); 3786eda14cbcSMatt Macy } 3787eda14cbcSMatt Macy } else { 3788eda14cbcSMatt Macy i = strcspn(drrb->drr_toname, "/@"); 3789eda14cbcSMatt Macy } 3790eda14cbcSMatt Macy /* zfs_receive_one() will create_parents() */ 3791eda14cbcSMatt Macy (void) strlcat(tofs, &drrb->drr_toname[i], 3792eda14cbcSMatt Macy sizeof (tofs)); 3793eda14cbcSMatt Macy *strchr(tofs, '@') = '\0'; 3794eda14cbcSMatt Macy } 3795eda14cbcSMatt Macy 3796eda14cbcSMatt Macy if (!flags->dryrun && !flags->nomount) { 3797184c1b94SMartin Matuska renamed = fnvlist_alloc(); 3798eda14cbcSMatt Macy } 3799eda14cbcSMatt Macy 3800eda14cbcSMatt Macy softerr = recv_incremental_replication(hdl, tofs, flags, 3801eda14cbcSMatt Macy stream_nv, stream_avl, renamed); 3802eda14cbcSMatt Macy 3803eda14cbcSMatt Macy /* Unmount renamed filesystems before receiving. */ 3804eda14cbcSMatt Macy while ((pair = nvlist_next_nvpair(renamed, 3805eda14cbcSMatt Macy pair)) != NULL) { 3806eda14cbcSMatt Macy zfs_handle_t *zhp; 3807eda14cbcSMatt Macy prop_changelist_t *clp = NULL; 3808eda14cbcSMatt Macy 3809eda14cbcSMatt Macy zhp = zfs_open(hdl, nvpair_name(pair), 3810eda14cbcSMatt Macy ZFS_TYPE_FILESYSTEM); 3811eda14cbcSMatt Macy if (zhp != NULL) { 3812eda14cbcSMatt Macy clp = changelist_gather(zhp, 3813eda14cbcSMatt Macy ZFS_PROP_MOUNTPOINT, 0, 3814eda14cbcSMatt Macy flags->forceunmount ? MS_FORCE : 0); 3815eda14cbcSMatt Macy zfs_close(zhp); 3816eda14cbcSMatt Macy if (clp != NULL) { 3817eda14cbcSMatt Macy softerr |= 3818eda14cbcSMatt Macy changelist_prefix(clp); 3819eda14cbcSMatt Macy changelist_free(clp); 3820eda14cbcSMatt Macy } 3821eda14cbcSMatt Macy } 3822eda14cbcSMatt Macy } 3823eda14cbcSMatt Macy 3824184c1b94SMartin Matuska fnvlist_free(renamed); 3825eda14cbcSMatt Macy } 3826eda14cbcSMatt Macy } 3827eda14cbcSMatt Macy 3828eda14cbcSMatt Macy /* 3829eda14cbcSMatt Macy * Get the fs specified by the first path in the stream (the top level 3830eda14cbcSMatt Macy * specified by 'zfs send') and pass it to each invocation of 3831eda14cbcSMatt Macy * zfs_receive_one(). 3832eda14cbcSMatt Macy */ 3833eda14cbcSMatt Macy (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname, 3834eda14cbcSMatt Macy sizeof (sendfs)); 3835eda14cbcSMatt Macy if ((cp = strchr(sendfs, '@')) != NULL) { 3836eda14cbcSMatt Macy *cp = '\0'; 3837eda14cbcSMatt Macy /* 3838eda14cbcSMatt Macy * Find the "sendsnap", the final snapshot in a replication 3839eda14cbcSMatt Macy * stream. zfs_receive_one() handles certain errors 3840eda14cbcSMatt Macy * differently, depending on if the contained stream is the 3841eda14cbcSMatt Macy * last one or not. 3842eda14cbcSMatt Macy */ 3843eda14cbcSMatt Macy sendsnap = (cp + 1); 3844eda14cbcSMatt Macy } 3845eda14cbcSMatt Macy 3846eda14cbcSMatt Macy /* Finally, receive each contained stream */ 3847eda14cbcSMatt Macy do { 3848eda14cbcSMatt Macy /* 3849eda14cbcSMatt Macy * we should figure out if it has a recoverable 3850eda14cbcSMatt Macy * error, in which case do a recv_skip() and drive on. 3851eda14cbcSMatt Macy * Note, if we fail due to already having this guid, 3852eda14cbcSMatt Macy * zfs_receive_one() will take care of it (ie, 3853eda14cbcSMatt Macy * recv_skip() and return 0). 3854eda14cbcSMatt Macy */ 3855eda14cbcSMatt Macy error = zfs_receive_impl(hdl, destname, NULL, flags, fd, 3856eda14cbcSMatt Macy sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops); 3857eda14cbcSMatt Macy if (error == ENODATA) { 3858eda14cbcSMatt Macy error = 0; 3859eda14cbcSMatt Macy break; 3860eda14cbcSMatt Macy } 3861eda14cbcSMatt Macy anyerr |= error; 3862eda14cbcSMatt Macy } while (error == 0); 3863eda14cbcSMatt Macy 3864eda14cbcSMatt Macy if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) { 3865eda14cbcSMatt Macy /* 3866eda14cbcSMatt Macy * Now that we have the fs's they sent us, try the 3867eda14cbcSMatt Macy * renames again. 3868eda14cbcSMatt Macy */ 3869eda14cbcSMatt Macy softerr = recv_incremental_replication(hdl, tofs, flags, 3870eda14cbcSMatt Macy stream_nv, stream_avl, NULL); 3871eda14cbcSMatt Macy } 3872eda14cbcSMatt Macy 3873eda14cbcSMatt Macy if (raw && softerr == 0 && *top_zfs != NULL) { 3874eda14cbcSMatt Macy softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs, 3875c03c5b1cSMartin Matuska stream_nv); 3876eda14cbcSMatt Macy } 3877eda14cbcSMatt Macy 3878eda14cbcSMatt Macy out: 3879eda14cbcSMatt Macy fsavl_destroy(stream_avl); 3880184c1b94SMartin Matuska fnvlist_free(stream_nv); 3881eda14cbcSMatt Macy if (softerr) 3882eda14cbcSMatt Macy error = -2; 3883eda14cbcSMatt Macy if (anyerr) 3884eda14cbcSMatt Macy error = -1; 3885eda14cbcSMatt Macy return (error); 3886eda14cbcSMatt Macy } 3887eda14cbcSMatt Macy 3888eda14cbcSMatt Macy static void 3889eda14cbcSMatt Macy trunc_prop_errs(int truncated) 3890eda14cbcSMatt Macy { 3891eda14cbcSMatt Macy ASSERT(truncated != 0); 3892eda14cbcSMatt Macy 3893eda14cbcSMatt Macy if (truncated == 1) 3894eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 3895eda14cbcSMatt Macy "1 more property could not be set\n")); 3896eda14cbcSMatt Macy else 3897eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 3898eda14cbcSMatt Macy "%d more properties could not be set\n"), truncated); 3899eda14cbcSMatt Macy } 3900eda14cbcSMatt Macy 3901eda14cbcSMatt Macy static int 3902eda14cbcSMatt Macy recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) 3903eda14cbcSMatt Macy { 3904eda14cbcSMatt Macy dmu_replay_record_t *drr; 3905eda14cbcSMatt Macy void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE); 3906eda14cbcSMatt Macy uint64_t payload_size; 39071f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 3908eda14cbcSMatt Macy 3909eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3910eda14cbcSMatt Macy "cannot receive")); 3911eda14cbcSMatt Macy 3912eda14cbcSMatt Macy /* XXX would be great to use lseek if possible... */ 3913eda14cbcSMatt Macy drr = buf; 3914eda14cbcSMatt Macy 3915eda14cbcSMatt Macy while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), 3916eda14cbcSMatt Macy byteswap, NULL) == 0) { 3917eda14cbcSMatt Macy if (byteswap) 3918eda14cbcSMatt Macy drr->drr_type = BSWAP_32(drr->drr_type); 3919eda14cbcSMatt Macy 3920eda14cbcSMatt Macy switch (drr->drr_type) { 3921eda14cbcSMatt Macy case DRR_BEGIN: 3922eda14cbcSMatt Macy if (drr->drr_payloadlen != 0) { 3923eda14cbcSMatt Macy (void) recv_read(hdl, fd, buf, 3924eda14cbcSMatt Macy drr->drr_payloadlen, B_FALSE, NULL); 3925eda14cbcSMatt Macy } 3926eda14cbcSMatt Macy break; 3927eda14cbcSMatt Macy 3928eda14cbcSMatt Macy case DRR_END: 3929eda14cbcSMatt Macy free(buf); 3930eda14cbcSMatt Macy return (0); 3931eda14cbcSMatt Macy 3932eda14cbcSMatt Macy case DRR_OBJECT: 3933eda14cbcSMatt Macy if (byteswap) { 3934eda14cbcSMatt Macy drr->drr_u.drr_object.drr_bonuslen = 3935eda14cbcSMatt Macy BSWAP_32(drr->drr_u.drr_object. 3936eda14cbcSMatt Macy drr_bonuslen); 3937eda14cbcSMatt Macy drr->drr_u.drr_object.drr_raw_bonuslen = 3938eda14cbcSMatt Macy BSWAP_32(drr->drr_u.drr_object. 3939eda14cbcSMatt Macy drr_raw_bonuslen); 3940eda14cbcSMatt Macy } 3941eda14cbcSMatt Macy 3942eda14cbcSMatt Macy payload_size = 3943eda14cbcSMatt Macy DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object); 3944eda14cbcSMatt Macy (void) recv_read(hdl, fd, buf, payload_size, 3945eda14cbcSMatt Macy B_FALSE, NULL); 3946eda14cbcSMatt Macy break; 3947eda14cbcSMatt Macy 3948eda14cbcSMatt Macy case DRR_WRITE: 3949eda14cbcSMatt Macy if (byteswap) { 3950eda14cbcSMatt Macy drr->drr_u.drr_write.drr_logical_size = 3951eda14cbcSMatt Macy BSWAP_64( 3952eda14cbcSMatt Macy drr->drr_u.drr_write.drr_logical_size); 3953eda14cbcSMatt Macy drr->drr_u.drr_write.drr_compressed_size = 3954eda14cbcSMatt Macy BSWAP_64( 3955eda14cbcSMatt Macy drr->drr_u.drr_write.drr_compressed_size); 3956eda14cbcSMatt Macy } 3957eda14cbcSMatt Macy payload_size = 3958eda14cbcSMatt Macy DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write); 3959ac0bf12eSMatt Macy assert(payload_size <= SPA_MAXBLOCKSIZE); 3960eda14cbcSMatt Macy (void) recv_read(hdl, fd, buf, 3961eda14cbcSMatt Macy payload_size, B_FALSE, NULL); 3962eda14cbcSMatt Macy break; 3963eda14cbcSMatt Macy case DRR_SPILL: 3964eda14cbcSMatt Macy if (byteswap) { 3965eda14cbcSMatt Macy drr->drr_u.drr_spill.drr_length = 3966eda14cbcSMatt Macy BSWAP_64(drr->drr_u.drr_spill.drr_length); 3967eda14cbcSMatt Macy drr->drr_u.drr_spill.drr_compressed_size = 3968eda14cbcSMatt Macy BSWAP_64(drr->drr_u.drr_spill. 3969eda14cbcSMatt Macy drr_compressed_size); 3970eda14cbcSMatt Macy } 3971eda14cbcSMatt Macy 3972eda14cbcSMatt Macy payload_size = 3973eda14cbcSMatt Macy DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill); 3974eda14cbcSMatt Macy (void) recv_read(hdl, fd, buf, payload_size, 3975eda14cbcSMatt Macy B_FALSE, NULL); 3976eda14cbcSMatt Macy break; 3977eda14cbcSMatt Macy case DRR_WRITE_EMBEDDED: 3978eda14cbcSMatt Macy if (byteswap) { 3979eda14cbcSMatt Macy drr->drr_u.drr_write_embedded.drr_psize = 3980eda14cbcSMatt Macy BSWAP_32(drr->drr_u.drr_write_embedded. 3981eda14cbcSMatt Macy drr_psize); 3982eda14cbcSMatt Macy } 3983eda14cbcSMatt Macy (void) recv_read(hdl, fd, buf, 3984eda14cbcSMatt Macy P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize, 3985eda14cbcSMatt Macy 8), B_FALSE, NULL); 3986eda14cbcSMatt Macy break; 3987eda14cbcSMatt Macy case DRR_OBJECT_RANGE: 3988eda14cbcSMatt Macy case DRR_WRITE_BYREF: 3989eda14cbcSMatt Macy case DRR_FREEOBJECTS: 3990eda14cbcSMatt Macy case DRR_FREE: 3991eda14cbcSMatt Macy break; 3992eda14cbcSMatt Macy 3993eda14cbcSMatt Macy default: 3994eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3995eda14cbcSMatt Macy "invalid record type")); 3996eda14cbcSMatt Macy free(buf); 3997eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3998eda14cbcSMatt Macy } 3999eda14cbcSMatt Macy } 4000eda14cbcSMatt Macy 4001eda14cbcSMatt Macy free(buf); 4002eda14cbcSMatt Macy return (-1); 4003eda14cbcSMatt Macy } 4004eda14cbcSMatt Macy 4005eda14cbcSMatt Macy static void 4006eda14cbcSMatt Macy recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap, 4007eda14cbcSMatt Macy boolean_t resumable, boolean_t checksum) 4008eda14cbcSMatt Macy { 4009eda14cbcSMatt Macy char target_fs[ZFS_MAX_DATASET_NAME_LEN]; 4010eda14cbcSMatt Macy 4011eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ? 4012eda14cbcSMatt Macy "checksum mismatch" : "incomplete stream"))); 4013eda14cbcSMatt Macy 4014eda14cbcSMatt Macy if (!resumable) 4015eda14cbcSMatt Macy return; 4016eda14cbcSMatt Macy (void) strlcpy(target_fs, target_snap, sizeof (target_fs)); 4017eda14cbcSMatt Macy *strchr(target_fs, '@') = '\0'; 4018eda14cbcSMatt Macy zfs_handle_t *zhp = zfs_open(hdl, target_fs, 4019eda14cbcSMatt Macy ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4020eda14cbcSMatt Macy if (zhp == NULL) 4021eda14cbcSMatt Macy return; 4022eda14cbcSMatt Macy 4023eda14cbcSMatt Macy char token_buf[ZFS_MAXPROPLEN]; 4024eda14cbcSMatt Macy int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 4025eda14cbcSMatt Macy token_buf, sizeof (token_buf), 4026eda14cbcSMatt Macy NULL, NULL, 0, B_TRUE); 4027eda14cbcSMatt Macy if (error == 0) { 4028eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4029eda14cbcSMatt Macy "checksum mismatch or incomplete stream.\n" 4030eda14cbcSMatt Macy "Partially received snapshot is saved.\n" 4031eda14cbcSMatt Macy "A resuming stream can be generated on the sending " 4032eda14cbcSMatt Macy "system by running:\n" 4033eda14cbcSMatt Macy " zfs send -t %s"), 4034eda14cbcSMatt Macy token_buf); 4035eda14cbcSMatt Macy } 4036eda14cbcSMatt Macy zfs_close(zhp); 4037eda14cbcSMatt Macy } 4038eda14cbcSMatt Macy 4039eda14cbcSMatt Macy /* 4040eda14cbcSMatt Macy * Prepare a new nvlist of properties that are to override (-o) or be excluded 4041eda14cbcSMatt Macy * (-x) from the received dataset 4042eda14cbcSMatt Macy * recvprops: received properties from the send stream 4043eda14cbcSMatt Macy * cmdprops: raw input properties from command line 4044eda14cbcSMatt Macy * origprops: properties, both locally-set and received, currently set on the 4045eda14cbcSMatt Macy * target dataset if it exists, NULL otherwise. 4046eda14cbcSMatt Macy * oxprops: valid output override (-o) and excluded (-x) properties 4047eda14cbcSMatt Macy */ 4048eda14cbcSMatt Macy static int 4049eda14cbcSMatt Macy zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type, 4050eda14cbcSMatt Macy char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs, 4051eda14cbcSMatt Macy boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops, 4052eda14cbcSMatt Macy nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out, 4053eda14cbcSMatt Macy uint_t *wkeylen_out, const char *errbuf) 4054eda14cbcSMatt Macy { 4055eda14cbcSMatt Macy nvpair_t *nvp; 4056eda14cbcSMatt Macy nvlist_t *oprops, *voprops; 4057eda14cbcSMatt Macy zfs_handle_t *zhp = NULL; 4058eda14cbcSMatt Macy zpool_handle_t *zpool_hdl = NULL; 4059eda14cbcSMatt Macy char *cp; 4060eda14cbcSMatt Macy int ret = 0; 4061eda14cbcSMatt Macy char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 4062eda14cbcSMatt Macy 4063eda14cbcSMatt Macy if (nvlist_empty(cmdprops)) 4064eda14cbcSMatt Macy return (0); /* No properties to override or exclude */ 4065eda14cbcSMatt Macy 4066eda14cbcSMatt Macy *oxprops = fnvlist_alloc(); 4067eda14cbcSMatt Macy oprops = fnvlist_alloc(); 4068eda14cbcSMatt Macy 4069eda14cbcSMatt Macy strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN); 4070eda14cbcSMatt Macy 4071eda14cbcSMatt Macy /* 4072eda14cbcSMatt Macy * Get our dataset handle. The target dataset may not exist yet. 4073eda14cbcSMatt Macy */ 4074eda14cbcSMatt Macy if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) { 4075eda14cbcSMatt Macy zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET); 4076eda14cbcSMatt Macy if (zhp == NULL) { 4077eda14cbcSMatt Macy ret = -1; 4078eda14cbcSMatt Macy goto error; 4079eda14cbcSMatt Macy } 4080eda14cbcSMatt Macy } 4081eda14cbcSMatt Macy 4082eda14cbcSMatt Macy /* open the zpool handle */ 4083eda14cbcSMatt Macy cp = strchr(namebuf, '/'); 4084eda14cbcSMatt Macy if (cp != NULL) 4085eda14cbcSMatt Macy *cp = '\0'; 4086eda14cbcSMatt Macy zpool_hdl = zpool_open(hdl, namebuf); 4087eda14cbcSMatt Macy if (zpool_hdl == NULL) { 4088eda14cbcSMatt Macy ret = -1; 4089eda14cbcSMatt Macy goto error; 4090eda14cbcSMatt Macy } 4091eda14cbcSMatt Macy 4092eda14cbcSMatt Macy /* restore namebuf to match fsname for later use */ 4093eda14cbcSMatt Macy if (cp != NULL) 4094eda14cbcSMatt Macy *cp = '/'; 4095eda14cbcSMatt Macy 4096eda14cbcSMatt Macy /* 4097eda14cbcSMatt Macy * first iteration: process excluded (-x) properties now and gather 4098eda14cbcSMatt Macy * added (-o) properties to be later processed by zfs_valid_proplist() 4099eda14cbcSMatt Macy */ 4100eda14cbcSMatt Macy nvp = NULL; 4101eda14cbcSMatt Macy while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) { 4102eda14cbcSMatt Macy const char *name = nvpair_name(nvp); 4103eda14cbcSMatt Macy zfs_prop_t prop = zfs_name_to_prop(name); 4104eda14cbcSMatt Macy 410581b22a98SMartin Matuska /* 410681b22a98SMartin Matuska * It turns out, if we don't normalize "aliased" names 410781b22a98SMartin Matuska * e.g. compress= against the "real" names (e.g. compression) 410881b22a98SMartin Matuska * here, then setting/excluding them does not work as 410981b22a98SMartin Matuska * intended. 411081b22a98SMartin Matuska * 411181b22a98SMartin Matuska * But since user-defined properties wouldn't have a valid 411281b22a98SMartin Matuska * mapping here, we do this conditional dance. 411381b22a98SMartin Matuska */ 411481b22a98SMartin Matuska const char *newname = name; 411581b22a98SMartin Matuska if (prop >= ZFS_PROP_TYPE) 411681b22a98SMartin Matuska newname = zfs_prop_to_name(prop); 411781b22a98SMartin Matuska 4118eda14cbcSMatt Macy /* "origin" is processed separately, don't handle it here */ 4119eda14cbcSMatt Macy if (prop == ZFS_PROP_ORIGIN) 4120eda14cbcSMatt Macy continue; 4121eda14cbcSMatt Macy 4122eda14cbcSMatt Macy /* raw streams can't override encryption properties */ 4123eda14cbcSMatt Macy if ((zfs_prop_encryption_key_param(prop) || 4124eda14cbcSMatt Macy prop == ZFS_PROP_ENCRYPTION) && raw) { 4125eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4126eda14cbcSMatt Macy "encryption property '%s' cannot " 4127eda14cbcSMatt Macy "be set or excluded for raw streams."), name); 4128eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_BADPROP, errbuf); 4129eda14cbcSMatt Macy goto error; 4130eda14cbcSMatt Macy } 4131eda14cbcSMatt Macy 4132eda14cbcSMatt Macy /* incremental streams can only exclude encryption properties */ 4133eda14cbcSMatt Macy if ((zfs_prop_encryption_key_param(prop) || 4134eda14cbcSMatt Macy prop == ZFS_PROP_ENCRYPTION) && !newfs && 4135eda14cbcSMatt Macy nvpair_type(nvp) != DATA_TYPE_BOOLEAN) { 4136eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4137eda14cbcSMatt Macy "encryption property '%s' cannot " 4138eda14cbcSMatt Macy "be set for incremental streams."), name); 4139eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_BADPROP, errbuf); 4140eda14cbcSMatt Macy goto error; 4141eda14cbcSMatt Macy } 4142eda14cbcSMatt Macy 4143eda14cbcSMatt Macy switch (nvpair_type(nvp)) { 4144eda14cbcSMatt Macy case DATA_TYPE_BOOLEAN: /* -x property */ 4145eda14cbcSMatt Macy /* 4146eda14cbcSMatt Macy * DATA_TYPE_BOOLEAN is the way we're asked to "exclude" 4147eda14cbcSMatt Macy * a property: this is done by forcing an explicit 4148eda14cbcSMatt Macy * inherit on the destination so the effective value is 4149eda14cbcSMatt Macy * not the one we received from the send stream. 415016038816SMartin Matuska */ 415116038816SMartin Matuska if (!zfs_prop_valid_for_type(prop, type, B_FALSE) && 415216038816SMartin Matuska !zfs_prop_user(name)) { 415316038816SMartin Matuska (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 415416038816SMartin Matuska "Warning: %s: property '%s' does not " 415516038816SMartin Matuska "apply to datasets of this type\n"), 415616038816SMartin Matuska fsname, name); 415716038816SMartin Matuska continue; 415816038816SMartin Matuska } 415916038816SMartin Matuska /* 4160eda14cbcSMatt Macy * We do this only if the property is not already 4161eda14cbcSMatt Macy * locally-set, in which case its value will take 4162eda14cbcSMatt Macy * priority over the received anyway. 4163eda14cbcSMatt Macy */ 416481b22a98SMartin Matuska if (nvlist_exists(origprops, newname)) { 4165eda14cbcSMatt Macy nvlist_t *attrs; 4166eda14cbcSMatt Macy char *source = NULL; 4167eda14cbcSMatt Macy 416881b22a98SMartin Matuska attrs = fnvlist_lookup_nvlist(origprops, 416981b22a98SMartin Matuska newname); 4170eda14cbcSMatt Macy if (nvlist_lookup_string(attrs, 4171eda14cbcSMatt Macy ZPROP_SOURCE, &source) == 0 && 4172eda14cbcSMatt Macy strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0) 4173eda14cbcSMatt Macy continue; 4174eda14cbcSMatt Macy } 4175eda14cbcSMatt Macy /* 4176eda14cbcSMatt Macy * We can't force an explicit inherit on non-inheritable 4177eda14cbcSMatt Macy * properties: if we're asked to exclude this kind of 4178eda14cbcSMatt Macy * values we remove them from "recvprops" input nvlist. 4179eda14cbcSMatt Macy */ 4180c03c5b1cSMartin Matuska if (!zfs_prop_user(name) && /* can be inherited too */ 4181c03c5b1cSMartin Matuska !zfs_prop_inheritable(prop) && 418281b22a98SMartin Matuska nvlist_exists(recvprops, newname)) 418381b22a98SMartin Matuska fnvlist_remove(recvprops, newname); 4184eda14cbcSMatt Macy else 418581b22a98SMartin Matuska fnvlist_add_boolean(*oxprops, newname); 4186eda14cbcSMatt Macy break; 4187eda14cbcSMatt Macy case DATA_TYPE_STRING: /* -o property=value */ 418816038816SMartin Matuska /* 418916038816SMartin Matuska * we're trying to override a property that does not 419016038816SMartin Matuska * make sense for this type of dataset, but we don't 419116038816SMartin Matuska * want to fail if the receive is recursive: this comes 419216038816SMartin Matuska * in handy when the send stream contains, for 419316038816SMartin Matuska * instance, a child ZVOL and we're trying to receive 419416038816SMartin Matuska * it with "-o atime=on" 419516038816SMartin Matuska */ 419616038816SMartin Matuska if (!zfs_prop_valid_for_type(prop, type, B_FALSE) && 419716038816SMartin Matuska !zfs_prop_user(name)) { 419816038816SMartin Matuska if (recursive) 419916038816SMartin Matuska continue; 420016038816SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 420116038816SMartin Matuska "property '%s' does not apply to datasets " 420216038816SMartin Matuska "of this type"), name); 420316038816SMartin Matuska ret = zfs_error(hdl, EZFS_BADPROP, errbuf); 420416038816SMartin Matuska goto error; 420516038816SMartin Matuska } 420681b22a98SMartin Matuska fnvlist_add_string(oprops, newname, 420781b22a98SMartin Matuska fnvpair_value_string(nvp)); 4208eda14cbcSMatt Macy break; 4209eda14cbcSMatt Macy default: 4210eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4211eda14cbcSMatt Macy "property '%s' must be a string or boolean"), name); 4212eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_BADPROP, errbuf); 4213eda14cbcSMatt Macy goto error; 4214eda14cbcSMatt Macy } 4215eda14cbcSMatt Macy } 4216eda14cbcSMatt Macy 4217eda14cbcSMatt Macy if (toplevel) { 4218eda14cbcSMatt Macy /* convert override strings properties to native */ 4219eda14cbcSMatt Macy if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET, 4220eda14cbcSMatt Macy oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) { 4221eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_BADPROP, errbuf); 4222eda14cbcSMatt Macy goto error; 4223eda14cbcSMatt Macy } 4224eda14cbcSMatt Macy 4225eda14cbcSMatt Macy /* 4226eda14cbcSMatt Macy * zfs_crypto_create() requires the parent name. Get it 4227eda14cbcSMatt Macy * by truncating the fsname copy stored in namebuf. 4228eda14cbcSMatt Macy */ 4229eda14cbcSMatt Macy cp = strrchr(namebuf, '/'); 4230eda14cbcSMatt Macy if (cp != NULL) 4231eda14cbcSMatt Macy *cp = '\0'; 4232eda14cbcSMatt Macy 4233eda14cbcSMatt Macy if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL, 4234eda14cbcSMatt Macy B_FALSE, wkeydata_out, wkeylen_out) != 0) { 4235eda14cbcSMatt Macy fnvlist_free(voprops); 4236eda14cbcSMatt Macy ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); 4237eda14cbcSMatt Macy goto error; 4238eda14cbcSMatt Macy } 4239eda14cbcSMatt Macy 4240eda14cbcSMatt Macy /* second pass: process "-o" properties */ 4241eda14cbcSMatt Macy fnvlist_merge(*oxprops, voprops); 4242eda14cbcSMatt Macy fnvlist_free(voprops); 4243eda14cbcSMatt Macy } else { 4244eda14cbcSMatt Macy /* override props on child dataset are inherited */ 4245eda14cbcSMatt Macy nvp = NULL; 4246eda14cbcSMatt Macy while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) { 4247eda14cbcSMatt Macy const char *name = nvpair_name(nvp); 4248eda14cbcSMatt Macy fnvlist_add_boolean(*oxprops, name); 4249eda14cbcSMatt Macy } 4250eda14cbcSMatt Macy } 4251eda14cbcSMatt Macy 4252eda14cbcSMatt Macy error: 4253eda14cbcSMatt Macy if (zhp != NULL) 4254eda14cbcSMatt Macy zfs_close(zhp); 4255eda14cbcSMatt Macy if (zpool_hdl != NULL) 4256eda14cbcSMatt Macy zpool_close(zpool_hdl); 4257eda14cbcSMatt Macy fnvlist_free(oprops); 4258eda14cbcSMatt Macy return (ret); 4259eda14cbcSMatt Macy } 4260eda14cbcSMatt Macy 4261eda14cbcSMatt Macy /* 4262eda14cbcSMatt Macy * Restores a backup of tosnap from the file descriptor specified by infd. 4263eda14cbcSMatt Macy */ 4264eda14cbcSMatt Macy static int 4265eda14cbcSMatt Macy zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, 4266eda14cbcSMatt Macy const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr, 4267eda14cbcSMatt Macy dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv, 4268eda14cbcSMatt Macy avl_tree_t *stream_avl, char **top_zfs, 4269eda14cbcSMatt Macy const char *finalsnap, nvlist_t *cmdprops) 4270eda14cbcSMatt Macy { 4271c03c5b1cSMartin Matuska struct timespec begin_time; 4272eda14cbcSMatt Macy int ioctl_err, ioctl_errno, err; 4273eda14cbcSMatt Macy char *cp; 4274eda14cbcSMatt Macy struct drr_begin *drrb = &drr->drr_u.drr_begin; 42751f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 4276eda14cbcSMatt Macy const char *chopprefix; 4277eda14cbcSMatt Macy boolean_t newfs = B_FALSE; 4278180f8225SMatt Macy boolean_t stream_wantsnewfs, stream_resumingnewfs; 4279eda14cbcSMatt Macy boolean_t newprops = B_FALSE; 4280eda14cbcSMatt Macy uint64_t read_bytes = 0; 4281eda14cbcSMatt Macy uint64_t errflags = 0; 4282eda14cbcSMatt Macy uint64_t parent_snapguid = 0; 4283eda14cbcSMatt Macy prop_changelist_t *clp = NULL; 4284eda14cbcSMatt Macy nvlist_t *snapprops_nvlist = NULL; 4285eda14cbcSMatt Macy nvlist_t *snapholds_nvlist = NULL; 4286eda14cbcSMatt Macy zprop_errflags_t prop_errflags; 4287eda14cbcSMatt Macy nvlist_t *prop_errors = NULL; 4288eda14cbcSMatt Macy boolean_t recursive; 4289eda14cbcSMatt Macy char *snapname = NULL; 4290eda14cbcSMatt Macy char destsnap[MAXPATHLEN * 2]; 4291da5137abSMartin Matuska char origin[MAXNAMELEN] = {0}; 4292eda14cbcSMatt Macy char name[MAXPATHLEN]; 4293da5137abSMartin Matuska char tmp_keylocation[MAXNAMELEN] = {0}; 4294eda14cbcSMatt Macy nvlist_t *rcvprops = NULL; /* props received from the send stream */ 4295eda14cbcSMatt Macy nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */ 4296eda14cbcSMatt Macy nvlist_t *origprops = NULL; /* original props (if destination exists) */ 4297c03c5b1cSMartin Matuska zfs_type_t type = ZFS_TYPE_INVALID; 4298eda14cbcSMatt Macy boolean_t toplevel = B_FALSE; 4299eda14cbcSMatt Macy boolean_t zoned = B_FALSE; 4300eda14cbcSMatt Macy boolean_t hastoken = B_FALSE; 4301eda14cbcSMatt Macy boolean_t redacted; 4302eda14cbcSMatt Macy uint8_t *wkeydata = NULL; 4303eda14cbcSMatt Macy uint_t wkeylen = 0; 4304eda14cbcSMatt Macy 4305c03c5b1cSMartin Matuska #ifndef CLOCK_MONOTONIC_RAW 4306c03c5b1cSMartin Matuska #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC 4307c03c5b1cSMartin Matuska #endif 4308c03c5b1cSMartin Matuska clock_gettime(CLOCK_MONOTONIC_RAW, &begin_time); 4309eda14cbcSMatt Macy 4310eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4311eda14cbcSMatt Macy "cannot receive")); 4312eda14cbcSMatt Macy 4313eda14cbcSMatt Macy recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 4314eda14cbcSMatt Macy ENOENT); 4315eda14cbcSMatt Macy 4316eda14cbcSMatt Macy /* Did the user request holds be skipped via zfs recv -k? */ 4317eda14cbcSMatt Macy boolean_t holds = flags->holds && !flags->skipholds; 4318eda14cbcSMatt Macy 4319eda14cbcSMatt Macy if (stream_avl != NULL) { 4320eda14cbcSMatt Macy char *keylocation = NULL; 4321eda14cbcSMatt Macy nvlist_t *lookup = NULL; 4322eda14cbcSMatt Macy nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, 4323eda14cbcSMatt Macy &snapname); 4324eda14cbcSMatt Macy 4325eda14cbcSMatt Macy (void) nvlist_lookup_uint64(fs, "parentfromsnap", 4326eda14cbcSMatt Macy &parent_snapguid); 4327eda14cbcSMatt Macy err = nvlist_lookup_nvlist(fs, "props", &rcvprops); 4328eda14cbcSMatt Macy if (err) { 4329184c1b94SMartin Matuska rcvprops = fnvlist_alloc(); 4330eda14cbcSMatt Macy newprops = B_TRUE; 4331eda14cbcSMatt Macy } 4332eda14cbcSMatt Macy 4333eda14cbcSMatt Macy /* 4334eda14cbcSMatt Macy * The keylocation property may only be set on encryption roots, 4335eda14cbcSMatt Macy * but this dataset might not become an encryption root until 4336eda14cbcSMatt Macy * recv_fix_encryption_hierarchy() is called. That function 4337eda14cbcSMatt Macy * will fixup the keylocation anyway, so we temporarily unset 4338eda14cbcSMatt Macy * the keylocation for now to avoid any errors from the receive 4339eda14cbcSMatt Macy * ioctl. 4340eda14cbcSMatt Macy */ 4341eda14cbcSMatt Macy err = nvlist_lookup_string(rcvprops, 4342eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation); 4343eda14cbcSMatt Macy if (err == 0) { 4344eda14cbcSMatt Macy strcpy(tmp_keylocation, keylocation); 4345eda14cbcSMatt Macy (void) nvlist_remove_all(rcvprops, 4346eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_KEYLOCATION)); 4347eda14cbcSMatt Macy } 4348eda14cbcSMatt Macy 4349eda14cbcSMatt Macy if (flags->canmountoff) { 4350184c1b94SMartin Matuska fnvlist_add_uint64(rcvprops, 4351184c1b94SMartin Matuska zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0); 4352eda14cbcSMatt Macy } else if (newprops) { /* nothing in rcvprops, eliminate it */ 4353184c1b94SMartin Matuska fnvlist_free(rcvprops); 4354eda14cbcSMatt Macy rcvprops = NULL; 4355eda14cbcSMatt Macy newprops = B_FALSE; 4356eda14cbcSMatt Macy } 4357eda14cbcSMatt Macy if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) { 4358184c1b94SMartin Matuska snapprops_nvlist = fnvlist_lookup_nvlist(lookup, 4359184c1b94SMartin Matuska snapname); 4360eda14cbcSMatt Macy } 4361eda14cbcSMatt Macy if (holds) { 4362eda14cbcSMatt Macy if (0 == nvlist_lookup_nvlist(fs, "snapholds", 4363eda14cbcSMatt Macy &lookup)) { 4364184c1b94SMartin Matuska snapholds_nvlist = fnvlist_lookup_nvlist( 4365184c1b94SMartin Matuska lookup, snapname); 4366eda14cbcSMatt Macy } 4367eda14cbcSMatt Macy } 4368eda14cbcSMatt Macy } 4369eda14cbcSMatt Macy 4370eda14cbcSMatt Macy cp = NULL; 4371eda14cbcSMatt Macy 4372eda14cbcSMatt Macy /* 4373eda14cbcSMatt Macy * Determine how much of the snapshot name stored in the stream 4374eda14cbcSMatt Macy * we are going to tack on to the name they specified on the 4375eda14cbcSMatt Macy * command line, and how much we are going to chop off. 4376eda14cbcSMatt Macy * 4377eda14cbcSMatt Macy * If they specified a snapshot, chop the entire name stored in 4378eda14cbcSMatt Macy * the stream. 4379eda14cbcSMatt Macy */ 4380eda14cbcSMatt Macy if (flags->istail) { 4381eda14cbcSMatt Macy /* 4382eda14cbcSMatt Macy * A filesystem was specified with -e. We want to tack on only 4383eda14cbcSMatt Macy * the tail of the sent snapshot path. 4384eda14cbcSMatt Macy */ 4385eda14cbcSMatt Macy if (strchr(tosnap, '@')) { 4386eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 4387eda14cbcSMatt Macy "argument - snapshot not allowed with -e")); 4388eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4389eda14cbcSMatt Macy goto out; 4390eda14cbcSMatt Macy } 4391eda14cbcSMatt Macy 4392eda14cbcSMatt Macy chopprefix = strrchr(sendfs, '/'); 4393eda14cbcSMatt Macy 4394eda14cbcSMatt Macy if (chopprefix == NULL) { 4395eda14cbcSMatt Macy /* 4396eda14cbcSMatt Macy * The tail is the poolname, so we need to 4397eda14cbcSMatt Macy * prepend a path separator. 4398eda14cbcSMatt Macy */ 4399eda14cbcSMatt Macy int len = strlen(drrb->drr_toname); 4400eda14cbcSMatt Macy cp = malloc(len + 2); 4401eda14cbcSMatt Macy cp[0] = '/'; 4402eda14cbcSMatt Macy (void) strcpy(&cp[1], drrb->drr_toname); 4403eda14cbcSMatt Macy chopprefix = cp; 4404eda14cbcSMatt Macy } else { 4405eda14cbcSMatt Macy chopprefix = drrb->drr_toname + (chopprefix - sendfs); 4406eda14cbcSMatt Macy } 4407eda14cbcSMatt Macy } else if (flags->isprefix) { 4408eda14cbcSMatt Macy /* 4409eda14cbcSMatt Macy * A filesystem was specified with -d. We want to tack on 4410eda14cbcSMatt Macy * everything but the first element of the sent snapshot path 4411eda14cbcSMatt Macy * (all but the pool name). 4412eda14cbcSMatt Macy */ 4413eda14cbcSMatt Macy if (strchr(tosnap, '@')) { 4414eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 4415eda14cbcSMatt Macy "argument - snapshot not allowed with -d")); 4416eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4417eda14cbcSMatt Macy goto out; 4418eda14cbcSMatt Macy } 4419eda14cbcSMatt Macy 4420eda14cbcSMatt Macy chopprefix = strchr(drrb->drr_toname, '/'); 4421eda14cbcSMatt Macy if (chopprefix == NULL) 4422eda14cbcSMatt Macy chopprefix = strchr(drrb->drr_toname, '@'); 4423eda14cbcSMatt Macy } else if (strchr(tosnap, '@') == NULL) { 4424eda14cbcSMatt Macy /* 4425eda14cbcSMatt Macy * If a filesystem was specified without -d or -e, we want to 4426eda14cbcSMatt Macy * tack on everything after the fs specified by 'zfs send'. 4427eda14cbcSMatt Macy */ 4428eda14cbcSMatt Macy chopprefix = drrb->drr_toname + strlen(sendfs); 4429eda14cbcSMatt Macy } else { 4430eda14cbcSMatt Macy /* A snapshot was specified as an exact path (no -d or -e). */ 4431eda14cbcSMatt Macy if (recursive) { 4432eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4433eda14cbcSMatt Macy "cannot specify snapshot name for multi-snapshot " 4434eda14cbcSMatt Macy "stream")); 4435eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 4436eda14cbcSMatt Macy goto out; 4437eda14cbcSMatt Macy } 4438eda14cbcSMatt Macy chopprefix = drrb->drr_toname + strlen(drrb->drr_toname); 4439eda14cbcSMatt Macy } 4440eda14cbcSMatt Macy 4441eda14cbcSMatt Macy ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname); 4442eda14cbcSMatt Macy ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL); 4443eda14cbcSMatt Macy ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) || 4444eda14cbcSMatt Macy strchr(sendfs, '/') == NULL); 4445eda14cbcSMatt Macy ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' || 4446eda14cbcSMatt Macy chopprefix[0] == '\0'); 4447eda14cbcSMatt Macy 4448eda14cbcSMatt Macy /* 4449eda14cbcSMatt Macy * Determine name of destination snapshot. 4450eda14cbcSMatt Macy */ 4451eda14cbcSMatt Macy (void) strlcpy(destsnap, tosnap, sizeof (destsnap)); 4452eda14cbcSMatt Macy (void) strlcat(destsnap, chopprefix, sizeof (destsnap)); 4453eda14cbcSMatt Macy free(cp); 4454eda14cbcSMatt Macy if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) { 4455eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4456eda14cbcSMatt Macy goto out; 4457eda14cbcSMatt Macy } 4458eda14cbcSMatt Macy 4459eda14cbcSMatt Macy /* 4460eda14cbcSMatt Macy * Determine the name of the origin snapshot. 4461eda14cbcSMatt Macy */ 4462eda14cbcSMatt Macy if (originsnap) { 4463eda14cbcSMatt Macy (void) strlcpy(origin, originsnap, sizeof (origin)); 4464eda14cbcSMatt Macy if (flags->verbose) 4465eda14cbcSMatt Macy (void) printf("using provided clone origin %s\n", 4466eda14cbcSMatt Macy origin); 4467eda14cbcSMatt Macy } else if (drrb->drr_flags & DRR_FLAG_CLONE) { 4468eda14cbcSMatt Macy if (guid_to_name(hdl, destsnap, 4469eda14cbcSMatt Macy drrb->drr_fromguid, B_FALSE, origin) != 0) { 4470eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4471eda14cbcSMatt Macy "local origin for clone %s does not exist"), 4472eda14cbcSMatt Macy destsnap); 4473eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_NOENT, errbuf); 4474eda14cbcSMatt Macy goto out; 4475eda14cbcSMatt Macy } 4476eda14cbcSMatt Macy if (flags->verbose) 4477eda14cbcSMatt Macy (void) printf("found clone origin %s\n", origin); 4478eda14cbcSMatt Macy } 4479eda14cbcSMatt Macy 4480eda14cbcSMatt Macy if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 4481eda14cbcSMatt Macy DMU_BACKUP_FEATURE_DEDUP)) { 4482eda14cbcSMatt Macy (void) fprintf(stderr, 4483eda14cbcSMatt Macy gettext("ERROR: \"zfs receive\" no longer supports " 4484eda14cbcSMatt Macy "deduplicated send streams. Use\n" 4485eda14cbcSMatt Macy "the \"zstream redup\" command to convert this stream " 4486eda14cbcSMatt Macy "to a regular,\n" 4487eda14cbcSMatt Macy "non-deduplicated stream.\n")); 4488eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_NOTSUP, errbuf); 4489eda14cbcSMatt Macy goto out; 4490eda14cbcSMatt Macy } 4491eda14cbcSMatt Macy 4492eda14cbcSMatt Macy boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 4493eda14cbcSMatt Macy DMU_BACKUP_FEATURE_RESUMING; 4494eda14cbcSMatt Macy boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 4495eda14cbcSMatt Macy DMU_BACKUP_FEATURE_RAW; 4496eda14cbcSMatt Macy boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 4497eda14cbcSMatt Macy DMU_BACKUP_FEATURE_EMBED_DATA; 4498eda14cbcSMatt Macy stream_wantsnewfs = (drrb->drr_fromguid == 0 || 4499eda14cbcSMatt Macy (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming; 4500180f8225SMatt Macy stream_resumingnewfs = (drrb->drr_fromguid == 0 || 4501180f8225SMatt Macy (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && resuming; 4502eda14cbcSMatt Macy 4503eda14cbcSMatt Macy if (stream_wantsnewfs) { 4504eda14cbcSMatt Macy /* 4505eda14cbcSMatt Macy * if the parent fs does not exist, look for it based on 4506eda14cbcSMatt Macy * the parent snap GUID 4507eda14cbcSMatt Macy */ 4508eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4509eda14cbcSMatt Macy "cannot receive new filesystem stream")); 4510eda14cbcSMatt Macy 4511eda14cbcSMatt Macy (void) strcpy(name, destsnap); 4512eda14cbcSMatt Macy cp = strrchr(name, '/'); 4513eda14cbcSMatt Macy if (cp) 4514eda14cbcSMatt Macy *cp = '\0'; 4515eda14cbcSMatt Macy if (cp && 4516eda14cbcSMatt Macy !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) { 4517eda14cbcSMatt Macy char suffix[ZFS_MAX_DATASET_NAME_LEN]; 4518eda14cbcSMatt Macy (void) strcpy(suffix, strrchr(destsnap, '/')); 4519eda14cbcSMatt Macy if (guid_to_name(hdl, name, parent_snapguid, 4520eda14cbcSMatt Macy B_FALSE, destsnap) == 0) { 4521eda14cbcSMatt Macy *strchr(destsnap, '@') = '\0'; 4522eda14cbcSMatt Macy (void) strcat(destsnap, suffix); 4523eda14cbcSMatt Macy } 4524eda14cbcSMatt Macy } 4525eda14cbcSMatt Macy } else { 4526eda14cbcSMatt Macy /* 4527eda14cbcSMatt Macy * If the fs does not exist, look for it based on the 4528eda14cbcSMatt Macy * fromsnap GUID. 4529eda14cbcSMatt Macy */ 4530eda14cbcSMatt Macy if (resuming) { 4531eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), 4532eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4533eda14cbcSMatt Macy "cannot receive resume stream")); 4534eda14cbcSMatt Macy } else { 4535eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), 4536eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4537eda14cbcSMatt Macy "cannot receive incremental stream")); 4538eda14cbcSMatt Macy } 4539eda14cbcSMatt Macy 4540eda14cbcSMatt Macy (void) strcpy(name, destsnap); 4541eda14cbcSMatt Macy *strchr(name, '@') = '\0'; 4542eda14cbcSMatt Macy 4543eda14cbcSMatt Macy /* 4544eda14cbcSMatt Macy * If the exact receive path was specified and this is the 4545eda14cbcSMatt Macy * topmost path in the stream, then if the fs does not exist we 4546eda14cbcSMatt Macy * should look no further. 4547eda14cbcSMatt Macy */ 4548eda14cbcSMatt Macy if ((flags->isprefix || (*(chopprefix = drrb->drr_toname + 4549eda14cbcSMatt Macy strlen(sendfs)) != '\0' && *chopprefix != '@')) && 4550eda14cbcSMatt Macy !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) { 4551eda14cbcSMatt Macy char snap[ZFS_MAX_DATASET_NAME_LEN]; 4552eda14cbcSMatt Macy (void) strcpy(snap, strchr(destsnap, '@')); 4553eda14cbcSMatt Macy if (guid_to_name(hdl, name, drrb->drr_fromguid, 4554eda14cbcSMatt Macy B_FALSE, destsnap) == 0) { 4555eda14cbcSMatt Macy *strchr(destsnap, '@') = '\0'; 4556eda14cbcSMatt Macy (void) strcat(destsnap, snap); 4557eda14cbcSMatt Macy } 4558eda14cbcSMatt Macy } 4559eda14cbcSMatt Macy } 4560eda14cbcSMatt Macy 4561eda14cbcSMatt Macy (void) strcpy(name, destsnap); 4562eda14cbcSMatt Macy *strchr(name, '@') = '\0'; 4563eda14cbcSMatt Macy 4564eda14cbcSMatt Macy redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 4565eda14cbcSMatt Macy DMU_BACKUP_FEATURE_REDACTED; 4566eda14cbcSMatt Macy 4567*271171e0SMartin Matuska if (flags->heal) { 4568*271171e0SMartin Matuska if (flags->isprefix || flags->istail || flags->force || 4569*271171e0SMartin Matuska flags->canmountoff || flags->resumable || flags->nomount || 4570*271171e0SMartin Matuska flags->skipholds) { 4571*271171e0SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4572*271171e0SMartin Matuska "corrective recv can not be used when combined with" 4573*271171e0SMartin Matuska " this flag")); 4574*271171e0SMartin Matuska err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4575*271171e0SMartin Matuska goto out; 4576*271171e0SMartin Matuska } 4577*271171e0SMartin Matuska uint64_t guid = 4578*271171e0SMartin Matuska get_snap_guid(hdl, name, strchr(destsnap, '@') + 1); 4579*271171e0SMartin Matuska if (guid == 0) { 4580*271171e0SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4581*271171e0SMartin Matuska "corrective recv must specify an existing snapshot" 4582*271171e0SMartin Matuska " to heal")); 4583*271171e0SMartin Matuska err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 4584*271171e0SMartin Matuska goto out; 4585*271171e0SMartin Matuska } else if (guid != drrb->drr_toguid) { 4586*271171e0SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4587*271171e0SMartin Matuska "local snapshot doesn't match the snapshot" 4588*271171e0SMartin Matuska " in the provided stream")); 4589*271171e0SMartin Matuska err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf); 4590*271171e0SMartin Matuska goto out; 4591*271171e0SMartin Matuska } 4592*271171e0SMartin Matuska } else if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) { 4593eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4594*271171e0SMartin Matuska zfs_handle_t *zhp = NULL; 4595eda14cbcSMatt Macy boolean_t encrypted; 4596eda14cbcSMatt Macy 4597eda14cbcSMatt Macy (void) strcpy(zc.zc_name, name); 4598eda14cbcSMatt Macy 4599eda14cbcSMatt Macy /* 4600eda14cbcSMatt Macy * Destination fs exists. It must be one of these cases: 4601eda14cbcSMatt Macy * - an incremental send stream 4602eda14cbcSMatt Macy * - the stream specifies a new fs (full stream or clone) 4603eda14cbcSMatt Macy * and they want us to blow away the existing fs (and 4604eda14cbcSMatt Macy * have therefore specified -F and removed any snapshots) 4605eda14cbcSMatt Macy * - we are resuming a failed receive. 4606eda14cbcSMatt Macy */ 4607eda14cbcSMatt Macy if (stream_wantsnewfs) { 4608eda14cbcSMatt Macy boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL; 4609eda14cbcSMatt Macy if (!flags->force) { 4610eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4611eda14cbcSMatt Macy "destination '%s' exists\n" 4612eda14cbcSMatt Macy "must specify -F to overwrite it"), name); 4613eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_EXISTS, errbuf); 4614eda14cbcSMatt Macy goto out; 4615eda14cbcSMatt Macy } 4616eda14cbcSMatt Macy if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT, 4617eda14cbcSMatt Macy &zc) == 0) { 4618eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4619eda14cbcSMatt Macy "destination has snapshots (eg. %s)\n" 4620eda14cbcSMatt Macy "must destroy them to overwrite it"), 4621eda14cbcSMatt Macy zc.zc_name); 4622eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_EXISTS, errbuf); 4623eda14cbcSMatt Macy goto out; 4624eda14cbcSMatt Macy } 4625eda14cbcSMatt Macy if (is_volume && strrchr(name, '/') == NULL) { 4626eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4627eda14cbcSMatt Macy "destination %s is the root dataset\n" 4628eda14cbcSMatt Macy "cannot overwrite with a ZVOL"), 4629eda14cbcSMatt Macy name); 4630eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_EXISTS, errbuf); 4631eda14cbcSMatt Macy goto out; 4632eda14cbcSMatt Macy } 4633eda14cbcSMatt Macy if (is_volume && 4634eda14cbcSMatt Macy zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT, 4635eda14cbcSMatt Macy &zc) == 0) { 4636eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4637eda14cbcSMatt Macy "destination has children (eg. %s)\n" 4638eda14cbcSMatt Macy "cannot overwrite with a ZVOL"), 4639eda14cbcSMatt Macy zc.zc_name); 4640eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf); 4641eda14cbcSMatt Macy goto out; 4642eda14cbcSMatt Macy } 4643eda14cbcSMatt Macy } 4644eda14cbcSMatt Macy 4645eda14cbcSMatt Macy if ((zhp = zfs_open(hdl, name, 4646eda14cbcSMatt Macy ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 4647eda14cbcSMatt Macy err = -1; 4648eda14cbcSMatt Macy goto out; 4649eda14cbcSMatt Macy } 4650eda14cbcSMatt Macy 4651eda14cbcSMatt Macy if (stream_wantsnewfs && 4652eda14cbcSMatt Macy zhp->zfs_dmustats.dds_origin[0]) { 4653eda14cbcSMatt Macy zfs_close(zhp); 4654eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4655eda14cbcSMatt Macy "destination '%s' is a clone\n" 4656eda14cbcSMatt Macy "must destroy it to overwrite it"), name); 4657eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_EXISTS, errbuf); 4658eda14cbcSMatt Macy goto out; 4659eda14cbcSMatt Macy } 4660eda14cbcSMatt Macy 4661eda14cbcSMatt Macy /* 4662eda14cbcSMatt Macy * Raw sends can not be performed as an incremental on top 4663eda14cbcSMatt Macy * of existing unencrypted datasets. zfs recv -F can't be 4664eda14cbcSMatt Macy * used to blow away an existing encrypted filesystem. This 4665eda14cbcSMatt Macy * is because it would require the dsl dir to point to the 4666eda14cbcSMatt Macy * new key (or lack of a key) and the old key at the same 4667eda14cbcSMatt Macy * time. The -F flag may still be used for deleting 4668eda14cbcSMatt Macy * intermediate snapshots that would otherwise prevent the 4669eda14cbcSMatt Macy * receive from working. 4670eda14cbcSMatt Macy */ 4671eda14cbcSMatt Macy encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != 4672eda14cbcSMatt Macy ZIO_CRYPT_OFF; 4673eda14cbcSMatt Macy if (!stream_wantsnewfs && !encrypted && raw) { 4674eda14cbcSMatt Macy zfs_close(zhp); 4675eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4676eda14cbcSMatt Macy "cannot perform raw receive on top of " 4677eda14cbcSMatt Macy "existing unencrypted dataset")); 4678eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); 4679eda14cbcSMatt Macy goto out; 4680eda14cbcSMatt Macy } 4681eda14cbcSMatt Macy 4682eda14cbcSMatt Macy if (stream_wantsnewfs && flags->force && 4683eda14cbcSMatt Macy ((raw && !encrypted) || encrypted)) { 4684eda14cbcSMatt Macy zfs_close(zhp); 4685eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4686eda14cbcSMatt Macy "zfs receive -F cannot be used to destroy an " 4687eda14cbcSMatt Macy "encrypted filesystem or overwrite an " 4688eda14cbcSMatt Macy "unencrypted one with an encrypted one")); 4689eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); 4690eda14cbcSMatt Macy goto out; 4691eda14cbcSMatt Macy } 4692eda14cbcSMatt Macy 4693eda14cbcSMatt Macy if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && 4694180f8225SMatt Macy (stream_wantsnewfs || stream_resumingnewfs)) { 4695eda14cbcSMatt Macy /* We can't do online recv in this case */ 4696eda14cbcSMatt Macy clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 4697eda14cbcSMatt Macy flags->forceunmount ? MS_FORCE : 0); 4698eda14cbcSMatt Macy if (clp == NULL) { 4699eda14cbcSMatt Macy zfs_close(zhp); 4700eda14cbcSMatt Macy err = -1; 4701eda14cbcSMatt Macy goto out; 4702eda14cbcSMatt Macy } 4703eda14cbcSMatt Macy if (changelist_prefix(clp) != 0) { 4704eda14cbcSMatt Macy changelist_free(clp); 4705eda14cbcSMatt Macy zfs_close(zhp); 4706eda14cbcSMatt Macy err = -1; 4707eda14cbcSMatt Macy goto out; 4708eda14cbcSMatt Macy } 4709eda14cbcSMatt Macy } 4710eda14cbcSMatt Macy 4711eda14cbcSMatt Macy /* 4712eda14cbcSMatt Macy * If we are resuming a newfs, set newfs here so that we will 4713eda14cbcSMatt Macy * mount it if the recv succeeds this time. We can tell 4714eda14cbcSMatt Macy * that it was a newfs on the first recv because the fs 4715eda14cbcSMatt Macy * itself will be inconsistent (if the fs existed when we 4716eda14cbcSMatt Macy * did the first recv, we would have received it into 4717eda14cbcSMatt Macy * .../%recv). 4718eda14cbcSMatt Macy */ 4719eda14cbcSMatt Macy if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT)) 4720eda14cbcSMatt Macy newfs = B_TRUE; 4721eda14cbcSMatt Macy 4722eda14cbcSMatt Macy /* we want to know if we're zoned when validating -o|-x props */ 4723eda14cbcSMatt Macy zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 4724eda14cbcSMatt Macy 4725eda14cbcSMatt Macy /* may need this info later, get it now we have zhp around */ 4726eda14cbcSMatt Macy if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0, 4727eda14cbcSMatt Macy NULL, NULL, 0, B_TRUE) == 0) 4728eda14cbcSMatt Macy hastoken = B_TRUE; 4729eda14cbcSMatt Macy 4730eda14cbcSMatt Macy /* gather existing properties on destination */ 4731eda14cbcSMatt Macy origprops = fnvlist_alloc(); 4732eda14cbcSMatt Macy fnvlist_merge(origprops, zhp->zfs_props); 4733eda14cbcSMatt Macy fnvlist_merge(origprops, zhp->zfs_user_props); 4734eda14cbcSMatt Macy 4735eda14cbcSMatt Macy zfs_close(zhp); 4736eda14cbcSMatt Macy } else { 4737eda14cbcSMatt Macy zfs_handle_t *zhp; 4738eda14cbcSMatt Macy 4739eda14cbcSMatt Macy /* 4740eda14cbcSMatt Macy * Destination filesystem does not exist. Therefore we better 4741eda14cbcSMatt Macy * be creating a new filesystem (either from a full backup, or 4742eda14cbcSMatt Macy * a clone). It would therefore be invalid if the user 4743eda14cbcSMatt Macy * specified only the pool name (i.e. if the destination name 4744eda14cbcSMatt Macy * contained no slash character). 4745eda14cbcSMatt Macy */ 4746eda14cbcSMatt Macy cp = strrchr(name, '/'); 4747eda14cbcSMatt Macy 4748eda14cbcSMatt Macy if (!stream_wantsnewfs || cp == NULL) { 4749eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4750eda14cbcSMatt Macy "destination '%s' does not exist"), name); 4751eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_NOENT, errbuf); 4752eda14cbcSMatt Macy goto out; 4753eda14cbcSMatt Macy } 4754eda14cbcSMatt Macy 4755eda14cbcSMatt Macy /* 4756eda14cbcSMatt Macy * Trim off the final dataset component so we perform the 4757eda14cbcSMatt Macy * recvbackup ioctl to the filesystems's parent. 4758eda14cbcSMatt Macy */ 4759eda14cbcSMatt Macy *cp = '\0'; 4760eda14cbcSMatt Macy 4761eda14cbcSMatt Macy if (flags->isprefix && !flags->istail && !flags->dryrun && 4762eda14cbcSMatt Macy create_parents(hdl, destsnap, strlen(tosnap)) != 0) { 4763eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); 4764eda14cbcSMatt Macy goto out; 4765eda14cbcSMatt Macy } 4766eda14cbcSMatt Macy 4767eda14cbcSMatt Macy /* validate parent */ 4768eda14cbcSMatt Macy zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 4769eda14cbcSMatt Macy if (zhp == NULL) { 4770eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); 4771eda14cbcSMatt Macy goto out; 4772eda14cbcSMatt Macy } 4773eda14cbcSMatt Macy if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 4774eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4775eda14cbcSMatt Macy "parent '%s' is not a filesystem"), name); 4776eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf); 4777eda14cbcSMatt Macy zfs_close(zhp); 4778eda14cbcSMatt Macy goto out; 4779eda14cbcSMatt Macy } 4780eda14cbcSMatt Macy 4781eda14cbcSMatt Macy zfs_close(zhp); 4782eda14cbcSMatt Macy 4783eda14cbcSMatt Macy newfs = B_TRUE; 4784eda14cbcSMatt Macy *cp = '/'; 4785eda14cbcSMatt Macy } 4786eda14cbcSMatt Macy 4787eda14cbcSMatt Macy if (flags->verbose) { 4788*271171e0SMartin Matuska (void) printf("%s %s%s stream of %s into %s\n", 4789eda14cbcSMatt Macy flags->dryrun ? "would receive" : "receiving", 4790*271171e0SMartin Matuska flags->heal ? " corrective" : "", 4791eda14cbcSMatt Macy drrb->drr_fromguid ? "incremental" : "full", 4792eda14cbcSMatt Macy drrb->drr_toname, destsnap); 4793eda14cbcSMatt Macy (void) fflush(stdout); 4794eda14cbcSMatt Macy } 4795eda14cbcSMatt Macy 4796eda14cbcSMatt Macy /* 4797eda14cbcSMatt Macy * If this is the top-level dataset, record it so we can use it 4798eda14cbcSMatt Macy * for recursive operations later. 4799eda14cbcSMatt Macy */ 4800eda14cbcSMatt Macy if (top_zfs != NULL && 4801eda14cbcSMatt Macy (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) { 4802eda14cbcSMatt Macy toplevel = B_TRUE; 4803eda14cbcSMatt Macy if (*top_zfs == NULL) 4804eda14cbcSMatt Macy *top_zfs = zfs_strdup(hdl, name); 4805eda14cbcSMatt Macy } 4806eda14cbcSMatt Macy 4807eda14cbcSMatt Macy if (drrb->drr_type == DMU_OST_ZVOL) { 4808eda14cbcSMatt Macy type = ZFS_TYPE_VOLUME; 4809eda14cbcSMatt Macy } else if (drrb->drr_type == DMU_OST_ZFS) { 4810eda14cbcSMatt Macy type = ZFS_TYPE_FILESYSTEM; 4811eda14cbcSMatt Macy } else { 4812eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4813eda14cbcSMatt Macy "invalid record type: 0x%d"), drrb->drr_type); 4814eda14cbcSMatt Macy err = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 4815eda14cbcSMatt Macy goto out; 4816eda14cbcSMatt Macy } 4817eda14cbcSMatt Macy if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive, 4818eda14cbcSMatt Macy stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops, 4819eda14cbcSMatt Macy &oxprops, &wkeydata, &wkeylen, errbuf)) != 0) 4820eda14cbcSMatt Macy goto out; 4821eda14cbcSMatt Macy 4822eda14cbcSMatt Macy /* 4823eda14cbcSMatt Macy * When sending with properties (zfs send -p), the encryption property 4824eda14cbcSMatt Macy * is not included because it is a SETONCE property and therefore 4825eda14cbcSMatt Macy * treated as read only. However, we are always able to determine its 4826eda14cbcSMatt Macy * value because raw sends will include it in the DRR_BDEGIN payload 4827eda14cbcSMatt Macy * and non-raw sends with properties are not allowed for encrypted 4828eda14cbcSMatt Macy * datasets. Therefore, if this is a non-raw properties stream, we can 4829eda14cbcSMatt Macy * infer that the value should be ZIO_CRYPT_OFF and manually add that 4830eda14cbcSMatt Macy * to the received properties. 4831eda14cbcSMatt Macy */ 4832eda14cbcSMatt Macy if (stream_wantsnewfs && !raw && rcvprops != NULL && 4833eda14cbcSMatt Macy !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) { 4834eda14cbcSMatt Macy if (oxprops == NULL) 4835eda14cbcSMatt Macy oxprops = fnvlist_alloc(); 4836eda14cbcSMatt Macy fnvlist_add_uint64(oxprops, 4837eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF); 4838eda14cbcSMatt Macy } 4839eda14cbcSMatt Macy 484016038816SMartin Matuska if (flags->dryrun) { 484116038816SMartin Matuska void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE); 484216038816SMartin Matuska 484316038816SMartin Matuska /* 484416038816SMartin Matuska * We have read the DRR_BEGIN record, but we have 484516038816SMartin Matuska * not yet read the payload. For non-dryrun sends 484616038816SMartin Matuska * this will be done by the kernel, so we must 484716038816SMartin Matuska * emulate that here, before attempting to read 484816038816SMartin Matuska * more records. 484916038816SMartin Matuska */ 485016038816SMartin Matuska err = recv_read(hdl, infd, buf, drr->drr_payloadlen, 485116038816SMartin Matuska flags->byteswap, NULL); 485216038816SMartin Matuska free(buf); 485316038816SMartin Matuska if (err != 0) 485416038816SMartin Matuska goto out; 485516038816SMartin Matuska 485616038816SMartin Matuska err = recv_skip(hdl, infd, flags->byteswap); 485716038816SMartin Matuska goto out; 485816038816SMartin Matuska } 485916038816SMartin Matuska 4860*271171e0SMartin Matuska if (flags->heal) { 4861*271171e0SMartin Matuska err = ioctl_err = lzc_receive_with_heal(destsnap, rcvprops, 4862*271171e0SMartin Matuska oxprops, wkeydata, wkeylen, origin, flags->force, 4863*271171e0SMartin Matuska flags->heal, flags->resumable, raw, infd, drr_noswap, -1, 4864*271171e0SMartin Matuska &read_bytes, &errflags, NULL, &prop_errors); 4865*271171e0SMartin Matuska } else { 4866eda14cbcSMatt Macy err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops, 4867*271171e0SMartin Matuska oxprops, wkeydata, wkeylen, origin, flags->force, 4868*271171e0SMartin Matuska flags->resumable, raw, infd, drr_noswap, -1, &read_bytes, 4869*271171e0SMartin Matuska &errflags, NULL, &prop_errors); 4870*271171e0SMartin Matuska } 4871eda14cbcSMatt Macy ioctl_errno = ioctl_err; 4872eda14cbcSMatt Macy prop_errflags = errflags; 4873eda14cbcSMatt Macy 4874eda14cbcSMatt Macy if (err == 0) { 4875eda14cbcSMatt Macy nvpair_t *prop_err = NULL; 4876eda14cbcSMatt Macy 4877eda14cbcSMatt Macy while ((prop_err = nvlist_next_nvpair(prop_errors, 4878eda14cbcSMatt Macy prop_err)) != NULL) { 4879eda14cbcSMatt Macy char tbuf[1024]; 4880eda14cbcSMatt Macy zfs_prop_t prop; 4881eda14cbcSMatt Macy int intval; 4882eda14cbcSMatt Macy 4883eda14cbcSMatt Macy prop = zfs_name_to_prop(nvpair_name(prop_err)); 4884eda14cbcSMatt Macy (void) nvpair_value_int32(prop_err, &intval); 4885eda14cbcSMatt Macy if (strcmp(nvpair_name(prop_err), 4886eda14cbcSMatt Macy ZPROP_N_MORE_ERRORS) == 0) { 4887eda14cbcSMatt Macy trunc_prop_errs(intval); 4888eda14cbcSMatt Macy break; 4889eda14cbcSMatt Macy } else if (snapname == NULL || finalsnap == NULL || 4890eda14cbcSMatt Macy strcmp(finalsnap, snapname) == 0 || 4891eda14cbcSMatt Macy strcmp(nvpair_name(prop_err), 4892eda14cbcSMatt Macy zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) { 4893eda14cbcSMatt Macy /* 4894eda14cbcSMatt Macy * Skip the special case of, for example, 4895eda14cbcSMatt Macy * "refquota", errors on intermediate 4896eda14cbcSMatt Macy * snapshots leading up to a final one. 4897eda14cbcSMatt Macy * That's why we have all of the checks above. 4898eda14cbcSMatt Macy * 4899eda14cbcSMatt Macy * See zfs_ioctl.c's extract_delay_props() for 4900eda14cbcSMatt Macy * a list of props which can fail on 4901eda14cbcSMatt Macy * intermediate snapshots, but shouldn't 4902eda14cbcSMatt Macy * affect the overall receive. 4903eda14cbcSMatt Macy */ 4904eda14cbcSMatt Macy (void) snprintf(tbuf, sizeof (tbuf), 4905eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, 4906eda14cbcSMatt Macy "cannot receive %s property on %s"), 4907eda14cbcSMatt Macy nvpair_name(prop_err), name); 4908eda14cbcSMatt Macy zfs_setprop_error(hdl, prop, intval, tbuf); 4909eda14cbcSMatt Macy } 4910eda14cbcSMatt Macy } 4911eda14cbcSMatt Macy } 4912eda14cbcSMatt Macy 4913eda14cbcSMatt Macy if (err == 0 && snapprops_nvlist) { 4914eda14cbcSMatt Macy zfs_cmd_t zc = {"\0"}; 4915eda14cbcSMatt Macy 4916eda14cbcSMatt Macy (void) strcpy(zc.zc_name, destsnap); 4917eda14cbcSMatt Macy zc.zc_cookie = B_TRUE; /* received */ 4918716fd348SMartin Matuska zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist); 4919eda14cbcSMatt Macy (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 4920eda14cbcSMatt Macy zcmd_free_nvlists(&zc); 4921eda14cbcSMatt Macy } 4922eda14cbcSMatt Macy if (err == 0 && snapholds_nvlist) { 4923eda14cbcSMatt Macy nvpair_t *pair; 4924eda14cbcSMatt Macy nvlist_t *holds, *errors = NULL; 4925eda14cbcSMatt Macy int cleanup_fd = -1; 4926eda14cbcSMatt Macy 4927eda14cbcSMatt Macy VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP)); 4928eda14cbcSMatt Macy for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL); 4929eda14cbcSMatt Macy pair != NULL; 4930eda14cbcSMatt Macy pair = nvlist_next_nvpair(snapholds_nvlist, pair)) { 4931184c1b94SMartin Matuska fnvlist_add_string(holds, destsnap, nvpair_name(pair)); 4932eda14cbcSMatt Macy } 4933eda14cbcSMatt Macy (void) lzc_hold(holds, cleanup_fd, &errors); 4934184c1b94SMartin Matuska fnvlist_free(snapholds_nvlist); 4935184c1b94SMartin Matuska fnvlist_free(holds); 4936eda14cbcSMatt Macy } 4937eda14cbcSMatt Macy 4938eda14cbcSMatt Macy if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) { 4939eda14cbcSMatt Macy /* 4940eda14cbcSMatt Macy * It may be that this snapshot already exists, 4941eda14cbcSMatt Macy * in which case we want to consume & ignore it 4942eda14cbcSMatt Macy * rather than failing. 4943eda14cbcSMatt Macy */ 4944eda14cbcSMatt Macy avl_tree_t *local_avl; 4945eda14cbcSMatt Macy nvlist_t *local_nv, *fs; 4946eda14cbcSMatt Macy cp = strchr(destsnap, '@'); 4947eda14cbcSMatt Macy 4948eda14cbcSMatt Macy /* 4949eda14cbcSMatt Macy * XXX Do this faster by just iterating over snaps in 4950eda14cbcSMatt Macy * this fs. Also if zc_value does not exist, we will 4951eda14cbcSMatt Macy * get a strange "does not exist" error message. 4952eda14cbcSMatt Macy */ 4953eda14cbcSMatt Macy *cp = '\0'; 4954eda14cbcSMatt Macy if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE, 495516038816SMartin Matuska B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, 495616038816SMartin Matuska B_TRUE, &local_nv, &local_avl) == 0) { 4957eda14cbcSMatt Macy *cp = '@'; 4958eda14cbcSMatt Macy fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); 4959eda14cbcSMatt Macy fsavl_destroy(local_avl); 4960184c1b94SMartin Matuska fnvlist_free(local_nv); 4961eda14cbcSMatt Macy 4962eda14cbcSMatt Macy if (fs != NULL) { 4963eda14cbcSMatt Macy if (flags->verbose) { 4964eda14cbcSMatt Macy (void) printf("snap %s already exists; " 4965eda14cbcSMatt Macy "ignoring\n", destsnap); 4966eda14cbcSMatt Macy } 4967eda14cbcSMatt Macy err = ioctl_err = recv_skip(hdl, infd, 4968eda14cbcSMatt Macy flags->byteswap); 4969eda14cbcSMatt Macy } 4970eda14cbcSMatt Macy } 4971eda14cbcSMatt Macy *cp = '@'; 4972eda14cbcSMatt Macy } 4973eda14cbcSMatt Macy 4974eda14cbcSMatt Macy if (ioctl_err != 0) { 4975eda14cbcSMatt Macy switch (ioctl_errno) { 4976eda14cbcSMatt Macy case ENODEV: 4977eda14cbcSMatt Macy cp = strchr(destsnap, '@'); 4978eda14cbcSMatt Macy *cp = '\0'; 4979eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4980eda14cbcSMatt Macy "most recent snapshot of %s does not\n" 4981eda14cbcSMatt Macy "match incremental source"), destsnap); 4982eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 4983eda14cbcSMatt Macy *cp = '@'; 4984eda14cbcSMatt Macy break; 4985eda14cbcSMatt Macy case ETXTBSY: 4986eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4987eda14cbcSMatt Macy "destination %s has been modified\n" 4988eda14cbcSMatt Macy "since most recent snapshot"), name); 4989eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 4990eda14cbcSMatt Macy break; 4991eda14cbcSMatt Macy case EACCES: 4992*271171e0SMartin Matuska if (flags->heal) { 4993*271171e0SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4994*271171e0SMartin Matuska "key must be loaded to do a non-raw " 4995*271171e0SMartin Matuska "corrective recv on an encrypted " 4996*271171e0SMartin Matuska "dataset.")); 4997*271171e0SMartin Matuska } else if (raw && stream_wantsnewfs) { 4998eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4999eda14cbcSMatt Macy "failed to create encryption key")); 5000eda14cbcSMatt Macy } else if (raw && !stream_wantsnewfs) { 5001eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5002eda14cbcSMatt Macy "encryption key does not match " 5003eda14cbcSMatt Macy "existing key")); 5004eda14cbcSMatt Macy } else { 5005eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5006eda14cbcSMatt Macy "inherited key must be loaded")); 5007eda14cbcSMatt Macy } 5008eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); 5009eda14cbcSMatt Macy break; 5010eda14cbcSMatt Macy case EEXIST: 5011eda14cbcSMatt Macy cp = strchr(destsnap, '@'); 5012eda14cbcSMatt Macy if (newfs) { 5013eda14cbcSMatt Macy /* it's the containing fs that exists */ 5014eda14cbcSMatt Macy *cp = '\0'; 5015eda14cbcSMatt Macy } 5016eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5017eda14cbcSMatt Macy "destination already exists")); 5018eda14cbcSMatt Macy (void) zfs_error_fmt(hdl, EZFS_EXISTS, 5019eda14cbcSMatt Macy dgettext(TEXT_DOMAIN, "cannot restore to %s"), 5020eda14cbcSMatt Macy destsnap); 5021eda14cbcSMatt Macy *cp = '@'; 5022eda14cbcSMatt Macy break; 5023eda14cbcSMatt Macy case EINVAL: 5024eda14cbcSMatt Macy if (flags->resumable) { 5025eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5026eda14cbcSMatt Macy "kernel modules must be upgraded to " 5027eda14cbcSMatt Macy "receive this stream.")); 5028eda14cbcSMatt Macy } else if (embedded && !raw) { 5029eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5030eda14cbcSMatt Macy "incompatible embedded data stream " 5031eda14cbcSMatt Macy "feature with encrypted receive.")); 5032eda14cbcSMatt Macy } 5033eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 5034eda14cbcSMatt Macy break; 5035eda14cbcSMatt Macy case ECKSUM: 5036eda14cbcSMatt Macy case ZFS_ERR_STREAM_TRUNCATED: 5037*271171e0SMartin Matuska if (flags->heal) 5038*271171e0SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5039*271171e0SMartin Matuska "corrective receive was not able to " 5040*271171e0SMartin Matuska "reconstruct the data needed for " 5041*271171e0SMartin Matuska "healing.")); 5042*271171e0SMartin Matuska else 5043*271171e0SMartin Matuska recv_ecksum_set_aux(hdl, destsnap, 5044*271171e0SMartin Matuska flags->resumable, ioctl_err == ECKSUM); 5045eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 5046eda14cbcSMatt Macy break; 5047eda14cbcSMatt Macy case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH: 5048eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5049eda14cbcSMatt Macy "incremental send stream requires -L " 5050eda14cbcSMatt Macy "(--large-block), to match previous receive.")); 5051eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 5052eda14cbcSMatt Macy break; 5053eda14cbcSMatt Macy case ENOTSUP: 5054*271171e0SMartin Matuska if (flags->heal) 5055eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5056*271171e0SMartin Matuska "stream is not compatible with the " 5057*271171e0SMartin Matuska "data in the pool.")); 5058*271171e0SMartin Matuska else 5059*271171e0SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5060*271171e0SMartin Matuska "pool must be upgraded to receive this " 5061*271171e0SMartin Matuska "stream.")); 5062eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 5063eda14cbcSMatt Macy break; 5064eda14cbcSMatt Macy case EDQUOT: 5065eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5066eda14cbcSMatt Macy "destination %s space quota exceeded."), name); 5067eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_NOSPC, errbuf); 5068eda14cbcSMatt Macy break; 5069eda14cbcSMatt Macy case ZFS_ERR_FROM_IVSET_GUID_MISSING: 5070eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5071eda14cbcSMatt Macy "IV set guid missing. See errata %u at " 5072ac0bf12eSMatt Macy "https://openzfs.github.io/openzfs-docs/msg/" 5073ac0bf12eSMatt Macy "ZFS-8000-ER."), 5074eda14cbcSMatt Macy ZPOOL_ERRATA_ZOL_8308_ENCRYPTION); 5075eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 5076eda14cbcSMatt Macy break; 5077eda14cbcSMatt Macy case ZFS_ERR_FROM_IVSET_GUID_MISMATCH: 5078eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5079eda14cbcSMatt Macy "IV set guid mismatch. See the 'zfs receive' " 5080eda14cbcSMatt Macy "man page section\n discussing the limitations " 5081eda14cbcSMatt Macy "of raw encrypted send streams.")); 5082eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 5083eda14cbcSMatt Macy break; 5084eda14cbcSMatt Macy case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING: 5085eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5086eda14cbcSMatt Macy "Spill block flag missing for raw send.\n" 5087eda14cbcSMatt Macy "The zfs software on the sending system must " 5088eda14cbcSMatt Macy "be updated.")); 5089eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 5090eda14cbcSMatt Macy break; 5091eda14cbcSMatt Macy case EBUSY: 5092eda14cbcSMatt Macy if (hastoken) { 5093eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5094eda14cbcSMatt Macy "destination %s contains " 5095eda14cbcSMatt Macy "partially-complete state from " 5096eda14cbcSMatt Macy "\"zfs receive -s\"."), name); 5097eda14cbcSMatt Macy (void) zfs_error(hdl, EZFS_BUSY, errbuf); 5098eda14cbcSMatt Macy break; 5099eda14cbcSMatt Macy } 5100c03c5b1cSMartin Matuska zfs_fallthrough; 5101eda14cbcSMatt Macy default: 5102eda14cbcSMatt Macy (void) zfs_standard_error(hdl, ioctl_errno, errbuf); 5103eda14cbcSMatt Macy } 5104eda14cbcSMatt Macy } 5105eda14cbcSMatt Macy 5106eda14cbcSMatt Macy /* 5107eda14cbcSMatt Macy * Mount the target filesystem (if created). Also mount any 5108eda14cbcSMatt Macy * children of the target filesystem if we did a replication 5109eda14cbcSMatt Macy * receive (indicated by stream_avl being non-NULL). 5110eda14cbcSMatt Macy */ 5111eda14cbcSMatt Macy if (clp) { 5112eda14cbcSMatt Macy if (!flags->nomount) 5113eda14cbcSMatt Macy err |= changelist_postfix(clp); 5114eda14cbcSMatt Macy changelist_free(clp); 5115eda14cbcSMatt Macy } 5116eda14cbcSMatt Macy 5117eda14cbcSMatt Macy if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted) 5118eda14cbcSMatt Macy flags->domount = B_TRUE; 5119eda14cbcSMatt Macy 5120eda14cbcSMatt Macy if (prop_errflags & ZPROP_ERR_NOCLEAR) { 5121eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " 5122eda14cbcSMatt Macy "failed to clear unreceived properties on %s"), name); 5123eda14cbcSMatt Macy (void) fprintf(stderr, "\n"); 5124eda14cbcSMatt Macy } 5125eda14cbcSMatt Macy if (prop_errflags & ZPROP_ERR_NORESTORE) { 5126eda14cbcSMatt Macy (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " 5127eda14cbcSMatt Macy "failed to restore original properties on %s"), name); 5128eda14cbcSMatt Macy (void) fprintf(stderr, "\n"); 5129eda14cbcSMatt Macy } 5130eda14cbcSMatt Macy 5131eda14cbcSMatt Macy if (err || ioctl_err) { 5132eda14cbcSMatt Macy err = -1; 5133eda14cbcSMatt Macy goto out; 5134eda14cbcSMatt Macy } 5135eda14cbcSMatt Macy 5136eda14cbcSMatt Macy if (flags->verbose) { 5137eda14cbcSMatt Macy char buf1[64]; 5138eda14cbcSMatt Macy char buf2[64]; 5139eda14cbcSMatt Macy uint64_t bytes = read_bytes; 5140c03c5b1cSMartin Matuska struct timespec delta; 5141c03c5b1cSMartin Matuska clock_gettime(CLOCK_MONOTONIC_RAW, &delta); 5142c03c5b1cSMartin Matuska if (begin_time.tv_nsec > delta.tv_nsec) { 5143c03c5b1cSMartin Matuska delta.tv_nsec = 5144c03c5b1cSMartin Matuska 1000000000 + delta.tv_nsec - begin_time.tv_nsec; 5145c03c5b1cSMartin Matuska delta.tv_sec -= 1; 5146c03c5b1cSMartin Matuska } else 5147c03c5b1cSMartin Matuska delta.tv_nsec -= begin_time.tv_nsec; 5148c03c5b1cSMartin Matuska delta.tv_sec -= begin_time.tv_sec; 5149c03c5b1cSMartin Matuska if (delta.tv_sec == 0 && delta.tv_nsec == 0) 5150c03c5b1cSMartin Matuska delta.tv_nsec = 1; 5151c03c5b1cSMartin Matuska double delta_f = delta.tv_sec + (delta.tv_nsec / 1e9); 5152eda14cbcSMatt Macy zfs_nicebytes(bytes, buf1, sizeof (buf1)); 5153c03c5b1cSMartin Matuska zfs_nicebytes(bytes / delta_f, buf2, sizeof (buf2)); 5154eda14cbcSMatt Macy 5155c03c5b1cSMartin Matuska (void) printf("received %s stream in %.2f seconds (%s/sec)\n", 5156c03c5b1cSMartin Matuska buf1, delta_f, buf2); 5157eda14cbcSMatt Macy } 5158eda14cbcSMatt Macy 5159eda14cbcSMatt Macy err = 0; 5160eda14cbcSMatt Macy out: 5161eda14cbcSMatt Macy if (prop_errors != NULL) 5162184c1b94SMartin Matuska fnvlist_free(prop_errors); 5163eda14cbcSMatt Macy 5164eda14cbcSMatt Macy if (tmp_keylocation[0] != '\0') { 5165184c1b94SMartin Matuska fnvlist_add_string(rcvprops, 5166184c1b94SMartin Matuska zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation); 5167eda14cbcSMatt Macy } 5168eda14cbcSMatt Macy 5169eda14cbcSMatt Macy if (newprops) 5170184c1b94SMartin Matuska fnvlist_free(rcvprops); 5171eda14cbcSMatt Macy 5172184c1b94SMartin Matuska fnvlist_free(oxprops); 5173184c1b94SMartin Matuska fnvlist_free(origprops); 5174eda14cbcSMatt Macy 5175eda14cbcSMatt Macy return (err); 5176eda14cbcSMatt Macy } 5177eda14cbcSMatt Macy 5178eda14cbcSMatt Macy /* 5179eda14cbcSMatt Macy * Check properties we were asked to override (both -o|-x) 5180eda14cbcSMatt Macy */ 5181eda14cbcSMatt Macy static boolean_t 5182eda14cbcSMatt Macy zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props, 5183eda14cbcSMatt Macy const char *errbuf) 5184eda14cbcSMatt Macy { 5185c03c5b1cSMartin Matuska nvpair_t *nvp = NULL; 5186eda14cbcSMatt Macy zfs_prop_t prop; 5187eda14cbcSMatt Macy const char *name; 5188eda14cbcSMatt Macy 5189eda14cbcSMatt Macy while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) { 5190eda14cbcSMatt Macy name = nvpair_name(nvp); 5191eda14cbcSMatt Macy prop = zfs_name_to_prop(name); 5192eda14cbcSMatt Macy 51931f1e2261SMartin Matuska if (prop == ZPROP_USERPROP) { 5194eda14cbcSMatt Macy if (!zfs_prop_user(name)) { 5195eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5196c03c5b1cSMartin Matuska "%s: invalid property '%s'"), errbuf, name); 5197eda14cbcSMatt Macy return (B_FALSE); 5198eda14cbcSMatt Macy } 5199eda14cbcSMatt Macy continue; 5200eda14cbcSMatt Macy } 5201eda14cbcSMatt Macy /* 5202eda14cbcSMatt Macy * "origin" is readonly but is used to receive datasets as 5203eda14cbcSMatt Macy * clones so we don't raise an error here 5204eda14cbcSMatt Macy */ 5205eda14cbcSMatt Macy if (prop == ZFS_PROP_ORIGIN) 5206eda14cbcSMatt Macy continue; 5207eda14cbcSMatt Macy 5208eda14cbcSMatt Macy /* encryption params have their own verification later */ 5209eda14cbcSMatt Macy if (prop == ZFS_PROP_ENCRYPTION || 5210eda14cbcSMatt Macy zfs_prop_encryption_key_param(prop)) 5211eda14cbcSMatt Macy continue; 5212eda14cbcSMatt Macy 5213eda14cbcSMatt Macy /* 5214eda14cbcSMatt Macy * cannot override readonly, set-once and other specific 5215eda14cbcSMatt Macy * settable properties 5216eda14cbcSMatt Macy */ 5217eda14cbcSMatt Macy if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION || 5218eda14cbcSMatt Macy prop == ZFS_PROP_VOLSIZE) { 5219eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5220c03c5b1cSMartin Matuska "%s: invalid property '%s'"), errbuf, name); 5221eda14cbcSMatt Macy return (B_FALSE); 5222eda14cbcSMatt Macy } 5223eda14cbcSMatt Macy } 5224eda14cbcSMatt Macy 5225eda14cbcSMatt Macy return (B_TRUE); 5226eda14cbcSMatt Macy } 5227eda14cbcSMatt Macy 5228eda14cbcSMatt Macy static int 5229eda14cbcSMatt Macy zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, 5230eda14cbcSMatt Macy const char *originsnap, recvflags_t *flags, int infd, const char *sendfs, 5231eda14cbcSMatt Macy nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, 5232eda14cbcSMatt Macy const char *finalsnap, nvlist_t *cmdprops) 5233eda14cbcSMatt Macy { 5234eda14cbcSMatt Macy int err; 5235eda14cbcSMatt Macy dmu_replay_record_t drr, drr_noswap; 5236eda14cbcSMatt Macy struct drr_begin *drrb = &drr.drr_u.drr_begin; 52371f1e2261SMartin Matuska char errbuf[ERRBUFLEN]; 5238eda14cbcSMatt Macy zio_cksum_t zcksum = { { 0 } }; 5239eda14cbcSMatt Macy uint64_t featureflags; 5240eda14cbcSMatt Macy int hdrtype; 5241eda14cbcSMatt Macy 5242eda14cbcSMatt Macy (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 5243eda14cbcSMatt Macy "cannot receive")); 5244eda14cbcSMatt Macy 5245eda14cbcSMatt Macy /* check cmdline props, raise an error if they cannot be received */ 5246c03c5b1cSMartin Matuska if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) 5247eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 5248eda14cbcSMatt Macy 5249eda14cbcSMatt Macy if (flags->isprefix && 5250eda14cbcSMatt Macy !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { 5251eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " 5252eda14cbcSMatt Macy "(%s) does not exist"), tosnap); 5253eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOENT, errbuf)); 5254eda14cbcSMatt Macy } 5255eda14cbcSMatt Macy if (originsnap && 5256eda14cbcSMatt Macy !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) { 5257eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs " 5258eda14cbcSMatt Macy "(%s) does not exist"), originsnap); 5259eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_NOENT, errbuf)); 5260eda14cbcSMatt Macy } 5261eda14cbcSMatt Macy 5262eda14cbcSMatt Macy /* read in the BEGIN record */ 5263eda14cbcSMatt Macy if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, 5264eda14cbcSMatt Macy &zcksum))) 5265eda14cbcSMatt Macy return (err); 5266eda14cbcSMatt Macy 5267eda14cbcSMatt Macy if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { 5268eda14cbcSMatt Macy /* It's the double end record at the end of a package */ 5269eda14cbcSMatt Macy return (ENODATA); 5270eda14cbcSMatt Macy } 5271eda14cbcSMatt Macy 5272eda14cbcSMatt Macy /* the kernel needs the non-byteswapped begin record */ 5273eda14cbcSMatt Macy drr_noswap = drr; 5274eda14cbcSMatt Macy 5275eda14cbcSMatt Macy flags->byteswap = B_FALSE; 5276eda14cbcSMatt Macy if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { 5277eda14cbcSMatt Macy /* 5278eda14cbcSMatt Macy * We computed the checksum in the wrong byteorder in 5279eda14cbcSMatt Macy * recv_read() above; do it again correctly. 5280eda14cbcSMatt Macy */ 5281da5137abSMartin Matuska memset(&zcksum, 0, sizeof (zio_cksum_t)); 5282eda14cbcSMatt Macy fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum); 5283eda14cbcSMatt Macy flags->byteswap = B_TRUE; 5284eda14cbcSMatt Macy 5285eda14cbcSMatt Macy drr.drr_type = BSWAP_32(drr.drr_type); 5286eda14cbcSMatt Macy drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); 5287eda14cbcSMatt Macy drrb->drr_magic = BSWAP_64(drrb->drr_magic); 5288eda14cbcSMatt Macy drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); 5289eda14cbcSMatt Macy drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 5290eda14cbcSMatt Macy drrb->drr_type = BSWAP_32(drrb->drr_type); 5291eda14cbcSMatt Macy drrb->drr_flags = BSWAP_32(drrb->drr_flags); 5292eda14cbcSMatt Macy drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 5293eda14cbcSMatt Macy drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 5294eda14cbcSMatt Macy } 5295eda14cbcSMatt Macy 5296eda14cbcSMatt Macy if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { 5297eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 5298eda14cbcSMatt Macy "stream (bad magic number)")); 5299eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 5300eda14cbcSMatt Macy } 5301eda14cbcSMatt Macy 5302eda14cbcSMatt Macy featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 5303eda14cbcSMatt Macy hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo); 5304eda14cbcSMatt Macy 5305eda14cbcSMatt Macy if (!DMU_STREAM_SUPPORTED(featureflags) || 5306eda14cbcSMatt Macy (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) { 530781b22a98SMartin Matuska /* 530881b22a98SMartin Matuska * Let's be explicit about this one, since rather than 530981b22a98SMartin Matuska * being a new feature we can't know, it's an old 531081b22a98SMartin Matuska * feature we dropped. 531181b22a98SMartin Matuska */ 531281b22a98SMartin Matuska if (featureflags & DMU_BACKUP_FEATURE_DEDUP) { 5313eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 531481b22a98SMartin Matuska "stream has deprecated feature: dedup, try " 531581b22a98SMartin Matuska "'zstream redup [send in a file] | zfs recv " 531681b22a98SMartin Matuska "[...]'")); 531781b22a98SMartin Matuska } else { 531881b22a98SMartin Matuska zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 531981b22a98SMartin Matuska "stream has unsupported feature, feature flags = " 532081b22a98SMartin Matuska "%llx (unknown flags = %llx)"), 532181b22a98SMartin Matuska (u_longlong_t)featureflags, 532281b22a98SMartin Matuska (u_longlong_t)((featureflags) & 532381b22a98SMartin Matuska ~DMU_BACKUP_FEATURE_MASK)); 532481b22a98SMartin Matuska } 5325eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 5326eda14cbcSMatt Macy } 5327eda14cbcSMatt Macy 5328eda14cbcSMatt Macy /* Holds feature is set once in the compound stream header. */ 5329eda14cbcSMatt Macy if (featureflags & DMU_BACKUP_FEATURE_HOLDS) 5330eda14cbcSMatt Macy flags->holds = B_TRUE; 5331eda14cbcSMatt Macy 5332eda14cbcSMatt Macy if (strchr(drrb->drr_toname, '@') == NULL) { 5333eda14cbcSMatt Macy zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 5334eda14cbcSMatt Macy "stream (bad snapshot name)")); 5335eda14cbcSMatt Macy return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 5336eda14cbcSMatt Macy } 5337eda14cbcSMatt Macy 5338eda14cbcSMatt Macy if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) { 5339eda14cbcSMatt Macy char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN]; 5340eda14cbcSMatt Macy if (sendfs == NULL) { 5341eda14cbcSMatt Macy /* 5342eda14cbcSMatt Macy * We were not called from zfs_receive_package(). Get 5343eda14cbcSMatt Macy * the fs specified by 'zfs send'. 5344eda14cbcSMatt Macy */ 5345eda14cbcSMatt Macy char *cp; 5346eda14cbcSMatt Macy (void) strlcpy(nonpackage_sendfs, 5347eda14cbcSMatt Macy drr.drr_u.drr_begin.drr_toname, 5348eda14cbcSMatt Macy sizeof (nonpackage_sendfs)); 5349eda14cbcSMatt Macy if ((cp = strchr(nonpackage_sendfs, '@')) != NULL) 5350eda14cbcSMatt Macy *cp = '\0'; 5351eda14cbcSMatt Macy sendfs = nonpackage_sendfs; 5352eda14cbcSMatt Macy VERIFY(finalsnap == NULL); 5353eda14cbcSMatt Macy } 5354eda14cbcSMatt Macy return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags, 5355eda14cbcSMatt Macy &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs, 5356eda14cbcSMatt Macy finalsnap, cmdprops)); 5357eda14cbcSMatt Macy } else { 5358eda14cbcSMatt Macy assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == 5359eda14cbcSMatt Macy DMU_COMPOUNDSTREAM); 5360eda14cbcSMatt Macy return (zfs_receive_package(hdl, infd, tosnap, flags, &drr, 5361eda14cbcSMatt Macy &zcksum, top_zfs, cmdprops)); 5362eda14cbcSMatt Macy } 5363eda14cbcSMatt Macy } 5364eda14cbcSMatt Macy 5365eda14cbcSMatt Macy /* 5366eda14cbcSMatt Macy * Restores a backup of tosnap from the file descriptor specified by infd. 5367eda14cbcSMatt Macy * Return 0 on total success, -2 if some things couldn't be 5368eda14cbcSMatt Macy * destroyed/renamed/promoted, -1 if some things couldn't be received. 5369eda14cbcSMatt Macy * (-1 will override -2, if -1 and the resumable flag was specified the 5370eda14cbcSMatt Macy * transfer can be resumed if the sending side supports it). 5371eda14cbcSMatt Macy */ 5372eda14cbcSMatt Macy int 5373eda14cbcSMatt Macy zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props, 5374eda14cbcSMatt Macy recvflags_t *flags, int infd, avl_tree_t *stream_avl) 5375eda14cbcSMatt Macy { 5376eda14cbcSMatt Macy char *top_zfs = NULL; 5377eda14cbcSMatt Macy int err; 5378eda14cbcSMatt Macy struct stat sb; 5379eda14cbcSMatt Macy char *originsnap = NULL; 5380eda14cbcSMatt Macy 5381eda14cbcSMatt Macy /* 5382eda14cbcSMatt Macy * The only way fstat can fail is if we do not have a valid file 5383eda14cbcSMatt Macy * descriptor. 5384eda14cbcSMatt Macy */ 5385eda14cbcSMatt Macy if (fstat(infd, &sb) == -1) { 5386eda14cbcSMatt Macy perror("fstat"); 5387eda14cbcSMatt Macy return (-2); 5388eda14cbcSMatt Macy } 5389eda14cbcSMatt Macy 5390eda14cbcSMatt Macy if (props) { 5391eda14cbcSMatt Macy err = nvlist_lookup_string(props, "origin", &originsnap); 5392eda14cbcSMatt Macy if (err && err != ENOENT) 5393eda14cbcSMatt Macy return (err); 5394eda14cbcSMatt Macy } 5395eda14cbcSMatt Macy 5396eda14cbcSMatt Macy err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL, 5397eda14cbcSMatt Macy stream_avl, &top_zfs, NULL, props); 5398eda14cbcSMatt Macy 5399eda14cbcSMatt Macy if (err == 0 && !flags->nomount && flags->domount && top_zfs) { 5400eda14cbcSMatt Macy zfs_handle_t *zhp = NULL; 5401eda14cbcSMatt Macy prop_changelist_t *clp = NULL; 5402eda14cbcSMatt Macy 5403eda14cbcSMatt Macy zhp = zfs_open(hdl, top_zfs, 5404eda14cbcSMatt Macy ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 5405eda14cbcSMatt Macy if (zhp == NULL) { 5406eda14cbcSMatt Macy err = -1; 5407eda14cbcSMatt Macy goto out; 5408eda14cbcSMatt Macy } else { 5409eda14cbcSMatt Macy if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 5410eda14cbcSMatt Macy zfs_close(zhp); 5411eda14cbcSMatt Macy goto out; 5412eda14cbcSMatt Macy } 5413eda14cbcSMatt Macy 5414eda14cbcSMatt Macy clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 5415eda14cbcSMatt Macy CL_GATHER_MOUNT_ALWAYS, 5416eda14cbcSMatt Macy flags->forceunmount ? MS_FORCE : 0); 5417eda14cbcSMatt Macy zfs_close(zhp); 5418eda14cbcSMatt Macy if (clp == NULL) { 5419eda14cbcSMatt Macy err = -1; 5420eda14cbcSMatt Macy goto out; 5421eda14cbcSMatt Macy } 5422eda14cbcSMatt Macy 5423eda14cbcSMatt Macy /* mount and share received datasets */ 5424eda14cbcSMatt Macy err = changelist_postfix(clp); 5425eda14cbcSMatt Macy changelist_free(clp); 5426eda14cbcSMatt Macy if (err != 0) 5427eda14cbcSMatt Macy err = -1; 5428eda14cbcSMatt Macy } 5429eda14cbcSMatt Macy } 5430eda14cbcSMatt Macy 5431eda14cbcSMatt Macy out: 5432eda14cbcSMatt Macy if (top_zfs) 5433eda14cbcSMatt Macy free(top_zfs); 5434eda14cbcSMatt Macy 5435eda14cbcSMatt Macy return (err); 5436eda14cbcSMatt Macy } 5437