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