Linux 6.13-rc4
[linux.git] / fs / bcachefs / inode.h
blobeab82b5eb8977d8c6e8120b55fce870f8ffec26a
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
5 #include "bkey.h"
6 #include "bkey_methods.h"
7 #include "opts.h"
8 #include "snapshot.h"
10 enum bch_validate_flags;
11 extern const char * const bch2_inode_opts[];
13 int bch2_inode_validate(struct bch_fs *, struct bkey_s_c,
14 enum bch_validate_flags);
15 int bch2_inode_v2_validate(struct bch_fs *, struct bkey_s_c,
16 enum bch_validate_flags);
17 int bch2_inode_v3_validate(struct bch_fs *, struct bkey_s_c,
18 enum bch_validate_flags);
19 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
21 int __bch2_inode_has_child_snapshots(struct btree_trans *, struct bpos);
23 static inline int bch2_inode_has_child_snapshots(struct btree_trans *trans, struct bpos pos)
25 return bch2_snapshot_is_leaf(trans->c, pos.snapshot) <= 0
26 ? __bch2_inode_has_child_snapshots(trans, pos)
27 : 0;
30 int bch2_trigger_inode(struct btree_trans *, enum btree_id, unsigned,
31 struct bkey_s_c, struct bkey_s,
32 enum btree_iter_update_trigger_flags);
34 #define bch2_bkey_ops_inode ((struct bkey_ops) { \
35 .key_validate = bch2_inode_validate, \
36 .val_to_text = bch2_inode_to_text, \
37 .trigger = bch2_trigger_inode, \
38 .min_val_size = 16, \
41 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) { \
42 .key_validate = bch2_inode_v2_validate, \
43 .val_to_text = bch2_inode_to_text, \
44 .trigger = bch2_trigger_inode, \
45 .min_val_size = 32, \
48 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) { \
49 .key_validate = bch2_inode_v3_validate, \
50 .val_to_text = bch2_inode_to_text, \
51 .trigger = bch2_trigger_inode, \
52 .min_val_size = 48, \
55 static inline bool bkey_is_inode(const struct bkey *k)
57 return k->type == KEY_TYPE_inode ||
58 k->type == KEY_TYPE_inode_v2 ||
59 k->type == KEY_TYPE_inode_v3;
62 int bch2_inode_generation_validate(struct bch_fs *, struct bkey_s_c,
63 enum bch_validate_flags);
64 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
66 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) { \
67 .key_validate = bch2_inode_generation_validate, \
68 .val_to_text = bch2_inode_generation_to_text, \
69 .min_val_size = 8, \
72 #if 0
73 typedef struct {
74 u64 lo;
75 u32 hi;
76 } __packed __aligned(4) u96;
77 #endif
78 typedef u64 u96;
80 struct bch_inode_unpacked {
81 u64 bi_inum;
82 u32 bi_snapshot;
83 u64 bi_journal_seq;
84 __le64 bi_hash_seed;
85 u64 bi_size;
86 u64 bi_sectors;
87 u64 bi_version;
88 u32 bi_flags;
89 u16 bi_mode;
91 #define x(_name, _bits) u##_bits _name;
92 BCH_INODE_FIELDS_v3()
93 #undef x
95 BITMASK(INODE_STR_HASH, struct bch_inode_unpacked, bi_flags, 20, 24);
97 struct bkey_inode_buf {
98 struct bkey_i_inode_v3 inode;
100 #define x(_name, _bits) + 8 + _bits / 8
101 u8 _pad[0 + BCH_INODE_FIELDS_v3()];
102 #undef x
105 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
106 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
107 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
109 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
111 int __bch2_inode_peek(struct btree_trans *, struct btree_iter *,
112 struct bch_inode_unpacked *, subvol_inum, unsigned, bool);
114 static inline int bch2_inode_peek_nowarn(struct btree_trans *trans,
115 struct btree_iter *iter,
116 struct bch_inode_unpacked *inode,
117 subvol_inum inum, unsigned flags)
119 return __bch2_inode_peek(trans, iter, inode, inum, flags, false);
122 static inline int bch2_inode_peek(struct btree_trans *trans,
123 struct btree_iter *iter,
124 struct bch_inode_unpacked *inode,
125 subvol_inum inum, unsigned flags)
127 return __bch2_inode_peek(trans, iter, inode, inum, flags, true);
128 int ret = bch2_inode_peek_nowarn(trans, iter, inode, inum, flags);
129 return ret;
132 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
133 struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags);
135 static inline int bch2_inode_write(struct btree_trans *trans,
136 struct btree_iter *iter,
137 struct bch_inode_unpacked *inode)
139 return bch2_inode_write_flags(trans, iter, inode, 0);
142 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
143 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
145 void bch2_inode_init_early(struct bch_fs *,
146 struct bch_inode_unpacked *);
147 void bch2_inode_init_late(struct bch_inode_unpacked *, u64,
148 uid_t, gid_t, umode_t, dev_t,
149 struct bch_inode_unpacked *);
150 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
151 uid_t, gid_t, umode_t, dev_t,
152 struct bch_inode_unpacked *);
154 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
155 struct bch_inode_unpacked *, u32, u64);
157 int bch2_inode_rm(struct bch_fs *, subvol_inum);
159 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
160 subvol_inum,
161 struct bch_inode_unpacked *);
162 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
163 struct bch_inode_unpacked *);
164 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
165 struct bch_inode_unpacked *);
167 #define inode_opt_get(_c, _inode, _name) \
168 ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
170 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
171 enum inode_opt_id id, u64 v)
173 switch (id) {
174 #define x(_name, ...) \
175 case Inode_opt_##_name: \
176 inode->bi_##_name = v; \
177 break;
178 BCH_INODE_OPTS()
179 #undef x
180 default:
181 BUG();
185 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
186 enum inode_opt_id id)
188 switch (id) {
189 #define x(_name, ...) \
190 case Inode_opt_##_name: \
191 return inode->bi_##_name;
192 BCH_INODE_OPTS()
193 #undef x
194 default:
195 BUG();
199 static inline u8 mode_to_type(umode_t mode)
201 return (mode >> 12) & 15;
204 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
206 return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
209 static inline u32 bch2_inode_flags(struct bkey_s_c k)
211 switch (k.k->type) {
212 case KEY_TYPE_inode:
213 return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
214 case KEY_TYPE_inode_v2:
215 return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
216 case KEY_TYPE_inode_v3:
217 return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
218 default:
219 return 0;
223 /* i_nlink: */
225 static inline unsigned nlink_bias(umode_t mode)
227 return S_ISDIR(mode) ? 2 : 1;
230 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
232 return bi->bi_flags & BCH_INODE_unlinked
234 : bi->bi_nlink + nlink_bias(bi->bi_mode);
237 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
238 unsigned nlink)
240 if (nlink) {
241 bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
242 bi->bi_flags &= ~BCH_INODE_unlinked;
243 } else {
244 bi->bi_nlink = 0;
245 bi->bi_flags |= BCH_INODE_unlinked;
249 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
250 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
252 static inline bool bch2_inode_should_have_bp(struct bch_inode_unpacked *inode)
254 bool inode_has_bp = inode->bi_dir || inode->bi_dir_offset;
256 return S_ISDIR(inode->bi_mode) ||
257 (!inode->bi_nlink && inode_has_bp);
260 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
261 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
262 struct bch_inode_unpacked *);
263 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
265 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
266 int bch2_delete_dead_inodes(struct bch_fs *);
268 #endif /* _BCACHEFS_INODE_H */