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