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