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/atomic.h>
13 #include <linux/sort.h>
15 static int btree_write_buffered_key_cmp(const void *_l, const void *_r)
17 const struct btree_write_buffered_key *l = _l;
18 const struct btree_write_buffered_key *r = _r;
20 return cmp_int(l->btree, r->btree) ?:
21 bpos_cmp(l->k.k.p, r->k.k.p) ?:
22 cmp_int(l->journal_seq, r->journal_seq) ?:
23 cmp_int(l->journal_offset, r->journal_offset);
26 static int btree_write_buffered_journal_cmp(const void *_l, const void *_r)
28 const struct btree_write_buffered_key *l = _l;
29 const struct btree_write_buffered_key *r = _r;
31 return cmp_int(l->journal_seq, r->journal_seq);
34 static int bch2_btree_write_buffer_flush_one(struct btree_trans *trans,
35 struct btree_iter *iter,
36 struct btree_write_buffered_key *wb,
37 unsigned commit_flags,
41 struct bch_fs *c = trans->c;
42 struct btree_path *path;
45 ret = bch2_btree_iter_traverse(iter);
52 ret = bch2_btree_node_lock_write(trans, path, &path->l[0].b->c);
56 bch2_btree_node_prep_for_write(trans, path, path->l[0].b);
60 if (!bch2_btree_node_insert_fits(c, path->l[0].b, wb->k.k.u64s)) {
61 bch2_btree_node_unlock_write(trans, path, path->l[0].b);
62 *write_locked = false;
66 bch2_btree_insert_key_leaf(trans, path, &wb->k, wb->journal_seq);
71 * We can't clone a path that has write locks: if the path is
72 * shared, unlock before set_pos(), traverse():
74 bch2_btree_node_unlock_write(trans, path, path->l[0].b);
75 *write_locked = false;
79 return bch2_trans_update_seq(trans, wb->journal_seq, iter, &wb->k,
80 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
81 bch2_trans_commit(trans, NULL, NULL,
83 BTREE_INSERT_NOCHECK_RW|
85 BTREE_INSERT_JOURNAL_RECLAIM);
88 static union btree_write_buffer_state btree_write_buffer_switch(struct btree_write_buffer *wb)
90 union btree_write_buffer_state old, new;
91 u64 v = READ_ONCE(wb->state.v);
98 } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
100 while (old.idx == 0 ? wb->state.ref0 : wb->state.ref1)
109 * Update a btree with a write buffered key using the journal seq of the
110 * original write buffer insert.
112 * It is not safe to rejournal the key once it has been inserted into the write
113 * buffer because that may break recovery ordering. For example, the key may
114 * have already been modified in the active write buffer in a seq that comes
115 * before the current transaction. If we were to journal this key again and
116 * crash, recovery would process updates in the wrong order.
119 btree_write_buffered_insert(struct btree_trans *trans,
120 struct btree_write_buffered_key *wb)
122 struct btree_iter iter;
125 bch2_trans_iter_init(trans, &iter, wb->btree, bkey_start_pos(&wb->k.k),
126 BTREE_ITER_CACHED|BTREE_ITER_INTENT);
128 ret = bch2_btree_iter_traverse(&iter) ?:
129 bch2_trans_update_seq(trans, wb->journal_seq, &iter, &wb->k,
130 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
131 bch2_trans_iter_exit(trans, &iter);
135 int __bch2_btree_write_buffer_flush(struct btree_trans *trans, unsigned commit_flags,
138 struct bch_fs *c = trans->c;
139 struct journal *j = &c->journal;
140 struct btree_write_buffer *wb = &c->btree_write_buffer;
141 struct journal_entry_pin pin;
142 struct btree_write_buffered_key *i, *keys;
143 struct btree_iter iter = { NULL };
144 size_t nr = 0, skipped = 0, fast = 0, slowpath = 0;
145 bool write_locked = false;
146 union btree_write_buffer_state s;
149 memset(&pin, 0, sizeof(pin));
151 if (!locked && !mutex_trylock(&wb->flush_lock))
154 bch2_journal_pin_copy(j, &pin, &wb->journal_pin, NULL);
155 bch2_journal_pin_drop(j, &wb->journal_pin);
157 s = btree_write_buffer_switch(wb);
158 keys = wb->keys[s.idx];
165 * We first sort so that we can detect and skip redundant updates, and
166 * then we attempt to flush in sorted btree order, as this is most
169 * However, since we're not flushing in the order they appear in the
170 * journal we won't be able to drop our journal pin until everything is
171 * flushed - which means this could deadlock the journal if we weren't
172 * passing BTREE_INSERT_JOURNAL_RECLAIM. This causes the update to fail
173 * if it would block taking a journal reservation.
175 * If that happens, simply skip the key so we can optimistically insert
176 * as many keys as possible in the fast path.
178 sort(keys, nr, sizeof(keys[0]),
179 btree_write_buffered_key_cmp, NULL);
181 for (i = keys; i < keys + nr; i++) {
182 if (i + 1 < keys + nr &&
183 i[0].btree == i[1].btree &&
184 bpos_eq(i[0].k.k.p, i[1].k.k.p)) {
191 (iter.path->btree_id != i->btree ||
192 bpos_gt(i->k.k.p, iter.path->l[0].b->key.k.p))) {
193 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
194 write_locked = false;
197 if (!iter.path || iter.path->btree_id != i->btree) {
198 bch2_trans_iter_exit(trans, &iter);
199 bch2_trans_iter_init(trans, &iter, i->btree, i->k.k.p,
200 BTREE_ITER_INTENT|BTREE_ITER_ALL_SNAPSHOTS);
203 bch2_btree_iter_set_pos(&iter, i->k.k.p);
204 iter.path->preserve = false;
207 ret = bch2_btree_write_buffer_flush_one(trans, &iter, i,
208 commit_flags, &write_locked, &fast);
210 bch2_trans_begin(trans);
211 } while (bch2_err_matches(ret, BCH_ERR_transaction_restart));
213 if (ret == -BCH_ERR_journal_reclaim_would_deadlock) {
224 bch2_btree_node_unlock_write(trans, iter.path, iter.path->l[0].b);
225 bch2_trans_iter_exit(trans, &iter);
227 trace_write_buffer_flush(trans, nr, skipped, fast, wb->size);
232 bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret));
234 bch2_journal_pin_drop(j, &pin);
235 mutex_unlock(&wb->flush_lock);
238 trace_write_buffer_flush_slowpath(trans, i - keys, nr);
241 * Now sort the rest by journal seq and bump the journal pin as we go.
242 * The slowpath zapped the seq of keys that were successfully flushed so
243 * we can skip those here.
245 sort(keys, nr, sizeof(keys[0]),
246 btree_write_buffered_journal_cmp,
249 commit_flags &= ~BCH_WATERMARK_MASK;
250 commit_flags |= BCH_WATERMARK_reclaim;
252 for (i = keys; i < keys + nr; i++) {
256 if (i->journal_seq > pin.seq) {
257 struct journal_entry_pin pin2;
259 memset(&pin2, 0, sizeof(pin2));
261 bch2_journal_pin_add(j, i->journal_seq, &pin2, NULL);
262 bch2_journal_pin_drop(j, &pin);
263 bch2_journal_pin_copy(j, &pin, &pin2, NULL);
264 bch2_journal_pin_drop(j, &pin2);
267 ret = commit_do(trans, NULL, NULL,
270 BTREE_INSERT_JOURNAL_RECLAIM,
271 btree_write_buffered_insert(trans, i));
272 if (bch2_fs_fatal_err_on(ret, c, "%s: insert error %s", __func__, bch2_err_str(ret)))
279 int bch2_btree_write_buffer_flush_sync(struct btree_trans *trans)
281 bch2_trans_unlock(trans);
282 mutex_lock(&trans->c->btree_write_buffer.flush_lock);
283 return __bch2_btree_write_buffer_flush(trans, 0, true);
286 int bch2_btree_write_buffer_flush(struct btree_trans *trans)
288 return __bch2_btree_write_buffer_flush(trans, 0, false);
291 static int bch2_btree_write_buffer_journal_flush(struct journal *j,
292 struct journal_entry_pin *_pin, u64 seq)
294 struct bch_fs *c = container_of(j, struct bch_fs, journal);
295 struct btree_write_buffer *wb = &c->btree_write_buffer;
297 mutex_lock(&wb->flush_lock);
299 return bch2_trans_run(c,
300 __bch2_btree_write_buffer_flush(trans, BTREE_INSERT_NOCHECK_RW, true));
303 static inline u64 btree_write_buffer_ref(int idx)
305 return ((union btree_write_buffer_state) {
311 int bch2_btree_insert_keys_write_buffer(struct btree_trans *trans)
313 struct bch_fs *c = trans->c;
314 struct btree_write_buffer *wb = &c->btree_write_buffer;
315 struct btree_write_buffered_key *i;
316 union btree_write_buffer_state old, new;
320 trans_for_each_wb_update(trans, i) {
321 EBUG_ON(i->k.k.u64s > BTREE_WRITE_BUFERED_U64s_MAX);
323 i->journal_seq = trans->journal_res.seq;
324 i->journal_offset = trans->journal_res.offset;
328 v = READ_ONCE(wb->state.v);
332 new.v += btree_write_buffer_ref(new.idx);
333 new.nr += trans->nr_wb_updates;
334 if (new.nr > wb->size) {
335 ret = -BCH_ERR_btree_insert_need_flush_buffer;
338 } while ((v = atomic64_cmpxchg_acquire(&wb->state.counter, old.v, new.v)) != old.v);
340 memcpy(wb->keys[new.idx] + old.nr,
342 sizeof(trans->wb_updates[0]) * trans->nr_wb_updates);
344 bch2_journal_pin_add(&c->journal, trans->journal_res.seq, &wb->journal_pin,
345 bch2_btree_write_buffer_journal_flush);
347 atomic64_sub_return_release(btree_write_buffer_ref(new.idx), &wb->state.counter);
353 void bch2_fs_btree_write_buffer_exit(struct bch_fs *c)
355 struct btree_write_buffer *wb = &c->btree_write_buffer;
357 BUG_ON(wb->state.nr && !bch2_journal_error(&c->journal));
363 int bch2_fs_btree_write_buffer_init(struct bch_fs *c)
365 struct btree_write_buffer *wb = &c->btree_write_buffer;
367 mutex_init(&wb->flush_lock);
368 wb->size = c->opts.btree_write_buffer_size;
370 wb->keys[0] = kvmalloc_array(wb->size, sizeof(*wb->keys[0]), GFP_KERNEL);
371 wb->keys[1] = kvmalloc_array(wb->size, sizeof(*wb->keys[1]), GFP_KERNEL);
372 if (!wb->keys[0] || !wb->keys[1])
373 return -BCH_ERR_ENOMEM_fs_btree_write_buffer_init;