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