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