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 42 static char *dmu_recv_tag = "dmu_recv_tag"; 43 44 struct backuparg { 45 dmu_replay_record_t *drr; 46 vnode_t *vp; 47 offset_t *off; 48 objset_t *os; 49 zio_cksum_t zc; 50 int err; 51 }; 52 53 static int 54 dump_bytes(struct backuparg *ba, void *buf, int len) 55 { 56 ssize_t resid; /* have to get resid to get detailed errno */ 57 ASSERT3U(len % 8, ==, 0); 58 59 fletcher_4_incremental_native(buf, len, &ba->zc); 60 ba->err = vn_rdwr(UIO_WRITE, ba->vp, 61 (caddr_t)buf, len, 62 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid); 63 *ba->off += len; 64 return (ba->err); 65 } 66 67 static int 68 dump_free(struct backuparg *ba, uint64_t object, uint64_t offset, 69 uint64_t length) 70 { 71 /* write a FREE record */ 72 bzero(ba->drr, sizeof (dmu_replay_record_t)); 73 ba->drr->drr_type = DRR_FREE; 74 ba->drr->drr_u.drr_free.drr_object = object; 75 ba->drr->drr_u.drr_free.drr_offset = offset; 76 ba->drr->drr_u.drr_free.drr_length = length; 77 78 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t))) 79 return (EINTR); 80 return (0); 81 } 82 83 static int 84 dump_data(struct backuparg *ba, dmu_object_type_t type, 85 uint64_t object, uint64_t offset, int blksz, void *data) 86 { 87 /* write a DATA record */ 88 bzero(ba->drr, sizeof (dmu_replay_record_t)); 89 ba->drr->drr_type = DRR_WRITE; 90 ba->drr->drr_u.drr_write.drr_object = object; 91 ba->drr->drr_u.drr_write.drr_type = type; 92 ba->drr->drr_u.drr_write.drr_offset = offset; 93 ba->drr->drr_u.drr_write.drr_length = blksz; 94 95 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t))) 96 return (EINTR); 97 if (dump_bytes(ba, data, blksz)) 98 return (EINTR); 99 return (0); 100 } 101 102 static int 103 dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs) 104 { 105 /* write a FREEOBJECTS record */ 106 bzero(ba->drr, sizeof (dmu_replay_record_t)); 107 ba->drr->drr_type = DRR_FREEOBJECTS; 108 ba->drr->drr_u.drr_freeobjects.drr_firstobj = firstobj; 109 ba->drr->drr_u.drr_freeobjects.drr_numobjs = numobjs; 110 111 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t))) 112 return (EINTR); 113 return (0); 114 } 115 116 static int 117 dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp) 118 { 119 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE) 120 return (dump_freeobjects(ba, object, 1)); 121 122 /* write an OBJECT record */ 123 bzero(ba->drr, sizeof (dmu_replay_record_t)); 124 ba->drr->drr_type = DRR_OBJECT; 125 ba->drr->drr_u.drr_object.drr_object = object; 126 ba->drr->drr_u.drr_object.drr_type = dnp->dn_type; 127 ba->drr->drr_u.drr_object.drr_bonustype = dnp->dn_bonustype; 128 ba->drr->drr_u.drr_object.drr_blksz = 129 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; 130 ba->drr->drr_u.drr_object.drr_bonuslen = dnp->dn_bonuslen; 131 ba->drr->drr_u.drr_object.drr_checksum = dnp->dn_checksum; 132 ba->drr->drr_u.drr_object.drr_compress = dnp->dn_compress; 133 134 if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t))) 135 return (EINTR); 136 137 if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8))) 138 return (EINTR); 139 140 /* free anything past the end of the file */ 141 if (dump_free(ba, object, (dnp->dn_maxblkid + 1) * 142 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL)) 143 return (EINTR); 144 if (ba->err) 145 return (EINTR); 146 return (0); 147 } 148 149 #define BP_SPAN(dnp, level) \ 150 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \ 151 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) 152 153 static int 154 backup_cb(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb, 155 const dnode_phys_t *dnp, void *arg) 156 { 157 struct backuparg *ba = arg; 158 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE; 159 int err = 0; 160 161 if (issig(JUSTLOOKING) && issig(FORREAL)) 162 return (EINTR); 163 164 if (zb->zb_object != 0 && DMU_OBJECT_IS_SPECIAL(zb->zb_object)) { 165 return (0); 166 } else if (bp == NULL && zb->zb_object == 0) { 167 uint64_t span = BP_SPAN(dnp, zb->zb_level); 168 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT; 169 err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT); 170 } else if (bp == NULL) { 171 uint64_t span = BP_SPAN(dnp, zb->zb_level); 172 err = dump_free(ba, zb->zb_object, zb->zb_blkid * span, span); 173 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) { 174 return (0); 175 } else if (type == DMU_OT_DNODE) { 176 dnode_phys_t *blk; 177 int i; 178 int blksz = BP_GET_LSIZE(bp); 179 uint32_t aflags = ARC_WAIT; 180 arc_buf_t *abuf; 181 182 if (arc_read_nolock(NULL, spa, bp, 183 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 184 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) 185 return (EIO); 186 187 blk = abuf->b_data; 188 for (i = 0; i < blksz >> DNODE_SHIFT; i++) { 189 uint64_t dnobj = (zb->zb_blkid << 190 (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i; 191 err = dump_dnode(ba, dnobj, blk+i); 192 if (err) 193 break; 194 } 195 (void) arc_buf_remove_ref(abuf, &abuf); 196 } else { /* it's a level-0 block of a regular object */ 197 uint32_t aflags = ARC_WAIT; 198 arc_buf_t *abuf; 199 int blksz = BP_GET_LSIZE(bp); 200 201 if (arc_read_nolock(NULL, spa, bp, 202 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 203 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) 204 return (EIO); 205 206 err = dump_data(ba, type, zb->zb_object, zb->zb_blkid * blksz, 207 blksz, abuf->b_data); 208 (void) arc_buf_remove_ref(abuf, &abuf); 209 } 210 211 ASSERT(err == 0 || err == EINTR); 212 return (err); 213 } 214 215 int 216 dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin, 217 vnode_t *vp, offset_t *off) 218 { 219 dsl_dataset_t *ds = tosnap->os_dsl_dataset; 220 dsl_dataset_t *fromds = fromsnap ? fromsnap->os_dsl_dataset : NULL; 221 dmu_replay_record_t *drr; 222 struct backuparg ba; 223 int err; 224 uint64_t fromtxg = 0; 225 226 /* tosnap must be a snapshot */ 227 if (ds->ds_phys->ds_next_snap_obj == 0) 228 return (EINVAL); 229 230 /* fromsnap must be an earlier snapshot from the same fs as tosnap */ 231 if (fromds && (ds->ds_dir != fromds->ds_dir || 232 fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg)) 233 return (EXDEV); 234 235 if (fromorigin) { 236 dsl_pool_t *dp = ds->ds_dir->dd_pool; 237 238 if (fromsnap) 239 return (EINVAL); 240 241 if (dsl_dir_is_clone(ds->ds_dir)) { 242 rw_enter(&dp->dp_config_rwlock, RW_READER); 243 err = dsl_dataset_hold_obj(dp, 244 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds); 245 rw_exit(&dp->dp_config_rwlock); 246 if (err) 247 return (err); 248 } else { 249 fromorigin = B_FALSE; 250 } 251 } 252 253 254 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 255 drr->drr_type = DRR_BEGIN; 256 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 257 drr->drr_u.drr_begin.drr_version = DMU_BACKUP_STREAM_VERSION; 258 drr->drr_u.drr_begin.drr_creation_time = 259 ds->ds_phys->ds_creation_time; 260 drr->drr_u.drr_begin.drr_type = tosnap->os_phys->os_type; 261 if (fromorigin) 262 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE; 263 drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid; 264 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 265 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA; 266 267 if (fromds) 268 drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid; 269 dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname); 270 271 if (fromds) 272 fromtxg = fromds->ds_phys->ds_creation_txg; 273 if (fromorigin) 274 dsl_dataset_rele(fromds, FTAG); 275 276 ba.drr = drr; 277 ba.vp = vp; 278 ba.os = tosnap; 279 ba.off = off; 280 ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0); 281 282 if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) { 283 kmem_free(drr, sizeof (dmu_replay_record_t)); 284 return (ba.err); 285 } 286 287 err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH, 288 backup_cb, &ba); 289 290 if (err) { 291 if (err == EINTR && ba.err) 292 err = ba.err; 293 kmem_free(drr, sizeof (dmu_replay_record_t)); 294 return (err); 295 } 296 297 bzero(drr, sizeof (dmu_replay_record_t)); 298 drr->drr_type = DRR_END; 299 drr->drr_u.drr_end.drr_checksum = ba.zc; 300 301 if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) { 302 kmem_free(drr, sizeof (dmu_replay_record_t)); 303 return (ba.err); 304 } 305 306 kmem_free(drr, sizeof (dmu_replay_record_t)); 307 308 return (0); 309 } 310 311 struct recvbeginsyncarg { 312 const char *tofs; 313 const char *tosnap; 314 dsl_dataset_t *origin; 315 uint64_t fromguid; 316 dmu_objset_type_t type; 317 void *tag; 318 boolean_t force; 319 uint64_t dsflags; 320 char clonelastname[MAXNAMELEN]; 321 dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */ 322 }; 323 324 /* ARGSUSED */ 325 static int 326 recv_new_check(void *arg1, void *arg2, dmu_tx_t *tx) 327 { 328 dsl_dir_t *dd = arg1; 329 struct recvbeginsyncarg *rbsa = arg2; 330 objset_t *mos = dd->dd_pool->dp_meta_objset; 331 uint64_t val; 332 int err; 333 334 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 335 strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val); 336 337 if (err != ENOENT) 338 return (err ? err : EEXIST); 339 340 if (rbsa->origin) { 341 /* make sure it's a snap in the same pool */ 342 if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool) 343 return (EXDEV); 344 if (!dsl_dataset_is_snapshot(rbsa->origin)) 345 return (EINVAL); 346 if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid) 347 return (ENODEV); 348 } 349 350 return (0); 351 } 352 353 static void 354 recv_new_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 355 { 356 dsl_dir_t *dd = arg1; 357 struct recvbeginsyncarg *rbsa = arg2; 358 uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags; 359 uint64_t dsobj; 360 361 /* Create and open new dataset. */ 362 dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1, 363 rbsa->origin, flags, cr, tx); 364 VERIFY(0 == dsl_dataset_own_obj(dd->dd_pool, dsobj, 365 B_TRUE, dmu_recv_tag, &rbsa->ds)); 366 367 if (rbsa->origin == NULL) { 368 (void) dmu_objset_create_impl(dd->dd_pool->dp_spa, 369 rbsa->ds, &rbsa->ds->ds_phys->ds_bp, rbsa->type, tx); 370 } 371 372 spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC, 373 dd->dd_pool->dp_spa, tx, cr, "dataset = %lld", dsobj); 374 } 375 376 /* ARGSUSED */ 377 static int 378 recv_existing_check(void *arg1, void *arg2, dmu_tx_t *tx) 379 { 380 dsl_dataset_t *ds = arg1; 381 struct recvbeginsyncarg *rbsa = arg2; 382 int err; 383 uint64_t val; 384 385 /* must not have any changes since most recent snapshot */ 386 if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds)) 387 return (ETXTBSY); 388 389 if (rbsa->fromguid) { 390 /* if incremental, most recent snapshot must match fromguid */ 391 if (ds->ds_prev == NULL) 392 return (ENODEV); 393 if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid) 394 return (ENODEV); 395 } else { 396 /* if full, most recent snapshot must be $ORIGIN */ 397 if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL) 398 return (ENODEV); 399 } 400 401 /* temporary clone name must not exist */ 402 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 403 ds->ds_dir->dd_phys->dd_child_dir_zapobj, 404 rbsa->clonelastname, 8, 1, &val); 405 if (err == 0) 406 return (EEXIST); 407 if (err != ENOENT) 408 return (err); 409 410 /* new snapshot name must not exist */ 411 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 412 ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val); 413 if (err == 0) 414 return (EEXIST); 415 if (err != ENOENT) 416 return (err); 417 return (0); 418 } 419 420 /* ARGSUSED */ 421 static void 422 recv_existing_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 423 { 424 dsl_dataset_t *ohds = arg1; 425 struct recvbeginsyncarg *rbsa = arg2; 426 dsl_pool_t *dp = ohds->ds_dir->dd_pool; 427 dsl_dataset_t *cds; 428 uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags; 429 uint64_t dsobj; 430 431 /* create and open the temporary clone */ 432 dsobj = dsl_dataset_create_sync(ohds->ds_dir, rbsa->clonelastname, 433 ohds->ds_prev, flags, cr, tx); 434 VERIFY(0 == dsl_dataset_own_obj(dp, dsobj, B_TRUE, dmu_recv_tag, &cds)); 435 436 /* 437 * If we actually created a non-clone, we need to create the 438 * objset in our new dataset. 439 */ 440 if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds))) { 441 (void) dmu_objset_create_impl(dp->dp_spa, 442 cds, dsl_dataset_get_blkptr(cds), rbsa->type, tx); 443 } 444 445 /* copy the refquota from the target fs to the clone */ 446 if (ohds->ds_quota > 0) 447 dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx); 448 449 rbsa->ds = cds; 450 451 spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC, 452 dp->dp_spa, tx, cr, "dataset = %lld", dsobj); 453 } 454 455 /* 456 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin() 457 * succeeds; otherwise we will leak the holds on the datasets. 458 */ 459 int 460 dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb, 461 boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc) 462 { 463 int err = 0; 464 boolean_t byteswap; 465 struct recvbeginsyncarg rbsa = { 0 }; 466 uint64_t version; 467 int flags; 468 dsl_dataset_t *ds; 469 470 if (drrb->drr_magic == DMU_BACKUP_MAGIC) 471 byteswap = FALSE; 472 else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 473 byteswap = TRUE; 474 else 475 return (EINVAL); 476 477 rbsa.tofs = tofs; 478 rbsa.tosnap = tosnap; 479 rbsa.origin = origin ? origin->os_dsl_dataset : NULL; 480 rbsa.fromguid = drrb->drr_fromguid; 481 rbsa.type = drrb->drr_type; 482 rbsa.tag = FTAG; 483 rbsa.dsflags = 0; 484 version = drrb->drr_version; 485 flags = drrb->drr_flags; 486 487 if (byteswap) { 488 rbsa.type = BSWAP_32(rbsa.type); 489 rbsa.fromguid = BSWAP_64(rbsa.fromguid); 490 version = BSWAP_64(version); 491 flags = BSWAP_32(flags); 492 } 493 494 if (version != DMU_BACKUP_STREAM_VERSION || 495 rbsa.type >= DMU_OST_NUMTYPES || 496 ((flags & DRR_FLAG_CLONE) && origin == NULL)) 497 return (EINVAL); 498 499 if (flags & DRR_FLAG_CI_DATA) 500 rbsa.dsflags = DS_FLAG_CI_DATASET; 501 502 bzero(drc, sizeof (dmu_recv_cookie_t)); 503 drc->drc_drrb = drrb; 504 drc->drc_tosnap = tosnap; 505 drc->drc_force = force; 506 507 /* 508 * Process the begin in syncing context. 509 */ 510 511 /* open the dataset we are logically receiving into */ 512 err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds); 513 if (err == 0) { 514 /* target fs already exists; recv into temp clone */ 515 516 /* Can't recv a clone into an existing fs */ 517 if (flags & DRR_FLAG_CLONE) { 518 dsl_dataset_rele(ds, dmu_recv_tag); 519 return (EINVAL); 520 } 521 522 /* must not have an incremental recv already in progress */ 523 if (!mutex_tryenter(&ds->ds_recvlock)) { 524 dsl_dataset_rele(ds, dmu_recv_tag); 525 return (EBUSY); 526 } 527 528 /* tmp clone name is: tofs/%tosnap" */ 529 (void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname), 530 "%%%s", tosnap); 531 rbsa.force = force; 532 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 533 recv_existing_check, recv_existing_sync, ds, &rbsa, 5); 534 if (err) { 535 mutex_exit(&ds->ds_recvlock); 536 dsl_dataset_rele(ds, dmu_recv_tag); 537 return (err); 538 } 539 drc->drc_logical_ds = ds; 540 drc->drc_real_ds = rbsa.ds; 541 } else if (err == ENOENT) { 542 /* target fs does not exist; must be a full backup or clone */ 543 char *cp; 544 545 /* 546 * If it's a non-clone incremental, we are missing the 547 * target fs, so fail the recv. 548 */ 549 if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE)) 550 return (ENOENT); 551 552 /* Open the parent of tofs */ 553 cp = strrchr(tofs, '/'); 554 *cp = '\0'; 555 err = dsl_dataset_hold(tofs, FTAG, &ds); 556 *cp = '/'; 557 if (err) 558 return (err); 559 560 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 561 recv_new_check, recv_new_sync, ds->ds_dir, &rbsa, 5); 562 dsl_dataset_rele(ds, FTAG); 563 if (err) 564 return (err); 565 drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds; 566 drc->drc_newfs = B_TRUE; 567 } 568 569 return (err); 570 } 571 572 struct restorearg { 573 int err; 574 int byteswap; 575 vnode_t *vp; 576 char *buf; 577 uint64_t voff; 578 int bufsize; /* amount of memory allocated for buf */ 579 zio_cksum_t cksum; 580 }; 581 582 static void * 583 restore_read(struct restorearg *ra, int len) 584 { 585 void *rv; 586 int done = 0; 587 588 /* some things will require 8-byte alignment, so everything must */ 589 ASSERT3U(len % 8, ==, 0); 590 591 while (done < len) { 592 ssize_t resid; 593 594 ra->err = vn_rdwr(UIO_READ, ra->vp, 595 (caddr_t)ra->buf + done, len - done, 596 ra->voff, UIO_SYSSPACE, FAPPEND, 597 RLIM64_INFINITY, CRED(), &resid); 598 599 if (resid == len - done) 600 ra->err = EINVAL; 601 ra->voff += len - done - resid; 602 done = len - resid; 603 if (ra->err) 604 return (NULL); 605 } 606 607 ASSERT3U(done, ==, len); 608 rv = ra->buf; 609 if (ra->byteswap) 610 fletcher_4_incremental_byteswap(rv, len, &ra->cksum); 611 else 612 fletcher_4_incremental_native(rv, len, &ra->cksum); 613 return (rv); 614 } 615 616 static void 617 backup_byteswap(dmu_replay_record_t *drr) 618 { 619 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X)) 620 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X)) 621 drr->drr_type = BSWAP_32(drr->drr_type); 622 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen); 623 switch (drr->drr_type) { 624 case DRR_BEGIN: 625 DO64(drr_begin.drr_magic); 626 DO64(drr_begin.drr_version); 627 DO64(drr_begin.drr_creation_time); 628 DO32(drr_begin.drr_type); 629 DO32(drr_begin.drr_flags); 630 DO64(drr_begin.drr_toguid); 631 DO64(drr_begin.drr_fromguid); 632 break; 633 case DRR_OBJECT: 634 DO64(drr_object.drr_object); 635 /* DO64(drr_object.drr_allocation_txg); */ 636 DO32(drr_object.drr_type); 637 DO32(drr_object.drr_bonustype); 638 DO32(drr_object.drr_blksz); 639 DO32(drr_object.drr_bonuslen); 640 break; 641 case DRR_FREEOBJECTS: 642 DO64(drr_freeobjects.drr_firstobj); 643 DO64(drr_freeobjects.drr_numobjs); 644 break; 645 case DRR_WRITE: 646 DO64(drr_write.drr_object); 647 DO32(drr_write.drr_type); 648 DO64(drr_write.drr_offset); 649 DO64(drr_write.drr_length); 650 break; 651 case DRR_FREE: 652 DO64(drr_free.drr_object); 653 DO64(drr_free.drr_offset); 654 DO64(drr_free.drr_length); 655 break; 656 case DRR_END: 657 DO64(drr_end.drr_checksum.zc_word[0]); 658 DO64(drr_end.drr_checksum.zc_word[1]); 659 DO64(drr_end.drr_checksum.zc_word[2]); 660 DO64(drr_end.drr_checksum.zc_word[3]); 661 break; 662 } 663 #undef DO64 664 #undef DO32 665 } 666 667 static int 668 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro) 669 { 670 int err; 671 dmu_tx_t *tx; 672 void *data = NULL; 673 674 if (drro->drr_type == DMU_OT_NONE || 675 drro->drr_type >= DMU_OT_NUMTYPES || 676 drro->drr_bonustype >= DMU_OT_NUMTYPES || 677 drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS || 678 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS || 679 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) || 680 drro->drr_blksz < SPA_MINBLOCKSIZE || 681 drro->drr_blksz > SPA_MAXBLOCKSIZE || 682 drro->drr_bonuslen > DN_MAX_BONUSLEN) { 683 return (EINVAL); 684 } 685 686 err = dmu_object_info(os, drro->drr_object, NULL); 687 688 if (err != 0 && err != ENOENT) 689 return (EINVAL); 690 691 if (drro->drr_bonuslen) { 692 data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8)); 693 if (ra->err) 694 return (ra->err); 695 } 696 697 if (err == ENOENT) { 698 /* currently free, want to be allocated */ 699 tx = dmu_tx_create(os); 700 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 701 err = dmu_tx_assign(tx, TXG_WAIT); 702 if (err) { 703 dmu_tx_abort(tx); 704 return (err); 705 } 706 err = dmu_object_claim(os, drro->drr_object, 707 drro->drr_type, drro->drr_blksz, 708 drro->drr_bonustype, drro->drr_bonuslen, tx); 709 dmu_tx_commit(tx); 710 } else { 711 /* currently allocated, want to be allocated */ 712 err = dmu_object_reclaim(os, drro->drr_object, 713 drro->drr_type, drro->drr_blksz, 714 drro->drr_bonustype, drro->drr_bonuslen); 715 } 716 if (err) 717 return (EINVAL); 718 719 tx = dmu_tx_create(os); 720 dmu_tx_hold_bonus(tx, drro->drr_object); 721 err = dmu_tx_assign(tx, TXG_WAIT); 722 if (err) { 723 dmu_tx_abort(tx); 724 return (err); 725 } 726 727 dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx); 728 dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx); 729 730 if (data != NULL) { 731 dmu_buf_t *db; 732 733 VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db)); 734 dmu_buf_will_dirty(db, tx); 735 736 ASSERT3U(db->db_size, >=, drro->drr_bonuslen); 737 bcopy(data, db->db_data, drro->drr_bonuslen); 738 if (ra->byteswap) { 739 dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data, 740 drro->drr_bonuslen); 741 } 742 dmu_buf_rele(db, FTAG); 743 } 744 dmu_tx_commit(tx); 745 return (0); 746 } 747 748 /* ARGSUSED */ 749 static int 750 restore_freeobjects(struct restorearg *ra, objset_t *os, 751 struct drr_freeobjects *drrfo) 752 { 753 uint64_t obj; 754 755 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj) 756 return (EINVAL); 757 758 for (obj = drrfo->drr_firstobj; 759 obj < drrfo->drr_firstobj + drrfo->drr_numobjs; 760 (void) dmu_object_next(os, &obj, FALSE, 0)) { 761 int err; 762 763 if (dmu_object_info(os, obj, NULL) != 0) 764 continue; 765 766 err = dmu_free_object(os, obj); 767 if (err) 768 return (err); 769 } 770 return (0); 771 } 772 773 static int 774 restore_write(struct restorearg *ra, objset_t *os, 775 struct drr_write *drrw) 776 { 777 dmu_tx_t *tx; 778 void *data; 779 int err; 780 781 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset || 782 drrw->drr_type >= DMU_OT_NUMTYPES) 783 return (EINVAL); 784 785 data = restore_read(ra, drrw->drr_length); 786 if (data == NULL) 787 return (ra->err); 788 789 if (dmu_object_info(os, drrw->drr_object, NULL) != 0) 790 return (EINVAL); 791 792 tx = dmu_tx_create(os); 793 794 dmu_tx_hold_write(tx, drrw->drr_object, 795 drrw->drr_offset, drrw->drr_length); 796 err = dmu_tx_assign(tx, TXG_WAIT); 797 if (err) { 798 dmu_tx_abort(tx); 799 return (err); 800 } 801 if (ra->byteswap) 802 dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length); 803 dmu_write(os, drrw->drr_object, 804 drrw->drr_offset, drrw->drr_length, data, tx); 805 dmu_tx_commit(tx); 806 return (0); 807 } 808 809 /* ARGSUSED */ 810 static int 811 restore_free(struct restorearg *ra, objset_t *os, 812 struct drr_free *drrf) 813 { 814 int err; 815 816 if (drrf->drr_length != -1ULL && 817 drrf->drr_offset + drrf->drr_length < drrf->drr_offset) 818 return (EINVAL); 819 820 if (dmu_object_info(os, drrf->drr_object, NULL) != 0) 821 return (EINVAL); 822 823 err = dmu_free_long_range(os, drrf->drr_object, 824 drrf->drr_offset, drrf->drr_length); 825 return (err); 826 } 827 828 /* 829 * NB: callers *must* call dmu_recv_end() if this succeeds. 830 */ 831 int 832 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp) 833 { 834 struct restorearg ra = { 0 }; 835 dmu_replay_record_t *drr; 836 objset_t *os; 837 zio_cksum_t pcksum; 838 839 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 840 ra.byteswap = TRUE; 841 842 { 843 /* compute checksum of drr_begin record */ 844 dmu_replay_record_t *drr; 845 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 846 847 drr->drr_type = DRR_BEGIN; 848 drr->drr_u.drr_begin = *drc->drc_drrb; 849 if (ra.byteswap) { 850 fletcher_4_incremental_byteswap(drr, 851 sizeof (dmu_replay_record_t), &ra.cksum); 852 } else { 853 fletcher_4_incremental_native(drr, 854 sizeof (dmu_replay_record_t), &ra.cksum); 855 } 856 kmem_free(drr, sizeof (dmu_replay_record_t)); 857 } 858 859 if (ra.byteswap) { 860 struct drr_begin *drrb = drc->drc_drrb; 861 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 862 drrb->drr_version = BSWAP_64(drrb->drr_version); 863 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 864 drrb->drr_type = BSWAP_32(drrb->drr_type); 865 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 866 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 867 } 868 869 ra.vp = vp; 870 ra.voff = *voffp; 871 ra.bufsize = 1<<20; 872 ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP); 873 874 /* these were verified in dmu_recv_begin */ 875 ASSERT(drc->drc_drrb->drr_version == DMU_BACKUP_STREAM_VERSION); 876 ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES); 877 878 /* 879 * Open the objset we are modifying. 880 */ 881 VERIFY(dmu_objset_from_ds(drc->drc_real_ds, &os) == 0); 882 883 ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT); 884 885 /* 886 * Read records and process them. 887 */ 888 pcksum = ra.cksum; 889 while (ra.err == 0 && 890 NULL != (drr = restore_read(&ra, sizeof (*drr)))) { 891 if (issig(JUSTLOOKING) && issig(FORREAL)) { 892 ra.err = EINTR; 893 goto out; 894 } 895 896 if (ra.byteswap) 897 backup_byteswap(drr); 898 899 switch (drr->drr_type) { 900 case DRR_OBJECT: 901 { 902 /* 903 * We need to make a copy of the record header, 904 * because restore_{object,write} may need to 905 * restore_read(), which will invalidate drr. 906 */ 907 struct drr_object drro = drr->drr_u.drr_object; 908 ra.err = restore_object(&ra, os, &drro); 909 break; 910 } 911 case DRR_FREEOBJECTS: 912 { 913 struct drr_freeobjects drrfo = 914 drr->drr_u.drr_freeobjects; 915 ra.err = restore_freeobjects(&ra, os, &drrfo); 916 break; 917 } 918 case DRR_WRITE: 919 { 920 struct drr_write drrw = drr->drr_u.drr_write; 921 ra.err = restore_write(&ra, os, &drrw); 922 break; 923 } 924 case DRR_FREE: 925 { 926 struct drr_free drrf = drr->drr_u.drr_free; 927 ra.err = restore_free(&ra, os, &drrf); 928 break; 929 } 930 case DRR_END: 931 { 932 struct drr_end drre = drr->drr_u.drr_end; 933 /* 934 * We compare against the *previous* checksum 935 * value, because the stored checksum is of 936 * everything before the DRR_END record. 937 */ 938 if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum)) 939 ra.err = ECKSUM; 940 goto out; 941 } 942 default: 943 ra.err = EINVAL; 944 goto out; 945 } 946 pcksum = ra.cksum; 947 } 948 ASSERT(ra.err != 0); 949 950 out: 951 if (ra.err != 0) { 952 /* 953 * destroy what we created, so we don't leave it in the 954 * inconsistent restoring state. 955 */ 956 txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0); 957 958 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, 959 B_FALSE); 960 if (drc->drc_real_ds != drc->drc_logical_ds) { 961 mutex_exit(&drc->drc_logical_ds->ds_recvlock); 962 dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag); 963 } 964 } 965 966 kmem_free(ra.buf, ra.bufsize); 967 *voffp = ra.voff; 968 return (ra.err); 969 } 970 971 struct recvendsyncarg { 972 char *tosnap; 973 uint64_t creation_time; 974 uint64_t toguid; 975 }; 976 977 static int 978 recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx) 979 { 980 dsl_dataset_t *ds = arg1; 981 struct recvendsyncarg *resa = arg2; 982 983 return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx)); 984 } 985 986 static void 987 recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx) 988 { 989 dsl_dataset_t *ds = arg1; 990 struct recvendsyncarg *resa = arg2; 991 992 dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx); 993 994 /* set snapshot's creation time and guid */ 995 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 996 ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time; 997 ds->ds_prev->ds_phys->ds_guid = resa->toguid; 998 ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT; 999 1000 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1001 ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT; 1002 } 1003 1004 static int 1005 dmu_recv_existing_end(dmu_recv_cookie_t *drc) 1006 { 1007 struct recvendsyncarg resa; 1008 dsl_dataset_t *ds = drc->drc_logical_ds; 1009 int err; 1010 1011 /* 1012 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean() 1013 * expects it to have a ds_user_ptr (and zil), but clone_swap() 1014 * can close it. 1015 */ 1016 txg_wait_synced(ds->ds_dir->dd_pool, 0); 1017 1018 if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) { 1019 err = dsl_dataset_clone_swap(drc->drc_real_ds, ds, 1020 drc->drc_force); 1021 if (err) 1022 goto out; 1023 } else { 1024 mutex_exit(&ds->ds_recvlock); 1025 dsl_dataset_rele(ds, dmu_recv_tag); 1026 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, 1027 B_FALSE); 1028 return (EBUSY); 1029 } 1030 1031 resa.creation_time = drc->drc_drrb->drr_creation_time; 1032 resa.toguid = drc->drc_drrb->drr_toguid; 1033 resa.tosnap = drc->drc_tosnap; 1034 1035 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1036 recv_end_check, recv_end_sync, ds, &resa, 3); 1037 if (err) { 1038 /* swap back */ 1039 (void) dsl_dataset_clone_swap(drc->drc_real_ds, ds, B_TRUE); 1040 } 1041 1042 out: 1043 mutex_exit(&ds->ds_recvlock); 1044 dsl_dataset_disown(ds, dmu_recv_tag); 1045 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, B_FALSE); 1046 return (err); 1047 } 1048 1049 static int 1050 dmu_recv_new_end(dmu_recv_cookie_t *drc) 1051 { 1052 struct recvendsyncarg resa; 1053 dsl_dataset_t *ds = drc->drc_logical_ds; 1054 int err; 1055 1056 /* 1057 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean() 1058 * expects it to have a ds_user_ptr (and zil), but clone_swap() 1059 * can close it. 1060 */ 1061 txg_wait_synced(ds->ds_dir->dd_pool, 0); 1062 1063 resa.creation_time = drc->drc_drrb->drr_creation_time; 1064 resa.toguid = drc->drc_drrb->drr_toguid; 1065 resa.tosnap = drc->drc_tosnap; 1066 1067 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1068 recv_end_check, recv_end_sync, ds, &resa, 3); 1069 if (err) { 1070 /* clean up the fs we just recv'd into */ 1071 (void) dsl_dataset_destroy(ds, dmu_recv_tag, B_FALSE); 1072 } else { 1073 /* release the hold from dmu_recv_begin */ 1074 dsl_dataset_disown(ds, dmu_recv_tag); 1075 } 1076 return (err); 1077 } 1078 1079 int 1080 dmu_recv_end(dmu_recv_cookie_t *drc) 1081 { 1082 if (drc->drc_logical_ds != drc->drc_real_ds) 1083 return (dmu_recv_existing_end(drc)); 1084 else 1085 return (dmu_recv_new_end(drc)); 1086 } 1087