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>
42 #include "zfs_namecheck.h"
44 #include "zfs_fletcher.h"
45 #include "libzfs_impl.h"
47 #include <sys/zio_checksum.h>
50 /* in libzfs_dataset.c */
51 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
53 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
54 int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
56 static const zio_cksum_t zero_cksum = { { 0 } };
58 typedef struct dedup_arg {
61 libzfs_handle_t *dedup_hdl;
64 typedef struct dataref {
70 typedef struct dedup_entry {
71 struct dedup_entry *dde_next;
72 zio_cksum_t dde_chksum;
77 #define MAX_DDT_PHYSMEM_PERCENT 20
78 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
80 typedef struct dedup_table {
81 dedup_entry_t **dedup_hash_array;
82 umem_cache_t *ddecache;
83 uint64_t max_ddt_size; /* max dedup table size in bytes */
84 uint64_t cur_ddt_size; /* current dedup table size in bytes */
91 high_order_bit(uint64_t n)
95 for (count = 0; n != 0; count++)
101 ssread(void *buf, size_t len, FILE *stream)
105 if ((outlen = fread(buf, len, 1, stream)) == 0)
112 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
113 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
117 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
118 if (ddt->ddt_full == B_FALSE) {
119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
120 "Dedup table full. Deduplication will continue "
121 "with existing table entries"));
122 ddt->ddt_full = B_TRUE;
127 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
129 assert(*ddepp == NULL);
130 dde->dde_next = NULL;
131 dde->dde_chksum = *cs;
132 dde->dde_prop = prop;
135 ddt->cur_ddt_size += sizeof (dedup_entry_t);
141 * Using the specified dedup table, do a lookup for an entry with
142 * the checksum cs. If found, return the block's reference info
143 * in *dr. Otherwise, insert a new entry in the dedup table, using
144 * the reference information specified by *dr.
146 * return value: true - entry was found
147 * false - entry was not found
150 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
151 uint64_t prop, dataref_t *dr)
154 dedup_entry_t **ddepp;
156 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
158 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
159 ddepp = &((*ddepp)->dde_next)) {
160 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
161 (*ddepp)->dde_prop == prop) {
162 *dr = (*ddepp)->dde_ref;
166 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
171 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
173 fletcher_4_incremental_native(buf, len, zc);
174 return (write(outfd, buf, len));
178 * This function is started in a separate thread when the dedup option
179 * has been requested. The main send thread determines the list of
180 * snapshots to be included in the send stream and makes the ioctl calls
181 * for each one. But instead of having the ioctl send the output to the
182 * the output fd specified by the caller of zfs_send()), the
183 * ioctl is told to direct the output to a pipe, which is read by the
184 * alternate thread running THIS function. This function does the
186 * 1. building a dedup table (the DDT)
187 * 2. doing checksums on each data block and inserting a record in the DDT
188 * 3. looking for matching checksums, and
189 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
190 * a duplicate block is found.
191 * The output of this function then goes to the output fd requested
192 * by the caller of zfs_send().
197 dedup_arg_t *dda = arg;
198 char *buf = malloc(1<<20);
199 dmu_replay_record_t thedrr;
200 dmu_replay_record_t *drr = &thedrr;
201 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
202 struct drr_end *drre = &thedrr.drr_u.drr_end;
203 struct drr_object *drro = &thedrr.drr_u.drr_object;
204 struct drr_write *drrw = &thedrr.drr_u.drr_write;
205 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
208 dmu_replay_record_t wbr_drr = {0};
209 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
211 zio_cksum_t stream_cksum;
212 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
216 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
217 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
219 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
222 * numbuckets must be a power of 2. Increase number to
223 * a power of 2 if necessary.
225 if (!ISP2(numbuckets))
226 numbuckets = 1 << high_order_bit(numbuckets);
228 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
229 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
230 NULL, NULL, NULL, NULL, NULL, 0);
231 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
232 ddt.numhashbits = high_order_bit(numbuckets) - 1;
233 ddt.ddt_full = B_FALSE;
235 /* Initialize the write-by-reference block. */
236 wbr_drr.drr_type = DRR_WRITE_BYREF;
237 wbr_drr.drr_payloadlen = 0;
239 outfd = dda->outputfd;
240 ofp = fdopen(dda->inputfd, "r");
241 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
243 switch (drr->drr_type) {
247 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
249 /* set the DEDUP feature flag for this stream */
250 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
251 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
252 DMU_BACKUP_FEATURE_DEDUPPROPS);
253 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
255 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
256 &stream_cksum, outfd) == -1)
258 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
259 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
260 int sz = drr->drr_payloadlen;
266 (void) ssread(buf, sz, ofp);
269 if (cksum_and_write(buf, sz, &stream_cksum,
278 /* use the recalculated checksum */
279 ZIO_SET_CHECKSUM(&drre->drr_checksum,
280 stream_cksum.zc_word[0], stream_cksum.zc_word[1],
281 stream_cksum.zc_word[2], stream_cksum.zc_word[3]);
282 if ((write(outfd, drr,
283 sizeof (dmu_replay_record_t))) == -1)
290 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
291 &stream_cksum, outfd) == -1)
293 if (drro->drr_bonuslen > 0) {
295 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
297 if (cksum_and_write(buf,
298 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
299 &stream_cksum, outfd) == -1)
307 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
308 &stream_cksum, outfd) == -1)
310 (void) ssread(buf, drrs->drr_length, ofp);
311 if (cksum_and_write(buf, drrs->drr_length,
312 &stream_cksum, outfd) == -1)
317 case DRR_FREEOBJECTS:
319 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
320 &stream_cksum, outfd) == -1)
329 (void) ssread(buf, drrw->drr_length, ofp);
332 * Use the existing checksum if it's dedup-capable,
333 * else calculate a SHA256 checksum for it.
336 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
338 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
340 zio_cksum_t tmpsha256;
343 SHA256Update(&ctx, buf, drrw->drr_length);
344 SHA256Final(&tmpsha256, &ctx);
345 drrw->drr_key.ddk_cksum.zc_word[0] =
346 BE_64(tmpsha256.zc_word[0]);
347 drrw->drr_key.ddk_cksum.zc_word[1] =
348 BE_64(tmpsha256.zc_word[1]);
349 drrw->drr_key.ddk_cksum.zc_word[2] =
350 BE_64(tmpsha256.zc_word[2]);
351 drrw->drr_key.ddk_cksum.zc_word[3] =
352 BE_64(tmpsha256.zc_word[3]);
353 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
354 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
357 dataref.ref_guid = drrw->drr_toguid;
358 dataref.ref_object = drrw->drr_object;
359 dataref.ref_offset = drrw->drr_offset;
361 if (ddt_update(dda->dedup_hdl, &ddt,
362 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
364 /* block already present in stream */
365 wbr_drrr->drr_object = drrw->drr_object;
366 wbr_drrr->drr_offset = drrw->drr_offset;
367 wbr_drrr->drr_length = drrw->drr_length;
368 wbr_drrr->drr_toguid = drrw->drr_toguid;
369 wbr_drrr->drr_refguid = dataref.ref_guid;
370 wbr_drrr->drr_refobject =
372 wbr_drrr->drr_refoffset =
375 wbr_drrr->drr_checksumtype =
376 drrw->drr_checksumtype;
377 wbr_drrr->drr_checksumflags =
378 drrw->drr_checksumtype;
379 wbr_drrr->drr_key.ddk_cksum =
380 drrw->drr_key.ddk_cksum;
381 wbr_drrr->drr_key.ddk_prop =
382 drrw->drr_key.ddk_prop;
384 if (cksum_and_write(&wbr_drr,
385 sizeof (dmu_replay_record_t), &stream_cksum,
389 /* block not previously seen */
390 if (cksum_and_write(drr,
391 sizeof (dmu_replay_record_t), &stream_cksum,
394 if (cksum_and_write(buf,
396 &stream_cksum, outfd) == -1)
404 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
405 &stream_cksum, outfd) == -1)
411 (void) printf("INVALID record type 0x%x\n",
413 /* should never happen, so assert */
418 umem_cache_destroy(ddt.ddecache);
419 free(ddt.dedup_hash_array);
427 * Routines for dealing with the AVL tree of fs-nvlists
429 typedef struct fsavl_node {
437 fsavl_compare(const void *arg1, const void *arg2)
439 const fsavl_node_t *fn1 = arg1;
440 const fsavl_node_t *fn2 = arg2;
442 if (fn1->fn_guid > fn2->fn_guid)
444 else if (fn1->fn_guid < fn2->fn_guid)
451 * Given the GUID of a snapshot, find its containing filesystem and
455 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
457 fsavl_node_t fn_find;
460 fn_find.fn_guid = snapguid;
462 fn = avl_find(avl, &fn_find, NULL);
465 *snapname = fn->fn_snapname;
466 return (fn->fn_nvfs);
472 fsavl_destroy(avl_tree_t *avl)
481 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
488 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
491 fsavl_create(nvlist_t *fss)
494 nvpair_t *fselem = NULL;
496 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
499 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
500 offsetof(fsavl_node_t, fn_node));
502 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
503 nvlist_t *nvfs, *snaps;
504 nvpair_t *snapelem = NULL;
506 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
507 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
510 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
514 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
515 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
516 fsavl_destroy(fsavl);
520 fn->fn_snapname = nvpair_name(snapelem);
524 * Note: if there are multiple snaps with the
525 * same GUID, we ignore all but one.
527 if (avl_find(fsavl, fn, NULL) == NULL)
538 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
540 typedef struct send_data {
541 uint64_t parent_fromsnap_guid;
542 nvlist_t *parent_snaps;
545 const char *fromsnap;
550 * The header nvlist is of the following format:
553 * "fromsnap" -> string (if incremental)
557 * "name" -> string (full name; for debugging)
558 * "parentfromsnap" -> number (guid of fromsnap in parent)
560 * "props" -> { name -> value (only if set here) }
561 * "snaps" -> { name (lastname) -> number (guid) }
562 * "snapprops" -> { name (lastname) -> { name -> value } }
564 * "origin" -> number (guid) (if clone)
565 * "sent" -> boolean (not on-disk)
573 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
576 send_iterate_snap(zfs_handle_t *zhp, void *arg)
578 send_data_t *sd = arg;
579 uint64_t guid = zhp->zfs_dmustats.dds_guid;
583 snapname = strrchr(zhp->zfs_name, '@')+1;
585 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
587 * NB: if there is no fromsnap here (it's a newly created fs in
588 * an incremental replication), we will substitute the tosnap.
590 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
591 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
592 strcmp(snapname, sd->tosnap) == 0)) {
593 sd->parent_fromsnap_guid = guid;
596 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
597 send_iterate_prop(zhp, nv);
598 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
606 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
608 nvpair_t *elem = NULL;
610 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
611 char *propname = nvpair_name(elem);
612 zfs_prop_t prop = zfs_name_to_prop(propname);
615 if (!zfs_prop_user(propname)) {
617 * Realistically, this should never happen. However,
618 * we want the ability to add DSL properties without
619 * needing to make incompatible version changes. We
620 * need to ignore unknown properties to allow older
621 * software to still send datasets containing these
622 * properties, with the unknown properties elided.
624 if (prop == ZPROP_INVAL)
627 if (zfs_prop_readonly(prop))
631 verify(nvpair_value_nvlist(elem, &propnv) == 0);
632 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
633 prop == ZFS_PROP_REFQUOTA ||
634 prop == ZFS_PROP_REFRESERVATION) {
637 verify(nvlist_lookup_uint64(propnv,
638 ZPROP_VALUE, &value) == 0);
639 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
642 * May have no source before SPA_VERSION_RECVD_PROPS,
643 * but is still modifiable.
645 if (nvlist_lookup_string(propnv,
646 ZPROP_SOURCE, &source) == 0) {
647 if ((strcmp(source, zhp->zfs_name) != 0) &&
649 ZPROP_SOURCE_VAL_RECVD) != 0))
654 if (nvlist_lookup_string(propnv,
655 ZPROP_SOURCE, &source) != 0)
657 if ((strcmp(source, zhp->zfs_name) != 0) &&
658 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
662 if (zfs_prop_user(propname) ||
663 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
665 verify(nvlist_lookup_string(propnv,
666 ZPROP_VALUE, &value) == 0);
667 VERIFY(0 == nvlist_add_string(nv, propname, value));
670 verify(nvlist_lookup_uint64(propnv,
671 ZPROP_VALUE, &value) == 0);
672 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
678 * recursively generate nvlists describing datasets. See comment
679 * for the data structure send_data_t above for description of contents
683 send_iterate_fs(zfs_handle_t *zhp, void *arg)
685 send_data_t *sd = arg;
688 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
689 uint64_t guid = zhp->zfs_dmustats.dds_guid;
692 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
693 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
694 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
695 sd->parent_fromsnap_guid));
697 if (zhp->zfs_dmustats.dds_origin[0]) {
698 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
699 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
702 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
703 origin->zfs_dmustats.dds_guid));
706 /* iterate over props */
707 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
708 send_iterate_prop(zhp, nv);
709 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
712 /* iterate over snaps, and set sd->parent_fromsnap_guid */
713 sd->parent_fromsnap_guid = 0;
714 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
715 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
716 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
717 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
718 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
719 nvlist_free(sd->parent_snaps);
720 nvlist_free(sd->snapprops);
722 /* add this fs to nvlist */
723 (void) snprintf(guidstring, sizeof (guidstring),
724 "0x%llx", (longlong_t)guid);
725 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
728 /* iterate over children */
730 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
732 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
739 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
740 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
743 send_data_t sd = { 0 };
746 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
748 return (EZFS_BADTYPE);
750 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
751 sd.fromsnap = fromsnap;
753 sd.recursive = recursive;
755 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
763 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
774 * Routines for dealing with the sorted snapshot functionality
776 typedef struct zfs_node {
777 zfs_handle_t *zn_handle;
778 avl_node_t zn_avlnode;
782 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
784 avl_tree_t *avl = data;
788 search.zn_handle = zhp;
789 node = avl_find(avl, &search, NULL);
792 * If this snapshot was renamed while we were creating the
793 * AVL tree, it's possible that we already inserted it under
794 * its old name. Remove the old handle before adding the new
797 zfs_close(node->zn_handle);
798 avl_remove(avl, node);
802 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
803 node->zn_handle = zhp;
810 zfs_snapshot_compare(const void *larg, const void *rarg)
812 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
813 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
814 uint64_t lcreate, rcreate;
817 * Sort them according to creation time. We use the hidden
818 * CREATETXG property to get an absolute ordering of snapshots.
820 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
821 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
823 if (lcreate < rcreate)
825 else if (lcreate > rcreate)
832 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
839 avl_create(&avl, zfs_snapshot_compare,
840 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
842 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
844 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
845 ret |= callback(node->zn_handle, data);
847 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
856 * Routines specific to "zfs send"
858 typedef struct send_dump_data {
859 /* these are all just the short snapname (the part after the @) */
860 const char *fromsnap;
862 char prevsnap[ZFS_MAXNAMELEN];
863 uint64_t prevsnap_obj;
864 boolean_t seenfrom, seento, replicate, doall, fromorigin;
870 snapfilter_cb_t *filter_cb;
873 char holdtag[ZFS_MAXNAMELEN];
878 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
879 * NULL) to the file descriptor specified by outfd.
882 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
883 boolean_t fromorigin, int outfd, nvlist_t *debugnv)
885 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
886 libzfs_handle_t *hdl = zhp->zfs_hdl;
889 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
890 assert(fromsnap_obj == 0 || !fromorigin);
892 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
893 zc.zc_cookie = outfd;
894 zc.zc_obj = fromorigin;
895 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
896 zc.zc_fromobj = fromsnap_obj;
898 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
899 if (fromsnap && fromsnap[0] != '\0') {
900 VERIFY(0 == nvlist_add_string(thisdbg,
901 "fromsnap", fromsnap));
904 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
906 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
907 "warning: cannot send '%s'"), zhp->zfs_name);
909 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
911 VERIFY(0 == nvlist_add_nvlist(debugnv,
912 zhp->zfs_name, thisdbg));
914 nvlist_free(thisdbg);
919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
920 "not an earlier snapshot from the same fs"));
921 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
924 if (zfs_dataset_exists(hdl, zc.zc_name,
925 ZFS_TYPE_SNAPSHOT)) {
926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
927 "incremental source (@%s) does not exist"),
930 return (zfs_error(hdl, EZFS_NOENT, errbuf));
943 zfs_error_aux(hdl, strerror(errno));
944 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
947 return (zfs_standard_error(hdl, errno, errbuf));
952 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
953 nvlist_free(thisdbg);
959 hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
965 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
968 * zfs_send() only opens a cleanup_fd for sends that need it,
969 * e.g. replication and doall.
971 if (sdd->cleanup_fd == -1)
974 thissnap = strchr(zhp->zfs_name, '@') + 1;
975 *(thissnap - 1) = '\0';
976 pzhp = zfs_open(zhp->zfs_hdl, zhp->zfs_name, ZFS_TYPE_DATASET);
977 *(thissnap - 1) = '@';
980 * It's OK if the parent no longer exists. The send code will
984 error = zfs_hold(pzhp, thissnap, sdd->holdtag,
985 B_FALSE, B_TRUE, B_TRUE, sdd->cleanup_fd,
986 zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID),
987 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG));
995 dump_snapshot(zfs_handle_t *zhp, void *arg)
997 send_dump_data_t *sdd = arg;
1000 boolean_t isfromsnap, istosnap;
1001 boolean_t exclude = B_FALSE;
1003 thissnap = strchr(zhp->zfs_name, '@') + 1;
1004 isfromsnap = (sdd->fromsnap != NULL &&
1005 strcmp(sdd->fromsnap, thissnap) == 0);
1007 if (!sdd->seenfrom && isfromsnap) {
1008 err = hold_for_send(zhp, sdd);
1010 sdd->seenfrom = B_TRUE;
1011 (void) strcpy(sdd->prevsnap, thissnap);
1012 sdd->prevsnap_obj = zfs_prop_get_int(zhp,
1014 } else if (err == ENOENT) {
1021 if (sdd->seento || !sdd->seenfrom) {
1026 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1028 sdd->seento = B_TRUE;
1030 if (!sdd->doall && !isfromsnap && !istosnap) {
1031 if (sdd->replicate) {
1033 nvlist_t *snapprops;
1035 * Filter out all intermediate snapshots except origin
1036 * snapshots needed to replicate clones.
1038 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1039 zhp->zfs_dmustats.dds_guid, &snapname);
1041 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1042 "snapprops", &snapprops));
1043 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1044 thissnap, &snapprops));
1045 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1052 * If a filter function exists, call it to determine whether
1053 * this snapshot will be sent.
1055 if (exclude || (sdd->filter_cb != NULL &&
1056 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1058 * This snapshot is filtered out. Don't send it, and don't
1059 * set prevsnap_obj, so it will be as if this snapshot didn't
1060 * exist, and the next accepted snapshot will be sent as
1061 * an incremental from the last accepted one, or as the
1062 * first (and full) snapshot in the case of a replication,
1063 * non-incremental send.
1069 err = hold_for_send(zhp, sdd);
1079 (void) fprintf(stderr, "sending from @%s to %s\n",
1080 sdd->prevsnap, zhp->zfs_name);
1083 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1084 sdd->prevsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
1085 sdd->outfd, sdd->debugnv);
1087 (void) strcpy(sdd->prevsnap, thissnap);
1088 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1094 dump_filesystem(zfs_handle_t *zhp, void *arg)
1097 send_dump_data_t *sdd = arg;
1098 boolean_t missingfrom = B_FALSE;
1099 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1101 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1102 zhp->zfs_name, sdd->tosnap);
1103 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1104 (void) fprintf(stderr, "WARNING: "
1105 "could not send %s@%s: does not exist\n",
1106 zhp->zfs_name, sdd->tosnap);
1111 if (sdd->replicate && sdd->fromsnap) {
1113 * If this fs does not have fromsnap, and we're doing
1114 * recursive, we need to send a full stream from the
1115 * beginning (or an incremental from the origin if this
1116 * is a clone). If we're doing non-recursive, then let
1117 * them get the error.
1119 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1120 zhp->zfs_name, sdd->fromsnap);
1121 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1122 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1123 missingfrom = B_TRUE;
1127 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1128 sdd->prevsnap_obj = 0;
1129 if (sdd->fromsnap == NULL || missingfrom)
1130 sdd->seenfrom = B_TRUE;
1132 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1133 if (!sdd->seenfrom) {
1134 (void) fprintf(stderr,
1135 "WARNING: could not send %s@%s:\n"
1136 "incremental source (%s@%s) does not exist\n",
1137 zhp->zfs_name, sdd->tosnap,
1138 zhp->zfs_name, sdd->fromsnap);
1140 } else if (!sdd->seento) {
1141 if (sdd->fromsnap) {
1142 (void) fprintf(stderr,
1143 "WARNING: could not send %s@%s:\n"
1144 "incremental source (%s@%s) "
1145 "is not earlier than it\n",
1146 zhp->zfs_name, sdd->tosnap,
1147 zhp->zfs_name, sdd->fromsnap);
1149 (void) fprintf(stderr, "WARNING: "
1150 "could not send %s@%s: does not exist\n",
1151 zhp->zfs_name, sdd->tosnap);
1160 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1162 send_dump_data_t *sdd = arg;
1164 boolean_t needagain, progress;
1166 if (!sdd->replicate)
1167 return (dump_filesystem(rzhp, sdd));
1169 /* Mark the clone origin snapshots. */
1170 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1171 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1173 uint64_t origin_guid = 0;
1175 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1176 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1177 if (origin_guid != 0) {
1179 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1180 origin_guid, &snapname);
1181 if (origin_nv != NULL) {
1182 nvlist_t *snapprops;
1183 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1184 "snapprops", &snapprops));
1185 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1186 snapname, &snapprops));
1187 VERIFY(0 == nvlist_add_boolean(
1188 snapprops, "is_clone_origin"));
1193 needagain = progress = B_FALSE;
1194 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1195 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1200 uint64_t origin_guid = 0;
1202 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1203 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1206 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1207 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1209 if (origin_guid != 0) {
1210 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1212 if (origin_nv != NULL &&
1213 nvlist_lookup_boolean(origin_nv,
1214 "sent") == ENOENT) {
1216 * origin has not been sent yet;
1224 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1227 err = dump_filesystem(zhp, sdd);
1228 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1242 * Generate a send stream for the dataset identified by the argument zhp.
1244 * The content of the send stream is the snapshot identified by
1245 * 'tosnap'. Incremental streams are requested in two ways:
1246 * - from the snapshot identified by "fromsnap" (if non-null) or
1247 * - from the origin of the dataset identified by zhp, which must
1248 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1251 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1252 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1253 * if "replicate" is set. If "doall" is set, dump all the intermediate
1254 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1255 * case too. If "props" is set, send properties.
1258 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1259 sendflags_t flags, int outfd, snapfilter_cb_t filter_func,
1260 void *cb_arg, nvlist_t **debugnvp)
1263 send_dump_data_t sdd = { 0 };
1265 nvlist_t *fss = NULL;
1266 avl_tree_t *fsavl = NULL;
1267 static uint64_t holdseq;
1269 boolean_t holdsnaps = B_FALSE;
1272 dedup_arg_t dda = { 0 };
1273 int featureflags = 0;
1275 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1276 "cannot send '%s'"), zhp->zfs_name);
1278 if (fromsnap && fromsnap[0] == '\0') {
1279 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1280 "zero-length incremental source"));
1281 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1284 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1286 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1287 if (version >= ZPL_VERSION_SA) {
1288 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1292 if (zfs_spa_version(zhp, &spa_version) == 0 &&
1293 spa_version >= SPA_VERSION_USERREFS &&
1294 (flags.doall || flags.replicate))
1298 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1299 DMU_BACKUP_FEATURE_DEDUPPROPS);
1300 if ((err = pipe(pipefd))) {
1301 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1302 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1305 dda.outputfd = outfd;
1306 dda.inputfd = pipefd[1];
1307 dda.dedup_hdl = zhp->zfs_hdl;
1308 if ((err = pthread_create(&tid, NULL, cksummer, &dda))) {
1309 (void) close(pipefd[0]);
1310 (void) close(pipefd[1]);
1311 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1312 return (zfs_error(zhp->zfs_hdl,
1313 EZFS_THREADCREATEFAILED, errbuf));
1317 if (flags.replicate || flags.doall || flags.props) {
1318 dmu_replay_record_t drr = { 0 };
1319 char *packbuf = NULL;
1321 zio_cksum_t zc = { { 0 } };
1323 if (flags.replicate || flags.props) {
1326 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1328 VERIFY(0 == nvlist_add_string(hdrnv,
1329 "fromsnap", fromsnap));
1331 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1332 if (!flags.replicate) {
1333 VERIFY(0 == nvlist_add_boolean(hdrnv,
1337 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1338 fromsnap, tosnap, flags.replicate, &fss, &fsavl);
1341 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1342 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1349 fsavl_destroy(fsavl);
1355 /* write first begin record */
1356 drr.drr_type = DRR_BEGIN;
1357 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1358 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.drr_versioninfo,
1359 DMU_COMPOUNDSTREAM);
1360 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.drr_versioninfo,
1362 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1363 sizeof (drr.drr_u.drr_begin.drr_toname),
1364 "%s@%s", zhp->zfs_name, tosnap);
1365 drr.drr_payloadlen = buflen;
1366 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
1368 /* write header nvlist */
1369 if (err != -1 && packbuf != NULL) {
1370 err = cksum_and_write(packbuf, buflen, &zc, outfd);
1374 fsavl_destroy(fsavl);
1380 /* write end record */
1382 bzero(&drr, sizeof (drr));
1383 drr.drr_type = DRR_END;
1384 drr.drr_u.drr_end.drr_checksum = zc;
1385 err = write(outfd, &drr, sizeof (drr));
1387 fsavl_destroy(fsavl);
1395 /* dump each stream */
1396 sdd.fromsnap = fromsnap;
1397 sdd.tosnap = tosnap;
1399 sdd.outfd = pipefd[0];
1402 sdd.replicate = flags.replicate;
1403 sdd.doall = flags.doall;
1404 sdd.fromorigin = flags.fromorigin;
1407 sdd.verbose = flags.verbose;
1408 sdd.filter_cb = filter_func;
1409 sdd.filter_cb_arg = cb_arg;
1411 sdd.debugnv = *debugnvp;
1414 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1415 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1416 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
1417 if (sdd.cleanup_fd < 0) {
1422 sdd.cleanup_fd = -1;
1424 err = dump_filesystems(zhp, &sdd);
1425 fsavl_destroy(fsavl);
1429 (void) close(pipefd[0]);
1430 (void) pthread_join(tid, NULL);
1433 if (sdd.cleanup_fd != -1) {
1434 VERIFY(0 == close(sdd.cleanup_fd));
1435 sdd.cleanup_fd = -1;
1438 if (flags.replicate || flags.doall || flags.props) {
1440 * write final end record. NB: want to do this even if
1441 * there was some error, because it might not be totally
1444 dmu_replay_record_t drr = { 0 };
1445 drr.drr_type = DRR_END;
1446 if (write(outfd, &drr, sizeof (drr)) == -1) {
1447 return (zfs_standard_error(zhp->zfs_hdl,
1452 return (err || sdd.err);
1455 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1457 if (sdd.cleanup_fd != -1)
1458 VERIFY(0 == close(sdd.cleanup_fd));
1460 (void) pthread_cancel(tid);
1461 (void) pthread_join(tid, NULL);
1462 (void) close(pipefd[0]);
1468 * Routines specific to "zfs recv"
1472 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1473 boolean_t byteswap, zio_cksum_t *zc)
1480 rv = read(fd, cp, len);
1485 if (rv < 0 || len != 0) {
1486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1487 "failed to read from stream"));
1488 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1489 "cannot receive")));
1494 fletcher_4_incremental_byteswap(buf, ilen, zc);
1496 fletcher_4_incremental_native(buf, ilen, zc);
1502 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1503 boolean_t byteswap, zio_cksum_t *zc)
1508 buf = zfs_alloc(hdl, len);
1512 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1518 err = nvlist_unpack(buf, len, nvp, 0);
1521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1522 "stream (malformed nvlist)"));
1529 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1530 int baselen, char *newname, recvflags_t flags)
1533 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1535 prop_changelist_t *clp;
1538 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1541 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1542 flags.force ? MS_FORCE : 0);
1546 err = changelist_prefix(clp);
1550 zc.zc_objset_type = DMU_OST_ZFS;
1551 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1554 (void) strcpy(newname, tryname);
1556 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1558 if (flags.verbose) {
1559 (void) printf("attempting rename %s to %s\n",
1560 zc.zc_name, zc.zc_value);
1562 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1564 changelist_rename(clp, name, tryname);
1569 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
1572 (void) strncpy(newname, name, baselen);
1573 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
1574 "recv-%ld-%u", (long) getpid(), seq);
1575 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1577 if (flags.verbose) {
1578 (void) printf("failed - trying rename %s to %s\n",
1579 zc.zc_name, zc.zc_value);
1581 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1583 changelist_rename(clp, name, newname);
1584 if (err && flags.verbose) {
1585 (void) printf("failed (%u) - "
1586 "will try again on next pass\n", errno);
1589 } else if (flags.verbose) {
1591 (void) printf("success\n");
1593 (void) printf("failed (%u)\n", errno);
1596 (void) changelist_postfix(clp);
1597 changelist_free(clp);
1603 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1604 char *newname, recvflags_t flags)
1606 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1608 prop_changelist_t *clp;
1610 boolean_t defer = B_FALSE;
1613 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1616 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1617 flags.force ? MS_FORCE : 0);
1618 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1619 zfs_spa_version(zhp, &spa_version) == 0 &&
1620 spa_version >= SPA_VERSION_USERREFS)
1625 err = changelist_prefix(clp);
1629 zc.zc_objset_type = DMU_OST_ZFS;
1630 zc.zc_defer_destroy = defer;
1631 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1634 (void) printf("attempting destroy %s\n", zc.zc_name);
1635 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1638 (void) printf("success\n");
1639 changelist_remove(clp, zc.zc_name);
1642 (void) changelist_postfix(clp);
1643 changelist_free(clp);
1646 * Deferred destroy might destroy the snapshot or only mark it to be
1647 * destroyed later, and it returns success in either case.
1649 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1650 ZFS_TYPE_SNAPSHOT))) {
1651 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1657 typedef struct guid_to_name_data {
1660 } guid_to_name_data_t;
1663 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1665 guid_to_name_data_t *gtnd = arg;
1668 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1669 (void) strcpy(gtnd->name, zhp->zfs_name);
1673 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1679 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1682 /* exhaustive search all local snapshots */
1683 guid_to_name_data_t gtnd;
1691 if (strchr(parent, '@') == NULL) {
1692 zhp = make_dataset_handle(hdl, parent);
1694 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1701 cp = strchr(parent, '/');
1704 zhp = make_dataset_handle(hdl, parent);
1709 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1713 return (err == EEXIST ? 0 : ENOENT);
1718 * Return true if dataset guid1 is created before guid2.
1721 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1722 uint64_t guid1, uint64_t guid2)
1725 char *fsname, *snapname;
1726 char buf[ZFS_MAXNAMELEN];
1728 zfs_node_t zn1, zn2;
1735 nvfs = fsavl_find(avl, guid1, &snapname);
1736 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1737 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1738 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1739 if (zn1.zn_handle == NULL)
1742 nvfs = fsavl_find(avl, guid2, &snapname);
1743 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1744 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1745 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1746 if (zn2.zn_handle == NULL) {
1747 zfs_close(zn2.zn_handle);
1751 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1753 zfs_close(zn1.zn_handle);
1754 zfs_close(zn2.zn_handle);
1760 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1761 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
1765 avl_tree_t *local_avl;
1766 nvpair_t *fselem, *nextfselem;
1768 char newname[ZFS_MAXNAMELEN];
1770 boolean_t needagain, progress, recursive;
1773 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1775 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
1782 needagain = progress = B_FALSE;
1784 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1785 recursive, &local_nv, &local_avl)) != 0)
1789 * Process deletes and renames
1791 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1792 fselem; fselem = nextfselem) {
1793 nvlist_t *nvfs, *snaps;
1794 nvlist_t *stream_nvfs = NULL;
1795 nvpair_t *snapelem, *nextsnapelem;
1796 uint64_t fromguid = 0;
1797 uint64_t originguid = 0;
1798 uint64_t stream_originguid = 0;
1799 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1800 char *fsname, *stream_fsname;
1802 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1804 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1805 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1806 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1807 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1808 &parent_fromsnap_guid));
1809 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1812 * First find the stream's fs, so we can check for
1813 * a different origin (due to "zfs promote")
1815 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1816 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1819 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1820 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1822 if (stream_nvfs != NULL)
1826 /* check for promote */
1827 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1828 &stream_originguid);
1829 if (stream_nvfs && originguid != stream_originguid) {
1830 switch (created_before(hdl, local_avl,
1831 stream_originguid, originguid)) {
1834 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1835 nvlist_t *origin_nvfs;
1836 char *origin_fsname;
1839 (void) printf("promoting %s\n", fsname);
1841 origin_nvfs = fsavl_find(local_avl, originguid,
1843 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1844 "name", &origin_fsname));
1845 (void) strlcpy(zc.zc_value, origin_fsname,
1846 sizeof (zc.zc_value));
1847 (void) strlcpy(zc.zc_name, fsname,
1848 sizeof (zc.zc_name));
1849 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1857 fsavl_destroy(local_avl);
1858 nvlist_free(local_nv);
1862 * We had/have the wrong origin, therefore our
1863 * list of snapshots is wrong. Need to handle
1864 * them on the next pass.
1870 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1871 snapelem; snapelem = nextsnapelem) {
1873 char *stream_snapname;
1874 nvlist_t *found, *props;
1876 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1878 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1879 found = fsavl_find(stream_avl, thisguid,
1882 /* check for delete */
1883 if (found == NULL) {
1884 char name[ZFS_MAXNAMELEN];
1889 (void) snprintf(name, sizeof (name), "%s@%s",
1890 fsname, nvpair_name(snapelem));
1892 error = recv_destroy(hdl, name,
1893 strlen(fsname)+1, newname, flags);
1901 stream_nvfs = found;
1903 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1904 &props) && 0 == nvlist_lookup_nvlist(props,
1905 stream_snapname, &props)) {
1906 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1908 zc.zc_cookie = B_TRUE; /* received */
1909 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1910 "%s@%s", fsname, nvpair_name(snapelem));
1911 if (zcmd_write_src_nvlist(hdl, &zc,
1913 (void) zfs_ioctl(hdl,
1914 ZFS_IOC_SET_PROP, &zc);
1915 zcmd_free_nvlists(&zc);
1919 /* check for different snapname */
1920 if (strcmp(nvpair_name(snapelem),
1921 stream_snapname) != 0) {
1922 char name[ZFS_MAXNAMELEN];
1923 char tryname[ZFS_MAXNAMELEN];
1925 (void) snprintf(name, sizeof (name), "%s@%s",
1926 fsname, nvpair_name(snapelem));
1927 (void) snprintf(tryname, sizeof (name), "%s@%s",
1928 fsname, stream_snapname);
1930 error = recv_rename(hdl, name, tryname,
1931 strlen(fsname)+1, newname, flags);
1938 if (strcmp(stream_snapname, fromsnap) == 0)
1939 fromguid = thisguid;
1942 /* check for delete */
1943 if (stream_nvfs == NULL) {
1947 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1956 if (fromguid == 0) {
1957 if (flags.verbose) {
1958 (void) printf("local fs %s does not have "
1959 "fromsnap (%s in stream); must have "
1960 "been deleted locally; ignoring\n",
1966 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1967 "name", &stream_fsname));
1968 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1969 "parentfromsnap", &stream_parent_fromsnap_guid));
1971 s1 = strrchr(fsname, '/');
1972 s2 = strrchr(stream_fsname, '/');
1975 * Check for rename. If the exact receive path is specified, it
1976 * does not count as a rename, but we still need to check the
1977 * datasets beneath it.
1979 if ((stream_parent_fromsnap_guid != 0 &&
1980 parent_fromsnap_guid != 0 &&
1981 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1982 ((flags.isprefix || strcmp(tofs, fsname) != 0) &&
1983 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
1985 char tryname[ZFS_MAXNAMELEN];
1987 parent = fsavl_find(local_avl,
1988 stream_parent_fromsnap_guid, NULL);
1990 * NB: parent might not be found if we used the
1991 * tosnap for stream_parent_fromsnap_guid,
1992 * because the parent is a newly-created fs;
1993 * we'll be able to rename it after we recv the
1996 if (parent != NULL) {
1999 VERIFY(0 == nvlist_lookup_string(parent, "name",
2001 (void) snprintf(tryname, sizeof (tryname),
2002 "%s%s", pname, strrchr(stream_fsname, '/'));
2005 if (flags.verbose) {
2006 (void) printf("local fs %s new parent "
2007 "not found\n", fsname);
2013 error = recv_rename(hdl, fsname, tryname,
2014 strlen(tofs)+1, newname, flags);
2016 if (renamed != NULL && newname[0] != '\0') {
2017 VERIFY(0 == nvlist_add_boolean(renamed,
2028 fsavl_destroy(local_avl);
2029 nvlist_free(local_nv);
2031 if (needagain && progress) {
2032 /* do another pass to fix up temporary names */
2034 (void) printf("another pass:\n");
2042 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2043 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2044 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2046 nvlist_t *stream_nv = NULL;
2047 avl_tree_t *stream_avl = NULL;
2048 char *fromsnap = NULL;
2050 char tofs[ZFS_MAXNAMELEN];
2051 char sendfs[ZFS_MAXNAMELEN];
2053 dmu_replay_record_t drre;
2055 boolean_t anyerr = B_FALSE;
2056 boolean_t softerr = B_FALSE;
2057 boolean_t recursive;
2059 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2062 assert(drr->drr_type == DRR_BEGIN);
2063 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2064 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2065 DMU_COMPOUNDSTREAM);
2068 * Read in the nvlist from the stream.
2070 if (drr->drr_payloadlen != 0) {
2071 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2072 &stream_nv, flags.byteswap, zc);
2074 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2079 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2082 if (recursive && strchr(destname, '@')) {
2083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2084 "cannot specify snapshot name for multi-snapshot stream"));
2085 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2090 * Read in the end record and verify checksum.
2092 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2093 flags.byteswap, NULL)))
2095 if (flags.byteswap) {
2096 drre.drr_type = BSWAP_32(drre.drr_type);
2097 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2098 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2099 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2100 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2101 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2102 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2103 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2104 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2106 if (drre.drr_type != DRR_END) {
2107 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2110 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2112 "incorrect header checksum"));
2113 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2117 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2119 if (drr->drr_payloadlen != 0) {
2120 nvlist_t *stream_fss;
2122 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2124 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2126 "couldn't allocate avl tree"));
2127 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2131 if (fromsnap != NULL) {
2132 nvlist_t *renamed = NULL;
2133 nvpair_t *pair = NULL;
2135 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2136 if (flags.isprefix) {
2137 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2141 cp = strrchr(drrb->drr_toname, '/');
2143 (void) strlcat(tofs, "/",
2147 i = (cp - drrb->drr_toname);
2150 i = strcspn(drrb->drr_toname, "/@");
2152 /* zfs_receive_one() will create_parents() */
2153 (void) strlcat(tofs, &drrb->drr_toname[i],
2155 *strchr(tofs, '@') = '\0';
2158 if (recursive && !flags.dryrun && !flags.nomount) {
2159 VERIFY(0 == nvlist_alloc(&renamed,
2160 NV_UNIQUE_NAME, 0));
2163 softerr = recv_incremental_replication(hdl, tofs, flags,
2164 stream_nv, stream_avl, renamed);
2166 /* Unmount renamed filesystems before receiving. */
2167 while ((pair = nvlist_next_nvpair(renamed,
2170 prop_changelist_t *clp = NULL;
2172 zhp = zfs_open(hdl, nvpair_name(pair),
2173 ZFS_TYPE_FILESYSTEM);
2175 clp = changelist_gather(zhp,
2176 ZFS_PROP_MOUNTPOINT, 0, 0);
2180 changelist_prefix(clp);
2181 changelist_free(clp);
2186 nvlist_free(renamed);
2191 * Get the fs specified by the first path in the stream (the top level
2192 * specified by 'zfs send') and pass it to each invocation of
2193 * zfs_receive_one().
2195 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2197 if ((cp = strchr(sendfs, '@')) != NULL)
2200 /* Finally, receive each contained stream */
2203 * we should figure out if it has a recoverable
2204 * error, in which case do a recv_skip() and drive on.
2205 * Note, if we fail due to already having this guid,
2206 * zfs_receive_one() will take care of it (ie,
2207 * recv_skip() and return 0).
2209 error = zfs_receive_impl(hdl, destname, flags, fd,
2210 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2212 if (error == ENODATA) {
2217 } while (error == 0);
2219 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2221 * Now that we have the fs's they sent us, try the
2224 softerr = recv_incremental_replication(hdl, tofs, flags,
2225 stream_nv, stream_avl, NULL);
2229 fsavl_destroy(stream_avl);
2231 nvlist_free(stream_nv);
2240 trunc_prop_errs(int truncated)
2242 ASSERT(truncated != 0);
2245 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2246 "1 more property could not be set\n"));
2248 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2249 "%d more properties could not be set\n"), truncated);
2253 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2255 dmu_replay_record_t *drr;
2256 void *buf = malloc(1<<20);
2259 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2260 "cannot receive:"));
2262 /* XXX would be great to use lseek if possible... */
2265 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2266 byteswap, NULL) == 0) {
2268 drr->drr_type = BSWAP_32(drr->drr_type);
2270 switch (drr->drr_type) {
2272 /* NB: not to be used on v2 stream packages */
2273 if (drr->drr_payloadlen != 0) {
2274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2275 "invalid substream header"));
2276 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2286 drr->drr_u.drr_object.drr_bonuslen =
2287 BSWAP_32(drr->drr_u.drr_object.
2290 (void) recv_read(hdl, fd, buf,
2291 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2297 drr->drr_u.drr_write.drr_length =
2298 BSWAP_64(drr->drr_u.drr_write.drr_length);
2300 (void) recv_read(hdl, fd, buf,
2301 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2305 drr->drr_u.drr_write.drr_length =
2306 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2308 (void) recv_read(hdl, fd, buf,
2309 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2311 case DRR_WRITE_BYREF:
2312 case DRR_FREEOBJECTS:
2317 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2318 "invalid record type"));
2319 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2328 * Restores a backup of tosnap from the file descriptor specified by infd.
2331 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2332 recvflags_t flags, dmu_replay_record_t *drr,
2333 dmu_replay_record_t *drr_noswap, const char *sendfs,
2334 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2335 uint64_t *action_handlep)
2337 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2339 int ioctl_err, ioctl_errno, err;
2341 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2343 char prop_errbuf[1024];
2344 const char *chopprefix;
2345 boolean_t newfs = B_FALSE;
2346 boolean_t stream_wantsnewfs;
2347 uint64_t parent_snapguid = 0;
2348 prop_changelist_t *clp = NULL;
2349 nvlist_t *snapprops_nvlist = NULL;
2350 zprop_errflags_t prop_errflags;
2351 boolean_t recursive;
2353 begin_time = time(NULL);
2355 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2358 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2361 if (stream_avl != NULL) {
2363 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2368 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2370 err = nvlist_lookup_nvlist(fs, "props", &props);
2372 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2374 if (flags.canmountoff) {
2375 VERIFY(0 == nvlist_add_uint64(props,
2376 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2378 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2382 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2383 VERIFY(0 == nvlist_lookup_nvlist(props,
2384 snapname, &snapprops_nvlist));
2394 * Determine how much of the snapshot name stored in the stream
2395 * we are going to tack on to the name they specified on the
2396 * command line, and how much we are going to chop off.
2398 * If they specified a snapshot, chop the entire name stored in
2403 * A filesystem was specified with -e. We want to tack on only
2404 * the tail of the sent snapshot path.
2406 if (strchr(tosnap, '@')) {
2407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2408 "argument - snapshot not allowed with -e"));
2409 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2412 chopprefix = strrchr(sendfs, '/');
2414 if (chopprefix == NULL) {
2416 * The tail is the poolname, so we need to
2417 * prepend a path separator.
2419 int len = strlen(drrb->drr_toname);
2420 cp = malloc(len + 2);
2422 (void) strcpy(&cp[1], drrb->drr_toname);
2425 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2427 } else if (flags.isprefix) {
2429 * A filesystem was specified with -d. We want to tack on
2430 * everything but the first element of the sent snapshot path
2431 * (all but the pool name).
2433 if (strchr(tosnap, '@')) {
2434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2435 "argument - snapshot not allowed with -d"));
2436 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2439 chopprefix = strchr(drrb->drr_toname, '/');
2440 if (chopprefix == NULL)
2441 chopprefix = strchr(drrb->drr_toname, '@');
2442 } else if (strchr(tosnap, '@') == NULL) {
2444 * If a filesystem was specified without -d or -e, we want to
2445 * tack on everything after the fs specified by 'zfs send'.
2447 chopprefix = drrb->drr_toname + strlen(sendfs);
2449 /* A snapshot was specified as an exact path (no -d or -e). */
2451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2452 "cannot specify snapshot name for multi-snapshot "
2454 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2456 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2459 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2460 ASSERT(chopprefix > drrb->drr_toname);
2461 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2462 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2463 chopprefix[0] == '\0');
2466 * Determine name of destination snapshot, store in zc_value.
2468 (void) strcpy(zc.zc_top_ds, tosnap);
2469 (void) strcpy(zc.zc_value, tosnap);
2470 (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2472 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2473 zcmd_free_nvlists(&zc);
2474 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2478 * Determine the name of the origin snapshot, store in zc_string.
2480 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2481 if (guid_to_name(hdl, tosnap,
2482 drrb->drr_fromguid, zc.zc_string) != 0) {
2483 zcmd_free_nvlists(&zc);
2484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2485 "local origin for clone %s does not exist"),
2487 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2490 (void) printf("found clone origin %s\n", zc.zc_string);
2493 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2494 (drrb->drr_flags & DRR_FLAG_CLONE));
2496 if (stream_wantsnewfs) {
2498 * if the parent fs does not exist, look for it based on
2499 * the parent snap GUID
2501 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2502 "cannot receive new filesystem stream"));
2504 (void) strcpy(zc.zc_name, zc.zc_value);
2505 cp = strrchr(zc.zc_name, '/');
2509 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2510 char suffix[ZFS_MAXNAMELEN];
2511 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2512 if (guid_to_name(hdl, tosnap, parent_snapguid,
2513 zc.zc_value) == 0) {
2514 *strchr(zc.zc_value, '@') = '\0';
2515 (void) strcat(zc.zc_value, suffix);
2520 * if the fs does not exist, look for it based on the
2523 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2524 "cannot receive incremental stream"));
2526 (void) strcpy(zc.zc_name, zc.zc_value);
2527 *strchr(zc.zc_name, '@') = '\0';
2530 * If the exact receive path was specified and this is the
2531 * topmost path in the stream, then if the fs does not exist we
2532 * should look no further.
2534 if ((flags.isprefix || (*(chopprefix = drrb->drr_toname +
2535 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2536 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2537 char snap[ZFS_MAXNAMELEN];
2538 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2539 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
2540 zc.zc_value) == 0) {
2541 *strchr(zc.zc_value, '@') = '\0';
2542 (void) strcat(zc.zc_value, snap);
2547 (void) strcpy(zc.zc_name, zc.zc_value);
2548 *strchr(zc.zc_name, '@') = '\0';
2550 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2554 * Destination fs exists. Therefore this should either
2555 * be an incremental, or the stream specifies a new fs
2556 * (full stream or clone) and they want us to blow it
2557 * away (and have therefore specified -F and removed any
2560 if (stream_wantsnewfs) {
2562 zcmd_free_nvlists(&zc);
2563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2564 "destination '%s' exists\n"
2565 "must specify -F to overwrite it"),
2567 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2569 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2571 zcmd_free_nvlists(&zc);
2572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2573 "destination has snapshots (eg. %s)\n"
2574 "must destroy them to overwrite it"),
2576 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2580 if ((zhp = zfs_open(hdl, zc.zc_name,
2581 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2582 zcmd_free_nvlists(&zc);
2586 if (stream_wantsnewfs &&
2587 zhp->zfs_dmustats.dds_origin[0]) {
2588 zcmd_free_nvlists(&zc);
2590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2591 "destination '%s' is a clone\n"
2592 "must destroy it to overwrite it"),
2594 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2597 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2598 stream_wantsnewfs) {
2599 /* We can't do online recv in this case */
2600 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2603 zcmd_free_nvlists(&zc);
2606 if (changelist_prefix(clp) != 0) {
2607 changelist_free(clp);
2609 zcmd_free_nvlists(&zc);
2616 * Destination filesystem does not exist. Therefore we better
2617 * be creating a new filesystem (either from a full backup, or
2618 * a clone). It would therefore be invalid if the user
2619 * specified only the pool name (i.e. if the destination name
2620 * contained no slash character).
2622 if (!stream_wantsnewfs ||
2623 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2624 zcmd_free_nvlists(&zc);
2625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2626 "destination '%s' does not exist"), zc.zc_name);
2627 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2631 * Trim off the final dataset component so we perform the
2632 * recvbackup ioctl to the filesystems's parent.
2636 if (flags.isprefix && !flags.istail && !flags.dryrun &&
2637 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2638 zcmd_free_nvlists(&zc);
2639 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2645 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2646 zc.zc_cookie = infd;
2647 zc.zc_guid = flags.force;
2648 if (flags.verbose) {
2649 (void) printf("%s %s stream of %s into %s\n",
2650 flags.dryrun ? "would receive" : "receiving",
2651 drrb->drr_fromguid ? "incremental" : "full",
2652 drrb->drr_toname, zc.zc_value);
2653 (void) fflush(stdout);
2657 zcmd_free_nvlists(&zc);
2658 return (recv_skip(hdl, infd, flags.byteswap));
2661 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2662 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2663 zc.zc_cleanup_fd = cleanup_fd;
2664 zc.zc_action_handle = *action_handlep;
2666 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2667 ioctl_errno = errno;
2668 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2671 nvlist_t *prop_errors;
2672 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2673 zc.zc_nvlist_dst_size, &prop_errors, 0));
2675 nvpair_t *prop_err = NULL;
2677 while ((prop_err = nvlist_next_nvpair(prop_errors,
2678 prop_err)) != NULL) {
2683 prop = zfs_name_to_prop(nvpair_name(prop_err));
2684 (void) nvpair_value_int32(prop_err, &intval);
2685 if (strcmp(nvpair_name(prop_err),
2686 ZPROP_N_MORE_ERRORS) == 0) {
2687 trunc_prop_errs(intval);
2690 (void) snprintf(tbuf, sizeof (tbuf),
2691 dgettext(TEXT_DOMAIN,
2692 "cannot receive %s property on %s"),
2693 nvpair_name(prop_err), zc.zc_name);
2694 zfs_setprop_error(hdl, prop, intval, tbuf);
2697 nvlist_free(prop_errors);
2700 zc.zc_nvlist_dst = 0;
2701 zc.zc_nvlist_dst_size = 0;
2702 zcmd_free_nvlists(&zc);
2704 if (err == 0 && snapprops_nvlist) {
2705 zfs_cmd_t zc2 = { "\0", "\0", "\0", "\0", 0 };
2707 (void) strcpy(zc2.zc_name, zc.zc_value);
2708 zc2.zc_cookie = B_TRUE; /* received */
2709 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
2710 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
2711 zcmd_free_nvlists(&zc2);
2715 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
2717 * It may be that this snapshot already exists,
2718 * in which case we want to consume & ignore it
2719 * rather than failing.
2721 avl_tree_t *local_avl;
2722 nvlist_t *local_nv, *fs;
2723 cp = strchr(zc.zc_value, '@');
2726 * XXX Do this faster by just iterating over snaps in
2727 * this fs. Also if zc_value does not exist, we will
2728 * get a strange "does not exist" error message.
2731 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
2732 &local_nv, &local_avl) == 0) {
2734 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
2735 fsavl_destroy(local_avl);
2736 nvlist_free(local_nv);
2739 if (flags.verbose) {
2740 (void) printf("snap %s already exists; "
2741 "ignoring\n", zc.zc_value);
2743 err = ioctl_err = recv_skip(hdl, infd,
2750 if (ioctl_err != 0) {
2751 switch (ioctl_errno) {
2753 cp = strchr(zc.zc_value, '@');
2755 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2756 "most recent snapshot of %s does not\n"
2757 "match incremental source"), zc.zc_value);
2758 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2763 "destination %s has been modified\n"
2764 "since most recent snapshot"), zc.zc_name);
2765 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2768 cp = strchr(zc.zc_value, '@');
2770 /* it's the containing fs that exists */
2773 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2774 "destination already exists"));
2775 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
2776 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
2781 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2784 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2785 "invalid stream (checksum mismatch)"));
2786 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2789 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2790 "pool must be upgraded to receive this stream."));
2791 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
2794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2795 "destination %s space quota exceeded"), zc.zc_name);
2796 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2799 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
2804 * Mount the target filesystem (if created). Also mount any
2805 * children of the target filesystem if we did a replication
2806 * receive (indicated by stream_avl being non-NULL).
2808 cp = strchr(zc.zc_value, '@');
2809 if (cp && (ioctl_err == 0 || !newfs)) {
2813 h = zfs_open(hdl, zc.zc_value,
2814 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2816 if (h->zfs_type == ZFS_TYPE_VOLUME) {
2818 } else if (newfs || stream_avl) {
2820 * Track the first/top of hierarchy fs,
2821 * for mounting and sharing later.
2823 if (top_zfs && *top_zfs == NULL)
2824 *top_zfs = zfs_strdup(hdl, zc.zc_value);
2832 err |= changelist_postfix(clp);
2833 changelist_free(clp);
2836 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
2837 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2838 "failed to clear unreceived properties on %s"),
2840 (void) fprintf(stderr, "\n");
2842 if (prop_errflags & ZPROP_ERR_NORESTORE) {
2843 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2844 "failed to restore original properties on %s"),
2846 (void) fprintf(stderr, "\n");
2849 if (err || ioctl_err)
2852 *action_handlep = zc.zc_action_handle;
2854 if (flags.verbose) {
2857 uint64_t bytes = zc.zc_cookie;
2858 time_t delta = time(NULL) - begin_time;
2861 zfs_nicenum(bytes, buf1, sizeof (buf1));
2862 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
2864 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
2872 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2873 int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2874 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2877 dmu_replay_record_t drr, drr_noswap;
2878 struct drr_begin *drrb = &drr.drr_u.drr_begin;
2880 zio_cksum_t zcksum = { { 0 } };
2881 uint64_t featureflags;
2884 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2887 if (flags.isprefix &&
2888 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2889 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2890 "(%s) does not exist"), tosnap);
2891 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2894 /* read in the BEGIN record */
2895 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2899 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2900 /* It's the double end record at the end of a package */
2904 /* the kernel needs the non-byteswapped begin record */
2907 flags.byteswap = B_FALSE;
2908 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2910 * We computed the checksum in the wrong byteorder in
2911 * recv_read() above; do it again correctly.
2913 bzero(&zcksum, sizeof (zio_cksum_t));
2914 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2915 flags.byteswap = B_TRUE;
2917 drr.drr_type = BSWAP_32(drr.drr_type);
2918 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2919 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2920 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
2921 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2922 drrb->drr_type = BSWAP_32(drrb->drr_type);
2923 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2924 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2925 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2928 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2930 "stream (bad magic number)"));
2931 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2934 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
2935 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
2937 if (!DMU_STREAM_SUPPORTED(featureflags) ||
2938 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
2939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2940 "stream has unsupported feature, feature flags = %lx"),
2942 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2945 if (strchr(drrb->drr_toname, '@') == NULL) {
2946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2947 "stream (bad snapshot name)"));
2948 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2951 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
2952 char nonpackage_sendfs[ZFS_MAXNAMELEN];
2953 if (sendfs == NULL) {
2955 * We were not called from zfs_receive_package(). Get
2956 * the fs specified by 'zfs send'.
2959 (void) strlcpy(nonpackage_sendfs,
2960 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
2961 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
2963 sendfs = nonpackage_sendfs;
2965 return (zfs_receive_one(hdl, infd, tosnap, flags,
2966 &drr, &drr_noswap, sendfs, stream_nv, stream_avl,
2967 top_zfs, cleanup_fd, action_handlep));
2969 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
2970 DMU_COMPOUNDSTREAM);
2971 return (zfs_receive_package(hdl, infd, tosnap, flags,
2972 &drr, &zcksum, top_zfs, cleanup_fd, action_handlep));
2977 * Restores a backup of tosnap from the file descriptor specified by infd.
2978 * Return 0 on total success, -2 if some things couldn't be
2979 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2980 * (-1 will override -2).
2983 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2984 int infd, avl_tree_t *stream_avl)
2986 char *top_zfs = NULL;
2989 uint64_t action_handle = 0;
2991 cleanup_fd = open(ZFS_DEV, O_RDWR);
2992 VERIFY(cleanup_fd >= 0);
2994 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
2995 stream_avl, &top_zfs, cleanup_fd, &action_handle);
2997 VERIFY(0 == close(cleanup_fd));
2999 if (err == 0 && !flags.nomount && top_zfs) {
3001 prop_changelist_t *clp;
3003 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3005 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3006 CL_GATHER_MOUNT_ALWAYS, 0);
3009 /* mount and share received datasets */
3010 err = changelist_postfix(clp);
3011 changelist_free(clp);
3014 if (zhp == NULL || clp == NULL || err)