int dm_array_get_value(struct dm_array_info *info, dm_block_t root, uint32_t index, void *value_le) { int r; struct dm_block *block; struct array_block *ab; size_t size_of_block; unsigned entry, max_entries; size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm)); max_entries = calc_max_entries(info->value_type.size, size_of_block); r = lookup_ablock(info, root, index / max_entries, &block, &ab); if (r) return r; entry = index % max_entries; if (entry >= le32_to_cpu(ab->nr_entries)) r = -ENODATA; else memcpy(value_le, element_at(info, ab, entry), info->value_type.size); unlock_ablock(info, block); return r; }
static int array_resize(struct dm_array_info *info, dm_block_t root, uint32_t old_size, uint32_t new_size, const void *value, dm_block_t *new_root) { int r; struct resize resize; if (old_size == new_size) return 0; resize.info = info; resize.root = root; resize.size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm)); resize.max_entries = calc_max_entries(info->value_type.size, resize.size_of_block); resize.old_nr_full_blocks = old_size / resize.max_entries; resize.old_nr_entries_in_last_block = old_size % resize.max_entries; resize.new_nr_full_blocks = new_size / resize.max_entries; resize.new_nr_entries_in_last_block = new_size % resize.max_entries; resize.value = value; r = ((new_size > old_size) ? grow : shrink)(&resize); if (r) return r; *new_root = resize.root; return 0; }
int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root) { int r; struct dm_block *b; struct btree_node *n; size_t block_size; uint32_t max_entries; r = new_block(info, &b); if (r < 0) return r; block_size = dm_bm_block_size(dm_tm_get_bm(info->tm)); max_entries = calc_max_entries(info->value_type.size, block_size); n = dm_block_data(b); memset(n, 0, block_size); n->header.flags = cpu_to_le32(LEAF_NODE); n->header.nr_entries = cpu_to_le32(0); n->header.max_entries = cpu_to_le32(max_entries); n->header.value_size = cpu_to_le32(info->value_type.size); *root = dm_block_location(b); return unlock_block(info, b); }
static void prefetch_children(struct del_stack *s, struct frame *f) { unsigned i; struct dm_block_manager *bm = dm_tm_get_bm(s->tm); for (i = 0; i < f->nr_children; i++) dm_bm_prefetch(bm, value64(f->n, i)); }
static int rebalance_children(struct shadow_spine *s, struct dm_btree_info *info, struct dm_btree_value_type *vt, uint64_t key) { int i, r, has_left_sibling, has_right_sibling; uint32_t child_entries; struct btree_node *n; n = dm_block_data(shadow_current(s)); if (le32_to_cpu(n->header.nr_entries) == 1) { struct dm_block *child; dm_block_t b = value64(n, 0); r = dm_tm_read_lock(info->tm, b, &btree_node_validator, &child); if (r) return r; memcpy(n, dm_block_data(child), dm_bm_block_size(dm_tm_get_bm(info->tm))); r = dm_tm_unlock(info->tm, child); if (r) return r; dm_tm_dec(info->tm, dm_block_location(child)); return 0; } i = lower_bound(n, key); if (i < 0) return -ENODATA; r = get_nr_entries(info->tm, value64(n, i), &child_entries); if (r) return r; has_left_sibling = i > 0; has_right_sibling = i < (le32_to_cpu(n->header.nr_entries) - 1); if (!has_left_sibling) r = rebalance2(s, info, vt, i); else if (!has_right_sibling) r = rebalance2(s, info, vt, i - 1); else r = rebalance3(s, info, vt, i - 1); return r; }
static int array_set_value(struct dm_array_info *info, dm_block_t root, uint32_t index, const void *value, dm_block_t *new_root) { int r; struct dm_block *block; struct array_block *ab; size_t size_of_block; unsigned max_entries; unsigned entry; void *old_value; struct dm_btree_value_type *vt = &info->value_type; size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm)); max_entries = calc_max_entries(info->value_type.size, size_of_block); r = shadow_ablock(info, &root, index / max_entries, &block, &ab); if (r) return r; *new_root = root; entry = index % max_entries; if (entry >= le32_to_cpu(ab->nr_entries)) { r = -ENODATA; goto out; } old_value = element_at(info, ab, entry); if (vt->dec && (!vt->equal || !vt->equal(vt->context, old_value, value))) { vt->dec(vt->context, old_value); if (vt->inc) vt->inc(vt->context, value); } memcpy(old_value, value, info->value_type.size); out: unlock_ablock(info, block); return r; }
/* * Splits a node by creating two new children beneath the given node. * * Before: * +----------+ * | A ++++++ | * +----------+ * * * After: * +------------+ * | A (shadow) | * +------------+ * | | * +------+ +----+ * | | * v v * +-------+ +-------+ * | B +++ | | C +++ | * +-------+ +-------+ */ static int btree_split_beneath(struct shadow_spine *s, uint64_t key) { int r; size_t size; unsigned nr_left, nr_right; struct dm_block *left, *right, *new_parent; struct btree_node *pn, *ln, *rn; __le64 val; new_parent = shadow_current(s); r = new_block(s->info, &left); if (r < 0) return r; r = new_block(s->info, &right); if (r < 0) { /* FIXME: put left */ return r; } pn = dm_block_data(new_parent); ln = dm_block_data(left); rn = dm_block_data(right); nr_left = le32_to_cpu(pn->header.nr_entries) / 2; nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left; ln->header.flags = pn->header.flags; ln->header.nr_entries = cpu_to_le32(nr_left); ln->header.max_entries = pn->header.max_entries; ln->header.value_size = pn->header.value_size; rn->header.flags = pn->header.flags; rn->header.nr_entries = cpu_to_le32(nr_right); rn->header.max_entries = pn->header.max_entries; rn->header.value_size = pn->header.value_size; memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0])); memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0])); size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ? sizeof(__le64) : s->info->value_type.size; memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size); memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left), nr_right * size); /* new_parent should just point to l and r now */ pn->header.flags = cpu_to_le32(INTERNAL_NODE); pn->header.nr_entries = cpu_to_le32(2); pn->header.max_entries = cpu_to_le32( calc_max_entries(sizeof(__le64), dm_bm_block_size( dm_tm_get_bm(s->info->tm)))); pn->header.value_size = cpu_to_le32(sizeof(__le64)); val = cpu_to_le64(dm_block_location(left)); __dm_bless_for_disk(&val); pn->keys[0] = ln->keys[0]; memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64)); val = cpu_to_le64(dm_block_location(right)); __dm_bless_for_disk(&val); pn->keys[1] = rn->keys[0]; memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64)); /* * rejig the spine. This is ugly, since it knows too * much about the spine */ if (s->nodes[0] != new_parent) { unlock_block(s->info, s->nodes[0]); s->nodes[0] = new_parent; } if (key < le64_to_cpu(rn->keys[0])) { unlock_block(s->info, right); s->nodes[1] = left; } else { unlock_block(s->info, left); s->nodes[1] = right; } s->count = 2; return 0; }