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> |
35b7e476 | 8 | #include <linux/writeback.h> |
eb60ceac CM |
9 | #include "ctree.h" |
10 | #include "disk-io.h" | |
e089f05c | 11 | #include "transaction.h" |
0f7d52f4 | 12 | #include "btrfs_inode.h" |
eb60ceac | 13 | |
7eccb903 CM |
14 | u64 bh_blocknr(struct buffer_head *bh) |
15 | { | |
0cf6c620 | 16 | return bh->b_blocknr; |
7eccb903 CM |
17 | } |
18 | ||
e20d96d6 | 19 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 20 | { |
e20d96d6 | 21 | struct btrfs_node *node = btrfs_buffer_node(buf); |
7eccb903 | 22 | if (bh_blocknr(buf) != btrfs_header_blocknr(&node->header)) { |
5af3981c CM |
23 | printk(KERN_CRIT "bh_blocknr(buf) is %llu, header is %llu\n", |
24 | (unsigned long long)bh_blocknr(buf), | |
25 | (unsigned long long)btrfs_header_blocknr(&node->header)); | |
39279cc3 | 26 | return 1; |
d98237b3 | 27 | } |
9a8dd150 | 28 | return 0; |
eb60ceac CM |
29 | } |
30 | ||
d98237b3 CM |
31 | struct buffer_head *btrfs_find_tree_block(struct btrfs_root *root, u64 blocknr) |
32 | { | |
33 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
34 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
35 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
36 | struct page *page; | |
37 | struct buffer_head *bh; | |
38 | struct buffer_head *head; | |
39 | struct buffer_head *ret = NULL; | |
40 | ||
2c90e5d6 | 41 | |
d98237b3 CM |
42 | page = find_lock_page(mapping, index); |
43 | if (!page) | |
44 | return NULL; | |
45 | ||
46 | if (!page_has_buffers(page)) | |
47 | goto out_unlock; | |
48 | ||
49 | head = page_buffers(page); | |
50 | bh = head; | |
51 | do { | |
7eccb903 | 52 | if (buffer_mapped(bh) && bh_blocknr(bh) == blocknr) { |
d98237b3 CM |
53 | ret = bh; |
54 | get_bh(bh); | |
55 | goto out_unlock; | |
56 | } | |
57 | bh = bh->b_this_page; | |
58 | } while (bh != head); | |
59 | out_unlock: | |
60 | unlock_page(page); | |
61 | page_cache_release(page); | |
62 | return ret; | |
63 | } | |
64 | ||
8352d8a4 | 65 | int btrfs_map_bh_to_logical(struct btrfs_root *root, struct buffer_head *bh, |
7eccb903 CM |
66 | u64 logical) |
67 | { | |
236454df CM |
68 | if (logical == 0) { |
69 | bh->b_bdev = NULL; | |
70 | bh->b_blocknr = 0; | |
71 | set_buffer_mapped(bh); | |
0cf6c620 CM |
72 | } else { |
73 | map_bh(bh, root->fs_info->sb, logical); | |
7eccb903 | 74 | } |
0cf6c620 | 75 | return 0; |
7eccb903 CM |
76 | } |
77 | ||
d98237b3 CM |
78 | struct buffer_head *btrfs_find_create_tree_block(struct btrfs_root *root, |
79 | u64 blocknr) | |
80 | { | |
81 | struct address_space *mapping = root->fs_info->btree_inode->i_mapping; | |
82 | int blockbits = root->fs_info->sb->s_blocksize_bits; | |
83 | unsigned long index = blocknr >> (PAGE_CACHE_SHIFT - blockbits); | |
84 | struct page *page; | |
85 | struct buffer_head *bh; | |
86 | struct buffer_head *head; | |
87 | struct buffer_head *ret = NULL; | |
7eccb903 | 88 | int err; |
d98237b3 | 89 | u64 first_block = index << (PAGE_CACHE_SHIFT - blockbits); |
22b0ebda | 90 | |
d98237b3 CM |
91 | page = grab_cache_page(mapping, index); |
92 | if (!page) | |
93 | return NULL; | |
94 | ||
d98237b3 CM |
95 | if (!page_has_buffers(page)) |
96 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, 0); | |
97 | head = page_buffers(page); | |
98 | bh = head; | |
99 | do { | |
100 | if (!buffer_mapped(bh)) { | |
8352d8a4 | 101 | err = btrfs_map_bh_to_logical(root, bh, first_block); |
7eccb903 | 102 | BUG_ON(err); |
d98237b3 | 103 | } |
7eccb903 | 104 | if (bh_blocknr(bh) == blocknr) { |
d98237b3 CM |
105 | ret = bh; |
106 | get_bh(bh); | |
107 | goto out_unlock; | |
108 | } | |
109 | bh = bh->b_this_page; | |
110 | first_block++; | |
111 | } while (bh != head); | |
112 | out_unlock: | |
113 | unlock_page(page); | |
22b0ebda CM |
114 | if (ret) |
115 | touch_buffer(ret); | |
d98237b3 CM |
116 | page_cache_release(page); |
117 | return ret; | |
118 | } | |
119 | ||
d98237b3 CM |
120 | static int btree_get_block(struct inode *inode, sector_t iblock, |
121 | struct buffer_head *bh, int create) | |
122 | { | |
7eccb903 CM |
123 | int err; |
124 | struct btrfs_root *root = BTRFS_I(bh->b_page->mapping->host)->root; | |
8352d8a4 | 125 | err = btrfs_map_bh_to_logical(root, bh, iblock); |
7eccb903 | 126 | return err; |
d98237b3 CM |
127 | } |
128 | ||
f254e52c CM |
129 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
130 | char *result) | |
87cbda5c | 131 | { |
87cbda5c CM |
132 | struct scatterlist sg; |
133 | struct crypto_hash *tfm = root->fs_info->hash_tfm; | |
134 | struct hash_desc desc; | |
135 | int ret; | |
87cbda5c CM |
136 | |
137 | desc.tfm = tfm; | |
138 | desc.flags = 0; | |
f254e52c | 139 | sg_init_one(&sg, data, len); |
87cbda5c | 140 | spin_lock(&root->fs_info->hash_lock); |
22b0ebda | 141 | ret = crypto_hash_digest(&desc, &sg, 1, result); |
87cbda5c CM |
142 | spin_unlock(&root->fs_info->hash_lock); |
143 | if (ret) { | |
509659cd | 144 | printk("digest failed\n"); |
87cbda5c | 145 | } |
f254e52c CM |
146 | return ret; |
147 | } | |
148 | static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, | |
149 | int verify) | |
150 | { | |
509659cd | 151 | char result[BTRFS_CRC32_SIZE]; |
f254e52c CM |
152 | int ret; |
153 | struct btrfs_node *node; | |
154 | ||
155 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, | |
156 | bh->b_size - BTRFS_CSUM_SIZE, result); | |
157 | if (ret) | |
158 | return ret; | |
87cbda5c | 159 | if (verify) { |
509659cd | 160 | if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) { |
5af3981c CM |
161 | printk("btrfs: %s checksum verify failed on %llu\n", |
162 | root->fs_info->sb->s_id, | |
163 | (unsigned long long)bh_blocknr(bh)); | |
f254e52c CM |
164 | return 1; |
165 | } | |
166 | } else { | |
167 | node = btrfs_buffer_node(bh); | |
509659cd | 168 | memcpy(node->header.csum, result, BTRFS_CRC32_SIZE); |
f254e52c | 169 | } |
87cbda5c CM |
170 | return 0; |
171 | } | |
172 | ||
d98237b3 | 173 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 174 | { |
87cbda5c | 175 | struct buffer_head *bh; |
0f7d52f4 | 176 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 177 | struct buffer_head *head; |
87cbda5c CM |
178 | if (!page_has_buffers(page)) { |
179 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
180 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
181 | } | |
182 | head = page_buffers(page); | |
183 | bh = head; | |
184 | do { | |
185 | if (buffer_dirty(bh)) | |
186 | csum_tree_block(root, bh, 0); | |
187 | bh = bh->b_this_page; | |
188 | } while (bh != head); | |
d98237b3 | 189 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb CM |
190 | } |
191 | ||
d98237b3 | 192 | static int btree_readpage(struct file * file, struct page * page) |
eb60ceac | 193 | { |
d98237b3 | 194 | return block_read_full_page(page, btree_get_block); |
eb60ceac CM |
195 | } |
196 | ||
d98237b3 CM |
197 | static struct address_space_operations btree_aops = { |
198 | .readpage = btree_readpage, | |
199 | .writepage = btree_writepage, | |
200 | .sync_page = block_sync_page, | |
201 | }; | |
202 | ||
090d1875 CM |
203 | int readahead_tree_block(struct btrfs_root *root, u64 blocknr) |
204 | { | |
205 | struct buffer_head *bh = NULL; | |
de428b63 | 206 | int ret = 0; |
090d1875 CM |
207 | |
208 | bh = btrfs_find_create_tree_block(root, blocknr); | |
209 | if (!bh) | |
210 | return 0; | |
de428b63 CM |
211 | if (buffer_uptodate(bh)) { |
212 | ret = 1; | |
090d1875 | 213 | goto done; |
de428b63 CM |
214 | } |
215 | if (test_set_buffer_locked(bh)) { | |
216 | ret = 1; | |
090d1875 | 217 | goto done; |
de428b63 | 218 | } |
090d1875 CM |
219 | if (!buffer_uptodate(bh)) { |
220 | get_bh(bh); | |
221 | bh->b_end_io = end_buffer_read_sync; | |
222 | submit_bh(READ, bh); | |
223 | } else { | |
224 | unlock_buffer(bh); | |
de428b63 | 225 | ret = 1; |
090d1875 CM |
226 | } |
227 | done: | |
228 | brelse(bh); | |
de428b63 | 229 | return ret; |
090d1875 CM |
230 | } |
231 | ||
e20d96d6 | 232 | struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 233 | { |
d98237b3 | 234 | struct buffer_head *bh = NULL; |
eb60ceac | 235 | |
d98237b3 CM |
236 | bh = btrfs_find_create_tree_block(root, blocknr); |
237 | if (!bh) | |
238 | return bh; | |
9d64272c CM |
239 | if (buffer_uptodate(bh)) |
240 | goto uptodate; | |
d98237b3 CM |
241 | lock_buffer(bh); |
242 | if (!buffer_uptodate(bh)) { | |
243 | get_bh(bh); | |
244 | bh->b_end_io = end_buffer_read_sync; | |
245 | submit_bh(READ, bh); | |
246 | wait_on_buffer(bh); | |
247 | if (!buffer_uptodate(bh)) | |
248 | goto fail; | |
249 | } else { | |
250 | unlock_buffer(bh); | |
251 | } | |
9d64272c | 252 | uptodate: |
090d1875 CM |
253 | if (!buffer_checked(bh)) { |
254 | csum_tree_block(root, bh, 1); | |
255 | set_buffer_checked(bh); | |
256 | } | |
d98237b3 | 257 | if (check_tree_block(root, bh)) |
39279cc3 | 258 | goto fail; |
d98237b3 CM |
259 | return bh; |
260 | fail: | |
261 | brelse(bh); | |
262 | return NULL; | |
eb60ceac CM |
263 | } |
264 | ||
e089f05c | 265 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 266 | struct buffer_head *buf) |
ed2ff2cb | 267 | { |
d6025579 | 268 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 269 | mark_buffer_dirty(buf); |
ed2ff2cb CM |
270 | return 0; |
271 | } | |
272 | ||
e089f05c | 273 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
e20d96d6 | 274 | struct buffer_head *buf) |
ed2ff2cb | 275 | { |
d6025579 | 276 | WARN_ON(atomic_read(&buf->b_count) == 0); |
e20d96d6 | 277 | clear_buffer_dirty(buf); |
ed2ff2cb CM |
278 | return 0; |
279 | } | |
280 | ||
2c90e5d6 | 281 | static int __setup_root(int blocksize, |
9f5fae2f CM |
282 | struct btrfs_root *root, |
283 | struct btrfs_fs_info *fs_info, | |
e20d96d6 | 284 | u64 objectid) |
d97e63b6 | 285 | { |
cfaa7295 | 286 | root->node = NULL; |
0f7d52f4 | 287 | root->inode = NULL; |
a28ec197 | 288 | root->commit_root = NULL; |
2c90e5d6 | 289 | root->blocksize = blocksize; |
123abc88 | 290 | root->ref_cows = 0; |
9f5fae2f | 291 | root->fs_info = fs_info; |
0f7d52f4 CM |
292 | root->objectid = objectid; |
293 | root->last_trans = 0; | |
1b05da2e CM |
294 | root->highest_inode = 0; |
295 | root->last_inode_alloc = 0; | |
3768f368 CM |
296 | memset(&root->root_key, 0, sizeof(root->root_key)); |
297 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
4d775673 | 298 | root->root_key.objectid = objectid; |
3768f368 CM |
299 | return 0; |
300 | } | |
301 | ||
2c90e5d6 | 302 | static int find_and_setup_root(int blocksize, |
9f5fae2f CM |
303 | struct btrfs_root *tree_root, |
304 | struct btrfs_fs_info *fs_info, | |
305 | u64 objectid, | |
e20d96d6 | 306 | struct btrfs_root *root) |
3768f368 CM |
307 | { |
308 | int ret; | |
309 | ||
2c90e5d6 | 310 | __setup_root(blocksize, root, fs_info, objectid); |
3768f368 CM |
311 | ret = btrfs_find_last_root(tree_root, objectid, |
312 | &root->root_item, &root->root_key); | |
313 | BUG_ON(ret); | |
314 | ||
315 | root->node = read_tree_block(root, | |
316 | btrfs_root_blocknr(&root->root_item)); | |
3768f368 | 317 | BUG_ON(!root->node); |
d97e63b6 CM |
318 | return 0; |
319 | } | |
320 | ||
0f7d52f4 CM |
321 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, |
322 | struct btrfs_key *location) | |
323 | { | |
324 | struct btrfs_root *root; | |
325 | struct btrfs_root *tree_root = fs_info->tree_root; | |
326 | struct btrfs_path *path; | |
327 | struct btrfs_leaf *l; | |
1b05da2e | 328 | u64 highest_inode; |
0f7d52f4 CM |
329 | int ret = 0; |
330 | ||
2619ba1f CM |
331 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
332 | (unsigned long)location->objectid); | |
0cf6c620 | 333 | if (root) |
2619ba1f | 334 | return root; |
0f7d52f4 | 335 | root = kmalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 336 | if (!root) |
0f7d52f4 | 337 | return ERR_PTR(-ENOMEM); |
0f7d52f4 CM |
338 | if (location->offset == (u64)-1) { |
339 | ret = find_and_setup_root(fs_info->sb->s_blocksize, | |
340 | fs_info->tree_root, fs_info, | |
341 | location->objectid, root); | |
342 | if (ret) { | |
0f7d52f4 CM |
343 | kfree(root); |
344 | return ERR_PTR(ret); | |
345 | } | |
346 | goto insert; | |
347 | } | |
348 | ||
349 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, | |
350 | location->objectid); | |
351 | ||
352 | path = btrfs_alloc_path(); | |
353 | BUG_ON(!path); | |
354 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
355 | if (ret != 0) { | |
0f7d52f4 CM |
356 | if (ret > 0) |
357 | ret = -ENOENT; | |
358 | goto out; | |
359 | } | |
360 | l = btrfs_buffer_leaf(path->nodes[0]); | |
361 | memcpy(&root->root_item, | |
362 | btrfs_item_ptr(l, path->slots[0], struct btrfs_root_item), | |
363 | sizeof(root->root_item)); | |
364 | memcpy(&root->root_key, location, sizeof(*location)); | |
365 | ret = 0; | |
366 | out: | |
367 | btrfs_release_path(root, path); | |
368 | btrfs_free_path(path); | |
369 | if (ret) { | |
370 | kfree(root); | |
371 | return ERR_PTR(ret); | |
372 | } | |
373 | root->node = read_tree_block(root, | |
374 | btrfs_root_blocknr(&root->root_item)); | |
375 | BUG_ON(!root->node); | |
376 | insert: | |
0f7d52f4 | 377 | root->ref_cows = 1; |
2619ba1f CM |
378 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
379 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
380 | root); |
381 | if (ret) { | |
0f7d52f4 CM |
382 | brelse(root->node); |
383 | kfree(root); | |
384 | return ERR_PTR(ret); | |
385 | } | |
1b05da2e CM |
386 | ret = btrfs_find_highest_inode(root, &highest_inode); |
387 | if (ret == 0) { | |
388 | root->highest_inode = highest_inode; | |
389 | root->last_inode_alloc = highest_inode; | |
1b05da2e | 390 | } |
0f7d52f4 CM |
391 | return root; |
392 | } | |
393 | ||
2c90e5d6 | 394 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 395 | { |
e20d96d6 CM |
396 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
397 | GFP_NOFS); | |
398 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
399 | GFP_NOFS); | |
e20d96d6 CM |
400 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
401 | GFP_NOFS); | |
eb60ceac | 402 | int ret; |
39279cc3 | 403 | int err = -EIO; |
2c90e5d6 | 404 | struct btrfs_super_block *disk_super; |
eb60ceac | 405 | |
39279cc3 CM |
406 | if (!extent_root || !tree_root || !fs_info) { |
407 | err = -ENOMEM; | |
408 | goto fail; | |
409 | } | |
8ef97622 CM |
410 | init_bit_radix(&fs_info->pinned_radix); |
411 | init_bit_radix(&fs_info->pending_del_radix); | |
e37c9e69 | 412 | init_bit_radix(&fs_info->extent_map_radix); |
0f7d52f4 | 413 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
9078a3e1 | 414 | INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL); |
be744175 | 415 | INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL); |
8fd17795 | 416 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 417 | INIT_LIST_HEAD(&fs_info->dead_roots); |
2c90e5d6 | 418 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 419 | fs_info->running_transaction = NULL; |
9f5fae2f CM |
420 | fs_info->tree_root = tree_root; |
421 | fs_info->extent_root = extent_root; | |
e20d96d6 | 422 | fs_info->sb = sb; |
d98237b3 CM |
423 | fs_info->btree_inode = new_inode(sb); |
424 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 425 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
426 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
427 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
e66f709b | 428 | fs_info->do_barriers = 1; |
f2458e1d CM |
429 | fs_info->extent_tree_insert_nr = 0; |
430 | fs_info->extent_tree_prealloc_nr = 0; | |
facda1e7 CM |
431 | fs_info->closing = 0; |
432 | ||
08607c1b | 433 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
0f7d52f4 CM |
434 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
435 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
436 | sizeof(struct btrfs_key)); | |
22b0ebda | 437 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 438 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
509659cd | 439 | fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC); |
30ae8467 | 440 | spin_lock_init(&fs_info->hash_lock); |
39279cc3 | 441 | |
30ae8467 | 442 | if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { |
39279cc3 CM |
443 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); |
444 | err = -ENOMEM; | |
445 | goto fail_iput; | |
87cbda5c | 446 | } |
79154b1b | 447 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 448 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 449 | |
2c90e5d6 CM |
450 | __setup_root(sb->s_blocksize, tree_root, |
451 | fs_info, BTRFS_ROOT_TREE_OBJECTID); | |
7eccb903 | 452 | |
2c90e5d6 CM |
453 | fs_info->sb_buffer = read_tree_block(tree_root, |
454 | BTRFS_SUPER_INFO_OFFSET / | |
455 | sb->s_blocksize); | |
d98237b3 | 456 | |
0f7d52f4 | 457 | if (!fs_info->sb_buffer) |
39279cc3 | 458 | goto fail_iput; |
d98237b3 | 459 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
39279cc3 | 460 | |
0f7d52f4 | 461 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 462 | goto fail_sb_buffer; |
0f7d52f4 | 463 | |
8352d8a4 CM |
464 | i_size_write(fs_info->btree_inode, |
465 | btrfs_super_total_blocks(disk_super) << | |
466 | fs_info->btree_inode->i_blkbits); | |
467 | ||
d98237b3 | 468 | fs_info->disk_super = disk_super; |
39279cc3 CM |
469 | |
470 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, | |
471 | sizeof(disk_super->magic))) { | |
472 | printk("btrfs: valid FS not found on %s\n", sb->s_id); | |
473 | goto fail_sb_buffer; | |
474 | } | |
e20d96d6 CM |
475 | tree_root->node = read_tree_block(tree_root, |
476 | btrfs_super_root(disk_super)); | |
39279cc3 CM |
477 | if (!tree_root->node) |
478 | goto fail_sb_buffer; | |
3768f368 | 479 | |
2c90e5d6 CM |
480 | mutex_lock(&fs_info->fs_mutex); |
481 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, | |
e20d96d6 | 482 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
39279cc3 CM |
483 | if (ret) { |
484 | mutex_unlock(&fs_info->fs_mutex); | |
485 | goto fail_tree_root; | |
486 | } | |
3768f368 | 487 | |
9078a3e1 CM |
488 | btrfs_read_block_groups(extent_root); |
489 | ||
0f7d52f4 | 490 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
5be6f7f1 | 491 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 492 | return tree_root; |
39279cc3 CM |
493 | |
494 | fail_tree_root: | |
495 | btrfs_block_release(tree_root, tree_root->node); | |
496 | fail_sb_buffer: | |
497 | btrfs_block_release(tree_root, fs_info->sb_buffer); | |
498 | fail_iput: | |
499 | iput(fs_info->btree_inode); | |
500 | fail: | |
501 | kfree(extent_root); | |
502 | kfree(tree_root); | |
503 | kfree(fs_info); | |
504 | return ERR_PTR(err); | |
eb60ceac CM |
505 | } |
506 | ||
e089f05c | 507 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 508 | *root) |
eb60ceac | 509 | { |
e66f709b | 510 | int ret; |
d5719762 | 511 | struct buffer_head *bh = root->fs_info->sb_buffer; |
2c90e5d6 | 512 | |
d5719762 | 513 | btrfs_set_super_root(root->fs_info->disk_super, |
7eccb903 | 514 | bh_blocknr(root->fs_info->tree_root->node)); |
d5719762 | 515 | lock_buffer(bh); |
2c90e5d6 | 516 | WARN_ON(atomic_read(&bh->b_count) < 1); |
d5719762 | 517 | clear_buffer_dirty(bh); |
87cbda5c | 518 | csum_tree_block(root, bh, 0); |
d5719762 CM |
519 | bh->b_end_io = end_buffer_write_sync; |
520 | get_bh(bh); | |
e66f709b CM |
521 | if (root->fs_info->do_barriers) |
522 | ret = submit_bh(WRITE_BARRIER, bh); | |
523 | else | |
524 | ret = submit_bh(WRITE, bh); | |
525 | if (ret == -EOPNOTSUPP) { | |
526 | set_buffer_uptodate(bh); | |
527 | root->fs_info->do_barriers = 0; | |
528 | ret = submit_bh(WRITE, bh); | |
529 | } | |
d5719762 CM |
530 | wait_on_buffer(bh); |
531 | if (!buffer_uptodate(bh)) { | |
532 | WARN_ON(1); | |
533 | return -EIO; | |
cfaa7295 CM |
534 | } |
535 | return 0; | |
536 | } | |
537 | ||
2619ba1f CM |
538 | static int free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
539 | { | |
540 | radix_tree_delete(&fs_info->fs_roots_radix, | |
541 | (unsigned long)root->root_key.objectid); | |
542 | if (root->inode) | |
543 | iput(root->inode); | |
544 | if (root->node) | |
545 | brelse(root->node); | |
546 | if (root->commit_root) | |
547 | brelse(root->commit_root); | |
548 | kfree(root); | |
549 | return 0; | |
550 | } | |
551 | ||
35b7e476 | 552 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
553 | { |
554 | int ret; | |
555 | struct btrfs_root *gang[8]; | |
556 | int i; | |
557 | ||
558 | while(1) { | |
559 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
560 | (void **)gang, 0, | |
561 | ARRAY_SIZE(gang)); | |
562 | if (!ret) | |
563 | break; | |
2619ba1f CM |
564 | for (i = 0; i < ret; i++) |
565 | free_fs_root(fs_info, gang[i]); | |
0f7d52f4 CM |
566 | } |
567 | return 0; | |
568 | } | |
b4100d64 | 569 | |
e20d96d6 | 570 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 571 | { |
3768f368 | 572 | int ret; |
e089f05c | 573 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 574 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 575 | |
facda1e7 | 576 | fs_info->closing = 1; |
08607c1b | 577 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 578 | mutex_lock(&fs_info->fs_mutex); |
79154b1b CM |
579 | trans = btrfs_start_transaction(root, 1); |
580 | btrfs_commit_transaction(trans, root); | |
581 | /* run commit again to drop the original snapshot */ | |
582 | trans = btrfs_start_transaction(root, 1); | |
583 | btrfs_commit_transaction(trans, root); | |
584 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 585 | BUG_ON(ret); |
79154b1b | 586 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
587 | mutex_unlock(&fs_info->fs_mutex); |
588 | ||
589 | if (fs_info->extent_root->node) | |
590 | btrfs_block_release(fs_info->extent_root, | |
591 | fs_info->extent_root->node); | |
0f7d52f4 CM |
592 | if (fs_info->tree_root->node) |
593 | btrfs_block_release(fs_info->tree_root, | |
594 | fs_info->tree_root->node); | |
595 | btrfs_block_release(root, fs_info->sb_buffer); | |
596 | crypto_free_hash(fs_info->hash_tfm); | |
597 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); | |
598 | iput(fs_info->btree_inode); | |
7eccb903 | 599 | |
9078a3e1 | 600 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 CM |
601 | del_fs_roots(fs_info); |
602 | kfree(fs_info->extent_root); | |
0f7d52f4 | 603 | kfree(fs_info->tree_root); |
eb60ceac CM |
604 | return 0; |
605 | } | |
606 | ||
e20d96d6 | 607 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
eb60ceac | 608 | { |
7cfcc17e | 609 | brelse(buf); |
eb60ceac CM |
610 | } |
611 | ||
35b7e476 CM |
612 | void btrfs_btree_balance_dirty(struct btrfs_root *root) |
613 | { | |
614 | balance_dirty_pages_ratelimited(root->fs_info->btree_inode->i_mapping); | |
615 | } |