xref: /openbmc/linux/fs/btrfs/space-info.h (revision a68a90b2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_SPACE_INFO_H
4 #define BTRFS_SPACE_INFO_H
5 
6 struct btrfs_space_info {
7 	spinlock_t lock;
8 
9 	u64 total_bytes;	/* total bytes in the space,
10 				   this doesn't take mirrors into account */
11 	u64 bytes_used;		/* total bytes used,
12 				   this doesn't take mirrors into account */
13 	u64 bytes_pinned;	/* total bytes pinned, will be freed when the
14 				   transaction finishes */
15 	u64 bytes_reserved;	/* total bytes the allocator has reserved for
16 				   current allocations */
17 	u64 bytes_may_use;	/* number of bytes that may be used for
18 				   delalloc/allocations */
19 	u64 bytes_readonly;	/* total bytes that are read only */
20 	u64 bytes_zone_unusable;	/* total bytes that are unusable until
21 					   resetting the device zone */
22 
23 	u64 max_extent_size;	/* This will hold the maximum extent size of
24 				   the space info if we had an ENOSPC in the
25 				   allocator. */
26 
27 	int clamp;		/* Used to scale our threshold for preemptive
28 				   flushing. The value is >> clamp, so turns
29 				   out to be a 2^clamp divisor. */
30 
31 	unsigned int full:1;	/* indicates that we cannot allocate any more
32 				   chunks for this space */
33 	unsigned int chunk_alloc:1;	/* set if we are allocating a chunk */
34 
35 	unsigned int flush:1;		/* set if we are trying to make space */
36 
37 	unsigned int force_alloc;	/* set if we need to force a chunk
38 					   alloc for this space */
39 
40 	u64 disk_used;		/* total bytes used on disk */
41 	u64 disk_total;		/* total bytes on disk, takes mirrors into
42 				   account */
43 
44 	u64 flags;
45 
46 	/*
47 	 * bytes_pinned is kept in line with what is actually pinned, as in
48 	 * we've called update_block_group and dropped the bytes_used counter
49 	 * and increased the bytes_pinned counter.  However this means that
50 	 * bytes_pinned does not reflect the bytes that will be pinned once the
51 	 * delayed refs are flushed, so this counter is inc'ed every time we
52 	 * call btrfs_free_extent so it is a realtime count of what will be
53 	 * freed once the transaction is committed.  It will be zeroed every
54 	 * time the transaction commits.
55 	 */
56 	struct percpu_counter total_bytes_pinned;
57 
58 	struct list_head list;
59 	/* Protected by the spinlock 'lock'. */
60 	struct list_head ro_bgs;
61 	struct list_head priority_tickets;
62 	struct list_head tickets;
63 
64 	/*
65 	 * Size of space that needs to be reclaimed in order to satisfy pending
66 	 * tickets
67 	 */
68 	u64 reclaim_size;
69 
70 	/*
71 	 * tickets_id just indicates the next ticket will be handled, so note
72 	 * it's not stored per ticket.
73 	 */
74 	u64 tickets_id;
75 
76 	struct rw_semaphore groups_sem;
77 	/* for block groups in our same type */
78 	struct list_head block_groups[BTRFS_NR_RAID_TYPES];
79 
80 	struct kobject kobj;
81 	struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
82 };
83 
84 struct reserve_ticket {
85 	u64 bytes;
86 	int error;
87 	bool steal;
88 	struct list_head list;
89 	wait_queue_head_t wait;
90 };
91 
92 static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
93 {
94 	return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
95 		(space_info->flags & BTRFS_BLOCK_GROUP_DATA));
96 }
97 
98 /*
99  *
100  * Declare a helper function to detect underflow of various space info members
101  */
102 #define DECLARE_SPACE_INFO_UPDATE(name, trace_name)			\
103 static inline void							\
104 btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info,		\
105 			       struct btrfs_space_info *sinfo,		\
106 			       s64 bytes)				\
107 {									\
108 	const u64 abs_bytes = (bytes < 0) ? -bytes : bytes;		\
109 	lockdep_assert_held(&sinfo->lock);				\
110 	trace_update_##name(fs_info, sinfo, sinfo->name, bytes);	\
111 	trace_btrfs_space_reservation(fs_info, trace_name,		\
112 				      sinfo->flags, abs_bytes,		\
113 				      bytes > 0);			\
114 	if (bytes < 0 && sinfo->name < -bytes) {			\
115 		WARN_ON(1);						\
116 		sinfo->name = 0;					\
117 		return;							\
118 	}								\
119 	sinfo->name += bytes;						\
120 }
121 
122 DECLARE_SPACE_INFO_UPDATE(bytes_may_use, "space_info");
123 DECLARE_SPACE_INFO_UPDATE(bytes_pinned, "pinned");
124 
125 int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
126 void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
127 			     u64 total_bytes, u64 bytes_used,
128 			     u64 bytes_readonly, u64 bytes_zone_unusable,
129 			     struct btrfs_space_info **space_info);
130 struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
131 					       u64 flags);
132 u64 __pure btrfs_space_info_used(struct btrfs_space_info *s_info,
133 			  bool may_use_included);
134 void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
135 void btrfs_dump_space_info(struct btrfs_fs_info *fs_info,
136 			   struct btrfs_space_info *info, u64 bytes,
137 			   int dump_block_groups);
138 int btrfs_reserve_metadata_bytes(struct btrfs_root *root,
139 				 struct btrfs_block_rsv *block_rsv,
140 				 u64 orig_bytes,
141 				 enum btrfs_reserve_flush_enum flush);
142 void btrfs_try_granting_tickets(struct btrfs_fs_info *fs_info,
143 				struct btrfs_space_info *space_info);
144 int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
145 			 struct btrfs_space_info *space_info, u64 bytes,
146 			 enum btrfs_reserve_flush_enum flush);
147 
148 static inline void btrfs_space_info_free_bytes_may_use(
149 				struct btrfs_fs_info *fs_info,
150 				struct btrfs_space_info *space_info,
151 				u64 num_bytes)
152 {
153 	spin_lock(&space_info->lock);
154 	btrfs_space_info_update_bytes_may_use(fs_info, space_info, -num_bytes);
155 	btrfs_try_granting_tickets(fs_info, space_info);
156 	spin_unlock(&space_info->lock);
157 }
158 int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
159 			     enum btrfs_reserve_flush_enum flush);
160 
161 static inline void __btrfs_mod_total_bytes_pinned(
162 					struct btrfs_space_info *space_info,
163 					s64 mod)
164 {
165 	percpu_counter_add_batch(&space_info->total_bytes_pinned, mod,
166 				 BTRFS_TOTAL_BYTES_PINNED_BATCH);
167 }
168 
169 static inline void btrfs_mod_total_bytes_pinned(struct btrfs_fs_info *fs_info,
170 						u64 flags, s64 mod)
171 {
172 	struct btrfs_space_info *space_info = btrfs_find_space_info(fs_info, flags);
173 
174 	ASSERT(space_info);
175 	__btrfs_mod_total_bytes_pinned(space_info, mod);
176 }
177 
178 #endif /* BTRFS_SPACE_INFO_H */
179