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 int bch2_inode_invalid(const struct bch_fs *, struct bkey_s_c, int, struct printbuf *);
10 int bch2_inode_v2_invalid(const struct bch_fs *, struct bkey_s_c, int, struct printbuf *);
11 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
13 #define bch2_bkey_ops_inode (struct bkey_ops) { \
14 .key_invalid = bch2_inode_invalid, \
15 .val_to_text = bch2_inode_to_text, \
16 .trans_trigger = bch2_trans_mark_inode, \
17 .atomic_trigger = bch2_mark_inode, \
20 #define bch2_bkey_ops_inode_v2 (struct bkey_ops) { \
21 .key_invalid = bch2_inode_v2_invalid, \
22 .val_to_text = bch2_inode_to_text, \
23 .trans_trigger = bch2_trans_mark_inode, \
24 .atomic_trigger = bch2_mark_inode, \
27 static inline bool bkey_is_inode(const struct bkey *k)
29 return k->type == KEY_TYPE_inode ||
30 k->type == KEY_TYPE_inode_v2;
33 int bch2_inode_generation_invalid(const struct bch_fs *, struct bkey_s_c,
34 int, struct printbuf *);
35 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
37 #define bch2_bkey_ops_inode_generation (struct bkey_ops) { \
38 .key_invalid = bch2_inode_generation_invalid, \
39 .val_to_text = bch2_inode_generation_to_text, \
46 } __packed __aligned(4) u96;
50 struct bch_inode_unpacked {
57 #define x(_name, _bits) u##_bits _name;
62 struct bkey_inode_buf {
63 struct bkey_i_inode_v2 inode;
65 #define x(_name, _bits) + 8 + _bits / 8
66 u8 _pad[0 + BCH_INODE_FIELDS()];
68 } __attribute__((packed, aligned(8)));
70 void bch2_inode_pack(struct bch_fs *, struct bkey_inode_buf *,
71 const struct bch_inode_unpacked *);
72 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
74 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
76 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
77 struct bch_inode_unpacked *, subvol_inum, unsigned);
78 int bch2_inode_write(struct btree_trans *, struct btree_iter *,
79 struct bch_inode_unpacked *);
81 void bch2_inode_init_early(struct bch_fs *,
82 struct bch_inode_unpacked *);
83 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
84 uid_t, gid_t, umode_t, dev_t,
85 struct bch_inode_unpacked *);
86 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
87 uid_t, gid_t, umode_t, dev_t,
88 struct bch_inode_unpacked *);
90 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
91 struct bch_inode_unpacked *, u32, u64);
93 int bch2_inode_rm(struct bch_fs *, subvol_inum);
95 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
96 struct bch_inode_unpacked *);
97 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
98 struct bch_inode_unpacked *);
100 static inline struct bch_io_opts bch2_inode_opts_get(struct bch_inode_unpacked *inode)
102 struct bch_io_opts ret = { 0 };
104 #define x(_name, _bits) \
105 if (inode->bi_##_name) \
106 opt_set(ret, _name, inode->bi_##_name - 1);
112 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
113 enum inode_opt_id id, u64 v)
116 #define x(_name, ...) \
117 case Inode_opt_##_name: \
118 inode->bi_##_name = v; \
127 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
128 enum inode_opt_id id)
131 #define x(_name, ...) \
132 case Inode_opt_##_name: \
133 return inode->bi_##_name;
141 static inline struct bch_io_opts
142 io_opts(struct bch_fs *c, struct bch_inode_unpacked *inode)
144 struct bch_io_opts opts = bch2_opts_to_inode_opts(c->opts);
146 bch2_io_opts_apply(&opts, bch2_inode_opts_get(inode));
150 static inline u8 mode_to_type(umode_t mode)
152 return (mode >> 12) & 15;
155 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
157 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
162 static inline unsigned nlink_bias(umode_t mode)
164 return S_ISDIR(mode) ? 2 : 1;
167 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
169 return bi->bi_flags & BCH_INODE_UNLINKED
171 : bi->bi_nlink + nlink_bias(bi->bi_mode);
174 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
178 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
179 bi->bi_flags &= ~BCH_INODE_UNLINKED;
182 bi->bi_flags |= BCH_INODE_UNLINKED;
186 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
187 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
189 #endif /* _BCACHEFS_INODE_H */