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