]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/buckets.h
Update bcachefs sources to 9ceb982d77 bcachefs: Store bucket gens in a btree
[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 /*
49  * Returns 0 if no pointers or device offline - only for tracepoints!
50  */
51 static inline size_t PTR_BUCKET_NR_TRACE(const struct bch_fs *c,
52                                          const struct bkey_i *k,
53                                          unsigned ptr)
54 {
55         size_t bucket = 0;
56 #if 0
57         if (bkey_extent_is_data(&k->k)) {
58                 const struct bch_extent_ptr *ptr;
59
60                 extent_for_each_ptr(bkey_i_to_s_c_extent(k), ptr) {
61                         const struct bch_dev *ca = c->devs[ptr->dev];
62                         bucket = PTR_BUCKET_NR(ca, ptr);
63                         break;
64                 }
65         }
66 #endif
67         return bucket;
68 }
69
70 static inline struct bucket *PTR_BUCKET(const struct bch_dev *ca,
71                                         const struct bch_extent_ptr *ptr)
72 {
73         return ca->buckets + PTR_BUCKET_NR(ca, ptr);
74 }
75
76 static inline int gen_cmp(u8 a, u8 b)
77 {
78         return (s8) (a - b);
79 }
80
81 static inline int gen_after(u8 a, u8 b)
82 {
83         int r = gen_cmp(a, b);
84
85         return r > 0 ? r : 0;
86 }
87
88 /**
89  * ptr_stale() - check if a pointer points into a bucket that has been
90  * invalidated.
91  */
92 static inline u8 ptr_stale(const struct bch_dev *ca,
93                            const struct bch_extent_ptr *ptr)
94 {
95         return gen_after(PTR_BUCKET(ca, ptr)->mark.gen, ptr->gen);
96 }
97
98 /* bucket gc marks */
99
100 /* The dirty and cached sector counts saturate. If this occurs,
101  * reference counting alone will not free the bucket, and a btree
102  * GC must be performed. */
103 #define GC_MAX_SECTORS_USED ((1U << 15) - 1)
104
105 static inline unsigned bucket_sectors_used(struct bucket_mark mark)
106 {
107         return mark.dirty_sectors + mark.cached_sectors;
108 }
109
110 static inline bool bucket_unused(struct bucket_mark mark)
111 {
112         return !mark.owned_by_allocator &&
113                 !mark.data_type &&
114                 !bucket_sectors_used(mark);
115 }
116
117 /* Per device stats: */
118
119 struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *);
120 struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *);
121
122 static inline u64 __dev_buckets_available(struct bch_dev *ca,
123                                           struct bch_dev_usage stats)
124 {
125         return max_t(s64, 0,
126                      ca->mi.nbuckets - ca->mi.first_bucket -
127                      stats.buckets_dirty -
128                      stats.buckets_alloc -
129                      stats.buckets_meta);
130 }
131
132 /*
133  * Number of reclaimable buckets - only for use by the allocator thread:
134  */
135 static inline u64 dev_buckets_available(struct bch_dev *ca)
136 {
137         return __dev_buckets_available(ca, bch2_dev_usage_read(ca));
138 }
139
140 static inline u64 __dev_buckets_free(struct bch_dev *ca,
141                                        struct bch_dev_usage stats)
142 {
143         return __dev_buckets_available(ca, stats) +
144                 fifo_used(&ca->free[RESERVE_NONE]) +
145                 fifo_used(&ca->free_inc);
146 }
147
148 static inline u64 dev_buckets_free(struct bch_dev *ca)
149 {
150         return __dev_buckets_free(ca, bch2_dev_usage_read(ca));
151 }
152
153 /* Cache set stats: */
154
155 struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *);
156 struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *);
157 void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *,
158                         struct disk_reservation *, struct gc_pos);
159
160 static inline u64 __bch2_fs_sectors_used(struct bch_fs *c)
161 {
162         struct bch_fs_usage stats = __bch2_fs_usage_read(c);
163         u64 reserved = stats.persistent_reserved +
164                 stats.online_reserved;
165
166         return stats.s[S_COMPRESSED][S_META] +
167                 stats.s[S_COMPRESSED][S_DIRTY] +
168                 reserved +
169                 (reserved >> 7);
170 }
171
172 static inline u64 bch2_fs_sectors_used(struct bch_fs *c)
173 {
174         return min(c->capacity, __bch2_fs_sectors_used(c));
175 }
176
177 static inline bool is_available_bucket(struct bucket_mark mark)
178 {
179         return (!mark.owned_by_allocator &&
180                 mark.data_type == BUCKET_DATA &&
181                 !mark.dirty_sectors &&
182                 !mark.nouse);
183 }
184
185 static inline bool bucket_needs_journal_commit(struct bucket_mark m,
186                                                u16 last_seq_ondisk)
187 {
188         return m.journal_seq_valid &&
189                 ((s16) m.journal_seq - (s16) last_seq_ondisk > 0);
190 }
191
192 void bch2_bucket_seq_cleanup(struct bch_fs *);
193
194 bool bch2_invalidate_bucket(struct bch_dev *, struct bucket *,
195                             struct bucket_mark *);
196 bool bch2_mark_alloc_bucket_startup(struct bch_dev *, struct bucket *);
197 void bch2_mark_free_bucket(struct bch_dev *, struct bucket *);
198 void bch2_mark_alloc_bucket(struct bch_dev *, struct bucket *, bool);
199 void bch2_mark_metadata_bucket(struct bch_dev *, struct bucket *,
200                                enum bucket_data_type, bool);
201
202 void __bch2_gc_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool,
203                        struct bch_fs_usage *);
204 void bch2_gc_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool);
205 void bch2_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool,
206                   struct gc_pos, struct bch_fs_usage *, u64);
207
208 void bch2_recalc_sectors_available(struct bch_fs *);
209
210 void bch2_disk_reservation_put(struct bch_fs *,
211                               struct disk_reservation *);
212
213 #define BCH_DISK_RESERVATION_NOFAIL             (1 << 0)
214 #define BCH_DISK_RESERVATION_METADATA           (1 << 1)
215 #define BCH_DISK_RESERVATION_GC_LOCK_HELD       (1 << 2)
216 #define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD   (1 << 3)
217
218 int bch2_disk_reservation_add(struct bch_fs *,
219                              struct disk_reservation *,
220                              unsigned, int);
221 int bch2_disk_reservation_get(struct bch_fs *,
222                              struct disk_reservation *,
223                              unsigned, int);
224
225 #endif /* _BUCKETS_H */