]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to 2cb70a82bc bcachefs: delete some debug code
[bcachefs-tools-debian] / libbcachefs / super-io.h
1 #ifndef _BCACHEFS_SUPER_IO_H
2 #define _BCACHEFS_SUPER_IO_H
3
4 #include "extents.h"
5 #include "eytzinger.h"
6 #include "super_types.h"
7 #include "super.h"
8
9 #include <asm/byteorder.h>
10
11 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
12 struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *,
13                                           enum bch_sb_field_type, unsigned);
14
15 #define field_to_type(_f, _name)                                        \
16         container_of_or_null(_f, struct bch_sb_field_##_name, field)
17
18 #define x(_name, _nr)                                                   \
19 static inline struct bch_sb_field_##_name *                             \
20 bch2_sb_get_##_name(struct bch_sb *sb)                                  \
21 {                                                                       \
22         return field_to_type(bch2_sb_field_get(sb,                      \
23                                 BCH_SB_FIELD_##_name), _name);          \
24 }                                                                       \
25                                                                         \
26 static inline struct bch_sb_field_##_name *                             \
27 bch2_sb_resize_##_name(struct bch_sb_handle *sb, unsigned u64s) \
28 {                                                                       \
29         return field_to_type(bch2_sb_field_resize(sb,                   \
30                                 BCH_SB_FIELD_##_name, u64s), _name);    \
31 }
32
33 BCH_SB_FIELDS()
34 #undef x
35
36 extern const char * const bch2_sb_fields[];
37
38 struct bch_sb_field_ops {
39         const char *    (*validate)(struct bch_sb *, struct bch_sb_field *);
40         size_t          (*to_text)(char *, size_t, struct bch_sb *,
41                                    struct bch_sb_field *);
42 };
43
44 static inline bool bch2_sb_test_feature(struct bch_sb *sb,
45                                         enum bch_sb_features f)
46 {
47         unsigned w = f / 64;
48         unsigned b = f % 64;
49
50         return le64_to_cpu(sb->features[w]) & (1ULL << b);
51 }
52
53 static inline void bch2_sb_set_feature(struct bch_sb *sb,
54                                        enum bch_sb_features f)
55 {
56         if (!bch2_sb_test_feature(sb, f)) {
57                 unsigned w = f / 64;
58                 unsigned b = f % 64;
59
60                 le64_add_cpu(&sb->features[w], 1ULL << b);
61         }
62 }
63
64 static inline __le64 bch2_sb_magic(struct bch_fs *c)
65 {
66         __le64 ret;
67         memcpy(&ret, &c->sb.uuid, sizeof(ret));
68         return ret;
69 }
70
71 static inline __u64 jset_magic(struct bch_fs *c)
72 {
73         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
74 }
75
76 static inline __u64 bset_magic(struct bch_fs *c)
77 {
78         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
79 }
80
81 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
82 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
83
84 void bch2_free_super(struct bch_sb_handle *);
85 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
86
87 const char *bch2_sb_validate(struct bch_sb_handle *);
88
89 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
90 void bch2_write_super(struct bch_fs *);
91
92 /* BCH_SB_FIELD_journal: */
93
94 static inline unsigned bch2_nr_journal_buckets(struct bch_sb_field_journal *j)
95 {
96         return j
97                 ? (__le64 *) vstruct_end(&j->field) - j->buckets
98                 : 0;
99 }
100
101 /* BCH_SB_FIELD_members: */
102
103 static inline bool bch2_member_exists(struct bch_member *m)
104 {
105         return !bch2_is_zero(m->uuid.b, sizeof(uuid_le));
106 }
107
108 static inline bool bch2_dev_exists(struct bch_sb *sb,
109                                    struct bch_sb_field_members *mi,
110                                    unsigned dev)
111 {
112         return dev < sb->nr_devices &&
113                 bch2_member_exists(&mi->members[dev]);
114 }
115
116 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
117 {
118         return (struct bch_member_cpu) {
119                 .nbuckets       = le64_to_cpu(mi->nbuckets),
120                 .first_bucket   = le16_to_cpu(mi->first_bucket),
121                 .bucket_size    = le16_to_cpu(mi->bucket_size),
122                 .group          = BCH_MEMBER_GROUP(mi),
123                 .state          = BCH_MEMBER_STATE(mi),
124                 .replacement    = BCH_MEMBER_REPLACEMENT(mi),
125                 .discard        = BCH_MEMBER_DISCARD(mi),
126                 .data_allowed   = BCH_MEMBER_DATA_ALLOWED(mi),
127                 .durability     = BCH_MEMBER_DURABILITY(mi)
128                         ? BCH_MEMBER_DURABILITY(mi) - 1
129                         : 1,
130                 .valid          = !bch2_is_zero(mi->uuid.b, sizeof(uuid_le)),
131         };
132 }
133
134 /* BCH_SB_FIELD_clean: */
135
136 void bch2_fs_mark_clean(struct bch_fs *, bool);
137
138 size_t bch2_sb_field_to_text(char *, size_t, struct bch_sb *,
139                              struct bch_sb_field *);
140
141 #endif /* _BCACHEFS_SUPER_IO_H */