]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/replicas.h
2ffafad7c6314b37ba26693005865d3951233c5e
[bcachefs-tools-debian] / libbcachefs / replicas.h
1 #ifndef _BCACHEFS_REPLICAS_H
2 #define _BCACHEFS_REPLICAS_H
3
4 #include "eytzinger.h"
5 #include "replicas_types.h"
6
7 void bch2_replicas_entry_to_text(struct printbuf *,
8                                  struct bch_replicas_entry *);
9 void bch2_cpu_replicas_to_text(struct printbuf *, struct bch_replicas_cpu *);
10
11 static inline struct bch_replicas_entry *
12 cpu_replicas_entry(struct bch_replicas_cpu *r, unsigned i)
13 {
14         return (void *) r->entries + r->entry_size * i;
15 }
16
17 int bch2_replicas_entry_idx(struct bch_fs *,
18                             struct bch_replicas_entry *);
19
20 void bch2_devlist_to_replicas(struct bch_replicas_entry *,
21                               enum bch_data_type,
22                               struct bch_devs_list);
23 bool bch2_replicas_marked(struct bch_fs *,
24                           struct bch_replicas_entry *, bool);
25 int bch2_mark_replicas(struct bch_fs *,
26                        struct bch_replicas_entry *);
27
28 bool bch2_bkey_replicas_marked_locked(struct bch_fs *,
29                                       struct bkey_s_c, bool);
30 void bch2_bkey_to_replicas(struct bch_replicas_entry *, struct bkey_s_c);
31 bool bch2_bkey_replicas_marked(struct bch_fs *,
32                                struct bkey_s_c, bool);
33 int bch2_mark_bkey_replicas(struct bch_fs *, struct bkey_s_c);
34
35 static inline void bch2_replicas_entry_cached(struct bch_replicas_entry *e,
36                                               unsigned dev)
37 {
38         e->data_type    = BCH_DATA_CACHED;
39         e->nr_devs      = 1;
40         e->nr_required  = 1;
41         e->devs[0]      = dev;
42 }
43
44 struct replicas_status {
45         struct {
46                 int             redundancy;
47                 unsigned        nr_offline;
48         }                       replicas[BCH_DATA_NR];
49 };
50
51 struct replicas_status __bch2_replicas_status(struct bch_fs *,
52                                               struct bch_devs_mask);
53 struct replicas_status bch2_replicas_status(struct bch_fs *);
54 bool bch2_have_enough_devs(struct replicas_status, unsigned);
55
56 int bch2_replicas_online(struct bch_fs *, bool);
57 unsigned bch2_dev_has_data(struct bch_fs *, struct bch_dev *);
58
59 int bch2_replicas_gc_end(struct bch_fs *, int);
60 int bch2_replicas_gc_start(struct bch_fs *, unsigned);
61
62 int bch2_replicas_set_usage(struct bch_fs *,
63                             struct bch_replicas_entry *,
64                             u64);
65
66 #define for_each_cpu_replicas_entry(_r, _i)                             \
67         for (_i = (_r)->entries;                                        \
68              (void *) (_i) < (void *) (_r)->entries + (_r)->nr * (_r)->entry_size;\
69              _i = (void *) (_i) + (_r)->entry_size)
70
71 /* iterate over superblock replicas - used by userspace tools: */
72
73 #define replicas_entry_bytes(_i)                                        \
74         (offsetof(typeof(*(_i)), devs) + (_i)->nr_devs)
75
76 #define replicas_entry_next(_i)                                         \
77         ((typeof(_i)) ((void *) (_i) + replicas_entry_bytes(_i)))
78
79 #define for_each_replicas_entry(_r, _i)                                 \
80         for (_i = (_r)->entries;                                        \
81              (void *) (_i) < vstruct_end(&(_r)->field) && (_i)->data_type;\
82              (_i) = replicas_entry_next(_i))
83
84 #define for_each_replicas_entry_v0(_r, _i)                              \
85         for (_i = (_r)->entries;                                        \
86              (void *) (_i) < vstruct_end(&(_r)->field) && (_i)->data_type;\
87              (_i) = replicas_entry_next(_i))
88
89 int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *);
90
91 extern const struct bch_sb_field_ops bch_sb_field_ops_replicas;
92 extern const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0;
93
94 int bch2_fs_replicas_init(struct bch_fs *);
95
96 #endif /* _BCACHEFS_REPLICAS_H */