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