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