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