Print this page
4047 panic from dbuf_free_range() from dmu_free_object() while doing zfs receive
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/dsl_destroy.c
+++ new/usr/src/uts/common/fs/zfs/dsl_destroy.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 (c) 2013 by Delphix. All rights reserved.
24 24 * Copyright (c) 2013 Steven Hartland. All rights reserved.
25 25 */
26 26
27 27 #include <sys/zfs_context.h>
28 28 #include <sys/dsl_userhold.h>
29 29 #include <sys/dsl_dataset.h>
30 30 #include <sys/dsl_synctask.h>
31 31 #include <sys/dmu_tx.h>
32 32 #include <sys/dsl_pool.h>
33 33 #include <sys/dsl_dir.h>
34 34 #include <sys/dmu_traverse.h>
35 35 #include <sys/dsl_scan.h>
36 36 #include <sys/dmu_objset.h>
37 37 #include <sys/zap.h>
38 38 #include <sys/zfeature.h>
39 39 #include <sys/zfs_ioctl.h>
40 40 #include <sys/dsl_deleg.h>
41 41
42 42 typedef struct dmu_snapshots_destroy_arg {
43 43 nvlist_t *dsda_snaps;
44 44 nvlist_t *dsda_successful_snaps;
45 45 boolean_t dsda_defer;
46 46 nvlist_t *dsda_errlist;
47 47 } dmu_snapshots_destroy_arg_t;
48 48
49 49 int
50 50 dsl_destroy_snapshot_check_impl(dsl_dataset_t *ds, boolean_t defer)
51 51 {
52 52 if (!dsl_dataset_is_snapshot(ds))
53 53 return (SET_ERROR(EINVAL));
54 54
55 55 if (dsl_dataset_long_held(ds))
56 56 return (SET_ERROR(EBUSY));
57 57
58 58 /*
59 59 * Only allow deferred destroy on pools that support it.
60 60 * NOTE: deferred destroy is only supported on snapshots.
61 61 */
62 62 if (defer) {
63 63 if (spa_version(ds->ds_dir->dd_pool->dp_spa) <
64 64 SPA_VERSION_USERREFS)
65 65 return (SET_ERROR(ENOTSUP));
66 66 return (0);
67 67 }
68 68
69 69 /*
70 70 * If this snapshot has an elevated user reference count,
71 71 * we can't destroy it yet.
72 72 */
73 73 if (ds->ds_userrefs > 0)
74 74 return (SET_ERROR(EBUSY));
75 75
76 76 /*
77 77 * Can't delete a branch point.
78 78 */
79 79 if (ds->ds_phys->ds_num_children > 1)
80 80 return (SET_ERROR(EEXIST));
81 81
82 82 return (0);
83 83 }
84 84
85 85 static int
86 86 dsl_destroy_snapshot_check(void *arg, dmu_tx_t *tx)
87 87 {
88 88 dmu_snapshots_destroy_arg_t *dsda = arg;
89 89 dsl_pool_t *dp = dmu_tx_pool(tx);
90 90 nvpair_t *pair;
91 91 int error = 0;
92 92
93 93 if (!dmu_tx_is_syncing(tx))
94 94 return (0);
95 95
96 96 for (pair = nvlist_next_nvpair(dsda->dsda_snaps, NULL);
97 97 pair != NULL; pair = nvlist_next_nvpair(dsda->dsda_snaps, pair)) {
98 98 dsl_dataset_t *ds;
99 99
100 100 error = dsl_dataset_hold(dp, nvpair_name(pair),
101 101 FTAG, &ds);
102 102
103 103 /*
104 104 * If the snapshot does not exist, silently ignore it
105 105 * (it's "already destroyed").
106 106 */
107 107 if (error == ENOENT)
108 108 continue;
109 109
110 110 if (error == 0) {
111 111 error = dsl_destroy_snapshot_check_impl(ds,
112 112 dsda->dsda_defer);
113 113 dsl_dataset_rele(ds, FTAG);
114 114 }
115 115
116 116 if (error == 0) {
117 117 fnvlist_add_boolean(dsda->dsda_successful_snaps,
118 118 nvpair_name(pair));
119 119 } else {
120 120 fnvlist_add_int32(dsda->dsda_errlist,
121 121 nvpair_name(pair), error);
122 122 }
123 123 }
124 124
125 125 pair = nvlist_next_nvpair(dsda->dsda_errlist, NULL);
126 126 if (pair != NULL)
127 127 return (fnvpair_value_int32(pair));
128 128
129 129 return (0);
130 130 }
131 131
132 132 struct process_old_arg {
133 133 dsl_dataset_t *ds;
134 134 dsl_dataset_t *ds_prev;
135 135 boolean_t after_branch_point;
136 136 zio_t *pio;
137 137 uint64_t used, comp, uncomp;
138 138 };
139 139
140 140 static int
141 141 process_old_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
142 142 {
143 143 struct process_old_arg *poa = arg;
144 144 dsl_pool_t *dp = poa->ds->ds_dir->dd_pool;
145 145
146 146 if (bp->blk_birth <= poa->ds->ds_phys->ds_prev_snap_txg) {
147 147 dsl_deadlist_insert(&poa->ds->ds_deadlist, bp, tx);
148 148 if (poa->ds_prev && !poa->after_branch_point &&
149 149 bp->blk_birth >
150 150 poa->ds_prev->ds_phys->ds_prev_snap_txg) {
151 151 poa->ds_prev->ds_phys->ds_unique_bytes +=
152 152 bp_get_dsize_sync(dp->dp_spa, bp);
153 153 }
154 154 } else {
155 155 poa->used += bp_get_dsize_sync(dp->dp_spa, bp);
156 156 poa->comp += BP_GET_PSIZE(bp);
157 157 poa->uncomp += BP_GET_UCSIZE(bp);
158 158 dsl_free_sync(poa->pio, dp, tx->tx_txg, bp);
159 159 }
160 160 return (0);
161 161 }
162 162
163 163 static void
164 164 process_old_deadlist(dsl_dataset_t *ds, dsl_dataset_t *ds_prev,
165 165 dsl_dataset_t *ds_next, boolean_t after_branch_point, dmu_tx_t *tx)
166 166 {
167 167 struct process_old_arg poa = { 0 };
168 168 dsl_pool_t *dp = ds->ds_dir->dd_pool;
169 169 objset_t *mos = dp->dp_meta_objset;
170 170 uint64_t deadlist_obj;
171 171
172 172 ASSERT(ds->ds_deadlist.dl_oldfmt);
173 173 ASSERT(ds_next->ds_deadlist.dl_oldfmt);
174 174
175 175 poa.ds = ds;
176 176 poa.ds_prev = ds_prev;
177 177 poa.after_branch_point = after_branch_point;
178 178 poa.pio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
179 179 VERIFY0(bpobj_iterate(&ds_next->ds_deadlist.dl_bpobj,
180 180 process_old_cb, &poa, tx));
181 181 VERIFY0(zio_wait(poa.pio));
182 182 ASSERT3U(poa.used, ==, ds->ds_phys->ds_unique_bytes);
183 183
184 184 /* change snapused */
185 185 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
186 186 -poa.used, -poa.comp, -poa.uncomp, tx);
187 187
188 188 /* swap next's deadlist to our deadlist */
189 189 dsl_deadlist_close(&ds->ds_deadlist);
190 190 dsl_deadlist_close(&ds_next->ds_deadlist);
191 191 deadlist_obj = ds->ds_phys->ds_deadlist_obj;
192 192 ds->ds_phys->ds_deadlist_obj = ds_next->ds_phys->ds_deadlist_obj;
193 193 ds_next->ds_phys->ds_deadlist_obj = deadlist_obj;
194 194 dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
195 195 dsl_deadlist_open(&ds_next->ds_deadlist, mos,
196 196 ds_next->ds_phys->ds_deadlist_obj);
197 197 }
198 198
199 199 static void
200 200 dsl_dataset_remove_clones_key(dsl_dataset_t *ds, uint64_t mintxg, dmu_tx_t *tx)
201 201 {
202 202 objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
203 203 zap_cursor_t zc;
204 204 zap_attribute_t za;
205 205
206 206 /*
207 207 * If it is the old version, dd_clones doesn't exist so we can't
208 208 * find the clones, but dsl_deadlist_remove_key() is a no-op so it
209 209 * doesn't matter.
210 210 */
211 211 if (ds->ds_dir->dd_phys->dd_clones == 0)
212 212 return;
213 213
214 214 for (zap_cursor_init(&zc, mos, ds->ds_dir->dd_phys->dd_clones);
215 215 zap_cursor_retrieve(&zc, &za) == 0;
216 216 zap_cursor_advance(&zc)) {
217 217 dsl_dataset_t *clone;
218 218
219 219 VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
220 220 za.za_first_integer, FTAG, &clone));
221 221 if (clone->ds_dir->dd_origin_txg > mintxg) {
222 222 dsl_deadlist_remove_key(&clone->ds_deadlist,
223 223 mintxg, tx);
224 224 dsl_dataset_remove_clones_key(clone, mintxg, tx);
225 225 }
226 226 dsl_dataset_rele(clone, FTAG);
227 227 }
228 228 zap_cursor_fini(&zc);
229 229 }
230 230
231 231 void
232 232 dsl_destroy_snapshot_sync_impl(dsl_dataset_t *ds, boolean_t defer, dmu_tx_t *tx)
233 233 {
234 234 int err;
235 235 int after_branch_point = FALSE;
236 236 dsl_pool_t *dp = ds->ds_dir->dd_pool;
237 237 objset_t *mos = dp->dp_meta_objset;
238 238 dsl_dataset_t *ds_prev = NULL;
239 239 uint64_t obj;
240 240
241 241 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
242 242 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
243 243 ASSERT(refcount_is_zero(&ds->ds_longholds));
244 244
245 245 if (defer &&
246 246 (ds->ds_userrefs > 0 || ds->ds_phys->ds_num_children > 1)) {
247 247 ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
248 248 dmu_buf_will_dirty(ds->ds_dbuf, tx);
249 249 ds->ds_phys->ds_flags |= DS_FLAG_DEFER_DESTROY;
250 250 spa_history_log_internal_ds(ds, "defer_destroy", tx, "");
251 251 return;
252 252 }
253 253
254 254 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
255 255
256 256 /* We need to log before removing it from the namespace. */
257 257 spa_history_log_internal_ds(ds, "destroy", tx, "");
258 258
259 259 dsl_scan_ds_destroyed(ds, tx);
260 260
261 261 obj = ds->ds_object;
262 262
263 263 if (ds->ds_phys->ds_prev_snap_obj != 0) {
264 264 ASSERT3P(ds->ds_prev, ==, NULL);
265 265 VERIFY0(dsl_dataset_hold_obj(dp,
266 266 ds->ds_phys->ds_prev_snap_obj, FTAG, &ds_prev));
267 267 after_branch_point =
268 268 (ds_prev->ds_phys->ds_next_snap_obj != obj);
269 269
270 270 dmu_buf_will_dirty(ds_prev->ds_dbuf, tx);
271 271 if (after_branch_point &&
272 272 ds_prev->ds_phys->ds_next_clones_obj != 0) {
273 273 dsl_dataset_remove_from_next_clones(ds_prev, obj, tx);
274 274 if (ds->ds_phys->ds_next_snap_obj != 0) {
275 275 VERIFY0(zap_add_int(mos,
276 276 ds_prev->ds_phys->ds_next_clones_obj,
277 277 ds->ds_phys->ds_next_snap_obj, tx));
278 278 }
279 279 }
280 280 if (!after_branch_point) {
281 281 ds_prev->ds_phys->ds_next_snap_obj =
282 282 ds->ds_phys->ds_next_snap_obj;
283 283 }
284 284 }
285 285
286 286 dsl_dataset_t *ds_next;
287 287 uint64_t old_unique;
288 288 uint64_t used = 0, comp = 0, uncomp = 0;
289 289
290 290 VERIFY0(dsl_dataset_hold_obj(dp,
291 291 ds->ds_phys->ds_next_snap_obj, FTAG, &ds_next));
292 292 ASSERT3U(ds_next->ds_phys->ds_prev_snap_obj, ==, obj);
293 293
294 294 old_unique = ds_next->ds_phys->ds_unique_bytes;
295 295
296 296 dmu_buf_will_dirty(ds_next->ds_dbuf, tx);
297 297 ds_next->ds_phys->ds_prev_snap_obj =
298 298 ds->ds_phys->ds_prev_snap_obj;
299 299 ds_next->ds_phys->ds_prev_snap_txg =
300 300 ds->ds_phys->ds_prev_snap_txg;
301 301 ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
302 302 ds_prev ? ds_prev->ds_phys->ds_creation_txg : 0);
303 303
304 304 if (ds_next->ds_deadlist.dl_oldfmt) {
305 305 process_old_deadlist(ds, ds_prev, ds_next,
306 306 after_branch_point, tx);
307 307 } else {
308 308 /* Adjust prev's unique space. */
309 309 if (ds_prev && !after_branch_point) {
310 310 dsl_deadlist_space_range(&ds_next->ds_deadlist,
311 311 ds_prev->ds_phys->ds_prev_snap_txg,
312 312 ds->ds_phys->ds_prev_snap_txg,
313 313 &used, &comp, &uncomp);
314 314 ds_prev->ds_phys->ds_unique_bytes += used;
315 315 }
316 316
317 317 /* Adjust snapused. */
318 318 dsl_deadlist_space_range(&ds_next->ds_deadlist,
319 319 ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
320 320 &used, &comp, &uncomp);
321 321 dsl_dir_diduse_space(ds->ds_dir, DD_USED_SNAP,
322 322 -used, -comp, -uncomp, tx);
323 323
324 324 /* Move blocks to be freed to pool's free list. */
325 325 dsl_deadlist_move_bpobj(&ds_next->ds_deadlist,
326 326 &dp->dp_free_bpobj, ds->ds_phys->ds_prev_snap_txg,
327 327 tx);
328 328 dsl_dir_diduse_space(tx->tx_pool->dp_free_dir,
329 329 DD_USED_HEAD, used, comp, uncomp, tx);
330 330
331 331 /* Merge our deadlist into next's and free it. */
332 332 dsl_deadlist_merge(&ds_next->ds_deadlist,
333 333 ds->ds_phys->ds_deadlist_obj, tx);
334 334 }
335 335 dsl_deadlist_close(&ds->ds_deadlist);
336 336 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
337 337 dmu_buf_will_dirty(ds->ds_dbuf, tx);
338 338 ds->ds_phys->ds_deadlist_obj = 0;
339 339
340 340 /* Collapse range in clone heads */
341 341 dsl_dataset_remove_clones_key(ds,
342 342 ds->ds_phys->ds_creation_txg, tx);
343 343
344 344 if (dsl_dataset_is_snapshot(ds_next)) {
345 345 dsl_dataset_t *ds_nextnext;
346 346
347 347 /*
348 348 * Update next's unique to include blocks which
349 349 * were previously shared by only this snapshot
350 350 * and it. Those blocks will be born after the
351 351 * prev snap and before this snap, and will have
352 352 * died after the next snap and before the one
353 353 * after that (ie. be on the snap after next's
354 354 * deadlist).
355 355 */
356 356 VERIFY0(dsl_dataset_hold_obj(dp,
357 357 ds_next->ds_phys->ds_next_snap_obj, FTAG, &ds_nextnext));
358 358 dsl_deadlist_space_range(&ds_nextnext->ds_deadlist,
359 359 ds->ds_phys->ds_prev_snap_txg,
360 360 ds->ds_phys->ds_creation_txg,
361 361 &used, &comp, &uncomp);
362 362 ds_next->ds_phys->ds_unique_bytes += used;
363 363 dsl_dataset_rele(ds_nextnext, FTAG);
364 364 ASSERT3P(ds_next->ds_prev, ==, NULL);
365 365
366 366 /* Collapse range in this head. */
367 367 dsl_dataset_t *hds;
368 368 VERIFY0(dsl_dataset_hold_obj(dp,
369 369 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &hds));
370 370 dsl_deadlist_remove_key(&hds->ds_deadlist,
371 371 ds->ds_phys->ds_creation_txg, tx);
372 372 dsl_dataset_rele(hds, FTAG);
373 373
374 374 } else {
375 375 ASSERT3P(ds_next->ds_prev, ==, ds);
376 376 dsl_dataset_rele(ds_next->ds_prev, ds_next);
377 377 ds_next->ds_prev = NULL;
378 378 if (ds_prev) {
379 379 VERIFY0(dsl_dataset_hold_obj(dp,
380 380 ds->ds_phys->ds_prev_snap_obj,
381 381 ds_next, &ds_next->ds_prev));
382 382 }
383 383
384 384 dsl_dataset_recalc_head_uniq(ds_next);
385 385
386 386 /*
387 387 * Reduce the amount of our unconsumed refreservation
388 388 * being charged to our parent by the amount of
389 389 * new unique data we have gained.
390 390 */
391 391 if (old_unique < ds_next->ds_reserved) {
392 392 int64_t mrsdelta;
393 393 uint64_t new_unique =
394 394 ds_next->ds_phys->ds_unique_bytes;
395 395
396 396 ASSERT(old_unique <= new_unique);
397 397 mrsdelta = MIN(new_unique - old_unique,
398 398 ds_next->ds_reserved - old_unique);
399 399 dsl_dir_diduse_space(ds->ds_dir,
400 400 DD_USED_REFRSRV, -mrsdelta, 0, 0, tx);
401 401 }
402 402 }
403 403 dsl_dataset_rele(ds_next, FTAG);
404 404
405 405 /*
406 406 * This must be done after the dsl_traverse(), because it will
407 407 * re-open the objset.
408 408 */
409 409 if (ds->ds_objset) {
410 410 dmu_objset_evict(ds->ds_objset);
411 411 ds->ds_objset = NULL;
412 412 }
413 413
414 414 /* remove from snapshot namespace */
415 415 dsl_dataset_t *ds_head;
416 416 ASSERT(ds->ds_phys->ds_snapnames_zapobj == 0);
417 417 VERIFY0(dsl_dataset_hold_obj(dp,
418 418 ds->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ds_head));
419 419 VERIFY0(dsl_dataset_get_snapname(ds));
420 420 #ifdef ZFS_DEBUG
421 421 {
422 422 uint64_t val;
423 423
424 424 err = dsl_dataset_snap_lookup(ds_head,
425 425 ds->ds_snapname, &val);
426 426 ASSERT0(err);
427 427 ASSERT3U(val, ==, obj);
428 428 }
429 429 #endif
430 430 VERIFY0(dsl_dataset_snap_remove(ds_head, ds->ds_snapname, tx));
431 431 dsl_dataset_rele(ds_head, FTAG);
432 432
433 433 if (ds_prev != NULL)
434 434 dsl_dataset_rele(ds_prev, FTAG);
435 435
436 436 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
437 437
438 438 if (ds->ds_phys->ds_next_clones_obj != 0) {
439 439 uint64_t count;
440 440 ASSERT0(zap_count(mos,
441 441 ds->ds_phys->ds_next_clones_obj, &count) && count == 0);
442 442 VERIFY0(dmu_object_free(mos,
443 443 ds->ds_phys->ds_next_clones_obj, tx));
444 444 }
445 445 if (ds->ds_phys->ds_props_obj != 0)
446 446 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_props_obj, tx));
447 447 if (ds->ds_phys->ds_userrefs_obj != 0)
448 448 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_userrefs_obj, tx));
449 449 dsl_dir_rele(ds->ds_dir, ds);
450 450 ds->ds_dir = NULL;
451 451 VERIFY0(dmu_object_free(mos, obj, tx));
452 452 }
453 453
454 454 static void
455 455 dsl_destroy_snapshot_sync(void *arg, dmu_tx_t *tx)
456 456 {
457 457 dmu_snapshots_destroy_arg_t *dsda = arg;
458 458 dsl_pool_t *dp = dmu_tx_pool(tx);
459 459 nvpair_t *pair;
460 460
461 461 for (pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, NULL);
462 462 pair != NULL;
463 463 pair = nvlist_next_nvpair(dsda->dsda_successful_snaps, pair)) {
464 464 dsl_dataset_t *ds;
465 465
466 466 VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
467 467
468 468 dsl_destroy_snapshot_sync_impl(ds, dsda->dsda_defer, tx);
469 469 dsl_dataset_rele(ds, FTAG);
470 470 }
471 471 }
472 472
473 473 /*
474 474 * The semantics of this function are described in the comment above
475 475 * lzc_destroy_snaps(). To summarize:
476 476 *
477 477 * The snapshots must all be in the same pool.
478 478 *
479 479 * Snapshots that don't exist will be silently ignored (considered to be
480 480 * "already deleted").
481 481 *
482 482 * On success, all snaps will be destroyed and this will return 0.
483 483 * On failure, no snaps will be destroyed, the errlist will be filled in,
484 484 * and this will return an errno.
485 485 */
486 486 int
487 487 dsl_destroy_snapshots_nvl(nvlist_t *snaps, boolean_t defer,
488 488 nvlist_t *errlist)
489 489 {
490 490 dmu_snapshots_destroy_arg_t dsda;
491 491 int error;
492 492 nvpair_t *pair;
493 493
494 494 pair = nvlist_next_nvpair(snaps, NULL);
495 495 if (pair == NULL)
496 496 return (0);
497 497
498 498 dsda.dsda_snaps = snaps;
499 499 dsda.dsda_successful_snaps = fnvlist_alloc();
500 500 dsda.dsda_defer = defer;
501 501 dsda.dsda_errlist = errlist;
502 502
503 503 error = dsl_sync_task(nvpair_name(pair),
504 504 dsl_destroy_snapshot_check, dsl_destroy_snapshot_sync,
505 505 &dsda, 0);
506 506 fnvlist_free(dsda.dsda_successful_snaps);
507 507
508 508 return (error);
509 509 }
510 510
511 511 int
512 512 dsl_destroy_snapshot(const char *name, boolean_t defer)
513 513 {
514 514 int error;
515 515 nvlist_t *nvl = fnvlist_alloc();
516 516 nvlist_t *errlist = fnvlist_alloc();
517 517
518 518 fnvlist_add_boolean(nvl, name);
519 519 error = dsl_destroy_snapshots_nvl(nvl, defer, errlist);
520 520 fnvlist_free(errlist);
521 521 fnvlist_free(nvl);
522 522 return (error);
523 523 }
524 524
525 525 struct killarg {
526 526 dsl_dataset_t *ds;
527 527 dmu_tx_t *tx;
528 528 };
529 529
530 530 /* ARGSUSED */
531 531 static int
532 532 kill_blkptr(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
533 533 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
534 534 {
535 535 struct killarg *ka = arg;
536 536 dmu_tx_t *tx = ka->tx;
537 537
538 538 if (bp == NULL)
539 539 return (0);
540 540
541 541 if (zb->zb_level == ZB_ZIL_LEVEL) {
542 542 ASSERT(zilog != NULL);
543 543 /*
544 544 * It's a block in the intent log. It has no
545 545 * accounting, so just free it.
546 546 */
547 547 dsl_free(ka->tx->tx_pool, ka->tx->tx_txg, bp);
548 548 } else {
549 549 ASSERT(zilog == NULL);
550 550 ASSERT3U(bp->blk_birth, >, ka->ds->ds_phys->ds_prev_snap_txg);
551 551 (void) dsl_dataset_block_kill(ka->ds, bp, tx, B_FALSE);
552 552 }
553 553
554 554 return (0);
555 555 }
556 556
557 557 static void
558 558 old_synchronous_dataset_destroy(dsl_dataset_t *ds, dmu_tx_t *tx)
559 559 {
560 560 struct killarg ka;
561 561
562 562 /*
563 563 * Free everything that we point to (that's born after
564 564 * the previous snapshot, if we are a clone)
565 565 *
566 566 * NB: this should be very quick, because we already
567 567 * freed all the objects in open context.
568 568 */
569 569 ka.ds = ds;
570 570 ka.tx = tx;
571 571 VERIFY0(traverse_dataset(ds,
572 572 ds->ds_phys->ds_prev_snap_txg, TRAVERSE_POST,
573 573 kill_blkptr, &ka));
574 574 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) || ds->ds_phys->ds_unique_bytes == 0);
575 575 }
576 576
577 577 typedef struct dsl_destroy_head_arg {
578 578 const char *ddha_name;
579 579 } dsl_destroy_head_arg_t;
580 580
581 581 int
582 582 dsl_destroy_head_check_impl(dsl_dataset_t *ds, int expected_holds)
583 583 {
584 584 int error;
585 585 uint64_t count;
586 586 objset_t *mos;
587 587
588 588 if (dsl_dataset_is_snapshot(ds))
589 589 return (SET_ERROR(EINVAL));
590 590
591 591 if (refcount_count(&ds->ds_longholds) != expected_holds)
592 592 return (SET_ERROR(EBUSY));
593 593
594 594 mos = ds->ds_dir->dd_pool->dp_meta_objset;
595 595
596 596 /*
597 597 * Can't delete a head dataset if there are snapshots of it.
598 598 * (Except if the only snapshots are from the branch we cloned
599 599 * from.)
600 600 */
601 601 if (ds->ds_prev != NULL &&
602 602 ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object)
603 603 return (SET_ERROR(EBUSY));
604 604
605 605 /*
606 606 * Can't delete if there are children of this fs.
607 607 */
608 608 error = zap_count(mos,
609 609 ds->ds_dir->dd_phys->dd_child_dir_zapobj, &count);
610 610 if (error != 0)
611 611 return (error);
612 612 if (count != 0)
613 613 return (SET_ERROR(EEXIST));
614 614
615 615 if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev) &&
616 616 ds->ds_prev->ds_phys->ds_num_children == 2 &&
617 617 ds->ds_prev->ds_userrefs == 0) {
618 618 /* We need to remove the origin snapshot as well. */
619 619 if (!refcount_is_zero(&ds->ds_prev->ds_longholds))
620 620 return (SET_ERROR(EBUSY));
621 621 }
622 622 return (0);
623 623 }
624 624
625 625 static int
626 626 dsl_destroy_head_check(void *arg, dmu_tx_t *tx)
627 627 {
628 628 dsl_destroy_head_arg_t *ddha = arg;
629 629 dsl_pool_t *dp = dmu_tx_pool(tx);
630 630 dsl_dataset_t *ds;
631 631 int error;
632 632
633 633 error = dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds);
634 634 if (error != 0)
635 635 return (error);
636 636
637 637 error = dsl_destroy_head_check_impl(ds, 0);
638 638 dsl_dataset_rele(ds, FTAG);
639 639 return (error);
640 640 }
641 641
642 642 static void
643 643 dsl_dir_destroy_sync(uint64_t ddobj, dmu_tx_t *tx)
644 644 {
645 645 dsl_dir_t *dd;
646 646 dsl_pool_t *dp = dmu_tx_pool(tx);
647 647 objset_t *mos = dp->dp_meta_objset;
648 648 dd_used_t t;
649 649
650 650 ASSERT(RRW_WRITE_HELD(&dmu_tx_pool(tx)->dp_config_rwlock));
651 651
652 652 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
653 653
654 654 ASSERT0(dd->dd_phys->dd_head_dataset_obj);
655 655
656 656 /*
657 657 * Remove our reservation. The impl() routine avoids setting the
658 658 * actual property, which would require the (already destroyed) ds.
659 659 */
660 660 dsl_dir_set_reservation_sync_impl(dd, 0, tx);
661 661
662 662 ASSERT0(dd->dd_phys->dd_used_bytes);
663 663 ASSERT0(dd->dd_phys->dd_reserved);
664 664 for (t = 0; t < DD_USED_NUM; t++)
665 665 ASSERT0(dd->dd_phys->dd_used_breakdown[t]);
666 666
667 667 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx));
668 668 VERIFY0(zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx));
669 669 VERIFY0(dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx));
670 670 VERIFY0(zap_remove(mos,
671 671 dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx));
672 672
673 673 dsl_dir_rele(dd, FTAG);
674 674 VERIFY0(dmu_object_free(mos, ddobj, tx));
675 675 }
676 676
677 677 void
678 678 dsl_destroy_head_sync_impl(dsl_dataset_t *ds, dmu_tx_t *tx)
679 679 {
680 680 dsl_pool_t *dp = dmu_tx_pool(tx);
681 681 objset_t *mos = dp->dp_meta_objset;
682 682 uint64_t obj, ddobj, prevobj = 0;
683 683 boolean_t rmorigin;
684 684
685 685 ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
686 686 ASSERT(ds->ds_prev == NULL ||
687 687 ds->ds_prev->ds_phys->ds_next_snap_obj != ds->ds_object);
688 688 ASSERT3U(ds->ds_phys->ds_bp.blk_birth, <=, tx->tx_txg);
689 689 ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
690 690
691 691 /* We need to log before removing it from the namespace. */
692 692 spa_history_log_internal_ds(ds, "destroy", tx, "");
693 693
694 694 rmorigin = (dsl_dir_is_clone(ds->ds_dir) &&
695 695 DS_IS_DEFER_DESTROY(ds->ds_prev) &&
696 696 ds->ds_prev->ds_phys->ds_num_children == 2 &&
697 697 ds->ds_prev->ds_userrefs == 0);
698 698
699 699 /* Remove our reservation */
700 700 if (ds->ds_reserved != 0) {
701 701 dsl_dataset_set_refreservation_sync_impl(ds,
702 702 (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED),
703 703 0, tx);
704 704 ASSERT0(ds->ds_reserved);
705 705 }
706 706
707 707 dsl_scan_ds_destroyed(ds, tx);
708 708
709 709 obj = ds->ds_object;
710 710
711 711 if (ds->ds_phys->ds_prev_snap_obj != 0) {
712 712 /* This is a clone */
713 713 ASSERT(ds->ds_prev != NULL);
714 714 ASSERT3U(ds->ds_prev->ds_phys->ds_next_snap_obj, !=, obj);
715 715 ASSERT0(ds->ds_phys->ds_next_snap_obj);
716 716
717 717 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
718 718 if (ds->ds_prev->ds_phys->ds_next_clones_obj != 0) {
719 719 dsl_dataset_remove_from_next_clones(ds->ds_prev,
720 720 obj, tx);
721 721 }
722 722
723 723 ASSERT3U(ds->ds_prev->ds_phys->ds_num_children, >, 1);
724 724 ds->ds_prev->ds_phys->ds_num_children--;
725 725 }
726 726
727 727 zfeature_info_t *async_destroy =
728 728 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY];
729 729 objset_t *os;
730 730
731 731 /*
732 732 * Destroy the deadlist. Unless it's a clone, the
733 733 * deadlist should be empty. (If it's a clone, it's
734 734 * safe to ignore the deadlist contents.)
735 735 */
736 736 dsl_deadlist_close(&ds->ds_deadlist);
737 737 dsl_deadlist_free(mos, ds->ds_phys->ds_deadlist_obj, tx);
738 738 dmu_buf_will_dirty(ds->ds_dbuf, tx);
739 739 ds->ds_phys->ds_deadlist_obj = 0;
740 740
741 741 VERIFY0(dmu_objset_from_ds(ds, &os));
742 742
743 743 if (!spa_feature_is_enabled(dp->dp_spa, async_destroy)) {
744 744 old_synchronous_dataset_destroy(ds, tx);
745 745 } else {
746 746 /*
747 747 * Move the bptree into the pool's list of trees to
748 748 * clean up and update space accounting information.
749 749 */
750 750 uint64_t used, comp, uncomp;
751 751
752 752 zil_destroy_sync(dmu_objset_zil(os), tx);
753 753
754 754 if (!spa_feature_is_active(dp->dp_spa, async_destroy)) {
755 755 dsl_scan_t *scn = dp->dp_scan;
756 756
757 757 spa_feature_incr(dp->dp_spa, async_destroy, tx);
758 758 dp->dp_bptree_obj = bptree_alloc(mos, tx);
759 759 VERIFY0(zap_add(mos,
760 760 DMU_POOL_DIRECTORY_OBJECT,
761 761 DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
762 762 &dp->dp_bptree_obj, tx));
763 763 ASSERT(!scn->scn_async_destroying);
764 764 scn->scn_async_destroying = B_TRUE;
765 765 }
766 766
767 767 used = ds->ds_dir->dd_phys->dd_used_bytes;
768 768 comp = ds->ds_dir->dd_phys->dd_compressed_bytes;
769 769 uncomp = ds->ds_dir->dd_phys->dd_uncompressed_bytes;
770 770
771 771 ASSERT(!DS_UNIQUE_IS_ACCURATE(ds) ||
772 772 ds->ds_phys->ds_unique_bytes == used);
773 773
774 774 bptree_add(mos, dp->dp_bptree_obj,
775 775 &ds->ds_phys->ds_bp, ds->ds_phys->ds_prev_snap_txg,
776 776 used, comp, uncomp, tx);
777 777 dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
778 778 -used, -comp, -uncomp, tx);
779 779 dsl_dir_diduse_space(dp->dp_free_dir, DD_USED_HEAD,
780 780 used, comp, uncomp, tx);
781 781 }
782 782
783 783 if (ds->ds_prev != NULL) {
784 784 if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
785 785 VERIFY0(zap_remove_int(mos,
786 786 ds->ds_prev->ds_dir->dd_phys->dd_clones,
787 787 ds->ds_object, tx));
788 788 }
789 789 prevobj = ds->ds_prev->ds_object;
790 790 dsl_dataset_rele(ds->ds_prev, ds);
791 791 ds->ds_prev = NULL;
792 792 }
793 793
794 794 /*
795 795 * This must be done after the dsl_traverse(), because it will
796 796 * re-open the objset.
797 797 */
798 798 if (ds->ds_objset) {
799 799 dmu_objset_evict(ds->ds_objset);
800 800 ds->ds_objset = NULL;
801 801 }
802 802
803 803 /* Erase the link in the dir */
804 804 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
805 805 ds->ds_dir->dd_phys->dd_head_dataset_obj = 0;
806 806 ddobj = ds->ds_dir->dd_object;
807 807 ASSERT(ds->ds_phys->ds_snapnames_zapobj != 0);
808 808 VERIFY0(zap_destroy(mos, ds->ds_phys->ds_snapnames_zapobj, tx));
809 809
810 810 spa_prop_clear_bootfs(dp->dp_spa, ds->ds_object, tx);
811 811
812 812 ASSERT0(ds->ds_phys->ds_next_clones_obj);
813 813 ASSERT0(ds->ds_phys->ds_props_obj);
814 814 ASSERT0(ds->ds_phys->ds_userrefs_obj);
815 815 dsl_dir_rele(ds->ds_dir, ds);
816 816 ds->ds_dir = NULL;
817 817 VERIFY0(dmu_object_free(mos, obj, tx));
818 818
819 819 dsl_dir_destroy_sync(ddobj, tx);
820 820
821 821 if (rmorigin) {
822 822 dsl_dataset_t *prev;
823 823 VERIFY0(dsl_dataset_hold_obj(dp, prevobj, FTAG, &prev));
824 824 dsl_destroy_snapshot_sync_impl(prev, B_FALSE, tx);
825 825 dsl_dataset_rele(prev, FTAG);
826 826 }
827 827 }
828 828
829 829 static void
830 830 dsl_destroy_head_sync(void *arg, dmu_tx_t *tx)
831 831 {
832 832 dsl_destroy_head_arg_t *ddha = arg;
833 833 dsl_pool_t *dp = dmu_tx_pool(tx);
834 834 dsl_dataset_t *ds;
835 835
836 836 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
837 837 dsl_destroy_head_sync_impl(ds, tx);
838 838 dsl_dataset_rele(ds, FTAG);
839 839 }
840 840
841 841 static void
842 842 dsl_destroy_head_begin_sync(void *arg, dmu_tx_t *tx)
843 843 {
844 844 dsl_destroy_head_arg_t *ddha = arg;
845 845 dsl_pool_t *dp = dmu_tx_pool(tx);
846 846 dsl_dataset_t *ds;
847 847
848 848 VERIFY0(dsl_dataset_hold(dp, ddha->ddha_name, FTAG, &ds));
849 849
850 850 /* Mark it as inconsistent on-disk, in case we crash */
851 851 dmu_buf_will_dirty(ds->ds_dbuf, tx);
852 852 ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
853 853
854 854 spa_history_log_internal_ds(ds, "destroy begin", tx, "");
855 855 dsl_dataset_rele(ds, FTAG);
856 856 }
857 857
858 858 int
859 859 dsl_destroy_head(const char *name)
860 860 {
861 861 dsl_destroy_head_arg_t ddha;
862 862 int error;
863 863 spa_t *spa;
864 864 boolean_t isenabled;
865 865
866 866 #ifdef _KERNEL
867 867 zfs_destroy_unmount_origin(name);
868 868 #endif
869 869
870 870 error = spa_open(name, &spa, FTAG);
871 871 if (error != 0)
872 872 return (error);
873 873 isenabled = spa_feature_is_enabled(spa,
874 874 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
875 875 spa_close(spa, FTAG);
876 876
877 877 ddha.ddha_name = name;
878 878
879 879 if (!isenabled) {
880 880 objset_t *os;
881 881
882 882 error = dsl_sync_task(name, dsl_destroy_head_check,
883 883 dsl_destroy_head_begin_sync, &ddha, 0);
884 884 if (error != 0)
885 885 return (error);
886 886
887 887 /*
888 888 * Head deletion is processed in one txg on old pools;
↓ open down ↓ |
888 lines elided |
↑ open up ↑ |
889 889 * remove the objects from open context so that the txg sync
890 890 * is not too long.
891 891 */
892 892 error = dmu_objset_own(name, DMU_OST_ANY, B_FALSE, FTAG, &os);
893 893 if (error == 0) {
894 894 uint64_t prev_snap_txg =
895 895 dmu_objset_ds(os)->ds_phys->ds_prev_snap_txg;
896 896 for (uint64_t obj = 0; error == 0;
897 897 error = dmu_object_next(os, &obj, FALSE,
898 898 prev_snap_txg))
899 - (void) dmu_free_object(os, obj);
899 + (void) dmu_free_long_object(os, obj);
900 900 /* sync out all frees */
901 901 txg_wait_synced(dmu_objset_pool(os), 0);
902 902 dmu_objset_disown(os, FTAG);
903 903 }
904 904 }
905 905
906 906 return (dsl_sync_task(name, dsl_destroy_head_check,
907 907 dsl_destroy_head_sync, &ddha, 0));
908 908 }
909 909
910 910 /*
911 911 * Note, this function is used as the callback for dmu_objset_find(). We
912 912 * always return 0 so that we will continue to find and process
913 913 * inconsistent datasets, even if we encounter an error trying to
914 914 * process one of them.
915 915 */
916 916 /* ARGSUSED */
917 917 int
918 918 dsl_destroy_inconsistent(const char *dsname, void *arg)
919 919 {
920 920 objset_t *os;
921 921
922 922 if (dmu_objset_hold(dsname, FTAG, &os) == 0) {
923 923 boolean_t inconsistent = DS_IS_INCONSISTENT(dmu_objset_ds(os));
924 924 dmu_objset_rele(os, FTAG);
925 925 if (inconsistent)
926 926 (void) dsl_destroy_head(dsname);
927 927 }
928 928 return (0);
929 929 }
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX