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