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