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