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