Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/dmu_send.c
+++ new/usr/src/uts/common/fs/zfs/dmu_send.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26 26 * Copyright 2014 HybridCluster. All rights reserved.
27 27 * Copyright 2016 RackTop Systems.
28 28 * Copyright (c) 2014 Integros [integros.com]
29 29 */
30 30
31 31 #include <sys/dmu.h>
32 32 #include <sys/dmu_impl.h>
33 33 #include <sys/dmu_tx.h>
34 34 #include <sys/dbuf.h>
35 35 #include <sys/dnode.h>
36 36 #include <sys/zfs_context.h>
37 37 #include <sys/dmu_objset.h>
38 38 #include <sys/dmu_traverse.h>
39 39 #include <sys/dsl_dataset.h>
40 40 #include <sys/dsl_dir.h>
41 41 #include <sys/dsl_prop.h>
42 42 #include <sys/dsl_pool.h>
43 43 #include <sys/dsl_synctask.h>
44 44 #include <sys/zfs_ioctl.h>
45 45 #include <sys/zap.h>
46 46 #include <sys/zio_checksum.h>
47 47 #include <sys/zfs_znode.h>
48 48 #include <zfs_fletcher.h>
49 49 #include <sys/avl.h>
50 50 #include <sys/ddt.h>
51 51 #include <sys/zfs_onexit.h>
52 52 #include <sys/dmu_send.h>
53 53 #include <sys/dsl_destroy.h>
54 54 #include <sys/blkptr.h>
55 55 #include <sys/dsl_bookmark.h>
56 56 #include <sys/zfeature.h>
57 57 #include <sys/bqueue.h>
58 58
59 59 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
60 60 int zfs_send_corrupt_data = B_FALSE;
61 61 int zfs_send_queue_length = 16 * 1024 * 1024;
62 62 int zfs_recv_queue_length = 16 * 1024 * 1024;
63 63 /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
64 64 int zfs_send_set_freerecords_bit = B_TRUE;
65 65
66 66 static char *dmu_recv_tag = "dmu_recv_tag";
67 67 const char *recv_clone_name = "%recv";
68 68
69 69 #define BP_SPAN(datablkszsec, indblkshift, level) \
70 70 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
71 71 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
72 72
73 73 static void byteswap_record(dmu_replay_record_t *drr);
74 74
75 75 struct send_thread_arg {
76 76 bqueue_t q;
77 77 dsl_dataset_t *ds; /* Dataset to traverse */
78 78 uint64_t fromtxg; /* Traverse from this txg */
79 79 int flags; /* flags to pass to traverse_dataset */
80 80 int error_code;
81 81 boolean_t cancel;
82 82 zbookmark_phys_t resume;
83 83 };
84 84
85 85 struct send_block_record {
86 86 boolean_t eos_marker; /* Marks the end of the stream */
87 87 blkptr_t bp;
88 88 zbookmark_phys_t zb;
89 89 uint8_t indblkshift;
90 90 uint16_t datablkszsec;
91 91 bqueue_node_t ln;
92 92 };
93 93
94 94 static int
95 95 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
96 96 {
97 97 dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
98 98 ssize_t resid; /* have to get resid to get detailed errno */
99 99
100 100 /*
101 101 * The code does not rely on this (len being a multiple of 8). We keep
102 102 * this assertion because of the corresponding assertion in
103 103 * receive_read(). Keeping this assertion ensures that we do not
104 104 * inadvertently break backwards compatibility (causing the assertion
105 105 * in receive_read() to trigger on old software).
106 106 *
107 107 * Removing the assertions could be rolled into a new feature that uses
108 108 * data that isn't 8-byte aligned; if the assertions were removed, a
109 109 * feature flag would have to be added.
110 110 */
111 111
112 112 ASSERT0(len % 8);
113 113
114 114 dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
115 115 (caddr_t)buf, len,
116 116 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
117 117
118 118 mutex_enter(&ds->ds_sendstream_lock);
119 119 *dsp->dsa_off += len;
120 120 mutex_exit(&ds->ds_sendstream_lock);
121 121
122 122 return (dsp->dsa_err);
123 123 }
124 124
125 125 /*
126 126 * For all record types except BEGIN, fill in the checksum (overlaid in
127 127 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
128 128 * up to the start of the checksum itself.
129 129 */
130 130 static int
131 131 dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
132 132 {
133 133 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
134 134 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
135 135 fletcher_4_incremental_native(dsp->dsa_drr,
136 136 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
137 137 &dsp->dsa_zc);
138 138 if (dsp->dsa_drr->drr_type != DRR_BEGIN) {
139 139 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
140 140 drr_checksum.drr_checksum));
141 141 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
142 142 }
143 143 fletcher_4_incremental_native(&dsp->dsa_drr->
144 144 drr_u.drr_checksum.drr_checksum,
145 145 sizeof (zio_cksum_t), &dsp->dsa_zc);
146 146 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
147 147 return (SET_ERROR(EINTR));
148 148 if (payload_len != 0) {
149 149 fletcher_4_incremental_native(payload, payload_len,
150 150 &dsp->dsa_zc);
151 151 if (dump_bytes(dsp, payload, payload_len) != 0)
152 152 return (SET_ERROR(EINTR));
153 153 }
154 154 return (0);
155 155 }
156 156
157 157 /*
158 158 * Fill in the drr_free struct, or perform aggregation if the previous record is
159 159 * also a free record, and the two are adjacent.
160 160 *
161 161 * Note that we send free records even for a full send, because we want to be
162 162 * able to receive a full send as a clone, which requires a list of all the free
163 163 * and freeobject records that were generated on the source.
164 164 */
165 165 static int
166 166 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
167 167 uint64_t length)
168 168 {
169 169 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
170 170
171 171 /*
172 172 * When we receive a free record, dbuf_free_range() assumes
173 173 * that the receiving system doesn't have any dbufs in the range
174 174 * being freed. This is always true because there is a one-record
175 175 * constraint: we only send one WRITE record for any given
176 176 * object,offset. We know that the one-record constraint is
177 177 * true because we always send data in increasing order by
178 178 * object,offset.
179 179 *
180 180 * If the increasing-order constraint ever changes, we should find
181 181 * another way to assert that the one-record constraint is still
182 182 * satisfied.
183 183 */
184 184 ASSERT(object > dsp->dsa_last_data_object ||
185 185 (object == dsp->dsa_last_data_object &&
186 186 offset > dsp->dsa_last_data_offset));
187 187
188 188 if (length != -1ULL && offset + length < offset)
189 189 length = -1ULL;
190 190
191 191 /*
192 192 * If there is a pending op, but it's not PENDING_FREE, push it out,
193 193 * since free block aggregation can only be done for blocks of the
194 194 * same type (i.e., DRR_FREE records can only be aggregated with
195 195 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
196 196 * aggregated with other DRR_FREEOBJECTS records.
197 197 */
198 198 if (dsp->dsa_pending_op != PENDING_NONE &&
199 199 dsp->dsa_pending_op != PENDING_FREE) {
200 200 if (dump_record(dsp, NULL, 0) != 0)
201 201 return (SET_ERROR(EINTR));
202 202 dsp->dsa_pending_op = PENDING_NONE;
203 203 }
204 204
205 205 if (dsp->dsa_pending_op == PENDING_FREE) {
206 206 /*
207 207 * There should never be a PENDING_FREE if length is -1
208 208 * (because dump_dnode is the only place where this
209 209 * function is called with a -1, and only after flushing
210 210 * any pending record).
211 211 */
212 212 ASSERT(length != -1ULL);
213 213 /*
214 214 * Check to see whether this free block can be aggregated
215 215 * with pending one.
216 216 */
217 217 if (drrf->drr_object == object && drrf->drr_offset +
218 218 drrf->drr_length == offset) {
219 219 drrf->drr_length += length;
220 220 return (0);
221 221 } else {
222 222 /* not a continuation. Push out pending record */
223 223 if (dump_record(dsp, NULL, 0) != 0)
224 224 return (SET_ERROR(EINTR));
225 225 dsp->dsa_pending_op = PENDING_NONE;
226 226 }
227 227 }
228 228 /* create a FREE record and make it pending */
229 229 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
230 230 dsp->dsa_drr->drr_type = DRR_FREE;
231 231 drrf->drr_object = object;
232 232 drrf->drr_offset = offset;
233 233 drrf->drr_length = length;
234 234 drrf->drr_toguid = dsp->dsa_toguid;
235 235 if (length == -1ULL) {
236 236 if (dump_record(dsp, NULL, 0) != 0)
237 237 return (SET_ERROR(EINTR));
238 238 } else {
239 239 dsp->dsa_pending_op = PENDING_FREE;
240 240 }
241 241
242 242 return (0);
243 243 }
244 244
245 245 static int
246 246 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
247 247 uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
248 248 {
249 249 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
250 250
251 251 /*
252 252 * We send data in increasing object, offset order.
253 253 * See comment in dump_free() for details.
254 254 */
255 255 ASSERT(object > dsp->dsa_last_data_object ||
256 256 (object == dsp->dsa_last_data_object &&
257 257 offset > dsp->dsa_last_data_offset));
258 258 dsp->dsa_last_data_object = object;
259 259 dsp->dsa_last_data_offset = offset + blksz - 1;
260 260
261 261 /*
262 262 * If there is any kind of pending aggregation (currently either
263 263 * a grouping of free objects or free blocks), push it out to
264 264 * the stream, since aggregation can't be done across operations
265 265 * of different types.
266 266 */
267 267 if (dsp->dsa_pending_op != PENDING_NONE) {
268 268 if (dump_record(dsp, NULL, 0) != 0)
269 269 return (SET_ERROR(EINTR));
270 270 dsp->dsa_pending_op = PENDING_NONE;
271 271 }
272 272 /* write a WRITE record */
273 273 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
274 274 dsp->dsa_drr->drr_type = DRR_WRITE;
275 275 drrw->drr_object = object;
276 276 drrw->drr_type = type;
277 277 drrw->drr_offset = offset;
278 278 drrw->drr_length = blksz;
279 279 drrw->drr_toguid = dsp->dsa_toguid;
280 280 if (bp == NULL || BP_IS_EMBEDDED(bp)) {
281 281 /*
282 282 * There's no pre-computed checksum for partial-block
283 283 * writes or embedded BP's, so (like
284 284 * fletcher4-checkummed blocks) userland will have to
285 285 * compute a dedup-capable checksum itself.
286 286 */
287 287 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
288 288 } else {
289 289 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
290 290 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
291 291 ZCHECKSUM_FLAG_DEDUP)
292 292 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
293 293 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
294 294 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
295 295 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
296 296 drrw->drr_key.ddk_cksum = bp->blk_cksum;
297 297 }
298 298
299 299 if (dump_record(dsp, data, blksz) != 0)
300 300 return (SET_ERROR(EINTR));
301 301 return (0);
302 302 }
303 303
304 304 static int
305 305 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
306 306 int blksz, const blkptr_t *bp)
307 307 {
308 308 char buf[BPE_PAYLOAD_SIZE];
309 309 struct drr_write_embedded *drrw =
310 310 &(dsp->dsa_drr->drr_u.drr_write_embedded);
311 311
312 312 if (dsp->dsa_pending_op != PENDING_NONE) {
313 313 if (dump_record(dsp, NULL, 0) != 0)
314 314 return (EINTR);
315 315 dsp->dsa_pending_op = PENDING_NONE;
316 316 }
317 317
318 318 ASSERT(BP_IS_EMBEDDED(bp));
319 319
320 320 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
321 321 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
322 322 drrw->drr_object = object;
323 323 drrw->drr_offset = offset;
324 324 drrw->drr_length = blksz;
325 325 drrw->drr_toguid = dsp->dsa_toguid;
326 326 drrw->drr_compression = BP_GET_COMPRESS(bp);
327 327 drrw->drr_etype = BPE_GET_ETYPE(bp);
328 328 drrw->drr_lsize = BPE_GET_LSIZE(bp);
329 329 drrw->drr_psize = BPE_GET_PSIZE(bp);
330 330
331 331 decode_embedded_bp_compressed(bp, buf);
332 332
333 333 if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
334 334 return (EINTR);
335 335 return (0);
336 336 }
337 337
338 338 static int
339 339 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
340 340 {
341 341 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
342 342
343 343 if (dsp->dsa_pending_op != PENDING_NONE) {
344 344 if (dump_record(dsp, NULL, 0) != 0)
345 345 return (SET_ERROR(EINTR));
346 346 dsp->dsa_pending_op = PENDING_NONE;
347 347 }
348 348
349 349 /* write a SPILL record */
350 350 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
351 351 dsp->dsa_drr->drr_type = DRR_SPILL;
352 352 drrs->drr_object = object;
353 353 drrs->drr_length = blksz;
354 354 drrs->drr_toguid = dsp->dsa_toguid;
355 355
356 356 if (dump_record(dsp, data, blksz) != 0)
357 357 return (SET_ERROR(EINTR));
358 358 return (0);
359 359 }
360 360
361 361 static int
362 362 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
363 363 {
364 364 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
365 365
366 366 /*
367 367 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
368 368 * push it out, since free block aggregation can only be done for
369 369 * blocks of the same type (i.e., DRR_FREE records can only be
370 370 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
371 371 * can only be aggregated with other DRR_FREEOBJECTS records.
372 372 */
373 373 if (dsp->dsa_pending_op != PENDING_NONE &&
374 374 dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
375 375 if (dump_record(dsp, NULL, 0) != 0)
376 376 return (SET_ERROR(EINTR));
377 377 dsp->dsa_pending_op = PENDING_NONE;
378 378 }
379 379 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
380 380 /*
381 381 * See whether this free object array can be aggregated
382 382 * with pending one
383 383 */
384 384 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
385 385 drrfo->drr_numobjs += numobjs;
386 386 return (0);
387 387 } else {
388 388 /* can't be aggregated. Push out pending record */
389 389 if (dump_record(dsp, NULL, 0) != 0)
390 390 return (SET_ERROR(EINTR));
391 391 dsp->dsa_pending_op = PENDING_NONE;
392 392 }
393 393 }
394 394
395 395 /* write a FREEOBJECTS record */
396 396 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
397 397 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
398 398 drrfo->drr_firstobj = firstobj;
399 399 drrfo->drr_numobjs = numobjs;
400 400 drrfo->drr_toguid = dsp->dsa_toguid;
401 401
402 402 dsp->dsa_pending_op = PENDING_FREEOBJECTS;
403 403
404 404 return (0);
405 405 }
406 406
407 407 static int
408 408 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
409 409 {
410 410 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
411 411
412 412 if (object < dsp->dsa_resume_object) {
413 413 /*
414 414 * Note: when resuming, we will visit all the dnodes in
415 415 * the block of dnodes that we are resuming from. In
416 416 * this case it's unnecessary to send the dnodes prior to
417 417 * the one we are resuming from. We should be at most one
418 418 * block's worth of dnodes behind the resume point.
419 419 */
420 420 ASSERT3U(dsp->dsa_resume_object - object, <,
421 421 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
422 422 return (0);
423 423 }
424 424
425 425 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
426 426 return (dump_freeobjects(dsp, object, 1));
427 427
428 428 if (dsp->dsa_pending_op != PENDING_NONE) {
429 429 if (dump_record(dsp, NULL, 0) != 0)
430 430 return (SET_ERROR(EINTR));
431 431 dsp->dsa_pending_op = PENDING_NONE;
432 432 }
433 433
434 434 /* write an OBJECT record */
435 435 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
436 436 dsp->dsa_drr->drr_type = DRR_OBJECT;
437 437 drro->drr_object = object;
438 438 drro->drr_type = dnp->dn_type;
439 439 drro->drr_bonustype = dnp->dn_bonustype;
440 440 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
441 441 drro->drr_bonuslen = dnp->dn_bonuslen;
442 442 drro->drr_checksumtype = dnp->dn_checksum;
443 443 drro->drr_compress = dnp->dn_compress;
444 444 drro->drr_toguid = dsp->dsa_toguid;
445 445
446 446 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
447 447 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
448 448 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
449 449
450 450 if (dump_record(dsp, DN_BONUS(dnp),
451 451 P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
452 452 return (SET_ERROR(EINTR));
453 453 }
454 454
455 455 /* Free anything past the end of the file. */
456 456 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
457 457 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
458 458 return (SET_ERROR(EINTR));
459 459 if (dsp->dsa_err != 0)
460 460 return (SET_ERROR(EINTR));
461 461 return (0);
462 462 }
463 463
464 464 static boolean_t
465 465 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
466 466 {
467 467 if (!BP_IS_EMBEDDED(bp))
468 468 return (B_FALSE);
469 469
470 470 /*
471 471 * Compression function must be legacy, or explicitly enabled.
472 472 */
473 473 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
474 474 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
475 475 return (B_FALSE);
476 476
477 477 /*
478 478 * Embed type must be explicitly enabled.
479 479 */
480 480 switch (BPE_GET_ETYPE(bp)) {
481 481 case BP_EMBEDDED_TYPE_DATA:
482 482 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
483 483 return (B_TRUE);
484 484 break;
485 485 default:
486 486 return (B_FALSE);
487 487 }
488 488 return (B_FALSE);
489 489 }
490 490
491 491 /*
492 492 * This is the callback function to traverse_dataset that acts as the worker
493 493 * thread for dmu_send_impl.
494 494 */
495 495 /*ARGSUSED*/
496 496 static int
497 497 send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
498 498 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
499 499 {
500 500 struct send_thread_arg *sta = arg;
501 501 struct send_block_record *record;
502 502 uint64_t record_size;
503 503 int err = 0;
504 504
505 505 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
506 506 zb->zb_object >= sta->resume.zb_object);
507 507
508 508 if (sta->cancel)
509 509 return (SET_ERROR(EINTR));
510 510
511 511 if (bp == NULL) {
512 512 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
513 513 return (0);
514 514 } else if (zb->zb_level < 0) {
515 515 return (0);
516 516 }
517 517
518 518 record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
519 519 record->eos_marker = B_FALSE;
520 520 record->bp = *bp;
521 521 record->zb = *zb;
522 522 record->indblkshift = dnp->dn_indblkshift;
523 523 record->datablkszsec = dnp->dn_datablkszsec;
524 524 record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
525 525 bqueue_enqueue(&sta->q, record, record_size);
526 526
527 527 return (err);
528 528 }
529 529
530 530 /*
531 531 * This function kicks off the traverse_dataset. It also handles setting the
532 532 * error code of the thread in case something goes wrong, and pushes the End of
533 533 * Stream record when the traverse_dataset call has finished. If there is no
534 534 * dataset to traverse, the thread immediately pushes End of Stream marker.
535 535 */
536 536 static void
537 537 send_traverse_thread(void *arg)
538 538 {
539 539 struct send_thread_arg *st_arg = arg;
540 540 int err;
541 541 struct send_block_record *data;
542 542
543 543 if (st_arg->ds != NULL) {
544 544 err = traverse_dataset_resume(st_arg->ds,
545 545 st_arg->fromtxg, &st_arg->resume,
546 546 st_arg->flags, send_cb, st_arg);
547 547
548 548 if (err != EINTR)
549 549 st_arg->error_code = err;
550 550 }
551 551 data = kmem_zalloc(sizeof (*data), KM_SLEEP);
552 552 data->eos_marker = B_TRUE;
553 553 bqueue_enqueue(&st_arg->q, data, 1);
554 554 }
555 555
556 556 /*
557 557 * This function actually handles figuring out what kind of record needs to be
558 558 * dumped, reading the data (which has hopefully been prefetched), and calling
559 559 * the appropriate helper function.
560 560 */
561 561 static int
562 562 do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
563 563 {
564 564 dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
565 565 const blkptr_t *bp = &data->bp;
566 566 const zbookmark_phys_t *zb = &data->zb;
567 567 uint8_t indblkshift = data->indblkshift;
568 568 uint16_t dblkszsec = data->datablkszsec;
569 569 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
570 570 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
571 571 int err = 0;
572 572
573 573 ASSERT3U(zb->zb_level, >=, 0);
574 574
575 575 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
576 576 zb->zb_object >= dsa->dsa_resume_object);
577 577
578 578 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
579 579 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
580 580 return (0);
581 581 } else if (BP_IS_HOLE(bp) &&
582 582 zb->zb_object == DMU_META_DNODE_OBJECT) {
583 583 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
584 584 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
585 585 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
586 586 } else if (BP_IS_HOLE(bp)) {
587 587 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
588 588 uint64_t offset = zb->zb_blkid * span;
589 589 err = dump_free(dsa, zb->zb_object, offset, span);
590 590 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
591 591 return (0);
592 592 } else if (type == DMU_OT_DNODE) {
593 593 int blksz = BP_GET_LSIZE(bp);
594 594 arc_flags_t aflags = ARC_FLAG_WAIT;
595 595 arc_buf_t *abuf;
596 596
597 597 ASSERT0(zb->zb_level);
598 598
599 599 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
600 600 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
601 601 &aflags, zb) != 0)
602 602 return (SET_ERROR(EIO));
603 603
604 604 dnode_phys_t *blk = abuf->b_data;
605 605 uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
606 606 for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
607 607 err = dump_dnode(dsa, dnobj + i, blk + i);
608 608 if (err != 0)
609 609 break;
610 610 }
611 611 (void) arc_buf_remove_ref(abuf, &abuf);
612 612 } else if (type == DMU_OT_SA) {
613 613 arc_flags_t aflags = ARC_FLAG_WAIT;
614 614 arc_buf_t *abuf;
615 615 int blksz = BP_GET_LSIZE(bp);
616 616
617 617 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
618 618 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
619 619 &aflags, zb) != 0)
620 620 return (SET_ERROR(EIO));
621 621
622 622 err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
623 623 (void) arc_buf_remove_ref(abuf, &abuf);
624 624 } else if (backup_do_embed(dsa, bp)) {
625 625 /* it's an embedded level-0 block of a regular object */
626 626 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
627 627 ASSERT0(zb->zb_level);
628 628 err = dump_write_embedded(dsa, zb->zb_object,
629 629 zb->zb_blkid * blksz, blksz, bp);
630 630 } else {
631 631 /* it's a level-0 block of a regular object */
632 632 arc_flags_t aflags = ARC_FLAG_WAIT;
633 633 arc_buf_t *abuf;
634 634 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
635 635 uint64_t offset;
636 636
637 637 ASSERT0(zb->zb_level);
638 638 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
639 639 (zb->zb_object == dsa->dsa_resume_object &&
640 640 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
641 641
642 642 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
643 643 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
644 644 &aflags, zb) != 0) {
645 645 if (zfs_send_corrupt_data) {
646 646 /* Send a block filled with 0x"zfs badd bloc" */
647 647 abuf = arc_buf_alloc(spa, blksz, &abuf,
648 648 ARC_BUFC_DATA);
649 649 uint64_t *ptr;
650 650 for (ptr = abuf->b_data;
651 651 (char *)ptr < (char *)abuf->b_data + blksz;
652 652 ptr++)
653 653 *ptr = 0x2f5baddb10cULL;
654 654 } else {
655 655 return (SET_ERROR(EIO));
656 656 }
657 657 }
658 658
659 659 offset = zb->zb_blkid * blksz;
660 660
661 661 if (!(dsa->dsa_featureflags &
662 662 DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
663 663 blksz > SPA_OLD_MAXBLOCKSIZE) {
664 664 char *buf = abuf->b_data;
665 665 while (blksz > 0 && err == 0) {
666 666 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
667 667 err = dump_write(dsa, type, zb->zb_object,
668 668 offset, n, NULL, buf);
669 669 offset += n;
670 670 buf += n;
671 671 blksz -= n;
672 672 }
673 673 } else {
674 674 err = dump_write(dsa, type, zb->zb_object,
675 675 offset, blksz, bp, abuf->b_data);
676 676 }
677 677 (void) arc_buf_remove_ref(abuf, &abuf);
678 678 }
679 679
680 680 ASSERT(err == 0 || err == EINTR);
681 681 return (err);
682 682 }
683 683
684 684 /*
685 685 * Pop the new data off the queue, and free the old data.
686 686 */
687 687 static struct send_block_record *
688 688 get_next_record(bqueue_t *bq, struct send_block_record *data)
689 689 {
690 690 struct send_block_record *tmp = bqueue_dequeue(bq);
691 691 kmem_free(data, sizeof (*data));
692 692 return (tmp);
693 693 }
694 694
695 695 /*
696 696 * Actually do the bulk of the work in a zfs send.
697 697 *
698 698 * Note: Releases dp using the specified tag.
699 699 */
700 700 static int
701 701 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
702 702 zfs_bookmark_phys_t *ancestor_zb,
↓ open down ↓ |
702 lines elided |
↑ open up ↑ |
703 703 boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
704 704 uint64_t resumeobj, uint64_t resumeoff,
705 705 vnode_t *vp, offset_t *off)
706 706 {
707 707 objset_t *os;
708 708 dmu_replay_record_t *drr;
709 709 dmu_sendarg_t *dsp;
710 710 int err;
711 711 uint64_t fromtxg = 0;
712 712 uint64_t featureflags = 0;
713 - struct send_thread_arg to_arg = { 0 };
713 + struct send_thread_arg to_arg = { .error_code = 0 };
714 714
715 715 err = dmu_objset_from_ds(to_ds, &os);
716 716 if (err != 0) {
717 717 dsl_pool_rele(dp, tag);
718 718 return (err);
719 719 }
720 720
721 721 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
722 722 drr->drr_type = DRR_BEGIN;
723 723 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
724 724 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
725 725 DMU_SUBSTREAM);
726 726
727 727 #ifdef _KERNEL
728 728 if (dmu_objset_type(os) == DMU_OST_ZFS) {
729 729 uint64_t version;
730 730 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
731 731 kmem_free(drr, sizeof (dmu_replay_record_t));
732 732 dsl_pool_rele(dp, tag);
733 733 return (SET_ERROR(EINVAL));
734 734 }
735 735 if (version >= ZPL_VERSION_SA) {
736 736 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
737 737 }
738 738 }
739 739 #endif
740 740
741 741 if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
742 742 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
743 743 if (embedok &&
744 744 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
745 745 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
746 746 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
747 747 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
748 748 }
749 749
750 750 if (resumeobj != 0 || resumeoff != 0) {
751 751 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
752 752 }
753 753
754 754 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
755 755 featureflags);
756 756
757 757 drr->drr_u.drr_begin.drr_creation_time =
758 758 dsl_dataset_phys(to_ds)->ds_creation_time;
759 759 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
760 760 if (is_clone)
761 761 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
762 762 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
763 763 if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
764 764 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
765 765 if (zfs_send_set_freerecords_bit)
766 766 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
767 767
768 768 if (ancestor_zb != NULL) {
769 769 drr->drr_u.drr_begin.drr_fromguid =
770 770 ancestor_zb->zbm_guid;
771 771 fromtxg = ancestor_zb->zbm_creation_txg;
772 772 }
773 773 dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
774 774 if (!to_ds->ds_is_snapshot) {
775 775 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
776 776 sizeof (drr->drr_u.drr_begin.drr_toname));
777 777 }
778 778
779 779 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
780 780
781 781 dsp->dsa_drr = drr;
782 782 dsp->dsa_vp = vp;
783 783 dsp->dsa_outfd = outfd;
784 784 dsp->dsa_proc = curproc;
785 785 dsp->dsa_os = os;
786 786 dsp->dsa_off = off;
787 787 dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
788 788 dsp->dsa_pending_op = PENDING_NONE;
789 789 dsp->dsa_featureflags = featureflags;
790 790 dsp->dsa_resume_object = resumeobj;
791 791 dsp->dsa_resume_offset = resumeoff;
792 792
793 793 mutex_enter(&to_ds->ds_sendstream_lock);
794 794 list_insert_head(&to_ds->ds_sendstreams, dsp);
795 795 mutex_exit(&to_ds->ds_sendstream_lock);
796 796
797 797 dsl_dataset_long_hold(to_ds, FTAG);
798 798 dsl_pool_rele(dp, tag);
799 799
800 800 void *payload = NULL;
801 801 size_t payload_len = 0;
802 802 if (resumeobj != 0 || resumeoff != 0) {
803 803 dmu_object_info_t to_doi;
804 804 err = dmu_object_info(os, resumeobj, &to_doi);
805 805 if (err != 0)
806 806 goto out;
807 807 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
808 808 resumeoff / to_doi.doi_data_block_size);
809 809
810 810 nvlist_t *nvl = fnvlist_alloc();
811 811 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
812 812 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
813 813 payload = fnvlist_pack(nvl, &payload_len);
814 814 drr->drr_payloadlen = payload_len;
815 815 fnvlist_free(nvl);
816 816 }
817 817
818 818 err = dump_record(dsp, payload, payload_len);
819 819 fnvlist_pack_free(payload, payload_len);
820 820 if (err != 0) {
821 821 err = dsp->dsa_err;
822 822 goto out;
823 823 }
824 824
825 825 err = bqueue_init(&to_arg.q, zfs_send_queue_length,
826 826 offsetof(struct send_block_record, ln));
827 827 to_arg.error_code = 0;
828 828 to_arg.cancel = B_FALSE;
829 829 to_arg.ds = to_ds;
830 830 to_arg.fromtxg = fromtxg;
831 831 to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
832 832 (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
833 833 TS_RUN, minclsyspri);
834 834
835 835 struct send_block_record *to_data;
836 836 to_data = bqueue_dequeue(&to_arg.q);
837 837
838 838 while (!to_data->eos_marker && err == 0) {
839 839 err = do_dump(dsp, to_data);
840 840 to_data = get_next_record(&to_arg.q, to_data);
841 841 if (issig(JUSTLOOKING) && issig(FORREAL))
842 842 err = EINTR;
843 843 }
844 844
845 845 if (err != 0) {
846 846 to_arg.cancel = B_TRUE;
847 847 while (!to_data->eos_marker) {
848 848 to_data = get_next_record(&to_arg.q, to_data);
849 849 }
850 850 }
851 851 kmem_free(to_data, sizeof (*to_data));
852 852
853 853 bqueue_destroy(&to_arg.q);
854 854
855 855 if (err == 0 && to_arg.error_code != 0)
856 856 err = to_arg.error_code;
857 857
858 858 if (err != 0)
859 859 goto out;
860 860
861 861 if (dsp->dsa_pending_op != PENDING_NONE)
862 862 if (dump_record(dsp, NULL, 0) != 0)
863 863 err = SET_ERROR(EINTR);
864 864
865 865 if (err != 0) {
866 866 if (err == EINTR && dsp->dsa_err != 0)
867 867 err = dsp->dsa_err;
868 868 goto out;
869 869 }
870 870
871 871 bzero(drr, sizeof (dmu_replay_record_t));
872 872 drr->drr_type = DRR_END;
873 873 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
874 874 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
875 875
876 876 if (dump_record(dsp, NULL, 0) != 0)
877 877 err = dsp->dsa_err;
878 878
879 879 out:
880 880 mutex_enter(&to_ds->ds_sendstream_lock);
881 881 list_remove(&to_ds->ds_sendstreams, dsp);
882 882 mutex_exit(&to_ds->ds_sendstream_lock);
883 883
884 884 kmem_free(drr, sizeof (dmu_replay_record_t));
885 885 kmem_free(dsp, sizeof (dmu_sendarg_t));
886 886
887 887 dsl_dataset_long_rele(to_ds, FTAG);
888 888
889 889 return (err);
890 890 }
891 891
892 892 int
893 893 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
894 894 boolean_t embedok, boolean_t large_block_ok,
895 895 int outfd, vnode_t *vp, offset_t *off)
896 896 {
897 897 dsl_pool_t *dp;
898 898 dsl_dataset_t *ds;
899 899 dsl_dataset_t *fromds = NULL;
900 900 int err;
901 901
902 902 err = dsl_pool_hold(pool, FTAG, &dp);
903 903 if (err != 0)
904 904 return (err);
905 905
906 906 err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
907 907 if (err != 0) {
908 908 dsl_pool_rele(dp, FTAG);
909 909 return (err);
910 910 }
911 911
912 912 if (fromsnap != 0) {
913 913 zfs_bookmark_phys_t zb;
914 914 boolean_t is_clone;
915 915
916 916 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
917 917 if (err != 0) {
918 918 dsl_dataset_rele(ds, FTAG);
919 919 dsl_pool_rele(dp, FTAG);
920 920 return (err);
921 921 }
922 922 if (!dsl_dataset_is_before(ds, fromds, 0))
923 923 err = SET_ERROR(EXDEV);
924 924 zb.zbm_creation_time =
925 925 dsl_dataset_phys(fromds)->ds_creation_time;
926 926 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
927 927 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
928 928 is_clone = (fromds->ds_dir != ds->ds_dir);
929 929 dsl_dataset_rele(fromds, FTAG);
930 930 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
931 931 embedok, large_block_ok, outfd, 0, 0, vp, off);
932 932 } else {
933 933 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
934 934 embedok, large_block_ok, outfd, 0, 0, vp, off);
935 935 }
936 936 dsl_dataset_rele(ds, FTAG);
937 937 return (err);
938 938 }
939 939
940 940 int
941 941 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
942 942 boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
943 943 vnode_t *vp, offset_t *off)
944 944 {
945 945 dsl_pool_t *dp;
946 946 dsl_dataset_t *ds;
947 947 int err;
948 948 boolean_t owned = B_FALSE;
949 949
950 950 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
951 951 return (SET_ERROR(EINVAL));
952 952
953 953 err = dsl_pool_hold(tosnap, FTAG, &dp);
954 954 if (err != 0)
955 955 return (err);
956 956
957 957 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
958 958 /*
959 959 * We are sending a filesystem or volume. Ensure
960 960 * that it doesn't change by owning the dataset.
961 961 */
962 962 err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
963 963 owned = B_TRUE;
964 964 } else {
965 965 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
966 966 }
967 967 if (err != 0) {
968 968 dsl_pool_rele(dp, FTAG);
969 969 return (err);
970 970 }
971 971
972 972 if (fromsnap != NULL) {
973 973 zfs_bookmark_phys_t zb;
974 974 boolean_t is_clone = B_FALSE;
975 975 int fsnamelen = strchr(tosnap, '@') - tosnap;
976 976
977 977 /*
978 978 * If the fromsnap is in a different filesystem, then
979 979 * mark the send stream as a clone.
980 980 */
981 981 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
982 982 (fromsnap[fsnamelen] != '@' &&
983 983 fromsnap[fsnamelen] != '#')) {
984 984 is_clone = B_TRUE;
985 985 }
986 986
987 987 if (strchr(fromsnap, '@')) {
988 988 dsl_dataset_t *fromds;
989 989 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
990 990 if (err == 0) {
991 991 if (!dsl_dataset_is_before(ds, fromds, 0))
992 992 err = SET_ERROR(EXDEV);
993 993 zb.zbm_creation_time =
994 994 dsl_dataset_phys(fromds)->ds_creation_time;
995 995 zb.zbm_creation_txg =
996 996 dsl_dataset_phys(fromds)->ds_creation_txg;
997 997 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
998 998 is_clone = (ds->ds_dir != fromds->ds_dir);
999 999 dsl_dataset_rele(fromds, FTAG);
1000 1000 }
1001 1001 } else {
1002 1002 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1003 1003 }
1004 1004 if (err != 0) {
1005 1005 dsl_dataset_rele(ds, FTAG);
1006 1006 dsl_pool_rele(dp, FTAG);
1007 1007 return (err);
1008 1008 }
1009 1009 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1010 1010 embedok, large_block_ok,
1011 1011 outfd, resumeobj, resumeoff, vp, off);
1012 1012 } else {
1013 1013 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1014 1014 embedok, large_block_ok,
1015 1015 outfd, resumeobj, resumeoff, vp, off);
1016 1016 }
1017 1017 if (owned)
1018 1018 dsl_dataset_disown(ds, FTAG);
1019 1019 else
1020 1020 dsl_dataset_rele(ds, FTAG);
1021 1021 return (err);
1022 1022 }
1023 1023
1024 1024 static int
1025 1025 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1026 1026 uint64_t *sizep)
1027 1027 {
1028 1028 int err;
1029 1029 /*
1030 1030 * Assume that space (both on-disk and in-stream) is dominated by
1031 1031 * data. We will adjust for indirect blocks and the copies property,
1032 1032 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1033 1033 */
1034 1034
1035 1035 /*
1036 1036 * Subtract out approximate space used by indirect blocks.
1037 1037 * Assume most space is used by data blocks (non-indirect, non-dnode).
1038 1038 * Assume all blocks are recordsize. Assume ditto blocks and
1039 1039 * internal fragmentation counter out compression.
1040 1040 *
1041 1041 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1042 1042 * block, which we observe in practice.
1043 1043 */
1044 1044 uint64_t recordsize;
1045 1045 err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1046 1046 if (err != 0)
1047 1047 return (err);
1048 1048 size -= size / recordsize * sizeof (blkptr_t);
1049 1049
1050 1050 /* Add in the space for the record associated with each block. */
1051 1051 size += size / recordsize * sizeof (dmu_replay_record_t);
1052 1052
1053 1053 *sizep = size;
1054 1054
1055 1055 return (0);
1056 1056 }
1057 1057
1058 1058 int
1059 1059 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
1060 1060 {
1061 1061 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1062 1062 int err;
1063 1063 uint64_t size;
1064 1064
1065 1065 ASSERT(dsl_pool_config_held(dp));
1066 1066
1067 1067 /* tosnap must be a snapshot */
1068 1068 if (!ds->ds_is_snapshot)
1069 1069 return (SET_ERROR(EINVAL));
1070 1070
1071 1071 /* fromsnap, if provided, must be a snapshot */
1072 1072 if (fromds != NULL && !fromds->ds_is_snapshot)
1073 1073 return (SET_ERROR(EINVAL));
1074 1074
1075 1075 /*
1076 1076 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1077 1077 * or the origin's fs.
1078 1078 */
1079 1079 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1080 1080 return (SET_ERROR(EXDEV));
1081 1081
1082 1082 /* Get uncompressed size estimate of changed data. */
1083 1083 if (fromds == NULL) {
1084 1084 size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1085 1085 } else {
1086 1086 uint64_t used, comp;
1087 1087 err = dsl_dataset_space_written(fromds, ds,
1088 1088 &used, &comp, &size);
1089 1089 if (err != 0)
1090 1090 return (err);
1091 1091 }
1092 1092
1093 1093 err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1094 1094 return (err);
1095 1095 }
1096 1096
1097 1097 /*
1098 1098 * Simple callback used to traverse the blocks of a snapshot and sum their
1099 1099 * uncompressed size
1100 1100 */
1101 1101 /* ARGSUSED */
1102 1102 static int
1103 1103 dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1104 1104 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1105 1105 {
1106 1106 uint64_t *spaceptr = arg;
1107 1107 if (bp != NULL && !BP_IS_HOLE(bp)) {
1108 1108 *spaceptr += BP_GET_UCSIZE(bp);
1109 1109 }
1110 1110 return (0);
1111 1111 }
1112 1112
1113 1113 /*
1114 1114 * Given a desination snapshot and a TXG, calculate the approximate size of a
1115 1115 * send stream sent from that TXG. from_txg may be zero, indicating that the
1116 1116 * whole snapshot will be sent.
1117 1117 */
1118 1118 int
1119 1119 dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1120 1120 uint64_t *sizep)
1121 1121 {
1122 1122 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1123 1123 int err;
1124 1124 uint64_t size = 0;
1125 1125
1126 1126 ASSERT(dsl_pool_config_held(dp));
1127 1127
1128 1128 /* tosnap must be a snapshot */
1129 1129 if (!dsl_dataset_is_snapshot(ds))
1130 1130 return (SET_ERROR(EINVAL));
1131 1131
1132 1132 /* verify that from_txg is before the provided snapshot was taken */
1133 1133 if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1134 1134 return (SET_ERROR(EXDEV));
1135 1135 }
1136 1136
1137 1137 /*
1138 1138 * traverse the blocks of the snapshot with birth times after
1139 1139 * from_txg, summing their uncompressed size
1140 1140 */
1141 1141 err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1142 1142 dmu_calculate_send_traversal, &size);
1143 1143 if (err)
1144 1144 return (err);
1145 1145
1146 1146 err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1147 1147 return (err);
1148 1148 }
1149 1149
1150 1150 typedef struct dmu_recv_begin_arg {
1151 1151 const char *drba_origin;
1152 1152 dmu_recv_cookie_t *drba_cookie;
1153 1153 cred_t *drba_cred;
1154 1154 uint64_t drba_snapobj;
1155 1155 } dmu_recv_begin_arg_t;
1156 1156
1157 1157 static int
1158 1158 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1159 1159 uint64_t fromguid)
1160 1160 {
1161 1161 uint64_t val;
1162 1162 int error;
1163 1163 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1164 1164
1165 1165 /* temporary clone name must not exist */
1166 1166 error = zap_lookup(dp->dp_meta_objset,
1167 1167 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
1168 1168 8, 1, &val);
1169 1169 if (error != ENOENT)
1170 1170 return (error == 0 ? EBUSY : error);
1171 1171
1172 1172 /* new snapshot name must not exist */
1173 1173 error = zap_lookup(dp->dp_meta_objset,
1174 1174 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1175 1175 drba->drba_cookie->drc_tosnap, 8, 1, &val);
1176 1176 if (error != ENOENT)
1177 1177 return (error == 0 ? EEXIST : error);
1178 1178
1179 1179 /*
1180 1180 * Check snapshot limit before receiving. We'll recheck again at the
1181 1181 * end, but might as well abort before receiving if we're already over
1182 1182 * the limit.
1183 1183 *
1184 1184 * Note that we do not check the file system limit with
1185 1185 * dsl_dir_fscount_check because the temporary %clones don't count
1186 1186 * against that limit.
1187 1187 */
1188 1188 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1189 1189 NULL, drba->drba_cred);
1190 1190 if (error != 0)
1191 1191 return (error);
1192 1192
1193 1193 if (fromguid != 0) {
1194 1194 dsl_dataset_t *snap;
1195 1195 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1196 1196
1197 1197 /* Find snapshot in this dir that matches fromguid. */
1198 1198 while (obj != 0) {
1199 1199 error = dsl_dataset_hold_obj(dp, obj, FTAG,
1200 1200 &snap);
1201 1201 if (error != 0)
1202 1202 return (SET_ERROR(ENODEV));
1203 1203 if (snap->ds_dir != ds->ds_dir) {
1204 1204 dsl_dataset_rele(snap, FTAG);
1205 1205 return (SET_ERROR(ENODEV));
1206 1206 }
1207 1207 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
1208 1208 break;
1209 1209 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
1210 1210 dsl_dataset_rele(snap, FTAG);
1211 1211 }
1212 1212 if (obj == 0)
1213 1213 return (SET_ERROR(ENODEV));
1214 1214
1215 1215 if (drba->drba_cookie->drc_force) {
1216 1216 drba->drba_snapobj = obj;
1217 1217 } else {
1218 1218 /*
1219 1219 * If we are not forcing, there must be no
1220 1220 * changes since fromsnap.
1221 1221 */
1222 1222 if (dsl_dataset_modified_since_snap(ds, snap)) {
1223 1223 dsl_dataset_rele(snap, FTAG);
1224 1224 return (SET_ERROR(ETXTBSY));
1225 1225 }
1226 1226 drba->drba_snapobj = ds->ds_prev->ds_object;
1227 1227 }
1228 1228
1229 1229 dsl_dataset_rele(snap, FTAG);
1230 1230 } else {
1231 1231 /* if full, then must be forced */
1232 1232 if (!drba->drba_cookie->drc_force)
1233 1233 return (SET_ERROR(EEXIST));
1234 1234 /* start from $ORIGIN@$ORIGIN, if supported */
1235 1235 drba->drba_snapobj = dp->dp_origin_snap != NULL ?
1236 1236 dp->dp_origin_snap->ds_object : 0;
1237 1237 }
1238 1238
1239 1239 return (0);
1240 1240
1241 1241 }
1242 1242
1243 1243 static int
1244 1244 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1245 1245 {
1246 1246 dmu_recv_begin_arg_t *drba = arg;
1247 1247 dsl_pool_t *dp = dmu_tx_pool(tx);
1248 1248 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1249 1249 uint64_t fromguid = drrb->drr_fromguid;
1250 1250 int flags = drrb->drr_flags;
1251 1251 int error;
1252 1252 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1253 1253 dsl_dataset_t *ds;
1254 1254 const char *tofs = drba->drba_cookie->drc_tofs;
1255 1255
1256 1256 /* already checked */
1257 1257 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1258 1258 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
1259 1259
1260 1260 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1261 1261 DMU_COMPOUNDSTREAM ||
1262 1262 drrb->drr_type >= DMU_OST_NUMTYPES ||
1263 1263 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1264 1264 return (SET_ERROR(EINVAL));
1265 1265
1266 1266 /* Verify pool version supports SA if SA_SPILL feature set */
1267 1267 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1268 1268 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1269 1269 return (SET_ERROR(ENOTSUP));
1270 1270
1271 1271 if (drba->drba_cookie->drc_resumable &&
1272 1272 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1273 1273 return (SET_ERROR(ENOTSUP));
1274 1274
1275 1275 /*
1276 1276 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1277 1277 * record to a plan WRITE record, so the pool must have the
1278 1278 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1279 1279 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1280 1280 */
1281 1281 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1282 1282 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1283 1283 return (SET_ERROR(ENOTSUP));
1284 1284 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1285 1285 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1286 1286 return (SET_ERROR(ENOTSUP));
1287 1287
1288 1288 /*
1289 1289 * The receiving code doesn't know how to translate large blocks
1290 1290 * to smaller ones, so the pool must have the LARGE_BLOCKS
1291 1291 * feature enabled if the stream has LARGE_BLOCKS.
1292 1292 */
1293 1293 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1294 1294 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1295 1295 return (SET_ERROR(ENOTSUP));
1296 1296
1297 1297 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1298 1298 if (error == 0) {
1299 1299 /* target fs already exists; recv into temp clone */
1300 1300
1301 1301 /* Can't recv a clone into an existing fs */
1302 1302 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
1303 1303 dsl_dataset_rele(ds, FTAG);
1304 1304 return (SET_ERROR(EINVAL));
1305 1305 }
1306 1306
1307 1307 error = recv_begin_check_existing_impl(drba, ds, fromguid);
1308 1308 dsl_dataset_rele(ds, FTAG);
1309 1309 } else if (error == ENOENT) {
1310 1310 /* target fs does not exist; must be a full backup or clone */
1311 1311 char buf[ZFS_MAX_DATASET_NAME_LEN];
1312 1312
1313 1313 /*
1314 1314 * If it's a non-clone incremental, we are missing the
1315 1315 * target fs, so fail the recv.
1316 1316 */
1317 1317 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1318 1318 drba->drba_origin))
1319 1319 return (SET_ERROR(ENOENT));
1320 1320
1321 1321 /*
1322 1322 * If we're receiving a full send as a clone, and it doesn't
1323 1323 * contain all the necessary free records and freeobject
1324 1324 * records, reject it.
1325 1325 */
1326 1326 if (fromguid == 0 && drba->drba_origin &&
1327 1327 !(flags & DRR_FLAG_FREERECORDS))
1328 1328 return (SET_ERROR(EINVAL));
1329 1329
1330 1330 /* Open the parent of tofs */
1331 1331 ASSERT3U(strlen(tofs), <, sizeof (buf));
1332 1332 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1333 1333 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1334 1334 if (error != 0)
1335 1335 return (error);
1336 1336
1337 1337 /*
1338 1338 * Check filesystem and snapshot limits before receiving. We'll
1339 1339 * recheck snapshot limits again at the end (we create the
1340 1340 * filesystems and increment those counts during begin_sync).
1341 1341 */
1342 1342 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1343 1343 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1344 1344 if (error != 0) {
1345 1345 dsl_dataset_rele(ds, FTAG);
1346 1346 return (error);
1347 1347 }
1348 1348
1349 1349 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1350 1350 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1351 1351 if (error != 0) {
1352 1352 dsl_dataset_rele(ds, FTAG);
1353 1353 return (error);
1354 1354 }
1355 1355
1356 1356 if (drba->drba_origin != NULL) {
1357 1357 dsl_dataset_t *origin;
1358 1358 error = dsl_dataset_hold(dp, drba->drba_origin,
1359 1359 FTAG, &origin);
1360 1360 if (error != 0) {
1361 1361 dsl_dataset_rele(ds, FTAG);
1362 1362 return (error);
1363 1363 }
1364 1364 if (!origin->ds_is_snapshot) {
1365 1365 dsl_dataset_rele(origin, FTAG);
1366 1366 dsl_dataset_rele(ds, FTAG);
1367 1367 return (SET_ERROR(EINVAL));
1368 1368 }
1369 1369 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1370 1370 fromguid != 0) {
1371 1371 dsl_dataset_rele(origin, FTAG);
1372 1372 dsl_dataset_rele(ds, FTAG);
1373 1373 return (SET_ERROR(ENODEV));
1374 1374 }
1375 1375 dsl_dataset_rele(origin, FTAG);
1376 1376 }
1377 1377 dsl_dataset_rele(ds, FTAG);
1378 1378 error = 0;
1379 1379 }
1380 1380 return (error);
1381 1381 }
1382 1382
1383 1383 static void
1384 1384 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1385 1385 {
1386 1386 dmu_recv_begin_arg_t *drba = arg;
1387 1387 dsl_pool_t *dp = dmu_tx_pool(tx);
1388 1388 objset_t *mos = dp->dp_meta_objset;
1389 1389 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1390 1390 const char *tofs = drba->drba_cookie->drc_tofs;
1391 1391 dsl_dataset_t *ds, *newds;
1392 1392 uint64_t dsobj;
1393 1393 int error;
1394 1394 uint64_t crflags = 0;
1395 1395
1396 1396 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1397 1397 crflags |= DS_FLAG_CI_DATASET;
1398 1398
1399 1399 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1400 1400 if (error == 0) {
1401 1401 /* create temporary clone */
1402 1402 dsl_dataset_t *snap = NULL;
1403 1403 if (drba->drba_snapobj != 0) {
1404 1404 VERIFY0(dsl_dataset_hold_obj(dp,
1405 1405 drba->drba_snapobj, FTAG, &snap));
1406 1406 }
1407 1407 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1408 1408 snap, crflags, drba->drba_cred, tx);
1409 1409 if (drba->drba_snapobj != 0)
1410 1410 dsl_dataset_rele(snap, FTAG);
1411 1411 dsl_dataset_rele(ds, FTAG);
1412 1412 } else {
1413 1413 dsl_dir_t *dd;
1414 1414 const char *tail;
1415 1415 dsl_dataset_t *origin = NULL;
1416 1416
1417 1417 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1418 1418
1419 1419 if (drba->drba_origin != NULL) {
1420 1420 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1421 1421 FTAG, &origin));
1422 1422 }
1423 1423
1424 1424 /* Create new dataset. */
1425 1425 dsobj = dsl_dataset_create_sync(dd,
1426 1426 strrchr(tofs, '/') + 1,
1427 1427 origin, crflags, drba->drba_cred, tx);
1428 1428 if (origin != NULL)
1429 1429 dsl_dataset_rele(origin, FTAG);
1430 1430 dsl_dir_rele(dd, FTAG);
1431 1431 drba->drba_cookie->drc_newfs = B_TRUE;
1432 1432 }
1433 1433 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
1434 1434
1435 1435 if (drba->drba_cookie->drc_resumable) {
1436 1436 dsl_dataset_zapify(newds, tx);
1437 1437 if (drrb->drr_fromguid != 0) {
1438 1438 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1439 1439 8, 1, &drrb->drr_fromguid, tx));
1440 1440 }
1441 1441 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1442 1442 8, 1, &drrb->drr_toguid, tx));
1443 1443 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1444 1444 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1445 1445 uint64_t one = 1;
1446 1446 uint64_t zero = 0;
1447 1447 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1448 1448 8, 1, &one, tx));
1449 1449 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1450 1450 8, 1, &zero, tx));
1451 1451 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1452 1452 8, 1, &zero, tx));
1453 1453 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1454 1454 DMU_BACKUP_FEATURE_EMBED_DATA) {
1455 1455 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1456 1456 8, 1, &one, tx));
1457 1457 }
1458 1458 }
1459 1459
1460 1460 dmu_buf_will_dirty(newds->ds_dbuf, tx);
1461 1461 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
1462 1462
1463 1463 /*
1464 1464 * If we actually created a non-clone, we need to create the
1465 1465 * objset in our new dataset.
1466 1466 */
1467 1467 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1468 1468 (void) dmu_objset_create_impl(dp->dp_spa,
1469 1469 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1470 1470 }
1471 1471
1472 1472 drba->drba_cookie->drc_ds = newds;
1473 1473
1474 1474 spa_history_log_internal_ds(newds, "receive", tx, "");
1475 1475 }
1476 1476
1477 1477 static int
1478 1478 dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1479 1479 {
1480 1480 dmu_recv_begin_arg_t *drba = arg;
1481 1481 dsl_pool_t *dp = dmu_tx_pool(tx);
1482 1482 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1483 1483 int error;
1484 1484 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1485 1485 dsl_dataset_t *ds;
1486 1486 const char *tofs = drba->drba_cookie->drc_tofs;
1487 1487
1488 1488 /* already checked */
1489 1489 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1490 1490 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1491 1491
1492 1492 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1493 1493 DMU_COMPOUNDSTREAM ||
1494 1494 drrb->drr_type >= DMU_OST_NUMTYPES)
1495 1495 return (SET_ERROR(EINVAL));
1496 1496
1497 1497 /* Verify pool version supports SA if SA_SPILL feature set */
1498 1498 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1499 1499 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1500 1500 return (SET_ERROR(ENOTSUP));
1501 1501
1502 1502 /*
1503 1503 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1504 1504 * record to a plain WRITE record, so the pool must have the
1505 1505 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1506 1506 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1507 1507 */
1508 1508 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1509 1509 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1510 1510 return (SET_ERROR(ENOTSUP));
1511 1511 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1512 1512 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1513 1513 return (SET_ERROR(ENOTSUP));
1514 1514
1515 1515 /* 6 extra bytes for /%recv */
1516 1516 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1517 1517
1518 1518 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1519 1519 tofs, recv_clone_name);
1520 1520
1521 1521 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1522 1522 /* %recv does not exist; continue in tofs */
1523 1523 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1524 1524 if (error != 0)
1525 1525 return (error);
1526 1526 }
1527 1527
1528 1528 /* check that ds is marked inconsistent */
1529 1529 if (!DS_IS_INCONSISTENT(ds)) {
1530 1530 dsl_dataset_rele(ds, FTAG);
1531 1531 return (SET_ERROR(EINVAL));
1532 1532 }
1533 1533
1534 1534 /* check that there is resuming data, and that the toguid matches */
1535 1535 if (!dsl_dataset_is_zapified(ds)) {
1536 1536 dsl_dataset_rele(ds, FTAG);
1537 1537 return (SET_ERROR(EINVAL));
1538 1538 }
1539 1539 uint64_t val;
1540 1540 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1541 1541 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1542 1542 if (error != 0 || drrb->drr_toguid != val) {
1543 1543 dsl_dataset_rele(ds, FTAG);
1544 1544 return (SET_ERROR(EINVAL));
1545 1545 }
1546 1546
1547 1547 /*
1548 1548 * Check if the receive is still running. If so, it will be owned.
1549 1549 * Note that nothing else can own the dataset (e.g. after the receive
1550 1550 * fails) because it will be marked inconsistent.
1551 1551 */
1552 1552 if (dsl_dataset_has_owner(ds)) {
1553 1553 dsl_dataset_rele(ds, FTAG);
1554 1554 return (SET_ERROR(EBUSY));
1555 1555 }
1556 1556
1557 1557 /* There should not be any snapshots of this fs yet. */
1558 1558 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1559 1559 dsl_dataset_rele(ds, FTAG);
1560 1560 return (SET_ERROR(EINVAL));
1561 1561 }
1562 1562
1563 1563 /*
1564 1564 * Note: resume point will be checked when we process the first WRITE
1565 1565 * record.
1566 1566 */
1567 1567
1568 1568 /* check that the origin matches */
1569 1569 val = 0;
1570 1570 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1571 1571 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1572 1572 if (drrb->drr_fromguid != val) {
1573 1573 dsl_dataset_rele(ds, FTAG);
1574 1574 return (SET_ERROR(EINVAL));
1575 1575 }
1576 1576
1577 1577 dsl_dataset_rele(ds, FTAG);
1578 1578 return (0);
1579 1579 }
1580 1580
1581 1581 static void
1582 1582 dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1583 1583 {
1584 1584 dmu_recv_begin_arg_t *drba = arg;
1585 1585 dsl_pool_t *dp = dmu_tx_pool(tx);
1586 1586 const char *tofs = drba->drba_cookie->drc_tofs;
1587 1587 dsl_dataset_t *ds;
1588 1588 uint64_t dsobj;
1589 1589 /* 6 extra bytes for /%recv */
1590 1590 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1591 1591
1592 1592 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1593 1593 tofs, recv_clone_name);
1594 1594
1595 1595 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1596 1596 /* %recv does not exist; continue in tofs */
1597 1597 VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1598 1598 drba->drba_cookie->drc_newfs = B_TRUE;
1599 1599 }
1600 1600
1601 1601 /* clear the inconsistent flag so that we can own it */
1602 1602 ASSERT(DS_IS_INCONSISTENT(ds));
1603 1603 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1604 1604 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1605 1605 dsobj = ds->ds_object;
1606 1606 dsl_dataset_rele(ds, FTAG);
1607 1607
1608 1608 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1609 1609
1610 1610 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1611 1611 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1612 1612
1613 1613 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1614 1614
1615 1615 drba->drba_cookie->drc_ds = ds;
1616 1616
1617 1617 spa_history_log_internal_ds(ds, "resume receive", tx, "");
1618 1618 }
1619 1619
1620 1620 /*
1621 1621 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1622 1622 * succeeds; otherwise we will leak the holds on the datasets.
1623 1623 */
1624 1624 int
1625 1625 dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1626 1626 boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1627 1627 {
1628 1628 dmu_recv_begin_arg_t drba = { 0 };
1629 1629
1630 1630 bzero(drc, sizeof (dmu_recv_cookie_t));
1631 1631 drc->drc_drr_begin = drr_begin;
1632 1632 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1633 1633 drc->drc_tosnap = tosnap;
1634 1634 drc->drc_tofs = tofs;
1635 1635 drc->drc_force = force;
1636 1636 drc->drc_resumable = resumable;
1637 1637 drc->drc_cred = CRED();
1638 1638
1639 1639 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1640 1640 drc->drc_byteswap = B_TRUE;
1641 1641 fletcher_4_incremental_byteswap(drr_begin,
1642 1642 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1643 1643 byteswap_record(drr_begin);
1644 1644 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1645 1645 fletcher_4_incremental_native(drr_begin,
1646 1646 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1647 1647 } else {
1648 1648 return (SET_ERROR(EINVAL));
1649 1649 }
1650 1650
1651 1651 drba.drba_origin = origin;
1652 1652 drba.drba_cookie = drc;
1653 1653 drba.drba_cred = CRED();
1654 1654
1655 1655 if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1656 1656 DMU_BACKUP_FEATURE_RESUMING) {
1657 1657 return (dsl_sync_task(tofs,
1658 1658 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1659 1659 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1660 1660 } else {
1661 1661 return (dsl_sync_task(tofs,
1662 1662 dmu_recv_begin_check, dmu_recv_begin_sync,
1663 1663 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1664 1664 }
1665 1665 }
1666 1666
1667 1667 struct receive_record_arg {
1668 1668 dmu_replay_record_t header;
1669 1669 void *payload; /* Pointer to a buffer containing the payload */
1670 1670 /*
1671 1671 * If the record is a write, pointer to the arc_buf_t containing the
1672 1672 * payload.
1673 1673 */
1674 1674 arc_buf_t *write_buf;
1675 1675 int payload_size;
1676 1676 uint64_t bytes_read; /* bytes read from stream when record created */
1677 1677 boolean_t eos_marker; /* Marks the end of the stream */
1678 1678 bqueue_node_t node;
1679 1679 };
1680 1680
1681 1681 struct receive_writer_arg {
1682 1682 objset_t *os;
1683 1683 boolean_t byteswap;
1684 1684 bqueue_t q;
1685 1685
1686 1686 /*
1687 1687 * These three args are used to signal to the main thread that we're
1688 1688 * done.
1689 1689 */
1690 1690 kmutex_t mutex;
1691 1691 kcondvar_t cv;
1692 1692 boolean_t done;
1693 1693
1694 1694 int err;
1695 1695 /* A map from guid to dataset to help handle dedup'd streams. */
1696 1696 avl_tree_t *guid_to_ds_map;
1697 1697 boolean_t resumable;
1698 1698 uint64_t last_object, last_offset;
1699 1699 uint64_t bytes_read; /* bytes read when current record created */
1700 1700 };
1701 1701
1702 1702 struct objlist {
1703 1703 list_t list; /* List of struct receive_objnode. */
1704 1704 /*
1705 1705 * Last object looked up. Used to assert that objects are being looked
1706 1706 * up in ascending order.
1707 1707 */
1708 1708 uint64_t last_lookup;
1709 1709 };
1710 1710
1711 1711 struct receive_objnode {
1712 1712 list_node_t node;
1713 1713 uint64_t object;
1714 1714 };
1715 1715
1716 1716 struct receive_arg {
1717 1717 objset_t *os;
1718 1718 vnode_t *vp; /* The vnode to read the stream from */
1719 1719 uint64_t voff; /* The current offset in the stream */
1720 1720 uint64_t bytes_read;
1721 1721 /*
1722 1722 * A record that has had its payload read in, but hasn't yet been handed
1723 1723 * off to the worker thread.
1724 1724 */
1725 1725 struct receive_record_arg *rrd;
1726 1726 /* A record that has had its header read in, but not its payload. */
1727 1727 struct receive_record_arg *next_rrd;
1728 1728 zio_cksum_t cksum;
1729 1729 zio_cksum_t prev_cksum;
1730 1730 int err;
1731 1731 boolean_t byteswap;
1732 1732 /* Sorted list of objects not to issue prefetches for. */
1733 1733 struct objlist ignore_objlist;
1734 1734 };
1735 1735
1736 1736 typedef struct guid_map_entry {
1737 1737 uint64_t guid;
1738 1738 dsl_dataset_t *gme_ds;
1739 1739 avl_node_t avlnode;
1740 1740 } guid_map_entry_t;
1741 1741
1742 1742 static int
1743 1743 guid_compare(const void *arg1, const void *arg2)
1744 1744 {
1745 1745 const guid_map_entry_t *gmep1 = arg1;
1746 1746 const guid_map_entry_t *gmep2 = arg2;
1747 1747
1748 1748 if (gmep1->guid < gmep2->guid)
1749 1749 return (-1);
1750 1750 else if (gmep1->guid > gmep2->guid)
1751 1751 return (1);
1752 1752 return (0);
1753 1753 }
1754 1754
1755 1755 static void
1756 1756 free_guid_map_onexit(void *arg)
1757 1757 {
1758 1758 avl_tree_t *ca = arg;
1759 1759 void *cookie = NULL;
1760 1760 guid_map_entry_t *gmep;
1761 1761
1762 1762 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
1763 1763 dsl_dataset_long_rele(gmep->gme_ds, gmep);
1764 1764 dsl_dataset_rele(gmep->gme_ds, gmep);
1765 1765 kmem_free(gmep, sizeof (guid_map_entry_t));
1766 1766 }
1767 1767 avl_destroy(ca);
1768 1768 kmem_free(ca, sizeof (avl_tree_t));
1769 1769 }
1770 1770
1771 1771 static int
1772 1772 receive_read(struct receive_arg *ra, int len, void *buf)
1773 1773 {
1774 1774 int done = 0;
1775 1775
1776 1776 /*
1777 1777 * The code doesn't rely on this (lengths being multiples of 8). See
1778 1778 * comment in dump_bytes.
1779 1779 */
1780 1780 ASSERT0(len % 8);
1781 1781
1782 1782 while (done < len) {
1783 1783 ssize_t resid;
1784 1784
1785 1785 ra->err = vn_rdwr(UIO_READ, ra->vp,
1786 1786 (char *)buf + done, len - done,
1787 1787 ra->voff, UIO_SYSSPACE, FAPPEND,
1788 1788 RLIM64_INFINITY, CRED(), &resid);
1789 1789
1790 1790 if (resid == len - done) {
1791 1791 /*
1792 1792 * Note: ECKSUM indicates that the receive
1793 1793 * was interrupted and can potentially be resumed.
1794 1794 */
1795 1795 ra->err = SET_ERROR(ECKSUM);
1796 1796 }
1797 1797 ra->voff += len - done - resid;
1798 1798 done = len - resid;
1799 1799 if (ra->err != 0)
1800 1800 return (ra->err);
1801 1801 }
1802 1802
1803 1803 ra->bytes_read += len;
1804 1804
1805 1805 ASSERT3U(done, ==, len);
1806 1806 return (0);
1807 1807 }
1808 1808
1809 1809 static void
1810 1810 byteswap_record(dmu_replay_record_t *drr)
1811 1811 {
1812 1812 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1813 1813 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1814 1814 drr->drr_type = BSWAP_32(drr->drr_type);
1815 1815 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1816 1816
1817 1817 switch (drr->drr_type) {
1818 1818 case DRR_BEGIN:
1819 1819 DO64(drr_begin.drr_magic);
1820 1820 DO64(drr_begin.drr_versioninfo);
1821 1821 DO64(drr_begin.drr_creation_time);
1822 1822 DO32(drr_begin.drr_type);
1823 1823 DO32(drr_begin.drr_flags);
1824 1824 DO64(drr_begin.drr_toguid);
1825 1825 DO64(drr_begin.drr_fromguid);
1826 1826 break;
1827 1827 case DRR_OBJECT:
1828 1828 DO64(drr_object.drr_object);
1829 1829 DO32(drr_object.drr_type);
1830 1830 DO32(drr_object.drr_bonustype);
1831 1831 DO32(drr_object.drr_blksz);
1832 1832 DO32(drr_object.drr_bonuslen);
1833 1833 DO64(drr_object.drr_toguid);
1834 1834 break;
1835 1835 case DRR_FREEOBJECTS:
1836 1836 DO64(drr_freeobjects.drr_firstobj);
1837 1837 DO64(drr_freeobjects.drr_numobjs);
1838 1838 DO64(drr_freeobjects.drr_toguid);
1839 1839 break;
1840 1840 case DRR_WRITE:
1841 1841 DO64(drr_write.drr_object);
1842 1842 DO32(drr_write.drr_type);
1843 1843 DO64(drr_write.drr_offset);
1844 1844 DO64(drr_write.drr_length);
1845 1845 DO64(drr_write.drr_toguid);
1846 1846 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
1847 1847 DO64(drr_write.drr_key.ddk_prop);
1848 1848 break;
1849 1849 case DRR_WRITE_BYREF:
1850 1850 DO64(drr_write_byref.drr_object);
1851 1851 DO64(drr_write_byref.drr_offset);
1852 1852 DO64(drr_write_byref.drr_length);
1853 1853 DO64(drr_write_byref.drr_toguid);
1854 1854 DO64(drr_write_byref.drr_refguid);
1855 1855 DO64(drr_write_byref.drr_refobject);
1856 1856 DO64(drr_write_byref.drr_refoffset);
1857 1857 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
1858 1858 drr_key.ddk_cksum);
1859 1859 DO64(drr_write_byref.drr_key.ddk_prop);
1860 1860 break;
1861 1861 case DRR_WRITE_EMBEDDED:
1862 1862 DO64(drr_write_embedded.drr_object);
1863 1863 DO64(drr_write_embedded.drr_offset);
1864 1864 DO64(drr_write_embedded.drr_length);
1865 1865 DO64(drr_write_embedded.drr_toguid);
1866 1866 DO32(drr_write_embedded.drr_lsize);
1867 1867 DO32(drr_write_embedded.drr_psize);
1868 1868 break;
1869 1869 case DRR_FREE:
1870 1870 DO64(drr_free.drr_object);
1871 1871 DO64(drr_free.drr_offset);
1872 1872 DO64(drr_free.drr_length);
1873 1873 DO64(drr_free.drr_toguid);
1874 1874 break;
1875 1875 case DRR_SPILL:
1876 1876 DO64(drr_spill.drr_object);
1877 1877 DO64(drr_spill.drr_length);
1878 1878 DO64(drr_spill.drr_toguid);
1879 1879 break;
1880 1880 case DRR_END:
1881 1881 DO64(drr_end.drr_toguid);
1882 1882 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1883 1883 break;
1884 1884 }
1885 1885
1886 1886 if (drr->drr_type != DRR_BEGIN) {
1887 1887 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
1888 1888 }
1889 1889
1890 1890 #undef DO64
1891 1891 #undef DO32
1892 1892 }
1893 1893
1894 1894 static inline uint8_t
1895 1895 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1896 1896 {
1897 1897 if (bonus_type == DMU_OT_SA) {
1898 1898 return (1);
1899 1899 } else {
1900 1900 return (1 +
1901 1901 ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1902 1902 }
1903 1903 }
1904 1904
1905 1905 static void
1906 1906 save_resume_state(struct receive_writer_arg *rwa,
1907 1907 uint64_t object, uint64_t offset, dmu_tx_t *tx)
1908 1908 {
1909 1909 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1910 1910
1911 1911 if (!rwa->resumable)
1912 1912 return;
1913 1913
1914 1914 /*
1915 1915 * We use ds_resume_bytes[] != 0 to indicate that we need to
1916 1916 * update this on disk, so it must not be 0.
1917 1917 */
1918 1918 ASSERT(rwa->bytes_read != 0);
1919 1919
1920 1920 /*
1921 1921 * We only resume from write records, which have a valid
1922 1922 * (non-meta-dnode) object number.
1923 1923 */
1924 1924 ASSERT(object != 0);
1925 1925
1926 1926 /*
1927 1927 * For resuming to work correctly, we must receive records in order,
1928 1928 * sorted by object,offset. This is checked by the callers, but
1929 1929 * assert it here for good measure.
1930 1930 */
1931 1931 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
1932 1932 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
1933 1933 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
1934 1934 ASSERT3U(rwa->bytes_read, >=,
1935 1935 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
1936 1936
1937 1937 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
1938 1938 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
1939 1939 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
1940 1940 }
1941 1941
1942 1942 static int
1943 1943 receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1944 1944 void *data)
1945 1945 {
1946 1946 dmu_object_info_t doi;
1947 1947 dmu_tx_t *tx;
1948 1948 uint64_t object;
1949 1949 int err;
1950 1950
1951 1951 if (drro->drr_type == DMU_OT_NONE ||
1952 1952 !DMU_OT_IS_VALID(drro->drr_type) ||
1953 1953 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
1954 1954 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1955 1955 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1956 1956 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1957 1957 drro->drr_blksz < SPA_MINBLOCKSIZE ||
1958 1958 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1959 1959 drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1960 1960 return (SET_ERROR(EINVAL));
1961 1961 }
1962 1962
1963 1963 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
1964 1964
1965 1965 if (err != 0 && err != ENOENT)
1966 1966 return (SET_ERROR(EINVAL));
1967 1967 object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
1968 1968
1969 1969 /*
1970 1970 * If we are losing blkptrs or changing the block size this must
1971 1971 * be a new file instance. We must clear out the previous file
1972 1972 * contents before we can change this type of metadata in the dnode.
1973 1973 */
1974 1974 if (err == 0) {
1975 1975 int nblkptr;
1976 1976
1977 1977 nblkptr = deduce_nblkptr(drro->drr_bonustype,
1978 1978 drro->drr_bonuslen);
1979 1979
1980 1980 if (drro->drr_blksz != doi.doi_data_block_size ||
1981 1981 nblkptr < doi.doi_nblkptr) {
1982 1982 err = dmu_free_long_range(rwa->os, drro->drr_object,
1983 1983 0, DMU_OBJECT_END);
1984 1984 if (err != 0)
1985 1985 return (SET_ERROR(EINVAL));
1986 1986 }
1987 1987 }
1988 1988
1989 1989 tx = dmu_tx_create(rwa->os);
1990 1990 dmu_tx_hold_bonus(tx, object);
1991 1991 err = dmu_tx_assign(tx, TXG_WAIT);
1992 1992 if (err != 0) {
1993 1993 dmu_tx_abort(tx);
1994 1994 return (err);
1995 1995 }
1996 1996
1997 1997 if (object == DMU_NEW_OBJECT) {
1998 1998 /* currently free, want to be allocated */
1999 1999 err = dmu_object_claim(rwa->os, drro->drr_object,
2000 2000 drro->drr_type, drro->drr_blksz,
2001 2001 drro->drr_bonustype, drro->drr_bonuslen, tx);
2002 2002 } else if (drro->drr_type != doi.doi_type ||
2003 2003 drro->drr_blksz != doi.doi_data_block_size ||
2004 2004 drro->drr_bonustype != doi.doi_bonus_type ||
2005 2005 drro->drr_bonuslen != doi.doi_bonus_size) {
2006 2006 /* currently allocated, but with different properties */
2007 2007 err = dmu_object_reclaim(rwa->os, drro->drr_object,
2008 2008 drro->drr_type, drro->drr_blksz,
2009 2009 drro->drr_bonustype, drro->drr_bonuslen, tx);
2010 2010 }
2011 2011 if (err != 0) {
2012 2012 dmu_tx_commit(tx);
2013 2013 return (SET_ERROR(EINVAL));
2014 2014 }
2015 2015
2016 2016 dmu_object_set_checksum(rwa->os, drro->drr_object,
2017 2017 drro->drr_checksumtype, tx);
2018 2018 dmu_object_set_compress(rwa->os, drro->drr_object,
2019 2019 drro->drr_compress, tx);
2020 2020
2021 2021 if (data != NULL) {
2022 2022 dmu_buf_t *db;
2023 2023
2024 2024 VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2025 2025 dmu_buf_will_dirty(db, tx);
2026 2026
2027 2027 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2028 2028 bcopy(data, db->db_data, drro->drr_bonuslen);
2029 2029 if (rwa->byteswap) {
2030 2030 dmu_object_byteswap_t byteswap =
2031 2031 DMU_OT_BYTESWAP(drro->drr_bonustype);
2032 2032 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2033 2033 drro->drr_bonuslen);
2034 2034 }
2035 2035 dmu_buf_rele(db, FTAG);
2036 2036 }
2037 2037 dmu_tx_commit(tx);
2038 2038
2039 2039 return (0);
2040 2040 }
2041 2041
2042 2042 /* ARGSUSED */
2043 2043 static int
2044 2044 receive_freeobjects(struct receive_writer_arg *rwa,
2045 2045 struct drr_freeobjects *drrfo)
2046 2046 {
2047 2047 uint64_t obj;
2048 2048 int next_err = 0;
2049 2049
2050 2050 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2051 2051 return (SET_ERROR(EINVAL));
2052 2052
2053 2053 for (obj = drrfo->drr_firstobj;
2054 2054 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2055 2055 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2056 2056 int err;
2057 2057
2058 2058 if (dmu_object_info(rwa->os, obj, NULL) != 0)
2059 2059 continue;
2060 2060
2061 2061 err = dmu_free_long_object(rwa->os, obj);
2062 2062 if (err != 0)
2063 2063 return (err);
2064 2064 }
2065 2065 if (next_err != ESRCH)
2066 2066 return (next_err);
2067 2067 return (0);
2068 2068 }
2069 2069
2070 2070 static int
2071 2071 receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2072 2072 arc_buf_t *abuf)
2073 2073 {
2074 2074 dmu_tx_t *tx;
2075 2075 int err;
2076 2076
2077 2077 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2078 2078 !DMU_OT_IS_VALID(drrw->drr_type))
2079 2079 return (SET_ERROR(EINVAL));
2080 2080
2081 2081 /*
2082 2082 * For resuming to work, records must be in increasing order
2083 2083 * by (object, offset).
2084 2084 */
2085 2085 if (drrw->drr_object < rwa->last_object ||
2086 2086 (drrw->drr_object == rwa->last_object &&
2087 2087 drrw->drr_offset < rwa->last_offset)) {
2088 2088 return (SET_ERROR(EINVAL));
2089 2089 }
2090 2090 rwa->last_object = drrw->drr_object;
2091 2091 rwa->last_offset = drrw->drr_offset;
2092 2092
2093 2093 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
2094 2094 return (SET_ERROR(EINVAL));
2095 2095
2096 2096 tx = dmu_tx_create(rwa->os);
2097 2097
2098 2098 dmu_tx_hold_write(tx, drrw->drr_object,
2099 2099 drrw->drr_offset, drrw->drr_length);
2100 2100 err = dmu_tx_assign(tx, TXG_WAIT);
2101 2101 if (err != 0) {
2102 2102 dmu_tx_abort(tx);
2103 2103 return (err);
2104 2104 }
2105 2105 if (rwa->byteswap) {
2106 2106 dmu_object_byteswap_t byteswap =
2107 2107 DMU_OT_BYTESWAP(drrw->drr_type);
2108 2108 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2109 2109 drrw->drr_length);
2110 2110 }
2111 2111
2112 2112 dmu_buf_t *bonus;
2113 2113 if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
2114 2114 return (SET_ERROR(EINVAL));
2115 2115 dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
2116 2116
2117 2117 /*
2118 2118 * Note: If the receive fails, we want the resume stream to start
2119 2119 * with the same record that we last successfully received (as opposed
2120 2120 * to the next record), so that we can verify that we are
2121 2121 * resuming from the correct location.
2122 2122 */
2123 2123 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2124 2124 dmu_tx_commit(tx);
2125 2125 dmu_buf_rele(bonus, FTAG);
2126 2126
2127 2127 return (0);
2128 2128 }
2129 2129
2130 2130 /*
2131 2131 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2132 2132 * streams to refer to a copy of the data that is already on the
2133 2133 * system because it came in earlier in the stream. This function
2134 2134 * finds the earlier copy of the data, and uses that copy instead of
2135 2135 * data from the stream to fulfill this write.
2136 2136 */
2137 2137 static int
2138 2138 receive_write_byref(struct receive_writer_arg *rwa,
2139 2139 struct drr_write_byref *drrwbr)
2140 2140 {
2141 2141 dmu_tx_t *tx;
2142 2142 int err;
2143 2143 guid_map_entry_t gmesrch;
2144 2144 guid_map_entry_t *gmep;
2145 2145 avl_index_t where;
2146 2146 objset_t *ref_os = NULL;
2147 2147 dmu_buf_t *dbp;
2148 2148
2149 2149 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2150 2150 return (SET_ERROR(EINVAL));
2151 2151
2152 2152 /*
2153 2153 * If the GUID of the referenced dataset is different from the
2154 2154 * GUID of the target dataset, find the referenced dataset.
2155 2155 */
2156 2156 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2157 2157 gmesrch.guid = drrwbr->drr_refguid;
2158 2158 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
2159 2159 &where)) == NULL) {
2160 2160 return (SET_ERROR(EINVAL));
2161 2161 }
2162 2162 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2163 2163 return (SET_ERROR(EINVAL));
2164 2164 } else {
2165 2165 ref_os = rwa->os;
2166 2166 }
2167 2167
2168 2168 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
2169 2169 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
2170 2170 if (err != 0)
2171 2171 return (err);
2172 2172
2173 2173 tx = dmu_tx_create(rwa->os);
2174 2174
2175 2175 dmu_tx_hold_write(tx, drrwbr->drr_object,
2176 2176 drrwbr->drr_offset, drrwbr->drr_length);
2177 2177 err = dmu_tx_assign(tx, TXG_WAIT);
2178 2178 if (err != 0) {
2179 2179 dmu_tx_abort(tx);
2180 2180 return (err);
2181 2181 }
2182 2182 dmu_write(rwa->os, drrwbr->drr_object,
2183 2183 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2184 2184 dmu_buf_rele(dbp, FTAG);
2185 2185
2186 2186 /* See comment in restore_write. */
2187 2187 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
2188 2188 dmu_tx_commit(tx);
2189 2189 return (0);
2190 2190 }
2191 2191
2192 2192 static int
2193 2193 receive_write_embedded(struct receive_writer_arg *rwa,
2194 2194 struct drr_write_embedded *drrwe, void *data)
2195 2195 {
2196 2196 dmu_tx_t *tx;
2197 2197 int err;
2198 2198
2199 2199 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2200 2200 return (EINVAL);
2201 2201
2202 2202 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2203 2203 return (EINVAL);
2204 2204
2205 2205 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2206 2206 return (EINVAL);
2207 2207 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2208 2208 return (EINVAL);
2209 2209
2210 2210 tx = dmu_tx_create(rwa->os);
2211 2211
2212 2212 dmu_tx_hold_write(tx, drrwe->drr_object,
2213 2213 drrwe->drr_offset, drrwe->drr_length);
2214 2214 err = dmu_tx_assign(tx, TXG_WAIT);
2215 2215 if (err != 0) {
2216 2216 dmu_tx_abort(tx);
2217 2217 return (err);
2218 2218 }
2219 2219
2220 2220 dmu_write_embedded(rwa->os, drrwe->drr_object,
2221 2221 drrwe->drr_offset, data, drrwe->drr_etype,
2222 2222 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2223 2223 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2224 2224
2225 2225 /* See comment in restore_write. */
2226 2226 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2227 2227 dmu_tx_commit(tx);
2228 2228 return (0);
2229 2229 }
2230 2230
2231 2231 static int
2232 2232 receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2233 2233 void *data)
2234 2234 {
2235 2235 dmu_tx_t *tx;
2236 2236 dmu_buf_t *db, *db_spill;
2237 2237 int err;
2238 2238
2239 2239 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2240 2240 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2241 2241 return (SET_ERROR(EINVAL));
2242 2242
2243 2243 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2244 2244 return (SET_ERROR(EINVAL));
2245 2245
2246 2246 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2247 2247 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2248 2248 dmu_buf_rele(db, FTAG);
2249 2249 return (err);
2250 2250 }
2251 2251
2252 2252 tx = dmu_tx_create(rwa->os);
2253 2253
2254 2254 dmu_tx_hold_spill(tx, db->db_object);
2255 2255
2256 2256 err = dmu_tx_assign(tx, TXG_WAIT);
2257 2257 if (err != 0) {
2258 2258 dmu_buf_rele(db, FTAG);
2259 2259 dmu_buf_rele(db_spill, FTAG);
2260 2260 dmu_tx_abort(tx);
2261 2261 return (err);
2262 2262 }
2263 2263 dmu_buf_will_dirty(db_spill, tx);
2264 2264
2265 2265 if (db_spill->db_size < drrs->drr_length)
2266 2266 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2267 2267 drrs->drr_length, tx));
2268 2268 bcopy(data, db_spill->db_data, drrs->drr_length);
2269 2269
2270 2270 dmu_buf_rele(db, FTAG);
2271 2271 dmu_buf_rele(db_spill, FTAG);
2272 2272
2273 2273 dmu_tx_commit(tx);
2274 2274 return (0);
2275 2275 }
2276 2276
2277 2277 /* ARGSUSED */
2278 2278 static int
2279 2279 receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2280 2280 {
2281 2281 int err;
2282 2282
2283 2283 if (drrf->drr_length != -1ULL &&
2284 2284 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2285 2285 return (SET_ERROR(EINVAL));
2286 2286
2287 2287 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2288 2288 return (SET_ERROR(EINVAL));
2289 2289
2290 2290 err = dmu_free_long_range(rwa->os, drrf->drr_object,
2291 2291 drrf->drr_offset, drrf->drr_length);
2292 2292
2293 2293 return (err);
2294 2294 }
2295 2295
2296 2296 /* used to destroy the drc_ds on error */
2297 2297 static void
2298 2298 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2299 2299 {
2300 2300 if (drc->drc_resumable) {
2301 2301 /* wait for our resume state to be written to disk */
2302 2302 txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2303 2303 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2304 2304 } else {
2305 2305 char name[ZFS_MAX_DATASET_NAME_LEN];
2306 2306 dsl_dataset_name(drc->drc_ds, name);
2307 2307 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2308 2308 (void) dsl_destroy_head(name);
2309 2309 }
2310 2310 }
2311 2311
2312 2312 static void
2313 2313 receive_cksum(struct receive_arg *ra, int len, void *buf)
2314 2314 {
2315 2315 if (ra->byteswap) {
2316 2316 fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
2317 2317 } else {
2318 2318 fletcher_4_incremental_native(buf, len, &ra->cksum);
2319 2319 }
2320 2320 }
2321 2321
2322 2322 /*
2323 2323 * Read the payload into a buffer of size len, and update the current record's
2324 2324 * payload field.
2325 2325 * Allocate ra->next_rrd and read the next record's header into
2326 2326 * ra->next_rrd->header.
2327 2327 * Verify checksum of payload and next record.
2328 2328 */
2329 2329 static int
2330 2330 receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
2331 2331 {
2332 2332 int err;
2333 2333
2334 2334 if (len != 0) {
2335 2335 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2336 2336 err = receive_read(ra, len, buf);
2337 2337 if (err != 0)
2338 2338 return (err);
2339 2339 receive_cksum(ra, len, buf);
2340 2340
2341 2341 /* note: rrd is NULL when reading the begin record's payload */
2342 2342 if (ra->rrd != NULL) {
2343 2343 ra->rrd->payload = buf;
2344 2344 ra->rrd->payload_size = len;
2345 2345 ra->rrd->bytes_read = ra->bytes_read;
2346 2346 }
2347 2347 }
2348 2348
2349 2349 ra->prev_cksum = ra->cksum;
2350 2350
2351 2351 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2352 2352 err = receive_read(ra, sizeof (ra->next_rrd->header),
2353 2353 &ra->next_rrd->header);
2354 2354 ra->next_rrd->bytes_read = ra->bytes_read;
2355 2355 if (err != 0) {
2356 2356 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2357 2357 ra->next_rrd = NULL;
2358 2358 return (err);
2359 2359 }
2360 2360 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2361 2361 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2362 2362 ra->next_rrd = NULL;
2363 2363 return (SET_ERROR(EINVAL));
2364 2364 }
2365 2365
2366 2366 /*
2367 2367 * Note: checksum is of everything up to but not including the
2368 2368 * checksum itself.
2369 2369 */
2370 2370 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2371 2371 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2372 2372 receive_cksum(ra,
2373 2373 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2374 2374 &ra->next_rrd->header);
2375 2375
2376 2376 zio_cksum_t cksum_orig =
2377 2377 ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2378 2378 zio_cksum_t *cksump =
2379 2379 &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2380 2380
2381 2381 if (ra->byteswap)
2382 2382 byteswap_record(&ra->next_rrd->header);
2383 2383
2384 2384 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2385 2385 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2386 2386 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2387 2387 ra->next_rrd = NULL;
2388 2388 return (SET_ERROR(ECKSUM));
2389 2389 }
2390 2390
2391 2391 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
2392 2392
2393 2393 return (0);
2394 2394 }
2395 2395
2396 2396 static void
2397 2397 objlist_create(struct objlist *list)
2398 2398 {
2399 2399 list_create(&list->list, sizeof (struct receive_objnode),
2400 2400 offsetof(struct receive_objnode, node));
2401 2401 list->last_lookup = 0;
2402 2402 }
2403 2403
2404 2404 static void
2405 2405 objlist_destroy(struct objlist *list)
2406 2406 {
2407 2407 for (struct receive_objnode *n = list_remove_head(&list->list);
2408 2408 n != NULL; n = list_remove_head(&list->list)) {
2409 2409 kmem_free(n, sizeof (*n));
2410 2410 }
2411 2411 list_destroy(&list->list);
2412 2412 }
2413 2413
2414 2414 /*
2415 2415 * This function looks through the objlist to see if the specified object number
2416 2416 * is contained in the objlist. In the process, it will remove all object
2417 2417 * numbers in the list that are smaller than the specified object number. Thus,
2418 2418 * any lookup of an object number smaller than a previously looked up object
2419 2419 * number will always return false; therefore, all lookups should be done in
2420 2420 * ascending order.
2421 2421 */
2422 2422 static boolean_t
2423 2423 objlist_exists(struct objlist *list, uint64_t object)
2424 2424 {
2425 2425 struct receive_objnode *node = list_head(&list->list);
2426 2426 ASSERT3U(object, >=, list->last_lookup);
2427 2427 list->last_lookup = object;
2428 2428 while (node != NULL && node->object < object) {
2429 2429 VERIFY3P(node, ==, list_remove_head(&list->list));
2430 2430 kmem_free(node, sizeof (*node));
2431 2431 node = list_head(&list->list);
2432 2432 }
2433 2433 return (node != NULL && node->object == object);
2434 2434 }
2435 2435
2436 2436 /*
2437 2437 * The objlist is a list of object numbers stored in ascending order. However,
2438 2438 * the insertion of new object numbers does not seek out the correct location to
2439 2439 * store a new object number; instead, it appends it to the list for simplicity.
2440 2440 * Thus, any users must take care to only insert new object numbers in ascending
2441 2441 * order.
2442 2442 */
2443 2443 static void
2444 2444 objlist_insert(struct objlist *list, uint64_t object)
2445 2445 {
2446 2446 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2447 2447 node->object = object;
2448 2448 #ifdef ZFS_DEBUG
2449 2449 struct receive_objnode *last_object = list_tail(&list->list);
2450 2450 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2451 2451 ASSERT3U(node->object, >, last_objnum);
2452 2452 #endif
2453 2453 list_insert_tail(&list->list, node);
2454 2454 }
2455 2455
2456 2456 /*
2457 2457 * Issue the prefetch reads for any necessary indirect blocks.
2458 2458 *
2459 2459 * We use the object ignore list to tell us whether or not to issue prefetches
2460 2460 * for a given object. We do this for both correctness (in case the blocksize
2461 2461 * of an object has changed) and performance (if the object doesn't exist, don't
2462 2462 * needlessly try to issue prefetches). We also trim the list as we go through
2463 2463 * the stream to prevent it from growing to an unbounded size.
2464 2464 *
2465 2465 * The object numbers within will always be in sorted order, and any write
2466 2466 * records we see will also be in sorted order, but they're not sorted with
2467 2467 * respect to each other (i.e. we can get several object records before
2468 2468 * receiving each object's write records). As a result, once we've reached a
2469 2469 * given object number, we can safely remove any reference to lower object
2470 2470 * numbers in the ignore list. In practice, we receive up to 32 object records
2471 2471 * before receiving write records, so the list can have up to 32 nodes in it.
2472 2472 */
2473 2473 /* ARGSUSED */
2474 2474 static void
2475 2475 receive_read_prefetch(struct receive_arg *ra,
2476 2476 uint64_t object, uint64_t offset, uint64_t length)
2477 2477 {
2478 2478 if (!objlist_exists(&ra->ignore_objlist, object)) {
2479 2479 dmu_prefetch(ra->os, object, 1, offset, length,
2480 2480 ZIO_PRIORITY_SYNC_READ);
2481 2481 }
2482 2482 }
2483 2483
2484 2484 /*
2485 2485 * Read records off the stream, issuing any necessary prefetches.
2486 2486 */
2487 2487 static int
2488 2488 receive_read_record(struct receive_arg *ra)
2489 2489 {
2490 2490 int err;
2491 2491
2492 2492 switch (ra->rrd->header.drr_type) {
2493 2493 case DRR_OBJECT:
2494 2494 {
2495 2495 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2496 2496 uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2497 2497 void *buf = kmem_zalloc(size, KM_SLEEP);
2498 2498 dmu_object_info_t doi;
2499 2499 err = receive_read_payload_and_next_header(ra, size, buf);
2500 2500 if (err != 0) {
2501 2501 kmem_free(buf, size);
2502 2502 return (err);
2503 2503 }
2504 2504 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2505 2505 /*
2506 2506 * See receive_read_prefetch for an explanation why we're
2507 2507 * storing this object in the ignore_obj_list.
2508 2508 */
2509 2509 if (err == ENOENT ||
2510 2510 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2511 2511 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2512 2512 err = 0;
2513 2513 }
2514 2514 return (err);
2515 2515 }
2516 2516 case DRR_FREEOBJECTS:
2517 2517 {
2518 2518 err = receive_read_payload_and_next_header(ra, 0, NULL);
2519 2519 return (err);
2520 2520 }
2521 2521 case DRR_WRITE:
2522 2522 {
2523 2523 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2524 2524 arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2525 2525 drrw->drr_length);
2526 2526
2527 2527 err = receive_read_payload_and_next_header(ra,
2528 2528 drrw->drr_length, abuf->b_data);
2529 2529 if (err != 0) {
2530 2530 dmu_return_arcbuf(abuf);
2531 2531 return (err);
2532 2532 }
2533 2533 ra->rrd->write_buf = abuf;
2534 2534 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2535 2535 drrw->drr_length);
2536 2536 return (err);
2537 2537 }
2538 2538 case DRR_WRITE_BYREF:
2539 2539 {
2540 2540 struct drr_write_byref *drrwb =
2541 2541 &ra->rrd->header.drr_u.drr_write_byref;
2542 2542 err = receive_read_payload_and_next_header(ra, 0, NULL);
2543 2543 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2544 2544 drrwb->drr_length);
2545 2545 return (err);
2546 2546 }
2547 2547 case DRR_WRITE_EMBEDDED:
2548 2548 {
2549 2549 struct drr_write_embedded *drrwe =
2550 2550 &ra->rrd->header.drr_u.drr_write_embedded;
2551 2551 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2552 2552 void *buf = kmem_zalloc(size, KM_SLEEP);
2553 2553
2554 2554 err = receive_read_payload_and_next_header(ra, size, buf);
2555 2555 if (err != 0) {
2556 2556 kmem_free(buf, size);
2557 2557 return (err);
2558 2558 }
2559 2559
2560 2560 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2561 2561 drrwe->drr_length);
2562 2562 return (err);
2563 2563 }
2564 2564 case DRR_FREE:
2565 2565 {
2566 2566 /*
2567 2567 * It might be beneficial to prefetch indirect blocks here, but
2568 2568 * we don't really have the data to decide for sure.
2569 2569 */
2570 2570 err = receive_read_payload_and_next_header(ra, 0, NULL);
2571 2571 return (err);
2572 2572 }
2573 2573 case DRR_END:
2574 2574 {
2575 2575 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2576 2576 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2577 2577 return (SET_ERROR(ECKSUM));
2578 2578 return (0);
2579 2579 }
2580 2580 case DRR_SPILL:
2581 2581 {
2582 2582 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2583 2583 void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2584 2584 err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2585 2585 buf);
2586 2586 if (err != 0)
2587 2587 kmem_free(buf, drrs->drr_length);
2588 2588 return (err);
2589 2589 }
2590 2590 default:
2591 2591 return (SET_ERROR(EINVAL));
2592 2592 }
2593 2593 }
2594 2594
2595 2595 /*
2596 2596 * Commit the records to the pool.
2597 2597 */
2598 2598 static int
2599 2599 receive_process_record(struct receive_writer_arg *rwa,
2600 2600 struct receive_record_arg *rrd)
2601 2601 {
2602 2602 int err;
2603 2603
2604 2604 /* Processing in order, therefore bytes_read should be increasing. */
2605 2605 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2606 2606 rwa->bytes_read = rrd->bytes_read;
2607 2607
2608 2608 switch (rrd->header.drr_type) {
2609 2609 case DRR_OBJECT:
2610 2610 {
2611 2611 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2612 2612 err = receive_object(rwa, drro, rrd->payload);
2613 2613 kmem_free(rrd->payload, rrd->payload_size);
2614 2614 rrd->payload = NULL;
2615 2615 return (err);
2616 2616 }
2617 2617 case DRR_FREEOBJECTS:
2618 2618 {
2619 2619 struct drr_freeobjects *drrfo =
2620 2620 &rrd->header.drr_u.drr_freeobjects;
2621 2621 return (receive_freeobjects(rwa, drrfo));
2622 2622 }
2623 2623 case DRR_WRITE:
2624 2624 {
2625 2625 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2626 2626 err = receive_write(rwa, drrw, rrd->write_buf);
2627 2627 /* if receive_write() is successful, it consumes the arc_buf */
2628 2628 if (err != 0)
2629 2629 dmu_return_arcbuf(rrd->write_buf);
2630 2630 rrd->write_buf = NULL;
2631 2631 rrd->payload = NULL;
2632 2632 return (err);
2633 2633 }
2634 2634 case DRR_WRITE_BYREF:
2635 2635 {
2636 2636 struct drr_write_byref *drrwbr =
2637 2637 &rrd->header.drr_u.drr_write_byref;
2638 2638 return (receive_write_byref(rwa, drrwbr));
2639 2639 }
2640 2640 case DRR_WRITE_EMBEDDED:
2641 2641 {
2642 2642 struct drr_write_embedded *drrwe =
2643 2643 &rrd->header.drr_u.drr_write_embedded;
2644 2644 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2645 2645 kmem_free(rrd->payload, rrd->payload_size);
2646 2646 rrd->payload = NULL;
2647 2647 return (err);
2648 2648 }
2649 2649 case DRR_FREE:
2650 2650 {
2651 2651 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2652 2652 return (receive_free(rwa, drrf));
2653 2653 }
2654 2654 case DRR_SPILL:
2655 2655 {
2656 2656 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2657 2657 err = receive_spill(rwa, drrs, rrd->payload);
2658 2658 kmem_free(rrd->payload, rrd->payload_size);
2659 2659 rrd->payload = NULL;
2660 2660 return (err);
2661 2661 }
2662 2662 default:
2663 2663 return (SET_ERROR(EINVAL));
2664 2664 }
2665 2665 }
2666 2666
2667 2667 /*
2668 2668 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2669 2669 * receive_process_record When we're done, signal the main thread and exit.
2670 2670 */
2671 2671 static void
2672 2672 receive_writer_thread(void *arg)
2673 2673 {
2674 2674 struct receive_writer_arg *rwa = arg;
2675 2675 struct receive_record_arg *rrd;
2676 2676 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2677 2677 rrd = bqueue_dequeue(&rwa->q)) {
2678 2678 /*
2679 2679 * If there's an error, the main thread will stop putting things
2680 2680 * on the queue, but we need to clear everything in it before we
2681 2681 * can exit.
2682 2682 */
2683 2683 if (rwa->err == 0) {
2684 2684 rwa->err = receive_process_record(rwa, rrd);
2685 2685 } else if (rrd->write_buf != NULL) {
2686 2686 dmu_return_arcbuf(rrd->write_buf);
2687 2687 rrd->write_buf = NULL;
2688 2688 rrd->payload = NULL;
2689 2689 } else if (rrd->payload != NULL) {
2690 2690 kmem_free(rrd->payload, rrd->payload_size);
2691 2691 rrd->payload = NULL;
2692 2692 }
2693 2693 kmem_free(rrd, sizeof (*rrd));
2694 2694 }
2695 2695 kmem_free(rrd, sizeof (*rrd));
2696 2696 mutex_enter(&rwa->mutex);
2697 2697 rwa->done = B_TRUE;
2698 2698 cv_signal(&rwa->cv);
2699 2699 mutex_exit(&rwa->mutex);
2700 2700 }
2701 2701
2702 2702 static int
2703 2703 resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2704 2704 {
2705 2705 uint64_t val;
2706 2706 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2707 2707 uint64_t dsobj = dmu_objset_id(ra->os);
2708 2708 uint64_t resume_obj, resume_off;
2709 2709
2710 2710 if (nvlist_lookup_uint64(begin_nvl,
2711 2711 "resume_object", &resume_obj) != 0 ||
2712 2712 nvlist_lookup_uint64(begin_nvl,
2713 2713 "resume_offset", &resume_off) != 0) {
2714 2714 return (SET_ERROR(EINVAL));
2715 2715 }
2716 2716 VERIFY0(zap_lookup(mos, dsobj,
2717 2717 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2718 2718 if (resume_obj != val)
2719 2719 return (SET_ERROR(EINVAL));
2720 2720 VERIFY0(zap_lookup(mos, dsobj,
2721 2721 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2722 2722 if (resume_off != val)
2723 2723 return (SET_ERROR(EINVAL));
2724 2724
2725 2725 return (0);
2726 2726 }
2727 2727
2728 2728 /*
2729 2729 * Read in the stream's records, one by one, and apply them to the pool. There
2730 2730 * are two threads involved; the thread that calls this function will spin up a
2731 2731 * worker thread, read the records off the stream one by one, and issue
2732 2732 * prefetches for any necessary indirect blocks. It will then push the records
2733 2733 * onto an internal blocking queue. The worker thread will pull the records off
2734 2734 * the queue, and actually write the data into the DMU. This way, the worker
2735 2735 * thread doesn't have to wait for reads to complete, since everything it needs
2736 2736 * (the indirect blocks) will be prefetched.
2737 2737 *
2738 2738 * NB: callers *must* call dmu_recv_end() if this succeeds.
2739 2739 */
2740 2740 int
2741 2741 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2742 2742 int cleanup_fd, uint64_t *action_handlep)
2743 2743 {
2744 2744 int err = 0;
2745 2745 struct receive_arg ra = { 0 };
2746 2746 struct receive_writer_arg rwa = { 0 };
2747 2747 int featureflags;
2748 2748 nvlist_t *begin_nvl = NULL;
2749 2749
2750 2750 ra.byteswap = drc->drc_byteswap;
2751 2751 ra.cksum = drc->drc_cksum;
2752 2752 ra.vp = vp;
2753 2753 ra.voff = *voffp;
2754 2754
2755 2755 if (dsl_dataset_is_zapified(drc->drc_ds)) {
2756 2756 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2757 2757 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2758 2758 sizeof (ra.bytes_read), 1, &ra.bytes_read);
2759 2759 }
2760 2760
2761 2761 objlist_create(&ra.ignore_objlist);
2762 2762
2763 2763 /* these were verified in dmu_recv_begin */
2764 2764 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2765 2765 DMU_SUBSTREAM);
2766 2766 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2767 2767
2768 2768 /*
2769 2769 * Open the objset we are modifying.
2770 2770 */
2771 2771 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2772 2772
2773 2773 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2774 2774
2775 2775 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2776 2776
2777 2777 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2778 2778 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2779 2779 minor_t minor;
2780 2780
2781 2781 if (cleanup_fd == -1) {
2782 2782 ra.err = SET_ERROR(EBADF);
2783 2783 goto out;
2784 2784 }
2785 2785 ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2786 2786 if (ra.err != 0) {
2787 2787 cleanup_fd = -1;
2788 2788 goto out;
2789 2789 }
2790 2790
2791 2791 if (*action_handlep == 0) {
2792 2792 rwa.guid_to_ds_map =
2793 2793 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2794 2794 avl_create(rwa.guid_to_ds_map, guid_compare,
2795 2795 sizeof (guid_map_entry_t),
2796 2796 offsetof(guid_map_entry_t, avlnode));
2797 2797 err = zfs_onexit_add_cb(minor,
2798 2798 free_guid_map_onexit, rwa.guid_to_ds_map,
2799 2799 action_handlep);
2800 2800 if (ra.err != 0)
2801 2801 goto out;
2802 2802 } else {
2803 2803 err = zfs_onexit_cb_data(minor, *action_handlep,
2804 2804 (void **)&rwa.guid_to_ds_map);
2805 2805 if (ra.err != 0)
2806 2806 goto out;
2807 2807 }
2808 2808
2809 2809 drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
2810 2810 }
2811 2811
2812 2812 uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
2813 2813 void *payload = NULL;
2814 2814 if (payloadlen != 0)
2815 2815 payload = kmem_alloc(payloadlen, KM_SLEEP);
2816 2816
2817 2817 err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
2818 2818 if (err != 0) {
2819 2819 if (payloadlen != 0)
2820 2820 kmem_free(payload, payloadlen);
2821 2821 goto out;
2822 2822 }
2823 2823 if (payloadlen != 0) {
2824 2824 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2825 2825 kmem_free(payload, payloadlen);
2826 2826 if (err != 0)
2827 2827 goto out;
2828 2828 }
2829 2829
2830 2830 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2831 2831 err = resume_check(&ra, begin_nvl);
2832 2832 if (err != 0)
2833 2833 goto out;
2834 2834 }
2835 2835
2836 2836 (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2837 2837 offsetof(struct receive_record_arg, node));
2838 2838 cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2839 2839 mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2840 2840 rwa.os = ra.os;
2841 2841 rwa.byteswap = drc->drc_byteswap;
2842 2842 rwa.resumable = drc->drc_resumable;
2843 2843
2844 2844 (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
2845 2845 TS_RUN, minclsyspri);
2846 2846 /*
2847 2847 * We're reading rwa.err without locks, which is safe since we are the
2848 2848 * only reader, and the worker thread is the only writer. It's ok if we
2849 2849 * miss a write for an iteration or two of the loop, since the writer
2850 2850 * thread will keep freeing records we send it until we send it an eos
2851 2851 * marker.
2852 2852 *
2853 2853 * We can leave this loop in 3 ways: First, if rwa.err is
2854 2854 * non-zero. In that case, the writer thread will free the rrd we just
2855 2855 * pushed. Second, if we're interrupted; in that case, either it's the
2856 2856 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2857 2857 * has been handed off to the writer thread who will free it. Finally,
2858 2858 * if receive_read_record fails or we're at the end of the stream, then
2859 2859 * we free ra.rrd and exit.
2860 2860 */
2861 2861 while (rwa.err == 0) {
2862 2862 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2863 2863 err = SET_ERROR(EINTR);
2864 2864 break;
2865 2865 }
2866 2866
2867 2867 ASSERT3P(ra.rrd, ==, NULL);
2868 2868 ra.rrd = ra.next_rrd;
2869 2869 ra.next_rrd = NULL;
2870 2870 /* Allocates and loads header into ra.next_rrd */
2871 2871 err = receive_read_record(&ra);
2872 2872
2873 2873 if (ra.rrd->header.drr_type == DRR_END || err != 0) {
2874 2874 kmem_free(ra.rrd, sizeof (*ra.rrd));
2875 2875 ra.rrd = NULL;
2876 2876 break;
2877 2877 }
2878 2878
2879 2879 bqueue_enqueue(&rwa.q, ra.rrd,
2880 2880 sizeof (struct receive_record_arg) + ra.rrd->payload_size);
2881 2881 ra.rrd = NULL;
2882 2882 }
2883 2883 if (ra.next_rrd == NULL)
2884 2884 ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
2885 2885 ra.next_rrd->eos_marker = B_TRUE;
2886 2886 bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
2887 2887
2888 2888 mutex_enter(&rwa.mutex);
2889 2889 while (!rwa.done) {
2890 2890 cv_wait(&rwa.cv, &rwa.mutex);
2891 2891 }
2892 2892 mutex_exit(&rwa.mutex);
2893 2893
2894 2894 cv_destroy(&rwa.cv);
2895 2895 mutex_destroy(&rwa.mutex);
2896 2896 bqueue_destroy(&rwa.q);
2897 2897 if (err == 0)
2898 2898 err = rwa.err;
2899 2899
2900 2900 out:
2901 2901 nvlist_free(begin_nvl);
2902 2902 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2903 2903 zfs_onexit_fd_rele(cleanup_fd);
2904 2904
2905 2905 if (err != 0) {
2906 2906 /*
2907 2907 * Clean up references. If receive is not resumable,
2908 2908 * destroy what we created, so we don't leave it in
2909 2909 * the inconsistent state.
2910 2910 */
2911 2911 dmu_recv_cleanup_ds(drc);
2912 2912 }
2913 2913
2914 2914 *voffp = ra.voff;
2915 2915 objlist_destroy(&ra.ignore_objlist);
2916 2916 return (err);
2917 2917 }
2918 2918
2919 2919 static int
2920 2920 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2921 2921 {
2922 2922 dmu_recv_cookie_t *drc = arg;
2923 2923 dsl_pool_t *dp = dmu_tx_pool(tx);
2924 2924 int error;
2925 2925
2926 2926 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
2927 2927
2928 2928 if (!drc->drc_newfs) {
2929 2929 dsl_dataset_t *origin_head;
2930 2930
2931 2931 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
2932 2932 if (error != 0)
2933 2933 return (error);
2934 2934 if (drc->drc_force) {
2935 2935 /*
2936 2936 * We will destroy any snapshots in tofs (i.e. before
2937 2937 * origin_head) that are after the origin (which is
2938 2938 * the snap before drc_ds, because drc_ds can not
2939 2939 * have any snaps of its own).
2940 2940 */
2941 2941 uint64_t obj;
2942 2942
2943 2943 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2944 2944 while (obj !=
2945 2945 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
2946 2946 dsl_dataset_t *snap;
2947 2947 error = dsl_dataset_hold_obj(dp, obj, FTAG,
2948 2948 &snap);
2949 2949 if (error != 0)
2950 2950 break;
2951 2951 if (snap->ds_dir != origin_head->ds_dir)
2952 2952 error = SET_ERROR(EINVAL);
2953 2953 if (error == 0) {
2954 2954 error = dsl_destroy_snapshot_check_impl(
2955 2955 snap, B_FALSE);
2956 2956 }
2957 2957 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
2958 2958 dsl_dataset_rele(snap, FTAG);
2959 2959 if (error != 0)
2960 2960 break;
2961 2961 }
2962 2962 if (error != 0) {
2963 2963 dsl_dataset_rele(origin_head, FTAG);
2964 2964 return (error);
2965 2965 }
2966 2966 }
2967 2967 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
2968 2968 origin_head, drc->drc_force, drc->drc_owner, tx);
2969 2969 if (error != 0) {
2970 2970 dsl_dataset_rele(origin_head, FTAG);
2971 2971 return (error);
2972 2972 }
2973 2973 error = dsl_dataset_snapshot_check_impl(origin_head,
2974 2974 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
2975 2975 dsl_dataset_rele(origin_head, FTAG);
2976 2976 if (error != 0)
2977 2977 return (error);
2978 2978
2979 2979 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
2980 2980 } else {
2981 2981 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2982 2982 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
2983 2983 }
2984 2984 return (error);
2985 2985 }
2986 2986
2987 2987 static void
2988 2988 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
2989 2989 {
2990 2990 dmu_recv_cookie_t *drc = arg;
2991 2991 dsl_pool_t *dp = dmu_tx_pool(tx);
2992 2992
2993 2993 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
2994 2994 tx, "snap=%s", drc->drc_tosnap);
2995 2995
2996 2996 if (!drc->drc_newfs) {
2997 2997 dsl_dataset_t *origin_head;
2998 2998
2999 2999 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3000 3000 &origin_head));
3001 3001
3002 3002 if (drc->drc_force) {
3003 3003 /*
3004 3004 * Destroy any snapshots of drc_tofs (origin_head)
3005 3005 * after the origin (the snap before drc_ds).
3006 3006 */
3007 3007 uint64_t obj;
3008 3008
3009 3009 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3010 3010 while (obj !=
3011 3011 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3012 3012 dsl_dataset_t *snap;
3013 3013 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3014 3014 &snap));
3015 3015 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3016 3016 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3017 3017 dsl_destroy_snapshot_sync_impl(snap,
3018 3018 B_FALSE, tx);
3019 3019 dsl_dataset_rele(snap, FTAG);
3020 3020 }
3021 3021 }
3022 3022 VERIFY3P(drc->drc_ds->ds_prev, ==,
3023 3023 origin_head->ds_prev);
3024 3024
3025 3025 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3026 3026 origin_head, tx);
3027 3027 dsl_dataset_snapshot_sync_impl(origin_head,
3028 3028 drc->drc_tosnap, tx);
3029 3029
3030 3030 /* set snapshot's creation time and guid */
3031 3031 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3032 3032 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3033 3033 drc->drc_drrb->drr_creation_time;
3034 3034 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3035 3035 drc->drc_drrb->drr_toguid;
3036 3036 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3037 3037 ~DS_FLAG_INCONSISTENT;
3038 3038
3039 3039 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3040 3040 dsl_dataset_phys(origin_head)->ds_flags &=
3041 3041 ~DS_FLAG_INCONSISTENT;
3042 3042
3043 3043 dsl_dataset_rele(origin_head, FTAG);
3044 3044 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3045 3045
3046 3046 if (drc->drc_owner != NULL)
3047 3047 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3048 3048 } else {
3049 3049 dsl_dataset_t *ds = drc->drc_ds;
3050 3050
3051 3051 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3052 3052
3053 3053 /* set snapshot's creation time and guid */
3054 3054 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3055 3055 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3056 3056 drc->drc_drrb->drr_creation_time;
3057 3057 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3058 3058 drc->drc_drrb->drr_toguid;
3059 3059 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3060 3060 ~DS_FLAG_INCONSISTENT;
3061 3061
3062 3062 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3063 3063 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3064 3064 if (dsl_dataset_has_resume_receive_state(ds)) {
3065 3065 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3066 3066 DS_FIELD_RESUME_FROMGUID, tx);
3067 3067 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3068 3068 DS_FIELD_RESUME_OBJECT, tx);
3069 3069 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3070 3070 DS_FIELD_RESUME_OFFSET, tx);
3071 3071 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3072 3072 DS_FIELD_RESUME_BYTES, tx);
3073 3073 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3074 3074 DS_FIELD_RESUME_TOGUID, tx);
3075 3075 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3076 3076 DS_FIELD_RESUME_TONAME, tx);
3077 3077 }
3078 3078 }
3079 3079 drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3080 3080 /*
3081 3081 * Release the hold from dmu_recv_begin. This must be done before
3082 3082 * we return to open context, so that when we free the dataset's dnode,
3083 3083 * we can evict its bonus buffer.
3084 3084 */
3085 3085 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3086 3086 drc->drc_ds = NULL;
3087 3087 }
3088 3088
3089 3089 static int
3090 3090 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3091 3091 {
3092 3092 dsl_pool_t *dp;
3093 3093 dsl_dataset_t *snapds;
3094 3094 guid_map_entry_t *gmep;
3095 3095 int err;
3096 3096
3097 3097 ASSERT(guid_map != NULL);
3098 3098
3099 3099 err = dsl_pool_hold(name, FTAG, &dp);
3100 3100 if (err != 0)
3101 3101 return (err);
3102 3102 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3103 3103 err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3104 3104 if (err == 0) {
3105 3105 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3106 3106 gmep->gme_ds = snapds;
3107 3107 avl_add(guid_map, gmep);
3108 3108 dsl_dataset_long_hold(snapds, gmep);
3109 3109 } else {
3110 3110 kmem_free(gmep, sizeof (*gmep));
3111 3111 }
3112 3112
3113 3113 dsl_pool_rele(dp, FTAG);
3114 3114 return (err);
3115 3115 }
3116 3116
3117 3117 static int dmu_recv_end_modified_blocks = 3;
3118 3118
3119 3119 static int
3120 3120 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3121 3121 {
3122 3122 int error;
3123 3123
3124 3124 #ifdef _KERNEL
3125 3125 /*
3126 3126 * We will be destroying the ds; make sure its origin is unmounted if
3127 3127 * necessary.
3128 3128 */
3129 3129 char name[ZFS_MAX_DATASET_NAME_LEN];
3130 3130 dsl_dataset_name(drc->drc_ds, name);
3131 3131 zfs_destroy_unmount_origin(name);
3132 3132 #endif
3133 3133
3134 3134 error = dsl_sync_task(drc->drc_tofs,
3135 3135 dmu_recv_end_check, dmu_recv_end_sync, drc,
3136 3136 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3137 3137
3138 3138 if (error != 0)
3139 3139 dmu_recv_cleanup_ds(drc);
3140 3140 return (error);
3141 3141 }
3142 3142
3143 3143 static int
3144 3144 dmu_recv_new_end(dmu_recv_cookie_t *drc)
3145 3145 {
3146 3146 int error;
3147 3147
3148 3148 error = dsl_sync_task(drc->drc_tofs,
3149 3149 dmu_recv_end_check, dmu_recv_end_sync, drc,
3150 3150 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3151 3151
3152 3152 if (error != 0) {
3153 3153 dmu_recv_cleanup_ds(drc);
3154 3154 } else if (drc->drc_guid_to_ds_map != NULL) {
3155 3155 (void) add_ds_to_guidmap(drc->drc_tofs,
3156 3156 drc->drc_guid_to_ds_map,
3157 3157 drc->drc_newsnapobj);
3158 3158 }
3159 3159 return (error);
3160 3160 }
3161 3161
3162 3162 int
3163 3163 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3164 3164 {
3165 3165 drc->drc_owner = owner;
3166 3166
3167 3167 if (drc->drc_newfs)
3168 3168 return (dmu_recv_new_end(drc));
3169 3169 else
3170 3170 return (dmu_recv_existing_end(drc));
3171 3171 }
3172 3172
3173 3173 /*
3174 3174 * Return TRUE if this objset is currently being received into.
3175 3175 */
3176 3176 boolean_t
3177 3177 dmu_objset_is_receiving(objset_t *os)
3178 3178 {
3179 3179 return (os->os_dsl_dataset != NULL &&
3180 3180 os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3181 3181 }
↓ open down ↓ |
2458 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX