Commit b43a0f60 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Cleanup i_nlink handling

Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent d55460bb
...@@ -131,9 +131,7 @@ void bch2_inode_update_after_write(struct bch_fs *c, ...@@ -131,9 +131,7 @@ void bch2_inode_update_after_write(struct bch_fs *c,
struct bch_inode_unpacked *bi, struct bch_inode_unpacked *bi,
unsigned fields) unsigned fields)
{ {
set_nlink(&inode->v, bi->bi_flags & BCH_INODE_UNLINKED set_nlink(&inode->v, bch2_inode_nlink_get(bi));
? 0
: bi->bi_nlink + nlink_bias(inode->v.i_mode));
i_uid_write(&inode->v, bi->bi_uid); i_uid_write(&inode->v, bi->bi_uid);
i_gid_write(&inode->v, bi->bi_gid); i_gid_write(&inode->v, bi->bi_gid);
inode->v.i_mode = bi->bi_mode; inode->v.i_mode = bi->bi_mode;
...@@ -552,12 +550,7 @@ static int inode_update_for_link_fn(struct bch_inode_info *inode, ...@@ -552,12 +550,7 @@ static int inode_update_for_link_fn(struct bch_inode_info *inode,
struct bch_fs *c = inode->v.i_sb->s_fs_info; struct bch_fs *c = inode->v.i_sb->s_fs_info;
bi->bi_ctime = bch2_current_time(c); bi->bi_ctime = bch2_current_time(c);
bch2_inode_nlink_inc(bi);
if (bi->bi_flags & BCH_INODE_UNLINKED)
bi->bi_flags &= ~BCH_INODE_UNLINKED;
else
bi->bi_nlink++;
return 0; return 0;
} }
...@@ -640,11 +633,7 @@ static int inode_update_for_unlink_fn(struct bch_inode_info *inode, ...@@ -640,11 +633,7 @@ static int inode_update_for_unlink_fn(struct bch_inode_info *inode,
struct bch_fs *c = inode->v.i_sb->s_fs_info; struct bch_fs *c = inode->v.i_sb->s_fs_info;
bi->bi_ctime = bch2_current_time(c); bi->bi_ctime = bch2_current_time(c);
if (bi->bi_nlink) bch2_inode_nlink_dec(bi);
bi->bi_nlink--;
else
bi->bi_flags |= BCH_INODE_UNLINKED;
return 0; return 0;
} }
...@@ -815,10 +804,7 @@ static int inode_update_for_rename_fn(struct bch_inode_info *inode, ...@@ -815,10 +804,7 @@ static int inode_update_for_rename_fn(struct bch_inode_info *inode,
BUG_ON(bi->bi_nlink && BUG_ON(bi->bi_nlink &&
S_ISDIR(info->dst_inode->v.i_mode)); S_ISDIR(info->dst_inode->v.i_mode));
if (bi->bi_nlink) bch2_inode_nlink_dec(bi);
bi->bi_nlink--;
else
bi->bi_flags |= BCH_INODE_UNLINKED;
} }
if (inode == info->src_dir || if (inode == info->src_dir ||
......
...@@ -109,11 +109,6 @@ static inline u8 mode_to_type(umode_t mode) ...@@ -109,11 +109,6 @@ static inline u8 mode_to_type(umode_t mode)
return (mode >> 12) & 15; return (mode >> 12) & 15;
} }
static inline unsigned nlink_bias(umode_t mode)
{
return S_ISDIR(mode) ? 2 : 1;
}
static inline bool inode_attr_changing(struct bch_inode_info *dir, static inline bool inode_attr_changing(struct bch_inode_info *dir,
struct bch_inode_info *inode, struct bch_inode_info *inode,
enum inode_opt_id id) enum inode_opt_id id)
......
...@@ -1116,9 +1116,7 @@ static int check_inode_nlink(struct bch_fs *c, ...@@ -1116,9 +1116,7 @@ static int check_inode_nlink(struct bch_fs *c,
struct nlink *link, struct nlink *link,
bool *do_update) bool *do_update)
{ {
u32 i_nlink = u->bi_flags & BCH_INODE_UNLINKED u32 i_nlink = bch2_inode_nlink_get(u);
? 0
: u->bi_nlink + nlink_bias(u->bi_mode);
u32 real_i_nlink = u32 real_i_nlink =
link->count * nlink_bias(u->bi_mode) + link->count * nlink_bias(u->bi_mode) +
link->dir_count; link->dir_count;
...@@ -1197,14 +1195,7 @@ static int check_inode_nlink(struct bch_fs *c, ...@@ -1197,14 +1195,7 @@ static int check_inode_nlink(struct bch_fs *c,
u->bi_inum, i_nlink, real_i_nlink); u->bi_inum, i_nlink, real_i_nlink);
set_i_nlink: set_i_nlink:
if (i_nlink != real_i_nlink) { if (i_nlink != real_i_nlink) {
if (real_i_nlink) { bch2_inode_nlink_set(u, real_i_nlink);
u->bi_nlink = real_i_nlink - nlink_bias(u->bi_mode);
u->bi_flags &= ~BCH_INODE_UNLINKED;
} else {
u->bi_nlink = 0;
u->bi_flags |= BCH_INODE_UNLINKED;
}
*do_update = true; *do_update = true;
} }
fsck_err: fsck_err:
......
...@@ -103,6 +103,49 @@ static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode, ...@@ -103,6 +103,49 @@ static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
} }
} }
/* i_nlink: */
static inline unsigned nlink_bias(umode_t mode)
{
return S_ISDIR(mode) ? 2 : 1;
}
static inline void bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
{
if (bi->bi_flags & BCH_INODE_UNLINKED)
bi->bi_flags &= ~BCH_INODE_UNLINKED;
else
bi->bi_nlink++;
}
static inline void bch2_inode_nlink_dec(struct bch_inode_unpacked *bi)
{
BUG_ON(bi->bi_flags & BCH_INODE_UNLINKED);
if (bi->bi_nlink)
bi->bi_nlink--;
else
bi->bi_flags |= BCH_INODE_UNLINKED;
}
static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
{
return bi->bi_flags & BCH_INODE_UNLINKED
? 0
: bi->bi_nlink + nlink_bias(bi->bi_mode);
}
static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
unsigned nlink)
{
if (nlink) {
bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
bi->bi_flags &= ~BCH_INODE_UNLINKED;
} else {
bi->bi_nlink = 0;
bi->bi_flags |= BCH_INODE_UNLINKED;
}
}
#ifdef CONFIG_BCACHEFS_DEBUG #ifdef CONFIG_BCACHEFS_DEBUG
void bch2_inode_pack_test(void); void bch2_inode_pack_test(void);
#else #else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment