summaryrefslogtreecommitdiff
path: root/fs/bcachefs/inode.c
blob: 0a350c6d0932d336a5d01fe35b22f65f6d6c677b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: GPL-2.0

#include "bcachefs.h"
#include "bkey_methods.h"
#include "btree_update.h"
#include "error.h"
#include "extents.h"
#include "inode.h"
#include "io.h"
#include "keylist.h"

#include <linux/random.h>

#include <asm/unaligned.h>

#define FIELD_BYTES()						\

static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
static const u8 bits_table[8] = {
	1  * 8 - 1,
	2  * 8 - 2,
	3  * 8 - 3,
	4  * 8 - 4,
	6  * 8 - 5,
	8  * 8 - 6,
	10 * 8 - 7,
	13 * 8 - 8,
};

static int inode_encode_field(u8 *out, u8 *end, u64 hi, u64 lo)
{
	__be64 in[2] = { cpu_to_be64(hi), cpu_to_be64(lo), };
	unsigned shift, bytes, bits = likely(!hi)
		? fls64(lo)
		: fls64(hi) + 64;

	for (shift = 1; shift <= 8; shift++)
		if (bits < bits_table[shift - 1])
			goto got_shift;

	BUG();
got_shift:
	bytes = byte_table[shift - 1];

	BUG_ON(out + bytes > end);

	memcpy(out, (u8 *) in + 16 - bytes, bytes);
	*out |= (1 << 8) >> shift;

	return bytes;
}

static int inode_decode_field(const u8 *in, const u8 *end,
			      u64 out[2], unsigned *out_bits)
{
	__be64 be[2] = { 0, 0 };
	unsigned bytes, shift;
	u8 *p;

	if (in >= end)
		return -1;

	if (!*in)
		return -1;

	/*
	 * position of highest set bit indicates number of bytes:
	 * shift = number of bits to remove in high byte:
	 */
	shift	= 8 - __fls(*in); /* 1 <= shift <= 8 */
	bytes	= byte_table[shift - 1];

	if (in + bytes > end)
		return -1;

	p = (u8 *) be + 16 - bytes;
	memcpy(p, in, bytes);
	*p ^= (1 << 8) >> shift;

	out[0] = be64_to_cpu(be[0]);
	out[1] = be64_to_cpu(be[1]);
	*out_bits = out[0] ? 64 + fls64(out[0]) : fls64(out[1]);

	return bytes;
}

void bch2_inode_pack(struct bkey_inode_buf *packed,
		     const struct bch_inode_unpacked *inode)
{
	u8 *out = packed->inode.v.fields;
	u8 *end = (void *) &packed[1];
	u8 *last_nonzero_field = out;
	unsigned nr_fields = 0, last_nonzero_fieldnr = 0;

	bkey_inode_init(&packed->inode.k_i);
	packed->inode.k.p.inode		= inode->bi_inum;
	packed->inode.v.bi_hash_seed	= inode->bi_hash_seed;
	packed->inode.v.bi_flags	= cpu_to_le32(inode->bi_flags);
	packed->inode.v.bi_mode		= cpu_to_le16(inode->bi_mode);

#define BCH_INODE_FIELD(_name, _bits)					\
	out += inode_encode_field(out, end, 0, inode->_name);		\
	nr_fields++;							\
									\
	if (inode->_name) {						\
		last_nonzero_field = out;				\
		last_nonzero_fieldnr = nr_fields;			\
	}

	BCH_INODE_FIELDS()
#undef  BCH_INODE_FIELD

	out = last_nonzero_field;
	nr_fields = last_nonzero_fieldnr;

	set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v);
	memset(out, 0,
	       (u8 *) &packed->inode.v +
	       bkey_val_bytes(&packed->inode.k) - out);

	SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);

	if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
		struct bch_inode_unpacked unpacked;

		int ret = bch2_inode_unpack(inode_i_to_s_c(&packed->inode),
					   &unpacked);
		BUG_ON(ret);
		BUG_ON(unpacked.bi_inum		!= inode->bi_inum);
		BUG_ON(unpacked.bi_hash_seed	!= inode->bi_hash_seed);
		BUG_ON(unpacked.bi_mode		!= inode->bi_mode);

#define BCH_INODE_FIELD(_name, _bits)	BUG_ON(unpacked._name != inode->_name);
		BCH_INODE_FIELDS()
#undef  BCH_INODE_FIELD
	}
}

int bch2_inode_unpack(struct bkey_s_c_inode inode,
		      struct bch_inode_unpacked *unpacked)
{
	const u8 *in = inode.v->fields;
	const u8 *end = (void *) inode.v + bkey_val_bytes(inode.k);
	u64 field[2];
	unsigned fieldnr = 0, field_bits;
	int ret;

