+// SPDX-License-Identifier: GPL-2.0
#ifdef CONFIG_BCACHEFS_POSIX_ACL
#include "bcachefs.h"
{
switch (type) {
case ACL_TYPE_ACCESS:
- return BCH_XATTR_INDEX_POSIX_ACL_ACCESS;
+ return KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS;
case ACL_TYPE_DEFAULT:
- return BCH_XATTR_INDEX_POSIX_ACL_DEFAULT;
+ return KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT;
default:
BUG();
}
* Convert from in-memory to filesystem representation.
*/
static struct bkey_i_xattr *
-bch2_acl_to_xattr(const struct posix_acl *acl,
+bch2_acl_to_xattr(struct btree_trans *trans,
+ const struct posix_acl *acl,
int type)
{
struct bkey_i_xattr *xattr;
if (u64s > U8_MAX)
return ERR_PTR(-E2BIG);
- xattr = kmalloc(u64s * sizeof(u64), GFP_KERNEL);
+ xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));
if (IS_ERR(xattr))
return xattr;
bkey_xattr_init(&xattr->k_i);
xattr->k.u64s = u64s;
xattr->v.x_type = acl_to_xattr_type(type);
- xattr->v.x_name_len = 0,
+ xattr->v.x_name_len = 0;
xattr->v.x_val_len = cpu_to_le16(acl_len);
acl_header = xattr_val(&xattr->v);
return xattr;
}
-struct posix_acl *bch2_get_acl(struct inode *vinode, int type)
+struct posix_acl *bch2_get_acl(struct inode *vinode, int type, bool rcu)
{
struct bch_inode_info *inode = to_bch_ei(vinode);
struct bch_fs *c = inode->v.i_sb->s_fs_info;
- struct btree_iter iter;
+ struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode);
+ struct btree_trans trans;
+ struct btree_iter iter = { NULL };
struct bkey_s_c_xattr xattr;
- struct bkey_s_c k;
struct posix_acl *acl = NULL;
- int name_index = acl_to_xattr_type(type);
+ struct bkey_s_c k;
+ int ret;
- k = bch2_xattr_get_iter(c, &iter, inode, "", name_index);
- if (IS_ERR(k.k)) {
- if (PTR_ERR(k.k) != -ENOENT)
- acl = ERR_CAST(k.k);
+ if (rcu)
+ return ERR_PTR(-ECHILD);
+
+ bch2_trans_init(&trans, c, 0, 0);
+retry:
+ bch2_trans_begin(&trans);
+
+ ret = bch2_hash_lookup(&trans, &iter, bch2_xattr_hash_desc,
+ &hash, inode_inum(inode),
+ &X_SEARCH(acl_to_xattr_type(type), "", 0),
+ 0);
+ if (ret) {
+ if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
+ goto retry;
+ if (ret != -ENOENT)
+ acl = ERR_PTR(ret);
goto out;
}
- xattr = bkey_s_c_to_xattr(k);
+ k = bch2_btree_iter_peek_slot(&iter);
+ ret = bkey_err(k);
+ if (ret) {
+ acl = ERR_PTR(ret);
+ goto out;
+ }
+ xattr = bkey_s_c_to_xattr(k);
acl = bch2_acl_from_disk(xattr_val(xattr.v),
le16_to_cpu(xattr.v->x_val_len));
if (!IS_ERR(acl))
set_cached_acl(&inode->v, type, acl);
out:
- bch2_btree_iter_unlock(&iter);
+ bch2_trans_iter_exit(&trans, &iter);
+ bch2_trans_exit(&trans);
return acl;
}
-int __bch2_set_acl(struct inode *vinode, struct posix_acl *acl, int type)
+int bch2_set_acl_trans(struct btree_trans *trans, subvol_inum inum,
+ struct bch_inode_unpacked *inode_u,
+ struct posix_acl *acl, int type)
{
- struct bch_inode_info *inode = to_bch_ei(vinode);
- struct bch_fs *c = inode->v.i_sb->s_fs_info;
+ struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode_u);
int ret;
if (type == ACL_TYPE_DEFAULT &&
- !S_ISDIR(inode->v.i_mode))
+ !S_ISDIR(inode_u->bi_mode))
return acl ? -EACCES : 0;
if (acl) {
struct bkey_i_xattr *xattr =
- bch2_acl_to_xattr(acl, type);
+ bch2_acl_to_xattr(trans, acl, type);
if (IS_ERR(xattr))
return PTR_ERR(xattr);
- ret = bch2_hash_set(bch2_xattr_hash_desc, &inode->ei_str_hash,
- c, inode->v.i_ino, &inode->ei_journal_seq,
- &xattr->k_i, 0);
- kfree(xattr);
+ ret = bch2_hash_set(trans, bch2_xattr_hash_desc, &hash_info,
+ inum, &xattr->k_i, 0);
} else {
struct xattr_search_key search =
X_SEARCH(acl_to_xattr_type(type), "", 0);
- ret = bch2_hash_delete(bch2_xattr_hash_desc, &inode->ei_str_hash,
- c, inode->v.i_ino, &inode->ei_journal_seq,
- &search);
+ ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, &hash_info,
+ inum, &search);
}
- if (!ret)
- set_cached_acl(&inode->v, type, acl);
-
- return ret;
+ return ret == -ENOENT ? 0 : ret;
}
-int bch2_set_acl(struct inode *vinode, struct posix_acl *acl, int type)
+int bch2_set_acl(struct user_namespace *mnt_userns,
+ struct inode *vinode, struct posix_acl *_acl, int type)
{
struct bch_inode_info *inode = to_bch_ei(vinode);
struct bch_fs *c = inode->v.i_sb->s_fs_info;
- umode_t mode = inode->v.i_mode;
+ struct btree_trans trans;
+ struct btree_iter inode_iter = { NULL };
+ struct bch_inode_unpacked inode_u;
+ struct posix_acl *acl;
+ umode_t mode;
int ret;
- if (type == ACL_TYPE_ACCESS && acl) {
- ret = posix_acl_update_mode(&inode->v, &mode, &acl);
+ mutex_lock(&inode->ei_update_lock);
+ bch2_trans_init(&trans, c, 0, 0);
+retry:
+ bch2_trans_begin(&trans);
+ acl = _acl;
+
+ ret = bch2_inode_peek(&trans, &inode_iter, &inode_u, inode_inum(inode),
+ BTREE_ITER_INTENT);
+ if (ret)
+ goto btree_err;
+
+ mode = inode_u.bi_mode;
+
+ if (type == ACL_TYPE_ACCESS) {
+ ret = posix_acl_update_mode(mnt_userns, &inode->v, &mode, &acl);
if (ret)
- return ret;
+ goto btree_err;
}
- ret = __bch2_set_acl(vinode, acl, type);
+ ret = bch2_set_acl_trans(&trans, inode_inum(inode), &inode_u, acl, type);
+ if (ret)
+ goto btree_err;
+
+ inode_u.bi_ctime = bch2_current_time(c);
+ inode_u.bi_mode = mode;
+
+ ret = bch2_inode_write(&trans, &inode_iter, &inode_u) ?:
+ bch2_trans_commit(&trans, NULL, NULL, 0);
+btree_err:
+ bch2_trans_iter_exit(&trans, &inode_iter);
+
+ if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
+ goto retry;
+ if (unlikely(ret))
+ goto err;
+
+ bch2_inode_update_after_write(&trans, inode, &inode_u,
+ ATTR_CTIME|ATTR_MODE);
+
+ set_cached_acl(&inode->v, type, acl);
+err:
+ bch2_trans_exit(&trans);
+ mutex_unlock(&inode->ei_update_lock);
+
+ return ret;
+}
+
+int bch2_acl_chmod(struct btree_trans *trans, subvol_inum inum,
+ struct bch_inode_unpacked *inode,
+ umode_t mode,
+ struct posix_acl **new_acl)
+{
+ struct bch_hash_info hash_info = bch2_hash_info_init(trans->c, inode);
+ struct btree_iter iter;
+ struct bkey_s_c_xattr xattr;
+ struct bkey_i_xattr *new;
+ struct posix_acl *acl;
+ struct bkey_s_c k;
+ int ret;
+
+ ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc,
+ &hash_info, inum,
+ &X_SEARCH(KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS, "", 0),
+ BTREE_ITER_INTENT);
+ if (ret)
+ return ret == -ENOENT ? 0 : ret;
+
+ k = bch2_btree_iter_peek_slot(&iter);
+ xattr = bkey_s_c_to_xattr(k);
if (ret)
- return ret;
+ goto err;
- if (mode != inode->v.i_mode) {
- mutex_lock(&inode->ei_update_lock);
- inode->v.i_mode = mode;
- inode->v.i_ctime = current_time(&inode->v);
+ acl = bch2_acl_from_disk(xattr_val(xattr.v),
+ le16_to_cpu(xattr.v->x_val_len));
+ ret = PTR_ERR_OR_ZERO(acl);
+ if (IS_ERR_OR_NULL(acl))
+ goto err;
+
+ ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
+ if (ret)
+ goto err;
- ret = bch2_write_inode(c, inode);
- mutex_unlock(&inode->ei_update_lock);
+ new = bch2_acl_to_xattr(trans, acl, ACL_TYPE_ACCESS);
+ if (IS_ERR(new)) {
+ ret = PTR_ERR(new);
+ goto err;
}
+ new->k.p = iter.pos;
+ ret = bch2_trans_update(trans, &iter, &new->k_i, 0);
+ *new_acl = acl;
+ acl = NULL;
+err:
+ bch2_trans_iter_exit(trans, &iter);
+ if (!IS_ERR_OR_NULL(acl))
+ kfree(acl);
return ret;
}