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