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