summaryrefslogtreecommitdiff
path: root/fs/bcachefs/inode.h
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2019-09-25 23:19:52 +0300
committerKent Overstreet <kent.overstreet@linux.dev>2023-10-23 00:08:28 +0300
commitb43a0f60a61e8e0adea6b1b9adc9a97600fc2f00 (patch)
treed8a331f8a1200e116dbb369b3d1f4bd212d8ddbb /fs/bcachefs/inode.h
parentd55460bb099592ccec816afb5e662896ec5fffaa (diff)
downloadlinux-b43a0f60a61e8e0adea6b1b9adc9a97600fc2f00.tar.xz
bcachefs: Cleanup i_nlink handling
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs/bcachefs/inode.h')
-rw-r--r--fs/bcachefs/inode.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/fs/bcachefs/inode.h b/fs/bcachefs/inode.h
index af0c355f2f04..e88ec78071bd 100644
--- a/fs/bcachefs/inode.h
+++ b/fs/bcachefs/inode.h
@@ -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
void bch2_inode_pack_test(void);
#else