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