1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
6 #include "bkey_methods.h"
9 enum bkey_invalid_flags;
10 extern const char * const bch2_inode_opts[];
12 int bch2_inode_invalid(struct bch_fs *, struct bkey_s_c,
13 enum bkey_invalid_flags, struct printbuf *);
14 int bch2_inode_v2_invalid(struct bch_fs *, struct bkey_s_c,
15 enum bkey_invalid_flags, struct printbuf *);
16 int bch2_inode_v3_invalid(struct bch_fs *, struct bkey_s_c,
17 enum bkey_invalid_flags, struct printbuf *);
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, unsigned);
23 #define bch2_bkey_ops_inode ((struct bkey_ops) { \
24 .key_invalid = bch2_inode_invalid, \
25 .val_to_text = bch2_inode_to_text, \
26 .trigger = bch2_trigger_inode, \
30 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
31 .key_invalid = bch2_inode_v2_invalid, \
32 .val_to_text = bch2_inode_to_text, \
33 .trigger = bch2_trigger_inode, \
37 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
38 .key_invalid = bch2_inode_v3_invalid, \
39 .val_to_text = bch2_inode_to_text, \
40 .trigger = bch2_trigger_inode, \
44 static inline bool bkey_is_inode(const struct bkey *k)
46 return k->type == KEY_TYPE_inode ||
47 k->type == KEY_TYPE_inode_v2 ||
48 k->type == KEY_TYPE_inode_v3;
51 int bch2_inode_generation_invalid(struct bch_fs *, struct bkey_s_c,
52 enum bkey_invalid_flags, struct printbuf *);
53 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
55 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
56 .key_invalid = bch2_inode_generation_invalid, \
57 .val_to_text = bch2_inode_generation_to_text, \
65 } __packed __aligned(4) u96;
69 struct bch_inode_unpacked {
79 #define x(_name, _bits) u##_bits _name;
84 struct bkey_inode_buf {
85 struct bkey_i_inode_v3 inode;
87 #define x(_name, _bits) + 8 + _bits / 8
88 u8 _pad[0 + BCH_INODE_FIELDS_v3()];
90 } __packed __aligned(8);
92 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
93 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
94 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
96 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
98 int bch2_inode_peek_nowarn(struct btree_trans *, struct btree_iter *,
99 struct bch_inode_unpacked *, subvol_inum, unsigned);
100 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
101 struct bch_inode_unpacked *, subvol_inum, unsigned);
103 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
104 struct bch_inode_unpacked *, enum btree_update_flags);
106 static inline int bch2_inode_write(struct btree_trans *trans,
107 struct btree_iter *iter,
108 struct bch_inode_unpacked *inode)
110 return bch2_inode_write_flags(trans, iter, inode, 0);
113 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
114 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *, u32);
116 void bch2_inode_init_early(struct bch_fs *,
117 struct bch_inode_unpacked *);
118 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
119 uid_t, gid_t, umode_t, dev_t,
120 struct bch_inode_unpacked *);
121 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
122 uid_t, gid_t, umode_t, dev_t,
123 struct bch_inode_unpacked *);
125 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
126 struct bch_inode_unpacked *, u32, u64);
128 int bch2_inode_rm(struct bch_fs *, subvol_inum);
130 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
132 struct bch_inode_unpacked *);
133 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
134 struct bch_inode_unpacked *);
135 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
136 struct bch_inode_unpacked *);
138 #define inode_opt_get(_c, _inode, _name) \
139 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
141 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
142 enum inode_opt_id id, u64 v)
145 #define x(_name, ...) \
146 case Inode_opt_##_name: \
147 inode->bi_##_name = v; \
156 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
157 enum inode_opt_id id)
160 #define x(_name, ...) \
161 case Inode_opt_##_name: \
162 return inode->bi_##_name;
170 static inline u8 mode_to_type(umode_t mode)
172 return (mode >> 12) & 15;
175 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
177 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
180 static inline u32 bch2_inode_flags(struct bkey_s_c k)
184 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
185 case KEY_TYPE_inode_v2:
186 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
187 case KEY_TYPE_inode_v3:
188 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
196 static inline unsigned nlink_bias(umode_t mode)
198 return S_ISDIR(mode) ? 2 : 1;
201 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
203 return bi->bi_flags & BCH_INODE_unlinked
205 : bi->bi_nlink + nlink_bias(bi->bi_mode);
208 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
212 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
213 bi->bi_flags &= ~BCH_INODE_unlinked;
216 bi->bi_flags |= BCH_INODE_unlinked;
220 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
221 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
223 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
224 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
225 struct bch_inode_unpacked *);
226 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
228 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
229 int bch2_delete_dead_inodes(struct bch_fs *);
231 #endif /* _BCACHEFS_INODE_H */