]>
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. |
ea04106b | 24 | * Copyright (c) 2011, 2014 by Delphix. All rights reserved. |
37abac6d | 25 | * Copyright (c) 2012, Joyent, Inc. All rights reserved. |
a08ee875 | 26 | * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. |
0cee2406 | 27 | * All rights reserved |
a08ee875 | 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> | |
ea04106b | 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> | |
ea04106b | 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; | |
217 | char *buf = malloc(1<<20); | |
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; | |
ea04106b | 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 | ||
282 | if (sz > 1<<20) { | |
283 | free(buf); | |
284 | buf = malloc(sz); | |
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 | ||
ea04106b AX |
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; |
ea04106b AX |
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; |
ea04106b | 617 | boolean_t isfromsnap, istosnap, istosnapwithnofrom; |
34dc7c2f BB |
618 | |
619 | snapname = strrchr(zhp->zfs_name, '@')+1; | |
ea04106b AX |
620 | isfromsnap = (sd->fromsnap != NULL && |
621 | strcmp(sd->fromsnap, snapname) == 0); | |
622 | istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0)); | |
623 | istosnapwithnofrom = (istosnap && sd->fromsnap == NULL); | |
34dc7c2f BB |
624 | |
625 | VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid)); | |
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 | */ | |
ea04106b | 630 | if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) { |
34dc7c2f BB |
631 | sd->parent_fromsnap_guid = guid; |
632 | } | |
633 | ||
ea04106b AX |
634 | if (!sd->recursive) { |
635 | if (!sd->seenfrom && isfromsnap) { | |
636 | sd->seenfrom = B_TRUE; | |
637 | zfs_close(zhp); | |
638 | return (0); | |
639 | } | |
640 | ||
641 | if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) { | |
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)); |
ea04106b | 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; |
ea04106b | 837 | boolean_t verbose, dryrun, parsable, progress, embed_data; |
34dc7c2f BB |
838 | int outfd; |
839 | boolean_t err; | |
840 | nvlist_t *fss; | |
a08ee875 | 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 | { | |
a08ee875 | 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, |
ea04106b AX |
917 | boolean_t fromorigin, int outfd, enum lzc_send_flags flags, |
918 | nvlist_t *debugnv) | |
34dc7c2f | 919 | { |
a08ee875 | 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; | |
ea04106b | 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 | ||
a08ee875 LG |
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 | /* | |
a08ee875 | 999 | * zfs_send() only sets snapholds for sends that need them, |
572e2857 BB |
1000 | * e.g. replication and doall. |
1001 | */ | |
a08ee875 LG |
1002 | if (sdd->snapholds == NULL) |
1003 | return; | |
572e2857 | 1004 | |
a08ee875 | 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 | ||
a08ee875 | 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 | |
a08ee875 | 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) { |
a08ee875 LG |
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); |
a08ee875 | 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 | ||
a08ee875 | 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 | ||
ea04106b AX |
1183 | enum lzc_send_flags flags = 0; |
1184 | if (sdd->embed_data) | |
1185 | flags |= LZC_SEND_FLAG_EMBED_DATA; | |
1186 | ||
330d06f9 | 1187 | err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj, |
ea04106b | 1188 | fromorigin, sdd->outfd, flags, sdd->debugnv); |
37abac6d BP |
1189 | |
1190 | if (sdd->progress) { | |
1191 | (void) pthread_cancel(tid); | |
1192 | (void) pthread_join(tid, NULL); | |
1193 | } | |
330d06f9 | 1194 | } |
34dc7c2f | 1195 | |
572e2857 BB |
1196 | (void) strcpy(sdd->prevsnap, thissnap); |
1197 | sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID); | |
34dc7c2f BB |
1198 | zfs_close(zhp); |
1199 | return (err); | |
1200 | } | |
1201 | ||
1202 | static int | |
1203 | dump_filesystem(zfs_handle_t *zhp, void *arg) | |
1204 | { | |
1205 | int rv = 0; | |
1206 | send_dump_data_t *sdd = arg; | |
1207 | boolean_t missingfrom = B_FALSE; | |
a08ee875 | 1208 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1209 | |
1210 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", | |
1211 | zhp->zfs_name, sdd->tosnap); | |
1212 | if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { | |
330d06f9 MA |
1213 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
1214 | "WARNING: could not send %s@%s: does not exist\n"), | |
34dc7c2f BB |
1215 | zhp->zfs_name, sdd->tosnap); |
1216 | sdd->err = B_TRUE; | |
1217 | return (0); | |
1218 | } | |
1219 | ||
1220 | if (sdd->replicate && sdd->fromsnap) { | |
1221 | /* | |
1222 | * If this fs does not have fromsnap, and we're doing | |
1223 | * recursive, we need to send a full stream from the | |
1224 | * beginning (or an incremental from the origin if this | |
1225 | * is a clone). If we're doing non-recursive, then let | |
1226 | * them get the error. | |
1227 | */ | |
1228 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s", | |
1229 | zhp->zfs_name, sdd->fromsnap); | |
1230 | if (ioctl(zhp->zfs_hdl->libzfs_fd, | |
1231 | ZFS_IOC_OBJSET_STATS, &zc) != 0) { | |
1232 | missingfrom = B_TRUE; | |
1233 | } | |
1234 | } | |
1235 | ||
428870ff | 1236 | sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0; |
572e2857 | 1237 | sdd->prevsnap_obj = 0; |
428870ff BB |
1238 | if (sdd->fromsnap == NULL || missingfrom) |
1239 | sdd->seenfrom = B_TRUE; | |
34dc7c2f | 1240 | |
428870ff BB |
1241 | rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg); |
1242 | if (!sdd->seenfrom) { | |
330d06f9 | 1243 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
428870ff | 1244 | "WARNING: could not send %s@%s:\n" |
330d06f9 | 1245 | "incremental source (%s@%s) does not exist\n"), |
428870ff BB |
1246 | zhp->zfs_name, sdd->tosnap, |
1247 | zhp->zfs_name, sdd->fromsnap); | |
1248 | sdd->err = B_TRUE; | |
1249 | } else if (!sdd->seento) { | |
1250 | if (sdd->fromsnap) { | |
330d06f9 | 1251 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
34dc7c2f | 1252 | "WARNING: could not send %s@%s:\n" |
428870ff | 1253 | "incremental source (%s@%s) " |
330d06f9 | 1254 | "is not earlier than it\n"), |
34dc7c2f BB |
1255 | zhp->zfs_name, sdd->tosnap, |
1256 | zhp->zfs_name, sdd->fromsnap); | |
34dc7c2f | 1257 | } else { |
330d06f9 MA |
1258 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, |
1259 | "WARNING: " | |
1260 | "could not send %s@%s: does not exist\n"), | |
428870ff | 1261 | zhp->zfs_name, sdd->tosnap); |
34dc7c2f | 1262 | } |
428870ff | 1263 | sdd->err = B_TRUE; |
34dc7c2f BB |
1264 | } |
1265 | ||
1266 | return (rv); | |
1267 | } | |
1268 | ||
1269 | static int | |
1270 | dump_filesystems(zfs_handle_t *rzhp, void *arg) | |
1271 | { | |
1272 | send_dump_data_t *sdd = arg; | |
1273 | nvpair_t *fspair; | |
1274 | boolean_t needagain, progress; | |
1275 | ||
1276 | if (!sdd->replicate) | |
1277 | return (dump_filesystem(rzhp, sdd)); | |
1278 | ||
428870ff BB |
1279 | /* Mark the clone origin snapshots. */ |
1280 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1281 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
1282 | nvlist_t *nvfs; | |
1283 | uint64_t origin_guid = 0; | |
1284 | ||
1285 | VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs)); | |
1286 | (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid); | |
1287 | if (origin_guid != 0) { | |
1288 | char *snapname; | |
1289 | nvlist_t *origin_nv = fsavl_find(sdd->fsavl, | |
1290 | origin_guid, &snapname); | |
1291 | if (origin_nv != NULL) { | |
1292 | nvlist_t *snapprops; | |
1293 | VERIFY(0 == nvlist_lookup_nvlist(origin_nv, | |
1294 | "snapprops", &snapprops)); | |
1295 | VERIFY(0 == nvlist_lookup_nvlist(snapprops, | |
1296 | snapname, &snapprops)); | |
1297 | VERIFY(0 == nvlist_add_boolean( | |
1298 | snapprops, "is_clone_origin")); | |
1299 | } | |
1300 | } | |
1301 | } | |
34dc7c2f BB |
1302 | again: |
1303 | needagain = progress = B_FALSE; | |
1304 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1305 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
330d06f9 | 1306 | nvlist_t *fslist, *parent_nv; |
34dc7c2f BB |
1307 | char *fsname; |
1308 | zfs_handle_t *zhp; | |
1309 | int err; | |
1310 | uint64_t origin_guid = 0; | |
330d06f9 | 1311 | uint64_t parent_guid = 0; |
34dc7c2f BB |
1312 | |
1313 | VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); | |
1314 | if (nvlist_lookup_boolean(fslist, "sent") == 0) | |
1315 | continue; | |
1316 | ||
1317 | VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0); | |
1318 | (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid); | |
330d06f9 MA |
1319 | (void) nvlist_lookup_uint64(fslist, "parentfromsnap", |
1320 | &parent_guid); | |
1321 | ||
1322 | if (parent_guid != 0) { | |
1323 | parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL); | |
1324 | if (!nvlist_exists(parent_nv, "sent")) { | |
1325 | /* parent has not been sent; skip this one */ | |
1326 | needagain = B_TRUE; | |
1327 | continue; | |
1328 | } | |
1329 | } | |
34dc7c2f | 1330 | |
428870ff BB |
1331 | if (origin_guid != 0) { |
1332 | nvlist_t *origin_nv = fsavl_find(sdd->fsavl, | |
1333 | origin_guid, NULL); | |
1334 | if (origin_nv != NULL && | |
330d06f9 | 1335 | !nvlist_exists(origin_nv, "sent")) { |
428870ff BB |
1336 | /* |
1337 | * origin has not been sent yet; | |
1338 | * skip this clone. | |
1339 | */ | |
1340 | needagain = B_TRUE; | |
1341 | continue; | |
1342 | } | |
34dc7c2f BB |
1343 | } |
1344 | ||
1345 | zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET); | |
1346 | if (zhp == NULL) | |
1347 | return (-1); | |
1348 | err = dump_filesystem(zhp, sdd); | |
1349 | VERIFY(nvlist_add_boolean(fslist, "sent") == 0); | |
1350 | progress = B_TRUE; | |
1351 | zfs_close(zhp); | |
1352 | if (err) | |
1353 | return (err); | |
1354 | } | |
1355 | if (needagain) { | |
1356 | assert(progress); | |
1357 | goto again; | |
1358 | } | |
330d06f9 MA |
1359 | |
1360 | /* clean out the sent flags in case we reuse this fss */ | |
1361 | for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair; | |
1362 | fspair = nvlist_next_nvpair(sdd->fss, fspair)) { | |
1363 | nvlist_t *fslist; | |
1364 | ||
1365 | VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0); | |
1366 | (void) nvlist_remove_all(fslist, "sent"); | |
1367 | } | |
1368 | ||
34dc7c2f BB |
1369 | return (0); |
1370 | } | |
1371 | ||
1372 | /* | |
45d1cae3 BB |
1373 | * Generate a send stream for the dataset identified by the argument zhp. |
1374 | * | |
1375 | * The content of the send stream is the snapshot identified by | |
1376 | * 'tosnap'. Incremental streams are requested in two ways: | |
1377 | * - from the snapshot identified by "fromsnap" (if non-null) or | |
1378 | * - from the origin of the dataset identified by zhp, which must | |
1379 | * be a clone. In this case, "fromsnap" is null and "fromorigin" | |
1380 | * is TRUE. | |
1381 | * | |
1382 | * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and | |
428870ff | 1383 | * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM) |
45d1cae3 | 1384 | * if "replicate" is set. If "doall" is set, dump all the intermediate |
428870ff BB |
1385 | * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall" |
1386 | * case too. If "props" is set, send properties. | |
34dc7c2f BB |
1387 | */ |
1388 | int | |
1389 | zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, | |
330d06f9 | 1390 | sendflags_t *flags, int outfd, snapfilter_cb_t filter_func, |
428870ff | 1391 | void *cb_arg, nvlist_t **debugnvp) |
34dc7c2f BB |
1392 | { |
1393 | char errbuf[1024]; | |
1394 | send_dump_data_t sdd = { 0 }; | |
330d06f9 | 1395 | int err = 0; |
34dc7c2f BB |
1396 | nvlist_t *fss = NULL; |
1397 | avl_tree_t *fsavl = NULL; | |
428870ff BB |
1398 | static uint64_t holdseq; |
1399 | int spa_version; | |
a08ee875 | 1400 | pthread_t tid = 0; |
428870ff BB |
1401 | int pipefd[2]; |
1402 | dedup_arg_t dda = { 0 }; | |
1403 | int featureflags = 0; | |
1404 | ||
34dc7c2f BB |
1405 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
1406 | "cannot send '%s'"), zhp->zfs_name); | |
1407 | ||
1408 | if (fromsnap && fromsnap[0] == '\0') { | |
1409 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
1410 | "zero-length incremental source")); | |
1411 | return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); | |
1412 | } | |
1413 | ||
572e2857 BB |
1414 | if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) { |
1415 | uint64_t version; | |
1416 | version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); | |
1417 | if (version >= ZPL_VERSION_SA) { | |
1418 | featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; | |
1419 | } | |
1420 | } | |
1421 | ||
330d06f9 | 1422 | if (flags->dedup && !flags->dryrun) { |
428870ff BB |
1423 | featureflags |= (DMU_BACKUP_FEATURE_DEDUP | |
1424 | DMU_BACKUP_FEATURE_DEDUPPROPS); | |
1b9d8c34 | 1425 | if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd))) { |
428870ff BB |
1426 | zfs_error_aux(zhp->zfs_hdl, strerror(errno)); |
1427 | return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, | |
1428 | errbuf)); | |
1429 | } | |
1430 | dda.outputfd = outfd; | |
1431 | dda.inputfd = pipefd[1]; | |
1432 | dda.dedup_hdl = zhp->zfs_hdl; | |
c65aa5b2 | 1433 | if ((err = pthread_create(&tid, NULL, cksummer, &dda))) { |
428870ff BB |
1434 | (void) close(pipefd[0]); |
1435 | (void) close(pipefd[1]); | |
1436 | zfs_error_aux(zhp->zfs_hdl, strerror(errno)); | |
1437 | return (zfs_error(zhp->zfs_hdl, | |
1438 | EZFS_THREADCREATEFAILED, errbuf)); | |
1439 | } | |
1440 | } | |
1441 | ||
330d06f9 | 1442 | if (flags->replicate || flags->doall || flags->props) { |
34dc7c2f BB |
1443 | dmu_replay_record_t drr = { 0 }; |
1444 | char *packbuf = NULL; | |
1445 | size_t buflen = 0; | |
2598c001 | 1446 | zio_cksum_t zc = { { 0 } }; |
34dc7c2f | 1447 | |
330d06f9 | 1448 | if (flags->replicate || flags->props) { |
34dc7c2f BB |
1449 | nvlist_t *hdrnv; |
1450 | ||
1451 | VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0)); | |
1452 | if (fromsnap) { | |
1453 | VERIFY(0 == nvlist_add_string(hdrnv, | |
1454 | "fromsnap", fromsnap)); | |
1455 | } | |
1456 | VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap)); | |
330d06f9 | 1457 | if (!flags->replicate) { |
428870ff BB |
1458 | VERIFY(0 == nvlist_add_boolean(hdrnv, |
1459 | "not_recursive")); | |
1460 | } | |
34dc7c2f BB |
1461 | |
1462 | err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name, | |
330d06f9 | 1463 | fromsnap, tosnap, flags->replicate, &fss, &fsavl); |
572e2857 | 1464 | if (err) |
428870ff | 1465 | goto err_out; |
34dc7c2f BB |
1466 | VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss)); |
1467 | err = nvlist_pack(hdrnv, &packbuf, &buflen, | |
1468 | NV_ENCODE_XDR, 0); | |
428870ff BB |
1469 | if (debugnvp) |
1470 | *debugnvp = hdrnv; | |
1471 | else | |
1472 | nvlist_free(hdrnv); | |
a08ee875 | 1473 | if (err) |
428870ff | 1474 | goto stderr_out; |
34dc7c2f BB |
1475 | } |
1476 | ||
330d06f9 MA |
1477 | if (!flags->dryrun) { |
1478 | /* write first begin record */ | |
1479 | drr.drr_type = DRR_BEGIN; | |
1480 | drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; | |
1481 | DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin. | |
1482 | drr_versioninfo, DMU_COMPOUNDSTREAM); | |
1483 | DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin. | |
1484 | drr_versioninfo, featureflags); | |
1485 | (void) snprintf(drr.drr_u.drr_begin.drr_toname, | |
1486 | sizeof (drr.drr_u.drr_begin.drr_toname), | |
1487 | "%s@%s", zhp->zfs_name, tosnap); | |
1488 | drr.drr_payloadlen = buflen; | |
1489 | err = cksum_and_write(&drr, sizeof (drr), &zc, outfd); | |
1490 | ||
1491 | /* write header nvlist */ | |
1492 | if (err != -1 && packbuf != NULL) { | |
1493 | err = cksum_and_write(packbuf, buflen, &zc, | |
1494 | outfd); | |
1495 | } | |
1496 | free(packbuf); | |
1497 | if (err == -1) { | |
330d06f9 MA |
1498 | err = errno; |
1499 | goto stderr_out; | |
1500 | } | |
34dc7c2f | 1501 | |
330d06f9 | 1502 | /* write end record */ |
34dc7c2f BB |
1503 | bzero(&drr, sizeof (drr)); |
1504 | drr.drr_type = DRR_END; | |
1505 | drr.drr_u.drr_end.drr_checksum = zc; | |
1506 | err = write(outfd, &drr, sizeof (drr)); | |
1507 | if (err == -1) { | |
428870ff | 1508 | err = errno; |
428870ff | 1509 | goto stderr_out; |
34dc7c2f | 1510 | } |
330d06f9 MA |
1511 | |
1512 | err = 0; | |
34dc7c2f BB |
1513 | } |
1514 | } | |
1515 | ||
1516 | /* dump each stream */ | |
1517 | sdd.fromsnap = fromsnap; | |
1518 | sdd.tosnap = tosnap; | |
a08ee875 | 1519 | if (tid != 0) |
428870ff BB |
1520 | sdd.outfd = pipefd[0]; |
1521 | else | |
1522 | sdd.outfd = outfd; | |
330d06f9 MA |
1523 | sdd.replicate = flags->replicate; |
1524 | sdd.doall = flags->doall; | |
1525 | sdd.fromorigin = flags->fromorigin; | |
34dc7c2f BB |
1526 | sdd.fss = fss; |
1527 | sdd.fsavl = fsavl; | |
330d06f9 MA |
1528 | sdd.verbose = flags->verbose; |
1529 | sdd.parsable = flags->parsable; | |
37abac6d | 1530 | sdd.progress = flags->progress; |
330d06f9 | 1531 | sdd.dryrun = flags->dryrun; |
ea04106b | 1532 | sdd.embed_data = flags->embed_data; |
428870ff BB |
1533 | sdd.filter_cb = filter_func; |
1534 | sdd.filter_cb_arg = cb_arg; | |
1535 | if (debugnvp) | |
1536 | sdd.debugnv = *debugnvp; | |
e956d651 CS |
1537 | |
1538 | /* | |
1539 | * Some flags require that we place user holds on the datasets that are | |
1540 | * being sent so they don't get destroyed during the send. We can skip | |
1541 | * this step if the pool is imported read-only since the datasets cannot | |
1542 | * be destroyed. | |
1543 | */ | |
1544 | if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp), | |
1545 | ZPOOL_PROP_READONLY, NULL) && | |
1546 | zfs_spa_version(zhp, &spa_version) == 0 && | |
1547 | spa_version >= SPA_VERSION_USERREFS && | |
1548 | (flags->doall || flags->replicate)) { | |
572e2857 BB |
1549 | ++holdseq; |
1550 | (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag), | |
1551 | ".send-%d-%llu", getpid(), (u_longlong_t)holdseq); | |
325f0235 | 1552 | sdd.cleanup_fd = open(ZFS_DEV, O_RDWR); |
572e2857 BB |
1553 | if (sdd.cleanup_fd < 0) { |
1554 | err = errno; | |
1555 | goto stderr_out; | |
1556 | } | |
a08ee875 | 1557 | sdd.snapholds = fnvlist_alloc(); |
572e2857 BB |
1558 | } else { |
1559 | sdd.cleanup_fd = -1; | |
a08ee875 | 1560 | sdd.snapholds = NULL; |
572e2857 | 1561 | } |
a08ee875 | 1562 | if (flags->verbose || sdd.snapholds != NULL) { |
330d06f9 MA |
1563 | /* |
1564 | * Do a verbose no-op dry run to get all the verbose output | |
a08ee875 LG |
1565 | * or to gather snapshot hold's before generating any data, |
1566 | * then do a non-verbose real run to generate the streams. | |
330d06f9 MA |
1567 | */ |
1568 | sdd.dryrun = B_TRUE; | |
1569 | err = dump_filesystems(zhp, &sdd); | |
a08ee875 LG |
1570 | |
1571 | if (err != 0) | |
1572 | goto stderr_out; | |
1573 | ||
1574 | if (flags->verbose) { | |
1575 | if (flags->parsable) { | |
1576 | (void) fprintf(stderr, "size\t%llu\n", | |
1577 | (longlong_t)sdd.size); | |
1578 | } else { | |
1579 | char buf[16]; | |
1580 | zfs_nicenum(sdd.size, buf, sizeof (buf)); | |
1581 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
1582 | "total estimated size is %s\n"), buf); | |
1583 | } | |
1584 | } | |
1585 | ||
1586 | /* Ensure no snaps found is treated as an error. */ | |
1587 | if (!sdd.seento) { | |
1588 | err = ENOENT; | |
1589 | goto err_out; | |
1590 | } | |
1591 | ||
1592 | /* Skip the second run if dryrun was requested. */ | |
1593 | if (flags->dryrun) | |
1594 | goto err_out; | |
1595 | ||
1596 | if (sdd.snapholds != NULL) { | |
1597 | err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds); | |
1598 | if (err != 0) | |
1599 | goto stderr_out; | |
1600 | ||
1601 | fnvlist_free(sdd.snapholds); | |
1602 | sdd.snapholds = NULL; | |
330d06f9 | 1603 | } |
a08ee875 LG |
1604 | |
1605 | sdd.dryrun = B_FALSE; | |
1606 | sdd.verbose = B_FALSE; | |
330d06f9 | 1607 | } |
a08ee875 | 1608 | |
34dc7c2f BB |
1609 | err = dump_filesystems(zhp, &sdd); |
1610 | fsavl_destroy(fsavl); | |
1611 | nvlist_free(fss); | |
1612 | ||
a08ee875 LG |
1613 | /* Ensure no snaps found is treated as an error. */ |
1614 | if (err == 0 && !sdd.seento) | |
1615 | err = ENOENT; | |
1616 | ||
1617 | if (tid != 0) { | |
1618 | if (err != 0) | |
1619 | (void) pthread_cancel(tid); | |
428870ff BB |
1620 | (void) close(pipefd[0]); |
1621 | (void) pthread_join(tid, NULL); | |
1622 | } | |
1623 | ||
572e2857 BB |
1624 | if (sdd.cleanup_fd != -1) { |
1625 | VERIFY(0 == close(sdd.cleanup_fd)); | |
1626 | sdd.cleanup_fd = -1; | |
1627 | } | |
1628 | ||
330d06f9 MA |
1629 | if (!flags->dryrun && (flags->replicate || flags->doall || |
1630 | flags->props)) { | |
34dc7c2f BB |
1631 | /* |
1632 | * write final end record. NB: want to do this even if | |
1633 | * there was some error, because it might not be totally | |
1634 | * failed. | |
1635 | */ | |
1636 | dmu_replay_record_t drr = { 0 }; | |
1637 | drr.drr_type = DRR_END; | |
1638 | if (write(outfd, &drr, sizeof (drr)) == -1) { | |
1639 | return (zfs_standard_error(zhp->zfs_hdl, | |
1640 | errno, errbuf)); | |
1641 | } | |
1642 | } | |
1643 | ||
1644 | return (err || sdd.err); | |
428870ff BB |
1645 | |
1646 | stderr_out: | |
1647 | err = zfs_standard_error(zhp->zfs_hdl, err, errbuf); | |
1648 | err_out: | |
a08ee875 LG |
1649 | fsavl_destroy(fsavl); |
1650 | nvlist_free(fss); | |
1651 | fnvlist_free(sdd.snapholds); | |
1652 | ||
572e2857 BB |
1653 | if (sdd.cleanup_fd != -1) |
1654 | VERIFY(0 == close(sdd.cleanup_fd)); | |
a08ee875 | 1655 | if (tid != 0) { |
428870ff | 1656 | (void) pthread_cancel(tid); |
428870ff | 1657 | (void) close(pipefd[0]); |
a08ee875 | 1658 | (void) pthread_join(tid, NULL); |
428870ff BB |
1659 | } |
1660 | return (err); | |
34dc7c2f BB |
1661 | } |
1662 | ||
ea04106b AX |
1663 | int |
1664 | zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, | |
1665 | enum lzc_send_flags flags) | |
1666 | { | |
1667 | int err; | |
1668 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
1669 | ||
1670 | char errbuf[1024]; | |
1671 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
1672 | "warning: cannot send '%s'"), zhp->zfs_name); | |
1673 | ||
1674 | err = lzc_send(zhp->zfs_name, from, fd, flags); | |
1675 | if (err != 0) { | |
1676 | switch (errno) { | |
1677 | case EXDEV: | |
1678 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1679 | "not an earlier snapshot from the same fs")); | |
1680 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
1681 | ||
1682 | case ENOENT: | |
1683 | case ESRCH: | |
1684 | if (lzc_exists(zhp->zfs_name)) { | |
1685 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1686 | "incremental source (%s) does not exist"), | |
1687 | from); | |
1688 | } | |
1689 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
1690 | ||
1691 | case EBUSY: | |
1692 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1693 | "target is busy; if a filesystem, " | |
1694 | "it must not be mounted")); | |
1695 | return (zfs_error(hdl, EZFS_BUSY, errbuf)); | |
1696 | ||
1697 | case EDQUOT: | |
1698 | case EFBIG: | |
1699 | case EIO: | |
1700 | case ENOLINK: | |
1701 | case ENOSPC: | |
1702 | case ENOSTR: | |
1703 | case ENXIO: | |
1704 | case EPIPE: | |
1705 | case ERANGE: | |
1706 | case EFAULT: | |
1707 | case EROFS: | |
1708 | zfs_error_aux(hdl, strerror(errno)); | |
1709 | return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); | |
1710 | ||
1711 | default: | |
1712 | return (zfs_standard_error(hdl, errno, errbuf)); | |
1713 | } | |
1714 | } | |
1715 | return (err != 0); | |
1716 | } | |
1717 | ||
34dc7c2f BB |
1718 | /* |
1719 | * Routines specific to "zfs recv" | |
1720 | */ | |
1721 | ||
1722 | static int | |
1723 | recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen, | |
1724 | boolean_t byteswap, zio_cksum_t *zc) | |
1725 | { | |
1726 | char *cp = buf; | |
1727 | int rv; | |
1728 | int len = ilen; | |
1729 | ||
1730 | do { | |
1731 | rv = read(fd, cp, len); | |
1732 | cp += rv; | |
1733 | len -= rv; | |
1734 | } while (rv > 0); | |
1735 | ||
1736 | if (rv < 0 || len != 0) { | |
1737 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1738 | "failed to read from stream")); | |
1739 | return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN, | |
1740 | "cannot receive"))); | |
1741 | } | |
1742 | ||
1743 | if (zc) { | |
1744 | if (byteswap) | |
1745 | fletcher_4_incremental_byteswap(buf, ilen, zc); | |
1746 | else | |
1747 | fletcher_4_incremental_native(buf, ilen, zc); | |
1748 | } | |
1749 | return (0); | |
1750 | } | |
1751 | ||
1752 | static int | |
1753 | recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp, | |
1754 | boolean_t byteswap, zio_cksum_t *zc) | |
1755 | { | |
1756 | char *buf; | |
1757 | int err; | |
1758 | ||
1759 | buf = zfs_alloc(hdl, len); | |
1760 | if (buf == NULL) | |
1761 | return (ENOMEM); | |
1762 | ||
1763 | err = recv_read(hdl, fd, buf, len, byteswap, zc); | |
1764 | if (err != 0) { | |
1765 | free(buf); | |
1766 | return (err); | |
1767 | } | |
1768 | ||
1769 | err = nvlist_unpack(buf, len, nvp, 0); | |
1770 | free(buf); | |
1771 | if (err != 0) { | |
1772 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
1773 | "stream (malformed nvlist)")); | |
1774 | return (EINVAL); | |
1775 | } | |
1776 | return (0); | |
1777 | } | |
1778 | ||
1779 | static int | |
1780 | recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname, | |
330d06f9 | 1781 | int baselen, char *newname, recvflags_t *flags) |
34dc7c2f BB |
1782 | { |
1783 | static int seq; | |
a08ee875 | 1784 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1785 | int err; |
1786 | prop_changelist_t *clp; | |
1787 | zfs_handle_t *zhp; | |
1788 | ||
1789 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
1790 | if (zhp == NULL) | |
1791 | return (-1); | |
b128c09f | 1792 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, |
330d06f9 | 1793 | flags->force ? MS_FORCE : 0); |
34dc7c2f BB |
1794 | zfs_close(zhp); |
1795 | if (clp == NULL) | |
1796 | return (-1); | |
1797 | err = changelist_prefix(clp); | |
1798 | if (err) | |
1799 | return (err); | |
1800 | ||
45d1cae3 BB |
1801 | zc.zc_objset_type = DMU_OST_ZFS; |
1802 | (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); | |
1803 | ||
34dc7c2f BB |
1804 | if (tryname) { |
1805 | (void) strcpy(newname, tryname); | |
1806 | ||
34dc7c2f BB |
1807 | (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value)); |
1808 | ||
330d06f9 | 1809 | if (flags->verbose) { |
34dc7c2f BB |
1810 | (void) printf("attempting rename %s to %s\n", |
1811 | zc.zc_name, zc.zc_value); | |
1812 | } | |
1813 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); | |
1814 | if (err == 0) | |
1815 | changelist_rename(clp, name, tryname); | |
1816 | } else { | |
1817 | err = ENOENT; | |
1818 | } | |
1819 | ||
a08ee875 | 1820 | if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) { |
34dc7c2f BB |
1821 | seq++; |
1822 | ||
a08ee875 LG |
1823 | (void) snprintf(newname, ZFS_MAXNAMELEN, "%.*srecv-%u-%u", |
1824 | baselen, name, getpid(), seq); | |
34dc7c2f BB |
1825 | (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value)); |
1826 | ||
330d06f9 | 1827 | if (flags->verbose) { |
34dc7c2f BB |
1828 | (void) printf("failed - trying rename %s to %s\n", |
1829 | zc.zc_name, zc.zc_value); | |
1830 | } | |
1831 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc); | |
1832 | if (err == 0) | |
1833 | changelist_rename(clp, name, newname); | |
330d06f9 | 1834 | if (err && flags->verbose) { |
34dc7c2f BB |
1835 | (void) printf("failed (%u) - " |
1836 | "will try again on next pass\n", errno); | |
1837 | } | |
1838 | err = EAGAIN; | |
330d06f9 | 1839 | } else if (flags->verbose) { |
34dc7c2f BB |
1840 | if (err == 0) |
1841 | (void) printf("success\n"); | |
1842 | else | |
1843 | (void) printf("failed (%u)\n", errno); | |
1844 | } | |
1845 | ||
1846 | (void) changelist_postfix(clp); | |
1847 | changelist_free(clp); | |
1848 | ||
1849 | return (err); | |
1850 | } | |
1851 | ||
1852 | static int | |
1853 | recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen, | |
330d06f9 | 1854 | char *newname, recvflags_t *flags) |
34dc7c2f | 1855 | { |
a08ee875 | 1856 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
1857 | int err = 0; |
1858 | prop_changelist_t *clp; | |
1859 | zfs_handle_t *zhp; | |
45d1cae3 BB |
1860 | boolean_t defer = B_FALSE; |
1861 | int spa_version; | |
34dc7c2f BB |
1862 | |
1863 | zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET); | |
1864 | if (zhp == NULL) | |
1865 | return (-1); | |
b128c09f | 1866 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, |
330d06f9 | 1867 | flags->force ? MS_FORCE : 0); |
45d1cae3 BB |
1868 | if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && |
1869 | zfs_spa_version(zhp, &spa_version) == 0 && | |
1870 | spa_version >= SPA_VERSION_USERREFS) | |
1871 | defer = B_TRUE; | |
34dc7c2f BB |
1872 | zfs_close(zhp); |
1873 | if (clp == NULL) | |
1874 | return (-1); | |
1875 | err = changelist_prefix(clp); | |
1876 | if (err) | |
1877 | return (err); | |
1878 | ||
1879 | zc.zc_objset_type = DMU_OST_ZFS; | |
45d1cae3 | 1880 | zc.zc_defer_destroy = defer; |
34dc7c2f BB |
1881 | (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name)); |
1882 | ||
330d06f9 | 1883 | if (flags->verbose) |
34dc7c2f BB |
1884 | (void) printf("attempting destroy %s\n", zc.zc_name); |
1885 | err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc); | |
34dc7c2f | 1886 | if (err == 0) { |
330d06f9 | 1887 | if (flags->verbose) |
34dc7c2f BB |
1888 | (void) printf("success\n"); |
1889 | changelist_remove(clp, zc.zc_name); | |
1890 | } | |
1891 | ||
1892 | (void) changelist_postfix(clp); | |
1893 | changelist_free(clp); | |
1894 | ||
45d1cae3 | 1895 | /* |
428870ff BB |
1896 | * Deferred destroy might destroy the snapshot or only mark it to be |
1897 | * destroyed later, and it returns success in either case. | |
45d1cae3 | 1898 | */ |
428870ff BB |
1899 | if (err != 0 || (defer && zfs_dataset_exists(hdl, name, |
1900 | ZFS_TYPE_SNAPSHOT))) { | |
34dc7c2f | 1901 | err = recv_rename(hdl, name, NULL, baselen, newname, flags); |
428870ff | 1902 | } |
34dc7c2f BB |
1903 | |
1904 | return (err); | |
1905 | } | |
1906 | ||
1907 | typedef struct guid_to_name_data { | |
1908 | uint64_t guid; | |
1909 | char *name; | |
330d06f9 | 1910 | char *skip; |
34dc7c2f BB |
1911 | } guid_to_name_data_t; |
1912 | ||
1913 | static int | |
1914 | guid_to_name_cb(zfs_handle_t *zhp, void *arg) | |
1915 | { | |
1916 | guid_to_name_data_t *gtnd = arg; | |
1917 | int err; | |
1918 | ||
330d06f9 MA |
1919 | if (gtnd->skip != NULL && |
1920 | strcmp(zhp->zfs_name, gtnd->skip) == 0) { | |
1921 | return (0); | |
1922 | } | |
1923 | ||
34dc7c2f BB |
1924 | if (zhp->zfs_dmustats.dds_guid == gtnd->guid) { |
1925 | (void) strcpy(gtnd->name, zhp->zfs_name); | |
428870ff | 1926 | zfs_close(zhp); |
34dc7c2f BB |
1927 | return (EEXIST); |
1928 | } | |
330d06f9 | 1929 | |
34dc7c2f BB |
1930 | err = zfs_iter_children(zhp, guid_to_name_cb, gtnd); |
1931 | zfs_close(zhp); | |
1932 | return (err); | |
1933 | } | |
1934 | ||
330d06f9 MA |
1935 | /* |
1936 | * Attempt to find the local dataset associated with this guid. In the case of | |
1937 | * multiple matches, we attempt to find the "best" match by searching | |
1938 | * progressively larger portions of the hierarchy. This allows one to send a | |
1939 | * tree of datasets individually and guarantee that we will find the source | |
1940 | * guid within that hierarchy, even if there are multiple matches elsewhere. | |
1941 | */ | |
34dc7c2f BB |
1942 | static int |
1943 | guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid, | |
1944 | char *name) | |
1945 | { | |
1946 | /* exhaustive search all local snapshots */ | |
330d06f9 | 1947 | char pname[ZFS_MAXNAMELEN]; |
34dc7c2f BB |
1948 | guid_to_name_data_t gtnd; |
1949 | int err = 0; | |
1950 | zfs_handle_t *zhp; | |
1951 | char *cp; | |
1952 | ||
1953 | gtnd.guid = guid; | |
1954 | gtnd.name = name; | |
330d06f9 | 1955 | gtnd.skip = NULL; |
34dc7c2f | 1956 | |
330d06f9 | 1957 | (void) strlcpy(pname, parent, sizeof (pname)); |
34dc7c2f | 1958 | |
330d06f9 MA |
1959 | /* |
1960 | * Search progressively larger portions of the hierarchy. This will | |
1961 | * select the "most local" version of the origin snapshot in the case | |
1962 | * that there are multiple matching snapshots in the system. | |
1963 | */ | |
1964 | while ((cp = strrchr(pname, '/')) != NULL) { | |
1965 | ||
1966 | /* Chop off the last component and open the parent */ | |
34dc7c2f | 1967 | *cp = '\0'; |
330d06f9 MA |
1968 | zhp = make_dataset_handle(hdl, pname); |
1969 | ||
1970 | if (zhp == NULL) | |
1971 | continue; | |
34dc7c2f | 1972 | |
34dc7c2f BB |
1973 | err = zfs_iter_children(zhp, guid_to_name_cb, >nd); |
1974 | zfs_close(zhp); | |
330d06f9 MA |
1975 | if (err == EEXIST) |
1976 | return (0); | |
34dc7c2f | 1977 | |
330d06f9 MA |
1978 | /* |
1979 | * Remember the dataset that we already searched, so we | |
1980 | * skip it next time through. | |
1981 | */ | |
1982 | gtnd.skip = pname; | |
1983 | } | |
34dc7c2f | 1984 | |
330d06f9 | 1985 | return (ENOENT); |
34dc7c2f BB |
1986 | } |
1987 | ||
1988 | /* | |
330d06f9 MA |
1989 | * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if |
1990 | * guid1 is after guid2. | |
34dc7c2f BB |
1991 | */ |
1992 | static int | |
1993 | created_before(libzfs_handle_t *hdl, avl_tree_t *avl, | |
1994 | uint64_t guid1, uint64_t guid2) | |
1995 | { | |
1996 | nvlist_t *nvfs; | |
1997 | char *fsname, *snapname; | |
1998 | char buf[ZFS_MAXNAMELEN]; | |
1999 | int rv; | |
330d06f9 MA |
2000 | zfs_handle_t *guid1hdl, *guid2hdl; |
2001 | uint64_t create1, create2; | |
34dc7c2f BB |
2002 | |
2003 | if (guid2 == 0) | |
2004 | return (0); | |
2005 | if (guid1 == 0) | |
2006 | return (1); | |
2007 | ||
2008 | nvfs = fsavl_find(avl, guid1, &snapname); | |
2009 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2010 | (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); | |
330d06f9 MA |
2011 | guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); |
2012 | if (guid1hdl == NULL) | |
34dc7c2f BB |
2013 | return (-1); |
2014 | ||
2015 | nvfs = fsavl_find(avl, guid2, &snapname); | |
2016 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2017 | (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname); | |
330d06f9 MA |
2018 | guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT); |
2019 | if (guid2hdl == NULL) { | |
2020 | zfs_close(guid1hdl); | |
34dc7c2f BB |
2021 | return (-1); |
2022 | } | |
2023 | ||
330d06f9 MA |
2024 | create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG); |
2025 | create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG); | |
34dc7c2f | 2026 | |
330d06f9 MA |
2027 | if (create1 < create2) |
2028 | rv = -1; | |
2029 | else if (create1 > create2) | |
2030 | rv = +1; | |
2031 | else | |
2032 | rv = 0; | |
2033 | ||
2034 | zfs_close(guid1hdl); | |
2035 | zfs_close(guid2hdl); | |
34dc7c2f BB |
2036 | |
2037 | return (rv); | |
2038 | } | |
2039 | ||
2040 | static int | |
2041 | recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs, | |
330d06f9 | 2042 | recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl, |
428870ff | 2043 | nvlist_t *renamed) |
34dc7c2f | 2044 | { |
ea04106b | 2045 | nvlist_t *local_nv, *deleted = NULL; |
34dc7c2f BB |
2046 | avl_tree_t *local_avl; |
2047 | nvpair_t *fselem, *nextfselem; | |
428870ff | 2048 | char *fromsnap; |
34dc7c2f | 2049 | char newname[ZFS_MAXNAMELEN]; |
ea04106b | 2050 | char guidname[32]; |
34dc7c2f | 2051 | int error; |
428870ff BB |
2052 | boolean_t needagain, progress, recursive; |
2053 | char *s1, *s2; | |
34dc7c2f BB |
2054 | |
2055 | VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap)); | |
428870ff BB |
2056 | |
2057 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == | |
2058 | ENOENT); | |
34dc7c2f | 2059 | |
330d06f9 | 2060 | if (flags->dryrun) |
34dc7c2f BB |
2061 | return (0); |
2062 | ||
2063 | again: | |
2064 | needagain = progress = B_FALSE; | |
2065 | ||
ea04106b AX |
2066 | VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0)); |
2067 | ||
34dc7c2f | 2068 | if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL, |
428870ff | 2069 | recursive, &local_nv, &local_avl)) != 0) |
34dc7c2f BB |
2070 | return (error); |
2071 | ||
2072 | /* | |
2073 | * Process deletes and renames | |
2074 | */ | |
2075 | for (fselem = nvlist_next_nvpair(local_nv, NULL); | |
2076 | fselem; fselem = nextfselem) { | |
2077 | nvlist_t *nvfs, *snaps; | |
2078 | nvlist_t *stream_nvfs = NULL; | |
2079 | nvpair_t *snapelem, *nextsnapelem; | |
2080 | uint64_t fromguid = 0; | |
2081 | uint64_t originguid = 0; | |
2082 | uint64_t stream_originguid = 0; | |
2083 | uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid; | |
2084 | char *fsname, *stream_fsname; | |
2085 | ||
2086 | nextfselem = nvlist_next_nvpair(local_nv, fselem); | |
2087 | ||
2088 | VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs)); | |
2089 | VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps)); | |
2090 | VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname)); | |
2091 | VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap", | |
2092 | &parent_fromsnap_guid)); | |
2093 | (void) nvlist_lookup_uint64(nvfs, "origin", &originguid); | |
2094 | ||
2095 | /* | |
2096 | * First find the stream's fs, so we can check for | |
2097 | * a different origin (due to "zfs promote") | |
2098 | */ | |
2099 | for (snapelem = nvlist_next_nvpair(snaps, NULL); | |
2100 | snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) { | |
2101 | uint64_t thisguid; | |
2102 | ||
2103 | VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); | |
2104 | stream_nvfs = fsavl_find(stream_avl, thisguid, NULL); | |
2105 | ||
2106 | if (stream_nvfs != NULL) | |
2107 | break; | |
2108 | } | |
2109 | ||
2110 | /* check for promote */ | |
2111 | (void) nvlist_lookup_uint64(stream_nvfs, "origin", | |
2112 | &stream_originguid); | |
2113 | if (stream_nvfs && originguid != stream_originguid) { | |
2114 | switch (created_before(hdl, local_avl, | |
2115 | stream_originguid, originguid)) { | |
2116 | case 1: { | |
2117 | /* promote it! */ | |
a08ee875 | 2118 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
2119 | nvlist_t *origin_nvfs; |
2120 | char *origin_fsname; | |
2121 | ||
330d06f9 | 2122 | if (flags->verbose) |
34dc7c2f BB |
2123 | (void) printf("promoting %s\n", fsname); |
2124 | ||
2125 | origin_nvfs = fsavl_find(local_avl, originguid, | |
2126 | NULL); | |
2127 | VERIFY(0 == nvlist_lookup_string(origin_nvfs, | |
2128 | "name", &origin_fsname)); | |
2129 | (void) strlcpy(zc.zc_value, origin_fsname, | |
2130 | sizeof (zc.zc_value)); | |
2131 | (void) strlcpy(zc.zc_name, fsname, | |
2132 | sizeof (zc.zc_name)); | |
2133 | error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); | |
2134 | if (error == 0) | |
2135 | progress = B_TRUE; | |
2136 | break; | |
2137 | } | |
2138 | default: | |
2139 | break; | |
2140 | case -1: | |
2141 | fsavl_destroy(local_avl); | |
2142 | nvlist_free(local_nv); | |
2143 | return (-1); | |
2144 | } | |
2145 | /* | |
2146 | * We had/have the wrong origin, therefore our | |
2147 | * list of snapshots is wrong. Need to handle | |
2148 | * them on the next pass. | |
2149 | */ | |
2150 | needagain = B_TRUE; | |
2151 | continue; | |
2152 | } | |
2153 | ||
2154 | for (snapelem = nvlist_next_nvpair(snaps, NULL); | |
2155 | snapelem; snapelem = nextsnapelem) { | |
2156 | uint64_t thisguid; | |
2157 | char *stream_snapname; | |
b128c09f | 2158 | nvlist_t *found, *props; |
34dc7c2f BB |
2159 | |
2160 | nextsnapelem = nvlist_next_nvpair(snaps, snapelem); | |
2161 | ||
2162 | VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid)); | |
2163 | found = fsavl_find(stream_avl, thisguid, | |
2164 | &stream_snapname); | |
2165 | ||
2166 | /* check for delete */ | |
2167 | if (found == NULL) { | |
2168 | char name[ZFS_MAXNAMELEN]; | |
2169 | ||
330d06f9 | 2170 | if (!flags->force) |
34dc7c2f BB |
2171 | continue; |
2172 | ||
2173 | (void) snprintf(name, sizeof (name), "%s@%s", | |
2174 | fsname, nvpair_name(snapelem)); | |
2175 | ||
2176 | error = recv_destroy(hdl, name, | |
2177 | strlen(fsname)+1, newname, flags); | |
2178 | if (error) | |
2179 | needagain = B_TRUE; | |
2180 | else | |
2181 | progress = B_TRUE; | |
ea04106b AX |
2182 | sprintf(guidname, "%lu", thisguid); |
2183 | nvlist_add_boolean(deleted, guidname); | |
34dc7c2f BB |
2184 | continue; |
2185 | } | |
2186 | ||
2187 | stream_nvfs = found; | |
2188 | ||
b128c09f BB |
2189 | if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops", |
2190 | &props) && 0 == nvlist_lookup_nvlist(props, | |
2191 | stream_snapname, &props)) { | |
a08ee875 | 2192 | zfs_cmd_t zc = {"\0"}; |
b128c09f | 2193 | |
428870ff | 2194 | zc.zc_cookie = B_TRUE; /* received */ |
b128c09f BB |
2195 | (void) snprintf(zc.zc_name, sizeof (zc.zc_name), |
2196 | "%s@%s", fsname, nvpair_name(snapelem)); | |
2197 | if (zcmd_write_src_nvlist(hdl, &zc, | |
2198 | props) == 0) { | |
2199 | (void) zfs_ioctl(hdl, | |
2200 | ZFS_IOC_SET_PROP, &zc); | |
2201 | zcmd_free_nvlists(&zc); | |
2202 | } | |
2203 | } | |
2204 | ||
34dc7c2f BB |
2205 | /* check for different snapname */ |
2206 | if (strcmp(nvpair_name(snapelem), | |
2207 | stream_snapname) != 0) { | |
2208 | char name[ZFS_MAXNAMELEN]; | |
2209 | char tryname[ZFS_MAXNAMELEN]; | |
2210 | ||
2211 | (void) snprintf(name, sizeof (name), "%s@%s", | |
2212 | fsname, nvpair_name(snapelem)); | |
2213 | (void) snprintf(tryname, sizeof (name), "%s@%s", | |
2214 | fsname, stream_snapname); | |
2215 | ||
2216 | error = recv_rename(hdl, name, tryname, | |
2217 | strlen(fsname)+1, newname, flags); | |
2218 | if (error) | |
2219 | needagain = B_TRUE; | |
2220 | else | |
2221 | progress = B_TRUE; | |
2222 | } | |
2223 | ||
2224 | if (strcmp(stream_snapname, fromsnap) == 0) | |
2225 | fromguid = thisguid; | |
2226 | } | |
2227 | ||
2228 | /* check for delete */ | |
2229 | if (stream_nvfs == NULL) { | |
330d06f9 | 2230 | if (!flags->force) |
34dc7c2f BB |
2231 | continue; |
2232 | ||
2233 | error = recv_destroy(hdl, fsname, strlen(tofs)+1, | |
2234 | newname, flags); | |
2235 | if (error) | |
2236 | needagain = B_TRUE; | |
2237 | else | |
2238 | progress = B_TRUE; | |
ea04106b AX |
2239 | sprintf(guidname, "%lu", parent_fromsnap_guid); |
2240 | nvlist_add_boolean(deleted, guidname); | |
34dc7c2f BB |
2241 | continue; |
2242 | } | |
2243 | ||
428870ff | 2244 | if (fromguid == 0) { |
330d06f9 | 2245 | if (flags->verbose) { |
428870ff BB |
2246 | (void) printf("local fs %s does not have " |
2247 | "fromsnap (%s in stream); must have " | |
2248 | "been deleted locally; ignoring\n", | |
2249 | fsname, fromsnap); | |
2250 | } | |
34dc7c2f BB |
2251 | continue; |
2252 | } | |
2253 | ||
2254 | VERIFY(0 == nvlist_lookup_string(stream_nvfs, | |
2255 | "name", &stream_fsname)); | |
2256 | VERIFY(0 == nvlist_lookup_uint64(stream_nvfs, | |
2257 | "parentfromsnap", &stream_parent_fromsnap_guid)); | |
2258 | ||
428870ff BB |
2259 | s1 = strrchr(fsname, '/'); |
2260 | s2 = strrchr(stream_fsname, '/'); | |
2261 | ||
ea04106b AX |
2262 | /* |
2263 | * Check if we're going to rename based on parent guid change | |
2264 | * and the current parent guid was also deleted. If it was then | |
2265 | * rename will fail and is likely unneeded, so avoid this and | |
2266 | * force an early retry to determine the new | |
2267 | * parent_fromsnap_guid. | |
2268 | */ | |
2269 | if (stream_parent_fromsnap_guid != 0 && | |
2270 | parent_fromsnap_guid != 0 && | |
2271 | stream_parent_fromsnap_guid != parent_fromsnap_guid) { | |
2272 | sprintf(guidname, "%lu", parent_fromsnap_guid); | |
2273 | if (nvlist_exists(deleted, guidname)) { | |
2274 | progress = B_TRUE; | |
2275 | needagain = B_TRUE; | |
2276 | goto doagain; | |
2277 | } | |
2278 | } | |
2279 | ||
428870ff BB |
2280 | /* |
2281 | * Check for rename. If the exact receive path is specified, it | |
2282 | * does not count as a rename, but we still need to check the | |
2283 | * datasets beneath it. | |
2284 | */ | |
34dc7c2f | 2285 | if ((stream_parent_fromsnap_guid != 0 && |
428870ff | 2286 | parent_fromsnap_guid != 0 && |
34dc7c2f | 2287 | stream_parent_fromsnap_guid != parent_fromsnap_guid) || |
330d06f9 | 2288 | ((flags->isprefix || strcmp(tofs, fsname) != 0) && |
428870ff | 2289 | (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) { |
34dc7c2f BB |
2290 | nvlist_t *parent; |
2291 | char tryname[ZFS_MAXNAMELEN]; | |
2292 | ||
2293 | parent = fsavl_find(local_avl, | |
2294 | stream_parent_fromsnap_guid, NULL); | |
2295 | /* | |
2296 | * NB: parent might not be found if we used the | |
2297 | * tosnap for stream_parent_fromsnap_guid, | |
2298 | * because the parent is a newly-created fs; | |
2299 | * we'll be able to rename it after we recv the | |
2300 | * new fs. | |
2301 | */ | |
2302 | if (parent != NULL) { | |
2303 | char *pname; | |
2304 | ||
2305 | VERIFY(0 == nvlist_lookup_string(parent, "name", | |
2306 | &pname)); | |
2307 | (void) snprintf(tryname, sizeof (tryname), | |
2308 | "%s%s", pname, strrchr(stream_fsname, '/')); | |
2309 | } else { | |
2310 | tryname[0] = '\0'; | |
330d06f9 | 2311 | if (flags->verbose) { |
34dc7c2f BB |
2312 | (void) printf("local fs %s new parent " |
2313 | "not found\n", fsname); | |
2314 | } | |
2315 | } | |
2316 | ||
428870ff BB |
2317 | newname[0] = '\0'; |
2318 | ||
34dc7c2f BB |
2319 | error = recv_rename(hdl, fsname, tryname, |
2320 | strlen(tofs)+1, newname, flags); | |
428870ff BB |
2321 | |
2322 | if (renamed != NULL && newname[0] != '\0') { | |
2323 | VERIFY(0 == nvlist_add_boolean(renamed, | |
2324 | newname)); | |
2325 | } | |
2326 | ||
34dc7c2f BB |
2327 | if (error) |
2328 | needagain = B_TRUE; | |
2329 | else | |
2330 | progress = B_TRUE; | |
2331 | } | |
2332 | } | |
2333 | ||
ea04106b | 2334 | doagain: |
34dc7c2f BB |
2335 | fsavl_destroy(local_avl); |
2336 | nvlist_free(local_nv); | |
ea04106b | 2337 | nvlist_free(deleted); |
34dc7c2f BB |
2338 | |
2339 | if (needagain && progress) { | |
2340 | /* do another pass to fix up temporary names */ | |
330d06f9 | 2341 | if (flags->verbose) |
34dc7c2f BB |
2342 | (void) printf("another pass:\n"); |
2343 | goto again; | |
2344 | } | |
2345 | ||
2346 | return (needagain); | |
2347 | } | |
2348 | ||
2349 | static int | |
2350 | zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname, | |
330d06f9 | 2351 | recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc, |
572e2857 | 2352 | char **top_zfs, int cleanup_fd, uint64_t *action_handlep) |
34dc7c2f BB |
2353 | { |
2354 | nvlist_t *stream_nv = NULL; | |
2355 | avl_tree_t *stream_avl = NULL; | |
2356 | char *fromsnap = NULL; | |
428870ff | 2357 | char *cp; |
34dc7c2f | 2358 | char tofs[ZFS_MAXNAMELEN]; |
428870ff | 2359 | char sendfs[ZFS_MAXNAMELEN]; |
34dc7c2f BB |
2360 | char errbuf[1024]; |
2361 | dmu_replay_record_t drre; | |
2362 | int error; | |
2363 | boolean_t anyerr = B_FALSE; | |
2364 | boolean_t softerr = B_FALSE; | |
428870ff | 2365 | boolean_t recursive; |
34dc7c2f BB |
2366 | |
2367 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2368 | "cannot receive")); | |
2369 | ||
34dc7c2f BB |
2370 | assert(drr->drr_type == DRR_BEGIN); |
2371 | assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC); | |
428870ff BB |
2372 | assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) == |
2373 | DMU_COMPOUNDSTREAM); | |
34dc7c2f BB |
2374 | |
2375 | /* | |
2376 | * Read in the nvlist from the stream. | |
2377 | */ | |
2378 | if (drr->drr_payloadlen != 0) { | |
34dc7c2f | 2379 | error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen, |
330d06f9 | 2380 | &stream_nv, flags->byteswap, zc); |
34dc7c2f BB |
2381 | if (error) { |
2382 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2383 | goto out; | |
2384 | } | |
2385 | } | |
2386 | ||
428870ff BB |
2387 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == |
2388 | ENOENT); | |
2389 | ||
2390 | if (recursive && strchr(destname, '@')) { | |
2391 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2392 | "cannot specify snapshot name for multi-snapshot stream")); | |
2393 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2394 | goto out; | |
2395 | } | |
2396 | ||
34dc7c2f BB |
2397 | /* |
2398 | * Read in the end record and verify checksum. | |
2399 | */ | |
2400 | if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre), | |
330d06f9 | 2401 | flags->byteswap, NULL))) |
34dc7c2f | 2402 | goto out; |
330d06f9 | 2403 | if (flags->byteswap) { |
34dc7c2f BB |
2404 | drre.drr_type = BSWAP_32(drre.drr_type); |
2405 | drre.drr_u.drr_end.drr_checksum.zc_word[0] = | |
2406 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]); | |
2407 | drre.drr_u.drr_end.drr_checksum.zc_word[1] = | |
2408 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]); | |
2409 | drre.drr_u.drr_end.drr_checksum.zc_word[2] = | |
2410 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]); | |
2411 | drre.drr_u.drr_end.drr_checksum.zc_word[3] = | |
2412 | BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]); | |
2413 | } | |
2414 | if (drre.drr_type != DRR_END) { | |
2415 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2416 | goto out; | |
2417 | } | |
2418 | if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) { | |
2419 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2420 | "incorrect header checksum")); | |
2421 | error = zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
2422 | goto out; | |
2423 | } | |
2424 | ||
2425 | (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap); | |
2426 | ||
2427 | if (drr->drr_payloadlen != 0) { | |
2428 | nvlist_t *stream_fss; | |
2429 | ||
2430 | VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", | |
2431 | &stream_fss)); | |
2432 | if ((stream_avl = fsavl_create(stream_fss)) == NULL) { | |
2433 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2434 | "couldn't allocate avl tree")); | |
2435 | error = zfs_error(hdl, EZFS_NOMEM, errbuf); | |
2436 | goto out; | |
2437 | } | |
2438 | ||
2439 | if (fromsnap != NULL) { | |
428870ff BB |
2440 | nvlist_t *renamed = NULL; |
2441 | nvpair_t *pair = NULL; | |
2442 | ||
34dc7c2f | 2443 | (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN); |
330d06f9 | 2444 | if (flags->isprefix) { |
428870ff BB |
2445 | struct drr_begin *drrb = &drr->drr_u.drr_begin; |
2446 | int i; | |
2447 | ||
330d06f9 | 2448 | if (flags->istail) { |
428870ff BB |
2449 | cp = strrchr(drrb->drr_toname, '/'); |
2450 | if (cp == NULL) { | |
2451 | (void) strlcat(tofs, "/", | |
2452 | ZFS_MAXNAMELEN); | |
2453 | i = 0; | |
2454 | } else { | |
2455 | i = (cp - drrb->drr_toname); | |
2456 | } | |
2457 | } else { | |
2458 | i = strcspn(drrb->drr_toname, "/@"); | |
2459 | } | |
34dc7c2f | 2460 | /* zfs_receive_one() will create_parents() */ |
428870ff | 2461 | (void) strlcat(tofs, &drrb->drr_toname[i], |
34dc7c2f BB |
2462 | ZFS_MAXNAMELEN); |
2463 | *strchr(tofs, '@') = '\0'; | |
2464 | } | |
428870ff | 2465 | |
330d06f9 | 2466 | if (recursive && !flags->dryrun && !flags->nomount) { |
428870ff BB |
2467 | VERIFY(0 == nvlist_alloc(&renamed, |
2468 | NV_UNIQUE_NAME, 0)); | |
2469 | } | |
2470 | ||
2471 | softerr = recv_incremental_replication(hdl, tofs, flags, | |
2472 | stream_nv, stream_avl, renamed); | |
2473 | ||
2474 | /* Unmount renamed filesystems before receiving. */ | |
2475 | while ((pair = nvlist_next_nvpair(renamed, | |
2476 | pair)) != NULL) { | |
2477 | zfs_handle_t *zhp; | |
2478 | prop_changelist_t *clp = NULL; | |
2479 | ||
2480 | zhp = zfs_open(hdl, nvpair_name(pair), | |
2481 | ZFS_TYPE_FILESYSTEM); | |
2482 | if (zhp != NULL) { | |
2483 | clp = changelist_gather(zhp, | |
2484 | ZFS_PROP_MOUNTPOINT, 0, 0); | |
2485 | zfs_close(zhp); | |
2486 | if (clp != NULL) { | |
2487 | softerr |= | |
2488 | changelist_prefix(clp); | |
2489 | changelist_free(clp); | |
2490 | } | |
2491 | } | |
2492 | } | |
2493 | ||
2494 | nvlist_free(renamed); | |
34dc7c2f BB |
2495 | } |
2496 | } | |
2497 | ||
428870ff BB |
2498 | /* |
2499 | * Get the fs specified by the first path in the stream (the top level | |
2500 | * specified by 'zfs send') and pass it to each invocation of | |
2501 | * zfs_receive_one(). | |
2502 | */ | |
2503 | (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname, | |
2504 | ZFS_MAXNAMELEN); | |
2505 | if ((cp = strchr(sendfs, '@')) != NULL) | |
2506 | *cp = '\0'; | |
34dc7c2f BB |
2507 | |
2508 | /* Finally, receive each contained stream */ | |
2509 | do { | |
2510 | /* | |
2511 | * we should figure out if it has a recoverable | |
2512 | * error, in which case do a recv_skip() and drive on. | |
2513 | * Note, if we fail due to already having this guid, | |
2514 | * zfs_receive_one() will take care of it (ie, | |
2515 | * recv_skip() and return 0). | |
2516 | */ | |
b128c09f | 2517 | error = zfs_receive_impl(hdl, destname, flags, fd, |
572e2857 BB |
2518 | sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd, |
2519 | action_handlep); | |
34dc7c2f BB |
2520 | if (error == ENODATA) { |
2521 | error = 0; | |
2522 | break; | |
2523 | } | |
2524 | anyerr |= error; | |
2525 | } while (error == 0); | |
2526 | ||
2527 | if (drr->drr_payloadlen != 0 && fromsnap != NULL) { | |
2528 | /* | |
2529 | * Now that we have the fs's they sent us, try the | |
2530 | * renames again. | |
2531 | */ | |
2532 | softerr = recv_incremental_replication(hdl, tofs, flags, | |
428870ff | 2533 | stream_nv, stream_avl, NULL); |
34dc7c2f BB |
2534 | } |
2535 | ||
2536 | out: | |
2537 | fsavl_destroy(stream_avl); | |
2538 | if (stream_nv) | |
2539 | nvlist_free(stream_nv); | |
2540 | if (softerr) | |
2541 | error = -2; | |
2542 | if (anyerr) | |
2543 | error = -1; | |
2544 | return (error); | |
2545 | } | |
2546 | ||
428870ff BB |
2547 | static void |
2548 | trunc_prop_errs(int truncated) | |
2549 | { | |
2550 | ASSERT(truncated != 0); | |
2551 | ||
2552 | if (truncated == 1) | |
2553 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
2554 | "1 more property could not be set\n")); | |
2555 | else | |
2556 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, | |
2557 | "%d more properties could not be set\n"), truncated); | |
2558 | } | |
2559 | ||
34dc7c2f BB |
2560 | static int |
2561 | recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap) | |
2562 | { | |
2563 | dmu_replay_record_t *drr; | |
2564 | void *buf = malloc(1<<20); | |
428870ff BB |
2565 | char errbuf[1024]; |
2566 | ||
2567 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2568 | "cannot receive:")); | |
34dc7c2f BB |
2569 | |
2570 | /* XXX would be great to use lseek if possible... */ | |
2571 | drr = buf; | |
2572 | ||
2573 | while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t), | |
2574 | byteswap, NULL) == 0) { | |
2575 | if (byteswap) | |
2576 | drr->drr_type = BSWAP_32(drr->drr_type); | |
2577 | ||
2578 | switch (drr->drr_type) { | |
2579 | case DRR_BEGIN: | |
2580 | /* NB: not to be used on v2 stream packages */ | |
428870ff BB |
2581 | if (drr->drr_payloadlen != 0) { |
2582 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2583 | "invalid substream header")); | |
2584 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
2585 | } | |
34dc7c2f BB |
2586 | break; |
2587 | ||
2588 | case DRR_END: | |
2589 | free(buf); | |
2590 | return (0); | |
2591 | ||
2592 | case DRR_OBJECT: | |
2593 | if (byteswap) { | |
2594 | drr->drr_u.drr_object.drr_bonuslen = | |
2595 | BSWAP_32(drr->drr_u.drr_object. | |
2596 | drr_bonuslen); | |
2597 | } | |
2598 | (void) recv_read(hdl, fd, buf, | |
2599 | P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8), | |
2600 | B_FALSE, NULL); | |
2601 | break; | |
2602 | ||
2603 | case DRR_WRITE: | |
2604 | if (byteswap) { | |
2605 | drr->drr_u.drr_write.drr_length = | |
b128c09f | 2606 | BSWAP_64(drr->drr_u.drr_write.drr_length); |
34dc7c2f BB |
2607 | } |
2608 | (void) recv_read(hdl, fd, buf, | |
2609 | drr->drr_u.drr_write.drr_length, B_FALSE, NULL); | |
2610 | break; | |
428870ff BB |
2611 | case DRR_SPILL: |
2612 | if (byteswap) { | |
2613 | drr->drr_u.drr_write.drr_length = | |
2614 | BSWAP_64(drr->drr_u.drr_spill.drr_length); | |
2615 | } | |
2616 | (void) recv_read(hdl, fd, buf, | |
2617 | drr->drr_u.drr_spill.drr_length, B_FALSE, NULL); | |
2618 | break; | |
ea04106b AX |
2619 | case DRR_WRITE_EMBEDDED: |
2620 | if (byteswap) { | |
2621 | drr->drr_u.drr_write_embedded.drr_psize = | |
2622 | BSWAP_32(drr->drr_u.drr_write_embedded. | |
2623 | drr_psize); | |
2624 | } | |
2625 | (void) recv_read(hdl, fd, buf, | |
2626 | P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize, | |
2627 | 8), B_FALSE, NULL); | |
2628 | break; | |
428870ff | 2629 | case DRR_WRITE_BYREF: |
34dc7c2f BB |
2630 | case DRR_FREEOBJECTS: |
2631 | case DRR_FREE: | |
2632 | break; | |
2633 | ||
2634 | default: | |
428870ff BB |
2635 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2636 | "invalid record type")); | |
2637 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
34dc7c2f BB |
2638 | } |
2639 | } | |
2640 | ||
2641 | free(buf); | |
2642 | return (-1); | |
2643 | } | |
2644 | ||
2645 | /* | |
2646 | * Restores a backup of tosnap from the file descriptor specified by infd. | |
2647 | */ | |
2648 | static int | |
2649 | zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap, | |
330d06f9 | 2650 | recvflags_t *flags, dmu_replay_record_t *drr, |
428870ff | 2651 | dmu_replay_record_t *drr_noswap, const char *sendfs, |
572e2857 BB |
2652 | nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd, |
2653 | uint64_t *action_handlep) | |
34dc7c2f | 2654 | { |
a08ee875 | 2655 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f | 2656 | time_t begin_time; |
428870ff | 2657 | int ioctl_err, ioctl_errno, err; |
34dc7c2f BB |
2658 | char *cp; |
2659 | struct drr_begin *drrb = &drr->drr_u.drr_begin; | |
2660 | char errbuf[1024]; | |
428870ff BB |
2661 | char prop_errbuf[1024]; |
2662 | const char *chopprefix; | |
34dc7c2f BB |
2663 | boolean_t newfs = B_FALSE; |
2664 | boolean_t stream_wantsnewfs; | |
2665 | uint64_t parent_snapguid = 0; | |
2666 | prop_changelist_t *clp = NULL; | |
b128c09f | 2667 | nvlist_t *snapprops_nvlist = NULL; |
428870ff BB |
2668 | zprop_errflags_t prop_errflags; |
2669 | boolean_t recursive; | |
34dc7c2f BB |
2670 | |
2671 | begin_time = time(NULL); | |
2672 | ||
2673 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2674 | "cannot receive")); | |
2675 | ||
428870ff BB |
2676 | recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") == |
2677 | ENOENT); | |
2678 | ||
34dc7c2f | 2679 | if (stream_avl != NULL) { |
b128c09f BB |
2680 | char *snapname; |
2681 | nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, | |
2682 | &snapname); | |
34dc7c2f BB |
2683 | nvlist_t *props; |
2684 | int ret; | |
2685 | ||
2686 | (void) nvlist_lookup_uint64(fs, "parentfromsnap", | |
2687 | &parent_snapguid); | |
2688 | err = nvlist_lookup_nvlist(fs, "props", &props); | |
2689 | if (err) | |
2690 | VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0)); | |
2691 | ||
330d06f9 | 2692 | if (flags->canmountoff) { |
34dc7c2f BB |
2693 | VERIFY(0 == nvlist_add_uint64(props, |
2694 | zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0)); | |
2695 | } | |
2696 | ret = zcmd_write_src_nvlist(hdl, &zc, props); | |
2697 | if (err) | |
2698 | nvlist_free(props); | |
34dc7c2f BB |
2699 | if (ret != 0) |
2700 | return (-1); | |
2701 | } | |
2702 | ||
428870ff BB |
2703 | cp = NULL; |
2704 | ||
34dc7c2f BB |
2705 | /* |
2706 | * Determine how much of the snapshot name stored in the stream | |
2707 | * we are going to tack on to the name they specified on the | |
2708 | * command line, and how much we are going to chop off. | |
2709 | * | |
2710 | * If they specified a snapshot, chop the entire name stored in | |
2711 | * the stream. | |
2712 | */ | |
330d06f9 | 2713 | if (flags->istail) { |
428870ff BB |
2714 | /* |
2715 | * A filesystem was specified with -e. We want to tack on only | |
2716 | * the tail of the sent snapshot path. | |
2717 | */ | |
2718 | if (strchr(tosnap, '@')) { | |
2719 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
2720 | "argument - snapshot not allowed with -e")); | |
2721 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
2722 | } | |
2723 | ||
2724 | chopprefix = strrchr(sendfs, '/'); | |
2725 | ||
2726 | if (chopprefix == NULL) { | |
2727 | /* | |
2728 | * The tail is the poolname, so we need to | |
2729 | * prepend a path separator. | |
2730 | */ | |
2731 | int len = strlen(drrb->drr_toname); | |
2732 | cp = malloc(len + 2); | |
2733 | cp[0] = '/'; | |
2734 | (void) strcpy(&cp[1], drrb->drr_toname); | |
2735 | chopprefix = cp; | |
2736 | } else { | |
2737 | chopprefix = drrb->drr_toname + (chopprefix - sendfs); | |
2738 | } | |
330d06f9 | 2739 | } else if (flags->isprefix) { |
34dc7c2f | 2740 | /* |
428870ff BB |
2741 | * A filesystem was specified with -d. We want to tack on |
2742 | * everything but the first element of the sent snapshot path | |
2743 | * (all but the pool name). | |
34dc7c2f BB |
2744 | */ |
2745 | if (strchr(tosnap, '@')) { | |
2746 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
2747 | "argument - snapshot not allowed with -d")); | |
2748 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
2749 | } | |
428870ff BB |
2750 | |
2751 | chopprefix = strchr(drrb->drr_toname, '/'); | |
2752 | if (chopprefix == NULL) | |
2753 | chopprefix = strchr(drrb->drr_toname, '@'); | |
34dc7c2f BB |
2754 | } else if (strchr(tosnap, '@') == NULL) { |
2755 | /* | |
428870ff BB |
2756 | * If a filesystem was specified without -d or -e, we want to |
2757 | * tack on everything after the fs specified by 'zfs send'. | |
34dc7c2f | 2758 | */ |
428870ff BB |
2759 | chopprefix = drrb->drr_toname + strlen(sendfs); |
2760 | } else { | |
2761 | /* A snapshot was specified as an exact path (no -d or -e). */ | |
2762 | if (recursive) { | |
2763 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2764 | "cannot specify snapshot name for multi-snapshot " | |
2765 | "stream")); | |
2766 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
2767 | } | |
2768 | chopprefix = drrb->drr_toname + strlen(drrb->drr_toname); | |
34dc7c2f | 2769 | } |
428870ff BB |
2770 | |
2771 | ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname); | |
2772 | ASSERT(chopprefix > drrb->drr_toname); | |
2773 | ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname)); | |
2774 | ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' || | |
2775 | chopprefix[0] == '\0'); | |
34dc7c2f BB |
2776 | |
2777 | /* | |
2778 | * Determine name of destination snapshot, store in zc_value. | |
2779 | */ | |
2780 | (void) strcpy(zc.zc_value, tosnap); | |
2a442d16 | 2781 | (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value)); |
428870ff | 2782 | free(cp); |
34dc7c2f BB |
2783 | if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) { |
2784 | zcmd_free_nvlists(&zc); | |
2785 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
2786 | } | |
2787 | ||
2788 | /* | |
2789 | * Determine the name of the origin snapshot, store in zc_string. | |
2790 | */ | |
2791 | if (drrb->drr_flags & DRR_FLAG_CLONE) { | |
330d06f9 | 2792 | if (guid_to_name(hdl, zc.zc_value, |
34dc7c2f BB |
2793 | drrb->drr_fromguid, zc.zc_string) != 0) { |
2794 | zcmd_free_nvlists(&zc); | |
2795 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2796 | "local origin for clone %s does not exist"), | |
2797 | zc.zc_value); | |
2798 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
2799 | } | |
330d06f9 | 2800 | if (flags->verbose) |
34dc7c2f BB |
2801 | (void) printf("found clone origin %s\n", zc.zc_string); |
2802 | } | |
2803 | ||
b8864a23 | 2804 | stream_wantsnewfs = (drrb->drr_fromguid == 0 || |
34dc7c2f BB |
2805 | (drrb->drr_flags & DRR_FLAG_CLONE)); |
2806 | ||
2807 | if (stream_wantsnewfs) { | |
2808 | /* | |
2809 | * if the parent fs does not exist, look for it based on | |
2810 | * the parent snap GUID | |
2811 | */ | |
2812 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2813 | "cannot receive new filesystem stream")); | |
2814 | ||
2815 | (void) strcpy(zc.zc_name, zc.zc_value); | |
2816 | cp = strrchr(zc.zc_name, '/'); | |
2817 | if (cp) | |
2818 | *cp = '\0'; | |
2819 | if (cp && | |
2820 | !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { | |
2821 | char suffix[ZFS_MAXNAMELEN]; | |
2822 | (void) strcpy(suffix, strrchr(zc.zc_value, '/')); | |
330d06f9 | 2823 | if (guid_to_name(hdl, zc.zc_name, parent_snapguid, |
34dc7c2f BB |
2824 | zc.zc_value) == 0) { |
2825 | *strchr(zc.zc_value, '@') = '\0'; | |
2826 | (void) strcat(zc.zc_value, suffix); | |
2827 | } | |
2828 | } | |
2829 | } else { | |
2830 | /* | |
2831 | * if the fs does not exist, look for it based on the | |
2832 | * fromsnap GUID | |
2833 | */ | |
2834 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2835 | "cannot receive incremental stream")); | |
2836 | ||
2837 | (void) strcpy(zc.zc_name, zc.zc_value); | |
2838 | *strchr(zc.zc_name, '@') = '\0'; | |
2839 | ||
428870ff BB |
2840 | /* |
2841 | * If the exact receive path was specified and this is the | |
2842 | * topmost path in the stream, then if the fs does not exist we | |
2843 | * should look no further. | |
2844 | */ | |
330d06f9 | 2845 | if ((flags->isprefix || (*(chopprefix = drrb->drr_toname + |
428870ff BB |
2846 | strlen(sendfs)) != '\0' && *chopprefix != '@')) && |
2847 | !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { | |
34dc7c2f BB |
2848 | char snap[ZFS_MAXNAMELEN]; |
2849 | (void) strcpy(snap, strchr(zc.zc_value, '@')); | |
330d06f9 | 2850 | if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid, |
34dc7c2f BB |
2851 | zc.zc_value) == 0) { |
2852 | *strchr(zc.zc_value, '@') = '\0'; | |
2853 | (void) strcat(zc.zc_value, snap); | |
2854 | } | |
2855 | } | |
2856 | } | |
2857 | ||
2858 | (void) strcpy(zc.zc_name, zc.zc_value); | |
2859 | *strchr(zc.zc_name, '@') = '\0'; | |
2860 | ||
2861 | if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { | |
2862 | zfs_handle_t *zhp; | |
428870ff | 2863 | |
34dc7c2f BB |
2864 | /* |
2865 | * Destination fs exists. Therefore this should either | |
2866 | * be an incremental, or the stream specifies a new fs | |
2867 | * (full stream or clone) and they want us to blow it | |
2868 | * away (and have therefore specified -F and removed any | |
2869 | * snapshots). | |
2870 | */ | |
34dc7c2f | 2871 | if (stream_wantsnewfs) { |
330d06f9 | 2872 | if (!flags->force) { |
34dc7c2f BB |
2873 | zcmd_free_nvlists(&zc); |
2874 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2875 | "destination '%s' exists\n" | |
2876 | "must specify -F to overwrite it"), | |
2877 | zc.zc_name); | |
2878 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
2879 | } | |
2880 | if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, | |
2881 | &zc) == 0) { | |
2882 | zcmd_free_nvlists(&zc); | |
2883 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2884 | "destination has snapshots (eg. %s)\n" | |
2885 | "must destroy them to overwrite it"), | |
2886 | zc.zc_name); | |
2887 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
2888 | } | |
2889 | } | |
2890 | ||
2891 | if ((zhp = zfs_open(hdl, zc.zc_name, | |
2892 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) { | |
2893 | zcmd_free_nvlists(&zc); | |
2894 | return (-1); | |
2895 | } | |
2896 | ||
2897 | if (stream_wantsnewfs && | |
2898 | zhp->zfs_dmustats.dds_origin[0]) { | |
2899 | zcmd_free_nvlists(&zc); | |
2900 | zfs_close(zhp); | |
2901 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2902 | "destination '%s' is a clone\n" | |
2903 | "must destroy it to overwrite it"), | |
2904 | zc.zc_name); | |
2905 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
2906 | } | |
2907 | ||
330d06f9 | 2908 | if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM && |
34dc7c2f BB |
2909 | stream_wantsnewfs) { |
2910 | /* We can't do online recv in this case */ | |
b128c09f | 2911 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0); |
34dc7c2f | 2912 | if (clp == NULL) { |
45d1cae3 | 2913 | zfs_close(zhp); |
34dc7c2f BB |
2914 | zcmd_free_nvlists(&zc); |
2915 | return (-1); | |
2916 | } | |
2917 | if (changelist_prefix(clp) != 0) { | |
2918 | changelist_free(clp); | |
45d1cae3 | 2919 | zfs_close(zhp); |
34dc7c2f BB |
2920 | zcmd_free_nvlists(&zc); |
2921 | return (-1); | |
2922 | } | |
2923 | } | |
34dc7c2f BB |
2924 | zfs_close(zhp); |
2925 | } else { | |
2926 | /* | |
2927 | * Destination filesystem does not exist. Therefore we better | |
2928 | * be creating a new filesystem (either from a full backup, or | |
2929 | * a clone). It would therefore be invalid if the user | |
2930 | * specified only the pool name (i.e. if the destination name | |
2931 | * contained no slash character). | |
2932 | */ | |
2933 | if (!stream_wantsnewfs || | |
2934 | (cp = strrchr(zc.zc_name, '/')) == NULL) { | |
2935 | zcmd_free_nvlists(&zc); | |
2936 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2937 | "destination '%s' does not exist"), zc.zc_name); | |
2938 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
2939 | } | |
2940 | ||
2941 | /* | |
2942 | * Trim off the final dataset component so we perform the | |
2943 | * recvbackup ioctl to the filesystems's parent. | |
2944 | */ | |
2945 | *cp = '\0'; | |
2946 | ||
330d06f9 | 2947 | if (flags->isprefix && !flags->istail && !flags->dryrun && |
34dc7c2f BB |
2948 | create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) { |
2949 | zcmd_free_nvlists(&zc); | |
2950 | return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); | |
2951 | } | |
2952 | ||
2953 | newfs = B_TRUE; | |
2954 | } | |
2955 | ||
2956 | zc.zc_begin_record = drr_noswap->drr_u.drr_begin; | |
2957 | zc.zc_cookie = infd; | |
330d06f9 MA |
2958 | zc.zc_guid = flags->force; |
2959 | if (flags->verbose) { | |
34dc7c2f | 2960 | (void) printf("%s %s stream of %s into %s\n", |
330d06f9 | 2961 | flags->dryrun ? "would receive" : "receiving", |
34dc7c2f BB |
2962 | drrb->drr_fromguid ? "incremental" : "full", |
2963 | drrb->drr_toname, zc.zc_value); | |
2964 | (void) fflush(stdout); | |
2965 | } | |
2966 | ||
330d06f9 | 2967 | if (flags->dryrun) { |
34dc7c2f | 2968 | zcmd_free_nvlists(&zc); |
330d06f9 | 2969 | return (recv_skip(hdl, infd, flags->byteswap)); |
34dc7c2f BB |
2970 | } |
2971 | ||
428870ff BB |
2972 | zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf; |
2973 | zc.zc_nvlist_dst_size = sizeof (prop_errbuf); | |
572e2857 BB |
2974 | zc.zc_cleanup_fd = cleanup_fd; |
2975 | zc.zc_action_handle = *action_handlep; | |
428870ff | 2976 | |
34dc7c2f BB |
2977 | err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc); |
2978 | ioctl_errno = errno; | |
428870ff BB |
2979 | prop_errflags = (zprop_errflags_t)zc.zc_obj; |
2980 | ||
2981 | if (err == 0) { | |
2982 | nvlist_t *prop_errors; | |
2983 | VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst, | |
2984 | zc.zc_nvlist_dst_size, &prop_errors, 0)); | |
2985 | ||
2986 | nvpair_t *prop_err = NULL; | |
2987 | ||
2988 | while ((prop_err = nvlist_next_nvpair(prop_errors, | |
2989 | prop_err)) != NULL) { | |
2990 | char tbuf[1024]; | |
2991 | zfs_prop_t prop; | |
2992 | int intval; | |
2993 | ||
2994 | prop = zfs_name_to_prop(nvpair_name(prop_err)); | |
2995 | (void) nvpair_value_int32(prop_err, &intval); | |
2996 | if (strcmp(nvpair_name(prop_err), | |
2997 | ZPROP_N_MORE_ERRORS) == 0) { | |
2998 | trunc_prop_errs(intval); | |
2999 | break; | |
3000 | } else { | |
3001 | (void) snprintf(tbuf, sizeof (tbuf), | |
3002 | dgettext(TEXT_DOMAIN, | |
3003 | "cannot receive %s property on %s"), | |
3004 | nvpair_name(prop_err), zc.zc_name); | |
3005 | zfs_setprop_error(hdl, prop, intval, tbuf); | |
3006 | } | |
3007 | } | |
3008 | nvlist_free(prop_errors); | |
3009 | } | |
3010 | ||
3011 | zc.zc_nvlist_dst = 0; | |
3012 | zc.zc_nvlist_dst_size = 0; | |
b128c09f BB |
3013 | zcmd_free_nvlists(&zc); |
3014 | ||
3015 | if (err == 0 && snapprops_nvlist) { | |
a08ee875 | 3016 | zfs_cmd_t zc2 = {"\0"}; |
b128c09f BB |
3017 | |
3018 | (void) strcpy(zc2.zc_name, zc.zc_value); | |
428870ff | 3019 | zc2.zc_cookie = B_TRUE; /* received */ |
b128c09f BB |
3020 | if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) { |
3021 | (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2); | |
3022 | zcmd_free_nvlists(&zc2); | |
3023 | } | |
3024 | } | |
3025 | ||
428870ff | 3026 | if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) { |
34dc7c2f BB |
3027 | /* |
3028 | * It may be that this snapshot already exists, | |
3029 | * in which case we want to consume & ignore it | |
3030 | * rather than failing. | |
3031 | */ | |
3032 | avl_tree_t *local_avl; | |
3033 | nvlist_t *local_nv, *fs; | |
428870ff | 3034 | cp = strchr(zc.zc_value, '@'); |
34dc7c2f BB |
3035 | |
3036 | /* | |
3037 | * XXX Do this faster by just iterating over snaps in | |
3038 | * this fs. Also if zc_value does not exist, we will | |
3039 | * get a strange "does not exist" error message. | |
3040 | */ | |
3041 | *cp = '\0'; | |
428870ff | 3042 | if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE, |
34dc7c2f BB |
3043 | &local_nv, &local_avl) == 0) { |
3044 | *cp = '@'; | |
3045 | fs = fsavl_find(local_avl, drrb->drr_toguid, NULL); | |
3046 | fsavl_destroy(local_avl); | |
3047 | nvlist_free(local_nv); | |
3048 | ||
3049 | if (fs != NULL) { | |
330d06f9 | 3050 | if (flags->verbose) { |
34dc7c2f BB |
3051 | (void) printf("snap %s already exists; " |
3052 | "ignoring\n", zc.zc_value); | |
3053 | } | |
428870ff | 3054 | err = ioctl_err = recv_skip(hdl, infd, |
330d06f9 | 3055 | flags->byteswap); |
34dc7c2f BB |
3056 | } |
3057 | } | |
3058 | *cp = '@'; | |
3059 | } | |
3060 | ||
34dc7c2f BB |
3061 | if (ioctl_err != 0) { |
3062 | switch (ioctl_errno) { | |
3063 | case ENODEV: | |
3064 | cp = strchr(zc.zc_value, '@'); | |
3065 | *cp = '\0'; | |
3066 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3067 | "most recent snapshot of %s does not\n" | |
3068 | "match incremental source"), zc.zc_value); | |
3069 | (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3070 | *cp = '@'; | |
3071 | break; | |
3072 | case ETXTBSY: | |
3073 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3074 | "destination %s has been modified\n" | |
3075 | "since most recent snapshot"), zc.zc_name); | |
3076 | (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); | |
3077 | break; | |
3078 | case EEXIST: | |
3079 | cp = strchr(zc.zc_value, '@'); | |
3080 | if (newfs) { | |
3081 | /* it's the containing fs that exists */ | |
3082 | *cp = '\0'; | |
3083 | } | |
3084 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3085 | "destination already exists")); | |
3086 | (void) zfs_error_fmt(hdl, EZFS_EXISTS, | |
3087 | dgettext(TEXT_DOMAIN, "cannot restore to %s"), | |
3088 | zc.zc_value); | |
3089 | *cp = '@'; | |
3090 | break; | |
3091 | case EINVAL: | |
3092 | (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3093 | break; | |
3094 | case ECKSUM: | |
3095 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3096 | "invalid stream (checksum mismatch)")); | |
3097 | (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); | |
3098 | break; | |
428870ff BB |
3099 | case ENOTSUP: |
3100 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3101 | "pool must be upgraded to receive this stream.")); | |
3102 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
3103 | break; | |
3104 | case EDQUOT: | |
3105 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3106 | "destination %s space quota exceeded"), zc.zc_name); | |
330d06f9 | 3107 | (void) zfs_error(hdl, EZFS_NOSPC, errbuf); |
428870ff | 3108 | break; |
34dc7c2f BB |
3109 | default: |
3110 | (void) zfs_standard_error(hdl, ioctl_errno, errbuf); | |
3111 | } | |
3112 | } | |
3113 | ||
3114 | /* | |
428870ff BB |
3115 | * Mount the target filesystem (if created). Also mount any |
3116 | * children of the target filesystem if we did a replication | |
3117 | * receive (indicated by stream_avl being non-NULL). | |
34dc7c2f BB |
3118 | */ |
3119 | cp = strchr(zc.zc_value, '@'); | |
3120 | if (cp && (ioctl_err == 0 || !newfs)) { | |
3121 | zfs_handle_t *h; | |
3122 | ||
3123 | *cp = '\0'; | |
3124 | h = zfs_open(hdl, zc.zc_value, | |
3125 | ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); | |
34dc7c2f BB |
3126 | if (h != NULL) { |
3127 | if (h->zfs_type == ZFS_TYPE_VOLUME) { | |
b128c09f | 3128 | *cp = '@'; |
45d1cae3 | 3129 | } else if (newfs || stream_avl) { |
b128c09f BB |
3130 | /* |
3131 | * Track the first/top of hierarchy fs, | |
3132 | * for mounting and sharing later. | |
3133 | */ | |
3134 | if (top_zfs && *top_zfs == NULL) | |
3135 | *top_zfs = zfs_strdup(hdl, zc.zc_value); | |
34dc7c2f BB |
3136 | } |
3137 | zfs_close(h); | |
3138 | } | |
b128c09f | 3139 | *cp = '@'; |
34dc7c2f BB |
3140 | } |
3141 | ||
3142 | if (clp) { | |
3143 | err |= changelist_postfix(clp); | |
3144 | changelist_free(clp); | |
3145 | } | |
3146 | ||
428870ff BB |
3147 | if (prop_errflags & ZPROP_ERR_NOCLEAR) { |
3148 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " | |
3149 | "failed to clear unreceived properties on %s"), | |
3150 | zc.zc_name); | |
3151 | (void) fprintf(stderr, "\n"); | |
3152 | } | |
3153 | if (prop_errflags & ZPROP_ERR_NORESTORE) { | |
3154 | (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: " | |
3155 | "failed to restore original properties on %s"), | |
3156 | zc.zc_name); | |
3157 | (void) fprintf(stderr, "\n"); | |
3158 | } | |
3159 | ||
34dc7c2f BB |
3160 | if (err || ioctl_err) |
3161 | return (-1); | |
3162 | ||
572e2857 BB |
3163 | *action_handlep = zc.zc_action_handle; |
3164 | ||
330d06f9 | 3165 | if (flags->verbose) { |
34dc7c2f BB |
3166 | char buf1[64]; |
3167 | char buf2[64]; | |
3168 | uint64_t bytes = zc.zc_cookie; | |
3169 | time_t delta = time(NULL) - begin_time; | |
3170 | if (delta == 0) | |
3171 | delta = 1; | |
3172 | zfs_nicenum(bytes, buf1, sizeof (buf1)); | |
3173 | zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); | |
3174 | ||
3175 | (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", | |
3176 | buf1, delta, buf2); | |
3177 | } | |
3178 | ||
3179 | return (0); | |
3180 | } | |
3181 | ||
b128c09f | 3182 | static int |
330d06f9 | 3183 | zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags, |
428870ff | 3184 | int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl, |
572e2857 | 3185 | char **top_zfs, int cleanup_fd, uint64_t *action_handlep) |
34dc7c2f BB |
3186 | { |
3187 | int err; | |
3188 | dmu_replay_record_t drr, drr_noswap; | |
3189 | struct drr_begin *drrb = &drr.drr_u.drr_begin; | |
3190 | char errbuf[1024]; | |
2598c001 | 3191 | zio_cksum_t zcksum = { { 0 } }; |
428870ff BB |
3192 | uint64_t featureflags; |
3193 | int hdrtype; | |
34dc7c2f BB |
3194 | |
3195 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3196 | "cannot receive")); | |
3197 | ||
330d06f9 | 3198 | if (flags->isprefix && |
34dc7c2f BB |
3199 | !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) { |
3200 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs " | |
3201 | "(%s) does not exist"), tosnap); | |
3202 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
3203 | } | |
3204 | ||
3205 | /* read in the BEGIN record */ | |
3206 | if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE, | |
3207 | &zcksum))) | |
3208 | return (err); | |
3209 | ||
3210 | if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) { | |
3211 | /* It's the double end record at the end of a package */ | |
3212 | return (ENODATA); | |
3213 | } | |
3214 | ||
3215 | /* the kernel needs the non-byteswapped begin record */ | |
3216 | drr_noswap = drr; | |
3217 | ||
330d06f9 | 3218 | flags->byteswap = B_FALSE; |
34dc7c2f BB |
3219 | if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { |
3220 | /* | |
3221 | * We computed the checksum in the wrong byteorder in | |
3222 | * recv_read() above; do it again correctly. | |
3223 | */ | |
3224 | bzero(&zcksum, sizeof (zio_cksum_t)); | |
3225 | fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum); | |
330d06f9 | 3226 | flags->byteswap = B_TRUE; |
34dc7c2f BB |
3227 | |
3228 | drr.drr_type = BSWAP_32(drr.drr_type); | |
3229 | drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen); | |
3230 | drrb->drr_magic = BSWAP_64(drrb->drr_magic); | |
428870ff | 3231 | drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); |
34dc7c2f BB |
3232 | drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); |
3233 | drrb->drr_type = BSWAP_32(drrb->drr_type); | |
3234 | drrb->drr_flags = BSWAP_32(drrb->drr_flags); | |
3235 | drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); | |
3236 | drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); | |
3237 | } | |
3238 | ||
3239 | if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) { | |
3240 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
3241 | "stream (bad magic number)")); | |
3242 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
3243 | } | |
3244 | ||
428870ff BB |
3245 | featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); |
3246 | hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo); | |
3247 | ||
3248 | if (!DMU_STREAM_SUPPORTED(featureflags) || | |
3249 | (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) { | |
3250 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3251 | "stream has unsupported feature, feature flags = %lx"), | |
3252 | featureflags); | |
3253 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
3254 | } | |
3255 | ||
34dc7c2f BB |
3256 | if (strchr(drrb->drr_toname, '@') == NULL) { |
3257 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " | |
3258 | "stream (bad snapshot name)")); | |
3259 | return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); | |
3260 | } | |
3261 | ||
428870ff BB |
3262 | if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) { |
3263 | char nonpackage_sendfs[ZFS_MAXNAMELEN]; | |
3264 | if (sendfs == NULL) { | |
3265 | /* | |
3266 | * We were not called from zfs_receive_package(). Get | |
3267 | * the fs specified by 'zfs send'. | |
3268 | */ | |
3269 | char *cp; | |
3270 | (void) strlcpy(nonpackage_sendfs, | |
3271 | drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN); | |
3272 | if ((cp = strchr(nonpackage_sendfs, '@')) != NULL) | |
3273 | *cp = '\0'; | |
3274 | sendfs = nonpackage_sendfs; | |
3275 | } | |
34dc7c2f | 3276 | return (zfs_receive_one(hdl, infd, tosnap, flags, |
428870ff | 3277 | &drr, &drr_noswap, sendfs, stream_nv, stream_avl, |
572e2857 | 3278 | top_zfs, cleanup_fd, action_handlep)); |
428870ff BB |
3279 | } else { |
3280 | assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == | |
3281 | DMU_COMPOUNDSTREAM); | |
34dc7c2f | 3282 | return (zfs_receive_package(hdl, infd, tosnap, flags, |
572e2857 | 3283 | &drr, &zcksum, top_zfs, cleanup_fd, action_handlep)); |
34dc7c2f BB |
3284 | } |
3285 | } | |
b128c09f BB |
3286 | |
3287 | /* | |
3288 | * Restores a backup of tosnap from the file descriptor specified by infd. | |
3289 | * Return 0 on total success, -2 if some things couldn't be | |
3290 | * destroyed/renamed/promoted, -1 if some things couldn't be received. | |
3291 | * (-1 will override -2). | |
3292 | */ | |
3293 | int | |
330d06f9 | 3294 | zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags, |
b128c09f BB |
3295 | int infd, avl_tree_t *stream_avl) |
3296 | { | |
3297 | char *top_zfs = NULL; | |
3298 | int err; | |
572e2857 BB |
3299 | int cleanup_fd; |
3300 | uint64_t action_handle = 0; | |
ea04106b AX |
3301 | struct stat sb; |
3302 | ||
3303 | /* | |
3304 | * The only way fstat can fail is if we do not have a valid file | |
3305 | * descriptor. | |
3306 | */ | |
3307 | if (fstat(infd, &sb) == -1) { | |
3308 | perror("fstat"); | |
3309 | return (-2); | |
3310 | } | |
3311 | ||
3312 | #ifdef __linux__ | |
3313 | #ifndef F_SETPIPE_SZ | |
3314 | #define F_SETPIPE_SZ (F_SETLEASE + 7) | |
3315 | #endif /* F_SETPIPE_SZ */ | |
3316 | ||
3317 | #ifndef F_GETPIPE_SZ | |
3318 | #define F_GETPIPE_SZ (F_GETLEASE + 7) | |
3319 | #endif /* F_GETPIPE_SZ */ | |
3320 | ||
3321 | /* | |
3322 | * It is not uncommon for gigabytes to be processed in zfs receive. | |
3323 | * Speculatively increase the buffer size via Linux-specific fcntl() | |
3324 | * call. | |
3325 | */ | |
3326 | if (S_ISFIFO(sb.st_mode)) { | |
3327 | FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r"); | |
3328 | ||
3329 | if (procf != NULL) { | |
3330 | unsigned long max_psize; | |
3331 | long cur_psize; | |
3332 | if (fscanf(procf, "%lu", &max_psize) > 0) { | |
3333 | cur_psize = fcntl(infd, F_GETPIPE_SZ); | |
3334 | if (cur_psize > 0 && | |
3335 | max_psize > (unsigned long) cur_psize) | |
3336 | (void) fcntl(infd, F_SETPIPE_SZ, | |
3337 | max_psize); | |
3338 | } | |
3339 | fclose(procf); | |
3340 | } | |
3341 | } | |
3342 | #endif /* __linux__ */ | |
572e2857 | 3343 | |
325f0235 | 3344 | cleanup_fd = open(ZFS_DEV, O_RDWR); |
572e2857 | 3345 | VERIFY(cleanup_fd >= 0); |
b128c09f | 3346 | |
428870ff | 3347 | err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL, |
572e2857 BB |
3348 | stream_avl, &top_zfs, cleanup_fd, &action_handle); |
3349 | ||
3350 | VERIFY(0 == close(cleanup_fd)); | |
b128c09f | 3351 | |
330d06f9 | 3352 | if (err == 0 && !flags->nomount && top_zfs) { |
b128c09f BB |
3353 | zfs_handle_t *zhp; |
3354 | prop_changelist_t *clp; | |
3355 | ||
3356 | zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM); | |
3357 | if (zhp != NULL) { | |
3358 | clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, | |
3359 | CL_GATHER_MOUNT_ALWAYS, 0); | |
3360 | zfs_close(zhp); | |
3361 | if (clp != NULL) { | |
3362 | /* mount and share received datasets */ | |
3363 | err = changelist_postfix(clp); | |
3364 | changelist_free(clp); | |
3365 | } | |
3366 | } | |
3367 | if (zhp == NULL || clp == NULL || err) | |
3368 | err = -1; | |
3369 | } | |
3370 | if (top_zfs) | |
3371 | free(top_zfs); | |
3372 | ||
3373 | return (err); | |
3374 | } |