Btrfs: Drop the hardware crc32c asm code
[safe/jmp/linux-2.6] / fs / btrfs / xattr.c
index f4ac5e0..7f332e2 100644 (file)
 #include "xattr.h"
 #include "disk-io.h"
 
-static struct xattr_handler *btrfs_xattr_handler_map[] = {
-       [BTRFS_XATTR_INDEX_USER]                = &btrfs_xattr_user_handler,
-       [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS]    = &btrfs_xattr_acl_access_handler,
-       [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT]   = &btrfs_xattr_acl_default_handler,
-       [BTRFS_XATTR_INDEX_TRUSTED]             = &btrfs_xattr_trusted_handler,
-       [BTRFS_XATTR_INDEX_SECURITY]            = &btrfs_xattr_security_handler,
-       [BTRFS_XATTR_INDEX_SYSTEM]              = &btrfs_xattr_system_handler,
-};
-
-struct xattr_handler *btrfs_xattr_handlers[] = {
-       &btrfs_xattr_user_handler,
-       &btrfs_xattr_acl_access_handler,
-       &btrfs_xattr_acl_default_handler,
-       &btrfs_xattr_trusted_handler,
-       &btrfs_xattr_security_handler,
-       &btrfs_xattr_system_handler,
-       NULL,
-};
-
-/*
- * @param name - the xattr name
- * @return - the xattr_handler for the xattr, NULL if its not found
- *
- * use this with listxattr where we don't already know the type of xattr we
- * have
- */
-static struct xattr_handler *find_btrfs_xattr_handler(struct extent_buffer *l,
-                                                     unsigned long name_ptr,
-                                                     u16 name_len)
-{
-       struct xattr_handler *handler = NULL;
-       int i = 0;
-
-       for (handler = btrfs_xattr_handlers[i]; handler != NULL; i++,
-            handler = btrfs_xattr_handlers[i]) {
-               u16 prefix_len = strlen(handler->prefix);
-
-               if (name_len < prefix_len)
-                       continue;
-
-               if (memcmp_extent_buffer(l, handler->prefix, name_ptr,
-                                        prefix_len) == 0)
-                       break;
-       }
-
-       return handler;
-}
-
-/*
- * @param name_index - the index for the xattr handler
- * @return the xattr_handler if we found it, NULL otherwise
- *
- * use this if we know the type of the xattr already
- */
-static struct xattr_handler *btrfs_xattr_handler(int name_index)
-{
-       struct xattr_handler *handler = NULL;
-
-       if (name_index >= 0 &&
-           name_index < ARRAY_SIZE(btrfs_xattr_handler_map))
-               handler = btrfs_xattr_handler_map[name_index];
-
-       return handler;
-}
-
-static inline char *get_name(const char *name, int name_index)
-{
-       char *ret = NULL;
-       struct xattr_handler *handler = btrfs_xattr_handler(name_index);
-       int prefix_len;
-
-       if (!handler)
-               return ret;
-
-       prefix_len = strlen(handler->prefix);
-
-       ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL);
-       if (!ret)
-               return ret;
 
-       memcpy(ret, handler->prefix, prefix_len);
-       memcpy(ret+prefix_len, name, strlen(name));
-       ret[prefix_len + strlen(name)] = '\0';
-
-       return ret;
-}
-
-size_t btrfs_xattr_generic_list(struct inode *inode, char *list,
-                               size_t list_size, const char *name,
-                               size_t name_len)
-{
-       if (list && (name_len+1) <= list_size) {
-               memcpy(list, name, name_len);
-               list[name_len] = '\0';
-       } else
-               return -ERANGE;
-
-       return name_len+1;
-}
-
-ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
-                       const char *attr_name, void *buffer, size_t size)
+ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
+                               void *buffer, size_t size)
 {
        struct btrfs_dir_item *di;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_path *path;
        struct extent_buffer *leaf;
-       struct xattr_handler *handler = btrfs_xattr_handler(name_index);
        int ret = 0;
        unsigned long data_ptr;
-       char *name;
-
-       if (!handler)
-               return -EOPNOTSUPP;
-
-       /* just in case... */
-       if (*attr_name == '\0')
-               return -EINVAL;
-
-       name = get_name(attr_name, name_index);
-       if (!name)
-               return -ENOMEM;
 
        path = btrfs_alloc_path();
-       if (!path) {
-               kfree(name);
+       if (!path)
                return -ENOMEM;
-       }
 
-       mutex_lock(&root->fs_info->fs_mutex);
        /* lookup the xattr by name */
        di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
                                strlen(name), 0);
@@ -180,46 +65,27 @@ ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
        data_ptr = (unsigned long)((char *)(di + 1) +
                                   btrfs_dir_name_len(leaf, di));
        read_extent_buffer(leaf, buffer, data_ptr,
-                          btrfs_dir_name_len(leaf, di));
+                          btrfs_dir_data_len(leaf, di));
        ret = btrfs_dir_data_len(leaf, di);
 
 out:
