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