]> Pileus Git - ~andy/linux/blobdiff - fs/btrfs/xattr.c
Btrfs: make sure reserve_metadata_bytes doesn't leak out strange errors
[~andy/linux] / fs / btrfs / xattr.c
index cfd660550ded035fd2fad7aadce102aa23fc27fb..d733b9cfea343207e71bdb6d8d8b51323717c800 100644 (file)
@@ -44,7 +44,7 @@ ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
                return -ENOMEM;
 
        /* lookup the xattr by name */
-       di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
+       di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name,
                                strlen(name), 0);
        if (!di) {
                ret = -ENODATA;
@@ -102,43 +102,57 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
        if (!path)
                return -ENOMEM;
 
-       /* first lets see if we already have this xattr */
-       di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
-                               strlen(name), -1);
-       if (IS_ERR(di)) {
-               ret = PTR_ERR(di);
-               goto out;
+       if (flags & XATTR_REPLACE) {
+               di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
+                                       name_len, -1);
+               if (IS_ERR(di)) {
+                       ret = PTR_ERR(di);
+                       goto out;
+               } else if (!di) {
+                       ret = -ENODATA;
+                       goto out;
+               }
+               ret = btrfs_delete_one_dir_name(trans, root, path, di);
+               if (ret)
+                       goto out;
+               btrfs_release_path(path);
        }
 
-       /* ok we already have this xattr, lets remove it */
-       if (di) {
-               /* if we want create only exit */
-               if (flags & XATTR_CREATE) {
-                       ret = -EEXIST;
+again:
+       ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
+                                     name, name_len, value, size);
+       if (ret == -EEXIST) {
+               if (flags & XATTR_CREATE)
                        goto out;
+               /*
+                * We can't use the path we already have since we won't have the
+                * proper locking for a delete, so release the path and
+                * re-lookup to delete the thing.
+                */
+               btrfs_release_path(path);
+               di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
+                                       name, name_len, -1);
+               if (IS_ERR(di)) {
+                       ret = PTR_ERR(di);
+                       goto out;
+               } else if (!di) {
+                       /* Shouldn't happen but just in case... */
+                       btrfs_release_path(path);
+                       goto again;
                }
 
                ret = btrfs_delete_one_dir_name(trans, root, path, di);
-               BUG_ON(ret);
-               btrfs_release_path(root, path);
-
-               /* if we don't have a value then we are removing the xattr */
-               if (!value)
+               if (ret)
                        goto out;
-       } else {
-               btrfs_release_path(root, path);
 
-               if (flags & XATTR_REPLACE) {
-                       /* we couldn't find the attr to replace */
-                       ret = -ENODATA;
-                       goto out;
+               /*
+                * We have a value to set, so go back and try to insert it now.
+                */
+               if (value) {
+                       btrfs_release_path(path);
+                       goto again;
                }
        }
-
-       /* ok we have to create a completely new xattr */
-       ret = btrfs_insert_xattr_item(trans, root, path, inode->i_ino,
-                                     name, name_len, value, size);
-       BUG_ON(ret);
 out:
        btrfs_free_path(path);
        return ret;
@@ -158,8 +172,6 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans,
        if (IS_ERR(trans))
                return PTR_ERR(trans);
 
-       btrfs_set_trans_block_group(trans, inode);
-
        ret = do_setxattr(trans, inode, name, value, size, flags);
        if (ret)
                goto out;
@@ -190,7 +202,7 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
         * NOTE: we set key.offset = 0; because we want to start with the
         * first xattr that we find and walk forward
         */
-       key.objectid = inode->i_ino;
+       key.objectid = btrfs_ino(inode);
        btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
        key.offset = 0;