]>
Commit | Line | Data |
---|---|---|
2e635a27 | 1 | #include <linux/module.h> |
e20d96d6 | 2 | #include <linux/buffer_head.h> |
2e635a27 CM |
3 | #include <linux/fs.h> |
4 | #include <linux/pagemap.h> | |
5 | #include <linux/highmem.h> | |
6 | #include <linux/time.h> | |
7 | #include <linux/init.h> | |
8 | #include <linux/string.h> | |
9 | #include <linux/smp_lock.h> | |
10 | #include <linux/backing-dev.h> | |
dee26a9f | 11 | #include <linux/mpage.h> |
75dfe396 CM |
12 | #include <linux/swap.h> |
13 | #include <linux/writeback.h> | |
2e635a27 | 14 | #include "ctree.h" |
e20d96d6 | 15 | #include "disk-io.h" |
d5719762 | 16 | #include "transaction.h" |
2c90e5d6 | 17 | #include "btrfs_inode.h" |
c5739bba | 18 | #include "ioctl.h" |
2e635a27 | 19 | |
d6e4a428 CM |
20 | void btrfs_fsinfo_release(struct kobject *obj) |
21 | { | |
22 | struct btrfs_fs_info *fsinfo = container_of(obj, | |
23 | struct btrfs_fs_info, kobj); | |
24 | kfree(fsinfo); | |
25 | } | |
26 | ||
27 | struct kobj_type btrfs_fsinfo_ktype = { | |
28 | .release = btrfs_fsinfo_release, | |
29 | }; | |
30 | ||
c5739bba CM |
31 | struct btrfs_iget_args { |
32 | u64 ino; | |
33 | struct btrfs_root *root; | |
34 | }; | |
35 | ||
d6e4a428 CM |
36 | decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL); |
37 | ||
2e635a27 | 38 | #define BTRFS_SUPER_MAGIC 0x9123682E |
e20d96d6 CM |
39 | |
40 | static struct inode_operations btrfs_dir_inode_operations; | |
d6e4a428 | 41 | static struct inode_operations btrfs_dir_ro_inode_operations; |
e20d96d6 CM |
42 | static struct super_operations btrfs_super_ops; |
43 | static struct file_operations btrfs_dir_file_operations; | |
dee26a9f CM |
44 | static struct inode_operations btrfs_file_inode_operations; |
45 | static struct address_space_operations btrfs_aops; | |
46 | static struct file_operations btrfs_file_operations; | |
e20d96d6 | 47 | |
e20d96d6 | 48 | static void btrfs_read_locked_inode(struct inode *inode) |
2e635a27 | 49 | { |
5caf2a00 | 50 | struct btrfs_path *path; |
e20d96d6 | 51 | struct btrfs_inode_item *inode_item; |
d6e4a428 CM |
52 | struct btrfs_root *root = BTRFS_I(inode)->root; |
53 | struct btrfs_key location; | |
e20d96d6 | 54 | int ret; |
f4b9aa8d | 55 | |
5caf2a00 CM |
56 | path = btrfs_alloc_path(); |
57 | BUG_ON(!path); | |
58 | btrfs_init_path(path); | |
f4b9aa8d CM |
59 | mutex_lock(&root->fs_info->fs_mutex); |
60 | ||
d6e4a428 CM |
61 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
62 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); | |
e20d96d6 | 63 | if (ret) { |
7cfcc17e | 64 | btrfs_free_path(path); |
d6e4a428 | 65 | goto make_bad; |
e20d96d6 | 66 | } |
5caf2a00 CM |
67 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
68 | path->slots[0], | |
e20d96d6 | 69 | struct btrfs_inode_item); |
2e635a27 | 70 | |
e20d96d6 CM |
71 | inode->i_mode = btrfs_inode_mode(inode_item); |
72 | inode->i_nlink = btrfs_inode_nlink(inode_item); | |
73 | inode->i_uid = btrfs_inode_uid(inode_item); | |
74 | inode->i_gid = btrfs_inode_gid(inode_item); | |
75 | inode->i_size = btrfs_inode_size(inode_item); | |
76 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); | |
77 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); | |
78 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); | |
79 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); | |
80 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); | |
81 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); | |
82 | inode->i_blocks = btrfs_inode_nblocks(inode_item); | |
83 | inode->i_generation = btrfs_inode_generation(inode_item); | |
5caf2a00 | 84 | |
5caf2a00 CM |
85 | btrfs_free_path(path); |
86 | inode_item = NULL; | |
87 | ||
f4b9aa8d | 88 | mutex_unlock(&root->fs_info->fs_mutex); |
1b05da2e | 89 | |
e20d96d6 CM |
90 | switch (inode->i_mode & S_IFMT) { |
91 | #if 0 | |
92 | default: | |
93 | init_special_inode(inode, inode->i_mode, | |
94 | btrfs_inode_rdev(inode_item)); | |
95 | break; | |
96 | #endif | |
97 | case S_IFREG: | |
dee26a9f CM |
98 | inode->i_mapping->a_ops = &btrfs_aops; |
99 | inode->i_fop = &btrfs_file_operations; | |
100 | inode->i_op = &btrfs_file_inode_operations; | |
e20d96d6 CM |
101 | break; |
102 | case S_IFDIR: | |
e20d96d6 | 103 | inode->i_fop = &btrfs_dir_file_operations; |
d6e4a428 CM |
104 | if (root == root->fs_info->tree_root) |
105 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
106 | else | |
107 | inode->i_op = &btrfs_dir_inode_operations; | |
e20d96d6 CM |
108 | break; |
109 | case S_IFLNK: | |
e20d96d6 CM |
110 | // inode->i_op = &page_symlink_inode_operations; |
111 | break; | |
2e635a27 | 112 | } |
e20d96d6 | 113 | return; |
d6e4a428 CM |
114 | |
115 | make_bad: | |
116 | btrfs_release_path(root, path); | |
117 | btrfs_free_path(path); | |
118 | mutex_unlock(&root->fs_info->fs_mutex); | |
119 | make_bad_inode(inode); | |
2e635a27 CM |
120 | } |
121 | ||
5f443fd2 CM |
122 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
123 | struct btrfs_root *root, | |
124 | struct inode *dir, | |
125 | struct dentry *dentry) | |
134e9731 | 126 | { |
5caf2a00 | 127 | struct btrfs_path *path; |
134e9731 CM |
128 | const char *name = dentry->d_name.name; |
129 | int name_len = dentry->d_name.len; | |
130 | int ret; | |
131 | u64 objectid; | |
132 | struct btrfs_dir_item *di; | |
133 | ||
5caf2a00 CM |
134 | path = btrfs_alloc_path(); |
135 | BUG_ON(!path); | |
136 | btrfs_init_path(path); | |
137 | ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, | |
134e9731 CM |
138 | name, name_len, -1); |
139 | if (ret < 0) | |
140 | goto err; | |
141 | if (ret > 0) { | |
142 | ret = -ENOENT; | |
143 | goto err; | |
144 | } | |
5caf2a00 | 145 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
134e9731 | 146 | struct btrfs_dir_item); |
d6e4a428 | 147 | objectid = btrfs_disk_key_objectid(&di->location); |
134e9731 | 148 | |
5f26f772 CM |
149 | ret = btrfs_del_item(trans, root, path); |
150 | BUG_ON(ret); | |
151 | ||
152 | btrfs_release_path(root, path); | |
153 | ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, | |
154 | objectid, -1); | |
155 | BUG_ON(ret); | |
5caf2a00 | 156 | ret = btrfs_del_item(trans, root, path); |
134e9731 CM |
157 | BUG_ON(ret); |
158 | dentry->d_inode->i_ctime = dir->i_ctime; | |
159 | err: | |
5caf2a00 CM |
160 | btrfs_release_path(root, path); |
161 | btrfs_free_path(path); | |
d4dbff95 | 162 | if (ret == 0) { |
134e9731 | 163 | inode_dec_link_count(dentry->d_inode); |
5f26f772 | 164 | dir->i_size -= name_len * 2; |
d4dbff95 CM |
165 | mark_inode_dirty(dir); |
166 | } | |
134e9731 CM |
167 | return ret; |
168 | } | |
169 | ||
5f443fd2 CM |
170 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
171 | { | |
172 | struct btrfs_root *root; | |
173 | struct btrfs_trans_handle *trans; | |
174 | int ret; | |
175 | ||
d6e4a428 | 176 | root = BTRFS_I(dir)->root; |
5f443fd2 CM |
177 | mutex_lock(&root->fs_info->fs_mutex); |
178 | trans = btrfs_start_transaction(root, 1); | |
179 | ret = btrfs_unlink_trans(trans, root, dir, dentry); | |
180 | btrfs_end_transaction(trans, root); | |
181 | mutex_unlock(&root->fs_info->fs_mutex); | |
182 | return ret; | |
183 | } | |
184 | ||
185 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |
186 | { | |
187 | struct inode *inode = dentry->d_inode; | |
188 | int err; | |
189 | int ret; | |
d6e4a428 | 190 | struct btrfs_root *root = BTRFS_I(dir)->root; |
5caf2a00 | 191 | struct btrfs_path *path; |
5f443fd2 CM |
192 | struct btrfs_key key; |
193 | struct btrfs_trans_handle *trans; | |
5f26f772 CM |
194 | struct btrfs_key found_key; |
195 | int found_type; | |
5f443fd2 | 196 | struct btrfs_leaf *leaf; |
5f26f772 | 197 | char *goodnames = ".."; |
5f443fd2 | 198 | |
5caf2a00 CM |
199 | path = btrfs_alloc_path(); |
200 | BUG_ON(!path); | |
201 | btrfs_init_path(path); | |
5f443fd2 CM |
202 | mutex_lock(&root->fs_info->fs_mutex); |
203 | trans = btrfs_start_transaction(root, 1); | |
204 | key.objectid = inode->i_ino; | |
205 | key.offset = (u64)-1; | |
5f26f772 CM |
206 | key.flags = (u32)-1; |
207 | while(1) { | |
208 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
209 | if (ret < 0) { | |
210 | err = ret; | |
211 | goto out; | |
212 | } | |
213 | BUG_ON(ret == 0); | |
214 | if (path->slots[0] == 0) { | |
215 | err = -ENOENT; | |
216 | goto out; | |
217 | } | |
218 | path->slots[0]--; | |
219 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
220 | btrfs_disk_key_to_cpu(&found_key, | |
221 | &leaf->items[path->slots[0]].key); | |
222 | found_type = btrfs_key_type(&found_key); | |
223 | if (found_key.objectid != inode->i_ino) { | |
224 | err = -ENOENT; | |
225 | goto out; | |
226 | } | |
227 | if ((found_type != BTRFS_DIR_ITEM_KEY && | |
228 | found_type != BTRFS_DIR_INDEX_KEY) || | |
229 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && | |
230 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { | |
231 | err = -ENOTEMPTY; | |
232 | goto out; | |
233 | } | |
234 | ret = btrfs_del_item(trans, root, path); | |
235 | BUG_ON(ret); | |
5f443fd2 | 236 | |
5f26f772 CM |
237 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
238 | break; | |
239 | btrfs_release_path(root, path); | |
5f443fd2 | 240 | } |
5f26f772 | 241 | ret = 0; |
5caf2a00 | 242 | btrfs_release_path(root, path); |
5f443fd2 CM |
243 | |
244 | /* now the directory is empty */ | |
245 | err = btrfs_unlink_trans(trans, root, dir, dentry); | |
246 | if (!err) { | |
247 | inode->i_size = 0; | |
248 | } | |
249 | out: | |
7cfcc17e CM |
250 | btrfs_release_path(root, path); |
251 | btrfs_free_path(path); | |
5f443fd2 CM |
252 | mutex_unlock(&root->fs_info->fs_mutex); |
253 | ret = btrfs_end_transaction(trans, root); | |
254 | if (ret && !err) | |
255 | err = ret; | |
256 | return err; | |
257 | } | |
258 | ||
134e9731 CM |
259 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
260 | struct btrfs_root *root, | |
261 | struct inode *inode) | |
262 | { | |
5caf2a00 | 263 | struct btrfs_path *path; |
134e9731 | 264 | int ret; |
5caf2a00 | 265 | |
134e9731 | 266 | clear_inode(inode); |
5caf2a00 CM |
267 | |
268 | path = btrfs_alloc_path(); | |
269 | BUG_ON(!path); | |
270 | btrfs_init_path(path); | |
d6e4a428 CM |
271 | ret = btrfs_lookup_inode(trans, root, path, |
272 | &BTRFS_I(inode)->location, -1); | |
134e9731 | 273 | BUG_ON(ret); |
5caf2a00 | 274 | ret = btrfs_del_item(trans, root, path); |
134e9731 | 275 | BUG_ON(ret); |
5caf2a00 | 276 | btrfs_free_path(path); |
134e9731 CM |
277 | return ret; |
278 | } | |
279 | ||
f4b9aa8d CM |
280 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
281 | struct btrfs_root *root, | |
282 | struct inode *inode) | |
283 | { | |
284 | int ret; | |
5caf2a00 | 285 | struct btrfs_path *path; |
f4b9aa8d CM |
286 | struct btrfs_key key; |
287 | struct btrfs_disk_key *found_key; | |
288 | struct btrfs_leaf *leaf; | |
f254e52c CM |
289 | struct btrfs_file_extent_item *fi = NULL; |
290 | u64 extent_start = 0; | |
291 | u64 extent_num_blocks = 0; | |
292 | int found_extent; | |
f4b9aa8d | 293 | |
5caf2a00 CM |
294 | path = btrfs_alloc_path(); |
295 | BUG_ON(!path); | |
f4b9aa8d CM |
296 | /* FIXME, add redo link to tree so we don't leak on crash */ |
297 | key.objectid = inode->i_ino; | |
298 | key.offset = (u64)-1; | |
299 | key.flags = 0; | |
d4dbff95 CM |
300 | /* |
301 | * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys | |
302 | * or extent data | |
303 | */ | |
f254e52c | 304 | btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY); |
f4b9aa8d | 305 | while(1) { |
5caf2a00 CM |
306 | btrfs_init_path(path); |
307 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
f4b9aa8d | 308 | if (ret < 0) { |
f4b9aa8d CM |
309 | goto error; |
310 | } | |
311 | if (ret > 0) { | |
5caf2a00 CM |
312 | BUG_ON(path->slots[0] == 0); |
313 | path->slots[0]--; | |
f4b9aa8d | 314 | } |
5caf2a00 CM |
315 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
316 | found_key = &leaf->items[path->slots[0]].key; | |
f4b9aa8d CM |
317 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
318 | break; | |
f254e52c | 319 | if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY && |
d4dbff95 | 320 | btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY && |
f254e52c | 321 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
f4b9aa8d | 322 | break; |
f4b9aa8d CM |
323 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
324 | break; | |
f254e52c | 325 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
5caf2a00 CM |
326 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
327 | path->slots[0], | |
f254e52c CM |
328 | struct btrfs_file_extent_item); |
329 | extent_start = btrfs_file_extent_disk_blocknr(fi); | |
330 | extent_num_blocks = | |
331 | btrfs_file_extent_disk_num_blocks(fi); | |
332 | inode->i_blocks -= | |
333 | btrfs_file_extent_num_blocks(fi) >> 9; | |
334 | found_extent = 1; | |
335 | } else { | |
336 | found_extent = 0; | |
337 | } | |
5caf2a00 | 338 | ret = btrfs_del_item(trans, root, path); |
f4b9aa8d | 339 | BUG_ON(ret); |
5caf2a00 | 340 | btrfs_release_path(root, path); |
f254e52c CM |
341 | if (found_extent) { |
342 | ret = btrfs_free_extent(trans, root, extent_start, | |
343 | extent_num_blocks, 0); | |
344 | BUG_ON(ret); | |
345 | } | |
f4b9aa8d | 346 | } |
f4b9aa8d CM |
347 | ret = 0; |
348 | error: | |
5caf2a00 CM |
349 | btrfs_release_path(root, path); |
350 | btrfs_free_path(path); | |
f4b9aa8d CM |
351 | return ret; |
352 | } | |
353 | ||
134e9731 CM |
354 | static void btrfs_delete_inode(struct inode *inode) |
355 | { | |
356 | struct btrfs_trans_handle *trans; | |
d6e4a428 | 357 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f4b9aa8d CM |
358 | int ret; |
359 | ||
134e9731 CM |
360 | truncate_inode_pages(&inode->i_data, 0); |
361 | if (is_bad_inode(inode)) { | |
362 | goto no_delete; | |
363 | } | |
364 | inode->i_size = 0; | |
134e9731 CM |
365 | mutex_lock(&root->fs_info->fs_mutex); |
366 | trans = btrfs_start_transaction(root, 1); | |
f4b9aa8d CM |
367 | if (S_ISREG(inode->i_mode)) { |
368 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
369 | BUG_ON(ret); | |
370 | } | |
134e9731 CM |
371 | btrfs_free_inode(trans, root, inode); |
372 | btrfs_end_transaction(trans, root); | |
373 | mutex_unlock(&root->fs_info->fs_mutex); | |
374 | return; | |
375 | no_delete: | |
376 | clear_inode(inode); | |
377 | } | |
378 | ||
e20d96d6 | 379 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
d6e4a428 | 380 | struct btrfs_key *location) |
e20d96d6 CM |
381 | { |
382 | const char *name = dentry->d_name.name; | |
383 | int namelen = dentry->d_name.len; | |
384 | struct btrfs_dir_item *di; | |
5caf2a00 | 385 | struct btrfs_path *path; |
d6e4a428 | 386 | struct btrfs_root *root = BTRFS_I(dir)->root; |
e20d96d6 CM |
387 | int ret; |
388 | ||
5caf2a00 CM |
389 | path = btrfs_alloc_path(); |
390 | BUG_ON(!path); | |
391 | btrfs_init_path(path); | |
392 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, | |
e20d96d6 | 393 | namelen, 0); |
5caf2a00 | 394 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
d6e4a428 | 395 | location->objectid = 0; |
2c90e5d6 | 396 | ret = 0; |
e20d96d6 CM |
397 | goto out; |
398 | } | |
5caf2a00 | 399 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
e20d96d6 | 400 | struct btrfs_dir_item); |
d6e4a428 | 401 | btrfs_disk_key_to_cpu(location, &di->location); |
e20d96d6 | 402 | out: |
5caf2a00 CM |
403 | btrfs_release_path(root, path); |
404 | btrfs_free_path(path); | |
e20d96d6 CM |
405 | return ret; |
406 | } | |
407 | ||
d6e4a428 CM |
408 | int fixup_tree_root_location(struct btrfs_root *root, |
409 | struct btrfs_key *location, | |
410 | struct btrfs_root **sub_root) | |
411 | { | |
412 | struct btrfs_path *path; | |
413 | struct btrfs_root_item *ri; | |
d6e4a428 CM |
414 | |
415 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | |
416 | return 0; | |
417 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | |
418 | return 0; | |
419 | ||
420 | path = btrfs_alloc_path(); | |
421 | BUG_ON(!path); | |
422 | mutex_lock(&root->fs_info->fs_mutex); | |
423 | ||
0f7d52f4 CM |
424 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
425 | if (IS_ERR(*sub_root)) | |
426 | return PTR_ERR(*sub_root); | |
427 | ||
428 | ri = &(*sub_root)->root_item; | |
d6e4a428 CM |
429 | location->objectid = btrfs_root_dirid(ri); |
430 | location->flags = 0; | |
431 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
432 | location->offset = 0; | |
0f7d52f4 | 433 | |
d6e4a428 CM |
434 | btrfs_free_path(path); |
435 | mutex_unlock(&root->fs_info->fs_mutex); | |
0f7d52f4 | 436 | return 0; |
d6e4a428 CM |
437 | } |
438 | ||
c5739bba CM |
439 | int btrfs_init_locked_inode(struct inode *inode, void *p) |
440 | { | |
441 | struct btrfs_iget_args *args = p; | |
442 | inode->i_ino = args->ino; | |
443 | BTRFS_I(inode)->root = args->root; | |
444 | return 0; | |
445 | } | |
446 | ||
447 | int btrfs_find_actor(struct inode *inode, void *opaque) | |
448 | { | |
449 | struct btrfs_iget_args *args = opaque; | |
450 | return (args->ino == inode->i_ino && | |
451 | args->root == BTRFS_I(inode)->root); | |
452 | } | |
453 | ||
454 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, | |
455 | struct btrfs_root *root) | |
456 | { | |
457 | struct inode *inode; | |
458 | struct btrfs_iget_args args; | |
459 | args.ino = objectid; | |
460 | args.root = root; | |
461 | ||
462 | inode = iget5_locked(s, objectid, btrfs_find_actor, | |
463 | btrfs_init_locked_inode, | |
464 | (void *)&args); | |
465 | return inode; | |
466 | } | |
d6e4a428 | 467 | |
e20d96d6 CM |
468 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
469 | struct nameidata *nd) | |
470 | { | |
471 | struct inode * inode; | |
d6e4a428 CM |
472 | struct btrfs_inode *bi = BTRFS_I(dir); |
473 | struct btrfs_root *root = bi->root; | |
474 | struct btrfs_root *sub_root = root; | |
475 | struct btrfs_key location; | |
e20d96d6 CM |
476 | int ret; |
477 | ||
478 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
479 | return ERR_PTR(-ENAMETOOLONG); | |
22b0ebda | 480 | mutex_lock(&root->fs_info->fs_mutex); |
d6e4a428 | 481 | ret = btrfs_inode_by_name(dir, dentry, &location); |
22b0ebda | 482 | mutex_unlock(&root->fs_info->fs_mutex); |
e20d96d6 CM |
483 | if (ret < 0) |
484 | return ERR_PTR(ret); | |
485 | inode = NULL; | |
d6e4a428 CM |
486 | if (location.objectid) { |
487 | ret = fixup_tree_root_location(root, &location, &sub_root); | |
488 | if (ret < 0) | |
489 | return ERR_PTR(ret); | |
490 | if (ret > 0) | |
491 | return ERR_PTR(-ENOENT); | |
c5739bba CM |
492 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
493 | sub_root); | |
e20d96d6 CM |
494 | if (!inode) |
495 | return ERR_PTR(-EACCES); | |
d6e4a428 | 496 | if (inode->i_state & I_NEW) { |
0f7d52f4 | 497 | if (sub_root != root) { |
c5739bba | 498 | printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root); |
0f7d52f4 CM |
499 | igrab(inode); |
500 | sub_root->inode = inode; | |
501 | } | |
d6e4a428 CM |
502 | BTRFS_I(inode)->root = sub_root; |
503 | memcpy(&BTRFS_I(inode)->location, &location, | |
504 | sizeof(location)); | |
505 | btrfs_read_locked_inode(inode); | |
506 | unlock_new_inode(inode); | |
507 | } | |
e20d96d6 CM |
508 | } |
509 | return d_splice_alias(inode, dentry); | |
510 | } | |
511 | ||
512 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
513 | { | |
514 | struct inode *inode = filp->f_path.dentry->d_inode; | |
d6e4a428 | 515 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e20d96d6 CM |
516 | struct btrfs_item *item; |
517 | struct btrfs_dir_item *di; | |
518 | struct btrfs_key key; | |
5caf2a00 | 519 | struct btrfs_path *path; |
e20d96d6 CM |
520 | int ret; |
521 | u32 nritems; | |
522 | struct btrfs_leaf *leaf; | |
523 | int slot; | |
524 | int advance; | |
525 | unsigned char d_type = DT_UNKNOWN; | |
7f5c1516 | 526 | int over = 0; |
d6e4a428 CM |
527 | int key_type = BTRFS_DIR_INDEX_KEY; |
528 | ||
529 | /* FIXME, use a real flag for deciding about the key type */ | |
530 | if (root->fs_info->tree_root == root) | |
531 | key_type = BTRFS_DIR_ITEM_KEY; | |
22b0ebda | 532 | mutex_lock(&root->fs_info->fs_mutex); |
e20d96d6 | 533 | key.objectid = inode->i_ino; |
e20d96d6 | 534 | key.flags = 0; |
d6e4a428 | 535 | btrfs_set_key_type(&key, key_type); |
e20d96d6 | 536 | key.offset = filp->f_pos; |
5caf2a00 CM |
537 | path = btrfs_alloc_path(); |
538 | btrfs_init_path(path); | |
539 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1b05da2e | 540 | if (ret < 0) |
e20d96d6 | 541 | goto err; |
7f5c1516 | 542 | advance = 0; |
e20d96d6 | 543 | while(1) { |
5caf2a00 | 544 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
e20d96d6 | 545 | nritems = btrfs_header_nritems(&leaf->header); |
5caf2a00 | 546 | slot = path->slots[0]; |
dee26a9f CM |
547 | if (advance || slot >= nritems) { |
548 | if (slot >= nritems -1) { | |
5caf2a00 | 549 | ret = btrfs_next_leaf(root, path); |
e20d96d6 CM |
550 | if (ret) |
551 | break; | |
5caf2a00 | 552 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
e20d96d6 | 553 | nritems = btrfs_header_nritems(&leaf->header); |
5caf2a00 | 554 | slot = path->slots[0]; |
e20d96d6 CM |
555 | } else { |
556 | slot++; | |
5caf2a00 | 557 | path->slots[0]++; |
e20d96d6 CM |
558 | } |
559 | } | |
560 | advance = 1; | |
561 | item = leaf->items + slot; | |
e20d96d6 CM |
562 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
563 | break; | |
d6e4a428 | 564 | if (key_type == BTRFS_DIR_INDEX_KEY && |
1b05da2e | 565 | btrfs_disk_key_offset(&item->key) > root->highest_inode) |
5be6f7f1 | 566 | break; |
d6e4a428 | 567 | if (btrfs_disk_key_type(&item->key) != key_type) |
e20d96d6 | 568 | continue; |
7f5c1516 CM |
569 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
570 | continue; | |
7fcde0e3 | 571 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
dee26a9f | 572 | advance = 1; |
e20d96d6 | 573 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
e20d96d6 CM |
574 | over = filldir(dirent, (const char *)(di + 1), |
575 | btrfs_dir_name_len(di), | |
576 | btrfs_disk_key_offset(&item->key), | |
d6e4a428 | 577 | btrfs_disk_key_objectid(&di->location), d_type); |
7fcde0e3 CM |
578 | if (over) |
579 | goto nopos; | |
e20d96d6 | 580 | } |
7fcde0e3 CM |
581 | filp->f_pos++; |
582 | nopos: | |
e20d96d6 CM |
583 | ret = 0; |
584 | err: | |
5caf2a00 CM |
585 | btrfs_release_path(root, path); |
586 | btrfs_free_path(path); | |
22b0ebda | 587 | mutex_unlock(&root->fs_info->fs_mutex); |
e20d96d6 CM |
588 | return ret; |
589 | } | |
590 | ||
591 | static void btrfs_put_super (struct super_block * sb) | |
592 | { | |
593 | struct btrfs_root *root = btrfs_sb(sb); | |
594 | int ret; | |
595 | ||
596 | ret = close_ctree(root); | |
597 | if (ret) { | |
598 | printk("close ctree returns %d\n", ret); | |
599 | } | |
600 | sb->s_fs_info = NULL; | |
601 | } | |
2e635a27 CM |
602 | |
603 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) | |
604 | { | |
605 | struct inode * inode; | |
e20d96d6 CM |
606 | struct dentry * root_dentry; |
607 | struct btrfs_super_block *disk_super; | |
0f7d52f4 | 608 | struct btrfs_root *tree_root; |
d6e4a428 | 609 | struct btrfs_inode *bi; |
2e635a27 CM |
610 | |
611 | sb->s_maxbytes = MAX_LFS_FILESIZE; | |
2e635a27 | 612 | sb->s_magic = BTRFS_SUPER_MAGIC; |
e20d96d6 | 613 | sb->s_op = &btrfs_super_ops; |
2e635a27 | 614 | sb->s_time_gran = 1; |
e20d96d6 | 615 | |
0f7d52f4 | 616 | tree_root = open_ctree(sb); |
d98237b3 | 617 | |
0f7d52f4 | 618 | if (!tree_root) { |
e20d96d6 CM |
619 | printk("btrfs: open_ctree failed\n"); |
620 | return -EIO; | |
621 | } | |
0f7d52f4 CM |
622 | sb->s_fs_info = tree_root; |
623 | disk_super = tree_root->fs_info->disk_super; | |
e20d96d6 CM |
624 | printk("read in super total blocks %Lu root %Lu\n", |
625 | btrfs_super_total_blocks(disk_super), | |
626 | btrfs_super_root_dir(disk_super)); | |
627 | ||
c5739bba CM |
628 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
629 | tree_root); | |
d6e4a428 CM |
630 | bi = BTRFS_I(inode); |
631 | bi->location.objectid = inode->i_ino; | |
632 | bi->location.offset = 0; | |
633 | bi->location.flags = 0; | |
0f7d52f4 | 634 | bi->root = tree_root; |
d6e4a428 CM |
635 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
636 | ||
2e635a27 CM |
637 | if (!inode) |
638 | return -ENOMEM; | |
e20d96d6 CM |
639 | if (inode->i_state & I_NEW) { |
640 | btrfs_read_locked_inode(inode); | |
641 | unlock_new_inode(inode); | |
642 | } | |
2e635a27 | 643 | |
e20d96d6 CM |
644 | root_dentry = d_alloc_root(inode); |
645 | if (!root_dentry) { | |
2e635a27 CM |
646 | iput(inode); |
647 | return -ENOMEM; | |
648 | } | |
e20d96d6 CM |
649 | sb->s_root = root_dentry; |
650 | ||
2e635a27 CM |
651 | return 0; |
652 | } | |
653 | ||
d5719762 CM |
654 | static void fill_inode_item(struct btrfs_inode_item *item, |
655 | struct inode *inode) | |
656 | { | |
657 | btrfs_set_inode_uid(item, inode->i_uid); | |
658 | btrfs_set_inode_gid(item, inode->i_gid); | |
659 | btrfs_set_inode_size(item, inode->i_size); | |
660 | btrfs_set_inode_mode(item, inode->i_mode); | |
661 | btrfs_set_inode_nlink(item, inode->i_nlink); | |
662 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); | |
663 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); | |
664 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); | |
665 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); | |
666 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); | |
667 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); | |
668 | btrfs_set_inode_nblocks(item, inode->i_blocks); | |
669 | btrfs_set_inode_generation(item, inode->i_generation); | |
670 | } | |
671 | ||
4730a4bc CM |
672 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
673 | struct btrfs_root *root, | |
674 | struct inode *inode) | |
675 | { | |
676 | struct btrfs_inode_item *inode_item; | |
5caf2a00 | 677 | struct btrfs_path *path; |
4730a4bc CM |
678 | int ret; |
679 | ||
5caf2a00 CM |
680 | path = btrfs_alloc_path(); |
681 | BUG_ON(!path); | |
682 | btrfs_init_path(path); | |
d6e4a428 CM |
683 | ret = btrfs_lookup_inode(trans, root, path, |
684 | &BTRFS_I(inode)->location, 1); | |
4730a4bc CM |
685 | if (ret) { |
686 | if (ret > 0) | |
687 | ret = -ENOENT; | |
688 | goto failed; | |
689 | } | |
690 | ||
5caf2a00 CM |
691 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
692 | path->slots[0], | |
4730a4bc CM |
693 | struct btrfs_inode_item); |
694 | ||
695 | fill_inode_item(inode_item, inode); | |
5caf2a00 | 696 | btrfs_mark_buffer_dirty(path->nodes[0]); |
1b05da2e | 697 | ret = 0; |
4730a4bc | 698 | failed: |
5caf2a00 CM |
699 | btrfs_release_path(root, path); |
700 | btrfs_free_path(path); | |
1b05da2e | 701 | return ret; |
4730a4bc CM |
702 | } |
703 | ||
704 | static int btrfs_write_inode(struct inode *inode, int wait) | |
705 | { | |
d6e4a428 | 706 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4730a4bc CM |
707 | struct btrfs_trans_handle *trans; |
708 | int ret; | |
709 | ||
710 | mutex_lock(&root->fs_info->fs_mutex); | |
711 | trans = btrfs_start_transaction(root, 1); | |
712 | ret = btrfs_update_inode(trans, root, inode); | |
713 | if (wait) | |
714 | btrfs_commit_transaction(trans, root); | |
715 | else | |
716 | btrfs_end_transaction(trans, root); | |
717 | mutex_unlock(&root->fs_info->fs_mutex); | |
718 | return ret; | |
719 | } | |
720 | ||
d5719762 | 721 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
2619ba1f CM |
722 | struct btrfs_root *root, |
723 | u64 objectid, int mode) | |
d5719762 CM |
724 | { |
725 | struct inode *inode; | |
726 | struct btrfs_inode_item inode_item; | |
1b05da2e | 727 | struct btrfs_key *location; |
d5719762 | 728 | int ret; |
d5719762 | 729 | |
2619ba1f | 730 | inode = new_inode(root->fs_info->sb); |
d5719762 CM |
731 | if (!inode) |
732 | return ERR_PTR(-ENOMEM); | |
733 | ||
2619ba1f | 734 | BTRFS_I(inode)->root = root; |
d5719762 CM |
735 | |
736 | inode->i_uid = current->fsuid; | |
737 | inode->i_gid = current->fsgid; | |
738 | inode->i_mode = mode; | |
739 | inode->i_ino = objectid; | |
740 | inode->i_blocks = 0; | |
c5739bba | 741 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
d5719762 | 742 | fill_inode_item(&inode_item, inode); |
1b05da2e CM |
743 | location = &BTRFS_I(inode)->location; |
744 | location->objectid = objectid; | |
745 | location->flags = 0; | |
746 | location->offset = 0; | |
747 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
d5719762 CM |
748 | |
749 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); | |
750 | BUG_ON(ret); | |
751 | ||
752 | insert_inode_hash(inode); | |
d5719762 CM |
753 | return inode; |
754 | } | |
755 | ||
756 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | |
757 | struct dentry *dentry, struct inode *inode) | |
758 | { | |
759 | int ret; | |
d6e4a428 CM |
760 | struct btrfs_key key; |
761 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; | |
762 | key.objectid = inode->i_ino; | |
763 | key.flags = 0; | |
764 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
765 | key.offset = 0; | |
766 | ||
767 | ret = btrfs_insert_dir_item(trans, root, | |
d5719762 CM |
768 | dentry->d_name.name, dentry->d_name.len, |
769 | dentry->d_parent->d_inode->i_ino, | |
d6e4a428 | 770 | &key, 0); |
4730a4bc | 771 | if (ret == 0) { |
5f26f772 | 772 | dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2; |
d6e4a428 | 773 | ret = btrfs_update_inode(trans, root, |
4730a4bc CM |
774 | dentry->d_parent->d_inode); |
775 | } | |
d5719762 CM |
776 | return ret; |
777 | } | |
778 | ||
779 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
780 | struct dentry *dentry, struct inode *inode) | |
781 | { | |
782 | int err = btrfs_add_link(trans, dentry, inode); | |
783 | if (!err) { | |
784 | d_instantiate(dentry, inode); | |
785 | return 0; | |
786 | } | |
2c90e5d6 CM |
787 | if (err > 0) |
788 | err = -EEXIST; | |
d5719762 CM |
789 | return err; |
790 | } | |
791 | ||
792 | static int btrfs_create(struct inode *dir, struct dentry *dentry, | |
793 | int mode, struct nameidata *nd) | |
794 | { | |
795 | struct btrfs_trans_handle *trans; | |
d6e4a428 | 796 | struct btrfs_root *root = BTRFS_I(dir)->root; |
d5719762 CM |
797 | struct inode *inode; |
798 | int err; | |
134e9731 | 799 | int drop_inode = 0; |
2619ba1f | 800 | u64 objectid; |
d5719762 | 801 | |
d561c025 | 802 | mutex_lock(&root->fs_info->fs_mutex); |
d5719762 | 803 | trans = btrfs_start_transaction(root, 1); |
2619ba1f CM |
804 | |
805 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
806 | if (err) { | |
807 | err = -ENOSPC; | |
808 | goto out_unlock; | |
809 | } | |
810 | ||
811 | inode = btrfs_new_inode(trans, root, objectid, mode); | |
d5719762 CM |
812 | err = PTR_ERR(inode); |
813 | if (IS_ERR(inode)) | |
d561c025 | 814 | goto out_unlock; |
d5719762 CM |
815 | // FIXME mark the inode dirty |
816 | err = btrfs_add_nondir(trans, dentry, inode); | |
134e9731 CM |
817 | if (err) |
818 | drop_inode = 1; | |
dee26a9f CM |
819 | else { |
820 | inode->i_mapping->a_ops = &btrfs_aops; | |
821 | inode->i_fop = &btrfs_file_operations; | |
822 | inode->i_op = &btrfs_file_inode_operations; | |
823 | } | |
d5719762 | 824 | dir->i_sb->s_dirt = 1; |
d561c025 | 825 | out_unlock: |
22b0ebda | 826 | btrfs_end_transaction(trans, root); |
d561c025 | 827 | mutex_unlock(&root->fs_info->fs_mutex); |
2c90e5d6 | 828 | |
134e9731 CM |
829 | if (drop_inode) { |
830 | inode_dec_link_count(inode); | |
831 | iput(inode); | |
832 | } | |
d5719762 CM |
833 | return err; |
834 | } | |
835 | ||
f7922033 | 836 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
2619ba1f CM |
837 | struct btrfs_root *root, |
838 | u64 objectid, u64 dirid) | |
f7922033 | 839 | { |
f7922033 CM |
840 | int ret; |
841 | char buf[2]; | |
d6e4a428 CM |
842 | struct btrfs_key key; |
843 | ||
f7922033 CM |
844 | buf[0] = '.'; |
845 | buf[1] = '.'; | |
846 | ||
2619ba1f | 847 | key.objectid = objectid; |
d6e4a428 CM |
848 | key.offset = 0; |
849 | key.flags = 0; | |
850 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
851 | ||
2619ba1f | 852 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
d6e4a428 | 853 | &key, 1); |
f7922033 CM |
854 | if (ret) |
855 | goto error; | |
2619ba1f CM |
856 | key.objectid = dirid; |
857 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, | |
d6e4a428 | 858 | &key, 1); |
4730a4bc CM |
859 | if (ret) |
860 | goto error; | |
f7922033 CM |
861 | error: |
862 | return ret; | |
863 | } | |
864 | ||
865 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
866 | { | |
867 | struct inode *inode; | |
868 | struct btrfs_trans_handle *trans; | |
d6e4a428 | 869 | struct btrfs_root *root = BTRFS_I(dir)->root; |
f7922033 CM |
870 | int err = 0; |
871 | int drop_on_err = 0; | |
2619ba1f | 872 | u64 objectid; |
f7922033 CM |
873 | |
874 | mutex_lock(&root->fs_info->fs_mutex); | |
875 | trans = btrfs_start_transaction(root, 1); | |
876 | if (IS_ERR(trans)) { | |
877 | err = PTR_ERR(trans); | |
878 | goto out_unlock; | |
879 | } | |
2619ba1f CM |
880 | |
881 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
882 | if (err) { | |
883 | err = -ENOSPC; | |
884 | goto out_unlock; | |
885 | } | |
886 | ||
887 | inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode); | |
f7922033 CM |
888 | if (IS_ERR(inode)) { |
889 | err = PTR_ERR(inode); | |
890 | goto out_fail; | |
891 | } | |
892 | drop_on_err = 1; | |
893 | inode->i_op = &btrfs_dir_inode_operations; | |
894 | inode->i_fop = &btrfs_dir_file_operations; | |
895 | ||
2619ba1f CM |
896 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
897 | if (err) | |
898 | goto out_fail; | |
899 | ||
900 | inode->i_size = 6; | |
901 | err = btrfs_update_inode(trans, root, inode); | |
f7922033 CM |
902 | if (err) |
903 | goto out_fail; | |
904 | err = btrfs_add_link(trans, dentry, inode); | |
905 | if (err) | |
906 | goto out_fail; | |
907 | d_instantiate(dentry, inode); | |
f7922033 CM |
908 | drop_on_err = 0; |
909 | ||
910 | out_fail: | |
911 | btrfs_end_transaction(trans, root); | |
912 | out_unlock: | |
913 | mutex_unlock(&root->fs_info->fs_mutex); | |
914 | if (drop_on_err) | |
915 | iput(inode); | |
916 | return err; | |
917 | } | |
918 | ||
d5719762 CM |
919 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
920 | { | |
921 | struct btrfs_trans_handle *trans; | |
922 | struct btrfs_root *root; | |
923 | int ret; | |
d98237b3 | 924 | root = btrfs_sb(sb); |
df2ce34c | 925 | |
d5719762 | 926 | sb->s_dirt = 0; |
d561c025 | 927 | if (!wait) { |
7cfcc17e | 928 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
d561c025 CM |
929 | return 0; |
930 | } | |
7cfcc17e | 931 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
d561c025 | 932 | mutex_lock(&root->fs_info->fs_mutex); |
d5719762 CM |
933 | trans = btrfs_start_transaction(root, 1); |
934 | ret = btrfs_commit_transaction(trans, root); | |
935 | sb->s_dirt = 0; | |
936 | BUG_ON(ret); | |
937 | printk("btrfs sync_fs\n"); | |
d561c025 | 938 | mutex_unlock(&root->fs_info->fs_mutex); |
d5719762 CM |
939 | return 0; |
940 | } | |
941 | ||
e8f05c45 | 942 | #if 0 |
df24a2b9 CM |
943 | static int btrfs_get_block_inline(struct inode *inode, sector_t iblock, |
944 | struct buffer_head *result, int create) | |
945 | { | |
946 | struct btrfs_root *root = btrfs_sb(inode->i_sb); | |
947 | struct btrfs_path *path; | |
948 | struct btrfs_key key; | |
949 | struct btrfs_leaf *leaf; | |
950 | int num_bytes = result->b_size; | |
951 | int item_size; | |
952 | int ret; | |
953 | u64 pos; | |
954 | char *ptr; | |
955 | int copy_size; | |
956 | int err = 0; | |
957 | char *safe_ptr; | |
958 | char *data_ptr; | |
959 | ||
960 | path = btrfs_alloc_path(); | |
961 | BUG_ON(!path); | |
962 | ||
963 | WARN_ON(create); | |
964 | if (create) { | |
965 | return 0; | |
966 | } | |
967 | pos = iblock << inode->i_blkbits; | |
968 | key.objectid = inode->i_ino; | |
969 | key.flags = 0; | |
970 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); | |
971 | ptr = kmap(result->b_page); | |
972 | safe_ptr = ptr; | |
973 | ptr += (pos & (PAGE_CACHE_SIZE -1)); | |
974 | again: | |
975 | key.offset = pos; | |
976 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
977 | if (ret) { | |
978 | if (ret < 0) | |
979 | err = ret; | |
980 | else | |
981 | err = 0; | |
982 | goto out; | |
983 | } | |
984 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
985 | item_size = btrfs_item_size(leaf->items + path->slots[0]); | |
986 | copy_size = min(num_bytes, item_size); | |
987 | data_ptr = btrfs_item_ptr(leaf, path->slots[0], char); | |
988 | WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size); | |
989 | memcpy(ptr, data_ptr, copy_size); | |
990 | pos += copy_size; | |
991 | num_bytes -= copy_size; | |
992 | WARN_ON(num_bytes < 0); | |
993 | ptr += copy_size; | |
994 | btrfs_release_path(root, path); | |
995 | if (num_bytes != 0) { | |
996 | if (pos >= i_size_read(inode)) | |
997 | memset(ptr, 0, num_bytes); | |
998 | else | |
999 | goto again; | |
1000 | } | |
1001 | set_buffer_uptodate(result); | |
1002 | map_bh(result, inode->i_sb, 0); | |
1003 | err = 0; | |
1004 | out: | |
1005 | btrfs_free_path(path); | |
1006 | kunmap(result->b_page); | |
1007 | return err; | |
1008 | } | |
e8f05c45 | 1009 | #endif |
df24a2b9 | 1010 | |
75dfe396 | 1011 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
dee26a9f CM |
1012 | struct buffer_head *result, int create) |
1013 | { | |
1014 | int ret; | |
1015 | int err = 0; | |
1016 | u64 blocknr; | |
1017 | u64 extent_start = 0; | |
1018 | u64 extent_end = 0; | |
1019 | u64 objectid = inode->i_ino; | |
5caf2a00 | 1020 | struct btrfs_path *path; |
d6e4a428 | 1021 | struct btrfs_root *root = BTRFS_I(inode)->root; |
dee26a9f CM |
1022 | struct btrfs_trans_handle *trans = NULL; |
1023 | struct btrfs_file_extent_item *item; | |
1024 | struct btrfs_leaf *leaf; | |
1025 | struct btrfs_disk_key *found_key; | |
1026 | ||
5caf2a00 CM |
1027 | path = btrfs_alloc_path(); |
1028 | BUG_ON(!path); | |
1029 | btrfs_init_path(path); | |
dee26a9f CM |
1030 | if (create) |
1031 | trans = btrfs_start_transaction(root, 1); | |
1032 | ||
5caf2a00 | 1033 | ret = btrfs_lookup_file_extent(trans, root, path, |
9773a788 | 1034 | inode->i_ino, |
79b2cb1f | 1035 | iblock << inode->i_blkbits, create); |
dee26a9f | 1036 | if (ret < 0) { |
dee26a9f CM |
1037 | err = ret; |
1038 | goto out; | |
1039 | } | |
1040 | ||
1041 | if (ret != 0) { | |
5caf2a00 CM |
1042 | if (path->slots[0] == 0) { |
1043 | btrfs_release_path(root, path); | |
dee26a9f CM |
1044 | goto allocate; |
1045 | } | |
5caf2a00 | 1046 | path->slots[0]--; |
dee26a9f CM |
1047 | } |
1048 | ||
5caf2a00 | 1049 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
dee26a9f | 1050 | struct btrfs_file_extent_item); |
5caf2a00 | 1051 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
dee26a9f CM |
1052 | blocknr = btrfs_file_extent_disk_blocknr(item); |
1053 | blocknr += btrfs_file_extent_offset(item); | |
1054 | ||
48ddc6f4 CM |
1055 | /* exact match found, use it, FIXME, deal with extents |
1056 | * other than the page size | |
1057 | */ | |
dee26a9f CM |
1058 | if (ret == 0) { |
1059 | err = 0; | |
48ddc6f4 | 1060 | BUG_ON(btrfs_file_extent_disk_num_blocks(item) != 1); |
79b2cb1f CM |
1061 | if (create && |
1062 | btrfs_file_extent_generation(item) != trans->transid) { | |
48ddc6f4 CM |
1063 | struct btrfs_key ins; |
1064 | ret = btrfs_alloc_extent(trans, root, 1, | |
1065 | blocknr, (u64)-1, &ins); | |
1066 | BUG_ON(ret); | |
1067 | btrfs_set_file_extent_disk_blocknr(item, ins.objectid); | |
1068 | mark_buffer_dirty(path->nodes[0]); | |
1069 | ret = btrfs_free_extent(trans, root, | |
1070 | blocknr, 1, 0); | |
1071 | BUG_ON(ret); | |
1072 | blocknr = ins.objectid; | |
1073 | ||
1074 | } | |
dee26a9f | 1075 | map_bh(result, inode->i_sb, blocknr); |
8352d8a4 | 1076 | btrfs_map_bh_to_logical(root, result, blocknr); |
dee26a9f CM |
1077 | goto out; |
1078 | } | |
1079 | ||
1080 | /* are we inside the extent that was found? */ | |
5caf2a00 | 1081 | found_key = &leaf->items[path->slots[0]].key; |
dee26a9f CM |
1082 | if (btrfs_disk_key_objectid(found_key) != objectid || |
1083 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { | |
1084 | extent_end = 0; | |
1085 | extent_start = 0; | |
5caf2a00 | 1086 | btrfs_release_path(root, path); |
dee26a9f CM |
1087 | goto allocate; |
1088 | } | |
1089 | ||
5caf2a00 | 1090 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
9773a788 | 1091 | extent_start = extent_start >> inode->i_blkbits; |
dee26a9f CM |
1092 | extent_start += btrfs_file_extent_offset(item); |
1093 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); | |
dee26a9f CM |
1094 | if (iblock >= extent_start && iblock < extent_end) { |
1095 | err = 0; | |
8352d8a4 CM |
1096 | btrfs_map_bh_to_logical(root, result, blocknr + iblock - |
1097 | extent_start); | |
dee26a9f CM |
1098 | goto out; |
1099 | } | |
1100 | allocate: | |
1101 | /* ok, create a new extent */ | |
1102 | if (!create) { | |
1103 | err = 0; | |
1104 | goto out; | |
1105 | } | |
9773a788 CM |
1106 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
1107 | iblock << inode->i_blkbits, | |
dee26a9f CM |
1108 | 1, extent_end, &blocknr); |
1109 | if (ret) { | |
1110 | err = ret; | |
1111 | goto out; | |
1112 | } | |
f4b9aa8d CM |
1113 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
1114 | set_buffer_new(result); | |
dee26a9f CM |
1115 | map_bh(result, inode->i_sb, blocknr); |
1116 | ||
8352d8a4 | 1117 | btrfs_map_bh_to_logical(root, result, blocknr); |
dee26a9f | 1118 | out: |
5caf2a00 CM |
1119 | btrfs_release_path(root, path); |
1120 | btrfs_free_path(path); | |
dee26a9f CM |
1121 | if (trans) |
1122 | btrfs_end_transaction(trans, root); | |
75dfe396 CM |
1123 | return err; |
1124 | } | |
1125 | ||
1126 | static int btrfs_get_block(struct inode *inode, sector_t iblock, | |
1127 | struct buffer_head *result, int create) | |
1128 | { | |
1129 | int err; | |
d6e4a428 | 1130 | struct btrfs_root *root = BTRFS_I(inode)->root; |
75dfe396 | 1131 | mutex_lock(&root->fs_info->fs_mutex); |
e8f05c45 CM |
1132 | err = btrfs_get_block_lock(inode, iblock, result, create); |
1133 | // err = btrfs_get_block_inline(inode, iblock, result, create); | |
dee26a9f CM |
1134 | mutex_unlock(&root->fs_info->fs_mutex); |
1135 | return err; | |
1136 | } | |
1137 | ||
1138 | static int btrfs_prepare_write(struct file *file, struct page *page, | |
1139 | unsigned from, unsigned to) | |
1140 | { | |
6407bf6d | 1141 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
dee26a9f | 1142 | } |
75dfe396 CM |
1143 | static int btrfs_commit_write(struct file *file, struct page *page, |
1144 | unsigned from, unsigned to) | |
1145 | { | |
75dfe396 CM |
1146 | return nobh_commit_write(file, page, from, to); |
1147 | } | |
dee26a9f | 1148 | |
d561c025 CM |
1149 | static void btrfs_write_super(struct super_block *sb) |
1150 | { | |
1151 | btrfs_sync_fs(sb, 1); | |
1152 | } | |
1153 | ||
dee26a9f CM |
1154 | static int btrfs_readpage(struct file *file, struct page *page) |
1155 | { | |
1156 | return mpage_readpage(page, btrfs_get_block); | |
1157 | } | |
1158 | ||
dee26a9f CM |
1159 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
1160 | { | |
6407bf6d | 1161 | return nobh_writepage(page, btrfs_get_block, wbc); |
dee26a9f | 1162 | } |
d561c025 | 1163 | |
f4b9aa8d CM |
1164 | static void btrfs_truncate(struct inode *inode) |
1165 | { | |
d6e4a428 | 1166 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f4b9aa8d CM |
1167 | int ret; |
1168 | struct btrfs_trans_handle *trans; | |
1169 | ||
1170 | if (!S_ISREG(inode->i_mode)) | |
1171 | return; | |
1172 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
1173 | return; | |
1174 | ||
e8f05c45 | 1175 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
f4b9aa8d CM |
1176 | |
1177 | /* FIXME, add redo link to tree so we don't leak on crash */ | |
1178 | mutex_lock(&root->fs_info->fs_mutex); | |
1179 | trans = btrfs_start_transaction(root, 1); | |
1180 | ret = btrfs_truncate_in_trans(trans, root, inode); | |
1181 | BUG_ON(ret); | |
1182 | ret = btrfs_end_transaction(trans, root); | |
1183 | BUG_ON(ret); | |
1184 | mutex_unlock(&root->fs_info->fs_mutex); | |
1185 | mark_inode_dirty(inode); | |
1186 | } | |
1187 | ||
75dfe396 CM |
1188 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
1189 | struct page **prepared_pages, | |
1190 | const char __user * buf) | |
1191 | { | |
1192 | long page_fault = 0; | |
1193 | int i; | |
1194 | int offset = pos & (PAGE_CACHE_SIZE - 1); | |
1195 | ||
1196 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { | |
1197 | size_t count = min_t(size_t, | |
1198 | PAGE_CACHE_SIZE - offset, write_bytes); | |
1199 | struct page *page = prepared_pages[i]; | |
1200 | fault_in_pages_readable(buf, count); | |
1201 | ||
1202 | /* Copy data from userspace to the current page */ | |
1203 | kmap(page); | |
1204 | page_fault = __copy_from_user(page_address(page) + offset, | |
1205 | buf, count); | |
1206 | /* Flush processor's dcache for this page */ | |
1207 | flush_dcache_page(page); | |
1208 | kunmap(page); | |
1209 | buf += count; | |
1210 | write_bytes -= count; | |
1211 | ||
1212 | if (page_fault) | |
1213 | break; | |
1214 | } | |
1215 | return page_fault ? -EFAULT : 0; | |
1216 | } | |
1217 | ||
1218 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) | |
1219 | { | |
1220 | size_t i; | |
1221 | for (i = 0; i < num_pages; i++) { | |
1222 | if (!pages[i]) | |
1223 | break; | |
1224 | unlock_page(pages[i]); | |
1225 | mark_page_accessed(pages[i]); | |
1226 | page_cache_release(pages[i]); | |
1227 | } | |
1228 | } | |
1229 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, | |
1230 | struct btrfs_root *root, | |
1231 | struct file *file, | |
1232 | struct page **pages, | |
1233 | size_t num_pages, | |
1234 | loff_t pos, | |
1235 | size_t write_bytes) | |
1236 | { | |
1237 | int i; | |
1238 | int offset; | |
1239 | int err = 0; | |
1240 | int ret; | |
1241 | int this_write; | |
f254e52c | 1242 | struct inode *inode = file->f_path.dentry->d_inode; |
75dfe396 CM |
1243 | |
1244 | for (i = 0; i < num_pages; i++) { | |
1245 | offset = pos & (PAGE_CACHE_SIZE -1); | |
1246 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); | |
f254e52c CM |
1247 | /* FIXME, one block at a time */ |
1248 | ||
1249 | mutex_lock(&root->fs_info->fs_mutex); | |
1250 | trans = btrfs_start_transaction(root, 1); | |
1251 | btrfs_csum_file_block(trans, root, inode->i_ino, | |
1252 | pages[i]->index << PAGE_CACHE_SHIFT, | |
1253 | kmap(pages[i]), PAGE_CACHE_SIZE); | |
1254 | kunmap(pages[i]); | |
1255 | SetPageChecked(pages[i]); | |
1256 | ret = btrfs_end_transaction(trans, root); | |
1257 | BUG_ON(ret); | |
1258 | mutex_unlock(&root->fs_info->fs_mutex); | |
1259 | ||
75dfe396 CM |
1260 | ret = nobh_commit_write(file, pages[i], offset, |
1261 | offset + this_write); | |
1262 | pos += this_write; | |
1263 | if (ret) { | |
1264 | err = ret; | |
1265 | goto failed; | |
1266 | } | |
1267 | WARN_ON(this_write > write_bytes); | |
1268 | write_bytes -= this_write; | |
1269 | } | |
1270 | failed: | |
1271 | return err; | |
1272 | } | |
1273 | ||
1274 | static int prepare_pages(struct btrfs_trans_handle *trans, | |
1275 | struct btrfs_root *root, | |
1276 | struct file *file, | |
1277 | struct page **pages, | |
1278 | size_t num_pages, | |
1279 | loff_t pos, | |
2932f3ec CM |
1280 | unsigned long first_index, |
1281 | unsigned long last_index, | |
75dfe396 CM |
1282 | size_t write_bytes) |
1283 | { | |
1284 | int i; | |
1285 | unsigned long index = pos >> PAGE_CACHE_SHIFT; | |
1286 | struct inode *inode = file->f_path.dentry->d_inode; | |
1287 | int offset; | |
1288 | int err = 0; | |
1289 | int ret; | |
1290 | int this_write; | |
1291 | loff_t isize = i_size_read(inode); | |
1292 | ||
1293 | memset(pages, 0, num_pages * sizeof(struct page *)); | |
1294 | ||
1295 | for (i = 0; i < num_pages; i++) { | |
1296 | pages[i] = grab_cache_page(inode->i_mapping, index + i); | |
1297 | if (!pages[i]) { | |
1298 | err = -ENOMEM; | |
1299 | goto failed_release; | |
1300 | } | |
1301 | offset = pos & (PAGE_CACHE_SIZE -1); | |
1302 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); | |
2932f3ec CM |
1303 | if (!PageUptodate(pages[i]) && |
1304 | (pages[i]->index == first_index || | |
1305 | pages[i]->index == last_index) && pos < isize) { | |
1306 | ret = mpage_readpage(pages[i], btrfs_get_block); | |
1307 | BUG_ON(ret); | |
1308 | lock_page(pages[i]); | |
1309 | } | |
75dfe396 CM |
1310 | ret = nobh_prepare_write(pages[i], offset, |
1311 | offset + this_write, | |
f254e52c | 1312 | btrfs_get_block); |
75dfe396 CM |
1313 | pos += this_write; |
1314 | if (ret) { | |
1315 | err = ret; | |
1316 | goto failed_truncate; | |
1317 | } | |
1318 | WARN_ON(this_write > write_bytes); | |
1319 | write_bytes -= this_write; | |
1320 | } | |
1321 | return 0; | |
1322 | ||
1323 | failed_release: | |
1324 | btrfs_drop_pages(pages, num_pages); | |
1325 | return err; | |
1326 | ||
1327 | failed_truncate: | |
1328 | btrfs_drop_pages(pages, num_pages); | |
1329 | if (pos > isize) | |
1330 | vmtruncate(inode, isize); | |
1331 | return err; | |
1332 | } | |
1333 | ||
1334 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, | |
1335 | size_t count, loff_t *ppos) | |
1336 | { | |
1337 | loff_t pos; | |
1338 | size_t num_written = 0; | |
1339 | int err = 0; | |
1340 | int ret = 0; | |
75dfe396 | 1341 | struct inode *inode = file->f_path.dentry->d_inode; |
d6e4a428 | 1342 | struct btrfs_root *root = BTRFS_I(inode)->root; |
75dfe396 | 1343 | struct page *pages[1]; |
2932f3ec CM |
1344 | unsigned long first_index; |
1345 | unsigned long last_index; | |
75dfe396 CM |
1346 | |
1347 | if (file->f_flags & O_DIRECT) | |
1348 | return -EINVAL; | |
1349 | pos = *ppos; | |
1350 | ||
1351 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
1352 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | |
1353 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
1354 | if (err) | |
1355 | goto out; | |
1356 | if (count == 0) | |
1357 | goto out; | |
1358 | err = remove_suid(file->f_path.dentry); | |
1359 | if (err) | |
1360 | goto out; | |
1361 | file_update_time(file); | |
1362 | mutex_lock(&inode->i_mutex); | |
2932f3ec CM |
1363 | first_index = pos >> PAGE_CACHE_SHIFT; |
1364 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; | |
75dfe396 CM |
1365 | while(count > 0) { |
1366 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); | |
1367 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); | |
1368 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> | |
1369 | PAGE_CACHE_SHIFT; | |
f254e52c | 1370 | ret = prepare_pages(NULL, root, file, pages, num_pages, |
2932f3ec | 1371 | pos, first_index, last_index, write_bytes); |
75dfe396 CM |
1372 | BUG_ON(ret); |
1373 | ret = btrfs_copy_from_user(pos, num_pages, | |
1374 | write_bytes, pages, buf); | |
1375 | BUG_ON(ret); | |
1376 | ||
f254e52c | 1377 | ret = dirty_and_release_pages(NULL, root, file, pages, |
75dfe396 CM |
1378 | num_pages, pos, write_bytes); |
1379 | BUG_ON(ret); | |
1380 | btrfs_drop_pages(pages, num_pages); | |
1381 | ||
75dfe396 CM |
1382 | buf += write_bytes; |
1383 | count -= write_bytes; | |
1384 | pos += write_bytes; | |
1385 | num_written += write_bytes; | |
1386 | ||
1387 | balance_dirty_pages_ratelimited(inode->i_mapping); | |
1388 | cond_resched(); | |
1389 | } | |
1390 | mutex_unlock(&inode->i_mutex); | |
1391 | out: | |
1392 | *ppos = pos; | |
1393 | current->backing_dev_info = NULL; | |
1394 | return num_written ? num_written : err; | |
1395 | } | |
1396 | ||
e8f05c45 | 1397 | #if 0 |
df24a2b9 CM |
1398 | static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode, |
1399 | struct page *page, loff_t pos, | |
1400 | size_t offset, size_t write_bytes) | |
1401 | { | |
1402 | struct btrfs_path *path; | |
1403 | struct btrfs_trans_handle *trans; | |
1404 | struct btrfs_key key; | |
1405 | struct btrfs_leaf *leaf; | |
1406 | struct btrfs_key found_key; | |
1407 | int ret; | |
1408 | size_t copy_size = 0; | |
1409 | char *dst = NULL; | |
1410 | int err = 0; | |
1411 | size_t num_written = 0; | |
1412 | ||
1413 | path = btrfs_alloc_path(); | |
1414 | BUG_ON(!path); | |
1415 | mutex_lock(&root->fs_info->fs_mutex); | |
1416 | trans = btrfs_start_transaction(root, 1); | |
1417 | key.objectid = inode->i_ino; | |
1418 | key.flags = 0; | |
1419 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); | |
1420 | ||
1421 | again: | |
1422 | key.offset = pos; | |
1423 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
1424 | if (ret < 0) { | |
1425 | err = ret; | |
1426 | goto out; | |
1427 | } | |
1428 | if (ret == 0) { | |
1429 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
1430 | btrfs_disk_key_to_cpu(&found_key, | |
1431 | &leaf->items[path->slots[0]].key); | |
1432 | copy_size = btrfs_item_size(leaf->items + path->slots[0]); | |
1433 | dst = btrfs_item_ptr(leaf, path->slots[0], char); | |
1434 | copy_size = min(write_bytes, copy_size); | |
1435 | goto copyit; | |
1436 | } else { | |
1437 | int slot = path->slots[0]; | |
1438 | if (slot > 0) { | |
1439 | slot--; | |
1440 | } | |
1441 | // FIXME find max key | |
1442 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
1443 | btrfs_disk_key_to_cpu(&found_key, | |
1444 | &leaf->items[slot].key); | |
1445 | if (found_key.objectid != inode->i_ino) | |
1446 | goto insert; | |
1447 | if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY) | |
1448 | goto insert; | |
1449 | copy_size = btrfs_item_size(leaf->items + slot); | |
1450 | if (found_key.offset + copy_size <= pos) | |
1451 | goto insert; | |
1452 | dst = btrfs_item_ptr(leaf, path->slots[0], char); | |
1453 | dst += pos - found_key.offset; | |
1454 | copy_size = copy_size - (pos - found_key.offset); | |
1455 | BUG_ON(copy_size < 0); | |
1456 | copy_size = min(write_bytes, copy_size); | |
1457 | WARN_ON(copy_size == 0); | |
1458 | goto copyit; | |
1459 | } | |
1460 | insert: | |
1461 | btrfs_release_path(root, path); | |
d4dbff95 CM |
1462 | copy_size = min(write_bytes, |
1463 | (size_t)BTRFS_LEAF_DATA_SIZE(root) - | |
1464 | sizeof(struct btrfs_item) * 4); | |
df24a2b9 CM |
1465 | ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size); |
1466 | BUG_ON(ret); | |
1467 | dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
1468 | path->slots[0], char); | |
1469 | copyit: | |
1470 | WARN_ON(copy_size == 0); | |
1471 | WARN_ON(dst + copy_size > | |
1472 | btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
1473 | path->slots[0], char) + | |
1474 | btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items + | |
1475 | path->slots[0])); | |
1476 | btrfs_memcpy(root, path->nodes[0]->b_data, dst, | |
1477 | page_address(page) + offset, copy_size); | |
1478 | mark_buffer_dirty(path->nodes[0]); | |
1479 | btrfs_release_path(root, path); | |
1480 | pos += copy_size; | |
1481 | offset += copy_size; | |
1482 | num_written += copy_size; | |
1483 | write_bytes -= copy_size; | |
1484 | if (write_bytes) | |
1485 | goto again; | |
1486 | out: | |
1487 | btrfs_free_path(path); | |
1488 | ret = btrfs_end_transaction(trans, root); | |
1489 | BUG_ON(ret); | |
1490 | mutex_unlock(&root->fs_info->fs_mutex); | |
1491 | return num_written ? num_written : err; | |
1492 | } | |
1493 | ||
1494 | static ssize_t btrfs_file_inline_write(struct file *file, | |
1495 | const char __user *buf, | |
1496 | size_t count, loff_t *ppos) | |
1497 | { | |
1498 | loff_t pos; | |
1499 | size_t num_written = 0; | |
1500 | int err = 0; | |
1501 | int ret = 0; | |
1502 | struct inode *inode = file->f_path.dentry->d_inode; | |
d6e4a428 | 1503 | struct btrfs_root *root = BTRFS_I(inode)->root; |
df24a2b9 CM |
1504 | unsigned long page_index; |
1505 | ||
1506 | if (file->f_flags & O_DIRECT) | |
1507 | return -EINVAL; | |
1508 | pos = *ppos; | |
1509 | ||
1510 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
1511 | current->backing_dev_info = inode->i_mapping->backing_dev_info; | |
1512 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
1513 | if (err) | |
1514 | goto out; | |
1515 | if (count == 0) | |
1516 | goto out; | |
1517 | err = remove_suid(file->f_path.dentry); | |
1518 | if (err) | |
1519 | goto out; | |
1520 | file_update_time(file); | |
1521 | mutex_lock(&inode->i_mutex); | |
1522 | while(count > 0) { | |
1523 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); | |
1524 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); | |
1525 | struct page *page; | |
1526 | ||
1527 | page_index = pos >> PAGE_CACHE_SHIFT; | |
1528 | page = grab_cache_page(inode->i_mapping, page_index); | |
1529 | if (!PageUptodate(page)) { | |
1530 | ret = mpage_readpage(page, btrfs_get_block); | |
1531 | BUG_ON(ret); | |
1532 | lock_page(page); | |
1533 | } | |
1534 | ret = btrfs_copy_from_user(pos, 1, | |
1535 | write_bytes, &page, buf); | |
1536 | BUG_ON(ret); | |
1537 | write_bytes = inline_one_page(root, inode, page, pos, | |
1538 | offset, write_bytes); | |
1539 | SetPageUptodate(page); | |
1540 | if (write_bytes > 0 && pos + write_bytes > inode->i_size) { | |
1541 | i_size_write(inode, pos + write_bytes); | |
1542 | mark_inode_dirty(inode); | |
1543 | } | |
1544 | page_cache_release(page); | |
1545 | unlock_page(page); | |
1546 | if (write_bytes < 0) | |
1547 | goto out_unlock; | |
1548 | buf += write_bytes; | |
1549 | count -= write_bytes; | |
1550 | pos += write_bytes; | |
1551 | num_written += write_bytes; | |
1552 | ||
1553 | balance_dirty_pages_ratelimited(inode->i_mapping); | |
1554 | cond_resched(); | |
1555 | } | |
1556 | out_unlock: | |
1557 | mutex_unlock(&inode->i_mutex); | |
1558 | out: | |
1559 | *ppos = pos; | |
1560 | current->backing_dev_info = NULL; | |
1561 | return num_written ? num_written : err; | |
1562 | } | |
e8f05c45 | 1563 | #endif |
df24a2b9 | 1564 | |
f254e52c CM |
1565 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
1566 | unsigned long offset, unsigned long size) | |
1567 | { | |
1568 | char *kaddr; | |
1569 | unsigned long left, count = desc->count; | |
d6e4a428 | 1570 | struct inode *inode = page->mapping->host; |
f254e52c CM |
1571 | |
1572 | if (size > count) | |
1573 | size = count; | |
1574 | ||
1575 | if (!PageChecked(page)) { | |
1576 | /* FIXME, do it per block */ | |
d6e4a428 | 1577 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f254e52c CM |
1578 | int ret = btrfs_csum_verify_file_block(root, |
1579 | page->mapping->host->i_ino, | |
1580 | page->index << PAGE_CACHE_SHIFT, | |
1581 | kmap(page), PAGE_CACHE_SIZE); | |
1582 | if (ret) { | |
1583 | printk("failed to verify ino %lu page %lu\n", | |
1584 | page->mapping->host->i_ino, | |
1585 | page->index); | |
1586 | memset(page_address(page), 0, PAGE_CACHE_SIZE); | |
1587 | } | |
1588 | SetPageChecked(page); | |
1589 | kunmap(page); | |
1590 | } | |
1591 | /* | |
1592 | * Faults on the destination of a read are common, so do it before | |
1593 | * taking the kmap. | |
1594 | */ | |
1595 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { | |
1596 | kaddr = kmap_atomic(page, KM_USER0); | |
1597 | left = __copy_to_user_inatomic(desc->arg.buf, | |
1598 | kaddr + offset, size); | |
1599 | kunmap_atomic(kaddr, KM_USER0); | |
1600 | if (left == 0) | |
1601 | goto success; | |
1602 | } | |
1603 | ||
1604 | /* Do it the slow way */ | |
1605 | kaddr = kmap(page); | |
1606 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); | |
1607 | kunmap(page); | |
1608 | ||
1609 | if (left) { | |
1610 | size -= left; | |
1611 | desc->error = -EFAULT; | |
1612 | } | |
1613 | success: | |
1614 | desc->count = count - size; | |
1615 | desc->written += size; | |
1616 | desc->arg.buf += size; | |
1617 | return size; | |
1618 | } | |
1619 | ||
1620 | /** | |
1621 | * btrfs_file_aio_read - filesystem read routine | |
1622 | * @iocb: kernel I/O control block | |
1623 | * @iov: io vector request | |
1624 | * @nr_segs: number of segments in the iovec | |
1625 | * @pos: current file position | |
1626 | */ | |
1627 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, | |
1628 | unsigned long nr_segs, loff_t pos) | |
1629 | { | |
1630 | struct file *filp = iocb->ki_filp; | |
1631 | ssize_t retval; | |
1632 | unsigned long seg; | |
1633 | size_t count; | |
1634 | loff_t *ppos = &iocb->ki_pos; | |
1635 | ||
1636 | count = 0; | |
1637 | for (seg = 0; seg < nr_segs; seg++) { | |
1638 | const struct iovec *iv = &iov[seg]; | |
1639 | ||
1640 | /* | |
1641 | * If any segment has a negative length, or the cumulative | |
1642 | * length ever wraps negative then return -EINVAL. | |
1643 | */ | |
1644 | count += iv->iov_len; | |
1645 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) | |
1646 | return -EINVAL; | |
1647 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) | |
1648 | continue; | |
1649 | if (seg == 0) | |
1650 | return -EFAULT; | |
1651 | nr_segs = seg; | |
1652 | count -= iv->iov_len; /* This segment is no good */ | |
1653 | break; | |
1654 | } | |
1655 | retval = 0; | |
1656 | if (count) { | |
1657 | for (seg = 0; seg < nr_segs; seg++) { | |
1658 | read_descriptor_t desc; | |
1659 | ||
1660 | desc.written = 0; | |
1661 | desc.arg.buf = iov[seg].iov_base; | |
1662 | desc.count = iov[seg].iov_len; | |
1663 | if (desc.count == 0) | |
1664 | continue; | |
1665 | desc.error = 0; | |
1666 | do_generic_file_read(filp, ppos, &desc, | |
1667 | btrfs_read_actor); | |
1668 | retval += desc.written; | |
1669 | if (desc.error) { | |
1670 | retval = retval ?: desc.error; | |
1671 | break; | |
1672 | } | |
1673 | } | |
1674 | } | |
1675 | return retval; | |
1676 | } | |
1677 | ||
2619ba1f CM |
1678 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
1679 | { | |
1680 | struct btrfs_trans_handle *trans; | |
1681 | struct btrfs_key key; | |
1682 | struct btrfs_root_item root_item; | |
1683 | struct btrfs_inode_item *inode_item; | |
1684 | struct buffer_head *subvol; | |
1685 | struct btrfs_leaf *leaf; | |
1686 | struct btrfs_root *new_root; | |
1687 | struct inode *inode; | |
1688 | int ret; | |
1689 | u64 objectid; | |
1690 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
1691 | ||
1692 | mutex_lock(&root->fs_info->fs_mutex); | |
1693 | trans = btrfs_start_transaction(root, 1); | |
1694 | BUG_ON(!trans); | |
1695 | ||
1696 | subvol = btrfs_alloc_free_block(trans, root); | |
1697 | leaf = btrfs_buffer_leaf(subvol); | |
1698 | btrfs_set_header_nritems(&leaf->header, 0); | |
1699 | btrfs_set_header_level(&leaf->header, 0); | |
7eccb903 | 1700 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
2619ba1f CM |
1701 | btrfs_set_header_generation(&leaf->header, trans->transid); |
1702 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, | |
1703 | sizeof(leaf->header.fsid)); | |
1704 | ||
1705 | inode_item = &root_item.inode; | |
1706 | memset(inode_item, 0, sizeof(*inode_item)); | |
1707 | btrfs_set_inode_generation(inode_item, 1); | |
1708 | btrfs_set_inode_size(inode_item, 3); | |
1709 | btrfs_set_inode_nlink(inode_item, 1); | |
1710 | btrfs_set_inode_nblocks(inode_item, 1); | |
1711 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); | |
1712 | ||
7eccb903 | 1713 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
2619ba1f CM |
1714 | btrfs_set_root_refs(&root_item, 1); |
1715 | ||
1716 | mark_buffer_dirty(subvol); | |
1717 | brelse(subvol); | |
1718 | subvol = NULL; | |
1719 | ||
1720 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, | |
1721 | 0, &objectid); | |
1722 | BUG_ON(ret); | |
1723 | ||
1724 | btrfs_set_root_dirid(&root_item, new_dirid); | |
1725 | ||
1726 | key.objectid = objectid; | |
1727 | key.offset = 1; | |
1728 | key.flags = 0; | |
1729 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
1730 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
1731 | &root_item); | |
1732 | BUG_ON(ret); | |
1733 | ||
1734 | /* | |
1735 | * insert the directory item | |
1736 | */ | |
1737 | key.offset = (u64)-1; | |
1738 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
1739 | name, namelen, | |
1740 | root->fs_info->sb->s_root->d_inode->i_ino, | |
1741 | &key, 0); | |
1742 | BUG_ON(ret); | |
1743 | ||
1744 | ret = btrfs_commit_transaction(trans, root); | |
1745 | BUG_ON(ret); | |
1746 | ||
1747 | new_root = btrfs_read_fs_root(root->fs_info, &key); | |
1748 | BUG_ON(!new_root); | |
1749 | ||
1750 | trans = btrfs_start_transaction(new_root, 1); | |
1751 | BUG_ON(!trans); | |
1752 | ||
1753 | inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700); | |
1754 | inode->i_op = &btrfs_dir_inode_operations; | |
1755 | inode->i_fop = &btrfs_dir_file_operations; | |
1756 | ||
1757 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); | |
1758 | BUG_ON(ret); | |
1759 | ||
1760 | inode->i_nlink = 1; | |
1761 | inode->i_size = 6; | |
1762 | ret = btrfs_update_inode(trans, new_root, inode); | |
1763 | BUG_ON(ret); | |
1764 | ||
1765 | ret = btrfs_commit_transaction(trans, new_root); | |
1766 | BUG_ON(ret); | |
1767 | ||
1768 | iput(inode); | |
1769 | ||
1770 | mutex_unlock(&root->fs_info->fs_mutex); | |
1771 | return 0; | |
1772 | } | |
1773 | ||
c5739bba CM |
1774 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
1775 | { | |
1776 | struct btrfs_trans_handle *trans; | |
1777 | struct btrfs_key key; | |
1778 | struct btrfs_root_item new_root_item; | |
1779 | int ret; | |
1780 | u64 objectid; | |
1781 | ||
2619ba1f CM |
1782 | if (!root->ref_cows) |
1783 | return -EINVAL; | |
1784 | ||
c5739bba CM |
1785 | mutex_lock(&root->fs_info->fs_mutex); |
1786 | trans = btrfs_start_transaction(root, 1); | |
1787 | BUG_ON(!trans); | |
1788 | ||
1789 | ret = btrfs_update_inode(trans, root, root->inode); | |
1790 | BUG_ON(ret); | |
1791 | ||
1b05da2e CM |
1792 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
1793 | 0, &objectid); | |
c5739bba CM |
1794 | BUG_ON(ret); |
1795 | ||
c5739bba CM |
1796 | memcpy(&new_root_item, &root->root_item, |
1797 | sizeof(new_root_item)); | |
1798 | ||
c5739bba CM |
1799 | key.objectid = objectid; |
1800 | key.offset = 1; | |
1801 | key.flags = 0; | |
1802 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
7eccb903 | 1803 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
c5739bba CM |
1804 | |
1805 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
1806 | &new_root_item); | |
1807 | BUG_ON(ret); | |
1808 | ||
1809 | printk("adding snapshot name %.*s root %Lu %Lu %u\n", namelen, name, key.objectid, key.offset, key.flags); | |
1810 | ||
1811 | /* | |
1812 | * insert the directory item | |
1813 | */ | |
1814 | key.offset = (u64)-1; | |
1815 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
1816 | name, namelen, | |
1817 | root->fs_info->sb->s_root->d_inode->i_ino, | |
1818 | &key, 0); | |
1819 | ||
1820 | BUG_ON(ret); | |
1821 | ||
1822 | ret = btrfs_inc_root_ref(trans, root); | |
1823 | BUG_ON(ret); | |
1824 | ||
1825 | ret = btrfs_commit_transaction(trans, root); | |
1826 | BUG_ON(ret); | |
1827 | mutex_unlock(&root->fs_info->fs_mutex); | |
1828 | return 0; | |
1829 | } | |
1830 | ||
8352d8a4 CM |
1831 | static int add_disk(struct btrfs_root *root, char *name, int namelen) |
1832 | { | |
1833 | struct block_device *bdev; | |
1834 | struct btrfs_path *path; | |
1835 | struct super_block *sb = root->fs_info->sb; | |
1836 | struct btrfs_root *dev_root = root->fs_info->dev_root; | |
1837 | struct btrfs_trans_handle *trans; | |
1838 | struct btrfs_device_item *dev_item; | |
1839 | struct btrfs_key key; | |
1840 | u16 item_size; | |
1841 | u64 num_blocks; | |
1842 | u64 new_blocks; | |
b4100d64 | 1843 | u64 device_id; |
8352d8a4 | 1844 | int ret; |
b4100d64 | 1845 | |
8352d8a4 CM |
1846 | printk("adding disk %s\n", name); |
1847 | path = btrfs_alloc_path(); | |
1848 | if (!path) | |
1849 | return -ENOMEM; | |
1850 | num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super); | |
1851 | bdev = open_bdev_excl(name, O_RDWR, sb); | |
1852 | if (IS_ERR(bdev)) { | |
1853 | ret = PTR_ERR(bdev); | |
1854 | printk("open bdev excl failed ret %d\n", ret); | |
1855 | goto out_nolock; | |
1856 | } | |
1857 | set_blocksize(bdev, sb->s_blocksize); | |
1858 | new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits; | |
1859 | key.objectid = num_blocks; | |
1860 | key.offset = new_blocks; | |
1861 | key.flags = 0; | |
1862 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); | |
1863 | ||
1864 | mutex_lock(&dev_root->fs_info->fs_mutex); | |
1865 | trans = btrfs_start_transaction(dev_root, 1); | |
1866 | item_size = sizeof(*dev_item) + namelen; | |
1867 | printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size); | |
1868 | ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size); | |
1869 | if (ret) { | |
1870 | printk("insert failed %d\n", ret); | |
1871 | close_bdev_excl(bdev); | |
1872 | if (ret > 0) | |
1873 | ret = -EEXIST; | |
1874 | goto out; | |
1875 | } | |
1876 | dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | |
1877 | path->slots[0], struct btrfs_device_item); | |
1878 | btrfs_set_device_pathlen(dev_item, namelen); | |
1879 | memcpy(dev_item + 1, name, namelen); | |
b4100d64 CM |
1880 | |
1881 | device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1; | |
1882 | btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id); | |
1883 | btrfs_set_device_id(dev_item, device_id); | |
8352d8a4 CM |
1884 | mark_buffer_dirty(path->nodes[0]); |
1885 | ||
b4100d64 CM |
1886 | ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks, |
1887 | new_blocks); | |
8352d8a4 CM |
1888 | |
1889 | if (!ret) { | |
1890 | btrfs_set_super_total_blocks(root->fs_info->disk_super, | |
1891 | num_blocks + new_blocks); | |
1892 | i_size_write(root->fs_info->btree_inode, | |
1893 | (num_blocks + new_blocks) << | |
1894 | root->fs_info->btree_inode->i_blkbits); | |
1895 | } | |
1896 | ||
1897 | out: | |
1898 | ret = btrfs_commit_transaction(trans, dev_root); | |
1899 | BUG_ON(ret); | |
1900 | mutex_unlock(&root->fs_info->fs_mutex); | |
1901 | out_nolock: | |
1902 | btrfs_free_path(path); | |
1903 | ||
1904 | return ret; | |
1905 | } | |
1906 | ||
c5739bba CM |
1907 | static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
1908 | cmd, unsigned long arg) | |
1909 | { | |
1910 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1911 | struct btrfs_ioctl_vol_args vol_args; | |
8352d8a4 | 1912 | int ret = 0; |
c5739bba | 1913 | int namelen; |
2619ba1f CM |
1914 | struct btrfs_path *path; |
1915 | u64 root_dirid; | |
c5739bba | 1916 | |
c5739bba CM |
1917 | switch (cmd) { |
1918 | case BTRFS_IOC_SNAP_CREATE: | |
1919 | if (copy_from_user(&vol_args, | |
1920 | (struct btrfs_ioctl_vol_args __user *)arg, | |
1921 | sizeof(vol_args))) | |
1922 | return -EFAULT; | |
1923 | namelen = strlen(vol_args.name); | |
1924 | if (namelen > BTRFS_VOL_NAME_MAX) | |
1925 | return -EINVAL; | |
2619ba1f CM |
1926 | path = btrfs_alloc_path(); |
1927 | if (!path) | |
1928 | return -ENOMEM; | |
2d13d8d0 | 1929 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
2619ba1f CM |
1930 | mutex_lock(&root->fs_info->fs_mutex); |
1931 | ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, | |
1932 | path, root_dirid, | |
1933 | vol_args.name, namelen, 0); | |
1934 | mutex_unlock(&root->fs_info->fs_mutex); | |
2d13d8d0 | 1935 | btrfs_free_path(path); |
2619ba1f CM |
1936 | if (ret == 0) |
1937 | return -EEXIST; | |
1938 | ||
1939 | if (root == root->fs_info->tree_root) | |
1940 | ret = create_subvol(root, vol_args.name, namelen); | |
1941 | else | |
1942 | ret = create_snapshot(root, vol_args.name, namelen); | |
c5739bba CM |
1943 | WARN_ON(ret); |
1944 | break; | |
8352d8a4 CM |
1945 | case BTRFS_IOC_ADD_DISK: |
1946 | if (copy_from_user(&vol_args, | |
1947 | (struct btrfs_ioctl_vol_args __user *)arg, | |
1948 | sizeof(vol_args))) | |
1949 | return -EFAULT; | |
1950 | namelen = strlen(vol_args.name); | |
1951 | if (namelen > BTRFS_VOL_NAME_MAX) | |
1952 | return -EINVAL; | |
1953 | vol_args.name[namelen] = '\0'; | |
1954 | ret = add_disk(root, vol_args.name, namelen); | |
1955 | break; | |
c5739bba CM |
1956 | default: |
1957 | return -ENOTTY; | |
1958 | } | |
8352d8a4 | 1959 | return ret; |
c5739bba CM |
1960 | } |
1961 | ||
2c90e5d6 CM |
1962 | static struct kmem_cache *btrfs_inode_cachep; |
1963 | struct kmem_cache *btrfs_trans_handle_cachep; | |
1964 | struct kmem_cache *btrfs_transaction_cachep; | |
1965 | struct kmem_cache *btrfs_bit_radix_cachep; | |
1966 | struct kmem_cache *btrfs_path_cachep; | |
1967 | ||
1968 | /* | |
1969 | * Called inside transaction, so use GFP_NOFS | |
1970 | */ | |
1971 | static struct inode *btrfs_alloc_inode(struct super_block *sb) | |
1972 | { | |
1973 | struct btrfs_inode *ei; | |
1974 | ||
1975 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
1976 | if (!ei) | |
1977 | return NULL; | |
2c90e5d6 CM |
1978 | return &ei->vfs_inode; |
1979 | } | |
1980 | ||
1981 | static void btrfs_destroy_inode(struct inode *inode) | |
1982 | { | |
2c90e5d6 | 1983 | WARN_ON(!list_empty(&inode->i_dentry)); |
2c90e5d6 CM |
1984 | WARN_ON(inode->i_data.nrpages); |
1985 | ||
2c90e5d6 CM |
1986 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
1987 | } | |
1988 | ||
1989 | static void init_once(void * foo, struct kmem_cache * cachep, | |
1990 | unsigned long flags) | |
1991 | { | |
1992 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
1993 | ||
1994 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
1995 | SLAB_CTOR_CONSTRUCTOR) { | |
1996 | inode_init_once(&ei->vfs_inode); | |
1997 | } | |
1998 | } | |
1999 | ||
2000 | static int init_inodecache(void) | |
2001 | { | |
2002 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", | |
2003 | sizeof(struct btrfs_inode), | |
2004 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2005 | SLAB_MEM_SPREAD), | |
2006 | init_once, NULL); | |
2007 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", | |
2008 | sizeof(struct btrfs_trans_handle), | |
2009 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2010 | SLAB_MEM_SPREAD), | |
2011 | NULL, NULL); | |
2012 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", | |
2013 | sizeof(struct btrfs_transaction), | |
2014 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2015 | SLAB_MEM_SPREAD), | |
2016 | NULL, NULL); | |
2017 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", | |
2018 | sizeof(struct btrfs_transaction), | |
2019 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2020 | SLAB_MEM_SPREAD), | |
2021 | NULL, NULL); | |
2022 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", | |
2023 | 256, | |
2024 | 0, (SLAB_RECLAIM_ACCOUNT| | |
2025 | SLAB_MEM_SPREAD | | |
2026 | SLAB_DESTROY_BY_RCU), | |
2027 | NULL, NULL); | |
2028 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || | |
2029 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) | |
2030 | return -ENOMEM; | |
2031 | return 0; | |
2032 | } | |
2033 | ||
2034 | static void destroy_inodecache(void) | |
2035 | { | |
2036 | kmem_cache_destroy(btrfs_inode_cachep); | |
2037 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
2038 | kmem_cache_destroy(btrfs_transaction_cachep); | |
2039 | kmem_cache_destroy(btrfs_bit_radix_cachep); | |
2040 | kmem_cache_destroy(btrfs_path_cachep); | |
2041 | } | |
2042 | ||
2e635a27 CM |
2043 | static int btrfs_get_sb(struct file_system_type *fs_type, |
2044 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | |
2045 | { | |
2046 | return get_sb_bdev(fs_type, flags, dev_name, data, | |
2047 | btrfs_fill_super, mnt); | |
2048 | } | |
2049 | ||
2050 | static struct file_system_type btrfs_fs_type = { | |
2051 | .owner = THIS_MODULE, | |
2052 | .name = "btrfs", | |
2053 | .get_sb = btrfs_get_sb, | |
2054 | .kill_sb = kill_block_super, | |
2055 | .fs_flags = FS_REQUIRES_DEV, | |
2056 | }; | |
2057 | ||
e20d96d6 CM |
2058 | static struct super_operations btrfs_super_ops = { |
2059 | .statfs = simple_statfs, | |
134e9731 | 2060 | .delete_inode = btrfs_delete_inode, |
e20d96d6 CM |
2061 | .put_super = btrfs_put_super, |
2062 | .read_inode = btrfs_read_locked_inode, | |
d5719762 CM |
2063 | .write_super = btrfs_write_super, |
2064 | .sync_fs = btrfs_sync_fs, | |
4730a4bc | 2065 | .write_inode = btrfs_write_inode, |
2c90e5d6 CM |
2066 | .alloc_inode = btrfs_alloc_inode, |
2067 | .destroy_inode = btrfs_destroy_inode, | |
e20d96d6 CM |
2068 | }; |
2069 | ||
2070 | static struct inode_operations btrfs_dir_inode_operations = { | |
2071 | .lookup = btrfs_lookup, | |
d5719762 | 2072 | .create = btrfs_create, |
134e9731 | 2073 | .unlink = btrfs_unlink, |
f7922033 | 2074 | .mkdir = btrfs_mkdir, |
5f443fd2 | 2075 | .rmdir = btrfs_rmdir, |
e20d96d6 CM |
2076 | }; |
2077 | ||
d6e4a428 CM |
2078 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
2079 | .lookup = btrfs_lookup, | |
2080 | }; | |
2081 | ||
e20d96d6 CM |
2082 | static struct file_operations btrfs_dir_file_operations = { |
2083 | .llseek = generic_file_llseek, | |
2084 | .read = generic_read_dir, | |
2085 | .readdir = btrfs_readdir, | |
c5739bba | 2086 | .ioctl = btrfs_ioctl, |
e20d96d6 CM |
2087 | }; |
2088 | ||
dee26a9f CM |
2089 | static struct address_space_operations btrfs_aops = { |
2090 | .readpage = btrfs_readpage, | |
dee26a9f CM |
2091 | .writepage = btrfs_writepage, |
2092 | .sync_page = block_sync_page, | |
2093 | .prepare_write = btrfs_prepare_write, | |
75dfe396 | 2094 | .commit_write = btrfs_commit_write, |
dee26a9f CM |
2095 | }; |
2096 | ||
2097 | static struct inode_operations btrfs_file_inode_operations = { | |
f4b9aa8d | 2098 | .truncate = btrfs_truncate, |
dee26a9f CM |
2099 | }; |
2100 | ||
2101 | static struct file_operations btrfs_file_operations = { | |
2102 | .llseek = generic_file_llseek, | |
2103 | .read = do_sync_read, | |
e8f05c45 CM |
2104 | .aio_read = btrfs_file_aio_read, |
2105 | .write = btrfs_file_write, | |
dee26a9f CM |
2106 | .mmap = generic_file_mmap, |
2107 | .open = generic_file_open, | |
c5739bba | 2108 | .ioctl = btrfs_ioctl, |
dee26a9f | 2109 | }; |
e20d96d6 | 2110 | |
2e635a27 CM |
2111 | static int __init init_btrfs_fs(void) |
2112 | { | |
2c90e5d6 | 2113 | int err; |
2e635a27 | 2114 | printk("btrfs loaded!\n"); |
2c90e5d6 CM |
2115 | err = init_inodecache(); |
2116 | if (err) | |
2117 | return err; | |
d6e4a428 CM |
2118 | kset_set_kset_s(&btrfs_subsys, fs_subsys); |
2119 | err = subsystem_register(&btrfs_subsys); | |
2120 | if (err) | |
2121 | goto out; | |
2e635a27 | 2122 | return register_filesystem(&btrfs_fs_type); |
d6e4a428 CM |
2123 | out: |
2124 | destroy_inodecache(); | |
2125 | return err; | |
2e635a27 CM |
2126 | } |
2127 | ||
2128 | static void __exit exit_btrfs_fs(void) | |
2129 | { | |
2c90e5d6 | 2130 | destroy_inodecache(); |
2e635a27 | 2131 | unregister_filesystem(&btrfs_fs_type); |
d6e4a428 | 2132 | subsystem_unregister(&btrfs_subsys); |
2e635a27 CM |
2133 | printk("btrfs unloaded\n"); |
2134 | } | |
2135 | ||
2136 | module_init(init_btrfs_fs) | |
2137 | module_exit(exit_btrfs_fs) | |
2138 | ||
2139 | MODULE_LICENSE("GPL"); |