Skip to content

Commit e614a6c

Browse files
Hongbo LiKent Overstreet
authored andcommitted
bcachefs: make directory i_size meaningful
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 <lihongbo22@huawei.com> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 4204e3b commit e614a6c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

fs/bcachefs/dirent.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ static inline unsigned dirent_val_u64s(unsigned len)
3131
sizeof(u64));
3232
}
3333

34+
static inline unsigned int dirent_occupied_size(const struct qstr *name)
35+
{
36+
return (BKEY_U64s + dirent_val_u64s(name->len)) * sizeof(u64);
37+
}
38+
3439
int bch2_dirent_read_target(struct btree_trans *, subvol_inum,
3540
struct bkey_s_c_dirent, subvol_inum *);
3641

fs/bcachefs/fs-common.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ int bch2_create_trans(struct btree_trans *trans,
152152
if (is_subdir_for_nlink(new_inode))
153153
dir_u->bi_nlink++;
154154
dir_u->bi_mtime = dir_u->bi_ctime = now;
155+
dir_u->bi_size += dirent_occupied_size(name);
155156

156157
ret = bch2_inode_write(trans, &dir_iter, dir_u);
157158
if (ret)
@@ -220,6 +221,7 @@ int bch2_link_trans(struct btree_trans *trans,
220221
}
221222

222223
dir_u->bi_mtime = dir_u->bi_ctime = now;
224+
dir_u->bi_size += dirent_occupied_size(name);
223225

224226
dir_hash = bch2_hash_info_init(c, dir_u);
225227

@@ -322,6 +324,7 @@ int bch2_unlink_trans(struct btree_trans *trans,
322324

323325
dir_u->bi_mtime = dir_u->bi_ctime = inode_u->bi_ctime = now;
324326
dir_u->bi_nlink -= is_subdir_for_nlink(inode_u);
327+
dir_u->bi_size -= dirent_occupied_size(name);
325328

326329
ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
327330
&dir_hash, &dirent_iter,
@@ -460,6 +463,14 @@ int bch2_rename_trans(struct btree_trans *trans,
460463
goto err;
461464
}
462465

466+
if (mode == BCH_RENAME) {
467+
src_dir_u->bi_size -= dirent_occupied_size(src_name);
468+
dst_dir_u->bi_size += dirent_occupied_size(dst_name);
469+
}
470+
471+
if (mode == BCH_RENAME_OVERWRITE)
472+
src_dir_u->bi_size -= dirent_occupied_size(src_name);
473+
463474
if (src_inode_u->bi_parent_subvol)
464475
src_inode_u->bi_parent_subvol = dst_dir.subvol;
465476

fs/bcachefs/fs.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ void bch2_inode_update_after_write(struct btree_trans *trans,
6767
i_gid_write(&inode->v, bi->bi_gid);
6868
inode->v.i_mode = bi->bi_mode;
6969

70+
if (fields & ATTR_SIZE)
71+
i_size_write(&inode->v, bi->bi_size);
72+
7073
if (fields & ATTR_ATIME)
7174
inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
7275
if (fields & ATTR_MTIME)
@@ -582,7 +585,7 @@ __bch2_create(struct mnt_idmap *idmap,
582585

583586
if (!(flags & BCH_CREATE_TMPFILE)) {
584587
bch2_inode_update_after_write(trans, dir, &dir_u,
585-
ATTR_MTIME|ATTR_CTIME);
588+
ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
586589
mutex_unlock(&dir->ei_update_lock);
587590
}
588591

@@ -739,7 +742,7 @@ static int __bch2_link(struct bch_fs *c,
739742

740743
if (likely(!ret)) {
741744
bch2_inode_update_after_write(trans, dir, &dir_u,
742-
ATTR_MTIME|ATTR_CTIME);
745+
ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
743746
bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
744747
}
745748

@@ -792,7 +795,7 @@ int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
792795
goto err;
793796

794797
bch2_inode_update_after_write(trans, dir, &dir_u,
795-
ATTR_MTIME|ATTR_CTIME);
798+
ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
796799
bch2_inode_update_after_write(trans, inode, &inode_u,
797800
ATTR_MTIME);
798801

@@ -970,11 +973,11 @@ static int bch2_rename2(struct mnt_idmap *idmap,
970973
dst_inode->v.i_ino != dst_inode_u.bi_inum);
971974

972975
bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
973-
ATTR_MTIME|ATTR_CTIME);
976+
ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
974977

975978
if (src_dir != dst_dir)
976979
bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
977-
ATTR_MTIME|ATTR_CTIME);
980+
ATTR_MTIME|ATTR_CTIME|ATTR_SIZE);
978981

979982
bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
980983
ATTR_CTIME);

0 commit comments

Comments
 (0)