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