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