]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/super-io.h
Rename from bcache-tools to bcachefs-tools
[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 "super_types.h"
6
7 #include <asm/byteorder.h>
8
9 struct bch_sb_field *bch2_sb_field_get(struct bch_sb *, enum bch_sb_field_type);
10 struct bch_sb_field *bch2_sb_field_resize(struct bcache_superblock *,
11                                          enum bch_sb_field_type, unsigned);
12 struct bch_sb_field *bch2_fs_sb_field_resize(struct bch_fs *,
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 BCH_SB_FIELD_TYPE(_name)                                        \
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 bcache_superblock *sb, unsigned u64s)     \
28 {                                                                       \
29         return field_to_type(bch2_sb_field_resize(sb,                   \
30                                 BCH_SB_FIELD_##_name, u64s), _name);    \
31 }                                                                       \
32                                                                         \
33 static inline struct bch_sb_field_##_name *                             \
34 bch2_fs_sb_resize_##_name(struct bch_fs *c, unsigned u64s)              \
35 {                                                                       \
36         return field_to_type(bch2_fs_sb_field_resize(c,                 \
37                                 BCH_SB_FIELD_##_name, u64s), _name);    \
38 }
39
40 BCH_SB_FIELD_TYPE(journal);
41 BCH_SB_FIELD_TYPE(members);
42 BCH_SB_FIELD_TYPE(crypt);
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 pset_magic(struct bch_fs *c)
77 {
78         return __le64_to_cpu(bch2_sb_magic(c) ^ PSET_MAGIC);
79 }
80
81 static inline __u64 bset_magic(struct bch_fs *c)
82 {
83         return __le64_to_cpu(bch2_sb_magic(c) ^ BSET_MAGIC);
84 }
85
86 static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
87 {
88         return (struct bch_member_cpu) {
89                 .nbuckets       = le64_to_cpu(mi->nbuckets),
90                 .first_bucket   = le16_to_cpu(mi->first_bucket),
91                 .bucket_size    = le16_to_cpu(mi->bucket_size),
92                 .state          = BCH_MEMBER_STATE(mi),
93                 .tier           = BCH_MEMBER_TIER(mi),
94                 .has_metadata   = BCH_MEMBER_HAS_METADATA(mi),
95                 .has_data       = BCH_MEMBER_HAS_DATA(mi),
96                 .replacement    = BCH_MEMBER_REPLACEMENT(mi),
97                 .discard        = BCH_MEMBER_DISCARD(mi),
98                 .valid          = !bch2_is_zero(mi->uuid.b, sizeof(uuid_le)),
99         };
100 }
101
102 int bch2_sb_to_fs(struct bch_fs *, struct bch_sb *);
103 int bch2_sb_from_fs(struct bch_fs *, struct bch_dev *);
104
105 void bch2_free_super(struct bcache_superblock *);
106 int bch2_super_realloc(struct bcache_superblock *, unsigned);
107
108 const char *bch2_validate_journal_layout(struct bch_sb *,
109                                          struct bch_member_cpu);
110 const char *bch2_validate_cache_super(struct bcache_superblock *);
111
112 const char *bch2_read_super(struct bcache_superblock *,
113                            struct bch_opts, const char *);
114 void bch2_write_super(struct bch_fs *);
115
116 void bch2_check_mark_super_slowpath(struct bch_fs *,
117                                     const struct bkey_i *, bool);
118
119 static inline bool bch2_check_super_marked(struct bch_fs *c,
120                                            const struct bkey_i *k, bool meta)
121 {
122         struct bkey_s_c_extent e = bkey_i_to_s_c_extent(k);
123         const struct bch_extent_ptr *ptr;
124         unsigned nr_replicas = 0;
125         bool ret = true;
126
127         extent_for_each_ptr(e, ptr) {
128                 struct bch_dev *ca = c->devs[ptr->dev];
129
130                 if (ptr->cached)
131                         continue;
132
133                 if (!(meta
134                       ? ca->mi.has_metadata
135                       : ca->mi.has_data)) {
136                         ret = false;
137                         break;
138                 }
139
140                 nr_replicas++;
141         }
142
143         if (nr_replicas <
144             (meta ? c->sb.meta_replicas_have : c->sb.data_replicas_have))
145                 ret = false;
146
147         return ret;
148 }
149
150 static inline void bch2_check_mark_super(struct bch_fs *c,
151                                          const struct bkey_i *k, bool meta)
152 {
153         if (bch2_check_super_marked(c, k, meta))
154                 return;
155
156         bch2_check_mark_super_slowpath(c, k, meta);
157 }
158
159 #endif /* _BCACHE_SUPER_IO_H */