Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 1 | #include <linux/fs.h> |
| 2 | #include <linux/types.h> |
| 3 | #include "ctree.h" |
| 4 | #include "disk-io.h" |
| 5 | #include "btrfs_inode.h" |
| 6 | #include "print-tree.h" |
| 7 | #include "export.h" |
| 8 | #include "compat.h" |
| 9 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 10 | #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \ |
| 11 | parent_objectid) / 4) |
| 12 | #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \ |
| 13 | parent_root_objectid) / 4) |
| 14 | #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4) |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 15 | |
| 16 | static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len, |
| 17 | int connectable) |
| 18 | { |
| 19 | struct btrfs_fid *fid = (struct btrfs_fid *)fh; |
| 20 | struct inode *inode = dentry->d_inode; |
| 21 | int len = *max_len; |
| 22 | int type; |
| 23 | |
| 24 | if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) || |
| 25 | (connectable && len < BTRFS_FID_SIZE_CONNECTABLE)) |
| 26 | return 255; |
| 27 | |
| 28 | len = BTRFS_FID_SIZE_NON_CONNECTABLE; |
| 29 | type = FILEID_BTRFS_WITHOUT_PARENT; |
| 30 | |
| 31 | fid->objectid = BTRFS_I(inode)->location.objectid; |
| 32 | fid->root_objectid = BTRFS_I(inode)->root->objectid; |
| 33 | fid->gen = inode->i_generation; |
| 34 | |
| 35 | if (connectable && !S_ISDIR(inode->i_mode)) { |
| 36 | struct inode *parent; |
| 37 | u64 parent_root_id; |
| 38 | |
| 39 | spin_lock(&dentry->d_lock); |
| 40 | |
| 41 | parent = dentry->d_parent->d_inode; |
| 42 | fid->parent_objectid = BTRFS_I(parent)->location.objectid; |
| 43 | fid->parent_gen = parent->i_generation; |
| 44 | parent_root_id = BTRFS_I(parent)->root->objectid; |
| 45 | |
| 46 | spin_unlock(&dentry->d_lock); |
| 47 | |
| 48 | if (parent_root_id != fid->root_objectid) { |
| 49 | fid->parent_root_objectid = parent_root_id; |
| 50 | len = BTRFS_FID_SIZE_CONNECTABLE_ROOT; |
| 51 | type = FILEID_BTRFS_WITH_PARENT_ROOT; |
| 52 | } else { |
| 53 | len = BTRFS_FID_SIZE_CONNECTABLE; |
| 54 | type = FILEID_BTRFS_WITH_PARENT; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | *max_len = len; |
| 59 | return type; |
| 60 | } |
| 61 | |
| 62 | static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid, |
| 63 | u64 root_objectid, u32 generation) |
| 64 | { |
| 65 | struct btrfs_root *root; |
| 66 | struct inode *inode; |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 67 | struct btrfs_key key; |
| 68 | |
David Woodhouse | 2d4d9fbd | 2008-08-19 22:20:17 +0100 | [diff] [blame] | 69 | key.objectid = root_objectid; |
| 70 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 71 | key.offset = (u64)-1; |
| 72 | |
| 73 | root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key); |
| 74 | if (IS_ERR(root)) |
| 75 | return ERR_CAST(root); |
| 76 | |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 77 | key.objectid = objectid; |
| 78 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 79 | key.offset = 0; |
| 80 | |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 81 | inode = btrfs_iget(sb, &key, root, NULL); |
| 82 | if (IS_ERR(inode)) |
| 83 | return (void *)inode; |
| 84 | |
| 85 | if (generation != inode->i_generation) { |
| 86 | iput(inode); |
| 87 | return ERR_PTR(-ESTALE); |
| 88 | } |
| 89 | |
Christoph Hellwig | 50ec891 | 2008-09-05 16:43:20 -0400 | [diff] [blame] | 90 | return d_obtain_alias(inode); |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh, |
| 94 | int fh_len, int fh_type) |
| 95 | { |
| 96 | struct btrfs_fid *fid = (struct btrfs_fid *) fh; |
| 97 | u64 objectid, root_objectid; |
| 98 | u32 generation; |
| 99 | |
| 100 | if (fh_type == FILEID_BTRFS_WITH_PARENT) { |
| 101 | if (fh_len != BTRFS_FID_SIZE_CONNECTABLE) |
| 102 | return NULL; |
| 103 | root_objectid = fid->root_objectid; |
| 104 | } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) { |
| 105 | if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) |
| 106 | return NULL; |
| 107 | root_objectid = fid->parent_root_objectid; |
| 108 | } else |
| 109 | return NULL; |
| 110 | |
| 111 | objectid = fid->parent_objectid; |
| 112 | generation = fid->parent_gen; |
| 113 | |
| 114 | return btrfs_get_dentry(sb, objectid, root_objectid, generation); |
| 115 | } |
| 116 | |
| 117 | static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh, |
| 118 | int fh_len, int fh_type) |
| 119 | { |
| 120 | struct btrfs_fid *fid = (struct btrfs_fid *) fh; |
| 121 | u64 objectid, root_objectid; |
| 122 | u32 generation; |
| 123 | |
| 124 | if ((fh_type != FILEID_BTRFS_WITH_PARENT || |
| 125 | fh_len != BTRFS_FID_SIZE_CONNECTABLE) && |
| 126 | (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT || |
| 127 | fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) && |
| 128 | (fh_type != FILEID_BTRFS_WITHOUT_PARENT || |
| 129 | fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE)) |
| 130 | return NULL; |
| 131 | |
| 132 | objectid = fid->objectid; |
| 133 | root_objectid = fid->root_objectid; |
| 134 | generation = fid->gen; |
| 135 | |
| 136 | return btrfs_get_dentry(sb, objectid, root_objectid, generation); |
| 137 | } |
| 138 | |
| 139 | static struct dentry *btrfs_get_parent(struct dentry *child) |
| 140 | { |
| 141 | struct inode *dir = child->d_inode; |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 142 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 143 | struct btrfs_key key; |
| 144 | struct btrfs_path *path; |
| 145 | struct extent_buffer *leaf; |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 146 | int slot; |
| 147 | u64 objectid; |
| 148 | int ret; |
| 149 | |
| 150 | path = btrfs_alloc_path(); |
| 151 | |
| 152 | key.objectid = dir->i_ino; |
| 153 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); |
David Woodhouse | 87acb4e | 2008-08-18 22:50:22 +0100 | [diff] [blame] | 154 | key.offset = (u64)-1; |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 155 | |
David Woodhouse | 87acb4e | 2008-08-18 22:50:22 +0100 | [diff] [blame] | 156 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
David Woodhouse | d54a839 | 2008-08-19 22:33:04 +0100 | [diff] [blame] | 157 | if (ret < 0) { |
| 158 | /* Error */ |
| 159 | btrfs_free_path(path); |
| 160 | return ERR_PTR(ret); |
| 161 | } |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 162 | leaf = path->nodes[0]; |
| 163 | slot = path->slots[0]; |
David Woodhouse | d54a839 | 2008-08-19 22:33:04 +0100 | [diff] [blame] | 164 | if (ret) { |
| 165 | /* btrfs_search_slot() returns the slot where we'd want to |
| 166 | insert a backref for parent inode #0xFFFFFFFFFFFFFFFF. |
| 167 | The _real_ backref, telling us what the parent inode |
| 168 | _actually_ is, will be in the slot _before_ the one |
| 169 | that btrfs_search_slot() returns. */ |
| 170 | if (!slot) { |
| 171 | /* Unless there is _no_ key in the tree before... */ |
| 172 | btrfs_free_path(path); |
| 173 | return ERR_PTR(-EIO); |
| 174 | } |
| 175 | slot--; |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 176 | } |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 177 | |
| 178 | btrfs_item_key_to_cpu(leaf, &key, slot); |
David Woodhouse | 87acb4e | 2008-08-18 22:50:22 +0100 | [diff] [blame] | 179 | btrfs_free_path(path); |
| 180 | |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 181 | if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY) |
David Woodhouse | d54a839 | 2008-08-19 22:33:04 +0100 | [diff] [blame] | 182 | return ERR_PTR(-EINVAL); |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 183 | |
| 184 | objectid = key.offset; |
| 185 | |
David Woodhouse | 2d4d9fbd | 2008-08-19 22:20:17 +0100 | [diff] [blame] | 186 | /* If we are already at the root of a subvol, return the real root */ |
| 187 | if (objectid == dir->i_ino) |
| 188 | return dget(dir->i_sb->s_root); |
| 189 | |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 190 | /* Build a new key for the inode item */ |
| 191 | key.objectid = objectid; |
| 192 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 193 | key.offset = 0; |
| 194 | |
Christoph Hellwig | 50ec891 | 2008-09-05 16:43:20 -0400 | [diff] [blame] | 195 | return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL)); |
Balaji Rao | be6e8dc | 2008-07-21 02:01:56 +0530 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | const struct export_operations btrfs_export_ops = { |
| 199 | .encode_fh = btrfs_encode_fh, |
| 200 | .fh_to_dentry = btrfs_fh_to_dentry, |
| 201 | .fh_to_parent = btrfs_fh_to_parent, |
| 202 | .get_parent = btrfs_get_parent, |
| 203 | }; |