]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Update bcachefs sources to 14e9ac5016 bcachefs: btree_iter fastpath
[bcachefs-tools-debian] / libbcachefs / super-io.h
1 #ifndef _BCACHE_SUPER_IO_H
2 #define _BCACHE_SUPER_IO_H
3
4 #include "extents.h"
5 #include "eytzinger.h"
6 #include "super_types.h"
7
8 #include <asm/byteorder.h>
9
10 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
11 struct bch_sb_field *bch2_sb_field_resize(struct bcache_superblock *,
12                                          enum bch_sb_field_type, unsigned);
13 struct bch_sb_field *bch2_fs_sb_field_resize(struct bch_fs *,
14                                          enum bch_sb_field_type, unsigned);
15
16 #define field_to_type(_f, _name)                                        \
17         container_of_or_null(_f, struct bch_sb_field_##_name, field)
18
19 #define BCH_SB_FIELD_TYPE(_name)                                        \
20 static inline struct bch_sb_field_##_name *                             \
21 bch2_sb_get_##_name(struct bch_sb *sb)                                  \
22 {                                                                       \
23         return field_to_type(bch2_sb_field_get(sb,                      \
24                                 BCH_SB_FIELD_##_name), _name);          \
25 }                                                                       \
26                                                                         \
27 static inline struct bch_sb_field_##_name *                             \
28 bch2_sb_resize_##_name(struct bcache_superblock *sb, unsigned u64s)     \
29 {                                                                       \
30         return field_to_type(bch2_sb_field_resize(sb,                   \
31                                 BCH_SB_FIELD_##_name, u64s), _name);    \
32 }                                                                       \
33                                                                         \
34 static inline struct bch_sb_field_##_name *                             \
35 bch2_fs_sb_resize_##_name(struct bch_fs *c, unsigned u64s)              \
36 {                                                                       \
37         return field_to_type(bch2_fs_sb_field_resize(c,                 \
38                                 BCH_SB_FIELD_##_name, u64s), _name);    \
39 }
40
41 BCH_SB_FIELD_TYPE(journal);
42 BCH_SB_FIELD_TYPE(members);
43 BCH_SB_FIELD_TYPE(crypt);
44 BCH_SB_FIELD_TYPE(replicas);
45
46 static inline bool bch2_dev_exists(struct bch_sb *sb,
47                                    struct bch_sb_field_members *mi,
48                                    unsigned dev)
49 {
50         return dev < sb->nr_devices &&
51                 !bch2_is_zero(mi->members[dev].uuid.b, sizeof(uuid_le));
52 }
53
54 static inline bool bch2_sb_test_feature(struct bch_sb *sb,
55                                         enum bch_sb_features f)
56 {
57         unsigned w = f / 64;
58         unsigned b = f % 64;
59
60         return le64_to_cpu(sb->features[w]) & (1ULL << b);
61 }
62
63 static inline void bch2_sb_set_feature(struct bch_sb *sb,
64                                        enum bch_sb_features f)
65 {
66         if (!bch2_sb_test_feature(sb, f)) {
67                 unsigned w = f / 64;
68                 unsigned b = f % 64;
69
70                 le64_add_cpu(&sb->features[w], 1ULL << b);
71         }
72 }
73
74 static inline __le64 bch2_sb_magic(struct bch_fs *c)
75 {
76         __le64 ret;
77         memcpy(&ret, &c->sb.uuid, sizeof(ret));
78         return ret;
79 }
80
81 static inline __u64 jset_magic(struct bch_fs *c)
82 {
83         return __le64_to_cpu(bch2_sb_magic(c) ^ JSET_MAGIC);
84 }
85
86 static inline __u64 pset_magic(struct bch_fs *c)
87 {
88         return __le64_to_cpu(bch2_sb_magic(c) ^ PSET_MAGIC);
89 }
90
91 static inline __u64 bset_magic(struct bch_fs *c)
92 {
93         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
94 }
95
96 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
97 {
98         return (struct bch_member_cpu) {
99                 .nbuckets       = le64_to_cpu(mi->nbuckets),
100                 .first_bucket   = le16_to_cpu(mi->first_bucket),
101                 .bucket_size    = le16_to_cpu(mi->bucket_size),
102                 .state          = BCH_MEMBER_STATE(mi),
103                 .tier           = BCH_MEMBER_TIER(mi),
104                 .replacement    = BCH_MEMBER_REPLACEMENT(mi),
105                 .discard        = BCH_MEMBER_DISCARD(mi),
106                 .valid          = !bch2_is_zero(mi->uuid.b, sizeof(uuid_le)),
107         };
108 }
109
110 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
111 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
112
113 void bch2_free_super(struct bcache_superblock *);
114 int bch2_super_realloc(struct bcache_superblock *, unsigned);
115
116 const char *bch2_sb_validate_journal(struct bch_sb *,
117                                          struct bch_member_cpu);
118 const char *bch2_sb_validate(struct bcache_superblock *);
119
120 const char *bch2_read_super(struct bcache_superblock *,
121                            struct bch_opts, const char *);
122 void bch2_write_super(struct bch_fs *);
123
124 bool bch2_sb_has_replicas(struct bch_fs *, struct bkey_s_c_extent,
125                           enum bch_data_types);
126 int bch2_check_mark_super(struct bch_fs *, struct bkey_s_c_extent,
127                           enum bch_data_types);
128
129 struct replicas_status {
130         struct {
131                 unsigned        nr_online;
132                 unsigned        nr_offline;
133         }                       replicas[BCH_DATA_NR];
134 };
135
136 struct replicas_status __bch2_replicas_status(struct bch_fs *,
137                                               struct bch_dev *);
138 struct replicas_status bch2_replicas_status(struct bch_fs *);
139
140 unsigned bch2_replicas_online(struct bch_fs *, bool);
141 unsigned bch2_dev_has_data(struct bch_fs *, struct bch_dev *);
142
143 int bch2_replicas_gc_end(struct bch_fs *, int);
144 int bch2_replicas_gc_start(struct bch_fs *, unsigned);
145
146 #endif /* _BCACHE_SUPER_IO_H */