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