-       mutex_unlock(&root->fs_info->fs_mutex);
-       kfree(name);
        btrfs_free_path(path);
        return ret;
 }
 
-int btrfs_xattr_set(struct inode *inode, int name_index,
-                   const char *attr_name, const void *value, size_t size,
-                   int flags)
+int __btrfs_setxattr(struct inode *inode, const char *name,
+                           const void *value, size_t size, int flags)
 {
        struct btrfs_dir_item *di;
        struct btrfs_root *root = BTRFS_I(inode)->root;
        struct btrfs_trans_handle *trans;
        struct btrfs_path *path;
-       struct xattr_handler *handler = btrfs_xattr_handler(name_index);
-       char *name;
        int ret = 0, mod = 0;
 
-       if (!handler)
-               return -EOPNOTSUPP;
-
-       /* just in case... */
-       if (*attr_name == '\0')
-               return -EINVAL;
-
-       name = get_name(attr_name, name_index);
-       if (!name)
-               return -ENOMEM;
-
        path = btrfs_alloc_path();
-       if (!path) {
-               kfree(name);
+       if (!path)
                return -ENOMEM;
-       }
 
-       mutex_lock(&root->fs_info->fs_mutex);
        trans = btrfs_start_transaction(root, 1);
        btrfs_set_trans_block_group(trans, inode);
 
@@ -249,10 +115,14 @@ int btrfs_xattr_set(struct inode *inode, int name_index,
                        mod = 1;
                        goto out;
                }
-       } else if (flags & XATTR_REPLACE) {
-               /* we couldn't find the attr to replace, so error out */
-               ret = -ENODATA;
-               goto out;
+       } else {
+               btrfs_release_path(root, path);
+
+               if (flags & XATTR_REPLACE) {
+                       /* we couldn't find the attr to replace */
+                       ret = -ENODATA;
+                       goto out;
+               }
        }
 
        /* ok we have to create a completely new xattr */
@@ -269,10 +139,7 @@ out:
        }
 
        btrfs_end_transaction(trans, root);
-       mutex_unlock(&root->fs_info->fs_mutex);
-       kfree(name);
        btrfs_free_path(path);
-
        return ret;
 }
 
@@ -285,11 +152,10 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
        struct btrfs_item *item;
        struct extent_buffer *leaf;
        struct btrfs_dir_item *di;
-       struct xattr_handler *handler;
        int ret = 0, slot, advance;
-       size_t total_size = 0, size_left = size, written;
+       size_t total_size = 0, size_left = size;
        unsigned long name_ptr;
-       char *name;
+       size_t name_len;
        u32 nritems;
 
        /*
@@ -302,11 +168,9 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
        key.offset = 0;
 
        path = btrfs_alloc_path();
-       path->reada = 2;
        if (!path)
                return -ENOMEM;
-
-       mutex_lock(&root->fs_info->fs_mutex);
+       path->reada = 2;
 
        /* search for our xattrs */
        ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
@@ -353,145 +217,106 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
 
                di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
 
-               total_size += btrfs_dir_name_len(leaf, di)+1;
+               name_len = btrfs_dir_name_len(leaf, di);
+               total_size += name_len + 1;
 
                /* we are just looking for how big our buffer needs to be */
                if (!size)
                        continue;
 
-               /* find our handler for this xattr */
-               name_ptr = (unsigned long)(di + 1);
-               handler = find_btrfs_xattr_handler(leaf, name_ptr,
-                                                  btrfs_dir_name_len(leaf, di));
-               if (!handler) {
-                       printk(KERN_ERR "btrfs: unsupported xattr found\n");
-                       continue;
-               }
-
-               name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL);
-               read_extent_buffer(leaf, name, name_ptr,
-                                  btrfs_dir_name_len(leaf, di));
-
-               /* call the list function associated with this xattr */
-               written = handler->list(inode, buffer, size_left, name,
-                                       btrfs_dir_name_len(leaf, di));
-               kfree(name);
-
-               if (written < 0) {
+               if (!buffer || (name_len + 1) > size_left) {
                        ret = -ERANGE;
-                       break;
+                       goto err;
                }
 
-               size_left -= written;
-               buffer += written;
+               name_ptr = (unsigned long)(di + 1);
+               read_extent_buffer(leaf, buffer, name_ptr, name_len);
+               buffer[name_len] = '\0';
+
+               size_left -= name_len + 1;
+               buffer += name_len + 1;
        }
        ret = total_size;
 
 err:
-       mutex_unlock(&root->fs_info->fs_mutex);
        btrfs_free_path(path);
 
        return ret;
 }
 
 /*
- * delete all the xattrs associated with the inode.  fs_mutex should be
- * held when we come into here
+ * List of handlers for synthetic system.* attributes.  All real ondisk
+ * attributes are handled directly.
  */
