libzfs_sendrecv.c revision 364978
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22/* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 25 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 26 * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved. 27 * Copyright (c) 2013 Steven Hartland. All rights reserved. 28 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. 29 * Copyright (c) 2014 Integros [integros.com] 30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 31 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 32 * Copyright (c) 2019 Datto Inc. 33 */ 34 35#include <assert.h> 36#include <ctype.h> 37#include <errno.h> 38#include <libintl.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <strings.h> 42#include <unistd.h> 43#include <stddef.h> 44#include <fcntl.h> 45#include <sys/param.h> 46#include <sys/mount.h> 47#include <pthread.h> 48#include <umem.h> 49#include <time.h> 50 51#include <libzfs.h> 52#include <libzfs_core.h> 53 54#include "zfs_namecheck.h" 55#include "zfs_prop.h" 56#include "zfs_fletcher.h" 57#include "libzfs_impl.h" 58#include <zlib.h> 59#include <sha2.h> 60#include <sys/zio_checksum.h> 61#include <sys/ddt.h> 62 63#ifdef __FreeBSD__ 64extern int zfs_ioctl_version; 65#endif 66 67/* in libzfs_dataset.c */ 68extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *); 69/* We need to use something for ENODATA. */ 70#define ENODATA EIDRM 71 72static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *, 73 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int, 74 uint64_t *, const char *); 75static int guid_to_name(libzfs_handle_t *, const char *, 76 uint64_t, boolean_t, char *); 77 78static const zio_cksum_t zero_cksum = { 0 }; 79 80typedef struct dedup_arg { 81 int inputfd; 82 int outputfd; 83 libzfs_handle_t *dedup_hdl; 84} dedup_arg_t; 85 86typedef struct progress_arg { 87 zfs_handle_t *pa_zhp; 88 int pa_fd; 89 boolean_t pa_parsable; 90 boolean_t pa_astitle; 91 uint64_t pa_size; 92} progress_arg_t; 93 94typedef struct dataref { 95 uint64_t ref_guid; 96 uint64_t ref_object; 97 uint64_t ref_offset; 98} dataref_t; 99 100typedef struct dedup_entry { 101 struct dedup_entry *dde_next; 102 zio_cksum_t dde_chksum; 103 uint64_t dde_prop; 104 dataref_t dde_ref; 105} dedup_entry_t; 106 107#define MAX_DDT_PHYSMEM_PERCENT 20 108#define SMALLEST_POSSIBLE_MAX_DDT_MB 128 109 110typedef struct dedup_table { 111 dedup_entry_t **dedup_hash_array; 112 umem_cache_t *ddecache; 113 uint64_t max_ddt_size; /* max dedup table size in bytes */ 114 uint64_t cur_ddt_size; /* current dedup table size in bytes */ 115 uint64_t ddt_count; 116 int numhashbits; 117 boolean_t ddt_full; 118} dedup_table_t; 119 120static int 121high_order_bit(uint64_t n) 122{ 123 int count; 124 125 for (count = 0; n != 0; count++) 126 n >>= 1; 127 return (count); 128} 129 130static size_t 131ssread(void *buf, size_t len, FILE *stream) 132{ 133 size_t outlen; 134 135 if ((outlen = fread(buf, len, 1, stream)) == 0) 136 return (0); 137 138 return (outlen); 139} 140 141static void 142ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp, 143 zio_cksum_t *cs, uint64_t prop, dataref_t *dr) 144{ 145 dedup_entry_t *dde; 146 147 if (ddt->cur_ddt_size >= ddt->max_ddt_size) { 148 if (ddt->ddt_full == B_FALSE) { 149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 150 "Dedup table full. Deduplication will continue " 151 "with existing table entries")); 152 ddt->ddt_full = B_TRUE; 153 } 154 return; 155 } 156 157 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT)) 158 != NULL) { 159 assert(*ddepp == NULL); 160 dde->dde_next = NULL; 161 dde->dde_chksum = *cs; 162 dde->dde_prop = prop; 163 dde->dde_ref = *dr; 164 *ddepp = dde; 165 ddt->cur_ddt_size += sizeof (dedup_entry_t); 166 ddt->ddt_count++; 167 } 168} 169 170/* 171 * Using the specified dedup table, do a lookup for an entry with 172 * the checksum cs. If found, return the block's reference info 173 * in *dr. Otherwise, insert a new entry in the dedup table, using 174 * the reference information specified by *dr. 175 * 176 * return value: true - entry was found 177 * false - entry was not found 178 */ 179static boolean_t 180ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs, 181 uint64_t prop, dataref_t *dr) 182{ 183 uint32_t hashcode; 184 dedup_entry_t **ddepp; 185 186 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits); 187 188 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL; 189 ddepp = &((*ddepp)->dde_next)) { 190 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) && 191 (*ddepp)->dde_prop == prop) { 192 *dr = (*ddepp)->dde_ref; 193 return (B_TRUE); 194 } 195 } 196 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr); 197 return (B_FALSE); 198} 199 200static int 201dump_record(dmu_replay_record_t *drr, void *payload, int payload_len, 202 zio_cksum_t *zc, int outfd) 203{ 204 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), 205 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t)); 206 (void) fletcher_4_incremental_native(drr, 207 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc); 208 if (drr->drr_type != DRR_BEGIN) { 209 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u. 210 drr_checksum.drr_checksum)); 211 drr->drr_u.drr_checksum.drr_checksum = *zc; 212 } 213 (void) fletcher_4_incremental_native( 214 &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc); 215 if (write(outfd, drr, sizeof (*drr)) == -1) 216 return (errno); 217 if (payload_len != 0) { 218 (void) fletcher_4_incremental_native(payload, payload_len, zc); 219 if (write(outfd, payload, payload_len) == -1) 220 return (errno); 221 } 222 return (0); 223} 224 225/* 226 * This function is started in a separate thread when the dedup option 227 * has been requested. The main send thread determines the list of 228 * snapshots to be included in the send stream and makes the ioctl calls 229 * for each one. But instead of having the ioctl send the output to the 230 * the output fd specified by the caller of zfs_send()), the 231 * ioctl is told to direct the output to a pipe, which is read by the 232 * alternate thread running THIS function. This function does the 233 * dedup'ing by: 234 * 1. building a dedup table (the DDT) 235 * 2. doing checksums on each data block and inserting a record in the DDT 236 * 3. looking for matching checksums, and 237 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever 238 * a duplicate block is found. 239 * The output of this function then goes to the output fd requested 240 * by the caller of zfs_send(). 241 */ 242static void * 243cksummer(void *arg) 244{ 245 dedup_arg_t *dda = arg; 246 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE); 247 dmu_replay_record_t thedrr; 248 dmu_replay_record_t *drr = &thedrr; 249 FILE *ofp; 250 int outfd; 251 dedup_table_t ddt; 252 zio_cksum_t stream_cksum; 253 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); 254 uint64_t numbuckets; 255 256 ddt.max_ddt_size = 257 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100, 258 SMALLEST_POSSIBLE_MAX_DDT_MB << 20); 259 260 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t)); 261 262 /* 263 * numbuckets must be a power of 2. Increase number to 264 * a power of 2 if necessary. 265 */ 266 if (!ISP2(numbuckets)) 267 numbuckets = 1 << high_order_bit(numbuckets); 268 269 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *)); 270 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0, 271 NULL, NULL, NULL, NULL, NULL, 0); 272 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *); 273 ddt.numhashbits = high_order_bit(numbuckets) - 1; 274 ddt.ddt_full = B_FALSE; 275 276 outfd = dda->outputfd; 277 ofp = fdopen(dda->inputfd, "r"); 278 while (ssread(drr, sizeof (*drr), ofp) != 0) { 279 280 /* 281 * kernel filled in checksum, we are going to write same 282 * record, but need to regenerate checksum. 283 */ 284 if (drr->drr_type != DRR_BEGIN) { 285 bzero(&drr->drr_u.drr_checksum.drr_checksum, 286 sizeof (drr->drr_u.drr_checksum.drr_checksum)); 287 } 288 289 switch (drr->drr_type) { 290 case DRR_BEGIN: 291 { 292 struct drr_begin *drrb = &drr->drr_u.drr_begin; 293 int fflags; 294 int sz = 0; 295 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0); 296 297 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC); 298 299 /* set the DEDUP feature flag for this stream */ 300 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 301 fflags |= (DMU_BACKUP_FEATURE_DEDUP | 302 DMU_BACKUP_FEATURE_DEDUPPROPS); 303 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags); 304 305 if (drr->drr_payloadlen != 0) { 306 sz = drr->drr_payloadlen; 307 308 if (sz > SPA_MAXBLOCKSIZE) { 309 buf = zfs_realloc(dda->dedup_hdl, buf, 310 SPA_MAXBLOCKSIZE, sz); 311 } 312 (void) ssread(buf, sz, ofp); 313 if (ferror(stdin)) 314 perror("fread"); 315 } 316 if (dump_record(drr, buf, sz, &stream_cksum, 317 outfd) != 0) 318 goto out; 319 break; 320 } 321 322 case DRR_END: 323 { 324 struct drr_end *drre = &drr->drr_u.drr_end; 325 /* use the recalculated checksum */ 326 drre->drr_checksum = stream_cksum; 327 if (dump_record(drr, NULL, 0, &stream_cksum, 328 outfd) != 0) 329 goto out; 330 break; 331 } 332 333 case DRR_OBJECT: 334 { 335 struct drr_object *drro = &drr->drr_u.drr_object; 336 if (drro->drr_bonuslen > 0) { 337 (void) ssread(buf, 338 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8), 339 ofp); 340 } 341 if (dump_record(drr, buf, 342 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8), 343 &stream_cksum, outfd) != 0) 344 goto out; 345 break; 346 } 347 348 case DRR_SPILL: 349 { 350 struct drr_spill *drrs = &drr->drr_u.drr_spill; 351 (void) ssread(buf, drrs->drr_length, ofp); 352 if (dump_record(drr, buf, drrs->drr_length, 353 &stream_cksum, outfd) != 0) 354 goto out; 355 break; 356 } 357 358 case DRR_FREEOBJECTS: 359 { 360 if (dump_record(drr, NULL, 0, &stream_cksum, 361 outfd) != 0) 362 goto out; 363 break; 364 } 365 366 case DRR_WRITE: 367 { 368 struct drr_write *drrw = &drr->drr_u.drr_write; 369 dataref_t dataref; 370 uint64_t payload_size; 371 372 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw); 373 (void) ssread(buf, payload_size, ofp); 374 375 /* 376 * Use the existing checksum if it's dedup-capable, 377 * else calculate a SHA256 checksum for it. 378 */ 379 380 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum, 381 zero_cksum) || 382 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) { 383 SHA256_CTX ctx; 384 zio_cksum_t tmpsha256; 385 386 SHA256Init(&ctx); 387 SHA256Update(&ctx, buf, payload_size); 388 SHA256Final(&tmpsha256, &ctx); 389 drrw->drr_key.ddk_cksum.zc_word[0] = 390 BE_64(tmpsha256.zc_word[0]); 391 drrw->drr_key.ddk_cksum.zc_word[1] = 392 BE_64(tmpsha256.zc_word[1]); 393 drrw->drr_key.ddk_cksum.zc_word[2] = 394 BE_64(tmpsha256.zc_word[2]); 395 drrw->drr_key.ddk_cksum.zc_word[3] = 396 BE_64(tmpsha256.zc_word[3]); 397 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256; 398 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP; 399 } 400 401 dataref.ref_guid = drrw->drr_toguid; 402 dataref.ref_object = drrw->drr_object; 403 dataref.ref_offset = drrw->drr_offset; 404 405 if (ddt_update(dda->dedup_hdl, &ddt, 406 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop, 407 &dataref)) { 408 dmu_replay_record_t wbr_drr = {0}; 409 struct drr_write_byref *wbr_drrr = 410 &wbr_drr.drr_u.drr_write_byref; 411 412 /* block already present in stream */ 413 wbr_drr.drr_type = DRR_WRITE_BYREF; 414 415 wbr_drrr->drr_object = drrw->drr_object; 416 wbr_drrr->drr_offset = drrw->drr_offset; 417 wbr_drrr->drr_length = drrw->drr_logical_size; 418 wbr_drrr->drr_toguid = drrw->drr_toguid; 419 wbr_drrr->drr_refguid = dataref.ref_guid; 420 wbr_drrr->drr_refobject = 421 dataref.ref_object; 422 wbr_drrr->drr_refoffset = 423 dataref.ref_offset; 424 425 wbr_drrr->drr_checksumtype = 426 drrw->drr_checksumtype; 427 wbr_drrr->drr_checksumflags = 428 drrw->drr_checksumtype; 429 wbr_drrr->drr_key.ddk_cksum = 430 drrw->drr_key.ddk_cksum; 431 wbr_drrr->drr_key.ddk_prop = 432 drrw->drr_key.ddk_prop; 433 434 if (dump_record(&wbr_drr, NULL, 0, 435 &stream_cksum, outfd) != 0) 436 goto out; 437 } else { 438 /* block not previously seen */ 439 if (dump_record(drr, buf, payload_size, 440 &stream_cksum, outfd) != 0) 441 goto out; 442 } 443 break; 444 } 445 446 case DRR_WRITE_EMBEDDED: 447 { 448 struct drr_write_embedded *drrwe = 449 &drr->drr_u.drr_write_embedded; 450 (void) ssread(buf, 451 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp); 452 if (dump_record(drr, buf, 453 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), 454 &stream_cksum, outfd) != 0) 455 goto out; 456 break; 457 } 458 459 case DRR_FREE: 460 { 461 if (dump_record(drr, NULL, 0, &stream_cksum, 462 outfd) != 0) 463 goto out; 464 break; 465 } 466 467 default: 468 (void) fprintf(stderr, "INVALID record type 0x%x\n", 469 drr->drr_type); 470 /* should never happen, so assert */ 471 assert(B_FALSE); 472 } 473 } 474out: 475 umem_cache_destroy(ddt.ddecache); 476 free(ddt.dedup_hash_array); 477 free(buf); 478 (void) fclose(ofp); 479 480 return (NULL); 481} 482 483/* 484 * Routines for dealing with the AVL tree of fs-nvlists 485 */ 486typedef struct fsavl_node { 487 avl_node_t fn_node; 488 nvlist_t *fn_nvfs; 489 char *fn_snapname; 490 uint64_t fn_guid; 491} fsavl_node_t; 492 493static int 494fsavl_compare(const void *arg1, const void *arg2) 495{ 496 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1; 497 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2; 498 499 return (AVL_CMP(fn1->fn_guid, fn2->fn_guid)); 500} 501 502/* 503 * Given the GUID of a snapshot, find its containing filesystem and 504 * (optionally) name. 505 */ 506static nvlist_t * 507fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname) 508{ 509 fsavl_node_t fn_find; 510 fsavl_node_t *fn; 511 512 fn_find.fn_guid = snapguid; 513 514 fn = avl_find(avl, &fn_find, NULL); 515 if (fn) { 516 if (snapname) 517 *snapname = fn->fn_snapname; 518 return (fn->fn_nvfs); 519 } 520 return (NULL); 521} 522 523static void 524fsavl_destroy(avl_tree_t *avl) 525{ 526 fsavl_node_t *fn; 527 void *cookie; 528 529 if (avl == NULL) 530 return; 531 532 cookie = NULL; 533 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL) 534 free(fn); 535 avl_destroy(avl); 536 free(avl); 537} 538 539/* 540 * Given an nvlist, produce an avl tree of snapshots, ordered by guid 541 */ 542static avl_tree_t * 543fsavl_create(nvlist_t *fss) 544{ 545 avl_tree_t *fsavl; 546 nvpair_t *fselem = NULL; 547 548 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL) 549 return (NULL); 550 551 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t), 552 offsetof(fsavl_node_t, fn_node)); 553 554 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) { 555 nvlist_t *nvfs, *snaps; 556 nvpair_t *snapelem = NULL; 557 558 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); 559 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); 560 561 while ((snapelem = 562 nvlist_next_nvpair(snaps, snapelem)) != NULL) { 563 fsavl_node_t *fn; 564 uint64_t guid; 565 566 VERIFY(0 == nvpair_value_uint64(snapelem, &guid)); 567 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) { 568 fsavl_destroy(fsavl); 569 return (NULL); 570 } 571 fn->fn_nvfs = nvfs; 572 fn->fn_snapname = nvpair_name(snapelem); 573 fn->fn_guid = guid; 574 575 /* 576 * Note: if there are multiple snaps with the 577 * same GUID, we ignore all but one. 578 */ 579 if (avl_find(fsavl, fn, NULL) == NULL) 580 avl_add(fsavl, fn); 581 else 582 free(fn); 583 } 584 } 585 586 return (fsavl); 587} 588 589/* 590 * Routines for dealing with the giant nvlist of fs-nvlists, etc. 591 */ 592typedef struct send_data { 593 /* 594 * assigned inside every recursive call, 595 * restored from *_save on return: 596 * 597 * guid of fromsnap snapshot in parent dataset 598 * txg of fromsnap snapshot in current dataset 599 * txg of tosnap snapshot in current dataset 600 */ 601 602 uint64_t parent_fromsnap_guid; 603 uint64_t fromsnap_txg; 604 uint64_t tosnap_txg; 605 606 /* the nvlists get accumulated during depth-first traversal */ 607 nvlist_t *parent_snaps; 608 nvlist_t *fss; 609 nvlist_t *snapprops; 610 611 /* send-receive configuration, does not change during traversal */ 612 const char *fsname; 613 const char *fromsnap; 614 const char *tosnap; 615 boolean_t recursive; 616 boolean_t verbose; 617 618 /* 619 * The header nvlist is of the following format: 620 * { 621 * "tosnap" -> string 622 * "fromsnap" -> string (if incremental) 623 * "fss" -> { 624 * id -> { 625 * 626 * "name" -> string (full name; for debugging) 627 * "parentfromsnap" -> number (guid of fromsnap in parent) 628 * 629 * "props" -> { name -> value (only if set here) } 630 * "snaps" -> { name (lastname) -> number (guid) } 631 * "snapprops" -> { name (lastname) -> { name -> value } } 632 * 633 * "origin" -> number (guid) (if clone) 634 * "sent" -> boolean (not on-disk) 635 * } 636 * } 637 * } 638 * 639 */ 640} send_data_t; 641 642static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv); 643 644static int 645send_iterate_snap(zfs_handle_t *zhp, void *arg) 646{ 647 send_data_t *sd = arg; 648 uint64_t guid = zhp->zfs_dmustats.dds_guid; 649 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg; 650 char *snapname; 651 nvlist_t *nv; 652 653 snapname = strrchr(zhp->zfs_name, '@')+1; 654 655 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) { 656 if (sd->verbose) { 657 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 658 "skipping snapshot %s because it was created " 659 "after the destination snapshot (%s)\n"), 660 zhp->zfs_name, sd->tosnap); 661 } 662 zfs_close(zhp); 663 return (0); 664 } 665 666 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid)); 667 /* 668 * NB: if there is no fromsnap here (it's a newly created fs in 669 * an incremental replication), we will substitute the tosnap. 670 */ 671 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) || 672 (sd->parent_fromsnap_guid == 0 && sd->tosnap && 673 strcmp(snapname, sd->tosnap) == 0)) { 674 sd->parent_fromsnap_guid = guid; 675 } 676 677 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); 678 send_iterate_prop(zhp, nv); 679 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv)); 680 nvlist_free(nv); 681 682 zfs_close(zhp); 683 return (0); 684} 685 686static void 687send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv) 688{ 689 nvpair_t *elem = NULL; 690 691 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) { 692 char *propname = nvpair_name(elem); 693 zfs_prop_t prop = zfs_name_to_prop(propname); 694 nvlist_t *propnv; 695 696 if (!zfs_prop_user(propname)) { 697 /* 698 * Realistically, this should never happen. However, 699 * we want the ability to add DSL properties without 700 * needing to make incompatible version changes. We 701 * need to ignore unknown properties to allow older 702 * software to still send datasets containing these 703 * properties, with the unknown properties elided. 704 */ 705 if (prop == ZPROP_INVAL) 706 continue; 707 708 if (zfs_prop_readonly(prop)) 709 continue; 710 } 711 712 verify(nvpair_value_nvlist(elem, &propnv) == 0); 713 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION || 714 prop == ZFS_PROP_REFQUOTA || 715 prop == ZFS_PROP_REFRESERVATION) { 716 char *source; 717 uint64_t value; 718 verify(nvlist_lookup_uint64(propnv, 719 ZPROP_VALUE, &value) == 0); 720 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 721 continue; 722 /* 723 * May have no source before SPA_VERSION_RECVD_PROPS, 724 * but is still modifiable. 725 */ 726 if (nvlist_lookup_string(propnv, 727 ZPROP_SOURCE, &source) == 0) { 728 if ((strcmp(source, zhp->zfs_name) != 0) && 729 (strcmp(source, 730 ZPROP_SOURCE_VAL_RECVD) != 0)) 731 continue; 732 } 733 } else { 734 char *source; 735 if (nvlist_lookup_string(propnv, 736 ZPROP_SOURCE, &source) != 0) 737 continue; 738 if ((strcmp(source, zhp->zfs_name) != 0) && 739 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)) 740 continue; 741 } 742 743 if (zfs_prop_user(propname) || 744 zfs_prop_get_type(prop) == PROP_TYPE_STRING) { 745 char *value; 746 verify(nvlist_lookup_string(propnv, 747 ZPROP_VALUE, &value) == 0); 748 VERIFY(0 == nvlist_add_string(nv, propname, value)); 749 } else { 750 uint64_t value; 751 verify(nvlist_lookup_uint64(propnv, 752 ZPROP_VALUE, &value) == 0); 753 VERIFY(0 == nvlist_add_uint64(nv, propname, value)); 754 } 755 } 756} 757 758/* 759 * returns snapshot creation txg 760 * and returns 0 if the snapshot does not exist 761 */ 762static uint64_t 763get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap) 764{ 765 char name[ZFS_MAX_DATASET_NAME_LEN]; 766 uint64_t txg = 0; 767 768 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0') 769 return (txg); 770 771 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap); 772 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) { 773 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT); 774 if (zhp != NULL) { 775 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG); 776 zfs_close(zhp); 777 } 778 } 779 780 return (txg); 781} 782 783/* 784 * recursively generate nvlists describing datasets. See comment 785 * for the data structure send_data_t above for description of contents 786 * of the nvlist. 787 */ 788static int 789send_iterate_fs(zfs_handle_t *zhp, void *arg) 790{ 791 send_data_t *sd = arg; 792 nvlist_t *nvfs, *nv; 793 int rv = 0; 794 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid; 795 uint64_t fromsnap_txg_save = sd->fromsnap_txg; 796 uint64_t tosnap_txg_save = sd->tosnap_txg; 797 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg; 798 uint64_t guid = zhp->zfs_dmustats.dds_guid; 799 uint64_t fromsnap_txg, tosnap_txg; 800 char guidstring[64]; 801 802 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap); 803 if (fromsnap_txg != 0) 804 sd->fromsnap_txg = fromsnap_txg; 805 806 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap); 807 if (tosnap_txg != 0) 808 sd->tosnap_txg = tosnap_txg; 809 810 /* 811 * on the send side, if the current dataset does not have tosnap, 812 * perform two additional checks: 813 * 814 * - skip sending the current dataset if it was created later than 815 * the parent tosnap 816 * - return error if the current dataset was created earlier than 817 * the parent tosnap 818 */ 819 if (sd->tosnap != NULL && tosnap_txg == 0) { 820 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) { 821 if (sd->verbose) { 822 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 823 "skipping dataset %s: snapshot %s does " 824 "not exist\n"), zhp->zfs_name, sd->tosnap); 825 } 826 } else { 827 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 828 "cannot send %s@%s%s: snapshot %s@%s does not " 829 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ? 830 dgettext(TEXT_DOMAIN, " recursively") : "", 831 zhp->zfs_name, sd->tosnap); 832 rv = -1; 833 } 834 goto out; 835 } 836 837 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0)); 838 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name)); 839 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap", 840 sd->parent_fromsnap_guid)); 841 842 if (zhp->zfs_dmustats.dds_origin[0]) { 843 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl, 844 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 845 if (origin == NULL) { 846 rv = -1; 847 goto out; 848 } 849 VERIFY(0 == nvlist_add_uint64(nvfs, "origin", 850 origin->zfs_dmustats.dds_guid)); 851 } 852 853 /* iterate over props */ 854 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); 855 send_iterate_prop(zhp, nv); 856 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv)); 857 nvlist_free(nv); 858 859 /* iterate over snaps, and set sd->parent_fromsnap_guid */ 860 sd->parent_fromsnap_guid = 0; 861 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0)); 862 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0)); 863 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd); 864 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps)); 865 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops)); 866 nvlist_free(sd->parent_snaps); 867 nvlist_free(sd->snapprops); 868 869 /* add this fs to nvlist */ 870 (void) snprintf(guidstring, sizeof (guidstring), 871 "0x%llx", (longlong_t)guid); 872 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs)); 873 nvlist_free(nvfs); 874 875 /* iterate over children */ 876 if (sd->recursive) 877 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd); 878 879out: 880 sd->parent_fromsnap_guid = parent_fromsnap_guid_save; 881 sd->fromsnap_txg = fromsnap_txg_save; 882 sd->tosnap_txg = tosnap_txg_save; 883 884 zfs_close(zhp); 885 return (rv); 886} 887 888static int 889gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap, 890 const char *tosnap, boolean_t recursive, boolean_t verbose, 891 nvlist_t **nvlp, avl_tree_t **avlp) 892{ 893 zfs_handle_t *zhp; 894 send_data_t sd = { 0 }; 895 int error; 896 897 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 898 if (zhp == NULL) 899 return (EZFS_BADTYPE); 900 901 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0)); 902 sd.fsname = fsname; 903 sd.fromsnap = fromsnap; 904 sd.tosnap = tosnap; 905 sd.recursive = recursive; 906 sd.verbose = verbose; 907 908 if ((error = send_iterate_fs(zhp, &sd)) != 0) { 909 nvlist_free(sd.fss); 910 if (avlp != NULL) 911 *avlp = NULL; 912 *nvlp = NULL; 913 return (error); 914 } 915 916 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) { 917 nvlist_free(sd.fss); 918 *nvlp = NULL; 919 return (EZFS_NOMEM); 920 } 921 922 *nvlp = sd.fss; 923 return (0); 924} 925 926/* 927 * Routines specific to "zfs send" 928 */ 929typedef struct send_dump_data { 930 /* these are all just the short snapname (the part after the @) */ 931 const char *fromsnap; 932 const char *tosnap; 933 char prevsnap[ZFS_MAX_DATASET_NAME_LEN]; 934 uint64_t prevsnap_obj; 935 boolean_t seenfrom, seento, replicate, doall, fromorigin; 936 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out; 937 boolean_t progressastitle; 938 boolean_t large_block, compress; 939 int outfd; 940 boolean_t err; 941 nvlist_t *fss; 942 nvlist_t *snapholds; 943 avl_tree_t *fsavl; 944 snapfilter_cb_t *filter_cb; 945 void *filter_cb_arg; 946 nvlist_t *debugnv; 947 char holdtag[ZFS_MAX_DATASET_NAME_LEN]; 948 int cleanup_fd; 949 uint64_t size; 950} send_dump_data_t; 951 952static int 953zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from, 954 enum lzc_send_flags flags, uint64_t *spacep) 955{ 956 libzfs_handle_t *hdl = zhp->zfs_hdl; 957 int error; 958 959 assert(snapname != NULL); 960 error = lzc_send_space(snapname, from, flags, spacep); 961 962 if (error != 0) { 963 char errbuf[1024]; 964 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 965 "warning: cannot estimate space for '%s'"), snapname); 966 967 switch (error) { 968 case EXDEV: 969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 970 "not an earlier snapshot from the same fs")); 971 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 972 973 case ENOENT: 974 if (zfs_dataset_exists(hdl, snapname, 975 ZFS_TYPE_SNAPSHOT)) { 976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 977 "incremental source (%s) does not exist"), 978 snapname); 979 } 980 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 981 982 case EDQUOT: 983 case EFBIG: 984 case EIO: 985 case ENOLINK: 986 case ENOSPC: 987 case ENXIO: 988 case EPIPE: 989 case ERANGE: 990 case EFAULT: 991 case EROFS: 992 case EINVAL: 993 zfs_error_aux(hdl, strerror(error)); 994 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 995 996 default: 997 return (zfs_standard_error(hdl, error, errbuf)); 998 } 999 } 1000 1001 return (0); 1002} 1003 1004/* 1005 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 1006 * NULL) to the file descriptor specified by outfd. 1007 */ 1008static int 1009dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj, 1010 boolean_t fromorigin, int outfd, enum lzc_send_flags flags, 1011 nvlist_t *debugnv) 1012{ 1013 zfs_cmd_t zc = { 0 }; 1014 libzfs_handle_t *hdl = zhp->zfs_hdl; 1015 nvlist_t *thisdbg; 1016 1017 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 1018 assert(fromsnap_obj == 0 || !fromorigin); 1019 1020 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1021 zc.zc_cookie = outfd; 1022 zc.zc_obj = fromorigin; 1023 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); 1024 zc.zc_fromobj = fromsnap_obj; 1025 zc.zc_flags = flags; 1026 1027 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0)); 1028 if (fromsnap && fromsnap[0] != '\0') { 1029 VERIFY(0 == nvlist_add_string(thisdbg, 1030 "fromsnap", fromsnap)); 1031 } 1032 1033 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) { 1034 char errbuf[1024]; 1035 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1036 "warning: cannot send '%s'"), zhp->zfs_name); 1037 1038 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno)); 1039 if (debugnv) { 1040 VERIFY(0 == nvlist_add_nvlist(debugnv, 1041 zhp->zfs_name, thisdbg)); 1042 } 1043 nvlist_free(thisdbg); 1044 1045 switch (errno) { 1046 case EXDEV: 1047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1048 "not an earlier snapshot from the same fs")); 1049 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 1050 1051 case ENOENT: 1052 if (zfs_dataset_exists(hdl, zc.zc_name, 1053 ZFS_TYPE_SNAPSHOT)) { 1054 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1055 "incremental source (@%s) does not exist"), 1056 zc.zc_value); 1057 } 1058 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 1059 1060 case EDQUOT: 1061 case EFBIG: 1062 case EIO: 1063 case ENOLINK: 1064 case ENOSPC: 1065#ifdef illumos 1066 case ENOSTR: 1067#endif 1068 case ENXIO: 1069 case EPIPE: 1070 case ERANGE: 1071 case EFAULT: 1072 case EROFS: 1073 zfs_error_aux(hdl, strerror(errno)); 1074 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 1075 1076 default: 1077 return (zfs_standard_error(hdl, errno, errbuf)); 1078 } 1079 } 1080 1081 if (debugnv) 1082 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg)); 1083 nvlist_free(thisdbg); 1084 1085 return (0); 1086} 1087 1088static void 1089gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd) 1090{ 1091 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 1092 1093 /* 1094 * zfs_send() only sets snapholds for sends that need them, 1095 * e.g. replication and doall. 1096 */ 1097 if (sdd->snapholds == NULL) 1098 return; 1099 1100 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag); 1101} 1102 1103static void * 1104send_progress_thread(void *arg) 1105{ 1106 progress_arg_t *pa = arg; 1107 zfs_cmd_t zc = { 0 }; 1108 zfs_handle_t *zhp = pa->pa_zhp; 1109 libzfs_handle_t *hdl = zhp->zfs_hdl; 1110 unsigned long long bytes, total; 1111 char buf[16]; 1112 time_t t; 1113 struct tm *tm; 1114 1115 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1116 1117 if (!pa->pa_parsable && !pa->pa_astitle) 1118 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n"); 1119 1120 /* 1121 * Print the progress from ZFS_IOC_SEND_PROGRESS every second. 1122 */ 1123 for (;;) { 1124 (void) sleep(1); 1125 1126 zc.zc_cookie = pa->pa_fd; 1127 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0) 1128 return ((void *)-1); 1129 1130 (void) time(&t); 1131 tm = localtime(&t); 1132 bytes = zc.zc_cookie; 1133 1134 if (pa->pa_astitle) { 1135 int pct; 1136 if (pa->pa_size > bytes) 1137 pct = 100 * bytes / pa->pa_size; 1138 else 1139 pct = 100; 1140 1141 setproctitle("sending %s (%d%%: %llu/%llu)", 1142 zhp->zfs_name, pct, bytes, pa->pa_size); 1143 } else if (pa->pa_parsable) { 1144 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n", 1145 tm->tm_hour, tm->tm_min, tm->tm_sec, 1146 bytes, zhp->zfs_name); 1147 } else { 1148 zfs_nicenum(bytes, buf, sizeof (buf)); 1149 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n", 1150 tm->tm_hour, tm->tm_min, tm->tm_sec, 1151 buf, zhp->zfs_name); 1152 } 1153 } 1154} 1155 1156static void 1157send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap, 1158 uint64_t size, boolean_t parsable) 1159{ 1160 if (parsable) { 1161 if (fromsnap != NULL) { 1162 (void) fprintf(fout, "incremental\t%s\t%s", 1163 fromsnap, tosnap); 1164 } else { 1165 (void) fprintf(fout, "full\t%s", 1166 tosnap); 1167 } 1168 } else { 1169 if (fromsnap != NULL) { 1170 if (strchr(fromsnap, '@') == NULL && 1171 strchr(fromsnap, '#') == NULL) { 1172 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1173 "send from @%s to %s"), 1174 fromsnap, tosnap); 1175 } else { 1176 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1177 "send from %s to %s"), 1178 fromsnap, tosnap); 1179 } 1180 } else { 1181 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1182 "full send of %s"), 1183 tosnap); 1184 } 1185 } 1186 1187 if (parsable) { 1188 (void) fprintf(fout, "\t%llu", 1189 (longlong_t)size); 1190 } else if (size != 0) { 1191 char buf[16]; 1192 zfs_nicenum(size, buf, sizeof (buf)); 1193 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1194 " estimated size is %s"), buf); 1195 } 1196 (void) fprintf(fout, "\n"); 1197} 1198 1199static int 1200dump_snapshot(zfs_handle_t *zhp, void *arg) 1201{ 1202 send_dump_data_t *sdd = arg; 1203 progress_arg_t pa = { 0 }; 1204 pthread_t tid; 1205 char *thissnap; 1206 enum lzc_send_flags flags = 0; 1207 int err; 1208 boolean_t isfromsnap, istosnap, fromorigin; 1209 boolean_t exclude = B_FALSE; 1210 FILE *fout = sdd->std_out ? stdout : stderr; 1211 1212 err = 0; 1213 thissnap = strchr(zhp->zfs_name, '@') + 1; 1214 isfromsnap = (sdd->fromsnap != NULL && 1215 strcmp(sdd->fromsnap, thissnap) == 0); 1216 1217 if (!sdd->seenfrom && isfromsnap) { 1218 gather_holds(zhp, sdd); 1219 sdd->seenfrom = B_TRUE; 1220 (void) strcpy(sdd->prevsnap, thissnap); 1221 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); 1222 zfs_close(zhp); 1223 return (0); 1224 } 1225 1226 if (sdd->seento || !sdd->seenfrom) { 1227 zfs_close(zhp); 1228 return (0); 1229 } 1230 1231 istosnap = (strcmp(sdd->tosnap, thissnap) == 0); 1232 if (istosnap) 1233 sdd->seento = B_TRUE; 1234 1235 if (sdd->large_block) 1236 flags |= LZC_SEND_FLAG_LARGE_BLOCK; 1237 if (sdd->embed_data) 1238 flags |= LZC_SEND_FLAG_EMBED_DATA; 1239 if (sdd->compress) 1240 flags |= LZC_SEND_FLAG_COMPRESS; 1241 1242 if (!sdd->doall && !isfromsnap && !istosnap) { 1243 if (sdd->replicate) { 1244 char *snapname; 1245 nvlist_t *snapprops; 1246 /* 1247 * Filter out all intermediate snapshots except origin 1248 * snapshots needed to replicate clones. 1249 */ 1250 nvlist_t *nvfs = fsavl_find(sdd->fsavl, 1251 zhp->zfs_dmustats.dds_guid, &snapname); 1252 1253 VERIFY(0 == nvlist_lookup_nvlist(nvfs, 1254 "snapprops", &snapprops)); 1255 VERIFY(0 == nvlist_lookup_nvlist(snapprops, 1256 thissnap, &snapprops)); 1257 exclude = !nvlist_exists(snapprops, "is_clone_origin"); 1258 } else { 1259 exclude = B_TRUE; 1260 } 1261 } 1262 1263 /* 1264 * If a filter function exists, call it to determine whether 1265 * this snapshot will be sent. 1266 */ 1267 if (exclude || (sdd->filter_cb != NULL && 1268 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) { 1269 /* 1270 * This snapshot is filtered out. Don't send it, and don't 1271 * set prevsnap_obj, so it will be as if this snapshot didn't 1272 * exist, and the next accepted snapshot will be sent as 1273 * an incremental from the last accepted one, or as the 1274 * first (and full) snapshot in the case of a replication, 1275 * non-incremental send. 1276 */ 1277 zfs_close(zhp); 1278 return (0); 1279 } 1280 1281 gather_holds(zhp, sdd); 1282 fromorigin = sdd->prevsnap[0] == '\0' && 1283 (sdd->fromorigin || sdd->replicate); 1284 1285 if (sdd->verbose || sdd->progress) { 1286 uint64_t size = 0; 1287 char fromds[ZFS_MAX_DATASET_NAME_LEN]; 1288 1289 if (sdd->prevsnap[0] != '\0') { 1290 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds)); 1291 *(strchr(fromds, '@') + 1) = '\0'; 1292 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds)); 1293 } 1294 if (zfs_send_space(zhp, zhp->zfs_name, 1295 sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) { 1296 size = 0; /* cannot estimate send space */ 1297 } else { 1298 send_print_verbose(fout, zhp->zfs_name, 1299 sdd->prevsnap[0] ? sdd->prevsnap : NULL, 1300 size, sdd->parsable); 1301 } 1302 sdd->size += size; 1303 } 1304 1305 if (!sdd->dryrun) { 1306 /* 1307 * If progress reporting is requested, spawn a new thread to 1308 * poll ZFS_IOC_SEND_PROGRESS at a regular interval. 1309 */ 1310 if (sdd->progress) { 1311 pa.pa_zhp = zhp; 1312 pa.pa_fd = sdd->outfd; 1313 pa.pa_parsable = sdd->parsable; 1314 pa.pa_size = sdd->size; 1315 pa.pa_astitle = sdd->progressastitle; 1316 1317 if ((err = pthread_create(&tid, NULL, 1318 send_progress_thread, &pa)) != 0) { 1319 zfs_close(zhp); 1320 return (err); 1321 } 1322 } 1323 1324 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj, 1325 fromorigin, sdd->outfd, flags, sdd->debugnv); 1326 1327 if (sdd->progress) { 1328 (void) pthread_cancel(tid); 1329 (void) pthread_join(tid, NULL); 1330 } 1331 } 1332 1333 (void) strcpy(sdd->prevsnap, thissnap); 1334 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); 1335 zfs_close(zhp); 1336 return (err); 1337} 1338 1339static int 1340dump_filesystem(zfs_handle_t *zhp, void *arg) 1341{ 1342 int rv = 0; 1343 send_dump_data_t *sdd = arg; 1344 boolean_t missingfrom = B_FALSE; 1345 zfs_cmd_t zc = { 0 }; 1346 1347 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 1348 zhp->zfs_name, sdd->tosnap); 1349 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 1350 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1351 "WARNING: could not send %s@%s: does not exist\n"), 1352 zhp->zfs_name, sdd->tosnap); 1353 sdd->err = B_TRUE; 1354 return (0); 1355 } 1356 1357 if (sdd->replicate && sdd->fromsnap) { 1358 /* 1359 * If this fs does not have fromsnap, and we're doing 1360 * recursive, we need to send a full stream from the 1361 * beginning (or an incremental from the origin if this 1362 * is a clone). If we're doing non-recursive, then let 1363 * them get the error. 1364 */ 1365 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", 1366 zhp->zfs_name, sdd->fromsnap); 1367 if (ioctl(zhp->zfs_hdl->libzfs_fd, 1368 ZFS_IOC_OBJSET_STATS, &zc) != 0) { 1369 missingfrom = B_TRUE; 1370 } 1371 } 1372 1373 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0; 1374 sdd->prevsnap_obj = 0; 1375 if (sdd->fromsnap == NULL || missingfrom) 1376 sdd->seenfrom = B_TRUE; 1377 1378 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg); 1379 if (!sdd->seenfrom) { 1380 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1381 "WARNING: could not send %s@%s:\n" 1382 "incremental source (%s@%s) does not exist\n"), 1383 zhp->zfs_name, sdd->tosnap, 1384 zhp->zfs_name, sdd->fromsnap); 1385 sdd->err = B_TRUE; 1386 } else if (!sdd->seento) { 1387 if (sdd->fromsnap) { 1388 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1389 "WARNING: could not send %s@%s:\n" 1390 "incremental source (%s@%s) " 1391 "is not earlier than it\n"), 1392 zhp->zfs_name, sdd->tosnap, 1393 zhp->zfs_name, sdd->fromsnap); 1394 } else { 1395 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1396 "WARNING: " 1397 "could not send %s@%s: does not exist\n"), 1398 zhp->zfs_name, sdd->tosnap); 1399 } 1400 sdd->err = B_TRUE; 1401 } 1402 1403 return (rv); 1404} 1405 1406static int 1407dump_filesystems(zfs_handle_t *rzhp, void *arg) 1408{ 1409 send_dump_data_t *sdd = arg; 1410 nvpair_t *fspair; 1411 boolean_t needagain, progress; 1412 1413 if (!sdd->replicate) 1414 return (dump_filesystem(rzhp, sdd)); 1415 1416 /* Mark the clone origin snapshots. */ 1417 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1418 fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1419 nvlist_t *nvfs; 1420 uint64_t origin_guid = 0; 1421 1422 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs)); 1423 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid); 1424 if (origin_guid != 0) { 1425 char *snapname; 1426 nvlist_t *origin_nv = fsavl_find(sdd->fsavl, 1427 origin_guid, &snapname); 1428 if (origin_nv != NULL) { 1429 nvlist_t *snapprops; 1430 VERIFY(0 == nvlist_lookup_nvlist(origin_nv, 1431 "snapprops", &snapprops)); 1432 VERIFY(0 == nvlist_lookup_nvlist(snapprops, 1433 snapname, &snapprops)); 1434 VERIFY(0 == nvlist_add_boolean( 1435 snapprops, "is_clone_origin")); 1436 } 1437 } 1438 } 1439again: 1440 needagain = progress = B_FALSE; 1441 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1442 fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1443 nvlist_t *fslist, *parent_nv; 1444 char *fsname; 1445 zfs_handle_t *zhp; 1446 int err; 1447 uint64_t origin_guid = 0; 1448 uint64_t parent_guid = 0; 1449 1450 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); 1451 if (nvlist_lookup_boolean(fslist, "sent") == 0) 1452 continue; 1453 1454 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0); 1455 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); 1456 (void) nvlist_lookup_uint64(fslist, "parentfromsnap", 1457 &parent_guid); 1458 1459 if (parent_guid != 0) { 1460 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL); 1461 if (!nvlist_exists(parent_nv, "sent")) { 1462 /* parent has not been sent; skip this one */ 1463 needagain = B_TRUE; 1464 continue; 1465 } 1466 } 1467 1468 if (origin_guid != 0) { 1469 nvlist_t *origin_nv = fsavl_find(sdd->fsavl, 1470 origin_guid, NULL); 1471 if (origin_nv != NULL && 1472 !nvlist_exists(origin_nv, "sent")) { 1473 /* 1474 * origin has not been sent yet; 1475 * skip this clone. 1476 */ 1477 needagain = B_TRUE; 1478 continue; 1479 } 1480 } 1481 1482 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); 1483 if (zhp == NULL) 1484 return (-1); 1485 err = dump_filesystem(zhp, sdd); 1486 VERIFY(nvlist_add_boolean(fslist, "sent") == 0); 1487 progress = B_TRUE; 1488 zfs_close(zhp); 1489 if (err) 1490 return (err); 1491 } 1492 if (needagain) { 1493 assert(progress); 1494 goto again; 1495 } 1496 1497 /* clean out the sent flags in case we reuse this fss */ 1498 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; 1499 fspair = nvlist_next_nvpair(sdd->fss, fspair)) { 1500 nvlist_t *fslist; 1501 1502 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); 1503 (void) nvlist_remove_all(fslist, "sent"); 1504 } 1505 1506 return (0); 1507} 1508 1509nvlist_t * 1510zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token) 1511{ 1512 unsigned int version; 1513 int nread; 1514 unsigned long long checksum, packed_len; 1515 1516 /* 1517 * Decode token header, which is: 1518 * <token version>-<checksum of payload>-<uncompressed payload length> 1519 * Note that the only supported token version is 1. 1520 */ 1521 nread = sscanf(token, "%u-%llx-%llx-", 1522 &version, &checksum, &packed_len); 1523 if (nread != 3) { 1524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1525 "resume token is corrupt (invalid format)")); 1526 return (NULL); 1527 } 1528 1529 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) { 1530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1531 "resume token is corrupt (invalid version %u)"), 1532 version); 1533 return (NULL); 1534 } 1535 1536 /* convert hexadecimal representation to binary */ 1537 token = strrchr(token, '-') + 1; 1538 int len = strlen(token) / 2; 1539 unsigned char *compressed = zfs_alloc(hdl, len); 1540 for (int i = 0; i < len; i++) { 1541 nread = sscanf(token + i * 2, "%2hhx", compressed + i); 1542 if (nread != 1) { 1543 free(compressed); 1544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1545 "resume token is corrupt " 1546 "(payload is not hex-encoded)")); 1547 return (NULL); 1548 } 1549 } 1550 1551 /* verify checksum */ 1552 zio_cksum_t cksum; 1553 fletcher_4_native(compressed, len, NULL, &cksum); 1554 if (cksum.zc_word[0] != checksum) { 1555 free(compressed); 1556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1557 "resume token is corrupt (incorrect checksum)")); 1558 return (NULL); 1559 } 1560 1561 /* uncompress */ 1562 void *packed = zfs_alloc(hdl, packed_len); 1563 uLongf packed_len_long = packed_len; 1564 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK || 1565 packed_len_long != packed_len) { 1566 free(packed); 1567 free(compressed); 1568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1569 "resume token is corrupt (decompression failed)")); 1570 return (NULL); 1571 } 1572 1573 /* unpack nvlist */ 1574 nvlist_t *nv; 1575 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP); 1576 free(packed); 1577 free(compressed); 1578 if (error != 0) { 1579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1580 "resume token is corrupt (nvlist_unpack failed)")); 1581 return (NULL); 1582 } 1583 return (nv); 1584} 1585 1586int 1587zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd, 1588 const char *resume_token) 1589{ 1590 char errbuf[1024]; 1591 char *toname; 1592 char *fromname = NULL; 1593 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes; 1594 zfs_handle_t *zhp; 1595 int error = 0; 1596 char name[ZFS_MAX_DATASET_NAME_LEN]; 1597 enum lzc_send_flags lzc_flags = 0; 1598 uint64_t size = 0; 1599 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr; 1600 1601 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1602 "cannot resume send")); 1603 1604 nvlist_t *resume_nvl = 1605 zfs_send_resume_token_to_nvlist(hdl, resume_token); 1606 if (resume_nvl == NULL) { 1607 /* 1608 * zfs_error_aux has already been set by 1609 * zfs_send_resume_token_to_nvlist 1610 */ 1611 return (zfs_error(hdl, EZFS_FAULT, errbuf)); 1612 } 1613 if (flags->verbose) { 1614 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1615 "resume token contents:\n")); 1616 nvlist_print(fout, resume_nvl); 1617 } 1618 1619 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 || 1620 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 || 1621 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 || 1622 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 || 1623 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) { 1624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1625 "resume token is corrupt")); 1626 return (zfs_error(hdl, EZFS_FAULT, errbuf)); 1627 } 1628 fromguid = 0; 1629 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid); 1630 1631 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok")) 1632 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 1633 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok")) 1634 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 1635 if (flags->compress || nvlist_exists(resume_nvl, "compressok")) 1636 lzc_flags |= LZC_SEND_FLAG_COMPRESS; 1637 1638 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) { 1639 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) { 1640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1641 "'%s' is no longer the same snapshot used in " 1642 "the initial send"), toname); 1643 } else { 1644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1645 "'%s' used in the initial send no longer exists"), 1646 toname); 1647 } 1648 return (zfs_error(hdl, EZFS_BADPATH, errbuf)); 1649 } 1650 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 1651 if (zhp == NULL) { 1652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1653 "unable to access '%s'"), name); 1654 return (zfs_error(hdl, EZFS_BADPATH, errbuf)); 1655 } 1656 1657 if (fromguid != 0) { 1658 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) { 1659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1660 "incremental source %#llx no longer exists"), 1661 (longlong_t)fromguid); 1662 return (zfs_error(hdl, EZFS_BADPATH, errbuf)); 1663 } 1664 fromname = name; 1665 } 1666 1667 if (flags->progress || flags->verbose) { 1668 error = lzc_send_space(zhp->zfs_name, fromname, 1669 lzc_flags, &size); 1670 if (error == 0) 1671 size = MAX(0, (int64_t)(size - bytes)); 1672 } 1673 if (flags->verbose) { 1674 send_print_verbose(fout, zhp->zfs_name, fromname, 1675 size, flags->parsable); 1676 } 1677 1678 if (!flags->dryrun) { 1679 progress_arg_t pa = { 0 }; 1680 pthread_t tid; 1681 /* 1682 * If progress reporting is requested, spawn a new thread to 1683 * poll ZFS_IOC_SEND_PROGRESS at a regular interval. 1684 */ 1685 if (flags->progress) { 1686 pa.pa_zhp = zhp; 1687 pa.pa_fd = outfd; 1688 pa.pa_parsable = flags->parsable; 1689 pa.pa_size = size; 1690 pa.pa_astitle = flags->progressastitle; 1691 1692 error = pthread_create(&tid, NULL, 1693 send_progress_thread, &pa); 1694 if (error != 0) { 1695 zfs_close(zhp); 1696 return (error); 1697 } 1698 } 1699 1700 error = lzc_send_resume(zhp->zfs_name, fromname, outfd, 1701 lzc_flags, resumeobj, resumeoff); 1702 1703 if (flags->progress) { 1704 (void) pthread_cancel(tid); 1705 (void) pthread_join(tid, NULL); 1706 } 1707 1708 char errbuf[1024]; 1709 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1710 "warning: cannot send '%s'"), zhp->zfs_name); 1711 1712 zfs_close(zhp); 1713 1714 switch (error) { 1715 case 0: 1716 return (0); 1717 case EXDEV: 1718 case ENOENT: 1719 case EDQUOT: 1720 case EFBIG: 1721 case EIO: 1722 case ENOLINK: 1723 case ENOSPC: 1724#ifdef illumos 1725 case ENOSTR: 1726#endif 1727 case ENXIO: 1728 case EPIPE: 1729 case ERANGE: 1730 case EFAULT: 1731 case EROFS: 1732 zfs_error_aux(hdl, strerror(errno)); 1733 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 1734 1735 default: 1736 return (zfs_standard_error(hdl, errno, errbuf)); 1737 } 1738 } 1739 1740 1741 zfs_close(zhp); 1742 1743 return (error); 1744} 1745 1746/* 1747 * Generate a send stream for the dataset identified by the argument zhp. 1748 * 1749 * The content of the send stream is the snapshot identified by 1750 * 'tosnap'. Incremental streams are requested in two ways: 1751 * - from the snapshot identified by "fromsnap" (if non-null) or 1752 * - from the origin of the dataset identified by zhp, which must 1753 * be a clone. In this case, "fromsnap" is null and "fromorigin" 1754 * is TRUE. 1755 * 1756 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and 1757 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM) 1758 * if "replicate" is set. If "doall" is set, dump all the intermediate 1759 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall" 1760 * case too. If "props" is set, send properties. 1761 */ 1762int 1763zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 1764 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func, 1765 void *cb_arg, nvlist_t **debugnvp) 1766{ 1767 char errbuf[1024]; 1768 send_dump_data_t sdd = { 0 }; 1769 int err = 0; 1770 nvlist_t *fss = NULL; 1771 avl_tree_t *fsavl = NULL; 1772 static uint64_t holdseq; 1773 int spa_version; 1774 pthread_t tid = 0; 1775 int pipefd[2]; 1776 dedup_arg_t dda = { 0 }; 1777 int featureflags = 0; 1778 FILE *fout; 1779 1780 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1781 "cannot send '%s'"), zhp->zfs_name); 1782 1783 if (fromsnap && fromsnap[0] == '\0') { 1784 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 1785 "zero-length incremental source")); 1786 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 1787 } 1788 1789 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) { 1790 uint64_t version; 1791 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1792 if (version >= ZPL_VERSION_SA) { 1793 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; 1794 } 1795 } 1796 1797 if (flags->dedup && !flags->dryrun) { 1798 featureflags |= (DMU_BACKUP_FEATURE_DEDUP | 1799 DMU_BACKUP_FEATURE_DEDUPPROPS); 1800 if ((err = pipe(pipefd)) != 0) { 1801 zfs_error_aux(zhp->zfs_hdl, strerror(errno)); 1802 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, 1803 errbuf)); 1804 } 1805 dda.outputfd = outfd; 1806 dda.inputfd = pipefd[1]; 1807 dda.dedup_hdl = zhp->zfs_hdl; 1808 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) { 1809 (void) close(pipefd[0]); 1810 (void) close(pipefd[1]); 1811 zfs_error_aux(zhp->zfs_hdl, strerror(errno)); 1812 return (zfs_error(zhp->zfs_hdl, 1813 EZFS_THREADCREATEFAILED, errbuf)); 1814 } 1815 } 1816 1817 if (flags->replicate || flags->doall || flags->props) { 1818 dmu_replay_record_t drr = { 0 }; 1819 char *packbuf = NULL; 1820 size_t buflen = 0; 1821 zio_cksum_t zc = { 0 }; 1822 1823 if (flags->replicate || flags->props) { 1824 nvlist_t *hdrnv; 1825 1826 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0)); 1827 if (fromsnap) { 1828 VERIFY(0 == nvlist_add_string(hdrnv, 1829 "fromsnap", fromsnap)); 1830 } 1831 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap)); 1832 if (!flags->replicate) { 1833 VERIFY(0 == nvlist_add_boolean(hdrnv, 1834 "not_recursive")); 1835 } 1836 1837 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name, 1838 fromsnap, tosnap, flags->replicate, flags->verbose, 1839 &fss, &fsavl); 1840 if (err) 1841 goto err_out; 1842 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss)); 1843 err = nvlist_pack(hdrnv, &packbuf, &buflen, 1844 NV_ENCODE_XDR, 0); 1845 if (debugnvp) 1846 *debugnvp = hdrnv; 1847 else 1848 nvlist_free(hdrnv); 1849 if (err) 1850 goto stderr_out; 1851 } 1852 1853 if (!flags->dryrun) { 1854 /* write first begin record */ 1855 drr.drr_type = DRR_BEGIN; 1856 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 1857 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin. 1858 drr_versioninfo, DMU_COMPOUNDSTREAM); 1859 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin. 1860 drr_versioninfo, featureflags); 1861 (void) snprintf(drr.drr_u.drr_begin.drr_toname, 1862 sizeof (drr.drr_u.drr_begin.drr_toname), 1863 "%s@%s", zhp->zfs_name, tosnap); 1864 drr.drr_payloadlen = buflen; 1865 1866 err = dump_record(&drr, packbuf, buflen, &zc, outfd); 1867 free(packbuf); 1868 if (err != 0) 1869 goto stderr_out; 1870 1871 /* write end record */ 1872 bzero(&drr, sizeof (drr)); 1873 drr.drr_type = DRR_END; 1874 drr.drr_u.drr_end.drr_checksum = zc; 1875 err = write(outfd, &drr, sizeof (drr)); 1876 if (err == -1) { 1877 err = errno; 1878 goto stderr_out; 1879 } 1880 1881 err = 0; 1882 } 1883 } 1884 1885 /* dump each stream */ 1886 sdd.fromsnap = fromsnap; 1887 sdd.tosnap = tosnap; 1888 if (tid != 0) 1889 sdd.outfd = pipefd[0]; 1890 else 1891 sdd.outfd = outfd; 1892 sdd.replicate = flags->replicate; 1893 sdd.doall = flags->doall; 1894 sdd.fromorigin = flags->fromorigin; 1895 sdd.fss = fss; 1896 sdd.fsavl = fsavl; 1897 sdd.verbose = flags->verbose; 1898 sdd.parsable = flags->parsable; 1899 sdd.progress = flags->progress; 1900 sdd.progressastitle = flags->progressastitle; 1901 sdd.dryrun = flags->dryrun; 1902 sdd.large_block = flags->largeblock; 1903 sdd.embed_data = flags->embed_data; 1904 sdd.compress = flags->compress; 1905 sdd.filter_cb = filter_func; 1906 sdd.filter_cb_arg = cb_arg; 1907 if (debugnvp) 1908 sdd.debugnv = *debugnvp; 1909 if (sdd.verbose && sdd.dryrun) 1910 sdd.std_out = B_TRUE; 1911 fout = sdd.std_out ? stdout : stderr; 1912 1913 /* 1914 * Some flags require that we place user holds on the datasets that are 1915 * being sent so they don't get destroyed during the send. We can skip 1916 * this step if the pool is imported read-only since the datasets cannot 1917 * be destroyed. 1918 */ 1919 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp), 1920 ZPOOL_PROP_READONLY, NULL) && 1921 zfs_spa_version(zhp, &spa_version) == 0 && 1922 spa_version >= SPA_VERSION_USERREFS && 1923 (flags->doall || flags->replicate)) { 1924 ++holdseq; 1925 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag), 1926 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq); 1927 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL); 1928 if (sdd.cleanup_fd < 0) { 1929 err = errno; 1930 goto stderr_out; 1931 } 1932 sdd.snapholds = fnvlist_alloc(); 1933 } else { 1934 sdd.cleanup_fd = -1; 1935 sdd.snapholds = NULL; 1936 } 1937 if (flags->progress || flags->verbose || sdd.snapholds != NULL) { 1938 /* 1939 * Do a verbose no-op dry run to get all the verbose output 1940 * or to gather snapshot hold's before generating any data, 1941 * then do a non-verbose real run to generate the streams. 1942 */ 1943 sdd.dryrun = B_TRUE; 1944 err = dump_filesystems(zhp, &sdd); 1945 1946 if (err != 0) 1947 goto stderr_out; 1948 1949 if (flags->verbose) { 1950 if (flags->parsable) { 1951 (void) fprintf(fout, "size\t%llu\n", 1952 (longlong_t)sdd.size); 1953 } else { 1954 char buf[16]; 1955 zfs_nicenum(sdd.size, buf, sizeof (buf)); 1956 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 1957 "total estimated size is %s\n"), buf); 1958 } 1959 } 1960 1961 /* Ensure no snaps found is treated as an error. */ 1962 if (!sdd.seento) { 1963 err = ENOENT; 1964 goto err_out; 1965 } 1966 1967 /* Skip the second run if dryrun was requested. */ 1968 if (flags->dryrun) 1969 goto err_out; 1970 1971 if (sdd.snapholds != NULL) { 1972 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds); 1973 if (err != 0) 1974 goto stderr_out; 1975 1976 fnvlist_free(sdd.snapholds); 1977 sdd.snapholds = NULL; 1978 } 1979 1980 sdd.dryrun = B_FALSE; 1981 sdd.verbose = B_FALSE; 1982 } 1983 1984 err = dump_filesystems(zhp, &sdd); 1985 fsavl_destroy(fsavl); 1986 nvlist_free(fss); 1987 1988 /* Ensure no snaps found is treated as an error. */ 1989 if (err == 0 && !sdd.seento) 1990 err = ENOENT; 1991 1992 if (tid != 0) { 1993 if (err != 0) 1994 (void) pthread_cancel(tid); 1995 (void) close(pipefd[0]); 1996 (void) pthread_join(tid, NULL); 1997 } 1998 1999 if (sdd.cleanup_fd != -1) { 2000 VERIFY(0 == close(sdd.cleanup_fd)); 2001 sdd.cleanup_fd = -1; 2002 } 2003 2004 if (!flags->dryrun && (flags->replicate || flags->doall || 2005 flags->props)) { 2006 /* 2007 * write final end record. NB: want to do this even if 2008 * there was some error, because it might not be totally 2009 * failed. 2010 */ 2011 dmu_replay_record_t drr = { 0 }; 2012 drr.drr_type = DRR_END; 2013 if (write(outfd, &drr, sizeof (drr)) == -1) { 2014 return (zfs_standard_error(zhp->zfs_hdl, 2015 errno, errbuf)); 2016 } 2017 } 2018 2019 return (err || sdd.err); 2020 2021stderr_out: 2022 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf); 2023err_out: 2024 fsavl_destroy(fsavl); 2025 nvlist_free(fss); 2026 fnvlist_free(sdd.snapholds); 2027 2028 if (sdd.cleanup_fd != -1) 2029 VERIFY(0 == close(sdd.cleanup_fd)); 2030 if (tid != 0) { 2031 (void) pthread_cancel(tid); 2032 (void) close(pipefd[0]); 2033 (void) pthread_join(tid, NULL); 2034 } 2035 return (err); 2036} 2037 2038int 2039zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t flags) 2040{ 2041 int err = 0; 2042 libzfs_handle_t *hdl = zhp->zfs_hdl; 2043 enum lzc_send_flags lzc_flags = 0; 2044 FILE *fout = (flags.verbose && flags.dryrun) ? stdout : stderr; 2045 char errbuf[1024]; 2046 2047 if (flags.largeblock) 2048 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 2049 if (flags.embed_data) 2050 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 2051 if (flags.compress) 2052 lzc_flags |= LZC_SEND_FLAG_COMPRESS; 2053 2054 if (flags.verbose) { 2055 uint64_t size = 0; 2056 err = lzc_send_space(zhp->zfs_name, from, lzc_flags, &size); 2057 if (err == 0) { 2058 send_print_verbose(fout, zhp->zfs_name, from, size, 2059 flags.parsable); 2060 if (flags.parsable) { 2061 (void) fprintf(fout, "size\t%llu\n", 2062 (longlong_t)size); 2063 } else { 2064 char buf[16]; 2065 zfs_nicenum(size, buf, sizeof (buf)); 2066 (void) fprintf(fout, dgettext(TEXT_DOMAIN, 2067 "total estimated size is %s\n"), buf); 2068 } 2069 } else { 2070 (void) fprintf(stderr, "Cannot estimate send size: " 2071 "%s\n", strerror(errno)); 2072 } 2073 } 2074 2075 if (flags.dryrun) 2076 return (err); 2077 2078 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2079 "warning: cannot send '%s'"), zhp->zfs_name); 2080 2081 err = lzc_send(zhp->zfs_name, from, fd, lzc_flags); 2082 if (err != 0) { 2083 switch (errno) { 2084 case EXDEV: 2085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2086 "not an earlier snapshot from the same fs")); 2087 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 2088 2089 case ENOENT: 2090 case ESRCH: 2091 if (lzc_exists(zhp->zfs_name)) { 2092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2093 "incremental source (%s) does not exist"), 2094 from); 2095 } 2096 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2097 2098 case EBUSY: 2099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2100 "target is busy; if a filesystem, " 2101 "it must not be mounted")); 2102 return (zfs_error(hdl, EZFS_BUSY, errbuf)); 2103 2104 case EDQUOT: 2105 case EFBIG: 2106 case EIO: 2107 case ENOLINK: 2108 case ENOSPC: 2109#ifdef illumos 2110 case ENOSTR: 2111#endif 2112 case ENXIO: 2113 case EPIPE: 2114 case ERANGE: 2115 case EFAULT: 2116 case EROFS: 2117 zfs_error_aux(hdl, strerror(errno)); 2118 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 2119 2120 default: 2121 return (zfs_standard_error(hdl, errno, errbuf)); 2122 } 2123 } 2124 return (err != 0); 2125} 2126 2127/* 2128 * Routines specific to "zfs recv" 2129 */ 2130 2131static int 2132recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, 2133 boolean_t byteswap, zio_cksum_t *zc) 2134{ 2135 char *cp = buf; 2136 int rv; 2137 int len = ilen; 2138 2139 assert(ilen <= SPA_MAXBLOCKSIZE); 2140 2141 do { 2142 rv = read(fd, cp, len); 2143 cp += rv; 2144 len -= rv; 2145 } while (rv > 0); 2146 2147 if (rv < 0 || len != 0) { 2148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2149 "failed to read from stream")); 2150 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, 2151 "cannot receive"))); 2152 } 2153 2154 if (zc) { 2155 if (byteswap) 2156 (void) fletcher_4_incremental_byteswap(buf, ilen, zc); 2157 else 2158 (void) fletcher_4_incremental_native(buf, ilen, zc); 2159 } 2160 return (0); 2161} 2162 2163static int 2164recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, 2165 boolean_t byteswap, zio_cksum_t *zc) 2166{ 2167 char *buf; 2168 int err; 2169 2170 buf = zfs_alloc(hdl, len); 2171 if (buf == NULL) 2172 return (ENOMEM); 2173 2174 err = recv_read(hdl, fd, buf, len, byteswap, zc); 2175 if (err != 0) { 2176 free(buf); 2177 return (err); 2178 } 2179 2180 err = nvlist_unpack(buf, len, nvp, 0); 2181 free(buf); 2182 if (err != 0) { 2183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 2184 "stream (malformed nvlist)")); 2185 return (EINVAL); 2186 } 2187 return (0); 2188} 2189 2190static int 2191recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, 2192 int baselen, char *newname, recvflags_t *flags) 2193{ 2194 static int seq; 2195 int err; 2196 prop_changelist_t *clp; 2197 zfs_handle_t *zhp; 2198 2199 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 2200 if (zhp == NULL) 2201 return (-1); 2202 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 2203 flags->force ? MS_FORCE : 0); 2204 zfs_close(zhp); 2205 if (clp == NULL) 2206 return (-1); 2207 err = changelist_prefix(clp); 2208 if (err) 2209 return (err); 2210 2211 if (tryname) { 2212 (void) strcpy(newname, tryname); 2213 if (flags->verbose) { 2214 (void) printf("attempting rename %s to %s\n", 2215 name, newname); 2216 } 2217 err = lzc_rename(name, newname); 2218 if (err == 0) 2219 changelist_rename(clp, name, tryname); 2220 } else { 2221 err = ENOENT; 2222 } 2223 2224 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) { 2225 seq++; 2226 2227 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN, 2228 "%.*srecv-%u-%u", baselen, name, getpid(), seq); 2229 if (flags->verbose) { 2230 (void) printf("failed - trying rename %s to %s\n", 2231 name, newname); 2232 } 2233 err = lzc_rename(name, newname); 2234 if (err == 0) 2235 changelist_rename(clp, name, newname); 2236 if (err && flags->verbose) { 2237 (void) printf("failed (%u) - " 2238 "will try again on next pass\n", errno); 2239 } 2240 err = EAGAIN; 2241 } else if (flags->verbose) { 2242 if (err == 0) 2243 (void) printf("success\n"); 2244 else 2245 (void) printf("failed (%u)\n", errno); 2246 } 2247 2248 (void) changelist_postfix(clp); 2249 changelist_free(clp); 2250 2251 return (err); 2252} 2253 2254static int 2255recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, 2256 char *newname, recvflags_t *flags) 2257{ 2258 int err = 0; 2259 prop_changelist_t *clp; 2260 zfs_handle_t *zhp; 2261 boolean_t defer = B_FALSE; 2262 int spa_version; 2263 2264 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); 2265 if (zhp == NULL) 2266 return (-1); 2267 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 2268 flags->force ? MS_FORCE : 0); 2269 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 2270 zfs_spa_version(zhp, &spa_version) == 0 && 2271 spa_version >= SPA_VERSION_USERREFS) 2272 defer = B_TRUE; 2273 zfs_close(zhp); 2274 if (clp == NULL) 2275 return (-1); 2276 err = changelist_prefix(clp); 2277 if (err) 2278 return (err); 2279 2280 if (flags->verbose) 2281 (void) printf("attempting destroy %s\n", name); 2282 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 2283 nvlist_t *nv = fnvlist_alloc(); 2284 fnvlist_add_boolean(nv, name); 2285 err = lzc_destroy_snaps(nv, defer, NULL); 2286 fnvlist_free(nv); 2287 } else { 2288 err = lzc_destroy(name); 2289 } 2290 if (err == 0) { 2291 if (flags->verbose) 2292 (void) printf("success\n"); 2293 changelist_remove(clp, name); 2294 } 2295 2296 (void) changelist_postfix(clp); 2297 changelist_free(clp); 2298 2299 /* 2300 * Deferred destroy might destroy the snapshot or only mark it to be 2301 * destroyed later, and it returns success in either case. 2302 */ 2303 if (err != 0 || (defer && zfs_dataset_exists(hdl, name, 2304 ZFS_TYPE_SNAPSHOT))) { 2305 err = recv_rename(hdl, name, NULL, baselen, newname, flags); 2306 } 2307 2308 return (err); 2309} 2310 2311typedef struct guid_to_name_data { 2312 uint64_t guid; 2313 boolean_t bookmark_ok; 2314 char *name; 2315 char *skip; 2316} guid_to_name_data_t; 2317 2318static int 2319guid_to_name_cb(zfs_handle_t *zhp, void *arg) 2320{ 2321 guid_to_name_data_t *gtnd = arg; 2322 const char *slash; 2323 int err; 2324 2325 if (gtnd->skip != NULL && 2326 (slash = strrchr(zhp->zfs_name, '/')) != NULL && 2327 strcmp(slash + 1, gtnd->skip) == 0) { 2328 zfs_close(zhp); 2329 return (0); 2330 } 2331 2332 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) { 2333 (void) strcpy(gtnd->name, zhp->zfs_name); 2334 zfs_close(zhp); 2335 return (EEXIST); 2336 } 2337 2338 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); 2339 if (err != EEXIST && gtnd->bookmark_ok) 2340 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd); 2341 zfs_close(zhp); 2342 return (err); 2343} 2344 2345/* 2346 * Attempt to find the local dataset associated with this guid. In the case of 2347 * multiple matches, we attempt to find the "best" match by searching 2348 * progressively larger portions of the hierarchy. This allows one to send a 2349 * tree of datasets individually and guarantee that we will find the source 2350 * guid within that hierarchy, even if there are multiple matches elsewhere. 2351 */ 2352static int 2353guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, 2354 boolean_t bookmark_ok, char *name) 2355{ 2356 char pname[ZFS_MAX_DATASET_NAME_LEN]; 2357 guid_to_name_data_t gtnd; 2358 2359 gtnd.guid = guid; 2360 gtnd.bookmark_ok = bookmark_ok; 2361 gtnd.name = name; 2362 gtnd.skip = NULL; 2363 2364 /* 2365 * Search progressively larger portions of the hierarchy, starting 2366 * with the filesystem specified by 'parent'. This will 2367 * select the "most local" version of the origin snapshot in the case 2368 * that there are multiple matching snapshots in the system. 2369 */ 2370 (void) strlcpy(pname, parent, sizeof (pname)); 2371 char *cp = strrchr(pname, '@'); 2372 if (cp == NULL) 2373 cp = strchr(pname, '\0'); 2374 for (; cp != NULL; cp = strrchr(pname, '/')) { 2375 /* Chop off the last component and open the parent */ 2376 *cp = '\0'; 2377 zfs_handle_t *zhp = make_dataset_handle(hdl, pname); 2378 2379 if (zhp == NULL) 2380 continue; 2381 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd); 2382 if (err != EEXIST) 2383 err = zfs_iter_children(zhp, guid_to_name_cb, >nd); 2384 if (err != EEXIST && bookmark_ok) 2385 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd); 2386 zfs_close(zhp); 2387 if (err == EEXIST) 2388 return (0); 2389 2390 /* 2391 * Remember the last portion of the dataset so we skip it next 2392 * time through (as we've already searched that portion of the 2393 * hierarchy). 2394 */ 2395 gtnd.skip = strrchr(pname, '/') + 1; 2396 } 2397 2398 return (ENOENT); 2399} 2400 2401/* 2402 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if 2403 * guid1 is after guid2. 2404 */ 2405static int 2406created_before(libzfs_handle_t *hdl, avl_tree_t *avl, 2407 uint64_t guid1, uint64_t guid2) 2408{ 2409 nvlist_t *nvfs; 2410 char *fsname, *snapname; 2411 char buf[ZFS_MAX_DATASET_NAME_LEN]; 2412 int rv; 2413 zfs_handle_t *guid1hdl, *guid2hdl; 2414 uint64_t create1, create2; 2415 2416 if (guid2 == 0) 2417 return (0); 2418 if (guid1 == 0) 2419 return (1); 2420 2421 nvfs = fsavl_find(avl, guid1, &snapname); 2422 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 2423 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 2424 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 2425 if (guid1hdl == NULL) 2426 return (-1); 2427 2428 nvfs = fsavl_find(avl, guid2, &snapname); 2429 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 2430 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); 2431 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); 2432 if (guid2hdl == NULL) { 2433 zfs_close(guid1hdl); 2434 return (-1); 2435 } 2436 2437 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG); 2438 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG); 2439 2440 if (create1 < create2) 2441 rv = -1; 2442 else if (create1 > create2) 2443 rv = +1; 2444 else 2445 rv = 0; 2446 2447 zfs_close(guid1hdl); 2448 zfs_close(guid2hdl); 2449 2450 return (rv); 2451} 2452 2453static int 2454recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, 2455 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl, 2456 nvlist_t *renamed) 2457{ 2458 nvlist_t *local_nv, *deleted = NULL; 2459 avl_tree_t *local_avl; 2460 nvpair_t *fselem, *nextfselem; 2461 char *fromsnap; 2462 char newname[ZFS_MAX_DATASET_NAME_LEN]; 2463 char guidname[32]; 2464 int error; 2465 boolean_t needagain, progress, recursive; 2466 char *s1, *s2; 2467 2468 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap)); 2469 2470 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 2471 ENOENT); 2472 2473 if (flags->dryrun) 2474 return (0); 2475 2476again: 2477 needagain = progress = B_FALSE; 2478 2479 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0)); 2480 2481 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, 2482 recursive, B_FALSE, &local_nv, &local_avl)) != 0) 2483 return (error); 2484 2485 /* 2486 * Process deletes and renames 2487 */ 2488 for (fselem = nvlist_next_nvpair(local_nv, NULL); 2489 fselem; fselem = nextfselem) { 2490 nvlist_t *nvfs, *snaps; 2491 nvlist_t *stream_nvfs = NULL; 2492 nvpair_t *snapelem, *nextsnapelem; 2493 uint64_t fromguid = 0; 2494 uint64_t originguid = 0; 2495 uint64_t stream_originguid = 0; 2496 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; 2497 char *fsname, *stream_fsname; 2498 2499 nextfselem = nvlist_next_nvpair(local_nv, fselem); 2500 2501 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); 2502 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); 2503 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); 2504 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap", 2505 &parent_fromsnap_guid)); 2506 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); 2507 2508 /* 2509 * First find the stream's fs, so we can check for 2510 * a different origin (due to "zfs promote") 2511 */ 2512 for (snapelem = nvlist_next_nvpair(snaps, NULL); 2513 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { 2514 uint64_t thisguid; 2515 2516 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); 2517 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); 2518 2519 if (stream_nvfs != NULL) 2520 break; 2521 } 2522 2523 /* check for promote */ 2524 (void) nvlist_lookup_uint64(stream_nvfs, "origin", 2525 &stream_originguid); 2526 if (stream_nvfs && originguid != stream_originguid) { 2527 switch (created_before(hdl, local_avl, 2528 stream_originguid, originguid)) { 2529 case 1: { 2530 /* promote it! */ 2531 zfs_cmd_t zc = { 0 }; 2532 nvlist_t *origin_nvfs; 2533 char *origin_fsname; 2534 2535 if (flags->verbose) 2536 (void) printf("promoting %s\n", fsname); 2537 2538 origin_nvfs = fsavl_find(local_avl, originguid, 2539 NULL); 2540 VERIFY(0 == nvlist_lookup_string(origin_nvfs, 2541 "name", &origin_fsname)); 2542 (void) strlcpy(zc.zc_value, origin_fsname, 2543 sizeof (zc.zc_value)); 2544 (void) strlcpy(zc.zc_name, fsname, 2545 sizeof (zc.zc_name)); 2546 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 2547 if (error == 0) 2548 progress = B_TRUE; 2549 break; 2550 } 2551 default: 2552 break; 2553 case -1: 2554 fsavl_destroy(local_avl); 2555 nvlist_free(local_nv); 2556 return (-1); 2557 } 2558 /* 2559 * We had/have the wrong origin, therefore our 2560 * list of snapshots is wrong. Need to handle 2561 * them on the next pass. 2562 */ 2563 needagain = B_TRUE; 2564 continue; 2565 } 2566 2567 for (snapelem = nvlist_next_nvpair(snaps, NULL); 2568 snapelem; snapelem = nextsnapelem) { 2569 uint64_t thisguid; 2570 char *stream_snapname; 2571 nvlist_t *found, *props; 2572 2573 nextsnapelem = nvlist_next_nvpair(snaps, snapelem); 2574 2575 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); 2576 found = fsavl_find(stream_avl, thisguid, 2577 &stream_snapname); 2578 2579 /* check for delete */ 2580 if (found == NULL) { 2581 char name[ZFS_MAX_DATASET_NAME_LEN]; 2582 2583 if (!flags->force) 2584 continue; 2585 2586 (void) snprintf(name, sizeof (name), "%s@%s", 2587 fsname, nvpair_name(snapelem)); 2588 2589 error = recv_destroy(hdl, name, 2590 strlen(fsname)+1, newname, flags); 2591 if (error) 2592 needagain = B_TRUE; 2593 else 2594 progress = B_TRUE; 2595 sprintf(guidname, "%" PRIu64, thisguid); 2596 nvlist_add_boolean(deleted, guidname); 2597 continue; 2598 } 2599 2600 stream_nvfs = found; 2601 2602 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops", 2603 &props) && 0 == nvlist_lookup_nvlist(props, 2604 stream_snapname, &props)) { 2605 zfs_cmd_t zc = { 0 }; 2606 2607 zc.zc_cookie = B_TRUE; /* received */ 2608 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), 2609 "%s@%s", fsname, nvpair_name(snapelem)); 2610 if (zcmd_write_src_nvlist(hdl, &zc, 2611 props) == 0) { 2612 (void) zfs_ioctl(hdl, 2613 ZFS_IOC_SET_PROP, &zc); 2614 zcmd_free_nvlists(&zc); 2615 } 2616 } 2617 2618 /* check for different snapname */ 2619 if (strcmp(nvpair_name(snapelem), 2620 stream_snapname) != 0) { 2621 char name[ZFS_MAX_DATASET_NAME_LEN]; 2622 char tryname[ZFS_MAX_DATASET_NAME_LEN]; 2623 2624 (void) snprintf(name, sizeof (name), "%s@%s", 2625 fsname, nvpair_name(snapelem)); 2626 (void) snprintf(tryname, sizeof (name), "%s@%s", 2627 fsname, stream_snapname); 2628 2629 error = recv_rename(hdl, name, tryname, 2630 strlen(fsname)+1, newname, flags); 2631 if (error) 2632 needagain = B_TRUE; 2633 else 2634 progress = B_TRUE; 2635 } 2636 2637 if (strcmp(stream_snapname, fromsnap) == 0) 2638 fromguid = thisguid; 2639 } 2640 2641 /* check for delete */ 2642 if (stream_nvfs == NULL) { 2643 if (!flags->force) 2644 continue; 2645 2646 error = recv_destroy(hdl, fsname, strlen(tofs)+1, 2647 newname, flags); 2648 if (error) 2649 needagain = B_TRUE; 2650 else 2651 progress = B_TRUE; 2652 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid); 2653 nvlist_add_boolean(deleted, guidname); 2654 continue; 2655 } 2656 2657 if (fromguid == 0) { 2658 if (flags->verbose) { 2659 (void) printf("local fs %s does not have " 2660 "fromsnap (%s in stream); must have " 2661 "been deleted locally; ignoring\n", 2662 fsname, fromsnap); 2663 } 2664 continue; 2665 } 2666 2667 VERIFY(0 == nvlist_lookup_string(stream_nvfs, 2668 "name", &stream_fsname)); 2669 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs, 2670 "parentfromsnap", &stream_parent_fromsnap_guid)); 2671 2672 s1 = strrchr(fsname, '/'); 2673 s2 = strrchr(stream_fsname, '/'); 2674 2675 /* 2676 * Check if we're going to rename based on parent guid change 2677 * and the current parent guid was also deleted. If it was then 2678 * rename will fail and is likely unneeded, so avoid this and 2679 * force an early retry to determine the new 2680 * parent_fromsnap_guid. 2681 */ 2682 if (stream_parent_fromsnap_guid != 0 && 2683 parent_fromsnap_guid != 0 && 2684 stream_parent_fromsnap_guid != parent_fromsnap_guid) { 2685 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid); 2686 if (nvlist_exists(deleted, guidname)) { 2687 progress = B_TRUE; 2688 needagain = B_TRUE; 2689 goto doagain; 2690 } 2691 } 2692 2693 /* 2694 * Check for rename. If the exact receive path is specified, it 2695 * does not count as a rename, but we still need to check the 2696 * datasets beneath it. 2697 */ 2698 if ((stream_parent_fromsnap_guid != 0 && 2699 parent_fromsnap_guid != 0 && 2700 stream_parent_fromsnap_guid != parent_fromsnap_guid) || 2701 ((flags->isprefix || strcmp(tofs, fsname) != 0) && 2702 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) { 2703 nvlist_t *parent; 2704 char tryname[ZFS_MAX_DATASET_NAME_LEN]; 2705 2706 parent = fsavl_find(local_avl, 2707 stream_parent_fromsnap_guid, NULL); 2708 /* 2709 * NB: parent might not be found if we used the 2710 * tosnap for stream_parent_fromsnap_guid, 2711 * because the parent is a newly-created fs; 2712 * we'll be able to rename it after we recv the 2713 * new fs. 2714 */ 2715 if (parent != NULL) { 2716 char *pname; 2717 2718 VERIFY(0 == nvlist_lookup_string(parent, "name", 2719 &pname)); 2720 (void) snprintf(tryname, sizeof (tryname), 2721 "%s%s", pname, strrchr(stream_fsname, '/')); 2722 } else { 2723 tryname[0] = '\0'; 2724 if (flags->verbose) { 2725 (void) printf("local fs %s new parent " 2726 "not found\n", fsname); 2727 } 2728 } 2729 2730 newname[0] = '\0'; 2731 2732 error = recv_rename(hdl, fsname, tryname, 2733 strlen(tofs)+1, newname, flags); 2734 2735 if (renamed != NULL && newname[0] != '\0') { 2736 VERIFY(0 == nvlist_add_boolean(renamed, 2737 newname)); 2738 } 2739 2740 if (error) 2741 needagain = B_TRUE; 2742 else 2743 progress = B_TRUE; 2744 } 2745 } 2746 2747doagain: 2748 fsavl_destroy(local_avl); 2749 nvlist_free(local_nv); 2750 nvlist_free(deleted); 2751 2752 if (needagain && progress) { 2753 /* do another pass to fix up temporary names */ 2754 if (flags->verbose) 2755 (void) printf("another pass:\n"); 2756 goto again; 2757 } 2758 2759 return (needagain); 2760} 2761 2762static int 2763zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, 2764 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc, 2765 char **top_zfs, int cleanup_fd, uint64_t *action_handlep) 2766{ 2767 nvlist_t *stream_nv = NULL; 2768 avl_tree_t *stream_avl = NULL; 2769 char *fromsnap = NULL; 2770 char *sendsnap = NULL; 2771 char *cp; 2772 char tofs[ZFS_MAX_DATASET_NAME_LEN]; 2773 char sendfs[ZFS_MAX_DATASET_NAME_LEN]; 2774 char errbuf[1024]; 2775 dmu_replay_record_t drre; 2776 int error; 2777 boolean_t anyerr = B_FALSE; 2778 boolean_t softerr = B_FALSE; 2779 boolean_t recursive; 2780 2781 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2782 "cannot receive")); 2783 2784 assert(drr->drr_type == DRR_BEGIN); 2785 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); 2786 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) == 2787 DMU_COMPOUNDSTREAM); 2788 2789 /* 2790 * Read in the nvlist from the stream. 2791 */ 2792 if (drr->drr_payloadlen != 0) { 2793 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, 2794 &stream_nv, flags->byteswap, zc); 2795 if (error) { 2796 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 2797 goto out; 2798 } 2799 } 2800 2801 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 2802 ENOENT); 2803 2804 if (recursive && strchr(destname, '@')) { 2805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2806 "cannot specify snapshot name for multi-snapshot stream")); 2807 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 2808 goto out; 2809 } 2810 2811 /* 2812 * Read in the end record and verify checksum. 2813 */ 2814 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), 2815 flags->byteswap, NULL))) 2816 goto out; 2817 if (flags->byteswap) { 2818 drre.drr_type = BSWAP_32(drre.drr_type); 2819 drre.drr_u.drr_end.drr_checksum.zc_word[0] = 2820 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); 2821 drre.drr_u.drr_end.drr_checksum.zc_word[1] = 2822 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); 2823 drre.drr_u.drr_end.drr_checksum.zc_word[2] = 2824 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); 2825 drre.drr_u.drr_end.drr_checksum.zc_word[3] = 2826 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); 2827 } 2828 if (drre.drr_type != DRR_END) { 2829 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 2830 goto out; 2831 } 2832 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { 2833 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2834 "incorrect header checksum")); 2835 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); 2836 goto out; 2837 } 2838 2839 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); 2840 2841 if (drr->drr_payloadlen != 0) { 2842 nvlist_t *stream_fss; 2843 2844 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", 2845 &stream_fss)); 2846 if ((stream_avl = fsavl_create(stream_fss)) == NULL) { 2847 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2848 "couldn't allocate avl tree")); 2849 error = zfs_error(hdl, EZFS_NOMEM, errbuf); 2850 goto out; 2851 } 2852 2853 if (fromsnap != NULL && recursive) { 2854 nvlist_t *renamed = NULL; 2855 nvpair_t *pair = NULL; 2856 2857 (void) strlcpy(tofs, destname, sizeof (tofs)); 2858 if (flags->isprefix) { 2859 struct drr_begin *drrb = &drr->drr_u.drr_begin; 2860 int i; 2861 2862 if (flags->istail) { 2863 cp = strrchr(drrb->drr_toname, '/'); 2864 if (cp == NULL) { 2865 (void) strlcat(tofs, "/", 2866 sizeof (tofs)); 2867 i = 0; 2868 } else { 2869 i = (cp - drrb->drr_toname); 2870 } 2871 } else { 2872 i = strcspn(drrb->drr_toname, "/@"); 2873 } 2874 /* zfs_receive_one() will create_parents() */ 2875 (void) strlcat(tofs, &drrb->drr_toname[i], 2876 sizeof (tofs)); 2877 *strchr(tofs, '@') = '\0'; 2878 } 2879 2880 if (!flags->dryrun && !flags->nomount) { 2881 VERIFY(0 == nvlist_alloc(&renamed, 2882 NV_UNIQUE_NAME, 0)); 2883 } 2884 2885 softerr = recv_incremental_replication(hdl, tofs, flags, 2886 stream_nv, stream_avl, renamed); 2887 2888 /* Unmount renamed filesystems before receiving. */ 2889 while ((pair = nvlist_next_nvpair(renamed, 2890 pair)) != NULL) { 2891 zfs_handle_t *zhp; 2892 prop_changelist_t *clp = NULL; 2893 2894 zhp = zfs_open(hdl, nvpair_name(pair), 2895 ZFS_TYPE_FILESYSTEM); 2896 if (zhp != NULL) { 2897 clp = changelist_gather(zhp, 2898 ZFS_PROP_MOUNTPOINT, 0, 0); 2899 zfs_close(zhp); 2900 if (clp != NULL) { 2901 softerr |= 2902 changelist_prefix(clp); 2903 changelist_free(clp); 2904 } 2905 } 2906 } 2907 2908 nvlist_free(renamed); 2909 } 2910 } 2911 2912 /* 2913 * Get the fs specified by the first path in the stream (the top level 2914 * specified by 'zfs send') and pass it to each invocation of 2915 * zfs_receive_one(). 2916 */ 2917 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname, 2918 sizeof (sendfs)); 2919 if ((cp = strchr(sendfs, '@')) != NULL) { 2920 *cp = '\0'; 2921 /* 2922 * Find the "sendsnap", the final snapshot in a replication 2923 * stream. zfs_receive_one() handles certain errors 2924 * differently, depending on if the contained stream is the 2925 * last one or not. 2926 */ 2927 sendsnap = (cp + 1); 2928 } 2929 2930 /* Finally, receive each contained stream */ 2931 do { 2932 /* 2933 * we should figure out if it has a recoverable 2934 * error, in which case do a recv_skip() and drive on. 2935 * Note, if we fail due to already having this guid, 2936 * zfs_receive_one() will take care of it (ie, 2937 * recv_skip() and return 0). 2938 */ 2939 error = zfs_receive_impl(hdl, destname, NULL, flags, fd, 2940 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd, 2941 action_handlep, sendsnap); 2942 if (error == ENODATA) { 2943 error = 0; 2944 break; 2945 } 2946 anyerr |= error; 2947 } while (error == 0); 2948 2949 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) { 2950 /* 2951 * Now that we have the fs's they sent us, try the 2952 * renames again. 2953 */ 2954 softerr = recv_incremental_replication(hdl, tofs, flags, 2955 stream_nv, stream_avl, NULL); 2956 } 2957 2958out: 2959 fsavl_destroy(stream_avl); 2960 nvlist_free(stream_nv); 2961 if (softerr) 2962 error = -2; 2963 if (anyerr) 2964 error = -1; 2965 return (error); 2966} 2967 2968static void 2969trunc_prop_errs(int truncated) 2970{ 2971 ASSERT(truncated != 0); 2972 2973 if (truncated == 1) 2974 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 2975 "1 more property could not be set\n")); 2976 else 2977 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 2978 "%d more properties could not be set\n"), truncated); 2979} 2980 2981static int 2982recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) 2983{ 2984 dmu_replay_record_t *drr; 2985 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE); 2986 char errbuf[1024]; 2987 2988 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2989 "cannot receive:")); 2990 2991 /* XXX would be great to use lseek if possible... */ 2992 drr = buf; 2993 2994 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), 2995 byteswap, NULL) == 0) { 2996 if (byteswap) 2997 drr->drr_type = BSWAP_32(drr->drr_type); 2998 2999 switch (drr->drr_type) { 3000 case DRR_BEGIN: 3001 if (drr->drr_payloadlen != 0) { 3002 (void) recv_read(hdl, fd, buf, 3003 drr->drr_payloadlen, B_FALSE, NULL); 3004 } 3005 break; 3006 3007 case DRR_END: 3008 free(buf); 3009 return (0); 3010 3011 case DRR_OBJECT: 3012 if (byteswap) { 3013 drr->drr_u.drr_object.drr_bonuslen = 3014 BSWAP_32(drr->drr_u.drr_object. 3015 drr_bonuslen); 3016 } 3017 (void) recv_read(hdl, fd, buf, 3018 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8), 3019 B_FALSE, NULL); 3020 break; 3021 3022 case DRR_WRITE: 3023 if (byteswap) { 3024 drr->drr_u.drr_write.drr_logical_size = 3025 BSWAP_64( 3026 drr->drr_u.drr_write.drr_logical_size); 3027 drr->drr_u.drr_write.drr_compressed_size = 3028 BSWAP_64( 3029 drr->drr_u.drr_write.drr_compressed_size); 3030 } 3031 uint64_t payload_size = 3032 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write); 3033 (void) recv_read(hdl, fd, buf, 3034 payload_size, B_FALSE, NULL); 3035 break; 3036 case DRR_SPILL: 3037 if (byteswap) { 3038 drr->drr_u.drr_spill.drr_length = 3039 BSWAP_64(drr->drr_u.drr_spill.drr_length); 3040 } 3041 (void) recv_read(hdl, fd, buf, 3042 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL); 3043 break; 3044 case DRR_WRITE_EMBEDDED: 3045 if (byteswap) { 3046 drr->drr_u.drr_write_embedded.drr_psize = 3047 BSWAP_32(drr->drr_u.drr_write_embedded. 3048 drr_psize); 3049 } 3050 (void) recv_read(hdl, fd, buf, 3051 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize, 3052 8), B_FALSE, NULL); 3053 break; 3054 case DRR_WRITE_BYREF: 3055 case DRR_FREEOBJECTS: 3056 case DRR_FREE: 3057 break; 3058 3059 default: 3060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3061 "invalid record type")); 3062 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3063 } 3064 } 3065 3066 free(buf); 3067 return (-1); 3068} 3069 3070static void 3071recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap, 3072 boolean_t resumable) 3073{ 3074 char target_fs[ZFS_MAX_DATASET_NAME_LEN]; 3075 3076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3077 "checksum mismatch or incomplete stream")); 3078 3079 if (!resumable) 3080 return; 3081 (void) strlcpy(target_fs, target_snap, sizeof (target_fs)); 3082 *strchr(target_fs, '@') = '\0'; 3083 zfs_handle_t *zhp = zfs_open(hdl, target_fs, 3084 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3085 if (zhp == NULL) 3086 return; 3087 3088 char token_buf[ZFS_MAXPROPLEN]; 3089 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 3090 token_buf, sizeof (token_buf), 3091 NULL, NULL, 0, B_TRUE); 3092 if (error == 0) { 3093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3094 "checksum mismatch or incomplete stream.\n" 3095 "Partially received snapshot is saved.\n" 3096 "A resuming stream can be generated on the sending " 3097 "system by running:\n" 3098 " zfs send -t %s"), 3099 token_buf); 3100 } 3101 zfs_close(zhp); 3102} 3103 3104/* 3105 * Restores a backup of tosnap from the file descriptor specified by infd. 3106 */ 3107static int 3108zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, 3109 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr, 3110 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv, 3111 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd, 3112 uint64_t *action_handlep, const char *finalsnap) 3113{ 3114 zfs_cmd_t zc = { 0 }; 3115 time_t begin_time; 3116 int ioctl_err, ioctl_errno, err; 3117 char *cp; 3118 struct drr_begin *drrb = &drr->drr_u.drr_begin; 3119 char errbuf[1024]; 3120 char prop_errbuf[1024]; 3121 const char *chopprefix; 3122 boolean_t newfs = B_FALSE; 3123 boolean_t stream_wantsnewfs; 3124 uint64_t parent_snapguid = 0; 3125 prop_changelist_t *clp = NULL; 3126 nvlist_t *snapprops_nvlist = NULL; 3127 zprop_errflags_t prop_errflags; 3128 boolean_t recursive; 3129 char *snapname = NULL; 3130 3131 begin_time = time(NULL); 3132 3133 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3134 "cannot receive")); 3135 3136 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == 3137 ENOENT); 3138 3139 if (stream_avl != NULL) { 3140 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, 3141 &snapname); 3142 nvlist_t *props; 3143 int ret; 3144 3145 (void) nvlist_lookup_uint64(fs, "parentfromsnap", 3146 &parent_snapguid); 3147 err = nvlist_lookup_nvlist(fs, "props", &props); 3148 if (err) 3149 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0)); 3150 3151 if (flags->canmountoff) { 3152 VERIFY(0 == nvlist_add_uint64(props, 3153 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0)); 3154 } 3155 ret = zcmd_write_src_nvlist(hdl, &zc, props); 3156 if (err) 3157 nvlist_free(props); 3158 3159 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) { 3160 VERIFY(0 == nvlist_lookup_nvlist(props, 3161 snapname, &snapprops_nvlist)); 3162 } 3163 3164 if (ret != 0) 3165 return (-1); 3166 } 3167 3168 cp = NULL; 3169 3170 /* 3171 * Determine how much of the snapshot name stored in the stream 3172 * we are going to tack on to the name they specified on the 3173 * command line, and how much we are going to chop off. 3174 * 3175 * If they specified a snapshot, chop the entire name stored in 3176 * the stream. 3177 */ 3178 if (flags->istail) { 3179 /* 3180 * A filesystem was specified with -e. We want to tack on only 3181 * the tail of the sent snapshot path. 3182 */ 3183 if (strchr(tosnap, '@')) { 3184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3185 "argument - snapshot not allowed with -e")); 3186 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3187 } 3188 3189 chopprefix = strrchr(sendfs, '/'); 3190 3191 if (chopprefix == NULL) { 3192 /* 3193 * The tail is the poolname, so we need to 3194 * prepend a path separator. 3195 */ 3196 int len = strlen(drrb->drr_toname); 3197 cp = malloc(len + 2); 3198 cp[0] = '/'; 3199 (void) strcpy(&cp[1], drrb->drr_toname); 3200 chopprefix = cp; 3201 } else { 3202 chopprefix = drrb->drr_toname + (chopprefix - sendfs); 3203 } 3204 } else if (flags->isprefix) { 3205 /* 3206 * A filesystem was specified with -d. We want to tack on 3207 * everything but the first element of the sent snapshot path 3208 * (all but the pool name). 3209 */ 3210 if (strchr(tosnap, '@')) { 3211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3212 "argument - snapshot not allowed with -d")); 3213 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3214 } 3215 3216 chopprefix = strchr(drrb->drr_toname, '/'); 3217 if (chopprefix == NULL) 3218 chopprefix = strchr(drrb->drr_toname, '@'); 3219 } else if (strchr(tosnap, '@') == NULL) { 3220 /* 3221 * If a filesystem was specified without -d or -e, we want to 3222 * tack on everything after the fs specified by 'zfs send'. 3223 */ 3224 chopprefix = drrb->drr_toname + strlen(sendfs); 3225 } else { 3226 /* A snapshot was specified as an exact path (no -d or -e). */ 3227 if (recursive) { 3228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3229 "cannot specify snapshot name for multi-snapshot " 3230 "stream")); 3231 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3232 } 3233 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname); 3234 } 3235 3236 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname); 3237 ASSERT(chopprefix > drrb->drr_toname); 3238 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname)); 3239 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' || 3240 chopprefix[0] == '\0'); 3241 3242 /* 3243 * Determine name of destination snapshot, store in zc_value. 3244 */ 3245 (void) strcpy(zc.zc_value, tosnap); 3246 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value)); 3247#ifdef __FreeBSD__ 3248 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF) 3249 zfs_ioctl_version = get_zfs_ioctl_version(); 3250 /* 3251 * For forward compatibility hide tosnap in zc_value 3252 */ 3253 if (zfs_ioctl_version < ZFS_IOCVER_LZC) 3254 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap); 3255#endif 3256 free(cp); 3257 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) { 3258 zcmd_free_nvlists(&zc); 3259 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3260 } 3261 3262 /* 3263 * Determine the name of the origin snapshot, store in zc_string. 3264 */ 3265 if (originsnap) { 3266 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string)); 3267 if (flags->verbose) 3268 (void) printf("using provided clone origin %s\n", 3269 zc.zc_string); 3270 } else if (drrb->drr_flags & DRR_FLAG_CLONE) { 3271 if (guid_to_name(hdl, zc.zc_value, 3272 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) { 3273 zcmd_free_nvlists(&zc); 3274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3275 "local origin for clone %s does not exist"), 3276 zc.zc_value); 3277 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3278 } 3279 if (flags->verbose) 3280 (void) printf("found clone origin %s\n", zc.zc_string); 3281 } 3282 3283 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 3284 DMU_BACKUP_FEATURE_RESUMING; 3285 stream_wantsnewfs = (drrb->drr_fromguid == 0 || 3286 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming; 3287 3288 if (stream_wantsnewfs) { 3289 /* 3290 * if the parent fs does not exist, look for it based on 3291 * the parent snap GUID 3292 */ 3293 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3294 "cannot receive new filesystem stream")); 3295 3296 (void) strcpy(zc.zc_name, zc.zc_value); 3297 cp = strrchr(zc.zc_name, '/'); 3298 if (cp) 3299 *cp = '\0'; 3300 if (cp && 3301 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 3302 char suffix[ZFS_MAX_DATASET_NAME_LEN]; 3303 (void) strcpy(suffix, strrchr(zc.zc_value, '/')); 3304 if (guid_to_name(hdl, zc.zc_name, parent_snapguid, 3305 B_FALSE, zc.zc_value) == 0) { 3306 *strchr(zc.zc_value, '@') = '\0'; 3307 (void) strcat(zc.zc_value, suffix); 3308 } 3309 } 3310 } else { 3311 /* 3312 * If the fs does not exist, look for it based on the 3313 * fromsnap GUID. 3314 */ 3315 if (resuming) { 3316 (void) snprintf(errbuf, sizeof (errbuf), 3317 dgettext(TEXT_DOMAIN, 3318 "cannot receive resume stream")); 3319 } else { 3320 (void) snprintf(errbuf, sizeof (errbuf), 3321 dgettext(TEXT_DOMAIN, 3322 "cannot receive incremental stream")); 3323 } 3324 3325 (void) strcpy(zc.zc_name, zc.zc_value); 3326 *strchr(zc.zc_name, '@') = '\0'; 3327 3328 /* 3329 * If the exact receive path was specified and this is the 3330 * topmost path in the stream, then if the fs does not exist we 3331 * should look no further. 3332 */ 3333 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname + 3334 strlen(sendfs)) != '\0' && *chopprefix != '@')) && 3335 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 3336 char snap[ZFS_MAX_DATASET_NAME_LEN]; 3337 (void) strcpy(snap, strchr(zc.zc_value, '@')); 3338 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid, 3339 B_FALSE, zc.zc_value) == 0) { 3340 *strchr(zc.zc_value, '@') = '\0'; 3341 (void) strcat(zc.zc_value, snap); 3342 } 3343 } 3344 } 3345 3346 (void) strcpy(zc.zc_name, zc.zc_value); 3347 *strchr(zc.zc_name, '@') = '\0'; 3348 3349 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 3350 zfs_handle_t *zhp; 3351 3352 /* 3353 * Destination fs exists. It must be one of these cases: 3354 * - an incremental send stream 3355 * - the stream specifies a new fs (full stream or clone) 3356 * and they want us to blow away the existing fs (and 3357 * have therefore specified -F and removed any snapshots) 3358 * - we are resuming a failed receive. 3359 */ 3360 if (stream_wantsnewfs) { 3361 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL; 3362 if (!flags->force) { 3363 zcmd_free_nvlists(&zc); 3364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3365 "destination '%s' exists\n" 3366 "must specify -F to overwrite it"), 3367 zc.zc_name); 3368 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3369 } 3370 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 3371 &zc) == 0) { 3372 zcmd_free_nvlists(&zc); 3373 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3374 "destination has snapshots (eg. %s)\n" 3375 "must destroy them to overwrite it"), 3376 zc.zc_name); 3377 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3378 } 3379 if (is_volume && strrchr(zc.zc_name, '/') == NULL) { 3380 zcmd_free_nvlists(&zc); 3381 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3382 "destination '%s' is the root dataset\n" 3383 "cannot overwrite with a ZVOL"), 3384 zc.zc_name); 3385 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3386 } 3387 if (is_volume && 3388 ioctl(hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, 3389 &zc) == 0) { 3390 zcmd_free_nvlists(&zc); 3391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3392 "destination has children (eg. %s)\n" 3393 "cannot overwrite with a ZVOL"), 3394 zc.zc_name); 3395 return (zfs_error(hdl, EZFS_WRONG_PARENT, 3396 errbuf)); 3397 } 3398 } 3399 3400 if ((zhp = zfs_open(hdl, zc.zc_name, 3401 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { 3402 zcmd_free_nvlists(&zc); 3403 return (-1); 3404 } 3405 3406 if (stream_wantsnewfs && 3407 zhp->zfs_dmustats.dds_origin[0]) { 3408 zcmd_free_nvlists(&zc); 3409 zfs_close(zhp); 3410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3411 "destination '%s' is a clone\n" 3412 "must destroy it to overwrite it"), 3413 zc.zc_name); 3414 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3415 } 3416 3417 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && 3418 (stream_wantsnewfs || resuming)) { 3419 /* We can't do online recv in this case */ 3420 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0); 3421 if (clp == NULL) { 3422 zfs_close(zhp); 3423 zcmd_free_nvlists(&zc); 3424 return (-1); 3425 } 3426 if (changelist_prefix(clp) != 0) { 3427 changelist_free(clp); 3428 zfs_close(zhp); 3429 zcmd_free_nvlists(&zc); 3430 return (-1); 3431 } 3432 } 3433 3434 /* 3435 * If we are resuming a newfs, set newfs here so that we will 3436 * mount it if the recv succeeds this time. We can tell 3437 * that it was a newfs on the first recv because the fs 3438 * itself will be inconsistent (if the fs existed when we 3439 * did the first recv, we would have received it into 3440 * .../%recv). 3441 */ 3442 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT)) 3443 newfs = B_TRUE; 3444 3445 zfs_close(zhp); 3446 } else { 3447 zfs_handle_t *zhp; 3448 3449 /* 3450 * Destination filesystem does not exist. Therefore we better 3451 * be creating a new filesystem (either from a full backup, or 3452 * a clone). It would therefore be invalid if the user 3453 * specified only the pool name (i.e. if the destination name 3454 * contained no slash character). 3455 */ 3456 if (!stream_wantsnewfs || 3457 (cp = strrchr(zc.zc_name, '/')) == NULL) { 3458 zcmd_free_nvlists(&zc); 3459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3460 "destination '%s' does not exist"), zc.zc_name); 3461 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3462 } 3463 3464 /* 3465 * Trim off the final dataset component so we perform the 3466 * recvbackup ioctl to the filesystems's parent. 3467 */ 3468 *cp = '\0'; 3469 3470 if (flags->isprefix && !flags->istail && !flags->dryrun && 3471 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) { 3472 zcmd_free_nvlists(&zc); 3473 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 3474 } 3475 3476 /* validate parent */ 3477 zhp = zfs_open(hdl, zc.zc_name, ZFS_TYPE_DATASET); 3478 if (zhp == NULL) { 3479 zcmd_free_nvlists(&zc); 3480 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 3481 } 3482 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 3483 zcmd_free_nvlists(&zc); 3484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3485 "parent '%s' is not a filesystem"), zc.zc_name); 3486 zfs_close(zhp); 3487 return (zfs_error(hdl, EZFS_WRONG_PARENT, errbuf)); 3488 } 3489 zfs_close(zhp); 3490 3491 newfs = B_TRUE; 3492 } 3493 3494 zc.zc_begin_record = *drr_noswap; 3495 zc.zc_cookie = infd; 3496 zc.zc_guid = flags->force; 3497 zc.zc_resumable = flags->resumable; 3498 if (flags->verbose) { 3499 (void) printf("%s %s stream of %s into %s\n", 3500 flags->dryrun ? "would receive" : "receiving", 3501 drrb->drr_fromguid ? "incremental" : "full", 3502 drrb->drr_toname, zc.zc_value); 3503 (void) fflush(stdout); 3504 } 3505 3506 if (flags->dryrun) { 3507 zcmd_free_nvlists(&zc); 3508 return (recv_skip(hdl, infd, flags->byteswap)); 3509 } 3510 3511 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf; 3512 zc.zc_nvlist_dst_size = sizeof (prop_errbuf); 3513 zc.zc_cleanup_fd = cleanup_fd; 3514 zc.zc_action_handle = *action_handlep; 3515 3516 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc); 3517 ioctl_errno = errno; 3518 prop_errflags = (zprop_errflags_t)zc.zc_obj; 3519 3520 if (err == 0) { 3521 nvlist_t *prop_errors; 3522 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst, 3523 zc.zc_nvlist_dst_size, &prop_errors, 0)); 3524 3525 nvpair_t *prop_err = NULL; 3526 3527 while ((prop_err = nvlist_next_nvpair(prop_errors, 3528 prop_err)) != NULL) { 3529 char tbuf[1024]; 3530 zfs_prop_t prop; 3531 int intval; 3532 3533 prop = zfs_name_to_prop(nvpair_name(prop_err)); 3534 (void) nvpair_value_int32(prop_err, &intval); 3535 if (strcmp(nvpair_name(prop_err), 3536 ZPROP_N_MORE_ERRORS) == 0) { 3537 trunc_prop_errs(intval); 3538 break; 3539 } else if (snapname == NULL || finalsnap == NULL || 3540 strcmp(finalsnap, snapname) == 0 || 3541 strcmp(nvpair_name(prop_err), 3542 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) { 3543 /* 3544 * Skip the special case of, for example, 3545 * "refquota", errors on intermediate 3546 * snapshots leading up to a final one. 3547 * That's why we have all of the checks above. 3548 * 3549 * See zfs_ioctl.c's extract_delay_props() for 3550 * a list of props which can fail on 3551 * intermediate snapshots, but shouldn't 3552 * affect the overall receive. 3553 */ 3554 (void) snprintf(tbuf, sizeof (tbuf), 3555 dgettext(TEXT_DOMAIN, 3556 "cannot receive %s property on %s"), 3557 nvpair_name(prop_err), zc.zc_name); 3558 zfs_setprop_error(hdl, prop, intval, tbuf); 3559 } 3560 } 3561 nvlist_free(prop_errors); 3562 } 3563 3564 zc.zc_nvlist_dst = 0; 3565 zc.zc_nvlist_dst_size = 0; 3566 zcmd_free_nvlists(&zc); 3567 3568 if (err == 0 && snapprops_nvlist) { 3569 zfs_cmd_t zc2 = { 0 }; 3570 3571 (void) strcpy(zc2.zc_name, zc.zc_value); 3572 zc2.zc_cookie = B_TRUE; /* received */ 3573 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) { 3574 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2); 3575 zcmd_free_nvlists(&zc2); 3576 } 3577 } 3578 3579 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) { 3580 /* 3581 * It may be that this snapshot already exists, 3582 * in which case we want to consume & ignore it 3583 * rather than failing. 3584 */ 3585 avl_tree_t *local_avl; 3586 nvlist_t *local_nv, *fs; 3587 cp = strchr(zc.zc_value, '@'); 3588 3589 /* 3590 * XXX Do this faster by just iterating over snaps in 3591 * this fs. Also if zc_value does not exist, we will 3592 * get a strange "does not exist" error message. 3593 */ 3594 *cp = '\0'; 3595 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE, 3596 B_FALSE, &local_nv, &local_avl) == 0) { 3597 *cp = '@'; 3598 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); 3599 fsavl_destroy(local_avl); 3600 nvlist_free(local_nv); 3601 3602 if (fs != NULL) { 3603 if (flags->verbose) { 3604 (void) printf("snap %s already exists; " 3605 "ignoring\n", zc.zc_value); 3606 } 3607 err = ioctl_err = recv_skip(hdl, infd, 3608 flags->byteswap); 3609 } 3610 } 3611 *cp = '@'; 3612 } 3613 3614 if (ioctl_err != 0) { 3615 switch (ioctl_errno) { 3616 case ENODEV: 3617 cp = strchr(zc.zc_value, '@'); 3618 *cp = '\0'; 3619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3620 "most recent snapshot of %s does not\n" 3621 "match incremental source"), zc.zc_value); 3622 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3623 *cp = '@'; 3624 break; 3625 case ETXTBSY: 3626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3627 "destination %s has been modified\n" 3628 "since most recent snapshot"), zc.zc_name); 3629 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3630 break; 3631 case EEXIST: 3632 cp = strchr(zc.zc_value, '@'); 3633 if (newfs) { 3634 /* it's the containing fs that exists */ 3635 *cp = '\0'; 3636 } 3637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3638 "destination already exists")); 3639 (void) zfs_error_fmt(hdl, EZFS_EXISTS, 3640 dgettext(TEXT_DOMAIN, "cannot restore to %s"), 3641 zc.zc_value); 3642 *cp = '@'; 3643 break; 3644 case EINVAL: 3645 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3646 break; 3647 case ECKSUM: 3648 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable); 3649 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3650 break; 3651 case ENOTSUP: 3652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3653 "pool must be upgraded to receive this stream.")); 3654 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 3655 break; 3656 case EDQUOT: 3657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3658 "destination %s space quota exceeded"), zc.zc_name); 3659 (void) zfs_error(hdl, EZFS_NOSPC, errbuf); 3660 break; 3661 default: 3662 (void) zfs_standard_error(hdl, ioctl_errno, errbuf); 3663 } 3664 } 3665 3666 /* 3667 * Mount the target filesystem (if created). Also mount any 3668 * children of the target filesystem if we did a replication 3669 * receive (indicated by stream_avl being non-NULL). 3670 */ 3671 cp = strchr(zc.zc_value, '@'); 3672 if (cp && (ioctl_err == 0 || !newfs)) { 3673 zfs_handle_t *h; 3674 3675 *cp = '\0'; 3676 h = zfs_open(hdl, zc.zc_value, 3677 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3678 if (h != NULL) { 3679 if (h->zfs_type == ZFS_TYPE_VOLUME) { 3680 *cp = '@'; 3681 } else if (newfs || stream_avl) { 3682 /* 3683 * Track the first/top of hierarchy fs, 3684 * for mounting and sharing later. 3685 */ 3686 if (top_zfs && *top_zfs == NULL) 3687 *top_zfs = zfs_strdup(hdl, zc.zc_value); 3688 } 3689 zfs_close(h); 3690 } 3691 *cp = '@'; 3692 } 3693 3694 if (clp) { 3695 if (!flags->nomount) 3696 err |= changelist_postfix(clp); 3697 changelist_free(clp); 3698 } 3699 3700 if (prop_errflags & ZPROP_ERR_NOCLEAR) { 3701 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " 3702 "failed to clear unreceived properties on %s"), 3703 zc.zc_name); 3704 (void) fprintf(stderr, "\n"); 3705 } 3706 if (prop_errflags & ZPROP_ERR_NORESTORE) { 3707 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " 3708 "failed to restore original properties on %s"), 3709 zc.zc_name); 3710 (void) fprintf(stderr, "\n"); 3711 } 3712 3713 if (err || ioctl_err) 3714 return (-1); 3715 3716 *action_handlep = zc.zc_action_handle; 3717 3718 if (flags->verbose) { 3719 char buf1[64]; 3720 char buf2[64]; 3721 uint64_t bytes = zc.zc_cookie; 3722 time_t delta = time(NULL) - begin_time; 3723 if (delta == 0) 3724 delta = 1; 3725 zfs_nicenum(bytes, buf1, sizeof (buf1)); 3726 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3727 3728 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3729 buf1, delta, buf2); 3730 } 3731 3732 return (0); 3733} 3734 3735static int 3736zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, 3737 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs, 3738 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd, 3739 uint64_t *action_handlep, const char *finalsnap) 3740{ 3741 int err; 3742 dmu_replay_record_t drr, drr_noswap; 3743 struct drr_begin *drrb = &drr.drr_u.drr_begin; 3744 char errbuf[1024]; 3745 zio_cksum_t zcksum = { 0 }; 3746 uint64_t featureflags; 3747 int hdrtype; 3748 3749 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3750 "cannot receive")); 3751 3752 if (flags->isprefix && 3753 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { 3754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " 3755 "(%s) does not exist"), tosnap); 3756 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3757 } 3758 if (originsnap && 3759 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) { 3760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs " 3761 "(%s) does not exist"), originsnap); 3762 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3763 } 3764 3765 /* read in the BEGIN record */ 3766 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, 3767 &zcksum))) 3768 return (err); 3769 3770 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { 3771 /* It's the double end record at the end of a package */ 3772 return (ENODATA); 3773 } 3774 3775 /* the kernel needs the non-byteswapped begin record */ 3776 drr_noswap = drr; 3777 3778 flags->byteswap = B_FALSE; 3779 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { 3780 /* 3781 * We computed the checksum in the wrong byteorder in 3782 * recv_read() above; do it again correctly. 3783 */ 3784 bzero(&zcksum, sizeof (zio_cksum_t)); 3785 (void) fletcher_4_incremental_byteswap(&drr, 3786 sizeof (drr), &zcksum); 3787 flags->byteswap = B_TRUE; 3788 3789 drr.drr_type = BSWAP_32(drr.drr_type); 3790 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); 3791 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 3792 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); 3793 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 3794 drrb->drr_type = BSWAP_32(drrb->drr_type); 3795 drrb->drr_flags = BSWAP_32(drrb->drr_flags); 3796 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 3797 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 3798 } 3799 3800 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { 3801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3802 "stream (bad magic number)")); 3803 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3804 } 3805 3806 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 3807 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo); 3808 3809 if (!DMU_STREAM_SUPPORTED(featureflags) || 3810 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) { 3811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3812 "stream has unsupported feature, feature flags = %lx"), 3813 featureflags); 3814 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3815 } 3816 3817 if (strchr(drrb->drr_toname, '@') == NULL) { 3818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3819 "stream (bad snapshot name)")); 3820 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3821 } 3822 3823 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) { 3824 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN]; 3825 if (sendfs == NULL) { 3826 /* 3827 * We were not called from zfs_receive_package(). Get 3828 * the fs specified by 'zfs send'. 3829 */ 3830 char *cp; 3831 (void) strlcpy(nonpackage_sendfs, 3832 drr.drr_u.drr_begin.drr_toname, 3833 sizeof (nonpackage_sendfs)); 3834 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL) 3835 *cp = '\0'; 3836 sendfs = nonpackage_sendfs; 3837 VERIFY(finalsnap == NULL); 3838 } 3839 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags, 3840 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs, 3841 cleanup_fd, action_handlep, finalsnap)); 3842 } else { 3843 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == 3844 DMU_COMPOUNDSTREAM); 3845 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr, 3846 &zcksum, top_zfs, cleanup_fd, action_handlep)); 3847 } 3848} 3849 3850/* 3851 * Restores a backup of tosnap from the file descriptor specified by infd. 3852 * Return 0 on total success, -2 if some things couldn't be 3853 * destroyed/renamed/promoted, -1 if some things couldn't be received. 3854 * (-1 will override -2, if -1 and the resumable flag was specified the 3855 * transfer can be resumed if the sending side supports it). 3856 */ 3857int 3858zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props, 3859 recvflags_t *flags, int infd, avl_tree_t *stream_avl) 3860{ 3861 char *top_zfs = NULL; 3862 int err; 3863 int cleanup_fd; 3864 uint64_t action_handle = 0; 3865 char *originsnap = NULL; 3866 if (props) { 3867 err = nvlist_lookup_string(props, "origin", &originsnap); 3868 if (err && err != ENOENT) 3869 return (err); 3870 } 3871 3872 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL); 3873 VERIFY(cleanup_fd >= 0); 3874 3875 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL, 3876 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL); 3877 3878 VERIFY(0 == close(cleanup_fd)); 3879 3880 if (err == 0 && !flags->nomount && top_zfs) { 3881 zfs_handle_t *zhp; 3882 prop_changelist_t *clp; 3883 3884 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM); 3885 if (zhp != NULL) { 3886 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 3887 CL_GATHER_MOUNT_ALWAYS, 0); 3888 zfs_close(zhp); 3889 if (clp != NULL) { 3890 /* mount and share received datasets */ 3891 err = changelist_postfix(clp); 3892 changelist_free(clp); 3893 } 3894 } 3895 if (zhp == NULL || clp == NULL || err) 3896 err = -1; 3897 } 3898 if (top_zfs) 3899 free(top_zfs); 3900 3901 return (err); 3902} 3903