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.
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.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
40 #include <sys/mount.h>
41 #include <sys/mntent.h>
42 #include <sys/mnttab.h>
44 #include <sys/debug.h>
52 #include "zfs_namecheck.h"
54 #include "zfs_fletcher.h"
55 #include "libzfs_impl.h"
56 #include <sys/zio_checksum.h>
58 #include <sys/socket.h>
60 /* in libzfs_dataset.c */
61 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
63 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t *,
64 int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
66 static const zio_cksum_t zero_cksum = { { 0 } };
68 typedef struct dedup_arg {
71 libzfs_handle_t *dedup_hdl;
74 typedef struct progress_arg {
77 boolean_t pa_parsable;
80 typedef struct dataref {
86 typedef struct dedup_entry {
87 struct dedup_entry *dde_next;
88 zio_cksum_t dde_chksum;
93 #define MAX_DDT_PHYSMEM_PERCENT 20
94 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
96 typedef struct dedup_table {
97 dedup_entry_t **dedup_hash_array;
98 umem_cache_t *ddecache;
99 uint64_t max_ddt_size; /* max dedup table size in bytes */
100 uint64_t cur_ddt_size; /* current dedup table size in bytes */
107 high_order_bit(uint64_t n)
111 for (count = 0; n != 0; count++)
117 ssread(void *buf, size_t len, FILE *stream)
121 if ((outlen = fread(buf, len, 1, stream)) == 0)
128 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
129 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
133 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
134 if (ddt->ddt_full == B_FALSE) {
135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136 "Dedup table full. Deduplication will continue "
137 "with existing table entries"));
138 ddt->ddt_full = B_TRUE;
143 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
145 assert(*ddepp == NULL);
146 dde->dde_next = NULL;
147 dde->dde_chksum = *cs;
148 dde->dde_prop = prop;
151 ddt->cur_ddt_size += sizeof (dedup_entry_t);
157 * Using the specified dedup table, do a lookup for an entry with
158 * the checksum cs. If found, return the block's reference info
159 * in *dr. Otherwise, insert a new entry in the dedup table, using
160 * the reference information specified by *dr.
162 * return value: true - entry was found
163 * false - entry was not found
166 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
167 uint64_t prop, dataref_t *dr)
170 dedup_entry_t **ddepp;
172 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
174 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
175 ddepp = &((*ddepp)->dde_next)) {
176 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
177 (*ddepp)->dde_prop == prop) {
178 *dr = (*ddepp)->dde_ref;
182 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
187 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
189 fletcher_4_incremental_native(buf, len, zc);
190 return (write(outfd, buf, len));
194 * This function is started in a separate thread when the dedup option
195 * has been requested. The main send thread determines the list of
196 * snapshots to be included in the send stream and makes the ioctl calls
197 * for each one. But instead of having the ioctl send the output to the
198 * the output fd specified by the caller of zfs_send()), the
199 * ioctl is told to direct the output to a pipe, which is read by the
200 * alternate thread running THIS function. This function does the
202 * 1. building a dedup table (the DDT)
203 * 2. doing checksums on each data block and inserting a record in the DDT
204 * 3. looking for matching checksums, and
205 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
206 * a duplicate block is found.
207 * The output of this function then goes to the output fd requested
208 * by the caller of zfs_send().
213 dedup_arg_t *dda = arg;
214 char *buf = malloc(1<<20);
215 dmu_replay_record_t thedrr;
216 dmu_replay_record_t *drr = &thedrr;
217 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
218 struct drr_end *drre = &thedrr.drr_u.drr_end;
219 struct drr_object *drro = &thedrr.drr_u.drr_object;
220 struct drr_write *drrw = &thedrr.drr_u.drr_write;
221 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
224 dmu_replay_record_t wbr_drr = {0};
225 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
227 zio_cksum_t stream_cksum;
228 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
232 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
233 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
235 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
238 * numbuckets must be a power of 2. Increase number to
239 * a power of 2 if necessary.
241 if (!ISP2(numbuckets))
242 numbuckets = 1 << high_order_bit(numbuckets);
244 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
245 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
246 NULL, NULL, NULL, NULL, NULL, 0);
247 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
248 ddt.numhashbits = high_order_bit(numbuckets) - 1;
249 ddt.ddt_full = B_FALSE;
251 /* Initialize the write-by-reference block. */
252 wbr_drr.drr_type = DRR_WRITE_BYREF;
253 wbr_drr.drr_payloadlen = 0;
255 outfd = dda->outputfd;
256 ofp = fdopen(dda->inputfd, "r");
257 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
259 switch (drr->drr_type) {
263 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
265 /* set the DEDUP feature flag for this stream */
266 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
267 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
268 DMU_BACKUP_FEATURE_DEDUPPROPS);
269 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
271 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
272 &stream_cksum, outfd) == -1)
274 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
275 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
276 int sz = drr->drr_payloadlen;
282 (void) ssread(buf, sz, ofp);
285 if (cksum_and_write(buf, sz, &stream_cksum,
294 /* use the recalculated checksum */
295 ZIO_SET_CHECKSUM(&drre->drr_checksum,
296 stream_cksum.zc_word[0], stream_cksum.zc_word[1],
297 stream_cksum.zc_word[2], stream_cksum.zc_word[3]);
298 if ((write(outfd, drr,
299 sizeof (dmu_replay_record_t))) == -1)
306 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
307 &stream_cksum, outfd) == -1)
309 if (drro->drr_bonuslen > 0) {
311 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
313 if (cksum_and_write(buf,
314 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
315 &stream_cksum, outfd) == -1)
323 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
324 &stream_cksum, outfd) == -1)
326 (void) ssread(buf, drrs->drr_length, ofp);
327 if (cksum_and_write(buf, drrs->drr_length,
328 &stream_cksum, outfd) == -1)
333 case DRR_FREEOBJECTS:
335 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
336 &stream_cksum, outfd) == -1)
345 (void) ssread(buf, drrw->drr_length, ofp);
348 * Use the existing checksum if it's dedup-capable,
349 * else calculate a SHA256 checksum for it.
352 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
354 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
355 zio_cksum_t tmpsha256;
357 zio_checksum_SHA256(buf,
358 drrw->drr_length, &tmpsha256);
360 drrw->drr_key.ddk_cksum.zc_word[0] =
361 BE_64(tmpsha256.zc_word[0]);
362 drrw->drr_key.ddk_cksum.zc_word[1] =
363 BE_64(tmpsha256.zc_word[1]);
364 drrw->drr_key.ddk_cksum.zc_word[2] =
365 BE_64(tmpsha256.zc_word[2]);
366 drrw->drr_key.ddk_cksum.zc_word[3] =
367 BE_64(tmpsha256.zc_word[3]);
368 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
369 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
372 dataref.ref_guid = drrw->drr_toguid;
373 dataref.ref_object = drrw->drr_object;
374 dataref.ref_offset = drrw->drr_offset;
376 if (ddt_update(dda->dedup_hdl, &ddt,
377 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
379 /* block already present in stream */
380 wbr_drrr->drr_object = drrw->drr_object;
381 wbr_drrr->drr_offset = drrw->drr_offset;
382 wbr_drrr->drr_length = drrw->drr_length;
383 wbr_drrr->drr_toguid = drrw->drr_toguid;
384 wbr_drrr->drr_refguid = dataref.ref_guid;
385 wbr_drrr->drr_refobject =
387 wbr_drrr->drr_refoffset =
390 wbr_drrr->drr_checksumtype =
391 drrw->drr_checksumtype;
392 wbr_drrr->drr_checksumflags =
393 drrw->drr_checksumtype;
394 wbr_drrr->drr_key.ddk_cksum =
395 drrw->drr_key.ddk_cksum;
396 wbr_drrr->drr_key.ddk_prop =
397 drrw->drr_key.ddk_prop;
399 if (cksum_and_write(&wbr_drr,
400 sizeof (dmu_replay_record_t), &stream_cksum,
404 /* block not previously seen */
405 if (cksum_and_write(drr,
406 sizeof (dmu_replay_record_t), &stream_cksum,
409 if (cksum_and_write(buf,
411 &stream_cksum, outfd) == -1)
419 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
420 &stream_cksum, outfd) == -1)
426 (void) printf("INVALID record type 0x%x\n",
428 /* should never happen, so assert */
433 umem_cache_destroy(ddt.ddecache);
434 free(ddt.dedup_hash_array);
442 * Routines for dealing with the AVL tree of fs-nvlists
444 typedef struct fsavl_node {
452 fsavl_compare(const void *arg1, const void *arg2)
454 const fsavl_node_t *fn1 = arg1;
455 const fsavl_node_t *fn2 = arg2;
457 if (fn1->fn_guid > fn2->fn_guid)
459 else if (fn1->fn_guid < fn2->fn_guid)
466 * Given the GUID of a snapshot, find its containing filesystem and
470 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
472 fsavl_node_t fn_find;
475 fn_find.fn_guid = snapguid;
477 fn = avl_find(avl, &fn_find, NULL);
480 *snapname = fn->fn_snapname;
481 return (fn->fn_nvfs);
487 fsavl_destroy(avl_tree_t *avl)
496 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
503 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
506 fsavl_create(nvlist_t *fss)
509 nvpair_t *fselem = NULL;
511 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
514 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
515 offsetof(fsavl_node_t, fn_node));
517 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
518 nvlist_t *nvfs, *snaps;
519 nvpair_t *snapelem = NULL;
521 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
522 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
525 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
529 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
530 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
531 fsavl_destroy(fsavl);
535 fn->fn_snapname = nvpair_name(snapelem);
539 * Note: if there are multiple snaps with the
540 * same GUID, we ignore all but one.
542 if (avl_find(fsavl, fn, NULL) == NULL)
553 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
555 typedef struct send_data {
556 uint64_t parent_fromsnap_guid;
557 nvlist_t *parent_snaps;
560 const char *fromsnap;
565 * The header nvlist is of the following format:
568 * "fromsnap" -> string (if incremental)
572 * "name" -> string (full name; for debugging)
573 * "parentfromsnap" -> number (guid of fromsnap in parent)
575 * "props" -> { name -> value (only if set here) }
576 * "snaps" -> { name (lastname) -> number (guid) }
577 * "snapprops" -> { name (lastname) -> { name -> value } }
579 * "origin" -> number (guid) (if clone)
580 * "sent" -> boolean (not on-disk)
588 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
591 send_iterate_snap(zfs_handle_t *zhp, void *arg)
593 send_data_t *sd = arg;
594 uint64_t guid = zhp->zfs_dmustats.dds_guid;
598 snapname = strrchr(zhp->zfs_name, '@')+1;
600 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
602 * NB: if there is no fromsnap here (it's a newly created fs in
603 * an incremental replication), we will substitute the tosnap.
605 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
606 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
607 strcmp(snapname, sd->tosnap) == 0)) {
608 sd->parent_fromsnap_guid = guid;
611 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
612 send_iterate_prop(zhp, nv);
613 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
621 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
623 nvpair_t *elem = NULL;
625 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
626 char *propname = nvpair_name(elem);
627 zfs_prop_t prop = zfs_name_to_prop(propname);
630 if (!zfs_prop_user(propname)) {
632 * Realistically, this should never happen. However,
633 * we want the ability to add DSL properties without
634 * needing to make incompatible version changes. We
635 * need to ignore unknown properties to allow older
636 * software to still send datasets containing these
637 * properties, with the unknown properties elided.
639 if (prop == ZPROP_INVAL)
642 if (zfs_prop_readonly(prop))
646 verify(nvpair_value_nvlist(elem, &propnv) == 0);
647 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
648 prop == ZFS_PROP_REFQUOTA ||
649 prop == ZFS_PROP_REFRESERVATION) {
652 verify(nvlist_lookup_uint64(propnv,
653 ZPROP_VALUE, &value) == 0);
654 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
657 * May have no source before SPA_VERSION_RECVD_PROPS,
658 * but is still modifiable.
660 if (nvlist_lookup_string(propnv,
661 ZPROP_SOURCE, &source) == 0) {
662 if ((strcmp(source, zhp->zfs_name) != 0) &&
664 ZPROP_SOURCE_VAL_RECVD) != 0))
669 if (nvlist_lookup_string(propnv,
670 ZPROP_SOURCE, &source) != 0)
672 if ((strcmp(source, zhp->zfs_name) != 0) &&
673 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
677 if (zfs_prop_user(propname) ||
678 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
680 verify(nvlist_lookup_string(propnv,
681 ZPROP_VALUE, &value) == 0);
682 VERIFY(0 == nvlist_add_string(nv, propname, value));
685 verify(nvlist_lookup_uint64(propnv,
686 ZPROP_VALUE, &value) == 0);
687 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
693 * recursively generate nvlists describing datasets. See comment
694 * for the data structure send_data_t above for description of contents
698 send_iterate_fs(zfs_handle_t *zhp, void *arg)
700 send_data_t *sd = arg;
703 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
704 uint64_t guid = zhp->zfs_dmustats.dds_guid;
707 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
708 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
709 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
710 sd->parent_fromsnap_guid));
712 if (zhp->zfs_dmustats.dds_origin[0]) {
713 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
714 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
717 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
718 origin->zfs_dmustats.dds_guid));
721 /* iterate over props */
722 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
723 send_iterate_prop(zhp, nv);
724 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
727 /* iterate over snaps, and set sd->parent_fromsnap_guid */
728 sd->parent_fromsnap_guid = 0;
729 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
730 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
731 (void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
732 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
733 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
734 nvlist_free(sd->parent_snaps);
735 nvlist_free(sd->snapprops);
737 /* add this fs to nvlist */
738 (void) snprintf(guidstring, sizeof (guidstring),
739 "0x%llx", (longlong_t)guid);
740 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
743 /* iterate over children */
745 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
747 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
754 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
755 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
758 send_data_t sd = { 0 };
761 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
763 return (EZFS_BADTYPE);
765 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
766 sd.fromsnap = fromsnap;
768 sd.recursive = recursive;
770 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
778 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
789 * Routines specific to "zfs send"
791 typedef struct send_dump_data {
792 /* these are all just the short snapname (the part after the @) */
793 const char *fromsnap;
795 char prevsnap[ZFS_MAXNAMELEN];
796 uint64_t prevsnap_obj;
797 boolean_t seenfrom, seento, replicate, doall, fromorigin;
798 boolean_t verbose, dryrun, parsable, progress;
803 snapfilter_cb_t *filter_cb;
806 char holdtag[ZFS_MAXNAMELEN];
812 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
813 boolean_t fromorigin, uint64_t *sizep)
815 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
816 libzfs_handle_t *hdl = zhp->zfs_hdl;
818 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
819 assert(fromsnap_obj == 0 || !fromorigin);
821 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
822 zc.zc_obj = fromorigin;
823 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
824 zc.zc_fromobj = fromsnap_obj;
825 zc.zc_guid = 1; /* estimate flag */
827 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
829 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
830 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
835 "not an earlier snapshot from the same fs"));
836 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
839 if (zfs_dataset_exists(hdl, zc.zc_name,
840 ZFS_TYPE_SNAPSHOT)) {
841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
842 "incremental source (@%s) does not exist"),
845 return (zfs_error(hdl, EZFS_NOENT, errbuf));
858 zfs_error_aux(hdl, strerror(errno));
859 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
862 return (zfs_standard_error(hdl, errno, errbuf));
866 *sizep = zc.zc_objset_type;
872 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
873 * NULL) to the file descriptor specified by outfd.
876 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
877 boolean_t fromorigin, int outfd, nvlist_t *debugnv)
879 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
880 libzfs_handle_t *hdl = zhp->zfs_hdl;
883 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
884 assert(fromsnap_obj == 0 || !fromorigin);
886 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
887 zc.zc_cookie = outfd;
888 zc.zc_obj = fromorigin;
889 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
890 zc.zc_fromobj = fromsnap_obj;
892 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
893 if (fromsnap && fromsnap[0] != '\0') {
894 VERIFY(0 == nvlist_add_string(thisdbg,
895 "fromsnap", fromsnap));
898 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
900 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
901 "warning: cannot send '%s'"), zhp->zfs_name);
903 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
905 VERIFY(0 == nvlist_add_nvlist(debugnv,
906 zhp->zfs_name, thisdbg));
908 nvlist_free(thisdbg);
912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
913 "not an earlier snapshot from the same fs"));
914 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
917 if (zfs_dataset_exists(hdl, zc.zc_name,
918 ZFS_TYPE_SNAPSHOT)) {
919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
920 "incremental source (@%s) does not exist"),
923 return (zfs_error(hdl, EZFS_NOENT, errbuf));
936 zfs_error_aux(hdl, strerror(errno));
937 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
940 return (zfs_standard_error(hdl, errno, errbuf));
945 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
946 nvlist_free(thisdbg);
952 hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
958 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
964 * zfs_send() only opens a cleanup_fd for sends that need it,
965 * e.g. replication and doall.
967 if (sdd->cleanup_fd == -1)
970 thissnap = strchr(zhp->zfs_name, '@') + 1;
971 *(thissnap - 1) = '\0';
972 pzhp = zfs_open(zhp->zfs_hdl, zhp->zfs_name, ZFS_TYPE_DATASET);
973 *(thissnap - 1) = '@';
976 * It's OK if the parent no longer exists. The send code will
980 error = zfs_hold(pzhp, thissnap, sdd->holdtag,
981 B_FALSE, B_TRUE, B_TRUE, sdd->cleanup_fd,
982 zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID),
983 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG));
991 send_progress_thread(void *arg)
993 progress_arg_t *pa = arg;
995 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
996 zfs_handle_t *zhp = pa->pa_zhp;
997 libzfs_handle_t *hdl = zhp->zfs_hdl;
998 unsigned long long bytes;
1004 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1005 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1007 if (!pa->pa_parsable)
1008 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1011 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1016 zc.zc_cookie = pa->pa_fd;
1017 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1018 return ((void *)-1);
1022 bytes = zc.zc_cookie;
1024 if (pa->pa_parsable) {
1025 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1026 tm->tm_hour, tm->tm_min, tm->tm_sec,
1027 bytes, zhp->zfs_name);
1029 zfs_nicenum(bytes, buf, sizeof (buf));
1030 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1031 tm->tm_hour, tm->tm_min, tm->tm_sec,
1032 buf, zhp->zfs_name);
1038 dump_snapshot(zfs_handle_t *zhp, void *arg)
1040 send_dump_data_t *sdd = arg;
1041 progress_arg_t pa = { 0 };
1046 boolean_t isfromsnap, istosnap, fromorigin;
1047 boolean_t exclude = B_FALSE;
1049 thissnap = strchr(zhp->zfs_name, '@') + 1;
1050 isfromsnap = (sdd->fromsnap != NULL &&
1051 strcmp(sdd->fromsnap, thissnap) == 0);
1053 if (!sdd->seenfrom && isfromsnap) {
1054 err = hold_for_send(zhp, sdd);
1056 sdd->seenfrom = B_TRUE;
1057 (void) strcpy(sdd->prevsnap, thissnap);
1058 sdd->prevsnap_obj = zfs_prop_get_int(zhp,
1060 } else if (err == ENOENT) {
1067 if (sdd->seento || !sdd->seenfrom) {
1072 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1074 sdd->seento = B_TRUE;
1076 if (!sdd->doall && !isfromsnap && !istosnap) {
1077 if (sdd->replicate) {
1079 nvlist_t *snapprops;
1081 * Filter out all intermediate snapshots except origin
1082 * snapshots needed to replicate clones.
1084 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1085 zhp->zfs_dmustats.dds_guid, &snapname);
1087 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1088 "snapprops", &snapprops));
1089 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1090 thissnap, &snapprops));
1091 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1098 * If a filter function exists, call it to determine whether
1099 * this snapshot will be sent.
1101 if (exclude || (sdd->filter_cb != NULL &&
1102 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1104 * This snapshot is filtered out. Don't send it, and don't
1105 * set prevsnap_obj, so it will be as if this snapshot didn't
1106 * exist, and the next accepted snapshot will be sent as
1107 * an incremental from the last accepted one, or as the
1108 * first (and full) snapshot in the case of a replication,
1109 * non-incremental send.
1115 err = hold_for_send(zhp, sdd);
1123 fromorigin = sdd->prevsnap[0] == '\0' &&
1124 (sdd->fromorigin || sdd->replicate);
1128 err = estimate_ioctl(zhp, sdd->prevsnap_obj,
1131 if (sdd->parsable) {
1132 if (sdd->prevsnap[0] != '\0') {
1133 (void) fprintf(stderr, "incremental\t%s\t%s",
1134 sdd->prevsnap, zhp->zfs_name);
1136 (void) fprintf(stderr, "full\t%s",
1140 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1141 "send from @%s to %s"),
1142 sdd->prevsnap, zhp->zfs_name);
1145 if (sdd->parsable) {
1146 (void) fprintf(stderr, "\t%llu\n",
1150 zfs_nicenum(size, buf, sizeof (buf));
1151 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1152 " estimated size is %s\n"), buf);
1156 (void) fprintf(stderr, "\n");
1162 * If progress reporting is requested, spawn a new thread to
1163 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1165 if (sdd->progress) {
1167 pa.pa_fd = sdd->outfd;
1168 pa.pa_parsable = sdd->parsable;
1170 if ((err = pthread_create(&tid, NULL,
1171 send_progress_thread, &pa))) {
1177 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1178 fromorigin, sdd->outfd, sdd->debugnv);
1180 if (sdd->progress) {
1181 (void) pthread_cancel(tid);
1182 (void) pthread_join(tid, NULL);
1186 (void) strcpy(sdd->prevsnap, thissnap);
1187 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1193 dump_filesystem(zfs_handle_t *zhp, void *arg)
1196 send_dump_data_t *sdd = arg;
1197 boolean_t missingfrom = B_FALSE;
1198 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1200 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1201 zhp->zfs_name, sdd->tosnap);
1202 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1203 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1204 "WARNING: could not send %s@%s: does not exist\n"),
1205 zhp->zfs_name, sdd->tosnap);
1210 if (sdd->replicate && sdd->fromsnap) {
1212 * If this fs does not have fromsnap, and we're doing
1213 * recursive, we need to send a full stream from the
1214 * beginning (or an incremental from the origin if this
1215 * is a clone). If we're doing non-recursive, then let
1216 * them get the error.
1218 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1219 zhp->zfs_name, sdd->fromsnap);
1220 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1221 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1222 missingfrom = B_TRUE;
1226 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1227 sdd->prevsnap_obj = 0;
1228 if (sdd->fromsnap == NULL || missingfrom)
1229 sdd->seenfrom = B_TRUE;
1231 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1232 if (!sdd->seenfrom) {
1233 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1234 "WARNING: could not send %s@%s:\n"
1235 "incremental source (%s@%s) does not exist\n"),
1236 zhp->zfs_name, sdd->tosnap,
1237 zhp->zfs_name, sdd->fromsnap);
1239 } else if (!sdd->seento) {
1240 if (sdd->fromsnap) {
1241 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1242 "WARNING: could not send %s@%s:\n"
1243 "incremental source (%s@%s) "
1244 "is not earlier than it\n"),
1245 zhp->zfs_name, sdd->tosnap,
1246 zhp->zfs_name, sdd->fromsnap);
1248 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1250 "could not send %s@%s: does not exist\n"),
1251 zhp->zfs_name, sdd->tosnap);
1260 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1262 send_dump_data_t *sdd = arg;
1264 boolean_t needagain, progress;
1266 if (!sdd->replicate)
1267 return (dump_filesystem(rzhp, sdd));
1269 /* Mark the clone origin snapshots. */
1270 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1271 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1273 uint64_t origin_guid = 0;
1275 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1276 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1277 if (origin_guid != 0) {
1279 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1280 origin_guid, &snapname);
1281 if (origin_nv != NULL) {
1282 nvlist_t *snapprops;
1283 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1284 "snapprops", &snapprops));
1285 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1286 snapname, &snapprops));
1287 VERIFY(0 == nvlist_add_boolean(
1288 snapprops, "is_clone_origin"));
1293 needagain = progress = B_FALSE;
1294 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1295 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1296 nvlist_t *fslist, *parent_nv;
1300 uint64_t origin_guid = 0;
1301 uint64_t parent_guid = 0;
1303 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1304 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1307 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1308 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1309 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1312 if (parent_guid != 0) {
1313 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1314 if (!nvlist_exists(parent_nv, "sent")) {
1315 /* parent has not been sent; skip this one */
1321 if (origin_guid != 0) {
1322 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1324 if (origin_nv != NULL &&
1325 !nvlist_exists(origin_nv, "sent")) {
1327 * origin has not been sent yet;
1335 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1338 err = dump_filesystem(zhp, sdd);
1339 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1350 /* clean out the sent flags in case we reuse this fss */
1351 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1352 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1355 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1356 (void) nvlist_remove_all(fslist, "sent");
1363 * Generate a send stream for the dataset identified by the argument zhp.
1365 * The content of the send stream is the snapshot identified by
1366 * 'tosnap'. Incremental streams are requested in two ways:
1367 * - from the snapshot identified by "fromsnap" (if non-null) or
1368 * - from the origin of the dataset identified by zhp, which must
1369 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1372 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1373 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1374 * if "replicate" is set. If "doall" is set, dump all the intermediate
1375 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1376 * case too. If "props" is set, send properties.
1379 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1380 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1381 void *cb_arg, nvlist_t **debugnvp)
1384 send_dump_data_t sdd = { 0 };
1386 nvlist_t *fss = NULL;
1387 avl_tree_t *fsavl = NULL;
1388 static uint64_t holdseq;
1392 dedup_arg_t dda = { 0 };
1393 int featureflags = 0;
1395 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1396 "cannot send '%s'"), zhp->zfs_name);
1398 if (fromsnap && fromsnap[0] == '\0') {
1399 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1400 "zero-length incremental source"));
1401 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1404 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1406 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1407 if (version >= ZPL_VERSION_SA) {
1408 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1412 if (flags->dedup && !flags->dryrun) {
1413 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1414 DMU_BACKUP_FEATURE_DEDUPPROPS);
1415 if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd))) {
1416 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1417 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1420 dda.outputfd = outfd;
1421 dda.inputfd = pipefd[1];
1422 dda.dedup_hdl = zhp->zfs_hdl;
1423 if ((err = pthread_create(&tid, NULL, cksummer, &dda))) {
1424 (void) close(pipefd[0]);
1425 (void) close(pipefd[1]);
1426 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1427 return (zfs_error(zhp->zfs_hdl,
1428 EZFS_THREADCREATEFAILED, errbuf));
1432 if (flags->replicate || flags->doall || flags->props) {
1433 dmu_replay_record_t drr = { 0 };
1434 char *packbuf = NULL;
1436 zio_cksum_t zc = { { 0 } };
1438 if (flags->replicate || flags->props) {
1441 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1443 VERIFY(0 == nvlist_add_string(hdrnv,
1444 "fromsnap", fromsnap));
1446 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1447 if (!flags->replicate) {
1448 VERIFY(0 == nvlist_add_boolean(hdrnv,
1452 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1453 fromsnap, tosnap, flags->replicate, &fss, &fsavl);
1456 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1457 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1464 fsavl_destroy(fsavl);
1470 if (!flags->dryrun) {
1471 /* write first begin record */
1472 drr.drr_type = DRR_BEGIN;
1473 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1474 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1475 drr_versioninfo, DMU_COMPOUNDSTREAM);
1476 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1477 drr_versioninfo, featureflags);
1478 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1479 sizeof (drr.drr_u.drr_begin.drr_toname),
1480 "%s@%s", zhp->zfs_name, tosnap);
1481 drr.drr_payloadlen = buflen;
1482 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
1484 /* write header nvlist */
1485 if (err != -1 && packbuf != NULL) {
1486 err = cksum_and_write(packbuf, buflen, &zc,
1491 fsavl_destroy(fsavl);
1497 /* write end record */
1498 bzero(&drr, sizeof (drr));
1499 drr.drr_type = DRR_END;
1500 drr.drr_u.drr_end.drr_checksum = zc;
1501 err = write(outfd, &drr, sizeof (drr));
1503 fsavl_destroy(fsavl);
1513 /* dump each stream */
1514 sdd.fromsnap = fromsnap;
1515 sdd.tosnap = tosnap;
1517 sdd.outfd = pipefd[0];
1520 sdd.replicate = flags->replicate;
1521 sdd.doall = flags->doall;
1522 sdd.fromorigin = flags->fromorigin;
1525 sdd.verbose = flags->verbose;
1526 sdd.parsable = flags->parsable;
1527 sdd.progress = flags->progress;
1528 sdd.dryrun = flags->dryrun;
1529 sdd.filter_cb = filter_func;
1530 sdd.filter_cb_arg = cb_arg;
1532 sdd.debugnv = *debugnvp;
1535 * Some flags require that we place user holds on the datasets that are
1536 * being sent so they don't get destroyed during the send. We can skip
1537 * this step if the pool is imported read-only since the datasets cannot
1540 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1541 ZPOOL_PROP_READONLY, NULL) &&
1542 zfs_spa_version(zhp, &spa_version) == 0 &&
1543 spa_version >= SPA_VERSION_USERREFS &&
1544 (flags->doall || flags->replicate)) {
1546 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1547 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1548 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
1549 if (sdd.cleanup_fd < 0) {
1554 sdd.cleanup_fd = -1;
1556 if (flags->verbose) {
1558 * Do a verbose no-op dry run to get all the verbose output
1559 * before generating any data. Then do a non-verbose real
1560 * run to generate the streams.
1562 sdd.dryrun = B_TRUE;
1563 err = dump_filesystems(zhp, &sdd);
1564 sdd.dryrun = flags->dryrun;
1565 sdd.verbose = B_FALSE;
1566 if (flags->parsable) {
1567 (void) fprintf(stderr, "size\t%llu\n",
1568 (longlong_t)sdd.size);
1571 zfs_nicenum(sdd.size, buf, sizeof (buf));
1572 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1573 "total estimated size is %s\n"), buf);
1576 err = dump_filesystems(zhp, &sdd);
1577 fsavl_destroy(fsavl);
1581 (void) close(pipefd[0]);
1582 (void) pthread_join(tid, NULL);
1585 if (sdd.cleanup_fd != -1) {
1586 VERIFY(0 == close(sdd.cleanup_fd));
1587 sdd.cleanup_fd = -1;
1590 if (!flags->dryrun && (flags->replicate || flags->doall ||
1593 * write final end record. NB: want to do this even if
1594 * there was some error, because it might not be totally
1597 dmu_replay_record_t drr = { 0 };
1598 drr.drr_type = DRR_END;
1599 if (write(outfd, &drr, sizeof (drr)) == -1) {
1600 return (zfs_standard_error(zhp->zfs_hdl,
1605 return (err || sdd.err);
1608 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1610 if (sdd.cleanup_fd != -1)
1611 VERIFY(0 == close(sdd.cleanup_fd));
1613 (void) pthread_cancel(tid);
1614 (void) pthread_join(tid, NULL);
1615 (void) close(pipefd[0]);
1621 * Routines specific to "zfs recv"
1625 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1626 boolean_t byteswap, zio_cksum_t *zc)
1633 rv = read(fd, cp, len);
1638 if (rv < 0 || len != 0) {
1639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1640 "failed to read from stream"));
1641 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1642 "cannot receive")));
1647 fletcher_4_incremental_byteswap(buf, ilen, zc);
1649 fletcher_4_incremental_native(buf, ilen, zc);
1655 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1656 boolean_t byteswap, zio_cksum_t *zc)
1661 buf = zfs_alloc(hdl, len);
1665 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1671 err = nvlist_unpack(buf, len, nvp, 0);
1674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1675 "stream (malformed nvlist)"));
1682 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1683 int baselen, char *newname, recvflags_t *flags)
1686 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1688 prop_changelist_t *clp;
1691 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1694 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1695 flags->force ? MS_FORCE : 0);
1699 err = changelist_prefix(clp);
1703 zc.zc_objset_type = DMU_OST_ZFS;
1704 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1707 (void) strcpy(newname, tryname);
1709 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1711 if (flags->verbose) {
1712 (void) printf("attempting rename %s to %s\n",
1713 zc.zc_name, zc.zc_value);
1715 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1717 changelist_rename(clp, name, tryname);
1722 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
1725 (void) strncpy(newname, name, baselen);
1726 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
1727 "recv-%ld-%u", (long) getpid(), seq);
1728 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1730 if (flags->verbose) {
1731 (void) printf("failed - trying rename %s to %s\n",
1732 zc.zc_name, zc.zc_value);
1734 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1736 changelist_rename(clp, name, newname);
1737 if (err && flags->verbose) {
1738 (void) printf("failed (%u) - "
1739 "will try again on next pass\n", errno);
1742 } else if (flags->verbose) {
1744 (void) printf("success\n");
1746 (void) printf("failed (%u)\n", errno);
1749 (void) changelist_postfix(clp);
1750 changelist_free(clp);
1756 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1757 char *newname, recvflags_t *flags)
1759 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1761 prop_changelist_t *clp;
1763 boolean_t defer = B_FALSE;
1766 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1769 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1770 flags->force ? MS_FORCE : 0);
1771 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1772 zfs_spa_version(zhp, &spa_version) == 0 &&
1773 spa_version >= SPA_VERSION_USERREFS)
1778 err = changelist_prefix(clp);
1782 zc.zc_objset_type = DMU_OST_ZFS;
1783 zc.zc_defer_destroy = defer;
1784 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1787 (void) printf("attempting destroy %s\n", zc.zc_name);
1788 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1791 (void) printf("success\n");
1792 changelist_remove(clp, zc.zc_name);
1795 (void) changelist_postfix(clp);
1796 changelist_free(clp);
1799 * Deferred destroy might destroy the snapshot or only mark it to be
1800 * destroyed later, and it returns success in either case.
1802 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1803 ZFS_TYPE_SNAPSHOT))) {
1804 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1810 typedef struct guid_to_name_data {
1814 } guid_to_name_data_t;
1817 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1819 guid_to_name_data_t *gtnd = arg;
1822 if (gtnd->skip != NULL &&
1823 strcmp(zhp->zfs_name, gtnd->skip) == 0) {
1827 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1828 (void) strcpy(gtnd->name, zhp->zfs_name);
1833 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1839 * Attempt to find the local dataset associated with this guid. In the case of
1840 * multiple matches, we attempt to find the "best" match by searching
1841 * progressively larger portions of the hierarchy. This allows one to send a
1842 * tree of datasets individually and guarantee that we will find the source
1843 * guid within that hierarchy, even if there are multiple matches elsewhere.
1846 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1849 /* exhaustive search all local snapshots */
1850 char pname[ZFS_MAXNAMELEN];
1851 guid_to_name_data_t gtnd;
1860 (void) strlcpy(pname, parent, sizeof (pname));
1863 * Search progressively larger portions of the hierarchy. This will
1864 * select the "most local" version of the origin snapshot in the case
1865 * that there are multiple matching snapshots in the system.
1867 while ((cp = strrchr(pname, '/')) != NULL) {
1869 /* Chop off the last component and open the parent */
1871 zhp = make_dataset_handle(hdl, pname);
1876 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1882 * Remember the dataset that we already searched, so we
1883 * skip it next time through.
1892 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1893 * guid1 is after guid2.
1896 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1897 uint64_t guid1, uint64_t guid2)
1900 char *fsname, *snapname;
1901 char buf[ZFS_MAXNAMELEN];
1903 zfs_handle_t *guid1hdl, *guid2hdl;
1904 uint64_t create1, create2;
1911 nvfs = fsavl_find(avl, guid1, &snapname);
1912 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1913 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1914 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1915 if (guid1hdl == NULL)
1918 nvfs = fsavl_find(avl, guid2, &snapname);
1919 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1920 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1921 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1922 if (guid2hdl == NULL) {
1923 zfs_close(guid1hdl);
1927 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
1928 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
1930 if (create1 < create2)
1932 else if (create1 > create2)
1937 zfs_close(guid1hdl);
1938 zfs_close(guid2hdl);
1944 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1945 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
1949 avl_tree_t *local_avl;
1950 nvpair_t *fselem, *nextfselem;
1952 char newname[ZFS_MAXNAMELEN];
1954 boolean_t needagain, progress, recursive;
1957 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1959 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
1966 needagain = progress = B_FALSE;
1968 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1969 recursive, &local_nv, &local_avl)) != 0)
1973 * Process deletes and renames
1975 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1976 fselem; fselem = nextfselem) {
1977 nvlist_t *nvfs, *snaps;
1978 nvlist_t *stream_nvfs = NULL;
1979 nvpair_t *snapelem, *nextsnapelem;
1980 uint64_t fromguid = 0;
1981 uint64_t originguid = 0;
1982 uint64_t stream_originguid = 0;
1983 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1984 char *fsname, *stream_fsname;
1986 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1988 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1989 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1990 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1991 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1992 &parent_fromsnap_guid));
1993 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1996 * First find the stream's fs, so we can check for
1997 * a different origin (due to "zfs promote")
1999 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2000 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2003 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2004 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2006 if (stream_nvfs != NULL)
2010 /* check for promote */
2011 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2012 &stream_originguid);
2013 if (stream_nvfs && originguid != stream_originguid) {
2014 switch (created_before(hdl, local_avl,
2015 stream_originguid, originguid)) {
2018 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2019 nvlist_t *origin_nvfs;
2020 char *origin_fsname;
2023 (void) printf("promoting %s\n", fsname);
2025 origin_nvfs = fsavl_find(local_avl, originguid,
2027 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2028 "name", &origin_fsname));
2029 (void) strlcpy(zc.zc_value, origin_fsname,
2030 sizeof (zc.zc_value));
2031 (void) strlcpy(zc.zc_name, fsname,
2032 sizeof (zc.zc_name));
2033 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2041 fsavl_destroy(local_avl);
2042 nvlist_free(local_nv);
2046 * We had/have the wrong origin, therefore our
2047 * list of snapshots is wrong. Need to handle
2048 * them on the next pass.
2054 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2055 snapelem; snapelem = nextsnapelem) {
2057 char *stream_snapname;
2058 nvlist_t *found, *props;
2060 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2062 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2063 found = fsavl_find(stream_avl, thisguid,
2066 /* check for delete */
2067 if (found == NULL) {
2068 char name[ZFS_MAXNAMELEN];
2073 (void) snprintf(name, sizeof (name), "%s@%s",
2074 fsname, nvpair_name(snapelem));
2076 error = recv_destroy(hdl, name,
2077 strlen(fsname)+1, newname, flags);
2085 stream_nvfs = found;
2087 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2088 &props) && 0 == nvlist_lookup_nvlist(props,
2089 stream_snapname, &props)) {
2090 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2092 zc.zc_cookie = B_TRUE; /* received */
2093 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2094 "%s@%s", fsname, nvpair_name(snapelem));
2095 if (zcmd_write_src_nvlist(hdl, &zc,
2097 (void) zfs_ioctl(hdl,
2098 ZFS_IOC_SET_PROP, &zc);
2099 zcmd_free_nvlists(&zc);
2103 /* check for different snapname */
2104 if (strcmp(nvpair_name(snapelem),
2105 stream_snapname) != 0) {
2106 char name[ZFS_MAXNAMELEN];
2107 char tryname[ZFS_MAXNAMELEN];
2109 (void) snprintf(name, sizeof (name), "%s@%s",
2110 fsname, nvpair_name(snapelem));
2111 (void) snprintf(tryname, sizeof (name), "%s@%s",
2112 fsname, stream_snapname);
2114 error = recv_rename(hdl, name, tryname,
2115 strlen(fsname)+1, newname, flags);
2122 if (strcmp(stream_snapname, fromsnap) == 0)
2123 fromguid = thisguid;
2126 /* check for delete */
2127 if (stream_nvfs == NULL) {
2131 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2140 if (fromguid == 0) {
2141 if (flags->verbose) {
2142 (void) printf("local fs %s does not have "
2143 "fromsnap (%s in stream); must have "
2144 "been deleted locally; ignoring\n",
2150 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2151 "name", &stream_fsname));
2152 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2153 "parentfromsnap", &stream_parent_fromsnap_guid));
2155 s1 = strrchr(fsname, '/');
2156 s2 = strrchr(stream_fsname, '/');
2159 * Check for rename. If the exact receive path is specified, it
2160 * does not count as a rename, but we still need to check the
2161 * datasets beneath it.
2163 if ((stream_parent_fromsnap_guid != 0 &&
2164 parent_fromsnap_guid != 0 &&
2165 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2166 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2167 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2169 char tryname[ZFS_MAXNAMELEN];
2171 parent = fsavl_find(local_avl,
2172 stream_parent_fromsnap_guid, NULL);
2174 * NB: parent might not be found if we used the
2175 * tosnap for stream_parent_fromsnap_guid,
2176 * because the parent is a newly-created fs;
2177 * we'll be able to rename it after we recv the
2180 if (parent != NULL) {
2183 VERIFY(0 == nvlist_lookup_string(parent, "name",
2185 (void) snprintf(tryname, sizeof (tryname),
2186 "%s%s", pname, strrchr(stream_fsname, '/'));
2189 if (flags->verbose) {
2190 (void) printf("local fs %s new parent "
2191 "not found\n", fsname);
2197 error = recv_rename(hdl, fsname, tryname,
2198 strlen(tofs)+1, newname, flags);
2200 if (renamed != NULL && newname[0] != '\0') {
2201 VERIFY(0 == nvlist_add_boolean(renamed,
2212 fsavl_destroy(local_avl);
2213 nvlist_free(local_nv);
2215 if (needagain && progress) {
2216 /* do another pass to fix up temporary names */
2218 (void) printf("another pass:\n");
2226 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2227 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2228 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2230 nvlist_t *stream_nv = NULL;
2231 avl_tree_t *stream_avl = NULL;
2232 char *fromsnap = NULL;
2234 char tofs[ZFS_MAXNAMELEN];
2235 char sendfs[ZFS_MAXNAMELEN];
2237 dmu_replay_record_t drre;
2239 boolean_t anyerr = B_FALSE;
2240 boolean_t softerr = B_FALSE;
2241 boolean_t recursive;
2243 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2246 assert(drr->drr_type == DRR_BEGIN);
2247 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2248 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2249 DMU_COMPOUNDSTREAM);
2252 * Read in the nvlist from the stream.
2254 if (drr->drr_payloadlen != 0) {
2255 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2256 &stream_nv, flags->byteswap, zc);
2258 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2263 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2266 if (recursive && strchr(destname, '@')) {
2267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2268 "cannot specify snapshot name for multi-snapshot stream"));
2269 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2274 * Read in the end record and verify checksum.
2276 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2277 flags->byteswap, NULL)))
2279 if (flags->byteswap) {
2280 drre.drr_type = BSWAP_32(drre.drr_type);
2281 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2282 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2283 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2284 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2285 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2286 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2287 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2288 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2290 if (drre.drr_type != DRR_END) {
2291 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2294 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2296 "incorrect header checksum"));
2297 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2301 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2303 if (drr->drr_payloadlen != 0) {
2304 nvlist_t *stream_fss;
2306 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2308 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2309 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2310 "couldn't allocate avl tree"));
2311 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2315 if (fromsnap != NULL) {
2316 nvlist_t *renamed = NULL;
2317 nvpair_t *pair = NULL;
2319 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2320 if (flags->isprefix) {
2321 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2324 if (flags->istail) {
2325 cp = strrchr(drrb->drr_toname, '/');
2327 (void) strlcat(tofs, "/",
2331 i = (cp - drrb->drr_toname);
2334 i = strcspn(drrb->drr_toname, "/@");
2336 /* zfs_receive_one() will create_parents() */
2337 (void) strlcat(tofs, &drrb->drr_toname[i],
2339 *strchr(tofs, '@') = '\0';
2342 if (recursive && !flags->dryrun && !flags->nomount) {
2343 VERIFY(0 == nvlist_alloc(&renamed,
2344 NV_UNIQUE_NAME, 0));
2347 softerr = recv_incremental_replication(hdl, tofs, flags,
2348 stream_nv, stream_avl, renamed);
2350 /* Unmount renamed filesystems before receiving. */
2351 while ((pair = nvlist_next_nvpair(renamed,
2354 prop_changelist_t *clp = NULL;
2356 zhp = zfs_open(hdl, nvpair_name(pair),
2357 ZFS_TYPE_FILESYSTEM);
2359 clp = changelist_gather(zhp,
2360 ZFS_PROP_MOUNTPOINT, 0, 0);
2364 changelist_prefix(clp);
2365 changelist_free(clp);
2370 nvlist_free(renamed);
2375 * Get the fs specified by the first path in the stream (the top level
2376 * specified by 'zfs send') and pass it to each invocation of
2377 * zfs_receive_one().
2379 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2381 if ((cp = strchr(sendfs, '@')) != NULL)
2384 /* Finally, receive each contained stream */
2387 * we should figure out if it has a recoverable
2388 * error, in which case do a recv_skip() and drive on.
2389 * Note, if we fail due to already having this guid,
2390 * zfs_receive_one() will take care of it (ie,
2391 * recv_skip() and return 0).
2393 error = zfs_receive_impl(hdl, destname, flags, fd,
2394 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2396 if (error == ENODATA) {
2401 } while (error == 0);
2403 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2405 * Now that we have the fs's they sent us, try the
2408 softerr = recv_incremental_replication(hdl, tofs, flags,
2409 stream_nv, stream_avl, NULL);
2413 fsavl_destroy(stream_avl);
2415 nvlist_free(stream_nv);
2424 trunc_prop_errs(int truncated)
2426 ASSERT(truncated != 0);
2429 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2430 "1 more property could not be set\n"));
2432 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2433 "%d more properties could not be set\n"), truncated);
2437 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2439 dmu_replay_record_t *drr;
2440 void *buf = malloc(1<<20);
2443 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2444 "cannot receive:"));
2446 /* XXX would be great to use lseek if possible... */
2449 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2450 byteswap, NULL) == 0) {
2452 drr->drr_type = BSWAP_32(drr->drr_type);
2454 switch (drr->drr_type) {
2456 /* NB: not to be used on v2 stream packages */
2457 if (drr->drr_payloadlen != 0) {
2458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2459 "invalid substream header"));
2460 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2470 drr->drr_u.drr_object.drr_bonuslen =
2471 BSWAP_32(drr->drr_u.drr_object.
2474 (void) recv_read(hdl, fd, buf,
2475 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2481 drr->drr_u.drr_write.drr_length =
2482 BSWAP_64(drr->drr_u.drr_write.drr_length);
2484 (void) recv_read(hdl, fd, buf,
2485 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2489 drr->drr_u.drr_write.drr_length =
2490 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2492 (void) recv_read(hdl, fd, buf,
2493 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2495 case DRR_WRITE_BYREF:
2496 case DRR_FREEOBJECTS:
2501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2502 "invalid record type"));
2503 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2512 * Restores a backup of tosnap from the file descriptor specified by infd.
2515 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2516 recvflags_t *flags, dmu_replay_record_t *drr,
2517 dmu_replay_record_t *drr_noswap, const char *sendfs,
2518 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2519 uint64_t *action_handlep)
2521 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2523 int ioctl_err, ioctl_errno, err;
2525 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2527 char prop_errbuf[1024];
2528 const char *chopprefix;
2529 boolean_t newfs = B_FALSE;
2530 boolean_t stream_wantsnewfs;
2531 uint64_t parent_snapguid = 0;
2532 prop_changelist_t *clp = NULL;
2533 nvlist_t *snapprops_nvlist = NULL;
2534 zprop_errflags_t prop_errflags;
2535 boolean_t recursive;
2537 begin_time = time(NULL);
2539 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2542 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2545 if (stream_avl != NULL) {
2547 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2552 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2554 err = nvlist_lookup_nvlist(fs, "props", &props);
2556 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2558 if (flags->canmountoff) {
2559 VERIFY(0 == nvlist_add_uint64(props,
2560 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2562 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2566 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2567 VERIFY(0 == nvlist_lookup_nvlist(props,
2568 snapname, &snapprops_nvlist));
2578 * Determine how much of the snapshot name stored in the stream
2579 * we are going to tack on to the name they specified on the
2580 * command line, and how much we are going to chop off.
2582 * If they specified a snapshot, chop the entire name stored in
2585 if (flags->istail) {
2587 * A filesystem was specified with -e. We want to tack on only
2588 * the tail of the sent snapshot path.
2590 if (strchr(tosnap, '@')) {
2591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2592 "argument - snapshot not allowed with -e"));
2593 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2596 chopprefix = strrchr(sendfs, '/');
2598 if (chopprefix == NULL) {
2600 * The tail is the poolname, so we need to
2601 * prepend a path separator.
2603 int len = strlen(drrb->drr_toname);
2604 cp = malloc(len + 2);
2606 (void) strcpy(&cp[1], drrb->drr_toname);
2609 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2611 } else if (flags->isprefix) {
2613 * A filesystem was specified with -d. We want to tack on
2614 * everything but the first element of the sent snapshot path
2615 * (all but the pool name).
2617 if (strchr(tosnap, '@')) {
2618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2619 "argument - snapshot not allowed with -d"));
2620 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2623 chopprefix = strchr(drrb->drr_toname, '/');
2624 if (chopprefix == NULL)
2625 chopprefix = strchr(drrb->drr_toname, '@');
2626 } else if (strchr(tosnap, '@') == NULL) {
2628 * If a filesystem was specified without -d or -e, we want to
2629 * tack on everything after the fs specified by 'zfs send'.
2631 chopprefix = drrb->drr_toname + strlen(sendfs);
2633 /* A snapshot was specified as an exact path (no -d or -e). */
2635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2636 "cannot specify snapshot name for multi-snapshot "
2638 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2640 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2643 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2644 ASSERT(chopprefix > drrb->drr_toname);
2645 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2646 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2647 chopprefix[0] == '\0');
2650 * Determine name of destination snapshot, store in zc_value.
2652 (void) strcpy(zc.zc_top_ds, tosnap);
2653 (void) strcpy(zc.zc_value, tosnap);
2654 (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2656 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2657 zcmd_free_nvlists(&zc);
2658 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2662 * Determine the name of the origin snapshot, store in zc_string.
2664 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2665 if (guid_to_name(hdl, zc.zc_value,
2666 drrb->drr_fromguid, zc.zc_string) != 0) {
2667 zcmd_free_nvlists(&zc);
2668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2669 "local origin for clone %s does not exist"),
2671 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2674 (void) printf("found clone origin %s\n", zc.zc_string);
2677 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2678 (drrb->drr_flags & DRR_FLAG_CLONE));
2680 if (stream_wantsnewfs) {
2682 * if the parent fs does not exist, look for it based on
2683 * the parent snap GUID
2685 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2686 "cannot receive new filesystem stream"));
2688 (void) strcpy(zc.zc_name, zc.zc_value);
2689 cp = strrchr(zc.zc_name, '/');
2693 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2694 char suffix[ZFS_MAXNAMELEN];
2695 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2696 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
2697 zc.zc_value) == 0) {
2698 *strchr(zc.zc_value, '@') = '\0';
2699 (void) strcat(zc.zc_value, suffix);
2704 * if the fs does not exist, look for it based on the
2707 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2708 "cannot receive incremental stream"));
2710 (void) strcpy(zc.zc_name, zc.zc_value);
2711 *strchr(zc.zc_name, '@') = '\0';
2714 * If the exact receive path was specified and this is the
2715 * topmost path in the stream, then if the fs does not exist we
2716 * should look no further.
2718 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
2719 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2720 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2721 char snap[ZFS_MAXNAMELEN];
2722 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2723 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
2724 zc.zc_value) == 0) {
2725 *strchr(zc.zc_value, '@') = '\0';
2726 (void) strcat(zc.zc_value, snap);
2731 (void) strcpy(zc.zc_name, zc.zc_value);
2732 *strchr(zc.zc_name, '@') = '\0';
2734 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2738 * Destination fs exists. Therefore this should either
2739 * be an incremental, or the stream specifies a new fs
2740 * (full stream or clone) and they want us to blow it
2741 * away (and have therefore specified -F and removed any
2744 if (stream_wantsnewfs) {
2745 if (!flags->force) {
2746 zcmd_free_nvlists(&zc);
2747 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2748 "destination '%s' exists\n"
2749 "must specify -F to overwrite it"),
2751 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2753 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2755 zcmd_free_nvlists(&zc);
2756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2757 "destination has snapshots (eg. %s)\n"
2758 "must destroy them to overwrite it"),
2760 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2764 if ((zhp = zfs_open(hdl, zc.zc_name,
2765 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2766 zcmd_free_nvlists(&zc);
2770 if (stream_wantsnewfs &&
2771 zhp->zfs_dmustats.dds_origin[0]) {
2772 zcmd_free_nvlists(&zc);
2774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2775 "destination '%s' is a clone\n"
2776 "must destroy it to overwrite it"),
2778 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2781 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2782 stream_wantsnewfs) {
2783 /* We can't do online recv in this case */
2784 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2787 zcmd_free_nvlists(&zc);
2790 if (changelist_prefix(clp) != 0) {
2791 changelist_free(clp);
2793 zcmd_free_nvlists(&zc);
2797 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
2798 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
2800 zcmd_free_nvlists(&zc);
2806 * Destination filesystem does not exist. Therefore we better
2807 * be creating a new filesystem (either from a full backup, or
2808 * a clone). It would therefore be invalid if the user
2809 * specified only the pool name (i.e. if the destination name
2810 * contained no slash character).
2812 if (!stream_wantsnewfs ||
2813 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2814 zcmd_free_nvlists(&zc);
2815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2816 "destination '%s' does not exist"), zc.zc_name);
2817 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2821 * Trim off the final dataset component so we perform the
2822 * recvbackup ioctl to the filesystems's parent.
2826 if (flags->isprefix && !flags->istail && !flags->dryrun &&
2827 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2828 zcmd_free_nvlists(&zc);
2829 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2835 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2836 zc.zc_cookie = infd;
2837 zc.zc_guid = flags->force;
2838 if (flags->verbose) {
2839 (void) printf("%s %s stream of %s into %s\n",
2840 flags->dryrun ? "would receive" : "receiving",
2841 drrb->drr_fromguid ? "incremental" : "full",
2842 drrb->drr_toname, zc.zc_value);
2843 (void) fflush(stdout);
2846 if (flags->dryrun) {
2847 zcmd_free_nvlists(&zc);
2848 return (recv_skip(hdl, infd, flags->byteswap));
2851 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2852 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2853 zc.zc_cleanup_fd = cleanup_fd;
2854 zc.zc_action_handle = *action_handlep;
2856 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2857 ioctl_errno = errno;
2858 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2861 nvlist_t *prop_errors;
2862 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2863 zc.zc_nvlist_dst_size, &prop_errors, 0));
2865 nvpair_t *prop_err = NULL;
2867 while ((prop_err = nvlist_next_nvpair(prop_errors,
2868 prop_err)) != NULL) {
2873 prop = zfs_name_to_prop(nvpair_name(prop_err));
2874 (void) nvpair_value_int32(prop_err, &intval);
2875 if (strcmp(nvpair_name(prop_err),
2876 ZPROP_N_MORE_ERRORS) == 0) {
2877 trunc_prop_errs(intval);
2880 (void) snprintf(tbuf, sizeof (tbuf),
2881 dgettext(TEXT_DOMAIN,
2882 "cannot receive %s property on %s"),
2883 nvpair_name(prop_err), zc.zc_name);
2884 zfs_setprop_error(hdl, prop, intval, tbuf);
2887 nvlist_free(prop_errors);
2890 zc.zc_nvlist_dst = 0;
2891 zc.zc_nvlist_dst_size = 0;
2892 zcmd_free_nvlists(&zc);
2894 if (err == 0 && snapprops_nvlist) {
2895 zfs_cmd_t zc2 = { "\0", "\0", "\0", "\0", 0 };
2897 (void) strcpy(zc2.zc_name, zc.zc_value);
2898 zc2.zc_cookie = B_TRUE; /* received */
2899 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
2900 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
2901 zcmd_free_nvlists(&zc2);
2905 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
2907 * It may be that this snapshot already exists,
2908 * in which case we want to consume & ignore it
2909 * rather than failing.
2911 avl_tree_t *local_avl;
2912 nvlist_t *local_nv, *fs;
2913 cp = strchr(zc.zc_value, '@');
2916 * XXX Do this faster by just iterating over snaps in
2917 * this fs. Also if zc_value does not exist, we will
2918 * get a strange "does not exist" error message.
2921 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
2922 &local_nv, &local_avl) == 0) {
2924 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
2925 fsavl_destroy(local_avl);
2926 nvlist_free(local_nv);
2929 if (flags->verbose) {
2930 (void) printf("snap %s already exists; "
2931 "ignoring\n", zc.zc_value);
2933 err = ioctl_err = recv_skip(hdl, infd,
2940 if (ioctl_err != 0) {
2941 switch (ioctl_errno) {
2943 cp = strchr(zc.zc_value, '@');
2945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2946 "most recent snapshot of %s does not\n"
2947 "match incremental source"), zc.zc_value);
2948 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2953 "destination %s has been modified\n"
2954 "since most recent snapshot"), zc.zc_name);
2955 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2958 cp = strchr(zc.zc_value, '@');
2960 /* it's the containing fs that exists */
2963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2964 "destination already exists"));
2965 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
2966 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
2971 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2974 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2975 "invalid stream (checksum mismatch)"));
2976 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2980 "pool must be upgraded to receive this stream."));
2981 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
2984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2985 "destination %s space quota exceeded"), zc.zc_name);
2986 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
2989 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
2994 * Mount the target filesystem (if created). Also mount any
2995 * children of the target filesystem if we did a replication
2996 * receive (indicated by stream_avl being non-NULL).
2998 cp = strchr(zc.zc_value, '@');
2999 if (cp && (ioctl_err == 0 || !newfs)) {
3003 h = zfs_open(hdl, zc.zc_value,
3004 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3006 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3008 err = zvol_create_link(hdl, h->zfs_name);
3009 if (err == 0 && ioctl_err == 0)
3010 err = zvol_create_link(hdl,
3012 } else if (newfs || stream_avl) {
3014 * Track the first/top of hierarchy fs,
3015 * for mounting and sharing later.
3017 if (top_zfs && *top_zfs == NULL)
3018 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3026 err |= changelist_postfix(clp);
3027 changelist_free(clp);
3030 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3031 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3032 "failed to clear unreceived properties on %s"),
3034 (void) fprintf(stderr, "\n");
3036 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3037 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3038 "failed to restore original properties on %s"),
3040 (void) fprintf(stderr, "\n");
3043 if (err || ioctl_err)
3046 *action_handlep = zc.zc_action_handle;
3048 if (flags->verbose) {
3051 uint64_t bytes = zc.zc_cookie;
3052 time_t delta = time(NULL) - begin_time;
3055 zfs_nicenum(bytes, buf1, sizeof (buf1));
3056 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3058 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3066 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3067 int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3068 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
3071 dmu_replay_record_t drr, drr_noswap;
3072 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3074 zio_cksum_t zcksum = { { 0 } };
3075 uint64_t featureflags;
3078 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3081 if (flags->isprefix &&
3082 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3084 "(%s) does not exist"), tosnap);
3085 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3088 /* read in the BEGIN record */
3089 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3093 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3094 /* It's the double end record at the end of a package */
3098 /* the kernel needs the non-byteswapped begin record */
3101 flags->byteswap = B_FALSE;
3102 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3104 * We computed the checksum in the wrong byteorder in
3105 * recv_read() above; do it again correctly.
3107 bzero(&zcksum, sizeof (zio_cksum_t));
3108 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
3109 flags->byteswap = B_TRUE;
3111 drr.drr_type = BSWAP_32(drr.drr_type);
3112 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3113 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3114 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3115 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3116 drrb->drr_type = BSWAP_32(drrb->drr_type);
3117 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3118 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3119 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3122 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3124 "stream (bad magic number)"));
3125 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3128 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3129 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3131 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3132 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3134 "stream has unsupported feature, feature flags = %lx"),
3136 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3139 if (strchr(drrb->drr_toname, '@') == NULL) {
3140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3141 "stream (bad snapshot name)"));
3142 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3145 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3146 char nonpackage_sendfs[ZFS_MAXNAMELEN];
3147 if (sendfs == NULL) {
3149 * We were not called from zfs_receive_package(). Get
3150 * the fs specified by 'zfs send'.
3153 (void) strlcpy(nonpackage_sendfs,
3154 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
3155 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3157 sendfs = nonpackage_sendfs;
3159 return (zfs_receive_one(hdl, infd, tosnap, flags,
3160 &drr, &drr_noswap, sendfs, stream_nv, stream_avl,
3161 top_zfs, cleanup_fd, action_handlep));
3163 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3164 DMU_COMPOUNDSTREAM);
3165 return (zfs_receive_package(hdl, infd, tosnap, flags,
3166 &drr, &zcksum, top_zfs, cleanup_fd, action_handlep));
3171 * Restores a backup of tosnap from the file descriptor specified by infd.
3172 * Return 0 on total success, -2 if some things couldn't be
3173 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3174 * (-1 will override -2).
3177 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3178 int infd, avl_tree_t *stream_avl)
3180 char *top_zfs = NULL;
3183 uint64_t action_handle = 0;
3185 cleanup_fd = open(ZFS_DEV, O_RDWR);
3186 VERIFY(cleanup_fd >= 0);
3188 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
3189 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3191 VERIFY(0 == close(cleanup_fd));
3193 if (err == 0 && !flags->nomount && top_zfs) {
3195 prop_changelist_t *clp;
3197 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3199 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3200 CL_GATHER_MOUNT_ALWAYS, 0);
3203 /* mount and share received datasets */
3204 err = changelist_postfix(clp);
3205 changelist_free(clp);
3208 if (zhp == NULL || clp == NULL || err)