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