1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2009 Oracle. All rights reserved.
6 #ifndef BTRFS_FREE_SPACE_CACHE_H
7 #define BTRFS_FREE_SPACE_CACHE_H
10 * This is the trim state of an extent or bitmap.
12 * BTRFS_TRIM_STATE_TRIMMING is special and used to maintain the state of a
13 * bitmap as we may need several trims to fully trim a single bitmap entry.
14 * This is reset should any free space other than trimmed space be added to the
17 enum btrfs_trim_state
{
18 BTRFS_TRIM_STATE_UNTRIMMED
,
19 BTRFS_TRIM_STATE_TRIMMED
,
20 BTRFS_TRIM_STATE_TRIMMING
,
23 struct btrfs_free_space
{
24 struct rb_node offset_index
;
28 unsigned long *bitmap
;
29 struct list_head list
;
30 enum btrfs_trim_state trim_state
;
34 static inline bool btrfs_free_space_trimmed(struct btrfs_free_space
*info
)
36 return (info
->trim_state
== BTRFS_TRIM_STATE_TRIMMED
);
39 static inline bool btrfs_free_space_trimming_bitmap(
40 struct btrfs_free_space
*info
)
42 return (info
->trim_state
== BTRFS_TRIM_STATE_TRIMMING
);
45 struct btrfs_free_space_ctl
{
47 struct rb_root free_space_offset
;
54 s32 discardable_extents
[BTRFS_STAT_NR_ENTRIES
];
55 s64 discardable_bytes
[BTRFS_STAT_NR_ENTRIES
];
56 const struct btrfs_free_space_op
*op
;
58 struct mutex cache_writeout_mutex
;
59 struct list_head trimming_ranges
;
62 struct btrfs_free_space_op
{
63 bool (*use_bitmap
)(struct btrfs_free_space_ctl
*ctl
,
64 struct btrfs_free_space
*info
);
71 struct btrfs_fs_info
*fs_info
;
80 struct inode
*lookup_free_space_inode(struct btrfs_block_group
*block_group
,
81 struct btrfs_path
*path
);
82 int create_free_space_inode(struct btrfs_trans_handle
*trans
,
83 struct btrfs_block_group
*block_group
,
84 struct btrfs_path
*path
);
85 int btrfs_remove_free_space_inode(struct btrfs_trans_handle
*trans
,
87 struct btrfs_block_group
*block_group
);
89 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info
*fs_info
,
90 struct btrfs_block_rsv
*rsv
);
91 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle
*trans
,
92 struct btrfs_block_group
*block_group
,
94 int load_free_space_cache(struct btrfs_block_group
*block_group
);
95 int btrfs_wait_cache_io(struct btrfs_trans_handle
*trans
,
96 struct btrfs_block_group
*block_group
,
97 struct btrfs_path
*path
);
98 int btrfs_write_out_cache(struct btrfs_trans_handle
*trans
,
99 struct btrfs_block_group
*block_group
,
100 struct btrfs_path
*path
);
102 void btrfs_init_free_space_ctl(struct btrfs_block_group
*block_group
,
103 struct btrfs_free_space_ctl
*ctl
);
104 int __btrfs_add_free_space(struct btrfs_fs_info
*fs_info
,
105 struct btrfs_free_space_ctl
*ctl
,
106 u64 bytenr
, u64 size
,
107 enum btrfs_trim_state trim_state
);
108 int btrfs_add_free_space(struct btrfs_block_group
*block_group
,
109 u64 bytenr
, u64 size
);
110 int btrfs_add_free_space_async_trimmed(struct btrfs_block_group
*block_group
,
111 u64 bytenr
, u64 size
);
112 int btrfs_remove_free_space(struct btrfs_block_group
*block_group
,
113 u64 bytenr
, u64 size
);
114 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl
*ctl
);
115 void btrfs_remove_free_space_cache(struct btrfs_block_group
*block_group
);
116 bool btrfs_is_free_space_trimmed(struct btrfs_block_group
*block_group
);
117 u64
btrfs_find_space_for_alloc(struct btrfs_block_group
*block_group
,
118 u64 offset
, u64 bytes
, u64 empty_size
,
119 u64
*max_extent_size
);
120 void btrfs_dump_free_space(struct btrfs_block_group
*block_group
,
122 int btrfs_find_space_cluster(struct btrfs_block_group
*block_group
,
123 struct btrfs_free_cluster
*cluster
,
124 u64 offset
, u64 bytes
, u64 empty_size
);
125 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
);
126 u64
btrfs_alloc_from_cluster(struct btrfs_block_group
*block_group
,
127 struct btrfs_free_cluster
*cluster
, u64 bytes
,
128 u64 min_start
, u64
*max_extent_size
);
129 void btrfs_return_cluster_to_free_space(
130 struct btrfs_block_group
*block_group
,
131 struct btrfs_free_cluster
*cluster
);
132 int btrfs_trim_block_group(struct btrfs_block_group
*block_group
,
133 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
);
134 int btrfs_trim_block_group_extents(struct btrfs_block_group
*block_group
,
135 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
,
137 int btrfs_trim_block_group_bitmaps(struct btrfs_block_group
*block_group
,
138 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
,
139 u64 maxlen
, bool async
);
141 bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info
*fs_info
);
142 int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info
*fs_info
, bool active
);
143 /* Support functions for running our sanity tests */
144 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145 int test_add_free_space_entry(struct btrfs_block_group
*cache
,
146 u64 offset
, u64 bytes
, bool bitmap
);
147 int test_check_exists(struct btrfs_block_group
*cache
, u64 offset
, u64 bytes
);