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