xref: /linux/fs/bcachefs/journal_io.c (revision 4a4b30ea80d8cb5e8c4c62bb86201f4ea0d9b030)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
5 #include "btree_io.h"
6 #include "btree_update_interior.h"
7 #include "btree_write_buffer.h"
8 #include "buckets.h"
9 #include "checksum.h"
10 #include "disk_groups.h"
11 #include "error.h"
12 #include "journal.h"
13 #include "journal_io.h"
14 #include "journal_reclaim.h"
15 #include "journal_seq_blacklist.h"
16 #include "replicas.h"
17 #include "sb-clean.h"
18 #include "trace.h"
19 
20 #include <linux/ioprio.h>
21 #include <linux/string_choices.h>
22 
bch2_journal_pos_from_member_info_set(struct bch_fs * c)23 void bch2_journal_pos_from_member_info_set(struct bch_fs *c)
24 {
25 	lockdep_assert_held(&c->sb_lock);
26 
27 	for_each_member_device(c, ca) {
28 		struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
29 
30 		m->last_journal_bucket = cpu_to_le32(ca->journal.cur_idx);
31 		m->last_journal_bucket_offset = cpu_to_le32(ca->mi.bucket_size - ca->journal.sectors_free);
32 	}
33 }
34 
bch2_journal_pos_from_member_info_resume(struct bch_fs * c)35 void bch2_journal_pos_from_member_info_resume(struct bch_fs *c)
36 {
37 	mutex_lock(&c->sb_lock);
38 	for_each_member_device(c, ca) {
39 		struct bch_member m = bch2_sb_member_get(c->disk_sb.sb, ca->dev_idx);
40 
41 		unsigned idx = le32_to_cpu(m.last_journal_bucket);
42 		if (idx < ca->journal.nr)
43 			ca->journal.cur_idx = idx;
44 		unsigned offset = le32_to_cpu(m.last_journal_bucket_offset);
45 		if (offset <= ca->mi.bucket_size)
46 			ca->journal.sectors_free = ca->mi.bucket_size - offset;
47 	}
48 	mutex_unlock(&c->sb_lock);
49 }
50 
bch2_journal_ptrs_to_text(struct printbuf * out,struct bch_fs * c,struct journal_replay * j)51 void bch2_journal_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
52 			       struct journal_replay *j)
53 {
54 	darray_for_each(j->ptrs, i) {
55 		if (i != j->ptrs.data)
56 			prt_printf(out, " ");
57 		prt_printf(out, "%u:%u:%u (sector %llu)",
58 			   i->dev, i->bucket, i->bucket_offset, i->sector);
59 	}
60 }
61 
bch2_journal_replay_to_text(struct printbuf * out,struct bch_fs * c,struct journal_replay * j)62 static void bch2_journal_replay_to_text(struct printbuf *out, struct bch_fs *c,
63 					struct journal_replay *j)
64 {
65 	prt_printf(out, "seq %llu ", le64_to_cpu(j->j.seq));
66 
67 	bch2_journal_ptrs_to_text(out, c, j);
68 
69 	for_each_jset_entry_type(entry, &j->j, BCH_JSET_ENTRY_datetime) {
70 		struct jset_entry_datetime *datetime =
71 			container_of(entry, struct jset_entry_datetime, entry);
72 		bch2_prt_datetime(out, le64_to_cpu(datetime->seconds));
73 		break;
74 	}
75 }
76 
journal_nonce(const struct jset * jset)77 static struct nonce journal_nonce(const struct jset *jset)
78 {
79 	return (struct nonce) {{
80 		[0] = 0,
81 		[1] = ((__le32 *) &jset->seq)[0],
82 		[2] = ((__le32 *) &jset->seq)[1],
83 		[3] = BCH_NONCE_JOURNAL,
84 	}};
85 }
86 
jset_csum_good(struct bch_fs * c,struct jset * j,struct bch_csum * csum)87 static bool jset_csum_good(struct bch_fs *c, struct jset *j, struct bch_csum *csum)
88 {
89 	if (!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(j))) {
90 		*csum = (struct bch_csum) {};
91 		return false;
92 	}
93 
94 	*csum = csum_vstruct(c, JSET_CSUM_TYPE(j), journal_nonce(j), j);
95 	return !bch2_crc_cmp(j->csum, *csum);
96 }
97 
journal_entry_radix_idx(struct bch_fs * c,u64 seq)98 static inline u32 journal_entry_radix_idx(struct bch_fs *c, u64 seq)
99 {
100 	return (seq - c->journal_entries_base_seq) & (~0U >> 1);
101 }
102 
__journal_replay_free(struct bch_fs * c,struct journal_replay * i)103 static void __journal_replay_free(struct bch_fs *c,
104 				  struct journal_replay *i)
105 {
106 	struct journal_replay **p =
107 		genradix_ptr(&c->journal_entries,
108 			     journal_entry_radix_idx(c, le64_to_cpu(i->j.seq)));
109 
110 	BUG_ON(*p != i);
111 	*p = NULL;
112 	kvfree(i);
113 }
114 
journal_replay_free(struct bch_fs * c,struct journal_replay * i,bool blacklisted)115 static void journal_replay_free(struct bch_fs *c, struct journal_replay *i, bool blacklisted)
116 {
117 	if (blacklisted)
118 		i->ignore_blacklisted = true;
119 	else
120 		i->ignore_not_dirty = true;
121 
122 	if (!c->opts.read_entire_journal)
123 		__journal_replay_free(c, i);
124 }
125 
126 struct journal_list {
127 	struct closure		cl;
128 	u64			last_seq;
129 	struct mutex		lock;
130 	int			ret;
131 };
132 
133 #define JOURNAL_ENTRY_ADD_OK		0
134 #define JOURNAL_ENTRY_ADD_OUT_OF_RANGE	5
135 
136 /*
137  * Given a journal entry we just read, add it to the list of journal entries to
138  * be replayed:
139  */
journal_entry_add(struct bch_fs * c,struct bch_dev * ca,struct journal_ptr entry_ptr,struct journal_list * jlist,struct jset * j)140 static int journal_entry_add(struct bch_fs *c, struct bch_dev *ca,
141 			     struct journal_ptr entry_ptr,
142 			     struct journal_list *jlist, struct jset *j)
143 {
144 	struct genradix_iter iter;
145 	struct journal_replay **_i, *i, *dup;
146 	size_t bytes = vstruct_bytes(j);
147 	u64 last_seq = !JSET_NO_FLUSH(j) ? le64_to_cpu(j->last_seq) : 0;
148 	struct printbuf buf = PRINTBUF;
149 	int ret = JOURNAL_ENTRY_ADD_OK;
150 
151 	if (!c->journal.oldest_seq_found_ondisk ||
152 	    le64_to_cpu(j->seq) < c->journal.oldest_seq_found_ondisk)
153 		c->journal.oldest_seq_found_ondisk = le64_to_cpu(j->seq);
154 
155 	/* Is this entry older than the range we need? */
156 	if (!c->opts.read_entire_journal &&
157 	    le64_to_cpu(j->seq) < jlist->last_seq)
158 		return JOURNAL_ENTRY_ADD_OUT_OF_RANGE;
159 
160 	/*
161 	 * genradixes are indexed by a ulong, not a u64, so we can't index them
162 	 * by sequence number directly: Assume instead that they will all fall
163 	 * within the range of +-2billion of the filrst one we find.
164 	 */
165 	if (!c->journal_entries_base_seq)
166 		c->journal_entries_base_seq = max_t(s64, 1, le64_to_cpu(j->seq) - S32_MAX);
167 
168 	/* Drop entries we don't need anymore */
169 	if (last_seq > jlist->last_seq && !c->opts.read_entire_journal) {
170 		genradix_for_each_from(&c->journal_entries, iter, _i,
171 				       journal_entry_radix_idx(c, jlist->last_seq)) {
172 			i = *_i;
173 
174 			if (journal_replay_ignore(i))
175 				continue;
176 
177 			if (le64_to_cpu(i->j.seq) >= last_seq)
178 				break;
179 
180 			journal_replay_free(c, i, false);
181 		}
182 	}
183 
184 	jlist->last_seq = max(jlist->last_seq, last_seq);
185 
186 	_i = genradix_ptr_alloc(&c->journal_entries,
187 				journal_entry_radix_idx(c, le64_to_cpu(j->seq)),
188 				GFP_KERNEL);
189 	if (!_i)
190 		return -BCH_ERR_ENOMEM_journal_entry_add;
191 
192 	/*
193 	 * Duplicate journal entries? If so we want the one that didn't have a
194 	 * checksum error:
195 	 */
196 	dup = *_i;
197 	if (dup) {
198 		bool identical = bytes == vstruct_bytes(&dup->j) &&
199 			!memcmp(j, &dup->j, bytes);
200 		bool not_identical = !identical &&
201 			entry_ptr.csum_good &&
202 			dup->csum_good;
203 
204 		bool same_device = false;
205 		darray_for_each(dup->ptrs, ptr)
206 			if (ptr->dev == ca->dev_idx)
207 				same_device = true;
208 
209 		ret = darray_push(&dup->ptrs, entry_ptr);
210 		if (ret)
211 			goto out;
212 
213 		bch2_journal_replay_to_text(&buf, c, dup);
214 
215 		fsck_err_on(same_device,
216 			    c, journal_entry_dup_same_device,
217 			    "duplicate journal entry on same device\n  %s",
218 			    buf.buf);
219 
220 		fsck_err_on(not_identical,
221 			    c, journal_entry_replicas_data_mismatch,
222 			    "found duplicate but non identical journal entries\n  %s",
223 			    buf.buf);
224 
225 		if (entry_ptr.csum_good && !identical)
226 			goto replace;
227 
228 		goto out;
229 	}
230 replace:
231 	i = kvmalloc(offsetof(struct journal_replay, j) + bytes, GFP_KERNEL);
232 	if (!i)
233 		return -BCH_ERR_ENOMEM_journal_entry_add;
234 
235 	darray_init(&i->ptrs);
236 	i->csum_good		= entry_ptr.csum_good;
237 	i->ignore_blacklisted	= false;
238 	i->ignore_not_dirty	= false;
239 	unsafe_memcpy(&i->j, j, bytes, "embedded variable length struct");
240 
241 	if (dup) {
242 		/* The first ptr should represent the jset we kept: */
243 		darray_for_each(dup->ptrs, ptr)
244 			darray_push(&i->ptrs, *ptr);
245 		__journal_replay_free(c, dup);
246 	} else {
247 		darray_push(&i->ptrs, entry_ptr);
248 	}
249 
250 	*_i = i;
251 out:
252 fsck_err:
253 	printbuf_exit(&buf);
254 	return ret;
255 }
256 
257 /* this fills in a range with empty jset_entries: */
journal_entry_null_range(void * start,void * end)258 static void journal_entry_null_range(void *start, void *end)
259 {
260 	struct jset_entry *entry;
261 
262 	for (entry = start; entry != end; entry = vstruct_next(entry))
263 		memset(entry, 0, sizeof(*entry));
264 }
265 
266 #define JOURNAL_ENTRY_REREAD	5
267 #define JOURNAL_ENTRY_NONE	6
268 #define JOURNAL_ENTRY_BAD	7
269 
journal_entry_err_msg(struct printbuf * out,u32 version,struct jset * jset,struct jset_entry * entry)270 static void journal_entry_err_msg(struct printbuf *out,
271 				  u32 version,
272 				  struct jset *jset,
273 				  struct jset_entry *entry)
274 {
275 	prt_str(out, "invalid journal entry, version=");
276 	bch2_version_to_text(out, version);
277 
278 	if (entry) {
279 		prt_str(out, " type=");
280 		bch2_prt_jset_entry_type(out, entry->type);
281 	}
282 
283 	if (!jset) {
284 		prt_printf(out, " in superblock");
285 	} else {
286 
287 		prt_printf(out, " seq=%llu", le64_to_cpu(jset->seq));
288 
289 		if (entry)
290 			prt_printf(out, " offset=%zi/%u",
291 				   (u64 *) entry - jset->_data,
292 				   le32_to_cpu(jset->u64s));
293 	}
294 
295 	prt_str(out, ": ");
296 }
297 
298 #define journal_entry_err(c, version, jset, entry, _err, msg, ...)	\
299 ({									\
300 	struct printbuf _buf = PRINTBUF;				\
301 									\
302 	journal_entry_err_msg(&_buf, version, jset, entry);		\
303 	prt_printf(&_buf, msg, ##__VA_ARGS__);				\
304 									\
305 	switch (from.flags & BCH_VALIDATE_write) {			\
306 	case READ:							\
307 		mustfix_fsck_err(c, _err, "%s", _buf.buf);		\
308 		break;							\
309 	case WRITE:							\
310 		bch2_sb_error_count(c, BCH_FSCK_ERR_##_err);		\
311 		bch_err(c, "corrupt metadata before write: %s\n", _buf.buf);\
312 		if (bch2_fs_inconsistent(c)) {				\
313 			ret = -BCH_ERR_fsck_errors_not_fixed;		\
314 			goto fsck_err;					\
315 		}							\
316 		break;							\
317 	}								\
318 									\
319 	printbuf_exit(&_buf);						\
320 	true;								\
321 })
322 
323 #define journal_entry_err_on(cond, ...)					\
324 	((cond) ? journal_entry_err(__VA_ARGS__) : false)
325 
326 #define FSCK_DELETED_KEY	5
327 
journal_validate_key(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,struct bkey_i * k,struct bkey_validate_context from,unsigned version,int big_endian)328 static int journal_validate_key(struct bch_fs *c,
329 				struct jset *jset,
330 				struct jset_entry *entry,
331 				struct bkey_i *k,
332 				struct bkey_validate_context from,
333 				unsigned version, int big_endian)
334 {
335 	enum bch_validate_flags flags = from.flags;
336 	int write = flags & BCH_VALIDATE_write;
337 	void *next = vstruct_next(entry);
338 	int ret = 0;
339 
340 	if (journal_entry_err_on(!k->k.u64s,
341 				 c, version, jset, entry,
342 				 journal_entry_bkey_u64s_0,
343 				 "k->u64s 0")) {
344 		entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
345 		journal_entry_null_range(vstruct_next(entry), next);
346 		return FSCK_DELETED_KEY;
347 	}
348 
349 	if (journal_entry_err_on((void *) bkey_next(k) >
350 				 (void *) vstruct_next(entry),
351 				 c, version, jset, entry,
352 				 journal_entry_bkey_past_end,
353 				 "extends past end of journal entry")) {
354 		entry->u64s = cpu_to_le16((u64 *) k - entry->_data);
355 		journal_entry_null_range(vstruct_next(entry), next);
356 		return FSCK_DELETED_KEY;
357 	}
358 
359 	if (journal_entry_err_on(k->k.format != KEY_FORMAT_CURRENT,
360 				 c, version, jset, entry,
361 				 journal_entry_bkey_bad_format,
362 				 "bad format %u", k->k.format)) {
363 		le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
364 		memmove(k, bkey_next(k), next - (void *) bkey_next(k));
365 		journal_entry_null_range(vstruct_next(entry), next);
366 		return FSCK_DELETED_KEY;
367 	}
368 
369 	if (!write)
370 		bch2_bkey_compat(from.level, from.btree, version, big_endian,
371 				 write, NULL, bkey_to_packed(k));
372 
373 	ret = bch2_bkey_validate(c, bkey_i_to_s_c(k), from);
374 	if (ret == -BCH_ERR_fsck_delete_bkey) {
375 		le16_add_cpu(&entry->u64s, -((u16) k->k.u64s));
376 		memmove(k, bkey_next(k), next - (void *) bkey_next(k));
377 		journal_entry_null_range(vstruct_next(entry), next);
378 		return FSCK_DELETED_KEY;
379 	}
380 	if (ret)
381 		goto fsck_err;
382 
383 	if (write)
384 		bch2_bkey_compat(from.level, from.btree, version, big_endian,
385 				 write, NULL, bkey_to_packed(k));
386 fsck_err:
387 	return ret;
388 }
389 
journal_entry_btree_keys_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)390 static int journal_entry_btree_keys_validate(struct bch_fs *c,
391 				struct jset *jset,
392 				struct jset_entry *entry,
393 				unsigned version, int big_endian,
394 				struct bkey_validate_context from)
395 {
396 	struct bkey_i *k = entry->start;
397 
398 	from.level	= entry->level;
399 	from.btree	= entry->btree_id;
400 
401 	while (k != vstruct_last(entry)) {
402 		int ret = journal_validate_key(c, jset, entry, k, from, version, big_endian);
403 		if (ret == FSCK_DELETED_KEY)
404 			continue;
405 		else if (ret)
406 			return ret;
407 
408 		k = bkey_next(k);
409 	}
410 
411 	return 0;
412 }
413 
journal_entry_btree_keys_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)414 static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs *c,
415 					     struct jset_entry *entry)
416 {
417 	bool first = true;
418 
419 	jset_entry_for_each_key(entry, k) {
420 		if (!first) {
421 			prt_newline(out);
422 			bch2_prt_jset_entry_type(out, entry->type);
423 			prt_str(out, ": ");
424 		}
425 		bch2_btree_id_level_to_text(out, entry->btree_id, entry->level);
426 		prt_char(out, ' ');
427 		bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
428 		first = false;
429 	}
430 }
431 
journal_entry_btree_root_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)432 static int journal_entry_btree_root_validate(struct bch_fs *c,
433 				struct jset *jset,
434 				struct jset_entry *entry,
435 				unsigned version, int big_endian,
436 				struct bkey_validate_context from)
437 {
438 	struct bkey_i *k = entry->start;
439 	int ret = 0;
440 
441 	from.root	= true;
442 	from.level	= entry->level + 1;
443 	from.btree	= entry->btree_id;
444 
445 	if (journal_entry_err_on(!entry->u64s ||
446 				 le16_to_cpu(entry->u64s) != k->k.u64s,
447 				 c, version, jset, entry,
448 				 journal_entry_btree_root_bad_size,
449 				 "invalid btree root journal entry: wrong number of keys")) {
450 		void *next = vstruct_next(entry);
451 		/*
452 		 * we don't want to null out this jset_entry,
453 		 * just the contents, so that later we can tell
454 		 * we were _supposed_ to have a btree root
455 		 */
456 		entry->u64s = 0;
457 		journal_entry_null_range(vstruct_next(entry), next);
458 		return 0;
459 	}
460 
461 	ret = journal_validate_key(c, jset, entry, k, from, version, big_endian);
462 	if (ret == FSCK_DELETED_KEY)
463 		ret = 0;
464 fsck_err:
465 	return ret;
466 }
467 
journal_entry_btree_root_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)468 static void journal_entry_btree_root_to_text(struct printbuf *out, struct bch_fs *c,
469 					     struct jset_entry *entry)
470 {
471 	journal_entry_btree_keys_to_text(out, c, entry);
472 }
473 
journal_entry_prio_ptrs_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)474 static int journal_entry_prio_ptrs_validate(struct bch_fs *c,
475 				struct jset *jset,
476 				struct jset_entry *entry,
477 				unsigned version, int big_endian,
478 				struct bkey_validate_context from)
479 {
480 	/* obsolete, don't care: */
481 	return 0;
482 }
483 
journal_entry_prio_ptrs_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)484 static void journal_entry_prio_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
485 					    struct jset_entry *entry)
486 {
487 }
488 
journal_entry_blacklist_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)489 static int journal_entry_blacklist_validate(struct bch_fs *c,
490 				struct jset *jset,
491 				struct jset_entry *entry,
492 				unsigned version, int big_endian,
493 				struct bkey_validate_context from)
494 {
495 	int ret = 0;
496 
497 	if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 1,
498 				 c, version, jset, entry,
499 				 journal_entry_blacklist_bad_size,
500 		"invalid journal seq blacklist entry: bad size")) {
501 		journal_entry_null_range(entry, vstruct_next(entry));
502 	}
503 fsck_err:
504 	return ret;
505 }
506 
journal_entry_blacklist_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)507 static void journal_entry_blacklist_to_text(struct printbuf *out, struct bch_fs *c,
508 					    struct jset_entry *entry)
509 {
510 	struct jset_entry_blacklist *bl =
511 		container_of(entry, struct jset_entry_blacklist, entry);
512 
513 	prt_printf(out, "seq=%llu", le64_to_cpu(bl->seq));
514 }
515 
journal_entry_blacklist_v2_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)516 static int journal_entry_blacklist_v2_validate(struct bch_fs *c,
517 				struct jset *jset,
518 				struct jset_entry *entry,
519 				unsigned version, int big_endian,
520 				struct bkey_validate_context from)
521 {
522 	struct jset_entry_blacklist_v2 *bl_entry;
523 	int ret = 0;
524 
525 	if (journal_entry_err_on(le16_to_cpu(entry->u64s) != 2,
526 				 c, version, jset, entry,
527 				 journal_entry_blacklist_v2_bad_size,
528 		"invalid journal seq blacklist entry: bad size")) {
529 		journal_entry_null_range(entry, vstruct_next(entry));
530 		goto out;
531 	}
532 
533 	bl_entry = container_of(entry, struct jset_entry_blacklist_v2, entry);
534 
535 	if (journal_entry_err_on(le64_to_cpu(bl_entry->start) >
536 				 le64_to_cpu(bl_entry->end),
537 				 c, version, jset, entry,
538 				 journal_entry_blacklist_v2_start_past_end,
539 		"invalid journal seq blacklist entry: start > end")) {
540 		journal_entry_null_range(entry, vstruct_next(entry));
541 	}
542 out:
543 fsck_err:
544 	return ret;
545 }
546 
journal_entry_blacklist_v2_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)547 static void journal_entry_blacklist_v2_to_text(struct printbuf *out, struct bch_fs *c,
548 					       struct jset_entry *entry)
549 {
550 	struct jset_entry_blacklist_v2 *bl =
551 		container_of(entry, struct jset_entry_blacklist_v2, entry);
552 
553 	prt_printf(out, "start=%llu end=%llu",
554 	       le64_to_cpu(bl->start),
555 	       le64_to_cpu(bl->end));
556 }
557 
journal_entry_usage_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)558 static int journal_entry_usage_validate(struct bch_fs *c,
559 				struct jset *jset,
560 				struct jset_entry *entry,
561 				unsigned version, int big_endian,
562 				struct bkey_validate_context from)
563 {
564 	struct jset_entry_usage *u =
565 		container_of(entry, struct jset_entry_usage, entry);
566 	unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
567 	int ret = 0;
568 
569 	if (journal_entry_err_on(bytes < sizeof(*u),
570 				 c, version, jset, entry,
571 				 journal_entry_usage_bad_size,
572 				 "invalid journal entry usage: bad size")) {
573 		journal_entry_null_range(entry, vstruct_next(entry));
574 		return ret;
575 	}
576 
577 fsck_err:
578 	return ret;
579 }
580 
journal_entry_usage_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)581 static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
582 					struct jset_entry *entry)
583 {
584 	struct jset_entry_usage *u =
585 		container_of(entry, struct jset_entry_usage, entry);
586 
587 	prt_str(out, "type=");
588 	bch2_prt_fs_usage_type(out, u->entry.btree_id);
589 	prt_printf(out, " v=%llu", le64_to_cpu(u->v));
590 }
591 
journal_entry_data_usage_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)592 static int journal_entry_data_usage_validate(struct bch_fs *c,
593 				struct jset *jset,
594 				struct jset_entry *entry,
595 				unsigned version, int big_endian,
596 				struct bkey_validate_context from)
597 {
598 	struct jset_entry_data_usage *u =
599 		container_of(entry, struct jset_entry_data_usage, entry);
600 	unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
601 	struct printbuf err = PRINTBUF;
602 	int ret = 0;
603 
604 	if (journal_entry_err_on(bytes < sizeof(*u) ||
605 				 bytes < sizeof(*u) + u->r.nr_devs,
606 				 c, version, jset, entry,
607 				 journal_entry_data_usage_bad_size,
608 				 "invalid journal entry usage: bad size")) {
609 		journal_entry_null_range(entry, vstruct_next(entry));
610 		goto out;
611 	}
612 
613 	if (journal_entry_err_on(bch2_replicas_entry_validate(&u->r, c, &err),
614 				 c, version, jset, entry,
615 				 journal_entry_data_usage_bad_size,
616 				 "invalid journal entry usage: %s", err.buf)) {
617 		journal_entry_null_range(entry, vstruct_next(entry));
618 		goto out;
619 	}
620 out:
621 fsck_err:
622 	printbuf_exit(&err);
623 	return ret;
624 }
625 
journal_entry_data_usage_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)626 static void journal_entry_data_usage_to_text(struct printbuf *out, struct bch_fs *c,
627 					     struct jset_entry *entry)
628 {
629 	struct jset_entry_data_usage *u =
630 		container_of(entry, struct jset_entry_data_usage, entry);
631 
632 	bch2_replicas_entry_to_text(out, &u->r);
633 	prt_printf(out, "=%llu", le64_to_cpu(u->v));
634 }
635 
journal_entry_clock_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)636 static int journal_entry_clock_validate(struct bch_fs *c,
637 				struct jset *jset,
638 				struct jset_entry *entry,
639 				unsigned version, int big_endian,
640 				struct bkey_validate_context from)
641 {
642 	struct jset_entry_clock *clock =
643 		container_of(entry, struct jset_entry_clock, entry);
644 	unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
645 	int ret = 0;
646 
647 	if (journal_entry_err_on(bytes != sizeof(*clock),
648 				 c, version, jset, entry,
649 				 journal_entry_clock_bad_size,
650 				 "bad size")) {
651 		journal_entry_null_range(entry, vstruct_next(entry));
652 		return ret;
653 	}
654 
655 	if (journal_entry_err_on(clock->rw > 1,
656 				 c, version, jset, entry,
657 				 journal_entry_clock_bad_rw,
658 				 "bad rw")) {
659 		journal_entry_null_range(entry, vstruct_next(entry));
660 		return ret;
661 	}
662 
663 fsck_err:
664 	return ret;
665 }
666 
journal_entry_clock_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)667 static void journal_entry_clock_to_text(struct printbuf *out, struct bch_fs *c,
668 					struct jset_entry *entry)
669 {
670 	struct jset_entry_clock *clock =
671 		container_of(entry, struct jset_entry_clock, entry);
672 
673 	prt_printf(out, "%s=%llu", str_write_read(clock->rw), le64_to_cpu(clock->time));
674 }
675 
journal_entry_dev_usage_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)676 static int journal_entry_dev_usage_validate(struct bch_fs *c,
677 				struct jset *jset,
678 				struct jset_entry *entry,
679 				unsigned version, int big_endian,
680 				struct bkey_validate_context from)
681 {
682 	struct jset_entry_dev_usage *u =
683 		container_of(entry, struct jset_entry_dev_usage, entry);
684 	unsigned bytes = jset_u64s(le16_to_cpu(entry->u64s)) * sizeof(u64);
685 	unsigned expected = sizeof(*u);
686 	int ret = 0;
687 
688 	if (journal_entry_err_on(bytes < expected,
689 				 c, version, jset, entry,
690 				 journal_entry_dev_usage_bad_size,
691 				 "bad size (%u < %u)",
692 				 bytes, expected)) {
693 		journal_entry_null_range(entry, vstruct_next(entry));
694 		return ret;
695 	}
696 
697 	if (journal_entry_err_on(u->pad,
698 				 c, version, jset, entry,
699 				 journal_entry_dev_usage_bad_pad,
700 				 "bad pad")) {
701 		journal_entry_null_range(entry, vstruct_next(entry));
702 		return ret;
703 	}
704 
705 fsck_err:
706 	return ret;
707 }
708 
journal_entry_dev_usage_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)709 static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs *c,
710 					    struct jset_entry *entry)
711 {
712 	struct jset_entry_dev_usage *u =
713 		container_of(entry, struct jset_entry_dev_usage, entry);
714 	unsigned i, nr_types = jset_entry_dev_usage_nr_types(u);
715 
716 	if (vstruct_bytes(entry) < sizeof(*u))
717 		return;
718 
719 	prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
720 
721 	printbuf_indent_add(out, 2);
722 	for (i = 0; i < nr_types; i++) {
723 		prt_newline(out);
724 		bch2_prt_data_type(out, i);
725 		prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
726 		       le64_to_cpu(u->d[i].buckets),
727 		       le64_to_cpu(u->d[i].sectors),
728 		       le64_to_cpu(u->d[i].fragmented));
729 	}
730 	printbuf_indent_sub(out, 2);
731 }
732 
journal_entry_log_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)733 static int journal_entry_log_validate(struct bch_fs *c,
734 				struct jset *jset,
735 				struct jset_entry *entry,
736 				unsigned version, int big_endian,
737 				struct bkey_validate_context from)
738 {
739 	return 0;
740 }
741 
journal_entry_log_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)742 static void journal_entry_log_to_text(struct printbuf *out, struct bch_fs *c,
743 				      struct jset_entry *entry)
744 {
745 	struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
746 
747 	prt_printf(out, "%.*s", jset_entry_log_msg_bytes(l), l->d);
748 }
749 
journal_entry_overwrite_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)750 static int journal_entry_overwrite_validate(struct bch_fs *c,
751 				struct jset *jset,
752 				struct jset_entry *entry,
753 				unsigned version, int big_endian,
754 				struct bkey_validate_context from)
755 {
756 	from.flags = 0;
757 	return journal_entry_btree_keys_validate(c, jset, entry,
758 				version, big_endian, from);
759 }
760 
journal_entry_overwrite_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)761 static void journal_entry_overwrite_to_text(struct printbuf *out, struct bch_fs *c,
762 					    struct jset_entry *entry)
763 {
764 	journal_entry_btree_keys_to_text(out, c, entry);
765 }
766 
journal_entry_write_buffer_keys_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)767 static int journal_entry_write_buffer_keys_validate(struct bch_fs *c,
768 				struct jset *jset,
769 				struct jset_entry *entry,
770 				unsigned version, int big_endian,
771 				struct bkey_validate_context from)
772 {
773 	return journal_entry_btree_keys_validate(c, jset, entry,
774 				version, big_endian, from);
775 }
776 
journal_entry_write_buffer_keys_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)777 static void journal_entry_write_buffer_keys_to_text(struct printbuf *out, struct bch_fs *c,
778 					    struct jset_entry *entry)
779 {
780 	journal_entry_btree_keys_to_text(out, c, entry);
781 }
782 
journal_entry_datetime_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)783 static int journal_entry_datetime_validate(struct bch_fs *c,
784 				struct jset *jset,
785 				struct jset_entry *entry,
786 				unsigned version, int big_endian,
787 				struct bkey_validate_context from)
788 {
789 	unsigned bytes = vstruct_bytes(entry);
790 	unsigned expected = 16;
791 	int ret = 0;
792 
793 	if (journal_entry_err_on(vstruct_bytes(entry) < expected,
794 				 c, version, jset, entry,
795 				 journal_entry_dev_usage_bad_size,
796 				 "bad size (%u < %u)",
797 				 bytes, expected)) {
798 		journal_entry_null_range(entry, vstruct_next(entry));
799 		return ret;
800 	}
801 fsck_err:
802 	return ret;
803 }
804 
journal_entry_datetime_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)805 static void journal_entry_datetime_to_text(struct printbuf *out, struct bch_fs *c,
806 					    struct jset_entry *entry)
807 {
808 	struct jset_entry_datetime *datetime =
809 		container_of(entry, struct jset_entry_datetime, entry);
810 
811 	bch2_prt_datetime(out, le64_to_cpu(datetime->seconds));
812 }
813 
814 struct jset_entry_ops {
815 	int (*validate)(struct bch_fs *, struct jset *,
816 			struct jset_entry *, unsigned, int,
817 			struct bkey_validate_context);
818 	void (*to_text)(struct printbuf *, struct bch_fs *, struct jset_entry *);
819 };
820 
821 static const struct jset_entry_ops bch2_jset_entry_ops[] = {
822 #define x(f, nr)						\
823 	[BCH_JSET_ENTRY_##f]	= (struct jset_entry_ops) {	\
824 		.validate	= journal_entry_##f##_validate,	\
825 		.to_text	= journal_entry_##f##_to_text,	\
826 	},
827 	BCH_JSET_ENTRY_TYPES()
828 #undef x
829 };
830 
bch2_journal_entry_validate(struct bch_fs * c,struct jset * jset,struct jset_entry * entry,unsigned version,int big_endian,struct bkey_validate_context from)831 int bch2_journal_entry_validate(struct bch_fs *c,
832 				struct jset *jset,
833 				struct jset_entry *entry,
834 				unsigned version, int big_endian,
835 				struct bkey_validate_context from)
836 {
837 	return entry->type < BCH_JSET_ENTRY_NR
838 		? bch2_jset_entry_ops[entry->type].validate(c, jset, entry,
839 				version, big_endian, from)
840 		: 0;
841 }
842 
bch2_journal_entry_to_text(struct printbuf * out,struct bch_fs * c,struct jset_entry * entry)843 void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
844 				struct jset_entry *entry)
845 {
846 	bch2_prt_jset_entry_type(out, entry->type);
847 
848 	if (entry->type < BCH_JSET_ENTRY_NR) {
849 		prt_str(out, ": ");
850 		bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
851 	}
852 }
853 
jset_validate_entries(struct bch_fs * c,struct jset * jset,enum bch_validate_flags flags)854 static int jset_validate_entries(struct bch_fs *c, struct jset *jset,
855 				 enum bch_validate_flags flags)
856 {
857 	struct bkey_validate_context from = {
858 		.flags		= flags,
859 		.from		= BKEY_VALIDATE_journal,
860 		.journal_seq	= le64_to_cpu(jset->seq),
861 	};
862 
863 	unsigned version = le32_to_cpu(jset->version);
864 	int ret = 0;
865 
866 	vstruct_for_each(jset, entry) {
867 		from.journal_offset = (u64 *) entry - jset->_data;
868 
869 		if (journal_entry_err_on(vstruct_next(entry) > vstruct_last(jset),
870 				c, version, jset, entry,
871 				journal_entry_past_jset_end,
872 				"journal entry extends past end of jset")) {
873 			jset->u64s = cpu_to_le32((u64 *) entry - jset->_data);
874 			break;
875 		}
876 
877 		ret = bch2_journal_entry_validate(c, jset, entry, version,
878 						  JSET_BIG_ENDIAN(jset), from);
879 		if (ret)
880 			break;
881 	}
882 fsck_err:
883 	return ret;
884 }
885 
jset_validate(struct bch_fs * c,struct bch_dev * ca,struct jset * jset,u64 sector,enum bch_validate_flags flags)886 static int jset_validate(struct bch_fs *c,
887 			 struct bch_dev *ca,
888 			 struct jset *jset, u64 sector,
889 			 enum bch_validate_flags flags)
890 {
891 	struct bkey_validate_context from = {
892 		.flags		= flags,
893 		.from		= BKEY_VALIDATE_journal,
894 		.journal_seq	= le64_to_cpu(jset->seq),
895 	};
896 	int ret = 0;
897 
898 	if (le64_to_cpu(jset->magic) != jset_magic(c))
899 		return JOURNAL_ENTRY_NONE;
900 
901 	unsigned version = le32_to_cpu(jset->version);
902 	if (journal_entry_err_on(!bch2_version_compatible(version),
903 			c, version, jset, NULL,
904 			jset_unsupported_version,
905 			"%s sector %llu seq %llu: incompatible journal entry version %u.%u",
906 			ca ? ca->name : c->name,
907 			sector, le64_to_cpu(jset->seq),
908 			BCH_VERSION_MAJOR(version),
909 			BCH_VERSION_MINOR(version))) {
910 		/* don't try to continue: */
911 		return -EINVAL;
912 	}
913 
914 	if (journal_entry_err_on(!bch2_checksum_type_valid(c, JSET_CSUM_TYPE(jset)),
915 			c, version, jset, NULL,
916 			jset_unknown_csum,
917 			"%s sector %llu seq %llu: journal entry with unknown csum type %llu",
918 			ca ? ca->name : c->name,
919 			sector, le64_to_cpu(jset->seq),
920 			JSET_CSUM_TYPE(jset)))
921 		ret = JOURNAL_ENTRY_BAD;
922 
923 	/* last_seq is ignored when JSET_NO_FLUSH is true */
924 	if (journal_entry_err_on(!JSET_NO_FLUSH(jset) &&
925 				 le64_to_cpu(jset->last_seq) > le64_to_cpu(jset->seq),
926 				 c, version, jset, NULL,
927 				 jset_last_seq_newer_than_seq,
928 				 "invalid journal entry: last_seq > seq (%llu > %llu)",
929 				 le64_to_cpu(jset->last_seq),
930 				 le64_to_cpu(jset->seq))) {
931 		jset->last_seq = jset->seq;
932 		return JOURNAL_ENTRY_BAD;
933 	}
934 
935 	ret = jset_validate_entries(c, jset, flags);
936 fsck_err:
937 	return ret;
938 }
939 
jset_validate_early(struct bch_fs * c,struct bch_dev * ca,struct jset * jset,u64 sector,unsigned bucket_sectors_left,unsigned sectors_read)940 static int jset_validate_early(struct bch_fs *c,
941 			 struct bch_dev *ca,
942 			 struct jset *jset, u64 sector,
943 			 unsigned bucket_sectors_left,
944 			 unsigned sectors_read)
945 {
946 	struct bkey_validate_context from = {
947 		.from		= BKEY_VALIDATE_journal,
948 		.journal_seq	= le64_to_cpu(jset->seq),
949 	};
950 	int ret = 0;
951 
952 	if (le64_to_cpu(jset->magic) != jset_magic(c))
953 		return JOURNAL_ENTRY_NONE;
954 
955 	unsigned version = le32_to_cpu(jset->version);
956 	if (journal_entry_err_on(!bch2_version_compatible(version),
957 			c, version, jset, NULL,
958 			jset_unsupported_version,
959 			"%s sector %llu seq %llu: unknown journal entry version %u.%u",
960 			ca ? ca->name : c->name,
961 			sector, le64_to_cpu(jset->seq),
962 			BCH_VERSION_MAJOR(version),
963 			BCH_VERSION_MINOR(version))) {
964 		/* don't try to continue: */
965 		return -EINVAL;
966 	}
967 
968 	size_t bytes = vstruct_bytes(jset);
969 	if (bytes > (sectors_read << 9) &&
970 	    sectors_read < bucket_sectors_left)
971 		return JOURNAL_ENTRY_REREAD;
972 
973 	if (journal_entry_err_on(bytes > bucket_sectors_left << 9,
974 			c, version, jset, NULL,
975 			jset_past_bucket_end,
976 			"%s sector %llu seq %llu: journal entry too big (%zu bytes)",
977 			ca ? ca->name : c->name,
978 			sector, le64_to_cpu(jset->seq), bytes))
979 		le32_add_cpu(&jset->u64s,
980 			     -((bytes - (bucket_sectors_left << 9)) / 8));
981 fsck_err:
982 	return ret;
983 }
984 
985 struct journal_read_buf {
986 	void		*data;
987 	size_t		size;
988 };
989 
journal_read_buf_realloc(struct journal_read_buf * b,size_t new_size)990 static int journal_read_buf_realloc(struct journal_read_buf *b,
991 				    size_t new_size)
992 {
993 	void *n;
994 
995 	/* the bios are sized for this many pages, max: */
996 	if (new_size > JOURNAL_ENTRY_SIZE_MAX)
997 		return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
998 
999 	new_size = roundup_pow_of_two(new_size);
1000 	n = kvmalloc(new_size, GFP_KERNEL);
1001 	if (!n)
1002 		return -BCH_ERR_ENOMEM_journal_read_buf_realloc;
1003 
1004 	kvfree(b->data);
1005 	b->data = n;
1006 	b->size = new_size;
1007 	return 0;
1008 }
1009 
journal_read_bucket(struct bch_dev * ca,struct journal_read_buf * buf,struct journal_list * jlist,unsigned bucket)1010 static int journal_read_bucket(struct bch_dev *ca,
1011 			       struct journal_read_buf *buf,
1012 			       struct journal_list *jlist,
1013 			       unsigned bucket)
1014 {
1015 	struct bch_fs *c = ca->fs;
1016 	struct journal_device *ja = &ca->journal;
1017 	struct jset *j = NULL;
1018 	unsigned sectors, sectors_read = 0;
1019 	u64 offset = bucket_to_sector(ca, ja->buckets[bucket]),
1020 	    end = offset + ca->mi.bucket_size;
1021 	bool saw_bad = false, csum_good;
1022 	struct printbuf err = PRINTBUF;
1023 	int ret = 0;
1024 
1025 	pr_debug("reading %u", bucket);
1026 
1027 	while (offset < end) {
1028 		if (!sectors_read) {
1029 			struct bio *bio;
1030 			unsigned nr_bvecs;
1031 reread:
1032 			sectors_read = min_t(unsigned,
1033 				end - offset, buf->size >> 9);
1034 			nr_bvecs = buf_pages(buf->data, sectors_read << 9);
1035 
1036 			bio = bio_kmalloc(nr_bvecs, GFP_KERNEL);
1037 			if (!bio)
1038 				return -BCH_ERR_ENOMEM_journal_read_bucket;
1039 			bio_init(bio, ca->disk_sb.bdev, bio->bi_inline_vecs, nr_bvecs, REQ_OP_READ);
1040 
1041 			bio->bi_iter.bi_sector = offset;
1042 			bch2_bio_map(bio, buf->data, sectors_read << 9);
1043 
1044 			u64 submit_time = local_clock();
1045 			ret = submit_bio_wait(bio);
1046 			kfree(bio);
1047 
1048 			if (!ret && bch2_meta_read_fault("journal"))
1049 				ret = -BCH_ERR_EIO_fault_injected;
1050 
1051 			bch2_account_io_completion(ca, BCH_MEMBER_ERROR_read,
1052 						   submit_time, !ret);
1053 
1054 			if (ret) {
1055 				bch_err_dev_ratelimited(ca,
1056 					"journal read error: sector %llu", offset);
1057 				/*
1058 				 * We don't error out of the recovery process
1059 				 * here, since the relevant journal entry may be
1060 				 * found on a different device, and missing or
1061 				 * no journal entries will be handled later
1062 				 */
1063 				goto out;
1064 			}
1065 
1066 			j = buf->data;
1067 		}
1068 
1069 		ret = jset_validate_early(c, ca, j, offset,
1070 				    end - offset, sectors_read);
1071 		switch (ret) {
1072 		case 0:
1073 			sectors = vstruct_sectors(j, c->block_bits);
1074 			break;
1075 		case JOURNAL_ENTRY_REREAD:
1076 			if (vstruct_bytes(j) > buf->size) {
1077 				ret = journal_read_buf_realloc(buf,
1078 							vstruct_bytes(j));
1079 				if (ret)
1080 					goto err;
1081 			}
1082 			goto reread;
1083 		case JOURNAL_ENTRY_NONE:
1084 			if (!saw_bad)
1085 				goto out;
1086 			/*
1087 			 * On checksum error we don't really trust the size
1088 			 * field of the journal entry we read, so try reading
1089 			 * again at next block boundary:
1090 			 */
1091 			sectors = block_sectors(c);
1092 			goto next_block;
1093 		default:
1094 			goto err;
1095 		}
1096 
1097 		if (le64_to_cpu(j->seq) > ja->highest_seq_found) {
1098 			ja->highest_seq_found = le64_to_cpu(j->seq);
1099 			ja->cur_idx = bucket;
1100 			ja->sectors_free = ca->mi.bucket_size -
1101 				bucket_remainder(ca, offset) - sectors;
1102 		}
1103 
1104 		/*
1105 		 * This happens sometimes if we don't have discards on -
1106 		 * when we've partially overwritten a bucket with new
1107 		 * journal entries. We don't need the rest of the
1108 		 * bucket:
1109 		 */
1110 		if (le64_to_cpu(j->seq) < ja->bucket_seq[bucket])
1111 			goto out;
1112 
1113 		ja->bucket_seq[bucket] = le64_to_cpu(j->seq);
1114 
1115 		enum bch_csum_type csum_type = JSET_CSUM_TYPE(j);
1116 		struct bch_csum csum;
1117 		csum_good = jset_csum_good(c, j, &csum);
1118 
1119 		bch2_account_io_completion(ca, BCH_MEMBER_ERROR_checksum, 0, csum_good);
1120 
1121 		if (!csum_good) {
1122 			bch_err_dev_ratelimited(ca, "%s",
1123 				(printbuf_reset(&err),
1124 				 prt_str(&err, "journal "),
1125 				 bch2_csum_err_msg(&err, csum_type, j->csum, csum),
1126 				 err.buf));
1127 			saw_bad = true;
1128 		}
1129 
1130 		ret = bch2_encrypt(c, JSET_CSUM_TYPE(j), journal_nonce(j),
1131 			     j->encrypted_start,
1132 			     vstruct_end(j) - (void *) j->encrypted_start);
1133 		bch2_fs_fatal_err_on(ret, c, "decrypting journal entry: %s", bch2_err_str(ret));
1134 
1135 		mutex_lock(&jlist->lock);
1136 		ret = journal_entry_add(c, ca, (struct journal_ptr) {
1137 					.csum_good	= csum_good,
1138 					.dev		= ca->dev_idx,
1139 					.bucket		= bucket,
1140 					.bucket_offset	= offset -
1141 						bucket_to_sector(ca, ja->buckets[bucket]),
1142 					.sector		= offset,
1143 					}, jlist, j);
1144 		mutex_unlock(&jlist->lock);
1145 
1146 		switch (ret) {
1147 		case JOURNAL_ENTRY_ADD_OK:
1148 			break;
1149 		case JOURNAL_ENTRY_ADD_OUT_OF_RANGE:
1150 			break;
1151 		default:
1152 			goto err;
1153 		}
1154 next_block:
1155 		pr_debug("next");
1156 		offset		+= sectors;
1157 		sectors_read	-= sectors;
1158 		j = ((void *) j) + (sectors << 9);
1159 	}
1160 
1161 out:
1162 	ret = 0;
1163 err:
1164 	printbuf_exit(&err);
1165 	return ret;
1166 }
1167 
CLOSURE_CALLBACK(bch2_journal_read_device)1168 static CLOSURE_CALLBACK(bch2_journal_read_device)
1169 {
1170 	closure_type(ja, struct journal_device, read);
1171 	struct bch_dev *ca = container_of(ja, struct bch_dev, journal);
1172 	struct bch_fs *c = ca->fs;
1173 	struct journal_list *jlist =
1174 		container_of(cl->parent, struct journal_list, cl);
1175 	struct journal_read_buf buf = { NULL, 0 };
1176 	unsigned i;
1177 	int ret = 0;
1178 
1179 	if (!ja->nr)
1180 		goto out;
1181 
1182 	ret = journal_read_buf_realloc(&buf, PAGE_SIZE);
1183 	if (ret)
1184 		goto err;
1185 
1186 	pr_debug("%u journal buckets", ja->nr);
1187 
1188 	for (i = 0; i < ja->nr; i++) {
1189 		ret = journal_read_bucket(ca, &buf, jlist, i);
1190 		if (ret)
1191 			goto err;
1192 	}
1193 
1194 	/*
1195 	 * Set dirty_idx to indicate the entire journal is full and needs to be
1196 	 * reclaimed - journal reclaim will immediately reclaim whatever isn't
1197 	 * pinned when it first runs:
1198 	 */
1199 	ja->discard_idx = ja->dirty_idx_ondisk =
1200 		ja->dirty_idx = (ja->cur_idx + 1) % ja->nr;
1201 out:
1202 	bch_verbose(c, "journal read done on device %s, ret %i", ca->name, ret);
1203 	kvfree(buf.data);
1204 	percpu_ref_put(&ca->io_ref);
1205 	closure_return(cl);
1206 	return;
1207 err:
1208 	mutex_lock(&jlist->lock);
1209 	jlist->ret = ret;
1210 	mutex_unlock(&jlist->lock);
1211 	goto out;
1212 }
1213 
bch2_journal_read(struct bch_fs * c,u64 * last_seq,u64 * blacklist_seq,u64 * start_seq)1214 int bch2_journal_read(struct bch_fs *c,
1215 		      u64 *last_seq,
1216 		      u64 *blacklist_seq,
1217 		      u64 *start_seq)
1218 {
1219 	struct journal_list jlist;
1220 	struct journal_replay *i, **_i, *prev = NULL;
1221 	struct genradix_iter radix_iter;
1222 	struct printbuf buf = PRINTBUF;
1223 	bool degraded = false, last_write_torn = false;
1224 	u64 seq;
1225 	int ret = 0;
1226 
1227 	closure_init_stack(&jlist.cl);
1228 	mutex_init(&jlist.lock);
1229 	jlist.last_seq = 0;
1230 	jlist.ret = 0;
1231 
1232 	for_each_member_device(c, ca) {
1233 		if (!c->opts.fsck &&
1234 		    !(bch2_dev_has_data(c, ca) & (1 << BCH_DATA_journal)))
1235 			continue;
1236 
1237 		if ((ca->mi.state == BCH_MEMBER_STATE_rw ||
1238 		     ca->mi.state == BCH_MEMBER_STATE_ro) &&
1239 		    percpu_ref_tryget(&ca->io_ref))
1240 			closure_call(&ca->journal.read,
1241 				     bch2_journal_read_device,
1242 				     system_unbound_wq,
1243 				     &jlist.cl);
1244 		else
1245 			degraded = true;
1246 	}
1247 
1248 	closure_sync(&jlist.cl);
1249 
1250 	if (jlist.ret)
1251 		return jlist.ret;
1252 
1253 	*last_seq	= 0;
1254 	*start_seq	= 0;
1255 	*blacklist_seq	= 0;
1256 
1257 	/*
1258 	 * Find most recent flush entry, and ignore newer non flush entries -
1259 	 * those entries will be blacklisted:
1260 	 */
1261 	genradix_for_each_reverse(&c->journal_entries, radix_iter, _i) {
1262 		i = *_i;
1263 
1264 		if (journal_replay_ignore(i))
1265 			continue;
1266 
1267 		if (!*start_seq)
1268 			*blacklist_seq = *start_seq = le64_to_cpu(i->j.seq) + 1;
1269 
1270 		if (JSET_NO_FLUSH(&i->j)) {
1271 			i->ignore_blacklisted = true;
1272 			continue;
1273 		}
1274 
1275 		if (!last_write_torn && !i->csum_good) {
1276 			last_write_torn = true;
1277 			i->ignore_blacklisted = true;
1278 			continue;
1279 		}
1280 
1281 		struct bkey_validate_context from = {
1282 			.from		= BKEY_VALIDATE_journal,
1283 			.journal_seq	= le64_to_cpu(i->j.seq),
1284 		};
1285 		if (journal_entry_err_on(le64_to_cpu(i->j.last_seq) > le64_to_cpu(i->j.seq),
1286 					 c, le32_to_cpu(i->j.version), &i->j, NULL,
1287 					 jset_last_seq_newer_than_seq,
1288 					 "invalid journal entry: last_seq > seq (%llu > %llu)",
1289 					 le64_to_cpu(i->j.last_seq),
1290 					 le64_to_cpu(i->j.seq)))
1291 			i->j.last_seq = i->j.seq;
1292 
1293 		*last_seq	= le64_to_cpu(i->j.last_seq);
1294 		*blacklist_seq	= le64_to_cpu(i->j.seq) + 1;
1295 		break;
1296 	}
1297 
1298 	if (!*start_seq) {
1299 		bch_info(c, "journal read done, but no entries found");
1300 		return 0;
1301 	}
1302 
1303 	if (!*last_seq) {
1304 		fsck_err(c, dirty_but_no_journal_entries_post_drop_nonflushes,
1305 			 "journal read done, but no entries found after dropping non-flushes");
1306 		return 0;
1307 	}
1308 
1309 	bch_info(c, "journal read done, replaying entries %llu-%llu",
1310 		 *last_seq, *blacklist_seq - 1);
1311 
1312 	if (*start_seq != *blacklist_seq)
1313 		bch_info(c, "dropped unflushed entries %llu-%llu",
1314 			 *blacklist_seq, *start_seq - 1);
1315 
1316 	/* Drop blacklisted entries and entries older than last_seq: */
1317 	genradix_for_each(&c->journal_entries, radix_iter, _i) {
1318 		i = *_i;
1319 
1320 		if (journal_replay_ignore(i))
1321 			continue;
1322 
1323 		seq = le64_to_cpu(i->j.seq);
1324 		if (seq < *last_seq) {
1325 			journal_replay_free(c, i, false);
1326 			continue;
1327 		}
1328 
1329 		if (bch2_journal_seq_is_blacklisted(c, seq, true)) {
1330 			fsck_err_on(!JSET_NO_FLUSH(&i->j), c,
1331 				    jset_seq_blacklisted,
1332 				    "found blacklisted journal entry %llu", seq);
1333 			i->ignore_blacklisted = true;
1334 		}
1335 	}
1336 
1337 	/* Check for missing entries: */
1338 	seq = *last_seq;
1339 	genradix_for_each(&c->journal_entries, radix_iter, _i) {
1340 		i = *_i;
1341 
1342 		if (journal_replay_ignore(i))
1343 			continue;
1344 
1345 		BUG_ON(seq > le64_to_cpu(i->j.seq));
1346 
1347 		while (seq < le64_to_cpu(i->j.seq)) {
1348 			u64 missing_start, missing_end;
1349 			struct printbuf buf1 = PRINTBUF, buf2 = PRINTBUF;
1350 
1351 			while (seq < le64_to_cpu(i->j.seq) &&
1352 			       bch2_journal_seq_is_blacklisted(c, seq, false))
1353 				seq++;
1354 
1355 			if (seq == le64_to_cpu(i->j.seq))
1356 				break;
1357 
1358 			missing_start = seq;
1359 
1360 			while (seq < le64_to_cpu(i->j.seq) &&
1361 			       !bch2_journal_seq_is_blacklisted(c, seq, false))
1362 				seq++;
1363 
1364 			if (prev) {
1365 				bch2_journal_ptrs_to_text(&buf1, c, prev);
1366 				prt_printf(&buf1, " size %zu", vstruct_sectors(&prev->j, c->block_bits));
1367 			} else
1368 				prt_printf(&buf1, "(none)");
1369 			bch2_journal_ptrs_to_text(&buf2, c, i);
1370 
1371 			missing_end = seq - 1;
1372 			fsck_err(c, journal_entries_missing,
1373 				 "journal entries %llu-%llu missing! (replaying %llu-%llu)\n"
1374 				 "  prev at %s\n"
1375 				 "  next at %s, continue?",
1376 				 missing_start, missing_end,
1377 				 *last_seq, *blacklist_seq - 1,
1378 				 buf1.buf, buf2.buf);
1379 
1380 			printbuf_exit(&buf1);
1381 			printbuf_exit(&buf2);
1382 		}
1383 
1384 		prev = i;
1385 		seq++;
1386 	}
1387 
1388 	genradix_for_each(&c->journal_entries, radix_iter, _i) {
1389 		struct bch_replicas_padded replicas = {
1390 			.e.data_type = BCH_DATA_journal,
1391 			.e.nr_devs = 0,
1392 			.e.nr_required = 1,
1393 		};
1394 
1395 		i = *_i;
1396 		if (journal_replay_ignore(i))
1397 			continue;
1398 
1399 		darray_for_each(i->ptrs, ptr) {
1400 			struct bch_dev *ca = bch2_dev_have_ref(c, ptr->dev);
1401 
1402 			if (!ptr->csum_good)
1403 				bch_err_dev_offset(ca, ptr->sector,
1404 						   "invalid journal checksum, seq %llu%s",
1405 						   le64_to_cpu(i->j.seq),
1406 						   i->csum_good ? " (had good copy on another device)" : "");
1407 		}
1408 
1409 		ret = jset_validate(c,
1410 				    bch2_dev_have_ref(c, i->ptrs.data[0].dev),
1411 				    &i->j,
1412 				    i->ptrs.data[0].sector,
1413 				    READ);
1414 		if (ret)
1415 			goto err;
1416 
1417 		darray_for_each(i->ptrs, ptr)
1418 			replicas_entry_add_dev(&replicas.e, ptr->dev);
1419 
1420 		bch2_replicas_entry_sort(&replicas.e);
1421 
1422 		printbuf_reset(&buf);
1423 		bch2_replicas_entry_to_text(&buf, &replicas.e);
1424 
1425 		if (!degraded &&
1426 		    !bch2_replicas_marked(c, &replicas.e) &&
1427 		    (le64_to_cpu(i->j.seq) == *last_seq ||
1428 		     fsck_err(c, journal_entry_replicas_not_marked,
1429 			      "superblock not marked as containing replicas for journal entry %llu\n  %s",
1430 			      le64_to_cpu(i->j.seq), buf.buf))) {
1431 			ret = bch2_mark_replicas(c, &replicas.e);
1432 			if (ret)
1433 				goto err;
1434 		}
1435 	}
1436 err:
1437 fsck_err:
1438 	printbuf_exit(&buf);
1439 	return ret;
1440 }
1441 
1442 /* journal write: */
1443 
journal_advance_devs_to_next_bucket(struct journal * j,struct dev_alloc_list * devs,unsigned sectors,u64 seq)1444 static void journal_advance_devs_to_next_bucket(struct journal *j,
1445 						struct dev_alloc_list *devs,
1446 						unsigned sectors, u64 seq)
1447 {
1448 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1449 
1450 	darray_for_each(*devs, i) {
1451 		struct bch_dev *ca = rcu_dereference(c->devs[*i]);
1452 		if (!ca)
1453 			continue;
1454 
1455 		struct journal_device *ja = &ca->journal;
1456 
1457 		if (sectors > ja->sectors_free &&
1458 		    sectors <= ca->mi.bucket_size &&
1459 		    bch2_journal_dev_buckets_available(j, ja,
1460 					journal_space_discarded)) {
1461 			ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
1462 			ja->sectors_free = ca->mi.bucket_size;
1463 
1464 			/*
1465 			 * ja->bucket_seq[ja->cur_idx] must always have
1466 			 * something sensible:
1467 			 */
1468 			ja->bucket_seq[ja->cur_idx] = le64_to_cpu(seq);
1469 		}
1470 	}
1471 }
1472 
__journal_write_alloc(struct journal * j,struct journal_buf * w,struct dev_alloc_list * devs,unsigned sectors,unsigned * replicas,unsigned replicas_want)1473 static void __journal_write_alloc(struct journal *j,
1474 				  struct journal_buf *w,
1475 				  struct dev_alloc_list *devs,
1476 				  unsigned sectors,
1477 				  unsigned *replicas,
1478 				  unsigned replicas_want)
1479 {
1480 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1481 
1482 	darray_for_each(*devs, i) {
1483 		struct bch_dev *ca = rcu_dereference(c->devs[*i]);
1484 		if (!ca)
1485 			continue;
1486 
1487 		struct journal_device *ja = &ca->journal;
1488 
1489 		/*
1490 		 * Check that we can use this device, and aren't already using
1491 		 * it:
1492 		 */
1493 		if (!ca->mi.durability ||
1494 		    ca->mi.state != BCH_MEMBER_STATE_rw ||
1495 		    !ja->nr ||
1496 		    bch2_bkey_has_device_c(bkey_i_to_s_c(&w->key), ca->dev_idx) ||
1497 		    sectors > ja->sectors_free)
1498 			continue;
1499 
1500 		bch2_dev_stripe_increment(ca, &j->wp.stripe);
1501 
1502 		bch2_bkey_append_ptr(&w->key,
1503 			(struct bch_extent_ptr) {
1504 				  .offset = bucket_to_sector(ca,
1505 					ja->buckets[ja->cur_idx]) +
1506 					ca->mi.bucket_size -
1507 					ja->sectors_free,
1508 				  .dev = ca->dev_idx,
1509 		});
1510 
1511 		ja->sectors_free -= sectors;
1512 		ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1513 
1514 		*replicas += ca->mi.durability;
1515 
1516 		if (*replicas >= replicas_want)
1517 			break;
1518 	}
1519 }
1520 
1521 /**
1522  * journal_write_alloc - decide where to write next journal entry
1523  *
1524  * @j:		journal object
1525  * @w:		journal buf (entry to be written)
1526  *
1527  * Returns: 0 on success, or -BCH_ERR_insufficient_devices on failure
1528  */
journal_write_alloc(struct journal * j,struct journal_buf * w)1529 static int journal_write_alloc(struct journal *j, struct journal_buf *w)
1530 {
1531 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1532 	struct bch_devs_mask devs;
1533 	struct dev_alloc_list devs_sorted;
1534 	unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1535 	unsigned target = c->opts.metadata_target ?:
1536 		c->opts.foreground_target;
1537 	unsigned replicas = 0, replicas_want =
1538 		READ_ONCE(c->opts.metadata_replicas);
1539 	unsigned replicas_need = min_t(unsigned, replicas_want,
1540 				       READ_ONCE(c->opts.metadata_replicas_required));
1541 	bool advance_done = false;
1542 
1543 	rcu_read_lock();
1544 
1545 	/* We might run more than once if we have to stop and do discards: */
1546 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&w->key));
1547 	bkey_for_each_ptr(ptrs, p) {
1548 		struct bch_dev *ca = bch2_dev_rcu_noerror(c, p->dev);
1549 		if (ca)
1550 			replicas += ca->mi.durability;
1551 	}
1552 
1553 retry_target:
1554 	devs = target_rw_devs(c, BCH_DATA_journal, target);
1555 	devs_sorted = bch2_dev_alloc_list(c, &j->wp.stripe, &devs);
1556 retry_alloc:
1557 	__journal_write_alloc(j, w, &devs_sorted, sectors, &replicas, replicas_want);
1558 
1559 	if (likely(replicas >= replicas_want))
1560 		goto done;
1561 
1562 	if (!advance_done) {
1563 		journal_advance_devs_to_next_bucket(j, &devs_sorted, sectors, w->data->seq);
1564 		advance_done = true;
1565 		goto retry_alloc;
1566 	}
1567 
1568 	if (replicas < replicas_want && target) {
1569 		/* Retry from all devices: */
1570 		target = 0;
1571 		advance_done = false;
1572 		goto retry_target;
1573 	}
1574 done:
1575 	rcu_read_unlock();
1576 
1577 	BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
1578 
1579 	return replicas >= replicas_need ? 0 : -BCH_ERR_insufficient_journal_devices;
1580 }
1581 
journal_buf_realloc(struct journal * j,struct journal_buf * buf)1582 static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)
1583 {
1584 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1585 
1586 	/* we aren't holding j->lock: */
1587 	unsigned new_size = READ_ONCE(j->buf_size_want);
1588 	void *new_buf;
1589 
1590 	if (buf->buf_size >= new_size)
1591 		return;
1592 
1593 	size_t btree_write_buffer_size = new_size / 64;
1594 
1595 	if (bch2_btree_write_buffer_resize(c, btree_write_buffer_size))
1596 		return;
1597 
1598 	new_buf = kvmalloc(new_size, GFP_NOFS|__GFP_NOWARN);
1599 	if (!new_buf)
1600 		return;
1601 
1602 	memcpy(new_buf, buf->data, buf->buf_size);
1603 
1604 	spin_lock(&j->lock);
1605 	swap(buf->data,		new_buf);
1606 	swap(buf->buf_size,	new_size);
1607 	spin_unlock(&j->lock);
1608 
1609 	kvfree(new_buf);
1610 }
1611 
CLOSURE_CALLBACK(journal_write_done)1612 static CLOSURE_CALLBACK(journal_write_done)
1613 {
1614 	closure_type(w, struct journal_buf, io);
1615 	struct journal *j = container_of(w, struct journal, buf[w->idx]);
1616 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1617 	struct bch_replicas_padded replicas;
1618 	u64 seq = le64_to_cpu(w->data->seq);
1619 	int err = 0;
1620 
1621 	bch2_time_stats_update(!JSET_NO_FLUSH(w->data)
1622 			       ? j->flush_write_time
1623 			       : j->noflush_write_time, j->write_start_time);
1624 
1625 	if (!w->devs_written.nr) {
1626 		bch_err(c, "unable to write journal to sufficient devices");
1627 		err = -BCH_ERR_journal_write_err;
1628 	} else {
1629 		bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
1630 					 w->devs_written);
1631 		err = bch2_mark_replicas(c, &replicas.e);
1632 	}
1633 
1634 	if (err)
1635 		bch2_fatal_error(c);
1636 
1637 	closure_debug_destroy(cl);
1638 
1639 	spin_lock(&j->lock);
1640 	if (seq >= j->pin.front)
1641 		journal_seq_pin(j, seq)->devs = w->devs_written;
1642 	if (err && (!j->err_seq || seq < j->err_seq))
1643 		j->err_seq	= seq;
1644 	w->write_done = true;
1645 
1646 	if (!j->free_buf || j->free_buf_size < w->buf_size) {
1647 		swap(j->free_buf,	w->data);
1648 		swap(j->free_buf_size,	w->buf_size);
1649 	}
1650 
1651 	if (w->data) {
1652 		void *buf = w->data;
1653 		w->data = NULL;
1654 		w->buf_size = 0;
1655 
1656 		spin_unlock(&j->lock);
1657 		kvfree(buf);
1658 		spin_lock(&j->lock);
1659 	}
1660 
1661 	bool completed = false;
1662 	bool do_discards = false;
1663 
1664 	for (seq = journal_last_unwritten_seq(j);
1665 	     seq <= journal_cur_seq(j);
1666 	     seq++) {
1667 		w = j->buf + (seq & JOURNAL_BUF_MASK);
1668 		if (!w->write_done)
1669 			break;
1670 
1671 		if (!j->err_seq && !w->noflush) {
1672 			j->flushed_seq_ondisk = seq;
1673 			j->last_seq_ondisk = w->last_seq;
1674 
1675 			closure_wake_up(&c->freelist_wait);
1676 			bch2_reset_alloc_cursors(c);
1677 		}
1678 
1679 		j->seq_ondisk = seq;
1680 
1681 		/*
1682 		 * Updating last_seq_ondisk may let bch2_journal_reclaim_work() discard
1683 		 * more buckets:
1684 		 *
1685 		 * Must come before signaling write completion, for
1686 		 * bch2_fs_journal_stop():
1687 		 */
1688 		if (j->watermark != BCH_WATERMARK_stripe)
1689 			journal_reclaim_kick(&c->journal);
1690 
1691 		closure_wake_up(&w->wait);
1692 		completed = true;
1693 	}
1694 
1695 	if (completed) {
1696 		bch2_journal_reclaim_fast(j);
1697 		bch2_journal_space_available(j);
1698 
1699 		track_event_change(&c->times[BCH_TIME_blocked_journal_max_in_flight], false);
1700 
1701 		journal_wake(j);
1702 	}
1703 
1704 	if (journal_last_unwritten_seq(j) == journal_cur_seq(j) &&
1705 	    j->reservations.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL) {
1706 		struct journal_buf *buf = journal_cur_buf(j);
1707 		long delta = buf->expires - jiffies;
1708 
1709 		/*
1710 		 * We don't close a journal entry to write it while there's
1711 		 * previous entries still in flight - the current journal entry
1712 		 * might want to be written now:
1713 		 */
1714 		mod_delayed_work(j->wq, &j->write_work, max(0L, delta));
1715 	}
1716 
1717 	/*
1718 	 * We don't typically trigger journal writes from her - the next journal
1719 	 * write will be triggered immediately after the previous one is
1720 	 * allocated, in bch2_journal_write() - but the journal write error path
1721 	 * is special:
1722 	 */
1723 	bch2_journal_do_writes(j);
1724 	spin_unlock(&j->lock);
1725 
1726 	if (do_discards)
1727 		bch2_do_discards(c);
1728 }
1729 
journal_write_endio(struct bio * bio)1730 static void journal_write_endio(struct bio *bio)
1731 {
1732 	struct journal_bio *jbio = container_of(bio, struct journal_bio, bio);
1733 	struct bch_dev *ca = jbio->ca;
1734 	struct journal *j = &ca->fs->journal;
1735 	struct journal_buf *w = j->buf + jbio->buf_idx;
1736 
1737 	bch2_account_io_completion(ca, BCH_MEMBER_ERROR_write,
1738 				   jbio->submit_time, !bio->bi_status);
1739 
1740 	if (bio->bi_status) {
1741 		bch_err_dev_ratelimited(ca,
1742 			       "error writing journal entry %llu: %s",
1743 			       le64_to_cpu(w->data->seq),
1744 			       bch2_blk_status_to_str(bio->bi_status));
1745 
1746 		unsigned long flags;
1747 		spin_lock_irqsave(&j->err_lock, flags);
1748 		bch2_dev_list_drop_dev(&w->devs_written, ca->dev_idx);
1749 		spin_unlock_irqrestore(&j->err_lock, flags);
1750 	}
1751 
1752 	closure_put(&w->io);
1753 	percpu_ref_put(&ca->io_ref);
1754 }
1755 
CLOSURE_CALLBACK(journal_write_submit)1756 static CLOSURE_CALLBACK(journal_write_submit)
1757 {
1758 	closure_type(w, struct journal_buf, io);
1759 	struct journal *j = container_of(w, struct journal, buf[w->idx]);
1760 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1761 	unsigned sectors = vstruct_sectors(w->data, c->block_bits);
1762 
1763 	extent_for_each_ptr(bkey_i_to_s_extent(&w->key), ptr) {
1764 		struct bch_dev *ca = bch2_dev_get_ioref(c, ptr->dev, WRITE);
1765 		if (!ca) {
1766 			/* XXX: fix this */
1767 			bch_err(c, "missing device for journal write\n");
1768 			continue;
1769 		}
1770 
1771 		this_cpu_add(ca->io_done->sectors[WRITE][BCH_DATA_journal],
1772 			     sectors);
1773 
1774 		struct journal_device *ja = &ca->journal;
1775 		struct journal_bio *jbio = ja->bio[w->idx];
1776 		struct bio *bio = &jbio->bio;
1777 
1778 		jbio->submit_time	= local_clock();
1779 
1780 		bio_reset(bio, ca->disk_sb.bdev, REQ_OP_WRITE|REQ_SYNC|REQ_META);
1781 		bio->bi_iter.bi_sector	= ptr->offset;
1782 		bio->bi_end_io		= journal_write_endio;
1783 		bio->bi_private		= ca;
1784 		bio->bi_ioprio		= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 0);
1785 
1786 		BUG_ON(bio->bi_iter.bi_sector == ca->prev_journal_sector);
1787 		ca->prev_journal_sector = bio->bi_iter.bi_sector;
1788 
1789 		if (!JSET_NO_FLUSH(w->data))
1790 			bio->bi_opf    |= REQ_FUA;
1791 		if (!JSET_NO_FLUSH(w->data) && !w->separate_flush)
1792 			bio->bi_opf    |= REQ_PREFLUSH;
1793 
1794 		bch2_bio_map(bio, w->data, sectors << 9);
1795 
1796 		trace_and_count(c, journal_write, bio);
1797 		closure_bio_submit(bio, cl);
1798 
1799 		ja->bucket_seq[ja->cur_idx] = le64_to_cpu(w->data->seq);
1800 	}
1801 
1802 	continue_at(cl, journal_write_done, j->wq);
1803 }
1804 
CLOSURE_CALLBACK(journal_write_preflush)1805 static CLOSURE_CALLBACK(journal_write_preflush)
1806 {
1807 	closure_type(w, struct journal_buf, io);
1808 	struct journal *j = container_of(w, struct journal, buf[w->idx]);
1809 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1810 
1811 	/*
1812 	 * Wait for previous journal writes to comelete; they won't necessarily
1813 	 * be flushed if they're still in flight
1814 	 */
1815 	if (j->seq_ondisk + 1 != le64_to_cpu(w->data->seq)) {
1816 		spin_lock(&j->lock);
1817 		if (j->seq_ondisk + 1 != le64_to_cpu(w->data->seq)) {
1818 			closure_wait(&j->async_wait, cl);
1819 			spin_unlock(&j->lock);
1820 			continue_at(cl, journal_write_preflush, j->wq);
1821 			return;
1822 		}
1823 		spin_unlock(&j->lock);
1824 	}
1825 
1826 	if (w->separate_flush) {
1827 		for_each_rw_member(c, ca) {
1828 			percpu_ref_get(&ca->io_ref);
1829 
1830 			struct journal_device *ja = &ca->journal;
1831 			struct bio *bio = &ja->bio[w->idx]->bio;
1832 			bio_reset(bio, ca->disk_sb.bdev,
1833 				  REQ_OP_WRITE|REQ_SYNC|REQ_META|REQ_PREFLUSH);
1834 			bio->bi_end_io		= journal_write_endio;
1835 			bio->bi_private		= ca;
1836 			closure_bio_submit(bio, cl);
1837 		}
1838 
1839 		continue_at(cl, journal_write_submit, j->wq);
1840 	} else {
1841 		/*
1842 		 * no need to punt to another work item if we're not waiting on
1843 		 * preflushes
1844 		 */
1845 		journal_write_submit(&cl->work);
1846 	}
1847 }
1848 
bch2_journal_write_prep(struct journal * j,struct journal_buf * w)1849 static int bch2_journal_write_prep(struct journal *j, struct journal_buf *w)
1850 {
1851 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1852 	struct jset_entry *start, *end;
1853 	struct jset *jset = w->data;
1854 	struct journal_keys_to_wb wb = { NULL };
1855 	unsigned sectors, bytes, u64s;
1856 	unsigned long btree_roots_have = 0;
1857 	bool validate_before_checksum = false;
1858 	u64 seq = le64_to_cpu(jset->seq);
1859 	int ret;
1860 
1861 	/*
1862 	 * Simple compaction, dropping empty jset_entries (from journal
1863 	 * reservations that weren't fully used) and merging jset_entries that
1864 	 * can be.
1865 	 *
1866 	 * If we wanted to be really fancy here, we could sort all the keys in
1867 	 * the jset and drop keys that were overwritten - probably not worth it:
1868 	 */
1869 	vstruct_for_each(jset, i) {
1870 		unsigned u64s = le16_to_cpu(i->u64s);
1871 
1872 		/* Empty entry: */
1873 		if (!u64s)
1874 			continue;
1875 
1876 		/*
1877 		 * New btree roots are set by journalling them; when the journal
1878 		 * entry gets written we have to propagate them to
1879 		 * c->btree_roots
1880 		 *
1881 		 * But, every journal entry we write has to contain all the
1882 		 * btree roots (at least for now); so after we copy btree roots
1883 		 * to c->btree_roots we have to get any missing btree roots and
1884 		 * add them to this journal entry:
1885 		 */
1886 		switch (i->type) {
1887 		case BCH_JSET_ENTRY_btree_root:
1888 			bch2_journal_entry_to_btree_root(c, i);
1889 			__set_bit(i->btree_id, &btree_roots_have);
1890 			break;
1891 		case BCH_JSET_ENTRY_write_buffer_keys:
1892 			EBUG_ON(!w->need_flush_to_write_buffer);
1893 
1894 			if (!wb.wb)
1895 				bch2_journal_keys_to_write_buffer_start(c, &wb, seq);
1896 
1897 			jset_entry_for_each_key(i, k) {
1898 				ret = bch2_journal_key_to_wb(c, &wb, i->btree_id, k);
1899 				if (ret) {
1900 					bch2_fs_fatal_error(c, "flushing journal keys to btree write buffer: %s",
1901 							    bch2_err_str(ret));
1902 					bch2_journal_keys_to_write_buffer_end(c, &wb);
1903 					return ret;
1904 				}
1905 			}
1906 			i->type = BCH_JSET_ENTRY_btree_keys;
1907 			break;
1908 		}
1909 	}
1910 
1911 	if (wb.wb) {
1912 		ret = bch2_journal_keys_to_write_buffer_end(c, &wb);
1913 		if (ret) {
1914 			bch2_fs_fatal_error(c, "error flushing journal keys to btree write buffer: %s",
1915 					    bch2_err_str(ret));
1916 			return ret;
1917 		}
1918 	}
1919 
1920 	spin_lock(&c->journal.lock);
1921 	w->need_flush_to_write_buffer = false;
1922 	spin_unlock(&c->journal.lock);
1923 
1924 	start = end = vstruct_last(jset);
1925 
1926 	end	= bch2_btree_roots_to_journal_entries(c, end, btree_roots_have);
1927 
1928 	struct jset_entry_datetime *d =
1929 		container_of(jset_entry_init(&end, sizeof(*d)), struct jset_entry_datetime, entry);
1930 	d->entry.type	= BCH_JSET_ENTRY_datetime;
1931 	d->seconds	= cpu_to_le64(ktime_get_real_seconds());
1932 
1933 	bch2_journal_super_entries_add_common(c, &end, seq);
1934 	u64s	= (u64 *) end - (u64 *) start;
1935 
1936 	WARN_ON(u64s > j->entry_u64s_reserved);
1937 
1938 	le32_add_cpu(&jset->u64s, u64s);
1939 
1940 	sectors = vstruct_sectors(jset, c->block_bits);
1941 	bytes	= vstruct_bytes(jset);
1942 
1943 	if (sectors > w->sectors) {
1944 		bch2_fs_fatal_error(c, ": journal write overran available space, %zu > %u (extra %u reserved %u/%u)",
1945 				    vstruct_bytes(jset), w->sectors << 9,
1946 				    u64s, w->u64s_reserved, j->entry_u64s_reserved);
1947 		return -EINVAL;
1948 	}
1949 
1950 	jset->magic		= cpu_to_le64(jset_magic(c));
1951 	jset->version		= cpu_to_le32(c->sb.version);
1952 
1953 	SET_JSET_BIG_ENDIAN(jset, CPU_BIG_ENDIAN);
1954 	SET_JSET_CSUM_TYPE(jset, bch2_meta_checksum_type(c));
1955 
1956 	if (!JSET_NO_FLUSH(jset) && journal_entry_empty(jset))
1957 		j->last_empty_seq = seq;
1958 
1959 	if (bch2_csum_type_is_encryption(JSET_CSUM_TYPE(jset)))
1960 		validate_before_checksum = true;
1961 
1962 	if (le32_to_cpu(jset->version) < bcachefs_metadata_version_current)
1963 		validate_before_checksum = true;
1964 
1965 	if (validate_before_checksum &&
1966 	    (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1967 		return ret;
1968 
1969 	ret = bch2_encrypt(c, JSET_CSUM_TYPE(jset), journal_nonce(jset),
1970 		    jset->encrypted_start,
1971 		    vstruct_end(jset) - (void *) jset->encrypted_start);
1972 	if (bch2_fs_fatal_err_on(ret, c, "decrypting journal entry: %s", bch2_err_str(ret)))
1973 		return ret;
1974 
1975 	jset->csum = csum_vstruct(c, JSET_CSUM_TYPE(jset),
1976 				  journal_nonce(jset), jset);
1977 
1978 	if (!validate_before_checksum &&
1979 	    (ret = jset_validate(c, NULL, jset, 0, WRITE)))
1980 		return ret;
1981 
1982 	memset((void *) jset + bytes, 0, (sectors << 9) - bytes);
1983 	return 0;
1984 }
1985 
bch2_journal_write_pick_flush(struct journal * j,struct journal_buf * w)1986 static int bch2_journal_write_pick_flush(struct journal *j, struct journal_buf *w)
1987 {
1988 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
1989 	int error = bch2_journal_error(j);
1990 
1991 	/*
1992 	 * If the journal is in an error state - we did an emergency shutdown -
1993 	 * we prefer to continue doing journal writes. We just mark them as
1994 	 * noflush so they'll never be used, but they'll still be visible by the
1995 	 * list_journal tool - this helps in debugging.
1996 	 *
1997 	 * There's a caveat: the first journal write after marking the
1998 	 * superblock dirty must always be a flush write, because on startup
1999 	 * from a clean shutdown we didn't necessarily read the journal and the
2000 	 * new journal write might overwrite whatever was in the journal
2001 	 * previously - we can't leave the journal without any flush writes in
2002 	 * it.
2003 	 *
2004 	 * So if we're in an error state, and we're still starting up, we don't
2005 	 * write anything at all.
2006 	 */
2007 	if (error && test_bit(JOURNAL_need_flush_write, &j->flags))
2008 		return error;
2009 
2010 	if (error ||
2011 	    w->noflush ||
2012 	    (!w->must_flush &&
2013 	     time_before(jiffies, j->last_flush_write +
2014 		 msecs_to_jiffies(c->opts.journal_flush_delay)) &&
2015 	     test_bit(JOURNAL_may_skip_flush, &j->flags))) {
2016 		w->noflush = true;
2017 		SET_JSET_NO_FLUSH(w->data, true);
2018 		w->data->last_seq	= 0;
2019 		w->last_seq		= 0;
2020 
2021 		j->nr_noflush_writes++;
2022 	} else {
2023 		w->must_flush = true;
2024 		j->last_flush_write = jiffies;
2025 		j->nr_flush_writes++;
2026 		clear_bit(JOURNAL_need_flush_write, &j->flags);
2027 	}
2028 
2029 	return 0;
2030 }
2031 
CLOSURE_CALLBACK(bch2_journal_write)2032 CLOSURE_CALLBACK(bch2_journal_write)
2033 {
2034 	closure_type(w, struct journal_buf, io);
2035 	struct journal *j = container_of(w, struct journal, buf[w->idx]);
2036 	struct bch_fs *c = container_of(j, struct bch_fs, journal);
2037 	struct bch_replicas_padded replicas;
2038 	unsigned nr_rw_members = 0;
2039 	int ret;
2040 
2041 	for_each_rw_member(c, ca)
2042 		nr_rw_members++;
2043 
2044 	BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
2045 	BUG_ON(!w->write_started);
2046 	BUG_ON(w->write_allocated);
2047 	BUG_ON(w->write_done);
2048 
2049 	j->write_start_time = local_clock();
2050 
2051 	spin_lock(&j->lock);
2052 	if (nr_rw_members > 1)
2053 		w->separate_flush = true;
2054 
2055 	ret = bch2_journal_write_pick_flush(j, w);
2056 	spin_unlock(&j->lock);
2057 	if (ret)
2058 		goto err;
2059 
2060 	mutex_lock(&j->buf_lock);
2061 	journal_buf_realloc(j, w);
2062 
2063 	ret = bch2_journal_write_prep(j, w);
2064 	mutex_unlock(&j->buf_lock);
2065 	if (ret)
2066 		goto err;
2067 
2068 	j->entry_bytes_written += vstruct_bytes(w->data);
2069 
2070 	while (1) {
2071 		spin_lock(&j->lock);
2072 		ret = journal_write_alloc(j, w);
2073 		if (!ret || !j->can_discard)
2074 			break;
2075 
2076 		spin_unlock(&j->lock);
2077 		bch2_journal_do_discards(j);
2078 	}
2079 
2080 	if (ret && !bch2_journal_error(j)) {
2081 		struct printbuf buf = PRINTBUF;
2082 		buf.atomic++;
2083 
2084 		prt_printf(&buf, bch2_fmt(c, "Unable to allocate journal write at seq %llu for %zu sectors: %s"),
2085 					  le64_to_cpu(w->data->seq),
2086 					  vstruct_sectors(w->data, c->block_bits),
2087 					  bch2_err_str(ret));
2088 		__bch2_journal_debug_to_text(&buf, j);
2089 		spin_unlock(&j->lock);
2090 		bch2_print_string_as_lines(KERN_ERR, buf.buf);
2091 		printbuf_exit(&buf);
2092 	}
2093 	if (ret)
2094 		goto err;
2095 
2096 	/*
2097 	 * write is allocated, no longer need to account for it in
2098 	 * bch2_journal_space_available():
2099 	 */
2100 	w->sectors = 0;
2101 	w->write_allocated = true;
2102 
2103 	/*
2104 	 * journal entry has been compacted and allocated, recalculate space
2105 	 * available:
2106 	 */
2107 	bch2_journal_space_available(j);
2108 	bch2_journal_do_writes(j);
2109 	spin_unlock(&j->lock);
2110 
2111 	w->devs_written = bch2_bkey_devs(bkey_i_to_s_c(&w->key));
2112 
2113 	if (c->opts.nochanges)
2114 		goto no_io;
2115 
2116 	/*
2117 	 * Mark journal replicas before we submit the write to guarantee
2118 	 * recovery will find the journal entries after a crash.
2119 	 */
2120 	bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
2121 				 w->devs_written);
2122 	ret = bch2_mark_replicas(c, &replicas.e);
2123 	if (ret)
2124 		goto err;
2125 
2126 	if (!JSET_NO_FLUSH(w->data))
2127 		continue_at(cl, journal_write_preflush, j->wq);
2128 	else
2129 		continue_at(cl, journal_write_submit, j->wq);
2130 	return;
2131 no_io:
2132 	continue_at(cl, journal_write_done, j->wq);
2133 	return;
2134 err:
2135 	bch2_fatal_error(c);
2136 	continue_at(cl, journal_write_done, j->wq);
2137 }
2138