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" |
db94535d | 31 | #include "print-tree.h" |
eb60ceac | 32 | |
5f39d397 CM |
33 | #if 0 |
34 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) | |
7eccb903 | 35 | { |
5f39d397 CM |
36 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
37 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", | |
38 | (unsigned long long)extent_buffer_blocknr(buf), | |
39 | (unsigned long long)btrfs_header_blocknr(buf)); | |
39279cc3 | 40 | return 1; |
d98237b3 | 41 | } |
9a8dd150 | 42 | return 0; |
eb60ceac | 43 | } |
5f39d397 | 44 | #endif |
eb60ceac | 45 | |
0da5468f CM |
46 | static struct extent_map_ops btree_extent_map_ops; |
47 | ||
5f39d397 | 48 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
db94535d | 49 | u64 bytenr, u32 blocksize) |
d98237b3 | 50 | { |
5f39d397 | 51 | struct inode *btree_inode = root->fs_info->btree_inode; |
f510cfec CM |
52 | struct extent_buffer *eb; |
53 | eb = find_extent_buffer(&BTRFS_I(btree_inode)->extent_tree, | |
db94535d | 54 | bytenr, blocksize, GFP_NOFS); |
f510cfec | 55 | return eb; |
5f39d397 | 56 | } |
d98237b3 | 57 | |
5f39d397 | 58 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, |
db94535d | 59 | u64 bytenr, u32 blocksize) |
5f39d397 CM |
60 | { |
61 | struct inode *btree_inode = root->fs_info->btree_inode; | |
f510cfec | 62 | struct extent_buffer *eb; |
db94535d | 63 | |
f510cfec | 64 | eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->extent_tree, |
19c00ddc | 65 | bytenr, blocksize, NULL, GFP_NOFS); |
f510cfec | 66 | return eb; |
d98237b3 CM |
67 | } |
68 | ||
5f39d397 CM |
69 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
70 | size_t page_offset, u64 start, u64 end, | |
71 | int create) | |
7eccb903 | 72 | { |
5f39d397 CM |
73 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
74 | struct extent_map *em; | |
75 | int ret; | |
76 | ||
77 | again: | |
78 | em = lookup_extent_mapping(em_tree, start, end); | |
79 | if (em) { | |
80 | goto out; | |
7eccb903 | 81 | } |
5f39d397 CM |
82 | em = alloc_extent_map(GFP_NOFS); |
83 | if (!em) { | |
84 | em = ERR_PTR(-ENOMEM); | |
85 | goto out; | |
86 | } | |
87 | em->start = 0; | |
88 | em->end = (i_size_read(inode) & ~((u64)PAGE_CACHE_SIZE -1)) - 1; | |
89 | em->block_start = 0; | |
90 | em->block_end = em->end; | |
91 | em->bdev = inode->i_sb->s_bdev; | |
92 | ret = add_extent_mapping(em_tree, em); | |
93 | if (ret == -EEXIST) { | |
94 | free_extent_map(em); | |
95 | em = NULL; | |
96 | goto again; | |
97 | } else if (ret) { | |
98 | em = ERR_PTR(ret); | |
99 | } | |
100 | out: | |
101 | return em; | |
7eccb903 CM |
102 | } |
103 | ||
19c00ddc CM |
104 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
105 | { | |
106 | return crc32c(seed, data, len); | |
107 | } | |
108 | ||
109 | void btrfs_csum_final(u32 crc, char *result) | |
110 | { | |
111 | *(__le32 *)result = ~cpu_to_le32(crc); | |
112 | } | |
113 | ||
114 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, | |
115 | int verify) | |
116 | { | |
117 | char result[BTRFS_CRC32_SIZE]; | |
118 | unsigned long len; | |
119 | unsigned long cur_len; | |
120 | unsigned long offset = BTRFS_CSUM_SIZE; | |
121 | char *map_token = NULL; | |
122 | char *kaddr; | |
123 | unsigned long map_start; | |
124 | unsigned long map_len; | |
125 | int err; | |
126 | u32 crc = ~(u32)0; | |
127 | ||
128 | len = buf->len - offset; | |
129 | while(len > 0) { | |
130 | err = map_private_extent_buffer(buf, offset, 32, | |
131 | &map_token, &kaddr, | |
132 | &map_start, &map_len, KM_USER0); | |
133 | if (err) { | |
134 | printk("failed to map extent buffer! %lu\n", | |
135 | offset); | |
136 | return 1; | |
137 | } | |
138 | cur_len = min(len, map_len - (offset - map_start)); | |
139 | crc = btrfs_csum_data(root, kaddr + offset - map_start, | |
140 | crc, cur_len); | |
141 | len -= cur_len; | |
142 | offset += cur_len; | |
143 | unmap_extent_buffer(buf, map_token, KM_USER0); | |
144 | } | |
145 | btrfs_csum_final(crc, result); | |
146 | ||
147 | if (verify) { | |
148 | if (memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { | |
149 | printk("btrfs: %s checksum verify failed on %llu\n", | |
150 | root->fs_info->sb->s_id, | |
151 | buf->start); | |
152 | return 1; | |
153 | } | |
154 | } else { | |
155 | write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); | |
156 | } | |
157 | return 0; | |
158 | } | |
159 | ||
160 | ||
161 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) | |
162 | { | |
163 | struct extent_map_tree *tree; | |
35ebb934 | 164 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
19c00ddc CM |
165 | u64 found_start; |
166 | int found_level; | |
167 | unsigned long len; | |
168 | struct extent_buffer *eb; | |
169 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
170 | ||
171 | if (page->private == EXTENT_PAGE_PRIVATE) | |
172 | goto out; | |
173 | if (!page->private) | |
174 | goto out; | |
175 | len = page->private >> 2; | |
176 | if (len == 0) { | |
177 | WARN_ON(1); | |
178 | } | |
179 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); | |
180 | read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1); | |
181 | found_start = btrfs_header_bytenr(eb); | |
182 | if (found_start != start) { | |
183 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", | |
184 | start, found_start, len); | |
185 | } | |
186 | found_level = btrfs_header_level(eb); | |
187 | csum_tree_block(root, eb, 0); | |
188 | free_extent_buffer(eb); | |
189 | out: | |
190 | return 0; | |
191 | } | |
192 | ||
0da5468f | 193 | static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) |
d98237b3 | 194 | { |
19c00ddc | 195 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
19c00ddc CM |
196 | |
197 | csum_dirty_buffer(root, page); | |
0da5468f CM |
198 | return 0; |
199 | } | |
200 | ||
201 | static int btree_writepage(struct page *page, struct writeback_control *wbc) | |
202 | { | |
203 | struct extent_map_tree *tree; | |
204 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
5f39d397 CM |
205 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
206 | } | |
0da5468f CM |
207 | |
208 | static int btree_writepages(struct address_space *mapping, | |
209 | struct writeback_control *wbc) | |
210 | { | |
211 | struct extent_map_tree *tree; | |
212 | tree = &BTRFS_I(mapping->host)->extent_tree; | |
8790bad7 | 213 | if (wbc->sync_mode == WB_SYNC_NONE && current_is_pdflush()) { |
793955bc CM |
214 | u64 num_dirty; |
215 | u64 start = 0; | |
216 | unsigned long thresh = 96 * 1024 * 1024; | |
217 | num_dirty = count_range_bits(tree, &start, thresh, EXTENT_DIRTY); | |
218 | if (num_dirty < thresh) { | |
219 | return 0; | |
220 | } | |
221 | } | |
0da5468f CM |
222 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
223 | } | |
224 | ||
5f39d397 CM |
225 | int btree_readpage(struct file *file, struct page *page) |
226 | { | |
227 | struct extent_map_tree *tree; | |
228 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
229 | return extent_read_full_page(tree, page, btree_get_extent); | |
230 | } | |
22b0ebda | 231 | |
5f39d397 CM |
232 | static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags) |
233 | { | |
234 | struct extent_map_tree *tree; | |
235 | int ret; | |
d98237b3 | 236 | |
5f39d397 CM |
237 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
238 | ret = try_release_extent_mapping(tree, page); | |
239 | if (ret == 1) { | |
240 | ClearPagePrivate(page); | |
241 | set_page_private(page, 0); | |
242 | page_cache_release(page); | |
243 | } | |
d98237b3 CM |
244 | return ret; |
245 | } | |
246 | ||
5f39d397 | 247 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
d98237b3 | 248 | { |
5f39d397 CM |
249 | struct extent_map_tree *tree; |
250 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
251 | extent_invalidatepage(tree, page, offset); | |
252 | btree_releasepage(page, GFP_NOFS); | |
d98237b3 CM |
253 | } |
254 | ||
5f39d397 | 255 | #if 0 |
d98237b3 | 256 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 257 | { |
87cbda5c | 258 | struct buffer_head *bh; |
0f7d52f4 | 259 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 260 | struct buffer_head *head; |
87cbda5c CM |
261 | if (!page_has_buffers(page)) { |
262 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
263 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
264 | } | |
265 | head = page_buffers(page); | |
266 | bh = head; | |
267 | do { | |
268 | if (buffer_dirty(bh)) | |
269 | csum_tree_block(root, bh, 0); | |
270 | bh = bh->b_this_page; | |
271 | } while (bh != head); | |
d98237b3 | 272 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb | 273 | } |
5f39d397 | 274 | #endif |
eb60ceac | 275 | |
d98237b3 CM |
276 | static struct address_space_operations btree_aops = { |
277 | .readpage = btree_readpage, | |
278 | .writepage = btree_writepage, | |
0da5468f | 279 | .writepages = btree_writepages, |
5f39d397 CM |
280 | .releasepage = btree_releasepage, |
281 | .invalidatepage = btree_invalidatepage, | |
d98237b3 CM |
282 | .sync_page = block_sync_page, |
283 | }; | |
284 | ||
db94535d | 285 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
090d1875 | 286 | { |
5f39d397 CM |
287 | struct extent_buffer *buf = NULL; |
288 | struct inode *btree_inode = root->fs_info->btree_inode; | |
de428b63 | 289 | int ret = 0; |
090d1875 | 290 | |
db94535d | 291 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 | 292 | if (!buf) |
090d1875 | 293 | return 0; |
5f39d397 | 294 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
19c00ddc | 295 | buf, 0, 0); |
5f39d397 | 296 | free_extent_buffer(buf); |
de428b63 | 297 | return ret; |
090d1875 CM |
298 | } |
299 | ||
db94535d CM |
300 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
301 | u32 blocksize) | |
eb60ceac | 302 | { |
5f39d397 CM |
303 | struct extent_buffer *buf = NULL; |
304 | struct inode *btree_inode = root->fs_info->btree_inode; | |
19c00ddc CM |
305 | struct extent_map_tree *extent_tree; |
306 | int ret; | |
307 | ||
308 | extent_tree = &BTRFS_I(btree_inode)->extent_tree; | |
5f39d397 | 309 | |
db94535d | 310 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 CM |
311 | if (!buf) |
312 | return NULL; | |
313 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, | |
19c00ddc CM |
314 | buf, 0, 1); |
315 | if (buf->flags & EXTENT_CSUM) { | |
316 | return buf; | |
317 | } | |
318 | if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1, | |
319 | EXTENT_CSUM, 1)) { | |
320 | buf->flags |= EXTENT_CSUM; | |
321 | return buf; | |
322 | } | |
323 | ret = csum_tree_block(root, buf, 1); | |
324 | set_extent_bits(extent_tree, buf->start, | |
325 | buf->start + buf->len - 1, | |
326 | EXTENT_CSUM, GFP_NOFS); | |
327 | buf->flags |= EXTENT_CSUM; | |
5f39d397 | 328 | return buf; |
eb60ceac CM |
329 | } |
330 | ||
e089f05c | 331 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 332 | struct extent_buffer *buf) |
ed2ff2cb | 333 | { |
5f39d397 CM |
334 | struct inode *btree_inode = root->fs_info->btree_inode; |
335 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); | |
336 | return 0; | |
337 | } | |
338 | ||
339 | int wait_on_tree_block_writeback(struct btrfs_root *root, | |
340 | struct extent_buffer *buf) | |
341 | { | |
342 | struct inode *btree_inode = root->fs_info->btree_inode; | |
343 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree, | |
344 | buf); | |
345 | return 0; | |
346 | } | |
347 | ||
db94535d | 348 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
9f5fae2f CM |
349 | struct btrfs_root *root, |
350 | struct btrfs_fs_info *fs_info, | |
e20d96d6 | 351 | u64 objectid) |
d97e63b6 | 352 | { |
cfaa7295 | 353 | root->node = NULL; |
0f7d52f4 | 354 | root->inode = NULL; |
a28ec197 | 355 | root->commit_root = NULL; |
db94535d CM |
356 | root->sectorsize = sectorsize; |
357 | root->nodesize = nodesize; | |
358 | root->leafsize = leafsize; | |
123abc88 | 359 | root->ref_cows = 0; |
9f5fae2f | 360 | root->fs_info = fs_info; |
0f7d52f4 CM |
361 | root->objectid = objectid; |
362 | root->last_trans = 0; | |
1b05da2e CM |
363 | root->highest_inode = 0; |
364 | root->last_inode_alloc = 0; | |
58176a96 | 365 | root->name = NULL; |
3768f368 CM |
366 | memset(&root->root_key, 0, sizeof(root->root_key)); |
367 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 368 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
58176a96 JB |
369 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
370 | init_completion(&root->kobj_unregister); | |
011410bd | 371 | init_rwsem(&root->snap_sem); |
6702ed49 CM |
372 | root->defrag_running = 0; |
373 | root->defrag_level = 0; | |
4d775673 | 374 | root->root_key.objectid = objectid; |
3768f368 CM |
375 | return 0; |
376 | } | |
377 | ||
db94535d | 378 | static int find_and_setup_root(struct btrfs_root *tree_root, |
9f5fae2f CM |
379 | struct btrfs_fs_info *fs_info, |
380 | u64 objectid, | |
e20d96d6 | 381 | struct btrfs_root *root) |
3768f368 CM |
382 | { |
383 | int ret; | |
db94535d | 384 | u32 blocksize; |
3768f368 | 385 | |
db94535d CM |
386 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
387 | tree_root->sectorsize, root, fs_info, objectid); | |
3768f368 CM |
388 | ret = btrfs_find_last_root(tree_root, objectid, |
389 | &root->root_item, &root->root_key); | |
390 | BUG_ON(ret); | |
391 | ||
db94535d CM |
392 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
393 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
394 | blocksize); | |
3768f368 | 395 | BUG_ON(!root->node); |
d97e63b6 CM |
396 | return 0; |
397 | } | |
398 | ||
5eda7b5e CM |
399 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
400 | struct btrfs_key *location) | |
0f7d52f4 CM |
401 | { |
402 | struct btrfs_root *root; | |
403 | struct btrfs_root *tree_root = fs_info->tree_root; | |
404 | struct btrfs_path *path; | |
5f39d397 | 405 | struct extent_buffer *l; |
1b05da2e | 406 | u64 highest_inode; |
db94535d | 407 | u32 blocksize; |
0f7d52f4 CM |
408 | int ret = 0; |
409 | ||
5eda7b5e | 410 | root = kzalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 411 | if (!root) |
0f7d52f4 | 412 | return ERR_PTR(-ENOMEM); |
0f7d52f4 | 413 | if (location->offset == (u64)-1) { |
db94535d | 414 | ret = find_and_setup_root(tree_root, fs_info, |
0f7d52f4 CM |
415 | location->objectid, root); |
416 | if (ret) { | |
0f7d52f4 CM |
417 | kfree(root); |
418 | return ERR_PTR(ret); | |
419 | } | |
420 | goto insert; | |
421 | } | |
422 | ||
db94535d CM |
423 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
424 | tree_root->sectorsize, root, fs_info, | |
0f7d52f4 CM |
425 | location->objectid); |
426 | ||
427 | path = btrfs_alloc_path(); | |
428 | BUG_ON(!path); | |
429 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
430 | if (ret != 0) { | |
0f7d52f4 CM |
431 | if (ret > 0) |
432 | ret = -ENOENT; | |
433 | goto out; | |
434 | } | |
5f39d397 CM |
435 | l = path->nodes[0]; |
436 | read_extent_buffer(l, &root->root_item, | |
437 | btrfs_item_ptr_offset(l, path->slots[0]), | |
0f7d52f4 | 438 | sizeof(root->root_item)); |
44b36eb2 | 439 | memcpy(&root->root_key, location, sizeof(*location)); |
0f7d52f4 CM |
440 | ret = 0; |
441 | out: | |
442 | btrfs_release_path(root, path); | |
443 | btrfs_free_path(path); | |
444 | if (ret) { | |
445 | kfree(root); | |
446 | return ERR_PTR(ret); | |
447 | } | |
db94535d CM |
448 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
449 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
450 | blocksize); | |
0f7d52f4 CM |
451 | BUG_ON(!root->node); |
452 | insert: | |
0f7d52f4 | 453 | root->ref_cows = 1; |
5eda7b5e CM |
454 | ret = btrfs_find_highest_inode(root, &highest_inode); |
455 | if (ret == 0) { | |
456 | root->highest_inode = highest_inode; | |
457 | root->last_inode_alloc = highest_inode; | |
458 | } | |
459 | return root; | |
460 | } | |
461 | ||
462 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, | |
58176a96 JB |
463 | struct btrfs_key *location, |
464 | const char *name, int namelen) | |
5eda7b5e CM |
465 | { |
466 | struct btrfs_root *root; | |
467 | int ret; | |
468 | ||
469 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
470 | (unsigned long)location->objectid); | |
471 | if (root) | |
472 | return root; | |
473 | ||
474 | root = btrfs_read_fs_root_no_radix(fs_info, location); | |
475 | if (IS_ERR(root)) | |
476 | return root; | |
2619ba1f CM |
477 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
478 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
479 | root); |
480 | if (ret) { | |
5f39d397 | 481 | free_extent_buffer(root->node); |
0f7d52f4 CM |
482 | kfree(root); |
483 | return ERR_PTR(ret); | |
484 | } | |
58176a96 JB |
485 | |
486 | ret = btrfs_set_root_name(root, name, namelen); | |
487 | if (ret) { | |
5f39d397 | 488 | free_extent_buffer(root->node); |
58176a96 JB |
489 | kfree(root); |
490 | return ERR_PTR(ret); | |
491 | } | |
492 | ||
493 | ret = btrfs_sysfs_add_root(root); | |
494 | if (ret) { | |
5f39d397 | 495 | free_extent_buffer(root->node); |
58176a96 JB |
496 | kfree(root->name); |
497 | kfree(root); | |
498 | return ERR_PTR(ret); | |
499 | } | |
500 | ||
5ce14bbc CM |
501 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
502 | root->root_key.objectid, root); | |
503 | BUG_ON(ret); | |
504 | ||
0f7d52f4 CM |
505 | return root; |
506 | } | |
19c00ddc CM |
507 | #if 0 |
508 | static int add_hasher(struct btrfs_fs_info *info, char *type) { | |
509 | struct btrfs_hasher *hasher; | |
510 | ||
511 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); | |
512 | if (!hasher) | |
513 | return -ENOMEM; | |
514 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); | |
515 | if (!hasher->hash_tfm) { | |
516 | kfree(hasher); | |
517 | return -EINVAL; | |
518 | } | |
519 | spin_lock(&info->hash_lock); | |
520 | list_add(&hasher->list, &info->hashers); | |
521 | spin_unlock(&info->hash_lock); | |
522 | return 0; | |
523 | } | |
524 | #endif | |
2c90e5d6 | 525 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 526 | { |
db94535d CM |
527 | u32 sectorsize; |
528 | u32 nodesize; | |
529 | u32 leafsize; | |
530 | u32 blocksize; | |
e20d96d6 CM |
531 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
532 | GFP_NOFS); | |
533 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
534 | GFP_NOFS); | |
e20d96d6 CM |
535 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
536 | GFP_NOFS); | |
eb60ceac | 537 | int ret; |
39279cc3 | 538 | int err = -EIO; |
2c90e5d6 | 539 | struct btrfs_super_block *disk_super; |
eb60ceac | 540 | |
39279cc3 CM |
541 | if (!extent_root || !tree_root || !fs_info) { |
542 | err = -ENOMEM; | |
543 | goto fail; | |
544 | } | |
0f7d52f4 | 545 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
8fd17795 | 546 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 547 | INIT_LIST_HEAD(&fs_info->dead_roots); |
19c00ddc CM |
548 | INIT_LIST_HEAD(&fs_info->hashers); |
549 | spin_lock_init(&fs_info->hash_lock); | |
550 | ||
58176a96 JB |
551 | memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); |
552 | init_completion(&fs_info->kobj_unregister); | |
2c90e5d6 | 553 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 554 | fs_info->running_transaction = NULL; |
15ee9bc7 | 555 | fs_info->last_trans_committed = 0; |
9f5fae2f CM |
556 | fs_info->tree_root = tree_root; |
557 | fs_info->extent_root = extent_root; | |
e20d96d6 | 558 | fs_info->sb = sb; |
d98237b3 CM |
559 | fs_info->btree_inode = new_inode(sb); |
560 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 561 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
562 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
563 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
5f39d397 CM |
564 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
565 | fs_info->btree_inode->i_mapping, | |
566 | GFP_NOFS); | |
0da5468f CM |
567 | BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops; |
568 | ||
f510cfec CM |
569 | extent_map_tree_init(&fs_info->free_space_cache, |
570 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
96b5179d CM |
571 | extent_map_tree_init(&fs_info->block_group_cache, |
572 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
1a5bc167 CM |
573 | extent_map_tree_init(&fs_info->pinned_extents, |
574 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
575 | extent_map_tree_init(&fs_info->pending_del, | |
576 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
577 | extent_map_tree_init(&fs_info->extent_ins, | |
578 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
e66f709b | 579 | fs_info->do_barriers = 1; |
facda1e7 | 580 | fs_info->closing = 0; |
324ae4df | 581 | fs_info->total_pinned = 0; |
facda1e7 | 582 | |
08607c1b | 583 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
0f7d52f4 CM |
584 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
585 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
586 | sizeof(struct btrfs_key)); | |
22b0ebda | 587 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 588 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
39279cc3 | 589 | |
79154b1b | 590 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 591 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 592 | |
19c00ddc CM |
593 | #if 0 |
594 | ret = add_hasher(fs_info, "crc32c"); | |
595 | if (ret) { | |
596 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); | |
597 | err = -ENOMEM; | |
598 | goto fail_iput; | |
599 | } | |
600 | #endif | |
db94535d | 601 | __setup_root(512, 512, 512, tree_root, |
2c90e5d6 | 602 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
7eccb903 | 603 | |
2c90e5d6 | 604 | fs_info->sb_buffer = read_tree_block(tree_root, |
db94535d CM |
605 | BTRFS_SUPER_INFO_OFFSET, |
606 | 512); | |
d98237b3 | 607 | |
0f7d52f4 | 608 | if (!fs_info->sb_buffer) |
39279cc3 | 609 | goto fail_iput; |
39279cc3 | 610 | |
5f39d397 CM |
611 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
612 | sizeof(fs_info->super_copy)); | |
613 | ||
614 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, | |
615 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), | |
616 | BTRFS_FSID_SIZE); | |
617 | disk_super = &fs_info->super_copy; | |
0f7d52f4 | 618 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 619 | goto fail_sb_buffer; |
0f7d52f4 | 620 | |
db94535d CM |
621 | nodesize = btrfs_super_nodesize(disk_super); |
622 | leafsize = btrfs_super_leafsize(disk_super); | |
623 | sectorsize = btrfs_super_sectorsize(disk_super); | |
624 | tree_root->nodesize = nodesize; | |
625 | tree_root->leafsize = leafsize; | |
626 | tree_root->sectorsize = sectorsize; | |
ff79f819 | 627 | sb_set_blocksize(sb, sectorsize); |
db94535d | 628 | |
8352d8a4 | 629 | i_size_write(fs_info->btree_inode, |
db94535d | 630 | btrfs_super_total_bytes(disk_super)); |
8352d8a4 | 631 | |
39279cc3 CM |
632 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
633 | sizeof(disk_super->magic))) { | |
634 | printk("btrfs: valid FS not found on %s\n", sb->s_id); | |
635 | goto fail_sb_buffer; | |
636 | } | |
19c00ddc | 637 | |
db94535d CM |
638 | blocksize = btrfs_level_size(tree_root, |
639 | btrfs_super_root_level(disk_super)); | |
19c00ddc | 640 | |
e20d96d6 | 641 | tree_root->node = read_tree_block(tree_root, |
db94535d CM |
642 | btrfs_super_root(disk_super), |
643 | blocksize); | |
39279cc3 CM |
644 | if (!tree_root->node) |
645 | goto fail_sb_buffer; | |
3768f368 | 646 | |
2c90e5d6 | 647 | mutex_lock(&fs_info->fs_mutex); |
db94535d CM |
648 | |
649 | ret = find_and_setup_root(tree_root, fs_info, | |
e20d96d6 | 650 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
39279cc3 CM |
651 | if (ret) { |
652 | mutex_unlock(&fs_info->fs_mutex); | |
653 | goto fail_tree_root; | |
654 | } | |
3768f368 | 655 | |
9078a3e1 CM |
656 | btrfs_read_block_groups(extent_root); |
657 | ||
0f7d52f4 | 658 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
5be6f7f1 | 659 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 660 | return tree_root; |
39279cc3 CM |
661 | |
662 | fail_tree_root: | |
5f39d397 | 663 | free_extent_buffer(tree_root->node); |
39279cc3 | 664 | fail_sb_buffer: |
5f39d397 | 665 | free_extent_buffer(fs_info->sb_buffer); |
39279cc3 CM |
666 | fail_iput: |
667 | iput(fs_info->btree_inode); | |
668 | fail: | |
669 | kfree(extent_root); | |
670 | kfree(tree_root); | |
671 | kfree(fs_info); | |
672 | return ERR_PTR(err); | |
eb60ceac CM |
673 | } |
674 | ||
e089f05c | 675 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 676 | *root) |
eb60ceac | 677 | { |
e66f709b | 678 | int ret; |
5f39d397 CM |
679 | struct extent_buffer *super = root->fs_info->sb_buffer; |
680 | struct inode *btree_inode = root->fs_info->btree_inode; | |
681 | ||
682 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super); | |
683 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, | |
684 | super->start, super->len); | |
685 | return ret; | |
cfaa7295 CM |
686 | } |
687 | ||
5eda7b5e | 688 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
2619ba1f CM |
689 | { |
690 | radix_tree_delete(&fs_info->fs_roots_radix, | |
691 | (unsigned long)root->root_key.objectid); | |
58176a96 | 692 | btrfs_sysfs_del_root(root); |
2619ba1f CM |
693 | if (root->inode) |
694 | iput(root->inode); | |
695 | if (root->node) | |
5f39d397 | 696 | free_extent_buffer(root->node); |
2619ba1f | 697 | if (root->commit_root) |
5f39d397 | 698 | free_extent_buffer(root->commit_root); |
58176a96 JB |
699 | if (root->name) |
700 | kfree(root->name); | |
2619ba1f CM |
701 | kfree(root); |
702 | return 0; | |
703 | } | |
704 | ||
35b7e476 | 705 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
706 | { |
707 | int ret; | |
708 | struct btrfs_root *gang[8]; | |
709 | int i; | |
710 | ||
711 | while(1) { | |
712 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
713 | (void **)gang, 0, | |
714 | ARRAY_SIZE(gang)); | |
715 | if (!ret) | |
716 | break; | |
2619ba1f | 717 | for (i = 0; i < ret; i++) |
5eda7b5e | 718 | btrfs_free_fs_root(fs_info, gang[i]); |
0f7d52f4 CM |
719 | } |
720 | return 0; | |
721 | } | |
b4100d64 | 722 | |
e20d96d6 | 723 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 724 | { |
3768f368 | 725 | int ret; |
e089f05c | 726 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 727 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 728 | |
facda1e7 | 729 | fs_info->closing = 1; |
08607c1b | 730 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 731 | mutex_lock(&fs_info->fs_mutex); |
6702ed49 | 732 | btrfs_defrag_dirty_roots(root->fs_info); |
79154b1b | 733 | trans = btrfs_start_transaction(root, 1); |
54aa1f4d | 734 | ret = btrfs_commit_transaction(trans, root); |
79154b1b CM |
735 | /* run commit again to drop the original snapshot */ |
736 | trans = btrfs_start_transaction(root, 1); | |
737 | btrfs_commit_transaction(trans, root); | |
738 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 739 | BUG_ON(ret); |
79154b1b | 740 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
741 | mutex_unlock(&fs_info->fs_mutex); |
742 | ||
743 | if (fs_info->extent_root->node) | |
5f39d397 | 744 | free_extent_buffer(fs_info->extent_root->node); |
f510cfec | 745 | |
0f7d52f4 | 746 | if (fs_info->tree_root->node) |
5f39d397 | 747 | free_extent_buffer(fs_info->tree_root->node); |
f510cfec | 748 | |
5f39d397 | 749 | free_extent_buffer(fs_info->sb_buffer); |
7eccb903 | 750 | |
9078a3e1 | 751 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 | 752 | del_fs_roots(fs_info); |
19c00ddc | 753 | extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree); |
db94535d CM |
754 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
755 | iput(fs_info->btree_inode); | |
19c00ddc CM |
756 | #if 0 |
757 | while(!list_empty(&fs_info->hashers)) { | |
758 | struct btrfs_hasher *hasher; | |
759 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, | |
760 | hashers); | |
761 | list_del(&hasher->hashers); | |
762 | crypto_free_hash(&fs_info->hash_tfm); | |
763 | kfree(hasher); | |
764 | } | |
765 | #endif | |
0f7d52f4 | 766 | kfree(fs_info->extent_root); |
0f7d52f4 | 767 | kfree(fs_info->tree_root); |
eb60ceac CM |
768 | return 0; |
769 | } | |
770 | ||
5f39d397 CM |
771 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
772 | { | |
810191ff | 773 | struct inode *btree_inode = buf->first_page->mapping->host; |
5f39d397 CM |
774 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf); |
775 | } | |
776 | ||
777 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) | |
ccd467d6 | 778 | { |
810191ff | 779 | struct inode *btree_inode = buf->first_page->mapping->host; |
5f39d397 CM |
780 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, |
781 | buf); | |
782 | } | |
6702ed49 | 783 | |
5f39d397 CM |
784 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
785 | { | |
810191ff | 786 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
5f39d397 CM |
787 | u64 transid = btrfs_header_generation(buf); |
788 | struct inode *btree_inode = root->fs_info->btree_inode; | |
6702ed49 | 789 | |
ccd467d6 CM |
790 | if (transid != root->fs_info->generation) { |
791 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", | |
db94535d | 792 | (unsigned long long)buf->start, |
ccd467d6 CM |
793 | transid, root->fs_info->generation); |
794 | WARN_ON(1); | |
795 | } | |
5f39d397 | 796 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
eb60ceac CM |
797 | } |
798 | ||
d3c2fdcf | 799 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
35b7e476 | 800 | { |
d3c2fdcf | 801 | balance_dirty_pages_ratelimited_nr( |
304fced6 | 802 | root->fs_info->btree_inode->i_mapping, 1); |
35b7e476 | 803 | } |
6b80053d CM |
804 | |
805 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) | |
806 | { | |
810191ff | 807 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
808 | struct inode *btree_inode = root->fs_info->btree_inode; |
809 | set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start, | |
810 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); | |
811 | } | |
812 | ||
813 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) | |
814 | { | |
810191ff | 815 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
816 | struct inode *btree_inode = root->fs_info->btree_inode; |
817 | set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start, | |
818 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, | |
819 | GFP_NOFS); | |
820 | } | |
821 | ||
822 | int btrfs_buffer_defrag(struct extent_buffer *buf) | |
823 | { | |
810191ff | 824 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
825 | struct inode *btree_inode = root->fs_info->btree_inode; |
826 | return test_range_bit(&BTRFS_I(btree_inode)->extent_tree, | |
827 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); | |
828 | } | |
829 | ||
830 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) | |
831 | { | |
810191ff | 832 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
833 | struct inode *btree_inode = root->fs_info->btree_inode; |
834 | return test_range_bit(&BTRFS_I(btree_inode)->extent_tree, | |
835 | buf->start, buf->start + buf->len - 1, | |
836 | EXTENT_DEFRAG_DONE, 0); | |
837 | } | |
838 | ||
839 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) | |
840 | { | |
810191ff | 841 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
842 | struct inode *btree_inode = root->fs_info->btree_inode; |
843 | return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree, | |
844 | buf->start, buf->start + buf->len - 1, | |
845 | EXTENT_DEFRAG_DONE, GFP_NOFS); | |
846 | } | |
847 | ||
848 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) | |
849 | { | |
810191ff | 850 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
851 | struct inode *btree_inode = root->fs_info->btree_inode; |
852 | return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree, | |
853 | buf->start, buf->start + buf->len - 1, | |
854 | EXTENT_DEFRAG, GFP_NOFS); | |
855 | } | |
856 | ||
857 | int btrfs_read_buffer(struct extent_buffer *buf) | |
858 | { | |
810191ff | 859 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
860 | struct inode *btree_inode = root->fs_info->btree_inode; |
861 | return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, | |
19c00ddc | 862 | buf, 0, 1); |
6b80053d | 863 | } |
0da5468f CM |
864 | |
865 | static struct extent_map_ops btree_extent_map_ops = { | |
866 | .writepage_io_hook = btree_writepage_io_hook, | |
867 | }; |