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