1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_DISK_ACCOUNTING_H
3 #define _BCACHEFS_DISK_ACCOUNTING_H
6 #include "sb-members.h"
8 static inline void bch2_u64s_neg(u64
*v
, unsigned nr
)
10 for (unsigned i
= 0; i
< nr
; i
++)
14 static inline unsigned bch2_accounting_counters(const struct bkey
*k
)
16 return bkey_val_u64s(k
) - offsetof(struct bch_accounting
, d
) / sizeof(u64
);
19 static inline void bch2_accounting_neg(struct bkey_s_accounting a
)
21 bch2_u64s_neg(a
.v
->d
, bch2_accounting_counters(a
.k
));
24 static inline bool bch2_accounting_key_is_zero(struct bkey_s_c_accounting a
)
26 for (unsigned i
= 0; i
< bch2_accounting_counters(a
.k
); i
++)
32 static inline void bch2_accounting_accumulate(struct bkey_i_accounting
*dst
,
33 struct bkey_s_c_accounting src
)
35 EBUG_ON(dst
->k
.u64s
!= src
.k
->u64s
);
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
;
43 static inline void fs_usage_data_type_to_base(struct bch_fs_usage_base
*fs_usage
,
44 enum bch_data_type data_type
,
49 fs_usage
->btree
+= sectors
;
53 fs_usage
->data
+= sectors
;
56 fs_usage
->cached
+= sectors
;
63 static inline void bpos_to_disk_accounting_pos(struct disk_accounting_pos
*acc
, struct bpos p
)
66 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
67 bch2_bpos_swab(&acc
->_pad
);
71 static inline struct bpos
disk_accounting_pos_to_bpos(struct disk_accounting_pos
*k
)
73 struct bpos ret
= k
->_pad
;
75 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
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);
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
);
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, \
97 int bch2_accounting_update_sb(struct btree_trans
*);
99 static inline int accounting_pos_cmp(const void *_l
, const void *_r
)
101 const struct bpos
*l
= _l
, *r
= _r
;
103 return bpos_cmp(*l
, *r
);
106 enum bch_accounting_mode
{
107 BCH_ACCOUNTING_normal
,
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
*);
116 * Update in memory counters so they match the btree update we're doing; called
117 * from transaction commit path
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
)
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
;
129 EBUG_ON(gc
&& !acc
->gc_running
);
131 if (acc_k
.type
== BCH_DISK_ACCOUNTING_inum
)
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];
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]);
142 case BCH_DISK_ACCOUNTING_dev_data_type
:
144 struct bch_dev
*ca
= bch2_dev_rcu(c
, acc_k
.dev_data_type
.dev
);
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]);
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
);
164 struct accounting_mem_entry
*e
= &acc
->k
.data
[idx
];
166 EBUG_ON(bch2_accounting_counters(a
.k
) != e
->nr_counters
);
168 for (unsigned i
= 0; i
< bch2_accounting_counters(a
.k
); i
++)
169 this_cpu_add(e
->v
[gc
][i
], a
.v
->d
[i
]);
173 static inline int bch2_accounting_mem_add(struct btree_trans
*trans
, struct bkey_s_c_accounting a
, bool gc
)
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
);
181 static inline void bch2_accounting_mem_read_counters(struct bch_accounting_mem
*acc
,
182 unsigned idx
, u64
*v
, unsigned nr
, bool gc
)
184 memset(v
, 0, sizeof(*v
) * nr
);
186 if (unlikely(idx
>= acc
->k
.nr
))
189 struct accounting_mem_entry
*e
= &acc
->k
.data
[idx
];
191 nr
= min_t(unsigned, nr
, e
->nr_counters
);
193 for (unsigned i
= 0; i
< nr
; i
++)
194 v
[i
] = percpu_u64_get(e
->v
[gc
] + i
);
197 static inline void bch2_accounting_mem_read(struct bch_fs
*c
, struct bpos p
,
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
);
204 bch2_accounting_mem_read_counters(acc
, idx
, v
, nr
, false);
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
*);
211 int bch2_gc_accounting_start(struct bch_fs
*);
212 int bch2_gc_accounting_done(struct bch_fs
*);
214 int bch2_accounting_read(struct bch_fs
*);
216 int bch2_dev_usage_remove(struct bch_fs
*, unsigned);
217 int bch2_dev_usage_init(struct bch_dev
*, bool);
219 void bch2_verify_accounting_clean(struct bch_fs
*c
);
221 void bch2_accounting_gc_free(struct bch_fs
*);
222 void bch2_fs_accounting_exit(struct bch_fs
*);
224 #endif /* _BCACHEFS_DISK_ACCOUNTING_H */