]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/btrfs/disk-io.c
Btrfs: use a btree inode instead of sb_getblk
[mv-sheeva.git] / fs / btrfs / disk-io.c
1 #include <linux/module.h>
2 #include <linux/fs.h>
3 #include <linux/blkdev.h>
4 #include "ctree.h"
5 #include "disk-io.h"
6 #include "transaction.h"
7
8
9 static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
10 {
11         struct btrfs_node *node = btrfs_buffer_node(buf);
12         if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) {
13                 BUG();
14         }
15         if (root->node && btrfs_header_parentid(&node->header) !=
16             btrfs_header_parentid(btrfs_buffer_header(root->node))) {
17                 BUG();
18         }
19         return 0;
20 }
21
22 struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr)
23 {
24         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
25         int blockbits = root->fs_info->sb->s_blocksize_bits;
26         unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
27         struct page *page;
28         struct buffer_head *bh;
29         struct buffer_head *head;
30         struct buffer_head *ret = NULL;
31
32         page = find_lock_page(mapping, index);
33         if (!page)
34                 return NULL;
35
36         if (!page_has_buffers(page))
37                 goto out_unlock;
38
39         head = page_buffers(page);
40         bh = head;
41         do {
42                 if (buffer_mapped(bh) && bh->b_blocknr == blocknr) {
43                         ret = bh;
44                         get_bh(bh);
45                         goto out_unlock;
46                 }
47                 bh = bh->b_this_page;
48         } while (bh != head);
49 out_unlock:
50         unlock_page(page);
51         page_cache_release(page);
52         return ret;
53 }
54
55 struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root,
56                                                  u64 blocknr)
57 {
58         struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
59         int blockbits = root->fs_info->sb->s_blocksize_bits;
60         unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits);
61         struct page *page;
62         struct buffer_head *bh;
63         struct buffer_head *head;
64         struct buffer_head *ret = NULL;
65         u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits);
66         page = grab_cache_page(mapping, index);
67         if (!page)
68                 return NULL;
69
70         wait_on_page_writeback(page);
71         if (!page_has_buffers(page))
72                 create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0);
73         head = page_buffers(page);
74         bh = head;
75         do {
76                 if (!buffer_mapped(bh)) {
77                         bh->b_bdev = root->fs_info->sb->s_bdev;
78                         bh->b_blocknr = first_block;
79                         set_buffer_mapped(bh);
80                 }
81                 if (bh->b_blocknr == blocknr) {
82                         ret = bh;
83                         get_bh(bh);
84                         goto out_unlock;
85                 }
86                 bh = bh->b_this_page;
87                 first_block++;
88         } while (bh != head);
89 out_unlock:
90         unlock_page(page);
91         page_cache_release(page);
92         return ret;
93 }
94
95 static sector_t max_block(struct block_device *bdev)
96 {
97         sector_t retval = ~((sector_t)0);
98         loff_t sz = i_size_read(bdev->bd_inode);
99
100         if (sz) {
101                 unsigned int size = block_size(bdev);
102                 unsigned int sizebits = blksize_bits(size);
103                 retval = (sz >> sizebits);
104         }
105         return retval;
106 }
107
108 static int btree_get_block(struct inode *inode, sector_t iblock,
109                            struct buffer_head *bh, int create)
110 {
111         if (iblock >= max_block(inode->i_sb->s_bdev)) {
112                 if (create)
113                         return -EIO;
114
115                 /*
116                  * for reads, we're just trying to fill a partial page.
117                  * return a hole, they will have to call get_block again
118                  * before they can fill it, and they will get -EIO at that
119                  * time
120                  */
121                 return 0;
122         }
123         bh->b_bdev = inode->i_sb->s_bdev;
124         bh->b_blocknr = iblock;
125         set_buffer_mapped(bh);
126         return 0;
127 }
128
129 static int btree_writepage(struct page *page, struct writeback_control *wbc)
130 {
131         return block_write_full_page(page, btree_get_block, wbc);
132 }
133
134 static int btree_readpage(struct file * file, struct page * page)
135 {
136         return block_read_full_page(page, btree_get_block);
137 }
138
139 static struct address_space_operations btree_aops = {
140         .readpage       = btree_readpage,
141         .writepage      = btree_writepage,
142         .sync_page      = block_sync_page,
143 };
144
145 struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
146 {
147         struct buffer_head *bh = NULL;
148
149         bh = btrfs_find_create_tree_block(root, blocknr);
150         if (!bh)
151                 return bh;
152         lock_buffer(bh);
153         if (!buffer_uptodate(bh)) {
154                 get_bh(bh);
155                 bh->b_end_io = end_buffer_read_sync;
156                 submit_bh(READ, bh);
157                 wait_on_buffer(bh);
158                 if (!buffer_uptodate(bh))
159                         goto fail;
160         } else {
161                 unlock_buffer(bh);
162         }
163         if (check_tree_block(root, bh))
164                 BUG();
165         return bh;
166 fail:
167         brelse(bh);
168         return NULL;
169
170 }
171
172 int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
173                      struct buffer_head *buf)
174 {
175         mark_buffer_dirty(buf);
176         return 0;
177 }
178
179 int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
180                      struct buffer_head *buf)
181 {
182         clear_buffer_dirty(buf);
183         return 0;
184 }
185
186 static int __setup_root(struct btrfs_super_block *super,
187                         struct btrfs_root *root,
188                         struct btrfs_fs_info *fs_info,
189                         u64 objectid)
190 {
191         root->node = NULL;
192         root->commit_root = NULL;
193         root->blocksize = btrfs_super_blocksize(super);
194         root->ref_cows = 0;
195         root->fs_info = fs_info;
196         memset(&root->root_key, 0, sizeof(root->root_key));
197         memset(&root->root_item, 0, sizeof(root->root_item));
198         return 0;
199 }
200
201 static int find_and_setup_root(struct btrfs_super_block *super,
202                                struct btrfs_root *tree_root,
203                                struct btrfs_fs_info *fs_info,
204                                u64 objectid,
205                                struct btrfs_root *root)
206 {
207         int ret;
208
209         __setup_root(super, root, fs_info, objectid);
210         ret = btrfs_find_last_root(tree_root, objectid,
211                                    &root->root_item, &root->root_key);
212         BUG_ON(ret);
213
214         root->node = read_tree_block(root,
215                                      btrfs_root_blocknr(&root->root_item));
216         BUG_ON(!root->node);
217         return 0;
218 }
219
220 struct btrfs_root *open_ctree(struct super_block *sb,
221                               struct buffer_head *sb_buffer,
222                               struct btrfs_super_block *disk_super)
223 {
224         struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
225                                           GFP_NOFS);
226         struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
227                                                  GFP_NOFS);
228         struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
229                                                GFP_NOFS);
230         struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
231                                                 GFP_NOFS);
232         struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
233                                                 GFP_NOFS);
234         int ret;
235
236         if (!btrfs_super_root(disk_super))
237                 return NULL;
238         init_bit_radix(&fs_info->pinned_radix);
239         init_bit_radix(&fs_info->pending_del_radix);
240         sb_set_blocksize(sb, sb_buffer->b_size);
241         fs_info->running_transaction = NULL;
242         fs_info->fs_root = root;
243         fs_info->tree_root = tree_root;
244         fs_info->extent_root = extent_root;
245         fs_info->inode_root = inode_root;
246         fs_info->last_inode_alloc = 0;
247         fs_info->last_inode_alloc_dirid = 0;
248         fs_info->disk_super = disk_super;
249         fs_info->sb = sb;
250         fs_info->btree_inode = new_inode(sb);
251         fs_info->btree_inode->i_ino = 1;
252         fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
253         fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
254         mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
255
256         mutex_init(&fs_info->trans_mutex);
257         mutex_init(&fs_info->fs_mutex);
258         memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
259         memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
260
261         __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
262
263         fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr);
264
265         if (!fs_info->sb_buffer)
266                 return NULL;
267
268         brelse(sb_buffer);
269         sb_buffer = NULL;
270         disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data;
271         fs_info->disk_super = disk_super;
272
273         tree_root->node = read_tree_block(tree_root,
274                                           btrfs_super_root(disk_super));
275         BUG_ON(!tree_root->node);
276
277         ret = find_and_setup_root(disk_super, tree_root, fs_info,
278                                   BTRFS_EXTENT_TREE_OBJECTID, extent_root);
279         BUG_ON(ret);
280
281         ret = find_and_setup_root(disk_super, tree_root, fs_info,
282                                   BTRFS_INODE_MAP_OBJECTID, inode_root);
283         BUG_ON(ret);
284
285         ret = find_and_setup_root(disk_super, tree_root, fs_info,
286                                   BTRFS_FS_TREE_OBJECTID, root);
287         BUG_ON(ret);
288         root->commit_root = root->node;
289         get_bh(root->node);
290         root->ref_cows = 1;
291         root->fs_info->generation = root->root_key.offset + 1;
292         return root;
293 }
294
295 int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
296                       *root)
297 {
298         struct buffer_head *bh = root->fs_info->sb_buffer;
299         btrfs_set_super_root(root->fs_info->disk_super,
300                              root->fs_info->tree_root->node->b_blocknr);
301         lock_buffer(bh);
302         clear_buffer_dirty(bh);
303         bh->b_end_io = end_buffer_write_sync;
304         get_bh(bh);
305         submit_bh(WRITE, bh);
306         wait_on_buffer(bh);
307         if (!buffer_uptodate(bh)) {
308                 WARN_ON(1);
309                 return -EIO;
310         }
311         return 0;
312 }
313
314 int close_ctree(struct btrfs_root *root)
315 {
316         int ret;
317         struct btrfs_trans_handle *trans;
318
319         trans = btrfs_start_transaction(root, 1);
320         btrfs_commit_transaction(trans, root);
321         /* run commit again to  drop the original snapshot */
322         trans = btrfs_start_transaction(root, 1);
323         btrfs_commit_transaction(trans, root);
324         ret = btrfs_write_and_wait_transaction(NULL, root);
325         BUG_ON(ret);
326         write_ctree_super(NULL, root);
327
328         if (root->node)
329                 btrfs_block_release(root, root->node);
330         if (root->fs_info->extent_root->node)
331                 btrfs_block_release(root->fs_info->extent_root,
332                                     root->fs_info->extent_root->node);
333         if (root->fs_info->inode_root->node)
334                 btrfs_block_release(root->fs_info->inode_root,
335                                     root->fs_info->inode_root->node);
336         if (root->fs_info->tree_root->node)
337                 btrfs_block_release(root->fs_info->tree_root,
338                                     root->fs_info->tree_root->node);
339         btrfs_block_release(root, root->commit_root);
340         btrfs_block_release(root, root->fs_info->sb_buffer);
341         iput(root->fs_info->btree_inode);
342         kfree(root->fs_info->extent_root);
343         kfree(root->fs_info->inode_root);
344         kfree(root->fs_info->tree_root);
345         kfree(root->fs_info);
346         kfree(root);
347         return 0;
348 }
349
350 void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
351 {
352         brelse(buf);
353 }
354