]>
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. |
9b67f605 | 24 | * Copyright (c) 2011, 2014 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. |
34dc7c2f BB |
29 | */ |
30 | ||
34dc7c2f BB |
31 | #include <assert.h> |
32 | #include <ctype.h> | |
33 | #include <errno.h> | |
34dc7c2f BB |
34 | #include <libintl.h> |
35 | #include <stdio.h> | |
36 | #include <stdlib.h> | |
37 | #include <strings.h> | |
38 | #include <unistd.h> | |
39 | #include <stddef.h> | |
40 | #include <fcntl.h> | |
41 | #include <sys/mount.h> | |
9b020fd9 BB |
42 | #include <sys/mntent.h> |
43 | #include <sys/mnttab.h> | |
44 | #include <sys/avl.h> | |
45 | #include <sys/debug.h> | |
5c3f61eb | 46 | #include <sys/stat.h> |
9b020fd9 | 47 | #include <stddef.h> |
428870ff BB |
48 | #include <pthread.h> |
49 | #include <umem.h> | |
37abac6d | 50 | #include <time.h> |
34dc7c2f BB |
51 | |
52 | #include <libzfs.h> | |
9b67f605 | 53 | #include <libzfs_core.h> |
34dc7c2f BB |
54 | |
55 | #include "zfs_namecheck.h" | |
56 | #include "zfs_prop.h" | |
428870ff | 57 | #include "zfs_fletcher.h" |
34dc7c2f | 58 | #include "libzfs_impl.h" |
428870ff BB |
59 | #include <sys/zio_checksum.h> |
60 | #include <sys/ddt.h> | |
1b9d8c34 | 61 | #include <sys/socket.h> |
34dc7c2f | 62 | |
428870ff BB |
63 | /* in libzfs_dataset.c */ |
64 | extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *); | |
34dc7c2f | 65 | |
330d06f9 | 66 | static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t *, |
572e2857 | 67 | int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *); |
428870ff | 68 | |
2598c001 | 69 | static const zio_cksum_t zero_cksum = { { 0 } }; |
428870ff BB |
70 | |
71 | typedef struct dedup_arg { | |
72 | int inputfd; | |
73 | int outputfd; | |
74 | libzfs_handle_t *dedup_hdl; | |
75 | } dedup_arg_t; | |
76 | ||
37abac6d BP |
77 | typedef struct progress_arg { |
78 | zfs_handle_t *pa_zhp; | |
79 | int pa_fd; | |
80 | boolean_t pa_parsable; | |
81 | } progress_arg_t; | |
82 | ||
428870ff BB |
83 | typedef struct dataref { |
84 | uint64_t ref_guid; | |
85 | uint64_t ref_object; | |
86 | uint64_t ref_offset; | |
87 | } dataref_t; | |
88 | ||
89 | typedef struct dedup_entry { | |
90 | struct dedup_entry *dde_next; | |
91 | zio_cksum_t dde_chksum; | |
92 | uint64_t dde_prop; | |
93 | dataref_t dde_ref; | |
94 | } dedup_entry_t; | |
95 | ||
96 | #define MAX_DDT_PHYSMEM_PERCENT 20 | |
97 | #define SMALLEST_POSSIBLE_MAX_DDT_MB 128 | |
98 | ||
99 | typedef struct dedup_table { | |
100 | dedup_entry_t **dedup_hash_array; | |
101 | umem_cache_t *ddecache; | |
102 | uint64_t max_ddt_size; /* max dedup table size in bytes */ | |
103 | uint64_t cur_ddt_size; /* current dedup table size in bytes */ | |
104 | uint64_t ddt_count; | |
105 | int numhashbits; | |
106 | boolean_t ddt_full; | |
107 | } dedup_table_t; | |
108 | ||
109 | static int | |
110 | high_order_bit(uint64_t n) | |
111 | { | |
112 | int count; | |
113 | ||
114 | for (count = 0; n != 0; count++) | |
115 | n >>= 1; | |
116 | return (count); | |
117 | } | |
118 | ||
119 | static size_t | |
120 | ssread(void *buf, size_t len, FILE *stream) | |
121 | { | |
122 | size_t outlen; | |
123 | ||
124 | if ((outlen = fread(buf, len, 1, stream)) == 0) | |
125 | return (0); | |
126 | ||
127 | return (outlen); | |
128 | } | |
129 | ||
130 | static void | |
131 | ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp, | |
132 | zio_cksum_t *cs, uint64_t prop, dataref_t *dr) | |
133 | { | |
134 | dedup_entry_t *dde; | |
135 | ||
136 | if (ddt->cur_ddt_size >= ddt->max_ddt_size) { | |
137 | if (ddt->ddt_full == B_FALSE) { | |
138 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
139 | "Dedup table full. Deduplication will continue " | |
140 | "with existing table entries")); | |
141 | ddt->ddt_full = B_TRUE; | |
142 | } | |
143 | return; | |
144 | } | |
145 | ||
146 | if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT)) | |
147 | != NULL) { | |
148 | assert(*ddepp == NULL); | |
149 | dde->dde_next = NULL; | |
150 | dde->dde_chksum = *cs; | |
151 | dde->dde_prop = prop; | |
152 | dde->dde_ref = *dr; | |
153 | *ddepp = dde; | |
154 | ddt->cur_ddt_size += sizeof (dedup_entry_t); | |
155 | ddt->ddt_count++; | |
156 | } | |
157 | } | |
158 | ||
159 | /* | |
160 | * Using the specified dedup table, do a lookup for an entry with | |
161 | * the checksum cs. If found, return the block's reference info | |
162 | * in *dr. Otherwise, insert a new entry in the dedup table, using | |
163 | * the reference information specified by *dr. | |
164 | * | |
165 | * return value: true - entry was found | |
166 | * false - entry was not found | |
167 | */ | |
168 | static boolean_t | |
169 | ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs, | |
170 | uint64_t prop, dataref_t *dr) | |
171 | { | |
172 | uint32_t hashcode; | |
173 | dedup_entry_t **ddepp; | |
174 | ||
175 | hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits); | |
176 | ||
177 | for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL; | |
178 | ddepp = &((*ddepp)->dde_next)) { | |
179 | if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) && | |
180 | (*ddepp)->dde_prop == prop) { | |
181 | *dr = (*ddepp)->dde_ref; | |
182 | return (B_TRUE); | |
183 | } | |
184 | } | |
185 | ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr); | |
186 | return (B_FALSE); | |
187 | } | |
188 | ||
189 | static int | |
190 | cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd) | |
191 | { | |
192 | fletcher_4_incremental_native(buf, len, zc); | |
193 | return (write(outfd, buf, len)); | |
194 | } | |
195 | ||
196 | /* | |
197 | * This function is started in a separate thread when the dedup option | |
198 | * has been requested. The main send thread determines the list of | |
199 | * snapshots to be included in the send stream and makes the ioctl calls | |
200 | * for each one. But instead of having the ioctl send the output to the | |
201 | * the output fd specified by the caller of zfs_send()), the | |
202 | * ioctl is told to direct the output to a pipe, which is read by the | |
203 | * alternate thread running THIS function. This function does the | |
204 | * dedup'ing by: | |
205 | * 1. building a dedup table (the DDT) | |
206 | * 2. doing checksums on each data block and inserting a record in the DDT | |
207 | * 3. looking for matching checksums, and | |
208 | * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever | |
209 | * a duplicate block is found. | |
210 | * The output of this function then goes to the output fd requested | |
211 | * by the caller of zfs_send(). | |
212 | */ | |
213 | static void * | |
214 | cksummer(void *arg) | |
215 | { | |
216 | dedup_arg_t *dda = arg; | |
f1512ee6 | 217 | char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE); |
428870ff BB |
218 | dmu_replay_record_t thedrr; |
219 | dmu_replay_record_t *drr = &thedrr; | |
220 | struct drr_begin *drrb = &thedrr.drr_u.drr_begin; | |
221 | struct drr_end *drre = &thedrr.drr_u.drr_end; | |
222 | struct drr_object *drro = &thedrr.drr_u.drr_object; | |
223 | struct drr_write *drrw = &thedrr.drr_u.drr_write; | |
224 | struct drr_spill *drrs = &thedrr.drr_u.drr_spill; | |
9b67f605 | 225 | struct drr_write_embedded *drrwe = &thedrr.drr_u.drr_write_embedded; |
428870ff BB |
226 | FILE *ofp; |
227 | int outfd; | |
228 | dmu_replay_record_t wbr_drr = {0}; | |
229 | struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref; | |
230 | dedup_table_t ddt; | |
231 | zio_cksum_t stream_cksum; | |
232 | uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE); | |
233 | uint64_t numbuckets; | |
234 | ||
235 | ddt.max_ddt_size = | |
236 | MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100, | |
237 | SMALLEST_POSSIBLE_MAX_DDT_MB<<20); | |
238 | ||
239 | numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t)); | |
240 | ||
241 | /* | |
242 | * numbuckets must be a power of 2. Increase number to | |
243 | * a power of 2 if necessary. | |
244 | */ | |
245 | if (!ISP2(numbuckets)) | |
246 | numbuckets = 1 << high_order_bit(numbuckets); | |
247 | ||
248 | ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *)); | |
249 | ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0, | |
250 | NULL, NULL, NULL, NULL, NULL, 0); | |
251 | ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *); | |
252 | ddt.numhashbits = high_order_bit(numbuckets) - 1; | |
253 | ddt.ddt_full = B_FALSE; | |
254 | ||
255 | /* Initialize the write-by-reference block. */ | |
256 | wbr_drr.drr_type = DRR_WRITE_BYREF; | |
257 | wbr_drr.drr_payloadlen = 0; | |
258 | ||
259 | outfd = dda->outputfd; | |
260 | ofp = fdopen(dda->inputfd, "r"); | |
261 | while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) { | |
262 | ||
263 | switch (drr->drr_type) { | |
264 | case DRR_BEGIN: | |
265 | { | |
266 | int fflags; | |
267 | ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0); | |
268 | ||
269 | /* set the DEDUP feature flag for this stream */ | |
270 | fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); | |
271 | fflags |= (DMU_BACKUP_FEATURE_DEDUP | | |
272 | DMU_BACKUP_FEATURE_DEDUPPROPS); | |
273 | DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags); | |
274 | ||
275 | if (cksum_and_write(drr, sizeof (dmu_replay_record_t), | |
276 | &stream_cksum, outfd) == -1) | |
277 | goto out; | |
278 | if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == | |
279 | DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) { | |
280 | int sz = drr->drr_payloadlen; | |
281 | ||
f1512ee6 MA |
282 | if (sz > SPA_MAXBLOCKSIZE) { |
283 | buf = zfs_realloc(dda->dedup_hdl, buf, | |
284 | SPA_MAXBLOCKSIZE, sz); | |
428870ff BB |
285 | } |
286 | (void) ssread(buf, sz, ofp); | |
287 | if (ferror(stdin)) | |
288 | perror("fread"); | |
289 | if (cksum_and_write(buf, sz, &stream_cksum, | |
290 | outfd) == -1) | |
291 | goto out; | |
292 | } | |
293 | break; | |
294 | } | |
295 | ||
296 | case DRR_END: | |
297 | { | |
298 | /* use the recalculated checksum */ | |
299 | ZIO_SET_CHECKSUM(&drre->drr_checksum, | |
300 | stream_cksum.zc_word[0], stream_cksum.zc_word[1], | |
301 | stream_cksum.zc_word[2], stream_cksum.zc_word[3]); | |
302 | if ((write(outfd, drr, | |
303 | sizeof (dmu_replay_record_t))) == -1) | |
304 | goto out; | |
305 | break; | |
306 | } | |
307 | ||
308 | case DRR_OBJECT: | |
309 | { | |
310 | if (cksum_and_write(drr, sizeof (dmu_replay_record_t), | |
311 | &stream_cksum, outfd) == -1) | |
312 | goto out; | |
313 | if (drro->drr_bonuslen > 0) { | |
314 | (void) ssread(buf, | |
315 | P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8), | |
316 | ofp); | |
317 | if (cksum_and_write(buf, | |
318 | P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8), | |
319 | &stream_cksum, outfd) == -1) | |
320 | goto out; | |
321 | } | |
322 | break; | |
323 | } | |
324 | ||
325 | case DRR_SPILL: | |
326 | { | |
327 | if (cksum_and_write(drr, sizeof (dmu_replay_record_t), | |
328 | &stream_cksum, outfd) == -1) | |
329 | goto out; | |
330 | (void) ssread(buf, drrs->drr_length, ofp); | |
331 | if (cksum_and_write(buf, drrs->drr_length, | |
332 | &stream_cksum, outfd) == -1) | |
333 | goto out; | |
334 | break; | |
335 | } | |
336 | ||
337 | case DRR_FREEOBJECTS: | |
338 | { | |
339 | if (cksum_and_write(drr, sizeof (dmu_replay_record_t), | |
340 | &stream_cksum, outfd) == -1) | |
341 | goto out; | |
342 | break; | |
343 | } | |
344 | ||
345 | case DRR_WRITE: | |
346 | { | |
347 | dataref_t dataref; | |
348 | ||
349 | (void) ssread(buf, drrw->drr_length, ofp); | |
350 | ||
351 | /* | |
352 | * Use the existing checksum if it's dedup-capable, | |
353 | * else calculate a SHA256 checksum for it. | |
354 | */ | |
355 | ||
356 | if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum, | |
357 | zero_cksum) || | |
358 | !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) { | |
9c905c55 BB |
359 | zio_cksum_t tmpsha256; |
360 | ||
361 | zio_checksum_SHA256(buf, | |
362 | drrw->drr_length, &tmpsha256); | |
428870ff | 363 | |
428870ff BB |
364 | drrw->drr_key.ddk_cksum.zc_word[0] = |
365 | BE_64(tmpsha256.zc_word[0]); | |
366 | drrw->drr_key.ddk_cksum.zc_word[1] = | |
367 | BE_64(tmpsha256.zc_word[1]); | |
368 | drrw->drr_key.ddk_cksum.zc_word[2] = | |
369 | BE_64(tmpsha256.zc_word[2]); | |
370 | drrw->drr_key.ddk_cksum.zc_word[3] = | |
371 | BE_64(tmpsha256.zc_word[3]); | |
372 | drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256; | |
373 | drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP; | |
374 | } | |
375 | ||
376 | dataref.ref_guid = drrw->drr_toguid; | |
377 | dataref.ref_object = drrw->drr_object; | |
378 | dataref.ref_offset = drrw->drr_offset; | |
379 | ||
380 | if (ddt_update(dda->dedup_hdl, &ddt, | |
381 | &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop, | |
382 | &dataref)) { | |
383 | /* block already present in stream */ | |
384 | wbr_drrr->drr_object = drrw->drr_object; | |
385 | wbr_drrr->drr_offset = drrw->drr_offset; | |
386 | wbr_drrr->drr_length = drrw->drr_length; | |
387 | wbr_drrr->drr_toguid = drrw->drr_toguid; | |
388 | wbr_drrr->drr_refguid = dataref.ref_guid; | |
389 | wbr_drrr->drr_refobject = | |
390 | dataref.ref_object; | |
391 | wbr_drrr->drr_refoffset = | |
392 | dataref.ref_offset; | |
393 | ||
394 | wbr_drrr->drr_checksumtype = | |
395 | drrw->drr_checksumtype; | |
396 | wbr_drrr->drr_checksumflags = | |
397 | drrw->drr_checksumtype; | |
398 | wbr_drrr->drr_key.ddk_cksum = | |
399 | drrw->drr_key.ddk_cksum; | |
400 | wbr_drrr->drr_key.ddk_prop = | |
401 | drrw->drr_key.ddk_prop; | |
402 | ||
403 | if (cksum_and_write(&wbr_drr, | |
404 | sizeof (dmu_replay_record_t), &stream_cksum, | |
405 | outfd) == -1) | |
406 | goto out; | |
407 | } else { | |
408 | /* block not previously seen */ | |
409 | if (cksum_and_write(drr, | |
410 | sizeof (dmu_replay_record_t), &stream_cksum, | |
411 | outfd) == -1) | |
412 | goto out; | |
413 | if (cksum_and_write(buf, | |
414 | drrw->drr_length, | |
415 | &stream_cksum, outfd) == -1) | |
416 | goto out; | |
417 | } | |
418 | break; | |
419 | } | |
420 | ||
9b67f605 MA |
421 | case DRR_WRITE_EMBEDDED: |
422 | { | |
423 | if (cksum_and_write(drr, sizeof (dmu_replay_record_t), | |
424 | &stream_cksum, outfd) == -1) | |
425 | goto out; | |
426 | (void) ssread(buf, | |
427 | P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp); | |
428 | if (cksum_and_write(buf, | |
429 | P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), | |
430 | &stream_cksum, outfd) == -1) | |
431 | goto out; | |
432 | break; | |
433 | } | |
434 | ||
428870ff BB |
435 | case DRR_FREE: |
436 | { | |
437 | if (cksum_and_write(drr, sizeof (dmu_replay_record_t), | |
438 | &stream_cksum, outfd) == -1) | |
439 | goto out; | |
440 | break; | |
441 | } | |
442 | ||
443 | default: | |
444 | (void) printf("INVALID record type 0x%x\n", | |
445 | drr->drr_type); | |
446 | /* should never happen, so assert */ | |
447 | assert(B_FALSE); | |
448 | } | |
449 | } | |
450 | out: | |
451 | umem_cache_destroy(ddt.ddecache); | |
452 | free(ddt.dedup_hash_array); | |
453 | free(buf); | |
454 | (void) fclose(ofp); | |
455 | ||
456 | return (NULL); | |
457 | } | |
b128c09f | 458 | |
34dc7c2f BB |
459 | /* |
460 | * Routines for dealing with the AVL tree of fs-nvlists | |
461 | */ | |
462 | typedef struct fsavl_node { | |
463 | avl_node_t fn_node; | |
464 | nvlist_t *fn_nvfs; | |
465 | char *fn_snapname; | |
466 | uint64_t fn_guid; | |
467 | } fsavl_node_t; | |
468 | ||
469 | static int | |
470 | fsavl_compare(const void *arg1, const void *arg2) | |
471 | { | |
472 | const fsavl_node_t *fn1 = arg1; | |
473 | const fsavl_node_t *fn2 = arg2; | |
474 | ||
475 | if (fn1->fn_guid > fn2->fn_guid) | |
476 | return (+1); | |
477 | else if (fn1->fn_guid < fn2->fn_guid) | |
478 | return (-1); | |
479 | else | |
480 | return (0); | |
481 | } | |
482 | ||
483 | /* | |
484 | * Given the GUID of a snapshot, find its containing filesystem and | |
485 | * (optionally) name. | |
486 | */ | |
487 | static nvlist_t * | |
488 | fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname) | |
489 | { | |
490 | fsavl_node_t fn_find; | |
491 | fsavl_node_t *fn; | |
492 | ||
493 | fn_find.fn_guid = snapguid; | |
494 | ||
495 | fn = avl_find(avl, &fn_find, NULL); | |
496 | if (fn) { | |
497 | if (snapname) | |
498 | *snapname = fn->fn_snapname; | |
499 | return (fn->fn_nvfs); | |
500 | } | |
501 | return (NULL); | |
502 | } | |
503 | ||
504 | static void | |
505 | fsavl_destroy(avl_tree_t *avl) | |
506 | { | |
507 | fsavl_node_t *fn; | |
508 | void *cookie; | |
509 | ||
510 | if (avl == NULL) | |
511 | return; | |
512 | ||
513 | cookie = NULL; | |
514 | while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL) | |
515 | free(fn); | |
516 | avl_destroy(avl); | |
517 | free(avl); | |
518 | } | |
519 | ||
45d1cae3 BB |
520 | /* |
521 | * Given an nvlist, produce an avl tree of snapshots, ordered by guid | |
522 | */ | |
34dc7c2f BB |
523 | static avl_tree_t * |
524 | fsavl_create(nvlist_t *fss) | |
525 | { | |
526 | avl_tree_t *fsavl; | |
527 | nvpair_t *fselem = NULL; | |
528 | ||
529 | if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL) | |
530 | return (NULL); | |
531 | ||
532 | avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t), | |
533 | offsetof(fsavl_node_t, fn_node)); | |
534 | ||
535 | while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) { | |
536 | nvlist_t *nvfs, *snaps; | |
537 | nvpair_t *snapelem = NULL; | |
538 | ||
539 | VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); | |
540 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); | |
541 | ||
542 | while ((snapelem = | |
543 | nvlist_next_nvpair(snaps, snapelem)) != NULL) { | |
544 | fsavl_node_t *fn; | |
545 | uint64_t guid; | |
546 | ||
547 | VERIFY(0 == nvpair_value_uint64(snapelem, &guid)); | |
548 | if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) { | |
549 | fsavl_destroy(fsavl); | |
550 | return (NULL); | |
551 | } | |
552 | fn->fn_nvfs = nvfs; | |
553 | fn->fn_snapname = nvpair_name(snapelem); | |
554 | fn->fn_guid = guid; | |
555 | ||
556 | /* | |
557 | * Note: if there are multiple snaps with the | |
558 | * same GUID, we ignore all but one. | |
559 | */ | |
560 | if (avl_find(fsavl, fn, NULL) == NULL) | |
561 | avl_add(fsavl, fn); | |
562 | else | |
563 | free(fn); | |
564 | } | |
565 | } | |
566 | ||
567 | return (fsavl); | |
568 | } | |
569 | ||
570 | /* | |
571 | * Routines for dealing with the giant nvlist of fs-nvlists, etc. | |
572 | */ | |
573 | typedef struct send_data { | |
574 | uint64_t parent_fromsnap_guid; | |
575 | nvlist_t *parent_snaps; | |
576 | nvlist_t *fss; | |
b128c09f | 577 | nvlist_t *snapprops; |
34dc7c2f BB |
578 | const char *fromsnap; |
579 | const char *tosnap; | |
428870ff | 580 | boolean_t recursive; |
05748550 AG |
581 | boolean_t seenfrom; |
582 | boolean_t seento; | |
34dc7c2f BB |
583 | |
584 | /* | |
585 | * The header nvlist is of the following format: | |
586 | * { | |
587 | * "tosnap" -> string | |
588 | * "fromsnap" -> string (if incremental) | |
589 | * "fss" -> { | |
590 | * id -> { | |
591 | * | |
592 | * "name" -> string (full name; for debugging) | |
593 | * "parentfromsnap" -> number (guid of fromsnap in parent) | |
594 | * | |
595 | * "props" -> { name -> value (only if set here) } | |
596 | * "snaps" -> { name (lastname) -> number (guid) } | |
b128c09f | 597 | * "snapprops" -> { name (lastname) -> { name -> value } } |
34dc7c2f BB |
598 | * |
599 | * "origin" -> number (guid) (if clone) | |
600 | * "sent" -> boolean (not on-disk) | |
601 | * } | |
602 | * } | |
603 | * } | |
604 | * | |
605 | */ | |
606 | } send_data_t; | |
607 | ||
b128c09f BB |
608 | static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv); |
609 | ||
34dc7c2f BB |
610 | static int |
611 | send_iterate_snap(zfs_handle_t *zhp, void *arg) | |
612 | { | |
613 | send_data_t *sd = arg; | |
614 | uint64_t guid = zhp->zfs_dmustats.dds_guid; | |
615 | char *snapname; | |
b128c09f | 616 | nvlist_t *nv; |
e890dd85 | 617 | boolean_t isfromsnap, istosnap, istosnapwithnofrom; |
34dc7c2f BB |
618 | |
619 | snapname = strrchr(zhp->zfs_name, '@')+1; | |
05748550 AG |
620 | isfromsnap = (sd->fromsnap != NULL && |
621 | strcmp(sd->fromsnap, snapname) == 0); | |
622 | istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0)); | |
e890dd85 | 623 | istosnapwithnofrom = (istosnap && sd->fromsnap == NULL); |
34dc7c2f | 624 | |
e890dd85 | 625 | VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid)); |
34dc7c2f BB |
626 | /* |
627 | * NB: if there is no fromsnap here (it's a newly created fs in | |
628 | * an incremental replication), we will substitute the tosnap. | |
629 | */ | |
05748550 | 630 | if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) { |
34dc7c2f BB |
631 | sd->parent_fromsnap_guid = guid; |
632 | } | |
633 | ||
05748550 AG |
634 | if (!sd->recursive) { |
635 | if (!sd->seenfrom && isfromsnap) { | |
636 | sd->seenfrom = B_TRUE; | |
637 | zfs_close(zhp); | |
638 | return (0); | |
639 | } | |
640 | ||
e890dd85 | 641 | if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) { |
05748550 AG |
642 | zfs_close(zhp); |
643 | return (0); | |
644 | } | |
645 | ||
646 | if (istosnap) | |
647 | sd->seento = B_TRUE; | |
648 | } | |
649 | ||
b128c09f BB |
650 | VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); |
651 | send_iterate_prop(zhp, nv); | |
652 | VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv)); | |
653 | nvlist_free(nv); | |
654 | ||
34dc7c2f BB |
655 | zfs_close(zhp); |
656 | return (0); | |
657 | } | |
658 | ||
659 | static void | |
660 | send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv) | |
661 | { | |
662 | nvpair_t *elem = NULL; | |
663 | ||
664 | while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) { | |
665 | char *propname = nvpair_name(elem); | |
666 | zfs_prop_t prop = zfs_name_to_prop(propname); | |
667 | nvlist_t *propnv; | |
668 | ||
428870ff BB |
669 | if (!zfs_prop_user(propname)) { |
670 | /* | |
671 | * Realistically, this should never happen. However, | |
672 | * we want the ability to add DSL properties without | |
673 | * needing to make incompatible version changes. We | |
674 | * need to ignore unknown properties to allow older | |
675 | * software to still send datasets containing these | |
676 | * properties, with the unknown properties elided. | |
677 | */ | |
678 | if (prop == ZPROP_INVAL) | |
679 | continue; | |
9babb374 | 680 | |
428870ff BB |
681 | if (zfs_prop_readonly(prop)) |
682 | continue; | |
683 | } | |
34dc7c2f BB |
684 | |
685 | verify(nvpair_value_nvlist(elem, &propnv) == 0); | |
45d1cae3 BB |
686 | if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION || |
687 | prop == ZFS_PROP_REFQUOTA || | |
688 | prop == ZFS_PROP_REFRESERVATION) { | |
428870ff | 689 | char *source; |
34dc7c2f BB |
690 | uint64_t value; |
691 | verify(nvlist_lookup_uint64(propnv, | |
692 | ZPROP_VALUE, &value) == 0); | |
b128c09f BB |
693 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) |
694 | continue; | |
428870ff BB |
695 | /* |
696 | * May have no source before SPA_VERSION_RECVD_PROPS, | |
697 | * but is still modifiable. | |
698 | */ | |
699 | if (nvlist_lookup_string(propnv, | |
700 | ZPROP_SOURCE, &source) == 0) { | |
701 | if ((strcmp(source, zhp->zfs_name) != 0) && | |
702 | (strcmp(source, | |
703 | ZPROP_SOURCE_VAL_RECVD) != 0)) | |
704 | continue; | |
705 | } | |
34dc7c2f BB |
706 | } else { |
707 | char *source; | |
708 | if (nvlist_lookup_string(propnv, | |
709 | ZPROP_SOURCE, &source) != 0) | |
710 | continue; | |
428870ff BB |
711 | if ((strcmp(source, zhp->zfs_name) != 0) && |
712 | (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)) | |
34dc7c2f BB |
713 | continue; |
714 | } | |
715 | ||
716 | if (zfs_prop_user(propname) || | |
717 | zfs_prop_get_type(prop) == PROP_TYPE_STRING) { | |
718 | char *value; | |
719 | verify(nvlist_lookup_string(propnv, | |
720 | ZPROP_VALUE, &value) == 0); | |
721 | VERIFY(0 == nvlist_add_string(nv, propname, value)); | |
722 | } else { | |
723 | uint64_t value; | |
724 | verify(nvlist_lookup_uint64(propnv, | |
725 | ZPROP_VALUE, &value) == 0); | |
726 | VERIFY(0 == nvlist_add_uint64(nv, propname, value)); | |
727 | } | |
728 | } | |
729 | } | |
730 | ||
45d1cae3 BB |
731 | /* |
732 | * recursively generate nvlists describing datasets. See comment | |
733 | * for the data structure send_data_t above for description of contents | |
734 | * of the nvlist. | |
735 | */ | |
34dc7c2f BB |
736 | static int |
737 | send_iterate_fs(zfs_handle_t *zhp, void *arg) | |
738 | { | |
739 | send_data_t *sd = arg; | |
740 | nvlist_t *nvfs, *nv; | |
428870ff | 741 | int rv = 0; |
34dc7c2f BB |
742 | uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid; |
743 | uint64_t guid = zhp->zfs_dmustats.dds_guid; | |
744 | char guidstring[64]; | |
745 | ||
746 | VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0)); | |
747 | VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name)); | |
748 | VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap", | |
749 | sd->parent_fromsnap_guid)); | |
750 | ||
751 | if (zhp->zfs_dmustats.dds_origin[0]) { | |
752 | zfs_handle_t *origin = zfs_open(zhp->zfs_hdl, | |
753 | zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); | |
754 | if (origin == NULL) | |
755 | return (-1); | |
756 | VERIFY(0 == nvlist_add_uint64(nvfs, "origin", | |
757 | origin->zfs_dmustats.dds_guid)); | |
758 | } | |
759 | ||
760 | /* iterate over props */ | |
761 | VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0)); | |
762 | send_iterate_prop(zhp, nv); | |
763 | VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv)); | |
764 | nvlist_free(nv); | |
765 | ||
766 | /* iterate over snaps, and set sd->parent_fromsnap_guid */ | |
767 | sd->parent_fromsnap_guid = 0; | |
768 | VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0)); | |
b128c09f | 769 | VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0)); |
7509a3d2 | 770 | (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd); |
34dc7c2f | 771 | VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps)); |
b128c09f | 772 | VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops)); |
34dc7c2f | 773 | nvlist_free(sd->parent_snaps); |
b128c09f | 774 | nvlist_free(sd->snapprops); |
34dc7c2f BB |
775 | |
776 | /* add this fs to nvlist */ | |
777 | (void) snprintf(guidstring, sizeof (guidstring), | |
778 | "0x%llx", (longlong_t)guid); | |
779 | VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs)); | |
780 | nvlist_free(nvfs); | |
781 | ||
782 | /* iterate over children */ | |
428870ff BB |
783 | if (sd->recursive) |
784 | rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd); | |
34dc7c2f BB |
785 | |
786 | sd->parent_fromsnap_guid = parent_fromsnap_guid_save; | |
787 | ||
788 | zfs_close(zhp); | |
789 | return (rv); | |
790 | } | |
791 | ||
792 | static int | |
793 | gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap, | |
428870ff | 794 | const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp) |
34dc7c2f BB |
795 | { |
796 | zfs_handle_t *zhp; | |
797 | send_data_t sd = { 0 }; | |
798 | int error; | |
799 | ||
800 | zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); | |
801 | if (zhp == NULL) | |
802 | return (EZFS_BADTYPE); | |
803 | ||
804 | VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0)); | |
805 | sd.fromsnap = fromsnap; | |
806 | sd.tosnap = tosnap; | |
428870ff | 807 | sd.recursive = recursive; |
34dc7c2f BB |
808 | |
809 | if ((error = send_iterate_fs(zhp, &sd)) != 0) { | |
810 | nvlist_free(sd.fss); | |
811 | if (avlp != NULL) | |
812 | *avlp = NULL; | |
813 | *nvlp = NULL; | |
814 | return (error); | |
815 | } | |
816 | ||
817 | if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) { | |
818 | nvlist_free(sd.fss); | |
819 | *nvlp = NULL; | |
820 | return (EZFS_NOMEM); | |
821 | } | |
822 | ||
823 | *nvlp = sd.fss; | |
824 | return (0); | |
825 | } | |
826 | ||
34dc7c2f BB |
827 | /* |
828 | * Routines specific to "zfs send" | |
829 | */ | |
830 | typedef struct send_dump_data { | |
831 | /* these are all just the short snapname (the part after the @) */ | |
832 | const char *fromsnap; | |
833 | const char *tosnap; | |
428870ff | 834 | char prevsnap[ZFS_MAXNAMELEN]; |
572e2857 | 835 | uint64_t prevsnap_obj; |
34dc7c2f | 836 | boolean_t seenfrom, seento, replicate, doall, fromorigin; |
f1512ee6 | 837 | boolean_t verbose, dryrun, parsable, progress, embed_data, large_block; |
34dc7c2f BB |
838 | int outfd; |
839 | boolean_t err; | |
840 | nvlist_t *fss; | |
95fd54a1 | 841 | nvlist_t *snapholds; |
34dc7c2f | 842 | avl_tree_t *fsavl; |
428870ff BB |
843 | snapfilter_cb_t *filter_cb; |
844 | void *filter_cb_arg; | |
845 | nvlist_t *debugnv; | |
572e2857 BB |
846 | char holdtag[ZFS_MAXNAMELEN]; |
847 | int cleanup_fd; | |
330d06f9 | 848 | uint64_t size; |
34dc7c2f BB |
849 | } send_dump_data_t; |
850 | ||
330d06f9 MA |
851 | static int |
852 | estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj, | |
853 | boolean_t fromorigin, uint64_t *sizep) | |
854 | { | |
13fe0198 | 855 | zfs_cmd_t zc = {"\0"}; |
330d06f9 MA |
856 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
857 | ||
858 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); | |
859 | assert(fromsnap_obj == 0 || !fromorigin); | |
860 | ||
861 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
862 | zc.zc_obj = fromorigin; | |
863 | zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); | |
864 | zc.zc_fromobj = fromsnap_obj; | |
865 | zc.zc_guid = 1; /* estimate flag */ | |
866 | ||
867 | if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) { | |
868 | char errbuf[1024]; | |
869 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
870 | "warning: cannot estimate space for '%s'"), zhp->zfs_name); | |
871 | ||
872 | switch (errno) { | |
873 | case EXDEV: | |
874 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
875 | "not an earlier snapshot from the same fs")); | |
876 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
877 | ||
878 | case ENOENT: | |
879 | if (zfs_dataset_exists(hdl, zc.zc_name, | |
880 | ZFS_TYPE_SNAPSHOT)) { | |
881 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
882 | "incremental source (@%s) does not exist"), | |
883 | zc.zc_value); | |
884 | } | |
885 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
886 | ||
887 | case EDQUOT: | |
888 | case EFBIG: | |
889 | case EIO: | |
890 | case ENOLINK: | |
891 | case ENOSPC: | |
892 | case ENOSTR: | |
893 | case ENXIO: | |
894 | case EPIPE: | |
895 | case ERANGE: | |
896 | case EFAULT: | |
897 | case EROFS: | |
898 | zfs_error_aux(hdl, strerror(errno)); | |
899 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
900 | ||
901 | default: | |
902 | return (zfs_standard_error(hdl, errno, errbuf)); | |
903 | } | |
904 | } | |
905 | ||
906 | *sizep = zc.zc_objset_type; | |
907 | ||
908 | return (0); | |
909 | } | |
910 | ||
34dc7c2f BB |
911 | /* |
912 | * Dumps a backup of the given snapshot (incremental from fromsnap if it's not | |
913 | * NULL) to the file descriptor specified by outfd. | |
914 | */ | |
915 | static int | |
572e2857 | 916 | dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj, |
9b67f605 MA |
917 | boolean_t fromorigin, int outfd, enum lzc_send_flags flags, |
918 | nvlist_t *debugnv) | |
34dc7c2f | 919 | { |
13fe0198 | 920 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f | 921 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
428870ff | 922 | nvlist_t *thisdbg; |
34dc7c2f BB |
923 | |
924 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); | |
572e2857 | 925 | assert(fromsnap_obj == 0 || !fromorigin); |
34dc7c2f BB |
926 | |
927 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
34dc7c2f BB |
928 | zc.zc_cookie = outfd; |
929 | zc.zc_obj = fromorigin; | |
572e2857 BB |
930 | zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); |
931 | zc.zc_fromobj = fromsnap_obj; | |
9b67f605 | 932 | zc.zc_flags = flags; |
428870ff BB |
933 | |
934 | VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0)); | |
935 | if (fromsnap && fromsnap[0] != '\0') { | |
936 | VERIFY(0 == nvlist_add_string(thisdbg, | |
937 | "fromsnap", fromsnap)); | |
938 | } | |
939 | ||
330d06f9 | 940 | if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) { |
34dc7c2f BB |
941 | char errbuf[1024]; |
942 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
943 | "warning: cannot send '%s'"), zhp->zfs_name); | |
944 | ||
428870ff BB |
945 | VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno)); |
946 | if (debugnv) { | |
947 | VERIFY(0 == nvlist_add_nvlist(debugnv, | |
948 | zhp->zfs_name, thisdbg)); | |
949 | } | |
950 | nvlist_free(thisdbg); | |
951 | ||
34dc7c2f | 952 | switch (errno) { |
34dc7c2f BB |
953 | case EXDEV: |
954 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
955 | "not an earlier snapshot from the same fs")); | |
956 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
957 | ||
958 | case ENOENT: | |
959 | if (zfs_dataset_exists(hdl, zc.zc_name, | |
960 | ZFS_TYPE_SNAPSHOT)) { | |
961 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
962 | "incremental source (@%s) does not exist"), | |
963 | zc.zc_value); | |
964 | } | |
965 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
966 | ||
967 | case EDQUOT: | |
968 | case EFBIG: | |
969 | case EIO: | |
970 | case ENOLINK: | |
971 | case ENOSPC: | |
972 | case ENOSTR: | |
973 | case ENXIO: | |
974 | case EPIPE: | |
975 | case ERANGE: | |
976 | case EFAULT: | |
977 | case EROFS: | |
978 | zfs_error_aux(hdl, strerror(errno)); | |
979 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
980 | ||
981 | default: | |
982 | return (zfs_standard_error(hdl, errno, errbuf)); | |
983 | } | |
984 | } | |
985 | ||
428870ff BB |
986 | if (debugnv) |
987 | VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg)); | |
988 | nvlist_free(thisdbg); | |
989 | ||
34dc7c2f BB |
990 | return (0); |
991 | } | |
992 | ||
95fd54a1 SH |
993 | static void |
994 | gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd) | |
572e2857 | 995 | { |
572e2857 BB |
996 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); |
997 | ||
998 | /* | |
95fd54a1 | 999 | * zfs_send() only sets snapholds for sends that need them, |
572e2857 BB |
1000 | * e.g. replication and doall. |
1001 | */ | |
95fd54a1 SH |
1002 | if (sdd->snapholds == NULL) |
1003 | return; | |
572e2857 | 1004 | |
95fd54a1 | 1005 | fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag); |
572e2857 BB |
1006 | } |
1007 | ||
37abac6d BP |
1008 | static void * |
1009 | send_progress_thread(void *arg) | |
1010 | { | |
1011 | progress_arg_t *pa = arg; | |
1012 | ||
13fe0198 | 1013 | zfs_cmd_t zc = {"\0"}; |
37abac6d BP |
1014 | zfs_handle_t *zhp = pa->pa_zhp; |
1015 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
1016 | unsigned long long bytes; | |
1017 | char buf[16]; | |
1018 | ||
1019 | time_t t; | |
1020 | struct tm *tm; | |
1021 | ||
1022 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); | |
1023 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
1024 | ||
1025 | if (!pa->pa_parsable) | |
1026 | (void) fprintf(stderr, "TIME SENT SNAPSHOT\n"); | |
1027 | ||
1028 | /* | |
1029 | * Print the progress from ZFS_IOC_SEND_PROGRESS every second. | |
1030 | */ | |
1031 | for (;;) { | |
1032 | (void) sleep(1); | |
1033 | ||
1034 | zc.zc_cookie = pa->pa_fd; | |
1035 | if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0) | |
1036 | return ((void *)-1); | |
1037 | ||
1038 | (void) time(&t); | |
1039 | tm = localtime(&t); | |
1040 | bytes = zc.zc_cookie; | |
1041 | ||
1042 | if (pa->pa_parsable) { | |
1043 | (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n", | |
1044 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
1045 | bytes, zhp->zfs_name); | |
1046 | } else { | |
1047 | zfs_nicenum(bytes, buf, sizeof (buf)); | |
1048 | (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n", | |
1049 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
1050 | buf, zhp->zfs_name); | |
1051 | } | |
1052 | } | |
1053 | } | |
1054 | ||
34dc7c2f BB |
1055 | static int |
1056 | dump_snapshot(zfs_handle_t *zhp, void *arg) | |
1057 | { | |
1058 | send_dump_data_t *sdd = arg; | |
37abac6d BP |
1059 | progress_arg_t pa = { 0 }; |
1060 | pthread_t tid; | |
572e2857 | 1061 | char *thissnap; |
34dc7c2f | 1062 | int err; |
330d06f9 | 1063 | boolean_t isfromsnap, istosnap, fromorigin; |
428870ff | 1064 | boolean_t exclude = B_FALSE; |
34dc7c2f | 1065 | |
95fd54a1 | 1066 | err = 0; |
34dc7c2f | 1067 | thissnap = strchr(zhp->zfs_name, '@') + 1; |
428870ff BB |
1068 | isfromsnap = (sdd->fromsnap != NULL && |
1069 | strcmp(sdd->fromsnap, thissnap) == 0); | |
34dc7c2f | 1070 | |
428870ff | 1071 | if (!sdd->seenfrom && isfromsnap) { |
95fd54a1 SH |
1072 | gather_holds(zhp, sdd); |
1073 | sdd->seenfrom = B_TRUE; | |
1074 | (void) strcpy(sdd->prevsnap, thissnap); | |
1075 | sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); | |
34dc7c2f | 1076 | zfs_close(zhp); |
95fd54a1 | 1077 | return (0); |
34dc7c2f BB |
1078 | } |
1079 | ||
1080 | if (sdd->seento || !sdd->seenfrom) { | |
1081 | zfs_close(zhp); | |
1082 | return (0); | |
1083 | } | |
1084 | ||
428870ff BB |
1085 | istosnap = (strcmp(sdd->tosnap, thissnap) == 0); |
1086 | if (istosnap) | |
1087 | sdd->seento = B_TRUE; | |
1088 | ||
1089 | if (!sdd->doall && !isfromsnap && !istosnap) { | |
1090 | if (sdd->replicate) { | |
1091 | char *snapname; | |
1092 | nvlist_t *snapprops; | |
1093 | /* | |
1094 | * Filter out all intermediate snapshots except origin | |
1095 | * snapshots needed to replicate clones. | |
1096 | */ | |
1097 | nvlist_t *nvfs = fsavl_find(sdd->fsavl, | |
1098 | zhp->zfs_dmustats.dds_guid, &snapname); | |
1099 | ||
1100 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, | |
1101 | "snapprops", &snapprops)); | |
1102 | VERIFY(0 == nvlist_lookup_nvlist(snapprops, | |
1103 | thissnap, &snapprops)); | |
1104 | exclude = !nvlist_exists(snapprops, "is_clone_origin"); | |
1105 | } else { | |
1106 | exclude = B_TRUE; | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | /* | |
1111 | * If a filter function exists, call it to determine whether | |
1112 | * this snapshot will be sent. | |
1113 | */ | |
1114 | if (exclude || (sdd->filter_cb != NULL && | |
1115 | sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) { | |
1116 | /* | |
1117 | * This snapshot is filtered out. Don't send it, and don't | |
572e2857 | 1118 | * set prevsnap_obj, so it will be as if this snapshot didn't |
428870ff BB |
1119 | * exist, and the next accepted snapshot will be sent as |
1120 | * an incremental from the last accepted one, or as the | |
1121 | * first (and full) snapshot in the case of a replication, | |
1122 | * non-incremental send. | |
1123 | */ | |
1124 | zfs_close(zhp); | |
1125 | return (0); | |
1126 | } | |
1127 | ||
95fd54a1 | 1128 | gather_holds(zhp, sdd); |
330d06f9 MA |
1129 | fromorigin = sdd->prevsnap[0] == '\0' && |
1130 | (sdd->fromorigin || sdd->replicate); | |
1131 | ||
34dc7c2f | 1132 | if (sdd->verbose) { |
330d06f9 MA |
1133 | uint64_t size; |
1134 | err = estimate_ioctl(zhp, sdd->prevsnap_obj, | |
1135 | fromorigin, &size); | |
1136 | ||
1137 | if (sdd->parsable) { | |
1138 | if (sdd->prevsnap[0] != '\0') { | |
1139 | (void) fprintf(stderr, "incremental\t%s\t%s", | |
1140 | sdd->prevsnap, zhp->zfs_name); | |
1141 | } else { | |
1142 | (void) fprintf(stderr, "full\t%s", | |
1143 | zhp->zfs_name); | |
1144 | } | |
1145 | } else { | |
1146 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
1147 | "send from @%s to %s"), | |
1148 | sdd->prevsnap, zhp->zfs_name); | |
1149 | } | |
1150 | if (err == 0) { | |
1151 | if (sdd->parsable) { | |
1152 | (void) fprintf(stderr, "\t%llu\n", | |
1153 | (longlong_t)size); | |
1154 | } else { | |
1155 | char buf[16]; | |
1156 | zfs_nicenum(size, buf, sizeof (buf)); | |
1157 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
1158 | " estimated size is %s\n"), buf); | |
1159 | } | |
1160 | sdd->size += size; | |
1161 | } else { | |
1162 | (void) fprintf(stderr, "\n"); | |
1163 | } | |
34dc7c2f BB |
1164 | } |
1165 | ||
330d06f9 | 1166 | if (!sdd->dryrun) { |
37abac6d BP |
1167 | /* |
1168 | * If progress reporting is requested, spawn a new thread to | |
1169 | * poll ZFS_IOC_SEND_PROGRESS at a regular interval. | |
1170 | */ | |
1171 | if (sdd->progress) { | |
1172 | pa.pa_zhp = zhp; | |
1173 | pa.pa_fd = sdd->outfd; | |
1174 | pa.pa_parsable = sdd->parsable; | |
1175 | ||
1176 | if ((err = pthread_create(&tid, NULL, | |
1177 | send_progress_thread, &pa))) { | |
1178 | zfs_close(zhp); | |
1179 | return (err); | |
1180 | } | |
1181 | } | |
1182 | ||
9b67f605 | 1183 | enum lzc_send_flags flags = 0; |
f1512ee6 MA |
1184 | if (sdd->large_block) |
1185 | flags |= LZC_SEND_FLAG_LARGE_BLOCK; | |
9b67f605 MA |
1186 | if (sdd->embed_data) |
1187 | flags |= LZC_SEND_FLAG_EMBED_DATA; | |
1188 | ||
330d06f9 | 1189 | err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj, |
9b67f605 | 1190 | fromorigin, sdd->outfd, flags, sdd->debugnv); |
37abac6d BP |
1191 | |
1192 | if (sdd->progress) { | |
1193 | (void) pthread_cancel(tid); | |
1194 | (void) pthread_join(tid, NULL); | |
1195 | } | |
330d06f9 | 1196 | } |
34dc7c2f | 1197 | |
572e2857 BB |
1198 | (void) strcpy(sdd->prevsnap, thissnap); |
1199 | sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); | |
34dc7c2f BB |
1200 | zfs_close(zhp); |
1201 | return (err); | |
1202 | } | |
1203 | ||
1204 | static int | |
1205 | dump_filesystem(zfs_handle_t *zhp, void *arg) | |
1206 | { | |
1207 | int rv = 0; | |
1208 | send_dump_data_t *sdd = arg; | |
1209 | boolean_t missingfrom = B_FALSE; | |
13fe0198 | 1210 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1211 | |
1212 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", | |
1213 | zhp->zfs_name, sdd->tosnap); | |
1214 | if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { | |
330d06f9 MA |
1215 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
1216 | "WARNING: could not send %s@%s: does not exist\n"), | |
34dc7c2f BB |
1217 | zhp->zfs_name, sdd->tosnap); |
1218 | sdd->err = B_TRUE; | |
1219 | return (0); | |
1220 | } | |
1221 | ||
1222 | if (sdd->replicate && sdd->fromsnap) { | |
1223 | /* | |
1224 | * If this fs does not have fromsnap, and we're doing | |
1225 | * recursive, we need to send a full stream from the | |
1226 | * beginning (or an incremental from the origin if this | |
1227 | * is a clone). If we're doing non-recursive, then let | |
1228 | * them get the error. | |
1229 | */ | |
1230 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", | |
1231 | zhp->zfs_name, sdd->fromsnap); | |
1232 | if (ioctl(zhp->zfs_hdl->libzfs_fd, | |
1233 | ZFS_IOC_OBJSET_STATS, &zc) != 0) { | |
1234 | missingfrom = B_TRUE; | |
1235 | } | |
1236 | } | |
1237 | ||
428870ff | 1238 | sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0; |
572e2857 | 1239 | sdd->prevsnap_obj = 0; |
428870ff BB |
1240 | if (sdd->fromsnap == NULL || missingfrom) |
1241 | sdd->seenfrom = B_TRUE; | |
34dc7c2f | 1242 | |
428870ff BB |
1243 | rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg); |
1244 | if (!sdd->seenfrom) { | |
330d06f9 | 1245 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
428870ff | 1246 | "WARNING: could not send %s@%s:\n" |
330d06f9 | 1247 | "incremental source (%s@%s) does not exist\n"), |
428870ff BB |
1248 | zhp->zfs_name, sdd->tosnap, |
1249 | zhp->zfs_name, sdd->fromsnap); | |
1250 | sdd->err = B_TRUE; | |
1251 | } else if (!sdd->seento) { | |
1252 | if (sdd->fromsnap) { | |
330d06f9 | 1253 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
34dc7c2f | 1254 | "WARNING: could not send %s@%s:\n" |
428870ff | 1255 | "incremental source (%s@%s) " |
330d06f9 | 1256 | "is not earlier than it\n"), |
34dc7c2f BB |
1257 | zhp->zfs_name, sdd->tosnap, |
1258 | zhp->zfs_name, sdd->fromsnap); | |
34dc7c2f | 1259 | } else { |
330d06f9 MA |
1260 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
1261 | "WARNING: " | |
1262 | "could not send %s@%s: does not exist\n"), | |
428870ff | 1263 | zhp->zfs_name, sdd->tosnap); |
34dc7c2f | 1264 | } |
428870ff | 1265 | sdd->err = B_TRUE; |
34dc7c2f BB |
1266 | } |
1267 | ||
1268 | return (rv); | |
1269 | } | |
1270 | ||
1271 | static int | |
1272 | dump_filesystems(zfs_handle_t *rzhp, void *arg) | |
1273 | { | |
1274 | send_dump_data_t *sdd = arg; | |
1275 | nvpair_t *fspair; | |
1276 | boolean_t needagain, progress; | |
1277 | ||
1278 | if (!sdd->replicate) | |
1279 | return (dump_filesystem(rzhp, sdd)); | |
1280 | ||
428870ff BB |
1281 | /* Mark the clone origin snapshots. */ |
1282 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1283 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
1284 | nvlist_t *nvfs; | |
1285 | uint64_t origin_guid = 0; | |
1286 | ||
1287 | VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs)); | |
1288 | (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid); | |
1289 | if (origin_guid != 0) { | |
1290 | char *snapname; | |
1291 | nvlist_t *origin_nv = fsavl_find(sdd->fsavl, | |
1292 | origin_guid, &snapname); | |
1293 | if (origin_nv != NULL) { | |
1294 | nvlist_t *snapprops; | |
1295 | VERIFY(0 == nvlist_lookup_nvlist(origin_nv, | |
1296 | "snapprops", &snapprops)); | |
1297 | VERIFY(0 == nvlist_lookup_nvlist(snapprops, | |
1298 | snapname, &snapprops)); | |
1299 | VERIFY(0 == nvlist_add_boolean( | |
1300 | snapprops, "is_clone_origin")); | |
1301 | } | |
1302 | } | |
1303 | } | |
34dc7c2f BB |
1304 | again: |
1305 | needagain = progress = B_FALSE; | |
1306 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1307 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
330d06f9 | 1308 | nvlist_t *fslist, *parent_nv; |
34dc7c2f BB |
1309 | char *fsname; |
1310 | zfs_handle_t *zhp; | |
1311 | int err; | |
1312 | uint64_t origin_guid = 0; | |
330d06f9 | 1313 | uint64_t parent_guid = 0; |
34dc7c2f BB |
1314 | |
1315 | VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); | |
1316 | if (nvlist_lookup_boolean(fslist, "sent") == 0) | |
1317 | continue; | |
1318 | ||
1319 | VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0); | |
1320 | (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); | |
330d06f9 MA |
1321 | (void) nvlist_lookup_uint64(fslist, "parentfromsnap", |
1322 | &parent_guid); | |
1323 | ||
1324 | if (parent_guid != 0) { | |
1325 | parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL); | |
1326 | if (!nvlist_exists(parent_nv, "sent")) { | |
1327 | /* parent has not been sent; skip this one */ | |
1328 | needagain = B_TRUE; | |
1329 | continue; | |
1330 | } | |
1331 | } | |
34dc7c2f | 1332 | |
428870ff BB |
1333 | if (origin_guid != 0) { |
1334 | nvlist_t *origin_nv = fsavl_find(sdd->fsavl, | |
1335 | origin_guid, NULL); | |
1336 | if (origin_nv != NULL && | |
330d06f9 | 1337 | !nvlist_exists(origin_nv, "sent")) { |
428870ff BB |
1338 | /* |
1339 | * origin has not been sent yet; | |
1340 | * skip this clone. | |
1341 | */ | |
1342 | needagain = B_TRUE; | |
1343 | continue; | |
1344 | } | |
34dc7c2f BB |
1345 | } |
1346 | ||
1347 | zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); | |
1348 | if (zhp == NULL) | |
1349 | return (-1); | |
1350 | err = dump_filesystem(zhp, sdd); | |
1351 | VERIFY(nvlist_add_boolean(fslist, "sent") == 0); | |
1352 | progress = B_TRUE; | |
1353 | zfs_close(zhp); | |
1354 | if (err) | |
1355 | return (err); | |
1356 | } | |
1357 | if (needagain) { | |
1358 | assert(progress); | |
1359 | goto again; | |
1360 | } | |
330d06f9 MA |
1361 | |
1362 | /* clean out the sent flags in case we reuse this fss */ | |
1363 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1364 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
1365 | nvlist_t *fslist; | |
1366 | ||
1367 | VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); | |
1368 | (void) nvlist_remove_all(fslist, "sent"); | |
1369 | } | |
1370 | ||
34dc7c2f BB |
1371 | return (0); |
1372 | } | |
1373 | ||
1374 | /* | |
45d1cae3 BB |
1375 | * Generate a send stream for the dataset identified by the argument zhp. |
1376 | * | |
1377 | * The content of the send stream is the snapshot identified by | |
1378 | * 'tosnap'. Incremental streams are requested in two ways: | |
1379 | * - from the snapshot identified by "fromsnap" (if non-null) or | |
1380 | * - from the origin of the dataset identified by zhp, which must | |
1381 | * be a clone. In this case, "fromsnap" is null and "fromorigin" | |
1382 | * is TRUE. | |
1383 | * | |
1384 | * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and | |
428870ff | 1385 | * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM) |
45d1cae3 | 1386 | * if "replicate" is set. If "doall" is set, dump all the intermediate |
428870ff BB |
1387 | * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall" |
1388 | * case too. If "props" is set, send properties. | |
34dc7c2f BB |
1389 | */ |
1390 | int | |
1391 | zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, | |
330d06f9 | 1392 | sendflags_t *flags, int outfd, snapfilter_cb_t filter_func, |
428870ff | 1393 | void *cb_arg, nvlist_t **debugnvp) |
34dc7c2f BB |
1394 | { |
1395 | char errbuf[1024]; | |
1396 | send_dump_data_t sdd = { 0 }; | |
330d06f9 | 1397 | int err = 0; |
34dc7c2f BB |
1398 | nvlist_t *fss = NULL; |
1399 | avl_tree_t *fsavl = NULL; | |
428870ff BB |
1400 | static uint64_t holdseq; |
1401 | int spa_version; | |
95fd54a1 | 1402 | pthread_t tid = 0; |
428870ff BB |
1403 | int pipefd[2]; |
1404 | dedup_arg_t dda = { 0 }; | |
1405 | int featureflags = 0; | |
1406 | ||
34dc7c2f BB |
1407 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
1408 | "cannot send '%s'"), zhp->zfs_name); | |
1409 | ||
1410 | if (fromsnap && fromsnap[0] == '\0') { | |
1411 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
1412 | "zero-length incremental source")); | |
1413 | return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); | |
1414 | } | |
1415 | ||
572e2857 BB |
1416 | if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) { |
1417 | uint64_t version; | |
1418 | version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); | |
1419 | if (version >= ZPL_VERSION_SA) { | |
1420 | featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; | |
1421 | } | |
1422 | } | |
1423 | ||
330d06f9 | 1424 | if (flags->dedup && !flags->dryrun) { |
428870ff BB |
1425 | featureflags |= (DMU_BACKUP_FEATURE_DEDUP | |
1426 | DMU_BACKUP_FEATURE_DEDUPPROPS); | |
1b9d8c34 | 1427 | if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd))) { |
428870ff BB |
1428 | zfs_error_aux(zhp->zfs_hdl, strerror(errno)); |
1429 | return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, | |
1430 | errbuf)); | |
1431 | } | |
1432 | dda.outputfd = outfd; | |
1433 | dda.inputfd = pipefd[1]; | |
1434 | dda.dedup_hdl = zhp->zfs_hdl; | |
c65aa5b2 | 1435 | if ((err = pthread_create(&tid, NULL, cksummer, &dda))) { |
428870ff BB |
1436 | (void) close(pipefd[0]); |
1437 | (void) close(pipefd[1]); | |
1438 | zfs_error_aux(zhp->zfs_hdl, strerror(errno)); | |
1439 | return (zfs_error(zhp->zfs_hdl, | |
1440 | EZFS_THREADCREATEFAILED, errbuf)); | |
1441 | } | |
1442 | } | |
1443 | ||
330d06f9 | 1444 | if (flags->replicate || flags->doall || flags->props) { |
34dc7c2f BB |
1445 | dmu_replay_record_t drr = { 0 }; |
1446 | char *packbuf = NULL; | |
1447 | size_t buflen = 0; | |
2598c001 | 1448 | zio_cksum_t zc = { { 0 } }; |
34dc7c2f | 1449 | |
330d06f9 | 1450 | if (flags->replicate || flags->props) { |
34dc7c2f BB |
1451 | nvlist_t *hdrnv; |
1452 | ||
1453 | VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0)); | |
1454 | if (fromsnap) { | |
1455 | VERIFY(0 == nvlist_add_string(hdrnv, | |
1456 | "fromsnap", fromsnap)); | |
1457 | } | |
1458 | VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap)); | |
330d06f9 | 1459 | if (!flags->replicate) { |
428870ff BB |
1460 | VERIFY(0 == nvlist_add_boolean(hdrnv, |
1461 | "not_recursive")); | |
1462 | } | |
34dc7c2f BB |
1463 | |
1464 | err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name, | |
330d06f9 | 1465 | fromsnap, tosnap, flags->replicate, &fss, &fsavl); |
572e2857 | 1466 | if (err) |
428870ff | 1467 | goto err_out; |
34dc7c2f BB |
1468 | VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss)); |
1469 | err = nvlist_pack(hdrnv, &packbuf, &buflen, | |
1470 | NV_ENCODE_XDR, 0); | |
428870ff BB |
1471 | if (debugnvp) |
1472 | *debugnvp = hdrnv; | |
1473 | else | |
1474 | nvlist_free(hdrnv); | |
95fd54a1 | 1475 | if (err) |
428870ff | 1476 | goto stderr_out; |
34dc7c2f BB |
1477 | } |
1478 | ||
330d06f9 MA |
1479 | if (!flags->dryrun) { |
1480 | /* write first begin record */ | |
1481 | drr.drr_type = DRR_BEGIN; | |
1482 | drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; | |
1483 | DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin. | |
1484 | drr_versioninfo, DMU_COMPOUNDSTREAM); | |
1485 | DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin. | |
1486 | drr_versioninfo, featureflags); | |
1487 | (void) snprintf(drr.drr_u.drr_begin.drr_toname, | |
1488 | sizeof (drr.drr_u.drr_begin.drr_toname), | |
1489 | "%s@%s", zhp->zfs_name, tosnap); | |
1490 | drr.drr_payloadlen = buflen; | |
1491 | err = cksum_and_write(&drr, sizeof (drr), &zc, outfd); | |
1492 | ||
1493 | /* write header nvlist */ | |
1494 | if (err != -1 && packbuf != NULL) { | |
1495 | err = cksum_and_write(packbuf, buflen, &zc, | |
1496 | outfd); | |
1497 | } | |
1498 | free(packbuf); | |
1499 | if (err == -1) { | |
330d06f9 MA |
1500 | err = errno; |
1501 | goto stderr_out; | |
1502 | } | |
34dc7c2f | 1503 | |
330d06f9 | 1504 | /* write end record */ |
34dc7c2f BB |
1505 | bzero(&drr, sizeof (drr)); |
1506 | drr.drr_type = DRR_END; | |
1507 | drr.drr_u.drr_end.drr_checksum = zc; | |
1508 | err = write(outfd, &drr, sizeof (drr)); | |
1509 | if (err == -1) { | |
428870ff | 1510 | err = errno; |
428870ff | 1511 | goto stderr_out; |
34dc7c2f | 1512 | } |
330d06f9 MA |
1513 | |
1514 | err = 0; | |
34dc7c2f BB |
1515 | } |
1516 | } | |
1517 | ||
1518 | /* dump each stream */ | |
1519 | sdd.fromsnap = fromsnap; | |
1520 | sdd.tosnap = tosnap; | |
95fd54a1 | 1521 | if (tid != 0) |
428870ff BB |
1522 | sdd.outfd = pipefd[0]; |
1523 | else | |
1524 | sdd.outfd = outfd; | |
330d06f9 MA |
1525 | sdd.replicate = flags->replicate; |
1526 | sdd.doall = flags->doall; | |
1527 | sdd.fromorigin = flags->fromorigin; | |
34dc7c2f BB |
1528 | sdd.fss = fss; |
1529 | sdd.fsavl = fsavl; | |
330d06f9 MA |
1530 | sdd.verbose = flags->verbose; |
1531 | sdd.parsable = flags->parsable; | |
37abac6d | 1532 | sdd.progress = flags->progress; |
330d06f9 | 1533 | sdd.dryrun = flags->dryrun; |
f1512ee6 | 1534 | sdd.large_block = flags->largeblock; |
9b67f605 | 1535 | sdd.embed_data = flags->embed_data; |
428870ff BB |
1536 | sdd.filter_cb = filter_func; |
1537 | sdd.filter_cb_arg = cb_arg; | |
1538 | if (debugnvp) | |
1539 | sdd.debugnv = *debugnvp; | |
e956d651 CS |
1540 | |
1541 | /* | |
1542 | * Some flags require that we place user holds on the datasets that are | |
1543 | * being sent so they don't get destroyed during the send. We can skip | |
1544 | * this step if the pool is imported read-only since the datasets cannot | |
1545 | * be destroyed. | |
1546 | */ | |
1547 | if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp), | |
1548 | ZPOOL_PROP_READONLY, NULL) && | |
1549 | zfs_spa_version(zhp, &spa_version) == 0 && | |
1550 | spa_version >= SPA_VERSION_USERREFS && | |
1551 | (flags->doall || flags->replicate)) { | |
572e2857 BB |
1552 | ++holdseq; |
1553 | (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag), | |
1554 | ".send-%d-%llu", getpid(), (u_longlong_t)holdseq); | |
325f0235 | 1555 | sdd.cleanup_fd = open(ZFS_DEV, O_RDWR); |
572e2857 BB |
1556 | if (sdd.cleanup_fd < 0) { |
1557 | err = errno; | |
1558 | goto stderr_out; | |
1559 | } | |
95fd54a1 | 1560 | sdd.snapholds = fnvlist_alloc(); |
572e2857 BB |
1561 | } else { |
1562 | sdd.cleanup_fd = -1; | |
95fd54a1 | 1563 | sdd.snapholds = NULL; |
572e2857 | 1564 | } |
95fd54a1 | 1565 | if (flags->verbose || sdd.snapholds != NULL) { |
330d06f9 MA |
1566 | /* |
1567 | * Do a verbose no-op dry run to get all the verbose output | |
95fd54a1 SH |
1568 | * or to gather snapshot hold's before generating any data, |
1569 | * then do a non-verbose real run to generate the streams. | |
330d06f9 MA |
1570 | */ |
1571 | sdd.dryrun = B_TRUE; | |
1572 | err = dump_filesystems(zhp, &sdd); | |
95fd54a1 SH |
1573 | |
1574 | if (err != 0) | |
1575 | goto stderr_out; | |
1576 | ||
1577 | if (flags->verbose) { | |
1578 | if (flags->parsable) { | |
1579 | (void) fprintf(stderr, "size\t%llu\n", | |
1580 | (longlong_t)sdd.size); | |
1581 | } else { | |
1582 | char buf[16]; | |
1583 | zfs_nicenum(sdd.size, buf, sizeof (buf)); | |
1584 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
1585 | "total estimated size is %s\n"), buf); | |
1586 | } | |
330d06f9 | 1587 | } |
95fd54a1 SH |
1588 | |
1589 | /* Ensure no snaps found is treated as an error. */ | |
1590 | if (!sdd.seento) { | |
1591 | err = ENOENT; | |
1592 | goto err_out; | |
1593 | } | |
1594 | ||
1595 | /* Skip the second run if dryrun was requested. */ | |
1596 | if (flags->dryrun) | |
1597 | goto err_out; | |
1598 | ||
1599 | if (sdd.snapholds != NULL) { | |
1600 | err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds); | |
1601 | if (err != 0) | |
1602 | goto stderr_out; | |
1603 | ||
1604 | fnvlist_free(sdd.snapholds); | |
1605 | sdd.snapholds = NULL; | |
1606 | } | |
1607 | ||
1608 | sdd.dryrun = B_FALSE; | |
1609 | sdd.verbose = B_FALSE; | |
330d06f9 | 1610 | } |
95fd54a1 | 1611 | |
34dc7c2f BB |
1612 | err = dump_filesystems(zhp, &sdd); |
1613 | fsavl_destroy(fsavl); | |
1614 | nvlist_free(fss); | |
1615 | ||
95fd54a1 SH |
1616 | /* Ensure no snaps found is treated as an error. */ |
1617 | if (err == 0 && !sdd.seento) | |
1618 | err = ENOENT; | |
1619 | ||
1620 | if (tid != 0) { | |
1621 | if (err != 0) | |
1622 | (void) pthread_cancel(tid); | |
95fd54a1 | 1623 | (void) close(pipefd[0]); |
6389d422 | 1624 | (void) pthread_join(tid, NULL); |
428870ff BB |
1625 | } |
1626 | ||
572e2857 BB |
1627 | if (sdd.cleanup_fd != -1) { |
1628 | VERIFY(0 == close(sdd.cleanup_fd)); | |
1629 | sdd.cleanup_fd = -1; | |
1630 | } | |
1631 | ||
330d06f9 MA |
1632 | if (!flags->dryrun && (flags->replicate || flags->doall || |
1633 | flags->props)) { | |
34dc7c2f BB |
1634 | /* |
1635 | * write final end record. NB: want to do this even if | |
1636 | * there was some error, because it might not be totally | |
1637 | * failed. | |
1638 | */ | |
1639 | dmu_replay_record_t drr = { 0 }; | |
1640 | drr.drr_type = DRR_END; | |
1641 | if (write(outfd, &drr, sizeof (drr)) == -1) { | |
1642 | return (zfs_standard_error(zhp->zfs_hdl, | |
1643 | errno, errbuf)); | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | return (err || sdd.err); | |
428870ff BB |
1648 | |
1649 | stderr_out: | |
1650 | err = zfs_standard_error(zhp->zfs_hdl, err, errbuf); | |
1651 | err_out: | |
95fd54a1 SH |
1652 | fsavl_destroy(fsavl); |
1653 | nvlist_free(fss); | |
1654 | fnvlist_free(sdd.snapholds); | |
1655 | ||
572e2857 BB |
1656 | if (sdd.cleanup_fd != -1) |
1657 | VERIFY(0 == close(sdd.cleanup_fd)); | |
95fd54a1 | 1658 | if (tid != 0) { |
428870ff | 1659 | (void) pthread_cancel(tid); |
428870ff | 1660 | (void) close(pipefd[0]); |
6389d422 | 1661 | (void) pthread_join(tid, NULL); |
428870ff BB |
1662 | } |
1663 | return (err); | |
34dc7c2f BB |
1664 | } |
1665 | ||
da536844 | 1666 | int |
9b67f605 MA |
1667 | zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, |
1668 | enum lzc_send_flags flags) | |
da536844 MA |
1669 | { |
1670 | int err; | |
1671 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
1672 | ||
1673 | char errbuf[1024]; | |
1674 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
1675 | "warning: cannot send '%s'"), zhp->zfs_name); | |
1676 | ||
9b67f605 | 1677 | err = lzc_send(zhp->zfs_name, from, fd, flags); |
da536844 MA |
1678 | if (err != 0) { |
1679 | switch (errno) { | |
1680 | case EXDEV: | |
1681 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1682 | "not an earlier snapshot from the same fs")); | |
1683 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
1684 | ||
1685 | case ENOENT: | |
1686 | case ESRCH: | |
1687 | if (lzc_exists(zhp->zfs_name)) { | |
1688 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1689 | "incremental source (%s) does not exist"), | |
1690 | from); | |
1691 | } | |
1692 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
1693 | ||
1694 | case EBUSY: | |
1695 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1696 | "target is busy; if a filesystem, " | |
1697 | "it must not be mounted")); | |
1698 | return (zfs_error(hdl, EZFS_BUSY, errbuf)); | |
1699 | ||
1700 | case EDQUOT: | |
1701 | case EFBIG: | |
1702 | case EIO: | |
1703 | case ENOLINK: | |
1704 | case ENOSPC: | |
1705 | case ENOSTR: | |
1706 | case ENXIO: | |
1707 | case EPIPE: | |
1708 | case ERANGE: | |
1709 | case EFAULT: | |
1710 | case EROFS: | |
1711 | zfs_error_aux(hdl, strerror(errno)); | |
1712 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
1713 | ||
1714 | default: | |
1715 | return (zfs_standard_error(hdl, errno, errbuf)); | |
1716 | } | |
1717 | } | |
1718 | return (err != 0); | |
1719 | } | |
1720 | ||
34dc7c2f BB |
1721 | /* |
1722 | * Routines specific to "zfs recv" | |
1723 | */ | |
1724 | ||
1725 | static int | |
1726 | recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, | |
1727 | boolean_t byteswap, zio_cksum_t *zc) | |
1728 | { | |
1729 | char *cp = buf; | |
1730 | int rv; | |
1731 | int len = ilen; | |
1732 | ||
1733 | do { | |
1734 | rv = read(fd, cp, len); | |
1735 | cp += rv; | |
1736 | len -= rv; | |
1737 | } while (rv > 0); | |
1738 | ||
1739 | if (rv < 0 || len != 0) { | |
1740 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1741 | "failed to read from stream")); | |
1742 | return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, | |
1743 | "cannot receive"))); | |
1744 | } | |
1745 | ||
1746 | if (zc) { | |
1747 | if (byteswap) | |
1748 | fletcher_4_incremental_byteswap(buf, ilen, zc); | |
1749 | else | |
1750 | fletcher_4_incremental_native(buf, ilen, zc); | |
1751 | } | |
1752 | return (0); | |
1753 | } | |
1754 | ||
1755 | static int | |
1756 | recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, | |
1757 | boolean_t byteswap, zio_cksum_t *zc) | |
1758 | { | |
1759 | char *buf; | |
1760 | int err; | |
1761 | ||
1762 | buf = zfs_alloc(hdl, len); | |
1763 | if (buf == NULL) | |
1764 | return (ENOMEM); | |
1765 | ||
1766 | err = recv_read(hdl, fd, buf, len, byteswap, zc); | |
1767 | if (err != 0) { | |
1768 | free(buf); | |
1769 | return (err); | |
1770 | } | |
1771 | ||
1772 | err = nvlist_unpack(buf, len, nvp, 0); | |
1773 | free(buf); | |
1774 | if (err != 0) { | |
1775 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
1776 | "stream (malformed nvlist)")); | |
1777 | return (EINVAL); | |
1778 | } | |
1779 | return (0); | |
1780 | } | |
1781 | ||
1782 | static int | |
1783 | recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, | |
330d06f9 | 1784 | int baselen, char *newname, recvflags_t *flags) |
34dc7c2f BB |
1785 | { |
1786 | static int seq; | |
13fe0198 | 1787 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1788 | int err; |
1789 | prop_changelist_t *clp; | |
1790 | zfs_handle_t *zhp; | |
1791 | ||
1792 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
1793 | if (zhp == NULL) | |
1794 | return (-1); | |
b128c09f | 1795 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, |
330d06f9 | 1796 | flags->force ? MS_FORCE : 0); |
34dc7c2f BB |
1797 | zfs_close(zhp); |
1798 | if (clp == NULL) | |
1799 | return (-1); | |
1800 | err = changelist_prefix(clp); | |
1801 | if (err) | |
1802 | return (err); | |
1803 | ||
45d1cae3 BB |
1804 | zc.zc_objset_type = DMU_OST_ZFS; |
1805 | (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); | |
1806 | ||
34dc7c2f BB |
1807 | if (tryname) { |
1808 | (void) strcpy(newname, tryname); | |
1809 | ||
34dc7c2f BB |
1810 | (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value)); |
1811 | ||
330d06f9 | 1812 | if (flags->verbose) { |
34dc7c2f BB |
1813 | (void) printf("attempting rename %s to %s\n", |
1814 | zc.zc_name, zc.zc_value); | |
1815 | } | |
1816 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); | |
1817 | if (err == 0) | |
1818 | changelist_rename(clp, name, tryname); | |
1819 | } else { | |
1820 | err = ENOENT; | |
1821 | } | |
1822 | ||
13fe0198 | 1823 | if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) { |
34dc7c2f BB |
1824 | seq++; |
1825 | ||
13fe0198 MA |
1826 | (void) snprintf(newname, ZFS_MAXNAMELEN, "%.*srecv-%u-%u", |
1827 | baselen, name, getpid(), seq); | |
34dc7c2f BB |
1828 | (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value)); |
1829 | ||
330d06f9 | 1830 | if (flags->verbose) { |
34dc7c2f BB |
1831 | (void) printf("failed - trying rename %s to %s\n", |
1832 | zc.zc_name, zc.zc_value); | |
1833 | } | |
1834 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); | |
1835 | if (err == 0) | |
1836 | changelist_rename(clp, name, newname); | |
330d06f9 | 1837 | if (err && flags->verbose) { |
34dc7c2f BB |
1838 | (void) printf("failed (%u) - " |
1839 | "will try again on next pass\n", errno); | |
1840 | } | |
1841 | err = EAGAIN; | |
330d06f9 | 1842 | } else if (flags->verbose) { |
34dc7c2f BB |
1843 | if (err == 0) |
1844 | (void) printf("success\n"); | |
1845 | else | |
1846 | (void) printf("failed (%u)\n", errno); | |
1847 | } | |
1848 | ||
1849 | (void) changelist_postfix(clp); | |
1850 | changelist_free(clp); | |
1851 | ||
1852 | return (err); | |
1853 | } | |
1854 | ||
1855 | static int | |
1856 | recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, | |
330d06f9 | 1857 | char *newname, recvflags_t *flags) |
34dc7c2f | 1858 | { |
13fe0198 | 1859 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1860 | int err = 0; |
1861 | prop_changelist_t *clp; | |
1862 | zfs_handle_t *zhp; | |
45d1cae3 BB |
1863 | boolean_t defer = B_FALSE; |
1864 | int spa_version; | |
34dc7c2f BB |
1865 | |
1866 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
1867 | if (zhp == NULL) | |
1868 | return (-1); | |
b128c09f | 1869 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, |
330d06f9 | 1870 | flags->force ? MS_FORCE : 0); |
45d1cae3 BB |
1871 | if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && |
1872 | zfs_spa_version(zhp, &spa_version) == 0 && | |
1873 | spa_version >= SPA_VERSION_USERREFS) | |
1874 | defer = B_TRUE; | |
34dc7c2f BB |
1875 | zfs_close(zhp); |
1876 | if (clp == NULL) | |
1877 | return (-1); | |
1878 | err = changelist_prefix(clp); | |
1879 | if (err) | |
1880 | return (err); | |
1881 | ||
1882 | zc.zc_objset_type = DMU_OST_ZFS; | |
45d1cae3 | 1883 | zc.zc_defer_destroy = defer; |
34dc7c2f BB |
1884 | (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); |
1885 | ||
330d06f9 | 1886 | if (flags->verbose) |
34dc7c2f BB |
1887 | (void) printf("attempting destroy %s\n", zc.zc_name); |
1888 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc); | |
34dc7c2f | 1889 | if (err == 0) { |
330d06f9 | 1890 | if (flags->verbose) |
34dc7c2f BB |
1891 | (void) printf("success\n"); |
1892 | changelist_remove(clp, zc.zc_name); | |
1893 | } | |
1894 | ||
1895 | (void) changelist_postfix(clp); | |
1896 | changelist_free(clp); | |
1897 | ||
45d1cae3 | 1898 | /* |
428870ff BB |
1899 | * Deferred destroy might destroy the snapshot or only mark it to be |
1900 | * destroyed later, and it returns success in either case. | |
45d1cae3 | 1901 | */ |
428870ff BB |
1902 | if (err != 0 || (defer && zfs_dataset_exists(hdl, name, |
1903 | ZFS_TYPE_SNAPSHOT))) { | |
34dc7c2f | 1904 | err = recv_rename(hdl, name, NULL, baselen, newname, flags); |
428870ff | 1905 | } |
34dc7c2f BB |
1906 | |
1907 | return (err); | |
1908 | } | |
1909 | ||
1910 | typedef struct guid_to_name_data { | |
1911 | uint64_t guid; | |
1912 | char *name; | |
330d06f9 | 1913 | char *skip; |
34dc7c2f BB |
1914 | } guid_to_name_data_t; |
1915 | ||
1916 | static int | |
1917 | guid_to_name_cb(zfs_handle_t *zhp, void *arg) | |
1918 | { | |
1919 | guid_to_name_data_t *gtnd = arg; | |
1920 | int err; | |
1921 | ||
330d06f9 MA |
1922 | if (gtnd->skip != NULL && |
1923 | strcmp(zhp->zfs_name, gtnd->skip) == 0) { | |
1924 | return (0); | |
1925 | } | |
1926 | ||
34dc7c2f BB |
1927 | if (zhp->zfs_dmustats.dds_guid == gtnd->guid) { |
1928 | (void) strcpy(gtnd->name, zhp->zfs_name); | |
428870ff | 1929 | zfs_close(zhp); |
34dc7c2f BB |
1930 | return (EEXIST); |
1931 | } | |
330d06f9 | 1932 | |
34dc7c2f BB |
1933 | err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); |
1934 | zfs_close(zhp); | |
1935 | return (err); | |
1936 | } | |
1937 | ||
330d06f9 MA |
1938 | /* |
1939 | * Attempt to find the local dataset associated with this guid. In the case of | |
1940 | * multiple matches, we attempt to find the "best" match by searching | |
1941 | * progressively larger portions of the hierarchy. This allows one to send a | |
1942 | * tree of datasets individually and guarantee that we will find the source | |
1943 | * guid within that hierarchy, even if there are multiple matches elsewhere. | |
1944 | */ | |
34dc7c2f BB |
1945 | static int |
1946 | guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, | |
1947 | char *name) | |
1948 | { | |
1949 | /* exhaustive search all local snapshots */ | |
330d06f9 | 1950 | char pname[ZFS_MAXNAMELEN]; |
34dc7c2f BB |
1951 | guid_to_name_data_t gtnd; |
1952 | int err = 0; | |
1953 | zfs_handle_t *zhp; | |
1954 | char *cp; | |
1955 | ||
1956 | gtnd.guid = guid; | |
1957 | gtnd.name = name; | |
330d06f9 | 1958 | gtnd.skip = NULL; |
34dc7c2f | 1959 | |
330d06f9 | 1960 | (void) strlcpy(pname, parent, sizeof (pname)); |
34dc7c2f | 1961 | |
330d06f9 MA |
1962 | /* |
1963 | * Search progressively larger portions of the hierarchy. This will | |
1964 | * select the "most local" version of the origin snapshot in the case | |
1965 | * that there are multiple matching snapshots in the system. | |
1966 | */ | |
1967 | while ((cp = strrchr(pname, '/')) != NULL) { | |
1968 | ||
1969 | /* Chop off the last component and open the parent */ | |
34dc7c2f | 1970 | *cp = '\0'; |
330d06f9 MA |
1971 | zhp = make_dataset_handle(hdl, pname); |
1972 | ||
1973 | if (zhp == NULL) | |
1974 | continue; | |
34dc7c2f | 1975 | |
34dc7c2f BB |
1976 | err = zfs_iter_children(zhp, guid_to_name_cb, >nd); |
1977 | zfs_close(zhp); | |
330d06f9 MA |
1978 | if (err == EEXIST) |
1979 | return (0); | |
34dc7c2f | 1980 | |
330d06f9 MA |
1981 | /* |
1982 | * Remember the dataset that we already searched, so we | |
1983 | * skip it next time through. | |
1984 | */ | |
1985 | gtnd.skip = pname; | |
1986 | } | |
34dc7c2f | 1987 | |
330d06f9 | 1988 | return (ENOENT); |
34dc7c2f BB |
1989 | } |
1990 | ||
1991 | /* | |
330d06f9 MA |
1992 | * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if |
1993 | * guid1 is after guid2. | |
34dc7c2f BB |
1994 | */ |
1995 | static int | |
1996 | created_before(libzfs_handle_t *hdl, avl_tree_t *avl, | |
1997 | uint64_t guid1, uint64_t guid2) | |
1998 | { | |
1999 | nvlist_t *nvfs; | |
2000 | char *fsname, *snapname; | |
2001 | char buf[ZFS_MAXNAMELEN]; | |
2002 | int rv; | |
330d06f9 MA |
2003 | zfs_handle_t *guid1hdl, *guid2hdl; |
2004 | uint64_t create1, create2; | |
34dc7c2f BB |
2005 | |
2006 | if (guid2 == 0) | |
2007 | return (0); | |
2008 | if (guid1 == 0) | |
2009 | return (1); | |
2010 | ||
2011 | nvfs = fsavl_find(avl, guid1, &snapname); | |
2012 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2013 | (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); | |
330d06f9 MA |
2014 | guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); |
2015 | if (guid1hdl == NULL) | |
34dc7c2f BB |
2016 | return (-1); |
2017 | ||
2018 | nvfs = fsavl_find(avl, guid2, &snapname); | |
2019 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2020 | (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); | |
330d06f9 MA |
2021 | guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); |
2022 | if (guid2hdl == NULL) { | |
2023 | zfs_close(guid1hdl); | |
34dc7c2f BB |
2024 | return (-1); |
2025 | } | |
2026 | ||
330d06f9 MA |
2027 | create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG); |
2028 | create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG); | |
34dc7c2f | 2029 | |
330d06f9 MA |
2030 | if (create1 < create2) |
2031 | rv = -1; | |
2032 | else if (create1 > create2) | |
2033 | rv = +1; | |
2034 | else | |
2035 | rv = 0; | |
2036 | ||
2037 | zfs_close(guid1hdl); | |
2038 | zfs_close(guid2hdl); | |
34dc7c2f BB |
2039 | |
2040 | return (rv); | |
2041 | } | |
2042 | ||
2043 | static int | |
2044 | recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, | |
330d06f9 | 2045 | recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl, |
428870ff | 2046 | nvlist_t *renamed) |
34dc7c2f | 2047 | { |
7509a3d2 | 2048 | nvlist_t *local_nv, *deleted = NULL; |
34dc7c2f BB |
2049 | avl_tree_t *local_avl; |
2050 | nvpair_t *fselem, *nextfselem; | |
428870ff | 2051 | char *fromsnap; |
34dc7c2f | 2052 | char newname[ZFS_MAXNAMELEN]; |
7509a3d2 | 2053 | char guidname[32]; |
34dc7c2f | 2054 | int error; |
428870ff BB |
2055 | boolean_t needagain, progress, recursive; |
2056 | char *s1, *s2; | |
34dc7c2f BB |
2057 | |
2058 | VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap)); | |
428870ff BB |
2059 | |
2060 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == | |
2061 | ENOENT); | |
34dc7c2f | 2062 | |
330d06f9 | 2063 | if (flags->dryrun) |
34dc7c2f BB |
2064 | return (0); |
2065 | ||
2066 | again: | |
2067 | needagain = progress = B_FALSE; | |
2068 | ||
7509a3d2 | 2069 | VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0)); |
2070 | ||
34dc7c2f | 2071 | if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, |
428870ff | 2072 | recursive, &local_nv, &local_avl)) != 0) |
34dc7c2f BB |
2073 | return (error); |
2074 | ||
2075 | /* | |
2076 | * Process deletes and renames | |
2077 | */ | |
2078 | for (fselem = nvlist_next_nvpair(local_nv, NULL); | |
2079 | fselem; fselem = nextfselem) { | |
2080 | nvlist_t *nvfs, *snaps; | |
2081 | nvlist_t *stream_nvfs = NULL; | |
2082 | nvpair_t *snapelem, *nextsnapelem; | |
2083 | uint64_t fromguid = 0; | |
2084 | uint64_t originguid = 0; | |
2085 | uint64_t stream_originguid = 0; | |
2086 | uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; | |
2087 | char *fsname, *stream_fsname; | |
2088 | ||
2089 | nextfselem = nvlist_next_nvpair(local_nv, fselem); | |
2090 | ||
2091 | VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); | |
2092 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); | |
2093 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2094 | VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap", | |
2095 | &parent_fromsnap_guid)); | |
2096 | (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); | |
2097 | ||
2098 | /* | |
2099 | * First find the stream's fs, so we can check for | |
2100 | * a different origin (due to "zfs promote") | |
2101 | */ | |
2102 | for (snapelem = nvlist_next_nvpair(snaps, NULL); | |
2103 | snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { | |
2104 | uint64_t thisguid; | |
2105 | ||
2106 | VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); | |
2107 | stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); | |
2108 | ||
2109 | if (stream_nvfs != NULL) | |
2110 | break; | |
2111 | } | |
2112 | ||
2113 | /* check for promote */ | |
2114 | (void) nvlist_lookup_uint64(stream_nvfs, "origin", | |
2115 | &stream_originguid); | |
2116 | if (stream_nvfs && originguid != stream_originguid) { | |
2117 | switch (created_before(hdl, local_avl, | |
2118 | stream_originguid, originguid)) { | |
2119 | case 1: { | |
2120 | /* promote it! */ | |
13fe0198 | 2121 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
2122 | nvlist_t *origin_nvfs; |
2123 | char *origin_fsname; | |
2124 | ||
330d06f9 | 2125 | if (flags->verbose) |
34dc7c2f BB |
2126 | (void) printf("promoting %s\n", fsname); |
2127 | ||
2128 | origin_nvfs = fsavl_find(local_avl, originguid, | |
2129 | NULL); | |
2130 | VERIFY(0 == nvlist_lookup_string(origin_nvfs, | |
2131 | "name", &origin_fsname)); | |
2132 | (void) strlcpy(zc.zc_value, origin_fsname, | |
2133 | sizeof (zc.zc_value)); | |
2134 | (void) strlcpy(zc.zc_name, fsname, | |
2135 | sizeof (zc.zc_name)); | |
2136 | error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); | |
2137 | if (error == 0) | |
2138 | progress = B_TRUE; | |
2139 | break; | |
2140 | } | |
2141 | default: | |
2142 | break; | |
2143 | case -1: | |
2144 | fsavl_destroy(local_avl); | |
2145 | nvlist_free(local_nv); | |
2146 | return (-1); | |
2147 | } | |
2148 | /* | |
2149 | * We had/have the wrong origin, therefore our | |
2150 | * list of snapshots is wrong. Need to handle | |
2151 | * them on the next pass. | |
2152 | */ | |
2153 | needagain = B_TRUE; | |
2154 | continue; | |
2155 | } | |
2156 | ||
2157 | for (snapelem = nvlist_next_nvpair(snaps, NULL); | |
2158 | snapelem; snapelem = nextsnapelem) { | |
2159 | uint64_t thisguid; | |
2160 | char *stream_snapname; | |
b128c09f | 2161 | nvlist_t *found, *props; |
34dc7c2f BB |
2162 | |
2163 | nextsnapelem = nvlist_next_nvpair(snaps, snapelem); | |
2164 | ||
2165 | VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); | |
2166 | found = fsavl_find(stream_avl, thisguid, | |
2167 | &stream_snapname); | |
2168 | ||
2169 | /* check for delete */ | |
2170 | if (found == NULL) { | |
2171 | char name[ZFS_MAXNAMELEN]; | |
2172 | ||
330d06f9 | 2173 | if (!flags->force) |
34dc7c2f BB |
2174 | continue; |
2175 | ||
2176 | (void) snprintf(name, sizeof (name), "%s@%s", | |
2177 | fsname, nvpair_name(snapelem)); | |
2178 | ||
2179 | error = recv_destroy(hdl, name, | |
2180 | strlen(fsname)+1, newname, flags); | |
2181 | if (error) | |
2182 | needagain = B_TRUE; | |
2183 | else | |
2184 | progress = B_TRUE; | |
3df29340 BB |
2185 | sprintf(guidname, "%llu", |
2186 | (u_longlong_t)thisguid); | |
7509a3d2 | 2187 | nvlist_add_boolean(deleted, guidname); |
34dc7c2f BB |
2188 | continue; |
2189 | } | |
2190 | ||
2191 | stream_nvfs = found; | |
2192 | ||
b128c09f BB |
2193 | if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops", |
2194 | &props) && 0 == nvlist_lookup_nvlist(props, | |
2195 | stream_snapname, &props)) { | |
13fe0198 | 2196 | zfs_cmd_t zc = {"\0"}; |
b128c09f | 2197 | |
428870ff | 2198 | zc.zc_cookie = B_TRUE; /* received */ |
b128c09f BB |
2199 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), |
2200 | "%s@%s", fsname, nvpair_name(snapelem)); | |
2201 | if (zcmd_write_src_nvlist(hdl, &zc, | |
2202 | props) == 0) { | |
2203 | (void) zfs_ioctl(hdl, | |
2204 | ZFS_IOC_SET_PROP, &zc); | |
2205 | zcmd_free_nvlists(&zc); | |
2206 | } | |
2207 | } | |
2208 | ||
34dc7c2f BB |
2209 | /* check for different snapname */ |
2210 | if (strcmp(nvpair_name(snapelem), | |
2211 | stream_snapname) != 0) { | |
2212 | char name[ZFS_MAXNAMELEN]; | |
2213 | char tryname[ZFS_MAXNAMELEN]; | |
2214 | ||
2215 | (void) snprintf(name, sizeof (name), "%s@%s", | |
2216 | fsname, nvpair_name(snapelem)); | |
2217 | (void) snprintf(tryname, sizeof (name), "%s@%s", | |
2218 | fsname, stream_snapname); | |
2219 | ||
2220 | error = recv_rename(hdl, name, tryname, | |
2221 | strlen(fsname)+1, newname, flags); | |
2222 | if (error) | |
2223 | needagain = B_TRUE; | |
2224 | else | |
2225 | progress = B_TRUE; | |
2226 | } | |
2227 | ||
2228 | if (strcmp(stream_snapname, fromsnap) == 0) | |
2229 | fromguid = thisguid; | |
2230 | } | |
2231 | ||
2232 | /* check for delete */ | |
2233 | if (stream_nvfs == NULL) { | |
330d06f9 | 2234 | if (!flags->force) |
34dc7c2f BB |
2235 | continue; |
2236 | ||
2237 | error = recv_destroy(hdl, fsname, strlen(tofs)+1, | |
2238 | newname, flags); | |
2239 | if (error) | |
2240 | needagain = B_TRUE; | |
2241 | else | |
2242 | progress = B_TRUE; | |
3df29340 BB |
2243 | sprintf(guidname, "%llu", |
2244 | (u_longlong_t) parent_fromsnap_guid); | |
7509a3d2 | 2245 | nvlist_add_boolean(deleted, guidname); |
34dc7c2f BB |
2246 | continue; |
2247 | } | |
2248 | ||
428870ff | 2249 | if (fromguid == 0) { |
330d06f9 | 2250 | if (flags->verbose) { |
428870ff BB |
2251 | (void) printf("local fs %s does not have " |
2252 | "fromsnap (%s in stream); must have " | |
2253 | "been deleted locally; ignoring\n", | |
2254 | fsname, fromsnap); | |
2255 | } | |
34dc7c2f BB |
2256 | continue; |
2257 | } | |
2258 | ||
2259 | VERIFY(0 == nvlist_lookup_string(stream_nvfs, | |
2260 | "name", &stream_fsname)); | |
2261 | VERIFY(0 == nvlist_lookup_uint64(stream_nvfs, | |
2262 | "parentfromsnap", &stream_parent_fromsnap_guid)); | |
2263 | ||
428870ff BB |
2264 | s1 = strrchr(fsname, '/'); |
2265 | s2 = strrchr(stream_fsname, '/'); | |
2266 | ||
7509a3d2 | 2267 | /* |
2268 | * Check if we're going to rename based on parent guid change | |
2269 | * and the current parent guid was also deleted. If it was then | |
2270 | * rename will fail and is likely unneeded, so avoid this and | |
2271 | * force an early retry to determine the new | |
2272 | * parent_fromsnap_guid. | |
2273 | */ | |
2274 | if (stream_parent_fromsnap_guid != 0 && | |
2275 | parent_fromsnap_guid != 0 && | |
2276 | stream_parent_fromsnap_guid != parent_fromsnap_guid) { | |
3df29340 BB |
2277 | sprintf(guidname, "%llu", |
2278 | (u_longlong_t) parent_fromsnap_guid); | |
7509a3d2 | 2279 | if (nvlist_exists(deleted, guidname)) { |
2280 | progress = B_TRUE; | |
2281 | needagain = B_TRUE; | |
2282 | goto doagain; | |
2283 | } | |
2284 | } | |
2285 | ||
428870ff BB |
2286 | /* |
2287 | * Check for rename. If the exact receive path is specified, it | |
2288 | * does not count as a rename, but we still need to check the | |
2289 | * datasets beneath it. | |
2290 | */ | |
34dc7c2f | 2291 | if ((stream_parent_fromsnap_guid != 0 && |
428870ff | 2292 | parent_fromsnap_guid != 0 && |
34dc7c2f | 2293 | stream_parent_fromsnap_guid != parent_fromsnap_guid) || |
330d06f9 | 2294 | ((flags->isprefix || strcmp(tofs, fsname) != 0) && |
428870ff | 2295 | (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) { |
34dc7c2f BB |
2296 | nvlist_t *parent; |
2297 | char tryname[ZFS_MAXNAMELEN]; | |
2298 | ||
2299 | parent = fsavl_find(local_avl, | |
2300 | stream_parent_fromsnap_guid, NULL); | |
2301 | /* | |
2302 | * NB: parent might not be found if we used the | |
2303 | * tosnap for stream_parent_fromsnap_guid, | |
2304 | * because the parent is a newly-created fs; | |
2305 | * we'll be able to rename it after we recv the | |
2306 | * new fs. | |
2307 | */ | |
2308 | if (parent != NULL) { | |
2309 | char *pname; | |
2310 | ||
2311 | VERIFY(0 == nvlist_lookup_string(parent, "name", | |
2312 | &pname)); | |
2313 | (void) snprintf(tryname, sizeof (tryname), | |
2314 | "%s%s", pname, strrchr(stream_fsname, '/')); | |
2315 | } else { | |
2316 | tryname[0] = '\0'; | |
330d06f9 | 2317 | if (flags->verbose) { |
34dc7c2f BB |
2318 | (void) printf("local fs %s new parent " |
2319 | "not found\n", fsname); | |
2320 | } | |
2321 | } | |
2322 | ||
428870ff BB |
2323 | newname[0] = '\0'; |
2324 | ||
34dc7c2f BB |
2325 | error = recv_rename(hdl, fsname, tryname, |
2326 | strlen(tofs)+1, newname, flags); | |
428870ff BB |
2327 | |
2328 | if (renamed != NULL && newname[0] != '\0') { | |
2329 | VERIFY(0 == nvlist_add_boolean(renamed, | |
2330 | newname)); | |
2331 | } | |
2332 | ||
34dc7c2f BB |
2333 | if (error) |
2334 | needagain = B_TRUE; | |
2335 | else | |
2336 | progress = B_TRUE; | |
2337 | } | |
2338 | } | |
2339 | ||
7509a3d2 | 2340 | doagain: |
34dc7c2f BB |
2341 | fsavl_destroy(local_avl); |
2342 | nvlist_free(local_nv); | |
7509a3d2 | 2343 | nvlist_free(deleted); |
34dc7c2f BB |
2344 | |
2345 | if (needagain && progress) { | |
2346 | /* do another pass to fix up temporary names */ | |
330d06f9 | 2347 | if (flags->verbose) |
34dc7c2f BB |
2348 | (void) printf("another pass:\n"); |
2349 | goto again; | |
2350 | } | |
2351 | ||
2352 | return (needagain); | |
2353 | } | |
2354 | ||
2355 | static int | |
2356 | zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, | |
330d06f9 | 2357 | recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc, |
572e2857 | 2358 | char **top_zfs, int cleanup_fd, uint64_t *action_handlep) |
34dc7c2f BB |
2359 | { |
2360 | nvlist_t *stream_nv = NULL; | |
2361 | avl_tree_t *stream_avl = NULL; | |
2362 | char *fromsnap = NULL; | |
428870ff | 2363 | char *cp; |
34dc7c2f | 2364 | char tofs[ZFS_MAXNAMELEN]; |
428870ff | 2365 | char sendfs[ZFS_MAXNAMELEN]; |
34dc7c2f BB |
2366 | char errbuf[1024]; |
2367 | dmu_replay_record_t drre; | |
2368 | int error; | |
2369 | boolean_t anyerr = B_FALSE; | |
2370 | boolean_t softerr = B_FALSE; | |
428870ff | 2371 | boolean_t recursive; |
34dc7c2f BB |
2372 | |
2373 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2374 | "cannot receive")); | |
2375 | ||
34dc7c2f BB |
2376 | assert(drr->drr_type == DRR_BEGIN); |
2377 | assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); | |
428870ff BB |
2378 | assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) == |
2379 | DMU_COMPOUNDSTREAM); | |
34dc7c2f BB |
2380 | |
2381 | /* | |
2382 | * Read in the nvlist from the stream. | |
2383 | */ | |
2384 | if (drr->drr_payloadlen != 0) { | |
34dc7c2f | 2385 | error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, |
330d06f9 | 2386 | &stream_nv, flags->byteswap, zc); |
34dc7c2f BB |
2387 | if (error) { |
2388 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2389 | goto out; | |
2390 | } | |
2391 | } | |
2392 | ||
428870ff BB |
2393 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == |
2394 | ENOENT); | |
2395 | ||
2396 | if (recursive && strchr(destname, '@')) { | |
2397 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2398 | "cannot specify snapshot name for multi-snapshot stream")); | |
2399 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2400 | goto out; | |
2401 | } | |
2402 | ||
34dc7c2f BB |
2403 | /* |
2404 | * Read in the end record and verify checksum. | |
2405 | */ | |
2406 | if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), | |
330d06f9 | 2407 | flags->byteswap, NULL))) |
34dc7c2f | 2408 | goto out; |
330d06f9 | 2409 | if (flags->byteswap) { |
34dc7c2f BB |
2410 | drre.drr_type = BSWAP_32(drre.drr_type); |
2411 | drre.drr_u.drr_end.drr_checksum.zc_word[0] = | |
2412 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); | |
2413 | drre.drr_u.drr_end.drr_checksum.zc_word[1] = | |
2414 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); | |
2415 | drre.drr_u.drr_end.drr_checksum.zc_word[2] = | |
2416 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); | |
2417 | drre.drr_u.drr_end.drr_checksum.zc_word[3] = | |
2418 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); | |
2419 | } | |
2420 | if (drre.drr_type != DRR_END) { | |
2421 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2422 | goto out; | |
2423 | } | |
2424 | if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { | |
2425 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2426 | "incorrect header checksum")); | |
2427 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2428 | goto out; | |
2429 | } | |
2430 | ||
2431 | (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); | |
2432 | ||
2433 | if (drr->drr_payloadlen != 0) { | |
2434 | nvlist_t *stream_fss; | |
2435 | ||
2436 | VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", | |
2437 | &stream_fss)); | |
2438 | if ((stream_avl = fsavl_create(stream_fss)) == NULL) { | |
2439 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2440 | "couldn't allocate avl tree")); | |
2441 | error = zfs_error(hdl, EZFS_NOMEM, errbuf); | |
2442 | goto out; | |
2443 | } | |
2444 | ||
2445 | if (fromsnap != NULL) { | |
428870ff BB |
2446 | nvlist_t *renamed = NULL; |
2447 | nvpair_t *pair = NULL; | |
2448 | ||
34dc7c2f | 2449 | (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN); |
330d06f9 | 2450 | if (flags->isprefix) { |
428870ff BB |
2451 | struct drr_begin *drrb = &drr->drr_u.drr_begin; |
2452 | int i; | |
2453 | ||
330d06f9 | 2454 | if (flags->istail) { |
428870ff BB |
2455 | cp = strrchr(drrb->drr_toname, '/'); |
2456 | if (cp == NULL) { | |
2457 | (void) strlcat(tofs, "/", | |
2458 | ZFS_MAXNAMELEN); | |
2459 | i = 0; | |
2460 | } else { | |
2461 | i = (cp - drrb->drr_toname); | |
2462 | } | |
2463 | } else { | |
2464 | i = strcspn(drrb->drr_toname, "/@"); | |
2465 | } | |
34dc7c2f | 2466 | /* zfs_receive_one() will create_parents() */ |
428870ff | 2467 | (void) strlcat(tofs, &drrb->drr_toname[i], |
34dc7c2f BB |
2468 | ZFS_MAXNAMELEN); |
2469 | *strchr(tofs, '@') = '\0'; | |
2470 | } | |
428870ff | 2471 | |
330d06f9 | 2472 | if (recursive && !flags->dryrun && !flags->nomount) { |
428870ff BB |
2473 | VERIFY(0 == nvlist_alloc(&renamed, |
2474 | NV_UNIQUE_NAME, 0)); | |
2475 | } | |
2476 | ||
2477 | softerr = recv_incremental_replication(hdl, tofs, flags, | |
2478 | stream_nv, stream_avl, renamed); | |
2479 | ||
2480 | /* Unmount renamed filesystems before receiving. */ | |
2481 | while ((pair = nvlist_next_nvpair(renamed, | |
2482 | pair)) != NULL) { | |
2483 | zfs_handle_t *zhp; | |
2484 | prop_changelist_t *clp = NULL; | |
2485 | ||
2486 | zhp = zfs_open(hdl, nvpair_name(pair), | |
2487 | ZFS_TYPE_FILESYSTEM); | |
2488 | if (zhp != NULL) { | |
2489 | clp = changelist_gather(zhp, | |
2490 | ZFS_PROP_MOUNTPOINT, 0, 0); | |
2491 | zfs_close(zhp); | |
2492 | if (clp != NULL) { | |
2493 | softerr |= | |
2494 | changelist_prefix(clp); | |
2495 | changelist_free(clp); | |
2496 | } | |
2497 | } | |
2498 | } | |
2499 | ||
2500 | nvlist_free(renamed); | |
34dc7c2f BB |
2501 | } |
2502 | } | |
2503 | ||
428870ff BB |
2504 | /* |
2505 | * Get the fs specified by the first path in the stream (the top level | |
2506 | * specified by 'zfs send') and pass it to each invocation of | |
2507 | * zfs_receive_one(). | |
2508 | */ | |
2509 | (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname, | |
2510 | ZFS_MAXNAMELEN); | |
2511 | if ((cp = strchr(sendfs, '@')) != NULL) | |
2512 | *cp = '\0'; | |
34dc7c2f BB |
2513 | |
2514 | /* Finally, receive each contained stream */ | |
2515 | do { | |
2516 | /* | |
2517 | * we should figure out if it has a recoverable | |
2518 | * error, in which case do a recv_skip() and drive on. | |
2519 | * Note, if we fail due to already having this guid, | |
2520 | * zfs_receive_one() will take care of it (ie, | |
2521 | * recv_skip() and return 0). | |
2522 | */ | |
b128c09f | 2523 | error = zfs_receive_impl(hdl, destname, flags, fd, |
572e2857 BB |
2524 | sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd, |
2525 | action_handlep); | |
34dc7c2f BB |
2526 | if (error == ENODATA) { |
2527 | error = 0; | |
2528 | break; | |
2529 | } | |
2530 | anyerr |= error; | |
2531 | } while (error == 0); | |
2532 | ||
2533 | if (drr->drr_payloadlen != 0 && fromsnap != NULL) { | |
2534 | /* | |
2535 | * Now that we have the fs's they sent us, try the | |
2536 | * renames again. | |
2537 | */ | |
2538 | softerr = recv_incremental_replication(hdl, tofs, flags, | |
428870ff | 2539 | stream_nv, stream_avl, NULL); |
34dc7c2f BB |
2540 | } |
2541 | ||
2542 | out: | |
2543 | fsavl_destroy(stream_avl); | |
2544 | if (stream_nv) | |
2545 | nvlist_free(stream_nv); | |
2546 | if (softerr) | |
2547 | error = -2; | |
2548 | if (anyerr) | |
2549 | error = -1; | |
2550 | return (error); | |
2551 | } | |
2552 | ||
428870ff BB |
2553 | static void |
2554 | trunc_prop_errs(int truncated) | |
2555 | { | |
2556 | ASSERT(truncated != 0); | |
2557 | ||
2558 | if (truncated == 1) | |
2559 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
2560 | "1 more property could not be set\n")); | |
2561 | else | |
2562 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
2563 | "%d more properties could not be set\n"), truncated); | |
2564 | } | |
2565 | ||
34dc7c2f BB |
2566 | static int |
2567 | recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) | |
2568 | { | |
2569 | dmu_replay_record_t *drr; | |
f1512ee6 | 2570 | void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE); |
428870ff BB |
2571 | char errbuf[1024]; |
2572 | ||
2573 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2574 | "cannot receive:")); | |
34dc7c2f BB |
2575 | |
2576 | /* XXX would be great to use lseek if possible... */ | |
2577 | drr = buf; | |
2578 | ||
2579 | while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), | |
2580 | byteswap, NULL) == 0) { | |
2581 | if (byteswap) | |
2582 | drr->drr_type = BSWAP_32(drr->drr_type); | |
2583 | ||
2584 | switch (drr->drr_type) { | |
2585 | case DRR_BEGIN: | |
2586 | /* NB: not to be used on v2 stream packages */ | |
428870ff BB |
2587 | if (drr->drr_payloadlen != 0) { |
2588 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2589 | "invalid substream header")); | |
2590 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
2591 | } | |
34dc7c2f BB |
2592 | break; |
2593 | ||
2594 | case DRR_END: | |
2595 | free(buf); | |
2596 | return (0); | |
2597 | ||
2598 | case DRR_OBJECT: | |
2599 | if (byteswap) { | |
2600 | drr->drr_u.drr_object.drr_bonuslen = | |
2601 | BSWAP_32(drr->drr_u.drr_object. | |
2602 | drr_bonuslen); | |
2603 | } | |
2604 | (void) recv_read(hdl, fd, buf, | |
2605 | P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8), | |
2606 | B_FALSE, NULL); | |
2607 | break; | |
2608 | ||
2609 | case DRR_WRITE: | |
2610 | if (byteswap) { | |
2611 | drr->drr_u.drr_write.drr_length = | |
b128c09f | 2612 | BSWAP_64(drr->drr_u.drr_write.drr_length); |
34dc7c2f BB |
2613 | } |
2614 | (void) recv_read(hdl, fd, buf, | |
2615 | drr->drr_u.drr_write.drr_length, B_FALSE, NULL); | |
2616 | break; | |
428870ff BB |
2617 | case DRR_SPILL: |
2618 | if (byteswap) { | |
2619 | drr->drr_u.drr_write.drr_length = | |
2620 | BSWAP_64(drr->drr_u.drr_spill.drr_length); | |
2621 | } | |
2622 | (void) recv_read(hdl, fd, buf, | |
2623 | drr->drr_u.drr_spill.drr_length, B_FALSE, NULL); | |
2624 | break; | |
9b67f605 MA |
2625 | case DRR_WRITE_EMBEDDED: |
2626 | if (byteswap) { | |
2627 | drr->drr_u.drr_write_embedded.drr_psize = | |
2628 | BSWAP_32(drr->drr_u.drr_write_embedded. | |
2629 | drr_psize); | |
2630 | } | |
2631 | (void) recv_read(hdl, fd, buf, | |
2632 | P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize, | |
2633 | 8), B_FALSE, NULL); | |
2634 | break; | |
428870ff | 2635 | case DRR_WRITE_BYREF: |
34dc7c2f BB |
2636 | case DRR_FREEOBJECTS: |
2637 | case DRR_FREE: | |
2638 | break; | |
2639 | ||
2640 | default: | |
428870ff BB |
2641 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2642 | "invalid record type")); | |
2643 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
34dc7c2f BB |
2644 | } |
2645 | } | |
2646 | ||
2647 | free(buf); | |
2648 | return (-1); | |
2649 | } | |
2650 | ||
2651 | /* | |
2652 | * Restores a backup of tosnap from the file descriptor specified by infd. | |
2653 | */ | |
2654 | static int | |
2655 | zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, | |
330d06f9 | 2656 | recvflags_t *flags, dmu_replay_record_t *drr, |
428870ff | 2657 | dmu_replay_record_t *drr_noswap, const char *sendfs, |
572e2857 BB |
2658 | nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd, |
2659 | uint64_t *action_handlep) | |
34dc7c2f | 2660 | { |
13fe0198 | 2661 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f | 2662 | time_t begin_time; |
428870ff | 2663 | int ioctl_err, ioctl_errno, err; |
34dc7c2f BB |
2664 | char *cp; |
2665 | struct drr_begin *drrb = &drr->drr_u.drr_begin; | |
2666 | char errbuf[1024]; | |
428870ff BB |
2667 | char prop_errbuf[1024]; |
2668 | const char *chopprefix; | |
34dc7c2f BB |
2669 | boolean_t newfs = B_FALSE; |
2670 | boolean_t stream_wantsnewfs; | |
2671 | uint64_t parent_snapguid = 0; | |
2672 | prop_changelist_t *clp = NULL; | |
b128c09f | 2673 | nvlist_t *snapprops_nvlist = NULL; |
428870ff BB |
2674 | zprop_errflags_t prop_errflags; |
2675 | boolean_t recursive; | |
34dc7c2f BB |
2676 | |
2677 | begin_time = time(NULL); | |
2678 | ||
2679 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2680 | "cannot receive")); | |
2681 | ||
428870ff BB |
2682 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == |
2683 | ENOENT); | |
2684 | ||
34dc7c2f | 2685 | if (stream_avl != NULL) { |
b128c09f BB |
2686 | char *snapname; |
2687 | nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, | |
2688 | &snapname); | |
34dc7c2f BB |
2689 | nvlist_t *props; |
2690 | int ret; | |
2691 | ||
2692 | (void) nvlist_lookup_uint64(fs, "parentfromsnap", | |
2693 | &parent_snapguid); | |
2694 | err = nvlist_lookup_nvlist(fs, "props", &props); | |
2695 | if (err) | |
2696 | VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0)); | |
2697 | ||
330d06f9 | 2698 | if (flags->canmountoff) { |
34dc7c2f BB |
2699 | VERIFY(0 == nvlist_add_uint64(props, |
2700 | zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0)); | |
2701 | } | |
2702 | ret = zcmd_write_src_nvlist(hdl, &zc, props); | |
2703 | if (err) | |
2704 | nvlist_free(props); | |
34dc7c2f BB |
2705 | if (ret != 0) |
2706 | return (-1); | |
2707 | } | |
2708 | ||
428870ff BB |
2709 | cp = NULL; |
2710 | ||
34dc7c2f BB |
2711 | /* |
2712 | * Determine how much of the snapshot name stored in the stream | |
2713 | * we are going to tack on to the name they specified on the | |
2714 | * command line, and how much we are going to chop off. | |
2715 | * | |
2716 | * If they specified a snapshot, chop the entire name stored in | |
2717 | * the stream. | |
2718 | */ | |
330d06f9 | 2719 | if (flags->istail) { |
428870ff BB |
2720 | /* |
2721 | * A filesystem was specified with -e. We want to tack on only | |
2722 | * the tail of the sent snapshot path. | |
2723 | */ | |
2724 | if (strchr(tosnap, '@')) { | |
2725 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
2726 | "argument - snapshot not allowed with -e")); | |
2727 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
2728 | } | |
2729 | ||
2730 | chopprefix = strrchr(sendfs, '/'); | |
2731 | ||
2732 | if (chopprefix == NULL) { | |
2733 | /* | |
2734 | * The tail is the poolname, so we need to | |
2735 | * prepend a path separator. | |
2736 | */ | |
2737 | int len = strlen(drrb->drr_toname); | |
2738 | cp = malloc(len + 2); | |
2739 | cp[0] = '/'; | |
2740 | (void) strcpy(&cp[1], drrb->drr_toname); | |
2741 | chopprefix = cp; | |
2742 | } else { | |
2743 | chopprefix = drrb->drr_toname + (chopprefix - sendfs); | |
2744 | } | |
330d06f9 | 2745 | } else if (flags->isprefix) { |
34dc7c2f | 2746 | /* |
428870ff BB |
2747 | * A filesystem was specified with -d. We want to tack on |
2748 | * everything but the first element of the sent snapshot path | |
2749 | * (all but the pool name). | |
34dc7c2f BB |
2750 | */ |
2751 | if (strchr(tosnap, '@')) { | |
2752 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
2753 | "argument - snapshot not allowed with -d")); | |
2754 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
2755 | } | |
428870ff BB |
2756 | |
2757 | chopprefix = strchr(drrb->drr_toname, '/'); | |
2758 | if (chopprefix == NULL) | |
2759 | chopprefix = strchr(drrb->drr_toname, '@'); | |
34dc7c2f BB |
2760 | } else if (strchr(tosnap, '@') == NULL) { |
2761 | /* | |
428870ff BB |
2762 | * If a filesystem was specified without -d or -e, we want to |
2763 | * tack on everything after the fs specified by 'zfs send'. | |
34dc7c2f | 2764 | */ |
428870ff BB |
2765 | chopprefix = drrb->drr_toname + strlen(sendfs); |
2766 | } else { | |
2767 | /* A snapshot was specified as an exact path (no -d or -e). */ | |
2768 | if (recursive) { | |
2769 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2770 | "cannot specify snapshot name for multi-snapshot " | |
2771 | "stream")); | |
2772 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
2773 | } | |
2774 | chopprefix = drrb->drr_toname + strlen(drrb->drr_toname); | |
34dc7c2f | 2775 | } |
428870ff BB |
2776 | |
2777 | ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname); | |
2778 | ASSERT(chopprefix > drrb->drr_toname); | |
2779 | ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname)); | |
2780 | ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' || | |
2781 | chopprefix[0] == '\0'); | |
34dc7c2f BB |
2782 | |
2783 | /* | |
2784 | * Determine name of destination snapshot, store in zc_value. | |
2785 | */ | |
2786 | (void) strcpy(zc.zc_value, tosnap); | |
2a442d16 | 2787 | (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value)); |
428870ff | 2788 | free(cp); |
34dc7c2f BB |
2789 | if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) { |
2790 | zcmd_free_nvlists(&zc); | |
2791 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
2792 | } | |
2793 | ||
2794 | /* | |
2795 | * Determine the name of the origin snapshot, store in zc_string. | |
2796 | */ | |
2797 | if (drrb->drr_flags & DRR_FLAG_CLONE) { | |
330d06f9 | 2798 | if (guid_to_name(hdl, zc.zc_value, |
34dc7c2f BB |
2799 | drrb->drr_fromguid, zc.zc_string) != 0) { |
2800 | zcmd_free_nvlists(&zc); | |
2801 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2802 | "local origin for clone %s does not exist"), | |
2803 | zc.zc_value); | |
2804 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
2805 | } | |
330d06f9 | 2806 | if (flags->verbose) |
34dc7c2f BB |
2807 | (void) printf("found clone origin %s\n", zc.zc_string); |
2808 | } | |
2809 | ||
b8864a23 | 2810 | stream_wantsnewfs = (drrb->drr_fromguid == 0 || |
34dc7c2f BB |
2811 | (drrb->drr_flags & DRR_FLAG_CLONE)); |
2812 | ||
2813 | if (stream_wantsnewfs) { | |
2814 | /* | |
2815 | * if the parent fs does not exist, look for it based on | |
2816 | * the parent snap GUID | |
2817 | */ | |
2818 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2819 | "cannot receive new filesystem stream")); | |
2820 | ||
2821 | (void) strcpy(zc.zc_name, zc.zc_value); | |
2822 | cp = strrchr(zc.zc_name, '/'); | |
2823 | if (cp) | |
2824 | *cp = '\0'; | |
2825 | if (cp && | |
2826 | !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { | |
2827 | char suffix[ZFS_MAXNAMELEN]; | |
2828 | (void) strcpy(suffix, strrchr(zc.zc_value, '/')); | |
330d06f9 | 2829 | if (guid_to_name(hdl, zc.zc_name, parent_snapguid, |
34dc7c2f BB |
2830 | zc.zc_value) == 0) { |
2831 | *strchr(zc.zc_value, '@') = '\0'; | |
2832 | (void) strcat(zc.zc_value, suffix); | |
2833 | } | |
2834 | } | |
2835 | } else { | |
2836 | /* | |
2837 | * if the fs does not exist, look for it based on the | |
2838 | * fromsnap GUID | |
2839 | */ | |
2840 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2841 | "cannot receive incremental stream")); | |
2842 | ||
2843 | (void) strcpy(zc.zc_name, zc.zc_value); | |
2844 | *strchr(zc.zc_name, '@') = '\0'; | |
2845 | ||
428870ff BB |
2846 | /* |
2847 | * If the exact receive path was specified and this is the | |
2848 | * topmost path in the stream, then if the fs does not exist we | |
2849 | * should look no further. | |
2850 | */ | |
330d06f9 | 2851 | if ((flags->isprefix || (*(chopprefix = drrb->drr_toname + |
428870ff BB |
2852 | strlen(sendfs)) != '\0' && *chopprefix != '@')) && |
2853 | !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { | |
34dc7c2f BB |
2854 | char snap[ZFS_MAXNAMELEN]; |
2855 | (void) strcpy(snap, strchr(zc.zc_value, '@')); | |
330d06f9 | 2856 | if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid, |
34dc7c2f BB |
2857 | zc.zc_value) == 0) { |
2858 | *strchr(zc.zc_value, '@') = '\0'; | |
2859 | (void) strcat(zc.zc_value, snap); | |
2860 | } | |
2861 | } | |
2862 | } | |
2863 | ||
2864 | (void) strcpy(zc.zc_name, zc.zc_value); | |
2865 | *strchr(zc.zc_name, '@') = '\0'; | |
2866 | ||
2867 | if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { | |
2868 | zfs_handle_t *zhp; | |
428870ff | 2869 | |
34dc7c2f BB |
2870 | /* |
2871 | * Destination fs exists. Therefore this should either | |
2872 | * be an incremental, or the stream specifies a new fs | |
2873 | * (full stream or clone) and they want us to blow it | |
2874 | * away (and have therefore specified -F and removed any | |
2875 | * snapshots). | |
2876 | */ | |
34dc7c2f | 2877 | if (stream_wantsnewfs) { |
330d06f9 | 2878 | if (!flags->force) { |
34dc7c2f BB |
2879 | zcmd_free_nvlists(&zc); |
2880 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2881 | "destination '%s' exists\n" | |
2882 | "must specify -F to overwrite it"), | |
2883 | zc.zc_name); | |
2884 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
2885 | } | |
2886 | if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, | |
2887 | &zc) == 0) { | |
2888 | zcmd_free_nvlists(&zc); | |
2889 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2890 | "destination has snapshots (eg. %s)\n" | |
2891 | "must destroy them to overwrite it"), | |
2892 | zc.zc_name); | |
2893 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
2894 | } | |
2895 | } | |
2896 | ||
2897 | if ((zhp = zfs_open(hdl, zc.zc_name, | |
2898 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { | |
2899 | zcmd_free_nvlists(&zc); | |
2900 | return (-1); | |
2901 | } | |
2902 | ||
2903 | if (stream_wantsnewfs && | |
2904 | zhp->zfs_dmustats.dds_origin[0]) { | |
2905 | zcmd_free_nvlists(&zc); | |
2906 | zfs_close(zhp); | |
2907 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2908 | "destination '%s' is a clone\n" | |
2909 | "must destroy it to overwrite it"), | |
2910 | zc.zc_name); | |
2911 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
2912 | } | |
2913 | ||
330d06f9 | 2914 | if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && |
34dc7c2f BB |
2915 | stream_wantsnewfs) { |
2916 | /* We can't do online recv in this case */ | |
b128c09f | 2917 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0); |
34dc7c2f | 2918 | if (clp == NULL) { |
45d1cae3 | 2919 | zfs_close(zhp); |
34dc7c2f BB |
2920 | zcmd_free_nvlists(&zc); |
2921 | return (-1); | |
2922 | } | |
2923 | if (changelist_prefix(clp) != 0) { | |
2924 | changelist_free(clp); | |
45d1cae3 | 2925 | zfs_close(zhp); |
34dc7c2f BB |
2926 | zcmd_free_nvlists(&zc); |
2927 | return (-1); | |
2928 | } | |
2929 | } | |
34dc7c2f BB |
2930 | zfs_close(zhp); |
2931 | } else { | |
2932 | /* | |
2933 | * Destination filesystem does not exist. Therefore we better | |
2934 | * be creating a new filesystem (either from a full backup, or | |
2935 | * a clone). It would therefore be invalid if the user | |
2936 | * specified only the pool name (i.e. if the destination name | |
2937 | * contained no slash character). | |
2938 | */ | |
2939 | if (!stream_wantsnewfs || | |
2940 | (cp = strrchr(zc.zc_name, '/')) == NULL) { | |
2941 | zcmd_free_nvlists(&zc); | |
2942 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2943 | "destination '%s' does not exist"), zc.zc_name); | |
2944 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
2945 | } | |
2946 | ||
2947 | /* | |
2948 | * Trim off the final dataset component so we perform the | |
2949 | * recvbackup ioctl to the filesystems's parent. | |
2950 | */ | |
2951 | *cp = '\0'; | |
2952 | ||
330d06f9 | 2953 | if (flags->isprefix && !flags->istail && !flags->dryrun && |
34dc7c2f BB |
2954 | create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) { |
2955 | zcmd_free_nvlists(&zc); | |
2956 | return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); | |
2957 | } | |
2958 | ||
2959 | newfs = B_TRUE; | |
2960 | } | |
2961 | ||
2962 | zc.zc_begin_record = drr_noswap->drr_u.drr_begin; | |
2963 | zc.zc_cookie = infd; | |
330d06f9 MA |
2964 | zc.zc_guid = flags->force; |
2965 | if (flags->verbose) { | |
34dc7c2f | 2966 | (void) printf("%s %s stream of %s into %s\n", |
330d06f9 | 2967 | flags->dryrun ? "would receive" : "receiving", |
34dc7c2f BB |
2968 | drrb->drr_fromguid ? "incremental" : "full", |
2969 | drrb->drr_toname, zc.zc_value); | |
2970 | (void) fflush(stdout); | |
2971 | } | |
2972 | ||
330d06f9 | 2973 | if (flags->dryrun) { |
34dc7c2f | 2974 | zcmd_free_nvlists(&zc); |
330d06f9 | 2975 | return (recv_skip(hdl, infd, flags->byteswap)); |
34dc7c2f BB |
2976 | } |
2977 | ||
428870ff BB |
2978 | zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf; |
2979 | zc.zc_nvlist_dst_size = sizeof (prop_errbuf); | |
572e2857 BB |
2980 | zc.zc_cleanup_fd = cleanup_fd; |
2981 | zc.zc_action_handle = *action_handlep; | |
428870ff | 2982 | |
34dc7c2f BB |
2983 | err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc); |
2984 | ioctl_errno = errno; | |
428870ff BB |
2985 | prop_errflags = (zprop_errflags_t)zc.zc_obj; |
2986 | ||
2987 | if (err == 0) { | |
2988 | nvlist_t *prop_errors; | |
2989 | VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst, | |
2990 | zc.zc_nvlist_dst_size, &prop_errors, 0)); | |
2991 | ||
2992 | nvpair_t *prop_err = NULL; | |
2993 | ||
2994 | while ((prop_err = nvlist_next_nvpair(prop_errors, | |
2995 | prop_err)) != NULL) { | |
2996 | char tbuf[1024]; | |
2997 | zfs_prop_t prop; | |
2998 | int intval; | |
2999 | ||
3000 | prop = zfs_name_to_prop(nvpair_name(prop_err)); | |
3001 | (void) nvpair_value_int32(prop_err, &intval); | |
3002 | if (strcmp(nvpair_name(prop_err), | |
3003 | ZPROP_N_MORE_ERRORS) == 0) { | |
3004 | trunc_prop_errs(intval); | |
3005 | break; | |
3006 | } else { | |
3007 | (void) snprintf(tbuf, sizeof (tbuf), | |
3008 | dgettext(TEXT_DOMAIN, | |
3009 | "cannot receive %s property on %s"), | |
3010 | nvpair_name(prop_err), zc.zc_name); | |
3011 | zfs_setprop_error(hdl, prop, intval, tbuf); | |
3012 | } | |
3013 | } | |
3014 | nvlist_free(prop_errors); | |
3015 | } | |
3016 | ||
3017 | zc.zc_nvlist_dst = 0; | |
3018 | zc.zc_nvlist_dst_size = 0; | |
b128c09f BB |
3019 | zcmd_free_nvlists(&zc); |
3020 | ||
3021 | if (err == 0 && snapprops_nvlist) { | |
13fe0198 | 3022 | zfs_cmd_t zc2 = {"\0"}; |
b128c09f BB |
3023 | |
3024 | (void) strcpy(zc2.zc_name, zc.zc_value); | |
428870ff | 3025 | zc2.zc_cookie = B_TRUE; /* received */ |
b128c09f BB |
3026 | if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) { |
3027 | (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2); | |
3028 | zcmd_free_nvlists(&zc2); | |
3029 | } | |
3030 | } | |
3031 | ||
428870ff | 3032 | if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) { |
34dc7c2f BB |
3033 | /* |
3034 | * It may be that this snapshot already exists, | |
3035 | * in which case we want to consume & ignore it | |
3036 | * rather than failing. | |
3037 | */ | |
3038 | avl_tree_t *local_avl; | |
3039 | nvlist_t *local_nv, *fs; | |
428870ff | 3040 | cp = strchr(zc.zc_value, '@'); |
34dc7c2f BB |
3041 | |
3042 | /* | |
3043 | * XXX Do this faster by just iterating over snaps in | |
3044 | * this fs. Also if zc_value does not exist, we will | |
3045 | * get a strange "does not exist" error message. | |
3046 | */ | |
3047 | *cp = '\0'; | |
428870ff | 3048 | if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE, |
34dc7c2f BB |
3049 | &local_nv, &local_avl) == 0) { |
3050 | *cp = '@'; | |
3051 | fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); | |
3052 | fsavl_destroy(local_avl); | |
3053 | nvlist_free(local_nv); | |
3054 | ||
3055 | if (fs != NULL) { | |
330d06f9 | 3056 | if (flags->verbose) { |
34dc7c2f BB |
3057 | (void) printf("snap %s already exists; " |
3058 | "ignoring\n", zc.zc_value); | |
3059 | } | |
428870ff | 3060 | err = ioctl_err = recv_skip(hdl, infd, |
330d06f9 | 3061 | flags->byteswap); |
34dc7c2f BB |
3062 | } |
3063 | } | |
3064 | *cp = '@'; | |
3065 | } | |
3066 | ||
34dc7c2f BB |
3067 | if (ioctl_err != 0) { |
3068 | switch (ioctl_errno) { | |
3069 | case ENODEV: | |
3070 | cp = strchr(zc.zc_value, '@'); | |
3071 | *cp = '\0'; | |
3072 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3073 | "most recent snapshot of %s does not\n" | |
3074 | "match incremental source"), zc.zc_value); | |
3075 | (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3076 | *cp = '@'; | |
3077 | break; | |
3078 | case ETXTBSY: | |
3079 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3080 | "destination %s has been modified\n" | |
3081 | "since most recent snapshot"), zc.zc_name); | |
3082 | (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3083 | break; | |
3084 | case EEXIST: | |
3085 | cp = strchr(zc.zc_value, '@'); | |
3086 | if (newfs) { | |
3087 | /* it's the containing fs that exists */ | |
3088 | *cp = '\0'; | |
3089 | } | |
3090 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3091 | "destination already exists")); | |
3092 | (void) zfs_error_fmt(hdl, EZFS_EXISTS, | |
3093 | dgettext(TEXT_DOMAIN, "cannot restore to %s"), | |
3094 | zc.zc_value); | |
3095 | *cp = '@'; | |
3096 | break; | |
3097 | case EINVAL: | |
3098 | (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3099 | break; | |
3100 | case ECKSUM: | |
3101 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3102 | "invalid stream (checksum mismatch)")); | |
3103 | (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3104 | break; | |
428870ff BB |
3105 | case ENOTSUP: |
3106 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3107 | "pool must be upgraded to receive this stream.")); | |
3108 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
3109 | break; | |
3110 | case EDQUOT: | |
3111 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3112 | "destination %s space quota exceeded"), zc.zc_name); | |
330d06f9 | 3113 | (void) zfs_error(hdl, EZFS_NOSPC, errbuf); |
428870ff | 3114 | break; |
34dc7c2f BB |
3115 | default: |
3116 | (void) zfs_standard_error(hdl, ioctl_errno, errbuf); | |
3117 | } | |
3118 | } | |
3119 | ||
3120 | /* | |
428870ff BB |
3121 | * Mount the target filesystem (if created). Also mount any |
3122 | * children of the target filesystem if we did a replication | |
3123 | * receive (indicated by stream_avl being non-NULL). | |
34dc7c2f BB |
3124 | */ |
3125 | cp = strchr(zc.zc_value, '@'); | |
3126 | if (cp && (ioctl_err == 0 || !newfs)) { | |
3127 | zfs_handle_t *h; | |
3128 | ||
3129 | *cp = '\0'; | |
3130 | h = zfs_open(hdl, zc.zc_value, | |
3131 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); | |
34dc7c2f BB |
3132 | if (h != NULL) { |
3133 | if (h->zfs_type == ZFS_TYPE_VOLUME) { | |
b128c09f | 3134 | *cp = '@'; |
45d1cae3 | 3135 | } else if (newfs || stream_avl) { |
b128c09f BB |
3136 | /* |
3137 | * Track the first/top of hierarchy fs, | |
3138 | * for mounting and sharing later. | |
3139 | */ | |
3140 | if (top_zfs && *top_zfs == NULL) | |
3141 | *top_zfs = zfs_strdup(hdl, zc.zc_value); | |
34dc7c2f BB |
3142 | } |
3143 | zfs_close(h); | |
3144 | } | |
b128c09f | 3145 | *cp = '@'; |
34dc7c2f BB |
3146 | } |
3147 | ||
3148 | if (clp) { | |
3149 | err |= changelist_postfix(clp); | |
3150 | changelist_free(clp); | |
3151 | } | |
3152 | ||
428870ff BB |
3153 | if (prop_errflags & ZPROP_ERR_NOCLEAR) { |
3154 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " | |
3155 | "failed to clear unreceived properties on %s"), | |
3156 | zc.zc_name); | |
3157 | (void) fprintf(stderr, "\n"); | |
3158 | } | |
3159 | if (prop_errflags & ZPROP_ERR_NORESTORE) { | |
3160 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " | |
3161 | "failed to restore original properties on %s"), | |
3162 | zc.zc_name); | |
3163 | (void) fprintf(stderr, "\n"); | |
3164 | } | |
3165 | ||
34dc7c2f BB |
3166 | if (err || ioctl_err) |
3167 | return (-1); | |
3168 | ||
572e2857 BB |
3169 | *action_handlep = zc.zc_action_handle; |
3170 | ||
330d06f9 | 3171 | if (flags->verbose) { |
34dc7c2f BB |
3172 | char buf1[64]; |
3173 | char buf2[64]; | |
3174 | uint64_t bytes = zc.zc_cookie; | |
3175 | time_t delta = time(NULL) - begin_time; | |
3176 | if (delta == 0) | |
3177 | delta = 1; | |
3178 | zfs_nicenum(bytes, buf1, sizeof (buf1)); | |
3179 | zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); | |
3180 | ||
3181 | (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", | |
3182 | buf1, delta, buf2); | |
3183 | } | |
3184 | ||
3185 | return (0); | |
3186 | } | |
3187 | ||
b128c09f | 3188 | static int |
330d06f9 | 3189 | zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags, |
428870ff | 3190 | int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl, |
572e2857 | 3191 | char **top_zfs, int cleanup_fd, uint64_t *action_handlep) |
34dc7c2f BB |
3192 | { |
3193 | int err; | |
3194 | dmu_replay_record_t drr, drr_noswap; | |
3195 | struct drr_begin *drrb = &drr.drr_u.drr_begin; | |
3196 | char errbuf[1024]; | |
2598c001 | 3197 | zio_cksum_t zcksum = { { 0 } }; |
428870ff BB |
3198 | uint64_t featureflags; |
3199 | int hdrtype; | |
34dc7c2f BB |
3200 | |
3201 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3202 | "cannot receive")); | |
3203 | ||
330d06f9 | 3204 | if (flags->isprefix && |
34dc7c2f BB |
3205 | !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { |
3206 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " | |
3207 | "(%s) does not exist"), tosnap); | |
3208 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
3209 | } | |
3210 | ||
3211 | /* read in the BEGIN record */ | |
3212 | if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, | |
3213 | &zcksum))) | |
3214 | return (err); | |
3215 | ||
3216 | if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { | |
3217 | /* It's the double end record at the end of a package */ | |
3218 | return (ENODATA); | |
3219 | } | |
3220 | ||
3221 | /* the kernel needs the non-byteswapped begin record */ | |
3222 | drr_noswap = drr; | |
3223 | ||
330d06f9 | 3224 | flags->byteswap = B_FALSE; |
34dc7c2f BB |
3225 | if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { |
3226 | /* | |
3227 | * We computed the checksum in the wrong byteorder in | |
3228 | * recv_read() above; do it again correctly. | |
3229 | */ | |
3230 | bzero(&zcksum, sizeof (zio_cksum_t)); | |
3231 | fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum); | |
330d06f9 | 3232 | flags->byteswap = B_TRUE; |
34dc7c2f BB |
3233 | |
3234 | drr.drr_type = BSWAP_32(drr.drr_type); | |
3235 | drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); | |
3236 | drrb->drr_magic = BSWAP_64(drrb->drr_magic); | |
428870ff | 3237 | drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); |
34dc7c2f BB |
3238 | drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); |
3239 | drrb->drr_type = BSWAP_32(drrb->drr_type); | |
3240 | drrb->drr_flags = BSWAP_32(drrb->drr_flags); | |
3241 | drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); | |
3242 | drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); | |
3243 | } | |
3244 | ||
3245 | if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { | |
3246 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
3247 | "stream (bad magic number)")); | |
3248 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
3249 | } | |
3250 | ||
428870ff BB |
3251 | featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); |
3252 | hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo); | |
3253 | ||
3254 | if (!DMU_STREAM_SUPPORTED(featureflags) || | |
3255 | (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) { | |
3256 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3257 | "stream has unsupported feature, feature flags = %lx"), | |
3258 | featureflags); | |
3259 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
3260 | } | |
3261 | ||
34dc7c2f BB |
3262 | if (strchr(drrb->drr_toname, '@') == NULL) { |
3263 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
3264 | "stream (bad snapshot name)")); | |
3265 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
3266 | } | |
3267 | ||
428870ff BB |
3268 | if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) { |
3269 | char nonpackage_sendfs[ZFS_MAXNAMELEN]; | |
3270 | if (sendfs == NULL) { | |
3271 | /* | |
3272 | * We were not called from zfs_receive_package(). Get | |
3273 | * the fs specified by 'zfs send'. | |
3274 | */ | |
3275 | char *cp; | |
3276 | (void) strlcpy(nonpackage_sendfs, | |
3277 | drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN); | |
3278 | if ((cp = strchr(nonpackage_sendfs, '@')) != NULL) | |
3279 | *cp = '\0'; | |
3280 | sendfs = nonpackage_sendfs; | |
3281 | } | |
34dc7c2f | 3282 | return (zfs_receive_one(hdl, infd, tosnap, flags, |
428870ff | 3283 | &drr, &drr_noswap, sendfs, stream_nv, stream_avl, |
572e2857 | 3284 | top_zfs, cleanup_fd, action_handlep)); |
428870ff BB |
3285 | } else { |
3286 | assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == | |
3287 | DMU_COMPOUNDSTREAM); | |
34dc7c2f | 3288 | return (zfs_receive_package(hdl, infd, tosnap, flags, |
572e2857 | 3289 | &drr, &zcksum, top_zfs, cleanup_fd, action_handlep)); |
34dc7c2f BB |
3290 | } |
3291 | } | |
b128c09f BB |
3292 | |
3293 | /* | |
3294 | * Restores a backup of tosnap from the file descriptor specified by infd. | |
3295 | * Return 0 on total success, -2 if some things couldn't be | |
3296 | * destroyed/renamed/promoted, -1 if some things couldn't be received. | |
3297 | * (-1 will override -2). | |
3298 | */ | |
3299 | int | |
330d06f9 | 3300 | zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags, |
b128c09f BB |
3301 | int infd, avl_tree_t *stream_avl) |
3302 | { | |
3303 | char *top_zfs = NULL; | |
3304 | int err; | |
572e2857 BB |
3305 | int cleanup_fd; |
3306 | uint64_t action_handle = 0; | |
5c3f61eb RY |
3307 | struct stat sb; |
3308 | ||
3309 | /* | |
3310 | * The only way fstat can fail is if we do not have a valid file | |
3311 | * descriptor. | |
3312 | */ | |
3313 | if (fstat(infd, &sb) == -1) { | |
3314 | perror("fstat"); | |
3315 | return (-2); | |
3316 | } | |
3317 | ||
3318 | #ifdef __linux__ | |
3319 | #ifndef F_SETPIPE_SZ | |
3320 | #define F_SETPIPE_SZ (F_SETLEASE + 7) | |
3321 | #endif /* F_SETPIPE_SZ */ | |
3322 | ||
3323 | #ifndef F_GETPIPE_SZ | |
3324 | #define F_GETPIPE_SZ (F_GETLEASE + 7) | |
3325 | #endif /* F_GETPIPE_SZ */ | |
3326 | ||
3327 | /* | |
3328 | * It is not uncommon for gigabytes to be processed in zfs receive. | |
3329 | * Speculatively increase the buffer size via Linux-specific fcntl() | |
3330 | * call. | |
3331 | */ | |
3332 | if (S_ISFIFO(sb.st_mode)) { | |
3333 | FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r"); | |
3334 | ||
3335 | if (procf != NULL) { | |
3336 | unsigned long max_psize; | |
3337 | long cur_psize; | |
3338 | if (fscanf(procf, "%lu", &max_psize) > 0) { | |
3339 | cur_psize = fcntl(infd, F_GETPIPE_SZ); | |
3340 | if (cur_psize > 0 && | |
3341 | max_psize > (unsigned long) cur_psize) | |
3342 | (void) fcntl(infd, F_SETPIPE_SZ, | |
3343 | max_psize); | |
3344 | } | |
3345 | fclose(procf); | |
3346 | } | |
3347 | } | |
3348 | #endif /* __linux__ */ | |
572e2857 | 3349 | |
325f0235 | 3350 | cleanup_fd = open(ZFS_DEV, O_RDWR); |
572e2857 | 3351 | VERIFY(cleanup_fd >= 0); |
b128c09f | 3352 | |
428870ff | 3353 | err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL, |
572e2857 BB |
3354 | stream_avl, &top_zfs, cleanup_fd, &action_handle); |
3355 | ||
3356 | VERIFY(0 == close(cleanup_fd)); | |
b128c09f | 3357 | |
330d06f9 | 3358 | if (err == 0 && !flags->nomount && top_zfs) { |
b128c09f BB |
3359 | zfs_handle_t *zhp; |
3360 | prop_changelist_t *clp; | |
3361 | ||
3362 | zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM); | |
3363 | if (zhp != NULL) { | |
3364 | clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, | |
3365 | CL_GATHER_MOUNT_ALWAYS, 0); | |
3366 | zfs_close(zhp); | |
3367 | if (clp != NULL) { | |
3368 | /* mount and share received datasets */ | |
3369 | err = changelist_postfix(clp); | |
3370 | changelist_free(clp); | |
3371 | } | |
3372 | } | |
3373 | if (zhp == NULL || clp == NULL || err) | |
3374 | err = -1; | |
3375 | } | |
3376 | if (top_zfs) | |
3377 | free(top_zfs); | |
3378 | ||
3379 | return (err); | |
3380 | } |