1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
7 extern const char * const bch2_inode_opts[];
9 const char *bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c);
10 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
12 #define bch2_bkey_ops_inode (struct bkey_ops) { \
13 .key_invalid = bch2_inode_invalid, \
14 .val_to_text = bch2_inode_to_text, \
17 const char *bch2_inode_generation_invalid(const struct bch_fs *,
19 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *,
22 #define bch2_bkey_ops_inode_generation (struct bkey_ops) { \
23 .key_invalid = bch2_inode_generation_invalid, \
24 .val_to_text = bch2_inode_generation_to_text, \
31 } __packed __aligned(4) u96;
35 struct bch_inode_unpacked {
41 #define x(_name, _bits) u##_bits _name;
46 struct bkey_inode_buf {
47 struct bkey_i_inode inode;
49 #define x(_name, _bits) + 8 + _bits / 8
50 u8 _pad[0 + BCH_INODE_FIELDS()];
52 } __attribute__((packed, aligned(8)));
54 void bch2_inode_pack(struct bch_fs *, struct bkey_inode_buf *,
55 const struct bch_inode_unpacked *);
56 int bch2_inode_unpack(struct bkey_s_c_inode, struct bch_inode_unpacked *);
58 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
60 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
61 struct bch_inode_unpacked *, u64, unsigned);
62 int bch2_inode_write(struct btree_trans *, struct btree_iter *,
63 struct bch_inode_unpacked *);
65 void bch2_inode_init_early(struct bch_fs *,
66 struct bch_inode_unpacked *);
67 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
68 uid_t, gid_t, umode_t, dev_t,
69 struct bch_inode_unpacked *);
70 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
71 uid_t, gid_t, umode_t, dev_t,
72 struct bch_inode_unpacked *);
74 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
75 struct bch_inode_unpacked *, u32, u64);
77 int bch2_inode_rm(struct bch_fs *, u64, bool);
79 int bch2_inode_find_by_inum(struct bch_fs *, u64, struct bch_inode_unpacked *);
81 static inline struct bch_io_opts bch2_inode_opts_get(struct bch_inode_unpacked *inode)
83 struct bch_io_opts ret = { 0 };
85 #define x(_name, _bits) \
86 if (inode->bi_##_name) \
87 opt_set(ret, _name, inode->bi_##_name - 1);
93 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
94 enum inode_opt_id id, u64 v)
97 #define x(_name, ...) \
98 case Inode_opt_##_name: \
99 inode->bi_##_name = v; \
108 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
109 enum inode_opt_id id)
112 #define x(_name, ...) \
113 case Inode_opt_##_name: \
114 return inode->bi_##_name;
122 static inline struct bch_io_opts
123 io_opts(struct bch_fs *c, struct bch_inode_unpacked *inode)
125 struct bch_io_opts opts = bch2_opts_to_inode_opts(c->opts);
127 bch2_io_opts_apply(&opts, bch2_inode_opts_get(inode));
131 static inline u8 mode_to_type(umode_t mode)
133 return (mode >> 12) & 15;
138 static inline unsigned nlink_bias(umode_t mode)
140 return S_ISDIR(mode) ? 2 : 1;
143 static inline void bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
145 if (bi->bi_flags & BCH_INODE_UNLINKED)
146 bi->bi_flags &= ~BCH_INODE_UNLINKED;
151 static inline void bch2_inode_nlink_dec(struct bch_inode_unpacked *bi)
153 BUG_ON(bi->bi_flags & BCH_INODE_UNLINKED);
157 bi->bi_flags |= BCH_INODE_UNLINKED;
160 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
162 return bi->bi_flags & BCH_INODE_UNLINKED
164 : bi->bi_nlink + nlink_bias(bi->bi_mode);
167 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
171 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
172 bi->bi_flags &= ~BCH_INODE_UNLINKED;
175 bi->bi_flags |= BCH_INODE_UNLINKED;
179 #endif /* _BCACHEFS_INODE_H */