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