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