2017-03-17 14:18:50 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
2019-09-23 07:10:21 +08:00
|
|
|
#include "btree_key_cache.h"
|
2017-03-17 14:18:50 +08:00
|
|
|
#include "bkey_methods.h"
|
|
|
|
#include "btree_update.h"
|
2021-12-23 11:39:50 +08:00
|
|
|
#include "buckets.h"
|
2017-03-17 14:18:50 +08:00
|
|
|
#include "error.h"
|
|
|
|
#include "extents.h"
|
2021-03-16 12:28:17 +08:00
|
|
|
#include "extent_update.h"
|
2017-03-17 14:18:50 +08:00
|
|
|
#include "inode.h"
|
2019-10-05 03:58:43 +08:00
|
|
|
#include "str_hash.h"
|
2021-03-16 12:42:25 +08:00
|
|
|
#include "subvolume.h"
|
2020-11-06 12:39:33 +08:00
|
|
|
#include "varint.h"
|
2017-03-17 14:18:50 +08:00
|
|
|
|
|
|
|
#include <linux/random.h>
|
|
|
|
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
2018-12-17 18:31:09 +08:00
|
|
|
const char * const bch2_inode_opts[] = {
|
|
|
|
#define x(name, ...) #name,
|
|
|
|
BCH_INODE_OPTS()
|
|
|
|
#undef x
|
|
|
|
NULL,
|
|
|
|
};
|
2017-03-17 14:18:50 +08:00
|
|
|
|
|
|
|
static const u8 byte_table[8] = { 1, 2, 3, 4, 6, 8, 10, 13 };
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:09:02 +08:00
|
|
|
static inline void bch2_inode_pack_inlined(struct bch_fs *c,
|
|
|
|
struct bkey_inode_buf *packed,
|
|
|
|
const struct bch_inode_unpacked *inode)
|
2020-11-06 12:39:33 +08:00
|
|
|
{
|
2021-10-30 09:14:23 +08:00
|
|
|
struct bkey_i_inode_v2 *k = &packed->inode;
|
2020-11-06 12:39:33 +08:00
|
|
|
u8 *out = k->v.fields;
|
|
|
|
u8 *end = (void *) &packed[1];
|
|
|
|
u8 *last_nonzero_field = out;
|
|
|
|
unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
|
|
|
|
unsigned bytes;
|
|
|
|
int ret;
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
bkey_inode_v2_init(&packed->inode.k_i);
|
|
|
|
packed->inode.k.p.offset = inode->bi_inum;
|
|
|
|
packed->inode.v.bi_journal_seq = cpu_to_le64(inode->bi_journal_seq);
|
|
|
|
packed->inode.v.bi_hash_seed = inode->bi_hash_seed;
|
|
|
|
packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
|
|
|
|
packed->inode.v.bi_flags = cpu_to_le64(inode->bi_flags);
|
|
|
|
packed->inode.v.bi_mode = cpu_to_le16(inode->bi_mode);
|
|
|
|
|
2020-11-06 12:39:33 +08:00
|
|
|
#define x(_name, _bits) \
|
|
|
|
nr_fields++; \
|
|
|
|
\
|
|
|
|
if (inode->_name) { \
|
2021-07-14 04:03:51 +08:00
|
|
|
ret = bch2_varint_encode_fast(out, inode->_name); \
|
2020-11-06 12:39:33 +08:00
|
|
|
out += ret; \
|
|
|
|
\
|
|
|
|
if (_bits > 64) \
|
|
|
|
*out++ = 0; \
|
|
|
|
\
|
|
|
|
last_nonzero_field = out; \
|
|
|
|
last_nonzero_fieldnr = nr_fields; \
|
|
|
|
} else { \
|
|
|
|
*out++ = 0; \
|
|
|
|
\
|
|
|
|
if (_bits > 64) \
|
|
|
|
*out++ = 0; \
|
|
|
|
}
|
|
|
|
|
|
|
|
BCH_INODE_FIELDS()
|
|
|
|
#undef x
|
|
|
|
BUG_ON(out > end);
|
|
|
|
|
|
|
|
out = last_nonzero_field;
|
|
|
|
nr_fields = last_nonzero_fieldnr;
|
|
|
|
|
|
|
|
bytes = out - (u8 *) &packed->inode.v;
|
|
|
|
set_bkey_val_bytes(&packed->inode.k, bytes);
|
|
|
|
memset_u64s_tail(&packed->inode.v, 0, bytes);
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
SET_INODEv2_NR_FIELDS(&k->v, nr_fields);
|
2017-03-17 14:18:50 +08:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG)) {
|
|
|
|
struct bch_inode_unpacked unpacked;
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
int ret = bch2_inode_unpack(bkey_i_to_s_c(&packed->inode.k_i),
|
2017-03-17 14:18:50 +08:00
|
|
|
&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);
|
|
|
|
|
2020-11-06 12:39:33 +08:00
|
|
|
#define x(_name, _bits) if (unpacked._name != inode->_name) \
|
|
|
|
panic("unpacked %llu should be %llu", \
|
|
|
|
(u64) unpacked._name, (u64) inode->_name);
|
2017-03-17 14:18:50 +08:00
|
|
|
BCH_INODE_FIELDS()
|
2018-12-13 19:01:30 +08:00
|
|
|
#undef x
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:09:02 +08:00
|
|
|
void bch2_inode_pack(struct bch_fs *c,
|
|
|
|
struct bkey_inode_buf *packed,
|
|
|
|
const struct bch_inode_unpacked *inode)
|
|
|
|
{
|
|
|
|
bch2_inode_pack_inlined(c, packed, inode);
|
|
|
|
}
|
|
|
|
|
2020-11-06 12:39:33 +08:00
|
|
|
static noinline int bch2_inode_unpack_v1(struct bkey_s_c_inode inode,
|
|
|
|
struct bch_inode_unpacked *unpacked)
|
2017-03-17 14:18:50 +08:00
|
|
|
{
|
|
|
|
const u8 *in = inode.v->fields;
|
2020-11-06 12:39:33 +08:00
|
|
|
const u8 *end = bkey_val_end(inode);
|
2017-03-17 14:18:50 +08:00
|
|
|
u64 field[2];
|
|
|
|
unsigned fieldnr = 0, field_bits;
|
|
|
|
int ret;
|
|
|
|
|
2018-12-13 19:01:30 +08:00
|
|
|
#define x(_name, _bits) \
|
2017-03-17 14:18:50 +08:00
|
|
|
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()
|
2018-12-13 19:01:30 +08:00
|
|
|
#undef x
|
2017-03-17 14:18:50 +08:00
|
|
|
|
|
|
|
/* XXX: signal if there were more fields than expected? */
|
2020-11-06 12:39:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
static int bch2_inode_unpack_v2(struct bch_inode_unpacked *unpacked,
|
|
|
|
const u8 *in, const u8 *end,
|
|
|
|
unsigned nr_fields)
|
2020-11-06 12:39:33 +08:00
|
|
|
{
|
|
|
|
unsigned fieldnr = 0;
|
|
|
|
int ret;
|
|
|
|
u64 v[2];
|
|
|
|
|
|
|
|
#define x(_name, _bits) \
|
2021-10-30 09:14:23 +08:00
|
|
|
if (fieldnr < nr_fields) { \
|
2021-07-14 04:03:51 +08:00
|
|
|
ret = bch2_varint_decode_fast(in, end, &v[0]); \
|
2020-11-06 12:39:33 +08:00
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
in += ret; \
|
|
|
|
\
|
|
|
|
if (_bits > 64) { \
|
2021-07-14 04:03:51 +08:00
|
|
|
ret = bch2_varint_decode_fast(in, end, &v[1]); \
|
2020-11-06 12:39:33 +08:00
|
|
|
if (ret < 0) \
|
|
|
|
return ret; \
|
|
|
|
in += ret; \
|
|
|
|
} else { \
|
|
|
|
v[1] = 0; \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
v[0] = v[1] = 0; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
unpacked->_name = v[0]; \
|
|
|
|
if (v[1] || v[0] != unpacked->_name) \
|
|
|
|
return -1; \
|
|
|
|
fieldnr++;
|
|
|
|
|
|
|
|
BCH_INODE_FIELDS()
|
|
|
|
#undef x
|
|
|
|
|
|
|
|
/* XXX: signal if there were more fields than expected? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
int bch2_inode_unpack(struct bkey_s_c k,
|
2020-11-06 12:39:33 +08:00
|
|
|
struct bch_inode_unpacked *unpacked)
|
|
|
|
{
|
2021-10-30 09:14:23 +08:00
|
|
|
switch (k.k->type) {
|
|
|
|
case KEY_TYPE_inode: {
|
|
|
|
struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
|
|
|
|
|
|
|
|
unpacked->bi_inum = inode.k->p.offset;
|
2021-11-14 06:57:52 +08:00
|
|
|
unpacked->bi_journal_seq= 0;
|
2021-10-30 09:14:23 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (INODE_NEW_VARINT(inode.v)) {
|
|
|
|
return bch2_inode_unpack_v2(unpacked, inode.v->fields,
|
|
|
|
bkey_val_end(inode),
|
|
|
|
INODE_NR_FIELDS(inode.v));
|
|
|
|
} else {
|
|
|
|
return bch2_inode_unpack_v1(inode, unpacked);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KEY_TYPE_inode_v2: {
|
|
|
|
struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
|
|
|
|
|
|
|
|
unpacked->bi_inum = inode.k->p.offset;
|
|
|
|
unpacked->bi_journal_seq= le64_to_cpu(inode.v->bi_journal_seq);
|
|
|
|
unpacked->bi_hash_seed = inode.v->bi_hash_seed;
|
|
|
|
unpacked->bi_flags = le64_to_cpu(inode.v->bi_flags);
|
|
|
|
unpacked->bi_mode = le16_to_cpu(inode.v->bi_mode);
|
|
|
|
|
|
|
|
return bch2_inode_unpack_v2(unpacked, inode.v->fields,
|
|
|
|
bkey_val_end(inode),
|
|
|
|
INODEv2_NR_FIELDS(inode.v));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
BUG();
|
2020-11-06 12:39:33 +08:00
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
|
|
|
|
2021-08-31 03:18:31 +08:00
|
|
|
int bch2_inode_peek(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter,
|
|
|
|
struct bch_inode_unpacked *inode,
|
2021-03-16 12:28:17 +08:00
|
|
|
subvol_inum inum, unsigned flags)
|
2019-10-02 04:51:57 +08:00
|
|
|
{
|
|
|
|
struct bkey_s_c k;
|
2021-03-16 12:28:17 +08:00
|
|
|
u32 snapshot;
|
2019-10-02 04:51:57 +08:00
|
|
|
int ret;
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
|
2022-01-12 15:13:21 +08:00
|
|
|
SPOS(0, inum.inum, snapshot),
|
|
|
|
flags|BTREE_ITER_CACHED);
|
2021-06-11 08:15:50 +08:00
|
|
|
k = bch2_btree_iter_peek_slot(iter);
|
2019-10-02 04:51:57 +08:00
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
ret = bkey_is_inode(k.k) ? 0 : -ENOENT;
|
2019-10-02 04:51:57 +08:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
ret = bch2_inode_unpack(k, inode);
|
2019-10-02 04:51:57 +08:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2021-08-31 03:18:31 +08:00
|
|
|
return 0;
|
2019-10-02 04:51:57 +08:00
|
|
|
err:
|
2021-08-31 03:18:31 +08:00
|
|
|
bch2_trans_iter_exit(trans, iter);
|
|
|
|
return ret;
|
2019-10-02 04:51:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_inode_write(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter,
|
|
|
|
struct bch_inode_unpacked *inode)
|
|
|
|
{
|
|
|
|
struct bkey_inode_buf *inode_p;
|
|
|
|
|
|
|
|
inode_p = bch2_trans_kmalloc(trans, sizeof(*inode_p));
|
|
|
|
if (IS_ERR(inode_p))
|
|
|
|
return PTR_ERR(inode_p);
|
|
|
|
|
2022-10-17 19:09:02 +08:00
|
|
|
bch2_inode_pack_inlined(trans->c, inode_p, inode);
|
bcachefs: Start using bpos.snapshot field
This patch starts treating the bpos.snapshot field like part of the key
in the btree code:
* bpos_successor() and bpos_predecessor() now include the snapshot field
* Keys in btrees that will be using snapshots (extents, inodes, dirents
and xattrs) now always have their snapshot field set to U32_MAX
The btree iterator code gets a new flag, BTREE_ITER_ALL_SNAPSHOTS, that
determines whether we're iterating over keys in all snapshots or not -
internally, this controlls whether bkey_(successor|predecessor)
increment/decrement the snapshot field, or only the higher bits of the
key.
We add a new member to struct btree_iter, iter->snapshot: when
BTREE_ITER_ALL_SNAPSHOTS is not set, iter->pos.snapshot should always
equal iter->snapshot, which will be 0 for btrees that don't use
snapshots, and alsways U32_MAX for btrees that will use snapshots
(until we enable snapshot creation).
This patch also introduces a new metadata version number, and compat
code for reading from/writing to older versions - this isn't a forced
upgrade (yet).
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2021-03-25 06:02:16 +08:00
|
|
|
inode_p->inode.k.p.snapshot = iter->snapshot;
|
2021-06-02 12:15:07 +08:00
|
|
|
return bch2_trans_update(trans, iter, &inode_p->inode.k_i, 0);
|
2019-10-02 04:51:57 +08:00
|
|
|
}
|
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
static int __bch2_inode_invalid(struct bkey_s_c k, struct printbuf *err)
|
2017-03-17 14:18:50 +08:00
|
|
|
{
|
2021-03-16 12:42:25 +08:00
|
|
|
struct bch_inode_unpacked unpacked;
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (k.k->p.inode) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "nonzero k.p.inode");
|
2022-04-04 05:50:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (k.k->p.offset < BLOCKDEV_INODE_MAX) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "fs inode in blockdev range");
|
2022-04-04 05:50:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-10-20 06:31:33 +08:00
|
|
|
if (bch2_inode_unpack(k, &unpacked)) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "invalid variable length fields");
|
2022-04-04 05:50:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-30 09:14:23 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (unpacked.bi_data_checksum >= BCH_CSUM_OPT_NR + 1) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "invalid data checksum type (%u >= %u",
|
2022-04-04 05:50:01 +08:00
|
|
|
unpacked.bi_data_checksum, BCH_CSUM_OPT_NR + 1);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-30 09:14:23 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (unpacked.bi_compression >= BCH_COMPRESSION_OPT_NR + 1) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "invalid data checksum type (%u >= %u)",
|
2022-04-04 05:50:01 +08:00
|
|
|
unpacked.bi_compression, BCH_COMPRESSION_OPT_NR + 1);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-30 09:14:23 +08:00
|
|
|
|
|
|
|
if ((unpacked.bi_flags & BCH_INODE_UNLINKED) &&
|
2022-04-04 05:50:01 +08:00
|
|
|
unpacked.bi_nlink != 0) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "flagged as unlinked but bi_nlink != 0");
|
2022-04-04 05:50:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-30 09:14:23 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (unpacked.bi_subvol && !S_ISDIR(unpacked.bi_mode)) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "subvolume root but not a directory");
|
2022-04-04 05:50:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-30 09:14:23 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
return 0;
|
2021-10-30 09:14:23 +08:00
|
|
|
}
|
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
int bch2_inode_invalid(const struct bch_fs *c, struct bkey_s_c k,
|
2022-04-04 09:50:25 +08:00
|
|
|
int rw, struct printbuf *err)
|
2021-10-30 09:14:23 +08:00
|
|
|
{
|
2022-04-04 05:50:01 +08:00
|
|
|
struct bkey_s_c_inode inode = bkey_s_c_to_inode(k);
|
2021-10-30 09:14:23 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "incorrect value size (%zu < %zu)",
|
2022-04-04 05:50:01 +08:00
|
|
|
bkey_val_bytes(k.k), sizeof(*inode.v));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-10-30 09:14:23 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (INODE_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "invalid str hash type (%llu >= %u)",
|
2022-04-04 05:50:01 +08:00
|
|
|
INODE_STR_HASH(inode.v), BCH_STR_HASH_NR);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
return __bch2_inode_invalid(k, err);
|
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
int bch2_inode_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
|
2022-04-04 09:50:25 +08:00
|
|
|
int rw, struct printbuf *err)
|
2022-04-04 05:50:01 +08:00
|
|
|
{
|
|
|
|
struct bkey_s_c_inode_v2 inode = bkey_s_c_to_inode_v2(k);
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (bkey_val_bytes(k.k) < sizeof(*inode.v)) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "incorrect value size (%zu < %zu)",
|
2022-04-04 05:50:01 +08:00
|
|
|
bkey_val_bytes(k.k), sizeof(*inode.v));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (INODEv2_STR_HASH(inode.v) >= BCH_STR_HASH_NR) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "invalid str hash type (%llu >= %u)",
|
2022-04-04 05:50:01 +08:00
|
|
|
INODEv2_STR_HASH(inode.v), BCH_STR_HASH_NR);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2021-03-16 12:42:25 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
return __bch2_inode_invalid(k, err);
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
|
|
|
|
2021-07-31 05:59:37 +08:00
|
|
|
static void __bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
|
|
|
|
{
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(out, "mode %o flags %x journal_seq %llu",
|
2021-10-30 09:14:23 +08:00
|
|
|
inode->bi_mode, inode->bi_flags,
|
|
|
|
inode->bi_journal_seq);
|
2021-07-31 05:59:37 +08:00
|
|
|
|
|
|
|
#define x(_name, _bits) \
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(out, " "#_name " %llu", (u64) inode->_name);
|
2021-07-31 05:59:37 +08:00
|
|
|
BCH_INODE_FIELDS()
|
|
|
|
#undef x
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_inode_unpacked_to_text(struct printbuf *out, struct bch_inode_unpacked *inode)
|
|
|
|
{
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(out, "inum: %llu ", inode->bi_inum);
|
2021-07-31 05:59:37 +08:00
|
|
|
__bch2_inode_unpacked_to_text(out, inode);
|
|
|
|
}
|
|
|
|
|
2018-11-09 14:24:07 +08:00
|
|
|
void bch2_inode_to_text(struct printbuf *out, struct bch_fs *c,
|
|
|
|
struct bkey_s_c k)
|
2017-03-17 14:18:50 +08:00
|
|
|
{
|
2021-10-30 09:14:23 +08:00
|
|
|
struct bch_inode_unpacked inode;
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
if (bch2_inode_unpack(k, &inode)) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(out, "(unpack error)");
|
2018-11-02 03:10:01 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
__bch2_inode_unpacked_to_text(out, &inode);
|
2018-11-02 03:10:01 +08:00
|
|
|
}
|
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
int bch2_inode_generation_invalid(const struct bch_fs *c, struct bkey_s_c k,
|
2022-04-04 09:50:25 +08:00
|
|
|
int rw, struct printbuf *err)
|
2018-11-02 03:10:01 +08:00
|
|
|
{
|
2022-04-04 05:50:01 +08:00
|
|
|
if (k.k->p.inode) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "nonzero k.p.inode");
|
2022-04-04 05:50:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-11-02 03:10:01 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
if (bkey_val_bytes(k.k) != sizeof(struct bch_inode_generation)) {
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(err, "incorrect value size (%zu != %zu)",
|
2022-04-04 05:50:01 +08:00
|
|
|
bkey_val_bytes(k.k), sizeof(struct bch_inode_generation));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-11-02 03:10:01 +08:00
|
|
|
|
2022-04-04 05:50:01 +08:00
|
|
|
return 0;
|
2018-11-02 03:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_inode_generation_to_text(struct printbuf *out, struct bch_fs *c,
|
|
|
|
struct bkey_s_c k)
|
|
|
|
{
|
2019-06-25 05:55:15 +08:00
|
|
|
struct bkey_s_c_inode_generation gen = bkey_s_c_to_inode_generation(k);
|
|
|
|
|
2023-02-04 10:01:40 +08:00
|
|
|
prt_printf(out, "generation: %u", le32_to_cpu(gen.v->bi_generation));
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
|
|
|
|
2019-10-03 06:35:36 +08:00
|
|
|
void bch2_inode_init_early(struct bch_fs *c,
|
|
|
|
struct bch_inode_unpacked *inode_u)
|
2017-03-17 14:18:50 +08:00
|
|
|
{
|
2019-10-05 03:58:43 +08:00
|
|
|
enum bch_str_hash_type str_hash =
|
|
|
|
bch2_str_hash_opt_to_type(c, c->opts.str_hash);
|
2017-03-17 14:18:50 +08:00
|
|
|
|
|
|
|
memset(inode_u, 0, sizeof(*inode_u));
|
|
|
|
|
|
|
|
/* ick */
|
2019-10-05 03:58:43 +08:00
|
|
|
inode_u->bi_flags |= str_hash << INODE_STR_HASH_OFFSET;
|
2018-12-17 19:11:14 +08:00
|
|
|
get_random_bytes(&inode_u->bi_hash_seed,
|
|
|
|
sizeof(inode_u->bi_hash_seed));
|
2019-10-03 06:35:36 +08:00
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2019-10-03 06:35:36 +08:00
|
|
|
void 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)
|
|
|
|
{
|
2017-03-17 14:18:50 +08:00
|
|
|
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;
|
|
|
|
|
2019-10-03 06:35:36 +08:00
|
|
|
if (parent && parent->bi_mode & S_ISGID) {
|
|
|
|
inode_u->bi_gid = parent->bi_gid;
|
|
|
|
if (S_ISDIR(mode))
|
|
|
|
inode_u->bi_mode |= S_ISGID;
|
|
|
|
}
|
|
|
|
|
2017-03-17 14:18:50 +08:00
|
|
|
if (parent) {
|
2018-12-13 21:24:21 +08:00
|
|
|
#define x(_name, ...) inode_u->bi_##_name = parent->bi_##_name;
|
|
|
|
BCH_INODE_OPTS()
|
2018-12-13 19:01:30 +08:00
|
|
|
#undef x
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 06:35:36 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
bch2_inode_init_early(c, inode_u);
|
|
|
|
bch2_inode_init_late(inode_u, bch2_current_time(c),
|
|
|
|
uid, gid, mode, rdev, parent);
|
|
|
|
}
|
|
|
|
|
2017-03-17 14:18:50 +08:00
|
|
|
static inline u32 bkey_generation(struct bkey_s_c k)
|
|
|
|
{
|
|
|
|
switch (k.k->type) {
|
2018-11-02 03:10:01 +08:00
|
|
|
case KEY_TYPE_inode:
|
2021-10-30 09:14:23 +08:00
|
|
|
case KEY_TYPE_inode_v2:
|
2017-03-17 14:18:50 +08:00
|
|
|
BUG();
|
2018-11-02 03:10:01 +08:00
|
|
|
case KEY_TYPE_inode_generation:
|
2017-03-17 14:18:50 +08:00
|
|
|
return le32_to_cpu(bkey_s_c_to_inode_generation(k).v->bi_generation);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
/*
|
|
|
|
* This just finds an empty slot:
|
|
|
|
*/
|
2021-08-31 03:18:31 +08:00
|
|
|
int bch2_inode_create(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter,
|
|
|
|
struct bch_inode_unpacked *inode_u,
|
|
|
|
u32 snapshot, u64 cpu)
|
2020-10-28 06:56:21 +08:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct bkey_s_c k;
|
2021-03-16 07:18:30 +08:00
|
|
|
u64 min, max, start, pos, *hint;
|
2021-05-15 08:02:44 +08:00
|
|
|
int ret = 0;
|
2021-05-28 08:20:20 +08:00
|
|
|
unsigned bits = (c->opts.inodes_32bit ? 31 : 63);
|
2020-11-03 12:51:33 +08:00
|
|
|
|
2021-05-28 08:20:20 +08:00
|
|
|
if (c->opts.shard_inode_numbers) {
|
|
|
|
bits -= c->inode_shard_bits;
|
2020-11-03 12:51:33 +08:00
|
|
|
|
2021-05-28 08:20:20 +08:00
|
|
|
min = (cpu << bits);
|
|
|
|
max = (cpu << bits) | ~(ULLONG_MAX << bits);
|
|
|
|
|
|
|
|
min = max_t(u64, min, BLOCKDEV_INODE_MAX);
|
|
|
|
hint = c->unused_inode_hints + cpu;
|
|
|
|
} else {
|
|
|
|
min = BLOCKDEV_INODE_MAX;
|
|
|
|
max = ~(ULLONG_MAX << bits);
|
|
|
|
hint = c->unused_inode_hints;
|
|
|
|
}
|
2020-11-03 12:51:33 +08:00
|
|
|
|
|
|
|
start = READ_ONCE(*hint);
|
|
|
|
|
|
|
|
if (start >= max || start < min)
|
|
|
|
start = min;
|
2021-03-16 07:18:30 +08:00
|
|
|
|
|
|
|
pos = start;
|
2021-08-31 03:18:31 +08:00
|
|
|
bch2_trans_iter_init(trans, iter, BTREE_ID_inodes, POS(0, pos),
|
|
|
|
BTREE_ITER_ALL_SNAPSHOTS|
|
|
|
|
BTREE_ITER_INTENT);
|
2020-11-03 12:51:33 +08:00
|
|
|
again:
|
2021-03-16 07:18:30 +08:00
|
|
|
while ((k = bch2_btree_iter_peek(iter)).k &&
|
|
|
|
!(ret = bkey_err(k)) &&
|
2022-11-24 16:12:22 +08:00
|
|
|
bkey_lt(k.k->p, POS(0, max))) {
|
2023-01-09 15:25:08 +08:00
|
|
|
if (pos < iter->pos.offset)
|
|
|
|
goto found_slot;
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2020-11-03 12:51:33 +08:00
|
|
|
/*
|
2021-03-16 07:18:30 +08:00
|
|
|
* We don't need to iterate over keys in every snapshot once
|
|
|
|
* we've found just one:
|
2020-11-03 12:51:33 +08:00
|
|
|
*/
|
2021-03-16 07:18:30 +08:00
|
|
|
pos = iter->pos.offset + 1;
|
|
|
|
bch2_btree_iter_set_pos(iter, POS(0, pos));
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:25:08 +08:00
|
|
|
if (!ret && pos < max)
|
|
|
|
goto found_slot;
|
2020-10-28 06:56:21 +08:00
|
|
|
|
2021-03-16 07:18:30 +08:00
|
|
|
if (!ret && start == min)
|
2022-09-19 05:10:33 +08:00
|
|
|
ret = -BCH_ERR_ENOSPC_inode_create;
|
2020-10-28 06:56:21 +08:00
|
|
|
|
2021-03-16 07:18:30 +08:00
|
|
|
if (ret) {
|
2021-08-31 03:18:31 +08:00
|
|
|
bch2_trans_iter_exit(trans, iter);
|
|
|
|
return ret;
|
2020-11-03 12:51:33 +08:00
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-03-16 07:18:30 +08:00
|
|
|
/* Retry from start */
|
|
|
|
pos = start = min;
|
|
|
|
bch2_btree_iter_set_pos(iter, POS(0, pos));
|
|
|
|
goto again;
|
2020-11-03 12:51:33 +08:00
|
|
|
found_slot:
|
2021-03-16 07:18:30 +08:00
|
|
|
bch2_btree_iter_set_pos(iter, SPOS(0, pos, snapshot));
|
|
|
|
k = bch2_btree_iter_peek_slot(iter);
|
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret) {
|
2021-08-31 03:18:31 +08:00
|
|
|
bch2_trans_iter_exit(trans, iter);
|
|
|
|
return ret;
|
2021-03-16 07:18:30 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:51:33 +08:00
|
|
|
*hint = k.k->p.offset;
|
|
|
|
inode_u->bi_inum = k.k->p.offset;
|
|
|
|
inode_u->bi_generation = bkey_generation(k);
|
2021-08-31 03:18:31 +08:00
|
|
|
return 0;
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
static int bch2_inode_delete_keys(struct btree_trans *trans,
|
|
|
|
subvol_inum inum, enum btree_id id)
|
|
|
|
{
|
2021-12-28 07:25:23 +08:00
|
|
|
struct btree_iter iter;
|
|
|
|
struct bkey_s_c k;
|
|
|
|
struct bkey_i delete;
|
|
|
|
u32 snapshot;
|
2021-03-16 12:28:17 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
2021-12-28 07:25:23 +08:00
|
|
|
/*
|
|
|
|
* We're never going to be deleting extents, no need to use an extent
|
|
|
|
* iterator:
|
|
|
|
*/
|
|
|
|
bch2_trans_iter_init(trans, &iter, id, POS(inum.inum, 0),
|
|
|
|
BTREE_ITER_INTENT);
|
2021-03-16 12:28:17 +08:00
|
|
|
|
2021-12-28 07:25:23 +08:00
|
|
|
while (1) {
|
2021-03-16 12:28:17 +08:00
|
|
|
bch2_trans_begin(trans);
|
|
|
|
|
|
|
|
ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
|
|
|
|
if (ret)
|
2021-12-28 07:25:23 +08:00
|
|
|
goto err;
|
2021-03-16 12:28:17 +08:00
|
|
|
|
2021-12-28 07:25:23 +08:00
|
|
|
bch2_btree_iter_set_snapshot(&iter, snapshot);
|
2021-03-16 12:28:17 +08:00
|
|
|
|
2022-03-12 01:31:52 +08:00
|
|
|
k = bch2_btree_iter_peek_upto(&iter, POS(inum.inum, U64_MAX));
|
2021-03-16 12:28:17 +08:00
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2022-03-12 01:31:52 +08:00
|
|
|
if (!k.k)
|
2021-12-28 07:25:23 +08:00
|
|
|
break;
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
bkey_init(&delete.k);
|
|
|
|
delete.k.p = iter.pos;
|
|
|
|
|
2022-11-18 08:07:40 +08:00
|
|
|
if (iter.flags & BTREE_ITER_IS_EXTENTS) {
|
|
|
|
bch2_key_resize(&delete.k, k.k->p.offset - iter.pos.offset);
|
|
|
|
|
|
|
|
ret = bch2_extent_trim_atomic(trans, &iter, &delete);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
ret = bch2_trans_update(trans, &iter, &delete, 0) ?:
|
2021-12-28 07:25:23 +08:00
|
|
|
bch2_trans_commit(trans, NULL, NULL,
|
2021-03-16 12:28:17 +08:00
|
|
|
BTREE_INSERT_NOFAIL);
|
|
|
|
err:
|
2022-07-18 11:06:38 +08:00
|
|
|
if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2021-12-28 07:25:23 +08:00
|
|
|
break;
|
2021-03-16 12:28:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 07:25:23 +08:00
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2021-03-16 12:28:17 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-12 15:13:21 +08:00
|
|
|
int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
|
2017-03-17 14:18:50 +08:00
|
|
|
{
|
2019-03-14 08:49:16 +08:00
|
|
|
struct btree_trans trans;
|
2021-08-31 03:18:31 +08:00
|
|
|
struct btree_iter iter = { NULL };
|
2017-03-17 14:18:50 +08:00
|
|
|
struct bkey_i_inode_generation delete;
|
2021-03-22 10:01:12 +08:00
|
|
|
struct bch_inode_unpacked inode_u;
|
2019-09-23 07:10:21 +08:00
|
|
|
struct bkey_s_c k;
|
2021-03-16 12:28:17 +08:00
|
|
|
u32 snapshot;
|
2017-03-17 14:18:50 +08:00
|
|
|
int ret;
|
|
|
|
|
2021-06-03 11:31:42 +08:00
|
|
|
bch2_trans_init(&trans, c, 0, 1024);
|
2020-11-21 10:28:55 +08:00
|
|
|
|
2017-03-17 14:18:50 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2021-03-16 12:28:17 +08:00
|
|
|
ret = bch2_inode_delete_keys(&trans, inum, BTREE_ID_extents) ?:
|
|
|
|
bch2_inode_delete_keys(&trans, inum, BTREE_ID_xattrs) ?:
|
|
|
|
bch2_inode_delete_keys(&trans, inum, BTREE_ID_dirents);
|
2018-08-09 07:53:30 +08:00
|
|
|
if (ret)
|
2020-11-21 10:28:55 +08:00
|
|
|
goto err;
|
2019-09-23 07:10:21 +08:00
|
|
|
retry:
|
|
|
|
bch2_trans_begin(&trans);
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2021-08-31 03:18:31 +08:00
|
|
|
bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
|
2022-01-12 15:13:21 +08:00
|
|
|
SPOS(0, inum.inum, snapshot),
|
|
|
|
BTREE_ITER_INTENT|BTREE_ITER_CACHED);
|
2021-08-31 03:18:31 +08:00
|
|
|
k = bch2_btree_iter_peek_slot(&iter);
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2019-09-23 07:10:21 +08:00
|
|
|
ret = bkey_err(k);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
if (!bkey_is_inode(k.k)) {
|
2021-03-22 10:01:12 +08:00
|
|
|
bch2_fs_inconsistent(trans.c,
|
|
|
|
"inode %llu not found when deleting",
|
2021-03-16 12:28:17 +08:00
|
|
|
inum.inum);
|
2021-03-22 10:01:12 +08:00
|
|
|
ret = -EIO;
|
|
|
|
goto err;
|
2019-09-23 07:10:21 +08:00
|
|
|
}
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-10-30 09:14:23 +08:00
|
|
|
bch2_inode_unpack(k, &inode_u);
|
2021-03-22 10:01:12 +08:00
|
|
|
|
2021-03-16 12:42:25 +08:00
|
|
|
/* Subvolume root? */
|
2021-10-12 00:03:19 +08:00
|
|
|
BUG_ON(inode_u.bi_subvol);
|
2021-03-16 12:42:25 +08:00
|
|
|
|
2021-03-22 10:01:12 +08:00
|
|
|
bkey_inode_generation_init(&delete.k_i);
|
2021-08-31 03:18:31 +08:00
|
|
|
delete.k.p = iter.pos;
|
2021-03-22 10:01:12 +08:00
|
|
|
delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-08-31 03:18:31 +08:00
|
|
|
ret = bch2_trans_update(&trans, &iter, &delete.k_i, 0) ?:
|
2021-06-02 12:15:07 +08:00
|
|
|
bch2_trans_commit(&trans, NULL, NULL,
|
2019-09-23 07:10:21 +08:00
|
|
|
BTREE_INSERT_NOFAIL);
|
|
|
|
err:
|
2021-08-31 03:18:31 +08:00
|
|
|
bch2_trans_iter_exit(&trans, &iter);
|
2022-07-18 11:06:38 +08:00
|
|
|
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
|
2019-09-23 07:10:21 +08:00
|
|
|
goto retry;
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2019-03-14 08:49:16 +08:00
|
|
|
bch2_trans_exit(&trans);
|
2017-03-17 14:18:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:03:40 +08:00
|
|
|
int bch2_inode_find_by_inum_trans(struct btree_trans *trans,
|
|
|
|
subvol_inum inum,
|
|
|
|
struct bch_inode_unpacked *inode)
|
2017-03-17 14:18:50 +08:00
|
|
|
{
|
2021-03-16 12:28:17 +08:00
|
|
|
struct btree_iter iter;
|
2019-10-26 07:06:26 +08:00
|
|
|
int ret;
|
2017-03-17 14:18:50 +08:00
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
ret = bch2_inode_peek(trans, &iter, inode, inum, 0);
|
|
|
|
if (!ret)
|
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2019-04-01 05:37:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:28:17 +08:00
|
|
|
int bch2_inode_find_by_inum(struct bch_fs *c, subvol_inum inum,
|
2019-04-01 05:37:30 +08:00
|
|
|
struct bch_inode_unpacked *inode)
|
|
|
|
{
|
2019-12-23 12:04:30 +08:00
|
|
|
return bch2_trans_do(c, NULL, NULL, 0,
|
2021-03-16 12:28:17 +08:00
|
|
|
bch2_inode_find_by_inum_trans(&trans, inum, inode));
|
2017-03-17 14:18:50 +08:00
|
|
|
}
|
2022-06-24 06:26:01 +08:00
|
|
|
|
|
|
|
int bch2_inode_nlink_inc(struct bch_inode_unpacked *bi)
|
|
|
|
{
|
|
|
|
if (bi->bi_flags & BCH_INODE_UNLINKED)
|
|
|
|
bi->bi_flags &= ~BCH_INODE_UNLINKED;
|
|
|
|
else {
|
|
|
|
if (bi->bi_nlink == U32_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
bi->bi_nlink++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_inode_nlink_dec(struct btree_trans *trans, struct bch_inode_unpacked *bi)
|
|
|
|
{
|
|
|
|
if (bi->bi_nlink && (bi->bi_flags & BCH_INODE_UNLINKED)) {
|
|
|
|
bch2_trans_inconsistent(trans, "inode %llu unlinked but link count nonzero",
|
|
|
|
bi->bi_inum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bi->bi_flags & BCH_INODE_UNLINKED) {
|
|
|
|
bch2_trans_inconsistent(trans, "inode %llu link count underflow", bi->bi_inum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bi->bi_nlink)
|
|
|
|
bi->bi_nlink--;
|
|
|
|
else
|
|
|
|
bi->bi_flags |= BCH_INODE_UNLINKED;
|
|
|
|
}
|
2022-11-24 09:28:15 +08:00
|
|
|
|
|
|
|
struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
|
|
|
|
{
|
|
|
|
struct bch_opts ret = { 0 };
|
|
|
|
#define x(_name, _bits) \
|
|
|
|
if (inode->bi_##_name) \
|
|
|
|
opt_set(ret, _name, inode->bi_##_name - 1);
|
|
|
|
BCH_INODE_OPTS()
|
|
|
|
#undef x
|
|
|
|
return ret;
|
|
|
|
}
|