]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/btrfs/disk-io.c
Btrfs: transaction rework
[mv-sheeva.git] / fs / btrfs / disk-io.c
1 #include <linux/module.h>
2 #include <linux/fs.h>
3 #include "ctree.h"
4 #include "disk-io.h"
5 #include "transaction.h"
6
7 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
8 {
9         struct btrfs_node *node = btrfs_buffer_node(buf);
10         if (buf->b_blocknr != btrfs_header_blocknr(&node->header))
11                 BUG();
12         if (root->node && btrfs_header_parentid(&node->header) !=
13             btrfs_header_parentid(btrfs_buffer_header(root->node)))
14                 BUG();
15         return 0;
16 }
17
18 struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
19 {
20         return sb_getblk(root->fs_info->sb, blocknr);
21 }
22
23 struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
24 {
25         return sb_getblk(root->fs_info->sb, blocknr);
26 }
27
28 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
29 {
30         struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
31
32         if (!buf)
33                 return buf;
34         if (check_tree_block(root, buf))
35                 BUG();
36         return buf;
37 }
38
39 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40                      struct buffer_head *buf)
41 {
42         mark_buffer_dirty(buf);
43         return 0;
44 }
45
46 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
47                      struct buffer_head *buf)
48 {
49         clear_buffer_dirty(buf);
50         return 0;
51 }
52
53 static int __setup_root(struct btrfs_super_block *super,
54                         struct btrfs_root *root,
55                         struct btrfs_fs_info *fs_info,
56                         u64 objectid)
57 {
58         root->node = NULL;
59         root->commit_root = NULL;
60         root->blocksize = btrfs_super_blocksize(super);
61         root->ref_cows = 0;
62         root->fs_info = fs_info;
63         memset(&root->root_key, 0, sizeof(root->root_key));
64         memset(&root->root_item, 0, sizeof(root->root_item));
65         return 0;
66 }
67
68 static int find_and_setup_root(struct btrfs_super_block *super,
69                                struct btrfs_root *tree_root,
70                                struct btrfs_fs_info *fs_info,
71                                u64 objectid,
72                                struct btrfs_root *root)
73 {
74         int ret;
75
76         __setup_root(super, root, fs_info, objectid);
77         ret = btrfs_find_last_root(tree_root, objectid,
78                                    &root->root_item, &root->root_key);
79         BUG_ON(ret);
80
81         root->node = read_tree_block(root,
82                                      btrfs_root_blocknr(&root->root_item));
83         BUG_ON(!root->node);
84         return 0;
85 }
86
87 struct btrfs_root *open_ctree(struct super_block *sb,
88                               struct buffer_head *sb_buffer,
89                               struct btrfs_super_block *disk_super)
90 {
91         struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
92                                           GFP_NOFS);
93         struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
94                                                  GFP_NOFS);
95         struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
96                                                GFP_NOFS);
97         struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
98                                                 GFP_NOFS);
99         struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
100                                                 GFP_NOFS);
101         int ret;
102
103         /* FIXME: don't be stupid */
104         if (!btrfs_super_root(disk_super))
105                 return NULL;
106         INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
107         fs_info->running_transaction = NULL;
108         fs_info->fs_root = root;
109         fs_info->tree_root = tree_root;
110         fs_info->extent_root = extent_root;
111         fs_info->inode_root = inode_root;
112         fs_info->last_inode_alloc = 0;
113         fs_info->last_inode_alloc_dirid = 0;
114         fs_info->disk_super = disk_super;
115         fs_info->sb_buffer = sb_buffer;
116         fs_info->sb = sb;
117         mutex_init(&fs_info->trans_mutex);
118         memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
119         memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
120
121         __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
122         tree_root->node = read_tree_block(tree_root,
123                                           btrfs_super_root(disk_super));
124         BUG_ON(!tree_root->node);
125
126         ret = find_and_setup_root(disk_super, tree_root, fs_info,
127                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
128         BUG_ON(ret);
129
130         ret = find_and_setup_root(disk_super, tree_root, fs_info,
131                                   BTRFS_INODE_MAP_OBJECTID, inode_root);
132         BUG_ON(ret);
133
134         ret = find_and_setup_root(disk_super, tree_root, fs_info,
135                                   BTRFS_FS_TREE_OBJECTID, root);
136         BUG_ON(ret);
137
138         root->commit_root = root->node;
139         get_bh(root->node);
140         root->ref_cows = 1;
141         root->fs_info->generation = root->root_key.offset + 1;
142         return root;
143 }
144
145 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
146                       *root)
147 {
148         return 0;
149 #if 0
150         int ret;
151         btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);
152
153         ret = pwrite(root->fs_info->fp, s, sizeof(*s),
154                      BTRFS_SUPER_INFO_OFFSET);
155         if (ret != sizeof(*s)) {
156                 fprintf(stderr, "failed to write new super block err %d\n", ret);
157                 return ret;
158         }
159         return 0;
160 #endif
161 }
162
163 int close_ctree(struct btrfs_root *root)
164 {
165         int ret;
166         struct btrfs_trans_handle *trans;
167
168         trans = btrfs_start_transaction(root, 1);
169         btrfs_commit_transaction(trans, root);
170         /* run commit again to  drop the original snapshot */
171         trans = btrfs_start_transaction(root, 1);
172         btrfs_commit_transaction(trans, root);
173         ret = btrfs_write_and_wait_transaction(NULL, root);
174         BUG_ON(ret);
175         write_ctree_super(NULL, root);
176
177         if (root->node)
178                 btrfs_block_release(root, root->node);
179         if (root->fs_info->extent_root->node)
180                 btrfs_block_release(root->fs_info->extent_root,
181                                     root->fs_info->extent_root->node);
182         if (root->fs_info->inode_root->node)
183                 btrfs_block_release(root->fs_info->inode_root,
184                                     root->fs_info->inode_root->node);
185         if (root->fs_info->tree_root->node)
186                 btrfs_block_release(root->fs_info->tree_root,
187                                     root->fs_info->tree_root->node);
188         btrfs_block_release(root, root->commit_root);
189         btrfs_block_release(root, root->fs_info->sb_buffer);
190         kfree(root->fs_info->extent_root);
191         kfree(root->fs_info->inode_root);
192         kfree(root->fs_info->tree_root);
193         kfree(root->fs_info);
194         kfree(root);
195         return 0;
196 }
197
198 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
199 {
200         brelse(buf);
201 }
202