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