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