xref: /linux/fs/bcachefs/super-io.h (revision 031fba65fc202abf1f193e321be7a2c274fd88ba)
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 static inline bool bch2_version_compatible(u16 version)
14 {
15 	return BCH_VERSION_MAJOR(version) <= BCH_VERSION_MAJOR(bcachefs_metadata_version_current) &&
16 		version >= bcachefs_metadata_version_min;
17 }
18 
19 void bch2_version_to_text(struct printbuf *, unsigned);
20 unsigned bch2_latest_compatible_version(unsigned);
21 
22 u64 bch2_upgrade_recovery_passes(struct bch_fs *c,
23 				 unsigned,
24 				 unsigned);
25 
26 #define field_to_type(_f, _name)					\
27 	container_of_or_null(_f, struct bch_sb_field_##_name, field)
28 
29 struct bch_sb_field *bch2_sb_field_get_id(struct bch_sb *, enum bch_sb_field_type);
30 #define bch2_sb_field_get(_sb, _name)					\
31 	field_to_type(bch2_sb_field_get_id(_sb, BCH_SB_FIELD_##_name), _name)
32 
33 struct bch_sb_field *bch2_sb_field_resize_id(struct bch_sb_handle *,
34 					     enum bch_sb_field_type, unsigned);
35 #define bch2_sb_field_resize(_sb, _name, _u64s)				\
36 	field_to_type(bch2_sb_field_resize_id(_sb, BCH_SB_FIELD_##_name, _u64s), _name)
37 
38 void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type);
39 
40 extern const char * const bch2_sb_fields[];
41 
42 struct bch_sb_field_ops {
43 	int	(*validate)(struct bch_sb *, struct bch_sb_field *, struct printbuf *);
44 	void	(*to_text)(struct printbuf *, struct bch_sb *, struct bch_sb_field *);
45 };
46 
47 static inline __le64 bch2_sb_magic(struct bch_fs *c)
48 {
49 	__le64 ret;
50 
51 	memcpy(&ret, &c->sb.uuid, sizeof(ret));
52 	return ret;
53 }
54 
55 static inline __u64 jset_magic(struct bch_fs *c)
56 {
57 	return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
58 }
59 
60 static inline __u64 bset_magic(struct bch_fs *c)
61 {
62 	return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
63 }
64 
65 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
66 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
67 
68 void bch2_free_super(struct bch_sb_handle *);
69 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
70 
71 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
72 int bch2_write_super(struct bch_fs *);
73 void __bch2_check_set_feature(struct bch_fs *, unsigned);
74 
75 static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat)
76 {
77 	if (!(c->sb.features & (1ULL << feat)))
78 		__bch2_check_set_feature(c, feat);
79 }
80 
81 /* BCH_SB_FIELD_members_v1: */
82 
83 static inline bool bch2_member_exists(struct bch_member *m)
84 {
85 	return !bch2_is_zero(&m->uuid, sizeof(m->uuid));
86 }
87 
88 static inline bool bch2_dev_exists(struct bch_sb *sb,
89 				   unsigned dev)
90 {
91 	if (dev < sb->nr_devices) {
92 	struct bch_member m = bch2_sb_member_get(sb, dev);
93 		return bch2_member_exists(&m);
94 	}
95 	return false;
96 }
97 
98 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
99 {
100 	return (struct bch_member_cpu) {
101 		.nbuckets	= le64_to_cpu(mi->nbuckets),
102 		.first_bucket	= le16_to_cpu(mi->first_bucket),
103 		.bucket_size	= le16_to_cpu(mi->bucket_size),
104 		.group		= BCH_MEMBER_GROUP(mi),
105 		.state		= BCH_MEMBER_STATE(mi),
106 		.discard	= BCH_MEMBER_DISCARD(mi),
107 		.data_allowed	= BCH_MEMBER_DATA_ALLOWED(mi),
108 		.durability	= BCH_MEMBER_DURABILITY(mi)
109 			? BCH_MEMBER_DURABILITY(mi) - 1
110 			: 1,
111 		.freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
112 		.valid		= bch2_member_exists(mi),
113 	};
114 }
115 
116 void bch2_sb_maybe_downgrade(struct bch_fs *);
117 void bch2_sb_upgrade(struct bch_fs *, unsigned);
118 
119 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
120 			   struct bch_sb_field *);
121 void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *);
122 void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned);
123 
124 #endif /* _BCACHEFS_SUPER_IO_H */
125