xref: /openbmc/linux/fs/btrfs/space-info.h (revision b338b013e18a28341aaf9e665ac1edc9fae518d1)
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 
68719aaaeSJosef Bacik struct btrfs_space_info {
78719aaaeSJosef Bacik 	spinlock_t lock;
88719aaaeSJosef Bacik 
98719aaaeSJosef Bacik 	u64 total_bytes;	/* total bytes in the space,
108719aaaeSJosef Bacik 				   this doesn't take mirrors into account */
118719aaaeSJosef Bacik 	u64 bytes_used;		/* total bytes used,
128719aaaeSJosef Bacik 				   this doesn't take mirrors into account */
138719aaaeSJosef Bacik 	u64 bytes_pinned;	/* total bytes pinned, will be freed when the
148719aaaeSJosef Bacik 				   transaction finishes */
158719aaaeSJosef Bacik 	u64 bytes_reserved;	/* total bytes the allocator has reserved for
168719aaaeSJosef Bacik 				   current allocations */
178719aaaeSJosef Bacik 	u64 bytes_may_use;	/* number of bytes that may be used for
188719aaaeSJosef Bacik 				   delalloc/allocations */
198719aaaeSJosef Bacik 	u64 bytes_readonly;	/* total bytes that are read only */
208719aaaeSJosef Bacik 
218719aaaeSJosef Bacik 	u64 max_extent_size;	/* This will hold the maximum extent size of
228719aaaeSJosef Bacik 				   the space info if we had an ENOSPC in the
238719aaaeSJosef Bacik 				   allocator. */
248719aaaeSJosef Bacik 
258719aaaeSJosef Bacik 	unsigned int full:1;	/* indicates that we cannot allocate any more
268719aaaeSJosef Bacik 				   chunks for this space */
278719aaaeSJosef Bacik 	unsigned int chunk_alloc:1;	/* set if we are allocating a chunk */
288719aaaeSJosef Bacik 
298719aaaeSJosef Bacik 	unsigned int flush:1;		/* set if we are trying to make space */
308719aaaeSJosef Bacik 
318719aaaeSJosef Bacik 	unsigned int force_alloc;	/* set if we need to force a chunk
328719aaaeSJosef Bacik 					   alloc for this space */
338719aaaeSJosef Bacik 
348719aaaeSJosef Bacik 	u64 disk_used;		/* total bytes used on disk */
358719aaaeSJosef Bacik 	u64 disk_total;		/* total bytes on disk, takes mirrors into
368719aaaeSJosef Bacik 				   account */
378719aaaeSJosef Bacik 
388719aaaeSJosef Bacik 	u64 flags;
398719aaaeSJosef Bacik 
408719aaaeSJosef Bacik 	/*
418719aaaeSJosef Bacik 	 * bytes_pinned is kept in line with what is actually pinned, as in
428719aaaeSJosef Bacik 	 * we've called update_block_group and dropped the bytes_used counter
438719aaaeSJosef Bacik 	 * and increased the bytes_pinned counter.  However this means that
448719aaaeSJosef Bacik 	 * bytes_pinned does not reflect the bytes that will be pinned once the
458719aaaeSJosef Bacik 	 * delayed refs are flushed, so this counter is inc'ed every time we
468719aaaeSJosef Bacik 	 * call btrfs_free_extent so it is a realtime count of what will be
478719aaaeSJosef Bacik 	 * freed once the transaction is committed.  It will be zeroed every
488719aaaeSJosef Bacik 	 * time the transaction commits.
498719aaaeSJosef Bacik 	 */
508719aaaeSJosef Bacik 	struct percpu_counter total_bytes_pinned;
518719aaaeSJosef Bacik 
528719aaaeSJosef Bacik 	struct list_head list;
538719aaaeSJosef Bacik 	/* Protected by the spinlock 'lock'. */
548719aaaeSJosef Bacik 	struct list_head ro_bgs;
558719aaaeSJosef Bacik 	struct list_head priority_tickets;
568719aaaeSJosef Bacik 	struct list_head tickets;
578719aaaeSJosef Bacik 	/*
588719aaaeSJosef Bacik 	 * tickets_id just indicates the next ticket will be handled, so note
598719aaaeSJosef Bacik 	 * it's not stored per ticket.
608719aaaeSJosef Bacik 	 */
618719aaaeSJosef Bacik 	u64 tickets_id;
628719aaaeSJosef Bacik 
638719aaaeSJosef Bacik 	struct rw_semaphore groups_sem;
648719aaaeSJosef Bacik 	/* for block groups in our same type */
658719aaaeSJosef Bacik 	struct list_head block_groups[BTRFS_NR_RAID_TYPES];
668719aaaeSJosef Bacik 	wait_queue_head_t wait;
678719aaaeSJosef Bacik 
688719aaaeSJosef Bacik 	struct kobject kobj;
698719aaaeSJosef Bacik 	struct kobject *block_group_kobjs[BTRFS_NR_RAID_TYPES];
708719aaaeSJosef Bacik };
718719aaaeSJosef Bacik 
72*b338b013SJosef Bacik struct reserve_ticket {
73*b338b013SJosef Bacik 	u64 orig_bytes;
74*b338b013SJosef Bacik 	u64 bytes;
75*b338b013SJosef Bacik 	int error;
76*b338b013SJosef Bacik 	struct list_head list;
77*b338b013SJosef Bacik 	wait_queue_head_t wait;
78*b338b013SJosef Bacik };
79*b338b013SJosef Bacik 
808719aaaeSJosef Bacik static inline bool btrfs_mixed_space_info(struct btrfs_space_info *space_info)
818719aaaeSJosef Bacik {
828719aaaeSJosef Bacik 	return ((space_info->flags & BTRFS_BLOCK_GROUP_METADATA) &&
838719aaaeSJosef Bacik 		(space_info->flags & BTRFS_BLOCK_GROUP_DATA));
848719aaaeSJosef Bacik }
858719aaaeSJosef Bacik 
86bb96c4e5SJosef Bacik /*
87bb96c4e5SJosef Bacik  *
88bb96c4e5SJosef Bacik  * Declare a helper function to detect underflow of various space info members
89bb96c4e5SJosef Bacik  */
90bb96c4e5SJosef Bacik #define DECLARE_SPACE_INFO_UPDATE(name)					\
91bb96c4e5SJosef Bacik static inline void							\
92bb96c4e5SJosef Bacik btrfs_space_info_update_##name(struct btrfs_fs_info *fs_info,		\
93bb96c4e5SJosef Bacik 			       struct btrfs_space_info *sinfo,		\
94bb96c4e5SJosef Bacik 			       s64 bytes)				\
95bb96c4e5SJosef Bacik {									\
96bb96c4e5SJosef Bacik 	lockdep_assert_held(&sinfo->lock);				\
97bb96c4e5SJosef Bacik 	trace_update_##name(fs_info, sinfo, sinfo->name, bytes);	\
98bb96c4e5SJosef Bacik 	if (bytes < 0 && sinfo->name < -bytes) {			\
99bb96c4e5SJosef Bacik 		WARN_ON(1);						\
100bb96c4e5SJosef Bacik 		sinfo->name = 0;					\
101bb96c4e5SJosef Bacik 		return;							\
102bb96c4e5SJosef Bacik 	}								\
103bb96c4e5SJosef Bacik 	sinfo->name += bytes;						\
104bb96c4e5SJosef Bacik }
105bb96c4e5SJosef Bacik 
106bb96c4e5SJosef Bacik DECLARE_SPACE_INFO_UPDATE(bytes_may_use);
107bb96c4e5SJosef Bacik DECLARE_SPACE_INFO_UPDATE(bytes_pinned);
108bb96c4e5SJosef Bacik 
109d44b72aaSJosef Bacik void btrfs_space_info_add_new_bytes(struct btrfs_fs_info *fs_info,
110d44b72aaSJosef Bacik 				    struct btrfs_space_info *space_info,
111d44b72aaSJosef Bacik 				    u64 num_bytes);
112d44b72aaSJosef Bacik void btrfs_space_info_add_old_bytes(struct btrfs_fs_info *fs_info,
113d44b72aaSJosef Bacik 				    struct btrfs_space_info *space_info,
114d44b72aaSJosef Bacik 				    u64 num_bytes);
115280c2908SJosef Bacik int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
116280c2908SJosef Bacik void btrfs_update_space_info(struct btrfs_fs_info *info, u64 flags,
117280c2908SJosef Bacik 			     u64 total_bytes, u64 bytes_used,
118280c2908SJosef Bacik 			     u64 bytes_readonly,
119280c2908SJosef Bacik 			     struct btrfs_space_info **space_info);
120280c2908SJosef Bacik struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info,
121280c2908SJosef Bacik 					       u64 flags);
122280c2908SJosef Bacik u64 btrfs_space_info_used(struct btrfs_space_info *s_info,
123280c2908SJosef Bacik 			  bool may_use_included);
124280c2908SJosef Bacik void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
12541783ef2SJosef Bacik int btrfs_can_overcommit(struct btrfs_fs_info *fs_info,
12641783ef2SJosef Bacik 			 struct btrfs_space_info *space_info, u64 bytes,
12741783ef2SJosef Bacik 			 enum btrfs_reserve_flush_enum flush,
12841783ef2SJosef Bacik 			 bool system_chunk);
129d44b72aaSJosef Bacik 
1308719aaaeSJosef Bacik #endif /* BTRFS_SPACE_INFO_H */
131