]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_deadlist.c
Move ziltest.sh to the ZTS framework
[mirror_zfs.git] / module / zfs / dsl_deadlist.c
CommitLineData
428870ff
BB
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) 2010, Oracle and/or its affiliates. All rights reserved.
753c3839 23 * Copyright (c) 2012 by Delphix. All rights reserved.
0c66c32d 24 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
428870ff
BB
25 */
26
27#include <sys/dsl_dataset.h>
28#include <sys/dmu.h>
29#include <sys/refcount.h>
30#include <sys/zap.h>
31#include <sys/zfs_context.h>
32#include <sys/dsl_pool.h>
33
330d06f9
MA
34/*
35 * Deadlist concurrency:
36 *
37 * Deadlists can only be modified from the syncing thread.
38 *
39 * Except for dsl_deadlist_insert(), it can only be modified with the
40 * dp_config_rwlock held with RW_WRITER.
41 *
42 * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can
43 * be called concurrently, from open context, with the dl_config_rwlock held
44 * with RW_READER.
45 *
46 * Therefore, we only need to provide locking between dsl_deadlist_insert() and
47 * the accessors, protecting:
48 * dl_phys->dl_used,comp,uncomp
49 * and protecting the dl_tree from being loaded.
50 * The locking is provided by dl_lock. Note that locking on the bpobj_t
51 * provides its own locking, and dl_oldfmt is immutable.
52 */
53
428870ff
BB
54static int
55dsl_deadlist_compare(const void *arg1, const void *arg2)
56{
ee36c709
GN
57 const dsl_deadlist_entry_t *dle1 = (const dsl_deadlist_entry_t *)arg1;
58 const dsl_deadlist_entry_t *dle2 = (const dsl_deadlist_entry_t *)arg2;
428870ff 59
ee36c709 60 return (AVL_CMP(dle1->dle_mintxg, dle2->dle_mintxg));
428870ff
BB
61}
62
63static void
64dsl_deadlist_load_tree(dsl_deadlist_t *dl)
65{
66 zap_cursor_t zc;
67 zap_attribute_t za;
68
69 ASSERT(!dl->dl_oldfmt);
70 if (dl->dl_havetree)
71 return;
72
73 avl_create(&dl->dl_tree, dsl_deadlist_compare,
74 sizeof (dsl_deadlist_entry_t),
75 offsetof(dsl_deadlist_entry_t, dle_node));
76 for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
77 zap_cursor_retrieve(&zc, &za) == 0;
78 zap_cursor_advance(&zc)) {
b8d06fca
RY
79 dsl_deadlist_entry_t *dle;
80
79c76d5b 81 dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
428870ff
BB
82 dle->dle_mintxg = strtonum(za.za_name, NULL);
83 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os,
84 za.za_first_integer));
85 avl_add(&dl->dl_tree, dle);
86 }
87 zap_cursor_fini(&zc);
88 dl->dl_havetree = B_TRUE;
89}
90
91void
92dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
93{
94 dmu_object_info_t doi;
95
96 mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
97 dl->dl_os = os;
98 dl->dl_object = object;
99 VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf));
100 dmu_object_info_from_db(dl->dl_dbuf, &doi);
101 if (doi.doi_type == DMU_OT_BPOBJ) {
102 dmu_buf_rele(dl->dl_dbuf, dl);
103 dl->dl_dbuf = NULL;
104 dl->dl_oldfmt = B_TRUE;
105 VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object));
106 return;
107 }
108
109 dl->dl_oldfmt = B_FALSE;
110 dl->dl_phys = dl->dl_dbuf->db_data;
111 dl->dl_havetree = B_FALSE;
112}
113
114void
115dsl_deadlist_close(dsl_deadlist_t *dl)
116{
117 void *cookie = NULL;
118 dsl_deadlist_entry_t *dle;
119
0c66c32d
JG
120 dl->dl_os = NULL;
121
428870ff
BB
122 if (dl->dl_oldfmt) {
123 dl->dl_oldfmt = B_FALSE;
124 bpobj_close(&dl->dl_bpobj);
125 return;
126 }
127
128 if (dl->dl_havetree) {
129 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
130 != NULL) {
131 bpobj_close(&dle->dle_bpobj);
132 kmem_free(dle, sizeof (*dle));
133 }
134 avl_destroy(&dl->dl_tree);
135 }
136 dmu_buf_rele(dl->dl_dbuf, dl);
137 mutex_destroy(&dl->dl_lock);
138 dl->dl_dbuf = NULL;
139 dl->dl_phys = NULL;
140}
141
142uint64_t
143dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
144{
145 if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
f1512ee6 146 return (bpobj_alloc(os, SPA_OLD_MAXBLOCKSIZE, tx));
428870ff
BB
147 return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR,
148 sizeof (dsl_deadlist_phys_t), tx));
149}
150
151void
152dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
153{
154 dmu_object_info_t doi;
155 zap_cursor_t zc;
156 zap_attribute_t za;
157
158 VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi));
159 if (doi.doi_type == DMU_OT_BPOBJ) {
160 bpobj_free(os, dlobj, tx);
161 return;
162 }
163
164 for (zap_cursor_init(&zc, os, dlobj);
165 zap_cursor_retrieve(&zc, &za) == 0;
753c3839
MA
166 zap_cursor_advance(&zc)) {
167 uint64_t obj = za.za_first_integer;
168 if (obj == dmu_objset_pool(os)->dp_empty_bpobj)
169 bpobj_decr_empty(os, tx);
170 else
171 bpobj_free(os, obj, tx);
172 }
428870ff
BB
173 zap_cursor_fini(&zc);
174 VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx));
175}
176
753c3839
MA
177static void
178dle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
179 const blkptr_t *bp, dmu_tx_t *tx)
180{
181 if (dle->dle_bpobj.bpo_object ==
182 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
f1512ee6 183 uint64_t obj = bpobj_alloc(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
753c3839
MA
184 bpobj_close(&dle->dle_bpobj);
185 bpobj_decr_empty(dl->dl_os, tx);
186 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
187 VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
188 dle->dle_mintxg, obj, tx));
189 }
190 bpobj_enqueue(&dle->dle_bpobj, bp, tx);
191}
192
193static void
194dle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
195 uint64_t obj, dmu_tx_t *tx)
196{
197 if (dle->dle_bpobj.bpo_object !=
198 dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
199 bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
200 } else {
201 bpobj_close(&dle->dle_bpobj);
202 bpobj_decr_empty(dl->dl_os, tx);
203 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
204 VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
205 dle->dle_mintxg, obj, tx));
206 }
207}
208
428870ff
BB
209void
210dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx)
211{
212 dsl_deadlist_entry_t dle_tofind;
213 dsl_deadlist_entry_t *dle;
214 avl_index_t where;
215
216 if (dl->dl_oldfmt) {
217 bpobj_enqueue(&dl->dl_bpobj, bp, tx);
218 return;
219 }
220
221 dsl_deadlist_load_tree(dl);
222
223 dmu_buf_will_dirty(dl->dl_dbuf, tx);
224 mutex_enter(&dl->dl_lock);
225 dl->dl_phys->dl_used +=
226 bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
227 dl->dl_phys->dl_comp += BP_GET_PSIZE(bp);
228 dl->dl_phys->dl_uncomp += BP_GET_UCSIZE(bp);
229 mutex_exit(&dl->dl_lock);
230
231 dle_tofind.dle_mintxg = bp->blk_birth;
232 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
233 if (dle == NULL)
234 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
235 else
236 dle = AVL_PREV(&dl->dl_tree, dle);
eba9e745
BB
237
238 if (dle == NULL) {
239 zfs_panic_recover("blkptr at %p has invalid BLK_BIRTH %llu",
240 bp, (longlong_t)bp->blk_birth);
241 dle = avl_first(&dl->dl_tree);
242 }
243
244 ASSERT3P(dle, !=, NULL);
753c3839 245 dle_enqueue(dl, dle, bp, tx);
428870ff
BB
246}
247
248/*
249 * Insert new key in deadlist, which must be > all current entries.
250 * mintxg is not inclusive.
251 */
252void
253dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
254{
255 uint64_t obj;
256 dsl_deadlist_entry_t *dle;
257
258 if (dl->dl_oldfmt)
259 return;
260
261 dsl_deadlist_load_tree(dl);
262
79c76d5b 263 dle = kmem_alloc(sizeof (*dle), KM_SLEEP);
428870ff 264 dle->dle_mintxg = mintxg;
f1512ee6 265 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
428870ff
BB
266 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
267 avl_add(&dl->dl_tree, dle);
268
269 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object,
270 mintxg, obj, tx));
271}
272
273/*
274 * Remove this key, merging its entries into the previous key.
275 */
276void
277dsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
278{
279 dsl_deadlist_entry_t dle_tofind;
280 dsl_deadlist_entry_t *dle, *dle_prev;
281
282 if (dl->dl_oldfmt)
283 return;
284
285 dsl_deadlist_load_tree(dl);
286
287 dle_tofind.dle_mintxg = mintxg;
288 dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
289 dle_prev = AVL_PREV(&dl->dl_tree, dle);
290
753c3839 291 dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx);
428870ff
BB
292
293 avl_remove(&dl->dl_tree, dle);
294 bpobj_close(&dle->dle_bpobj);
295 kmem_free(dle, sizeof (*dle));
296
297 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
298}
299
300/*
301 * Walk ds's snapshots to regenerate generate ZAP & AVL.
302 */
303static void
304dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
305 uint64_t mrs_obj, dmu_tx_t *tx)
306{
307 dsl_deadlist_t dl;
308 dsl_pool_t *dp = dmu_objset_pool(os);
309
310 dsl_deadlist_open(&dl, os, dlobj);
311 if (dl.dl_oldfmt) {
312 dsl_deadlist_close(&dl);
313 return;
314 }
315
316 while (mrs_obj != 0) {
317 dsl_dataset_t *ds;
318 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
d683ddbb
JG
319 dsl_deadlist_add_key(&dl,
320 dsl_dataset_phys(ds)->ds_prev_snap_txg, tx);
321 mrs_obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
428870ff
BB
322 dsl_dataset_rele(ds, FTAG);
323 }
324 dsl_deadlist_close(&dl);
325}
326
327uint64_t
328dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
329 uint64_t mrs_obj, dmu_tx_t *tx)
330{
331 dsl_deadlist_entry_t *dle;
332 uint64_t newobj;
333
334 newobj = dsl_deadlist_alloc(dl->dl_os, tx);
335
336 if (dl->dl_oldfmt) {
337 dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
338 return (newobj);
339 }
340
341 dsl_deadlist_load_tree(dl);
342
343 for (dle = avl_first(&dl->dl_tree); dle;
344 dle = AVL_NEXT(&dl->dl_tree, dle)) {
345 uint64_t obj;
346
347 if (dle->dle_mintxg >= maxtxg)
348 break;
349
f1512ee6 350 obj = bpobj_alloc_empty(dl->dl_os, SPA_OLD_MAXBLOCKSIZE, tx);
428870ff
BB
351 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, newobj,
352 dle->dle_mintxg, obj, tx));
353 }
354 return (newobj);
355}
356
357void
358dsl_deadlist_space(dsl_deadlist_t *dl,
359 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
360{
361 if (dl->dl_oldfmt) {
362 VERIFY3U(0, ==, bpobj_space(&dl->dl_bpobj,
363 usedp, compp, uncompp));
364 return;
365 }
366
367 mutex_enter(&dl->dl_lock);
368 *usedp = dl->dl_phys->dl_used;
369 *compp = dl->dl_phys->dl_comp;
370 *uncompp = dl->dl_phys->dl_uncomp;
371 mutex_exit(&dl->dl_lock);
372}
373
374/*
375 * return space used in the range (mintxg, maxtxg].
376 * Includes maxtxg, does not include mintxg.
377 * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
330d06f9 378 * larger than any bp in the deadlist (eg. UINT64_MAX)).
428870ff
BB
379 */
380void
381dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg,
382 uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
383{
428870ff 384 dsl_deadlist_entry_t *dle;
330d06f9 385 dsl_deadlist_entry_t dle_tofind;
428870ff
BB
386 avl_index_t where;
387
388 if (dl->dl_oldfmt) {
389 VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj,
390 mintxg, maxtxg, usedp, compp, uncompp));
391 return;
392 }
393
428870ff
BB
394 *usedp = *compp = *uncompp = 0;
395
330d06f9
MA
396 mutex_enter(&dl->dl_lock);
397 dsl_deadlist_load_tree(dl);
428870ff
BB
398 dle_tofind.dle_mintxg = mintxg;
399 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
400 /*
401 * If we don't find this mintxg, there shouldn't be anything
402 * after it either.
403 */
404 ASSERT(dle != NULL ||
405 avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL);
330d06f9 406
428870ff
BB
407 for (; dle && dle->dle_mintxg < maxtxg;
408 dle = AVL_NEXT(&dl->dl_tree, dle)) {
409 uint64_t used, comp, uncomp;
410
411 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
412 &used, &comp, &uncomp));
413
414 *usedp += used;
415 *compp += comp;
416 *uncompp += uncomp;
417 }
330d06f9 418 mutex_exit(&dl->dl_lock);
428870ff
BB
419}
420
421static void
422dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
423 dmu_tx_t *tx)
424{
425 dsl_deadlist_entry_t dle_tofind;
426 dsl_deadlist_entry_t *dle;
427 avl_index_t where;
428 uint64_t used, comp, uncomp;
429 bpobj_t bpo;
430
431 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
432 VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp));
433 bpobj_close(&bpo);
434
435 dsl_deadlist_load_tree(dl);
436
437 dmu_buf_will_dirty(dl->dl_dbuf, tx);
438 mutex_enter(&dl->dl_lock);
439 dl->dl_phys->dl_used += used;
440 dl->dl_phys->dl_comp += comp;
441 dl->dl_phys->dl_uncomp += uncomp;
442 mutex_exit(&dl->dl_lock);
443
444 dle_tofind.dle_mintxg = birth;
445 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
446 if (dle == NULL)
447 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
753c3839 448 dle_enqueue_subobj(dl, dle, obj, tx);
428870ff
BB
449}
450
451static int
452dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
453{
454 dsl_deadlist_t *dl = arg;
455 dsl_deadlist_insert(dl, bp, tx);
456 return (0);
457}
458
459/*
460 * Merge the deadlist pointed to by 'obj' into dl. obj will be left as
461 * an empty deadlist.
462 */
463void
464dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
465{
466 zap_cursor_t zc;
467 zap_attribute_t za;
468 dmu_buf_t *bonus;
469 dsl_deadlist_phys_t *dlp;
470 dmu_object_info_t doi;
471
472 VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi));
473 if (doi.doi_type == DMU_OT_BPOBJ) {
474 bpobj_t bpo;
475 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
476 VERIFY3U(0, ==, bpobj_iterate(&bpo,
477 dsl_deadlist_insert_cb, dl, tx));
478 bpobj_close(&bpo);
479 return;
480 }
481
482 for (zap_cursor_init(&zc, dl->dl_os, obj);
483 zap_cursor_retrieve(&zc, &za) == 0;
484 zap_cursor_advance(&zc)) {
485 uint64_t mintxg = strtonum(za.za_name, NULL);
486 dsl_deadlist_insert_bpobj(dl, za.za_first_integer, mintxg, tx);
487 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, obj, mintxg, tx));
488 }
489 zap_cursor_fini(&zc);
490
491 VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
492 dlp = bonus->db_data;
493 dmu_buf_will_dirty(bonus, tx);
494 bzero(dlp, sizeof (*dlp));
495 dmu_buf_rele(bonus, FTAG);
496}
497
498/*
499 * Remove entries on dl that are >= mintxg, and put them on the bpobj.
500 */
501void
502dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
503 dmu_tx_t *tx)
504{
505 dsl_deadlist_entry_t dle_tofind;
506 dsl_deadlist_entry_t *dle;
507 avl_index_t where;
508
509 ASSERT(!dl->dl_oldfmt);
510 dmu_buf_will_dirty(dl->dl_dbuf, tx);
511 dsl_deadlist_load_tree(dl);
512
513 dle_tofind.dle_mintxg = mintxg;
514 dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
515 if (dle == NULL)
516 dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
517 while (dle) {
518 uint64_t used, comp, uncomp;
519 dsl_deadlist_entry_t *dle_next;
520
521 bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
522
523 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
524 &used, &comp, &uncomp));
525 mutex_enter(&dl->dl_lock);
526 ASSERT3U(dl->dl_phys->dl_used, >=, used);
527 ASSERT3U(dl->dl_phys->dl_comp, >=, comp);
528 ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);
529 dl->dl_phys->dl_used -= used;
530 dl->dl_phys->dl_comp -= comp;
531 dl->dl_phys->dl_uncomp -= uncomp;
532 mutex_exit(&dl->dl_lock);
533
534 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object,
535 dle->dle_mintxg, tx));
536
537 dle_next = AVL_NEXT(&dl->dl_tree, dle);
538 avl_remove(&dl->dl_tree, dle);
539 bpobj_close(&dle->dle_bpobj);
540 kmem_free(dle, sizeof (*dle));
541 dle = dle_next;
542 }
543}