1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "btree_key_cache.h"
5 #include "btree_write_buffer.h"
6 #include "bkey_methods.h"
7 #include "btree_update.h"
8 #include "buckets.h"
9 #include "compress.h"
10 #include "dirent.h"
11 #include "disk_accounting.h"
12 #include "error.h"
13 #include "extents.h"
14 #include "extent_update.h"
15 #include "fs.h"
16 #include "inode.h"
17 #include "namei.h"
18 #include "opts.h"
19 #include "str_hash.h"
20 #include "snapshot.h"
21 #include "subvolume.h"
22 #include "varint.h"
23 
24 #include <linux/random.h>
25 
26 #include <linux/unaligned.h>
27 
28 #define x(name, ...)	#name,
29 const char * const bch2_inode_opts[] = {
30 	BCH_INODE_OPTS()
31 	NULL,
32 };
33 
34 static const char * const bch2_inode_flag_strs[] = {
35 	BCH_INODE_FLAGS()
36 	NULL
37 };
38 #undef  x
39 
40 static int delete_ancestor_snapshot_inodes(struct btree_trans *, struct bpos);
41 
42 static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
43 
inode_decode_field(const u8 * in,const u8 * end,u64 out[2],unsigned * out_bits)44 static int inode_decode_field(const u8 *in, const u8 *end,
45 			      u64 out[2], unsigned *out_bits)
46 {
47 	__be64 be[2] = { 0, 0 };
48 	unsigned bytes, shift;
49 	u8 *p;
50 
51 	if (in >= end)
52 		return -BCH_ERR_inode_unpack_error;
53 
54 	if (!*in)
55 		return -BCH_ERR_inode_unpack_error;
56 
57 	/*
58 	 * position of highest set bit indicates number of bytes:
59 	 * shift = number of bits to remove in high byte:
60 	 */
61 	shift	= 8 - __fls(*in); /* 1 <= shift <= 8 */
62 	bytes	= byte_table[shift - 1];
63 
64 	if (in + bytes > end)
65 		return -BCH_ERR_inode_unpack_error;
66 
67 	p = (u8 *) be + 16 - bytes;
68 	memcpy(p, in, bytes);
69 	*p ^= (1 << 8) >> shift;
70 
71 	out[0] = be64_to_cpu(be[0]);
72 	out[1] = be64_to_cpu(be[1]);
73 	*out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);
74 
75 	return bytes;
76 }
77 
bch2_inode_pack_inlined(struct bkey_inode_buf * packed,const struct bch_inode_unpacked * inode)78 static inline void bch2_inode_pack_inlined(struct bkey_inode_buf *packed,
79 					   const struct bch_inode_unpacked *inode)
80 {
81 	struct bkey_i_inode_v3 *k = &packed->inode;
82 	u8 *out = k->v.fields;
83 	u8 *end = (void *) &packed[1];
84 	u8 *last_nonzero_field = out;
85 	unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
86 	unsigned bytes;
87 	int ret;
88 
89 	bkey_inode_v3_init(&packed->inode.k_i);
90 	packed->inode.k.p.offset	= inode->bi_inum;
91 	packed->inode.v.bi_journal_seq	= cpu_to_le64(inode->bi_journal_seq);
92 	packed->inode.v.bi_hash_seed	= inode->bi_hash_seed;
93 	packed->inode.v.bi_flags	= cpu_to_le64(inode->bi_flags);
94 	packed->inode.v.bi_sectors	= cpu_to_le64(inode->bi_sectors);
95 	packed->inode.v.bi_size		= cpu_to_le64(inode->bi_size);
96 	packed->inode.v.bi_version	= cpu_to_le64(inode->bi_version);
97 	SET_INODEv3_MODE(&packed->inode.v, inode->bi_mode);
98 	SET_INODEv3_FIELDS_START(&packed->inode.v, INODEv3_FIELDS_START_CUR);
99 
100 
101 #define x(_name, _bits)							\
102 	nr_fields++;							\
103 									\
104 	if (inode->_name) {						\
105 		ret = bch2_varint_encode_fast(out, inode->_name);	\
106 		out += ret;						\
107 									\
108 		if (_bits > 64)						\
109 			*out++ = 0;					\
110 									\
111 		last_nonzero_field = out;				\
112 		last_nonzero_fieldnr = nr_fields;			\
113 	} else {							\
114 		*out++ = 0;						\
115 									\
116 		if (_bits > 64)						\
117 			*out++ = 0;					\
118 	}
119 
120 	BCH_INODE_FIELDS_v3()
121 #undef  x
122 	BUG_ON(out > end);
123 
124 	out = last_nonzero_field;
125 	nr_fields = last_nonzero_fieldnr;
126 
127 	bytes = out - (u8 *) &packed->inode.v;
128 	set_bkey_val_bytes(&packed->inode.k, bytes);
129 	memset_u64s_tail(&packed->inode.v, 0, bytes);
130 
131 	SET_INODEv3_NR_FIELDS(&k->v, nr_fields);
132 
133 	if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
134 		struct bch_inode_unpacked unpacked;
135 
136 		ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i), &unpacked);
137 		BUG_ON(ret);
138 		BUG_ON(unpacked.bi_inum		!= inode->bi_inum);
139 		BUG_ON(unpacked.bi_hash_seed	!= inode->bi_hash_seed);
140 		BUG_ON(unpacked.bi_sectors	!= inode->bi_sectors);
141 		BUG_ON(unpacked.bi_size		!= inode->bi_size);
142 		BUG_ON(unpacked.bi_version	!= inode->bi_version);
143 		BUG_ON(unpacked.bi_mode		!= inode->bi_mode);
144 
145 #define x(_name, _bits)	if (unpacked._name != inode->_name)		\
146 			panic("unpacked %llu should be %llu",		\
147 			      (u64) unpacked._name, (u64) inode->_name);
148 		BCH_INODE_FIELDS_v3()
149 #undef  x
150 	}
151 }
152 
bch2_inode_pack(struct bkey_inode_buf * packed,const struct bch_inode_unpacked * inode)153 void bch2_inode_pack(struct bkey_inode_buf *packed,
154 		     const struct bch_inode_unpacked *inode)
155 {
156 	bch2_inode_pack_inlined(packed, inode);
157 }
158 
bch2_inode_unpack_v1(struct bkey_s_c_inode inode,struct bch_inode_unpacked * unpacked)159 static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
160 				struct bch_inode_unpacked *unpacked)
161 {
162 	const u8 *in = inode.v->fields;
163 	const u8 *end = bkey_val_end(inode);
164 	u64 field[2];
165 	unsigned fieldnr = 0, field_bits;
166 	int ret;
167 
168 #define x(_name, _bits)							\
169 	if (fieldnr++ == INODEv1_NR_FIELDS(inode.v)) {			\
170 		unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
171 		memset((void *) unpacked + offset, 0,			\
172 		       sizeof(*unpacked) - offset);			\
173 		return 0;						\
174 	}								\
175 									\
176 	ret = inode_decode_field(in, end, field, &field_bits);		\
177 	if (ret < 0)							\
178 		return ret;						\
179 									\
180 	if (field_bits > sizeof(unpacked->_name) * 8)			\
181 		return -BCH_ERR_inode_unpack_error;			\
182 									\
183 	unpacked->_name = field[1];					\
184 	in += ret;
185 
186 	BCH_INODE_FIELDS_v2()
187 #undef  x
188 
189 	/* XXX: signal if there were more fields than expected? */
190 	return 0;
191 }
192 
bch2_inode_unpack_v2(struct bch_inode_unpacked * unpacked,const u8 * in,const u8 * end,unsigned nr_fields)193 static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
194 				const u8 *in, const u8 *end,
195 				unsigned nr_fields)
196 {
197 	unsigned fieldnr = 0;
198 	int ret;
199 	u64 v[2];
200 
201 #define x(_name, _bits)							\
202 	if (fieldnr < nr_fields) {					\
203 		ret = bch2_varint_decode_fast(in, end, &v[0]);		\
204 		if (ret < 0)						\
205 			return ret;					\
206 		in += ret;						\
207 									\
208 		if (_bits > 64) {					\
209 			ret = bch2_varint_decode_fast(in, end, &v[1]);	\
210 			if (ret < 0)					\
211 				return ret;				\
212 			in += ret;					\
213 		} else {						\
214 			v[1] = 0;					\
215 		}							\
216 	} else {							\
217 		v[0] = v[1] = 0;					\
218 	}								\
219 									\
220 	unpacked->_name = v[0];						\
221 	if (v[1] || v[0] != unpacked->_name)				\
222 		return -BCH_ERR_inode_unpack_error;			\
223 	fieldnr++;
224 
225 	BCH_INODE_FIELDS_v2()
226 #undef  x
227 
228 	/* XXX: signal if there were more fields than expected? */
229 	return 0;
230 }
231 
bch2_inode_unpack_v3(struct bkey_s_c k,struct bch_inode_unpacked * unpacked)232 static int bch2_inode_unpack_v3(struct bkey_s_c k,
233 				struct bch_inode_unpacked *unpacked)
234 {
235 	struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
236 	const u8 *in = inode.v->fields;
237 	const u8 *end = bkey_val_end(inode);
238 	unsigned nr_fields = INODEv3_NR_FIELDS(inode.v);
239 	unsigned fieldnr = 0;
240 	int ret;
241 	u64 v[2];
242 
243 	unpacked->bi_inum	= inode.k->p.offset;
244 	unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
245 	unpacked->bi_hash_seed	= inode.v->bi_hash_seed;
246 	unpacked->bi_flags	= le64_to_cpu(inode.v->bi_flags);
247 	unpacked->bi_sectors	= le64_to_cpu(inode.v->bi_sectors);
248 	unpacked->bi_size	= le64_to_cpu(inode.v->bi_size);
249 	unpacked->bi_version	= le64_to_cpu(inode.v->bi_version);
250 	unpacked->bi_mode	= INODEv3_MODE(inode.v);
251 
252 #define x(_name, _bits)							\
253 	if (fieldnr < nr_fields) {					\
254 		ret = bch2_varint_decode_fast(in, end, &v[0]);		\
255 		if (ret < 0)						\
256 			return ret;					\
257 		in += ret;						\
258 									\
259 		if (_bits > 64) {					\
260 			ret = bch2_varint_decode_fast(in, end, &v[1]);	\
261 			if (ret < 0)					\
262 				return ret;				\
263 			in += ret;					\
264 		} else {						\
265 			v[1] = 0;					\
266 		}							\
267 	} else {							\
268 		v[0] = v[1] = 0;					\
269 	}								\
270 									\
271 	unpacked->_name = v[0];						\
272 	if (v[1] || v[0] != unpacked->_name)				\
273 		return -BCH_ERR_inode_unpack_error;			\
274 	fieldnr++;
275 
276 	BCH_INODE_FIELDS_v3()
277 #undef  x
278 
279 	/* XXX: signal if there were more fields than expected? */
280 	return 0;
281 }
282 
bch2_inode_unpack_slowpath(struct bkey_s_c k,struct bch_inode_unpacked * unpacked)283 static noinline int bch2_inode_unpack_slowpath(struct bkey_s_c k,
284 					       struct bch_inode_unpacked *unpacked)
285 {
286 	memset(unpacked, 0, sizeof(*unpacked));
287 
288 	unpacked->bi_snapshot = k.k->p.snapshot;
289 
290 	switch (k.k->type) {
291 	case KEY_TYPE_inode: {
292 		struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
293 
294 		unpacked->bi_inum	= inode.k->p.offset;
295 		unpacked->bi_journal_seq= 0;
296 		unpacked->bi_hash_seed	= inode.v->bi_hash_seed;
297 		unpacked->bi_flags	= le32_to_cpu(inode.v->bi_flags);
298 		unpacked->bi_mode	= le16_to_cpu(inode.v->bi_mode);
299 
300 		if (INODEv1_NEW_VARINT(inode.v)) {
301 			return bch2_inode_unpack_v2(unpacked, inode.v->fields,
302 						    bkey_val_end(inode),
303 						    INODEv1_NR_FIELDS(inode.v));
304 		} else {
305 			return bch2_inode_unpack_v1(inode, unpacked);
306 		}
307 		break;
308 	}
309 	case KEY_TYPE_inode_v2: {
310 		struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
311 
312 		unpacked->bi_inum	= inode.k->p.offset;
313 		unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
314 		unpacked->bi_hash_seed	= inode.v->bi_hash_seed;
315 		unpacked->bi_flags	= le64_to_cpu(inode.v->bi_flags);
316 		unpacked->bi_mode	= le16_to_cpu(inode.v->bi_mode);
317 
318 		return bch2_inode_unpack_v2(unpacked, inode.v->fields,
319 					    bkey_val_end(inode),
320 					    INODEv2_NR_FIELDS(inode.v));
321 	}
322 	default:
323 		BUG();
324 	}
325 }
326 
bch2_inode_unpack(struct bkey_s_c k,struct bch_inode_unpacked * unpacked)327 int bch2_inode_unpack(struct bkey_s_c k,
328 		      struct bch_inode_unpacked *unpacked)
329 {
330 	unpacked->bi_snapshot = k.k->p.snapshot;
331 
332 	return likely(k.k->type == KEY_TYPE_inode_v3)
333 		? bch2_inode_unpack_v3(k, unpacked)
334 		: bch2_inode_unpack_slowpath(k, unpacked);
335 }
336 
__bch2_inode_peek(struct btree_trans * trans,struct btree_iter * iter,struct bch_inode_unpacked * inode,subvol_inum inum,unsigned flags,bool warn)337 int __bch2_inode_peek(struct btree_trans *trans,
338 		      struct btree_iter *iter,
339 		      struct bch_inode_unpacked *inode,
340 		      subvol_inum inum, unsigned flags,
341 		      bool warn)
342 {
343 	u32 snapshot;
344 	int ret = __bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot, warn);
345 	if (ret)
346 		return ret;
347 
348 	struct bkey_s_c k = bch2_bkey_get_iter(trans, iter, BTREE_ID_inodes,
349 					       SPOS(0, inum.inum, snapshot),
350 					       flags|BTREE_ITER_cached);
351 	ret = bkey_err(k);
352 	if (ret)
353 		return ret;
354 
355 	ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
356 	if (ret)
357 		goto err;
358 
359 	ret = bch2_inode_unpack(k, inode);
360 	if (ret)
361 		goto err;
362 
363 	return 0;
364 err:
365 	if (warn)
366 		bch_err_msg(trans->c, ret, "looking up inum %llu:%llu:", inum.subvol, inum.inum);
367 	bch2_trans_iter_exit(trans, iter);
368 	return ret;
369 }
370 
bch2_inode_write_flags(struct btree_trans * trans,struct btree_iter * iter,struct bch_inode_unpacked * inode,enum btree_iter_update_trigger_flags flags)371 int bch2_inode_write_flags(struct btree_trans *trans,
372 		     struct btree_iter *iter,
373 		     struct bch_inode_unpacked *inode,
374 		     enum btree_iter_update_trigger_flags flags)
375 {
376 	struct bkey_inode_buf *inode_p;
377 
378 	inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
379 	if (IS_ERR(inode_p))
380 		return PTR_ERR(inode_p);
381 
382 	bch2_inode_pack_inlined(inode_p, inode);
383 	inode_p->inode.k.p.snapshot = iter->snapshot;
384 	return bch2_trans_update(trans, iter, &inode_p->inode.k_i, flags);
385 }
386 
__bch2_fsck_write_inode(struct btree_trans * trans,struct bch_inode_unpacked * inode)387 int __bch2_fsck_write_inode(struct btree_trans *trans, struct bch_inode_unpacked *inode)
388 {
389 	struct bkey_inode_buf *inode_p =
390 		bch2_trans_kmalloc(trans, sizeof(*inode_p));
391 
392 	if (IS_ERR(inode_p))
393 		return PTR_ERR(inode_p);
394 
395 	bch2_inode_pack(inode_p, inode);
396 	inode_p->inode.k.p.snapshot = inode->bi_snapshot;
397 
398 	return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
399 				&inode_p->inode.k_i,
400 				BTREE_UPDATE_internal_snapshot_node);
401 }
402 
bch2_fsck_write_inode(struct btree_trans * trans,struct bch_inode_unpacked * inode)403 int bch2_fsck_write_inode(struct btree_trans *trans, struct bch_inode_unpacked *inode)
404 {
405 	int ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
406 			    __bch2_fsck_write_inode(trans, inode));
407 	bch_err_fn(trans->c, ret);
408 	return ret;
409 }
410 
bch2_inode_to_v3(struct btree_trans * trans,struct bkey_i * k)411 struct bkey_i *bch2_inode_to_v3(struct btree_trans *trans, struct bkey_i *k)
412 {
413 	struct bch_inode_unpacked u;
414 	struct bkey_inode_buf *inode_p;
415 	int ret;
416 
417 	if (!bkey_is_inode(&k->k))
418 		return ERR_PTR(-ENOENT);
419 
420 	inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
421 	if (IS_ERR(inode_p))
422 		return ERR_CAST(inode_p);
423 
424 	ret = bch2_inode_unpack(bkey_i_to_s_c(k), &u);
425 	if (ret)
426 		return ERR_PTR(ret);
427 
428 	bch2_inode_pack(inode_p, &u);
429 	return &inode_p->inode.k_i;
430 }
431 
__bch2_inode_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)432 static int __bch2_inode_validate(struct bch_fs *c, struct bkey_s_c k,
433 				 struct bkey_validate_context from)
434 {
435 	struct bch_inode_unpacked unpacked;
436 	int ret = 0;
437 
438 	bkey_fsck_err_on(k.k->p.inode,
439 			 c, inode_pos_inode_nonzero,
440 			 "nonzero k.p.inode");
441 
442 	bkey_fsck_err_on(k.k->p.offset < BLOCKDEV_INODE_MAX,
443 			 c, inode_pos_blockdev_range,
444 			 "fs inode in blockdev range");
445 
446 	bkey_fsck_err_on(bch2_inode_unpack(k, &unpacked),
447 			 c, inode_unpack_error,
448 			 "invalid variable length fields");
449 
450 	bkey_fsck_err_on(unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1,
451 			 c, inode_checksum_type_invalid,
452 			 "invalid data checksum type (%u >= %u",
453 			 unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
454 
455 	bkey_fsck_err_on(unpacked.bi_compression &&
456 			 !bch2_compression_opt_valid(unpacked.bi_compression - 1),
457 			 c, inode_compression_type_invalid,
458 			 "invalid compression opt %u", unpacked.bi_compression - 1);
459 
460 	bkey_fsck_err_on((unpacked.bi_flags & BCH_INODE_unlinked) &&
461 			 unpacked.bi_nlink != 0,
462 			 c, inode_unlinked_but_nlink_nonzero,
463 			 "flagged as unlinked but bi_nlink != 0");
464 
465 	bkey_fsck_err_on(unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode),
466 			 c, inode_subvol_root_but_not_dir,
467 			 "subvolume root but not a directory");
468 fsck_err:
469 	return ret;
470 }
471 
bch2_inode_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)472 int bch2_inode_validate(struct bch_fs *c, struct bkey_s_c k,
473 			struct bkey_validate_context from)
474 {
475 	struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
476 	int ret = 0;
477 
478 	bkey_fsck_err_on(INODEv1_STR_HASH(inode.v) >= BCH_STR_HASH_NR,
479 			 c, inode_str_hash_invalid,
480 			 "invalid str hash type (%llu >= %u)",
481 			 INODEv1_STR_HASH(inode.v), BCH_STR_HASH_NR);
482 
483 	ret = __bch2_inode_validate(c, k, from);
484 fsck_err:
485 	return ret;
486 }
487 
bch2_inode_v2_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)488 int bch2_inode_v2_validate(struct bch_fs *c, struct bkey_s_c k,
489 			   struct bkey_validate_context from)
490 {
491 	struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
492 	int ret = 0;
493 
494 	bkey_fsck_err_on(INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR,
495 			 c, inode_str_hash_invalid,
496 			 "invalid str hash type (%llu >= %u)",
497 			 INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
498 
499 	ret = __bch2_inode_validate(c, k, from);
500 fsck_err:
501 	return ret;
502 }
503 
bch2_inode_v3_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)504 int bch2_inode_v3_validate(struct bch_fs *c, struct bkey_s_c k,
505 			   struct bkey_validate_context from)
506 {
507 	struct bkey_s_c_inode_v3 inode = bkey_s_c_to_inode_v3(k);
508 	int ret = 0;
509 
510 	bkey_fsck_err_on(INODEv3_FIELDS_START(inode.v) < INODEv3_FIELDS_START_INITIAL ||
511 			 INODEv3_FIELDS_START(inode.v) > bkey_val_u64s(inode.k),
512 			 c, inode_v3_fields_start_bad,
513 			 "invalid fields_start (got %llu, min %u max %zu)",
514 			 INODEv3_FIELDS_START(inode.v),
515 			 INODEv3_FIELDS_START_INITIAL,
516 			 bkey_val_u64s(inode.k));
517 
518 	bkey_fsck_err_on(INODEv3_STR_HASH(inode.v) >= BCH_STR_HASH_NR,
519 			 c, inode_str_hash_invalid,
520 			 "invalid str hash type (%llu >= %u)",
521 			 INODEv3_STR_HASH(inode.v), BCH_STR_HASH_NR);
522 
523 	ret = __bch2_inode_validate(c, k, from);
524 fsck_err:
525 	return ret;
526 }
527 
__bch2_inode_unpacked_to_text(struct printbuf * out,struct bch_inode_unpacked * inode)528 static void __bch2_inode_unpacked_to_text(struct printbuf *out,
529 					  struct bch_inode_unpacked *inode)
530 {
531 	prt_printf(out, "\n");
532 	printbuf_indent_add(out, 2);
533 	prt_printf(out, "mode=%o\n", inode->bi_mode);
534 
535 	prt_str(out, "flags=");
536 	prt_bitflags(out, bch2_inode_flag_strs, inode->bi_flags & ((1U << 20) - 1));
537 	prt_printf(out, "(%x)\n", inode->bi_flags);
538 
539 	prt_printf(out, "journal_seq=%llu\n",	inode->bi_journal_seq);
540 	prt_printf(out, "hash_seed=%llx\n",	inode->bi_hash_seed);
541 	prt_printf(out, "hash_type=");
542 	bch2_prt_str_hash_type(out, INODE_STR_HASH(inode));
543 	prt_newline(out);
544 	prt_printf(out, "bi_size=%llu\n",	inode->bi_size);
545 	prt_printf(out, "bi_sectors=%llu\n",	inode->bi_sectors);
546 	prt_printf(out, "bi_version=%llu\n",	inode->bi_version);
547 
548 #define x(_name, _bits)						\
549 	prt_printf(out, #_name "=%llu\n", (u64) inode->_name);
550 	BCH_INODE_FIELDS_v3()
551 #undef  x
552 
553 	bch2_printbuf_strip_trailing_newline(out);
554 	printbuf_indent_sub(out, 2);
555 }
556 
bch2_inode_unpacked_to_text(struct printbuf * out,struct bch_inode_unpacked * inode)557 void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
558 {
559 	prt_printf(out, "inum: %llu:%u ", inode->bi_inum, inode->bi_snapshot);
560 	__bch2_inode_unpacked_to_text(out, inode);
561 }
562 
bch2_inode_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)563 void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
564 {
565 	struct bch_inode_unpacked inode;
566 
567 	if (bch2_inode_unpack(k, &inode)) {
568 		prt_printf(out, "(unpack error)");
569 		return;
570 	}
571 
572 	__bch2_inode_unpacked_to_text(out, &inode);
573 }
574 
bkey_inode_flags(struct bkey_s_c k)575 static inline u64 bkey_inode_flags(struct bkey_s_c k)
576 {
577 	switch (k.k->type) {
578 	case KEY_TYPE_inode:
579 		return le32_to_cpu(bkey_s_c_to_inode(k).v->bi_flags);
580 	case KEY_TYPE_inode_v2:
581 		return le64_to_cpu(bkey_s_c_to_inode_v2(k).v->bi_flags);
582 	case KEY_TYPE_inode_v3:
583 		return le64_to_cpu(bkey_s_c_to_inode_v3(k).v->bi_flags);
584 	default:
585 		return 0;
586 	}
587 }
588 
bkey_inode_flags_set(struct bkey_s k,u64 f)589 static inline void bkey_inode_flags_set(struct bkey_s k, u64 f)
590 {
591 	switch (k.k->type) {
592 	case KEY_TYPE_inode:
593 		bkey_s_to_inode(k).v->bi_flags = cpu_to_le32(f);
594 		return;
595 	case KEY_TYPE_inode_v2:
596 		bkey_s_to_inode_v2(k).v->bi_flags = cpu_to_le64(f);
597 		return;
598 	case KEY_TYPE_inode_v3:
599 		bkey_s_to_inode_v3(k).v->bi_flags = cpu_to_le64(f);
600 		return;
601 	default:
602 		BUG();
603 	}
604 }
605 
bkey_is_unlinked_inode(struct bkey_s_c k)606 static inline bool bkey_is_unlinked_inode(struct bkey_s_c k)
607 {
608 	unsigned f = bkey_inode_flags(k) & BCH_INODE_unlinked;
609 
610 	return (f & BCH_INODE_unlinked) && !(f & BCH_INODE_has_child_snapshot);
611 }
612 
613 static struct bkey_s_c
bch2_bkey_get_iter_snapshot_parent(struct btree_trans * trans,struct btree_iter * iter,enum btree_id btree,struct bpos pos,unsigned flags)614 bch2_bkey_get_iter_snapshot_parent(struct btree_trans *trans, struct btree_iter *iter,
615 				   enum btree_id btree, struct bpos pos,
616 				   unsigned flags)
617 {
618 	struct bch_fs *c = trans->c;
619 	struct bkey_s_c k;
620 	int ret = 0;
621 
622 	for_each_btree_key_max_norestart(trans, *iter, btree,
623 					  bpos_successor(pos),
624 					  SPOS(pos.inode, pos.offset, U32_MAX),
625 					  flags|BTREE_ITER_all_snapshots, k, ret)
626 		if (bch2_snapshot_is_ancestor(c, pos.snapshot, k.k->p.snapshot))
627 			return k;
628 
629 	bch2_trans_iter_exit(trans, iter);
630 	return ret ? bkey_s_c_err(ret) : bkey_s_c_null;
631 }
632 
633 static struct bkey_s_c
bch2_inode_get_iter_snapshot_parent(struct btree_trans * trans,struct btree_iter * iter,struct bpos pos,unsigned flags)634 bch2_inode_get_iter_snapshot_parent(struct btree_trans *trans, struct btree_iter *iter,
635 				    struct bpos pos, unsigned flags)
636 {
637 	struct bkey_s_c k;
638 again:
639 	k = bch2_bkey_get_iter_snapshot_parent(trans, iter, BTREE_ID_inodes, pos, flags);
640 	if (!k.k ||
641 	    bkey_err(k) ||
642 	    bkey_is_inode(k.k))
643 		return k;
644 
645 	bch2_trans_iter_exit(trans, iter);
646 	pos = k.k->p;
647 	goto again;
648 }
649 
__bch2_inode_has_child_snapshots(struct btree_trans * trans,struct bpos pos)650 int __bch2_inode_has_child_snapshots(struct btree_trans *trans, struct bpos pos)
651 {
652 	struct bch_fs *c = trans->c;
653 	struct btree_iter iter;
654 	struct bkey_s_c k;
655 	int ret = 0;
656 
657 	for_each_btree_key_max_norestart(trans, iter,
658 			BTREE_ID_inodes, POS(0, pos.offset), bpos_predecessor(pos),
659 			BTREE_ITER_all_snapshots|
660 			BTREE_ITER_with_updates, k, ret)
661 		if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot) &&
662 		    bkey_is_inode(k.k)) {
663 			ret = 1;
664 			break;
665 		}
666 	bch2_trans_iter_exit(trans, &iter);
667 	return ret;
668 }
669 
update_inode_has_children(struct btree_trans * trans,struct bkey_s k,bool have_child)670 static int update_inode_has_children(struct btree_trans *trans,
671 				     struct bkey_s k,
672 				     bool have_child)
673 {
674 	if (!have_child) {
675 		int ret = bch2_inode_has_child_snapshots(trans, k.k->p);
676 		if (ret)
677 			return ret < 0 ? ret : 0;
678 	}
679 
680 	u64 f = bkey_inode_flags(k.s_c);
681 	if (have_child != !!(f & BCH_INODE_has_child_snapshot))
682 		bkey_inode_flags_set(k, f ^ BCH_INODE_has_child_snapshot);
683 
684 	return 0;
685 }
686 
update_parent_inode_has_children(struct btree_trans * trans,struct bpos pos,bool have_child)687 static int update_parent_inode_has_children(struct btree_trans *trans, struct bpos pos,
688 					    bool have_child)
689 {
690 	struct btree_iter iter;
691 	struct bkey_s_c k = bch2_inode_get_iter_snapshot_parent(trans,
692 						&iter, pos, BTREE_ITER_with_updates);
693 	int ret = bkey_err(k);
694 	if (ret)
695 		return ret;
696 	if (!k.k)
697 		return 0;
698 
699 	if (!have_child) {
700 		ret = bch2_inode_has_child_snapshots(trans, k.k->p);
701 		if (ret) {
702 			ret = ret < 0 ? ret : 0;
703 			goto err;
704 		}
705 	}
706 
707 	u64 f = bkey_inode_flags(k);
708 	if (have_child != !!(f & BCH_INODE_has_child_snapshot)) {
709 		struct bkey_i *update = bch2_bkey_make_mut(trans, &iter, &k,
710 					     BTREE_UPDATE_internal_snapshot_node);
711 		ret = PTR_ERR_OR_ZERO(update);
712 		if (ret)
713 			goto err;
714 
715 		bkey_inode_flags_set(bkey_i_to_s(update), f ^ BCH_INODE_has_child_snapshot);
716 	}
717 err:
718 	bch2_trans_iter_exit(trans, &iter);
719 	return ret;
720 }
721 
bch2_trigger_inode(struct btree_trans * trans,enum btree_id btree_id,unsigned level,struct bkey_s_c old,struct bkey_s new,enum btree_iter_update_trigger_flags flags)722 int bch2_trigger_inode(struct btree_trans *trans,
723 		       enum btree_id btree_id, unsigned level,
724 		       struct bkey_s_c old,
725 		       struct bkey_s new,
726 		       enum btree_iter_update_trigger_flags flags)
727 {
728 	struct bch_fs *c = trans->c;
729 
730 	if ((flags & BTREE_TRIGGER_atomic) && (flags & BTREE_TRIGGER_insert)) {
731 		BUG_ON(!trans->journal_res.seq);
732 		bkey_s_to_inode_v3(new).v->bi_journal_seq = cpu_to_le64(trans->journal_res.seq);
733 	}
734 
735 	s64 nr[1] = { bkey_is_inode(new.k) - bkey_is_inode(old.k) };
736 	if ((flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) && nr[0]) {
737 		int ret = bch2_disk_accounting_mod2(trans, flags & BTREE_TRIGGER_gc, nr, nr_inodes);
738 		if (ret)
739 			return ret;
740 	}
741 
742 	if (flags & BTREE_TRIGGER_transactional) {
743 		int unlinked_delta =	(int) bkey_is_unlinked_inode(new.s_c) -
744 					(int) bkey_is_unlinked_inode(old);
745 		if (unlinked_delta) {
746 			int ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_deleted_inodes,
747 							      new.k->p, unlinked_delta > 0);
748 			if (ret)
749 				return ret;
750 		}
751 
752 		/*
753 		 * If we're creating or deleting an inode at this snapshot ID,
754 		 * and there might be an inode in a parent snapshot ID, we might
755 		 * need to set or clear the has_child_snapshot flag on the
756 		 * parent.
757 		 */
758 		int deleted_delta = (int) bkey_is_inode(new.k) -
759 				    (int) bkey_is_inode(old.k);
760 		if (deleted_delta &&
761 		    bch2_snapshot_parent(c, new.k->p.snapshot)) {
762 			int ret = update_parent_inode_has_children(trans, new.k->p,
763 								   deleted_delta > 0);
764 			if (ret)
765 				return ret;
766 		}
767 
768 		/*
769 		 * When an inode is first updated in a new snapshot, we may need
770 		 * to clear has_child_snapshot
771 		 */
772 		if (deleted_delta > 0) {
773 			int ret = update_inode_has_children(trans, new, false);
774 			if (ret)
775 				return ret;
776 		}
777 	}
778 
779 	return 0;
780 }
781 
bch2_inode_generation_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)782 int bch2_inode_generation_validate(struct bch_fs *c, struct bkey_s_c k,
783 				   struct bkey_validate_context from)
784 {
785 	int ret = 0;
786 
787 	bkey_fsck_err_on(k.k->p.inode,
788 			 c, inode_pos_inode_nonzero,
789 			 "nonzero k.p.inode");
790 fsck_err:
791 	return ret;
792 }
793 
bch2_inode_generation_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)794 void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
795 				   struct bkey_s_c k)
796 {
797 	struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
798 
799 	prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
800 }
801 
bch2_inode_alloc_cursor_validate(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from)802 int bch2_inode_alloc_cursor_validate(struct bch_fs *c, struct bkey_s_c k,
803 				   struct bkey_validate_context from)
804 {
805 	int ret = 0;
806 
807 	bkey_fsck_err_on(k.k->p.inode != LOGGED_OPS_INUM_inode_cursors,
808 			 c, inode_alloc_cursor_inode_bad,
809 			 "k.p.inode bad");
810 fsck_err:
811 	return ret;
812 }
813 
bch2_inode_alloc_cursor_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)814 void bch2_inode_alloc_cursor_to_text(struct printbuf *out, struct bch_fs *c,
815 				     struct bkey_s_c k)
816 {
817 	struct bkey_s_c_inode_alloc_cursor i = bkey_s_c_to_inode_alloc_cursor(k);
818 
819 	prt_printf(out, "idx %llu generation %llu",
820 		   le64_to_cpu(i.v->idx),
821 		   le64_to_cpu(i.v->gen));
822 }
823 
bch2_inode_init_early(struct bch_fs * c,struct bch_inode_unpacked * inode_u)824 void bch2_inode_init_early(struct bch_fs *c,
825 			   struct bch_inode_unpacked *inode_u)
826 {
827 	enum bch_str_hash_type str_hash =
828 		bch2_str_hash_opt_to_type(c, c->opts.str_hash);
829 
830 	memset(inode_u, 0, sizeof(*inode_u));
831 
832 	SET_INODE_STR_HASH(inode_u, str_hash);
833 	get_random_bytes(&inode_u->bi_hash_seed, sizeof(inode_u->bi_hash_seed));
834 }
835 
bch2_inode_init_late(struct bch_inode_unpacked * inode_u,u64 now,uid_t uid,gid_t gid,umode_t mode,dev_t rdev,struct bch_inode_unpacked * parent)836 void bch2_inode_init_late(struct bch_inode_unpacked *inode_u, u64 now,
837 			  uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
838 			  struct bch_inode_unpacked *parent)
839 {
840 	inode_u->bi_mode	= mode;
841 	inode_u->bi_uid		= uid;
842 	inode_u->bi_gid		= gid;
843 	inode_u->bi_dev		= rdev;
844 	inode_u->bi_atime	= now;
845 	inode_u->bi_mtime	= now;
846 	inode_u->bi_ctime	= now;
847 	inode_u->bi_otime	= now;
848 
849 	if (parent && parent->bi_mode & S_ISGID) {
850 		inode_u->bi_gid = parent->bi_gid;
851 		if (S_ISDIR(mode))
852 			inode_u->bi_mode |= S_ISGID;
853 	}
854 
855 	if (parent) {
856 #define x(_name, ...)	inode_u->bi_##_name = parent->bi_##_name;
857 		BCH_INODE_OPTS()
858 #undef x
859 	}
860 }
861 
bch2_inode_init(struct bch_fs * c,struct bch_inode_unpacked * inode_u,uid_t uid,gid_t gid,umode_t mode,dev_t rdev,struct bch_inode_unpacked * parent)862 void bch2_inode_init(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
863 		     uid_t uid, gid_t gid, umode_t mode, dev_t rdev,
864 		     struct bch_inode_unpacked *parent)
865 {
866 	bch2_inode_init_early(c, inode_u);
867 	bch2_inode_init_late(inode_u, bch2_current_time(c),
868 			     uid, gid, mode, rdev, parent);
869 }
870 
871 static struct bkey_i_inode_alloc_cursor *
bch2_inode_alloc_cursor_get(struct btree_trans * trans,u64 cpu,u64 * min,u64 * max)872 bch2_inode_alloc_cursor_get(struct btree_trans *trans, u64 cpu, u64 *min, u64 *max)
873 {
874 	struct bch_fs *c = trans->c;
875 
876 	u64 cursor_idx = c->opts.inodes_32bit ? 0 : cpu + 1;
877 
878 	cursor_idx &= ~(~0ULL << c->opts.shard_inode_numbers_bits);
879 
880 	struct btree_iter iter;
881 	struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter,
882 					BTREE_ID_logged_ops,
883 					POS(LOGGED_OPS_INUM_inode_cursors, cursor_idx),
884 					BTREE_ITER_cached);
885 	int ret = bkey_err(k);
886 	if (ret)
887 		return ERR_PTR(ret);
888 
889 	struct bkey_i_inode_alloc_cursor *cursor =
890 		k.k->type == KEY_TYPE_inode_alloc_cursor
891 		? bch2_bkey_make_mut_typed(trans, &iter, &k, 0, inode_alloc_cursor)
892 		: bch2_bkey_alloc(trans, &iter, 0, inode_alloc_cursor);
893 	ret = PTR_ERR_OR_ZERO(cursor);
894 	if (ret)
895 		goto err;
896 
897 	if (c->opts.inodes_32bit) {
898 		*min = BLOCKDEV_INODE_MAX;
899 		*max = INT_MAX;
900 	} else {
901 		cursor->v.bits = c->opts.shard_inode_numbers_bits;
902 
903 		unsigned bits = 63 - c->opts.shard_inode_numbers_bits;
904 
905 		*min = max(cpu << bits, (u64) INT_MAX + 1);
906 		*max = (cpu << bits) | ~(ULLONG_MAX << bits);
907 	}
908 
909 	if (le64_to_cpu(cursor->v.idx)  < *min)
910 		cursor->v.idx = cpu_to_le64(*min);
911 
912 	if (le64_to_cpu(cursor->v.idx) >= *max) {
913 		cursor->v.idx = cpu_to_le64(*min);
914 		le32_add_cpu(&cursor->v.gen, 1);
915 	}
916 err:
917 	bch2_trans_iter_exit(trans, &iter);
918 	return ret ? ERR_PTR(ret) : cursor;
919 }
920 
921 /*
922  * This just finds an empty slot:
923  */
bch2_inode_create(struct btree_trans * trans,struct btree_iter * iter,struct bch_inode_unpacked * inode_u,u32 snapshot,u64 cpu)924 int bch2_inode_create(struct btree_trans *trans,
925 		      struct btree_iter *iter,
926 		      struct bch_inode_unpacked *inode_u,
927 		      u32 snapshot, u64 cpu)
928 {
929 	u64 min, max;
930 	struct bkey_i_inode_alloc_cursor *cursor =
931 		bch2_inode_alloc_cursor_get(trans, cpu, &min, &max);
932 	int ret = PTR_ERR_OR_ZERO(cursor);
933 	if (ret)
934 		return ret;
935 
936 	u64 start = le64_to_cpu(cursor->v.idx);
937 	u64 pos = start;
938 
939 	bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
940 			     BTREE_ITER_all_snapshots|
941 			     BTREE_ITER_intent);
942 	struct bkey_s_c k;
943 again:
944 	while ((k = bch2_btree_iter_peek(trans, iter)).k &&
945 	       !(ret = bkey_err(k)) &&
946 	       bkey_lt(k.k->p, POS(0, max))) {
947 		if (pos < iter->pos.offset)
948 			goto found_slot;
949 
950 		/*
951 		 * We don't need to iterate over keys in every snapshot once
952 		 * we've found just one:
953 		 */
954 		pos = iter->pos.offset + 1;
955 		bch2_btree_iter_set_pos(trans, iter, POS(0, pos));
956 	}
957 
958 	if (!ret && pos < max)
959 		goto found_slot;
960 
961 	if (!ret && start == min)
962 		ret = -BCH_ERR_ENOSPC_inode_create;
963 
964 	if (ret) {
965 		bch2_trans_iter_exit(trans, iter);
966 		return ret;
967 	}
968 
969 	/* Retry from start */
970 	pos = start = min;
971 	bch2_btree_iter_set_pos(trans, iter, POS(0, pos));
972 	le32_add_cpu(&cursor->v.gen, 1);
973 	goto again;
974 found_slot:
975 	bch2_btree_iter_set_pos(trans, iter, SPOS(0, pos, snapshot));
976 	k = bch2_btree_iter_peek_slot(trans, iter);
977 	ret = bkey_err(k);
978 	if (ret) {
979 		bch2_trans_iter_exit(trans, iter);
980 		return ret;
981 	}
982 
983 	inode_u->bi_inum	= k.k->p.offset;
984 	inode_u->bi_generation	= le64_to_cpu(cursor->v.gen);
985 	cursor->v.idx		= cpu_to_le64(k.k->p.offset + 1);
986 	return 0;
987 }
988 
bch2_inode_delete_keys(struct btree_trans * trans,subvol_inum inum,enum btree_id id)989 static int bch2_inode_delete_keys(struct btree_trans *trans,
990 				  subvol_inum inum, enum btree_id id)
991 {
992 	struct btree_iter iter;
993 	struct bkey_s_c k;
994 	struct bkey_i delete;
995 	struct bpos end = POS(inum.inum, U64_MAX);
996 	u32 snapshot;
997 	int ret = 0;
998 
999 	/*
1000 	 * We're never going to be deleting partial extents, no need to use an
1001 	 * extent iterator:
1002 	 */
1003 	bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
1004 			     BTREE_ITER_intent);
1005 
1006 	while (1) {
1007 		bch2_trans_begin(trans);
1008 
1009 		ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
1010 		if (ret)
1011 			goto err;
1012 
1013 		bch2_btree_iter_set_snapshot(trans, &iter, snapshot);
1014 
1015 		k = bch2_btree_iter_peek_max(trans, &iter, end);
1016 		ret = bkey_err(k);
1017 		if (ret)
1018 			goto err;
1019 
1020 		if (!k.k)
1021 			break;
1022 
1023 		bkey_init(&delete.k);
1024 		delete.k.p = iter.pos;
1025 
1026 		if (iter.flags & BTREE_ITER_is_extents)
1027 			bch2_key_resize(&delete.k,
1028 					bpos_min(end, k.k->p).offset -
1029 					iter.pos.offset);
1030 
1031 		ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
1032 		      bch2_trans_commit(trans, NULL, NULL,
1033 					BCH_TRANS_COMMIT_no_enospc);
1034 err:
1035 		if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1036 			break;
1037 	}
1038 
1039 	bch2_trans_iter_exit(trans, &iter);
1040 	return ret;
1041 }
1042 
bch2_inode_rm(struct bch_fs * c,subvol_inum inum)1043 int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
1044 {
1045 	struct btree_trans *trans = bch2_trans_get(c);
1046 	struct btree_iter iter = {};
1047 	struct bkey_s_c k;
1048 	u32 snapshot;
1049 	int ret;
1050 
1051 	/*
1052 	 * If this was a directory, there shouldn't be any real dirents left -
1053 	 * but there could be whiteouts (from hash collisions) that we should
1054 	 * delete:
1055 	 *
1056 	 * XXX: the dirent could ideally would delete whiteouts when they're no
1057 	 * longer needed
1058 	 */
1059 	ret   = bch2_inode_delete_keys(trans, inum, BTREE_ID_extents) ?:
1060 		bch2_inode_delete_keys(trans, inum, BTREE_ID_xattrs) ?:
1061 		bch2_inode_delete_keys(trans, inum, BTREE_ID_dirents);
1062 	if (ret)
1063 		goto err;
1064 retry:
1065 	bch2_trans_begin(trans);
1066 
1067 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
1068 	if (ret)
1069 		goto err;
1070 
1071 	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
1072 			       SPOS(0, inum.inum, snapshot),
1073 			       BTREE_ITER_intent|BTREE_ITER_cached);
1074 	ret = bkey_err(k);
1075 	if (ret)
1076 		goto err;
1077 
1078 	if (!bkey_is_inode(k.k)) {
1079 		bch2_fs_inconsistent(c,
1080 				     "inode %llu:%u not found when deleting",
1081 				     inum.inum, snapshot);
1082 		ret = -BCH_ERR_ENOENT_inode;
1083 		goto err;
1084 	}
1085 
1086 	ret   = bch2_btree_delete_at(trans, &iter, 0) ?:
1087 		bch2_trans_commit(trans, NULL, NULL,
1088 				BCH_TRANS_COMMIT_no_enospc);
1089 err:
1090 	bch2_trans_iter_exit(trans, &iter);
1091 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1092 		goto retry;
1093 
1094 	if (ret)
1095 		goto err2;
1096 
1097 	ret = delete_ancestor_snapshot_inodes(trans, SPOS(0, inum.inum, snapshot));
1098 err2:
1099 	bch2_trans_put(trans);
1100 	return ret;
1101 }
1102 
bch2_inode_find_by_inum_nowarn_trans(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * inode)1103 int bch2_inode_find_by_inum_nowarn_trans(struct btree_trans *trans,
1104 				  subvol_inum inum,
1105 				  struct bch_inode_unpacked *inode)
1106 {
1107 	struct btree_iter iter;
1108 	int ret;
1109 
1110 	ret = bch2_inode_peek_nowarn(trans, &iter, inode, inum, 0);
1111 	if (!ret)
1112 		bch2_trans_iter_exit(trans, &iter);
1113 	return ret;
1114 }
1115 
bch2_inode_find_by_inum_trans(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * inode)1116 int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
1117 				  subvol_inum inum,
1118 				  struct bch_inode_unpacked *inode)
1119 {
1120 	struct btree_iter iter;
1121 	int ret;
1122 
1123 	ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
1124 	if (!ret)
1125 		bch2_trans_iter_exit(trans, &iter);
1126 	return ret;
1127 }
1128 
bch2_inode_find_by_inum(struct bch_fs * c,subvol_inum inum,struct bch_inode_unpacked * inode)1129 int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
1130 			    struct bch_inode_unpacked *inode)
1131 {
1132 	return bch2_trans_do(c, bch2_inode_find_by_inum_trans(trans, inum, inode));
1133 }
1134 
bch2_inode_nlink_inc(struct bch_inode_unpacked * bi)1135 int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
1136 {
1137 	if (bi->bi_flags & BCH_INODE_unlinked)
1138 		bi->bi_flags &= ~BCH_INODE_unlinked;
1139 	else {
1140 		if (bi->bi_nlink == U32_MAX)
1141 			return -EINVAL;
1142 
1143 		bi->bi_nlink++;
1144 	}
1145 
1146 	return 0;
1147 }
1148 
bch2_inode_nlink_dec(struct btree_trans * trans,struct bch_inode_unpacked * bi)1149 void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
1150 {
1151 	if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_unlinked)) {
1152 		bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
1153 					bi->bi_inum);
1154 		return;
1155 	}
1156 
1157 	if (bi->bi_flags & BCH_INODE_unlinked) {
1158 		bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
1159 		return;
1160 	}
1161 
1162 	if (bi->bi_nlink)
1163 		bi->bi_nlink--;
1164 	else
1165 		bi->bi_flags |= BCH_INODE_unlinked;
1166 }
1167 
bch2_inode_opts_to_opts(struct bch_inode_unpacked * inode)1168 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
1169 {
1170 	struct bch_opts ret = { 0 };
1171 #define x(_name, _bits)							\
1172 	if (inode->bi_##_name)						\
1173 		opt_set(ret, _name, inode->bi_##_name - 1);
1174 	BCH_INODE_OPTS()
1175 #undef x
1176 	return ret;
1177 }
1178 
bch2_inode_opts_get(struct bch_io_opts * opts,struct bch_fs * c,struct bch_inode_unpacked * inode)1179 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
1180 			 struct bch_inode_unpacked *inode)
1181 {
1182 #define x(_name, _bits)							\
1183 	if ((inode)->bi_##_name) {					\
1184 		opts->_name = inode->bi_##_name - 1;			\
1185 		opts->_name##_from_inode = true;			\
1186 	} else {							\
1187 		opts->_name = c->opts._name;				\
1188 		opts->_name##_from_inode = false;			\
1189 	}
1190 	BCH_INODE_OPTS()
1191 #undef x
1192 
1193 	bch2_io_opts_fixups(opts);
1194 }
1195 
bch2_inum_opts_get(struct btree_trans * trans,subvol_inum inum,struct bch_io_opts * opts)1196 int bch2_inum_opts_get(struct btree_trans *trans, subvol_inum inum, struct bch_io_opts *opts)
1197 {
1198 	struct bch_inode_unpacked inode;
1199 	int ret = lockrestart_do(trans, bch2_inode_find_by_inum_trans(trans, inum, &inode));
1200 
1201 	if (ret)
1202 		return ret;
1203 
1204 	bch2_inode_opts_get(opts, trans->c, &inode);
1205 	return 0;
1206 }
1207 
bch2_inode_set_casefold(struct btree_trans * trans,subvol_inum inum,struct bch_inode_unpacked * bi,unsigned v)1208 int bch2_inode_set_casefold(struct btree_trans *trans, subvol_inum inum,
1209 			    struct bch_inode_unpacked *bi, unsigned v)
1210 {
1211 	struct bch_fs *c = trans->c;
1212 
1213 #ifdef CONFIG_UNICODE
1214 	int ret = 0;
1215 	/* Not supported on individual files. */
1216 	if (!S_ISDIR(bi->bi_mode))
1217 		return -EOPNOTSUPP;
1218 
1219 	/*
1220 	 * Make sure the dir is empty, as otherwise we'd need to
1221 	 * rehash everything and update the dirent keys.
1222 	 */
1223 	ret = bch2_empty_dir_trans(trans, inum);
1224 	if (ret < 0)
1225 		return ret;
1226 
1227 	ret = bch2_request_incompat_feature(c, bcachefs_metadata_version_casefolding);
1228 	if (ret)
1229 		return ret;
1230 
1231 	bch2_check_set_feature(c, BCH_FEATURE_casefolding);
1232 
1233 	bi->bi_casefold = v + 1;
1234 	bi->bi_fields_set |= BIT(Inode_opt_casefold);
1235 
1236 	return 0;
1237 #else
1238 	bch_err(c, "Cannot use casefolding on a kernel without CONFIG_UNICODE");
1239 	return -EOPNOTSUPP;
1240 #endif
1241 }
1242 
__bch2_inode_rm_snapshot(struct btree_trans * trans,u64 inum,u32 snapshot)1243 static noinline int __bch2_inode_rm_snapshot(struct btree_trans *trans, u64 inum, u32 snapshot)
1244 {
1245 	struct bch_fs *c = trans->c;
1246 	struct btree_iter iter = {};
1247 	struct bkey_i_inode_generation delete;
1248 	struct bch_inode_unpacked inode_u;
1249 	struct bkey_s_c k;
1250 	int ret;
1251 
1252 	do {
1253 		ret   = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1254 						      SPOS(inum, 0, snapshot),
1255 						      SPOS(inum, U64_MAX, snapshot),
1256 						      0, NULL) ?:
1257 			bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
1258 						      SPOS(inum, 0, snapshot),
1259 						      SPOS(inum, U64_MAX, snapshot),
1260 						      0, NULL) ?:
1261 			bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
1262 						      SPOS(inum, 0, snapshot),
1263 						      SPOS(inum, U64_MAX, snapshot),
1264 						      0, NULL);
1265 	} while (ret == -BCH_ERR_transaction_restart_nested);
1266 	if (ret)
1267 		goto err;
1268 retry:
1269 	bch2_trans_begin(trans);
1270 
1271 	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
1272 			       SPOS(0, inum, snapshot), BTREE_ITER_intent);
1273 	ret = bkey_err(k);
1274 	if (ret)
1275 		goto err;
1276 
1277 	if (!bkey_is_inode(k.k)) {
1278 		bch2_fs_inconsistent(c,
1279 				     "inode %llu:%u not found when deleting",
1280 				     inum, snapshot);
1281 		ret = -BCH_ERR_ENOENT_inode;
1282 		goto err;
1283 	}
1284 
1285 	bch2_inode_unpack(k, &inode_u);
1286 
1287 	/* Subvolume root? */
1288 	if (inode_u.bi_subvol)
1289 		bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
1290 
1291 	bkey_inode_generation_init(&delete.k_i);
1292 	delete.k.p = iter.pos;
1293 	delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
1294 
1295 	ret   = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
1296 		bch2_trans_commit(trans, NULL, NULL,
1297 				BCH_TRANS_COMMIT_no_enospc);
1298 err:
1299 	bch2_trans_iter_exit(trans, &iter);
1300 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1301 		goto retry;
1302 
1303 	return ret ?: -BCH_ERR_transaction_restart_nested;
1304 }
1305 
1306 /*
1307  * After deleting an inode, there may be versions in older snapshots that should
1308  * also be deleted - if they're not referenced by sibling snapshots and not open
1309  * in other subvolumes:
1310  */
delete_ancestor_snapshot_inodes(struct btree_trans * trans,struct bpos pos)1311 static int delete_ancestor_snapshot_inodes(struct btree_trans *trans, struct bpos pos)
1312 {
1313 	struct btree_iter iter;
1314 	struct bkey_s_c k;
1315 	int ret;
1316 next_parent:
1317 	ret = lockrestart_do(trans,
1318 		bkey_err(k = bch2_inode_get_iter_snapshot_parent(trans, &iter, pos, 0)));
1319 	if (ret || !k.k)
1320 		return ret;
1321 
1322 	bool unlinked = bkey_is_unlinked_inode(k);
1323 	pos = k.k->p;
1324 	bch2_trans_iter_exit(trans, &iter);
1325 
1326 	if (!unlinked)
1327 		return 0;
1328 
1329 	ret = lockrestart_do(trans, bch2_inode_or_descendents_is_open(trans, pos));
1330 	if (ret)
1331 		return ret < 0 ? ret : 0;
1332 
1333 	ret = __bch2_inode_rm_snapshot(trans, pos.offset, pos.snapshot);
1334 	if (ret)
1335 		return ret;
1336 	goto next_parent;
1337 }
1338 
bch2_inode_rm_snapshot(struct btree_trans * trans,u64 inum,u32 snapshot)1339 int bch2_inode_rm_snapshot(struct btree_trans *trans, u64 inum, u32 snapshot)
1340 {
1341 	return __bch2_inode_rm_snapshot(trans, inum, snapshot) ?:
1342 		delete_ancestor_snapshot_inodes(trans, SPOS(0, inum, snapshot));
1343 }
1344 
may_delete_deleted_inode(struct btree_trans * trans,struct btree_iter * iter,struct bpos pos,bool * need_another_pass)1345 static int may_delete_deleted_inode(struct btree_trans *trans,
1346 				    struct btree_iter *iter,
1347 				    struct bpos pos,
1348 				    bool *need_another_pass)
1349 {
1350 	struct bch_fs *c = trans->c;
1351 	struct btree_iter inode_iter;
1352 	struct bkey_s_c k;
1353 	struct bch_inode_unpacked inode;
1354 	struct printbuf buf = PRINTBUF;
1355 	int ret;
1356 
1357 	k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes, pos, BTREE_ITER_cached);
1358 	ret = bkey_err(k);
1359 	if (ret)
1360 		return ret;
1361 
1362 	ret = bkey_is_inode(k.k) ? 0 : -BCH_ERR_ENOENT_inode;
1363 	if (fsck_err_on(!bkey_is_inode(k.k),
1364 			trans, deleted_inode_missing,
1365 			"nonexistent inode %llu:%u in deleted_inodes btree",
1366 			pos.offset, pos.snapshot))
1367 		goto delete;
1368 
1369 	ret = bch2_inode_unpack(k, &inode);
1370 	if (ret)
1371 		goto out;
1372 
1373 	if (S_ISDIR(inode.bi_mode)) {
1374 		ret = bch2_empty_dir_snapshot(trans, pos.offset, 0, pos.snapshot);
1375 		if (fsck_err_on(bch2_err_matches(ret, ENOTEMPTY),
1376 				trans, deleted_inode_is_dir,
1377 				"non empty directory %llu:%u in deleted_inodes btree",
1378 				pos.offset, pos.snapshot))
1379 			goto delete;
1380 		if (ret)
1381 			goto out;
1382 	}
1383 
1384 	if (fsck_err_on(!(inode.bi_flags & BCH_INODE_unlinked),
1385 			trans, deleted_inode_not_unlinked,
1386 			"non-deleted inode %llu:%u in deleted_inodes btree",
1387 			pos.offset, pos.snapshot))
1388 		goto delete;
1389 
1390 	if (fsck_err_on(inode.bi_flags & BCH_INODE_has_child_snapshot,
1391 			trans, deleted_inode_has_child_snapshots,
1392 			"inode with child snapshots %llu:%u in deleted_inodes btree",
1393 			pos.offset, pos.snapshot))
1394 		goto delete;
1395 
1396 	ret = bch2_inode_has_child_snapshots(trans, k.k->p);
1397 	if (ret < 0)
1398 		goto out;
1399 
1400 	if (ret) {
1401 		if (fsck_err(trans, inode_has_child_snapshots_wrong,
1402 			     "inode has_child_snapshots flag wrong (should be set)\n%s",
1403 			     (printbuf_reset(&buf),
1404 			      bch2_inode_unpacked_to_text(&buf, &inode),
1405 			      buf.buf))) {
1406 			inode.bi_flags |= BCH_INODE_has_child_snapshot;
1407 			ret = __bch2_fsck_write_inode(trans, &inode);
1408 			if (ret)
1409 				goto out;
1410 		}
1411 		goto delete;
1412 
1413 	}
1414 
1415 	if (test_bit(BCH_FS_clean_recovery, &c->flags) &&
1416 	    !fsck_err(trans, deleted_inode_but_clean,
1417 		      "filesystem marked as clean but have deleted inode %llu:%u",
1418 		      pos.offset, pos.snapshot)) {
1419 		ret = 0;
1420 		goto out;
1421 	}
1422 
1423 	ret = 1;
1424 out:
1425 fsck_err:
1426 	bch2_trans_iter_exit(trans, &inode_iter);
1427 	printbuf_exit(&buf);
1428 	return ret;
1429 delete:
1430 	ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_deleted_inodes, pos, false);
1431 	goto out;
1432 }
1433 
bch2_delete_dead_inodes(struct bch_fs * c)1434 int bch2_delete_dead_inodes(struct bch_fs *c)
1435 {
1436 	struct btree_trans *trans = bch2_trans_get(c);
1437 	bool need_another_pass;
1438 	int ret;
1439 again:
1440 	/*
1441 	 * if we ran check_inodes() unlinked inodes will have already been
1442 	 * cleaned up but the write buffer will be out of sync; therefore we
1443 	 * alway need a write buffer flush
1444 	 */
1445 	ret = bch2_btree_write_buffer_flush_sync(trans);
1446 	if (ret)
1447 		goto err;
1448 
1449 	need_another_pass = false;
1450 
1451 	/*
1452 	 * Weird transaction restart handling here because on successful delete,
1453 	 * bch2_inode_rm_snapshot() will return a nested transaction restart,
1454 	 * but we can't retry because the btree write buffer won't have been
1455 	 * flushed and we'd spin:
1456 	 */
1457 	ret = for_each_btree_key_commit(trans, iter, BTREE_ID_deleted_inodes, POS_MIN,
1458 					BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1459 					NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
1460 		ret = may_delete_deleted_inode(trans, &iter, k.k->p, &need_another_pass);
1461 		if (ret > 0) {
1462 			bch_verbose_ratelimited(c, "deleting unlinked inode %llu:%u",
1463 						k.k->p.offset, k.k->p.snapshot);
1464 
1465 			ret = bch2_inode_rm_snapshot(trans, k.k->p.offset, k.k->p.snapshot);
1466 			/*
1467 			 * We don't want to loop here: a transaction restart
1468 			 * error here means we handled a transaction restart and
1469 			 * we're actually done, but if we loop we'll retry the
1470 			 * same key because the write buffer hasn't been flushed
1471 			 * yet
1472 			 */
1473 			if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1474 				ret = 0;
1475 				continue;
1476 			}
1477 		}
1478 
1479 		ret;
1480 	}));
1481 
1482 	if (!ret && need_another_pass)
1483 		goto again;
1484 err:
1485 	bch2_trans_put(trans);
1486 	return ret;
1487 }
1488