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