xref: /openbmc/linux/fs/btrfs/space-info.h (revision f04fbcc64e4be16185151f9fca44ea1b3d074bd0)
18719aaaeSJosef Bacik /* SPDX-License-Identifier: GPL-2.0 */
28719aaaeSJosef Bacik 
38719aaaeSJosef Bacik #ifndef BTRFS_SPACE_INFO_H
48719aaaeSJosef Bacik #define BTRFS_SPACE_INFO_H
58719aaaeSJosef Bacik 
6*f04fbcc6SQu Wenruo #include "volumes.h"
7*f04fbcc6SQu Wenruo 
88719aaaeSJosef Bacik struct btrfs_space_info {
98719aaaeSJosef Bacik 	spinlock_t lock;
108719aaaeSJosef Bacik 
118719aaaeSJosef Bacik 	u64 total_bytes;	/* total bytes in the space,
128719aaaeSJosef Bacik 				   this doesn't take mirrors into account */
138719aaaeSJosef Bacik 	u64 bytes_used;		/* total bytes used,
148719aaaeSJosef Bacik 				   this doesn't take mirrors into account */
158719aaaeSJosef Bacik 	u64 bytes_pinned;	/* total bytes pinned, will be freed when the
168719aaaeSJosef Bacik 				   transaction finishes */
178719aaaeSJosef Bacik 	u64 bytes_reserved;	/* total bytes the allocator has reserved for
188719aaaeSJosef Bacik 				   current allocations */
198719aaaeSJosef Bacik 	u64 bytes_may_use;	/* number of bytes that may be used for
208719aaaeSJosef Bacik 				   delalloc/allocations */
218719aaaeSJosef Bacik 	u64 bytes_readonly;	/* total bytes that are read only */
22169e0da9SNaohiro Aota 	u64 bytes_zone_unusable;	/* total bytes that are unusable until
23169e0da9SNaohiro Aota 					   resetting the device zone */
248719aaaeSJosef Bacik 
258719aaaeSJosef Bacik 	u64 max_extent_size;	/* This will hold the maximum extent size of
268719aaaeSJosef Bacik 				   the space info if we had an ENOSPC in the
278719aaaeSJosef Bacik 				   allocator. */
288719aaaeSJosef Bacik 
29bb5a098dSJosef Bacik 	/*
30bb5a098dSJosef Bacik 	 * Once a block group drops below this threshold (percents) we'll
31bb5a098dSJosef Bacik 	 * schedule it for reclaim.
32bb5a098dSJosef Bacik 	 */
33bb5a098dSJosef Bacik 	int bg_reclaim_threshold;
34bb5a098dSJosef Bacik 
3588a777a6SJosef Bacik 	int clamp;		/* Used to scale our threshold for preemptive
3688a777a6SJosef Bacik 				   flushing. The value is >> clamp, so turns
3788a777a6SJosef Bacik 				   out to be a 2^clamp divisor. */
3888a777a6SJosef Bacik 
398719aaaeSJosef Bacik 	unsigned int full:1;	/* indicates that we cannot allocate any more
408719aaaeSJosef Bacik 				   chunks for this space */
418719aaaeSJosef Bacik 	unsigned int chunk_alloc:1;	/* set if we are allocating a chunk */
428719aaaeSJosef Bacik 
438719aaaeSJosef Bacik 	unsigned int flush:1;		/* set if we are trying to make space */
448719aaaeSJosef Bacik 
458719aaaeSJosef Bacik 	unsigned int force_alloc;	/* set if we need to force a chunk
468719aaaeSJosef Bacik 					   alloc for this space */
478719aaaeSJosef Bacik 
488719aaaeSJosef Bacik 	u64 disk_used;		/* total bytes used on disk */
498719aaaeSJosef Bacik 	u64 disk_total;		/* total bytes on disk, takes mirrors into
508719aaaeSJosef Bacik 				   account */
518719aaaeSJosef Bacik 
528719aaaeSJosef Bacik 	u64 flags;
538719aaaeSJosef Bacik 
548719aaaeSJosef Bacik 	struct list_head list;
558719aaaeSJosef Bacik 	/* Protected by the spinlock 'lock'. */
568719aaaeSJosef Bacik 	struct list_head ro_bgs;
578719aaaeSJosef Bacik 	struct list_head priority_tickets;
588719aaaeSJosef Bacik 	struct list_head tickets;
59db161806SNikolay Borisov 
60db161806SNikolay Borisov 	/*
61db161806SNikolay Borisov 	 * Size of space that needs to be reclaimed in order to satisfy pending
62db161806SNikolay Borisov 	 * tickets
63db161806SNikolay Borisov 	 */
64db161806SNikolay Borisov 	u64 reclaim_size;
65db161806SNikolay Borisov 
668719aaaeSJosef Bacik 	/*
678719aaaeSJosef Bacik 	 * tickets_id just indicates the next ticket will be handled, so note
688719aaaeSJosef Bacik 	 * it's not stored per ticket.
698719aaaeSJosef Bacik 	 */
708719aaaeSJosef Bacik 	u64 tickets_id;
718719aaaeSJosef Bacik 
728719aaaeSJosef Bacik 	struct rw_semaphore groups_sem;
738719aaaeSJosef Bacik 	/* for block groups in our same type */
748719aaaeSJosef Bacik 	struct list_head block_groups[BTRFS_NR_RAID_TYPES];
758719aaaeSJosef Bacik 
768719aaaeSJosef Bacik 	struct kobject kobj;
778719aaaeSJosef Bacik 	struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
788719aaaeSJosef Bacik };
798719aaaeSJosef Bacik 
80b338b013SJosef Bacik struct reserve_ticket {
81b338b013SJosef Bacik 	u64 bytes;
82b338b013SJosef Bacik 	int error;
837f9fe614SJosef Bacik 	bool steal;
84b338b013SJosef Bacik 	struct list_head list;
85b338b013SJosef Bacik 	wait_queue_head_t wait;
86b338b013SJosef Bacik };
87b338b013SJosef Bacik 
888719aaaeSJosef Bacik static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
898719aaaeSJosef Bacik {
908719aaaeSJosef Bacik 	return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
918719aaaeSJosef Bacik 		(space_info->flags & BTRFS_BLOCK_GROUP_DATA));
928719aaaeSJosef Bacik }
938719aaaeSJosef Bacik 
94bb96c4e5SJosef Bacik /*
95bb96c4e5SJosef Bacik  *
96bb96c4e5SJosef Bacik  * Declare a helper function to detect underflow of various space info members
97bb96c4e5SJosef Bacik  */
98f3e75e38SJosef Bacik #define DECLARE_SPACE_INFO_UPDATE(name, trace_name)			\
99bb96c4e5SJosef Bacik static inline void							\
100bb96c4e5SJosef Bacik btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info,		\
101bb96c4e5SJosef Bacik 			       struct btrfs_space_info *sinfo,		\
102bb96c4e5SJosef Bacik 			       s64 bytes)				\
103bb96c4e5SJosef Bacik {									\
104f3e75e38SJosef Bacik 	const u64 abs_bytes = (bytes < 0) ? -bytes : bytes;		\
105bb96c4e5SJosef Bacik 	lockdep_assert_held(&sinfo->lock);				\
106bb96c4e5SJosef Bacik 	trace_update_##name(fs_info, sinfo, sinfo->name, bytes);	\
107f3e75e38SJosef Bacik 	trace_btrfs_space_reservation(fs_info, trace_name,		\
108f3e75e38SJosef Bacik 				      sinfo->flags, abs_bytes,		\
109f3e75e38SJosef Bacik 				      bytes > 0);			\
110bb96c4e5SJosef Bacik 	if (bytes < 0 && sinfo->name < -bytes) {			\
111bb96c4e5SJosef Bacik 		WARN_ON(1);						\
112bb96c4e5SJosef Bacik 		sinfo->name = 0;					\
113bb96c4e5SJosef Bacik 		return;							\
114bb96c4e5SJosef Bacik 	}								\
115bb96c4e5SJosef Bacik 	sinfo->name += bytes;						\
116bb96c4e5SJosef Bacik }
117bb96c4e5SJosef Bacik 
118f3e75e38SJosef Bacik DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
119f3e75e38SJosef Bacik DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
120bb96c4e5SJosef Bacik 
121280c2908SJosef Bacik int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
122280c2908SJosef Bacik void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
123280c2908SJosef Bacik 			     u64 total_bytes, u64 bytes_used,
124169e0da9SNaohiro Aota 			     u64 bytes_readonly, u64 bytes_zone_unusable,
125280c2908SJosef Bacik 			     struct btrfs_space_info **space_info);
126280c2908SJosef Bacik struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
127280c2908SJosef Bacik 					       u64 flags);
128e1f60a65SDavid Sterba u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
129280c2908SJosef Bacik 			  bool may_use_included);
130280c2908SJosef Bacik void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
1315da6afebSJosef Bacik void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
1325da6afebSJosef Bacik 			   struct btrfs_space_info *info, u64 bytes,
1335da6afebSJosef Bacik 			   int dump_block_groups);
1349270501cSJosef Bacik int btrfs_reserve_metadata_bytes(struct btrfs_fs_info *fs_info,
1350d9764f6SJosef Bacik 				 struct btrfs_block_rsv *block_rsv,
1360d9764f6SJosef Bacik 				 u64 orig_bytes,
1370d9764f6SJosef Bacik 				 enum btrfs_reserve_flush_enum flush);
13818fa2284SJosef Bacik void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
13918fa2284SJosef Bacik 				struct btrfs_space_info *space_info);
140a30a3d20SJosef Bacik int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
141a30a3d20SJosef Bacik 			 struct btrfs_space_info *space_info, u64 bytes,
142a30a3d20SJosef Bacik 			 enum btrfs_reserve_flush_enum flush);
14318fa2284SJosef Bacik 
144d05e4649SJosef Bacik static inline void btrfs_space_info_free_bytes_may_use(
14518fa2284SJosef Bacik 				struct btrfs_fs_info *fs_info,
14618fa2284SJosef Bacik 				struct btrfs_space_info *space_info,
14718fa2284SJosef Bacik 				u64 num_bytes)
14818fa2284SJosef Bacik {
14918fa2284SJosef Bacik 	spin_lock(&space_info->lock);
15018fa2284SJosef Bacik 	btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
15118fa2284SJosef Bacik 	btrfs_try_granting_tickets(fs_info, space_info);
15218fa2284SJosef Bacik 	spin_unlock(&space_info->lock);
15318fa2284SJosef Bacik }
1548698fc4eSJosef Bacik int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
1558698fc4eSJosef Bacik 			     enum btrfs_reserve_flush_enum flush);
1568719aaaeSJosef Bacik #endif /* BTRFS_SPACE_INFO_H */
157