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.
37 #include <sys/param.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 /* We need to use something for ENODATA. */
55 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
56 int, avl_tree_t *, char **);
59 * Routines for dealing with the AVL tree of fs-nvlists
61 typedef struct fsavl_node {
69 fsavl_compare(const void *arg1, const void *arg2)
71 const fsavl_node_t *fn1 = arg1;
72 const fsavl_node_t *fn2 = arg2;
74 if (fn1->fn_guid > fn2->fn_guid)
76 else if (fn1->fn_guid < fn2->fn_guid)
83 * Given the GUID of a snapshot, find its containing filesystem and
87 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
92 fn_find.fn_guid = snapguid;
94 fn = avl_find(avl, &fn_find, NULL);
97 *snapname = fn->fn_snapname;
104 fsavl_destroy(avl_tree_t *avl)
113 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
120 fsavl_create(nvlist_t *fss)
123 nvpair_t *fselem = NULL;
125 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
128 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
129 offsetof(fsavl_node_t, fn_node));
131 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
132 nvlist_t *nvfs, *snaps;
133 nvpair_t *snapelem = NULL;
135 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
136 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
139 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
143 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
144 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
145 fsavl_destroy(fsavl);
149 fn->fn_snapname = nvpair_name(snapelem);
153 * Note: if there are multiple snaps with the
154 * same GUID, we ignore all but one.
156 if (avl_find(fsavl, fn, NULL) == NULL)
167 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
169 typedef struct send_data {
170 uint64_t parent_fromsnap_guid;
171 nvlist_t *parent_snaps;
174 const char *fromsnap;
178 * The header nvlist is of the following format:
181 * "fromsnap" -> string (if incremental)
185 * "name" -> string (full name; for debugging)
186 * "parentfromsnap" -> number (guid of fromsnap in parent)
188 * "props" -> { name -> value (only if set here) }
189 * "snaps" -> { name (lastname) -> number (guid) }
190 * "snapprops" -> { name (lastname) -> { name -> value } }
192 * "origin" -> number (guid) (if clone)
193 * "sent" -> boolean (not on-disk)
201 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
204 send_iterate_snap(zfs_handle_t *zhp, void *arg)
206 send_data_t *sd = arg;
207 uint64_t guid = zhp->zfs_dmustats.dds_guid;
211 snapname = strrchr(zhp->zfs_name, '@')+1;
213 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
215 * NB: if there is no fromsnap here (it's a newly created fs in
216 * an incremental replication), we will substitute the tosnap.
218 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
219 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
220 strcmp(snapname, sd->tosnap) == 0)) {
221 sd->parent_fromsnap_guid = guid;
224 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
225 send_iterate_prop(zhp, nv);
226 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
234 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
236 nvpair_t *elem = NULL;
238 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
239 char *propname = nvpair_name(elem);
240 zfs_prop_t prop = zfs_name_to_prop(propname);
243 assert(zfs_prop_user(propname) || prop != ZPROP_INVAL);
245 if (!zfs_prop_user(propname) && zfs_prop_readonly(prop))
248 verify(nvpair_value_nvlist(elem, &propnv) == 0);
249 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION) {
250 /* these guys are modifyable, but have no source */
252 verify(nvlist_lookup_uint64(propnv,
253 ZPROP_VALUE, &value) == 0);
254 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
258 if (nvlist_lookup_string(propnv,
259 ZPROP_SOURCE, &source) != 0)
261 if (strcmp(source, zhp->zfs_name) != 0)
265 if (zfs_prop_user(propname) ||
266 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
268 verify(nvlist_lookup_string(propnv,
269 ZPROP_VALUE, &value) == 0);
270 VERIFY(0 == nvlist_add_string(nv, propname, value));
273 verify(nvlist_lookup_uint64(propnv,
274 ZPROP_VALUE, &value) == 0);
275 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
281 send_iterate_fs(zfs_handle_t *zhp, void *arg)
283 send_data_t *sd = arg;
286 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
287 uint64_t guid = zhp->zfs_dmustats.dds_guid;
290 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
291 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
292 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
293 sd->parent_fromsnap_guid));
295 if (zhp->zfs_dmustats.dds_origin[0]) {
296 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
297 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
300 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
301 origin->zfs_dmustats.dds_guid));
304 /* iterate over props */
305 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
306 send_iterate_prop(zhp, nv);
307 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
310 /* iterate over snaps, and set sd->parent_fromsnap_guid */
311 sd->parent_fromsnap_guid = 0;
312 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
313 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
314 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
315 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
316 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
317 nvlist_free(sd->parent_snaps);
318 nvlist_free(sd->snapprops);
320 /* add this fs to nvlist */
321 (void) snprintf(guidstring, sizeof (guidstring),
322 "0x%llx", (longlong_t)guid);
323 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
326 /* iterate over children */
327 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
329 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
336 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
337 const char *tosnap, nvlist_t **nvlp, avl_tree_t **avlp)
340 send_data_t sd = { 0 };
343 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
345 return (EZFS_BADTYPE);
347 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
348 sd.fromsnap = fromsnap;
351 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
359 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
370 * Routines for dealing with the sorted snapshot functionality
372 typedef struct zfs_node {
373 zfs_handle_t *zn_handle;
374 avl_node_t zn_avlnode;
378 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
380 avl_tree_t *avl = data;
381 zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
383 node->zn_handle = zhp;
390 zfs_snapshot_compare(const void *larg, const void *rarg)
392 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
393 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
394 uint64_t lcreate, rcreate;
397 * Sort them according to creation time. We use the hidden
398 * CREATETXG property to get an absolute ordering of snapshots.
400 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
401 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
403 if (lcreate < rcreate)
405 else if (lcreate > rcreate)
412 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
419 avl_create(&avl, zfs_snapshot_compare,
420 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
422 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
424 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
425 ret |= callback(node->zn_handle, data);
427 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
436 * Routines specific to "zfs send"
438 typedef struct send_dump_data {
439 /* these are all just the short snapname (the part after the @) */
440 const char *fromsnap;
442 char lastsnap[ZFS_MAXNAMELEN];
443 boolean_t seenfrom, seento, replicate, doall, fromorigin;
452 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
453 * NULL) to the file descriptor specified by outfd.
456 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin,
459 zfs_cmd_t zc = { 0 };
460 libzfs_handle_t *hdl = zhp->zfs_hdl;
462 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
463 assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin);
465 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
467 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value));
468 zc.zc_cookie = outfd;
469 zc.zc_obj = fromorigin;
471 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
473 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
474 "warning: cannot send '%s'"), zhp->zfs_name);
479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
480 "not an earlier snapshot from the same fs"));
481 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
484 if (zfs_dataset_exists(hdl, zc.zc_name,
485 ZFS_TYPE_SNAPSHOT)) {
486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
487 "incremental source (@%s) does not exist"),
490 return (zfs_error(hdl, EZFS_NOENT, errbuf));
502 zfs_error_aux(hdl, strerror(errno));
503 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
506 return (zfs_standard_error(hdl, errno, errbuf));
514 dump_snapshot(zfs_handle_t *zhp, void *arg)
516 send_dump_data_t *sdd = arg;
517 const char *thissnap;
520 thissnap = strchr(zhp->zfs_name, '@') + 1;
522 if (sdd->fromsnap && !sdd->seenfrom &&
523 strcmp(sdd->fromsnap, thissnap) == 0) {
524 sdd->seenfrom = B_TRUE;
525 (void) strcpy(sdd->lastsnap, thissnap);
530 if (sdd->seento || !sdd->seenfrom) {
537 (void) fprintf(stderr, "sending from @%s to %s\n",
538 sdd->lastsnap, zhp->zfs_name);
541 err = dump_ioctl(zhp, sdd->lastsnap,
542 sdd->lastsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
545 if (!sdd->seento && strcmp(sdd->tosnap, thissnap) == 0)
546 sdd->seento = B_TRUE;
548 (void) strcpy(sdd->lastsnap, thissnap);
554 dump_filesystem(zfs_handle_t *zhp, void *arg)
557 send_dump_data_t *sdd = arg;
558 boolean_t missingfrom = B_FALSE;
559 zfs_cmd_t zc = { 0 };
561 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
562 zhp->zfs_name, sdd->tosnap);
563 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
564 (void) fprintf(stderr, "WARNING: "
565 "could not send %s@%s: does not exist\n",
566 zhp->zfs_name, sdd->tosnap);
571 if (sdd->replicate && sdd->fromsnap) {
573 * If this fs does not have fromsnap, and we're doing
574 * recursive, we need to send a full stream from the
575 * beginning (or an incremental from the origin if this
576 * is a clone). If we're doing non-recursive, then let
577 * them get the error.
579 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
580 zhp->zfs_name, sdd->fromsnap);
581 if (ioctl(zhp->zfs_hdl->libzfs_fd,
582 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
583 missingfrom = B_TRUE;
588 sdd->seenfrom = sdd->seento = sdd->lastsnap[0] = 0;
589 if (sdd->fromsnap == NULL || missingfrom)
590 sdd->seenfrom = B_TRUE;
592 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
593 if (!sdd->seenfrom) {
594 (void) fprintf(stderr,
595 "WARNING: could not send %s@%s:\n"
596 "incremental source (%s@%s) does not exist\n",
597 zhp->zfs_name, sdd->tosnap,
598 zhp->zfs_name, sdd->fromsnap);
600 } else if (!sdd->seento) {
602 (void) fprintf(stderr,
603 "WARNING: could not send %s@%s:\n"
604 "incremental source (%s@%s) "
605 "is not earlier than it\n",
606 zhp->zfs_name, sdd->tosnap,
607 zhp->zfs_name, sdd->fromsnap);
609 (void) fprintf(stderr, "WARNING: "
610 "could not send %s@%s: does not exist\n",
611 zhp->zfs_name, sdd->tosnap);
616 zfs_handle_t *snapzhp;
617 char snapname[ZFS_MAXNAMELEN];
619 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
620 zfs_get_name(zhp), sdd->tosnap);
621 snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
622 if (snapzhp == NULL) {
625 rv = dump_ioctl(snapzhp,
626 missingfrom ? NULL : sdd->fromsnap,
627 sdd->fromorigin || missingfrom,
629 sdd->seento = B_TRUE;
638 dump_filesystems(zfs_handle_t *rzhp, void *arg)
640 send_dump_data_t *sdd = arg;
642 boolean_t needagain, progress;
645 return (dump_filesystem(rzhp, sdd));
648 needagain = progress = B_FALSE;
649 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
650 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
655 uint64_t origin_guid = 0;
658 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
659 if (nvlist_lookup_boolean(fslist, "sent") == 0)
662 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
663 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
665 origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL);
667 nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) {
669 * origin has not been sent yet;
676 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
679 err = dump_filesystem(zhp, sdd);
680 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
694 * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
695 * If 'doall', dump all intermediate snaps.
696 * If 'replicate', dump special header and do recursively.
699 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
700 boolean_t replicate, boolean_t doall, boolean_t fromorigin,
701 boolean_t verbose, int outfd)
704 send_dump_data_t sdd = { 0 };
706 nvlist_t *fss = NULL;
707 avl_tree_t *fsavl = NULL;
709 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
710 "cannot send '%s'"), zhp->zfs_name);
712 if (fromsnap && fromsnap[0] == '\0') {
713 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
714 "zero-length incremental source"));
715 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
718 if (replicate || doall) {
719 dmu_replay_record_t drr = { 0 };
720 char *packbuf = NULL;
722 zio_cksum_t zc = { 0 };
724 assert(fromsnap || doall);
729 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
731 VERIFY(0 == nvlist_add_string(hdrnv,
732 "fromsnap", fromsnap));
734 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
736 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
737 fromsnap, tosnap, &fss, &fsavl);
740 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
741 err = nvlist_pack(hdrnv, &packbuf, &buflen,
745 fsavl_destroy(fsavl);
747 return (zfs_standard_error(zhp->zfs_hdl,
752 /* write first begin record */
753 drr.drr_type = DRR_BEGIN;
754 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
755 drr.drr_u.drr_begin.drr_version = DMU_BACKUP_HEADER_VERSION;
756 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
757 sizeof (drr.drr_u.drr_begin.drr_toname),
758 "%s@%s", zhp->zfs_name, tosnap);
759 drr.drr_payloadlen = buflen;
760 fletcher_4_incremental_native(&drr, sizeof (drr), &zc);
761 err = write(outfd, &drr, sizeof (drr));
763 /* write header nvlist */
765 fletcher_4_incremental_native(packbuf, buflen, &zc);
766 err = write(outfd, packbuf, buflen);
770 fsavl_destroy(fsavl);
772 return (zfs_standard_error(zhp->zfs_hdl,
776 /* write end record */
778 bzero(&drr, sizeof (drr));
779 drr.drr_type = DRR_END;
780 drr.drr_u.drr_end.drr_checksum = zc;
781 err = write(outfd, &drr, sizeof (drr));
783 fsavl_destroy(fsavl);
785 return (zfs_standard_error(zhp->zfs_hdl,
791 /* dump each stream */
792 sdd.fromsnap = fromsnap;
795 sdd.replicate = replicate;
797 sdd.fromorigin = fromorigin;
800 sdd.verbose = verbose;
801 err = dump_filesystems(zhp, &sdd);
802 fsavl_destroy(fsavl);
805 if (replicate || doall) {
807 * write final end record. NB: want to do this even if
808 * there was some error, because it might not be totally
811 dmu_replay_record_t drr = { 0 };
812 drr.drr_type = DRR_END;
813 if (write(outfd, &drr, sizeof (drr)) == -1) {
814 return (zfs_standard_error(zhp->zfs_hdl,
819 return (err || sdd.err);
823 * Routines specific to "zfs recv"
827 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
828 boolean_t byteswap, zio_cksum_t *zc)
835 rv = read(fd, cp, len);
840 if (rv < 0 || len != 0) {
841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
842 "failed to read from stream"));
843 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
849 fletcher_4_incremental_byteswap(buf, ilen, zc);
851 fletcher_4_incremental_native(buf, ilen, zc);
857 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
858 boolean_t byteswap, zio_cksum_t *zc)
863 buf = zfs_alloc(hdl, len);
867 err = recv_read(hdl, fd, buf, len, byteswap, zc);
873 err = nvlist_unpack(buf, len, nvp, 0);
876 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
877 "stream (malformed nvlist)"));
884 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
885 int baselen, char *newname, recvflags_t flags)
888 zfs_cmd_t zc = { 0 };
890 prop_changelist_t *clp;
893 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
896 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
897 flags.force ? MS_FORCE : 0);
901 err = changelist_prefix(clp);
906 (void) strcpy(newname, tryname);
908 zc.zc_objset_type = DMU_OST_ZFS;
909 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
910 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
913 (void) printf("attempting rename %s to %s\n",
914 zc.zc_name, zc.zc_value);
916 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
918 changelist_rename(clp, name, tryname);
923 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
926 (void) strncpy(newname, name, baselen);
927 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
928 "recv-%u-%u", getpid(), seq);
929 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
932 (void) printf("failed - trying rename %s to %s\n",
933 zc.zc_name, zc.zc_value);
935 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
937 changelist_rename(clp, name, newname);
938 if (err && flags.verbose) {
939 (void) printf("failed (%u) - "
940 "will try again on next pass\n", errno);
943 } else if (flags.verbose) {
945 (void) printf("success\n");
947 (void) printf("failed (%u)\n", errno);
950 (void) changelist_postfix(clp);
951 changelist_free(clp);
957 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
958 char *newname, recvflags_t flags)
960 zfs_cmd_t zc = { 0 };
962 prop_changelist_t *clp;
965 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
968 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
969 flags.force ? MS_FORCE : 0);
973 err = changelist_prefix(clp);
977 zc.zc_objset_type = DMU_OST_ZFS;
978 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
981 (void) printf("attempting destroy %s\n", zc.zc_name);
982 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
986 (void) printf("success\n");
987 changelist_remove(clp, zc.zc_name);
990 (void) changelist_postfix(clp);
991 changelist_free(clp);
994 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
999 typedef struct guid_to_name_data {
1002 } guid_to_name_data_t;
1005 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1007 guid_to_name_data_t *gtnd = arg;
1010 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1011 (void) strcpy(gtnd->name, zhp->zfs_name);
1014 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1020 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1023 /* exhaustive search all local snapshots */
1024 guid_to_name_data_t gtnd;
1032 if (strchr(parent, '@') == NULL) {
1033 zhp = make_dataset_handle(hdl, parent);
1035 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1042 cp = strchr(parent, '/');
1045 zhp = make_dataset_handle(hdl, parent);
1050 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1054 return (err == EEXIST ? 0 : ENOENT);
1059 * Return true if dataset guid1 is created before guid2.
1062 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1063 uint64_t guid1, uint64_t guid2)
1066 char *fsname, *snapname;
1067 char buf[ZFS_MAXNAMELEN];
1069 zfs_node_t zn1, zn2;
1076 nvfs = fsavl_find(avl, guid1, &snapname);
1077 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1078 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1079 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1080 if (zn1.zn_handle == NULL)
1083 nvfs = fsavl_find(avl, guid2, &snapname);
1084 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1085 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1086 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1087 if (zn2.zn_handle == NULL) {
1088 zfs_close(zn2.zn_handle);
1092 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1094 zfs_close(zn1.zn_handle);
1095 zfs_close(zn2.zn_handle);
1101 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1102 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl)
1105 avl_tree_t *local_avl;
1106 nvpair_t *fselem, *nextfselem;
1107 char *tosnap, *fromsnap;
1108 char newname[ZFS_MAXNAMELEN];
1110 boolean_t needagain, progress;
1113 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1114 VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap));
1120 needagain = progress = B_FALSE;
1122 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1123 &local_nv, &local_avl)) != 0)
1127 * Process deletes and renames
1129 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1130 fselem; fselem = nextfselem) {
1131 nvlist_t *nvfs, *snaps;
1132 nvlist_t *stream_nvfs = NULL;
1133 nvpair_t *snapelem, *nextsnapelem;
1134 uint64_t fromguid = 0;
1135 uint64_t originguid = 0;
1136 uint64_t stream_originguid = 0;
1137 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1138 char *fsname, *stream_fsname, *p1, *p2;
1140 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1142 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1143 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1144 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1145 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1146 &parent_fromsnap_guid));
1147 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1150 * First find the stream's fs, so we can check for
1151 * a different origin (due to "zfs promote")
1153 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1154 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1157 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1158 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1160 if (stream_nvfs != NULL)
1164 /* check for promote */
1165 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1166 &stream_originguid);
1167 if (stream_nvfs && originguid != stream_originguid) {
1168 switch (created_before(hdl, local_avl,
1169 stream_originguid, originguid)) {
1172 zfs_cmd_t zc = { 0 };
1173 nvlist_t *origin_nvfs;
1174 char *origin_fsname;
1177 (void) printf("promoting %s\n", fsname);
1179 origin_nvfs = fsavl_find(local_avl, originguid,
1181 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1182 "name", &origin_fsname));
1183 (void) strlcpy(zc.zc_value, origin_fsname,
1184 sizeof (zc.zc_value));
1185 (void) strlcpy(zc.zc_name, fsname,
1186 sizeof (zc.zc_name));
1187 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1195 fsavl_destroy(local_avl);
1196 nvlist_free(local_nv);
1200 * We had/have the wrong origin, therefore our
1201 * list of snapshots is wrong. Need to handle
1202 * them on the next pass.
1208 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1209 snapelem; snapelem = nextsnapelem) {
1211 char *stream_snapname;
1212 nvlist_t *found, *props;
1214 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1216 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1217 found = fsavl_find(stream_avl, thisguid,
1220 /* check for delete */
1221 if (found == NULL) {
1222 char name[ZFS_MAXNAMELEN];
1227 (void) snprintf(name, sizeof (name), "%s@%s",
1228 fsname, nvpair_name(snapelem));
1230 error = recv_destroy(hdl, name,
1231 strlen(fsname)+1, newname, flags);
1239 stream_nvfs = found;
1241 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1242 &props) && 0 == nvlist_lookup_nvlist(props,
1243 stream_snapname, &props)) {
1244 zfs_cmd_t zc = { 0 };
1246 zc.zc_cookie = B_TRUE; /* clear current props */
1247 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1248 "%s@%s", fsname, nvpair_name(snapelem));
1249 if (zcmd_write_src_nvlist(hdl, &zc,
1251 (void) zfs_ioctl(hdl,
1252 ZFS_IOC_SET_PROP, &zc);
1253 zcmd_free_nvlists(&zc);
1257 /* check for different snapname */
1258 if (strcmp(nvpair_name(snapelem),
1259 stream_snapname) != 0) {
1260 char name[ZFS_MAXNAMELEN];
1261 char tryname[ZFS_MAXNAMELEN];
1263 (void) snprintf(name, sizeof (name), "%s@%s",
1264 fsname, nvpair_name(snapelem));
1265 (void) snprintf(tryname, sizeof (name), "%s@%s",
1266 fsname, stream_snapname);
1268 error = recv_rename(hdl, name, tryname,
1269 strlen(fsname)+1, newname, flags);
1276 if (strcmp(stream_snapname, fromsnap) == 0)
1277 fromguid = thisguid;
1280 /* check for delete */
1281 if (stream_nvfs == NULL) {
1285 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1294 if (fromguid == 0 && flags.verbose) {
1295 (void) printf("local fs %s does not have fromsnap "
1296 "(%s in stream); must have been deleted locally; "
1297 "ignoring\n", fsname, fromsnap);
1301 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1302 "name", &stream_fsname));
1303 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1304 "parentfromsnap", &stream_parent_fromsnap_guid));
1306 s1 = strrchr(fsname, '/');
1307 s2 = strrchr(stream_fsname, '/');
1309 /* check for rename */
1310 if ((stream_parent_fromsnap_guid != 0 &&
1311 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1312 ((s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
1314 char tryname[ZFS_MAXNAMELEN];
1316 parent = fsavl_find(local_avl,
1317 stream_parent_fromsnap_guid, NULL);
1319 * NB: parent might not be found if we used the
1320 * tosnap for stream_parent_fromsnap_guid,
1321 * because the parent is a newly-created fs;
1322 * we'll be able to rename it after we recv the
1325 if (parent != NULL) {
1328 VERIFY(0 == nvlist_lookup_string(parent, "name",
1330 (void) snprintf(tryname, sizeof (tryname),
1331 "%s%s", pname, p2 != NULL ? p2 : "");
1334 if (flags.verbose) {
1335 (void) printf("local fs %s new parent "
1336 "not found\n", fsname);
1340 error = recv_rename(hdl, fsname, tryname,
1341 strlen(tofs)+1, newname, flags);
1349 fsavl_destroy(local_avl);
1350 nvlist_free(local_nv);
1352 if (needagain && progress) {
1353 /* do another pass to fix up temporary names */
1355 (void) printf("another pass:\n");
1363 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
1364 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
1367 nvlist_t *stream_nv = NULL;
1368 avl_tree_t *stream_avl = NULL;
1369 char *fromsnap = NULL;
1370 char tofs[ZFS_MAXNAMELEN];
1372 dmu_replay_record_t drre;
1374 boolean_t anyerr = B_FALSE;
1375 boolean_t softerr = B_FALSE;
1377 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1380 if (strchr(destname, '@')) {
1381 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1382 "can not specify snapshot name for multi-snapshot stream"));
1383 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1386 assert(drr->drr_type == DRR_BEGIN);
1387 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
1388 assert(drr->drr_u.drr_begin.drr_version == DMU_BACKUP_HEADER_VERSION);
1391 * Read in the nvlist from the stream.
1393 if (drr->drr_payloadlen != 0) {
1394 if (!flags.isprefix) {
1395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1396 "must use -d to receive replication "
1397 "(send -R) stream"));
1398 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1401 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
1402 &stream_nv, flags.byteswap, zc);
1404 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1410 * Read in the end record and verify checksum.
1412 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
1413 flags.byteswap, NULL)))
1415 if (flags.byteswap) {
1416 drre.drr_type = BSWAP_32(drre.drr_type);
1417 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
1418 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
1419 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
1420 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
1421 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
1422 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
1423 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
1424 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
1426 if (drre.drr_type != DRR_END) {
1427 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1430 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
1431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 "incorrect header checksum"));
1433 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1437 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
1439 if (drr->drr_payloadlen != 0) {
1440 nvlist_t *stream_fss;
1442 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
1444 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
1445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1446 "couldn't allocate avl tree"));
1447 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
1451 if (fromsnap != NULL) {
1452 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
1453 if (flags.isprefix) {
1454 int i = strcspn(drr->drr_u.drr_begin.drr_toname,
1456 /* zfs_receive_one() will create_parents() */
1457 (void) strlcat(tofs,
1458 &drr->drr_u.drr_begin.drr_toname[i],
1460 *strchr(tofs, '@') = '\0';
1462 softerr = recv_incremental_replication(hdl, tofs,
1463 flags, stream_nv, stream_avl);
1468 /* Finally, receive each contained stream */
1471 * we should figure out if it has a recoverable
1472 * error, in which case do a recv_skip() and drive on.
1473 * Note, if we fail due to already having this guid,
1474 * zfs_receive_one() will take care of it (ie,
1475 * recv_skip() and return 0).
1477 error = zfs_receive_impl(hdl, destname, flags, fd,
1478 stream_avl, top_zfs);
1479 if (error == ENODATA) {
1484 } while (error == 0);
1486 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
1488 * Now that we have the fs's they sent us, try the
1491 softerr = recv_incremental_replication(hdl, tofs, flags,
1492 stream_nv, stream_avl);
1496 fsavl_destroy(stream_avl);
1498 nvlist_free(stream_nv);
1507 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
1509 dmu_replay_record_t *drr;
1510 void *buf = malloc(1<<20);
1512 /* XXX would be great to use lseek if possible... */
1515 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
1516 byteswap, NULL) == 0) {
1518 drr->drr_type = BSWAP_32(drr->drr_type);
1520 switch (drr->drr_type) {
1522 /* NB: not to be used on v2 stream packages */
1523 assert(drr->drr_payloadlen == 0);
1532 drr->drr_u.drr_object.drr_bonuslen =
1533 BSWAP_32(drr->drr_u.drr_object.
1536 (void) recv_read(hdl, fd, buf,
1537 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
1543 drr->drr_u.drr_write.drr_length =
1544 BSWAP_64(drr->drr_u.drr_write.drr_length);
1546 (void) recv_read(hdl, fd, buf,
1547 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
1550 case DRR_FREEOBJECTS:
1555 assert(!"invalid record type");
1564 * Restores a backup of tosnap from the file descriptor specified by infd.
1567 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
1568 recvflags_t flags, dmu_replay_record_t *drr,
1569 dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl,
1572 zfs_cmd_t zc = { 0 };
1574 int ioctl_err, ioctl_errno, err, choplen;
1576 struct drr_begin *drrb = &drr->drr_u.drr_begin;
1578 char chopprefix[ZFS_MAXNAMELEN];
1579 boolean_t newfs = B_FALSE;
1580 boolean_t stream_wantsnewfs;
1581 uint64_t parent_snapguid = 0;
1582 prop_changelist_t *clp = NULL;
1583 nvlist_t *snapprops_nvlist = NULL;
1585 begin_time = time(NULL);
1587 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1590 if (stream_avl != NULL) {
1592 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
1597 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
1599 err = nvlist_lookup_nvlist(fs, "props", &props);
1601 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
1603 if (flags.canmountoff) {
1604 VERIFY(0 == nvlist_add_uint64(props,
1605 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
1607 ret = zcmd_write_src_nvlist(hdl, &zc, props);
1611 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
1612 VERIFY(0 == nvlist_lookup_nvlist(props,
1613 snapname, &snapprops_nvlist));
1621 * Determine how much of the snapshot name stored in the stream
1622 * we are going to tack on to the name they specified on the
1623 * command line, and how much we are going to chop off.
1625 * If they specified a snapshot, chop the entire name stored in
1628 (void) strcpy(chopprefix, drrb->drr_toname);
1629 if (flags.isprefix) {
1631 * They specified a fs with -d, we want to tack on
1632 * everything but the pool name stored in the stream
1634 if (strchr(tosnap, '@')) {
1635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1636 "argument - snapshot not allowed with -d"));
1637 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1639 cp = strchr(chopprefix, '/');
1641 cp = strchr(chopprefix, '@');
1643 } else if (strchr(tosnap, '@') == NULL) {
1645 * If they specified a filesystem without -d, we want to
1646 * tack on everything after the fs specified in the
1647 * first name from the stream.
1649 cp = strchr(chopprefix, '@');
1652 choplen = strlen(chopprefix);
1655 * Determine name of destination snapshot, store in zc_value.
1657 (void) strcpy(zc.zc_value, tosnap);
1658 (void) strncat(zc.zc_value, drrb->drr_toname+choplen,
1659 sizeof (zc.zc_value));
1660 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
1661 zcmd_free_nvlists(&zc);
1662 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1666 * Determine the name of the origin snapshot, store in zc_string.
1668 if (drrb->drr_flags & DRR_FLAG_CLONE) {
1669 if (guid_to_name(hdl, tosnap,
1670 drrb->drr_fromguid, zc.zc_string) != 0) {
1671 zcmd_free_nvlists(&zc);
1672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1673 "local origin for clone %s does not exist"),
1675 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1678 (void) printf("found clone origin %s\n", zc.zc_string);
1681 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
1682 (drrb->drr_flags & DRR_FLAG_CLONE));
1684 if (stream_wantsnewfs) {
1686 * if the parent fs does not exist, look for it based on
1687 * the parent snap GUID
1689 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1690 "cannot receive new filesystem stream"));
1692 (void) strcpy(zc.zc_name, zc.zc_value);
1693 cp = strrchr(zc.zc_name, '/');
1697 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1698 char suffix[ZFS_MAXNAMELEN];
1699 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
1700 if (guid_to_name(hdl, tosnap, parent_snapguid,
1701 zc.zc_value) == 0) {
1702 *strchr(zc.zc_value, '@') = '\0';
1703 (void) strcat(zc.zc_value, suffix);
1708 * if the fs does not exist, look for it based on the
1711 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1712 "cannot receive incremental stream"));
1714 (void) strcpy(zc.zc_name, zc.zc_value);
1715 *strchr(zc.zc_name, '@') = '\0';
1717 if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1718 char snap[ZFS_MAXNAMELEN];
1719 (void) strcpy(snap, strchr(zc.zc_value, '@'));
1720 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
1721 zc.zc_value) == 0) {
1722 *strchr(zc.zc_value, '@') = '\0';
1723 (void) strcat(zc.zc_value, snap);
1728 (void) strcpy(zc.zc_name, zc.zc_value);
1729 *strchr(zc.zc_name, '@') = '\0';
1731 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1734 * Destination fs exists. Therefore this should either
1735 * be an incremental, or the stream specifies a new fs
1736 * (full stream or clone) and they want us to blow it
1737 * away (and have therefore specified -F and removed any
1741 if (stream_wantsnewfs) {
1743 zcmd_free_nvlists(&zc);
1744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1745 "destination '%s' exists\n"
1746 "must specify -F to overwrite it"),
1748 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1750 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
1752 zcmd_free_nvlists(&zc);
1753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1754 "destination has snapshots (eg. %s)\n"
1755 "must destroy them to overwrite it"),
1757 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1761 if ((zhp = zfs_open(hdl, zc.zc_name,
1762 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
1763 zcmd_free_nvlists(&zc);
1767 if (stream_wantsnewfs &&
1768 zhp->zfs_dmustats.dds_origin[0]) {
1769 zcmd_free_nvlists(&zc);
1771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1772 "destination '%s' is a clone\n"
1773 "must destroy it to overwrite it"),
1775 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1778 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
1779 stream_wantsnewfs) {
1780 /* We can't do online recv in this case */
1781 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
1783 zcmd_free_nvlists(&zc);
1786 if (changelist_prefix(clp) != 0) {
1787 changelist_free(clp);
1788 zcmd_free_nvlists(&zc);
1792 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
1793 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
1795 zcmd_free_nvlists(&zc);
1801 * Destination filesystem does not exist. Therefore we better
1802 * be creating a new filesystem (either from a full backup, or
1803 * a clone). It would therefore be invalid if the user
1804 * specified only the pool name (i.e. if the destination name
1805 * contained no slash character).
1807 if (!stream_wantsnewfs ||
1808 (cp = strrchr(zc.zc_name, '/')) == NULL) {
1809 zcmd_free_nvlists(&zc);
1810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1811 "destination '%s' does not exist"), zc.zc_name);
1812 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1816 * Trim off the final dataset component so we perform the
1817 * recvbackup ioctl to the filesystems's parent.
1821 if (flags.isprefix && !flags.dryrun &&
1822 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
1823 zcmd_free_nvlists(&zc);
1824 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
1830 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
1831 zc.zc_cookie = infd;
1832 zc.zc_guid = flags.force;
1833 if (flags.verbose) {
1834 (void) printf("%s %s stream of %s into %s\n",
1835 flags.dryrun ? "would receive" : "receiving",
1836 drrb->drr_fromguid ? "incremental" : "full",
1837 drrb->drr_toname, zc.zc_value);
1838 (void) fflush(stdout);
1842 zcmd_free_nvlists(&zc);
1843 return (recv_skip(hdl, infd, flags.byteswap));
1846 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
1847 ioctl_errno = errno;
1848 zcmd_free_nvlists(&zc);
1850 if (err == 0 && snapprops_nvlist) {
1851 zfs_cmd_t zc2 = { 0 };
1853 (void) strcpy(zc2.zc_name, zc.zc_value);
1854 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
1855 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
1856 zcmd_free_nvlists(&zc2);
1860 if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) {
1862 * It may be that this snapshot already exists,
1863 * in which case we want to consume & ignore it
1864 * rather than failing.
1866 avl_tree_t *local_avl;
1867 nvlist_t *local_nv, *fs;
1868 char *cp = strchr(zc.zc_value, '@');
1871 * XXX Do this faster by just iterating over snaps in
1872 * this fs. Also if zc_value does not exist, we will
1873 * get a strange "does not exist" error message.
1876 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL,
1877 &local_nv, &local_avl) == 0) {
1879 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
1880 fsavl_destroy(local_avl);
1881 nvlist_free(local_nv);
1884 if (flags.verbose) {
1885 (void) printf("snap %s already exists; "
1886 "ignoring\n", zc.zc_value);
1888 ioctl_err = recv_skip(hdl, infd,
1896 if (ioctl_err != 0) {
1897 switch (ioctl_errno) {
1899 cp = strchr(zc.zc_value, '@');
1901 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1902 "most recent snapshot of %s does not\n"
1903 "match incremental source"), zc.zc_value);
1904 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1908 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1909 "destination %s has been modified\n"
1910 "since most recent snapshot"), zc.zc_name);
1911 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1914 cp = strchr(zc.zc_value, '@');
1916 /* it's the containing fs that exists */
1919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1920 "destination already exists"));
1921 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
1922 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
1927 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1930 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1931 "invalid stream (checksum mismatch)"));
1932 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1935 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
1940 * Mount or recreate the /dev links for the target filesystem
1941 * (if created, or if we tore them down to do an incremental
1942 * restore), and the /dev links for the new snapshot (if
1943 * created). Also mount any children of the target filesystem
1944 * if we did an incremental receive.
1946 cp = strchr(zc.zc_value, '@');
1947 if (cp && (ioctl_err == 0 || !newfs)) {
1951 h = zfs_open(hdl, zc.zc_value,
1952 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1954 if (h->zfs_type == ZFS_TYPE_VOLUME) {
1956 err = zvol_create_link(hdl, h->zfs_name);
1957 if (err == 0 && ioctl_err == 0)
1958 err = zvol_create_link(hdl,
1962 * Track the first/top of hierarchy fs,
1963 * for mounting and sharing later.
1965 if (top_zfs && *top_zfs == NULL)
1966 *top_zfs = zfs_strdup(hdl, zc.zc_value);
1974 err |= changelist_postfix(clp);
1975 changelist_free(clp);
1978 if (err || ioctl_err)
1981 if (flags.verbose) {
1984 uint64_t bytes = zc.zc_cookie;
1985 time_t delta = time(NULL) - begin_time;
1988 zfs_nicenum(bytes, buf1, sizeof (buf1));
1989 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
1991 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
1999 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2000 int infd, avl_tree_t *stream_avl, char **top_zfs)
2003 dmu_replay_record_t drr, drr_noswap;
2004 struct drr_begin *drrb = &drr.drr_u.drr_begin;
2006 zio_cksum_t zcksum = { 0 };
2008 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2011 if (flags.isprefix &&
2012 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2014 "(%s) does not exist"), tosnap);
2015 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2018 /* read in the BEGIN record */
2019 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2023 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2024 /* It's the double end record at the end of a package */
2028 /* the kernel needs the non-byteswapped begin record */
2031 flags.byteswap = B_FALSE;
2032 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2034 * We computed the checksum in the wrong byteorder in
2035 * recv_read() above; do it again correctly.
2037 bzero(&zcksum, sizeof (zio_cksum_t));
2038 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2039 flags.byteswap = B_TRUE;
2041 drr.drr_type = BSWAP_32(drr.drr_type);
2042 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2043 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2044 drrb->drr_version = BSWAP_64(drrb->drr_version);
2045 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2046 drrb->drr_type = BSWAP_32(drrb->drr_type);
2047 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2048 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2049 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2052 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2054 "stream (bad magic number)"));
2055 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2058 if (strchr(drrb->drr_toname, '@') == NULL) {
2059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2060 "stream (bad snapshot name)"));
2061 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2064 if (drrb->drr_version == DMU_BACKUP_STREAM_VERSION) {
2065 return (zfs_receive_one(hdl, infd, tosnap, flags,
2066 &drr, &drr_noswap, stream_avl, top_zfs));
2067 } else if (drrb->drr_version == DMU_BACKUP_HEADER_VERSION) {
2068 return (zfs_receive_package(hdl, infd, tosnap, flags,
2069 &drr, &zcksum, top_zfs));
2071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2072 "stream is unsupported version %llu"),
2074 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2079 * Restores a backup of tosnap from the file descriptor specified by infd.
2080 * Return 0 on total success, -2 if some things couldn't be
2081 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2082 * (-1 will override -2).
2085 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2086 int infd, avl_tree_t *stream_avl)
2088 char *top_zfs = NULL;
2091 err = zfs_receive_impl(hdl, tosnap, flags, infd, stream_avl, &top_zfs);
2093 if (err == 0 && !flags.nomount && top_zfs) {
2095 prop_changelist_t *clp;
2097 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
2099 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
2100 CL_GATHER_MOUNT_ALWAYS, 0);
2103 /* mount and share received datasets */
2104 err = changelist_postfix(clp);
2105 changelist_free(clp);
2108 if (zhp == NULL || clp == NULL || err)