blob: 77fbfd52edcf99d6e3a35d2b9adc9d2b638c6088 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
3 * bcache journalling code, for btree insertions
4 *
5 * Copyright 2012 Google, Inc.
6 */
7
8#include "bcache.h"
9#include "btree.h"
10#include "debug.h"
Kent Overstreet9aa61a92014-04-10 17:58:49 -070011#include "extents.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070012
Kent Overstreetc37511b2013-04-26 15:39:55 -070013#include <trace/events/bcache.h>
14
Kent Overstreetcafe5632013-03-23 16:11:31 -070015/*
16 * Journal replay/recovery:
17 *
18 * This code is all driven from run_cache_set(); we first read the journal
19 * entries, do some other stuff, then we mark all the keys in the journal
20 * entries (same as garbage collection would), then we replay them - reinserting
21 * them into the cache in precisely the same order as they appear in the
22 * journal.
23 *
24 * We only journal keys that go in leaf nodes, which simplifies things quite a
25 * bit.
26 */
27
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020028static void journal_read_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -070029{
30 struct closure *cl = bio->bi_private;
Coly Li1fae7cf2018-08-11 13:19:45 +080031
Kent Overstreetcafe5632013-03-23 16:11:31 -070032 closure_put(cl);
33}
34
35static int journal_read_bucket(struct cache *ca, struct list_head *list,
Coly Li6f10f7d2018-08-11 13:19:44 +080036 unsigned int bucket_index)
Kent Overstreetcafe5632013-03-23 16:11:31 -070037{
38 struct journal_device *ja = &ca->journal;
39 struct bio *bio = &ja->bio;
40
41 struct journal_replay *i;
42 struct jset *j, *data = ca->set->journal.w[0].data;
Kent Overstreetc18536a2013-07-24 17:44:17 -070043 struct closure cl;
Coly Li6f10f7d2018-08-11 13:19:44 +080044 unsigned int len, left, offset = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -070045 int ret = 0;
46 sector_t bucket = bucket_to_sector(ca->set, ca->sb.d[bucket_index]);
47
Kent Overstreetc18536a2013-07-24 17:44:17 -070048 closure_init_stack(&cl);
49
Joe Perches46f5aa82020-05-27 12:01:52 +080050 pr_debug("reading %u\n", bucket_index);
Kent Overstreetcafe5632013-03-23 16:11:31 -070051
52 while (offset < ca->sb.bucket_size) {
53reread: left = ca->sb.bucket_size - offset;
Coly Li6f10f7d2018-08-11 13:19:44 +080054 len = min_t(unsigned int, left, PAGE_SECTORS << JSET_BITS);
Kent Overstreetcafe5632013-03-23 16:11:31 -070055
56 bio_reset(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -070057 bio->bi_iter.bi_sector = bucket + offset;
Christoph Hellwig74d46992017-08-23 19:10:32 +020058 bio_set_dev(bio, ca->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -070059 bio->bi_iter.bi_size = len << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -070060
61 bio->bi_end_io = journal_read_endio;
Kent Overstreetc18536a2013-07-24 17:44:17 -070062 bio->bi_private = &cl;
Mike Christiead0d9e72016-06-05 14:32:05 -050063 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Kent Overstreet169ef1c2013-03-28 12:50:55 -060064 bch_bio_map(bio, data);
Kent Overstreetcafe5632013-03-23 16:11:31 -070065
Coly Li771f3932018-03-18 17:36:17 -070066 closure_bio_submit(ca->set, bio, &cl);
Kent Overstreetc18536a2013-07-24 17:44:17 -070067 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -070068
69 /* This function could be simpler now since we no longer write
70 * journal entries that overlap bucket boundaries; this means
71 * the start of a bucket will always have a valid journal entry
72 * if it has any journal entries at all.
73 */
74
75 j = data;
76 while (len) {
77 struct list_head *where;
78 size_t blocks, bytes = set_bytes(j);
79
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070080 if (j->magic != jset_magic(&ca->sb)) {
Joe Perches46f5aa82020-05-27 12:01:52 +080081 pr_debug("%u: bad magic\n", bucket_index);
Kent Overstreetcafe5632013-03-23 16:11:31 -070082 return ret;
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070083 }
Kent Overstreetcafe5632013-03-23 16:11:31 -070084
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070085 if (bytes > left << 9 ||
86 bytes > PAGE_SIZE << JSET_BITS) {
Joe Perches46f5aa82020-05-27 12:01:52 +080087 pr_info("%u: too big, %zu bytes, offset %u\n",
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070088 bucket_index, bytes, offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -070089 return ret;
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070090 }
Kent Overstreetcafe5632013-03-23 16:11:31 -070091
92 if (bytes > len << 9)
93 goto reread;
94
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070095 if (j->csum != csum_set(j)) {
Joe Perches46f5aa82020-05-27 12:01:52 +080096 pr_info("%u: bad csum, %zu bytes, offset %u\n",
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070097 bucket_index, bytes, offset);
Kent Overstreetcafe5632013-03-23 16:11:31 -070098 return ret;
Kent Overstreetb3fa7e72013-08-05 14:04:06 -070099 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700100
Kent Overstreetee811282013-12-17 23:49:49 -0800101 blocks = set_blocks(j, block_bytes(ca->set));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700102
Coly Li2464b692019-06-28 19:59:50 +0800103 /*
104 * Nodes in 'list' are in linear increasing order of
105 * i->j.seq, the node on head has the smallest (oldest)
106 * journal seq, the node on tail has the biggest
107 * (latest) journal seq.
108 */
109
110 /*
111 * Check from the oldest jset for last_seq. If
112 * i->j.seq < j->last_seq, it means the oldest jset
113 * in list is expired and useless, remove it from
114 * this list. Otherwise, j is a condidate jset for
115 * further following checks.
116 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700117 while (!list_empty(list)) {
118 i = list_first_entry(list,
119 struct journal_replay, list);
120 if (i->j.seq >= j->last_seq)
121 break;
122 list_del(&i->list);
123 kfree(i);
124 }
125
Coly Li2464b692019-06-28 19:59:50 +0800126 /* iterate list in reverse order (from latest jset) */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700127 list_for_each_entry_reverse(i, list, list) {
128 if (j->seq == i->j.seq)
129 goto next_set;
130
Coly Li2464b692019-06-28 19:59:50 +0800131 /*
132 * if j->seq is less than any i->j.last_seq
133 * in list, j is an expired and useless jset.
134 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700135 if (j->seq < i->j.last_seq)
136 goto next_set;
137
Coly Li2464b692019-06-28 19:59:50 +0800138 /*
139 * 'where' points to first jset in list which
140 * is elder then j.
141 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700142 if (j->seq > i->j.seq) {
143 where = &i->list;
144 goto add;
145 }
146 }
147
148 where = list;
149add:
150 i = kmalloc(offsetof(struct journal_replay, j) +
151 bytes, GFP_KERNEL);
152 if (!i)
153 return -ENOMEM;
154 memcpy(&i->j, j, bytes);
Coly Li2464b692019-06-28 19:59:50 +0800155 /* Add to the location after 'where' points to */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700156 list_add(&i->list, where);
157 ret = 1;
158
Coly Lia231f072019-06-28 19:59:51 +0800159 if (j->seq > ja->seq[bucket_index])
160 ja->seq[bucket_index] = j->seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700161next_set:
162 offset += blocks * ca->sb.block_size;
163 len -= blocks * ca->sb.block_size;
164 j = ((void *) j) + blocks * block_bytes(ca);
165 }
166 }
167
168 return ret;
169}
170
Kent Overstreetc18536a2013-07-24 17:44:17 -0700171int bch_journal_read(struct cache_set *c, struct list_head *list)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700172{
173#define read_bucket(b) \
174 ({ \
Coly Li14215ee2019-04-25 00:48:32 +0800175 ret = journal_read_bucket(ca, list, b); \
Kent Overstreetcafe5632013-03-23 16:11:31 -0700176 __set_bit(b, bitmap); \
177 if (ret < 0) \
178 return ret; \
179 ret; \
180 })
181
182 struct cache *ca;
Coly Li6f10f7d2018-08-11 13:19:44 +0800183 unsigned int iter;
Coly Li14215ee2019-04-25 00:48:32 +0800184 int ret = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700185
186 for_each_cache(ca, c, iter) {
187 struct journal_device *ja = &ca->journal;
Joe Perchesd1aa1ab2015-06-30 14:59:54 -0700188 DECLARE_BITMAP(bitmap, SB_JOURNAL_BUCKETS);
Coly Li6f10f7d2018-08-11 13:19:44 +0800189 unsigned int i, l, r, m;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190 uint64_t seq;
191
192 bitmap_zero(bitmap, SB_JOURNAL_BUCKETS);
Joe Perches46f5aa82020-05-27 12:01:52 +0800193 pr_debug("%u journal buckets\n", ca->sb.njournal_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700194
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700195 /*
196 * Read journal buckets ordered by golden ratio hash to quickly
Kent Overstreetcafe5632013-03-23 16:11:31 -0700197 * find a sequence of buckets with valid journal entries
198 */
199 for (i = 0; i < ca->sb.njournal_buckets; i++) {
Tang Junhuibb22caf2017-11-24 15:14:24 -0800200 /*
201 * We must try the index l with ZERO first for
202 * correctness due to the scenario that the journal
203 * bucket is circular buffer which might have wrapped
204 */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700205 l = (i * 2654435769U) % ca->sb.njournal_buckets;
206
207 if (test_bit(l, bitmap))
208 break;
209
210 if (read_bucket(l))
211 goto bsearch;
212 }
213
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700214 /*
215 * If that fails, check all the buckets we haven't checked
Kent Overstreetcafe5632013-03-23 16:11:31 -0700216 * already
217 */
Joe Perches46f5aa82020-05-27 12:01:52 +0800218 pr_debug("falling back to linear search\n");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219
Xu Wangef4eeb82020-07-25 20:00:17 +0800220 for_each_clear_bit(l, bitmap, ca->sb.njournal_buckets)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700221 if (read_bucket(l))
222 goto bsearch;
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700223
Kent Overstreet6b708de2014-06-02 15:39:44 -0700224 /* no journal entries on this device? */
225 if (l == ca->sb.njournal_buckets)
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700226 continue;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700227bsearch:
Kent Overstreet6b708de2014-06-02 15:39:44 -0700228 BUG_ON(list_empty(list));
229
Kent Overstreetcafe5632013-03-23 16:11:31 -0700230 /* Binary search */
Surbhi Palandedbd810a2014-04-10 16:09:51 -0700231 m = l;
232 r = find_next_bit(bitmap, ca->sb.njournal_buckets, l + 1);
Joe Perches46f5aa82020-05-27 12:01:52 +0800233 pr_debug("starting binary search, l %u r %u\n", l, r);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700234
235 while (l + 1 < r) {
Kent Overstreetfaa56732013-07-11 22:42:14 -0700236 seq = list_entry(list->prev, struct journal_replay,
237 list)->j.seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700238
Kent Overstreetfaa56732013-07-11 22:42:14 -0700239 m = (l + r) >> 1;
240 read_bucket(m);
241
242 if (seq != list_entry(list->prev, struct journal_replay,
243 list)->j.seq)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700244 l = m;
245 else
246 r = m;
247 }
248
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700249 /*
250 * Read buckets in reverse order until we stop finding more
Kent Overstreetcafe5632013-03-23 16:11:31 -0700251 * journal entries
252 */
Joe Perches46f5aa82020-05-27 12:01:52 +0800253 pr_debug("finishing up: m %u njournal_buckets %u\n",
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700254 m, ca->sb.njournal_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700255 l = m;
256
257 while (1) {
258 if (!l--)
259 l = ca->sb.njournal_buckets - 1;
260
261 if (l == m)
262 break;
263
264 if (test_bit(l, bitmap))
265 continue;
266
267 if (!read_bucket(l))
268 break;
269 }
270
271 seq = 0;
272
273 for (i = 0; i < ca->sb.njournal_buckets; i++)
274 if (ja->seq[i] > seq) {
275 seq = ja->seq[i];
Kent Overstreet27201cfd2014-03-13 13:44:21 -0700276 /*
277 * When journal_reclaim() goes to allocate for
278 * the first time, it'll use the bucket after
279 * ja->cur_idx
280 */
281 ja->cur_idx = i;
282 ja->last_idx = ja->discard_idx = (i + 1) %
283 ca->sb.njournal_buckets;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700284
285 }
286 }
287
Kent Overstreetc426c4f2013-09-23 23:17:29 -0700288 if (!list_empty(list))
289 c->journal.seq = list_entry(list->prev,
290 struct journal_replay,
291 list)->j.seq;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700292
Coly Li0ae49cb2019-06-28 19:59:26 +0800293 return 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700294#undef read_bucket
295}
296
297void bch_journal_mark(struct cache_set *c, struct list_head *list)
298{
299 atomic_t p = { 0 };
300 struct bkey *k;
301 struct journal_replay *i;
302 struct journal *j = &c->journal;
303 uint64_t last = j->seq;
304
305 /*
306 * journal.pin should never fill up - we never write a journal
307 * entry when it would fill up. But if for some reason it does, we
308 * iterate over the list in reverse order so that we can just skip that
309 * refcount instead of bugging.
310 */
311
312 list_for_each_entry_reverse(i, list, list) {
313 BUG_ON(last < i->j.seq);
314 i->pin = NULL;
315
316 while (last-- != i->j.seq)
317 if (fifo_free(&j->pin) > 1) {
318 fifo_push_front(&j->pin, p);
319 atomic_set(&fifo_front(&j->pin), 0);
320 }
321
322 if (fifo_free(&j->pin) > 1) {
323 fifo_push_front(&j->pin, p);
324 i->pin = &fifo_front(&j->pin);
325 atomic_set(i->pin, 1);
326 }
327
328 for (k = i->j.start;
Kent Overstreetfafff812013-12-17 21:56:21 -0800329 k < bset_bkey_last(&i->j);
Kent Overstreet9aa61a92014-04-10 17:58:49 -0700330 k = bkey_next(k))
331 if (!__bch_extent_invalid(c, k)) {
Coly Li6f10f7d2018-08-11 13:19:44 +0800332 unsigned int j;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700333
Kent Overstreet9aa61a92014-04-10 17:58:49 -0700334 for (j = 0; j < KEY_PTRS(k); j++)
335 if (ptr_available(c, k, j))
336 atomic_inc(&PTR_BUCKET(c, k, j)->pin);
Kent Overstreet65ddf452014-02-24 19:55:28 -0800337
Kent Overstreet9aa61a92014-04-10 17:58:49 -0700338 bch_initial_mark_key(c, 0, k);
339 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700340 }
341}
342
Jens Axboe2d5abb92019-05-01 06:34:09 -0600343static bool is_discard_enabled(struct cache_set *s)
Tang Junhui63120732019-04-25 00:48:41 +0800344{
345 struct cache *ca;
346 unsigned int i;
347
348 for_each_cache(ca, s, i)
349 if (ca->discard)
350 return true;
351
352 return false;
353}
354
Kent Overstreetc18536a2013-07-24 17:44:17 -0700355int bch_journal_replay(struct cache_set *s, struct list_head *list)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700356{
357 int ret = 0, keys = 0, entries = 0;
358 struct bkey *k;
359 struct journal_replay *i =
360 list_entry(list->prev, struct journal_replay, list);
361
362 uint64_t start = i->j.last_seq, end = i->j.seq, n = start;
Kent Overstreet0b932072013-07-24 17:26:51 -0700363 struct keylist keylist;
364
Kent Overstreetcafe5632013-03-23 16:11:31 -0700365 list_for_each_entry(i, list, list) {
366 BUG_ON(i->pin && atomic_read(i->pin) != 1);
367
Coly Li68d10e62019-04-25 00:48:36 +0800368 if (n != i->j.seq) {
Tang Junhui63120732019-04-25 00:48:41 +0800369 if (n == start && is_discard_enabled(s))
Joe Perches46f5aa82020-05-27 12:01:52 +0800370 pr_info("journal entries %llu-%llu may be discarded! (replaying %llu-%llu)\n",
Tang Junhui63120732019-04-25 00:48:41 +0800371 n, i->j.seq - 1, start, end);
372 else {
Joe Perches46f5aa82020-05-27 12:01:52 +0800373 pr_err("journal entries %llu-%llu missing! (replaying %llu-%llu)\n",
Tang Junhui63120732019-04-25 00:48:41 +0800374 n, i->j.seq - 1, start, end);
375 ret = -EIO;
376 goto err;
377 }
Coly Li68d10e62019-04-25 00:48:36 +0800378 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700379
380 for (k = i->j.start;
Kent Overstreetfafff812013-12-17 21:56:21 -0800381 k < bset_bkey_last(&i->j);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700382 k = bkey_next(k)) {
Kent Overstreetc37511b2013-04-26 15:39:55 -0700383 trace_bcache_journal_replay_key(k);
384
Kent Overstreetc13f3af2014-01-08 21:22:02 -0800385 bch_keylist_init_single(&keylist, k);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700386
Kent Overstreetcc7b8812013-07-24 18:07:22 -0700387 ret = bch_btree_insert(s, &keylist, i->pin, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700388 if (ret)
389 goto err;
390
Kent Overstreet0b932072013-07-24 17:26:51 -0700391 BUG_ON(!bch_keylist_empty(&keylist));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700392 keys++;
393
394 cond_resched();
395 }
396
397 if (i->pin)
398 atomic_dec(i->pin);
399 n = i->j.seq + 1;
400 entries++;
401 }
402
Joe Perches46f5aa82020-05-27 12:01:52 +0800403 pr_info("journal replay done, %i keys in %i entries, seq %llu\n",
Kent Overstreetcafe5632013-03-23 16:11:31 -0700404 keys, entries, end);
Kent Overstreetb54d6932013-07-24 18:04:18 -0700405err:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700406 while (!list_empty(list)) {
407 i = list_first_entry(list, struct journal_replay, list);
408 list_del(&i->list);
409 kfree(i);
410 }
Kent Overstreetb54d6932013-07-24 18:04:18 -0700411
Kent Overstreetcafe5632013-03-23 16:11:31 -0700412 return ret;
413}
414
415/* Journalling */
416
417static void btree_flush_write(struct cache_set *c)
418{
Coly Li91be66e2019-06-28 19:59:59 +0800419 struct btree *b, *t, *btree_nodes[BTREE_FLUSH_NR];
Coly Lid1c3cc32020-02-01 22:42:34 +0800420 unsigned int i, nr;
421 int ref_nr;
Coly Li2aa8c522020-01-24 01:01:37 +0800422 atomic_t *fifo_front_p, *now_fifo_front_p;
423 size_t mask;
Coly Li91be66e2019-06-28 19:59:59 +0800424
425 if (c->journal.btree_flushing)
426 return;
427
428 spin_lock(&c->journal.flush_write_lock);
429 if (c->journal.btree_flushing) {
430 spin_unlock(&c->journal.flush_write_lock);
431 return;
432 }
433 c->journal.btree_flushing = true;
434 spin_unlock(&c->journal.flush_write_lock);
Tang Junhuia728eac2018-02-07 11:41:39 -0800435
Coly Li2aa8c522020-01-24 01:01:37 +0800436 /* get the oldest journal entry and check its refcount */
437 spin_lock(&c->journal.lock);
438 fifo_front_p = &fifo_front(&c->journal.pin);
439 ref_nr = atomic_read(fifo_front_p);
440 if (ref_nr <= 0) {
441 /*
442 * do nothing if no btree node references
443 * the oldest journal entry
444 */
445 spin_unlock(&c->journal.lock);
446 goto out;
447 }
448 spin_unlock(&c->journal.lock);
449
450 mask = c->journal.pin.mask;
451 nr = 0;
Tang Junhuia728eac2018-02-07 11:41:39 -0800452 atomic_long_inc(&c->flush_write);
Coly Li91be66e2019-06-28 19:59:59 +0800453 memset(btree_nodes, 0, sizeof(btree_nodes));
Coly Li249a5f62019-06-28 19:59:54 +0800454
Coly Li50a260e2019-06-28 19:59:58 +0800455 mutex_lock(&c->bucket_lock);
Coly Li91be66e2019-06-28 19:59:59 +0800456 list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) {
Coly Li2aa8c522020-01-24 01:01:37 +0800457 /*
458 * It is safe to get now_fifo_front_p without holding
459 * c->journal.lock here, because we don't need to know
460 * the exactly accurate value, just check whether the
461 * front pointer of c->journal.pin is changed.
462 */
463 now_fifo_front_p = &fifo_front(&c->journal.pin);
464 /*
465 * If the oldest journal entry is reclaimed and front
466 * pointer of c->journal.pin changes, it is unnecessary
467 * to scan c->btree_cache anymore, just quit the loop and
468 * flush out what we have already.
469 */
470 if (now_fifo_front_p != fifo_front_p)
471 break;
472 /*
473 * quit this loop if all matching btree nodes are
474 * scanned and record in btree_nodes[] already.
475 */
476 ref_nr = atomic_read(fifo_front_p);
477 if (nr >= ref_nr)
478 break;
479
Coly Li91be66e2019-06-28 19:59:59 +0800480 if (btree_node_journal_flush(b))
Joe Perches46f5aa82020-05-27 12:01:52 +0800481 pr_err("BUG: flush_write bit should not be set here!\n");
Coly Li91be66e2019-06-28 19:59:59 +0800482
483 mutex_lock(&b->write_lock);
484
485 if (!btree_node_dirty(b)) {
486 mutex_unlock(&b->write_lock);
487 continue;
Coly Li249a5f62019-06-28 19:59:54 +0800488 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700489
Coly Li91be66e2019-06-28 19:59:59 +0800490 if (!btree_current_write(b)->journal) {
491 mutex_unlock(&b->write_lock);
492 continue;
493 }
494
Coly Li2aa8c522020-01-24 01:01:37 +0800495 /*
496 * Only select the btree node which exactly references
497 * the oldest journal entry.
498 *
499 * If the journal entry pointed by fifo_front_p is
500 * reclaimed in parallel, don't worry:
501 * - the list_for_each_xxx loop will quit when checking
502 * next now_fifo_front_p.
503 * - If there are matched nodes recorded in btree_nodes[],
504 * they are clean now (this is why and how the oldest
505 * journal entry can be reclaimed). These selected nodes
506 * will be ignored and skipped in the folowing for-loop.
507 */
Coly Li4ec31cb2020-02-13 22:12:07 +0800508 if (((btree_current_write(b)->journal - fifo_front_p) &
509 mask) != 0) {
Coly Li2aa8c522020-01-24 01:01:37 +0800510 mutex_unlock(&b->write_lock);
511 continue;
512 }
513
Coly Li50a260e2019-06-28 19:59:58 +0800514 set_btree_node_journal_flush(b);
Coly Li91be66e2019-06-28 19:59:59 +0800515
516 mutex_unlock(&b->write_lock);
517
Coly Li2aa8c522020-01-24 01:01:37 +0800518 btree_nodes[nr++] = b;
519 /*
520 * To avoid holding c->bucket_lock too long time,
521 * only scan for BTREE_FLUSH_NR matched btree nodes
522 * at most. If there are more btree nodes reference
523 * the oldest journal entry, try to flush them next
524 * time when btree_flush_write() is called.
525 */
526 if (nr == BTREE_FLUSH_NR)
Coly Li91be66e2019-06-28 19:59:59 +0800527 break;
528 }
Coly Li50a260e2019-06-28 19:59:58 +0800529 mutex_unlock(&c->bucket_lock);
530
Coly Li2aa8c522020-01-24 01:01:37 +0800531 for (i = 0; i < nr; i++) {
Coly Li91be66e2019-06-28 19:59:59 +0800532 b = btree_nodes[i];
533 if (!b) {
Joe Perches46f5aa82020-05-27 12:01:52 +0800534 pr_err("BUG: btree_nodes[%d] is NULL\n", i);
Coly Li91be66e2019-06-28 19:59:59 +0800535 continue;
536 }
537
538 /* safe to check without holding b->write_lock */
539 if (!btree_node_journal_flush(b)) {
Joe Perches46f5aa82020-05-27 12:01:52 +0800540 pr_err("BUG: bnode %p: journal_flush bit cleaned\n", b);
Coly Li91be66e2019-06-28 19:59:59 +0800541 continue;
542 }
543
Kent Overstreet2a285682014-03-04 16:42:42 -0800544 mutex_lock(&b->write_lock);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700545 if (!btree_current_write(b)->journal) {
Coly Li50a260e2019-06-28 19:59:58 +0800546 clear_bit(BTREE_NODE_journal_flush, &b->flags);
Kent Overstreet2a285682014-03-04 16:42:42 -0800547 mutex_unlock(&b->write_lock);
Joe Perches46f5aa82020-05-27 12:01:52 +0800548 pr_debug("bnode %p: written by others\n", b);
Coly Li91be66e2019-06-28 19:59:59 +0800549 continue;
550 }
551
552 if (!btree_node_dirty(b)) {
553 clear_bit(BTREE_NODE_journal_flush, &b->flags);
554 mutex_unlock(&b->write_lock);
Joe Perches46f5aa82020-05-27 12:01:52 +0800555 pr_debug("bnode %p: dirty bit cleaned by others\n", b);
Coly Li91be66e2019-06-28 19:59:59 +0800556 continue;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700557 }
558
Kent Overstreet2a285682014-03-04 16:42:42 -0800559 __bch_btree_node_write(b, NULL);
Coly Li50a260e2019-06-28 19:59:58 +0800560 clear_bit(BTREE_NODE_journal_flush, &b->flags);
Kent Overstreet2a285682014-03-04 16:42:42 -0800561 mutex_unlock(&b->write_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700562 }
Coly Li91be66e2019-06-28 19:59:59 +0800563
Coly Li2aa8c522020-01-24 01:01:37 +0800564out:
Coly Li91be66e2019-06-28 19:59:59 +0800565 spin_lock(&c->journal.flush_write_lock);
566 c->journal.btree_flushing = false;
567 spin_unlock(&c->journal.flush_write_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700568}
569
570#define last_seq(j) ((j)->seq - fifo_used(&(j)->pin) + 1)
571
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200572static void journal_discard_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700573{
574 struct journal_device *ja =
575 container_of(bio, struct journal_device, discard_bio);
576 struct cache *ca = container_of(ja, struct cache, journal);
577
578 atomic_set(&ja->discard_in_flight, DISCARD_DONE);
579
580 closure_wake_up(&ca->set->journal.wait);
581 closure_put(&ca->set->cl);
582}
583
584static void journal_discard_work(struct work_struct *work)
585{
586 struct journal_device *ja =
587 container_of(work, struct journal_device, discard_work);
588
Mike Christie4e49ea42016-06-05 14:31:41 -0500589 submit_bio(&ja->discard_bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700590}
591
592static void do_journal_discard(struct cache *ca)
593{
594 struct journal_device *ja = &ca->journal;
595 struct bio *bio = &ja->discard_bio;
596
597 if (!ca->discard) {
598 ja->discard_idx = ja->last_idx;
599 return;
600 }
601
Kent Overstreet6d9d21e2013-09-23 23:17:27 -0700602 switch (atomic_read(&ja->discard_in_flight)) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700603 case DISCARD_IN_FLIGHT:
604 return;
605
606 case DISCARD_DONE:
607 ja->discard_idx = (ja->discard_idx + 1) %
608 ca->sb.njournal_buckets;
609
610 atomic_set(&ja->discard_in_flight, DISCARD_READY);
611 /* fallthrough */
612
613 case DISCARD_READY:
614 if (ja->discard_idx == ja->last_idx)
615 return;
616
617 atomic_set(&ja->discard_in_flight, DISCARD_IN_FLIGHT);
618
Ming Lei3a83f462016-11-22 08:57:21 -0700619 bio_init(bio, bio->bi_inline_vecs, 1);
Mike Christiead0d9e72016-06-05 14:32:05 -0500620 bio_set_op_attrs(bio, REQ_OP_DISCARD, 0);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700621 bio->bi_iter.bi_sector = bucket_to_sector(ca->set,
Kent Overstreetb1a67b02013-03-25 11:46:44 -0700622 ca->sb.d[ja->discard_idx]);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200623 bio_set_dev(bio, ca->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700624 bio->bi_iter.bi_size = bucket_bytes(ca);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700625 bio->bi_end_io = journal_discard_endio;
626
627 closure_get(&ca->set->cl);
628 INIT_WORK(&ja->discard_work, journal_discard_work);
Guoju Fang0f843e62018-09-27 23:41:46 +0800629 queue_work(bch_journal_wq, &ja->discard_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700630 }
631}
632
633static void journal_reclaim(struct cache_set *c)
634{
635 struct bkey *k = &c->journal.key;
636 struct cache *ca;
637 uint64_t last_seq;
Coly Li6f10f7d2018-08-11 13:19:44 +0800638 unsigned int iter, n = 0;
Bart Van Assche42361462018-03-18 17:36:31 -0700639 atomic_t p __maybe_unused;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700640
Tang Junhuia728eac2018-02-07 11:41:39 -0800641 atomic_long_inc(&c->reclaim);
642
Kent Overstreetcafe5632013-03-23 16:11:31 -0700643 while (!atomic_read(&fifo_front(&c->journal.pin)))
644 fifo_pop(&c->journal.pin, p);
645
646 last_seq = last_seq(&c->journal);
647
648 /* Update last_idx */
649
650 for_each_cache(ca, c, iter) {
651 struct journal_device *ja = &ca->journal;
652
653 while (ja->last_idx != ja->cur_idx &&
654 ja->seq[ja->last_idx] < last_seq)
655 ja->last_idx = (ja->last_idx + 1) %
656 ca->sb.njournal_buckets;
657 }
658
659 for_each_cache(ca, c, iter)
660 do_journal_discard(ca);
661
662 if (c->journal.blocks_free)
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700663 goto out;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700664
665 /*
666 * Allocate:
667 * XXX: Sort by free journal space
668 */
669
670 for_each_cache(ca, c, iter) {
671 struct journal_device *ja = &ca->journal;
Coly Li6f10f7d2018-08-11 13:19:44 +0800672 unsigned int next = (ja->cur_idx + 1) % ca->sb.njournal_buckets;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700673
674 /* No space available on this device */
675 if (next == ja->discard_idx)
676 continue;
677
678 ja->cur_idx = next;
Huacai Chencf33c1e2017-11-24 15:14:25 -0800679 k->ptr[n++] = MAKE_PTR(0,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700680 bucket_to_sector(c, ca->sb.d[ja->cur_idx]),
681 ca->sb.nr_this_dev);
Coly Lidff90d52019-06-28 20:00:00 +0800682 atomic_long_inc(&c->reclaimed_journal_buckets);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700683 }
684
Coly Li1bee2ad2019-04-25 00:48:33 +0800685 if (n) {
686 bkey_init(k);
687 SET_KEY_PTRS(k, n);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700688 c->journal.blocks_free = c->sb.bucket_size >> c->block_bits;
Coly Li1bee2ad2019-04-25 00:48:33 +0800689 }
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700690out:
Kent Overstreetcafe5632013-03-23 16:11:31 -0700691 if (!journal_full(&c->journal))
692 __closure_wake_up(&c->journal.wait);
693}
694
695void bch_journal_next(struct journal *j)
696{
697 atomic_t p = { 1 };
698
699 j->cur = (j->cur == j->w)
700 ? &j->w[1]
701 : &j->w[0];
702
703 /*
704 * The fifo_push() needs to happen at the same time as j->seq is
705 * incremented for last_seq() to be calculated correctly
706 */
707 BUG_ON(!fifo_push(&j->pin, p));
708 atomic_set(&fifo_back(&j->pin), 1);
709
710 j->cur->data->seq = ++j->seq;
Kent Overstreetdabb4432014-02-19 19:48:26 -0800711 j->cur->dirty = false;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700712 j->cur->need_write = false;
713 j->cur->data->keys = 0;
714
715 if (fifo_full(&j->pin))
Joe Perches46f5aa82020-05-27 12:01:52 +0800716 pr_debug("journal_pin full (%zu)\n", fifo_used(&j->pin));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700717}
718
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200719static void journal_write_endio(struct bio *bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700720{
721 struct journal_write *w = bio->bi_private;
722
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200723 cache_set_err_on(bio->bi_status, w->c, "journal io error");
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700724 closure_put(&w->c->journal.io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700725}
726
Coly Lifc2d5982018-08-11 13:19:46 +0800727static void journal_write(struct closure *cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700728
729static void journal_write_done(struct closure *cl)
730{
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700731 struct journal *j = container_of(cl, struct journal, io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700732 struct journal_write *w = (j->cur == j->w)
733 ? &j->w[1]
734 : &j->w[0];
735
736 __closure_wake_up(&w->wait);
Guoju Fang0f843e62018-09-27 23:41:46 +0800737 continue_at_nobarrier(cl, journal_write, bch_journal_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700738}
739
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800740static void journal_write_unlock(struct closure *cl)
Bart Van Assche20d3a512018-03-18 17:36:32 -0700741 __releases(&c->journal.lock)
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800742{
743 struct cache_set *c = container_of(cl, struct cache_set, journal.io);
744
745 c->journal.io_in_flight = 0;
746 spin_unlock(&c->journal.lock);
747}
748
Kent Overstreetcafe5632013-03-23 16:11:31 -0700749static void journal_write_unlocked(struct closure *cl)
Kent Overstreetc19ed232013-03-26 13:49:02 -0700750 __releases(c->journal.lock)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700751{
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700752 struct cache_set *c = container_of(cl, struct cache_set, journal.io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700753 struct cache *ca;
754 struct journal_write *w = c->journal.cur;
755 struct bkey *k = &c->journal.key;
Coly Li6f10f7d2018-08-11 13:19:44 +0800756 unsigned int i, sectors = set_blocks(w->data, block_bytes(c)) *
Kent Overstreetee811282013-12-17 23:49:49 -0800757 c->sb.block_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700758
759 struct bio *bio;
760 struct bio_list list;
Coly Li1fae7cf2018-08-11 13:19:45 +0800761
Kent Overstreetcafe5632013-03-23 16:11:31 -0700762 bio_list_init(&list);
763
764 if (!w->need_write) {
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800765 closure_return_with_destructor(cl, journal_write_unlock);
Jens Axboe77b5a082015-03-06 08:37:46 -0700766 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700767 } else if (journal_full(&c->journal)) {
768 journal_reclaim(c);
769 spin_unlock(&c->journal.lock);
770
771 btree_flush_write(c);
Guoju Fang0f843e62018-09-27 23:41:46 +0800772 continue_at(cl, journal_write, bch_journal_wq);
Jens Axboe77b5a082015-03-06 08:37:46 -0700773 return;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700774 }
775
Kent Overstreetee811282013-12-17 23:49:49 -0800776 c->journal.blocks_free -= set_blocks(w->data, block_bytes(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700777
778 w->data->btree_level = c->root->level;
779
780 bkey_copy(&w->data->btree_root, &c->root->key);
781 bkey_copy(&w->data->uuid_bucket, &c->uuid_bucket);
782
783 for_each_cache(ca, c, i)
784 w->data->prio_bucket[ca->sb.nr_this_dev] = ca->prio_buckets[0];
785
Kent Overstreet81ab4192013-10-31 15:46:42 -0700786 w->data->magic = jset_magic(&c->sb);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700787 w->data->version = BCACHE_JSET_VERSION;
788 w->data->last_seq = last_seq(&c->journal);
789 w->data->csum = csum_set(w->data);
790
791 for (i = 0; i < KEY_PTRS(k); i++) {
792 ca = PTR_CACHE(c, k, i);
793 bio = &ca->journal.bio;
794
795 atomic_long_add(sectors, &ca->meta_sectors_written);
796
797 bio_reset(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700798 bio->bi_iter.bi_sector = PTR_OFFSET(k, i);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200799 bio_set_dev(bio, ca->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700800 bio->bi_iter.bi_size = sectors << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700801
802 bio->bi_end_io = journal_write_endio;
803 bio->bi_private = w;
Mike Christiead0d9e72016-06-05 14:32:05 -0500804 bio_set_op_attrs(bio, REQ_OP_WRITE,
Mike Christie28a8f0d2016-06-05 14:32:25 -0500805 REQ_SYNC|REQ_META|REQ_PREFLUSH|REQ_FUA);
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600806 bch_bio_map(bio, w->data);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700807
Guoju Fange78bd0d2018-12-13 22:53:57 +0800808 trace_bcache_journal_write(bio, w->data->keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700809 bio_list_add(&list, bio);
810
811 SET_PTR_OFFSET(k, i, PTR_OFFSET(k, i) + sectors);
812
813 ca->journal.seq[ca->journal.cur_idx] = w->data->seq;
814 }
815
Coly Li1bee2ad2019-04-25 00:48:33 +0800816 /* If KEY_PTRS(k) == 0, this jset gets lost in air */
817 BUG_ON(i == 0);
818
Kent Overstreetcafe5632013-03-23 16:11:31 -0700819 atomic_dec_bug(&fifo_back(&c->journal.pin));
820 bch_journal_next(&c->journal);
821 journal_reclaim(c);
822
823 spin_unlock(&c->journal.lock);
824
825 while ((bio = bio_list_pop(&list)))
Coly Li771f3932018-03-18 17:36:17 -0700826 closure_bio_submit(c, bio, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700827
828 continue_at(cl, journal_write_done, NULL);
829}
830
831static void journal_write(struct closure *cl)
832{
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700833 struct cache_set *c = container_of(cl, struct cache_set, journal.io);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700834
835 spin_lock(&c->journal.lock);
836 journal_write_unlocked(cl);
837}
838
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700839static void journal_try_write(struct cache_set *c)
Kent Overstreetc19ed232013-03-26 13:49:02 -0700840 __releases(c->journal.lock)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700841{
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700842 struct closure *cl = &c->journal.io;
843 struct journal_write *w = c->journal.cur;
844
845 w->need_write = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700846
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800847 if (!c->journal.io_in_flight) {
848 c->journal.io_in_flight = 1;
849 closure_call(cl, journal_write_unlocked, NULL, &c->cl);
850 } else {
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700851 spin_unlock(&c->journal.lock);
Kent Overstreetcb7a5832013-12-16 15:27:25 -0800852 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700853}
854
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700855static struct journal_write *journal_wait_for_write(struct cache_set *c,
Coly Li6f10f7d2018-08-11 13:19:44 +0800856 unsigned int nkeys)
Bart Van Assche20d3a512018-03-18 17:36:32 -0700857 __acquires(&c->journal.lock)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700858{
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700859 size_t sectors;
860 struct closure cl;
Kent Overstreet5775e212013-12-10 16:10:46 -0800861 bool wait = false;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700862
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700863 closure_init_stack(&cl);
864
865 spin_lock(&c->journal.lock);
866
867 while (1) {
868 struct journal_write *w = c->journal.cur;
869
870 sectors = __set_blocks(w->data, w->data->keys + nkeys,
Kent Overstreetee811282013-12-17 23:49:49 -0800871 block_bytes(c)) * c->sb.block_size;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700872
873 if (sectors <= min_t(size_t,
874 c->journal.blocks_free * c->sb.block_size,
875 PAGE_SECTORS << JSET_BITS))
876 return w;
877
Kent Overstreet5775e212013-12-10 16:10:46 -0800878 if (wait)
879 closure_wait(&c->journal.wait, &cl);
880
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700881 if (!journal_full(&c->journal)) {
Kent Overstreet5775e212013-12-10 16:10:46 -0800882 if (wait)
883 trace_bcache_journal_entry_full(c);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700884
885 /*
886 * XXX: If we were inserting so many keys that they
887 * won't fit in an _empty_ journal write, we'll
888 * deadlock. For now, handle this in
889 * bch_keylist_realloc() - but something to think about.
890 */
891 BUG_ON(!w->data->keys);
892
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700893 journal_try_write(c); /* unlocks */
894 } else {
Kent Overstreet5775e212013-12-10 16:10:46 -0800895 if (wait)
896 trace_bcache_journal_full(c);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700897
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700898 journal_reclaim(c);
899 spin_unlock(&c->journal.lock);
900
901 btree_flush_write(c);
902 }
903
904 closure_sync(&cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700905 spin_lock(&c->journal.lock);
Kent Overstreet5775e212013-12-10 16:10:46 -0800906 wait = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700907 }
908}
909
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700910static void journal_write_work(struct work_struct *work)
911{
912 struct cache_set *c = container_of(to_delayed_work(work),
913 struct cache_set,
914 journal.work);
915 spin_lock(&c->journal.lock);
Kent Overstreetdabb4432014-02-19 19:48:26 -0800916 if (c->journal.cur->dirty)
917 journal_try_write(c);
918 else
919 spin_unlock(&c->journal.lock);
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700920}
921
Kent Overstreetcafe5632013-03-23 16:11:31 -0700922/*
923 * Entry point to the journalling code - bio_insert() and btree_invalidate()
924 * pass bch_journal() a list of keys to be journalled, and then
925 * bch_journal() hands those same keys off to btree_insert_async()
926 */
927
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700928atomic_t *bch_journal(struct cache_set *c,
929 struct keylist *keys,
930 struct closure *parent)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700931{
Kent Overstreetcafe5632013-03-23 16:11:31 -0700932 struct journal_write *w;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700933 atomic_t *ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700934
Coly Li383ff212019-06-28 19:59:36 +0800935 /* No journaling if CACHE_SET_IO_DISABLE set already */
936 if (unlikely(test_bit(CACHE_SET_IO_DISABLE, &c->flags)))
937 return NULL;
938
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700939 if (!CACHE_SYNC(&c->sb))
940 return NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700941
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700942 w = journal_wait_for_write(c, bch_keylist_nkeys(keys));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700943
Kent Overstreetfafff812013-12-17 21:56:21 -0800944 memcpy(bset_bkey_last(w->data), keys->keys, bch_keylist_bytes(keys));
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700945 w->data->keys += bch_keylist_nkeys(keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700946
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700947 ret = &fifo_back(&c->journal.pin);
948 atomic_inc(ret);
Kent Overstreetc37511b2013-04-26 15:39:55 -0700949
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700950 if (parent) {
951 closure_wait(&w->wait, parent);
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700952 journal_try_write(c);
Kent Overstreetdabb4432014-02-19 19:48:26 -0800953 } else if (!w->dirty) {
954 w->dirty = true;
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700955 schedule_delayed_work(&c->journal.work,
956 msecs_to_jiffies(c->journal_delay_ms));
957 spin_unlock(&c->journal.lock);
958 } else {
959 spin_unlock(&c->journal.lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700960 }
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700961
962
963 return ret;
964}
965
966void bch_journal_meta(struct cache_set *c, struct closure *cl)
967{
968 struct keylist keys;
969 atomic_t *ref;
970
971 bch_keylist_init(&keys);
972
973 ref = bch_journal(c, &keys, cl);
974 if (ref)
975 atomic_dec_bug(ref);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700976}
977
978void bch_journal_free(struct cache_set *c)
979{
980 free_pages((unsigned long) c->journal.w[1].data, JSET_BITS);
981 free_pages((unsigned long) c->journal.w[0].data, JSET_BITS);
982 free_fifo(&c->journal.pin);
983}
984
985int bch_journal_alloc(struct cache_set *c)
986{
987 struct journal *j = &c->journal;
988
Kent Overstreetcafe5632013-03-23 16:11:31 -0700989 spin_lock_init(&j->lock);
Coly Li91be66e2019-06-28 19:59:59 +0800990 spin_lock_init(&j->flush_write_lock);
Kent Overstreet7857d5d42013-10-08 15:50:46 -0700991 INIT_DELAYED_WORK(&j->work, journal_write_work);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700992
993 c->journal_delay_ms = 100;
994
995 j->w[0].c = c;
996 j->w[1].c = c;
997
Coly Li249a5f62019-06-28 19:59:54 +0800998 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
Coly Li5fe48862020-07-25 20:00:16 +0800999 !(j->w[0].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)) ||
1000 !(j->w[1].data = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP, JSET_BITS)))
Kent Overstreetcafe5632013-03-23 16:11:31 -07001001 return -ENOMEM;
1002
1003 return 0;
1004}