Commit | Line | Data |
---|---|---|
e20d96d6 CM |
1 | #include <linux/module.h> |
2 | #include <linux/fs.h> | |
d98237b3 | 3 | #include <linux/blkdev.h> |
87cbda5c CM |
4 | #include <linux/crypto.h> |
5 | #include <linux/scatterlist.h> | |
22b0ebda | 6 | #include <linux/swap.h> |
0f7d52f4 | 7 | #include <linux/radix-tree.h> |
eb60ceac CM |
8 | #include "ctree.h" |
9 | #include "disk-io.h" | |
e089f05c | 10 | #include "transaction.h" |
0f7d52f4 | 11 | #include "btrfs_inode.h" |
eb60ceac | 12 | |
7eccb903 CM |
13 | struct dev_lookup { |
14 | u64 block_start; | |
15 | u64 num_blocks; | |
16 | struct block_device *bdev; | |
17 | }; | |
18 | ||
8352d8a4 CM |
19 | int btrfs_insert_dev_radix(struct btrfs_root *root, |
20 | struct block_device *bdev, | |
21 | u64 block_start, | |
22 | u64 num_blocks) | |
23 | { | |
24 | struct dev_lookup *lookup; | |
25 | char b[BDEVNAME_SIZE]; | |
26 | int ret; | |
27 | ||
28 | lookup = kmalloc(sizeof(*lookup), GFP_NOFS); | |
29 | if (!lookup) | |
30 | return -ENOMEM; | |
31 | lookup->block_start = block_start; | |
32 | lookup->num_blocks = num_blocks; | |
33 | lookup->bdev = bdev; | |
34 | printk("inserting %s into dev radix %Lu %Lu\n", bdevname(bdev, b), block_start, num_blocks); | |
35 | ||
36 | ret = radix_tree_insert(&root->fs_info->dev_radix, block_start + | |
37 | num_blocks - 1, lookup); | |
38 | return ret; | |
39 | } | |
40 | ||
7eccb903 CM |
41 | u64 bh_blocknr(struct buffer_head *bh) |
42 | { | |
43 | int blkbits = bh->b_page->mapping->host->i_blkbits; | |
44 | u64 blocknr = bh->b_page->index << (PAGE_CACHE_SHIFT - blkbits); | |
45 | unsigned long offset; | |
46 | ||
47 | if (PageHighMem(bh->b_page)) | |
48 | offset = (unsigned long)bh->b_data; | |
49 | else | |
50 | offset = bh->b_data - (char *)page_address(bh->b_page); | |
51 | blocknr += offset >> (PAGE_CACHE_SHIFT - blkbits); | |
52 | return blocknr; | |
53 | } | |
54 | ||
e20d96d6 | 55 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 56 | { |
e20d96d6 | 57 | struct btrfs_node *node = btrfs_buffer_node(buf); |
7eccb903 | 58 | if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) { |
8352d8a4 CM |
59 | printk(KERN_CRIT "bh_blocknr(buf) is %Lu, header is %Lu\n", |
60 | bh_blocknr(buf), btrfs_header_blocknr(&node->header)); | |
9a8dd150 | 61 | BUG(); |
d98237b3 | 62 | } |
9a8dd150 | 63 | return 0; |
eb60ceac CM |
64 | } |
65 | ||
d98237b3 CM |
66 | struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) |
67 | { | |
68 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
69 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
70 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
71 | struct page *page; | |
72 | struct buffer_head *bh; | |
73 | struct buffer_head *head; | |
74 | struct buffer_head *ret = NULL; | |
75 | ||
2c90e5d6 | 76 | |
d98237b3 CM |
77 | page = find_lock_page(mapping, index); |
78 | if (!page) | |
79 | return NULL; | |
80 | ||
81 | if (!page_has_buffers(page)) | |
82 | goto out_unlock; | |
83 | ||
84 | head = page_buffers(page); | |
85 | bh = head; | |
86 | do { | |
7eccb903 | 87 | if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) { |
d98237b3 CM |
88 | ret = bh; |
89 | get_bh(bh); | |
90 | goto out_unlock; | |
91 | } | |
92 | bh = bh->b_this_page; | |
93 | } while (bh != head); | |
94 | out_unlock: | |
95 | unlock_page(page); | |
d6025579 | 96 | if (ret) { |
22b0ebda | 97 | touch_buffer(ret); |
d6025579 | 98 | } |
d98237b3 CM |
99 | page_cache_release(page); |
100 | return ret; | |
101 | } | |
102 | ||
8352d8a4 | 103 | int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh, |
7eccb903 CM |
104 | u64 logical) |
105 | { | |
106 | struct dev_lookup *lookup[2]; | |
7eccb903 CM |
107 | |
108 | int ret; | |
109 | ||
110 | root = root->fs_info->dev_root; | |
111 | ret = radix_tree_gang_lookup(&root->fs_info->dev_radix, | |
112 | (void **)lookup, | |
113 | (unsigned long)logical, | |
114 | ARRAY_SIZE(lookup)); | |
115 | if (ret == 0 || lookup[0]->block_start > logical || | |
116 | lookup[0]->block_start + lookup[0]->num_blocks <= logical) { | |
117 | ret = -ENOENT; | |
118 | goto out; | |
119 | } | |
120 | bh->b_bdev = lookup[0]->bdev; | |
121 | bh->b_blocknr = logical - lookup[0]->block_start; | |
7eccb903 CM |
122 | set_buffer_mapped(bh); |
123 | ret = 0; | |
124 | out: | |
125 | return ret; | |
126 | } | |
127 | ||
d98237b3 CM |
128 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
129 | u64 blocknr) | |
130 | { | |
131 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
132 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
133 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
134 | struct page *page; | |
135 | struct buffer_head *bh; | |
136 | struct buffer_head *head; | |
137 | struct buffer_head *ret = NULL; | |
7eccb903 | 138 | int err; |
d98237b3 | 139 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
22b0ebda | 140 | |
d98237b3 CM |
141 | page = grab_cache_page(mapping, index); |
142 | if (!page) | |
143 | return NULL; | |
144 | ||
d98237b3 CM |
145 | if (!page_has_buffers(page)) |
146 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); | |
147 | head = page_buffers(page); | |
148 | bh = head; | |
149 | do { | |
150 | if (!buffer_mapped(bh)) { | |
8352d8a4 | 151 | err = btrfs_map_bh_to_logical(root, bh, first_block); |
7eccb903 | 152 | BUG_ON(err); |
d98237b3 | 153 | } |
7eccb903 | 154 | if (bh_blocknr(bh) == blocknr) { |
d98237b3 CM |
155 | ret = bh; |
156 | get_bh(bh); | |
157 | goto out_unlock; | |
158 | } | |
159 | bh = bh->b_this_page; | |
160 | first_block++; | |
161 | } while (bh != head); | |
162 | out_unlock: | |
163 | unlock_page(page); | |
22b0ebda CM |
164 | if (ret) |
165 | touch_buffer(ret); | |
d98237b3 CM |
166 | page_cache_release(page); |
167 | return ret; | |
168 | } | |
169 | ||
d98237b3 CM |
170 | static int btree_get_block(struct inode *inode, sector_t iblock, |
171 | struct buffer_head *bh, int create) | |
172 | { | |
7eccb903 CM |
173 | int err; |
174 | struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; | |
8352d8a4 | 175 | err = btrfs_map_bh_to_logical(root, bh, iblock); |
7eccb903 | 176 | return err; |
d98237b3 CM |
177 | } |
178 | ||
f254e52c CM |
179 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
180 | char *result) | |
87cbda5c | 181 | { |
87cbda5c CM |
182 | struct scatterlist sg; |
183 | struct crypto_hash *tfm = root->fs_info->hash_tfm; | |
184 | struct hash_desc desc; | |
185 | int ret; | |
87cbda5c CM |
186 | |
187 | desc.tfm = tfm; | |
188 | desc.flags = 0; | |
f254e52c | 189 | sg_init_one(&sg, data, len); |
87cbda5c | 190 | spin_lock(&root->fs_info->hash_lock); |
22b0ebda | 191 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
87cbda5c CM |
192 | spin_unlock(&root->fs_info->hash_lock); |
193 | if (ret) { | |
194 | printk("sha256 digest failed\n"); | |
195 | } | |
f254e52c CM |
196 | return ret; |
197 | } | |
198 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, | |
199 | int verify) | |
200 | { | |
201 | char result[BTRFS_CSUM_SIZE]; | |
202 | int ret; | |
203 | struct btrfs_node *node; | |
204 | ||
205 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, | |
206 | bh->b_size - BTRFS_CSUM_SIZE, result); | |
207 | if (ret) | |
208 | return ret; | |
87cbda5c | 209 | if (verify) { |
f254e52c | 210 | if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { |
7eccb903 CM |
211 | printk("checksum verify failed on %Lu\n", |
212 | bh_blocknr(bh)); | |
f254e52c CM |
213 | return 1; |
214 | } | |
215 | } else { | |
216 | node = btrfs_buffer_node(bh); | |
22b0ebda | 217 | memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); |
f254e52c | 218 | } |
87cbda5c CM |
219 | return 0; |
220 | } | |
221 | ||
d98237b3 | 222 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 223 | { |
87cbda5c | 224 | struct buffer_head *bh; |
0f7d52f4 | 225 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 226 | struct buffer_head *head; |
87cbda5c CM |
227 | if (!page_has_buffers(page)) { |
228 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
229 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
230 | } | |
231 | head = page_buffers(page); | |
232 | bh = head; | |
233 | do { | |
234 | if (buffer_dirty(bh)) | |
235 | csum_tree_block(root, bh, 0); | |
236 | bh = bh->b_this_page; | |
237 | } while (bh != head); | |
d98237b3 | 238 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb CM |
239 | } |
240 | ||
d98237b3 | 241 | static int btree_readpage(struct file * file, struct page * page) |
eb60ceac | 242 | { |
d98237b3 | 243 | return block_read_full_page(page, btree_get_block); |
eb60ceac CM |
244 | } |
245 | ||
d98237b3 CM |
246 | static struct address_space_operations btree_aops = { |
247 | .readpage = btree_readpage, | |
248 | .writepage = btree_writepage, | |
249 | .sync_page = block_sync_page, | |
250 | }; | |
251 | ||
e20d96d6 | 252 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 253 | { |
d98237b3 | 254 | struct buffer_head *bh = NULL; |
eb60ceac | 255 | |
d98237b3 CM |
256 | bh = btrfs_find_create_tree_block(root, blocknr); |
257 | if (!bh) | |
258 | return bh; | |
9d64272c CM |
259 | if (buffer_uptodate(bh)) |
260 | goto uptodate; | |
d98237b3 CM |
261 | lock_buffer(bh); |
262 | if (!buffer_uptodate(bh)) { | |
263 | get_bh(bh); | |
264 | bh->b_end_io = end_buffer_read_sync; | |
265 | submit_bh(READ, bh); | |
266 | wait_on_buffer(bh); | |
267 | if (!buffer_uptodate(bh)) | |
268 | goto fail; | |
87cbda5c | 269 | csum_tree_block(root, bh, 1); |
d98237b3 CM |
270 | } else { |
271 | unlock_buffer(bh); | |
272 | } | |
9d64272c | 273 | uptodate: |
d98237b3 | 274 | if (check_tree_block(root, bh)) |
cfaa7295 | 275 | BUG(); |
d98237b3 CM |
276 | return bh; |
277 | fail: | |
278 | brelse(bh); | |
279 | return NULL; | |
eb60ceac CM |
280 | } |
281 | ||
e089f05c | 282 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 283 | struct buffer_head *buf) |
ed2ff2cb | 284 | { |
d6025579 | 285 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 286 | mark_buffer_dirty(buf); |
ed2ff2cb CM |
287 | return 0; |
288 | } | |
289 | ||
e089f05c | 290 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 291 | struct buffer_head *buf) |
ed2ff2cb | 292 | { |
d6025579 | 293 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 294 | clear_buffer_dirty(buf); |
ed2ff2cb CM |
295 | return 0; |
296 | } | |
297 | ||
2c90e5d6 | 298 | static int __setup_root(int blocksize, |
9f5fae2f CM |
299 | struct btrfs_root *root, |
300 | struct btrfs_fs_info *fs_info, | |
e20d96d6 | 301 | u64 objectid) |
d97e63b6 | 302 | { |
cfaa7295 | 303 | root->node = NULL; |
0f7d52f4 | 304 | root->inode = NULL; |
a28ec197 | 305 | root->commit_root = NULL; |
2c90e5d6 | 306 | root->blocksize = blocksize; |
123abc88 | 307 | root->ref_cows = 0; |
9f5fae2f | 308 | root->fs_info = fs_info; |
0f7d52f4 CM |
309 | root->objectid = objectid; |
310 | root->last_trans = 0; | |
1b05da2e CM |
311 | root->highest_inode = 0; |
312 | root->last_inode_alloc = 0; | |
3768f368 CM |
313 | memset(&root->root_key, 0, sizeof(root->root_key)); |
314 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
315 | return 0; | |
316 | } | |
317 | ||
2c90e5d6 | 318 | static int find_and_setup_root(int blocksize, |
9f5fae2f CM |
319 | struct btrfs_root *tree_root, |
320 | struct btrfs_fs_info *fs_info, | |
321 | u64 objectid, | |
e20d96d6 | 322 | struct btrfs_root *root) |
3768f368 CM |
323 | { |
324 | int ret; | |
325 | ||
2c90e5d6 | 326 | __setup_root(blocksize, root, fs_info, objectid); |
3768f368 CM |
327 | ret = btrfs_find_last_root(tree_root, objectid, |
328 | &root->root_item, &root->root_key); | |
329 | BUG_ON(ret); | |
330 | ||
331 | root->node = read_tree_block(root, | |
332 | btrfs_root_blocknr(&root->root_item)); | |
3768f368 | 333 | BUG_ON(!root->node); |
d97e63b6 CM |
334 | return 0; |
335 | } | |
336 | ||
0f7d52f4 CM |
337 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
338 | struct btrfs_key *location) | |
339 | { | |
340 | struct btrfs_root *root; | |
341 | struct btrfs_root *tree_root = fs_info->tree_root; | |
342 | struct btrfs_path *path; | |
343 | struct btrfs_leaf *l; | |
1b05da2e | 344 | u64 highest_inode; |
0f7d52f4 CM |
345 | int ret = 0; |
346 | ||
347 | printk("read_fs_root looking for %Lu %Lu %u\n", location->objectid, location->offset, location->flags); | |
2619ba1f CM |
348 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
349 | (unsigned long)location->objectid); | |
350 | if (root) { | |
351 | printk("found %p in cache\n", root); | |
352 | return root; | |
353 | } | |
0f7d52f4 CM |
354 | root = kmalloc(sizeof(*root), GFP_NOFS); |
355 | if (!root) { | |
356 | printk("failed1\n"); | |
357 | return ERR_PTR(-ENOMEM); | |
358 | } | |
359 | if (location->offset == (u64)-1) { | |
360 | ret = find_and_setup_root(fs_info->sb->s_blocksize, | |
361 | fs_info->tree_root, fs_info, | |
362 | location->objectid, root); | |
363 | if (ret) { | |
364 | printk("failed2\n"); | |
365 | kfree(root); | |
366 | return ERR_PTR(ret); | |
367 | } | |
368 | goto insert; | |
369 | } | |
370 | ||
371 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, | |
372 | location->objectid); | |
373 | ||
374 | path = btrfs_alloc_path(); | |
375 | BUG_ON(!path); | |
376 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
377 | if (ret != 0) { | |
378 | printk("internal search_slot gives us %d\n", ret); | |
379 | if (ret > 0) | |
380 | ret = -ENOENT; | |
381 | goto out; | |
382 | } | |
383 | l = btrfs_buffer_leaf(path->nodes[0]); | |
384 | memcpy(&root->root_item, | |
385 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), | |
386 | sizeof(root->root_item)); | |
387 | memcpy(&root->root_key, location, sizeof(*location)); | |
388 | ret = 0; | |
389 | out: | |
390 | btrfs_release_path(root, path); | |
391 | btrfs_free_path(path); | |
392 | if (ret) { | |
393 | kfree(root); | |
394 | return ERR_PTR(ret); | |
395 | } | |
396 | root->node = read_tree_block(root, | |
397 | btrfs_root_blocknr(&root->root_item)); | |
398 | BUG_ON(!root->node); | |
399 | insert: | |
400 | printk("inserting %p\n", root); | |
401 | root->ref_cows = 1; | |
2619ba1f CM |
402 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
403 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
404 | root); |
405 | if (ret) { | |
406 | printk("radix_tree_insert gives us %d\n", ret); | |
407 | brelse(root->node); | |
408 | kfree(root); | |
409 | return ERR_PTR(ret); | |
410 | } | |
1b05da2e CM |
411 | ret = btrfs_find_highest_inode(root, &highest_inode); |
412 | if (ret == 0) { | |
413 | root->highest_inode = highest_inode; | |
414 | root->last_inode_alloc = highest_inode; | |
415 | printk("highest inode is %Lu\n", highest_inode); | |
416 | } | |
0f7d52f4 CM |
417 | printk("all worked\n"); |
418 | return root; | |
419 | } | |
420 | ||
8352d8a4 CM |
421 | int btrfs_open_disk(struct btrfs_root *root, u64 block_start, u64 num_blocks, |
422 | char *filename, int name_len) | |
423 | { | |
424 | char *null_filename; | |
425 | struct block_device *bdev; | |
426 | int ret; | |
427 | ||
428 | if (block_start == 0) { | |
429 | printk("skipping disk with block_start == 0\n"); | |
430 | return 0; | |
431 | } | |
432 | null_filename = kmalloc(name_len + 1, GFP_NOFS); | |
433 | if (!null_filename) | |
434 | return -ENOMEM; | |
435 | memcpy(null_filename, filename, name_len); | |
436 | null_filename[name_len] = '\0'; | |
437 | ||
438 | bdev = open_bdev_excl(null_filename, O_RDWR, root->fs_info->sb); | |
439 | if (IS_ERR(bdev)) { | |
440 | ret = PTR_ERR(bdev); | |
441 | goto out; | |
442 | } | |
443 | set_blocksize(bdev, root->fs_info->sb->s_blocksize); | |
444 | ret = btrfs_insert_dev_radix(root, bdev, block_start, num_blocks); | |
445 | BUG_ON(ret); | |
446 | ret = 0; | |
447 | out: | |
448 | kfree(null_filename); | |
449 | return ret; | |
450 | } | |
451 | ||
452 | static int read_device_info(struct btrfs_root *root) | |
453 | { | |
454 | struct btrfs_path *path; | |
455 | int ret; | |
456 | struct btrfs_key key; | |
457 | struct btrfs_leaf *leaf; | |
458 | struct btrfs_device_item *dev_item; | |
459 | int nritems; | |
460 | int slot; | |
461 | ||
462 | root = root->fs_info->dev_root; | |
463 | ||
464 | path = btrfs_alloc_path(); | |
465 | if (!path) | |
466 | return -ENOMEM; | |
467 | key.objectid = 0; | |
468 | key.offset = 0; | |
469 | key.flags = 0; | |
470 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); | |
471 | ||
472 | mutex_lock(&root->fs_info->fs_mutex); | |
473 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
474 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
475 | nritems = btrfs_header_nritems(&leaf->header); | |
476 | while(1) { | |
477 | slot = path->slots[0]; | |
478 | if (slot >= nritems) { | |
479 | ret = btrfs_next_leaf(root, path); | |
480 | if (ret) | |
481 | break; | |
482 | leaf = btrfs_buffer_leaf(path->nodes[0]); | |
483 | nritems = btrfs_header_nritems(&leaf->header); | |
484 | slot = path->slots[0]; | |
485 | } | |
486 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); | |
487 | if (btrfs_key_type(&key) != BTRFS_DEV_ITEM_KEY) { | |
488 | path->slots[0]++; | |
489 | continue; | |
490 | } | |
491 | dev_item = btrfs_item_ptr(leaf, slot, struct btrfs_device_item); | |
492 | printk("found key %Lu %Lu\n", key.objectid, key.offset); | |
493 | ret = btrfs_open_disk(root, key.objectid, key.offset, | |
494 | (char *)(dev_item + 1), | |
495 | btrfs_device_pathlen(dev_item)); | |
496 | BUG_ON(ret); | |
497 | path->slots[0]++; | |
498 | } | |
499 | btrfs_free_path(path); | |
500 | mutex_unlock(&root->fs_info->fs_mutex); | |
501 | return 0; | |
502 | } | |
503 | ||
2c90e5d6 | 504 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 505 | { |
e20d96d6 CM |
506 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
507 | GFP_NOFS); | |
0bd93ba0 CM |
508 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), |
509 | GFP_NOFS); | |
e20d96d6 CM |
510 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
511 | GFP_NOFS); | |
e20d96d6 CM |
512 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
513 | GFP_NOFS); | |
eb60ceac | 514 | int ret; |
2c90e5d6 | 515 | struct btrfs_super_block *disk_super; |
7eccb903 | 516 | struct dev_lookup *dev_lookup; |
eb60ceac | 517 | |
8ef97622 CM |
518 | init_bit_radix(&fs_info->pinned_radix); |
519 | init_bit_radix(&fs_info->pending_del_radix); | |
0f7d52f4 | 520 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
7eccb903 | 521 | INIT_RADIX_TREE(&fs_info->dev_radix, GFP_NOFS); |
2c90e5d6 | 522 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 523 | fs_info->running_transaction = NULL; |
9f5fae2f CM |
524 | fs_info->tree_root = tree_root; |
525 | fs_info->extent_root = extent_root; | |
0bd93ba0 | 526 | fs_info->dev_root = dev_root; |
e20d96d6 | 527 | fs_info->sb = sb; |
d98237b3 CM |
528 | fs_info->btree_inode = new_inode(sb); |
529 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 530 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
531 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
532 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
0f7d52f4 CM |
533 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
534 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
535 | sizeof(struct btrfs_key)); | |
22b0ebda | 536 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 537 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
87cbda5c | 538 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); |
30ae8467 | 539 | spin_lock_init(&fs_info->hash_lock); |
30ae8467 | 540 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
87cbda5c CM |
541 | printk("failed to allocate sha256 hash\n"); |
542 | return NULL; | |
543 | } | |
79154b1b | 544 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 545 | mutex_init(&fs_info->fs_mutex); |
9f5fae2f CM |
546 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
547 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); | |
3768f368 | 548 | |
0bd93ba0 CM |
549 | __setup_root(sb->s_blocksize, dev_root, |
550 | fs_info, BTRFS_DEV_TREE_OBJECTID); | |
551 | ||
2c90e5d6 CM |
552 | __setup_root(sb->s_blocksize, tree_root, |
553 | fs_info, BTRFS_ROOT_TREE_OBJECTID); | |
7eccb903 CM |
554 | |
555 | dev_lookup = kmalloc(sizeof(*dev_lookup), GFP_NOFS); | |
556 | dev_lookup->block_start = 0; | |
557 | dev_lookup->num_blocks = (u32)-2; | |
558 | dev_lookup->bdev = sb->s_bdev; | |
559 | ret = radix_tree_insert(&fs_info->dev_radix, (u32)-2, dev_lookup); | |
560 | BUG_ON(ret); | |
2c90e5d6 CM |
561 | fs_info->sb_buffer = read_tree_block(tree_root, |
562 | BTRFS_SUPER_INFO_OFFSET / | |
563 | sb->s_blocksize); | |
d98237b3 | 564 | |
0f7d52f4 | 565 | if (!fs_info->sb_buffer) |
d98237b3 | 566 | return NULL; |
d98237b3 | 567 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
0f7d52f4 | 568 | if (!btrfs_super_root(disk_super)) |
2c90e5d6 | 569 | return NULL; |
0f7d52f4 | 570 | |
8352d8a4 CM |
571 | i_size_write(fs_info->btree_inode, |
572 | btrfs_super_total_blocks(disk_super) << | |
573 | fs_info->btree_inode->i_blkbits); | |
574 | ||
7eccb903 CM |
575 | radix_tree_delete(&fs_info->dev_radix, (u32)-2); |
576 | dev_lookup->block_start = btrfs_super_device_block_start(disk_super); | |
577 | dev_lookup->num_blocks = btrfs_super_device_num_blocks(disk_super); | |
578 | ret = radix_tree_insert(&fs_info->dev_radix, | |
579 | dev_lookup->block_start + | |
8352d8a4 | 580 | dev_lookup->num_blocks - 1, dev_lookup); |
7eccb903 CM |
581 | BUG_ON(ret); |
582 | ||
d98237b3 | 583 | fs_info->disk_super = disk_super; |
8352d8a4 | 584 | |
0bd93ba0 CM |
585 | dev_root->node = read_tree_block(tree_root, |
586 | btrfs_super_device_root(disk_super)); | |
8352d8a4 CM |
587 | |
588 | ret = read_device_info(dev_root); | |
589 | BUG_ON(ret); | |
590 | ||
e20d96d6 CM |
591 | tree_root->node = read_tree_block(tree_root, |
592 | btrfs_super_root(disk_super)); | |
3768f368 CM |
593 | BUG_ON(!tree_root->node); |
594 | ||
2c90e5d6 CM |
595 | mutex_lock(&fs_info->fs_mutex); |
596 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, | |
e20d96d6 | 597 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
3768f368 CM |
598 | BUG_ON(ret); |
599 | ||
0f7d52f4 | 600 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
d6e4a428 CM |
601 | memset(&fs_info->kobj, 0, sizeof(fs_info->kobj)); |
602 | kobj_set_kset_s(fs_info, btrfs_subsys); | |
603 | kobject_set_name(&fs_info->kobj, "%s", sb->s_id); | |
604 | kobject_register(&fs_info->kobj); | |
5be6f7f1 | 605 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 606 | return tree_root; |
eb60ceac CM |
607 | } |
608 | ||
e089f05c | 609 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 610 | *root) |
eb60ceac | 611 | { |
d5719762 | 612 | struct buffer_head *bh = root->fs_info->sb_buffer; |
2c90e5d6 | 613 | |
d5719762 | 614 | btrfs_set_super_root(root->fs_info->disk_super, |
7eccb903 | 615 | bh_blocknr(root->fs_info->tree_root->node)); |
d5719762 | 616 | lock_buffer(bh); |
2c90e5d6 | 617 | WARN_ON(atomic_read(&bh->b_count) < 1); |
d5719762 | 618 | clear_buffer_dirty(bh); |
87cbda5c | 619 | csum_tree_block(root, bh, 0); |
d5719762 CM |
620 | bh->b_end_io = end_buffer_write_sync; |
621 | get_bh(bh); | |
622 | submit_bh(WRITE, bh); | |
623 | wait_on_buffer(bh); | |
624 | if (!buffer_uptodate(bh)) { | |
625 | WARN_ON(1); | |
626 | return -EIO; | |
cfaa7295 CM |
627 | } |
628 | return 0; | |
629 | } | |
630 | ||
2619ba1f CM |
631 | static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
632 | { | |
633 | radix_tree_delete(&fs_info->fs_roots_radix, | |
634 | (unsigned long)root->root_key.objectid); | |
635 | if (root->inode) | |
636 | iput(root->inode); | |
637 | if (root->node) | |
638 | brelse(root->node); | |
639 | if (root->commit_root) | |
640 | brelse(root->commit_root); | |
641 | kfree(root); | |
642 | return 0; | |
643 | } | |
644 | ||
0f7d52f4 CM |
645 | int del_fs_roots(struct btrfs_fs_info *fs_info) |
646 | { | |
647 | int ret; | |
648 | struct btrfs_root *gang[8]; | |
649 | int i; | |
650 | ||
651 | while(1) { | |
652 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
653 | (void **)gang, 0, | |
654 | ARRAY_SIZE(gang)); | |
655 | if (!ret) | |
656 | break; | |
2619ba1f CM |
657 | for (i = 0; i < ret; i++) |
658 | free_fs_root(fs_info, gang[i]); | |
0f7d52f4 CM |
659 | } |
660 | return 0; | |
661 | } | |
7eccb903 CM |
662 | static int free_dev_radix(struct btrfs_fs_info *fs_info) |
663 | { | |
664 | struct dev_lookup *lookup[8]; | |
665 | struct block_device *super_bdev = fs_info->sb->s_bdev; | |
666 | int ret; | |
667 | int i; | |
668 | while(1) { | |
669 | ret = radix_tree_gang_lookup(&fs_info->dev_radix, | |
670 | (void **)lookup, 0, | |
671 | ARRAY_SIZE(lookup)); | |
672 | if (!ret) | |
673 | break; | |
674 | for (i = 0; i < ret; i++) { | |
675 | if (lookup[i]->bdev != super_bdev) | |
676 | close_bdev_excl(lookup[i]->bdev); | |
677 | radix_tree_delete(&fs_info->dev_radix, | |
678 | lookup[i]->block_start + | |
8352d8a4 | 679 | lookup[i]->num_blocks - 1); |
7eccb903 CM |
680 | kfree(lookup[i]); |
681 | } | |
682 | } | |
683 | return 0; | |
684 | } | |
0f7d52f4 | 685 | |
e20d96d6 | 686 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 687 | { |
3768f368 | 688 | int ret; |
e089f05c | 689 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 690 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 691 | |
0f7d52f4 | 692 | mutex_lock(&fs_info->fs_mutex); |
79154b1b CM |
693 | trans = btrfs_start_transaction(root, 1); |
694 | btrfs_commit_transaction(trans, root); | |
695 | /* run commit again to drop the original snapshot */ | |
696 | trans = btrfs_start_transaction(root, 1); | |
697 | btrfs_commit_transaction(trans, root); | |
698 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 699 | BUG_ON(ret); |
79154b1b | 700 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
701 | mutex_unlock(&fs_info->fs_mutex); |
702 | ||
703 | if (fs_info->extent_root->node) | |
704 | btrfs_block_release(fs_info->extent_root, | |
705 | fs_info->extent_root->node); | |
0bd93ba0 CM |
706 | if (fs_info->dev_root->node) |
707 | btrfs_block_release(fs_info->dev_root, | |
708 | fs_info->dev_root->node); | |
0f7d52f4 CM |
709 | if (fs_info->tree_root->node) |
710 | btrfs_block_release(fs_info->tree_root, | |
711 | fs_info->tree_root->node); | |
712 | btrfs_block_release(root, fs_info->sb_buffer); | |
713 | crypto_free_hash(fs_info->hash_tfm); | |
714 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); | |
715 | iput(fs_info->btree_inode); | |
7eccb903 CM |
716 | |
717 | free_dev_radix(fs_info); | |
0f7d52f4 CM |
718 | del_fs_roots(fs_info); |
719 | kfree(fs_info->extent_root); | |
0f7d52f4 CM |
720 | kfree(fs_info->tree_root); |
721 | kobject_unregister(&fs_info->kobj); | |
eb60ceac CM |
722 | return 0; |
723 | } | |
724 | ||
e20d96d6 | 725 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 726 | { |
7cfcc17e | 727 | brelse(buf); |
eb60ceac CM |
728 | } |
729 |