xref: /linux/fs/bcachefs/alloc_types.h (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ALLOC_TYPES_H
3 #define _BCACHEFS_ALLOC_TYPES_H
4 
5 #include <linux/mutex.h>
6 #include <linux/spinlock.h>
7 
8 #include "clock_types.h"
9 #include "fifo.h"
10 
11 #define BCH_WATERMARKS()		\
12 	x(stripe)			\
13 	x(normal)			\
14 	x(copygc)			\
15 	x(btree)			\
16 	x(btree_copygc)			\
17 	x(reclaim)			\
18 	x(interior_updates)
19 
20 enum bch_watermark {
21 #define x(name)	BCH_WATERMARK_##name,
22 	BCH_WATERMARKS()
23 #undef x
24 	BCH_WATERMARK_NR,
25 };
26 
27 #define BCH_WATERMARK_BITS	3
28 #define BCH_WATERMARK_MASK	~(~0U << BCH_WATERMARK_BITS)
29 
30 #define OPEN_BUCKETS_COUNT	1024
31 
32 #define WRITE_POINT_HASH_NR	32
33 #define WRITE_POINT_MAX		32
34 
35 /*
36  * 0 is never a valid open_bucket_idx_t:
37  */
38 typedef u16			open_bucket_idx_t;
39 
40 struct open_bucket {
41 	spinlock_t		lock;
42 	atomic_t		pin;
43 	open_bucket_idx_t	freelist;
44 	open_bucket_idx_t	hash;
45 
46 	/*
47 	 * When an open bucket has an ec_stripe attached, this is the index of
48 	 * the block in the stripe this open_bucket corresponds to:
49 	 */
50 	u8			ec_idx;
51 	enum bch_data_type	data_type:6;
52 	unsigned		valid:1;
53 	unsigned		on_partial_list:1;
54 
55 	u8			dev;
56 	u8			gen;
57 	u32			sectors_free;
58 	u64			bucket;
59 	struct ec_stripe_new	*ec;
60 };
61 
62 #define OPEN_BUCKET_LIST_MAX	15
63 
64 struct open_buckets {
65 	open_bucket_idx_t	nr;
66 	open_bucket_idx_t	v[OPEN_BUCKET_LIST_MAX];
67 };
68 
69 struct dev_stripe_state {
70 	u64			next_alloc[BCH_SB_MEMBERS_MAX];
71 };
72 
73 #define WRITE_POINT_STATES()		\
74 	x(stopped)			\
75 	x(waiting_io)			\
76 	x(waiting_work)			\
77 	x(runnable)			\
78 	x(running)
79 
80 enum write_point_state {
81 #define x(n)	WRITE_POINT_##n,
82 	WRITE_POINT_STATES()
83 #undef x
84 	WRITE_POINT_STATE_NR
85 };
86 
87 struct write_point {
88 	struct {
89 		struct hlist_node	node;
90 		struct mutex		lock;
91 		u64			last_used;
92 		unsigned long		write_point;
93 		enum bch_data_type	data_type;
94 
95 		/* calculated based on how many pointers we're actually going to use: */
96 		unsigned		sectors_free;
97 
98 		struct open_buckets	ptrs;
99 		struct dev_stripe_state	stripe;
100 
101 		u64			sectors_allocated;
102 	} __aligned(SMP_CACHE_BYTES);
103 
104 	struct {
105 		struct work_struct	index_update_work;
106 
107 		struct list_head	writes;
108 		spinlock_t		writes_lock;
109 
110 		enum write_point_state	state;
111 		u64			last_state_change;
112 		u64			time[WRITE_POINT_STATE_NR];
113 		u64			last_runtime;
114 	} __aligned(SMP_CACHE_BYTES);
115 };
116 
117 struct write_point_specifier {
118 	unsigned long		v;
119 };
120 
121 #endif /* _BCACHEFS_ALLOC_TYPES_H */
122