]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to 400f275d46 bcachefs: Fix check_overlapping_extents()
[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         int     (*validate)(struct bch_sb *, struct bch_sb_field *, struct printbuf *);
42         void    (*to_text)(struct printbuf *, struct bch_sb *, struct bch_sb_field *);
43 };
44
45 static inline __le64 bch2_sb_magic(struct bch_fs *c)
46 {
47         __le64 ret;
48         memcpy(&ret, &c->sb.uuid, sizeof(ret));
49         return ret;
50 }
51
52 static inline __u64 jset_magic(struct bch_fs *c)
53 {
54         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
55 }
56
57 static inline __u64 bset_magic(struct bch_fs *c)
58 {
59         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
60 }
61
62 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
63 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
64
65 void bch2_free_super(struct bch_sb_handle *);
66 int bch2_sb_realloc(struct bch_sb_handle *, unsigned);
67
68 int bch2_read_super(const char *, struct bch_opts *, struct bch_sb_handle *);
69 int bch2_write_super(struct bch_fs *);
70 void __bch2_check_set_feature(struct bch_fs *, unsigned);
71
72 static inline void bch2_check_set_feature(struct bch_fs *c, unsigned feat)
73 {
74         if (!(c->sb.features & (1ULL << feat)))
75                 __bch2_check_set_feature(c, feat);
76 }
77
78 /* BCH_SB_FIELD_members: */
79
80 static inline bool bch2_member_exists(struct bch_member *m)
81 {
82         return !bch2_is_zero(m->uuid.b, sizeof(uuid_le));
83 }
84
85 static inline bool bch2_dev_exists(struct bch_sb *sb,
86                                    struct bch_sb_field_members *mi,
87                                    unsigned dev)
88 {
89         return dev < sb->nr_devices &&
90                 bch2_member_exists(&mi->members[dev]);
91 }
92
93 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
94 {
95         return (struct bch_member_cpu) {
96                 .nbuckets       = le64_to_cpu(mi->nbuckets),
97                 .first_bucket   = le16_to_cpu(mi->first_bucket),
98                 .bucket_size    = le16_to_cpu(mi->bucket_size),
99                 .group          = BCH_MEMBER_GROUP(mi),
100                 .state          = BCH_MEMBER_STATE(mi),
101                 .discard        = BCH_MEMBER_DISCARD(mi),
102                 .data_allowed   = BCH_MEMBER_DATA_ALLOWED(mi),
103                 .durability     = BCH_MEMBER_DURABILITY(mi)
104                         ? BCH_MEMBER_DURABILITY(mi) - 1
105                         : 1,
106                 .freespace_initialized = BCH_MEMBER_FREESPACE_INITIALIZED(mi),
107                 .valid          = !bch2_is_zero(mi->uuid.b, sizeof(uuid_le)),
108         };
109 }
110
111 /* BCH_SB_FIELD_clean: */
112
113 void bch2_journal_super_entries_add_common(struct bch_fs *,
114                                            struct jset_entry **, u64);
115
116 int bch2_sb_clean_validate_late(struct bch_fs *, struct bch_sb_field_clean *, int);
117
118 int bch2_fs_mark_dirty(struct bch_fs *);
119 void bch2_fs_mark_clean(struct bch_fs *);
120
121 void bch2_sb_field_to_text(struct printbuf *, struct bch_sb *,
122                            struct bch_sb_field *);
123 void bch2_sb_layout_to_text(struct printbuf *, struct bch_sb_layout *);
124 void bch2_sb_to_text(struct printbuf *, struct bch_sb *, bool, unsigned);
125
126 #endif /* _BCACHEFS_SUPER_IO_H */