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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/dmu.h> 27 #include <sys/dmu_impl.h> 28 #include <sys/dmu_tx.h> 29 #include <sys/dbuf.h> 30 #include <sys/dnode.h> 31 #include <sys/zfs_context.h> 32 #include <sys/dmu_objset.h> 33 #include <sys/dmu_traverse.h> 34 #include <sys/dsl_dataset.h> 35 #include <sys/dsl_dir.h> 36 #include <sys/dsl_pool.h> 37 #include <sys/dsl_synctask.h> 38 #include <sys/zfs_ioctl.h> 39 #include <sys/zap.h> 40 #include <sys/zio_checksum.h> 41 #include <sys/avl.h> 42 43 static char *dmu_recv_tag = "dmu_recv_tag"; 44 45 /* 46 * The list of data whose inclusion in a send stream can be pending from 47 * one call to backup_cb to another. Multiple calls to dump_free() and 48 * dump_freeobjects() can be aggregated into a single DRR_FREE or 49 * DRR_FREEOBJECTS replay record. 50 */ 51 typedef enum { 52 PENDING_NONE, 53 PENDING_FREE, 54 PENDING_FREEOBJECTS 55 } pendop_t; 56 57 struct backuparg { 58 dmu_replay_record_t *drr; 59 vnode_t *vp; 60 offset_t *off; 61 objset_t *os; 62 zio_cksum_t zc; 63 uint64_t toguid; 64 int err; 65 pendop_t pending_op; 66 }; 67 68 static int 69 dump_bytes(struct backuparg *ba, void *buf, int len) 70 { 71 ssize_t resid; /* have to get resid to get detailed errno */ 72 ASSERT3U(len % 8, ==, 0); 73 74 fletcher_4_incremental_native(buf, len, &ba->zc); 75 ba->err = vn_rdwr(UIO_WRITE, ba->vp, 76 (caddr_t)buf, len, 77 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid); 78 *ba->off += len; 79 return (ba->err); 80 } 81 82 static int 83 dump_free(struct backuparg *ba, uint64_t object, uint64_t offset, 84 uint64_t length) 85 { 86 struct drr_free *drrf = &(ba->drr->drr_u.drr_free); 87 88 /* 89 * If there is a pending op, but it's not PENDING_FREE, push it out, 90 * since free block aggregation can only be done for blocks of the 91 * same type (i.e., DRR_FREE records can only be aggregated with 92 * other DRR_FREE records. DRR_FREEOBJECTS records can only be 93 * aggregated with other DRR_FREEOBJECTS records. 94 */ 95 if (ba->pending_op != PENDING_NONE && ba->pending_op != PENDING_FREE) { 96 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 97 return (EINTR); 98 ba->pending_op = PENDING_NONE; 99 } 100 101 if (ba->pending_op == PENDING_FREE) { 102 /* 103 * There should never be a PENDING_FREE if length is -1 104 * (because dump_dnode is the only place where this 105 * function is called with a -1, and only after flushing 106 * any pending record). 107 */ 108 ASSERT(length != -1ULL); 109 /* 110 * Check to see whether this free block can be aggregated 111 * with pending one. 112 */ 113 if (drrf->drr_object == object && drrf->drr_offset + 114 drrf->drr_length == offset) { 115 drrf->drr_length += length; 116 return (0); 117 } else { 118 /* not a continuation. Push out pending record */ 119 if (dump_bytes(ba, ba->drr, 120 sizeof (dmu_replay_record_t)) != 0) 121 return (EINTR); 122 ba->pending_op = PENDING_NONE; 123 } 124 } 125 /* create a FREE record and make it pending */ 126 bzero(ba->drr, sizeof (dmu_replay_record_t)); 127 ba->drr->drr_type = DRR_FREE; 128 drrf->drr_object = object; 129 drrf->drr_offset = offset; 130 drrf->drr_length = length; 131 drrf->drr_toguid = ba->toguid; 132 if (length == -1ULL) { 133 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 134 return (EINTR); 135 } else { 136 ba->pending_op = PENDING_FREE; 137 } 138 139 return (0); 140 } 141 142 static int 143 dump_data(struct backuparg *ba, dmu_object_type_t type, 144 uint64_t object, uint64_t offset, int blksz, void *data) 145 { 146 struct drr_write *drrw = &(ba->drr->drr_u.drr_write); 147 148 /* 149 * If there is any kind of pending aggregation (currently either 150 * a grouping of free objects or free blocks), push it out to 151 * the stream, since aggregation can't be done across operations 152 * of different types. 153 */ 154 if (ba->pending_op != PENDING_NONE) { 155 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 156 return (EINTR); 157 ba->pending_op = PENDING_NONE; 158 } 159 /* write a DATA record */ 160 bzero(ba->drr, sizeof (dmu_replay_record_t)); 161 ba->drr->drr_type = DRR_WRITE; 162 drrw->drr_object = object; 163 drrw->drr_type = type; 164 drrw->drr_offset = offset; 165 drrw->drr_length = blksz; 166 drrw->drr_toguid = ba->toguid; 167 168 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 169 return (EINTR); 170 if (dump_bytes(ba, data, blksz) != 0) 171 return (EINTR); 172 return (0); 173 } 174 175 static int 176 dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs) 177 { 178 struct drr_freeobjects *drrfo = &(ba->drr->drr_u.drr_freeobjects); 179 180 /* 181 * If there is a pending op, but it's not PENDING_FREEOBJECTS, 182 * push it out, since free block aggregation can only be done for 183 * blocks of the same type (i.e., DRR_FREE records can only be 184 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records 185 * can only be aggregated with other DRR_FREEOBJECTS records. 186 */ 187 if (ba->pending_op != PENDING_NONE && 188 ba->pending_op != PENDING_FREEOBJECTS) { 189 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 190 return (EINTR); 191 ba->pending_op = PENDING_NONE; 192 } 193 if (ba->pending_op == PENDING_FREEOBJECTS) { 194 /* 195 * See whether this free object array can be aggregated 196 * with pending one 197 */ 198 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) { 199 drrfo->drr_numobjs += numobjs; 200 return (0); 201 } else { 202 /* can't be aggregated. Push out pending record */ 203 if (dump_bytes(ba, ba->drr, 204 sizeof (dmu_replay_record_t)) != 0) 205 return (EINTR); 206 ba->pending_op = PENDING_NONE; 207 } 208 } 209 210 /* write a FREEOBJECTS record */ 211 bzero(ba->drr, sizeof (dmu_replay_record_t)); 212 ba->drr->drr_type = DRR_FREEOBJECTS; 213 drrfo->drr_firstobj = firstobj; 214 drrfo->drr_numobjs = numobjs; 215 drrfo->drr_toguid = ba->toguid; 216 217 ba->pending_op = PENDING_FREEOBJECTS; 218 219 return (0); 220 } 221 222 static int 223 dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp) 224 { 225 struct drr_object *drro = &(ba->drr->drr_u.drr_object); 226 227 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE) 228 return (dump_freeobjects(ba, object, 1)); 229 230 if (ba->pending_op != PENDING_NONE) { 231 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 232 return (EINTR); 233 ba->pending_op = PENDING_NONE; 234 } 235 236 /* write an OBJECT record */ 237 bzero(ba->drr, sizeof (dmu_replay_record_t)); 238 ba->drr->drr_type = DRR_OBJECT; 239 drro->drr_object = object; 240 drro->drr_type = dnp->dn_type; 241 drro->drr_bonustype = dnp->dn_bonustype; 242 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; 243 drro->drr_bonuslen = dnp->dn_bonuslen; 244 drro->drr_checksumtype = dnp->dn_checksum; 245 drro->drr_compress = dnp->dn_compress; 246 drro->drr_toguid = ba->toguid; 247 248 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0) 249 return (EINTR); 250 251 if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) 252 return (EINTR); 253 254 /* free anything past the end of the file */ 255 if (dump_free(ba, object, (dnp->dn_maxblkid + 1) * 256 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL)) 257 return (EINTR); 258 if (ba->err) 259 return (EINTR); 260 return (0); 261 } 262 263 #define BP_SPAN(dnp, level) \ 264 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \ 265 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) 266 267 /* ARGSUSED */ 268 static int 269 backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 270 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 271 { 272 struct backuparg *ba = arg; 273 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE; 274 int err = 0; 275 276 if (issig(JUSTLOOKING) && issig(FORREAL)) 277 return (EINTR); 278 279 if (zb->zb_object != DMU_META_DNODE_OBJECT && 280 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) { 281 return (0); 282 } else if (bp == NULL && zb->zb_object == DMU_META_DNODE_OBJECT) { 283 uint64_t span = BP_SPAN(dnp, zb->zb_level); 284 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT; 285 err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT); 286 } else if (bp == NULL) { 287 uint64_t span = BP_SPAN(dnp, zb->zb_level); 288 err = dump_free(ba, zb->zb_object, zb->zb_blkid * span, span); 289 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) { 290 return (0); 291 } else if (type == DMU_OT_DNODE) { 292 dnode_phys_t *blk; 293 int i; 294 int blksz = BP_GET_LSIZE(bp); 295 uint32_t aflags = ARC_WAIT; 296 arc_buf_t *abuf; 297 298 if (arc_read_nolock(NULL, spa, bp, 299 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 300 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) 301 return (EIO); 302 303 blk = abuf->b_data; 304 for (i = 0; i < blksz >> DNODE_SHIFT; i++) { 305 uint64_t dnobj = (zb->zb_blkid << 306 (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i; 307 err = dump_dnode(ba, dnobj, blk+i); 308 if (err) 309 break; 310 } 311 (void) arc_buf_remove_ref(abuf, &abuf); 312 } else { /* it's a level-0 block of a regular object */ 313 uint32_t aflags = ARC_WAIT; 314 arc_buf_t *abuf; 315 int blksz = BP_GET_LSIZE(bp); 316 317 if (arc_read_nolock(NULL, spa, bp, 318 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 319 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) 320 return (EIO); 321 322 err = dump_data(ba, type, zb->zb_object, zb->zb_blkid * blksz, 323 blksz, abuf->b_data); 324 (void) arc_buf_remove_ref(abuf, &abuf); 325 } 326 327 ASSERT(err == 0 || err == EINTR); 328 return (err); 329 } 330 331 int 332 dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin, 333 vnode_t *vp, offset_t *off) 334 { 335 dsl_dataset_t *ds = tosnap->os_dsl_dataset; 336 dsl_dataset_t *fromds = fromsnap ? fromsnap->os_dsl_dataset : NULL; 337 dmu_replay_record_t *drr; 338 struct backuparg ba; 339 int err; 340 uint64_t fromtxg = 0; 341 342 /* tosnap must be a snapshot */ 343 if (ds->ds_phys->ds_next_snap_obj == 0) 344 return (EINVAL); 345 346 /* fromsnap must be an earlier snapshot from the same fs as tosnap */ 347 if (fromds && (ds->ds_dir != fromds->ds_dir || 348 fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg)) 349 return (EXDEV); 350 351 if (fromorigin) { 352 dsl_pool_t *dp = ds->ds_dir->dd_pool; 353 354 if (fromsnap) 355 return (EINVAL); 356 357 if (dsl_dir_is_clone(ds->ds_dir)) { 358 rw_enter(&dp->dp_config_rwlock, RW_READER); 359 err = dsl_dataset_hold_obj(dp, 360 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds); 361 rw_exit(&dp->dp_config_rwlock); 362 if (err) 363 return (err); 364 } else { 365 fromorigin = B_FALSE; 366 } 367 } 368 369 370 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 371 drr->drr_type = DRR_BEGIN; 372 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 373 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo, 374 DMU_SUBSTREAM); 375 drr->drr_u.drr_begin.drr_creation_time = 376 ds->ds_phys->ds_creation_time; 377 drr->drr_u.drr_begin.drr_type = tosnap->os_phys->os_type; 378 if (fromorigin) 379 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE; 380 drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid; 381 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 382 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA; 383 384 if (fromds) 385 drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid; 386 dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname); 387 388 if (fromds) 389 fromtxg = fromds->ds_phys->ds_creation_txg; 390 if (fromorigin) 391 dsl_dataset_rele(fromds, FTAG); 392 393 ba.drr = drr; 394 ba.vp = vp; 395 ba.os = tosnap; 396 ba.off = off; 397 ba.toguid = ds->ds_phys->ds_guid; 398 ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0); 399 ba.pending_op = PENDING_NONE; 400 401 if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)) != 0) { 402 kmem_free(drr, sizeof (dmu_replay_record_t)); 403 return (ba.err); 404 } 405 406 err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH, 407 backup_cb, &ba); 408 409 if (ba.pending_op != PENDING_NONE) 410 if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)) != 0) 411 err = EINTR; 412 413 if (err) { 414 if (err == EINTR && ba.err) 415 err = ba.err; 416 kmem_free(drr, sizeof (dmu_replay_record_t)); 417 return (err); 418 } 419 420 bzero(drr, sizeof (dmu_replay_record_t)); 421 drr->drr_type = DRR_END; 422 drr->drr_u.drr_end.drr_checksum = ba.zc; 423 drr->drr_u.drr_end.drr_toguid = ba.toguid; 424 425 if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)) != 0) { 426 kmem_free(drr, sizeof (dmu_replay_record_t)); 427 return (ba.err); 428 } 429 430 kmem_free(drr, sizeof (dmu_replay_record_t)); 431 432 return (0); 433 } 434 435 struct recvbeginsyncarg { 436 const char *tofs; 437 const char *tosnap; 438 dsl_dataset_t *origin; 439 uint64_t fromguid; 440 dmu_objset_type_t type; 441 void *tag; 442 boolean_t force; 443 uint64_t dsflags; 444 char clonelastname[MAXNAMELEN]; 445 dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */ 446 }; 447 448 /* ARGSUSED */ 449 static int 450 recv_new_check(void *arg1, void *arg2, dmu_tx_t *tx) 451 { 452 dsl_dir_t *dd = arg1; 453 struct recvbeginsyncarg *rbsa = arg2; 454 objset_t *mos = dd->dd_pool->dp_meta_objset; 455 uint64_t val; 456 int err; 457 458 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 459 strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val); 460 461 if (err != ENOENT) 462 return (err ? err : EEXIST); 463 464 if (rbsa->origin) { 465 /* make sure it's a snap in the same pool */ 466 if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool) 467 return (EXDEV); 468 if (!dsl_dataset_is_snapshot(rbsa->origin)) 469 return (EINVAL); 470 if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid) 471 return (ENODEV); 472 } 473 474 return (0); 475 } 476 477 static void 478 recv_new_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 479 { 480 dsl_dir_t *dd = arg1; 481 struct recvbeginsyncarg *rbsa = arg2; 482 uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags; 483 uint64_t dsobj; 484 485 /* Create and open new dataset. */ 486 dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1, 487 rbsa->origin, flags, cr, tx); 488 VERIFY(0 == dsl_dataset_own_obj(dd->dd_pool, dsobj, 489 B_TRUE, dmu_recv_tag, &rbsa->ds)); 490 491 if (rbsa->origin == NULL) { 492 (void) dmu_objset_create_impl(dd->dd_pool->dp_spa, 493 rbsa->ds, &rbsa->ds->ds_phys->ds_bp, rbsa->type, tx); 494 } 495 496 spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC, 497 dd->dd_pool->dp_spa, tx, cr, "dataset = %lld", dsobj); 498 } 499 500 /* ARGSUSED */ 501 static int 502 recv_existing_check(void *arg1, void *arg2, dmu_tx_t *tx) 503 { 504 dsl_dataset_t *ds = arg1; 505 struct recvbeginsyncarg *rbsa = arg2; 506 int err; 507 uint64_t val; 508 509 /* must not have any changes since most recent snapshot */ 510 if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds)) 511 return (ETXTBSY); 512 513 if (rbsa->fromguid) { 514 /* if incremental, most recent snapshot must match fromguid */ 515 if (ds->ds_prev == NULL) 516 return (ENODEV); 517 if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid) 518 return (ENODEV); 519 } else { 520 /* if full, most recent snapshot must be $ORIGIN */ 521 if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL) 522 return (ENODEV); 523 } 524 525 /* temporary clone name must not exist */ 526 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 527 ds->ds_dir->dd_phys->dd_child_dir_zapobj, 528 rbsa->clonelastname, 8, 1, &val); 529 if (err == 0) 530 return (EEXIST); 531 if (err != ENOENT) 532 return (err); 533 534 /* new snapshot name must not exist */ 535 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 536 ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val); 537 if (err == 0) 538 return (EEXIST); 539 if (err != ENOENT) 540 return (err); 541 return (0); 542 } 543 544 /* ARGSUSED */ 545 static void 546 recv_existing_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 547 { 548 dsl_dataset_t *ohds = arg1; 549 struct recvbeginsyncarg *rbsa = arg2; 550 dsl_pool_t *dp = ohds->ds_dir->dd_pool; 551 dsl_dataset_t *cds; 552 uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags; 553 uint64_t dsobj; 554 555 /* create and open the temporary clone */ 556 dsobj = dsl_dataset_create_sync(ohds->ds_dir, rbsa->clonelastname, 557 ohds->ds_prev, flags, cr, tx); 558 VERIFY(0 == dsl_dataset_own_obj(dp, dsobj, B_TRUE, dmu_recv_tag, &cds)); 559 560 /* 561 * If we actually created a non-clone, we need to create the 562 * objset in our new dataset. 563 */ 564 if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds))) { 565 (void) dmu_objset_create_impl(dp->dp_spa, 566 cds, dsl_dataset_get_blkptr(cds), rbsa->type, tx); 567 } 568 569 /* copy the refquota from the target fs to the clone */ 570 if (ohds->ds_quota > 0) 571 dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx); 572 573 rbsa->ds = cds; 574 575 spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC, 576 dp->dp_spa, tx, cr, "dataset = %lld", dsobj); 577 } 578 579 /* 580 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin() 581 * succeeds; otherwise we will leak the holds on the datasets. 582 */ 583 int 584 dmu_recv_begin(char *tofs, char *tosnap, char *top_ds, struct drr_begin *drrb, 585 boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc) 586 { 587 int err = 0; 588 boolean_t byteswap; 589 struct recvbeginsyncarg rbsa = { 0 }; 590 uint64_t versioninfo; 591 int flags; 592 dsl_dataset_t *ds; 593 594 if (drrb->drr_magic == DMU_BACKUP_MAGIC) 595 byteswap = FALSE; 596 else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 597 byteswap = TRUE; 598 else 599 return (EINVAL); 600 601 rbsa.tofs = tofs; 602 rbsa.tosnap = tosnap; 603 rbsa.origin = origin ? origin->os_dsl_dataset : NULL; 604 rbsa.fromguid = drrb->drr_fromguid; 605 rbsa.type = drrb->drr_type; 606 rbsa.tag = FTAG; 607 rbsa.dsflags = 0; 608 versioninfo = drrb->drr_versioninfo; 609 flags = drrb->drr_flags; 610 611 if (byteswap) { 612 rbsa.type = BSWAP_32(rbsa.type); 613 rbsa.fromguid = BSWAP_64(rbsa.fromguid); 614 versioninfo = BSWAP_64(versioninfo); 615 flags = BSWAP_32(flags); 616 } 617 618 if (DMU_GET_STREAM_HDRTYPE(versioninfo) == DMU_COMPOUNDSTREAM || 619 rbsa.type >= DMU_OST_NUMTYPES || 620 ((flags & DRR_FLAG_CLONE) && origin == NULL)) 621 return (EINVAL); 622 623 if (flags & DRR_FLAG_CI_DATA) 624 rbsa.dsflags = DS_FLAG_CI_DATASET; 625 626 bzero(drc, sizeof (dmu_recv_cookie_t)); 627 drc->drc_drrb = drrb; 628 drc->drc_tosnap = tosnap; 629 drc->drc_top_ds = top_ds; 630 drc->drc_force = force; 631 632 /* 633 * Process the begin in syncing context. 634 */ 635 636 /* open the dataset we are logically receiving into */ 637 err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds); 638 if (err == 0) { 639 /* target fs already exists; recv into temp clone */ 640 641 /* Can't recv a clone into an existing fs */ 642 if (flags & DRR_FLAG_CLONE) { 643 dsl_dataset_rele(ds, dmu_recv_tag); 644 return (EINVAL); 645 } 646 647 /* must not have an incremental recv already in progress */ 648 if (!mutex_tryenter(&ds->ds_recvlock)) { 649 dsl_dataset_rele(ds, dmu_recv_tag); 650 return (EBUSY); 651 } 652 653 /* tmp clone name is: tofs/%tosnap" */ 654 (void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname), 655 "%%%s", tosnap); 656 rbsa.force = force; 657 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 658 recv_existing_check, recv_existing_sync, ds, &rbsa, 5); 659 if (err) { 660 mutex_exit(&ds->ds_recvlock); 661 dsl_dataset_rele(ds, dmu_recv_tag); 662 return (err); 663 } 664 drc->drc_logical_ds = ds; 665 drc->drc_real_ds = rbsa.ds; 666 } else if (err == ENOENT) { 667 /* target fs does not exist; must be a full backup or clone */ 668 char *cp; 669 670 /* 671 * If it's a non-clone incremental, we are missing the 672 * target fs, so fail the recv. 673 */ 674 if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE)) 675 return (ENOENT); 676 677 /* Open the parent of tofs */ 678 cp = strrchr(tofs, '/'); 679 *cp = '\0'; 680 err = dsl_dataset_hold(tofs, FTAG, &ds); 681 *cp = '/'; 682 if (err) 683 return (err); 684 685 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 686 recv_new_check, recv_new_sync, ds->ds_dir, &rbsa, 5); 687 dsl_dataset_rele(ds, FTAG); 688 if (err) 689 return (err); 690 drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds; 691 drc->drc_newfs = B_TRUE; 692 } 693 694 return (err); 695 } 696 697 struct restorearg { 698 int err; 699 int byteswap; 700 vnode_t *vp; 701 char *buf; 702 uint64_t voff; 703 int bufsize; /* amount of memory allocated for buf */ 704 zio_cksum_t cksum; 705 avl_tree_t guid_to_ds_map; 706 }; 707 708 typedef struct guid_map_entry { 709 uint64_t guid; 710 dsl_dataset_t *gme_ds; 711 avl_node_t avlnode; 712 } guid_map_entry_t; 713 714 static int 715 guid_compare(const void *arg1, const void *arg2) 716 { 717 const guid_map_entry_t *gmep1 = arg1; 718 const guid_map_entry_t *gmep2 = arg2; 719 720 if (gmep1->guid < gmep2->guid) 721 return (-1); 722 else if (gmep1->guid > gmep2->guid) 723 return (1); 724 return (0); 725 } 726 727 /* 728 * This function is a callback used by dmu_objset_find() (which 729 * enumerates the object sets) to build an avl tree that maps guids 730 * to datasets. The resulting table is used when processing DRR_WRITE_BYREF 731 * send stream records. These records, which are used in dedup'ed 732 * streams, do not contain data themselves, but refer to a copy 733 * of the data block that has already been written because it was 734 * earlier in the stream. That previous copy is identified by the 735 * guid of the dataset with the referenced data. 736 */ 737 int 738 find_ds_by_guid(char *name, void *arg) 739 { 740 dsl_dataset_t *ds, *snapds; 741 avl_tree_t *guid_map = arg; 742 guid_map_entry_t *gmep; 743 dsl_pool_t *dp; 744 int err; 745 uint64_t lastobj, firstobj; 746 747 if (dsl_dataset_hold(name, FTAG, &ds) != 0) 748 return (0); 749 750 dp = ds->ds_dir->dd_pool; 751 rw_enter(&dp->dp_config_rwlock, RW_READER); 752 firstobj = ds->ds_dir->dd_phys->dd_origin_obj; 753 lastobj = ds->ds_phys->ds_prev_snap_obj; 754 755 while (lastobj != firstobj) { 756 err = dsl_dataset_hold_obj(dp, lastobj, guid_map, &snapds); 757 if (err) { 758 /* 759 * Skip this snapshot and move on. It's not 760 * clear why this would ever happen, but the 761 * remainder of the snapshot streadm can be 762 * processed. 763 */ 764 rw_exit(&dp->dp_config_rwlock); 765 dsl_dataset_rele(ds, FTAG); 766 return (0); 767 } 768 769 gmep = kmem_alloc(sizeof (guid_map_entry_t), KM_SLEEP); 770 gmep->guid = snapds->ds_phys->ds_guid; 771 gmep->gme_ds = snapds; 772 avl_add(guid_map, gmep); 773 lastobj = snapds->ds_phys->ds_prev_snap_obj; 774 } 775 776 rw_exit(&dp->dp_config_rwlock); 777 dsl_dataset_rele(ds, FTAG); 778 779 return (0); 780 } 781 782 static void * 783 restore_read(struct restorearg *ra, int len) 784 { 785 void *rv; 786 int done = 0; 787 788 /* some things will require 8-byte alignment, so everything must */ 789 ASSERT3U(len % 8, ==, 0); 790 791 while (done < len) { 792 ssize_t resid; 793 794 ra->err = vn_rdwr(UIO_READ, ra->vp, 795 (caddr_t)ra->buf + done, len - done, 796 ra->voff, UIO_SYSSPACE, FAPPEND, 797 RLIM64_INFINITY, CRED(), &resid); 798 799 if (resid == len - done) 800 ra->err = EINVAL; 801 ra->voff += len - done - resid; 802 done = len - resid; 803 if (ra->err) 804 return (NULL); 805 } 806 807 ASSERT3U(done, ==, len); 808 rv = ra->buf; 809 if (ra->byteswap) 810 fletcher_4_incremental_byteswap(rv, len, &ra->cksum); 811 else 812 fletcher_4_incremental_native(rv, len, &ra->cksum); 813 return (rv); 814 } 815 816 static void 817 backup_byteswap(dmu_replay_record_t *drr) 818 { 819 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X)) 820 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X)) 821 drr->drr_type = BSWAP_32(drr->drr_type); 822 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen); 823 switch (drr->drr_type) { 824 case DRR_BEGIN: 825 DO64(drr_begin.drr_magic); 826 DO64(drr_begin.drr_versioninfo); 827 DO64(drr_begin.drr_creation_time); 828 DO32(drr_begin.drr_type); 829 DO32(drr_begin.drr_flags); 830 DO64(drr_begin.drr_toguid); 831 DO64(drr_begin.drr_fromguid); 832 break; 833 case DRR_OBJECT: 834 DO64(drr_object.drr_object); 835 /* DO64(drr_object.drr_allocation_txg); */ 836 DO32(drr_object.drr_type); 837 DO32(drr_object.drr_bonustype); 838 DO32(drr_object.drr_blksz); 839 DO32(drr_object.drr_bonuslen); 840 DO64(drr_object.drr_toguid); 841 break; 842 case DRR_FREEOBJECTS: 843 DO64(drr_freeobjects.drr_firstobj); 844 DO64(drr_freeobjects.drr_numobjs); 845 DO64(drr_freeobjects.drr_toguid); 846 break; 847 case DRR_WRITE: 848 DO64(drr_write.drr_object); 849 DO32(drr_write.drr_type); 850 DO64(drr_write.drr_offset); 851 DO64(drr_write.drr_length); 852 DO64(drr_write.drr_toguid); 853 DO64(drr_write.drr_blkcksum.zc_word[0]); 854 DO64(drr_write.drr_blkcksum.zc_word[1]); 855 DO64(drr_write.drr_blkcksum.zc_word[2]); 856 DO64(drr_write.drr_blkcksum.zc_word[3]); 857 break; 858 case DRR_WRITE_BYREF: 859 DO64(drr_write_byref.drr_object); 860 DO64(drr_write_byref.drr_offset); 861 DO64(drr_write_byref.drr_length); 862 DO64(drr_write_byref.drr_toguid); 863 DO64(drr_write_byref.drr_refguid); 864 DO64(drr_write_byref.drr_refobject); 865 DO64(drr_write_byref.drr_refoffset); 866 DO64(drr_write_byref.drr_blkcksum.zc_word[0]); 867 DO64(drr_write_byref.drr_blkcksum.zc_word[1]); 868 DO64(drr_write_byref.drr_blkcksum.zc_word[2]); 869 DO64(drr_write_byref.drr_blkcksum.zc_word[3]); 870 break; 871 case DRR_FREE: 872 DO64(drr_free.drr_object); 873 DO64(drr_free.drr_offset); 874 DO64(drr_free.drr_length); 875 DO64(drr_free.drr_toguid); 876 break; 877 case DRR_END: 878 DO64(drr_end.drr_checksum.zc_word[0]); 879 DO64(drr_end.drr_checksum.zc_word[1]); 880 DO64(drr_end.drr_checksum.zc_word[2]); 881 DO64(drr_end.drr_checksum.zc_word[3]); 882 DO64(drr_end.drr_toguid); 883 break; 884 } 885 #undef DO64 886 #undef DO32 887 } 888 889 static int 890 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro) 891 { 892 int err; 893 dmu_tx_t *tx; 894 void *data = NULL; 895 896 if (drro->drr_type == DMU_OT_NONE || 897 drro->drr_type >= DMU_OT_NUMTYPES || 898 drro->drr_bonustype >= DMU_OT_NUMTYPES || 899 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS || 900 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS || 901 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) || 902 drro->drr_blksz < SPA_MINBLOCKSIZE || 903 drro->drr_blksz > SPA_MAXBLOCKSIZE || 904 drro->drr_bonuslen > DN_MAX_BONUSLEN) { 905 return (EINVAL); 906 } 907 908 err = dmu_object_info(os, drro->drr_object, NULL); 909 910 if (err != 0 && err != ENOENT) 911 return (EINVAL); 912 913 if (drro->drr_bonuslen) { 914 data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8)); 915 if (ra->err) 916 return (ra->err); 917 } 918 919 if (err == ENOENT) { 920 /* currently free, want to be allocated */ 921 tx = dmu_tx_create(os); 922 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 923 err = dmu_tx_assign(tx, TXG_WAIT); 924 if (err) { 925 dmu_tx_abort(tx); 926 return (err); 927 } 928 err = dmu_object_claim(os, drro->drr_object, 929 drro->drr_type, drro->drr_blksz, 930 drro->drr_bonustype, drro->drr_bonuslen, tx); 931 dmu_tx_commit(tx); 932 } else { 933 /* currently allocated, want to be allocated */ 934 err = dmu_object_reclaim(os, drro->drr_object, 935 drro->drr_type, drro->drr_blksz, 936 drro->drr_bonustype, drro->drr_bonuslen); 937 } 938 if (err) 939 return (EINVAL); 940 941 tx = dmu_tx_create(os); 942 dmu_tx_hold_bonus(tx, drro->drr_object); 943 err = dmu_tx_assign(tx, TXG_WAIT); 944 if (err) { 945 dmu_tx_abort(tx); 946 return (err); 947 } 948 949 dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype, 950 tx); 951 dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx); 952 953 if (data != NULL) { 954 dmu_buf_t *db; 955 956 VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db)); 957 dmu_buf_will_dirty(db, tx); 958 959 ASSERT3U(db->db_size, >=, drro->drr_bonuslen); 960 bcopy(data, db->db_data, drro->drr_bonuslen); 961 if (ra->byteswap) { 962 dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data, 963 drro->drr_bonuslen); 964 } 965 dmu_buf_rele(db, FTAG); 966 } 967 dmu_tx_commit(tx); 968 return (0); 969 } 970 971 /* ARGSUSED */ 972 static int 973 restore_freeobjects(struct restorearg *ra, objset_t *os, 974 struct drr_freeobjects *drrfo) 975 { 976 uint64_t obj; 977 978 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj) 979 return (EINVAL); 980 981 for (obj = drrfo->drr_firstobj; 982 obj < drrfo->drr_firstobj + drrfo->drr_numobjs; 983 (void) dmu_object_next(os, &obj, FALSE, 0)) { 984 int err; 985 986 if (dmu_object_info(os, obj, NULL) != 0) 987 continue; 988 989 err = dmu_free_object(os, obj); 990 if (err) 991 return (err); 992 } 993 return (0); 994 } 995 996 static int 997 restore_write(struct restorearg *ra, objset_t *os, 998 struct drr_write *drrw) 999 { 1000 dmu_tx_t *tx; 1001 void *data; 1002 int err; 1003 1004 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset || 1005 drrw->drr_type >= DMU_OT_NUMTYPES) 1006 return (EINVAL); 1007 1008 data = restore_read(ra, drrw->drr_length); 1009 if (data == NULL) 1010 return (ra->err); 1011 1012 if (dmu_object_info(os, drrw->drr_object, NULL) != 0) 1013 return (EINVAL); 1014 1015 tx = dmu_tx_create(os); 1016 1017 dmu_tx_hold_write(tx, drrw->drr_object, 1018 drrw->drr_offset, drrw->drr_length); 1019 err = dmu_tx_assign(tx, TXG_WAIT); 1020 if (err) { 1021 dmu_tx_abort(tx); 1022 return (err); 1023 } 1024 if (ra->byteswap) 1025 dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length); 1026 dmu_write(os, drrw->drr_object, 1027 drrw->drr_offset, drrw->drr_length, data, tx); 1028 dmu_tx_commit(tx); 1029 return (0); 1030 } 1031 1032 /* 1033 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed 1034 * streams to refer to a copy of the data that is already on the 1035 * system because it came in earlier in the stream. This function 1036 * finds the earlier copy of the data, and uses that copy instead of 1037 * data from the stream to fulfill this write. 1038 */ 1039 static int 1040 restore_write_byref(struct restorearg *ra, objset_t *os, 1041 struct drr_write_byref *drrwbr) 1042 { 1043 dmu_tx_t *tx; 1044 int err; 1045 guid_map_entry_t gmesrch; 1046 guid_map_entry_t *gmep; 1047 avl_index_t where; 1048 objset_t *ref_os = NULL; 1049 dmu_buf_t *dbp; 1050 1051 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset) 1052 return (EINVAL); 1053 1054 /* 1055 * If the GUID of the referenced dataset is different from the 1056 * GUID of the target dataset, find the referenced dataset. 1057 */ 1058 if (drrwbr->drr_toguid != drrwbr->drr_refguid) { 1059 gmesrch.guid = drrwbr->drr_refguid; 1060 if ((gmep = avl_find(&ra->guid_to_ds_map, &gmesrch, 1061 &where)) == NULL) { 1062 return (EINVAL); 1063 } 1064 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os)) 1065 return (EINVAL); 1066 } else { 1067 ref_os = os; 1068 } 1069 1070 if (err = dmu_buf_hold(ref_os, drrwbr->drr_refobject, 1071 drrwbr->drr_refoffset, FTAG, &dbp)) 1072 return (err); 1073 1074 tx = dmu_tx_create(os); 1075 1076 dmu_tx_hold_write(tx, drrwbr->drr_object, 1077 drrwbr->drr_offset, drrwbr->drr_length); 1078 err = dmu_tx_assign(tx, TXG_WAIT); 1079 if (err) { 1080 dmu_tx_abort(tx); 1081 return (err); 1082 } 1083 dmu_write(os, drrwbr->drr_object, 1084 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx); 1085 dmu_buf_rele(dbp, FTAG); 1086 dmu_tx_commit(tx); 1087 return (0); 1088 } 1089 1090 /* ARGSUSED */ 1091 static int 1092 restore_free(struct restorearg *ra, objset_t *os, 1093 struct drr_free *drrf) 1094 { 1095 int err; 1096 1097 if (drrf->drr_length != -1ULL && 1098 drrf->drr_offset + drrf->drr_length < drrf->drr_offset) 1099 return (EINVAL); 1100 1101 if (dmu_object_info(os, drrf->drr_object, NULL) != 0) 1102 return (EINVAL); 1103 1104 err = dmu_free_long_range(os, drrf->drr_object, 1105 drrf->drr_offset, drrf->drr_length); 1106 return (err); 1107 } 1108 1109 /* 1110 * NB: callers *must* call dmu_recv_end() if this succeeds. 1111 */ 1112 int 1113 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp) 1114 { 1115 struct restorearg ra = { 0 }; 1116 dmu_replay_record_t *drr; 1117 objset_t *os; 1118 zio_cksum_t pcksum; 1119 guid_map_entry_t *gmep; 1120 int featureflags; 1121 1122 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 1123 ra.byteswap = TRUE; 1124 1125 { 1126 /* compute checksum of drr_begin record */ 1127 dmu_replay_record_t *drr; 1128 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 1129 1130 drr->drr_type = DRR_BEGIN; 1131 drr->drr_u.drr_begin = *drc->drc_drrb; 1132 if (ra.byteswap) { 1133 fletcher_4_incremental_byteswap(drr, 1134 sizeof (dmu_replay_record_t), &ra.cksum); 1135 } else { 1136 fletcher_4_incremental_native(drr, 1137 sizeof (dmu_replay_record_t), &ra.cksum); 1138 } 1139 kmem_free(drr, sizeof (dmu_replay_record_t)); 1140 } 1141 1142 if (ra.byteswap) { 1143 struct drr_begin *drrb = drc->drc_drrb; 1144 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 1145 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); 1146 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 1147 drrb->drr_type = BSWAP_32(drrb->drr_type); 1148 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 1149 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 1150 } 1151 1152 ra.vp = vp; 1153 ra.voff = *voffp; 1154 ra.bufsize = 1<<20; 1155 ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP); 1156 1157 /* these were verified in dmu_recv_begin */ 1158 ASSERT(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo) == 1159 DMU_SUBSTREAM); 1160 ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES); 1161 1162 /* 1163 * Open the objset we are modifying. 1164 */ 1165 VERIFY(dmu_objset_from_ds(drc->drc_real_ds, &os) == 0); 1166 1167 ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT); 1168 1169 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo); 1170 1171 /* if this stream is dedup'ed, set up the avl tree for guid mapping */ 1172 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) { 1173 avl_create(&ra.guid_to_ds_map, guid_compare, 1174 sizeof (guid_map_entry_t), 1175 offsetof(guid_map_entry_t, avlnode)); 1176 (void) dmu_objset_find(drc->drc_top_ds, find_ds_by_guid, 1177 (void *)&ra.guid_to_ds_map, 1178 DS_FIND_CHILDREN); 1179 } 1180 1181 /* 1182 * Read records and process them. 1183 */ 1184 pcksum = ra.cksum; 1185 while (ra.err == 0 && 1186 NULL != (drr = restore_read(&ra, sizeof (*drr)))) { 1187 if (issig(JUSTLOOKING) && issig(FORREAL)) { 1188 ra.err = EINTR; 1189 goto out; 1190 } 1191 1192 if (ra.byteswap) 1193 backup_byteswap(drr); 1194 1195 switch (drr->drr_type) { 1196 case DRR_OBJECT: 1197 { 1198 /* 1199 * We need to make a copy of the record header, 1200 * because restore_{object,write} may need to 1201 * restore_read(), which will invalidate drr. 1202 */ 1203 struct drr_object drro = drr->drr_u.drr_object; 1204 ra.err = restore_object(&ra, os, &drro); 1205 break; 1206 } 1207 case DRR_FREEOBJECTS: 1208 { 1209 struct drr_freeobjects drrfo = 1210 drr->drr_u.drr_freeobjects; 1211 ra.err = restore_freeobjects(&ra, os, &drrfo); 1212 break; 1213 } 1214 case DRR_WRITE: 1215 { 1216 struct drr_write drrw = drr->drr_u.drr_write; 1217 ra.err = restore_write(&ra, os, &drrw); 1218 break; 1219 } 1220 case DRR_WRITE_BYREF: 1221 { 1222 struct drr_write_byref drrwbr = 1223 drr->drr_u.drr_write_byref; 1224 ra.err = restore_write_byref(&ra, os, &drrwbr); 1225 break; 1226 } 1227 case DRR_FREE: 1228 { 1229 struct drr_free drrf = drr->drr_u.drr_free; 1230 ra.err = restore_free(&ra, os, &drrf); 1231 break; 1232 } 1233 case DRR_END: 1234 { 1235 struct drr_end drre = drr->drr_u.drr_end; 1236 /* 1237 * We compare against the *previous* checksum 1238 * value, because the stored checksum is of 1239 * everything before the DRR_END record. 1240 */ 1241 if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum)) 1242 ra.err = ECKSUM; 1243 goto out; 1244 } 1245 default: 1246 ra.err = EINVAL; 1247 goto out; 1248 } 1249 pcksum = ra.cksum; 1250 } 1251 ASSERT(ra.err != 0); 1252 1253 out: 1254 if (ra.err != 0) { 1255 /* 1256 * destroy what we created, so we don't leave it in the 1257 * inconsistent restoring state. 1258 */ 1259 txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0); 1260 1261 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, 1262 B_FALSE); 1263 if (drc->drc_real_ds != drc->drc_logical_ds) { 1264 mutex_exit(&drc->drc_logical_ds->ds_recvlock); 1265 dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag); 1266 } 1267 } 1268 1269 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) { 1270 void *cookie = NULL; 1271 1272 while (gmep = avl_destroy_nodes(&ra.guid_to_ds_map, &cookie)) { 1273 dsl_dataset_rele(gmep->gme_ds, &ra.guid_to_ds_map); 1274 kmem_free(gmep, sizeof (guid_map_entry_t)); 1275 } 1276 avl_destroy(&ra.guid_to_ds_map); 1277 } 1278 1279 kmem_free(ra.buf, ra.bufsize); 1280 *voffp = ra.voff; 1281 return (ra.err); 1282 } 1283 1284 struct recvendsyncarg { 1285 char *tosnap; 1286 uint64_t creation_time; 1287 uint64_t toguid; 1288 }; 1289 1290 static int 1291 recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx) 1292 { 1293 dsl_dataset_t *ds = arg1; 1294 struct recvendsyncarg *resa = arg2; 1295 1296 return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx)); 1297 } 1298 1299 static void 1300 recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 1301 { 1302 dsl_dataset_t *ds = arg1; 1303 struct recvendsyncarg *resa = arg2; 1304 1305 dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx); 1306 1307 /* set snapshot's creation time and guid */ 1308 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1309 ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time; 1310 ds->ds_prev->ds_phys->ds_guid = resa->toguid; 1311 ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT; 1312 1313 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1314 ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT; 1315 } 1316 1317 static int 1318 dmu_recv_existing_end(dmu_recv_cookie_t *drc) 1319 { 1320 struct recvendsyncarg resa; 1321 dsl_dataset_t *ds = drc->drc_logical_ds; 1322 int err; 1323 1324 /* 1325 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean() 1326 * expects it to have a ds_user_ptr (and zil), but clone_swap() 1327 * can close it. 1328 */ 1329 txg_wait_synced(ds->ds_dir->dd_pool, 0); 1330 1331 if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) { 1332 err = dsl_dataset_clone_swap(drc->drc_real_ds, ds, 1333 drc->drc_force); 1334 if (err) 1335 goto out; 1336 } else { 1337 mutex_exit(&ds->ds_recvlock); 1338 dsl_dataset_rele(ds, dmu_recv_tag); 1339 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, 1340 B_FALSE); 1341 return (EBUSY); 1342 } 1343 1344 resa.creation_time = drc->drc_drrb->drr_creation_time; 1345 resa.toguid = drc->drc_drrb->drr_toguid; 1346 resa.tosnap = drc->drc_tosnap; 1347 1348 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1349 recv_end_check, recv_end_sync, ds, &resa, 3); 1350 if (err) { 1351 /* swap back */ 1352 (void) dsl_dataset_clone_swap(drc->drc_real_ds, ds, B_TRUE); 1353 } 1354 1355 out: 1356 mutex_exit(&ds->ds_recvlock); 1357 dsl_dataset_disown(ds, dmu_recv_tag); 1358 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, B_FALSE); 1359 return (err); 1360 } 1361 1362 static int 1363 dmu_recv_new_end(dmu_recv_cookie_t *drc) 1364 { 1365 struct recvendsyncarg resa; 1366 dsl_dataset_t *ds = drc->drc_logical_ds; 1367 int err; 1368 1369 /* 1370 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean() 1371 * expects it to have a ds_user_ptr (and zil), but clone_swap() 1372 * can close it. 1373 */ 1374 txg_wait_synced(ds->ds_dir->dd_pool, 0); 1375 1376 resa.creation_time = drc->drc_drrb->drr_creation_time; 1377 resa.toguid = drc->drc_drrb->drr_toguid; 1378 resa.tosnap = drc->drc_tosnap; 1379 1380 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1381 recv_end_check, recv_end_sync, ds, &resa, 3); 1382 if (err) { 1383 /* clean up the fs we just recv'd into */ 1384 (void) dsl_dataset_destroy(ds, dmu_recv_tag, B_FALSE); 1385 } else { 1386 /* release the hold from dmu_recv_begin */ 1387 dsl_dataset_disown(ds, dmu_recv_tag); 1388 } 1389 return (err); 1390 } 1391 1392 int 1393 dmu_recv_end(dmu_recv_cookie_t *drc) 1394 { 1395 if (drc->drc_logical_ds != drc->drc_real_ds) 1396 return (dmu_recv_existing_end(drc)); 1397 else 1398 return (dmu_recv_new_end(drc)); 1399 } 1400