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