]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_foreground.h
Update bcachefs sources to 99750eab4d bcachefs: Persist stripe blocks_used
[bcachefs-tools-debian] / libbcachefs / alloc_foreground.h
1 #ifndef _BCACHEFS_ALLOC_FOREGROUND_H
2 #define _BCACHEFS_ALLOC_FOREGROUND_H
3
4 #include "bcachefs.h"
5 #include "alloc_types.h"
6
7 #include <linux/hash.h>
8
9 struct bkey;
10 struct bch_dev;
11 struct bch_fs;
12 struct bch_devs_List;
13
14 struct dev_alloc_list {
15         unsigned        nr;
16         u8              devs[BCH_SB_MEMBERS_MAX];
17 };
18
19 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *,
20                                           struct dev_stripe_state *,
21                                           struct bch_devs_mask *);
22 void bch2_dev_stripe_increment(struct bch_fs *, struct bch_dev *,
23                                struct dev_stripe_state *);
24
25 long bch2_bucket_alloc_new_fs(struct bch_dev *);
26
27 struct open_bucket *bch2_bucket_alloc(struct bch_fs *, struct bch_dev *,
28                                       enum alloc_reserve, bool,
29                                       struct closure *);
30
31 static inline void ob_push(struct bch_fs *c, struct open_buckets *obs,
32                            struct open_bucket *ob)
33 {
34         BUG_ON(obs->nr >= ARRAY_SIZE(obs->v));
35
36         obs->v[obs->nr++] = ob - c->open_buckets;
37 }
38
39 #define open_bucket_for_each(_c, _obs, _ob, _i)                         \
40         for ((_i) = 0;                                                  \
41              (_i) < (_obs)->nr &&                                       \
42              ((_ob) = (_c)->open_buckets + (_obs)->v[_i], true);        \
43              (_i)++)
44
45 static inline struct open_bucket *ec_open_bucket(struct bch_fs *c,
46                                                  struct open_buckets *obs)
47 {
48         struct open_bucket *ob;
49         unsigned i;
50
51         open_bucket_for_each(c, obs, ob, i)
52                 if (ob->ec)
53                         return ob;
54
55         return NULL;
56 }
57
58 void bch2_open_bucket_write_error(struct bch_fs *,
59                         struct open_buckets *, unsigned);
60
61 void __bch2_open_bucket_put(struct bch_fs *, struct open_bucket *);
62
63 static inline void bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
64 {
65         if (atomic_dec_and_test(&ob->pin))
66                 __bch2_open_bucket_put(c, ob);
67 }
68
69 static inline void bch2_open_buckets_put(struct bch_fs *c,
70                                          struct open_buckets *ptrs)
71 {
72         struct open_bucket *ob;
73         unsigned i;
74
75         open_bucket_for_each(c, ptrs, ob, i)
76                 bch2_open_bucket_put(c, ob);
77         ptrs->nr = 0;
78 }
79
80 static inline void bch2_open_bucket_get(struct bch_fs *c,
81                                         struct write_point *wp,
82                                         struct open_buckets *ptrs)
83 {
84         struct open_bucket *ob;
85         unsigned i;
86
87         open_bucket_for_each(c, &wp->ptrs, ob, i) {
88                 ob->type = wp->type;
89                 atomic_inc(&ob->pin);
90                 ob_push(c, ptrs, ob);
91         }
92 }
93
94 struct write_point *bch2_alloc_sectors_start(struct bch_fs *,
95                                              unsigned, unsigned,
96                                              struct write_point_specifier,
97                                              struct bch_devs_list *,
98                                              unsigned, unsigned,
99                                              enum alloc_reserve,
100                                              unsigned,
101                                              struct closure *);
102
103 void bch2_alloc_sectors_append_ptrs(struct bch_fs *, struct write_point *,
104                                     struct bkey_i *, unsigned);
105 void bch2_alloc_sectors_done(struct bch_fs *, struct write_point *);
106
107 void bch2_open_buckets_stop_dev(struct bch_fs *, struct bch_dev *,
108                                 struct open_buckets *, enum bch_data_type);
109
110 void bch2_writepoint_stop(struct bch_fs *, struct bch_dev *,
111                           struct write_point *);
112
113 static inline struct write_point_specifier writepoint_hashed(unsigned long v)
114 {
115         return (struct write_point_specifier) { .v = v | 1 };
116 }
117
118 static inline struct write_point_specifier writepoint_ptr(struct write_point *wp)
119 {
120         return (struct write_point_specifier) { .v = (unsigned long) wp };
121 }
122
123 static inline void writepoint_init(struct write_point *wp,
124                                    enum bch_data_type type)
125 {
126         mutex_init(&wp->lock);
127         wp->type = type;
128 }
129
130 void bch2_fs_allocator_foreground_init(struct bch_fs *);
131
132 #endif /* _BCACHEFS_ALLOC_FOREGROUND_H */