1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_locking.h"
5 #include "btree_update.h"
6 #include "btree_update_interior.h"
7 #include "btree_write_buffer.h"
10 #include "journal_reclaim.h"
12 #include <linux/sort.h>
14 static int btree_write_buffered_key_cmp(const void *_l, const void *_r)
16 const struct btree_write_buffered_key *l = _l;
17 const struct btree_write_buffered_key *r = _r;
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);
25 static int btree_write_buffered_journal_cmp(const void *_l, const void *_r)
27 const struct btree_write_buffered_key *l = _l;
28 const struct btree_write_buffered_key *r = _r;
30 return cmp_int(l->journal_seq, r->journal_seq);
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,
40 struct bch_fs *c = trans->c;
41 struct btree_path *path;
44 ret = bch2_btree_iter_traverse(iter);
51 ret = bch2_btree_node_lock_write(trans, path, &path->l[0].b->c);
55 bch2_btree_node_prep_for_write(trans, path, path->l[0].b);
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;
65 bch2_btree_insert_key_leaf(trans, path, &wb->k, wb->journal_seq);
69 return bch2_trans_update(trans, iter, &wb->k, 0) ?:
70 bch2_trans_commit(trans, NULL, NULL,
73 BTREE_INSERT_JOURNAL_RECLAIM);
76 static union btree_write_buffer_state btree_write_buffer_switch(struct btree_write_buffer *wb)
78 union btree_write_buffer_state old, new;
79 u64 v = READ_ONCE(wb->state.v);
86 } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
88 while (old.idx == 0 ? wb->state.ref0 : wb->state.ref1)
96 int __bch2_btree_write_buffer_flush(struct btree_trans *trans, unsigned commit_flags,
99 struct bch_fs *c = trans->c;
100 struct journal *j = &c->journal;
101 struct btree_write_buffer *wb = &c->btree_write_buffer;
102 struct journal_entry_pin pin;
103 struct btree_write_buffered_key *i, *dst, *keys;
104 struct btree_iter iter = { NULL };
105 size_t nr = 0, skipped = 0, fast = 0;
106 bool write_locked = false;
107 union btree_write_buffer_state s;
110 memset(&pin, 0, sizeof(pin));
112 if (!locked && !mutex_trylock(&wb->flush_lock))
115 bch2_journal_pin_copy(j, &pin, &wb->journal_pin, NULL);
116 bch2_journal_pin_drop(j, &wb->journal_pin);
118 s = btree_write_buffer_switch(wb);
119 keys = wb->keys[s.idx];
123 * We first sort so that we can detect and skip redundant updates, and
124 * then we attempt to flush in sorted btree order, as this is most
127 * However, since we're not flushing in the order they appear in the
128 * journal we won't be able to drop our journal pin until everything is
129 * flushed - which means this could deadlock the journal, if we weren't
130 * passing BTREE_INSERT_JORUNAL_RECLAIM. This causes the update to fail
131 * if it would block taking a journal reservation.
133 * If that happens, we sort them by the order they appeared in the
134 * journal - after dropping redundant entries - and then restart
135 * flushing, this time dropping journal pins as we go.
138 sort(keys, nr, sizeof(keys[0]),
139 btree_write_buffered_key_cmp, NULL);
141 for (i = keys; i < keys + nr; i++) {
142 if (i + 1 < keys + nr &&
143 i[0].btree == i[1].btree &&
144 bpos_eq(i[0].k.k.p, i[1].k.k.p)) {
150 (iter.path->btree_id != i->btree ||
151 bpos_gt(i->k.k.p, iter.path->l[0].b->key.k.p))) {
152 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
153 write_locked = false;
156 if (!iter.path || iter.path->btree_id != i->btree) {
157 bch2_trans_iter_exit(trans, &iter);
158 bch2_trans_iter_init(trans, &iter, i->btree, i->k.k.p, BTREE_ITER_INTENT);
161 bch2_btree_iter_set_pos(&iter, i->k.k.p);
162 iter.path->preserve = false;
165 ret = bch2_btree_write_buffer_flush_one(trans, &iter, i,
166 commit_flags, &write_locked, &fast);
168 bch2_trans_begin(trans);
169 } while (bch2_err_matches(ret, BCH_ERR_transaction_restart));
176 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
177 bch2_trans_iter_exit(trans, &iter);
179 trace_write_buffer_flush(trans, nr, skipped, fast, wb->size);
181 if (ret == -BCH_ERR_journal_reclaim_would_deadlock)
184 bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret));
186 bch2_journal_pin_drop(j, &pin);
187 mutex_unlock(&wb->flush_lock);
190 trace_write_buffer_flush_slowpath(trans, i - keys, nr);
193 for (; i < keys + nr; i++) {
194 if (i + 1 < keys + nr &&
195 i[0].btree == i[1].btree &&
196 bpos_eq(i[0].k.k.p, i[1].k.k.p))
204 sort(keys, nr, sizeof(keys[0]),
205 btree_write_buffered_journal_cmp,
208 for (i = keys; i < keys + nr; i++) {
209 if (i->journal_seq > pin.seq) {
210 struct journal_entry_pin pin2;
212 memset(&pin2, 0, sizeof(pin2));
214 bch2_journal_pin_add(j, i->journal_seq, &pin2, NULL);
215 bch2_journal_pin_drop(j, &pin);
216 bch2_journal_pin_copy(j, &pin, &pin2, NULL);
217 bch2_journal_pin_drop(j, &pin2);
220 ret = commit_do(trans, NULL, NULL,
223 BTREE_INSERT_JOURNAL_RECLAIM|
224 JOURNAL_WATERMARK_reserved,
225 __bch2_btree_insert(trans, i->btree, &i->k, 0));
226 if (bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret)))
233 int bch2_btree_write_buffer_flush_sync(struct btree_trans *trans)
235 bch2_trans_unlock(trans);
236 mutex_lock(&trans->c->btree_write_buffer.flush_lock);
237 return __bch2_btree_write_buffer_flush(trans, 0, true);
240 int bch2_btree_write_buffer_flush(struct btree_trans *trans)
242 return __bch2_btree_write_buffer_flush(trans, 0, false);
245 static int bch2_btree_write_buffer_journal_flush(struct journal *j,
246 struct journal_entry_pin *_pin, u64 seq)
248 struct bch_fs *c = container_of(j, struct bch_fs, journal);
249 struct btree_write_buffer *wb = &c->btree_write_buffer;
251 mutex_lock(&wb->flush_lock);
253 return bch2_trans_run(c,
254 __bch2_btree_write_buffer_flush(&trans, BTREE_INSERT_NOCHECK_RW, true));
257 static inline u64 btree_write_buffer_ref(int idx)
259 return ((union btree_write_buffer_state) {
265 int bch2_btree_insert_keys_write_buffer(struct btree_trans *trans)
267 struct bch_fs *c = trans->c;
268 struct btree_write_buffer *wb = &c->btree_write_buffer;
269 struct btree_write_buffered_key *i;
270 union btree_write_buffer_state old, new;
274 trans_for_each_wb_update(trans, i) {
275 EBUG_ON(i->k.k.u64s > BTREE_WRITE_BUFERED_U64s_MAX);
277 i->journal_seq = trans->journal_res.seq;
278 i->journal_offset = trans->journal_res.offset;
282 v = READ_ONCE(wb->state.v);
286 new.v += btree_write_buffer_ref(new.idx);
287 new.nr += trans->nr_wb_updates;
288 if (new.nr > wb->size) {
289 ret = -BCH_ERR_btree_insert_need_flush_buffer;
292 } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
294 memcpy(wb->keys[new.idx] + old.nr,
296 sizeof(trans->wb_updates[0]) * trans->nr_wb_updates);
298 bch2_journal_pin_add(&c->journal, trans->journal_res.seq, &wb->journal_pin,
299 bch2_btree_write_buffer_journal_flush);
301 atomic64_sub_return_release(btree_write_buffer_ref(new.idx), &wb->state.counter);
307 void bch2_fs_btree_write_buffer_exit(struct bch_fs *c)
309 struct btree_write_buffer *wb = &c->btree_write_buffer;
311 BUG_ON(wb->state.nr && !bch2_journal_error(&c->journal));
317 int bch2_fs_btree_write_buffer_init(struct bch_fs *c)
319 struct btree_write_buffer *wb = &c->btree_write_buffer;
321 mutex_init(&wb->flush_lock);
322 wb->size = c->opts.btree_write_buffer_size;
324 wb->keys[0] = kvmalloc_array(wb->size, sizeof(*wb->keys[0]), GFP_KERNEL);
325 wb->keys[1] = kvmalloc_array(wb->size, sizeof(*wb->keys[1]), GFP_KERNEL);
326 if (!wb->keys[0] || !wb->keys[1])