^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) #ifndef _LINUX_DM_SPACE_MAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define _LINUX_DM_SPACE_MAP_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "dm-block-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) typedef void (*dm_sm_threshold_fn)(void *context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * struct dm_space_map keeps a record of how many times each block in a device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * is referenced. It needs to be fixed on disk as part of the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct dm_space_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void (*destroy)(struct dm_space_map *sm);
^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) * You must commit before allocating the newly added space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int (*extend)(struct dm_space_map *sm, dm_block_t extra_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Extensions do not appear in this count until after commit has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * been called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int (*get_nr_blocks)(struct dm_space_map *sm, dm_block_t *count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Space maps must never allocate a block from the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * transaction, in case we need to rollback. This complicates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * semantics of get_nr_free(), it should return the number of blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * that are available for allocation _now_. For instance you may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * have blocks with a zero reference count that will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * available for allocation until after the next commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int (*get_nr_free)(struct dm_space_map *sm, dm_block_t *count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int (*get_count)(struct dm_space_map *sm, dm_block_t b, uint32_t *result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int (*count_is_more_than_one)(struct dm_space_map *sm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int *result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int (*set_count)(struct dm_space_map *sm, dm_block_t b, uint32_t count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int (*commit)(struct dm_space_map *sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int (*inc_block)(struct dm_space_map *sm, dm_block_t b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int (*dec_block)(struct dm_space_map *sm, dm_block_t b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * new_block will increment the returned block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int (*new_block)(struct dm_space_map *sm, dm_block_t *b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * The root contains all the information needed to fix the space map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Generally this info is small, so squirrel it away in a disk block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * along with other info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int (*root_size)(struct dm_space_map *sm, size_t *result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int (*copy_root)(struct dm_space_map *sm, void *copy_to_here_le, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * You can register one threshold callback which is edge-triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * when the free space in the space map drops below the threshold.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int (*register_threshold_callback)(struct dm_space_map *sm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dm_block_t threshold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dm_sm_threshold_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) void *context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static inline void dm_sm_destroy(struct dm_space_map *sm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) sm->destroy(sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static inline int dm_sm_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return sm->extend(sm, extra_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static inline int dm_sm_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return sm->get_nr_blocks(sm, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static inline int dm_sm_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return sm->get_nr_free(sm, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline int dm_sm_get_count(struct dm_space_map *sm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) uint32_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return sm->get_count(sm, b, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static inline int dm_sm_count_is_more_than_one(struct dm_space_map *sm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) dm_block_t b, int *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return sm->count_is_more_than_one(sm, b, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static inline int dm_sm_set_count(struct dm_space_map *sm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) uint32_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return sm->set_count(sm, b, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline int dm_sm_commit(struct dm_space_map *sm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return sm->commit(sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline int dm_sm_inc_block(struct dm_space_map *sm, dm_block_t b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return sm->inc_block(sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline int dm_sm_dec_block(struct dm_space_map *sm, dm_block_t b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return sm->dec_block(sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static inline int dm_sm_new_block(struct dm_space_map *sm, dm_block_t *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return sm->new_block(sm, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static inline int dm_sm_root_size(struct dm_space_map *sm, size_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return sm->root_size(sm, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static inline int dm_sm_copy_root(struct dm_space_map *sm, void *copy_to_here_le, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return sm->copy_root(sm, copy_to_here_le, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline int dm_sm_register_threshold_callback(struct dm_space_map *sm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) dm_block_t threshold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dm_sm_threshold_fn fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (sm->register_threshold_callback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return sm->register_threshold_callback(sm, threshold, fn, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #endif /* _LINUX_DM_SPACE_MAP_H */