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