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