xref: /linux/fs/bcachefs/buckets.h (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Code for manipulating bucket marks for garbage collection.
4  *
5  * Copyright 2014 Datera, Inc.
6  */
7 
8 #ifndef _BUCKETS_H
9 #define _BUCKETS_H
10 
11 #include "buckets_types.h"
12 #include "extents.h"
13 #include "sb-members.h"
14 
15 static inline u64 sector_to_bucket(const struct bch_dev *ca, sector_t s)
16 {
17 	return div_u64(s, ca->mi.bucket_size);
18 }
19 
20 static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b)
21 {
22 	return ((sector_t) b) * ca->mi.bucket_size;
23 }
24 
25 static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s)
26 {
27 	u32 remainder;
28 
29 	div_u64_rem(s, ca->mi.bucket_size, &remainder);
30 	return remainder;
31 }
32 
33 static inline u64 sector_to_bucket_and_offset(const struct bch_dev *ca, sector_t s, u32 *offset)
34 {
35 	return div_u64_rem(s, ca->mi.bucket_size, offset);
36 }
37 
38 #define for_each_bucket(_b, _buckets)				\
39 	for (_b = (_buckets)->b + (_buckets)->first_bucket;	\
40 	     _b < (_buckets)->b + (_buckets)->nbuckets; _b++)
41 
42 /*
43  * Ugly hack alert:
44  *
45  * We need to cram a spinlock in a single byte, because that's what we have left
46  * in struct bucket, and we care about the size of these - during fsck, we need
47  * in memory state for every single bucket on every device.
48  *
49  * We used to do
50  *   while (xchg(&b->lock, 1) cpu_relax();
51  * but, it turns out not all architectures support xchg on a single byte.
52  *
53  * So now we use bit_spin_lock(), with fun games since we can't burn a whole
54  * ulong for this - we just need to make sure the lock bit always ends up in the
55  * first byte.
56  */
57 
58 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
59 #define BUCKET_LOCK_BITNR	0
60 #else
61 #define BUCKET_LOCK_BITNR	(BITS_PER_LONG - 1)
62 #endif
63 
64 union ulong_byte_assert {
65 	ulong	ulong;
66 	u8	byte;
67 };
68 
69 static inline void bucket_unlock(struct bucket *b)
70 {
71 	BUILD_BUG_ON(!((union ulong_byte_assert) { .ulong = 1UL << BUCKET_LOCK_BITNR }).byte);
72 
73 	clear_bit_unlock(BUCKET_LOCK_BITNR, (void *) &b->lock);
74 	wake_up_bit((void *) &b->lock, BUCKET_LOCK_BITNR);
75 }
76 
77 static inline void bucket_lock(struct bucket *b)
78 {
79 	wait_on_bit_lock((void *) &b->lock, BUCKET_LOCK_BITNR,
80 			 TASK_UNINTERRUPTIBLE);
81 }
82 
83 static inline struct bucket_array *gc_bucket_array(struct bch_dev *ca)
84 {
85 	return rcu_dereference_check(ca->buckets_gc,
86 				     !ca->fs ||
87 				     percpu_rwsem_is_held(&ca->fs->mark_lock) ||
88 				     lockdep_is_held(&ca->fs->gc_lock) ||
89 				     lockdep_is_held(&ca->bucket_lock));
90 }
91 
92 static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
93 {
94 	struct bucket_array *buckets = gc_bucket_array(ca);
95 
96 	BUG_ON(!bucket_valid(ca, b));
97 	return buckets->b + b;
98 }
99 
100 static inline struct bucket_gens *bucket_gens(struct bch_dev *ca)
101 {
102 	return rcu_dereference_check(ca->bucket_gens,
103 				     !ca->fs ||
104 				     percpu_rwsem_is_held(&ca->fs->mark_lock) ||
105 				     lockdep_is_held(&ca->fs->gc_lock) ||
106 				     lockdep_is_held(&ca->bucket_lock));
107 }
108 
109 static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
110 {
111 	struct bucket_gens *gens = bucket_gens(ca);
112 
113 	BUG_ON(!bucket_valid(ca, b));
114 	return gens->b + b;
115 }
116 
117 static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
118 				   const struct bch_extent_ptr *ptr)
119 {
120 	return sector_to_bucket(ca, ptr->offset);
121 }
122 
123 static inline struct bpos PTR_BUCKET_POS(const struct bch_dev *ca,
124 					 const struct bch_extent_ptr *ptr)
125 {
126 	return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
127 }
128 
129 static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_dev *ca,
130 						const struct bch_extent_ptr *ptr,
131 						u32 *bucket_offset)
132 {
133 	return POS(ptr->dev, sector_to_bucket_and_offset(ca, ptr->offset, bucket_offset));
134 }
135 
136 static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca,
137 					   const struct bch_extent_ptr *ptr)
138 {
139 	return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr));
140 }
141 
142 static inline enum bch_data_type ptr_data_type(const struct bkey *k,
143 					       const struct bch_extent_ptr *ptr)
144 {
145 	if (bkey_is_btree_ptr(k))
146 		return BCH_DATA_btree;
147 
148 	return ptr->cached ? BCH_DATA_cached : BCH_DATA_user;
149 }
150 
151 static inline s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
152 {
153 	EBUG_ON(sectors < 0);
154 
155 	return crc_is_compressed(p.crc)
156 		? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size,
157 				   p.crc.uncompressed_size)
158 		: sectors;
159 }
160 
161 static inline int gen_cmp(u8 a, u8 b)
162 {
163 	return (s8) (a - b);
164 }
165 
166 static inline int gen_after(u8 a, u8 b)
167 {
168 	int r = gen_cmp(a, b);
169 
170 	return r > 0 ? r : 0;
171 }
172 
173 static inline u8 dev_ptr_stale_rcu(struct bch_dev *ca, const struct bch_extent_ptr *ptr)
174 {
175 	return gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen);
176 }
177 
178 /**
179  * dev_ptr_stale() - check if a pointer points into a bucket that has been
180  * invalidated.
181  */
182 static inline u8 dev_ptr_stale(struct bch_dev *ca, const struct bch_extent_ptr *ptr)
183 {
184 	rcu_read_lock();
185 	u8 ret = dev_ptr_stale_rcu(ca, ptr);
186 	rcu_read_unlock();
187 
188 	return ret;
189 }
190 
191 /* Device usage: */
192 
193 void bch2_dev_usage_read_fast(struct bch_dev *, struct bch_dev_usage *);
194 static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
195 {
196 	struct bch_dev_usage ret;
197 
198 	bch2_dev_usage_read_fast(ca, &ret);
199 	return ret;
200 }
201 
202 void bch2_dev_usage_init(struct bch_dev *);
203 void bch2_dev_usage_to_text(struct printbuf *, struct bch_dev_usage *);
204 
205 static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum bch_watermark watermark)
206 {
207 	s64 reserved = 0;
208 
209 	switch (watermark) {
210 	case BCH_WATERMARK_NR:
211 		BUG();
212 	case BCH_WATERMARK_stripe:
213 		reserved += ca->mi.nbuckets >> 6;
214 		fallthrough;
215 	case BCH_WATERMARK_normal:
216 		reserved += ca->mi.nbuckets >> 6;
217 		fallthrough;
218 	case BCH_WATERMARK_copygc:
219 		reserved += ca->nr_btree_reserve;
220 		fallthrough;
221 	case BCH_WATERMARK_btree:
222 		reserved += ca->nr_btree_reserve;
223 		fallthrough;
224 	case BCH_WATERMARK_btree_copygc:
225 	case BCH_WATERMARK_reclaim:
226 	case BCH_WATERMARK_interior_updates:
227 		break;
228 	}
229 
230 	return reserved;
231 }
232 
233 static inline u64 dev_buckets_free(struct bch_dev *ca,
234 				   struct bch_dev_usage usage,
235 				   enum bch_watermark watermark)
236 {
237 	return max_t(s64, 0,
238 		     usage.d[BCH_DATA_free].buckets -
239 		     ca->nr_open_buckets -
240 		     bch2_dev_buckets_reserved(ca, watermark));
241 }
242 
243 static inline u64 __dev_buckets_available(struct bch_dev *ca,
244 					  struct bch_dev_usage usage,
245 					  enum bch_watermark watermark)
246 {
247 	return max_t(s64, 0,
248 		       usage.d[BCH_DATA_free].buckets
249 		     + usage.d[BCH_DATA_cached].buckets
250 		     + usage.d[BCH_DATA_need_gc_gens].buckets
251 		     + usage.d[BCH_DATA_need_discard].buckets
252 		     - ca->nr_open_buckets
253 		     - bch2_dev_buckets_reserved(ca, watermark));
254 }
255 
256 static inline u64 dev_buckets_available(struct bch_dev *ca,
257 					enum bch_watermark watermark)
258 {
259 	return __dev_buckets_available(ca, bch2_dev_usage_read(ca), watermark);
260 }
261 
262 /* Filesystem usage: */
263 
264 static inline unsigned __fs_usage_u64s(unsigned nr_replicas)
265 {
266 	return sizeof(struct bch_fs_usage) / sizeof(u64) + nr_replicas;
267 }
268 
269 static inline unsigned fs_usage_u64s(struct bch_fs *c)
270 {
271 	return __fs_usage_u64s(READ_ONCE(c->replicas.nr));
272 }
273 
274 static inline unsigned __fs_usage_online_u64s(unsigned nr_replicas)
275 {
276 	return sizeof(struct bch_fs_usage_online) / sizeof(u64) + nr_replicas;
277 }
278 
279 static inline unsigned fs_usage_online_u64s(struct bch_fs *c)
280 {
281 	return __fs_usage_online_u64s(READ_ONCE(c->replicas.nr));
282 }
283 
284 static inline unsigned dev_usage_u64s(void)
285 {
286 	return sizeof(struct bch_dev_usage) / sizeof(u64);
287 }
288 
289 u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *);
290 
291 struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *);
292 
293 void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned);
294 
295 void bch2_fs_usage_to_text(struct printbuf *,
296 			   struct bch_fs *, struct bch_fs_usage_online *);
297 
298 u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *);
299 
300 struct bch_fs_usage_short
301 bch2_fs_usage_read_short(struct bch_fs *);
302 
303 void bch2_dev_usage_update(struct bch_fs *, struct bch_dev *,
304 			   const struct bch_alloc_v4 *,
305 			   const struct bch_alloc_v4 *, u64, bool);
306 
307 /* key/bucket marking: */
308 
309 static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
310 						unsigned journal_seq,
311 						bool gc)
312 {
313 	percpu_rwsem_assert_held(&c->mark_lock);
314 	BUG_ON(!gc && !journal_seq);
315 
316 	return this_cpu_ptr(gc
317 			    ? c->usage_gc
318 			    : c->usage[journal_seq & JOURNAL_BUF_MASK]);
319 }
320 
321 int bch2_update_replicas(struct bch_fs *, struct bkey_s_c,
322 			 struct bch_replicas_entry_v1 *, s64,
323 			 unsigned, bool);
324 int bch2_update_replicas_list(struct btree_trans *,
325 			 struct bch_replicas_entry_v1 *, s64);
326 int bch2_update_cached_sectors_list(struct btree_trans *, unsigned, s64);
327 int bch2_replicas_deltas_realloc(struct btree_trans *, unsigned);
328 
329 void bch2_fs_usage_initialize(struct bch_fs *);
330 
331 int bch2_bucket_ref_update(struct btree_trans *, struct bch_dev *,
332 			   struct bkey_s_c, const struct bch_extent_ptr *,
333 			   s64, enum bch_data_type, u8, u8, u32 *);
334 
335 int bch2_check_fix_ptrs(struct btree_trans *,
336 			enum btree_id, unsigned, struct bkey_s_c,
337 			enum btree_iter_update_trigger_flags);
338 
339 int bch2_trigger_extent(struct btree_trans *, enum btree_id, unsigned,
340 			struct bkey_s_c, struct bkey_s,
341 			enum btree_iter_update_trigger_flags);
342 int bch2_trigger_reservation(struct btree_trans *, enum btree_id, unsigned,
343 			  struct bkey_s_c, struct bkey_s,
344 			  enum btree_iter_update_trigger_flags);
345 
346 #define trigger_run_overwrite_then_insert(_fn, _trans, _btree_id, _level, _old, _new, _flags)\
347 ({												\
348 	int ret = 0;										\
349 												\
350 	if (_old.k->type)									\
351 		ret = _fn(_trans, _btree_id, _level, _old, _flags & ~BTREE_TRIGGER_insert);	\
352 	if (!ret && _new.k->type)								\
353 		ret = _fn(_trans, _btree_id, _level, _new.s_c, _flags & ~BTREE_TRIGGER_overwrite);\
354 	ret;											\
355 })
356 
357 void bch2_trans_account_disk_usage_change(struct btree_trans *);
358 
359 void bch2_trans_fs_usage_revert(struct btree_trans *, struct replicas_delta_list *);
360 int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *);
361 
362 int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *, u64,
363 				    enum bch_data_type, unsigned,
364 				    enum btree_iter_update_trigger_flags);
365 int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *,
366 				    enum btree_iter_update_trigger_flags);
367 int bch2_trans_mark_dev_sbs_flags(struct bch_fs *,
368 				    enum btree_iter_update_trigger_flags);
369 int bch2_trans_mark_dev_sbs(struct bch_fs *);
370 
371 static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b)
372 {
373 	struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
374 	u64 b_offset	= bucket_to_sector(ca, b);
375 	u64 b_end	= bucket_to_sector(ca, b + 1);
376 	unsigned i;
377 
378 	if (!b)
379 		return true;
380 
381 	for (i = 0; i < layout->nr_superblocks; i++) {
382 		u64 offset = le64_to_cpu(layout->sb_offset[i]);
383 		u64 end = offset + (1 << layout->sb_max_size_bits);
384 
385 		if (!(offset >= b_end || end <= b_offset))
386 			return true;
387 	}
388 
389 	return false;
390 }
391 
392 static inline const char *bch2_data_type_str(enum bch_data_type type)
393 {
394 	return type < BCH_DATA_NR
395 		? __bch2_data_types[type]
396 		: "(invalid data type)";
397 }
398 
399 /* disk reservations: */
400 
401 static inline void bch2_disk_reservation_put(struct bch_fs *c,
402 					     struct disk_reservation *res)
403 {
404 	if (res->sectors) {
405 		this_cpu_sub(*c->online_reserved, res->sectors);
406 		res->sectors = 0;
407 	}
408 }
409 
410 #define BCH_DISK_RESERVATION_NOFAIL		(1 << 0)
411 
412 int __bch2_disk_reservation_add(struct bch_fs *,
413 				struct disk_reservation *,
414 				u64, int);
415 
416 static inline int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
417 					    u64 sectors, int flags)
418 {
419 #ifdef __KERNEL__
420 	u64 old, new;
421 
422 	do {
423 		old = this_cpu_read(c->pcpu->sectors_available);
424 		if (sectors > old)
425 			return __bch2_disk_reservation_add(c, res, sectors, flags);
426 
427 		new = old - sectors;
428 	} while (this_cpu_cmpxchg(c->pcpu->sectors_available, old, new) != old);
429 
430 	this_cpu_add(*c->online_reserved, sectors);
431 	res->sectors			+= sectors;
432 	return 0;
433 #else
434 	return __bch2_disk_reservation_add(c, res, sectors, flags);
435 #endif
436 }
437 
438 static inline struct disk_reservation
439 bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas)
440 {
441 	return (struct disk_reservation) {
442 		.sectors	= 0,
443 #if 0
444 		/* not used yet: */
445 		.gen		= c->capacity_gen,
446 #endif
447 		.nr_replicas	= nr_replicas,
448 	};
449 }
450 
451 static inline int bch2_disk_reservation_get(struct bch_fs *c,
452 					    struct disk_reservation *res,
453 					    u64 sectors, unsigned nr_replicas,
454 					    int flags)
455 {
456 	*res = bch2_disk_reservation_init(c, nr_replicas);
457 
458 	return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags);
459 }
460 
461 #define RESERVE_FACTOR	6
462 
463 static inline u64 avail_factor(u64 r)
464 {
465 	return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1);
466 }
467 
468 void bch2_buckets_nouse_free(struct bch_fs *);
469 int bch2_buckets_nouse_alloc(struct bch_fs *);
470 
471 int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64);
472 void bch2_dev_buckets_free(struct bch_dev *);
473 int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *);
474 
475 #endif /* _BUCKETS_H */
476