	unpacked->bi_inum	= inode.k->p.inode;
	unpacked->bi_hash_seed	= inode.v->bi_hash_seed;
	unpacked->bi_flags	= le32_to_cpu(inode.v->bi_flags);
	unpacked->bi_mode	= le16_to_cpu(inode.v->bi_mode);

#define BCH_INODE_FIELD(_name, _bits)					\
	if (fieldnr++ == INODE_NR_FIELDS(inode.v)) {			\
		unsigned offset = offsetof(struct bch_inode_unpacked, _name);\
		memset((void *) unpacked + offset, 0,			\
		       sizeof(*unpacked) - offset);			\
		return 0;						\
	}								\
									\
	ret = inode_decode_field(in, end, field, &field_bits);		\
	if (ret < 0)							\
		return ret;						\
									\
	if (field_bits > sizeof(unpacked->_name) * 8)			\
		return -1;						\
									\
	unpacked->_name = field[1];					\
	in += ret;

	BCH_INODE_FIELDS()
#undef  BCH_INODE_FIELD

	/* XXX: signal if there were more fields than expected? */

	return 0;
}

const char *bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k)
{
	if (k.k->p.offset)
		return "nonzero offset";

	switch (k.k->type) {
	case BCH_INODE_FS: {
		struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
		struct bch_inode_unpacked unpacked;

		if (bkey_val_bytes(k.k) < sizeof(struct bch_inode))
			return "incorrect value size";

		if (k.k->p.inode < BLOCKDEV_INODE_MAX)
			return "fs inode in blockdev range";

		if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR)
			return "invalid str hash type";

		if (bch2_inode_unpack(inode, &unpacked))
			return "invalid variable length fields";

		if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1)
			return "invalid data checksum type";

		if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1)
			return "invalid data checksum type";

		if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
		    unpacked.bi_nlink != 0)
			return "flagged as unlinked but bi_nlink != 0";

		return NULL;
	}
	case BCH_INODE_BLOCKDEV:
		if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_blockdev))
			return "incorrect value size";

		if (k.k->p.inode >= BLOCKDEV_INODE_MAX)
			return "blockdev inode in fs range";

		return NULL;
	case BCH_INODE_GENERATION:
		if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation))
			return "incorrect value size";

		return NULL;
	default:
		return "invalid type";
	}
}

void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
		       struct bkey_s_c k)
{
	struct bkey_s_c_inode inode;
	struct bch_inode_unpacked unpacked;

	switch (k.k->type) {
	case BCH_INODE_FS:
		inode = bkey_s_c_to_inode(k);
		if (bch2_inode_unpack(inode, &unpacked)) {
			pr_buf(out, "(unpack error)");
			break;
		}

#define BCH_INODE_FIELD(_name, _bits)						\
		pr_buf(out, #_name ": %llu ", (u64) unpacked._name);
		BCH_INODE_FIELDS()
#undef  BCH_INODE_FIELD
		break;
	}
}

void 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)
{
	s64 now = bch2_current_time(c);

	memset(inode_u, 0, sizeof(*inode_u));

	/* ick */
	inode_u->bi_flags |= c->opts.str_hash << INODE_STR_HASH_OFFSET;
	get_random_bytes(&inode_u->bi_hash_seed, sizeof(inode_u->bi_hash_seed));

	inode_u->bi_mode	= mode;
	inode_u->bi_uid		= uid;
	inode_u->bi_gid		= gid;
	inode_u->bi_dev		= rdev;
	inode_u->bi_atime	= now;
	inode_u->bi_mtime	= now;
	inode_u->bi_ctime	= now;
	inode_u->bi_otime	= now;

	if (parent) {
#define BCH_INODE_FIELD(_name)	inode_u->_name = parent->_name;
		BCH_INODE_FIELDS_INHERIT()
#undef BCH_INODE_FIELD
	}
}

static inline u32 bkey_generation(struct bkey_s_c k)
{
	switch (k.k->type) {
	case BCH_INODE_BLOCKDEV:
	case BCH_INODE_FS:
		BUG();
	case BCH_INODE_GENERATION:
		return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
	default:
		return 0;
	}
}

int __bch2_inode_create(struct btree_trans *trans,
			struct bch_inode_unpacked *inode_u,
			u64 min, u64 max, u64 *hint)
{
	struct bch_fs *c = trans->c;
	struct bkey_inode_buf *inode_p;
	struct btree_iter *iter;
	u64 start;
	int ret;

	if (!max)
		max = ULLONG_MAX;

	if (c->opts.inodes_32bit)
		max = min_t(u64, max, U32_MAX);

	start = READ_ONCE(*hint);

	if (start >= max || start < min)
		start = min;

	inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
	if (IS_ERR(inode_p))
		return PTR_ERR(inode_p);

	iter = bch2_trans_get_iter(trans,
			BTREE_ID_INODES, POS(start, 0),
			BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
	if (IS_ERR(iter))
		return PTR_ERR(iter);
again:
	while (1) {
		struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);

		ret = btree_iter_err(k);
		if (ret)
			return ret;

		switch (k.k->type) {
		case BCH_INODE_BLOCKDEV:
		case BCH_INODE_FS:
			/* slot used */
			if (iter->pos.inode >= max)
				goto out;

			bch2_btree_iter_next_slot(iter);
			break;

		default:
			*hint			= k.k->p.inode;
			inode_u->bi_inum	= k.k->p.inode;
			inode_u->bi_generation	= bkey_generation(k);

			bch2_inode_pack(inode_p, inode_u);
			bch2_trans_update(trans,
				BTREE_INSERT_ENTRY(iter, &inode_p->inode.k_i));
			return 0;
		}
	}
out:
	if (start != min) {
		/* Retry from start */
		start = min;
		bch2_btree_iter_set_pos(iter, POS(start, 0));
		goto again;
	}

	return -ENOSPC;
}

