]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_write_buffer.c
05b755a0e79ca21edf751778fc4c10e1b6472424
[bcachefs-tools-debian] / libbcachefs / btree_write_buffer.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_locking.h"
5 #include "btree_update.h"
6 #include "btree_update_interior.h"
7 #include "btree_write_buffer.h"
8 #include "error.h"
9 #include "journal.h"
10 #include "journal_reclaim.h"
11
12 #include <linux/sort.h>
13
14 static int btree_write_buffered_key_cmp(const void *_l, const void *_r)
15 {
16         const struct btree_write_buffered_key *l = _l;
17         const struct btree_write_buffered_key *r = _r;
18
19         return  cmp_int(l->btree, r->btree) ?:
20                 bpos_cmp(l->k.k.p, r->k.k.p) ?:
21                 cmp_int(l->journal_seq, r->journal_seq) ?:
22                 cmp_int(l->journal_offset, r->journal_offset);
23 }
24
25 static int btree_write_buffered_journal_cmp(const void *_l, const void *_r)
26 {
27         const struct btree_write_buffered_key *l = _l;
28         const struct btree_write_buffered_key *r = _r;
29
30         return  cmp_int(l->journal_seq, r->journal_seq);
31 }
32
33 static int bch2_btree_write_buffer_flush_one(struct btree_trans *trans,
34                                              struct btree_iter *iter,
35                                              struct btree_write_buffered_key *wb,
36                                              unsigned commit_flags,
37                                              bool *write_locked,
38                                              size_t *fast)
39 {
40         struct bch_fs *c = trans->c;
41         struct btree_path *path;
42         int ret;
43
44         ret = bch2_btree_iter_traverse(iter);
45         if (ret)
46                 return ret;
47
48         path = iter->path;
49
50         if (!*write_locked) {
51                 ret = bch2_btree_node_lock_write(trans, path, &path->l[0].b->c);
52                 if (ret)
53                         return ret;
54
55                 bch2_btree_node_prep_for_write(trans, path, path->l[0].b);
56                 *write_locked = true;
57         }
58
59         if (!bch2_btree_node_insert_fits(c, path->l[0].b, wb->k.k.u64s)) {
60                 bch2_btree_node_unlock_write(trans, path, path->l[0].b);
61                 *write_locked = false;
62                 goto trans_commit;
63         }
64
65         bch2_btree_insert_key_leaf(trans, path, &wb->k, wb->journal_seq);
66         (*fast)++;
67         return 0;
68 trans_commit:
69         return  bch2_trans_update(trans, iter, &wb->k, 0) ?:
70                 bch2_trans_commit(trans, NULL, NULL,
71                                   commit_flags|
72                                   BTREE_INSERT_NOFAIL|
73                                   BTREE_INSERT_JOURNAL_RECLAIM);
74 }
75
76 static union btree_write_buffer_state btree_write_buffer_switch(struct btree_write_buffer *wb)
77 {
78         union btree_write_buffer_state old, new;
79         u64 v = READ_ONCE(wb->state.v);
80
81         do {
82                 old.v = new.v = v;
83
84                 new.nr = 0;
85                 new.idx++;
86         } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
87
88         while (old.idx == 0 ? wb->state.ref0 : wb->state.ref1)
89                 cpu_relax();
90
91         return old;
92 }
93
94 int __bch2_btree_write_buffer_flush(struct btree_trans *trans, unsigned commit_flags,
95                                     bool locked)
96 {
97         struct bch_fs *c = trans->c;
98         struct journal *j = &c->journal;
99         struct btree_write_buffer *wb = &c->btree_write_buffer;
100         struct journal_entry_pin pin;
101         struct btree_write_buffered_key *i, *dst, *keys;
102         struct btree_iter iter = { NULL };
103         size_t nr = 0, skipped = 0, fast = 0;
104         bool write_locked = false;
105         union btree_write_buffer_state s;
106         int ret = 0;
107
108         memset(&pin, 0, sizeof(pin));
109
110         if (!locked && !mutex_trylock(&wb->flush_lock))
111                 return 0;
112
113         bch2_journal_pin_copy(j, &pin, &wb->journal_pin, NULL);
114         bch2_journal_pin_drop(j, &wb->journal_pin);
115
116         s = btree_write_buffer_switch(wb);
117         keys = wb->keys[s.idx];
118         nr = s.nr;
119
120         /*
121          * We first sort so that we can detect and skip redundant updates, and
122          * then we attempt to flush in sorted btree order, as this is most
123          * efficient.
124          *
125          * However, since we're not flushing in the order they appear in the
126          * journal we won't be able to drop our journal pin until everything is
127          * flushed - which means this could deadlock the journal, if we weren't
128          * passing BTREE_INSERT_JORUNAL_RECLAIM. This causes the update to fail
129          * if it would block taking a journal reservation.
130          *
131          * If that happens, we sort them by the order they appeared in the
132          * journal - after dropping redundant entries - and then restart
133          * flushing, this time dropping journal pins as we go.
134          */
135
136         sort(keys, nr, sizeof(keys[0]),
137              btree_write_buffered_key_cmp, NULL);
138
139         for (i = keys; i < keys + nr; i++) {
140                 if (i + 1 < keys + nr &&
141                     i[0].btree == i[1].btree &&
142                     bpos_eq(i[0].k.k.p, i[1].k.k.p)) {
143                         skipped++;
144                         continue;
145                 }
146
147                 if (write_locked &&
148                     (iter.path->btree_id != i->btree ||
149                      bpos_gt(i->k.k.p, iter.path->l[0].b->key.k.p))) {
150                         bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
151                         write_locked = false;
152                 }
153
154                 if (!iter.path || iter.path->btree_id != i->btree) {
155                         bch2_trans_iter_exit(trans, &iter);
156                         bch2_trans_iter_init(trans, &iter, i->btree, i->k.k.p, BTREE_ITER_INTENT);
157                 }
158
159                 bch2_btree_iter_set_pos(&iter, i->k.k.p);
160                 iter.path->preserve = false;
161
162                 do {
163                         ret = bch2_btree_write_buffer_flush_one(trans, &iter, i,
164                                                 commit_flags, &write_locked, &fast);
165                         if (!write_locked)
166                                 bch2_trans_begin(trans);
167                 } while (bch2_err_matches(ret, BCH_ERR_transaction_restart));
168
169                 if (ret)
170                         break;
171         }
172
173         if (write_locked)
174                 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
175         bch2_trans_iter_exit(trans, &iter);
176
177         trace_write_buffer_flush(trans, nr, skipped, fast, wb->size);
178
179         if (ret == -BCH_ERR_journal_reclaim_would_deadlock)
180                 goto slowpath;
181
182         bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret));
183 out:
184         bch2_journal_pin_drop(j, &pin);
185         mutex_unlock(&wb->flush_lock);
186         return ret;
187 slowpath:
188         trace_write_buffer_flush_slowpath(trans, i - keys, nr);
189
190         dst = keys;
191         for (; i < keys + nr; i++) {
192                 if (i + 1 < keys + nr &&
193                     i[0].btree == i[1].btree &&
194                     bpos_eq(i[0].k.k.p, i[1].k.k.p))
195                         continue;
196
197                 *dst = *i;
198                 dst++;
199         }
200         nr = dst - keys;
201
202         sort(keys, nr, sizeof(keys[0]),
203              btree_write_buffered_journal_cmp,
204              NULL);
205
206         for (i = keys; i < keys + nr; i++) {
207                 if (i->journal_seq > pin.seq) {
208                         struct journal_entry_pin pin2;
209
210                         memset(&pin2, 0, sizeof(pin2));
211
212                         bch2_journal_pin_add(j, i->journal_seq, &pin2, NULL);
213                         bch2_journal_pin_drop(j, &pin);
214                         bch2_journal_pin_copy(j, &pin, &pin2, NULL);
215                         bch2_journal_pin_drop(j, &pin2);
216                 }
217
218                 ret = commit_do(trans, NULL, NULL,
219                                 commit_flags|
220                                 BTREE_INSERT_NOFAIL|
221                                 BTREE_INSERT_JOURNAL_RECLAIM|
222                                 JOURNAL_WATERMARK_reserved,
223                                 __bch2_btree_insert(trans, i->btree, &i->k, 0));
224                 if (bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret)))
225                         break;
226         }
227
228         goto out;
229 }
230
231 int bch2_btree_write_buffer_flush_sync(struct btree_trans *trans)
232 {
233         bch2_trans_unlock(trans);
234         mutex_lock(&trans->c->btree_write_buffer.flush_lock);
235         return __bch2_btree_write_buffer_flush(trans, 0, true);
236 }
237
238 int bch2_btree_write_buffer_flush(struct btree_trans *trans)
239 {
240         return __bch2_btree_write_buffer_flush(trans, 0, false);
241 }
242
243 static int bch2_btree_write_buffer_journal_flush(struct journal *j,
244                                 struct journal_entry_pin *_pin, u64 seq)
245 {
246         struct bch_fs *c = container_of(j, struct bch_fs, journal);
247         struct btree_write_buffer *wb = &c->btree_write_buffer;
248
249         mutex_lock(&wb->flush_lock);
250
251         return bch2_trans_run(c,
252                         __bch2_btree_write_buffer_flush(&trans, BTREE_INSERT_NOCHECK_RW, true));
253 }
254
255 static inline u64 btree_write_buffer_ref(int idx)
256 {
257         return ((union btree_write_buffer_state) {
258                 .ref0 = idx == 0,
259                 .ref1 = idx == 1,
260         }).v;
261 }
262
263 int bch2_btree_insert_keys_write_buffer(struct btree_trans *trans)
264 {
265         struct bch_fs *c = trans->c;
266         struct btree_write_buffer *wb = &c->btree_write_buffer;
267         struct btree_write_buffered_key *i;
268         union btree_write_buffer_state old, new;
269         int ret = 0;
270         u64 v;
271
272         trans_for_each_wb_update(trans, i) {
273                 EBUG_ON(i->k.k.u64s > BTREE_WRITE_BUFERED_U64s_MAX);
274
275                 i->journal_seq          = trans->journal_res.seq;
276                 i->journal_offset       = trans->journal_res.offset;
277         }
278
279         preempt_disable();
280         v = READ_ONCE(wb->state.v);
281         do {
282                 old.v = new.v = v;
283
284                 new.v += btree_write_buffer_ref(new.idx);
285                 new.nr += trans->nr_wb_updates;
286                 if (new.nr > wb->size) {
287                         ret = -BCH_ERR_btree_insert_need_flush_buffer;
288                         goto out;
289                 }
290         } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
291
292         memcpy(wb->keys[new.idx] + old.nr,
293                trans->wb_updates,
294                sizeof(trans->wb_updates[0]) * trans->nr_wb_updates);
295
296         bch2_journal_pin_add(&c->journal, trans->journal_res.seq, &wb->journal_pin,
297                              bch2_btree_write_buffer_journal_flush);
298
299         atomic64_sub_return_release(btree_write_buffer_ref(new.idx), &wb->state.counter);
300 out:
301         preempt_enable();
302         return ret;
303 }
304
305 void bch2_fs_btree_write_buffer_exit(struct bch_fs *c)
306 {
307         struct btree_write_buffer *wb = &c->btree_write_buffer;
308
309         BUG_ON(wb->state.nr && !bch2_journal_error(&c->journal));
310
311         kvfree(wb->keys[1]);
312         kvfree(wb->keys[0]);
313 }
314
315 int bch2_fs_btree_write_buffer_init(struct bch_fs *c)
316 {
317         struct btree_write_buffer *wb = &c->btree_write_buffer;
318
319         mutex_init(&wb->flush_lock);
320         wb->size = c->opts.btree_write_buffer_size;
321
322         wb->keys[0] = kvmalloc_array(wb->size, sizeof(*wb->keys[0]), GFP_KERNEL);
323         wb->keys[1] = kvmalloc_array(wb->size, sizeof(*wb->keys[1]), GFP_KERNEL);
324         if (!wb->keys[0] || !wb->keys[1])
325                 return -ENOMEM;
326
327         return 0;
328 }