]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzfs/libzfs_sendrecv.c
nvpair: Constify string functions
[mirror_zfs.git] / lib / libzfs / libzfs_sendrecv.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
196bee4c 24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
37abac6d 25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
95fd54a1 26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
0cee2406 27 * All rights reserved
95fd54a1 28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
671c9354 29 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
23d70cde 30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
d8d418ff 31 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
4c0883fb 32 * Copyright (c) 2019 Datto Inc.
34dc7c2f
BB
33 */
34
34dc7c2f
BB
35#include <assert.h>
36#include <ctype.h>
37#include <errno.h>
34dc7c2f
BB
38#include <libintl.h>
39#include <stdio.h>
40#include <stdlib.h>
d465fc58 41#include <string.h>
34dc7c2f
BB
42#include <unistd.h>
43#include <stddef.h>
44#include <fcntl.h>
45#include <sys/mount.h>
9b020fd9
BB
46#include <sys/mntent.h>
47#include <sys/mnttab.h>
48#include <sys/avl.h>
49#include <sys/debug.h>
5c3f61eb 50#include <sys/stat.h>
428870ff
BB
51#include <pthread.h>
52#include <umem.h>
37abac6d 53#include <time.h>
34dc7c2f
BB
54
55#include <libzfs.h>
9b67f605 56#include <libzfs_core.h>
e89f1295 57#include <libzutil.h>
34dc7c2f
BB
58
59#include "zfs_namecheck.h"
60#include "zfs_prop.h"
428870ff 61#include "zfs_fletcher.h"
34dc7c2f 62#include "libzfs_impl.h"
c618f87c 63#include <cityhash.h>
47dfff3b 64#include <zlib.h>
428870ff 65#include <sys/zio_checksum.h>
b5256303 66#include <sys/dsl_crypt.h>
428870ff 67#include <sys/ddt.h>
1b9d8c34 68#include <sys/socket.h>
3c67d83a 69#include <sys/sha2.h>
34dc7c2f 70
fcff0f35 71static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
196bee4c
MA
72 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **,
73 const char *, nvlist_t *);
30af21b0
PD
74static int guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
75 uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
76 uint64_t num_redact_snaps, char *name);
47dfff3b
MA
77static int guid_to_name(libzfs_handle_t *, const char *,
78 uint64_t, boolean_t, char *);
428870ff 79
37abac6d
BP
80typedef struct progress_arg {
81 zfs_handle_t *pa_zhp;
82 int pa_fd;
83 boolean_t pa_parsable;
30af21b0
PD
84 boolean_t pa_estimate;
85 int pa_verbosity;
19d39615 86 boolean_t pa_astitle;
05b72415 87 boolean_t pa_progress;
19d39615 88 uint64_t pa_size;
37abac6d
BP
89} progress_arg_t;
90
428870ff 91static int
01a0039a 92dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
37f8a883 93 zio_cksum_t *zc, int outfd)
428870ff 94{
37f8a883
MA
95 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
96 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
97 fletcher_4_incremental_native(drr,
98 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
99 if (drr->drr_type != DRR_BEGIN) {
100 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
101 drr_checksum.drr_checksum));
102 drr->drr_u.drr_checksum.drr_checksum = *zc;
103 }
104 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
105 sizeof (zio_cksum_t), zc);
106 if (write(outfd, drr, sizeof (*drr)) == -1)
107 return (errno);
108 if (payload_len != 0) {
109 fletcher_4_incremental_native(payload, payload_len, zc);
110 if (write(outfd, payload, payload_len) == -1)
111 return (errno);
112 }
113 return (0);
428870ff
BB
114}
115
34dc7c2f
BB
116/*
117 * Routines for dealing with the AVL tree of fs-nvlists
118 */
119typedef struct fsavl_node {
120 avl_node_t fn_node;
121 nvlist_t *fn_nvfs;
d1807f16 122 const char *fn_snapname;
34dc7c2f
BB
123 uint64_t fn_guid;
124} fsavl_node_t;
125
126static int
127fsavl_compare(const void *arg1, const void *arg2)
128{
ee36c709
GN
129 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
130 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
131
ca577779 132 return (TREE_CMP(fn1->fn_guid, fn2->fn_guid));
34dc7c2f
BB
133}
134
135/*
136 * Given the GUID of a snapshot, find its containing filesystem and
137 * (optionally) name.
138 */
139static nvlist_t *
d1807f16 140fsavl_find(avl_tree_t *avl, uint64_t snapguid, const char **snapname)
34dc7c2f
BB
141{
142 fsavl_node_t fn_find;
143 fsavl_node_t *fn;
144
145 fn_find.fn_guid = snapguid;
146
147 fn = avl_find(avl, &fn_find, NULL);
148 if (fn) {
149 if (snapname)
150 *snapname = fn->fn_snapname;
151 return (fn->fn_nvfs);
152 }
153 return (NULL);
154}
155
156static void
157fsavl_destroy(avl_tree_t *avl)
158{
159 fsavl_node_t *fn;
160 void *cookie;
161
162 if (avl == NULL)
163 return;
164
165 cookie = NULL;
166 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
167 free(fn);
168 avl_destroy(avl);
169 free(avl);
170}
171
45d1cae3
BB
172/*
173 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
174 */
34dc7c2f
BB
175static avl_tree_t *
176fsavl_create(nvlist_t *fss)
177{
178 avl_tree_t *fsavl;
179 nvpair_t *fselem = NULL;
180
181 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
182 return (NULL);
183
184 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
185 offsetof(fsavl_node_t, fn_node));
186
187 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
188 nvlist_t *nvfs, *snaps;
189 nvpair_t *snapelem = NULL;
190
60a2434b
RM
191 nvfs = fnvpair_value_nvlist(fselem);
192 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
34dc7c2f
BB
193
194 while ((snapelem =
195 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
196 fsavl_node_t *fn;
34dc7c2f 197
34dc7c2f
BB
198 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
199 fsavl_destroy(fsavl);
200 return (NULL);
201 }
202 fn->fn_nvfs = nvfs;
203 fn->fn_snapname = nvpair_name(snapelem);
1488e822 204 fn->fn_guid = fnvpair_value_uint64(snapelem);
34dc7c2f
BB
205
206 /*
207 * Note: if there are multiple snaps with the
208 * same GUID, we ignore all but one.
209 */
af2b1fbd
RM
210 avl_index_t where = 0;
211 if (avl_find(fsavl, fn, &where) == NULL)
212 avl_insert(fsavl, fn, where);
34dc7c2f
BB
213 else
214 free(fn);
215 }
216 }
217
218 return (fsavl);
219}
220
221/*
222 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
223 */
224typedef struct send_data {
66356240
K
225 /*
226 * assigned inside every recursive call,
227 * restored from *_save on return:
228 *
229 * guid of fromsnap snapshot in parent dataset
230 * txg of fromsnap snapshot in current dataset
231 * txg of tosnap snapshot in current dataset
232 */
233
34dc7c2f 234 uint64_t parent_fromsnap_guid;
66356240
K
235 uint64_t fromsnap_txg;
236 uint64_t tosnap_txg;
237
238 /* the nvlists get accumulated during depth-first traversal */
34dc7c2f
BB
239 nvlist_t *parent_snaps;
240 nvlist_t *fss;
b128c09f 241 nvlist_t *snapprops;
9c5e88b1 242 nvlist_t *snapholds; /* user holds */
66356240
K
243
244 /* send-receive configuration, does not change during traversal */
245 const char *fsname;
34dc7c2f
BB
246 const char *fromsnap;
247 const char *tosnap;
428870ff 248 boolean_t recursive;
4c0883fb 249 boolean_t raw;
f94b3cbf 250 boolean_t doall;
4c0883fb 251 boolean_t replicate;
099fa7e4 252 boolean_t skipmissing;
66356240 253 boolean_t verbose;
4c0883fb 254 boolean_t backup;
05748550
AG
255 boolean_t seenfrom;
256 boolean_t seento;
9c5e88b1
PZ
257 boolean_t holds; /* were holds requested with send -h */
258 boolean_t props;
34dc7c2f
BB
259
260 /*
261 * The header nvlist is of the following format:
262 * {
263 * "tosnap" -> string
264 * "fromsnap" -> string (if incremental)
265 * "fss" -> {
266 * id -> {
267 *
268 * "name" -> string (full name; for debugging)
269 * "parentfromsnap" -> number (guid of fromsnap in parent)
270 *
271 * "props" -> { name -> value (only if set here) }
272 * "snaps" -> { name (lastname) -> number (guid) }
b128c09f 273 * "snapprops" -> { name (lastname) -> { name -> value } }
9c5e88b1 274 * "snapholds" -> { name (lastname) -> { holdname -> crtime } }
34dc7c2f
BB
275 *
276 * "origin" -> number (guid) (if clone)
b5256303 277 * "is_encroot" -> boolean
34dc7c2f
BB
278 * "sent" -> boolean (not on-disk)
279 * }
280 * }
281 * }
282 *
283 */
284} send_data_t;
285
faa97c16 286static void
287send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
b128c09f 288
dd59c422
RM
289/*
290 * Collect guid, valid props, optionally holds, etc. of a snapshot.
291 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
292 */
34dc7c2f
BB
293static int
294send_iterate_snap(zfs_handle_t *zhp, void *arg)
295{
296 send_data_t *sd = arg;
297 uint64_t guid = zhp->zfs_dmustats.dds_guid;
66356240 298 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
e890dd85 299 boolean_t isfromsnap, istosnap, istosnapwithnofrom;
5cf3c24f 300 char *snapname;
dd59c422
RM
301 const char *from = sd->fromsnap;
302 const char *to = sd->tosnap;
34dc7c2f 303
5cf3c24f
AZ
304 snapname = strrchr(zhp->zfs_name, '@');
305 assert(snapname != NULL);
306 ++snapname;
dd59c422
RM
307
308 isfromsnap = (from != NULL && strcmp(from, snapname) == 0);
309 istosnap = (to != NULL && strcmp(to, snapname) == 0);
310 istosnapwithnofrom = (istosnap && from == NULL);
34dc7c2f 311
66356240
K
312 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
313 if (sd->verbose) {
314 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
315 "skipping snapshot %s because it was created "
316 "after the destination snapshot (%s)\n"),
dd59c422 317 zhp->zfs_name, to);
66356240
K
318 }
319 zfs_close(zhp);
320 return (0);
321 }
322
60a2434b 323 fnvlist_add_uint64(sd->parent_snaps, snapname, guid);
dd59c422 324
34dc7c2f
BB
325 /*
326 * NB: if there is no fromsnap here (it's a newly created fs in
327 * an incremental replication), we will substitute the tosnap.
328 */
dd59c422 329 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap))
34dc7c2f 330 sd->parent_fromsnap_guid = guid;
34dc7c2f 331
05748550 332 if (!sd->recursive) {
b9c07ec7
CM
333 /*
334 * To allow a doall stream to work properly
335 * with a NULL fromsnap
336 */
dd59c422 337 if (sd->doall && from == NULL && !sd->seenfrom)
b9c07ec7 338 sd->seenfrom = B_TRUE;
b9c07ec7 339
05748550
AG
340 if (!sd->seenfrom && isfromsnap) {
341 sd->seenfrom = B_TRUE;
342 zfs_close(zhp);
343 return (0);
344 }
345
e890dd85 346 if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
05748550
AG
347 zfs_close(zhp);
348 return (0);
349 }
350
351 if (istosnap)
352 sd->seento = B_TRUE;
353 }
354
dd59c422 355 nvlist_t *nv = fnvlist_alloc();
faa97c16 356 send_iterate_prop(zhp, sd->backup, nv);
60a2434b
RM
357 fnvlist_add_nvlist(sd->snapprops, snapname, nv);
358 fnvlist_free(nv);
dd59c422 359
9c5e88b1 360 if (sd->holds) {
f2b36b2d
RM
361 nvlist_t *holds;
362 if (lzc_get_holds(zhp->zfs_name, &holds) == 0) {
60a2434b 363 fnvlist_add_nvlist(sd->snapholds, snapname, holds);
f2b36b2d 364 fnvlist_free(holds);
9c5e88b1 365 }
9c5e88b1 366 }
b128c09f 367
34dc7c2f
BB
368 zfs_close(zhp);
369 return (0);
370}
371
2b6b7111
RM
372/*
373 * Collect all valid props from the handle snap into an nvlist.
374 */
34dc7c2f 375static void
faa97c16 376send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
34dc7c2f 377{
2b6b7111 378 nvlist_t *props;
34dc7c2f 379
faa97c16 380 if (received_only)
381 props = zfs_get_recvd_props(zhp);
382 else
383 props = zhp->zfs_props;
384
2b6b7111 385 nvpair_t *elem = NULL;
faa97c16 386 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
d1807f16 387 const char *propname = nvpair_name(elem);
34dc7c2f 388 zfs_prop_t prop = zfs_name_to_prop(propname);
34dc7c2f 389
428870ff
BB
390 if (!zfs_prop_user(propname)) {
391 /*
392 * Realistically, this should never happen. However,
393 * we want the ability to add DSL properties without
394 * needing to make incompatible version changes. We
395 * need to ignore unknown properties to allow older
396 * software to still send datasets containing these
397 * properties, with the unknown properties elided.
398 */
399 if (prop == ZPROP_INVAL)
400 continue;
9babb374 401
428870ff
BB
402 if (zfs_prop_readonly(prop))
403 continue;
404 }
34dc7c2f 405
2b6b7111
RM
406 nvlist_t *propnv = fnvpair_value_nvlist(elem);
407
408 boolean_t isspacelimit = (prop == ZFS_PROP_QUOTA ||
409 prop == ZFS_PROP_RESERVATION ||
45d1cae3 410 prop == ZFS_PROP_REFQUOTA ||
2b6b7111
RM
411 prop == ZFS_PROP_REFRESERVATION);
412 if (isspacelimit && zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
413 continue;
414
d1807f16 415 const char *source;
2b6b7111
RM
416 if (nvlist_lookup_string(propnv, ZPROP_SOURCE, &source) == 0) {
417 if (strcmp(source, zhp->zfs_name) != 0 &&
418 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
b128c09f 419 continue;
2b6b7111 420 } else {
428870ff
BB
421 /*
422 * May have no source before SPA_VERSION_RECVD_PROPS,
423 * but is still modifiable.
424 */
2b6b7111 425 if (!isspacelimit)
34dc7c2f
BB
426 continue;
427 }
428
429 if (zfs_prop_user(propname) ||
430 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
d1807f16 431 const char *value;
60a2434b
RM
432 value = fnvlist_lookup_string(propnv, ZPROP_VALUE);
433 fnvlist_add_string(nv, propname, value);
34dc7c2f
BB
434 } else {
435 uint64_t value;
60a2434b
RM
436 value = fnvlist_lookup_uint64(propnv, ZPROP_VALUE);
437 fnvlist_add_uint64(nv, propname, value);
34dc7c2f
BB
438 }
439 }
440}
441
e8cf3a4f
AP
442/*
443 * returns snapshot guid
444 * and returns 0 if the snapshot does not exist
445 */
446static uint64_t
447get_snap_guid(libzfs_handle_t *hdl, const char *fs, const char *snap)
448{
449 char name[MAXPATHLEN + 1];
450 uint64_t guid = 0;
451
452 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
453 return (guid);
454
455 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
456 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
457 if (zhp != NULL) {
458 guid = zfs_prop_get_int(zhp, ZFS_PROP_GUID);
459 zfs_close(zhp);
460 }
461
462 return (guid);
463}
464
66356240
K
465/*
466 * returns snapshot creation txg
467 * and returns 0 if the snapshot does not exist
468 */
469static uint64_t
470get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
471{
472 char name[ZFS_MAX_DATASET_NAME_LEN];
473 uint64_t txg = 0;
474
475 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
476 return (txg);
477
478 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
479 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
480 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
481 if (zhp != NULL) {
482 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
483 zfs_close(zhp);
484 }
485 }
486
487 return (txg);
488}
489
45d1cae3 490/*
0c1c746a 491 * Recursively generate nvlists describing datasets. See comment
45d1cae3
BB
492 * for the data structure send_data_t above for description of contents
493 * of the nvlist.
494 */
34dc7c2f
BB
495static int
496send_iterate_fs(zfs_handle_t *zhp, void *arg)
497{
498 send_data_t *sd = arg;
b5256303 499 nvlist_t *nvfs = NULL, *nv = NULL;
428870ff 500 int rv = 0;
4c0883fb 501 uint64_t min_txg = 0, max_txg = 0;
66356240 502 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
34dc7c2f 503 uint64_t guid = zhp->zfs_dmustats.dds_guid;
66356240 504 uint64_t fromsnap_txg, tosnap_txg;
34dc7c2f
BB
505 char guidstring[64];
506
0c1c746a
RM
507 /* These fields are restored on return from a recursive call. */
508 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
509 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
510 uint64_t tosnap_txg_save = sd->tosnap_txg;
511
66356240
K
512 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
513 if (fromsnap_txg != 0)
514 sd->fromsnap_txg = fromsnap_txg;
515
516 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
517 if (tosnap_txg != 0)
518 sd->tosnap_txg = tosnap_txg;
519
520 /*
0c1c746a 521 * On the send side, if the current dataset does not have tosnap,
66356240
K
522 * perform two additional checks:
523 *
0c1c746a
RM
524 * - Skip sending the current dataset if it was created later than
525 * the parent tosnap.
526 * - Return error if the current dataset was created earlier than
099fa7e4 527 * the parent tosnap, unless --skip-missing specified. Then
0c1c746a 528 * just print a warning.
66356240
K
529 */
530 if (sd->tosnap != NULL && tosnap_txg == 0) {
531 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
532 if (sd->verbose) {
533 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
534 "skipping dataset %s: snapshot %s does "
535 "not exist\n"), zhp->zfs_name, sd->tosnap);
536 }
099fa7e4
PCG
537 } else if (sd->skipmissing) {
538 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
539 "WARNING: skipping dataset %s and its children:"
540 " snapshot %s does not exist\n"),
541 zhp->zfs_name, sd->tosnap);
66356240
K
542 } else {
543 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
544 "cannot send %s@%s%s: snapshot %s@%s does not "
545 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
546 dgettext(TEXT_DOMAIN, " recursively") : "",
547 zhp->zfs_name, sd->tosnap);
30af21b0 548 rv = EZFS_NOENT;
66356240
K
549 }
550 goto out;
551 }
552
4c0883fb
AP
553 nvfs = fnvlist_alloc();
554 fnvlist_add_string(nvfs, "name", zhp->zfs_name);
0c1c746a 555 fnvlist_add_uint64(nvfs, "parentfromsnap", sd->parent_fromsnap_guid);
34dc7c2f 556
0c1c746a 557 if (zhp->zfs_dmustats.dds_origin[0] != '\0') {
34dc7c2f
BB
558 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
559 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
66356240
K
560 if (origin == NULL) {
561 rv = -1;
562 goto out;
563 }
4c0883fb
AP
564 fnvlist_add_uint64(nvfs, "origin",
565 origin->zfs_dmustats.dds_guid);
ad7e908a 566 zfs_close(origin);
34dc7c2f
BB
567 }
568
0c1c746a 569 /* Iterate over props. */
9c5e88b1 570 if (sd->props || sd->backup || sd->recursive) {
4c0883fb 571 nv = fnvlist_alloc();
9c5e88b1 572 send_iterate_prop(zhp, sd->backup, nv);
0c1c746a 573 fnvlist_add_nvlist(nvfs, "props", nv);
9c5e88b1 574 }
b5256303
TC
575 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
576 boolean_t encroot;
577
0c1c746a 578 /* Determine if this dataset is an encryption root. */
b5256303
TC
579 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
580 rv = -1;
581 goto out;
582 }
583
584 if (encroot)
4c0883fb 585 fnvlist_add_boolean(nvfs, "is_encroot");
b5256303
TC
586
587 /*
588 * Encrypted datasets can only be sent with properties if
589 * the raw flag is specified because the receive side doesn't
590 * currently have a mechanism for recursively asking the user
591 * for new encryption parameters.
592 */
593 if (!sd->raw) {
594 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
595 "cannot send %s@%s: encrypted dataset %s may not "
596 "be sent with properties without the raw flag\n"),
597 sd->fsname, sd->tosnap, zhp->zfs_name);
598 rv = -1;
599 goto out;
600 }
601
602 }
603
f94b3cbf 604 /*
0c1c746a
RM
605 * Iterate over snaps, and set sd->parent_fromsnap_guid.
606 *
f94b3cbf
TC
607 * If this is a "doall" send, a replicate send or we're just trying
608 * to gather a list of previous snapshots, iterate through all the
609 * snaps in the txg range. Otherwise just look at the one we're
610 * interested in.
611 */
0c1c746a
RM
612 sd->parent_fromsnap_guid = 0;
613 sd->parent_snaps = fnvlist_alloc();
614 sd->snapprops = fnvlist_alloc();
615 if (sd->holds)
616 sd->snapholds = fnvlist_alloc();
f94b3cbf
TC
617 if (sd->doall || sd->replicate || sd->tosnap == NULL) {
618 if (!sd->replicate && fromsnap_txg != 0)
619 min_txg = fromsnap_txg;
620 if (!sd->replicate && tosnap_txg != 0)
621 max_txg = tosnap_txg;
dc95911d 622 (void) zfs_iter_snapshots_sorted(zhp, 0, send_iterate_snap, sd,
f94b3cbf
TC
623 min_txg, max_txg);
624 } else {
dc95911d 625 char snapname[MAXPATHLEN] = { 0 };
f94b3cbf
TC
626 zfs_handle_t *snap;
627
f0ce0436 628 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
f94b3cbf
TC
629 zhp->zfs_name, sd->tosnap);
630 if (sd->fromsnap != NULL)
631 sd->seenfrom = B_TRUE;
0c1c746a 632 snap = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
f94b3cbf
TC
633 if (snap != NULL)
634 (void) send_iterate_snap(snap, sd);
635 }
636
4c0883fb 637 fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
4c0883fb 638 fnvlist_free(sd->parent_snaps);
0c1c746a 639 fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
4c0883fb 640 fnvlist_free(sd->snapprops);
0c1c746a
RM
641 if (sd->holds) {
642 fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
643 fnvlist_free(sd->snapholds);
644 }
34dc7c2f 645
7a6c12fd
AJ
646 /* Do not allow the size of the properties list to exceed the limit */
647 if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
648 zhp->zfs_hdl->libzfs_max_nvlist) {
649 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
650 "warning: cannot send %s@%s: the size of the list of "
651 "snapshots and properties is too large to be received "
652 "successfully.\n"
653 "Select a smaller number of snapshots to send.\n"),
654 zhp->zfs_name, sd->tosnap);
655 rv = EZFS_NOSPC;
656 goto out;
657 }
0c1c746a 658 /* Add this fs to nvlist. */
34dc7c2f
BB
659 (void) snprintf(guidstring, sizeof (guidstring),
660 "0x%llx", (longlong_t)guid);
4c0883fb 661 fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
34dc7c2f 662
0c1c746a 663 /* Iterate over children. */
428870ff 664 if (sd->recursive)
dc95911d 665 rv = zfs_iter_filesystems(zhp, 0, send_iterate_fs, sd);
34dc7c2f 666
66356240 667out:
0c1c746a 668 /* Restore saved fields. */
34dc7c2f 669 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
66356240
K
670 sd->fromsnap_txg = fromsnap_txg_save;
671 sd->tosnap_txg = tosnap_txg_save;
0c1c746a 672
4c0883fb
AP
673 fnvlist_free(nv);
674 fnvlist_free(nvfs);
34dc7c2f
BB
675
676 zfs_close(zhp);
677 return (rv);
678}
679
680static int
681gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
f94b3cbf 682 const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
099fa7e4
PCG
683 boolean_t replicate, boolean_t skipmissing, boolean_t verbose,
684 boolean_t backup, boolean_t holds, boolean_t props, nvlist_t **nvlp,
685 avl_tree_t **avlp)
34dc7c2f
BB
686{
687 zfs_handle_t *zhp;
688 send_data_t sd = { 0 };
689 int error;
690
691 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
692 if (zhp == NULL)
693 return (EZFS_BADTYPE);
694
60a2434b 695 sd.fss = fnvlist_alloc();
66356240 696 sd.fsname = fsname;
34dc7c2f
BB
697 sd.fromsnap = fromsnap;
698 sd.tosnap = tosnap;
428870ff 699 sd.recursive = recursive;
b5256303 700 sd.raw = raw;
f94b3cbf 701 sd.doall = doall;
4c0883fb 702 sd.replicate = replicate;
099fa7e4 703 sd.skipmissing = skipmissing;
66356240 704 sd.verbose = verbose;
faa97c16 705 sd.backup = backup;
9c5e88b1
PZ
706 sd.holds = holds;
707 sd.props = props;
34dc7c2f
BB
708
709 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
60a2434b 710 fnvlist_free(sd.fss);
34dc7c2f
BB
711 if (avlp != NULL)
712 *avlp = NULL;
713 *nvlp = NULL;
714 return (error);
715 }
716
717 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
60a2434b 718 fnvlist_free(sd.fss);
34dc7c2f
BB
719 *nvlp = NULL;
720 return (EZFS_NOMEM);
721 }
722
723 *nvlp = sd.fss;
724 return (0);
725}
726
34dc7c2f
BB
727/*
728 * Routines specific to "zfs send"
729 */
730typedef struct send_dump_data {
731 /* these are all just the short snapname (the part after the @) */
732 const char *fromsnap;
733 const char *tosnap;
eca7b760 734 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
572e2857 735 uint64_t prevsnap_obj;
34dc7c2f 736 boolean_t seenfrom, seento, replicate, doall, fromorigin;
30af21b0 737 boolean_t dryrun, parsable, progress, embed_data, std_out;
9c5e88b1 738 boolean_t large_block, compress, raw, holds;
19d39615 739 boolean_t progressastitle;
34dc7c2f
BB
740 int outfd;
741 boolean_t err;
742 nvlist_t *fss;
95fd54a1 743 nvlist_t *snapholds;
34dc7c2f 744 avl_tree_t *fsavl;
428870ff
BB
745 snapfilter_cb_t *filter_cb;
746 void *filter_cb_arg;
747 nvlist_t *debugnv;
eca7b760 748 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
572e2857 749 int cleanup_fd;
30af21b0 750 int verbosity;
330d06f9 751 uint64_t size;
34dc7c2f
BB
752} send_dump_data_t;
753
330d06f9 754static int
cf7684bc 755zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
756 enum lzc_send_flags flags, uint64_t *spacep)
330d06f9 757{
cf7684bc 758 assert(snapname != NULL);
330d06f9 759
a8747c04
RM
760 int error = lzc_send_space(snapname, from, flags, spacep);
761 if (error == 0)
762 return (0);
763
d68a2bfa 764 char errbuf[ERRBUFLEN];
a8747c04
RM
765 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
766 "warning: cannot estimate space for '%s'"), snapname);
330d06f9 767
a8747c04
RM
768 libzfs_handle_t *hdl = zhp->zfs_hdl;
769 switch (error) {
770 case EXDEV:
771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
772 "not an earlier snapshot from the same fs"));
773 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
774
775 case ENOENT:
776 if (zfs_dataset_exists(hdl, snapname,
777 ZFS_TYPE_SNAPSHOT)) {
330d06f9 778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
a8747c04
RM
779 "incremental source (%s) does not exist"),
780 snapname);
330d06f9 781 }
a8747c04
RM
782 return (zfs_error(hdl, EZFS_NOENT, errbuf));
783
784 case EDQUOT:
785 case EFBIG:
786 case EIO:
787 case ENOLINK:
788 case ENOSPC:
789 case ENOSTR:
790 case ENXIO:
791 case EPIPE:
792 case ERANGE:
793 case EFAULT:
794 case EROFS:
795 case EINVAL:
796 zfs_error_aux(hdl, "%s", strerror(error));
797 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
798
799 default:
800 return (zfs_standard_error(hdl, error, errbuf));
330d06f9 801 }
330d06f9
MA
802}
803
34dc7c2f
BB
804/*
805 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
806 * NULL) to the file descriptor specified by outfd.
807 */
808static int
572e2857 809dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
9b67f605
MA
810 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
811 nvlist_t *debugnv)
34dc7c2f 812{
13fe0198 813 zfs_cmd_t zc = {"\0"};
34dc7c2f 814 libzfs_handle_t *hdl = zhp->zfs_hdl;
428870ff 815 nvlist_t *thisdbg;
34dc7c2f
BB
816
817 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
572e2857 818 assert(fromsnap_obj == 0 || !fromorigin);
34dc7c2f
BB
819
820 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
34dc7c2f
BB
821 zc.zc_cookie = outfd;
822 zc.zc_obj = fromorigin;
572e2857
BB
823 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
824 zc.zc_fromobj = fromsnap_obj;
9b67f605 825 zc.zc_flags = flags;
428870ff 826
25074b47
RM
827 if (debugnv != NULL) {
828 thisdbg = fnvlist_alloc();
829 if (fromsnap != NULL && fromsnap[0] != '\0')
830 fnvlist_add_string(thisdbg, "fromsnap", fromsnap);
428870ff
BB
831 }
832
330d06f9 833 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
d68a2bfa 834 char errbuf[ERRBUFLEN];
25074b47
RM
835 int error = errno;
836
806739f9
DS
837 (void) snprintf(errbuf, sizeof (errbuf), "%s '%s'",
838 dgettext(TEXT_DOMAIN, "warning: cannot send"),
839 zhp->zfs_name);
34dc7c2f 840
25074b47
RM
841 if (debugnv != NULL) {
842 fnvlist_add_uint64(thisdbg, "error", error);
60a2434b 843 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
25074b47 844 fnvlist_free(thisdbg);
428870ff 845 }
428870ff 846
25074b47 847 switch (error) {
34dc7c2f
BB
848 case EXDEV:
849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
850 "not an earlier snapshot from the same fs"));
851 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
852
b5256303
TC
853 case EACCES:
854 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
855 "source key must be loaded"));
856 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
857
34dc7c2f
BB
858 case ENOENT:
859 if (zfs_dataset_exists(hdl, zc.zc_name,
860 ZFS_TYPE_SNAPSHOT)) {
861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
862 "incremental source (@%s) does not exist"),
863 zc.zc_value);
864 }
865 return (zfs_error(hdl, EZFS_NOENT, errbuf));
866
867 case EDQUOT:
868 case EFBIG:
869 case EIO:
870 case ENOLINK:
871 case ENOSPC:
872 case ENOSTR:
873 case ENXIO:
874 case EPIPE:
875 case ERANGE:
876 case EFAULT:
877 case EROFS:
860051f1 878 case EINVAL:
f00f4690 879 zfs_error_aux(hdl, "%s", strerror(errno));
34dc7c2f
BB
880 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
881
882 default:
883 return (zfs_standard_error(hdl, errno, errbuf));
884 }
885 }
886
25074b47 887 if (debugnv != NULL) {
60a2434b 888 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
25074b47
RM
889 fnvlist_free(thisdbg);
890 }
428870ff 891
34dc7c2f
BB
892 return (0);
893}
894
95fd54a1
SH
895static void
896gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
572e2857 897{
572e2857
BB
898 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
899
900 /*
95fd54a1 901 * zfs_send() only sets snapholds for sends that need them,
572e2857
BB
902 * e.g. replication and doall.
903 */
95fd54a1
SH
904 if (sdd->snapholds == NULL)
905 return;
572e2857 906
95fd54a1 907 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
572e2857
BB
908}
909
30af21b0
PD
910int
911zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written,
912 uint64_t *blocks_visited)
913{
659f4008
RM
914 zfs_cmd_t zc = {"\0"};
915
24f1aa02
RM
916 if (bytes_written != NULL)
917 *bytes_written = 0;
918 if (blocks_visited != NULL)
919 *blocks_visited = 0;
30af21b0
PD
920 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
921 zc.zc_cookie = fd;
922 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
923 return (errno);
924 if (bytes_written != NULL)
925 *bytes_written = zc.zc_cookie;
926 if (blocks_visited != NULL)
927 *blocks_visited = zc.zc_objset_type;
928 return (0);
929}
930
37abac6d
BP
931static void *
932send_progress_thread(void *arg)
933{
934 progress_arg_t *pa = arg;
37abac6d 935 zfs_handle_t *zhp = pa->pa_zhp;
30af21b0
PD
936 uint64_t bytes;
937 uint64_t blocks;
19d39615 938 uint64_t total = pa->pa_size / 100;
37abac6d 939 char buf[16];
37abac6d 940 time_t t;
8ab27948 941 struct tm tm;
fbcc25c9
RM
942 int err;
943
05b72415 944 if (!pa->pa_parsable && pa->pa_progress) {
fbcc25c9
RM
945 (void) fprintf(stderr,
946 "TIME %s %sSNAPSHOT %s\n",
947 pa->pa_estimate ? "BYTES" : " SENT",
948 pa->pa_verbosity >= 2 ? " BLOCKS " : "",
949 zhp->zfs_name);
950 }
37abac6d
BP
951
952 /*
953 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
954 */
955 for (;;) {
956 (void) sleep(1);
30af21b0
PD
957 if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes,
958 &blocks)) != 0) {
959 if (err == EINTR || err == ENOENT)
960 return ((void *)0);
961 return ((void *)(uintptr_t)err);
962 }
37abac6d 963
37abac6d 964 (void) time(&t);
8ab27948 965 localtime_r(&t, &tm);
37abac6d 966
19d39615
AH
967 if (pa->pa_astitle) {
968 char buf_bytes[16];
969 char buf_size[16];
970 int pct;
971 zfs_nicenum(bytes, buf_bytes, sizeof (buf_bytes));
972 zfs_nicenum(pa->pa_size, buf_size, sizeof (buf_size));
973 pct = (total > 0) ? bytes / total : 100;
974 zfs_setproctitle("sending %s (%d%%: %s/%s)",
975 zhp->zfs_name, MIN(pct, 100), buf_bytes, buf_size);
976 }
977
30af21b0
PD
978 if (pa->pa_verbosity >= 2 && pa->pa_parsable) {
979 (void) fprintf(stderr,
980 "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
8ab27948 981 tm.tm_hour, tm.tm_min, tm.tm_sec,
30af21b0
PD
982 (u_longlong_t)bytes, (u_longlong_t)blocks,
983 zhp->zfs_name);
984 } else if (pa->pa_verbosity >= 2) {
985 zfs_nicenum(bytes, buf, sizeof (buf));
986 (void) fprintf(stderr,
987 "%02d:%02d:%02d %5s %8llu %s\n",
8ab27948 988 tm.tm_hour, tm.tm_min, tm.tm_sec,
30af21b0
PD
989 buf, (u_longlong_t)blocks, zhp->zfs_name);
990 } else if (pa->pa_parsable) {
37abac6d 991 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
8ab27948 992 tm.tm_hour, tm.tm_min, tm.tm_sec,
30af21b0 993 (u_longlong_t)bytes, zhp->zfs_name);
05b72415 994 } else if (pa->pa_progress) {
e7fbeb60 995 zfs_nicebytes(bytes, buf, sizeof (buf));
37abac6d 996 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
8ab27948 997 tm.tm_hour, tm.tm_min, tm.tm_sec,
37abac6d
BP
998 buf, zhp->zfs_name);
999 }
1000 }
1001}
1002
e3e4c30b
AZ
1003static boolean_t
1004send_progress_thread_exit(libzfs_handle_t *hdl, pthread_t ptid)
1005{
1006 void *status = NULL;
1007 (void) pthread_cancel(ptid);
1008 (void) pthread_join(ptid, &status);
1009 int error = (int)(uintptr_t)status;
1010 if (error != 0 && status != PTHREAD_CANCELED)
1011 return (zfs_standard_error(hdl, error,
1012 dgettext(TEXT_DOMAIN, "progress thread exited nonzero")));
1013 else
1014 return (B_FALSE);
1015}
1016
47dfff3b
MA
1017static void
1018send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1019 uint64_t size, boolean_t parsable)
1020{
1021 if (parsable) {
1022 if (fromsnap != NULL) {
447e90e3
RM
1023 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1024 "incremental\t%s\t%s"), fromsnap, tosnap);
47dfff3b 1025 } else {
3c1e1933
DS
1026/*
1027 * Workaround for GCC 12+ with UBSan enabled deficencies.
1028 *
1029 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1030 * below as violating -Wformat-overflow.
1031 */
1032#if defined(__GNUC__) && !defined(__clang__) && \
1033 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1034#pragma GCC diagnostic push
1035#pragma GCC diagnostic ignored "-Wformat-overflow"
1036#endif
447e90e3
RM
1037 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1038 "full\t%s"), tosnap);
3c1e1933
DS
1039#if defined(__GNUC__) && !defined(__clang__) && \
1040 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1041#pragma GCC diagnostic pop
1042#endif
47dfff3b 1043 }
447e90e3 1044 (void) fprintf(fout, "\t%llu", (longlong_t)size);
47dfff3b
MA
1045 } else {
1046 if (fromsnap != NULL) {
1047 if (strchr(fromsnap, '@') == NULL &&
1048 strchr(fromsnap, '#') == NULL) {
1049 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
447e90e3 1050 "send from @%s to %s"), fromsnap, tosnap);
47dfff3b
MA
1051 } else {
1052 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
447e90e3 1053 "send from %s to %s"), fromsnap, tosnap);
47dfff3b
MA
1054 }
1055 } else {
1056 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
447e90e3
RM
1057 "full send of %s"), tosnap);
1058 }
1059 if (size != 0) {
1060 char buf[16];
1061 zfs_nicebytes(size, buf, sizeof (buf));
3c1e1933
DS
1062/*
1063 * Workaround for GCC 12+ with UBSan enabled deficencies.
1064 *
1065 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1066 * below as violating -Wformat-overflow.
1067 */
1068#if defined(__GNUC__) && !defined(__clang__) && \
1069 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1070#pragma GCC diagnostic push
1071#pragma GCC diagnostic ignored "-Wformat-overflow"
1072#endif
447e90e3
RM
1073 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1074 " estimated size is %s"), buf);
3c1e1933
DS
1075#if defined(__GNUC__) && !defined(__clang__) && \
1076 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1077#pragma GCC diagnostic pop
1078#endif
47dfff3b 1079 }
47dfff3b
MA
1080 }
1081 (void) fprintf(fout, "\n");
1082}
1083
1ae78351
RM
1084/*
1085 * Send a single filesystem snapshot, updating the send dump data.
1086 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
1087 */
34dc7c2f
BB
1088static int
1089dump_snapshot(zfs_handle_t *zhp, void *arg)
1090{
1091 send_dump_data_t *sdd = arg;
37abac6d
BP
1092 progress_arg_t pa = { 0 };
1093 pthread_t tid;
572e2857 1094 char *thissnap;
2aa34383 1095 enum lzc_send_flags flags = 0;
34dc7c2f 1096 int err;
330d06f9 1097 boolean_t isfromsnap, istosnap, fromorigin;
428870ff 1098 boolean_t exclude = B_FALSE;
93f6d7e2 1099 FILE *fout = sdd->std_out ? stdout : stderr;
34dc7c2f 1100
95fd54a1 1101 err = 0;
34dc7c2f 1102 thissnap = strchr(zhp->zfs_name, '@') + 1;
428870ff
BB
1103 isfromsnap = (sdd->fromsnap != NULL &&
1104 strcmp(sdd->fromsnap, thissnap) == 0);
34dc7c2f 1105
428870ff 1106 if (!sdd->seenfrom && isfromsnap) {
95fd54a1
SH
1107 gather_holds(zhp, sdd);
1108 sdd->seenfrom = B_TRUE;
1ae78351 1109 (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
95fd54a1 1110 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
34dc7c2f 1111 zfs_close(zhp);
95fd54a1 1112 return (0);
34dc7c2f
BB
1113 }
1114
1115 if (sdd->seento || !sdd->seenfrom) {
1116 zfs_close(zhp);
1117 return (0);
1118 }
1119
428870ff
BB
1120 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1121 if (istosnap)
1122 sdd->seento = B_TRUE;
1123
2aa34383
DK
1124 if (sdd->large_block)
1125 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1126 if (sdd->embed_data)
1127 flags |= LZC_SEND_FLAG_EMBED_DATA;
1128 if (sdd->compress)
1129 flags |= LZC_SEND_FLAG_COMPRESS;
b5256303
TC
1130 if (sdd->raw)
1131 flags |= LZC_SEND_FLAG_RAW;
2aa34383 1132
428870ff
BB
1133 if (!sdd->doall && !isfromsnap && !istosnap) {
1134 if (sdd->replicate) {
d1807f16 1135 const char *snapname;
428870ff
BB
1136 nvlist_t *snapprops;
1137 /*
1138 * Filter out all intermediate snapshots except origin
1139 * snapshots needed to replicate clones.
1140 */
1141 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1142 zhp->zfs_dmustats.dds_guid, &snapname);
1143
97bbeeb9
RM
1144 if (nvfs != NULL) {
1145 snapprops = fnvlist_lookup_nvlist(nvfs,
1146 "snapprops");
1147 snapprops = fnvlist_lookup_nvlist(snapprops,
1148 thissnap);
1149 exclude = !nvlist_exists(snapprops,
1150 "is_clone_origin");
1151 }
428870ff
BB
1152 } else {
1153 exclude = B_TRUE;
1154 }
1155 }
1156
1157 /*
1158 * If a filter function exists, call it to determine whether
1159 * this snapshot will be sent.
1160 */
1161 if (exclude || (sdd->filter_cb != NULL &&
1162 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1163 /*
1164 * This snapshot is filtered out. Don't send it, and don't
572e2857 1165 * set prevsnap_obj, so it will be as if this snapshot didn't
428870ff
BB
1166 * exist, and the next accepted snapshot will be sent as
1167 * an incremental from the last accepted one, or as the
1168 * first (and full) snapshot in the case of a replication,
1169 * non-incremental send.
1170 */
1171 zfs_close(zhp);
1172 return (0);
1173 }
1174
95fd54a1 1175 gather_holds(zhp, sdd);
330d06f9
MA
1176 fromorigin = sdd->prevsnap[0] == '\0' &&
1177 (sdd->fromorigin || sdd->replicate);
1178
30af21b0 1179 if (sdd->verbosity != 0) {
47dfff3b 1180 uint64_t size = 0;
cf7684bc 1181 char fromds[ZFS_MAX_DATASET_NAME_LEN];
330d06f9 1182
cf7684bc 1183 if (sdd->prevsnap[0] != '\0') {
1184 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1185 *(strchr(fromds, '@') + 1) = '\0';
1186 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1187 }
1188 if (zfs_send_space(zhp, zhp->zfs_name,
1ae78351 1189 sdd->prevsnap[0] ? fromds : NULL, flags, &size) == 0) {
cf7684bc 1190 send_print_verbose(fout, zhp->zfs_name,
1191 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1192 size, sdd->parsable);
1ae78351 1193 sdd->size += size;
cf7684bc 1194 }
34dc7c2f
BB
1195 }
1196
330d06f9 1197 if (!sdd->dryrun) {
37abac6d
BP
1198 /*
1199 * If progress reporting is requested, spawn a new thread to
1200 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1201 */
19d39615 1202 if (sdd->progress || sdd->progressastitle) {
37abac6d
BP
1203 pa.pa_zhp = zhp;
1204 pa.pa_fd = sdd->outfd;
1205 pa.pa_parsable = sdd->parsable;
30af21b0
PD
1206 pa.pa_estimate = B_FALSE;
1207 pa.pa_verbosity = sdd->verbosity;
19d39615
AH
1208 pa.pa_size = sdd->size;
1209 pa.pa_astitle = sdd->progressastitle;
05b72415 1210 pa.pa_progress = sdd->progress;
37abac6d
BP
1211
1212 if ((err = pthread_create(&tid, NULL,
23d70cde 1213 send_progress_thread, &pa)) != 0) {
37abac6d
BP
1214 zfs_close(zhp);
1215 return (err);
1216 }
1217 }
1218
330d06f9 1219 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
9b67f605 1220 fromorigin, sdd->outfd, flags, sdd->debugnv);
37abac6d 1221
19d39615 1222 if ((sdd->progress || sdd->progressastitle) &&
e3e4c30b
AZ
1223 send_progress_thread_exit(zhp->zfs_hdl, tid))
1224 return (-1);
330d06f9 1225 }
34dc7c2f 1226
a51288aa 1227 (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
572e2857 1228 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
34dc7c2f
BB
1229 zfs_close(zhp);
1230 return (err);
1231}
1232
1910a308
RM
1233/*
1234 * Send all snapshots for a filesystem, updating the send dump data.
1235 */
34dc7c2f 1236static int
1910a308 1237dump_filesystem(zfs_handle_t *zhp, send_dump_data_t *sdd)
34dc7c2f
BB
1238{
1239 int rv = 0;
34dc7c2f 1240 boolean_t missingfrom = B_FALSE;
13fe0198 1241 zfs_cmd_t zc = {"\0"};
4c0883fb 1242 uint64_t min_txg = 0, max_txg = 0;
34dc7c2f 1243
1910a308
RM
1244 /*
1245 * Make sure the tosnap exists.
1246 */
34dc7c2f
BB
1247 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1248 zhp->zfs_name, sdd->tosnap);
b834b58a 1249 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
330d06f9
MA
1250 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1251 "WARNING: could not send %s@%s: does not exist\n"),
34dc7c2f
BB
1252 zhp->zfs_name, sdd->tosnap);
1253 sdd->err = B_TRUE;
1254 return (0);
1255 }
1256
1910a308
RM
1257 /*
1258 * If this fs does not have fromsnap, and we're doing
1259 * recursive, we need to send a full stream from the
1260 * beginning (or an incremental from the origin if this
1261 * is a clone). If we're doing non-recursive, then let
1262 * them get the error.
1263 */
34dc7c2f
BB
1264 if (sdd->replicate && sdd->fromsnap) {
1265 /*
1910a308 1266 * Make sure the fromsnap exists.
34dc7c2f
BB
1267 */
1268 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1269 zhp->zfs_name, sdd->fromsnap);
1910a308 1270 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0)
34dc7c2f 1271 missingfrom = B_TRUE;
34dc7c2f
BB
1272 }
1273
1910a308
RM
1274 sdd->seenfrom = sdd->seento = B_FALSE;
1275 sdd->prevsnap[0] = '\0';
572e2857 1276 sdd->prevsnap_obj = 0;
428870ff
BB
1277 if (sdd->fromsnap == NULL || missingfrom)
1278 sdd->seenfrom = B_TRUE;
34dc7c2f 1279
f94b3cbf
TC
1280 /*
1281 * Iterate through all snapshots and process the ones we will be
1282 * sending. If we only have a "from" and "to" snapshot to deal
1283 * with, we can avoid iterating through all the other snapshots.
1284 */
1285 if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1910a308
RM
1286 if (!sdd->replicate) {
1287 if (sdd->fromsnap != NULL) {
1288 min_txg = get_snap_txg(zhp->zfs_hdl,
1289 zhp->zfs_name, sdd->fromsnap);
1290 }
1291 if (sdd->tosnap != NULL) {
1292 max_txg = get_snap_txg(zhp->zfs_hdl,
1293 zhp->zfs_name, sdd->tosnap);
1294 }
1295 }
dc95911d 1296 rv = zfs_iter_snapshots_sorted(zhp, 0, dump_snapshot, sdd,
f94b3cbf
TC
1297 min_txg, max_txg);
1298 } else {
1299 char snapname[MAXPATHLEN] = { 0 };
1300 zfs_handle_t *snap;
1301
1910a308 1302 /* Dump fromsnap. */
f94b3cbf 1303 if (!sdd->seenfrom) {
f0ce0436 1304 (void) snprintf(snapname, sizeof (snapname),
f94b3cbf
TC
1305 "%s@%s", zhp->zfs_name, sdd->fromsnap);
1306 snap = zfs_open(zhp->zfs_hdl, snapname,
1307 ZFS_TYPE_SNAPSHOT);
1308 if (snap != NULL)
1309 rv = dump_snapshot(snap, sdd);
1310 else
5f73bbba 1311 rv = errno;
f94b3cbf
TC
1312 }
1313
1910a308 1314 /* Dump tosnap. */
f94b3cbf 1315 if (rv == 0) {
f0ce0436 1316 (void) snprintf(snapname, sizeof (snapname),
f94b3cbf
TC
1317 "%s@%s", zhp->zfs_name, sdd->tosnap);
1318 snap = zfs_open(zhp->zfs_hdl, snapname,
1319 ZFS_TYPE_SNAPSHOT);
1320 if (snap != NULL)
1321 rv = dump_snapshot(snap, sdd);
1322 else
5f73bbba 1323 rv = errno;
f94b3cbf
TC
1324 }
1325 }
1326
428870ff 1327 if (!sdd->seenfrom) {
330d06f9 1328 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
428870ff 1329 "WARNING: could not send %s@%s:\n"
330d06f9 1330 "incremental source (%s@%s) does not exist\n"),
428870ff
BB
1331 zhp->zfs_name, sdd->tosnap,
1332 zhp->zfs_name, sdd->fromsnap);
1333 sdd->err = B_TRUE;
1334 } else if (!sdd->seento) {
1335 if (sdd->fromsnap) {
330d06f9 1336 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
34dc7c2f 1337 "WARNING: could not send %s@%s:\n"
428870ff 1338 "incremental source (%s@%s) "
330d06f9 1339 "is not earlier than it\n"),
34dc7c2f
BB
1340 zhp->zfs_name, sdd->tosnap,
1341 zhp->zfs_name, sdd->fromsnap);
34dc7c2f 1342 } else {
330d06f9
MA
1343 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1344 "WARNING: "
1345 "could not send %s@%s: does not exist\n"),
428870ff 1346 zhp->zfs_name, sdd->tosnap);
34dc7c2f 1347 }
428870ff 1348 sdd->err = B_TRUE;
34dc7c2f
BB
1349 }
1350
1351 return (rv);
1352}
1353
45932229
RM
1354/*
1355 * Send all snapshots for all filesystems in sdd.
1356 */
34dc7c2f 1357static int
45932229 1358dump_filesystems(zfs_handle_t *rzhp, send_dump_data_t *sdd)
34dc7c2f 1359{
34dc7c2f
BB
1360 nvpair_t *fspair;
1361 boolean_t needagain, progress;
1362
1363 if (!sdd->replicate)
1364 return (dump_filesystem(rzhp, sdd));
1365
428870ff
BB
1366 /* Mark the clone origin snapshots. */
1367 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1368 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1369 nvlist_t *nvfs;
1370 uint64_t origin_guid = 0;
1371
60a2434b 1372 nvfs = fnvpair_value_nvlist(fspair);
428870ff
BB
1373 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1374 if (origin_guid != 0) {
d1807f16 1375 const char *snapname;
428870ff
BB
1376 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1377 origin_guid, &snapname);
1378 if (origin_nv != NULL) {
1379 nvlist_t *snapprops;
60a2434b
RM
1380 snapprops = fnvlist_lookup_nvlist(origin_nv,
1381 "snapprops");
1382 snapprops = fnvlist_lookup_nvlist(snapprops,
1383 snapname);
1384 fnvlist_add_boolean(snapprops,
1385 "is_clone_origin");
428870ff
BB
1386 }
1387 }
1388 }
34dc7c2f
BB
1389again:
1390 needagain = progress = B_FALSE;
1391 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1392 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
330d06f9 1393 nvlist_t *fslist, *parent_nv;
d1807f16 1394 const char *fsname;
34dc7c2f
BB
1395 zfs_handle_t *zhp;
1396 int err;
1397 uint64_t origin_guid = 0;
330d06f9 1398 uint64_t parent_guid = 0;
34dc7c2f 1399
60a2434b 1400 fslist = fnvpair_value_nvlist(fspair);
34dc7c2f
BB
1401 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1402 continue;
1403
60a2434b 1404 fsname = fnvlist_lookup_string(fslist, "name");
34dc7c2f 1405 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
330d06f9
MA
1406 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1407 &parent_guid);
1408
1409 if (parent_guid != 0) {
1410 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1411 if (!nvlist_exists(parent_nv, "sent")) {
45932229 1412 /* Parent has not been sent; skip this one. */
330d06f9
MA
1413 needagain = B_TRUE;
1414 continue;
1415 }
1416 }
34dc7c2f 1417
428870ff
BB
1418 if (origin_guid != 0) {
1419 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1420 origin_guid, NULL);
1421 if (origin_nv != NULL &&
330d06f9 1422 !nvlist_exists(origin_nv, "sent")) {
428870ff 1423 /*
45932229 1424 * Origin has not been sent yet;
428870ff
BB
1425 * skip this clone.
1426 */
1427 needagain = B_TRUE;
1428 continue;
1429 }
34dc7c2f
BB
1430 }
1431
1432 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1433 if (zhp == NULL)
1434 return (-1);
1435 err = dump_filesystem(zhp, sdd);
60a2434b 1436 fnvlist_add_boolean(fslist, "sent");
34dc7c2f
BB
1437 progress = B_TRUE;
1438 zfs_close(zhp);
1439 if (err)
1440 return (err);
1441 }
1442 if (needagain) {
1443 assert(progress);
1444 goto again;
1445 }
330d06f9 1446
45932229 1447 /* Clean out the sent flags in case we reuse this fss. */
330d06f9
MA
1448 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1449 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1450 nvlist_t *fslist;
1451
60a2434b 1452 fslist = fnvpair_value_nvlist(fspair);
330d06f9
MA
1453 (void) nvlist_remove_all(fslist, "sent");
1454 }
1455
34dc7c2f
BB
1456 return (0);
1457}
1458
47dfff3b
MA
1459nvlist_t *
1460zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1461{
1462 unsigned int version;
1463 int nread, i;
1464 unsigned long long checksum, packed_len;
1465
1466 /*
1467 * Decode token header, which is:
1468 * <token version>-<checksum of payload>-<uncompressed payload length>
1469 * Note that the only supported token version is 1.
1470 */
1471 nread = sscanf(token, "%u-%llx-%llx-",
1472 &version, &checksum, &packed_len);
1473 if (nread != 3) {
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "resume token is corrupt (invalid format)"));
1476 return (NULL);
1477 }
1478
1479 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1481 "resume token is corrupt (invalid version %u)"),
1482 version);
1483 return (NULL);
1484 }
1485
2fafbfde 1486 /* Convert hexadecimal representation to binary. */
47dfff3b
MA
1487 token = strrchr(token, '-') + 1;
1488 int len = strlen(token) / 2;
1489 unsigned char *compressed = zfs_alloc(hdl, len);
1490 for (i = 0; i < len; i++) {
1491 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1492 if (nread != 1) {
1493 free(compressed);
1494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1495 "resume token is corrupt "
1496 "(payload is not hex-encoded)"));
1497 return (NULL);
1498 }
1499 }
1500
2fafbfde 1501 /* Verify checksum. */
47dfff3b 1502 zio_cksum_t cksum;
fc897b24 1503 fletcher_4_native_varsize(compressed, len, &cksum);
47dfff3b
MA
1504 if (cksum.zc_word[0] != checksum) {
1505 free(compressed);
1506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1507 "resume token is corrupt (incorrect checksum)"));
1508 return (NULL);
1509 }
1510
2fafbfde 1511 /* Uncompress. */
47dfff3b
MA
1512 void *packed = zfs_alloc(hdl, packed_len);
1513 uLongf packed_len_long = packed_len;
1514 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1515 packed_len_long != packed_len) {
1516 free(packed);
1517 free(compressed);
1518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1519 "resume token is corrupt (decompression failed)"));
1520 return (NULL);
1521 }
1522
2fafbfde 1523 /* Unpack nvlist. */
47dfff3b
MA
1524 nvlist_t *nv;
1525 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1526 free(packed);
1527 free(compressed);
1528 if (error != 0) {
1529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1530 "resume token is corrupt (nvlist_unpack failed)"));
1531 return (NULL);
1532 }
1533 return (nv);
1534}
2fafbfde 1535
30af21b0
PD
1536static enum lzc_send_flags
1537lzc_flags_from_sendflags(const sendflags_t *flags)
1538{
1539 enum lzc_send_flags lzc_flags = 0;
d1a38ee7 1540
30af21b0
PD
1541 if (flags->largeblock)
1542 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1543 if (flags->embed_data)
1544 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1545 if (flags->compress)
1546 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1547 if (flags->raw)
1548 lzc_flags |= LZC_SEND_FLAG_RAW;
ba0ba69e
TC
1549 if (flags->saved)
1550 lzc_flags |= LZC_SEND_FLAG_SAVED;
d1a38ee7 1551
30af21b0
PD
1552 return (lzc_flags);
1553}
1554
1555static int
1556estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
1557 uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes,
19d39615 1558 const char *redactbook, char *errbuf, uint64_t *sizep)
30af21b0
PD
1559{
1560 uint64_t size;
1561 FILE *fout = flags->dryrun ? stdout : stderr;
1562 progress_arg_t pa = { 0 };
1563 int err = 0;
1564 pthread_t ptid;
1565
19d39615 1566 if (flags->progress || flags->progressastitle) {
30af21b0
PD
1567 pa.pa_zhp = zhp;
1568 pa.pa_fd = fd;
1569 pa.pa_parsable = flags->parsable;
1570 pa.pa_estimate = B_TRUE;
1571 pa.pa_verbosity = flags->verbosity;
1572
1573 err = pthread_create(&ptid, NULL,
1574 send_progress_thread, &pa);
1575 if (err != 0) {
f00f4690 1576 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
30af21b0
PD
1577 return (zfs_error(zhp->zfs_hdl,
1578 EZFS_THREADCREATEFAILED, errbuf));
1579 }
1580 }
1581
1582 err = lzc_send_space_resume_redacted(zhp->zfs_name, from,
1583 lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes,
1584 redactbook, fd, &size);
19d39615 1585 *sizep = size;
30af21b0 1586
19d39615
AH
1587 if ((flags->progress || flags->progressastitle) &&
1588 send_progress_thread_exit(zhp->zfs_hdl, ptid))
e3e4c30b 1589 return (-1);
30af21b0 1590
19d39615
AH
1591 if (!flags->progress && !flags->parsable)
1592 return (err);
1593
30af21b0 1594 if (err != 0) {
f00f4690 1595 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
30af21b0
PD
1596 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
1597 errbuf));
1598 }
1599 send_print_verbose(fout, zhp->zfs_name, from, size,
1600 flags->parsable);
1601
1602 if (flags->parsable) {
1603 (void) fprintf(fout, "size\t%llu\n", (longlong_t)size);
1604 } else {
1605 char buf[16];
1606 zfs_nicenum(size, buf, sizeof (buf));
1607 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1608 "total estimated size is %s\n"), buf);
1609 }
1610 return (0);
1611}
1612
1613static boolean_t
1614redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
1615{
1616 for (int i = 0; i < num_snaps; i++) {
1617 if (snaps[i] == guid)
1618 return (B_TRUE);
1619 }
1620 return (B_FALSE);
1621}
1622
1623static boolean_t
1624redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1,
1625 const uint64_t *snaps2, uint64_t num_snaps2)
1626{
1627 if (num_snaps1 != num_snaps2)
1628 return (B_FALSE);
1629 for (int i = 0; i < num_snaps1; i++) {
1630 if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i]))
1631 return (B_FALSE);
1632 }
1633 return (B_TRUE);
1634}
1635
102eb673
RM
1636static int
1637get_bookmarks(const char *path, nvlist_t **bmarksp)
1638{
1639 nvlist_t *props = fnvlist_alloc();
1640 int error;
1641
1642 fnvlist_add_boolean(props, "redact_complete");
1643 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1644 error = lzc_get_bookmarks(path, props, bmarksp);
1645 fnvlist_free(props);
1646 return (error);
1647}
1648
1649static nvpair_t *
1650find_redact_pair(nvlist_t *bmarks, const uint64_t *redact_snap_guids,
1651 int num_redact_snaps)
1652{
1653 nvpair_t *pair;
1654
1655 for (pair = nvlist_next_nvpair(bmarks, NULL); pair;
1656 pair = nvlist_next_nvpair(bmarks, pair)) {
1657
1658 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1659 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark,
1660 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1661 uint_t len = 0;
1662 uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist,
1663 ZPROP_VALUE, &len);
1664 if (redact_snaps_equal(redact_snap_guids,
1665 num_redact_snaps, bmarksnaps, len)) {
1666 break;
1667 }
1668 }
1669 return (pair);
1670}
1671
1672static boolean_t
1673get_redact_complete(nvpair_t *pair)
1674{
1675 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1676 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete");
1677 boolean_t complete = fnvlist_lookup_boolean_value(vallist,
1678 ZPROP_VALUE);
1679
1680 return (complete);
1681}
1682
30af21b0
PD
1683/*
1684 * Check that the list of redaction snapshots in the bookmark matches the send
1685 * we're resuming, and return whether or not it's complete.
1686 *
1687 * Note that the caller needs to free the contents of *bookname with free() if
1688 * this function returns successfully.
1689 */
1690static int
1691find_redact_book(libzfs_handle_t *hdl, const char *path,
1692 const uint64_t *redact_snap_guids, int num_redact_snaps,
1693 char **bookname)
1694{
d68a2bfa 1695 char errbuf[ERRBUFLEN];
30af21b0
PD
1696 nvlist_t *bmarks;
1697
1698 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1699 "cannot resume send"));
1700
102eb673 1701 int error = get_bookmarks(path, &bmarks);
30af21b0
PD
1702 if (error != 0) {
1703 if (error == ESRCH) {
1704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1705 "nonexistent redaction bookmark provided"));
1706 } else if (error == ENOENT) {
1707 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1708 "dataset to be sent no longer exists"));
1709 } else {
1710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1711 "unknown error: %s"), strerror(error));
1712 }
1713 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1714 }
102eb673
RM
1715 nvpair_t *pair = find_redact_pair(bmarks, redact_snap_guids,
1716 num_redact_snaps);
30af21b0
PD
1717 if (pair == NULL) {
1718 fnvlist_free(bmarks);
1719 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1720 "no appropriate redaction bookmark exists"));
1721 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1722 }
102eb673 1723 boolean_t complete = get_redact_complete(pair);
30af21b0
PD
1724 if (!complete) {
1725 fnvlist_free(bmarks);
1726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1727 "incomplete redaction bookmark provided"));
1728 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1729 }
102eb673 1730 *bookname = strndup(nvpair_name(pair), ZFS_MAX_DATASET_NAME_LEN);
30af21b0
PD
1731 ASSERT3P(*bookname, !=, NULL);
1732 fnvlist_free(bmarks);
1733 return (0);
1734}
47dfff3b 1735
d1a38ee7
RM
1736static enum lzc_send_flags
1737lzc_flags_from_resume_nvl(nvlist_t *resume_nvl)
1738{
1739 enum lzc_send_flags lzc_flags = 0;
1740
1741 if (nvlist_exists(resume_nvl, "largeblockok"))
1742 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1743 if (nvlist_exists(resume_nvl, "embedok"))
1744 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1745 if (nvlist_exists(resume_nvl, "compressok"))
1746 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1747 if (nvlist_exists(resume_nvl, "rawok"))
1748 lzc_flags |= LZC_SEND_FLAG_RAW;
1749 if (nvlist_exists(resume_nvl, "savedok"))
1750 lzc_flags |= LZC_SEND_FLAG_SAVED;
1751
1752 return (lzc_flags);
1753}
1754
ba0ba69e 1755static int
3a909fe3
AZ
1756zfs_send_resume_impl_cb_impl(libzfs_handle_t *hdl, sendflags_t *flags,
1757 int outfd, nvlist_t *resume_nvl)
47dfff3b 1758{
d68a2bfa 1759 char errbuf[ERRBUFLEN];
d1807f16
RY
1760 const char *toname;
1761 const char *fromname = NULL;
47dfff3b
MA
1762 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1763 zfs_handle_t *zhp;
1764 int error = 0;
eca7b760 1765 char name[ZFS_MAX_DATASET_NAME_LEN];
30af21b0
PD
1766 FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr;
1767 uint64_t *redact_snap_guids = NULL;
1768 int num_redact_snaps = 0;
1769 char *redact_book = NULL;
19d39615 1770 uint64_t size = 0;
47dfff3b
MA
1771
1772 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1773 "cannot resume send"));
1774
30af21b0 1775 if (flags->verbosity != 0) {
aee1dd4d 1776 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
47dfff3b 1777 "resume token contents:\n"));
aee1dd4d 1778 nvlist_print(fout, resume_nvl);
47dfff3b
MA
1779 }
1780
1781 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1782 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1783 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1784 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1785 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1787 "resume token is corrupt"));
1788 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1789 }
1790 fromguid = 0;
1791 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1792
ba0ba69e 1793 if (flags->saved) {
a51288aa 1794 (void) strlcpy(name, toname, sizeof (name));
ba0ba69e
TC
1795 } else {
1796 error = guid_to_name(hdl, toname, toguid, B_FALSE, name);
1797 if (error != 0) {
1798 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1800 "'%s' is no longer the same snapshot "
1801 "used in the initial send"), toname);
1802 } else {
1803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1804 "'%s' used in the initial send no "
1805 "longer exists"), toname);
1806 }
1807 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
47dfff3b 1808 }
47dfff3b 1809 }
ba0ba69e 1810
47dfff3b
MA
1811 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1812 if (zhp == NULL) {
1813 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1814 "unable to access '%s'"), name);
1815 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1816 }
1817
30af21b0
PD
1818 if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps",
1819 &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) {
1820 num_redact_snaps = -1;
1821 }
1822
47dfff3b 1823 if (fromguid != 0) {
30af21b0
PD
1824 if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE,
1825 redact_snap_guids, num_redact_snaps, name) != 0) {
47dfff3b
MA
1826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1827 "incremental source %#llx no longer exists"),
1828 (longlong_t)fromguid);
1829 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1830 }
1831 fromname = name;
1832 }
1833
30af21b0
PD
1834 redact_snap_guids = NULL;
1835
1836 if (nvlist_lookup_uint64_array(resume_nvl,
1837 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids,
1838 (uint_t *)&num_redact_snaps) == 0) {
1839 char path[ZFS_MAX_DATASET_NAME_LEN];
1840
1841 (void) strlcpy(path, toname, sizeof (path));
1842 char *at = strchr(path, '@');
1843 ASSERT3P(at, !=, NULL);
1844
1845 *at = '\0';
1846
1847 if ((error = find_redact_book(hdl, path, redact_snap_guids,
1848 num_redact_snaps, &redact_book)) != 0) {
1849 return (error);
1850 }
1851 }
1852
d1a38ee7
RM
1853 enum lzc_send_flags lzc_flags = lzc_flags_from_sendflags(flags) |
1854 lzc_flags_from_resume_nvl(resume_nvl);
1855
19d39615 1856 if (flags->verbosity != 0 || flags->progressastitle) {
30af21b0
PD
1857 /*
1858 * Some of these may have come from the resume token, set them
1859 * here for size estimate purposes.
1860 */
1861 sendflags_t tmpflags = *flags;
1862 if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK)
1863 tmpflags.largeblock = B_TRUE;
1864 if (lzc_flags & LZC_SEND_FLAG_COMPRESS)
1865 tmpflags.compress = B_TRUE;
1866 if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA)
1867 tmpflags.embed_data = B_TRUE;
8f11b1d2
PD
1868 if (lzc_flags & LZC_SEND_FLAG_RAW)
1869 tmpflags.raw = B_TRUE;
1870 if (lzc_flags & LZC_SEND_FLAG_SAVED)
1871 tmpflags.saved = B_TRUE;
30af21b0 1872 error = estimate_size(zhp, fromname, outfd, &tmpflags,
19d39615 1873 resumeobj, resumeoff, bytes, redact_book, errbuf, &size);
47dfff3b
MA
1874 }
1875
1876 if (!flags->dryrun) {
1877 progress_arg_t pa = { 0 };
1878 pthread_t tid;
1879 /*
1880 * If progress reporting is requested, spawn a new thread to
1881 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1882 */
19d39615 1883 if (flags->progress || flags->progressastitle) {
47dfff3b
MA
1884 pa.pa_zhp = zhp;
1885 pa.pa_fd = outfd;
1886 pa.pa_parsable = flags->parsable;
30af21b0
PD
1887 pa.pa_estimate = B_FALSE;
1888 pa.pa_verbosity = flags->verbosity;
19d39615
AH
1889 pa.pa_size = size;
1890 pa.pa_astitle = flags->progressastitle;
05b72415 1891 pa.pa_progress = flags->progress;
47dfff3b
MA
1892
1893 error = pthread_create(&tid, NULL,
1894 send_progress_thread, &pa);
1895 if (error != 0) {
30af21b0
PD
1896 if (redact_book != NULL)
1897 free(redact_book);
47dfff3b
MA
1898 zfs_close(zhp);
1899 return (error);
1900 }
1901 }
1902
30af21b0
PD
1903 error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd,
1904 lzc_flags, resumeobj, resumeoff, redact_book);
1905 if (redact_book != NULL)
1906 free(redact_book);
47dfff3b 1907
19d39615
AH
1908 if ((flags->progressastitle || flags->progress) &&
1909 send_progress_thread_exit(hdl, tid)) {
1910 zfs_close(zhp);
e3e4c30b 1911 return (-1);
19d39615 1912 }
47dfff3b 1913
d68a2bfa 1914 char errbuf[ERRBUFLEN];
47dfff3b
MA
1915 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1916 "warning: cannot send '%s'"), zhp->zfs_name);
1917
1918 zfs_close(zhp);
1919
1920 switch (error) {
1921 case 0:
1922 return (0);
b5256303
TC
1923 case EACCES:
1924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1925 "source key must be loaded"));
1926 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
30af21b0
PD
1927 case ESRCH:
1928 if (lzc_exists(zhp->zfs_name)) {
1929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1930 "incremental source could not be found"));
1931 }
1932 return (zfs_error(hdl, EZFS_NOENT, errbuf));
b5256303 1933
47dfff3b
MA
1934 case EXDEV:
1935 case ENOENT:
1936 case EDQUOT:
1937 case EFBIG:
1938 case EIO:
1939 case ENOLINK:
1940 case ENOSPC:
1941 case ENOSTR:
1942 case ENXIO:
1943 case EPIPE:
1944 case ERANGE:
1945 case EFAULT:
1946 case EROFS:
f00f4690 1947 zfs_error_aux(hdl, "%s", strerror(errno));
47dfff3b
MA
1948 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1949
1950 default:
1951 return (zfs_standard_error(hdl, errno, errbuf));
1952 }
30af21b0
PD
1953 } else {
1954 if (redact_book != NULL)
1955 free(redact_book);
47dfff3b
MA
1956 }
1957
47dfff3b
MA
1958 zfs_close(zhp);
1959
1960 return (error);
1961}
1962
3a909fe3
AZ
1963struct zfs_send_resume_impl {
1964 libzfs_handle_t *hdl;
1965 sendflags_t *flags;
1966 nvlist_t *resume_nvl;
1967};
1968
1969static int
1970zfs_send_resume_impl_cb(int outfd, void *arg)
1971{
1972 struct zfs_send_resume_impl *zsri = arg;
1973 return (zfs_send_resume_impl_cb_impl(zsri->hdl, zsri->flags, outfd,
1974 zsri->resume_nvl));
1975}
1976
1977static int
1978zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1979 nvlist_t *resume_nvl)
1980{
1981 struct zfs_send_resume_impl zsri = {
1982 .hdl = hdl,
1983 .flags = flags,
1984 .resume_nvl = resume_nvl,
1985 };
1986 return (lzc_send_wrapper(zfs_send_resume_impl_cb, outfd, &zsri));
1987}
1988
ba0ba69e
TC
1989int
1990zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1991 const char *resume_token)
1992{
1993 int ret;
d68a2bfa 1994 char errbuf[ERRBUFLEN];
ba0ba69e
TC
1995 nvlist_t *resume_nvl;
1996
1997 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1998 "cannot resume send"));
1999
2000 resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token);
2001 if (resume_nvl == NULL) {
2002 /*
2003 * zfs_error_aux has already been set by
2004 * zfs_send_resume_token_to_nvlist()
2005 */
2006 return (zfs_error(hdl, EZFS_FAULT, errbuf));
2007 }
2008
2009 ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl);
60a2434b 2010 fnvlist_free(resume_nvl);
ba0ba69e
TC
2011
2012 return (ret);
2013}
2014
2015int
2016zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd,
2017 const char *resume_token)
2018{
2019 int ret;
2020 libzfs_handle_t *hdl = zhp->zfs_hdl;
2021 nvlist_t *saved_nvl = NULL, *resume_nvl = NULL;
2022 uint64_t saved_guid = 0, resume_guid = 0;
2023 uint64_t obj = 0, off = 0, bytes = 0;
2024 char token_buf[ZFS_MAXPROPLEN];
d68a2bfa 2025 char errbuf[ERRBUFLEN];
ba0ba69e
TC
2026
2027 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2028 "saved send failed"));
2029
2030 ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
2031 token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE);
2032 if (ret != 0)
2033 goto out;
2034
2035 saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf);
2036 if (saved_nvl == NULL) {
2037 /*
2038 * zfs_error_aux has already been set by
2039 * zfs_send_resume_token_to_nvlist()
2040 */
2041 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2042 goto out;
2043 }
2044
2045 /*
2046 * If a resume token is provided we use the object and offset
2047 * from that instead of the default, which starts from the
2048 * beginning.
2049 */
2050 if (resume_token != NULL) {
2051 resume_nvl = zfs_send_resume_token_to_nvlist(hdl,
2052 resume_token);
2053 if (resume_nvl == NULL) {
2054 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2055 goto out;
2056 }
2057
2058 if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 ||
2059 nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 ||
2060 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
2061 nvlist_lookup_uint64(resume_nvl, "toguid",
2062 &resume_guid) != 0) {
2063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2064 "provided resume token is corrupt"));
2065 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2066 goto out;
2067 }
2068
2069 if (nvlist_lookup_uint64(saved_nvl, "toguid",
2070 &saved_guid)) {
2071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2072 "dataset's resume token is corrupt"));
2073 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2074 goto out;
2075 }
2076
2077 if (resume_guid != saved_guid) {
2078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2079 "provided resume token does not match dataset"));
2080 ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf);
2081 goto out;
2082 }
2083 }
2084
2085 (void) nvlist_remove_all(saved_nvl, "object");
2086 fnvlist_add_uint64(saved_nvl, "object", obj);
2087
2088 (void) nvlist_remove_all(saved_nvl, "offset");
2089 fnvlist_add_uint64(saved_nvl, "offset", off);
2090
2091 (void) nvlist_remove_all(saved_nvl, "bytes");
2092 fnvlist_add_uint64(saved_nvl, "bytes", bytes);
2093
2094 (void) nvlist_remove_all(saved_nvl, "toname");
2095 fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name);
2096
2097 ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl);
2098
2099out:
60a2434b
RM
2100 fnvlist_free(saved_nvl);
2101 fnvlist_free(resume_nvl);
ba0ba69e
TC
2102 return (ret);
2103}
2104
34dc7c2f 2105/*
30af21b0
PD
2106 * This function informs the target system that the recursive send is complete.
2107 * The record is also expected in the case of a send -p.
2108 */
2109static int
2110send_conclusion_record(int fd, zio_cksum_t *zc)
2111{
2112 dmu_replay_record_t drr = { 0 };
2113 drr.drr_type = DRR_END;
2114 if (zc != NULL)
2115 drr.drr_u.drr_end.drr_checksum = *zc;
2116 if (write(fd, &drr, sizeof (drr)) == -1) {
2117 return (errno);
2118 }
2119 return (0);
2120}
2121
2122/*
2123 * This function is responsible for sending the records that contain the
2124 * necessary information for the target system's libzfs to be able to set the
2125 * properties of the filesystem being received, or to be able to prepare for
2126 * a recursive receive.
2127 *
2128 * The "zhp" argument is the handle of the snapshot we are sending
2129 * (the "tosnap"). The "from" argument is the short snapshot name (the part
2130 * after the @) of the incremental source.
2131 */
2132static int
2133send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
2134 boolean_t gather_props, boolean_t recursive, boolean_t verbose,
099fa7e4
PCG
2135 boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t skipmissing,
2136 boolean_t backup, boolean_t holds, boolean_t props, boolean_t doall,
30af21b0
PD
2137 nvlist_t **fssp, avl_tree_t **fsavlp)
2138{
2139 int err = 0;
2140 char *packbuf = NULL;
2141 size_t buflen = 0;
2142 zio_cksum_t zc = { {0} };
2143 int featureflags = 0;
2144 /* name of filesystem/volume that contains snapshot we are sending */
2145 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2146 /* short name of snap we are sending */
a926aab9 2147 const char *tosnap = "";
30af21b0 2148
d68a2bfa 2149 char errbuf[ERRBUFLEN];
30af21b0
PD
2150 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2151 "warning: cannot send '%s'"), zhp->zfs_name);
2152 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp,
2153 ZFS_PROP_VERSION) >= ZPL_VERSION_SA) {
2154 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
2155 }
2156
2157 if (holds)
2158 featureflags |= DMU_BACKUP_FEATURE_HOLDS;
2159
2160 (void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN);
2161 char *at = strchr(tofs, '@');
2162 if (at != NULL) {
2163 *at = '\0';
2164 tosnap = at + 1;
2165 }
2166
2167 if (gather_props) {
2168 nvlist_t *hdrnv = fnvlist_alloc();
2169 nvlist_t *fss = NULL;
2170
2171 if (from != NULL)
2172 fnvlist_add_string(hdrnv, "fromsnap", from);
2173 fnvlist_add_string(hdrnv, "tosnap", tosnap);
2174 if (!recursive)
2175 fnvlist_add_boolean(hdrnv, "not_recursive");
2176
2177 if (raw) {
60a2434b 2178 fnvlist_add_boolean(hdrnv, "raw");
30af21b0
PD
2179 }
2180
6a42939f 2181 if (gather_nvlist(zhp->zfs_hdl, tofs,
099fa7e4 2182 from, tosnap, recursive, raw, doall, replicate, skipmissing,
6a42939f 2183 verbose, backup, holds, props, &fss, fsavlp) != 0) {
30af21b0
PD
2184 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2185 errbuf));
2186 }
7a6c12fd
AJ
2187 /*
2188 * Do not allow the size of the properties list to exceed
2189 * the limit
2190 */
2191 if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
2192 zhp->zfs_hdl->libzfs_max_nvlist) {
2193 (void) snprintf(errbuf, sizeof (errbuf),
2194 dgettext(TEXT_DOMAIN, "warning: cannot send '%s': "
2195 "the size of the list of snapshots and properties "
2196 "is too large to be received successfully.\n"
2197 "Select a smaller number of snapshots to send.\n"),
2198 zhp->zfs_name);
2199 return (zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
2200 errbuf));
2201 }
30af21b0
PD
2202 fnvlist_add_nvlist(hdrnv, "fss", fss);
2203 VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR,
2204 0));
2205 if (fssp != NULL) {
2206 *fssp = fss;
2207 } else {
60a2434b 2208 fnvlist_free(fss);
30af21b0 2209 }
60a2434b 2210 fnvlist_free(hdrnv);
30af21b0
PD
2211 }
2212
2213 if (!dryrun) {
2214 dmu_replay_record_t drr = { 0 };
2215 /* write first begin record */
2216 drr.drr_type = DRR_BEGIN;
2217 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2218 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2219 drr_versioninfo, DMU_COMPOUNDSTREAM);
2220 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2221 drr_versioninfo, featureflags);
2222 if (snprintf(drr.drr_u.drr_begin.drr_toname,
2223 sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs,
2224 tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) {
2225 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2226 errbuf));
2227 }
2228 drr.drr_payloadlen = buflen;
2229
2230 err = dump_record(&drr, packbuf, buflen, &zc, fd);
2231 free(packbuf);
2232 if (err != 0) {
f00f4690 2233 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
30af21b0
PD
2234 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2235 errbuf));
2236 }
2237 err = send_conclusion_record(fd, &zc);
2238 if (err != 0) {
f00f4690 2239 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
30af21b0
PD
2240 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2241 errbuf));
2242 }
2243 }
2244 return (0);
2245}
2246
2247/*
2248 * Generate a send stream. The "zhp" argument is the filesystem/volume
2249 * that contains the snapshot to send. The "fromsnap" argument is the
2250 * short name (the part after the '@') of the snapshot that is the
2251 * incremental source to send from (if non-NULL). The "tosnap" argument
2252 * is the short name of the snapshot to send.
45d1cae3
BB
2253 *
2254 * The content of the send stream is the snapshot identified by
2255 * 'tosnap'. Incremental streams are requested in two ways:
2256 * - from the snapshot identified by "fromsnap" (if non-null) or
2257 * - from the origin of the dataset identified by zhp, which must
2258 * be a clone. In this case, "fromsnap" is null and "fromorigin"
2259 * is TRUE.
2260 *
2261 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
428870ff 2262 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
45d1cae3 2263 * if "replicate" is set. If "doall" is set, dump all the intermediate
428870ff
BB
2264 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2265 * case too. If "props" is set, send properties.
3a909fe3
AZ
2266 *
2267 * Pre-wrapped (cf. lzc_send_wrapper()).
34dc7c2f 2268 */
3a909fe3
AZ
2269static int
2270zfs_send_cb_impl(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
330d06f9 2271 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
428870ff 2272 void *cb_arg, nvlist_t **debugnvp)
34dc7c2f 2273{
d68a2bfa 2274 char errbuf[ERRBUFLEN];
34dc7c2f 2275 send_dump_data_t sdd = { 0 };
330d06f9 2276 int err = 0;
34dc7c2f
BB
2277 nvlist_t *fss = NULL;
2278 avl_tree_t *fsavl = NULL;
428870ff
BB
2279 static uint64_t holdseq;
2280 int spa_version;
93f6d7e2 2281 FILE *fout;
428870ff 2282
34dc7c2f
BB
2283 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2284 "cannot send '%s'"), zhp->zfs_name);
2285
2286 if (fromsnap && fromsnap[0] == '\0') {
2287 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2288 "zero-length incremental source"));
2289 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2290 }
2291
aad91df0
RE
2292 if (fromsnap) {
2293 char full_fromsnap_name[ZFS_MAX_DATASET_NAME_LEN];
2294 if (snprintf(full_fromsnap_name, sizeof (full_fromsnap_name),
2295 "%s@%s", zhp->zfs_name, fromsnap) >=
2296 sizeof (full_fromsnap_name)) {
2297 err = EINVAL;
2298 goto stderr_out;
2299 }
2300 zfs_handle_t *fromsnapn = zfs_open(zhp->zfs_hdl,
2301 full_fromsnap_name, ZFS_TYPE_SNAPSHOT);
2302 if (fromsnapn == NULL) {
2303 err = -1;
2304 goto err_out;
2305 }
2306 zfs_close(fromsnapn);
2307 }
2308
9c5e88b1
PZ
2309 if (flags->replicate || flags->doall || flags->props ||
2310 flags->holds || flags->backup) {
30af21b0
PD
2311 char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN];
2312 if (snprintf(full_tosnap_name, sizeof (full_tosnap_name),
2313 "%s@%s", zhp->zfs_name, tosnap) >=
2314 sizeof (full_tosnap_name)) {
2315 err = EINVAL;
2316 goto stderr_out;
34dc7c2f 2317 }
30af21b0
PD
2318 zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl,
2319 full_tosnap_name, ZFS_TYPE_SNAPSHOT);
1d20b763 2320 if (tosnap == NULL) {
2321 err = -1;
2322 goto err_out;
2323 }
30af21b0
PD
2324 err = send_prelim_records(tosnap, fromsnap, outfd,
2325 flags->replicate || flags->props || flags->holds,
2326 flags->replicate, flags->verbosity > 0, flags->dryrun,
099fa7e4
PCG
2327 flags->raw, flags->replicate, flags->skipmissing,
2328 flags->backup, flags->holds, flags->props, flags->doall,
2329 &fss, &fsavl);
30af21b0
PD
2330 zfs_close(tosnap);
2331 if (err != 0)
2332 goto err_out;
34dc7c2f
BB
2333 }
2334
2335 /* dump each stream */
2336 sdd.fromsnap = fromsnap;
2337 sdd.tosnap = tosnap;
36482bf6 2338 sdd.outfd = outfd;
330d06f9
MA
2339 sdd.replicate = flags->replicate;
2340 sdd.doall = flags->doall;
2341 sdd.fromorigin = flags->fromorigin;
34dc7c2f
BB
2342 sdd.fss = fss;
2343 sdd.fsavl = fsavl;
30af21b0 2344 sdd.verbosity = flags->verbosity;
330d06f9 2345 sdd.parsable = flags->parsable;
37abac6d 2346 sdd.progress = flags->progress;
19d39615 2347 sdd.progressastitle = flags->progressastitle;
330d06f9 2348 sdd.dryrun = flags->dryrun;
f1512ee6 2349 sdd.large_block = flags->largeblock;
9b67f605 2350 sdd.embed_data = flags->embed_data;
2aa34383 2351 sdd.compress = flags->compress;
b5256303 2352 sdd.raw = flags->raw;
9c5e88b1 2353 sdd.holds = flags->holds;
428870ff
BB
2354 sdd.filter_cb = filter_func;
2355 sdd.filter_cb_arg = cb_arg;
2356 if (debugnvp)
2357 sdd.debugnv = *debugnvp;
30af21b0 2358 if (sdd.verbosity != 0 && sdd.dryrun)
93f6d7e2
MJ
2359 sdd.std_out = B_TRUE;
2360 fout = sdd.std_out ? stdout : stderr;
e956d651
CS
2361
2362 /*
2363 * Some flags require that we place user holds on the datasets that are
2364 * being sent so they don't get destroyed during the send. We can skip
2365 * this step if the pool is imported read-only since the datasets cannot
2366 * be destroyed.
2367 */
2368 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2369 ZPOOL_PROP_READONLY, NULL) &&
2370 zfs_spa_version(zhp, &spa_version) == 0 &&
2371 spa_version >= SPA_VERSION_USERREFS &&
2372 (flags->doall || flags->replicate)) {
572e2857
BB
2373 ++holdseq;
2374 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2375 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
10b575d0 2376 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
572e2857
BB
2377 if (sdd.cleanup_fd < 0) {
2378 err = errno;
2379 goto stderr_out;
2380 }
95fd54a1 2381 sdd.snapholds = fnvlist_alloc();
572e2857
BB
2382 } else {
2383 sdd.cleanup_fd = -1;
95fd54a1 2384 sdd.snapholds = NULL;
572e2857 2385 }
9c5e88b1 2386
30af21b0 2387 if (flags->verbosity != 0 || sdd.snapholds != NULL) {
330d06f9
MA
2388 /*
2389 * Do a verbose no-op dry run to get all the verbose output
95fd54a1
SH
2390 * or to gather snapshot hold's before generating any data,
2391 * then do a non-verbose real run to generate the streams.
330d06f9
MA
2392 */
2393 sdd.dryrun = B_TRUE;
2394 err = dump_filesystems(zhp, &sdd);
95fd54a1
SH
2395
2396 if (err != 0)
2397 goto stderr_out;
2398
30af21b0 2399 if (flags->verbosity != 0) {
95fd54a1 2400 if (flags->parsable) {
93f6d7e2 2401 (void) fprintf(fout, "size\t%llu\n",
95fd54a1
SH
2402 (longlong_t)sdd.size);
2403 } else {
2404 char buf[16];
e7fbeb60 2405 zfs_nicebytes(sdd.size, buf, sizeof (buf));
93f6d7e2 2406 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
95fd54a1
SH
2407 "total estimated size is %s\n"), buf);
2408 }
330d06f9 2409 }
95fd54a1
SH
2410
2411 /* Ensure no snaps found is treated as an error. */
2412 if (!sdd.seento) {
2413 err = ENOENT;
2414 goto err_out;
2415 }
2416
2417 /* Skip the second run if dryrun was requested. */
2418 if (flags->dryrun)
2419 goto err_out;
2420
2421 if (sdd.snapholds != NULL) {
2422 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2423 if (err != 0)
2424 goto stderr_out;
2425
2426 fnvlist_free(sdd.snapholds);
2427 sdd.snapholds = NULL;
2428 }
2429
2430 sdd.dryrun = B_FALSE;
30af21b0 2431 sdd.verbosity = 0;
330d06f9 2432 }
95fd54a1 2433
34dc7c2f
BB
2434 err = dump_filesystems(zhp, &sdd);
2435 fsavl_destroy(fsavl);
60a2434b 2436 fnvlist_free(fss);
34dc7c2f 2437
95fd54a1
SH
2438 /* Ensure no snaps found is treated as an error. */
2439 if (err == 0 && !sdd.seento)
2440 err = ENOENT;
2441
572e2857
BB
2442 if (sdd.cleanup_fd != -1) {
2443 VERIFY(0 == close(sdd.cleanup_fd));
2444 sdd.cleanup_fd = -1;
2445 }
2446
330d06f9 2447 if (!flags->dryrun && (flags->replicate || flags->doall ||
9c5e88b1 2448 flags->props || flags->backup || flags->holds)) {
34dc7c2f
BB
2449 /*
2450 * write final end record. NB: want to do this even if
2451 * there was some error, because it might not be totally
2452 * failed.
2453 */
10891b37
AH
2454 int err2 = send_conclusion_record(outfd, NULL);
2455 if (err2 != 0)
2456 return (zfs_standard_error(zhp->zfs_hdl, err2, errbuf));
34dc7c2f
BB
2457 }
2458
2459 return (err || sdd.err);
428870ff
BB
2460
2461stderr_out:
2462 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2463err_out:
95fd54a1 2464 fsavl_destroy(fsavl);
60a2434b 2465 fnvlist_free(fss);
95fd54a1
SH
2466 fnvlist_free(sdd.snapholds);
2467
572e2857
BB
2468 if (sdd.cleanup_fd != -1)
2469 VERIFY(0 == close(sdd.cleanup_fd));
428870ff 2470 return (err);
34dc7c2f
BB
2471}
2472
3a909fe3
AZ
2473struct zfs_send {
2474 zfs_handle_t *zhp;
2475 const char *fromsnap;
2476 const char *tosnap;
2477 sendflags_t *flags;
2478 snapfilter_cb_t *filter_func;
2479 void *cb_arg;
2480 nvlist_t **debugnvp;
2481};
2482
2483static int
2484zfs_send_cb(int outfd, void *arg)
2485{
2486 struct zfs_send *zs = arg;
2487 return (zfs_send_cb_impl(zs->zhp, zs->fromsnap, zs->tosnap, zs->flags,
2488 outfd, zs->filter_func, zs->cb_arg, zs->debugnvp));
2489}
2490
2491int
2492zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2493 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2494 void *cb_arg, nvlist_t **debugnvp)
2495{
2496 struct zfs_send arg = {
2497 .zhp = zhp,
2498 .fromsnap = fromsnap,
2499 .tosnap = tosnap,
2500 .flags = flags,
2501 .filter_func = filter_func,
2502 .cb_arg = cb_arg,
2503 .debugnvp = debugnvp,
2504 };
2505 return (lzc_send_wrapper(zfs_send_cb, outfd, &arg));
2506}
2507
2508
65c7cc49 2509static zfs_handle_t *
30af21b0
PD
2510name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname)
2511{
2512 char dirname[ZFS_MAX_DATASET_NAME_LEN];
2513 (void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN);
2514 char *c = strchr(dirname, '@');
2515 if (c != NULL)
2516 *c = '\0';
2517 return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET));
2518}
2519
2520/*
2521 * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2522 * an earlier snapshot in the same filesystem, or a snapshot before later's
2523 * origin, or it's origin's origin, etc.
2524 */
2525static boolean_t
2526snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later)
2527{
2528 boolean_t ret;
2529 uint64_t later_txg =
2530 (later->zfs_type == ZFS_TYPE_FILESYSTEM ||
2531 later->zfs_type == ZFS_TYPE_VOLUME ?
2532 UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG));
2533 uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG);
2534
2535 if (earlier_txg >= later_txg)
2536 return (B_FALSE);
2537
2538 zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl,
2539 earlier->zfs_name);
2540 zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl,
2541 later->zfs_name);
2542
2543 if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) {
2544 zfs_close(earlier_dir);
2545 zfs_close(later_dir);
2546 return (B_TRUE);
2547 }
2548
2549 char clonename[ZFS_MAX_DATASET_NAME_LEN];
2550 if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename,
2551 ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) {
2552 zfs_close(earlier_dir);
2553 zfs_close(later_dir);
2554 return (B_FALSE);
2555 }
2556
2557 zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename,
2558 ZFS_TYPE_DATASET);
2559 uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG);
2560
2561 /*
2562 * If "earlier" is exactly the origin, then
2563 * snapshot_is_before(earlier, origin) will return false (because
2564 * they're the same).
2565 */
2566 if (origin_txg == earlier_txg &&
2567 strcmp(origin->zfs_name, earlier->zfs_name) == 0) {
2568 zfs_close(earlier_dir);
2569 zfs_close(later_dir);
2570 zfs_close(origin);
2571 return (B_TRUE);
2572 }
2573 zfs_close(earlier_dir);
2574 zfs_close(later_dir);
2575
2576 ret = snapshot_is_before(earlier, origin);
2577 zfs_close(origin);
2578 return (ret);
2579}
2580
2581/*
2582 * The "zhp" argument is the handle of the dataset to send (typically a
2583 * snapshot). The "from" argument is the full name of the snapshot or
2584 * bookmark that is the incremental source.
3a909fe3
AZ
2585 *
2586 * Pre-wrapped (cf. lzc_send_wrapper()).
30af21b0 2587 */
3a909fe3
AZ
2588static int
2589zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd,
2590 sendflags_t *flags, const char *redactbook)
da536844 2591{
30af21b0 2592 int err;
da536844 2593 libzfs_handle_t *hdl = zhp->zfs_hdl;
22df2457 2594 char *name = zhp->zfs_name;
196bee4c 2595 pthread_t ptid;
30af21b0 2596 progress_arg_t pa = { 0 };
19d39615 2597 uint64_t size = 0;
30af21b0 2598
d68a2bfa 2599 char errbuf[ERRBUFLEN];
30af21b0 2600 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
22df2457 2601 "warning: cannot send '%s'"), name);
835db585 2602
30af21b0
PD
2603 if (from != NULL && strchr(from, '@')) {
2604 zfs_handle_t *from_zhp = zfs_open(hdl, from,
2605 ZFS_TYPE_DATASET);
1d20b763 2606 if (from_zhp == NULL)
2607 return (-1);
30af21b0
PD
2608 if (!snapshot_is_before(from_zhp, zhp)) {
2609 zfs_close(from_zhp);
2610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2611 "not an earlier snapshot from the same fs"));
2612 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2613 }
2614 zfs_close(from_zhp);
2615 }
835db585 2616
22df2457
RM
2617 if (redactbook != NULL) {
2618 char bookname[ZFS_MAX_DATASET_NAME_LEN];
2619 nvlist_t *redact_snaps;
2620 zfs_handle_t *book_zhp;
2621 char *at, *pound;
2622 int dsnamelen;
2623
2624 pound = strchr(redactbook, '#');
2625 if (pound != NULL)
2626 redactbook = pound + 1;
2627 at = strchr(name, '@');
2628 if (at == NULL) {
2629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2630 "cannot do a redacted send to a filesystem"));
2631 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2632 }
2633 dsnamelen = at - name;
2634 if (snprintf(bookname, sizeof (bookname), "%.*s#%s",
2635 dsnamelen, name, redactbook)
2636 >= sizeof (bookname)) {
2637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2638 "invalid bookmark name"));
2639 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2640 }
2641 book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK);
2642 if (book_zhp == NULL)
2643 return (-1);
2644 if (nvlist_lookup_nvlist(book_zhp->zfs_props,
2645 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2646 &redact_snaps) != 0 || redact_snaps == NULL) {
2647 zfs_close(book_zhp);
2648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2649 "not a redaction bookmark"));
2650 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2651 }
2652 zfs_close(book_zhp);
2653 }
2654
30af21b0
PD
2655 /*
2656 * Send fs properties
2657 */
2658 if (flags->props || flags->holds || flags->backup) {
2659 /*
2660 * Note: the header generated by send_prelim_records()
2661 * assumes that the incremental source is in the same
2662 * filesystem/volume as the target (which is a requirement
2663 * when doing "zfs send -R"). But that isn't always the
2664 * case here (e.g. send from snap in origin, or send from
2665 * bookmark). We pass from=NULL, which will omit this
2666 * information from the prelim records; it isn't used
2667 * when receiving this type of stream.
2668 */
2669 err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE,
2670 flags->verbosity > 0, flags->dryrun, flags->raw,
099fa7e4 2671 flags->replicate, B_FALSE, flags->backup, flags->holds,
30af21b0
PD
2672 flags->props, flags->doall, NULL, NULL);
2673 if (err != 0)
2674 return (err);
2675 }
2676
2677 /*
2678 * Perform size estimate if verbose was specified.
2679 */
19d39615 2680 if (flags->verbosity != 0 || flags->progressastitle) {
30af21b0 2681 err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook,
19d39615 2682 errbuf, &size);
30af21b0
PD
2683 if (err != 0)
2684 return (err);
2685 }
2686
2687 if (flags->dryrun)
2688 return (0);
2689
30af21b0
PD
2690 /*
2691 * If progress reporting is requested, spawn a new thread to poll
2692 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2693 */
19d39615 2694 if (flags->progress || flags->progressastitle) {
30af21b0
PD
2695 pa.pa_zhp = zhp;
2696 pa.pa_fd = fd;
2697 pa.pa_parsable = flags->parsable;
2698 pa.pa_estimate = B_FALSE;
2699 pa.pa_verbosity = flags->verbosity;
19d39615
AH
2700 pa.pa_size = size;
2701 pa.pa_astitle = flags->progressastitle;
05b72415 2702 pa.pa_progress = flags->progress;
30af21b0
PD
2703
2704 err = pthread_create(&ptid, NULL,
2705 send_progress_thread, &pa);
2706 if (err != 0) {
f00f4690 2707 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
30af21b0
PD
2708 return (zfs_error(zhp->zfs_hdl,
2709 EZFS_THREADCREATEFAILED, errbuf));
835db585 2710 }
2711 }
2712
22df2457 2713 err = lzc_send_redacted(name, from, fd,
30af21b0 2714 lzc_flags_from_sendflags(flags), redactbook);
835db585 2715
19d39615
AH
2716 if ((flags->progress || flags->progressastitle) &&
2717 send_progress_thread_exit(hdl, ptid))
e3e4c30b 2718 return (-1);
da536844 2719
8623bd96 2720 if (err == 0 && (flags->props || flags->holds || flags->backup)) {
30af21b0 2721 /* Write the final end record. */
c14ad80f 2722 err = send_conclusion_record(fd, NULL);
30af21b0
PD
2723 if (err != 0)
2724 return (zfs_standard_error(hdl, err, errbuf));
2725 }
da536844
MA
2726 if (err != 0) {
2727 switch (errno) {
2728 case EXDEV:
2729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2730 "not an earlier snapshot from the same fs"));
2731 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2732
2733 case ENOENT:
2734 case ESRCH:
22df2457 2735 if (lzc_exists(name)) {
da536844
MA
2736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2737 "incremental source (%s) does not exist"),
2738 from);
2739 }
2740 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2741
b5256303
TC
2742 case EACCES:
2743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2744 "dataset key must be loaded"));
2745 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2746
da536844
MA
2747 case EBUSY:
2748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2749 "target is busy; if a filesystem, "
2750 "it must not be mounted"));
2751 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2752
2753 case EDQUOT:
22df2457 2754 case EFAULT:
da536844 2755 case EFBIG:
22df2457 2756 case EINVAL:
da536844
MA
2757 case EIO:
2758 case ENOLINK:
2759 case ENOSPC:
2760 case ENOSTR:
2761 case ENXIO:
2762 case EPIPE:
2763 case ERANGE:
da536844 2764 case EROFS:
f00f4690 2765 zfs_error_aux(hdl, "%s", strerror(errno));
da536844
MA
2766 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2767
2768 default:
2769 return (zfs_standard_error(hdl, errno, errbuf));
2770 }
2771 }
2772 return (err != 0);
2773}
2774
3a909fe3
AZ
2775struct zfs_send_one {
2776 zfs_handle_t *zhp;
2777 const char *from;
2778 sendflags_t *flags;
2779 const char *redactbook;
2780};
2781
2782static int
2783zfs_send_one_cb(int fd, void *arg)
2784{
2785 struct zfs_send_one *zso = arg;
2786 return (zfs_send_one_cb_impl(zso->zhp, zso->from, fd, zso->flags,
2787 zso->redactbook));
2788}
2789
2790int
2791zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
2792 const char *redactbook)
2793{
2794 struct zfs_send_one zso = {
2795 .zhp = zhp,
2796 .from = from,
2797 .flags = flags,
2798 .redactbook = redactbook,
2799 };
2800 return (lzc_send_wrapper(zfs_send_one_cb, fd, &zso));
2801}
2802
34dc7c2f
BB
2803/*
2804 * Routines specific to "zfs recv"
2805 */
2806
2807static int
2808recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2809 boolean_t byteswap, zio_cksum_t *zc)
2810{
2811 char *cp = buf;
2812 int rv;
2813 int len = ilen;
2814
2815 do {
2816 rv = read(fd, cp, len);
2817 cp += rv;
2818 len -= rv;
2819 } while (rv > 0);
2820
2821 if (rv < 0 || len != 0) {
2822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2823 "failed to read from stream"));
2824 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2825 "cannot receive")));
2826 }
2827
2828 if (zc) {
2829 if (byteswap)
2830 fletcher_4_incremental_byteswap(buf, ilen, zc);
2831 else
2832 fletcher_4_incremental_native(buf, ilen, zc);
2833 }
2834 return (0);
2835}
2836
2837static int
2838recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2839 boolean_t byteswap, zio_cksum_t *zc)
2840{
2841 char *buf;
2842 int err;
2843
2844 buf = zfs_alloc(hdl, len);
34dc7c2f 2845
7a6c12fd
AJ
2846 if (len > hdl->libzfs_max_nvlist) {
2847 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
908d43d0 2848 free(buf);
7a6c12fd
AJ
2849 return (ENOMEM);
2850 }
2851
34dc7c2f
BB
2852 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2853 if (err != 0) {
2854 free(buf);
2855 return (err);
2856 }
2857
2858 err = nvlist_unpack(buf, len, nvp, 0);
2859 free(buf);
2860 if (err != 0) {
2861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2862 "stream (malformed nvlist)"));
2863 return (EINVAL);
2864 }
2865 return (0);
2866}
2867
b5256303
TC
2868/*
2869 * Returns the grand origin (origin of origin of origin...) of a given handle.
2870 * If this dataset is not a clone, it simply returns a copy of the original
2871 * handle.
2872 */
2873static zfs_handle_t *
2874recv_open_grand_origin(zfs_handle_t *zhp)
2875{
2876 char origin[ZFS_MAX_DATASET_NAME_LEN];
610cb4fb 2877 zprop_source_t src;
b5256303
TC
2878 zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2879
2880 while (ozhp != NULL) {
2881 if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2882 sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2883 break;
2884
2885 (void) zfs_close(ozhp);
2886 ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2887 }
2888
2889 return (ozhp);
2890}
2891
2892static int
dc1c630b 2893recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
b5256303
TC
2894{
2895 int err;
2896 zfs_handle_t *ozhp = NULL;
2897
2898 /*
2899 * Attempt to rename the dataset. If it fails with EACCES we have
2900 * attempted to rename the dataset outside of its encryption root.
2901 * Force the dataset to become an encryption root and try again.
2902 */
dc1c630b 2903 err = lzc_rename(name, newname);
b5256303
TC
2904 if (err == EACCES) {
2905 ozhp = recv_open_grand_origin(zhp);
2906 if (ozhp == NULL) {
2907 err = ENOENT;
2908 goto out;
2909 }
2910
2911 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2912 NULL, NULL, 0);
2913 if (err != 0)
2914 goto out;
2915
dc1c630b 2916 err = lzc_rename(name, newname);
b5256303
TC
2917 }
2918
2919out:
2920 if (ozhp != NULL)
2921 zfs_close(ozhp);
2922 return (err);
2923}
2924
34dc7c2f
BB
2925static int
2926recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
330d06f9 2927 int baselen, char *newname, recvflags_t *flags)
34dc7c2f
BB
2928{
2929 static int seq;
34dc7c2f 2930 int err;
b5256303
TC
2931 prop_changelist_t *clp = NULL;
2932 zfs_handle_t *zhp = NULL;
34dc7c2f
BB
2933
2934 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
b5256303
TC
2935 if (zhp == NULL) {
2936 err = -1;
2937 goto out;
2938 }
b128c09f 2939 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
330d06f9 2940 flags->force ? MS_FORCE : 0);
b5256303
TC
2941 if (clp == NULL) {
2942 err = -1;
2943 goto out;
2944 }
34dc7c2f
BB
2945 err = changelist_prefix(clp);
2946 if (err)
b5256303 2947 goto out;
34dc7c2f
BB
2948
2949 if (tryname) {
a51288aa 2950 (void) strlcpy(newname, tryname, ZFS_MAX_DATASET_NAME_LEN);
330d06f9 2951 if (flags->verbose) {
34dc7c2f 2952 (void) printf("attempting rename %s to %s\n",
dc1c630b 2953 name, newname);
34dc7c2f 2954 }
dc1c630b 2955 err = recv_rename_impl(zhp, name, newname);
34dc7c2f
BB
2956 if (err == 0)
2957 changelist_rename(clp, name, tryname);
2958 } else {
2959 err = ENOENT;
2960 }
2961
13fe0198 2962 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
34dc7c2f
BB
2963 seq++;
2964
eca7b760
IK
2965 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2966 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
34dc7c2f 2967
330d06f9 2968 if (flags->verbose) {
34dc7c2f 2969 (void) printf("failed - trying rename %s to %s\n",
dc1c630b 2970 name, newname);
34dc7c2f 2971 }
dc1c630b 2972 err = recv_rename_impl(zhp, name, newname);
34dc7c2f
BB
2973 if (err == 0)
2974 changelist_rename(clp, name, newname);
330d06f9 2975 if (err && flags->verbose) {
34dc7c2f
BB
2976 (void) printf("failed (%u) - "
2977 "will try again on next pass\n", errno);
2978 }
2979 err = EAGAIN;
330d06f9 2980 } else if (flags->verbose) {
34dc7c2f
BB
2981 if (err == 0)
2982 (void) printf("success\n");
2983 else
2984 (void) printf("failed (%u)\n", errno);
2985 }
2986
2987 (void) changelist_postfix(clp);
b5256303
TC
2988
2989out:
2990 if (clp != NULL)
2991 changelist_free(clp);
2992 if (zhp != NULL)
2993 zfs_close(zhp);
2994
2995 return (err);
2996}
2997
2998static int
2999recv_promote(libzfs_handle_t *hdl, const char *fsname,
3000 const char *origin_fsname, recvflags_t *flags)
3001{
3002 int err;
3003 zfs_cmd_t zc = {"\0"};
3004 zfs_handle_t *zhp = NULL, *ozhp = NULL;
3005
3006 if (flags->verbose)
3007 (void) printf("promoting %s\n", fsname);
3008
3009 (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
3010 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
3011
3012 /*
3013 * Attempt to promote the dataset. If it fails with EACCES the
3014 * promotion would cause this dataset to leave its encryption root.
3015 * Force the origin to become an encryption root and try again.
3016 */
3017 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3018 if (err == EACCES) {
3019 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3020 if (zhp == NULL) {
3021 err = -1;
3022 goto out;
3023 }
3024
3025 ozhp = recv_open_grand_origin(zhp);
3026 if (ozhp == NULL) {
3027 err = -1;
3028 goto out;
3029 }
3030
3031 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
3032 NULL, NULL, 0);
3033 if (err != 0)
3034 goto out;
3035
3036 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3037 }
3038
3039out:
3040 if (zhp != NULL)
3041 zfs_close(zhp);
3042 if (ozhp != NULL)
3043 zfs_close(ozhp);
34dc7c2f
BB
3044
3045 return (err);
3046}
3047
3048static int
3049recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
330d06f9 3050 char *newname, recvflags_t *flags)
34dc7c2f 3051{
34dc7c2f
BB
3052 int err = 0;
3053 prop_changelist_t *clp;
3054 zfs_handle_t *zhp;
45d1cae3
BB
3055 boolean_t defer = B_FALSE;
3056 int spa_version;
34dc7c2f
BB
3057
3058 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
3059 if (zhp == NULL)
3060 return (-1);
63652e15
DS
3061 zfs_type_t type = zfs_get_type(zhp);
3062 if (type == ZFS_TYPE_SNAPSHOT &&
45d1cae3
BB
3063 zfs_spa_version(zhp, &spa_version) == 0 &&
3064 spa_version >= SPA_VERSION_USERREFS)
3065 defer = B_TRUE;
63652e15
DS
3066 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3067 flags->force ? MS_FORCE : 0);
34dc7c2f
BB
3068 zfs_close(zhp);
3069 if (clp == NULL)
3070 return (-1);
63652e15 3071
34dc7c2f
BB
3072 err = changelist_prefix(clp);
3073 if (err)
3074 return (err);
3075
330d06f9 3076 if (flags->verbose)
dc1c630b 3077 (void) printf("attempting destroy %s\n", name);
63652e15 3078 if (type == ZFS_TYPE_SNAPSHOT) {
dc1c630b
AG
3079 nvlist_t *nv = fnvlist_alloc();
3080 fnvlist_add_boolean(nv, name);
3081 err = lzc_destroy_snaps(nv, defer, NULL);
3082 fnvlist_free(nv);
3083 } else {
3084 err = lzc_destroy(name);
3085 }
34dc7c2f 3086 if (err == 0) {
330d06f9 3087 if (flags->verbose)
34dc7c2f 3088 (void) printf("success\n");
dc1c630b 3089 changelist_remove(clp, name);
34dc7c2f
BB
3090 }
3091
3092 (void) changelist_postfix(clp);
3093 changelist_free(clp);
3094
45d1cae3 3095 /*
428870ff
BB
3096 * Deferred destroy might destroy the snapshot or only mark it to be
3097 * destroyed later, and it returns success in either case.
45d1cae3 3098 */
428870ff
BB
3099 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
3100 ZFS_TYPE_SNAPSHOT))) {
34dc7c2f 3101 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
428870ff 3102 }
34dc7c2f
BB
3103
3104 return (err);
3105}
3106
3107typedef struct guid_to_name_data {
3108 uint64_t guid;
47dfff3b 3109 boolean_t bookmark_ok;
34dc7c2f 3110 char *name;
330d06f9 3111 char *skip;
30af21b0
PD
3112 uint64_t *redact_snap_guids;
3113 uint64_t num_redact_snaps;
34dc7c2f
BB
3114} guid_to_name_data_t;
3115
65c7cc49 3116static boolean_t
30af21b0
PD
3117redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd)
3118{
3119 uint64_t *bmark_snaps;
3120 uint_t bmark_num_snaps;
3121 nvlist_t *nvl;
3122 if (zhp->zfs_type != ZFS_TYPE_BOOKMARK)
3123 return (B_FALSE);
3124
3125 nvl = fnvlist_lookup_nvlist(zhp->zfs_props,
3126 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
3127 bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE,
3128 &bmark_num_snaps);
3129 if (bmark_num_snaps != gtnd->num_redact_snaps)
3130 return (B_FALSE);
3131 int i = 0;
3132 for (; i < bmark_num_snaps; i++) {
3133 int j = 0;
3134 for (; j < bmark_num_snaps; j++) {
3135 if (bmark_snaps[i] == gtnd->redact_snap_guids[j])
3136 break;
3137 }
3138 if (j == bmark_num_snaps)
3139 break;
3140 }
3141 return (i == bmark_num_snaps);
3142}
3143
34dc7c2f
BB
3144static int
3145guid_to_name_cb(zfs_handle_t *zhp, void *arg)
3146{
3147 guid_to_name_data_t *gtnd = arg;
47dfff3b 3148 const char *slash;
34dc7c2f
BB
3149 int err;
3150
330d06f9 3151 if (gtnd->skip != NULL &&
47dfff3b
MA
3152 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
3153 strcmp(slash + 1, gtnd->skip) == 0) {
3154 zfs_close(zhp);
330d06f9
MA
3155 return (0);
3156 }
3157
30af21b0
PD
3158 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid &&
3159 (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) {
34dc7c2f 3160 (void) strcpy(gtnd->name, zhp->zfs_name);
428870ff 3161 zfs_close(zhp);
34dc7c2f
BB
3162 return (EEXIST);
3163 }
330d06f9 3164
dc95911d 3165 err = zfs_iter_children(zhp, 0, guid_to_name_cb, gtnd);
47dfff3b 3166 if (err != EEXIST && gtnd->bookmark_ok)
dc95911d 3167 err = zfs_iter_bookmarks(zhp, 0, guid_to_name_cb, gtnd);
34dc7c2f
BB
3168 zfs_close(zhp);
3169 return (err);
3170}
3171
330d06f9
MA
3172/*
3173 * Attempt to find the local dataset associated with this guid. In the case of
3174 * multiple matches, we attempt to find the "best" match by searching
3175 * progressively larger portions of the hierarchy. This allows one to send a
3176 * tree of datasets individually and guarantee that we will find the source
3177 * guid within that hierarchy, even if there are multiple matches elsewhere.
30af21b0
PD
3178 *
3179 * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
3180 * the specified number of redaction snapshots. If num_redact_snaps isn't 0 or
3181 * -1, then redact_snap_guids will be an array of the guids of the snapshots the
3182 * redaction bookmark was created with. If num_redact_snaps is -1, then we will
3183 * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
3184 * given guid. Note that a redaction bookmark can be returned if
3185 * num_redact_snaps == -1.
330d06f9 3186 */
34dc7c2f 3187static int
30af21b0
PD
3188guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
3189 uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
3190 uint64_t num_redact_snaps, char *name)
34dc7c2f 3191{
eca7b760 3192 char pname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 3193 guid_to_name_data_t gtnd;
34dc7c2f
BB
3194
3195 gtnd.guid = guid;
47dfff3b 3196 gtnd.bookmark_ok = bookmark_ok;
34dc7c2f 3197 gtnd.name = name;
330d06f9 3198 gtnd.skip = NULL;
30af21b0
PD
3199 gtnd.redact_snap_guids = redact_snap_guids;
3200 gtnd.num_redact_snaps = num_redact_snaps;
34dc7c2f 3201
330d06f9 3202 /*
47dfff3b
MA
3203 * Search progressively larger portions of the hierarchy, starting
3204 * with the filesystem specified by 'parent'. This will
330d06f9
MA
3205 * select the "most local" version of the origin snapshot in the case
3206 * that there are multiple matching snapshots in the system.
3207 */
47dfff3b
MA
3208 (void) strlcpy(pname, parent, sizeof (pname));
3209 char *cp = strrchr(pname, '@');
3210 if (cp == NULL)
3211 cp = strchr(pname, '\0');
3212 for (; cp != NULL; cp = strrchr(pname, '/')) {
330d06f9 3213 /* Chop off the last component and open the parent */
34dc7c2f 3214 *cp = '\0';
47dfff3b 3215 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
330d06f9
MA
3216
3217 if (zhp == NULL)
3218 continue;
47dfff3b
MA
3219 int err = guid_to_name_cb(zfs_handle_dup(zhp), &gtnd);
3220 if (err != EEXIST)
dc95911d 3221 err = zfs_iter_children(zhp, 0, guid_to_name_cb, &gtnd);
47dfff3b 3222 if (err != EEXIST && bookmark_ok)
dc95911d
AJ
3223 err = zfs_iter_bookmarks(zhp, 0, guid_to_name_cb,
3224 &gtnd);
34dc7c2f 3225 zfs_close(zhp);
330d06f9
MA
3226 if (err == EEXIST)
3227 return (0);
34dc7c2f 3228
330d06f9 3229 /*
47dfff3b
MA
3230 * Remember the last portion of the dataset so we skip it next
3231 * time through (as we've already searched that portion of the
3232 * hierarchy).
330d06f9 3233 */
47dfff3b 3234 gtnd.skip = strrchr(pname, '/') + 1;
330d06f9 3235 }
34dc7c2f 3236
330d06f9 3237 return (ENOENT);
34dc7c2f
BB
3238}
3239
30af21b0
PD
3240static int
3241guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
3242 boolean_t bookmark_ok, char *name)
3243{
3244 return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL,
3245 -1, name));
3246}
3247
34dc7c2f 3248/*
330d06f9
MA
3249 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3250 * guid1 is after guid2.
34dc7c2f
BB
3251 */
3252static int
3253created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
3254 uint64_t guid1, uint64_t guid2)
3255{
3256 nvlist_t *nvfs;
d1807f16 3257 const char *fsname = NULL, *snapname = NULL;
eca7b760 3258 char buf[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 3259 int rv;
330d06f9
MA
3260 zfs_handle_t *guid1hdl, *guid2hdl;
3261 uint64_t create1, create2;
34dc7c2f
BB
3262
3263 if (guid2 == 0)
3264 return (0);
3265 if (guid1 == 0)
3266 return (1);
3267
3268 nvfs = fsavl_find(avl, guid1, &snapname);
60a2434b 3269 fsname = fnvlist_lookup_string(nvfs, "name");
34dc7c2f 3270 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
330d06f9
MA
3271 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3272 if (guid1hdl == NULL)
34dc7c2f
BB
3273 return (-1);
3274
3275 nvfs = fsavl_find(avl, guid2, &snapname);
60a2434b 3276 fsname = fnvlist_lookup_string(nvfs, "name");
34dc7c2f 3277 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
330d06f9
MA
3278 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3279 if (guid2hdl == NULL) {
3280 zfs_close(guid1hdl);
34dc7c2f
BB
3281 return (-1);
3282 }
3283
330d06f9
MA
3284 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
3285 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
34dc7c2f 3286
330d06f9
MA
3287 if (create1 < create2)
3288 rv = -1;
3289 else if (create1 > create2)
3290 rv = +1;
3291 else
3292 rv = 0;
3293
3294 zfs_close(guid1hdl);
3295 zfs_close(guid2hdl);
34dc7c2f
BB
3296
3297 return (rv);
3298}
3299
b5256303 3300/*
83472fab 3301 * This function reestablishes the hierarchy of encryption roots after a
b5256303
TC
3302 * recursive incremental receive has completed. This must be done after the
3303 * second call to recv_incremental_replication() has renamed and promoted all
83472fab 3304 * sent datasets to their final locations in the dataset hierarchy.
b5256303
TC
3305 */
3306static int
bb61cc31 3307recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
7633c0ae 3308 nvlist_t *stream_nv)
b5256303
TC
3309{
3310 int err;
3311 nvpair_t *fselem = NULL;
3312 nvlist_t *stream_fss;
3313
60a2434b 3314 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
b5256303
TC
3315
3316 while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3317 zfs_handle_t *zhp = NULL;
3318 uint64_t crypt;
3319 nvlist_t *snaps, *props, *stream_nvfs = NULL;
3320 nvpair_t *snapel = NULL;
3321 boolean_t is_encroot, is_clone, stream_encroot;
3322 char *cp;
d1807f16 3323 const char *stream_keylocation = NULL;
b5256303
TC
3324 char keylocation[MAXNAMELEN];
3325 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3326
3327 keylocation[0] = '\0';
60a2434b
RM
3328 stream_nvfs = fnvpair_value_nvlist(fselem);
3329 snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3330 props = fnvlist_lookup_nvlist(stream_nvfs, "props");
b5256303
TC
3331 stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3332
3333 /* find a snapshot from the stream that exists locally */
3334 err = ENOENT;
3335 while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3336 uint64_t guid;
3337
60a2434b 3338 guid = fnvpair_value_uint64(snapel);
bb61cc31 3339 err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
b5256303
TC
3340 fsname);
3341 if (err == 0)
3342 break;
3343 }
3344
3345 if (err != 0)
3346 continue;
3347
3348 cp = strchr(fsname, '@');
3349 if (cp != NULL)
3350 *cp = '\0';
3351
3352 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3353 if (zhp == NULL) {
3354 err = ENOENT;
3355 goto error;
3356 }
3357
3358 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3359 is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3360 (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3361
da689887 3362 /* we don't need to do anything for unencrypted datasets */
b5256303
TC
3363 if (crypt == ZIO_CRYPT_OFF) {
3364 zfs_close(zhp);
3365 continue;
3366 }
3367
3368 /*
3369 * If the dataset is flagged as an encryption root, was not
3370 * received as a clone and is not currently an encryption root,
3371 * force it to become one. Fixup the keylocation if necessary.
3372 */
3373 if (stream_encroot) {
3374 if (!is_clone && !is_encroot) {
3375 err = lzc_change_key(fsname,
3376 DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
3377 if (err != 0) {
3378 zfs_close(zhp);
3379 goto error;
3380 }
3381 }
3382
60a2434b
RM
3383 stream_keylocation = fnvlist_lookup_string(props,
3384 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
b5256303
TC
3385
3386 /*
3387 * Refresh the properties in case the call to
3388 * lzc_change_key() changed the value.
3389 */
3390 zfs_refresh_properties(zhp);
3391 err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
3392 keylocation, sizeof (keylocation), NULL, NULL,
3393 0, B_TRUE);
3394 if (err != 0) {
3395 zfs_close(zhp);
3396 goto error;
3397 }
3398
3399 if (strcmp(keylocation, stream_keylocation) != 0) {
3400 err = zfs_prop_set(zhp,
3401 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3402 stream_keylocation);
3403 if (err != 0) {
3404 zfs_close(zhp);
3405 goto error;
3406 }
3407 }
3408 }
3409
3410 /*
3411 * If the dataset is not flagged as an encryption root and is
3412 * currently an encryption root, force it to inherit from its
4807c0ba
TC
3413 * parent. The root of a raw send should never be
3414 * force-inherited.
b5256303 3415 */
4807c0ba
TC
3416 if (!stream_encroot && is_encroot &&
3417 strcmp(top_zfs, fsname) != 0) {
b5256303
TC
3418 err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
3419 NULL, NULL, 0);
3420 if (err != 0) {
3421 zfs_close(zhp);
3422 goto error;
3423 }
3424 }
3425
3426 zfs_close(zhp);
3427 }
3428
3429 return (0);
3430
3431error:
3432 return (err);
3433}
3434
34dc7c2f
BB
3435static int
3436recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
330d06f9 3437 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
428870ff 3438 nvlist_t *renamed)
34dc7c2f 3439{
7509a3d2 3440 nvlist_t *local_nv, *deleted = NULL;
34dc7c2f
BB
3441 avl_tree_t *local_avl;
3442 nvpair_t *fselem, *nextfselem;
d1807f16 3443 const char *fromsnap;
eca7b760 3444 char newname[ZFS_MAX_DATASET_NAME_LEN];
7509a3d2 3445 char guidname[32];
34dc7c2f 3446 int error;
428870ff 3447 boolean_t needagain, progress, recursive;
d1807f16 3448 const char *s1, *s2;
34dc7c2f 3449
60a2434b 3450 fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
428870ff
BB
3451
3452 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3453 ENOENT);
34dc7c2f 3454
330d06f9 3455 if (flags->dryrun)
34dc7c2f
BB
3456 return (0);
3457
3458again:
3459 needagain = progress = B_FALSE;
3460
60a2434b 3461 deleted = fnvlist_alloc();
7509a3d2 3462
34dc7c2f 3463 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
099fa7e4 3464 recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
f94b3cbf 3465 B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
34dc7c2f
BB
3466 return (error);
3467
3468 /*
3469 * Process deletes and renames
3470 */
3471 for (fselem = nvlist_next_nvpair(local_nv, NULL);
3472 fselem; fselem = nextfselem) {
3473 nvlist_t *nvfs, *snaps;
3474 nvlist_t *stream_nvfs = NULL;
3475 nvpair_t *snapelem, *nextsnapelem;
3476 uint64_t fromguid = 0;
3477 uint64_t originguid = 0;
3478 uint64_t stream_originguid = 0;
3479 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
d1807f16 3480 const char *fsname, *stream_fsname;
34dc7c2f
BB
3481
3482 nextfselem = nvlist_next_nvpair(local_nv, fselem);
3483
60a2434b
RM
3484 nvfs = fnvpair_value_nvlist(fselem);
3485 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3486 fsname = fnvlist_lookup_string(nvfs, "name");
3487 parent_fromsnap_guid = fnvlist_lookup_uint64(nvfs,
3488 "parentfromsnap");
34dc7c2f
BB
3489 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3490
3491 /*
3492 * First find the stream's fs, so we can check for
3493 * a different origin (due to "zfs promote")
3494 */
3495 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3496 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3497 uint64_t thisguid;
3498
60a2434b 3499 thisguid = fnvpair_value_uint64(snapelem);
34dc7c2f
BB
3500 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3501
3502 if (stream_nvfs != NULL)
3503 break;
3504 }
3505
3506 /* check for promote */
3507 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
3508 &stream_originguid);
3509 if (stream_nvfs && originguid != stream_originguid) {
3510 switch (created_before(hdl, local_avl,
3511 stream_originguid, originguid)) {
3512 case 1: {
3513 /* promote it! */
34dc7c2f 3514 nvlist_t *origin_nvfs;
d1807f16 3515 const char *origin_fsname;
34dc7c2f 3516
34dc7c2f
BB
3517 origin_nvfs = fsavl_find(local_avl, originguid,
3518 NULL);
60a2434b
RM
3519 origin_fsname = fnvlist_lookup_string(
3520 origin_nvfs, "name");
b5256303
TC
3521 error = recv_promote(hdl, fsname, origin_fsname,
3522 flags);
34dc7c2f
BB
3523 if (error == 0)
3524 progress = B_TRUE;
3525 break;
3526 }
3527 default:
3528 break;
3529 case -1:
3530 fsavl_destroy(local_avl);
60a2434b 3531 fnvlist_free(local_nv);
34dc7c2f
BB
3532 return (-1);
3533 }
3534 /*
3535 * We had/have the wrong origin, therefore our
3536 * list of snapshots is wrong. Need to handle
3537 * them on the next pass.
3538 */
3539 needagain = B_TRUE;
3540 continue;
3541 }
3542
3543 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3544 snapelem; snapelem = nextsnapelem) {
3545 uint64_t thisguid;
d1807f16 3546 const char *stream_snapname;
b128c09f 3547 nvlist_t *found, *props;
34dc7c2f
BB
3548
3549 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3550
60a2434b 3551 thisguid = fnvpair_value_uint64(snapelem);
34dc7c2f
BB
3552 found = fsavl_find(stream_avl, thisguid,
3553 &stream_snapname);
3554
3555 /* check for delete */
3556 if (found == NULL) {
eca7b760 3557 char name[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 3558
330d06f9 3559 if (!flags->force)
34dc7c2f
BB
3560 continue;
3561
3562 (void) snprintf(name, sizeof (name), "%s@%s",
3563 fsname, nvpair_name(snapelem));
3564
3565 error = recv_destroy(hdl, name,
3566 strlen(fsname)+1, newname, flags);
3567 if (error)
3568 needagain = B_TRUE;
3569 else
3570 progress = B_TRUE;
3df29340
BB
3571 sprintf(guidname, "%llu",
3572 (u_longlong_t)thisguid);
7509a3d2 3573 nvlist_add_boolean(deleted, guidname);
34dc7c2f
BB
3574 continue;
3575 }
3576
3577 stream_nvfs = found;
3578
b128c09f
BB
3579 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3580 &props) && 0 == nvlist_lookup_nvlist(props,
3581 stream_snapname, &props)) {
13fe0198 3582 zfs_cmd_t zc = {"\0"};
b128c09f 3583
428870ff 3584 zc.zc_cookie = B_TRUE; /* received */
b128c09f
BB
3585 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3586 "%s@%s", fsname, nvpair_name(snapelem));
18dbf5c8
AZ
3587 zcmd_write_src_nvlist(hdl, &zc, props);
3588 (void) zfs_ioctl(hdl,
3589 ZFS_IOC_SET_PROP, &zc);
3590 zcmd_free_nvlists(&zc);
b128c09f
BB
3591 }
3592
34dc7c2f
BB
3593 /* check for different snapname */
3594 if (strcmp(nvpair_name(snapelem),
3595 stream_snapname) != 0) {
eca7b760
IK
3596 char name[ZFS_MAX_DATASET_NAME_LEN];
3597 char tryname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
3598
3599 (void) snprintf(name, sizeof (name), "%s@%s",
3600 fsname, nvpair_name(snapelem));
3601 (void) snprintf(tryname, sizeof (name), "%s@%s",
3602 fsname, stream_snapname);
3603
3604 error = recv_rename(hdl, name, tryname,
3605 strlen(fsname)+1, newname, flags);
3606 if (error)
3607 needagain = B_TRUE;
3608 else
3609 progress = B_TRUE;
3610 }
3611
3612 if (strcmp(stream_snapname, fromsnap) == 0)
3613 fromguid = thisguid;
3614 }
3615
3616 /* check for delete */
3617 if (stream_nvfs == NULL) {
330d06f9 3618 if (!flags->force)
34dc7c2f
BB
3619 continue;
3620
3621 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3622 newname, flags);
3623 if (error)
3624 needagain = B_TRUE;
3625 else
3626 progress = B_TRUE;
3df29340 3627 sprintf(guidname, "%llu",
02730c33 3628 (u_longlong_t)parent_fromsnap_guid);
7509a3d2 3629 nvlist_add_boolean(deleted, guidname);
34dc7c2f
BB
3630 continue;
3631 }
3632
428870ff 3633 if (fromguid == 0) {
330d06f9 3634 if (flags->verbose) {
428870ff
BB
3635 (void) printf("local fs %s does not have "
3636 "fromsnap (%s in stream); must have "
3637 "been deleted locally; ignoring\n",
3638 fsname, fromsnap);
3639 }
34dc7c2f
BB
3640 continue;
3641 }
3642
60a2434b
RM
3643 stream_fsname = fnvlist_lookup_string(stream_nvfs, "name");
3644 stream_parent_fromsnap_guid = fnvlist_lookup_uint64(
3645 stream_nvfs, "parentfromsnap");
34dc7c2f 3646
428870ff
BB
3647 s1 = strrchr(fsname, '/');
3648 s2 = strrchr(stream_fsname, '/');
3649
7509a3d2 3650 /*
3651 * Check if we're going to rename based on parent guid change
3652 * and the current parent guid was also deleted. If it was then
3653 * rename will fail and is likely unneeded, so avoid this and
3654 * force an early retry to determine the new
3655 * parent_fromsnap_guid.
3656 */
3657 if (stream_parent_fromsnap_guid != 0 &&
3658 parent_fromsnap_guid != 0 &&
3659 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3df29340 3660 sprintf(guidname, "%llu",
02730c33 3661 (u_longlong_t)parent_fromsnap_guid);
7509a3d2 3662 if (nvlist_exists(deleted, guidname)) {
3663 progress = B_TRUE;
3664 needagain = B_TRUE;
3665 goto doagain;
3666 }
3667 }
3668
428870ff
BB
3669 /*
3670 * Check for rename. If the exact receive path is specified, it
3671 * does not count as a rename, but we still need to check the
3672 * datasets beneath it.
3673 */
34dc7c2f 3674 if ((stream_parent_fromsnap_guid != 0 &&
428870ff 3675 parent_fromsnap_guid != 0 &&
34dc7c2f 3676 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
330d06f9 3677 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
428870ff 3678 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
34dc7c2f 3679 nvlist_t *parent;
eca7b760 3680 char tryname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
3681
3682 parent = fsavl_find(local_avl,
3683 stream_parent_fromsnap_guid, NULL);
3684 /*
3685 * NB: parent might not be found if we used the
3686 * tosnap for stream_parent_fromsnap_guid,
3687 * because the parent is a newly-created fs;
3688 * we'll be able to rename it after we recv the
3689 * new fs.
3690 */
3691 if (parent != NULL) {
d1807f16 3692 const char *pname;
34dc7c2f 3693
60a2434b 3694 pname = fnvlist_lookup_string(parent, "name");
34dc7c2f
BB
3695 (void) snprintf(tryname, sizeof (tryname),
3696 "%s%s", pname, strrchr(stream_fsname, '/'));
3697 } else {
3698 tryname[0] = '\0';
330d06f9 3699 if (flags->verbose) {
34dc7c2f
BB
3700 (void) printf("local fs %s new parent "
3701 "not found\n", fsname);
3702 }
3703 }
3704
428870ff
BB
3705 newname[0] = '\0';
3706
34dc7c2f
BB
3707 error = recv_rename(hdl, fsname, tryname,
3708 strlen(tofs)+1, newname, flags);
428870ff
BB
3709
3710 if (renamed != NULL && newname[0] != '\0') {
60a2434b 3711 fnvlist_add_boolean(renamed, newname);
428870ff
BB
3712 }
3713
34dc7c2f
BB
3714 if (error)
3715 needagain = B_TRUE;
3716 else
3717 progress = B_TRUE;
3718 }
3719 }
3720
7509a3d2 3721doagain:
34dc7c2f 3722 fsavl_destroy(local_avl);
60a2434b
RM
3723 fnvlist_free(local_nv);
3724 fnvlist_free(deleted);
34dc7c2f
BB
3725
3726 if (needagain && progress) {
3727 /* do another pass to fix up temporary names */
330d06f9 3728 if (flags->verbose)
34dc7c2f
BB
3729 (void) printf("another pass:\n");
3730 goto again;
3731 }
3732
b5256303 3733 return (needagain || error != 0);
34dc7c2f
BB
3734}
3735
3736static int
3737zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
330d06f9 3738 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
196bee4c 3739 char **top_zfs, nvlist_t *cmdprops)
34dc7c2f
BB
3740{
3741 nvlist_t *stream_nv = NULL;
3742 avl_tree_t *stream_avl = NULL;
d1807f16
RY
3743 const char *fromsnap = NULL;
3744 const char *sendsnap = NULL;
428870ff 3745 char *cp;
eca7b760
IK
3746 char tofs[ZFS_MAX_DATASET_NAME_LEN];
3747 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
d68a2bfa 3748 char errbuf[ERRBUFLEN];
34dc7c2f
BB
3749 dmu_replay_record_t drre;
3750 int error;
3751 boolean_t anyerr = B_FALSE;
3752 boolean_t softerr = B_FALSE;
b5256303 3753 boolean_t recursive, raw;
34dc7c2f
BB
3754
3755 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3756 "cannot receive"));
3757
34dc7c2f
BB
3758 assert(drr->drr_type == DRR_BEGIN);
3759 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
428870ff
BB
3760 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3761 DMU_COMPOUNDSTREAM);
34dc7c2f
BB
3762
3763 /*
3764 * Read in the nvlist from the stream.
3765 */
3766 if (drr->drr_payloadlen != 0) {
34dc7c2f 3767 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
330d06f9 3768 &stream_nv, flags->byteswap, zc);
34dc7c2f
BB
3769 if (error) {
3770 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3771 goto out;
3772 }
3773 }
3774
428870ff
BB
3775 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3776 ENOENT);
b5256303 3777 raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
428870ff
BB
3778
3779 if (recursive && strchr(destname, '@')) {
3780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3781 "cannot specify snapshot name for multi-snapshot stream"));
3782 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3783 goto out;
3784 }
3785
34dc7c2f
BB
3786 /*
3787 * Read in the end record and verify checksum.
3788 */
3789 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
330d06f9 3790 flags->byteswap, NULL)))
34dc7c2f 3791 goto out;
330d06f9 3792 if (flags->byteswap) {
34dc7c2f
BB
3793 drre.drr_type = BSWAP_32(drre.drr_type);
3794 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3795 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3796 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3797 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3798 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3799 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3800 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3801 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3802 }
3803 if (drre.drr_type != DRR_END) {
3804 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3805 goto out;
3806 }
3807 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3809 "incorrect header checksum"));
3810 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3811 goto out;
3812 }
3813
3814 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3815
3816 if (drr->drr_payloadlen != 0) {
3817 nvlist_t *stream_fss;
3818
60a2434b 3819 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
34dc7c2f
BB
3820 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3822 "couldn't allocate avl tree"));
3823 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3824 goto out;
3825 }
3826
4c3c6b6c 3827 if (fromsnap != NULL && recursive) {
428870ff
BB
3828 nvlist_t *renamed = NULL;
3829 nvpair_t *pair = NULL;
3830
eca7b760 3831 (void) strlcpy(tofs, destname, sizeof (tofs));
330d06f9 3832 if (flags->isprefix) {
428870ff
BB
3833 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3834 int i;
3835
330d06f9 3836 if (flags->istail) {
428870ff
BB
3837 cp = strrchr(drrb->drr_toname, '/');
3838 if (cp == NULL) {
3839 (void) strlcat(tofs, "/",
eca7b760 3840 sizeof (tofs));
428870ff
BB
3841 i = 0;
3842 } else {
3843 i = (cp - drrb->drr_toname);
3844 }
3845 } else {
3846 i = strcspn(drrb->drr_toname, "/@");
3847 }
34dc7c2f 3848 /* zfs_receive_one() will create_parents() */
428870ff 3849 (void) strlcat(tofs, &drrb->drr_toname[i],
eca7b760 3850 sizeof (tofs));
34dc7c2f
BB
3851 *strchr(tofs, '@') = '\0';
3852 }
428870ff 3853
4c3c6b6c 3854 if (!flags->dryrun && !flags->nomount) {
60a2434b 3855 renamed = fnvlist_alloc();
428870ff
BB
3856 }
3857
3858 softerr = recv_incremental_replication(hdl, tofs, flags,
3859 stream_nv, stream_avl, renamed);
3860
3861 /* Unmount renamed filesystems before receiving. */
3862 while ((pair = nvlist_next_nvpair(renamed,
3863 pair)) != NULL) {
3864 zfs_handle_t *zhp;
3865 prop_changelist_t *clp = NULL;
3866
3867 zhp = zfs_open(hdl, nvpair_name(pair),
3868 ZFS_TYPE_FILESYSTEM);
3869 if (zhp != NULL) {
3870 clp = changelist_gather(zhp,
a57d3d45
MZ
3871 ZFS_PROP_MOUNTPOINT, 0,
3872 flags->forceunmount ? MS_FORCE : 0);
428870ff
BB
3873 zfs_close(zhp);
3874 if (clp != NULL) {
3875 softerr |=
3876 changelist_prefix(clp);
3877 changelist_free(clp);
3878 }
3879 }
3880 }
3881
60a2434b 3882 fnvlist_free(renamed);
34dc7c2f
BB
3883 }
3884 }
3885
428870ff
BB
3886 /*
3887 * Get the fs specified by the first path in the stream (the top level
3888 * specified by 'zfs send') and pass it to each invocation of
3889 * zfs_receive_one().
3890 */
3891 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
eca7b760 3892 sizeof (sendfs));
671c9354 3893 if ((cp = strchr(sendfs, '@')) != NULL) {
428870ff 3894 *cp = '\0';
671c9354
DM
3895 /*
3896 * Find the "sendsnap", the final snapshot in a replication
3897 * stream. zfs_receive_one() handles certain errors
3898 * differently, depending on if the contained stream is the
3899 * last one or not.
3900 */
3901 sendsnap = (cp + 1);
3902 }
34dc7c2f
BB
3903
3904 /* Finally, receive each contained stream */
3905 do {
3906 /*
3907 * we should figure out if it has a recoverable
3908 * error, in which case do a recv_skip() and drive on.
3909 * Note, if we fail due to already having this guid,
3910 * zfs_receive_one() will take care of it (ie,
3911 * recv_skip() and return 0).
3912 */
fcff0f35 3913 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
196bee4c 3914 sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops);
34dc7c2f
BB
3915 if (error == ENODATA) {
3916 error = 0;
3917 break;
3918 }
3919 anyerr |= error;
3920 } while (error == 0);
3921
4c3c6b6c 3922 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
34dc7c2f
BB
3923 /*
3924 * Now that we have the fs's they sent us, try the
3925 * renames again.
3926 */
3927 softerr = recv_incremental_replication(hdl, tofs, flags,
428870ff 3928 stream_nv, stream_avl, NULL);
34dc7c2f
BB
3929 }
3930
bb61cc31
TC
3931 if (raw && softerr == 0 && *top_zfs != NULL) {
3932 softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
7633c0ae 3933 stream_nv);
b5256303
TC
3934 }
3935
34dc7c2f
BB
3936out:
3937 fsavl_destroy(stream_avl);
60a2434b 3938 fnvlist_free(stream_nv);
34dc7c2f
BB
3939 if (softerr)
3940 error = -2;
3941 if (anyerr)
3942 error = -1;
3943 return (error);
3944}
3945
428870ff
BB
3946static void
3947trunc_prop_errs(int truncated)
3948{
3949 ASSERT(truncated != 0);
3950
3951 if (truncated == 1)
3952 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3953 "1 more property could not be set\n"));
3954 else
3955 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3956 "%d more properties could not be set\n"), truncated);
3957}
3958
34dc7c2f
BB
3959static int
3960recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3961{
3962 dmu_replay_record_t *drr;
f1512ee6 3963 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
870e7a52 3964 uint64_t payload_size;
d68a2bfa 3965 char errbuf[ERRBUFLEN];
428870ff
BB
3966
3967 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
870e7a52 3968 "cannot receive"));
34dc7c2f
BB
3969
3970 /* XXX would be great to use lseek if possible... */
3971 drr = buf;
3972
3973 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3974 byteswap, NULL) == 0) {
3975 if (byteswap)
3976 drr->drr_type = BSWAP_32(drr->drr_type);
3977
3978 switch (drr->drr_type) {
3979 case DRR_BEGIN:
428870ff 3980 if (drr->drr_payloadlen != 0) {
47dfff3b
MA
3981 (void) recv_read(hdl, fd, buf,
3982 drr->drr_payloadlen, B_FALSE, NULL);
428870ff 3983 }
34dc7c2f
BB
3984 break;
3985
3986 case DRR_END:
3987 free(buf);
3988 return (0);
3989
3990 case DRR_OBJECT:
3991 if (byteswap) {
3992 drr->drr_u.drr_object.drr_bonuslen =
3993 BSWAP_32(drr->drr_u.drr_object.
3994 drr_bonuslen);
870e7a52
TC
3995 drr->drr_u.drr_object.drr_raw_bonuslen =
3996 BSWAP_32(drr->drr_u.drr_object.
3997 drr_raw_bonuslen);
34dc7c2f 3998 }
870e7a52
TC
3999
4000 payload_size =
4001 DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object);
4002 (void) recv_read(hdl, fd, buf, payload_size,
34dc7c2f
BB
4003 B_FALSE, NULL);
4004 break;
4005
4006 case DRR_WRITE:
4007 if (byteswap) {
2aa34383
DK
4008 drr->drr_u.drr_write.drr_logical_size =
4009 BSWAP_64(
4010 drr->drr_u.drr_write.drr_logical_size);
4011 drr->drr_u.drr_write.drr_compressed_size =
4012 BSWAP_64(
4013 drr->drr_u.drr_write.drr_compressed_size);
34dc7c2f 4014 }
870e7a52 4015 payload_size =
2aa34383 4016 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
7a6c12fd 4017 assert(payload_size <= SPA_MAXBLOCKSIZE);
34dc7c2f 4018 (void) recv_read(hdl, fd, buf,
2aa34383 4019 payload_size, B_FALSE, NULL);
34dc7c2f 4020 break;
428870ff
BB
4021 case DRR_SPILL:
4022 if (byteswap) {
9f8026c8 4023 drr->drr_u.drr_spill.drr_length =
428870ff 4024 BSWAP_64(drr->drr_u.drr_spill.drr_length);
870e7a52
TC
4025 drr->drr_u.drr_spill.drr_compressed_size =
4026 BSWAP_64(drr->drr_u.drr_spill.
4027 drr_compressed_size);
428870ff 4028 }
870e7a52
TC
4029
4030 payload_size =
4031 DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill);
4032 (void) recv_read(hdl, fd, buf, payload_size,
4033 B_FALSE, NULL);
428870ff 4034 break;
9b67f605
MA
4035 case DRR_WRITE_EMBEDDED:
4036 if (byteswap) {
4037 drr->drr_u.drr_write_embedded.drr_psize =
4038 BSWAP_32(drr->drr_u.drr_write_embedded.
4039 drr_psize);
4040 }
4041 (void) recv_read(hdl, fd, buf,
4042 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
4043 8), B_FALSE, NULL);
4044 break;
30af21b0 4045 case DRR_OBJECT_RANGE:
428870ff 4046 case DRR_WRITE_BYREF:
34dc7c2f
BB
4047 case DRR_FREEOBJECTS:
4048 case DRR_FREE:
4049 break;
4050
4051 default:
428870ff
BB
4052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4053 "invalid record type"));
fad5fb01 4054 free(buf);
428870ff 4055 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
34dc7c2f
BB
4056 }
4057 }
4058
4059 free(buf);
4060 return (-1);
4061}
4062
47dfff3b
MA
4063static void
4064recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
7145123b 4065 boolean_t resumable, boolean_t checksum)
47dfff3b 4066{
eca7b760 4067 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
47dfff3b 4068
7145123b
PD
4069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ?
4070 "checksum mismatch" : "incomplete stream")));
47dfff3b
MA
4071
4072 if (!resumable)
4073 return;
4074 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
4075 *strchr(target_fs, '@') = '\0';
4076 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
4077 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4078 if (zhp == NULL)
4079 return;
4080
4081 char token_buf[ZFS_MAXPROPLEN];
4082 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4083 token_buf, sizeof (token_buf),
4084 NULL, NULL, 0, B_TRUE);
4085 if (error == 0) {
4086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4087 "checksum mismatch or incomplete stream.\n"
4088 "Partially received snapshot is saved.\n"
4089 "A resuming stream can be generated on the sending "
4090 "system by running:\n"
4091 " zfs send -t %s"),
4092 token_buf);
4093 }
4094 zfs_close(zhp);
4095}
4096
a3eeab2d 4097/*
4098 * Prepare a new nvlist of properties that are to override (-o) or be excluded
4099 * (-x) from the received dataset
4100 * recvprops: received properties from the send stream
4101 * cmdprops: raw input properties from command line
4102 * origprops: properties, both locally-set and received, currently set on the
4103 * target dataset if it exists, NULL otherwise.
4104 * oxprops: valid output override (-o) and excluded (-x) properties
4105 */
4106static int
d9c460a0
TC
4107zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
4108 char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
4109 boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
4110 nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
4111 uint_t *wkeylen_out, const char *errbuf)
a3eeab2d 4112{
4113 nvpair_t *nvp;
4114 nvlist_t *oprops, *voprops;
4115 zfs_handle_t *zhp = NULL;
4116 zpool_handle_t *zpool_hdl = NULL;
d9c460a0 4117 char *cp;
a3eeab2d 4118 int ret = 0;
d9c460a0 4119 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
a3eeab2d 4120
4121 if (nvlist_empty(cmdprops))
4122 return (0); /* No properties to override or exclude */
4123
4124 *oxprops = fnvlist_alloc();
4125 oprops = fnvlist_alloc();
4126
d9c460a0
TC
4127 strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
4128
4129 /*
4130 * Get our dataset handle. The target dataset may not exist yet.
4131 */
4132 if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
4133 zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
4134 if (zhp == NULL) {
4135 ret = -1;
4136 goto error;
4137 }
4138 }
4139
4140 /* open the zpool handle */
4141 cp = strchr(namebuf, '/');
4142 if (cp != NULL)
4143 *cp = '\0';
4144 zpool_hdl = zpool_open(hdl, namebuf);
4145 if (zpool_hdl == NULL) {
4146 ret = -1;
4147 goto error;
4148 }
4149
4150 /* restore namebuf to match fsname for later use */
4151 if (cp != NULL)
4152 *cp = '/';
4153
a3eeab2d 4154 /*
4155 * first iteration: process excluded (-x) properties now and gather
4156 * added (-o) properties to be later processed by zfs_valid_proplist()
4157 */
4158 nvp = NULL;
4159 while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
4160 const char *name = nvpair_name(nvp);
4161 zfs_prop_t prop = zfs_name_to_prop(name);
4162
4476ccd9
RE
4163 /*
4164 * It turns out, if we don't normalize "aliased" names
4165 * e.g. compress= against the "real" names (e.g. compression)
4166 * here, then setting/excluding them does not work as
4167 * intended.
4168 *
4169 * But since user-defined properties wouldn't have a valid
4170 * mapping here, we do this conditional dance.
4171 */
4172 const char *newname = name;
4173 if (prop >= ZFS_PROP_TYPE)
4174 newname = zfs_prop_to_name(prop);
4175
a3eeab2d 4176 /* "origin" is processed separately, don't handle it here */
4177 if (prop == ZFS_PROP_ORIGIN)
4178 continue;
4179
d9c460a0
TC
4180 /* raw streams can't override encryption properties */
4181 if ((zfs_prop_encryption_key_param(prop) ||
b4238327 4182 prop == ZFS_PROP_ENCRYPTION) && raw) {
d9c460a0
TC
4183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4184 "encryption property '%s' cannot "
b4238327
TC
4185 "be set or excluded for raw streams."), name);
4186 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4187 goto error;
4188 }
4189
9be34ec9
AH
4190 /*
4191 * For plain replicated send, we can ignore encryption
4192 * properties other than first stream
4193 */
4194 if ((zfs_prop_encryption_key_param(prop) || prop ==
4195 ZFS_PROP_ENCRYPTION) && !newfs && recursive && !raw) {
4196 continue;
4197 }
4198
b4238327
TC
4199 /* incremental streams can only exclude encryption properties */
4200 if ((zfs_prop_encryption_key_param(prop) ||
4201 prop == ZFS_PROP_ENCRYPTION) && !newfs &&
4202 nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
4203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4204 "encryption property '%s' cannot "
4205 "be set for incremental streams."), name);
d9c460a0
TC
4206 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4207 goto error;
4208 }
4209
a3eeab2d 4210 switch (nvpair_type(nvp)) {
4211 case DATA_TYPE_BOOLEAN: /* -x property */
4212 /*
4213 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
4214 * a property: this is done by forcing an explicit
4215 * inherit on the destination so the effective value is
4216 * not the one we received from the send stream.
b0269cd8
I
4217 */
4218 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4219 !zfs_prop_user(name)) {
4220 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4221 "Warning: %s: property '%s' does not "
4222 "apply to datasets of this type\n"),
4223 fsname, name);
4224 continue;
4225 }
4226 /*
a3eeab2d 4227 * We do this only if the property is not already
4228 * locally-set, in which case its value will take
4229 * priority over the received anyway.
4230 */
4476ccd9 4231 if (nvlist_exists(origprops, newname)) {
a3eeab2d 4232 nvlist_t *attrs;
d1807f16 4233 const char *source = NULL;
a3eeab2d 4234
4476ccd9
RE
4235 attrs = fnvlist_lookup_nvlist(origprops,
4236 newname);
b4238327
TC
4237 if (nvlist_lookup_string(attrs,
4238 ZPROP_SOURCE, &source) == 0 &&
4239 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
a3eeab2d 4240 continue;
4241 }
4242 /*
4243 * We can't force an explicit inherit on non-inheritable
4244 * properties: if we're asked to exclude this kind of
4245 * values we remove them from "recvprops" input nvlist.
4246 */
63652e15
DS
4247 if (!zfs_prop_user(name) && /* can be inherited too */
4248 !zfs_prop_inheritable(prop) &&
4476ccd9
RE
4249 nvlist_exists(recvprops, newname))
4250 fnvlist_remove(recvprops, newname);
a3eeab2d 4251 else
4476ccd9 4252 fnvlist_add_boolean(*oxprops, newname);
a3eeab2d 4253 break;
4254 case DATA_TYPE_STRING: /* -o property=value */
b0269cd8
I
4255 /*
4256 * we're trying to override a property that does not
4257 * make sense for this type of dataset, but we don't
4258 * want to fail if the receive is recursive: this comes
4259 * in handy when the send stream contains, for
4260 * instance, a child ZVOL and we're trying to receive
4261 * it with "-o atime=on"
4262 */
4263 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4264 !zfs_prop_user(name)) {
4265 if (recursive)
4266 continue;
4267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4268 "property '%s' does not apply to datasets "
4269 "of this type"), name);
4270 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4271 goto error;
4272 }
4476ccd9
RE
4273 fnvlist_add_string(oprops, newname,
4274 fnvpair_value_string(nvp));
a3eeab2d 4275 break;
4276 default:
4277 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4278 "property '%s' must be a string or boolean"), name);
4279 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4280 goto error;
4281 }
4282 }
4283
4284 if (toplevel) {
4285 /* convert override strings properties to native */
4286 if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
b5256303 4287 oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
a3eeab2d 4288 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4289 goto error;
4290 }
4291
d9c460a0
TC
4292 /*
4293 * zfs_crypto_create() requires the parent name. Get it
4294 * by truncating the fsname copy stored in namebuf.
4295 */
4296 cp = strrchr(namebuf, '/');
4297 if (cp != NULL)
4298 *cp = '\0';
4299
9be34ec9
AH
4300 if (!raw && !(!newfs && recursive) &&
4301 zfs_crypto_create(hdl, namebuf, voprops, NULL,
d9c460a0
TC
4302 B_FALSE, wkeydata_out, wkeylen_out) != 0) {
4303 fnvlist_free(voprops);
4304 ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4305 goto error;
4306 }
4307
a3eeab2d 4308 /* second pass: process "-o" properties */
4309 fnvlist_merge(*oxprops, voprops);
4310 fnvlist_free(voprops);
4311 } else {
4312 /* override props on child dataset are inherited */
4313 nvp = NULL;
4314 while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
4315 const char *name = nvpair_name(nvp);
4316 fnvlist_add_boolean(*oxprops, name);
4317 }
4318 }
4319
4320error:
d9c460a0
TC
4321 if (zhp != NULL)
4322 zfs_close(zhp);
4323 if (zpool_hdl != NULL)
4324 zpool_close(zpool_hdl);
a3eeab2d 4325 fnvlist_free(oprops);
4326 return (ret);
4327}
4328
34dc7c2f
BB
4329/*
4330 * Restores a backup of tosnap from the file descriptor specified by infd.
4331 */
4332static int
4333zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
fcff0f35
PD
4334 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
4335 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
196bee4c
MA
4336 avl_tree_t *stream_avl, char **top_zfs,
4337 const char *finalsnap, nvlist_t *cmdprops)
34dc7c2f 4338{
67de71e6 4339 struct timespec begin_time;
428870ff 4340 int ioctl_err, ioctl_errno, err;
34dc7c2f
BB
4341 char *cp;
4342 struct drr_begin *drrb = &drr->drr_u.drr_begin;
d68a2bfa 4343 char errbuf[ERRBUFLEN];
428870ff 4344 const char *chopprefix;
34dc7c2f 4345 boolean_t newfs = B_FALSE;
a132c2b4 4346 boolean_t stream_wantsnewfs, stream_resumingnewfs;
43e52edd
BB
4347 boolean_t newprops = B_FALSE;
4348 uint64_t read_bytes = 0;
4349 uint64_t errflags = 0;
34dc7c2f
BB
4350 uint64_t parent_snapguid = 0;
4351 prop_changelist_t *clp = NULL;
b128c09f 4352 nvlist_t *snapprops_nvlist = NULL;
9c5e88b1 4353 nvlist_t *snapholds_nvlist = NULL;
428870ff 4354 zprop_errflags_t prop_errflags;
43e52edd 4355 nvlist_t *prop_errors = NULL;
428870ff 4356 boolean_t recursive;
d1807f16 4357 const char *snapname = NULL;
43e52edd 4358 char destsnap[MAXPATHLEN * 2];
861166b0 4359 char origin[MAXNAMELEN] = {0};
43e52edd 4360 char name[MAXPATHLEN];
861166b0 4361 char tmp_keylocation[MAXNAMELEN] = {0};
a3eeab2d 4362 nvlist_t *rcvprops = NULL; /* props received from the send stream */
4363 nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
4364 nvlist_t *origprops = NULL; /* original props (if destination exists) */
806739f9 4365 zfs_type_t type = ZFS_TYPE_INVALID;
bee7e4ff 4366 boolean_t toplevel = B_FALSE;
a3eeab2d 4367 boolean_t zoned = B_FALSE;
c03f0470 4368 boolean_t hastoken = B_FALSE;
30af21b0 4369 boolean_t redacted;
d9c460a0
TC
4370 uint8_t *wkeydata = NULL;
4371 uint_t wkeylen = 0;
34dc7c2f 4372
67de71e6
AZ
4373#ifndef CLOCK_MONOTONIC_RAW
4374#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
4375#endif
4376 clock_gettime(CLOCK_MONOTONIC_RAW, &begin_time);
34dc7c2f
BB
4377
4378 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4379 "cannot receive"));
4380
428870ff
BB
4381 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
4382 ENOENT);
4383
9c5e88b1
PZ
4384 /* Did the user request holds be skipped via zfs recv -k? */
4385 boolean_t holds = flags->holds && !flags->skipholds;
4386
34dc7c2f 4387 if (stream_avl != NULL) {
d1807f16 4388 const char *keylocation = NULL;
48f783de 4389 nvlist_t *lookup = NULL;
b128c09f
BB
4390 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
4391 &snapname);
34dc7c2f
BB
4392
4393 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
4394 &parent_snapguid);
a3eeab2d 4395 err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
43e52edd 4396 if (err) {
60a2434b 4397 rcvprops = fnvlist_alloc();
43e52edd
BB
4398 newprops = B_TRUE;
4399 }
34dc7c2f 4400
b5256303
TC
4401 /*
4402 * The keylocation property may only be set on encryption roots,
4403 * but this dataset might not become an encryption root until
83472fab 4404 * recv_fix_encryption_hierarchy() is called. That function
b5256303
TC
4405 * will fixup the keylocation anyway, so we temporarily unset
4406 * the keylocation for now to avoid any errors from the receive
4407 * ioctl.
4408 */
4409 err = nvlist_lookup_string(rcvprops,
4410 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
4411 if (err == 0) {
a51288aa 4412 strlcpy(tmp_keylocation, keylocation, MAXNAMELEN);
b5256303
TC
4413 (void) nvlist_remove_all(rcvprops,
4414 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
4415 }
4416
330d06f9 4417 if (flags->canmountoff) {
60a2434b
RM
4418 fnvlist_add_uint64(rcvprops,
4419 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0);
9c5e88b1 4420 } else if (newprops) { /* nothing in rcvprops, eliminate it */
60a2434b 4421 fnvlist_free(rcvprops);
9c5e88b1
PZ
4422 rcvprops = NULL;
4423 newprops = B_FALSE;
34dc7c2f 4424 }
48f783de 4425 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
60a2434b
RM
4426 snapprops_nvlist = fnvlist_lookup_nvlist(lookup,
4427 snapname);
48f783de 4428 }
9c5e88b1
PZ
4429 if (holds) {
4430 if (0 == nvlist_lookup_nvlist(fs, "snapholds",
4431 &lookup)) {
60a2434b
RM
4432 snapholds_nvlist = fnvlist_lookup_nvlist(
4433 lookup, snapname);
9c5e88b1
PZ
4434 }
4435 }
34dc7c2f
BB
4436 }
4437
428870ff
BB
4438 cp = NULL;
4439
34dc7c2f
BB
4440 /*
4441 * Determine how much of the snapshot name stored in the stream
4442 * we are going to tack on to the name they specified on the
4443 * command line, and how much we are going to chop off.
4444 *
4445 * If they specified a snapshot, chop the entire name stored in
4446 * the stream.
4447 */
330d06f9 4448 if (flags->istail) {
428870ff
BB
4449 /*
4450 * A filesystem was specified with -e. We want to tack on only
4451 * the tail of the sent snapshot path.
4452 */
4453 if (strchr(tosnap, '@')) {
4454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4455 "argument - snapshot not allowed with -e"));
43e52edd
BB
4456 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4457 goto out;
428870ff
BB
4458 }
4459
4460 chopprefix = strrchr(sendfs, '/');
4461
4462 if (chopprefix == NULL) {
4463 /*
4464 * The tail is the poolname, so we need to
4465 * prepend a path separator.
4466 */
4467 int len = strlen(drrb->drr_toname);
72c99dc9 4468 cp = umem_alloc(len + 2, UMEM_NOFAIL);
428870ff
BB
4469 cp[0] = '/';
4470 (void) strcpy(&cp[1], drrb->drr_toname);
4471 chopprefix = cp;
4472 } else {
4473 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
4474 }
330d06f9 4475 } else if (flags->isprefix) {
34dc7c2f 4476 /*
428870ff
BB
4477 * A filesystem was specified with -d. We want to tack on
4478 * everything but the first element of the sent snapshot path
4479 * (all but the pool name).
34dc7c2f
BB
4480 */
4481 if (strchr(tosnap, '@')) {
4482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4483 "argument - snapshot not allowed with -d"));
43e52edd
BB
4484 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4485 goto out;
34dc7c2f 4486 }
428870ff
BB
4487
4488 chopprefix = strchr(drrb->drr_toname, '/');
4489 if (chopprefix == NULL)
4490 chopprefix = strchr(drrb->drr_toname, '@');
34dc7c2f
BB
4491 } else if (strchr(tosnap, '@') == NULL) {
4492 /*
428870ff
BB
4493 * If a filesystem was specified without -d or -e, we want to
4494 * tack on everything after the fs specified by 'zfs send'.
34dc7c2f 4495 */
428870ff
BB
4496 chopprefix = drrb->drr_toname + strlen(sendfs);
4497 } else {
4498 /* A snapshot was specified as an exact path (no -d or -e). */
4499 if (recursive) {
4500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4501 "cannot specify snapshot name for multi-snapshot "
4502 "stream"));
43e52edd
BB
4503 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4504 goto out;
428870ff
BB
4505 }
4506 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
34dc7c2f 4507 }
428870ff
BB
4508
4509 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
bdbd5477 4510 ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
4511 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
4512 strchr(sendfs, '/') == NULL);
428870ff
BB
4513 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
4514 chopprefix[0] == '\0');
34dc7c2f
BB
4515
4516 /*
43e52edd 4517 * Determine name of destination snapshot.
34dc7c2f 4518 */
45cb520b 4519 (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
43e52edd 4520 (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
72c99dc9
RY
4521 if (cp != NULL)
4522 umem_free(cp, strlen(cp) + 1);
43e52edd
BB
4523 if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
4524 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4525 goto out;
34dc7c2f
BB
4526 }
4527
4528 /*
43e52edd 4529 * Determine the name of the origin snapshot.
34dc7c2f 4530 */
160af771 4531 if (originsnap) {
21a4f5cc 4532 (void) strlcpy(origin, originsnap, sizeof (origin));
160af771
GM
4533 if (flags->verbose)
4534 (void) printf("using provided clone origin %s\n",
4535 origin);
4536 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
43e52edd
BB
4537 if (guid_to_name(hdl, destsnap,
4538 drrb->drr_fromguid, B_FALSE, origin) != 0) {
34dc7c2f
BB
4539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4540 "local origin for clone %s does not exist"),
43e52edd
BB
4541 destsnap);
4542 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4543 goto out;
34dc7c2f 4544 }
330d06f9 4545 if (flags->verbose)
43e52edd 4546 (void) printf("found clone origin %s\n", origin);
34dc7c2f
BB
4547 }
4548
196bee4c 4549 if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
652bdc9b
MA
4550 DMU_BACKUP_FEATURE_DEDUP)) {
4551 (void) fprintf(stderr,
196bee4c
MA
4552 gettext("ERROR: \"zfs receive\" no longer supports "
4553 "deduplicated send streams. Use\n"
4554 "the \"zstream redup\" command to convert this stream "
4555 "to a regular,\n"
4556 "non-deduplicated stream.\n"));
4557 err = zfs_error(hdl, EZFS_NOTSUP, errbuf);
4558 goto out;
652bdc9b
MA
4559 }
4560
47dfff3b
MA
4561 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4562 DMU_BACKUP_FEATURE_RESUMING;
b5256303
TC
4563 boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4564 DMU_BACKUP_FEATURE_RAW;
9b840763
TC
4565 boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4566 DMU_BACKUP_FEATURE_EMBED_DATA;
b8864a23 4567 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
47dfff3b 4568 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
a132c2b4
AS
4569 stream_resumingnewfs = (drrb->drr_fromguid == 0 ||
4570 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && resuming;
34dc7c2f
BB
4571
4572 if (stream_wantsnewfs) {
4573 /*
4574 * if the parent fs does not exist, look for it based on
4575 * the parent snap GUID
4576 */
4577 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4578 "cannot receive new filesystem stream"));
4579
a51288aa 4580 (void) strlcpy(name, destsnap, sizeof (name));
43e52edd 4581 cp = strrchr(name, '/');
34dc7c2f
BB
4582 if (cp)
4583 *cp = '\0';
4584 if (cp &&
43e52edd 4585 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
eca7b760 4586 char suffix[ZFS_MAX_DATASET_NAME_LEN];
a51288aa
RY
4587 (void) strlcpy(suffix, strrchr(destsnap, '/'),
4588 sizeof (suffix));
43e52edd
BB
4589 if (guid_to_name(hdl, name, parent_snapguid,
4590 B_FALSE, destsnap) == 0) {
4591 *strchr(destsnap, '@') = '\0';
a51288aa 4592 (void) strlcat(destsnap, suffix,
ab672133 4593 sizeof (destsnap));
34dc7c2f
BB
4594 }
4595 }
4596 } else {
4597 /*
ebeb6f23
AG
4598 * If the fs does not exist, look for it based on the
4599 * fromsnap GUID.
34dc7c2f 4600 */
ebeb6f23
AG
4601 if (resuming) {
4602 (void) snprintf(errbuf, sizeof (errbuf),
4603 dgettext(TEXT_DOMAIN,
4604 "cannot receive resume stream"));
4605 } else {
4606 (void) snprintf(errbuf, sizeof (errbuf),
4607 dgettext(TEXT_DOMAIN,
4608 "cannot receive incremental stream"));
4609 }
34dc7c2f 4610
a51288aa 4611 (void) strlcpy(name, destsnap, sizeof (name));
43e52edd 4612 *strchr(name, '@') = '\0';
34dc7c2f 4613
428870ff
BB
4614 /*
4615 * If the exact receive path was specified and this is the
4616 * topmost path in the stream, then if the fs does not exist we
4617 * should look no further.
4618 */
330d06f9 4619 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
428870ff 4620 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
43e52edd 4621 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
eca7b760 4622 char snap[ZFS_MAX_DATASET_NAME_LEN];
a51288aa
RY
4623 (void) strlcpy(snap, strchr(destsnap, '@'),
4624 sizeof (snap));
43e52edd
BB
4625 if (guid_to_name(hdl, name, drrb->drr_fromguid,
4626 B_FALSE, destsnap) == 0) {
4627 *strchr(destsnap, '@') = '\0';
a51288aa 4628 (void) strlcat(destsnap, snap,
ab672133 4629 sizeof (destsnap));
34dc7c2f
BB
4630 }
4631 }
4632 }
4633
a51288aa 4634 (void) strlcpy(name, destsnap, sizeof (name));
43e52edd 4635 *strchr(name, '@') = '\0';
34dc7c2f 4636
30af21b0
PD
4637 redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4638 DMU_BACKUP_FEATURE_REDACTED;
4639
e8cf3a4f
AP
4640 if (flags->heal) {
4641 if (flags->isprefix || flags->istail || flags->force ||
4642 flags->canmountoff || flags->resumable || flags->nomount ||
4643 flags->skipholds) {
4644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4645 "corrective recv can not be used when combined with"
4646 " this flag"));
4647 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4648 goto out;
4649 }
4650 uint64_t guid =
4651 get_snap_guid(hdl, name, strchr(destsnap, '@') + 1);
4652 if (guid == 0) {
4653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4654 "corrective recv must specify an existing snapshot"
4655 " to heal"));
4656 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4657 goto out;
4658 } else if (guid != drrb->drr_toguid) {
4659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4660 "local snapshot doesn't match the snapshot"
4661 " in the provided stream"));
4662 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4663 goto out;
4664 }
4665 } else if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
43e52edd 4666 zfs_cmd_t zc = {"\0"};
e8cf3a4f 4667 zfs_handle_t *zhp = NULL;
4a385862 4668 boolean_t encrypted;
428870ff 4669
43e52edd
BB
4670 (void) strcpy(zc.zc_name, name);
4671
34dc7c2f 4672 /*
47dfff3b
MA
4673 * Destination fs exists. It must be one of these cases:
4674 * - an incremental send stream
4675 * - the stream specifies a new fs (full stream or clone)
4676 * and they want us to blow away the existing fs (and
4677 * have therefore specified -F and removed any snapshots)
4678 * - we are resuming a failed receive.
34dc7c2f 4679 */
34dc7c2f 4680 if (stream_wantsnewfs) {
d8d418ff 4681 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
330d06f9 4682 if (!flags->force) {
34dc7c2f
BB
4683 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4684 "destination '%s' exists\n"
43e52edd
BB
4685 "must specify -F to overwrite it"), name);
4686 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4687 goto out;
34dc7c2f 4688 }
b834b58a 4689 if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT,
34dc7c2f 4690 &zc) == 0) {
34dc7c2f
BB
4691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4692 "destination has snapshots (eg. %s)\n"
4693 "must destroy them to overwrite it"),
b53cb02d 4694 zc.zc_name);
43e52edd
BB
4695 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4696 goto out;
34dc7c2f 4697 }
d8d418ff 4698 if (is_volume && strrchr(name, '/') == NULL) {
4699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4700 "destination %s is the root dataset\n"
4701 "cannot overwrite with a ZVOL"),
4702 name);
4703 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4704 goto out;
4705 }
4706 if (is_volume &&
b834b58a 4707 zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT,
d8d418ff 4708 &zc) == 0) {
4709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4710 "destination has children (eg. %s)\n"
4711 "cannot overwrite with a ZVOL"),
4712 zc.zc_name);
4713 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4714 goto out;
4715 }
34dc7c2f
BB
4716 }
4717
43e52edd 4718 if ((zhp = zfs_open(hdl, name,
34dc7c2f 4719 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
43e52edd
BB
4720 err = -1;
4721 goto out;
34dc7c2f
BB
4722 }
4723
3ed9d688
JP
4724 /*
4725 * When receiving full/newfs on existing dataset, then it
4726 * should be done with "-F" flag. Its enforced for initial
4727 * receive in previous checks in this function.
4728 * Similarly, on resuming full/newfs recv on existing dataset,
4729 * it should be done with "-F" flag.
4730 *
4731 * When dataset doesn't exist, then full/newfs recv is done on
4732 * newly created dataset and it's marked INCONSISTENT. But
4733 * When receiving on existing dataset, recv is first done on
4734 * %recv and its marked INCONSISTENT. Existing dataset is not
4735 * marked INCONSISTENT.
4736 * Resume of full/newfs receive with dataset not INCONSISTENT
4737 * indicates that its resuming newfs on existing dataset. So,
4738 * enforce "-F" flag in this case.
4739 */
4740 if (stream_resumingnewfs &&
4741 !zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
4742 !flags->force) {
4743 zfs_close(zhp);
4744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4745 "Resuming recv on existing destination '%s'\n"
4746 "must specify -F to overwrite it"), name);
4747 err = zfs_error(hdl, EZFS_RESUME_EXISTS, errbuf);
4748 goto out;
4749 }
4750
34dc7c2f
BB
4751 if (stream_wantsnewfs &&
4752 zhp->zfs_dmustats.dds_origin[0]) {
34dc7c2f
BB
4753 zfs_close(zhp);
4754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4755 "destination '%s' is a clone\n"
43e52edd
BB
4756 "must destroy it to overwrite it"), name);
4757 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4758 goto out;
34dc7c2f
BB
4759 }
4760
b5256303 4761 /*
4a385862 4762 * Raw sends can not be performed as an incremental on top
78595377 4763 * of existing unencrypted datasets. zfs recv -F can't be
4a385862
TC
4764 * used to blow away an existing encrypted filesystem. This
4765 * is because it would require the dsl dir to point to the
4766 * new key (or lack of a key) and the old key at the same
4767 * time. The -F flag may still be used for deleting
4768 * intermediate snapshots that would otherwise prevent the
4769 * receive from working.
b5256303 4770 */
4a385862
TC
4771 encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4772 ZIO_CRYPT_OFF;
4773 if (!stream_wantsnewfs && !encrypted && raw) {
b5256303
TC
4774 zfs_close(zhp);
4775 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4a385862
TC
4776 "cannot perform raw receive on top of "
4777 "existing unencrypted dataset"));
b5256303
TC
4778 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4779 goto out;
4780 }
4781
4a385862
TC
4782 if (stream_wantsnewfs && flags->force &&
4783 ((raw && !encrypted) || encrypted)) {
4784 zfs_close(zhp);
4785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4786 "zfs receive -F cannot be used to destroy an "
4787 "encrypted filesystem or overwrite an "
4788 "unencrypted one with an encrypted one"));
4789 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4790 goto out;
4791 }
b5256303 4792
330d06f9 4793 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
a132c2b4 4794 (stream_wantsnewfs || stream_resumingnewfs)) {
34dc7c2f 4795 /* We can't do online recv in this case */
a57d3d45
MZ
4796 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4797 flags->forceunmount ? MS_FORCE : 0);
34dc7c2f 4798 if (clp == NULL) {
45d1cae3 4799 zfs_close(zhp);
43e52edd
BB
4800 err = -1;
4801 goto out;
34dc7c2f
BB
4802 }
4803 if (changelist_prefix(clp) != 0) {
4804 changelist_free(clp);
45d1cae3 4805 zfs_close(zhp);
43e52edd
BB
4806 err = -1;
4807 goto out;
34dc7c2f
BB
4808 }
4809 }
47dfff3b
MA
4810
4811 /*
4812 * If we are resuming a newfs, set newfs here so that we will
4813 * mount it if the recv succeeds this time. We can tell
4814 * that it was a newfs on the first recv because the fs
4815 * itself will be inconsistent (if the fs existed when we
4816 * did the first recv, we would have received it into
4817 * .../%recv).
4818 */
4819 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4820 newfs = B_TRUE;
4821
a3eeab2d 4822 /* we want to know if we're zoned when validating -o|-x props */
4823 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4824
c03f0470 4825 /* may need this info later, get it now we have zhp around */
4826 if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4827 NULL, NULL, 0, B_TRUE) == 0)
4828 hastoken = B_TRUE;
4829
a3eeab2d 4830 /* gather existing properties on destination */
4831 origprops = fnvlist_alloc();
4832 fnvlist_merge(origprops, zhp->zfs_props);
4833 fnvlist_merge(origprops, zhp->zfs_user_props);
4834
34dc7c2f
BB
4835 zfs_close(zhp);
4836 } else {
b5256303
TC
4837 zfs_handle_t *zhp;
4838
34dc7c2f
BB
4839 /*
4840 * Destination filesystem does not exist. Therefore we better
4841 * be creating a new filesystem (either from a full backup, or
4842 * a clone). It would therefore be invalid if the user
4843 * specified only the pool name (i.e. if the destination name
4844 * contained no slash character).
4845 */
a64f903b
GN
4846 cp = strrchr(name, '/');
4847
4848 if (!stream_wantsnewfs || cp == NULL) {
34dc7c2f 4849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43e52edd
BB
4850 "destination '%s' does not exist"), name);
4851 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4852 goto out;
34dc7c2f
BB
4853 }
4854
4855 /*
4856 * Trim off the final dataset component so we perform the
4857 * recvbackup ioctl to the filesystems's parent.
4858 */
4859 *cp = '\0';
4860
330d06f9 4861 if (flags->isprefix && !flags->istail && !flags->dryrun &&
43e52edd
BB
4862 create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4863 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4864 goto out;
34dc7c2f
BB
4865 }
4866
d8d418ff 4867 /* validate parent */
4868 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4869 if (zhp == NULL) {
4870 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4871 goto out;
4872 }
4873 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4875 "parent '%s' is not a filesystem"), name);
4876 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4877 zfs_close(zhp);
4878 goto out;
4879 }
4880
d8d418ff 4881 zfs_close(zhp);
b5256303 4882
34dc7c2f 4883 newfs = B_TRUE;
b5256303 4884 *cp = '/';
34dc7c2f
BB
4885 }
4886
330d06f9 4887 if (flags->verbose) {
e8cf3a4f 4888 (void) printf("%s %s%s stream of %s into %s\n",
330d06f9 4889 flags->dryrun ? "would receive" : "receiving",
e8cf3a4f 4890 flags->heal ? " corrective" : "",
34dc7c2f 4891 drrb->drr_fromguid ? "incremental" : "full",
43e52edd 4892 drrb->drr_toname, destsnap);
34dc7c2f
BB
4893 (void) fflush(stdout);
4894 }
4895
bb61cc31
TC
4896 /*
4897 * If this is the top-level dataset, record it so we can use it
4898 * for recursive operations later.
4899 */
4900 if (top_zfs != NULL &&
4901 (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) {
bee7e4ff 4902 toplevel = B_TRUE;
bb61cc31
TC
4903 if (*top_zfs == NULL)
4904 *top_zfs = zfs_strdup(hdl, name);
4905 }
4906
a3eeab2d 4907 if (drrb->drr_type == DMU_OST_ZVOL) {
4908 type = ZFS_TYPE_VOLUME;
4909 } else if (drrb->drr_type == DMU_OST_ZFS) {
4910 type = ZFS_TYPE_FILESYSTEM;
4911 } else {
4912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4913 "invalid record type: 0x%d"), drrb->drr_type);
4914 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4915 goto out;
4916 }
d9c460a0
TC
4917 if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4918 stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4919 &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
a3eeab2d 4920 goto out;
4921
da689887
TC
4922 /*
4923 * When sending with properties (zfs send -p), the encryption property
4924 * is not included because it is a SETONCE property and therefore
4925 * treated as read only. However, we are always able to determine its
4926 * value because raw sends will include it in the DRR_BDEGIN payload
4927 * and non-raw sends with properties are not allowed for encrypted
4928 * datasets. Therefore, if this is a non-raw properties stream, we can
4929 * infer that the value should be ZIO_CRYPT_OFF and manually add that
4930 * to the received properties.
4931 */
4932 if (stream_wantsnewfs && !raw && rcvprops != NULL &&
4933 !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4934 if (oxprops == NULL)
4935 oxprops = fnvlist_alloc();
4936 fnvlist_add_uint64(oxprops,
4937 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF);
4938 }
4939
ee6615e0
I
4940 if (flags->dryrun) {
4941 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
4942
4943 /*
4944 * We have read the DRR_BEGIN record, but we have
4945 * not yet read the payload. For non-dryrun sends
4946 * this will be done by the kernel, so we must
4947 * emulate that here, before attempting to read
4948 * more records.
4949 */
4950 err = recv_read(hdl, infd, buf, drr->drr_payloadlen,
4951 flags->byteswap, NULL);
4952 free(buf);
4953 if (err != 0)
4954 goto out;
4955
4956 err = recv_skip(hdl, infd, flags->byteswap);
4957 goto out;
4958 }
4959
e8cf3a4f
AP
4960 if (flags->heal) {
4961 err = ioctl_err = lzc_receive_with_heal(destsnap, rcvprops,
4962 oxprops, wkeydata, wkeylen, origin, flags->force,
4963 flags->heal, flags->resumable, raw, infd, drr_noswap, -1,
4964 &read_bytes, &errflags, NULL, &prop_errors);
4965 } else {
4966 err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4967 oxprops, wkeydata, wkeylen, origin, flags->force,
4968 flags->resumable, raw, infd, drr_noswap, -1, &read_bytes,
4969 &errflags, NULL, &prop_errors);
4970 }
43e52edd
BB
4971 ioctl_errno = ioctl_err;
4972 prop_errflags = errflags;
428870ff
BB
4973
4974 if (err == 0) {
428870ff
BB
4975 nvpair_t *prop_err = NULL;
4976
4977 while ((prop_err = nvlist_next_nvpair(prop_errors,
4978 prop_err)) != NULL) {
4979 char tbuf[1024];
4980 zfs_prop_t prop;
4981 int intval;
4982
4983 prop = zfs_name_to_prop(nvpair_name(prop_err));
4984 (void) nvpair_value_int32(prop_err, &intval);
4985 if (strcmp(nvpair_name(prop_err),
4986 ZPROP_N_MORE_ERRORS) == 0) {
4987 trunc_prop_errs(intval);
4988 break;
671c9354
DM
4989 } else if (snapname == NULL || finalsnap == NULL ||
4990 strcmp(finalsnap, snapname) == 0 ||
4991 strcmp(nvpair_name(prop_err),
4992 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4993 /*
4994 * Skip the special case of, for example,
4995 * "refquota", errors on intermediate
4996 * snapshots leading up to a final one.
4997 * That's why we have all of the checks above.
4998 *
4999 * See zfs_ioctl.c's extract_delay_props() for
5000 * a list of props which can fail on
5001 * intermediate snapshots, but shouldn't
5002 * affect the overall receive.
5003 */
428870ff
BB
5004 (void) snprintf(tbuf, sizeof (tbuf),
5005 dgettext(TEXT_DOMAIN,
5006 "cannot receive %s property on %s"),
43e52edd 5007 nvpair_name(prop_err), name);
428870ff
BB
5008 zfs_setprop_error(hdl, prop, intval, tbuf);
5009 }
5010 }
428870ff
BB
5011 }
5012
b128c09f 5013 if (err == 0 && snapprops_nvlist) {
43e52edd 5014 zfs_cmd_t zc = {"\0"};
b128c09f 5015
a51288aa 5016 (void) strlcpy(zc.zc_name, destsnap, sizeof (zc.zc_name));
43e52edd 5017 zc.zc_cookie = B_TRUE; /* received */
18dbf5c8
AZ
5018 zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist);
5019 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
5020 zcmd_free_nvlists(&zc);
b128c09f 5021 }
9c5e88b1
PZ
5022 if (err == 0 && snapholds_nvlist) {
5023 nvpair_t *pair;
5024 nvlist_t *holds, *errors = NULL;
5025 int cleanup_fd = -1;
5026
5027 VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
5028 for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
5029 pair != NULL;
5030 pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
60a2434b 5031 fnvlist_add_string(holds, destsnap, nvpair_name(pair));
9c5e88b1
PZ
5032 }
5033 (void) lzc_hold(holds, cleanup_fd, &errors);
60a2434b
RM
5034 fnvlist_free(snapholds_nvlist);
5035 fnvlist_free(holds);
9c5e88b1 5036 }
b128c09f 5037
428870ff 5038 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
34dc7c2f
BB
5039 /*
5040 * It may be that this snapshot already exists,
5041 * in which case we want to consume & ignore it
5042 * rather than failing.
5043 */
5044 avl_tree_t *local_avl;
5045 nvlist_t *local_nv, *fs;
43e52edd 5046 cp = strchr(destsnap, '@');
34dc7c2f
BB
5047
5048 /*
5049 * XXX Do this faster by just iterating over snaps in
5050 * this fs. Also if zc_value does not exist, we will
5051 * get a strange "does not exist" error message.
5052 */
5053 *cp = '\0';
b5256303 5054 if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
099fa7e4
PCG
5055 B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE,
5056 B_TRUE, &local_nv, &local_avl) == 0) {
34dc7c2f
BB
5057 *cp = '@';
5058 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
5059 fsavl_destroy(local_avl);
60a2434b 5060 fnvlist_free(local_nv);
34dc7c2f
BB
5061
5062 if (fs != NULL) {
330d06f9 5063 if (flags->verbose) {
34dc7c2f 5064 (void) printf("snap %s already exists; "
43e52edd 5065 "ignoring\n", destsnap);
34dc7c2f 5066 }
428870ff 5067 err = ioctl_err = recv_skip(hdl, infd,
330d06f9 5068 flags->byteswap);
34dc7c2f
BB
5069 }
5070 }
5071 *cp = '@';
5072 }
5073
34dc7c2f
BB
5074 if (ioctl_err != 0) {
5075 switch (ioctl_errno) {
5076 case ENODEV:
43e52edd 5077 cp = strchr(destsnap, '@');
34dc7c2f
BB
5078 *cp = '\0';
5079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5080 "most recent snapshot of %s does not\n"
43e52edd 5081 "match incremental source"), destsnap);
34dc7c2f
BB
5082 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5083 *cp = '@';
5084 break;
5085 case ETXTBSY:
5086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5087 "destination %s has been modified\n"
43e52edd 5088 "since most recent snapshot"), name);
34dc7c2f
BB
5089 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5090 break;
b5256303 5091 case EACCES:
e8cf3a4f
AP
5092 if (flags->heal) {
5093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5094 "key must be loaded to do a non-raw "
5095 "corrective recv on an encrypted "
5096 "dataset."));
5097 } else if (raw && stream_wantsnewfs) {
b5256303
TC
5098 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5099 "failed to create encryption key"));
5100 } else if (raw && !stream_wantsnewfs) {
5101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5102 "encryption key does not match "
5103 "existing key"));
5104 } else {
5105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5106 "inherited key must be loaded"));
5107 }
5108 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
5109 break;
34dc7c2f 5110 case EEXIST:
43e52edd 5111 cp = strchr(destsnap, '@');
34dc7c2f
BB
5112 if (newfs) {
5113 /* it's the containing fs that exists */
5114 *cp = '\0';
5115 }
5116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5117 "destination already exists"));
5118 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
5119 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
43e52edd 5120 destsnap);
34dc7c2f
BB
5121 *cp = '@';
5122 break;
5123 case EINVAL:
63267f7f 5124 if (embedded && !raw) {
9b840763
TC
5125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5126 "incompatible embedded data stream "
5127 "feature with encrypted receive."));
63267f7f
AJ
5128 } else if (flags->resumable) {
5129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5130 "kernel modules must be upgraded to "
5131 "receive this stream."));
2ba59fa9 5132 }
34dc7c2f
BB
5133 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5134 break;
5135 case ECKSUM:
7145123b 5136 case ZFS_ERR_STREAM_TRUNCATED:
e8cf3a4f
AP
5137 if (flags->heal)
5138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5139 "corrective receive was not able to "
5140 "reconstruct the data needed for "
5141 "healing."));
5142 else
5143 recv_ecksum_set_aux(hdl, destsnap,
5144 flags->resumable, ioctl_err == ECKSUM);
34dc7c2f
BB
5145 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5146 break;
7bcb7f08
MA
5147 case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH:
5148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5149 "incremental send stream requires -L "
5150 "(--large-block), to match previous receive."));
5151 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5152 break;
428870ff 5153 case ENOTSUP:
e8cf3a4f
AP
5154 if (flags->heal)
5155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5156 "stream is not compatible with the "
5157 "data in the pool."));
5158 else
5159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5160 "pool must be upgraded to receive this "
5161 "stream."));
428870ff
BB
5162 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5163 break;
b988f32c
RN
5164 case ZFS_ERR_CRYPTO_NOTSUP:
5165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5166 "stream uses crypto parameters not compatible with "
5167 "this pool"));
5168 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5169 break;
428870ff
BB
5170 case EDQUOT:
5171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
c03f0470 5172 "destination %s space quota exceeded."), name);
330d06f9 5173 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
428870ff 5174 break;
f00ab3f2
TC
5175 case ZFS_ERR_FROM_IVSET_GUID_MISSING:
5176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
73c25a78 5177 "IV set guid missing. See errata %u at "
a2f944a1
RM
5178 "https://openzfs.github.io/openzfs-docs/msg/"
5179 "ZFS-8000-ER."),
f00ab3f2
TC
5180 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
5181 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5182 break;
5183 case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
5184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5185 "IV set guid mismatch. See the 'zfs receive' "
5186 "man page section\n discussing the limitations "
5187 "of raw encrypted send streams."));
5188 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5189 break;
caf9dd20
BB
5190 case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
5191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5192 "Spill block flag missing for raw send.\n"
5193 "The zfs software on the sending system must "
5194 "be updated."));
5195 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5196 break;
3ed9d688
JP
5197 case ZFS_ERR_RESUME_EXISTS:
5198 cp = strchr(destsnap, '@');
5199 if (newfs) {
5200 /* it's the containing fs that exists */
5201 *cp = '\0';
5202 }
5203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5204 "Resuming recv on existing dataset without force"));
5205 (void) zfs_error_fmt(hdl, EZFS_RESUME_EXISTS,
5206 dgettext(TEXT_DOMAIN, "cannot resume recv %s"),
5207 destsnap);
5208 *cp = '@';
5209 break;
73968def
RY
5210 case E2BIG:
5211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5212 "zfs receive required kernel memory allocation "
5213 "larger than the system can support. Please file "
5214 "an issue at the OpenZFS issue tracker:\n"
5215 "https://github.com/openzfs/zfs/issues/new"));
5216 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5217 break;
c03f0470 5218 case EBUSY:
5219 if (hastoken) {
5220 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5221 "destination %s contains "
5222 "partially-complete state from "
5223 "\"zfs receive -s\"."), name);
5224 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
5225 break;
5226 }
9a70e97f 5227 zfs_fallthrough;
34dc7c2f
BB
5228 default:
5229 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
5230 }
5231 }
5232
5233 /*
428870ff
BB
5234 * Mount the target filesystem (if created). Also mount any
5235 * children of the target filesystem if we did a replication
5236 * receive (indicated by stream_avl being non-NULL).
34dc7c2f 5237 */
34dc7c2f 5238 if (clp) {
89d43feb
GM
5239 if (!flags->nomount)
5240 err |= changelist_postfix(clp);
34dc7c2f
BB
5241 changelist_free(clp);
5242 }
5243
bb61cc31
TC
5244 if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted)
5245 flags->domount = B_TRUE;
5246
428870ff
BB
5247 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
5248 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
43e52edd 5249 "failed to clear unreceived properties on %s"), name);
428870ff
BB
5250 (void) fprintf(stderr, "\n");
5251 }
5252 if (prop_errflags & ZPROP_ERR_NORESTORE) {
5253 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
43e52edd 5254 "failed to restore original properties on %s"), name);
428870ff
BB
5255 (void) fprintf(stderr, "\n");
5256 }
5257
43e52edd
BB
5258 if (err || ioctl_err) {
5259 err = -1;
5260 goto out;
5261 }
572e2857 5262
330d06f9 5263 if (flags->verbose) {
34dc7c2f
BB
5264 char buf1[64];
5265 char buf2[64];
43e52edd 5266 uint64_t bytes = read_bytes;
67de71e6
AZ
5267 struct timespec delta;
5268 clock_gettime(CLOCK_MONOTONIC_RAW, &delta);
5269 if (begin_time.tv_nsec > delta.tv_nsec) {
5270 delta.tv_nsec =
5271 1000000000 + delta.tv_nsec - begin_time.tv_nsec;
5272 delta.tv_sec -= 1;
5273 } else
5274 delta.tv_nsec -= begin_time.tv_nsec;
5275 delta.tv_sec -= begin_time.tv_sec;
5276 if (delta.tv_sec == 0 && delta.tv_nsec == 0)
5277 delta.tv_nsec = 1;
5278 double delta_f = delta.tv_sec + (delta.tv_nsec / 1e9);
e7fbeb60 5279 zfs_nicebytes(bytes, buf1, sizeof (buf1));
67de71e6 5280 zfs_nicebytes(bytes / delta_f, buf2, sizeof (buf2));
34dc7c2f 5281
67de71e6
AZ
5282 (void) printf("received %s stream in %.2f seconds (%s/sec)\n",
5283 buf1, delta_f, buf2);
34dc7c2f
BB
5284 }
5285
43e52edd
BB
5286 err = 0;
5287out:
5288 if (prop_errors != NULL)
60a2434b 5289 fnvlist_free(prop_errors);
43e52edd 5290
b5256303 5291 if (tmp_keylocation[0] != '\0') {
60a2434b
RM
5292 fnvlist_add_string(rcvprops,
5293 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation);
b5256303
TC
5294 }
5295
43e52edd 5296 if (newprops)
60a2434b 5297 fnvlist_free(rcvprops);
a3eeab2d 5298
60a2434b
RM
5299 fnvlist_free(oxprops);
5300 fnvlist_free(origprops);
43e52edd
BB
5301
5302 return (err);
34dc7c2f
BB
5303}
5304
a3eeab2d 5305/*
5306 * Check properties we were asked to override (both -o|-x)
5307 */
5308static boolean_t
5309zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
5310 const char *errbuf)
5311{
ae07fc13 5312 nvpair_t *nvp = NULL;
a3eeab2d 5313 zfs_prop_t prop;
5314 const char *name;
5315
a3eeab2d 5316 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
5317 name = nvpair_name(nvp);
5318 prop = zfs_name_to_prop(name);
5319
4ff7a8fa 5320 if (prop == ZPROP_USERPROP) {
a3eeab2d 5321 if (!zfs_prop_user(name)) {
5322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7633c0ae 5323 "%s: invalid property '%s'"), errbuf, name);
a3eeab2d 5324 return (B_FALSE);
5325 }
5326 continue;
5327 }
5328 /*
5329 * "origin" is readonly but is used to receive datasets as
5330 * clones so we don't raise an error here
5331 */
5332 if (prop == ZFS_PROP_ORIGIN)
5333 continue;
5334
d9c460a0
TC
5335 /* encryption params have their own verification later */
5336 if (prop == ZFS_PROP_ENCRYPTION ||
5337 zfs_prop_encryption_key_param(prop))
5338 continue;
5339
a3eeab2d 5340 /*
5341 * cannot override readonly, set-once and other specific
5342 * settable properties
5343 */
5344 if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
5345 prop == ZFS_PROP_VOLSIZE) {
5346 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7633c0ae 5347 "%s: invalid property '%s'"), errbuf, name);
a3eeab2d 5348 return (B_FALSE);
5349 }
5350 }
5351
5352 return (B_TRUE);
5353}
5354
b128c09f 5355static int
fcff0f35
PD
5356zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
5357 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
196bee4c
MA
5358 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs,
5359 const char *finalsnap, nvlist_t *cmdprops)
34dc7c2f
BB
5360{
5361 int err;
5362 dmu_replay_record_t drr, drr_noswap;
5363 struct drr_begin *drrb = &drr.drr_u.drr_begin;
d68a2bfa 5364 char errbuf[ERRBUFLEN];
2598c001 5365 zio_cksum_t zcksum = { { 0 } };
428870ff
BB
5366 uint64_t featureflags;
5367 int hdrtype;
34dc7c2f
BB
5368
5369 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5370 "cannot receive"));
5371
a3eeab2d 5372 /* check cmdline props, raise an error if they cannot be received */
7633c0ae 5373 if (!zfs_receive_checkprops(hdl, cmdprops, errbuf))
ae07fc13 5374 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
a3eeab2d 5375
330d06f9 5376 if (flags->isprefix &&
34dc7c2f
BB
5377 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
5378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
5379 "(%s) does not exist"), tosnap);
5380 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5381 }
fcff0f35
PD
5382 if (originsnap &&
5383 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
5384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
5385 "(%s) does not exist"), originsnap);
5386 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5387 }
34dc7c2f
BB
5388
5389 /* read in the BEGIN record */
5390 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
5391 &zcksum)))
5392 return (err);
5393
5394 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
5395 /* It's the double end record at the end of a package */
5396 return (ENODATA);
5397 }
5398
5399 /* the kernel needs the non-byteswapped begin record */
5400 drr_noswap = drr;
5401
330d06f9 5402 flags->byteswap = B_FALSE;
34dc7c2f
BB
5403 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
5404 /*
5405 * We computed the checksum in the wrong byteorder in
5406 * recv_read() above; do it again correctly.
5407 */
861166b0 5408 memset(&zcksum, 0, sizeof (zio_cksum_t));
34dc7c2f 5409 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
330d06f9 5410 flags->byteswap = B_TRUE;
34dc7c2f
BB
5411
5412 drr.drr_type = BSWAP_32(drr.drr_type);
5413 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
5414 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
428870ff 5415 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
34dc7c2f
BB
5416 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
5417 drrb->drr_type = BSWAP_32(drrb->drr_type);
5418 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
5419 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
5420 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
5421 }
5422
5423 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
5424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5425 "stream (bad magic number)"));
5426 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5427 }
5428
428870ff
BB
5429 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
5430 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
5431
5432 if (!DMU_STREAM_SUPPORTED(featureflags) ||
5433 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
a2ffc0e0
RE
5434 /*
5435 * Let's be explicit about this one, since rather than
5436 * being a new feature we can't know, it's an old
5437 * feature we dropped.
5438 */
5439 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
5440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5441 "stream has deprecated feature: dedup, try "
5442 "'zstream redup [send in a file] | zfs recv "
5443 "[...]'"));
5444 } else {
5445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5446 "stream has unsupported feature, feature flags = "
5447 "%llx (unknown flags = %llx)"),
5448 (u_longlong_t)featureflags,
5449 (u_longlong_t)((featureflags) &
5450 ~DMU_BACKUP_FEATURE_MASK));
5451 }
428870ff
BB
5452 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5453 }
5454
9c5e88b1 5455 /* Holds feature is set once in the compound stream header. */
c618f87c 5456 if (featureflags & DMU_BACKUP_FEATURE_HOLDS)
9c5e88b1
PZ
5457 flags->holds = B_TRUE;
5458
34dc7c2f
BB
5459 if (strchr(drrb->drr_toname, '@') == NULL) {
5460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5461 "stream (bad snapshot name)"));
5462 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5463 }
5464
428870ff 5465 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
eca7b760 5466 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
428870ff
BB
5467 if (sendfs == NULL) {
5468 /*
5469 * We were not called from zfs_receive_package(). Get
5470 * the fs specified by 'zfs send'.
5471 */
5472 char *cp;
5473 (void) strlcpy(nonpackage_sendfs,
eca7b760
IK
5474 drr.drr_u.drr_begin.drr_toname,
5475 sizeof (nonpackage_sendfs));
428870ff
BB
5476 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
5477 *cp = '\0';
5478 sendfs = nonpackage_sendfs;
671c9354 5479 VERIFY(finalsnap == NULL);
428870ff 5480 }
fcff0f35
PD
5481 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
5482 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
196bee4c 5483 finalsnap, cmdprops));
428870ff
BB
5484 } else {
5485 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
5486 DMU_COMPOUNDSTREAM);
fcff0f35 5487 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
196bee4c 5488 &zcksum, top_zfs, cmdprops));
34dc7c2f
BB
5489 }
5490}
b128c09f
BB
5491
5492/*
5493 * Restores a backup of tosnap from the file descriptor specified by infd.
5494 * Return 0 on total success, -2 if some things couldn't be
5495 * destroyed/renamed/promoted, -1 if some things couldn't be received.
47dfff3b
MA
5496 * (-1 will override -2, if -1 and the resumable flag was specified the
5497 * transfer can be resumed if the sending side supports it).
b128c09f
BB
5498 */
5499int
fcff0f35
PD
5500zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
5501 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
b128c09f
BB
5502{
5503 char *top_zfs = NULL;
5504 int err;
5c3f61eb 5505 struct stat sb;
d1807f16 5506 const char *originsnap = NULL;
5c3f61eb
RY
5507
5508 /*
5509 * The only way fstat can fail is if we do not have a valid file
5510 * descriptor.
5511 */
5512 if (fstat(infd, &sb) == -1) {
5513 perror("fstat");
5514 return (-2);
5515 }
5516
fcff0f35
PD
5517 if (props) {
5518 err = nvlist_lookup_string(props, "origin", &originsnap);
5519 if (err && err != ENOENT)
5520 return (err);
5521 }
5522
fcff0f35 5523 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
196bee4c 5524 stream_avl, &top_zfs, NULL, props);
b128c09f 5525
bb61cc31 5526 if (err == 0 && !flags->nomount && flags->domount && top_zfs) {
689f093e
GN
5527 zfs_handle_t *zhp = NULL;
5528 prop_changelist_t *clp = NULL;
b128c09f 5529
bb61cc31
TC
5530 zhp = zfs_open(hdl, top_zfs,
5531 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5532 if (zhp == NULL) {
5533 err = -1;
5534 goto out;
5535 } else {
5536 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
5537 zfs_close(zhp);
5538 goto out;
5539 }
5540
b128c09f 5541 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
a57d3d45
MZ
5542 CL_GATHER_MOUNT_ALWAYS,
5543 flags->forceunmount ? MS_FORCE : 0);
b128c09f 5544 zfs_close(zhp);
bb61cc31
TC
5545 if (clp == NULL) {
5546 err = -1;
5547 goto out;
b128c09f 5548 }
bb61cc31
TC
5549
5550 /* mount and share received datasets */
5551 err = changelist_postfix(clp);
5552 changelist_free(clp);
5553 if (err != 0)
5554 err = -1;
b128c09f 5555 }
b128c09f 5556 }
bb61cc31
TC
5557
5558out:
b128c09f
BB
5559 if (top_zfs)
5560 free(top_zfs);
5561
5562 return (err);
5563}