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