xref: /linux/fs/bcachefs/super-io.h (revision 1cbfb828e05171ca2dd77b5988d068e6872480fe)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUPER_IO_H
3 #define _BCACHEFS_SUPER_IO_H
4 
5 #include "extents.h"
6 #include "eytzinger.h"
7 #include "super_types.h"
8 #include "super.h"
9 #include "sb-members.h"
10 
11 #include <asm/byteorder.h>
12 
13 #define BCH_SB_READ_SCRATCH_BUF_SIZE		4096
14 
15 static inline bool bch2_version_compatible(u16 version)
16 {
17 	return BCH_VERSION_MAJOR(version) <= BCH_VERSION_MAJOR(bcachefs_metadata_version_current) &&
18 		version >= bcachefs_metadata_version_min;
19 }
20 
21 void bch2_version_to_text(struct printbuf *, enum bcachefs_metadata_version);
22 enum bcachefs_metadata_version bch2_latest_compatible_version(enum bcachefs_metadata_version);
23 
24 void bch2_set_version_incompat(struct bch_fs *, enum bcachefs_metadata_version);
25 
26 static inline bool bch2_request_incompat_feature(struct bch_fs *c,
27 						 enum bcachefs_metadata_version version)
28 {
29 	if (unlikely(version > c->sb.version_incompat)) {
30 		if (version > c->sb.version_incompat_allowed)
31 			return false;
32 		bch2_set_version_incompat(c, version);
33 	}
34 	return true;
35 }
36 
37 static inline size_t bch2_sb_field_bytes(struct bch_sb_field *f)
38 {
39 	return le32_to_cpu(f->u64s) * sizeof(u64);
40 }
41 
42 #define field_to_type(_f, _name)					\
43 	container_of_or_null(_f, struct bch_sb_field_##_name, field)
44 
45 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *, enum bch_sb_field_type);
46 #define bch2_sb_field_get(_sb, _name)					\
47 	field_to_type(bch2_sb_field_get_id(_sb, BCH_SB_FIELD_##_name), _name)
48 
49 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *,
50 					     enum bch_sb_field_type, unsigned);
51 #define bch2_sb_field_resize(_sb, _name, _u64s)				\
52 	field_to_type(bch2_sb_field_resize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name)
53 
54 struct bch_sb_field *bch2_sb_field_get_minsize_id(struct bch_sb_handle *,
55 					enum bch_sb_field_type, unsigned);
56 #define bch2_sb_field_get_minsize(_sb, _name, _u64s)				\
57 	field_to_type(bch2_sb_field_get_minsize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name)
58 
59 #define bch2_sb_field_nr_entries(_f)					\
60 	(_f ? ((bch2_sb_field_bytes(&_f->field) - sizeof(*_f)) /	\
61 	       sizeof(_f->entries[0]))					\
62 	    : 0)
63 
64 void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type);
65 
66 extern const char * const bch2_sb_fields[];
67 
68 struct bch_sb_field_ops {
69 	int	(*validate)(struct bch_sb *, struct bch_sb_field *,
70 			    enum bch_validate_flags, struct printbuf *);
71 	void	(*to_text)(struct printbuf *, struct bch_sb *, struct bch_sb_field *);
72 };
73 
74 static inline __le64 bch2_sb_magic(struct bch_fs *c)
75 {
76 	__le64 ret;
77 
78 	memcpy(&ret, &c->sb.uuid, sizeof(ret));
79 	return ret;
80 }
81 
82 static inline __u64 jset_magic(struct bch_fs *c)
83 {
84 	return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
85 }
86 
87 static inline __u64 bset_magic(struct bch_fs *c)
88 {
89 	return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
90 }
91 
92 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
93 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
94 
95 void bch2_free_super(struct bch_sb_handle *);
96 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
97 
98 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
99 int bch2_read_super_silent(const char *, struct bch_opts *, struct bch_sb_handle *);
100 int bch2_write_super(struct bch_fs *);
101 void __bch2_check_set_feature(struct bch_fs *, unsigned);
102 
103 static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat)
104 {
105 	if (!(c->sb.features & (1ULL << feat)))
106 		__bch2_check_set_feature(c, feat);
107 }
108 
109 bool bch2_check_version_downgrade(struct bch_fs *);
110 void bch2_sb_upgrade(struct bch_fs *, unsigned, bool);
111 
112 void __bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
113 			     struct bch_sb_field *);
114 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
115 			   struct bch_sb_field *);
116 void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *);
117 void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned);
118 
119 #endif /* _BCACHEFS_SUPER_IO_H */
120