Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
e20d96d6 | 19 | #include <linux/fs.h> |
d98237b3 | 20 | #include <linux/blkdev.h> |
11bd143f | 21 | #include <linux/crc32c.h> |
87cbda5c | 22 | #include <linux/scatterlist.h> |
22b0ebda | 23 | #include <linux/swap.h> |
0f7d52f4 | 24 | #include <linux/radix-tree.h> |
35b7e476 | 25 | #include <linux/writeback.h> |
5f39d397 | 26 | #include <linux/buffer_head.h> // for block_sync_page |
eb60ceac CM |
27 | #include "ctree.h" |
28 | #include "disk-io.h" | |
e089f05c | 29 | #include "transaction.h" |
0f7d52f4 | 30 | #include "btrfs_inode.h" |
eb60ceac | 31 | |
5f39d397 CM |
32 | #if 0 |
33 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) | |
7eccb903 | 34 | { |
5f39d397 CM |
35 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
36 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", | |
37 | (unsigned long long)extent_buffer_blocknr(buf), | |
38 | (unsigned long long)btrfs_header_blocknr(buf)); | |
39279cc3 | 39 | return 1; |
d98237b3 | 40 | } |
9a8dd150 | 41 | return 0; |
eb60ceac | 42 | } |
5f39d397 | 43 | #endif |
eb60ceac | 44 | |
5f39d397 CM |
45 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
46 | u64 blocknr) | |
d98237b3 | 47 | { |
5f39d397 CM |
48 | struct inode *btree_inode = root->fs_info->btree_inode; |
49 | return find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree, | |
50 | blocknr * root->sectorsize, | |
51 | root->sectorsize, GFP_NOFS); | |
52 | } | |
d98237b3 | 53 | |
5f39d397 CM |
54 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, |
55 | u64 blocknr) | |
56 | { | |
57 | struct inode *btree_inode = root->fs_info->btree_inode; | |
58 | return alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree, | |
59 | blocknr * root->sectorsize, | |
60 | root->sectorsize, GFP_NOFS); | |
d98237b3 CM |
61 | } |
62 | ||
5f39d397 CM |
63 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
64 | size_t page_offset, u64 start, u64 end, | |
65 | int create) | |
7eccb903 | 66 | { |
5f39d397 CM |
67 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
68 | struct extent_map *em; | |
69 | int ret; | |
70 | ||
71 | again: | |
72 | em = lookup_extent_mapping(em_tree, start, end); | |
73 | if (em) { | |
74 | goto out; | |
7eccb903 | 75 | } |
5f39d397 CM |
76 | em = alloc_extent_map(GFP_NOFS); |
77 | if (!em) { | |
78 | em = ERR_PTR(-ENOMEM); | |
79 | goto out; | |
80 | } | |
81 | em->start = 0; | |
82 | em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1; | |
83 | em->block_start = 0; | |
84 | em->block_end = em->end; | |
85 | em->bdev = inode->i_sb->s_bdev; | |
86 | ret = add_extent_mapping(em_tree, em); | |
87 | if (ret == -EEXIST) { | |
88 | free_extent_map(em); | |
89 | em = NULL; | |
90 | goto again; | |
91 | } else if (ret) { | |
92 | em = ERR_PTR(ret); | |
93 | } | |
94 | out: | |
95 | return em; | |
7eccb903 CM |
96 | } |
97 | ||
5f39d397 | 98 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
d98237b3 | 99 | { |
5f39d397 CM |
100 | struct extent_map_tree *tree; |
101 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
102 | return extent_write_full_page(tree, page, btree_get_extent, wbc); | |
103 | } | |
104 | int btree_readpage(struct file *file, struct page *page) | |
105 | { | |
106 | struct extent_map_tree *tree; | |
107 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
108 | return extent_read_full_page(tree, page, btree_get_extent); | |
109 | } | |
22b0ebda | 110 | |
5f39d397 CM |
111 | static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags) |
112 | { | |
113 | struct extent_map_tree *tree; | |
114 | int ret; | |
d98237b3 | 115 | |
5f39d397 CM |
116 | BUG_ON(page->private != 1); |
117 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
118 | ret = try_release_extent_mapping(tree, page); | |
119 | if (ret == 1) { | |
120 | ClearPagePrivate(page); | |
121 | set_page_private(page, 0); | |
122 | page_cache_release(page); | |
123 | } | |
d98237b3 CM |
124 | return ret; |
125 | } | |
126 | ||
5f39d397 | 127 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
d98237b3 | 128 | { |
5f39d397 CM |
129 | struct extent_map_tree *tree; |
130 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
131 | extent_invalidatepage(tree, page, offset); | |
132 | btree_releasepage(page, GFP_NOFS); | |
d98237b3 CM |
133 | } |
134 | ||
f254e52c CM |
135 | int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, |
136 | char *result) | |
87cbda5c | 137 | { |
5f39d397 CM |
138 | return 0; |
139 | #if 0 | |
11bd143f CM |
140 | u32 crc; |
141 | crc = crc32c(0, data, len); | |
142 | memcpy(result, &crc, BTRFS_CRC32_SIZE); | |
143 | return 0; | |
5f39d397 | 144 | #endif |
f254e52c | 145 | } |
11bd143f | 146 | |
5f39d397 CM |
147 | #if 0 |
148 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, | |
f254e52c CM |
149 | int verify) |
150 | { | |
5f39d397 | 151 | return 0; |
509659cd | 152 | char result[BTRFS_CRC32_SIZE]; |
f254e52c CM |
153 | int ret; |
154 | struct btrfs_node *node; | |
155 | ||
156 | ret = btrfs_csum_data(root, bh->b_data + BTRFS_CSUM_SIZE, | |
157 | bh->b_size - BTRFS_CSUM_SIZE, result); | |
158 | if (ret) | |
159 | return ret; | |
87cbda5c | 160 | if (verify) { |
509659cd | 161 | if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) { |
5af3981c CM |
162 | printk("btrfs: %s checksum verify failed on %llu\n", |
163 | root->fs_info->sb->s_id, | |
164 | (unsigned long long)bh_blocknr(bh)); | |
f254e52c CM |
165 | return 1; |
166 | } | |
167 | } else { | |
168 | node = btrfs_buffer_node(bh); | |
509659cd | 169 | memcpy(node->header.csum, result, BTRFS_CRC32_SIZE); |
f254e52c | 170 | } |
87cbda5c CM |
171 | return 0; |
172 | } | |
5f39d397 | 173 | #endif |
87cbda5c | 174 | |
5f39d397 | 175 | #if 0 |
d98237b3 | 176 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 177 | { |
87cbda5c | 178 | struct buffer_head *bh; |
0f7d52f4 | 179 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 180 | struct buffer_head *head; |
87cbda5c CM |
181 | if (!page_has_buffers(page)) { |
182 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
183 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
184 | } | |
185 | head = page_buffers(page); | |
186 | bh = head; | |
187 | do { | |
188 | if (buffer_dirty(bh)) | |
189 | csum_tree_block(root, bh, 0); | |
190 | bh = bh->b_this_page; | |
191 | } while (bh != head); | |
d98237b3 | 192 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb | 193 | } |
5f39d397 | 194 | #endif |
eb60ceac | 195 | |
d98237b3 CM |
196 | static struct address_space_operations btree_aops = { |
197 | .readpage = btree_readpage, | |
198 | .writepage = btree_writepage, | |
5f39d397 CM |
199 | .releasepage = btree_releasepage, |
200 | .invalidatepage = btree_invalidatepage, | |
d98237b3 CM |
201 | .sync_page = block_sync_page, |
202 | }; | |
203 | ||
090d1875 CM |
204 | int readahead_tree_block(struct btrfs_root *root, u64 blocknr) |
205 | { | |
5f39d397 CM |
206 | struct extent_buffer *buf = NULL; |
207 | struct inode *btree_inode = root->fs_info->btree_inode; | |
de428b63 | 208 | int ret = 0; |
090d1875 | 209 | |
5f39d397 CM |
210 | buf = btrfs_find_create_tree_block(root, blocknr); |
211 | if (!buf) | |
090d1875 | 212 | return 0; |
5f39d397 CM |
213 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
214 | buf, 0); | |
215 | free_extent_buffer(buf); | |
de428b63 | 216 | return ret; |
090d1875 CM |
217 | } |
218 | ||
5f39d397 | 219 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr) |
eb60ceac | 220 | { |
5f39d397 CM |
221 | struct extent_buffer *buf = NULL; |
222 | struct inode *btree_inode = root->fs_info->btree_inode; | |
223 | ||
224 | buf = btrfs_find_create_tree_block(root, blocknr); | |
225 | if (!buf) | |
226 | return NULL; | |
227 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, | |
228 | buf, 1); | |
229 | return buf; | |
eb60ceac CM |
230 | } |
231 | ||
e089f05c | 232 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 233 | struct extent_buffer *buf) |
ed2ff2cb | 234 | { |
5f39d397 CM |
235 | struct inode *btree_inode = root->fs_info->btree_inode; |
236 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | int wait_on_tree_block_writeback(struct btrfs_root *root, | |
241 | struct extent_buffer *buf) | |
242 | { | |
243 | struct inode *btree_inode = root->fs_info->btree_inode; | |
244 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree, | |
245 | buf); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | int set_tree_block_dirty(struct btrfs_root *root, struct extent_buffer *buf) | |
250 | { | |
251 | struct inode *btree_inode = root->fs_info->btree_inode; | |
252 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); | |
ed2ff2cb CM |
253 | return 0; |
254 | } | |
255 | ||
2c90e5d6 | 256 | static int __setup_root(int blocksize, |
9f5fae2f CM |
257 | struct btrfs_root *root, |
258 | struct btrfs_fs_info *fs_info, | |
e20d96d6 | 259 | u64 objectid) |
d97e63b6 | 260 | { |
cfaa7295 | 261 | root->node = NULL; |
0f7d52f4 | 262 | root->inode = NULL; |
a28ec197 | 263 | root->commit_root = NULL; |
5f39d397 CM |
264 | root->sectorsize = blocksize; |
265 | root->nodesize = blocksize; | |
266 | root->leafsize = blocksize; | |
123abc88 | 267 | root->ref_cows = 0; |
9f5fae2f | 268 | root->fs_info = fs_info; |
0f7d52f4 CM |
269 | root->objectid = objectid; |
270 | root->last_trans = 0; | |
1b05da2e CM |
271 | root->highest_inode = 0; |
272 | root->last_inode_alloc = 0; | |
58176a96 | 273 | root->name = NULL; |
3768f368 CM |
274 | memset(&root->root_key, 0, sizeof(root->root_key)); |
275 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 276 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
58176a96 JB |
277 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
278 | init_completion(&root->kobj_unregister); | |
011410bd | 279 | init_rwsem(&root->snap_sem); |
6702ed49 CM |
280 | root->defrag_running = 0; |
281 | root->defrag_level = 0; | |
4d775673 | 282 | root->root_key.objectid = objectid; |
3768f368 CM |
283 | return 0; |
284 | } | |
285 | ||
2c90e5d6 | 286 | static int find_and_setup_root(int blocksize, |
9f5fae2f CM |
287 | struct btrfs_root *tree_root, |
288 | struct btrfs_fs_info *fs_info, | |
289 | u64 objectid, | |
e20d96d6 | 290 | struct btrfs_root *root) |
3768f368 CM |
291 | { |
292 | int ret; | |
293 | ||
2c90e5d6 | 294 | __setup_root(blocksize, root, fs_info, objectid); |
3768f368 CM |
295 | ret = btrfs_find_last_root(tree_root, objectid, |
296 | &root->root_item, &root->root_key); | |
297 | BUG_ON(ret); | |
298 | ||
299 | root->node = read_tree_block(root, | |
300 | btrfs_root_blocknr(&root->root_item)); | |
3768f368 | 301 | BUG_ON(!root->node); |
d97e63b6 CM |
302 | return 0; |
303 | } | |
304 | ||
5eda7b5e CM |
305 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
306 | struct btrfs_key *location) | |
0f7d52f4 CM |
307 | { |
308 | struct btrfs_root *root; | |
309 | struct btrfs_root *tree_root = fs_info->tree_root; | |
310 | struct btrfs_path *path; | |
5f39d397 | 311 | struct extent_buffer *l; |
1b05da2e | 312 | u64 highest_inode; |
0f7d52f4 CM |
313 | int ret = 0; |
314 | ||
5eda7b5e | 315 | root = kzalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 316 | if (!root) |
0f7d52f4 | 317 | return ERR_PTR(-ENOMEM); |
0f7d52f4 CM |
318 | if (location->offset == (u64)-1) { |
319 | ret = find_and_setup_root(fs_info->sb->s_blocksize, | |
320 | fs_info->tree_root, fs_info, | |
321 | location->objectid, root); | |
322 | if (ret) { | |
0f7d52f4 CM |
323 | kfree(root); |
324 | return ERR_PTR(ret); | |
325 | } | |
326 | goto insert; | |
327 | } | |
328 | ||
329 | __setup_root(fs_info->sb->s_blocksize, root, fs_info, | |
330 | location->objectid); | |
331 | ||
332 | path = btrfs_alloc_path(); | |
333 | BUG_ON(!path); | |
334 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
335 | if (ret != 0) { | |
0f7d52f4 CM |
336 | if (ret > 0) |
337 | ret = -ENOENT; | |
338 | goto out; | |
339 | } | |
5f39d397 CM |
340 | l = path->nodes[0]; |
341 | read_extent_buffer(l, &root->root_item, | |
342 | btrfs_item_ptr_offset(l, path->slots[0]), | |
0f7d52f4 | 343 | sizeof(root->root_item)); |
0f7d52f4 CM |
344 | ret = 0; |
345 | out: | |
346 | btrfs_release_path(root, path); | |
347 | btrfs_free_path(path); | |
348 | if (ret) { | |
349 | kfree(root); | |
350 | return ERR_PTR(ret); | |
351 | } | |
352 | root->node = read_tree_block(root, | |
353 | btrfs_root_blocknr(&root->root_item)); | |
354 | BUG_ON(!root->node); | |
355 | insert: | |
0f7d52f4 | 356 | root->ref_cows = 1; |
5eda7b5e CM |
357 | ret = btrfs_find_highest_inode(root, &highest_inode); |
358 | if (ret == 0) { | |
359 | root->highest_inode = highest_inode; | |
360 | root->last_inode_alloc = highest_inode; | |
361 | } | |
362 | return root; | |
363 | } | |
364 | ||
365 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, | |
58176a96 JB |
366 | struct btrfs_key *location, |
367 | const char *name, int namelen) | |
5eda7b5e CM |
368 | { |
369 | struct btrfs_root *root; | |
370 | int ret; | |
371 | ||
372 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
373 | (unsigned long)location->objectid); | |
374 | if (root) | |
375 | return root; | |
376 | ||
377 | root = btrfs_read_fs_root_no_radix(fs_info, location); | |
378 | if (IS_ERR(root)) | |
379 | return root; | |
2619ba1f CM |
380 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
381 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
382 | root); |
383 | if (ret) { | |
5f39d397 | 384 | free_extent_buffer(root->node); |
0f7d52f4 CM |
385 | kfree(root); |
386 | return ERR_PTR(ret); | |
387 | } | |
58176a96 JB |
388 | |
389 | ret = btrfs_set_root_name(root, name, namelen); | |
390 | if (ret) { | |
5f39d397 | 391 | free_extent_buffer(root->node); |
58176a96 JB |
392 | kfree(root); |
393 | return ERR_PTR(ret); | |
394 | } | |
395 | ||
396 | ret = btrfs_sysfs_add_root(root); | |
397 | if (ret) { | |
5f39d397 | 398 | free_extent_buffer(root->node); |
58176a96 JB |
399 | kfree(root->name); |
400 | kfree(root); | |
401 | return ERR_PTR(ret); | |
402 | } | |
403 | ||
5ce14bbc CM |
404 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
405 | root->root_key.objectid, root); | |
406 | BUG_ON(ret); | |
407 | ||
0f7d52f4 CM |
408 | return root; |
409 | } | |
410 | ||
2c90e5d6 | 411 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 412 | { |
e20d96d6 CM |
413 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
414 | GFP_NOFS); | |
415 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
416 | GFP_NOFS); | |
e20d96d6 CM |
417 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
418 | GFP_NOFS); | |
eb60ceac | 419 | int ret; |
39279cc3 | 420 | int err = -EIO; |
2c90e5d6 | 421 | struct btrfs_super_block *disk_super; |
eb60ceac | 422 | |
39279cc3 CM |
423 | if (!extent_root || !tree_root || !fs_info) { |
424 | err = -ENOMEM; | |
425 | goto fail; | |
426 | } | |
8ef97622 CM |
427 | init_bit_radix(&fs_info->pinned_radix); |
428 | init_bit_radix(&fs_info->pending_del_radix); | |
e37c9e69 | 429 | init_bit_radix(&fs_info->extent_map_radix); |
26b8003f | 430 | init_bit_radix(&fs_info->extent_ins_radix); |
0f7d52f4 | 431 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
9078a3e1 | 432 | INIT_RADIX_TREE(&fs_info->block_group_radix, GFP_KERNEL); |
be744175 | 433 | INIT_RADIX_TREE(&fs_info->block_group_data_radix, GFP_KERNEL); |
8fd17795 | 434 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 435 | INIT_LIST_HEAD(&fs_info->dead_roots); |
58176a96 JB |
436 | memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); |
437 | init_completion(&fs_info->kobj_unregister); | |
2c90e5d6 | 438 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 439 | fs_info->running_transaction = NULL; |
15ee9bc7 | 440 | fs_info->last_trans_committed = 0; |
9f5fae2f CM |
441 | fs_info->tree_root = tree_root; |
442 | fs_info->extent_root = extent_root; | |
e20d96d6 | 443 | fs_info->sb = sb; |
d98237b3 CM |
444 | fs_info->btree_inode = new_inode(sb); |
445 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 446 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
447 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
448 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
5f39d397 CM |
449 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
450 | fs_info->btree_inode->i_mapping, | |
451 | GFP_NOFS); | |
e66f709b | 452 | fs_info->do_barriers = 1; |
facda1e7 CM |
453 | fs_info->closing = 0; |
454 | ||
08607c1b | 455 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
0f7d52f4 CM |
456 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
457 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
458 | sizeof(struct btrfs_key)); | |
22b0ebda | 459 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 460 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
39279cc3 | 461 | |
79154b1b | 462 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 463 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 464 | |
2c90e5d6 CM |
465 | __setup_root(sb->s_blocksize, tree_root, |
466 | fs_info, BTRFS_ROOT_TREE_OBJECTID); | |
7eccb903 | 467 | |
2c90e5d6 CM |
468 | fs_info->sb_buffer = read_tree_block(tree_root, |
469 | BTRFS_SUPER_INFO_OFFSET / | |
470 | sb->s_blocksize); | |
d98237b3 | 471 | |
0f7d52f4 | 472 | if (!fs_info->sb_buffer) |
39279cc3 | 473 | goto fail_iput; |
39279cc3 | 474 | |
5f39d397 CM |
475 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
476 | sizeof(fs_info->super_copy)); | |
477 | ||
478 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, | |
479 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), | |
480 | BTRFS_FSID_SIZE); | |
481 | disk_super = &fs_info->super_copy; | |
0f7d52f4 | 482 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 483 | goto fail_sb_buffer; |
0f7d52f4 | 484 | |
8352d8a4 CM |
485 | i_size_write(fs_info->btree_inode, |
486 | btrfs_super_total_blocks(disk_super) << | |
487 | fs_info->btree_inode->i_blkbits); | |
488 | ||
39279cc3 CM |
489 | |
490 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, | |
491 | sizeof(disk_super->magic))) { | |
492 | printk("btrfs: valid FS not found on %s\n", sb->s_id); | |
493 | goto fail_sb_buffer; | |
494 | } | |
e20d96d6 CM |
495 | tree_root->node = read_tree_block(tree_root, |
496 | btrfs_super_root(disk_super)); | |
39279cc3 CM |
497 | if (!tree_root->node) |
498 | goto fail_sb_buffer; | |
3768f368 | 499 | |
2c90e5d6 CM |
500 | mutex_lock(&fs_info->fs_mutex); |
501 | ret = find_and_setup_root(sb->s_blocksize, tree_root, fs_info, | |
e20d96d6 | 502 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
39279cc3 CM |
503 | if (ret) { |
504 | mutex_unlock(&fs_info->fs_mutex); | |
505 | goto fail_tree_root; | |
506 | } | |
3768f368 | 507 | |
9078a3e1 CM |
508 | btrfs_read_block_groups(extent_root); |
509 | ||
0f7d52f4 | 510 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
5be6f7f1 | 511 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 512 | return tree_root; |
39279cc3 CM |
513 | |
514 | fail_tree_root: | |
5f39d397 | 515 | free_extent_buffer(tree_root->node); |
39279cc3 | 516 | fail_sb_buffer: |
5f39d397 | 517 | free_extent_buffer(fs_info->sb_buffer); |
39279cc3 CM |
518 | fail_iput: |
519 | iput(fs_info->btree_inode); | |
520 | fail: | |
521 | kfree(extent_root); | |
522 | kfree(tree_root); | |
523 | kfree(fs_info); | |
524 | return ERR_PTR(err); | |
eb60ceac CM |
525 | } |
526 | ||
e089f05c | 527 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 528 | *root) |
eb60ceac | 529 | { |
e66f709b | 530 | int ret; |
5f39d397 CM |
531 | struct extent_buffer *super = root->fs_info->sb_buffer; |
532 | struct inode *btree_inode = root->fs_info->btree_inode; | |
533 | ||
534 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super); | |
535 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, | |
536 | super->start, super->len); | |
537 | return ret; | |
cfaa7295 CM |
538 | } |
539 | ||
5eda7b5e | 540 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
2619ba1f CM |
541 | { |
542 | radix_tree_delete(&fs_info->fs_roots_radix, | |
543 | (unsigned long)root->root_key.objectid); | |
58176a96 | 544 | btrfs_sysfs_del_root(root); |
2619ba1f CM |
545 | if (root->inode) |
546 | iput(root->inode); | |
547 | if (root->node) | |
5f39d397 | 548 | free_extent_buffer(root->node); |
2619ba1f | 549 | if (root->commit_root) |
5f39d397 | 550 | free_extent_buffer(root->commit_root); |
58176a96 JB |
551 | if (root->name) |
552 | kfree(root->name); | |
2619ba1f CM |
553 | kfree(root); |
554 | return 0; | |
555 | } | |
556 | ||
35b7e476 | 557 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
558 | { |
559 | int ret; | |
560 | struct btrfs_root *gang[8]; | |
561 | int i; | |
562 | ||
563 | while(1) { | |
564 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
565 | (void **)gang, 0, | |
566 | ARRAY_SIZE(gang)); | |
567 | if (!ret) | |
568 | break; | |
2619ba1f | 569 | for (i = 0; i < ret; i++) |
5eda7b5e | 570 | btrfs_free_fs_root(fs_info, gang[i]); |
0f7d52f4 CM |
571 | } |
572 | return 0; | |
573 | } | |
b4100d64 | 574 | |
e20d96d6 | 575 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 576 | { |
3768f368 | 577 | int ret; |
e089f05c | 578 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 579 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 580 | |
facda1e7 | 581 | fs_info->closing = 1; |
08607c1b | 582 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 583 | mutex_lock(&fs_info->fs_mutex); |
6702ed49 | 584 | btrfs_defrag_dirty_roots(root->fs_info); |
79154b1b | 585 | trans = btrfs_start_transaction(root, 1); |
54aa1f4d | 586 | ret = btrfs_commit_transaction(trans, root); |
79154b1b CM |
587 | /* run commit again to drop the original snapshot */ |
588 | trans = btrfs_start_transaction(root, 1); | |
589 | btrfs_commit_transaction(trans, root); | |
590 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 591 | BUG_ON(ret); |
79154b1b | 592 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
593 | mutex_unlock(&fs_info->fs_mutex); |
594 | ||
595 | if (fs_info->extent_root->node) | |
5f39d397 | 596 | free_extent_buffer(fs_info->extent_root->node); |
0f7d52f4 | 597 | if (fs_info->tree_root->node) |
5f39d397 CM |
598 | free_extent_buffer(fs_info->tree_root->node); |
599 | free_extent_buffer(fs_info->sb_buffer); | |
0f7d52f4 CM |
600 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
601 | iput(fs_info->btree_inode); | |
7eccb903 | 602 | |
9078a3e1 | 603 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 CM |
604 | del_fs_roots(fs_info); |
605 | kfree(fs_info->extent_root); | |
0f7d52f4 | 606 | kfree(fs_info->tree_root); |
eb60ceac CM |
607 | return 0; |
608 | } | |
609 | ||
5f39d397 CM |
610 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
611 | { | |
612 | struct inode *btree_inode = buf->pages[0]->mapping->host; | |
613 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf); | |
614 | } | |
615 | ||
616 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) | |
ccd467d6 | 617 | { |
5f39d397 CM |
618 | struct inode *btree_inode = buf->pages[0]->mapping->host; |
619 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, | |
620 | buf); | |
621 | } | |
6702ed49 | 622 | |
5f39d397 CM |
623 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
624 | { | |
625 | struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; | |
626 | u64 transid = btrfs_header_generation(buf); | |
627 | struct inode *btree_inode = root->fs_info->btree_inode; | |
6702ed49 | 628 | |
ccd467d6 CM |
629 | if (transid != root->fs_info->generation) { |
630 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", | |
5f39d397 | 631 | (unsigned long long)extent_buffer_blocknr(buf), |
ccd467d6 CM |
632 | transid, root->fs_info->generation); |
633 | WARN_ON(1); | |
634 | } | |
5f39d397 | 635 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
eb60ceac CM |
636 | } |
637 | ||
d3c2fdcf | 638 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
35b7e476 | 639 | { |
d3c2fdcf CM |
640 | balance_dirty_pages_ratelimited_nr( |
641 | root->fs_info->btree_inode->i_mapping, nr); | |
35b7e476 | 642 | } |