]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/xattr.c
Update bcachefs sources to 940d6ca657 bcachefs: acl code improvements
[bcachefs-tools-debian] / libbcachefs / xattr.c
index 3a49d72817545161ac34a8cdf193afa8f80b53e2..c6b5015a0087c145227365bad2e0b4667eaf101e 100644 (file)
@@ -2,8 +2,10 @@
 #include "bcachefs.h"
 #include "bkey_methods.h"
 #include "btree_update.h"
+#include "compress.h"
 #include "extents.h"
 #include "fs.h"
+#include "rebalance.h"
 #include "str_hash.h"
 #include "xattr.h"
 
 #include <linux/posix_acl_xattr.h>
 #include <linux/xattr.h>
 
-static unsigned xattr_val_u64s(unsigned name_len, unsigned val_len)
-{
-       return DIV_ROUND_UP(sizeof(struct bch_xattr) +
-                           name_len + val_len, sizeof(u64));
-}
-
-#define xattr_val(_xattr)      ((_xattr)->x_name + (_xattr)->x_name_len)
-
 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);
 
-struct xattr_search_key {
-       u8              type;
-       struct qstr     name;
-};
-
-#define X_SEARCH(_type, _name, _len) ((struct xattr_search_key)        \
-       { .type = _type, .name = QSTR_INIT(_name, _len) })
-
 static u64 bch2_xattr_hash(const struct bch_hash_info *info,
                          const struct xattr_search_key *key)
 {
@@ -84,8 +70,7 @@ const struct bch_hash_desc bch2_xattr_hash_desc = {
        .cmp_bkey       = xattr_cmp_bkey,
 };
 
-static const char *bch2_xattr_invalid(const struct bch_fs *c,
-                                    struct bkey_s_c k)
+const char *bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k)
 {
        const struct xattr_handler *handler;
        struct bkey_s_c_xattr xattr;
@@ -124,8 +109,8 @@ static const char *bch2_xattr_invalid(const struct bch_fs *c,
        }
 }
 
-static void bch2_xattr_to_text(struct bch_fs *c, char *buf,
-                             size_t size, struct bkey_s_c k)
+void bch2_xattr_to_text(struct bch_fs *c, char *buf,
+                       size_t size, struct bkey_s_c k)
 {
        const struct xattr_handler *handler;
        struct bkey_s_c_xattr xattr;
@@ -157,10 +142,16 @@ static void bch2_xattr_to_text(struct bch_fs *c, char *buf,
        }
 }
 
-const struct bkey_ops bch2_bkey_xattr_ops = {
-       .key_invalid    = bch2_xattr_invalid,
-       .val_to_text    = bch2_xattr_to_text,
-};
+struct bkey_s_c bch2_xattr_get_iter(struct bch_fs *c,
+                                   struct btree_iter *iter,
+                                   struct bch_inode_info *inode,
+                                   const char *name, int type)
+{
+       return bch2_hash_lookup(bch2_xattr_hash_desc,
+                               &inode->ei_str_hash,
+                               c, inode->v.i_ino, iter,
+                               &X_SEARCH(type, name, strlen(name)));
+}
 
 int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
                  const char *name, void *buffer, size_t size, int type)
@@ -189,19 +180,15 @@ int bch2_xattr_get(struct bch_fs *c, struct bch_inode_info *inode,
        return ret;
 }
 
