* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
+/*
+ * Copyright (c) 2012 by Delphix. All rights reserved.
+ */
#include <sys/zfs_context.h>
#include <sys/spa.h>
#include <sys/zio.h>
#include <sys/space_map.h>
+static kmem_cache_t *space_seg_cache;
+
+void
+space_map_init(void)
+{
+ ASSERT(space_seg_cache == NULL);
+ space_seg_cache = kmem_cache_create("space_seg_cache",
+ sizeof (space_seg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
+}
+
+void
+space_map_fini(void)
+{
+ kmem_cache_destroy(space_seg_cache);
+ space_seg_cache = NULL;
+}
+
/*
* Space map routines.
* NOTE: caller is responsible for all locking.
space_map_destroy(space_map_t *sm)
{
ASSERT(!sm->sm_loaded && !sm->sm_loading);
- VERIFY3U(sm->sm_space, ==, 0);
+ VERIFY0(sm->sm_space);
avl_destroy(&sm->sm_root);
cv_destroy(&sm->sm_load_cv);
}
int merge_before, merge_after;
ASSERT(MUTEX_HELD(sm->sm_lock));
+ VERIFY(!sm->sm_condensing);
VERIFY(size != 0);
VERIFY3U(start, >=, sm->sm_start);
VERIFY3U(end, <=, sm->sm_start + sm->sm_size);
avl_remove(sm->sm_pp_root, ss_after);
}
ss_after->ss_start = ss_before->ss_start;
- kmem_free(ss_before, sizeof (*ss_before));
+ kmem_cache_free(space_seg_cache, ss_before);
ss = ss_after;
} else if (merge_before) {
ss_before->ss_end = end;
avl_remove(sm->sm_pp_root, ss_after);
ss = ss_after;
} else {
- ss = kmem_alloc(sizeof (*ss), KM_PUSHPAGE);
+ ss = kmem_cache_alloc(space_seg_cache, KM_PUSHPAGE);
ss->ss_start = start;
ss->ss_end = end;
avl_insert(&sm->sm_root, ss, where);
int left_over, right_over;
ASSERT(MUTEX_HELD(sm->sm_lock));
+ VERIFY(!sm->sm_condensing);
VERIFY(size != 0);
VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
avl_remove(sm->sm_pp_root, ss);
if (left_over && right_over) {
- newseg = kmem_alloc(sizeof (*newseg), KM_PUSHPAGE);
+ newseg = kmem_cache_alloc(space_seg_cache, KM_PUSHPAGE);
newseg->ss_start = end;
newseg->ss_end = ss->ss_end;
ss->ss_end = start;
ss->ss_start = end;
} else {
avl_remove(&sm->sm_root, ss);
- kmem_free(ss, sizeof (*ss));
+ kmem_cache_free(space_seg_cache, ss);
ss = NULL;
}
}
void
+space_map_swap(space_map_t **msrc, space_map_t **mdst)
+{
+ space_map_t *sm;
+
+ ASSERT(MUTEX_HELD((*msrc)->sm_lock));
+ ASSERT0((*mdst)->sm_space);
+ ASSERT0(avl_numnodes(&(*mdst)->sm_root));
+
+ sm = *msrc;
+ *msrc = *mdst;
+ *mdst = sm;
+}
+
+void
space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
{
space_seg_t *ss;
while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
if (func != NULL)
func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
- kmem_free(ss, sizeof (*ss));
+ kmem_cache_free(space_seg_cache, ss);
}
sm->sm_space = 0;
}
space = smo->smo_alloc;
ASSERT(sm->sm_ops == NULL);
- VERIFY3U(sm->sm_space, ==, 0);
+ VERIFY0(sm->sm_space);
if (maptype == SM_FREE) {
space_map_add(sm, sm->sm_start, sm->sm_size);
space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
{
spa_t *spa = dmu_objset_spa(os);
- void *cookie = NULL;
+ avl_tree_t *t = &sm->sm_root;
space_seg_t *ss;
- uint64_t bufsize, start, size, run_len;
+ uint64_t bufsize, start, size, run_len, total, sm_space, nodes;
uint64_t *entry, *entry_map, *entry_map_end;
ASSERT(MUTEX_HELD(sm->sm_lock));
SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
- while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
+ total = 0;
+ nodes = avl_numnodes(&sm->sm_root);
+ sm_space = sm->sm_space;
+ for (ss = avl_first(t); ss != NULL; ss = AVL_NEXT(t, ss)) {
size = ss->ss_end - ss->ss_start;
start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
- sm->sm_space -= size;
+ total += size;
size >>= sm->sm_shift;
while (size) {
start += run_len;
size -= run_len;
}
- kmem_free(ss, sizeof (*ss));
}
if (entry != entry_map) {
smo->smo_objsize += size;
}
- zio_buf_free(entry_map, bufsize);
+ /*
+ * Ensure that the space_map's accounting wasn't changed
+ * while we were in the middle of writing it out.
+ */
+ VERIFY3U(nodes, ==, avl_numnodes(&sm->sm_root));
+ VERIFY3U(sm->sm_space, ==, sm_space);
+ VERIFY3U(sm->sm_space, ==, total);
- VERIFY3U(sm->sm_space, ==, 0);
+ zio_buf_free(entry_map, bufsize);
}
void