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