Btrfs: Always use 64bit inode number
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
87cbda5c 21#include <linux/scatterlist.h>
22b0ebda 22#include <linux/swap.h>
0f7d52f4 23#include <linux/radix-tree.h>
35b7e476 24#include <linux/writeback.h>
d397712b 25#include <linux/buffer_head.h>
ce9adaa5 26#include <linux/workqueue.h>
a74a4b97 27#include <linux/kthread.h>
4b4e25f2 28#include <linux/freezer.h>
163e783e 29#include <linux/crc32c.h>
5a0e3ad6 30#include <linux/slab.h>
784b4e29 31#include <linux/migrate.h>
7e75bf3f 32#include <asm/unaligned.h>
4b4e25f2 33#include "compat.h"
eb60ceac
CM
34#include "ctree.h"
35#include "disk-io.h"
e089f05c 36#include "transaction.h"
0f7d52f4 37#include "btrfs_inode.h"
0b86a832 38#include "volumes.h"
db94535d 39#include "print-tree.h"
8b712842 40#include "async-thread.h"
925baedd 41#include "locking.h"
e02119d5 42#include "tree-log.h"
fa9c0d79 43#include "free-space-cache.h"
581bb050 44#include "inode-map.h"
eb60ceac 45
d1310b2e 46static struct extent_io_ops btree_extent_io_ops;
8b712842 47static void end_workqueue_fn(struct btrfs_work *work);
4df27c4d 48static void free_fs_root(struct btrfs_root *root);
acce952b 49static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
50 int read_only);
51static int btrfs_destroy_ordered_operations(struct btrfs_root *root);
52static int btrfs_destroy_ordered_extents(struct btrfs_root *root);
53static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
54 struct btrfs_root *root);
55static int btrfs_destroy_pending_snapshots(struct btrfs_transaction *t);
56static int btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
57static int btrfs_destroy_marked_extents(struct btrfs_root *root,
58 struct extent_io_tree *dirty_pages,
59 int mark);
60static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
61 struct extent_io_tree *pinned_extents);
62static int btrfs_cleanup_transaction(struct btrfs_root *root);
ce9adaa5 63
d352ac68
CM
64/*
65 * end_io_wq structs are used to do processing in task context when an IO is
66 * complete. This is used during reads to verify checksums, and it is used
67 * by writes to insert metadata for new file extents after IO is complete.
68 */
ce9adaa5
CM
69struct end_io_wq {
70 struct bio *bio;
71 bio_end_io_t *end_io;
72 void *private;
73 struct btrfs_fs_info *info;
74 int error;
22c59948 75 int metadata;
ce9adaa5 76 struct list_head list;
8b712842 77 struct btrfs_work work;
ce9adaa5 78};
0da5468f 79
d352ac68
CM
80/*
81 * async submit bios are used to offload expensive checksumming
82 * onto the worker threads. They checksum file and metadata bios
83 * just before they are sent down the IO stack.
84 */
44b8bd7e
CM
85struct async_submit_bio {
86 struct inode *inode;
87 struct bio *bio;
88 struct list_head list;
4a69a410
CM
89 extent_submit_bio_hook_t *submit_bio_start;
90 extent_submit_bio_hook_t *submit_bio_done;
44b8bd7e
CM
91 int rw;
92 int mirror_num;
c8b97818 93 unsigned long bio_flags;
eaf25d93
CM
94 /*
95 * bio_offset is optional, can be used if the pages in the bio
96 * can't tell us where in the file the bio should go
97 */
98 u64 bio_offset;
8b712842 99 struct btrfs_work work;
44b8bd7e
CM
100};
101
4008c04a
CM
102/* These are used to set the lockdep class on the extent buffer locks.
103 * The class is set by the readpage_end_io_hook after the buffer has
104 * passed csum validation but before the pages are unlocked.
105 *
106 * The lockdep class is also set by btrfs_init_new_buffer on freshly
107 * allocated blocks.
108 *
109 * The class is based on the level in the tree block, which allows lockdep
110 * to know that lower nodes nest inside the locks of higher nodes.
111 *
112 * We also add a check to make sure the highest level of the tree is
113 * the same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this
114 * code needs update as well.
115 */
116#ifdef CONFIG_DEBUG_LOCK_ALLOC
117# if BTRFS_MAX_LEVEL != 8
118# error
119# endif
120static struct lock_class_key btrfs_eb_class[BTRFS_MAX_LEVEL + 1];
121static const char *btrfs_eb_name[BTRFS_MAX_LEVEL + 1] = {
122 /* leaf */
123 "btrfs-extent-00",
124 "btrfs-extent-01",
125 "btrfs-extent-02",
126 "btrfs-extent-03",
127 "btrfs-extent-04",
128 "btrfs-extent-05",
129 "btrfs-extent-06",
130 "btrfs-extent-07",
131 /* highest possible level */
132 "btrfs-extent-08",
133};
134#endif
135
d352ac68
CM
136/*
137 * extents on the btree inode are pretty simple, there's one extent
138 * that covers the entire device
139 */
b2950863
CH
140static struct extent_map *btree_get_extent(struct inode *inode,
141 struct page *page, size_t page_offset, u64 start, u64 len,
142 int create)
7eccb903 143{
5f39d397
CM
144 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
145 struct extent_map *em;
146 int ret;
147
890871be 148 read_lock(&em_tree->lock);
d1310b2e 149 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
150 if (em) {
151 em->bdev =
152 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
890871be 153 read_unlock(&em_tree->lock);
5f39d397 154 goto out;
a061fc8d 155 }
890871be 156 read_unlock(&em_tree->lock);
7b13b7b1 157
5f39d397
CM
158 em = alloc_extent_map(GFP_NOFS);
159 if (!em) {
160 em = ERR_PTR(-ENOMEM);
161 goto out;
162 }
163 em->start = 0;
0afbaf8c 164 em->len = (u64)-1;
c8b97818 165 em->block_len = (u64)-1;
5f39d397 166 em->block_start = 0;
a061fc8d 167 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
d1310b2e 168
890871be 169 write_lock(&em_tree->lock);
5f39d397
CM
170 ret = add_extent_mapping(em_tree, em);
171 if (ret == -EEXIST) {
0afbaf8c
CM
172 u64 failed_start = em->start;
173 u64 failed_len = em->len;
174
5f39d397 175 free_extent_map(em);
7b13b7b1 176 em = lookup_extent_mapping(em_tree, start, len);
0afbaf8c 177 if (em) {
7b13b7b1 178 ret = 0;
0afbaf8c
CM
179 } else {
180 em = lookup_extent_mapping(em_tree, failed_start,
181 failed_len);
7b13b7b1 182 ret = -EIO;
0afbaf8c 183 }
5f39d397 184 } else if (ret) {
7b13b7b1
CM
185 free_extent_map(em);
186 em = NULL;
5f39d397 187 }
890871be 188 write_unlock(&em_tree->lock);
7b13b7b1
CM
189
190 if (ret)
191 em = ERR_PTR(ret);
5f39d397
CM
192out:
193 return em;
7eccb903
CM
194}
195
19c00ddc
CM
196u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
197{
163e783e 198 return crc32c(seed, data, len);
19c00ddc
CM
199}
200
201void btrfs_csum_final(u32 crc, char *result)
202{
7e75bf3f 203 put_unaligned_le32(~crc, result);
19c00ddc
CM
204}
205
d352ac68
CM
206/*
207 * compute the csum for a btree block, and either verify it or write it
208 * into the csum field of the block.
209 */
19c00ddc
CM
210static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
211 int verify)
212{
607d432d
JB
213 u16 csum_size =
214 btrfs_super_csum_size(&root->fs_info->super_copy);
215 char *result = NULL;
19c00ddc
CM
216 unsigned long len;
217 unsigned long cur_len;
218 unsigned long offset = BTRFS_CSUM_SIZE;
219 char *map_token = NULL;
220 char *kaddr;
221 unsigned long map_start;
222 unsigned long map_len;
223 int err;
224 u32 crc = ~(u32)0;
607d432d 225 unsigned long inline_result;
19c00ddc
CM
226
227 len = buf->len - offset;
d397712b 228 while (len > 0) {
19c00ddc
CM
229 err = map_private_extent_buffer(buf, offset, 32,
230 &map_token, &kaddr,
231 &map_start, &map_len, KM_USER0);
d397712b 232 if (err)
19c00ddc 233 return 1;
19c00ddc
CM
234 cur_len = min(len, map_len - (offset - map_start));
235 crc = btrfs_csum_data(root, kaddr + offset - map_start,
236 crc, cur_len);
237 len -= cur_len;
238 offset += cur_len;
239 unmap_extent_buffer(buf, map_token, KM_USER0);
240 }
607d432d
JB
241 if (csum_size > sizeof(inline_result)) {
242 result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
243 if (!result)
244 return 1;
245 } else {
246 result = (char *)&inline_result;
247 }
248
19c00ddc
CM
249 btrfs_csum_final(crc, result);
250
251 if (verify) {
607d432d 252 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
e4204ded
CM
253 u32 val;
254 u32 found = 0;
607d432d 255 memcpy(&found, result, csum_size);
e4204ded 256
607d432d 257 read_extent_buffer(buf, &val, 0, csum_size);
193f284d
CM
258 if (printk_ratelimit()) {
259 printk(KERN_INFO "btrfs: %s checksum verify "
260 "failed on %llu wanted %X found %X "
261 "level %d\n",
262 root->fs_info->sb->s_id,
263 (unsigned long long)buf->start, val, found,
264 btrfs_header_level(buf));
265 }
607d432d
JB
266 if (result != (char *)&inline_result)
267 kfree(result);
19c00ddc
CM
268 return 1;
269 }
270 } else {
607d432d 271 write_extent_buffer(buf, result, 0, csum_size);
19c00ddc 272 }
607d432d
JB
273 if (result != (char *)&inline_result)
274 kfree(result);
19c00ddc
CM
275 return 0;
276}
277
d352ac68
CM
278/*
279 * we can't consider a given block up to date unless the transid of the
280 * block matches the transid in the parent node's pointer. This is how we
281 * detect blocks that either didn't get written at all or got written
282 * in the wrong place.
283 */
1259ab75
CM
284static int verify_parent_transid(struct extent_io_tree *io_tree,
285 struct extent_buffer *eb, u64 parent_transid)
286{
2ac55d41 287 struct extent_state *cached_state = NULL;
1259ab75
CM
288 int ret;
289
290 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
291 return 0;
292
2ac55d41
JB
293 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
294 0, &cached_state, GFP_NOFS);
295 if (extent_buffer_uptodate(io_tree, eb, cached_state) &&
1259ab75
CM
296 btrfs_header_generation(eb) == parent_transid) {
297 ret = 0;
298 goto out;
299 }
193f284d
CM
300 if (printk_ratelimit()) {
301 printk("parent transid verify failed on %llu wanted %llu "
302 "found %llu\n",
303 (unsigned long long)eb->start,
304 (unsigned long long)parent_transid,
305 (unsigned long long)btrfs_header_generation(eb));
306 }
1259ab75 307 ret = 1;
2ac55d41 308 clear_extent_buffer_uptodate(io_tree, eb, &cached_state);
33958dc6 309out:
2ac55d41
JB
310 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
311 &cached_state, GFP_NOFS);
1259ab75 312 return ret;
1259ab75
CM
313}
314
d352ac68
CM
315/*
316 * helper to read a given tree block, doing retries as required when
317 * the checksums don't match and we have alternate mirrors to try.
318 */
f188591e
CM
319static int btree_read_extent_buffer_pages(struct btrfs_root *root,
320 struct extent_buffer *eb,
ca7a79ad 321 u64 start, u64 parent_transid)
f188591e
CM
322{
323 struct extent_io_tree *io_tree;
324 int ret;
325 int num_copies = 0;
326 int mirror_num = 0;
327
a826d6dc 328 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
f188591e
CM
329 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree;
330 while (1) {
331 ret = read_extent_buffer_pages(io_tree, eb, start, 1,
332 btree_get_extent, mirror_num);
1259ab75
CM
333 if (!ret &&
334 !verify_parent_transid(io_tree, eb, parent_transid))
f188591e 335 return ret;
d397712b 336
a826d6dc
JB
337 /*
338 * This buffer's crc is fine, but its contents are corrupted, so
339 * there is no reason to read the other copies, they won't be
340 * any less wrong.
341 */
342 if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags))
343 return ret;
344
f188591e
CM
345 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
346 eb->start, eb->len);
4235298e 347 if (num_copies == 1)
f188591e 348 return ret;
4235298e 349
f188591e 350 mirror_num++;
4235298e 351 if (mirror_num > num_copies)
f188591e 352 return ret;
f188591e 353 }
f188591e
CM
354 return -EIO;
355}
19c00ddc 356
d352ac68 357/*
d397712b
CM
358 * checksum a dirty tree block before IO. This has extra checks to make sure
359 * we only fill in the checksum field in the first page of a multi-page block
d352ac68 360 */
d397712b 361
b2950863 362static int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
19c00ddc 363{
d1310b2e 364 struct extent_io_tree *tree;
35ebb934 365 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
19c00ddc 366 u64 found_start;
19c00ddc
CM
367 unsigned long len;
368 struct extent_buffer *eb;
f188591e
CM
369 int ret;
370
d1310b2e 371 tree = &BTRFS_I(page->mapping->host)->io_tree;
19c00ddc 372
eb14ab8e
CM
373 if (page->private == EXTENT_PAGE_PRIVATE) {
374 WARN_ON(1);
19c00ddc 375 goto out;
eb14ab8e
CM
376 }
377 if (!page->private) {
378 WARN_ON(1);
19c00ddc 379 goto out;
eb14ab8e 380 }
19c00ddc 381 len = page->private >> 2;
d397712b
CM
382 WARN_ON(len == 0);
383
19c00ddc 384 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
91ca338d
TI
385 if (eb == NULL) {
386 WARN_ON(1);
387 goto out;
388 }
ca7a79ad
CM
389 ret = btree_read_extent_buffer_pages(root, eb, start + PAGE_CACHE_SIZE,
390 btrfs_header_generation(eb));
f188591e 391 BUG_ON(ret);
784b4e29
CM
392 WARN_ON(!btrfs_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN));
393
19c00ddc
CM
394 found_start = btrfs_header_bytenr(eb);
395 if (found_start != start) {
55c69072
CM
396 WARN_ON(1);
397 goto err;
398 }
399 if (eb->first_page != page) {
55c69072
CM
400 WARN_ON(1);
401 goto err;
402 }
403 if (!PageUptodate(page)) {
55c69072
CM
404 WARN_ON(1);
405 goto err;
19c00ddc 406 }
19c00ddc 407 csum_tree_block(root, eb, 0);
55c69072 408err:
19c00ddc
CM
409 free_extent_buffer(eb);
410out:
411 return 0;
412}
413
2b82032c
YZ
414static int check_tree_block_fsid(struct btrfs_root *root,
415 struct extent_buffer *eb)
416{
417 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
418 u8 fsid[BTRFS_UUID_SIZE];
419 int ret = 1;
420
421 read_extent_buffer(eb, fsid, (unsigned long)btrfs_header_fsid(eb),
422 BTRFS_FSID_SIZE);
423 while (fs_devices) {
424 if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
425 ret = 0;
426 break;
427 }
428 fs_devices = fs_devices->seed;
429 }
430 return ret;
431}
432
a826d6dc
JB
433#define CORRUPT(reason, eb, root, slot) \
434 printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \
435 "root=%llu, slot=%d\n", reason, \
436 (unsigned long long)btrfs_header_bytenr(eb), \
437 (unsigned long long)root->objectid, slot)
438
439static noinline int check_leaf(struct btrfs_root *root,
440 struct extent_buffer *leaf)
441{
442 struct btrfs_key key;
443 struct btrfs_key leaf_key;
444 u32 nritems = btrfs_header_nritems(leaf);
445 int slot;
446
447 if (nritems == 0)
448 return 0;
449
450 /* Check the 0 item */
451 if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) !=
452 BTRFS_LEAF_DATA_SIZE(root)) {
453 CORRUPT("invalid item offset size pair", leaf, root, 0);
454 return -EIO;
455 }
456
457 /*
458 * Check to make sure each items keys are in the correct order and their
459 * offsets make sense. We only have to loop through nritems-1 because
460 * we check the current slot against the next slot, which verifies the
461 * next slot's offset+size makes sense and that the current's slot
462 * offset is correct.
463 */
464 for (slot = 0; slot < nritems - 1; slot++) {
465 btrfs_item_key_to_cpu(leaf, &leaf_key, slot);
466 btrfs_item_key_to_cpu(leaf, &key, slot + 1);
467
468 /* Make sure the keys are in the right order */
469 if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) {
470 CORRUPT("bad key order", leaf, root, slot);
471 return -EIO;
472 }
473
474 /*
475 * Make sure the offset and ends are right, remember that the
476 * item data starts at the end of the leaf and grows towards the
477 * front.
478 */
479 if (btrfs_item_offset_nr(leaf, slot) !=
480 btrfs_item_end_nr(leaf, slot + 1)) {
481 CORRUPT("slot offset bad", leaf, root, slot);
482 return -EIO;
483 }
484
485 /*
486 * Check to make sure that we don't point outside of the leaf,
487 * just incase all the items are consistent to eachother, but
488 * all point outside of the leaf.
489 */
490 if (btrfs_item_end_nr(leaf, slot) >
491 BTRFS_LEAF_DATA_SIZE(root)) {
492 CORRUPT("slot end outside of leaf", leaf, root, slot);
493 return -EIO;
494 }
495 }
496
497 return 0;
498}
499
4008c04a
CM
500#ifdef CONFIG_DEBUG_LOCK_ALLOC
501void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level)
502{
503 lockdep_set_class_and_name(&eb->lock,
504 &btrfs_eb_class[level],
505 btrfs_eb_name[level]);
506}
507#endif
508
b2950863 509static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
ce9adaa5
CM
510 struct extent_state *state)
511{
512 struct extent_io_tree *tree;
513 u64 found_start;
514 int found_level;
515 unsigned long len;
516 struct extent_buffer *eb;
517 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
f188591e 518 int ret = 0;
ce9adaa5
CM
519
520 tree = &BTRFS_I(page->mapping->host)->io_tree;
521 if (page->private == EXTENT_PAGE_PRIVATE)
522 goto out;
523 if (!page->private)
524 goto out;
d397712b 525
ce9adaa5 526 len = page->private >> 2;
d397712b
CM
527 WARN_ON(len == 0);
528
ce9adaa5 529 eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
91ca338d
TI
530 if (eb == NULL) {
531 ret = -EIO;
532 goto out;
533 }
f188591e 534
ce9adaa5 535 found_start = btrfs_header_bytenr(eb);
23a07867 536 if (found_start != start) {
193f284d
CM
537 if (printk_ratelimit()) {
538 printk(KERN_INFO "btrfs bad tree block start "
539 "%llu %llu\n",
540 (unsigned long long)found_start,
541 (unsigned long long)eb->start);
542 }
f188591e 543 ret = -EIO;
ce9adaa5
CM
544 goto err;
545 }
546 if (eb->first_page != page) {
d397712b
CM
547 printk(KERN_INFO "btrfs bad first page %lu %lu\n",
548 eb->first_page->index, page->index);
ce9adaa5 549 WARN_ON(1);
f188591e 550 ret = -EIO;
ce9adaa5
CM
551 goto err;
552 }
2b82032c 553 if (check_tree_block_fsid(root, eb)) {
193f284d
CM
554 if (printk_ratelimit()) {
555 printk(KERN_INFO "btrfs bad fsid on block %llu\n",
556 (unsigned long long)eb->start);
557 }
1259ab75
CM
558 ret = -EIO;
559 goto err;
560 }
ce9adaa5
CM
561 found_level = btrfs_header_level(eb);
562
4008c04a
CM
563 btrfs_set_buffer_lockdep_class(eb, found_level);
564
ce9adaa5 565 ret = csum_tree_block(root, eb, 1);
a826d6dc 566 if (ret) {
f188591e 567 ret = -EIO;
a826d6dc
JB
568 goto err;
569 }
570
571 /*
572 * If this is a leaf block and it is corrupt, set the corrupt bit so
573 * that we don't try and read the other copies of this block, just
574 * return -EIO.
575 */
576 if (found_level == 0 && check_leaf(root, eb)) {
577 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
578 ret = -EIO;
579 }
ce9adaa5
CM
580
581 end = min_t(u64, eb->len, PAGE_CACHE_SIZE);
582 end = eb->start + end - 1;
ce9adaa5
CM
583err:
584 free_extent_buffer(eb);
585out:
f188591e 586 return ret;
ce9adaa5
CM
587}
588
ce9adaa5 589static void end_workqueue_bio(struct bio *bio, int err)
ce9adaa5
CM
590{
591 struct end_io_wq *end_io_wq = bio->bi_private;
592 struct btrfs_fs_info *fs_info;
ce9adaa5 593
ce9adaa5 594 fs_info = end_io_wq->info;
ce9adaa5 595 end_io_wq->error = err;
8b712842
CM
596 end_io_wq->work.func = end_workqueue_fn;
597 end_io_wq->work.flags = 0;
d20f7043 598
7b6d91da 599 if (bio->bi_rw & REQ_WRITE) {
0cb59c99 600 if (end_io_wq->metadata == 1)
cad321ad
CM
601 btrfs_queue_worker(&fs_info->endio_meta_write_workers,
602 &end_io_wq->work);
0cb59c99
JB
603 else if (end_io_wq->metadata == 2)
604 btrfs_queue_worker(&fs_info->endio_freespace_worker,
605 &end_io_wq->work);
cad321ad
CM
606 else
607 btrfs_queue_worker(&fs_info->endio_write_workers,
608 &end_io_wq->work);
d20f7043
CM
609 } else {
610 if (end_io_wq->metadata)
611 btrfs_queue_worker(&fs_info->endio_meta_workers,
612 &end_io_wq->work);
613 else
614 btrfs_queue_worker(&fs_info->endio_workers,
615 &end_io_wq->work);
616 }
ce9adaa5
CM
617}
618
0cb59c99
JB
619/*
620 * For the metadata arg you want
621 *
622 * 0 - if data
623 * 1 - if normal metadta
624 * 2 - if writing to the free space cache area
625 */
22c59948
CM
626int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
627 int metadata)
0b86a832 628{
ce9adaa5 629 struct end_io_wq *end_io_wq;
ce9adaa5
CM
630 end_io_wq = kmalloc(sizeof(*end_io_wq), GFP_NOFS);
631 if (!end_io_wq)
632 return -ENOMEM;
633
634 end_io_wq->private = bio->bi_private;
635 end_io_wq->end_io = bio->bi_end_io;
22c59948 636 end_io_wq->info = info;
ce9adaa5
CM
637 end_io_wq->error = 0;
638 end_io_wq->bio = bio;
22c59948 639 end_io_wq->metadata = metadata;
ce9adaa5
CM
640
641 bio->bi_private = end_io_wq;
642 bio->bi_end_io = end_workqueue_bio;
22c59948
CM
643 return 0;
644}
645
b64a2851 646unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
0986fe9e 647{
4854ddd0
CM
648 unsigned long limit = min_t(unsigned long,
649 info->workers.max_workers,
650 info->fs_devices->open_devices);
651 return 256 * limit;
652}
0986fe9e 653
4854ddd0
CM
654int btrfs_congested_async(struct btrfs_fs_info *info, int iodone)
655{
b64a2851
CM
656 return atomic_read(&info->nr_async_bios) >
657 btrfs_async_submit_limit(info);
0986fe9e
CM
658}
659
4a69a410
CM
660static void run_one_async_start(struct btrfs_work *work)
661{
4a69a410
CM
662 struct async_submit_bio *async;
663
664 async = container_of(work, struct async_submit_bio, work);
4a69a410 665 async->submit_bio_start(async->inode, async->rw, async->bio,
eaf25d93
CM
666 async->mirror_num, async->bio_flags,
667 async->bio_offset);
4a69a410
CM
668}
669
670static void run_one_async_done(struct btrfs_work *work)
8b712842
CM
671{
672 struct btrfs_fs_info *fs_info;
673 struct async_submit_bio *async;
4854ddd0 674 int limit;
8b712842
CM
675
676 async = container_of(work, struct async_submit_bio, work);
677 fs_info = BTRFS_I(async->inode)->root->fs_info;
4854ddd0 678
b64a2851 679 limit = btrfs_async_submit_limit(fs_info);
4854ddd0
CM
680 limit = limit * 2 / 3;
681
8b712842 682 atomic_dec(&fs_info->nr_async_submits);
0986fe9e 683
b64a2851
CM
684 if (atomic_read(&fs_info->nr_async_submits) < limit &&
685 waitqueue_active(&fs_info->async_submit_wait))
4854ddd0
CM
686 wake_up(&fs_info->async_submit_wait);
687
4a69a410 688 async->submit_bio_done(async->inode, async->rw, async->bio,
eaf25d93
CM
689 async->mirror_num, async->bio_flags,
690 async->bio_offset);
4a69a410
CM
691}
692
693static void run_one_async_free(struct btrfs_work *work)
694{
695 struct async_submit_bio *async;
696
697 async = container_of(work, struct async_submit_bio, work);
8b712842
CM
698 kfree(async);
699}
700
44b8bd7e
CM
701int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
702 int rw, struct bio *bio, int mirror_num,
c8b97818 703 unsigned long bio_flags,
eaf25d93 704 u64 bio_offset,
4a69a410
CM
705 extent_submit_bio_hook_t *submit_bio_start,
706 extent_submit_bio_hook_t *submit_bio_done)
44b8bd7e
CM
707{
708 struct async_submit_bio *async;
709
710 async = kmalloc(sizeof(*async), GFP_NOFS);
711 if (!async)
712 return -ENOMEM;
713
714 async->inode = inode;
715 async->rw = rw;
716 async->bio = bio;
717 async->mirror_num = mirror_num;
4a69a410
CM
718 async->submit_bio_start = submit_bio_start;
719 async->submit_bio_done = submit_bio_done;
720
721 async->work.func = run_one_async_start;
722 async->work.ordered_func = run_one_async_done;
723 async->work.ordered_free = run_one_async_free;
724
8b712842 725 async->work.flags = 0;
c8b97818 726 async->bio_flags = bio_flags;
eaf25d93 727 async->bio_offset = bio_offset;
8c8bee1d 728
cb03c743 729 atomic_inc(&fs_info->nr_async_submits);
d313d7a3 730
7b6d91da 731 if (rw & REQ_SYNC)
d313d7a3
CM
732 btrfs_set_work_high_prio(&async->work);
733
8b712842 734 btrfs_queue_worker(&fs_info->workers, &async->work);
9473f16c 735
d397712b 736 while (atomic_read(&fs_info->async_submit_draining) &&
771ed689
CM
737 atomic_read(&fs_info->nr_async_submits)) {
738 wait_event(fs_info->async_submit_wait,
739 (atomic_read(&fs_info->nr_async_submits) == 0));
740 }
741
44b8bd7e
CM
742 return 0;
743}
744
ce3ed71a
CM
745static int btree_csum_one_bio(struct bio *bio)
746{
747 struct bio_vec *bvec = bio->bi_io_vec;
748 int bio_index = 0;
749 struct btrfs_root *root;
750
751 WARN_ON(bio->bi_vcnt <= 0);
d397712b 752 while (bio_index < bio->bi_vcnt) {
ce3ed71a
CM
753 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
754 csum_dirty_buffer(root, bvec->bv_page);
755 bio_index++;
756 bvec++;
757 }
758 return 0;
759}
760
4a69a410
CM
761static int __btree_submit_bio_start(struct inode *inode, int rw,
762 struct bio *bio, int mirror_num,
eaf25d93
CM
763 unsigned long bio_flags,
764 u64 bio_offset)
22c59948 765{
8b712842
CM
766 /*
767 * when we're called for a write, we're already in the async
5443be45 768 * submission context. Just jump into btrfs_map_bio
8b712842 769 */
4a69a410
CM
770 btree_csum_one_bio(bio);
771 return 0;
772}
22c59948 773
4a69a410 774static int __btree_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
eaf25d93
CM
775 int mirror_num, unsigned long bio_flags,
776 u64 bio_offset)
4a69a410 777{
8b712842 778 /*
4a69a410
CM
779 * when we're called for a write, we're already in the async
780 * submission context. Just jump into btrfs_map_bio
8b712842 781 */
8b712842 782 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio, mirror_num, 1);
0b86a832
CM
783}
784
44b8bd7e 785static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
eaf25d93
CM
786 int mirror_num, unsigned long bio_flags,
787 u64 bio_offset)
44b8bd7e 788{
cad321ad
CM
789 int ret;
790
791 ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info,
792 bio, 1);
793 BUG_ON(ret);
794
7b6d91da 795 if (!(rw & REQ_WRITE)) {
4a69a410
CM
796 /*
797 * called for a read, do the setup so that checksum validation
798 * can happen in the async kernel threads
799 */
4a69a410 800 return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio,
6f3577bd 801 mirror_num, 0);
44b8bd7e 802 }
d313d7a3 803
cad321ad
CM
804 /*
805 * kthread helpers are used to submit writes so that checksumming
806 * can happen in parallel across all CPUs
807 */
44b8bd7e 808 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
c8b97818 809 inode, rw, bio, mirror_num, 0,
eaf25d93 810 bio_offset,
4a69a410
CM
811 __btree_submit_bio_start,
812 __btree_submit_bio_done);
44b8bd7e
CM
813}
814
3dd1462e 815#ifdef CONFIG_MIGRATION
784b4e29
CM
816static int btree_migratepage(struct address_space *mapping,
817 struct page *newpage, struct page *page)
818{
819 /*
820 * we can't safely write a btree page from here,
821 * we haven't done the locking hook
822 */
823 if (PageDirty(page))
824 return -EAGAIN;
825 /*
826 * Buffers may be managed in a filesystem specific way.
827 * We must have no buffers or drop them.
828 */
829 if (page_has_private(page) &&
830 !try_to_release_page(page, GFP_KERNEL))
831 return -EAGAIN;
784b4e29
CM
832 return migrate_page(mapping, newpage, page);
833}
3dd1462e 834#endif
784b4e29 835
0da5468f
CM
836static int btree_writepage(struct page *page, struct writeback_control *wbc)
837{
d1310b2e 838 struct extent_io_tree *tree;
b9473439
CM
839 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
840 struct extent_buffer *eb;
841 int was_dirty;
842
d1310b2e 843 tree = &BTRFS_I(page->mapping->host)->io_tree;
b9473439
CM
844 if (!(current->flags & PF_MEMALLOC)) {
845 return extent_write_full_page(tree, page,
846 btree_get_extent, wbc);
847 }
5443be45 848
b9473439 849 redirty_page_for_writepage(wbc, page);
784b4e29 850 eb = btrfs_find_tree_block(root, page_offset(page), PAGE_CACHE_SIZE);
b9473439
CM
851 WARN_ON(!eb);
852
853 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
854 if (!was_dirty) {
855 spin_lock(&root->fs_info->delalloc_lock);
856 root->fs_info->dirty_metadata_bytes += PAGE_CACHE_SIZE;
857 spin_unlock(&root->fs_info->delalloc_lock);
5443be45 858 }
b9473439
CM
859 free_extent_buffer(eb);
860
861 unlock_page(page);
862 return 0;
5f39d397 863}
0da5468f
CM
864
865static int btree_writepages(struct address_space *mapping,
866 struct writeback_control *wbc)
867{
d1310b2e
CM
868 struct extent_io_tree *tree;
869 tree = &BTRFS_I(mapping->host)->io_tree;
d8d5f3e1 870 if (wbc->sync_mode == WB_SYNC_NONE) {
b9473439 871 struct btrfs_root *root = BTRFS_I(mapping->host)->root;
793955bc 872 u64 num_dirty;
24ab9cd8 873 unsigned long thresh = 32 * 1024 * 1024;
448d640b
CM
874
875 if (wbc->for_kupdate)
876 return 0;
877
b9473439
CM
878 /* this is a bit racy, but that's ok */
879 num_dirty = root->fs_info->dirty_metadata_bytes;
d397712b 880 if (num_dirty < thresh)
793955bc 881 return 0;
793955bc 882 }
0da5468f
CM
883 return extent_writepages(tree, mapping, btree_get_extent, wbc);
884}
885
b2950863 886static int btree_readpage(struct file *file, struct page *page)
5f39d397 887{
d1310b2e
CM
888 struct extent_io_tree *tree;
889 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
890 return extent_read_full_page(tree, page, btree_get_extent);
891}
22b0ebda 892
70dec807 893static int btree_releasepage(struct page *page, gfp_t gfp_flags)
5f39d397 894{
d1310b2e
CM
895 struct extent_io_tree *tree;
896 struct extent_map_tree *map;
5f39d397 897 int ret;
d98237b3 898
98509cfc 899 if (PageWriteback(page) || PageDirty(page))
d397712b 900 return 0;
98509cfc 901
d1310b2e
CM
902 tree = &BTRFS_I(page->mapping->host)->io_tree;
903 map = &BTRFS_I(page->mapping->host)->extent_tree;
6af118ce 904
7b13b7b1 905 ret = try_release_extent_state(map, tree, page, gfp_flags);
d397712b 906 if (!ret)
6af118ce 907 return 0;
6af118ce
CM
908
909 ret = try_release_extent_buffer(tree, page);
5f39d397
CM
910 if (ret == 1) {
911 ClearPagePrivate(page);
912 set_page_private(page, 0);
913 page_cache_release(page);
914 }
6af118ce 915
d98237b3
CM
916 return ret;
917}
918
5f39d397 919static void btree_invalidatepage(struct page *page, unsigned long offset)
d98237b3 920{
d1310b2e
CM
921 struct extent_io_tree *tree;
922 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
923 extent_invalidatepage(tree, page, offset);
924 btree_releasepage(page, GFP_NOFS);
9ad6b7bc 925 if (PagePrivate(page)) {
d397712b
CM
926 printk(KERN_WARNING "btrfs warning page private not zero "
927 "on page %llu\n", (unsigned long long)page_offset(page));
9ad6b7bc
CM
928 ClearPagePrivate(page);
929 set_page_private(page, 0);
930 page_cache_release(page);
931 }
d98237b3
CM
932}
933
7f09410b 934static const struct address_space_operations btree_aops = {
d98237b3
CM
935 .readpage = btree_readpage,
936 .writepage = btree_writepage,
0da5468f 937 .writepages = btree_writepages,
5f39d397
CM
938 .releasepage = btree_releasepage,
939 .invalidatepage = btree_invalidatepage,
d98237b3 940 .sync_page = block_sync_page,
5a92bc88 941#ifdef CONFIG_MIGRATION
784b4e29 942 .migratepage = btree_migratepage,
5a92bc88 943#endif
d98237b3
CM
944};
945
ca7a79ad
CM
946int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
947 u64 parent_transid)
090d1875 948{
5f39d397
CM
949 struct extent_buffer *buf = NULL;
950 struct inode *btree_inode = root->fs_info->btree_inode;
de428b63 951 int ret = 0;
090d1875 952
db94535d 953 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397 954 if (!buf)
090d1875 955 return 0;
d1310b2e 956 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
f188591e 957 buf, 0, 0, btree_get_extent, 0);
5f39d397 958 free_extent_buffer(buf);
de428b63 959 return ret;
090d1875
CM
960}
961
0999df54
CM
962struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
963 u64 bytenr, u32 blocksize)
964{
965 struct inode *btree_inode = root->fs_info->btree_inode;
966 struct extent_buffer *eb;
967 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
968 bytenr, blocksize, GFP_NOFS);
969 return eb;
970}
971
972struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
973 u64 bytenr, u32 blocksize)
974{
975 struct inode *btree_inode = root->fs_info->btree_inode;
976 struct extent_buffer *eb;
977
978 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
979 bytenr, blocksize, NULL, GFP_NOFS);
980 return eb;
981}
982
983
e02119d5
CM
984int btrfs_write_tree_block(struct extent_buffer *buf)
985{
8aa38c31
CH
986 return filemap_fdatawrite_range(buf->first_page->mapping, buf->start,
987 buf->start + buf->len - 1);
e02119d5
CM
988}
989
990int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
991{
8aa38c31
CH
992 return filemap_fdatawait_range(buf->first_page->mapping,
993 buf->start, buf->start + buf->len - 1);
e02119d5
CM
994}
995
0999df54 996struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
ca7a79ad 997 u32 blocksize, u64 parent_transid)
0999df54
CM
998{
999 struct extent_buffer *buf = NULL;
0999df54
CM
1000 int ret;
1001
0999df54
CM
1002 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
1003 if (!buf)
1004 return NULL;
0999df54 1005
ca7a79ad 1006 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
ce9adaa5 1007
d397712b 1008 if (ret == 0)
b4ce94de 1009 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
5f39d397 1010 return buf;
ce9adaa5 1011
eb60ceac
CM
1012}
1013
e089f05c 1014int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 1015 struct extent_buffer *buf)
ed2ff2cb 1016{
5f39d397 1017 struct inode *btree_inode = root->fs_info->btree_inode;
55c69072 1018 if (btrfs_header_generation(buf) ==
925baedd 1019 root->fs_info->running_transaction->transid) {
b9447ef8 1020 btrfs_assert_tree_locked(buf);
b4ce94de 1021
b9473439
CM
1022 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
1023 spin_lock(&root->fs_info->delalloc_lock);
1024 if (root->fs_info->dirty_metadata_bytes >= buf->len)
1025 root->fs_info->dirty_metadata_bytes -= buf->len;
1026 else
1027 WARN_ON(1);
1028 spin_unlock(&root->fs_info->delalloc_lock);
1029 }
b4ce94de 1030
b9473439
CM
1031 /* ugh, clear_extent_buffer_dirty needs to lock the page */
1032 btrfs_set_lock_blocking(buf);
d1310b2e 1033 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
55c69072 1034 buf);
925baedd 1035 }
5f39d397
CM
1036 return 0;
1037}
1038
db94535d 1039static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
87ee04eb 1040 u32 stripesize, struct btrfs_root *root,
9f5fae2f 1041 struct btrfs_fs_info *fs_info,
e20d96d6 1042 u64 objectid)
d97e63b6 1043{
cfaa7295 1044 root->node = NULL;
a28ec197 1045 root->commit_root = NULL;
db94535d
CM
1046 root->sectorsize = sectorsize;
1047 root->nodesize = nodesize;
1048 root->leafsize = leafsize;
87ee04eb 1049 root->stripesize = stripesize;
123abc88 1050 root->ref_cows = 0;
0b86a832 1051 root->track_dirty = 0;
c71bf099 1052 root->in_radix = 0;
d68fc57b
YZ
1053 root->orphan_item_inserted = 0;
1054 root->orphan_cleanup_state = 0;
0b86a832 1055
9f5fae2f 1056 root->fs_info = fs_info;
0f7d52f4
CM
1057 root->objectid = objectid;
1058 root->last_trans = 0;
13a8a7c8 1059 root->highest_objectid = 0;
58176a96 1060 root->name = NULL;
4313b399 1061 root->in_sysfs = 0;
6bef4d31 1062 root->inode_tree = RB_ROOT;
f0486c68 1063 root->block_rsv = NULL;
d68fc57b 1064 root->orphan_block_rsv = NULL;
0b86a832
CM
1065
1066 INIT_LIST_HEAD(&root->dirty_list);
7b128766 1067 INIT_LIST_HEAD(&root->orphan_list);
5d4f98a2 1068 INIT_LIST_HEAD(&root->root_list);
925baedd 1069 spin_lock_init(&root->node_lock);
d68fc57b 1070 spin_lock_init(&root->orphan_lock);
5d4f98a2 1071 spin_lock_init(&root->inode_lock);
f0486c68 1072 spin_lock_init(&root->accounting_lock);
a2135011 1073 mutex_init(&root->objectid_mutex);
e02119d5 1074 mutex_init(&root->log_mutex);
7237f183
YZ
1075 init_waitqueue_head(&root->log_writer_wait);
1076 init_waitqueue_head(&root->log_commit_wait[0]);
1077 init_waitqueue_head(&root->log_commit_wait[1]);
1078 atomic_set(&root->log_commit[0], 0);
1079 atomic_set(&root->log_commit[1], 0);
1080 atomic_set(&root->log_writers, 0);
1081 root->log_batch = 0;
1082 root->log_transid = 0;
257c62e1 1083 root->last_log_commit = 0;
d0c803c4
CM
1084 extent_io_tree_init(&root->dirty_log_pages,
1085 fs_info->btree_inode->i_mapping, GFP_NOFS);
017e5369 1086
3768f368
CM
1087 memset(&root->root_key, 0, sizeof(root->root_key));
1088 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 1089 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
58176a96 1090 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
3f157a2f 1091 root->defrag_trans_start = fs_info->generation;
58176a96 1092 init_completion(&root->kobj_unregister);
6702ed49 1093 root->defrag_running = 0;
4d775673 1094 root->root_key.objectid = objectid;
3394e160
CM
1095 root->anon_super.s_root = NULL;
1096 root->anon_super.s_dev = 0;
1097 INIT_LIST_HEAD(&root->anon_super.s_list);
1098 INIT_LIST_HEAD(&root->anon_super.s_instances);
1099 init_rwsem(&root->anon_super.s_umount);
1100
3768f368
CM
1101 return 0;
1102}
1103
db94535d 1104static int find_and_setup_root(struct btrfs_root *tree_root,
9f5fae2f
CM
1105 struct btrfs_fs_info *fs_info,
1106 u64 objectid,
e20d96d6 1107 struct btrfs_root *root)
3768f368
CM
1108{
1109 int ret;
db94535d 1110 u32 blocksize;
84234f3a 1111 u64 generation;
3768f368 1112
db94535d 1113 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
1114 tree_root->sectorsize, tree_root->stripesize,
1115 root, fs_info, objectid);
3768f368
CM
1116 ret = btrfs_find_last_root(tree_root, objectid,
1117 &root->root_item, &root->root_key);
4df27c4d
YZ
1118 if (ret > 0)
1119 return -ENOENT;
3768f368
CM
1120 BUG_ON(ret);
1121
84234f3a 1122 generation = btrfs_root_generation(&root->root_item);
db94535d
CM
1123 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1124 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
84234f3a 1125 blocksize, generation);
68433b73
CM
1126 if (!root->node || !btrfs_buffer_uptodate(root->node, generation)) {
1127 free_extent_buffer(root->node);
1128 return -EIO;
1129 }
4df27c4d 1130 root->commit_root = btrfs_root_node(root);
d97e63b6
CM
1131 return 0;
1132}
1133
7237f183
YZ
1134static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1135 struct btrfs_fs_info *fs_info)
0f7d52f4
CM
1136{
1137 struct btrfs_root *root;
1138 struct btrfs_root *tree_root = fs_info->tree_root;
7237f183 1139 struct extent_buffer *leaf;
e02119d5
CM
1140
1141 root = kzalloc(sizeof(*root), GFP_NOFS);
1142 if (!root)
7237f183 1143 return ERR_PTR(-ENOMEM);
e02119d5
CM
1144
1145 __setup_root(tree_root->nodesize, tree_root->leafsize,
1146 tree_root->sectorsize, tree_root->stripesize,
1147 root, fs_info, BTRFS_TREE_LOG_OBJECTID);
1148
1149 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1150 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1151 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
7237f183
YZ
1152 /*
1153 * log trees do not get reference counted because they go away
1154 * before a real commit is actually done. They do store pointers
1155 * to file data extents, and those reference counts still get
1156 * updated (along with back refs to the log tree).
1157 */
e02119d5
CM
1158 root->ref_cows = 0;
1159
5d4f98a2
YZ
1160 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
1161 BTRFS_TREE_LOG_OBJECTID, NULL, 0, 0, 0);
7237f183
YZ
1162 if (IS_ERR(leaf)) {
1163 kfree(root);
1164 return ERR_CAST(leaf);
1165 }
e02119d5 1166
5d4f98a2
YZ
1167 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
1168 btrfs_set_header_bytenr(leaf, leaf->start);
1169 btrfs_set_header_generation(leaf, trans->transid);
1170 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1171 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
7237f183 1172 root->node = leaf;
e02119d5
CM
1173
1174 write_extent_buffer(root->node, root->fs_info->fsid,
1175 (unsigned long)btrfs_header_fsid(root->node),
1176 BTRFS_FSID_SIZE);
1177 btrfs_mark_buffer_dirty(root->node);
1178 btrfs_tree_unlock(root->node);
7237f183
YZ
1179 return root;
1180}
1181
1182int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1183 struct btrfs_fs_info *fs_info)
1184{
1185 struct btrfs_root *log_root;
1186
1187 log_root = alloc_log_tree(trans, fs_info);
1188 if (IS_ERR(log_root))
1189 return PTR_ERR(log_root);
1190 WARN_ON(fs_info->log_root_tree);
1191 fs_info->log_root_tree = log_root;
1192 return 0;
1193}
1194
1195int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *root)
1197{
1198 struct btrfs_root *log_root;
1199 struct btrfs_inode_item *inode_item;
1200
1201 log_root = alloc_log_tree(trans, root->fs_info);
1202 if (IS_ERR(log_root))
1203 return PTR_ERR(log_root);
1204
1205 log_root->last_trans = trans->transid;
1206 log_root->root_key.offset = root->root_key.objectid;
1207
1208 inode_item = &log_root->root_item.inode;
1209 inode_item->generation = cpu_to_le64(1);
1210 inode_item->size = cpu_to_le64(3);
1211 inode_item->nlink = cpu_to_le32(1);
1212 inode_item->nbytes = cpu_to_le64(root->leafsize);
1213 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
1214
5d4f98a2 1215 btrfs_set_root_node(&log_root->root_item, log_root->node);
7237f183
YZ
1216
1217 WARN_ON(root->log_root);
1218 root->log_root = log_root;
1219 root->log_transid = 0;
257c62e1 1220 root->last_log_commit = 0;
e02119d5
CM
1221 return 0;
1222}
1223
1224struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1225 struct btrfs_key *location)
1226{
1227 struct btrfs_root *root;
1228 struct btrfs_fs_info *fs_info = tree_root->fs_info;
0f7d52f4 1229 struct btrfs_path *path;
5f39d397 1230 struct extent_buffer *l;
84234f3a 1231 u64 generation;
db94535d 1232 u32 blocksize;
0f7d52f4
CM
1233 int ret = 0;
1234
5eda7b5e 1235 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 1236 if (!root)
0f7d52f4 1237 return ERR_PTR(-ENOMEM);
0f7d52f4 1238 if (location->offset == (u64)-1) {
db94535d 1239 ret = find_and_setup_root(tree_root, fs_info,
0f7d52f4
CM
1240 location->objectid, root);
1241 if (ret) {
0f7d52f4
CM
1242 kfree(root);
1243 return ERR_PTR(ret);
1244 }
13a8a7c8 1245 goto out;
0f7d52f4
CM
1246 }
1247
db94535d 1248 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
1249 tree_root->sectorsize, tree_root->stripesize,
1250 root, fs_info, location->objectid);
0f7d52f4
CM
1251
1252 path = btrfs_alloc_path();
db5b493a
TI
1253 if (!path) {
1254 kfree(root);
1255 return ERR_PTR(-ENOMEM);
1256 }
0f7d52f4 1257 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
13a8a7c8
YZ
1258 if (ret == 0) {
1259 l = path->nodes[0];
1260 read_extent_buffer(l, &root->root_item,
1261 btrfs_item_ptr_offset(l, path->slots[0]),
1262 sizeof(root->root_item));
1263 memcpy(&root->root_key, location, sizeof(*location));
0f7d52f4 1264 }
0f7d52f4
CM
1265 btrfs_free_path(path);
1266 if (ret) {
5e540f77 1267 kfree(root);
13a8a7c8
YZ
1268 if (ret > 0)
1269 ret = -ENOENT;
0f7d52f4
CM
1270 return ERR_PTR(ret);
1271 }
13a8a7c8 1272
84234f3a 1273 generation = btrfs_root_generation(&root->root_item);
db94535d
CM
1274 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1275 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
84234f3a 1276 blocksize, generation);
5d4f98a2 1277 root->commit_root = btrfs_root_node(root);
0f7d52f4 1278 BUG_ON(!root->node);
13a8a7c8 1279out:
08fe4db1 1280 if (location->objectid != BTRFS_TREE_LOG_OBJECTID) {
e02119d5 1281 root->ref_cows = 1;
08fe4db1
LZ
1282 btrfs_check_and_init_root_item(&root->root_item);
1283 }
13a8a7c8 1284
5eda7b5e
CM
1285 return root;
1286}
1287
dc17ff8f
CM
1288struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1289 u64 root_objectid)
1290{
1291 struct btrfs_root *root;
1292
1293 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
1294 return fs_info->tree_root;
1295 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
1296 return fs_info->extent_root;
1297
1298 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1299 (unsigned long)root_objectid);
1300 return root;
1301}
1302
edbd8d4e
CM
1303struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1304 struct btrfs_key *location)
5eda7b5e
CM
1305{
1306 struct btrfs_root *root;
1307 int ret;
1308
edbd8d4e
CM
1309 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1310 return fs_info->tree_root;
1311 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1312 return fs_info->extent_root;
8f18cf13
CM
1313 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1314 return fs_info->chunk_root;
1315 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1316 return fs_info->dev_root;
0403e47e
YZ
1317 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1318 return fs_info->csum_root;
4df27c4d
YZ
1319again:
1320 spin_lock(&fs_info->fs_roots_radix_lock);
5eda7b5e
CM
1321 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1322 (unsigned long)location->objectid);
4df27c4d 1323 spin_unlock(&fs_info->fs_roots_radix_lock);
5eda7b5e
CM
1324 if (root)
1325 return root;
1326
e02119d5 1327 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
5eda7b5e
CM
1328 if (IS_ERR(root))
1329 return root;
3394e160 1330
581bb050
LZ
1331 root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
1332 if (!root->free_ino_ctl)
1333 goto fail;
1334 root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
1335 GFP_NOFS);
1336 if (!root->free_ino_pinned)
1337 goto fail;
1338
1339 btrfs_init_free_ino_ctl(root);
1340 mutex_init(&root->fs_commit_mutex);
1341 spin_lock_init(&root->cache_lock);
1342 init_waitqueue_head(&root->cache_wait);
1343
3394e160
CM
1344 set_anon_super(&root->anon_super, NULL);
1345
d68fc57b
YZ
1346 if (btrfs_root_refs(&root->root_item) == 0) {
1347 ret = -ENOENT;
1348 goto fail;
1349 }
1350
1351 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1352 if (ret < 0)
1353 goto fail;
1354 if (ret == 0)
1355 root->orphan_item_inserted = 1;
1356
4df27c4d
YZ
1357 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1358 if (ret)
1359 goto fail;
1360
1361 spin_lock(&fs_info->fs_roots_radix_lock);
2619ba1f
CM
1362 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1363 (unsigned long)root->root_key.objectid,
0f7d52f4 1364 root);
d68fc57b 1365 if (ret == 0)
4df27c4d 1366 root->in_radix = 1;
d68fc57b 1367
4df27c4d
YZ
1368 spin_unlock(&fs_info->fs_roots_radix_lock);
1369 radix_tree_preload_end();
0f7d52f4 1370 if (ret) {
4df27c4d
YZ
1371 if (ret == -EEXIST) {
1372 free_fs_root(root);
1373 goto again;
1374 }
1375 goto fail;
0f7d52f4 1376 }
4df27c4d
YZ
1377
1378 ret = btrfs_find_dead_roots(fs_info->tree_root,
1379 root->root_key.objectid);
1380 WARN_ON(ret);
edbd8d4e 1381 return root;
4df27c4d
YZ
1382fail:
1383 free_fs_root(root);
1384 return ERR_PTR(ret);
edbd8d4e
CM
1385}
1386
1387struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1388 struct btrfs_key *location,
1389 const char *name, int namelen)
1390{
4df27c4d
YZ
1391 return btrfs_read_fs_root_no_name(fs_info, location);
1392#if 0
edbd8d4e
CM
1393 struct btrfs_root *root;
1394 int ret;
1395
1396 root = btrfs_read_fs_root_no_name(fs_info, location);
1397 if (!root)
1398 return NULL;
58176a96 1399
4313b399
CM
1400 if (root->in_sysfs)
1401 return root;
1402
58176a96
JB
1403 ret = btrfs_set_root_name(root, name, namelen);
1404 if (ret) {
5f39d397 1405 free_extent_buffer(root->node);
58176a96
JB
1406 kfree(root);
1407 return ERR_PTR(ret);
1408 }
4df27c4d 1409
58176a96
JB
1410 ret = btrfs_sysfs_add_root(root);
1411 if (ret) {
5f39d397 1412 free_extent_buffer(root->node);
58176a96
JB
1413 kfree(root->name);
1414 kfree(root);
1415 return ERR_PTR(ret);
1416 }
4313b399 1417 root->in_sysfs = 1;
0f7d52f4 1418 return root;
4df27c4d 1419#endif
0f7d52f4 1420}
04160088
CM
1421
1422static int btrfs_congested_fn(void *congested_data, int bdi_bits)
1423{
1424 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
1425 int ret = 0;
04160088
CM
1426 struct btrfs_device *device;
1427 struct backing_dev_info *bdi;
b7967db7 1428
c6e30871 1429 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
dfe25020
CM
1430 if (!device->bdev)
1431 continue;
04160088
CM
1432 bdi = blk_get_backing_dev_info(device->bdev);
1433 if (bdi && bdi_congested(bdi, bdi_bits)) {
1434 ret = 1;
1435 break;
1436 }
1437 }
1438 return ret;
1439}
1440
38b66988
CM
1441/*
1442 * this unplugs every device on the box, and it is only used when page
1443 * is null
1444 */
1445static void __unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1446{
38b66988
CM
1447 struct btrfs_device *device;
1448 struct btrfs_fs_info *info;
1449
1450 info = (struct btrfs_fs_info *)bdi->unplug_io_data;
c6e30871 1451 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
d20f7043
CM
1452 if (!device->bdev)
1453 continue;
1454
38b66988 1455 bdi = blk_get_backing_dev_info(device->bdev);
d397712b 1456 if (bdi->unplug_io_fn)
38b66988 1457 bdi->unplug_io_fn(bdi, page);
38b66988
CM
1458 }
1459}
1460
b2950863 1461static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
04160088 1462{
38b66988 1463 struct inode *inode;
f2d8d74d
CM
1464 struct extent_map_tree *em_tree;
1465 struct extent_map *em;
bcbfce8a 1466 struct address_space *mapping;
38b66988
CM
1467 u64 offset;
1468
bcbfce8a 1469 /* the generic O_DIRECT read code does this */
9f0ba5bd 1470 if (1 || !page) {
38b66988
CM
1471 __unplug_io_fn(bdi, page);
1472 return;
1473 }
1474
bcbfce8a
CM
1475 /*
1476 * page->mapping may change at any time. Get a consistent copy
1477 * and use that for everything below
1478 */
1479 smp_mb();
1480 mapping = page->mapping;
1481 if (!mapping)
1482 return;
1483
1484 inode = mapping->host;
240d5d48
CM
1485
1486 /*
1487 * don't do the expensive searching for a small number of
1488 * devices
1489 */
1490 if (BTRFS_I(inode)->root->fs_info->fs_devices->open_devices <= 2) {
1491 __unplug_io_fn(bdi, page);
1492 return;
1493 }
1494
38b66988 1495 offset = page_offset(page);
04160088 1496
f2d8d74d 1497 em_tree = &BTRFS_I(inode)->extent_tree;
890871be 1498 read_lock(&em_tree->lock);
f2d8d74d 1499 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
890871be 1500 read_unlock(&em_tree->lock);
89642229
CM
1501 if (!em) {
1502 __unplug_io_fn(bdi, page);
f2d8d74d 1503 return;
89642229 1504 }
f2d8d74d 1505
89642229
CM
1506 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1507 free_extent_map(em);
1508 __unplug_io_fn(bdi, page);
1509 return;
1510 }
f2d8d74d
CM
1511 offset = offset - em->start;
1512 btrfs_unplug_page(&BTRFS_I(inode)->root->fs_info->mapping_tree,
1513 em->block_start + offset, page);
1514 free_extent_map(em);
04160088
CM
1515}
1516
ad081f14
JA
1517/*
1518 * If this fails, caller must call bdi_destroy() to get rid of the
1519 * bdi again.
1520 */
04160088
CM
1521static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1522{
ad081f14
JA
1523 int err;
1524
1525 bdi->capabilities = BDI_CAP_MAP_COPY;
e6d086d8 1526 err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY);
ad081f14
JA
1527 if (err)
1528 return err;
1529
4575c9cc 1530 bdi->ra_pages = default_backing_dev_info.ra_pages;
04160088
CM
1531 bdi->unplug_io_fn = btrfs_unplug_io_fn;
1532 bdi->unplug_io_data = info;
1533 bdi->congested_fn = btrfs_congested_fn;
1534 bdi->congested_data = info;
1535 return 0;
1536}
1537
ce9adaa5
CM
1538static int bio_ready_for_csum(struct bio *bio)
1539{
1540 u64 length = 0;
1541 u64 buf_len = 0;
1542 u64 start = 0;
1543 struct page *page;
1544 struct extent_io_tree *io_tree = NULL;
ce9adaa5
CM
1545 struct bio_vec *bvec;
1546 int i;
1547 int ret;
1548
1549 bio_for_each_segment(bvec, bio, i) {
1550 page = bvec->bv_page;
1551 if (page->private == EXTENT_PAGE_PRIVATE) {
1552 length += bvec->bv_len;
1553 continue;
1554 }
1555 if (!page->private) {
1556 length += bvec->bv_len;
1557 continue;
1558 }
1559 length = bvec->bv_len;
1560 buf_len = page->private >> 2;
1561 start = page_offset(page) + bvec->bv_offset;
1562 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
ce9adaa5
CM
1563 }
1564 /* are we fully contained in this bio? */
1565 if (buf_len <= length)
1566 return 1;
1567
1568 ret = extent_range_uptodate(io_tree, start + length,
1569 start + buf_len - 1);
ce9adaa5
CM
1570 return ret;
1571}
1572
8b712842
CM
1573/*
1574 * called by the kthread helper functions to finally call the bio end_io
1575 * functions. This is where read checksum verification actually happens
1576 */
1577static void end_workqueue_fn(struct btrfs_work *work)
ce9adaa5 1578{
ce9adaa5 1579 struct bio *bio;
8b712842
CM
1580 struct end_io_wq *end_io_wq;
1581 struct btrfs_fs_info *fs_info;
ce9adaa5 1582 int error;
ce9adaa5 1583
8b712842
CM
1584 end_io_wq = container_of(work, struct end_io_wq, work);
1585 bio = end_io_wq->bio;
1586 fs_info = end_io_wq->info;
ce9adaa5 1587
cad321ad 1588 /* metadata bio reads are special because the whole tree block must
8b712842
CM
1589 * be checksummed at once. This makes sure the entire block is in
1590 * ram and up to date before trying to verify things. For
1591 * blocksize <= pagesize, it is basically a noop
1592 */
7b6d91da 1593 if (!(bio->bi_rw & REQ_WRITE) && end_io_wq->metadata &&
cad321ad 1594 !bio_ready_for_csum(bio)) {
d20f7043 1595 btrfs_queue_worker(&fs_info->endio_meta_workers,
8b712842
CM
1596 &end_io_wq->work);
1597 return;
1598 }
1599 error = end_io_wq->error;
1600 bio->bi_private = end_io_wq->private;
1601 bio->bi_end_io = end_io_wq->end_io;
1602 kfree(end_io_wq);
8b712842 1603 bio_endio(bio, error);
44b8bd7e
CM
1604}
1605
a74a4b97
CM
1606static int cleaner_kthread(void *arg)
1607{
1608 struct btrfs_root *root = arg;
1609
1610 do {
a74a4b97 1611 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
76dda93c
YZ
1612
1613 if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
1614 mutex_trylock(&root->fs_info->cleaner_mutex)) {
24bbcf04 1615 btrfs_run_delayed_iputs(root);
76dda93c
YZ
1616 btrfs_clean_old_snapshots(root);
1617 mutex_unlock(&root->fs_info->cleaner_mutex);
1618 }
a74a4b97
CM
1619
1620 if (freezing(current)) {
1621 refrigerator();
1622 } else {
a74a4b97 1623 set_current_state(TASK_INTERRUPTIBLE);
8929ecfa
YZ
1624 if (!kthread_should_stop())
1625 schedule();
a74a4b97
CM
1626 __set_current_state(TASK_RUNNING);
1627 }
1628 } while (!kthread_should_stop());
1629 return 0;
1630}
1631
1632static int transaction_kthread(void *arg)
1633{
1634 struct btrfs_root *root = arg;
1635 struct btrfs_trans_handle *trans;
1636 struct btrfs_transaction *cur;
8929ecfa 1637 u64 transid;
a74a4b97
CM
1638 unsigned long now;
1639 unsigned long delay;
1640 int ret;
1641
1642 do {
a74a4b97
CM
1643 delay = HZ * 30;
1644 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1645 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1646
8929ecfa 1647 spin_lock(&root->fs_info->new_trans_lock);
a74a4b97
CM
1648 cur = root->fs_info->running_transaction;
1649 if (!cur) {
8929ecfa 1650 spin_unlock(&root->fs_info->new_trans_lock);
a74a4b97
CM
1651 goto sleep;
1652 }
31153d81 1653
a74a4b97 1654 now = get_seconds();
8929ecfa
YZ
1655 if (!cur->blocked &&
1656 (now < cur->start_time || now - cur->start_time < 30)) {
1657 spin_unlock(&root->fs_info->new_trans_lock);
a74a4b97
CM
1658 delay = HZ * 5;
1659 goto sleep;
1660 }
8929ecfa
YZ
1661 transid = cur->transid;
1662 spin_unlock(&root->fs_info->new_trans_lock);
56bec294 1663
8929ecfa 1664 trans = btrfs_join_transaction(root, 1);
3612b495 1665 BUG_ON(IS_ERR(trans));
8929ecfa
YZ
1666 if (transid == trans->transid) {
1667 ret = btrfs_commit_transaction(trans, root);
1668 BUG_ON(ret);
1669 } else {
1670 btrfs_end_transaction(trans, root);
1671 }
a74a4b97
CM
1672sleep:
1673 wake_up_process(root->fs_info->cleaner_kthread);
1674 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
1675
1676 if (freezing(current)) {
1677 refrigerator();
1678 } else {
a74a4b97 1679 set_current_state(TASK_INTERRUPTIBLE);
8929ecfa
YZ
1680 if (!kthread_should_stop() &&
1681 !btrfs_transaction_blocked(root->fs_info))
1682 schedule_timeout(delay);
a74a4b97
CM
1683 __set_current_state(TASK_RUNNING);
1684 }
1685 } while (!kthread_should_stop());
1686 return 0;
1687}
1688
8a4b83cc 1689struct btrfs_root *open_ctree(struct super_block *sb,
dfe25020
CM
1690 struct btrfs_fs_devices *fs_devices,
1691 char *options)
2e635a27 1692{
db94535d
CM
1693 u32 sectorsize;
1694 u32 nodesize;
1695 u32 leafsize;
1696 u32 blocksize;
87ee04eb 1697 u32 stripesize;
84234f3a 1698 u64 generation;
f2b636e8 1699 u64 features;
3de4586c 1700 struct btrfs_key location;
a061fc8d 1701 struct buffer_head *bh;
e02119d5 1702 struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
e20d96d6 1703 GFP_NOFS);
d20f7043
CM
1704 struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
1705 GFP_NOFS);
450ba0ea
JB
1706 struct btrfs_root *tree_root = btrfs_sb(sb);
1707 struct btrfs_fs_info *fs_info = tree_root->fs_info;
e02119d5 1708 struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
0b86a832 1709 GFP_NOFS);
e02119d5 1710 struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
0b86a832 1711 GFP_NOFS);
e02119d5
CM
1712 struct btrfs_root *log_tree_root;
1713
eb60ceac 1714 int ret;
e58ca020 1715 int err = -EINVAL;
4543df7e 1716
2c90e5d6 1717 struct btrfs_super_block *disk_super;
8790d502 1718
0463bb4e 1719 if (!extent_root || !tree_root || !fs_info ||
d20f7043 1720 !chunk_root || !dev_root || !csum_root) {
39279cc3
CM
1721 err = -ENOMEM;
1722 goto fail;
1723 }
76dda93c
YZ
1724
1725 ret = init_srcu_struct(&fs_info->subvol_srcu);
1726 if (ret) {
1727 err = ret;
1728 goto fail;
1729 }
1730
1731 ret = setup_bdi(fs_info, &fs_info->bdi);
1732 if (ret) {
1733 err = ret;
1734 goto fail_srcu;
1735 }
1736
1737 fs_info->btree_inode = new_inode(sb);
1738 if (!fs_info->btree_inode) {
1739 err = -ENOMEM;
1740 goto fail_bdi;
1741 }
1742
1561deda
MX
1743 fs_info->btree_inode->i_mapping->flags &= ~__GFP_FS;
1744
76dda93c 1745 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
8fd17795 1746 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 1747 INIT_LIST_HEAD(&fs_info->dead_roots);
24bbcf04 1748 INIT_LIST_HEAD(&fs_info->delayed_iputs);
19c00ddc 1749 INIT_LIST_HEAD(&fs_info->hashers);
ea8c2819 1750 INIT_LIST_HEAD(&fs_info->delalloc_inodes);
5a3f23d5 1751 INIT_LIST_HEAD(&fs_info->ordered_operations);
11833d66 1752 INIT_LIST_HEAD(&fs_info->caching_block_groups);
1832a6d5 1753 spin_lock_init(&fs_info->delalloc_lock);
cee36a03 1754 spin_lock_init(&fs_info->new_trans_lock);
31153d81 1755 spin_lock_init(&fs_info->ref_cache_lock);
76dda93c 1756 spin_lock_init(&fs_info->fs_roots_radix_lock);
24bbcf04 1757 spin_lock_init(&fs_info->delayed_iput_lock);
19c00ddc 1758
58176a96 1759 init_completion(&fs_info->kobj_unregister);
9f5fae2f
CM
1760 fs_info->tree_root = tree_root;
1761 fs_info->extent_root = extent_root;
d20f7043 1762 fs_info->csum_root = csum_root;
0b86a832
CM
1763 fs_info->chunk_root = chunk_root;
1764 fs_info->dev_root = dev_root;
8a4b83cc 1765 fs_info->fs_devices = fs_devices;
0b86a832 1766 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
6324fbf3 1767 INIT_LIST_HEAD(&fs_info->space_info);
0b86a832 1768 btrfs_mapping_init(&fs_info->mapping_tree);
f0486c68
YZ
1769 btrfs_init_block_rsv(&fs_info->global_block_rsv);
1770 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv);
1771 btrfs_init_block_rsv(&fs_info->trans_block_rsv);
1772 btrfs_init_block_rsv(&fs_info->chunk_block_rsv);
1773 btrfs_init_block_rsv(&fs_info->empty_block_rsv);
1774 INIT_LIST_HEAD(&fs_info->durable_block_rsv_list);
1775 mutex_init(&fs_info->durable_block_rsv_mutex);
cb03c743 1776 atomic_set(&fs_info->nr_async_submits, 0);
771ed689 1777 atomic_set(&fs_info->async_delalloc_pages, 0);
8c8bee1d 1778 atomic_set(&fs_info->async_submit_draining, 0);
0986fe9e 1779 atomic_set(&fs_info->nr_async_bios, 0);
e20d96d6 1780 fs_info->sb = sb;
6f568d35 1781 fs_info->max_inline = 8192 * 1024;
9ed74f2d 1782 fs_info->metadata_ratio = 0;
c8b97818 1783
b34b086c
CM
1784 fs_info->thread_pool_size = min_t(unsigned long,
1785 num_online_cpus() + 2, 8);
0afbaf8c 1786
3eaa2885
CM
1787 INIT_LIST_HEAD(&fs_info->ordered_extents);
1788 spin_lock_init(&fs_info->ordered_extent_lock);
1789
a061fc8d
CM
1790 sb->s_blocksize = 4096;
1791 sb->s_blocksize_bits = blksize_bits(4096);
32a88aa1 1792 sb->s_bdi = &fs_info->bdi;
a061fc8d 1793
76dda93c
YZ
1794 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
1795 fs_info->btree_inode->i_nlink = 1;
0afbaf8c
CM
1796 /*
1797 * we set the i_size on the btree inode to the max possible int.
1798 * the real end of the address space is determined by all of
1799 * the devices in the system
1800 */
1801 fs_info->btree_inode->i_size = OFFSET_MAX;
d98237b3 1802 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
04160088
CM
1803 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1804
5d4f98a2 1805 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node);
d1310b2e 1806 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
5f39d397
CM
1807 fs_info->btree_inode->i_mapping,
1808 GFP_NOFS);
d1310b2e
CM
1809 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1810 GFP_NOFS);
1811
1812 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
0da5468f 1813
76dda93c
YZ
1814 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1815 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1816 sizeof(struct btrfs_key));
1817 BTRFS_I(fs_info->btree_inode)->dummy_inode = 1;
c65ddb52 1818 insert_inode_hash(fs_info->btree_inode);
76dda93c 1819
0f9dd46c 1820 spin_lock_init(&fs_info->block_group_cache_lock);
6bef4d31 1821 fs_info->block_group_cache_tree = RB_ROOT;
0f9dd46c 1822
11833d66 1823 extent_io_tree_init(&fs_info->freed_extents[0],
1a5bc167 1824 fs_info->btree_inode->i_mapping, GFP_NOFS);
11833d66
YZ
1825 extent_io_tree_init(&fs_info->freed_extents[1],
1826 fs_info->btree_inode->i_mapping, GFP_NOFS);
1827 fs_info->pinned_extents = &fs_info->freed_extents[0];
e66f709b 1828 fs_info->do_barriers = 1;
e18e4809 1829
39279cc3 1830
79154b1b 1831 mutex_init(&fs_info->trans_mutex);
5a3f23d5 1832 mutex_init(&fs_info->ordered_operations_mutex);
e02119d5 1833 mutex_init(&fs_info->tree_log_mutex);
925baedd 1834 mutex_init(&fs_info->chunk_mutex);
a74a4b97
CM
1835 mutex_init(&fs_info->transaction_kthread_mutex);
1836 mutex_init(&fs_info->cleaner_mutex);
7d9eb12c 1837 mutex_init(&fs_info->volume_mutex);
276e680d 1838 init_rwsem(&fs_info->extent_commit_sem);
c71bf099 1839 init_rwsem(&fs_info->cleanup_work_sem);
76dda93c 1840 init_rwsem(&fs_info->subvol_sem);
fa9c0d79
CM
1841
1842 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
1843 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
1844
e6dcd2dc 1845 init_waitqueue_head(&fs_info->transaction_throttle);
f9295749 1846 init_waitqueue_head(&fs_info->transaction_wait);
bb9c12c9 1847 init_waitqueue_head(&fs_info->transaction_blocked_wait);
4854ddd0 1848 init_waitqueue_head(&fs_info->async_submit_wait);
3768f368 1849
0b86a832 1850 __setup_root(4096, 4096, 4096, 4096, tree_root,
2c90e5d6 1851 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 1852
a512bbf8 1853 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
20b45077
DY
1854 if (!bh) {
1855 err = -EINVAL;
39279cc3 1856 goto fail_iput;
20b45077 1857 }
39279cc3 1858
a061fc8d 1859 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
2d69a0f8
YZ
1860 memcpy(&fs_info->super_for_commit, &fs_info->super_copy,
1861 sizeof(fs_info->super_for_commit));
a061fc8d 1862 brelse(bh);
5f39d397 1863
a061fc8d 1864 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
0b86a832 1865
5f39d397 1866 disk_super = &fs_info->super_copy;
0f7d52f4 1867 if (!btrfs_super_root(disk_super))
c6e2bac1 1868 goto fail_iput;
0f7d52f4 1869
acce952b 1870 /* check FS state, whether FS is broken. */
1871 fs_info->fs_state |= btrfs_super_flags(disk_super);
1872
1873 btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY);
1874
75e7cb7f
LB
1875 /*
1876 * In the long term, we'll store the compression type in the super
1877 * block, and it'll be used for per file compression control.
1878 */
1879 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
1880
2b82032c
YZ
1881 ret = btrfs_parse_options(tree_root, options);
1882 if (ret) {
1883 err = ret;
c6e2bac1 1884 goto fail_iput;
2b82032c 1885 }
dfe25020 1886
f2b636e8
JB
1887 features = btrfs_super_incompat_flags(disk_super) &
1888 ~BTRFS_FEATURE_INCOMPAT_SUPP;
1889 if (features) {
1890 printk(KERN_ERR "BTRFS: couldn't mount because of "
1891 "unsupported optional features (%Lx).\n",
21380931 1892 (unsigned long long)features);
f2b636e8 1893 err = -EINVAL;
c6e2bac1 1894 goto fail_iput;
f2b636e8
JB
1895 }
1896
5d4f98a2 1897 features = btrfs_super_incompat_flags(disk_super);
a6fa6fae
LZ
1898 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
1899 if (tree_root->fs_info->compress_type & BTRFS_COMPRESS_LZO)
1900 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1901 btrfs_set_super_incompat_flags(disk_super, features);
5d4f98a2 1902
f2b636e8
JB
1903 features = btrfs_super_compat_ro_flags(disk_super) &
1904 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
1905 if (!(sb->s_flags & MS_RDONLY) && features) {
1906 printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
1907 "unsupported option features (%Lx).\n",
21380931 1908 (unsigned long long)features);
f2b636e8 1909 err = -EINVAL;
c6e2bac1 1910 goto fail_iput;
f2b636e8 1911 }
61d92c32
CM
1912
1913 btrfs_init_workers(&fs_info->generic_worker,
1914 "genwork", 1, NULL);
1915
5443be45 1916 btrfs_init_workers(&fs_info->workers, "worker",
61d92c32
CM
1917 fs_info->thread_pool_size,
1918 &fs_info->generic_worker);
c8b97818 1919
771ed689 1920 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
61d92c32
CM
1921 fs_info->thread_pool_size,
1922 &fs_info->generic_worker);
771ed689 1923
5443be45 1924 btrfs_init_workers(&fs_info->submit_workers, "submit",
b720d209 1925 min_t(u64, fs_devices->num_devices,
61d92c32
CM
1926 fs_info->thread_pool_size),
1927 &fs_info->generic_worker);
61b49440
CM
1928
1929 /* a higher idle thresh on the submit workers makes it much more
1930 * likely that bios will be send down in a sane order to the
1931 * devices
1932 */
1933 fs_info->submit_workers.idle_thresh = 64;
53863232 1934
771ed689 1935 fs_info->workers.idle_thresh = 16;
4a69a410 1936 fs_info->workers.ordered = 1;
61b49440 1937
771ed689
CM
1938 fs_info->delalloc_workers.idle_thresh = 2;
1939 fs_info->delalloc_workers.ordered = 1;
1940
61d92c32
CM
1941 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1,
1942 &fs_info->generic_worker);
5443be45 1943 btrfs_init_workers(&fs_info->endio_workers, "endio",
61d92c32
CM
1944 fs_info->thread_pool_size,
1945 &fs_info->generic_worker);
d20f7043 1946 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
61d92c32
CM
1947 fs_info->thread_pool_size,
1948 &fs_info->generic_worker);
cad321ad 1949 btrfs_init_workers(&fs_info->endio_meta_write_workers,
61d92c32
CM
1950 "endio-meta-write", fs_info->thread_pool_size,
1951 &fs_info->generic_worker);
5443be45 1952 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
61d92c32
CM
1953 fs_info->thread_pool_size,
1954 &fs_info->generic_worker);
0cb59c99
JB
1955 btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write",
1956 1, &fs_info->generic_worker);
61b49440
CM
1957
1958 /*
1959 * endios are largely parallel and should have a very
1960 * low idle thresh
1961 */
1962 fs_info->endio_workers.idle_thresh = 4;
b51912c9
CM
1963 fs_info->endio_meta_workers.idle_thresh = 4;
1964
9042846b
CM
1965 fs_info->endio_write_workers.idle_thresh = 2;
1966 fs_info->endio_meta_write_workers.idle_thresh = 2;
1967
4543df7e 1968 btrfs_start_workers(&fs_info->workers, 1);
61d92c32 1969 btrfs_start_workers(&fs_info->generic_worker, 1);
1cc127b5 1970 btrfs_start_workers(&fs_info->submit_workers, 1);
771ed689 1971 btrfs_start_workers(&fs_info->delalloc_workers, 1);
247e743c 1972 btrfs_start_workers(&fs_info->fixup_workers, 1);
9042846b
CM
1973 btrfs_start_workers(&fs_info->endio_workers, 1);
1974 btrfs_start_workers(&fs_info->endio_meta_workers, 1);
1975 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1);
1976 btrfs_start_workers(&fs_info->endio_write_workers, 1);
0cb59c99 1977 btrfs_start_workers(&fs_info->endio_freespace_worker, 1);
4543df7e 1978
4575c9cc 1979 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
c8b97818
CM
1980 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
1981 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
4575c9cc 1982
db94535d
CM
1983 nodesize = btrfs_super_nodesize(disk_super);
1984 leafsize = btrfs_super_leafsize(disk_super);
1985 sectorsize = btrfs_super_sectorsize(disk_super);
87ee04eb 1986 stripesize = btrfs_super_stripesize(disk_super);
db94535d
CM
1987 tree_root->nodesize = nodesize;
1988 tree_root->leafsize = leafsize;
1989 tree_root->sectorsize = sectorsize;
87ee04eb 1990 tree_root->stripesize = stripesize;
a061fc8d
CM
1991
1992 sb->s_blocksize = sectorsize;
1993 sb->s_blocksize_bits = blksize_bits(sectorsize);
db94535d 1994
39279cc3
CM
1995 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1996 sizeof(disk_super->magic))) {
d397712b 1997 printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id);
39279cc3
CM
1998 goto fail_sb_buffer;
1999 }
19c00ddc 2000
925baedd 2001 mutex_lock(&fs_info->chunk_mutex);
e4404d6e 2002 ret = btrfs_read_sys_array(tree_root);
925baedd 2003 mutex_unlock(&fs_info->chunk_mutex);
84eed90f 2004 if (ret) {
d397712b
CM
2005 printk(KERN_WARNING "btrfs: failed to read the system "
2006 "array on %s\n", sb->s_id);
5d4f98a2 2007 goto fail_sb_buffer;
84eed90f 2008 }
0b86a832
CM
2009
2010 blocksize = btrfs_level_size(tree_root,
2011 btrfs_super_chunk_root_level(disk_super));
84234f3a 2012 generation = btrfs_super_chunk_root_generation(disk_super);
0b86a832
CM
2013
2014 __setup_root(nodesize, leafsize, sectorsize, stripesize,
2015 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
2016
2017 chunk_root->node = read_tree_block(chunk_root,
2018 btrfs_super_chunk_root(disk_super),
84234f3a 2019 blocksize, generation);
0b86a832 2020 BUG_ON(!chunk_root->node);
83121942
DW
2021 if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) {
2022 printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n",
2023 sb->s_id);
2024 goto fail_chunk_root;
2025 }
5d4f98a2
YZ
2026 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
2027 chunk_root->commit_root = btrfs_root_node(chunk_root);
0b86a832 2028
e17cade2 2029 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
d397712b
CM
2030 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
2031 BTRFS_UUID_SIZE);
e17cade2 2032
925baedd 2033 mutex_lock(&fs_info->chunk_mutex);
0b86a832 2034 ret = btrfs_read_chunk_tree(chunk_root);
925baedd 2035 mutex_unlock(&fs_info->chunk_mutex);
2b82032c 2036 if (ret) {
d397712b
CM
2037 printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n",
2038 sb->s_id);
2b82032c
YZ
2039 goto fail_chunk_root;
2040 }
0b86a832 2041
dfe25020
CM
2042 btrfs_close_extra_devices(fs_devices);
2043
db94535d
CM
2044 blocksize = btrfs_level_size(tree_root,
2045 btrfs_super_root_level(disk_super));
84234f3a 2046 generation = btrfs_super_generation(disk_super);
0b86a832 2047
e20d96d6 2048 tree_root->node = read_tree_block(tree_root,
db94535d 2049 btrfs_super_root(disk_super),
84234f3a 2050 blocksize, generation);
39279cc3 2051 if (!tree_root->node)
2b82032c 2052 goto fail_chunk_root;
83121942
DW
2053 if (!test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) {
2054 printk(KERN_WARNING "btrfs: failed to read tree root on %s\n",
2055 sb->s_id);
2056 goto fail_tree_root;
2057 }
5d4f98a2
YZ
2058 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
2059 tree_root->commit_root = btrfs_root_node(tree_root);
db94535d
CM
2060
2061 ret = find_and_setup_root(tree_root, fs_info,
e20d96d6 2062 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
0b86a832 2063 if (ret)
39279cc3 2064 goto fail_tree_root;
0b86a832
CM
2065 extent_root->track_dirty = 1;
2066
2067 ret = find_and_setup_root(tree_root, fs_info,
2068 BTRFS_DEV_TREE_OBJECTID, dev_root);
0b86a832
CM
2069 if (ret)
2070 goto fail_extent_root;
5d4f98a2 2071 dev_root->track_dirty = 1;
3768f368 2072
d20f7043
CM
2073 ret = find_and_setup_root(tree_root, fs_info,
2074 BTRFS_CSUM_TREE_OBJECTID, csum_root);
2075 if (ret)
5d4f98a2 2076 goto fail_dev_root;
d20f7043
CM
2077
2078 csum_root->track_dirty = 1;
2079
8929ecfa
YZ
2080 fs_info->generation = generation;
2081 fs_info->last_trans_committed = generation;
2082 fs_info->data_alloc_profile = (u64)-1;
2083 fs_info->metadata_alloc_profile = (u64)-1;
2084 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
2085
c59021f8 2086 ret = btrfs_init_space_info(fs_info);
2087 if (ret) {
2088 printk(KERN_ERR "Failed to initial space info: %d\n", ret);
2089 goto fail_block_groups;
2090 }
2091
1b1d1f66
JB
2092 ret = btrfs_read_block_groups(extent_root);
2093 if (ret) {
2094 printk(KERN_ERR "Failed to read block groups: %d\n", ret);
2095 goto fail_block_groups;
2096 }
9078a3e1 2097
a74a4b97
CM
2098 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
2099 "btrfs-cleaner");
57506d50 2100 if (IS_ERR(fs_info->cleaner_kthread))
1b1d1f66 2101 goto fail_block_groups;
a74a4b97
CM
2102
2103 fs_info->transaction_kthread = kthread_run(transaction_kthread,
2104 tree_root,
2105 "btrfs-transaction");
57506d50 2106 if (IS_ERR(fs_info->transaction_kthread))
3f157a2f 2107 goto fail_cleaner;
a74a4b97 2108
c289811c
CM
2109 if (!btrfs_test_opt(tree_root, SSD) &&
2110 !btrfs_test_opt(tree_root, NOSSD) &&
2111 !fs_info->fs_devices->rotating) {
2112 printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD "
2113 "mode\n");
2114 btrfs_set_opt(fs_info->mount_opt, SSD);
2115 }
2116
acce952b 2117 /* do not make disk changes in broken FS */
2118 if (btrfs_super_log_root(disk_super) != 0 &&
2119 !(fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)) {
e02119d5
CM
2120 u64 bytenr = btrfs_super_log_root(disk_super);
2121
7c2ca468 2122 if (fs_devices->rw_devices == 0) {
d397712b
CM
2123 printk(KERN_WARNING "Btrfs log replay required "
2124 "on RO media\n");
7c2ca468
CM
2125 err = -EIO;
2126 goto fail_trans_kthread;
2127 }
e02119d5
CM
2128 blocksize =
2129 btrfs_level_size(tree_root,
2130 btrfs_super_log_root_level(disk_super));
d18a2c44 2131
676e4c86
DC
2132 log_tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
2133 if (!log_tree_root) {
2134 err = -ENOMEM;
2135 goto fail_trans_kthread;
2136 }
e02119d5
CM
2137
2138 __setup_root(nodesize, leafsize, sectorsize, stripesize,
2139 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
2140
2141 log_tree_root->node = read_tree_block(tree_root, bytenr,
84234f3a
YZ
2142 blocksize,
2143 generation + 1);
e02119d5
CM
2144 ret = btrfs_recover_log_trees(log_tree_root);
2145 BUG_ON(ret);
e556ce2c
YZ
2146
2147 if (sb->s_flags & MS_RDONLY) {
2148 ret = btrfs_commit_super(tree_root);
2149 BUG_ON(ret);
2150 }
e02119d5 2151 }
1a40e23b 2152
76dda93c
YZ
2153 ret = btrfs_find_orphan_roots(tree_root);
2154 BUG_ON(ret);
2155
7c2ca468 2156 if (!(sb->s_flags & MS_RDONLY)) {
d68fc57b
YZ
2157 ret = btrfs_cleanup_fs_roots(fs_info);
2158 BUG_ON(ret);
2159
5d4f98a2 2160 ret = btrfs_recover_relocation(tree_root);
d7ce5843
MX
2161 if (ret < 0) {
2162 printk(KERN_WARNING
2163 "btrfs: failed to recover relocation\n");
2164 err = -EINVAL;
2165 goto fail_trans_kthread;
2166 }
7c2ca468 2167 }
1a40e23b 2168
3de4586c
CM
2169 location.objectid = BTRFS_FS_TREE_OBJECTID;
2170 location.type = BTRFS_ROOT_ITEM_KEY;
2171 location.offset = (u64)-1;
2172
3de4586c
CM
2173 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
2174 if (!fs_info->fs_root)
7c2ca468 2175 goto fail_trans_kthread;
3140c9a3
DC
2176 if (IS_ERR(fs_info->fs_root)) {
2177 err = PTR_ERR(fs_info->fs_root);
2178 goto fail_trans_kthread;
2179 }
c289811c 2180
e3acc2a6
JB
2181 if (!(sb->s_flags & MS_RDONLY)) {
2182 down_read(&fs_info->cleanup_work_sem);
66b4ffd1
JB
2183 err = btrfs_orphan_cleanup(fs_info->fs_root);
2184 if (!err)
2185 err = btrfs_orphan_cleanup(fs_info->tree_root);
e3acc2a6 2186 up_read(&fs_info->cleanup_work_sem);
66b4ffd1
JB
2187 if (err) {
2188 close_ctree(tree_root);
2189 return ERR_PTR(err);
2190 }
e3acc2a6
JB
2191 }
2192
0f7d52f4 2193 return tree_root;
39279cc3 2194
7c2ca468
CM
2195fail_trans_kthread:
2196 kthread_stop(fs_info->transaction_kthread);
3f157a2f 2197fail_cleaner:
a74a4b97 2198 kthread_stop(fs_info->cleaner_kthread);
7c2ca468
CM
2199
2200 /*
2201 * make sure we're done with the btree inode before we stop our
2202 * kthreads
2203 */
2204 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
2205 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
2206
1b1d1f66
JB
2207fail_block_groups:
2208 btrfs_free_block_groups(fs_info);
d20f7043 2209 free_extent_buffer(csum_root->node);
5d4f98a2
YZ
2210 free_extent_buffer(csum_root->commit_root);
2211fail_dev_root:
2212 free_extent_buffer(dev_root->node);
2213 free_extent_buffer(dev_root->commit_root);
0b86a832
CM
2214fail_extent_root:
2215 free_extent_buffer(extent_root->node);
5d4f98a2 2216 free_extent_buffer(extent_root->commit_root);
39279cc3 2217fail_tree_root:
5f39d397 2218 free_extent_buffer(tree_root->node);
5d4f98a2 2219 free_extent_buffer(tree_root->commit_root);
2b82032c
YZ
2220fail_chunk_root:
2221 free_extent_buffer(chunk_root->node);
5d4f98a2 2222 free_extent_buffer(chunk_root->commit_root);
39279cc3 2223fail_sb_buffer:
61d92c32 2224 btrfs_stop_workers(&fs_info->generic_worker);
247e743c 2225 btrfs_stop_workers(&fs_info->fixup_workers);
771ed689 2226 btrfs_stop_workers(&fs_info->delalloc_workers);
8b712842
CM
2227 btrfs_stop_workers(&fs_info->workers);
2228 btrfs_stop_workers(&fs_info->endio_workers);
d20f7043 2229 btrfs_stop_workers(&fs_info->endio_meta_workers);
cad321ad 2230 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
e6dcd2dc 2231 btrfs_stop_workers(&fs_info->endio_write_workers);
0cb59c99 2232 btrfs_stop_workers(&fs_info->endio_freespace_worker);
1cc127b5 2233 btrfs_stop_workers(&fs_info->submit_workers);
4543df7e 2234fail_iput:
7c2ca468 2235 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
4543df7e 2236 iput(fs_info->btree_inode);
7e662854 2237
dfe25020 2238 btrfs_close_devices(fs_info->fs_devices);
84eed90f 2239 btrfs_mapping_tree_free(&fs_info->mapping_tree);
ad081f14 2240fail_bdi:
7e662854 2241 bdi_destroy(&fs_info->bdi);
76dda93c
YZ
2242fail_srcu:
2243 cleanup_srcu_struct(&fs_info->subvol_srcu);
7e662854 2244fail:
39279cc3
CM
2245 kfree(extent_root);
2246 kfree(tree_root);
2247 kfree(fs_info);
83afeac4
JM
2248 kfree(chunk_root);
2249 kfree(dev_root);
d20f7043 2250 kfree(csum_root);
39279cc3 2251 return ERR_PTR(err);
eb60ceac
CM
2252}
2253
f2984462
CM
2254static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
2255{
2256 char b[BDEVNAME_SIZE];
2257
2258 if (uptodate) {
2259 set_buffer_uptodate(bh);
2260 } else {
c3b9a62c 2261 if (printk_ratelimit()) {
f2984462
CM
2262 printk(KERN_WARNING "lost page write due to "
2263 "I/O error on %s\n",
2264 bdevname(bh->b_bdev, b));
2265 }
1259ab75
CM
2266 /* note, we dont' set_buffer_write_io_error because we have
2267 * our own ways of dealing with the IO errors
2268 */
f2984462
CM
2269 clear_buffer_uptodate(bh);
2270 }
2271 unlock_buffer(bh);
2272 put_bh(bh);
2273}
2274
a512bbf8
YZ
2275struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
2276{
2277 struct buffer_head *bh;
2278 struct buffer_head *latest = NULL;
2279 struct btrfs_super_block *super;
2280 int i;
2281 u64 transid = 0;
2282 u64 bytenr;
2283
2284 /* we would like to check all the supers, but that would make
2285 * a btrfs mount succeed after a mkfs from a different FS.
2286 * So, we need to add a special mount option to scan for
2287 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
2288 */
2289 for (i = 0; i < 1; i++) {
2290 bytenr = btrfs_sb_offset(i);
2291 if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
2292 break;
2293 bh = __bread(bdev, bytenr / 4096, 4096);
2294 if (!bh)
2295 continue;
2296
2297 super = (struct btrfs_super_block *)bh->b_data;
2298 if (btrfs_super_bytenr(super) != bytenr ||
2299 strncmp((char *)(&super->magic), BTRFS_MAGIC,
2300 sizeof(super->magic))) {
2301 brelse(bh);
2302 continue;
2303 }
2304
2305 if (!latest || btrfs_super_generation(super) > transid) {
2306 brelse(latest);
2307 latest = bh;
2308 transid = btrfs_super_generation(super);
2309 } else {
2310 brelse(bh);
2311 }
2312 }
2313 return latest;
2314}
2315
4eedeb75
HH
2316/*
2317 * this should be called twice, once with wait == 0 and
2318 * once with wait == 1. When wait == 0 is done, all the buffer heads
2319 * we write are pinned.
2320 *
2321 * They are released when wait == 1 is done.
2322 * max_mirrors must be the same for both runs, and it indicates how
2323 * many supers on this one device should be written.
2324 *
2325 * max_mirrors == 0 means to write them all.
2326 */
a512bbf8
YZ
2327static int write_dev_supers(struct btrfs_device *device,
2328 struct btrfs_super_block *sb,
2329 int do_barriers, int wait, int max_mirrors)
2330{
2331 struct buffer_head *bh;
2332 int i;
2333 int ret;
2334 int errors = 0;
2335 u32 crc;
2336 u64 bytenr;
2337 int last_barrier = 0;
2338
2339 if (max_mirrors == 0)
2340 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
2341
2342 /* make sure only the last submit_bh does a barrier */
2343 if (do_barriers) {
2344 for (i = 0; i < max_mirrors; i++) {
2345 bytenr = btrfs_sb_offset(i);
2346 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
2347 device->total_bytes)
2348 break;
2349 last_barrier = i;
2350 }
2351 }
2352
2353 for (i = 0; i < max_mirrors; i++) {
2354 bytenr = btrfs_sb_offset(i);
2355 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
2356 break;
2357
2358 if (wait) {
2359 bh = __find_get_block(device->bdev, bytenr / 4096,
2360 BTRFS_SUPER_INFO_SIZE);
2361 BUG_ON(!bh);
a512bbf8 2362 wait_on_buffer(bh);
4eedeb75
HH
2363 if (!buffer_uptodate(bh))
2364 errors++;
2365
2366 /* drop our reference */
2367 brelse(bh);
2368
2369 /* drop the reference from the wait == 0 run */
2370 brelse(bh);
2371 continue;
a512bbf8
YZ
2372 } else {
2373 btrfs_set_super_bytenr(sb, bytenr);
2374
2375 crc = ~(u32)0;
2376 crc = btrfs_csum_data(NULL, (char *)sb +
2377 BTRFS_CSUM_SIZE, crc,
2378 BTRFS_SUPER_INFO_SIZE -
2379 BTRFS_CSUM_SIZE);
2380 btrfs_csum_final(crc, sb->csum);
2381
4eedeb75
HH
2382 /*
2383 * one reference for us, and we leave it for the
2384 * caller
2385 */
a512bbf8
YZ
2386 bh = __getblk(device->bdev, bytenr / 4096,
2387 BTRFS_SUPER_INFO_SIZE);
2388 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
2389
4eedeb75 2390 /* one reference for submit_bh */
a512bbf8 2391 get_bh(bh);
4eedeb75
HH
2392
2393 set_buffer_uptodate(bh);
a512bbf8
YZ
2394 lock_buffer(bh);
2395 bh->b_end_io = btrfs_end_buffer_write_sync;
2396 }
2397
c3b9a62c
CH
2398 if (i == last_barrier && do_barriers)
2399 ret = submit_bh(WRITE_FLUSH_FUA, bh);
2400 else
ffbd517d 2401 ret = submit_bh(WRITE_SYNC, bh);
a512bbf8 2402
4eedeb75 2403 if (ret)
a512bbf8 2404 errors++;
a512bbf8
YZ
2405 }
2406 return errors < i ? 0 : -1;
2407}
2408
2409int write_all_supers(struct btrfs_root *root, int max_mirrors)
f2984462 2410{
e5e9a520 2411 struct list_head *head;
f2984462 2412 struct btrfs_device *dev;
a061fc8d 2413 struct btrfs_super_block *sb;
f2984462 2414 struct btrfs_dev_item *dev_item;
f2984462
CM
2415 int ret;
2416 int do_barriers;
a236aed1
CM
2417 int max_errors;
2418 int total_errors = 0;
a061fc8d 2419 u64 flags;
f2984462 2420
a236aed1 2421 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
f2984462
CM
2422 do_barriers = !btrfs_test_opt(root, NOBARRIER);
2423
a061fc8d
CM
2424 sb = &root->fs_info->super_for_commit;
2425 dev_item = &sb->dev_item;
e5e9a520
CM
2426
2427 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2428 head = &root->fs_info->fs_devices->devices;
c6e30871 2429 list_for_each_entry(dev, head, dev_list) {
dfe25020
CM
2430 if (!dev->bdev) {
2431 total_errors++;
2432 continue;
2433 }
2b82032c 2434 if (!dev->in_fs_metadata || !dev->writeable)
dfe25020
CM
2435 continue;
2436
2b82032c 2437 btrfs_set_stack_device_generation(dev_item, 0);
a061fc8d
CM
2438 btrfs_set_stack_device_type(dev_item, dev->type);
2439 btrfs_set_stack_device_id(dev_item, dev->devid);
2440 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
2441 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
2442 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
2443 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
2444 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
2445 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
2b82032c 2446 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
a512bbf8 2447
a061fc8d
CM
2448 flags = btrfs_super_flags(sb);
2449 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
2450
a512bbf8 2451 ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
a236aed1
CM
2452 if (ret)
2453 total_errors++;
f2984462 2454 }
a236aed1 2455 if (total_errors > max_errors) {
d397712b
CM
2456 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2457 total_errors);
a236aed1
CM
2458 BUG();
2459 }
f2984462 2460
a512bbf8 2461 total_errors = 0;
c6e30871 2462 list_for_each_entry(dev, head, dev_list) {
dfe25020
CM
2463 if (!dev->bdev)
2464 continue;
2b82032c 2465 if (!dev->in_fs_metadata || !dev->writeable)
dfe25020
CM
2466 continue;
2467
a512bbf8
YZ
2468 ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
2469 if (ret)
2470 total_errors++;
f2984462 2471 }
e5e9a520 2472 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
a236aed1 2473 if (total_errors > max_errors) {
d397712b
CM
2474 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2475 total_errors);
a236aed1
CM
2476 BUG();
2477 }
f2984462
CM
2478 return 0;
2479}
2480
a512bbf8
YZ
2481int write_ctree_super(struct btrfs_trans_handle *trans,
2482 struct btrfs_root *root, int max_mirrors)
eb60ceac 2483{
e66f709b 2484 int ret;
5f39d397 2485
a512bbf8 2486 ret = write_all_supers(root, max_mirrors);
5f39d397 2487 return ret;
cfaa7295
CM
2488}
2489
5eda7b5e 2490int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619ba1f 2491{
4df27c4d 2492 spin_lock(&fs_info->fs_roots_radix_lock);
2619ba1f
CM
2493 radix_tree_delete(&fs_info->fs_roots_radix,
2494 (unsigned long)root->root_key.objectid);
4df27c4d 2495 spin_unlock(&fs_info->fs_roots_radix_lock);
76dda93c
YZ
2496
2497 if (btrfs_root_refs(&root->root_item) == 0)
2498 synchronize_srcu(&fs_info->subvol_srcu);
2499
581bb050
LZ
2500 __btrfs_remove_free_space_cache(root->free_ino_pinned);
2501 __btrfs_remove_free_space_cache(root->free_ino_ctl);
4df27c4d
YZ
2502 free_fs_root(root);
2503 return 0;
2504}
2505
2506static void free_fs_root(struct btrfs_root *root)
2507{
2508 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
3394e160
CM
2509 if (root->anon_super.s_dev) {
2510 down_write(&root->anon_super.s_umount);
2511 kill_anon_super(&root->anon_super);
2512 }
4df27c4d
YZ
2513 free_extent_buffer(root->node);
2514 free_extent_buffer(root->commit_root);
581bb050
LZ
2515 kfree(root->free_ino_ctl);
2516 kfree(root->free_ino_pinned);
d397712b 2517 kfree(root->name);
2619ba1f 2518 kfree(root);
2619ba1f
CM
2519}
2520
35b7e476 2521static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
2522{
2523 int ret;
2524 struct btrfs_root *gang[8];
2525 int i;
2526
76dda93c
YZ
2527 while (!list_empty(&fs_info->dead_roots)) {
2528 gang[0] = list_entry(fs_info->dead_roots.next,
2529 struct btrfs_root, root_list);
2530 list_del(&gang[0]->root_list);
2531
2532 if (gang[0]->in_radix) {
2533 btrfs_free_fs_root(fs_info, gang[0]);
2534 } else {
2535 free_extent_buffer(gang[0]->node);
2536 free_extent_buffer(gang[0]->commit_root);
2537 kfree(gang[0]);
2538 }
2539 }
2540
d397712b 2541 while (1) {
0f7d52f4
CM
2542 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2543 (void **)gang, 0,
2544 ARRAY_SIZE(gang));
2545 if (!ret)
2546 break;
2619ba1f 2547 for (i = 0; i < ret; i++)
5eda7b5e 2548 btrfs_free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
2549 }
2550 return 0;
2551}
b4100d64 2552
c146afad 2553int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
cfaa7295 2554{
c146afad
YZ
2555 u64 root_objectid = 0;
2556 struct btrfs_root *gang[8];
2557 int i;
3768f368 2558 int ret;
e089f05c 2559
c146afad
YZ
2560 while (1) {
2561 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2562 (void **)gang, root_objectid,
2563 ARRAY_SIZE(gang));
2564 if (!ret)
2565 break;
5d4f98a2
YZ
2566
2567 root_objectid = gang[ret - 1]->root_key.objectid + 1;
c146afad 2568 for (i = 0; i < ret; i++) {
66b4ffd1
JB
2569 int err;
2570
c146afad 2571 root_objectid = gang[i]->root_key.objectid;
66b4ffd1
JB
2572 err = btrfs_orphan_cleanup(gang[i]);
2573 if (err)
2574 return err;
c146afad
YZ
2575 }
2576 root_objectid++;
2577 }
2578 return 0;
2579}
a2135011 2580
c146afad
YZ
2581int btrfs_commit_super(struct btrfs_root *root)
2582{
2583 struct btrfs_trans_handle *trans;
2584 int ret;
a74a4b97 2585
c146afad 2586 mutex_lock(&root->fs_info->cleaner_mutex);
24bbcf04 2587 btrfs_run_delayed_iputs(root);
a74a4b97 2588 btrfs_clean_old_snapshots(root);
c146afad 2589 mutex_unlock(&root->fs_info->cleaner_mutex);
c71bf099
YZ
2590
2591 /* wait until ongoing cleanup work done */
2592 down_write(&root->fs_info->cleanup_work_sem);
2593 up_write(&root->fs_info->cleanup_work_sem);
2594
a22285a6 2595 trans = btrfs_join_transaction(root, 1);
3612b495
TI
2596 if (IS_ERR(trans))
2597 return PTR_ERR(trans);
54aa1f4d 2598 ret = btrfs_commit_transaction(trans, root);
c146afad
YZ
2599 BUG_ON(ret);
2600 /* run commit again to drop the original snapshot */
a22285a6 2601 trans = btrfs_join_transaction(root, 1);
3612b495
TI
2602 if (IS_ERR(trans))
2603 return PTR_ERR(trans);
79154b1b
CM
2604 btrfs_commit_transaction(trans, root);
2605 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 2606 BUG_ON(ret);
d6bfde87 2607
a512bbf8 2608 ret = write_ctree_super(NULL, root, 0);
c146afad
YZ
2609 return ret;
2610}
2611
2612int close_ctree(struct btrfs_root *root)
2613{
2614 struct btrfs_fs_info *fs_info = root->fs_info;
2615 int ret;
2616
2617 fs_info->closing = 1;
2618 smp_mb();
2619
0af3d00b 2620 btrfs_put_block_group_cache(fs_info);
acce952b 2621
2622 /*
2623 * Here come 2 situations when btrfs is broken to flip readonly:
2624 *
2625 * 1. when btrfs flips readonly somewhere else before
2626 * btrfs_commit_super, sb->s_flags has MS_RDONLY flag,
2627 * and btrfs will skip to write sb directly to keep
2628 * ERROR state on disk.
2629 *
2630 * 2. when btrfs flips readonly just in btrfs_commit_super,
2631 * and in such case, btrfs cannnot write sb via btrfs_commit_super,
2632 * and since fs_state has been set BTRFS_SUPER_FLAG_ERROR flag,
2633 * btrfs will cleanup all FS resources first and write sb then.
2634 */
c146afad 2635 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
acce952b 2636 ret = btrfs_commit_super(root);
2637 if (ret)
2638 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
2639 }
2640
2641 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
2642 ret = btrfs_error_commit_super(root);
d397712b
CM
2643 if (ret)
2644 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
c146afad 2645 }
0f7d52f4 2646
8929ecfa
YZ
2647 kthread_stop(root->fs_info->transaction_kthread);
2648 kthread_stop(root->fs_info->cleaner_kthread);
2649
f25784b3
YZ
2650 fs_info->closing = 2;
2651 smp_mb();
2652
b0c68f8b 2653 if (fs_info->delalloc_bytes) {
d397712b 2654 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
21380931 2655 (unsigned long long)fs_info->delalloc_bytes);
b0c68f8b 2656 }
31153d81 2657 if (fs_info->total_ref_cache_size) {
d397712b
CM
2658 printk(KERN_INFO "btrfs: at umount reference cache size %llu\n",
2659 (unsigned long long)fs_info->total_ref_cache_size);
31153d81 2660 }
bcc63abb 2661
5d4f98a2
YZ
2662 free_extent_buffer(fs_info->extent_root->node);
2663 free_extent_buffer(fs_info->extent_root->commit_root);
2664 free_extent_buffer(fs_info->tree_root->node);
2665 free_extent_buffer(fs_info->tree_root->commit_root);
2666 free_extent_buffer(root->fs_info->chunk_root->node);
2667 free_extent_buffer(root->fs_info->chunk_root->commit_root);
2668 free_extent_buffer(root->fs_info->dev_root->node);
2669 free_extent_buffer(root->fs_info->dev_root->commit_root);
2670 free_extent_buffer(root->fs_info->csum_root->node);
2671 free_extent_buffer(root->fs_info->csum_root->commit_root);
d20f7043 2672
9078a3e1 2673 btrfs_free_block_groups(root->fs_info);
d10c5f31 2674
c146afad 2675 del_fs_roots(fs_info);
d10c5f31 2676
c146afad 2677 iput(fs_info->btree_inode);
9ad6b7bc 2678
61d92c32 2679 btrfs_stop_workers(&fs_info->generic_worker);
247e743c 2680 btrfs_stop_workers(&fs_info->fixup_workers);
771ed689 2681 btrfs_stop_workers(&fs_info->delalloc_workers);
8b712842
CM
2682 btrfs_stop_workers(&fs_info->workers);
2683 btrfs_stop_workers(&fs_info->endio_workers);
d20f7043 2684 btrfs_stop_workers(&fs_info->endio_meta_workers);
cad321ad 2685 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
e6dcd2dc 2686 btrfs_stop_workers(&fs_info->endio_write_workers);
0cb59c99 2687 btrfs_stop_workers(&fs_info->endio_freespace_worker);
1cc127b5 2688 btrfs_stop_workers(&fs_info->submit_workers);
d6bfde87 2689
dfe25020 2690 btrfs_close_devices(fs_info->fs_devices);
0b86a832 2691 btrfs_mapping_tree_free(&fs_info->mapping_tree);
b248a415 2692
04160088 2693 bdi_destroy(&fs_info->bdi);
76dda93c 2694 cleanup_srcu_struct(&fs_info->subvol_srcu);
0b86a832 2695
0f7d52f4 2696 kfree(fs_info->extent_root);
0f7d52f4 2697 kfree(fs_info->tree_root);
0b86a832
CM
2698 kfree(fs_info->chunk_root);
2699 kfree(fs_info->dev_root);
d20f7043 2700 kfree(fs_info->csum_root);
83a4d548
LZ
2701 kfree(fs_info);
2702
eb60ceac
CM
2703 return 0;
2704}
2705
1259ab75 2706int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
5f39d397 2707{
1259ab75 2708 int ret;
810191ff 2709 struct inode *btree_inode = buf->first_page->mapping->host;
1259ab75 2710
2ac55d41
JB
2711 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf,
2712 NULL);
1259ab75
CM
2713 if (!ret)
2714 return ret;
2715
2716 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
2717 parent_transid);
2718 return !ret;
5f39d397
CM
2719}
2720
2721int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
ccd467d6 2722{
810191ff 2723 struct inode *btree_inode = buf->first_page->mapping->host;
d1310b2e 2724 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
5f39d397
CM
2725 buf);
2726}
6702ed49 2727
5f39d397
CM
2728void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
2729{
810191ff 2730 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
5f39d397
CM
2731 u64 transid = btrfs_header_generation(buf);
2732 struct inode *btree_inode = root->fs_info->btree_inode;
b9473439 2733 int was_dirty;
b4ce94de 2734
b9447ef8 2735 btrfs_assert_tree_locked(buf);
ccd467d6 2736 if (transid != root->fs_info->generation) {
d397712b
CM
2737 printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
2738 "found %llu running %llu\n",
db94535d 2739 (unsigned long long)buf->start,
d397712b
CM
2740 (unsigned long long)transid,
2741 (unsigned long long)root->fs_info->generation);
ccd467d6
CM
2742 WARN_ON(1);
2743 }
b9473439
CM
2744 was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
2745 buf);
2746 if (!was_dirty) {
2747 spin_lock(&root->fs_info->delalloc_lock);
2748 root->fs_info->dirty_metadata_bytes += buf->len;
2749 spin_unlock(&root->fs_info->delalloc_lock);
2750 }
eb60ceac
CM
2751}
2752
d3c2fdcf 2753void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
35b7e476 2754{
188de649
CM
2755 /*
2756 * looks as though older kernels can get into trouble with
2757 * this code, they end up stuck in balance_dirty_pages forever
2758 */
d6bfde87 2759 u64 num_dirty;
771ed689 2760 unsigned long thresh = 32 * 1024 * 1024;
d6bfde87 2761
6933c02e 2762 if (current->flags & PF_MEMALLOC)
d6bfde87
CM
2763 return;
2764
585ad2c3
CM
2765 num_dirty = root->fs_info->dirty_metadata_bytes;
2766
d6bfde87
CM
2767 if (num_dirty > thresh) {
2768 balance_dirty_pages_ratelimited_nr(
d7fc640e 2769 root->fs_info->btree_inode->i_mapping, 1);
d6bfde87 2770 }
188de649 2771 return;
35b7e476 2772}
6b80053d 2773
ca7a79ad 2774int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
6b80053d 2775{
810191ff 2776 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
ce9adaa5 2777 int ret;
ca7a79ad 2778 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
d397712b 2779 if (ret == 0)
b4ce94de 2780 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
ce9adaa5 2781 return ret;
6b80053d 2782}
0da5468f 2783
4bef0848
CM
2784int btree_lock_page_hook(struct page *page)
2785{
2786 struct inode *inode = page->mapping->host;
b9473439 2787 struct btrfs_root *root = BTRFS_I(inode)->root;
4bef0848
CM
2788 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2789 struct extent_buffer *eb;
2790 unsigned long len;
2791 u64 bytenr = page_offset(page);
2792
2793 if (page->private == EXTENT_PAGE_PRIVATE)
2794 goto out;
2795
2796 len = page->private >> 2;
2797 eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
2798 if (!eb)
2799 goto out;
2800
2801 btrfs_tree_lock(eb);
4bef0848 2802 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
b9473439
CM
2803
2804 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
2805 spin_lock(&root->fs_info->delalloc_lock);
2806 if (root->fs_info->dirty_metadata_bytes >= eb->len)
2807 root->fs_info->dirty_metadata_bytes -= eb->len;
2808 else
2809 WARN_ON(1);
2810 spin_unlock(&root->fs_info->delalloc_lock);
2811 }
2812
4bef0848
CM
2813 btrfs_tree_unlock(eb);
2814 free_extent_buffer(eb);
2815out:
2816 lock_page(page);
2817 return 0;
2818}
2819
acce952b 2820static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
2821 int read_only)
2822{
2823 if (read_only)
2824 return;
2825
2826 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2827 printk(KERN_WARNING "warning: mount fs with errors, "
2828 "running btrfsck is recommended\n");
2829}
2830
2831int btrfs_error_commit_super(struct btrfs_root *root)
2832{
2833 int ret;
2834
2835 mutex_lock(&root->fs_info->cleaner_mutex);
2836 btrfs_run_delayed_iputs(root);
2837 mutex_unlock(&root->fs_info->cleaner_mutex);
2838
2839 down_write(&root->fs_info->cleanup_work_sem);
2840 up_write(&root->fs_info->cleanup_work_sem);
2841
2842 /* cleanup FS via transaction */
2843 btrfs_cleanup_transaction(root);
2844
2845 ret = write_ctree_super(NULL, root, 0);
2846
2847 return ret;
2848}
2849
2850static int btrfs_destroy_ordered_operations(struct btrfs_root *root)
2851{
2852 struct btrfs_inode *btrfs_inode;
2853 struct list_head splice;
2854
2855 INIT_LIST_HEAD(&splice);
2856
2857 mutex_lock(&root->fs_info->ordered_operations_mutex);
2858 spin_lock(&root->fs_info->ordered_extent_lock);
2859
2860 list_splice_init(&root->fs_info->ordered_operations, &splice);
2861 while (!list_empty(&splice)) {
2862 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
2863 ordered_operations);
2864
2865 list_del_init(&btrfs_inode->ordered_operations);
2866
2867 btrfs_invalidate_inodes(btrfs_inode->root);
2868 }
2869
2870 spin_unlock(&root->fs_info->ordered_extent_lock);
2871 mutex_unlock(&root->fs_info->ordered_operations_mutex);
2872
2873 return 0;
2874}
2875
2876static int btrfs_destroy_ordered_extents(struct btrfs_root *root)
2877{
2878 struct list_head splice;
2879 struct btrfs_ordered_extent *ordered;
2880 struct inode *inode;
2881
2882 INIT_LIST_HEAD(&splice);
2883
2884 spin_lock(&root->fs_info->ordered_extent_lock);
2885
2886 list_splice_init(&root->fs_info->ordered_extents, &splice);
2887 while (!list_empty(&splice)) {
2888 ordered = list_entry(splice.next, struct btrfs_ordered_extent,
2889 root_extent_list);
2890
2891 list_del_init(&ordered->root_extent_list);
2892 atomic_inc(&ordered->refs);
2893
2894 /* the inode may be getting freed (in sys_unlink path). */
2895 inode = igrab(ordered->inode);
2896
2897 spin_unlock(&root->fs_info->ordered_extent_lock);
2898 if (inode)
2899 iput(inode);
2900
2901 atomic_set(&ordered->refs, 1);
2902 btrfs_put_ordered_extent(ordered);
2903
2904 spin_lock(&root->fs_info->ordered_extent_lock);
2905 }
2906
2907 spin_unlock(&root->fs_info->ordered_extent_lock);
2908
2909 return 0;
2910}
2911
2912static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
2913 struct btrfs_root *root)
2914{
2915 struct rb_node *node;
2916 struct btrfs_delayed_ref_root *delayed_refs;
2917 struct btrfs_delayed_ref_node *ref;
2918 int ret = 0;
2919
2920 delayed_refs = &trans->delayed_refs;
2921
2922 spin_lock(&delayed_refs->lock);
2923 if (delayed_refs->num_entries == 0) {
2924 printk(KERN_INFO "delayed_refs has NO entry\n");
2925 return ret;
2926 }
2927
2928 node = rb_first(&delayed_refs->root);
2929 while (node) {
2930 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2931 node = rb_next(node);
2932
2933 ref->in_tree = 0;
2934 rb_erase(&ref->rb_node, &delayed_refs->root);
2935 delayed_refs->num_entries--;
2936
2937 atomic_set(&ref->refs, 1);
2938 if (btrfs_delayed_ref_is_head(ref)) {
2939 struct btrfs_delayed_ref_head *head;
2940
2941 head = btrfs_delayed_node_to_head(ref);
2942 mutex_lock(&head->mutex);
2943 kfree(head->extent_op);
2944 delayed_refs->num_heads--;
2945 if (list_empty(&head->cluster))
2946 delayed_refs->num_heads_ready--;
2947 list_del_init(&head->cluster);
2948 mutex_unlock(&head->mutex);
2949 }
2950
2951 spin_unlock(&delayed_refs->lock);
2952 btrfs_put_delayed_ref(ref);
2953
2954 cond_resched();
2955 spin_lock(&delayed_refs->lock);
2956 }
2957
2958 spin_unlock(&delayed_refs->lock);
2959
2960 return ret;
2961}
2962
2963static int btrfs_destroy_pending_snapshots(struct btrfs_transaction *t)
2964{
2965 struct btrfs_pending_snapshot *snapshot;
2966 struct list_head splice;
2967
2968 INIT_LIST_HEAD(&splice);
2969
2970 list_splice_init(&t->pending_snapshots, &splice);
2971
2972 while (!list_empty(&splice)) {
2973 snapshot = list_entry(splice.next,
2974 struct btrfs_pending_snapshot,
2975 list);
2976
2977 list_del_init(&snapshot->list);
2978
2979 kfree(snapshot);
2980 }
2981
2982 return 0;
2983}
2984
2985static int btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
2986{
2987 struct btrfs_inode *btrfs_inode;
2988 struct list_head splice;
2989
2990 INIT_LIST_HEAD(&splice);
2991
2992 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
2993
2994 spin_lock(&root->fs_info->delalloc_lock);
2995
2996 while (!list_empty(&splice)) {
2997 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
2998 delalloc_inodes);
2999
3000 list_del_init(&btrfs_inode->delalloc_inodes);
3001
3002 btrfs_invalidate_inodes(btrfs_inode->root);
3003 }
3004
3005 spin_unlock(&root->fs_info->delalloc_lock);
3006
3007 return 0;
3008}
3009
3010static int btrfs_destroy_marked_extents(struct btrfs_root *root,
3011 struct extent_io_tree *dirty_pages,
3012 int mark)
3013{
3014 int ret;
3015 struct page *page;
3016 struct inode *btree_inode = root->fs_info->btree_inode;
3017 struct extent_buffer *eb;
3018 u64 start = 0;
3019 u64 end;
3020 u64 offset;
3021 unsigned long index;
3022
3023 while (1) {
3024 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
3025 mark);
3026 if (ret)
3027 break;
3028
3029 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
3030 while (start <= end) {
3031 index = start >> PAGE_CACHE_SHIFT;
3032 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
3033 page = find_get_page(btree_inode->i_mapping, index);
3034 if (!page)
3035 continue;
3036 offset = page_offset(page);
3037
3038 spin_lock(&dirty_pages->buffer_lock);
3039 eb = radix_tree_lookup(
3040 &(&BTRFS_I(page->mapping->host)->io_tree)->buffer,
3041 offset >> PAGE_CACHE_SHIFT);
3042 spin_unlock(&dirty_pages->buffer_lock);
3043 if (eb) {
3044 ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY,
3045 &eb->bflags);
3046 atomic_set(&eb->refs, 1);
3047 }
3048 if (PageWriteback(page))
3049 end_page_writeback(page);
3050
3051 lock_page(page);
3052 if (PageDirty(page)) {
3053 clear_page_dirty_for_io(page);
3054 spin_lock_irq(&page->mapping->tree_lock);
3055 radix_tree_tag_clear(&page->mapping->page_tree,
3056 page_index(page),
3057 PAGECACHE_TAG_DIRTY);
3058 spin_unlock_irq(&page->mapping->tree_lock);
3059 }
3060
3061 page->mapping->a_ops->invalidatepage(page, 0);
3062 unlock_page(page);
3063 }
3064 }
3065
3066 return ret;
3067}
3068
3069static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
3070 struct extent_io_tree *pinned_extents)
3071{
3072 struct extent_io_tree *unpin;
3073 u64 start;
3074 u64 end;
3075 int ret;
3076
3077 unpin = pinned_extents;
3078 while (1) {
3079 ret = find_first_extent_bit(unpin, 0, &start, &end,
3080 EXTENT_DIRTY);
3081 if (ret)
3082 break;
3083
3084 /* opt_discard */
5378e607
LD
3085 if (btrfs_test_opt(root, DISCARD))
3086 ret = btrfs_error_discard_extent(root, start,
3087 end + 1 - start,
3088 NULL);
acce952b 3089
3090 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3091 btrfs_error_unpin_extent_range(root, start, end);
3092 cond_resched();
3093 }
3094
3095 return 0;
3096}
3097
3098static int btrfs_cleanup_transaction(struct btrfs_root *root)
3099{
3100 struct btrfs_transaction *t;
3101 LIST_HEAD(list);
3102
3103 WARN_ON(1);
3104
3105 mutex_lock(&root->fs_info->trans_mutex);
3106 mutex_lock(&root->fs_info->transaction_kthread_mutex);
3107
3108 list_splice_init(&root->fs_info->trans_list, &list);
3109 while (!list_empty(&list)) {
3110 t = list_entry(list.next, struct btrfs_transaction, list);
3111 if (!t)
3112 break;
3113
3114 btrfs_destroy_ordered_operations(root);
3115
3116 btrfs_destroy_ordered_extents(root);
3117
3118 btrfs_destroy_delayed_refs(t, root);
3119
3120 btrfs_block_rsv_release(root,
3121 &root->fs_info->trans_block_rsv,
3122 t->dirty_pages.dirty_bytes);
3123
3124 /* FIXME: cleanup wait for commit */
3125 t->in_commit = 1;
3126 t->blocked = 1;
3127 if (waitqueue_active(&root->fs_info->transaction_blocked_wait))
3128 wake_up(&root->fs_info->transaction_blocked_wait);
3129
3130 t->blocked = 0;
3131 if (waitqueue_active(&root->fs_info->transaction_wait))
3132 wake_up(&root->fs_info->transaction_wait);
3133 mutex_unlock(&root->fs_info->trans_mutex);
3134
3135 mutex_lock(&root->fs_info->trans_mutex);
3136 t->commit_done = 1;
3137 if (waitqueue_active(&t->commit_wait))
3138 wake_up(&t->commit_wait);
3139 mutex_unlock(&root->fs_info->trans_mutex);
3140
3141 mutex_lock(&root->fs_info->trans_mutex);
3142
3143 btrfs_destroy_pending_snapshots(t);
3144
3145 btrfs_destroy_delalloc_inodes(root);
3146
3147 spin_lock(&root->fs_info->new_trans_lock);
3148 root->fs_info->running_transaction = NULL;
3149 spin_unlock(&root->fs_info->new_trans_lock);
3150
3151 btrfs_destroy_marked_extents(root, &t->dirty_pages,
3152 EXTENT_DIRTY);
3153
3154 btrfs_destroy_pinned_extent(root,
3155 root->fs_info->pinned_extents);
3156
13c5a93e 3157 atomic_set(&t->use_count, 0);
acce952b 3158 list_del_init(&t->list);
3159 memset(t, 0, sizeof(*t));
3160 kmem_cache_free(btrfs_transaction_cachep, t);
3161 }
3162
3163 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
3164 mutex_unlock(&root->fs_info->trans_mutex);
3165
3166 return 0;
3167}
3168
d1310b2e 3169static struct extent_io_ops btree_extent_io_ops = {
4bef0848 3170 .write_cache_pages_lock_hook = btree_lock_page_hook,
ce9adaa5 3171 .readpage_end_io_hook = btree_readpage_end_io_hook,
0b86a832 3172 .submit_bio_hook = btree_submit_bio_hook,
239b14b3
CM
3173 /* note we're sharing with inode.c for the merge bio hook */
3174 .merge_bio_hook = btrfs_merge_bio_hook,
0da5468f 3175};