xref: /linux/fs/bcachefs/journal_io.h (revision ff0905bbf991f4337b5ebc19c0d43525ebb0d96b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_JOURNAL_IO_H
3 #define _BCACHEFS_JOURNAL_IO_H
4 
5 #include "darray.h"
6 
7 void bch2_journal_pos_from_member_info_set(struct bch_fs *);
8 void bch2_journal_pos_from_member_info_resume(struct bch_fs *);
9 
10 struct journal_ptr {
11 	bool		csum_good;
12 	struct bch_csum	csum;
13 	u8		dev;
14 	u32		bucket;
15 	u32		bucket_offset;
16 	u64		sector;
17 };
18 
19 /*
20  * Only used for holding the journal entries we read in btree_journal_read()
21  * during cache_registration
22  */
23 struct journal_replay {
24 	DARRAY_PREALLOCATED(struct journal_ptr, 8) ptrs;
25 
26 	bool			csum_good;
27 	bool			ignore_blacklisted;
28 	bool			ignore_not_dirty;
29 	/* must be last: */
30 	struct jset		j;
31 };
32 
journal_replay_ignore(struct journal_replay * i)33 static inline bool journal_replay_ignore(struct journal_replay *i)
34 {
35 	return !i || i->ignore_blacklisted || i->ignore_not_dirty;
36 }
37 
__jset_entry_type_next(struct jset * jset,struct jset_entry * entry,unsigned type)38 static inline struct jset_entry *__jset_entry_type_next(struct jset *jset,
39 					struct jset_entry *entry, unsigned type)
40 {
41 	while (entry < vstruct_last(jset)) {
42 		if (entry->type == type)
43 			return entry;
44 
45 		entry = vstruct_next(entry);
46 	}
47 
48 	return NULL;
49 }
50 
51 #define for_each_jset_entry_type(entry, jset, type)			\
52 	for (struct jset_entry *entry = (jset)->start;			\
53 	     (entry = __jset_entry_type_next(jset, entry, type));	\
54 	     entry = vstruct_next(entry))
55 
56 #define jset_entry_for_each_key(_e, _k)					\
57 	for (struct bkey_i *_k = (_e)->start;				\
58 	     _k < vstruct_last(_e);					\
59 	     _k = bkey_next(_k))
60 
61 #define for_each_jset_key(k, entry, jset)				\
62 	for_each_jset_entry_type(entry, jset, BCH_JSET_ENTRY_btree_keys)\
63 		jset_entry_for_each_key(entry, k)
64 
65 int bch2_journal_entry_validate(struct bch_fs *, struct jset *,
66 				struct jset_entry *, unsigned, int,
67 				struct bkey_validate_context);
68 void bch2_journal_entry_to_text(struct printbuf *, struct bch_fs *,
69 				struct jset_entry *);
70 
71 void bch2_journal_ptrs_to_text(struct printbuf *, struct bch_fs *,
72 			       struct journal_replay *);
73 
74 int bch2_journal_read(struct bch_fs *, u64 *, u64 *, u64 *);
75 
76 CLOSURE_CALLBACK(bch2_journal_write);
77 
jset_entry_init(struct jset_entry ** end,size_t size)78 static inline struct jset_entry *jset_entry_init(struct jset_entry **end, size_t size)
79 {
80 	struct jset_entry *entry = *end;
81 	unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
82 
83 	memset(entry, 0, u64s * sizeof(u64));
84 	/*
85 	 * The u64s field counts from the start of data, ignoring the shared
86 	 * fields.
87 	 */
88 	entry->u64s = cpu_to_le16(u64s - 1);
89 
90 	*end = vstruct_next(*end);
91 	return entry;
92 }
93 
94 #endif /* _BCACHEFS_JOURNAL_IO_H */
95