^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2011 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "dm-space-map-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "dm-space-map-disk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "dm-space-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "dm-transaction-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device-mapper.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DM_MSG_PREFIX "space map disk"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Space map interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct sm_disk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct dm_space_map sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct ll_disk ll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct ll_disk old_ll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) dm_block_t begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) dm_block_t nr_allocated_this_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static void sm_disk_destroy(struct dm_space_map *sm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) kfree(smd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int sm_disk_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return sm_ll_extend(&smd->ll, extra_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int sm_disk_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *count = smd->old_ll.nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int sm_disk_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *count = (smd->old_ll.nr_blocks - smd->old_ll.nr_allocated) - smd->nr_allocated_this_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int sm_disk_get_count(struct dm_space_map *sm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) uint32_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return sm_ll_lookup(&smd->ll, b, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int sm_disk_count_is_more_than_one(struct dm_space_map *sm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) uint32_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) r = sm_disk_get_count(sm, b, &count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *result = count > 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int sm_disk_set_count(struct dm_space_map *sm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) uint32_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) uint32_t old_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) enum allocation_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) r = sm_ll_insert(&smd->ll, b, count, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) switch (ev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case SM_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) case SM_ALLOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * This _must_ be free in the prior transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * otherwise we've lost atomicity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) smd->nr_allocated_this_transaction++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) case SM_FREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * It's only free if it's also free in the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) r = sm_ll_lookup(&smd->old_ll, b, &old_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!old_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) smd->nr_allocated_this_transaction--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int sm_disk_inc_block(struct dm_space_map *sm, dm_block_t b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) enum allocation_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) r = sm_ll_inc(&smd->ll, b, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!r && (ev == SM_ALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * This _must_ be free in the prior transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * otherwise we've lost atomicity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) smd->nr_allocated_this_transaction++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int sm_disk_dec_block(struct dm_space_map *sm, dm_block_t b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) uint32_t old_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) enum allocation_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) r = sm_ll_dec(&smd->ll, b, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!r && (ev == SM_FREE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * It's only free if it's also free in the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) r = sm_ll_lookup(&smd->old_ll, b, &old_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!r && !old_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) smd->nr_allocated_this_transaction--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) enum allocation_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Any block we allocate has to be free in both the old and current ll.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (r == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * There's no free block between smd->begin and the end of the metadata device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * We search before smd->begin in case something has been freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, 0, smd->begin, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) smd->begin = *b + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) r = sm_ll_inc(&smd->ll, *b, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) BUG_ON(ev != SM_ALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) smd->nr_allocated_this_transaction++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int sm_disk_commit(struct dm_space_map *sm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) dm_block_t nr_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) r = sm_disk_get_nr_free(sm, &nr_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) r = sm_ll_commit(&smd->ll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) memcpy(&smd->old_ll, &smd->ll, sizeof(smd->old_ll));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) smd->nr_allocated_this_transaction = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) r = sm_disk_get_nr_free(sm, &nr_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int sm_disk_root_size(struct dm_space_map *sm, size_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *result = sizeof(struct disk_sm_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int sm_disk_copy_root(struct dm_space_map *sm, void *where_le, size_t max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct disk_sm_root root_le;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) root_le.nr_blocks = cpu_to_le64(smd->ll.nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) root_le.nr_allocated = cpu_to_le64(smd->ll.nr_allocated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) root_le.bitmap_root = cpu_to_le64(smd->ll.bitmap_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) root_le.ref_count_root = cpu_to_le64(smd->ll.ref_count_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (max < sizeof(root_le))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) memcpy(where_le, &root_le, sizeof(root_le));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static struct dm_space_map ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) .destroy = sm_disk_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) .extend = sm_disk_extend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) .get_nr_blocks = sm_disk_get_nr_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) .get_nr_free = sm_disk_get_nr_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .get_count = sm_disk_get_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .count_is_more_than_one = sm_disk_count_is_more_than_one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) .set_count = sm_disk_set_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .inc_block = sm_disk_inc_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) .dec_block = sm_disk_dec_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) .new_block = sm_disk_new_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) .commit = sm_disk_commit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) .root_size = sm_disk_root_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) .copy_root = sm_disk_copy_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) .register_threshold_callback = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct dm_space_map *dm_sm_disk_create(struct dm_transaction_manager *tm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) dm_block_t nr_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct sm_disk *smd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) smd = kmalloc(sizeof(*smd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!smd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) smd->begin = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) smd->nr_allocated_this_transaction = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) memcpy(&smd->sm, &ops, sizeof(smd->sm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) r = sm_ll_new_disk(&smd->ll, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) r = sm_ll_extend(&smd->ll, nr_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) r = sm_disk_commit(&smd->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return &smd->sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) kfree(smd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) EXPORT_SYMBOL_GPL(dm_sm_disk_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct dm_space_map *dm_sm_disk_open(struct dm_transaction_manager *tm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) void *root_le, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct sm_disk *smd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) smd = kmalloc(sizeof(*smd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!smd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) smd->begin = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) smd->nr_allocated_this_transaction = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) memcpy(&smd->sm, &ops, sizeof(smd->sm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) r = sm_ll_open_disk(&smd->ll, tm, root_le, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) r = sm_disk_commit(&smd->sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return &smd->sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) kfree(smd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return ERR_PTR(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) EXPORT_SYMBOL_GPL(dm_sm_disk_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) /*----------------------------------------------------------------*/