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