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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2012 by Delphix. All rights reserved.
29 #include <sys/zfs_context.h>
33 #include <sys/space_map.h>
35 SYSCTL_DECL(_vfs_zfs);
36 static int space_map_last_hope;
37 TUNABLE_INT("vfs.zfs.space_map_last_hope", &space_map_last_hope);
38 SYSCTL_INT(_vfs_zfs, OID_AUTO, space_map_last_hope, CTLFLAG_RDTUN,
39 &space_map_last_hope, 0,
40 "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.");
42 static kmem_cache_t *space_seg_cache;
47 ASSERT(space_seg_cache == NULL);
48 space_seg_cache = kmem_cache_create("space_seg_cache",
49 sizeof (space_seg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
55 kmem_cache_destroy(space_seg_cache);
56 space_seg_cache = NULL;
61 * NOTE: caller is responsible for all locking.
64 space_map_seg_compare(const void *x1, const void *x2)
66 const space_seg_t *s1 = x1;
67 const space_seg_t *s2 = x2;
69 if (s1->ss_start < s2->ss_start) {
70 if (s1->ss_end > s2->ss_start)
74 if (s1->ss_start > s2->ss_start) {
75 if (s1->ss_start < s2->ss_end)
83 space_map_create(space_map_t *sm, uint64_t start, uint64_t size, uint8_t shift,
86 bzero(sm, sizeof (*sm));
88 cv_init(&sm->sm_load_cv, NULL, CV_DEFAULT, NULL);
90 avl_create(&sm->sm_root, space_map_seg_compare,
91 sizeof (space_seg_t), offsetof(struct space_seg, ss_node));
100 space_map_destroy(space_map_t *sm)
102 ASSERT(!sm->sm_loaded && !sm->sm_loading);
103 VERIFY0(sm->sm_space);
104 avl_destroy(&sm->sm_root);
105 cv_destroy(&sm->sm_load_cv);
109 space_map_add(space_map_t *sm, uint64_t start, uint64_t size)
112 space_seg_t ssearch, *ss_before, *ss_after, *ss;
113 uint64_t end = start + size;
114 int merge_before, merge_after;
116 ASSERT(MUTEX_HELD(sm->sm_lock));
118 VERIFY3U(start, >=, sm->sm_start);
119 VERIFY3U(end, <=, sm->sm_start + sm->sm_size);
120 VERIFY(sm->sm_space + size <= sm->sm_size);
121 VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
122 VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
124 ssearch.ss_start = start;
125 ssearch.ss_end = end;
126 ss = avl_find(&sm->sm_root, &ssearch, &where);
128 if (ss != NULL && ss->ss_start <= start && ss->ss_end >= end) {
129 zfs_panic_recover("zfs: allocating allocated segment"
130 "(offset=%llu size=%llu)\n",
131 (longlong_t)start, (longlong_t)size);
134 if (ss != NULL && space_map_last_hope) {
135 uint64_t sstart, ssize;
137 if (ss->ss_start > start)
138 sstart = ss->ss_start;
141 if (ss->ss_end > end)
142 ssize = end - sstart;
144 ssize = ss->ss_end - sstart;
146 "Removing colliding space_map range (start=%ju end=%ju). Good luck!",
147 (uintmax_t)sstart, (uintmax_t)(sstart + ssize));
148 space_map_remove(sm, sstart, ssize);
152 /* Make sure we don't overlap with either of our neighbors */
155 ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
156 ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
158 merge_before = (ss_before != NULL && ss_before->ss_end == start);
159 merge_after = (ss_after != NULL && ss_after->ss_start == end);
161 if (merge_before && merge_after) {
162 avl_remove(&sm->sm_root, ss_before);
163 if (sm->sm_pp_root) {
164 avl_remove(sm->sm_pp_root, ss_before);
165 avl_remove(sm->sm_pp_root, ss_after);
167 ss_after->ss_start = ss_before->ss_start;
168 kmem_cache_free(space_seg_cache, ss_before);
170 } else if (merge_before) {
171 ss_before->ss_end = end;
173 avl_remove(sm->sm_pp_root, ss_before);
175 } else if (merge_after) {
176 ss_after->ss_start = start;
178 avl_remove(sm->sm_pp_root, ss_after);
181 ss = kmem_cache_alloc(space_seg_cache, KM_SLEEP);
182 ss->ss_start = start;
184 avl_insert(&sm->sm_root, ss, where);
188 avl_add(sm->sm_pp_root, ss);
190 sm->sm_space += size;
194 space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
196 space_seg_t ssearch, *ss, *newseg;
197 uint64_t end = start + size;
198 int left_over, right_over;
200 ASSERT(MUTEX_HELD(sm->sm_lock));
202 VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
203 VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
205 ssearch.ss_start = start;
206 ssearch.ss_end = end;
207 ss = avl_find(&sm->sm_root, &ssearch, NULL);
209 /* Make sure we completely overlap with someone */
211 zfs_panic_recover("zfs: freeing free segment "
212 "(offset=%llu size=%llu)",
213 (longlong_t)start, (longlong_t)size);
216 VERIFY3U(ss->ss_start, <=, start);
217 VERIFY3U(ss->ss_end, >=, end);
218 VERIFY(sm->sm_space - size < sm->sm_size);
220 left_over = (ss->ss_start != start);
221 right_over = (ss->ss_end != end);
224 avl_remove(sm->sm_pp_root, ss);
226 if (left_over && right_over) {
227 newseg = kmem_cache_alloc(space_seg_cache, KM_SLEEP);
228 newseg->ss_start = end;
229 newseg->ss_end = ss->ss_end;
231 avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
233 avl_add(sm->sm_pp_root, newseg);
234 } else if (left_over) {
236 } else if (right_over) {
239 avl_remove(&sm->sm_root, ss);
240 kmem_cache_free(space_seg_cache, ss);
244 if (sm->sm_pp_root && ss != NULL)
245 avl_add(sm->sm_pp_root, ss);
247 sm->sm_space -= size;
251 space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
254 space_seg_t ssearch, *ss;
255 uint64_t end = start + size;
257 ASSERT(MUTEX_HELD(sm->sm_lock));
259 VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
260 VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
262 ssearch.ss_start = start;
263 ssearch.ss_end = end;
264 ss = avl_find(&sm->sm_root, &ssearch, &where);
266 return (ss != NULL && ss->ss_start <= start && ss->ss_end >= end);
270 space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
275 ASSERT(MUTEX_HELD(sm->sm_lock));
277 while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
279 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
280 kmem_cache_free(space_seg_cache, ss);
286 space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
290 ASSERT(MUTEX_HELD(sm->sm_lock));
292 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
293 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
297 * Wait for any in-progress space_map_load() to complete.
300 space_map_load_wait(space_map_t *sm)
302 ASSERT(MUTEX_HELD(sm->sm_lock));
304 while (sm->sm_loading) {
305 ASSERT(!sm->sm_loaded);
306 cv_wait(&sm->sm_load_cv, sm->sm_lock);
311 * Note: space_map_load() will drop sm_lock across dmu_read() calls.
312 * The caller must be OK with this.
315 space_map_load(space_map_t *sm, space_map_ops_t *ops, uint8_t maptype,
316 space_map_obj_t *smo, objset_t *os)
318 uint64_t *entry, *entry_map, *entry_map_end;
319 uint64_t bufsize, size, offset, end, space;
320 uint64_t mapstart = sm->sm_start;
323 ASSERT(MUTEX_HELD(sm->sm_lock));
324 ASSERT(!sm->sm_loaded);
325 ASSERT(!sm->sm_loading);
327 sm->sm_loading = B_TRUE;
328 end = smo->smo_objsize;
329 space = smo->smo_alloc;
331 ASSERT(sm->sm_ops == NULL);
332 VERIFY0(sm->sm_space);
334 if (maptype == SM_FREE) {
335 space_map_add(sm, sm->sm_start, sm->sm_size);
336 space = sm->sm_size - space;
339 bufsize = 1ULL << SPACE_MAP_BLOCKSHIFT;
340 entry_map = zio_buf_alloc(bufsize);
342 mutex_exit(sm->sm_lock);
344 dmu_prefetch(os, smo->smo_object, bufsize, end - bufsize);
345 mutex_enter(sm->sm_lock);
347 for (offset = 0; offset < end; offset += bufsize) {
348 size = MIN(end - offset, bufsize);
349 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
352 dprintf("object=%llu offset=%llx size=%llx\n",
353 smo->smo_object, offset, size);
355 mutex_exit(sm->sm_lock);
356 error = dmu_read(os, smo->smo_object, offset, size, entry_map,
358 mutex_enter(sm->sm_lock);
362 entry_map_end = entry_map + (size / sizeof (uint64_t));
363 for (entry = entry_map; entry < entry_map_end; entry++) {
366 if (SM_DEBUG_DECODE(e)) /* Skip debug entries */
369 (SM_TYPE_DECODE(e) == maptype ?
370 space_map_add : space_map_remove)(sm,
371 (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
372 SM_RUN_DECODE(e) << sm->sm_shift);
377 VERIFY3U(sm->sm_space, ==, space);
379 sm->sm_loaded = B_TRUE;
384 space_map_vacate(sm, NULL, NULL);
387 zio_buf_free(entry_map, bufsize);
389 sm->sm_loading = B_FALSE;
391 cv_broadcast(&sm->sm_load_cv);
397 space_map_unload(space_map_t *sm)
399 ASSERT(MUTEX_HELD(sm->sm_lock));
401 if (sm->sm_loaded && sm->sm_ops != NULL)
402 sm->sm_ops->smop_unload(sm);
404 sm->sm_loaded = B_FALSE;
407 space_map_vacate(sm, NULL, NULL);
411 space_map_maxsize(space_map_t *sm)
413 ASSERT(sm->sm_ops != NULL);
414 return (sm->sm_ops->smop_max(sm));
418 space_map_alloc(space_map_t *sm, uint64_t size)
422 start = sm->sm_ops->smop_alloc(sm, size);
424 space_map_remove(sm, start, size);
429 space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
431 sm->sm_ops->smop_claim(sm, start, size);
432 space_map_remove(sm, start, size);
436 space_map_free(space_map_t *sm, uint64_t start, uint64_t size)
438 space_map_add(sm, start, size);
439 sm->sm_ops->smop_free(sm, start, size);
443 * Note: space_map_sync() will drop sm_lock across dmu_write() calls.
446 space_map_sync(space_map_t *sm, uint8_t maptype,
447 space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
449 spa_t *spa = dmu_objset_spa(os);
452 uint64_t bufsize, start, size, run_len, delta, sm_space;
453 uint64_t *entry, *entry_map, *entry_map_end;
455 ASSERT(MUTEX_HELD(sm->sm_lock));
457 if (sm->sm_space == 0)
460 dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
461 smo->smo_object, dmu_tx_get_txg(tx), spa_sync_pass(spa),
462 maptype == SM_ALLOC ? 'A' : 'F', avl_numnodes(&sm->sm_root),
465 if (maptype == SM_ALLOC)
466 smo->smo_alloc += sm->sm_space;
468 smo->smo_alloc -= sm->sm_space;
470 bufsize = (8 + avl_numnodes(&sm->sm_root)) * sizeof (uint64_t);
471 bufsize = MIN(bufsize, 1ULL << SPACE_MAP_BLOCKSHIFT);
472 entry_map = zio_buf_alloc(bufsize);
473 entry_map_end = entry_map + (bufsize / sizeof (uint64_t));
476 *entry++ = SM_DEBUG_ENCODE(1) |
477 SM_DEBUG_ACTION_ENCODE(maptype) |
478 SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
479 SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
482 sm_space = sm->sm_space;
483 while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
484 size = ss->ss_end - ss->ss_start;
485 start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
488 size >>= sm->sm_shift;
491 run_len = MIN(size, SM_RUN_MAX);
493 if (entry == entry_map_end) {
494 mutex_exit(sm->sm_lock);
495 dmu_write(os, smo->smo_object, smo->smo_objsize,
496 bufsize, entry_map, tx);
497 mutex_enter(sm->sm_lock);
498 smo->smo_objsize += bufsize;
502 *entry++ = SM_OFFSET_ENCODE(start) |
503 SM_TYPE_ENCODE(maptype) |
504 SM_RUN_ENCODE(run_len);
509 kmem_cache_free(space_seg_cache, ss);
512 if (entry != entry_map) {
513 size = (entry - entry_map) * sizeof (uint64_t);
514 mutex_exit(sm->sm_lock);
515 dmu_write(os, smo->smo_object, smo->smo_objsize,
516 size, entry_map, tx);
517 mutex_enter(sm->sm_lock);
518 smo->smo_objsize += size;
522 * Ensure that the space_map's accounting wasn't changed
523 * while we were in the middle of writing it out.
525 VERIFY3U(sm->sm_space, ==, sm_space);
527 zio_buf_free(entry_map, bufsize);
529 sm->sm_space -= delta;
530 VERIFY0(sm->sm_space);
534 space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
536 VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
538 smo->smo_objsize = 0;
543 * Space map reference trees.
545 * A space map is a collection of integers. Every integer is either
546 * in the map, or it's not. A space map reference tree generalizes
547 * the idea: it allows its members to have arbitrary reference counts,
548 * as opposed to the implicit reference count of 0 or 1 in a space map.
549 * This representation comes in handy when computing the union or
550 * intersection of multiple space maps. For example, the union of
551 * N space maps is the subset of the reference tree with refcnt >= 1.
552 * The intersection of N space maps is the subset with refcnt >= N.
554 * [It's very much like a Fourier transform. Unions and intersections
555 * are hard to perform in the 'space map domain', so we convert the maps
556 * into the 'reference count domain', where it's trivial, then invert.]
558 * vdev_dtl_reassess() uses computations of this form to determine
559 * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
560 * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
561 * has an outage wherever refcnt >= vdev_children.
564 space_map_ref_compare(const void *x1, const void *x2)
566 const space_ref_t *sr1 = x1;
567 const space_ref_t *sr2 = x2;
569 if (sr1->sr_offset < sr2->sr_offset)
571 if (sr1->sr_offset > sr2->sr_offset)
583 space_map_ref_create(avl_tree_t *t)
585 avl_create(t, space_map_ref_compare,
586 sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
590 space_map_ref_destroy(avl_tree_t *t)
595 while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
596 kmem_free(sr, sizeof (*sr));
602 space_map_ref_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
606 sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
607 sr->sr_offset = offset;
608 sr->sr_refcnt = refcnt;
614 space_map_ref_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
617 space_map_ref_add_node(t, start, refcnt);
618 space_map_ref_add_node(t, end, -refcnt);
622 * Convert (or add) a space map into a reference tree.
625 space_map_ref_add_map(avl_tree_t *t, space_map_t *sm, int64_t refcnt)
629 ASSERT(MUTEX_HELD(sm->sm_lock));
631 for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
632 space_map_ref_add_seg(t, ss->ss_start, ss->ss_end, refcnt);
636 * Convert a reference tree into a space map. The space map will contain
637 * all members of the reference tree for which refcnt >= minref.
640 space_map_ref_generate_map(avl_tree_t *t, space_map_t *sm, int64_t minref)
642 uint64_t start = -1ULL;
646 ASSERT(MUTEX_HELD(sm->sm_lock));
648 space_map_vacate(sm, NULL, NULL);
650 for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
651 refcnt += sr->sr_refcnt;
652 if (refcnt >= minref) {
653 if (start == -1ULL) {
654 start = sr->sr_offset;
657 if (start != -1ULL) {
658 uint64_t end = sr->sr_offset;
659 ASSERT(start <= end);
661 space_map_add(sm, start, end - start);
667 ASSERT(start == -1ULL);