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