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