]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/btrfs/super.c
4fd2b168b2c46ce85c3f826e453ca1084d9592a9
[linux-2.6-omap-h63xx.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                 filp->f_pos = btrfs_disk_key_offset(&item->key);
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                         goto nopos;
498         }
499         filp->f_pos++;
500 nopos:
501         ret = 0;
502 err:
503         btrfs_release_path(root, path);
504         btrfs_free_path(path);
505         mutex_unlock(&root->fs_info->fs_mutex);
506         return ret;
507 }
508
509 static void btrfs_put_super (struct super_block * sb)
510 {
511         struct btrfs_root *root = btrfs_sb(sb);
512         int ret;
513
514         ret = close_ctree(root);
515         if (ret) {
516                 printk("close ctree returns %d\n", ret);
517         }
518         sb->s_fs_info = NULL;
519 }
520
521 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
522 {
523         struct inode * inode;
524         struct dentry * root_dentry;
525         struct btrfs_super_block *disk_super;
526         struct btrfs_root *root;
527
528         sb->s_maxbytes = MAX_LFS_FILESIZE;
529         sb->s_magic = BTRFS_SUPER_MAGIC;
530         sb->s_op = &btrfs_super_ops;
531         sb->s_time_gran = 1;
532
533         root = open_ctree(sb);
534
535         if (!root) {
536                 printk("btrfs: open_ctree failed\n");
537                 return -EIO;
538         }
539         sb->s_fs_info = root;
540         disk_super = root->fs_info->disk_super;
541         printk("read in super total blocks %Lu root %Lu\n",
542                btrfs_super_total_blocks(disk_super),
543                btrfs_super_root_dir(disk_super));
544
545         inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
546         if (!inode)
547                 return -ENOMEM;
548         if (inode->i_state & I_NEW) {
549                 btrfs_read_locked_inode(inode);
550                 unlock_new_inode(inode);
551         }
552
553         root_dentry = d_alloc_root(inode);
554         if (!root_dentry) {
555                 iput(inode);
556                 return -ENOMEM;
557         }
558         sb->s_root = root_dentry;
559
560         return 0;
561 }
562
563 static void fill_inode_item(struct btrfs_inode_item *item,
564                             struct inode *inode)
565 {
566         btrfs_set_inode_uid(item, inode->i_uid);
567         btrfs_set_inode_gid(item, inode->i_gid);
568         btrfs_set_inode_size(item, inode->i_size);
569         btrfs_set_inode_mode(item, inode->i_mode);
570         btrfs_set_inode_nlink(item, inode->i_nlink);
571         btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
572         btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
573         btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
574         btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
575         btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
576         btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
577         btrfs_set_inode_nblocks(item, inode->i_blocks);
578         btrfs_set_inode_generation(item, inode->i_generation);
579         check_inode(inode);
580 }
581
582 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
583                               struct btrfs_root *root,
584                               struct inode *inode)
585 {
586         struct btrfs_inode_item *inode_item;
587         struct btrfs_path *path;
588         int ret;
589
590         path = btrfs_alloc_path();
591         BUG_ON(!path);
592         btrfs_init_path(path);
593
594         ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
595         if (ret) {
596                 if (ret > 0)
597                         ret = -ENOENT;
598                 goto failed;
599         }
600
601         inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
602                                   path->slots[0],
603                                   struct btrfs_inode_item);
604
605         fill_inode_item(inode_item, inode);
606         btrfs_mark_buffer_dirty(path->nodes[0]);
607 failed:
608         btrfs_release_path(root, path);
609         btrfs_free_path(path);
610         check_inode(inode);
611         return 0;
612 }
613
614 static int btrfs_write_inode(struct inode *inode, int wait)
615 {
616         struct btrfs_root *root = btrfs_sb(inode->i_sb);
617         struct btrfs_trans_handle *trans;
618         int ret;
619
620         mutex_lock(&root->fs_info->fs_mutex);
621         trans = btrfs_start_transaction(root, 1);
622         ret = btrfs_update_inode(trans, root, inode);
623         if (wait)
624                 btrfs_commit_transaction(trans, root);
625         else
626                 btrfs_end_transaction(trans, root);
627         mutex_unlock(&root->fs_info->fs_mutex);
628         check_inode(inode);
629         return ret;
630 }
631
632 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
633                                      struct inode *dir, int mode)
634 {
635         struct inode *inode;
636         struct btrfs_inode_item inode_item;
637         struct btrfs_root *root = btrfs_sb(dir->i_sb);
638         struct btrfs_key key;
639         int ret;
640         u64 objectid;
641
642         inode = new_inode(dir->i_sb);
643         if (!inode)
644                 return ERR_PTR(-ENOMEM);
645
646         check_inode(inode);
647         ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
648         BUG_ON(ret);
649
650         inode->i_uid = current->fsuid;
651         inode->i_gid = current->fsgid;
652         inode->i_mode = mode;
653         inode->i_ino = objectid;
654         inode->i_blocks = 0;
655         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
656         fill_inode_item(&inode_item, inode);
657
658         key.objectid = objectid;
659         key.flags = 0;
660         key.offset = 0;
661         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
662         ret = btrfs_insert_inode_map(trans, root, objectid, &key);
663         BUG_ON(ret);
664
665         ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
666         BUG_ON(ret);
667
668         insert_inode_hash(inode);
669         check_inode(inode);
670         check_inode(dir);
671         return inode;
672 }
673
674 static int btrfs_add_link(struct btrfs_trans_handle *trans,
675                             struct dentry *dentry, struct inode *inode)
676 {
677         int ret;
678         ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
679                                     dentry->d_name.name, dentry->d_name.len,
680                                     dentry->d_parent->d_inode->i_ino,
681                                     inode->i_ino, 0);
682         if (ret == 0) {
683                 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
684                 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
685                                          dentry->d_parent->d_inode);
686         }
687         check_inode(inode);
688         check_inode(dentry->d_parent->d_inode);
689         return ret;
690 }
691
692 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
693                             struct dentry *dentry, struct inode *inode)
694 {
695         int err = btrfs_add_link(trans, dentry, inode);
696         if (!err) {
697                 d_instantiate(dentry, inode);
698                 return 0;
699         }
700         if (err > 0)
701                 err = -EEXIST;
702         check_inode(inode);
703         return err;
704 }
705
706 static int btrfs_create(struct inode *dir, struct dentry *dentry,
707                         int mode, struct nameidata *nd)
708 {
709         struct btrfs_trans_handle *trans;
710         struct btrfs_root *root = btrfs_sb(dir->i_sb);
711         struct inode *inode;
712         int err;
713         int drop_inode = 0;
714
715         mutex_lock(&root->fs_info->fs_mutex);
716         trans = btrfs_start_transaction(root, 1);
717         inode = btrfs_new_inode(trans, dir, mode);
718         err = PTR_ERR(inode);
719         if (IS_ERR(inode))
720                 goto out_unlock;
721         // FIXME mark the inode dirty
722         err = btrfs_add_nondir(trans, dentry, inode);
723         if (err)
724                 drop_inode = 1;
725         else {
726                 inode->i_mapping->a_ops = &btrfs_aops;
727                 inode->i_fop = &btrfs_file_operations;
728                 inode->i_op = &btrfs_file_inode_operations;
729         }
730         dir->i_sb->s_dirt = 1;
731 out_unlock:
732         btrfs_end_transaction(trans, root);
733         mutex_unlock(&root->fs_info->fs_mutex);
734         check_inode(inode);
735         check_inode(dir);
736
737         if (drop_inode) {
738                 inode_dec_link_count(inode);
739                 iput(inode);
740         }
741         return err;
742 }
743
744 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
745                                 struct inode *inode, struct inode *dir)
746 {
747         struct btrfs_root *root = btrfs_sb(inode->i_sb);
748         int ret;
749         char buf[2];
750         buf[0] = '.';
751         buf[1] = '.';
752
753         ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
754                                     inode->i_ino, 1);
755         if (ret)
756                 goto error;
757         ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
758                                     dir->i_ino, 1);
759         if (ret)
760                 goto error;
761         inode->i_size = 6;
762         ret = btrfs_update_inode(trans, root, inode);
763 error:
764         return ret;
765 }
766
767 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
768 {
769         struct inode *inode;
770         struct btrfs_trans_handle *trans;
771         struct btrfs_root *root = btrfs_sb(dir->i_sb);
772         int err = 0;
773         int drop_on_err = 0;
774
775         mutex_lock(&root->fs_info->fs_mutex);
776         trans = btrfs_start_transaction(root, 1);
777         if (IS_ERR(trans)) {
778                 err = PTR_ERR(trans);
779                 goto out_unlock;
780         }
781         inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
782         if (IS_ERR(inode)) {
783                 err = PTR_ERR(inode);
784                 goto out_fail;
785         }
786         drop_on_err = 1;
787         inode->i_op = &btrfs_dir_inode_operations;
788         inode->i_fop = &btrfs_dir_file_operations;
789
790         err = btrfs_make_empty_dir(trans, inode, dir);
791         if (err)
792                 goto out_fail;
793         err = btrfs_add_link(trans, dentry, inode);
794         if (err)
795                 goto out_fail;
796         d_instantiate(dentry, inode);
797         drop_on_err = 0;
798
799 out_fail:
800         btrfs_end_transaction(trans, root);
801 out_unlock:
802         mutex_unlock(&root->fs_info->fs_mutex);
803         if (drop_on_err)
804                 iput(inode);
805         return err;
806 }
807
808 static int btrfs_sync_fs(struct super_block *sb, int wait)
809 {
810         struct btrfs_trans_handle *trans;
811         struct btrfs_root *root;
812         int ret;
813         root = btrfs_sb(sb);
814
815         sb->s_dirt = 0;
816         if (!wait) {
817                 filemap_flush(root->fs_info->btree_inode->i_mapping);
818                 return 0;
819         }
820         filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
821         mutex_lock(&root->fs_info->fs_mutex);
822         trans = btrfs_start_transaction(root, 1);
823         ret = btrfs_commit_transaction(trans, root);
824         sb->s_dirt = 0;
825         BUG_ON(ret);
826 printk("btrfs sync_fs\n");
827         mutex_unlock(&root->fs_info->fs_mutex);
828         return 0;
829 }
830
831 #if 0
832 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
833                            struct buffer_head *result, int create)
834 {
835         struct btrfs_root *root = btrfs_sb(inode->i_sb);
836         struct btrfs_path *path;
837         struct btrfs_key key;
838         struct btrfs_leaf *leaf;
839         int num_bytes = result->b_size;
840         int item_size;
841         int ret;
842         u64 pos;
843         char *ptr;
844         int copy_size;
845         int err = 0;
846         char *safe_ptr;
847         char *data_ptr;
848
849         path = btrfs_alloc_path();
850         BUG_ON(!path);
851
852         WARN_ON(create);
853         if (create) {
854                 return 0;
855         }
856         pos = iblock << inode->i_blkbits;
857         key.objectid = inode->i_ino;
858         key.flags = 0;
859         btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
860         ptr = kmap(result->b_page);
861         safe_ptr = ptr;
862         ptr += (pos & (PAGE_CACHE_SIZE -1));
863 again:
864         key.offset = pos;
865         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
866         if (ret) {
867                 if (ret < 0)
868                         err = ret;
869                 else
870                         err = 0;
871                 goto out;
872         }
873         leaf = btrfs_buffer_leaf(path->nodes[0]);
874         item_size = btrfs_item_size(leaf->items + path->slots[0]);
875         copy_size = min(num_bytes, item_size);
876         data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
877         WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
878         memcpy(ptr, data_ptr, copy_size);
879         pos += copy_size;
880         num_bytes -= copy_size;
881         WARN_ON(num_bytes < 0);
882         ptr += copy_size;
883         btrfs_release_path(root, path);
884         if (num_bytes != 0) {
885                 if (pos >= i_size_read(inode))
886                         memset(ptr, 0, num_bytes);
887                 else
888                         goto again;
889         }
890         set_buffer_uptodate(result);
891         map_bh(result, inode->i_sb, 0);
892         err = 0;
893 out:
894         btrfs_free_path(path);
895         kunmap(result->b_page);
896         return err;
897 }
898 #endif
899
900 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
901                            struct buffer_head *result, int create)
902 {
903         int ret;
904         int err = 0;
905         u64 blocknr;
906         u64 extent_start = 0;
907         u64 extent_end = 0;
908         u64 objectid = inode->i_ino;
909         struct btrfs_path *path;
910         struct btrfs_root *root = btrfs_sb(inode->i_sb);
911         struct btrfs_trans_handle *trans = NULL;
912         struct btrfs_file_extent_item *item;
913         struct btrfs_leaf *leaf;
914         struct btrfs_disk_key *found_key;
915
916         path = btrfs_alloc_path();
917         BUG_ON(!path);
918         btrfs_init_path(path);
919         if (create)
920                 trans = btrfs_start_transaction(root, 1);
921
922         ret = btrfs_lookup_file_extent(trans, root, path,
923                                        inode->i_ino,
924                                        iblock << inode->i_blkbits, 0);
925         if (ret < 0) {
926                 err = ret;
927                 goto out;
928         }
929
930         if (ret != 0) {
931                 if (path->slots[0] == 0) {
932                         btrfs_release_path(root, path);
933                         goto allocate;
934                 }
935                 path->slots[0]--;
936         }
937
938         item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
939                               struct btrfs_file_extent_item);
940         leaf = btrfs_buffer_leaf(path->nodes[0]);
941         blocknr = btrfs_file_extent_disk_blocknr(item);
942         blocknr += btrfs_file_extent_offset(item);
943
944         /* exact match found, use it */
945         if (ret == 0) {
946                 err = 0;
947                 map_bh(result, inode->i_sb, blocknr);
948                 goto out;
949         }
950
951         /* are we inside the extent that was found? */
952         found_key = &leaf->items[path->slots[0]].key;
953         if (btrfs_disk_key_objectid(found_key) != objectid ||
954             btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
955                 extent_end = 0;
956                 extent_start = 0;
957                 btrfs_release_path(root, path);
958                 goto allocate;
959         }
960
961         extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
962         extent_start = extent_start >> inode->i_blkbits;
963         extent_start += btrfs_file_extent_offset(item);
964         extent_end = extent_start + btrfs_file_extent_num_blocks(item);
965         if (iblock >= extent_start && iblock < extent_end) {
966                 err = 0;
967                 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
968                 goto out;
969         }
970 allocate:
971         /* ok, create a new extent */
972         if (!create) {
973                 err = 0;
974                 goto out;
975         }
976         ret = btrfs_alloc_file_extent(trans, root, objectid,
977                                       iblock << inode->i_blkbits,
978                                       1, extent_end, &blocknr);
979         if (ret) {
980                 err = ret;
981                 goto out;
982         }
983         inode->i_blocks += inode->i_sb->s_blocksize >> 9;
984         set_buffer_new(result);
985         map_bh(result, inode->i_sb, blocknr);
986
987 out:
988         btrfs_release_path(root, path);
989         btrfs_free_path(path);
990         if (trans)
991                 btrfs_end_transaction(trans, root);
992         return err;
993 }
994
995 static int btrfs_get_block(struct inode *inode, sector_t iblock,
996                            struct buffer_head *result, int create)
997 {
998         int err;
999         struct btrfs_root *root = btrfs_sb(inode->i_sb);
1000         mutex_lock(&root->fs_info->fs_mutex);
1001         err = btrfs_get_block_lock(inode, iblock, result, create);
1002         // err = btrfs_get_block_inline(inode, iblock, result, create);
1003         mutex_unlock(&root->fs_info->fs_mutex);
1004         return err;
1005 }
1006
1007 static int btrfs_prepare_write(struct file *file, struct page *page,
1008                                unsigned from, unsigned to)
1009 {
1010         return nobh_prepare_write(page, from, to, btrfs_get_block);
1011 }
1012 static int btrfs_commit_write(struct file *file, struct page *page,
1013                                unsigned from, unsigned to)
1014 {
1015         return nobh_commit_write(file, page, from, to);
1016 }
1017
1018 static void btrfs_write_super(struct super_block *sb)
1019 {
1020         btrfs_sync_fs(sb, 1);
1021 }
1022
1023 static int btrfs_readpage(struct file *file, struct page *page)
1024 {
1025         return mpage_readpage(page, btrfs_get_block);
1026 }
1027
1028 static int btrfs_readpages(struct file *file, struct address_space *mapping,
1029                            struct list_head *pages, unsigned nr_pages)
1030 {
1031         return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1032 }
1033
1034 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1035 {
1036         return nobh_writepage(page, btrfs_get_block, wbc);
1037 }
1038
1039 static void btrfs_truncate(struct inode *inode)
1040 {
1041         struct btrfs_root *root = btrfs_sb(inode->i_sb);
1042         int ret;
1043         struct btrfs_trans_handle *trans;
1044
1045         if (!S_ISREG(inode->i_mode))
1046                 return;
1047         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1048                 return;
1049
1050         nobh_truncate_page(inode->i_mapping, inode->i_size);
1051
1052         /* FIXME, add redo link to tree so we don't leak on crash */
1053         mutex_lock(&root->fs_info->fs_mutex);
1054         trans = btrfs_start_transaction(root, 1);
1055         ret = btrfs_truncate_in_trans(trans, root, inode);
1056         BUG_ON(ret);
1057         ret = btrfs_end_transaction(trans, root);
1058         BUG_ON(ret);
1059         mutex_unlock(&root->fs_info->fs_mutex);
1060         mark_inode_dirty(inode);
1061 }
1062
1063 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1064                                 struct page **prepared_pages,
1065                                 const char __user * buf)
1066 {
1067         long page_fault = 0;
1068         int i;
1069         int offset = pos & (PAGE_CACHE_SIZE - 1);
1070
1071         for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1072                 size_t count = min_t(size_t,
1073                                      PAGE_CACHE_SIZE - offset, write_bytes);
1074                 struct page *page = prepared_pages[i];
1075                 fault_in_pages_readable(buf, count);
1076
1077                 /* Copy data from userspace to the current page */
1078                 kmap(page);
1079                 page_fault = __copy_from_user(page_address(page) + offset,
1080                                               buf, count);
1081                 /* Flush processor's dcache for this page */
1082                 flush_dcache_page(page);
1083                 kunmap(page);
1084                 buf += count;
1085                 write_bytes -= count;
1086
1087                 if (page_fault)
1088                         break;
1089         }
1090         return page_fault ? -EFAULT : 0;
1091 }
1092
1093 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1094 {
1095         size_t i;
1096         for (i = 0; i < num_pages; i++) {
1097                 if (!pages[i])
1098                         break;
1099                 unlock_page(pages[i]);
1100                 mark_page_accessed(pages[i]);
1101                 page_cache_release(pages[i]);
1102         }
1103 }
1104 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1105                                    struct btrfs_root *root,
1106                                    struct file *file,
1107                                    struct page **pages,
1108                                    size_t num_pages,
1109                                    loff_t pos,
1110                                    size_t write_bytes)
1111 {
1112         int i;
1113         int offset;
1114         int err = 0;
1115         int ret;
1116         int this_write;
1117         struct inode *inode = file->f_path.dentry->d_inode;
1118
1119         for (i = 0; i < num_pages; i++) {
1120                 offset = pos & (PAGE_CACHE_SIZE -1);
1121                 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1122                 /* FIXME, one block at a time */
1123
1124                 mutex_lock(&root->fs_info->fs_mutex);
1125                 trans = btrfs_start_transaction(root, 1);
1126                 btrfs_csum_file_block(trans, root, inode->i_ino,
1127                                       pages[i]->index << PAGE_CACHE_SHIFT,
1128                                       kmap(pages[i]), PAGE_CACHE_SIZE);
1129                 kunmap(pages[i]);
1130                 SetPageChecked(pages[i]);
1131                 ret = btrfs_end_transaction(trans, root);
1132                 BUG_ON(ret);
1133                 mutex_unlock(&root->fs_info->fs_mutex);
1134
1135                 ret = nobh_commit_write(file, pages[i], offset,
1136                                          offset + this_write);
1137                 pos += this_write;
1138                 if (ret) {
1139                         err = ret;
1140                         goto failed;
1141                 }
1142                 WARN_ON(this_write > write_bytes);
1143                 write_bytes -= this_write;
1144         }
1145 failed:
1146         return err;
1147 }
1148
1149 static int prepare_pages(struct btrfs_trans_handle *trans,
1150                          struct btrfs_root *root,
1151                          struct file *file,
1152                          struct page **pages,
1153                          size_t num_pages,
1154                          loff_t pos,
1155                          size_t write_bytes)
1156 {
1157         int i;
1158         unsigned long index = pos >> PAGE_CACHE_SHIFT;
1159         struct inode *inode = file->f_path.dentry->d_inode;
1160         int offset;
1161         int err = 0;
1162         int ret;
1163         int this_write;
1164         loff_t isize = i_size_read(inode);
1165
1166         memset(pages, 0, num_pages * sizeof(struct page *));
1167
1168         for (i = 0; i < num_pages; i++) {
1169                 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1170                 if (!pages[i]) {
1171                         err = -ENOMEM;
1172                         goto failed_release;
1173                 }
1174                 offset = pos & (PAGE_CACHE_SIZE -1);
1175                 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1176                 ret = nobh_prepare_write(pages[i], offset,
1177                                          offset + this_write,
1178                                          btrfs_get_block);
1179                 pos += this_write;
1180                 if (ret) {
1181                         err = ret;
1182                         goto failed_truncate;
1183                 }
1184                 WARN_ON(this_write > write_bytes);
1185                 write_bytes -= this_write;
1186         }
1187         return 0;
1188
1189 failed_release:
1190         btrfs_drop_pages(pages, num_pages);
1191         return err;
1192
1193 failed_truncate:
1194         btrfs_drop_pages(pages, num_pages);
1195         if (pos > isize)
1196                 vmtruncate(inode, isize);
1197         return err;
1198 }
1199
1200 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1201                                 size_t count, loff_t *ppos)
1202 {
1203         loff_t pos;
1204         size_t num_written = 0;
1205         int err = 0;
1206         int ret = 0;
1207         struct inode *inode = file->f_path.dentry->d_inode;
1208         struct btrfs_root *root = btrfs_sb(inode->i_sb);
1209         struct page *pages[1];
1210
1211         if (file->f_flags & O_DIRECT)
1212                 return -EINVAL;
1213         pos = *ppos;
1214
1215         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1216         current->backing_dev_info = inode->i_mapping->backing_dev_info;
1217         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1218         if (err)
1219                 goto out;
1220         if (count == 0)
1221                 goto out;
1222         err = remove_suid(file->f_path.dentry);
1223         if (err)
1224                 goto out;
1225         file_update_time(file);
1226         mutex_lock(&inode->i_mutex);
1227         while(count > 0) {
1228                 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1229                 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1230                 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1231                                         PAGE_CACHE_SHIFT;
1232                 ret = prepare_pages(NULL, root, file, pages, num_pages,
1233                                     pos, write_bytes);
1234                 BUG_ON(ret);
1235                 ret = btrfs_copy_from_user(pos, num_pages,
1236                                            write_bytes, pages, buf);
1237                 BUG_ON(ret);
1238
1239                 ret = dirty_and_release_pages(NULL, root, file, pages,
1240                                               num_pages, pos, write_bytes);
1241                 BUG_ON(ret);
1242                 btrfs_drop_pages(pages, num_pages);
1243
1244                 buf += write_bytes;
1245                 count -= write_bytes;
1246                 pos += write_bytes;
1247                 num_written += write_bytes;
1248
1249                 balance_dirty_pages_ratelimited(inode->i_mapping);
1250                 cond_resched();
1251         }
1252         mutex_unlock(&inode->i_mutex);
1253 out:
1254         *ppos = pos;
1255         current->backing_dev_info = NULL;
1256         return num_written ? num_written : err;
1257 }
1258
1259 #if 0
1260 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1261                            struct page *page, loff_t pos,
1262                            size_t offset, size_t write_bytes)
1263 {
1264         struct btrfs_path *path;
1265         struct btrfs_trans_handle *trans;
1266         struct btrfs_key key;
1267         struct btrfs_leaf *leaf;
1268         struct btrfs_key found_key;
1269         int ret;
1270         size_t copy_size = 0;
1271         char *dst = NULL;
1272         int err = 0;
1273         size_t num_written = 0;
1274
1275         path = btrfs_alloc_path();
1276         BUG_ON(!path);
1277         mutex_lock(&root->fs_info->fs_mutex);
1278         trans = btrfs_start_transaction(root, 1);
1279         key.objectid = inode->i_ino;
1280         key.flags = 0;
1281         btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1282
1283 again:
1284         key.offset = pos;
1285         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1286         if (ret < 0) {
1287                 err = ret;
1288                 goto out;
1289         }
1290         if (ret == 0) {
1291                 leaf = btrfs_buffer_leaf(path->nodes[0]);
1292                 btrfs_disk_key_to_cpu(&found_key,
1293                                       &leaf->items[path->slots[0]].key);
1294                 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1295                 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1296                 copy_size = min(write_bytes, copy_size);
1297                 goto copyit;
1298         } else {
1299                 int slot = path->slots[0];
1300                 if (slot > 0) {
1301                         slot--;
1302                 }
1303                 // FIXME find max key
1304                 leaf = btrfs_buffer_leaf(path->nodes[0]);
1305                 btrfs_disk_key_to_cpu(&found_key,
1306                                       &leaf->items[slot].key);
1307                 if (found_key.objectid != inode->i_ino)
1308                         goto insert;
1309                 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1310                         goto insert;
1311                 copy_size = btrfs_item_size(leaf->items + slot);
1312                 if (found_key.offset + copy_size <= pos)
1313                         goto insert;
1314                 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1315                 dst += pos - found_key.offset;
1316                 copy_size = copy_size - (pos - found_key.offset);
1317                 BUG_ON(copy_size < 0);
1318                 copy_size = min(write_bytes, copy_size);
1319                 WARN_ON(copy_size == 0);
1320                 goto copyit;
1321         }
1322 insert:
1323         btrfs_release_path(root, path);
1324         copy_size = min(write_bytes,
1325                         (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1326                         sizeof(struct btrfs_item) * 4);
1327         ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1328         BUG_ON(ret);
1329         dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1330                              path->slots[0], char);
1331 copyit:
1332         WARN_ON(copy_size == 0);
1333         WARN_ON(dst + copy_size >
1334                 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1335                                                  path->slots[0], char) +
1336                 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1337                                                   path->slots[0]));
1338         btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1339                      page_address(page) + offset, copy_size);
1340         mark_buffer_dirty(path->nodes[0]);
1341         btrfs_release_path(root, path);
1342         pos += copy_size;
1343         offset += copy_size;
1344         num_written += copy_size;
1345         write_bytes -= copy_size;
1346         if (write_bytes)
1347                 goto again;
1348 out:
1349         btrfs_free_path(path);
1350         ret = btrfs_end_transaction(trans, root);
1351         BUG_ON(ret);
1352         mutex_unlock(&root->fs_info->fs_mutex);
1353         return num_written ? num_written : err;
1354 }
1355
1356 static ssize_t btrfs_file_inline_write(struct file *file,
1357                                        const char __user *buf,
1358                                        size_t count, loff_t *ppos)
1359 {
1360         loff_t pos;
1361         size_t num_written = 0;
1362         int err = 0;
1363         int ret = 0;
1364         struct inode *inode = file->f_path.dentry->d_inode;
1365         struct btrfs_root *root = btrfs_sb(inode->i_sb);
1366         unsigned long page_index;
1367
1368         if (file->f_flags & O_DIRECT)
1369                 return -EINVAL;
1370         pos = *ppos;
1371
1372         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1373         current->backing_dev_info = inode->i_mapping->backing_dev_info;
1374         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1375         if (err)
1376                 goto out;
1377         if (count == 0)
1378                 goto out;
1379         err = remove_suid(file->f_path.dentry);
1380         if (err)
1381                 goto out;
1382         file_update_time(file);
1383         mutex_lock(&inode->i_mutex);
1384         while(count > 0) {
1385                 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1386                 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1387                 struct page *page;
1388
1389                 page_index = pos >> PAGE_CACHE_SHIFT;
1390                 page = grab_cache_page(inode->i_mapping, page_index);
1391                 if (!PageUptodate(page)) {
1392                         ret = mpage_readpage(page, btrfs_get_block);
1393                         BUG_ON(ret);
1394                         lock_page(page);
1395                 }
1396                 ret = btrfs_copy_from_user(pos, 1,
1397                                            write_bytes, &page, buf);
1398                 BUG_ON(ret);
1399                 write_bytes = inline_one_page(root, inode, page, pos,
1400                                       offset, write_bytes);
1401                 SetPageUptodate(page);
1402                 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1403                         i_size_write(inode, pos + write_bytes);
1404                         mark_inode_dirty(inode);
1405                 }
1406                 page_cache_release(page);
1407                 unlock_page(page);
1408                 if (write_bytes < 0)
1409                         goto out_unlock;
1410                 buf += write_bytes;
1411                 count -= write_bytes;
1412                 pos += write_bytes;
1413                 num_written += write_bytes;
1414
1415                 balance_dirty_pages_ratelimited(inode->i_mapping);
1416                 cond_resched();
1417         }
1418 out_unlock:
1419         mutex_unlock(&inode->i_mutex);
1420 out:
1421         *ppos = pos;
1422         current->backing_dev_info = NULL;
1423         return num_written ? num_written : err;
1424 }
1425 #endif
1426
1427 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1428                         unsigned long offset, unsigned long size)
1429 {
1430         char *kaddr;
1431         unsigned long left, count = desc->count;
1432
1433         if (size > count)
1434                 size = count;
1435
1436         if (!PageChecked(page)) {
1437                 /* FIXME, do it per block */
1438                 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1439                 int ret = btrfs_csum_verify_file_block(root,
1440                                           page->mapping->host->i_ino,
1441                                           page->index << PAGE_CACHE_SHIFT,
1442                                           kmap(page), PAGE_CACHE_SIZE);
1443                 if (ret) {
1444                         printk("failed to verify ino %lu page %lu\n",
1445                                page->mapping->host->i_ino,
1446                                page->index);
1447                         memset(page_address(page), 0, PAGE_CACHE_SIZE);
1448                 }
1449                 SetPageChecked(page);
1450                 kunmap(page);
1451         }
1452         /*
1453          * Faults on the destination of a read are common, so do it before
1454          * taking the kmap.
1455          */
1456         if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1457                 kaddr = kmap_atomic(page, KM_USER0);
1458                 left = __copy_to_user_inatomic(desc->arg.buf,
1459                                                 kaddr + offset, size);
1460                 kunmap_atomic(kaddr, KM_USER0);
1461                 if (left == 0)
1462                         goto success;
1463         }
1464
1465         /* Do it the slow way */
1466         kaddr = kmap(page);
1467         left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1468         kunmap(page);
1469
1470         if (left) {
1471                 size -= left;
1472                 desc->error = -EFAULT;
1473         }
1474 success:
1475         desc->count = count - size;
1476         desc->written += size;
1477         desc->arg.buf += size;
1478         return size;
1479 }
1480
1481 /**
1482  * btrfs_file_aio_read - filesystem read routine
1483  * @iocb:       kernel I/O control block
1484  * @iov:        io vector request
1485  * @nr_segs:    number of segments in the iovec
1486  * @pos:        current file position
1487  */
1488 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1489                                    unsigned long nr_segs, loff_t pos)
1490 {
1491         struct file *filp = iocb->ki_filp;
1492         ssize_t retval;
1493         unsigned long seg;
1494         size_t count;
1495         loff_t *ppos = &iocb->ki_pos;
1496
1497         count = 0;
1498         for (seg = 0; seg < nr_segs; seg++) {
1499                 const struct iovec *iv = &iov[seg];
1500
1501                 /*
1502                  * If any segment has a negative length, or the cumulative
1503                  * length ever wraps negative then return -EINVAL.
1504                  */
1505                 count += iv->iov_len;
1506                 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1507                         return -EINVAL;
1508                 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1509                         continue;
1510                 if (seg == 0)
1511                         return -EFAULT;
1512                 nr_segs = seg;
1513                 count -= iv->iov_len;   /* This segment is no good */
1514                 break;
1515         }
1516         retval = 0;
1517         if (count) {
1518                 for (seg = 0; seg < nr_segs; seg++) {
1519                         read_descriptor_t desc;
1520
1521                         desc.written = 0;
1522                         desc.arg.buf = iov[seg].iov_base;
1523                         desc.count = iov[seg].iov_len;
1524                         if (desc.count == 0)
1525                                 continue;
1526                         desc.error = 0;
1527                         do_generic_file_read(filp, ppos, &desc,
1528                                              btrfs_read_actor);
1529                         retval += desc.written;
1530                         if (desc.error) {
1531                                 retval = retval ?: desc.error;
1532                                 break;
1533                         }
1534                 }
1535         }
1536         return retval;
1537 }
1538
1539 static struct kmem_cache *btrfs_inode_cachep;
1540 struct kmem_cache *btrfs_trans_handle_cachep;
1541 struct kmem_cache *btrfs_transaction_cachep;
1542 struct kmem_cache *btrfs_bit_radix_cachep;
1543 struct kmem_cache *btrfs_path_cachep;
1544
1545 /*
1546  * Called inside transaction, so use GFP_NOFS
1547  */
1548 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1549 {
1550         struct btrfs_inode *ei;
1551
1552         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1553         if (!ei)
1554                 return NULL;
1555         ei->magic = 0xDEADBEEF;
1556         ei->magic2 = 0xDEADBEAF;
1557         return &ei->vfs_inode;
1558 }
1559
1560 static void btrfs_destroy_inode(struct inode *inode)
1561 {
1562         struct btrfs_inode *ei = BTRFS_I(inode);
1563         WARN_ON(ei->magic != 0xDEADBEEF);
1564         WARN_ON(ei->magic2 != 0xDEADBEAF);
1565         WARN_ON(!list_empty(&inode->i_dentry));
1566         WARN_ON(inode->i_data.nrpages);
1567
1568         ei->magic = 0;
1569         ei->magic2 = 0;
1570         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1571 }
1572
1573 static void init_once(void * foo, struct kmem_cache * cachep,
1574                       unsigned long flags)
1575 {
1576         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1577
1578         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1579             SLAB_CTOR_CONSTRUCTOR) {
1580                 inode_init_once(&ei->vfs_inode);
1581         }
1582 }
1583
1584 static int init_inodecache(void)
1585 {
1586         btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1587                                              sizeof(struct btrfs_inode),
1588                                              0, (SLAB_RECLAIM_ACCOUNT|
1589                                                 SLAB_MEM_SPREAD),
1590                                              init_once, NULL);
1591         btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1592                                              sizeof(struct btrfs_trans_handle),
1593                                              0, (SLAB_RECLAIM_ACCOUNT|
1594                                                 SLAB_MEM_SPREAD),
1595                                              NULL, NULL);
1596         btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1597                                              sizeof(struct btrfs_transaction),
1598                                              0, (SLAB_RECLAIM_ACCOUNT|
1599                                                 SLAB_MEM_SPREAD),
1600                                              NULL, NULL);
1601         btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1602                                              sizeof(struct btrfs_transaction),
1603                                              0, (SLAB_RECLAIM_ACCOUNT|
1604                                                 SLAB_MEM_SPREAD),
1605                                              NULL, NULL);
1606         btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1607                                              256,
1608                                              0, (SLAB_RECLAIM_ACCOUNT|
1609                                                 SLAB_MEM_SPREAD |
1610                                                 SLAB_DESTROY_BY_RCU),
1611                                              NULL, NULL);
1612         if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1613             btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1614                 return -ENOMEM;
1615         return 0;
1616 }
1617
1618 static void destroy_inodecache(void)
1619 {
1620         kmem_cache_destroy(btrfs_inode_cachep);
1621         kmem_cache_destroy(btrfs_trans_handle_cachep);
1622         kmem_cache_destroy(btrfs_transaction_cachep);
1623         kmem_cache_destroy(btrfs_bit_radix_cachep);
1624         kmem_cache_destroy(btrfs_path_cachep);
1625 }
1626
1627 static int btrfs_get_sb(struct file_system_type *fs_type,
1628         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1629 {
1630         return get_sb_bdev(fs_type, flags, dev_name, data,
1631                            btrfs_fill_super, mnt);
1632 }
1633
1634 static struct file_system_type btrfs_fs_type = {
1635         .owner          = THIS_MODULE,
1636         .name           = "btrfs",
1637         .get_sb         = btrfs_get_sb,
1638         .kill_sb        = kill_block_super,
1639         .fs_flags       = FS_REQUIRES_DEV,
1640 };
1641
1642 static struct super_operations btrfs_super_ops = {
1643         .statfs         = simple_statfs,
1644         .delete_inode   = btrfs_delete_inode,
1645         .put_super      = btrfs_put_super,
1646         .read_inode     = btrfs_read_locked_inode,
1647         .write_super    = btrfs_write_super,
1648         .sync_fs        = btrfs_sync_fs,
1649         .write_inode    = btrfs_write_inode,
1650         .alloc_inode    = btrfs_alloc_inode,
1651         .destroy_inode  = btrfs_destroy_inode,
1652 };
1653
1654 static struct inode_operations btrfs_dir_inode_operations = {
1655         .lookup         = btrfs_lookup,
1656         .create         = btrfs_create,
1657         .unlink         = btrfs_unlink,
1658         .mkdir          = btrfs_mkdir,
1659         .rmdir          = btrfs_rmdir,
1660 };
1661
1662 static struct file_operations btrfs_dir_file_operations = {
1663         .llseek         = generic_file_llseek,
1664         .read           = generic_read_dir,
1665         .readdir        = btrfs_readdir,
1666 };
1667
1668 static struct address_space_operations btrfs_aops = {
1669         .readpage       = btrfs_readpage,
1670         .readpages      = btrfs_readpages,
1671         .writepage      = btrfs_writepage,
1672         .sync_page      = block_sync_page,
1673         .prepare_write  = btrfs_prepare_write,
1674         .commit_write   = btrfs_commit_write,
1675 };
1676
1677 static struct inode_operations btrfs_file_inode_operations = {
1678         .truncate       = btrfs_truncate,
1679 };
1680
1681 static struct file_operations btrfs_file_operations = {
1682         .llseek         = generic_file_llseek,
1683         .read           = do_sync_read,
1684         .aio_read       = btrfs_file_aio_read,
1685         .write          = btrfs_file_write,
1686         .mmap           = generic_file_mmap,
1687         .open           = generic_file_open,
1688 };
1689
1690 static int __init init_btrfs_fs(void)
1691 {
1692         int err;
1693         printk("btrfs loaded!\n");
1694         err = init_inodecache();
1695         if (err)
1696                 return err;
1697         return register_filesystem(&btrfs_fs_type);
1698 }
1699
1700 static void __exit exit_btrfs_fs(void)
1701 {
1702         destroy_inodecache();
1703         unregister_filesystem(&btrfs_fs_type);
1704         printk("btrfs unloaded\n");
1705 }
1706
1707 module_init(init_btrfs_fs)
1708 module_exit(exit_btrfs_fs)
1709
1710 MODULE_LICENSE("GPL");