bcachefs: bcachefs_metadata_version_disk_accounting_inum

This adds another disk accounting counter to track usage per inode
number (any snapshot ID).

This will be used for a couple things:

- It'll give us a way to tell the user how much space a given file ista
  consuming in all snapshots; i.e. how much extra space it's consuming
  due to snapshot versioning.

- It counts number of extents and total size of extents (both in btree
  keyspace sectors and actual disk usage), meaning it gives us average
  extent size: that is, it'll let us cheaply find fragmented files that
  should be defragmented.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2024-08-12 02:27:36 -04:00
parent 5132b99bb6
commit 58474f76a7
6 changed files with 33 additions and 3 deletions

View File

@ -676,7 +676,8 @@ struct bch_sb_field_ext {
x(mi_btree_bitmap, BCH_VERSION(1, 7)) \ x(mi_btree_bitmap, BCH_VERSION(1, 7)) \
x(bucket_stripe_sectors, BCH_VERSION(1, 8)) \ x(bucket_stripe_sectors, BCH_VERSION(1, 8)) \
x(disk_accounting_v2, BCH_VERSION(1, 9)) \ x(disk_accounting_v2, BCH_VERSION(1, 9)) \
x(disk_accounting_v3, BCH_VERSION(1, 10)) x(disk_accounting_v3, BCH_VERSION(1, 10)) \
x(disk_accounting_inum, BCH_VERSION(1, 11))
enum bcachefs_metadata_version { enum bcachefs_metadata_version {
bcachefs_metadata_version_min = 9, bcachefs_metadata_version_min = 9,

View File

@ -810,6 +810,20 @@ static int __trigger_extent(struct btree_trans *trans,
ret = bch2_disk_accounting_mod(trans, &acc_btree_key, &replicas_sectors, 1, gc); ret = bch2_disk_accounting_mod(trans, &acc_btree_key, &replicas_sectors, 1, gc);
if (ret) if (ret)
return ret; return ret;
} else {
bool insert = !(flags & BTREE_TRIGGER_overwrite);
struct disk_accounting_pos acc_inum_key = {
.type = BCH_DISK_ACCOUNTING_inum,
.inum.inum = k.k->p.inode,
};
s64 v[3] = {
insert ? 1 : -1,
insert ? k.k->size : -((s64) k.k->size),
replicas_sectors,
};
ret = bch2_disk_accounting_mod(trans, &acc_inum_key, v, ARRAY_SIZE(v), gc);
if (ret)
return ret;
} }
if (bch2_bkey_rebalance_opts(k)) { if (bch2_bkey_rebalance_opts(k)) {

View File

@ -768,6 +768,9 @@ void bch2_verify_accounting_clean(struct bch_fs *c)
if (acc_k.type >= BCH_DISK_ACCOUNTING_TYPE_NR) if (acc_k.type >= BCH_DISK_ACCOUNTING_TYPE_NR)
continue; continue;
if (acc_k.type == BCH_DISK_ACCOUNTING_inum)
continue;
bch2_accounting_mem_read(c, k.k->p, v, nr); bch2_accounting_mem_read(c, k.k->p, v, nr);
if (memcmp(a.v->d, v, nr * sizeof(u64))) { if (memcmp(a.v->d, v, nr * sizeof(u64))) {

View File

@ -116,6 +116,9 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans, stru
struct disk_accounting_pos acc_k; struct disk_accounting_pos acc_k;
bpos_to_disk_accounting_pos(&acc_k, a.k->p); bpos_to_disk_accounting_pos(&acc_k, a.k->p);
if (acc_k.type == BCH_DISK_ACCOUNTING_inum)
return 0;
if (!gc && !read) { if (!gc && !read) {
switch (acc_k.type) { switch (acc_k.type) {
case BCH_DISK_ACCOUNTING_persistent_reserved: case BCH_DISK_ACCOUNTING_persistent_reserved:

View File

@ -103,7 +103,8 @@ static inline bool data_type_is_hidden(enum bch_data_type type)
x(compression, 4) \ x(compression, 4) \
x(snapshot, 5) \ x(snapshot, 5) \
x(btree, 6) \ x(btree, 6) \
x(rebalance_work, 7) x(rebalance_work, 7) \
x(inum, 8)
enum disk_accounting_type { enum disk_accounting_type {
#define x(f, nr) BCH_DISK_ACCOUNTING_##f = nr, #define x(f, nr) BCH_DISK_ACCOUNTING_##f = nr,
@ -136,6 +137,10 @@ struct bch_acct_btree {
__u32 id; __u32 id;
} __packed; } __packed;
struct bch_acct_inum {
__u64 inum;
} __packed;
struct bch_acct_rebalance_work { struct bch_acct_rebalance_work {
}; };
@ -152,6 +157,7 @@ struct disk_accounting_pos {
struct bch_acct_snapshot snapshot; struct bch_acct_snapshot snapshot;
struct bch_acct_btree btree; struct bch_acct_btree btree;
struct bch_acct_rebalance_work rebalance_work; struct bch_acct_rebalance_work rebalance_work;
struct bch_acct_inum inum;
} __packed; } __packed;
} __packed; } __packed;
struct bpos _pad; struct bpos _pad;

View File

@ -72,7 +72,10 @@
BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \ BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \
BCH_FSCK_ERR_accounting_key_replicas_nr_required_bad, \ BCH_FSCK_ERR_accounting_key_replicas_nr_required_bad, \
BCH_FSCK_ERR_accounting_key_replicas_devs_unsorted, \ BCH_FSCK_ERR_accounting_key_replicas_devs_unsorted, \
BCH_FSCK_ERR_accounting_key_junk_at_end) BCH_FSCK_ERR_accounting_key_junk_at_end) \
x(disk_accounting_inum, \
BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \
BCH_FSCK_ERR_accounting_mismatch)
#define DOWNGRADE_TABLE() \ #define DOWNGRADE_TABLE() \
x(bucket_stripe_sectors, \ x(bucket_stripe_sectors, \