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