xref: /linux/fs/bcachefs/inode.h (revision 6f2a71a99ebd5dfaa7948a2e9c59eae94b741bd8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_INODE_H
3 #define _BCACHEFS_INODE_H
4 
5 #include "bkey.h"
6 #include "bkey_methods.h"
7 #include "opts.h"
8 #include "snapshot.h"
9 
10 extern const char * const bch2_inode_opts[];
11 
12 int bch2_inode_validate(struct bch_fs *, struct bkey_s_c,
13 			struct bkey_validate_context);
14 int bch2_inode_v2_validate(struct bch_fs *, struct bkey_s_c,
15 			   struct bkey_validate_context);
16 int bch2_inode_v3_validate(struct bch_fs *, struct bkey_s_c,
17 			   struct bkey_validate_context);
18 void bch2_inode_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
19 
20 int __bch2_inode_has_child_snapshots(struct btree_trans *, struct bpos);
21 
bch2_inode_has_child_snapshots(struct btree_trans * trans,struct bpos pos)22 static inline int bch2_inode_has_child_snapshots(struct btree_trans *trans, struct bpos pos)
23 {
24 	return bch2_snapshot_is_leaf(trans->c, pos.snapshot) <= 0
25 		? __bch2_inode_has_child_snapshots(trans, pos)
26 		: 0;
27 }
28 
29 int bch2_trigger_inode(struct btree_trans *, enum btree_id, unsigned,
30 		       struct bkey_s_c, struct bkey_s,
31 		       enum btree_iter_update_trigger_flags);
32 
33 #define bch2_bkey_ops_inode ((struct bkey_ops) {	\
34 	.key_validate	= bch2_inode_validate,		\
35 	.val_to_text	= bch2_inode_to_text,		\
36 	.trigger	= bch2_trigger_inode,		\
37 	.min_val_size	= 16,				\
38 })
39 
40 #define bch2_bkey_ops_inode_v2 ((struct bkey_ops) {	\
41 	.key_validate	= bch2_inode_v2_validate,	\
42 	.val_to_text	= bch2_inode_to_text,		\
43 	.trigger	= bch2_trigger_inode,		\
44 	.min_val_size	= 32,				\
45 })
46 
47 #define bch2_bkey_ops_inode_v3 ((struct bkey_ops) {	\
48 	.key_validate	= bch2_inode_v3_validate,	\
49 	.val_to_text	= bch2_inode_to_text,		\
50 	.trigger	= bch2_trigger_inode,		\
51 	.min_val_size	= 48,				\
52 })
53 
bkey_is_inode(const struct bkey * k)54 static inline bool bkey_is_inode(const struct bkey *k)
55 {
56 	return  k->type == KEY_TYPE_inode ||
57 		k->type == KEY_TYPE_inode_v2 ||
58 		k->type == KEY_TYPE_inode_v3;
59 }
60 
61 int bch2_inode_generation_validate(struct bch_fs *, struct bkey_s_c,
62 				   struct bkey_validate_context);
63 void bch2_inode_generation_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
64 
65 #define bch2_bkey_ops_inode_generation ((struct bkey_ops) {	\
66 	.key_validate	= bch2_inode_generation_validate,	\
67 	.val_to_text	= bch2_inode_generation_to_text,	\
68 	.min_val_size	= 8,					\
69 })
70 
71 int bch2_inode_alloc_cursor_validate(struct bch_fs *, struct bkey_s_c,
72 				     struct bkey_validate_context);
73 void bch2_inode_alloc_cursor_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
74 
75 #define bch2_bkey_ops_inode_alloc_cursor ((struct bkey_ops) {	\
76 	.key_validate	= bch2_inode_alloc_cursor_validate,	\
77 	.val_to_text	= bch2_inode_alloc_cursor_to_text,	\
78 	.min_val_size	= 16,					\
79 })
80 
81 #if 0
82 typedef struct {
83 	u64			lo;
84 	u32			hi;
85 } __packed __aligned(4) u96;
86 #endif
87 typedef u64 u96;
88 
89 struct bch_inode_unpacked {
90 	u64			bi_inum;
91 	u32			bi_snapshot;
92 	u64			bi_journal_seq;
93 	__le64			bi_hash_seed;
94 	u64			bi_size;
95 	u64			bi_sectors;
96 	u64			bi_version;
97 	u32			bi_flags;
98 	u16			bi_mode;
99 
100 #define x(_name, _bits)	u##_bits _name;
101 	BCH_INODE_FIELDS_v3()
102 #undef  x
103 };
104 BITMASK(INODE_STR_HASH,	struct bch_inode_unpacked, bi_flags, 20, 24);
105 
106 struct bkey_inode_buf {
107 	struct bkey_i_inode_v3	inode;
108 
109 #define x(_name, _bits)		+ 8 + _bits / 8
110 	u8		_pad[0 + BCH_INODE_FIELDS_v3()];
111 #undef  x
112 };
113 
114 void bch2_inode_pack(struct bkey_inode_buf *, const struct bch_inode_unpacked *);
115 int bch2_inode_unpack(struct bkey_s_c, struct bch_inode_unpacked *);
116 struct bkey_i *bch2_inode_to_v3(struct btree_trans *, struct bkey_i *);
117 
118 void bch2_inode_unpacked_to_text(struct printbuf *, struct bch_inode_unpacked *);
119 
120 int __bch2_inode_peek(struct btree_trans *, struct btree_iter *,
121 		      struct bch_inode_unpacked *, subvol_inum, unsigned, bool);
122 
bch2_inode_peek_nowarn(struct btree_trans * trans,struct btree_iter * iter,struct bch_inode_unpacked * inode,subvol_inum inum,unsigned flags)123 static inline int bch2_inode_peek_nowarn(struct btree_trans *trans,
124 					 struct btree_iter *iter,
125 					 struct bch_inode_unpacked *inode,
126 					 subvol_inum inum, unsigned flags)
127 {
128 	return __bch2_inode_peek(trans, iter, inode, inum, flags, false);
129 }
130 
bch2_inode_peek(struct btree_trans * trans,struct btree_iter * iter,struct bch_inode_unpacked * inode,subvol_inum inum,unsigned flags)131 static inline int bch2_inode_peek(struct btree_trans *trans,
132 				  struct btree_iter *iter,
133 				  struct bch_inode_unpacked *inode,
134 				  subvol_inum inum, unsigned flags)
135 {
136 	return __bch2_inode_peek(trans, iter, inode, inum, flags, true);
137 }
138 
139 int bch2_inode_find_by_inum_snapshot(struct btree_trans *, u64, u32,
140 				     struct bch_inode_unpacked *, unsigned);
141 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *,
142 				  subvol_inum,
143 				  struct bch_inode_unpacked *);
144 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
145 				  struct bch_inode_unpacked *);
146 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
147 			    struct bch_inode_unpacked *);
148 
149 int bch2_inode_find_snapshot_root(struct btree_trans *trans, u64 inum,
150 				  struct bch_inode_unpacked *root);
151 
152 int bch2_inode_write_flags(struct btree_trans *, struct btree_iter *,
153 		     struct bch_inode_unpacked *, enum btree_iter_update_trigger_flags);
154 
bch2_inode_write(struct btree_trans * trans,struct btree_iter * iter,struct bch_inode_unpacked * inode)155 static inline int bch2_inode_write(struct btree_trans *trans,
156 		     struct btree_iter *iter,
157 		     struct bch_inode_unpacked *inode)
158 {
159 	return bch2_inode_write_flags(trans, iter, inode, 0);
160 }
161 
162 int __bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
163 int bch2_fsck_write_inode(struct btree_trans *, struct bch_inode_unpacked *);
164 
165 void bch2_inode_init_early(struct bch_fs *,
166 			   struct bch_inode_unpacked *);
167 void bch2_inode_init_late(struct bch_fs *, struct bch_inode_unpacked *, u64,
168 			  uid_t, gid_t, umode_t, dev_t,
169 			  struct bch_inode_unpacked *);
170 void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
171 		     uid_t, gid_t, umode_t, dev_t,
172 		     struct bch_inode_unpacked *);
173 
174 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
175 		      struct bch_inode_unpacked *, u32, u64);
176 
177 int bch2_inode_rm(struct bch_fs *, subvol_inum);
178 
179 #define inode_opt_get(_c, _inode, _name)			\
180 	((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
181 
bch2_inode_opt_set(struct bch_inode_unpacked * inode,enum inode_opt_id id,u64 v)182 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
183 				      enum inode_opt_id id, u64 v)
184 {
185 	switch (id) {
186 #define x(_name, ...)							\
187 	case Inode_opt_##_name:						\
188 		inode->bi_##_name = v;					\
189 		break;
190 	BCH_INODE_OPTS()
191 #undef x
192 	default:
193 		BUG();
194 	}
195 }
196 
bch2_inode_opt_get(struct bch_inode_unpacked * inode,enum inode_opt_id id)197 static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
198 				     enum inode_opt_id id)
199 {
200 	switch (id) {
201 #define x(_name, ...)							\
202 	case Inode_opt_##_name:						\
203 		return inode->bi_##_name;
204 	BCH_INODE_OPTS()
205 #undef x
206 	default:
207 		BUG();
208 	}
209 }
210 
mode_to_type(umode_t mode)211 static inline u8 mode_to_type(umode_t mode)
212 {
213 	return (mode >> 12) & 15;
214 }
215 
inode_d_type(struct bch_inode_unpacked * inode)216 static inline u8 inode_d_type(struct bch_inode_unpacked *inode)
217 {
218 	return inode->bi_subvol ? DT_SUBVOL : mode_to_type(inode->bi_mode);
219 }
220 
bch2_inode_flags(struct bkey_s_c k)221 static inline u32 bch2_inode_flags(struct bkey_s_c k)
222 {
223 	switch (k.k->type) {
224 	case KEY_TYPE_inode:
225 		return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
226 	case KEY_TYPE_inode_v2:
227 		return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
228 	case KEY_TYPE_inode_v3:
229 		return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
230 	default:
231 		return 0;
232 	}
233 }
234 
bkey_inode_mode(struct bkey_s_c k)235 static inline unsigned bkey_inode_mode(struct bkey_s_c k)
236 {
237 	switch (k.k->type) {
238 	case KEY_TYPE_inode:
239 		return le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode);
240 	case KEY_TYPE_inode_v2:
241 		return le16_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_mode);
242 	case KEY_TYPE_inode_v3:
243 		return INODEv3_MODE(bkey_s_c_to_inode_v3(k).v);
244 	default:
245 		return 0;
246 	}
247 }
248 
bch2_inode_casefold(struct bch_fs * c,const struct bch_inode_unpacked * bi)249 static inline bool bch2_inode_casefold(struct bch_fs *c, const struct bch_inode_unpacked *bi)
250 {
251 	/* inode opts are stored with a +1 bias: 0 means "unset, use fs opt" */
252 	return bi->bi_casefold
253 		? bi->bi_casefold - 1
254 		: c->opts.casefold;
255 }
256 
bch2_inode_has_backpointer(const struct bch_inode_unpacked * bi)257 static inline bool bch2_inode_has_backpointer(const struct bch_inode_unpacked *bi)
258 {
259 	return bi->bi_dir || bi->bi_dir_offset;
260 }
261 
262 /* i_nlink: */
263 
nlink_bias(umode_t mode)264 static inline unsigned nlink_bias(umode_t mode)
265 {
266 	return S_ISDIR(mode) ? 2 : 1;
267 }
268 
bch2_inode_nlink_get(struct bch_inode_unpacked * bi)269 static inline unsigned bch2_inode_nlink_get(struct bch_inode_unpacked *bi)
270 {
271 	return bi->bi_flags & BCH_INODE_unlinked
272 		  ? 0
273 		  : bi->bi_nlink + nlink_bias(bi->bi_mode);
274 }
275 
bch2_inode_nlink_set(struct bch_inode_unpacked * bi,unsigned nlink)276 static inline void bch2_inode_nlink_set(struct bch_inode_unpacked *bi,
277 					unsigned nlink)
278 {
279 	if (nlink) {
280 		bi->bi_nlink = nlink - nlink_bias(bi->bi_mode);
281 		bi->bi_flags &= ~BCH_INODE_unlinked;
282 	} else {
283 		bi->bi_nlink = 0;
284 		bi->bi_flags |= BCH_INODE_unlinked;
285 	}
286 }
287 
288 int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
289 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
290 
291 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
292 void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
293 			 struct bch_inode_unpacked *);
294 int bch2_inum_opts_get(struct btree_trans *, subvol_inum, struct bch_io_opts *);
295 int bch2_inode_set_casefold(struct btree_trans *, subvol_inum,
296 			    struct bch_inode_unpacked *, unsigned);
297 
298 #include "rebalance.h"
299 
300 static inline struct bch_extent_rebalance
bch2_inode_rebalance_opts_get(struct bch_fs * c,struct bch_inode_unpacked * inode)301 bch2_inode_rebalance_opts_get(struct bch_fs *c, struct bch_inode_unpacked *inode)
302 {
303 	struct bch_io_opts io_opts;
304 	bch2_inode_opts_get(&io_opts, c, inode);
305 	return io_opts_to_rebalance_opts(c, &io_opts);
306 }
307 
308 #define BCACHEFS_ROOT_SUBVOL_INUM					\
309 	((subvol_inum) { BCACHEFS_ROOT_SUBVOL,	BCACHEFS_ROOT_INO })
310 
subvol_inum_eq(subvol_inum a,subvol_inum b)311 static inline bool subvol_inum_eq(subvol_inum a, subvol_inum b)
312 {
313 	return a.subvol == b.subvol && a.inum == b.inum;
314 }
315 
316 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
317 int bch2_delete_dead_inodes(struct bch_fs *);
318 
319 #endif /* _BCACHEFS_INODE_H */
320