]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.h
Update bcachefs sources to e82e656279 bcachefs: Cleanups for building in userspace
[bcachefs-tools-debian] / libbcachefs / buckets.h
1 /*
2  * Code for manipulating bucket marks for garbage collection.
3  *
4  * Copyright 2014 Datera, Inc.
5  */
6
7 #ifndef _BUCKETS_H
8 #define _BUCKETS_H
9
10 #include "buckets_types.h"
11 #include "super.h"
12
13 #define for_each_bucket(b, ca)                                  \
14         for (b = (ca)->buckets + (ca)->mi.first_bucket;         \
15              b < (ca)->buckets + (ca)->mi.nbuckets; b++)
16
17 #define bucket_cmpxchg(g, new, expr)                            \
18 ({                                                              \
19         u64 _v = READ_ONCE((g)->_mark.counter);                 \
20         struct bucket_mark _old;                                \
21                                                                 \
22         do {                                                    \
23                 (new).counter = _old.counter = _v;              \
24                 expr;                                           \
25         } while ((_v = cmpxchg(&(g)->_mark.counter,             \
26                                _old.counter,                    \
27                                (new).counter)) != _old.counter);\
28         _old;                                                   \
29 })
30
31 /*
32  * bucket_gc_gen() returns the difference between the bucket's current gen and
33  * the oldest gen of any pointer into that bucket in the btree.
34  */
35
36 static inline u8 bucket_gc_gen(struct bch_dev *ca, struct bucket *g)
37 {
38         unsigned long r = g - ca->buckets;
39         return g->mark.gen - ca->oldest_gens[r];
40 }
41
42 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
43                                    const struct bch_extent_ptr *ptr)
44 {
45         return sector_to_bucket(ca, ptr->offset);
46 }
47
48 static inline struct bucket *PTR_BUCKET(const struct bch_dev *ca,
49                                         const struct bch_extent_ptr *ptr)
50 {
51         return ca->buckets + PTR_BUCKET_NR(ca, ptr);
52 }
53
54 static inline int gen_cmp(u8 a, u8 b)
55 {
56         return (s8) (a - b);
57 }
58
59 static inline int gen_after(u8 a, u8 b)
60 {
61         int r = gen_cmp(a, b);
62
63         return r > 0 ? r : 0;
64 }
65
66 /**
67  * ptr_stale() - check if a pointer points into a bucket that has been
68  * invalidated.
69  */
70 static inline u8 ptr_stale(const struct bch_dev *ca,
71                            const struct bch_extent_ptr *ptr)
72 {
73         return gen_after(PTR_BUCKET(ca, ptr)->mark.gen, ptr->gen);
74 }
75
76 /* bucket gc marks */
77
78 /* The dirty and cached sector counts saturate. If this occurs,
79  * reference counting alone will not free the bucket, and a btree
80  * GC must be performed. */
81 #define GC_MAX_SECTORS_USED ((1U << 15) - 1)
82
83 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
84 {
85         return mark.dirty_sectors + mark.cached_sectors;
86 }
87
88 static inline bool bucket_unused(struct bucket_mark mark)
89 {
90         return !mark.owned_by_allocator &&
91                 !mark.data_type &&
92                 !bucket_sectors_used(mark);
93 }
94
95 /* Per device stats: */
96
97 struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *);
98 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
99
100 static inline u64 __dev_buckets_available(struct bch_dev *ca,
101                                           struct bch_dev_usage stats)
102 {
103         return max_t(s64, 0,
104                      ca->mi.nbuckets - ca->mi.first_bucket -
105                      stats.buckets[S_META] -
106                      stats.buckets[S_DIRTY] -
107                      stats.buckets_alloc);
108 }
109
110 /*
111  * Number of reclaimable buckets - only for use by the allocator thread:
112  */
113 static inline u64 dev_buckets_available(struct bch_dev *ca)
114 {
115         return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
116 }
117
118 static inline u64 __dev_buckets_free(struct bch_dev *ca,
119                                        struct bch_dev_usage stats)
120 {
121         return __dev_buckets_available(ca, stats) +
122                 fifo_used(&ca->free[RESERVE_NONE]) +
123                 fifo_used(&ca->free_inc);
124 }
125
126 static inline u64 dev_buckets_free(struct bch_dev *ca)
127 {
128         return __dev_buckets_free(ca, bch2_dev_usage_read(ca));
129 }
130
131 /* Cache set stats: */
132
133 struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *);
134 struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *);
135 void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
136                         struct disk_reservation *, struct gc_pos);
137
138 struct fs_usage_sum {
139         u64     data;
140         u64     reserved;
141 };
142
143 static inline struct fs_usage_sum __fs_usage_sum(struct bch_fs_usage stats)
144 {
145         struct fs_usage_sum sum = { 0 };
146         unsigned i;
147
148         for (i = 0; i < BCH_REPLICAS_MAX; i++) {
149                 sum.data += (stats.s[i].data[S_META] +
150                              stats.s[i].data[S_DIRTY]) * (i + 1);
151                 sum.reserved += stats.s[i].persistent_reserved * (i + 1);
152         }
153
154         sum.reserved += stats.online_reserved;
155         return sum;
156 }
157
158 static inline u64 __bch2_fs_sectors_used(struct bch_fs *c)
159 {
160         struct fs_usage_sum sum = __fs_usage_sum(__bch2_fs_usage_read(c));
161
162         return sum.data + sum.reserved + (sum.reserved >> 7);
163 }
164
165 static inline u64 bch2_fs_sectors_used(struct bch_fs *c)
166 {
167         return min(c->capacity, __bch2_fs_sectors_used(c));
168 }
169
170 static inline bool is_available_bucket(struct bucket_mark mark)
171 {
172         return (!mark.owned_by_allocator &&
173                 mark.data_type == BUCKET_DATA &&
174                 !mark.dirty_sectors &&
175                 !mark.nouse);
176 }
177
178 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
179                                                u16 last_seq_ondisk)
180 {
181         return m.journal_seq_valid &&
182                 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
183 }
184
185 void bch2_bucket_seq_cleanup(struct bch_fs *);
186
187 bool bch2_invalidate_bucket(struct bch_dev *, struct bucket *,
188                             struct bucket_mark *);
189 bool bch2_mark_alloc_bucket_startup(struct bch_dev *, struct bucket *);
190 void bch2_mark_free_bucket(struct bch_dev *, struct bucket *);
191 void bch2_mark_alloc_bucket(struct bch_dev *, struct bucket *, bool);
192 void bch2_mark_metadata_bucket(struct bch_dev *, struct bucket *,
193                                enum bucket_data_type, bool);
194
195 #define BCH_BUCKET_MARK_NOATOMIC                (1 << 0)
196 #define BCH_BUCKET_MARK_GC_WILL_VISIT           (1 << 1)
197 #define BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE    (1 << 2)
198
199 void __bch2_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool,
200                      struct bch_fs_usage *, u64, unsigned);
201
202 void bch2_gc_mark_key(struct bch_fs *, struct bkey_s_c,
203                       s64, bool, unsigned);
204 void bch2_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool,
205                   struct gc_pos, struct bch_fs_usage *, u64);
206
207 void bch2_recalc_sectors_available(struct bch_fs *);
208
209 void bch2_disk_reservation_put(struct bch_fs *,
210                               struct disk_reservation *);
211
212 #define BCH_DISK_RESERVATION_NOFAIL             (1 << 0)
213 #define BCH_DISK_RESERVATION_METADATA           (1 << 1)
214 #define BCH_DISK_RESERVATION_GC_LOCK_HELD       (1 << 2)
215 #define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD   (1 << 3)
216
217 int bch2_disk_reservation_add(struct bch_fs *,
218                              struct disk_reservation *,
219                              unsigned, int);
220 int bch2_disk_reservation_get(struct bch_fs *,
221                              struct disk_reservation *,
222                              unsigned, int);
223
224 #endif /* _BUCKETS_H */