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