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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <assert.h> 30 #include <ctype.h> 31 #include <errno.h> 32 #include <libdevinfo.h> 33 #include <libintl.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <strings.h> 37 #include <unistd.h> 38 #include <stddef.h> 39 #include <fcntl.h> 40 #include <sys/mount.h> 41 #include <sys/mntent.h> 42 #include <sys/mnttab.h> 43 #include <sys/avl.h> 44 #include <stddef.h> 45 46 #include <libzfs.h> 47 48 #include "zfs_namecheck.h" 49 #include "zfs_prop.h" 50 #include "libzfs_impl.h" 51 52 #include <fletcher.c> /* XXX */ 53 54 /* 55 * Routines for dealing with the AVL tree of fs-nvlists 56 */ 57 typedef struct fsavl_node { 58 avl_node_t fn_node; 59 nvlist_t *fn_nvfs; 60 char *fn_snapname; 61 uint64_t fn_guid; 62 } fsavl_node_t; 63 64 static int 65 fsavl_compare(const void *arg1, const void *arg2) 66 { 67 const fsavl_node_t *fn1 = arg1; 68 const fsavl_node_t *fn2 = arg2; 69 70 if (fn1->fn_guid > fn2->fn_guid) 71 return (+1); 72 else if (fn1->fn_guid < fn2->fn_guid) 73 return (-1); 74 else 75 return (0); 76 } 77 78 /* 79 * Given the GUID of a snapshot, find its containing filesystem and 80 * (optionally) name. 81 */ 82 static nvlist_t * 83 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname) 84 { 85 fsavl_node_t fn_find; 86 fsavl_node_t *fn; 87 88 fn_find.fn_guid = snapguid; 89 90 fn = avl_find(avl, &fn_find, NULL); 91 if (fn) { 92 if (snapname) 93 *snapname = fn->fn_snapname; 94 return (fn->fn_nvfs); 95 } 96 return (NULL); 97 } 98 99 static void 100 fsavl_destroy(avl_tree_t *avl) 101 { 102 fsavl_node_t *fn; 103 void *cookie; 104 105 if (avl == NULL) 106 return; 107 108 cookie = NULL; 109 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL) 110 free(fn); 111 avl_destroy(avl); 112 free(avl); 113 } 114 115 static avl_tree_t * 116 fsavl_create(nvlist_t *fss) 117 { 118 avl_tree_t *fsavl; 119 nvpair_t *fselem = NULL; 120 121 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL) 122 return (NULL); 123 124 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t), 125 offsetof(fsavl_node_t, fn_node)); 126 127 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) { 128 nvlist_t *nvfs, *snaps; 129 nvpair_t *snapelem = NULL; 130 131 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); 132 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); 133 134 while ((snapelem = 135 nvlist_next_nvpair(snaps, snapelem)) != NULL) { 136 fsavl_node_t *fn; 137 uint64_t guid; 138 139 VERIFY(0 == nvpair_value_uint64(snapelem, &guid)); 140 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) { 141 fsavl_destroy(fsavl); 142 return (NULL); 143 } 144 fn->fn_nvfs = nvfs; 145 fn->fn_snapname = nvpair_name(snapelem); 146 fn->fn_guid = guid; 147 148 /* 149 * Note: if there are multiple snaps with the 150 * same GUID, we ignore all but one. 151 */ 152 if (avl_find(fsavl, fn, NULL) == NULL) 153 avl_add(fsavl, fn); 154 else 155 free(fn); 156 } 157 } 158 159 return (fsavl); 160 } 161 162 /* 163 * Routines for dealing with the giant nvlist of fs-nvlists, etc. 164 */ 165 typedef struct send_data { 166 uint64_t parent_fromsnap_guid; 167 nvlist_t *parent_snaps; 168 nvlist_t *fss; 169 const char *fromsnap; 170 const char *tosnap; 171 172 /* 173 * The header nvlist is of the following format: 174 * { 175 * "tosnap" -> string 176 * "fromsnap" -> string (if incremental) 177 * "fss" -> { 178 * id -> { 179 * 180 * "name" -> string (full name; for debugging) 181 * "parentfromsnap" -> number (guid of fromsnap in parent) 182 * 183 * "props" -> { name -> value (only if set here) } 184 * "snaps" -> { name (lastname) -> number (guid) } 185 * 186 * "origin" -> number (guid) (if clone) 187 * "sent" -> boolean (not on-disk) 188 * } 189 * } 190 * } 191 * 192 */ 193 } send_data_t; 194 195 static int 196 send_iterate_snap(zfs_handle_t *zhp, void *arg) 197 { 198 send_data_t *sd = arg; 199 uint64_t guid = zhp->zfs_dmustats.dds_guid; 200 char *snapname; 201 202 snapname = strrchr(zhp->zfs_name, '@')+1; 203 204 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid)); 205 /* 206 * NB: if there is no fromsnap here (it's a newly created fs in 207 * an incremental replication), we will substitute the tosnap. 208 */ 209 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) || 210 (sd->parent_fromsnap_guid == 0 && sd->tosnap && 211 strcmp(snapname, sd->tosnap) == 0)) { 212 sd->parent_fromsnap_guid = guid; 213 } 214 215 zfs_close(zhp); 216 return (0); 217 } 218 219 static void 220 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv) 221 { 222 nvpair_t *elem = NULL; 223 224 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) { 225 char *propname = nvpair_name(elem); 226 zfs_prop_t prop = zfs_name_to_prop(propname); 227 nvlist_t *propnv; 228 229 if (!zfs_prop_user(propname) && zfs_prop_readonly(prop)) 230 continue; 231 232 verify(nvpair_value_nvlist(elem, &propnv) == 0); 233 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION) { 234 /* these guys are modifyable, but have no source */ 235 uint64_t value; 236 verify(nvlist_lookup_uint64(propnv, 237 ZPROP_VALUE, &value) == 0); 238 } else { 239 char *source; 240 if (nvlist_lookup_string(propnv, 241 ZPROP_SOURCE, &source) != 0) 242 continue; 243 if (strcmp(source, zhp->zfs_name) != 0) 244 continue; 245 } 246 247 if (zfs_prop_user(propname) || 248 zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 249 char *value; 250 verify(nvlist_lookup_string(propnv, 251 ZPROP_VALUE, &value) == 0); 252 VERIFY(0 == nvlist_add_string(nv, propname, value)); 253 } else { 254 uint64_t value; 255 verify(nvlist_lookup_uint64(propnv, 256 ZPROP_VALUE, &value) == 0); 257 VERIFY(0 == nvlist_add_uint64(nv, propname, value)); 258 } 259 } 260 } 261 262 static int 263 send_iterate_fs(zfs_handle_t *zhp, void *arg) 264 { 265 send_data_t *sd = arg; 266 nvlist_t *nvfs, *nv; 267 int rv; 268 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid; 269 uint64_t guid = zhp->zfs_dmustats.dds_guid; 270 char guidstring[64]; 271 272 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0)); 273 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name)); 274 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap", 275 sd->parent_fromsnap_guid)); 276 277 if (zhp->zfs_dmustats.dds_origin[0]) { 278 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl, 279 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 280 if (origin == NULL) 281 return (-1); 282 VERIFY(0 == nvlist_add_uint64(nvfs, "origin", 283 origin->zfs_dmustats.dds_guid)); 284 } 285 286 /* iterate over props */ 287 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); 288 send_iterate_prop(zhp, nv); 289 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv)); 290 nvlist_free(nv); 291 292 /* iterate over snaps, and set sd->parent_fromsnap_guid */ 293 sd->parent_fromsnap_guid = 0; 294 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0)); 295 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd); 296 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps)); 297 nvlist_free(sd->parent_snaps); 298 299 /* add this fs to nvlist */ 300 (void) snprintf(guidstring, sizeof (guidstring), 301 "0x%llx", (longlong_t)guid); 302 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs)); 303 nvlist_free(nvfs); 304 305 /* iterate over children */ 306 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd); 307 308 sd->parent_fromsnap_guid = parent_fromsnap_guid_save; 309 310 zfs_close(zhp); 311 return (rv); 312 } 313 314 static int 315 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap, 316 const char *tosnap, nvlist_t **nvlp, avl_tree_t **avlp) 317 { 318 zfs_handle_t *zhp; 319 send_data_t sd = { 0 }; 320 int error; 321 322 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 323 if (zhp == NULL) 324 return (EZFS_BADTYPE); 325 326 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0)); 327 sd.fromsnap = fromsnap; 328 sd.tosnap = tosnap; 329 330 if ((error = send_iterate_fs(zhp, &sd)) != 0) { 331 nvlist_free(sd.fss); 332 if (avlp != NULL) 333 *avlp = NULL; 334 *nvlp = NULL; 335 return (error); 336 } 337 338 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) { 339 nvlist_free(sd.fss); 340 *nvlp = NULL; 341 return (EZFS_NOMEM); 342 } 343 344 *nvlp = sd.fss; 345 return (0); 346 } 347 348 /* 349 * Routines for dealing with the sorted snapshot functionality 350 */ 351 typedef struct zfs_node { 352 zfs_handle_t *zn_handle; 353 avl_node_t zn_avlnode; 354 } zfs_node_t; 355 356 static int 357 zfs_sort_snaps(zfs_handle_t *zhp, void *data) 358 { 359 avl_tree_t *avl = data; 360 zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t)); 361 362 node->zn_handle = zhp; 363 avl_add(avl, node); 364 return (0); 365 } 366 367 /* ARGSUSED */ 368 static int 369 zfs_snapshot_compare(const void *larg, const void *rarg) 370 { 371 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 372 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 373 uint64_t lcreate, rcreate; 374 375 /* 376 * Sort them according to creation time. We use the hidden 377 * CREATETXG property to get an absolute ordering of snapshots. 378 */ 379 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 380 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 381 382 if (lcreate < rcreate) 383 return (-1); 384 else if (lcreate > rcreate) 385 return (+1); 386 else 387 return (0); 388 } 389 390 static int 391 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data) 392 { 393 int ret = 0; 394 zfs_node_t *node; 395 avl_tree_t avl; 396 void *cookie = NULL; 397 398 avl_create(&avl, zfs_snapshot_compare, 399 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode)); 400 401 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl); 402 403 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node)) 404 ret |= callback(node->zn_handle, data); 405 406 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL) 407 free(node); 408 409 avl_destroy(&avl); 410 411 return (ret); 412 } 413 414 /* 415 * Routines specific to "zfs send" 416 */ 417 typedef struct send_dump_data { 418 /* these are all just the short snapname (the part after the @) */ 419 const char *fromsnap; 420 const char *tosnap; 421 char lastsnap[ZFS_MAXNAMELEN]; 422 boolean_t seenfrom, seento, replicate, doall, fromorigin; 423 boolean_t verbose; 424 int outfd; 425 boolean_t err; 426 nvlist_t *fss; 427 avl_tree_t *fsavl; 428 } send_dump_data_t; 429 430 /* 431 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 432 * NULL) to the file descriptor specified by outfd. 433 */ 434 static int 435 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin, 436 int outfd) 437 { 438 zfs_cmd_t zc = { 0 }; 439 libzfs_handle_t *hdl = zhp->zfs_hdl; 440 441 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 442 assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin); 443 444 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 445 if (fromsnap) 446 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value)); 447 zc.zc_cookie = outfd; 448 zc.zc_obj = fromorigin; 449 450 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) { 451 char errbuf[1024]; 452 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 453 "warning: cannot send '%s'"), zhp->zfs_name); 454 455 switch (errno) { 456 457 case EXDEV: 458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 459 "not an earlier snapshot from the same fs")); 460 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 461 462 case ENOENT: 463 if (zfs_dataset_exists(hdl, zc.zc_name, 464 ZFS_TYPE_SNAPSHOT)) { 465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 466 "incremental source (@%s) does not exist"), 467 zc.zc_value); 468 } 469 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 470 471 case EDQUOT: 472 case EFBIG: 473 case EIO: 474 case ENOLINK: 475 case ENOSPC: 476 case ENOSTR: 477 case ENXIO: 478 case EPIPE: 479 case ERANGE: 480 case EFAULT: 481 case EROFS: 482 zfs_error_aux(hdl, strerror(errno)); 483 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 484 485 default: 486 return (zfs_standard_error(hdl, errno, errbuf)); 487 } 488 } 489 490 return (0); 491 } 492 493 static int 494 dump_snapshot(zfs_handle_t *zhp, void *arg) 495 { 496 send_dump_data_t *sdd = arg; 497 const char *thissnap; 498 int err; 499 500 thissnap = strchr(zhp->zfs_name, '@') + 1; 501 502 if (sdd->fromsnap && !sdd->seenfrom && 503 strcmp(sdd->fromsnap, thissnap) == 0) { 504 sdd->seenfrom = B_TRUE; 505 (void) strcpy(sdd->lastsnap, thissnap); 506 zfs_close(zhp); 507 return (0); 508 } 509 510 if (sdd->seento || !sdd->seenfrom) { 511 zfs_close(zhp); 512 return (0); 513 } 514 515 /* send it */ 516 if (sdd->verbose) { 517 (void) fprintf(stderr, "sending from @%s to %s\n", 518 sdd->lastsnap, zhp->zfs_name); 519 } 520 521 err = dump_ioctl(zhp, sdd->lastsnap, 522 sdd->lastsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate), 523 sdd->outfd); 524 525 if (!sdd->seento && strcmp(sdd->tosnap, thissnap) == 0) 526 sdd->seento = B_TRUE; 527 528 (void) strcpy(sdd->lastsnap, thissnap); 529 zfs_close(zhp); 530 return (err); 531 } 532 533 static int 534 dump_filesystem(zfs_handle_t *zhp, void *arg) 535 { 536 int rv = 0; 537 send_dump_data_t *sdd = arg; 538 boolean_t missingfrom = B_FALSE; 539 zfs_cmd_t zc = { 0 }; 540 541 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 542 zhp->zfs_name, sdd->tosnap); 543 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 544 (void) fprintf(stderr, "WARNING: " 545 "could not send %s@%s: does not exist\n", 546 zhp->zfs_name, sdd->tosnap); 547 sdd->err = B_TRUE; 548 return (0); 549 } 550 551 if (sdd->replicate && sdd->fromsnap) { 552 /* 553 * If this fs does not have fromsnap, and we're doing 554 * recursive, we need to send a full stream from the 555 * beginning (or an incremental from the origin if this 556 * is a clone). If we're doing non-recursive, then let 557 * them get the error. 558 */ 559 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 560 zhp->zfs_name, sdd->fromsnap); 561 if (ioctl(zhp->zfs_hdl->libzfs_fd, 562 ZFS_IOC_OBJSET_STATS, &zc) != 0) { 563 missingfrom = B_TRUE; 564 } 565 } 566 567 if (sdd->doall) { 568 sdd->seenfrom = sdd->seento = sdd->lastsnap[0] = 0; 569 if (sdd->fromsnap == NULL || missingfrom) 570 sdd->seenfrom = B_TRUE; 571 572 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg); 573 if (!sdd->seenfrom) { 574 (void) fprintf(stderr, 575 "WARNING: could not send %s@%s:\n" 576 "incremental source (%s@%s) does not exist\n", 577 zhp->zfs_name, sdd->tosnap, 578 zhp->zfs_name, sdd->fromsnap); 579 sdd->err = B_TRUE; 580 } else if (!sdd->seento) { 581 (void) fprintf(stderr, 582 "WARNING: could not send %s@%s:\n" 583 "incremental source (%s@%s) " 584 "is not earlier than it\n", 585 zhp->zfs_name, sdd->tosnap, 586 zhp->zfs_name, sdd->fromsnap); 587 sdd->err = B_TRUE; 588 } 589 } else { 590 zfs_handle_t *snapzhp; 591 char snapname[ZFS_MAXNAMELEN]; 592 593 (void) snprintf(snapname, sizeof (snapname), "%s@%s", 594 zfs_get_name(zhp), sdd->tosnap); 595 snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT); 596 rv = dump_ioctl(snapzhp, 597 missingfrom ? NULL : sdd->fromsnap, 598 sdd->fromorigin || missingfrom, 599 sdd->outfd); 600 sdd->seento = B_TRUE; 601 zfs_close(snapzhp); 602 } 603 604 return (rv); 605 } 606 607 static int 608 dump_filesystems(zfs_handle_t *rzhp, void *arg) 609 { 610 send_dump_data_t *sdd = arg; 611 nvpair_t *fspair; 612 boolean_t needagain, progress; 613 614 if (!sdd->replicate) 615 return (dump_filesystem(rzhp, sdd)); 616 617 again: 618 needagain = progress = B_FALSE; 619 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 620 fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 621 nvlist_t *fslist; 622 char *fsname; 623 zfs_handle_t *zhp; 624 int err; 625 uint64_t origin_guid = 0; 626 nvlist_t *origin_nv; 627 628 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); 629 if (nvlist_lookup_boolean(fslist, "sent") == 0) 630 continue; 631 632 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0); 633 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); 634 635 origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL); 636 if (origin_nv && 637 nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) { 638 /* 639 * origin has not been sent yet; 640 * skip this clone. 641 */ 642 needagain = B_TRUE; 643 continue; 644 } 645 646 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); 647 err = dump_filesystem(zhp, sdd); 648 VERIFY(nvlist_add_boolean(fslist, "sent") == 0); 649 progress = B_TRUE; 650 zfs_close(zhp); 651 if (err) 652 return (err); 653 } 654 if (needagain) { 655 assert(progress); 656 goto again; 657 } 658 return (0); 659 } 660 661 /* 662 * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL. 663 * If 'doall', dump all intermediate snaps. 664 * If 'replicate', dump special header and do recursively. 665 */ 666 int 667 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 668 boolean_t replicate, boolean_t doall, boolean_t fromorigin, 669 boolean_t verbose, int outfd) 670 { 671 char errbuf[1024]; 672 send_dump_data_t sdd = { 0 }; 673 int err; 674 nvlist_t *fss = NULL; 675 avl_tree_t *fsavl = NULL; 676 677 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 678 "cannot send '%s'"), zhp->zfs_name); 679 680 if (fromsnap && fromsnap[0] == '\0') { 681 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 682 "zero-length incremental source")); 683 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 684 } 685 686 if (replicate || doall) { 687 dmu_replay_record_t drr = { 0 }; 688 char *packbuf = NULL; 689 size_t buflen = 0; 690 zio_cksum_t zc = { 0 }; 691 692 assert(fromsnap || doall); 693 694 if (replicate) { 695 nvlist_t *hdrnv; 696 697 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0)); 698 if (fromsnap) { 699 VERIFY(0 == nvlist_add_string(hdrnv, 700 "fromsnap", fromsnap)); 701 } 702 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap)); 703 704 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name, 705 fromsnap, tosnap, &fss, &fsavl); 706 if (err) 707 return (err); 708 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss)); 709 err = nvlist_pack(hdrnv, &packbuf, &buflen, 710 NV_ENCODE_XDR, 0); 711 nvlist_free(hdrnv); 712 if (err) { 713 fsavl_destroy(fsavl); 714 nvlist_free(fss); 715 return (zfs_standard_error(zhp->zfs_hdl, 716 err, errbuf)); 717 } 718 } 719 720 /* write first begin record */ 721 drr.drr_type = DRR_BEGIN; 722 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 723 drr.drr_u.drr_begin.drr_version = DMU_BACKUP_HEADER_VERSION; 724 (void) snprintf(drr.drr_u.drr_begin.drr_toname, 725 sizeof (drr.drr_u.drr_begin.drr_toname), 726 "%s@%s", zhp->zfs_name, tosnap); 727 drr.drr_payloadlen = buflen; 728 fletcher_4_incremental_native(&drr, sizeof (drr), &zc); 729 err = write(outfd, &drr, sizeof (drr)); 730 731 /* write header nvlist */ 732 if (err != -1) { 733 fletcher_4_incremental_native(packbuf, buflen, &zc); 734 err = write(outfd, packbuf, buflen); 735 } 736 free(packbuf); 737 if (err == -1) { 738 fsavl_destroy(fsavl); 739 nvlist_free(fss); 740 return (zfs_standard_error(zhp->zfs_hdl, 741 errno, errbuf)); 742 } 743 744 /* write end record */ 745 if (err != -1) { 746 bzero(&drr, sizeof (drr)); 747 drr.drr_type = DRR_END; 748 drr.drr_u.drr_end.drr_checksum = zc; 749 err = write(outfd, &drr, sizeof (drr)); 750 if (err == -1) { 751 fsavl_destroy(fsavl); 752 nvlist_free(fss); 753 return (zfs_standard_error(zhp->zfs_hdl, 754 errno, errbuf)); 755 } 756 } 757 } 758 759 /* dump each stream */ 760 sdd.fromsnap = fromsnap; 761 sdd.tosnap = tosnap; 762 sdd.outfd = outfd; 763 sdd.replicate = replicate; 764 sdd.doall = doall; 765 sdd.fromorigin = fromorigin; 766 sdd.fss = fss; 767 sdd.fsavl = fsavl; 768 sdd.verbose = verbose; 769 err = dump_filesystems(zhp, &sdd); 770 fsavl_destroy(fsavl); 771 nvlist_free(fss); 772 773 if (replicate || doall) { 774 /* 775 * write final end record. NB: want to do this even if 776 * there was some error, because it might not be totally 777 * failed. 778 */ 779 dmu_replay_record_t drr = { 0 }; 780 drr.drr_type = DRR_END; 781 if (write(outfd, &drr, sizeof (drr)) == -1) { 782 return (zfs_standard_error(zhp->zfs_hdl, 783 errno, errbuf)); 784 } 785 } 786 787 return (err || sdd.err); 788 } 789 790 /* 791 * Routines specific to "zfs recv" 792 */ 793 794 static int 795 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, 796 boolean_t byteswap, zio_cksum_t *zc) 797 { 798 char *cp = buf; 799 int rv; 800 int len = ilen; 801 802 do { 803 rv = read(fd, cp, len); 804 cp += rv; 805 len -= rv; 806 } while (rv > 0); 807 808 if (rv < 0 || len != 0) { 809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 810 "failed to read from stream")); 811 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, 812 "cannot receive"))); 813 } 814 815 if (zc) { 816 if (byteswap) 817 fletcher_4_incremental_byteswap(buf, ilen, zc); 818 else 819 fletcher_4_incremental_native(buf, ilen, zc); 820 } 821 return (0); 822 } 823 824 static int 825 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, 826 boolean_t byteswap, zio_cksum_t *zc) 827 { 828 char *buf; 829 int err; 830 831 buf = zfs_alloc(hdl, len); 832 if (buf == NULL) 833 return (ENOMEM); 834 835 err = recv_read(hdl, fd, buf, len, byteswap, zc); 836 if (err != 0) { 837 free(buf); 838 return (err); 839 } 840 841 err = nvlist_unpack(buf, len, nvp, 0); 842 free(buf); 843 if (err != 0) { 844 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 845 "stream (malformed nvlist)")); 846 return (EINVAL); 847 } 848 return (0); 849 } 850 851 static int 852 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, 853 int baselen, char *newname, recvflags_t flags) 854 { 855 static int seq; 856 zfs_cmd_t zc = { 0 }; 857 int err; 858 prop_changelist_t *clp; 859 zfs_handle_t *zhp; 860 861 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 862 if (zhp == NULL) 863 return (-1); 864 clp = changelist_gather(zhp, ZFS_PROP_NAME, flags.force ? MS_FORCE : 0); 865 zfs_close(zhp); 866 if (clp == NULL) 867 return (-1); 868 err = changelist_prefix(clp); 869 if (err) 870 return (err); 871 872 if (tryname) { 873 (void) strcpy(newname, tryname); 874 875 zc.zc_objset_type = DMU_OST_ZFS; 876 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); 877 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value)); 878 879 if (flags.verbose) { 880 (void) printf("attempting rename %s to %s\n", 881 zc.zc_name, zc.zc_value); 882 } 883 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); 884 if (err == 0) 885 changelist_rename(clp, name, tryname); 886 } else { 887 err = ENOENT; 888 } 889 890 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) { 891 seq++; 892 893 (void) strncpy(newname, name, baselen); 894 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen, 895 "recv-%u-%u", getpid(), seq); 896 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value)); 897 898 if (flags.verbose) { 899 (void) printf("failed - trying rename %s to %s\n", 900 zc.zc_name, zc.zc_value); 901 } 902 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); 903 if (err == 0) 904 changelist_rename(clp, name, newname); 905 if (err && flags.verbose) { 906 (void) printf("failed (%u) - " 907 "will try again on next pass\n", errno); 908 } 909 err = EAGAIN; 910 } else if (flags.verbose) { 911 if (err == 0) 912 (void) printf("success\n"); 913 else 914 (void) printf("failed (%u)\n", errno); 915 } 916 917 (void) changelist_postfix(clp); 918 changelist_free(clp); 919 920 return (err); 921 } 922 923 static int 924 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, 925 char *newname, recvflags_t flags) 926 { 927 zfs_cmd_t zc = { 0 }; 928 int err = 0; 929 prop_changelist_t *clp; 930 zfs_handle_t *zhp; 931 932 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 933 if (zhp == NULL) 934 return (-1); 935 clp = changelist_gather(zhp, ZFS_PROP_NAME, flags.force ? MS_FORCE : 0); 936 zfs_close(zhp); 937 if (clp == NULL) 938 return (-1); 939 err = changelist_prefix(clp); 940 if (err) 941 return (err); 942 943 zc.zc_objset_type = DMU_OST_ZFS; 944 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); 945 946 if (flags.verbose) 947 (void) printf("attempting destroy %s\n", zc.zc_name); 948 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc); 949 950 if (err == 0) { 951 if (flags.verbose) 952 (void) printf("success\n"); 953 changelist_remove(clp, zc.zc_name); 954 } 955 956 (void) changelist_postfix(clp); 957 changelist_free(clp); 958 959 if (err != 0) 960 err = recv_rename(hdl, name, NULL, baselen, newname, flags); 961 962 return (err); 963 } 964 965 typedef struct guid_to_name_data { 966 uint64_t guid; 967 char *name; 968 } guid_to_name_data_t; 969 970 static int 971 guid_to_name_cb(zfs_handle_t *zhp, void *arg) 972 { 973 guid_to_name_data_t *gtnd = arg; 974 int err; 975 976 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) { 977 (void) strcpy(gtnd->name, zhp->zfs_name); 978 return (EEXIST); 979 } 980 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); 981 zfs_close(zhp); 982 return (err); 983 } 984 985 static int 986 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, 987 char *name) 988 { 989 /* exhaustive search all local snapshots */ 990 guid_to_name_data_t gtnd; 991 int err = 0; 992 zfs_handle_t *zhp; 993 char *cp; 994 995 gtnd.guid = guid; 996 gtnd.name = name; 997 998 if (strchr(parent, '@') == NULL) { 999 zhp = make_dataset_handle(hdl, parent); 1000 if (zhp != NULL) { 1001 err = zfs_iter_children(zhp, guid_to_name_cb, >nd); 1002 zfs_close(zhp); 1003 if (err == EEXIST) 1004 return (0); 1005 } 1006 } 1007 1008 cp = strchr(parent, '/'); 1009 if (cp) 1010 *cp = '\0'; 1011 zhp = make_dataset_handle(hdl, parent); 1012 if (cp) 1013 *cp = '/'; 1014 1015 if (zhp) { 1016 err = zfs_iter_children(zhp, guid_to_name_cb, >nd); 1017 zfs_close(zhp); 1018 } 1019 1020 return (err == EEXIST ? 0 : ENOENT); 1021 1022 } 1023 1024 /* 1025 * Return true if dataset guid1 is created before guid2. 1026 */ 1027 static boolean_t 1028 created_before(libzfs_handle_t *hdl, avl_tree_t *avl, 1029 uint64_t guid1, uint64_t guid2) 1030 { 1031 nvlist_t *nvfs; 1032 char *fsname, *snapname; 1033 char buf[ZFS_MAXNAMELEN]; 1034 boolean_t rv; 1035 zfs_node_t zn1, zn2; 1036 1037 if (guid2 == 0) 1038 return (B_FALSE); 1039 if (guid1 == 0) 1040 return (B_TRUE); 1041 1042 nvfs = fsavl_find(avl, guid1, &snapname); 1043 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 1044 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 1045 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 1046 1047 nvfs = fsavl_find(avl, guid2, &snapname); 1048 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 1049 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 1050 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 1051 1052 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1); 1053 1054 zfs_close(zn1.zn_handle); 1055 zfs_close(zn2.zn_handle); 1056 1057 return (rv); 1058 } 1059 1060 static int 1061 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, 1062 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl) 1063 { 1064 nvlist_t *local_nv; 1065 avl_tree_t *local_avl; 1066 nvpair_t *fselem, *nextfselem; 1067 char *tosnap, *fromsnap; 1068 char newname[ZFS_MAXNAMELEN]; 1069 int error; 1070 boolean_t needagain, progress; 1071 1072 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap)); 1073 VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap)); 1074 1075 if (flags.dryrun) 1076 return (0); 1077 1078 again: 1079 needagain = progress = B_FALSE; 1080 1081 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, 1082 &local_nv, &local_avl)) != 0) 1083 return (error); 1084 1085 /* 1086 * Process deletes and renames 1087 */ 1088 for (fselem = nvlist_next_nvpair(local_nv, NULL); 1089 fselem; fselem = nextfselem) { 1090 nvlist_t *nvfs, *snaps; 1091 nvlist_t *stream_nvfs = NULL; 1092 nvpair_t *snapelem, *nextsnapelem; 1093 uint64_t fromguid = 0; 1094 uint64_t originguid = 0; 1095 uint64_t stream_originguid = 0; 1096 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; 1097 char *fsname, *stream_fsname; 1098 1099 nextfselem = nvlist_next_nvpair(local_nv, fselem); 1100 1101 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); 1102 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); 1103 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 1104 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap", 1105 &parent_fromsnap_guid)); 1106 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); 1107 1108 /* 1109 * First find the stream's fs, so we can check for 1110 * a different origin (due to "zfs promote") 1111 */ 1112 for (snapelem = nvlist_next_nvpair(snaps, NULL); 1113 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { 1114 uint64_t thisguid; 1115 1116 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); 1117 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); 1118 1119 if (stream_nvfs != NULL) 1120 break; 1121 } 1122 1123 /* check for promote */ 1124 (void) nvlist_lookup_uint64(stream_nvfs, "origin", 1125 &stream_originguid); 1126 if (stream_nvfs && originguid != stream_originguid) { 1127 if (created_before(hdl, local_avl, stream_originguid, 1128 originguid)) { 1129 /* promote it! */ 1130 zfs_cmd_t zc = { 0 }; 1131 nvlist_t *origin_nvfs; 1132 char *origin_fsname; 1133 1134 if (flags.verbose) 1135 (void) printf("promoting %s\n", fsname); 1136 1137 origin_nvfs = fsavl_find(local_avl, originguid, 1138 NULL); 1139 VERIFY(0 == nvlist_lookup_string(origin_nvfs, 1140 "name", &origin_fsname)); 1141 (void) strlcpy(zc.zc_value, origin_fsname, 1142 sizeof (zc.zc_value)); 1143 (void) strlcpy(zc.zc_name, fsname, 1144 sizeof (zc.zc_name)); 1145 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 1146 if (error == 0) 1147 progress = B_TRUE; 1148 } 1149 /* 1150 * We had/have the wrong origin, therefore our 1151 * list of snapshots is wrong. Need to handle 1152 * them on the next pass. 1153 */ 1154 needagain = B_TRUE; 1155 continue; 1156 } 1157 1158 for (snapelem = nvlist_next_nvpair(snaps, NULL); 1159 snapelem; snapelem = nextsnapelem) { 1160 uint64_t thisguid; 1161 char *stream_snapname; 1162 nvlist_t *found; 1163 1164 nextsnapelem = nvlist_next_nvpair(snaps, snapelem); 1165 1166 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); 1167 found = fsavl_find(stream_avl, thisguid, 1168 &stream_snapname); 1169 1170 /* check for delete */ 1171 if (found == NULL) { 1172 char name[ZFS_MAXNAMELEN]; 1173 1174 if (!flags.force) 1175 continue; 1176 1177 (void) snprintf(name, sizeof (name), "%s@%s", 1178 fsname, nvpair_name(snapelem)); 1179 1180 error = recv_destroy(hdl, name, 1181 strlen(fsname)+1, newname, flags); 1182 if (error) 1183 needagain = B_TRUE; 1184 else 1185 progress = B_TRUE; 1186 continue; 1187 } 1188 1189 stream_nvfs = found; 1190 1191 /* check for different snapname */ 1192 if (strcmp(nvpair_name(snapelem), 1193 stream_snapname) != 0) { 1194 char name[ZFS_MAXNAMELEN]; 1195 char tryname[ZFS_MAXNAMELEN]; 1196 1197 (void) snprintf(name, sizeof (name), "%s@%s", 1198 fsname, nvpair_name(snapelem)); 1199 (void) snprintf(tryname, sizeof (name), "%s@%s", 1200 fsname, stream_snapname); 1201 1202 error = recv_rename(hdl, name, tryname, 1203 strlen(fsname)+1, newname, flags); 1204 if (error) 1205 needagain = B_TRUE; 1206 else 1207 progress = B_TRUE; 1208 } 1209 1210 if (strcmp(stream_snapname, fromsnap) == 0) 1211 fromguid = thisguid; 1212 } 1213 1214 /* check for delete */ 1215 if (stream_nvfs == NULL) { 1216 if (!flags.force) 1217 continue; 1218 1219 error = recv_destroy(hdl, fsname, strlen(tofs)+1, 1220 newname, flags); 1221 if (error) 1222 needagain = B_TRUE; 1223 else 1224 progress = B_TRUE; 1225 continue; 1226 } 1227 1228 if (fromguid == 0 && flags.verbose) { 1229 (void) printf("local fs %s does not have fromsnap " 1230 "(%s in stream); must have been deleted locally; " 1231 "ignoring\n", fsname, fromsnap); 1232 continue; 1233 } 1234 1235 VERIFY(0 == nvlist_lookup_string(stream_nvfs, 1236 "name", &stream_fsname)); 1237 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs, 1238 "parentfromsnap", &stream_parent_fromsnap_guid)); 1239 1240 /* check for rename */ 1241 if ((stream_parent_fromsnap_guid != 0 && 1242 stream_parent_fromsnap_guid != parent_fromsnap_guid) || 1243 strcmp(strrchr(fsname, '/'), 1244 strrchr(stream_fsname, '/')) != 0) { 1245 nvlist_t *parent; 1246 char tryname[ZFS_MAXNAMELEN]; 1247 1248 parent = fsavl_find(local_avl, 1249 stream_parent_fromsnap_guid, NULL); 1250 /* 1251 * NB: parent might not be found if we used the 1252 * tosnap for stream_parent_fromsnap_guid, 1253 * because the parent is a newly-created fs; 1254 * we'll be able to rename it after we recv the 1255 * new fs. 1256 */ 1257 if (parent != NULL) { 1258 char *pname; 1259 1260 VERIFY(0 == nvlist_lookup_string(parent, "name", 1261 &pname)); 1262 (void) snprintf(tryname, sizeof (tryname), 1263 "%s%s", pname, strrchr(stream_fsname, '/')); 1264 } else { 1265 tryname[0] = '\0'; 1266 if (flags.verbose) { 1267 (void) printf("local fs %s new parent " 1268 "not found\n", fsname); 1269 } 1270 } 1271 1272 error = recv_rename(hdl, fsname, tryname, 1273 strlen(tofs)+1, newname, flags); 1274 if (error) 1275 needagain = B_TRUE; 1276 else 1277 progress = B_TRUE; 1278 } 1279 } 1280 1281 fsavl_destroy(local_avl); 1282 nvlist_free(local_nv); 1283 1284 if (needagain && progress) { 1285 /* do another pass to fix up temporary names */ 1286 if (flags.verbose) 1287 (void) printf("another pass:\n"); 1288 goto again; 1289 } 1290 1291 return (needagain); 1292 } 1293 1294 static int 1295 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, 1296 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc) 1297 { 1298 nvlist_t *stream_nv = NULL; 1299 avl_tree_t *stream_avl = NULL; 1300 char *fromsnap = NULL; 1301 char tofs[ZFS_MAXNAMELEN]; 1302 char errbuf[1024]; 1303 dmu_replay_record_t drre; 1304 int error; 1305 boolean_t anyerr = B_FALSE; 1306 1307 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1308 "cannot receive")); 1309 1310 if (strchr(destname, '@')) { 1311 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1312 "can not specify snapshot name for multi-snapshot stream")); 1313 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 1314 } 1315 1316 assert(drr->drr_type == DRR_BEGIN); 1317 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); 1318 assert(drr->drr_u.drr_begin.drr_version == DMU_BACKUP_HEADER_VERSION); 1319 1320 /* 1321 * Read in the nvlist from the stream. 1322 */ 1323 if (drr->drr_payloadlen != 0) { 1324 if (!flags.isprefix) { 1325 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1326 "must use -d to receive replication " 1327 "(send -R) stream")); 1328 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 1329 } 1330 1331 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, 1332 &stream_nv, flags.byteswap, zc); 1333 if (error) { 1334 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1335 goto out; 1336 } 1337 } 1338 1339 /* 1340 * Read in the end record and verify checksum. 1341 */ 1342 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), 1343 flags.byteswap, NULL))) 1344 goto out; 1345 if (flags.byteswap) { 1346 drre.drr_type = BSWAP_32(drre.drr_type); 1347 drre.drr_u.drr_end.drr_checksum.zc_word[0] = 1348 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); 1349 drre.drr_u.drr_end.drr_checksum.zc_word[1] = 1350 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); 1351 drre.drr_u.drr_end.drr_checksum.zc_word[2] = 1352 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); 1353 drre.drr_u.drr_end.drr_checksum.zc_word[3] = 1354 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); 1355 } 1356 if (drre.drr_type != DRR_END) { 1357 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1358 goto out; 1359 } 1360 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { 1361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1362 "incorrect header checksum")); 1363 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1364 goto out; 1365 } 1366 1367 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); 1368 1369 if (drr->drr_payloadlen != 0) { 1370 nvlist_t *stream_fss; 1371 1372 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", 1373 &stream_fss)); 1374 if ((stream_avl = fsavl_create(stream_fss)) == NULL) { 1375 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1376 "couldn't allocate avl tree")); 1377 error = zfs_error(hdl, EZFS_NOMEM, errbuf); 1378 goto out; 1379 } 1380 1381 if (fromsnap != NULL) { 1382 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN); 1383 if (flags.isprefix) { 1384 int i = strcspn(drr->drr_u.drr_begin.drr_toname, 1385 "/@"); 1386 /* zfs_receive_one() will create_parents() */ 1387 (void) strlcat(tofs, 1388 &drr->drr_u.drr_begin.drr_toname[i], 1389 ZFS_MAXNAMELEN); 1390 *strchr(tofs, '@') = '\0'; 1391 } 1392 anyerr |= recv_incremental_replication(hdl, tofs, 1393 flags, stream_nv, stream_avl); 1394 } 1395 } 1396 1397 1398 /* Finally, receive each contained stream */ 1399 do { 1400 /* 1401 * we should figure out if it has a recoverable 1402 * error, in which case do a recv_skip() and drive on. 1403 * Note, if we fail due to already having this guid, 1404 * zfs_receive_one() will take care of it (ie, 1405 * recv_skip() and return 0). 1406 */ 1407 error = zfs_receive(hdl, destname, flags, fd, stream_avl); 1408 if (error == ENODATA) { 1409 error = 0; 1410 break; 1411 } 1412 anyerr |= error; 1413 } while (error == 0); 1414 1415 if (drr->drr_payloadlen != 0 && fromsnap != NULL) { 1416 /* 1417 * Now that we have the fs's they sent us, try the 1418 * renames again. 1419 */ 1420 anyerr |= recv_incremental_replication(hdl, tofs, flags, 1421 stream_nv, stream_avl); 1422 } 1423 1424 out: 1425 fsavl_destroy(stream_avl); 1426 if (stream_nv) 1427 nvlist_free(stream_nv); 1428 if (anyerr) 1429 error = -1; 1430 return (error); 1431 } 1432 1433 static int 1434 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) 1435 { 1436 dmu_replay_record_t *drr; 1437 void *buf = malloc(1<<20); 1438 1439 /* XXX would be great to use lseek if possible... */ 1440 drr = buf; 1441 1442 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), 1443 byteswap, NULL) == 0) { 1444 if (byteswap) 1445 drr->drr_type = BSWAP_32(drr->drr_type); 1446 1447 switch (drr->drr_type) { 1448 case DRR_BEGIN: 1449 /* NB: not to be used on v2 stream packages */ 1450 assert(drr->drr_payloadlen == 0); 1451 break; 1452 1453 case DRR_END: 1454 free(buf); 1455 return (0); 1456 1457 case DRR_OBJECT: 1458 if (byteswap) { 1459 drr->drr_u.drr_object.drr_bonuslen = 1460 BSWAP_32(drr->drr_u.drr_object. 1461 drr_bonuslen); 1462 } 1463 (void) recv_read(hdl, fd, buf, 1464 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8), 1465 B_FALSE, NULL); 1466 break; 1467 1468 case DRR_WRITE: 1469 if (byteswap) { 1470 drr->drr_u.drr_write.drr_length = 1471 BSWAP_32(drr->drr_u.drr_write.drr_length); 1472 } 1473 (void) recv_read(hdl, fd, buf, 1474 drr->drr_u.drr_write.drr_length, B_FALSE, NULL); 1475 break; 1476 1477 case DRR_FREEOBJECTS: 1478 case DRR_FREE: 1479 break; 1480 1481 default: 1482 assert(!"invalid record type"); 1483 } 1484 } 1485 1486 free(buf); 1487 return (-1); 1488 } 1489 1490 /* 1491 * Restores a backup of tosnap from the file descriptor specified by infd. 1492 */ 1493 static int 1494 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, 1495 recvflags_t flags, dmu_replay_record_t *drr, 1496 dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl) 1497 { 1498 zfs_cmd_t zc = { 0 }; 1499 time_t begin_time; 1500 int ioctl_err, ioctl_errno, err, choplen; 1501 char *cp; 1502 struct drr_begin *drrb = &drr->drr_u.drr_begin; 1503 char errbuf[1024]; 1504 char chopprefix[ZFS_MAXNAMELEN]; 1505 boolean_t newfs = B_FALSE; 1506 boolean_t stream_wantsnewfs; 1507 uint64_t parent_snapguid = 0; 1508 prop_changelist_t *clp = NULL; 1509 1510 begin_time = time(NULL); 1511 1512 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1513 "cannot receive")); 1514 1515 if (stream_avl != NULL) { 1516 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, NULL); 1517 nvlist_t *props; 1518 int ret; 1519 1520 (void) nvlist_lookup_uint64(fs, "parentfromsnap", 1521 &parent_snapguid); 1522 err = nvlist_lookup_nvlist(fs, "props", &props); 1523 if (err) 1524 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0)); 1525 1526 if (flags.canmountoff) { 1527 VERIFY(0 == nvlist_add_uint64(props, 1528 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0)); 1529 } 1530 ret = zcmd_write_src_nvlist(hdl, &zc, props); 1531 if (err) 1532 nvlist_free(props); 1533 1534 if (ret != 0) 1535 return (-1); 1536 } 1537 1538 /* 1539 * Determine how much of the snapshot name stored in the stream 1540 * we are going to tack on to the name they specified on the 1541 * command line, and how much we are going to chop off. 1542 * 1543 * If they specified a snapshot, chop the entire name stored in 1544 * the stream. 1545 */ 1546 (void) strcpy(chopprefix, drrb->drr_toname); 1547 if (flags.isprefix) { 1548 /* 1549 * They specified a fs with -d, we want to tack on 1550 * everything but the pool name stored in the stream 1551 */ 1552 if (strchr(tosnap, '@')) { 1553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 1554 "argument - snapshot not allowed with -d")); 1555 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 1556 } 1557 cp = strchr(chopprefix, '/'); 1558 if (cp == NULL) 1559 cp = strchr(chopprefix, '@'); 1560 *cp = '\0'; 1561 } else if (strchr(tosnap, '@') == NULL) { 1562 /* 1563 * If they specified a filesystem without -d, we want to 1564 * tack on everything after the fs specified in the 1565 * first name from the stream. 1566 */ 1567 cp = strchr(chopprefix, '@'); 1568 *cp = '\0'; 1569 } 1570 choplen = strlen(chopprefix); 1571 1572 /* 1573 * Determine name of destination snapshot, store in zc_value. 1574 */ 1575 (void) strcpy(zc.zc_value, tosnap); 1576 (void) strncat(zc.zc_value, drrb->drr_toname+choplen, 1577 sizeof (zc.zc_value)); 1578 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) { 1579 zcmd_free_nvlists(&zc); 1580 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 1581 } 1582 1583 /* 1584 * Determine the name of the origin snapshot, store in zc_string. 1585 */ 1586 if (drrb->drr_flags & DRR_FLAG_CLONE) { 1587 if (guid_to_name(hdl, tosnap, 1588 drrb->drr_fromguid, zc.zc_string) != 0) { 1589 zcmd_free_nvlists(&zc); 1590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1591 "local origin for clone %s does not exist"), 1592 zc.zc_value); 1593 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 1594 } 1595 if (flags.verbose) 1596 (void) printf("found clone origin %s\n", zc.zc_string); 1597 } 1598 1599 stream_wantsnewfs = (drrb->drr_fromguid == NULL || 1600 (drrb->drr_flags & DRR_FLAG_CLONE)); 1601 1602 if (stream_wantsnewfs) { 1603 /* 1604 * if the parent fs does not exist, look for it based on 1605 * the parent snap GUID 1606 */ 1607 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1608 "cannot receive new filesystem stream")); 1609 1610 (void) strcpy(zc.zc_name, zc.zc_value); 1611 cp = strrchr(zc.zc_name, '/'); 1612 if (cp) 1613 *cp = '\0'; 1614 if (cp && 1615 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 1616 char suffix[ZFS_MAXNAMELEN]; 1617 (void) strcpy(suffix, strrchr(zc.zc_value, '/')); 1618 if (guid_to_name(hdl, tosnap, parent_snapguid, 1619 zc.zc_value) == 0) { 1620 *strchr(zc.zc_value, '@') = '\0'; 1621 (void) strcat(zc.zc_value, suffix); 1622 } 1623 } 1624 } else { 1625 /* 1626 * if the fs does not exist, look for it based on the 1627 * fromsnap GUID 1628 */ 1629 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1630 "cannot receive incremental stream")); 1631 1632 (void) strcpy(zc.zc_name, zc.zc_value); 1633 *strchr(zc.zc_name, '@') = '\0'; 1634 1635 if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 1636 char snap[ZFS_MAXNAMELEN]; 1637 (void) strcpy(snap, strchr(zc.zc_value, '@')); 1638 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid, 1639 zc.zc_value) == 0) { 1640 *strchr(zc.zc_value, '@') = '\0'; 1641 (void) strcat(zc.zc_value, snap); 1642 } 1643 } 1644 } 1645 1646 (void) strcpy(zc.zc_name, zc.zc_value); 1647 *strchr(zc.zc_name, '@') = '\0'; 1648 1649 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 1650 zfs_handle_t *zhp; 1651 /* 1652 * Destination fs exists. Therefore this should either 1653 * be an incremental, or the stream specifies a new fs 1654 * (full stream or clone) and they want us to blow it 1655 * away (and have therefore specified -F and removed any 1656 * snapshots). 1657 */ 1658 1659 if (stream_wantsnewfs) { 1660 if (!flags.force) { 1661 zcmd_free_nvlists(&zc); 1662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1663 "destination '%s' exists\n" 1664 "must specify -F to overwrite it"), 1665 zc.zc_name); 1666 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 1667 } 1668 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 1669 &zc) == 0) { 1670 zcmd_free_nvlists(&zc); 1671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1672 "destination has snapshots (eg. %s)\n" 1673 "must destroy them to overwrite it"), 1674 zc.zc_name); 1675 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 1676 } 1677 } 1678 1679 if ((zhp = zfs_open(hdl, zc.zc_name, 1680 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 1681 zcmd_free_nvlists(&zc); 1682 return (-1); 1683 } 1684 1685 if (stream_wantsnewfs && 1686 zhp->zfs_dmustats.dds_origin[0]) { 1687 zcmd_free_nvlists(&zc); 1688 zfs_close(zhp); 1689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1690 "destination '%s' is a clone\n" 1691 "must destroy it to overwrite it"), 1692 zc.zc_name); 1693 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 1694 } 1695 1696 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && 1697 stream_wantsnewfs) { 1698 /* We can't do online recv in this case */ 1699 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0); 1700 if (clp == NULL) { 1701 zcmd_free_nvlists(&zc); 1702 return (-1); 1703 } 1704 if (changelist_prefix(clp) != 0) { 1705 changelist_free(clp); 1706 zcmd_free_nvlists(&zc); 1707 return (-1); 1708 } 1709 } 1710 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME && 1711 zvol_remove_link(hdl, zhp->zfs_name) != 0) { 1712 zfs_close(zhp); 1713 zcmd_free_nvlists(&zc); 1714 return (-1); 1715 } 1716 zfs_close(zhp); 1717 } else { 1718 /* 1719 * Destination filesystem does not exist. Therefore we better 1720 * be creating a new filesystem (either from a full backup, or 1721 * a clone). It would therefore be invalid if the user 1722 * specified only the pool name (i.e. if the destination name 1723 * contained no slash character). 1724 */ 1725 if (!stream_wantsnewfs || 1726 (cp = strrchr(zc.zc_name, '/')) == NULL) { 1727 zcmd_free_nvlists(&zc); 1728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1729 "destination '%s' does not exist"), zc.zc_name); 1730 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 1731 } 1732 1733 /* 1734 * Trim off the final dataset component so we perform the 1735 * recvbackup ioctl to the filesystems's parent. 1736 */ 1737 *cp = '\0'; 1738 1739 if (flags.isprefix && !flags.dryrun && 1740 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) { 1741 zcmd_free_nvlists(&zc); 1742 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 1743 } 1744 1745 newfs = B_TRUE; 1746 } 1747 1748 zc.zc_begin_record = drr_noswap->drr_u.drr_begin; 1749 zc.zc_cookie = infd; 1750 zc.zc_guid = flags.force; 1751 if (flags.verbose) { 1752 (void) printf("%s %s stream of %s into %s\n", 1753 flags.dryrun ? "would receive" : "receiving", 1754 drrb->drr_fromguid ? "incremental" : "full", 1755 drrb->drr_toname, zc.zc_value); 1756 (void) fflush(stdout); 1757 } 1758 1759 if (flags.dryrun) { 1760 zcmd_free_nvlists(&zc); 1761 return (recv_skip(hdl, infd, flags.byteswap)); 1762 } 1763 1764 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc); 1765 ioctl_errno = errno; 1766 if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) { 1767 /* 1768 * It may be that this snapshot already exists, 1769 * in which case we want to consume & ignore it 1770 * rather than failing. 1771 */ 1772 avl_tree_t *local_avl; 1773 nvlist_t *local_nv, *fs; 1774 char *cp = strchr(zc.zc_value, '@'); 1775 1776 /* 1777 * XXX Do this faster by just iterating over snaps in 1778 * this fs. Also if zc_value does not exist, we will 1779 * get a strange "does not exist" error message. 1780 */ 1781 *cp = '\0'; 1782 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, 1783 &local_nv, &local_avl) == 0) { 1784 *cp = '@'; 1785 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); 1786 fsavl_destroy(local_avl); 1787 nvlist_free(local_nv); 1788 1789 if (fs != NULL) { 1790 if (flags.verbose) { 1791 (void) printf("snap %s already exists; " 1792 "ignoring\n", zc.zc_value); 1793 } 1794 ioctl_err = recv_skip(hdl, infd, 1795 flags.byteswap); 1796 } 1797 } 1798 *cp = '@'; 1799 } 1800 1801 zcmd_free_nvlists(&zc); 1802 1803 if (ioctl_err != 0) { 1804 switch (ioctl_errno) { 1805 case ENODEV: 1806 cp = strchr(zc.zc_value, '@'); 1807 *cp = '\0'; 1808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1809 "most recent snapshot of %s does not\n" 1810 "match incremental source"), zc.zc_value); 1811 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 1812 *cp = '@'; 1813 break; 1814 case ETXTBSY: 1815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1816 "destination %s has been modified\n" 1817 "since most recent snapshot"), zc.zc_name); 1818 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 1819 break; 1820 case EEXIST: 1821 cp = strchr(zc.zc_value, '@'); 1822 if (newfs) { 1823 /* it's the containing fs that exists */ 1824 *cp = '\0'; 1825 } 1826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1827 "destination already exists")); 1828 (void) zfs_error_fmt(hdl, EZFS_EXISTS, 1829 dgettext(TEXT_DOMAIN, "cannot restore to %s"), 1830 zc.zc_value); 1831 *cp = '@'; 1832 break; 1833 case EINVAL: 1834 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1835 break; 1836 case ECKSUM: 1837 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1838 "invalid stream (checksum mismatch)")); 1839 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1840 break; 1841 default: 1842 (void) zfs_standard_error(hdl, ioctl_errno, errbuf); 1843 } 1844 } 1845 1846 /* 1847 * Mount or recreate the /dev links for the target filesystem 1848 * (if created, or if we tore them down to do an incremental 1849 * restore), and the /dev links for the new snapshot (if 1850 * created). Also mount any children of the target filesystem 1851 * if we did an incremental receive. 1852 */ 1853 cp = strchr(zc.zc_value, '@'); 1854 if (cp && (ioctl_err == 0 || !newfs)) { 1855 zfs_handle_t *h; 1856 1857 *cp = '\0'; 1858 h = zfs_open(hdl, zc.zc_value, 1859 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1860 *cp = '@'; 1861 if (h) { 1862 if (h->zfs_type == ZFS_TYPE_VOLUME) { 1863 err = zvol_create_link(hdl, h->zfs_name); 1864 if (err == 0 && ioctl_err == 0) 1865 err = zvol_create_link(hdl, 1866 zc.zc_value); 1867 } else if (newfs) { 1868 err = zfs_mount(h, NULL, 0); 1869 } 1870 zfs_close(h); 1871 } 1872 } 1873 1874 if (clp) { 1875 err |= changelist_postfix(clp); 1876 changelist_free(clp); 1877 } 1878 1879 if (err || ioctl_err) 1880 return (-1); 1881 1882 if (flags.verbose) { 1883 char buf1[64]; 1884 char buf2[64]; 1885 uint64_t bytes = zc.zc_cookie; 1886 time_t delta = time(NULL) - begin_time; 1887 if (delta == 0) 1888 delta = 1; 1889 zfs_nicenum(bytes, buf1, sizeof (buf1)); 1890 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 1891 1892 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 1893 buf1, delta, buf2); 1894 } 1895 1896 return (0); 1897 } 1898 1899 /* 1900 * Restores a backup of tosnap from the file descriptor specified by infd. 1901 */ 1902 int 1903 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags, 1904 int infd, avl_tree_t *stream_avl) 1905 { 1906 int err; 1907 dmu_replay_record_t drr, drr_noswap; 1908 struct drr_begin *drrb = &drr.drr_u.drr_begin; 1909 char errbuf[1024]; 1910 zio_cksum_t zcksum = { 0 }; 1911 1912 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1913 "cannot receive")); 1914 1915 if (flags.isprefix && 1916 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { 1917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " 1918 "(%s) does not exist"), tosnap); 1919 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 1920 } 1921 1922 /* read in the BEGIN record */ 1923 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, 1924 &zcksum))) 1925 return (err); 1926 1927 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { 1928 /* It's the double end record at the end of a package */ 1929 return (ENODATA); 1930 } 1931 1932 /* the kernel needs the non-byteswapped begin record */ 1933 drr_noswap = drr; 1934 1935 flags.byteswap = B_FALSE; 1936 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { 1937 /* 1938 * We computed the checksum in the wrong byteorder in 1939 * recv_read() above; do it again correctly. 1940 */ 1941 bzero(&zcksum, sizeof (zio_cksum_t)); 1942 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum); 1943 flags.byteswap = B_TRUE; 1944 1945 drr.drr_type = BSWAP_32(drr.drr_type); 1946 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); 1947 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 1948 drrb->drr_version = BSWAP_64(drrb->drr_version); 1949 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 1950 drrb->drr_type = BSWAP_32(drrb->drr_type); 1951 drrb->drr_flags = BSWAP_32(drrb->drr_flags); 1952 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 1953 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 1954 } 1955 1956 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { 1957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 1958 "stream (bad magic number)")); 1959 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 1960 } 1961 1962 if (strchr(drrb->drr_toname, '@') == NULL) { 1963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 1964 "stream (bad snapshot name)")); 1965 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 1966 } 1967 1968 if (drrb->drr_version == DMU_BACKUP_STREAM_VERSION) { 1969 return (zfs_receive_one(hdl, infd, tosnap, flags, 1970 &drr, &drr_noswap, stream_avl)); 1971 } else if (drrb->drr_version == DMU_BACKUP_HEADER_VERSION) { 1972 return (zfs_receive_package(hdl, infd, tosnap, flags, 1973 &drr, &zcksum)); 1974 } else { 1975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1976 "stream is unsupported version %llu"), 1977 drrb->drr_version); 1978 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 1979 } 1980 } 1981