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