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