]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to 1e3ca87f7b bcachefs: bcachefs_metadata_version_major_minor
[bcachefs-tools-debian] / libbcachefs / super-io.h
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
10 #include <asm/byteorder.h>
11
12 static inline bool bch2_version_compatible(u16 version)
13 {
14         return BCH_VERSION_MAJOR(version) <= BCH_VERSION_MAJOR(bcachefs_metadata_version_current) &&
15                 version >= bcachefs_metadata_version_min;
16 }
17
18 void bch2_version_to_text(struct printbuf *, unsigned);
19 unsigned bch2_latest_compatible_version(unsigned);
20
21 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
22 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *,
23                                           enum bch_sb_field_type, unsigned);
24 void bch2_sb_field_delete(struct bch_sb_handle *, enum bch_sb_field_type);
25
26 #define field_to_type(_f, _name)                                        \
27         container_of_or_null(_f, struct bch_sb_field_##_name, field)
28
29 #define x(_name, _nr)                                                   \
30 static inline struct bch_sb_field_##_name *                             \
31 bch2_sb_get_##_name(struct bch_sb *sb)                                  \
32 {                                                                       \
33         return field_to_type(bch2_sb_field_get(sb,                      \
34                                 BCH_SB_FIELD_##_name), _name);          \
35 }                                                                       \
36                                                                         \
37 static inline struct bch_sb_field_##_name *                             \
38 bch2_sb_resize_##_name(struct bch_sb_handle *sb, unsigned u64s) \
39 {                                                                       \
40         return field_to_type(bch2_sb_field_resize(sb,                   \
41                                 BCH_SB_FIELD_##_name, u64s), _name);    \
42 }
43
44 BCH_SB_FIELDS()
45 #undef x
46
47 extern const char * const bch2_sb_fields[];
48
49 struct bch_sb_field_ops {
50         int     (*validate)(struct bch_sb *, struct bch_sb_field *, struct printbuf *);
51         void    (*to_text)(struct printbuf *, struct bch_sb *, struct bch_sb_field *);
52 };
53
54 static inline __le64 bch2_sb_magic(struct bch_fs *c)
55 {
56         __le64 ret;
57         memcpy(&ret, &c->sb.uuid, sizeof(ret));
58         return ret;
59 }
60
61 static inline __u64 jset_magic(struct bch_fs *c)
62 {
63         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
64 }
65
66 static inline __u64 bset_magic(struct bch_fs *c)
67 {
68         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
69 }
70
71 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
72 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
73
74 void bch2_free_super(struct bch_sb_handle *);
75 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
76
77 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
78 int bch2_write_super(struct bch_fs *);
79 void __bch2_check_set_feature(struct bch_fs *, unsigned);
80
81 static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat)
82 {
83         if (!(c->sb.features & (1ULL << feat)))
84                 __bch2_check_set_feature(c, feat);
85 }
86
87 /* BCH_SB_FIELD_members: */
88
89 static inline bool bch2_member_exists(struct bch_member *m)
90 {
91         return !bch2_is_zero(&m->uuid, sizeof(m->uuid));
92 }
93
94 static inline bool bch2_dev_exists(struct bch_sb *sb,
95                                    struct bch_sb_field_members *mi,
96                                    unsigned dev)
97 {
98         return dev < sb->nr_devices &&
99                 bch2_member_exists(&mi->members[dev]);
100 }
101
102 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
103 {
104         return (struct bch_member_cpu) {
105                 .nbuckets       = le64_to_cpu(mi->nbuckets),
106                 .first_bucket   = le16_to_cpu(mi->first_bucket),
107                 .bucket_size    = le16_to_cpu(mi->bucket_size),
108                 .group          = BCH_MEMBER_GROUP(mi),
109                 .state          = BCH_MEMBER_STATE(mi),
110                 .discard        = BCH_MEMBER_DISCARD(mi),
111                 .data_allowed   = BCH_MEMBER_DATA_ALLOWED(mi),
112                 .durability     = BCH_MEMBER_DURABILITY(mi)
113                         ? BCH_MEMBER_DURABILITY(mi) - 1
114                         : 1,
115                 .freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
116                 .valid          = bch2_member_exists(mi),
117         };
118 }
119
120 /* BCH_SB_FIELD_clean: */
121
122 void bch2_journal_super_entries_add_common(struct bch_fs *,
123                                            struct jset_entry **, u64);
124
125 int bch2_sb_clean_validate_late(struct bch_fs *, struct bch_sb_field_clean *, int);
126
127 int bch2_fs_mark_dirty(struct bch_fs *);
128 void bch2_fs_mark_clean(struct bch_fs *);
129
130 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
131                            struct bch_sb_field *);
132 void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *);
133 void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned);
134
135 #endif /* _BCACHEFS_SUPER_IO_H */