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