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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
30 #include <libdevinfo.h>
38 #include <sys/mount.h>
39 #include <sys/mntent.h>
40 #include <sys/mnttab.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
50 #include <fletcher.c> /* XXX */
52 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
53 int, avl_tree_t *, char **);
56 * Routines for dealing with the AVL tree of fs-nvlists
58 typedef struct fsavl_node {
66 fsavl_compare(const void *arg1, const void *arg2)
68 const fsavl_node_t *fn1 = arg1;
69 const fsavl_node_t *fn2 = arg2;
71 if (fn1->fn_guid > fn2->fn_guid)
73 else if (fn1->fn_guid < fn2->fn_guid)
80 * Given the GUID of a snapshot, find its containing filesystem and
84 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
89 fn_find.fn_guid = snapguid;
91 fn = avl_find(avl, &fn_find, NULL);
94 *snapname = fn->fn_snapname;
101 fsavl_destroy(avl_tree_t *avl)
110 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
117 fsavl_create(nvlist_t *fss)
120 nvpair_t *fselem = NULL;
122 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
125 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
126 offsetof(fsavl_node_t, fn_node));
128 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
129 nvlist_t *nvfs, *snaps;
130 nvpair_t *snapelem = NULL;
132 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
133 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
136 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
140 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
141 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
142 fsavl_destroy(fsavl);
146 fn->fn_snapname = nvpair_name(snapelem);
150 * Note: if there are multiple snaps with the
151 * same GUID, we ignore all but one.
153 if (avl_find(fsavl, fn, NULL) == NULL)
164 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
166 typedef struct send_data {
167 uint64_t parent_fromsnap_guid;
168 nvlist_t *parent_snaps;
171 const char *fromsnap;
175 * The header nvlist is of the following format:
178 * "fromsnap" -> string (if incremental)
182 * "name" -> string (full name; for debugging)
183 * "parentfromsnap" -> number (guid of fromsnap in parent)
185 * "props" -> { name -> value (only if set here) }
186 * "snaps" -> { name (lastname) -> number (guid) }
187 * "snapprops" -> { name (lastname) -> { name -> value } }
189 * "origin" -> number (guid) (if clone)
190 * "sent" -> boolean (not on-disk)
198 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
201 send_iterate_snap(zfs_handle_t *zhp, void *arg)
203 send_data_t *sd = arg;
204 uint64_t guid = zhp->zfs_dmustats.dds_guid;
208 snapname = strrchr(zhp->zfs_name, '@')+1;
210 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
212 * NB: if there is no fromsnap here (it's a newly created fs in
213 * an incremental replication), we will substitute the tosnap.
215 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
216 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
217 strcmp(snapname, sd->tosnap) == 0)) {
218 sd->parent_fromsnap_guid = guid;
221 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
222 send_iterate_prop(zhp, nv);
223 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
231 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
233 nvpair_t *elem = NULL;
235 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
236 char *propname = nvpair_name(elem);
237 zfs_prop_t prop = zfs_name_to_prop(propname);
240 assert(zfs_prop_user(propname) || prop != ZPROP_INVAL);
242 if (!zfs_prop_user(propname) && zfs_prop_readonly(prop))
245 verify(nvpair_value_nvlist(elem, &propnv) == 0);
246 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION) {
247 /* these guys are modifyable, but have no source */
249 verify(nvlist_lookup_uint64(propnv,
250 ZPROP_VALUE, &value) == 0);
251 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
255 if (nvlist_lookup_string(propnv,
256 ZPROP_SOURCE, &source) != 0)
258 if (strcmp(source, zhp->zfs_name) != 0)
262 if (zfs_prop_user(propname) ||
263 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
265 verify(nvlist_lookup_string(propnv,
266 ZPROP_VALUE, &value) == 0);
267 VERIFY(0 == nvlist_add_string(nv, propname, value));
270 verify(nvlist_lookup_uint64(propnv,
271 ZPROP_VALUE, &value) == 0);
272 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
278 send_iterate_fs(zfs_handle_t *zhp, void *arg)
280 send_data_t *sd = arg;
283 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
284 uint64_t guid = zhp->zfs_dmustats.dds_guid;
287 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
288 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
289 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
290 sd->parent_fromsnap_guid));
292 if (zhp->zfs_dmustats.dds_origin[0]) {
293 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
294 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
297 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
298 origin->zfs_dmustats.dds_guid));
301 /* iterate over props */
302 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
303 send_iterate_prop(zhp, nv);
304 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
307 /* iterate over snaps, and set sd->parent_fromsnap_guid */
308 sd->parent_fromsnap_guid = 0;
309 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
310 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
311 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
312 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
313 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
314 nvlist_free(sd->parent_snaps);
315 nvlist_free(sd->snapprops);
317 /* add this fs to nvlist */
318 (void) snprintf(guidstring, sizeof (guidstring),
319 "0x%llx", (longlong_t)guid);
320 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
323 /* iterate over children */
324 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
326 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
333 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
334 const char *tosnap, nvlist_t **nvlp, avl_tree_t **avlp)
337 send_data_t sd = { 0 };
340 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
342 return (EZFS_BADTYPE);
344 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
345 sd.fromsnap = fromsnap;
348 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
356 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
367 * Routines for dealing with the sorted snapshot functionality
369 typedef struct zfs_node {
370 zfs_handle_t *zn_handle;
371 avl_node_t zn_avlnode;
375 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
377 avl_tree_t *avl = data;
378 zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
380 node->zn_handle = zhp;
387 zfs_snapshot_compare(const void *larg, const void *rarg)
389 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
390 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
391 uint64_t lcreate, rcreate;
394 * Sort them according to creation time. We use the hidden
395 * CREATETXG property to get an absolute ordering of snapshots.
397 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
398 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
400 if (lcreate < rcreate)
402 else if (lcreate > rcreate)
409 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
416 avl_create(&avl, zfs_snapshot_compare,
417 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
419 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
421 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
422 ret |= callback(node->zn_handle, data);
424 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
433 * Routines specific to "zfs send"
435 typedef struct send_dump_data {
436 /* these are all just the short snapname (the part after the @) */
437 const char *fromsnap;
439 char lastsnap[ZFS_MAXNAMELEN];
440 boolean_t seenfrom, seento, replicate, doall, fromorigin;
449 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
450 * NULL) to the file descriptor specified by outfd.
453 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin,
456 zfs_cmd_t zc = { 0 };
457 libzfs_handle_t *hdl = zhp->zfs_hdl;
459 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
460 assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin);
462 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
464 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value));
465 zc.zc_cookie = outfd;
466 zc.zc_obj = fromorigin;
468 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
470 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
471 "warning: cannot send '%s'"), zhp->zfs_name);
476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
477 "not an earlier snapshot from the same fs"));
478 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
481 if (zfs_dataset_exists(hdl, zc.zc_name,
482 ZFS_TYPE_SNAPSHOT)) {
483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
484 "incremental source (@%s) does not exist"),
487 return (zfs_error(hdl, EZFS_NOENT, errbuf));
500 zfs_error_aux(hdl, strerror(errno));
501 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
504 return (zfs_standard_error(hdl, errno, errbuf));
512 dump_snapshot(zfs_handle_t *zhp, void *arg)
514 send_dump_data_t *sdd = arg;
515 const char *thissnap;
518 thissnap = strchr(zhp->zfs_name, '@') + 1;
520 if (sdd->fromsnap && !sdd->seenfrom &&
521 strcmp(sdd->fromsnap, thissnap) == 0) {
522 sdd->seenfrom = B_TRUE;
523 (void) strcpy(sdd->lastsnap, thissnap);
528 if (sdd->seento || !sdd->seenfrom) {
535 (void) fprintf(stderr, "sending from @%s to %s\n",
536 sdd->lastsnap, zhp->zfs_name);
539 err = dump_ioctl(zhp, sdd->lastsnap,
540 sdd->lastsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
543 if (!sdd->seento && strcmp(sdd->tosnap, thissnap) == 0)
544 sdd->seento = B_TRUE;
546 (void) strcpy(sdd->lastsnap, thissnap);
552 dump_filesystem(zfs_handle_t *zhp, void *arg)
555 send_dump_data_t *sdd = arg;
556 boolean_t missingfrom = B_FALSE;
557 zfs_cmd_t zc = { 0 };
559 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
560 zhp->zfs_name, sdd->tosnap);
561 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
562 (void) fprintf(stderr, "WARNING: "
563 "could not send %s@%s: does not exist\n",
564 zhp->zfs_name, sdd->tosnap);
569 if (sdd->replicate && sdd->fromsnap) {
571 * If this fs does not have fromsnap, and we're doing
572 * recursive, we need to send a full stream from the
573 * beginning (or an incremental from the origin if this
574 * is a clone). If we're doing non-recursive, then let
575 * them get the error.
577 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
578 zhp->zfs_name, sdd->fromsnap);
579 if (ioctl(zhp->zfs_hdl->libzfs_fd,
580 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
581 missingfrom = B_TRUE;
586 sdd->seenfrom = sdd->seento = sdd->lastsnap[0] = 0;
587 if (sdd->fromsnap == NULL || missingfrom)
588 sdd->seenfrom = B_TRUE;
590 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
591 if (!sdd->seenfrom) {
592 (void) fprintf(stderr,
593 "WARNING: could not send %s@%s:\n"
594 "incremental source (%s@%s) does not exist\n",
595 zhp->zfs_name, sdd->tosnap,
596 zhp->zfs_name, sdd->fromsnap);
598 } else if (!sdd->seento) {
600 (void) fprintf(stderr,
601 "WARNING: could not send %s@%s:\n"
602 "incremental source (%s@%s) "
603 "is not earlier than it\n",
604 zhp->zfs_name, sdd->tosnap,
605 zhp->zfs_name, sdd->fromsnap);
607 (void) fprintf(stderr, "WARNING: "
608 "could not send %s@%s: does not exist\n",
609 zhp->zfs_name, sdd->tosnap);
614 zfs_handle_t *snapzhp;
615 char snapname[ZFS_MAXNAMELEN];
617 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
618 zfs_get_name(zhp), sdd->tosnap);
619 snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
620 if (snapzhp == NULL) {
623 rv = dump_ioctl(snapzhp,
624 missingfrom ? NULL : sdd->fromsnap,
625 sdd->fromorigin || missingfrom,
627 sdd->seento = B_TRUE;
636 dump_filesystems(zfs_handle_t *rzhp, void *arg)
638 send_dump_data_t *sdd = arg;
640 boolean_t needagain, progress;
643 return (dump_filesystem(rzhp, sdd));
646 needagain = progress = B_FALSE;
647 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
648 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
653 uint64_t origin_guid = 0;
656 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
657 if (nvlist_lookup_boolean(fslist, "sent") == 0)
660 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
661 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
663 origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL);
665 nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) {
667 * origin has not been sent yet;
674 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
677 err = dump_filesystem(zhp, sdd);
678 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
692 * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
693 * If 'doall', dump all intermediate snaps.
694 * If 'replicate', dump special header and do recursively.
697 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
698 boolean_t replicate, boolean_t doall, boolean_t fromorigin,
699 boolean_t verbose, int outfd)
702 send_dump_data_t sdd = { 0 };
704 nvlist_t *fss = NULL;
705 avl_tree_t *fsavl = NULL;
707 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
708 "cannot send '%s'"), zhp->zfs_name);
710 if (fromsnap && fromsnap[0] == '\0') {
711 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
712 "zero-length incremental source"));
713 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
716 if (replicate || doall) {
717 dmu_replay_record_t drr = { 0 };
718 char *packbuf = NULL;
720 zio_cksum_t zc = { 0 };
722 assert(fromsnap || doall);
727 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
729 VERIFY(0 == nvlist_add_string(hdrnv,
730 "fromsnap", fromsnap));
732 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
734 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
735 fromsnap, tosnap, &fss, &fsavl);
738 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
739 err = nvlist_pack(hdrnv, &packbuf, &buflen,
743 fsavl_destroy(fsavl);
745 return (zfs_standard_error(zhp->zfs_hdl,
750 /* write first begin record */
751 drr.drr_type = DRR_BEGIN;
752 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
753 drr.drr_u.drr_begin.drr_version = DMU_BACKUP_HEADER_VERSION;
754 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
755 sizeof (drr.drr_u.drr_begin.drr_toname),
756 "%s@%s", zhp->zfs_name, tosnap);
757 drr.drr_payloadlen = buflen;
758 fletcher_4_incremental_native(&drr, sizeof (drr), &zc);
759 err = write(outfd, &drr, sizeof (drr));
761 /* write header nvlist */
763 fletcher_4_incremental_native(packbuf, buflen, &zc);
764 err = write(outfd, packbuf, buflen);
768 fsavl_destroy(fsavl);
770 return (zfs_standard_error(zhp->zfs_hdl,
774 /* write end record */
776 bzero(&drr, sizeof (drr));
777 drr.drr_type = DRR_END;
778 drr.drr_u.drr_end.drr_checksum = zc;
779 err = write(outfd, &drr, sizeof (drr));
781 fsavl_destroy(fsavl);
783 return (zfs_standard_error(zhp->zfs_hdl,
789 /* dump each stream */
790 sdd.fromsnap = fromsnap;
793 sdd.replicate = replicate;
795 sdd.fromorigin = fromorigin;
798 sdd.verbose = verbose;
799 err = dump_filesystems(zhp, &sdd);
800 fsavl_destroy(fsavl);
803 if (replicate || doall) {
805 * write final end record. NB: want to do this even if
806 * there was some error, because it might not be totally
809 dmu_replay_record_t drr = { 0 };
810 drr.drr_type = DRR_END;
811 if (write(outfd, &drr, sizeof (drr)) == -1) {
812 return (zfs_standard_error(zhp->zfs_hdl,
817 return (err || sdd.err);
821 * Routines specific to "zfs recv"
825 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
826 boolean_t byteswap, zio_cksum_t *zc)
833 rv = read(fd, cp, len);
838 if (rv < 0 || len != 0) {
839 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
840 "failed to read from stream"));
841 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
847 fletcher_4_incremental_byteswap(buf, ilen, zc);
849 fletcher_4_incremental_native(buf, ilen, zc);
855 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
856 boolean_t byteswap, zio_cksum_t *zc)
861 buf = zfs_alloc(hdl, len);
865 err = recv_read(hdl, fd, buf, len, byteswap, zc);
871 err = nvlist_unpack(buf, len, nvp, 0);
874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
875 "stream (malformed nvlist)"));
882 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
883 int baselen, char *newname, recvflags_t flags)
886 zfs_cmd_t zc = { 0 };
888 prop_changelist_t *clp;
891 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
894 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
895 flags.force ? MS_FORCE : 0);
899 err = changelist_prefix(clp);
904 (void) strcpy(newname, tryname);
906 zc.zc_objset_type = DMU_OST_ZFS;
907 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
908 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
911 (void) printf("attempting rename %s to %s\n",
912 zc.zc_name, zc.zc_value);
914 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
916 changelist_rename(clp, name, tryname);
921 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
924 (void) strncpy(newname, name, baselen);
925 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
926 "recv-%u-%u", getpid(), seq);
927 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
930 (void) printf("failed - trying rename %s to %s\n",
931 zc.zc_name, zc.zc_value);
933 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
935 changelist_rename(clp, name, newname);
936 if (err && flags.verbose) {
937 (void) printf("failed (%u) - "
938 "will try again on next pass\n", errno);
941 } else if (flags.verbose) {
943 (void) printf("success\n");
945 (void) printf("failed (%u)\n", errno);
948 (void) changelist_postfix(clp);
949 changelist_free(clp);
955 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
956 char *newname, recvflags_t flags)
958 zfs_cmd_t zc = { 0 };
960 prop_changelist_t *clp;
963 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
966 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
967 flags.force ? MS_FORCE : 0);
971 err = changelist_prefix(clp);
975 zc.zc_objset_type = DMU_OST_ZFS;
976 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
979 (void) printf("attempting destroy %s\n", zc.zc_name);
980 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
984 (void) printf("success\n");
985 changelist_remove(clp, zc.zc_name);
988 (void) changelist_postfix(clp);
989 changelist_free(clp);
992 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
997 typedef struct guid_to_name_data {
1000 } guid_to_name_data_t;
1003 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1005 guid_to_name_data_t *gtnd = arg;
1008 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1009 (void) strcpy(gtnd->name, zhp->zfs_name);
1012 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1018 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1021 /* exhaustive search all local snapshots */
1022 guid_to_name_data_t gtnd;
1030 if (strchr(parent, '@') == NULL) {
1031 zhp = make_dataset_handle(hdl, parent);
1033 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1040 cp = strchr(parent, '/');
1043 zhp = make_dataset_handle(hdl, parent);
1048 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1052 return (err == EEXIST ? 0 : ENOENT);
1057 * Return true if dataset guid1 is created before guid2.
1060 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1061 uint64_t guid1, uint64_t guid2)
1064 char *fsname, *snapname;
1065 char buf[ZFS_MAXNAMELEN];
1067 zfs_node_t zn1, zn2;
1074 nvfs = fsavl_find(avl, guid1, &snapname);
1075 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1076 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1077 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1078 if (zn1.zn_handle == NULL)
1081 nvfs = fsavl_find(avl, guid2, &snapname);
1082 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1083 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1084 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1085 if (zn2.zn_handle == NULL) {
1086 zfs_close(zn2.zn_handle);
1090 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1092 zfs_close(zn1.zn_handle);
1093 zfs_close(zn2.zn_handle);
1099 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1100 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl)
1103 avl_tree_t *local_avl;
1104 nvpair_t *fselem, *nextfselem;
1105 char *tosnap, *fromsnap;
1106 char newname[ZFS_MAXNAMELEN];
1108 boolean_t needagain, progress;
1110 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1111 VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap));
1117 needagain = progress = B_FALSE;
1119 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1120 &local_nv, &local_avl)) != 0)
1124 * Process deletes and renames
1126 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1127 fselem; fselem = nextfselem) {
1128 nvlist_t *nvfs, *snaps;
1129 nvlist_t *stream_nvfs = NULL;
1130 nvpair_t *snapelem, *nextsnapelem;
1131 uint64_t fromguid = 0;
1132 uint64_t originguid = 0;
1133 uint64_t stream_originguid = 0;
1134 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1135 char *fsname, *stream_fsname;
1137 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1139 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1140 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1141 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1142 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1143 &parent_fromsnap_guid));
1144 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1147 * First find the stream's fs, so we can check for
1148 * a different origin (due to "zfs promote")
1150 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1151 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1154 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1155 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1157 if (stream_nvfs != NULL)
1161 /* check for promote */
1162 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1163 &stream_originguid);
1164 if (stream_nvfs && originguid != stream_originguid) {
1165 switch (created_before(hdl, local_avl,
1166 stream_originguid, originguid)) {
1169 zfs_cmd_t zc = { 0 };
1170 nvlist_t *origin_nvfs;
1171 char *origin_fsname;
1174 (void) printf("promoting %s\n", fsname);
1176 origin_nvfs = fsavl_find(local_avl, originguid,
1178 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1179 "name", &origin_fsname));
1180 (void) strlcpy(zc.zc_value, origin_fsname,
1181 sizeof (zc.zc_value));
1182 (void) strlcpy(zc.zc_name, fsname,
1183 sizeof (zc.zc_name));
1184 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1192 fsavl_destroy(local_avl);
1193 nvlist_free(local_nv);
1197 * We had/have the wrong origin, therefore our
1198 * list of snapshots is wrong. Need to handle
1199 * them on the next pass.
1205 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1206 snapelem; snapelem = nextsnapelem) {
1208 char *stream_snapname;
1209 nvlist_t *found, *props;
1211 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1213 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1214 found = fsavl_find(stream_avl, thisguid,
1217 /* check for delete */
1218 if (found == NULL) {
1219 char name[ZFS_MAXNAMELEN];
1224 (void) snprintf(name, sizeof (name), "%s@%s",
1225 fsname, nvpair_name(snapelem));
1227 error = recv_destroy(hdl, name,
1228 strlen(fsname)+1, newname, flags);
1236 stream_nvfs = found;
1238 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1239 &props) && 0 == nvlist_lookup_nvlist(props,
1240 stream_snapname, &props)) {
1241 zfs_cmd_t zc = { 0 };
1243 zc.zc_cookie = B_TRUE; /* clear current props */
1244 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1245 "%s@%s", fsname, nvpair_name(snapelem));
1246 if (zcmd_write_src_nvlist(hdl, &zc,
1248 (void) zfs_ioctl(hdl,
1249 ZFS_IOC_SET_PROP, &zc);
1250 zcmd_free_nvlists(&zc);
1254 /* check for different snapname */
1255 if (strcmp(nvpair_name(snapelem),
1256 stream_snapname) != 0) {
1257 char name[ZFS_MAXNAMELEN];
1258 char tryname[ZFS_MAXNAMELEN];
1260 (void) snprintf(name, sizeof (name), "%s@%s",
1261 fsname, nvpair_name(snapelem));
1262 (void) snprintf(tryname, sizeof (name), "%s@%s",
1263 fsname, stream_snapname);
1265 error = recv_rename(hdl, name, tryname,
1266 strlen(fsname)+1, newname, flags);
1273 if (strcmp(stream_snapname, fromsnap) == 0)
1274 fromguid = thisguid;
1277 /* check for delete */
1278 if (stream_nvfs == NULL) {
1282 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1291 if (fromguid == 0 && flags.verbose) {
1292 (void) printf("local fs %s does not have fromsnap "
1293 "(%s in stream); must have been deleted locally; "
1294 "ignoring\n", fsname, fromsnap);
1298 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1299 "name", &stream_fsname));
1300 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1301 "parentfromsnap", &stream_parent_fromsnap_guid));
1303 /* check for rename */
1304 if ((stream_parent_fromsnap_guid != 0 &&
1305 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1306 strcmp(strrchr(fsname, '/'),
1307 strrchr(stream_fsname, '/')) != 0) {
1309 char tryname[ZFS_MAXNAMELEN];
1311 parent = fsavl_find(local_avl,
1312 stream_parent_fromsnap_guid, NULL);
1314 * NB: parent might not be found if we used the
1315 * tosnap for stream_parent_fromsnap_guid,
1316 * because the parent is a newly-created fs;
1317 * we'll be able to rename it after we recv the
1320 if (parent != NULL) {
1323 VERIFY(0 == nvlist_lookup_string(parent, "name",
1325 (void) snprintf(tryname, sizeof (tryname),
1326 "%s%s", pname, strrchr(stream_fsname, '/'));
1329 if (flags.verbose) {
1330 (void) printf("local fs %s new parent "
1331 "not found\n", fsname);
1335 error = recv_rename(hdl, fsname, tryname,
1336 strlen(tofs)+1, newname, flags);
1344 fsavl_destroy(local_avl);
1345 nvlist_free(local_nv);
1347 if (needagain && progress) {
1348 /* do another pass to fix up temporary names */
1350 (void) printf("another pass:\n");
1358 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
1359 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
1362 nvlist_t *stream_nv = NULL;
1363 avl_tree_t *stream_avl = NULL;
1364 char *fromsnap = NULL;
1365 char tofs[ZFS_MAXNAMELEN];
1367 dmu_replay_record_t drre;
1369 boolean_t anyerr = B_FALSE;
1370 boolean_t softerr = B_FALSE;
1372 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1375 if (strchr(destname, '@')) {
1376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1377 "can not specify snapshot name for multi-snapshot stream"));
1378 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1381 assert(drr->drr_type == DRR_BEGIN);
1382 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
1383 assert(drr->drr_u.drr_begin.drr_version == DMU_BACKUP_HEADER_VERSION);
1386 * Read in the nvlist from the stream.
1388 if (drr->drr_payloadlen != 0) {
1389 if (!flags.isprefix) {
1390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1391 "must use -d to receive replication "
1392 "(send -R) stream"));
1393 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1396 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
1397 &stream_nv, flags.byteswap, zc);
1399 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1405 * Read in the end record and verify checksum.
1407 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
1408 flags.byteswap, NULL)))
1410 if (flags.byteswap) {
1411 drre.drr_type = BSWAP_32(drre.drr_type);
1412 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
1413 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
1414 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
1415 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
1416 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
1417 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
1418 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
1419 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
1421 if (drre.drr_type != DRR_END) {
1422 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1425 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
1426 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1427 "incorrect header checksum"));
1428 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1432 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
1434 if (drr->drr_payloadlen != 0) {
1435 nvlist_t *stream_fss;
1437 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
1439 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
1440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1441 "couldn't allocate avl tree"));
1442 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
1446 if (fromsnap != NULL) {
1447 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
1448 if (flags.isprefix) {
1449 int i = strcspn(drr->drr_u.drr_begin.drr_toname,
1451 /* zfs_receive_one() will create_parents() */
1452 (void) strlcat(tofs,
1453 &drr->drr_u.drr_begin.drr_toname[i],
1455 *strchr(tofs, '@') = '\0';
1457 softerr = recv_incremental_replication(hdl, tofs,
1458 flags, stream_nv, stream_avl);
1463 /* Finally, receive each contained stream */
1466 * we should figure out if it has a recoverable
1467 * error, in which case do a recv_skip() and drive on.
1468 * Note, if we fail due to already having this guid,
1469 * zfs_receive_one() will take care of it (ie,
1470 * recv_skip() and return 0).
1472 error = zfs_receive_impl(hdl, destname, flags, fd,
1473 stream_avl, top_zfs);
1474 if (error == ENODATA) {
1479 } while (error == 0);
1481 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
1483 * Now that we have the fs's they sent us, try the
1486 softerr = recv_incremental_replication(hdl, tofs, flags,
1487 stream_nv, stream_avl);
1491 fsavl_destroy(stream_avl);
1493 nvlist_free(stream_nv);
1502 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
1504 dmu_replay_record_t *drr;
1505 void *buf = malloc(1<<20);
1507 /* XXX would be great to use lseek if possible... */
1510 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
1511 byteswap, NULL) == 0) {
1513 drr->drr_type = BSWAP_32(drr->drr_type);
1515 switch (drr->drr_type) {
1517 /* NB: not to be used on v2 stream packages */
1518 assert(drr->drr_payloadlen == 0);
1527 drr->drr_u.drr_object.drr_bonuslen =
1528 BSWAP_32(drr->drr_u.drr_object.
1531 (void) recv_read(hdl, fd, buf,
1532 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
1538 drr->drr_u.drr_write.drr_length =
1539 BSWAP_64(drr->drr_u.drr_write.drr_length);
1541 (void) recv_read(hdl, fd, buf,
1542 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
1545 case DRR_FREEOBJECTS:
1550 assert(!"invalid record type");
1559 * Restores a backup of tosnap from the file descriptor specified by infd.
1562 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
1563 recvflags_t flags, dmu_replay_record_t *drr,
1564 dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl,
1567 zfs_cmd_t zc = { 0 };
1569 int ioctl_err, ioctl_errno, err, choplen;
1571 struct drr_begin *drrb = &drr->drr_u.drr_begin;
1573 char chopprefix[ZFS_MAXNAMELEN];
1574 boolean_t newfs = B_FALSE;
1575 boolean_t stream_wantsnewfs;
1576 uint64_t parent_snapguid = 0;
1577 prop_changelist_t *clp = NULL;
1578 nvlist_t *snapprops_nvlist = NULL;
1580 begin_time = time(NULL);
1582 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1585 if (stream_avl != NULL) {
1587 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
1592 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
1594 err = nvlist_lookup_nvlist(fs, "props", &props);
1596 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
1598 if (flags.canmountoff) {
1599 VERIFY(0 == nvlist_add_uint64(props,
1600 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
1602 ret = zcmd_write_src_nvlist(hdl, &zc, props);
1606 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
1607 VERIFY(0 == nvlist_lookup_nvlist(props,
1608 snapname, &snapprops_nvlist));
1616 * Determine how much of the snapshot name stored in the stream
1617 * we are going to tack on to the name they specified on the
1618 * command line, and how much we are going to chop off.
1620 * If they specified a snapshot, chop the entire name stored in
1623 (void) strcpy(chopprefix, drrb->drr_toname);
1624 if (flags.isprefix) {
1626 * They specified a fs with -d, we want to tack on
1627 * everything but the pool name stored in the stream
1629 if (strchr(tosnap, '@')) {
1630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1631 "argument - snapshot not allowed with -d"));
1632 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1634 cp = strchr(chopprefix, '/');
1636 cp = strchr(chopprefix, '@');
1638 } else if (strchr(tosnap, '@') == NULL) {
1640 * If they specified a filesystem without -d, we want to
1641 * tack on everything after the fs specified in the
1642 * first name from the stream.
1644 cp = strchr(chopprefix, '@');
1647 choplen = strlen(chopprefix);
1650 * Determine name of destination snapshot, store in zc_value.
1652 (void) strcpy(zc.zc_value, tosnap);
1653 (void) strncat(zc.zc_value, drrb->drr_toname+choplen,
1654 sizeof (zc.zc_value));
1655 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
1656 zcmd_free_nvlists(&zc);
1657 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1661 * Determine the name of the origin snapshot, store in zc_string.
1663 if (drrb->drr_flags & DRR_FLAG_CLONE) {
1664 if (guid_to_name(hdl, tosnap,
1665 drrb->drr_fromguid, zc.zc_string) != 0) {
1666 zcmd_free_nvlists(&zc);
1667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1668 "local origin for clone %s does not exist"),
1670 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1673 (void) printf("found clone origin %s\n", zc.zc_string);
1676 stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
1677 (drrb->drr_flags & DRR_FLAG_CLONE));
1679 if (stream_wantsnewfs) {
1681 * if the parent fs does not exist, look for it based on
1682 * the parent snap GUID
1684 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1685 "cannot receive new filesystem stream"));
1687 (void) strcpy(zc.zc_name, zc.zc_value);
1688 cp = strrchr(zc.zc_name, '/');
1692 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1693 char suffix[ZFS_MAXNAMELEN];
1694 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
1695 if (guid_to_name(hdl, tosnap, parent_snapguid,
1696 zc.zc_value) == 0) {
1697 *strchr(zc.zc_value, '@') = '\0';
1698 (void) strcat(zc.zc_value, suffix);
1703 * if the fs does not exist, look for it based on the
1706 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1707 "cannot receive incremental stream"));
1709 (void) strcpy(zc.zc_name, zc.zc_value);
1710 *strchr(zc.zc_name, '@') = '\0';
1712 if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1713 char snap[ZFS_MAXNAMELEN];
1714 (void) strcpy(snap, strchr(zc.zc_value, '@'));
1715 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
1716 zc.zc_value) == 0) {
1717 *strchr(zc.zc_value, '@') = '\0';
1718 (void) strcat(zc.zc_value, snap);
1723 (void) strcpy(zc.zc_name, zc.zc_value);
1724 *strchr(zc.zc_name, '@') = '\0';
1726 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1729 * Destination fs exists. Therefore this should either
1730 * be an incremental, or the stream specifies a new fs
1731 * (full stream or clone) and they want us to blow it
1732 * away (and have therefore specified -F and removed any
1736 if (stream_wantsnewfs) {
1738 zcmd_free_nvlists(&zc);
1739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1740 "destination '%s' exists\n"
1741 "must specify -F to overwrite it"),
1743 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1745 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
1747 zcmd_free_nvlists(&zc);
1748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1749 "destination has snapshots (eg. %s)\n"
1750 "must destroy them to overwrite it"),
1752 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1756 if ((zhp = zfs_open(hdl, zc.zc_name,
1757 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
1758 zcmd_free_nvlists(&zc);
1762 if (stream_wantsnewfs &&
1763 zhp->zfs_dmustats.dds_origin[0]) {
1764 zcmd_free_nvlists(&zc);
1766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1767 "destination '%s' is a clone\n"
1768 "must destroy it to overwrite it"),
1770 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1773 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
1774 stream_wantsnewfs) {
1775 /* We can't do online recv in this case */
1776 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
1778 zcmd_free_nvlists(&zc);
1781 if (changelist_prefix(clp) != 0) {
1782 changelist_free(clp);
1783 zcmd_free_nvlists(&zc);
1787 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
1788 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
1790 zcmd_free_nvlists(&zc);
1796 * Destination filesystem does not exist. Therefore we better
1797 * be creating a new filesystem (either from a full backup, or
1798 * a clone). It would therefore be invalid if the user
1799 * specified only the pool name (i.e. if the destination name
1800 * contained no slash character).
1802 if (!stream_wantsnewfs ||
1803 (cp = strrchr(zc.zc_name, '/')) == NULL) {
1804 zcmd_free_nvlists(&zc);
1805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1806 "destination '%s' does not exist"), zc.zc_name);
1807 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1811 * Trim off the final dataset component so we perform the
1812 * recvbackup ioctl to the filesystems's parent.
1816 if (flags.isprefix && !flags.dryrun &&
1817 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
1818 zcmd_free_nvlists(&zc);
1819 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
1825 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
1826 zc.zc_cookie = infd;
1827 zc.zc_guid = flags.force;
1828 if (flags.verbose) {
1829 (void) printf("%s %s stream of %s into %s\n",
1830 flags.dryrun ? "would receive" : "receiving",
1831 drrb->drr_fromguid ? "incremental" : "full",
1832 drrb->drr_toname, zc.zc_value);
1833 (void) fflush(stdout);
1837 zcmd_free_nvlists(&zc);
1838 return (recv_skip(hdl, infd, flags.byteswap));
1841 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
1842 ioctl_errno = errno;
1843 zcmd_free_nvlists(&zc);
1845 if (err == 0 && snapprops_nvlist) {
1846 zfs_cmd_t zc2 = { 0 };
1848 (void) strcpy(zc2.zc_name, zc.zc_value);
1849 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
1850 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
1851 zcmd_free_nvlists(&zc2);
1855 if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) {
1857 * It may be that this snapshot already exists,
1858 * in which case we want to consume & ignore it
1859 * rather than failing.
1861 avl_tree_t *local_avl;
1862 nvlist_t *local_nv, *fs;
1863 char *cp = strchr(zc.zc_value, '@');
1866 * XXX Do this faster by just iterating over snaps in
1867 * this fs. Also if zc_value does not exist, we will
1868 * get a strange "does not exist" error message.
1871 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL,
1872 &local_nv, &local_avl) == 0) {
1874 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
1875 fsavl_destroy(local_avl);
1876 nvlist_free(local_nv);
1879 if (flags.verbose) {
1880 (void) printf("snap %s already exists; "
1881 "ignoring\n", zc.zc_value);
1883 ioctl_err = recv_skip(hdl, infd,
1891 if (ioctl_err != 0) {
1892 switch (ioctl_errno) {
1894 cp = strchr(zc.zc_value, '@');
1896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1897 "most recent snapshot of %s does not\n"
1898 "match incremental source"), zc.zc_value);
1899 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1903 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1904 "destination %s has been modified\n"
1905 "since most recent snapshot"), zc.zc_name);
1906 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1909 cp = strchr(zc.zc_value, '@');
1911 /* it's the containing fs that exists */
1914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1915 "destination already exists"));
1916 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
1917 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
1922 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1925 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1926 "invalid stream (checksum mismatch)"));
1927 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1930 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
1935 * Mount or recreate the /dev links for the target filesystem
1936 * (if created, or if we tore them down to do an incremental
1937 * restore), and the /dev links for the new snapshot (if
1938 * created). Also mount any children of the target filesystem
1939 * if we did an incremental receive.
1941 cp = strchr(zc.zc_value, '@');
1942 if (cp && (ioctl_err == 0 || !newfs)) {
1946 h = zfs_open(hdl, zc.zc_value,
1947 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1949 if (h->zfs_type == ZFS_TYPE_VOLUME) {
1951 err = zvol_create_link(hdl, h->zfs_name);
1952 if (err == 0 && ioctl_err == 0)
1953 err = zvol_create_link(hdl,
1957 * Track the first/top of hierarchy fs,
1958 * for mounting and sharing later.
1960 if (top_zfs && *top_zfs == NULL)
1961 *top_zfs = zfs_strdup(hdl, zc.zc_value);
1969 err |= changelist_postfix(clp);
1970 changelist_free(clp);
1973 if (err || ioctl_err)
1976 if (flags.verbose) {
1979 uint64_t bytes = zc.zc_cookie;
1980 time_t delta = time(NULL) - begin_time;
1983 zfs_nicenum(bytes, buf1, sizeof (buf1));
1984 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
1986 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
1994 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
1995 int infd, avl_tree_t *stream_avl, char **top_zfs)
1998 dmu_replay_record_t drr, drr_noswap;
1999 struct drr_begin *drrb = &drr.drr_u.drr_begin;
2001 zio_cksum_t zcksum = { 0 };
2003 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2006 if (flags.isprefix &&
2007 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2009 "(%s) does not exist"), tosnap);
2010 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2013 /* read in the BEGIN record */
2014 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2018 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2019 /* It's the double end record at the end of a package */
2023 /* the kernel needs the non-byteswapped begin record */
2026 flags.byteswap = B_FALSE;
2027 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2029 * We computed the checksum in the wrong byteorder in
2030 * recv_read() above; do it again correctly.
2032 bzero(&zcksum, sizeof (zio_cksum_t));
2033 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2034 flags.byteswap = B_TRUE;
2036 drr.drr_type = BSWAP_32(drr.drr_type);
2037 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2038 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2039 drrb->drr_version = BSWAP_64(drrb->drr_version);
2040 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2041 drrb->drr_type = BSWAP_32(drrb->drr_type);
2042 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2043 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2044 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2047 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2049 "stream (bad magic number)"));
2050 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2053 if (strchr(drrb->drr_toname, '@') == NULL) {
2054 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2055 "stream (bad snapshot name)"));
2056 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2059 if (drrb->drr_version == DMU_BACKUP_STREAM_VERSION) {
2060 return (zfs_receive_one(hdl, infd, tosnap, flags,
2061 &drr, &drr_noswap, stream_avl, top_zfs));
2062 } else if (drrb->drr_version == DMU_BACKUP_HEADER_VERSION) {
2063 return (zfs_receive_package(hdl, infd, tosnap, flags,
2064 &drr, &zcksum, top_zfs));
2066 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2067 "stream is unsupported version %llu"),
2069 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2074 * Restores a backup of tosnap from the file descriptor specified by infd.
2075 * Return 0 on total success, -2 if some things couldn't be
2076 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2077 * (-1 will override -2).
2080 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2081 int infd, avl_tree_t *stream_avl)
2083 char *top_zfs = NULL;
2086 err = zfs_receive_impl(hdl, tosnap, flags, infd, stream_avl, &top_zfs);
2088 if (err == 0 && !flags.nomount && top_zfs) {
2090 prop_changelist_t *clp;
2092 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
2094 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
2095 CL_GATHER_MOUNT_ALWAYS, 0);
2098 /* mount and share received datasets */
2099 err = changelist_postfix(clp);
2100 changelist_free(clp);
2103 if (zhp == NULL || clp == NULL || err)