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(struct btree_trans *, struct btree_iter *,
99 struct bch_inode_unpacked *, subvol_inum, unsigned);
101 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
102 struct bch_inode_unpacked *, enum btree_update_flags);
104 static inline int bch2_inode_write(struct btree_trans *trans,
105 struct btree_iter *iter,
106 struct bch_inode_unpacked *inode)
108 return bch2_inode_write_flags(trans, iter, inode, 0);
111 void bch2_inode_init_early(struct bch_fs *,
112 struct bch_inode_unpacked *);
113 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
114 uid_t, gid_t, umode_t, dev_t,
115 struct bch_inode_unpacked *);
116 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
117 uid_t, gid_t, umode_t, dev_t,
118 struct bch_inode_unpacked *);
120 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
121 struct bch_inode_unpacked *, u32, u64);
123 int bch2_inode_rm(struct bch_fs *, subvol_inum);
125 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
127 struct bch_inode_unpacked *);
128 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
129 struct bch_inode_unpacked *);
130 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
131 struct bch_inode_unpacked *);
133 #define inode_opt_get(_c, _inode, _name) \
134 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
136 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
137 enum inode_opt_id id, u64 v)
140 #define x(_name, ...) \
141 case Inode_opt_##_name: \
142 inode->bi_##_name = v; \
151 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
152 enum inode_opt_id id)
155 #define x(_name, ...) \
156 case Inode_opt_##_name: \
157 return inode->bi_##_name;
165 static inline u8 mode_to_type(umode_t mode)
167 return (mode >> 12) & 15;
170 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
172 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
177 static inline unsigned nlink_bias(umode_t mode)
179 return S_ISDIR(mode) ? 2 : 1;
182 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
184 return bi->bi_flags & BCH_INODE_unlinked
186 : bi->bi_nlink + nlink_bias(bi->bi_mode);
189 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
193 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
194 bi->bi_flags &= ~BCH_INODE_unlinked;
197 bi->bi_flags |= BCH_INODE_unlinked;
201 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
202 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
204 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
205 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
206 struct bch_inode_unpacked *);
207 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
209 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
210 int bch2_delete_dead_inodes(struct bch_fs *);
212 #endif /* _BCACHEFS_INODE_H */