The isize of directory is 0 in bcachefs if the directory is empty.
With more child dirents created, its size ought to change. Many
other filesystems changed as that (ie. xfs and btrfs). And many of
them changed as the size of child dirent name. Although the directory
size may not seem to convey much, we can still give it some meaning.
The formula of dentry size as follow:
occupied_size = 40 + ALIGN(9 + namelen, 8)
Signed-off-by: Hongbo Li <[email protected]>
---
fs/bcachefs/dirent.h | 5 +++++
fs/bcachefs/fs-common.c | 11 +++++++++++
fs/bcachefs/fs.c | 13 ++++++++-----
3 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/fs/bcachefs/dirent.h b/fs/bcachefs/dirent.h
index 8945145865c5..3b9987e1c727 100644
--- a/fs/bcachefs/dirent.h
+++ b/fs/bcachefs/dirent.h
@@ -31,6 +31,11 @@ static inline unsigned dirent_val_u64s(unsigned len)
sizeof(u64));
}
+static inline unsigned int dirent_occupied_size(const struct qstr *name)
+{
+ return (BKEY_U64s + dirent_val_u64s(name->len)) * sizeof(u64);
+}
+
int bch2_dirent_read_target(struct btree_trans *, subvol_inum,
struct bkey_s_c_dirent, subvol_inum *);
diff --git a/fs/bcachefs/fs-common.c b/fs/bcachefs/fs-common.c
index 7e10a9ddcfd9..b71f712e0471 100644
--- a/fs/bcachefs/fs-common.c
+++ b/fs/bcachefs/fs-common.c
@@ -154,6 +154,7 @@ int bch2_create_trans(struct btree_trans *trans,
if (is_subdir_for_nlink(new_inode))
dir_u->bi_nlink++;
dir_u->bi_mtime = dir_u->bi_ctime = now;
+ dir_u->bi_size += dirent_occupied_size(name);
ret = bch2_inode_write(trans, &dir_iter, dir_u);
if (ret)
@@ -218,6 +219,7 @@ int bch2_link_trans(struct btree_trans *trans,
}
dir_u->bi_mtime = dir_u->bi_ctime = now;
+ dir_u->bi_size += dirent_occupied_size(name);
dir_hash = bch2_hash_info_init(c, dir_u);
@@ -320,6 +322,7 @@ int bch2_unlink_trans(struct btree_trans *trans,
dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
+ dir_u->bi_size -= dirent_occupied_size(name);
ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
&dir_hash, &dirent_iter,
@@ -458,6 +461,14 @@ int bch2_rename_trans(struct btree_trans *trans,
goto err;
}
+ if (mode == BCH_RENAME) {
+ src_dir_u->bi_size -= dirent_occupied_size(src_name);
+ dst_dir_u->bi_size += dirent_occupied_size(dst_name);
+ }
+
+ if (mode == BCH_RENAME_OVERWRITE)
+ src_dir_u->bi_size -= dirent_occupied_size(src_name);
+
if (src_inode_u->bi_parent_subvol)
src_inode_u->bi_parent_subvol = dst_dir.subvol;
diff --git a/fs/bcachefs/fs.c b/fs/bcachefs/fs.c
index e774cb3e6b1a..23ca2f29330b 100644
--- a/fs/bcachefs/fs.c
+++ b/fs/bcachefs/fs.c
@@ -65,6 +65,9 @@ void bch2_inode_update_after_write(struct btree_trans *trans,
i_gid_write(&inode->v, bi->bi_gid);
inode->v.i_mode = bi->bi_mode;
+ if (fields & ATTR_SIZE)
+ i_size_write(&inode->v, bi->bi_size);
+
if (fields & ATTR_ATIME)
inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c,
bi->bi_atime));
if (fields & ATTR_MTIME)
@@ -438,7 +441,7 @@ __bch2_create(struct mnt_idmap *idmap,
if (!(flags & BCH_CREATE_TMPFILE)) {
bch2_inode_update_after_write(trans, dir, &dir_u,
- ATTR_MTIME|ATTR_CTIME);
+ ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
mutex_unlock(&dir->ei_update_lock);
}
@@ -595,7 +598,7 @@ static int __bch2_link(struct bch_fs *c,
if (likely(!ret)) {
bch2_inode_update_after_write(trans, dir, &dir_u,
- ATTR_MTIME|ATTR_CTIME);
+ ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
bch2_inode_update_after_write(trans, inode, &inode_u,
ATTR_CTIME);
}
@@ -648,7 +651,7 @@ int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
goto err;
bch2_inode_update_after_write(trans, dir, &dir_u,
- ATTR_MTIME|ATTR_CTIME);
+ ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
bch2_inode_update_after_write(trans, inode, &inode_u,
ATTR_MTIME);
@@ -826,11 +829,11 @@ static int bch2_rename2(struct mnt_idmap *idmap,
dst_inode->v.i_ino != dst_inode_u.bi_inum);
bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
- ATTR_MTIME|ATTR_CTIME);
+ ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
if (src_dir != dst_dir)
bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
- ATTR_MTIME|ATTR_CTIME);
+ ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
ATTR_CTIME);
--
2.34.1