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