2017-03-16 23:18:50 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
2023-09-08 15:14:08 -07:00
|
|
|
#include "acl.h"
|
2017-03-16 23:18:50 -07:00
|
|
|
#include "bkey_methods.h"
|
|
|
|
#include "btree_update.h"
|
|
|
|
#include "extents.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "rebalance.h"
|
|
|
|
#include "str_hash.h"
|
|
|
|
#include "xattr.h"
|
|
|
|
|
|
|
|
#include <linux/dcache.h>
|
|
|
|
#include <linux/posix_acl_xattr.h>
|
|
|
|
#include <linux/xattr.h>
|
|
|
|
|
|
|
|
static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
|
|
|
|
|
|
|
|
static u64 bch2_xattr_hash(const struct bch_hash_info *info,
|
|
|
|
const struct xattr_search_key *key)
|
|
|
|
{
|
|
|
|
struct bch_str_hash_ctx ctx;
|
|
|
|
|
|
|
|
bch2_str_hash_init(&ctx, info);
|
|
|
|
bch2_str_hash_update(&ctx, info, &key->type, sizeof(key->type));
|
|
|
|
bch2_str_hash_update(&ctx, info, key->name.name, key->name.len);
|
|
|
|
|
|
|
|
return bch2_str_hash_end(&ctx, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)
|
|
|
|
{
|
|
|
|
return bch2_xattr_hash(info, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)
|
|
|
|
{
|
|
|
|
struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);
|
|
|
|
|
|
|
|
return bch2_xattr_hash(info,
|
|
|
|
&X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)
|
|
|
|
{
|
|
|
|
struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
|
|
|
|
const struct xattr_search_key *r = _r;
|
|
|
|
|
|
|
|
return l.v->x_type != r->type ||
|
|
|
|
l.v->x_name_len != r->name.len ||
|
|
|
|
memcmp(l.v->x_name, r->name.name, r->name.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)
|
|
|
|
{
|
|
|
|
struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);
|
|
|
|
struct bkey_s_c_xattr r = bkey_s_c_to_xattr(_r);
|
|
|
|
|
|
|
|
return l.v->x_type != r.v->x_type ||
|
|
|
|
l.v->x_name_len != r.v->x_name_len ||
|
|
|
|
memcmp(l.v->x_name, r.v->x_name, r.v->x_name_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct bch_hash_desc bch2_xattr_hash_desc = {
|
2021-02-20 17:27:37 -07:00
|
|
|
.btree_id = BTREE_ID_xattrs,
|
2018-11-01 12:10:01 -07:00
|
|
|
.key_type = KEY_TYPE_xattr,
|
2017-03-16 23:18:50 -07:00
|
|
|
.hash_key = xattr_hash_key,
|
|
|
|
.hash_bkey = xattr_hash_bkey,
|
|
|
|
.cmp_key = xattr_cmp_key,
|
|
|
|
.cmp_bkey = xattr_cmp_bkey,
|
|
|
|
};
|
|
|
|
|
2024-08-12 18:31:25 -07:00
|
|
|
int bch2_xattr_validate(struct bch_fs *c, struct bkey_s_c k,
|
|
|
|
enum bch_validate_flags flags)
|
2017-03-16 23:18:50 -07:00
|
|
|
{
|
2018-11-01 12:10:01 -07:00
|
|
|
struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
|
2023-10-24 17:44:36 -07:00
|
|
|
unsigned val_u64s = xattr_val_u64s(xattr.v->x_name_len,
|
|
|
|
le16_to_cpu(xattr.v->x_val_len));
|
|
|
|
int ret = 0;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2024-08-12 18:31:25 -07:00
|
|
|
bkey_fsck_err_on(bkey_val_u64s(k.k) < val_u64s,
|
|
|
|
c, xattr_val_size_too_small,
|
2023-10-24 17:44:36 -07:00
|
|
|
"value too small (%zu < %u)",
|
|
|
|
bkey_val_u64s(k.k), val_u64s);
|
2022-04-03 14:50:01 -07:00
|
|
|
|
|
|
|
/* XXX why +4 ? */
|
2023-10-24 17:44:36 -07:00
|
|
|
val_u64s = xattr_val_u64s(xattr.v->x_name_len,
|
|
|
|
le16_to_cpu(xattr.v->x_val_len) + 4);
|
|
|
|
|
2024-08-12 18:31:25 -07:00
|
|
|
bkey_fsck_err_on(bkey_val_u64s(k.k) > val_u64s,
|
|
|
|
c, xattr_val_size_too_big,
|
2023-10-24 17:44:36 -07:00
|
|
|
"value too big (%zu > %u)",
|
|
|
|
bkey_val_u64s(k.k), val_u64s);
|
|
|
|
|
2024-08-12 18:31:25 -07:00
|
|
|
bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr.v->x_type),
|
|
|
|
c, xattr_invalid_type,
|
2023-10-24 17:44:36 -07:00
|
|
|
"invalid type (%u)", xattr.v->x_type);
|
|
|
|
|
2024-08-12 18:31:25 -07:00
|
|
|
bkey_fsck_err_on(memchr(xattr.v->x_name, '\0', xattr.v->x_name_len),
|
|
|
|
c, xattr_name_invalid_chars,
|
2023-10-24 17:44:36 -07:00
|
|
|
"xattr name has invalid characters");
|
|
|
|
fsck_err:
|
|
|
|
return ret;
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
2018-11-08 23:24:07 -07:00
|
|
|
void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
|
|
|
|
struct bkey_s_c k)
|
2017-03-16 23:18:50 -07:00
|
|
|
{
|
|
|
|
const struct xattr_handler *handler;
|
2018-11-01 12:10:01 -07:00
|
|
|
struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2018-11-01 12:10:01 -07:00
|
|
|
handler = bch2_xattr_type_to_handler(xattr.v->x_type);
|
|
|
|
if (handler && handler->prefix)
|
2023-02-03 19:01:40 -07:00
|
|
|
prt_printf(out, "%s", handler->prefix);
|
2018-11-01 12:10:01 -07:00
|
|
|
else if (handler)
|
2023-02-03 19:01:40 -07:00
|
|
|
prt_printf(out, "(type %u)", xattr.v->x_type);
|
2018-11-01 12:10:01 -07:00
|
|
|
else
|
2023-02-03 19:01:40 -07:00
|
|
|
prt_printf(out, "(unknown type %u)", xattr.v->x_type);
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2024-05-07 23:50:42 -07:00
|
|
|
unsigned name_len = xattr.v->x_name_len;
|
|
|
|
unsigned val_len = le16_to_cpu(xattr.v->x_val_len);
|
|
|
|
unsigned max_name_val_bytes = bkey_val_bytes(xattr.k) -
|
|
|
|
offsetof(struct bch_xattr, x_name);
|
|
|
|
|
|
|
|
val_len = min_t(int, val_len, max_name_val_bytes - name_len);
|
|
|
|
name_len = min(name_len, max_name_val_bytes);
|
|
|
|
|
2023-02-03 19:01:40 -07:00
|
|
|
prt_printf(out, "%.*s:%.*s",
|
2024-05-07 23:50:42 -07:00
|
|
|
name_len, xattr.v->x_name,
|
|
|
|
val_len, (char *) xattr_val(xattr.v));
|
2023-09-08 15:14:08 -07:00
|
|
|
|
|
|
|
if (xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS ||
|
|
|
|
xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT) {
|
|
|
|
prt_char(out, ' ');
|
|
|
|
bch2_acl_to_text(out, xattr_val(xattr.v),
|
|
|
|
le16_to_cpu(xattr.v->x_val_len));
|
|
|
|
}
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
2021-07-20 18:07:21 -07:00
|
|
|
static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode,
|
|
|
|
const char *name, void *buffer, size_t size, int type)
|
2017-03-16 23:18:50 -07:00
|
|
|
{
|
2021-07-20 18:07:21 -07:00
|
|
|
struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
|
2023-07-06 19:47:42 -07:00
|
|
|
struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
|
2021-08-30 12:18:31 -07:00
|
|
|
struct btree_iter iter;
|
2024-04-07 13:16:49 -07:00
|
|
|
struct bkey_s_c k = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
|
|
|
|
inode_inum(inode), &search, 0);
|
|
|
|
int ret = bkey_err(k);
|
2021-07-24 16:50:40 -07:00
|
|
|
if (ret)
|
2024-04-07 13:16:49 -07:00
|
|
|
return ret;
|
2021-07-24 16:50:40 -07:00
|
|
|
|
2024-04-07 13:16:49 -07:00
|
|
|
struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
|
2017-03-16 23:18:50 -07:00
|
|
|
ret = le16_to_cpu(xattr.v->x_val_len);
|
|
|
|
if (buffer) {
|
|
|
|
if (ret > size)
|
|
|
|
ret = -ERANGE;
|
|
|
|
else
|
|
|
|
memcpy(buffer, xattr_val(xattr.v), ret);
|
|
|
|
}
|
2021-08-30 12:18:31 -07:00
|
|
|
bch2_trans_iter_exit(trans, &iter);
|
2024-04-07 13:16:49 -07:00
|
|
|
return ret;
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
2021-03-15 21:28:17 -07:00
|
|
|
int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
|
2023-07-09 11:18:28 -07:00
|
|
|
struct bch_inode_unpacked *inode_u,
|
2017-03-16 23:18:50 -07:00
|
|
|
const struct bch_hash_info *hash_info,
|
|
|
|
const char *name, const void *value, size_t size,
|
|
|
|
int type, int flags)
|
|
|
|
{
|
2023-07-09 11:18:28 -07:00
|
|
|
struct bch_fs *c = trans->c;
|
2021-11-05 12:17:13 -07:00
|
|
|
struct btree_iter inode_iter = { NULL };
|
2017-03-16 23:18:50 -07:00
|
|
|
int ret;
|
|
|
|
|
2023-12-29 19:16:32 -07:00
|
|
|
ret = bch2_subvol_is_ro_trans(trans, inum.subvol) ?:
|
2024-04-07 15:05:34 -07:00
|
|
|
bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);
|
2023-07-09 11:18:28 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2021-11-05 12:17:13 -07:00
|
|
|
|
2023-07-09 11:18:28 -07:00
|
|
|
inode_u->bi_ctime = bch2_current_time(c);
|
|
|
|
|
|
|
|
ret = bch2_inode_write(trans, &inode_iter, inode_u);
|
2021-11-05 12:17:13 -07:00
|
|
|
bch2_trans_iter_exit(trans, &inode_iter);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
if (value) {
|
|
|
|
struct bkey_i_xattr *xattr;
|
|
|
|
unsigned namelen = strlen(name);
|
|
|
|
unsigned u64s = BKEY_U64s +
|
|
|
|
xattr_val_u64s(namelen, size);
|
|
|
|
|
|
|
|
if (u64s > U8_MAX)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
|
|
|
|
if (IS_ERR(xattr))
|
|
|
|
return PTR_ERR(xattr);
|
|
|
|
|
|
|
|
bkey_xattr_init(&xattr->k_i);
|
|
|
|
xattr->k.u64s = u64s;
|
|
|
|
xattr->v.x_type = type;
|
|
|
|
xattr->v.x_name_len = namelen;
|
|
|
|
xattr->v.x_val_len = cpu_to_le16(size);
|
|
|
|
memcpy(xattr->v.x_name, name, namelen);
|
|
|
|
memcpy(xattr_val(&xattr->v), value, size);
|
|
|
|
|
2019-03-13 17:49:16 -07:00
|
|
|
ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,
|
2017-03-16 23:18:50 -07:00
|
|
|
inum, &xattr->k_i,
|
2024-04-07 15:05:34 -07:00
|
|
|
(flags & XATTR_CREATE ? STR_HASH_must_create : 0)|
|
|
|
|
(flags & XATTR_REPLACE ? STR_HASH_must_replace : 0));
|
2017-03-16 23:18:50 -07:00
|
|
|
} else {
|
|
|
|
struct xattr_search_key search =
|
|
|
|
X_SEARCH(type, name, strlen(name));
|
|
|
|
|
|
|
|
ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,
|
|
|
|
hash_info, inum, &search);
|
|
|
|
}
|
|
|
|
|
2023-05-27 16:59:59 -07:00
|
|
|
if (bch2_err_matches(ret, ENOENT))
|
2017-03-16 23:18:50 -07:00
|
|
|
ret = flags & XATTR_REPLACE ? -ENODATA : 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
struct xattr_buf {
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
|
|
|
size_t used;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __bch2_xattr_emit(const char *prefix,
|
|
|
|
const char *name, size_t name_len,
|
|
|
|
struct xattr_buf *buf)
|
2018-12-17 03:31:09 -07:00
|
|
|
{
|
|
|
|
const size_t prefix_len = strlen(prefix);
|
|
|
|
const size_t total_len = prefix_len + name_len + 1;
|
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
if (buf->buf) {
|
|
|
|
if (buf->used + total_len > buf->len)
|
|
|
|
return -ERANGE;
|
2018-12-17 03:31:09 -07:00
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
memcpy(buf->buf + buf->used, prefix, prefix_len);
|
|
|
|
memcpy(buf->buf + buf->used + prefix_len,
|
2018-12-17 03:31:09 -07:00
|
|
|
name, name_len);
|
2019-04-17 12:49:28 -07:00
|
|
|
buf->buf[buf->used + prefix_len + name_len] = '\0';
|
2018-12-17 03:31:09 -07:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
buf->used += total_len;
|
|
|
|
return 0;
|
2018-12-17 03:31:09 -07:00
|
|
|
}
|
|
|
|
|
2024-08-15 01:57:44 -07:00
|
|
|
static inline const char *bch2_xattr_prefix(unsigned type, struct dentry *dentry)
|
|
|
|
{
|
|
|
|
const struct xattr_handler *handler = bch2_xattr_type_to_handler(type);
|
|
|
|
|
|
|
|
if (!xattr_handler_can_list(handler, dentry))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return xattr_prefix(handler);
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
static int bch2_xattr_emit(struct dentry *dentry,
|
2018-12-17 03:31:09 -07:00
|
|
|
const struct bch_xattr *xattr,
|
2019-04-17 12:49:28 -07:00
|
|
|
struct xattr_buf *buf)
|
2017-03-16 23:18:50 -07:00
|
|
|
{
|
2024-08-15 01:57:44 -07:00
|
|
|
const char *prefix;
|
|
|
|
|
|
|
|
prefix = bch2_xattr_prefix(xattr->x_type, dentry);
|
|
|
|
if (!prefix)
|
|
|
|
return 0;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2024-08-15 01:57:44 -07:00
|
|
|
return __bch2_xattr_emit(prefix, xattr->x_name, xattr->x_name_len, buf);
|
2018-12-17 03:31:09 -07:00
|
|
|
}
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
static int bch2_xattr_list_bcachefs(struct bch_fs *c,
|
2021-03-02 16:35:30 -07:00
|
|
|
struct bch_inode_unpacked *inode,
|
2019-04-17 12:49:28 -07:00
|
|
|
struct xattr_buf *buf,
|
|
|
|
bool all)
|
2018-12-17 03:31:09 -07:00
|
|
|
{
|
|
|
|
const char *prefix = all ? "bcachefs_effective." : "bcachefs.";
|
|
|
|
unsigned id;
|
2019-04-17 12:49:28 -07:00
|
|
|
int ret = 0;
|
2018-12-17 03:31:09 -07:00
|
|
|
u64 v;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
for (id = 0; id < Inode_opt_nr; id++) {
|
2021-03-02 16:35:30 -07:00
|
|
|
v = bch2_inode_opt_get(inode, id);
|
2018-12-17 03:31:09 -07:00
|
|
|
if (!v)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!all &&
|
2021-03-02 16:35:30 -07:00
|
|
|
!(inode->bi_fields_set & (1 << id)))
|
2018-12-17 03:31:09 -07:00
|
|
|
continue;
|
|
|
|
|
2019-04-17 12:49:28 -07:00
|
|
|
ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],
|
|
|
|
strlen(bch2_inode_opts[id]), buf);
|
|
|
|
if (ret)
|
2018-12-17 03:31:09 -07:00
|
|
|
break;
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
2019-04-17 12:49:28 -07:00
|
|
|
|
|
|
|
return ret;
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = dentry->d_sb->s_fs_info;
|
2018-12-17 03:31:09 -07:00
|
|
|
struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
|
2019-04-17 12:49:28 -07:00
|
|
|
struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
|
2021-03-15 21:28:17 -07:00
|
|
|
u64 offset = 0, inum = inode->ei_inode.bi_inum;
|
|
|
|
|
2024-07-17 10:24:28 -07:00
|
|
|
int ret = bch2_trans_run(c,
|
|
|
|
for_each_btree_key_in_subvolume_upto(trans, iter, BTREE_ID_xattrs,
|
|
|
|
POS(inum, offset),
|
|
|
|
POS(inum, U64_MAX),
|
|
|
|
inode->ei_inum.subvol, 0, k, ({
|
|
|
|
if (k.k->type != KEY_TYPE_xattr)
|
|
|
|
continue;
|
2021-03-19 17:29:11 -07:00
|
|
|
|
2024-07-17 10:24:28 -07:00
|
|
|
bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);
|
|
|
|
}))) ?:
|
|
|
|
bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false) ?:
|
|
|
|
bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2024-07-17 10:24:28 -07:00
|
|
|
return ret ? bch2_err_class(ret) : buf.used;
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_xattr_get_handler(const struct xattr_handler *handler,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, void *buffer, size_t size)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2023-07-09 11:12:58 -07:00
|
|
|
int ret = bch2_trans_do(c, NULL, NULL, 0,
|
2023-09-12 14:16:02 -07:00
|
|
|
bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags));
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2024-04-07 13:16:49 -07:00
|
|
|
if (ret < 0 && bch2_err_matches(ret, ENOENT))
|
|
|
|
ret = -ENODATA;
|
|
|
|
|
2022-09-18 12:43:50 -07:00
|
|
|
return bch2_err_class(ret);
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_xattr_set_handler(const struct xattr_handler *handler,
|
|
|
|
struct mnt_idmap *idmap,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, const void *value,
|
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
2021-03-02 16:35:30 -07:00
|
|
|
struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
|
2023-07-09 11:18:28 -07:00
|
|
|
struct bch_inode_unpacked inode_u;
|
2022-09-18 12:43:50 -07:00
|
|
|
int ret;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2023-09-12 14:16:02 -07:00
|
|
|
ret = bch2_trans_run(c,
|
|
|
|
commit_do(trans, NULL, NULL, 0,
|
|
|
|
bch2_xattr_set(trans, inode_inum(inode), &inode_u,
|
2023-07-09 11:18:28 -07:00
|
|
|
&hash, name, value, size,
|
2023-09-12 14:16:02 -07:00
|
|
|
handler->flags, flags)) ?:
|
|
|
|
(bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0));
|
2023-07-09 11:18:28 -07:00
|
|
|
|
2022-09-18 12:43:50 -07:00
|
|
|
return bch2_err_class(ret);
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct xattr_handler bch_xattr_user_handler = {
|
|
|
|
.prefix = XATTR_USER_PREFIX,
|
|
|
|
.get = bch2_xattr_get_handler,
|
|
|
|
.set = bch2_xattr_set_handler,
|
2018-11-01 12:10:01 -07:00
|
|
|
.flags = KEY_TYPE_XATTR_INDEX_USER,
|
2017-03-16 23:18:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool bch2_xattr_trusted_list(struct dentry *dentry)
|
|
|
|
{
|
|
|
|
return capable(CAP_SYS_ADMIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct xattr_handler bch_xattr_trusted_handler = {
|
|
|
|
.prefix = XATTR_TRUSTED_PREFIX,
|
|
|
|
.list = bch2_xattr_trusted_list,
|
|
|
|
.get = bch2_xattr_get_handler,
|
|
|
|
.set = bch2_xattr_set_handler,
|
2018-11-01 12:10:01 -07:00
|
|
|
.flags = KEY_TYPE_XATTR_INDEX_TRUSTED,
|
2017-03-16 23:18:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct xattr_handler bch_xattr_security_handler = {
|
|
|
|
.prefix = XATTR_SECURITY_PREFIX,
|
|
|
|
.get = bch2_xattr_get_handler,
|
|
|
|
.set = bch2_xattr_set_handler,
|
2018-11-01 12:10:01 -07:00
|
|
|
.flags = KEY_TYPE_XATTR_INDEX_SECURITY,
|
2017-03-16 23:18:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef NO_BCACHEFS_FS
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
static int opt_to_inode_opt(int id)
|
|
|
|
{
|
|
|
|
switch (id) {
|
|
|
|
#define x(name, ...) \
|
|
|
|
case Opt_##name: return Inode_opt_##name;
|
|
|
|
BCH_INODE_OPTS()
|
|
|
|
#undef x
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, void *buffer, size_t size,
|
|
|
|
bool all)
|
2017-03-16 23:18:50 -07:00
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
|
|
|
struct bch_opts opts =
|
2022-11-23 18:28:15 -07:00
|
|
|
bch2_inode_opts_to_opts(&inode->ei_inode);
|
2017-03-16 23:18:50 -07:00
|
|
|
const struct bch_option *opt;
|
2018-12-17 03:31:09 -07:00
|
|
|
int id, inode_opt_id;
|
2022-02-25 11:18:19 -07:00
|
|
|
struct printbuf out = PRINTBUF;
|
|
|
|
int ret;
|
2017-03-16 23:18:50 -07:00
|
|
|
u64 v;
|
|
|
|
|
|
|
|
id = bch2_opt_lookup(name);
|
|
|
|
if (id < 0 || !bch2_opt_is_inode_opt(id))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
inode_opt_id = opt_to_inode_opt(id);
|
|
|
|
if (inode_opt_id < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
opt = bch2_opt_table + id;
|
|
|
|
|
|
|
|
if (!bch2_opt_defined_by_id(&opts, id))
|
|
|
|
return -ENODATA;
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
if (!all &&
|
|
|
|
!(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))
|
|
|
|
return -ENODATA;
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
v = bch2_opt_get_by_id(&opts, id);
|
2022-03-05 10:01:16 -07:00
|
|
|
bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2022-02-25 11:18:19 -07:00
|
|
|
ret = out.pos;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2022-02-25 11:18:19 -07:00
|
|
|
if (out.allocation_failure) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
} else if (buffer) {
|
|
|
|
if (out.pos > size)
|
|
|
|
ret = -ERANGE;
|
|
|
|
else
|
|
|
|
memcpy(buffer, out.buf, out.pos);
|
|
|
|
}
|
2018-11-08 23:24:07 -07:00
|
|
|
|
2022-02-25 11:18:19 -07:00
|
|
|
printbuf_exit(&out);
|
|
|
|
return ret;
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, void *buffer, size_t size)
|
|
|
|
{
|
|
|
|
return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
|
|
|
|
name, buffer, size, false);
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
struct inode_opt_set {
|
|
|
|
int id;
|
|
|
|
u64 v;
|
|
|
|
bool defined;
|
|
|
|
};
|
|
|
|
|
2023-08-12 07:47:45 -07:00
|
|
|
static int inode_opt_set_fn(struct btree_trans *trans,
|
|
|
|
struct bch_inode_info *inode,
|
2017-03-16 23:18:50 -07:00
|
|
|
struct bch_inode_unpacked *bi,
|
|
|
|
void *p)
|
|
|
|
{
|
|
|
|
struct inode_opt_set *s = p;
|
|
|
|
|
|
|
|
if (s->defined)
|
2018-12-17 03:31:09 -07:00
|
|
|
bi->bi_fields_set |= 1U << s->id;
|
2017-03-16 23:18:50 -07:00
|
|
|
else
|
2018-12-17 03:31:09 -07:00
|
|
|
bi->bi_fields_set &= ~(1U << s->id);
|
|
|
|
|
|
|
|
bch2_inode_opt_set(bi, s->id, s->v);
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
|
|
|
|
struct mnt_idmap *idmap,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, const void *value,
|
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
struct bch_inode_info *inode = to_bch_ei(vinode);
|
|
|
|
struct bch_fs *c = inode->v.i_sb->s_fs_info;
|
|
|
|
const struct bch_option *opt;
|
|
|
|
char *buf;
|
|
|
|
struct inode_opt_set s;
|
2018-12-17 03:31:09 -07:00
|
|
|
int opt_id, inode_opt_id, ret;
|
|
|
|
|
|
|
|
opt_id = bch2_opt_lookup(name);
|
|
|
|
if (opt_id < 0)
|
|
|
|
return -EINVAL;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
opt = bch2_opt_table + opt_id;
|
|
|
|
|
|
|
|
inode_opt_id = opt_to_inode_opt(opt_id);
|
|
|
|
if (inode_opt_id < 0)
|
2017-03-16 23:18:50 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
s.id = inode_opt_id;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
|
|
|
if (value) {
|
2018-12-17 03:31:09 -07:00
|
|
|
u64 v = 0;
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
buf = kmalloc(size + 1, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
memcpy(buf, value, size);
|
|
|
|
buf[size] = '\0';
|
|
|
|
|
2022-03-20 21:15:38 -07:00
|
|
|
ret = bch2_opt_parse(c, opt, buf, &v, NULL);
|
2017-03-16 23:18:50 -07:00
|
|
|
kfree(buf);
|
|
|
|
|
|
|
|
if (ret < 0)
|
2024-02-29 20:17:45 -07:00
|
|
|
goto err_class_exit;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
ret = bch2_opt_check_may_set(c, opt_id, v);
|
2018-11-12 16:30:55 -07:00
|
|
|
if (ret < 0)
|
2024-02-29 20:17:45 -07:00
|
|
|
goto err_class_exit;
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
s.v = v + 1;
|
2017-03-16 23:18:50 -07:00
|
|
|
s.defined = true;
|
|
|
|
} else {
|
2023-11-14 17:11:04 -07:00
|
|
|
/*
|
|
|
|
* Check if this option was set on the parent - if so, switched
|
|
|
|
* back to inheriting from the parent:
|
|
|
|
*
|
|
|
|
* rename() also has to deal with keeping inherited options up
|
|
|
|
* to date - see bch2_reinherit_attrs()
|
|
|
|
*/
|
|
|
|
spin_lock(&dentry->d_lock);
|
2018-12-17 03:31:09 -07:00
|
|
|
if (!IS_ROOT(dentry)) {
|
|
|
|
struct bch_inode_info *dir =
|
|
|
|
to_bch_ei(d_inode(dentry->d_parent));
|
|
|
|
|
|
|
|
s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);
|
|
|
|
} else {
|
|
|
|
s.v = 0;
|
|
|
|
}
|
2023-11-14 17:11:04 -07:00
|
|
|
spin_unlock(&dentry->d_lock);
|
2018-12-17 03:31:09 -07:00
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
s.defined = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&inode->ei_update_lock);
|
2018-12-19 06:43:01 -07:00
|
|
|
if (inode_opt_id == Inode_opt_project) {
|
2020-08-24 11:57:48 -07:00
|
|
|
/*
|
|
|
|
* inode fields accessible via the xattr interface are stored
|
|
|
|
* with a +1 bias, so that 0 means unset:
|
|
|
|
*/
|
|
|
|
ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);
|
2018-12-19 06:43:01 -07:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2018-07-17 11:12:42 -07:00
|
|
|
ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
|
2018-12-19 06:43:01 -07:00
|
|
|
err:
|
2017-03-16 23:18:50 -07:00
|
|
|
mutex_unlock(&inode->ei_update_lock);
|
|
|
|
|
|
|
|
if (value &&
|
2024-01-16 14:20:21 -07:00
|
|
|
(opt_id == Opt_background_target ||
|
|
|
|
opt_id == Opt_background_compression ||
|
|
|
|
(opt_id == Opt_compression && !inode_opt_get(c, &inode->ei_inode, background_compression))))
|
bcachefs: rebalance_work
This adds a new btree, rebalance_work, to eliminate scanning required
for finding extents that need work done on them in the background - i.e.
for the background_target and background_compression options.
rebalance_work is a bitset btree, where a KEY_TYPE_set corresponds to an
extent in the extents or reflink btree at the same pos.
A new extent field is added, bch_extent_rebalance, which indicates that
this extent has work that needs to be done in the background - and which
options to use. This allows per-inode options to be propagated to
indirect extents - at least in some circumstances. In this patch,
changing IO options on a file will not propagate the new options to
indirect extents pointed to by that file.
Updating (setting/clearing) the rebalance_work btree is done by the
extent trigger, which looks at the bch_extent_rebalance field.
Scanning is still requrired after changing IO path options - either just
for a given inode, or for the whole filesystem. We indicate that
scanning is required by adding a KEY_TYPE_cookie key to the
rebalance_work btree: the cookie counter is so that we can detect that
scanning is still required when an option has been flipped mid-way
through an existing scan.
Future possible work:
- Propagate options to indirect extents when being changed
- Add other IO path options - nr_replicas, ec, to rebalance_work so
they can be applied in the background when they change
- Add a counter, for bcachefs fs usage output, showing the pending
amount of rebalance work: we'll probably want to do this after the
disk space accounting rewrite (moving it to a new btree)
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-10-20 10:33:14 -07:00
|
|
|
bch2_set_rebalance_needs_scan(c, inode->ei_inode.bi_inum);
|
2017-03-16 23:18:50 -07:00
|
|
|
|
2024-02-29 20:17:45 -07:00
|
|
|
err_class_exit:
|
2023-05-27 16:59:59 -07:00
|
|
|
return bch2_err_class(ret);
|
2017-03-16 23:18:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct xattr_handler bch_xattr_bcachefs_handler = {
|
|
|
|
.prefix = "bcachefs.",
|
|
|
|
.get = bch2_xattr_bcachefs_get,
|
|
|
|
.set = bch2_xattr_bcachefs_set,
|
|
|
|
};
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
static int bch2_xattr_bcachefs_get_effective(
|
|
|
|
const struct xattr_handler *handler,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, void *buffer, size_t size)
|
|
|
|
{
|
|
|
|
return __bch2_xattr_bcachefs_get(handler, dentry, vinode,
|
|
|
|
name, buffer, size, true);
|
|
|
|
}
|
|
|
|
|
2024-08-18 10:24:26 -07:00
|
|
|
/* Noop - xattrs in the bcachefs_effective namespace are inherited */
|
|
|
|
static int bch2_xattr_bcachefs_set_effective(const struct xattr_handler *handler,
|
|
|
|
struct mnt_idmap *idmap,
|
|
|
|
struct dentry *dentry, struct inode *vinode,
|
|
|
|
const char *name, const void *value,
|
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-17 03:31:09 -07:00
|
|
|
static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
|
|
|
|
.prefix = "bcachefs_effective.",
|
|
|
|
.get = bch2_xattr_bcachefs_get_effective,
|
2024-08-18 10:24:26 -07:00
|
|
|
.set = bch2_xattr_bcachefs_set_effective,
|
2018-12-17 03:31:09 -07:00
|
|
|
};
|
|
|
|
|
2017-03-16 23:18:50 -07:00
|
|
|
#endif /* NO_BCACHEFS_FS */
|
|
|
|
|
|
|
|
const struct xattr_handler *bch2_xattr_handlers[] = {
|
|
|
|
&bch_xattr_user_handler,
|
|
|
|
&bch_xattr_trusted_handler,
|
|
|
|
&bch_xattr_security_handler,
|
|
|
|
#ifndef NO_BCACHEFS_FS
|
|
|
|
&bch_xattr_bcachefs_handler,
|
2018-12-17 03:31:09 -07:00
|
|
|
&bch_xattr_bcachefs_effective_handler,
|
2017-03-16 23:18:50 -07:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct xattr_handler *bch_xattr_handler_map[] = {
|
2018-11-01 12:10:01 -07:00
|
|
|
[KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler,
|
|
|
|
[KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
|
2017-03-16 23:18:50 -07:00
|
|
|
&nop_posix_acl_access,
|
2018-11-01 12:10:01 -07:00
|
|
|
[KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] =
|
2017-03-16 23:18:50 -07:00
|
|
|
&nop_posix_acl_default,
|
2018-11-01 12:10:01 -07:00
|
|
|
[KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler,
|
|
|
|
[KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler,
|
2017-03-16 23:18:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
|
|
|
|
{
|
|
|
|
return type < ARRAY_SIZE(bch_xattr_handler_map)
|
|
|
|
? bch_xattr_handler_map[type]
|
|
|
|
: NULL;
|
|
|
|
}
|