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