Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[deliverable/linux.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,
5a92bc88 850#ifdef CONFIG_MIGRATION
784b4e29 851 .migratepage = btree_migratepage,
5a92bc88 852#endif
d98237b3
CM
853};
854
ca7a79ad
CM
855int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
856 u64 parent_transid)
090d1875 857{
5f39d397
CM
858 struct extent_buffer *buf = NULL;
859 struct inode *btree_inode = root->fs_info->btree_inode;
de428b63 860 int ret = 0;
090d1875 861
db94535d 862 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5f39d397 863 if (!buf)
090d1875 864 return 0;
d1310b2e 865 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
f188591e 866 buf, 0, 0, btree_get_extent, 0);
5f39d397 867 free_extent_buffer(buf);
de428b63 868 return ret;
090d1875
CM
869}
870
0999df54
CM
871struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
872 u64 bytenr, u32 blocksize)
873{
874 struct inode *btree_inode = root->fs_info->btree_inode;
875 struct extent_buffer *eb;
876 eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
877 bytenr, blocksize, GFP_NOFS);
878 return eb;
879}
880
881struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
882 u64 bytenr, u32 blocksize)
883{
884 struct inode *btree_inode = root->fs_info->btree_inode;
885 struct extent_buffer *eb;
886
887 eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
888 bytenr, blocksize, NULL, GFP_NOFS);
889 return eb;
890}
891
892
e02119d5
CM
893int btrfs_write_tree_block(struct extent_buffer *buf)
894{
8aa38c31
CH
895 return filemap_fdatawrite_range(buf->first_page->mapping, buf->start,
896 buf->start + buf->len - 1);
e02119d5
CM
897}
898
899int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
900{
8aa38c31
CH
901 return filemap_fdatawait_range(buf->first_page->mapping,
902 buf->start, buf->start + buf->len - 1);
e02119d5
CM
903}
904
0999df54 905struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
ca7a79ad 906 u32 blocksize, u64 parent_transid)
0999df54
CM
907{
908 struct extent_buffer *buf = NULL;
0999df54
CM
909 int ret;
910
0999df54
CM
911 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
912 if (!buf)
913 return NULL;
0999df54 914
ca7a79ad 915 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
ce9adaa5 916
d397712b 917 if (ret == 0)
b4ce94de 918 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
5f39d397 919 return buf;
ce9adaa5 920
eb60ceac
CM
921}
922
e089f05c 923int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 924 struct extent_buffer *buf)
ed2ff2cb 925{
5f39d397 926 struct inode *btree_inode = root->fs_info->btree_inode;
55c69072 927 if (btrfs_header_generation(buf) ==
925baedd 928 root->fs_info->running_transaction->transid) {
b9447ef8 929 btrfs_assert_tree_locked(buf);
b4ce94de 930
b9473439
CM
931 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
932 spin_lock(&root->fs_info->delalloc_lock);
933 if (root->fs_info->dirty_metadata_bytes >= buf->len)
934 root->fs_info->dirty_metadata_bytes -= buf->len;
935 else
936 WARN_ON(1);
937 spin_unlock(&root->fs_info->delalloc_lock);
938 }
b4ce94de 939
b9473439
CM
940 /* ugh, clear_extent_buffer_dirty needs to lock the page */
941 btrfs_set_lock_blocking(buf);
d1310b2e 942 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
55c69072 943 buf);
925baedd 944 }
5f39d397
CM
945 return 0;
946}
947
db94535d 948static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
87ee04eb 949 u32 stripesize, struct btrfs_root *root,
9f5fae2f 950 struct btrfs_fs_info *fs_info,
e20d96d6 951 u64 objectid)
d97e63b6 952{
cfaa7295 953 root->node = NULL;
a28ec197 954 root->commit_root = NULL;
db94535d
CM
955 root->sectorsize = sectorsize;
956 root->nodesize = nodesize;
957 root->leafsize = leafsize;
87ee04eb 958 root->stripesize = stripesize;
123abc88 959 root->ref_cows = 0;
0b86a832 960 root->track_dirty = 0;
c71bf099 961 root->in_radix = 0;
d68fc57b
YZ
962 root->orphan_item_inserted = 0;
963 root->orphan_cleanup_state = 0;
0b86a832 964
9f5fae2f 965 root->fs_info = fs_info;
0f7d52f4
CM
966 root->objectid = objectid;
967 root->last_trans = 0;
13a8a7c8 968 root->highest_objectid = 0;
58176a96 969 root->name = NULL;
4313b399 970 root->in_sysfs = 0;
6bef4d31 971 root->inode_tree = RB_ROOT;
f0486c68 972 root->block_rsv = NULL;
d68fc57b 973 root->orphan_block_rsv = NULL;
0b86a832
CM
974
975 INIT_LIST_HEAD(&root->dirty_list);
7b128766 976 INIT_LIST_HEAD(&root->orphan_list);
5d4f98a2 977 INIT_LIST_HEAD(&root->root_list);
925baedd 978 spin_lock_init(&root->node_lock);
d68fc57b 979 spin_lock_init(&root->orphan_lock);
5d4f98a2 980 spin_lock_init(&root->inode_lock);
f0486c68 981 spin_lock_init(&root->accounting_lock);
a2135011 982 mutex_init(&root->objectid_mutex);
e02119d5 983 mutex_init(&root->log_mutex);
7237f183
YZ
984 init_waitqueue_head(&root->log_writer_wait);
985 init_waitqueue_head(&root->log_commit_wait[0]);
986 init_waitqueue_head(&root->log_commit_wait[1]);
987 atomic_set(&root->log_commit[0], 0);
988 atomic_set(&root->log_commit[1], 0);
989 atomic_set(&root->log_writers, 0);
990 root->log_batch = 0;
991 root->log_transid = 0;
257c62e1 992 root->last_log_commit = 0;
d0c803c4
CM
993 extent_io_tree_init(&root->dirty_log_pages,
994 fs_info->btree_inode->i_mapping, GFP_NOFS);
017e5369 995
3768f368
CM
996 memset(&root->root_key, 0, sizeof(root->root_key));
997 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 998 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
58176a96 999 memset(&root->root_kobj, 0, sizeof(root->root_kobj));
3f157a2f 1000 root->defrag_trans_start = fs_info->generation;
58176a96 1001 init_completion(&root->kobj_unregister);
6702ed49 1002 root->defrag_running = 0;
4d775673 1003 root->root_key.objectid = objectid;
3394e160
CM
1004 root->anon_super.s_root = NULL;
1005 root->anon_super.s_dev = 0;
1006 INIT_LIST_HEAD(&root->anon_super.s_list);
1007 INIT_LIST_HEAD(&root->anon_super.s_instances);
1008 init_rwsem(&root->anon_super.s_umount);
1009
3768f368
CM
1010 return 0;
1011}
1012
db94535d 1013static int find_and_setup_root(struct btrfs_root *tree_root,
9f5fae2f
CM
1014 struct btrfs_fs_info *fs_info,
1015 u64 objectid,
e20d96d6 1016 struct btrfs_root *root)
3768f368
CM
1017{
1018 int ret;
db94535d 1019 u32 blocksize;
84234f3a 1020 u64 generation;
3768f368 1021
db94535d 1022 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
1023 tree_root->sectorsize, tree_root->stripesize,
1024 root, fs_info, objectid);
3768f368
CM
1025 ret = btrfs_find_last_root(tree_root, objectid,
1026 &root->root_item, &root->root_key);
4df27c4d
YZ
1027 if (ret > 0)
1028 return -ENOENT;
3768f368
CM
1029 BUG_ON(ret);
1030
84234f3a 1031 generation = btrfs_root_generation(&root->root_item);
db94535d
CM
1032 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1033 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
84234f3a 1034 blocksize, generation);
68433b73
CM
1035 if (!root->node || !btrfs_buffer_uptodate(root->node, generation)) {
1036 free_extent_buffer(root->node);
1037 return -EIO;
1038 }
4df27c4d 1039 root->commit_root = btrfs_root_node(root);
d97e63b6
CM
1040 return 0;
1041}
1042
7237f183
YZ
1043static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1044 struct btrfs_fs_info *fs_info)
0f7d52f4
CM
1045{
1046 struct btrfs_root *root;
1047 struct btrfs_root *tree_root = fs_info->tree_root;
7237f183 1048 struct extent_buffer *leaf;
e02119d5
CM
1049
1050 root = kzalloc(sizeof(*root), GFP_NOFS);
1051 if (!root)
7237f183 1052 return ERR_PTR(-ENOMEM);
e02119d5
CM
1053
1054 __setup_root(tree_root->nodesize, tree_root->leafsize,
1055 tree_root->sectorsize, tree_root->stripesize,
1056 root, fs_info, BTRFS_TREE_LOG_OBJECTID);
1057
1058 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1059 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1060 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
7237f183
YZ
1061 /*
1062 * log trees do not get reference counted because they go away
1063 * before a real commit is actually done. They do store pointers
1064 * to file data extents, and those reference counts still get
1065 * updated (along with back refs to the log tree).
1066 */
e02119d5
CM
1067 root->ref_cows = 0;
1068
5d4f98a2
YZ
1069 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
1070 BTRFS_TREE_LOG_OBJECTID, NULL, 0, 0, 0);
7237f183
YZ
1071 if (IS_ERR(leaf)) {
1072 kfree(root);
1073 return ERR_CAST(leaf);
1074 }
e02119d5 1075
5d4f98a2
YZ
1076 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
1077 btrfs_set_header_bytenr(leaf, leaf->start);
1078 btrfs_set_header_generation(leaf, trans->transid);
1079 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1080 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
7237f183 1081 root->node = leaf;
e02119d5
CM
1082
1083 write_extent_buffer(root->node, root->fs_info->fsid,
1084 (unsigned long)btrfs_header_fsid(root->node),
1085 BTRFS_FSID_SIZE);
1086 btrfs_mark_buffer_dirty(root->node);
1087 btrfs_tree_unlock(root->node);
7237f183
YZ
1088 return root;
1089}
1090
1091int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1092 struct btrfs_fs_info *fs_info)
1093{
1094 struct btrfs_root *log_root;
1095
1096 log_root = alloc_log_tree(trans, fs_info);
1097 if (IS_ERR(log_root))
1098 return PTR_ERR(log_root);
1099 WARN_ON(fs_info->log_root_tree);
1100 fs_info->log_root_tree = log_root;
1101 return 0;
1102}
1103
1104int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1105 struct btrfs_root *root)
1106{
1107 struct btrfs_root *log_root;
1108 struct btrfs_inode_item *inode_item;
1109
1110 log_root = alloc_log_tree(trans, root->fs_info);
1111 if (IS_ERR(log_root))
1112 return PTR_ERR(log_root);
1113
1114 log_root->last_trans = trans->transid;
1115 log_root->root_key.offset = root->root_key.objectid;
1116
1117 inode_item = &log_root->root_item.inode;
1118 inode_item->generation = cpu_to_le64(1);
1119 inode_item->size = cpu_to_le64(3);
1120 inode_item->nlink = cpu_to_le32(1);
1121 inode_item->nbytes = cpu_to_le64(root->leafsize);
1122 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
1123
5d4f98a2 1124 btrfs_set_root_node(&log_root->root_item, log_root->node);
7237f183
YZ
1125
1126 WARN_ON(root->log_root);
1127 root->log_root = log_root;
1128 root->log_transid = 0;
257c62e1 1129 root->last_log_commit = 0;
e02119d5
CM
1130 return 0;
1131}
1132
1133struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1134 struct btrfs_key *location)
1135{
1136 struct btrfs_root *root;
1137 struct btrfs_fs_info *fs_info = tree_root->fs_info;
0f7d52f4 1138 struct btrfs_path *path;
5f39d397 1139 struct extent_buffer *l;
84234f3a 1140 u64 generation;
db94535d 1141 u32 blocksize;
0f7d52f4
CM
1142 int ret = 0;
1143
5eda7b5e 1144 root = kzalloc(sizeof(*root), GFP_NOFS);
0cf6c620 1145 if (!root)
0f7d52f4 1146 return ERR_PTR(-ENOMEM);
0f7d52f4 1147 if (location->offset == (u64)-1) {
db94535d 1148 ret = find_and_setup_root(tree_root, fs_info,
0f7d52f4
CM
1149 location->objectid, root);
1150 if (ret) {
0f7d52f4
CM
1151 kfree(root);
1152 return ERR_PTR(ret);
1153 }
13a8a7c8 1154 goto out;
0f7d52f4
CM
1155 }
1156
db94535d 1157 __setup_root(tree_root->nodesize, tree_root->leafsize,
87ee04eb
CM
1158 tree_root->sectorsize, tree_root->stripesize,
1159 root, fs_info, location->objectid);
0f7d52f4
CM
1160
1161 path = btrfs_alloc_path();
1162 BUG_ON(!path);
1163 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
13a8a7c8
YZ
1164 if (ret == 0) {
1165 l = path->nodes[0];
1166 read_extent_buffer(l, &root->root_item,
1167 btrfs_item_ptr_offset(l, path->slots[0]),
1168 sizeof(root->root_item));
1169 memcpy(&root->root_key, location, sizeof(*location));
0f7d52f4 1170 }
0f7d52f4
CM
1171 btrfs_free_path(path);
1172 if (ret) {
5e540f77 1173 kfree(root);
13a8a7c8
YZ
1174 if (ret > 0)
1175 ret = -ENOENT;
0f7d52f4
CM
1176 return ERR_PTR(ret);
1177 }
13a8a7c8 1178
84234f3a 1179 generation = btrfs_root_generation(&root->root_item);
db94535d
CM
1180 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1181 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
84234f3a 1182 blocksize, generation);
5d4f98a2 1183 root->commit_root = btrfs_root_node(root);
0f7d52f4 1184 BUG_ON(!root->node);
13a8a7c8
YZ
1185out:
1186 if (location->objectid != BTRFS_TREE_LOG_OBJECTID)
e02119d5 1187 root->ref_cows = 1;
13a8a7c8 1188
5eda7b5e
CM
1189 return root;
1190}
1191
dc17ff8f
CM
1192struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1193 u64 root_objectid)
1194{
1195 struct btrfs_root *root;
1196
1197 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
1198 return fs_info->tree_root;
1199 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
1200 return fs_info->extent_root;
1201
1202 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1203 (unsigned long)root_objectid);
1204 return root;
1205}
1206
edbd8d4e
CM
1207struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1208 struct btrfs_key *location)
5eda7b5e
CM
1209{
1210 struct btrfs_root *root;
1211 int ret;
1212
edbd8d4e
CM
1213 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1214 return fs_info->tree_root;
1215 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1216 return fs_info->extent_root;
8f18cf13
CM
1217 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1218 return fs_info->chunk_root;
1219 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1220 return fs_info->dev_root;
0403e47e
YZ
1221 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1222 return fs_info->csum_root;
4df27c4d
YZ
1223again:
1224 spin_lock(&fs_info->fs_roots_radix_lock);
5eda7b5e
CM
1225 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1226 (unsigned long)location->objectid);
4df27c4d 1227 spin_unlock(&fs_info->fs_roots_radix_lock);
5eda7b5e
CM
1228 if (root)
1229 return root;
1230
e02119d5 1231 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
5eda7b5e
CM
1232 if (IS_ERR(root))
1233 return root;
3394e160
CM
1234
1235 set_anon_super(&root->anon_super, NULL);
1236
d68fc57b
YZ
1237 if (btrfs_root_refs(&root->root_item) == 0) {
1238 ret = -ENOENT;
1239 goto fail;
1240 }
1241
1242 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1243 if (ret < 0)
1244 goto fail;
1245 if (ret == 0)
1246 root->orphan_item_inserted = 1;
1247
4df27c4d
YZ
1248 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1249 if (ret)
1250 goto fail;
1251
1252 spin_lock(&fs_info->fs_roots_radix_lock);
2619ba1f
CM
1253 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1254 (unsigned long)root->root_key.objectid,
0f7d52f4 1255 root);
d68fc57b 1256 if (ret == 0)
4df27c4d 1257 root->in_radix = 1;
d68fc57b 1258
4df27c4d
YZ
1259 spin_unlock(&fs_info->fs_roots_radix_lock);
1260 radix_tree_preload_end();
0f7d52f4 1261 if (ret) {
4df27c4d
YZ
1262 if (ret == -EEXIST) {
1263 free_fs_root(root);
1264 goto again;
1265 }
1266 goto fail;
0f7d52f4 1267 }
4df27c4d
YZ
1268
1269 ret = btrfs_find_dead_roots(fs_info->tree_root,
1270 root->root_key.objectid);
1271 WARN_ON(ret);
edbd8d4e 1272 return root;
4df27c4d
YZ
1273fail:
1274 free_fs_root(root);
1275 return ERR_PTR(ret);
edbd8d4e
CM
1276}
1277
1278struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1279 struct btrfs_key *location,
1280 const char *name, int namelen)
1281{
4df27c4d
YZ
1282 return btrfs_read_fs_root_no_name(fs_info, location);
1283#if 0
edbd8d4e
CM
1284 struct btrfs_root *root;
1285 int ret;
1286
1287 root = btrfs_read_fs_root_no_name(fs_info, location);
1288 if (!root)
1289 return NULL;
58176a96 1290
4313b399
CM
1291 if (root->in_sysfs)
1292 return root;
1293
58176a96
JB
1294 ret = btrfs_set_root_name(root, name, namelen);
1295 if (ret) {
5f39d397 1296 free_extent_buffer(root->node);
58176a96
JB
1297 kfree(root);
1298 return ERR_PTR(ret);
1299 }
4df27c4d 1300
58176a96
JB
1301 ret = btrfs_sysfs_add_root(root);
1302 if (ret) {
5f39d397 1303 free_extent_buffer(root->node);
58176a96
JB
1304 kfree(root->name);
1305 kfree(root);
1306 return ERR_PTR(ret);
1307 }
4313b399 1308 root->in_sysfs = 1;
0f7d52f4 1309 return root;
4df27c4d 1310#endif
0f7d52f4 1311}
04160088
CM
1312
1313static int btrfs_congested_fn(void *congested_data, int bdi_bits)
1314{
1315 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
1316 int ret = 0;
04160088
CM
1317 struct btrfs_device *device;
1318 struct backing_dev_info *bdi;
b7967db7 1319
c6e30871 1320 list_for_each_entry(device, &info->fs_devices->devices, dev_list) {
dfe25020
CM
1321 if (!device->bdev)
1322 continue;
04160088
CM
1323 bdi = blk_get_backing_dev_info(device->bdev);
1324 if (bdi && bdi_congested(bdi, bdi_bits)) {
1325 ret = 1;
1326 break;
1327 }
1328 }
1329 return ret;
1330}
1331
ad081f14
JA
1332/*
1333 * If this fails, caller must call bdi_destroy() to get rid of the
1334 * bdi again.
1335 */
04160088
CM
1336static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1337{
ad081f14
JA
1338 int err;
1339
1340 bdi->capabilities = BDI_CAP_MAP_COPY;
e6d086d8 1341 err = bdi_setup_and_register(bdi, "btrfs", BDI_CAP_MAP_COPY);
ad081f14
JA
1342 if (err)
1343 return err;
1344
4575c9cc 1345 bdi->ra_pages = default_backing_dev_info.ra_pages;
04160088
CM
1346 bdi->congested_fn = btrfs_congested_fn;
1347 bdi->congested_data = info;
1348 return 0;
1349}
1350
ce9adaa5
CM
1351static int bio_ready_for_csum(struct bio *bio)
1352{
1353 u64 length = 0;
1354 u64 buf_len = 0;
1355 u64 start = 0;
1356 struct page *page;
1357 struct extent_io_tree *io_tree = NULL;
ce9adaa5
CM
1358 struct bio_vec *bvec;
1359 int i;
1360 int ret;
1361
1362 bio_for_each_segment(bvec, bio, i) {
1363 page = bvec->bv_page;
1364 if (page->private == EXTENT_PAGE_PRIVATE) {
1365 length += bvec->bv_len;
1366 continue;
1367 }
1368 if (!page->private) {
1369 length += bvec->bv_len;
1370 continue;
1371 }
1372 length = bvec->bv_len;
1373 buf_len = page->private >> 2;
1374 start = page_offset(page) + bvec->bv_offset;
1375 io_tree = &BTRFS_I(page->mapping->host)->io_tree;
ce9adaa5
CM
1376 }
1377 /* are we fully contained in this bio? */
1378 if (buf_len <= length)
1379 return 1;
1380
1381 ret = extent_range_uptodate(io_tree, start + length,
1382 start + buf_len - 1);
ce9adaa5
CM
1383 return ret;
1384}
1385
8b712842
CM
1386/*
1387 * called by the kthread helper functions to finally call the bio end_io
1388 * functions. This is where read checksum verification actually happens
1389 */
1390static void end_workqueue_fn(struct btrfs_work *work)
ce9adaa5 1391{
ce9adaa5 1392 struct bio *bio;
8b712842
CM
1393 struct end_io_wq *end_io_wq;
1394 struct btrfs_fs_info *fs_info;
ce9adaa5 1395 int error;
ce9adaa5 1396
8b712842
CM
1397 end_io_wq = container_of(work, struct end_io_wq, work);
1398 bio = end_io_wq->bio;
1399 fs_info = end_io_wq->info;
ce9adaa5 1400
cad321ad 1401 /* metadata bio reads are special because the whole tree block must
8b712842
CM
1402 * be checksummed at once. This makes sure the entire block is in
1403 * ram and up to date before trying to verify things. For
1404 * blocksize <= pagesize, it is basically a noop
1405 */
7b6d91da 1406 if (!(bio->bi_rw & REQ_WRITE) && end_io_wq->metadata &&
cad321ad 1407 !bio_ready_for_csum(bio)) {
d20f7043 1408 btrfs_queue_worker(&fs_info->endio_meta_workers,
8b712842
CM
1409 &end_io_wq->work);
1410 return;
1411 }
1412 error = end_io_wq->error;
1413 bio->bi_private = end_io_wq->private;
1414 bio->bi_end_io = end_io_wq->end_io;
1415 kfree(end_io_wq);
8b712842 1416 bio_endio(bio, error);
44b8bd7e
CM
1417}
1418
a74a4b97
CM
1419static int cleaner_kthread(void *arg)
1420{
1421 struct btrfs_root *root = arg;
1422
1423 do {
a74a4b97 1424 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
76dda93c
YZ
1425
1426 if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
1427 mutex_trylock(&root->fs_info->cleaner_mutex)) {
24bbcf04 1428 btrfs_run_delayed_iputs(root);
76dda93c
YZ
1429 btrfs_clean_old_snapshots(root);
1430 mutex_unlock(&root->fs_info->cleaner_mutex);
1431 }
a74a4b97
CM
1432
1433 if (freezing(current)) {
1434 refrigerator();
1435 } else {
a74a4b97 1436 set_current_state(TASK_INTERRUPTIBLE);
8929ecfa
YZ
1437 if (!kthread_should_stop())
1438 schedule();
a74a4b97
CM
1439 __set_current_state(TASK_RUNNING);
1440 }
1441 } while (!kthread_should_stop());
1442 return 0;
1443}
1444
1445static int transaction_kthread(void *arg)
1446{
1447 struct btrfs_root *root = arg;
1448 struct btrfs_trans_handle *trans;
1449 struct btrfs_transaction *cur;
8929ecfa 1450 u64 transid;
a74a4b97
CM
1451 unsigned long now;
1452 unsigned long delay;
1453 int ret;
1454
1455 do {
a74a4b97
CM
1456 delay = HZ * 30;
1457 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1458 mutex_lock(&root->fs_info->transaction_kthread_mutex);
1459
8929ecfa 1460 spin_lock(&root->fs_info->new_trans_lock);
a74a4b97
CM
1461 cur = root->fs_info->running_transaction;
1462 if (!cur) {
8929ecfa 1463 spin_unlock(&root->fs_info->new_trans_lock);
a74a4b97
CM
1464 goto sleep;
1465 }
31153d81 1466
a74a4b97 1467 now = get_seconds();
8929ecfa
YZ
1468 if (!cur->blocked &&
1469 (now < cur->start_time || now - cur->start_time < 30)) {
1470 spin_unlock(&root->fs_info->new_trans_lock);
a74a4b97
CM
1471 delay = HZ * 5;
1472 goto sleep;
1473 }
8929ecfa
YZ
1474 transid = cur->transid;
1475 spin_unlock(&root->fs_info->new_trans_lock);
56bec294 1476
8929ecfa 1477 trans = btrfs_join_transaction(root, 1);
3612b495 1478 BUG_ON(IS_ERR(trans));
8929ecfa
YZ
1479 if (transid == trans->transid) {
1480 ret = btrfs_commit_transaction(trans, root);
1481 BUG_ON(ret);
1482 } else {
1483 btrfs_end_transaction(trans, root);
1484 }
a74a4b97
CM
1485sleep:
1486 wake_up_process(root->fs_info->cleaner_kthread);
1487 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
1488
1489 if (freezing(current)) {
1490 refrigerator();
1491 } else {
a74a4b97 1492 set_current_state(TASK_INTERRUPTIBLE);
8929ecfa
YZ
1493 if (!kthread_should_stop() &&
1494 !btrfs_transaction_blocked(root->fs_info))
1495 schedule_timeout(delay);
a74a4b97
CM
1496 __set_current_state(TASK_RUNNING);
1497 }
1498 } while (!kthread_should_stop());
1499 return 0;
1500}
1501
8a4b83cc 1502struct btrfs_root *open_ctree(struct super_block *sb,
dfe25020
CM
1503 struct btrfs_fs_devices *fs_devices,
1504 char *options)
2e635a27 1505{
db94535d
CM
1506 u32 sectorsize;
1507 u32 nodesize;
1508 u32 leafsize;
1509 u32 blocksize;
87ee04eb 1510 u32 stripesize;
84234f3a 1511 u64 generation;
f2b636e8 1512 u64 features;
3de4586c 1513 struct btrfs_key location;
a061fc8d 1514 struct buffer_head *bh;
e02119d5 1515 struct btrfs_root *extent_root = kzalloc(sizeof(struct btrfs_root),
e20d96d6 1516 GFP_NOFS);
d20f7043
CM
1517 struct btrfs_root *csum_root = kzalloc(sizeof(struct btrfs_root),
1518 GFP_NOFS);
450ba0ea
JB
1519 struct btrfs_root *tree_root = btrfs_sb(sb);
1520 struct btrfs_fs_info *fs_info = tree_root->fs_info;
e02119d5 1521 struct btrfs_root *chunk_root = kzalloc(sizeof(struct btrfs_root),
0b86a832 1522 GFP_NOFS);
e02119d5 1523 struct btrfs_root *dev_root = kzalloc(sizeof(struct btrfs_root),
0b86a832 1524 GFP_NOFS);
e02119d5
CM
1525 struct btrfs_root *log_tree_root;
1526
eb60ceac 1527 int ret;
e58ca020 1528 int err = -EINVAL;
4543df7e 1529
2c90e5d6 1530 struct btrfs_super_block *disk_super;
8790d502 1531
0463bb4e 1532 if (!extent_root || !tree_root || !fs_info ||
d20f7043 1533 !chunk_root || !dev_root || !csum_root) {
39279cc3
CM
1534 err = -ENOMEM;
1535 goto fail;
1536 }
76dda93c
YZ
1537
1538 ret = init_srcu_struct(&fs_info->subvol_srcu);
1539 if (ret) {
1540 err = ret;
1541 goto fail;
1542 }
1543
1544 ret = setup_bdi(fs_info, &fs_info->bdi);
1545 if (ret) {
1546 err = ret;
1547 goto fail_srcu;
1548 }
1549
1550 fs_info->btree_inode = new_inode(sb);
1551 if (!fs_info->btree_inode) {
1552 err = -ENOMEM;
1553 goto fail_bdi;
1554 }
1555
1556 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
8fd17795 1557 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 1558 INIT_LIST_HEAD(&fs_info->dead_roots);
24bbcf04 1559 INIT_LIST_HEAD(&fs_info->delayed_iputs);
19c00ddc 1560 INIT_LIST_HEAD(&fs_info->hashers);
ea8c2819 1561 INIT_LIST_HEAD(&fs_info->delalloc_inodes);
5a3f23d5 1562 INIT_LIST_HEAD(&fs_info->ordered_operations);
11833d66 1563 INIT_LIST_HEAD(&fs_info->caching_block_groups);
1832a6d5 1564 spin_lock_init(&fs_info->delalloc_lock);
cee36a03 1565 spin_lock_init(&fs_info->new_trans_lock);
31153d81 1566 spin_lock_init(&fs_info->ref_cache_lock);
76dda93c 1567 spin_lock_init(&fs_info->fs_roots_radix_lock);
24bbcf04 1568 spin_lock_init(&fs_info->delayed_iput_lock);
19c00ddc 1569
58176a96 1570 init_completion(&fs_info->kobj_unregister);
9f5fae2f
CM
1571 fs_info->tree_root = tree_root;
1572 fs_info->extent_root = extent_root;
d20f7043 1573 fs_info->csum_root = csum_root;
0b86a832
CM
1574 fs_info->chunk_root = chunk_root;
1575 fs_info->dev_root = dev_root;
8a4b83cc 1576 fs_info->fs_devices = fs_devices;
0b86a832 1577 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
6324fbf3 1578 INIT_LIST_HEAD(&fs_info->space_info);
0b86a832 1579 btrfs_mapping_init(&fs_info->mapping_tree);
f0486c68
YZ
1580 btrfs_init_block_rsv(&fs_info->global_block_rsv);
1581 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv);
1582 btrfs_init_block_rsv(&fs_info->trans_block_rsv);
1583 btrfs_init_block_rsv(&fs_info->chunk_block_rsv);
1584 btrfs_init_block_rsv(&fs_info->empty_block_rsv);
1585 INIT_LIST_HEAD(&fs_info->durable_block_rsv_list);
1586 mutex_init(&fs_info->durable_block_rsv_mutex);
cb03c743 1587 atomic_set(&fs_info->nr_async_submits, 0);
771ed689 1588 atomic_set(&fs_info->async_delalloc_pages, 0);
8c8bee1d 1589 atomic_set(&fs_info->async_submit_draining, 0);
0986fe9e 1590 atomic_set(&fs_info->nr_async_bios, 0);
e20d96d6 1591 fs_info->sb = sb;
6f568d35 1592 fs_info->max_inline = 8192 * 1024;
9ed74f2d 1593 fs_info->metadata_ratio = 0;
c8b97818 1594
b34b086c
CM
1595 fs_info->thread_pool_size = min_t(unsigned long,
1596 num_online_cpus() + 2, 8);
0afbaf8c 1597
3eaa2885
CM
1598 INIT_LIST_HEAD(&fs_info->ordered_extents);
1599 spin_lock_init(&fs_info->ordered_extent_lock);
1600
a061fc8d
CM
1601 sb->s_blocksize = 4096;
1602 sb->s_blocksize_bits = blksize_bits(4096);
32a88aa1 1603 sb->s_bdi = &fs_info->bdi;
a061fc8d 1604
76dda93c
YZ
1605 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
1606 fs_info->btree_inode->i_nlink = 1;
0afbaf8c
CM
1607 /*
1608 * we set the i_size on the btree inode to the max possible int.
1609 * the real end of the address space is determined by all of
1610 * the devices in the system
1611 */
1612 fs_info->btree_inode->i_size = OFFSET_MAX;
d98237b3 1613 fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
04160088
CM
1614 fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
1615
5d4f98a2 1616 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node);
d1310b2e 1617 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
5f39d397
CM
1618 fs_info->btree_inode->i_mapping,
1619 GFP_NOFS);
d1310b2e
CM
1620 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
1621 GFP_NOFS);
1622
1623 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
0da5468f 1624
76dda93c
YZ
1625 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1626 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1627 sizeof(struct btrfs_key));
1628 BTRFS_I(fs_info->btree_inode)->dummy_inode = 1;
c65ddb52 1629 insert_inode_hash(fs_info->btree_inode);
76dda93c 1630
0f9dd46c 1631 spin_lock_init(&fs_info->block_group_cache_lock);
6bef4d31 1632 fs_info->block_group_cache_tree = RB_ROOT;
0f9dd46c 1633
11833d66 1634 extent_io_tree_init(&fs_info->freed_extents[0],
1a5bc167 1635 fs_info->btree_inode->i_mapping, GFP_NOFS);
11833d66
YZ
1636 extent_io_tree_init(&fs_info->freed_extents[1],
1637 fs_info->btree_inode->i_mapping, GFP_NOFS);
1638 fs_info->pinned_extents = &fs_info->freed_extents[0];
e66f709b 1639 fs_info->do_barriers = 1;
e18e4809 1640
39279cc3 1641
79154b1b 1642 mutex_init(&fs_info->trans_mutex);
5a3f23d5 1643 mutex_init(&fs_info->ordered_operations_mutex);
e02119d5 1644 mutex_init(&fs_info->tree_log_mutex);
925baedd 1645 mutex_init(&fs_info->chunk_mutex);
a74a4b97
CM
1646 mutex_init(&fs_info->transaction_kthread_mutex);
1647 mutex_init(&fs_info->cleaner_mutex);
7d9eb12c 1648 mutex_init(&fs_info->volume_mutex);
276e680d 1649 init_rwsem(&fs_info->extent_commit_sem);
c71bf099 1650 init_rwsem(&fs_info->cleanup_work_sem);
76dda93c 1651 init_rwsem(&fs_info->subvol_sem);
fa9c0d79
CM
1652
1653 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
1654 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
1655
e6dcd2dc 1656 init_waitqueue_head(&fs_info->transaction_throttle);
f9295749 1657 init_waitqueue_head(&fs_info->transaction_wait);
bb9c12c9 1658 init_waitqueue_head(&fs_info->transaction_blocked_wait);
4854ddd0 1659 init_waitqueue_head(&fs_info->async_submit_wait);
3768f368 1660
0b86a832 1661 __setup_root(4096, 4096, 4096, 4096, tree_root,
2c90e5d6 1662 fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 1663
a512bbf8 1664 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
20b45077
DY
1665 if (!bh) {
1666 err = -EINVAL;
39279cc3 1667 goto fail_iput;
20b45077 1668 }
39279cc3 1669
a061fc8d 1670 memcpy(&fs_info->super_copy, bh->b_data, sizeof(fs_info->super_copy));
2d69a0f8
YZ
1671 memcpy(&fs_info->super_for_commit, &fs_info->super_copy,
1672 sizeof(fs_info->super_for_commit));
a061fc8d 1673 brelse(bh);
5f39d397 1674
a061fc8d 1675 memcpy(fs_info->fsid, fs_info->super_copy.fsid, BTRFS_FSID_SIZE);
0b86a832 1676
5f39d397 1677 disk_super = &fs_info->super_copy;
0f7d52f4 1678 if (!btrfs_super_root(disk_super))
c6e2bac1 1679 goto fail_iput;
0f7d52f4 1680
acce952b 1681 /* check FS state, whether FS is broken. */
1682 fs_info->fs_state |= btrfs_super_flags(disk_super);
1683
1684 btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY);
1685
2b82032c
YZ
1686 ret = btrfs_parse_options(tree_root, options);
1687 if (ret) {
1688 err = ret;
c6e2bac1 1689 goto fail_iput;
2b82032c 1690 }
dfe25020 1691
f2b636e8
JB
1692 features = btrfs_super_incompat_flags(disk_super) &
1693 ~BTRFS_FEATURE_INCOMPAT_SUPP;
1694 if (features) {
1695 printk(KERN_ERR "BTRFS: couldn't mount because of "
1696 "unsupported optional features (%Lx).\n",
21380931 1697 (unsigned long long)features);
f2b636e8 1698 err = -EINVAL;
c6e2bac1 1699 goto fail_iput;
f2b636e8
JB
1700 }
1701
5d4f98a2 1702 features = btrfs_super_incompat_flags(disk_super);
a6fa6fae
LZ
1703 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
1704 if (tree_root->fs_info->compress_type & BTRFS_COMPRESS_LZO)
1705 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1706 btrfs_set_super_incompat_flags(disk_super, features);
5d4f98a2 1707
f2b636e8
JB
1708 features = btrfs_super_compat_ro_flags(disk_super) &
1709 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
1710 if (!(sb->s_flags & MS_RDONLY) && features) {
1711 printk(KERN_ERR "BTRFS: couldn't mount RDWR because of "
1712 "unsupported option features (%Lx).\n",
21380931 1713 (unsigned long long)features);
f2b636e8 1714 err = -EINVAL;
c6e2bac1 1715 goto fail_iput;
f2b636e8 1716 }
61d92c32
CM
1717
1718 btrfs_init_workers(&fs_info->generic_worker,
1719 "genwork", 1, NULL);
1720
5443be45 1721 btrfs_init_workers(&fs_info->workers, "worker",
61d92c32
CM
1722 fs_info->thread_pool_size,
1723 &fs_info->generic_worker);
c8b97818 1724
771ed689 1725 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
61d92c32
CM
1726 fs_info->thread_pool_size,
1727 &fs_info->generic_worker);
771ed689 1728
5443be45 1729 btrfs_init_workers(&fs_info->submit_workers, "submit",
b720d209 1730 min_t(u64, fs_devices->num_devices,
61d92c32
CM
1731 fs_info->thread_pool_size),
1732 &fs_info->generic_worker);
61b49440
CM
1733
1734 /* a higher idle thresh on the submit workers makes it much more
1735 * likely that bios will be send down in a sane order to the
1736 * devices
1737 */
1738 fs_info->submit_workers.idle_thresh = 64;
53863232 1739
771ed689 1740 fs_info->workers.idle_thresh = 16;
4a69a410 1741 fs_info->workers.ordered = 1;
61b49440 1742
771ed689
CM
1743 fs_info->delalloc_workers.idle_thresh = 2;
1744 fs_info->delalloc_workers.ordered = 1;
1745
61d92c32
CM
1746 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1,
1747 &fs_info->generic_worker);
5443be45 1748 btrfs_init_workers(&fs_info->endio_workers, "endio",
61d92c32
CM
1749 fs_info->thread_pool_size,
1750 &fs_info->generic_worker);
d20f7043 1751 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
61d92c32
CM
1752 fs_info->thread_pool_size,
1753 &fs_info->generic_worker);
cad321ad 1754 btrfs_init_workers(&fs_info->endio_meta_write_workers,
61d92c32
CM
1755 "endio-meta-write", fs_info->thread_pool_size,
1756 &fs_info->generic_worker);
5443be45 1757 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
61d92c32
CM
1758 fs_info->thread_pool_size,
1759 &fs_info->generic_worker);
0cb59c99
JB
1760 btrfs_init_workers(&fs_info->endio_freespace_worker, "freespace-write",
1761 1, &fs_info->generic_worker);
61b49440
CM
1762
1763 /*
1764 * endios are largely parallel and should have a very
1765 * low idle thresh
1766 */
1767 fs_info->endio_workers.idle_thresh = 4;
b51912c9
CM
1768 fs_info->endio_meta_workers.idle_thresh = 4;
1769
9042846b
CM
1770 fs_info->endio_write_workers.idle_thresh = 2;
1771 fs_info->endio_meta_write_workers.idle_thresh = 2;
1772
4543df7e 1773 btrfs_start_workers(&fs_info->workers, 1);
61d92c32 1774 btrfs_start_workers(&fs_info->generic_worker, 1);
1cc127b5 1775 btrfs_start_workers(&fs_info->submit_workers, 1);
771ed689 1776 btrfs_start_workers(&fs_info->delalloc_workers, 1);
247e743c 1777 btrfs_start_workers(&fs_info->fixup_workers, 1);
9042846b
CM
1778 btrfs_start_workers(&fs_info->endio_workers, 1);
1779 btrfs_start_workers(&fs_info->endio_meta_workers, 1);
1780 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1);
1781 btrfs_start_workers(&fs_info->endio_write_workers, 1);
0cb59c99 1782 btrfs_start_workers(&fs_info->endio_freespace_worker, 1);
4543df7e 1783
4575c9cc 1784 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
c8b97818
CM
1785 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
1786 4 * 1024 * 1024 / PAGE_CACHE_SIZE);
4575c9cc 1787
db94535d
CM
1788 nodesize = btrfs_super_nodesize(disk_super);
1789 leafsize = btrfs_super_leafsize(disk_super);
1790 sectorsize = btrfs_super_sectorsize(disk_super);
87ee04eb 1791 stripesize = btrfs_super_stripesize(disk_super);
db94535d
CM
1792 tree_root->nodesize = nodesize;
1793 tree_root->leafsize = leafsize;
1794 tree_root->sectorsize = sectorsize;
87ee04eb 1795 tree_root->stripesize = stripesize;
a061fc8d
CM
1796
1797 sb->s_blocksize = sectorsize;
1798 sb->s_blocksize_bits = blksize_bits(sectorsize);
db94535d 1799
39279cc3
CM
1800 if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
1801 sizeof(disk_super->magic))) {
d397712b 1802 printk(KERN_INFO "btrfs: valid FS not found on %s\n", sb->s_id);
39279cc3
CM
1803 goto fail_sb_buffer;
1804 }
19c00ddc 1805
925baedd 1806 mutex_lock(&fs_info->chunk_mutex);
e4404d6e 1807 ret = btrfs_read_sys_array(tree_root);
925baedd 1808 mutex_unlock(&fs_info->chunk_mutex);
84eed90f 1809 if (ret) {
d397712b
CM
1810 printk(KERN_WARNING "btrfs: failed to read the system "
1811 "array on %s\n", sb->s_id);
5d4f98a2 1812 goto fail_sb_buffer;
84eed90f 1813 }
0b86a832
CM
1814
1815 blocksize = btrfs_level_size(tree_root,
1816 btrfs_super_chunk_root_level(disk_super));
84234f3a 1817 generation = btrfs_super_chunk_root_generation(disk_super);
0b86a832
CM
1818
1819 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1820 chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
1821
1822 chunk_root->node = read_tree_block(chunk_root,
1823 btrfs_super_chunk_root(disk_super),
84234f3a 1824 blocksize, generation);
0b86a832 1825 BUG_ON(!chunk_root->node);
83121942
DW
1826 if (!test_bit(EXTENT_BUFFER_UPTODATE, &chunk_root->node->bflags)) {
1827 printk(KERN_WARNING "btrfs: failed to read chunk root on %s\n",
1828 sb->s_id);
1829 goto fail_chunk_root;
1830 }
5d4f98a2
YZ
1831 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
1832 chunk_root->commit_root = btrfs_root_node(chunk_root);
0b86a832 1833
e17cade2 1834 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
d397712b
CM
1835 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node),
1836 BTRFS_UUID_SIZE);
e17cade2 1837
925baedd 1838 mutex_lock(&fs_info->chunk_mutex);
0b86a832 1839 ret = btrfs_read_chunk_tree(chunk_root);
925baedd 1840 mutex_unlock(&fs_info->chunk_mutex);
2b82032c 1841 if (ret) {
d397712b
CM
1842 printk(KERN_WARNING "btrfs: failed to read chunk tree on %s\n",
1843 sb->s_id);
2b82032c
YZ
1844 goto fail_chunk_root;
1845 }
0b86a832 1846
dfe25020
CM
1847 btrfs_close_extra_devices(fs_devices);
1848
db94535d
CM
1849 blocksize = btrfs_level_size(tree_root,
1850 btrfs_super_root_level(disk_super));
84234f3a 1851 generation = btrfs_super_generation(disk_super);
0b86a832 1852
e20d96d6 1853 tree_root->node = read_tree_block(tree_root,
db94535d 1854 btrfs_super_root(disk_super),
84234f3a 1855 blocksize, generation);
39279cc3 1856 if (!tree_root->node)
2b82032c 1857 goto fail_chunk_root;
83121942
DW
1858 if (!test_bit(EXTENT_BUFFER_UPTODATE, &tree_root->node->bflags)) {
1859 printk(KERN_WARNING "btrfs: failed to read tree root on %s\n",
1860 sb->s_id);
1861 goto fail_tree_root;
1862 }
5d4f98a2
YZ
1863 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
1864 tree_root->commit_root = btrfs_root_node(tree_root);
db94535d
CM
1865
1866 ret = find_and_setup_root(tree_root, fs_info,
e20d96d6 1867 BTRFS_EXTENT_TREE_OBJECTID, extent_root);
0b86a832 1868 if (ret)
39279cc3 1869 goto fail_tree_root;
0b86a832
CM
1870 extent_root->track_dirty = 1;
1871
1872 ret = find_and_setup_root(tree_root, fs_info,
1873 BTRFS_DEV_TREE_OBJECTID, dev_root);
0b86a832
CM
1874 if (ret)
1875 goto fail_extent_root;
5d4f98a2 1876 dev_root->track_dirty = 1;
3768f368 1877
d20f7043
CM
1878 ret = find_and_setup_root(tree_root, fs_info,
1879 BTRFS_CSUM_TREE_OBJECTID, csum_root);
1880 if (ret)
5d4f98a2 1881 goto fail_dev_root;
d20f7043
CM
1882
1883 csum_root->track_dirty = 1;
1884
8929ecfa
YZ
1885 fs_info->generation = generation;
1886 fs_info->last_trans_committed = generation;
1887 fs_info->data_alloc_profile = (u64)-1;
1888 fs_info->metadata_alloc_profile = (u64)-1;
1889 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1890
1b1d1f66
JB
1891 ret = btrfs_read_block_groups(extent_root);
1892 if (ret) {
1893 printk(KERN_ERR "Failed to read block groups: %d\n", ret);
1894 goto fail_block_groups;
1895 }
9078a3e1 1896
a74a4b97
CM
1897 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
1898 "btrfs-cleaner");
57506d50 1899 if (IS_ERR(fs_info->cleaner_kthread))
1b1d1f66 1900 goto fail_block_groups;
a74a4b97
CM
1901
1902 fs_info->transaction_kthread = kthread_run(transaction_kthread,
1903 tree_root,
1904 "btrfs-transaction");
57506d50 1905 if (IS_ERR(fs_info->transaction_kthread))
3f157a2f 1906 goto fail_cleaner;
a74a4b97 1907
c289811c
CM
1908 if (!btrfs_test_opt(tree_root, SSD) &&
1909 !btrfs_test_opt(tree_root, NOSSD) &&
1910 !fs_info->fs_devices->rotating) {
1911 printk(KERN_INFO "Btrfs detected SSD devices, enabling SSD "
1912 "mode\n");
1913 btrfs_set_opt(fs_info->mount_opt, SSD);
1914 }
1915
acce952b 1916 /* do not make disk changes in broken FS */
1917 if (btrfs_super_log_root(disk_super) != 0 &&
1918 !(fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)) {
e02119d5
CM
1919 u64 bytenr = btrfs_super_log_root(disk_super);
1920
7c2ca468 1921 if (fs_devices->rw_devices == 0) {
d397712b
CM
1922 printk(KERN_WARNING "Btrfs log replay required "
1923 "on RO media\n");
7c2ca468
CM
1924 err = -EIO;
1925 goto fail_trans_kthread;
1926 }
e02119d5
CM
1927 blocksize =
1928 btrfs_level_size(tree_root,
1929 btrfs_super_log_root_level(disk_super));
d18a2c44 1930
676e4c86
DC
1931 log_tree_root = kzalloc(sizeof(struct btrfs_root), GFP_NOFS);
1932 if (!log_tree_root) {
1933 err = -ENOMEM;
1934 goto fail_trans_kthread;
1935 }
e02119d5
CM
1936
1937 __setup_root(nodesize, leafsize, sectorsize, stripesize,
1938 log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
1939
1940 log_tree_root->node = read_tree_block(tree_root, bytenr,
84234f3a
YZ
1941 blocksize,
1942 generation + 1);
e02119d5
CM
1943 ret = btrfs_recover_log_trees(log_tree_root);
1944 BUG_ON(ret);
e556ce2c
YZ
1945
1946 if (sb->s_flags & MS_RDONLY) {
1947 ret = btrfs_commit_super(tree_root);
1948 BUG_ON(ret);
1949 }
e02119d5 1950 }
1a40e23b 1951
76dda93c
YZ
1952 ret = btrfs_find_orphan_roots(tree_root);
1953 BUG_ON(ret);
1954
7c2ca468 1955 if (!(sb->s_flags & MS_RDONLY)) {
d68fc57b
YZ
1956 ret = btrfs_cleanup_fs_roots(fs_info);
1957 BUG_ON(ret);
1958
5d4f98a2 1959 ret = btrfs_recover_relocation(tree_root);
d7ce5843
MX
1960 if (ret < 0) {
1961 printk(KERN_WARNING
1962 "btrfs: failed to recover relocation\n");
1963 err = -EINVAL;
1964 goto fail_trans_kthread;
1965 }
7c2ca468 1966 }
1a40e23b 1967
3de4586c
CM
1968 location.objectid = BTRFS_FS_TREE_OBJECTID;
1969 location.type = BTRFS_ROOT_ITEM_KEY;
1970 location.offset = (u64)-1;
1971
3de4586c
CM
1972 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
1973 if (!fs_info->fs_root)
7c2ca468 1974 goto fail_trans_kthread;
3140c9a3
DC
1975 if (IS_ERR(fs_info->fs_root)) {
1976 err = PTR_ERR(fs_info->fs_root);
1977 goto fail_trans_kthread;
1978 }
c289811c 1979
e3acc2a6
JB
1980 if (!(sb->s_flags & MS_RDONLY)) {
1981 down_read(&fs_info->cleanup_work_sem);
1982 btrfs_orphan_cleanup(fs_info->fs_root);
0af3d00b 1983 btrfs_orphan_cleanup(fs_info->tree_root);
e3acc2a6
JB
1984 up_read(&fs_info->cleanup_work_sem);
1985 }
1986
0f7d52f4 1987 return tree_root;
39279cc3 1988
7c2ca468
CM
1989fail_trans_kthread:
1990 kthread_stop(fs_info->transaction_kthread);
3f157a2f 1991fail_cleaner:
a74a4b97 1992 kthread_stop(fs_info->cleaner_kthread);
7c2ca468
CM
1993
1994 /*
1995 * make sure we're done with the btree inode before we stop our
1996 * kthreads
1997 */
1998 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
1999 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
2000
1b1d1f66
JB
2001fail_block_groups:
2002 btrfs_free_block_groups(fs_info);
d20f7043 2003 free_extent_buffer(csum_root->node);
5d4f98a2
YZ
2004 free_extent_buffer(csum_root->commit_root);
2005fail_dev_root:
2006 free_extent_buffer(dev_root->node);
2007 free_extent_buffer(dev_root->commit_root);
0b86a832
CM
2008fail_extent_root:
2009 free_extent_buffer(extent_root->node);
5d4f98a2 2010 free_extent_buffer(extent_root->commit_root);
39279cc3 2011fail_tree_root:
5f39d397 2012 free_extent_buffer(tree_root->node);
5d4f98a2 2013 free_extent_buffer(tree_root->commit_root);
2b82032c
YZ
2014fail_chunk_root:
2015 free_extent_buffer(chunk_root->node);
5d4f98a2 2016 free_extent_buffer(chunk_root->commit_root);
39279cc3 2017fail_sb_buffer:
61d92c32 2018 btrfs_stop_workers(&fs_info->generic_worker);
247e743c 2019 btrfs_stop_workers(&fs_info->fixup_workers);
771ed689 2020 btrfs_stop_workers(&fs_info->delalloc_workers);
8b712842
CM
2021 btrfs_stop_workers(&fs_info->workers);
2022 btrfs_stop_workers(&fs_info->endio_workers);
d20f7043 2023 btrfs_stop_workers(&fs_info->endio_meta_workers);
cad321ad 2024 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
e6dcd2dc 2025 btrfs_stop_workers(&fs_info->endio_write_workers);
0cb59c99 2026 btrfs_stop_workers(&fs_info->endio_freespace_worker);
1cc127b5 2027 btrfs_stop_workers(&fs_info->submit_workers);
4543df7e 2028fail_iput:
7c2ca468 2029 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
4543df7e 2030 iput(fs_info->btree_inode);
7e662854 2031
dfe25020 2032 btrfs_close_devices(fs_info->fs_devices);
84eed90f 2033 btrfs_mapping_tree_free(&fs_info->mapping_tree);
ad081f14 2034fail_bdi:
7e662854 2035 bdi_destroy(&fs_info->bdi);
76dda93c
YZ
2036fail_srcu:
2037 cleanup_srcu_struct(&fs_info->subvol_srcu);
7e662854 2038fail:
39279cc3
CM
2039 kfree(extent_root);
2040 kfree(tree_root);
2041 kfree(fs_info);
83afeac4
JM
2042 kfree(chunk_root);
2043 kfree(dev_root);
d20f7043 2044 kfree(csum_root);
39279cc3 2045 return ERR_PTR(err);
eb60ceac
CM
2046}
2047
f2984462
CM
2048static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
2049{
2050 char b[BDEVNAME_SIZE];
2051
2052 if (uptodate) {
2053 set_buffer_uptodate(bh);
2054 } else {
c3b9a62c 2055 if (printk_ratelimit()) {
f2984462
CM
2056 printk(KERN_WARNING "lost page write due to "
2057 "I/O error on %s\n",
2058 bdevname(bh->b_bdev, b));
2059 }
1259ab75
CM
2060 /* note, we dont' set_buffer_write_io_error because we have
2061 * our own ways of dealing with the IO errors
2062 */
f2984462
CM
2063 clear_buffer_uptodate(bh);
2064 }
2065 unlock_buffer(bh);
2066 put_bh(bh);
2067}
2068
a512bbf8
YZ
2069struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
2070{
2071 struct buffer_head *bh;
2072 struct buffer_head *latest = NULL;
2073 struct btrfs_super_block *super;
2074 int i;
2075 u64 transid = 0;
2076 u64 bytenr;
2077
2078 /* we would like to check all the supers, but that would make
2079 * a btrfs mount succeed after a mkfs from a different FS.
2080 * So, we need to add a special mount option to scan for
2081 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
2082 */
2083 for (i = 0; i < 1; i++) {
2084 bytenr = btrfs_sb_offset(i);
2085 if (bytenr + 4096 >= i_size_read(bdev->bd_inode))
2086 break;
2087 bh = __bread(bdev, bytenr / 4096, 4096);
2088 if (!bh)
2089 continue;
2090
2091 super = (struct btrfs_super_block *)bh->b_data;
2092 if (btrfs_super_bytenr(super) != bytenr ||
2093 strncmp((char *)(&super->magic), BTRFS_MAGIC,
2094 sizeof(super->magic))) {
2095 brelse(bh);
2096 continue;
2097 }
2098
2099 if (!latest || btrfs_super_generation(super) > transid) {
2100 brelse(latest);
2101 latest = bh;
2102 transid = btrfs_super_generation(super);
2103 } else {
2104 brelse(bh);
2105 }
2106 }
2107 return latest;
2108}
2109
4eedeb75
HH
2110/*
2111 * this should be called twice, once with wait == 0 and
2112 * once with wait == 1. When wait == 0 is done, all the buffer heads
2113 * we write are pinned.
2114 *
2115 * They are released when wait == 1 is done.
2116 * max_mirrors must be the same for both runs, and it indicates how
2117 * many supers on this one device should be written.
2118 *
2119 * max_mirrors == 0 means to write them all.
2120 */
a512bbf8
YZ
2121static int write_dev_supers(struct btrfs_device *device,
2122 struct btrfs_super_block *sb,
2123 int do_barriers, int wait, int max_mirrors)
2124{
2125 struct buffer_head *bh;
2126 int i;
2127 int ret;
2128 int errors = 0;
2129 u32 crc;
2130 u64 bytenr;
2131 int last_barrier = 0;
2132
2133 if (max_mirrors == 0)
2134 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
2135
2136 /* make sure only the last submit_bh does a barrier */
2137 if (do_barriers) {
2138 for (i = 0; i < max_mirrors; i++) {
2139 bytenr = btrfs_sb_offset(i);
2140 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
2141 device->total_bytes)
2142 break;
2143 last_barrier = i;
2144 }
2145 }
2146
2147 for (i = 0; i < max_mirrors; i++) {
2148 bytenr = btrfs_sb_offset(i);
2149 if (bytenr + BTRFS_SUPER_INFO_SIZE >= device->total_bytes)
2150 break;
2151
2152 if (wait) {
2153 bh = __find_get_block(device->bdev, bytenr / 4096,
2154 BTRFS_SUPER_INFO_SIZE);
2155 BUG_ON(!bh);
a512bbf8 2156 wait_on_buffer(bh);
4eedeb75
HH
2157 if (!buffer_uptodate(bh))
2158 errors++;
2159
2160 /* drop our reference */
2161 brelse(bh);
2162
2163 /* drop the reference from the wait == 0 run */
2164 brelse(bh);
2165 continue;
a512bbf8
YZ
2166 } else {
2167 btrfs_set_super_bytenr(sb, bytenr);
2168
2169 crc = ~(u32)0;
2170 crc = btrfs_csum_data(NULL, (char *)sb +
2171 BTRFS_CSUM_SIZE, crc,
2172 BTRFS_SUPER_INFO_SIZE -
2173 BTRFS_CSUM_SIZE);
2174 btrfs_csum_final(crc, sb->csum);
2175
4eedeb75
HH
2176 /*
2177 * one reference for us, and we leave it for the
2178 * caller
2179 */
a512bbf8
YZ
2180 bh = __getblk(device->bdev, bytenr / 4096,
2181 BTRFS_SUPER_INFO_SIZE);
2182 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
2183
4eedeb75 2184 /* one reference for submit_bh */
a512bbf8 2185 get_bh(bh);
4eedeb75
HH
2186
2187 set_buffer_uptodate(bh);
a512bbf8
YZ
2188 lock_buffer(bh);
2189 bh->b_end_io = btrfs_end_buffer_write_sync;
2190 }
2191
c3b9a62c
CH
2192 if (i == last_barrier && do_barriers)
2193 ret = submit_bh(WRITE_FLUSH_FUA, bh);
2194 else
ffbd517d 2195 ret = submit_bh(WRITE_SYNC, bh);
a512bbf8 2196
4eedeb75 2197 if (ret)
a512bbf8 2198 errors++;
a512bbf8
YZ
2199 }
2200 return errors < i ? 0 : -1;
2201}
2202
2203int write_all_supers(struct btrfs_root *root, int max_mirrors)
f2984462 2204{
e5e9a520 2205 struct list_head *head;
f2984462 2206 struct btrfs_device *dev;
a061fc8d 2207 struct btrfs_super_block *sb;
f2984462 2208 struct btrfs_dev_item *dev_item;
f2984462
CM
2209 int ret;
2210 int do_barriers;
a236aed1
CM
2211 int max_errors;
2212 int total_errors = 0;
a061fc8d 2213 u64 flags;
f2984462 2214
a236aed1 2215 max_errors = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
f2984462
CM
2216 do_barriers = !btrfs_test_opt(root, NOBARRIER);
2217
a061fc8d
CM
2218 sb = &root->fs_info->super_for_commit;
2219 dev_item = &sb->dev_item;
e5e9a520
CM
2220
2221 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2222 head = &root->fs_info->fs_devices->devices;
c6e30871 2223 list_for_each_entry(dev, head, dev_list) {
dfe25020
CM
2224 if (!dev->bdev) {
2225 total_errors++;
2226 continue;
2227 }
2b82032c 2228 if (!dev->in_fs_metadata || !dev->writeable)
dfe25020
CM
2229 continue;
2230
2b82032c 2231 btrfs_set_stack_device_generation(dev_item, 0);
a061fc8d
CM
2232 btrfs_set_stack_device_type(dev_item, dev->type);
2233 btrfs_set_stack_device_id(dev_item, dev->devid);
2234 btrfs_set_stack_device_total_bytes(dev_item, dev->total_bytes);
2235 btrfs_set_stack_device_bytes_used(dev_item, dev->bytes_used);
2236 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
2237 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
2238 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
2239 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
2b82032c 2240 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
a512bbf8 2241
a061fc8d
CM
2242 flags = btrfs_super_flags(sb);
2243 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
2244
a512bbf8 2245 ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
a236aed1
CM
2246 if (ret)
2247 total_errors++;
f2984462 2248 }
a236aed1 2249 if (total_errors > max_errors) {
d397712b
CM
2250 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2251 total_errors);
a236aed1
CM
2252 BUG();
2253 }
f2984462 2254
a512bbf8 2255 total_errors = 0;
c6e30871 2256 list_for_each_entry(dev, head, dev_list) {
dfe25020
CM
2257 if (!dev->bdev)
2258 continue;
2b82032c 2259 if (!dev->in_fs_metadata || !dev->writeable)
dfe25020
CM
2260 continue;
2261
a512bbf8
YZ
2262 ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
2263 if (ret)
2264 total_errors++;
f2984462 2265 }
e5e9a520 2266 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
a236aed1 2267 if (total_errors > max_errors) {
d397712b
CM
2268 printk(KERN_ERR "btrfs: %d errors while writing supers\n",
2269 total_errors);
a236aed1
CM
2270 BUG();
2271 }
f2984462
CM
2272 return 0;
2273}
2274
a512bbf8
YZ
2275int write_ctree_super(struct btrfs_trans_handle *trans,
2276 struct btrfs_root *root, int max_mirrors)
eb60ceac 2277{
e66f709b 2278 int ret;
5f39d397 2279
a512bbf8 2280 ret = write_all_supers(root, max_mirrors);
5f39d397 2281 return ret;
cfaa7295
CM
2282}
2283
5eda7b5e 2284int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619ba1f 2285{
4df27c4d 2286 spin_lock(&fs_info->fs_roots_radix_lock);
2619ba1f
CM
2287 radix_tree_delete(&fs_info->fs_roots_radix,
2288 (unsigned long)root->root_key.objectid);
4df27c4d 2289 spin_unlock(&fs_info->fs_roots_radix_lock);
76dda93c
YZ
2290
2291 if (btrfs_root_refs(&root->root_item) == 0)
2292 synchronize_srcu(&fs_info->subvol_srcu);
2293
4df27c4d
YZ
2294 free_fs_root(root);
2295 return 0;
2296}
2297
2298static void free_fs_root(struct btrfs_root *root)
2299{
2300 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
3394e160
CM
2301 if (root->anon_super.s_dev) {
2302 down_write(&root->anon_super.s_umount);
2303 kill_anon_super(&root->anon_super);
2304 }
4df27c4d
YZ
2305 free_extent_buffer(root->node);
2306 free_extent_buffer(root->commit_root);
d397712b 2307 kfree(root->name);
2619ba1f 2308 kfree(root);
2619ba1f
CM
2309}
2310
35b7e476 2311static int del_fs_roots(struct btrfs_fs_info *fs_info)
0f7d52f4
CM
2312{
2313 int ret;
2314 struct btrfs_root *gang[8];
2315 int i;
2316
76dda93c
YZ
2317 while (!list_empty(&fs_info->dead_roots)) {
2318 gang[0] = list_entry(fs_info->dead_roots.next,
2319 struct btrfs_root, root_list);
2320 list_del(&gang[0]->root_list);
2321
2322 if (gang[0]->in_radix) {
2323 btrfs_free_fs_root(fs_info, gang[0]);
2324 } else {
2325 free_extent_buffer(gang[0]->node);
2326 free_extent_buffer(gang[0]->commit_root);
2327 kfree(gang[0]);
2328 }
2329 }
2330
d397712b 2331 while (1) {
0f7d52f4
CM
2332 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2333 (void **)gang, 0,
2334 ARRAY_SIZE(gang));
2335 if (!ret)
2336 break;
2619ba1f 2337 for (i = 0; i < ret; i++)
5eda7b5e 2338 btrfs_free_fs_root(fs_info, gang[i]);
0f7d52f4
CM
2339 }
2340 return 0;
2341}
b4100d64 2342
c146afad 2343int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
cfaa7295 2344{
c146afad
YZ
2345 u64 root_objectid = 0;
2346 struct btrfs_root *gang[8];
2347 int i;
3768f368 2348 int ret;
e089f05c 2349
c146afad
YZ
2350 while (1) {
2351 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2352 (void **)gang, root_objectid,
2353 ARRAY_SIZE(gang));
2354 if (!ret)
2355 break;
5d4f98a2
YZ
2356
2357 root_objectid = gang[ret - 1]->root_key.objectid + 1;
c146afad
YZ
2358 for (i = 0; i < ret; i++) {
2359 root_objectid = gang[i]->root_key.objectid;
c146afad
YZ
2360 btrfs_orphan_cleanup(gang[i]);
2361 }
2362 root_objectid++;
2363 }
2364 return 0;
2365}
a2135011 2366
c146afad
YZ
2367int btrfs_commit_super(struct btrfs_root *root)
2368{
2369 struct btrfs_trans_handle *trans;
2370 int ret;
a74a4b97 2371
c146afad 2372 mutex_lock(&root->fs_info->cleaner_mutex);
24bbcf04 2373 btrfs_run_delayed_iputs(root);
a74a4b97 2374 btrfs_clean_old_snapshots(root);
c146afad 2375 mutex_unlock(&root->fs_info->cleaner_mutex);
c71bf099
YZ
2376
2377 /* wait until ongoing cleanup work done */
2378 down_write(&root->fs_info->cleanup_work_sem);
2379 up_write(&root->fs_info->cleanup_work_sem);
2380
a22285a6 2381 trans = btrfs_join_transaction(root, 1);
3612b495
TI
2382 if (IS_ERR(trans))
2383 return PTR_ERR(trans);
54aa1f4d 2384 ret = btrfs_commit_transaction(trans, root);
c146afad
YZ
2385 BUG_ON(ret);
2386 /* run commit again to drop the original snapshot */
a22285a6 2387 trans = btrfs_join_transaction(root, 1);
3612b495
TI
2388 if (IS_ERR(trans))
2389 return PTR_ERR(trans);
79154b1b
CM
2390 btrfs_commit_transaction(trans, root);
2391 ret = btrfs_write_and_wait_transaction(NULL, root);
3768f368 2392 BUG_ON(ret);
d6bfde87 2393
a512bbf8 2394 ret = write_ctree_super(NULL, root, 0);
c146afad
YZ
2395 return ret;
2396}
2397
2398int close_ctree(struct btrfs_root *root)
2399{
2400 struct btrfs_fs_info *fs_info = root->fs_info;
2401 int ret;
2402
2403 fs_info->closing = 1;
2404 smp_mb();
2405
0af3d00b 2406 btrfs_put_block_group_cache(fs_info);
acce952b 2407
2408 /*
2409 * Here come 2 situations when btrfs is broken to flip readonly:
2410 *
2411 * 1. when btrfs flips readonly somewhere else before
2412 * btrfs_commit_super, sb->s_flags has MS_RDONLY flag,
2413 * and btrfs will skip to write sb directly to keep
2414 * ERROR state on disk.
2415 *
2416 * 2. when btrfs flips readonly just in btrfs_commit_super,
ae0e47f0 2417 * and in such case, btrfs cannot write sb via btrfs_commit_super,
acce952b 2418 * and since fs_state has been set BTRFS_SUPER_FLAG_ERROR flag,
2419 * btrfs will cleanup all FS resources first and write sb then.
2420 */
c146afad 2421 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
acce952b 2422 ret = btrfs_commit_super(root);
2423 if (ret)
2424 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
2425 }
2426
2427 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
2428 ret = btrfs_error_commit_super(root);
d397712b
CM
2429 if (ret)
2430 printk(KERN_ERR "btrfs: commit super ret %d\n", ret);
c146afad 2431 }
0f7d52f4 2432
8929ecfa
YZ
2433 kthread_stop(root->fs_info->transaction_kthread);
2434 kthread_stop(root->fs_info->cleaner_kthread);
2435
f25784b3
YZ
2436 fs_info->closing = 2;
2437 smp_mb();
2438
b0c68f8b 2439 if (fs_info->delalloc_bytes) {
d397712b 2440 printk(KERN_INFO "btrfs: at unmount delalloc count %llu\n",
21380931 2441 (unsigned long long)fs_info->delalloc_bytes);
b0c68f8b 2442 }
31153d81 2443 if (fs_info->total_ref_cache_size) {
d397712b
CM
2444 printk(KERN_INFO "btrfs: at umount reference cache size %llu\n",
2445 (unsigned long long)fs_info->total_ref_cache_size);
31153d81 2446 }
bcc63abb 2447
5d4f98a2
YZ
2448 free_extent_buffer(fs_info->extent_root->node);
2449 free_extent_buffer(fs_info->extent_root->commit_root);
2450 free_extent_buffer(fs_info->tree_root->node);
2451 free_extent_buffer(fs_info->tree_root->commit_root);
2452 free_extent_buffer(root->fs_info->chunk_root->node);
2453 free_extent_buffer(root->fs_info->chunk_root->commit_root);
2454 free_extent_buffer(root->fs_info->dev_root->node);
2455 free_extent_buffer(root->fs_info->dev_root->commit_root);
2456 free_extent_buffer(root->fs_info->csum_root->node);
2457 free_extent_buffer(root->fs_info->csum_root->commit_root);
d20f7043 2458
9078a3e1 2459 btrfs_free_block_groups(root->fs_info);
d10c5f31 2460
c146afad 2461 del_fs_roots(fs_info);
d10c5f31 2462
c146afad 2463 iput(fs_info->btree_inode);
9ad6b7bc 2464
61d92c32 2465 btrfs_stop_workers(&fs_info->generic_worker);
247e743c 2466 btrfs_stop_workers(&fs_info->fixup_workers);
771ed689 2467 btrfs_stop_workers(&fs_info->delalloc_workers);
8b712842
CM
2468 btrfs_stop_workers(&fs_info->workers);
2469 btrfs_stop_workers(&fs_info->endio_workers);
d20f7043 2470 btrfs_stop_workers(&fs_info->endio_meta_workers);
cad321ad 2471 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
e6dcd2dc 2472 btrfs_stop_workers(&fs_info->endio_write_workers);
0cb59c99 2473 btrfs_stop_workers(&fs_info->endio_freespace_worker);
1cc127b5 2474 btrfs_stop_workers(&fs_info->submit_workers);
d6bfde87 2475
dfe25020 2476 btrfs_close_devices(fs_info->fs_devices);
0b86a832 2477 btrfs_mapping_tree_free(&fs_info->mapping_tree);
b248a415 2478
04160088 2479 bdi_destroy(&fs_info->bdi);
76dda93c 2480 cleanup_srcu_struct(&fs_info->subvol_srcu);
0b86a832 2481
0f7d52f4 2482 kfree(fs_info->extent_root);
0f7d52f4 2483 kfree(fs_info->tree_root);
0b86a832
CM
2484 kfree(fs_info->chunk_root);
2485 kfree(fs_info->dev_root);
d20f7043 2486 kfree(fs_info->csum_root);
83a4d548
LZ
2487 kfree(fs_info);
2488
eb60ceac
CM
2489 return 0;
2490}
2491
1259ab75 2492int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
5f39d397 2493{
1259ab75 2494 int ret;
810191ff 2495 struct inode *btree_inode = buf->first_page->mapping->host;
1259ab75 2496
2ac55d41
JB
2497 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf,
2498 NULL);
1259ab75
CM
2499 if (!ret)
2500 return ret;
2501
2502 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
2503 parent_transid);
2504 return !ret;
5f39d397
CM
2505}
2506
2507int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
ccd467d6 2508{
810191ff 2509 struct inode *btree_inode = buf->first_page->mapping->host;
d1310b2e 2510 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
5f39d397
CM
2511 buf);
2512}
6702ed49 2513
5f39d397
CM
2514void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
2515{
810191ff 2516 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
5f39d397
CM
2517 u64 transid = btrfs_header_generation(buf);
2518 struct inode *btree_inode = root->fs_info->btree_inode;
b9473439 2519 int was_dirty;
b4ce94de 2520
b9447ef8 2521 btrfs_assert_tree_locked(buf);
ccd467d6 2522 if (transid != root->fs_info->generation) {
d397712b
CM
2523 printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
2524 "found %llu running %llu\n",
db94535d 2525 (unsigned long long)buf->start,
d397712b
CM
2526 (unsigned long long)transid,
2527 (unsigned long long)root->fs_info->generation);
ccd467d6
CM
2528 WARN_ON(1);
2529 }
b9473439
CM
2530 was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
2531 buf);
2532 if (!was_dirty) {
2533 spin_lock(&root->fs_info->delalloc_lock);
2534 root->fs_info->dirty_metadata_bytes += buf->len;
2535 spin_unlock(&root->fs_info->delalloc_lock);
2536 }
eb60ceac
CM
2537}
2538
d3c2fdcf 2539void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
35b7e476 2540{
188de649
CM
2541 /*
2542 * looks as though older kernels can get into trouble with
2543 * this code, they end up stuck in balance_dirty_pages forever
2544 */
d6bfde87 2545 u64 num_dirty;
771ed689 2546 unsigned long thresh = 32 * 1024 * 1024;
d6bfde87 2547
6933c02e 2548 if (current->flags & PF_MEMALLOC)
d6bfde87
CM
2549 return;
2550
585ad2c3
CM
2551 num_dirty = root->fs_info->dirty_metadata_bytes;
2552
d6bfde87
CM
2553 if (num_dirty > thresh) {
2554 balance_dirty_pages_ratelimited_nr(
d7fc640e 2555 root->fs_info->btree_inode->i_mapping, 1);
d6bfde87 2556 }
188de649 2557 return;
35b7e476 2558}
6b80053d 2559
ca7a79ad 2560int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
6b80053d 2561{
810191ff 2562 struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
ce9adaa5 2563 int ret;
ca7a79ad 2564 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
d397712b 2565 if (ret == 0)
b4ce94de 2566 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
ce9adaa5 2567 return ret;
6b80053d 2568}
0da5468f 2569
4bef0848
CM
2570int btree_lock_page_hook(struct page *page)
2571{
2572 struct inode *inode = page->mapping->host;
b9473439 2573 struct btrfs_root *root = BTRFS_I(inode)->root;
4bef0848
CM
2574 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2575 struct extent_buffer *eb;
2576 unsigned long len;
2577 u64 bytenr = page_offset(page);
2578
2579 if (page->private == EXTENT_PAGE_PRIVATE)
2580 goto out;
2581
2582 len = page->private >> 2;
2583 eb = find_extent_buffer(io_tree, bytenr, len, GFP_NOFS);
2584 if (!eb)
2585 goto out;
2586
2587 btrfs_tree_lock(eb);
4bef0848 2588 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
b9473439
CM
2589
2590 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
2591 spin_lock(&root->fs_info->delalloc_lock);
2592 if (root->fs_info->dirty_metadata_bytes >= eb->len)
2593 root->fs_info->dirty_metadata_bytes -= eb->len;
2594 else
2595 WARN_ON(1);
2596 spin_unlock(&root->fs_info->delalloc_lock);
2597 }
2598
4bef0848
CM
2599 btrfs_tree_unlock(eb);
2600 free_extent_buffer(eb);
2601out:
2602 lock_page(page);
2603 return 0;
2604}
2605
acce952b 2606static void btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
2607 int read_only)
2608{
2609 if (read_only)
2610 return;
2611
2612 if (fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
2613 printk(KERN_WARNING "warning: mount fs with errors, "
2614 "running btrfsck is recommended\n");
2615}
2616
2617int btrfs_error_commit_super(struct btrfs_root *root)
2618{
2619 int ret;
2620
2621 mutex_lock(&root->fs_info->cleaner_mutex);
2622 btrfs_run_delayed_iputs(root);
2623 mutex_unlock(&root->fs_info->cleaner_mutex);
2624
2625 down_write(&root->fs_info->cleanup_work_sem);
2626 up_write(&root->fs_info->cleanup_work_sem);
2627
2628 /* cleanup FS via transaction */
2629 btrfs_cleanup_transaction(root);
2630
2631 ret = write_ctree_super(NULL, root, 0);
2632
2633 return ret;
2634}
2635
2636static int btrfs_destroy_ordered_operations(struct btrfs_root *root)
2637{
2638 struct btrfs_inode *btrfs_inode;
2639 struct list_head splice;
2640
2641 INIT_LIST_HEAD(&splice);
2642
2643 mutex_lock(&root->fs_info->ordered_operations_mutex);
2644 spin_lock(&root->fs_info->ordered_extent_lock);
2645
2646 list_splice_init(&root->fs_info->ordered_operations, &splice);
2647 while (!list_empty(&splice)) {
2648 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
2649 ordered_operations);
2650
2651 list_del_init(&btrfs_inode->ordered_operations);
2652
2653 btrfs_invalidate_inodes(btrfs_inode->root);
2654 }
2655
2656 spin_unlock(&root->fs_info->ordered_extent_lock);
2657 mutex_unlock(&root->fs_info->ordered_operations_mutex);
2658
2659 return 0;
2660}
2661
2662static int btrfs_destroy_ordered_extents(struct btrfs_root *root)
2663{
2664 struct list_head splice;
2665 struct btrfs_ordered_extent *ordered;
2666 struct inode *inode;
2667
2668 INIT_LIST_HEAD(&splice);
2669
2670 spin_lock(&root->fs_info->ordered_extent_lock);
2671
2672 list_splice_init(&root->fs_info->ordered_extents, &splice);
2673 while (!list_empty(&splice)) {
2674 ordered = list_entry(splice.next, struct btrfs_ordered_extent,
2675 root_extent_list);
2676
2677 list_del_init(&ordered->root_extent_list);
2678 atomic_inc(&ordered->refs);
2679
2680 /* the inode may be getting freed (in sys_unlink path). */
2681 inode = igrab(ordered->inode);
2682
2683 spin_unlock(&root->fs_info->ordered_extent_lock);
2684 if (inode)
2685 iput(inode);
2686
2687 atomic_set(&ordered->refs, 1);
2688 btrfs_put_ordered_extent(ordered);
2689
2690 spin_lock(&root->fs_info->ordered_extent_lock);
2691 }
2692
2693 spin_unlock(&root->fs_info->ordered_extent_lock);
2694
2695 return 0;
2696}
2697
2698static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
2699 struct btrfs_root *root)
2700{
2701 struct rb_node *node;
2702 struct btrfs_delayed_ref_root *delayed_refs;
2703 struct btrfs_delayed_ref_node *ref;
2704 int ret = 0;
2705
2706 delayed_refs = &trans->delayed_refs;
2707
2708 spin_lock(&delayed_refs->lock);
2709 if (delayed_refs->num_entries == 0) {
2710 printk(KERN_INFO "delayed_refs has NO entry\n");
2711 return ret;
2712 }
2713
2714 node = rb_first(&delayed_refs->root);
2715 while (node) {
2716 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2717 node = rb_next(node);
2718
2719 ref->in_tree = 0;
2720 rb_erase(&ref->rb_node, &delayed_refs->root);
2721 delayed_refs->num_entries--;
2722
2723 atomic_set(&ref->refs, 1);
2724 if (btrfs_delayed_ref_is_head(ref)) {
2725 struct btrfs_delayed_ref_head *head;
2726
2727 head = btrfs_delayed_node_to_head(ref);
2728 mutex_lock(&head->mutex);
2729 kfree(head->extent_op);
2730 delayed_refs->num_heads--;
2731 if (list_empty(&head->cluster))
2732 delayed_refs->num_heads_ready--;
2733 list_del_init(&head->cluster);
2734 mutex_unlock(&head->mutex);
2735 }
2736
2737 spin_unlock(&delayed_refs->lock);
2738 btrfs_put_delayed_ref(ref);
2739
2740 cond_resched();
2741 spin_lock(&delayed_refs->lock);
2742 }
2743
2744 spin_unlock(&delayed_refs->lock);
2745
2746 return ret;
2747}
2748
2749static int btrfs_destroy_pending_snapshots(struct btrfs_transaction *t)
2750{
2751 struct btrfs_pending_snapshot *snapshot;
2752 struct list_head splice;
2753
2754 INIT_LIST_HEAD(&splice);
2755
2756 list_splice_init(&t->pending_snapshots, &splice);
2757
2758 while (!list_empty(&splice)) {
2759 snapshot = list_entry(splice.next,
2760 struct btrfs_pending_snapshot,
2761 list);
2762
2763 list_del_init(&snapshot->list);
2764
2765 kfree(snapshot);
2766 }
2767
2768 return 0;
2769}
2770
2771static int btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
2772{
2773 struct btrfs_inode *btrfs_inode;
2774 struct list_head splice;
2775
2776 INIT_LIST_HEAD(&splice);
2777
2778 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
2779
2780 spin_lock(&root->fs_info->delalloc_lock);
2781
2782 while (!list_empty(&splice)) {
2783 btrfs_inode = list_entry(splice.next, struct btrfs_inode,
2784 delalloc_inodes);
2785
2786 list_del_init(&btrfs_inode->delalloc_inodes);
2787
2788 btrfs_invalidate_inodes(btrfs_inode->root);
2789 }
2790
2791 spin_unlock(&root->fs_info->delalloc_lock);
2792
2793 return 0;
2794}
2795
2796static int btrfs_destroy_marked_extents(struct btrfs_root *root,
2797 struct extent_io_tree *dirty_pages,
2798 int mark)
2799{
2800 int ret;
2801 struct page *page;
2802 struct inode *btree_inode = root->fs_info->btree_inode;
2803 struct extent_buffer *eb;
2804 u64 start = 0;
2805 u64 end;
2806 u64 offset;
2807 unsigned long index;
2808
2809 while (1) {
2810 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
2811 mark);
2812 if (ret)
2813 break;
2814
2815 clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
2816 while (start <= end) {
2817 index = start >> PAGE_CACHE_SHIFT;
2818 start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
2819 page = find_get_page(btree_inode->i_mapping, index);
2820 if (!page)
2821 continue;
2822 offset = page_offset(page);
2823
2824 spin_lock(&dirty_pages->buffer_lock);
2825 eb = radix_tree_lookup(
2826 &(&BTRFS_I(page->mapping->host)->io_tree)->buffer,
2827 offset >> PAGE_CACHE_SHIFT);
2828 spin_unlock(&dirty_pages->buffer_lock);
2829 if (eb) {
2830 ret = test_and_clear_bit(EXTENT_BUFFER_DIRTY,
2831 &eb->bflags);
2832 atomic_set(&eb->refs, 1);
2833 }
2834 if (PageWriteback(page))
2835 end_page_writeback(page);
2836
2837 lock_page(page);
2838 if (PageDirty(page)) {
2839 clear_page_dirty_for_io(page);
2840 spin_lock_irq(&page->mapping->tree_lock);
2841 radix_tree_tag_clear(&page->mapping->page_tree,
2842 page_index(page),
2843 PAGECACHE_TAG_DIRTY);
2844 spin_unlock_irq(&page->mapping->tree_lock);
2845 }
2846
2847 page->mapping->a_ops->invalidatepage(page, 0);
2848 unlock_page(page);
2849 }
2850 }
2851
2852 return ret;
2853}
2854
2855static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
2856 struct extent_io_tree *pinned_extents)
2857{
2858 struct extent_io_tree *unpin;
2859 u64 start;
2860 u64 end;
2861 int ret;
2862
2863 unpin = pinned_extents;
2864 while (1) {
2865 ret = find_first_extent_bit(unpin, 0, &start, &end,
2866 EXTENT_DIRTY);
2867 if (ret)
2868 break;
2869
2870 /* opt_discard */
2871 ret = btrfs_error_discard_extent(root, start, end + 1 - start);
2872
2873 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2874 btrfs_error_unpin_extent_range(root, start, end);
2875 cond_resched();
2876 }
2877
2878 return 0;
2879}
2880
2881static int btrfs_cleanup_transaction(struct btrfs_root *root)
2882{
2883 struct btrfs_transaction *t;
2884 LIST_HEAD(list);
2885
2886 WARN_ON(1);
2887
2888 mutex_lock(&root->fs_info->trans_mutex);
2889 mutex_lock(&root->fs_info->transaction_kthread_mutex);
2890
2891 list_splice_init(&root->fs_info->trans_list, &list);
2892 while (!list_empty(&list)) {
2893 t = list_entry(list.next, struct btrfs_transaction, list);
2894 if (!t)
2895 break;
2896
2897 btrfs_destroy_ordered_operations(root);
2898
2899 btrfs_destroy_ordered_extents(root);
2900
2901 btrfs_destroy_delayed_refs(t, root);
2902
2903 btrfs_block_rsv_release(root,
2904 &root->fs_info->trans_block_rsv,
2905 t->dirty_pages.dirty_bytes);
2906
2907 /* FIXME: cleanup wait for commit */
2908 t->in_commit = 1;
2909 t->blocked = 1;
2910 if (waitqueue_active(&root->fs_info->transaction_blocked_wait))
2911 wake_up(&root->fs_info->transaction_blocked_wait);
2912
2913 t->blocked = 0;
2914 if (waitqueue_active(&root->fs_info->transaction_wait))
2915 wake_up(&root->fs_info->transaction_wait);
2916 mutex_unlock(&root->fs_info->trans_mutex);
2917
2918 mutex_lock(&root->fs_info->trans_mutex);
2919 t->commit_done = 1;
2920 if (waitqueue_active(&t->commit_wait))
2921 wake_up(&t->commit_wait);
2922 mutex_unlock(&root->fs_info->trans_mutex);
2923
2924 mutex_lock(&root->fs_info->trans_mutex);
2925
2926 btrfs_destroy_pending_snapshots(t);
2927
2928 btrfs_destroy_delalloc_inodes(root);
2929
2930 spin_lock(&root->fs_info->new_trans_lock);
2931 root->fs_info->running_transaction = NULL;
2932 spin_unlock(&root->fs_info->new_trans_lock);
2933
2934 btrfs_destroy_marked_extents(root, &t->dirty_pages,
2935 EXTENT_DIRTY);
2936
2937 btrfs_destroy_pinned_extent(root,
2938 root->fs_info->pinned_extents);
2939
2940 t->use_count = 0;
2941 list_del_init(&t->list);
2942 memset(t, 0, sizeof(*t));
2943 kmem_cache_free(btrfs_transaction_cachep, t);
2944 }
2945
2946 mutex_unlock(&root->fs_info->transaction_kthread_mutex);
2947 mutex_unlock(&root->fs_info->trans_mutex);
2948
2949 return 0;
2950}
2951
d1310b2e 2952static struct extent_io_ops btree_extent_io_ops = {
4bef0848 2953 .write_cache_pages_lock_hook = btree_lock_page_hook,
ce9adaa5 2954 .readpage_end_io_hook = btree_readpage_end_io_hook,
0b86a832 2955 .submit_bio_hook = btree_submit_bio_hook,
239b14b3
CM
2956 /* note we're sharing with inode.c for the merge bio hook */
2957 .merge_bio_hook = btrfs_merge_bio_hook,
0da5468f 2958};
This page took 0.366069 seconds and 5 git commands to generate.