]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_foreground.h
Update bcachefs sources to 96b991466a bcachefs: Improve error message in fsck
[bcachefs-tools-debian] / libbcachefs / 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_dev_alloc_list(struct bch_fs *,
21                                           struct dev_stripe_state *,
22                                           struct bch_devs_mask *);
23 void bch2_dev_stripe_increment(struct bch_fs *, struct bch_dev *,
24                                struct dev_stripe_state *);
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 static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
47                                                  struct open_buckets *obs)
48 {
49         struct open_bucket *ob;
50         unsigned i;
51
52         open_bucket_for_each(c, obs, ob, i)
53                 if (ob->ec)
54                         return ob;
55
56         return NULL;
57 }
58
59 void bch2_open_bucket_write_error(struct bch_fs *,
60                         struct open_buckets *, unsigned);
61
62 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
63
64 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
65 {
66         if (atomic_dec_and_test(&ob->pin))
67                 __bch2_open_bucket_put(c, ob);
68 }
69
70 static inline void bch2_open_buckets_put(struct bch_fs *c,
71                                          struct open_buckets *ptrs)
72 {
73         struct open_bucket *ob;
74         unsigned i;
75
76         open_bucket_for_each(c, ptrs, ob, i)
77                 bch2_open_bucket_put(c, ob);
78         ptrs->nr = 0;
79 }
80
81 static inline void bch2_open_bucket_get(struct bch_fs *c,
82                                         struct write_point *wp,
83                                         struct open_buckets *ptrs)
84 {
85         struct open_bucket *ob;
86         unsigned i;
87
88         open_bucket_for_each(c, &wp->ptrs, ob, i) {
89                 ob->type = wp->type;
90                 atomic_inc(&ob->pin);
91                 ob_push(c, ptrs, ob);
92         }
93 }
94
95 struct write_point *bch2_alloc_sectors_start(struct bch_fs *,
96                                              unsigned, unsigned,
97                                              struct write_point_specifier,
98                                              struct bch_devs_list *,
99                                              unsigned, unsigned,
100                                              enum alloc_reserve,
101                                              unsigned,
102                                              struct closure *);
103
104 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
105                                     struct bkey_i *, unsigned);
106 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
107
108 void bch2_open_buckets_stop_dev(struct bch_fs *, struct bch_dev *,
109                                 struct open_buckets *);
110
111 void bch2_writepoint_stop(struct bch_fs *, struct bch_dev *,
112                           struct write_point *);
113
114 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
115 {
116         return (struct write_point_specifier) { .v = v | 1 };
117 }
118
119 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
120 {
121         return (struct write_point_specifier) { .v = (unsigned long) wp };
122 }
123
124 static inline void writepoint_init(struct write_point *wp,
125                                    enum bch_data_type type)
126 {
127         mutex_init(&wp->lock);
128         wp->type = type;
129 }
130
131 void bch2_fs_allocator_foreground_init(struct bch_fs *);
132
133 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */