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