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