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_trans_mark_inode(struct btree_trans *, enum btree_id, unsigned,
21 struct bkey_s_c, struct bkey_i *, unsigned);
22 int bch2_mark_inode(struct btree_trans *, enum btree_id, unsigned,
23 struct bkey_s_c, struct bkey_s_c, unsigned);
25 #define bch2_bkey_ops_inode ((struct bkey_ops) { \
26 .key_invalid = bch2_inode_invalid, \
27 .val_to_text = bch2_inode_to_text, \
28 .trans_trigger = bch2_trans_mark_inode, \
29 .atomic_trigger = bch2_mark_inode, \
33 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
34 .key_invalid = bch2_inode_v2_invalid, \
35 .val_to_text = bch2_inode_to_text, \
36 .trans_trigger = bch2_trans_mark_inode, \
37 .atomic_trigger = bch2_mark_inode, \
41 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
42 .key_invalid = bch2_inode_v3_invalid, \
43 .val_to_text = bch2_inode_to_text, \
44 .trans_trigger = bch2_trans_mark_inode, \
45 .atomic_trigger = bch2_mark_inode, \
49 static inline bool bkey_is_inode(const struct bkey *k)
51 return k->type == KEY_TYPE_inode ||
52 k->type == KEY_TYPE_inode_v2 ||
53 k->type == KEY_TYPE_inode_v3;
56 int bch2_inode_generation_invalid(struct bch_fs *, struct bkey_s_c,
57 enum bkey_invalid_flags, struct printbuf *);
58 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
60 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
61 .key_invalid = bch2_inode_generation_invalid, \
62 .val_to_text = bch2_inode_generation_to_text, \
70 } __packed __aligned(4) u96;
74 struct bch_inode_unpacked {
84 #define x(_name, _bits) u##_bits _name;
89 struct bkey_inode_buf {
90 struct bkey_i_inode_v3 inode;
92 #define x(_name, _bits) + 8 + _bits / 8
93 u8 _pad[0 + BCH_INODE_FIELDS_v3()];
95 } __packed __aligned(8);
97 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
98 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
99 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
101 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
103 int bch2_inode_peek(struct btree_trans *, struct btree_iter *,
104 struct bch_inode_unpacked *, subvol_inum, unsigned);
106 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
107 struct bch_inode_unpacked *, enum btree_update_flags);
109 static inline int bch2_inode_write(struct btree_trans *trans,
110 struct btree_iter *iter,
111 struct bch_inode_unpacked *inode)
113 return bch2_inode_write_flags(trans, iter, inode, 0);
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);
182 static inline unsigned nlink_bias(umode_t mode)
184 return S_ISDIR(mode) ? 2 : 1;
187 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
189 return bi->bi_flags & BCH_INODE_unlinked
191 : bi->bi_nlink + nlink_bias(bi->bi_mode);
194 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
198 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
199 bi->bi_flags &= ~BCH_INODE_unlinked;
202 bi->bi_flags |= BCH_INODE_unlinked;
206 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
207 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
209 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
210 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
211 struct bch_inode_unpacked *);
212 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
214 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
215 int bch2_delete_dead_inodes(struct bch_fs *);
217 #endif /* _BCACHEFS_INODE_H */