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 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <assert.h> 28 #include <ctype.h> 29 #include <errno.h> 30 #include <libintl.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <strings.h> 34 #include <unistd.h> 35 #include <stddef.h> 36 #include <fcntl.h> 37 #include <sys/mount.h> 38 #include <pthread.h> 39 #include <umem.h> 40 41 #include <libzfs.h> 42 43 #include "zfs_namecheck.h" 44 #include "zfs_prop.h" 45 #include "zfs_fletcher.h" 46 #include "libzfs_impl.h" 47 #include <sha2.h> 48 #include <sys/zio_checksum.h> 49 #include <sys/ddt.h> 50 51 /* in libzfs_dataset.c */ 52 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *); 53 54 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t, 55 int, avl_tree_t *, char **); 56 57 static const zio_cksum_t zero_cksum = { 0 }; 58 59 typedef struct dedup_arg { 60 int inputfd; 61 int outputfd; 62 libzfs_handle_t *dedup_hdl; 63 } dedup_arg_t; 64 65 typedef struct dataref { 66 uint64_t ref_guid; 67 uint64_t ref_object; 68 uint64_t ref_offset; 69 } dataref_t; 70 71 typedef struct dedup_entry { 72 struct dedup_entry *dde_next; 73 zio_cksum_t dde_chksum; 74 uint64_t dde_prop; 75 dataref_t dde_ref; 76 } dedup_entry_t; 77 78 #define MAX_DDT_PHYSMEM_PERCENT 20 79 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128 80 81 typedef struct dedup_table { 82 dedup_entry_t **dedup_hash_array; 83 umem_cache_t *ddecache; 84 uint64_t max_ddt_size; /* max dedup table size in bytes */ 85 uint64_t cur_ddt_size; /* current dedup table size in bytes */ 86 uint64_t ddt_count; 87 int numhashbits; 88 boolean_t ddt_full; 89 } dedup_table_t; 90 91 static int 92 high_order_bit(uint64_t n) 93 { 94 int count; 95 96 for (count = 0; n != 0; count++) 97 n >>= 1; 98 return (count); 99 } 100 101 static size_t 102 ssread(void *buf, size_t len, FILE *stream) 103 { 104 size_t outlen; 105 106 if ((outlen = fread(buf, len, 1, stream)) == 0) 107 return (0); 108 109 return (outlen); 110 } 111 112 static void 113 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp, 114 zio_cksum_t *cs, uint64_t prop, dataref_t *dr) 115 { 116 dedup_entry_t *dde; 117 118 if (ddt->cur_ddt_size >= ddt->max_ddt_size) { 119 if (ddt->ddt_full == B_FALSE) { 120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 121 "Dedup table full. Deduplication will continue " 122 "with existing table entries")); 123 ddt->ddt_full = B_TRUE; 124 } 125 return; 126 } 127 128 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT)) 129 != NULL) { 130 assert(*ddepp == NULL); 131 dde->dde_next = NULL; 132 dde->dde_chksum = *cs; 133 dde->dde_prop = prop; 134 dde->dde_ref = *dr; 135 *ddepp = dde; 136 ddt->cur_ddt_size += sizeof (dedup_entry_t); 137 ddt->ddt_count++; 138 } 139 } 140 141 /* 142 * Using the specified dedup table, do a lookup for an entry with 143 * the checksum cs. If found, return the block's reference info 144 * in *dr. Otherwise, insert a new entry in the dedup table, using 145 * the reference information specified by *dr. 146 * 147 * return value: true - entry was found 148 * false - entry was not found 149 */ 150 static boolean_t 151 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs, 152 uint64_t prop, dataref_t *dr) 153 { 154 uint32_t hashcode; 155 dedup_entry_t **ddepp; 156 157 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits); 158 159 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL; 160 ddepp = &((*ddepp)->dde_next)) { 161 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) && 162 (*ddepp)->dde_prop == prop) { 163 *dr = (*ddepp)->dde_ref; 164 return (B_TRUE); 165 } 166 } 167 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr); 168 return (B_FALSE); 169 } 170 171 static int 172 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd) 173 { 174 fletcher_4_incremental_native(buf, len, zc); 175 return (write(outfd, buf, len)); 176 } 177 178 /* 179 * This function is started in a separate thread when the dedup option 180 * has been requested. The main send thread determines the list of 181 * snapshots to be included in the send stream and makes the ioctl calls 182 * for each one. But instead of having the ioctl send the output to the 183 * the output fd specified by the caller of zfs_send()), the 184 * ioctl is told to direct the output to a pipe, which is read by the 185 * alternate thread running THIS function. This function does the 186 * dedup'ing by: 187 * 1. building a dedup table (the DDT) 188 * 2. doing checksums on each data block and inserting a record in the DDT 189 * 3. looking for matching checksums, and 190 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever 191 * a duplicate block is found. 192 * The output of this function then goes to the output fd requested 193 * by the caller of zfs_send(). 194 */ 195 static void * 196 cksummer(void *arg) 197 { 198 dedup_arg_t *dda = arg; 199 char *buf = malloc(1<<20); 200 dmu_replay_record_t thedrr; 201 dmu_replay_record_t *drr = &thedrr; 202 struct drr_begin *drrb = &thedrr.drr_u.drr_begin; 203 struct drr_end *drre = &thedrr.drr_u.drr_end; 204 struct drr_object *drro = &thedrr.drr_u.drr_object; 205 struct drr_write *drrw = &thedrr.drr_u.drr_write; 206 FILE *ofp; 207 int outfd; 208 dmu_replay_record_t wbr_drr = {0}; 209 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref; 210 dedup_table_t ddt; 211 zio_cksum_t stream_cksum; 212 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); 213 uint64_t numbuckets; 214 215 ddt.max_ddt_size = 216 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100, 217 SMALLEST_POSSIBLE_MAX_DDT_MB<<20); 218 219 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t)); 220 221 /* 222 * numbuckets must be a power of 2. Increase number to 223 * a power of 2 if necessary. 224 */ 225 if (!ISP2(numbuckets)) 226 numbuckets = 1 << high_order_bit(numbuckets); 227 228 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *)); 229 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0, 230 NULL, NULL, NULL, NULL, NULL, 0); 231 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *); 232 ddt.numhashbits = high_order_bit(numbuckets) - 1; 233 ddt.ddt_full = B_FALSE; 234 235 /* Initialize the write-by-reference block. */ 236 wbr_drr.drr_type = DRR_WRITE_BYREF; 237 wbr_drr.drr_payloadlen = 0; 238 239 outfd = dda->outputfd; 240 ofp = fdopen(dda->inputfd, "r"); 241 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) { 242 243 switch (drr->drr_type) { 244 case DRR_BEGIN: 245 { 246 int fflags; 247 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0); 248 249 /* set the DEDUP feature flag for this stream */ 250 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 251 fflags |= (DMU_BACKUP_FEATURE_DEDUP | 252 DMU_BACKUP_FEATURE_DEDUPPROPS); 253 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags); 254 255 if (cksum_and_write(drr, sizeof (dmu_replay_record_t), 256 &stream_cksum, outfd) == -1) 257 goto out; 258 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == 259 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) { 260 int sz = drr->drr_payloadlen; 261 262 if (sz > 1<<20) { 263 free(buf); 264 buf = malloc(sz); 265 } 266 (void) ssread(buf, sz, ofp); 267 if (ferror(stdin)) 268 perror("fread"); 269 if (cksum_and_write(buf, sz, &stream_cksum, 270 outfd) == -1) 271 goto out; 272 } 273 break; 274 } 275 276 case DRR_END: 277 { 278 /* use the recalculated checksum */ 279 ZIO_SET_CHECKSUM(&drre->drr_checksum, 280 stream_cksum.zc_word[0], stream_cksum.zc_word[1], 281 stream_cksum.zc_word[2], stream_cksum.zc_word[3]); 282 if ((write(outfd, drr, 283 sizeof (dmu_replay_record_t))) == -1) 284 goto out; 285 break; 286 } 287 288 case DRR_OBJECT: 289 { 290 if (cksum_and_write(drr, sizeof (dmu_replay_record_t), 291 &stream_cksum, outfd) == -1) 292 goto out; 293 if (drro->drr_bonuslen > 0) { 294 (void) ssread(buf, 295 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8), 296 ofp); 297 if (cksum_and_write(buf, 298 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8), 299 &stream_cksum, outfd) == -1) 300 goto out; 301 } 302 break; 303 } 304 305 case DRR_FREEOBJECTS: 306 { 307 if (cksum_and_write(drr, sizeof (dmu_replay_record_t), 308 &stream_cksum, outfd) == -1) 309 goto out; 310 break; 311 } 312 313 case DRR_WRITE: 314 { 315 dataref_t dataref; 316 317 (void) ssread(buf, drrw->drr_length, ofp); 318 319 /* 320 * Use the existing checksum if it's dedup-capable, 321 * else calculate a SHA256 checksum for it. 322 */ 323 324 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum, 325 zero_cksum) || 326 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) { 327 SHA256_CTX ctx; 328 zio_cksum_t tmpsha256; 329 330 SHA256Init(&ctx); 331 SHA256Update(&ctx, buf, drrw->drr_length); 332 SHA256Final(&tmpsha256, &ctx); 333 drrw->drr_key.ddk_cksum.zc_word[0] = 334 BE_64(tmpsha256.zc_word[0]); 335 drrw->drr_key.ddk_cksum.zc_word[1] = 336 BE_64(tmpsha256.zc_word[1]); 337 drrw->drr_key.ddk_cksum.zc_word[2] = 338 BE_64(tmpsha256.zc_word[2]); 339 drrw->drr_key.ddk_cksum.zc_word[3] = 340 BE_64(tmpsha256.zc_word[3]); 341 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256; 342 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP; 343 } 344 345 dataref.ref_guid = drrw->drr_toguid; 346 dataref.ref_object = drrw->drr_object; 347 dataref.ref_offset = drrw->drr_offset; 348 349 if (ddt_update(dda->dedup_hdl, &ddt, 350 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop, 351 &dataref)) { 352 /* block already present in stream */ 353 wbr_drrr->drr_object = drrw->drr_object; 354 wbr_drrr->drr_offset = drrw->drr_offset; 355 wbr_drrr->drr_length = drrw->drr_length; 356 wbr_drrr->drr_toguid = drrw->drr_toguid; 357 wbr_drrr->drr_refguid = dataref.ref_guid; 358 wbr_drrr->drr_refobject = 359 dataref.ref_object; 360 wbr_drrr->drr_refoffset = 361 dataref.ref_offset; 362 363 wbr_drrr->drr_checksumtype = 364 drrw->drr_checksumtype; 365 wbr_drrr->drr_checksumflags = 366 drrw->drr_checksumtype; 367 wbr_drrr->drr_key.ddk_cksum = 368 drrw->drr_key.ddk_cksum; 369 wbr_drrr->drr_key.ddk_prop = 370 drrw->drr_key.ddk_prop; 371 372 if (cksum_and_write(&wbr_drr, 373 sizeof (dmu_replay_record_t), &stream_cksum, 374 outfd) == -1) 375 goto out; 376 } else { 377 /* block not previously seen */ 378 if (cksum_and_write(drr, 379 sizeof (dmu_replay_record_t), &stream_cksum, 380 outfd) == -1) 381 goto out; 382 if (cksum_and_write(buf, 383 drrw->drr_length, 384 &stream_cksum, outfd) == -1) 385 goto out; 386 } 387 break; 388 } 389 390 case DRR_FREE: 391 { 392 if (cksum_and_write(drr, sizeof (dmu_replay_record_t), 393 &stream_cksum, outfd) == -1) 394 goto out; 395 break; 396 } 397 398 default: 399 (void) printf("INVALID record type 0x%x\n", 400 drr->drr_type); 401 /* should never happen, so assert */ 402 assert(B_FALSE); 403 } 404 } 405 out: 406 umem_cache_destroy(ddt.ddecache); 407 free(ddt.dedup_hash_array); 408 free(buf); 409 (void) fclose(ofp); 410 411 return (NULL); 412 } 413 414 /* 415 * Routines for dealing with the AVL tree of fs-nvlists 416 */ 417 typedef struct fsavl_node { 418 avl_node_t fn_node; 419 nvlist_t *fn_nvfs; 420 char *fn_snapname; 421 uint64_t fn_guid; 422 } fsavl_node_t; 423 424 static int 425 fsavl_compare(const void *arg1, const void *arg2) 426 { 427 const fsavl_node_t *fn1 = arg1; 428 const fsavl_node_t *fn2 = arg2; 429 430 if (fn1->fn_guid > fn2->fn_guid) 431 return (+1); 432 else if (fn1->fn_guid < fn2->fn_guid) 433 return (-1); 434 else 435 return (0); 436 } 437 438 /* 439 * Given the GUID of a snapshot, find its containing filesystem and 440 * (optionally) name. 441 */ 442 static nvlist_t * 443 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname) 444 { 445 fsavl_node_t fn_find; 446 fsavl_node_t *fn; 447 448 fn_find.fn_guid = snapguid; 449 450 fn = avl_find(avl, &fn_find, NULL); 451 if (fn) { 452 if (snapname) 453 *snapname = fn->fn_snapname; 454 return (fn->fn_nvfs); 455 } 456 return (NULL); 457 } 458 459 static void 460 fsavl_destroy(avl_tree_t *avl) 461 { 462 fsavl_node_t *fn; 463 void *cookie; 464 465 if (avl == NULL) 466 return; 467 468 cookie = NULL; 469 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL) 470 free(fn); 471 avl_destroy(avl); 472 free(avl); 473 } 474 475 /* 476 * Given an nvlist, produce an avl tree of snapshots, ordered by guid 477 */ 478 static avl_tree_t * 479 fsavl_create(nvlist_t *fss) 480 { 481 avl_tree_t *fsavl; 482 nvpair_t *fselem = NULL; 483 484 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL) 485 return (NULL); 486 487 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t), 488 offsetof(fsavl_node_t, fn_node)); 489 490 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) { 491 nvlist_t *nvfs, *snaps; 492 nvpair_t *snapelem = NULL; 493 494 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); 495 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); 496 497 while ((snapelem = 498 nvlist_next_nvpair(snaps, snapelem)) != NULL) { 499 fsavl_node_t *fn; 500 uint64_t guid; 501 502 VERIFY(0 == nvpair_value_uint64(snapelem, &guid)); 503 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) { 504 fsavl_destroy(fsavl); 505 return (NULL); 506 } 507 fn->fn_nvfs = nvfs; 508 fn->fn_snapname = nvpair_name(snapelem); 509 fn->fn_guid = guid; 510 511 /* 512 * Note: if there are multiple snaps with the 513 * same GUID, we ignore all but one. 514 */ 515 if (avl_find(fsavl, fn, NULL) == NULL) 516 avl_add(fsavl, fn); 517 else 518 free(fn); 519 } 520 } 521 522 return (fsavl); 523 } 524 525 /* 526 * Routines for dealing with the giant nvlist of fs-nvlists, etc. 527 */ 528 typedef struct send_data { 529 uint64_t parent_fromsnap_guid; 530 nvlist_t *parent_snaps; 531 nvlist_t *fss; 532 nvlist_t *snapprops; 533 const char *fromsnap; 534 const char *tosnap; 535 boolean_t recursive; 536 537 /* 538 * The header nvlist is of the following format: 539 * { 540 * "tosnap" -> string 541 * "fromsnap" -> string (if incremental) 542 * "fss" -> { 543 * id -> { 544 * 545 * "name" -> string (full name; for debugging) 546 * "parentfromsnap" -> number (guid of fromsnap in parent) 547 * 548 * "props" -> { name -> value (only if set here) } 549 * "snaps" -> { name (lastname) -> number (guid) } 550 * "snapprops" -> { name (lastname) -> { name -> value } } 551 * 552 * "origin" -> number (guid) (if clone) 553 * "sent" -> boolean (not on-disk) 554 * } 555 * } 556 * } 557 * 558 */ 559 } send_data_t; 560 561 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv); 562 563 static int 564 send_iterate_snap(zfs_handle_t *zhp, void *arg) 565 { 566 send_data_t *sd = arg; 567 uint64_t guid = zhp->zfs_dmustats.dds_guid; 568 char *snapname; 569 nvlist_t *nv; 570 571 snapname = strrchr(zhp->zfs_name, '@')+1; 572 573 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid)); 574 /* 575 * NB: if there is no fromsnap here (it's a newly created fs in 576 * an incremental replication), we will substitute the tosnap. 577 */ 578 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) || 579 (sd->parent_fromsnap_guid == 0 && sd->tosnap && 580 strcmp(snapname, sd->tosnap) == 0)) { 581 sd->parent_fromsnap_guid = guid; 582 } 583 584 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); 585 send_iterate_prop(zhp, nv); 586 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv)); 587 nvlist_free(nv); 588 589 zfs_close(zhp); 590 return (0); 591 } 592 593 static void 594 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv) 595 { 596 nvpair_t *elem = NULL; 597 598 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) { 599 char *propname = nvpair_name(elem); 600 zfs_prop_t prop = zfs_name_to_prop(propname); 601 nvlist_t *propnv; 602 603 if (!zfs_prop_user(propname)) { 604 /* 605 * Realistically, this should never happen. However, 606 * we want the ability to add DSL properties without 607 * needing to make incompatible version changes. We 608 * need to ignore unknown properties to allow older 609 * software to still send datasets containing these 610 * properties, with the unknown properties elided. 611 */ 612 if (prop == ZPROP_INVAL) 613 continue; 614 615 if (zfs_prop_readonly(prop)) 616 continue; 617 } 618 619 verify(nvpair_value_nvlist(elem, &propnv) == 0); 620 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION || 621 prop == ZFS_PROP_REFQUOTA || 622 prop == ZFS_PROP_REFRESERVATION) { 623 char *source; 624 uint64_t value; 625 verify(nvlist_lookup_uint64(propnv, 626 ZPROP_VALUE, &value) == 0); 627 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 628 continue; 629 /* 630 * May have no source before SPA_VERSION_RECVD_PROPS, 631 * but is still modifiable. 632 */ 633 if (nvlist_lookup_string(propnv, 634 ZPROP_SOURCE, &source) == 0) { 635 if ((strcmp(source, zhp->zfs_name) != 0) && 636 (strcmp(source, 637 ZPROP_SOURCE_VAL_RECVD) != 0)) 638 continue; 639 } 640 } else { 641 char *source; 642 if (nvlist_lookup_string(propnv, 643 ZPROP_SOURCE, &source) != 0) 644 continue; 645 if ((strcmp(source, zhp->zfs_name) != 0) && 646 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)) 647 continue; 648 } 649 650 if (zfs_prop_user(propname) || 651 zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 652 char *value; 653 verify(nvlist_lookup_string(propnv, 654 ZPROP_VALUE, &value) == 0); 655 VERIFY(0 == nvlist_add_string(nv, propname, value)); 656 } else { 657 uint64_t value; 658 verify(nvlist_lookup_uint64(propnv, 659 ZPROP_VALUE, &value) == 0); 660 VERIFY(0 == nvlist_add_uint64(nv, propname, value)); 661 } 662 } 663 } 664 665 /* 666 * recursively generate nvlists describing datasets. See comment 667 * for the data structure send_data_t above for description of contents 668 * of the nvlist. 669 */ 670 static int 671 send_iterate_fs(zfs_handle_t *zhp, void *arg) 672 { 673 send_data_t *sd = arg; 674 nvlist_t *nvfs, *nv; 675 int rv = 0; 676 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid; 677 uint64_t guid = zhp->zfs_dmustats.dds_guid; 678 char guidstring[64]; 679 680 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0)); 681 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name)); 682 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap", 683 sd->parent_fromsnap_guid)); 684 685 if (zhp->zfs_dmustats.dds_origin[0]) { 686 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl, 687 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 688 if (origin == NULL) 689 return (-1); 690 VERIFY(0 == nvlist_add_uint64(nvfs, "origin", 691 origin->zfs_dmustats.dds_guid)); 692 } 693 694 /* iterate over props */ 695 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); 696 send_iterate_prop(zhp, nv); 697 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv)); 698 nvlist_free(nv); 699 700 /* iterate over snaps, and set sd->parent_fromsnap_guid */ 701 sd->parent_fromsnap_guid = 0; 702 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0)); 703 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0)); 704 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd); 705 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps)); 706 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops)); 707 nvlist_free(sd->parent_snaps); 708 nvlist_free(sd->snapprops); 709 710 /* add this fs to nvlist */ 711 (void) snprintf(guidstring, sizeof (guidstring), 712 "0x%llx", (longlong_t)guid); 713 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs)); 714 nvlist_free(nvfs); 715 716 /* iterate over children */ 717 if (sd->recursive) 718 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd); 719 720 sd->parent_fromsnap_guid = parent_fromsnap_guid_save; 721 722 zfs_close(zhp); 723 return (rv); 724 } 725 726 static int 727 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap, 728 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp) 729 { 730 zfs_handle_t *zhp; 731 send_data_t sd = { 0 }; 732 int error; 733 734 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 735 if (zhp == NULL) 736 return (EZFS_BADTYPE); 737 738 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0)); 739 sd.fromsnap = fromsnap; 740 sd.tosnap = tosnap; 741 sd.recursive = recursive; 742 743 if ((error = send_iterate_fs(zhp, &sd)) != 0) { 744 nvlist_free(sd.fss); 745 if (avlp != NULL) 746 *avlp = NULL; 747 *nvlp = NULL; 748 return (error); 749 } 750 751 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) { 752 nvlist_free(sd.fss); 753 *nvlp = NULL; 754 return (EZFS_NOMEM); 755 } 756 757 *nvlp = sd.fss; 758 return (0); 759 } 760 761 /* 762 * Routines for dealing with the sorted snapshot functionality 763 */ 764 typedef struct zfs_node { 765 zfs_handle_t *zn_handle; 766 avl_node_t zn_avlnode; 767 } zfs_node_t; 768 769 static int 770 zfs_sort_snaps(zfs_handle_t *zhp, void *data) 771 { 772 avl_tree_t *avl = data; 773 zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t)); 774 775 node->zn_handle = zhp; 776 avl_add(avl, node); 777 return (0); 778 } 779 780 /* ARGSUSED */ 781 static int 782 zfs_snapshot_compare(const void *larg, const void *rarg) 783 { 784 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 785 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 786 uint64_t lcreate, rcreate; 787 788 /* 789 * Sort them according to creation time. We use the hidden 790 * CREATETXG property to get an absolute ordering of snapshots. 791 */ 792 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 793 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 794 795 if (lcreate < rcreate) 796 return (-1); 797 else if (lcreate > rcreate) 798 return (+1); 799 else 800 return (0); 801 } 802 803 int 804 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data) 805 { 806 int ret = 0; 807 zfs_node_t *node; 808 avl_tree_t avl; 809 void *cookie = NULL; 810 811 avl_create(&avl, zfs_snapshot_compare, 812 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode)); 813 814 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl); 815 816 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node)) 817 ret |= callback(node->zn_handle, data); 818 819 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL) 820 free(node); 821 822 avl_destroy(&avl); 823 824 return (ret); 825 } 826 827 /* 828 * Routines specific to "zfs send" 829 */ 830 typedef struct send_dump_data { 831 /* these are all just the short snapname (the part after the @) */ 832 const char *fromsnap; 833 const char *tosnap; 834 char prevsnap[ZFS_MAXNAMELEN]; 835 boolean_t seenfrom, seento, replicate, doall, fromorigin; 836 boolean_t verbose; 837 int outfd; 838 boolean_t err; 839 nvlist_t *fss; 840 avl_tree_t *fsavl; 841 snapfilter_cb_t *filter_cb; 842 void *filter_cb_arg; 843 } send_dump_data_t; 844 845 /* 846 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 847 * NULL) to the file descriptor specified by outfd. 848 */ 849 static int 850 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin, 851 int outfd, boolean_t enoent_ok, boolean_t *got_enoent) 852 { 853 zfs_cmd_t zc = { 0 }; 854 libzfs_handle_t *hdl = zhp->zfs_hdl; 855 856 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 857 assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin); 858 859 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 860 if (fromsnap) 861 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value)); 862 zc.zc_cookie = outfd; 863 zc.zc_obj = fromorigin; 864 865 *got_enoent = B_FALSE; 866 867 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) { 868 char errbuf[1024]; 869 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 870 "warning: cannot send '%s'"), zhp->zfs_name); 871 872 switch (errno) { 873 874 case EXDEV: 875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 876 "not an earlier snapshot from the same fs")); 877 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 878 879 case ENOENT: 880 if (enoent_ok) { 881 *got_enoent = B_TRUE; 882 return (0); 883 } 884 if (zfs_dataset_exists(hdl, zc.zc_name, 885 ZFS_TYPE_SNAPSHOT)) { 886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 887 "incremental source (@%s) does not exist"), 888 zc.zc_value); 889 } 890 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 891 892 case EDQUOT: 893 case EFBIG: 894 case EIO: 895 case ENOLINK: 896 case ENOSPC: 897 case ENOSTR: 898 case ENXIO: 899 case EPIPE: 900 case ERANGE: 901 case EFAULT: 902 case EROFS: 903 zfs_error_aux(hdl, strerror(errno)); 904 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 905 906 default: 907 return (zfs_standard_error(hdl, errno, errbuf)); 908 } 909 } 910 911 return (0); 912 } 913 914 static int 915 dump_snapshot(zfs_handle_t *zhp, void *arg) 916 { 917 send_dump_data_t *sdd = arg; 918 const char *thissnap; 919 int err; 920 boolean_t got_enoent; 921 922 thissnap = strchr(zhp->zfs_name, '@') + 1; 923 924 if (sdd->fromsnap && !sdd->seenfrom && 925 strcmp(sdd->fromsnap, thissnap) == 0) { 926 sdd->seenfrom = B_TRUE; 927 (void) strcpy(sdd->prevsnap, thissnap); 928 zfs_close(zhp); 929 return (0); 930 } 931 932 if (sdd->seento || !sdd->seenfrom) { 933 zfs_close(zhp); 934 return (0); 935 } 936 937 if (strcmp(sdd->tosnap, thissnap) == 0) 938 sdd->seento = B_TRUE; 939 940 /* 941 * If a filter function exists, call it to determine whether 942 * this snapshot will be sent. 943 */ 944 if (sdd->filter_cb != NULL && 945 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE) { 946 /* 947 * This snapshot is filtered out. Don't send it, and don't 948 * set prevsnap, so it will be as if this snapshot didn't 949 * exist, and the next accepted snapshot will be sent as 950 * an incremental from the last accepted one, or as the 951 * first (and full) snapshot in the case of a replication, 952 * non-incremental send. 953 */ 954 zfs_close(zhp); 955 return (0); 956 } 957 958 /* send it */ 959 if (sdd->verbose) { 960 (void) fprintf(stderr, "sending from @%s to %s\n", 961 sdd->prevsnap, zhp->zfs_name); 962 } 963 964 err = dump_ioctl(zhp, sdd->prevsnap, 965 sdd->prevsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate), 966 sdd->outfd, B_TRUE, &got_enoent); 967 968 if (got_enoent) 969 err = 0; 970 else 971 (void) strcpy(sdd->prevsnap, thissnap); 972 zfs_close(zhp); 973 return (err); 974 } 975 976 static int 977 dump_filesystem(zfs_handle_t *zhp, void *arg) 978 { 979 int rv = 0; 980 send_dump_data_t *sdd = arg; 981 boolean_t missingfrom = B_FALSE; 982 zfs_cmd_t zc = { 0 }; 983 984 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 985 zhp->zfs_name, sdd->tosnap); 986 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 987 (void) fprintf(stderr, "WARNING: " 988 "could not send %s@%s: does not exist\n", 989 zhp->zfs_name, sdd->tosnap); 990 sdd->err = B_TRUE; 991 return (0); 992 } 993 994 if (sdd->replicate && sdd->fromsnap) { 995 /* 996 * If this fs does not have fromsnap, and we're doing 997 * recursive, we need to send a full stream from the 998 * beginning (or an incremental from the origin if this 999 * is a clone). If we're doing non-recursive, then let 1000 * them get the error. 1001 */ 1002 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 1003 zhp->zfs_name, sdd->fromsnap); 1004 if (ioctl(zhp->zfs_hdl->libzfs_fd, 1005 ZFS_IOC_OBJSET_STATS, &zc) != 0) { 1006 missingfrom = B_TRUE; 1007 } 1008 } 1009 1010 if (sdd->doall) { 1011 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0; 1012 if (sdd->fromsnap == NULL || missingfrom) 1013 sdd->seenfrom = B_TRUE; 1014 1015 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg); 1016 if (!sdd->seenfrom) { 1017 (void) fprintf(stderr, 1018 "WARNING: could not send %s@%s:\n" 1019 "incremental source (%s@%s) does not exist\n", 1020 zhp->zfs_name, sdd->tosnap, 1021 zhp->zfs_name, sdd->fromsnap); 1022 sdd->err = B_TRUE; 1023 } else if (!sdd->seento) { 1024 if (sdd->fromsnap) { 1025 (void) fprintf(stderr, 1026 "WARNING: could not send %s@%s:\n" 1027 "incremental source (%s@%s) " 1028 "is not earlier than it\n", 1029 zhp->zfs_name, sdd->tosnap, 1030 zhp->zfs_name, sdd->fromsnap); 1031 } else { 1032 (void) fprintf(stderr, "WARNING: " 1033 "could not send %s@%s: does not exist\n", 1034 zhp->zfs_name, sdd->tosnap); 1035 } 1036 sdd->err = B_TRUE; 1037 } 1038 } else { 1039 zfs_handle_t *snapzhp; 1040 char snapname[ZFS_MAXNAMELEN]; 1041 1042 (void) snprintf(snapname, sizeof (snapname), "%s@%s", 1043 zfs_get_name(zhp), sdd->tosnap); 1044 snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT); 1045 if (snapzhp == NULL) { 1046 rv = -1; 1047 } else { 1048 if (sdd->filter_cb == NULL || 1049 sdd->filter_cb(snapzhp, sdd->filter_cb_arg) == 1050 B_TRUE) { 1051 boolean_t got_enoent; 1052 1053 rv = dump_ioctl(snapzhp, 1054 missingfrom ? NULL : sdd->fromsnap, 1055 sdd->fromorigin || missingfrom, 1056 sdd->outfd, B_FALSE, &got_enoent); 1057 } 1058 sdd->seento = B_TRUE; 1059 zfs_close(snapzhp); 1060 } 1061 } 1062 1063 return (rv); 1064 } 1065 1066 static int 1067 dump_filesystems(zfs_handle_t *rzhp, void *arg) 1068 { 1069 send_dump_data_t *sdd = arg; 1070 nvpair_t *fspair; 1071 boolean_t needagain, progress; 1072 1073 if (!sdd->replicate) 1074 return (dump_filesystem(rzhp, sdd)); 1075 1076 again: 1077 needagain = progress = B_FALSE; 1078 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1079 fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1080 nvlist_t *fslist; 1081 char *fsname; 1082 zfs_handle_t *zhp; 1083 int err; 1084 uint64_t origin_guid = 0; 1085 nvlist_t *origin_nv; 1086 1087 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); 1088 if (nvlist_lookup_boolean(fslist, "sent") == 0) 1089 continue; 1090 1091 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0); 1092 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); 1093 1094 origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL); 1095 if (origin_nv && 1096 nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) { 1097 /* 1098 * origin has not been sent yet; 1099 * skip this clone. 1100 */ 1101 needagain = B_TRUE; 1102 continue; 1103 } 1104 1105 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); 1106 if (zhp == NULL) 1107 return (-1); 1108 err = dump_filesystem(zhp, sdd); 1109 VERIFY(nvlist_add_boolean(fslist, "sent") == 0); 1110 progress = B_TRUE; 1111 zfs_close(zhp); 1112 if (err) 1113 return (err); 1114 } 1115 if (needagain) { 1116 assert(progress); 1117 goto again; 1118 } 1119 return (0); 1120 } 1121 1122 /* 1123 * Generate a send stream for the dataset identified by the argument zhp. 1124 * 1125 * The content of the send stream is the snapshot identified by 1126 * 'tosnap'. Incremental streams are requested in two ways: 1127 * - from the snapshot identified by "fromsnap" (if non-null) or 1128 * - from the origin of the dataset identified by zhp, which must 1129 * be a clone. In this case, "fromsnap" is null and "fromorigin" 1130 * is TRUE. 1131 * 1132 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and 1133 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM) 1134 * if "replicate" is set. If "doall" is set, dump all the intermediate 1135 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall" 1136 * case too. If "props" is set, send properties. 1137 */ 1138 int 1139 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 1140 sendflags_t flags, int outfd, snapfilter_cb_t filter_func, 1141 void *cb_arg) 1142 { 1143 char errbuf[1024]; 1144 send_dump_data_t sdd = { 0 }; 1145 int err; 1146 nvlist_t *fss = NULL; 1147 avl_tree_t *fsavl = NULL; 1148 char holdtag[128]; 1149 static uint64_t holdseq; 1150 int spa_version; 1151 boolean_t holdsnaps = B_FALSE; 1152 pthread_t tid; 1153 int pipefd[2]; 1154 dedup_arg_t dda = { 0 }; 1155 int featureflags = 0; 1156 1157 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1158 "cannot send '%s'"), zhp->zfs_name); 1159 1160 if (fromsnap && fromsnap[0] == '\0') { 1161 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 1162 "zero-length incremental source")); 1163 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 1164 } 1165 1166 if (zfs_spa_version(zhp, &spa_version) == 0 && 1167 spa_version >= SPA_VERSION_USERREFS) 1168 holdsnaps = B_TRUE; 1169 1170 if (flags.dedup) { 1171 featureflags |= (DMU_BACKUP_FEATURE_DEDUP | 1172 DMU_BACKUP_FEATURE_DEDUPPROPS); 1173 if (err = pipe(pipefd)) { 1174 zfs_error_aux(zhp->zfs_hdl, strerror(errno)); 1175 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, 1176 errbuf)); 1177 } 1178 dda.outputfd = outfd; 1179 dda.inputfd = pipefd[1]; 1180 dda.dedup_hdl = zhp->zfs_hdl; 1181 if (err = pthread_create(&tid, NULL, cksummer, &dda)) { 1182 (void) close(pipefd[0]); 1183 (void) close(pipefd[1]); 1184 zfs_error_aux(zhp->zfs_hdl, strerror(errno)); 1185 return (zfs_error(zhp->zfs_hdl, 1186 EZFS_THREADCREATEFAILED, errbuf)); 1187 } 1188 } 1189 1190 if (flags.replicate || flags.doall || flags.props) { 1191 dmu_replay_record_t drr = { 0 }; 1192 char *packbuf = NULL; 1193 size_t buflen = 0; 1194 zio_cksum_t zc = { 0 }; 1195 1196 if (holdsnaps) { 1197 (void) snprintf(holdtag, sizeof (holdtag), 1198 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq); 1199 ++holdseq; 1200 err = zfs_hold_range(zhp, fromsnap, tosnap, 1201 holdtag, flags.replicate, B_TRUE, filter_func, 1202 cb_arg); 1203 if (err) 1204 goto err_out; 1205 } 1206 1207 if (flags.replicate || flags.props) { 1208 nvlist_t *hdrnv; 1209 1210 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0)); 1211 if (fromsnap) { 1212 VERIFY(0 == nvlist_add_string(hdrnv, 1213 "fromsnap", fromsnap)); 1214 } 1215 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap)); 1216 if (!flags.replicate) { 1217 VERIFY(0 == nvlist_add_boolean(hdrnv, 1218 "not_recursive")); 1219 } 1220 1221 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name, 1222 fromsnap, tosnap, flags.replicate, &fss, &fsavl); 1223 if (err) { 1224 if (holdsnaps) { 1225 (void) zfs_release_range(zhp, fromsnap, 1226 tosnap, holdtag, flags.replicate); 1227 } 1228 goto err_out; 1229 } 1230 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss)); 1231 err = nvlist_pack(hdrnv, &packbuf, &buflen, 1232 NV_ENCODE_XDR, 0); 1233 nvlist_free(hdrnv); 1234 if (err) { 1235 fsavl_destroy(fsavl); 1236 nvlist_free(fss); 1237 if (holdsnaps) { 1238 (void) zfs_release_range(zhp, fromsnap, 1239 tosnap, holdtag, flags.replicate); 1240 } 1241 goto stderr_out; 1242 } 1243 } 1244 1245 /* write first begin record */ 1246 drr.drr_type = DRR_BEGIN; 1247 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 1248 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.drr_versioninfo, 1249 DMU_COMPOUNDSTREAM); 1250 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.drr_versioninfo, 1251 featureflags); 1252 (void) snprintf(drr.drr_u.drr_begin.drr_toname, 1253 sizeof (drr.drr_u.drr_begin.drr_toname), 1254 "%s@%s", zhp->zfs_name, tosnap); 1255 drr.drr_payloadlen = buflen; 1256 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd); 1257 1258 /* write header nvlist */ 1259 if (err != -1 && packbuf != NULL) { 1260 err = cksum_and_write(packbuf, buflen, &zc, outfd); 1261 } 1262 free(packbuf); 1263 if (err == -1) { 1264 fsavl_destroy(fsavl); 1265 nvlist_free(fss); 1266 if (holdsnaps) { 1267 (void) zfs_release_range(zhp, fromsnap, tosnap, 1268 holdtag, flags.replicate); 1269 } 1270 err = errno; 1271 goto stderr_out; 1272 } 1273 1274 /* write end record */ 1275 if (err != -1) { 1276 bzero(&drr, sizeof (drr)); 1277 drr.drr_type = DRR_END; 1278 drr.drr_u.drr_end.drr_checksum = zc; 1279 err = write(outfd, &drr, sizeof (drr)); 1280 if (err == -1) { 1281 fsavl_destroy(fsavl); 1282 nvlist_free(fss); 1283 err = errno; 1284 if (holdsnaps) { 1285 (void) zfs_release_range(zhp, fromsnap, 1286 tosnap, holdtag, flags.replicate); 1287 } 1288 goto stderr_out; 1289 } 1290 } 1291 } 1292 1293 /* dump each stream */ 1294 sdd.fromsnap = fromsnap; 1295 sdd.tosnap = tosnap; 1296 if (flags.dedup) 1297 sdd.outfd = pipefd[0]; 1298 else 1299 sdd.outfd = outfd; 1300 sdd.replicate = flags.replicate; 1301 sdd.doall = flags.doall; 1302 sdd.fromorigin = flags.fromorigin; 1303 sdd.fss = fss; 1304 sdd.fsavl = fsavl; 1305 sdd.verbose = flags.verbose; 1306 sdd.filter_cb = filter_func; 1307 sdd.filter_cb_arg = cb_arg; 1308 err = dump_filesystems(zhp, &sdd); 1309 fsavl_destroy(fsavl); 1310 nvlist_free(fss); 1311 1312 if (flags.dedup) { 1313 (void) close(pipefd[0]); 1314 (void) pthread_join(tid, NULL); 1315 } 1316 1317 if (flags.replicate || flags.doall || flags.props) { 1318 /* 1319 * write final end record. NB: want to do this even if 1320 * there was some error, because it might not be totally 1321 * failed. 1322 */ 1323 dmu_replay_record_t drr = { 0 }; 1324 drr.drr_type = DRR_END; 1325 if (holdsnaps) { 1326 (void) zfs_release_range(zhp, fromsnap, tosnap, 1327 holdtag, flags.replicate); 1328 } 1329 if (write(outfd, &drr, sizeof (drr)) == -1) { 1330 return (zfs_standard_error(zhp->zfs_hdl, 1331 errno, errbuf)); 1332 } 1333 } 1334 1335 return (err || sdd.err); 1336 1337 stderr_out: 1338 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf); 1339 err_out: 1340 if (flags.dedup) { 1341 (void) pthread_cancel(tid); 1342 (void) pthread_join(tid, NULL); 1343 (void) close(pipefd[0]); 1344 } 1345 return (err); 1346 } 1347 1348 /* 1349 * Routines specific to "zfs recv" 1350 */ 1351 1352 static int 1353 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, 1354 boolean_t byteswap, zio_cksum_t *zc) 1355 { 1356 char *cp = buf; 1357 int rv; 1358 int len = ilen; 1359 1360 do { 1361 rv = read(fd, cp, len); 1362 cp += rv; 1363 len -= rv; 1364 } while (rv > 0); 1365 1366 if (rv < 0 || len != 0) { 1367 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1368 "failed to read from stream")); 1369 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, 1370 "cannot receive"))); 1371 } 1372 1373 if (zc) { 1374 if (byteswap) 1375 fletcher_4_incremental_byteswap(buf, ilen, zc); 1376 else 1377 fletcher_4_incremental_native(buf, ilen, zc); 1378 } 1379 return (0); 1380 } 1381 1382 static int 1383 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, 1384 boolean_t byteswap, zio_cksum_t *zc) 1385 { 1386 char *buf; 1387 int err; 1388 1389 buf = zfs_alloc(hdl, len); 1390 if (buf == NULL) 1391 return (ENOMEM); 1392 1393 err = recv_read(hdl, fd, buf, len, byteswap, zc); 1394 if (err != 0) { 1395 free(buf); 1396 return (err); 1397 } 1398 1399 err = nvlist_unpack(buf, len, nvp, 0); 1400 free(buf); 1401 if (err != 0) { 1402 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 1403 "stream (malformed nvlist)")); 1404 return (EINVAL); 1405 } 1406 return (0); 1407 } 1408 1409 static int 1410 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, 1411 int baselen, char *newname, recvflags_t flags) 1412 { 1413 static int seq; 1414 zfs_cmd_t zc = { 0 }; 1415 int err; 1416 prop_changelist_t *clp; 1417 zfs_handle_t *zhp; 1418 1419 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 1420 if (zhp == NULL) 1421 return (-1); 1422 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 1423 flags.force ? MS_FORCE : 0); 1424 zfs_close(zhp); 1425 if (clp == NULL) 1426 return (-1); 1427 err = changelist_prefix(clp); 1428 if (err) 1429 return (err); 1430 1431 zc.zc_objset_type = DMU_OST_ZFS; 1432 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); 1433 1434 if (tryname) { 1435 (void) strcpy(newname, tryname); 1436 1437 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value)); 1438 1439 if (flags.verbose) { 1440 (void) printf("attempting rename %s to %s\n", 1441 zc.zc_name, zc.zc_value); 1442 } 1443 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); 1444 if (err == 0) 1445 changelist_rename(clp, name, tryname); 1446 } else { 1447 err = ENOENT; 1448 } 1449 1450 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) { 1451 seq++; 1452 1453 (void) strncpy(newname, name, baselen); 1454 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen, 1455 "recv-%u-%u", getpid(), seq); 1456 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value)); 1457 1458 if (flags.verbose) { 1459 (void) printf("failed - trying rename %s to %s\n", 1460 zc.zc_name, zc.zc_value); 1461 } 1462 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); 1463 if (err == 0) 1464 changelist_rename(clp, name, newname); 1465 if (err && flags.verbose) { 1466 (void) printf("failed (%u) - " 1467 "will try again on next pass\n", errno); 1468 } 1469 err = EAGAIN; 1470 } else if (flags.verbose) { 1471 if (err == 0) 1472 (void) printf("success\n"); 1473 else 1474 (void) printf("failed (%u)\n", errno); 1475 } 1476 1477 (void) changelist_postfix(clp); 1478 changelist_free(clp); 1479 1480 return (err); 1481 } 1482 1483 static int 1484 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, 1485 char *newname, recvflags_t flags) 1486 { 1487 zfs_cmd_t zc = { 0 }; 1488 int err = 0; 1489 prop_changelist_t *clp; 1490 zfs_handle_t *zhp; 1491 boolean_t defer = B_FALSE; 1492 int spa_version; 1493 1494 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 1495 if (zhp == NULL) 1496 return (-1); 1497 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 1498 flags.force ? MS_FORCE : 0); 1499 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 1500 zfs_spa_version(zhp, &spa_version) == 0 && 1501 spa_version >= SPA_VERSION_USERREFS) 1502 defer = B_TRUE; 1503 zfs_close(zhp); 1504 if (clp == NULL) 1505 return (-1); 1506 err = changelist_prefix(clp); 1507 if (err) 1508 return (err); 1509 1510 zc.zc_objset_type = DMU_OST_ZFS; 1511 zc.zc_defer_destroy = defer; 1512 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); 1513 1514 if (flags.verbose) 1515 (void) printf("attempting destroy %s\n", zc.zc_name); 1516 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc); 1517 if (err == 0) { 1518 if (flags.verbose) 1519 (void) printf("success\n"); 1520 changelist_remove(clp, zc.zc_name); 1521 } 1522 1523 (void) changelist_postfix(clp); 1524 changelist_free(clp); 1525 1526 /* 1527 * Deferred destroy might destroy the snapshot or only mark it to be 1528 * destroyed later, and it returns success in either case. 1529 */ 1530 if (err != 0 || (defer && zfs_dataset_exists(hdl, name, 1531 ZFS_TYPE_SNAPSHOT))) { 1532 err = recv_rename(hdl, name, NULL, baselen, newname, flags); 1533 } 1534 1535 return (err); 1536 } 1537 1538 typedef struct guid_to_name_data { 1539 uint64_t guid; 1540 char *name; 1541 } guid_to_name_data_t; 1542 1543 static int 1544 guid_to_name_cb(zfs_handle_t *zhp, void *arg) 1545 { 1546 guid_to_name_data_t *gtnd = arg; 1547 int err; 1548 1549 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) { 1550 (void) strcpy(gtnd->name, zhp->zfs_name); 1551 zfs_close(zhp); 1552 return (EEXIST); 1553 } 1554 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); 1555 zfs_close(zhp); 1556 return (err); 1557 } 1558 1559 static int 1560 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, 1561 char *name) 1562 { 1563 /* exhaustive search all local snapshots */ 1564 guid_to_name_data_t gtnd; 1565 int err = 0; 1566 zfs_handle_t *zhp; 1567 char *cp; 1568 1569 gtnd.guid = guid; 1570 gtnd.name = name; 1571 1572 if (strchr(parent, '@') == NULL) { 1573 zhp = make_dataset_handle(hdl, parent); 1574 if (zhp != NULL) { 1575 err = zfs_iter_children(zhp, guid_to_name_cb, >nd); 1576 zfs_close(zhp); 1577 if (err == EEXIST) 1578 return (0); 1579 } 1580 } 1581 1582 cp = strchr(parent, '/'); 1583 if (cp) 1584 *cp = '\0'; 1585 zhp = make_dataset_handle(hdl, parent); 1586 if (cp) 1587 *cp = '/'; 1588 1589 if (zhp) { 1590 err = zfs_iter_children(zhp, guid_to_name_cb, >nd); 1591 zfs_close(zhp); 1592 } 1593 1594 return (err == EEXIST ? 0 : ENOENT); 1595 1596 } 1597 1598 /* 1599 * Return true if dataset guid1 is created before guid2. 1600 */ 1601 static int 1602 created_before(libzfs_handle_t *hdl, avl_tree_t *avl, 1603 uint64_t guid1, uint64_t guid2) 1604 { 1605 nvlist_t *nvfs; 1606 char *fsname, *snapname; 1607 char buf[ZFS_MAXNAMELEN]; 1608 int rv; 1609 zfs_node_t zn1, zn2; 1610 1611 if (guid2 == 0) 1612 return (0); 1613 if (guid1 == 0) 1614 return (1); 1615 1616 nvfs = fsavl_find(avl, guid1, &snapname); 1617 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 1618 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 1619 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 1620 if (zn1.zn_handle == NULL) 1621 return (-1); 1622 1623 nvfs = fsavl_find(avl, guid2, &snapname); 1624 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 1625 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 1626 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 1627 if (zn2.zn_handle == NULL) { 1628 zfs_close(zn2.zn_handle); 1629 return (-1); 1630 } 1631 1632 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1); 1633 1634 zfs_close(zn1.zn_handle); 1635 zfs_close(zn2.zn_handle); 1636 1637 return (rv); 1638 } 1639 1640 static int 1641 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, 1642 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl) 1643 { 1644 nvlist_t *local_nv; 1645 avl_tree_t *local_avl; 1646 nvpair_t *fselem, *nextfselem; 1647 char *tosnap, *fromsnap; 1648 char newname[ZFS_MAXNAMELEN]; 1649 int error; 1650 boolean_t needagain, progress, recursive; 1651 char *s1, *s2; 1652 1653 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap)); 1654 VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap)); 1655 1656 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 1657 ENOENT); 1658 1659 if (flags.dryrun) 1660 return (0); 1661 1662 again: 1663 needagain = progress = B_FALSE; 1664 1665 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, 1666 recursive, &local_nv, &local_avl)) != 0) 1667 return (error); 1668 1669 /* 1670 * Process deletes and renames 1671 */ 1672 for (fselem = nvlist_next_nvpair(local_nv, NULL); 1673 fselem; fselem = nextfselem) { 1674 nvlist_t *nvfs, *snaps; 1675 nvlist_t *stream_nvfs = NULL; 1676 nvpair_t *snapelem, *nextsnapelem; 1677 uint64_t fromguid = 0; 1678 uint64_t originguid = 0; 1679 uint64_t stream_originguid = 0; 1680 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; 1681 char *fsname, *stream_fsname; 1682 1683 nextfselem = nvlist_next_nvpair(local_nv, fselem); 1684 1685 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); 1686 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); 1687 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 1688 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap", 1689 &parent_fromsnap_guid)); 1690 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); 1691 1692 /* 1693 * First find the stream's fs, so we can check for 1694 * a different origin (due to "zfs promote") 1695 */ 1696 for (snapelem = nvlist_next_nvpair(snaps, NULL); 1697 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { 1698 uint64_t thisguid; 1699 1700 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); 1701 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); 1702 1703 if (stream_nvfs != NULL) 1704 break; 1705 } 1706 1707 /* check for promote */ 1708 (void) nvlist_lookup_uint64(stream_nvfs, "origin", 1709 &stream_originguid); 1710 if (stream_nvfs && originguid != stream_originguid) { 1711 switch (created_before(hdl, local_avl, 1712 stream_originguid, originguid)) { 1713 case 1: { 1714 /* promote it! */ 1715 zfs_cmd_t zc = { 0 }; 1716 nvlist_t *origin_nvfs; 1717 char *origin_fsname; 1718 1719 if (flags.verbose) 1720 (void) printf("promoting %s\n", fsname); 1721 1722 origin_nvfs = fsavl_find(local_avl, originguid, 1723 NULL); 1724 VERIFY(0 == nvlist_lookup_string(origin_nvfs, 1725 "name", &origin_fsname)); 1726 (void) strlcpy(zc.zc_value, origin_fsname, 1727 sizeof (zc.zc_value)); 1728 (void) strlcpy(zc.zc_name, fsname, 1729 sizeof (zc.zc_name)); 1730 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 1731 if (error == 0) 1732 progress = B_TRUE; 1733 break; 1734 } 1735 default: 1736 break; 1737 case -1: 1738 fsavl_destroy(local_avl); 1739 nvlist_free(local_nv); 1740 return (-1); 1741 } 1742 /* 1743 * We had/have the wrong origin, therefore our 1744 * list of snapshots is wrong. Need to handle 1745 * them on the next pass. 1746 */ 1747 needagain = B_TRUE; 1748 continue; 1749 } 1750 1751 for (snapelem = nvlist_next_nvpair(snaps, NULL); 1752 snapelem; snapelem = nextsnapelem) { 1753 uint64_t thisguid; 1754 char *stream_snapname; 1755 nvlist_t *found, *props; 1756 1757 nextsnapelem = nvlist_next_nvpair(snaps, snapelem); 1758 1759 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); 1760 found = fsavl_find(stream_avl, thisguid, 1761 &stream_snapname); 1762 1763 /* check for delete */ 1764 if (found == NULL) { 1765 char name[ZFS_MAXNAMELEN]; 1766 1767 if (!flags.force) 1768 continue; 1769 1770 (void) snprintf(name, sizeof (name), "%s@%s", 1771 fsname, nvpair_name(snapelem)); 1772 1773 error = recv_destroy(hdl, name, 1774 strlen(fsname)+1, newname, flags); 1775 if (error) 1776 needagain = B_TRUE; 1777 else 1778 progress = B_TRUE; 1779 continue; 1780 } 1781 1782 stream_nvfs = found; 1783 1784 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops", 1785 &props) && 0 == nvlist_lookup_nvlist(props, 1786 stream_snapname, &props)) { 1787 zfs_cmd_t zc = { 0 }; 1788 1789 zc.zc_cookie = B_TRUE; /* received */ 1790 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), 1791 "%s@%s", fsname, nvpair_name(snapelem)); 1792 if (zcmd_write_src_nvlist(hdl, &zc, 1793 props) == 0) { 1794 (void) zfs_ioctl(hdl, 1795 ZFS_IOC_SET_PROP, &zc); 1796 zcmd_free_nvlists(&zc); 1797 } 1798 } 1799 1800 /* check for different snapname */ 1801 if (strcmp(nvpair_name(snapelem), 1802 stream_snapname) != 0) { 1803 char name[ZFS_MAXNAMELEN]; 1804 char tryname[ZFS_MAXNAMELEN]; 1805 1806 (void) snprintf(name, sizeof (name), "%s@%s", 1807 fsname, nvpair_name(snapelem)); 1808 (void) snprintf(tryname, sizeof (name), "%s@%s", 1809 fsname, stream_snapname); 1810 1811 error = recv_rename(hdl, name, tryname, 1812 strlen(fsname)+1, newname, flags); 1813 if (error) 1814 needagain = B_TRUE; 1815 else 1816 progress = B_TRUE; 1817 } 1818 1819 if (strcmp(stream_snapname, fromsnap) == 0) 1820 fromguid = thisguid; 1821 } 1822 1823 /* check for delete */ 1824 if (stream_nvfs == NULL) { 1825 if (!flags.force) 1826 continue; 1827 1828 error = recv_destroy(hdl, fsname, strlen(tofs)+1, 1829 newname, flags); 1830 if (error) 1831 needagain = B_TRUE; 1832 else 1833 progress = B_TRUE; 1834 continue; 1835 } 1836 1837 if (fromguid == 0 && flags.verbose) { 1838 (void) printf("local fs %s does not have fromsnap " 1839 "(%s in stream); must have been deleted locally; " 1840 "ignoring\n", fsname, fromsnap); 1841 continue; 1842 } 1843 1844 VERIFY(0 == nvlist_lookup_string(stream_nvfs, 1845 "name", &stream_fsname)); 1846 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs, 1847 "parentfromsnap", &stream_parent_fromsnap_guid)); 1848 1849 s1 = strrchr(fsname, '/'); 1850 s2 = strrchr(stream_fsname, '/'); 1851 1852 /* check for rename */ 1853 if ((stream_parent_fromsnap_guid != 0 && 1854 stream_parent_fromsnap_guid != parent_fromsnap_guid) || 1855 ((s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) { 1856 nvlist_t *parent; 1857 char tryname[ZFS_MAXNAMELEN]; 1858 1859 parent = fsavl_find(local_avl, 1860 stream_parent_fromsnap_guid, NULL); 1861 /* 1862 * NB: parent might not be found if we used the 1863 * tosnap for stream_parent_fromsnap_guid, 1864 * because the parent is a newly-created fs; 1865 * we'll be able to rename it after we recv the 1866 * new fs. 1867 */ 1868 if (parent != NULL) { 1869 char *pname; 1870 1871 VERIFY(0 == nvlist_lookup_string(parent, "name", 1872 &pname)); 1873 (void) snprintf(tryname, sizeof (tryname), 1874 "%s%s", pname, strrchr(stream_fsname, '/')); 1875 } else { 1876 tryname[0] = '\0'; 1877 if (flags.verbose) { 1878 (void) printf("local fs %s new parent " 1879 "not found\n", fsname); 1880 } 1881 } 1882 1883 error = recv_rename(hdl, fsname, tryname, 1884 strlen(tofs)+1, newname, flags); 1885 if (error) 1886 needagain = B_TRUE; 1887 else 1888 progress = B_TRUE; 1889 } 1890 } 1891 1892 fsavl_destroy(local_avl); 1893 nvlist_free(local_nv); 1894 1895 if (needagain && progress) { 1896 /* do another pass to fix up temporary names */ 1897 if (flags.verbose) 1898 (void) printf("another pass:\n"); 1899 goto again; 1900 } 1901 1902 return (needagain); 1903 } 1904 1905 static int 1906 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, 1907 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc, 1908 char **top_zfs) 1909 { 1910 nvlist_t *stream_nv = NULL; 1911 avl_tree_t *stream_avl = NULL; 1912 char *fromsnap = NULL; 1913 char tofs[ZFS_MAXNAMELEN]; 1914 char errbuf[1024]; 1915 dmu_replay_record_t drre; 1916 int error; 1917 boolean_t anyerr = B_FALSE; 1918 boolean_t softerr = B_FALSE; 1919 1920 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1921 "cannot receive")); 1922 1923 if (strchr(destname, '@')) { 1924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1925 "can not specify snapshot name for multi-snapshot stream")); 1926 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 1927 } 1928 1929 assert(drr->drr_type == DRR_BEGIN); 1930 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); 1931 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) == 1932 DMU_COMPOUNDSTREAM); 1933 1934 /* 1935 * Read in the nvlist from the stream. 1936 */ 1937 if (drr->drr_payloadlen != 0) { 1938 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, 1939 &stream_nv, flags.byteswap, zc); 1940 if (error) { 1941 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1942 goto out; 1943 } 1944 } 1945 1946 /* 1947 * Read in the end record and verify checksum. 1948 */ 1949 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), 1950 flags.byteswap, NULL))) 1951 goto out; 1952 if (flags.byteswap) { 1953 drre.drr_type = BSWAP_32(drre.drr_type); 1954 drre.drr_u.drr_end.drr_checksum.zc_word[0] = 1955 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); 1956 drre.drr_u.drr_end.drr_checksum.zc_word[1] = 1957 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); 1958 drre.drr_u.drr_end.drr_checksum.zc_word[2] = 1959 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); 1960 drre.drr_u.drr_end.drr_checksum.zc_word[3] = 1961 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); 1962 } 1963 if (drre.drr_type != DRR_END) { 1964 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1965 goto out; 1966 } 1967 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { 1968 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1969 "incorrect header checksum")); 1970 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 1971 goto out; 1972 } 1973 1974 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); 1975 1976 if (drr->drr_payloadlen != 0) { 1977 nvlist_t *stream_fss; 1978 1979 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", 1980 &stream_fss)); 1981 if ((stream_avl = fsavl_create(stream_fss)) == NULL) { 1982 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1983 "couldn't allocate avl tree")); 1984 error = zfs_error(hdl, EZFS_NOMEM, errbuf); 1985 goto out; 1986 } 1987 1988 if (fromsnap != NULL) { 1989 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN); 1990 if (flags.isprefix) { 1991 int i = strcspn(drr->drr_u.drr_begin.drr_toname, 1992 "/@"); 1993 /* zfs_receive_one() will create_parents() */ 1994 (void) strlcat(tofs, 1995 &drr->drr_u.drr_begin.drr_toname[i], 1996 ZFS_MAXNAMELEN); 1997 *strchr(tofs, '@') = '\0'; 1998 } 1999 softerr = recv_incremental_replication(hdl, tofs, 2000 flags, stream_nv, stream_avl); 2001 } 2002 } 2003 2004 2005 /* Finally, receive each contained stream */ 2006 do { 2007 /* 2008 * we should figure out if it has a recoverable 2009 * error, in which case do a recv_skip() and drive on. 2010 * Note, if we fail due to already having this guid, 2011 * zfs_receive_one() will take care of it (ie, 2012 * recv_skip() and return 0). 2013 */ 2014 error = zfs_receive_impl(hdl, destname, flags, fd, 2015 stream_avl, top_zfs); 2016 if (error == ENODATA) { 2017 error = 0; 2018 break; 2019 } 2020 anyerr |= error; 2021 } while (error == 0); 2022 2023 if (drr->drr_payloadlen != 0 && fromsnap != NULL) { 2024 /* 2025 * Now that we have the fs's they sent us, try the 2026 * renames again. 2027 */ 2028 softerr = recv_incremental_replication(hdl, tofs, flags, 2029 stream_nv, stream_avl); 2030 } 2031 2032 out: 2033 fsavl_destroy(stream_avl); 2034 if (stream_nv) 2035 nvlist_free(stream_nv); 2036 if (softerr) 2037 error = -2; 2038 if (anyerr) 2039 error = -1; 2040 return (error); 2041 } 2042 2043 static void 2044 trunc_prop_errs(int truncated) 2045 { 2046 ASSERT(truncated != 0); 2047 2048 if (truncated == 1) 2049 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 2050 "1 more property could not be set\n")); 2051 else 2052 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 2053 "%d more properties could not be set\n"), truncated); 2054 } 2055 2056 static int 2057 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) 2058 { 2059 dmu_replay_record_t *drr; 2060 void *buf = malloc(1<<20); 2061 char errbuf[1024]; 2062 2063 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2064 "cannot receive:")); 2065 2066 /* XXX would be great to use lseek if possible... */ 2067 drr = buf; 2068 2069 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), 2070 byteswap, NULL) == 0) { 2071 if (byteswap) 2072 drr->drr_type = BSWAP_32(drr->drr_type); 2073 2074 switch (drr->drr_type) { 2075 case DRR_BEGIN: 2076 /* NB: not to be used on v2 stream packages */ 2077 if (drr->drr_payloadlen != 0) { 2078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2079 "invalid substream header")); 2080 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 2081 } 2082 break; 2083 2084 case DRR_END: 2085 free(buf); 2086 return (0); 2087 2088 case DRR_OBJECT: 2089 if (byteswap) { 2090 drr->drr_u.drr_object.drr_bonuslen = 2091 BSWAP_32(drr->drr_u.drr_object. 2092 drr_bonuslen); 2093 } 2094 (void) recv_read(hdl, fd, buf, 2095 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8), 2096 B_FALSE, NULL); 2097 break; 2098 2099 case DRR_WRITE: 2100 if (byteswap) { 2101 drr->drr_u.drr_write.drr_length = 2102 BSWAP_64(drr->drr_u.drr_write.drr_length); 2103 } 2104 (void) recv_read(hdl, fd, buf, 2105 drr->drr_u.drr_write.drr_length, B_FALSE, NULL); 2106 break; 2107 2108 case DRR_WRITE_BYREF: 2109 case DRR_FREEOBJECTS: 2110 case DRR_FREE: 2111 break; 2112 2113 default: 2114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2115 "invalid record type")); 2116 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 2117 } 2118 } 2119 2120 free(buf); 2121 return (-1); 2122 } 2123 2124 /* 2125 * Restores a backup of tosnap from the file descriptor specified by infd. 2126 */ 2127 static int 2128 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, 2129 recvflags_t flags, dmu_replay_record_t *drr, 2130 dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl, 2131 char **top_zfs) 2132 { 2133 zfs_cmd_t zc = { 0 }; 2134 time_t begin_time; 2135 int ioctl_err, ioctl_errno, err, choplen; 2136 char *cp; 2137 struct drr_begin *drrb = &drr->drr_u.drr_begin; 2138 char errbuf[1024]; 2139 char prop_errbuf[1024]; 2140 char chopprefix[ZFS_MAXNAMELEN]; 2141 boolean_t newfs = B_FALSE; 2142 boolean_t stream_wantsnewfs; 2143 uint64_t parent_snapguid = 0; 2144 prop_changelist_t *clp = NULL; 2145 nvlist_t *snapprops_nvlist = NULL; 2146 zprop_errflags_t prop_errflags; 2147 2148 begin_time = time(NULL); 2149 2150 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2151 "cannot receive")); 2152 2153 if (stream_avl != NULL) { 2154 char *snapname; 2155 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, 2156 &snapname); 2157 nvlist_t *props; 2158 int ret; 2159 2160 (void) nvlist_lookup_uint64(fs, "parentfromsnap", 2161 &parent_snapguid); 2162 err = nvlist_lookup_nvlist(fs, "props", &props); 2163 if (err) 2164 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0)); 2165 2166 if (flags.canmountoff) { 2167 VERIFY(0 == nvlist_add_uint64(props, 2168 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0)); 2169 } 2170 ret = zcmd_write_src_nvlist(hdl, &zc, props); 2171 if (err) 2172 nvlist_free(props); 2173 2174 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) { 2175 VERIFY(0 == nvlist_lookup_nvlist(props, 2176 snapname, &snapprops_nvlist)); 2177 } 2178 2179 if (ret != 0) 2180 return (-1); 2181 } 2182 2183 /* 2184 * Determine how much of the snapshot name stored in the stream 2185 * we are going to tack on to the name they specified on the 2186 * command line, and how much we are going to chop off. 2187 * 2188 * If they specified a snapshot, chop the entire name stored in 2189 * the stream. 2190 */ 2191 (void) strcpy(chopprefix, drrb->drr_toname); 2192 if (flags.isprefix) { 2193 /* 2194 * They specified a fs with -d or -e. We want to tack on 2195 * everything but the first element of the sent snapshot path 2196 * (all but the pool name) in the case of -d, or only the tail 2197 * of the sent snapshot path in the case of -e. 2198 */ 2199 if (strchr(tosnap, '@')) { 2200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 2201 "argument - snapshot not allowed with %s"), 2202 (flags.istail ? "-e" : "-d")); 2203 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2204 } 2205 cp = (flags.istail ? strrchr(chopprefix, '/') : 2206 strchr(chopprefix, '/')); 2207 if (cp == NULL) 2208 cp = strchr(chopprefix, '@'); 2209 *cp = '\0'; 2210 } else if (strchr(tosnap, '@') == NULL) { 2211 /* 2212 * If they specified a filesystem without -d or -e, we want to 2213 * tack on everything after the fs specified in the first name 2214 * from the stream. 2215 */ 2216 cp = strchr(chopprefix, '@'); 2217 *cp = '\0'; 2218 } 2219 choplen = strlen(chopprefix); 2220 2221 /* 2222 * Determine name of destination snapshot, store in zc_value. 2223 */ 2224 (void) strcpy(zc.zc_top_ds, tosnap); 2225 (void) strcpy(zc.zc_value, tosnap); 2226 (void) strncat(zc.zc_value, drrb->drr_toname+choplen, 2227 sizeof (zc.zc_value)); 2228 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) { 2229 zcmd_free_nvlists(&zc); 2230 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2231 } 2232 2233 /* 2234 * Determine the name of the origin snapshot, store in zc_string. 2235 */ 2236 if (drrb->drr_flags & DRR_FLAG_CLONE) { 2237 if (guid_to_name(hdl, tosnap, 2238 drrb->drr_fromguid, zc.zc_string) != 0) { 2239 zcmd_free_nvlists(&zc); 2240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2241 "local origin for clone %s does not exist"), 2242 zc.zc_value); 2243 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2244 } 2245 if (flags.verbose) 2246 (void) printf("found clone origin %s\n", zc.zc_string); 2247 } 2248 2249 stream_wantsnewfs = (drrb->drr_fromguid == NULL || 2250 (drrb->drr_flags & DRR_FLAG_CLONE)); 2251 2252 if (stream_wantsnewfs) { 2253 /* 2254 * if the parent fs does not exist, look for it based on 2255 * the parent snap GUID 2256 */ 2257 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2258 "cannot receive new filesystem stream")); 2259 2260 (void) strcpy(zc.zc_name, zc.zc_value); 2261 cp = strrchr(zc.zc_name, '/'); 2262 if (cp) 2263 *cp = '\0'; 2264 if (cp && 2265 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 2266 char suffix[ZFS_MAXNAMELEN]; 2267 (void) strcpy(suffix, strrchr(zc.zc_value, '/')); 2268 if (guid_to_name(hdl, tosnap, parent_snapguid, 2269 zc.zc_value) == 0) { 2270 *strchr(zc.zc_value, '@') = '\0'; 2271 (void) strcat(zc.zc_value, suffix); 2272 } 2273 } 2274 } else { 2275 /* 2276 * if the fs does not exist, look for it based on the 2277 * fromsnap GUID 2278 */ 2279 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2280 "cannot receive incremental stream")); 2281 2282 (void) strcpy(zc.zc_name, zc.zc_value); 2283 *strchr(zc.zc_name, '@') = '\0'; 2284 2285 if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 2286 char snap[ZFS_MAXNAMELEN]; 2287 (void) strcpy(snap, strchr(zc.zc_value, '@')); 2288 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid, 2289 zc.zc_value) == 0) { 2290 *strchr(zc.zc_value, '@') = '\0'; 2291 (void) strcat(zc.zc_value, snap); 2292 } 2293 } 2294 } 2295 2296 (void) strcpy(zc.zc_name, zc.zc_value); 2297 *strchr(zc.zc_name, '@') = '\0'; 2298 2299 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 2300 zfs_handle_t *zhp; 2301 /* 2302 * Destination fs exists. Therefore this should either 2303 * be an incremental, or the stream specifies a new fs 2304 * (full stream or clone) and they want us to blow it 2305 * away (and have therefore specified -F and removed any 2306 * snapshots). 2307 */ 2308 2309 if (stream_wantsnewfs) { 2310 if (!flags.force) { 2311 zcmd_free_nvlists(&zc); 2312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2313 "destination '%s' exists\n" 2314 "must specify -F to overwrite it"), 2315 zc.zc_name); 2316 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2317 } 2318 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2319 &zc) == 0) { 2320 zcmd_free_nvlists(&zc); 2321 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2322 "destination has snapshots (eg. %s)\n" 2323 "must destroy them to overwrite it"), 2324 zc.zc_name); 2325 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2326 } 2327 } 2328 2329 if ((zhp = zfs_open(hdl, zc.zc_name, 2330 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 2331 zcmd_free_nvlists(&zc); 2332 return (-1); 2333 } 2334 2335 if (stream_wantsnewfs && 2336 zhp->zfs_dmustats.dds_origin[0]) { 2337 zcmd_free_nvlists(&zc); 2338 zfs_close(zhp); 2339 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2340 "destination '%s' is a clone\n" 2341 "must destroy it to overwrite it"), 2342 zc.zc_name); 2343 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2344 } 2345 2346 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && 2347 stream_wantsnewfs) { 2348 /* We can't do online recv in this case */ 2349 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0); 2350 if (clp == NULL) { 2351 zfs_close(zhp); 2352 zcmd_free_nvlists(&zc); 2353 return (-1); 2354 } 2355 if (changelist_prefix(clp) != 0) { 2356 changelist_free(clp); 2357 zfs_close(zhp); 2358 zcmd_free_nvlists(&zc); 2359 return (-1); 2360 } 2361 } 2362 zfs_close(zhp); 2363 } else { 2364 /* 2365 * Destination filesystem does not exist. Therefore we better 2366 * be creating a new filesystem (either from a full backup, or 2367 * a clone). It would therefore be invalid if the user 2368 * specified only the pool name (i.e. if the destination name 2369 * contained no slash character). 2370 */ 2371 if (!stream_wantsnewfs || 2372 (cp = strrchr(zc.zc_name, '/')) == NULL) { 2373 zcmd_free_nvlists(&zc); 2374 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2375 "destination '%s' does not exist"), zc.zc_name); 2376 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2377 } 2378 2379 /* 2380 * Trim off the final dataset component so we perform the 2381 * recvbackup ioctl to the filesystems's parent. 2382 */ 2383 *cp = '\0'; 2384 2385 if (flags.isprefix && !flags.dryrun && 2386 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) { 2387 zcmd_free_nvlists(&zc); 2388 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 2389 } 2390 2391 newfs = B_TRUE; 2392 } 2393 2394 zc.zc_begin_record = drr_noswap->drr_u.drr_begin; 2395 zc.zc_cookie = infd; 2396 zc.zc_guid = flags.force; 2397 if (flags.verbose) { 2398 (void) printf("%s %s stream of %s into %s\n", 2399 flags.dryrun ? "would receive" : "receiving", 2400 drrb->drr_fromguid ? "incremental" : "full", 2401 drrb->drr_toname, zc.zc_value); 2402 (void) fflush(stdout); 2403 } 2404 2405 if (flags.dryrun) { 2406 zcmd_free_nvlists(&zc); 2407 return (recv_skip(hdl, infd, flags.byteswap)); 2408 } 2409 2410 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf; 2411 zc.zc_nvlist_dst_size = sizeof (prop_errbuf); 2412 2413 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc); 2414 ioctl_errno = errno; 2415 prop_errflags = (zprop_errflags_t)zc.zc_obj; 2416 2417 if (err == 0) { 2418 nvlist_t *prop_errors; 2419 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst, 2420 zc.zc_nvlist_dst_size, &prop_errors, 0)); 2421 2422 nvpair_t *prop_err = NULL; 2423 2424 while ((prop_err = nvlist_next_nvpair(prop_errors, 2425 prop_err)) != NULL) { 2426 char tbuf[1024]; 2427 zfs_prop_t prop; 2428 int intval; 2429 2430 prop = zfs_name_to_prop(nvpair_name(prop_err)); 2431 (void) nvpair_value_int32(prop_err, &intval); 2432 if (strcmp(nvpair_name(prop_err), 2433 ZPROP_N_MORE_ERRORS) == 0) { 2434 trunc_prop_errs(intval); 2435 break; 2436 } else { 2437 (void) snprintf(tbuf, sizeof (tbuf), 2438 dgettext(TEXT_DOMAIN, 2439 "cannot receive %s property on %s"), 2440 nvpair_name(prop_err), zc.zc_name); 2441 zfs_setprop_error(hdl, prop, intval, tbuf); 2442 } 2443 } 2444 nvlist_free(prop_errors); 2445 } 2446 2447 zc.zc_nvlist_dst = 0; 2448 zc.zc_nvlist_dst_size = 0; 2449 zcmd_free_nvlists(&zc); 2450 2451 if (err == 0 && snapprops_nvlist) { 2452 zfs_cmd_t zc2 = { 0 }; 2453 2454 (void) strcpy(zc2.zc_name, zc.zc_value); 2455 zc2.zc_cookie = B_TRUE; /* received */ 2456 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) { 2457 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2); 2458 zcmd_free_nvlists(&zc2); 2459 } 2460 } 2461 2462 if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) { 2463 /* 2464 * It may be that this snapshot already exists, 2465 * in which case we want to consume & ignore it 2466 * rather than failing. 2467 */ 2468 avl_tree_t *local_avl; 2469 nvlist_t *local_nv, *fs; 2470 char *cp = strchr(zc.zc_value, '@'); 2471 2472 /* 2473 * XXX Do this faster by just iterating over snaps in 2474 * this fs. Also if zc_value does not exist, we will 2475 * get a strange "does not exist" error message. 2476 */ 2477 *cp = '\0'; 2478 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE, 2479 &local_nv, &local_avl) == 0) { 2480 *cp = '@'; 2481 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); 2482 fsavl_destroy(local_avl); 2483 nvlist_free(local_nv); 2484 2485 if (fs != NULL) { 2486 if (flags.verbose) { 2487 (void) printf("snap %s already exists; " 2488 "ignoring\n", zc.zc_value); 2489 } 2490 err = ioctl_err = recv_skip(hdl, infd, 2491 flags.byteswap); 2492 } 2493 } 2494 *cp = '@'; 2495 } 2496 2497 if (ioctl_err != 0) { 2498 switch (ioctl_errno) { 2499 case ENODEV: 2500 cp = strchr(zc.zc_value, '@'); 2501 *cp = '\0'; 2502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2503 "most recent snapshot of %s does not\n" 2504 "match incremental source"), zc.zc_value); 2505 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 2506 *cp = '@'; 2507 break; 2508 case ETXTBSY: 2509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2510 "destination %s has been modified\n" 2511 "since most recent snapshot"), zc.zc_name); 2512 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 2513 break; 2514 case EEXIST: 2515 cp = strchr(zc.zc_value, '@'); 2516 if (newfs) { 2517 /* it's the containing fs that exists */ 2518 *cp = '\0'; 2519 } 2520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2521 "destination already exists")); 2522 (void) zfs_error_fmt(hdl, EZFS_EXISTS, 2523 dgettext(TEXT_DOMAIN, "cannot restore to %s"), 2524 zc.zc_value); 2525 *cp = '@'; 2526 break; 2527 case EINVAL: 2528 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 2529 break; 2530 case ECKSUM: 2531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2532 "invalid stream (checksum mismatch)")); 2533 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 2534 break; 2535 default: 2536 (void) zfs_standard_error(hdl, ioctl_errno, errbuf); 2537 } 2538 } 2539 2540 /* 2541 * Mount the target filesystem (if created). Also mount any 2542 * children of the target filesystem if we did a replication 2543 * receive (indicated by stream_avl being non-NULL). 2544 */ 2545 cp = strchr(zc.zc_value, '@'); 2546 if (cp && (ioctl_err == 0 || !newfs)) { 2547 zfs_handle_t *h; 2548 2549 *cp = '\0'; 2550 h = zfs_open(hdl, zc.zc_value, 2551 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 2552 if (h != NULL) { 2553 if (h->zfs_type == ZFS_TYPE_VOLUME) { 2554 *cp = '@'; 2555 } else if (newfs || stream_avl) { 2556 /* 2557 * Track the first/top of hierarchy fs, 2558 * for mounting and sharing later. 2559 */ 2560 if (top_zfs && *top_zfs == NULL) 2561 *top_zfs = zfs_strdup(hdl, zc.zc_value); 2562 } 2563 zfs_close(h); 2564 } 2565 *cp = '@'; 2566 } 2567 2568 if (clp) { 2569 err |= changelist_postfix(clp); 2570 changelist_free(clp); 2571 } 2572 2573 if (prop_errflags & ZPROP_ERR_NOCLEAR) { 2574 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " 2575 "failed to clear unreceived properties on %s"), 2576 zc.zc_name); 2577 (void) fprintf(stderr, "\n"); 2578 } 2579 if (prop_errflags & ZPROP_ERR_NORESTORE) { 2580 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " 2581 "failed to restore original properties on %s"), 2582 zc.zc_name); 2583 (void) fprintf(stderr, "\n"); 2584 } 2585 2586 if (err || ioctl_err) 2587 return (-1); 2588 2589 if (flags.verbose) { 2590 char buf1[64]; 2591 char buf2[64]; 2592 uint64_t bytes = zc.zc_cookie; 2593 time_t delta = time(NULL) - begin_time; 2594 if (delta == 0) 2595 delta = 1; 2596 zfs_nicenum(bytes, buf1, sizeof (buf1)); 2597 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 2598 2599 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 2600 buf1, delta, buf2); 2601 } 2602 2603 return (0); 2604 } 2605 2606 static int 2607 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags, 2608 int infd, avl_tree_t *stream_avl, char **top_zfs) 2609 { 2610 int err; 2611 dmu_replay_record_t drr, drr_noswap; 2612 struct drr_begin *drrb = &drr.drr_u.drr_begin; 2613 char errbuf[1024]; 2614 zio_cksum_t zcksum = { 0 }; 2615 uint64_t featureflags; 2616 int hdrtype; 2617 2618 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2619 "cannot receive")); 2620 2621 if (flags.isprefix && 2622 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { 2623 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " 2624 "(%s) does not exist"), tosnap); 2625 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2626 } 2627 2628 /* read in the BEGIN record */ 2629 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, 2630 &zcksum))) 2631 return (err); 2632 2633 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { 2634 /* It's the double end record at the end of a package */ 2635 return (ENODATA); 2636 } 2637 2638 /* the kernel needs the non-byteswapped begin record */ 2639 drr_noswap = drr; 2640 2641 flags.byteswap = B_FALSE; 2642 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { 2643 /* 2644 * We computed the checksum in the wrong byteorder in 2645 * recv_read() above; do it again correctly. 2646 */ 2647 bzero(&zcksum, sizeof (zio_cksum_t)); 2648 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum); 2649 flags.byteswap = B_TRUE; 2650 2651 drr.drr_type = BSWAP_32(drr.drr_type); 2652 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); 2653 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 2654 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); 2655 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 2656 drrb->drr_type = BSWAP_32(drrb->drr_type); 2657 drrb->drr_flags = BSWAP_32(drrb->drr_flags); 2658 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 2659 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 2660 } 2661 2662 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { 2663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 2664 "stream (bad magic number)")); 2665 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 2666 } 2667 2668 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 2669 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo); 2670 2671 if (!DMU_STREAM_SUPPORTED(featureflags) || 2672 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) { 2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2674 "stream has unsupported feature, feature flags = %lx"), 2675 featureflags); 2676 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 2677 } 2678 2679 if (strchr(drrb->drr_toname, '@') == NULL) { 2680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 2681 "stream (bad snapshot name)")); 2682 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 2683 } 2684 2685 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) { 2686 return (zfs_receive_one(hdl, infd, tosnap, flags, 2687 &drr, &drr_noswap, stream_avl, top_zfs)); 2688 } else { /* must be DMU_COMPOUNDSTREAM */ 2689 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == 2690 DMU_COMPOUNDSTREAM); 2691 return (zfs_receive_package(hdl, infd, tosnap, flags, 2692 &drr, &zcksum, top_zfs)); 2693 } 2694 } 2695 2696 /* 2697 * Restores a backup of tosnap from the file descriptor specified by infd. 2698 * Return 0 on total success, -2 if some things couldn't be 2699 * destroyed/renamed/promoted, -1 if some things couldn't be received. 2700 * (-1 will override -2). 2701 */ 2702 int 2703 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags, 2704 int infd, avl_tree_t *stream_avl) 2705 { 2706 char *top_zfs = NULL; 2707 int err; 2708 2709 err = zfs_receive_impl(hdl, tosnap, flags, infd, stream_avl, &top_zfs); 2710 2711 if (err == 0 && !flags.nomount && top_zfs) { 2712 zfs_handle_t *zhp; 2713 prop_changelist_t *clp; 2714 2715 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM); 2716 if (zhp != NULL) { 2717 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 2718 CL_GATHER_MOUNT_ALWAYS, 0); 2719 zfs_close(zhp); 2720 if (clp != NULL) { 2721 /* mount and share received datasets */ 2722 err = changelist_postfix(clp); 2723 changelist_free(clp); 2724 } 2725 } 2726 if (zhp == NULL || clp == NULL || err) 2727 err = -1; 2728 } 2729 if (top_zfs) 2730 free(top_zfs); 2731 2732 return (err); 2733 } 2734