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