]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_write_buffer.c
Update bcachefs sources to da7d42a9a2 bcachefs: Add new assertions for shutdown path
[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
68         if (path->ref > 1) {
69                 /*
70                  * We can't clone a path that has write locks: if the path is
71                  * shared, unlock before set_pos(), traverse():
72                  */
73                 bch2_btree_node_unlock_write(trans, path, path->l[0].b);
74                 *write_locked = false;
75         }
76         return 0;
77 trans_commit:
78         return  bch2_trans_update(trans, iter, &wb->k, 0) ?:
79                 bch2_trans_commit(trans, NULL, NULL,
80                                   commit_flags|
81                                   BTREE_INSERT_NOCHECK_RW|
82                                   BTREE_INSERT_NOFAIL|
83                                   BTREE_INSERT_JOURNAL_RECLAIM);
84 }
85
86 static union btree_write_buffer_state btree_write_buffer_switch(struct btree_write_buffer *wb)
87 {
88         union btree_write_buffer_state old, new;
89         u64 v = READ_ONCE(wb->state.v);
90
91         do {
92                 old.v = new.v = v;
93
94                 new.nr = 0;
95                 new.idx++;
96         } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
97
98         while (old.idx == 0 ? wb->state.ref0 : wb->state.ref1)
99                 cpu_relax();
100
101         smp_mb();
102
103         return old;
104 }
105
106 int __bch2_btree_write_buffer_flush(struct btree_trans *trans, unsigned commit_flags,
107                                     bool locked)
108 {
109         struct bch_fs *c = trans->c;
110         struct journal *j = &c->journal;
111         struct btree_write_buffer *wb = &c->btree_write_buffer;
112         struct journal_entry_pin pin;
113         struct btree_write_buffered_key *i, *keys;
114         struct btree_iter iter = { NULL };
115         size_t nr = 0, skipped = 0, fast = 0, slowpath = 0;
116         bool write_locked = false;
117         union btree_write_buffer_state s;
118         int ret = 0;
119
120         memset(&pin, 0, sizeof(pin));
121
122         if (!locked && !mutex_trylock(&wb->flush_lock))
123                 return 0;
124
125         bch2_journal_pin_copy(j, &pin, &wb->journal_pin, NULL);
126         bch2_journal_pin_drop(j, &wb->journal_pin);
127
128         s = btree_write_buffer_switch(wb);
129         keys = wb->keys[s.idx];
130         nr = s.nr;
131
132         /*
133          * We first sort so that we can detect and skip redundant updates, and
134          * then we attempt to flush in sorted btree order, as this is most
135          * efficient.
136          *
137          * However, since we're not flushing in the order they appear in the
138          * journal we won't be able to drop our journal pin until everything is
139          * flushed - which means this could deadlock the journal if we weren't
140          * passing BTREE_INSERT_JOURNAL_RECLAIM. This causes the update to fail
141          * if it would block taking a journal reservation.
142          *
143          * If that happens, simply skip the key so we can optimistically insert
144          * as many keys as possible in the fast path.
145          */
146         sort(keys, nr, sizeof(keys[0]),
147              btree_write_buffered_key_cmp, NULL);
148
149         for (i = keys; i < keys + nr; i++) {
150                 if (i + 1 < keys + nr &&
151                     i[0].btree == i[1].btree &&
152                     bpos_eq(i[0].k.k.p, i[1].k.k.p)) {
153                         skipped++;
154                         i->journal_seq = 0;
155                         continue;
156                 }
157
158                 if (write_locked &&
159                     (iter.path->btree_id != i->btree ||
160                      bpos_gt(i->k.k.p, iter.path->l[0].b->key.k.p))) {
161                         bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
162                         write_locked = false;
163                 }
164
165                 if (!iter.path || iter.path->btree_id != i->btree) {
166                         bch2_trans_iter_exit(trans, &iter);
167                         bch2_trans_iter_init(trans, &iter, i->btree, i->k.k.p, BTREE_ITER_INTENT);
168                 }
169
170                 bch2_btree_iter_set_pos(&iter, i->k.k.p);
171                 iter.path->preserve = false;
172
173                 do {
174                         ret = bch2_btree_write_buffer_flush_one(trans, &iter, i,
175                                                 commit_flags, &write_locked, &fast);
176                         if (!write_locked)
177                                 bch2_trans_begin(trans);
178                 } while (bch2_err_matches(ret, BCH_ERR_transaction_restart));
179
180                 if (ret == -BCH_ERR_journal_reclaim_would_deadlock) {
181                         slowpath++;
182                         continue;
183                 }
184                 if (ret)
185                         break;
186
187                 i->journal_seq = 0;
188         }
189
190         if (write_locked)
191                 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
192         bch2_trans_iter_exit(trans, &iter);
193
194         trace_write_buffer_flush(trans, nr, skipped, fast, wb->size);
195
196         if (slowpath)
197                 goto slowpath;
198
199         bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret));
200 out:
201         bch2_journal_pin_drop(j, &pin);
202         mutex_unlock(&wb->flush_lock);
203         return ret;
204 slowpath:
205         trace_write_buffer_flush_slowpath(trans, i - keys, nr);
206
207         /*
208          * Now sort the rest by journal seq and bump the journal pin as we go.
209          * The slowpath zapped the seq of keys that were successfully flushed so
210          * we can skip those here.
211          */
212         sort(keys, nr, sizeof(keys[0]),
213              btree_write_buffered_journal_cmp,
214              NULL);
215
216         commit_flags &= ~BCH_WATERMARK_MASK;
217         commit_flags |= BCH_WATERMARK_reclaim;
218
219         for (i = keys; i < keys + nr; i++) {
220                 if (!i->journal_seq)
221                         continue;
222
223                 if (i->journal_seq > pin.seq) {
224                         struct journal_entry_pin pin2;
225
226                         memset(&pin2, 0, sizeof(pin2));
227
228                         bch2_journal_pin_add(j, i->journal_seq, &pin2, NULL);
229                         bch2_journal_pin_drop(j, &pin);
230                         bch2_journal_pin_copy(j, &pin, &pin2, NULL);
231                         bch2_journal_pin_drop(j, &pin2);
232                 }
233
234                 ret = commit_do(trans, NULL, NULL,
235                                 commit_flags|
236                                 BTREE_INSERT_NOFAIL|
237                                 BTREE_INSERT_JOURNAL_RECLAIM,
238                                 __bch2_btree_insert(trans, i->btree, &i->k, 0));
239                 if (bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret)))
240                         break;
241         }
242
243         goto out;
244 }
245
246 int bch2_btree_write_buffer_flush_sync(struct btree_trans *trans)
247 {
248         bch2_trans_unlock(trans);
249         mutex_lock(&trans->c->btree_write_buffer.flush_lock);
250         return __bch2_btree_write_buffer_flush(trans, 0, true);
251 }
252
253 int bch2_btree_write_buffer_flush(struct btree_trans *trans)
254 {
255         return __bch2_btree_write_buffer_flush(trans, 0, false);
256 }
257
258 static int bch2_btree_write_buffer_journal_flush(struct journal *j,
259                                 struct journal_entry_pin *_pin, u64 seq)
260 {
261         struct bch_fs *c = container_of(j, struct bch_fs, journal);
262         struct btree_write_buffer *wb = &c->btree_write_buffer;
263
264         mutex_lock(&wb->flush_lock);
265
266         return bch2_trans_run(c,
267                         __bch2_btree_write_buffer_flush(&trans, BTREE_INSERT_NOCHECK_RW, true));
268 }
269
270 static inline u64 btree_write_buffer_ref(int idx)
271 {
272         return ((union btree_write_buffer_state) {
273                 .ref0 = idx == 0,
274                 .ref1 = idx == 1,
275         }).v;
276 }
277
278 int bch2_btree_insert_keys_write_buffer(struct btree_trans *trans)
279 {
280         struct bch_fs *c = trans->c;
281         struct btree_write_buffer *wb = &c->btree_write_buffer;
282         struct btree_write_buffered_key *i;
283         union btree_write_buffer_state old, new;
284         int ret = 0;
285         u64 v;
286
287         trans_for_each_wb_update(trans, i) {
288                 EBUG_ON(i->k.k.u64s > BTREE_WRITE_BUFERED_U64s_MAX);
289
290                 i->journal_seq          = trans->journal_res.seq;
291                 i->journal_offset       = trans->journal_res.offset;
292         }
293
294         preempt_disable();
295         v = READ_ONCE(wb->state.v);
296         do {
297                 old.v = new.v = v;
298
299                 new.v += btree_write_buffer_ref(new.idx);
300                 new.nr += trans->nr_wb_updates;
301                 if (new.nr > wb->size) {
302                         ret = -BCH_ERR_btree_insert_need_flush_buffer;
303                         goto out;
304                 }
305         } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
306
307         memcpy(wb->keys[new.idx] + old.nr,
308                trans->wb_updates,
309                sizeof(trans->wb_updates[0]) * trans->nr_wb_updates);
310
311         bch2_journal_pin_add(&c->journal, trans->journal_res.seq, &wb->journal_pin,
312                              bch2_btree_write_buffer_journal_flush);
313
314         atomic64_sub_return_release(btree_write_buffer_ref(new.idx), &wb->state.counter);
315 out:
316         preempt_enable();
317         return ret;
318 }
319
320 void bch2_fs_btree_write_buffer_exit(struct bch_fs *c)
321 {
322         struct btree_write_buffer *wb = &c->btree_write_buffer;
323
324         BUG_ON(wb->state.nr && !bch2_journal_error(&c->journal));
325
326         kvfree(wb->keys[1]);
327         kvfree(wb->keys[0]);
328 }
329
330 int bch2_fs_btree_write_buffer_init(struct bch_fs *c)
331 {
332         struct btree_write_buffer *wb = &c->btree_write_buffer;
333
334         mutex_init(&wb->flush_lock);
335         wb->size = c->opts.btree_write_buffer_size;
336
337         wb->keys[0] = kvmalloc_array(wb->size, sizeof(*wb->keys[0]), GFP_KERNEL);
338         wb->keys[1] = kvmalloc_array(wb->size, sizeof(*wb->keys[1]), GFP_KERNEL);
339         if (!wb->keys[0] || !wb->keys[1])
340                 return -BCH_ERR_ENOMEM_fs_btree_write_buffer_init;
341
342         return 0;
343 }