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; | |
d8d5f3e1 | 213 | if (wbc->sync_mode == WB_SYNC_NONE) { |
793955bc CM |
214 | u64 num_dirty; |
215 | u64 start = 0; | |
216 | unsigned long thresh = 96 * 1024 * 1024; | |
448d640b CM |
217 | |
218 | if (wbc->for_kupdate) | |
219 | return 0; | |
220 | ||
ca664626 CM |
221 | if (current_is_pdflush()) { |
222 | thresh = 96 * 1024 * 1024; | |
223 | } else { | |
224 | thresh = 8 * 1024 * 1024; | |
225 | } | |
1832a6d5 CM |
226 | num_dirty = count_range_bits(tree, &start, (u64)-1, |
227 | thresh, EXTENT_DIRTY); | |
793955bc CM |
228 | if (num_dirty < thresh) { |
229 | return 0; | |
230 | } | |
231 | } | |
0da5468f CM |
232 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
233 | } | |
234 | ||
5f39d397 CM |
235 | int btree_readpage(struct file *file, struct page *page) |
236 | { | |
237 | struct extent_map_tree *tree; | |
238 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
239 | return extent_read_full_page(tree, page, btree_get_extent); | |
240 | } | |
22b0ebda | 241 | |
5f39d397 CM |
242 | static int btree_releasepage(struct page *page, gfp_t unused_gfp_flags) |
243 | { | |
244 | struct extent_map_tree *tree; | |
245 | int ret; | |
d98237b3 | 246 | |
5f39d397 CM |
247 | tree = &BTRFS_I(page->mapping->host)->extent_tree; |
248 | ret = try_release_extent_mapping(tree, page); | |
249 | if (ret == 1) { | |
250 | ClearPagePrivate(page); | |
251 | set_page_private(page, 0); | |
252 | page_cache_release(page); | |
253 | } | |
d98237b3 CM |
254 | return ret; |
255 | } | |
256 | ||
5f39d397 | 257 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
d98237b3 | 258 | { |
5f39d397 CM |
259 | struct extent_map_tree *tree; |
260 | tree = &BTRFS_I(page->mapping->host)->extent_tree; | |
261 | extent_invalidatepage(tree, page, offset); | |
262 | btree_releasepage(page, GFP_NOFS); | |
d98237b3 CM |
263 | } |
264 | ||
5f39d397 | 265 | #if 0 |
d98237b3 | 266 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 267 | { |
87cbda5c | 268 | struct buffer_head *bh; |
0f7d52f4 | 269 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 270 | struct buffer_head *head; |
87cbda5c CM |
271 | if (!page_has_buffers(page)) { |
272 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
273 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
274 | } | |
275 | head = page_buffers(page); | |
276 | bh = head; | |
277 | do { | |
278 | if (buffer_dirty(bh)) | |
279 | csum_tree_block(root, bh, 0); | |
280 | bh = bh->b_this_page; | |
281 | } while (bh != head); | |
d98237b3 | 282 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb | 283 | } |
5f39d397 | 284 | #endif |
eb60ceac | 285 | |
d98237b3 CM |
286 | static struct address_space_operations btree_aops = { |
287 | .readpage = btree_readpage, | |
288 | .writepage = btree_writepage, | |
0da5468f | 289 | .writepages = btree_writepages, |
5f39d397 CM |
290 | .releasepage = btree_releasepage, |
291 | .invalidatepage = btree_invalidatepage, | |
d98237b3 CM |
292 | .sync_page = block_sync_page, |
293 | }; | |
294 | ||
db94535d | 295 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
090d1875 | 296 | { |
5f39d397 CM |
297 | struct extent_buffer *buf = NULL; |
298 | struct inode *btree_inode = root->fs_info->btree_inode; | |
de428b63 | 299 | int ret = 0; |
090d1875 | 300 | |
db94535d | 301 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 | 302 | if (!buf) |
090d1875 | 303 | return 0; |
5f39d397 | 304 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, |
19c00ddc | 305 | buf, 0, 0); |
5f39d397 | 306 | free_extent_buffer(buf); |
de428b63 | 307 | return ret; |
090d1875 CM |
308 | } |
309 | ||
db94535d CM |
310 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
311 | u32 blocksize) | |
eb60ceac | 312 | { |
5f39d397 CM |
313 | struct extent_buffer *buf = NULL; |
314 | struct inode *btree_inode = root->fs_info->btree_inode; | |
19c00ddc CM |
315 | struct extent_map_tree *extent_tree; |
316 | int ret; | |
317 | ||
318 | extent_tree = &BTRFS_I(btree_inode)->extent_tree; | |
5f39d397 | 319 | |
db94535d | 320 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 CM |
321 | if (!buf) |
322 | return NULL; | |
323 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, | |
19c00ddc CM |
324 | buf, 0, 1); |
325 | if (buf->flags & EXTENT_CSUM) { | |
326 | return buf; | |
327 | } | |
328 | if (test_range_bit(extent_tree, buf->start, buf->start + buf->len - 1, | |
329 | EXTENT_CSUM, 1)) { | |
330 | buf->flags |= EXTENT_CSUM; | |
331 | return buf; | |
332 | } | |
333 | ret = csum_tree_block(root, buf, 1); | |
334 | set_extent_bits(extent_tree, buf->start, | |
335 | buf->start + buf->len - 1, | |
336 | EXTENT_CSUM, GFP_NOFS); | |
337 | buf->flags |= EXTENT_CSUM; | |
5f39d397 | 338 | return buf; |
eb60ceac CM |
339 | } |
340 | ||
e089f05c | 341 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 342 | struct extent_buffer *buf) |
ed2ff2cb | 343 | { |
5f39d397 CM |
344 | struct inode *btree_inode = root->fs_info->btree_inode; |
345 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); | |
346 | return 0; | |
347 | } | |
348 | ||
349 | int wait_on_tree_block_writeback(struct btrfs_root *root, | |
350 | struct extent_buffer *buf) | |
351 | { | |
352 | struct inode *btree_inode = root->fs_info->btree_inode; | |
353 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->extent_tree, | |
354 | buf); | |
355 | return 0; | |
356 | } | |
357 | ||
db94535d | 358 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
87ee04eb | 359 | u32 stripesize, struct btrfs_root *root, |
9f5fae2f | 360 | struct btrfs_fs_info *fs_info, |
e20d96d6 | 361 | u64 objectid) |
d97e63b6 | 362 | { |
cfaa7295 | 363 | root->node = NULL; |
0f7d52f4 | 364 | root->inode = NULL; |
a28ec197 | 365 | root->commit_root = NULL; |
db94535d CM |
366 | root->sectorsize = sectorsize; |
367 | root->nodesize = nodesize; | |
368 | root->leafsize = leafsize; | |
87ee04eb | 369 | root->stripesize = stripesize; |
123abc88 | 370 | root->ref_cows = 0; |
9f5fae2f | 371 | root->fs_info = fs_info; |
0f7d52f4 CM |
372 | root->objectid = objectid; |
373 | root->last_trans = 0; | |
1b05da2e CM |
374 | root->highest_inode = 0; |
375 | root->last_inode_alloc = 0; | |
58176a96 | 376 | root->name = NULL; |
3768f368 CM |
377 | memset(&root->root_key, 0, sizeof(root->root_key)); |
378 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 379 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
58176a96 JB |
380 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
381 | init_completion(&root->kobj_unregister); | |
011410bd | 382 | init_rwsem(&root->snap_sem); |
6702ed49 CM |
383 | root->defrag_running = 0; |
384 | root->defrag_level = 0; | |
4d775673 | 385 | root->root_key.objectid = objectid; |
3768f368 CM |
386 | return 0; |
387 | } | |
388 | ||
db94535d | 389 | static int find_and_setup_root(struct btrfs_root *tree_root, |
9f5fae2f CM |
390 | struct btrfs_fs_info *fs_info, |
391 | u64 objectid, | |
e20d96d6 | 392 | struct btrfs_root *root) |
3768f368 CM |
393 | { |
394 | int ret; | |
db94535d | 395 | u32 blocksize; |
3768f368 | 396 | |
db94535d | 397 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
398 | tree_root->sectorsize, tree_root->stripesize, |
399 | root, fs_info, objectid); | |
3768f368 CM |
400 | ret = btrfs_find_last_root(tree_root, objectid, |
401 | &root->root_item, &root->root_key); | |
402 | BUG_ON(ret); | |
403 | ||
db94535d CM |
404 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
405 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
406 | blocksize); | |
3768f368 | 407 | BUG_ON(!root->node); |
d97e63b6 CM |
408 | return 0; |
409 | } | |
410 | ||
5eda7b5e CM |
411 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
412 | struct btrfs_key *location) | |
0f7d52f4 CM |
413 | { |
414 | struct btrfs_root *root; | |
415 | struct btrfs_root *tree_root = fs_info->tree_root; | |
416 | struct btrfs_path *path; | |
5f39d397 | 417 | struct extent_buffer *l; |
1b05da2e | 418 | u64 highest_inode; |
db94535d | 419 | u32 blocksize; |
0f7d52f4 CM |
420 | int ret = 0; |
421 | ||
5eda7b5e | 422 | root = kzalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 423 | if (!root) |
0f7d52f4 | 424 | return ERR_PTR(-ENOMEM); |
0f7d52f4 | 425 | if (location->offset == (u64)-1) { |
db94535d | 426 | ret = find_and_setup_root(tree_root, fs_info, |
0f7d52f4 CM |
427 | location->objectid, root); |
428 | if (ret) { | |
0f7d52f4 CM |
429 | kfree(root); |
430 | return ERR_PTR(ret); | |
431 | } | |
432 | goto insert; | |
433 | } | |
434 | ||
db94535d | 435 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
436 | tree_root->sectorsize, tree_root->stripesize, |
437 | root, fs_info, location->objectid); | |
0f7d52f4 CM |
438 | |
439 | path = btrfs_alloc_path(); | |
440 | BUG_ON(!path); | |
441 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
442 | if (ret != 0) { | |
0f7d52f4 CM |
443 | if (ret > 0) |
444 | ret = -ENOENT; | |
445 | goto out; | |
446 | } | |
5f39d397 CM |
447 | l = path->nodes[0]; |
448 | read_extent_buffer(l, &root->root_item, | |
449 | btrfs_item_ptr_offset(l, path->slots[0]), | |
0f7d52f4 | 450 | sizeof(root->root_item)); |
44b36eb2 | 451 | memcpy(&root->root_key, location, sizeof(*location)); |
0f7d52f4 CM |
452 | ret = 0; |
453 | out: | |
454 | btrfs_release_path(root, path); | |
455 | btrfs_free_path(path); | |
456 | if (ret) { | |
457 | kfree(root); | |
458 | return ERR_PTR(ret); | |
459 | } | |
db94535d CM |
460 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
461 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
462 | blocksize); | |
0f7d52f4 CM |
463 | BUG_ON(!root->node); |
464 | insert: | |
0f7d52f4 | 465 | root->ref_cows = 1; |
5eda7b5e CM |
466 | ret = btrfs_find_highest_inode(root, &highest_inode); |
467 | if (ret == 0) { | |
468 | root->highest_inode = highest_inode; | |
469 | root->last_inode_alloc = highest_inode; | |
470 | } | |
471 | return root; | |
472 | } | |
473 | ||
edbd8d4e CM |
474 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
475 | struct btrfs_key *location) | |
5eda7b5e CM |
476 | { |
477 | struct btrfs_root *root; | |
478 | int ret; | |
479 | ||
edbd8d4e CM |
480 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
481 | return fs_info->tree_root; | |
482 | if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) | |
483 | return fs_info->extent_root; | |
484 | ||
5eda7b5e CM |
485 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
486 | (unsigned long)location->objectid); | |
487 | if (root) | |
488 | return root; | |
489 | ||
490 | root = btrfs_read_fs_root_no_radix(fs_info, location); | |
491 | if (IS_ERR(root)) | |
492 | return root; | |
2619ba1f CM |
493 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
494 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
495 | root); |
496 | if (ret) { | |
5f39d397 | 497 | free_extent_buffer(root->node); |
0f7d52f4 CM |
498 | kfree(root); |
499 | return ERR_PTR(ret); | |
500 | } | |
edbd8d4e CM |
501 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
502 | root->root_key.objectid, root); | |
503 | BUG_ON(ret); | |
504 | ||
505 | return root; | |
506 | } | |
507 | ||
508 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, | |
509 | struct btrfs_key *location, | |
510 | const char *name, int namelen) | |
511 | { | |
512 | struct btrfs_root *root; | |
513 | int ret; | |
514 | ||
515 | root = btrfs_read_fs_root_no_name(fs_info, location); | |
516 | if (!root) | |
517 | return NULL; | |
58176a96 JB |
518 | |
519 | ret = btrfs_set_root_name(root, name, namelen); | |
520 | if (ret) { | |
5f39d397 | 521 | free_extent_buffer(root->node); |
58176a96 JB |
522 | kfree(root); |
523 | return ERR_PTR(ret); | |
524 | } | |
525 | ||
526 | ret = btrfs_sysfs_add_root(root); | |
527 | if (ret) { | |
5f39d397 | 528 | free_extent_buffer(root->node); |
58176a96 JB |
529 | kfree(root->name); |
530 | kfree(root); | |
531 | return ERR_PTR(ret); | |
532 | } | |
0f7d52f4 CM |
533 | return root; |
534 | } | |
19c00ddc CM |
535 | #if 0 |
536 | static int add_hasher(struct btrfs_fs_info *info, char *type) { | |
537 | struct btrfs_hasher *hasher; | |
538 | ||
539 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); | |
540 | if (!hasher) | |
541 | return -ENOMEM; | |
542 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); | |
543 | if (!hasher->hash_tfm) { | |
544 | kfree(hasher); | |
545 | return -EINVAL; | |
546 | } | |
547 | spin_lock(&info->hash_lock); | |
548 | list_add(&hasher->list, &info->hashers); | |
549 | spin_unlock(&info->hash_lock); | |
550 | return 0; | |
551 | } | |
552 | #endif | |
2c90e5d6 | 553 | struct btrfs_root *open_ctree(struct super_block *sb) |
2e635a27 | 554 | { |
db94535d CM |
555 | u32 sectorsize; |
556 | u32 nodesize; | |
557 | u32 leafsize; | |
558 | u32 blocksize; | |
87ee04eb | 559 | u32 stripesize; |
e20d96d6 CM |
560 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
561 | GFP_NOFS); | |
562 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
563 | GFP_NOFS); | |
e20d96d6 CM |
564 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
565 | GFP_NOFS); | |
eb60ceac | 566 | int ret; |
39279cc3 | 567 | int err = -EIO; |
2c90e5d6 | 568 | struct btrfs_super_block *disk_super; |
eb60ceac | 569 | |
39279cc3 CM |
570 | if (!extent_root || !tree_root || !fs_info) { |
571 | err = -ENOMEM; | |
572 | goto fail; | |
573 | } | |
0f7d52f4 | 574 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
8fd17795 | 575 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 576 | INIT_LIST_HEAD(&fs_info->dead_roots); |
19c00ddc CM |
577 | INIT_LIST_HEAD(&fs_info->hashers); |
578 | spin_lock_init(&fs_info->hash_lock); | |
1832a6d5 | 579 | spin_lock_init(&fs_info->delalloc_lock); |
19c00ddc | 580 | |
58176a96 JB |
581 | memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); |
582 | init_completion(&fs_info->kobj_unregister); | |
2c90e5d6 | 583 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 584 | fs_info->running_transaction = NULL; |
15ee9bc7 | 585 | fs_info->last_trans_committed = 0; |
9f5fae2f CM |
586 | fs_info->tree_root = tree_root; |
587 | fs_info->extent_root = extent_root; | |
e20d96d6 | 588 | fs_info->sb = sb; |
b6cda9bc | 589 | fs_info->mount_opt = 0; |
c59f8951 | 590 | fs_info->max_extent = (u64)-1; |
1832a6d5 | 591 | fs_info->delalloc_bytes = 0; |
d98237b3 CM |
592 | fs_info->btree_inode = new_inode(sb); |
593 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 594 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
595 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
596 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
5f39d397 CM |
597 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
598 | fs_info->btree_inode->i_mapping, | |
599 | GFP_NOFS); | |
0da5468f CM |
600 | BTRFS_I(fs_info->btree_inode)->extent_tree.ops = &btree_extent_map_ops; |
601 | ||
f510cfec CM |
602 | extent_map_tree_init(&fs_info->free_space_cache, |
603 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
96b5179d CM |
604 | extent_map_tree_init(&fs_info->block_group_cache, |
605 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
1a5bc167 CM |
606 | extent_map_tree_init(&fs_info->pinned_extents, |
607 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
608 | extent_map_tree_init(&fs_info->pending_del, | |
609 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
610 | extent_map_tree_init(&fs_info->extent_ins, | |
611 | fs_info->btree_inode->i_mapping, GFP_NOFS); | |
e66f709b | 612 | fs_info->do_barriers = 1; |
facda1e7 | 613 | fs_info->closing = 0; |
324ae4df | 614 | fs_info->total_pinned = 0; |
6da6abae CM |
615 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
616 | INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); | |
617 | #else | |
08607c1b | 618 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
6da6abae | 619 | #endif |
0f7d52f4 CM |
620 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
621 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
622 | sizeof(struct btrfs_key)); | |
22b0ebda | 623 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 624 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
39279cc3 | 625 | |
79154b1b | 626 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 627 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 628 | |
19c00ddc CM |
629 | #if 0 |
630 | ret = add_hasher(fs_info, "crc32c"); | |
631 | if (ret) { | |
632 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); | |
633 | err = -ENOMEM; | |
634 | goto fail_iput; | |
635 | } | |
636 | #endif | |
87ee04eb | 637 | __setup_root(512, 512, 512, 512, tree_root, |
2c90e5d6 | 638 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
7eccb903 | 639 | |
2c90e5d6 | 640 | fs_info->sb_buffer = read_tree_block(tree_root, |
db94535d CM |
641 | BTRFS_SUPER_INFO_OFFSET, |
642 | 512); | |
d98237b3 | 643 | |
0f7d52f4 | 644 | if (!fs_info->sb_buffer) |
39279cc3 | 645 | goto fail_iput; |
39279cc3 | 646 | |
5f39d397 CM |
647 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
648 | sizeof(fs_info->super_copy)); | |
649 | ||
650 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, | |
651 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), | |
652 | BTRFS_FSID_SIZE); | |
653 | disk_super = &fs_info->super_copy; | |
0f7d52f4 | 654 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 655 | goto fail_sb_buffer; |
0f7d52f4 | 656 | |
db94535d CM |
657 | nodesize = btrfs_super_nodesize(disk_super); |
658 | leafsize = btrfs_super_leafsize(disk_super); | |
659 | sectorsize = btrfs_super_sectorsize(disk_super); | |
87ee04eb | 660 | stripesize = btrfs_super_stripesize(disk_super); |
db94535d CM |
661 | tree_root->nodesize = nodesize; |
662 | tree_root->leafsize = leafsize; | |
663 | tree_root->sectorsize = sectorsize; | |
87ee04eb | 664 | tree_root->stripesize = stripesize; |
ff79f819 | 665 | sb_set_blocksize(sb, sectorsize); |
db94535d | 666 | |
8352d8a4 | 667 | i_size_write(fs_info->btree_inode, |
db94535d | 668 | btrfs_super_total_bytes(disk_super)); |
8352d8a4 | 669 | |
39279cc3 CM |
670 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
671 | sizeof(disk_super->magic))) { | |
672 | printk("btrfs: valid FS not found on %s\n", sb->s_id); | |
673 | goto fail_sb_buffer; | |
674 | } | |
19c00ddc | 675 | |
db94535d CM |
676 | blocksize = btrfs_level_size(tree_root, |
677 | btrfs_super_root_level(disk_super)); | |
19c00ddc | 678 | |
e20d96d6 | 679 | tree_root->node = read_tree_block(tree_root, |
db94535d CM |
680 | btrfs_super_root(disk_super), |
681 | blocksize); | |
39279cc3 CM |
682 | if (!tree_root->node) |
683 | goto fail_sb_buffer; | |
3768f368 | 684 | |
2c90e5d6 | 685 | mutex_lock(&fs_info->fs_mutex); |
db94535d CM |
686 | |
687 | ret = find_and_setup_root(tree_root, fs_info, | |
e20d96d6 | 688 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
39279cc3 CM |
689 | if (ret) { |
690 | mutex_unlock(&fs_info->fs_mutex); | |
691 | goto fail_tree_root; | |
692 | } | |
3768f368 | 693 | |
9078a3e1 CM |
694 | btrfs_read_block_groups(extent_root); |
695 | ||
0f7d52f4 | 696 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
5be6f7f1 | 697 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 698 | return tree_root; |
39279cc3 CM |
699 | |
700 | fail_tree_root: | |
5f39d397 | 701 | free_extent_buffer(tree_root->node); |
39279cc3 | 702 | fail_sb_buffer: |
5f39d397 | 703 | free_extent_buffer(fs_info->sb_buffer); |
39279cc3 CM |
704 | fail_iput: |
705 | iput(fs_info->btree_inode); | |
706 | fail: | |
707 | kfree(extent_root); | |
708 | kfree(tree_root); | |
709 | kfree(fs_info); | |
710 | return ERR_PTR(err); | |
eb60ceac CM |
711 | } |
712 | ||
e089f05c | 713 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 714 | *root) |
eb60ceac | 715 | { |
e66f709b | 716 | int ret; |
5f39d397 CM |
717 | struct extent_buffer *super = root->fs_info->sb_buffer; |
718 | struct inode *btree_inode = root->fs_info->btree_inode; | |
719 | ||
720 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, super); | |
721 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, | |
722 | super->start, super->len); | |
723 | return ret; | |
cfaa7295 CM |
724 | } |
725 | ||
5eda7b5e | 726 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
2619ba1f CM |
727 | { |
728 | radix_tree_delete(&fs_info->fs_roots_radix, | |
729 | (unsigned long)root->root_key.objectid); | |
58176a96 | 730 | btrfs_sysfs_del_root(root); |
2619ba1f CM |
731 | if (root->inode) |
732 | iput(root->inode); | |
733 | if (root->node) | |
5f39d397 | 734 | free_extent_buffer(root->node); |
2619ba1f | 735 | if (root->commit_root) |
5f39d397 | 736 | free_extent_buffer(root->commit_root); |
58176a96 JB |
737 | if (root->name) |
738 | kfree(root->name); | |
2619ba1f CM |
739 | kfree(root); |
740 | return 0; | |
741 | } | |
742 | ||
35b7e476 | 743 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
744 | { |
745 | int ret; | |
746 | struct btrfs_root *gang[8]; | |
747 | int i; | |
748 | ||
749 | while(1) { | |
750 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
751 | (void **)gang, 0, | |
752 | ARRAY_SIZE(gang)); | |
753 | if (!ret) | |
754 | break; | |
2619ba1f | 755 | for (i = 0; i < ret; i++) |
5eda7b5e | 756 | btrfs_free_fs_root(fs_info, gang[i]); |
0f7d52f4 CM |
757 | } |
758 | return 0; | |
759 | } | |
b4100d64 | 760 | |
e20d96d6 | 761 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 762 | { |
3768f368 | 763 | int ret; |
e089f05c | 764 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 765 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 766 | |
facda1e7 | 767 | fs_info->closing = 1; |
08607c1b | 768 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 769 | mutex_lock(&fs_info->fs_mutex); |
6702ed49 | 770 | btrfs_defrag_dirty_roots(root->fs_info); |
79154b1b | 771 | trans = btrfs_start_transaction(root, 1); |
54aa1f4d | 772 | ret = btrfs_commit_transaction(trans, root); |
79154b1b CM |
773 | /* run commit again to drop the original snapshot */ |
774 | trans = btrfs_start_transaction(root, 1); | |
775 | btrfs_commit_transaction(trans, root); | |
776 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 777 | BUG_ON(ret); |
79154b1b | 778 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
779 | mutex_unlock(&fs_info->fs_mutex); |
780 | ||
781 | if (fs_info->extent_root->node) | |
5f39d397 | 782 | free_extent_buffer(fs_info->extent_root->node); |
f510cfec | 783 | |
0f7d52f4 | 784 | if (fs_info->tree_root->node) |
5f39d397 | 785 | free_extent_buffer(fs_info->tree_root->node); |
f510cfec | 786 | |
5f39d397 | 787 | free_extent_buffer(fs_info->sb_buffer); |
7eccb903 | 788 | |
9078a3e1 | 789 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 | 790 | del_fs_roots(fs_info); |
d10c5f31 CM |
791 | |
792 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); | |
793 | ||
794 | extent_map_tree_empty_lru(&fs_info->free_space_cache); | |
795 | extent_map_tree_empty_lru(&fs_info->block_group_cache); | |
796 | extent_map_tree_empty_lru(&fs_info->pinned_extents); | |
797 | extent_map_tree_empty_lru(&fs_info->pending_del); | |
798 | extent_map_tree_empty_lru(&fs_info->extent_ins); | |
19c00ddc | 799 | extent_map_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->extent_tree); |
d10c5f31 | 800 | |
db94535d | 801 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
d10c5f31 | 802 | |
db94535d | 803 | iput(fs_info->btree_inode); |
19c00ddc CM |
804 | #if 0 |
805 | while(!list_empty(&fs_info->hashers)) { | |
806 | struct btrfs_hasher *hasher; | |
807 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, | |
808 | hashers); | |
809 | list_del(&hasher->hashers); | |
810 | crypto_free_hash(&fs_info->hash_tfm); | |
811 | kfree(hasher); | |
812 | } | |
813 | #endif | |
0f7d52f4 | 814 | kfree(fs_info->extent_root); |
0f7d52f4 | 815 | kfree(fs_info->tree_root); |
eb60ceac CM |
816 | return 0; |
817 | } | |
818 | ||
5f39d397 CM |
819 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
820 | { | |
810191ff | 821 | struct inode *btree_inode = buf->first_page->mapping->host; |
5f39d397 CM |
822 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, buf); |
823 | } | |
824 | ||
825 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) | |
ccd467d6 | 826 | { |
810191ff | 827 | struct inode *btree_inode = buf->first_page->mapping->host; |
5f39d397 CM |
828 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->extent_tree, |
829 | buf); | |
830 | } | |
6702ed49 | 831 | |
5f39d397 CM |
832 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
833 | { | |
810191ff | 834 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
5f39d397 CM |
835 | u64 transid = btrfs_header_generation(buf); |
836 | struct inode *btree_inode = root->fs_info->btree_inode; | |
6702ed49 | 837 | |
ccd467d6 CM |
838 | if (transid != root->fs_info->generation) { |
839 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", | |
db94535d | 840 | (unsigned long long)buf->start, |
ccd467d6 CM |
841 | transid, root->fs_info->generation); |
842 | WARN_ON(1); | |
843 | } | |
5f39d397 | 844 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->extent_tree, buf); |
eb60ceac CM |
845 | } |
846 | ||
d3c2fdcf | 847 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
35b7e476 | 848 | { |
d3c2fdcf | 849 | balance_dirty_pages_ratelimited_nr( |
304fced6 | 850 | root->fs_info->btree_inode->i_mapping, 1); |
35b7e476 | 851 | } |
6b80053d CM |
852 | |
853 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) | |
854 | { | |
810191ff | 855 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
856 | struct inode *btree_inode = root->fs_info->btree_inode; |
857 | set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start, | |
858 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); | |
859 | } | |
860 | ||
861 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) | |
862 | { | |
810191ff | 863 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
864 | struct inode *btree_inode = root->fs_info->btree_inode; |
865 | set_extent_bits(&BTRFS_I(btree_inode)->extent_tree, buf->start, | |
866 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, | |
867 | GFP_NOFS); | |
868 | } | |
869 | ||
870 | int btrfs_buffer_defrag(struct extent_buffer *buf) | |
871 | { | |
810191ff | 872 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
873 | struct inode *btree_inode = root->fs_info->btree_inode; |
874 | return test_range_bit(&BTRFS_I(btree_inode)->extent_tree, | |
875 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); | |
876 | } | |
877 | ||
878 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) | |
879 | { | |
810191ff | 880 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
881 | struct inode *btree_inode = root->fs_info->btree_inode; |
882 | return test_range_bit(&BTRFS_I(btree_inode)->extent_tree, | |
883 | buf->start, buf->start + buf->len - 1, | |
884 | EXTENT_DEFRAG_DONE, 0); | |
885 | } | |
886 | ||
887 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) | |
888 | { | |
810191ff | 889 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
890 | struct inode *btree_inode = root->fs_info->btree_inode; |
891 | return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree, | |
892 | buf->start, buf->start + buf->len - 1, | |
893 | EXTENT_DEFRAG_DONE, GFP_NOFS); | |
894 | } | |
895 | ||
896 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) | |
897 | { | |
810191ff | 898 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
899 | struct inode *btree_inode = root->fs_info->btree_inode; |
900 | return clear_extent_bits(&BTRFS_I(btree_inode)->extent_tree, | |
901 | buf->start, buf->start + buf->len - 1, | |
902 | EXTENT_DEFRAG, GFP_NOFS); | |
903 | } | |
904 | ||
905 | int btrfs_read_buffer(struct extent_buffer *buf) | |
906 | { | |
810191ff | 907 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d CM |
908 | struct inode *btree_inode = root->fs_info->btree_inode; |
909 | return read_extent_buffer_pages(&BTRFS_I(btree_inode)->extent_tree, | |
19c00ddc | 910 | buf, 0, 1); |
6b80053d | 911 | } |
0da5468f CM |
912 | |
913 | static struct extent_map_ops btree_extent_map_ops = { | |
914 | .writepage_io_hook = btree_writepage_io_hook, | |
915 | }; |