13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */
23241b1d3SJoe Thornber /*
33241b1d3SJoe Thornber * Copyright (C) 2011 Red Hat, Inc.
43241b1d3SJoe Thornber *
53241b1d3SJoe Thornber * This file is released under the GPL.
63241b1d3SJoe Thornber */
73241b1d3SJoe Thornber
83241b1d3SJoe Thornber #ifndef _LINUX_DM_SPACE_MAP_H
93241b1d3SJoe Thornber #define _LINUX_DM_SPACE_MAP_H
103241b1d3SJoe Thornber
113241b1d3SJoe Thornber #include "dm-block-manager.h"
123241b1d3SJoe Thornber
137c3d3f2aSJoe Thornber typedef void (*dm_sm_threshold_fn)(void *context);
147c3d3f2aSJoe Thornber
153241b1d3SJoe Thornber /*
163241b1d3SJoe Thornber * struct dm_space_map keeps a record of how many times each block in a device
173241b1d3SJoe Thornber * is referenced. It needs to be fixed on disk as part of the transaction.
183241b1d3SJoe Thornber */
193241b1d3SJoe Thornber struct dm_space_map {
203241b1d3SJoe Thornber void (*destroy)(struct dm_space_map *sm);
213241b1d3SJoe Thornber
223241b1d3SJoe Thornber /*
233241b1d3SJoe Thornber * You must commit before allocating the newly added space.
243241b1d3SJoe Thornber */
253241b1d3SJoe Thornber int (*extend)(struct dm_space_map *sm, dm_block_t extra_blocks);
263241b1d3SJoe Thornber
273241b1d3SJoe Thornber /*
283241b1d3SJoe Thornber * Extensions do not appear in this count until after commit has
293241b1d3SJoe Thornber * been called.
303241b1d3SJoe Thornber */
313241b1d3SJoe Thornber int (*get_nr_blocks)(struct dm_space_map *sm, dm_block_t *count);
323241b1d3SJoe Thornber
333241b1d3SJoe Thornber /*
343241b1d3SJoe Thornber * Space maps must never allocate a block from the previous
353241b1d3SJoe Thornber * transaction, in case we need to rollback. This complicates the
363241b1d3SJoe Thornber * semantics of get_nr_free(), it should return the number of blocks
373241b1d3SJoe Thornber * that are available for allocation _now_. For instance you may
383241b1d3SJoe Thornber * have blocks with a zero reference count that will not be
393241b1d3SJoe Thornber * available for allocation until after the next commit.
403241b1d3SJoe Thornber */
413241b1d3SJoe Thornber int (*get_nr_free)(struct dm_space_map *sm, dm_block_t *count);
423241b1d3SJoe Thornber
433241b1d3SJoe Thornber int (*get_count)(struct dm_space_map *sm, dm_block_t b, uint32_t *result);
443241b1d3SJoe Thornber int (*count_is_more_than_one)(struct dm_space_map *sm, dm_block_t b,
453241b1d3SJoe Thornber int *result);
463241b1d3SJoe Thornber int (*set_count)(struct dm_space_map *sm, dm_block_t b, uint32_t count);
473241b1d3SJoe Thornber
483241b1d3SJoe Thornber int (*commit)(struct dm_space_map *sm);
493241b1d3SJoe Thornber
50be500ed7SJoe Thornber int (*inc_blocks)(struct dm_space_map *sm, dm_block_t b, dm_block_t e);
51be500ed7SJoe Thornber int (*dec_blocks)(struct dm_space_map *sm, dm_block_t b, dm_block_t e);
523241b1d3SJoe Thornber
533241b1d3SJoe Thornber /*
543241b1d3SJoe Thornber * new_block will increment the returned block.
553241b1d3SJoe Thornber */
563241b1d3SJoe Thornber int (*new_block)(struct dm_space_map *sm, dm_block_t *b);
573241b1d3SJoe Thornber
583241b1d3SJoe Thornber /*
593241b1d3SJoe Thornber * The root contains all the information needed to fix the space map.
603241b1d3SJoe Thornber * Generally this info is small, so squirrel it away in a disk block
613241b1d3SJoe Thornber * along with other info.
623241b1d3SJoe Thornber */
633241b1d3SJoe Thornber int (*root_size)(struct dm_space_map *sm, size_t *result);
643241b1d3SJoe Thornber int (*copy_root)(struct dm_space_map *sm, void *copy_to_here_le, size_t len);
657c3d3f2aSJoe Thornber
667c3d3f2aSJoe Thornber /*
677c3d3f2aSJoe Thornber * You can register one threshold callback which is edge-triggered
687c3d3f2aSJoe Thornber * when the free space in the space map drops below the threshold.
697c3d3f2aSJoe Thornber */
707c3d3f2aSJoe Thornber int (*register_threshold_callback)(struct dm_space_map *sm,
717c3d3f2aSJoe Thornber dm_block_t threshold,
727c3d3f2aSJoe Thornber dm_sm_threshold_fn fn,
737c3d3f2aSJoe Thornber void *context);
743241b1d3SJoe Thornber };
753241b1d3SJoe Thornber
763241b1d3SJoe Thornber /*----------------------------------------------------------------*/
773241b1d3SJoe Thornber
dm_sm_destroy(struct dm_space_map * sm)783241b1d3SJoe Thornber static inline void dm_sm_destroy(struct dm_space_map *sm)
793241b1d3SJoe Thornber {
80*d4830012SLi Lingfeng if (sm)
813241b1d3SJoe Thornber sm->destroy(sm);
823241b1d3SJoe Thornber }
833241b1d3SJoe Thornber
dm_sm_extend(struct dm_space_map * sm,dm_block_t extra_blocks)843241b1d3SJoe Thornber static inline int dm_sm_extend(struct dm_space_map *sm, dm_block_t extra_blocks)
853241b1d3SJoe Thornber {
863241b1d3SJoe Thornber return sm->extend(sm, extra_blocks);
873241b1d3SJoe Thornber }
883241b1d3SJoe Thornber
dm_sm_get_nr_blocks(struct dm_space_map * sm,dm_block_t * count)893241b1d3SJoe Thornber static inline int dm_sm_get_nr_blocks(struct dm_space_map *sm, dm_block_t *count)
903241b1d3SJoe Thornber {
913241b1d3SJoe Thornber return sm->get_nr_blocks(sm, count);
923241b1d3SJoe Thornber }
933241b1d3SJoe Thornber
dm_sm_get_nr_free(struct dm_space_map * sm,dm_block_t * count)943241b1d3SJoe Thornber static inline int dm_sm_get_nr_free(struct dm_space_map *sm, dm_block_t *count)
953241b1d3SJoe Thornber {
963241b1d3SJoe Thornber return sm->get_nr_free(sm, count);
973241b1d3SJoe Thornber }
983241b1d3SJoe Thornber
dm_sm_get_count(struct dm_space_map * sm,dm_block_t b,uint32_t * result)993241b1d3SJoe Thornber static inline int dm_sm_get_count(struct dm_space_map *sm, dm_block_t b,
1003241b1d3SJoe Thornber uint32_t *result)
1013241b1d3SJoe Thornber {
1023241b1d3SJoe Thornber return sm->get_count(sm, b, result);
1033241b1d3SJoe Thornber }
1043241b1d3SJoe Thornber
dm_sm_count_is_more_than_one(struct dm_space_map * sm,dm_block_t b,int * result)1053241b1d3SJoe Thornber static inline int dm_sm_count_is_more_than_one(struct dm_space_map *sm,
1063241b1d3SJoe Thornber dm_block_t b, int *result)
1073241b1d3SJoe Thornber {
1083241b1d3SJoe Thornber return sm->count_is_more_than_one(sm, b, result);
1093241b1d3SJoe Thornber }
1103241b1d3SJoe Thornber
dm_sm_set_count(struct dm_space_map * sm,dm_block_t b,uint32_t count)1113241b1d3SJoe Thornber static inline int dm_sm_set_count(struct dm_space_map *sm, dm_block_t b,
1123241b1d3SJoe Thornber uint32_t count)
1133241b1d3SJoe Thornber {
1143241b1d3SJoe Thornber return sm->set_count(sm, b, count);
1153241b1d3SJoe Thornber }
1163241b1d3SJoe Thornber
dm_sm_commit(struct dm_space_map * sm)1173241b1d3SJoe Thornber static inline int dm_sm_commit(struct dm_space_map *sm)
1183241b1d3SJoe Thornber {
1193241b1d3SJoe Thornber return sm->commit(sm);
1203241b1d3SJoe Thornber }
1213241b1d3SJoe Thornber
dm_sm_inc_blocks(struct dm_space_map * sm,dm_block_t b,dm_block_t e)122be500ed7SJoe Thornber static inline int dm_sm_inc_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
123be500ed7SJoe Thornber {
124be500ed7SJoe Thornber return sm->inc_blocks(sm, b, e);
125be500ed7SJoe Thornber }
126be500ed7SJoe Thornber
dm_sm_inc_block(struct dm_space_map * sm,dm_block_t b)1273241b1d3SJoe Thornber static inline int dm_sm_inc_block(struct dm_space_map *sm, dm_block_t b)
1283241b1d3SJoe Thornber {
129be500ed7SJoe Thornber return dm_sm_inc_blocks(sm, b, b + 1);
130be500ed7SJoe Thornber }
131be500ed7SJoe Thornber
dm_sm_dec_blocks(struct dm_space_map * sm,dm_block_t b,dm_block_t e)132be500ed7SJoe Thornber static inline int dm_sm_dec_blocks(struct dm_space_map *sm, dm_block_t b, dm_block_t e)
133be500ed7SJoe Thornber {
134be500ed7SJoe Thornber return sm->dec_blocks(sm, b, e);
1353241b1d3SJoe Thornber }
1363241b1d3SJoe Thornber
dm_sm_dec_block(struct dm_space_map * sm,dm_block_t b)1373241b1d3SJoe Thornber static inline int dm_sm_dec_block(struct dm_space_map *sm, dm_block_t b)
1383241b1d3SJoe Thornber {
139be500ed7SJoe Thornber return dm_sm_dec_blocks(sm, b, b + 1);
1403241b1d3SJoe Thornber }
1413241b1d3SJoe Thornber
dm_sm_new_block(struct dm_space_map * sm,dm_block_t * b)1423241b1d3SJoe Thornber static inline int dm_sm_new_block(struct dm_space_map *sm, dm_block_t *b)
1433241b1d3SJoe Thornber {
1443241b1d3SJoe Thornber return sm->new_block(sm, b);
1453241b1d3SJoe Thornber }
1463241b1d3SJoe Thornber
dm_sm_root_size(struct dm_space_map * sm,size_t * result)1473241b1d3SJoe Thornber static inline int dm_sm_root_size(struct dm_space_map *sm, size_t *result)
1483241b1d3SJoe Thornber {
1493241b1d3SJoe Thornber return sm->root_size(sm, result);
1503241b1d3SJoe Thornber }
1513241b1d3SJoe Thornber
dm_sm_copy_root(struct dm_space_map * sm,void * copy_to_here_le,size_t len)1523241b1d3SJoe Thornber static inline int dm_sm_copy_root(struct dm_space_map *sm, void *copy_to_here_le, size_t len)
1533241b1d3SJoe Thornber {
1543241b1d3SJoe Thornber return sm->copy_root(sm, copy_to_here_le, len);
1553241b1d3SJoe Thornber }
1563241b1d3SJoe Thornber
dm_sm_register_threshold_callback(struct dm_space_map * sm,dm_block_t threshold,dm_sm_threshold_fn fn,void * context)1577c3d3f2aSJoe Thornber static inline int dm_sm_register_threshold_callback(struct dm_space_map *sm,
1587c3d3f2aSJoe Thornber dm_block_t threshold,
1597c3d3f2aSJoe Thornber dm_sm_threshold_fn fn,
1607c3d3f2aSJoe Thornber void *context)
1617c3d3f2aSJoe Thornber {
1627c3d3f2aSJoe Thornber if (sm->register_threshold_callback)
1637c3d3f2aSJoe Thornber return sm->register_threshold_callback(sm, threshold, fn, context);
1647c3d3f2aSJoe Thornber
1657c3d3f2aSJoe Thornber return -EINVAL;
1667c3d3f2aSJoe Thornber }
1677c3d3f2aSJoe Thornber
1687c3d3f2aSJoe Thornber
1693241b1d3SJoe Thornber #endif /* _LINUX_DM_SPACE_MAP_H */
170