]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/space_map.c
Heavy DNSSEC Validation Load Can Cause a "Bad Cache" Assertion Failure
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / space_map.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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/dmu.h>
29 #include <sys/zio.h>
30 #include <sys/space_map.h>
31
32 SYSCTL_DECL(_vfs_zfs);
33 static int space_map_last_hope;
34 TUNABLE_INT("vfs.zfs.space_map_last_hope", &space_map_last_hope);
35 SYSCTL_INT(_vfs_zfs, OID_AUTO, space_map_last_hope, CTLFLAG_RDTUN,
36     &space_map_last_hope, 0,
37     "If kernel panic in space_map code on pool import, import the pool in readonly mode and backup all your data before trying this option.");
38
39 /*
40  * Space map routines.
41  * NOTE: caller is responsible for all locking.
42  */
43 static int
44 space_map_seg_compare(const void *x1, const void *x2)
45 {
46         const space_seg_t *s1 = x1;
47         const space_seg_t *s2 = x2;
48
49         if (s1->ss_start < s2->ss_start) {
50                 if (s1->ss_end > s2->ss_start)
51                         return (0);
52                 return (-1);
53         }
54         if (s1->ss_start > s2->ss_start) {
55                 if (s1->ss_start < s2->ss_end)
56                         return (0);
57                 return (1);
58         }
59         return (0);
60 }
61
62 void
63 space_map_create(space_map_t *sm, uint64_t start, uint64_t size, uint8_t shift,
64         kmutex_t *lp)
65 {
66         bzero(sm, sizeof (*sm));
67
68         cv_init(&sm->sm_load_cv, NULL, CV_DEFAULT, NULL);
69
70         avl_create(&sm->sm_root, space_map_seg_compare,
71             sizeof (space_seg_t), offsetof(struct space_seg, ss_node));
72
73         sm->sm_start = start;
74         sm->sm_size = size;
75         sm->sm_shift = shift;
76         sm->sm_lock = lp;
77 }
78
79 void
80 space_map_destroy(space_map_t *sm)
81 {
82         ASSERT(!sm->sm_loaded && !sm->sm_loading);
83         VERIFY3U(sm->sm_space, ==, 0);
84         avl_destroy(&sm->sm_root);
85         cv_destroy(&sm->sm_load_cv);
86 }
87
88 void
89 space_map_add(space_map_t *sm, uint64_t start, uint64_t size)
90 {
91         avl_index_t where;
92         space_seg_t ssearch, *ss_before, *ss_after, *ss;
93         uint64_t end = start + size;
94         int merge_before, merge_after;
95
96         ASSERT(MUTEX_HELD(sm->sm_lock));
97         VERIFY(size != 0);
98         VERIFY3U(start, >=, sm->sm_start);
99         VERIFY3U(end, <=, sm->sm_start + sm->sm_size);
100         VERIFY(sm->sm_space + size <= sm->sm_size);
101         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
102         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
103 again:
104         ssearch.ss_start = start;
105         ssearch.ss_end = end;
106         ss = avl_find(&sm->sm_root, &ssearch, &where);
107
108         if (ss != NULL && ss->ss_start <= start && ss->ss_end >= end) {
109                 zfs_panic_recover("zfs: allocating allocated segment"
110                     "(offset=%llu size=%llu)\n",
111                     (longlong_t)start, (longlong_t)size);
112                 return;
113         }
114         if (ss != NULL && space_map_last_hope) {
115                 uint64_t sstart, ssize;
116
117                 if (ss->ss_start > start)
118                         sstart = ss->ss_start;
119                 else
120                         sstart = start;
121                 if (ss->ss_end > end)
122                         ssize = end - sstart;
123                 else
124                         ssize = ss->ss_end - sstart;
125                 ZFS_LOG(0,
126                     "Removing colliding space_map range (start=%ju end=%ju). Good luck!",
127                     (uintmax_t)sstart, (uintmax_t)(sstart + ssize));
128                 space_map_remove(sm, sstart, ssize);
129                 goto again;
130         }
131
132         /* Make sure we don't overlap with either of our neighbors */
133         VERIFY(ss == NULL);
134
135         ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
136         ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
137
138         merge_before = (ss_before != NULL && ss_before->ss_end == start);
139         merge_after = (ss_after != NULL && ss_after->ss_start == end);
140
141         if (merge_before && merge_after) {
142                 avl_remove(&sm->sm_root, ss_before);
143                 if (sm->sm_pp_root) {
144                         avl_remove(sm->sm_pp_root, ss_before);
145                         avl_remove(sm->sm_pp_root, ss_after);
146                 }
147                 ss_after->ss_start = ss_before->ss_start;
148                 kmem_free(ss_before, sizeof (*ss_before));
149                 ss = ss_after;
150         } else if (merge_before) {
151                 ss_before->ss_end = end;
152                 if (sm->sm_pp_root)
153                         avl_remove(sm->sm_pp_root, ss_before);
154                 ss = ss_before;
155         } else if (merge_after) {
156                 ss_after->ss_start = start;
157                 if (sm->sm_pp_root)
158                         avl_remove(sm->sm_pp_root, ss_after);
159                 ss = ss_after;
160         } else {
161                 ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
162                 ss->ss_start = start;
163                 ss->ss_end = end;
164                 avl_insert(&sm->sm_root, ss, where);
165         }
166
167         if (sm->sm_pp_root)
168                 avl_add(sm->sm_pp_root, ss);
169
170         sm->sm_space += size;
171 }
172
173 void
174 space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
175 {
176         space_seg_t ssearch, *ss, *newseg;
177         uint64_t end = start + size;
178         int left_over, right_over;
179
180         ASSERT(MUTEX_HELD(sm->sm_lock));
181         VERIFY(size != 0);
182         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
183         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
184
185         ssearch.ss_start = start;
186         ssearch.ss_end = end;
187         ss = avl_find(&sm->sm_root, &ssearch, NULL);
188
189         /* Make sure we completely overlap with someone */
190         if (ss == NULL) {
191                 zfs_panic_recover("zfs: freeing free segment "
192                     "(offset=%llu size=%llu)",
193                     (longlong_t)start, (longlong_t)size);
194                 return;
195         }
196         VERIFY3U(ss->ss_start, <=, start);
197         VERIFY3U(ss->ss_end, >=, end);
198         VERIFY(sm->sm_space - size < sm->sm_size);
199
200         left_over = (ss->ss_start != start);
201         right_over = (ss->ss_end != end);
202
203         if (sm->sm_pp_root)
204                 avl_remove(sm->sm_pp_root, ss);
205
206         if (left_over && right_over) {
207                 newseg = kmem_alloc(sizeof (*newseg), KM_SLEEP);
208                 newseg->ss_start = end;
209                 newseg->ss_end = ss->ss_end;
210                 ss->ss_end = start;
211                 avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
212                 if (sm->sm_pp_root)
213                         avl_add(sm->sm_pp_root, newseg);
214         } else if (left_over) {
215                 ss->ss_end = start;
216         } else if (right_over) {
217                 ss->ss_start = end;
218         } else {
219                 avl_remove(&sm->sm_root, ss);
220                 kmem_free(ss, sizeof (*ss));
221                 ss = NULL;
222         }
223
224         if (sm->sm_pp_root && ss != NULL)
225                 avl_add(sm->sm_pp_root, ss);
226
227         sm->sm_space -= size;
228 }
229
230 boolean_t
231 space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
232 {
233         avl_index_t where;
234         space_seg_t ssearch, *ss;
235         uint64_t end = start + size;
236
237         ASSERT(MUTEX_HELD(sm->sm_lock));
238         VERIFY(size != 0);
239         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
240         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
241
242         ssearch.ss_start = start;
243         ssearch.ss_end = end;
244         ss = avl_find(&sm->sm_root, &ssearch, &where);
245
246         return (ss != NULL && ss->ss_start <= start && ss->ss_end >= end);
247 }
248
249 void
250 space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
251 {
252         space_seg_t *ss;
253         void *cookie = NULL;
254
255         ASSERT(MUTEX_HELD(sm->sm_lock));
256
257         while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
258                 if (func != NULL)
259                         func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
260                 kmem_free(ss, sizeof (*ss));
261         }
262         sm->sm_space = 0;
263 }
264
265 void
266 space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
267 {
268         space_seg_t *ss;
269
270         ASSERT(MUTEX_HELD(sm->sm_lock));
271
272         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
273                 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
274 }
275
276 /*
277  * Wait for any in-progress space_map_load() to complete.
278  */
279 void
280 space_map_load_wait(space_map_t *sm)
281 {
282         ASSERT(MUTEX_HELD(sm->sm_lock));
283
284         while (sm->sm_loading) {
285                 ASSERT(!sm->sm_loaded);
286                 cv_wait(&sm->sm_load_cv, sm->sm_lock);
287         }
288 }
289
290 /*
291  * Note: space_map_load() will drop sm_lock across dmu_read() calls.
292  * The caller must be OK with this.
293  */
294 int
295 space_map_load(space_map_t *sm, space_map_ops_t *ops, uint8_t maptype,
296         space_map_obj_t *smo, objset_t *os)
297 {
298         uint64_t *entry, *entry_map, *entry_map_end;
299         uint64_t bufsize, size, offset, end, space;
300         uint64_t mapstart = sm->sm_start;
301         int error = 0;
302
303         ASSERT(MUTEX_HELD(sm->sm_lock));
304         ASSERT(!sm->sm_loaded);
305         ASSERT(!sm->sm_loading);
306
307         sm->sm_loading = B_TRUE;
308         end = smo->smo_objsize;
309         space = smo->smo_alloc;
310
311         ASSERT(sm->sm_ops == NULL);
312         VERIFY3U(sm->sm_space, ==, 0);
313
314         if (maptype == SM_FREE) {
315                 space_map_add(sm, sm->sm_start, sm->sm_size);
316                 space = sm->sm_size - space;
317         }
318
319         bufsize = 1ULL << SPACE_MAP_BLOCKSHIFT;
320         entry_map = zio_buf_alloc(bufsize);
321
322         mutex_exit(sm->sm_lock);
323         if (end > bufsize)
324                 dmu_prefetch(os, smo->smo_object, bufsize, end - bufsize);
325         mutex_enter(sm->sm_lock);
326
327         for (offset = 0; offset < end; offset += bufsize) {
328                 size = MIN(end - offset, bufsize);
329                 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
330                 VERIFY(size != 0);
331
332                 dprintf("object=%llu  offset=%llx  size=%llx\n",
333                     smo->smo_object, offset, size);
334
335                 mutex_exit(sm->sm_lock);
336                 error = dmu_read(os, smo->smo_object, offset, size, entry_map,
337                     DMU_READ_PREFETCH);
338                 mutex_enter(sm->sm_lock);
339                 if (error != 0)
340                         break;
341
342                 entry_map_end = entry_map + (size / sizeof (uint64_t));
343                 for (entry = entry_map; entry < entry_map_end; entry++) {
344                         uint64_t e = *entry;
345
346                         if (SM_DEBUG_DECODE(e))         /* Skip debug entries */
347                                 continue;
348
349                         (SM_TYPE_DECODE(e) == maptype ?
350                             space_map_add : space_map_remove)(sm,
351                             (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
352                             SM_RUN_DECODE(e) << sm->sm_shift);
353                 }
354         }
355
356         if (error == 0) {
357                 VERIFY3U(sm->sm_space, ==, space);
358
359                 sm->sm_loaded = B_TRUE;
360                 sm->sm_ops = ops;
361                 if (ops != NULL)
362                         ops->smop_load(sm);
363         } else {
364                 space_map_vacate(sm, NULL, NULL);
365         }
366
367         zio_buf_free(entry_map, bufsize);
368
369         sm->sm_loading = B_FALSE;
370
371         cv_broadcast(&sm->sm_load_cv);
372
373         return (error);
374 }
375
376 void
377 space_map_unload(space_map_t *sm)
378 {
379         ASSERT(MUTEX_HELD(sm->sm_lock));
380
381         if (sm->sm_loaded && sm->sm_ops != NULL)
382                 sm->sm_ops->smop_unload(sm);
383
384         sm->sm_loaded = B_FALSE;
385         sm->sm_ops = NULL;
386
387         space_map_vacate(sm, NULL, NULL);
388 }
389
390 uint64_t
391 space_map_maxsize(space_map_t *sm)
392 {
393         ASSERT(sm->sm_ops != NULL);
394         return (sm->sm_ops->smop_max(sm));
395 }
396
397 uint64_t
398 space_map_alloc(space_map_t *sm, uint64_t size)
399 {
400         uint64_t start;
401
402         start = sm->sm_ops->smop_alloc(sm, size);
403         if (start != -1ULL)
404                 space_map_remove(sm, start, size);
405         return (start);
406 }
407
408 void
409 space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
410 {
411         sm->sm_ops->smop_claim(sm, start, size);
412         space_map_remove(sm, start, size);
413 }
414
415 void
416 space_map_free(space_map_t *sm, uint64_t start, uint64_t size)
417 {
418         space_map_add(sm, start, size);
419         sm->sm_ops->smop_free(sm, start, size);
420 }
421
422 /*
423  * Note: space_map_sync() will drop sm_lock across dmu_write() calls.
424  */
425 void
426 space_map_sync(space_map_t *sm, uint8_t maptype,
427         space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
428 {
429         spa_t *spa = dmu_objset_spa(os);
430         void *cookie = NULL;
431         space_seg_t *ss;
432         uint64_t bufsize, start, size, run_len;
433         uint64_t *entry, *entry_map, *entry_map_end;
434
435         ASSERT(MUTEX_HELD(sm->sm_lock));
436
437         if (sm->sm_space == 0)
438                 return;
439
440         dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
441             smo->smo_object, dmu_tx_get_txg(tx), spa_sync_pass(spa),
442             maptype == SM_ALLOC ? 'A' : 'F', avl_numnodes(&sm->sm_root),
443             sm->sm_space);
444
445         if (maptype == SM_ALLOC)
446                 smo->smo_alloc += sm->sm_space;
447         else
448                 smo->smo_alloc -= sm->sm_space;
449
450         bufsize = (8 + avl_numnodes(&sm->sm_root)) * sizeof (uint64_t);
451         bufsize = MIN(bufsize, 1ULL << SPACE_MAP_BLOCKSHIFT);
452         entry_map = zio_buf_alloc(bufsize);
453         entry_map_end = entry_map + (bufsize / sizeof (uint64_t));
454         entry = entry_map;
455
456         *entry++ = SM_DEBUG_ENCODE(1) |
457             SM_DEBUG_ACTION_ENCODE(maptype) |
458             SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
459             SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
460
461         while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
462                 size = ss->ss_end - ss->ss_start;
463                 start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
464
465                 sm->sm_space -= size;
466                 size >>= sm->sm_shift;
467
468                 while (size) {
469                         run_len = MIN(size, SM_RUN_MAX);
470
471                         if (entry == entry_map_end) {
472                                 mutex_exit(sm->sm_lock);
473                                 dmu_write(os, smo->smo_object, smo->smo_objsize,
474                                     bufsize, entry_map, tx);
475                                 mutex_enter(sm->sm_lock);
476                                 smo->smo_objsize += bufsize;
477                                 entry = entry_map;
478                         }
479
480                         *entry++ = SM_OFFSET_ENCODE(start) |
481                             SM_TYPE_ENCODE(maptype) |
482                             SM_RUN_ENCODE(run_len);
483
484                         start += run_len;
485                         size -= run_len;
486                 }
487                 kmem_free(ss, sizeof (*ss));
488         }
489
490         if (entry != entry_map) {
491                 size = (entry - entry_map) * sizeof (uint64_t);
492                 mutex_exit(sm->sm_lock);
493                 dmu_write(os, smo->smo_object, smo->smo_objsize,
494                     size, entry_map, tx);
495                 mutex_enter(sm->sm_lock);
496                 smo->smo_objsize += size;
497         }
498
499         zio_buf_free(entry_map, bufsize);
500
501         VERIFY3U(sm->sm_space, ==, 0);
502 }
503
504 void
505 space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
506 {
507         VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
508
509         smo->smo_objsize = 0;
510         smo->smo_alloc = 0;
511 }
512
513 /*
514  * Space map reference trees.
515  *
516  * A space map is a collection of integers.  Every integer is either
517  * in the map, or it's not.  A space map reference tree generalizes
518  * the idea: it allows its members to have arbitrary reference counts,
519  * as opposed to the implicit reference count of 0 or 1 in a space map.
520  * This representation comes in handy when computing the union or
521  * intersection of multiple space maps.  For example, the union of
522  * N space maps is the subset of the reference tree with refcnt >= 1.
523  * The intersection of N space maps is the subset with refcnt >= N.
524  *
525  * [It's very much like a Fourier transform.  Unions and intersections
526  * are hard to perform in the 'space map domain', so we convert the maps
527  * into the 'reference count domain', where it's trivial, then invert.]
528  *
529  * vdev_dtl_reassess() uses computations of this form to determine
530  * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
531  * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
532  * has an outage wherever refcnt >= vdev_children.
533  */
534 static int
535 space_map_ref_compare(const void *x1, const void *x2)
536 {
537         const space_ref_t *sr1 = x1;
538         const space_ref_t *sr2 = x2;
539
540         if (sr1->sr_offset < sr2->sr_offset)
541                 return (-1);
542         if (sr1->sr_offset > sr2->sr_offset)
543                 return (1);
544
545         if (sr1 < sr2)
546                 return (-1);
547         if (sr1 > sr2)
548                 return (1);
549
550         return (0);
551 }
552
553 void
554 space_map_ref_create(avl_tree_t *t)
555 {
556         avl_create(t, space_map_ref_compare,
557             sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
558 }
559
560 void
561 space_map_ref_destroy(avl_tree_t *t)
562 {
563         space_ref_t *sr;
564         void *cookie = NULL;
565
566         while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
567                 kmem_free(sr, sizeof (*sr));
568
569         avl_destroy(t);
570 }
571
572 static void
573 space_map_ref_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
574 {
575         space_ref_t *sr;
576
577         sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
578         sr->sr_offset = offset;
579         sr->sr_refcnt = refcnt;
580
581         avl_add(t, sr);
582 }
583
584 void
585 space_map_ref_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
586         int64_t refcnt)
587 {
588         space_map_ref_add_node(t, start, refcnt);
589         space_map_ref_add_node(t, end, -refcnt);
590 }
591
592 /*
593  * Convert (or add) a space map into a reference tree.
594  */
595 void
596 space_map_ref_add_map(avl_tree_t *t, space_map_t *sm, int64_t refcnt)
597 {
598         space_seg_t *ss;
599
600         ASSERT(MUTEX_HELD(sm->sm_lock));
601
602         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
603                 space_map_ref_add_seg(t, ss->ss_start, ss->ss_end, refcnt);
604 }
605
606 /*
607  * Convert a reference tree into a space map.  The space map will contain
608  * all members of the reference tree for which refcnt >= minref.
609  */
610 void
611 space_map_ref_generate_map(avl_tree_t *t, space_map_t *sm, int64_t minref)
612 {
613         uint64_t start = -1ULL;
614         int64_t refcnt = 0;
615         space_ref_t *sr;
616
617         ASSERT(MUTEX_HELD(sm->sm_lock));
618
619         space_map_vacate(sm, NULL, NULL);
620
621         for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
622                 refcnt += sr->sr_refcnt;
623                 if (refcnt >= minref) {
624                         if (start == -1ULL) {
625                                 start = sr->sr_offset;
626                         }
627                 } else {
628                         if (start != -1ULL) {
629                                 uint64_t end = sr->sr_offset;
630                                 ASSERT(start <= end);
631                                 if (end > start)
632                                         space_map_add(sm, start, end - start);
633                                 start = -1ULL;
634                         }
635                 }
636         }
637         ASSERT(refcnt == 0);
638         ASSERT(start == -1ULL);
639 }