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