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>
54 /* in libzfs_dataset.c */
55 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
57 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
58 int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
60 static const zio_cksum_t zero_cksum = { { 0 } };
62 typedef struct dedup_arg {
65 libzfs_handle_t *dedup_hdl;
68 typedef struct dataref {
74 typedef struct dedup_entry {
75 struct dedup_entry *dde_next;
76 zio_cksum_t dde_chksum;
81 #define MAX_DDT_PHYSMEM_PERCENT 20
82 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
84 typedef struct dedup_table {
85 dedup_entry_t **dedup_hash_array;
86 umem_cache_t *ddecache;
87 uint64_t max_ddt_size; /* max dedup table size in bytes */
88 uint64_t cur_ddt_size; /* current dedup table size in bytes */
95 high_order_bit(uint64_t n)
99 for (count = 0; n != 0; count++)
105 ssread(void *buf, size_t len, FILE *stream)
109 if ((outlen = fread(buf, len, 1, stream)) == 0)
116 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
117 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
121 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
122 if (ddt->ddt_full == B_FALSE) {
123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
124 "Dedup table full. Deduplication will continue "
125 "with existing table entries"));
126 ddt->ddt_full = B_TRUE;
131 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
133 assert(*ddepp == NULL);
134 dde->dde_next = NULL;
135 dde->dde_chksum = *cs;
136 dde->dde_prop = prop;
139 ddt->cur_ddt_size += sizeof (dedup_entry_t);
145 * Using the specified dedup table, do a lookup for an entry with
146 * the checksum cs. If found, return the block's reference info
147 * in *dr. Otherwise, insert a new entry in the dedup table, using
148 * the reference information specified by *dr.
150 * return value: true - entry was found
151 * false - entry was not found
154 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
155 uint64_t prop, dataref_t *dr)
158 dedup_entry_t **ddepp;
160 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
162 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
163 ddepp = &((*ddepp)->dde_next)) {
164 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
165 (*ddepp)->dde_prop == prop) {
166 *dr = (*ddepp)->dde_ref;
170 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
175 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
177 fletcher_4_incremental_native(buf, len, zc);
178 return (write(outfd, buf, len));
182 * This function is started in a separate thread when the dedup option
183 * has been requested. The main send thread determines the list of
184 * snapshots to be included in the send stream and makes the ioctl calls
185 * for each one. But instead of having the ioctl send the output to the
186 * the output fd specified by the caller of zfs_send()), the
187 * ioctl is told to direct the output to a pipe, which is read by the
188 * alternate thread running THIS function. This function does the
190 * 1. building a dedup table (the DDT)
191 * 2. doing checksums on each data block and inserting a record in the DDT
192 * 3. looking for matching checksums, and
193 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
194 * a duplicate block is found.
195 * The output of this function then goes to the output fd requested
196 * by the caller of zfs_send().
201 dedup_arg_t *dda = arg;
202 char *buf = malloc(1<<20);
203 dmu_replay_record_t thedrr;
204 dmu_replay_record_t *drr = &thedrr;
205 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
206 struct drr_end *drre = &thedrr.drr_u.drr_end;
207 struct drr_object *drro = &thedrr.drr_u.drr_object;
208 struct drr_write *drrw = &thedrr.drr_u.drr_write;
209 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
212 dmu_replay_record_t wbr_drr = {0};
213 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
215 zio_cksum_t stream_cksum;
216 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
220 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
221 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
223 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
226 * numbuckets must be a power of 2. Increase number to
227 * a power of 2 if necessary.
229 if (!ISP2(numbuckets))
230 numbuckets = 1 << high_order_bit(numbuckets);
232 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
233 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
234 NULL, NULL, NULL, NULL, NULL, 0);
235 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
236 ddt.numhashbits = high_order_bit(numbuckets) - 1;
237 ddt.ddt_full = B_FALSE;
239 /* Initialize the write-by-reference block. */
240 wbr_drr.drr_type = DRR_WRITE_BYREF;
241 wbr_drr.drr_payloadlen = 0;
243 outfd = dda->outputfd;
244 ofp = fdopen(dda->inputfd, "r");
245 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
247 switch (drr->drr_type) {
251 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
253 /* set the DEDUP feature flag for this stream */
254 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
255 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
256 DMU_BACKUP_FEATURE_DEDUPPROPS);
257 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
259 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
260 &stream_cksum, outfd) == -1)
262 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
263 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
264 int sz = drr->drr_payloadlen;
270 (void) ssread(buf, sz, ofp);
273 if (cksum_and_write(buf, sz, &stream_cksum,
282 /* use the recalculated checksum */
283 ZIO_SET_CHECKSUM(&drre->drr_checksum,
284 stream_cksum.zc_word[0], stream_cksum.zc_word[1],
285 stream_cksum.zc_word[2], stream_cksum.zc_word[3]);
286 if ((write(outfd, drr,
287 sizeof (dmu_replay_record_t))) == -1)
294 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
295 &stream_cksum, outfd) == -1)
297 if (drro->drr_bonuslen > 0) {
299 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
301 if (cksum_and_write(buf,
302 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
303 &stream_cksum, outfd) == -1)
311 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
312 &stream_cksum, outfd) == -1)
314 (void) ssread(buf, drrs->drr_length, ofp);
315 if (cksum_and_write(buf, drrs->drr_length,
316 &stream_cksum, outfd) == -1)
321 case DRR_FREEOBJECTS:
323 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
324 &stream_cksum, outfd) == -1)
333 (void) ssread(buf, drrw->drr_length, ofp);
336 * Use the existing checksum if it's dedup-capable,
337 * else calculate a SHA256 checksum for it.
340 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
342 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
343 zio_cksum_t tmpsha256;
345 zio_checksum_SHA256(buf,
346 drrw->drr_length, &tmpsha256);
348 drrw->drr_key.ddk_cksum.zc_word[0] =
349 BE_64(tmpsha256.zc_word[0]);
350 drrw->drr_key.ddk_cksum.zc_word[1] =
351 BE_64(tmpsha256.zc_word[1]);
352 drrw->drr_key.ddk_cksum.zc_word[2] =
353 BE_64(tmpsha256.zc_word[2]);
354 drrw->drr_key.ddk_cksum.zc_word[3] =
355 BE_64(tmpsha256.zc_word[3]);
356 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
357 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
360 dataref.ref_guid = drrw->drr_toguid;
361 dataref.ref_object = drrw->drr_object;
362 dataref.ref_offset = drrw->drr_offset;
364 if (ddt_update(dda->dedup_hdl, &ddt,
365 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
367 /* block already present in stream */
368 wbr_drrr->drr_object = drrw->drr_object;
369 wbr_drrr->drr_offset = drrw->drr_offset;
370 wbr_drrr->drr_length = drrw->drr_length;
371 wbr_drrr->drr_toguid = drrw->drr_toguid;
372 wbr_drrr->drr_refguid = dataref.ref_guid;
373 wbr_drrr->drr_refobject =
375 wbr_drrr->drr_refoffset =
378 wbr_drrr->drr_checksumtype =
379 drrw->drr_checksumtype;
380 wbr_drrr->drr_checksumflags =
381 drrw->drr_checksumtype;
382 wbr_drrr->drr_key.ddk_cksum =
383 drrw->drr_key.ddk_cksum;
384 wbr_drrr->drr_key.ddk_prop =
385 drrw->drr_key.ddk_prop;
387 if (cksum_and_write(&wbr_drr,
388 sizeof (dmu_replay_record_t), &stream_cksum,
392 /* block not previously seen */
393 if (cksum_and_write(drr,
394 sizeof (dmu_replay_record_t), &stream_cksum,
397 if (cksum_and_write(buf,
399 &stream_cksum, outfd) == -1)
407 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
408 &stream_cksum, outfd) == -1)
414 (void) printf("INVALID record type 0x%x\n",
416 /* should never happen, so assert */
421 umem_cache_destroy(ddt.ddecache);
422 free(ddt.dedup_hash_array);
430 * Routines for dealing with the AVL tree of fs-nvlists
432 typedef struct fsavl_node {
440 fsavl_compare(const void *arg1, const void *arg2)
442 const fsavl_node_t *fn1 = arg1;
443 const fsavl_node_t *fn2 = arg2;
445 if (fn1->fn_guid > fn2->fn_guid)
447 else if (fn1->fn_guid < fn2->fn_guid)
454 * Given the GUID of a snapshot, find its containing filesystem and
458 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
460 fsavl_node_t fn_find;
463 fn_find.fn_guid = snapguid;
465 fn = avl_find(avl, &fn_find, NULL);
468 *snapname = fn->fn_snapname;
469 return (fn->fn_nvfs);
475 fsavl_destroy(avl_tree_t *avl)
484 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
491 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
494 fsavl_create(nvlist_t *fss)
497 nvpair_t *fselem = NULL;
499 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
502 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
503 offsetof(fsavl_node_t, fn_node));
505 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
506 nvlist_t *nvfs, *snaps;
507 nvpair_t *snapelem = NULL;
509 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
510 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
513 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
517 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
518 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
519 fsavl_destroy(fsavl);
523 fn->fn_snapname = nvpair_name(snapelem);
527 * Note: if there are multiple snaps with the
528 * same GUID, we ignore all but one.
530 if (avl_find(fsavl, fn, NULL) == NULL)
541 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
543 typedef struct send_data {
544 uint64_t parent_fromsnap_guid;
545 nvlist_t *parent_snaps;
548 const char *fromsnap;
553 * The header nvlist is of the following format:
556 * "fromsnap" -> string (if incremental)
560 * "name" -> string (full name; for debugging)
561 * "parentfromsnap" -> number (guid of fromsnap in parent)
563 * "props" -> { name -> value (only if set here) }
564 * "snaps" -> { name (lastname) -> number (guid) }
565 * "snapprops" -> { name (lastname) -> { name -> value } }
567 * "origin" -> number (guid) (if clone)
568 * "sent" -> boolean (not on-disk)
576 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
579 send_iterate_snap(zfs_handle_t *zhp, void *arg)
581 send_data_t *sd = arg;
582 uint64_t guid = zhp->zfs_dmustats.dds_guid;
586 snapname = strrchr(zhp->zfs_name, '@')+1;
588 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
590 * NB: if there is no fromsnap here (it's a newly created fs in
591 * an incremental replication), we will substitute the tosnap.
593 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
594 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
595 strcmp(snapname, sd->tosnap) == 0)) {
596 sd->parent_fromsnap_guid = guid;
599 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
600 send_iterate_prop(zhp, nv);
601 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
609 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
611 nvpair_t *elem = NULL;
613 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
614 char *propname = nvpair_name(elem);
615 zfs_prop_t prop = zfs_name_to_prop(propname);
618 if (!zfs_prop_user(propname)) {
620 * Realistically, this should never happen. However,
621 * we want the ability to add DSL properties without
622 * needing to make incompatible version changes. We
623 * need to ignore unknown properties to allow older
624 * software to still send datasets containing these
625 * properties, with the unknown properties elided.
627 if (prop == ZPROP_INVAL)
630 if (zfs_prop_readonly(prop))
634 verify(nvpair_value_nvlist(elem, &propnv) == 0);
635 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
636 prop == ZFS_PROP_REFQUOTA ||
637 prop == ZFS_PROP_REFRESERVATION) {
640 verify(nvlist_lookup_uint64(propnv,
641 ZPROP_VALUE, &value) == 0);
642 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
645 * May have no source before SPA_VERSION_RECVD_PROPS,
646 * but is still modifiable.
648 if (nvlist_lookup_string(propnv,
649 ZPROP_SOURCE, &source) == 0) {
650 if ((strcmp(source, zhp->zfs_name) != 0) &&
652 ZPROP_SOURCE_VAL_RECVD) != 0))
657 if (nvlist_lookup_string(propnv,
658 ZPROP_SOURCE, &source) != 0)
660 if ((strcmp(source, zhp->zfs_name) != 0) &&
661 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
665 if (zfs_prop_user(propname) ||
666 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
668 verify(nvlist_lookup_string(propnv,
669 ZPROP_VALUE, &value) == 0);
670 VERIFY(0 == nvlist_add_string(nv, propname, value));
673 verify(nvlist_lookup_uint64(propnv,
674 ZPROP_VALUE, &value) == 0);
675 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
681 * recursively generate nvlists describing datasets. See comment
682 * for the data structure send_data_t above for description of contents
686 send_iterate_fs(zfs_handle_t *zhp, void *arg)
688 send_data_t *sd = arg;
691 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
692 uint64_t guid = zhp->zfs_dmustats.dds_guid;
695 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
696 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
697 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
698 sd->parent_fromsnap_guid));
700 if (zhp->zfs_dmustats.dds_origin[0]) {
701 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
702 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
705 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
706 origin->zfs_dmustats.dds_guid));
709 /* iterate over props */
710 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
711 send_iterate_prop(zhp, nv);
712 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
715 /* iterate over snaps, and set sd->parent_fromsnap_guid */
716 sd->parent_fromsnap_guid = 0;
717 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
718 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
719 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
720 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
721 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
722 nvlist_free(sd->parent_snaps);
723 nvlist_free(sd->snapprops);
725 /* add this fs to nvlist */
726 (void) snprintf(guidstring, sizeof (guidstring),
727 "0x%llx", (longlong_t)guid);
728 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
731 /* iterate over children */
733 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
735 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
742 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
743 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
746 send_data_t sd = { 0 };
749 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
751 return (EZFS_BADTYPE);
753 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
754 sd.fromsnap = fromsnap;
756 sd.recursive = recursive;
758 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
766 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
777 * Routines for dealing with the sorted snapshot functionality
779 typedef struct zfs_node {
780 zfs_handle_t *zn_handle;
781 avl_node_t zn_avlnode;
785 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
787 avl_tree_t *avl = data;
791 search.zn_handle = zhp;
792 node = avl_find(avl, &search, NULL);
795 * If this snapshot was renamed while we were creating the
796 * AVL tree, it's possible that we already inserted it under
797 * its old name. Remove the old handle before adding the new
800 zfs_close(node->zn_handle);
801 avl_remove(avl, node);
805 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
806 node->zn_handle = zhp;
813 zfs_snapshot_compare(const void *larg, const void *rarg)
815 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
816 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
817 uint64_t lcreate, rcreate;
820 * Sort them according to creation time. We use the hidden
821 * CREATETXG property to get an absolute ordering of snapshots.
823 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
824 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
826 if (lcreate < rcreate)
828 else if (lcreate > rcreate)
835 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
842 avl_create(&avl, zfs_snapshot_compare,
843 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
845 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
847 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
848 ret |= callback(node->zn_handle, data);
850 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
859 * Routines specific to "zfs send"
861 typedef struct send_dump_data {
862 /* these are all just the short snapname (the part after the @) */
863 const char *fromsnap;
865 char prevsnap[ZFS_MAXNAMELEN];
866 uint64_t prevsnap_obj;
867 boolean_t seenfrom, seento, replicate, doall, fromorigin;
873 snapfilter_cb_t *filter_cb;
876 char holdtag[ZFS_MAXNAMELEN];
881 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
882 * NULL) to the file descriptor specified by outfd.
885 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
886 boolean_t fromorigin, int outfd, nvlist_t *debugnv)
888 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
889 libzfs_handle_t *hdl = zhp->zfs_hdl;
892 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
893 assert(fromsnap_obj == 0 || !fromorigin);
895 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
896 zc.zc_cookie = outfd;
897 zc.zc_obj = fromorigin;
898 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
899 zc.zc_fromobj = fromsnap_obj;
901 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
902 if (fromsnap && fromsnap[0] != '\0') {
903 VERIFY(0 == nvlist_add_string(thisdbg,
904 "fromsnap", fromsnap));
907 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
909 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
910 "warning: cannot send '%s'"), zhp->zfs_name);
912 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
914 VERIFY(0 == nvlist_add_nvlist(debugnv,
915 zhp->zfs_name, thisdbg));
917 nvlist_free(thisdbg);
922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
923 "not an earlier snapshot from the same fs"));
924 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
927 if (zfs_dataset_exists(hdl, zc.zc_name,
928 ZFS_TYPE_SNAPSHOT)) {
929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
930 "incremental source (@%s) does not exist"),
933 return (zfs_error(hdl, EZFS_NOENT, errbuf));
946 zfs_error_aux(hdl, strerror(errno));
947 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
950 return (zfs_standard_error(hdl, errno, errbuf));
955 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
956 nvlist_free(thisdbg);
962 hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
968 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
971 * zfs_send() only opens a cleanup_fd for sends that need it,
972 * e.g. replication and doall.
974 if (sdd->cleanup_fd == -1)
977 thissnap = strchr(zhp->zfs_name, '@') + 1;
978 *(thissnap - 1) = '\0';
979 pzhp = zfs_open(zhp->zfs_hdl, zhp->zfs_name, ZFS_TYPE_DATASET);
980 *(thissnap - 1) = '@';
983 * It's OK if the parent no longer exists. The send code will
987 error = zfs_hold(pzhp, thissnap, sdd->holdtag,
988 B_FALSE, B_TRUE, B_TRUE, sdd->cleanup_fd,
989 zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID),
990 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG));
998 dump_snapshot(zfs_handle_t *zhp, void *arg)
1000 send_dump_data_t *sdd = arg;
1003 boolean_t isfromsnap, istosnap;
1004 boolean_t exclude = B_FALSE;
1006 thissnap = strchr(zhp->zfs_name, '@') + 1;
1007 isfromsnap = (sdd->fromsnap != NULL &&
1008 strcmp(sdd->fromsnap, thissnap) == 0);
1010 if (!sdd->seenfrom && isfromsnap) {
1011 err = hold_for_send(zhp, sdd);
1013 sdd->seenfrom = B_TRUE;
1014 (void) strcpy(sdd->prevsnap, thissnap);
1015 sdd->prevsnap_obj = zfs_prop_get_int(zhp,
1017 } else if (err == ENOENT) {
1024 if (sdd->seento || !sdd->seenfrom) {
1029 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1031 sdd->seento = B_TRUE;
1033 if (!sdd->doall && !isfromsnap && !istosnap) {
1034 if (sdd->replicate) {
1036 nvlist_t *snapprops;
1038 * Filter out all intermediate snapshots except origin
1039 * snapshots needed to replicate clones.
1041 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1042 zhp->zfs_dmustats.dds_guid, &snapname);
1044 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1045 "snapprops", &snapprops));
1046 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1047 thissnap, &snapprops));
1048 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1055 * If a filter function exists, call it to determine whether
1056 * this snapshot will be sent.
1058 if (exclude || (sdd->filter_cb != NULL &&
1059 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1061 * This snapshot is filtered out. Don't send it, and don't
1062 * set prevsnap_obj, so it will be as if this snapshot didn't
1063 * exist, and the next accepted snapshot will be sent as
1064 * an incremental from the last accepted one, or as the
1065 * first (and full) snapshot in the case of a replication,
1066 * non-incremental send.
1072 err = hold_for_send(zhp, sdd);
1082 (void) fprintf(stderr, "sending from @%s to %s\n",
1083 sdd->prevsnap, zhp->zfs_name);
1086 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1087 sdd->prevsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
1088 sdd->outfd, sdd->debugnv);
1090 (void) strcpy(sdd->prevsnap, thissnap);
1091 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1097 dump_filesystem(zfs_handle_t *zhp, void *arg)
1100 send_dump_data_t *sdd = arg;
1101 boolean_t missingfrom = B_FALSE;
1102 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1104 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1105 zhp->zfs_name, sdd->tosnap);
1106 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1107 (void) fprintf(stderr, "WARNING: "
1108 "could not send %s@%s: does not exist\n",
1109 zhp->zfs_name, sdd->tosnap);
1114 if (sdd->replicate && sdd->fromsnap) {
1116 * If this fs does not have fromsnap, and we're doing
1117 * recursive, we need to send a full stream from the
1118 * beginning (or an incremental from the origin if this
1119 * is a clone). If we're doing non-recursive, then let
1120 * them get the error.
1122 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1123 zhp->zfs_name, sdd->fromsnap);
1124 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1125 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1126 missingfrom = B_TRUE;
1130 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1131 sdd->prevsnap_obj = 0;
1132 if (sdd->fromsnap == NULL || missingfrom)
1133 sdd->seenfrom = B_TRUE;
1135 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1136 if (!sdd->seenfrom) {
1137 (void) fprintf(stderr,
1138 "WARNING: could not send %s@%s:\n"
1139 "incremental source (%s@%s) does not exist\n",
1140 zhp->zfs_name, sdd->tosnap,
1141 zhp->zfs_name, sdd->fromsnap);
1143 } else if (!sdd->seento) {
1144 if (sdd->fromsnap) {
1145 (void) fprintf(stderr,
1146 "WARNING: could not send %s@%s:\n"
1147 "incremental source (%s@%s) "
1148 "is not earlier than it\n",
1149 zhp->zfs_name, sdd->tosnap,
1150 zhp->zfs_name, sdd->fromsnap);
1152 (void) fprintf(stderr, "WARNING: "
1153 "could not send %s@%s: does not exist\n",
1154 zhp->zfs_name, sdd->tosnap);
1163 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1165 send_dump_data_t *sdd = arg;
1167 boolean_t needagain, progress;
1169 if (!sdd->replicate)
1170 return (dump_filesystem(rzhp, sdd));
1172 /* Mark the clone origin snapshots. */
1173 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1174 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1176 uint64_t origin_guid = 0;
1178 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1179 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1180 if (origin_guid != 0) {
1182 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1183 origin_guid, &snapname);
1184 if (origin_nv != NULL) {
1185 nvlist_t *snapprops;
1186 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1187 "snapprops", &snapprops));
1188 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1189 snapname, &snapprops));
1190 VERIFY(0 == nvlist_add_boolean(
1191 snapprops, "is_clone_origin"));
1196 needagain = progress = B_FALSE;
1197 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1198 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1203 uint64_t origin_guid = 0;
1205 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1206 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1209 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1210 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1212 if (origin_guid != 0) {
1213 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1215 if (origin_nv != NULL &&
1216 nvlist_lookup_boolean(origin_nv,
1217 "sent") == ENOENT) {
1219 * origin has not been sent yet;
1227 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1230 err = dump_filesystem(zhp, sdd);
1231 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1245 * Generate a send stream for the dataset identified by the argument zhp.
1247 * The content of the send stream is the snapshot identified by
1248 * 'tosnap'. Incremental streams are requested in two ways:
1249 * - from the snapshot identified by "fromsnap" (if non-null) or
1250 * - from the origin of the dataset identified by zhp, which must
1251 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1254 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1255 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1256 * if "replicate" is set. If "doall" is set, dump all the intermediate
1257 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1258 * case too. If "props" is set, send properties.
1261 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1262 sendflags_t flags, int outfd, snapfilter_cb_t filter_func,
1263 void *cb_arg, nvlist_t **debugnvp)
1266 send_dump_data_t sdd = { 0 };
1268 nvlist_t *fss = NULL;
1269 avl_tree_t *fsavl = NULL;
1270 static uint64_t holdseq;
1272 boolean_t holdsnaps = B_FALSE;
1275 dedup_arg_t dda = { 0 };
1276 int featureflags = 0;
1278 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1279 "cannot send '%s'"), zhp->zfs_name);
1281 if (fromsnap && fromsnap[0] == '\0') {
1282 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1283 "zero-length incremental source"));
1284 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1287 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1289 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1290 if (version >= ZPL_VERSION_SA) {
1291 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1295 if (zfs_spa_version(zhp, &spa_version) == 0 &&
1296 spa_version >= SPA_VERSION_USERREFS &&
1297 (flags.doall || flags.replicate))
1301 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1302 DMU_BACKUP_FEATURE_DEDUPPROPS);
1303 if ((err = pipe(pipefd))) {
1304 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1305 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1308 dda.outputfd = outfd;
1309 dda.inputfd = pipefd[1];
1310 dda.dedup_hdl = zhp->zfs_hdl;
1311 if ((err = pthread_create(&tid, NULL, cksummer, &dda))) {
1312 (void) close(pipefd[0]);
1313 (void) close(pipefd[1]);
1314 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1315 return (zfs_error(zhp->zfs_hdl,
1316 EZFS_THREADCREATEFAILED, errbuf));
1320 if (flags.replicate || flags.doall || flags.props) {
1321 dmu_replay_record_t drr = { 0 };
1322 char *packbuf = NULL;
1324 zio_cksum_t zc = { { 0 } };
1326 if (flags.replicate || flags.props) {
1329 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1331 VERIFY(0 == nvlist_add_string(hdrnv,
1332 "fromsnap", fromsnap));
1334 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1335 if (!flags.replicate) {
1336 VERIFY(0 == nvlist_add_boolean(hdrnv,
1340 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1341 fromsnap, tosnap, flags.replicate, &fss, &fsavl);
1344 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1345 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1352 fsavl_destroy(fsavl);
1358 /* write first begin record */
1359 drr.drr_type = DRR_BEGIN;
1360 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1361 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.drr_versioninfo,
1362 DMU_COMPOUNDSTREAM);
1363 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.drr_versioninfo,
1365 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1366 sizeof (drr.drr_u.drr_begin.drr_toname),
1367 "%s@%s", zhp->zfs_name, tosnap);
1368 drr.drr_payloadlen = buflen;
1369 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
1371 /* write header nvlist */
1372 if (err != -1 && packbuf != NULL) {
1373 err = cksum_and_write(packbuf, buflen, &zc, outfd);
1377 fsavl_destroy(fsavl);
1383 /* write end record */
1385 bzero(&drr, sizeof (drr));
1386 drr.drr_type = DRR_END;
1387 drr.drr_u.drr_end.drr_checksum = zc;
1388 err = write(outfd, &drr, sizeof (drr));
1390 fsavl_destroy(fsavl);
1398 /* dump each stream */
1399 sdd.fromsnap = fromsnap;
1400 sdd.tosnap = tosnap;
1402 sdd.outfd = pipefd[0];
1405 sdd.replicate = flags.replicate;
1406 sdd.doall = flags.doall;
1407 sdd.fromorigin = flags.fromorigin;
1410 sdd.verbose = flags.verbose;
1411 sdd.filter_cb = filter_func;
1412 sdd.filter_cb_arg = cb_arg;
1414 sdd.debugnv = *debugnvp;
1417 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1418 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1419 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
1420 if (sdd.cleanup_fd < 0) {
1425 sdd.cleanup_fd = -1;
1427 err = dump_filesystems(zhp, &sdd);
1428 fsavl_destroy(fsavl);
1432 (void) close(pipefd[0]);
1433 (void) pthread_join(tid, NULL);
1436 if (sdd.cleanup_fd != -1) {
1437 VERIFY(0 == close(sdd.cleanup_fd));
1438 sdd.cleanup_fd = -1;
1441 if (flags.replicate || flags.doall || flags.props) {
1443 * write final end record. NB: want to do this even if
1444 * there was some error, because it might not be totally
1447 dmu_replay_record_t drr = { 0 };
1448 drr.drr_type = DRR_END;
1449 if (write(outfd, &drr, sizeof (drr)) == -1) {
1450 return (zfs_standard_error(zhp->zfs_hdl,
1455 return (err || sdd.err);
1458 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1460 if (sdd.cleanup_fd != -1)
1461 VERIFY(0 == close(sdd.cleanup_fd));
1463 (void) pthread_cancel(tid);
1464 (void) pthread_join(tid, NULL);
1465 (void) close(pipefd[0]);
1471 * Routines specific to "zfs recv"
1475 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1476 boolean_t byteswap, zio_cksum_t *zc)
1483 rv = read(fd, cp, len);
1488 if (rv < 0 || len != 0) {
1489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1490 "failed to read from stream"));
1491 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1492 "cannot receive")));
1497 fletcher_4_incremental_byteswap(buf, ilen, zc);
1499 fletcher_4_incremental_native(buf, ilen, zc);
1505 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1506 boolean_t byteswap, zio_cksum_t *zc)
1511 buf = zfs_alloc(hdl, len);
1515 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1521 err = nvlist_unpack(buf, len, nvp, 0);
1524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1525 "stream (malformed nvlist)"));
1532 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1533 int baselen, char *newname, recvflags_t flags)
1536 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1538 prop_changelist_t *clp;
1541 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1544 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1545 flags.force ? MS_FORCE : 0);
1549 err = changelist_prefix(clp);
1553 zc.zc_objset_type = DMU_OST_ZFS;
1554 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1557 (void) strcpy(newname, tryname);
1559 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1561 if (flags.verbose) {
1562 (void) printf("attempting rename %s to %s\n",
1563 zc.zc_name, zc.zc_value);
1565 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1567 changelist_rename(clp, name, tryname);
1572 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
1575 (void) strncpy(newname, name, baselen);
1576 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
1577 "recv-%ld-%u", (long) getpid(), seq);
1578 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1580 if (flags.verbose) {
1581 (void) printf("failed - trying rename %s to %s\n",
1582 zc.zc_name, zc.zc_value);
1584 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1586 changelist_rename(clp, name, newname);
1587 if (err && flags.verbose) {
1588 (void) printf("failed (%u) - "
1589 "will try again on next pass\n", errno);
1592 } else if (flags.verbose) {
1594 (void) printf("success\n");
1596 (void) printf("failed (%u)\n", errno);
1599 (void) changelist_postfix(clp);
1600 changelist_free(clp);
1606 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1607 char *newname, recvflags_t flags)
1609 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1611 prop_changelist_t *clp;
1613 boolean_t defer = B_FALSE;
1616 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1619 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1620 flags.force ? MS_FORCE : 0);
1621 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1622 zfs_spa_version(zhp, &spa_version) == 0 &&
1623 spa_version >= SPA_VERSION_USERREFS)
1628 err = changelist_prefix(clp);
1632 zc.zc_objset_type = DMU_OST_ZFS;
1633 zc.zc_defer_destroy = defer;
1634 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1637 (void) printf("attempting destroy %s\n", zc.zc_name);
1638 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1641 (void) printf("success\n");
1642 changelist_remove(clp, zc.zc_name);
1645 (void) changelist_postfix(clp);
1646 changelist_free(clp);
1649 * Deferred destroy might destroy the snapshot or only mark it to be
1650 * destroyed later, and it returns success in either case.
1652 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1653 ZFS_TYPE_SNAPSHOT))) {
1654 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1660 typedef struct guid_to_name_data {
1663 } guid_to_name_data_t;
1666 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1668 guid_to_name_data_t *gtnd = arg;
1671 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1672 (void) strcpy(gtnd->name, zhp->zfs_name);
1676 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1682 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1685 /* exhaustive search all local snapshots */
1686 guid_to_name_data_t gtnd;
1694 if (strchr(parent, '@') == NULL) {
1695 zhp = make_dataset_handle(hdl, parent);
1697 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1704 cp = strchr(parent, '/');
1707 zhp = make_dataset_handle(hdl, parent);
1712 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1716 return (err == EEXIST ? 0 : ENOENT);
1721 * Return true if dataset guid1 is created before guid2.
1724 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1725 uint64_t guid1, uint64_t guid2)
1728 char *fsname, *snapname;
1729 char buf[ZFS_MAXNAMELEN];
1731 zfs_node_t zn1, zn2;
1738 nvfs = fsavl_find(avl, guid1, &snapname);
1739 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1740 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1741 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1742 if (zn1.zn_handle == NULL)
1745 nvfs = fsavl_find(avl, guid2, &snapname);
1746 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1747 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1748 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1749 if (zn2.zn_handle == NULL) {
1750 zfs_close(zn2.zn_handle);
1754 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1756 zfs_close(zn1.zn_handle);
1757 zfs_close(zn2.zn_handle);
1763 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1764 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
1768 avl_tree_t *local_avl;
1769 nvpair_t *fselem, *nextfselem;
1771 char newname[ZFS_MAXNAMELEN];
1773 boolean_t needagain, progress, recursive;
1776 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1778 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
1785 needagain = progress = B_FALSE;
1787 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1788 recursive, &local_nv, &local_avl)) != 0)
1792 * Process deletes and renames
1794 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1795 fselem; fselem = nextfselem) {
1796 nvlist_t *nvfs, *snaps;
1797 nvlist_t *stream_nvfs = NULL;
1798 nvpair_t *snapelem, *nextsnapelem;
1799 uint64_t fromguid = 0;
1800 uint64_t originguid = 0;
1801 uint64_t stream_originguid = 0;
1802 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1803 char *fsname, *stream_fsname;
1805 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1807 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1808 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1809 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1810 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1811 &parent_fromsnap_guid));
1812 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1815 * First find the stream's fs, so we can check for
1816 * a different origin (due to "zfs promote")
1818 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1819 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1822 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1823 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1825 if (stream_nvfs != NULL)
1829 /* check for promote */
1830 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1831 &stream_originguid);
1832 if (stream_nvfs && originguid != stream_originguid) {
1833 switch (created_before(hdl, local_avl,
1834 stream_originguid, originguid)) {
1837 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1838 nvlist_t *origin_nvfs;
1839 char *origin_fsname;
1842 (void) printf("promoting %s\n", fsname);
1844 origin_nvfs = fsavl_find(local_avl, originguid,
1846 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1847 "name", &origin_fsname));
1848 (void) strlcpy(zc.zc_value, origin_fsname,
1849 sizeof (zc.zc_value));
1850 (void) strlcpy(zc.zc_name, fsname,
1851 sizeof (zc.zc_name));
1852 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1860 fsavl_destroy(local_avl);
1861 nvlist_free(local_nv);
1865 * We had/have the wrong origin, therefore our
1866 * list of snapshots is wrong. Need to handle
1867 * them on the next pass.
1873 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1874 snapelem; snapelem = nextsnapelem) {
1876 char *stream_snapname;
1877 nvlist_t *found, *props;
1879 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1881 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1882 found = fsavl_find(stream_avl, thisguid,
1885 /* check for delete */
1886 if (found == NULL) {
1887 char name[ZFS_MAXNAMELEN];
1892 (void) snprintf(name, sizeof (name), "%s@%s",
1893 fsname, nvpair_name(snapelem));
1895 error = recv_destroy(hdl, name,
1896 strlen(fsname)+1, newname, flags);
1904 stream_nvfs = found;
1906 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1907 &props) && 0 == nvlist_lookup_nvlist(props,
1908 stream_snapname, &props)) {
1909 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1911 zc.zc_cookie = B_TRUE; /* received */
1912 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1913 "%s@%s", fsname, nvpair_name(snapelem));
1914 if (zcmd_write_src_nvlist(hdl, &zc,
1916 (void) zfs_ioctl(hdl,
1917 ZFS_IOC_SET_PROP, &zc);
1918 zcmd_free_nvlists(&zc);
1922 /* check for different snapname */
1923 if (strcmp(nvpair_name(snapelem),
1924 stream_snapname) != 0) {
1925 char name[ZFS_MAXNAMELEN];
1926 char tryname[ZFS_MAXNAMELEN];
1928 (void) snprintf(name, sizeof (name), "%s@%s",
1929 fsname, nvpair_name(snapelem));
1930 (void) snprintf(tryname, sizeof (name), "%s@%s",
1931 fsname, stream_snapname);
1933 error = recv_rename(hdl, name, tryname,
1934 strlen(fsname)+1, newname, flags);
1941 if (strcmp(stream_snapname, fromsnap) == 0)
1942 fromguid = thisguid;
1945 /* check for delete */
1946 if (stream_nvfs == NULL) {
1950 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1959 if (fromguid == 0) {
1960 if (flags.verbose) {
1961 (void) printf("local fs %s does not have "
1962 "fromsnap (%s in stream); must have "
1963 "been deleted locally; ignoring\n",
1969 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1970 "name", &stream_fsname));
1971 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1972 "parentfromsnap", &stream_parent_fromsnap_guid));
1974 s1 = strrchr(fsname, '/');
1975 s2 = strrchr(stream_fsname, '/');
1978 * Check for rename. If the exact receive path is specified, it
1979 * does not count as a rename, but we still need to check the
1980 * datasets beneath it.
1982 if ((stream_parent_fromsnap_guid != 0 &&
1983 parent_fromsnap_guid != 0 &&
1984 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1985 ((flags.isprefix || strcmp(tofs, fsname) != 0) &&
1986 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
1988 char tryname[ZFS_MAXNAMELEN];
1990 parent = fsavl_find(local_avl,
1991 stream_parent_fromsnap_guid, NULL);
1993 * NB: parent might not be found if we used the
1994 * tosnap for stream_parent_fromsnap_guid,
1995 * because the parent is a newly-created fs;
1996 * we'll be able to rename it after we recv the
1999 if (parent != NULL) {
2002 VERIFY(0 == nvlist_lookup_string(parent, "name",
2004 (void) snprintf(tryname, sizeof (tryname),
2005 "%s%s", pname, strrchr(stream_fsname, '/'));
2008 if (flags.verbose) {
2009 (void) printf("local fs %s new parent "
2010 "not found\n", fsname);
2016 error = recv_rename(hdl, fsname, tryname,
2017 strlen(tofs)+1, newname, flags);
2019 if (renamed != NULL && newname[0] != '\0') {
2020 VERIFY(0 == nvlist_add_boolean(renamed,
2031 fsavl_destroy(local_avl);
2032 nvlist_free(local_nv);
2034 if (needagain && progress) {
2035 /* do another pass to fix up temporary names */
2037 (void) printf("another pass:\n");
2045 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2046 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2047 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2049 nvlist_t *stream_nv = NULL;
2050 avl_tree_t *stream_avl = NULL;
2051 char *fromsnap = NULL;
2053 char tofs[ZFS_MAXNAMELEN];
2054 char sendfs[ZFS_MAXNAMELEN];
2056 dmu_replay_record_t drre;
2058 boolean_t anyerr = B_FALSE;
2059 boolean_t softerr = B_FALSE;
2060 boolean_t recursive;
2062 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2065 assert(drr->drr_type == DRR_BEGIN);
2066 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2067 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2068 DMU_COMPOUNDSTREAM);
2071 * Read in the nvlist from the stream.
2073 if (drr->drr_payloadlen != 0) {
2074 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2075 &stream_nv, flags.byteswap, zc);
2077 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2082 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2085 if (recursive && strchr(destname, '@')) {
2086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2087 "cannot specify snapshot name for multi-snapshot stream"));
2088 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2093 * Read in the end record and verify checksum.
2095 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2096 flags.byteswap, NULL)))
2098 if (flags.byteswap) {
2099 drre.drr_type = BSWAP_32(drre.drr_type);
2100 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2101 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2102 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2103 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2104 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2105 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2106 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2107 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2109 if (drre.drr_type != DRR_END) {
2110 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2113 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2115 "incorrect header checksum"));
2116 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2120 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2122 if (drr->drr_payloadlen != 0) {
2123 nvlist_t *stream_fss;
2125 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2127 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2129 "couldn't allocate avl tree"));
2130 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2134 if (fromsnap != NULL) {
2135 nvlist_t *renamed = NULL;
2136 nvpair_t *pair = NULL;
2138 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2139 if (flags.isprefix) {
2140 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2144 cp = strrchr(drrb->drr_toname, '/');
2146 (void) strlcat(tofs, "/",
2150 i = (cp - drrb->drr_toname);
2153 i = strcspn(drrb->drr_toname, "/@");
2155 /* zfs_receive_one() will create_parents() */
2156 (void) strlcat(tofs, &drrb->drr_toname[i],
2158 *strchr(tofs, '@') = '\0';
2161 if (recursive && !flags.dryrun && !flags.nomount) {
2162 VERIFY(0 == nvlist_alloc(&renamed,
2163 NV_UNIQUE_NAME, 0));
2166 softerr = recv_incremental_replication(hdl, tofs, flags,
2167 stream_nv, stream_avl, renamed);
2169 /* Unmount renamed filesystems before receiving. */
2170 while ((pair = nvlist_next_nvpair(renamed,
2173 prop_changelist_t *clp = NULL;
2175 zhp = zfs_open(hdl, nvpair_name(pair),
2176 ZFS_TYPE_FILESYSTEM);
2178 clp = changelist_gather(zhp,
2179 ZFS_PROP_MOUNTPOINT, 0, 0);
2183 changelist_prefix(clp);
2184 changelist_free(clp);
2189 nvlist_free(renamed);
2194 * Get the fs specified by the first path in the stream (the top level
2195 * specified by 'zfs send') and pass it to each invocation of
2196 * zfs_receive_one().
2198 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2200 if ((cp = strchr(sendfs, '@')) != NULL)
2203 /* Finally, receive each contained stream */
2206 * we should figure out if it has a recoverable
2207 * error, in which case do a recv_skip() and drive on.
2208 * Note, if we fail due to already having this guid,
2209 * zfs_receive_one() will take care of it (ie,
2210 * recv_skip() and return 0).
2212 error = zfs_receive_impl(hdl, destname, flags, fd,
2213 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2215 if (error == ENODATA) {
2220 } while (error == 0);
2222 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2224 * Now that we have the fs's they sent us, try the
2227 softerr = recv_incremental_replication(hdl, tofs, flags,
2228 stream_nv, stream_avl, NULL);
2232 fsavl_destroy(stream_avl);
2234 nvlist_free(stream_nv);
2243 trunc_prop_errs(int truncated)
2245 ASSERT(truncated != 0);
2248 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2249 "1 more property could not be set\n"));
2251 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2252 "%d more properties could not be set\n"), truncated);
2256 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2258 dmu_replay_record_t *drr;
2259 void *buf = malloc(1<<20);
2262 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2263 "cannot receive:"));
2265 /* XXX would be great to use lseek if possible... */
2268 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2269 byteswap, NULL) == 0) {
2271 drr->drr_type = BSWAP_32(drr->drr_type);
2273 switch (drr->drr_type) {
2275 /* NB: not to be used on v2 stream packages */
2276 if (drr->drr_payloadlen != 0) {
2277 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2278 "invalid substream header"));
2279 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2289 drr->drr_u.drr_object.drr_bonuslen =
2290 BSWAP_32(drr->drr_u.drr_object.
2293 (void) recv_read(hdl, fd, buf,
2294 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2300 drr->drr_u.drr_write.drr_length =
2301 BSWAP_64(drr->drr_u.drr_write.drr_length);
2303 (void) recv_read(hdl, fd, buf,
2304 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2308 drr->drr_u.drr_write.drr_length =
2309 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2311 (void) recv_read(hdl, fd, buf,
2312 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2314 case DRR_WRITE_BYREF:
2315 case DRR_FREEOBJECTS:
2320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2321 "invalid record type"));
2322 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2331 * Restores a backup of tosnap from the file descriptor specified by infd.
2334 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2335 recvflags_t flags, dmu_replay_record_t *drr,
2336 dmu_replay_record_t *drr_noswap, const char *sendfs,
2337 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2338 uint64_t *action_handlep)
2340 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2342 int ioctl_err, ioctl_errno, err;
2344 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2346 char prop_errbuf[1024];
2347 const char *chopprefix;
2348 boolean_t newfs = B_FALSE;
2349 boolean_t stream_wantsnewfs;
2350 uint64_t parent_snapguid = 0;
2351 prop_changelist_t *clp = NULL;
2352 nvlist_t *snapprops_nvlist = NULL;
2353 zprop_errflags_t prop_errflags;
2354 boolean_t recursive;
2356 begin_time = time(NULL);
2358 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2361 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2364 if (stream_avl != NULL) {
2366 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2371 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2373 err = nvlist_lookup_nvlist(fs, "props", &props);
2375 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2377 if (flags.canmountoff) {
2378 VERIFY(0 == nvlist_add_uint64(props,
2379 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2381 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2385 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2386 VERIFY(0 == nvlist_lookup_nvlist(props,
2387 snapname, &snapprops_nvlist));
2397 * Determine how much of the snapshot name stored in the stream
2398 * we are going to tack on to the name they specified on the
2399 * command line, and how much we are going to chop off.
2401 * If they specified a snapshot, chop the entire name stored in
2406 * A filesystem was specified with -e. We want to tack on only
2407 * the tail of the sent snapshot path.
2409 if (strchr(tosnap, '@')) {
2410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2411 "argument - snapshot not allowed with -e"));
2412 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2415 chopprefix = strrchr(sendfs, '/');
2417 if (chopprefix == NULL) {
2419 * The tail is the poolname, so we need to
2420 * prepend a path separator.
2422 int len = strlen(drrb->drr_toname);
2423 cp = malloc(len + 2);
2425 (void) strcpy(&cp[1], drrb->drr_toname);
2428 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2430 } else if (flags.isprefix) {
2432 * A filesystem was specified with -d. We want to tack on
2433 * everything but the first element of the sent snapshot path
2434 * (all but the pool name).
2436 if (strchr(tosnap, '@')) {
2437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2438 "argument - snapshot not allowed with -d"));
2439 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2442 chopprefix = strchr(drrb->drr_toname, '/');
2443 if (chopprefix == NULL)
2444 chopprefix = strchr(drrb->drr_toname, '@');
2445 } else if (strchr(tosnap, '@') == NULL) {
2447 * If a filesystem was specified without -d or -e, we want to
2448 * tack on everything after the fs specified by 'zfs send'.
2450 chopprefix = drrb->drr_toname + strlen(sendfs);
2452 /* A snapshot was specified as an exact path (no -d or -e). */
2454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2455 "cannot specify snapshot name for multi-snapshot "
2457 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2459 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2462 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2463 ASSERT(chopprefix > drrb->drr_toname);
2464 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2465 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2466 chopprefix[0] == '\0');
2469 * Determine name of destination snapshot, store in zc_value.
2471 (void) strcpy(zc.zc_top_ds, tosnap);
2472 (void) strcpy(zc.zc_value, tosnap);
2473 (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2475 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2476 zcmd_free_nvlists(&zc);
2477 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2481 * Determine the name of the origin snapshot, store in zc_string.
2483 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2484 if (guid_to_name(hdl, tosnap,
2485 drrb->drr_fromguid, zc.zc_string) != 0) {
2486 zcmd_free_nvlists(&zc);
2487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2488 "local origin for clone %s does not exist"),
2490 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2493 (void) printf("found clone origin %s\n", zc.zc_string);
2496 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2497 (drrb->drr_flags & DRR_FLAG_CLONE));
2499 if (stream_wantsnewfs) {
2501 * if the parent fs does not exist, look for it based on
2502 * the parent snap GUID
2504 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2505 "cannot receive new filesystem stream"));
2507 (void) strcpy(zc.zc_name, zc.zc_value);
2508 cp = strrchr(zc.zc_name, '/');
2512 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2513 char suffix[ZFS_MAXNAMELEN];
2514 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2515 if (guid_to_name(hdl, tosnap, parent_snapguid,
2516 zc.zc_value) == 0) {
2517 *strchr(zc.zc_value, '@') = '\0';
2518 (void) strcat(zc.zc_value, suffix);
2523 * if the fs does not exist, look for it based on the
2526 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2527 "cannot receive incremental stream"));
2529 (void) strcpy(zc.zc_name, zc.zc_value);
2530 *strchr(zc.zc_name, '@') = '\0';
2533 * If the exact receive path was specified and this is the
2534 * topmost path in the stream, then if the fs does not exist we
2535 * should look no further.
2537 if ((flags.isprefix || (*(chopprefix = drrb->drr_toname +
2538 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2539 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2540 char snap[ZFS_MAXNAMELEN];
2541 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2542 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
2543 zc.zc_value) == 0) {
2544 *strchr(zc.zc_value, '@') = '\0';
2545 (void) strcat(zc.zc_value, snap);
2550 (void) strcpy(zc.zc_name, zc.zc_value);
2551 *strchr(zc.zc_name, '@') = '\0';
2553 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2557 * Destination fs exists. Therefore this should either
2558 * be an incremental, or the stream specifies a new fs
2559 * (full stream or clone) and they want us to blow it
2560 * away (and have therefore specified -F and removed any
2563 if (stream_wantsnewfs) {
2565 zcmd_free_nvlists(&zc);
2566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2567 "destination '%s' exists\n"
2568 "must specify -F to overwrite it"),
2570 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2572 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2574 zcmd_free_nvlists(&zc);
2575 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2576 "destination has snapshots (eg. %s)\n"
2577 "must destroy them to overwrite it"),
2579 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2583 if ((zhp = zfs_open(hdl, zc.zc_name,
2584 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2585 zcmd_free_nvlists(&zc);
2589 if (stream_wantsnewfs &&
2590 zhp->zfs_dmustats.dds_origin[0]) {
2591 zcmd_free_nvlists(&zc);
2593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2594 "destination '%s' is a clone\n"
2595 "must destroy it to overwrite it"),
2597 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2600 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2601 stream_wantsnewfs) {
2602 /* We can't do online recv in this case */
2603 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2606 zcmd_free_nvlists(&zc);
2609 if (changelist_prefix(clp) != 0) {
2610 changelist_free(clp);
2612 zcmd_free_nvlists(&zc);
2616 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
2617 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
2619 zcmd_free_nvlists(&zc);
2625 * Destination filesystem does not exist. Therefore we better
2626 * be creating a new filesystem (either from a full backup, or
2627 * a clone). It would therefore be invalid if the user
2628 * specified only the pool name (i.e. if the destination name
2629 * contained no slash character).
2631 if (!stream_wantsnewfs ||
2632 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2633 zcmd_free_nvlists(&zc);
2634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2635 "destination '%s' does not exist"), zc.zc_name);
2636 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2640 * Trim off the final dataset component so we perform the
2641 * recvbackup ioctl to the filesystems's parent.
2645 if (flags.isprefix && !flags.istail && !flags.dryrun &&
2646 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2647 zcmd_free_nvlists(&zc);
2648 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2654 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2655 zc.zc_cookie = infd;
2656 zc.zc_guid = flags.force;
2657 if (flags.verbose) {
2658 (void) printf("%s %s stream of %s into %s\n",
2659 flags.dryrun ? "would receive" : "receiving",
2660 drrb->drr_fromguid ? "incremental" : "full",
2661 drrb->drr_toname, zc.zc_value);
2662 (void) fflush(stdout);
2666 zcmd_free_nvlists(&zc);
2667 return (recv_skip(hdl, infd, flags.byteswap));
2670 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2671 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2672 zc.zc_cleanup_fd = cleanup_fd;
2673 zc.zc_action_handle = *action_handlep;
2675 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2676 ioctl_errno = errno;
2677 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2680 nvlist_t *prop_errors;
2681 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2682 zc.zc_nvlist_dst_size, &prop_errors, 0));
2684 nvpair_t *prop_err = NULL;
2686 while ((prop_err = nvlist_next_nvpair(prop_errors,
2687 prop_err)) != NULL) {
2692 prop = zfs_name_to_prop(nvpair_name(prop_err));
2693 (void) nvpair_value_int32(prop_err, &intval);
2694 if (strcmp(nvpair_name(prop_err),
2695 ZPROP_N_MORE_ERRORS) == 0) {
2696 trunc_prop_errs(intval);
2699 (void) snprintf(tbuf, sizeof (tbuf),
2700 dgettext(TEXT_DOMAIN,
2701 "cannot receive %s property on %s"),
2702 nvpair_name(prop_err), zc.zc_name);
2703 zfs_setprop_error(hdl, prop, intval, tbuf);
2706 nvlist_free(prop_errors);
2709 zc.zc_nvlist_dst = 0;
2710 zc.zc_nvlist_dst_size = 0;
2711 zcmd_free_nvlists(&zc);
2713 if (err == 0 && snapprops_nvlist) {
2714 zfs_cmd_t zc2 = { "\0", "\0", "\0", "\0", 0 };
2716 (void) strcpy(zc2.zc_name, zc.zc_value);
2717 zc2.zc_cookie = B_TRUE; /* received */
2718 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
2719 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
2720 zcmd_free_nvlists(&zc2);
2724 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
2726 * It may be that this snapshot already exists,
2727 * in which case we want to consume & ignore it
2728 * rather than failing.
2730 avl_tree_t *local_avl;
2731 nvlist_t *local_nv, *fs;
2732 cp = strchr(zc.zc_value, '@');
2735 * XXX Do this faster by just iterating over snaps in
2736 * this fs. Also if zc_value does not exist, we will
2737 * get a strange "does not exist" error message.
2740 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
2741 &local_nv, &local_avl) == 0) {
2743 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
2744 fsavl_destroy(local_avl);
2745 nvlist_free(local_nv);
2748 if (flags.verbose) {
2749 (void) printf("snap %s already exists; "
2750 "ignoring\n", zc.zc_value);
2752 err = ioctl_err = recv_skip(hdl, infd,
2759 if (ioctl_err != 0) {
2760 switch (ioctl_errno) {
2762 cp = strchr(zc.zc_value, '@');
2764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2765 "most recent snapshot of %s does not\n"
2766 "match incremental source"), zc.zc_value);
2767 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2772 "destination %s has been modified\n"
2773 "since most recent snapshot"), zc.zc_name);
2774 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2777 cp = strchr(zc.zc_value, '@');
2779 /* it's the containing fs that exists */
2782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2783 "destination already exists"));
2784 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
2785 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
2790 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2794 "invalid stream (checksum mismatch)"));
2795 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2798 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2799 "pool must be upgraded to receive this stream."));
2800 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
2803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2804 "destination %s space quota exceeded"), zc.zc_name);
2805 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2808 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
2813 * Mount the target filesystem (if created). Also mount any
2814 * children of the target filesystem if we did a replication
2815 * receive (indicated by stream_avl being non-NULL).
2817 cp = strchr(zc.zc_value, '@');
2818 if (cp && (ioctl_err == 0 || !newfs)) {
2822 h = zfs_open(hdl, zc.zc_value,
2823 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2825 if (h->zfs_type == ZFS_TYPE_VOLUME) {
2827 err = zvol_create_link(hdl, h->zfs_name);
2828 if (err == 0 && ioctl_err == 0)
2829 err = zvol_create_link(hdl,
2831 } else if (newfs || stream_avl) {
2833 * Track the first/top of hierarchy fs,
2834 * for mounting and sharing later.
2836 if (top_zfs && *top_zfs == NULL)
2837 *top_zfs = zfs_strdup(hdl, zc.zc_value);
2845 err |= changelist_postfix(clp);
2846 changelist_free(clp);
2849 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
2850 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2851 "failed to clear unreceived properties on %s"),
2853 (void) fprintf(stderr, "\n");
2855 if (prop_errflags & ZPROP_ERR_NORESTORE) {
2856 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
2857 "failed to restore original properties on %s"),
2859 (void) fprintf(stderr, "\n");
2862 if (err || ioctl_err)
2865 *action_handlep = zc.zc_action_handle;
2867 if (flags.verbose) {
2870 uint64_t bytes = zc.zc_cookie;
2871 time_t delta = time(NULL) - begin_time;
2874 zfs_nicenum(bytes, buf1, sizeof (buf1));
2875 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
2877 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
2885 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2886 int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2887 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2890 dmu_replay_record_t drr, drr_noswap;
2891 struct drr_begin *drrb = &drr.drr_u.drr_begin;
2893 zio_cksum_t zcksum = { { 0 } };
2894 uint64_t featureflags;
2897 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2900 if (flags.isprefix &&
2901 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2903 "(%s) does not exist"), tosnap);
2904 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2907 /* read in the BEGIN record */
2908 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2912 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2913 /* It's the double end record at the end of a package */
2917 /* the kernel needs the non-byteswapped begin record */
2920 flags.byteswap = B_FALSE;
2921 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2923 * We computed the checksum in the wrong byteorder in
2924 * recv_read() above; do it again correctly.
2926 bzero(&zcksum, sizeof (zio_cksum_t));
2927 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2928 flags.byteswap = B_TRUE;
2930 drr.drr_type = BSWAP_32(drr.drr_type);
2931 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2932 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2933 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
2934 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2935 drrb->drr_type = BSWAP_32(drrb->drr_type);
2936 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2937 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2938 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2941 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2943 "stream (bad magic number)"));
2944 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2947 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
2948 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
2950 if (!DMU_STREAM_SUPPORTED(featureflags) ||
2951 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
2952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2953 "stream has unsupported feature, feature flags = %lx"),
2955 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2958 if (strchr(drrb->drr_toname, '@') == NULL) {
2959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2960 "stream (bad snapshot name)"));
2961 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2964 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
2965 char nonpackage_sendfs[ZFS_MAXNAMELEN];
2966 if (sendfs == NULL) {
2968 * We were not called from zfs_receive_package(). Get
2969 * the fs specified by 'zfs send'.
2972 (void) strlcpy(nonpackage_sendfs,
2973 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
2974 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
2976 sendfs = nonpackage_sendfs;
2978 return (zfs_receive_one(hdl, infd, tosnap, flags,
2979 &drr, &drr_noswap, sendfs, stream_nv, stream_avl,
2980 top_zfs, cleanup_fd, action_handlep));
2982 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
2983 DMU_COMPOUNDSTREAM);
2984 return (zfs_receive_package(hdl, infd, tosnap, flags,
2985 &drr, &zcksum, top_zfs, cleanup_fd, action_handlep));
2990 * Restores a backup of tosnap from the file descriptor specified by infd.
2991 * Return 0 on total success, -2 if some things couldn't be
2992 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2993 * (-1 will override -2).
2996 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2997 int infd, avl_tree_t *stream_avl)
2999 char *top_zfs = NULL;
3002 uint64_t action_handle = 0;
3004 cleanup_fd = open(ZFS_DEV, O_RDWR);
3005 VERIFY(cleanup_fd >= 0);
3007 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
3008 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3010 VERIFY(0 == close(cleanup_fd));
3012 if (err == 0 && !flags.nomount && top_zfs) {
3014 prop_changelist_t *clp;
3016 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3018 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3019 CL_GATHER_MOUNT_ALWAYS, 0);
3022 /* mount and share received datasets */
3023 err = changelist_postfix(clp);
3024 changelist_free(clp);
3027 if (zhp == NULL || clp == NULL || err)