]> Git Repo - J-linux.git/blob - fs/bcachefs/disk_accounting.h
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / fs / bcachefs / disk_accounting.h
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.bversion, src.k->bversion) < 0)
40                 dst->k.bversion = src.k->bversion;
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_validate(struct bch_fs *, struct bkey_s_c, enum bch_validate_flags);
86 void bch2_accounting_key_to_text(struct printbuf *, struct disk_accounting_pos *);
87 void bch2_accounting_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
88 void bch2_accounting_swab(struct bkey_s);
89
90 #define bch2_bkey_ops_accounting ((struct bkey_ops) {   \
91         .key_validate   = bch2_accounting_validate,     \
92         .val_to_text    = bch2_accounting_to_text,      \
93         .swab           = bch2_accounting_swab,         \
94         .min_val_size   = 8,                            \
95 })
96
97 int bch2_accounting_update_sb(struct btree_trans *);
98
99 static inline int accounting_pos_cmp(const void *_l, const void *_r)
100 {
101         const struct bpos *l = _l, *r = _r;
102
103         return bpos_cmp(*l, *r);
104 }
105
106 enum bch_accounting_mode {
107         BCH_ACCOUNTING_normal,
108         BCH_ACCOUNTING_gc,
109         BCH_ACCOUNTING_read,
110 };
111
112 int bch2_accounting_mem_insert(struct bch_fs *, struct bkey_s_c_accounting, enum bch_accounting_mode);
113 void bch2_accounting_mem_gc(struct bch_fs *);
114
115 /*
116  * Update in memory counters so they match the btree update we're doing; called
117  * from transaction commit path
118  */
119 static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans,
120                                                  struct bkey_s_c_accounting a,
121                                                  enum bch_accounting_mode mode)
122 {
123         struct bch_fs *c = trans->c;
124         struct bch_accounting_mem *acc = &c->accounting;
125         struct disk_accounting_pos acc_k;
126         bpos_to_disk_accounting_pos(&acc_k, a.k->p);
127         bool gc = mode == BCH_ACCOUNTING_gc;
128
129         EBUG_ON(gc && !acc->gc_running);
130
131         if (acc_k.type == BCH_DISK_ACCOUNTING_inum)
132                 return 0;
133
134         if (mode == BCH_ACCOUNTING_normal) {
135                 switch (acc_k.type) {
136                 case BCH_DISK_ACCOUNTING_persistent_reserved:
137                         trans->fs_usage_delta.reserved += acc_k.persistent_reserved.nr_replicas * a.v->d[0];
138                         break;
139                 case BCH_DISK_ACCOUNTING_replicas:
140                         fs_usage_data_type_to_base(&trans->fs_usage_delta, acc_k.replicas.data_type, a.v->d[0]);
141                         break;
142                 case BCH_DISK_ACCOUNTING_dev_data_type:
143                         rcu_read_lock();
144                         struct bch_dev *ca = bch2_dev_rcu(c, acc_k.dev_data_type.dev);
145                         if (ca) {
146                                 this_cpu_add(ca->usage->d[acc_k.dev_data_type.data_type].buckets, a.v->d[0]);
147                                 this_cpu_add(ca->usage->d[acc_k.dev_data_type.data_type].sectors, a.v->d[1]);
148                                 this_cpu_add(ca->usage->d[acc_k.dev_data_type.data_type].fragmented, a.v->d[2]);
149                         }
150                         rcu_read_unlock();
151                         break;
152                 }
153         }
154
155         unsigned idx;
156
157         while ((idx = eytzinger0_find(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
158                                       accounting_pos_cmp, &a.k->p)) >= acc->k.nr) {
159                 int ret = bch2_accounting_mem_insert(c, a, mode);
160                 if (ret)
161                         return ret;
162         }
163
164         struct accounting_mem_entry *e = &acc->k.data[idx];
165
166         EBUG_ON(bch2_accounting_counters(a.k) != e->nr_counters);
167
168         for (unsigned i = 0; i < bch2_accounting_counters(a.k); i++)
169                 this_cpu_add(e->v[gc][i], a.v->d[i]);
170         return 0;
171 }
172
173 static inline int bch2_accounting_mem_add(struct btree_trans *trans, struct bkey_s_c_accounting a, bool gc)
174 {
175         percpu_down_read(&trans->c->mark_lock);
176         int ret = bch2_accounting_mem_mod_locked(trans, a, gc ? BCH_ACCOUNTING_gc : BCH_ACCOUNTING_normal);
177         percpu_up_read(&trans->c->mark_lock);
178         return ret;
179 }
180
181 static inline void bch2_accounting_mem_read_counters(struct bch_accounting_mem *acc,
182                                                      unsigned idx, u64 *v, unsigned nr, bool gc)
183 {
184         memset(v, 0, sizeof(*v) * nr);
185
186         if (unlikely(idx >= acc->k.nr))
187                 return;
188
189         struct accounting_mem_entry *e = &acc->k.data[idx];
190
191         nr = min_t(unsigned, nr, e->nr_counters);
192
193         for (unsigned i = 0; i < nr; i++)
194                 v[i] = percpu_u64_get(e->v[gc] + i);
195 }
196
197 static inline void bch2_accounting_mem_read(struct bch_fs *c, struct bpos p,
198                                             u64 *v, unsigned nr)
199 {
200         struct bch_accounting_mem *acc = &c->accounting;
201         unsigned idx = eytzinger0_find(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
202                                        accounting_pos_cmp, &p);
203
204         bch2_accounting_mem_read_counters(acc, idx, v, nr, false);
205 }
206
207 int bch2_fs_replicas_usage_read(struct bch_fs *, darray_char *);
208 int bch2_fs_accounting_read(struct bch_fs *, darray_char *, unsigned);
209 void bch2_fs_accounting_to_text(struct printbuf *, struct bch_fs *);
210
211 int bch2_gc_accounting_start(struct bch_fs *);
212 int bch2_gc_accounting_done(struct bch_fs *);
213
214 int bch2_accounting_read(struct bch_fs *);
215
216 int bch2_dev_usage_remove(struct bch_fs *, unsigned);
217 int bch2_dev_usage_init(struct bch_dev *, bool);
218
219 void bch2_verify_accounting_clean(struct bch_fs *c);
220
221 void bch2_accounting_gc_free(struct bch_fs *);
222 void bch2_fs_accounting_exit(struct bch_fs *);
223
224 #endif /* _BCACHEFS_DISK_ACCOUNTING_H */
This page took 0.039189 seconds and 4 git commands to generate.