1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
6 #include "bkey_methods.h"
9 enum bch_validate_flags;
10 extern const char * const bch2_inode_opts[];
12 int bch2_inode_validate(struct bch_fs *, struct bkey_s_c,
13 enum bch_validate_flags);
14 int bch2_inode_v2_validate(struct bch_fs *, struct bkey_s_c,
15 enum bch_validate_flags);
16 int bch2_inode_v3_validate(struct bch_fs *, struct bkey_s_c,
17 enum bch_validate_flags);
18 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
20 int bch2_trigger_inode(struct btree_trans *, enum btree_id, unsigned,
21 struct bkey_s_c, struct bkey_s,
22 enum btree_iter_update_trigger_flags);
24 #define bch2_bkey_ops_inode ((struct bkey_ops) { \
25 .key_validate = bch2_inode_validate, \
26 .val_to_text = bch2_inode_to_text, \
27 .trigger = bch2_trigger_inode, \
31 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
32 .key_validate = bch2_inode_v2_validate, \
33 .val_to_text = bch2_inode_to_text, \
34 .trigger = bch2_trigger_inode, \
38 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
39 .key_validate = bch2_inode_v3_validate, \
40 .val_to_text = bch2_inode_to_text, \
41 .trigger = bch2_trigger_inode, \
45 static inline bool bkey_is_inode(const struct bkey *k)
47 return k->type == KEY_TYPE_inode ||
48 k->type == KEY_TYPE_inode_v2 ||
49 k->type == KEY_TYPE_inode_v3;
52 int bch2_inode_generation_validate(struct bch_fs *, struct bkey_s_c,
53 enum bch_validate_flags);
54 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
56 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
57 .key_validate = bch2_inode_generation_validate, \
58 .val_to_text = bch2_inode_generation_to_text, \
66 } __packed __aligned(4) u96;
70 struct bch_inode_unpacked {
80 #define x(_name, _bits) u##_bits _name;
85 struct bkey_inode_buf {
86 struct bkey_i_inode_v3 inode;
88 #define x(_name, _bits) + 8 + _bits / 8
89 u8 _pad[0 + BCH_INODE_FIELDS_v3()];
93 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
94 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
95 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
97 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
99 int bch2_inode_peek_nowarn(struct btree_trans *, struct btree_iter *,
100 struct bch_inode_unpacked *, subvol_inum, unsigned);
101 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
102 struct bch_inode_unpacked *, subvol_inum, unsigned);
104 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
105 struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags);
107 static inline int bch2_inode_write(struct btree_trans *trans,
108 struct btree_iter *iter,
109 struct bch_inode_unpacked *inode)
111 return bch2_inode_write_flags(trans, iter, inode, 0);
114 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
115 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
117 void bch2_inode_init_early(struct bch_fs *,
118 struct bch_inode_unpacked *);
119 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
120 uid_t, gid_t, umode_t, dev_t,
121 struct bch_inode_unpacked *);
122 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
123 uid_t, gid_t, umode_t, dev_t,
124 struct bch_inode_unpacked *);
126 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
127 struct bch_inode_unpacked *, u32, u64);
129 int bch2_inode_rm(struct bch_fs *, subvol_inum);
131 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
133 struct bch_inode_unpacked *);
134 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
135 struct bch_inode_unpacked *);
136 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
137 struct bch_inode_unpacked *);
139 #define inode_opt_get(_c, _inode, _name) \
140 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
142 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
143 enum inode_opt_id id, u64 v)
146 #define x(_name, ...) \
147 case Inode_opt_##_name: \
148 inode->bi_##_name = v; \
157 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
158 enum inode_opt_id id)
161 #define x(_name, ...) \
162 case Inode_opt_##_name: \
163 return inode->bi_##_name;
171 static inline u8 mode_to_type(umode_t mode)
173 return (mode >> 12) & 15;
176 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
178 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
181 static inline u32 bch2_inode_flags(struct bkey_s_c k)
185 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
186 case KEY_TYPE_inode_v2:
187 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
188 case KEY_TYPE_inode_v3:
189 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
197 static inline unsigned nlink_bias(umode_t mode)
199 return S_ISDIR(mode) ? 2 : 1;
202 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
204 return bi->bi_flags & BCH_INODE_unlinked
206 : bi->bi_nlink + nlink_bias(bi->bi_mode);
209 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
213 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
214 bi->bi_flags &= ~BCH_INODE_unlinked;
217 bi->bi_flags |= BCH_INODE_unlinked;
221 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
222 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
224 static inline bool bch2_inode_should_have_bp(struct bch_inode_unpacked *inode)
226 bool inode_has_bp = inode->bi_dir || inode->bi_dir_offset;
228 return S_ISDIR(inode->bi_mode) ||
229 (!inode->bi_nlink && inode_has_bp);
232 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
233 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
234 struct bch_inode_unpacked *);
235 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
237 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
238 int bch2_delete_dead_inodes(struct bch_fs *);
240 #endif /* _BCACHEFS_INODE_H */