1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2011 Red Hat, Inc.
5 * This file is released under the GPL.
8 #ifndef _LINUX_DM_SPACE_MAP_H
9 #define _LINUX_DM_SPACE_MAP_H
11 #include "dm-block-manager.h"
13 typedef void (*dm_sm_threshold_fn
)(void *context
);
16 * struct dm_space_map keeps a record of how many times each block in a device
17 * is referenced. It needs to be fixed on disk as part of the transaction.
20 void (*destroy
)(struct dm_space_map
*sm
);
23 * You must commit before allocating the newly added space.
25 int (*extend
)(struct dm_space_map
*sm
, dm_block_t extra_blocks
);
28 * Extensions do not appear in this count until after commit has
31 int (*get_nr_blocks
)(struct dm_space_map
*sm
, dm_block_t
*count
);
34 * Space maps must never allocate a block from the previous
35 * transaction, in case we need to rollback. This complicates the
36 * semantics of get_nr_free(), it should return the number of blocks
37 * that are available for allocation _now_. For instance you may
38 * have blocks with a zero reference count that will not be
39 * available for allocation until after the next commit.
41 int (*get_nr_free
)(struct dm_space_map
*sm
, dm_block_t
*count
);
43 int (*get_count
)(struct dm_space_map
*sm
, dm_block_t b
, uint32_t *result
);
44 int (*count_is_more_than_one
)(struct dm_space_map
*sm
, dm_block_t b
,
46 int (*set_count
)(struct dm_space_map
*sm
, dm_block_t b
, uint32_t count
);
48 int (*commit
)(struct dm_space_map
*sm
);
50 int (*inc_blocks
)(struct dm_space_map
*sm
, dm_block_t b
, dm_block_t e
);
51 int (*dec_blocks
)(struct dm_space_map
*sm
, dm_block_t b
, dm_block_t e
);
54 * new_block will increment the returned block.
56 int (*new_block
)(struct dm_space_map
*sm
, dm_block_t
*b
);
59 * The root contains all the information needed to fix the space map.
60 * Generally this info is small, so squirrel it away in a disk block
61 * along with other info.
63 int (*root_size
)(struct dm_space_map
*sm
, size_t *result
);
64 int (*copy_root
)(struct dm_space_map
*sm
, void *copy_to_here_le
, size_t len
);
67 * You can register one threshold callback which is edge-triggered
68 * when the free space in the space map drops below the threshold.
70 int (*register_threshold_callback
)(struct dm_space_map
*sm
,
72 dm_sm_threshold_fn fn
,
76 /*----------------------------------------------------------------*/
78 static inline void dm_sm_destroy(struct dm_space_map
*sm
)
84 static inline int dm_sm_extend(struct dm_space_map
*sm
, dm_block_t extra_blocks
)
86 return sm
->extend(sm
, extra_blocks
);
89 static inline int dm_sm_get_nr_blocks(struct dm_space_map
*sm
, dm_block_t
*count
)
91 return sm
->get_nr_blocks(sm
, count
);
94 static inline int dm_sm_get_nr_free(struct dm_space_map
*sm
, dm_block_t
*count
)
96 return sm
->get_nr_free(sm
, count
);
99 static inline int dm_sm_get_count(struct dm_space_map
*sm
, dm_block_t b
,
102 return sm
->get_count(sm
, b
, result
);
105 static inline int dm_sm_count_is_more_than_one(struct dm_space_map
*sm
,
106 dm_block_t b
, int *result
)
108 return sm
->count_is_more_than_one(sm
, b
, result
);
111 static inline int dm_sm_set_count(struct dm_space_map
*sm
, dm_block_t b
,
114 return sm
->set_count(sm
, b
, count
);
117 static inline int dm_sm_commit(struct dm_space_map
*sm
)
119 return sm
->commit(sm
);
122 static inline int dm_sm_inc_blocks(struct dm_space_map
*sm
, dm_block_t b
, dm_block_t e
)
124 return sm
->inc_blocks(sm
, b
, e
);
127 static inline int dm_sm_inc_block(struct dm_space_map
*sm
, dm_block_t b
)
129 return dm_sm_inc_blocks(sm
, b
, b
+ 1);
132 static inline int dm_sm_dec_blocks(struct dm_space_map
*sm
, dm_block_t b
, dm_block_t e
)
134 return sm
->dec_blocks(sm
, b
, e
);
137 static inline int dm_sm_dec_block(struct dm_space_map
*sm
, dm_block_t b
)
139 return dm_sm_dec_blocks(sm
, b
, b
+ 1);
142 static inline int dm_sm_new_block(struct dm_space_map
*sm
, dm_block_t
*b
)
144 return sm
->new_block(sm
, b
);
147 static inline int dm_sm_root_size(struct dm_space_map
*sm
, size_t *result
)
149 return sm
->root_size(sm
, result
);
152 static inline int dm_sm_copy_root(struct dm_space_map
*sm
, void *copy_to_here_le
, size_t len
)
154 return sm
->copy_root(sm
, copy_to_here_le
, len
);
157 static inline int dm_sm_register_threshold_callback(struct dm_space_map
*sm
,
158 dm_block_t threshold
,
159 dm_sm_threshold_fn fn
,
162 if (sm
->register_threshold_callback
)
163 return sm
->register_threshold_callback(sm
, threshold
, fn
, context
);
169 #endif /* _LINUX_DM_SPACE_MAP_H */