1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
5 * This file is released under the GPL.
9 #include "dm-space-map.h"
10 #include "dm-transaction-manager.h"
12 #include <linux/export.h>
13 #include <linux/device-mapper.h>
15 #define DM_MSG_PREFIX "array"
17 /*----------------------------------------------------------------*/
20 * The array is implemented as a fully populated btree, which points to
21 * blocks that contain the packed values. This is more space efficient
22 * than just using a btree since we don't store 1 key per value.
29 __le64 blocknr; /* Block this node is supposed to live in. */
32 /*----------------------------------------------------------------*/
35 * Validator methods. As usual we calculate a checksum, and also write the
36 * block location into the header (paranoia about ssds remapping areas by
39 #define CSUM_XOR 595846735
41 static void array_block_prepare_for_write(struct dm_block_validator *v,
45 struct array_block *bh_le = dm_block_data(b);
47 bh_le->blocknr = cpu_to_le64(dm_block_location(b));
48 bh_le->csum = cpu_to_le32(dm_bm_checksum(&bh_le->max_entries,
49 size_of_block - sizeof(__le32),
53 static int array_block_check(struct dm_block_validator *v,
57 struct array_block *bh_le = dm_block_data(b);
60 if (dm_block_location(b) != le64_to_cpu(bh_le->blocknr)) {
61 DMERR_LIMIT("array_block_check failed: blocknr %llu != wanted %llu",
62 (unsigned long long) le64_to_cpu(bh_le->blocknr),
63 (unsigned long long) dm_block_location(b));
67 csum_disk = cpu_to_le32(dm_bm_checksum(&bh_le->max_entries,
68 size_of_block - sizeof(__le32),
70 if (csum_disk != bh_le->csum) {
71 DMERR_LIMIT("array_block_check failed: csum %u != wanted %u",
72 (unsigned) le32_to_cpu(csum_disk),
73 (unsigned) le32_to_cpu(bh_le->csum));
80 static struct dm_block_validator array_validator = {
82 .prepare_for_write = array_block_prepare_for_write,
83 .check = array_block_check
86 /*----------------------------------------------------------------*/
89 * Functions for manipulating the array blocks.
93 * Returns a pointer to a value within an array block.
95 * index - The index into _this_ specific block.
97 static void *element_at(struct dm_array_info *info, struct array_block *ab,
100 unsigned char *entry = (unsigned char *) (ab + 1);
102 entry += index * info->value_type.size;
108 * Utility function that calls one of the value_type methods on every value
111 static void on_entries(struct dm_array_info *info, struct array_block *ab,
112 void (*fn)(void *, const void *, unsigned))
114 unsigned nr_entries = le32_to_cpu(ab->nr_entries);
115 fn(info->value_type.context, element_at(info, ab, 0), nr_entries);
119 * Increment every value in an array block.
121 static void inc_ablock_entries(struct dm_array_info *info, struct array_block *ab)
123 struct dm_btree_value_type *vt = &info->value_type;
126 on_entries(info, ab, vt->inc);
130 * Decrement every value in an array block.
132 static void dec_ablock_entries(struct dm_array_info *info, struct array_block *ab)
134 struct dm_btree_value_type *vt = &info->value_type;
137 on_entries(info, ab, vt->dec);
141 * Each array block can hold this many values.
143 static uint32_t calc_max_entries(size_t value_size, size_t size_of_block)
145 return (size_of_block - sizeof(struct array_block)) / value_size;
149 * Allocate a new array block. The caller will need to unlock block.
151 static int alloc_ablock(struct dm_array_info *info, size_t size_of_block,
152 uint32_t max_entries,
153 struct dm_block **block, struct array_block **ab)
157 r = dm_tm_new_block(info->btree_info.tm, &array_validator, block);
161 (*ab) = dm_block_data(*block);
162 (*ab)->max_entries = cpu_to_le32(max_entries);
163 (*ab)->nr_entries = cpu_to_le32(0);
164 (*ab)->value_size = cpu_to_le32(info->value_type.size);
170 * Pad an array block out with a particular value. Every instance will
171 * cause an increment of the value_type. new_nr must always be more than
172 * the current number of entries.
174 static void fill_ablock(struct dm_array_info *info, struct array_block *ab,
175 const void *value, unsigned new_nr)
177 uint32_t nr_entries, delta, i;
178 struct dm_btree_value_type *vt = &info->value_type;
180 BUG_ON(new_nr > le32_to_cpu(ab->max_entries));
181 BUG_ON(new_nr < le32_to_cpu(ab->nr_entries));
183 nr_entries = le32_to_cpu(ab->nr_entries);
184 delta = new_nr - nr_entries;
186 vt->inc(vt->context, value, delta);
187 for (i = nr_entries; i < new_nr; i++)
188 memcpy(element_at(info, ab, i), value, vt->size);
189 ab->nr_entries = cpu_to_le32(new_nr);
193 * Remove some entries from the back of an array block. Every value
194 * removed will be decremented. new_nr must be <= the current number of
197 static void trim_ablock(struct dm_array_info *info, struct array_block *ab,
200 uint32_t nr_entries, delta;
201 struct dm_btree_value_type *vt = &info->value_type;
203 BUG_ON(new_nr > le32_to_cpu(ab->max_entries));
204 BUG_ON(new_nr > le32_to_cpu(ab->nr_entries));
206 nr_entries = le32_to_cpu(ab->nr_entries);
207 delta = nr_entries - new_nr;
209 vt->dec(vt->context, element_at(info, ab, new_nr - 1), delta);
210 ab->nr_entries = cpu_to_le32(new_nr);
214 * Read locks a block, and coerces it to an array block. The caller must
215 * unlock 'block' when finished.
217 static int get_ablock(struct dm_array_info *info, dm_block_t b,
218 struct dm_block **block, struct array_block **ab)
222 r = dm_tm_read_lock(info->btree_info.tm, b, &array_validator, block);
226 *ab = dm_block_data(*block);
231 * Unlocks an array block.
233 static void unlock_ablock(struct dm_array_info *info, struct dm_block *block)
235 dm_tm_unlock(info->btree_info.tm, block);
238 /*----------------------------------------------------------------*/
241 * Btree manipulation.
245 * Looks up an array block in the btree, and then read locks it.
247 * index is the index of the index of the array_block, (ie. the array index
250 static int lookup_ablock(struct dm_array_info *info, dm_block_t root,
251 unsigned index, struct dm_block **block,
252 struct array_block **ab)
255 uint64_t key = index;
258 r = dm_btree_lookup(&info->btree_info, root, &key, &block_le);
262 return get_ablock(info, le64_to_cpu(block_le), block, ab);
266 * Insert an array block into the btree. The block is _not_ unlocked.
268 static int insert_ablock(struct dm_array_info *info, uint64_t index,
269 struct dm_block *block, dm_block_t *root)
271 __le64 block_le = cpu_to_le64(dm_block_location(block));
273 __dm_bless_for_disk(block_le);
274 return dm_btree_insert(&info->btree_info, *root, &index, &block_le, root);
277 /*----------------------------------------------------------------*/
279 static int __shadow_ablock(struct dm_array_info *info, dm_block_t b,
280 struct dm_block **block, struct array_block **ab)
283 int r = dm_tm_shadow_block(info->btree_info.tm, b,
284 &array_validator, block, &inc);
288 *ab = dm_block_data(*block);
290 inc_ablock_entries(info, *ab);
296 * The shadow op will often be a noop. Only insert if it really
299 static int __reinsert_ablock(struct dm_array_info *info, unsigned index,
300 struct dm_block *block, dm_block_t b,
305 if (dm_block_location(block) != b) {
307 * dm_tm_shadow_block will have already decremented the old
308 * block, but it is still referenced by the btree. We
309 * increment to stop the insert decrementing it below zero
310 * when overwriting the old value.
312 dm_tm_inc(info->btree_info.tm, b);
313 r = insert_ablock(info, index, block, root);
320 * Looks up an array block in the btree. Then shadows it, and updates the
321 * btree to point to this new shadow. 'root' is an input/output parameter
322 * for both the current root block, and the new one.
324 static int shadow_ablock(struct dm_array_info *info, dm_block_t *root,
325 unsigned index, struct dm_block **block,
326 struct array_block **ab)
329 uint64_t key = index;
333 r = dm_btree_lookup(&info->btree_info, *root, &key, &block_le);
336 b = le64_to_cpu(block_le);
338 r = __shadow_ablock(info, b, block, ab);
342 return __reinsert_ablock(info, index, *block, b, root);
346 * Allocate an new array block, and fill it with some values.
348 static int insert_new_ablock(struct dm_array_info *info, size_t size_of_block,
349 uint32_t max_entries,
350 unsigned block_index, uint32_t nr,
351 const void *value, dm_block_t *root)
354 struct dm_block *block;
355 struct array_block *ab;
357 r = alloc_ablock(info, size_of_block, max_entries, &block, &ab);
361 fill_ablock(info, ab, value, nr);
362 r = insert_ablock(info, block_index, block, root);
363 unlock_ablock(info, block);
368 static int insert_full_ablocks(struct dm_array_info *info, size_t size_of_block,
369 unsigned begin_block, unsigned end_block,
370 unsigned max_entries, const void *value,
375 for (; !r && begin_block != end_block; begin_block++)
376 r = insert_new_ablock(info, size_of_block, max_entries, begin_block, max_entries, value, root);
382 * There are a bunch of functions involved with resizing an array. This
383 * structure holds information that commonly needed by them. Purely here
384 * to reduce parameter count.
388 * Describes the array.
390 struct dm_array_info *info;
393 * The current root of the array. This gets updated.
398 * Metadata block size. Used to calculate the nr entries in an
401 size_t size_of_block;
404 * Maximum nr entries in an array block.
406 unsigned max_entries;
409 * nr of completely full blocks in the array.
411 * 'old' refers to before the resize, 'new' after.
413 unsigned old_nr_full_blocks, new_nr_full_blocks;
416 * Number of entries in the final block. 0 iff only full blocks in
419 unsigned old_nr_entries_in_last_block, new_nr_entries_in_last_block;
422 * The default value used when growing the array.
428 * Removes a consecutive set of array blocks from the btree. The values
429 * in block are decremented as a side effect of the btree remove.
431 * begin_index - the index of the first array block to remove.
432 * end_index - the one-past-the-end value. ie. this block is not removed.
434 static int drop_blocks(struct resize *resize, unsigned begin_index,
439 while (begin_index != end_index) {
440 uint64_t key = begin_index++;
441 r = dm_btree_remove(&resize->info->btree_info, resize->root,
442 &key, &resize->root);
451 * Calculates how many blocks are needed for the array.
453 static unsigned total_nr_blocks_needed(unsigned nr_full_blocks,
454 unsigned nr_entries_in_last_block)
456 return nr_full_blocks + (nr_entries_in_last_block ? 1 : 0);
462 static int shrink(struct resize *resize)
466 struct dm_block *block;
467 struct array_block *ab;
470 * Lose some blocks from the back?
472 if (resize->new_nr_full_blocks < resize->old_nr_full_blocks) {
473 begin = total_nr_blocks_needed(resize->new_nr_full_blocks,
474 resize->new_nr_entries_in_last_block);
475 end = total_nr_blocks_needed(resize->old_nr_full_blocks,
476 resize->old_nr_entries_in_last_block);
478 r = drop_blocks(resize, begin, end);
484 * Trim the new tail block
486 if (resize->new_nr_entries_in_last_block) {
487 r = shadow_ablock(resize->info, &resize->root,
488 resize->new_nr_full_blocks, &block, &ab);
492 trim_ablock(resize->info, ab, resize->new_nr_entries_in_last_block);
493 unlock_ablock(resize->info, block);
502 static int grow_extend_tail_block(struct resize *resize, uint32_t new_nr_entries)
505 struct dm_block *block;
506 struct array_block *ab;
508 r = shadow_ablock(resize->info, &resize->root,
509 resize->old_nr_full_blocks, &block, &ab);
513 fill_ablock(resize->info, ab, resize->value, new_nr_entries);
514 unlock_ablock(resize->info, block);
519 static int grow_add_tail_block(struct resize *resize)
521 return insert_new_ablock(resize->info, resize->size_of_block,
523 resize->new_nr_full_blocks,
524 resize->new_nr_entries_in_last_block,
525 resize->value, &resize->root);
528 static int grow_needs_more_blocks(struct resize *resize)
531 unsigned old_nr_blocks = resize->old_nr_full_blocks;
533 if (resize->old_nr_entries_in_last_block > 0) {
536 r = grow_extend_tail_block(resize, resize->max_entries);
541 r = insert_full_ablocks(resize->info, resize->size_of_block,
543 resize->new_nr_full_blocks,
544 resize->max_entries, resize->value,
549 if (resize->new_nr_entries_in_last_block)
550 r = grow_add_tail_block(resize);
555 static int grow(struct resize *resize)
557 if (resize->new_nr_full_blocks > resize->old_nr_full_blocks)
558 return grow_needs_more_blocks(resize);
560 else if (resize->old_nr_entries_in_last_block)
561 return grow_extend_tail_block(resize, resize->new_nr_entries_in_last_block);
564 return grow_add_tail_block(resize);
567 /*----------------------------------------------------------------*/
570 * These are the value_type functions for the btree elements, which point
573 static void block_inc(void *context, const void *value, unsigned count)
575 const __le64 *block_le = value;
576 struct dm_array_info *info = context;
579 for (i = 0; i < count; i++, block_le++)
580 dm_tm_inc(info->btree_info.tm, le64_to_cpu(*block_le));
583 static void __block_dec(void *context, const void *value)
589 struct dm_block *block;
590 struct array_block *ab;
591 struct dm_array_info *info = context;
593 memcpy(&block_le, value, sizeof(block_le));
594 b = le64_to_cpu(block_le);
596 r = dm_tm_ref(info->btree_info.tm, b, &ref_count);
598 DMERR_LIMIT("couldn't get reference count for block %llu",
599 (unsigned long long) b);
603 if (ref_count == 1) {
605 * We're about to drop the last reference to this ablock.
606 * So we need to decrement the ref count of the contents.
608 r = get_ablock(info, b, &block, &ab);
610 DMERR_LIMIT("couldn't get array block %llu",
611 (unsigned long long) b);
615 dec_ablock_entries(info, ab);
616 unlock_ablock(info, block);
619 dm_tm_dec(info->btree_info.tm, b);
622 static void block_dec(void *context, const void *value, unsigned count)
625 for (i = 0; i < count; i++, value += sizeof(__le64))
626 __block_dec(context, value);
629 static int block_equal(void *context, const void *value1, const void *value2)
631 return !memcmp(value1, value2, sizeof(__le64));
634 /*----------------------------------------------------------------*/
636 void dm_array_info_init(struct dm_array_info *info,
637 struct dm_transaction_manager *tm,
638 struct dm_btree_value_type *vt)
640 struct dm_btree_value_type *bvt = &info->btree_info.value_type;
642 memcpy(&info->value_type, vt, sizeof(info->value_type));
643 info->btree_info.tm = tm;
644 info->btree_info.levels = 1;
647 bvt->size = sizeof(__le64);
648 bvt->inc = block_inc;
649 bvt->dec = block_dec;
650 bvt->equal = block_equal;
652 EXPORT_SYMBOL_GPL(dm_array_info_init);
654 int dm_array_empty(struct dm_array_info *info, dm_block_t *root)
656 return dm_btree_empty(&info->btree_info, root);
658 EXPORT_SYMBOL_GPL(dm_array_empty);
660 static int array_resize(struct dm_array_info *info, dm_block_t root,
661 uint32_t old_size, uint32_t new_size,
662 const void *value, dm_block_t *new_root)
665 struct resize resize;
667 if (old_size == new_size) {
674 resize.size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
675 resize.max_entries = calc_max_entries(info->value_type.size,
676 resize.size_of_block);
678 resize.old_nr_full_blocks = old_size / resize.max_entries;
679 resize.old_nr_entries_in_last_block = old_size % resize.max_entries;
680 resize.new_nr_full_blocks = new_size / resize.max_entries;
681 resize.new_nr_entries_in_last_block = new_size % resize.max_entries;
682 resize.value = value;
684 r = ((new_size > old_size) ? grow : shrink)(&resize);
688 *new_root = resize.root;
692 int dm_array_resize(struct dm_array_info *info, dm_block_t root,
693 uint32_t old_size, uint32_t new_size,
694 const void *value, dm_block_t *new_root)
695 __dm_written_to_disk(value)
697 int r = array_resize(info, root, old_size, new_size, value, new_root);
698 __dm_unbless_for_disk(value);
701 EXPORT_SYMBOL_GPL(dm_array_resize);
703 static int populate_ablock_with_values(struct dm_array_info *info, struct array_block *ab,
704 value_fn fn, void *context, unsigned base, unsigned new_nr)
708 struct dm_btree_value_type *vt = &info->value_type;
710 BUG_ON(le32_to_cpu(ab->nr_entries));
711 BUG_ON(new_nr > le32_to_cpu(ab->max_entries));
713 for (i = 0; i < new_nr; i++) {
714 r = fn(base + i, element_at(info, ab, i), context);
719 vt->inc(vt->context, element_at(info, ab, i), 1);
722 ab->nr_entries = cpu_to_le32(new_nr);
726 int dm_array_new(struct dm_array_info *info, dm_block_t *root,
727 uint32_t size, value_fn fn, void *context)
730 struct dm_block *block;
731 struct array_block *ab;
732 unsigned block_index, end_block, size_of_block, max_entries;
734 r = dm_array_empty(info, root);
738 size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
739 max_entries = calc_max_entries(info->value_type.size, size_of_block);
740 end_block = dm_div_up(size, max_entries);
742 for (block_index = 0; block_index != end_block; block_index++) {
743 r = alloc_ablock(info, size_of_block, max_entries, &block, &ab);
747 r = populate_ablock_with_values(info, ab, fn, context,
748 block_index * max_entries,
749 min(max_entries, size));
751 unlock_ablock(info, block);
755 r = insert_ablock(info, block_index, block, root);
756 unlock_ablock(info, block);
765 EXPORT_SYMBOL_GPL(dm_array_new);
767 int dm_array_del(struct dm_array_info *info, dm_block_t root)
769 return dm_btree_del(&info->btree_info, root);
771 EXPORT_SYMBOL_GPL(dm_array_del);
773 int dm_array_get_value(struct dm_array_info *info, dm_block_t root,
774 uint32_t index, void *value_le)
777 struct dm_block *block;
778 struct array_block *ab;
779 size_t size_of_block;
780 unsigned entry, max_entries;
782 size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
783 max_entries = calc_max_entries(info->value_type.size, size_of_block);
785 r = lookup_ablock(info, root, index / max_entries, &block, &ab);
789 entry = index % max_entries;
790 if (entry >= le32_to_cpu(ab->nr_entries))
793 memcpy(value_le, element_at(info, ab, entry),
794 info->value_type.size);
796 unlock_ablock(info, block);
799 EXPORT_SYMBOL_GPL(dm_array_get_value);
801 static int array_set_value(struct dm_array_info *info, dm_block_t root,
802 uint32_t index, const void *value, dm_block_t *new_root)
805 struct dm_block *block;
806 struct array_block *ab;
807 size_t size_of_block;
808 unsigned max_entries;
811 struct dm_btree_value_type *vt = &info->value_type;
813 size_of_block = dm_bm_block_size(dm_tm_get_bm(info->btree_info.tm));
814 max_entries = calc_max_entries(info->value_type.size, size_of_block);
816 r = shadow_ablock(info, &root, index / max_entries, &block, &ab);
821 entry = index % max_entries;
822 if (entry >= le32_to_cpu(ab->nr_entries)) {
827 old_value = element_at(info, ab, entry);
829 (!vt->equal || !vt->equal(vt->context, old_value, value))) {
830 vt->dec(vt->context, old_value, 1);
832 vt->inc(vt->context, value, 1);
835 memcpy(old_value, value, info->value_type.size);
838 unlock_ablock(info, block);
842 int dm_array_set_value(struct dm_array_info *info, dm_block_t root,
843 uint32_t index, const void *value, dm_block_t *new_root)
844 __dm_written_to_disk(value)
848 r = array_set_value(info, root, index, value, new_root);
849 __dm_unbless_for_disk(value);
852 EXPORT_SYMBOL_GPL(dm_array_set_value);
855 struct dm_array_info *info;
856 int (*fn)(void *context, uint64_t key, void *leaf);
860 static int walk_ablock(void *context, uint64_t *keys, void *leaf)
862 struct walk_info *wi = context;
867 unsigned nr_entries, max_entries;
868 struct dm_block *block;
869 struct array_block *ab;
871 memcpy(&block_le, leaf, sizeof(block_le));
872 r = get_ablock(wi->info, le64_to_cpu(block_le), &block, &ab);
876 max_entries = le32_to_cpu(ab->max_entries);
877 nr_entries = le32_to_cpu(ab->nr_entries);
878 for (i = 0; i < nr_entries; i++) {
879 r = wi->fn(wi->context, keys[0] * max_entries + i,
880 element_at(wi->info, ab, i));
886 unlock_ablock(wi->info, block);
890 int dm_array_walk(struct dm_array_info *info, dm_block_t root,
891 int (*fn)(void *, uint64_t key, void *leaf),
898 wi.context = context;
900 return dm_btree_walk(&info->btree_info, root, walk_ablock, &wi);
902 EXPORT_SYMBOL_GPL(dm_array_walk);
904 /*----------------------------------------------------------------*/
906 static int load_ablock(struct dm_array_cursor *c)
913 unlock_ablock(c->info, c->block);
919 r = dm_btree_cursor_get_value(&c->cursor, &key, &value_le);
921 DMERR("dm_btree_cursor_get_value failed");
922 dm_btree_cursor_end(&c->cursor);
925 r = get_ablock(c->info, le64_to_cpu(value_le), &c->block, &c->ab);
927 DMERR("get_ablock failed");
928 dm_btree_cursor_end(&c->cursor);
935 int dm_array_cursor_begin(struct dm_array_info *info, dm_block_t root,
936 struct dm_array_cursor *c)
940 memset(c, 0, sizeof(*c));
942 r = dm_btree_cursor_begin(&info->btree_info, root, true, &c->cursor);
944 DMERR("couldn't create btree cursor");
948 return load_ablock(c);
950 EXPORT_SYMBOL_GPL(dm_array_cursor_begin);
952 void dm_array_cursor_end(struct dm_array_cursor *c)
955 unlock_ablock(c->info, c->block);
956 dm_btree_cursor_end(&c->cursor);
959 EXPORT_SYMBOL_GPL(dm_array_cursor_end);
961 int dm_array_cursor_next(struct dm_array_cursor *c)
970 if (c->index >= le32_to_cpu(c->ab->nr_entries)) {
971 r = dm_btree_cursor_next(&c->cursor);
982 EXPORT_SYMBOL_GPL(dm_array_cursor_next);
984 int dm_array_cursor_skip(struct dm_array_cursor *c, uint32_t count)
989 uint32_t remaining = le32_to_cpu(c->ab->nr_entries) - c->index;
991 if (count < remaining) {
997 r = dm_array_cursor_next(c);
1003 EXPORT_SYMBOL_GPL(dm_array_cursor_skip);
1005 void dm_array_cursor_get_value(struct dm_array_cursor *c, void **value_le)
1007 *value_le = element_at(c->info, c->ab, c->index);
1009 EXPORT_SYMBOL_GPL(dm_array_cursor_get_value);
1011 /*----------------------------------------------------------------*/