Merge branch 'topic/hda' into for-linus
[pandora-kernel.git] / fs / btrfs / export.c
index 797b4cb..85315d2 100644 (file)
@@ -7,15 +7,11 @@
 #include "export.h"
 #include "compat.h"
 
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
-#define FILEID_BTRFS_WITHOUT_PARENT            0x4d
-#define FILEID_BTRFS_WITH_PARENT               0x4e
-#define FILEID_BTRFS_WITH_PARENT_ROOT          0x4f
-#endif
-
-#define BTRFS_FID_SIZE_NON_CONNECTABLE         (offsetof(struct btrfs_fid, parent_objectid)/4)
-#define BTRFS_FID_SIZE_CONNECTABLE             (offsetof(struct btrfs_fid, parent_root_objectid)/4)
-#define BTRFS_FID_SIZE_CONNECTABLE_ROOT                (sizeof(struct btrfs_fid)/4)
+#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
+                                                parent_objectid) / 4)
+#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
+                                            parent_root_objectid) / 4)
+#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
 
 static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
                           int connectable)
@@ -68,14 +64,20 @@ static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
 {
        struct btrfs_root *root;
        struct inode *inode;
-       struct dentry *result;
        struct btrfs_key key;
 
+       key.objectid = root_objectid;
+       btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
+       key.offset = (u64)-1;
+
+       root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
+       if (IS_ERR(root))
+               return ERR_CAST(root);
+
        key.objectid = objectid;
        btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
        key.offset = 0;
 
-       root = btrfs_lookup_fs_root(btrfs_sb(sb)->fs_info, root_objectid);
        inode = btrfs_iget(sb, &key, root, NULL);
        if (IS_ERR(inode))
                return (void *)inode;
@@ -85,11 +87,7 @@ static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
                return ERR_PTR(-ESTALE);
        }
 
-       result = d_obtain_alias(inode);
-       if (!result)
-               return ERR_PTR(-ENOMEM);
-
-       return result;
+       return d_obtain_alias(inode);
 }
 
 static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
@@ -141,13 +139,10 @@ static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
 static struct dentry *btrfs_get_parent(struct dentry *child)
 {
        struct inode *dir = child->d_inode;
-       struct inode *inode;
-       struct dentry *parent;
        struct btrfs_root *root = BTRFS_I(dir)->root;
        struct btrfs_key key;
        struct btrfs_path *path;
        struct extent_buffer *leaf;
-       u32 nritems;
        int slot;
        u64 objectid;
        int ret;
@@ -156,48 +151,48 @@ static struct dentry *btrfs_get_parent(struct dentry *child)
 
        key.objectid = dir->i_ino;
        btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
-       key.offset = 0;
-       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-       BUG_ON(ret == 0);
-       ret = 0;
+       key.offset = (u64)-1;
 
+       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+       if (ret < 0) {
+               /* Error */
+               btrfs_free_path(path);
+               return ERR_PTR(ret);
+       }
        leaf = path->nodes[0];
        slot = path->slots[0];
-       nritems = btrfs_header_nritems(leaf);
-       if (slot >= nritems) {
-               ret = btrfs_next_leaf(root, path);
-               if (ret) {
+       if (ret) {
+               /* btrfs_search_slot() returns the slot where we'd want to
+                  insert a backref for parent inode #0xFFFFFFFFFFFFFFFF.
+                  The _real_ backref, telling us what the parent inode
+                  _actually_ is, will be in the slot _before_ the one
+                  that btrfs_search_slot() returns. */
+               if (!slot) {
+                       /* Unless there is _no_ key in the tree before... */
                        btrfs_free_path(path);
-                       goto out;
+                       return ERR_PTR(-EIO);
                }
-               leaf = path->nodes[0];
-               slot = path->slots[0];
+               slot--;
        }
 
+       btrfs_item_key_to_cpu(leaf, &key, slot);
        btrfs_free_path(path);
 
-       btrfs_item_key_to_cpu(leaf, &key, slot);
        if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
-               goto out;
+               return ERR_PTR(-EINVAL);
 
        objectid = key.offset;
 
+       /* If we are already at the root of a subvol, return the real root */
+       if (objectid == dir->i_ino)
+               return dget(dir->i_sb->s_root);
+
        /* Build a new key for the inode item */
        key.objectid = objectid;
        btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
        key.offset = 0;
 
-       inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
-
-       parent = d_obtain_alias(inode);
-       if (!parent)
-               parent = ERR_PTR(-ENOMEM);
-
-       return parent;
-
-out:
-       btrfs_free_path(path);
-       return ERR_PTR(-EINVAL);
+       return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
 }
 
 const struct export_operations btrfs_export_ops = {