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