int bch2_inode_create(struct bch_fs *c, struct bch_inode_unpacked *inode_u,
		      u64 min, u64 max, u64 *hint)
{
	return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
			__bch2_inode_create(&trans, inode_u, min, max, hint));
}

int bch2_inode_rm(struct bch_fs *c, u64 inode_nr)
{
	struct btree_iter iter;
	struct bkey_i_inode_generation delete;
	struct bpos start = POS(inode_nr, 0);
	struct bpos end = POS(inode_nr + 1, 0);
	int ret;

	/*
	 * If this was a directory, there shouldn't be any real dirents left -
	 * but there could be whiteouts (from hash collisions) that we should
	 * delete:
	 *
	 * XXX: the dirent could ideally would delete whiteouts when they're no
	 * longer needed
	 */
	ret   = bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
					start, end, NULL) ?:
		bch2_btree_delete_range(c, BTREE_ID_XATTRS,
					start, end, NULL) ?:
		bch2_btree_delete_range(c, BTREE_ID_DIRENTS,
					start, end, NULL);
	if (ret)
		return ret;

	bch2_btree_iter_init(&iter, c, BTREE_ID_INODES, POS(inode_nr, 0),
			     BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
	do {
		struct bkey_s_c k = bch2_btree_iter_peek_slot(&iter);
		u32 bi_generation = 0;

		ret = btree_iter_err(k);
		if (ret) {
			bch2_btree_iter_unlock(&iter);
			return ret;
		}

		bch2_fs_inconsistent_on(k.k->type != BCH_INODE_FS, c,
					"inode %llu not found when deleting",
					inode_nr);

		switch (k.k->type) {
		case BCH_INODE_FS: {
			struct bch_inode_unpacked inode_u;

			if (!bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u))
				bi_generation = inode_u.bi_generation + 1;
			break;
		}
		case BCH_INODE_GENERATION: {
			struct bkey_s_c_inode_generation g =
				bkey_s_c_to_inode_generation(k);
			bi_generation = le32_to_cpu(g.v->bi_generation);
			break;
		}
		}

		if (!bi_generation) {
			bkey_init(&delete.k);
			delete.k.p.inode = inode_nr;
		} else {
			bkey_inode_generation_init(&delete.k_i);
			delete.k.p.inode = inode_nr;
			delete.v.bi_generation = cpu_to_le32(bi_generation);
		}

		ret = bch2_btree_insert_at(c, NULL, NULL,
				BTREE_INSERT_ATOMIC|
				BTREE_INSERT_NOFAIL,
				BTREE_INSERT_ENTRY(&iter, &delete.k_i));
	} while (ret == -EINTR);

	bch2_btree_iter_unlock(&iter);
	return ret;
}

int bch2_inode_find_by_inum(struct bch_fs *c, u64 inode_nr,
			    struct bch_inode_unpacked *inode)
{
	struct btree_iter iter;
	struct bkey_s_c k;
	int ret = -ENOENT;

	for_each_btree_key(&iter, c, BTREE_ID_INODES,
			   POS(inode_nr, 0),
			   BTREE_ITER_SLOTS, k) {
		switch (k.k->type) {
		case BCH_INODE_FS:
			ret = bch2_inode_unpack(bkey_s_c_to_inode(k), inode);
			break;
		default:
			/* hole, not found */
			break;
		}

		break;

	}

	return bch2_btree_iter_unlock(&iter) ?: ret;
}

#ifdef CONFIG_BCACHEFS_DEBUG
void bch2_inode_pack_test(void)
{
	struct bch_inode_unpacked *u, test_inodes[] = {
		{
			.bi_atime	= U64_MAX,
			.bi_ctime	= U64_MAX,
			.bi_mtime	= U64_MAX,
			.bi_otime	= U64_MAX,
			.bi_size	= U64_MAX,
			.bi_sectors	= U64_MAX,
			.bi_uid		= U32_MAX,
			.bi_gid		= U32_MAX,
			.bi_nlink	= U32_MAX,
			.bi_generation	= U32_MAX,
			.bi_dev		= U32_MAX,
		},
	};

	for (u = test_inodes;
	     u < test_inodes + ARRAY_SIZE(test_inodes);
	     u++) {
		struct bkey_inode_buf p;

		bch2_inode_pack(&p, u);
	}
}
#endif