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