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 2014 HybridCluster. All rights reserved. 27 */ 28 29 #include <sys/dmu.h> 30 #include <sys/dmu_impl.h> 31 #include <sys/dmu_tx.h> 32 #include <sys/dbuf.h> 33 #include <sys/dnode.h> 34 #include <sys/zfs_context.h> 35 #include <sys/dmu_objset.h> 36 #include <sys/dmu_traverse.h> 37 #include <sys/dsl_dataset.h> 38 #include <sys/dsl_dir.h> 39 #include <sys/dsl_prop.h> 40 #include <sys/dsl_pool.h> 41 #include <sys/dsl_synctask.h> 42 #include <sys/zfs_ioctl.h> 43 #include <sys/zap.h> 44 #include <sys/zio_checksum.h> 45 #include <sys/zfs_znode.h> 46 #include <zfs_fletcher.h> 47 #include <sys/avl.h> 48 #include <sys/ddt.h> 49 #include <sys/zfs_onexit.h> 50 #include <sys/dmu_send.h> 51 #include <sys/dsl_destroy.h> 52 #include <sys/blkptr.h> 53 #include <sys/dsl_bookmark.h> 54 #include <sys/zfeature.h> 55 56 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */ 57 int zfs_send_corrupt_data = B_FALSE; 58 59 static char *dmu_recv_tag = "dmu_recv_tag"; 60 static const char *recv_clone_name = "%recv"; 61 62 static int 63 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len) 64 { 65 dsl_dataset_t *ds = dsp->dsa_os->os_dsl_dataset; 66 ssize_t resid; /* have to get resid to get detailed errno */ 67 ASSERT0(len % 8); 68 69 fletcher_4_incremental_native(buf, len, &dsp->dsa_zc); 70 dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp, 71 (caddr_t)buf, len, 72 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid); 73 74 mutex_enter(&ds->ds_sendstream_lock); 75 *dsp->dsa_off += len; 76 mutex_exit(&ds->ds_sendstream_lock); 77 78 return (dsp->dsa_err); 79 } 80 81 static int 82 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset, 83 uint64_t length) 84 { 85 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free); 86 87 /* 88 * When we receive a free record, dbuf_free_range() assumes 89 * that the receiving system doesn't have any dbufs in the range 90 * being freed. This is always true because there is a one-record 91 * constraint: we only send one WRITE record for any given 92 * object+offset. We know that the one-record constraint is 93 * true because we always send data in increasing order by 94 * object,offset. 95 * 96 * If the increasing-order constraint ever changes, we should find 97 * another way to assert that the one-record constraint is still 98 * satisfied. 99 */ 100 ASSERT(object > dsp->dsa_last_data_object || 101 (object == dsp->dsa_last_data_object && 102 offset > dsp->dsa_last_data_offset)); 103 104 /* 105 * If we are doing a non-incremental send, then there can't 106 * be any data in the dataset we're receiving into. Therefore 107 * a free record would simply be a no-op. Save space by not 108 * sending it to begin with. 109 */ 110 if (!dsp->dsa_incremental) 111 return (0); 112 113 if (length != -1ULL && offset + length < offset) 114 length = -1ULL; 115 116 /* 117 * If there is a pending op, but it's not PENDING_FREE, push it out, 118 * since free block aggregation can only be done for blocks of the 119 * same type (i.e., DRR_FREE records can only be aggregated with 120 * other DRR_FREE records. DRR_FREEOBJECTS records can only be 121 * aggregated with other DRR_FREEOBJECTS records. 122 */ 123 if (dsp->dsa_pending_op != PENDING_NONE && 124 dsp->dsa_pending_op != PENDING_FREE) { 125 if (dump_bytes(dsp, dsp->dsa_drr, 126 sizeof (dmu_replay_record_t)) != 0) 127 return (SET_ERROR(EINTR)); 128 dsp->dsa_pending_op = PENDING_NONE; 129 } 130 131 if (dsp->dsa_pending_op == PENDING_FREE) { 132 /* 133 * There should never be a PENDING_FREE if length is -1 134 * (because dump_dnode is the only place where this 135 * function is called with a -1, and only after flushing 136 * any pending record). 137 */ 138 ASSERT(length != -1ULL); 139 /* 140 * Check to see whether this free block can be aggregated 141 * with pending one. 142 */ 143 if (drrf->drr_object == object && drrf->drr_offset + 144 drrf->drr_length == offset) { 145 drrf->drr_length += length; 146 return (0); 147 } else { 148 /* not a continuation. Push out pending record */ 149 if (dump_bytes(dsp, dsp->dsa_drr, 150 sizeof (dmu_replay_record_t)) != 0) 151 return (SET_ERROR(EINTR)); 152 dsp->dsa_pending_op = PENDING_NONE; 153 } 154 } 155 /* create a FREE record and make it pending */ 156 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 157 dsp->dsa_drr->drr_type = DRR_FREE; 158 drrf->drr_object = object; 159 drrf->drr_offset = offset; 160 drrf->drr_length = length; 161 drrf->drr_toguid = dsp->dsa_toguid; 162 if (length == -1ULL) { 163 if (dump_bytes(dsp, dsp->dsa_drr, 164 sizeof (dmu_replay_record_t)) != 0) 165 return (SET_ERROR(EINTR)); 166 } else { 167 dsp->dsa_pending_op = PENDING_FREE; 168 } 169 170 return (0); 171 } 172 173 static int 174 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type, 175 uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data) 176 { 177 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write); 178 179 /* 180 * We send data in increasing object, offset order. 181 * See comment in dump_free() for details. 182 */ 183 ASSERT(object > dsp->dsa_last_data_object || 184 (object == dsp->dsa_last_data_object && 185 offset > dsp->dsa_last_data_offset)); 186 dsp->dsa_last_data_object = object; 187 dsp->dsa_last_data_offset = offset + blksz - 1; 188 189 /* 190 * If there is any kind of pending aggregation (currently either 191 * a grouping of free objects or free blocks), push it out to 192 * the stream, since aggregation can't be done across operations 193 * of different types. 194 */ 195 if (dsp->dsa_pending_op != PENDING_NONE) { 196 if (dump_bytes(dsp, dsp->dsa_drr, 197 sizeof (dmu_replay_record_t)) != 0) 198 return (SET_ERROR(EINTR)); 199 dsp->dsa_pending_op = PENDING_NONE; 200 } 201 /* write a DATA record */ 202 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 203 dsp->dsa_drr->drr_type = DRR_WRITE; 204 drrw->drr_object = object; 205 drrw->drr_type = type; 206 drrw->drr_offset = offset; 207 drrw->drr_length = blksz; 208 drrw->drr_toguid = dsp->dsa_toguid; 209 if (bp == NULL || BP_IS_EMBEDDED(bp)) { 210 /* 211 * There's no pre-computed checksum for partial-block 212 * writes or embedded BP's, so (like 213 * fletcher4-checkummed blocks) userland will have to 214 * compute a dedup-capable checksum itself. 215 */ 216 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF; 217 } else { 218 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp); 219 if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup) 220 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP; 221 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp)); 222 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp)); 223 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp)); 224 drrw->drr_key.ddk_cksum = bp->blk_cksum; 225 } 226 227 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0) 228 return (SET_ERROR(EINTR)); 229 if (dump_bytes(dsp, data, blksz) != 0) 230 return (SET_ERROR(EINTR)); 231 return (0); 232 } 233 234 static int 235 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset, 236 int blksz, const blkptr_t *bp) 237 { 238 char buf[BPE_PAYLOAD_SIZE]; 239 struct drr_write_embedded *drrw = 240 &(dsp->dsa_drr->drr_u.drr_write_embedded); 241 242 if (dsp->dsa_pending_op != PENDING_NONE) { 243 if (dump_bytes(dsp, dsp->dsa_drr, 244 sizeof (dmu_replay_record_t)) != 0) 245 return (EINTR); 246 dsp->dsa_pending_op = PENDING_NONE; 247 } 248 249 ASSERT(BP_IS_EMBEDDED(bp)); 250 251 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 252 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED; 253 drrw->drr_object = object; 254 drrw->drr_offset = offset; 255 drrw->drr_length = blksz; 256 drrw->drr_toguid = dsp->dsa_toguid; 257 drrw->drr_compression = BP_GET_COMPRESS(bp); 258 drrw->drr_etype = BPE_GET_ETYPE(bp); 259 drrw->drr_lsize = BPE_GET_LSIZE(bp); 260 drrw->drr_psize = BPE_GET_PSIZE(bp); 261 262 decode_embedded_bp_compressed(bp, buf); 263 264 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0) 265 return (EINTR); 266 if (dump_bytes(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0) 267 return (EINTR); 268 return (0); 269 } 270 271 static int 272 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data) 273 { 274 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill); 275 276 if (dsp->dsa_pending_op != PENDING_NONE) { 277 if (dump_bytes(dsp, dsp->dsa_drr, 278 sizeof (dmu_replay_record_t)) != 0) 279 return (SET_ERROR(EINTR)); 280 dsp->dsa_pending_op = PENDING_NONE; 281 } 282 283 /* write a SPILL record */ 284 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 285 dsp->dsa_drr->drr_type = DRR_SPILL; 286 drrs->drr_object = object; 287 drrs->drr_length = blksz; 288 drrs->drr_toguid = dsp->dsa_toguid; 289 290 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t))) 291 return (SET_ERROR(EINTR)); 292 if (dump_bytes(dsp, data, blksz)) 293 return (SET_ERROR(EINTR)); 294 return (0); 295 } 296 297 static int 298 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs) 299 { 300 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects); 301 302 /* See comment in dump_free(). */ 303 if (!dsp->dsa_incremental) 304 return (0); 305 306 /* 307 * If there is a pending op, but it's not PENDING_FREEOBJECTS, 308 * push it out, since free block aggregation can only be done for 309 * blocks of the same type (i.e., DRR_FREE records can only be 310 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records 311 * can only be aggregated with other DRR_FREEOBJECTS records. 312 */ 313 if (dsp->dsa_pending_op != PENDING_NONE && 314 dsp->dsa_pending_op != PENDING_FREEOBJECTS) { 315 if (dump_bytes(dsp, dsp->dsa_drr, 316 sizeof (dmu_replay_record_t)) != 0) 317 return (SET_ERROR(EINTR)); 318 dsp->dsa_pending_op = PENDING_NONE; 319 } 320 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) { 321 /* 322 * See whether this free object array can be aggregated 323 * with pending one 324 */ 325 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) { 326 drrfo->drr_numobjs += numobjs; 327 return (0); 328 } else { 329 /* can't be aggregated. Push out pending record */ 330 if (dump_bytes(dsp, dsp->dsa_drr, 331 sizeof (dmu_replay_record_t)) != 0) 332 return (SET_ERROR(EINTR)); 333 dsp->dsa_pending_op = PENDING_NONE; 334 } 335 } 336 337 /* write a FREEOBJECTS record */ 338 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 339 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS; 340 drrfo->drr_firstobj = firstobj; 341 drrfo->drr_numobjs = numobjs; 342 drrfo->drr_toguid = dsp->dsa_toguid; 343 344 dsp->dsa_pending_op = PENDING_FREEOBJECTS; 345 346 return (0); 347 } 348 349 static int 350 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp) 351 { 352 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object); 353 354 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE) 355 return (dump_freeobjects(dsp, object, 1)); 356 357 if (dsp->dsa_pending_op != PENDING_NONE) { 358 if (dump_bytes(dsp, dsp->dsa_drr, 359 sizeof (dmu_replay_record_t)) != 0) 360 return (SET_ERROR(EINTR)); 361 dsp->dsa_pending_op = PENDING_NONE; 362 } 363 364 /* write an OBJECT record */ 365 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 366 dsp->dsa_drr->drr_type = DRR_OBJECT; 367 drro->drr_object = object; 368 drro->drr_type = dnp->dn_type; 369 drro->drr_bonustype = dnp->dn_bonustype; 370 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; 371 drro->drr_bonuslen = dnp->dn_bonuslen; 372 drro->drr_checksumtype = dnp->dn_checksum; 373 drro->drr_compress = dnp->dn_compress; 374 drro->drr_toguid = dsp->dsa_toguid; 375 376 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) && 377 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE) 378 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE; 379 380 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0) 381 return (SET_ERROR(EINTR)); 382 383 if (dump_bytes(dsp, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) 384 return (SET_ERROR(EINTR)); 385 386 /* Free anything past the end of the file. */ 387 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) * 388 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0) 389 return (SET_ERROR(EINTR)); 390 if (dsp->dsa_err != 0) 391 return (SET_ERROR(EINTR)); 392 return (0); 393 } 394 395 static boolean_t 396 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp) 397 { 398 if (!BP_IS_EMBEDDED(bp)) 399 return (B_FALSE); 400 401 /* 402 * Compression function must be legacy, or explicitly enabled. 403 */ 404 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS && 405 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4))) 406 return (B_FALSE); 407 408 /* 409 * Embed type must be explicitly enabled. 410 */ 411 switch (BPE_GET_ETYPE(bp)) { 412 case BP_EMBEDDED_TYPE_DATA: 413 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) 414 return (B_TRUE); 415 break; 416 default: 417 return (B_FALSE); 418 } 419 return (B_FALSE); 420 } 421 422 #define BP_SPAN(dnp, level) \ 423 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \ 424 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) 425 426 /* ARGSUSED */ 427 static int 428 backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 429 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 430 { 431 dmu_sendarg_t *dsp = arg; 432 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE; 433 int err = 0; 434 435 if (issig(JUSTLOOKING) && issig(FORREAL)) 436 return (SET_ERROR(EINTR)); 437 438 if (zb->zb_object != DMU_META_DNODE_OBJECT && 439 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) { 440 return (0); 441 } else if (zb->zb_level == ZB_ZIL_LEVEL) { 442 /* 443 * If we are sending a non-snapshot (which is allowed on 444 * read-only pools), it may have a ZIL, which must be ignored. 445 */ 446 return (0); 447 } else if (BP_IS_HOLE(bp) && 448 zb->zb_object == DMU_META_DNODE_OBJECT) { 449 uint64_t span = BP_SPAN(dnp, zb->zb_level); 450 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT; 451 err = dump_freeobjects(dsp, dnobj, span >> DNODE_SHIFT); 452 } else if (BP_IS_HOLE(bp)) { 453 uint64_t span = BP_SPAN(dnp, zb->zb_level); 454 err = dump_free(dsp, zb->zb_object, zb->zb_blkid * span, span); 455 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) { 456 return (0); 457 } else if (type == DMU_OT_DNODE) { 458 dnode_phys_t *blk; 459 int i; 460 int blksz = BP_GET_LSIZE(bp); 461 arc_flags_t aflags = ARC_FLAG_WAIT; 462 arc_buf_t *abuf; 463 464 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf, 465 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, 466 &aflags, zb) != 0) 467 return (SET_ERROR(EIO)); 468 469 blk = abuf->b_data; 470 for (i = 0; i < blksz >> DNODE_SHIFT; i++) { 471 uint64_t dnobj = (zb->zb_blkid << 472 (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i; 473 err = dump_dnode(dsp, dnobj, blk+i); 474 if (err != 0) 475 break; 476 } 477 (void) arc_buf_remove_ref(abuf, &abuf); 478 } else if (type == DMU_OT_SA) { 479 arc_flags_t aflags = ARC_FLAG_WAIT; 480 arc_buf_t *abuf; 481 int blksz = BP_GET_LSIZE(bp); 482 483 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf, 484 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, 485 &aflags, zb) != 0) 486 return (SET_ERROR(EIO)); 487 488 err = dump_spill(dsp, zb->zb_object, blksz, abuf->b_data); 489 (void) arc_buf_remove_ref(abuf, &abuf); 490 } else if (backup_do_embed(dsp, bp)) { 491 /* it's an embedded level-0 block of a regular object */ 492 int blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; 493 err = dump_write_embedded(dsp, zb->zb_object, 494 zb->zb_blkid * blksz, blksz, bp); 495 } else { /* it's a level-0 block of a regular object */ 496 arc_flags_t aflags = ARC_FLAG_WAIT; 497 arc_buf_t *abuf; 498 int blksz = BP_GET_LSIZE(bp); 499 uint64_t offset; 500 501 ASSERT3U(blksz, ==, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 502 ASSERT0(zb->zb_level); 503 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf, 504 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, 505 &aflags, zb) != 0) { 506 if (zfs_send_corrupt_data) { 507 /* Send a block filled with 0x"zfs badd bloc" */ 508 abuf = arc_buf_alloc(spa, blksz, &abuf, 509 ARC_BUFC_DATA); 510 uint64_t *ptr; 511 for (ptr = abuf->b_data; 512 (char *)ptr < (char *)abuf->b_data + blksz; 513 ptr++) 514 *ptr = 0x2f5baddb10cULL; 515 } else { 516 return (SET_ERROR(EIO)); 517 } 518 } 519 520 offset = zb->zb_blkid * blksz; 521 522 if (!(dsp->dsa_featureflags & 523 DMU_BACKUP_FEATURE_LARGE_BLOCKS) && 524 blksz > SPA_OLD_MAXBLOCKSIZE) { 525 char *buf = abuf->b_data; 526 while (blksz > 0 && err == 0) { 527 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE); 528 err = dump_write(dsp, type, zb->zb_object, 529 offset, n, NULL, buf); 530 offset += n; 531 buf += n; 532 blksz -= n; 533 } 534 } else { 535 err = dump_write(dsp, type, zb->zb_object, 536 offset, blksz, bp, abuf->b_data); 537 } 538 (void) arc_buf_remove_ref(abuf, &abuf); 539 } 540 541 ASSERT(err == 0 || err == EINTR); 542 return (err); 543 } 544 545 /* 546 * Releases dp using the specified tag. 547 */ 548 static int 549 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *ds, 550 zfs_bookmark_phys_t *fromzb, boolean_t is_clone, boolean_t embedok, 551 boolean_t large_block_ok, int outfd, vnode_t *vp, offset_t *off) 552 { 553 objset_t *os; 554 dmu_replay_record_t *drr; 555 dmu_sendarg_t *dsp; 556 int err; 557 uint64_t fromtxg = 0; 558 uint64_t featureflags = 0; 559 560 err = dmu_objset_from_ds(ds, &os); 561 if (err != 0) { 562 dsl_pool_rele(dp, tag); 563 return (err); 564 } 565 566 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 567 drr->drr_type = DRR_BEGIN; 568 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 569 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo, 570 DMU_SUBSTREAM); 571 572 #ifdef _KERNEL 573 if (dmu_objset_type(os) == DMU_OST_ZFS) { 574 uint64_t version; 575 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) { 576 kmem_free(drr, sizeof (dmu_replay_record_t)); 577 dsl_pool_rele(dp, tag); 578 return (SET_ERROR(EINVAL)); 579 } 580 if (version >= ZPL_VERSION_SA) { 581 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; 582 } 583 } 584 #endif 585 586 if (large_block_ok && ds->ds_large_blocks) 587 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS; 588 if (embedok && 589 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) { 590 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA; 591 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) 592 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4; 593 } else { 594 embedok = B_FALSE; 595 } 596 597 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo, 598 featureflags); 599 600 drr->drr_u.drr_begin.drr_creation_time = 601 dsl_dataset_phys(ds)->ds_creation_time; 602 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os); 603 if (is_clone) 604 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE; 605 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(ds)->ds_guid; 606 if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET) 607 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA; 608 609 if (fromzb != NULL) { 610 drr->drr_u.drr_begin.drr_fromguid = fromzb->zbm_guid; 611 fromtxg = fromzb->zbm_creation_txg; 612 } 613 dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname); 614 if (!ds->ds_is_snapshot) { 615 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--", 616 sizeof (drr->drr_u.drr_begin.drr_toname)); 617 } 618 619 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP); 620 621 dsp->dsa_drr = drr; 622 dsp->dsa_vp = vp; 623 dsp->dsa_outfd = outfd; 624 dsp->dsa_proc = curproc; 625 dsp->dsa_os = os; 626 dsp->dsa_off = off; 627 dsp->dsa_toguid = dsl_dataset_phys(ds)->ds_guid; 628 ZIO_SET_CHECKSUM(&dsp->dsa_zc, 0, 0, 0, 0); 629 dsp->dsa_pending_op = PENDING_NONE; 630 dsp->dsa_incremental = (fromzb != NULL); 631 dsp->dsa_featureflags = featureflags; 632 633 mutex_enter(&ds->ds_sendstream_lock); 634 list_insert_head(&ds->ds_sendstreams, dsp); 635 mutex_exit(&ds->ds_sendstream_lock); 636 637 dsl_dataset_long_hold(ds, FTAG); 638 dsl_pool_rele(dp, tag); 639 640 if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) { 641 err = dsp->dsa_err; 642 goto out; 643 } 644 645 err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH, 646 backup_cb, dsp); 647 648 if (dsp->dsa_pending_op != PENDING_NONE) 649 if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) 650 err = SET_ERROR(EINTR); 651 652 if (err != 0) { 653 if (err == EINTR && dsp->dsa_err != 0) 654 err = dsp->dsa_err; 655 goto out; 656 } 657 658 bzero(drr, sizeof (dmu_replay_record_t)); 659 drr->drr_type = DRR_END; 660 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc; 661 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid; 662 663 if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) { 664 err = dsp->dsa_err; 665 goto out; 666 } 667 668 out: 669 mutex_enter(&ds->ds_sendstream_lock); 670 list_remove(&ds->ds_sendstreams, dsp); 671 mutex_exit(&ds->ds_sendstream_lock); 672 673 kmem_free(drr, sizeof (dmu_replay_record_t)); 674 kmem_free(dsp, sizeof (dmu_sendarg_t)); 675 676 dsl_dataset_long_rele(ds, FTAG); 677 678 return (err); 679 } 680 681 int 682 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap, 683 boolean_t embedok, boolean_t large_block_ok, 684 int outfd, vnode_t *vp, offset_t *off) 685 { 686 dsl_pool_t *dp; 687 dsl_dataset_t *ds; 688 dsl_dataset_t *fromds = NULL; 689 int err; 690 691 err = dsl_pool_hold(pool, FTAG, &dp); 692 if (err != 0) 693 return (err); 694 695 err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds); 696 if (err != 0) { 697 dsl_pool_rele(dp, FTAG); 698 return (err); 699 } 700 701 if (fromsnap != 0) { 702 zfs_bookmark_phys_t zb; 703 boolean_t is_clone; 704 705 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds); 706 if (err != 0) { 707 dsl_dataset_rele(ds, FTAG); 708 dsl_pool_rele(dp, FTAG); 709 return (err); 710 } 711 if (!dsl_dataset_is_before(ds, fromds, 0)) 712 err = SET_ERROR(EXDEV); 713 zb.zbm_creation_time = 714 dsl_dataset_phys(fromds)->ds_creation_time; 715 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg; 716 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid; 717 is_clone = (fromds->ds_dir != ds->ds_dir); 718 dsl_dataset_rele(fromds, FTAG); 719 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone, 720 embedok, large_block_ok, outfd, vp, off); 721 } else { 722 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE, 723 embedok, large_block_ok, outfd, vp, off); 724 } 725 dsl_dataset_rele(ds, FTAG); 726 return (err); 727 } 728 729 int 730 dmu_send(const char *tosnap, const char *fromsnap, 731 boolean_t embedok, boolean_t large_block_ok, 732 int outfd, vnode_t *vp, offset_t *off) 733 { 734 dsl_pool_t *dp; 735 dsl_dataset_t *ds; 736 int err; 737 boolean_t owned = B_FALSE; 738 739 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL) 740 return (SET_ERROR(EINVAL)); 741 742 err = dsl_pool_hold(tosnap, FTAG, &dp); 743 if (err != 0) 744 return (err); 745 746 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) { 747 /* 748 * We are sending a filesystem or volume. Ensure 749 * that it doesn't change by owning the dataset. 750 */ 751 err = dsl_dataset_own(dp, tosnap, FTAG, &ds); 752 owned = B_TRUE; 753 } else { 754 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds); 755 } 756 if (err != 0) { 757 dsl_pool_rele(dp, FTAG); 758 return (err); 759 } 760 761 if (fromsnap != NULL) { 762 zfs_bookmark_phys_t zb; 763 boolean_t is_clone = B_FALSE; 764 int fsnamelen = strchr(tosnap, '@') - tosnap; 765 766 /* 767 * If the fromsnap is in a different filesystem, then 768 * mark the send stream as a clone. 769 */ 770 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 || 771 (fromsnap[fsnamelen] != '@' && 772 fromsnap[fsnamelen] != '#')) { 773 is_clone = B_TRUE; 774 } 775 776 if (strchr(fromsnap, '@')) { 777 dsl_dataset_t *fromds; 778 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds); 779 if (err == 0) { 780 if (!dsl_dataset_is_before(ds, fromds, 0)) 781 err = SET_ERROR(EXDEV); 782 zb.zbm_creation_time = 783 dsl_dataset_phys(fromds)->ds_creation_time; 784 zb.zbm_creation_txg = 785 dsl_dataset_phys(fromds)->ds_creation_txg; 786 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid; 787 is_clone = (ds->ds_dir != fromds->ds_dir); 788 dsl_dataset_rele(fromds, FTAG); 789 } 790 } else { 791 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb); 792 } 793 if (err != 0) { 794 dsl_dataset_rele(ds, FTAG); 795 dsl_pool_rele(dp, FTAG); 796 return (err); 797 } 798 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone, 799 embedok, large_block_ok, outfd, vp, off); 800 } else { 801 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE, 802 embedok, large_block_ok, outfd, vp, off); 803 } 804 if (owned) 805 dsl_dataset_disown(ds, FTAG); 806 else 807 dsl_dataset_rele(ds, FTAG); 808 return (err); 809 } 810 811 int 812 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep) 813 { 814 dsl_pool_t *dp = ds->ds_dir->dd_pool; 815 int err; 816 uint64_t size; 817 818 ASSERT(dsl_pool_config_held(dp)); 819 820 /* tosnap must be a snapshot */ 821 if (!ds->ds_is_snapshot) 822 return (SET_ERROR(EINVAL)); 823 824 /* 825 * fromsnap must be an earlier snapshot from the same fs as tosnap, 826 * or the origin's fs. 827 */ 828 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0)) 829 return (SET_ERROR(EXDEV)); 830 831 /* Get uncompressed size estimate of changed data. */ 832 if (fromds == NULL) { 833 size = dsl_dataset_phys(ds)->ds_uncompressed_bytes; 834 } else { 835 uint64_t used, comp; 836 err = dsl_dataset_space_written(fromds, ds, 837 &used, &comp, &size); 838 if (err != 0) 839 return (err); 840 } 841 842 /* 843 * Assume that space (both on-disk and in-stream) is dominated by 844 * data. We will adjust for indirect blocks and the copies property, 845 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records). 846 */ 847 848 /* 849 * Subtract out approximate space used by indirect blocks. 850 * Assume most space is used by data blocks (non-indirect, non-dnode). 851 * Assume all blocks are recordsize. Assume ditto blocks and 852 * internal fragmentation counter out compression. 853 * 854 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per 855 * block, which we observe in practice. 856 */ 857 uint64_t recordsize; 858 err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize); 859 if (err != 0) 860 return (err); 861 size -= size / recordsize * sizeof (blkptr_t); 862 863 /* Add in the space for the record associated with each block. */ 864 size += size / recordsize * sizeof (dmu_replay_record_t); 865 866 *sizep = size; 867 868 return (0); 869 } 870 871 typedef struct dmu_recv_begin_arg { 872 const char *drba_origin; 873 dmu_recv_cookie_t *drba_cookie; 874 cred_t *drba_cred; 875 uint64_t drba_snapobj; 876 } dmu_recv_begin_arg_t; 877 878 static int 879 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds, 880 uint64_t fromguid) 881 { 882 uint64_t val; 883 int error; 884 dsl_pool_t *dp = ds->ds_dir->dd_pool; 885 886 /* temporary clone name must not exist */ 887 error = zap_lookup(dp->dp_meta_objset, 888 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name, 889 8, 1, &val); 890 if (error != ENOENT) 891 return (error == 0 ? EBUSY : error); 892 893 /* new snapshot name must not exist */ 894 error = zap_lookup(dp->dp_meta_objset, 895 dsl_dataset_phys(ds)->ds_snapnames_zapobj, 896 drba->drba_cookie->drc_tosnap, 8, 1, &val); 897 if (error != ENOENT) 898 return (error == 0 ? EEXIST : error); 899 900 /* 901 * Check snapshot limit before receiving. We'll recheck again at the 902 * end, but might as well abort before receiving if we're already over 903 * the limit. 904 * 905 * Note that we do not check the file system limit with 906 * dsl_dir_fscount_check because the temporary %clones don't count 907 * against that limit. 908 */ 909 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT, 910 NULL, drba->drba_cred); 911 if (error != 0) 912 return (error); 913 914 if (fromguid != 0) { 915 dsl_dataset_t *snap; 916 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 917 918 /* Find snapshot in this dir that matches fromguid. */ 919 while (obj != 0) { 920 error = dsl_dataset_hold_obj(dp, obj, FTAG, 921 &snap); 922 if (error != 0) 923 return (SET_ERROR(ENODEV)); 924 if (snap->ds_dir != ds->ds_dir) { 925 dsl_dataset_rele(snap, FTAG); 926 return (SET_ERROR(ENODEV)); 927 } 928 if (dsl_dataset_phys(snap)->ds_guid == fromguid) 929 break; 930 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 931 dsl_dataset_rele(snap, FTAG); 932 } 933 if (obj == 0) 934 return (SET_ERROR(ENODEV)); 935 936 if (drba->drba_cookie->drc_force) { 937 drba->drba_snapobj = obj; 938 } else { 939 /* 940 * If we are not forcing, there must be no 941 * changes since fromsnap. 942 */ 943 if (dsl_dataset_modified_since_snap(ds, snap)) { 944 dsl_dataset_rele(snap, FTAG); 945 return (SET_ERROR(ETXTBSY)); 946 } 947 drba->drba_snapobj = ds->ds_prev->ds_object; 948 } 949 950 dsl_dataset_rele(snap, FTAG); 951 } else { 952 /* if full, most recent snapshot must be $ORIGIN */ 953 if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= TXG_INITIAL) 954 return (SET_ERROR(ENODEV)); 955 drba->drba_snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj; 956 } 957 958 return (0); 959 960 } 961 962 static int 963 dmu_recv_begin_check(void *arg, dmu_tx_t *tx) 964 { 965 dmu_recv_begin_arg_t *drba = arg; 966 dsl_pool_t *dp = dmu_tx_pool(tx); 967 struct drr_begin *drrb = drba->drba_cookie->drc_drrb; 968 uint64_t fromguid = drrb->drr_fromguid; 969 int flags = drrb->drr_flags; 970 int error; 971 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 972 dsl_dataset_t *ds; 973 const char *tofs = drba->drba_cookie->drc_tofs; 974 975 /* already checked */ 976 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC); 977 978 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == 979 DMU_COMPOUNDSTREAM || 980 drrb->drr_type >= DMU_OST_NUMTYPES || 981 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL)) 982 return (SET_ERROR(EINVAL)); 983 984 /* Verify pool version supports SA if SA_SPILL feature set */ 985 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) && 986 spa_version(dp->dp_spa) < SPA_VERSION_SA) 987 return (SET_ERROR(ENOTSUP)); 988 989 /* 990 * The receiving code doesn't know how to translate a WRITE_EMBEDDED 991 * record to a plan WRITE record, so the pool must have the 992 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED 993 * records. Same with WRITE_EMBEDDED records that use LZ4 compression. 994 */ 995 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) && 996 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) 997 return (SET_ERROR(ENOTSUP)); 998 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) && 999 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) 1000 return (SET_ERROR(ENOTSUP)); 1001 1002 /* 1003 * The receiving code doesn't know how to translate large blocks 1004 * to smaller ones, so the pool must have the LARGE_BLOCKS 1005 * feature enabled if the stream has LARGE_BLOCKS. 1006 */ 1007 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) && 1008 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS)) 1009 return (SET_ERROR(ENOTSUP)); 1010 1011 error = dsl_dataset_hold(dp, tofs, FTAG, &ds); 1012 if (error == 0) { 1013 /* target fs already exists; recv into temp clone */ 1014 1015 /* Can't recv a clone into an existing fs */ 1016 if (flags & DRR_FLAG_CLONE) { 1017 dsl_dataset_rele(ds, FTAG); 1018 return (SET_ERROR(EINVAL)); 1019 } 1020 1021 error = recv_begin_check_existing_impl(drba, ds, fromguid); 1022 dsl_dataset_rele(ds, FTAG); 1023 } else if (error == ENOENT) { 1024 /* target fs does not exist; must be a full backup or clone */ 1025 char buf[MAXNAMELEN]; 1026 1027 /* 1028 * If it's a non-clone incremental, we are missing the 1029 * target fs, so fail the recv. 1030 */ 1031 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE)) 1032 return (SET_ERROR(ENOENT)); 1033 1034 /* Open the parent of tofs */ 1035 ASSERT3U(strlen(tofs), <, MAXNAMELEN); 1036 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1); 1037 error = dsl_dataset_hold(dp, buf, FTAG, &ds); 1038 if (error != 0) 1039 return (error); 1040 1041 /* 1042 * Check filesystem and snapshot limits before receiving. We'll 1043 * recheck snapshot limits again at the end (we create the 1044 * filesystems and increment those counts during begin_sync). 1045 */ 1046 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, 1047 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred); 1048 if (error != 0) { 1049 dsl_dataset_rele(ds, FTAG); 1050 return (error); 1051 } 1052 1053 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, 1054 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred); 1055 if (error != 0) { 1056 dsl_dataset_rele(ds, FTAG); 1057 return (error); 1058 } 1059 1060 if (drba->drba_origin != NULL) { 1061 dsl_dataset_t *origin; 1062 error = dsl_dataset_hold(dp, drba->drba_origin, 1063 FTAG, &origin); 1064 if (error != 0) { 1065 dsl_dataset_rele(ds, FTAG); 1066 return (error); 1067 } 1068 if (!origin->ds_is_snapshot) { 1069 dsl_dataset_rele(origin, FTAG); 1070 dsl_dataset_rele(ds, FTAG); 1071 return (SET_ERROR(EINVAL)); 1072 } 1073 if (dsl_dataset_phys(origin)->ds_guid != fromguid) { 1074 dsl_dataset_rele(origin, FTAG); 1075 dsl_dataset_rele(ds, FTAG); 1076 return (SET_ERROR(ENODEV)); 1077 } 1078 dsl_dataset_rele(origin, FTAG); 1079 } 1080 dsl_dataset_rele(ds, FTAG); 1081 error = 0; 1082 } 1083 return (error); 1084 } 1085 1086 static void 1087 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx) 1088 { 1089 dmu_recv_begin_arg_t *drba = arg; 1090 dsl_pool_t *dp = dmu_tx_pool(tx); 1091 struct drr_begin *drrb = drba->drba_cookie->drc_drrb; 1092 const char *tofs = drba->drba_cookie->drc_tofs; 1093 dsl_dataset_t *ds, *newds; 1094 uint64_t dsobj; 1095 int error; 1096 uint64_t crflags; 1097 1098 crflags = (drrb->drr_flags & DRR_FLAG_CI_DATA) ? 1099 DS_FLAG_CI_DATASET : 0; 1100 1101 error = dsl_dataset_hold(dp, tofs, FTAG, &ds); 1102 if (error == 0) { 1103 /* create temporary clone */ 1104 dsl_dataset_t *snap = NULL; 1105 if (drba->drba_snapobj != 0) { 1106 VERIFY0(dsl_dataset_hold_obj(dp, 1107 drba->drba_snapobj, FTAG, &snap)); 1108 } 1109 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name, 1110 snap, crflags, drba->drba_cred, tx); 1111 dsl_dataset_rele(snap, FTAG); 1112 dsl_dataset_rele(ds, FTAG); 1113 } else { 1114 dsl_dir_t *dd; 1115 const char *tail; 1116 dsl_dataset_t *origin = NULL; 1117 1118 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail)); 1119 1120 if (drba->drba_origin != NULL) { 1121 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin, 1122 FTAG, &origin)); 1123 } 1124 1125 /* Create new dataset. */ 1126 dsobj = dsl_dataset_create_sync(dd, 1127 strrchr(tofs, '/') + 1, 1128 origin, crflags, drba->drba_cred, tx); 1129 if (origin != NULL) 1130 dsl_dataset_rele(origin, FTAG); 1131 dsl_dir_rele(dd, FTAG); 1132 drba->drba_cookie->drc_newfs = B_TRUE; 1133 } 1134 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds)); 1135 1136 if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) & 1137 DMU_BACKUP_FEATURE_LARGE_BLOCKS) && 1138 !newds->ds_large_blocks) { 1139 dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx); 1140 newds->ds_large_blocks = B_TRUE; 1141 } 1142 1143 dmu_buf_will_dirty(newds->ds_dbuf, tx); 1144 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT; 1145 1146 /* 1147 * If we actually created a non-clone, we need to create the 1148 * objset in our new dataset. 1149 */ 1150 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) { 1151 (void) dmu_objset_create_impl(dp->dp_spa, 1152 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx); 1153 } 1154 1155 drba->drba_cookie->drc_ds = newds; 1156 1157 spa_history_log_internal_ds(newds, "receive", tx, ""); 1158 } 1159 1160 /* 1161 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin() 1162 * succeeds; otherwise we will leak the holds on the datasets. 1163 */ 1164 int 1165 dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb, 1166 boolean_t force, char *origin, dmu_recv_cookie_t *drc) 1167 { 1168 dmu_recv_begin_arg_t drba = { 0 }; 1169 dmu_replay_record_t *drr; 1170 1171 bzero(drc, sizeof (dmu_recv_cookie_t)); 1172 drc->drc_drrb = drrb; 1173 drc->drc_tosnap = tosnap; 1174 drc->drc_tofs = tofs; 1175 drc->drc_force = force; 1176 drc->drc_cred = CRED(); 1177 1178 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 1179 drc->drc_byteswap = B_TRUE; 1180 else if (drrb->drr_magic != DMU_BACKUP_MAGIC) 1181 return (SET_ERROR(EINVAL)); 1182 1183 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 1184 drr->drr_type = DRR_BEGIN; 1185 drr->drr_u.drr_begin = *drc->drc_drrb; 1186 if (drc->drc_byteswap) { 1187 fletcher_4_incremental_byteswap(drr, 1188 sizeof (dmu_replay_record_t), &drc->drc_cksum); 1189 } else { 1190 fletcher_4_incremental_native(drr, 1191 sizeof (dmu_replay_record_t), &drc->drc_cksum); 1192 } 1193 kmem_free(drr, sizeof (dmu_replay_record_t)); 1194 1195 if (drc->drc_byteswap) { 1196 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 1197 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); 1198 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 1199 drrb->drr_type = BSWAP_32(drrb->drr_type); 1200 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 1201 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 1202 } 1203 1204 drba.drba_origin = origin; 1205 drba.drba_cookie = drc; 1206 drba.drba_cred = CRED(); 1207 1208 return (dsl_sync_task(tofs, dmu_recv_begin_check, dmu_recv_begin_sync, 1209 &drba, 5, ZFS_SPACE_CHECK_NORMAL)); 1210 } 1211 1212 struct restorearg { 1213 int err; 1214 boolean_t byteswap; 1215 vnode_t *vp; 1216 char *buf; 1217 uint64_t voff; 1218 int bufsize; /* amount of memory allocated for buf */ 1219 zio_cksum_t cksum; 1220 avl_tree_t *guid_to_ds_map; 1221 }; 1222 1223 typedef struct guid_map_entry { 1224 uint64_t guid; 1225 dsl_dataset_t *gme_ds; 1226 avl_node_t avlnode; 1227 } guid_map_entry_t; 1228 1229 static int 1230 guid_compare(const void *arg1, const void *arg2) 1231 { 1232 const guid_map_entry_t *gmep1 = arg1; 1233 const guid_map_entry_t *gmep2 = arg2; 1234 1235 if (gmep1->guid < gmep2->guid) 1236 return (-1); 1237 else if (gmep1->guid > gmep2->guid) 1238 return (1); 1239 return (0); 1240 } 1241 1242 static void 1243 free_guid_map_onexit(void *arg) 1244 { 1245 avl_tree_t *ca = arg; 1246 void *cookie = NULL; 1247 guid_map_entry_t *gmep; 1248 1249 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) { 1250 dsl_dataset_long_rele(gmep->gme_ds, gmep); 1251 dsl_dataset_rele(gmep->gme_ds, gmep); 1252 kmem_free(gmep, sizeof (guid_map_entry_t)); 1253 } 1254 avl_destroy(ca); 1255 kmem_free(ca, sizeof (avl_tree_t)); 1256 } 1257 1258 static void * 1259 restore_read(struct restorearg *ra, int len, char *buf) 1260 { 1261 int done = 0; 1262 1263 if (buf == NULL) 1264 buf = ra->buf; 1265 1266 /* some things will require 8-byte alignment, so everything must */ 1267 ASSERT0(len % 8); 1268 ASSERT3U(len, <=, ra->bufsize); 1269 1270 while (done < len) { 1271 ssize_t resid; 1272 1273 ra->err = vn_rdwr(UIO_READ, ra->vp, 1274 buf + done, len - done, 1275 ra->voff, UIO_SYSSPACE, FAPPEND, 1276 RLIM64_INFINITY, CRED(), &resid); 1277 1278 if (resid == len - done) 1279 ra->err = SET_ERROR(EINVAL); 1280 ra->voff += len - done - resid; 1281 done = len - resid; 1282 if (ra->err != 0) 1283 return (NULL); 1284 } 1285 1286 ASSERT3U(done, ==, len); 1287 if (ra->byteswap) 1288 fletcher_4_incremental_byteswap(buf, len, &ra->cksum); 1289 else 1290 fletcher_4_incremental_native(buf, len, &ra->cksum); 1291 return (buf); 1292 } 1293 1294 static void 1295 backup_byteswap(dmu_replay_record_t *drr) 1296 { 1297 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X)) 1298 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X)) 1299 drr->drr_type = BSWAP_32(drr->drr_type); 1300 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen); 1301 switch (drr->drr_type) { 1302 case DRR_BEGIN: 1303 DO64(drr_begin.drr_magic); 1304 DO64(drr_begin.drr_versioninfo); 1305 DO64(drr_begin.drr_creation_time); 1306 DO32(drr_begin.drr_type); 1307 DO32(drr_begin.drr_flags); 1308 DO64(drr_begin.drr_toguid); 1309 DO64(drr_begin.drr_fromguid); 1310 break; 1311 case DRR_OBJECT: 1312 DO64(drr_object.drr_object); 1313 DO32(drr_object.drr_type); 1314 DO32(drr_object.drr_bonustype); 1315 DO32(drr_object.drr_blksz); 1316 DO32(drr_object.drr_bonuslen); 1317 DO64(drr_object.drr_toguid); 1318 break; 1319 case DRR_FREEOBJECTS: 1320 DO64(drr_freeobjects.drr_firstobj); 1321 DO64(drr_freeobjects.drr_numobjs); 1322 DO64(drr_freeobjects.drr_toguid); 1323 break; 1324 case DRR_WRITE: 1325 DO64(drr_write.drr_object); 1326 DO32(drr_write.drr_type); 1327 DO64(drr_write.drr_offset); 1328 DO64(drr_write.drr_length); 1329 DO64(drr_write.drr_toguid); 1330 DO64(drr_write.drr_key.ddk_cksum.zc_word[0]); 1331 DO64(drr_write.drr_key.ddk_cksum.zc_word[1]); 1332 DO64(drr_write.drr_key.ddk_cksum.zc_word[2]); 1333 DO64(drr_write.drr_key.ddk_cksum.zc_word[3]); 1334 DO64(drr_write.drr_key.ddk_prop); 1335 break; 1336 case DRR_WRITE_BYREF: 1337 DO64(drr_write_byref.drr_object); 1338 DO64(drr_write_byref.drr_offset); 1339 DO64(drr_write_byref.drr_length); 1340 DO64(drr_write_byref.drr_toguid); 1341 DO64(drr_write_byref.drr_refguid); 1342 DO64(drr_write_byref.drr_refobject); 1343 DO64(drr_write_byref.drr_refoffset); 1344 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[0]); 1345 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[1]); 1346 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[2]); 1347 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[3]); 1348 DO64(drr_write_byref.drr_key.ddk_prop); 1349 break; 1350 case DRR_WRITE_EMBEDDED: 1351 DO64(drr_write_embedded.drr_object); 1352 DO64(drr_write_embedded.drr_offset); 1353 DO64(drr_write_embedded.drr_length); 1354 DO64(drr_write_embedded.drr_toguid); 1355 DO32(drr_write_embedded.drr_lsize); 1356 DO32(drr_write_embedded.drr_psize); 1357 break; 1358 case DRR_FREE: 1359 DO64(drr_free.drr_object); 1360 DO64(drr_free.drr_offset); 1361 DO64(drr_free.drr_length); 1362 DO64(drr_free.drr_toguid); 1363 break; 1364 case DRR_SPILL: 1365 DO64(drr_spill.drr_object); 1366 DO64(drr_spill.drr_length); 1367 DO64(drr_spill.drr_toguid); 1368 break; 1369 case DRR_END: 1370 DO64(drr_end.drr_checksum.zc_word[0]); 1371 DO64(drr_end.drr_checksum.zc_word[1]); 1372 DO64(drr_end.drr_checksum.zc_word[2]); 1373 DO64(drr_end.drr_checksum.zc_word[3]); 1374 DO64(drr_end.drr_toguid); 1375 break; 1376 } 1377 #undef DO64 1378 #undef DO32 1379 } 1380 1381 static inline uint8_t 1382 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size) 1383 { 1384 if (bonus_type == DMU_OT_SA) { 1385 return (1); 1386 } else { 1387 return (1 + 1388 ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT)); 1389 } 1390 } 1391 1392 static int 1393 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro) 1394 { 1395 dmu_object_info_t doi; 1396 dmu_tx_t *tx; 1397 void *data = NULL; 1398 uint64_t object; 1399 int err; 1400 1401 if (drro->drr_type == DMU_OT_NONE || 1402 !DMU_OT_IS_VALID(drro->drr_type) || 1403 !DMU_OT_IS_VALID(drro->drr_bonustype) || 1404 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS || 1405 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS || 1406 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) || 1407 drro->drr_blksz < SPA_MINBLOCKSIZE || 1408 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(os)) || 1409 drro->drr_bonuslen > DN_MAX_BONUSLEN) { 1410 return (SET_ERROR(EINVAL)); 1411 } 1412 1413 err = dmu_object_info(os, drro->drr_object, &doi); 1414 1415 if (err != 0 && err != ENOENT) 1416 return (SET_ERROR(EINVAL)); 1417 object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT; 1418 1419 if (drro->drr_bonuslen) { 1420 data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8), NULL); 1421 if (ra->err != 0) 1422 return (ra->err); 1423 } 1424 1425 /* 1426 * If we are losing blkptrs or changing the block size this must 1427 * be a new file instance. We must clear out the previous file 1428 * contents before we can change this type of metadata in the dnode. 1429 */ 1430 if (err == 0) { 1431 int nblkptr; 1432 1433 nblkptr = deduce_nblkptr(drro->drr_bonustype, 1434 drro->drr_bonuslen); 1435 1436 if (drro->drr_blksz != doi.doi_data_block_size || 1437 nblkptr < doi.doi_nblkptr) { 1438 err = dmu_free_long_range(os, drro->drr_object, 1439 0, DMU_OBJECT_END); 1440 if (err != 0) 1441 return (SET_ERROR(EINVAL)); 1442 } 1443 } 1444 1445 tx = dmu_tx_create(os); 1446 dmu_tx_hold_bonus(tx, object); 1447 err = dmu_tx_assign(tx, TXG_WAIT); 1448 if (err != 0) { 1449 dmu_tx_abort(tx); 1450 return (err); 1451 } 1452 1453 if (object == DMU_NEW_OBJECT) { 1454 /* currently free, want to be allocated */ 1455 err = dmu_object_claim(os, drro->drr_object, 1456 drro->drr_type, drro->drr_blksz, 1457 drro->drr_bonustype, drro->drr_bonuslen, tx); 1458 } else if (drro->drr_type != doi.doi_type || 1459 drro->drr_blksz != doi.doi_data_block_size || 1460 drro->drr_bonustype != doi.doi_bonus_type || 1461 drro->drr_bonuslen != doi.doi_bonus_size) { 1462 /* currently allocated, but with different properties */ 1463 err = dmu_object_reclaim(os, drro->drr_object, 1464 drro->drr_type, drro->drr_blksz, 1465 drro->drr_bonustype, drro->drr_bonuslen, tx); 1466 } 1467 if (err != 0) { 1468 dmu_tx_commit(tx); 1469 return (SET_ERROR(EINVAL)); 1470 } 1471 1472 dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype, 1473 tx); 1474 dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx); 1475 1476 if (data != NULL) { 1477 dmu_buf_t *db; 1478 1479 VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db)); 1480 dmu_buf_will_dirty(db, tx); 1481 1482 ASSERT3U(db->db_size, >=, drro->drr_bonuslen); 1483 bcopy(data, db->db_data, drro->drr_bonuslen); 1484 if (ra->byteswap) { 1485 dmu_object_byteswap_t byteswap = 1486 DMU_OT_BYTESWAP(drro->drr_bonustype); 1487 dmu_ot_byteswap[byteswap].ob_func(db->db_data, 1488 drro->drr_bonuslen); 1489 } 1490 dmu_buf_rele(db, FTAG); 1491 } 1492 dmu_tx_commit(tx); 1493 return (0); 1494 } 1495 1496 /* ARGSUSED */ 1497 static int 1498 restore_freeobjects(struct restorearg *ra, objset_t *os, 1499 struct drr_freeobjects *drrfo) 1500 { 1501 uint64_t obj; 1502 1503 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj) 1504 return (SET_ERROR(EINVAL)); 1505 1506 for (obj = drrfo->drr_firstobj; 1507 obj < drrfo->drr_firstobj + drrfo->drr_numobjs; 1508 (void) dmu_object_next(os, &obj, FALSE, 0)) { 1509 int err; 1510 1511 if (dmu_object_info(os, obj, NULL) != 0) 1512 continue; 1513 1514 err = dmu_free_long_object(os, obj); 1515 if (err != 0) 1516 return (err); 1517 } 1518 return (0); 1519 } 1520 1521 static int 1522 restore_write(struct restorearg *ra, objset_t *os, 1523 struct drr_write *drrw) 1524 { 1525 dmu_tx_t *tx; 1526 void *data; 1527 int err; 1528 1529 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset || 1530 !DMU_OT_IS_VALID(drrw->drr_type)) 1531 return (SET_ERROR(EINVAL)); 1532 1533 if (dmu_object_info(os, drrw->drr_object, NULL) != 0) 1534 return (SET_ERROR(EINVAL)); 1535 1536 dmu_buf_t *bonus; 1537 if (dmu_bonus_hold(os, drrw->drr_object, FTAG, &bonus) != 0) 1538 return (SET_ERROR(EINVAL)); 1539 1540 arc_buf_t *abuf = dmu_request_arcbuf(bonus, drrw->drr_length); 1541 1542 data = restore_read(ra, drrw->drr_length, abuf->b_data); 1543 if (data == NULL) { 1544 dmu_return_arcbuf(abuf); 1545 dmu_buf_rele(bonus, FTAG); 1546 return (ra->err); 1547 } 1548 1549 tx = dmu_tx_create(os); 1550 1551 dmu_tx_hold_write(tx, drrw->drr_object, 1552 drrw->drr_offset, drrw->drr_length); 1553 err = dmu_tx_assign(tx, TXG_WAIT); 1554 if (err != 0) { 1555 dmu_return_arcbuf(abuf); 1556 dmu_buf_rele(bonus, FTAG); 1557 dmu_tx_abort(tx); 1558 return (err); 1559 } 1560 if (ra->byteswap) { 1561 dmu_object_byteswap_t byteswap = 1562 DMU_OT_BYTESWAP(drrw->drr_type); 1563 dmu_ot_byteswap[byteswap].ob_func(data, drrw->drr_length); 1564 } 1565 dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx); 1566 dmu_tx_commit(tx); 1567 dmu_buf_rele(bonus, FTAG); 1568 return (0); 1569 } 1570 1571 /* 1572 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed 1573 * streams to refer to a copy of the data that is already on the 1574 * system because it came in earlier in the stream. This function 1575 * finds the earlier copy of the data, and uses that copy instead of 1576 * data from the stream to fulfill this write. 1577 */ 1578 static int 1579 restore_write_byref(struct restorearg *ra, objset_t *os, 1580 struct drr_write_byref *drrwbr) 1581 { 1582 dmu_tx_t *tx; 1583 int err; 1584 guid_map_entry_t gmesrch; 1585 guid_map_entry_t *gmep; 1586 avl_index_t where; 1587 objset_t *ref_os = NULL; 1588 dmu_buf_t *dbp; 1589 1590 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset) 1591 return (SET_ERROR(EINVAL)); 1592 1593 /* 1594 * If the GUID of the referenced dataset is different from the 1595 * GUID of the target dataset, find the referenced dataset. 1596 */ 1597 if (drrwbr->drr_toguid != drrwbr->drr_refguid) { 1598 gmesrch.guid = drrwbr->drr_refguid; 1599 if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch, 1600 &where)) == NULL) { 1601 return (SET_ERROR(EINVAL)); 1602 } 1603 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os)) 1604 return (SET_ERROR(EINVAL)); 1605 } else { 1606 ref_os = os; 1607 } 1608 1609 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject, 1610 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH); 1611 if (err != 0) 1612 return (err); 1613 1614 tx = dmu_tx_create(os); 1615 1616 dmu_tx_hold_write(tx, drrwbr->drr_object, 1617 drrwbr->drr_offset, drrwbr->drr_length); 1618 err = dmu_tx_assign(tx, TXG_WAIT); 1619 if (err != 0) { 1620 dmu_tx_abort(tx); 1621 return (err); 1622 } 1623 dmu_write(os, drrwbr->drr_object, 1624 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx); 1625 dmu_buf_rele(dbp, FTAG); 1626 dmu_tx_commit(tx); 1627 return (0); 1628 } 1629 1630 static int 1631 restore_write_embedded(struct restorearg *ra, objset_t *os, 1632 struct drr_write_embedded *drrwnp) 1633 { 1634 dmu_tx_t *tx; 1635 int err; 1636 void *data; 1637 1638 if (drrwnp->drr_offset + drrwnp->drr_length < drrwnp->drr_offset) 1639 return (EINVAL); 1640 1641 if (drrwnp->drr_psize > BPE_PAYLOAD_SIZE) 1642 return (EINVAL); 1643 1644 if (drrwnp->drr_etype >= NUM_BP_EMBEDDED_TYPES) 1645 return (EINVAL); 1646 if (drrwnp->drr_compression >= ZIO_COMPRESS_FUNCTIONS) 1647 return (EINVAL); 1648 1649 data = restore_read(ra, P2ROUNDUP(drrwnp->drr_psize, 8), NULL); 1650 if (data == NULL) 1651 return (ra->err); 1652 1653 tx = dmu_tx_create(os); 1654 1655 dmu_tx_hold_write(tx, drrwnp->drr_object, 1656 drrwnp->drr_offset, drrwnp->drr_length); 1657 err = dmu_tx_assign(tx, TXG_WAIT); 1658 if (err != 0) { 1659 dmu_tx_abort(tx); 1660 return (err); 1661 } 1662 1663 dmu_write_embedded(os, drrwnp->drr_object, 1664 drrwnp->drr_offset, data, drrwnp->drr_etype, 1665 drrwnp->drr_compression, drrwnp->drr_lsize, drrwnp->drr_psize, 1666 ra->byteswap ^ ZFS_HOST_BYTEORDER, tx); 1667 1668 dmu_tx_commit(tx); 1669 return (0); 1670 } 1671 1672 static int 1673 restore_spill(struct restorearg *ra, objset_t *os, struct drr_spill *drrs) 1674 { 1675 dmu_tx_t *tx; 1676 void *data; 1677 dmu_buf_t *db, *db_spill; 1678 int err; 1679 1680 if (drrs->drr_length < SPA_MINBLOCKSIZE || 1681 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(os))) 1682 return (SET_ERROR(EINVAL)); 1683 1684 data = restore_read(ra, drrs->drr_length, NULL); 1685 if (data == NULL) 1686 return (ra->err); 1687 1688 if (dmu_object_info(os, drrs->drr_object, NULL) != 0) 1689 return (SET_ERROR(EINVAL)); 1690 1691 VERIFY(0 == dmu_bonus_hold(os, drrs->drr_object, FTAG, &db)); 1692 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) { 1693 dmu_buf_rele(db, FTAG); 1694 return (err); 1695 } 1696 1697 tx = dmu_tx_create(os); 1698 1699 dmu_tx_hold_spill(tx, db->db_object); 1700 1701 err = dmu_tx_assign(tx, TXG_WAIT); 1702 if (err != 0) { 1703 dmu_buf_rele(db, FTAG); 1704 dmu_buf_rele(db_spill, FTAG); 1705 dmu_tx_abort(tx); 1706 return (err); 1707 } 1708 dmu_buf_will_dirty(db_spill, tx); 1709 1710 if (db_spill->db_size < drrs->drr_length) 1711 VERIFY(0 == dbuf_spill_set_blksz(db_spill, 1712 drrs->drr_length, tx)); 1713 bcopy(data, db_spill->db_data, drrs->drr_length); 1714 1715 dmu_buf_rele(db, FTAG); 1716 dmu_buf_rele(db_spill, FTAG); 1717 1718 dmu_tx_commit(tx); 1719 return (0); 1720 } 1721 1722 /* ARGSUSED */ 1723 static int 1724 restore_free(struct restorearg *ra, objset_t *os, 1725 struct drr_free *drrf) 1726 { 1727 int err; 1728 uint64_t length = drrf->drr_length; 1729 uint64_t offset = drrf->drr_offset; 1730 1731 if (length != -1ULL && offset + length < offset) 1732 length = -1ULL; 1733 1734 if (dmu_object_info(os, drrf->drr_object, NULL) != 0) 1735 return (SET_ERROR(EINVAL)); 1736 1737 err = dmu_free_long_range(os, drrf->drr_object, offset, length); 1738 1739 return (err); 1740 } 1741 1742 /* used to destroy the drc_ds on error */ 1743 static void 1744 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc) 1745 { 1746 char name[MAXNAMELEN]; 1747 dsl_dataset_name(drc->drc_ds, name); 1748 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag); 1749 (void) dsl_destroy_head(name); 1750 } 1751 1752 /* 1753 * NB: callers *must* call dmu_recv_end() if this succeeds. 1754 */ 1755 int 1756 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp, 1757 int cleanup_fd, uint64_t *action_handlep) 1758 { 1759 struct restorearg ra = { 0 }; 1760 dmu_replay_record_t *drr; 1761 objset_t *os; 1762 zio_cksum_t pcksum; 1763 int featureflags; 1764 1765 ra.byteswap = drc->drc_byteswap; 1766 ra.cksum = drc->drc_cksum; 1767 ra.vp = vp; 1768 ra.voff = *voffp; 1769 ra.bufsize = SPA_MAXBLOCKSIZE; 1770 ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP); 1771 1772 /* these were verified in dmu_recv_begin */ 1773 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==, 1774 DMU_SUBSTREAM); 1775 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES); 1776 1777 /* 1778 * Open the objset we are modifying. 1779 */ 1780 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &os)); 1781 1782 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT); 1783 1784 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo); 1785 1786 /* if this stream is dedup'ed, set up the avl tree for guid mapping */ 1787 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) { 1788 minor_t minor; 1789 1790 if (cleanup_fd == -1) { 1791 ra.err = SET_ERROR(EBADF); 1792 goto out; 1793 } 1794 ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor); 1795 if (ra.err != 0) { 1796 cleanup_fd = -1; 1797 goto out; 1798 } 1799 1800 if (*action_handlep == 0) { 1801 ra.guid_to_ds_map = 1802 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP); 1803 avl_create(ra.guid_to_ds_map, guid_compare, 1804 sizeof (guid_map_entry_t), 1805 offsetof(guid_map_entry_t, avlnode)); 1806 ra.err = zfs_onexit_add_cb(minor, 1807 free_guid_map_onexit, ra.guid_to_ds_map, 1808 action_handlep); 1809 if (ra.err != 0) 1810 goto out; 1811 } else { 1812 ra.err = zfs_onexit_cb_data(minor, *action_handlep, 1813 (void **)&ra.guid_to_ds_map); 1814 if (ra.err != 0) 1815 goto out; 1816 } 1817 1818 drc->drc_guid_to_ds_map = ra.guid_to_ds_map; 1819 } 1820 1821 /* 1822 * Read records and process them. 1823 */ 1824 pcksum = ra.cksum; 1825 while (ra.err == 0 && 1826 NULL != (drr = restore_read(&ra, sizeof (*drr), NULL))) { 1827 if (issig(JUSTLOOKING) && issig(FORREAL)) { 1828 ra.err = SET_ERROR(EINTR); 1829 goto out; 1830 } 1831 1832 if (ra.byteswap) 1833 backup_byteswap(drr); 1834 1835 switch (drr->drr_type) { 1836 case DRR_OBJECT: 1837 { 1838 /* 1839 * We need to make a copy of the record header, 1840 * because restore_{object,write} may need to 1841 * restore_read(), which will invalidate drr. 1842 */ 1843 struct drr_object drro = drr->drr_u.drr_object; 1844 ra.err = restore_object(&ra, os, &drro); 1845 break; 1846 } 1847 case DRR_FREEOBJECTS: 1848 { 1849 struct drr_freeobjects drrfo = 1850 drr->drr_u.drr_freeobjects; 1851 ra.err = restore_freeobjects(&ra, os, &drrfo); 1852 break; 1853 } 1854 case DRR_WRITE: 1855 { 1856 struct drr_write drrw = drr->drr_u.drr_write; 1857 ra.err = restore_write(&ra, os, &drrw); 1858 break; 1859 } 1860 case DRR_WRITE_BYREF: 1861 { 1862 struct drr_write_byref drrwbr = 1863 drr->drr_u.drr_write_byref; 1864 ra.err = restore_write_byref(&ra, os, &drrwbr); 1865 break; 1866 } 1867 case DRR_WRITE_EMBEDDED: 1868 { 1869 struct drr_write_embedded drrwe = 1870 drr->drr_u.drr_write_embedded; 1871 ra.err = restore_write_embedded(&ra, os, &drrwe); 1872 break; 1873 } 1874 case DRR_FREE: 1875 { 1876 struct drr_free drrf = drr->drr_u.drr_free; 1877 ra.err = restore_free(&ra, os, &drrf); 1878 break; 1879 } 1880 case DRR_END: 1881 { 1882 struct drr_end drre = drr->drr_u.drr_end; 1883 /* 1884 * We compare against the *previous* checksum 1885 * value, because the stored checksum is of 1886 * everything before the DRR_END record. 1887 */ 1888 if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum)) 1889 ra.err = SET_ERROR(ECKSUM); 1890 goto out; 1891 } 1892 case DRR_SPILL: 1893 { 1894 struct drr_spill drrs = drr->drr_u.drr_spill; 1895 ra.err = restore_spill(&ra, os, &drrs); 1896 break; 1897 } 1898 default: 1899 ra.err = SET_ERROR(EINVAL); 1900 goto out; 1901 } 1902 pcksum = ra.cksum; 1903 } 1904 ASSERT(ra.err != 0); 1905 1906 out: 1907 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1)) 1908 zfs_onexit_fd_rele(cleanup_fd); 1909 1910 if (ra.err != 0) { 1911 /* 1912 * destroy what we created, so we don't leave it in the 1913 * inconsistent restoring state. 1914 */ 1915 dmu_recv_cleanup_ds(drc); 1916 } 1917 1918 kmem_free(ra.buf, ra.bufsize); 1919 *voffp = ra.voff; 1920 return (ra.err); 1921 } 1922 1923 static int 1924 dmu_recv_end_check(void *arg, dmu_tx_t *tx) 1925 { 1926 dmu_recv_cookie_t *drc = arg; 1927 dsl_pool_t *dp = dmu_tx_pool(tx); 1928 int error; 1929 1930 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag); 1931 1932 if (!drc->drc_newfs) { 1933 dsl_dataset_t *origin_head; 1934 1935 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head); 1936 if (error != 0) 1937 return (error); 1938 if (drc->drc_force) { 1939 /* 1940 * We will destroy any snapshots in tofs (i.e. before 1941 * origin_head) that are after the origin (which is 1942 * the snap before drc_ds, because drc_ds can not 1943 * have any snaps of its own). 1944 */ 1945 uint64_t obj; 1946 1947 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj; 1948 while (obj != 1949 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) { 1950 dsl_dataset_t *snap; 1951 error = dsl_dataset_hold_obj(dp, obj, FTAG, 1952 &snap); 1953 if (error != 0) 1954 return (error); 1955 if (snap->ds_dir != origin_head->ds_dir) 1956 error = SET_ERROR(EINVAL); 1957 if (error == 0) { 1958 error = dsl_destroy_snapshot_check_impl( 1959 snap, B_FALSE); 1960 } 1961 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 1962 dsl_dataset_rele(snap, FTAG); 1963 if (error != 0) 1964 return (error); 1965 } 1966 } 1967 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds, 1968 origin_head, drc->drc_force, drc->drc_owner, tx); 1969 if (error != 0) { 1970 dsl_dataset_rele(origin_head, FTAG); 1971 return (error); 1972 } 1973 error = dsl_dataset_snapshot_check_impl(origin_head, 1974 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred); 1975 dsl_dataset_rele(origin_head, FTAG); 1976 if (error != 0) 1977 return (error); 1978 1979 error = dsl_destroy_head_check_impl(drc->drc_ds, 1); 1980 } else { 1981 error = dsl_dataset_snapshot_check_impl(drc->drc_ds, 1982 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred); 1983 } 1984 return (error); 1985 } 1986 1987 static void 1988 dmu_recv_end_sync(void *arg, dmu_tx_t *tx) 1989 { 1990 dmu_recv_cookie_t *drc = arg; 1991 dsl_pool_t *dp = dmu_tx_pool(tx); 1992 1993 spa_history_log_internal_ds(drc->drc_ds, "finish receiving", 1994 tx, "snap=%s", drc->drc_tosnap); 1995 1996 if (!drc->drc_newfs) { 1997 dsl_dataset_t *origin_head; 1998 1999 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG, 2000 &origin_head)); 2001 2002 if (drc->drc_force) { 2003 /* 2004 * Destroy any snapshots of drc_tofs (origin_head) 2005 * after the origin (the snap before drc_ds). 2006 */ 2007 uint64_t obj; 2008 2009 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj; 2010 while (obj != 2011 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) { 2012 dsl_dataset_t *snap; 2013 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, 2014 &snap)); 2015 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir); 2016 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj; 2017 dsl_destroy_snapshot_sync_impl(snap, 2018 B_FALSE, tx); 2019 dsl_dataset_rele(snap, FTAG); 2020 } 2021 } 2022 VERIFY3P(drc->drc_ds->ds_prev, ==, 2023 origin_head->ds_prev); 2024 2025 dsl_dataset_clone_swap_sync_impl(drc->drc_ds, 2026 origin_head, tx); 2027 dsl_dataset_snapshot_sync_impl(origin_head, 2028 drc->drc_tosnap, tx); 2029 2030 /* set snapshot's creation time and guid */ 2031 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx); 2032 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time = 2033 drc->drc_drrb->drr_creation_time; 2034 dsl_dataset_phys(origin_head->ds_prev)->ds_guid = 2035 drc->drc_drrb->drr_toguid; 2036 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &= 2037 ~DS_FLAG_INCONSISTENT; 2038 2039 dmu_buf_will_dirty(origin_head->ds_dbuf, tx); 2040 dsl_dataset_phys(origin_head)->ds_flags &= 2041 ~DS_FLAG_INCONSISTENT; 2042 2043 dsl_dataset_rele(origin_head, FTAG); 2044 dsl_destroy_head_sync_impl(drc->drc_ds, tx); 2045 2046 if (drc->drc_owner != NULL) 2047 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner); 2048 } else { 2049 dsl_dataset_t *ds = drc->drc_ds; 2050 2051 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx); 2052 2053 /* set snapshot's creation time and guid */ 2054 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 2055 dsl_dataset_phys(ds->ds_prev)->ds_creation_time = 2056 drc->drc_drrb->drr_creation_time; 2057 dsl_dataset_phys(ds->ds_prev)->ds_guid = 2058 drc->drc_drrb->drr_toguid; 2059 dsl_dataset_phys(ds->ds_prev)->ds_flags &= 2060 ~DS_FLAG_INCONSISTENT; 2061 2062 dmu_buf_will_dirty(ds->ds_dbuf, tx); 2063 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT; 2064 } 2065 drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj; 2066 /* 2067 * Release the hold from dmu_recv_begin. This must be done before 2068 * we return to open context, so that when we free the dataset's dnode, 2069 * we can evict its bonus buffer. 2070 */ 2071 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag); 2072 drc->drc_ds = NULL; 2073 } 2074 2075 static int 2076 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj) 2077 { 2078 dsl_pool_t *dp; 2079 dsl_dataset_t *snapds; 2080 guid_map_entry_t *gmep; 2081 int err; 2082 2083 ASSERT(guid_map != NULL); 2084 2085 err = dsl_pool_hold(name, FTAG, &dp); 2086 if (err != 0) 2087 return (err); 2088 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP); 2089 err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds); 2090 if (err == 0) { 2091 gmep->guid = dsl_dataset_phys(snapds)->ds_guid; 2092 gmep->gme_ds = snapds; 2093 avl_add(guid_map, gmep); 2094 dsl_dataset_long_hold(snapds, gmep); 2095 } else { 2096 kmem_free(gmep, sizeof (*gmep)); 2097 } 2098 2099 dsl_pool_rele(dp, FTAG); 2100 return (err); 2101 } 2102 2103 static int dmu_recv_end_modified_blocks = 3; 2104 2105 static int 2106 dmu_recv_existing_end(dmu_recv_cookie_t *drc) 2107 { 2108 int error; 2109 char name[MAXNAMELEN]; 2110 2111 #ifdef _KERNEL 2112 /* 2113 * We will be destroying the ds; make sure its origin is unmounted if 2114 * necessary. 2115 */ 2116 dsl_dataset_name(drc->drc_ds, name); 2117 zfs_destroy_unmount_origin(name); 2118 #endif 2119 2120 error = dsl_sync_task(drc->drc_tofs, 2121 dmu_recv_end_check, dmu_recv_end_sync, drc, 2122 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL); 2123 2124 if (error != 0) 2125 dmu_recv_cleanup_ds(drc); 2126 return (error); 2127 } 2128 2129 static int 2130 dmu_recv_new_end(dmu_recv_cookie_t *drc) 2131 { 2132 int error; 2133 2134 error = dsl_sync_task(drc->drc_tofs, 2135 dmu_recv_end_check, dmu_recv_end_sync, drc, 2136 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL); 2137 2138 if (error != 0) { 2139 dmu_recv_cleanup_ds(drc); 2140 } else if (drc->drc_guid_to_ds_map != NULL) { 2141 (void) add_ds_to_guidmap(drc->drc_tofs, 2142 drc->drc_guid_to_ds_map, 2143 drc->drc_newsnapobj); 2144 } 2145 return (error); 2146 } 2147 2148 int 2149 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner) 2150 { 2151 drc->drc_owner = owner; 2152 2153 if (drc->drc_newfs) 2154 return (dmu_recv_new_end(drc)); 2155 else 2156 return (dmu_recv_existing_end(drc)); 2157 } 2158 2159 /* 2160 * Return TRUE if this objset is currently being received into. 2161 */ 2162 boolean_t 2163 dmu_objset_is_receiving(objset_t *os) 2164 { 2165 return (os->os_dsl_dataset != NULL && 2166 os->os_dsl_dataset->ds_owner == dmu_recv_tag); 2167 } 2168