bcachefs: Scale down number of writepoints when low on space
[linux-block.git] / fs / bcachefs / alloc_foreground.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_FOREGROUND_H
3 #define _BCACHEFS_ALLOC_FOREGROUND_H
4
5 #include "bcachefs.h"
6 #include "alloc_types.h"
7
8 #include <linux/hash.h>
9
10 struct bkey;
11 struct bch_dev;
12 struct bch_fs;
13 struct bch_devs_List;
14
15 struct dev_alloc_list {
16         unsigned        nr;
17         u8              devs[BCH_SB_MEMBERS_MAX];
18 };
19
20 struct dev_alloc_list bch2_wp_alloc_list(struct bch_fs *,
21                                          struct write_point *,
22                                          struct bch_devs_mask *);
23 void bch2_wp_rescale(struct bch_fs *, struct bch_dev *,
24                      struct write_point *);
25
26 long bch2_bucket_alloc_new_fs(struct bch_dev *);
27
28 struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
29                                       enum alloc_reserve, bool,
30                                       struct closure *);
31
32 static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
33                            struct open_bucket *ob)
34 {
35         BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
36
37         obs->v[obs->nr++] = ob - c->open_buckets;
38 }
39
40 #define open_bucket_for_each(_c, _obs, _ob, _i)                         \
41         for ((_i) = 0;                                                  \
42              (_i) < (_obs)->nr &&                                       \
43              ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true);        \
44              (_i)++)
45
46 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
47
48 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
49 {
50         if (atomic_dec_and_test(&ob->pin))
51                 __bch2_open_bucket_put(c, ob);
52 }
53
54 static inline void bch2_open_buckets_put(struct bch_fs *c,
55                                          struct open_buckets *ptrs)
56 {
57         struct open_bucket *ob;
58         unsigned i;
59
60         open_bucket_for_each(c, ptrs, ob, i)
61                 bch2_open_bucket_put(c, ob);
62         ptrs->nr = 0;
63 }
64
65 static inline void bch2_open_bucket_get(struct bch_fs *c,
66                                         struct write_point *wp,
67                                         struct open_buckets *ptrs)
68 {
69         struct open_bucket *ob;
70         unsigned i;
71
72         open_bucket_for_each(c, &wp->ptrs, ob, i) {
73                 atomic_inc(&ob->pin);
74                 ob_push(c, ptrs, ob);
75         }
76 }
77
78 struct write_point *bch2_alloc_sectors_start(struct bch_fs *,
79                                              unsigned,
80                                              struct write_point_specifier,
81                                              struct bch_devs_list *,
82                                              unsigned, unsigned,
83                                              enum alloc_reserve,
84                                              unsigned,
85                                              struct closure *);
86
87 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
88                                     struct bkey_i_extent *, unsigned);
89 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
90
91 void bch2_writepoint_stop(struct bch_fs *, struct bch_dev *,
92                           struct write_point *);
93
94 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
95 {
96         return (struct write_point_specifier) { .v = v | 1 };
97 }
98
99 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
100 {
101         return (struct write_point_specifier) { .v = (unsigned long) wp };
102 }
103
104 static inline void writepoint_init(struct write_point *wp,
105                                    enum bch_data_type type)
106 {
107         mutex_init(&wp->lock);
108         wp->type = type;
109 }
110
111 void bch2_fs_allocator_foreground_init(struct bch_fs *);
112
113 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */