]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/xattr.c
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / libbcachefs / xattr.c
index 48e625ab15ff4feb0fae21d1a8b29d2329c112fd..5a1858fb9879afd1c70c3d5a64883315090d6dbe 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include "bcachefs.h"
+#include "acl.h"
 #include "bkey_methods.h"
 #include "btree_update.h"
 #include "extents.h"
@@ -69,32 +70,38 @@ const struct bch_hash_desc bch2_xattr_hash_desc = {
        .cmp_bkey       = xattr_cmp_bkey,
 };
 
-const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
+int bch2_xattr_invalid(struct bch_fs *c, struct bkey_s_c k,
+                      enum bkey_invalid_flags flags,
+                      struct printbuf *err)
 {
-       const struct xattr_handler *handler;
        struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);
+       unsigned val_u64s = xattr_val_u64s(xattr.v->x_name_len,
+                                          le16_to_cpu(xattr.v->x_val_len));
+       int ret = 0;
 
-       if (bkey_val_bytes(k.k) < sizeof(struct bch_xattr))
-               return "value too small";
-
-       if (bkey_val_u64s(k.k) <
-           xattr_val_u64s(xattr.v->x_name_len,
-                          le16_to_cpu(xattr.v->x_val_len)))
-               return "value too small";
+       bkey_fsck_err_on(bkey_val_u64s(k.k) < val_u64s, c, err,
+                        xattr_val_size_too_small,
+                        "value too small (%zu < %u)",
+                        bkey_val_u64s(k.k), val_u64s);
 
-       if (bkey_val_u64s(k.k) >
-           xattr_val_u64s(xattr.v->x_name_len,
-                          le16_to_cpu(xattr.v->x_val_len) + 4))
-               return "value too big";
+       /* XXX why +4 ? */
+       val_u64s = xattr_val_u64s(xattr.v->x_name_len,
+                                 le16_to_cpu(xattr.v->x_val_len) + 4);
 
-       handler = bch2_xattr_type_to_handler(xattr.v->x_type);
-       if (!handler)
-               return "invalid type";
+       bkey_fsck_err_on(bkey_val_u64s(k.k) > val_u64s, c, err,
+                        xattr_val_size_too_big,
+                        "value too big (%zu > %u)",
+                        bkey_val_u64s(k.k), val_u64s);
 
-       if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len))
-               return "xattr name has invalid characters";
+       bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr.v->x_type), c, err,
+                        xattr_invalid_type,
+                        "invalid type (%u)", xattr.v->x_type);
 
-       return NULL;
+       bkey_fsck_err_on(memchr(xattr.v->x_name, '\0', xattr.v->x_name_len), c, err,
+                        xattr_name_invalid_chars,
+                        "xattr name has invalid characters");
+fsck_err:
+       return ret;
 }
 
 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
@@ -105,32 +112,38 @@ void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,
 
        handler = bch2_xattr_type_to_handler(xattr.v->x_type);
        if (handler && handler->prefix)
-               pr_buf(out, "%s", handler->prefix);
+               prt_printf(out, "%s", handler->prefix);
        else if (handler)
-               pr_buf(out, "(type %u)", xattr.v->x_type);
+               prt_printf(out, "(type %u)", xattr.v->x_type);
        else
-               pr_buf(out, "(unknown type %u)", xattr.v->x_type);
+               prt_printf(out, "(unknown type %u)", xattr.v->x_type);
 
-       pr_buf(out, "%.*s:%.*s",
+       prt_printf(out, "%.*s:%.*s",
               xattr.v->x_name_len,
               xattr.v->x_name,
               le16_to_cpu(xattr.v->x_val_len),
               (char *) xattr_val(xattr.v));
+
+       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));
+       }
 }
 
 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)
 {
        struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode);
+       struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
        struct btree_iter iter;
        struct bkey_s_c_xattr xattr;
        struct bkey_s_c k;
        int ret;
 
        ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash,
-                              inode_inum(inode),
-                              &X_SEARCH(type, name, strlen(name)),
-                              0);
+                              inode_inum(inode), &search, 0);
        if (ret)
                goto err1;
 
@@ -150,34 +163,27 @@ static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info
 err2:
        bch2_trans_iter_exit(trans, &iter);
 err1:
-       return ret == -ENOENT ? -ENODATA : ret;
-}
-
-int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
-                  const char *name, void *buffer, size_t size, int type)
-{
-       return bch2_trans_do(c, NULL, NULL, 0,
-               bch2_xattr_get_trans(&trans, inode, name, buffer, size, type));
+       return ret < 0 && bch2_err_matches(ret, ENOENT) ? -ENODATA : ret;
 }
 
 int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
+                  struct bch_inode_unpacked *inode_u,
                   const struct bch_hash_info *hash_info,
                   const char *name, const void *value, size_t size,
                   int type, int flags)
 {
+       struct bch_fs *c = trans->c;
        struct btree_iter inode_iter = { NULL };
-       struct bch_inode_unpacked inode_u;
        int ret;
 
-       /*
-        * We need to do an inode update so that bi_journal_sync gets updated
-        * and fsync works:
-        *
-        * Perhaps we should be updating bi_mtime too?
-        */
+       ret   = bch2_subvol_is_ro_trans(trans, inum.subvol) ?:
+               bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT);
+       if (ret)
+               return ret;
+
+       inode_u->bi_ctime = bch2_current_time(c);
 
-       ret   = bch2_inode_peek(trans, &inode_iter, &inode_u, inum, BTREE_ITER_INTENT) ?:
-               bch2_inode_write(trans, &inode_iter, &inode_u);
+       ret = bch2_inode_write(trans, &inode_iter, inode_u);
        bch2_trans_iter_exit(trans, &inode_iter);
 
        if (ret)
@@ -216,7 +222,7 @@ int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum,
                                       hash_info, inum, &search);
        }
 
-       if (ret == -ENOENT)
+       if (bch2_err_matches(ret, ENOENT))
                ret = flags & XATTR_REPLACE ? -ENODATA : 0;
 
        return ret;
@@ -294,30 +300,24 @@ ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 {
        struct bch_fs *c = dentry->d_sb->s_fs_info;
        struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
-       struct btree_trans trans;
+       struct btree_trans *trans = bch2_trans_get(c);
        struct btree_iter iter;
        struct bkey_s_c k;
        struct xattr_buf buf = { .buf = buffer, .len = buffer_size };
        u64 offset = 0, inum = inode->ei_inode.bi_inum;
        u32 snapshot;
        int ret;
-
-       bch2_trans_init(&trans, c, 0, 0);
 retry:
-       bch2_trans_begin(&trans);
+       bch2_trans_begin(trans);
        iter = (struct btree_iter) { NULL };
 
-       ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot);
+       ret = bch2_subvolume_get_snapshot(trans, inode->ei_subvol, &snapshot);
        if (ret)
                goto err;
 
-       for_each_btree_key_norestart(&trans, iter, BTREE_ID_xattrs,
-                          SPOS(inum, offset, snapshot), 0, k, ret) {
-               BUG_ON(k.k->p.inode < inum);
-
-               if (k.k->p.inode > inum)
-                       break;
-
+       for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_xattrs,
+                          SPOS(inum, offset, snapshot),
+                          POS(inum, U64_MAX), 0, k, ret) {
                if (k.k->type != KEY_TYPE_xattr)
                        continue;
 
@@ -327,25 +327,27 @@ retry:
        }
 
        offset = iter.pos.offset;
-       bch2_trans_iter_exit(&trans, &iter);
+       bch2_trans_iter_exit(trans, &iter);
 err:
-       if (ret == -EINTR)
+       if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
                goto retry;
 
-       bch2_trans_exit(&trans);
+       bch2_trans_put(trans);
 
        if (ret)
-               return ret;
+               goto out;
 
        ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false);
        if (ret)
-               return ret;
+               goto out;
 
        ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);
        if (ret)
-               return ret;
+               goto out;
 
        return buf.used;