-int __bch2_xattr_set(struct bch_fs *c, u64 inum,
-                   const struct bch_hash_info *hash_info,
-                   const char *name, const void *value, size_t size,
-                   int flags, int type, u64 *journal_seq)
+int bch2_xattr_set(struct bch_fs *c, u64 inum,
+                  const struct bch_hash_info *hash_info,
+                  const char *name, const void *value, size_t size,
+                  int flags, int type, u64 *journal_seq)
 {
        struct xattr_search_key search = X_SEARCH(type, name, strlen(name));
        int ret;
 
-       if (!value) {
-               ret = bch2_hash_delete(bch2_xattr_hash_desc, hash_info,
-                                     c, inum,
-                                     journal_seq, &search);
-       } else {
+       if (value) {
                struct bkey_i_xattr *xattr;
                unsigned u64s = BKEY_U64s +
                        xattr_val_u64s(search.name.len, size);
@@ -227,6 +214,9 @@ int __bch2_xattr_set(struct bch_fs *c, u64 inum,
                                (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)|
                                (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0));
                kfree(xattr);
+       } else {
+               ret = bch2_hash_delete(bch2_xattr_hash_desc, hash_info,
+                                      c, inum, journal_seq, &search);
        }
 
        if (ret == -ENOENT)
@@ -235,15 +225,6 @@ int __bch2_xattr_set(struct bch_fs *c, u64 inum,
        return ret;
 }
 
-int bch2_xattr_set(struct bch_fs *c, struct bch_inode_info *inode,
-                  const char *name, const void *value, size_t size,
-                  int flags, int type)
-{
-       return __bch2_xattr_set(c, inode->v.i_ino, &inode->ei_str_hash,
-                               name, value, size, flags, type,
-                               &inode->ei_journal_seq);
-}
-
 static size_t bch2_xattr_emit(struct dentry *dentry,
                             const struct bch_xattr *xattr,
                             char *buffer, size_t buffer_size)
@@ -327,8 +308,9 @@ 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;
 
-       return bch2_xattr_set(c, inode, name, value, size, flags,
-                             handler->flags);
+       return bch2_xattr_set(c, inode->v.i_ino, &inode->ei_str_hash,
+                             name, value, size, flags, handler->flags,
+                             &inode->ei_journal_seq);
 }
 
 static const struct xattr_handler bch_xattr_user_handler = {
@@ -358,25 +340,140 @@ static const struct xattr_handler bch_xattr_security_handler = {
        .flags  = BCH_XATTR_INDEX_SECURITY,
 };
 
-static const struct xattr_handler *bch_xattr_handler_map[] = {
-       [BCH_XATTR_INDEX_USER]                  = &bch_xattr_user_handler,
-       [BCH_XATTR_INDEX_POSIX_ACL_ACCESS]      =
-               &posix_acl_access_xattr_handler,
-       [BCH_XATTR_INDEX_POSIX_ACL_DEFAULT]     =
-               &posix_acl_default_xattr_handler,
-       [BCH_XATTR_INDEX_TRUSTED]               = &bch_xattr_trusted_handler,
-       [BCH_XATTR_INDEX_SECURITY]              = &bch_xattr_security_handler,
+#ifndef NO_BCACHEFS_FS
+
+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)
+{
+       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));
+       const struct bch_option *opt;
+       int ret, id;
+       u64 v;
+
+       id = bch2_opt_lookup(name);
+       if (id < 0 || !bch2_opt_is_inode_opt(id))
+               return -EINVAL;
+
+       opt = bch2_opt_table + id;
+
+       if (!bch2_opt_defined_by_id(&opts, id))
+               return -ENODATA;
+
+       v = bch2_opt_get_by_id(&opts, id);
+
+       ret = bch2_opt_to_text(c, buffer, size, opt, v, 0);
+
+       return ret < size || !buffer ? ret : -ERANGE;
+}
+
+struct inode_opt_set {
+       int                     id;
+       u64                     v;
+       bool                    defined;
 };
 
+static int inode_opt_set_fn(struct bch_inode_info *inode,
+                           struct bch_inode_unpacked *bi,
+                           void *p)
+{
+       struct inode_opt_set *s = p;
+
+       if (s->defined)
+               bch2_inode_opt_set(bi, s->id, s->v);
+       else
+               bch2_inode_opt_clear(bi, s->id);
+       return 0;
+}
+
+static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
+                                  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;
+       int ret;
+
+       s.id = bch2_opt_lookup(name);
+       if (s.id < 0 || !bch2_opt_is_inode_opt(s.id))
+               return -EINVAL;
+
+       opt = bch2_opt_table + s.id;
+
+       if (value) {
+               buf = kmalloc(size + 1, GFP_KERNEL);
+               if (!buf)
+                       return -ENOMEM;
+               memcpy(buf, value, size);
+               buf[size] = '\0';
+
+               ret = bch2_opt_parse(c, opt, buf, &s.v);
+               kfree(buf);
+
+               if (ret < 0)
+                       return ret;
+
+               if (s.id == Opt_compression ||
+                   s.id == Opt_background_compression) {
+                       ret = bch2_check_set_has_compressed_data(c, s.v);
+                       if (ret)
+                               return ret;
+               }
+
+               s.defined = true;
+       } else {
+               s.defined = false;
+       }
+
+       mutex_lock(&inode->ei_update_lock);
+       ret = __bch2_write_inode(c, inode, inode_opt_set_fn, &s);
+       mutex_unlock(&inode->ei_update_lock);
+
+       if (value &&
+           (s.id == Opt_background_compression ||
+            s.id == Opt_background_target))
+               bch2_rebalance_add_work(c, inode->v.i_blocks);
+
+       return ret;
+}
+
+static const struct xattr_handler bch_xattr_bcachefs_handler = {
+       .prefix = "bcachefs.",
+       .get    = bch2_xattr_bcachefs_get,
+       .set    = bch2_xattr_bcachefs_set,
+};
+
+#endif /* NO_BCACHEFS_FS */
+
 const struct xattr_handler *bch2_xattr_handlers[] = {
        &bch_xattr_user_handler,
        &posix_acl_access_xattr_handler,
        &posix_acl_default_xattr_handler,
        &bch_xattr_trusted_handler,
        &bch_xattr_security_handler,
+#ifndef NO_BCACHEFS_FS
+       &bch_xattr_bcachefs_handler,
+#endif
        NULL
 };
 
+static const struct xattr_handler *bch_xattr_handler_map[] = {
+       [BCH_XATTR_INDEX_USER]                  = &bch_xattr_user_handler,
+       [BCH_XATTR_INDEX_POSIX_ACL_ACCESS]      =
+               &posix_acl_access_xattr_handler,
+       [BCH_XATTR_INDEX_POSIX_ACL_DEFAULT]     =
+               &posix_acl_default_xattr_handler,
+       [BCH_XATTR_INDEX_TRUSTED]               = &bch_xattr_trusted_handler,
+       [BCH_XATTR_INDEX_SECURITY]              = &bch_xattr_security_handler,
+};
+
 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)
 {
        return type < ARRAY_SIZE(bch_xattr_handler_map)