]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/btrfs/free-space-cache.h
btrfs: sysfs: add UUID/debug/discard directory
[mirror_ubuntu-hirsute-kernel.git] / fs / btrfs / free-space-cache.h
CommitLineData
9888c340 1/* SPDX-License-Identifier: GPL-2.0 */
fa9c0d79
CM
2/*
3 * Copyright (C) 2009 Oracle. All rights reserved.
fa9c0d79
CM
4 */
5
9888c340
DS
6#ifndef BTRFS_FREE_SPACE_CACHE_H
7#define BTRFS_FREE_SPACE_CACHE_H
fa9c0d79 8
a7ccb255
DZ
9/*
10 * This is the trim state of an extent or bitmap.
da080fe1
DZ
11 *
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
15 * bitmap.
a7ccb255
DZ
16 */
17enum btrfs_trim_state {
18 BTRFS_TRIM_STATE_UNTRIMMED,
19 BTRFS_TRIM_STATE_TRIMMED,
da080fe1 20 BTRFS_TRIM_STATE_TRIMMING,
a7ccb255
DZ
21};
22
96303081
JB
23struct btrfs_free_space {
24 struct rb_node offset_index;
25 u64 offset;
26 u64 bytes;
cef40483 27 u64 max_extent_size;
96303081
JB
28 unsigned long *bitmap;
29 struct list_head list;
a7ccb255 30 enum btrfs_trim_state trim_state;
96303081
JB
31};
32
a7ccb255
DZ
33static inline bool btrfs_free_space_trimmed(struct btrfs_free_space *info)
34{
35 return (info->trim_state == BTRFS_TRIM_STATE_TRIMMED);
36}
37
da080fe1
DZ
38static inline bool btrfs_free_space_trimming_bitmap(
39 struct btrfs_free_space *info)
40{
41 return (info->trim_state == BTRFS_TRIM_STATE_TRIMMING);
42}
43
34d52cb6
LZ
44struct btrfs_free_space_ctl {
45 spinlock_t tree_lock;
46 struct rb_root free_space_offset;
47 u64 free_space;
48 int extents_thresh;
49 int free_extents;
50 int total_bitmaps;
51 int unit;
52 u64 start;
20e5506b 53 const struct btrfs_free_space_op *op;
34d52cb6 54 void *private;
55507ce3
FM
55 struct mutex cache_writeout_mutex;
56 struct list_head trimming_ranges;
34d52cb6
LZ
57};
58
59struct btrfs_free_space_op {
60 void (*recalc_thresholds)(struct btrfs_free_space_ctl *ctl);
61 bool (*use_bitmap)(struct btrfs_free_space_ctl *ctl,
62 struct btrfs_free_space *info);
63};
64
67b61aef
DS
65struct btrfs_io_ctl {
66 void *cur, *orig;
67 struct page *page;
68 struct page **pages;
69 struct btrfs_fs_info *fs_info;
70 struct inode *inode;
71 unsigned long size;
72 int index;
73 int num_pages;
74 int entries;
75 int bitmaps;
76 unsigned check_crcs:1;
77};
c9dc4c65 78
32da5386 79struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
7949f339 80 struct btrfs_path *path);
4ca75f1b 81int create_free_space_inode(struct btrfs_trans_handle *trans,
32da5386 82 struct btrfs_block_group *block_group,
0af3d00b 83 struct btrfs_path *path);
0cb59c99 84
2ff7e61e 85int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
7b61cd92 86 struct btrfs_block_rsv *rsv);
77ab86bf 87int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
32da5386 88 struct btrfs_block_group *block_group,
0af3d00b 89 struct inode *inode);
32da5386 90int load_free_space_cache(struct btrfs_block_group *block_group);
afdb5718 91int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
32da5386 92 struct btrfs_block_group *block_group,
afdb5718 93 struct btrfs_path *path);
fe041534 94int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
32da5386 95 struct btrfs_block_group *block_group,
0cb59c99 96 struct btrfs_path *path);
82d5902d
LZ
97struct inode *lookup_free_ino_inode(struct btrfs_root *root,
98 struct btrfs_path *path);
99int create_free_ino_inode(struct btrfs_root *root,
100 struct btrfs_trans_handle *trans,
101 struct btrfs_path *path);
102int load_free_ino_cache(struct btrfs_fs_info *fs_info,
103 struct btrfs_root *root);
104int btrfs_write_out_ino_cache(struct btrfs_root *root,
105 struct btrfs_trans_handle *trans,
53645a91
FDBM
106 struct btrfs_path *path,
107 struct inode *inode);
82d5902d 108
32da5386 109void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group);
ab8d0fc4
JM
110int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
111 struct btrfs_free_space_ctl *ctl,
a7ccb255
DZ
112 u64 bytenr, u64 size,
113 enum btrfs_trim_state trim_state);
32da5386 114int btrfs_add_free_space(struct btrfs_block_group *block_group,
478b4d9f 115 u64 bytenr, u64 size);
b0643e59
DZ
116int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
117 u64 bytenr, u64 size);
32da5386 118int btrfs_remove_free_space(struct btrfs_block_group *block_group,
fa9c0d79 119 u64 bytenr, u64 size);
581bb050 120void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl);
32da5386 121void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group);
6e80d4f8 122bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group);
32da5386 123u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
a4820398
MX
124 u64 offset, u64 bytes, u64 empty_size,
125 u64 *max_extent_size);
581bb050 126u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root);
32da5386 127void btrfs_dump_free_space(struct btrfs_block_group *block_group,
fa9c0d79 128 u64 bytes);
32da5386 129int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
fa9c0d79
CM
130 struct btrfs_free_cluster *cluster,
131 u64 offset, u64 bytes, u64 empty_size);
132void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster);
32da5386 133u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
fa9c0d79 134 struct btrfs_free_cluster *cluster, u64 bytes,
a4820398 135 u64 min_start, u64 *max_extent_size);
fa9c0d79 136int btrfs_return_cluster_to_free_space(
32da5386 137 struct btrfs_block_group *block_group,
fa9c0d79 138 struct btrfs_free_cluster *cluster);
32da5386 139int btrfs_trim_block_group(struct btrfs_block_group *block_group,
f7039b1d 140 u64 *trimmed, u64 start, u64 end, u64 minlen);
2bee7eb8
DZ
141int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
142 u64 *trimmed, u64 start, u64 end, u64 minlen,
143 bool async);
144int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
145 u64 *trimmed, u64 start, u64 end, u64 minlen,
146 bool async);
74255aa0 147
01327610 148/* Support functions for running our sanity tests */
dc11dd5d 149#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
32da5386 150int test_add_free_space_entry(struct btrfs_block_group *cache,
dc11dd5d 151 u64 offset, u64 bytes, bool bitmap);
32da5386 152int test_check_exists(struct btrfs_block_group *cache, u64 offset, u64 bytes);
dc11dd5d 153#endif
74255aa0 154
fa9c0d79 155#endif