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