]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/ec.h
15f751fc2a35d32bea03efde0cf5cbbd332bb41a
[bcachefs-tools-debian] / libbcachefs / ec.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_EC_H
3 #define _BCACHEFS_EC_H
4
5 #include "ec_types.h"
6 #include "buckets_types.h"
7 #include "keylist_types.h"
8
9 const char *bch2_stripe_invalid(const struct bch_fs *, struct bkey_s_c);
10 void bch2_stripe_to_text(struct printbuf *, struct bch_fs *,
11                          struct bkey_s_c);
12
13 #define bch2_bkey_ops_stripe (struct bkey_ops) {        \
14         .key_invalid    = bch2_stripe_invalid,          \
15         .val_to_text    = bch2_stripe_to_text,          \
16         .swab           = bch2_ptr_swab,                \
17 }
18
19 static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
20 {
21         return DIV_ROUND_UP(le16_to_cpu(s->sectors),
22                             1 << s->csum_granularity_bits);
23 }
24
25 static inline unsigned stripe_csum_offset(const struct bch_stripe *s,
26                                           unsigned dev, unsigned csum_idx)
27 {
28         unsigned csum_bytes = bch_crc_bytes[s->csum_type];
29
30         return sizeof(struct bch_stripe) +
31                 sizeof(struct bch_extent_ptr) * s->nr_blocks +
32                 (dev * stripe_csums_per_device(s) + csum_idx) * csum_bytes;
33 }
34
35 static inline unsigned stripe_blockcount_offset(const struct bch_stripe *s,
36                                                 unsigned idx)
37 {
38         return stripe_csum_offset(s, s->nr_blocks, 0) +
39                 sizeof(u16) * idx;
40 }
41
42 static inline unsigned stripe_blockcount_get(const struct bch_stripe *s,
43                                              unsigned idx)
44 {
45         return le16_to_cpup((void *) s + stripe_blockcount_offset(s, idx));
46 }
47
48 static inline void stripe_blockcount_set(struct bch_stripe *s,
49                                          unsigned idx, unsigned v)
50 {
51         __le16 *p = (void *) s + stripe_blockcount_offset(s, idx);
52
53         *p = cpu_to_le16(v);
54 }
55
56 static inline unsigned stripe_val_u64s(const struct bch_stripe *s)
57 {
58         return DIV_ROUND_UP(stripe_blockcount_offset(s, s->nr_blocks),
59                             sizeof(u64));
60 }
61
62 static inline void *stripe_csum(struct bch_stripe *s,
63                                 unsigned dev, unsigned csum_idx)
64 {
65         return (void *) s + stripe_csum_offset(s, dev, csum_idx);
66 }
67
68 struct bch_read_bio;
69
70 struct ec_stripe_buf {
71         /* might not be buffering the entire stripe: */
72         unsigned                offset;
73         unsigned                size;
74         unsigned long           valid[BITS_TO_LONGS(EC_STRIPE_MAX)];
75
76         void                    *data[EC_STRIPE_MAX];
77
78         union {
79                 struct bkey_i_stripe    key;
80                 u64                     pad[255];
81         };
82 };
83
84 struct ec_stripe_head;
85
86 struct ec_stripe_new {
87         struct bch_fs           *c;
88         struct ec_stripe_head   *h;
89         struct mutex            lock;
90         struct list_head        list;
91
92         /* counts in flight writes, stripe is created when pin == 0 */
93         atomic_t                pin;
94
95         int                     err;
96
97         u8                      nr_data;
98         u8                      nr_parity;
99         bool                    allocated;
100         bool                    pending;
101         bool                    existing_stripe;
102         u64                     existing_stripe_idx;
103
104         unsigned long           blocks_allocated[BITS_TO_LONGS(EC_STRIPE_MAX)];
105
106         struct open_buckets     blocks;
107         u8                      data_block_idx[EC_STRIPE_MAX];
108         struct open_buckets     parity;
109         struct disk_reservation res;
110
111         struct keylist          keys;
112         u64                     inline_keys[BKEY_U64s * 8];
113
114         struct ec_stripe_buf    stripe;
115 };
116
117 struct ec_stripe_head {
118         struct list_head        list;
119         struct mutex            lock;
120
121         unsigned                target;
122         unsigned                algo;
123         unsigned                redundancy;
124
125         struct bch_devs_mask    devs;
126         unsigned                nr_active_devs;
127
128         unsigned                blocksize;
129
130         struct dev_stripe_state block_stripe;
131         struct dev_stripe_state parity_stripe;
132
133         struct ec_stripe_new    *s;
134 };
135
136 int bch2_ec_read_extent(struct bch_fs *, struct bch_read_bio *);
137
138 void *bch2_writepoint_ec_buf(struct bch_fs *, struct write_point *);
139 void bch2_ec_add_backpointer(struct bch_fs *, struct write_point *,
140                              struct bpos, unsigned);
141
142 void bch2_ec_bucket_written(struct bch_fs *, struct open_bucket *);
143 void bch2_ec_bucket_cancel(struct bch_fs *, struct open_bucket *);
144
145 int bch2_ec_stripe_new_alloc(struct bch_fs *, struct ec_stripe_head *);
146
147 void bch2_ec_stripe_head_put(struct bch_fs *, struct ec_stripe_head *);
148 struct ec_stripe_head *bch2_ec_stripe_head_get(struct bch_fs *, unsigned,
149                                                unsigned, unsigned);
150
151 void bch2_stripes_heap_update(struct bch_fs *, struct stripe *, size_t);
152 void bch2_stripes_heap_del(struct bch_fs *, struct stripe *, size_t);
153 void bch2_stripes_heap_insert(struct bch_fs *, struct stripe *, size_t);
154
155 void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *);
156
157 void bch2_ec_flush_new_stripes(struct bch_fs *);
158
159 struct journal_keys;
160 int bch2_stripes_read(struct bch_fs *, struct journal_keys *);
161 int bch2_stripes_write(struct bch_fs *, unsigned);
162
163 int bch2_ec_mem_alloc(struct bch_fs *, bool);
164
165 void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *);
166 void bch2_new_stripes_to_text(struct printbuf *, struct bch_fs *);
167
168 void bch2_fs_ec_exit(struct bch_fs *);
169 int bch2_fs_ec_init(struct bch_fs *);
170
171 #endif /* _BCACHEFS_EC_H */