xref: /linux/fs/bcachefs/disk_accounting.h (revision d40981350844c2cfa437abfc80596e10ea8f1149)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_DISK_ACCOUNTING_H
3 #define _BCACHEFS_DISK_ACCOUNTING_H
4 
5 #include "eytzinger.h"
6 #include "sb-members.h"
7 
8 static inline void bch2_u64s_neg(u64 *v, unsigned nr)
9 {
10 	for (unsigned i = 0; i < nr; i++)
11 		v[i] = -v[i];
12 }
13 
14 static inline unsigned bch2_accounting_counters(const struct bkey *k)
15 {
16 	return bkey_val_u64s(k) - offsetof(struct bch_accounting, d) / sizeof(u64);
17 }
18 
19 static inline void bch2_accounting_neg(struct bkey_s_accounting a)
20 {
21 	bch2_u64s_neg(a.v->d, bch2_accounting_counters(a.k));
22 }
23 
24 static inline bool bch2_accounting_key_is_zero(struct bkey_s_c_accounting a)
25 {
26 	for (unsigned i = 0;  i < bch2_accounting_counters(a.k); i++)
27 		if (a.v->d[i])
28 			return false;
29 	return true;
30 }
31 
32 static inline void bch2_accounting_accumulate(struct bkey_i_accounting *dst,
33 					      struct bkey_s_c_accounting src)
34 {
35 	EBUG_ON(dst->k.u64s != src.k->u64s);
36 
37 	for (unsigned i = 0; i < bch2_accounting_counters(&dst->k); i++)
38 		dst->v.d[i] += src.v->d[i];
39 	if (bversion_cmp(dst->k.version, src.k->version) < 0)
40 		dst->k.version = src.k->version;
41 }
42 
43 static inline void fs_usage_data_type_to_base(struct bch_fs_usage_base *fs_usage,
44 					      enum bch_data_type data_type,
45 					      s64 sectors)
46 {
47 	switch (data_type) {
48 	case BCH_DATA_btree:
49 		fs_usage->btree		+= sectors;
50 		break;
51 	case BCH_DATA_user:
52 	case BCH_DATA_parity:
53 		fs_usage->data		+= sectors;
54 		break;
55 	case BCH_DATA_cached:
56 		fs_usage->cached	+= sectors;
57 		break;
58 	default:
59 		break;
60 	}
61 }
62 
63 static inline void bpos_to_disk_accounting_pos(struct disk_accounting_pos *acc, struct bpos p)
64 {
65 	acc->_pad = p;
66 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
67 	bch2_bpos_swab(&acc->_pad);
68 #endif
69 }
70 
71 static inline struct bpos disk_accounting_pos_to_bpos(struct disk_accounting_pos *k)
72 {
73 	struct bpos ret = k->_pad;
74 
75 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
76 	bch2_bpos_swab(&ret);
77 #endif
78 	return ret;
79 }
80 
81 int bch2_disk_accounting_mod(struct btree_trans *, struct disk_accounting_pos *,
82 			     s64 *, unsigned, bool);
83 int bch2_mod_dev_cached_sectors(struct btree_trans *, unsigned, s64, bool);
84 
85 int bch2_accounting_invalid(struct bch_fs *, struct bkey_s_c,
86 			    enum bch_validate_flags, struct printbuf *);
87 void bch2_accounting_key_to_text(struct printbuf *, struct disk_accounting_pos *);
88 void bch2_accounting_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
89 void bch2_accounting_swab(struct bkey_s);
90 
91 #define bch2_bkey_ops_accounting ((struct bkey_ops) {	\
92 	.key_invalid	= bch2_accounting_invalid,	\
93 	.val_to_text	= bch2_accounting_to_text,	\
94 	.swab		= bch2_accounting_swab,		\
95 	.min_val_size	= 8,				\
96 })
97 
98 int bch2_accounting_update_sb(struct btree_trans *);
99 
100 static inline int accounting_pos_cmp(const void *_l, const void *_r)
101 {
102 	const struct bpos *l = _l, *r = _r;
103 
104 	return bpos_cmp(*l, *r);
105 }
106 
107 int bch2_accounting_mem_insert(struct bch_fs *, struct bkey_s_c_accounting, bool);
108 void bch2_accounting_mem_gc(struct bch_fs *);
109 
110 static inline int __bch2_accounting_mem_mod(struct bch_fs *c, struct bkey_s_c_accounting a, bool gc)
111 {
112 	struct bch_accounting_mem *acc = &c->accounting;
113 	unsigned idx;
114 
115 	EBUG_ON(gc && !acc->gc_running);
116 
117 	while ((idx = eytzinger0_find(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
118 				      accounting_pos_cmp, &a.k->p)) >= acc->k.nr) {
119 		int ret = bch2_accounting_mem_insert(c, a, gc);
120 		if (ret)
121 			return ret;
122 	}
123 
124 	struct accounting_mem_entry *e = &acc->k.data[idx];
125 
126 	EBUG_ON(bch2_accounting_counters(a.k) != e->nr_counters);
127 
128 	for (unsigned i = 0; i < bch2_accounting_counters(a.k); i++)
129 		this_cpu_add(e->v[gc][i], a.v->d[i]);
130 	return 0;
131 }
132 
133 /*
134  * Update in memory counters so they match the btree update we're doing; called
135  * from transaction commit path
136  */
137 static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans, struct bkey_s_c_accounting a, bool gc)
138 {
139 	struct bch_fs *c = trans->c;
140 
141 	if (!gc) {
142 		struct disk_accounting_pos acc_k;
143 		bpos_to_disk_accounting_pos(&acc_k, a.k->p);
144 
145 		switch (acc_k.type) {
146 		case BCH_DISK_ACCOUNTING_persistent_reserved:
147 			trans->fs_usage_delta.reserved += acc_k.persistent_reserved.nr_replicas * a.v->d[0];
148 			break;
149 		case BCH_DISK_ACCOUNTING_replicas:
150 			fs_usage_data_type_to_base(&trans->fs_usage_delta, acc_k.replicas.data_type, a.v->d[0]);
151 			break;
152 		case BCH_DISK_ACCOUNTING_dev_data_type:
153 			rcu_read_lock();
154 			struct bch_dev *ca = bch2_dev_rcu(c, acc_k.dev_data_type.dev);
155 			if (ca) {
156 				this_cpu_add(ca->usage->d[acc_k.dev_data_type.data_type].buckets, a.v->d[0]);
157 				this_cpu_add(ca->usage->d[acc_k.dev_data_type.data_type].sectors, a.v->d[1]);
158 				this_cpu_add(ca->usage->d[acc_k.dev_data_type.data_type].fragmented, a.v->d[2]);
159 			}
160 			rcu_read_unlock();
161 			break;
162 		}
163 	}
164 
165 	return __bch2_accounting_mem_mod(c, a, gc);
166 }
167 
168 static inline int bch2_accounting_mem_add(struct btree_trans *trans, struct bkey_s_c_accounting a, bool gc)
169 {
170 	percpu_down_read(&trans->c->mark_lock);
171 	int ret = bch2_accounting_mem_mod_locked(trans, a, gc);
172 	percpu_up_read(&trans->c->mark_lock);
173 	return ret;
174 }
175 
176 static inline void bch2_accounting_mem_read_counters(struct bch_accounting_mem *acc,
177 						     unsigned idx, u64 *v, unsigned nr, bool gc)
178 {
179 	memset(v, 0, sizeof(*v) * nr);
180 
181 	if (unlikely(idx >= acc->k.nr))
182 		return;
183 
184 	struct accounting_mem_entry *e = &acc->k.data[idx];
185 
186 	nr = min_t(unsigned, nr, e->nr_counters);
187 
188 	for (unsigned i = 0; i < nr; i++)
189 		v[i] = percpu_u64_get(e->v[gc] + i);
190 }
191 
192 static inline void bch2_accounting_mem_read(struct bch_fs *c, struct bpos p,
193 					    u64 *v, unsigned nr)
194 {
195 	struct bch_accounting_mem *acc = &c->accounting;
196 	unsigned idx = eytzinger0_find(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
197 				       accounting_pos_cmp, &p);
198 
199 	bch2_accounting_mem_read_counters(acc, idx, v, nr, false);
200 }
201 
202 int bch2_fs_replicas_usage_read(struct bch_fs *, darray_char *);
203 int bch2_fs_accounting_read(struct bch_fs *, darray_char *, unsigned);
204 void bch2_fs_accounting_to_text(struct printbuf *, struct bch_fs *);
205 
206 int bch2_gc_accounting_start(struct bch_fs *);
207 int bch2_gc_accounting_done(struct bch_fs *);
208 
209 int bch2_accounting_read(struct bch_fs *);
210 
211 int bch2_dev_usage_remove(struct bch_fs *, unsigned);
212 int bch2_dev_usage_init(struct bch_dev *, bool);
213 
214 void bch2_verify_accounting_clean(struct bch_fs *c);
215 
216 void bch2_accounting_gc_free(struct bch_fs *);
217 void bch2_fs_accounting_exit(struct bch_fs *);
218 
219 #endif /* _BCACHEFS_DISK_ACCOUNTING_H */
220