]>
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" |
0b86a832 | 31 | #include "volumes.h" |
db94535d | 32 | #include "print-tree.h" |
eb60ceac | 33 | |
5f39d397 CM |
34 | #if 0 |
35 | static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf) | |
7eccb903 | 36 | { |
5f39d397 CM |
37 | if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) { |
38 | printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n", | |
39 | (unsigned long long)extent_buffer_blocknr(buf), | |
40 | (unsigned long long)btrfs_header_blocknr(buf)); | |
39279cc3 | 41 | return 1; |
d98237b3 | 42 | } |
9a8dd150 | 43 | return 0; |
eb60ceac | 44 | } |
5f39d397 | 45 | #endif |
eb60ceac | 46 | |
d1310b2e | 47 | static struct extent_io_ops btree_extent_io_ops; |
0da5468f | 48 | |
5f39d397 | 49 | struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root, |
db94535d | 50 | u64 bytenr, u32 blocksize) |
d98237b3 | 51 | { |
5f39d397 | 52 | struct inode *btree_inode = root->fs_info->btree_inode; |
f510cfec | 53 | struct extent_buffer *eb; |
d1310b2e | 54 | eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree, |
db94535d | 55 | bytenr, blocksize, GFP_NOFS); |
f510cfec | 56 | return eb; |
5f39d397 | 57 | } |
d98237b3 | 58 | |
5f39d397 | 59 | struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, |
db94535d | 60 | u64 bytenr, u32 blocksize) |
5f39d397 CM |
61 | { |
62 | struct inode *btree_inode = root->fs_info->btree_inode; | |
f510cfec | 63 | struct extent_buffer *eb; |
db94535d | 64 | |
d1310b2e | 65 | eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree, |
19c00ddc | 66 | bytenr, blocksize, NULL, GFP_NOFS); |
f510cfec | 67 | return eb; |
d98237b3 CM |
68 | } |
69 | ||
5f39d397 | 70 | struct extent_map *btree_get_extent(struct inode *inode, struct page *page, |
d1310b2e | 71 | size_t page_offset, u64 start, u64 len, |
5f39d397 | 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: | |
d1310b2e CM |
79 | spin_lock(&em_tree->lock); |
80 | em = lookup_extent_mapping(em_tree, start, len); | |
81 | spin_unlock(&em_tree->lock); | |
5f39d397 CM |
82 | if (em) { |
83 | goto out; | |
7eccb903 | 84 | } |
5f39d397 CM |
85 | em = alloc_extent_map(GFP_NOFS); |
86 | if (!em) { | |
87 | em = ERR_PTR(-ENOMEM); | |
88 | goto out; | |
89 | } | |
90 | em->start = 0; | |
d1310b2e | 91 | em->len = i_size_read(inode); |
5f39d397 | 92 | em->block_start = 0; |
5f39d397 | 93 | em->bdev = inode->i_sb->s_bdev; |
d1310b2e CM |
94 | |
95 | spin_lock(&em_tree->lock); | |
5f39d397 | 96 | ret = add_extent_mapping(em_tree, em); |
d1310b2e CM |
97 | spin_unlock(&em_tree->lock); |
98 | ||
5f39d397 CM |
99 | if (ret == -EEXIST) { |
100 | free_extent_map(em); | |
101 | em = NULL; | |
102 | goto again; | |
103 | } else if (ret) { | |
104 | em = ERR_PTR(ret); | |
105 | } | |
106 | out: | |
107 | return em; | |
7eccb903 CM |
108 | } |
109 | ||
19c00ddc CM |
110 | u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len) |
111 | { | |
112 | return crc32c(seed, data, len); | |
113 | } | |
114 | ||
115 | void btrfs_csum_final(u32 crc, char *result) | |
116 | { | |
117 | *(__le32 *)result = ~cpu_to_le32(crc); | |
118 | } | |
119 | ||
120 | static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf, | |
121 | int verify) | |
122 | { | |
123 | char result[BTRFS_CRC32_SIZE]; | |
124 | unsigned long len; | |
125 | unsigned long cur_len; | |
126 | unsigned long offset = BTRFS_CSUM_SIZE; | |
127 | char *map_token = NULL; | |
128 | char *kaddr; | |
129 | unsigned long map_start; | |
130 | unsigned long map_len; | |
131 | int err; | |
132 | u32 crc = ~(u32)0; | |
133 | ||
134 | len = buf->len - offset; | |
135 | while(len > 0) { | |
136 | err = map_private_extent_buffer(buf, offset, 32, | |
137 | &map_token, &kaddr, | |
138 | &map_start, &map_len, KM_USER0); | |
139 | if (err) { | |
140 | printk("failed to map extent buffer! %lu\n", | |
141 | offset); | |
142 | return 1; | |
143 | } | |
144 | cur_len = min(len, map_len - (offset - map_start)); | |
145 | crc = btrfs_csum_data(root, kaddr + offset - map_start, | |
146 | crc, cur_len); | |
147 | len -= cur_len; | |
148 | offset += cur_len; | |
149 | unmap_extent_buffer(buf, map_token, KM_USER0); | |
150 | } | |
151 | btrfs_csum_final(crc, result); | |
152 | ||
153 | if (verify) { | |
e4204ded CM |
154 | int from_this_trans = 0; |
155 | ||
156 | if (root->fs_info->running_transaction && | |
157 | btrfs_header_generation(buf) == | |
158 | root->fs_info->running_transaction->transid) | |
159 | from_this_trans = 1; | |
160 | ||
161 | /* FIXME, this is not good */ | |
162 | if (from_this_trans == 0 && | |
163 | memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) { | |
164 | u32 val; | |
165 | u32 found = 0; | |
166 | memcpy(&found, result, BTRFS_CRC32_SIZE); | |
167 | ||
168 | read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE); | |
169 | printk("btrfs: %s checksum verify failed on %llu " | |
170 | "wanted %X found %X from_this_trans %d\n", | |
19c00ddc | 171 | root->fs_info->sb->s_id, |
e4204ded | 172 | buf->start, val, found, from_this_trans); |
19c00ddc CM |
173 | return 1; |
174 | } | |
175 | } else { | |
176 | write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE); | |
177 | } | |
178 | return 0; | |
179 | } | |
180 | ||
181 | ||
182 | int csum_dirty_buffer(struct btrfs_root *root, struct page *page) | |
183 | { | |
d1310b2e | 184 | struct extent_io_tree *tree; |
35ebb934 | 185 | u64 start = (u64)page->index << PAGE_CACHE_SHIFT; |
19c00ddc CM |
186 | u64 found_start; |
187 | int found_level; | |
188 | unsigned long len; | |
189 | struct extent_buffer *eb; | |
d1310b2e | 190 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
19c00ddc CM |
191 | |
192 | if (page->private == EXTENT_PAGE_PRIVATE) | |
193 | goto out; | |
194 | if (!page->private) | |
195 | goto out; | |
196 | len = page->private >> 2; | |
197 | if (len == 0) { | |
198 | WARN_ON(1); | |
199 | } | |
200 | eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS); | |
a86c12c7 CM |
201 | read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1, |
202 | btree_get_extent); | |
e18e4809 | 203 | btrfs_clear_buffer_defrag(eb); |
19c00ddc CM |
204 | found_start = btrfs_header_bytenr(eb); |
205 | if (found_start != start) { | |
206 | printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n", | |
207 | start, found_start, len); | |
55c69072 CM |
208 | WARN_ON(1); |
209 | goto err; | |
210 | } | |
211 | if (eb->first_page != page) { | |
212 | printk("bad first page %lu %lu\n", eb->first_page->index, | |
213 | page->index); | |
214 | WARN_ON(1); | |
215 | goto err; | |
216 | } | |
217 | if (!PageUptodate(page)) { | |
218 | printk("csum not up to date page %lu\n", page->index); | |
219 | WARN_ON(1); | |
220 | goto err; | |
19c00ddc CM |
221 | } |
222 | found_level = btrfs_header_level(eb); | |
223 | csum_tree_block(root, eb, 0); | |
55c69072 | 224 | err: |
19c00ddc CM |
225 | free_extent_buffer(eb); |
226 | out: | |
227 | return 0; | |
228 | } | |
229 | ||
0da5468f | 230 | static int btree_writepage_io_hook(struct page *page, u64 start, u64 end) |
d98237b3 | 231 | { |
19c00ddc | 232 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
19c00ddc CM |
233 | |
234 | csum_dirty_buffer(root, page); | |
0da5468f CM |
235 | return 0; |
236 | } | |
237 | ||
0b86a832 CM |
238 | static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio) |
239 | { | |
240 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
241 | u64 offset; | |
242 | offset = bio->bi_sector << 9; | |
243 | if (offset == BTRFS_SUPER_INFO_OFFSET) { | |
244 | bio->bi_bdev = root->fs_info->sb->s_bdev; | |
245 | submit_bio(rw, bio); | |
246 | return 0; | |
247 | } | |
248 | return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio); | |
249 | } | |
250 | ||
0da5468f CM |
251 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
252 | { | |
d1310b2e CM |
253 | struct extent_io_tree *tree; |
254 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
255 | return extent_write_full_page(tree, page, btree_get_extent, wbc); |
256 | } | |
0da5468f CM |
257 | |
258 | static int btree_writepages(struct address_space *mapping, | |
259 | struct writeback_control *wbc) | |
260 | { | |
d1310b2e CM |
261 | struct extent_io_tree *tree; |
262 | tree = &BTRFS_I(mapping->host)->io_tree; | |
d8d5f3e1 | 263 | if (wbc->sync_mode == WB_SYNC_NONE) { |
793955bc CM |
264 | u64 num_dirty; |
265 | u64 start = 0; | |
266 | unsigned long thresh = 96 * 1024 * 1024; | |
448d640b CM |
267 | |
268 | if (wbc->for_kupdate) | |
269 | return 0; | |
270 | ||
ca664626 CM |
271 | if (current_is_pdflush()) { |
272 | thresh = 96 * 1024 * 1024; | |
273 | } else { | |
274 | thresh = 8 * 1024 * 1024; | |
275 | } | |
1832a6d5 CM |
276 | num_dirty = count_range_bits(tree, &start, (u64)-1, |
277 | thresh, EXTENT_DIRTY); | |
793955bc CM |
278 | if (num_dirty < thresh) { |
279 | return 0; | |
280 | } | |
281 | } | |
0da5468f CM |
282 | return extent_writepages(tree, mapping, btree_get_extent, wbc); |
283 | } | |
284 | ||
5f39d397 CM |
285 | int btree_readpage(struct file *file, struct page *page) |
286 | { | |
d1310b2e CM |
287 | struct extent_io_tree *tree; |
288 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
289 | return extent_read_full_page(tree, page, btree_get_extent); |
290 | } | |
22b0ebda | 291 | |
70dec807 | 292 | static int btree_releasepage(struct page *page, gfp_t gfp_flags) |
5f39d397 | 293 | { |
d1310b2e CM |
294 | struct extent_io_tree *tree; |
295 | struct extent_map_tree *map; | |
5f39d397 | 296 | int ret; |
d98237b3 | 297 | |
d1310b2e CM |
298 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
299 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 300 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
5f39d397 CM |
301 | if (ret == 1) { |
302 | ClearPagePrivate(page); | |
303 | set_page_private(page, 0); | |
304 | page_cache_release(page); | |
305 | } | |
d98237b3 CM |
306 | return ret; |
307 | } | |
308 | ||
5f39d397 | 309 | static void btree_invalidatepage(struct page *page, unsigned long offset) |
d98237b3 | 310 | { |
d1310b2e CM |
311 | struct extent_io_tree *tree; |
312 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
5f39d397 CM |
313 | extent_invalidatepage(tree, page, offset); |
314 | btree_releasepage(page, GFP_NOFS); | |
d98237b3 CM |
315 | } |
316 | ||
5f39d397 | 317 | #if 0 |
d98237b3 | 318 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
ed2ff2cb | 319 | { |
87cbda5c | 320 | struct buffer_head *bh; |
0f7d52f4 | 321 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
87cbda5c | 322 | struct buffer_head *head; |
87cbda5c CM |
323 | if (!page_has_buffers(page)) { |
324 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | |
325 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
326 | } | |
327 | head = page_buffers(page); | |
328 | bh = head; | |
329 | do { | |
330 | if (buffer_dirty(bh)) | |
331 | csum_tree_block(root, bh, 0); | |
332 | bh = bh->b_this_page; | |
333 | } while (bh != head); | |
d98237b3 | 334 | return block_write_full_page(page, btree_get_block, wbc); |
ed2ff2cb | 335 | } |
5f39d397 | 336 | #endif |
eb60ceac | 337 | |
d98237b3 CM |
338 | static struct address_space_operations btree_aops = { |
339 | .readpage = btree_readpage, | |
340 | .writepage = btree_writepage, | |
0da5468f | 341 | .writepages = btree_writepages, |
5f39d397 CM |
342 | .releasepage = btree_releasepage, |
343 | .invalidatepage = btree_invalidatepage, | |
d98237b3 CM |
344 | .sync_page = block_sync_page, |
345 | }; | |
346 | ||
db94535d | 347 | int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize) |
090d1875 | 348 | { |
5f39d397 CM |
349 | struct extent_buffer *buf = NULL; |
350 | struct inode *btree_inode = root->fs_info->btree_inode; | |
de428b63 | 351 | int ret = 0; |
090d1875 | 352 | |
db94535d | 353 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 | 354 | if (!buf) |
090d1875 | 355 | return 0; |
d1310b2e | 356 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
a86c12c7 | 357 | buf, 0, 0, btree_get_extent); |
5f39d397 | 358 | free_extent_buffer(buf); |
de428b63 | 359 | return ret; |
090d1875 CM |
360 | } |
361 | ||
0b86a832 CM |
362 | static int close_all_devices(struct btrfs_fs_info *fs_info) |
363 | { | |
364 | struct list_head *list; | |
365 | struct list_head *next; | |
366 | struct btrfs_device *device; | |
367 | ||
8a4b83cc CM |
368 | list = &fs_info->fs_devices->devices; |
369 | list_for_each(next, list) { | |
0b86a832 | 370 | device = list_entry(next, struct btrfs_device, dev_list); |
8a4b83cc CM |
371 | if (device->bdev && device->bdev != fs_info->sb->s_bdev) |
372 | close_bdev_excl(device->bdev); | |
373 | device->bdev = NULL; | |
0b86a832 CM |
374 | } |
375 | return 0; | |
376 | } | |
377 | ||
db94535d CM |
378 | struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, |
379 | u32 blocksize) | |
eb60ceac | 380 | { |
5f39d397 CM |
381 | struct extent_buffer *buf = NULL; |
382 | struct inode *btree_inode = root->fs_info->btree_inode; | |
d1310b2e | 383 | struct extent_io_tree *io_tree; |
e4204ded | 384 | u64 end; |
19c00ddc CM |
385 | int ret; |
386 | ||
d1310b2e | 387 | io_tree = &BTRFS_I(btree_inode)->io_tree; |
5f39d397 | 388 | |
db94535d | 389 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
5f39d397 CM |
390 | if (!buf) |
391 | return NULL; | |
a86c12c7 CM |
392 | read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1, |
393 | btree_get_extent); | |
e4204ded CM |
394 | |
395 | if (buf->flags & EXTENT_CSUM) | |
19c00ddc | 396 | return buf; |
e4204ded CM |
397 | |
398 | end = buf->start + PAGE_CACHE_SIZE - 1; | |
d1310b2e | 399 | if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) { |
19c00ddc CM |
400 | buf->flags |= EXTENT_CSUM; |
401 | return buf; | |
402 | } | |
e4204ded | 403 | |
d1310b2e | 404 | lock_extent(io_tree, buf->start, end, GFP_NOFS); |
e4204ded | 405 | |
d1310b2e | 406 | if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) { |
e4204ded CM |
407 | buf->flags |= EXTENT_CSUM; |
408 | goto out_unlock; | |
409 | } | |
410 | ||
19c00ddc | 411 | ret = csum_tree_block(root, buf, 1); |
d1310b2e | 412 | set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS); |
19c00ddc | 413 | buf->flags |= EXTENT_CSUM; |
e4204ded CM |
414 | |
415 | out_unlock: | |
d1310b2e | 416 | unlock_extent(io_tree, buf->start, end, GFP_NOFS); |
5f39d397 | 417 | return buf; |
eb60ceac CM |
418 | } |
419 | ||
e089f05c | 420 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 421 | struct extent_buffer *buf) |
ed2ff2cb | 422 | { |
5f39d397 | 423 | struct inode *btree_inode = root->fs_info->btree_inode; |
55c69072 CM |
424 | if (btrfs_header_generation(buf) == |
425 | root->fs_info->running_transaction->transid) | |
d1310b2e | 426 | clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, |
55c69072 | 427 | buf); |
5f39d397 CM |
428 | return 0; |
429 | } | |
430 | ||
431 | int wait_on_tree_block_writeback(struct btrfs_root *root, | |
432 | struct extent_buffer *buf) | |
433 | { | |
434 | struct inode *btree_inode = root->fs_info->btree_inode; | |
d1310b2e | 435 | wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree, |
5f39d397 CM |
436 | buf); |
437 | return 0; | |
438 | } | |
439 | ||
db94535d | 440 | static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, |
87ee04eb | 441 | u32 stripesize, struct btrfs_root *root, |
9f5fae2f | 442 | struct btrfs_fs_info *fs_info, |
e20d96d6 | 443 | u64 objectid) |
d97e63b6 | 444 | { |
cfaa7295 | 445 | root->node = NULL; |
0f7d52f4 | 446 | root->inode = NULL; |
a28ec197 | 447 | root->commit_root = NULL; |
db94535d CM |
448 | root->sectorsize = sectorsize; |
449 | root->nodesize = nodesize; | |
450 | root->leafsize = leafsize; | |
87ee04eb | 451 | root->stripesize = stripesize; |
123abc88 | 452 | root->ref_cows = 0; |
0b86a832 CM |
453 | root->track_dirty = 0; |
454 | ||
9f5fae2f | 455 | root->fs_info = fs_info; |
0f7d52f4 CM |
456 | root->objectid = objectid; |
457 | root->last_trans = 0; | |
1b05da2e CM |
458 | root->highest_inode = 0; |
459 | root->last_inode_alloc = 0; | |
58176a96 | 460 | root->name = NULL; |
4313b399 | 461 | root->in_sysfs = 0; |
0b86a832 CM |
462 | |
463 | INIT_LIST_HEAD(&root->dirty_list); | |
3768f368 CM |
464 | memset(&root->root_key, 0, sizeof(root->root_key)); |
465 | memset(&root->root_item, 0, sizeof(root->root_item)); | |
6702ed49 | 466 | memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); |
58176a96 JB |
467 | memset(&root->root_kobj, 0, sizeof(root->root_kobj)); |
468 | init_completion(&root->kobj_unregister); | |
6702ed49 CM |
469 | root->defrag_running = 0; |
470 | root->defrag_level = 0; | |
4d775673 | 471 | root->root_key.objectid = objectid; |
3768f368 CM |
472 | return 0; |
473 | } | |
474 | ||
db94535d | 475 | static int find_and_setup_root(struct btrfs_root *tree_root, |
9f5fae2f CM |
476 | struct btrfs_fs_info *fs_info, |
477 | u64 objectid, | |
e20d96d6 | 478 | struct btrfs_root *root) |
3768f368 CM |
479 | { |
480 | int ret; | |
db94535d | 481 | u32 blocksize; |
3768f368 | 482 | |
db94535d | 483 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
484 | tree_root->sectorsize, tree_root->stripesize, |
485 | root, fs_info, objectid); | |
3768f368 CM |
486 | ret = btrfs_find_last_root(tree_root, objectid, |
487 | &root->root_item, &root->root_key); | |
488 | BUG_ON(ret); | |
489 | ||
db94535d CM |
490 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
491 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
492 | blocksize); | |
3768f368 | 493 | BUG_ON(!root->node); |
d97e63b6 CM |
494 | return 0; |
495 | } | |
496 | ||
5eda7b5e CM |
497 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, |
498 | struct btrfs_key *location) | |
0f7d52f4 CM |
499 | { |
500 | struct btrfs_root *root; | |
501 | struct btrfs_root *tree_root = fs_info->tree_root; | |
502 | struct btrfs_path *path; | |
5f39d397 | 503 | struct extent_buffer *l; |
1b05da2e | 504 | u64 highest_inode; |
db94535d | 505 | u32 blocksize; |
0f7d52f4 CM |
506 | int ret = 0; |
507 | ||
5eda7b5e | 508 | root = kzalloc(sizeof(*root), GFP_NOFS); |
0cf6c620 | 509 | if (!root) |
0f7d52f4 | 510 | return ERR_PTR(-ENOMEM); |
0f7d52f4 | 511 | if (location->offset == (u64)-1) { |
db94535d | 512 | ret = find_and_setup_root(tree_root, fs_info, |
0f7d52f4 CM |
513 | location->objectid, root); |
514 | if (ret) { | |
0f7d52f4 CM |
515 | kfree(root); |
516 | return ERR_PTR(ret); | |
517 | } | |
518 | goto insert; | |
519 | } | |
520 | ||
db94535d | 521 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
87ee04eb CM |
522 | tree_root->sectorsize, tree_root->stripesize, |
523 | root, fs_info, location->objectid); | |
0f7d52f4 CM |
524 | |
525 | path = btrfs_alloc_path(); | |
526 | BUG_ON(!path); | |
527 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | |
528 | if (ret != 0) { | |
0f7d52f4 CM |
529 | if (ret > 0) |
530 | ret = -ENOENT; | |
531 | goto out; | |
532 | } | |
5f39d397 CM |
533 | l = path->nodes[0]; |
534 | read_extent_buffer(l, &root->root_item, | |
535 | btrfs_item_ptr_offset(l, path->slots[0]), | |
0f7d52f4 | 536 | sizeof(root->root_item)); |
44b36eb2 | 537 | memcpy(&root->root_key, location, sizeof(*location)); |
0f7d52f4 CM |
538 | ret = 0; |
539 | out: | |
540 | btrfs_release_path(root, path); | |
541 | btrfs_free_path(path); | |
542 | if (ret) { | |
543 | kfree(root); | |
544 | return ERR_PTR(ret); | |
545 | } | |
db94535d CM |
546 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
547 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | |
548 | blocksize); | |
0f7d52f4 CM |
549 | BUG_ON(!root->node); |
550 | insert: | |
0f7d52f4 | 551 | root->ref_cows = 1; |
5eda7b5e CM |
552 | ret = btrfs_find_highest_inode(root, &highest_inode); |
553 | if (ret == 0) { | |
554 | root->highest_inode = highest_inode; | |
555 | root->last_inode_alloc = highest_inode; | |
556 | } | |
557 | return root; | |
558 | } | |
559 | ||
dc17ff8f CM |
560 | struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, |
561 | u64 root_objectid) | |
562 | { | |
563 | struct btrfs_root *root; | |
564 | ||
565 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID) | |
566 | return fs_info->tree_root; | |
567 | if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) | |
568 | return fs_info->extent_root; | |
569 | ||
570 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | |
571 | (unsigned long)root_objectid); | |
572 | return root; | |
573 | } | |
574 | ||
edbd8d4e CM |
575 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
576 | struct btrfs_key *location) | |
5eda7b5e CM |
577 | { |
578 | struct btrfs_root *root; | |
579 | int ret; | |
580 | ||
edbd8d4e CM |
581 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
582 | return fs_info->tree_root; | |
583 | if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID) | |
584 | return fs_info->extent_root; | |
585 | ||
5eda7b5e CM |
586 | root = radix_tree_lookup(&fs_info->fs_roots_radix, |
587 | (unsigned long)location->objectid); | |
588 | if (root) | |
589 | return root; | |
590 | ||
591 | root = btrfs_read_fs_root_no_radix(fs_info, location); | |
592 | if (IS_ERR(root)) | |
593 | return root; | |
2619ba1f CM |
594 | ret = radix_tree_insert(&fs_info->fs_roots_radix, |
595 | (unsigned long)root->root_key.objectid, | |
0f7d52f4 CM |
596 | root); |
597 | if (ret) { | |
5f39d397 | 598 | free_extent_buffer(root->node); |
0f7d52f4 CM |
599 | kfree(root); |
600 | return ERR_PTR(ret); | |
601 | } | |
edbd8d4e CM |
602 | ret = btrfs_find_dead_roots(fs_info->tree_root, |
603 | root->root_key.objectid, root); | |
604 | BUG_ON(ret); | |
605 | ||
606 | return root; | |
607 | } | |
608 | ||
609 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, | |
610 | struct btrfs_key *location, | |
611 | const char *name, int namelen) | |
612 | { | |
613 | struct btrfs_root *root; | |
614 | int ret; | |
615 | ||
616 | root = btrfs_read_fs_root_no_name(fs_info, location); | |
617 | if (!root) | |
618 | return NULL; | |
58176a96 | 619 | |
4313b399 CM |
620 | if (root->in_sysfs) |
621 | return root; | |
622 | ||
58176a96 JB |
623 | ret = btrfs_set_root_name(root, name, namelen); |
624 | if (ret) { | |
5f39d397 | 625 | free_extent_buffer(root->node); |
58176a96 JB |
626 | kfree(root); |
627 | return ERR_PTR(ret); | |
628 | } | |
629 | ||
630 | ret = btrfs_sysfs_add_root(root); | |
631 | if (ret) { | |
5f39d397 | 632 | free_extent_buffer(root->node); |
58176a96 JB |
633 | kfree(root->name); |
634 | kfree(root); | |
635 | return ERR_PTR(ret); | |
636 | } | |
4313b399 | 637 | root->in_sysfs = 1; |
0f7d52f4 CM |
638 | return root; |
639 | } | |
19c00ddc CM |
640 | #if 0 |
641 | static int add_hasher(struct btrfs_fs_info *info, char *type) { | |
642 | struct btrfs_hasher *hasher; | |
643 | ||
644 | hasher = kmalloc(sizeof(*hasher), GFP_NOFS); | |
645 | if (!hasher) | |
646 | return -ENOMEM; | |
647 | hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC); | |
648 | if (!hasher->hash_tfm) { | |
649 | kfree(hasher); | |
650 | return -EINVAL; | |
651 | } | |
652 | spin_lock(&info->hash_lock); | |
653 | list_add(&hasher->list, &info->hashers); | |
654 | spin_unlock(&info->hash_lock); | |
655 | return 0; | |
656 | } | |
657 | #endif | |
04160088 CM |
658 | |
659 | static int btrfs_congested_fn(void *congested_data, int bdi_bits) | |
660 | { | |
661 | struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data; | |
662 | int ret = 0; | |
663 | struct list_head *cur; | |
664 | struct btrfs_device *device; | |
665 | struct backing_dev_info *bdi; | |
666 | ||
667 | list_for_each(cur, &info->fs_devices->devices) { | |
668 | device = list_entry(cur, struct btrfs_device, dev_list); | |
669 | bdi = blk_get_backing_dev_info(device->bdev); | |
670 | if (bdi && bdi_congested(bdi, bdi_bits)) { | |
671 | ret = 1; | |
672 | break; | |
673 | } | |
674 | } | |
675 | return ret; | |
676 | } | |
677 | ||
678 | void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page) | |
679 | { | |
680 | struct list_head *cur; | |
681 | struct btrfs_device *device; | |
682 | struct btrfs_fs_info *info; | |
683 | ||
684 | info = (struct btrfs_fs_info *)bdi->unplug_io_data; | |
685 | list_for_each(cur, &info->fs_devices->devices) { | |
686 | device = list_entry(cur, struct btrfs_device, dev_list); | |
687 | bdi = blk_get_backing_dev_info(device->bdev); | |
688 | if (bdi->unplug_io_fn) { | |
689 | bdi->unplug_io_fn(bdi, page); | |
690 | } | |
691 | } | |
692 | } | |
693 | ||
694 | static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi) | |
695 | { | |
696 | bdi_init(bdi); | |
83041add | 697 | bdi->ra_pages = default_backing_dev_info.ra_pages * 4; |
04160088 CM |
698 | bdi->state = 0; |
699 | bdi->capabilities = default_backing_dev_info.capabilities; | |
700 | bdi->unplug_io_fn = btrfs_unplug_io_fn; | |
701 | bdi->unplug_io_data = info; | |
702 | bdi->congested_fn = btrfs_congested_fn; | |
703 | bdi->congested_data = info; | |
704 | return 0; | |
705 | } | |
706 | ||
8a4b83cc CM |
707 | struct btrfs_root *open_ctree(struct super_block *sb, |
708 | struct btrfs_fs_devices *fs_devices) | |
2e635a27 | 709 | { |
db94535d CM |
710 | u32 sectorsize; |
711 | u32 nodesize; | |
712 | u32 leafsize; | |
713 | u32 blocksize; | |
87ee04eb | 714 | u32 stripesize; |
e20d96d6 CM |
715 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
716 | GFP_NOFS); | |
717 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), | |
718 | GFP_NOFS); | |
e20d96d6 CM |
719 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
720 | GFP_NOFS); | |
0b86a832 CM |
721 | struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root), |
722 | GFP_NOFS); | |
723 | struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root), | |
724 | GFP_NOFS); | |
eb60ceac | 725 | int ret; |
39279cc3 | 726 | int err = -EIO; |
2c90e5d6 | 727 | struct btrfs_super_block *disk_super; |
eb60ceac | 728 | |
39279cc3 CM |
729 | if (!extent_root || !tree_root || !fs_info) { |
730 | err = -ENOMEM; | |
731 | goto fail; | |
732 | } | |
0f7d52f4 | 733 | INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); |
8fd17795 | 734 | INIT_LIST_HEAD(&fs_info->trans_list); |
facda1e7 | 735 | INIT_LIST_HEAD(&fs_info->dead_roots); |
19c00ddc CM |
736 | INIT_LIST_HEAD(&fs_info->hashers); |
737 | spin_lock_init(&fs_info->hash_lock); | |
1832a6d5 | 738 | spin_lock_init(&fs_info->delalloc_lock); |
cee36a03 | 739 | spin_lock_init(&fs_info->new_trans_lock); |
19c00ddc | 740 | |
58176a96 JB |
741 | memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); |
742 | init_completion(&fs_info->kobj_unregister); | |
2c90e5d6 | 743 | sb_set_blocksize(sb, 4096); |
9f5fae2f | 744 | fs_info->running_transaction = NULL; |
15ee9bc7 | 745 | fs_info->last_trans_committed = 0; |
9f5fae2f CM |
746 | fs_info->tree_root = tree_root; |
747 | fs_info->extent_root = extent_root; | |
0b86a832 CM |
748 | fs_info->chunk_root = chunk_root; |
749 | fs_info->dev_root = dev_root; | |
8a4b83cc | 750 | fs_info->fs_devices = fs_devices; |
0b86a832 | 751 | INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); |
6324fbf3 | 752 | INIT_LIST_HEAD(&fs_info->space_info); |
0b86a832 | 753 | btrfs_mapping_init(&fs_info->mapping_tree); |
e20d96d6 | 754 | fs_info->sb = sb; |
e2008b61 | 755 | fs_info->throttles = 0; |
b6cda9bc | 756 | fs_info->mount_opt = 0; |
c59f8951 | 757 | fs_info->max_extent = (u64)-1; |
6f568d35 | 758 | fs_info->max_inline = 8192 * 1024; |
1832a6d5 | 759 | fs_info->delalloc_bytes = 0; |
04160088 | 760 | setup_bdi(fs_info, &fs_info->bdi); |
d98237b3 CM |
761 | fs_info->btree_inode = new_inode(sb); |
762 | fs_info->btree_inode->i_ino = 1; | |
2c90e5d6 | 763 | fs_info->btree_inode->i_nlink = 1; |
d98237b3 CM |
764 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
765 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | |
04160088 CM |
766 | fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi; |
767 | ||
d1310b2e | 768 | extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, |
5f39d397 CM |
769 | fs_info->btree_inode->i_mapping, |
770 | GFP_NOFS); | |
d1310b2e CM |
771 | extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree, |
772 | GFP_NOFS); | |
773 | ||
774 | BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; | |
0da5468f | 775 | |
d1310b2e | 776 | extent_io_tree_init(&fs_info->free_space_cache, |
f510cfec | 777 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 778 | extent_io_tree_init(&fs_info->block_group_cache, |
96b5179d | 779 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 780 | extent_io_tree_init(&fs_info->pinned_extents, |
1a5bc167 | 781 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 782 | extent_io_tree_init(&fs_info->pending_del, |
1a5bc167 | 783 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
d1310b2e | 784 | extent_io_tree_init(&fs_info->extent_ins, |
1a5bc167 | 785 | fs_info->btree_inode->i_mapping, GFP_NOFS); |
e66f709b | 786 | fs_info->do_barriers = 1; |
facda1e7 | 787 | fs_info->closing = 0; |
324ae4df | 788 | fs_info->total_pinned = 0; |
e18e4809 | 789 | fs_info->last_alloc = 0; |
4529ba49 | 790 | fs_info->last_data_alloc = 0; |
593060d7 CM |
791 | fs_info->extra_alloc_bits = 0; |
792 | fs_info->extra_data_alloc_bits = 0; | |
e18e4809 | 793 | |
6da6abae CM |
794 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
795 | INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info); | |
796 | #else | |
08607c1b | 797 | INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner); |
6da6abae | 798 | #endif |
0f7d52f4 CM |
799 | BTRFS_I(fs_info->btree_inode)->root = tree_root; |
800 | memset(&BTRFS_I(fs_info->btree_inode)->location, 0, | |
801 | sizeof(struct btrfs_key)); | |
22b0ebda | 802 | insert_inode_hash(fs_info->btree_inode); |
d98237b3 | 803 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
39279cc3 | 804 | |
79154b1b | 805 | mutex_init(&fs_info->trans_mutex); |
d561c025 | 806 | mutex_init(&fs_info->fs_mutex); |
3768f368 | 807 | |
19c00ddc CM |
808 | #if 0 |
809 | ret = add_hasher(fs_info, "crc32c"); | |
810 | if (ret) { | |
811 | printk("btrfs: failed hash setup, modprobe cryptomgr?\n"); | |
812 | err = -ENOMEM; | |
813 | goto fail_iput; | |
814 | } | |
815 | #endif | |
0b86a832 | 816 | __setup_root(4096, 4096, 4096, 4096, tree_root, |
2c90e5d6 | 817 | fs_info, BTRFS_ROOT_TREE_OBJECTID); |
7eccb903 | 818 | |
2c90e5d6 | 819 | fs_info->sb_buffer = read_tree_block(tree_root, |
db94535d | 820 | BTRFS_SUPER_INFO_OFFSET, |
0b86a832 | 821 | 4096); |
d98237b3 | 822 | |
0f7d52f4 | 823 | if (!fs_info->sb_buffer) |
39279cc3 | 824 | goto fail_iput; |
39279cc3 | 825 | |
5f39d397 CM |
826 | read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0, |
827 | sizeof(fs_info->super_copy)); | |
828 | ||
829 | read_extent_buffer(fs_info->sb_buffer, fs_info->fsid, | |
830 | (unsigned long)btrfs_super_fsid(fs_info->sb_buffer), | |
831 | BTRFS_FSID_SIZE); | |
0b86a832 | 832 | |
5f39d397 | 833 | disk_super = &fs_info->super_copy; |
0f7d52f4 | 834 | if (!btrfs_super_root(disk_super)) |
39279cc3 | 835 | goto fail_sb_buffer; |
0f7d52f4 | 836 | |
8a4b83cc CM |
837 | if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) { |
838 | printk("Btrfs: wanted %llu devices, but found %llu\n", | |
839 | (unsigned long long)btrfs_super_num_devices(disk_super), | |
840 | (unsigned long long)fs_devices->num_devices); | |
841 | goto fail_sb_buffer; | |
842 | } | |
db94535d CM |
843 | nodesize = btrfs_super_nodesize(disk_super); |
844 | leafsize = btrfs_super_leafsize(disk_super); | |
845 | sectorsize = btrfs_super_sectorsize(disk_super); | |
87ee04eb | 846 | stripesize = btrfs_super_stripesize(disk_super); |
db94535d CM |
847 | tree_root->nodesize = nodesize; |
848 | tree_root->leafsize = leafsize; | |
849 | tree_root->sectorsize = sectorsize; | |
87ee04eb | 850 | tree_root->stripesize = stripesize; |
ff79f819 | 851 | sb_set_blocksize(sb, sectorsize); |
db94535d | 852 | |
8352d8a4 | 853 | i_size_write(fs_info->btree_inode, |
db94535d | 854 | btrfs_super_total_bytes(disk_super)); |
8352d8a4 | 855 | |
39279cc3 CM |
856 | if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, |
857 | sizeof(disk_super->magic))) { | |
858 | printk("btrfs: valid FS not found on %s\n", sb->s_id); | |
859 | goto fail_sb_buffer; | |
860 | } | |
19c00ddc | 861 | |
0b86a832 | 862 | mutex_lock(&fs_info->fs_mutex); |
0d81ba5d | 863 | |
0b86a832 CM |
864 | ret = btrfs_read_sys_array(tree_root); |
865 | BUG_ON(ret); | |
866 | ||
867 | blocksize = btrfs_level_size(tree_root, | |
868 | btrfs_super_chunk_root_level(disk_super)); | |
869 | ||
870 | __setup_root(nodesize, leafsize, sectorsize, stripesize, | |
871 | chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); | |
872 | ||
873 | chunk_root->node = read_tree_block(chunk_root, | |
874 | btrfs_super_chunk_root(disk_super), | |
875 | blocksize); | |
876 | BUG_ON(!chunk_root->node); | |
877 | ||
878 | ret = btrfs_read_chunk_tree(chunk_root); | |
879 | BUG_ON(ret); | |
880 | ||
db94535d CM |
881 | blocksize = btrfs_level_size(tree_root, |
882 | btrfs_super_root_level(disk_super)); | |
19c00ddc | 883 | |
0b86a832 | 884 | |
e20d96d6 | 885 | tree_root->node = read_tree_block(tree_root, |
db94535d CM |
886 | btrfs_super_root(disk_super), |
887 | blocksize); | |
39279cc3 CM |
888 | if (!tree_root->node) |
889 | goto fail_sb_buffer; | |
3768f368 | 890 | |
db94535d CM |
891 | |
892 | ret = find_and_setup_root(tree_root, fs_info, | |
e20d96d6 | 893 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
0b86a832 | 894 | if (ret) |
39279cc3 | 895 | goto fail_tree_root; |
0b86a832 CM |
896 | extent_root->track_dirty = 1; |
897 | ||
898 | ret = find_and_setup_root(tree_root, fs_info, | |
899 | BTRFS_DEV_TREE_OBJECTID, dev_root); | |
900 | dev_root->track_dirty = 1; | |
901 | ||
902 | if (ret) | |
903 | goto fail_extent_root; | |
3768f368 | 904 | |
9078a3e1 CM |
905 | btrfs_read_block_groups(extent_root); |
906 | ||
0f7d52f4 | 907 | fs_info->generation = btrfs_super_generation(disk_super) + 1; |
5be6f7f1 | 908 | mutex_unlock(&fs_info->fs_mutex); |
0f7d52f4 | 909 | return tree_root; |
39279cc3 | 910 | |
0b86a832 CM |
911 | fail_extent_root: |
912 | free_extent_buffer(extent_root->node); | |
39279cc3 | 913 | fail_tree_root: |
0b86a832 | 914 | mutex_unlock(&fs_info->fs_mutex); |
5f39d397 | 915 | free_extent_buffer(tree_root->node); |
39279cc3 | 916 | fail_sb_buffer: |
5f39d397 | 917 | free_extent_buffer(fs_info->sb_buffer); |
2d2ae547 | 918 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); |
39279cc3 CM |
919 | fail_iput: |
920 | iput(fs_info->btree_inode); | |
921 | fail: | |
8a4b83cc | 922 | close_all_devices(fs_info); |
39279cc3 CM |
923 | kfree(extent_root); |
924 | kfree(tree_root); | |
2d2ae547 | 925 | bdi_destroy(&fs_info->bdi); |
39279cc3 CM |
926 | kfree(fs_info); |
927 | return ERR_PTR(err); | |
eb60ceac CM |
928 | } |
929 | ||
e089f05c | 930 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
79154b1b | 931 | *root) |
eb60ceac | 932 | { |
e66f709b | 933 | int ret; |
5f39d397 CM |
934 | struct extent_buffer *super = root->fs_info->sb_buffer; |
935 | struct inode *btree_inode = root->fs_info->btree_inode; | |
21ad10cf | 936 | struct super_block *sb = root->fs_info->sb; |
5f39d397 | 937 | |
21ad10cf CM |
938 | if (!btrfs_test_opt(root, NOBARRIER)) |
939 | blkdev_issue_flush(sb->s_bdev, NULL); | |
d1310b2e | 940 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super); |
5f39d397 CM |
941 | ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping, |
942 | super->start, super->len); | |
21ad10cf CM |
943 | if (!btrfs_test_opt(root, NOBARRIER)) |
944 | blkdev_issue_flush(sb->s_bdev, NULL); | |
5f39d397 | 945 | return ret; |
cfaa7295 CM |
946 | } |
947 | ||
5eda7b5e | 948 | int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) |
2619ba1f CM |
949 | { |
950 | radix_tree_delete(&fs_info->fs_roots_radix, | |
951 | (unsigned long)root->root_key.objectid); | |
b99aa6cb CM |
952 | if (root->in_sysfs) |
953 | btrfs_sysfs_del_root(root); | |
2619ba1f CM |
954 | if (root->inode) |
955 | iput(root->inode); | |
956 | if (root->node) | |
5f39d397 | 957 | free_extent_buffer(root->node); |
2619ba1f | 958 | if (root->commit_root) |
5f39d397 | 959 | free_extent_buffer(root->commit_root); |
58176a96 JB |
960 | if (root->name) |
961 | kfree(root->name); | |
2619ba1f CM |
962 | kfree(root); |
963 | return 0; | |
964 | } | |
965 | ||
35b7e476 | 966 | static int del_fs_roots(struct btrfs_fs_info *fs_info) |
0f7d52f4 CM |
967 | { |
968 | int ret; | |
969 | struct btrfs_root *gang[8]; | |
970 | int i; | |
971 | ||
972 | while(1) { | |
973 | ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, | |
974 | (void **)gang, 0, | |
975 | ARRAY_SIZE(gang)); | |
976 | if (!ret) | |
977 | break; | |
2619ba1f | 978 | for (i = 0; i < ret; i++) |
5eda7b5e | 979 | btrfs_free_fs_root(fs_info, gang[i]); |
0f7d52f4 CM |
980 | } |
981 | return 0; | |
982 | } | |
b4100d64 | 983 | |
e20d96d6 | 984 | int close_ctree(struct btrfs_root *root) |
cfaa7295 | 985 | { |
3768f368 | 986 | int ret; |
e089f05c | 987 | struct btrfs_trans_handle *trans; |
0f7d52f4 | 988 | struct btrfs_fs_info *fs_info = root->fs_info; |
e089f05c | 989 | |
facda1e7 | 990 | fs_info->closing = 1; |
08607c1b | 991 | btrfs_transaction_flush_work(root); |
0f7d52f4 | 992 | mutex_lock(&fs_info->fs_mutex); |
6702ed49 | 993 | btrfs_defrag_dirty_roots(root->fs_info); |
79154b1b | 994 | trans = btrfs_start_transaction(root, 1); |
54aa1f4d | 995 | ret = btrfs_commit_transaction(trans, root); |
79154b1b CM |
996 | /* run commit again to drop the original snapshot */ |
997 | trans = btrfs_start_transaction(root, 1); | |
998 | btrfs_commit_transaction(trans, root); | |
999 | ret = btrfs_write_and_wait_transaction(NULL, root); | |
3768f368 | 1000 | BUG_ON(ret); |
79154b1b | 1001 | write_ctree_super(NULL, root); |
0f7d52f4 CM |
1002 | mutex_unlock(&fs_info->fs_mutex); |
1003 | ||
b0c68f8b CM |
1004 | if (fs_info->delalloc_bytes) { |
1005 | printk("btrfs: at unmount delalloc count %Lu\n", | |
1006 | fs_info->delalloc_bytes); | |
1007 | } | |
0f7d52f4 | 1008 | if (fs_info->extent_root->node) |
5f39d397 | 1009 | free_extent_buffer(fs_info->extent_root->node); |
f510cfec | 1010 | |
0f7d52f4 | 1011 | if (fs_info->tree_root->node) |
5f39d397 | 1012 | free_extent_buffer(fs_info->tree_root->node); |
f510cfec | 1013 | |
0b86a832 CM |
1014 | if (root->fs_info->chunk_root->node); |
1015 | free_extent_buffer(root->fs_info->chunk_root->node); | |
1016 | ||
1017 | if (root->fs_info->dev_root->node); | |
1018 | free_extent_buffer(root->fs_info->dev_root->node); | |
1019 | ||
5f39d397 | 1020 | free_extent_buffer(fs_info->sb_buffer); |
7eccb903 | 1021 | |
9078a3e1 | 1022 | btrfs_free_block_groups(root->fs_info); |
0f7d52f4 | 1023 | del_fs_roots(fs_info); |
d10c5f31 CM |
1024 | |
1025 | filemap_write_and_wait(fs_info->btree_inode->i_mapping); | |
1026 | ||
d1310b2e CM |
1027 | extent_io_tree_empty_lru(&fs_info->free_space_cache); |
1028 | extent_io_tree_empty_lru(&fs_info->block_group_cache); | |
1029 | extent_io_tree_empty_lru(&fs_info->pinned_extents); | |
1030 | extent_io_tree_empty_lru(&fs_info->pending_del); | |
1031 | extent_io_tree_empty_lru(&fs_info->extent_ins); | |
1032 | extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); | |
d10c5f31 | 1033 | |
db94535d | 1034 | truncate_inode_pages(fs_info->btree_inode->i_mapping, 0); |
d10c5f31 | 1035 | |
db94535d | 1036 | iput(fs_info->btree_inode); |
19c00ddc CM |
1037 | #if 0 |
1038 | while(!list_empty(&fs_info->hashers)) { | |
1039 | struct btrfs_hasher *hasher; | |
1040 | hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher, | |
1041 | hashers); | |
1042 | list_del(&hasher->hashers); | |
1043 | crypto_free_hash(&fs_info->hash_tfm); | |
1044 | kfree(hasher); | |
1045 | } | |
1046 | #endif | |
0b86a832 CM |
1047 | close_all_devices(fs_info); |
1048 | btrfs_mapping_tree_free(&fs_info->mapping_tree); | |
04160088 | 1049 | bdi_destroy(&fs_info->bdi); |
0b86a832 | 1050 | |
0f7d52f4 | 1051 | kfree(fs_info->extent_root); |
0f7d52f4 | 1052 | kfree(fs_info->tree_root); |
0b86a832 CM |
1053 | kfree(fs_info->chunk_root); |
1054 | kfree(fs_info->dev_root); | |
eb60ceac CM |
1055 | return 0; |
1056 | } | |
1057 | ||
5f39d397 CM |
1058 | int btrfs_buffer_uptodate(struct extent_buffer *buf) |
1059 | { | |
810191ff | 1060 | struct inode *btree_inode = buf->first_page->mapping->host; |
d1310b2e | 1061 | return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); |
5f39d397 CM |
1062 | } |
1063 | ||
1064 | int btrfs_set_buffer_uptodate(struct extent_buffer *buf) | |
ccd467d6 | 1065 | { |
810191ff | 1066 | struct inode *btree_inode = buf->first_page->mapping->host; |
d1310b2e | 1067 | return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, |
5f39d397 CM |
1068 | buf); |
1069 | } | |
6702ed49 | 1070 | |
5f39d397 CM |
1071 | void btrfs_mark_buffer_dirty(struct extent_buffer *buf) |
1072 | { | |
810191ff | 1073 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
5f39d397 CM |
1074 | u64 transid = btrfs_header_generation(buf); |
1075 | struct inode *btree_inode = root->fs_info->btree_inode; | |
6702ed49 | 1076 | |
ccd467d6 CM |
1077 | if (transid != root->fs_info->generation) { |
1078 | printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n", | |
db94535d | 1079 | (unsigned long long)buf->start, |
ccd467d6 CM |
1080 | transid, root->fs_info->generation); |
1081 | WARN_ON(1); | |
1082 | } | |
d1310b2e | 1083 | set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf); |
eb60ceac CM |
1084 | } |
1085 | ||
e2008b61 CM |
1086 | void btrfs_throttle(struct btrfs_root *root) |
1087 | { | |
55c69072 CM |
1088 | struct backing_dev_info *bdi; |
1089 | ||
1090 | bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info; | |
04005cc7 CM |
1091 | if (root->fs_info->throttles && bdi_write_congested(bdi)) { |
1092 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) | |
55c69072 | 1093 | congestion_wait(WRITE, HZ/20); |
04005cc7 CM |
1094 | #else |
1095 | blk_congestion_wait(WRITE, HZ/20); | |
1096 | #endif | |
1097 | } | |
e2008b61 CM |
1098 | } |
1099 | ||
d3c2fdcf | 1100 | void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr) |
35b7e476 | 1101 | { |
d3c2fdcf | 1102 | balance_dirty_pages_ratelimited_nr( |
d7fc640e | 1103 | root->fs_info->btree_inode->i_mapping, 1); |
35b7e476 | 1104 | } |
6b80053d CM |
1105 | |
1106 | void btrfs_set_buffer_defrag(struct extent_buffer *buf) | |
1107 | { | |
810191ff | 1108 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1109 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1110 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
6b80053d CM |
1111 | buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS); |
1112 | } | |
1113 | ||
1114 | void btrfs_set_buffer_defrag_done(struct extent_buffer *buf) | |
1115 | { | |
810191ff | 1116 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1117 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1118 | set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start, |
6b80053d CM |
1119 | buf->start + buf->len - 1, EXTENT_DEFRAG_DONE, |
1120 | GFP_NOFS); | |
1121 | } | |
1122 | ||
1123 | int btrfs_buffer_defrag(struct extent_buffer *buf) | |
1124 | { | |
810191ff | 1125 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1126 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1127 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1128 | buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0); |
1129 | } | |
1130 | ||
1131 | int btrfs_buffer_defrag_done(struct extent_buffer *buf) | |
1132 | { | |
810191ff | 1133 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1134 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1135 | return test_range_bit(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1136 | buf->start, buf->start + buf->len - 1, |
1137 | EXTENT_DEFRAG_DONE, 0); | |
1138 | } | |
1139 | ||
1140 | int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf) | |
1141 | { | |
810191ff | 1142 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1143 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1144 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1145 | buf->start, buf->start + buf->len - 1, |
1146 | EXTENT_DEFRAG_DONE, GFP_NOFS); | |
1147 | } | |
1148 | ||
1149 | int btrfs_clear_buffer_defrag(struct extent_buffer *buf) | |
1150 | { | |
810191ff | 1151 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1152 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1153 | return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree, |
6b80053d CM |
1154 | buf->start, buf->start + buf->len - 1, |
1155 | EXTENT_DEFRAG, GFP_NOFS); | |
1156 | } | |
1157 | ||
1158 | int btrfs_read_buffer(struct extent_buffer *buf) | |
1159 | { | |
810191ff | 1160 | struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root; |
6b80053d | 1161 | struct inode *btree_inode = root->fs_info->btree_inode; |
d1310b2e | 1162 | return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, |
a86c12c7 | 1163 | buf, 0, 1, btree_get_extent); |
6b80053d | 1164 | } |
0da5468f | 1165 | |
d1310b2e | 1166 | static struct extent_io_ops btree_extent_io_ops = { |
0da5468f | 1167 | .writepage_io_hook = btree_writepage_io_hook, |
0b86a832 | 1168 | .submit_bio_hook = btree_submit_bio_hook, |
239b14b3 CM |
1169 | /* note we're sharing with inode.c for the merge bio hook */ |
1170 | .merge_bio_hook = btrfs_merge_bio_hook, | |
0da5468f | 1171 | }; |