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