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