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