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