free-space-cache.h 5.1 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4 5
/*
 * Copyright (C) 2009 Oracle.  All rights reserved.
 */

6 7
#ifndef BTRFS_FREE_SPACE_CACHE_H
#define BTRFS_FREE_SPACE_CACHE_H
8

9 10
/*
 * This is the trim state of an extent or bitmap.
11 12 13 14 15
 *
 * BTRFS_TRIM_STATE_TRIMMING is special and used to maintain the state of a
 * bitmap as we may need several trims to fully trim a single bitmap entry.
 * This is reset should any free space other than trimmed space be added to the
 * bitmap.
16 17 18 19
 */
enum btrfs_trim_state {
	BTRFS_TRIM_STATE_UNTRIMMED,
	BTRFS_TRIM_STATE_TRIMMED,
20
	BTRFS_TRIM_STATE_TRIMMING,
21 22
};

23 24 25 26
struct btrfs_free_space {
	struct rb_node offset_index;
	u64 offset;
	u64 bytes;
27
	u64 max_extent_size;
28 29
	unsigned long *bitmap;
	struct list_head list;
30
	enum btrfs_trim_state trim_state;
31
	s32 bitmap_extents;
32 33
};

34 35 36 37 38
static inline bool btrfs_free_space_trimmed(struct btrfs_free_space *info)
{
	return (info->trim_state == BTRFS_TRIM_STATE_TRIMMED);
}

39 40 41 42 43 44
static inline bool btrfs_free_space_trimming_bitmap(
					    struct btrfs_free_space *info)
{
	return (info->trim_state == BTRFS_TRIM_STATE_TRIMMING);
}

45 46 47 48 49 50 51 52 53
struct btrfs_free_space_ctl {
	spinlock_t tree_lock;
	struct rb_root free_space_offset;
	u64 free_space;
	int extents_thresh;
	int free_extents;
	int total_bitmaps;
	int unit;
	u64 start;
54
	s32 discardable_extents[BTRFS_STAT_NR_ENTRIES];
55
	s64 discardable_bytes[BTRFS_STAT_NR_ENTRIES];
56
	const struct btrfs_free_space_op *op;
57
	void *private;
58 59
	struct mutex cache_writeout_mutex;
	struct list_head trimming_ranges;
60 61 62 63 64 65 66
};

struct btrfs_free_space_op {
	bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
			   struct btrfs_free_space *info);
};

67 68 69 70 71 72 73 74 75 76 77 78
struct btrfs_io_ctl {
	void *cur, *orig;
	struct page *page;
	struct page **pages;
	struct btrfs_fs_info *fs_info;
	struct inode *inode;
	unsigned long size;
	int index;
	int num_pages;
	int entries;
	int bitmaps;
};
79

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 86 87
int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans,
				  struct inode *inode,
				  struct btrfs_block_group *block_group);
J
Josef Bacik 已提交
88

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,
93
				    struct inode *inode);
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,
J
Josef Bacik 已提交
100
			  struct btrfs_path *path);
101

102 103
void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group,
			       struct btrfs_free_space_ctl *ctl);
104 105
int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
			   struct btrfs_free_space_ctl *ctl,
106 107
			   u64 bytenr, u64 size,
			   enum btrfs_trim_state trim_state);
108
int btrfs_add_free_space(struct btrfs_block_group *block_group,
109
			 u64 bytenr, u64 size);
110 111
int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
				       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 119
			       u64 offset, u64 bytes, u64 empty_size,
			       u64 *max_extent_size);
120
void btrfs_dump_free_space(struct btrfs_block_group *block_group,
121
			   u64 bytes);
122
int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
123 124 125
			     struct btrfs_free_cluster *cluster,
			     u64 offset, u64 bytes, u64 empty_size);
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 135 136 137 138
int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
				   u64 *trimmed, u64 start, u64 end, u64 minlen,
				   bool async);
int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
				   u64 *trimmed, u64 start, u64 end, u64 minlen,
D
Dennis Zhou 已提交
139
				   u64 maxlen, bool async);
140

141 142
bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info);
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);
148
#endif
149

150
#endif