-int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
-                       struct btrfs_root *root, struct inode *inode)
-{
-       struct btrfs_path *path;
-       struct btrfs_key key, found_key;
-       struct btrfs_item *item;
-       struct extent_buffer *leaf;
-       int ret;
-
-       path = btrfs_alloc_path();
-       if (!path)
-               return -ENOMEM;
-
-       key.objectid = inode->i_ino;
-       btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
-       key.offset = (u64)-1;
+struct xattr_handler *btrfs_xattr_handlers[] = {
+#ifdef CONFIG_FS_POSIX_ACL
+       &btrfs_xattr_acl_access_handler,
+       &btrfs_xattr_acl_default_handler,
+#endif
+       NULL,
+};
 
-       while(1) {
-               /* look for our next xattr */
-               ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
-               if (ret < 0)
-                       goto out;
-               BUG_ON(ret == 0);
+/*
+ * Check if the attribute is in a supported namespace.
+ *
+ * This applied after the check for the synthetic attributes in the system
+ * namespace.
+ */
+static bool btrfs_is_valid_xattr(const char *name)
+{
+       return !strncmp(name, XATTR_SECURITY_PREFIX,
+                       XATTR_SECURITY_PREFIX_LEN) ||
+              !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
+              !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
+              !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
+}
 
-               if (path->slots[0] == 0)
-                       break;
+ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
+                      void *buffer, size_t size)
+{
+       /*
+        * If this is a request for a synthetic attribute in the system.*
+        * namespace use the generic infrastructure to resolve a handler
+        * for it via sb->s_xattr.
+        */
+       if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+               return generic_getxattr(dentry, name, buffer, size);
 
-               path->slots[0]--;
-               leaf = path->nodes[0];
-               item = btrfs_item_nr(leaf, path->slots[0]);
-               btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+       if (!btrfs_is_valid_xattr(name))
+               return -EOPNOTSUPP;
+       return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
+}
 
-               if (found_key.objectid != key.objectid)
-                       break;
-               if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
-                       break;
+int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
+                  size_t size, int flags)
+{
+       /*
+        * If this is a request for a synthetic attribute in the system.*
+        * namespace use the generic infrastructure to resolve a handler
+        * for it via sb->s_xattr.
+        */
+       if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+               return generic_setxattr(dentry, name, value, size, flags);
 
-               ret = btrfs_del_item(trans, root, path);
-               BUG_ON(ret);
-               btrfs_release_path(root, path);
-       }
-       ret = 0;
-out:
-       btrfs_free_path(path);
+       if (!btrfs_is_valid_xattr(name))
+               return -EOPNOTSUPP;
 
-       return ret;
+       if (size == 0)
+               value = "";  /* empty EA, do not remove */
+       return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
 }
 
-/*
- * Handler functions
- */
-#define BTRFS_XATTR_SETGET_FUNCS(name, index)                          \
-static int btrfs_xattr_##name##_get(struct inode *inode,               \
-                                   const char *name, void *value,      \
-                                   size_t size)                        \
-{                                                                      \
-       return btrfs_xattr_get(inode, index, name, value, size);        \
-}                                                                      \
-static int btrfs_xattr_##name##_set(struct inode *inode,               \
-                                   const char *name, const void *value,\
-                                   size_t size, int flags)             \
-{                                                                      \
-       return btrfs_xattr_set(inode, index, name, value, size, flags); \
-}                                                                      \
-
-BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
-BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
-BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
-BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
-
-struct xattr_handler btrfs_xattr_security_handler = {
-       .prefix = XATTR_SECURITY_PREFIX,
-       .list   = btrfs_xattr_generic_list,
-       .get    = btrfs_xattr_security_get,
-       .set    = btrfs_xattr_security_set,
-};
-
-struct xattr_handler btrfs_xattr_system_handler = {
-       .prefix = XATTR_SYSTEM_PREFIX,
-       .list   = btrfs_xattr_generic_list,
-       .get    = btrfs_xattr_system_get,
-       .set    = btrfs_xattr_system_set,
-};
-
-struct xattr_handler btrfs_xattr_user_handler = {
-       .prefix = XATTR_USER_PREFIX,
-       .list   = btrfs_xattr_generic_list,
-       .get    = btrfs_xattr_user_get,
-       .set    = btrfs_xattr_user_set,
-};
+int btrfs_removexattr(struct dentry *dentry, const char *name)
+{
+       /*
+        * If this is a request for a synthetic attribute in the system.*
+        * namespace use the generic infrastructure to resolve a handler
+        * for it via sb->s_xattr.
+        */
+       if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+               return generic_removexattr(dentry, name);
 
-struct xattr_handler btrfs_xattr_trusted_handler = {
-       .prefix = XATTR_USER_PREFIX,
-       .list   = btrfs_xattr_generic_list,
-       .get    = btrfs_xattr_trusted_get,
-       .set    = btrfs_xattr_trusted_set,
-};
+       if (!btrfs_is_valid_xattr(name))
+               return -EOPNOTSUPP;
+       return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
+}