Btrfs: still corruption hunting
[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 "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "btrfs_inode.h"
18
19 #define BTRFS_SUPER_MAGIC 0x9123682E
20
21 static struct inode_operations btrfs_dir_inode_operations;
22 static struct super_operations btrfs_super_ops;
23 static struct file_operations btrfs_dir_file_operations;
24 static struct inode_operations btrfs_file_inode_operations;
25 static struct address_space_operations btrfs_aops;
26 static struct file_operations btrfs_file_operations;
27
28 static int check_inode(struct inode *inode)
29 {
30         struct btrfs_inode *ei = BTRFS_I(inode);
31         WARN_ON(ei->magic != 0xDEADBEEF);
32         WARN_ON(ei->magic2 != 0xDEADBEAF);
33         return 0;
34 }
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_sb(inode->i_sb);
41         int ret;
42
43         btrfs_init_path(&path);
44         mutex_lock(&root->fs_info->fs_mutex);
45
46         check_inode(inode);
47         ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0);
48         if (ret) {
49                 btrfs_release_path(root, &path);
50                 mutex_unlock(&root->fs_info->fs_mutex);
51                 make_bad_inode(inode);
52                 return;
53         }
54         check_inode(inode);
55         inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
56                                   path.slots[0],
57                                   struct btrfs_inode_item);
58
59         inode->i_mode = btrfs_inode_mode(inode_item);
60         inode->i_nlink = btrfs_inode_nlink(inode_item);
61         inode->i_uid = btrfs_inode_uid(inode_item);
62         inode->i_gid = btrfs_inode_gid(inode_item);
63         inode->i_size = btrfs_inode_size(inode_item);
64         inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
65         inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
66         inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
67         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
68         inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
69         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
70         inode->i_blocks = btrfs_inode_nblocks(inode_item);
71         inode->i_generation = btrfs_inode_generation(inode_item);
72         btrfs_release_path(root, &path);
73         mutex_unlock(&root->fs_info->fs_mutex);
74         check_inode(inode);
75         switch (inode->i_mode & S_IFMT) {
76 #if 0
77         default:
78                 init_special_inode(inode, inode->i_mode,
79                                    btrfs_inode_rdev(inode_item));
80                 break;
81 #endif
82         case S_IFREG:
83                 inode->i_mapping->a_ops = &btrfs_aops;
84                 inode->i_fop = &btrfs_file_operations;
85                 inode->i_op = &btrfs_file_inode_operations;
86                 break;
87         case S_IFDIR:
88                 inode->i_op = &btrfs_dir_inode_operations;
89                 inode->i_fop = &btrfs_dir_file_operations;
90                 break;
91         case S_IFLNK:
92                 // inode->i_op = &page_symlink_inode_operations;
93                 break;
94         }
95         check_inode(inode);
96         return;
97 }
98
99 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
100                               struct btrfs_root *root,
101                               struct inode *dir,
102                               struct dentry *dentry)
103 {
104         struct btrfs_path path;
105         const char *name = dentry->d_name.name;
106         int name_len = dentry->d_name.len;
107         int ret;
108         u64 objectid;
109         struct btrfs_dir_item *di;
110
111         btrfs_init_path(&path);
112         ret = btrfs_lookup_dir_item(trans, root, &path, dir->i_ino,
113                                     name, name_len, -1);
114         if (ret < 0)
115                 goto err;
116         if (ret > 0) {
117                 ret = -ENOENT;
118                 goto err;
119         }
120         di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
121                             struct btrfs_dir_item);
122         objectid = btrfs_dir_objectid(di);
123
124         ret = btrfs_del_item(trans, root, &path);
125         BUG_ON(ret);
126         dentry->d_inode->i_ctime = dir->i_ctime;
127 err:
128         btrfs_release_path(root, &path);
129         if (ret == 0)
130                 inode_dec_link_count(dentry->d_inode);
131         return ret;
132 }
133
134 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
135 {
136         struct btrfs_root *root;
137         struct btrfs_trans_handle *trans;
138         int ret;
139
140         root = btrfs_sb(dir->i_sb);
141         mutex_lock(&root->fs_info->fs_mutex);
142         trans = btrfs_start_transaction(root, 1);
143         ret = btrfs_unlink_trans(trans, root, dir, dentry);
144         btrfs_end_transaction(trans, root);
145         mutex_unlock(&root->fs_info->fs_mutex);
146         return ret;
147 }
148
149 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
150 {
151         struct inode *inode = dentry->d_inode;
152         int err;
153         int ret;
154         struct btrfs_root *root = btrfs_sb(dir->i_sb);
155         struct btrfs_path path;
156         struct btrfs_key key;
157         struct btrfs_trans_handle *trans;
158         struct btrfs_disk_key *found_key;
159         struct btrfs_leaf *leaf;
160
161         btrfs_init_path(&path);
162         mutex_lock(&root->fs_info->fs_mutex);
163         trans = btrfs_start_transaction(root, 1);
164         key.objectid = inode->i_ino;
165         key.offset = (u64)-1;
166         key.flags = 0;
167         btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
168         ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
169         if (ret < 0) {
170                 err = ret;
171                 goto out;
172         }
173
174         BUG_ON(ret == 0);
175         BUG_ON(path.slots[0] == 0);
176         path.slots[0]--;
177         leaf = btrfs_buffer_leaf(path.nodes[0]);
178         found_key = &leaf->items[path.slots[0]].key;
179         if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
180                 err = -ENOENT;
181                 goto out;
182         }
183         if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
184             btrfs_disk_key_offset(found_key) != 2) {
185                 err = -ENOTEMPTY;
186                 goto out;
187         }
188         ret = btrfs_del_item(trans, root, &path);
189         BUG_ON(ret);
190         btrfs_release_path(root, &path);
191         key.offset = 1;
192         ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
193         if (ret < 0) {
194                 err = ret;
195                 goto out;
196         }
197         if (ret > 0) {
198                 err = -ENOTEMPTY;
199                 goto out;
200         }
201         ret = btrfs_del_item(trans, root, &path);
202         if (ret) {
203                 err = ret;
204                 goto out;
205         }
206         btrfs_release_path(root, &path);
207
208         /* now the directory is empty */
209         err = btrfs_unlink_trans(trans, root, dir, dentry);
210         if (!err) {
211                 inode->i_size = 0;
212         }
213 out:
214         mutex_unlock(&root->fs_info->fs_mutex);
215         ret = btrfs_end_transaction(trans, root);
216         if (ret && !err)
217                 err = ret;
218         return err;
219 }
220
221 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
222                             struct btrfs_root *root,
223                             struct inode *inode)
224 {
225         u64 objectid = inode->i_ino;
226         struct btrfs_path path;
227         struct btrfs_inode_map_item *map;
228         struct btrfs_key stat_data_key;
229         int ret;
230         clear_inode(inode);
231         btrfs_init_path(&path);
232         ret = btrfs_lookup_inode_map(trans, root, &path, objectid, -1);
233         if (ret) {
234                 if (ret > 0)
235                         ret = -ENOENT;
236                 btrfs_release_path(root, &path);
237                 goto error;
238         }
239         map = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
240                             struct btrfs_inode_map_item);
241         btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
242         ret = btrfs_del_item(trans, root->fs_info->inode_root, &path);
243         BUG_ON(ret);
244         btrfs_release_path(root, &path);
245         btrfs_init_path(&path);
246
247         ret = btrfs_lookup_inode(trans, root, &path, objectid, -1);
248         BUG_ON(ret);
249         ret = btrfs_del_item(trans, root, &path);
250         BUG_ON(ret);
251         btrfs_release_path(root, &path);
252 error:
253         return ret;
254 }
255
256 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
257                                    struct btrfs_root *root,
258                                    struct inode *inode)
259 {
260         int ret;
261         struct btrfs_path path;
262         struct btrfs_key key;
263         struct btrfs_disk_key *found_key;
264         struct btrfs_leaf *leaf;
265         struct btrfs_file_extent_item *fi = NULL;
266         u64 extent_start = 0;
267         u64 extent_num_blocks = 0;
268         int found_extent;
269
270         /* FIXME, add redo link to tree so we don't leak on crash */
271         key.objectid = inode->i_ino;
272         key.offset = (u64)-1;
273         key.flags = 0;
274         btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
275         while(1) {
276                 btrfs_init_path(&path);
277                 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
278                 if (ret < 0) {
279                         btrfs_release_path(root, &path);
280                         goto error;
281                 }
282                 if (ret > 0) {
283                         BUG_ON(path.slots[0] == 0);
284                         path.slots[0]--;
285                 }
286                 leaf = btrfs_buffer_leaf(path.nodes[0]);
287                 found_key = &leaf->items[path.slots[0]].key;
288                 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
289                         break;
290                 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
291                     btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
292                         break;
293                 if (btrfs_disk_key_offset(found_key) < inode->i_size)
294                         break;
295                 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
296                         fi = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
297                                             path.slots[0],
298                                             struct btrfs_file_extent_item);
299                         extent_start = btrfs_file_extent_disk_blocknr(fi);
300                         extent_num_blocks =
301                                 btrfs_file_extent_disk_num_blocks(fi);
302                         inode->i_blocks -=
303                                 btrfs_file_extent_num_blocks(fi) >> 9;
304                         found_extent = 1;
305                 } else {
306                         found_extent = 0;
307                 }
308                 ret = btrfs_del_item(trans, root, &path);
309                 BUG_ON(ret);
310                 btrfs_release_path(root, &path);
311                 if (found_extent) {
312                         ret = btrfs_free_extent(trans, root, extent_start,
313                                                 extent_num_blocks, 0);
314                         BUG_ON(ret);
315                 }
316         }
317         btrfs_release_path(root, &path);
318         ret = 0;
319 error:
320         return ret;
321 }
322
323 static void btrfs_delete_inode(struct inode *inode)
324 {
325         struct btrfs_trans_handle *trans;
326         struct btrfs_root *root = btrfs_sb(inode->i_sb);
327         int ret;
328
329         truncate_inode_pages(&inode->i_data, 0);
330         if (is_bad_inode(inode)) {
331                 goto no_delete;
332         }
333         inode->i_size = 0;
334         mutex_lock(&root->fs_info->fs_mutex);
335         trans = btrfs_start_transaction(root, 1);
336         if (S_ISREG(inode->i_mode)) {
337                 ret = btrfs_truncate_in_trans(trans, root, inode);
338                 BUG_ON(ret);
339         }
340         btrfs_free_inode(trans, root, inode);
341         btrfs_end_transaction(trans, root);
342         mutex_unlock(&root->fs_info->fs_mutex);
343         return;
344 no_delete:
345         clear_inode(inode);
346 }
347
348 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
349                               ino_t *ino)
350 {
351         const char *name = dentry->d_name.name;
352         int namelen = dentry->d_name.len;
353         struct btrfs_dir_item *di;
354         struct btrfs_path path;
355         struct btrfs_root *root = btrfs_sb(dir->i_sb);
356         int ret;
357
358         btrfs_init_path(&path);
359         ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name,
360                                     namelen, 0);
361         if (ret || !btrfs_match_dir_item_name(root, &path, name, namelen)) {
362                 *ino = 0;
363                 ret = 0;
364                 goto out;
365         }
366         di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
367                             struct btrfs_dir_item);
368         *ino = btrfs_dir_objectid(di);
369 out:
370         btrfs_release_path(root, &path);
371         check_inode(dir);
372         return ret;
373 }
374
375 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
376                                    struct nameidata *nd)
377 {
378         struct inode * inode;
379         struct btrfs_root *root = btrfs_sb(dir->i_sb);
380         ino_t ino;
381         int ret;
382
383         if (dentry->d_name.len > BTRFS_NAME_LEN)
384                 return ERR_PTR(-ENAMETOOLONG);
385         mutex_lock(&root->fs_info->fs_mutex);
386         ret = btrfs_inode_by_name(dir, dentry, &ino);
387         mutex_unlock(&root->fs_info->fs_mutex);
388         if (ret < 0)
389                 return ERR_PTR(ret);
390         inode = NULL;
391         if (ino) {
392                 inode = iget(dir->i_sb, ino);
393                 if (!inode)
394                         return ERR_PTR(-EACCES);
395                 check_inode(inode);
396         }
397         check_inode(dir);
398         return d_splice_alias(inode, dentry);
399 }
400
401 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
402 {
403         struct inode *inode = filp->f_path.dentry->d_inode;
404         struct btrfs_root *root = btrfs_sb(inode->i_sb);
405         struct btrfs_item *item;
406         struct btrfs_dir_item *di;
407         struct btrfs_key key;
408         struct btrfs_path path;
409         int ret;
410         u32 nritems;
411         struct btrfs_leaf *leaf;
412         int slot;
413         int advance;
414         unsigned char d_type = DT_UNKNOWN;
415         int over = 0;
416
417         mutex_lock(&root->fs_info->fs_mutex);
418         key.objectid = inode->i_ino;
419         key.flags = 0;
420         btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
421         key.offset = filp->f_pos;
422         btrfs_init_path(&path);
423         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
424         if (ret < 0) {
425                 goto err;
426         }
427         advance = 0;
428         while(1) {
429                 leaf = btrfs_buffer_leaf(path.nodes[0]);
430                 nritems = btrfs_header_nritems(&leaf->header);
431                 slot = path.slots[0];
432                 if (advance || slot >= nritems) {
433                         if (slot >= nritems -1) {
434                                 ret = btrfs_next_leaf(root, &path);
435                                 if (ret)
436                                         break;
437                                 leaf = btrfs_buffer_leaf(path.nodes[0]);
438                                 nritems = btrfs_header_nritems(&leaf->header);
439                                 slot = path.slots[0];
440                         } else {
441                                 slot++;
442                                 path.slots[0]++;
443                         }
444                 }
445                 advance = 1;
446                 item = leaf->items + slot;
447                 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
448                         break;
449                 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
450                         continue;
451                 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
452                         continue;
453
454                 advance = 1;
455                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
456                 over = filldir(dirent, (const char *)(di + 1),
457                                btrfs_dir_name_len(di),
458                                btrfs_disk_key_offset(&item->key),
459                                btrfs_dir_objectid(di), d_type);
460                 if (over) {
461                         filp->f_pos = btrfs_disk_key_offset(&item->key);
462                         break;
463                 }
464                 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
465         }
466         ret = 0;
467 err:
468         btrfs_release_path(root, &path);
469         mutex_unlock(&root->fs_info->fs_mutex);
470         return ret;
471 }
472
473 static void btrfs_put_super (struct super_block * sb)
474 {
475         struct btrfs_root *root = btrfs_sb(sb);
476         int ret;
477
478         ret = close_ctree(root);
479         if (ret) {
480                 printk("close ctree returns %d\n", ret);
481         }
482         sb->s_fs_info = NULL;
483 }
484
485 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
486 {
487         struct inode * inode;
488         struct dentry * root_dentry;
489         struct btrfs_super_block *disk_super;
490         struct btrfs_root *root;
491
492         sb->s_maxbytes = MAX_LFS_FILESIZE;
493         sb->s_magic = BTRFS_SUPER_MAGIC;
494         sb->s_op = &btrfs_super_ops;
495         sb->s_time_gran = 1;
496
497         root = open_ctree(sb);
498
499         if (!root) {
500                 printk("btrfs: open_ctree failed\n");
501                 return -EIO;
502         }
503         sb->s_fs_info = root;
504         disk_super = root->fs_info->disk_super;
505         printk("read in super total blocks %Lu root %Lu\n",
506                btrfs_super_total_blocks(disk_super),
507                btrfs_super_root_dir(disk_super));
508
509         inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
510         if (!inode)
511                 return -ENOMEM;
512         if (inode->i_state & I_NEW) {
513                 btrfs_read_locked_inode(inode);
514                 unlock_new_inode(inode);
515         }
516
517         root_dentry = d_alloc_root(inode);
518         if (!root_dentry) {
519                 iput(inode);
520                 return -ENOMEM;
521         }
522         sb->s_root = root_dentry;
523
524         return 0;
525 }
526
527 static void fill_inode_item(struct btrfs_inode_item *item,
528                             struct inode *inode)
529 {
530         btrfs_set_inode_uid(item, inode->i_uid);
531         btrfs_set_inode_gid(item, inode->i_gid);
532         btrfs_set_inode_size(item, inode->i_size);
533         btrfs_set_inode_mode(item, inode->i_mode);
534         btrfs_set_inode_nlink(item, inode->i_nlink);
535         btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
536         btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
537         btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
538         btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
539         btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
540         btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
541         btrfs_set_inode_nblocks(item, inode->i_blocks);
542         btrfs_set_inode_generation(item, inode->i_generation);
543         check_inode(inode);
544 }
545
546 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
547                               struct btrfs_root *root,
548                               struct inode *inode)
549 {
550         struct btrfs_inode_item *inode_item;
551         struct btrfs_path path;
552         int ret;
553
554         btrfs_init_path(&path);
555
556         ret = btrfs_lookup_inode(trans, root, &path, inode->i_ino, 1);
557         if (ret) {
558                 if (ret > 0)
559                         ret = -ENOENT;
560                 goto failed;
561         }
562
563         inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
564                                   path.slots[0],
565                                   struct btrfs_inode_item);
566
567         fill_inode_item(inode_item, inode);
568         btrfs_mark_buffer_dirty(path.nodes[0]);
569 failed:
570         btrfs_release_path(root, &path);
571         check_inode(inode);
572         return 0;
573 }
574
575 static int btrfs_write_inode(struct inode *inode, int wait)
576 {
577         struct btrfs_root *root = btrfs_sb(inode->i_sb);
578         struct btrfs_trans_handle *trans;
579         int ret;
580
581         mutex_lock(&root->fs_info->fs_mutex);
582         trans = btrfs_start_transaction(root, 1);
583         ret = btrfs_update_inode(trans, root, inode);
584         if (wait)
585                 btrfs_commit_transaction(trans, root);
586         else
587                 btrfs_end_transaction(trans, root);
588         mutex_unlock(&root->fs_info->fs_mutex);
589         check_inode(inode);
590         return ret;
591 }
592
593 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
594                                      struct inode *dir, int mode)
595 {
596         struct inode *inode;
597         struct btrfs_inode_item inode_item;
598         struct btrfs_root *root = btrfs_sb(dir->i_sb);
599         struct btrfs_key key;
600         int ret;
601         u64 objectid;
602
603         inode = new_inode(dir->i_sb);
604         if (!inode)
605                 return ERR_PTR(-ENOMEM);
606
607         check_inode(inode);
608         ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
609         BUG_ON(ret);
610
611         inode->i_uid = current->fsuid;
612         inode->i_gid = current->fsgid;
613         inode->i_mode = mode;
614         inode->i_ino = objectid;
615         inode->i_blocks = 0;
616         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
617         fill_inode_item(&inode_item, inode);
618
619         key.objectid = objectid;
620         key.flags = 0;
621         key.offset = 0;
622         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
623         ret = btrfs_insert_inode_map(trans, root, objectid, &key);
624         BUG_ON(ret);
625
626         ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
627         BUG_ON(ret);
628
629         insert_inode_hash(inode);
630         check_inode(inode);
631         check_inode(dir);
632         return inode;
633 }
634
635 static int btrfs_add_link(struct btrfs_trans_handle *trans,
636                             struct dentry *dentry, struct inode *inode)
637 {
638         int ret;
639         ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
640                                     dentry->d_name.name, dentry->d_name.len,
641                                     dentry->d_parent->d_inode->i_ino,
642                                     inode->i_ino, 0);
643         if (ret == 0) {
644                 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
645                 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
646                                          dentry->d_parent->d_inode);
647         }
648         check_inode(inode);
649         check_inode(dentry->d_parent->d_inode);
650         return ret;
651 }
652
653 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
654                             struct dentry *dentry, struct inode *inode)
655 {
656         int err = btrfs_add_link(trans, dentry, inode);
657         if (!err) {
658                 d_instantiate(dentry, inode);
659                 return 0;
660         }
661         if (err > 0)
662                 err = -EEXIST;
663         check_inode(inode);
664         return err;
665 }
666
667 static int btrfs_create(struct inode *dir, struct dentry *dentry,
668                         int mode, struct nameidata *nd)
669 {
670         struct btrfs_trans_handle *trans;
671         struct btrfs_root *root = btrfs_sb(dir->i_sb);
672         struct inode *inode;
673         int err;
674         int drop_inode = 0;
675
676         mutex_lock(&root->fs_info->fs_mutex);
677         trans = btrfs_start_transaction(root, 1);
678         inode = btrfs_new_inode(trans, dir, mode);
679         err = PTR_ERR(inode);
680         if (IS_ERR(inode))
681                 goto out_unlock;
682         // FIXME mark the inode dirty
683         err = btrfs_add_nondir(trans, dentry, inode);
684         if (err)
685                 drop_inode = 1;
686         else {
687                 inode->i_mapping->a_ops = &btrfs_aops;
688                 inode->i_fop = &btrfs_file_operations;
689                 inode->i_op = &btrfs_file_inode_operations;
690         }
691         dir->i_sb->s_dirt = 1;
692 out_unlock:
693         btrfs_end_transaction(trans, root);
694         mutex_unlock(&root->fs_info->fs_mutex);
695         check_inode(inode);
696         check_inode(dir);
697
698         if (drop_inode) {
699                 inode_dec_link_count(inode);
700                 iput(inode);
701         }
702         return err;
703 }
704
705 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
706                                 struct inode *inode, struct inode *dir)
707 {
708         struct btrfs_root *root = btrfs_sb(inode->i_sb);
709         int ret;
710         char buf[2];
711         buf[0] = '.';
712         buf[1] = '.';
713
714         ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
715                                     inode->i_ino, 1);
716         if (ret)
717                 goto error;
718         ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
719                                     dir->i_ino, 1);
720         if (ret)
721                 goto error;
722         inode->i_size = 3;
723         ret = btrfs_update_inode(trans, root, inode);
724 error:
725         return ret;
726 }
727
728 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
729 {
730         struct inode *inode;
731         struct btrfs_trans_handle *trans;
732         struct btrfs_root *root = btrfs_sb(dir->i_sb);
733         int err = 0;
734         int drop_on_err = 0;
735
736         mutex_lock(&root->fs_info->fs_mutex);
737         trans = btrfs_start_transaction(root, 1);
738         if (IS_ERR(trans)) {
739                 err = PTR_ERR(trans);
740                 goto out_unlock;
741         }
742         inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
743         if (IS_ERR(inode)) {
744                 err = PTR_ERR(inode);
745                 goto out_fail;
746         }
747         drop_on_err = 1;
748         inode->i_op = &btrfs_dir_inode_operations;
749         inode->i_fop = &btrfs_dir_file_operations;
750
751         err = btrfs_make_empty_dir(trans, inode, dir);
752         if (err)
753                 goto out_fail;
754         err = btrfs_add_link(trans, dentry, inode);
755         if (err)
756                 goto out_fail;
757         d_instantiate(dentry, inode);
758         drop_on_err = 0;
759
760 out_fail:
761         btrfs_end_transaction(trans, root);
762 out_unlock:
763         mutex_unlock(&root->fs_info->fs_mutex);
764         if (drop_on_err)
765                 iput(inode);
766         return err;
767 }
768
769 static int btrfs_sync_fs(struct super_block *sb, int wait)
770 {
771         struct btrfs_trans_handle *trans;
772         struct btrfs_root *root;
773         int ret;
774         root = btrfs_sb(sb);
775
776         sb->s_dirt = 0;
777         if (!wait) {
778                 // filemap_flush(root->fs_info->btree_inode->i_mapping);
779                 filemap_flush(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
780                 return 0;
781         }
782         filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
783         mutex_lock(&root->fs_info->fs_mutex);
784         trans = btrfs_start_transaction(root, 1);
785         ret = btrfs_commit_transaction(trans, root);
786         sb->s_dirt = 0;
787         BUG_ON(ret);
788 printk("btrfs sync_fs\n");
789         mutex_unlock(&root->fs_info->fs_mutex);
790         return 0;
791 }
792
793 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
794                            struct buffer_head *result, int create)
795 {
796         int ret;
797         int err = 0;
798         u64 blocknr;
799         u64 extent_start = 0;
800         u64 extent_end = 0;
801         u64 objectid = inode->i_ino;
802         struct btrfs_path path;
803         struct btrfs_root *root = btrfs_sb(inode->i_sb);
804         struct btrfs_trans_handle *trans = NULL;
805         struct btrfs_file_extent_item *item;
806         struct btrfs_leaf *leaf;
807         struct btrfs_disk_key *found_key;
808
809         btrfs_init_path(&path);
810         if (create)
811                 trans = btrfs_start_transaction(root, 1);
812
813
814         ret = btrfs_lookup_file_extent(trans, root, &path,
815                                        inode->i_ino,
816                                        iblock << inode->i_blkbits, 0);
817         if (ret < 0) {
818                 btrfs_release_path(root, &path);
819                 err = ret;
820                 goto out;
821         }
822
823         if (ret != 0) {
824                 if (path.slots[0] == 0) {
825                         btrfs_release_path(root, &path);
826                         goto allocate;
827                 }
828                 path.slots[0]--;
829         }
830
831         item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
832                               struct btrfs_file_extent_item);
833         leaf = btrfs_buffer_leaf(path.nodes[0]);
834         blocknr = btrfs_file_extent_disk_blocknr(item);
835         blocknr += btrfs_file_extent_offset(item);
836
837         /* exact match found, use it */
838         if (ret == 0) {
839                 err = 0;
840                 map_bh(result, inode->i_sb, blocknr);
841                 btrfs_release_path(root, &path);
842                 goto out;
843         }
844
845         /* are we inside the extent that was found? */
846         found_key = &leaf->items[path.slots[0]].key;
847         if (btrfs_disk_key_objectid(found_key) != objectid ||
848             btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
849                 extent_end = 0;
850                 extent_start = 0;
851                 btrfs_release_path(root, &path);
852                 goto allocate;
853         }
854
855         extent_start = btrfs_disk_key_offset(&leaf->items[path.slots[0]].key);
856         extent_start = extent_start >> inode->i_blkbits;
857         extent_start += btrfs_file_extent_offset(item);
858         extent_end = extent_start + btrfs_file_extent_num_blocks(item);
859         btrfs_release_path(root, &path);
860         if (iblock >= extent_start && iblock < extent_end) {
861                 err = 0;
862                 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
863                 goto out;
864         }
865 allocate:
866         /* ok, create a new extent */
867         if (!create) {
868                 err = 0;
869                 goto out;
870         }
871         ret = btrfs_alloc_file_extent(trans, root, objectid,
872                                       iblock << inode->i_blkbits,
873                                       1, extent_end, &blocknr);
874         if (ret) {
875                 err = ret;
876                 goto out;
877         }
878         inode->i_blocks += inode->i_sb->s_blocksize >> 9;
879         set_buffer_new(result);
880         map_bh(result, inode->i_sb, blocknr);
881
882 out:
883         if (trans)
884                 btrfs_end_transaction(trans, root);
885         return err;
886 }
887
888 static int btrfs_get_block(struct inode *inode, sector_t iblock,
889                            struct buffer_head *result, int create)
890 {
891         int err;
892         struct btrfs_root *root = btrfs_sb(inode->i_sb);
893         mutex_lock(&root->fs_info->fs_mutex);
894         err = btrfs_get_block_lock(inode, iblock, result, create);
895         mutex_unlock(&root->fs_info->fs_mutex);
896         return err;
897 }
898
899 static int btrfs_prepare_write(struct file *file, struct page *page,
900                                unsigned from, unsigned to)
901 {
902         WARN_ON(1);
903         return nobh_prepare_write(page, from, to, btrfs_get_block);
904 }
905 static int btrfs_commit_write(struct file *file, struct page *page,
906                                unsigned from, unsigned to)
907 {
908         WARN_ON(1);
909         return nobh_commit_write(file, page, from, to);
910 }
911
912 static void btrfs_write_super(struct super_block *sb)
913 {
914         btrfs_sync_fs(sb, 1);
915 }
916
917 static int btrfs_readpage(struct file *file, struct page *page)
918 {
919         return mpage_readpage(page, btrfs_get_block);
920 }
921
922 static int btrfs_readpages(struct file *file, struct address_space *mapping,
923                            struct list_head *pages, unsigned nr_pages)
924 {
925         return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
926 }
927
928 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
929 {
930         return nobh_writepage(page, btrfs_get_block, wbc);
931 }
932
933 static void btrfs_truncate(struct inode *inode)
934 {
935         struct btrfs_root *root = btrfs_sb(inode->i_sb);
936         int ret;
937         struct btrfs_trans_handle *trans;
938
939         if (!S_ISREG(inode->i_mode))
940                 return;
941         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
942                 return;
943
944         nobh_truncate_page(inode->i_mapping, inode->i_size);
945
946         /* FIXME, add redo link to tree so we don't leak on crash */
947         mutex_lock(&root->fs_info->fs_mutex);
948         trans = btrfs_start_transaction(root, 1);
949         ret = btrfs_truncate_in_trans(trans, root, inode);
950         BUG_ON(ret);
951         ret = btrfs_end_transaction(trans, root);
952         BUG_ON(ret);
953         mutex_unlock(&root->fs_info->fs_mutex);
954         mark_inode_dirty(inode);
955 }
956
957 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
958                                 struct page **prepared_pages,
959                                 const char __user * buf)
960 {
961         long page_fault = 0;
962         int i;
963         int offset = pos & (PAGE_CACHE_SIZE - 1);
964
965         for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
966                 size_t count = min_t(size_t,
967                                      PAGE_CACHE_SIZE - offset, write_bytes);
968                 struct page *page = prepared_pages[i];
969                 fault_in_pages_readable(buf, count);
970
971                 /* Copy data from userspace to the current page */
972                 kmap(page);
973                 page_fault = __copy_from_user(page_address(page) + offset,
974                                               buf, count);
975                 /* Flush processor's dcache for this page */
976                 flush_dcache_page(page);
977                 kunmap(page);
978                 buf += count;
979                 write_bytes -= count;
980
981                 if (page_fault)
982                         break;
983         }
984         return page_fault ? -EFAULT : 0;
985 }
986
987 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
988 {
989         size_t i;
990         for (i = 0; i < num_pages; i++) {
991                 if (!pages[i])
992                         break;
993                 unlock_page(pages[i]);
994                 mark_page_accessed(pages[i]);
995                 page_cache_release(pages[i]);
996         }
997 }
998 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
999                                    struct btrfs_root *root,
1000                                    struct file *file,
1001                                    struct page **pages,
1002                                    size_t num_pages,
1003                                    loff_t pos,
1004                                    size_t write_bytes)
1005 {
1006         int i;
1007         int offset;
1008         int err = 0;
1009         int ret;
1010         int this_write;
1011         struct inode *inode = file->f_path.dentry->d_inode;
1012
1013         for (i = 0; i < num_pages; i++) {
1014                 offset = pos & (PAGE_CACHE_SIZE -1);
1015                 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1016                 /* FIXME, one block at a time */
1017
1018                 mutex_lock(&root->fs_info->fs_mutex);
1019                 trans = btrfs_start_transaction(root, 1);
1020                 btrfs_csum_file_block(trans, root, inode->i_ino,
1021                                       pages[i]->index << PAGE_CACHE_SHIFT,
1022                                       kmap(pages[i]), PAGE_CACHE_SIZE);
1023                 kunmap(pages[i]);
1024                 SetPageChecked(pages[i]);
1025                 ret = btrfs_end_transaction(trans, root);
1026                 BUG_ON(ret);
1027                 mutex_unlock(&root->fs_info->fs_mutex);
1028
1029                 ret = nobh_commit_write(file, pages[i], offset,
1030                                          offset + this_write);
1031                 pos += this_write;
1032                 if (ret) {
1033                         err = ret;
1034                         goto failed;
1035                 }
1036                 WARN_ON(this_write > write_bytes);
1037                 write_bytes -= this_write;
1038         }
1039 failed:
1040         return err;
1041 }
1042
1043 static int prepare_pages(struct btrfs_trans_handle *trans,
1044                          struct btrfs_root *root,
1045                          struct file *file,
1046                          struct page **pages,
1047                          size_t num_pages,
1048                          loff_t pos,
1049                          size_t write_bytes)
1050 {
1051         int i;
1052         unsigned long index = pos >> PAGE_CACHE_SHIFT;
1053         struct inode *inode = file->f_path.dentry->d_inode;
1054         int offset;
1055         int err = 0;
1056         int ret;
1057         int this_write;
1058         loff_t isize = i_size_read(inode);
1059
1060         memset(pages, 0, num_pages * sizeof(struct page *));
1061
1062         for (i = 0; i < num_pages; i++) {
1063                 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1064                 if (!pages[i]) {
1065                         err = -ENOMEM;
1066                         goto failed_release;
1067                 }
1068                 offset = pos & (PAGE_CACHE_SIZE -1);
1069                 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1070                 ret = nobh_prepare_write(pages[i], offset,
1071                                          offset + this_write,
1072                                          btrfs_get_block);
1073                 pos += this_write;
1074                 if (ret) {
1075                         err = ret;
1076                         goto failed_truncate;
1077                 }
1078                 WARN_ON(this_write > write_bytes);
1079                 write_bytes -= this_write;
1080         }
1081         return 0;
1082
1083 failed_release:
1084         btrfs_drop_pages(pages, num_pages);
1085         return err;
1086
1087 failed_truncate:
1088         btrfs_drop_pages(pages, num_pages);
1089         if (pos > isize)
1090                 vmtruncate(inode, isize);
1091         return err;
1092 }
1093
1094 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1095                                 size_t count, loff_t *ppos)
1096 {
1097         loff_t pos;
1098         size_t num_written = 0;
1099         int err = 0;
1100         int ret = 0;
1101         struct inode *inode = file->f_path.dentry->d_inode;
1102         struct btrfs_root *root = btrfs_sb(inode->i_sb);
1103         struct page *pages[1];
1104
1105         if (file->f_flags & O_DIRECT)
1106                 return -EINVAL;
1107         pos = *ppos;
1108
1109         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1110         current->backing_dev_info = inode->i_mapping->backing_dev_info;
1111         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1112         if (err)
1113                 goto out;
1114         if (count == 0)
1115                 goto out;
1116         err = remove_suid(file->f_path.dentry);
1117         if (err)
1118                 goto out;
1119         file_update_time(file);
1120         mutex_lock(&inode->i_mutex);
1121         while(count > 0) {
1122                 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1123                 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1124                 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1125                                         PAGE_CACHE_SHIFT;
1126                 ret = prepare_pages(NULL, root, file, pages, num_pages,
1127                                     pos, write_bytes);
1128                 BUG_ON(ret);
1129                 ret = btrfs_copy_from_user(pos, num_pages,
1130                                            write_bytes, pages, buf);
1131                 BUG_ON(ret);
1132
1133                 ret = dirty_and_release_pages(NULL, root, file, pages,
1134                                               num_pages, pos, write_bytes);
1135                 BUG_ON(ret);
1136                 btrfs_drop_pages(pages, num_pages);
1137
1138                 buf += write_bytes;
1139                 count -= write_bytes;
1140                 pos += write_bytes;
1141                 num_written += write_bytes;
1142
1143                 balance_dirty_pages_ratelimited(inode->i_mapping);
1144                 cond_resched();
1145         }
1146         mutex_unlock(&inode->i_mutex);
1147 out:
1148         *ppos = pos;
1149         current->backing_dev_info = NULL;
1150         return num_written ? num_written : err;
1151 }
1152
1153 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1154                         unsigned long offset, unsigned long size)
1155 {
1156         char *kaddr;
1157         unsigned long left, count = desc->count;
1158
1159         if (size > count)
1160                 size = count;
1161
1162         if (!PageChecked(page)) {
1163                 /* FIXME, do it per block */
1164                 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1165                 int ret = btrfs_csum_verify_file_block(root,
1166                                           page->mapping->host->i_ino,
1167                                           page->index << PAGE_CACHE_SHIFT,
1168                                           kmap(page), PAGE_CACHE_SIZE);
1169                 if (ret) {
1170                         printk("failed to verify ino %lu page %lu\n",
1171                                page->mapping->host->i_ino,
1172                                page->index);
1173                         memset(page_address(page), 0, PAGE_CACHE_SIZE);
1174                 }
1175                 SetPageChecked(page);
1176                 kunmap(page);
1177         }
1178         /*
1179          * Faults on the destination of a read are common, so do it before
1180          * taking the kmap.
1181          */
1182         if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1183                 kaddr = kmap_atomic(page, KM_USER0);
1184                 left = __copy_to_user_inatomic(desc->arg.buf,
1185                                                 kaddr + offset, size);
1186                 kunmap_atomic(kaddr, KM_USER0);
1187                 if (left == 0)
1188                         goto success;
1189         }
1190
1191         /* Do it the slow way */
1192         kaddr = kmap(page);
1193         left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1194         kunmap(page);
1195
1196         if (left) {
1197                 size -= left;
1198                 desc->error = -EFAULT;
1199         }
1200 success:
1201         desc->count = count - size;
1202         desc->written += size;
1203         desc->arg.buf += size;
1204         return size;
1205 }
1206
1207 /**
1208  * btrfs_file_aio_read - filesystem read routine
1209  * @iocb:       kernel I/O control block
1210  * @iov:        io vector request
1211  * @nr_segs:    number of segments in the iovec
1212  * @pos:        current file position
1213  */
1214 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1215                                    unsigned long nr_segs, loff_t pos)
1216 {
1217         struct file *filp = iocb->ki_filp;
1218         ssize_t retval;
1219         unsigned long seg;
1220         size_t count;
1221         loff_t *ppos = &iocb->ki_pos;
1222
1223         count = 0;
1224         for (seg = 0; seg < nr_segs; seg++) {
1225                 const struct iovec *iv = &iov[seg];
1226
1227                 /*
1228                  * If any segment has a negative length, or the cumulative
1229                  * length ever wraps negative then return -EINVAL.
1230                  */
1231                 count += iv->iov_len;
1232                 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1233                         return -EINVAL;
1234                 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1235                         continue;
1236                 if (seg == 0)
1237                         return -EFAULT;
1238                 nr_segs = seg;
1239                 count -= iv->iov_len;   /* This segment is no good */
1240                 break;
1241         }
1242         retval = 0;
1243         if (count) {
1244                 for (seg = 0; seg < nr_segs; seg++) {
1245                         read_descriptor_t desc;
1246
1247                         desc.written = 0;
1248                         desc.arg.buf = iov[seg].iov_base;
1249                         desc.count = iov[seg].iov_len;
1250                         if (desc.count == 0)
1251                                 continue;
1252                         desc.error = 0;
1253                         do_generic_file_read(filp, ppos, &desc,
1254                                              btrfs_read_actor);
1255                         retval += desc.written;
1256                         if (desc.error) {
1257                                 retval = retval ?: desc.error;
1258                                 break;
1259                         }
1260                 }
1261         }
1262         return retval;
1263 }
1264
1265 static struct kmem_cache *btrfs_inode_cachep;
1266 struct kmem_cache *btrfs_trans_handle_cachep;
1267 struct kmem_cache *btrfs_transaction_cachep;
1268 struct kmem_cache *btrfs_bit_radix_cachep;
1269 struct kmem_cache *btrfs_path_cachep;
1270
1271 /*
1272  * Called inside transaction, so use GFP_NOFS
1273  */
1274 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1275 {
1276         struct btrfs_inode *ei;
1277
1278         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1279         if (!ei)
1280                 return NULL;
1281         ei->magic = 0xDEADBEEF;
1282         ei->magic2 = 0xDEADBEAF;
1283         return &ei->vfs_inode;
1284 }
1285
1286 static void btrfs_destroy_inode(struct inode *inode)
1287 {
1288         struct btrfs_inode *ei = BTRFS_I(inode);
1289         WARN_ON(ei->magic != 0xDEADBEEF);
1290         WARN_ON(ei->magic2 != 0xDEADBEAF);
1291         WARN_ON(!list_empty(&inode->i_dentry));
1292         WARN_ON(inode->i_ino == 1);
1293         WARN_ON(inode->i_data.nrpages);
1294
1295         ei->magic = 0;
1296         ei->magic2 = 0;
1297         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1298 }
1299
1300 static void init_once(void * foo, struct kmem_cache * cachep,
1301                       unsigned long flags)
1302 {
1303         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1304
1305         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1306             SLAB_CTOR_CONSTRUCTOR) {
1307                 inode_init_once(&ei->vfs_inode);
1308         }
1309 }
1310
1311 static int init_inodecache(void)
1312 {
1313         btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1314                                              sizeof(struct btrfs_inode),
1315                                              0, (SLAB_RECLAIM_ACCOUNT|
1316                                                 SLAB_MEM_SPREAD),
1317                                              init_once, NULL);
1318         btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1319                                              sizeof(struct btrfs_trans_handle),
1320                                              0, (SLAB_RECLAIM_ACCOUNT|
1321                                                 SLAB_MEM_SPREAD),
1322                                              NULL, NULL);
1323         btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1324                                              sizeof(struct btrfs_transaction),
1325                                              0, (SLAB_RECLAIM_ACCOUNT|
1326                                                 SLAB_MEM_SPREAD),
1327                                              NULL, NULL);
1328         btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1329                                              sizeof(struct btrfs_transaction),
1330                                              0, (SLAB_RECLAIM_ACCOUNT|
1331                                                 SLAB_MEM_SPREAD),
1332                                              NULL, NULL);
1333         btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1334                                              256,
1335                                              0, (SLAB_RECLAIM_ACCOUNT|
1336                                                 SLAB_MEM_SPREAD |
1337                                                 SLAB_DESTROY_BY_RCU),
1338                                              NULL, NULL);
1339         if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1340             btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1341                 return -ENOMEM;
1342         return 0;
1343 }
1344
1345 static void destroy_inodecache(void)
1346 {
1347         kmem_cache_destroy(btrfs_inode_cachep);
1348         kmem_cache_destroy(btrfs_trans_handle_cachep);
1349         kmem_cache_destroy(btrfs_transaction_cachep);
1350         kmem_cache_destroy(btrfs_bit_radix_cachep);
1351         kmem_cache_destroy(btrfs_path_cachep);
1352 }
1353
1354 static int btrfs_get_sb(struct file_system_type *fs_type,
1355         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1356 {
1357         return get_sb_bdev(fs_type, flags, dev_name, data,
1358                            btrfs_fill_super, mnt);
1359 }
1360
1361 static struct file_system_type btrfs_fs_type = {
1362         .owner          = THIS_MODULE,
1363         .name           = "btrfs",
1364         .get_sb         = btrfs_get_sb,
1365         .kill_sb        = kill_block_super,
1366         .fs_flags       = FS_REQUIRES_DEV,
1367 };
1368
1369 static struct super_operations btrfs_super_ops = {
1370         .statfs         = simple_statfs,
1371         .delete_inode   = btrfs_delete_inode,
1372         .put_super      = btrfs_put_super,
1373         .read_inode     = btrfs_read_locked_inode,
1374         .write_super    = btrfs_write_super,
1375         .sync_fs        = btrfs_sync_fs,
1376         .write_inode    = btrfs_write_inode,
1377         .alloc_inode    = btrfs_alloc_inode,
1378         .destroy_inode  = btrfs_destroy_inode,
1379 };
1380
1381 static struct inode_operations btrfs_dir_inode_operations = {
1382         .lookup         = btrfs_lookup,
1383         .create         = btrfs_create,
1384         .unlink         = btrfs_unlink,
1385         .mkdir          = btrfs_mkdir,
1386         .rmdir          = btrfs_rmdir,
1387 };
1388
1389 static struct file_operations btrfs_dir_file_operations = {
1390         .llseek         = generic_file_llseek,
1391         .read           = generic_read_dir,
1392         .readdir        = btrfs_readdir,
1393 };
1394
1395 static struct address_space_operations btrfs_aops = {
1396         .readpage       = btrfs_readpage,
1397         .readpages      = btrfs_readpages,
1398         .writepage      = btrfs_writepage,
1399         .sync_page      = block_sync_page,
1400         .prepare_write  = btrfs_prepare_write,
1401         .commit_write   = btrfs_commit_write,
1402 };
1403
1404 static struct inode_operations btrfs_file_inode_operations = {
1405         .truncate       = btrfs_truncate,
1406 };
1407
1408 static struct file_operations btrfs_file_operations = {
1409         .llseek         = generic_file_llseek,
1410         .read           = do_sync_read,
1411         .aio_read       = btrfs_file_aio_read,
1412         .write          = btrfs_file_write,
1413         .mmap           = generic_file_mmap,
1414         .open           = generic_file_open,
1415 };
1416
1417 static int __init init_btrfs_fs(void)
1418 {
1419         int err;
1420         printk("btrfs loaded!\n");
1421         err = init_inodecache();
1422         if (err)
1423                 return err;
1424         return register_filesystem(&btrfs_fs_type);
1425 }
1426
1427 static void __exit exit_btrfs_fs(void)
1428 {
1429         destroy_inodecache();
1430         unregister_filesystem(&btrfs_fs_type);
1431         printk("btrfs unloaded\n");
1432 }
1433
1434 module_init(init_btrfs_fs)
1435 module_exit(exit_btrfs_fs)
1436
1437 MODULE_LICENSE("GPL");