]>
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 | |
9 | * or http://www.opensolaris.org/os/licensing. | |
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. |
47dfff3b | 24 | * Copyright (c) 2011, 2015 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> |
a3eeab2d | 31 | * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. |
34dc7c2f BB |
32 | */ |
33 | ||
34dc7c2f BB |
34 | #include <assert.h> |
35 | #include <ctype.h> | |
36 | #include <errno.h> | |
34dc7c2f BB |
37 | #include <libintl.h> |
38 | #include <stdio.h> | |
39 | #include <stdlib.h> | |
40 | #include <strings.h> | |
41 | #include <unistd.h> | |
42 | #include <stddef.h> | |
43 | #include <fcntl.h> | |
44 | #include <sys/mount.h> | |
9b020fd9 BB |
45 | #include <sys/mntent.h> |
46 | #include <sys/mnttab.h> | |
47 | #include <sys/avl.h> | |
48 | #include <sys/debug.h> | |
5c3f61eb | 49 | #include <sys/stat.h> |
9b020fd9 | 50 | #include <stddef.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> |
34dc7c2f BB |
57 | |
58 | #include "zfs_namecheck.h" | |
59 | #include "zfs_prop.h" | |
428870ff | 60 | #include "zfs_fletcher.h" |
34dc7c2f | 61 | #include "libzfs_impl.h" |
47dfff3b | 62 | #include <zlib.h> |
428870ff | 63 | #include <sys/zio_checksum.h> |
b5256303 | 64 | #include <sys/dsl_crypt.h> |
428870ff | 65 | #include <sys/ddt.h> |
1b9d8c34 | 66 | #include <sys/socket.h> |
3c67d83a | 67 | #include <sys/sha2.h> |
34dc7c2f | 68 | |
428870ff BB |
69 | /* in libzfs_dataset.c */ |
70 | extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *); | |
34dc7c2f | 71 | |
fcff0f35 PD |
72 | static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *, |
73 | recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int, | |
a3eeab2d | 74 | uint64_t *, const char *, nvlist_t *); |
47dfff3b MA |
75 | static int guid_to_name(libzfs_handle_t *, const char *, |
76 | uint64_t, boolean_t, char *); | |
428870ff | 77 | |
2598c001 | 78 | static const zio_cksum_t zero_cksum = { { 0 } }; |
428870ff BB |
79 | |
80 | typedef struct dedup_arg { | |
81 | int inputfd; | |
82 | int outputfd; | |
83 | libzfs_handle_t *dedup_hdl; | |
84 | } dedup_arg_t; | |
85 | ||
37abac6d BP |
86 | typedef struct progress_arg { |
87 | zfs_handle_t *pa_zhp; | |
88 | int pa_fd; | |
89 | boolean_t pa_parsable; | |
90 | } progress_arg_t; | |
91 | ||
428870ff BB |
92 | typedef struct dataref { |
93 | uint64_t ref_guid; | |
94 | uint64_t ref_object; | |
95 | uint64_t ref_offset; | |
96 | } dataref_t; | |
97 | ||
98 | typedef struct dedup_entry { | |
99 | struct dedup_entry *dde_next; | |
100 | zio_cksum_t dde_chksum; | |
101 | uint64_t dde_prop; | |
102 | dataref_t dde_ref; | |
103 | } dedup_entry_t; | |
104 | ||
105 | #define MAX_DDT_PHYSMEM_PERCENT 20 | |
106 | #define SMALLEST_POSSIBLE_MAX_DDT_MB 128 | |
107 | ||
108 | typedef struct dedup_table { | |
109 | dedup_entry_t **dedup_hash_array; | |
110 | umem_cache_t *ddecache; | |
111 | uint64_t max_ddt_size; /* max dedup table size in bytes */ | |
112 | uint64_t cur_ddt_size; /* current dedup table size in bytes */ | |
113 | uint64_t ddt_count; | |
114 | int numhashbits; | |
115 | boolean_t ddt_full; | |
116 | } dedup_table_t; | |
117 | ||
118 | static int | |
119 | high_order_bit(uint64_t n) | |
120 | { | |
121 | int count; | |
122 | ||
123 | for (count = 0; n != 0; count++) | |
124 | n >>= 1; | |
125 | return (count); | |
126 | } | |
127 | ||
128 | static size_t | |
129 | ssread(void *buf, size_t len, FILE *stream) | |
130 | { | |
131 | size_t outlen; | |
132 | ||
133 | if ((outlen = fread(buf, len, 1, stream)) == 0) | |
134 | return (0); | |
135 | ||
136 | return (outlen); | |
137 | } | |
138 | ||
139 | static void | |
140 | ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp, | |
141 | zio_cksum_t *cs, uint64_t prop, dataref_t *dr) | |
142 | { | |
143 | dedup_entry_t *dde; | |
144 | ||
145 | if (ddt->cur_ddt_size >= ddt->max_ddt_size) { | |
146 | if (ddt->ddt_full == B_FALSE) { | |
147 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
148 | "Dedup table full. Deduplication will continue " | |
149 | "with existing table entries")); | |
150 | ddt->ddt_full = B_TRUE; | |
151 | } | |
152 | return; | |
153 | } | |
154 | ||
155 | if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT)) | |
156 | != NULL) { | |
157 | assert(*ddepp == NULL); | |
158 | dde->dde_next = NULL; | |
159 | dde->dde_chksum = *cs; | |
160 | dde->dde_prop = prop; | |
161 | dde->dde_ref = *dr; | |
162 | *ddepp = dde; | |
163 | ddt->cur_ddt_size += sizeof (dedup_entry_t); | |
164 | ddt->ddt_count++; | |
165 | } | |
166 | } | |
167 | ||
168 | /* | |
169 | * Using the specified dedup table, do a lookup for an entry with | |
170 | * the checksum cs. If found, return the block's reference info | |
171 | * in *dr. Otherwise, insert a new entry in the dedup table, using | |
172 | * the reference information specified by *dr. | |
173 | * | |
174 | * return value: true - entry was found | |
175 | * false - entry was not found | |
176 | */ | |
177 | static boolean_t | |
178 | ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs, | |
179 | uint64_t prop, dataref_t *dr) | |
180 | { | |
181 | uint32_t hashcode; | |
182 | dedup_entry_t **ddepp; | |
183 | ||
184 | hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits); | |
185 | ||
186 | for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL; | |
187 | ddepp = &((*ddepp)->dde_next)) { | |
188 | if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) && | |
189 | (*ddepp)->dde_prop == prop) { | |
190 | *dr = (*ddepp)->dde_ref; | |
191 | return (B_TRUE); | |
192 | } | |
193 | } | |
194 | ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr); | |
195 | return (B_FALSE); | |
196 | } | |
197 | ||
198 | static int | |
37f8a883 MA |
199 | dump_record(dmu_replay_record_t *drr, void *payload, int payload_len, |
200 | zio_cksum_t *zc, int outfd) | |
428870ff | 201 | { |
37f8a883 MA |
202 | ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), |
203 | ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t)); | |
204 | fletcher_4_incremental_native(drr, | |
205 | offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc); | |
206 | if (drr->drr_type != DRR_BEGIN) { | |
207 | ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u. | |
208 | drr_checksum.drr_checksum)); | |
209 | drr->drr_u.drr_checksum.drr_checksum = *zc; | |
210 | } | |
211 | fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum, | |
212 | sizeof (zio_cksum_t), zc); | |
213 | if (write(outfd, drr, sizeof (*drr)) == -1) | |
214 | return (errno); | |
215 | if (payload_len != 0) { | |
216 | fletcher_4_incremental_native(payload, payload_len, zc); | |
217 | if (write(outfd, payload, payload_len) == -1) | |
218 | return (errno); | |
219 | } | |
220 | return (0); | |
428870ff BB |
221 | } |
222 | ||
223 | /* | |
224 | * This function is started in a separate thread when the dedup option | |
225 | * has been requested. The main send thread determines the list of | |
226 | * snapshots to be included in the send stream and makes the ioctl calls | |
227 | * for each one. But instead of having the ioctl send the output to the | |
228 | * the output fd specified by the caller of zfs_send()), the | |
229 | * ioctl is told to direct the output to a pipe, which is read by the | |
230 | * alternate thread running THIS function. This function does the | |
231 | * dedup'ing by: | |
232 | * 1. building a dedup table (the DDT) | |
233 | * 2. doing checksums on each data block and inserting a record in the DDT | |
234 | * 3. looking for matching checksums, and | |
235 | * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever | |
236 | * a duplicate block is found. | |
237 | * The output of this function then goes to the output fd requested | |
238 | * by the caller of zfs_send(). | |
239 | */ | |
240 | static void * | |
241 | cksummer(void *arg) | |
242 | { | |
243 | dedup_arg_t *dda = arg; | |
f1512ee6 | 244 | char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE); |
47dfff3b | 245 | dmu_replay_record_t thedrr = { 0 }; |
428870ff | 246 | dmu_replay_record_t *drr = &thedrr; |
428870ff BB |
247 | FILE *ofp; |
248 | int outfd; | |
428870ff BB |
249 | dedup_table_t ddt; |
250 | zio_cksum_t stream_cksum; | |
428870ff BB |
251 | uint64_t numbuckets; |
252 | ||
2e0e443a GM |
253 | #ifdef _ILP32 |
254 | ddt.max_ddt_size = SMALLEST_POSSIBLE_MAX_DDT_MB << 20; | |
255 | #else | |
256 | uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); | |
428870ff | 257 | ddt.max_ddt_size = |
37f8a883 MA |
258 | MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100, |
259 | SMALLEST_POSSIBLE_MAX_DDT_MB << 20); | |
2e0e443a | 260 | #endif |
428870ff | 261 | |
37f8a883 | 262 | numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t)); |
428870ff BB |
263 | |
264 | /* | |
265 | * numbuckets must be a power of 2. Increase number to | |
266 | * a power of 2 if necessary. | |
267 | */ | |
268 | if (!ISP2(numbuckets)) | |
f4bae2ed | 269 | numbuckets = 1ULL << high_order_bit(numbuckets); |
428870ff BB |
270 | |
271 | ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *)); | |
272 | ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0, | |
273 | NULL, NULL, NULL, NULL, NULL, 0); | |
274 | ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *); | |
275 | ddt.numhashbits = high_order_bit(numbuckets) - 1; | |
276 | ddt.ddt_full = B_FALSE; | |
277 | ||
428870ff BB |
278 | outfd = dda->outputfd; |
279 | ofp = fdopen(dda->inputfd, "r"); | |
37f8a883 | 280 | while (ssread(drr, sizeof (*drr), ofp) != 0) { |
428870ff | 281 | |
e2454897 GM |
282 | /* |
283 | * kernel filled in checksum, we are going to write same | |
284 | * record, but need to regenerate checksum. | |
285 | */ | |
286 | if (drr->drr_type != DRR_BEGIN) { | |
287 | bzero(&drr->drr_u.drr_checksum.drr_checksum, | |
288 | sizeof (drr->drr_u.drr_checksum.drr_checksum)); | |
289 | } | |
290 | ||
428870ff BB |
291 | switch (drr->drr_type) { |
292 | case DRR_BEGIN: | |
293 | { | |
37f8a883 MA |
294 | struct drr_begin *drrb = &drr->drr_u.drr_begin; |
295 | int fflags; | |
296 | int sz = 0; | |
428870ff BB |
297 | ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0); |
298 | ||
37f8a883 MA |
299 | ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC); |
300 | ||
428870ff BB |
301 | /* set the DEDUP feature flag for this stream */ |
302 | fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); | |
303 | fflags |= (DMU_BACKUP_FEATURE_DEDUP | | |
304 | DMU_BACKUP_FEATURE_DEDUPPROPS); | |
305 | DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags); | |
306 | ||
47dfff3b | 307 | if (drr->drr_payloadlen != 0) { |
37f8a883 | 308 | sz = drr->drr_payloadlen; |
428870ff | 309 | |
f1512ee6 MA |
310 | if (sz > SPA_MAXBLOCKSIZE) { |
311 | buf = zfs_realloc(dda->dedup_hdl, buf, | |
312 | SPA_MAXBLOCKSIZE, sz); | |
428870ff BB |
313 | } |
314 | (void) ssread(buf, sz, ofp); | |
315 | if (ferror(stdin)) | |
316 | perror("fread"); | |
428870ff | 317 | } |
37f8a883 MA |
318 | if (dump_record(drr, buf, sz, &stream_cksum, |
319 | outfd) != 0) | |
320 | goto out; | |
428870ff BB |
321 | break; |
322 | } | |
323 | ||
324 | case DRR_END: | |
325 | { | |
37f8a883 | 326 | struct drr_end *drre = &drr->drr_u.drr_end; |
428870ff | 327 | /* use the recalculated checksum */ |
37f8a883 MA |
328 | drre->drr_checksum = stream_cksum; |
329 | if (dump_record(drr, NULL, 0, &stream_cksum, | |
330 | outfd) != 0) | |
428870ff BB |
331 | goto out; |
332 | break; | |
333 | } | |
334 | ||
335 | case DRR_OBJECT: | |
336 | { | |
37f8a883 | 337 | struct drr_object *drro = &drr->drr_u.drr_object; |
428870ff BB |
338 | if (drro->drr_bonuslen > 0) { |
339 | (void) ssread(buf, | |
b5256303 | 340 | DRR_OBJECT_PAYLOAD_SIZE(drro), ofp); |
428870ff | 341 | } |
b5256303 | 342 | if (dump_record(drr, buf, DRR_OBJECT_PAYLOAD_SIZE(drro), |
37f8a883 MA |
343 | &stream_cksum, outfd) != 0) |
344 | goto out; | |
428870ff BB |
345 | break; |
346 | } | |
347 | ||
348 | case DRR_SPILL: | |
349 | { | |
37f8a883 | 350 | struct drr_spill *drrs = &drr->drr_u.drr_spill; |
b5256303 TC |
351 | (void) ssread(buf, DRR_SPILL_PAYLOAD_SIZE(drrs), ofp); |
352 | if (dump_record(drr, buf, DRR_SPILL_PAYLOAD_SIZE(drrs), | |
37f8a883 | 353 | &stream_cksum, outfd) != 0) |
428870ff BB |
354 | goto out; |
355 | break; | |
356 | } | |
357 | ||
358 | case DRR_FREEOBJECTS: | |
359 | { | |
37f8a883 MA |
360 | if (dump_record(drr, NULL, 0, &stream_cksum, |
361 | outfd) != 0) | |
428870ff BB |
362 | goto out; |
363 | break; | |
364 | } | |
365 | ||
366 | case DRR_WRITE: | |
367 | { | |
37f8a883 | 368 | struct drr_write *drrw = &drr->drr_u.drr_write; |
428870ff | 369 | dataref_t dataref; |
2aa34383 | 370 | uint64_t payload_size; |
428870ff | 371 | |
2aa34383 DK |
372 | payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw); |
373 | (void) ssread(buf, payload_size, ofp); | |
428870ff BB |
374 | |
375 | /* | |
376 | * Use the existing checksum if it's dedup-capable, | |
377 | * else calculate a SHA256 checksum for it. | |
378 | */ | |
379 | ||
380 | if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum, | |
381 | zero_cksum) || | |
b5256303 | 382 | !DRR_IS_DEDUP_CAPABLE(drrw->drr_flags)) { |
a6255b7f | 383 | SHA2_CTX ctx; |
9c905c55 BB |
384 | zio_cksum_t tmpsha256; |
385 | ||
a6255b7f DQ |
386 | SHA2Init(SHA256, &ctx); |
387 | SHA2Update(&ctx, buf, payload_size); | |
388 | SHA2Final(&tmpsha256, &ctx); | |
428870ff | 389 | |
428870ff BB |
390 | drrw->drr_key.ddk_cksum.zc_word[0] = |
391 | BE_64(tmpsha256.zc_word[0]); | |
392 | drrw->drr_key.ddk_cksum.zc_word[1] = | |
393 | BE_64(tmpsha256.zc_word[1]); | |
394 | drrw->drr_key.ddk_cksum.zc_word[2] = | |
395 | BE_64(tmpsha256.zc_word[2]); | |
396 | drrw->drr_key.ddk_cksum.zc_word[3] = | |
397 | BE_64(tmpsha256.zc_word[3]); | |
398 | drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256; | |
b5256303 | 399 | drrw->drr_flags |= DRR_CHECKSUM_DEDUP; |
428870ff BB |
400 | } |
401 | ||
402 | dataref.ref_guid = drrw->drr_toguid; | |
403 | dataref.ref_object = drrw->drr_object; | |
404 | dataref.ref_offset = drrw->drr_offset; | |
405 | ||
406 | if (ddt_update(dda->dedup_hdl, &ddt, | |
407 | &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop, | |
408 | &dataref)) { | |
37f8a883 MA |
409 | dmu_replay_record_t wbr_drr = {0}; |
410 | struct drr_write_byref *wbr_drrr = | |
411 | &wbr_drr.drr_u.drr_write_byref; | |
412 | ||
428870ff | 413 | /* block already present in stream */ |
37f8a883 MA |
414 | wbr_drr.drr_type = DRR_WRITE_BYREF; |
415 | ||
428870ff BB |
416 | wbr_drrr->drr_object = drrw->drr_object; |
417 | wbr_drrr->drr_offset = drrw->drr_offset; | |
2aa34383 | 418 | wbr_drrr->drr_length = drrw->drr_logical_size; |
428870ff BB |
419 | wbr_drrr->drr_toguid = drrw->drr_toguid; |
420 | wbr_drrr->drr_refguid = dataref.ref_guid; | |
421 | wbr_drrr->drr_refobject = | |
422 | dataref.ref_object; | |
423 | wbr_drrr->drr_refoffset = | |
424 | dataref.ref_offset; | |
425 | ||
426 | wbr_drrr->drr_checksumtype = | |
427 | drrw->drr_checksumtype; | |
b5256303 | 428 | wbr_drrr->drr_flags = drrw->drr_flags; |
428870ff BB |
429 | wbr_drrr->drr_key.ddk_cksum = |
430 | drrw->drr_key.ddk_cksum; | |
431 | wbr_drrr->drr_key.ddk_prop = | |
432 | drrw->drr_key.ddk_prop; | |
433 | ||
37f8a883 MA |
434 | if (dump_record(&wbr_drr, NULL, 0, |
435 | &stream_cksum, outfd) != 0) | |
428870ff BB |
436 | goto out; |
437 | } else { | |
438 | /* block not previously seen */ | |
2aa34383 | 439 | if (dump_record(drr, buf, payload_size, |
37f8a883 | 440 | &stream_cksum, outfd) != 0) |
428870ff BB |
441 | goto out; |
442 | } | |
443 | break; | |
444 | } | |
445 | ||
9b67f605 MA |
446 | case DRR_WRITE_EMBEDDED: |
447 | { | |
37f8a883 MA |
448 | struct drr_write_embedded *drrwe = |
449 | &drr->drr_u.drr_write_embedded; | |
9b67f605 MA |
450 | (void) ssread(buf, |
451 | P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp); | |
37f8a883 | 452 | if (dump_record(drr, buf, |
9b67f605 | 453 | P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), |
37f8a883 | 454 | &stream_cksum, outfd) != 0) |
9b67f605 MA |
455 | goto out; |
456 | break; | |
457 | } | |
458 | ||
428870ff BB |
459 | case DRR_FREE: |
460 | { | |
37f8a883 MA |
461 | if (dump_record(drr, NULL, 0, &stream_cksum, |
462 | outfd) != 0) | |
428870ff BB |
463 | goto out; |
464 | break; | |
465 | } | |
466 | ||
b5256303 TC |
467 | case DRR_OBJECT_RANGE: |
468 | { | |
469 | if (dump_record(drr, NULL, 0, &stream_cksum, | |
470 | outfd) != 0) | |
471 | goto out; | |
472 | break; | |
473 | } | |
474 | ||
428870ff | 475 | default: |
37f8a883 | 476 | (void) fprintf(stderr, "INVALID record type 0x%x\n", |
428870ff BB |
477 | drr->drr_type); |
478 | /* should never happen, so assert */ | |
479 | assert(B_FALSE); | |
480 | } | |
481 | } | |
482 | out: | |
483 | umem_cache_destroy(ddt.ddecache); | |
484 | free(ddt.dedup_hash_array); | |
485 | free(buf); | |
486 | (void) fclose(ofp); | |
487 | ||
488 | return (NULL); | |
489 | } | |
b128c09f | 490 | |
34dc7c2f BB |
491 | /* |
492 | * Routines for dealing with the AVL tree of fs-nvlists | |
493 | */ | |
494 | typedef struct fsavl_node { | |
495 | avl_node_t fn_node; | |
496 | nvlist_t *fn_nvfs; | |
497 | char *fn_snapname; | |
498 | uint64_t fn_guid; | |
499 | } fsavl_node_t; | |
500 | ||
501 | static int | |
502 | fsavl_compare(const void *arg1, const void *arg2) | |
503 | { | |
ee36c709 GN |
504 | const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1; |
505 | const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2; | |
506 | ||
507 | return (AVL_CMP(fn1->fn_guid, fn2->fn_guid)); | |
34dc7c2f BB |
508 | } |
509 | ||
510 | /* | |
511 | * Given the GUID of a snapshot, find its containing filesystem and | |
512 | * (optionally) name. | |
513 | */ | |
514 | static nvlist_t * | |
515 | fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname) | |
516 | { | |
517 | fsavl_node_t fn_find; | |
518 | fsavl_node_t *fn; | |
519 | ||
520 | fn_find.fn_guid = snapguid; | |
521 | ||
522 | fn = avl_find(avl, &fn_find, NULL); | |
523 | if (fn) { | |
524 | if (snapname) | |
525 | *snapname = fn->fn_snapname; | |
526 | return (fn->fn_nvfs); | |
527 | } | |
528 | return (NULL); | |
529 | } | |
530 | ||
531 | static void | |
532 | fsavl_destroy(avl_tree_t *avl) | |
533 | { | |
534 | fsavl_node_t *fn; | |
535 | void *cookie; | |
536 | ||
537 | if (avl == NULL) | |
538 | return; | |
539 | ||
540 | cookie = NULL; | |
541 | while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL) | |
542 | free(fn); | |
543 | avl_destroy(avl); | |
544 | free(avl); | |
545 | } | |
546 | ||
45d1cae3 BB |
547 | /* |
548 | * Given an nvlist, produce an avl tree of snapshots, ordered by guid | |
549 | */ | |
34dc7c2f BB |
550 | static avl_tree_t * |
551 | fsavl_create(nvlist_t *fss) | |
552 | { | |
553 | avl_tree_t *fsavl; | |
554 | nvpair_t *fselem = NULL; | |
555 | ||
556 | if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL) | |
557 | return (NULL); | |
558 | ||
559 | avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t), | |
560 | offsetof(fsavl_node_t, fn_node)); | |
561 | ||
562 | while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) { | |
563 | nvlist_t *nvfs, *snaps; | |
564 | nvpair_t *snapelem = NULL; | |
565 | ||
566 | VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); | |
567 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); | |
568 | ||
569 | while ((snapelem = | |
570 | nvlist_next_nvpair(snaps, snapelem)) != NULL) { | |
571 | fsavl_node_t *fn; | |
572 | uint64_t guid; | |
573 | ||
574 | VERIFY(0 == nvpair_value_uint64(snapelem, &guid)); | |
575 | if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) { | |
576 | fsavl_destroy(fsavl); | |
577 | return (NULL); | |
578 | } | |
579 | fn->fn_nvfs = nvfs; | |
580 | fn->fn_snapname = nvpair_name(snapelem); | |
581 | fn->fn_guid = guid; | |
582 | ||
583 | /* | |
584 | * Note: if there are multiple snaps with the | |
585 | * same GUID, we ignore all but one. | |
586 | */ | |
587 | if (avl_find(fsavl, fn, NULL) == NULL) | |
588 | avl_add(fsavl, fn); | |
589 | else | |
590 | free(fn); | |
591 | } | |
592 | } | |
593 | ||
594 | return (fsavl); | |
595 | } | |
596 | ||
597 | /* | |
598 | * Routines for dealing with the giant nvlist of fs-nvlists, etc. | |
599 | */ | |
600 | typedef struct send_data { | |
66356240 K |
601 | /* |
602 | * assigned inside every recursive call, | |
603 | * restored from *_save on return: | |
604 | * | |
605 | * guid of fromsnap snapshot in parent dataset | |
606 | * txg of fromsnap snapshot in current dataset | |
607 | * txg of tosnap snapshot in current dataset | |
608 | */ | |
609 | ||
34dc7c2f | 610 | uint64_t parent_fromsnap_guid; |
66356240 K |
611 | uint64_t fromsnap_txg; |
612 | uint64_t tosnap_txg; | |
613 | ||
614 | /* the nvlists get accumulated during depth-first traversal */ | |
34dc7c2f BB |
615 | nvlist_t *parent_snaps; |
616 | nvlist_t *fss; | |
b128c09f | 617 | nvlist_t *snapprops; |
66356240 K |
618 | |
619 | /* send-receive configuration, does not change during traversal */ | |
620 | const char *fsname; | |
34dc7c2f BB |
621 | const char *fromsnap; |
622 | const char *tosnap; | |
b5256303 | 623 | boolean_t raw; |
428870ff | 624 | boolean_t recursive; |
66356240 | 625 | boolean_t verbose; |
05748550 AG |
626 | boolean_t seenfrom; |
627 | boolean_t seento; | |
34dc7c2f BB |
628 | |
629 | /* | |
630 | * The header nvlist is of the following format: | |
631 | * { | |
632 | * "tosnap" -> string | |
633 | * "fromsnap" -> string (if incremental) | |
634 | * "fss" -> { | |
635 | * id -> { | |
636 | * | |
637 | * "name" -> string (full name; for debugging) | |
638 | * "parentfromsnap" -> number (guid of fromsnap in parent) | |
639 | * | |
640 | * "props" -> { name -> value (only if set here) } | |
641 | * "snaps" -> { name (lastname) -> number (guid) } | |
b128c09f | 642 | * "snapprops" -> { name (lastname) -> { name -> value } } |
34dc7c2f BB |
643 | * |
644 | * "origin" -> number (guid) (if clone) | |
b5256303 | 645 | * "is_encroot" -> boolean |
34dc7c2f BB |
646 | * "sent" -> boolean (not on-disk) |
647 | * } | |
648 | * } | |
649 | * } | |
650 | * | |
651 | */ | |
652 | } send_data_t; | |
653 | ||
b128c09f BB |
654 | static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv); |
655 | ||
34dc7c2f BB |
656 | static int |
657 | send_iterate_snap(zfs_handle_t *zhp, void *arg) | |
658 | { | |
659 | send_data_t *sd = arg; | |
660 | uint64_t guid = zhp->zfs_dmustats.dds_guid; | |
66356240 | 661 | uint64_t txg = zhp->zfs_dmustats.dds_creation_txg; |
34dc7c2f | 662 | char *snapname; |
b128c09f | 663 | nvlist_t *nv; |
e890dd85 | 664 | boolean_t isfromsnap, istosnap, istosnapwithnofrom; |
34dc7c2f BB |
665 | |
666 | snapname = strrchr(zhp->zfs_name, '@')+1; | |
05748550 AG |
667 | isfromsnap = (sd->fromsnap != NULL && |
668 | strcmp(sd->fromsnap, snapname) == 0); | |
669 | istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0)); | |
e890dd85 | 670 | istosnapwithnofrom = (istosnap && sd->fromsnap == NULL); |
34dc7c2f | 671 | |
66356240 K |
672 | if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) { |
673 | if (sd->verbose) { | |
674 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
675 | "skipping snapshot %s because it was created " | |
676 | "after the destination snapshot (%s)\n"), | |
677 | zhp->zfs_name, sd->tosnap); | |
678 | } | |
679 | zfs_close(zhp); | |
680 | return (0); | |
681 | } | |
682 | ||
e890dd85 | 683 | VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid)); |
34dc7c2f BB |
684 | /* |
685 | * NB: if there is no fromsnap here (it's a newly created fs in | |
686 | * an incremental replication), we will substitute the tosnap. | |
687 | */ | |
05748550 | 688 | if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) { |
34dc7c2f BB |
689 | sd->parent_fromsnap_guid = guid; |
690 | } | |
691 | ||
05748550 AG |
692 | if (!sd->recursive) { |
693 | if (!sd->seenfrom && isfromsnap) { | |
694 | sd->seenfrom = B_TRUE; | |
695 | zfs_close(zhp); | |
696 | return (0); | |
697 | } | |
698 | ||
e890dd85 | 699 | if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) { |
05748550 AG |
700 | zfs_close(zhp); |
701 | return (0); | |
702 | } | |
703 | ||
704 | if (istosnap) | |
705 | sd->seento = B_TRUE; | |
706 | } | |
707 | ||
b128c09f BB |
708 | VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); |
709 | send_iterate_prop(zhp, nv); | |
710 | VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv)); | |
711 | nvlist_free(nv); | |
712 | ||
34dc7c2f BB |
713 | zfs_close(zhp); |
714 | return (0); | |
715 | } | |
716 | ||
717 | static void | |
718 | send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv) | |
719 | { | |
720 | nvpair_t *elem = NULL; | |
721 | ||
722 | while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) { | |
723 | char *propname = nvpair_name(elem); | |
724 | zfs_prop_t prop = zfs_name_to_prop(propname); | |
725 | nvlist_t *propnv; | |
726 | ||
428870ff BB |
727 | if (!zfs_prop_user(propname)) { |
728 | /* | |
729 | * Realistically, this should never happen. However, | |
730 | * we want the ability to add DSL properties without | |
731 | * needing to make incompatible version changes. We | |
732 | * need to ignore unknown properties to allow older | |
733 | * software to still send datasets containing these | |
734 | * properties, with the unknown properties elided. | |
735 | */ | |
736 | if (prop == ZPROP_INVAL) | |
737 | continue; | |
9babb374 | 738 | |
428870ff BB |
739 | if (zfs_prop_readonly(prop)) |
740 | continue; | |
741 | } | |
34dc7c2f BB |
742 | |
743 | verify(nvpair_value_nvlist(elem, &propnv) == 0); | |
45d1cae3 BB |
744 | if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION || |
745 | prop == ZFS_PROP_REFQUOTA || | |
746 | prop == ZFS_PROP_REFRESERVATION) { | |
428870ff | 747 | char *source; |
34dc7c2f BB |
748 | uint64_t value; |
749 | verify(nvlist_lookup_uint64(propnv, | |
750 | ZPROP_VALUE, &value) == 0); | |
b128c09f BB |
751 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) |
752 | continue; | |
428870ff BB |
753 | /* |
754 | * May have no source before SPA_VERSION_RECVD_PROPS, | |
755 | * but is still modifiable. | |
756 | */ | |
757 | if (nvlist_lookup_string(propnv, | |
758 | ZPROP_SOURCE, &source) == 0) { | |
759 | if ((strcmp(source, zhp->zfs_name) != 0) && | |
760 | (strcmp(source, | |
761 | ZPROP_SOURCE_VAL_RECVD) != 0)) | |
762 | continue; | |
763 | } | |
34dc7c2f BB |
764 | } else { |
765 | char *source; | |
766 | if (nvlist_lookup_string(propnv, | |
767 | ZPROP_SOURCE, &source) != 0) | |
768 | continue; | |
428870ff BB |
769 | if ((strcmp(source, zhp->zfs_name) != 0) && |
770 | (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)) | |
34dc7c2f BB |
771 | continue; |
772 | } | |
773 | ||
774 | if (zfs_prop_user(propname) || | |
775 | zfs_prop_get_type(prop) == PROP_TYPE_STRING) { | |
776 | char *value; | |
777 | verify(nvlist_lookup_string(propnv, | |
778 | ZPROP_VALUE, &value) == 0); | |
779 | VERIFY(0 == nvlist_add_string(nv, propname, value)); | |
780 | } else { | |
781 | uint64_t value; | |
782 | verify(nvlist_lookup_uint64(propnv, | |
783 | ZPROP_VALUE, &value) == 0); | |
784 | VERIFY(0 == nvlist_add_uint64(nv, propname, value)); | |
785 | } | |
786 | } | |
787 | } | |
788 | ||
66356240 K |
789 | /* |
790 | * returns snapshot creation txg | |
791 | * and returns 0 if the snapshot does not exist | |
792 | */ | |
793 | static uint64_t | |
794 | get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap) | |
795 | { | |
796 | char name[ZFS_MAX_DATASET_NAME_LEN]; | |
797 | uint64_t txg = 0; | |
798 | ||
799 | if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0') | |
800 | return (txg); | |
801 | ||
802 | (void) snprintf(name, sizeof (name), "%s@%s", fs, snap); | |
803 | if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) { | |
804 | zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT); | |
805 | if (zhp != NULL) { | |
806 | txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG); | |
807 | zfs_close(zhp); | |
808 | } | |
809 | } | |
810 | ||
811 | return (txg); | |
812 | } | |
813 | ||
45d1cae3 BB |
814 | /* |
815 | * recursively generate nvlists describing datasets. See comment | |
816 | * for the data structure send_data_t above for description of contents | |
817 | * of the nvlist. | |
818 | */ | |
34dc7c2f BB |
819 | static int |
820 | send_iterate_fs(zfs_handle_t *zhp, void *arg) | |
821 | { | |
822 | send_data_t *sd = arg; | |
b5256303 | 823 | nvlist_t *nvfs = NULL, *nv = NULL; |
428870ff | 824 | int rv = 0; |
34dc7c2f | 825 | uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid; |
66356240 K |
826 | uint64_t fromsnap_txg_save = sd->fromsnap_txg; |
827 | uint64_t tosnap_txg_save = sd->tosnap_txg; | |
828 | uint64_t txg = zhp->zfs_dmustats.dds_creation_txg; | |
34dc7c2f | 829 | uint64_t guid = zhp->zfs_dmustats.dds_guid; |
66356240 | 830 | uint64_t fromsnap_txg, tosnap_txg; |
34dc7c2f BB |
831 | char guidstring[64]; |
832 | ||
66356240 K |
833 | fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap); |
834 | if (fromsnap_txg != 0) | |
835 | sd->fromsnap_txg = fromsnap_txg; | |
836 | ||
837 | tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap); | |
838 | if (tosnap_txg != 0) | |
839 | sd->tosnap_txg = tosnap_txg; | |
840 | ||
841 | /* | |
842 | * on the send side, if the current dataset does not have tosnap, | |
843 | * perform two additional checks: | |
844 | * | |
845 | * - skip sending the current dataset if it was created later than | |
846 | * the parent tosnap | |
847 | * - return error if the current dataset was created earlier than | |
848 | * the parent tosnap | |
849 | */ | |
850 | if (sd->tosnap != NULL && tosnap_txg == 0) { | |
851 | if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) { | |
852 | if (sd->verbose) { | |
853 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
854 | "skipping dataset %s: snapshot %s does " | |
855 | "not exist\n"), zhp->zfs_name, sd->tosnap); | |
856 | } | |
857 | } else { | |
858 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
859 | "cannot send %s@%s%s: snapshot %s@%s does not " | |
860 | "exist\n"), sd->fsname, sd->tosnap, sd->recursive ? | |
861 | dgettext(TEXT_DOMAIN, " recursively") : "", | |
862 | zhp->zfs_name, sd->tosnap); | |
863 | rv = -1; | |
864 | } | |
865 | goto out; | |
866 | } | |
867 | ||
34dc7c2f BB |
868 | VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0)); |
869 | VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name)); | |
870 | VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap", | |
871 | sd->parent_fromsnap_guid)); | |
872 | ||
873 | if (zhp->zfs_dmustats.dds_origin[0]) { | |
874 | zfs_handle_t *origin = zfs_open(zhp->zfs_hdl, | |
875 | zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); | |
66356240 K |
876 | if (origin == NULL) { |
877 | rv = -1; | |
878 | goto out; | |
879 | } | |
34dc7c2f BB |
880 | VERIFY(0 == nvlist_add_uint64(nvfs, "origin", |
881 | origin->zfs_dmustats.dds_guid)); | |
ad7e908a TC |
882 | |
883 | zfs_close(origin); | |
34dc7c2f BB |
884 | } |
885 | ||
886 | /* iterate over props */ | |
887 | VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); | |
888 | send_iterate_prop(zhp, nv); | |
b5256303 TC |
889 | |
890 | if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) { | |
891 | boolean_t encroot; | |
892 | ||
893 | /* determine if this dataset is an encryption root */ | |
894 | if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) { | |
895 | rv = -1; | |
896 | goto out; | |
897 | } | |
898 | ||
899 | if (encroot) | |
900 | VERIFY(0 == nvlist_add_boolean(nvfs, "is_encroot")); | |
901 | ||
902 | /* | |
903 | * Encrypted datasets can only be sent with properties if | |
904 | * the raw flag is specified because the receive side doesn't | |
905 | * currently have a mechanism for recursively asking the user | |
906 | * for new encryption parameters. | |
907 | */ | |
908 | if (!sd->raw) { | |
909 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
910 | "cannot send %s@%s: encrypted dataset %s may not " | |
911 | "be sent with properties without the raw flag\n"), | |
912 | sd->fsname, sd->tosnap, zhp->zfs_name); | |
913 | rv = -1; | |
914 | goto out; | |
915 | } | |
916 | ||
917 | } | |
918 | ||
34dc7c2f | 919 | VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv)); |
34dc7c2f BB |
920 | |
921 | /* iterate over snaps, and set sd->parent_fromsnap_guid */ | |
922 | sd->parent_fromsnap_guid = 0; | |
923 | VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0)); | |
b128c09f | 924 | VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0)); |
7509a3d2 | 925 | (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd); |
34dc7c2f | 926 | VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps)); |
b128c09f | 927 | VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops)); |
34dc7c2f | 928 | nvlist_free(sd->parent_snaps); |
b128c09f | 929 | nvlist_free(sd->snapprops); |
34dc7c2f BB |
930 | |
931 | /* add this fs to nvlist */ | |
932 | (void) snprintf(guidstring, sizeof (guidstring), | |
933 | "0x%llx", (longlong_t)guid); | |
934 | VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs)); | |
34dc7c2f BB |
935 | |
936 | /* iterate over children */ | |
428870ff BB |
937 | if (sd->recursive) |
938 | rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd); | |
34dc7c2f | 939 | |
66356240 | 940 | out: |
34dc7c2f | 941 | sd->parent_fromsnap_guid = parent_fromsnap_guid_save; |
66356240 K |
942 | sd->fromsnap_txg = fromsnap_txg_save; |
943 | sd->tosnap_txg = tosnap_txg_save; | |
b5256303 TC |
944 | nvlist_free(nv); |
945 | nvlist_free(nvfs); | |
34dc7c2f BB |
946 | |
947 | zfs_close(zhp); | |
948 | return (rv); | |
949 | } | |
950 | ||
951 | static int | |
952 | gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap, | |
b5256303 | 953 | const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t verbose, |
66356240 | 954 | nvlist_t **nvlp, avl_tree_t **avlp) |
34dc7c2f BB |
955 | { |
956 | zfs_handle_t *zhp; | |
957 | send_data_t sd = { 0 }; | |
958 | int error; | |
959 | ||
960 | zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); | |
961 | if (zhp == NULL) | |
962 | return (EZFS_BADTYPE); | |
963 | ||
964 | VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0)); | |
66356240 | 965 | sd.fsname = fsname; |
34dc7c2f BB |
966 | sd.fromsnap = fromsnap; |
967 | sd.tosnap = tosnap; | |
428870ff | 968 | sd.recursive = recursive; |
b5256303 | 969 | sd.raw = raw; |
66356240 | 970 | sd.verbose = verbose; |
34dc7c2f BB |
971 | |
972 | if ((error = send_iterate_fs(zhp, &sd)) != 0) { | |
973 | nvlist_free(sd.fss); | |
974 | if (avlp != NULL) | |
975 | *avlp = NULL; | |
976 | *nvlp = NULL; | |
977 | return (error); | |
978 | } | |
979 | ||
980 | if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) { | |
981 | nvlist_free(sd.fss); | |
982 | *nvlp = NULL; | |
983 | return (EZFS_NOMEM); | |
984 | } | |
985 | ||
986 | *nvlp = sd.fss; | |
987 | return (0); | |
988 | } | |
989 | ||
34dc7c2f BB |
990 | /* |
991 | * Routines specific to "zfs send" | |
992 | */ | |
993 | typedef struct send_dump_data { | |
994 | /* these are all just the short snapname (the part after the @) */ | |
995 | const char *fromsnap; | |
996 | const char *tosnap; | |
eca7b760 | 997 | char prevsnap[ZFS_MAX_DATASET_NAME_LEN]; |
572e2857 | 998 | uint64_t prevsnap_obj; |
34dc7c2f | 999 | boolean_t seenfrom, seento, replicate, doall, fromorigin; |
93f6d7e2 | 1000 | boolean_t verbose, dryrun, parsable, progress, embed_data, std_out; |
b5256303 | 1001 | boolean_t large_block, compress, raw; |
34dc7c2f BB |
1002 | int outfd; |
1003 | boolean_t err; | |
1004 | nvlist_t *fss; | |
95fd54a1 | 1005 | nvlist_t *snapholds; |
34dc7c2f | 1006 | avl_tree_t *fsavl; |
428870ff BB |
1007 | snapfilter_cb_t *filter_cb; |
1008 | void *filter_cb_arg; | |
1009 | nvlist_t *debugnv; | |
eca7b760 | 1010 | char holdtag[ZFS_MAX_DATASET_NAME_LEN]; |
572e2857 | 1011 | int cleanup_fd; |
330d06f9 | 1012 | uint64_t size; |
34dc7c2f BB |
1013 | } send_dump_data_t; |
1014 | ||
330d06f9 | 1015 | static int |
cf7684bc | 1016 | zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from, |
1017 | enum lzc_send_flags flags, uint64_t *spacep) | |
330d06f9 | 1018 | { |
330d06f9 | 1019 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
cf7684bc | 1020 | int error; |
330d06f9 | 1021 | |
cf7684bc | 1022 | assert(snapname != NULL); |
1023 | error = lzc_send_space(snapname, from, flags, spacep); | |
330d06f9 | 1024 | |
cf7684bc | 1025 | if (error != 0) { |
330d06f9 MA |
1026 | char errbuf[1024]; |
1027 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
cf7684bc | 1028 | "warning: cannot estimate space for '%s'"), snapname); |
330d06f9 | 1029 | |
cf7684bc | 1030 | switch (error) { |
330d06f9 MA |
1031 | case EXDEV: |
1032 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1033 | "not an earlier snapshot from the same fs")); | |
1034 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
1035 | ||
1036 | case ENOENT: | |
cf7684bc | 1037 | if (zfs_dataset_exists(hdl, snapname, |
330d06f9 MA |
1038 | ZFS_TYPE_SNAPSHOT)) { |
1039 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
cf7684bc | 1040 | "incremental source (%s) does not exist"), |
1041 | snapname); | |
330d06f9 MA |
1042 | } |
1043 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
1044 | ||
1045 | case EDQUOT: | |
1046 | case EFBIG: | |
1047 | case EIO: | |
1048 | case ENOLINK: | |
1049 | case ENOSPC: | |
1050 | case ENOSTR: | |
1051 | case ENXIO: | |
1052 | case EPIPE: | |
1053 | case ERANGE: | |
1054 | case EFAULT: | |
1055 | case EROFS: | |
cf7684bc | 1056 | case EINVAL: |
1057 | zfs_error_aux(hdl, strerror(error)); | |
330d06f9 MA |
1058 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); |
1059 | ||
1060 | default: | |
cf7684bc | 1061 | return (zfs_standard_error(hdl, error, errbuf)); |
330d06f9 MA |
1062 | } |
1063 | } | |
1064 | ||
330d06f9 MA |
1065 | return (0); |
1066 | } | |
1067 | ||
34dc7c2f BB |
1068 | /* |
1069 | * Dumps a backup of the given snapshot (incremental from fromsnap if it's not | |
1070 | * NULL) to the file descriptor specified by outfd. | |
1071 | */ | |
1072 | static int | |
572e2857 | 1073 | dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj, |
9b67f605 MA |
1074 | boolean_t fromorigin, int outfd, enum lzc_send_flags flags, |
1075 | nvlist_t *debugnv) | |
34dc7c2f | 1076 | { |
13fe0198 | 1077 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f | 1078 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
428870ff | 1079 | nvlist_t *thisdbg; |
34dc7c2f BB |
1080 | |
1081 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); | |
572e2857 | 1082 | assert(fromsnap_obj == 0 || !fromorigin); |
34dc7c2f BB |
1083 | |
1084 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
34dc7c2f BB |
1085 | zc.zc_cookie = outfd; |
1086 | zc.zc_obj = fromorigin; | |
572e2857 BB |
1087 | zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); |
1088 | zc.zc_fromobj = fromsnap_obj; | |
9b67f605 | 1089 | zc.zc_flags = flags; |
428870ff BB |
1090 | |
1091 | VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0)); | |
1092 | if (fromsnap && fromsnap[0] != '\0') { | |
1093 | VERIFY(0 == nvlist_add_string(thisdbg, | |
1094 | "fromsnap", fromsnap)); | |
1095 | } | |
1096 | ||
330d06f9 | 1097 | if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) { |
34dc7c2f BB |
1098 | char errbuf[1024]; |
1099 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
1100 | "warning: cannot send '%s'"), zhp->zfs_name); | |
1101 | ||
428870ff BB |
1102 | VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno)); |
1103 | if (debugnv) { | |
1104 | VERIFY(0 == nvlist_add_nvlist(debugnv, | |
1105 | zhp->zfs_name, thisdbg)); | |
1106 | } | |
1107 | nvlist_free(thisdbg); | |
1108 | ||
34dc7c2f | 1109 | switch (errno) { |
34dc7c2f BB |
1110 | case EXDEV: |
1111 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1112 | "not an earlier snapshot from the same fs")); | |
1113 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
1114 | ||
b5256303 TC |
1115 | case EACCES: |
1116 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1117 | "source key must be loaded")); | |
1118 | return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); | |
1119 | ||
34dc7c2f BB |
1120 | case ENOENT: |
1121 | if (zfs_dataset_exists(hdl, zc.zc_name, | |
1122 | ZFS_TYPE_SNAPSHOT)) { | |
1123 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1124 | "incremental source (@%s) does not exist"), | |
1125 | zc.zc_value); | |
1126 | } | |
1127 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
1128 | ||
1129 | case EDQUOT: | |
1130 | case EFBIG: | |
1131 | case EIO: | |
1132 | case ENOLINK: | |
1133 | case ENOSPC: | |
1134 | case ENOSTR: | |
1135 | case ENXIO: | |
1136 | case EPIPE: | |
1137 | case ERANGE: | |
1138 | case EFAULT: | |
1139 | case EROFS: | |
1140 | zfs_error_aux(hdl, strerror(errno)); | |
1141 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
1142 | ||
1143 | default: | |
1144 | return (zfs_standard_error(hdl, errno, errbuf)); | |
1145 | } | |
1146 | } | |
1147 | ||
428870ff BB |
1148 | if (debugnv) |
1149 | VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg)); | |
1150 | nvlist_free(thisdbg); | |
1151 | ||
34dc7c2f BB |
1152 | return (0); |
1153 | } | |
1154 | ||
95fd54a1 SH |
1155 | static void |
1156 | gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd) | |
572e2857 | 1157 | { |
572e2857 BB |
1158 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); |
1159 | ||
1160 | /* | |
95fd54a1 | 1161 | * zfs_send() only sets snapholds for sends that need them, |
572e2857 BB |
1162 | * e.g. replication and doall. |
1163 | */ | |
95fd54a1 SH |
1164 | if (sdd->snapholds == NULL) |
1165 | return; | |
572e2857 | 1166 | |
95fd54a1 | 1167 | fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag); |
572e2857 BB |
1168 | } |
1169 | ||
37abac6d BP |
1170 | static void * |
1171 | send_progress_thread(void *arg) | |
1172 | { | |
1173 | progress_arg_t *pa = arg; | |
13fe0198 | 1174 | zfs_cmd_t zc = {"\0"}; |
37abac6d BP |
1175 | zfs_handle_t *zhp = pa->pa_zhp; |
1176 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
1177 | unsigned long long bytes; | |
1178 | char buf[16]; | |
37abac6d BP |
1179 | time_t t; |
1180 | struct tm *tm; | |
1181 | ||
37abac6d BP |
1182 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
1183 | ||
1184 | if (!pa->pa_parsable) | |
1185 | (void) fprintf(stderr, "TIME SENT SNAPSHOT\n"); | |
1186 | ||
1187 | /* | |
1188 | * Print the progress from ZFS_IOC_SEND_PROGRESS every second. | |
1189 | */ | |
1190 | for (;;) { | |
1191 | (void) sleep(1); | |
1192 | ||
1193 | zc.zc_cookie = pa->pa_fd; | |
1194 | if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0) | |
1195 | return ((void *)-1); | |
1196 | ||
1197 | (void) time(&t); | |
1198 | tm = localtime(&t); | |
1199 | bytes = zc.zc_cookie; | |
1200 | ||
1201 | if (pa->pa_parsable) { | |
1202 | (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n", | |
1203 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
1204 | bytes, zhp->zfs_name); | |
1205 | } else { | |
e7fbeb60 | 1206 | zfs_nicebytes(bytes, buf, sizeof (buf)); |
37abac6d BP |
1207 | (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n", |
1208 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
1209 | buf, zhp->zfs_name); | |
1210 | } | |
1211 | } | |
1212 | } | |
1213 | ||
47dfff3b MA |
1214 | static void |
1215 | send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap, | |
1216 | uint64_t size, boolean_t parsable) | |
1217 | { | |
1218 | if (parsable) { | |
1219 | if (fromsnap != NULL) { | |
1220 | (void) fprintf(fout, "incremental\t%s\t%s", | |
1221 | fromsnap, tosnap); | |
1222 | } else { | |
1223 | (void) fprintf(fout, "full\t%s", | |
1224 | tosnap); | |
1225 | } | |
1226 | } else { | |
1227 | if (fromsnap != NULL) { | |
1228 | if (strchr(fromsnap, '@') == NULL && | |
1229 | strchr(fromsnap, '#') == NULL) { | |
1230 | (void) fprintf(fout, dgettext(TEXT_DOMAIN, | |
1231 | "send from @%s to %s"), | |
1232 | fromsnap, tosnap); | |
1233 | } else { | |
1234 | (void) fprintf(fout, dgettext(TEXT_DOMAIN, | |
1235 | "send from %s to %s"), | |
1236 | fromsnap, tosnap); | |
1237 | } | |
1238 | } else { | |
1239 | (void) fprintf(fout, dgettext(TEXT_DOMAIN, | |
1240 | "full send of %s"), | |
1241 | tosnap); | |
1242 | } | |
1243 | } | |
1244 | ||
835db585 | 1245 | if (parsable) { |
1246 | (void) fprintf(fout, "\t%llu", | |
1247 | (longlong_t)size); | |
1248 | } else if (size != 0) { | |
1249 | char buf[16]; | |
1250 | zfs_nicebytes(size, buf, sizeof (buf)); | |
1251 | (void) fprintf(fout, dgettext(TEXT_DOMAIN, | |
1252 | " estimated size is %s"), buf); | |
47dfff3b MA |
1253 | } |
1254 | (void) fprintf(fout, "\n"); | |
1255 | } | |
1256 | ||
34dc7c2f BB |
1257 | static int |
1258 | dump_snapshot(zfs_handle_t *zhp, void *arg) | |
1259 | { | |
1260 | send_dump_data_t *sdd = arg; | |
37abac6d BP |
1261 | progress_arg_t pa = { 0 }; |
1262 | pthread_t tid; | |
572e2857 | 1263 | char *thissnap; |
2aa34383 | 1264 | enum lzc_send_flags flags = 0; |
34dc7c2f | 1265 | int err; |
330d06f9 | 1266 | boolean_t isfromsnap, istosnap, fromorigin; |
428870ff | 1267 | boolean_t exclude = B_FALSE; |
93f6d7e2 | 1268 | FILE *fout = sdd->std_out ? stdout : stderr; |
34dc7c2f | 1269 | |
95fd54a1 | 1270 | err = 0; |
34dc7c2f | 1271 | thissnap = strchr(zhp->zfs_name, '@') + 1; |
428870ff BB |
1272 | isfromsnap = (sdd->fromsnap != NULL && |
1273 | strcmp(sdd->fromsnap, thissnap) == 0); | |
34dc7c2f | 1274 | |
428870ff | 1275 | if (!sdd->seenfrom && isfromsnap) { |
95fd54a1 SH |
1276 | gather_holds(zhp, sdd); |
1277 | sdd->seenfrom = B_TRUE; | |
c9d61adb | 1278 | (void) strlcpy(sdd->prevsnap, thissnap, |
1279 | sizeof (sdd->prevsnap)); | |
95fd54a1 | 1280 | sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); |
34dc7c2f | 1281 | zfs_close(zhp); |
95fd54a1 | 1282 | return (0); |
34dc7c2f BB |
1283 | } |
1284 | ||
1285 | if (sdd->seento || !sdd->seenfrom) { | |
1286 | zfs_close(zhp); | |
1287 | return (0); | |
1288 | } | |
1289 | ||
428870ff BB |
1290 | istosnap = (strcmp(sdd->tosnap, thissnap) == 0); |
1291 | if (istosnap) | |
1292 | sdd->seento = B_TRUE; | |
1293 | ||
2aa34383 DK |
1294 | if (sdd->large_block) |
1295 | flags |= LZC_SEND_FLAG_LARGE_BLOCK; | |
1296 | if (sdd->embed_data) | |
1297 | flags |= LZC_SEND_FLAG_EMBED_DATA; | |
1298 | if (sdd->compress) | |
1299 | flags |= LZC_SEND_FLAG_COMPRESS; | |
b5256303 TC |
1300 | if (sdd->raw) |
1301 | flags |= LZC_SEND_FLAG_RAW; | |
2aa34383 | 1302 | |
428870ff BB |
1303 | if (!sdd->doall && !isfromsnap && !istosnap) { |
1304 | if (sdd->replicate) { | |
1305 | char *snapname; | |
1306 | nvlist_t *snapprops; | |
1307 | /* | |
1308 | * Filter out all intermediate snapshots except origin | |
1309 | * snapshots needed to replicate clones. | |
1310 | */ | |
1311 | nvlist_t *nvfs = fsavl_find(sdd->fsavl, | |
1312 | zhp->zfs_dmustats.dds_guid, &snapname); | |
1313 | ||
1314 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, | |
1315 | "snapprops", &snapprops)); | |
1316 | VERIFY(0 == nvlist_lookup_nvlist(snapprops, | |
1317 | thissnap, &snapprops)); | |
1318 | exclude = !nvlist_exists(snapprops, "is_clone_origin"); | |
1319 | } else { | |
1320 | exclude = B_TRUE; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | /* | |
1325 | * If a filter function exists, call it to determine whether | |
1326 | * this snapshot will be sent. | |
1327 | */ | |
1328 | if (exclude || (sdd->filter_cb != NULL && | |
1329 | sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) { | |
1330 | /* | |
1331 | * This snapshot is filtered out. Don't send it, and don't | |
572e2857 | 1332 | * set prevsnap_obj, so it will be as if this snapshot didn't |
428870ff BB |
1333 | * exist, and the next accepted snapshot will be sent as |
1334 | * an incremental from the last accepted one, or as the | |
1335 | * first (and full) snapshot in the case of a replication, | |
1336 | * non-incremental send. | |
1337 | */ | |
1338 | zfs_close(zhp); | |
1339 | return (0); | |
1340 | } | |
1341 | ||
95fd54a1 | 1342 | gather_holds(zhp, sdd); |
330d06f9 MA |
1343 | fromorigin = sdd->prevsnap[0] == '\0' && |
1344 | (sdd->fromorigin || sdd->replicate); | |
1345 | ||
34dc7c2f | 1346 | if (sdd->verbose) { |
47dfff3b | 1347 | uint64_t size = 0; |
cf7684bc | 1348 | char fromds[ZFS_MAX_DATASET_NAME_LEN]; |
330d06f9 | 1349 | |
cf7684bc | 1350 | if (sdd->prevsnap[0] != '\0') { |
1351 | (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds)); | |
1352 | *(strchr(fromds, '@') + 1) = '\0'; | |
1353 | (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds)); | |
1354 | } | |
1355 | if (zfs_send_space(zhp, zhp->zfs_name, | |
1356 | sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) { | |
1357 | size = 0; /* cannot estimate send space */ | |
1358 | } else { | |
1359 | send_print_verbose(fout, zhp->zfs_name, | |
1360 | sdd->prevsnap[0] ? sdd->prevsnap : NULL, | |
1361 | size, sdd->parsable); | |
1362 | } | |
47dfff3b | 1363 | sdd->size += size; |
34dc7c2f BB |
1364 | } |
1365 | ||
330d06f9 | 1366 | if (!sdd->dryrun) { |
37abac6d BP |
1367 | /* |
1368 | * If progress reporting is requested, spawn a new thread to | |
1369 | * poll ZFS_IOC_SEND_PROGRESS at a regular interval. | |
1370 | */ | |
1371 | if (sdd->progress) { | |
1372 | pa.pa_zhp = zhp; | |
1373 | pa.pa_fd = sdd->outfd; | |
1374 | pa.pa_parsable = sdd->parsable; | |
1375 | ||
1376 | if ((err = pthread_create(&tid, NULL, | |
23d70cde | 1377 | send_progress_thread, &pa)) != 0) { |
37abac6d BP |
1378 | zfs_close(zhp); |
1379 | return (err); | |
1380 | } | |
1381 | } | |
1382 | ||
330d06f9 | 1383 | err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj, |
9b67f605 | 1384 | fromorigin, sdd->outfd, flags, sdd->debugnv); |
37abac6d BP |
1385 | |
1386 | if (sdd->progress) { | |
1387 | (void) pthread_cancel(tid); | |
1388 | (void) pthread_join(tid, NULL); | |
1389 | } | |
330d06f9 | 1390 | } |
34dc7c2f | 1391 | |
572e2857 BB |
1392 | (void) strcpy(sdd->prevsnap, thissnap); |
1393 | sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); | |
34dc7c2f BB |
1394 | zfs_close(zhp); |
1395 | return (err); | |
1396 | } | |
1397 | ||
1398 | static int | |
1399 | dump_filesystem(zfs_handle_t *zhp, void *arg) | |
1400 | { | |
1401 | int rv = 0; | |
1402 | send_dump_data_t *sdd = arg; | |
1403 | boolean_t missingfrom = B_FALSE; | |
13fe0198 | 1404 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1405 | |
1406 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", | |
1407 | zhp->zfs_name, sdd->tosnap); | |
1408 | if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { | |
330d06f9 MA |
1409 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
1410 | "WARNING: could not send %s@%s: does not exist\n"), | |
34dc7c2f BB |
1411 | zhp->zfs_name, sdd->tosnap); |
1412 | sdd->err = B_TRUE; | |
1413 | return (0); | |
1414 | } | |
1415 | ||
1416 | if (sdd->replicate && sdd->fromsnap) { | |
1417 | /* | |
1418 | * If this fs does not have fromsnap, and we're doing | |
1419 | * recursive, we need to send a full stream from the | |
1420 | * beginning (or an incremental from the origin if this | |
1421 | * is a clone). If we're doing non-recursive, then let | |
1422 | * them get the error. | |
1423 | */ | |
1424 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", | |
1425 | zhp->zfs_name, sdd->fromsnap); | |
1426 | if (ioctl(zhp->zfs_hdl->libzfs_fd, | |
1427 | ZFS_IOC_OBJSET_STATS, &zc) != 0) { | |
1428 | missingfrom = B_TRUE; | |
1429 | } | |
1430 | } | |
1431 | ||
428870ff | 1432 | sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0; |
572e2857 | 1433 | sdd->prevsnap_obj = 0; |
428870ff BB |
1434 | if (sdd->fromsnap == NULL || missingfrom) |
1435 | sdd->seenfrom = B_TRUE; | |
34dc7c2f | 1436 | |
428870ff BB |
1437 | rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg); |
1438 | if (!sdd->seenfrom) { | |
330d06f9 | 1439 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
428870ff | 1440 | "WARNING: could not send %s@%s:\n" |
330d06f9 | 1441 | "incremental source (%s@%s) does not exist\n"), |
428870ff BB |
1442 | zhp->zfs_name, sdd->tosnap, |
1443 | zhp->zfs_name, sdd->fromsnap); | |
1444 | sdd->err = B_TRUE; | |
1445 | } else if (!sdd->seento) { | |
1446 | if (sdd->fromsnap) { | |
330d06f9 | 1447 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
34dc7c2f | 1448 | "WARNING: could not send %s@%s:\n" |
428870ff | 1449 | "incremental source (%s@%s) " |
330d06f9 | 1450 | "is not earlier than it\n"), |
34dc7c2f BB |
1451 | zhp->zfs_name, sdd->tosnap, |
1452 | zhp->zfs_name, sdd->fromsnap); | |
34dc7c2f | 1453 | } else { |
330d06f9 MA |
1454 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
1455 | "WARNING: " | |
1456 | "could not send %s@%s: does not exist\n"), | |
428870ff | 1457 | zhp->zfs_name, sdd->tosnap); |
34dc7c2f | 1458 | } |
428870ff | 1459 | sdd->err = B_TRUE; |
34dc7c2f BB |
1460 | } |
1461 | ||
1462 | return (rv); | |
1463 | } | |
1464 | ||
1465 | static int | |
1466 | dump_filesystems(zfs_handle_t *rzhp, void *arg) | |
1467 | { | |
1468 | send_dump_data_t *sdd = arg; | |
1469 | nvpair_t *fspair; | |
1470 | boolean_t needagain, progress; | |
1471 | ||
1472 | if (!sdd->replicate) | |
1473 | return (dump_filesystem(rzhp, sdd)); | |
1474 | ||
428870ff BB |
1475 | /* Mark the clone origin snapshots. */ |
1476 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1477 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
1478 | nvlist_t *nvfs; | |
1479 | uint64_t origin_guid = 0; | |
1480 | ||
1481 | VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs)); | |
1482 | (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid); | |
1483 | if (origin_guid != 0) { | |
1484 | char *snapname; | |
1485 | nvlist_t *origin_nv = fsavl_find(sdd->fsavl, | |
1486 | origin_guid, &snapname); | |
1487 | if (origin_nv != NULL) { | |
1488 | nvlist_t *snapprops; | |
1489 | VERIFY(0 == nvlist_lookup_nvlist(origin_nv, | |
1490 | "snapprops", &snapprops)); | |
1491 | VERIFY(0 == nvlist_lookup_nvlist(snapprops, | |
1492 | snapname, &snapprops)); | |
1493 | VERIFY(0 == nvlist_add_boolean( | |
1494 | snapprops, "is_clone_origin")); | |
1495 | } | |
1496 | } | |
1497 | } | |
34dc7c2f BB |
1498 | again: |
1499 | needagain = progress = B_FALSE; | |
1500 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1501 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
330d06f9 | 1502 | nvlist_t *fslist, *parent_nv; |
34dc7c2f BB |
1503 | char *fsname; |
1504 | zfs_handle_t *zhp; | |
1505 | int err; | |
1506 | uint64_t origin_guid = 0; | |
330d06f9 | 1507 | uint64_t parent_guid = 0; |
34dc7c2f BB |
1508 | |
1509 | VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); | |
1510 | if (nvlist_lookup_boolean(fslist, "sent") == 0) | |
1511 | continue; | |
1512 | ||
1513 | VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0); | |
1514 | (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); | |
330d06f9 MA |
1515 | (void) nvlist_lookup_uint64(fslist, "parentfromsnap", |
1516 | &parent_guid); | |
1517 | ||
1518 | if (parent_guid != 0) { | |
1519 | parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL); | |
1520 | if (!nvlist_exists(parent_nv, "sent")) { | |
1521 | /* parent has not been sent; skip this one */ | |
1522 | needagain = B_TRUE; | |
1523 | continue; | |
1524 | } | |
1525 | } | |
34dc7c2f | 1526 | |
428870ff BB |
1527 | if (origin_guid != 0) { |
1528 | nvlist_t *origin_nv = fsavl_find(sdd->fsavl, | |
1529 | origin_guid, NULL); | |
1530 | if (origin_nv != NULL && | |
330d06f9 | 1531 | !nvlist_exists(origin_nv, "sent")) { |
428870ff BB |
1532 | /* |
1533 | * origin has not been sent yet; | |
1534 | * skip this clone. | |
1535 | */ | |
1536 | needagain = B_TRUE; | |
1537 | continue; | |
1538 | } | |
34dc7c2f BB |
1539 | } |
1540 | ||
1541 | zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); | |
1542 | if (zhp == NULL) | |
1543 | return (-1); | |
1544 | err = dump_filesystem(zhp, sdd); | |
1545 | VERIFY(nvlist_add_boolean(fslist, "sent") == 0); | |
1546 | progress = B_TRUE; | |
1547 | zfs_close(zhp); | |
1548 | if (err) | |
1549 | return (err); | |
1550 | } | |
1551 | if (needagain) { | |
1552 | assert(progress); | |
1553 | goto again; | |
1554 | } | |
330d06f9 MA |
1555 | |
1556 | /* clean out the sent flags in case we reuse this fss */ | |
1557 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1558 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
1559 | nvlist_t *fslist; | |
1560 | ||
1561 | VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); | |
1562 | (void) nvlist_remove_all(fslist, "sent"); | |
1563 | } | |
1564 | ||
34dc7c2f BB |
1565 | return (0); |
1566 | } | |
1567 | ||
47dfff3b MA |
1568 | nvlist_t * |
1569 | zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token) | |
1570 | { | |
1571 | unsigned int version; | |
1572 | int nread, i; | |
1573 | unsigned long long checksum, packed_len; | |
1574 | ||
1575 | /* | |
1576 | * Decode token header, which is: | |
1577 | * <token version>-<checksum of payload>-<uncompressed payload length> | |
1578 | * Note that the only supported token version is 1. | |
1579 | */ | |
1580 | nread = sscanf(token, "%u-%llx-%llx-", | |
1581 | &version, &checksum, &packed_len); | |
1582 | if (nread != 3) { | |
1583 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1584 | "resume token is corrupt (invalid format)")); | |
1585 | return (NULL); | |
1586 | } | |
1587 | ||
1588 | if (version != ZFS_SEND_RESUME_TOKEN_VERSION) { | |
1589 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1590 | "resume token is corrupt (invalid version %u)"), | |
1591 | version); | |
1592 | return (NULL); | |
1593 | } | |
1594 | ||
1595 | /* convert hexadecimal representation to binary */ | |
1596 | token = strrchr(token, '-') + 1; | |
1597 | int len = strlen(token) / 2; | |
1598 | unsigned char *compressed = zfs_alloc(hdl, len); | |
1599 | for (i = 0; i < len; i++) { | |
1600 | nread = sscanf(token + i * 2, "%2hhx", compressed + i); | |
1601 | if (nread != 1) { | |
1602 | free(compressed); | |
1603 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1604 | "resume token is corrupt " | |
1605 | "(payload is not hex-encoded)")); | |
1606 | return (NULL); | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | /* verify checksum */ | |
1611 | zio_cksum_t cksum; | |
fc897b24 | 1612 | fletcher_4_native_varsize(compressed, len, &cksum); |
47dfff3b MA |
1613 | if (cksum.zc_word[0] != checksum) { |
1614 | free(compressed); | |
1615 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1616 | "resume token is corrupt (incorrect checksum)")); | |
1617 | return (NULL); | |
1618 | } | |
1619 | ||
1620 | /* uncompress */ | |
1621 | void *packed = zfs_alloc(hdl, packed_len); | |
1622 | uLongf packed_len_long = packed_len; | |
1623 | if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK || | |
1624 | packed_len_long != packed_len) { | |
1625 | free(packed); | |
1626 | free(compressed); | |
1627 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1628 | "resume token is corrupt (decompression failed)")); | |
1629 | return (NULL); | |
1630 | } | |
1631 | ||
1632 | /* unpack nvlist */ | |
1633 | nvlist_t *nv; | |
1634 | int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP); | |
1635 | free(packed); | |
1636 | free(compressed); | |
1637 | if (error != 0) { | |
1638 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1639 | "resume token is corrupt (nvlist_unpack failed)")); | |
1640 | return (NULL); | |
1641 | } | |
1642 | return (nv); | |
1643 | } | |
1644 | ||
1645 | int | |
1646 | zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd, | |
1647 | const char *resume_token) | |
1648 | { | |
1649 | char errbuf[1024]; | |
1650 | char *toname; | |
1651 | char *fromname = NULL; | |
1652 | uint64_t resumeobj, resumeoff, toguid, fromguid, bytes; | |
1653 | zfs_handle_t *zhp; | |
1654 | int error = 0; | |
eca7b760 | 1655 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
47dfff3b MA |
1656 | enum lzc_send_flags lzc_flags = 0; |
1657 | ||
1658 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
1659 | "cannot resume send")); | |
1660 | ||
1661 | nvlist_t *resume_nvl = | |
1662 | zfs_send_resume_token_to_nvlist(hdl, resume_token); | |
1663 | if (resume_nvl == NULL) { | |
1664 | /* | |
1665 | * zfs_error_aux has already been set by | |
1666 | * zfs_send_resume_token_to_nvlist | |
1667 | */ | |
1668 | return (zfs_error(hdl, EZFS_FAULT, errbuf)); | |
1669 | } | |
1670 | if (flags->verbose) { | |
1671 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
1672 | "resume token contents:\n")); | |
1673 | nvlist_print(stderr, resume_nvl); | |
1674 | } | |
1675 | ||
1676 | if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 || | |
1677 | nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 || | |
1678 | nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 || | |
1679 | nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 || | |
1680 | nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) { | |
1681 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1682 | "resume token is corrupt")); | |
1683 | return (zfs_error(hdl, EZFS_FAULT, errbuf)); | |
1684 | } | |
1685 | fromguid = 0; | |
1686 | (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid); | |
1687 | ||
2aa34383 DK |
1688 | if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok")) |
1689 | lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; | |
47dfff3b MA |
1690 | if (flags->embed_data || nvlist_exists(resume_nvl, "embedok")) |
1691 | lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; | |
2aa34383 DK |
1692 | if (flags->compress || nvlist_exists(resume_nvl, "compressok")) |
1693 | lzc_flags |= LZC_SEND_FLAG_COMPRESS; | |
b5256303 TC |
1694 | if (flags->raw || nvlist_exists(resume_nvl, "rawok")) |
1695 | lzc_flags |= LZC_SEND_FLAG_RAW; | |
47dfff3b MA |
1696 | |
1697 | if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) { | |
1698 | if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) { | |
1699 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1700 | "'%s' is no longer the same snapshot used in " | |
1701 | "the initial send"), toname); | |
1702 | } else { | |
1703 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1704 | "'%s' used in the initial send no longer exists"), | |
1705 | toname); | |
1706 | } | |
1707 | return (zfs_error(hdl, EZFS_BADPATH, errbuf)); | |
1708 | } | |
1709 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
1710 | if (zhp == NULL) { | |
1711 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1712 | "unable to access '%s'"), name); | |
1713 | return (zfs_error(hdl, EZFS_BADPATH, errbuf)); | |
1714 | } | |
1715 | ||
1716 | if (fromguid != 0) { | |
1717 | if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) { | |
1718 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1719 | "incremental source %#llx no longer exists"), | |
1720 | (longlong_t)fromguid); | |
1721 | return (zfs_error(hdl, EZFS_BADPATH, errbuf)); | |
1722 | } | |
1723 | fromname = name; | |
1724 | } | |
1725 | ||
1726 | if (flags->verbose) { | |
1727 | uint64_t size = 0; | |
2aa34383 DK |
1728 | error = lzc_send_space(zhp->zfs_name, fromname, |
1729 | lzc_flags, &size); | |
47dfff3b MA |
1730 | if (error == 0) |
1731 | size = MAX(0, (int64_t)(size - bytes)); | |
1732 | send_print_verbose(stderr, zhp->zfs_name, fromname, | |
1733 | size, flags->parsable); | |
1734 | } | |
1735 | ||
1736 | if (!flags->dryrun) { | |
1737 | progress_arg_t pa = { 0 }; | |
1738 | pthread_t tid; | |
1739 | /* | |
1740 | * If progress reporting is requested, spawn a new thread to | |
1741 | * poll ZFS_IOC_SEND_PROGRESS at a regular interval. | |
1742 | */ | |
1743 | if (flags->progress) { | |
1744 | pa.pa_zhp = zhp; | |
1745 | pa.pa_fd = outfd; | |
1746 | pa.pa_parsable = flags->parsable; | |
1747 | ||
1748 | error = pthread_create(&tid, NULL, | |
1749 | send_progress_thread, &pa); | |
1750 | if (error != 0) { | |
1751 | zfs_close(zhp); | |
1752 | return (error); | |
1753 | } | |
1754 | } | |
1755 | ||
1756 | error = lzc_send_resume(zhp->zfs_name, fromname, outfd, | |
1757 | lzc_flags, resumeobj, resumeoff); | |
1758 | ||
1759 | if (flags->progress) { | |
1760 | (void) pthread_cancel(tid); | |
1761 | (void) pthread_join(tid, NULL); | |
1762 | } | |
1763 | ||
1764 | char errbuf[1024]; | |
1765 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
1766 | "warning: cannot send '%s'"), zhp->zfs_name); | |
1767 | ||
1768 | zfs_close(zhp); | |
1769 | ||
1770 | switch (error) { | |
1771 | case 0: | |
1772 | return (0); | |
b5256303 TC |
1773 | case EACCES: |
1774 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1775 | "source key must be loaded")); | |
1776 | return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); | |
1777 | ||
47dfff3b MA |
1778 | case EXDEV: |
1779 | case ENOENT: | |
1780 | case EDQUOT: | |
1781 | case EFBIG: | |
1782 | case EIO: | |
1783 | case ENOLINK: | |
1784 | case ENOSPC: | |
1785 | case ENOSTR: | |
1786 | case ENXIO: | |
1787 | case EPIPE: | |
1788 | case ERANGE: | |
1789 | case EFAULT: | |
1790 | case EROFS: | |
1791 | zfs_error_aux(hdl, strerror(errno)); | |
1792 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
1793 | ||
1794 | default: | |
1795 | return (zfs_standard_error(hdl, errno, errbuf)); | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | ||
1800 | zfs_close(zhp); | |
1801 | ||
1802 | return (error); | |
1803 | } | |
1804 | ||
34dc7c2f | 1805 | /* |
45d1cae3 BB |
1806 | * Generate a send stream for the dataset identified by the argument zhp. |
1807 | * | |
1808 | * The content of the send stream is the snapshot identified by | |
1809 | * 'tosnap'. Incremental streams are requested in two ways: | |
1810 | * - from the snapshot identified by "fromsnap" (if non-null) or | |
1811 | * - from the origin of the dataset identified by zhp, which must | |
1812 | * be a clone. In this case, "fromsnap" is null and "fromorigin" | |
1813 | * is TRUE. | |
1814 | * | |
1815 | * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and | |
428870ff | 1816 | * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM) |
45d1cae3 | 1817 | * if "replicate" is set. If "doall" is set, dump all the intermediate |
428870ff BB |
1818 | * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall" |
1819 | * case too. If "props" is set, send properties. | |
34dc7c2f BB |
1820 | */ |
1821 | int | |
1822 | zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, | |
330d06f9 | 1823 | sendflags_t *flags, int outfd, snapfilter_cb_t filter_func, |
428870ff | 1824 | void *cb_arg, nvlist_t **debugnvp) |
34dc7c2f BB |
1825 | { |
1826 | char errbuf[1024]; | |
1827 | send_dump_data_t sdd = { 0 }; | |
330d06f9 | 1828 | int err = 0; |
34dc7c2f BB |
1829 | nvlist_t *fss = NULL; |
1830 | avl_tree_t *fsavl = NULL; | |
428870ff BB |
1831 | static uint64_t holdseq; |
1832 | int spa_version; | |
95fd54a1 | 1833 | pthread_t tid = 0; |
428870ff BB |
1834 | int pipefd[2]; |
1835 | dedup_arg_t dda = { 0 }; | |
1836 | int featureflags = 0; | |
93f6d7e2 | 1837 | FILE *fout; |
428870ff | 1838 | |
34dc7c2f BB |
1839 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
1840 | "cannot send '%s'"), zhp->zfs_name); | |
1841 | ||
1842 | if (fromsnap && fromsnap[0] == '\0') { | |
1843 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
1844 | "zero-length incremental source")); | |
1845 | return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); | |
1846 | } | |
1847 | ||
572e2857 BB |
1848 | if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) { |
1849 | uint64_t version; | |
1850 | version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); | |
1851 | if (version >= ZPL_VERSION_SA) { | |
1852 | featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; | |
1853 | } | |
1854 | } | |
1855 | ||
b5256303 TC |
1856 | /* |
1857 | * Start the dedup thread if this is a dedup stream. We do not bother | |
1858 | * doing this if this a raw send of an encrypted dataset with dedup off | |
1859 | * because normal encrypted blocks won't dedup. | |
1860 | */ | |
1861 | if (flags->dedup && !flags->dryrun && !(flags->raw && | |
1862 | zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF && | |
1863 | zfs_prop_get_int(zhp, ZFS_PROP_DEDUP) == ZIO_CHECKSUM_OFF)) { | |
428870ff BB |
1864 | featureflags |= (DMU_BACKUP_FEATURE_DEDUP | |
1865 | DMU_BACKUP_FEATURE_DEDUPPROPS); | |
23d70cde | 1866 | if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd)) != 0) { |
428870ff BB |
1867 | zfs_error_aux(zhp->zfs_hdl, strerror(errno)); |
1868 | return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, | |
1869 | errbuf)); | |
1870 | } | |
1871 | dda.outputfd = outfd; | |
1872 | dda.inputfd = pipefd[1]; | |
1873 | dda.dedup_hdl = zhp->zfs_hdl; | |
23d70cde | 1874 | if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) { |
428870ff BB |
1875 | (void) close(pipefd[0]); |
1876 | (void) close(pipefd[1]); | |
1877 | zfs_error_aux(zhp->zfs_hdl, strerror(errno)); | |
1878 | return (zfs_error(zhp->zfs_hdl, | |
1879 | EZFS_THREADCREATEFAILED, errbuf)); | |
1880 | } | |
1881 | } | |
1882 | ||
330d06f9 | 1883 | if (flags->replicate || flags->doall || flags->props) { |
34dc7c2f BB |
1884 | dmu_replay_record_t drr = { 0 }; |
1885 | char *packbuf = NULL; | |
1886 | size_t buflen = 0; | |
47dfff3b MA |
1887 | zio_cksum_t zc; |
1888 | ||
1889 | ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0); | |
34dc7c2f | 1890 | |
330d06f9 | 1891 | if (flags->replicate || flags->props) { |
34dc7c2f BB |
1892 | nvlist_t *hdrnv; |
1893 | ||
1894 | VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0)); | |
1895 | if (fromsnap) { | |
1896 | VERIFY(0 == nvlist_add_string(hdrnv, | |
1897 | "fromsnap", fromsnap)); | |
1898 | } | |
1899 | VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap)); | |
330d06f9 | 1900 | if (!flags->replicate) { |
428870ff BB |
1901 | VERIFY(0 == nvlist_add_boolean(hdrnv, |
1902 | "not_recursive")); | |
1903 | } | |
b5256303 TC |
1904 | if (flags->raw) { |
1905 | VERIFY(0 == nvlist_add_boolean(hdrnv, "raw")); | |
1906 | } | |
34dc7c2f BB |
1907 | |
1908 | err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name, | |
b5256303 TC |
1909 | fromsnap, tosnap, flags->replicate, flags->raw, |
1910 | flags->verbose, &fss, &fsavl); | |
572e2857 | 1911 | if (err) |
428870ff | 1912 | goto err_out; |
34dc7c2f BB |
1913 | VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss)); |
1914 | err = nvlist_pack(hdrnv, &packbuf, &buflen, | |
1915 | NV_ENCODE_XDR, 0); | |
428870ff BB |
1916 | if (debugnvp) |
1917 | *debugnvp = hdrnv; | |
1918 | else | |
1919 | nvlist_free(hdrnv); | |
95fd54a1 | 1920 | if (err) |
428870ff | 1921 | goto stderr_out; |
34dc7c2f BB |
1922 | } |
1923 | ||
330d06f9 MA |
1924 | if (!flags->dryrun) { |
1925 | /* write first begin record */ | |
1926 | drr.drr_type = DRR_BEGIN; | |
1927 | drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; | |
1928 | DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin. | |
1929 | drr_versioninfo, DMU_COMPOUNDSTREAM); | |
1930 | DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin. | |
1931 | drr_versioninfo, featureflags); | |
682ce104 | 1932 | if (snprintf(drr.drr_u.drr_begin.drr_toname, |
330d06f9 | 1933 | sizeof (drr.drr_u.drr_begin.drr_toname), |
682ce104 TH |
1934 | "%s@%s", zhp->zfs_name, tosnap) >= |
1935 | sizeof (drr.drr_u.drr_begin.drr_toname)) { | |
1936 | err = EINVAL; | |
1937 | goto stderr_out; | |
1938 | } | |
330d06f9 | 1939 | drr.drr_payloadlen = buflen; |
330d06f9 | 1940 | |
37f8a883 | 1941 | err = dump_record(&drr, packbuf, buflen, &zc, outfd); |
330d06f9 | 1942 | free(packbuf); |
37f8a883 | 1943 | if (err != 0) |
330d06f9 | 1944 | goto stderr_out; |
34dc7c2f | 1945 | |
330d06f9 | 1946 | /* write end record */ |
34dc7c2f BB |
1947 | bzero(&drr, sizeof (drr)); |
1948 | drr.drr_type = DRR_END; | |
1949 | drr.drr_u.drr_end.drr_checksum = zc; | |
1950 | err = write(outfd, &drr, sizeof (drr)); | |
1951 | if (err == -1) { | |
428870ff | 1952 | err = errno; |
428870ff | 1953 | goto stderr_out; |
34dc7c2f | 1954 | } |
330d06f9 MA |
1955 | |
1956 | err = 0; | |
34dc7c2f BB |
1957 | } |
1958 | } | |
1959 | ||
1960 | /* dump each stream */ | |
1961 | sdd.fromsnap = fromsnap; | |
1962 | sdd.tosnap = tosnap; | |
95fd54a1 | 1963 | if (tid != 0) |
428870ff BB |
1964 | sdd.outfd = pipefd[0]; |
1965 | else | |
1966 | sdd.outfd = outfd; | |
330d06f9 MA |
1967 | sdd.replicate = flags->replicate; |
1968 | sdd.doall = flags->doall; | |
1969 | sdd.fromorigin = flags->fromorigin; | |
34dc7c2f BB |
1970 | sdd.fss = fss; |
1971 | sdd.fsavl = fsavl; | |
330d06f9 MA |
1972 | sdd.verbose = flags->verbose; |
1973 | sdd.parsable = flags->parsable; | |
37abac6d | 1974 | sdd.progress = flags->progress; |
330d06f9 | 1975 | sdd.dryrun = flags->dryrun; |
f1512ee6 | 1976 | sdd.large_block = flags->largeblock; |
9b67f605 | 1977 | sdd.embed_data = flags->embed_data; |
2aa34383 | 1978 | sdd.compress = flags->compress; |
b5256303 | 1979 | sdd.raw = flags->raw; |
428870ff BB |
1980 | sdd.filter_cb = filter_func; |
1981 | sdd.filter_cb_arg = cb_arg; | |
1982 | if (debugnvp) | |
1983 | sdd.debugnv = *debugnvp; | |
93f6d7e2 MJ |
1984 | if (sdd.verbose && sdd.dryrun) |
1985 | sdd.std_out = B_TRUE; | |
1986 | fout = sdd.std_out ? stdout : stderr; | |
e956d651 CS |
1987 | |
1988 | /* | |
1989 | * Some flags require that we place user holds on the datasets that are | |
1990 | * being sent so they don't get destroyed during the send. We can skip | |
1991 | * this step if the pool is imported read-only since the datasets cannot | |
1992 | * be destroyed. | |
1993 | */ | |
1994 | if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp), | |
1995 | ZPOOL_PROP_READONLY, NULL) && | |
1996 | zfs_spa_version(zhp, &spa_version) == 0 && | |
1997 | spa_version >= SPA_VERSION_USERREFS && | |
1998 | (flags->doall || flags->replicate)) { | |
572e2857 BB |
1999 | ++holdseq; |
2000 | (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag), | |
2001 | ".send-%d-%llu", getpid(), (u_longlong_t)holdseq); | |
325f0235 | 2002 | sdd.cleanup_fd = open(ZFS_DEV, O_RDWR); |
572e2857 BB |
2003 | if (sdd.cleanup_fd < 0) { |
2004 | err = errno; | |
2005 | goto stderr_out; | |
2006 | } | |
95fd54a1 | 2007 | sdd.snapholds = fnvlist_alloc(); |
572e2857 BB |
2008 | } else { |
2009 | sdd.cleanup_fd = -1; | |
95fd54a1 | 2010 | sdd.snapholds = NULL; |
572e2857 | 2011 | } |
95fd54a1 | 2012 | if (flags->verbose || sdd.snapholds != NULL) { |
330d06f9 MA |
2013 | /* |
2014 | * Do a verbose no-op dry run to get all the verbose output | |
95fd54a1 SH |
2015 | * or to gather snapshot hold's before generating any data, |
2016 | * then do a non-verbose real run to generate the streams. | |
330d06f9 MA |
2017 | */ |
2018 | sdd.dryrun = B_TRUE; | |
2019 | err = dump_filesystems(zhp, &sdd); | |
95fd54a1 SH |
2020 | |
2021 | if (err != 0) | |
2022 | goto stderr_out; | |
2023 | ||
2024 | if (flags->verbose) { | |
2025 | if (flags->parsable) { | |
93f6d7e2 | 2026 | (void) fprintf(fout, "size\t%llu\n", |
95fd54a1 SH |
2027 | (longlong_t)sdd.size); |
2028 | } else { | |
2029 | char buf[16]; | |
e7fbeb60 | 2030 | zfs_nicebytes(sdd.size, buf, sizeof (buf)); |
93f6d7e2 | 2031 | (void) fprintf(fout, dgettext(TEXT_DOMAIN, |
95fd54a1 SH |
2032 | "total estimated size is %s\n"), buf); |
2033 | } | |
330d06f9 | 2034 | } |
95fd54a1 SH |
2035 | |
2036 | /* Ensure no snaps found is treated as an error. */ | |
2037 | if (!sdd.seento) { | |
2038 | err = ENOENT; | |
2039 | goto err_out; | |
2040 | } | |
2041 | ||
2042 | /* Skip the second run if dryrun was requested. */ | |
2043 | if (flags->dryrun) | |
2044 | goto err_out; | |
2045 | ||
2046 | if (sdd.snapholds != NULL) { | |
2047 | err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds); | |
2048 | if (err != 0) | |
2049 | goto stderr_out; | |
2050 | ||
2051 | fnvlist_free(sdd.snapholds); | |
2052 | sdd.snapholds = NULL; | |
2053 | } | |
2054 | ||
2055 | sdd.dryrun = B_FALSE; | |
2056 | sdd.verbose = B_FALSE; | |
330d06f9 | 2057 | } |
95fd54a1 | 2058 | |
34dc7c2f BB |
2059 | err = dump_filesystems(zhp, &sdd); |
2060 | fsavl_destroy(fsavl); | |
2061 | nvlist_free(fss); | |
2062 | ||
95fd54a1 SH |
2063 | /* Ensure no snaps found is treated as an error. */ |
2064 | if (err == 0 && !sdd.seento) | |
2065 | err = ENOENT; | |
2066 | ||
2067 | if (tid != 0) { | |
2068 | if (err != 0) | |
2069 | (void) pthread_cancel(tid); | |
95fd54a1 | 2070 | (void) close(pipefd[0]); |
6389d422 | 2071 | (void) pthread_join(tid, NULL); |
428870ff BB |
2072 | } |
2073 | ||
572e2857 BB |
2074 | if (sdd.cleanup_fd != -1) { |
2075 | VERIFY(0 == close(sdd.cleanup_fd)); | |
2076 | sdd.cleanup_fd = -1; | |
2077 | } | |
2078 | ||
330d06f9 MA |
2079 | if (!flags->dryrun && (flags->replicate || flags->doall || |
2080 | flags->props)) { | |
34dc7c2f BB |
2081 | /* |
2082 | * write final end record. NB: want to do this even if | |
2083 | * there was some error, because it might not be totally | |
2084 | * failed. | |
2085 | */ | |
2086 | dmu_replay_record_t drr = { 0 }; | |
2087 | drr.drr_type = DRR_END; | |
2088 | if (write(outfd, &drr, sizeof (drr)) == -1) { | |
2089 | return (zfs_standard_error(zhp->zfs_hdl, | |
2090 | errno, errbuf)); | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | return (err || sdd.err); | |
428870ff BB |
2095 | |
2096 | stderr_out: | |
2097 | err = zfs_standard_error(zhp->zfs_hdl, err, errbuf); | |
2098 | err_out: | |
95fd54a1 SH |
2099 | fsavl_destroy(fsavl); |
2100 | nvlist_free(fss); | |
2101 | fnvlist_free(sdd.snapholds); | |
2102 | ||
572e2857 BB |
2103 | if (sdd.cleanup_fd != -1) |
2104 | VERIFY(0 == close(sdd.cleanup_fd)); | |
95fd54a1 | 2105 | if (tid != 0) { |
428870ff | 2106 | (void) pthread_cancel(tid); |
428870ff | 2107 | (void) close(pipefd[0]); |
6389d422 | 2108 | (void) pthread_join(tid, NULL); |
428870ff BB |
2109 | } |
2110 | return (err); | |
34dc7c2f BB |
2111 | } |
2112 | ||
da536844 | 2113 | int |
835db585 | 2114 | zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t flags) |
da536844 | 2115 | { |
835db585 | 2116 | int err = 0; |
da536844 | 2117 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
835db585 | 2118 | enum lzc_send_flags lzc_flags = 0; |
2119 | FILE *fout = (flags.verbose && flags.dryrun) ? stdout : stderr; | |
da536844 | 2120 | char errbuf[1024]; |
835db585 | 2121 | |
2122 | if (flags.largeblock) | |
2123 | lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; | |
2124 | if (flags.embed_data) | |
2125 | lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; | |
2126 | if (flags.compress) | |
2127 | lzc_flags |= LZC_SEND_FLAG_COMPRESS; | |
2128 | if (flags.raw) | |
2129 | lzc_flags |= LZC_SEND_FLAG_RAW; | |
2130 | ||
2131 | if (flags.verbose) { | |
2132 | uint64_t size = 0; | |
2133 | err = lzc_send_space(zhp->zfs_name, from, lzc_flags, &size); | |
2134 | if (err == 0) { | |
2135 | send_print_verbose(fout, zhp->zfs_name, from, size, | |
2136 | flags.parsable); | |
2137 | } else { | |
2138 | (void) fprintf(stderr, "Cannot estimate send size: " | |
2139 | "%s\n", strerror(errno)); | |
2140 | } | |
2141 | } | |
2142 | ||
2143 | if (flags.dryrun) | |
2144 | return (err); | |
2145 | ||
da536844 MA |
2146 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
2147 | "warning: cannot send '%s'"), zhp->zfs_name); | |
2148 | ||
835db585 | 2149 | err = lzc_send(zhp->zfs_name, from, fd, lzc_flags); |
da536844 MA |
2150 | if (err != 0) { |
2151 | switch (errno) { | |
2152 | case EXDEV: | |
2153 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2154 | "not an earlier snapshot from the same fs")); | |
2155 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
2156 | ||
2157 | case ENOENT: | |
2158 | case ESRCH: | |
2159 | if (lzc_exists(zhp->zfs_name)) { | |
2160 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2161 | "incremental source (%s) does not exist"), | |
2162 | from); | |
2163 | } | |
2164 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
2165 | ||
b5256303 TC |
2166 | case EACCES: |
2167 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2168 | "dataset key must be loaded")); | |
2169 | return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); | |
2170 | ||
da536844 MA |
2171 | case EBUSY: |
2172 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2173 | "target is busy; if a filesystem, " | |
2174 | "it must not be mounted")); | |
2175 | return (zfs_error(hdl, EZFS_BUSY, errbuf)); | |
2176 | ||
2177 | case EDQUOT: | |
2178 | case EFBIG: | |
2179 | case EIO: | |
2180 | case ENOLINK: | |
2181 | case ENOSPC: | |
2182 | case ENOSTR: | |
2183 | case ENXIO: | |
2184 | case EPIPE: | |
2185 | case ERANGE: | |
2186 | case EFAULT: | |
2187 | case EROFS: | |
2188 | zfs_error_aux(hdl, strerror(errno)); | |
2189 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
2190 | ||
2191 | default: | |
2192 | return (zfs_standard_error(hdl, errno, errbuf)); | |
2193 | } | |
2194 | } | |
2195 | return (err != 0); | |
2196 | } | |
2197 | ||
34dc7c2f BB |
2198 | /* |
2199 | * Routines specific to "zfs recv" | |
2200 | */ | |
2201 | ||
2202 | static int | |
2203 | recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, | |
2204 | boolean_t byteswap, zio_cksum_t *zc) | |
2205 | { | |
2206 | char *cp = buf; | |
2207 | int rv; | |
2208 | int len = ilen; | |
2209 | ||
37f8a883 MA |
2210 | assert(ilen <= SPA_MAXBLOCKSIZE); |
2211 | ||
34dc7c2f BB |
2212 | do { |
2213 | rv = read(fd, cp, len); | |
2214 | cp += rv; | |
2215 | len -= rv; | |
2216 | } while (rv > 0); | |
2217 | ||
2218 | if (rv < 0 || len != 0) { | |
2219 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2220 | "failed to read from stream")); | |
2221 | return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, | |
2222 | "cannot receive"))); | |
2223 | } | |
2224 | ||
2225 | if (zc) { | |
2226 | if (byteswap) | |
2227 | fletcher_4_incremental_byteswap(buf, ilen, zc); | |
2228 | else | |
2229 | fletcher_4_incremental_native(buf, ilen, zc); | |
2230 | } | |
2231 | return (0); | |
2232 | } | |
2233 | ||
2234 | static int | |
2235 | recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, | |
2236 | boolean_t byteswap, zio_cksum_t *zc) | |
2237 | { | |
2238 | char *buf; | |
2239 | int err; | |
2240 | ||
2241 | buf = zfs_alloc(hdl, len); | |
2242 | if (buf == NULL) | |
2243 | return (ENOMEM); | |
2244 | ||
2245 | err = recv_read(hdl, fd, buf, len, byteswap, zc); | |
2246 | if (err != 0) { | |
2247 | free(buf); | |
2248 | return (err); | |
2249 | } | |
2250 | ||
2251 | err = nvlist_unpack(buf, len, nvp, 0); | |
2252 | free(buf); | |
2253 | if (err != 0) { | |
2254 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
2255 | "stream (malformed nvlist)")); | |
2256 | return (EINVAL); | |
2257 | } | |
2258 | return (0); | |
2259 | } | |
2260 | ||
b5256303 TC |
2261 | /* |
2262 | * Returns the grand origin (origin of origin of origin...) of a given handle. | |
2263 | * If this dataset is not a clone, it simply returns a copy of the original | |
2264 | * handle. | |
2265 | */ | |
2266 | static zfs_handle_t * | |
2267 | recv_open_grand_origin(zfs_handle_t *zhp) | |
2268 | { | |
2269 | char origin[ZFS_MAX_DATASET_NAME_LEN]; | |
2270 | zprop_source_t src; | |
2271 | zfs_handle_t *ozhp = zfs_handle_dup(zhp); | |
2272 | ||
2273 | while (ozhp != NULL) { | |
2274 | if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin, | |
2275 | sizeof (origin), &src, NULL, 0, B_FALSE) != 0) | |
2276 | break; | |
2277 | ||
2278 | (void) zfs_close(ozhp); | |
2279 | ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM); | |
2280 | } | |
2281 | ||
2282 | return (ozhp); | |
2283 | } | |
2284 | ||
2285 | static int | |
2286 | recv_rename_impl(zfs_handle_t *zhp, zfs_cmd_t *zc) | |
2287 | { | |
2288 | int err; | |
2289 | zfs_handle_t *ozhp = NULL; | |
2290 | ||
2291 | /* | |
2292 | * Attempt to rename the dataset. If it fails with EACCES we have | |
2293 | * attempted to rename the dataset outside of its encryption root. | |
2294 | * Force the dataset to become an encryption root and try again. | |
2295 | */ | |
2296 | err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); | |
2297 | if (err == EACCES) { | |
2298 | ozhp = recv_open_grand_origin(zhp); | |
2299 | if (ozhp == NULL) { | |
2300 | err = ENOENT; | |
2301 | goto out; | |
2302 | } | |
2303 | ||
2304 | err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY, | |
2305 | NULL, NULL, 0); | |
2306 | if (err != 0) | |
2307 | goto out; | |
2308 | ||
2309 | err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); | |
2310 | } | |
2311 | ||
2312 | out: | |
2313 | if (ozhp != NULL) | |
2314 | zfs_close(ozhp); | |
2315 | return (err); | |
2316 | } | |
2317 | ||
34dc7c2f BB |
2318 | static int |
2319 | recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, | |
330d06f9 | 2320 | int baselen, char *newname, recvflags_t *flags) |
34dc7c2f BB |
2321 | { |
2322 | static int seq; | |
13fe0198 | 2323 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f | 2324 | int err; |
b5256303 TC |
2325 | prop_changelist_t *clp = NULL; |
2326 | zfs_handle_t *zhp = NULL; | |
34dc7c2f BB |
2327 | |
2328 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
b5256303 TC |
2329 | if (zhp == NULL) { |
2330 | err = -1; | |
2331 | goto out; | |
2332 | } | |
b128c09f | 2333 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, |
330d06f9 | 2334 | flags->force ? MS_FORCE : 0); |
b5256303 TC |
2335 | if (clp == NULL) { |
2336 | err = -1; | |
2337 | goto out; | |
2338 | } | |
34dc7c2f BB |
2339 | err = changelist_prefix(clp); |
2340 | if (err) | |
b5256303 | 2341 | goto out; |
34dc7c2f | 2342 | |
45d1cae3 BB |
2343 | zc.zc_objset_type = DMU_OST_ZFS; |
2344 | (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); | |
2345 | ||
34dc7c2f BB |
2346 | if (tryname) { |
2347 | (void) strcpy(newname, tryname); | |
2348 | ||
34dc7c2f BB |
2349 | (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value)); |
2350 | ||
330d06f9 | 2351 | if (flags->verbose) { |
34dc7c2f BB |
2352 | (void) printf("attempting rename %s to %s\n", |
2353 | zc.zc_name, zc.zc_value); | |
2354 | } | |
b5256303 | 2355 | err = recv_rename_impl(zhp, &zc); |
34dc7c2f BB |
2356 | if (err == 0) |
2357 | changelist_rename(clp, name, tryname); | |
2358 | } else { | |
2359 | err = ENOENT; | |
2360 | } | |
2361 | ||
13fe0198 | 2362 | if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) { |
34dc7c2f BB |
2363 | seq++; |
2364 | ||
eca7b760 IK |
2365 | (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN, |
2366 | "%.*srecv-%u-%u", baselen, name, getpid(), seq); | |
34dc7c2f BB |
2367 | (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value)); |
2368 | ||
330d06f9 | 2369 | if (flags->verbose) { |
34dc7c2f BB |
2370 | (void) printf("failed - trying rename %s to %s\n", |
2371 | zc.zc_name, zc.zc_value); | |
2372 | } | |
b5256303 | 2373 | err = recv_rename_impl(zhp, &zc); |
34dc7c2f BB |
2374 | if (err == 0) |
2375 | changelist_rename(clp, name, newname); | |
330d06f9 | 2376 | if (err && flags->verbose) { |
34dc7c2f BB |
2377 | (void) printf("failed (%u) - " |
2378 | "will try again on next pass\n", errno); | |
2379 | } | |
2380 | err = EAGAIN; | |
330d06f9 | 2381 | } else if (flags->verbose) { |
34dc7c2f BB |
2382 | if (err == 0) |
2383 | (void) printf("success\n"); | |
2384 | else | |
2385 | (void) printf("failed (%u)\n", errno); | |
2386 | } | |
2387 | ||
2388 | (void) changelist_postfix(clp); | |
b5256303 TC |
2389 | |
2390 | out: | |
2391 | if (clp != NULL) | |
2392 | changelist_free(clp); | |
2393 | if (zhp != NULL) | |
2394 | zfs_close(zhp); | |
2395 | ||
2396 | return (err); | |
2397 | } | |
2398 | ||
2399 | static int | |
2400 | recv_promote(libzfs_handle_t *hdl, const char *fsname, | |
2401 | const char *origin_fsname, recvflags_t *flags) | |
2402 | { | |
2403 | int err; | |
2404 | zfs_cmd_t zc = {"\0"}; | |
2405 | zfs_handle_t *zhp = NULL, *ozhp = NULL; | |
2406 | ||
2407 | if (flags->verbose) | |
2408 | (void) printf("promoting %s\n", fsname); | |
2409 | ||
2410 | (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value)); | |
2411 | (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name)); | |
2412 | ||
2413 | /* | |
2414 | * Attempt to promote the dataset. If it fails with EACCES the | |
2415 | * promotion would cause this dataset to leave its encryption root. | |
2416 | * Force the origin to become an encryption root and try again. | |
2417 | */ | |
2418 | err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); | |
2419 | if (err == EACCES) { | |
2420 | zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET); | |
2421 | if (zhp == NULL) { | |
2422 | err = -1; | |
2423 | goto out; | |
2424 | } | |
2425 | ||
2426 | ozhp = recv_open_grand_origin(zhp); | |
2427 | if (ozhp == NULL) { | |
2428 | err = -1; | |
2429 | goto out; | |
2430 | } | |
2431 | ||
2432 | err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY, | |
2433 | NULL, NULL, 0); | |
2434 | if (err != 0) | |
2435 | goto out; | |
2436 | ||
2437 | err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); | |
2438 | } | |
2439 | ||
2440 | out: | |
2441 | if (zhp != NULL) | |
2442 | zfs_close(zhp); | |
2443 | if (ozhp != NULL) | |
2444 | zfs_close(ozhp); | |
34dc7c2f BB |
2445 | |
2446 | return (err); | |
2447 | } | |
2448 | ||
2449 | static int | |
2450 | recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, | |
330d06f9 | 2451 | char *newname, recvflags_t *flags) |
34dc7c2f | 2452 | { |
13fe0198 | 2453 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
2454 | int err = 0; |
2455 | prop_changelist_t *clp; | |
2456 | zfs_handle_t *zhp; | |
45d1cae3 BB |
2457 | boolean_t defer = B_FALSE; |
2458 | int spa_version; | |
34dc7c2f BB |
2459 | |
2460 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
2461 | if (zhp == NULL) | |
2462 | return (-1); | |
b128c09f | 2463 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, |
330d06f9 | 2464 | flags->force ? MS_FORCE : 0); |
45d1cae3 BB |
2465 | if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && |
2466 | zfs_spa_version(zhp, &spa_version) == 0 && | |
2467 | spa_version >= SPA_VERSION_USERREFS) | |
2468 | defer = B_TRUE; | |
34dc7c2f BB |
2469 | zfs_close(zhp); |
2470 | if (clp == NULL) | |
2471 | return (-1); | |
2472 | err = changelist_prefix(clp); | |
2473 | if (err) | |
2474 | return (err); | |
2475 | ||
2476 | zc.zc_objset_type = DMU_OST_ZFS; | |
45d1cae3 | 2477 | zc.zc_defer_destroy = defer; |
34dc7c2f BB |
2478 | (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); |
2479 | ||
330d06f9 | 2480 | if (flags->verbose) |
34dc7c2f BB |
2481 | (void) printf("attempting destroy %s\n", zc.zc_name); |
2482 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc); | |
34dc7c2f | 2483 | if (err == 0) { |
330d06f9 | 2484 | if (flags->verbose) |
34dc7c2f BB |
2485 | (void) printf("success\n"); |
2486 | changelist_remove(clp, zc.zc_name); | |
2487 | } | |
2488 | ||
2489 | (void) changelist_postfix(clp); | |
2490 | changelist_free(clp); | |
2491 | ||
45d1cae3 | 2492 | /* |
428870ff BB |
2493 | * Deferred destroy might destroy the snapshot or only mark it to be |
2494 | * destroyed later, and it returns success in either case. | |
45d1cae3 | 2495 | */ |
428870ff BB |
2496 | if (err != 0 || (defer && zfs_dataset_exists(hdl, name, |
2497 | ZFS_TYPE_SNAPSHOT))) { | |
34dc7c2f | 2498 | err = recv_rename(hdl, name, NULL, baselen, newname, flags); |
428870ff | 2499 | } |
34dc7c2f BB |
2500 | |
2501 | return (err); | |
2502 | } | |
2503 | ||
2504 | typedef struct guid_to_name_data { | |
2505 | uint64_t guid; | |
47dfff3b | 2506 | boolean_t bookmark_ok; |
34dc7c2f | 2507 | char *name; |
330d06f9 | 2508 | char *skip; |
34dc7c2f BB |
2509 | } guid_to_name_data_t; |
2510 | ||
2511 | static int | |
2512 | guid_to_name_cb(zfs_handle_t *zhp, void *arg) | |
2513 | { | |
2514 | guid_to_name_data_t *gtnd = arg; | |
47dfff3b | 2515 | const char *slash; |
34dc7c2f BB |
2516 | int err; |
2517 | ||
330d06f9 | 2518 | if (gtnd->skip != NULL && |
47dfff3b MA |
2519 | (slash = strrchr(zhp->zfs_name, '/')) != NULL && |
2520 | strcmp(slash + 1, gtnd->skip) == 0) { | |
2521 | zfs_close(zhp); | |
330d06f9 MA |
2522 | return (0); |
2523 | } | |
2524 | ||
47dfff3b | 2525 | if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) { |
34dc7c2f | 2526 | (void) strcpy(gtnd->name, zhp->zfs_name); |
428870ff | 2527 | zfs_close(zhp); |
34dc7c2f BB |
2528 | return (EEXIST); |
2529 | } | |
330d06f9 | 2530 | |
34dc7c2f | 2531 | err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); |
47dfff3b MA |
2532 | if (err != EEXIST && gtnd->bookmark_ok) |
2533 | err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd); | |
34dc7c2f BB |
2534 | zfs_close(zhp); |
2535 | return (err); | |
2536 | } | |
2537 | ||
330d06f9 MA |
2538 | /* |
2539 | * Attempt to find the local dataset associated with this guid. In the case of | |
2540 | * multiple matches, we attempt to find the "best" match by searching | |
2541 | * progressively larger portions of the hierarchy. This allows one to send a | |
2542 | * tree of datasets individually and guarantee that we will find the source | |
2543 | * guid within that hierarchy, even if there are multiple matches elsewhere. | |
2544 | */ | |
34dc7c2f BB |
2545 | static int |
2546 | guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, | |
47dfff3b | 2547 | boolean_t bookmark_ok, char *name) |
34dc7c2f | 2548 | { |
eca7b760 | 2549 | char pname[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f | 2550 | guid_to_name_data_t gtnd; |
34dc7c2f BB |
2551 | |
2552 | gtnd.guid = guid; | |
47dfff3b | 2553 | gtnd.bookmark_ok = bookmark_ok; |
34dc7c2f | 2554 | gtnd.name = name; |
330d06f9 | 2555 | gtnd.skip = NULL; |
34dc7c2f | 2556 | |
330d06f9 | 2557 | /* |
47dfff3b MA |
2558 | * Search progressively larger portions of the hierarchy, starting |
2559 | * with the filesystem specified by 'parent'. This will | |
330d06f9 MA |
2560 | * select the "most local" version of the origin snapshot in the case |
2561 | * that there are multiple matching snapshots in the system. | |
2562 | */ | |
47dfff3b MA |
2563 | (void) strlcpy(pname, parent, sizeof (pname)); |
2564 | char *cp = strrchr(pname, '@'); | |
2565 | if (cp == NULL) | |
2566 | cp = strchr(pname, '\0'); | |
2567 | for (; cp != NULL; cp = strrchr(pname, '/')) { | |
330d06f9 | 2568 | /* Chop off the last component and open the parent */ |
34dc7c2f | 2569 | *cp = '\0'; |
47dfff3b | 2570 | zfs_handle_t *zhp = make_dataset_handle(hdl, pname); |
330d06f9 MA |
2571 | |
2572 | if (zhp == NULL) | |
2573 | continue; | |
47dfff3b MA |
2574 | int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd); |
2575 | if (err != EEXIST) | |
2576 | err = zfs_iter_children(zhp, guid_to_name_cb, >nd); | |
2577 | if (err != EEXIST && bookmark_ok) | |
2578 | err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd); | |
34dc7c2f | 2579 | zfs_close(zhp); |
330d06f9 MA |
2580 | if (err == EEXIST) |
2581 | return (0); | |
34dc7c2f | 2582 | |
330d06f9 | 2583 | /* |
47dfff3b MA |
2584 | * Remember the last portion of the dataset so we skip it next |
2585 | * time through (as we've already searched that portion of the | |
2586 | * hierarchy). | |
330d06f9 | 2587 | */ |
47dfff3b | 2588 | gtnd.skip = strrchr(pname, '/') + 1; |
330d06f9 | 2589 | } |
34dc7c2f | 2590 | |
330d06f9 | 2591 | return (ENOENT); |
34dc7c2f BB |
2592 | } |
2593 | ||
2594 | /* | |
330d06f9 MA |
2595 | * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if |
2596 | * guid1 is after guid2. | |
34dc7c2f BB |
2597 | */ |
2598 | static int | |
2599 | created_before(libzfs_handle_t *hdl, avl_tree_t *avl, | |
2600 | uint64_t guid1, uint64_t guid2) | |
2601 | { | |
2602 | nvlist_t *nvfs; | |
98401d23 | 2603 | char *fsname = NULL, *snapname = NULL; |
eca7b760 | 2604 | char buf[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f | 2605 | int rv; |
330d06f9 MA |
2606 | zfs_handle_t *guid1hdl, *guid2hdl; |
2607 | uint64_t create1, create2; | |
34dc7c2f BB |
2608 | |
2609 | if (guid2 == 0) | |
2610 | return (0); | |
2611 | if (guid1 == 0) | |
2612 | return (1); | |
2613 | ||
2614 | nvfs = fsavl_find(avl, guid1, &snapname); | |
2615 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2616 | (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); | |
330d06f9 MA |
2617 | guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); |
2618 | if (guid1hdl == NULL) | |
34dc7c2f BB |
2619 | return (-1); |
2620 | ||
2621 | nvfs = fsavl_find(avl, guid2, &snapname); | |
2622 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2623 | (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); | |
330d06f9 MA |
2624 | guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); |
2625 | if (guid2hdl == NULL) { | |
2626 | zfs_close(guid1hdl); | |
34dc7c2f BB |
2627 | return (-1); |
2628 | } | |
2629 | ||
330d06f9 MA |
2630 | create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG); |
2631 | create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG); | |
34dc7c2f | 2632 | |
330d06f9 MA |
2633 | if (create1 < create2) |
2634 | rv = -1; | |
2635 | else if (create1 > create2) | |
2636 | rv = +1; | |
2637 | else | |
2638 | rv = 0; | |
2639 | ||
2640 | zfs_close(guid1hdl); | |
2641 | zfs_close(guid2hdl); | |
34dc7c2f BB |
2642 | |
2643 | return (rv); | |
2644 | } | |
2645 | ||
b5256303 TC |
2646 | /* |
2647 | * This function reestablishes the heirarchy of encryption roots after a | |
2648 | * recursive incremental receive has completed. This must be done after the | |
2649 | * second call to recv_incremental_replication() has renamed and promoted all | |
2650 | * sent datasets to their final locations in the dataset heriarchy. | |
2651 | */ | |
2652 | static int | |
2653 | recv_fix_encryption_heirarchy(libzfs_handle_t *hdl, const char *destname, | |
2654 | nvlist_t *stream_nv, avl_tree_t *stream_avl) | |
2655 | { | |
2656 | int err; | |
2657 | nvpair_t *fselem = NULL; | |
2658 | nvlist_t *stream_fss; | |
2659 | ||
2660 | VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", &stream_fss)); | |
2661 | ||
2662 | while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) { | |
2663 | zfs_handle_t *zhp = NULL; | |
2664 | uint64_t crypt; | |
2665 | nvlist_t *snaps, *props, *stream_nvfs = NULL; | |
2666 | nvpair_t *snapel = NULL; | |
2667 | boolean_t is_encroot, is_clone, stream_encroot; | |
2668 | char *cp; | |
2669 | char *stream_keylocation = NULL; | |
2670 | char keylocation[MAXNAMELEN]; | |
2671 | char fsname[ZFS_MAX_DATASET_NAME_LEN]; | |
2672 | ||
2673 | keylocation[0] = '\0'; | |
2674 | VERIFY(0 == nvpair_value_nvlist(fselem, &stream_nvfs)); | |
2675 | VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps", &snaps)); | |
2676 | VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "props", &props)); | |
2677 | stream_encroot = nvlist_exists(stream_nvfs, "is_encroot"); | |
2678 | ||
2679 | /* find a snapshot from the stream that exists locally */ | |
2680 | err = ENOENT; | |
2681 | while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) { | |
2682 | uint64_t guid; | |
2683 | ||
2684 | VERIFY(0 == nvpair_value_uint64(snapel, &guid)); | |
2685 | err = guid_to_name(hdl, destname, guid, B_FALSE, | |
2686 | fsname); | |
2687 | if (err == 0) | |
2688 | break; | |
2689 | } | |
2690 | ||
2691 | if (err != 0) | |
2692 | continue; | |
2693 | ||
2694 | cp = strchr(fsname, '@'); | |
2695 | if (cp != NULL) | |
2696 | *cp = '\0'; | |
2697 | ||
2698 | zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET); | |
2699 | if (zhp == NULL) { | |
2700 | err = ENOENT; | |
2701 | goto error; | |
2702 | } | |
2703 | ||
2704 | crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION); | |
2705 | is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0'; | |
2706 | (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL); | |
2707 | ||
2708 | /* we don't need to do anything for unencrypted filesystems */ | |
2709 | if (crypt == ZIO_CRYPT_OFF) { | |
2710 | zfs_close(zhp); | |
2711 | continue; | |
2712 | } | |
2713 | ||
2714 | /* | |
2715 | * If the dataset is flagged as an encryption root, was not | |
2716 | * received as a clone and is not currently an encryption root, | |
2717 | * force it to become one. Fixup the keylocation if necessary. | |
2718 | */ | |
2719 | if (stream_encroot) { | |
2720 | if (!is_clone && !is_encroot) { | |
2721 | err = lzc_change_key(fsname, | |
2722 | DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0); | |
2723 | if (err != 0) { | |
2724 | zfs_close(zhp); | |
2725 | goto error; | |
2726 | } | |
2727 | } | |
2728 | ||
2729 | VERIFY(0 == nvlist_lookup_string(props, | |
2730 | zfs_prop_to_name(ZFS_PROP_KEYLOCATION), | |
2731 | &stream_keylocation)); | |
2732 | ||
2733 | /* | |
2734 | * Refresh the properties in case the call to | |
2735 | * lzc_change_key() changed the value. | |
2736 | */ | |
2737 | zfs_refresh_properties(zhp); | |
2738 | err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION, | |
2739 | keylocation, sizeof (keylocation), NULL, NULL, | |
2740 | 0, B_TRUE); | |
2741 | if (err != 0) { | |
2742 | zfs_close(zhp); | |
2743 | goto error; | |
2744 | } | |
2745 | ||
2746 | if (strcmp(keylocation, stream_keylocation) != 0) { | |
2747 | err = zfs_prop_set(zhp, | |
2748 | zfs_prop_to_name(ZFS_PROP_KEYLOCATION), | |
2749 | stream_keylocation); | |
2750 | if (err != 0) { | |
2751 | zfs_close(zhp); | |
2752 | goto error; | |
2753 | } | |
2754 | } | |
2755 | } | |
2756 | ||
2757 | /* | |
2758 | * If the dataset is not flagged as an encryption root and is | |
2759 | * currently an encryption root, force it to inherit from its | |
2760 | * parent. | |
2761 | */ | |
2762 | if (!stream_encroot && is_encroot) { | |
2763 | err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT, | |
2764 | NULL, NULL, 0); | |
2765 | if (err != 0) { | |
2766 | zfs_close(zhp); | |
2767 | goto error; | |
2768 | } | |
2769 | } | |
2770 | ||
2771 | zfs_close(zhp); | |
2772 | } | |
2773 | ||
2774 | return (0); | |
2775 | ||
2776 | error: | |
2777 | return (err); | |
2778 | } | |
2779 | ||
34dc7c2f BB |
2780 | static int |
2781 | recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, | |
330d06f9 | 2782 | recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl, |
428870ff | 2783 | nvlist_t *renamed) |
34dc7c2f | 2784 | { |
7509a3d2 | 2785 | nvlist_t *local_nv, *deleted = NULL; |
34dc7c2f BB |
2786 | avl_tree_t *local_avl; |
2787 | nvpair_t *fselem, *nextfselem; | |
428870ff | 2788 | char *fromsnap; |
eca7b760 | 2789 | char newname[ZFS_MAX_DATASET_NAME_LEN]; |
7509a3d2 | 2790 | char guidname[32]; |
34dc7c2f | 2791 | int error; |
428870ff BB |
2792 | boolean_t needagain, progress, recursive; |
2793 | char *s1, *s2; | |
34dc7c2f BB |
2794 | |
2795 | VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap)); | |
428870ff BB |
2796 | |
2797 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == | |
2798 | ENOENT); | |
34dc7c2f | 2799 | |
330d06f9 | 2800 | if (flags->dryrun) |
34dc7c2f BB |
2801 | return (0); |
2802 | ||
2803 | again: | |
2804 | needagain = progress = B_FALSE; | |
2805 | ||
7509a3d2 | 2806 | VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0)); |
2807 | ||
34dc7c2f | 2808 | if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, |
b5256303 | 2809 | recursive, B_TRUE, B_FALSE, &local_nv, &local_avl)) != 0) |
34dc7c2f BB |
2810 | return (error); |
2811 | ||
2812 | /* | |
2813 | * Process deletes and renames | |
2814 | */ | |
2815 | for (fselem = nvlist_next_nvpair(local_nv, NULL); | |
2816 | fselem; fselem = nextfselem) { | |
2817 | nvlist_t *nvfs, *snaps; | |
2818 | nvlist_t *stream_nvfs = NULL; | |
2819 | nvpair_t *snapelem, *nextsnapelem; | |
2820 | uint64_t fromguid = 0; | |
2821 | uint64_t originguid = 0; | |
2822 | uint64_t stream_originguid = 0; | |
2823 | uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; | |
2824 | char *fsname, *stream_fsname; | |
2825 | ||
2826 | nextfselem = nvlist_next_nvpair(local_nv, fselem); | |
2827 | ||
2828 | VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); | |
2829 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); | |
2830 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2831 | VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap", | |
2832 | &parent_fromsnap_guid)); | |
2833 | (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); | |
2834 | ||
2835 | /* | |
2836 | * First find the stream's fs, so we can check for | |
2837 | * a different origin (due to "zfs promote") | |
2838 | */ | |
2839 | for (snapelem = nvlist_next_nvpair(snaps, NULL); | |
2840 | snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { | |
2841 | uint64_t thisguid; | |
2842 | ||
2843 | VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); | |
2844 | stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); | |
2845 | ||
2846 | if (stream_nvfs != NULL) | |
2847 | break; | |
2848 | } | |
2849 | ||
2850 | /* check for promote */ | |
2851 | (void) nvlist_lookup_uint64(stream_nvfs, "origin", | |
2852 | &stream_originguid); | |
2853 | if (stream_nvfs && originguid != stream_originguid) { | |
2854 | switch (created_before(hdl, local_avl, | |
2855 | stream_originguid, originguid)) { | |
2856 | case 1: { | |
2857 | /* promote it! */ | |
34dc7c2f BB |
2858 | nvlist_t *origin_nvfs; |
2859 | char *origin_fsname; | |
2860 | ||
34dc7c2f BB |
2861 | origin_nvfs = fsavl_find(local_avl, originguid, |
2862 | NULL); | |
2863 | VERIFY(0 == nvlist_lookup_string(origin_nvfs, | |
2864 | "name", &origin_fsname)); | |
b5256303 TC |
2865 | error = recv_promote(hdl, fsname, origin_fsname, |
2866 | flags); | |
34dc7c2f BB |
2867 | if (error == 0) |
2868 | progress = B_TRUE; | |
2869 | break; | |
2870 | } | |
2871 | default: | |
2872 | break; | |
2873 | case -1: | |
2874 | fsavl_destroy(local_avl); | |
2875 | nvlist_free(local_nv); | |
2876 | return (-1); | |
2877 | } | |
2878 | /* | |
2879 | * We had/have the wrong origin, therefore our | |
2880 | * list of snapshots is wrong. Need to handle | |
2881 | * them on the next pass. | |
2882 | */ | |
2883 | needagain = B_TRUE; | |
2884 | continue; | |
2885 | } | |
2886 | ||
2887 | for (snapelem = nvlist_next_nvpair(snaps, NULL); | |
2888 | snapelem; snapelem = nextsnapelem) { | |
2889 | uint64_t thisguid; | |
2890 | char *stream_snapname; | |
b128c09f | 2891 | nvlist_t *found, *props; |
34dc7c2f BB |
2892 | |
2893 | nextsnapelem = nvlist_next_nvpair(snaps, snapelem); | |
2894 | ||
2895 | VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); | |
2896 | found = fsavl_find(stream_avl, thisguid, | |
2897 | &stream_snapname); | |
2898 | ||
2899 | /* check for delete */ | |
2900 | if (found == NULL) { | |
eca7b760 | 2901 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f | 2902 | |
330d06f9 | 2903 | if (!flags->force) |
34dc7c2f BB |
2904 | continue; |
2905 | ||
2906 | (void) snprintf(name, sizeof (name), "%s@%s", | |
2907 | fsname, nvpair_name(snapelem)); | |
2908 | ||
2909 | error = recv_destroy(hdl, name, | |
2910 | strlen(fsname)+1, newname, flags); | |
2911 | if (error) | |
2912 | needagain = B_TRUE; | |
2913 | else | |
2914 | progress = B_TRUE; | |
3df29340 BB |
2915 | sprintf(guidname, "%llu", |
2916 | (u_longlong_t)thisguid); | |
7509a3d2 | 2917 | nvlist_add_boolean(deleted, guidname); |
34dc7c2f BB |
2918 | continue; |
2919 | } | |
2920 | ||
2921 | stream_nvfs = found; | |
2922 | ||
b128c09f BB |
2923 | if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops", |
2924 | &props) && 0 == nvlist_lookup_nvlist(props, | |
2925 | stream_snapname, &props)) { | |
13fe0198 | 2926 | zfs_cmd_t zc = {"\0"}; |
b128c09f | 2927 | |
428870ff | 2928 | zc.zc_cookie = B_TRUE; /* received */ |
b128c09f BB |
2929 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), |
2930 | "%s@%s", fsname, nvpair_name(snapelem)); | |
2931 | if (zcmd_write_src_nvlist(hdl, &zc, | |
2932 | props) == 0) { | |
2933 | (void) zfs_ioctl(hdl, | |
2934 | ZFS_IOC_SET_PROP, &zc); | |
2935 | zcmd_free_nvlists(&zc); | |
2936 | } | |
2937 | } | |
2938 | ||
34dc7c2f BB |
2939 | /* check for different snapname */ |
2940 | if (strcmp(nvpair_name(snapelem), | |
2941 | stream_snapname) != 0) { | |
eca7b760 IK |
2942 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
2943 | char tryname[ZFS_MAX_DATASET_NAME_LEN]; | |
34dc7c2f BB |
2944 | |
2945 | (void) snprintf(name, sizeof (name), "%s@%s", | |
2946 | fsname, nvpair_name(snapelem)); | |
2947 | (void) snprintf(tryname, sizeof (name), "%s@%s", | |
2948 | fsname, stream_snapname); | |
2949 | ||
2950 | error = recv_rename(hdl, name, tryname, | |
2951 | strlen(fsname)+1, newname, flags); | |
2952 | if (error) | |
2953 | needagain = B_TRUE; | |
2954 | else | |
2955 | progress = B_TRUE; | |
2956 | } | |
2957 | ||
2958 | if (strcmp(stream_snapname, fromsnap) == 0) | |
2959 | fromguid = thisguid; | |
2960 | } | |
2961 | ||
2962 | /* check for delete */ | |
2963 | if (stream_nvfs == NULL) { | |
330d06f9 | 2964 | if (!flags->force) |
34dc7c2f BB |
2965 | continue; |
2966 | ||
2967 | error = recv_destroy(hdl, fsname, strlen(tofs)+1, | |
2968 | newname, flags); | |
2969 | if (error) | |
2970 | needagain = B_TRUE; | |
2971 | else | |
2972 | progress = B_TRUE; | |
3df29340 | 2973 | sprintf(guidname, "%llu", |
02730c33 | 2974 | (u_longlong_t)parent_fromsnap_guid); |
7509a3d2 | 2975 | nvlist_add_boolean(deleted, guidname); |
34dc7c2f BB |
2976 | continue; |
2977 | } | |
2978 | ||
428870ff | 2979 | if (fromguid == 0) { |
330d06f9 | 2980 | if (flags->verbose) { |
428870ff BB |
2981 | (void) printf("local fs %s does not have " |
2982 | "fromsnap (%s in stream); must have " | |
2983 | "been deleted locally; ignoring\n", | |
2984 | fsname, fromsnap); | |
2985 | } | |
34dc7c2f BB |
2986 | continue; |
2987 | } | |
2988 | ||
2989 | VERIFY(0 == nvlist_lookup_string(stream_nvfs, | |
2990 | "name", &stream_fsname)); | |
2991 | VERIFY(0 == nvlist_lookup_uint64(stream_nvfs, | |
2992 | "parentfromsnap", &stream_parent_fromsnap_guid)); | |
2993 | ||
428870ff BB |
2994 | s1 = strrchr(fsname, '/'); |
2995 | s2 = strrchr(stream_fsname, '/'); | |
2996 | ||
7509a3d2 | 2997 | /* |
2998 | * Check if we're going to rename based on parent guid change | |
2999 | * and the current parent guid was also deleted. If it was then | |
3000 | * rename will fail and is likely unneeded, so avoid this and | |
3001 | * force an early retry to determine the new | |
3002 | * parent_fromsnap_guid. | |
3003 | */ | |
3004 | if (stream_parent_fromsnap_guid != 0 && | |
3005 | parent_fromsnap_guid != 0 && | |
3006 | stream_parent_fromsnap_guid != parent_fromsnap_guid) { | |
3df29340 | 3007 | sprintf(guidname, "%llu", |
02730c33 | 3008 | (u_longlong_t)parent_fromsnap_guid); |
7509a3d2 | 3009 | if (nvlist_exists(deleted, guidname)) { |
3010 | progress = B_TRUE; | |
3011 | needagain = B_TRUE; | |
3012 | goto doagain; | |
3013 | } | |
3014 | } | |
3015 | ||
428870ff BB |
3016 | /* |
3017 | * Check for rename. If the exact receive path is specified, it | |
3018 | * does not count as a rename, but we still need to check the | |
3019 | * datasets beneath it. | |
3020 | */ | |
34dc7c2f | 3021 | if ((stream_parent_fromsnap_guid != 0 && |
428870ff | 3022 | parent_fromsnap_guid != 0 && |
34dc7c2f | 3023 | stream_parent_fromsnap_guid != parent_fromsnap_guid) || |
330d06f9 | 3024 | ((flags->isprefix || strcmp(tofs, fsname) != 0) && |
428870ff | 3025 | (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) { |
34dc7c2f | 3026 | nvlist_t *parent; |
eca7b760 | 3027 | char tryname[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f BB |
3028 | |
3029 | parent = fsavl_find(local_avl, | |
3030 | stream_parent_fromsnap_guid, NULL); | |
3031 | /* | |
3032 | * NB: parent might not be found if we used the | |
3033 | * tosnap for stream_parent_fromsnap_guid, | |
3034 | * because the parent is a newly-created fs; | |
3035 | * we'll be able to rename it after we recv the | |
3036 | * new fs. | |
3037 | */ | |
3038 | if (parent != NULL) { | |
3039 | char *pname; | |
3040 | ||
3041 | VERIFY(0 == nvlist_lookup_string(parent, "name", | |
3042 | &pname)); | |
3043 | (void) snprintf(tryname, sizeof (tryname), | |
3044 | "%s%s", pname, strrchr(stream_fsname, '/')); | |
3045 | } else { | |
3046 | tryname[0] = '\0'; | |
330d06f9 | 3047 | if (flags->verbose) { |
34dc7c2f BB |
3048 | (void) printf("local fs %s new parent " |
3049 | "not found\n", fsname); | |
3050 | } | |
3051 | } | |
3052 | ||
428870ff BB |
3053 | newname[0] = '\0'; |
3054 | ||
34dc7c2f BB |
3055 | error = recv_rename(hdl, fsname, tryname, |
3056 | strlen(tofs)+1, newname, flags); | |
428870ff BB |
3057 | |
3058 | if (renamed != NULL && newname[0] != '\0') { | |
3059 | VERIFY(0 == nvlist_add_boolean(renamed, | |
3060 | newname)); | |
3061 | } | |
3062 | ||
34dc7c2f BB |
3063 | if (error) |
3064 | needagain = B_TRUE; | |
3065 | else | |
3066 | progress = B_TRUE; | |
3067 | } | |
3068 | } | |
3069 | ||
7509a3d2 | 3070 | doagain: |
34dc7c2f BB |
3071 | fsavl_destroy(local_avl); |
3072 | nvlist_free(local_nv); | |
7509a3d2 | 3073 | nvlist_free(deleted); |
34dc7c2f BB |
3074 | |
3075 | if (needagain && progress) { | |
3076 | /* do another pass to fix up temporary names */ | |
330d06f9 | 3077 | if (flags->verbose) |
34dc7c2f BB |
3078 | (void) printf("another pass:\n"); |
3079 | goto again; | |
3080 | } | |
3081 | ||
b5256303 | 3082 | return (needagain || error != 0); |
34dc7c2f BB |
3083 | } |
3084 | ||
3085 | static int | |
3086 | zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, | |
330d06f9 | 3087 | recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc, |
a3eeab2d | 3088 | char **top_zfs, int cleanup_fd, uint64_t *action_handlep, |
3089 | nvlist_t *cmdprops) | |
34dc7c2f BB |
3090 | { |
3091 | nvlist_t *stream_nv = NULL; | |
3092 | avl_tree_t *stream_avl = NULL; | |
3093 | char *fromsnap = NULL; | |
671c9354 | 3094 | char *sendsnap = NULL; |
428870ff | 3095 | char *cp; |
eca7b760 IK |
3096 | char tofs[ZFS_MAX_DATASET_NAME_LEN]; |
3097 | char sendfs[ZFS_MAX_DATASET_NAME_LEN]; | |
34dc7c2f BB |
3098 | char errbuf[1024]; |
3099 | dmu_replay_record_t drre; | |
3100 | int error; | |
3101 | boolean_t anyerr = B_FALSE; | |
3102 | boolean_t softerr = B_FALSE; | |
b5256303 | 3103 | boolean_t recursive, raw; |
34dc7c2f BB |
3104 | |
3105 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3106 | "cannot receive")); | |
3107 | ||
34dc7c2f BB |
3108 | assert(drr->drr_type == DRR_BEGIN); |
3109 | assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); | |
428870ff BB |
3110 | assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) == |
3111 | DMU_COMPOUNDSTREAM); | |
34dc7c2f BB |
3112 | |
3113 | /* | |
3114 | * Read in the nvlist from the stream. | |
3115 | */ | |
3116 | if (drr->drr_payloadlen != 0) { | |
34dc7c2f | 3117 | error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, |
330d06f9 | 3118 | &stream_nv, flags->byteswap, zc); |
34dc7c2f BB |
3119 | if (error) { |
3120 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3121 | goto out; | |
3122 | } | |
3123 | } | |
3124 | ||
428870ff BB |
3125 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == |
3126 | ENOENT); | |
b5256303 | 3127 | raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0); |
428870ff BB |
3128 | |
3129 | if (recursive && strchr(destname, '@')) { | |
3130 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3131 | "cannot specify snapshot name for multi-snapshot stream")); | |
3132 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3133 | goto out; | |
3134 | } | |
3135 | ||
34dc7c2f BB |
3136 | /* |
3137 | * Read in the end record and verify checksum. | |
3138 | */ | |
3139 | if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), | |
330d06f9 | 3140 | flags->byteswap, NULL))) |
34dc7c2f | 3141 | goto out; |
330d06f9 | 3142 | if (flags->byteswap) { |
34dc7c2f BB |
3143 | drre.drr_type = BSWAP_32(drre.drr_type); |
3144 | drre.drr_u.drr_end.drr_checksum.zc_word[0] = | |
3145 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); | |
3146 | drre.drr_u.drr_end.drr_checksum.zc_word[1] = | |
3147 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); | |
3148 | drre.drr_u.drr_end.drr_checksum.zc_word[2] = | |
3149 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); | |
3150 | drre.drr_u.drr_end.drr_checksum.zc_word[3] = | |
3151 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); | |
3152 | } | |
3153 | if (drre.drr_type != DRR_END) { | |
3154 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3155 | goto out; | |
3156 | } | |
3157 | if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { | |
3158 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3159 | "incorrect header checksum")); | |
3160 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3161 | goto out; | |
3162 | } | |
3163 | ||
3164 | (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); | |
3165 | ||
3166 | if (drr->drr_payloadlen != 0) { | |
3167 | nvlist_t *stream_fss; | |
3168 | ||
3169 | VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", | |
3170 | &stream_fss)); | |
3171 | if ((stream_avl = fsavl_create(stream_fss)) == NULL) { | |
3172 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3173 | "couldn't allocate avl tree")); | |
3174 | error = zfs_error(hdl, EZFS_NOMEM, errbuf); | |
3175 | goto out; | |
3176 | } | |
3177 | ||
4c3c6b6c | 3178 | if (fromsnap != NULL && recursive) { |
428870ff BB |
3179 | nvlist_t *renamed = NULL; |
3180 | nvpair_t *pair = NULL; | |
3181 | ||
eca7b760 | 3182 | (void) strlcpy(tofs, destname, sizeof (tofs)); |
330d06f9 | 3183 | if (flags->isprefix) { |
428870ff BB |
3184 | struct drr_begin *drrb = &drr->drr_u.drr_begin; |
3185 | int i; | |
3186 | ||
330d06f9 | 3187 | if (flags->istail) { |
428870ff BB |
3188 | cp = strrchr(drrb->drr_toname, '/'); |
3189 | if (cp == NULL) { | |
3190 | (void) strlcat(tofs, "/", | |
eca7b760 | 3191 | sizeof (tofs)); |
428870ff BB |
3192 | i = 0; |
3193 | } else { | |
3194 | i = (cp - drrb->drr_toname); | |
3195 | } | |
3196 | } else { | |
3197 | i = strcspn(drrb->drr_toname, "/@"); | |
3198 | } | |
34dc7c2f | 3199 | /* zfs_receive_one() will create_parents() */ |
428870ff | 3200 | (void) strlcat(tofs, &drrb->drr_toname[i], |
eca7b760 | 3201 | sizeof (tofs)); |
34dc7c2f BB |
3202 | *strchr(tofs, '@') = '\0'; |
3203 | } | |
428870ff | 3204 | |
4c3c6b6c | 3205 | if (!flags->dryrun && !flags->nomount) { |
428870ff BB |
3206 | VERIFY(0 == nvlist_alloc(&renamed, |
3207 | NV_UNIQUE_NAME, 0)); | |
3208 | } | |
3209 | ||
3210 | softerr = recv_incremental_replication(hdl, tofs, flags, | |
3211 | stream_nv, stream_avl, renamed); | |
3212 | ||
3213 | /* Unmount renamed filesystems before receiving. */ | |
3214 | while ((pair = nvlist_next_nvpair(renamed, | |
3215 | pair)) != NULL) { | |
3216 | zfs_handle_t *zhp; | |
3217 | prop_changelist_t *clp = NULL; | |
3218 | ||
3219 | zhp = zfs_open(hdl, nvpair_name(pair), | |
3220 | ZFS_TYPE_FILESYSTEM); | |
3221 | if (zhp != NULL) { | |
3222 | clp = changelist_gather(zhp, | |
3223 | ZFS_PROP_MOUNTPOINT, 0, 0); | |
3224 | zfs_close(zhp); | |
3225 | if (clp != NULL) { | |
3226 | softerr |= | |
3227 | changelist_prefix(clp); | |
3228 | changelist_free(clp); | |
3229 | } | |
3230 | } | |
3231 | } | |
3232 | ||
3233 | nvlist_free(renamed); | |
34dc7c2f BB |
3234 | } |
3235 | } | |
3236 | ||
428870ff BB |
3237 | /* |
3238 | * Get the fs specified by the first path in the stream (the top level | |
3239 | * specified by 'zfs send') and pass it to each invocation of | |
3240 | * zfs_receive_one(). | |
3241 | */ | |
3242 | (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname, | |
eca7b760 | 3243 | sizeof (sendfs)); |
671c9354 | 3244 | if ((cp = strchr(sendfs, '@')) != NULL) { |
428870ff | 3245 | *cp = '\0'; |
671c9354 DM |
3246 | /* |
3247 | * Find the "sendsnap", the final snapshot in a replication | |
3248 | * stream. zfs_receive_one() handles certain errors | |
3249 | * differently, depending on if the contained stream is the | |
3250 | * last one or not. | |
3251 | */ | |
3252 | sendsnap = (cp + 1); | |
3253 | } | |
34dc7c2f BB |
3254 | |
3255 | /* Finally, receive each contained stream */ | |
3256 | do { | |
3257 | /* | |
3258 | * we should figure out if it has a recoverable | |
3259 | * error, in which case do a recv_skip() and drive on. | |
3260 | * Note, if we fail due to already having this guid, | |
3261 | * zfs_receive_one() will take care of it (ie, | |
3262 | * recv_skip() and return 0). | |
3263 | */ | |
fcff0f35 | 3264 | error = zfs_receive_impl(hdl, destname, NULL, flags, fd, |
572e2857 | 3265 | sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd, |
a3eeab2d | 3266 | action_handlep, sendsnap, cmdprops); |
34dc7c2f BB |
3267 | if (error == ENODATA) { |
3268 | error = 0; | |
3269 | break; | |
3270 | } | |
3271 | anyerr |= error; | |
3272 | } while (error == 0); | |
3273 | ||
4c3c6b6c | 3274 | if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) { |
34dc7c2f BB |
3275 | /* |
3276 | * Now that we have the fs's they sent us, try the | |
3277 | * renames again. | |
3278 | */ | |
3279 | softerr = recv_incremental_replication(hdl, tofs, flags, | |
428870ff | 3280 | stream_nv, stream_avl, NULL); |
34dc7c2f BB |
3281 | } |
3282 | ||
b5256303 TC |
3283 | if (raw && softerr == 0) { |
3284 | softerr = recv_fix_encryption_heirarchy(hdl, destname, | |
3285 | stream_nv, stream_avl); | |
3286 | } | |
3287 | ||
34dc7c2f BB |
3288 | out: |
3289 | fsavl_destroy(stream_avl); | |
8a5fc748 | 3290 | nvlist_free(stream_nv); |
34dc7c2f BB |
3291 | if (softerr) |
3292 | error = -2; | |
3293 | if (anyerr) | |
3294 | error = -1; | |
3295 | return (error); | |
3296 | } | |
3297 | ||
428870ff BB |
3298 | static void |
3299 | trunc_prop_errs(int truncated) | |
3300 | { | |
3301 | ASSERT(truncated != 0); | |
3302 | ||
3303 | if (truncated == 1) | |
3304 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
3305 | "1 more property could not be set\n")); | |
3306 | else | |
3307 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
3308 | "%d more properties could not be set\n"), truncated); | |
3309 | } | |
3310 | ||
34dc7c2f BB |
3311 | static int |
3312 | recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) | |
3313 | { | |
3314 | dmu_replay_record_t *drr; | |
f1512ee6 | 3315 | void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE); |
428870ff BB |
3316 | char errbuf[1024]; |
3317 | ||
3318 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3319 | "cannot receive:")); | |
34dc7c2f BB |
3320 | |
3321 | /* XXX would be great to use lseek if possible... */ | |
3322 | drr = buf; | |
3323 | ||
3324 | while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), | |
3325 | byteswap, NULL) == 0) { | |
3326 | if (byteswap) | |
3327 | drr->drr_type = BSWAP_32(drr->drr_type); | |
3328 | ||
3329 | switch (drr->drr_type) { | |
3330 | case DRR_BEGIN: | |
428870ff | 3331 | if (drr->drr_payloadlen != 0) { |
47dfff3b MA |
3332 | (void) recv_read(hdl, fd, buf, |
3333 | drr->drr_payloadlen, B_FALSE, NULL); | |
428870ff | 3334 | } |
34dc7c2f BB |
3335 | break; |
3336 | ||
3337 | case DRR_END: | |
3338 | free(buf); | |
3339 | return (0); | |
3340 | ||
3341 | case DRR_OBJECT: | |
3342 | if (byteswap) { | |
3343 | drr->drr_u.drr_object.drr_bonuslen = | |
3344 | BSWAP_32(drr->drr_u.drr_object. | |
3345 | drr_bonuslen); | |
3346 | } | |
3347 | (void) recv_read(hdl, fd, buf, | |
3348 | P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8), | |
3349 | B_FALSE, NULL); | |
3350 | break; | |
3351 | ||
3352 | case DRR_WRITE: | |
3353 | if (byteswap) { | |
2aa34383 DK |
3354 | drr->drr_u.drr_write.drr_logical_size = |
3355 | BSWAP_64( | |
3356 | drr->drr_u.drr_write.drr_logical_size); | |
3357 | drr->drr_u.drr_write.drr_compressed_size = | |
3358 | BSWAP_64( | |
3359 | drr->drr_u.drr_write.drr_compressed_size); | |
34dc7c2f | 3360 | } |
2aa34383 DK |
3361 | uint64_t payload_size = |
3362 | DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write); | |
34dc7c2f | 3363 | (void) recv_read(hdl, fd, buf, |
2aa34383 | 3364 | payload_size, B_FALSE, NULL); |
34dc7c2f | 3365 | break; |
428870ff BB |
3366 | case DRR_SPILL: |
3367 | if (byteswap) { | |
9f8026c8 | 3368 | drr->drr_u.drr_spill.drr_length = |
428870ff BB |
3369 | BSWAP_64(drr->drr_u.drr_spill.drr_length); |
3370 | } | |
3371 | (void) recv_read(hdl, fd, buf, | |
3372 | drr->drr_u.drr_spill.drr_length, B_FALSE, NULL); | |
3373 | break; | |
9b67f605 MA |
3374 | case DRR_WRITE_EMBEDDED: |
3375 | if (byteswap) { | |
3376 | drr->drr_u.drr_write_embedded.drr_psize = | |
3377 | BSWAP_32(drr->drr_u.drr_write_embedded. | |
3378 | drr_psize); | |
3379 | } | |
3380 | (void) recv_read(hdl, fd, buf, | |
3381 | P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize, | |
3382 | 8), B_FALSE, NULL); | |
3383 | break; | |
428870ff | 3384 | case DRR_WRITE_BYREF: |
34dc7c2f BB |
3385 | case DRR_FREEOBJECTS: |
3386 | case DRR_FREE: | |
3387 | break; | |
3388 | ||
3389 | default: | |
428870ff BB |
3390 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
3391 | "invalid record type")); | |
fad5fb01 | 3392 | free(buf); |
428870ff | 3393 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); |
34dc7c2f BB |
3394 | } |
3395 | } | |
3396 | ||
3397 | free(buf); | |
3398 | return (-1); | |
3399 | } | |
3400 | ||
47dfff3b MA |
3401 | static void |
3402 | recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap, | |
3403 | boolean_t resumable) | |
3404 | { | |
eca7b760 | 3405 | char target_fs[ZFS_MAX_DATASET_NAME_LEN]; |
47dfff3b MA |
3406 | |
3407 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3408 | "checksum mismatch or incomplete stream")); | |
3409 | ||
3410 | if (!resumable) | |
3411 | return; | |
3412 | (void) strlcpy(target_fs, target_snap, sizeof (target_fs)); | |
3413 | *strchr(target_fs, '@') = '\0'; | |
3414 | zfs_handle_t *zhp = zfs_open(hdl, target_fs, | |
3415 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); | |
3416 | if (zhp == NULL) | |
3417 | return; | |
3418 | ||
3419 | char token_buf[ZFS_MAXPROPLEN]; | |
3420 | int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, | |
3421 | token_buf, sizeof (token_buf), | |
3422 | NULL, NULL, 0, B_TRUE); | |
3423 | if (error == 0) { | |
3424 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3425 | "checksum mismatch or incomplete stream.\n" | |
3426 | "Partially received snapshot is saved.\n" | |
3427 | "A resuming stream can be generated on the sending " | |
3428 | "system by running:\n" | |
3429 | " zfs send -t %s"), | |
3430 | token_buf); | |
3431 | } | |
3432 | zfs_close(zhp); | |
3433 | } | |
3434 | ||
a3eeab2d | 3435 | /* |
3436 | * Prepare a new nvlist of properties that are to override (-o) or be excluded | |
3437 | * (-x) from the received dataset | |
3438 | * recvprops: received properties from the send stream | |
3439 | * cmdprops: raw input properties from command line | |
3440 | * origprops: properties, both locally-set and received, currently set on the | |
3441 | * target dataset if it exists, NULL otherwise. | |
3442 | * oxprops: valid output override (-o) and excluded (-x) properties | |
3443 | */ | |
3444 | static int | |
3445 | zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type, boolean_t zoned, | |
3446 | boolean_t recursive, boolean_t toplevel, nvlist_t *recvprops, | |
3447 | nvlist_t *cmdprops, nvlist_t *origprops, nvlist_t **oxprops, | |
3448 | const char *errbuf) | |
3449 | { | |
3450 | nvpair_t *nvp; | |
3451 | nvlist_t *oprops, *voprops; | |
3452 | zfs_handle_t *zhp = NULL; | |
3453 | zpool_handle_t *zpool_hdl = NULL; | |
3454 | int ret = 0; | |
3455 | ||
3456 | if (nvlist_empty(cmdprops)) | |
3457 | return (0); /* No properties to override or exclude */ | |
3458 | ||
3459 | *oxprops = fnvlist_alloc(); | |
3460 | oprops = fnvlist_alloc(); | |
3461 | ||
3462 | /* | |
3463 | * first iteration: process excluded (-x) properties now and gather | |
3464 | * added (-o) properties to be later processed by zfs_valid_proplist() | |
3465 | */ | |
3466 | nvp = NULL; | |
3467 | while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) { | |
3468 | const char *name = nvpair_name(nvp); | |
3469 | zfs_prop_t prop = zfs_name_to_prop(name); | |
3470 | ||
3471 | /* "origin" is processed separately, don't handle it here */ | |
3472 | if (prop == ZFS_PROP_ORIGIN) | |
3473 | continue; | |
3474 | ||
3475 | /* | |
3476 | * we're trying to override or exclude a property that does not | |
3477 | * make sense for this type of dataset, but we don't want to | |
3478 | * fail if the receive is recursive: this comes in handy when | |
3479 | * the send stream contains, for instance, a child ZVOL and | |
3480 | * we're trying to receive it with "-o atime=on" | |
3481 | */ | |
3482 | if (!zfs_prop_valid_for_type(prop, type, B_FALSE) && | |
3483 | !zfs_prop_user(name)) { | |
3484 | if (recursive) | |
3485 | continue; | |
3486 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3487 | "property '%s' does not apply to datasets of this " | |
3488 | "type"), name); | |
3489 | ret = zfs_error(hdl, EZFS_BADPROP, errbuf); | |
3490 | goto error; | |
3491 | } | |
3492 | ||
3493 | switch (nvpair_type(nvp)) { | |
3494 | case DATA_TYPE_BOOLEAN: /* -x property */ | |
3495 | /* | |
3496 | * DATA_TYPE_BOOLEAN is the way we're asked to "exclude" | |
3497 | * a property: this is done by forcing an explicit | |
3498 | * inherit on the destination so the effective value is | |
3499 | * not the one we received from the send stream. | |
3500 | * We do this only if the property is not already | |
3501 | * locally-set, in which case its value will take | |
3502 | * priority over the received anyway. | |
3503 | */ | |
3504 | if (nvlist_exists(origprops, name)) { | |
3505 | nvlist_t *attrs; | |
3506 | ||
3507 | attrs = fnvlist_lookup_nvlist(origprops, name); | |
3508 | if (strcmp(fnvlist_lookup_string(attrs, | |
3509 | ZPROP_SOURCE), ZPROP_SOURCE_VAL_RECVD) != 0) | |
3510 | continue; | |
3511 | } | |
3512 | /* | |
3513 | * We can't force an explicit inherit on non-inheritable | |
3514 | * properties: if we're asked to exclude this kind of | |
3515 | * values we remove them from "recvprops" input nvlist. | |
3516 | */ | |
3517 | if (!zfs_prop_inheritable(prop) && | |
3518 | !zfs_prop_user(name) && /* can be inherited too */ | |
3519 | nvlist_exists(recvprops, name)) | |
3520 | fnvlist_remove(recvprops, name); | |
3521 | else | |
3522 | fnvlist_add_nvpair(*oxprops, nvp); | |
3523 | break; | |
3524 | case DATA_TYPE_STRING: /* -o property=value */ | |
3525 | fnvlist_add_nvpair(oprops, nvp); | |
3526 | break; | |
3527 | default: | |
3528 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3529 | "property '%s' must be a string or boolean"), name); | |
3530 | ret = zfs_error(hdl, EZFS_BADPROP, errbuf); | |
3531 | goto error; | |
3532 | } | |
3533 | } | |
3534 | ||
3535 | if (toplevel) { | |
3536 | /* convert override strings properties to native */ | |
3537 | if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET, | |
b5256303 | 3538 | oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) { |
a3eeab2d | 3539 | ret = zfs_error(hdl, EZFS_BADPROP, errbuf); |
3540 | goto error; | |
3541 | } | |
3542 | ||
3543 | /* second pass: process "-o" properties */ | |
3544 | fnvlist_merge(*oxprops, voprops); | |
3545 | fnvlist_free(voprops); | |
3546 | } else { | |
3547 | /* override props on child dataset are inherited */ | |
3548 | nvp = NULL; | |
3549 | while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) { | |
3550 | const char *name = nvpair_name(nvp); | |
3551 | fnvlist_add_boolean(*oxprops, name); | |
3552 | } | |
3553 | } | |
3554 | ||
3555 | error: | |
3556 | fnvlist_free(oprops); | |
3557 | return (ret); | |
3558 | } | |
3559 | ||
34dc7c2f BB |
3560 | /* |
3561 | * Restores a backup of tosnap from the file descriptor specified by infd. | |
3562 | */ | |
3563 | static int | |
3564 | zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, | |
fcff0f35 PD |
3565 | const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr, |
3566 | dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv, | |
3567 | avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd, | |
a3eeab2d | 3568 | uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops) |
34dc7c2f | 3569 | { |
34dc7c2f | 3570 | time_t begin_time; |
428870ff | 3571 | int ioctl_err, ioctl_errno, err; |
34dc7c2f BB |
3572 | char *cp; |
3573 | struct drr_begin *drrb = &drr->drr_u.drr_begin; | |
3574 | char errbuf[1024]; | |
428870ff | 3575 | const char *chopprefix; |
34dc7c2f BB |
3576 | boolean_t newfs = B_FALSE; |
3577 | boolean_t stream_wantsnewfs; | |
43e52edd BB |
3578 | boolean_t newprops = B_FALSE; |
3579 | uint64_t read_bytes = 0; | |
3580 | uint64_t errflags = 0; | |
34dc7c2f BB |
3581 | uint64_t parent_snapguid = 0; |
3582 | prop_changelist_t *clp = NULL; | |
b128c09f | 3583 | nvlist_t *snapprops_nvlist = NULL; |
428870ff | 3584 | zprop_errflags_t prop_errflags; |
43e52edd | 3585 | nvlist_t *prop_errors = NULL; |
428870ff | 3586 | boolean_t recursive; |
671c9354 | 3587 | char *snapname = NULL; |
43e52edd BB |
3588 | char destsnap[MAXPATHLEN * 2]; |
3589 | char origin[MAXNAMELEN]; | |
3590 | char name[MAXPATHLEN]; | |
b5256303 | 3591 | char tmp_keylocation[MAXNAMELEN]; |
a3eeab2d | 3592 | nvlist_t *rcvprops = NULL; /* props received from the send stream */ |
3593 | nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */ | |
3594 | nvlist_t *origprops = NULL; /* original props (if destination exists) */ | |
3595 | zfs_type_t type; | |
3596 | boolean_t toplevel; | |
3597 | boolean_t zoned = B_FALSE; | |
34dc7c2f BB |
3598 | |
3599 | begin_time = time(NULL); | |
43e52edd | 3600 | bzero(origin, MAXNAMELEN); |
b5256303 | 3601 | bzero(tmp_keylocation, MAXNAMELEN); |
34dc7c2f BB |
3602 | |
3603 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3604 | "cannot receive")); | |
3605 | ||
428870ff BB |
3606 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == |
3607 | ENOENT); | |
3608 | ||
34dc7c2f | 3609 | if (stream_avl != NULL) { |
b5256303 | 3610 | char *keylocation = NULL; |
48f783de | 3611 | nvlist_t *lookup = NULL; |
b128c09f BB |
3612 | nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, |
3613 | &snapname); | |
34dc7c2f BB |
3614 | |
3615 | (void) nvlist_lookup_uint64(fs, "parentfromsnap", | |
3616 | &parent_snapguid); | |
a3eeab2d | 3617 | err = nvlist_lookup_nvlist(fs, "props", &rcvprops); |
43e52edd | 3618 | if (err) { |
a3eeab2d | 3619 | VERIFY(0 == nvlist_alloc(&rcvprops, NV_UNIQUE_NAME, 0)); |
43e52edd BB |
3620 | newprops = B_TRUE; |
3621 | } | |
34dc7c2f | 3622 | |
b5256303 TC |
3623 | /* |
3624 | * The keylocation property may only be set on encryption roots, | |
3625 | * but this dataset might not become an encryption root until | |
3626 | * recv_fix_encryption_heirarchy() is called. That function | |
3627 | * will fixup the keylocation anyway, so we temporarily unset | |
3628 | * the keylocation for now to avoid any errors from the receive | |
3629 | * ioctl. | |
3630 | */ | |
3631 | err = nvlist_lookup_string(rcvprops, | |
3632 | zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation); | |
3633 | if (err == 0) { | |
3634 | strcpy(tmp_keylocation, keylocation); | |
3635 | (void) nvlist_remove_all(rcvprops, | |
3636 | zfs_prop_to_name(ZFS_PROP_KEYLOCATION)); | |
3637 | } | |
3638 | ||
330d06f9 | 3639 | if (flags->canmountoff) { |
a3eeab2d | 3640 | VERIFY(0 == nvlist_add_uint64(rcvprops, |
34dc7c2f BB |
3641 | zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0)); |
3642 | } | |
48f783de | 3643 | if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) { |
3644 | VERIFY(0 == nvlist_lookup_nvlist(lookup, | |
3645 | snapname, &snapprops_nvlist)); | |
3646 | } | |
34dc7c2f BB |
3647 | } |
3648 | ||
428870ff BB |
3649 | cp = NULL; |
3650 | ||
34dc7c2f BB |
3651 | /* |
3652 | * Determine how much of the snapshot name stored in the stream | |
3653 | * we are going to tack on to the name they specified on the | |
3654 | * command line, and how much we are going to chop off. | |
3655 | * | |
3656 | * If they specified a snapshot, chop the entire name stored in | |
3657 | * the stream. | |
3658 | */ | |
330d06f9 | 3659 | if (flags->istail) { |
428870ff BB |
3660 | /* |
3661 | * A filesystem was specified with -e. We want to tack on only | |
3662 | * the tail of the sent snapshot path. | |
3663 | */ | |
3664 | if (strchr(tosnap, '@')) { | |
3665 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
3666 | "argument - snapshot not allowed with -e")); | |
43e52edd BB |
3667 | err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); |
3668 | goto out; | |
428870ff BB |
3669 | } |
3670 | ||
3671 | chopprefix = strrchr(sendfs, '/'); | |
3672 | ||
3673 | if (chopprefix == NULL) { | |
3674 | /* | |
3675 | * The tail is the poolname, so we need to | |
3676 | * prepend a path separator. | |
3677 | */ | |
3678 | int len = strlen(drrb->drr_toname); | |
3679 | cp = malloc(len + 2); | |
3680 | cp[0] = '/'; | |
3681 | (void) strcpy(&cp[1], drrb->drr_toname); | |
3682 | chopprefix = cp; | |
3683 | } else { | |
3684 | chopprefix = drrb->drr_toname + (chopprefix - sendfs); | |
3685 | } | |
330d06f9 | 3686 | } else if (flags->isprefix) { |
34dc7c2f | 3687 | /* |
428870ff BB |
3688 | * A filesystem was specified with -d. We want to tack on |
3689 | * everything but the first element of the sent snapshot path | |
3690 | * (all but the pool name). | |
34dc7c2f BB |
3691 | */ |
3692 | if (strchr(tosnap, '@')) { | |
3693 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
3694 | "argument - snapshot not allowed with -d")); | |
43e52edd BB |
3695 | err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); |
3696 | goto out; | |
34dc7c2f | 3697 | } |
428870ff BB |
3698 | |
3699 | chopprefix = strchr(drrb->drr_toname, '/'); | |
3700 | if (chopprefix == NULL) | |
3701 | chopprefix = strchr(drrb->drr_toname, '@'); | |
34dc7c2f BB |
3702 | } else if (strchr(tosnap, '@') == NULL) { |
3703 | /* | |
428870ff BB |
3704 | * If a filesystem was specified without -d or -e, we want to |
3705 | * tack on everything after the fs specified by 'zfs send'. | |
34dc7c2f | 3706 | */ |
428870ff BB |
3707 | chopprefix = drrb->drr_toname + strlen(sendfs); |
3708 | } else { | |
3709 | /* A snapshot was specified as an exact path (no -d or -e). */ | |
3710 | if (recursive) { | |
3711 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3712 | "cannot specify snapshot name for multi-snapshot " | |
3713 | "stream")); | |
43e52edd BB |
3714 | err = zfs_error(hdl, EZFS_BADSTREAM, errbuf); |
3715 | goto out; | |
428870ff BB |
3716 | } |
3717 | chopprefix = drrb->drr_toname + strlen(drrb->drr_toname); | |
34dc7c2f | 3718 | } |
428870ff BB |
3719 | |
3720 | ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname); | |
3721 | ASSERT(chopprefix > drrb->drr_toname); | |
3722 | ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname)); | |
3723 | ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' || | |
3724 | chopprefix[0] == '\0'); | |
34dc7c2f BB |
3725 | |
3726 | /* | |
43e52edd | 3727 | * Determine name of destination snapshot. |
34dc7c2f | 3728 | */ |
45cb520b | 3729 | (void) strlcpy(destsnap, tosnap, sizeof (destsnap)); |
43e52edd | 3730 | (void) strlcat(destsnap, chopprefix, sizeof (destsnap)); |
428870ff | 3731 | free(cp); |
43e52edd BB |
3732 | if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) { |
3733 | err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf); | |
3734 | goto out; | |
34dc7c2f BB |
3735 | } |
3736 | ||
3737 | /* | |
43e52edd | 3738 | * Determine the name of the origin snapshot. |
34dc7c2f | 3739 | */ |
160af771 GM |
3740 | if (originsnap) { |
3741 | (void) strncpy(origin, originsnap, sizeof (origin)); | |
3742 | if (flags->verbose) | |
3743 | (void) printf("using provided clone origin %s\n", | |
3744 | origin); | |
3745 | } else if (drrb->drr_flags & DRR_FLAG_CLONE) { | |
43e52edd BB |
3746 | if (guid_to_name(hdl, destsnap, |
3747 | drrb->drr_fromguid, B_FALSE, origin) != 0) { | |
34dc7c2f BB |
3748 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
3749 | "local origin for clone %s does not exist"), | |
43e52edd BB |
3750 | destsnap); |
3751 | err = zfs_error(hdl, EZFS_NOENT, errbuf); | |
3752 | goto out; | |
34dc7c2f | 3753 | } |
330d06f9 | 3754 | if (flags->verbose) |
43e52edd | 3755 | (void) printf("found clone origin %s\n", origin); |
34dc7c2f BB |
3756 | } |
3757 | ||
47dfff3b MA |
3758 | boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & |
3759 | DMU_BACKUP_FEATURE_RESUMING; | |
b5256303 TC |
3760 | boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & |
3761 | DMU_BACKUP_FEATURE_RAW; | |
9b840763 TC |
3762 | boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & |
3763 | DMU_BACKUP_FEATURE_EMBED_DATA; | |
b8864a23 | 3764 | stream_wantsnewfs = (drrb->drr_fromguid == 0 || |
47dfff3b | 3765 | (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming; |
34dc7c2f BB |
3766 | |
3767 | if (stream_wantsnewfs) { | |
3768 | /* | |
3769 | * if the parent fs does not exist, look for it based on | |
3770 | * the parent snap GUID | |
3771 | */ | |
3772 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3773 | "cannot receive new filesystem stream")); | |
3774 | ||
43e52edd BB |
3775 | (void) strcpy(name, destsnap); |
3776 | cp = strrchr(name, '/'); | |
34dc7c2f BB |
3777 | if (cp) |
3778 | *cp = '\0'; | |
3779 | if (cp && | |
43e52edd | 3780 | !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) { |
eca7b760 | 3781 | char suffix[ZFS_MAX_DATASET_NAME_LEN]; |
43e52edd BB |
3782 | (void) strcpy(suffix, strrchr(destsnap, '/')); |
3783 | if (guid_to_name(hdl, name, parent_snapguid, | |
3784 | B_FALSE, destsnap) == 0) { | |
3785 | *strchr(destsnap, '@') = '\0'; | |
3786 | (void) strcat(destsnap, suffix); | |
34dc7c2f BB |
3787 | } |
3788 | } | |
3789 | } else { | |
3790 | /* | |
3791 | * if the fs does not exist, look for it based on the | |
3792 | * fromsnap GUID | |
3793 | */ | |
3794 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3795 | "cannot receive incremental stream")); | |
3796 | ||
43e52edd BB |
3797 | (void) strcpy(name, destsnap); |
3798 | *strchr(name, '@') = '\0'; | |
34dc7c2f | 3799 | |
428870ff BB |
3800 | /* |
3801 | * If the exact receive path was specified and this is the | |
3802 | * topmost path in the stream, then if the fs does not exist we | |
3803 | * should look no further. | |
3804 | */ | |
330d06f9 | 3805 | if ((flags->isprefix || (*(chopprefix = drrb->drr_toname + |
428870ff | 3806 | strlen(sendfs)) != '\0' && *chopprefix != '@')) && |
43e52edd | 3807 | !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) { |
eca7b760 | 3808 | char snap[ZFS_MAX_DATASET_NAME_LEN]; |
43e52edd BB |
3809 | (void) strcpy(snap, strchr(destsnap, '@')); |
3810 | if (guid_to_name(hdl, name, drrb->drr_fromguid, | |
3811 | B_FALSE, destsnap) == 0) { | |
3812 | *strchr(destsnap, '@') = '\0'; | |
3813 | (void) strcat(destsnap, snap); | |
34dc7c2f BB |
3814 | } |
3815 | } | |
3816 | } | |
3817 | ||
43e52edd BB |
3818 | (void) strcpy(name, destsnap); |
3819 | *strchr(name, '@') = '\0'; | |
34dc7c2f | 3820 | |
43e52edd BB |
3821 | if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) { |
3822 | zfs_cmd_t zc = {"\0"}; | |
34dc7c2f | 3823 | zfs_handle_t *zhp; |
428870ff | 3824 | |
43e52edd BB |
3825 | (void) strcpy(zc.zc_name, name); |
3826 | ||
34dc7c2f | 3827 | /* |
47dfff3b MA |
3828 | * Destination fs exists. It must be one of these cases: |
3829 | * - an incremental send stream | |
3830 | * - the stream specifies a new fs (full stream or clone) | |
3831 | * and they want us to blow away the existing fs (and | |
3832 | * have therefore specified -F and removed any snapshots) | |
3833 | * - we are resuming a failed receive. | |
34dc7c2f | 3834 | */ |
34dc7c2f | 3835 | if (stream_wantsnewfs) { |
330d06f9 | 3836 | if (!flags->force) { |
34dc7c2f BB |
3837 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
3838 | "destination '%s' exists\n" | |
43e52edd BB |
3839 | "must specify -F to overwrite it"), name); |
3840 | err = zfs_error(hdl, EZFS_EXISTS, errbuf); | |
3841 | goto out; | |
34dc7c2f BB |
3842 | } |
3843 | if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, | |
3844 | &zc) == 0) { | |
34dc7c2f BB |
3845 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
3846 | "destination has snapshots (eg. %s)\n" | |
3847 | "must destroy them to overwrite it"), | |
43e52edd BB |
3848 | name); |
3849 | err = zfs_error(hdl, EZFS_EXISTS, errbuf); | |
3850 | goto out; | |
34dc7c2f BB |
3851 | } |
3852 | } | |
3853 | ||
43e52edd | 3854 | if ((zhp = zfs_open(hdl, name, |
34dc7c2f | 3855 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { |
43e52edd BB |
3856 | err = -1; |
3857 | goto out; | |
34dc7c2f BB |
3858 | } |
3859 | ||
3860 | if (stream_wantsnewfs && | |
3861 | zhp->zfs_dmustats.dds_origin[0]) { | |
34dc7c2f BB |
3862 | zfs_close(zhp); |
3863 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3864 | "destination '%s' is a clone\n" | |
43e52edd BB |
3865 | "must destroy it to overwrite it"), name); |
3866 | err = zfs_error(hdl, EZFS_EXISTS, errbuf); | |
3867 | goto out; | |
34dc7c2f BB |
3868 | } |
3869 | ||
b5256303 TC |
3870 | /* |
3871 | * zfs recv -F cant be used to blow away an existing | |
3872 | * encrypted filesystem. This is because it would require | |
3873 | * the dsl dir to point to the the new key (or lack of a | |
3874 | * key) and the old key at the same time. The -F flag may | |
3875 | * still be used for deleting intermediate snapshots that | |
3876 | * would otherwise prevent the receive from working. | |
3877 | */ | |
3878 | if (stream_wantsnewfs && flags->force && | |
3879 | zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != | |
3880 | ZIO_CRYPT_OFF) { | |
3881 | zfs_close(zhp); | |
3882 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3883 | "zfs receive -F cannot be used to " | |
3884 | "destroy an encrypted filesystem")); | |
3885 | err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3886 | goto out; | |
3887 | } | |
3888 | ||
3889 | ||
330d06f9 | 3890 | if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && |
34dc7c2f BB |
3891 | stream_wantsnewfs) { |
3892 | /* We can't do online recv in this case */ | |
b128c09f | 3893 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0); |
34dc7c2f | 3894 | if (clp == NULL) { |
45d1cae3 | 3895 | zfs_close(zhp); |
43e52edd BB |
3896 | err = -1; |
3897 | goto out; | |
34dc7c2f BB |
3898 | } |
3899 | if (changelist_prefix(clp) != 0) { | |
3900 | changelist_free(clp); | |
45d1cae3 | 3901 | zfs_close(zhp); |
43e52edd BB |
3902 | err = -1; |
3903 | goto out; | |
34dc7c2f BB |
3904 | } |
3905 | } | |
47dfff3b MA |
3906 | |
3907 | /* | |
3908 | * If we are resuming a newfs, set newfs here so that we will | |
3909 | * mount it if the recv succeeds this time. We can tell | |
3910 | * that it was a newfs on the first recv because the fs | |
3911 | * itself will be inconsistent (if the fs existed when we | |
3912 | * did the first recv, we would have received it into | |
3913 | * .../%recv). | |
3914 | */ | |
3915 | if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT)) | |
3916 | newfs = B_TRUE; | |
3917 | ||
a3eeab2d | 3918 | /* we want to know if we're zoned when validating -o|-x props */ |
3919 | zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); | |
3920 | ||
3921 | /* gather existing properties on destination */ | |
3922 | origprops = fnvlist_alloc(); | |
3923 | fnvlist_merge(origprops, zhp->zfs_props); | |
3924 | fnvlist_merge(origprops, zhp->zfs_user_props); | |
3925 | ||
34dc7c2f BB |
3926 | zfs_close(zhp); |
3927 | } else { | |
b5256303 TC |
3928 | zfs_handle_t *zhp; |
3929 | ||
34dc7c2f BB |
3930 | /* |
3931 | * Destination filesystem does not exist. Therefore we better | |
3932 | * be creating a new filesystem (either from a full backup, or | |
3933 | * a clone). It would therefore be invalid if the user | |
3934 | * specified only the pool name (i.e. if the destination name | |
3935 | * contained no slash character). | |
3936 | */ | |
a64f903b GN |
3937 | cp = strrchr(name, '/'); |
3938 | ||
3939 | if (!stream_wantsnewfs || cp == NULL) { | |
34dc7c2f | 3940 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
43e52edd BB |
3941 | "destination '%s' does not exist"), name); |
3942 | err = zfs_error(hdl, EZFS_NOENT, errbuf); | |
3943 | goto out; | |
34dc7c2f BB |
3944 | } |
3945 | ||
3946 | /* | |
3947 | * Trim off the final dataset component so we perform the | |
3948 | * recvbackup ioctl to the filesystems's parent. | |
3949 | */ | |
3950 | *cp = '\0'; | |
3951 | ||
330d06f9 | 3952 | if (flags->isprefix && !flags->istail && !flags->dryrun && |
43e52edd BB |
3953 | create_parents(hdl, destsnap, strlen(tosnap)) != 0) { |
3954 | err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3955 | goto out; | |
34dc7c2f BB |
3956 | } |
3957 | ||
b5256303 TC |
3958 | /* |
3959 | * It is invalid to receive a properties stream that was | |
3960 | * unencrypted on the send side as a child of an encrypted | |
3961 | * parent. Technically there is nothing preventing this, but | |
3962 | * it would mean that the encryption=off property which is | |
3963 | * locally set on the send side would not be received correctly. | |
3964 | * We can infer encryption=off if the stream is not raw and | |
3965 | * properties were included since the send side will only ever | |
3966 | * send the encryption property in a raw nvlist header. | |
3967 | */ | |
3968 | if (!raw && rcvprops != NULL) { | |
3969 | uint64_t crypt; | |
3970 | ||
3971 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
3972 | if (zhp == NULL) { | |
3973 | err = zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3974 | goto out; | |
3975 | } | |
3976 | ||
3977 | crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION); | |
3978 | zfs_close(zhp); | |
3979 | ||
3980 | if (crypt != ZIO_CRYPT_OFF) { | |
3981 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3982 | "parent '%s' must not be encrypted to " | |
3983 | "receive unenecrypted property"), name); | |
3984 | err = zfs_error(hdl, EZFS_BADPROP, errbuf); | |
3985 | goto out; | |
3986 | } | |
3987 | } | |
3988 | ||
34dc7c2f | 3989 | newfs = B_TRUE; |
b5256303 | 3990 | *cp = '/'; |
34dc7c2f BB |
3991 | } |
3992 | ||
330d06f9 | 3993 | if (flags->verbose) { |
34dc7c2f | 3994 | (void) printf("%s %s stream of %s into %s\n", |
330d06f9 | 3995 | flags->dryrun ? "would receive" : "receiving", |
34dc7c2f | 3996 | drrb->drr_fromguid ? "incremental" : "full", |
43e52edd | 3997 | drrb->drr_toname, destsnap); |
34dc7c2f BB |
3998 | (void) fflush(stdout); |
3999 | } | |
4000 | ||
330d06f9 | 4001 | if (flags->dryrun) { |
43e52edd BB |
4002 | err = recv_skip(hdl, infd, flags->byteswap); |
4003 | goto out; | |
34dc7c2f BB |
4004 | } |
4005 | ||
a3eeab2d | 4006 | toplevel = chopprefix[0] != '/'; |
4007 | if (drrb->drr_type == DMU_OST_ZVOL) { | |
4008 | type = ZFS_TYPE_VOLUME; | |
4009 | } else if (drrb->drr_type == DMU_OST_ZFS) { | |
4010 | type = ZFS_TYPE_FILESYSTEM; | |
4011 | } else { | |
4012 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4013 | "invalid record type: 0x%d"), drrb->drr_type); | |
4014 | err = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
4015 | goto out; | |
4016 | } | |
4017 | if ((err = zfs_setup_cmdline_props(hdl, type, zoned, recursive, | |
4018 | toplevel, rcvprops, cmdprops, origprops, &oxprops, errbuf)) != 0) | |
4019 | goto out; | |
4020 | ||
4021 | err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops, oxprops, | |
b5256303 | 4022 | origin, flags->force, flags->resumable, raw, infd, drr_noswap, |
a3eeab2d | 4023 | cleanup_fd, &read_bytes, &errflags, action_handlep, &prop_errors); |
43e52edd BB |
4024 | ioctl_errno = ioctl_err; |
4025 | prop_errflags = errflags; | |
428870ff BB |
4026 | |
4027 | if (err == 0) { | |
428870ff BB |
4028 | nvpair_t *prop_err = NULL; |
4029 | ||
4030 | while ((prop_err = nvlist_next_nvpair(prop_errors, | |
4031 | prop_err)) != NULL) { | |
4032 | char tbuf[1024]; | |
4033 | zfs_prop_t prop; | |
4034 | int intval; | |
4035 | ||
4036 | prop = zfs_name_to_prop(nvpair_name(prop_err)); | |
4037 | (void) nvpair_value_int32(prop_err, &intval); | |
4038 | if (strcmp(nvpair_name(prop_err), | |
4039 | ZPROP_N_MORE_ERRORS) == 0) { | |
4040 | trunc_prop_errs(intval); | |
4041 | break; | |
671c9354 DM |
4042 | } else if (snapname == NULL || finalsnap == NULL || |
4043 | strcmp(finalsnap, snapname) == 0 || | |
4044 | strcmp(nvpair_name(prop_err), | |
4045 | zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) { | |
4046 | /* | |
4047 | * Skip the special case of, for example, | |
4048 | * "refquota", errors on intermediate | |
4049 | * snapshots leading up to a final one. | |
4050 | * That's why we have all of the checks above. | |
4051 | * | |
4052 | * See zfs_ioctl.c's extract_delay_props() for | |
4053 | * a list of props which can fail on | |
4054 | * intermediate snapshots, but shouldn't | |
4055 | * affect the overall receive. | |
4056 | */ | |
428870ff BB |
4057 | (void) snprintf(tbuf, sizeof (tbuf), |
4058 | dgettext(TEXT_DOMAIN, | |
4059 | "cannot receive %s property on %s"), | |
43e52edd | 4060 | nvpair_name(prop_err), name); |
428870ff BB |
4061 | zfs_setprop_error(hdl, prop, intval, tbuf); |
4062 | } | |
4063 | } | |
428870ff BB |
4064 | } |
4065 | ||
b128c09f | 4066 | if (err == 0 && snapprops_nvlist) { |
43e52edd | 4067 | zfs_cmd_t zc = {"\0"}; |
b128c09f | 4068 | |
43e52edd BB |
4069 | (void) strcpy(zc.zc_name, destsnap); |
4070 | zc.zc_cookie = B_TRUE; /* received */ | |
4071 | if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) { | |
4072 | (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); | |
4073 | zcmd_free_nvlists(&zc); | |
b128c09f BB |
4074 | } |
4075 | } | |
4076 | ||
428870ff | 4077 | if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) { |
34dc7c2f BB |
4078 | /* |
4079 | * It may be that this snapshot already exists, | |
4080 | * in which case we want to consume & ignore it | |
4081 | * rather than failing. | |
4082 | */ | |
4083 | avl_tree_t *local_avl; | |
4084 | nvlist_t *local_nv, *fs; | |
43e52edd | 4085 | cp = strchr(destsnap, '@'); |
34dc7c2f BB |
4086 | |
4087 | /* | |
4088 | * XXX Do this faster by just iterating over snaps in | |
4089 | * this fs. Also if zc_value does not exist, we will | |
4090 | * get a strange "does not exist" error message. | |
4091 | */ | |
4092 | *cp = '\0'; | |
b5256303 | 4093 | if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE, |
66356240 | 4094 | B_FALSE, &local_nv, &local_avl) == 0) { |
34dc7c2f BB |
4095 | *cp = '@'; |
4096 | fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); | |
4097 | fsavl_destroy(local_avl); | |
4098 | nvlist_free(local_nv); | |
4099 | ||
4100 | if (fs != NULL) { | |
330d06f9 | 4101 | if (flags->verbose) { |
34dc7c2f | 4102 | (void) printf("snap %s already exists; " |
43e52edd | 4103 | "ignoring\n", destsnap); |
34dc7c2f | 4104 | } |
428870ff | 4105 | err = ioctl_err = recv_skip(hdl, infd, |
330d06f9 | 4106 | flags->byteswap); |
34dc7c2f BB |
4107 | } |
4108 | } | |
4109 | *cp = '@'; | |
4110 | } | |
4111 | ||
34dc7c2f BB |
4112 | if (ioctl_err != 0) { |
4113 | switch (ioctl_errno) { | |
4114 | case ENODEV: | |
43e52edd | 4115 | cp = strchr(destsnap, '@'); |
34dc7c2f BB |
4116 | *cp = '\0'; |
4117 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4118 | "most recent snapshot of %s does not\n" | |
43e52edd | 4119 | "match incremental source"), destsnap); |
34dc7c2f BB |
4120 | (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); |
4121 | *cp = '@'; | |
4122 | break; | |
4123 | case ETXTBSY: | |
4124 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4125 | "destination %s has been modified\n" | |
43e52edd | 4126 | "since most recent snapshot"), name); |
34dc7c2f BB |
4127 | (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); |
4128 | break; | |
b5256303 TC |
4129 | case EACCES: |
4130 | if (raw && stream_wantsnewfs) { | |
4131 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4132 | "failed to create encryption key")); | |
4133 | } else if (raw && !stream_wantsnewfs) { | |
4134 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4135 | "encryption key does not match " | |
4136 | "existing key")); | |
4137 | } else { | |
4138 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4139 | "inherited key must be loaded")); | |
4140 | } | |
4141 | (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); | |
4142 | break; | |
34dc7c2f | 4143 | case EEXIST: |
43e52edd | 4144 | cp = strchr(destsnap, '@'); |
34dc7c2f BB |
4145 | if (newfs) { |
4146 | /* it's the containing fs that exists */ | |
4147 | *cp = '\0'; | |
4148 | } | |
4149 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4150 | "destination already exists")); | |
4151 | (void) zfs_error_fmt(hdl, EZFS_EXISTS, | |
4152 | dgettext(TEXT_DOMAIN, "cannot restore to %s"), | |
43e52edd | 4153 | destsnap); |
34dc7c2f BB |
4154 | *cp = '@'; |
4155 | break; | |
4156 | case EINVAL: | |
43e52edd BB |
4157 | if (flags->resumable) |
4158 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4159 | "kernel modules must be upgraded to " | |
4160 | "receive this stream.")); | |
9b840763 TC |
4161 | if (embedded && !raw) |
4162 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4163 | "incompatible embedded data stream " | |
4164 | "feature with encrypted receive.")); | |
34dc7c2f BB |
4165 | (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); |
4166 | break; | |
4167 | case ECKSUM: | |
43e52edd | 4168 | recv_ecksum_set_aux(hdl, destsnap, flags->resumable); |
34dc7c2f BB |
4169 | (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); |
4170 | break; | |
428870ff BB |
4171 | case ENOTSUP: |
4172 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4173 | "pool must be upgraded to receive this stream.")); | |
4174 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
4175 | break; | |
4176 | case EDQUOT: | |
4177 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
43e52edd | 4178 | "destination %s space quota exceeded"), name); |
330d06f9 | 4179 | (void) zfs_error(hdl, EZFS_NOSPC, errbuf); |
428870ff | 4180 | break; |
34dc7c2f BB |
4181 | default: |
4182 | (void) zfs_standard_error(hdl, ioctl_errno, errbuf); | |
4183 | } | |
4184 | } | |
4185 | ||
4186 | /* | |
428870ff BB |
4187 | * Mount the target filesystem (if created). Also mount any |
4188 | * children of the target filesystem if we did a replication | |
4189 | * receive (indicated by stream_avl being non-NULL). | |
34dc7c2f | 4190 | */ |
43e52edd | 4191 | cp = strchr(destsnap, '@'); |
34dc7c2f BB |
4192 | if (cp && (ioctl_err == 0 || !newfs)) { |
4193 | zfs_handle_t *h; | |
4194 | ||
4195 | *cp = '\0'; | |
43e52edd | 4196 | h = zfs_open(hdl, destsnap, |
34dc7c2f | 4197 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); |
34dc7c2f BB |
4198 | if (h != NULL) { |
4199 | if (h->zfs_type == ZFS_TYPE_VOLUME) { | |
b128c09f | 4200 | *cp = '@'; |
45d1cae3 | 4201 | } else if (newfs || stream_avl) { |
b128c09f BB |
4202 | /* |
4203 | * Track the first/top of hierarchy fs, | |
4204 | * for mounting and sharing later. | |
4205 | */ | |
4206 | if (top_zfs && *top_zfs == NULL) | |
43e52edd | 4207 | *top_zfs = zfs_strdup(hdl, destsnap); |
34dc7c2f BB |
4208 | } |
4209 | zfs_close(h); | |
4210 | } | |
b128c09f | 4211 | *cp = '@'; |
34dc7c2f BB |
4212 | } |
4213 | ||
4214 | if (clp) { | |
89d43feb GM |
4215 | if (!flags->nomount) |
4216 | err |= changelist_postfix(clp); | |
34dc7c2f BB |
4217 | changelist_free(clp); |
4218 | } | |
4219 | ||
428870ff BB |
4220 | if (prop_errflags & ZPROP_ERR_NOCLEAR) { |
4221 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " | |
43e52edd | 4222 | "failed to clear unreceived properties on %s"), name); |
428870ff BB |
4223 | (void) fprintf(stderr, "\n"); |
4224 | } | |
4225 | if (prop_errflags & ZPROP_ERR_NORESTORE) { | |
4226 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " | |
43e52edd | 4227 | "failed to restore original properties on %s"), name); |
428870ff BB |
4228 | (void) fprintf(stderr, "\n"); |
4229 | } | |
4230 | ||
43e52edd BB |
4231 | if (err || ioctl_err) { |
4232 | err = -1; | |
4233 | goto out; | |
4234 | } | |
572e2857 | 4235 | |
330d06f9 | 4236 | if (flags->verbose) { |
34dc7c2f BB |
4237 | char buf1[64]; |
4238 | char buf2[64]; | |
43e52edd | 4239 | uint64_t bytes = read_bytes; |
34dc7c2f BB |
4240 | time_t delta = time(NULL) - begin_time; |
4241 | if (delta == 0) | |
4242 | delta = 1; | |
e7fbeb60 | 4243 | zfs_nicebytes(bytes, buf1, sizeof (buf1)); |
4244 | zfs_nicebytes(bytes/delta, buf2, sizeof (buf1)); | |
34dc7c2f | 4245 | |
e7fbeb60 | 4246 | (void) printf("received %s stream in %lu seconds (%s/sec)\n", |
34dc7c2f BB |
4247 | buf1, delta, buf2); |
4248 | } | |
4249 | ||
43e52edd BB |
4250 | err = 0; |
4251 | out: | |
4252 | if (prop_errors != NULL) | |
4253 | nvlist_free(prop_errors); | |
4254 | ||
b5256303 TC |
4255 | if (tmp_keylocation[0] != '\0') { |
4256 | VERIFY(0 == nvlist_add_string(rcvprops, | |
4257 | zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation)); | |
4258 | } | |
4259 | ||
43e52edd | 4260 | if (newprops) |
a3eeab2d | 4261 | nvlist_free(rcvprops); |
4262 | ||
4263 | nvlist_free(oxprops); | |
4264 | nvlist_free(origprops); | |
43e52edd BB |
4265 | |
4266 | return (err); | |
34dc7c2f BB |
4267 | } |
4268 | ||
a3eeab2d | 4269 | /* |
4270 | * Check properties we were asked to override (both -o|-x) | |
4271 | */ | |
4272 | static boolean_t | |
4273 | zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props, | |
4274 | const char *errbuf) | |
4275 | { | |
4276 | nvpair_t *nvp; | |
4277 | zfs_prop_t prop; | |
4278 | const char *name; | |
4279 | ||
4280 | nvp = NULL; | |
4281 | while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) { | |
4282 | name = nvpair_name(nvp); | |
4283 | prop = zfs_name_to_prop(name); | |
4284 | ||
4285 | if (prop == ZPROP_INVAL) { | |
4286 | if (!zfs_prop_user(name)) { | |
4287 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4288 | "invalid property '%s'"), name); | |
4289 | return (B_FALSE); | |
4290 | } | |
4291 | continue; | |
4292 | } | |
4293 | /* | |
4294 | * "origin" is readonly but is used to receive datasets as | |
4295 | * clones so we don't raise an error here | |
4296 | */ | |
4297 | if (prop == ZFS_PROP_ORIGIN) | |
4298 | continue; | |
4299 | ||
4300 | /* | |
4301 | * cannot override readonly, set-once and other specific | |
4302 | * settable properties | |
4303 | */ | |
4304 | if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION || | |
4305 | prop == ZFS_PROP_VOLSIZE) { | |
4306 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4307 | "invalid property '%s'"), name); | |
4308 | return (B_FALSE); | |
4309 | } | |
4310 | } | |
4311 | ||
4312 | return (B_TRUE); | |
4313 | } | |
4314 | ||
b128c09f | 4315 | static int |
fcff0f35 PD |
4316 | zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, |
4317 | const char *originsnap, recvflags_t *flags, int infd, const char *sendfs, | |
4318 | nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd, | |
a3eeab2d | 4319 | uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops) |
34dc7c2f BB |
4320 | { |
4321 | int err; | |
4322 | dmu_replay_record_t drr, drr_noswap; | |
4323 | struct drr_begin *drrb = &drr.drr_u.drr_begin; | |
4324 | char errbuf[1024]; | |
2598c001 | 4325 | zio_cksum_t zcksum = { { 0 } }; |
428870ff BB |
4326 | uint64_t featureflags; |
4327 | int hdrtype; | |
34dc7c2f BB |
4328 | |
4329 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
4330 | "cannot receive")); | |
4331 | ||
a3eeab2d | 4332 | /* check cmdline props, raise an error if they cannot be received */ |
4333 | if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) { | |
4334 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
4335 | } | |
4336 | ||
330d06f9 | 4337 | if (flags->isprefix && |
34dc7c2f BB |
4338 | !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { |
4339 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " | |
4340 | "(%s) does not exist"), tosnap); | |
4341 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
4342 | } | |
fcff0f35 PD |
4343 | if (originsnap && |
4344 | !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) { | |
4345 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs " | |
4346 | "(%s) does not exist"), originsnap); | |
4347 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
4348 | } | |
34dc7c2f BB |
4349 | |
4350 | /* read in the BEGIN record */ | |
4351 | if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, | |
4352 | &zcksum))) | |
4353 | return (err); | |
4354 | ||
4355 | if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { | |
4356 | /* It's the double end record at the end of a package */ | |
4357 | return (ENODATA); | |
4358 | } | |
4359 | ||
4360 | /* the kernel needs the non-byteswapped begin record */ | |
4361 | drr_noswap = drr; | |
4362 | ||
330d06f9 | 4363 | flags->byteswap = B_FALSE; |
34dc7c2f BB |
4364 | if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { |
4365 | /* | |
4366 | * We computed the checksum in the wrong byteorder in | |
4367 | * recv_read() above; do it again correctly. | |
4368 | */ | |
4369 | bzero(&zcksum, sizeof (zio_cksum_t)); | |
4370 | fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum); | |
330d06f9 | 4371 | flags->byteswap = B_TRUE; |
34dc7c2f BB |
4372 | |
4373 | drr.drr_type = BSWAP_32(drr.drr_type); | |
4374 | drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); | |
4375 | drrb->drr_magic = BSWAP_64(drrb->drr_magic); | |
428870ff | 4376 | drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); |
34dc7c2f BB |
4377 | drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); |
4378 | drrb->drr_type = BSWAP_32(drrb->drr_type); | |
4379 | drrb->drr_flags = BSWAP_32(drrb->drr_flags); | |
4380 | drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); | |
4381 | drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); | |
4382 | } | |
4383 | ||
4384 | if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { | |
4385 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
4386 | "stream (bad magic number)")); | |
4387 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
4388 | } | |
4389 | ||
428870ff BB |
4390 | featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); |
4391 | hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo); | |
4392 | ||
4393 | if (!DMU_STREAM_SUPPORTED(featureflags) || | |
4394 | (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) { | |
4395 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4396 | "stream has unsupported feature, feature flags = %lx"), | |
4397 | featureflags); | |
4398 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
4399 | } | |
4400 | ||
34dc7c2f BB |
4401 | if (strchr(drrb->drr_toname, '@') == NULL) { |
4402 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
4403 | "stream (bad snapshot name)")); | |
4404 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
4405 | } | |
4406 | ||
428870ff | 4407 | if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) { |
eca7b760 | 4408 | char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN]; |
428870ff BB |
4409 | if (sendfs == NULL) { |
4410 | /* | |
4411 | * We were not called from zfs_receive_package(). Get | |
4412 | * the fs specified by 'zfs send'. | |
4413 | */ | |
4414 | char *cp; | |
4415 | (void) strlcpy(nonpackage_sendfs, | |
eca7b760 IK |
4416 | drr.drr_u.drr_begin.drr_toname, |
4417 | sizeof (nonpackage_sendfs)); | |
428870ff BB |
4418 | if ((cp = strchr(nonpackage_sendfs, '@')) != NULL) |
4419 | *cp = '\0'; | |
4420 | sendfs = nonpackage_sendfs; | |
671c9354 | 4421 | VERIFY(finalsnap == NULL); |
428870ff | 4422 | } |
fcff0f35 PD |
4423 | return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags, |
4424 | &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs, | |
a3eeab2d | 4425 | cleanup_fd, action_handlep, finalsnap, cmdprops)); |
428870ff BB |
4426 | } else { |
4427 | assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == | |
4428 | DMU_COMPOUNDSTREAM); | |
fcff0f35 | 4429 | return (zfs_receive_package(hdl, infd, tosnap, flags, &drr, |
a3eeab2d | 4430 | &zcksum, top_zfs, cleanup_fd, action_handlep, cmdprops)); |
34dc7c2f BB |
4431 | } |
4432 | } | |
b128c09f BB |
4433 | |
4434 | /* | |
4435 | * Restores a backup of tosnap from the file descriptor specified by infd. | |
4436 | * Return 0 on total success, -2 if some things couldn't be | |
4437 | * destroyed/renamed/promoted, -1 if some things couldn't be received. | |
47dfff3b MA |
4438 | * (-1 will override -2, if -1 and the resumable flag was specified the |
4439 | * transfer can be resumed if the sending side supports it). | |
b128c09f BB |
4440 | */ |
4441 | int | |
fcff0f35 PD |
4442 | zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props, |
4443 | recvflags_t *flags, int infd, avl_tree_t *stream_avl) | |
b128c09f BB |
4444 | { |
4445 | char *top_zfs = NULL; | |
4446 | int err; | |
572e2857 BB |
4447 | int cleanup_fd; |
4448 | uint64_t action_handle = 0; | |
5c3f61eb | 4449 | struct stat sb; |
fcff0f35 | 4450 | char *originsnap = NULL; |
5c3f61eb RY |
4451 | |
4452 | /* | |
4453 | * The only way fstat can fail is if we do not have a valid file | |
4454 | * descriptor. | |
4455 | */ | |
4456 | if (fstat(infd, &sb) == -1) { | |
4457 | perror("fstat"); | |
4458 | return (-2); | |
4459 | } | |
4460 | ||
4461 | #ifdef __linux__ | |
4462 | #ifndef F_SETPIPE_SZ | |
4463 | #define F_SETPIPE_SZ (F_SETLEASE + 7) | |
4464 | #endif /* F_SETPIPE_SZ */ | |
4465 | ||
4466 | #ifndef F_GETPIPE_SZ | |
4467 | #define F_GETPIPE_SZ (F_GETLEASE + 7) | |
4468 | #endif /* F_GETPIPE_SZ */ | |
4469 | ||
4470 | /* | |
4471 | * It is not uncommon for gigabytes to be processed in zfs receive. | |
4472 | * Speculatively increase the buffer size via Linux-specific fcntl() | |
4473 | * call. | |
4474 | */ | |
4475 | if (S_ISFIFO(sb.st_mode)) { | |
4476 | FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r"); | |
4477 | ||
4478 | if (procf != NULL) { | |
4479 | unsigned long max_psize; | |
4480 | long cur_psize; | |
4481 | if (fscanf(procf, "%lu", &max_psize) > 0) { | |
4482 | cur_psize = fcntl(infd, F_GETPIPE_SZ); | |
4483 | if (cur_psize > 0 && | |
4484 | max_psize > (unsigned long) cur_psize) | |
4485 | (void) fcntl(infd, F_SETPIPE_SZ, | |
4486 | max_psize); | |
4487 | } | |
4488 | fclose(procf); | |
4489 | } | |
4490 | } | |
4491 | #endif /* __linux__ */ | |
572e2857 | 4492 | |
fcff0f35 PD |
4493 | if (props) { |
4494 | err = nvlist_lookup_string(props, "origin", &originsnap); | |
4495 | if (err && err != ENOENT) | |
4496 | return (err); | |
4497 | } | |
4498 | ||
325f0235 | 4499 | cleanup_fd = open(ZFS_DEV, O_RDWR); |
572e2857 | 4500 | VERIFY(cleanup_fd >= 0); |
b128c09f | 4501 | |
fcff0f35 | 4502 | err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL, |
a3eeab2d | 4503 | stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL, props); |
572e2857 BB |
4504 | |
4505 | VERIFY(0 == close(cleanup_fd)); | |
b128c09f | 4506 | |
330d06f9 | 4507 | if (err == 0 && !flags->nomount && top_zfs) { |
689f093e GN |
4508 | zfs_handle_t *zhp = NULL; |
4509 | prop_changelist_t *clp = NULL; | |
b128c09f BB |
4510 | |
4511 | zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM); | |
4512 | if (zhp != NULL) { | |
4513 | clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, | |
4514 | CL_GATHER_MOUNT_ALWAYS, 0); | |
4515 | zfs_close(zhp); | |
4516 | if (clp != NULL) { | |
4517 | /* mount and share received datasets */ | |
4518 | err = changelist_postfix(clp); | |
4519 | changelist_free(clp); | |
4520 | } | |
4521 | } | |
4522 | if (zhp == NULL || clp == NULL || err) | |
4523 | err = -1; | |
4524 | } | |
4525 | if (top_zfs) | |
4526 | free(top_zfs); | |
4527 | ||
4528 | return (err); | |
4529 | } |