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) 2012 by Delphix. All rights reserved. 25 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 26 */ 27 28 #include <sys/dmu.h> 29 #include <sys/dmu_impl.h> 30 #include <sys/dmu_tx.h> 31 #include <sys/dbuf.h> 32 #include <sys/dnode.h> 33 #include <sys/zfs_context.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/dmu_traverse.h> 36 #include <sys/dsl_dataset.h> 37 #include <sys/dsl_dir.h> 38 #include <sys/dsl_prop.h> 39 #include <sys/dsl_pool.h> 40 #include <sys/dsl_synctask.h> 41 #include <sys/zfs_ioctl.h> 42 #include <sys/zap.h> 43 #include <sys/zio_checksum.h> 44 #include <sys/zfs_znode.h> 45 #include <zfs_fletcher.h> 46 #include <sys/avl.h> 47 #include <sys/ddt.h> 48 #include <sys/zfs_onexit.h> 49 50 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */ 51 int zfs_send_corrupt_data = B_FALSE; 52 53 static char *dmu_recv_tag = "dmu_recv_tag"; 54 55 static int 56 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len) 57 { 58 dsl_dataset_t *ds = dsp->dsa_os->os_dsl_dataset; 59 ssize_t resid; /* have to get resid to get detailed errno */ 60 ASSERT3U(len % 8, ==, 0); 61 62 fletcher_4_incremental_native(buf, len, &dsp->dsa_zc); 63 dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp, 64 (caddr_t)buf, len, 65 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid); 66 67 mutex_enter(&ds->ds_sendstream_lock); 68 *dsp->dsa_off += len; 69 mutex_exit(&ds->ds_sendstream_lock); 70 71 return (dsp->dsa_err); 72 } 73 74 static int 75 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset, 76 uint64_t length) 77 { 78 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free); 79 80 if (length != -1ULL && offset + length < offset) 81 length = -1ULL; 82 83 /* 84 * If there is a pending op, but it's not PENDING_FREE, push it out, 85 * since free block aggregation can only be done for blocks of the 86 * same type (i.e., DRR_FREE records can only be aggregated with 87 * other DRR_FREE records. DRR_FREEOBJECTS records can only be 88 * aggregated with other DRR_FREEOBJECTS records. 89 */ 90 if (dsp->dsa_pending_op != PENDING_NONE && 91 dsp->dsa_pending_op != PENDING_FREE) { 92 if (dump_bytes(dsp, dsp->dsa_drr, 93 sizeof (dmu_replay_record_t)) != 0) 94 return (EINTR); 95 dsp->dsa_pending_op = PENDING_NONE; 96 } 97 98 if (dsp->dsa_pending_op == PENDING_FREE) { 99 /* 100 * There should never be a PENDING_FREE if length is -1 101 * (because dump_dnode is the only place where this 102 * function is called with a -1, and only after flushing 103 * any pending record). 104 */ 105 ASSERT(length != -1ULL); 106 /* 107 * Check to see whether this free block can be aggregated 108 * with pending one. 109 */ 110 if (drrf->drr_object == object && drrf->drr_offset + 111 drrf->drr_length == offset) { 112 drrf->drr_length += length; 113 return (0); 114 } else { 115 /* not a continuation. Push out pending record */ 116 if (dump_bytes(dsp, dsp->dsa_drr, 117 sizeof (dmu_replay_record_t)) != 0) 118 return (EINTR); 119 dsp->dsa_pending_op = PENDING_NONE; 120 } 121 } 122 /* create a FREE record and make it pending */ 123 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 124 dsp->dsa_drr->drr_type = DRR_FREE; 125 drrf->drr_object = object; 126 drrf->drr_offset = offset; 127 drrf->drr_length = length; 128 drrf->drr_toguid = dsp->dsa_toguid; 129 if (length == -1ULL) { 130 if (dump_bytes(dsp, dsp->dsa_drr, 131 sizeof (dmu_replay_record_t)) != 0) 132 return (EINTR); 133 } else { 134 dsp->dsa_pending_op = PENDING_FREE; 135 } 136 137 return (0); 138 } 139 140 static int 141 dump_data(dmu_sendarg_t *dsp, dmu_object_type_t type, 142 uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data) 143 { 144 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write); 145 146 147 /* 148 * If there is any kind of pending aggregation (currently either 149 * a grouping of free objects or free blocks), push it out to 150 * the stream, since aggregation can't be done across operations 151 * of different types. 152 */ 153 if (dsp->dsa_pending_op != PENDING_NONE) { 154 if (dump_bytes(dsp, dsp->dsa_drr, 155 sizeof (dmu_replay_record_t)) != 0) 156 return (EINTR); 157 dsp->dsa_pending_op = PENDING_NONE; 158 } 159 /* write a DATA record */ 160 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 161 dsp->dsa_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 = dsp->dsa_toguid; 167 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp); 168 if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup) 169 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP; 170 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp)); 171 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp)); 172 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp)); 173 drrw->drr_key.ddk_cksum = bp->blk_cksum; 174 175 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0) 176 return (EINTR); 177 if (dump_bytes(dsp, data, blksz) != 0) 178 return (EINTR); 179 return (0); 180 } 181 182 static int 183 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data) 184 { 185 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill); 186 187 if (dsp->dsa_pending_op != PENDING_NONE) { 188 if (dump_bytes(dsp, dsp->dsa_drr, 189 sizeof (dmu_replay_record_t)) != 0) 190 return (EINTR); 191 dsp->dsa_pending_op = PENDING_NONE; 192 } 193 194 /* write a SPILL record */ 195 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 196 dsp->dsa_drr->drr_type = DRR_SPILL; 197 drrs->drr_object = object; 198 drrs->drr_length = blksz; 199 drrs->drr_toguid = dsp->dsa_toguid; 200 201 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t))) 202 return (EINTR); 203 if (dump_bytes(dsp, data, blksz)) 204 return (EINTR); 205 return (0); 206 } 207 208 static int 209 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs) 210 { 211 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects); 212 213 /* 214 * If there is a pending op, but it's not PENDING_FREEOBJECTS, 215 * push it out, since free block aggregation can only be done for 216 * blocks of the same type (i.e., DRR_FREE records can only be 217 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records 218 * can only be aggregated with other DRR_FREEOBJECTS records. 219 */ 220 if (dsp->dsa_pending_op != PENDING_NONE && 221 dsp->dsa_pending_op != PENDING_FREEOBJECTS) { 222 if (dump_bytes(dsp, dsp->dsa_drr, 223 sizeof (dmu_replay_record_t)) != 0) 224 return (EINTR); 225 dsp->dsa_pending_op = PENDING_NONE; 226 } 227 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) { 228 /* 229 * See whether this free object array can be aggregated 230 * with pending one 231 */ 232 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) { 233 drrfo->drr_numobjs += numobjs; 234 return (0); 235 } else { 236 /* can't be aggregated. Push out pending record */ 237 if (dump_bytes(dsp, dsp->dsa_drr, 238 sizeof (dmu_replay_record_t)) != 0) 239 return (EINTR); 240 dsp->dsa_pending_op = PENDING_NONE; 241 } 242 } 243 244 /* write a FREEOBJECTS record */ 245 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 246 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS; 247 drrfo->drr_firstobj = firstobj; 248 drrfo->drr_numobjs = numobjs; 249 drrfo->drr_toguid = dsp->dsa_toguid; 250 251 dsp->dsa_pending_op = PENDING_FREEOBJECTS; 252 253 return (0); 254 } 255 256 static int 257 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp) 258 { 259 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object); 260 261 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE) 262 return (dump_freeobjects(dsp, object, 1)); 263 264 if (dsp->dsa_pending_op != PENDING_NONE) { 265 if (dump_bytes(dsp, dsp->dsa_drr, 266 sizeof (dmu_replay_record_t)) != 0) 267 return (EINTR); 268 dsp->dsa_pending_op = PENDING_NONE; 269 } 270 271 /* write an OBJECT record */ 272 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); 273 dsp->dsa_drr->drr_type = DRR_OBJECT; 274 drro->drr_object = object; 275 drro->drr_type = dnp->dn_type; 276 drro->drr_bonustype = dnp->dn_bonustype; 277 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; 278 drro->drr_bonuslen = dnp->dn_bonuslen; 279 drro->drr_checksumtype = dnp->dn_checksum; 280 drro->drr_compress = dnp->dn_compress; 281 drro->drr_toguid = dsp->dsa_toguid; 282 283 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0) 284 return (EINTR); 285 286 if (dump_bytes(dsp, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) 287 return (EINTR); 288 289 /* free anything past the end of the file */ 290 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) * 291 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL)) 292 return (EINTR); 293 if (dsp->dsa_err) 294 return (EINTR); 295 return (0); 296 } 297 298 #define BP_SPAN(dnp, level) \ 299 (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \ 300 (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) 301 302 /* ARGSUSED */ 303 static int 304 backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf, 305 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 306 { 307 dmu_sendarg_t *dsp = arg; 308 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE; 309 int err = 0; 310 311 if (issig(JUSTLOOKING) && issig(FORREAL)) 312 return (EINTR); 313 314 if (zb->zb_object != DMU_META_DNODE_OBJECT && 315 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) { 316 return (0); 317 } else if (bp == NULL && zb->zb_object == DMU_META_DNODE_OBJECT) { 318 uint64_t span = BP_SPAN(dnp, zb->zb_level); 319 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT; 320 err = dump_freeobjects(dsp, dnobj, span >> DNODE_SHIFT); 321 } else if (bp == NULL) { 322 uint64_t span = BP_SPAN(dnp, zb->zb_level); 323 err = dump_free(dsp, zb->zb_object, zb->zb_blkid * span, span); 324 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) { 325 return (0); 326 } else if (type == DMU_OT_DNODE) { 327 dnode_phys_t *blk; 328 int i; 329 int blksz = BP_GET_LSIZE(bp); 330 uint32_t aflags = ARC_WAIT; 331 arc_buf_t *abuf; 332 333 if (dsl_read(NULL, spa, bp, pbuf, 334 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 335 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) 336 return (EIO); 337 338 blk = abuf->b_data; 339 for (i = 0; i < blksz >> DNODE_SHIFT; i++) { 340 uint64_t dnobj = (zb->zb_blkid << 341 (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i; 342 err = dump_dnode(dsp, dnobj, blk+i); 343 if (err) 344 break; 345 } 346 (void) arc_buf_remove_ref(abuf, &abuf); 347 } else if (type == DMU_OT_SA) { 348 uint32_t aflags = ARC_WAIT; 349 arc_buf_t *abuf; 350 int blksz = BP_GET_LSIZE(bp); 351 352 if (arc_read_nolock(NULL, spa, bp, 353 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 354 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) 355 return (EIO); 356 357 err = dump_spill(dsp, zb->zb_object, blksz, abuf->b_data); 358 (void) arc_buf_remove_ref(abuf, &abuf); 359 } else { /* it's a level-0 block of a regular object */ 360 uint32_t aflags = ARC_WAIT; 361 arc_buf_t *abuf; 362 int blksz = BP_GET_LSIZE(bp); 363 364 if (dsl_read(NULL, spa, bp, pbuf, 365 arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ, 366 ZIO_FLAG_CANFAIL, &aflags, zb) != 0) { 367 if (zfs_send_corrupt_data) { 368 /* Send a block filled with 0x"zfs badd bloc" */ 369 abuf = arc_buf_alloc(spa, blksz, &abuf, 370 ARC_BUFC_DATA); 371 uint64_t *ptr; 372 for (ptr = abuf->b_data; 373 (char *)ptr < (char *)abuf->b_data + blksz; 374 ptr++) 375 *ptr = 0x2f5baddb10c; 376 } else { 377 return (EIO); 378 } 379 } 380 381 err = dump_data(dsp, type, zb->zb_object, zb->zb_blkid * blksz, 382 blksz, bp, abuf->b_data); 383 (void) arc_buf_remove_ref(abuf, &abuf); 384 } 385 386 ASSERT(err == 0 || err == EINTR); 387 return (err); 388 } 389 390 int 391 dmu_send(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin, 392 int outfd, vnode_t *vp, offset_t *off) 393 { 394 dsl_dataset_t *ds = tosnap->os_dsl_dataset; 395 dsl_dataset_t *fromds = fromsnap ? fromsnap->os_dsl_dataset : NULL; 396 dmu_replay_record_t *drr; 397 dmu_sendarg_t *dsp; 398 int err; 399 uint64_t fromtxg = 0; 400 401 /* tosnap must be a snapshot */ 402 if (ds->ds_phys->ds_next_snap_obj == 0) 403 return (EINVAL); 404 405 /* fromsnap must be an earlier snapshot from the same fs as tosnap */ 406 if (fromds && (ds->ds_dir != fromds->ds_dir || 407 fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg)) 408 return (EXDEV); 409 410 if (fromorigin) { 411 dsl_pool_t *dp = ds->ds_dir->dd_pool; 412 413 if (fromsnap) 414 return (EINVAL); 415 416 if (dsl_dir_is_clone(ds->ds_dir)) { 417 rw_enter(&dp->dp_config_rwlock, RW_READER); 418 err = dsl_dataset_hold_obj(dp, 419 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds); 420 rw_exit(&dp->dp_config_rwlock); 421 if (err) 422 return (err); 423 } else { 424 fromorigin = B_FALSE; 425 } 426 } 427 428 429 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 430 drr->drr_type = DRR_BEGIN; 431 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; 432 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo, 433 DMU_SUBSTREAM); 434 435 #ifdef _KERNEL 436 if (dmu_objset_type(tosnap) == DMU_OST_ZFS) { 437 uint64_t version; 438 if (zfs_get_zplprop(tosnap, ZFS_PROP_VERSION, &version) != 0) { 439 kmem_free(drr, sizeof (dmu_replay_record_t)); 440 return (EINVAL); 441 } 442 if (version == ZPL_VERSION_SA) { 443 DMU_SET_FEATUREFLAGS( 444 drr->drr_u.drr_begin.drr_versioninfo, 445 DMU_BACKUP_FEATURE_SA_SPILL); 446 } 447 } 448 #endif 449 450 drr->drr_u.drr_begin.drr_creation_time = 451 ds->ds_phys->ds_creation_time; 452 drr->drr_u.drr_begin.drr_type = tosnap->os_phys->os_type; 453 if (fromorigin) 454 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE; 455 drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid; 456 if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET) 457 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA; 458 459 if (fromds) 460 drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid; 461 dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname); 462 463 if (fromds) 464 fromtxg = fromds->ds_phys->ds_creation_txg; 465 if (fromorigin) 466 dsl_dataset_rele(fromds, FTAG); 467 468 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP); 469 470 dsp->dsa_drr = drr; 471 dsp->dsa_vp = vp; 472 dsp->dsa_outfd = outfd; 473 dsp->dsa_proc = curproc; 474 dsp->dsa_os = tosnap; 475 dsp->dsa_off = off; 476 dsp->dsa_toguid = ds->ds_phys->ds_guid; 477 ZIO_SET_CHECKSUM(&dsp->dsa_zc, 0, 0, 0, 0); 478 dsp->dsa_pending_op = PENDING_NONE; 479 480 mutex_enter(&ds->ds_sendstream_lock); 481 list_insert_head(&ds->ds_sendstreams, dsp); 482 mutex_exit(&ds->ds_sendstream_lock); 483 484 if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) { 485 err = dsp->dsa_err; 486 goto out; 487 } 488 489 err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH, 490 backup_cb, dsp); 491 492 if (dsp->dsa_pending_op != PENDING_NONE) 493 if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) 494 err = EINTR; 495 496 if (err) { 497 if (err == EINTR && dsp->dsa_err) 498 err = dsp->dsa_err; 499 goto out; 500 } 501 502 bzero(drr, sizeof (dmu_replay_record_t)); 503 drr->drr_type = DRR_END; 504 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc; 505 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid; 506 507 if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) { 508 err = dsp->dsa_err; 509 goto out; 510 } 511 512 out: 513 mutex_enter(&ds->ds_sendstream_lock); 514 list_remove(&ds->ds_sendstreams, dsp); 515 mutex_exit(&ds->ds_sendstream_lock); 516 517 kmem_free(drr, sizeof (dmu_replay_record_t)); 518 kmem_free(dsp, sizeof (dmu_sendarg_t)); 519 520 return (err); 521 } 522 523 int 524 dmu_send_estimate(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin, 525 uint64_t *sizep) 526 { 527 dsl_dataset_t *ds = tosnap->os_dsl_dataset; 528 dsl_dataset_t *fromds = fromsnap ? fromsnap->os_dsl_dataset : NULL; 529 dsl_pool_t *dp = ds->ds_dir->dd_pool; 530 int err; 531 uint64_t size; 532 533 /* tosnap must be a snapshot */ 534 if (ds->ds_phys->ds_next_snap_obj == 0) 535 return (EINVAL); 536 537 /* fromsnap must be an earlier snapshot from the same fs as tosnap */ 538 if (fromds && (ds->ds_dir != fromds->ds_dir || 539 fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg)) 540 return (EXDEV); 541 542 if (fromorigin) { 543 if (fromsnap) 544 return (EINVAL); 545 546 if (dsl_dir_is_clone(ds->ds_dir)) { 547 rw_enter(&dp->dp_config_rwlock, RW_READER); 548 err = dsl_dataset_hold_obj(dp, 549 ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds); 550 rw_exit(&dp->dp_config_rwlock); 551 if (err) 552 return (err); 553 } else { 554 fromorigin = B_FALSE; 555 } 556 } 557 558 /* Get uncompressed size estimate of changed data. */ 559 if (fromds == NULL) { 560 size = ds->ds_phys->ds_uncompressed_bytes; 561 } else { 562 uint64_t used, comp; 563 err = dsl_dataset_space_written(fromds, ds, 564 &used, &comp, &size); 565 if (fromorigin) 566 dsl_dataset_rele(fromds, FTAG); 567 if (err) 568 return (err); 569 } 570 571 /* 572 * Assume that space (both on-disk and in-stream) is dominated by 573 * data. We will adjust for indirect blocks and the copies property, 574 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records). 575 */ 576 577 /* 578 * Subtract out approximate space used by indirect blocks. 579 * Assume most space is used by data blocks (non-indirect, non-dnode). 580 * Assume all blocks are recordsize. Assume ditto blocks and 581 * internal fragmentation counter out compression. 582 * 583 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per 584 * block, which we observe in practice. 585 */ 586 uint64_t recordsize; 587 rw_enter(&dp->dp_config_rwlock, RW_READER); 588 err = dsl_prop_get_ds(ds, "recordsize", 589 sizeof (recordsize), 1, &recordsize, NULL); 590 rw_exit(&dp->dp_config_rwlock); 591 if (err) 592 return (err); 593 size -= size / recordsize * sizeof (blkptr_t); 594 595 /* Add in the space for the record associated with each block. */ 596 size += size / recordsize * sizeof (dmu_replay_record_t); 597 598 *sizep = size; 599 600 return (0); 601 } 602 603 struct recvbeginsyncarg { 604 const char *tofs; 605 const char *tosnap; 606 dsl_dataset_t *origin; 607 uint64_t fromguid; 608 dmu_objset_type_t type; 609 void *tag; 610 boolean_t force; 611 uint64_t dsflags; 612 char clonelastname[MAXNAMELEN]; 613 dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */ 614 cred_t *cr; 615 }; 616 617 /* ARGSUSED */ 618 static int 619 recv_new_check(void *arg1, void *arg2, dmu_tx_t *tx) 620 { 621 dsl_dir_t *dd = arg1; 622 struct recvbeginsyncarg *rbsa = arg2; 623 objset_t *mos = dd->dd_pool->dp_meta_objset; 624 uint64_t val; 625 int err; 626 627 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj, 628 strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val); 629 630 if (err != ENOENT) 631 return (err ? err : EEXIST); 632 633 if (rbsa->origin) { 634 /* make sure it's a snap in the same pool */ 635 if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool) 636 return (EXDEV); 637 if (!dsl_dataset_is_snapshot(rbsa->origin)) 638 return (EINVAL); 639 if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid) 640 return (ENODEV); 641 } 642 643 return (0); 644 } 645 646 static void 647 recv_new_sync(void *arg1, void *arg2, dmu_tx_t *tx) 648 { 649 dsl_dir_t *dd = arg1; 650 struct recvbeginsyncarg *rbsa = arg2; 651 uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags; 652 uint64_t dsobj; 653 654 /* Create and open new dataset. */ 655 dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1, 656 rbsa->origin, flags, rbsa->cr, tx); 657 VERIFY(0 == dsl_dataset_own_obj(dd->dd_pool, dsobj, 658 B_TRUE, dmu_recv_tag, &rbsa->ds)); 659 660 if (rbsa->origin == NULL) { 661 (void) dmu_objset_create_impl(dd->dd_pool->dp_spa, 662 rbsa->ds, &rbsa->ds->ds_phys->ds_bp, rbsa->type, tx); 663 } 664 665 spa_history_log_internal(LOG_DS_REPLAY_FULL_SYNC, 666 dd->dd_pool->dp_spa, tx, "dataset = %lld", dsobj); 667 } 668 669 /* ARGSUSED */ 670 static int 671 recv_existing_check(void *arg1, void *arg2, dmu_tx_t *tx) 672 { 673 dsl_dataset_t *ds = arg1; 674 struct recvbeginsyncarg *rbsa = arg2; 675 int err; 676 uint64_t val; 677 678 /* must not have any changes since most recent snapshot */ 679 if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds)) 680 return (ETXTBSY); 681 682 /* new snapshot name must not exist */ 683 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 684 ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val); 685 if (err == 0) 686 return (EEXIST); 687 if (err != ENOENT) 688 return (err); 689 690 if (rbsa->fromguid) { 691 /* if incremental, most recent snapshot must match fromguid */ 692 if (ds->ds_prev == NULL) 693 return (ENODEV); 694 695 /* 696 * most recent snapshot must match fromguid, or there are no 697 * changes since the fromguid one 698 */ 699 if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid) { 700 uint64_t birth = ds->ds_prev->ds_phys->ds_bp.blk_birth; 701 uint64_t obj = ds->ds_prev->ds_phys->ds_prev_snap_obj; 702 while (obj != 0) { 703 dsl_dataset_t *snap; 704 err = dsl_dataset_hold_obj(ds->ds_dir->dd_pool, 705 obj, FTAG, &snap); 706 if (err) 707 return (ENODEV); 708 if (snap->ds_phys->ds_creation_txg < birth) { 709 dsl_dataset_rele(snap, FTAG); 710 return (ENODEV); 711 } 712 if (snap->ds_phys->ds_guid == rbsa->fromguid) { 713 dsl_dataset_rele(snap, FTAG); 714 break; /* it's ok */ 715 } 716 obj = snap->ds_phys->ds_prev_snap_obj; 717 dsl_dataset_rele(snap, FTAG); 718 } 719 if (obj == 0) 720 return (ENODEV); 721 } 722 } else { 723 /* if full, most recent snapshot must be $ORIGIN */ 724 if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL) 725 return (ENODEV); 726 } 727 728 /* temporary clone name must not exist */ 729 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset, 730 ds->ds_dir->dd_phys->dd_child_dir_zapobj, 731 rbsa->clonelastname, 8, 1, &val); 732 if (err == 0) 733 return (EEXIST); 734 if (err != ENOENT) 735 return (err); 736 737 return (0); 738 } 739 740 /* ARGSUSED */ 741 static void 742 recv_existing_sync(void *arg1, void *arg2, dmu_tx_t *tx) 743 { 744 dsl_dataset_t *ohds = arg1; 745 struct recvbeginsyncarg *rbsa = arg2; 746 dsl_pool_t *dp = ohds->ds_dir->dd_pool; 747 dsl_dataset_t *cds; 748 uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags; 749 uint64_t dsobj; 750 751 /* create and open the temporary clone */ 752 dsobj = dsl_dataset_create_sync(ohds->ds_dir, rbsa->clonelastname, 753 ohds->ds_prev, flags, rbsa->cr, tx); 754 VERIFY(0 == dsl_dataset_own_obj(dp, dsobj, B_TRUE, dmu_recv_tag, &cds)); 755 756 /* 757 * If we actually created a non-clone, we need to create the 758 * objset in our new dataset. 759 */ 760 if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds))) { 761 (void) dmu_objset_create_impl(dp->dp_spa, 762 cds, dsl_dataset_get_blkptr(cds), rbsa->type, tx); 763 } 764 765 rbsa->ds = cds; 766 767 spa_history_log_internal(LOG_DS_REPLAY_INC_SYNC, 768 dp->dp_spa, tx, "dataset = %lld", dsobj); 769 } 770 771 static boolean_t 772 dmu_recv_verify_features(dsl_dataset_t *ds, struct drr_begin *drrb) 773 { 774 int featureflags; 775 776 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); 777 778 /* Verify pool version supports SA if SA_SPILL feature set */ 779 return ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) && 780 (spa_version(dsl_dataset_get_spa(ds)) < SPA_VERSION_SA)); 781 } 782 783 /* 784 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin() 785 * succeeds; otherwise we will leak the holds on the datasets. 786 */ 787 int 788 dmu_recv_begin(char *tofs, char *tosnap, char *top_ds, struct drr_begin *drrb, 789 boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc) 790 { 791 int err = 0; 792 boolean_t byteswap; 793 struct recvbeginsyncarg rbsa = { 0 }; 794 uint64_t versioninfo; 795 int flags; 796 dsl_dataset_t *ds; 797 798 if (drrb->drr_magic == DMU_BACKUP_MAGIC) 799 byteswap = FALSE; 800 else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 801 byteswap = TRUE; 802 else 803 return (EINVAL); 804 805 rbsa.tofs = tofs; 806 rbsa.tosnap = tosnap; 807 rbsa.origin = origin ? origin->os_dsl_dataset : NULL; 808 rbsa.fromguid = drrb->drr_fromguid; 809 rbsa.type = drrb->drr_type; 810 rbsa.tag = FTAG; 811 rbsa.dsflags = 0; 812 rbsa.cr = CRED(); 813 versioninfo = drrb->drr_versioninfo; 814 flags = drrb->drr_flags; 815 816 if (byteswap) { 817 rbsa.type = BSWAP_32(rbsa.type); 818 rbsa.fromguid = BSWAP_64(rbsa.fromguid); 819 versioninfo = BSWAP_64(versioninfo); 820 flags = BSWAP_32(flags); 821 } 822 823 if (DMU_GET_STREAM_HDRTYPE(versioninfo) == DMU_COMPOUNDSTREAM || 824 rbsa.type >= DMU_OST_NUMTYPES || 825 ((flags & DRR_FLAG_CLONE) && origin == NULL)) 826 return (EINVAL); 827 828 if (flags & DRR_FLAG_CI_DATA) 829 rbsa.dsflags = DS_FLAG_CI_DATASET; 830 831 bzero(drc, sizeof (dmu_recv_cookie_t)); 832 drc->drc_drrb = drrb; 833 drc->drc_tosnap = tosnap; 834 drc->drc_top_ds = top_ds; 835 drc->drc_force = force; 836 837 /* 838 * Process the begin in syncing context. 839 */ 840 841 /* open the dataset we are logically receiving into */ 842 err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds); 843 if (err == 0) { 844 if (dmu_recv_verify_features(ds, drrb)) { 845 dsl_dataset_rele(ds, dmu_recv_tag); 846 return (ENOTSUP); 847 } 848 /* target fs already exists; recv into temp clone */ 849 850 /* Can't recv a clone into an existing fs */ 851 if (flags & DRR_FLAG_CLONE) { 852 dsl_dataset_rele(ds, dmu_recv_tag); 853 return (EINVAL); 854 } 855 856 /* must not have an incremental recv already in progress */ 857 if (!mutex_tryenter(&ds->ds_recvlock)) { 858 dsl_dataset_rele(ds, dmu_recv_tag); 859 return (EBUSY); 860 } 861 862 /* tmp clone name is: tofs/%tosnap" */ 863 (void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname), 864 "%%%s", tosnap); 865 rbsa.force = force; 866 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 867 recv_existing_check, recv_existing_sync, ds, &rbsa, 5); 868 if (err) { 869 mutex_exit(&ds->ds_recvlock); 870 dsl_dataset_rele(ds, dmu_recv_tag); 871 return (err); 872 } 873 drc->drc_logical_ds = ds; 874 drc->drc_real_ds = rbsa.ds; 875 } else if (err == ENOENT) { 876 /* target fs does not exist; must be a full backup or clone */ 877 char *cp; 878 879 /* 880 * If it's a non-clone incremental, we are missing the 881 * target fs, so fail the recv. 882 */ 883 if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE)) 884 return (ENOENT); 885 886 /* Open the parent of tofs */ 887 cp = strrchr(tofs, '/'); 888 *cp = '\0'; 889 err = dsl_dataset_hold(tofs, FTAG, &ds); 890 *cp = '/'; 891 if (err) 892 return (err); 893 894 if (dmu_recv_verify_features(ds, drrb)) { 895 dsl_dataset_rele(ds, FTAG); 896 return (ENOTSUP); 897 } 898 899 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 900 recv_new_check, recv_new_sync, ds->ds_dir, &rbsa, 5); 901 dsl_dataset_rele(ds, FTAG); 902 if (err) 903 return (err); 904 drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds; 905 drc->drc_newfs = B_TRUE; 906 } 907 908 return (err); 909 } 910 911 struct restorearg { 912 int err; 913 int byteswap; 914 vnode_t *vp; 915 char *buf; 916 uint64_t voff; 917 int bufsize; /* amount of memory allocated for buf */ 918 zio_cksum_t cksum; 919 avl_tree_t *guid_to_ds_map; 920 }; 921 922 typedef struct guid_map_entry { 923 uint64_t guid; 924 dsl_dataset_t *gme_ds; 925 avl_node_t avlnode; 926 } guid_map_entry_t; 927 928 static int 929 guid_compare(const void *arg1, const void *arg2) 930 { 931 const guid_map_entry_t *gmep1 = arg1; 932 const guid_map_entry_t *gmep2 = arg2; 933 934 if (gmep1->guid < gmep2->guid) 935 return (-1); 936 else if (gmep1->guid > gmep2->guid) 937 return (1); 938 return (0); 939 } 940 941 static void 942 free_guid_map_onexit(void *arg) 943 { 944 avl_tree_t *ca = arg; 945 void *cookie = NULL; 946 guid_map_entry_t *gmep; 947 948 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) { 949 dsl_dataset_rele(gmep->gme_ds, ca); 950 kmem_free(gmep, sizeof (guid_map_entry_t)); 951 } 952 avl_destroy(ca); 953 kmem_free(ca, sizeof (avl_tree_t)); 954 } 955 956 static void * 957 restore_read(struct restorearg *ra, int len) 958 { 959 void *rv; 960 int done = 0; 961 962 /* some things will require 8-byte alignment, so everything must */ 963 ASSERT3U(len % 8, ==, 0); 964 965 while (done < len) { 966 ssize_t resid; 967 968 ra->err = vn_rdwr(UIO_READ, ra->vp, 969 (caddr_t)ra->buf + done, len - done, 970 ra->voff, UIO_SYSSPACE, FAPPEND, 971 RLIM64_INFINITY, CRED(), &resid); 972 973 if (resid == len - done) 974 ra->err = EINVAL; 975 ra->voff += len - done - resid; 976 done = len - resid; 977 if (ra->err) 978 return (NULL); 979 } 980 981 ASSERT3U(done, ==, len); 982 rv = ra->buf; 983 if (ra->byteswap) 984 fletcher_4_incremental_byteswap(rv, len, &ra->cksum); 985 else 986 fletcher_4_incremental_native(rv, len, &ra->cksum); 987 return (rv); 988 } 989 990 static void 991 backup_byteswap(dmu_replay_record_t *drr) 992 { 993 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X)) 994 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X)) 995 drr->drr_type = BSWAP_32(drr->drr_type); 996 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen); 997 switch (drr->drr_type) { 998 case DRR_BEGIN: 999 DO64(drr_begin.drr_magic); 1000 DO64(drr_begin.drr_versioninfo); 1001 DO64(drr_begin.drr_creation_time); 1002 DO32(drr_begin.drr_type); 1003 DO32(drr_begin.drr_flags); 1004 DO64(drr_begin.drr_toguid); 1005 DO64(drr_begin.drr_fromguid); 1006 break; 1007 case DRR_OBJECT: 1008 DO64(drr_object.drr_object); 1009 /* DO64(drr_object.drr_allocation_txg); */ 1010 DO32(drr_object.drr_type); 1011 DO32(drr_object.drr_bonustype); 1012 DO32(drr_object.drr_blksz); 1013 DO32(drr_object.drr_bonuslen); 1014 DO64(drr_object.drr_toguid); 1015 break; 1016 case DRR_FREEOBJECTS: 1017 DO64(drr_freeobjects.drr_firstobj); 1018 DO64(drr_freeobjects.drr_numobjs); 1019 DO64(drr_freeobjects.drr_toguid); 1020 break; 1021 case DRR_WRITE: 1022 DO64(drr_write.drr_object); 1023 DO32(drr_write.drr_type); 1024 DO64(drr_write.drr_offset); 1025 DO64(drr_write.drr_length); 1026 DO64(drr_write.drr_toguid); 1027 DO64(drr_write.drr_key.ddk_cksum.zc_word[0]); 1028 DO64(drr_write.drr_key.ddk_cksum.zc_word[1]); 1029 DO64(drr_write.drr_key.ddk_cksum.zc_word[2]); 1030 DO64(drr_write.drr_key.ddk_cksum.zc_word[3]); 1031 DO64(drr_write.drr_key.ddk_prop); 1032 break; 1033 case DRR_WRITE_BYREF: 1034 DO64(drr_write_byref.drr_object); 1035 DO64(drr_write_byref.drr_offset); 1036 DO64(drr_write_byref.drr_length); 1037 DO64(drr_write_byref.drr_toguid); 1038 DO64(drr_write_byref.drr_refguid); 1039 DO64(drr_write_byref.drr_refobject); 1040 DO64(drr_write_byref.drr_refoffset); 1041 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[0]); 1042 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[1]); 1043 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[2]); 1044 DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[3]); 1045 DO64(drr_write_byref.drr_key.ddk_prop); 1046 break; 1047 case DRR_FREE: 1048 DO64(drr_free.drr_object); 1049 DO64(drr_free.drr_offset); 1050 DO64(drr_free.drr_length); 1051 DO64(drr_free.drr_toguid); 1052 break; 1053 case DRR_SPILL: 1054 DO64(drr_spill.drr_object); 1055 DO64(drr_spill.drr_length); 1056 DO64(drr_spill.drr_toguid); 1057 break; 1058 case DRR_END: 1059 DO64(drr_end.drr_checksum.zc_word[0]); 1060 DO64(drr_end.drr_checksum.zc_word[1]); 1061 DO64(drr_end.drr_checksum.zc_word[2]); 1062 DO64(drr_end.drr_checksum.zc_word[3]); 1063 DO64(drr_end.drr_toguid); 1064 break; 1065 } 1066 #undef DO64 1067 #undef DO32 1068 } 1069 1070 static int 1071 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro) 1072 { 1073 int err; 1074 dmu_tx_t *tx; 1075 void *data = NULL; 1076 1077 if (drro->drr_type == DMU_OT_NONE || 1078 !DMU_OT_IS_VALID(drro->drr_type) || 1079 !DMU_OT_IS_VALID(drro->drr_bonustype) || 1080 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS || 1081 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS || 1082 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) || 1083 drro->drr_blksz < SPA_MINBLOCKSIZE || 1084 drro->drr_blksz > SPA_MAXBLOCKSIZE || 1085 drro->drr_bonuslen > DN_MAX_BONUSLEN) { 1086 return (EINVAL); 1087 } 1088 1089 err = dmu_object_info(os, drro->drr_object, NULL); 1090 1091 if (err != 0 && err != ENOENT) 1092 return (EINVAL); 1093 1094 if (drro->drr_bonuslen) { 1095 data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8)); 1096 if (ra->err) 1097 return (ra->err); 1098 } 1099 1100 if (err == ENOENT) { 1101 /* currently free, want to be allocated */ 1102 tx = dmu_tx_create(os); 1103 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1104 err = dmu_tx_assign(tx, TXG_WAIT); 1105 if (err) { 1106 dmu_tx_abort(tx); 1107 return (err); 1108 } 1109 err = dmu_object_claim(os, drro->drr_object, 1110 drro->drr_type, drro->drr_blksz, 1111 drro->drr_bonustype, drro->drr_bonuslen, tx); 1112 dmu_tx_commit(tx); 1113 } else { 1114 /* currently allocated, want to be allocated */ 1115 err = dmu_object_reclaim(os, drro->drr_object, 1116 drro->drr_type, drro->drr_blksz, 1117 drro->drr_bonustype, drro->drr_bonuslen); 1118 } 1119 if (err) { 1120 return (EINVAL); 1121 } 1122 1123 tx = dmu_tx_create(os); 1124 dmu_tx_hold_bonus(tx, drro->drr_object); 1125 err = dmu_tx_assign(tx, TXG_WAIT); 1126 if (err) { 1127 dmu_tx_abort(tx); 1128 return (err); 1129 } 1130 1131 dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype, 1132 tx); 1133 dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx); 1134 1135 if (data != NULL) { 1136 dmu_buf_t *db; 1137 1138 VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db)); 1139 dmu_buf_will_dirty(db, tx); 1140 1141 ASSERT3U(db->db_size, >=, drro->drr_bonuslen); 1142 bcopy(data, db->db_data, drro->drr_bonuslen); 1143 if (ra->byteswap) { 1144 dmu_object_byteswap_t byteswap = 1145 DMU_OT_BYTESWAP(drro->drr_bonustype); 1146 dmu_ot_byteswap[byteswap].ob_func(db->db_data, 1147 drro->drr_bonuslen); 1148 } 1149 dmu_buf_rele(db, FTAG); 1150 } 1151 dmu_tx_commit(tx); 1152 return (0); 1153 } 1154 1155 /* ARGSUSED */ 1156 static int 1157 restore_freeobjects(struct restorearg *ra, objset_t *os, 1158 struct drr_freeobjects *drrfo) 1159 { 1160 uint64_t obj; 1161 1162 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj) 1163 return (EINVAL); 1164 1165 for (obj = drrfo->drr_firstobj; 1166 obj < drrfo->drr_firstobj + drrfo->drr_numobjs; 1167 (void) dmu_object_next(os, &obj, FALSE, 0)) { 1168 int err; 1169 1170 if (dmu_object_info(os, obj, NULL) != 0) 1171 continue; 1172 1173 err = dmu_free_object(os, obj); 1174 if (err) 1175 return (err); 1176 } 1177 return (0); 1178 } 1179 1180 static int 1181 restore_write(struct restorearg *ra, objset_t *os, 1182 struct drr_write *drrw) 1183 { 1184 dmu_tx_t *tx; 1185 void *data; 1186 int err; 1187 1188 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset || 1189 !DMU_OT_IS_VALID(drrw->drr_type)) 1190 return (EINVAL); 1191 1192 data = restore_read(ra, drrw->drr_length); 1193 if (data == NULL) 1194 return (ra->err); 1195 1196 if (dmu_object_info(os, drrw->drr_object, NULL) != 0) 1197 return (EINVAL); 1198 1199 tx = dmu_tx_create(os); 1200 1201 dmu_tx_hold_write(tx, drrw->drr_object, 1202 drrw->drr_offset, drrw->drr_length); 1203 err = dmu_tx_assign(tx, TXG_WAIT); 1204 if (err) { 1205 dmu_tx_abort(tx); 1206 return (err); 1207 } 1208 if (ra->byteswap) { 1209 dmu_object_byteswap_t byteswap = 1210 DMU_OT_BYTESWAP(drrw->drr_type); 1211 dmu_ot_byteswap[byteswap].ob_func(data, drrw->drr_length); 1212 } 1213 dmu_write(os, drrw->drr_object, 1214 drrw->drr_offset, drrw->drr_length, data, tx); 1215 dmu_tx_commit(tx); 1216 return (0); 1217 } 1218 1219 /* 1220 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed 1221 * streams to refer to a copy of the data that is already on the 1222 * system because it came in earlier in the stream. This function 1223 * finds the earlier copy of the data, and uses that copy instead of 1224 * data from the stream to fulfill this write. 1225 */ 1226 static int 1227 restore_write_byref(struct restorearg *ra, objset_t *os, 1228 struct drr_write_byref *drrwbr) 1229 { 1230 dmu_tx_t *tx; 1231 int err; 1232 guid_map_entry_t gmesrch; 1233 guid_map_entry_t *gmep; 1234 avl_index_t where; 1235 objset_t *ref_os = NULL; 1236 dmu_buf_t *dbp; 1237 1238 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset) 1239 return (EINVAL); 1240 1241 /* 1242 * If the GUID of the referenced dataset is different from the 1243 * GUID of the target dataset, find the referenced dataset. 1244 */ 1245 if (drrwbr->drr_toguid != drrwbr->drr_refguid) { 1246 gmesrch.guid = drrwbr->drr_refguid; 1247 if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch, 1248 &where)) == NULL) { 1249 return (EINVAL); 1250 } 1251 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os)) 1252 return (EINVAL); 1253 } else { 1254 ref_os = os; 1255 } 1256 1257 if (err = dmu_buf_hold(ref_os, drrwbr->drr_refobject, 1258 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH)) 1259 return (err); 1260 1261 tx = dmu_tx_create(os); 1262 1263 dmu_tx_hold_write(tx, drrwbr->drr_object, 1264 drrwbr->drr_offset, drrwbr->drr_length); 1265 err = dmu_tx_assign(tx, TXG_WAIT); 1266 if (err) { 1267 dmu_tx_abort(tx); 1268 return (err); 1269 } 1270 dmu_write(os, drrwbr->drr_object, 1271 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx); 1272 dmu_buf_rele(dbp, FTAG); 1273 dmu_tx_commit(tx); 1274 return (0); 1275 } 1276 1277 static int 1278 restore_spill(struct restorearg *ra, objset_t *os, struct drr_spill *drrs) 1279 { 1280 dmu_tx_t *tx; 1281 void *data; 1282 dmu_buf_t *db, *db_spill; 1283 int err; 1284 1285 if (drrs->drr_length < SPA_MINBLOCKSIZE || 1286 drrs->drr_length > SPA_MAXBLOCKSIZE) 1287 return (EINVAL); 1288 1289 data = restore_read(ra, drrs->drr_length); 1290 if (data == NULL) 1291 return (ra->err); 1292 1293 if (dmu_object_info(os, drrs->drr_object, NULL) != 0) 1294 return (EINVAL); 1295 1296 VERIFY(0 == dmu_bonus_hold(os, drrs->drr_object, FTAG, &db)); 1297 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) { 1298 dmu_buf_rele(db, FTAG); 1299 return (err); 1300 } 1301 1302 tx = dmu_tx_create(os); 1303 1304 dmu_tx_hold_spill(tx, db->db_object); 1305 1306 err = dmu_tx_assign(tx, TXG_WAIT); 1307 if (err) { 1308 dmu_buf_rele(db, FTAG); 1309 dmu_buf_rele(db_spill, FTAG); 1310 dmu_tx_abort(tx); 1311 return (err); 1312 } 1313 dmu_buf_will_dirty(db_spill, tx); 1314 1315 if (db_spill->db_size < drrs->drr_length) 1316 VERIFY(0 == dbuf_spill_set_blksz(db_spill, 1317 drrs->drr_length, tx)); 1318 bcopy(data, db_spill->db_data, drrs->drr_length); 1319 1320 dmu_buf_rele(db, FTAG); 1321 dmu_buf_rele(db_spill, FTAG); 1322 1323 dmu_tx_commit(tx); 1324 return (0); 1325 } 1326 1327 /* ARGSUSED */ 1328 static int 1329 restore_free(struct restorearg *ra, objset_t *os, 1330 struct drr_free *drrf) 1331 { 1332 int err; 1333 1334 if (drrf->drr_length != -1ULL && 1335 drrf->drr_offset + drrf->drr_length < drrf->drr_offset) 1336 return (EINVAL); 1337 1338 if (dmu_object_info(os, drrf->drr_object, NULL) != 0) 1339 return (EINVAL); 1340 1341 err = dmu_free_long_range(os, drrf->drr_object, 1342 drrf->drr_offset, drrf->drr_length); 1343 return (err); 1344 } 1345 1346 /* 1347 * NB: callers *must* call dmu_recv_end() if this succeeds. 1348 */ 1349 int 1350 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp, 1351 int cleanup_fd, uint64_t *action_handlep) 1352 { 1353 struct restorearg ra = { 0 }; 1354 dmu_replay_record_t *drr; 1355 objset_t *os; 1356 zio_cksum_t pcksum; 1357 int featureflags; 1358 1359 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) 1360 ra.byteswap = TRUE; 1361 1362 { 1363 /* compute checksum of drr_begin record */ 1364 dmu_replay_record_t *drr; 1365 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); 1366 1367 drr->drr_type = DRR_BEGIN; 1368 drr->drr_u.drr_begin = *drc->drc_drrb; 1369 if (ra.byteswap) { 1370 fletcher_4_incremental_byteswap(drr, 1371 sizeof (dmu_replay_record_t), &ra.cksum); 1372 } else { 1373 fletcher_4_incremental_native(drr, 1374 sizeof (dmu_replay_record_t), &ra.cksum); 1375 } 1376 kmem_free(drr, sizeof (dmu_replay_record_t)); 1377 } 1378 1379 if (ra.byteswap) { 1380 struct drr_begin *drrb = drc->drc_drrb; 1381 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 1382 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo); 1383 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time); 1384 drrb->drr_type = BSWAP_32(drrb->drr_type); 1385 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 1386 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid); 1387 } 1388 1389 ra.vp = vp; 1390 ra.voff = *voffp; 1391 ra.bufsize = 1<<20; 1392 ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP); 1393 1394 /* these were verified in dmu_recv_begin */ 1395 ASSERT(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo) == 1396 DMU_SUBSTREAM); 1397 ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES); 1398 1399 /* 1400 * Open the objset we are modifying. 1401 */ 1402 VERIFY(dmu_objset_from_ds(drc->drc_real_ds, &os) == 0); 1403 1404 ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT); 1405 1406 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo); 1407 1408 /* if this stream is dedup'ed, set up the avl tree for guid mapping */ 1409 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) { 1410 minor_t minor; 1411 1412 if (cleanup_fd == -1) { 1413 ra.err = EBADF; 1414 goto out; 1415 } 1416 ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor); 1417 if (ra.err) { 1418 cleanup_fd = -1; 1419 goto out; 1420 } 1421 1422 if (*action_handlep == 0) { 1423 ra.guid_to_ds_map = 1424 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP); 1425 avl_create(ra.guid_to_ds_map, guid_compare, 1426 sizeof (guid_map_entry_t), 1427 offsetof(guid_map_entry_t, avlnode)); 1428 ra.err = zfs_onexit_add_cb(minor, 1429 free_guid_map_onexit, ra.guid_to_ds_map, 1430 action_handlep); 1431 if (ra.err) 1432 goto out; 1433 } else { 1434 ra.err = zfs_onexit_cb_data(minor, *action_handlep, 1435 (void **)&ra.guid_to_ds_map); 1436 if (ra.err) 1437 goto out; 1438 } 1439 1440 drc->drc_guid_to_ds_map = ra.guid_to_ds_map; 1441 } 1442 1443 /* 1444 * Read records and process them. 1445 */ 1446 pcksum = ra.cksum; 1447 while (ra.err == 0 && 1448 NULL != (drr = restore_read(&ra, sizeof (*drr)))) { 1449 if (issig(JUSTLOOKING) && issig(FORREAL)) { 1450 ra.err = EINTR; 1451 goto out; 1452 } 1453 1454 if (ra.byteswap) 1455 backup_byteswap(drr); 1456 1457 switch (drr->drr_type) { 1458 case DRR_OBJECT: 1459 { 1460 /* 1461 * We need to make a copy of the record header, 1462 * because restore_{object,write} may need to 1463 * restore_read(), which will invalidate drr. 1464 */ 1465 struct drr_object drro = drr->drr_u.drr_object; 1466 ra.err = restore_object(&ra, os, &drro); 1467 break; 1468 } 1469 case DRR_FREEOBJECTS: 1470 { 1471 struct drr_freeobjects drrfo = 1472 drr->drr_u.drr_freeobjects; 1473 ra.err = restore_freeobjects(&ra, os, &drrfo); 1474 break; 1475 } 1476 case DRR_WRITE: 1477 { 1478 struct drr_write drrw = drr->drr_u.drr_write; 1479 ra.err = restore_write(&ra, os, &drrw); 1480 break; 1481 } 1482 case DRR_WRITE_BYREF: 1483 { 1484 struct drr_write_byref drrwbr = 1485 drr->drr_u.drr_write_byref; 1486 ra.err = restore_write_byref(&ra, os, &drrwbr); 1487 break; 1488 } 1489 case DRR_FREE: 1490 { 1491 struct drr_free drrf = drr->drr_u.drr_free; 1492 ra.err = restore_free(&ra, os, &drrf); 1493 break; 1494 } 1495 case DRR_END: 1496 { 1497 struct drr_end drre = drr->drr_u.drr_end; 1498 /* 1499 * We compare against the *previous* checksum 1500 * value, because the stored checksum is of 1501 * everything before the DRR_END record. 1502 */ 1503 if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum)) 1504 ra.err = ECKSUM; 1505 goto out; 1506 } 1507 case DRR_SPILL: 1508 { 1509 struct drr_spill drrs = drr->drr_u.drr_spill; 1510 ra.err = restore_spill(&ra, os, &drrs); 1511 break; 1512 } 1513 default: 1514 ra.err = EINVAL; 1515 goto out; 1516 } 1517 pcksum = ra.cksum; 1518 } 1519 ASSERT(ra.err != 0); 1520 1521 out: 1522 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1)) 1523 zfs_onexit_fd_rele(cleanup_fd); 1524 1525 if (ra.err != 0) { 1526 /* 1527 * destroy what we created, so we don't leave it in the 1528 * inconsistent restoring state. 1529 */ 1530 txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0); 1531 1532 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, 1533 B_FALSE); 1534 if (drc->drc_real_ds != drc->drc_logical_ds) { 1535 mutex_exit(&drc->drc_logical_ds->ds_recvlock); 1536 dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag); 1537 } 1538 } 1539 1540 kmem_free(ra.buf, ra.bufsize); 1541 *voffp = ra.voff; 1542 return (ra.err); 1543 } 1544 1545 struct recvendsyncarg { 1546 char *tosnap; 1547 uint64_t creation_time; 1548 uint64_t toguid; 1549 }; 1550 1551 static int 1552 recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx) 1553 { 1554 dsl_dataset_t *ds = arg1; 1555 struct recvendsyncarg *resa = arg2; 1556 1557 return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx)); 1558 } 1559 1560 static void 1561 recv_end_sync(void *arg1, void *arg2, dmu_tx_t *tx) 1562 { 1563 dsl_dataset_t *ds = arg1; 1564 struct recvendsyncarg *resa = arg2; 1565 1566 dsl_dataset_snapshot_sync(ds, resa->tosnap, tx); 1567 1568 /* set snapshot's creation time and guid */ 1569 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); 1570 ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time; 1571 ds->ds_prev->ds_phys->ds_guid = resa->toguid; 1572 ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT; 1573 1574 dmu_buf_will_dirty(ds->ds_dbuf, tx); 1575 ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT; 1576 } 1577 1578 static int 1579 add_ds_to_guidmap(avl_tree_t *guid_map, dsl_dataset_t *ds) 1580 { 1581 dsl_pool_t *dp = ds->ds_dir->dd_pool; 1582 uint64_t snapobj = ds->ds_phys->ds_prev_snap_obj; 1583 dsl_dataset_t *snapds; 1584 guid_map_entry_t *gmep; 1585 int err; 1586 1587 ASSERT(guid_map != NULL); 1588 1589 rw_enter(&dp->dp_config_rwlock, RW_READER); 1590 err = dsl_dataset_hold_obj(dp, snapobj, guid_map, &snapds); 1591 if (err == 0) { 1592 gmep = kmem_alloc(sizeof (guid_map_entry_t), KM_SLEEP); 1593 gmep->guid = snapds->ds_phys->ds_guid; 1594 gmep->gme_ds = snapds; 1595 avl_add(guid_map, gmep); 1596 } 1597 1598 rw_exit(&dp->dp_config_rwlock); 1599 return (err); 1600 } 1601 1602 static int 1603 dmu_recv_existing_end(dmu_recv_cookie_t *drc) 1604 { 1605 struct recvendsyncarg resa; 1606 dsl_dataset_t *ds = drc->drc_logical_ds; 1607 int err, myerr; 1608 1609 /* 1610 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean() 1611 * expects it to have a ds_user_ptr (and zil), but clone_swap() 1612 * can close it. 1613 */ 1614 txg_wait_synced(ds->ds_dir->dd_pool, 0); 1615 1616 if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) { 1617 err = dsl_dataset_clone_swap(drc->drc_real_ds, ds, 1618 drc->drc_force); 1619 if (err) 1620 goto out; 1621 } else { 1622 mutex_exit(&ds->ds_recvlock); 1623 dsl_dataset_rele(ds, dmu_recv_tag); 1624 (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, 1625 B_FALSE); 1626 return (EBUSY); 1627 } 1628 1629 resa.creation_time = drc->drc_drrb->drr_creation_time; 1630 resa.toguid = drc->drc_drrb->drr_toguid; 1631 resa.tosnap = drc->drc_tosnap; 1632 1633 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1634 recv_end_check, recv_end_sync, ds, &resa, 3); 1635 if (err) { 1636 /* swap back */ 1637 (void) dsl_dataset_clone_swap(drc->drc_real_ds, ds, B_TRUE); 1638 } 1639 1640 out: 1641 mutex_exit(&ds->ds_recvlock); 1642 if (err == 0 && drc->drc_guid_to_ds_map != NULL) 1643 (void) add_ds_to_guidmap(drc->drc_guid_to_ds_map, ds); 1644 dsl_dataset_disown(ds, dmu_recv_tag); 1645 myerr = dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, B_FALSE); 1646 ASSERT3U(myerr, ==, 0); 1647 return (err); 1648 } 1649 1650 static int 1651 dmu_recv_new_end(dmu_recv_cookie_t *drc) 1652 { 1653 struct recvendsyncarg resa; 1654 dsl_dataset_t *ds = drc->drc_logical_ds; 1655 int err; 1656 1657 /* 1658 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean() 1659 * expects it to have a ds_user_ptr (and zil), but clone_swap() 1660 * can close it. 1661 */ 1662 txg_wait_synced(ds->ds_dir->dd_pool, 0); 1663 1664 resa.creation_time = drc->drc_drrb->drr_creation_time; 1665 resa.toguid = drc->drc_drrb->drr_toguid; 1666 resa.tosnap = drc->drc_tosnap; 1667 1668 err = dsl_sync_task_do(ds->ds_dir->dd_pool, 1669 recv_end_check, recv_end_sync, ds, &resa, 3); 1670 if (err) { 1671 /* clean up the fs we just recv'd into */ 1672 (void) dsl_dataset_destroy(ds, dmu_recv_tag, B_FALSE); 1673 } else { 1674 if (drc->drc_guid_to_ds_map != NULL) 1675 (void) add_ds_to_guidmap(drc->drc_guid_to_ds_map, ds); 1676 /* release the hold from dmu_recv_begin */ 1677 dsl_dataset_disown(ds, dmu_recv_tag); 1678 } 1679 return (err); 1680 } 1681 1682 int 1683 dmu_recv_end(dmu_recv_cookie_t *drc) 1684 { 1685 if (drc->drc_logical_ds != drc->drc_real_ds) 1686 return (dmu_recv_existing_end(drc)); 1687 else 1688 return (dmu_recv_new_end(drc)); 1689 }