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 Pawel Jakub Dawidek <pawel@dawidek.net>.
38 #include <sys/mount.h>
39 #include <sys/mntent.h>
40 #include <sys/mnttab.h>
42 #include <sys/debug.h>
49 #include "zfs_namecheck.h"
51 #include "zfs_fletcher.h"
52 #include "libzfs_impl.h"
53 #include <sys/zio_checksum.h>
55 #include <sys/socket.h>
57 /* in libzfs_dataset.c */
58 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
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 *);
63 static const zio_cksum_t zero_cksum = { { 0 } };
65 typedef struct dedup_arg {
68 libzfs_handle_t *dedup_hdl;
71 typedef struct dataref {
77 typedef struct dedup_entry {
78 struct dedup_entry *dde_next;
79 zio_cksum_t dde_chksum;
84 #define MAX_DDT_PHYSMEM_PERCENT 20
85 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
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 */
98 high_order_bit(uint64_t n)
102 for (count = 0; n != 0; count++)
108 ssread(void *buf, size_t len, FILE *stream)
112 if ((outlen = fread(buf, len, 1, stream)) == 0)
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)
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;
134 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
136 assert(*ddepp == NULL);
137 dde->dde_next = NULL;
138 dde->dde_chksum = *cs;
139 dde->dde_prop = prop;
142 ddt->cur_ddt_size += sizeof (dedup_entry_t);
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.
153 * return value: true - entry was found
154 * false - entry was not found
157 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
158 uint64_t prop, dataref_t *dr)
161 dedup_entry_t **ddepp;
163 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
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;
173 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
178 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
180 fletcher_4_incremental_native(buf, len, zc);
181 return (write(outfd, buf, len));
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
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().
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;
215 dmu_replay_record_t wbr_drr = {0};
216 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
218 zio_cksum_t stream_cksum;
219 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
223 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
224 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
226 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
229 * numbuckets must be a power of 2. Increase number to
230 * a power of 2 if necessary.
232 if (!ISP2(numbuckets))
233 numbuckets = 1 << high_order_bit(numbuckets);
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;
242 /* Initialize the write-by-reference block. */
243 wbr_drr.drr_type = DRR_WRITE_BYREF;
244 wbr_drr.drr_payloadlen = 0;
246 outfd = dda->outputfd;
247 ofp = fdopen(dda->inputfd, "r");
248 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
250 switch (drr->drr_type) {
254 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
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);
262 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
263 &stream_cksum, outfd) == -1)
265 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
266 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
267 int sz = drr->drr_payloadlen;
273 (void) ssread(buf, sz, ofp);
276 if (cksum_and_write(buf, sz, &stream_cksum,
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)
297 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
298 &stream_cksum, outfd) == -1)
300 if (drro->drr_bonuslen > 0) {
302 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
304 if (cksum_and_write(buf,
305 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
306 &stream_cksum, outfd) == -1)
314 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
315 &stream_cksum, outfd) == -1)
317 (void) ssread(buf, drrs->drr_length, ofp);
318 if (cksum_and_write(buf, drrs->drr_length,
319 &stream_cksum, outfd) == -1)
324 case DRR_FREEOBJECTS:
326 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
327 &stream_cksum, outfd) == -1)
336 (void) ssread(buf, drrw->drr_length, ofp);
339 * Use the existing checksum if it's dedup-capable,
340 * else calculate a SHA256 checksum for it.
343 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
345 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
346 zio_cksum_t tmpsha256;
348 zio_checksum_SHA256(buf,
349 drrw->drr_length, &tmpsha256);
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;
363 dataref.ref_guid = drrw->drr_toguid;
364 dataref.ref_object = drrw->drr_object;
365 dataref.ref_offset = drrw->drr_offset;
367 if (ddt_update(dda->dedup_hdl, &ddt,
368 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
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 =
378 wbr_drrr->drr_refoffset =
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;
390 if (cksum_and_write(&wbr_drr,
391 sizeof (dmu_replay_record_t), &stream_cksum,
395 /* block not previously seen */
396 if (cksum_and_write(drr,
397 sizeof (dmu_replay_record_t), &stream_cksum,
400 if (cksum_and_write(buf,
402 &stream_cksum, outfd) == -1)
410 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
411 &stream_cksum, outfd) == -1)
417 (void) printf("INVALID record type 0x%x\n",
419 /* should never happen, so assert */
424 umem_cache_destroy(ddt.ddecache);
425 free(ddt.dedup_hash_array);
433 * Routines for dealing with the AVL tree of fs-nvlists
435 typedef struct fsavl_node {
443 fsavl_compare(const void *arg1, const void *arg2)
445 const fsavl_node_t *fn1 = arg1;
446 const fsavl_node_t *fn2 = arg2;
448 if (fn1->fn_guid > fn2->fn_guid)
450 else if (fn1->fn_guid < fn2->fn_guid)
457 * Given the GUID of a snapshot, find its containing filesystem and
461 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
463 fsavl_node_t fn_find;
466 fn_find.fn_guid = snapguid;
468 fn = avl_find(avl, &fn_find, NULL);
471 *snapname = fn->fn_snapname;
472 return (fn->fn_nvfs);
478 fsavl_destroy(avl_tree_t *avl)
487 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
494 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
497 fsavl_create(nvlist_t *fss)
500 nvpair_t *fselem = NULL;
502 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
505 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
506 offsetof(fsavl_node_t, fn_node));
508 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
509 nvlist_t *nvfs, *snaps;
510 nvpair_t *snapelem = NULL;
512 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
513 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
516 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
520 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
521 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
522 fsavl_destroy(fsavl);
526 fn->fn_snapname = nvpair_name(snapelem);
530 * Note: if there are multiple snaps with the
531 * same GUID, we ignore all but one.
533 if (avl_find(fsavl, fn, NULL) == NULL)
544 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
546 typedef struct send_data {
547 uint64_t parent_fromsnap_guid;
548 nvlist_t *parent_snaps;
551 const char *fromsnap;
556 * The header nvlist is of the following format:
559 * "fromsnap" -> string (if incremental)
563 * "name" -> string (full name; for debugging)
564 * "parentfromsnap" -> number (guid of fromsnap in parent)
566 * "props" -> { name -> value (only if set here) }
567 * "snaps" -> { name (lastname) -> number (guid) }
568 * "snapprops" -> { name (lastname) -> { name -> value } }
570 * "origin" -> number (guid) (if clone)
571 * "sent" -> boolean (not on-disk)
579 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
582 send_iterate_snap(zfs_handle_t *zhp, void *arg)
584 send_data_t *sd = arg;
585 uint64_t guid = zhp->zfs_dmustats.dds_guid;
589 snapname = strrchr(zhp->zfs_name, '@')+1;
591 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
593 * NB: if there is no fromsnap here (it's a newly created fs in
594 * an incremental replication), we will substitute the tosnap.
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;
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));
612 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
614 nvpair_t *elem = NULL;
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);
621 if (!zfs_prop_user(propname)) {
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.
630 if (prop == ZPROP_INVAL)
633 if (zfs_prop_readonly(prop))
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) {
643 verify(nvlist_lookup_uint64(propnv,
644 ZPROP_VALUE, &value) == 0);
645 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
648 * May have no source before SPA_VERSION_RECVD_PROPS,
649 * but is still modifiable.
651 if (nvlist_lookup_string(propnv,
652 ZPROP_SOURCE, &source) == 0) {
653 if ((strcmp(source, zhp->zfs_name) != 0) &&
655 ZPROP_SOURCE_VAL_RECVD) != 0))
660 if (nvlist_lookup_string(propnv,
661 ZPROP_SOURCE, &source) != 0)
663 if ((strcmp(source, zhp->zfs_name) != 0) &&
664 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
668 if (zfs_prop_user(propname) ||
669 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
671 verify(nvlist_lookup_string(propnv,
672 ZPROP_VALUE, &value) == 0);
673 VERIFY(0 == nvlist_add_string(nv, propname, value));
676 verify(nvlist_lookup_uint64(propnv,
677 ZPROP_VALUE, &value) == 0);
678 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
684 * recursively generate nvlists describing datasets. See comment
685 * for the data structure send_data_t above for description of contents
689 send_iterate_fs(zfs_handle_t *zhp, void *arg)
691 send_data_t *sd = arg;
694 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
695 uint64_t guid = zhp->zfs_dmustats.dds_guid;
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));
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);
708 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
709 origin->zfs_dmustats.dds_guid));
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));
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);
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));
734 /* iterate over children */
736 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
738 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
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)
749 send_data_t sd = { 0 };
752 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
754 return (EZFS_BADTYPE);
756 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
757 sd.fromsnap = fromsnap;
759 sd.recursive = recursive;
761 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
769 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
780 * Routines for dealing with the sorted snapshot functionality
782 typedef struct zfs_node {
783 zfs_handle_t *zn_handle;
784 avl_node_t zn_avlnode;
788 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
790 avl_tree_t *avl = data;
794 search.zn_handle = zhp;
795 node = avl_find(avl, &search, NULL);
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
803 zfs_close(node->zn_handle);
804 avl_remove(avl, node);
808 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
809 node->zn_handle = zhp;
816 zfs_snapshot_compare(const void *larg, const void *rarg)
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;
823 * Sort them according to creation time. We use the hidden
824 * CREATETXG property to get an absolute ordering of snapshots.
826 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
827 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
829 if (lcreate < rcreate)
831 else if (lcreate > rcreate)
838 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
845 avl_create(&avl, zfs_snapshot_compare,
846 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
848 ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
850 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
851 ret |= callback(node->zn_handle, data);
853 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
862 * Routines specific to "zfs send"
864 typedef struct send_dump_data {
865 /* these are all just the short snapname (the part after the @) */
866 const char *fromsnap;
868 char prevsnap[ZFS_MAXNAMELEN];
869 uint64_t prevsnap_obj;
870 boolean_t seenfrom, seento, replicate, doall, fromorigin;
876 snapfilter_cb_t *filter_cb;
879 char holdtag[ZFS_MAXNAMELEN];
884 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
885 * NULL) to the file descriptor specified by outfd.
888 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
889 boolean_t fromorigin, int outfd, nvlist_t *debugnv)
891 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
892 libzfs_handle_t *hdl = zhp->zfs_hdl;
895 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
896 assert(fromsnap_obj == 0 || !fromorigin);
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;
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));
910 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
912 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
913 "warning: cannot send '%s'"), zhp->zfs_name);
915 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
917 VERIFY(0 == nvlist_add_nvlist(debugnv,
918 zhp->zfs_name, thisdbg));
920 nvlist_free(thisdbg);
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));
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"),
936 return (zfs_error(hdl, EZFS_NOENT, errbuf));
949 zfs_error_aux(hdl, strerror(errno));
950 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
953 return (zfs_standard_error(hdl, errno, errbuf));
958 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
959 nvlist_free(thisdbg);
965 hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
971 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
974 * zfs_send() only opens a cleanup_fd for sends that need it,
975 * e.g. replication and doall.
977 if (sdd->cleanup_fd == -1)
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) = '@';
986 * It's OK if the parent no longer exists. The send code will
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));
1001 dump_snapshot(zfs_handle_t *zhp, void *arg)
1003 send_dump_data_t *sdd = arg;
1006 boolean_t isfromsnap, istosnap;
1007 boolean_t exclude = B_FALSE;
1009 thissnap = strchr(zhp->zfs_name, '@') + 1;
1010 isfromsnap = (sdd->fromsnap != NULL &&
1011 strcmp(sdd->fromsnap, thissnap) == 0);
1013 if (!sdd->seenfrom && isfromsnap) {
1014 err = hold_for_send(zhp, sdd);
1016 sdd->seenfrom = B_TRUE;
1017 (void) strcpy(sdd->prevsnap, thissnap);
1018 sdd->prevsnap_obj = zfs_prop_get_int(zhp,
1020 } else if (err == ENOENT) {
1027 if (sdd->seento || !sdd->seenfrom) {
1032 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1034 sdd->seento = B_TRUE;
1036 if (!sdd->doall && !isfromsnap && !istosnap) {
1037 if (sdd->replicate) {
1039 nvlist_t *snapprops;
1041 * Filter out all intermediate snapshots except origin
1042 * snapshots needed to replicate clones.
1044 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1045 zhp->zfs_dmustats.dds_guid, &snapname);
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");
1058 * If a filter function exists, call it to determine whether
1059 * this snapshot will be sent.
1061 if (exclude || (sdd->filter_cb != NULL &&
1062 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
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.
1075 err = hold_for_send(zhp, sdd);
1085 (void) fprintf(stderr, "sending from @%s to %s\n",
1086 sdd->prevsnap, zhp->zfs_name);
1089 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1090 sdd->prevsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
1091 sdd->outfd, sdd->debugnv);
1093 (void) strcpy(sdd->prevsnap, thissnap);
1094 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1100 dump_filesystem(zfs_handle_t *zhp, void *arg)
1103 send_dump_data_t *sdd = arg;
1104 boolean_t missingfrom = B_FALSE;
1105 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
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);
1117 if (sdd->replicate && sdd->fromsnap) {
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.
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;
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;
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);
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);
1155 (void) fprintf(stderr, "WARNING: "
1156 "could not send %s@%s: does not exist\n",
1157 zhp->zfs_name, sdd->tosnap);
1166 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1168 send_dump_data_t *sdd = arg;
1170 boolean_t needagain, progress;
1172 if (!sdd->replicate)
1173 return (dump_filesystem(rzhp, sdd));
1175 /* Mark the clone origin snapshots. */
1176 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1177 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1179 uint64_t origin_guid = 0;
1181 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1182 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1183 if (origin_guid != 0) {
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"));
1199 needagain = progress = B_FALSE;
1200 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1201 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1206 uint64_t origin_guid = 0;
1208 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1209 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1212 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1213 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1215 if (origin_guid != 0) {
1216 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1218 if (origin_nv != NULL &&
1219 nvlist_lookup_boolean(origin_nv,
1220 "sent") == ENOENT) {
1222 * origin has not been sent yet;
1230 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1233 err = dump_filesystem(zhp, sdd);
1234 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1248 * Generate a send stream for the dataset identified by the argument zhp.
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"
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.
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)
1269 send_dump_data_t sdd = { 0 };
1271 nvlist_t *fss = NULL;
1272 avl_tree_t *fsavl = NULL;
1273 static uint64_t holdseq;
1275 boolean_t holdsnaps = B_FALSE;
1278 dedup_arg_t dda = { 0 };
1279 int featureflags = 0;
1281 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1282 "cannot send '%s'"), zhp->zfs_name);
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));
1290 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1292 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1293 if (version >= ZPL_VERSION_SA) {
1294 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1298 if (zfs_spa_version(zhp, &spa_version) == 0 &&
1299 spa_version >= SPA_VERSION_USERREFS &&
1300 (flags.doall || flags.replicate))
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,
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));
1323 if (flags.replicate || flags.doall || flags.props) {
1324 dmu_replay_record_t drr = { 0 };
1325 char *packbuf = NULL;
1327 zio_cksum_t zc = { { 0 } };
1329 if (flags.replicate || flags.props) {
1332 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1334 VERIFY(0 == nvlist_add_string(hdrnv,
1335 "fromsnap", fromsnap));
1337 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1338 if (!flags.replicate) {
1339 VERIFY(0 == nvlist_add_boolean(hdrnv,
1343 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1344 fromsnap, tosnap, flags.replicate, &fss, &fsavl);
1347 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1348 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1355 fsavl_destroy(fsavl);
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,
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);
1374 /* write header nvlist */
1375 if (err != -1 && packbuf != NULL) {
1376 err = cksum_and_write(packbuf, buflen, &zc, outfd);
1380 fsavl_destroy(fsavl);
1386 /* write end record */
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));
1393 fsavl_destroy(fsavl);
1401 /* dump each stream */
1402 sdd.fromsnap = fromsnap;
1403 sdd.tosnap = tosnap;
1405 sdd.outfd = pipefd[0];
1408 sdd.replicate = flags.replicate;
1409 sdd.doall = flags.doall;
1410 sdd.fromorigin = flags.fromorigin;
1413 sdd.verbose = flags.verbose;
1414 sdd.filter_cb = filter_func;
1415 sdd.filter_cb_arg = cb_arg;
1417 sdd.debugnv = *debugnvp;
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) {
1428 sdd.cleanup_fd = -1;
1430 err = dump_filesystems(zhp, &sdd);
1431 fsavl_destroy(fsavl);
1435 (void) close(pipefd[0]);
1436 (void) pthread_join(tid, NULL);
1439 if (sdd.cleanup_fd != -1) {
1440 VERIFY(0 == close(sdd.cleanup_fd));
1441 sdd.cleanup_fd = -1;
1444 if (flags.replicate || flags.doall || flags.props) {
1446 * write final end record. NB: want to do this even if
1447 * there was some error, because it might not be totally
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,
1458 return (err || sdd.err);
1461 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1463 if (sdd.cleanup_fd != -1)
1464 VERIFY(0 == close(sdd.cleanup_fd));
1466 (void) pthread_cancel(tid);
1467 (void) pthread_join(tid, NULL);
1468 (void) close(pipefd[0]);
1474 * Routines specific to "zfs recv"
1478 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1479 boolean_t byteswap, zio_cksum_t *zc)
1486 rv = read(fd, cp, len);
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")));
1500 fletcher_4_incremental_byteswap(buf, ilen, zc);
1502 fletcher_4_incremental_native(buf, ilen, zc);
1508 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1509 boolean_t byteswap, zio_cksum_t *zc)
1514 buf = zfs_alloc(hdl, len);
1518 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1524 err = nvlist_unpack(buf, len, nvp, 0);
1527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1528 "stream (malformed nvlist)"));
1535 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1536 int baselen, char *newname, recvflags_t flags)
1539 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1541 prop_changelist_t *clp;
1544 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1547 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1548 flags.force ? MS_FORCE : 0);
1552 err = changelist_prefix(clp);
1556 zc.zc_objset_type = DMU_OST_ZFS;
1557 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1560 (void) strcpy(newname, tryname);
1562 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1564 if (flags.verbose) {
1565 (void) printf("attempting rename %s to %s\n",
1566 zc.zc_name, zc.zc_value);
1568 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1570 changelist_rename(clp, name, tryname);
1575 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
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));
1583 if (flags.verbose) {
1584 (void) printf("failed - trying rename %s to %s\n",
1585 zc.zc_name, zc.zc_value);
1587 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
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);
1595 } else if (flags.verbose) {
1597 (void) printf("success\n");
1599 (void) printf("failed (%u)\n", errno);
1602 (void) changelist_postfix(clp);
1603 changelist_free(clp);
1609 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1610 char *newname, recvflags_t flags)
1612 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1614 prop_changelist_t *clp;
1616 boolean_t defer = B_FALSE;
1619 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
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)
1631 err = changelist_prefix(clp);
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));
1640 (void) printf("attempting destroy %s\n", zc.zc_name);
1641 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1644 (void) printf("success\n");
1645 changelist_remove(clp, zc.zc_name);
1648 (void) changelist_postfix(clp);
1649 changelist_free(clp);
1652 * Deferred destroy might destroy the snapshot or only mark it to be
1653 * destroyed later, and it returns success in either case.
1655 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1656 ZFS_TYPE_SNAPSHOT))) {
1657 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1663 typedef struct guid_to_name_data {
1666 } guid_to_name_data_t;
1669 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1671 guid_to_name_data_t *gtnd = arg;
1674 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1675 (void) strcpy(gtnd->name, zhp->zfs_name);
1679 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1685 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1688 /* exhaustive search all local snapshots */
1689 guid_to_name_data_t gtnd;
1697 if (strchr(parent, '@') == NULL) {
1698 zhp = make_dataset_handle(hdl, parent);
1700 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1707 cp = strchr(parent, '/');
1710 zhp = make_dataset_handle(hdl, parent);
1715 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1719 return (err == EEXIST ? 0 : ENOENT);
1724 * Return true if dataset guid1 is created before guid2.
1727 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1728 uint64_t guid1, uint64_t guid2)
1731 char *fsname, *snapname;
1732 char buf[ZFS_MAXNAMELEN];
1734 zfs_node_t zn1, zn2;
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)
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);
1757 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1759 zfs_close(zn1.zn_handle);
1760 zfs_close(zn2.zn_handle);
1766 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1767 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
1771 avl_tree_t *local_avl;
1772 nvpair_t *fselem, *nextfselem;
1774 char newname[ZFS_MAXNAMELEN];
1776 boolean_t needagain, progress, recursive;
1779 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1781 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
1788 needagain = progress = B_FALSE;
1790 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1791 recursive, &local_nv, &local_avl)) != 0)
1795 * Process deletes and renames
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;
1808 nextfselem = nvlist_next_nvpair(local_nv, fselem);
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);
1818 * First find the stream's fs, so we can check for
1819 * a different origin (due to "zfs promote")
1821 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1822 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1825 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1826 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1828 if (stream_nvfs != NULL)
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)) {
1840 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1841 nvlist_t *origin_nvfs;
1842 char *origin_fsname;
1845 (void) printf("promoting %s\n", fsname);
1847 origin_nvfs = fsavl_find(local_avl, originguid,
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);
1863 fsavl_destroy(local_avl);
1864 nvlist_free(local_nv);
1868 * We had/have the wrong origin, therefore our
1869 * list of snapshots is wrong. Need to handle
1870 * them on the next pass.
1876 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1877 snapelem; snapelem = nextsnapelem) {
1879 char *stream_snapname;
1880 nvlist_t *found, *props;
1882 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1884 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1885 found = fsavl_find(stream_avl, thisguid,
1888 /* check for delete */
1889 if (found == NULL) {
1890 char name[ZFS_MAXNAMELEN];
1895 (void) snprintf(name, sizeof (name), "%s@%s",
1896 fsname, nvpair_name(snapelem));
1898 error = recv_destroy(hdl, name,
1899 strlen(fsname)+1, newname, flags);
1907 stream_nvfs = found;
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 };
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,
1919 (void) zfs_ioctl(hdl,
1920 ZFS_IOC_SET_PROP, &zc);
1921 zcmd_free_nvlists(&zc);
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];
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);
1936 error = recv_rename(hdl, name, tryname,
1937 strlen(fsname)+1, newname, flags);
1944 if (strcmp(stream_snapname, fromsnap) == 0)
1945 fromguid = thisguid;
1948 /* check for delete */
1949 if (stream_nvfs == NULL) {
1953 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
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",
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));
1977 s1 = strrchr(fsname, '/');
1978 s2 = strrchr(stream_fsname, '/');
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.
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)) {
1991 char tryname[ZFS_MAXNAMELEN];
1993 parent = fsavl_find(local_avl,
1994 stream_parent_fromsnap_guid, NULL);
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
2002 if (parent != NULL) {
2005 VERIFY(0 == nvlist_lookup_string(parent, "name",
2007 (void) snprintf(tryname, sizeof (tryname),
2008 "%s%s", pname, strrchr(stream_fsname, '/'));
2011 if (flags.verbose) {
2012 (void) printf("local fs %s new parent "
2013 "not found\n", fsname);
2019 error = recv_rename(hdl, fsname, tryname,
2020 strlen(tofs)+1, newname, flags);
2022 if (renamed != NULL && newname[0] != '\0') {
2023 VERIFY(0 == nvlist_add_boolean(renamed,
2034 fsavl_destroy(local_avl);
2035 nvlist_free(local_nv);
2037 if (needagain && progress) {
2038 /* do another pass to fix up temporary names */
2040 (void) printf("another pass:\n");
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)
2052 nvlist_t *stream_nv = NULL;
2053 avl_tree_t *stream_avl = NULL;
2054 char *fromsnap = NULL;
2056 char tofs[ZFS_MAXNAMELEN];
2057 char sendfs[ZFS_MAXNAMELEN];
2059 dmu_replay_record_t drre;
2061 boolean_t anyerr = B_FALSE;
2062 boolean_t softerr = B_FALSE;
2063 boolean_t recursive;
2065 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
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);
2074 * Read in the nvlist from the stream.
2076 if (drr->drr_payloadlen != 0) {
2077 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2078 &stream_nv, flags.byteswap, zc);
2080 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2085 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
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);
2096 * Read in the end record and verify checksum.
2098 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2099 flags.byteswap, NULL)))
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]);
2112 if (drre.drr_type != DRR_END) {
2113 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
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);
2123 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2125 if (drr->drr_payloadlen != 0) {
2126 nvlist_t *stream_fss;
2128 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "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);
2137 if (fromsnap != NULL) {
2138 nvlist_t *renamed = NULL;
2139 nvpair_t *pair = NULL;
2141 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2142 if (flags.isprefix) {
2143 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2147 cp = strrchr(drrb->drr_toname, '/');
2149 (void) strlcat(tofs, "/",
2153 i = (cp - drrb->drr_toname);
2156 i = strcspn(drrb->drr_toname, "/@");
2158 /* zfs_receive_one() will create_parents() */
2159 (void) strlcat(tofs, &drrb->drr_toname[i],
2161 *strchr(tofs, '@') = '\0';
2164 if (recursive && !flags.dryrun && !flags.nomount) {
2165 VERIFY(0 == nvlist_alloc(&renamed,
2166 NV_UNIQUE_NAME, 0));
2169 softerr = recv_incremental_replication(hdl, tofs, flags,
2170 stream_nv, stream_avl, renamed);
2172 /* Unmount renamed filesystems before receiving. */
2173 while ((pair = nvlist_next_nvpair(renamed,
2176 prop_changelist_t *clp = NULL;
2178 zhp = zfs_open(hdl, nvpair_name(pair),
2179 ZFS_TYPE_FILESYSTEM);
2181 clp = changelist_gather(zhp,
2182 ZFS_PROP_MOUNTPOINT, 0, 0);
2186 changelist_prefix(clp);
2187 changelist_free(clp);
2192 nvlist_free(renamed);
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().
2201 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2203 if ((cp = strchr(sendfs, '@')) != NULL)
2206 /* Finally, receive each contained stream */
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).
2215 error = zfs_receive_impl(hdl, destname, flags, fd,
2216 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2218 if (error == ENODATA) {
2223 } while (error == 0);
2225 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2227 * Now that we have the fs's they sent us, try the
2230 softerr = recv_incremental_replication(hdl, tofs, flags,
2231 stream_nv, stream_avl, NULL);
2235 fsavl_destroy(stream_avl);
2237 nvlist_free(stream_nv);
2246 trunc_prop_errs(int truncated)
2248 ASSERT(truncated != 0);
2251 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2252 "1 more property could not be set\n"));
2254 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2255 "%d more properties could not be set\n"), truncated);
2259 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2261 dmu_replay_record_t *drr;
2262 void *buf = malloc(1<<20);
2265 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2266 "cannot receive:"));
2268 /* XXX would be great to use lseek if possible... */
2271 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2272 byteswap, NULL) == 0) {
2274 drr->drr_type = BSWAP_32(drr->drr_type);
2276 switch (drr->drr_type) {
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));
2292 drr->drr_u.drr_object.drr_bonuslen =
2293 BSWAP_32(drr->drr_u.drr_object.
2296 (void) recv_read(hdl, fd, buf,
2297 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2303 drr->drr_u.drr_write.drr_length =
2304 BSWAP_64(drr->drr_u.drr_write.drr_length);
2306 (void) recv_read(hdl, fd, buf,
2307 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2311 drr->drr_u.drr_write.drr_length =
2312 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2314 (void) recv_read(hdl, fd, buf,
2315 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2317 case DRR_WRITE_BYREF:
2318 case DRR_FREEOBJECTS:
2323 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2324 "invalid record type"));
2325 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2334 * Restores a backup of tosnap from the file descriptor specified by infd.
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)
2343 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2345 int ioctl_err, ioctl_errno, err;
2347 struct drr_begin *drrb = &drr->drr_u.drr_begin;
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;
2359 begin_time = time(NULL);
2361 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2364 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2367 if (stream_avl != NULL) {
2369 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2374 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2376 err = nvlist_lookup_nvlist(fs, "props", &props);
2378 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2380 if (flags.canmountoff) {
2381 VERIFY(0 == nvlist_add_uint64(props,
2382 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2384 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2388 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2389 VERIFY(0 == nvlist_lookup_nvlist(props,
2390 snapname, &snapprops_nvlist));
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.
2404 * If they specified a snapshot, chop the entire name stored in
2409 * A filesystem was specified with -e. We want to tack on only
2410 * the tail of the sent snapshot path.
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));
2418 chopprefix = strrchr(sendfs, '/');
2420 if (chopprefix == NULL) {
2422 * The tail is the poolname, so we need to
2423 * prepend a path separator.
2425 int len = strlen(drrb->drr_toname);
2426 cp = malloc(len + 2);
2428 (void) strcpy(&cp[1], drrb->drr_toname);
2431 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2433 } else if (flags.isprefix) {
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).
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));
2445 chopprefix = strchr(drrb->drr_toname, '/');
2446 if (chopprefix == NULL)
2447 chopprefix = strchr(drrb->drr_toname, '@');
2448 } else if (strchr(tosnap, '@') == NULL) {
2450 * If a filesystem was specified without -d or -e, we want to
2451 * tack on everything after the fs specified by 'zfs send'.
2453 chopprefix = drrb->drr_toname + strlen(sendfs);
2455 /* A snapshot was specified as an exact path (no -d or -e). */
2457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2458 "cannot specify snapshot name for multi-snapshot "
2460 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2462 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
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');
2472 * Determine name of destination snapshot, store in zc_value.
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));
2478 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2479 zcmd_free_nvlists(&zc);
2480 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2484 * Determine the name of the origin snapshot, store in zc_string.
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"),
2493 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2496 (void) printf("found clone origin %s\n", zc.zc_string);
2499 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2500 (drrb->drr_flags & DRR_FLAG_CLONE));
2502 if (stream_wantsnewfs) {
2504 * if the parent fs does not exist, look for it based on
2505 * the parent snap GUID
2507 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2508 "cannot receive new filesystem stream"));
2510 (void) strcpy(zc.zc_name, zc.zc_value);
2511 cp = strrchr(zc.zc_name, '/');
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);
2526 * if the fs does not exist, look for it based on the
2529 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2530 "cannot receive incremental stream"));
2532 (void) strcpy(zc.zc_name, zc.zc_value);
2533 *strchr(zc.zc_name, '@') = '\0';
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.
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);
2553 (void) strcpy(zc.zc_name, zc.zc_value);
2554 *strchr(zc.zc_name, '@') = '\0';
2556 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
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
2566 if (stream_wantsnewfs) {
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"),
2573 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2575 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
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"),
2582 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2586 if ((zhp = zfs_open(hdl, zc.zc_name,
2587 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2588 zcmd_free_nvlists(&zc);
2592 if (stream_wantsnewfs &&
2593 zhp->zfs_dmustats.dds_origin[0]) {
2594 zcmd_free_nvlists(&zc);
2596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2597 "destination '%s' is a clone\n"
2598 "must destroy it to overwrite it"),
2600 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
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);
2609 zcmd_free_nvlists(&zc);
2612 if (changelist_prefix(clp) != 0) {
2613 changelist_free(clp);
2615 zcmd_free_nvlists(&zc);
2619 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
2620 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
2622 zcmd_free_nvlists(&zc);
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).
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));
2643 * Trim off the final dataset component so we perform the
2644 * recvbackup ioctl to the filesystems's parent.
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));
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);
2669 zcmd_free_nvlists(&zc);
2670 return (recv_skip(hdl, infd, flags.byteswap));
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;
2678 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2679 ioctl_errno = errno;
2680 prop_errflags = (zprop_errflags_t)zc.zc_obj;
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));
2687 nvpair_t *prop_err = NULL;
2689 while ((prop_err = nvlist_next_nvpair(prop_errors,
2690 prop_err)) != NULL) {
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);
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);
2709 nvlist_free(prop_errors);
2712 zc.zc_nvlist_dst = 0;
2713 zc.zc_nvlist_dst_size = 0;
2714 zcmd_free_nvlists(&zc);
2716 if (err == 0 && snapprops_nvlist) {
2717 zfs_cmd_t zc2 = { "\0", "\0", "\0", "\0", 0 };
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);
2727 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
2729 * It may be that this snapshot already exists,
2730 * in which case we want to consume & ignore it
2731 * rather than failing.
2733 avl_tree_t *local_avl;
2734 nvlist_t *local_nv, *fs;
2735 cp = strchr(zc.zc_value, '@');
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.
2743 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
2744 &local_nv, &local_avl) == 0) {
2746 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
2747 fsavl_destroy(local_avl);
2748 nvlist_free(local_nv);
2751 if (flags.verbose) {
2752 (void) printf("snap %s already exists; "
2753 "ignoring\n", zc.zc_value);
2755 err = ioctl_err = recv_skip(hdl, infd,
2762 if (ioctl_err != 0) {
2763 switch (ioctl_errno) {
2765 cp = strchr(zc.zc_value, '@');
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);
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);
2780 cp = strchr(zc.zc_value, '@');
2782 /* it's the containing fs that exists */
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"),
2793 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2797 "invalid stream (checksum mismatch)"));
2798 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
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);
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);
2811 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
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).
2820 cp = strchr(zc.zc_value, '@');
2821 if (cp && (ioctl_err == 0 || !newfs)) {
2825 h = zfs_open(hdl, zc.zc_value,
2826 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2828 if (h->zfs_type == ZFS_TYPE_VOLUME) {
2830 err = zvol_create_link(hdl, h->zfs_name);
2831 if (err == 0 && ioctl_err == 0)
2832 err = zvol_create_link(hdl,
2834 } else if (newfs || stream_avl) {
2836 * Track the first/top of hierarchy fs,
2837 * for mounting and sharing later.
2839 if (top_zfs && *top_zfs == NULL)
2840 *top_zfs = zfs_strdup(hdl, zc.zc_value);
2848 err |= changelist_postfix(clp);
2849 changelist_free(clp);
2852 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
2853 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2854 "failed to clear unreceived properties on %s"),
2856 (void) fprintf(stderr, "\n");
2858 if (prop_errflags & ZPROP_ERR_NORESTORE) {
2859 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2860 "failed to restore original properties on %s"),
2862 (void) fprintf(stderr, "\n");
2865 if (err || ioctl_err)
2868 *action_handlep = zc.zc_action_handle;
2870 if (flags.verbose) {
2873 uint64_t bytes = zc.zc_cookie;
2874 time_t delta = time(NULL) - begin_time;
2877 zfs_nicenum(bytes, buf1, sizeof (buf1));
2878 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
2880 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
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)
2893 dmu_replay_record_t drr, drr_noswap;
2894 struct drr_begin *drrb = &drr.drr_u.drr_begin;
2896 zio_cksum_t zcksum = { { 0 } };
2897 uint64_t featureflags;
2900 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
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));
2910 /* read in the BEGIN record */
2911 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
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 */
2920 /* the kernel needs the non-byteswapped begin record */
2923 flags.byteswap = B_FALSE;
2924 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2926 * We computed the checksum in the wrong byteorder in
2927 * recv_read() above; do it again correctly.
2929 bzero(&zcksum, sizeof (zio_cksum_t));
2930 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2931 flags.byteswap = B_TRUE;
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);
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));
2950 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
2951 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
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"),
2958 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
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));
2967 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
2968 char nonpackage_sendfs[ZFS_MAXNAMELEN];
2969 if (sendfs == NULL) {
2971 * We were not called from zfs_receive_package(). Get
2972 * the fs specified by 'zfs send'.
2975 (void) strlcpy(nonpackage_sendfs,
2976 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
2977 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
2979 sendfs = nonpackage_sendfs;
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));
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));
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).
2999 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
3000 int infd, avl_tree_t *stream_avl)
3002 char *top_zfs = NULL;
3005 uint64_t action_handle = 0;
3007 cleanup_fd = open(ZFS_DEV, O_RDWR);
3008 VERIFY(cleanup_fd >= 0);
3010 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
3011 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3013 VERIFY(0 == close(cleanup_fd));
3015 if (err == 0 && !flags.nomount && top_zfs) {
3017 prop_changelist_t *clp;
3019 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3021 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3022 CL_GATHER_MOUNT_ALWAYS, 0);
3025 /* mount and share received datasets */
3026 err = changelist_postfix(clp);
3027 changelist_free(clp);
3030 if (zhp == NULL || clp == NULL || err)