+out:
+       return bch2_err_class(ret);
 }
 
 static int bch2_xattr_get_handler(const struct xattr_handler *handler,
@@ -354,12 +356,14 @@ static int bch2_xattr_get_handler(const struct xattr_handler *handler,
 {
        struct bch_inode_info *inode = to_bch_ei(vinode);
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
+       int ret = bch2_trans_do(c, NULL, NULL, 0,
+               bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags));
 
-       return bch2_xattr_get(c, inode, name, buffer, size, handler->flags);
+       return bch2_err_class(ret);
 }
 
 static int bch2_xattr_set_handler(const struct xattr_handler *handler,
-                                 struct user_namespace *mnt_userns,
+                                 struct mnt_idmap *idmap,
                                  struct dentry *dentry, struct inode *vinode,
                                  const char *name, const void *value,
                                  size_t size, int flags)
@@ -367,11 +371,17 @@ static int bch2_xattr_set_handler(const struct xattr_handler *handler,
        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 = bch2_hash_info_init(c, &inode->ei_inode);
+       struct bch_inode_unpacked inode_u;
+       int ret;
+
+       ret = bch2_trans_run(c,
+               commit_do(trans, NULL, NULL, 0,
+                       bch2_xattr_set(trans, inode_inum(inode), &inode_u,
+                                      &hash, name, value, size,
+                                      handler->flags, flags)) ?:
+               (bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0));
 
-       return bch2_trans_do(c, NULL, NULL, 0,
-                       bch2_xattr_set(&trans, inode_inum(inode), &hash,
-                                      name, value, size,
-                                      handler->flags, flags));
+       return bch2_err_class(ret);
 }
 
 static const struct xattr_handler bch_xattr_user_handler = {
@@ -423,7 +433,7 @@ static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
        struct bch_inode_info *inode = to_bch_ei(vinode);
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
        struct bch_opts opts =
-               bch2_inode_opts_to_opts(bch2_inode_opts_get(&inode->ei_inode));
+               bch2_inode_opts_to_opts(&inode->ei_inode);
        const struct bch_option *opt;
        int id, inode_opt_id;
        struct printbuf out = PRINTBUF;
@@ -448,7 +458,7 @@ static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler,
                return -ENODATA;
 
        v = bch2_opt_get_by_id(&opts, id);
-       bch2_opt_to_text(&out, c, opt, v, 0);
+       bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);
 
        ret = out.pos;
 
@@ -479,7 +489,8 @@ struct inode_opt_set {
        bool                    defined;
 };
 
-static int inode_opt_set_fn(struct bch_inode_info *inode,
+static int inode_opt_set_fn(struct btree_trans *trans,
+                           struct bch_inode_info *inode,
                            struct bch_inode_unpacked *bi,
                            void *p)
 {
@@ -496,7 +507,7 @@ static int inode_opt_set_fn(struct bch_inode_info *inode,
 }
 
 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
-                                  struct user_namespace *mnt_userns,
+                                  struct mnt_idmap *idmap,
                                   struct dentry *dentry, struct inode *vinode,
                                   const char *name, const void *value,
                                   size_t size, int flags)
@@ -529,7 +540,7 @@ static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
                memcpy(buf, value, size);
                buf[size] = '\0';
 
-               ret = bch2_opt_parse(c, NULL, opt, buf, &v);
+               ret = bch2_opt_parse(c, opt, buf, &v, NULL);
                kfree(buf);
 
                if (ret < 0)
@@ -542,6 +553,14 @@ static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
                s.v = v + 1;
                s.defined = true;
        } else {
+               /*
+                * 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);
                if (!IS_ROOT(dentry)) {
                        struct bch_inode_info *dir =
                                to_bch_ei(d_inode(dentry->d_parent));
@@ -550,6 +569,7 @@ static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
                } else {
                        s.v = 0;
                }
+               spin_unlock(&dentry->d_lock);
 
                s.defined = false;
        }
@@ -572,9 +592,9 @@ err:
        if (value &&
            (opt_id == Opt_background_compression ||
             opt_id == Opt_background_target))
-               bch2_rebalance_add_work(c, inode->v.i_blocks);
+               bch2_set_rebalance_needs_scan(c, inode->ei_inode.bi_inum);
 
-       return ret;
+       return bch2_err_class(ret);
 }
 
 static const struct xattr_handler bch_xattr_bcachefs_handler = {
@@ -603,8 +623,8 @@ static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
 const struct xattr_handler *bch2_xattr_handlers[] = {
        &bch_xattr_user_handler,
 #ifdef CONFIG_BCACHEFS_POSIX_ACL
-       &posix_acl_access_xattr_handler,
-       &posix_acl_default_xattr_handler,
+       &nop_posix_acl_access,
+       &nop_posix_acl_default,
 #endif
        &bch_xattr_trusted_handler,
        &bch_xattr_security_handler,
@@ -618,9 +638,9 @@ const struct xattr_handler *bch2_xattr_handlers[] = {
 static const struct xattr_handler *bch_xattr_handler_map[] = {
        [KEY_TYPE_XATTR_INDEX_USER]                     = &bch_xattr_user_handler,
        [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] =
-               &posix_acl_access_xattr_handler,
+               &nop_posix_acl_access,
        [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT]        =
-               &posix_acl_default_xattr_handler,
+               &nop_posix_acl_default,
        [KEY_TYPE_XATTR_INDEX_TRUSTED]          = &bch_xattr_trusted_handler,
        [KEY_TYPE_XATTR_INDEX_SECURITY]         = &bch_xattr_security_handler,
 };