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.
36 #include <sys/mount.h>
37 #include <sys/mntent.h>
38 #include <sys/mnttab.h>
40 #include <sys/debug.h>
47 #include "zfs_namecheck.h"
49 #include "zfs_fletcher.h"
50 #include "libzfs_impl.h"
51 #include <sys/zio_checksum.h>
53 #include <sys/socket.h>
55 /* in libzfs_dataset.c */
56 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
58 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
59 int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
61 static const zio_cksum_t zero_cksum = { { 0 } };
63 typedef struct dedup_arg {
66 libzfs_handle_t *dedup_hdl;
69 typedef struct dataref {
75 typedef struct dedup_entry {
76 struct dedup_entry *dde_next;
77 zio_cksum_t dde_chksum;
82 #define MAX_DDT_PHYSMEM_PERCENT 20
83 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
85 typedef struct dedup_table {
86 dedup_entry_t **dedup_hash_array;
87 umem_cache_t *ddecache;
88 uint64_t max_ddt_size; /* max dedup table size in bytes */
89 uint64_t cur_ddt_size; /* current dedup table size in bytes */
96 high_order_bit(uint64_t n)
100 for (count = 0; n != 0; count++)
106 ssread(void *buf, size_t len, FILE *stream)
110 if ((outlen = fread(buf, len, 1, stream)) == 0)
117 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
118 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
122 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
123 if (ddt->ddt_full == B_FALSE) {
124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
125 "Dedup table full. Deduplication will continue "
126 "with existing table entries"));
127 ddt->ddt_full = B_TRUE;
132 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
134 assert(*ddepp == NULL);
135 dde->dde_next = NULL;
136 dde->dde_chksum = *cs;
137 dde->dde_prop = prop;
140 ddt->cur_ddt_size += sizeof (dedup_entry_t);
146 * Using the specified dedup table, do a lookup for an entry with
147 * the checksum cs. If found, return the block's reference info
148 * in *dr. Otherwise, insert a new entry in the dedup table, using
149 * the reference information specified by *dr.
151 * return value: true - entry was found
152 * false - entry was not found
155 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
156 uint64_t prop, dataref_t *dr)
159 dedup_entry_t **ddepp;
161 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
163 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
164 ddepp = &((*ddepp)->dde_next)) {
165 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
166 (*ddepp)->dde_prop == prop) {
167 *dr = (*ddepp)->dde_ref;
171 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
176 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
178 fletcher_4_incremental_native(buf, len, zc);
179 return (write(outfd, buf, len));
183 * This function is started in a separate thread when the dedup option
184 * has been requested. The main send thread determines the list of
185 * snapshots to be included in the send stream and makes the ioctl calls
186 * for each one. But instead of having the ioctl send the output to the
187 * the output fd specified by the caller of zfs_send()), the
188 * ioctl is told to direct the output to a pipe, which is read by the
189 * alternate thread running THIS function. This function does the
191 * 1. building a dedup table (the DDT)
192 * 2. doing checksums on each data block and inserting a record in the DDT
193 * 3. looking for matching checksums, and
194 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
195 * a duplicate block is found.
196 * The output of this function then goes to the output fd requested
197 * by the caller of zfs_send().
202 dedup_arg_t *dda = arg;
203 char *buf = malloc(1<<20);
204 dmu_replay_record_t thedrr;
205 dmu_replay_record_t *drr = &thedrr;
206 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
207 struct drr_end *drre = &thedrr.drr_u.drr_end;
208 struct drr_object *drro = &thedrr.drr_u.drr_object;
209 struct drr_write *drrw = &thedrr.drr_u.drr_write;
210 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
213 dmu_replay_record_t wbr_drr = {0};
214 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
216 zio_cksum_t stream_cksum;
217 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
221 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
222 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
224 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
227 * numbuckets must be a power of 2. Increase number to
228 * a power of 2 if necessary.
230 if (!ISP2(numbuckets))
231 numbuckets = 1 << high_order_bit(numbuckets);
233 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
234 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
235 NULL, NULL, NULL, NULL, NULL, 0);
236 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
237 ddt.numhashbits = high_order_bit(numbuckets) - 1;
238 ddt.ddt_full = B_FALSE;
240 /* Initialize the write-by-reference block. */
241 wbr_drr.drr_type = DRR_WRITE_BYREF;
242 wbr_drr.drr_payloadlen = 0;
244 outfd = dda->outputfd;
245 ofp = fdopen(dda->inputfd, "r");
246 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
248 switch (drr->drr_type) {
252 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
254 /* set the DEDUP feature flag for this stream */
255 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
256 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
257 DMU_BACKUP_FEATURE_DEDUPPROPS);
258 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
260 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
261 &stream_cksum, outfd) == -1)
263 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
264 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
265 int sz = drr->drr_payloadlen;
271 (void) ssread(buf, sz, ofp);
274 if (cksum_and_write(buf, sz, &stream_cksum,
283 /* use the recalculated checksum */
284 ZIO_SET_CHECKSUM(&drre->drr_checksum,
285 stream_cksum.zc_word[0], stream_cksum.zc_word[1],
286 stream_cksum.zc_word[2], stream_cksum.zc_word[3]);
287 if ((write(outfd, drr,
288 sizeof (dmu_replay_record_t))) == -1)
295 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
296 &stream_cksum, outfd) == -1)
298 if (drro->drr_bonuslen > 0) {
300 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
302 if (cksum_and_write(buf,
303 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
304 &stream_cksum, outfd) == -1)
312 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
313 &stream_cksum, outfd) == -1)
315 (void) ssread(buf, drrs->drr_length, ofp);
316 if (cksum_and_write(buf, drrs->drr_length,
317 &stream_cksum, outfd) == -1)
322 case DRR_FREEOBJECTS:
324 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
325 &stream_cksum, outfd) == -1)
334 (void) ssread(buf, drrw->drr_length, ofp);
337 * Use the existing checksum if it's dedup-capable,
338 * else calculate a SHA256 checksum for it.
341 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
343 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
344 zio_cksum_t tmpsha256;
346 zio_checksum_SHA256(buf,
347 drrw->drr_length, &tmpsha256);
349 drrw->drr_key.ddk_cksum.zc_word[0] =
350 BE_64(tmpsha256.zc_word[0]);
351 drrw->drr_key.ddk_cksum.zc_word[1] =
352 BE_64(tmpsha256.zc_word[1]);
353 drrw->drr_key.ddk_cksum.zc_word[2] =
354 BE_64(tmpsha256.zc_word[2]);
355 drrw->drr_key.ddk_cksum.zc_word[3] =
356 BE_64(tmpsha256.zc_word[3]);
357 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
358 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
361 dataref.ref_guid = drrw->drr_toguid;
362 dataref.ref_object = drrw->drr_object;
363 dataref.ref_offset = drrw->drr_offset;
365 if (ddt_update(dda->dedup_hdl, &ddt,
366 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
368 /* block already present in stream */
369 wbr_drrr->drr_object = drrw->drr_object;
370 wbr_drrr->drr_offset = drrw->drr_offset;
371 wbr_drrr->drr_length = drrw->drr_length;
372 wbr_drrr->drr_toguid = drrw->drr_toguid;
373 wbr_drrr->drr_refguid = dataref.ref_guid;
374 wbr_drrr->drr_refobject =
376 wbr_drrr->drr_refoffset =
379 wbr_drrr->drr_checksumtype =
380 drrw->drr_checksumtype;
381 wbr_drrr->drr_checksumflags =
382 drrw->drr_checksumtype;
383 wbr_drrr->drr_key.ddk_cksum =
384 drrw->drr_key.ddk_cksum;
385 wbr_drrr->drr_key.ddk_prop =
386 drrw->drr_key.ddk_prop;
388 if (cksum_and_write(&wbr_drr,
389 sizeof (dmu_replay_record_t), &stream_cksum,
393 /* block not previously seen */
394 if (cksum_and_write(drr,
395 sizeof (dmu_replay_record_t), &stream_cksum,
398 if (cksum_and_write(buf,
400 &stream_cksum, outfd) == -1)
408 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
409 &stream_cksum, outfd) == -1)
415 (void) printf("INVALID record type 0x%x\n",
417 /* should never happen, so assert */
422 umem_cache_destroy(ddt.ddecache);
423 free(ddt.dedup_hash_array);
431 * Routines for dealing with the AVL tree of fs-nvlists
433 typedef struct fsavl_node {
441 fsavl_compare(const void *arg1, const void *arg2)
443 const fsavl_node_t *fn1 = arg1;
444 const fsavl_node_t *fn2 = arg2;
446 if (fn1->fn_guid > fn2->fn_guid)
448 else if (fn1->fn_guid < fn2->fn_guid)
455 * Given the GUID of a snapshot, find its containing filesystem and
459 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
461 fsavl_node_t fn_find;
464 fn_find.fn_guid = snapguid;
466 fn = avl_find(avl, &fn_find, NULL);
469 *snapname = fn->fn_snapname;
470 return (fn->fn_nvfs);
476 fsavl_destroy(avl_tree_t *avl)
485 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
492 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
495 fsavl_create(nvlist_t *fss)
498 nvpair_t *fselem = NULL;
500 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
503 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
504 offsetof(fsavl_node_t, fn_node));
506 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
507 nvlist_t *nvfs, *snaps;
508 nvpair_t *snapelem = NULL;
510 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
511 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
514 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
518 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
519 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
520 fsavl_destroy(fsavl);
524 fn->fn_snapname = nvpair_name(snapelem);
528 * Note: if there are multiple snaps with the
529 * same GUID, we ignore all but one.
531 if (avl_find(fsavl, fn, NULL) == NULL)
542 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
544 typedef struct send_data {
545 uint64_t parent_fromsnap_guid;
546 nvlist_t *parent_snaps;
549 const char *fromsnap;
554 * The header nvlist is of the following format:
557 * "fromsnap" -> string (if incremental)
561 * "name" -> string (full name; for debugging)
562 * "parentfromsnap" -> number (guid of fromsnap in parent)
564 * "props" -> { name -> value (only if set here) }
565 * "snaps" -> { name (lastname) -> number (guid) }
566 * "snapprops" -> { name (lastname) -> { name -> value } }
568 * "origin" -> number (guid) (if clone)
569 * "sent" -> boolean (not on-disk)
577 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
580 send_iterate_snap(zfs_handle_t *zhp, void *arg)
582 send_data_t *sd = arg;
583 uint64_t guid = zhp->zfs_dmustats.dds_guid;
587 snapname = strrchr(zhp->zfs_name, '@')+1;
589 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
591 * NB: if there is no fromsnap here (it's a newly created fs in
592 * an incremental replication), we will substitute the tosnap.
594 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
595 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
596 strcmp(snapname, sd->tosnap) == 0)) {
597 sd->parent_fromsnap_guid = guid;
600 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
601 send_iterate_prop(zhp, nv);
602 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
610 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
612 nvpair_t *elem = NULL;
614 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
615 char *propname = nvpair_name(elem);
616 zfs_prop_t prop = zfs_name_to_prop(propname);
619 if (!zfs_prop_user(propname)) {
621 * Realistically, this should never happen. However,
622 * we want the ability to add DSL properties without
623 * needing to make incompatible version changes. We
624 * need to ignore unknown properties to allow older
625 * software to still send datasets containing these
626 * properties, with the unknown properties elided.
628 if (prop == ZPROP_INVAL)
631 if (zfs_prop_readonly(prop))
635 verify(nvpair_value_nvlist(elem, &propnv) == 0);
636 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
637 prop == ZFS_PROP_REFQUOTA ||
638 prop == ZFS_PROP_REFRESERVATION) {
641 verify(nvlist_lookup_uint64(propnv,
642 ZPROP_VALUE, &value) == 0);
643 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
646 * May have no source before SPA_VERSION_RECVD_PROPS,
647 * but is still modifiable.
649 if (nvlist_lookup_string(propnv,
650 ZPROP_SOURCE, &source) == 0) {
651 if ((strcmp(source, zhp->zfs_name) != 0) &&
653 ZPROP_SOURCE_VAL_RECVD) != 0))
658 if (nvlist_lookup_string(propnv,
659 ZPROP_SOURCE, &source) != 0)
661 if ((strcmp(source, zhp->zfs_name) != 0) &&
662 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
666 if (zfs_prop_user(propname) ||
667 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
669 verify(nvlist_lookup_string(propnv,
670 ZPROP_VALUE, &value) == 0);
671 VERIFY(0 == nvlist_add_string(nv, propname, value));
674 verify(nvlist_lookup_uint64(propnv,
675 ZPROP_VALUE, &value) == 0);
676 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
682 * recursively generate nvlists describing datasets. See comment
683 * for the data structure send_data_t above for description of contents
687 send_iterate_fs(zfs_handle_t *zhp, void *arg)
689 send_data_t *sd = arg;
692 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
693 uint64_t guid = zhp->zfs_dmustats.dds_guid;
696 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
697 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
698 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
699 sd->parent_fromsnap_guid));
701 if (zhp->zfs_dmustats.dds_origin[0]) {
702 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
703 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
706 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
707 origin->zfs_dmustats.dds_guid));
710 /* iterate over props */
711 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
712 send_iterate_prop(zhp, nv);
713 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
716 /* iterate over snaps, and set sd->parent_fromsnap_guid */
717 sd->parent_fromsnap_guid = 0;
718 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
719 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
720 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
721 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
722 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
723 nvlist_free(sd->parent_snaps);
724 nvlist_free(sd->snapprops);
726 /* add this fs to nvlist */
727 (void) snprintf(guidstring, sizeof (guidstring),
728 "0x%llx", (longlong_t)guid);
729 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
732 /* iterate over children */
734 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
736 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
743 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
744 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
747 send_data_t sd = { 0 };
750 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
752 return (EZFS_BADTYPE);
754 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
755 sd.fromsnap = fromsnap;
757 sd.recursive = recursive;
759 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
767 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
778 * Routines for dealing with the sorted snapshot functionality
780 typedef struct zfs_node {
781 zfs_handle_t *zn_handle;
782 avl_node_t zn_avlnode;
786 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
788 avl_tree_t *avl = data;
792 search.zn_handle = zhp;
793 node = avl_find(avl, &search, NULL);
796 * If this snapshot was renamed while we were creating the
797 * AVL tree, it's possible that we already inserted it under
798 * its old name. Remove the old handle before adding the new
801 zfs_close(node->zn_handle);
802 avl_remove(avl, node);
806 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
807 node->zn_handle = zhp;
814 zfs_snapshot_compare(const void *larg, const void *rarg)
816 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
817 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
818 uint64_t lcreate, rcreate;
821 * Sort them according to creation time. We use the hidden
822 * CREATETXG property to get an absolute ordering of snapshots.
824 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
825 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
827 if (lcreate < rcreate)
829 else if (lcreate > rcreate)
836 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
843 avl_create(&avl, zfs_snapshot_compare,
844 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
846 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
848 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
849 ret |= callback(node->zn_handle, data);
851 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
860 * Routines specific to "zfs send"
862 typedef struct send_dump_data {
863 /* these are all just the short snapname (the part after the @) */
864 const char *fromsnap;
866 char prevsnap[ZFS_MAXNAMELEN];
867 uint64_t prevsnap_obj;
868 boolean_t seenfrom, seento, replicate, doall, fromorigin;
874 snapfilter_cb_t *filter_cb;
877 char holdtag[ZFS_MAXNAMELEN];
882 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
883 * NULL) to the file descriptor specified by outfd.
886 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
887 boolean_t fromorigin, int outfd, nvlist_t *debugnv)
889 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
890 libzfs_handle_t *hdl = zhp->zfs_hdl;
893 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
894 assert(fromsnap_obj == 0 || !fromorigin);
896 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
897 zc.zc_cookie = outfd;
898 zc.zc_obj = fromorigin;
899 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
900 zc.zc_fromobj = fromsnap_obj;
902 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
903 if (fromsnap && fromsnap[0] != '\0') {
904 VERIFY(0 == nvlist_add_string(thisdbg,
905 "fromsnap", fromsnap));
908 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
910 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
911 "warning: cannot send '%s'"), zhp->zfs_name);
913 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
915 VERIFY(0 == nvlist_add_nvlist(debugnv,
916 zhp->zfs_name, thisdbg));
918 nvlist_free(thisdbg);
923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
924 "not an earlier snapshot from the same fs"));
925 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
928 if (zfs_dataset_exists(hdl, zc.zc_name,
929 ZFS_TYPE_SNAPSHOT)) {
930 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
931 "incremental source (@%s) does not exist"),
934 return (zfs_error(hdl, EZFS_NOENT, errbuf));
947 zfs_error_aux(hdl, strerror(errno));
948 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
951 return (zfs_standard_error(hdl, errno, errbuf));
956 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
957 nvlist_free(thisdbg);
963 hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
969 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
972 * zfs_send() only opens a cleanup_fd for sends that need it,
973 * e.g. replication and doall.
975 if (sdd->cleanup_fd == -1)
978 thissnap = strchr(zhp->zfs_name, '@') + 1;
979 *(thissnap - 1) = '\0';
980 pzhp = zfs_open(zhp->zfs_hdl, zhp->zfs_name, ZFS_TYPE_DATASET);
981 *(thissnap - 1) = '@';
984 * It's OK if the parent no longer exists. The send code will
988 error = zfs_hold(pzhp, thissnap, sdd->holdtag,
989 B_FALSE, B_TRUE, B_TRUE, sdd->cleanup_fd,
990 zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID),
991 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG));
999 dump_snapshot(zfs_handle_t *zhp, void *arg)
1001 send_dump_data_t *sdd = arg;
1004 boolean_t isfromsnap, istosnap;
1005 boolean_t exclude = B_FALSE;
1007 thissnap = strchr(zhp->zfs_name, '@') + 1;
1008 isfromsnap = (sdd->fromsnap != NULL &&
1009 strcmp(sdd->fromsnap, thissnap) == 0);
1011 if (!sdd->seenfrom && isfromsnap) {
1012 err = hold_for_send(zhp, sdd);
1014 sdd->seenfrom = B_TRUE;
1015 (void) strcpy(sdd->prevsnap, thissnap);
1016 sdd->prevsnap_obj = zfs_prop_get_int(zhp,
1018 } else if (err == ENOENT) {
1025 if (sdd->seento || !sdd->seenfrom) {
1030 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1032 sdd->seento = B_TRUE;
1034 if (!sdd->doall && !isfromsnap && !istosnap) {
1035 if (sdd->replicate) {
1037 nvlist_t *snapprops;
1039 * Filter out all intermediate snapshots except origin
1040 * snapshots needed to replicate clones.
1042 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1043 zhp->zfs_dmustats.dds_guid, &snapname);
1045 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1046 "snapprops", &snapprops));
1047 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1048 thissnap, &snapprops));
1049 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1056 * If a filter function exists, call it to determine whether
1057 * this snapshot will be sent.
1059 if (exclude || (sdd->filter_cb != NULL &&
1060 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1062 * This snapshot is filtered out. Don't send it, and don't
1063 * set prevsnap_obj, so it will be as if this snapshot didn't
1064 * exist, and the next accepted snapshot will be sent as
1065 * an incremental from the last accepted one, or as the
1066 * first (and full) snapshot in the case of a replication,
1067 * non-incremental send.
1073 err = hold_for_send(zhp, sdd);
1083 (void) fprintf(stderr, "sending from @%s to %s\n",
1084 sdd->prevsnap, zhp->zfs_name);
1087 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1088 sdd->prevsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
1089 sdd->outfd, sdd->debugnv);
1091 (void) strcpy(sdd->prevsnap, thissnap);
1092 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1098 dump_filesystem(zfs_handle_t *zhp, void *arg)
1101 send_dump_data_t *sdd = arg;
1102 boolean_t missingfrom = B_FALSE;
1103 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1105 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1106 zhp->zfs_name, sdd->tosnap);
1107 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1108 (void) fprintf(stderr, "WARNING: "
1109 "could not send %s@%s: does not exist\n",
1110 zhp->zfs_name, sdd->tosnap);
1115 if (sdd->replicate && sdd->fromsnap) {
1117 * If this fs does not have fromsnap, and we're doing
1118 * recursive, we need to send a full stream from the
1119 * beginning (or an incremental from the origin if this
1120 * is a clone). If we're doing non-recursive, then let
1121 * them get the error.
1123 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1124 zhp->zfs_name, sdd->fromsnap);
1125 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1126 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1127 missingfrom = B_TRUE;
1131 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1132 sdd->prevsnap_obj = 0;
1133 if (sdd->fromsnap == NULL || missingfrom)
1134 sdd->seenfrom = B_TRUE;
1136 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1137 if (!sdd->seenfrom) {
1138 (void) fprintf(stderr,
1139 "WARNING: could not send %s@%s:\n"
1140 "incremental source (%s@%s) does not exist\n",
1141 zhp->zfs_name, sdd->tosnap,
1142 zhp->zfs_name, sdd->fromsnap);
1144 } else if (!sdd->seento) {
1145 if (sdd->fromsnap) {
1146 (void) fprintf(stderr,
1147 "WARNING: could not send %s@%s:\n"
1148 "incremental source (%s@%s) "
1149 "is not earlier than it\n",
1150 zhp->zfs_name, sdd->tosnap,
1151 zhp->zfs_name, sdd->fromsnap);
1153 (void) fprintf(stderr, "WARNING: "
1154 "could not send %s@%s: does not exist\n",
1155 zhp->zfs_name, sdd->tosnap);
1164 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1166 send_dump_data_t *sdd = arg;
1168 boolean_t needagain, progress;
1170 if (!sdd->replicate)
1171 return (dump_filesystem(rzhp, sdd));
1173 /* Mark the clone origin snapshots. */
1174 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1175 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1177 uint64_t origin_guid = 0;
1179 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1180 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1181 if (origin_guid != 0) {
1183 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1184 origin_guid, &snapname);
1185 if (origin_nv != NULL) {
1186 nvlist_t *snapprops;
1187 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1188 "snapprops", &snapprops));
1189 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1190 snapname, &snapprops));
1191 VERIFY(0 == nvlist_add_boolean(
1192 snapprops, "is_clone_origin"));
1197 needagain = progress = B_FALSE;
1198 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1199 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1204 uint64_t origin_guid = 0;
1206 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1207 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1210 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1211 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1213 if (origin_guid != 0) {
1214 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1216 if (origin_nv != NULL &&
1217 nvlist_lookup_boolean(origin_nv,
1218 "sent") == ENOENT) {
1220 * origin has not been sent yet;
1228 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1231 err = dump_filesystem(zhp, sdd);
1232 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1246 * Generate a send stream for the dataset identified by the argument zhp.
1248 * The content of the send stream is the snapshot identified by
1249 * 'tosnap'. Incremental streams are requested in two ways:
1250 * - from the snapshot identified by "fromsnap" (if non-null) or
1251 * - from the origin of the dataset identified by zhp, which must
1252 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1255 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1256 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1257 * if "replicate" is set. If "doall" is set, dump all the intermediate
1258 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1259 * case too. If "props" is set, send properties.
1262 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1263 sendflags_t flags, int outfd, snapfilter_cb_t filter_func,
1264 void *cb_arg, nvlist_t **debugnvp)
1267 send_dump_data_t sdd = { 0 };
1269 nvlist_t *fss = NULL;
1270 avl_tree_t *fsavl = NULL;
1271 static uint64_t holdseq;
1273 boolean_t holdsnaps = B_FALSE;
1276 dedup_arg_t dda = { 0 };
1277 int featureflags = 0;
1279 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1280 "cannot send '%s'"), zhp->zfs_name);
1282 if (fromsnap && fromsnap[0] == '\0') {
1283 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1284 "zero-length incremental source"));
1285 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1288 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1290 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1291 if (version >= ZPL_VERSION_SA) {
1292 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1296 if (zfs_spa_version(zhp, &spa_version) == 0 &&
1297 spa_version >= SPA_VERSION_USERREFS &&
1298 (flags.doall || flags.replicate))
1302 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1303 DMU_BACKUP_FEATURE_DEDUPPROPS);
1304 if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd))) {
1305 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1306 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1309 dda.outputfd = outfd;
1310 dda.inputfd = pipefd[1];
1311 dda.dedup_hdl = zhp->zfs_hdl;
1312 if ((err = pthread_create(&tid, NULL, cksummer, &dda))) {
1313 (void) close(pipefd[0]);
1314 (void) close(pipefd[1]);
1315 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1316 return (zfs_error(zhp->zfs_hdl,
1317 EZFS_THREADCREATEFAILED, errbuf));
1321 if (flags.replicate || flags.doall || flags.props) {
1322 dmu_replay_record_t drr = { 0 };
1323 char *packbuf = NULL;
1325 zio_cksum_t zc = { { 0 } };
1327 if (flags.replicate || flags.props) {
1330 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1332 VERIFY(0 == nvlist_add_string(hdrnv,
1333 "fromsnap", fromsnap));
1335 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1336 if (!flags.replicate) {
1337 VERIFY(0 == nvlist_add_boolean(hdrnv,
1341 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1342 fromsnap, tosnap, flags.replicate, &fss, &fsavl);
1345 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1346 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1353 fsavl_destroy(fsavl);
1359 /* write first begin record */
1360 drr.drr_type = DRR_BEGIN;
1361 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1362 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.drr_versioninfo,
1363 DMU_COMPOUNDSTREAM);
1364 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.drr_versioninfo,
1366 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1367 sizeof (drr.drr_u.drr_begin.drr_toname),
1368 "%s@%s", zhp->zfs_name, tosnap);
1369 drr.drr_payloadlen = buflen;
1370 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
1372 /* write header nvlist */
1373 if (err != -1 && packbuf != NULL) {
1374 err = cksum_and_write(packbuf, buflen, &zc, outfd);
1378 fsavl_destroy(fsavl);
1384 /* write end record */
1386 bzero(&drr, sizeof (drr));
1387 drr.drr_type = DRR_END;
1388 drr.drr_u.drr_end.drr_checksum = zc;
1389 err = write(outfd, &drr, sizeof (drr));
1391 fsavl_destroy(fsavl);
1399 /* dump each stream */
1400 sdd.fromsnap = fromsnap;
1401 sdd.tosnap = tosnap;
1403 sdd.outfd = pipefd[0];
1406 sdd.replicate = flags.replicate;
1407 sdd.doall = flags.doall;
1408 sdd.fromorigin = flags.fromorigin;
1411 sdd.verbose = flags.verbose;
1412 sdd.filter_cb = filter_func;
1413 sdd.filter_cb_arg = cb_arg;
1415 sdd.debugnv = *debugnvp;
1418 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1419 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1420 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
1421 if (sdd.cleanup_fd < 0) {
1426 sdd.cleanup_fd = -1;
1428 err = dump_filesystems(zhp, &sdd);
1429 fsavl_destroy(fsavl);
1433 (void) close(pipefd[0]);
1434 (void) pthread_join(tid, NULL);
1437 if (sdd.cleanup_fd != -1) {
1438 VERIFY(0 == close(sdd.cleanup_fd));
1439 sdd.cleanup_fd = -1;
1442 if (flags.replicate || flags.doall || flags.props) {
1444 * write final end record. NB: want to do this even if
1445 * there was some error, because it might not be totally
1448 dmu_replay_record_t drr = { 0 };
1449 drr.drr_type = DRR_END;
1450 if (write(outfd, &drr, sizeof (drr)) == -1) {
1451 return (zfs_standard_error(zhp->zfs_hdl,
1456 return (err || sdd.err);
1459 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1461 if (sdd.cleanup_fd != -1)
1462 VERIFY(0 == close(sdd.cleanup_fd));
1464 (void) pthread_cancel(tid);
1465 (void) pthread_join(tid, NULL);
1466 (void) close(pipefd[0]);
1472 * Routines specific to "zfs recv"
1476 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1477 boolean_t byteswap, zio_cksum_t *zc)
1484 rv = read(fd, cp, len);
1489 if (rv < 0 || len != 0) {
1490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1491 "failed to read from stream"));
1492 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1493 "cannot receive")));
1498 fletcher_4_incremental_byteswap(buf, ilen, zc);
1500 fletcher_4_incremental_native(buf, ilen, zc);
1506 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1507 boolean_t byteswap, zio_cksum_t *zc)
1512 buf = zfs_alloc(hdl, len);
1516 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1522 err = nvlist_unpack(buf, len, nvp, 0);
1525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1526 "stream (malformed nvlist)"));
1533 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1534 int baselen, char *newname, recvflags_t flags)
1537 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1539 prop_changelist_t *clp;
1542 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1545 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1546 flags.force ? MS_FORCE : 0);
1550 err = changelist_prefix(clp);
1554 zc.zc_objset_type = DMU_OST_ZFS;
1555 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1558 (void) strcpy(newname, tryname);
1560 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1562 if (flags.verbose) {
1563 (void) printf("attempting rename %s to %s\n",
1564 zc.zc_name, zc.zc_value);
1566 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1568 changelist_rename(clp, name, tryname);
1573 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
1576 (void) strncpy(newname, name, baselen);
1577 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
1578 "recv-%ld-%u", (long) getpid(), seq);
1579 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1581 if (flags.verbose) {
1582 (void) printf("failed - trying rename %s to %s\n",
1583 zc.zc_name, zc.zc_value);
1585 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1587 changelist_rename(clp, name, newname);
1588 if (err && flags.verbose) {
1589 (void) printf("failed (%u) - "
1590 "will try again on next pass\n", errno);
1593 } else if (flags.verbose) {
1595 (void) printf("success\n");
1597 (void) printf("failed (%u)\n", errno);
1600 (void) changelist_postfix(clp);
1601 changelist_free(clp);
1607 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1608 char *newname, recvflags_t flags)
1610 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1612 prop_changelist_t *clp;
1614 boolean_t defer = B_FALSE;
1617 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1620 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1621 flags.force ? MS_FORCE : 0);
1622 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1623 zfs_spa_version(zhp, &spa_version) == 0 &&
1624 spa_version >= SPA_VERSION_USERREFS)
1629 err = changelist_prefix(clp);
1633 zc.zc_objset_type = DMU_OST_ZFS;
1634 zc.zc_defer_destroy = defer;
1635 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1638 (void) printf("attempting destroy %s\n", zc.zc_name);
1639 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1642 (void) printf("success\n");
1643 changelist_remove(clp, zc.zc_name);
1646 (void) changelist_postfix(clp);
1647 changelist_free(clp);
1650 * Deferred destroy might destroy the snapshot or only mark it to be
1651 * destroyed later, and it returns success in either case.
1653 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1654 ZFS_TYPE_SNAPSHOT))) {
1655 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1661 typedef struct guid_to_name_data {
1664 } guid_to_name_data_t;
1667 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1669 guid_to_name_data_t *gtnd = arg;
1672 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1673 (void) strcpy(gtnd->name, zhp->zfs_name);
1677 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1683 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1686 /* exhaustive search all local snapshots */
1687 guid_to_name_data_t gtnd;
1695 if (strchr(parent, '@') == NULL) {
1696 zhp = make_dataset_handle(hdl, parent);
1698 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1705 cp = strchr(parent, '/');
1708 zhp = make_dataset_handle(hdl, parent);
1713 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1717 return (err == EEXIST ? 0 : ENOENT);
1722 * Return true if dataset guid1 is created before guid2.
1725 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1726 uint64_t guid1, uint64_t guid2)
1729 char *fsname, *snapname;
1730 char buf[ZFS_MAXNAMELEN];
1732 zfs_node_t zn1, zn2;
1739 nvfs = fsavl_find(avl, guid1, &snapname);
1740 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1741 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1742 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1743 if (zn1.zn_handle == NULL)
1746 nvfs = fsavl_find(avl, guid2, &snapname);
1747 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1748 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1749 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1750 if (zn2.zn_handle == NULL) {
1751 zfs_close(zn2.zn_handle);
1755 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1757 zfs_close(zn1.zn_handle);
1758 zfs_close(zn2.zn_handle);
1764 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1765 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
1769 avl_tree_t *local_avl;
1770 nvpair_t *fselem, *nextfselem;
1772 char newname[ZFS_MAXNAMELEN];
1774 boolean_t needagain, progress, recursive;
1777 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1779 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
1786 needagain = progress = B_FALSE;
1788 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1789 recursive, &local_nv, &local_avl)) != 0)
1793 * Process deletes and renames
1795 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1796 fselem; fselem = nextfselem) {
1797 nvlist_t *nvfs, *snaps;
1798 nvlist_t *stream_nvfs = NULL;
1799 nvpair_t *snapelem, *nextsnapelem;
1800 uint64_t fromguid = 0;
1801 uint64_t originguid = 0;
1802 uint64_t stream_originguid = 0;
1803 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1804 char *fsname, *stream_fsname;
1806 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1808 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1809 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1810 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1811 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1812 &parent_fromsnap_guid));
1813 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1816 * First find the stream's fs, so we can check for
1817 * a different origin (due to "zfs promote")
1819 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1820 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1823 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1824 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1826 if (stream_nvfs != NULL)
1830 /* check for promote */
1831 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1832 &stream_originguid);
1833 if (stream_nvfs && originguid != stream_originguid) {
1834 switch (created_before(hdl, local_avl,
1835 stream_originguid, originguid)) {
1838 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1839 nvlist_t *origin_nvfs;
1840 char *origin_fsname;
1843 (void) printf("promoting %s\n", fsname);
1845 origin_nvfs = fsavl_find(local_avl, originguid,
1847 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1848 "name", &origin_fsname));
1849 (void) strlcpy(zc.zc_value, origin_fsname,
1850 sizeof (zc.zc_value));
1851 (void) strlcpy(zc.zc_name, fsname,
1852 sizeof (zc.zc_name));
1853 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1861 fsavl_destroy(local_avl);
1862 nvlist_free(local_nv);
1866 * We had/have the wrong origin, therefore our
1867 * list of snapshots is wrong. Need to handle
1868 * them on the next pass.
1874 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1875 snapelem; snapelem = nextsnapelem) {
1877 char *stream_snapname;
1878 nvlist_t *found, *props;
1880 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1882 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1883 found = fsavl_find(stream_avl, thisguid,
1886 /* check for delete */
1887 if (found == NULL) {
1888 char name[ZFS_MAXNAMELEN];
1893 (void) snprintf(name, sizeof (name), "%s@%s",
1894 fsname, nvpair_name(snapelem));
1896 error = recv_destroy(hdl, name,
1897 strlen(fsname)+1, newname, flags);
1905 stream_nvfs = found;
1907 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1908 &props) && 0 == nvlist_lookup_nvlist(props,
1909 stream_snapname, &props)) {
1910 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1912 zc.zc_cookie = B_TRUE; /* received */
1913 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1914 "%s@%s", fsname, nvpair_name(snapelem));
1915 if (zcmd_write_src_nvlist(hdl, &zc,
1917 (void) zfs_ioctl(hdl,
1918 ZFS_IOC_SET_PROP, &zc);
1919 zcmd_free_nvlists(&zc);
1923 /* check for different snapname */
1924 if (strcmp(nvpair_name(snapelem),
1925 stream_snapname) != 0) {
1926 char name[ZFS_MAXNAMELEN];
1927 char tryname[ZFS_MAXNAMELEN];
1929 (void) snprintf(name, sizeof (name), "%s@%s",
1930 fsname, nvpair_name(snapelem));
1931 (void) snprintf(tryname, sizeof (name), "%s@%s",
1932 fsname, stream_snapname);
1934 error = recv_rename(hdl, name, tryname,
1935 strlen(fsname)+1, newname, flags);
1942 if (strcmp(stream_snapname, fromsnap) == 0)
1943 fromguid = thisguid;
1946 /* check for delete */
1947 if (stream_nvfs == NULL) {
1951 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1960 if (fromguid == 0) {
1961 if (flags.verbose) {
1962 (void) printf("local fs %s does not have "
1963 "fromsnap (%s in stream); must have "
1964 "been deleted locally; ignoring\n",
1970 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1971 "name", &stream_fsname));
1972 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1973 "parentfromsnap", &stream_parent_fromsnap_guid));
1975 s1 = strrchr(fsname, '/');
1976 s2 = strrchr(stream_fsname, '/');
1979 * Check for rename. If the exact receive path is specified, it
1980 * does not count as a rename, but we still need to check the
1981 * datasets beneath it.
1983 if ((stream_parent_fromsnap_guid != 0 &&
1984 parent_fromsnap_guid != 0 &&
1985 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1986 ((flags.isprefix || strcmp(tofs, fsname) != 0) &&
1987 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
1989 char tryname[ZFS_MAXNAMELEN];
1991 parent = fsavl_find(local_avl,
1992 stream_parent_fromsnap_guid, NULL);
1994 * NB: parent might not be found if we used the
1995 * tosnap for stream_parent_fromsnap_guid,
1996 * because the parent is a newly-created fs;
1997 * we'll be able to rename it after we recv the
2000 if (parent != NULL) {
2003 VERIFY(0 == nvlist_lookup_string(parent, "name",
2005 (void) snprintf(tryname, sizeof (tryname),
2006 "%s%s", pname, strrchr(stream_fsname, '/'));
2009 if (flags.verbose) {
2010 (void) printf("local fs %s new parent "
2011 "not found\n", fsname);
2017 error = recv_rename(hdl, fsname, tryname,
2018 strlen(tofs)+1, newname, flags);
2020 if (renamed != NULL && newname[0] != '\0') {
2021 VERIFY(0 == nvlist_add_boolean(renamed,
2032 fsavl_destroy(local_avl);
2033 nvlist_free(local_nv);
2035 if (needagain && progress) {
2036 /* do another pass to fix up temporary names */
2038 (void) printf("another pass:\n");
2046 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2047 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2048 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2050 nvlist_t *stream_nv = NULL;
2051 avl_tree_t *stream_avl = NULL;
2052 char *fromsnap = NULL;
2054 char tofs[ZFS_MAXNAMELEN];
2055 char sendfs[ZFS_MAXNAMELEN];
2057 dmu_replay_record_t drre;
2059 boolean_t anyerr = B_FALSE;
2060 boolean_t softerr = B_FALSE;
2061 boolean_t recursive;
2063 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2066 assert(drr->drr_type == DRR_BEGIN);
2067 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2068 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2069 DMU_COMPOUNDSTREAM);
2072 * Read in the nvlist from the stream.
2074 if (drr->drr_payloadlen != 0) {
2075 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2076 &stream_nv, flags.byteswap, zc);
2078 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2083 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2086 if (recursive && strchr(destname, '@')) {
2087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2088 "cannot specify snapshot name for multi-snapshot stream"));
2089 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2094 * Read in the end record and verify checksum.
2096 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2097 flags.byteswap, NULL)))
2099 if (flags.byteswap) {
2100 drre.drr_type = BSWAP_32(drre.drr_type);
2101 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2102 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2103 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2104 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2105 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2106 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2107 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2108 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2110 if (drre.drr_type != DRR_END) {
2111 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2114 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2116 "incorrect header checksum"));
2117 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2121 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2123 if (drr->drr_payloadlen != 0) {
2124 nvlist_t *stream_fss;
2126 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2128 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2130 "couldn't allocate avl tree"));
2131 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2135 if (fromsnap != NULL) {
2136 nvlist_t *renamed = NULL;
2137 nvpair_t *pair = NULL;
2139 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2140 if (flags.isprefix) {
2141 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2145 cp = strrchr(drrb->drr_toname, '/');
2147 (void) strlcat(tofs, "/",
2151 i = (cp - drrb->drr_toname);
2154 i = strcspn(drrb->drr_toname, "/@");
2156 /* zfs_receive_one() will create_parents() */
2157 (void) strlcat(tofs, &drrb->drr_toname[i],
2159 *strchr(tofs, '@') = '\0';
2162 if (recursive && !flags.dryrun && !flags.nomount) {
2163 VERIFY(0 == nvlist_alloc(&renamed,
2164 NV_UNIQUE_NAME, 0));
2167 softerr = recv_incremental_replication(hdl, tofs, flags,
2168 stream_nv, stream_avl, renamed);
2170 /* Unmount renamed filesystems before receiving. */
2171 while ((pair = nvlist_next_nvpair(renamed,
2174 prop_changelist_t *clp = NULL;
2176 zhp = zfs_open(hdl, nvpair_name(pair),
2177 ZFS_TYPE_FILESYSTEM);
2179 clp = changelist_gather(zhp,
2180 ZFS_PROP_MOUNTPOINT, 0, 0);
2184 changelist_prefix(clp);
2185 changelist_free(clp);
2190 nvlist_free(renamed);
2195 * Get the fs specified by the first path in the stream (the top level
2196 * specified by 'zfs send') and pass it to each invocation of
2197 * zfs_receive_one().
2199 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2201 if ((cp = strchr(sendfs, '@')) != NULL)
2204 /* Finally, receive each contained stream */
2207 * we should figure out if it has a recoverable
2208 * error, in which case do a recv_skip() and drive on.
2209 * Note, if we fail due to already having this guid,
2210 * zfs_receive_one() will take care of it (ie,
2211 * recv_skip() and return 0).
2213 error = zfs_receive_impl(hdl, destname, flags, fd,
2214 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2216 if (error == ENODATA) {
2221 } while (error == 0);
2223 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2225 * Now that we have the fs's they sent us, try the
2228 softerr = recv_incremental_replication(hdl, tofs, flags,
2229 stream_nv, stream_avl, NULL);
2233 fsavl_destroy(stream_avl);
2235 nvlist_free(stream_nv);
2244 trunc_prop_errs(int truncated)
2246 ASSERT(truncated != 0);
2249 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2250 "1 more property could not be set\n"));
2252 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2253 "%d more properties could not be set\n"), truncated);
2257 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2259 dmu_replay_record_t *drr;
2260 void *buf = malloc(1<<20);
2263 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2264 "cannot receive:"));
2266 /* XXX would be great to use lseek if possible... */
2269 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2270 byteswap, NULL) == 0) {
2272 drr->drr_type = BSWAP_32(drr->drr_type);
2274 switch (drr->drr_type) {
2276 /* NB: not to be used on v2 stream packages */
2277 if (drr->drr_payloadlen != 0) {
2278 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2279 "invalid substream header"));
2280 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2290 drr->drr_u.drr_object.drr_bonuslen =
2291 BSWAP_32(drr->drr_u.drr_object.
2294 (void) recv_read(hdl, fd, buf,
2295 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2301 drr->drr_u.drr_write.drr_length =
2302 BSWAP_64(drr->drr_u.drr_write.drr_length);
2304 (void) recv_read(hdl, fd, buf,
2305 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2309 drr->drr_u.drr_write.drr_length =
2310 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2312 (void) recv_read(hdl, fd, buf,
2313 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2315 case DRR_WRITE_BYREF:
2316 case DRR_FREEOBJECTS:
2321 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2322 "invalid record type"));
2323 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2332 * Restores a backup of tosnap from the file descriptor specified by infd.
2335 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2336 recvflags_t flags, dmu_replay_record_t *drr,
2337 dmu_replay_record_t *drr_noswap, const char *sendfs,
2338 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2339 uint64_t *action_handlep)
2341 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2343 int ioctl_err, ioctl_errno, err;
2345 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2347 char prop_errbuf[1024];
2348 const char *chopprefix;
2349 boolean_t newfs = B_FALSE;
2350 boolean_t stream_wantsnewfs;
2351 uint64_t parent_snapguid = 0;
2352 prop_changelist_t *clp = NULL;
2353 nvlist_t *snapprops_nvlist = NULL;
2354 zprop_errflags_t prop_errflags;
2355 boolean_t recursive;
2357 begin_time = time(NULL);
2359 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2362 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2365 if (stream_avl != NULL) {
2367 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2372 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2374 err = nvlist_lookup_nvlist(fs, "props", &props);
2376 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2378 if (flags.canmountoff) {
2379 VERIFY(0 == nvlist_add_uint64(props,
2380 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2382 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2386 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2387 VERIFY(0 == nvlist_lookup_nvlist(props,
2388 snapname, &snapprops_nvlist));
2398 * Determine how much of the snapshot name stored in the stream
2399 * we are going to tack on to the name they specified on the
2400 * command line, and how much we are going to chop off.
2402 * If they specified a snapshot, chop the entire name stored in
2407 * A filesystem was specified with -e. We want to tack on only
2408 * the tail of the sent snapshot path.
2410 if (strchr(tosnap, '@')) {
2411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2412 "argument - snapshot not allowed with -e"));
2413 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2416 chopprefix = strrchr(sendfs, '/');
2418 if (chopprefix == NULL) {
2420 * The tail is the poolname, so we need to
2421 * prepend a path separator.
2423 int len = strlen(drrb->drr_toname);
2424 cp = malloc(len + 2);
2426 (void) strcpy(&cp[1], drrb->drr_toname);
2429 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2431 } else if (flags.isprefix) {
2433 * A filesystem was specified with -d. We want to tack on
2434 * everything but the first element of the sent snapshot path
2435 * (all but the pool name).
2437 if (strchr(tosnap, '@')) {
2438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2439 "argument - snapshot not allowed with -d"));
2440 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2443 chopprefix = strchr(drrb->drr_toname, '/');
2444 if (chopprefix == NULL)
2445 chopprefix = strchr(drrb->drr_toname, '@');
2446 } else if (strchr(tosnap, '@') == NULL) {
2448 * If a filesystem was specified without -d or -e, we want to
2449 * tack on everything after the fs specified by 'zfs send'.
2451 chopprefix = drrb->drr_toname + strlen(sendfs);
2453 /* A snapshot was specified as an exact path (no -d or -e). */
2455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2456 "cannot specify snapshot name for multi-snapshot "
2458 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2460 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2463 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2464 ASSERT(chopprefix > drrb->drr_toname);
2465 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2466 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2467 chopprefix[0] == '\0');
2470 * Determine name of destination snapshot, store in zc_value.
2472 (void) strcpy(zc.zc_top_ds, tosnap);
2473 (void) strcpy(zc.zc_value, tosnap);
2474 (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2476 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2477 zcmd_free_nvlists(&zc);
2478 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2482 * Determine the name of the origin snapshot, store in zc_string.
2484 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2485 if (guid_to_name(hdl, tosnap,
2486 drrb->drr_fromguid, zc.zc_string) != 0) {
2487 zcmd_free_nvlists(&zc);
2488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2489 "local origin for clone %s does not exist"),
2491 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2494 (void) printf("found clone origin %s\n", zc.zc_string);
2497 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2498 (drrb->drr_flags & DRR_FLAG_CLONE));
2500 if (stream_wantsnewfs) {
2502 * if the parent fs does not exist, look for it based on
2503 * the parent snap GUID
2505 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2506 "cannot receive new filesystem stream"));
2508 (void) strcpy(zc.zc_name, zc.zc_value);
2509 cp = strrchr(zc.zc_name, '/');
2513 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2514 char suffix[ZFS_MAXNAMELEN];
2515 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2516 if (guid_to_name(hdl, tosnap, parent_snapguid,
2517 zc.zc_value) == 0) {
2518 *strchr(zc.zc_value, '@') = '\0';
2519 (void) strcat(zc.zc_value, suffix);
2524 * if the fs does not exist, look for it based on the
2527 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2528 "cannot receive incremental stream"));
2530 (void) strcpy(zc.zc_name, zc.zc_value);
2531 *strchr(zc.zc_name, '@') = '\0';
2534 * If the exact receive path was specified and this is the
2535 * topmost path in the stream, then if the fs does not exist we
2536 * should look no further.
2538 if ((flags.isprefix || (*(chopprefix = drrb->drr_toname +
2539 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2540 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2541 char snap[ZFS_MAXNAMELEN];
2542 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2543 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
2544 zc.zc_value) == 0) {
2545 *strchr(zc.zc_value, '@') = '\0';
2546 (void) strcat(zc.zc_value, snap);
2551 (void) strcpy(zc.zc_name, zc.zc_value);
2552 *strchr(zc.zc_name, '@') = '\0';
2554 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2558 * Destination fs exists. Therefore this should either
2559 * be an incremental, or the stream specifies a new fs
2560 * (full stream or clone) and they want us to blow it
2561 * away (and have therefore specified -F and removed any
2564 if (stream_wantsnewfs) {
2566 zcmd_free_nvlists(&zc);
2567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2568 "destination '%s' exists\n"
2569 "must specify -F to overwrite it"),
2571 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2573 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2575 zcmd_free_nvlists(&zc);
2576 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2577 "destination has snapshots (eg. %s)\n"
2578 "must destroy them to overwrite it"),
2580 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2584 if ((zhp = zfs_open(hdl, zc.zc_name,
2585 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2586 zcmd_free_nvlists(&zc);
2590 if (stream_wantsnewfs &&
2591 zhp->zfs_dmustats.dds_origin[0]) {
2592 zcmd_free_nvlists(&zc);
2594 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2595 "destination '%s' is a clone\n"
2596 "must destroy it to overwrite it"),
2598 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2601 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2602 stream_wantsnewfs) {
2603 /* We can't do online recv in this case */
2604 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2607 zcmd_free_nvlists(&zc);
2610 if (changelist_prefix(clp) != 0) {
2611 changelist_free(clp);
2613 zcmd_free_nvlists(&zc);
2617 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
2618 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
2620 zcmd_free_nvlists(&zc);
2626 * Destination filesystem does not exist. Therefore we better
2627 * be creating a new filesystem (either from a full backup, or
2628 * a clone). It would therefore be invalid if the user
2629 * specified only the pool name (i.e. if the destination name
2630 * contained no slash character).
2632 if (!stream_wantsnewfs ||
2633 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2634 zcmd_free_nvlists(&zc);
2635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2636 "destination '%s' does not exist"), zc.zc_name);
2637 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2641 * Trim off the final dataset component so we perform the
2642 * recvbackup ioctl to the filesystems's parent.
2646 if (flags.isprefix && !flags.istail && !flags.dryrun &&
2647 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2648 zcmd_free_nvlists(&zc);
2649 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2655 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2656 zc.zc_cookie = infd;
2657 zc.zc_guid = flags.force;
2658 if (flags.verbose) {
2659 (void) printf("%s %s stream of %s into %s\n",
2660 flags.dryrun ? "would receive" : "receiving",
2661 drrb->drr_fromguid ? "incremental" : "full",
2662 drrb->drr_toname, zc.zc_value);
2663 (void) fflush(stdout);
2667 zcmd_free_nvlists(&zc);
2668 return (recv_skip(hdl, infd, flags.byteswap));
2671 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2672 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2673 zc.zc_cleanup_fd = cleanup_fd;
2674 zc.zc_action_handle = *action_handlep;
2676 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2677 ioctl_errno = errno;
2678 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2681 nvlist_t *prop_errors;
2682 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2683 zc.zc_nvlist_dst_size, &prop_errors, 0));
2685 nvpair_t *prop_err = NULL;
2687 while ((prop_err = nvlist_next_nvpair(prop_errors,
2688 prop_err)) != NULL) {
2693 prop = zfs_name_to_prop(nvpair_name(prop_err));
2694 (void) nvpair_value_int32(prop_err, &intval);
2695 if (strcmp(nvpair_name(prop_err),
2696 ZPROP_N_MORE_ERRORS) == 0) {
2697 trunc_prop_errs(intval);
2700 (void) snprintf(tbuf, sizeof (tbuf),
2701 dgettext(TEXT_DOMAIN,
2702 "cannot receive %s property on %s"),
2703 nvpair_name(prop_err), zc.zc_name);
2704 zfs_setprop_error(hdl, prop, intval, tbuf);
2707 nvlist_free(prop_errors);
2710 zc.zc_nvlist_dst = 0;
2711 zc.zc_nvlist_dst_size = 0;
2712 zcmd_free_nvlists(&zc);
2714 if (err == 0 && snapprops_nvlist) {
2715 zfs_cmd_t zc2 = { "\0", "\0", "\0", "\0", 0 };
2717 (void) strcpy(zc2.zc_name, zc.zc_value);
2718 zc2.zc_cookie = B_TRUE; /* received */
2719 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
2720 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
2721 zcmd_free_nvlists(&zc2);
2725 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
2727 * It may be that this snapshot already exists,
2728 * in which case we want to consume & ignore it
2729 * rather than failing.
2731 avl_tree_t *local_avl;
2732 nvlist_t *local_nv, *fs;
2733 cp = strchr(zc.zc_value, '@');
2736 * XXX Do this faster by just iterating over snaps in
2737 * this fs. Also if zc_value does not exist, we will
2738 * get a strange "does not exist" error message.
2741 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
2742 &local_nv, &local_avl) == 0) {
2744 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
2745 fsavl_destroy(local_avl);
2746 nvlist_free(local_nv);
2749 if (flags.verbose) {
2750 (void) printf("snap %s already exists; "
2751 "ignoring\n", zc.zc_value);
2753 err = ioctl_err = recv_skip(hdl, infd,
2760 if (ioctl_err != 0) {
2761 switch (ioctl_errno) {
2763 cp = strchr(zc.zc_value, '@');
2765 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2766 "most recent snapshot of %s does not\n"
2767 "match incremental source"), zc.zc_value);
2768 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2773 "destination %s has been modified\n"
2774 "since most recent snapshot"), zc.zc_name);
2775 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2778 cp = strchr(zc.zc_value, '@');
2780 /* it's the containing fs that exists */
2783 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2784 "destination already exists"));
2785 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
2786 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
2791 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2795 "invalid stream (checksum mismatch)"));
2796 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2800 "pool must be upgraded to receive this stream."));
2801 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
2804 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2805 "destination %s space quota exceeded"), zc.zc_name);
2806 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2809 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
2814 * Mount the target filesystem (if created). Also mount any
2815 * children of the target filesystem if we did a replication
2816 * receive (indicated by stream_avl being non-NULL).
2818 cp = strchr(zc.zc_value, '@');
2819 if (cp && (ioctl_err == 0 || !newfs)) {
2823 h = zfs_open(hdl, zc.zc_value,
2824 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2826 if (h->zfs_type == ZFS_TYPE_VOLUME) {
2828 err = zvol_create_link(hdl, h->zfs_name);
2829 if (err == 0 && ioctl_err == 0)
2830 err = zvol_create_link(hdl,
2832 } else if (newfs || stream_avl) {
2834 * Track the first/top of hierarchy fs,
2835 * for mounting and sharing later.
2837 if (top_zfs && *top_zfs == NULL)
2838 *top_zfs = zfs_strdup(hdl, zc.zc_value);
2846 err |= changelist_postfix(clp);
2847 changelist_free(clp);
2850 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
2851 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2852 "failed to clear unreceived properties on %s"),
2854 (void) fprintf(stderr, "\n");
2856 if (prop_errflags & ZPROP_ERR_NORESTORE) {
2857 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2858 "failed to restore original properties on %s"),
2860 (void) fprintf(stderr, "\n");
2863 if (err || ioctl_err)
2866 *action_handlep = zc.zc_action_handle;
2868 if (flags.verbose) {
2871 uint64_t bytes = zc.zc_cookie;
2872 time_t delta = time(NULL) - begin_time;
2875 zfs_nicenum(bytes, buf1, sizeof (buf1));
2876 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
2878 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
2886 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2887 int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2888 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2891 dmu_replay_record_t drr, drr_noswap;
2892 struct drr_begin *drrb = &drr.drr_u.drr_begin;
2894 zio_cksum_t zcksum = { { 0 } };
2895 uint64_t featureflags;
2898 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2901 if (flags.isprefix &&
2902 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2903 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2904 "(%s) does not exist"), tosnap);
2905 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2908 /* read in the BEGIN record */
2909 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2913 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2914 /* It's the double end record at the end of a package */
2918 /* the kernel needs the non-byteswapped begin record */
2921 flags.byteswap = B_FALSE;
2922 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2924 * We computed the checksum in the wrong byteorder in
2925 * recv_read() above; do it again correctly.
2927 bzero(&zcksum, sizeof (zio_cksum_t));
2928 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2929 flags.byteswap = B_TRUE;
2931 drr.drr_type = BSWAP_32(drr.drr_type);
2932 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2933 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2934 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
2935 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2936 drrb->drr_type = BSWAP_32(drrb->drr_type);
2937 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2938 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2939 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2942 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2944 "stream (bad magic number)"));
2945 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2948 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
2949 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
2951 if (!DMU_STREAM_SUPPORTED(featureflags) ||
2952 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
2953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2954 "stream has unsupported feature, feature flags = %lx"),
2956 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2959 if (strchr(drrb->drr_toname, '@') == NULL) {
2960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2961 "stream (bad snapshot name)"));
2962 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2965 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
2966 char nonpackage_sendfs[ZFS_MAXNAMELEN];
2967 if (sendfs == NULL) {
2969 * We were not called from zfs_receive_package(). Get
2970 * the fs specified by 'zfs send'.
2973 (void) strlcpy(nonpackage_sendfs,
2974 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
2975 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
2977 sendfs = nonpackage_sendfs;
2979 return (zfs_receive_one(hdl, infd, tosnap, flags,
2980 &drr, &drr_noswap, sendfs, stream_nv, stream_avl,
2981 top_zfs, cleanup_fd, action_handlep));
2983 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
2984 DMU_COMPOUNDSTREAM);
2985 return (zfs_receive_package(hdl, infd, tosnap, flags,
2986 &drr, &zcksum, top_zfs, cleanup_fd, action_handlep));
2991 * Restores a backup of tosnap from the file descriptor specified by infd.
2992 * Return 0 on total success, -2 if some things couldn't be
2993 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2994 * (-1 will override -2).
2997 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2998 int infd, avl_tree_t *stream_avl)
3000 char *top_zfs = NULL;
3003 uint64_t action_handle = 0;
3005 cleanup_fd = open(ZFS_DEV, O_RDWR);
3006 VERIFY(cleanup_fd >= 0);
3008 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
3009 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3011 VERIFY(0 == close(cleanup_fd));
3013 if (err == 0 && !flags.nomount && top_zfs) {
3015 prop_changelist_t *clp;
3017 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3019 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3020 CL_GATHER_MOUNT_ALWAYS, 0);
3023 /* mount and share received datasets */
3024 err = changelist_postfix(clp);
3025 changelist_free(clp);
3028 if (zhp == NULL || clp == NULL || err)