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