Skip to content

Commit 58474f7

Browse files
author
Kent Overstreet
committed
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>
1 parent 5132b99 commit 58474f7

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

fs/bcachefs/bcachefs_format.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,8 @@ struct bch_sb_field_ext {
676676
x(mi_btree_bitmap, BCH_VERSION(1, 7)) \
677677
x(bucket_stripe_sectors, BCH_VERSION(1, 8)) \
678678
x(disk_accounting_v2, BCH_VERSION(1, 9)) \
679-
x(disk_accounting_v3, BCH_VERSION(1, 10))
679+
x(disk_accounting_v3, BCH_VERSION(1, 10)) \
680+
x(disk_accounting_inum, BCH_VERSION(1, 11))
680681

681682
enum bcachefs_metadata_version {
682683
bcachefs_metadata_version_min = 9,

fs/bcachefs/buckets.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,20 @@ static int __trigger_extent(struct btree_trans *trans,
810810
ret = bch2_disk_accounting_mod(trans, &acc_btree_key, &replicas_sectors, 1, gc);
811811
if (ret)
812812
return ret;
813+
} else {
814+
bool insert = !(flags & BTREE_TRIGGER_overwrite);
815+
struct disk_accounting_pos acc_inum_key = {
816+
.type = BCH_DISK_ACCOUNTING_inum,
817+
.inum.inum = k.k->p.inode,
818+
};
819+
s64 v[3] = {
820+
insert ? 1 : -1,
821+
insert ? k.k->size : -((s64) k.k->size),
822+
replicas_sectors,
823+
};
824+
ret = bch2_disk_accounting_mod(trans, &acc_inum_key, v, ARRAY_SIZE(v), gc);
825+
if (ret)
826+
return ret;
813827
}
814828

815829
if (bch2_bkey_rebalance_opts(k)) {

fs/bcachefs/disk_accounting.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ void bch2_verify_accounting_clean(struct bch_fs *c)
768768
if (acc_k.type >= BCH_DISK_ACCOUNTING_TYPE_NR)
769769
continue;
770770

771+
if (acc_k.type == BCH_DISK_ACCOUNTING_inum)
772+
continue;
773+
771774
bch2_accounting_mem_read(c, k.k->p, v, nr);
772775

773776
if (memcmp(a.v->d, v, nr * sizeof(u64))) {

fs/bcachefs/disk_accounting.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ static inline int bch2_accounting_mem_mod_locked(struct btree_trans *trans, stru
116116
struct disk_accounting_pos acc_k;
117117
bpos_to_disk_accounting_pos(&acc_k, a.k->p);
118118

119+
if (acc_k.type == BCH_DISK_ACCOUNTING_inum)
120+
return 0;
121+
119122
if (!gc && !read) {
120123
switch (acc_k.type) {
121124
case BCH_DISK_ACCOUNTING_persistent_reserved:

fs/bcachefs/disk_accounting_format.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ static inline bool data_type_is_hidden(enum bch_data_type type)
103103
x(compression, 4) \
104104
x(snapshot, 5) \
105105
x(btree, 6) \
106-
x(rebalance_work, 7)
106+
x(rebalance_work, 7) \
107+
x(inum, 8)
107108

108109
enum disk_accounting_type {
109110
#define x(f, nr) BCH_DISK_ACCOUNTING_##f = nr,
@@ -136,6 +137,10 @@ struct bch_acct_btree {
136137
__u32 id;
137138
} __packed;
138139

140+
struct bch_acct_inum {
141+
__u64 inum;
142+
} __packed;
143+
139144
struct bch_acct_rebalance_work {
140145
};
141146

@@ -152,6 +157,7 @@ struct disk_accounting_pos {
152157
struct bch_acct_snapshot snapshot;
153158
struct bch_acct_btree btree;
154159
struct bch_acct_rebalance_work rebalance_work;
160+
struct bch_acct_inum inum;
155161
} __packed;
156162
} __packed;
157163
struct bpos _pad;

fs/bcachefs/sb-downgrade.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
BCH_FSCK_ERR_accounting_key_replicas_nr_devs_0, \
7373
BCH_FSCK_ERR_accounting_key_replicas_nr_required_bad, \
7474
BCH_FSCK_ERR_accounting_key_replicas_devs_unsorted, \
75-
BCH_FSCK_ERR_accounting_key_junk_at_end)
75+
BCH_FSCK_ERR_accounting_key_junk_at_end) \
76+
x(disk_accounting_inum, \
77+
BIT_ULL(BCH_RECOVERY_PASS_check_allocations), \
78+
BCH_FSCK_ERR_accounting_mismatch)
7679

7780
#define DOWNGRADE_TABLE() \
7881
x(bucket_stripe_sectors, \

0 commit comments

Comments
 (0)