1c7f13d42SJosef Bacik /* SPDX-License-Identifier: GPL-2.0 */
2c7f13d42SJosef Bacik
3c7f13d42SJosef Bacik #ifndef BTRFS_FS_H
4c7f13d42SJosef Bacik #define BTRFS_FS_H
5c7f13d42SJosef Bacik
6243cf8d1SChristoph Hellwig #include <linux/blkdev.h>
73683fbbcSJosef Bacik #include <linux/fs.h>
83683fbbcSJosef Bacik #include <linux/btrfs_tree.h>
93683fbbcSJosef Bacik #include <linux/sizes.h>
103683fbbcSJosef Bacik #include "extent-io-tree.h"
113683fbbcSJosef Bacik #include "extent_map.h"
123683fbbcSJosef Bacik #include "async-thread.h"
133683fbbcSJosef Bacik #include "block-rsv.h"
143683fbbcSJosef Bacik
15a56159d4SJosef Bacik #define BTRFS_MAX_EXTENT_SIZE SZ_128M
16a56159d4SJosef Bacik
17a56159d4SJosef Bacik #define BTRFS_OLDEST_GENERATION 0ULL
18a56159d4SJosef Bacik
19a56159d4SJosef Bacik #define BTRFS_EMPTY_DIR_SIZE 0
20a56159d4SJosef Bacik
21a56159d4SJosef Bacik #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
22a56159d4SJosef Bacik
23a56159d4SJosef Bacik #define BTRFS_SUPER_INFO_OFFSET SZ_64K
24a56159d4SJosef Bacik #define BTRFS_SUPER_INFO_SIZE 4096
25a56159d4SJosef Bacik static_assert(sizeof(struct btrfs_super_block) == BTRFS_SUPER_INFO_SIZE);
26a56159d4SJosef Bacik
27a56159d4SJosef Bacik /*
285630e2bcSFilipe Manana * Number of metadata items necessary for an unlink operation:
295630e2bcSFilipe Manana *
305630e2bcSFilipe Manana * 1 for the possible orphan item
315630e2bcSFilipe Manana * 1 for the dir item
325630e2bcSFilipe Manana * 1 for the dir index
335630e2bcSFilipe Manana * 1 for the inode ref
345630e2bcSFilipe Manana * 1 for the inode
355630e2bcSFilipe Manana * 1 for the parent inode
365630e2bcSFilipe Manana */
375630e2bcSFilipe Manana #define BTRFS_UNLINK_METADATA_UNITS 6
385630e2bcSFilipe Manana
395630e2bcSFilipe Manana /*
40a56159d4SJosef Bacik * The reserved space at the beginning of each device. It covers the primary
41a56159d4SJosef Bacik * super block and leaves space for potential use by other tools like
42a56159d4SJosef Bacik * bootloaders or to lower potential damage of accidental overwrite.
43a56159d4SJosef Bacik */
44a56159d4SJosef Bacik #define BTRFS_DEVICE_RANGE_RESERVED (SZ_1M)
45ec8eb376SJosef Bacik /*
46ec8eb376SJosef Bacik * Runtime (in-memory) states of filesystem
47ec8eb376SJosef Bacik */
48ec8eb376SJosef Bacik enum {
49ec8eb376SJosef Bacik /*
50ec8eb376SJosef Bacik * Filesystem is being remounted, allow to skip some operations, like
51ec8eb376SJosef Bacik * defrag
52ec8eb376SJosef Bacik */
53ec8eb376SJosef Bacik BTRFS_FS_STATE_REMOUNTING,
54ec8eb376SJosef Bacik /* Filesystem in RO mode */
55ec8eb376SJosef Bacik BTRFS_FS_STATE_RO,
56ec8eb376SJosef Bacik /* Track if a transaction abort has been reported on this filesystem */
57ec8eb376SJosef Bacik BTRFS_FS_STATE_TRANS_ABORTED,
58ec8eb376SJosef Bacik /*
59ec8eb376SJosef Bacik * Bio operations should be blocked on this filesystem because a source
60ec8eb376SJosef Bacik * or target device is being destroyed as part of a device replace
61ec8eb376SJosef Bacik */
62ec8eb376SJosef Bacik BTRFS_FS_STATE_DEV_REPLACING,
63ec8eb376SJosef Bacik /* The btrfs_fs_info created for self-tests */
64ec8eb376SJosef Bacik BTRFS_FS_STATE_DUMMY_FS_INFO,
65ec8eb376SJosef Bacik
66ec8eb376SJosef Bacik BTRFS_FS_STATE_NO_CSUMS,
67ec8eb376SJosef Bacik
68ec8eb376SJosef Bacik /* Indicates there was an error cleaning up a log tree. */
69ec8eb376SJosef Bacik BTRFS_FS_STATE_LOG_CLEANUP_ERROR,
70ec8eb376SJosef Bacik
71ec8eb376SJosef Bacik BTRFS_FS_STATE_COUNT
72ec8eb376SJosef Bacik };
73ec8eb376SJosef Bacik
747966a6b5SJosef Bacik enum {
757966a6b5SJosef Bacik BTRFS_FS_CLOSING_START,
767966a6b5SJosef Bacik BTRFS_FS_CLOSING_DONE,
777966a6b5SJosef Bacik BTRFS_FS_LOG_RECOVERING,
787966a6b5SJosef Bacik BTRFS_FS_OPEN,
797966a6b5SJosef Bacik BTRFS_FS_QUOTA_ENABLED,
807966a6b5SJosef Bacik BTRFS_FS_UPDATE_UUID_TREE_GEN,
817966a6b5SJosef Bacik BTRFS_FS_CREATING_FREE_SPACE_TREE,
827966a6b5SJosef Bacik BTRFS_FS_BTREE_ERR,
837966a6b5SJosef Bacik BTRFS_FS_LOG1_ERR,
847966a6b5SJosef Bacik BTRFS_FS_LOG2_ERR,
857966a6b5SJosef Bacik BTRFS_FS_QUOTA_OVERRIDE,
867966a6b5SJosef Bacik /* Used to record internally whether fs has been frozen */
877966a6b5SJosef Bacik BTRFS_FS_FROZEN,
887966a6b5SJosef Bacik /*
897966a6b5SJosef Bacik * Indicate that balance has been set up from the ioctl and is in the
907966a6b5SJosef Bacik * main phase. The fs_info::balance_ctl is initialized.
917966a6b5SJosef Bacik */
927966a6b5SJosef Bacik BTRFS_FS_BALANCE_RUNNING,
937966a6b5SJosef Bacik
947966a6b5SJosef Bacik /*
957966a6b5SJosef Bacik * Indicate that relocation of a chunk has started, it's set per chunk
967966a6b5SJosef Bacik * and is toggled between chunks.
977966a6b5SJosef Bacik */
987966a6b5SJosef Bacik BTRFS_FS_RELOC_RUNNING,
997966a6b5SJosef Bacik
1007966a6b5SJosef Bacik /* Indicate that the cleaner thread is awake and doing something. */
1017966a6b5SJosef Bacik BTRFS_FS_CLEANER_RUNNING,
1027966a6b5SJosef Bacik
1037966a6b5SJosef Bacik /*
1047966a6b5SJosef Bacik * The checksumming has an optimized version and is considered fast,
1057966a6b5SJosef Bacik * so we don't need to offload checksums to workqueues.
1067966a6b5SJosef Bacik */
1077966a6b5SJosef Bacik BTRFS_FS_CSUM_IMPL_FAST,
1087966a6b5SJosef Bacik
1097966a6b5SJosef Bacik /* Indicate that the discard workqueue can service discards. */
1107966a6b5SJosef Bacik BTRFS_FS_DISCARD_RUNNING,
1117966a6b5SJosef Bacik
1127966a6b5SJosef Bacik /* Indicate that we need to cleanup space cache v1 */
1137966a6b5SJosef Bacik BTRFS_FS_CLEANUP_SPACE_CACHE_V1,
1147966a6b5SJosef Bacik
1157966a6b5SJosef Bacik /* Indicate that we can't trust the free space tree for caching yet */
1167966a6b5SJosef Bacik BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
1177966a6b5SJosef Bacik
1187966a6b5SJosef Bacik /* Indicate whether there are any tree modification log users */
1197966a6b5SJosef Bacik BTRFS_FS_TREE_MOD_LOG_USERS,
1207966a6b5SJosef Bacik
1217966a6b5SJosef Bacik /* Indicate that we want the transaction kthread to commit right now. */
1227966a6b5SJosef Bacik BTRFS_FS_COMMIT_TRANS,
1237966a6b5SJosef Bacik
1247966a6b5SJosef Bacik /* Indicate we have half completed snapshot deletions pending. */
1257966a6b5SJosef Bacik BTRFS_FS_UNFINISHED_DROPS,
1267966a6b5SJosef Bacik
1277966a6b5SJosef Bacik /* Indicate we have to finish a zone to do next allocation. */
1287966a6b5SJosef Bacik BTRFS_FS_NEED_ZONE_FINISH,
1297966a6b5SJosef Bacik
130c52cc7b7SJosef Bacik /* Indicate that we want to commit the transaction. */
131c52cc7b7SJosef Bacik BTRFS_FS_NEED_TRANS_COMMIT,
132c52cc7b7SJosef Bacik
133bf1f1fecSJosef Bacik /* This is set when active zone tracking is needed. */
134bf1f1fecSJosef Bacik BTRFS_FS_ACTIVE_ZONE_TRACKING,
13585e79ec7SNaohiro Aota
136b7625f46SQu Wenruo /*
137b7625f46SQu Wenruo * Indicate if we have some features changed, this is mostly for
138b7625f46SQu Wenruo * cleaner thread to update the sysfs interface.
139b7625f46SQu Wenruo */
140b7625f46SQu Wenruo BTRFS_FS_FEATURE_CHANGED,
141b7625f46SQu Wenruo
1427966a6b5SJosef Bacik #if BITS_PER_LONG == 32
1437966a6b5SJosef Bacik /* Indicate if we have error/warn message printed on 32bit systems */
1447966a6b5SJosef Bacik BTRFS_FS_32BIT_ERROR,
1457966a6b5SJosef Bacik BTRFS_FS_32BIT_WARN,
1467966a6b5SJosef Bacik #endif
1477966a6b5SJosef Bacik };
1487966a6b5SJosef Bacik
149fc97a410SJosef Bacik /*
150fc97a410SJosef Bacik * Flags for mount options.
151fc97a410SJosef Bacik *
152fc97a410SJosef Bacik * Note: don't forget to add new options to btrfs_show_options()
153fc97a410SJosef Bacik */
154fc97a410SJosef Bacik enum {
155fc97a410SJosef Bacik BTRFS_MOUNT_NODATASUM = (1UL << 0),
156fc97a410SJosef Bacik BTRFS_MOUNT_NODATACOW = (1UL << 1),
157fc97a410SJosef Bacik BTRFS_MOUNT_NOBARRIER = (1UL << 2),
158fc97a410SJosef Bacik BTRFS_MOUNT_SSD = (1UL << 3),
159fc97a410SJosef Bacik BTRFS_MOUNT_DEGRADED = (1UL << 4),
160fc97a410SJosef Bacik BTRFS_MOUNT_COMPRESS = (1UL << 5),
161fc97a410SJosef Bacik BTRFS_MOUNT_NOTREELOG = (1UL << 6),
162fc97a410SJosef Bacik BTRFS_MOUNT_FLUSHONCOMMIT = (1UL << 7),
163fc97a410SJosef Bacik BTRFS_MOUNT_SSD_SPREAD = (1UL << 8),
164fc97a410SJosef Bacik BTRFS_MOUNT_NOSSD = (1UL << 9),
165fc97a410SJosef Bacik BTRFS_MOUNT_DISCARD_SYNC = (1UL << 10),
166fc97a410SJosef Bacik BTRFS_MOUNT_FORCE_COMPRESS = (1UL << 11),
167fc97a410SJosef Bacik BTRFS_MOUNT_SPACE_CACHE = (1UL << 12),
168fc97a410SJosef Bacik BTRFS_MOUNT_CLEAR_CACHE = (1UL << 13),
169fc97a410SJosef Bacik BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED = (1UL << 14),
170fc97a410SJosef Bacik BTRFS_MOUNT_ENOSPC_DEBUG = (1UL << 15),
171fc97a410SJosef Bacik BTRFS_MOUNT_AUTO_DEFRAG = (1UL << 16),
172fc97a410SJosef Bacik BTRFS_MOUNT_USEBACKUPROOT = (1UL << 17),
173fc97a410SJosef Bacik BTRFS_MOUNT_SKIP_BALANCE = (1UL << 18),
174fc97a410SJosef Bacik BTRFS_MOUNT_CHECK_INTEGRITY = (1UL << 19),
175fc97a410SJosef Bacik BTRFS_MOUNT_CHECK_INTEGRITY_DATA = (1UL << 20),
176fc97a410SJosef Bacik BTRFS_MOUNT_PANIC_ON_FATAL_ERROR = (1UL << 21),
177fc97a410SJosef Bacik BTRFS_MOUNT_RESCAN_UUID_TREE = (1UL << 22),
178fc97a410SJosef Bacik BTRFS_MOUNT_FRAGMENT_DATA = (1UL << 23),
179fc97a410SJosef Bacik BTRFS_MOUNT_FRAGMENT_METADATA = (1UL << 24),
180fc97a410SJosef Bacik BTRFS_MOUNT_FREE_SPACE_TREE = (1UL << 25),
181fc97a410SJosef Bacik BTRFS_MOUNT_NOLOGREPLAY = (1UL << 26),
182fc97a410SJosef Bacik BTRFS_MOUNT_REF_VERIFY = (1UL << 27),
183fc97a410SJosef Bacik BTRFS_MOUNT_DISCARD_ASYNC = (1UL << 28),
184fc97a410SJosef Bacik BTRFS_MOUNT_IGNOREBADROOTS = (1UL << 29),
185fc97a410SJosef Bacik BTRFS_MOUNT_IGNOREDATACSUMS = (1UL << 30),
186fc97a410SJosef Bacik BTRFS_MOUNT_NODISCARD = (1UL << 31),
187fc97a410SJosef Bacik };
188fc97a410SJosef Bacik
189d83eb482SJosef Bacik /*
190d83eb482SJosef Bacik * Compat flags that we support. If any incompat flags are set other than the
191d83eb482SJosef Bacik * ones specified below then we will fail to mount
192d83eb482SJosef Bacik */
193d83eb482SJosef Bacik #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
194d83eb482SJosef Bacik #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
195d83eb482SJosef Bacik #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
196d83eb482SJosef Bacik
197d83eb482SJosef Bacik #define BTRFS_FEATURE_COMPAT_RO_SUPP \
198d83eb482SJosef Bacik (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
199d83eb482SJosef Bacik BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
200d83eb482SJosef Bacik BTRFS_FEATURE_COMPAT_RO_VERITY | \
201d83eb482SJosef Bacik BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
202d83eb482SJosef Bacik
203d83eb482SJosef Bacik #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
204d83eb482SJosef Bacik #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
205d83eb482SJosef Bacik
2060f202b25SAnand Jain #define BTRFS_FEATURE_INCOMPAT_SUPP_STABLE \
207d83eb482SJosef Bacik (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
208d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
209d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
210d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
211d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
212d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
213d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_RAID56 | \
214d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
215d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
216d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
217d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_METADATA_UUID | \
218d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_RAID1C34 | \
219d83eb482SJosef Bacik BTRFS_FEATURE_INCOMPAT_ZONED)
2200f202b25SAnand Jain
2210f202b25SAnand Jain #ifdef CONFIG_BTRFS_DEBUG
2220f202b25SAnand Jain /*
2230f202b25SAnand Jain * Features under developmen like Extent tree v2 support is enabled
2240f202b25SAnand Jain * only under CONFIG_BTRFS_DEBUG.
2250f202b25SAnand Jain */
2260f202b25SAnand Jain #define BTRFS_FEATURE_INCOMPAT_SUPP \
2270f202b25SAnand Jain (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE | \
2280f202b25SAnand Jain BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
2290f202b25SAnand Jain
2300f202b25SAnand Jain #else
2310f202b25SAnand Jain
2320f202b25SAnand Jain #define BTRFS_FEATURE_INCOMPAT_SUPP \
2330f202b25SAnand Jain (BTRFS_FEATURE_INCOMPAT_SUPP_STABLE)
2340f202b25SAnand Jain
235d83eb482SJosef Bacik #endif
236d83eb482SJosef Bacik
237d83eb482SJosef Bacik #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
238d83eb482SJosef Bacik (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
239d83eb482SJosef Bacik #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
240d83eb482SJosef Bacik
241fc97a410SJosef Bacik #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
242fc97a410SJosef Bacik #define BTRFS_DEFAULT_MAX_INLINE (2048)
243fc97a410SJosef Bacik
244a56159d4SJosef Bacik struct btrfs_dev_replace {
245a56159d4SJosef Bacik /* See #define above */
246a56159d4SJosef Bacik u64 replace_state;
247a56159d4SJosef Bacik /* Seconds since 1-Jan-1970 */
248a56159d4SJosef Bacik time64_t time_started;
249a56159d4SJosef Bacik /* Seconds since 1-Jan-1970 */
250a56159d4SJosef Bacik time64_t time_stopped;
251a56159d4SJosef Bacik atomic64_t num_write_errors;
252a56159d4SJosef Bacik atomic64_t num_uncorrectable_read_errors;
253a56159d4SJosef Bacik
254a56159d4SJosef Bacik u64 cursor_left;
255a56159d4SJosef Bacik u64 committed_cursor_left;
256a56159d4SJosef Bacik u64 cursor_left_last_write_of_item;
257a56159d4SJosef Bacik u64 cursor_right;
258a56159d4SJosef Bacik
259a56159d4SJosef Bacik /* See #define above */
260a56159d4SJosef Bacik u64 cont_reading_from_srcdev_mode;
261a56159d4SJosef Bacik
262a56159d4SJosef Bacik int is_valid;
263a56159d4SJosef Bacik int item_needs_writeback;
264a56159d4SJosef Bacik struct btrfs_device *srcdev;
265a56159d4SJosef Bacik struct btrfs_device *tgtdev;
266a56159d4SJosef Bacik
267a56159d4SJosef Bacik struct mutex lock_finishing_cancel_unmount;
268a56159d4SJosef Bacik struct rw_semaphore rwsem;
269a56159d4SJosef Bacik
270a56159d4SJosef Bacik struct btrfs_scrub_progress scrub_progress;
271a56159d4SJosef Bacik
272a56159d4SJosef Bacik struct percpu_counter bio_counter;
273a56159d4SJosef Bacik wait_queue_head_t replace_wait;
274*a5bc4e03SJohannes Thumshirn
275*a5bc4e03SJohannes Thumshirn struct task_struct *replace_task;
276a56159d4SJosef Bacik };
277a56159d4SJosef Bacik
278a56159d4SJosef Bacik /*
279a56159d4SJosef Bacik * Free clusters are used to claim free space in relatively large chunks,
280a56159d4SJosef Bacik * allowing us to do less seeky writes. They are used for all metadata
281a56159d4SJosef Bacik * allocations. In ssd_spread mode they are also used for data allocations.
282a56159d4SJosef Bacik */
283a56159d4SJosef Bacik struct btrfs_free_cluster {
284a56159d4SJosef Bacik spinlock_t lock;
285a56159d4SJosef Bacik spinlock_t refill_lock;
286a56159d4SJosef Bacik struct rb_root root;
287a56159d4SJosef Bacik
288a56159d4SJosef Bacik /* Largest extent in this cluster */
289a56159d4SJosef Bacik u64 max_size;
290a56159d4SJosef Bacik
291a56159d4SJosef Bacik /* First extent starting offset */
292a56159d4SJosef Bacik u64 window_start;
293a56159d4SJosef Bacik
294a56159d4SJosef Bacik /* We did a full search and couldn't create a cluster */
295a56159d4SJosef Bacik bool fragmented;
296a56159d4SJosef Bacik
297a56159d4SJosef Bacik struct btrfs_block_group *block_group;
298a56159d4SJosef Bacik /*
299a56159d4SJosef Bacik * When a cluster is allocated from a block group, we put the cluster
300a56159d4SJosef Bacik * onto a list in the block group so that it can be freed before the
301a56159d4SJosef Bacik * block group is freed.
302a56159d4SJosef Bacik */
303a56159d4SJosef Bacik struct list_head block_group_list;
304a56159d4SJosef Bacik };
305a56159d4SJosef Bacik
306a56159d4SJosef Bacik /* Discard control. */
307a56159d4SJosef Bacik /*
308a56159d4SJosef Bacik * Async discard uses multiple lists to differentiate the discard filter
309a56159d4SJosef Bacik * parameters. Index 0 is for completely free block groups where we need to
310a56159d4SJosef Bacik * ensure the entire block group is trimmed without being lossy. Indices
311a56159d4SJosef Bacik * afterwards represent monotonically decreasing discard filter sizes to
312a56159d4SJosef Bacik * prioritize what should be discarded next.
313a56159d4SJosef Bacik */
314a56159d4SJosef Bacik #define BTRFS_NR_DISCARD_LISTS 3
315a56159d4SJosef Bacik #define BTRFS_DISCARD_INDEX_UNUSED 0
316a56159d4SJosef Bacik #define BTRFS_DISCARD_INDEX_START 1
317a56159d4SJosef Bacik
318a56159d4SJosef Bacik struct btrfs_discard_ctl {
319a56159d4SJosef Bacik struct workqueue_struct *discard_workers;
320a56159d4SJosef Bacik struct delayed_work work;
321a56159d4SJosef Bacik spinlock_t lock;
322a56159d4SJosef Bacik struct btrfs_block_group *block_group;
323a56159d4SJosef Bacik struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
324a56159d4SJosef Bacik u64 prev_discard;
325a56159d4SJosef Bacik u64 prev_discard_time;
326a56159d4SJosef Bacik atomic_t discardable_extents;
327a56159d4SJosef Bacik atomic64_t discardable_bytes;
328a56159d4SJosef Bacik u64 max_discard_size;
329a56159d4SJosef Bacik u64 delay_ms;
330a56159d4SJosef Bacik u32 iops_limit;
331a56159d4SJosef Bacik u32 kbps_limit;
332a56159d4SJosef Bacik u64 discard_extent_bytes;
333a56159d4SJosef Bacik u64 discard_bitmap_bytes;
334a56159d4SJosef Bacik atomic64_t discard_bytes_saved;
335a56159d4SJosef Bacik };
336a56159d4SJosef Bacik
337a56159d4SJosef Bacik /*
338a56159d4SJosef Bacik * Exclusive operations (device replace, resize, device add/remove, balance)
339a56159d4SJosef Bacik */
340a56159d4SJosef Bacik enum btrfs_exclusive_operation {
341a56159d4SJosef Bacik BTRFS_EXCLOP_NONE,
342a56159d4SJosef Bacik BTRFS_EXCLOP_BALANCE_PAUSED,
343a56159d4SJosef Bacik BTRFS_EXCLOP_BALANCE,
344a56159d4SJosef Bacik BTRFS_EXCLOP_DEV_ADD,
345a56159d4SJosef Bacik BTRFS_EXCLOP_DEV_REMOVE,
346a56159d4SJosef Bacik BTRFS_EXCLOP_DEV_REPLACE,
347a56159d4SJosef Bacik BTRFS_EXCLOP_RESIZE,
348a56159d4SJosef Bacik BTRFS_EXCLOP_SWAP_ACTIVATE,
349a56159d4SJosef Bacik };
350a56159d4SJosef Bacik
351a56159d4SJosef Bacik /* Store data about transaction commits, exported via sysfs. */
352a56159d4SJosef Bacik struct btrfs_commit_stats {
353a56159d4SJosef Bacik /* Total number of commits */
354a56159d4SJosef Bacik u64 commit_count;
355a56159d4SJosef Bacik /* The maximum commit duration so far in ns */
356a56159d4SJosef Bacik u64 max_commit_dur;
357a56159d4SJosef Bacik /* The last commit duration in ns */
358a56159d4SJosef Bacik u64 last_commit_dur;
359a56159d4SJosef Bacik /* The total commit duration in ns */
360a56159d4SJosef Bacik u64 total_commit_dur;
361a56159d4SJosef Bacik };
362a56159d4SJosef Bacik
363a56159d4SJosef Bacik struct btrfs_fs_info {
364a56159d4SJosef Bacik u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
365a56159d4SJosef Bacik unsigned long flags;
366a56159d4SJosef Bacik struct btrfs_root *tree_root;
367a56159d4SJosef Bacik struct btrfs_root *chunk_root;
368a56159d4SJosef Bacik struct btrfs_root *dev_root;
369a56159d4SJosef Bacik struct btrfs_root *fs_root;
370a56159d4SJosef Bacik struct btrfs_root *quota_root;
371a56159d4SJosef Bacik struct btrfs_root *uuid_root;
372a56159d4SJosef Bacik struct btrfs_root *data_reloc_root;
373a56159d4SJosef Bacik struct btrfs_root *block_group_root;
374a56159d4SJosef Bacik
375a56159d4SJosef Bacik /* The log root tree is a directory of all the other log roots */
376a56159d4SJosef Bacik struct btrfs_root *log_root_tree;
377a56159d4SJosef Bacik
378a56159d4SJosef Bacik /* The tree that holds the global roots (csum, extent, etc) */
379a56159d4SJosef Bacik rwlock_t global_root_lock;
380a56159d4SJosef Bacik struct rb_root global_root_tree;
381a56159d4SJosef Bacik
382a56159d4SJosef Bacik spinlock_t fs_roots_radix_lock;
383a56159d4SJosef Bacik struct radix_tree_root fs_roots_radix;
384a56159d4SJosef Bacik
385a56159d4SJosef Bacik /* Block group cache stuff */
386a56159d4SJosef Bacik rwlock_t block_group_cache_lock;
387a56159d4SJosef Bacik struct rb_root_cached block_group_cache_tree;
388a56159d4SJosef Bacik
389a56159d4SJosef Bacik /* Keep track of unallocated space */
390a56159d4SJosef Bacik atomic64_t free_chunk_space;
391a56159d4SJosef Bacik
392a56159d4SJosef Bacik /* Track ranges which are used by log trees blocks/logged data extents */
393a56159d4SJosef Bacik struct extent_io_tree excluded_extents;
394a56159d4SJosef Bacik
395a56159d4SJosef Bacik /* logical->physical extent mapping */
396a56159d4SJosef Bacik struct extent_map_tree mapping_tree;
397a56159d4SJosef Bacik
398a56159d4SJosef Bacik /*
399a56159d4SJosef Bacik * Block reservation for extent, checksum, root tree and delayed dir
400a56159d4SJosef Bacik * index item.
401a56159d4SJosef Bacik */
402a56159d4SJosef Bacik struct btrfs_block_rsv global_block_rsv;
403a56159d4SJosef Bacik /* Block reservation for metadata operations */
404a56159d4SJosef Bacik struct btrfs_block_rsv trans_block_rsv;
405a56159d4SJosef Bacik /* Block reservation for chunk tree */
406a56159d4SJosef Bacik struct btrfs_block_rsv chunk_block_rsv;
407a56159d4SJosef Bacik /* Block reservation for delayed operations */
408a56159d4SJosef Bacik struct btrfs_block_rsv delayed_block_rsv;
409a56159d4SJosef Bacik /* Block reservation for delayed refs */
410a56159d4SJosef Bacik struct btrfs_block_rsv delayed_refs_rsv;
411a56159d4SJosef Bacik
412a56159d4SJosef Bacik struct btrfs_block_rsv empty_block_rsv;
413a56159d4SJosef Bacik
414a56159d4SJosef Bacik u64 generation;
415a56159d4SJosef Bacik u64 last_trans_committed;
416a56159d4SJosef Bacik /*
417a56159d4SJosef Bacik * Generation of the last transaction used for block group relocation
418a56159d4SJosef Bacik * since the filesystem was last mounted (or 0 if none happened yet).
419a56159d4SJosef Bacik * Must be written and read while holding btrfs_fs_info::commit_root_sem.
420a56159d4SJosef Bacik */
421a56159d4SJosef Bacik u64 last_reloc_trans;
422a56159d4SJosef Bacik
423a56159d4SJosef Bacik /*
424a56159d4SJosef Bacik * This is updated to the current trans every time a full commit is
425a56159d4SJosef Bacik * required instead of the faster short fsync log commits
426a56159d4SJosef Bacik */
427a56159d4SJosef Bacik u64 last_trans_log_full_commit;
428a56159d4SJosef Bacik unsigned long mount_opt;
429a56159d4SJosef Bacik
430a56159d4SJosef Bacik unsigned long compress_type:4;
431a56159d4SJosef Bacik unsigned int compress_level;
432a56159d4SJosef Bacik u32 commit_interval;
433a56159d4SJosef Bacik /*
434a56159d4SJosef Bacik * It is a suggestive number, the read side is safe even it gets a
435a56159d4SJosef Bacik * wrong number because we will write out the data into a regular
436a56159d4SJosef Bacik * extent. The write side(mount/remount) is under ->s_umount lock,
437a56159d4SJosef Bacik * so it is also safe.
438a56159d4SJosef Bacik */
439a56159d4SJosef Bacik u64 max_inline;
440a56159d4SJosef Bacik
441a56159d4SJosef Bacik struct btrfs_transaction *running_transaction;
442a56159d4SJosef Bacik wait_queue_head_t transaction_throttle;
443a56159d4SJosef Bacik wait_queue_head_t transaction_wait;
444a56159d4SJosef Bacik wait_queue_head_t transaction_blocked_wait;
445a56159d4SJosef Bacik wait_queue_head_t async_submit_wait;
446a56159d4SJosef Bacik
447a56159d4SJosef Bacik /*
448a56159d4SJosef Bacik * Used to protect the incompat_flags, compat_flags, compat_ro_flags
449a56159d4SJosef Bacik * when they are updated.
450a56159d4SJosef Bacik *
451a56159d4SJosef Bacik * Because we do not clear the flags for ever, so we needn't use
452a56159d4SJosef Bacik * the lock on the read side.
453a56159d4SJosef Bacik *
454a56159d4SJosef Bacik * We also needn't use the lock when we mount the fs, because
455a56159d4SJosef Bacik * there is no other task which will update the flag.
456a56159d4SJosef Bacik */
457a56159d4SJosef Bacik spinlock_t super_lock;
458a56159d4SJosef Bacik struct btrfs_super_block *super_copy;
459a56159d4SJosef Bacik struct btrfs_super_block *super_for_commit;
460a56159d4SJosef Bacik struct super_block *sb;
461a56159d4SJosef Bacik struct inode *btree_inode;
462a56159d4SJosef Bacik struct mutex tree_log_mutex;
463a56159d4SJosef Bacik struct mutex transaction_kthread_mutex;
464a56159d4SJosef Bacik struct mutex cleaner_mutex;
465a56159d4SJosef Bacik struct mutex chunk_mutex;
466a56159d4SJosef Bacik
467a56159d4SJosef Bacik /*
468a56159d4SJosef Bacik * This is taken to make sure we don't set block groups ro after the
469a56159d4SJosef Bacik * free space cache has been allocated on them.
470a56159d4SJosef Bacik */
471a56159d4SJosef Bacik struct mutex ro_block_group_mutex;
472a56159d4SJosef Bacik
473a56159d4SJosef Bacik /*
474a56159d4SJosef Bacik * This is used during read/modify/write to make sure no two ios are
475a56159d4SJosef Bacik * trying to mod the same stripe at the same time.
476a56159d4SJosef Bacik */
477a56159d4SJosef Bacik struct btrfs_stripe_hash_table *stripe_hash_table;
478a56159d4SJosef Bacik
479a56159d4SJosef Bacik /*
480a56159d4SJosef Bacik * This protects the ordered operations list only while we are
481a56159d4SJosef Bacik * processing all of the entries on it. This way we make sure the
482a56159d4SJosef Bacik * commit code doesn't find the list temporarily empty because another
483a56159d4SJosef Bacik * function happens to be doing non-waiting preflush before jumping
484a56159d4SJosef Bacik * into the main commit.
485a56159d4SJosef Bacik */
486a56159d4SJosef Bacik struct mutex ordered_operations_mutex;
487a56159d4SJosef Bacik
488a56159d4SJosef Bacik struct rw_semaphore commit_root_sem;
489a56159d4SJosef Bacik
490a56159d4SJosef Bacik struct rw_semaphore cleanup_work_sem;
491a56159d4SJosef Bacik
492a56159d4SJosef Bacik struct rw_semaphore subvol_sem;
493a56159d4SJosef Bacik
494a56159d4SJosef Bacik spinlock_t trans_lock;
495a56159d4SJosef Bacik /*
496a56159d4SJosef Bacik * The reloc mutex goes with the trans lock, it is taken during commit
497a56159d4SJosef Bacik * to protect us from the relocation code.
498a56159d4SJosef Bacik */
499a56159d4SJosef Bacik struct mutex reloc_mutex;
500a56159d4SJosef Bacik
501a56159d4SJosef Bacik struct list_head trans_list;
502a56159d4SJosef Bacik struct list_head dead_roots;
503a56159d4SJosef Bacik struct list_head caching_block_groups;
504a56159d4SJosef Bacik
505a56159d4SJosef Bacik spinlock_t delayed_iput_lock;
506a56159d4SJosef Bacik struct list_head delayed_iputs;
507a56159d4SJosef Bacik atomic_t nr_delayed_iputs;
508a56159d4SJosef Bacik wait_queue_head_t delayed_iputs_wait;
509a56159d4SJosef Bacik
510a56159d4SJosef Bacik atomic64_t tree_mod_seq;
511a56159d4SJosef Bacik
512a56159d4SJosef Bacik /* This protects tree_mod_log and tree_mod_seq_list */
513a56159d4SJosef Bacik rwlock_t tree_mod_log_lock;
514a56159d4SJosef Bacik struct rb_root tree_mod_log;
515a56159d4SJosef Bacik struct list_head tree_mod_seq_list;
516a56159d4SJosef Bacik
517a56159d4SJosef Bacik atomic_t async_delalloc_pages;
518a56159d4SJosef Bacik
519a56159d4SJosef Bacik /* This is used to protect the following list -- ordered_roots. */
520a56159d4SJosef Bacik spinlock_t ordered_root_lock;
521a56159d4SJosef Bacik
522a56159d4SJosef Bacik /*
523a56159d4SJosef Bacik * All fs/file tree roots in which there are data=ordered extents
524a56159d4SJosef Bacik * pending writeback are added into this list.
525a56159d4SJosef Bacik *
526a56159d4SJosef Bacik * These can span multiple transactions and basically include every
527a56159d4SJosef Bacik * dirty data page that isn't from nodatacow.
528a56159d4SJosef Bacik */
529a56159d4SJosef Bacik struct list_head ordered_roots;
530a56159d4SJosef Bacik
531a56159d4SJosef Bacik struct mutex delalloc_root_mutex;
532a56159d4SJosef Bacik spinlock_t delalloc_root_lock;
533a56159d4SJosef Bacik /* All fs/file tree roots that have delalloc inodes. */
534a56159d4SJosef Bacik struct list_head delalloc_roots;
535a56159d4SJosef Bacik
536a56159d4SJosef Bacik /*
537a56159d4SJosef Bacik * There is a pool of worker threads for checksumming during writes and
538a56159d4SJosef Bacik * a pool for checksumming after reads. This is because readers can
539a56159d4SJosef Bacik * run with FS locks held, and the writers may be waiting for those
540a56159d4SJosef Bacik * locks. We don't want ordering in the pending list to cause
541a56159d4SJosef Bacik * deadlocks, and so the two are serviced separately.
542a56159d4SJosef Bacik *
543a56159d4SJosef Bacik * A third pool does submit_bio to avoid deadlocking with the other two.
544a56159d4SJosef Bacik */
545a56159d4SJosef Bacik struct btrfs_workqueue *workers;
546a56159d4SJosef Bacik struct btrfs_workqueue *delalloc_workers;
547a56159d4SJosef Bacik struct btrfs_workqueue *flush_workers;
548a56159d4SJosef Bacik struct workqueue_struct *endio_workers;
549a56159d4SJosef Bacik struct workqueue_struct *endio_meta_workers;
550a56159d4SJosef Bacik struct workqueue_struct *rmw_workers;
551a56159d4SJosef Bacik struct workqueue_struct *compressed_write_workers;
552a56159d4SJosef Bacik struct btrfs_workqueue *endio_write_workers;
553a56159d4SJosef Bacik struct btrfs_workqueue *endio_freespace_worker;
554a56159d4SJosef Bacik struct btrfs_workqueue *caching_workers;
555a56159d4SJosef Bacik
556a56159d4SJosef Bacik /*
557a56159d4SJosef Bacik * Fixup workers take dirty pages that didn't properly go through the
558a56159d4SJosef Bacik * cow mechanism and make them safe to write. It happens for the
559a56159d4SJosef Bacik * sys_munmap function call path.
560a56159d4SJosef Bacik */
561a56159d4SJosef Bacik struct btrfs_workqueue *fixup_workers;
562a56159d4SJosef Bacik struct btrfs_workqueue *delayed_workers;
563a56159d4SJosef Bacik
564a56159d4SJosef Bacik struct task_struct *transaction_kthread;
565a56159d4SJosef Bacik struct task_struct *cleaner_kthread;
566a56159d4SJosef Bacik u32 thread_pool_size;
567a56159d4SJosef Bacik
568a56159d4SJosef Bacik struct kobject *space_info_kobj;
569a56159d4SJosef Bacik struct kobject *qgroups_kobj;
570a56159d4SJosef Bacik struct kobject *discard_kobj;
571a56159d4SJosef Bacik
572a56159d4SJosef Bacik /* Used to keep from writing metadata until there is a nice batch */
573a56159d4SJosef Bacik struct percpu_counter dirty_metadata_bytes;
574a56159d4SJosef Bacik struct percpu_counter delalloc_bytes;
575a56159d4SJosef Bacik struct percpu_counter ordered_bytes;
576a56159d4SJosef Bacik s32 dirty_metadata_batch;
577a56159d4SJosef Bacik s32 delalloc_batch;
578a56159d4SJosef Bacik
57908eb2ad9SFilipe Manana /* Protected by 'trans_lock'. */
580a56159d4SJosef Bacik struct list_head dirty_cowonly_roots;
581a56159d4SJosef Bacik
582a56159d4SJosef Bacik struct btrfs_fs_devices *fs_devices;
583a56159d4SJosef Bacik
584a56159d4SJosef Bacik /*
585a56159d4SJosef Bacik * The space_info list is effectively read only after initial setup.
586a56159d4SJosef Bacik * It is populated at mount time and cleaned up after all block groups
587a56159d4SJosef Bacik * are removed. RCU is used to protect it.
588a56159d4SJosef Bacik */
589a56159d4SJosef Bacik struct list_head space_info;
590a56159d4SJosef Bacik
591a56159d4SJosef Bacik struct btrfs_space_info *data_sinfo;
592a56159d4SJosef Bacik
593a56159d4SJosef Bacik struct reloc_control *reloc_ctl;
594a56159d4SJosef Bacik
595a56159d4SJosef Bacik /* data_alloc_cluster is only used in ssd_spread mode */
596a56159d4SJosef Bacik struct btrfs_free_cluster data_alloc_cluster;
597a56159d4SJosef Bacik
598a56159d4SJosef Bacik /* All metadata allocations go through this cluster. */
599a56159d4SJosef Bacik struct btrfs_free_cluster meta_alloc_cluster;
600a56159d4SJosef Bacik
601a56159d4SJosef Bacik /* Auto defrag inodes go here. */
602a56159d4SJosef Bacik spinlock_t defrag_inodes_lock;
603a56159d4SJosef Bacik struct rb_root defrag_inodes;
604a56159d4SJosef Bacik atomic_t defrag_running;
605a56159d4SJosef Bacik
606a56159d4SJosef Bacik /* Used to protect avail_{data, metadata, system}_alloc_bits */
607a56159d4SJosef Bacik seqlock_t profiles_lock;
608a56159d4SJosef Bacik /*
609a56159d4SJosef Bacik * These three are in extended format (availability of single chunks is
610a56159d4SJosef Bacik * denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other types are denoted
611a56159d4SJosef Bacik * by corresponding BTRFS_BLOCK_GROUP_* bits)
612a56159d4SJosef Bacik */
613a56159d4SJosef Bacik u64 avail_data_alloc_bits;
614a56159d4SJosef Bacik u64 avail_metadata_alloc_bits;
615a56159d4SJosef Bacik u64 avail_system_alloc_bits;
616a56159d4SJosef Bacik
617a56159d4SJosef Bacik /* Balance state */
618a56159d4SJosef Bacik spinlock_t balance_lock;
619a56159d4SJosef Bacik struct mutex balance_mutex;
620a56159d4SJosef Bacik atomic_t balance_pause_req;
621a56159d4SJosef Bacik atomic_t balance_cancel_req;
622a56159d4SJosef Bacik struct btrfs_balance_control *balance_ctl;
623a56159d4SJosef Bacik wait_queue_head_t balance_wait_q;
624a56159d4SJosef Bacik
625a56159d4SJosef Bacik /* Cancellation requests for chunk relocation */
626a56159d4SJosef Bacik atomic_t reloc_cancel_req;
627a56159d4SJosef Bacik
628a56159d4SJosef Bacik u32 data_chunk_allocations;
629a56159d4SJosef Bacik u32 metadata_ratio;
630a56159d4SJosef Bacik
631a56159d4SJosef Bacik void *bdev_holder;
632a56159d4SJosef Bacik
633a56159d4SJosef Bacik /* Private scrub information */
634a56159d4SJosef Bacik struct mutex scrub_lock;
635a56159d4SJosef Bacik atomic_t scrubs_running;
636a56159d4SJosef Bacik atomic_t scrub_pause_req;
637a56159d4SJosef Bacik atomic_t scrubs_paused;
638a56159d4SJosef Bacik atomic_t scrub_cancel_req;
639a56159d4SJosef Bacik wait_queue_head_t scrub_pause_wait;
640a56159d4SJosef Bacik /*
641a56159d4SJosef Bacik * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
642a56159d4SJosef Bacik * running.
643a56159d4SJosef Bacik */
644a56159d4SJosef Bacik refcount_t scrub_workers_refcnt;
645a56159d4SJosef Bacik struct workqueue_struct *scrub_workers;
646a56159d4SJosef Bacik struct btrfs_subpage_info *subpage_info;
647a56159d4SJosef Bacik
648a56159d4SJosef Bacik struct btrfs_discard_ctl discard_ctl;
649a56159d4SJosef Bacik
650a56159d4SJosef Bacik #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
651a56159d4SJosef Bacik u32 check_integrity_print_mask;
652a56159d4SJosef Bacik #endif
653a56159d4SJosef Bacik /* Is qgroup tracking in a consistent state? */
654a56159d4SJosef Bacik u64 qgroup_flags;
655a56159d4SJosef Bacik
656a56159d4SJosef Bacik /* Holds configuration and tracking. Protected by qgroup_lock. */
657a56159d4SJosef Bacik struct rb_root qgroup_tree;
658a56159d4SJosef Bacik spinlock_t qgroup_lock;
659a56159d4SJosef Bacik
660a56159d4SJosef Bacik /*
661a56159d4SJosef Bacik * Used to avoid frequently calling ulist_alloc()/ulist_free()
662a56159d4SJosef Bacik * when doing qgroup accounting, it must be protected by qgroup_lock.
663a56159d4SJosef Bacik */
664a56159d4SJosef Bacik struct ulist *qgroup_ulist;
665a56159d4SJosef Bacik
666a56159d4SJosef Bacik /*
667a56159d4SJosef Bacik * Protect user change for quota operations. If a transaction is needed,
668a56159d4SJosef Bacik * it must be started before locking this lock.
669a56159d4SJosef Bacik */
670a56159d4SJosef Bacik struct mutex qgroup_ioctl_lock;
671a56159d4SJosef Bacik
672a56159d4SJosef Bacik /* List of dirty qgroups to be written at next commit. */
673a56159d4SJosef Bacik struct list_head dirty_qgroups;
674a56159d4SJosef Bacik
675a56159d4SJosef Bacik /* Used by qgroup for an efficient tree traversal. */
676a56159d4SJosef Bacik u64 qgroup_seq;
677a56159d4SJosef Bacik
678a56159d4SJosef Bacik /* Qgroup rescan items. */
679a56159d4SJosef Bacik /* Protects the progress item */
680a56159d4SJosef Bacik struct mutex qgroup_rescan_lock;
681a56159d4SJosef Bacik struct btrfs_key qgroup_rescan_progress;
682a56159d4SJosef Bacik struct btrfs_workqueue *qgroup_rescan_workers;
683a56159d4SJosef Bacik struct completion qgroup_rescan_completion;
684a56159d4SJosef Bacik struct btrfs_work qgroup_rescan_work;
685a56159d4SJosef Bacik /* Protected by qgroup_rescan_lock */
686a56159d4SJosef Bacik bool qgroup_rescan_running;
687a56159d4SJosef Bacik u8 qgroup_drop_subtree_thres;
688a56159d4SJosef Bacik
689ae3364e5SFilipe Manana /*
690ae3364e5SFilipe Manana * If this is not 0, then it indicates a serious filesystem error has
691ae3364e5SFilipe Manana * happened and it contains that error (negative errno value).
692ae3364e5SFilipe Manana */
693ae3364e5SFilipe Manana int fs_error;
694ae3364e5SFilipe Manana
695a56159d4SJosef Bacik /* Filesystem state */
696a56159d4SJosef Bacik unsigned long fs_state;
697a56159d4SJosef Bacik
698a56159d4SJosef Bacik struct btrfs_delayed_root *delayed_root;
699a56159d4SJosef Bacik
700a56159d4SJosef Bacik /* Extent buffer radix tree */
701a56159d4SJosef Bacik spinlock_t buffer_lock;
702a56159d4SJosef Bacik /* Entries are eb->start / sectorsize */
703a56159d4SJosef Bacik struct radix_tree_root buffer_radix;
704a56159d4SJosef Bacik
705a56159d4SJosef Bacik /* Next backup root to be overwritten */
706a56159d4SJosef Bacik int backup_root_index;
707a56159d4SJosef Bacik
708a56159d4SJosef Bacik /* Device replace state */
709a56159d4SJosef Bacik struct btrfs_dev_replace dev_replace;
710a56159d4SJosef Bacik
711a56159d4SJosef Bacik struct semaphore uuid_tree_rescan_sem;
712a56159d4SJosef Bacik
713a56159d4SJosef Bacik /* Used to reclaim the metadata space in the background. */
714a56159d4SJosef Bacik struct work_struct async_reclaim_work;
715a56159d4SJosef Bacik struct work_struct async_data_reclaim_work;
716a56159d4SJosef Bacik struct work_struct preempt_reclaim_work;
717a56159d4SJosef Bacik
718a56159d4SJosef Bacik /* Reclaim partially filled block groups in the background */
719a56159d4SJosef Bacik struct work_struct reclaim_bgs_work;
720a56159d4SJosef Bacik struct list_head reclaim_bgs;
721a56159d4SJosef Bacik int bg_reclaim_threshold;
722a56159d4SJosef Bacik
723a56159d4SJosef Bacik spinlock_t unused_bgs_lock;
724a56159d4SJosef Bacik struct list_head unused_bgs;
725a56159d4SJosef Bacik struct mutex unused_bg_unpin_mutex;
726a56159d4SJosef Bacik /* Protect block groups that are going to be deleted */
727a56159d4SJosef Bacik struct mutex reclaim_bgs_lock;
728a56159d4SJosef Bacik
729a56159d4SJosef Bacik /* Cached block sizes */
730a56159d4SJosef Bacik u32 nodesize;
731a56159d4SJosef Bacik u32 sectorsize;
732a56159d4SJosef Bacik /* ilog2 of sectorsize, use to avoid 64bit division */
733a56159d4SJosef Bacik u32 sectorsize_bits;
734a56159d4SJosef Bacik u32 csum_size;
735a56159d4SJosef Bacik u32 csums_per_leaf;
736a56159d4SJosef Bacik u32 stripesize;
737a56159d4SJosef Bacik
738a56159d4SJosef Bacik /*
739a56159d4SJosef Bacik * Maximum size of an extent. BTRFS_MAX_EXTENT_SIZE on regular
740a56159d4SJosef Bacik * filesystem, on zoned it depends on the device constraints.
741a56159d4SJosef Bacik */
742a56159d4SJosef Bacik u64 max_extent_size;
743a56159d4SJosef Bacik
744a56159d4SJosef Bacik /* Block groups and devices containing active swapfiles. */
745a56159d4SJosef Bacik spinlock_t swapfile_pins_lock;
746a56159d4SJosef Bacik struct rb_root swapfile_pins;
747a56159d4SJosef Bacik
748a56159d4SJosef Bacik struct crypto_shash *csum_shash;
749a56159d4SJosef Bacik
750a56159d4SJosef Bacik /* Type of exclusive operation running, protected by super_lock */
751a56159d4SJosef Bacik enum btrfs_exclusive_operation exclusive_operation;
752a56159d4SJosef Bacik
753a56159d4SJosef Bacik /*
754a56159d4SJosef Bacik * Zone size > 0 when in ZONED mode, otherwise it's used for a check
755a56159d4SJosef Bacik * if the mode is enabled
756a56159d4SJosef Bacik */
757a56159d4SJosef Bacik u64 zone_size;
758a56159d4SJosef Bacik
759243cf8d1SChristoph Hellwig /* Constraints for ZONE_APPEND commands: */
760243cf8d1SChristoph Hellwig struct queue_limits limits;
761a56159d4SJosef Bacik u64 max_zone_append_size;
762243cf8d1SChristoph Hellwig
763a56159d4SJosef Bacik struct mutex zoned_meta_io_lock;
764a56159d4SJosef Bacik spinlock_t treelog_bg_lock;
765a56159d4SJosef Bacik u64 treelog_bg;
766a56159d4SJosef Bacik
767a56159d4SJosef Bacik /*
768a56159d4SJosef Bacik * Start of the dedicated data relocation block group, protected by
769a56159d4SJosef Bacik * relocation_bg_lock.
770a56159d4SJosef Bacik */
771a56159d4SJosef Bacik spinlock_t relocation_bg_lock;
772a56159d4SJosef Bacik u64 data_reloc_bg;
773a56159d4SJosef Bacik struct mutex zoned_data_reloc_io_lock;
774a56159d4SJosef Bacik
77513bb483dSNaohiro Aota struct btrfs_block_group *active_meta_bg;
77613bb483dSNaohiro Aota struct btrfs_block_group *active_system_bg;
77713bb483dSNaohiro Aota
778a56159d4SJosef Bacik u64 nr_global_roots;
779a56159d4SJosef Bacik
780a56159d4SJosef Bacik spinlock_t zone_active_bgs_lock;
781a56159d4SJosef Bacik struct list_head zone_active_bgs;
782a56159d4SJosef Bacik
783a56159d4SJosef Bacik /* Updates are not protected by any lock */
784a56159d4SJosef Bacik struct btrfs_commit_stats commit_stats;
785a56159d4SJosef Bacik
786a56159d4SJosef Bacik /*
787a56159d4SJosef Bacik * Last generation where we dropped a non-relocation root.
788a56159d4SJosef Bacik * Use btrfs_set_last_root_drop_gen() and btrfs_get_last_root_drop_gen()
789a56159d4SJosef Bacik * to change it and to read it, respectively.
790a56159d4SJosef Bacik */
791a56159d4SJosef Bacik u64 last_root_drop_gen;
792a56159d4SJosef Bacik
793a56159d4SJosef Bacik /*
794a56159d4SJosef Bacik * Annotations for transaction events (structures are empty when
795a56159d4SJosef Bacik * compiled without lockdep).
796a56159d4SJosef Bacik */
797a56159d4SJosef Bacik struct lockdep_map btrfs_trans_num_writers_map;
798a56159d4SJosef Bacik struct lockdep_map btrfs_trans_num_extwriters_map;
799a56159d4SJosef Bacik struct lockdep_map btrfs_state_change_map[4];
800a56159d4SJosef Bacik struct lockdep_map btrfs_trans_pending_ordered_map;
801a56159d4SJosef Bacik struct lockdep_map btrfs_ordered_extent_map;
802a56159d4SJosef Bacik
803a56159d4SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
804a56159d4SJosef Bacik spinlock_t ref_verify_lock;
805a56159d4SJosef Bacik struct rb_root block_tree;
806a56159d4SJosef Bacik #endif
807a56159d4SJosef Bacik
808a56159d4SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
809a56159d4SJosef Bacik struct kobject *debug_kobj;
810a56159d4SJosef Bacik struct list_head allocated_roots;
811a56159d4SJosef Bacik
812a56159d4SJosef Bacik spinlock_t eb_leak_lock;
813a56159d4SJosef Bacik struct list_head allocated_ebs;
814a56159d4SJosef Bacik #endif
815a56159d4SJosef Bacik };
816a56159d4SJosef Bacik
btrfs_set_last_root_drop_gen(struct btrfs_fs_info * fs_info,u64 gen)817a56159d4SJosef Bacik static inline void btrfs_set_last_root_drop_gen(struct btrfs_fs_info *fs_info,
818a56159d4SJosef Bacik u64 gen)
819a56159d4SJosef Bacik {
820a56159d4SJosef Bacik WRITE_ONCE(fs_info->last_root_drop_gen, gen);
821a56159d4SJosef Bacik }
822a56159d4SJosef Bacik
btrfs_get_last_root_drop_gen(const struct btrfs_fs_info * fs_info)823a56159d4SJosef Bacik static inline u64 btrfs_get_last_root_drop_gen(const struct btrfs_fs_info *fs_info)
824a56159d4SJosef Bacik {
825a56159d4SJosef Bacik return READ_ONCE(fs_info->last_root_drop_gen);
826a56159d4SJosef Bacik }
827a56159d4SJosef Bacik
828a56159d4SJosef Bacik /*
829a56159d4SJosef Bacik * Take the number of bytes to be checksummed and figure out how many leaves
830a56159d4SJosef Bacik * it would require to store the csums for that many bytes.
831a56159d4SJosef Bacik */
btrfs_csum_bytes_to_leaves(const struct btrfs_fs_info * fs_info,u64 csum_bytes)832a56159d4SJosef Bacik static inline u64 btrfs_csum_bytes_to_leaves(
833a56159d4SJosef Bacik const struct btrfs_fs_info *fs_info, u64 csum_bytes)
834a56159d4SJosef Bacik {
835a56159d4SJosef Bacik const u64 num_csums = csum_bytes >> fs_info->sectorsize_bits;
836a56159d4SJosef Bacik
837a56159d4SJosef Bacik return DIV_ROUND_UP_ULL(num_csums, fs_info->csums_per_leaf);
838a56159d4SJosef Bacik }
839a56159d4SJosef Bacik
840a56159d4SJosef Bacik /*
841a56159d4SJosef Bacik * Use this if we would be adding new items, as we could split nodes as we cow
842a56159d4SJosef Bacik * down the tree.
843a56159d4SJosef Bacik */
btrfs_calc_insert_metadata_size(const struct btrfs_fs_info * fs_info,unsigned num_items)844d1085c9cSFilipe Manana static inline u64 btrfs_calc_insert_metadata_size(const struct btrfs_fs_info *fs_info,
845a56159d4SJosef Bacik unsigned num_items)
846a56159d4SJosef Bacik {
847a56159d4SJosef Bacik return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
848a56159d4SJosef Bacik }
849a56159d4SJosef Bacik
850a56159d4SJosef Bacik /*
851a56159d4SJosef Bacik * Doing a truncate or a modification won't result in new nodes or leaves, just
852a56159d4SJosef Bacik * what we need for COW.
853a56159d4SJosef Bacik */
btrfs_calc_metadata_size(const struct btrfs_fs_info * fs_info,unsigned num_items)854d1085c9cSFilipe Manana static inline u64 btrfs_calc_metadata_size(const struct btrfs_fs_info *fs_info,
855a56159d4SJosef Bacik unsigned num_items)
856a56159d4SJosef Bacik {
857a56159d4SJosef Bacik return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
858a56159d4SJosef Bacik }
859a56159d4SJosef Bacik
860a56159d4SJosef Bacik #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
861a56159d4SJosef Bacik sizeof(struct btrfs_item))
862a56159d4SJosef Bacik
btrfs_is_zoned(const struct btrfs_fs_info * fs_info)863a56159d4SJosef Bacik static inline bool btrfs_is_zoned(const struct btrfs_fs_info *fs_info)
864a56159d4SJosef Bacik {
865dd8b7b04SChristoph Hellwig return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && fs_info->zone_size > 0;
866a56159d4SJosef Bacik }
867a56159d4SJosef Bacik
868a56159d4SJosef Bacik /*
869a56159d4SJosef Bacik * Count how many fs_info->max_extent_size cover the @size
870a56159d4SJosef Bacik */
count_max_extents(struct btrfs_fs_info * fs_info,u64 size)871a56159d4SJosef Bacik static inline u32 count_max_extents(struct btrfs_fs_info *fs_info, u64 size)
872a56159d4SJosef Bacik {
873a56159d4SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
874a56159d4SJosef Bacik if (!fs_info)
875a56159d4SJosef Bacik return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
876a56159d4SJosef Bacik #endif
877a56159d4SJosef Bacik
878a56159d4SJosef Bacik return div_u64(size + fs_info->max_extent_size - 1, fs_info->max_extent_size);
879a56159d4SJosef Bacik }
880a56159d4SJosef Bacik
881a56159d4SJosef Bacik bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
882a56159d4SJosef Bacik enum btrfs_exclusive_operation type);
883a56159d4SJosef Bacik bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
884a56159d4SJosef Bacik enum btrfs_exclusive_operation type);
885a56159d4SJosef Bacik void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info);
886a56159d4SJosef Bacik void btrfs_exclop_finish(struct btrfs_fs_info *fs_info);
887a56159d4SJosef Bacik void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
888a56159d4SJosef Bacik enum btrfs_exclusive_operation op);
889a56159d4SJosef Bacik
890c7f13d42SJosef Bacik /* Compatibility and incompatibility defines */
891c7f13d42SJosef Bacik void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
892c7f13d42SJosef Bacik const char *name);
893c7f13d42SJosef Bacik void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag,
894c7f13d42SJosef Bacik const char *name);
895c7f13d42SJosef Bacik void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
896c7f13d42SJosef Bacik const char *name);
897c7f13d42SJosef Bacik void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag,
898c7f13d42SJosef Bacik const char *name);
899c7f13d42SJosef Bacik
9000d3a9cf8SJosef Bacik #define __btrfs_fs_incompat(fs_info, flags) \
9010d3a9cf8SJosef Bacik (!!(btrfs_super_incompat_flags((fs_info)->super_copy) & (flags)))
9020d3a9cf8SJosef Bacik
9030d3a9cf8SJosef Bacik #define __btrfs_fs_compat_ro(fs_info, flags) \
9040d3a9cf8SJosef Bacik (!!(btrfs_super_compat_ro_flags((fs_info)->super_copy) & (flags)))
9050d3a9cf8SJosef Bacik
906c7f13d42SJosef Bacik #define btrfs_set_fs_incompat(__fs_info, opt) \
907c7f13d42SJosef Bacik __btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
908c7f13d42SJosef Bacik
909c7f13d42SJosef Bacik #define btrfs_clear_fs_incompat(__fs_info, opt) \
910c7f13d42SJosef Bacik __btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, #opt)
911c7f13d42SJosef Bacik
912c7f13d42SJosef Bacik #define btrfs_fs_incompat(fs_info, opt) \
913c7f13d42SJosef Bacik __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
914c7f13d42SJosef Bacik
915c7f13d42SJosef Bacik #define btrfs_set_fs_compat_ro(__fs_info, opt) \
916c7f13d42SJosef Bacik __btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
917c7f13d42SJosef Bacik
918c7f13d42SJosef Bacik #define btrfs_clear_fs_compat_ro(__fs_info, opt) \
919c7f13d42SJosef Bacik __btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, #opt)
920c7f13d42SJosef Bacik
921c7f13d42SJosef Bacik #define btrfs_fs_compat_ro(fs_info, opt) \
922c7f13d42SJosef Bacik __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
923c7f13d42SJosef Bacik
924fc97a410SJosef Bacik #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
925fc97a410SJosef Bacik #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
926fc97a410SJosef Bacik #define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
927fc97a410SJosef Bacik #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
928fc97a410SJosef Bacik BTRFS_MOUNT_##opt)
929fc97a410SJosef Bacik
930fc97a410SJosef Bacik #define btrfs_set_and_info(fs_info, opt, fmt, args...) \
931fc97a410SJosef Bacik do { \
932fc97a410SJosef Bacik if (!btrfs_test_opt(fs_info, opt)) \
933fc97a410SJosef Bacik btrfs_info(fs_info, fmt, ##args); \
934fc97a410SJosef Bacik btrfs_set_opt(fs_info->mount_opt, opt); \
935fc97a410SJosef Bacik } while (0)
936fc97a410SJosef Bacik
937fc97a410SJosef Bacik #define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
938fc97a410SJosef Bacik do { \
939fc97a410SJosef Bacik if (btrfs_test_opt(fs_info, opt)) \
940fc97a410SJosef Bacik btrfs_info(fs_info, fmt, ##args); \
941fc97a410SJosef Bacik btrfs_clear_opt(fs_info->mount_opt, opt); \
942fc97a410SJosef Bacik } while (0)
943fc97a410SJosef Bacik
btrfs_fs_closing(struct btrfs_fs_info * fs_info)944c7f13d42SJosef Bacik static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
945c7f13d42SJosef Bacik {
946c7f13d42SJosef Bacik /* Do it this way so we only ever do one test_bit in the normal case. */
947c7f13d42SJosef Bacik if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
948c7f13d42SJosef Bacik if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
949c7f13d42SJosef Bacik return 2;
950c7f13d42SJosef Bacik return 1;
951c7f13d42SJosef Bacik }
952c7f13d42SJosef Bacik return 0;
953c7f13d42SJosef Bacik }
954c7f13d42SJosef Bacik
955c7f13d42SJosef Bacik /*
956c7f13d42SJosef Bacik * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
957c7f13d42SJosef Bacik * anything except sleeping. This function is used to check the status of
958c7f13d42SJosef Bacik * the fs.
959c7f13d42SJosef Bacik * We check for BTRFS_FS_STATE_RO to avoid races with a concurrent remount,
960c7f13d42SJosef Bacik * since setting and checking for SB_RDONLY in the superblock's flags is not
961c7f13d42SJosef Bacik * atomic.
962c7f13d42SJosef Bacik */
btrfs_need_cleaner_sleep(struct btrfs_fs_info * fs_info)963c7f13d42SJosef Bacik static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
964c7f13d42SJosef Bacik {
965c7f13d42SJosef Bacik return test_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state) ||
966c7f13d42SJosef Bacik btrfs_fs_closing(fs_info);
967c7f13d42SJosef Bacik }
968c7f13d42SJosef Bacik
btrfs_wake_unfinished_drop(struct btrfs_fs_info * fs_info)9697966a6b5SJosef Bacik static inline void btrfs_wake_unfinished_drop(struct btrfs_fs_info *fs_info)
9707966a6b5SJosef Bacik {
9717966a6b5SJosef Bacik clear_and_wake_up_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags);
9727966a6b5SJosef Bacik }
9737966a6b5SJosef Bacik
974ae3364e5SFilipe Manana #define BTRFS_FS_ERROR(fs_info) (READ_ONCE((fs_info)->fs_error))
975ae3364e5SFilipe Manana
976ec8eb376SJosef Bacik #define BTRFS_FS_LOG_CLEANUP_ERROR(fs_info) \
977ec8eb376SJosef Bacik (unlikely(test_bit(BTRFS_FS_STATE_LOG_CLEANUP_ERROR, \
978ec8eb376SJosef Bacik &(fs_info)->fs_state)))
979ec8eb376SJosef Bacik
980ec8eb376SJosef Bacik #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9816a6b4dafSJosef Bacik
9826a6b4dafSJosef Bacik #define EXPORT_FOR_TESTS
9836a6b4dafSJosef Bacik
btrfs_is_testing(struct btrfs_fs_info * fs_info)984ec8eb376SJosef Bacik static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
985ec8eb376SJosef Bacik {
986ec8eb376SJosef Bacik return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
987ec8eb376SJosef Bacik }
9886a6b4dafSJosef Bacik
9896a6b4dafSJosef Bacik void btrfs_test_destroy_inode(struct inode *inode);
9906a6b4dafSJosef Bacik
991ec8eb376SJosef Bacik #else
9926a6b4dafSJosef Bacik
9936a6b4dafSJosef Bacik #define EXPORT_FOR_TESTS static
9946a6b4dafSJosef Bacik
btrfs_is_testing(struct btrfs_fs_info * fs_info)995ec8eb376SJosef Bacik static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
996ec8eb376SJosef Bacik {
997ec8eb376SJosef Bacik return 0;
998ec8eb376SJosef Bacik }
999ec8eb376SJosef Bacik #endif
1000ec8eb376SJosef Bacik
1001c7f13d42SJosef Bacik #endif
1002