]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/disk-io.c
btrfs: split btrfs_wait_marked_extents into normal and tree log functions
[mirror_ubuntu-artful-kernel.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>
5a0e3ad6 28#include <linux/slab.h>
784b4e29 29#include <linux/migrate.h>
7a36ddec 30#include <linux/ratelimit.h>
6463fe58 31#include <linux/uuid.h>
803b2f54 32#include <linux/semaphore.h>
7e75bf3f 33#include <asm/unaligned.h>
eb60ceac
CM
34#include "ctree.h"
35#include "disk-io.h"
0b947aff 36#include "hash.h"
e089f05c 37#include "transaction.h"
0f7d52f4 38#include "btrfs_inode.h"
0b86a832 39#include "volumes.h"
db94535d 40#include "print-tree.h"
925baedd 41#include "locking.h"
e02119d5 42#include "tree-log.h"
fa9c0d79 43#include "free-space-cache.h"
70f6d82e 44#include "free-space-tree.h"
581bb050 45#include "inode-map.h"
21adbd5c 46#include "check-integrity.h"
606686ee 47#include "rcu-string.h"
8dabb742 48#include "dev-replace.h"
53b381b3 49#include "raid56.h"
5ac1d209 50#include "sysfs.h"
fcebe456 51#include "qgroup.h"
ebb8765b 52#include "compression.h"
eb60ceac 53
de0022b9
JB
54#ifdef CONFIG_X86
55#include <asm/cpufeature.h>
56#endif
57
319e4d06
QW
58#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
59 BTRFS_HEADER_FLAG_RELOC |\
60 BTRFS_SUPER_FLAG_ERROR |\
61 BTRFS_SUPER_FLAG_SEEDING |\
62 BTRFS_SUPER_FLAG_METADUMP)
63
e8c9f186 64static const struct extent_io_ops btree_extent_io_ops;
8b712842 65static void end_workqueue_fn(struct btrfs_work *work);
4df27c4d 66static void free_fs_root(struct btrfs_root *root);
fcd1f065 67static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
acce952b 68 int read_only);
143bede5 69static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
acce952b 70static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
2ff7e61e 71 struct btrfs_fs_info *fs_info);
143bede5 72static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
2ff7e61e 73static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
acce952b 74 struct extent_io_tree *dirty_pages,
75 int mark);
2ff7e61e 76static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
acce952b 77 struct extent_io_tree *pinned_extents);
2ff7e61e
JM
78static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
79static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
ce9adaa5 80
d352ac68 81/*
97eb6b69
DS
82 * btrfs_end_io_wq structs are used to do processing in task context when an IO
83 * is complete. This is used during reads to verify checksums, and it is used
d352ac68
CM
84 * by writes to insert metadata for new file extents after IO is complete.
85 */
97eb6b69 86struct btrfs_end_io_wq {
ce9adaa5
CM
87 struct bio *bio;
88 bio_end_io_t *end_io;
89 void *private;
90 struct btrfs_fs_info *info;
91 int error;
bfebd8b5 92 enum btrfs_wq_endio_type metadata;
ce9adaa5 93 struct list_head list;
8b712842 94 struct btrfs_work work;
ce9adaa5 95};
0da5468f 96
97eb6b69
DS
97static struct kmem_cache *btrfs_end_io_wq_cache;
98
99int __init btrfs_end_io_wq_init(void)
100{
101 btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
102 sizeof(struct btrfs_end_io_wq),
103 0,
fba4b697 104 SLAB_MEM_SPREAD,
97eb6b69
DS
105 NULL);
106 if (!btrfs_end_io_wq_cache)
107 return -ENOMEM;
108 return 0;
109}
110
111void btrfs_end_io_wq_exit(void)
112{
5598e900 113 kmem_cache_destroy(btrfs_end_io_wq_cache);
97eb6b69
DS
114}
115
d352ac68
CM
116/*
117 * async submit bios are used to offload expensive checksumming
118 * onto the worker threads. They checksum file and metadata bios
119 * just before they are sent down the IO stack.
120 */
44b8bd7e
CM
121struct async_submit_bio {
122 struct inode *inode;
123 struct bio *bio;
124 struct list_head list;
4a69a410
CM
125 extent_submit_bio_hook_t *submit_bio_start;
126 extent_submit_bio_hook_t *submit_bio_done;
44b8bd7e 127 int mirror_num;
c8b97818 128 unsigned long bio_flags;
eaf25d93
CM
129 /*
130 * bio_offset is optional, can be used if the pages in the bio
131 * can't tell us where in the file the bio should go
132 */
133 u64 bio_offset;
8b712842 134 struct btrfs_work work;
79787eaa 135 int error;
44b8bd7e
CM
136};
137
85d4e461
CM
138/*
139 * Lockdep class keys for extent_buffer->lock's in this root. For a given
140 * eb, the lockdep key is determined by the btrfs_root it belongs to and
141 * the level the eb occupies in the tree.
142 *
143 * Different roots are used for different purposes and may nest inside each
144 * other and they require separate keysets. As lockdep keys should be
145 * static, assign keysets according to the purpose of the root as indicated
146 * by btrfs_root->objectid. This ensures that all special purpose roots
147 * have separate keysets.
4008c04a 148 *
85d4e461
CM
149 * Lock-nesting across peer nodes is always done with the immediate parent
150 * node locked thus preventing deadlock. As lockdep doesn't know this, use
151 * subclass to avoid triggering lockdep warning in such cases.
4008c04a 152 *
85d4e461
CM
153 * The key is set by the readpage_end_io_hook after the buffer has passed
154 * csum validation but before the pages are unlocked. It is also set by
155 * btrfs_init_new_buffer on freshly allocated blocks.
4008c04a 156 *
85d4e461
CM
157 * We also add a check to make sure the highest level of the tree is the
158 * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
159 * needs update as well.
4008c04a
CM
160 */
161#ifdef CONFIG_DEBUG_LOCK_ALLOC
162# if BTRFS_MAX_LEVEL != 8
163# error
164# endif
85d4e461
CM
165
166static struct btrfs_lockdep_keyset {
167 u64 id; /* root objectid */
168 const char *name_stem; /* lock name stem */
169 char names[BTRFS_MAX_LEVEL + 1][20];
170 struct lock_class_key keys[BTRFS_MAX_LEVEL + 1];
171} btrfs_lockdep_keysets[] = {
172 { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" },
173 { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" },
174 { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" },
175 { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" },
176 { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" },
177 { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" },
60b62978 178 { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" },
85d4e461
CM
179 { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" },
180 { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" },
181 { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" },
13fd8da9 182 { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" },
6b20e0ad 183 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" },
85d4e461 184 { .id = 0, .name_stem = "tree" },
4008c04a 185};
85d4e461
CM
186
187void __init btrfs_init_lockdep(void)
188{
189 int i, j;
190
191 /* initialize lockdep class names */
192 for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) {
193 struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i];
194
195 for (j = 0; j < ARRAY_SIZE(ks->names); j++)
196 snprintf(ks->names[j], sizeof(ks->names[j]),
197 "btrfs-%s-%02d", ks->name_stem, j);
198 }
199}
200
201void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
202 int level)
203{
204 struct btrfs_lockdep_keyset *ks;
205
206 BUG_ON(level >= ARRAY_SIZE(ks->keys));
207
208 /* find the matching keyset, id 0 is the default entry */
209 for (ks = btrfs_lockdep_keysets; ks->id; ks++)
210 if (ks->id == objectid)
211 break;
212
213 lockdep_set_class_and_name(&eb->lock,
214 &ks->keys[level], ks->names[level]);
215}
216
4008c04a
CM
217#endif
218
d352ac68
CM
219/*
220 * extents on the btree inode are pretty simple, there's one extent
221 * that covers the entire device
222 */
b2950863 223static struct extent_map *btree_get_extent(struct inode *inode,
306e16ce 224 struct page *page, size_t pg_offset, u64 start, u64 len,
b2950863 225 int create)
7eccb903 226{
0b246afa 227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5f39d397
CM
228 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
229 struct extent_map *em;
230 int ret;
231
890871be 232 read_lock(&em_tree->lock);
d1310b2e 233 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d 234 if (em) {
0b246afa 235 em->bdev = fs_info->fs_devices->latest_bdev;
890871be 236 read_unlock(&em_tree->lock);
5f39d397 237 goto out;
a061fc8d 238 }
890871be 239 read_unlock(&em_tree->lock);
7b13b7b1 240
172ddd60 241 em = alloc_extent_map();
5f39d397
CM
242 if (!em) {
243 em = ERR_PTR(-ENOMEM);
244 goto out;
245 }
246 em->start = 0;
0afbaf8c 247 em->len = (u64)-1;
c8b97818 248 em->block_len = (u64)-1;
5f39d397 249 em->block_start = 0;
0b246afa 250 em->bdev = fs_info->fs_devices->latest_bdev;
d1310b2e 251
890871be 252 write_lock(&em_tree->lock);
09a2a8f9 253 ret = add_extent_mapping(em_tree, em, 0);
5f39d397
CM
254 if (ret == -EEXIST) {
255 free_extent_map(em);
7b13b7b1 256 em = lookup_extent_mapping(em_tree, start, len);
b4f359ab 257 if (!em)
0433f20d 258 em = ERR_PTR(-EIO);
5f39d397 259 } else if (ret) {
7b13b7b1 260 free_extent_map(em);
0433f20d 261 em = ERR_PTR(ret);
5f39d397 262 }
890871be 263 write_unlock(&em_tree->lock);
7b13b7b1 264
5f39d397
CM
265out:
266 return em;
7eccb903
CM
267}
268
b0496686 269u32 btrfs_csum_data(char *data, u32 seed, size_t len)
19c00ddc 270{
0b947aff 271 return btrfs_crc32c(seed, data, len);
19c00ddc
CM
272}
273
0b5e3daf 274void btrfs_csum_final(u32 crc, u8 *result)
19c00ddc 275{
7e75bf3f 276 put_unaligned_le32(~crc, result);
19c00ddc
CM
277}
278
d352ac68
CM
279/*
280 * compute the csum for a btree block, and either verify it or write it
281 * into the csum field of the block.
282 */
01d58472
DD
283static int csum_tree_block(struct btrfs_fs_info *fs_info,
284 struct extent_buffer *buf,
19c00ddc
CM
285 int verify)
286{
01d58472 287 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
607d432d 288 char *result = NULL;
19c00ddc
CM
289 unsigned long len;
290 unsigned long cur_len;
291 unsigned long offset = BTRFS_CSUM_SIZE;
19c00ddc
CM
292 char *kaddr;
293 unsigned long map_start;
294 unsigned long map_len;
295 int err;
296 u32 crc = ~(u32)0;
607d432d 297 unsigned long inline_result;
19c00ddc
CM
298
299 len = buf->len - offset;
d397712b 300 while (len > 0) {
19c00ddc 301 err = map_private_extent_buffer(buf, offset, 32,
a6591715 302 &kaddr, &map_start, &map_len);
d397712b 303 if (err)
8bd98f0e 304 return err;
19c00ddc 305 cur_len = min(len, map_len - (offset - map_start));
b0496686 306 crc = btrfs_csum_data(kaddr + offset - map_start,
19c00ddc
CM
307 crc, cur_len);
308 len -= cur_len;
309 offset += cur_len;
19c00ddc 310 }
607d432d 311 if (csum_size > sizeof(inline_result)) {
31e818fe 312 result = kzalloc(csum_size, GFP_NOFS);
607d432d 313 if (!result)
8bd98f0e 314 return -ENOMEM;
607d432d
JB
315 } else {
316 result = (char *)&inline_result;
317 }
318
19c00ddc
CM
319 btrfs_csum_final(crc, result);
320
321 if (verify) {
607d432d 322 if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
e4204ded
CM
323 u32 val;
324 u32 found = 0;
607d432d 325 memcpy(&found, result, csum_size);
e4204ded 326
607d432d 327 read_extent_buffer(buf, &val, 0, csum_size);
94647322 328 btrfs_warn_rl(fs_info,
5d163e0e 329 "%s checksum verify failed on %llu wanted %X found %X level %d",
01d58472 330 fs_info->sb->s_id, buf->start,
efe120a0 331 val, found, btrfs_header_level(buf));
607d432d
JB
332 if (result != (char *)&inline_result)
333 kfree(result);
8bd98f0e 334 return -EUCLEAN;
19c00ddc
CM
335 }
336 } else {
607d432d 337 write_extent_buffer(buf, result, 0, csum_size);
19c00ddc 338 }
607d432d
JB
339 if (result != (char *)&inline_result)
340 kfree(result);
19c00ddc
CM
341 return 0;
342}
343
d352ac68
CM
344/*
345 * we can't consider a given block up to date unless the transid of the
346 * block matches the transid in the parent node's pointer. This is how we
347 * detect blocks that either didn't get written at all or got written
348 * in the wrong place.
349 */
1259ab75 350static int verify_parent_transid(struct extent_io_tree *io_tree,
b9fab919
CM
351 struct extent_buffer *eb, u64 parent_transid,
352 int atomic)
1259ab75 353{
2ac55d41 354 struct extent_state *cached_state = NULL;
1259ab75 355 int ret;
2755a0de 356 bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB);
1259ab75
CM
357
358 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
359 return 0;
360
b9fab919
CM
361 if (atomic)
362 return -EAGAIN;
363
a26e8c9f
JB
364 if (need_lock) {
365 btrfs_tree_read_lock(eb);
366 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
367 }
368
2ac55d41 369 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
ff13db41 370 &cached_state);
0b32f4bb 371 if (extent_buffer_uptodate(eb) &&
1259ab75
CM
372 btrfs_header_generation(eb) == parent_transid) {
373 ret = 0;
374 goto out;
375 }
94647322
DS
376 btrfs_err_rl(eb->fs_info,
377 "parent transid verify failed on %llu wanted %llu found %llu",
378 eb->start,
29549aec 379 parent_transid, btrfs_header_generation(eb));
1259ab75 380 ret = 1;
a26e8c9f
JB
381
382 /*
383 * Things reading via commit roots that don't have normal protection,
384 * like send, can have a really old block in cache that may point at a
01327610 385 * block that has been freed and re-allocated. So don't clear uptodate
a26e8c9f
JB
386 * if we find an eb that is under IO (dirty/writeback) because we could
387 * end up reading in the stale data and then writing it back out and
388 * making everybody very sad.
389 */
390 if (!extent_buffer_under_io(eb))
391 clear_extent_buffer_uptodate(eb);
33958dc6 392out:
2ac55d41
JB
393 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
394 &cached_state, GFP_NOFS);
472b909f
JB
395 if (need_lock)
396 btrfs_tree_read_unlock_blocking(eb);
1259ab75 397 return ret;
1259ab75
CM
398}
399
1104a885
DS
400/*
401 * Return 0 if the superblock checksum type matches the checksum value of that
402 * algorithm. Pass the raw disk superblock data.
403 */
ab8d0fc4
JM
404static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
405 char *raw_disk_sb)
1104a885
DS
406{
407 struct btrfs_super_block *disk_sb =
408 (struct btrfs_super_block *)raw_disk_sb;
409 u16 csum_type = btrfs_super_csum_type(disk_sb);
410 int ret = 0;
411
412 if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
413 u32 crc = ~(u32)0;
414 const int csum_size = sizeof(crc);
415 char result[csum_size];
416
417 /*
418 * The super_block structure does not span the whole
419 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
01327610 420 * is filled with zeros and is included in the checksum.
1104a885
DS
421 */
422 crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
423 crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
424 btrfs_csum_final(crc, result);
425
426 if (memcmp(raw_disk_sb, result, csum_size))
427 ret = 1;
428 }
429
430 if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
ab8d0fc4 431 btrfs_err(fs_info, "unsupported checksum algorithm %u",
1104a885
DS
432 csum_type);
433 ret = 1;
434 }
435
436 return ret;
437}
438
d352ac68
CM
439/*
440 * helper to read a given tree block, doing retries as required when
441 * the checksums don't match and we have alternate mirrors to try.
442 */
2ff7e61e 443static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info,
f188591e 444 struct extent_buffer *eb,
8436ea91 445 u64 parent_transid)
f188591e
CM
446{
447 struct extent_io_tree *io_tree;
ea466794 448 int failed = 0;
f188591e
CM
449 int ret;
450 int num_copies = 0;
451 int mirror_num = 0;
ea466794 452 int failed_mirror = 0;
f188591e 453
a826d6dc 454 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
0b246afa 455 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
f188591e 456 while (1) {
8436ea91 457 ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE,
f188591e 458 btree_get_extent, mirror_num);
256dd1bb
SB
459 if (!ret) {
460 if (!verify_parent_transid(io_tree, eb,
b9fab919 461 parent_transid, 0))
256dd1bb
SB
462 break;
463 else
464 ret = -EIO;
465 }
d397712b 466
a826d6dc
JB
467 /*
468 * This buffer's crc is fine, but its contents are corrupted, so
469 * there is no reason to read the other copies, they won't be
470 * any less wrong.
471 */
472 if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags))
ea466794
JB
473 break;
474
0b246afa 475 num_copies = btrfs_num_copies(fs_info,
f188591e 476 eb->start, eb->len);
4235298e 477 if (num_copies == 1)
ea466794 478 break;
4235298e 479
5cf1ab56
JB
480 if (!failed_mirror) {
481 failed = 1;
482 failed_mirror = eb->read_mirror;
483 }
484
f188591e 485 mirror_num++;
ea466794
JB
486 if (mirror_num == failed_mirror)
487 mirror_num++;
488
4235298e 489 if (mirror_num > num_copies)
ea466794 490 break;
f188591e 491 }
ea466794 492
c0901581 493 if (failed && !ret && failed_mirror)
2ff7e61e 494 repair_eb_io_failure(fs_info, eb, failed_mirror);
ea466794
JB
495
496 return ret;
f188591e 497}
19c00ddc 498
d352ac68 499/*
d397712b
CM
500 * checksum a dirty tree block before IO. This has extra checks to make sure
501 * we only fill in the checksum field in the first page of a multi-page block
d352ac68 502 */
d397712b 503
01d58472 504static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page)
19c00ddc 505{
4eee4fa4 506 u64 start = page_offset(page);
19c00ddc 507 u64 found_start;
19c00ddc 508 struct extent_buffer *eb;
f188591e 509
4f2de97a
JB
510 eb = (struct extent_buffer *)page->private;
511 if (page != eb->pages[0])
512 return 0;
0f805531 513
19c00ddc 514 found_start = btrfs_header_bytenr(eb);
0f805531
AL
515 /*
516 * Please do not consolidate these warnings into a single if.
517 * It is useful to know what went wrong.
518 */
519 if (WARN_ON(found_start != start))
520 return -EUCLEAN;
521 if (WARN_ON(!PageUptodate(page)))
522 return -EUCLEAN;
523
524 ASSERT(memcmp_extent_buffer(eb, fs_info->fsid,
525 btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0);
526
8bd98f0e 527 return csum_tree_block(fs_info, eb, 0);
19c00ddc
CM
528}
529
01d58472 530static int check_tree_block_fsid(struct btrfs_fs_info *fs_info,
2b82032c
YZ
531 struct extent_buffer *eb)
532{
01d58472 533 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2b82032c
YZ
534 u8 fsid[BTRFS_UUID_SIZE];
535 int ret = 1;
536
0a4e5586 537 read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
2b82032c
YZ
538 while (fs_devices) {
539 if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
540 ret = 0;
541 break;
542 }
543 fs_devices = fs_devices->seed;
544 }
545 return ret;
546}
547
0b246afa
JM
548#define CORRUPT(reason, eb, root, slot) \
549 btrfs_crit(root->fs_info, \
550 "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \
551 btrfs_header_level(eb) == 0 ? "leaf" : "node", \
6b722c17 552 reason, btrfs_header_bytenr(eb), root->objectid, slot)
a826d6dc
JB
553
554static noinline int check_leaf(struct btrfs_root *root,
555 struct extent_buffer *leaf)
556{
0b246afa 557 struct btrfs_fs_info *fs_info = root->fs_info;
a826d6dc
JB
558 struct btrfs_key key;
559 struct btrfs_key leaf_key;
560 u32 nritems = btrfs_header_nritems(leaf);
561 int slot;
562
1ba98d08
LB
563 if (nritems == 0) {
564 struct btrfs_root *check_root;
565
566 key.objectid = btrfs_header_owner(leaf);
567 key.type = BTRFS_ROOT_ITEM_KEY;
568 key.offset = (u64)-1;
569
0b246afa 570 check_root = btrfs_get_fs_root(fs_info, &key, false);
1ba98d08
LB
571 /*
572 * The only reason we also check NULL here is that during
573 * open_ctree() some roots has not yet been set up.
574 */
575 if (!IS_ERR_OR_NULL(check_root)) {
576 /* if leaf is the root, then it's fine */
577 if (leaf->start !=
578 btrfs_root_bytenr(&check_root->root_item)) {
579 CORRUPT("non-root leaf's nritems is 0",
580 leaf, root, 0);
581 return -EIO;
582 }
583 }
a826d6dc 584 return 0;
1ba98d08 585 }
a826d6dc
JB
586
587 /* Check the 0 item */
588 if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) !=
0b246afa 589 BTRFS_LEAF_DATA_SIZE(fs_info)) {
a826d6dc
JB
590 CORRUPT("invalid item offset size pair", leaf, root, 0);
591 return -EIO;
592 }
593
594 /*
595 * Check to make sure each items keys are in the correct order and their
596 * offsets make sense. We only have to loop through nritems-1 because
597 * we check the current slot against the next slot, which verifies the
598 * next slot's offset+size makes sense and that the current's slot
599 * offset is correct.
600 */
601 for (slot = 0; slot < nritems - 1; slot++) {
602 btrfs_item_key_to_cpu(leaf, &leaf_key, slot);
603 btrfs_item_key_to_cpu(leaf, &key, slot + 1);
604
605 /* Make sure the keys are in the right order */
606 if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) {
607 CORRUPT("bad key order", leaf, root, slot);
608 return -EIO;
609 }
610
611 /*
612 * Make sure the offset and ends are right, remember that the
613 * item data starts at the end of the leaf and grows towards the
614 * front.
615 */
616 if (btrfs_item_offset_nr(leaf, slot) !=
617 btrfs_item_end_nr(leaf, slot + 1)) {
618 CORRUPT("slot offset bad", leaf, root, slot);
619 return -EIO;
620 }
621
622 /*
623 * Check to make sure that we don't point outside of the leaf,
01327610 624 * just in case all the items are consistent to each other, but
a826d6dc
JB
625 * all point outside of the leaf.
626 */
627 if (btrfs_item_end_nr(leaf, slot) >
0b246afa 628 BTRFS_LEAF_DATA_SIZE(fs_info)) {
a826d6dc
JB
629 CORRUPT("slot end outside of leaf", leaf, root, slot);
630 return -EIO;
631 }
632 }
633
634 return 0;
635}
636
053ab70f
LB
637static int check_node(struct btrfs_root *root, struct extent_buffer *node)
638{
639 unsigned long nr = btrfs_header_nritems(node);
6b722c17
LB
640 struct btrfs_key key, next_key;
641 int slot;
642 u64 bytenr;
643 int ret = 0;
053ab70f 644
da17066c 645 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) {
053ab70f
LB
646 btrfs_crit(root->fs_info,
647 "corrupt node: block %llu root %llu nritems %lu",
648 node->start, root->objectid, nr);
649 return -EIO;
650 }
6b722c17
LB
651
652 for (slot = 0; slot < nr - 1; slot++) {
653 bytenr = btrfs_node_blockptr(node, slot);
654 btrfs_node_key_to_cpu(node, &key, slot);
655 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
656
657 if (!bytenr) {
658 CORRUPT("invalid item slot", node, root, slot);
659 ret = -EIO;
660 goto out;
661 }
662
663 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
664 CORRUPT("bad key order", node, root, slot);
665 ret = -EIO;
666 goto out;
667 }
668 }
669out:
670 return ret;
053ab70f
LB
671}
672
facc8a22
MX
673static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
674 u64 phy_offset, struct page *page,
675 u64 start, u64 end, int mirror)
ce9adaa5 676{
ce9adaa5
CM
677 u64 found_start;
678 int found_level;
ce9adaa5
CM
679 struct extent_buffer *eb;
680 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
02873e43 681 struct btrfs_fs_info *fs_info = root->fs_info;
f188591e 682 int ret = 0;
727011e0 683 int reads_done;
ce9adaa5 684
ce9adaa5
CM
685 if (!page->private)
686 goto out;
d397712b 687
4f2de97a 688 eb = (struct extent_buffer *)page->private;
d397712b 689
0b32f4bb
JB
690 /* the pending IO might have been the only thing that kept this buffer
691 * in memory. Make sure we have a ref for all this other checks
692 */
693 extent_buffer_get(eb);
694
695 reads_done = atomic_dec_and_test(&eb->io_pages);
727011e0
CM
696 if (!reads_done)
697 goto err;
f188591e 698
5cf1ab56 699 eb->read_mirror = mirror;
656f30db 700 if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
ea466794
JB
701 ret = -EIO;
702 goto err;
703 }
704
ce9adaa5 705 found_start = btrfs_header_bytenr(eb);
727011e0 706 if (found_start != eb->start) {
02873e43
ZL
707 btrfs_err_rl(fs_info, "bad tree block start %llu %llu",
708 found_start, eb->start);
f188591e 709 ret = -EIO;
ce9adaa5
CM
710 goto err;
711 }
02873e43
ZL
712 if (check_tree_block_fsid(fs_info, eb)) {
713 btrfs_err_rl(fs_info, "bad fsid on block %llu",
714 eb->start);
1259ab75
CM
715 ret = -EIO;
716 goto err;
717 }
ce9adaa5 718 found_level = btrfs_header_level(eb);
1c24c3ce 719 if (found_level >= BTRFS_MAX_LEVEL) {
02873e43
ZL
720 btrfs_err(fs_info, "bad tree block level %d",
721 (int)btrfs_header_level(eb));
1c24c3ce
JB
722 ret = -EIO;
723 goto err;
724 }
ce9adaa5 725
85d4e461
CM
726 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb),
727 eb, found_level);
4008c04a 728
02873e43 729 ret = csum_tree_block(fs_info, eb, 1);
8bd98f0e 730 if (ret)
a826d6dc 731 goto err;
a826d6dc
JB
732
733 /*
734 * If this is a leaf block and it is corrupt, set the corrupt bit so
735 * that we don't try and read the other copies of this block, just
736 * return -EIO.
737 */
738 if (found_level == 0 && check_leaf(root, eb)) {
739 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
740 ret = -EIO;
741 }
ce9adaa5 742
053ab70f
LB
743 if (found_level > 0 && check_node(root, eb))
744 ret = -EIO;
745
0b32f4bb
JB
746 if (!ret)
747 set_extent_buffer_uptodate(eb);
ce9adaa5 748err:
79fb65a1
JB
749 if (reads_done &&
750 test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
fc2e901f 751 btree_readahead_hook(fs_info, eb, ret);
4bb31e92 752
53b381b3
DW
753 if (ret) {
754 /*
755 * our io error hook is going to dec the io pages
756 * again, we have to make sure it has something
757 * to decrement
758 */
759 atomic_inc(&eb->io_pages);
0b32f4bb 760 clear_extent_buffer_uptodate(eb);
53b381b3 761 }
0b32f4bb 762 free_extent_buffer(eb);
ce9adaa5 763out:
f188591e 764 return ret;
ce9adaa5
CM
765}
766
ea466794 767static int btree_io_failed_hook(struct page *page, int failed_mirror)
4bb31e92 768{
4bb31e92 769 struct extent_buffer *eb;
4bb31e92 770
4f2de97a 771 eb = (struct extent_buffer *)page->private;
656f30db 772 set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
5cf1ab56 773 eb->read_mirror = failed_mirror;
53b381b3 774 atomic_dec(&eb->io_pages);
ea466794 775 if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
fc2e901f 776 btree_readahead_hook(eb->fs_info, eb, -EIO);
4bb31e92
AJ
777 return -EIO; /* we fixed nothing */
778}
779
4246a0b6 780static void end_workqueue_bio(struct bio *bio)
ce9adaa5 781{
97eb6b69 782 struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
ce9adaa5 783 struct btrfs_fs_info *fs_info;
9e0af237
LB
784 struct btrfs_workqueue *wq;
785 btrfs_work_func_t func;
ce9adaa5 786
ce9adaa5 787 fs_info = end_io_wq->info;
4246a0b6 788 end_io_wq->error = bio->bi_error;
d20f7043 789
37226b21 790 if (bio_op(bio) == REQ_OP_WRITE) {
9e0af237
LB
791 if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) {
792 wq = fs_info->endio_meta_write_workers;
793 func = btrfs_endio_meta_write_helper;
794 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) {
795 wq = fs_info->endio_freespace_worker;
796 func = btrfs_freespace_write_helper;
797 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
798 wq = fs_info->endio_raid56_workers;
799 func = btrfs_endio_raid56_helper;
800 } else {
801 wq = fs_info->endio_write_workers;
802 func = btrfs_endio_write_helper;
803 }
d20f7043 804 } else {
8b110e39
MX
805 if (unlikely(end_io_wq->metadata ==
806 BTRFS_WQ_ENDIO_DIO_REPAIR)) {
807 wq = fs_info->endio_repair_workers;
808 func = btrfs_endio_repair_helper;
809 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
9e0af237
LB
810 wq = fs_info->endio_raid56_workers;
811 func = btrfs_endio_raid56_helper;
812 } else if (end_io_wq->metadata) {
813 wq = fs_info->endio_meta_workers;
814 func = btrfs_endio_meta_helper;
815 } else {
816 wq = fs_info->endio_workers;
817 func = btrfs_endio_helper;
818 }
d20f7043 819 }
9e0af237
LB
820
821 btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL);
822 btrfs_queue_work(wq, &end_io_wq->work);
ce9adaa5
CM
823}
824
22c59948 825int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
bfebd8b5 826 enum btrfs_wq_endio_type metadata)
0b86a832 827{
97eb6b69 828 struct btrfs_end_io_wq *end_io_wq;
8b110e39 829
97eb6b69 830 end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
ce9adaa5
CM
831 if (!end_io_wq)
832 return -ENOMEM;
833
834 end_io_wq->private = bio->bi_private;
835 end_io_wq->end_io = bio->bi_end_io;
22c59948 836 end_io_wq->info = info;
ce9adaa5
CM
837 end_io_wq->error = 0;
838 end_io_wq->bio = bio;
22c59948 839 end_io_wq->metadata = metadata;
ce9adaa5
CM
840
841 bio->bi_private = end_io_wq;
842 bio->bi_end_io = end_workqueue_bio;
22c59948
CM
843 return 0;
844}
845
b64a2851 846unsigned long btrfs_async_submit_limit(struct btrfs_fs_info *info)
0986fe9e 847{
4854ddd0 848 unsigned long limit = min_t(unsigned long,
5cdc7ad3 849 info->thread_pool_size,
4854ddd0
CM
850 info->fs_devices->open_devices);
851 return 256 * limit;
852}
0986fe9e 853
4a69a410
CM
854static void run_one_async_start(struct btrfs_work *work)
855{
4a69a410 856 struct async_submit_bio *async;
79787eaa 857 int ret;
4a69a410
CM
858
859 async = container_of(work, struct async_submit_bio, work);
81a75f67 860 ret = async->submit_bio_start(async->inode, async->bio,
79787eaa
JM
861 async->mirror_num, async->bio_flags,
862 async->bio_offset);
863 if (ret)
864 async->error = ret;
4a69a410
CM
865}
866
867static void run_one_async_done(struct btrfs_work *work)
8b712842
CM
868{
869 struct btrfs_fs_info *fs_info;
870 struct async_submit_bio *async;
4854ddd0 871 int limit;
8b712842
CM
872
873 async = container_of(work, struct async_submit_bio, work);
874 fs_info = BTRFS_I(async->inode)->root->fs_info;
4854ddd0 875
b64a2851 876 limit = btrfs_async_submit_limit(fs_info);
4854ddd0
CM
877 limit = limit * 2 / 3;
878
ee863954
DS
879 /*
880 * atomic_dec_return implies a barrier for waitqueue_active
881 */
66657b31 882 if (atomic_dec_return(&fs_info->nr_async_submits) < limit &&
b64a2851 883 waitqueue_active(&fs_info->async_submit_wait))
4854ddd0
CM
884 wake_up(&fs_info->async_submit_wait);
885
bb7ab3b9 886 /* If an error occurred we just want to clean up the bio and move on */
79787eaa 887 if (async->error) {
4246a0b6
CH
888 async->bio->bi_error = async->error;
889 bio_endio(async->bio);
79787eaa
JM
890 return;
891 }
892
81a75f67
MC
893 async->submit_bio_done(async->inode, async->bio, async->mirror_num,
894 async->bio_flags, async->bio_offset);
4a69a410
CM
895}
896
897static void run_one_async_free(struct btrfs_work *work)
898{
899 struct async_submit_bio *async;
900
901 async = container_of(work, struct async_submit_bio, work);
8b712842
CM
902 kfree(async);
903}
904
44b8bd7e 905int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
81a75f67 906 struct bio *bio, int mirror_num,
c8b97818 907 unsigned long bio_flags,
eaf25d93 908 u64 bio_offset,
4a69a410
CM
909 extent_submit_bio_hook_t *submit_bio_start,
910 extent_submit_bio_hook_t *submit_bio_done)
44b8bd7e
CM
911{
912 struct async_submit_bio *async;
913
914 async = kmalloc(sizeof(*async), GFP_NOFS);
915 if (!async)
916 return -ENOMEM;
917
918 async->inode = inode;
44b8bd7e
CM
919 async->bio = bio;
920 async->mirror_num = mirror_num;
4a69a410
CM
921 async->submit_bio_start = submit_bio_start;
922 async->submit_bio_done = submit_bio_done;
923
9e0af237 924 btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start,
5cdc7ad3 925 run_one_async_done, run_one_async_free);
4a69a410 926
c8b97818 927 async->bio_flags = bio_flags;
eaf25d93 928 async->bio_offset = bio_offset;
8c8bee1d 929
79787eaa
JM
930 async->error = 0;
931
cb03c743 932 atomic_inc(&fs_info->nr_async_submits);
d313d7a3 933
1eff9d32 934 if (bio->bi_opf & REQ_SYNC)
5cdc7ad3 935 btrfs_set_work_high_priority(&async->work);
d313d7a3 936
5cdc7ad3 937 btrfs_queue_work(fs_info->workers, &async->work);
9473f16c 938
d397712b 939 while (atomic_read(&fs_info->async_submit_draining) &&
771ed689
CM
940 atomic_read(&fs_info->nr_async_submits)) {
941 wait_event(fs_info->async_submit_wait,
942 (atomic_read(&fs_info->nr_async_submits) == 0));
943 }
944
44b8bd7e
CM
945 return 0;
946}
947
ce3ed71a
CM
948static int btree_csum_one_bio(struct bio *bio)
949{
2c30c71b 950 struct bio_vec *bvec;
ce3ed71a 951 struct btrfs_root *root;
2c30c71b 952 int i, ret = 0;
ce3ed71a 953
2c30c71b 954 bio_for_each_segment_all(bvec, bio, i) {
ce3ed71a 955 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
01d58472 956 ret = csum_dirty_buffer(root->fs_info, bvec->bv_page);
79787eaa
JM
957 if (ret)
958 break;
ce3ed71a 959 }
2c30c71b 960
79787eaa 961 return ret;
ce3ed71a
CM
962}
963
81a75f67
MC
964static int __btree_submit_bio_start(struct inode *inode, struct bio *bio,
965 int mirror_num, unsigned long bio_flags,
eaf25d93 966 u64 bio_offset)
22c59948 967{
8b712842
CM
968 /*
969 * when we're called for a write, we're already in the async
5443be45 970 * submission context. Just jump into btrfs_map_bio
8b712842 971 */
79787eaa 972 return btree_csum_one_bio(bio);
4a69a410 973}
22c59948 974
81a75f67 975static int __btree_submit_bio_done(struct inode *inode, struct bio *bio,
eaf25d93
CM
976 int mirror_num, unsigned long bio_flags,
977 u64 bio_offset)
4a69a410 978{
61891923
SB
979 int ret;
980
8b712842 981 /*
4a69a410
CM
982 * when we're called for a write, we're already in the async
983 * submission context. Just jump into btrfs_map_bio
8b712842 984 */
2ff7e61e 985 ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1);
4246a0b6
CH
986 if (ret) {
987 bio->bi_error = ret;
988 bio_endio(bio);
989 }
61891923 990 return ret;
0b86a832
CM
991}
992
de0022b9
JB
993static int check_async_write(struct inode *inode, unsigned long bio_flags)
994{
995 if (bio_flags & EXTENT_BIO_TREE_LOG)
996 return 0;
997#ifdef CONFIG_X86
bc696ca0 998 if (static_cpu_has(X86_FEATURE_XMM4_2))
de0022b9
JB
999 return 0;
1000#endif
1001 return 1;
1002}
1003
81a75f67 1004static int btree_submit_bio_hook(struct inode *inode, struct bio *bio,
eaf25d93
CM
1005 int mirror_num, unsigned long bio_flags,
1006 u64 bio_offset)
44b8bd7e 1007{
0b246afa 1008 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
de0022b9 1009 int async = check_async_write(inode, bio_flags);
cad321ad
CM
1010 int ret;
1011
37226b21 1012 if (bio_op(bio) != REQ_OP_WRITE) {
4a69a410
CM
1013 /*
1014 * called for a read, do the setup so that checksum validation
1015 * can happen in the async kernel threads
1016 */
0b246afa
JM
1017 ret = btrfs_bio_wq_end_io(fs_info, bio,
1018 BTRFS_WQ_ENDIO_METADATA);
1d4284bd 1019 if (ret)
61891923 1020 goto out_w_error;
2ff7e61e 1021 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
de0022b9
JB
1022 } else if (!async) {
1023 ret = btree_csum_one_bio(bio);
1024 if (ret)
61891923 1025 goto out_w_error;
2ff7e61e 1026 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
61891923
SB
1027 } else {
1028 /*
1029 * kthread helpers are used to submit writes so that
1030 * checksumming can happen in parallel across all CPUs
1031 */
0b246afa 1032 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, 0,
61891923
SB
1033 bio_offset,
1034 __btree_submit_bio_start,
1035 __btree_submit_bio_done);
44b8bd7e 1036 }
d313d7a3 1037
4246a0b6
CH
1038 if (ret)
1039 goto out_w_error;
1040 return 0;
1041
61891923 1042out_w_error:
4246a0b6
CH
1043 bio->bi_error = ret;
1044 bio_endio(bio);
61891923 1045 return ret;
44b8bd7e
CM
1046}
1047
3dd1462e 1048#ifdef CONFIG_MIGRATION
784b4e29 1049static int btree_migratepage(struct address_space *mapping,
a6bc32b8
MG
1050 struct page *newpage, struct page *page,
1051 enum migrate_mode mode)
784b4e29
CM
1052{
1053 /*
1054 * we can't safely write a btree page from here,
1055 * we haven't done the locking hook
1056 */
1057 if (PageDirty(page))
1058 return -EAGAIN;
1059 /*
1060 * Buffers may be managed in a filesystem specific way.
1061 * We must have no buffers or drop them.
1062 */
1063 if (page_has_private(page) &&
1064 !try_to_release_page(page, GFP_KERNEL))
1065 return -EAGAIN;
a6bc32b8 1066 return migrate_page(mapping, newpage, page, mode);
784b4e29 1067}
3dd1462e 1068#endif
784b4e29 1069
0da5468f
CM
1070
1071static int btree_writepages(struct address_space *mapping,
1072 struct writeback_control *wbc)
1073{
e2d84521
MX
1074 struct btrfs_fs_info *fs_info;
1075 int ret;
1076
d8d5f3e1 1077 if (wbc->sync_mode == WB_SYNC_NONE) {
448d640b
CM
1078
1079 if (wbc->for_kupdate)
1080 return 0;
1081
e2d84521 1082 fs_info = BTRFS_I(mapping->host)->root->fs_info;
b9473439 1083 /* this is a bit racy, but that's ok */
e2d84521
MX
1084 ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes,
1085 BTRFS_DIRTY_METADATA_THRESH);
1086 if (ret < 0)
793955bc 1087 return 0;
793955bc 1088 }
0b32f4bb 1089 return btree_write_cache_pages(mapping, wbc);
0da5468f
CM
1090}
1091
b2950863 1092static int btree_readpage(struct file *file, struct page *page)
5f39d397 1093{
d1310b2e
CM
1094 struct extent_io_tree *tree;
1095 tree = &BTRFS_I(page->mapping->host)->io_tree;
8ddc7d9c 1096 return extent_read_full_page(tree, page, btree_get_extent, 0);
5f39d397 1097}
22b0ebda 1098
70dec807 1099static int btree_releasepage(struct page *page, gfp_t gfp_flags)
5f39d397 1100{
98509cfc 1101 if (PageWriteback(page) || PageDirty(page))
d397712b 1102 return 0;
0c4e538b 1103
f7a52a40 1104 return try_release_extent_buffer(page);
d98237b3
CM
1105}
1106
d47992f8
LC
1107static void btree_invalidatepage(struct page *page, unsigned int offset,
1108 unsigned int length)
d98237b3 1109{
d1310b2e
CM
1110 struct extent_io_tree *tree;
1111 tree = &BTRFS_I(page->mapping->host)->io_tree;
5f39d397
CM
1112 extent_invalidatepage(tree, page, offset);
1113 btree_releasepage(page, GFP_NOFS);
9ad6b7bc 1114 if (PagePrivate(page)) {
efe120a0
FH
1115 btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
1116 "page private not zero on page %llu",
1117 (unsigned long long)page_offset(page));
9ad6b7bc
CM
1118 ClearPagePrivate(page);
1119 set_page_private(page, 0);
09cbfeaf 1120 put_page(page);
9ad6b7bc 1121 }
d98237b3
CM
1122}
1123
0b32f4bb
JB
1124static int btree_set_page_dirty(struct page *page)
1125{
bb146eb2 1126#ifdef DEBUG
0b32f4bb
JB
1127 struct extent_buffer *eb;
1128
1129 BUG_ON(!PagePrivate(page));
1130 eb = (struct extent_buffer *)page->private;
1131 BUG_ON(!eb);
1132 BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1133 BUG_ON(!atomic_read(&eb->refs));
1134 btrfs_assert_tree_locked(eb);
bb146eb2 1135#endif
0b32f4bb
JB
1136 return __set_page_dirty_nobuffers(page);
1137}
1138
7f09410b 1139static const struct address_space_operations btree_aops = {
d98237b3 1140 .readpage = btree_readpage,
0da5468f 1141 .writepages = btree_writepages,
5f39d397
CM
1142 .releasepage = btree_releasepage,
1143 .invalidatepage = btree_invalidatepage,
5a92bc88 1144#ifdef CONFIG_MIGRATION
784b4e29 1145 .migratepage = btree_migratepage,
5a92bc88 1146#endif
0b32f4bb 1147 .set_page_dirty = btree_set_page_dirty,
d98237b3
CM
1148};
1149
2ff7e61e 1150void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr)
090d1875 1151{
5f39d397 1152 struct extent_buffer *buf = NULL;
2ff7e61e 1153 struct inode *btree_inode = fs_info->btree_inode;
090d1875 1154
2ff7e61e 1155 buf = btrfs_find_create_tree_block(fs_info, bytenr);
c871b0f2 1156 if (IS_ERR(buf))
6197d86e 1157 return;
d1310b2e 1158 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
8436ea91 1159 buf, WAIT_NONE, btree_get_extent, 0);
5f39d397 1160 free_extent_buffer(buf);
090d1875
CM
1161}
1162
2ff7e61e 1163int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
ab0fff03
AJ
1164 int mirror_num, struct extent_buffer **eb)
1165{
1166 struct extent_buffer *buf = NULL;
2ff7e61e 1167 struct inode *btree_inode = fs_info->btree_inode;
ab0fff03
AJ
1168 struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
1169 int ret;
1170
2ff7e61e 1171 buf = btrfs_find_create_tree_block(fs_info, bytenr);
c871b0f2 1172 if (IS_ERR(buf))
ab0fff03
AJ
1173 return 0;
1174
1175 set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags);
1176
8436ea91 1177 ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK,
ab0fff03
AJ
1178 btree_get_extent, mirror_num);
1179 if (ret) {
1180 free_extent_buffer(buf);
1181 return ret;
1182 }
1183
1184 if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
1185 free_extent_buffer(buf);
1186 return -EIO;
0b32f4bb 1187 } else if (extent_buffer_uptodate(buf)) {
ab0fff03
AJ
1188 *eb = buf;
1189 } else {
1190 free_extent_buffer(buf);
1191 }
1192 return 0;
1193}
1194
2ff7e61e
JM
1195struct extent_buffer *btrfs_find_create_tree_block(
1196 struct btrfs_fs_info *fs_info,
1197 u64 bytenr)
0999df54 1198{
0b246afa
JM
1199 if (btrfs_is_testing(fs_info))
1200 return alloc_test_extent_buffer(fs_info, bytenr);
1201 return alloc_extent_buffer(fs_info, bytenr);
0999df54
CM
1202}
1203
1204
e02119d5
CM
1205int btrfs_write_tree_block(struct extent_buffer *buf)
1206{
727011e0 1207 return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
8aa38c31 1208 buf->start + buf->len - 1);
e02119d5
CM
1209}
1210
1211int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
1212{
727011e0 1213 return filemap_fdatawait_range(buf->pages[0]->mapping,
8aa38c31 1214 buf->start, buf->start + buf->len - 1);
e02119d5
CM
1215}
1216
2ff7e61e 1217struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
ce86cd59 1218 u64 parent_transid)
0999df54
CM
1219{
1220 struct extent_buffer *buf = NULL;
0999df54
CM
1221 int ret;
1222
2ff7e61e 1223 buf = btrfs_find_create_tree_block(fs_info, bytenr);
c871b0f2
LB
1224 if (IS_ERR(buf))
1225 return buf;
0999df54 1226
2ff7e61e 1227 ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid);
0f0fe8f7
FDBM
1228 if (ret) {
1229 free_extent_buffer(buf);
64c043de 1230 return ERR_PTR(ret);
0f0fe8f7 1231 }
5f39d397 1232 return buf;
ce9adaa5 1233
eb60ceac
CM
1234}
1235
01d58472
DD
1236void clean_tree_block(struct btrfs_trans_handle *trans,
1237 struct btrfs_fs_info *fs_info,
d5c13f92 1238 struct extent_buffer *buf)
ed2ff2cb 1239{
55c69072 1240 if (btrfs_header_generation(buf) ==
e2d84521 1241 fs_info->running_transaction->transid) {
b9447ef8 1242 btrfs_assert_tree_locked(buf);
b4ce94de 1243
b9473439 1244 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
e2d84521
MX
1245 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
1246 -buf->len,
1247 fs_info->dirty_metadata_batch);
ed7b63eb
JB
1248 /* ugh, clear_extent_buffer_dirty needs to lock the page */
1249 btrfs_set_lock_blocking(buf);
1250 clear_extent_buffer_dirty(buf);
1251 }
925baedd 1252 }
5f39d397
CM
1253}
1254
8257b2dc
MX
1255static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void)
1256{
1257 struct btrfs_subvolume_writers *writers;
1258 int ret;
1259
1260 writers = kmalloc(sizeof(*writers), GFP_NOFS);
1261 if (!writers)
1262 return ERR_PTR(-ENOMEM);
1263
908c7f19 1264 ret = percpu_counter_init(&writers->counter, 0, GFP_KERNEL);
8257b2dc
MX
1265 if (ret < 0) {
1266 kfree(writers);
1267 return ERR_PTR(ret);
1268 }
1269
1270 init_waitqueue_head(&writers->wait);
1271 return writers;
1272}
1273
1274static void
1275btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers)
1276{
1277 percpu_counter_destroy(&writers->counter);
1278 kfree(writers);
1279}
1280
da17066c 1281static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
143bede5 1282 u64 objectid)
d97e63b6 1283{
7c0260ee 1284 bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
cfaa7295 1285 root->node = NULL;
a28ec197 1286 root->commit_root = NULL;
27cdeb70 1287 root->state = 0;
d68fc57b 1288 root->orphan_cleanup_state = 0;
0b86a832 1289
0f7d52f4
CM
1290 root->objectid = objectid;
1291 root->last_trans = 0;
13a8a7c8 1292 root->highest_objectid = 0;
eb73c1b7 1293 root->nr_delalloc_inodes = 0;
199c2a9c 1294 root->nr_ordered_extents = 0;
58176a96 1295 root->name = NULL;
6bef4d31 1296 root->inode_tree = RB_ROOT;
16cdcec7 1297 INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
f0486c68 1298 root->block_rsv = NULL;
d68fc57b 1299 root->orphan_block_rsv = NULL;
0b86a832
CM
1300
1301 INIT_LIST_HEAD(&root->dirty_list);
5d4f98a2 1302 INIT_LIST_HEAD(&root->root_list);
eb73c1b7
MX
1303 INIT_LIST_HEAD(&root->delalloc_inodes);
1304 INIT_LIST_HEAD(&root->delalloc_root);
199c2a9c
MX
1305 INIT_LIST_HEAD(&root->ordered_extents);
1306 INIT_LIST_HEAD(&root->ordered_root);
2ab28f32
JB
1307 INIT_LIST_HEAD(&root->logged_list[0]);
1308 INIT_LIST_HEAD(&root->logged_list[1]);
d68fc57b 1309 spin_lock_init(&root->orphan_lock);
5d4f98a2 1310 spin_lock_init(&root->inode_lock);
eb73c1b7 1311 spin_lock_init(&root->delalloc_lock);
199c2a9c 1312 spin_lock_init(&root->ordered_extent_lock);
f0486c68 1313 spin_lock_init(&root->accounting_lock);
2ab28f32
JB
1314 spin_lock_init(&root->log_extents_lock[0]);
1315 spin_lock_init(&root->log_extents_lock[1]);
a2135011 1316 mutex_init(&root->objectid_mutex);
e02119d5 1317 mutex_init(&root->log_mutex);
31f3d255 1318 mutex_init(&root->ordered_extent_mutex);
573bfb72 1319 mutex_init(&root->delalloc_mutex);
7237f183
YZ
1320 init_waitqueue_head(&root->log_writer_wait);
1321 init_waitqueue_head(&root->log_commit_wait[0]);
1322 init_waitqueue_head(&root->log_commit_wait[1]);
8b050d35
MX
1323 INIT_LIST_HEAD(&root->log_ctxs[0]);
1324 INIT_LIST_HEAD(&root->log_ctxs[1]);
7237f183
YZ
1325 atomic_set(&root->log_commit[0], 0);
1326 atomic_set(&root->log_commit[1], 0);
1327 atomic_set(&root->log_writers, 0);
2ecb7923 1328 atomic_set(&root->log_batch, 0);
8a35d95f 1329 atomic_set(&root->orphan_inodes, 0);
b0feb9d9 1330 atomic_set(&root->refs, 1);
8257b2dc 1331 atomic_set(&root->will_be_snapshoted, 0);
55eeaf05 1332 atomic_set(&root->qgroup_meta_rsv, 0);
7237f183 1333 root->log_transid = 0;
d1433deb 1334 root->log_transid_committed = -1;
257c62e1 1335 root->last_log_commit = 0;
7c0260ee 1336 if (!dummy)
06ea65a3
JB
1337 extent_io_tree_init(&root->dirty_log_pages,
1338 fs_info->btree_inode->i_mapping);
017e5369 1339
3768f368
CM
1340 memset(&root->root_key, 0, sizeof(root->root_key));
1341 memset(&root->root_item, 0, sizeof(root->root_item));
6702ed49 1342 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
7c0260ee 1343 if (!dummy)
06ea65a3
JB
1344 root->defrag_trans_start = fs_info->generation;
1345 else
1346 root->defrag_trans_start = 0;
4d775673 1347 root->root_key.objectid = objectid;
0ee5dc67 1348 root->anon_dev = 0;
8ea05e3a 1349
5f3ab90a 1350 spin_lock_init(&root->root_item_lock);
3768f368
CM
1351}
1352
74e4d827
DS
1353static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
1354 gfp_t flags)
6f07e42e 1355{
74e4d827 1356 struct btrfs_root *root = kzalloc(sizeof(*root), flags);
6f07e42e
AV
1357 if (root)
1358 root->fs_info = fs_info;
1359 return root;
1360}
1361
06ea65a3
JB
1362#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1363/* Should only be used by the testing infrastructure */
da17066c 1364struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
06ea65a3
JB
1365{
1366 struct btrfs_root *root;
1367
7c0260ee
JM
1368 if (!fs_info)
1369 return ERR_PTR(-EINVAL);
1370
1371 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
06ea65a3
JB
1372 if (!root)
1373 return ERR_PTR(-ENOMEM);
da17066c 1374
b9ef22de 1375 /* We don't use the stripesize in selftest, set it as sectorsize */
da17066c 1376 __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
faa2dbf0 1377 root->alloc_bytenr = 0;
06ea65a3
JB
1378
1379 return root;
1380}
1381#endif
1382
20897f5c
AJ
1383struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
1384 struct btrfs_fs_info *fs_info,
1385 u64 objectid)
1386{
1387 struct extent_buffer *leaf;
1388 struct btrfs_root *tree_root = fs_info->tree_root;
1389 struct btrfs_root *root;
1390 struct btrfs_key key;
1391 int ret = 0;
6463fe58 1392 uuid_le uuid;
20897f5c 1393
74e4d827 1394 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
20897f5c
AJ
1395 if (!root)
1396 return ERR_PTR(-ENOMEM);
1397
da17066c 1398 __setup_root(root, fs_info, objectid);
20897f5c
AJ
1399 root->root_key.objectid = objectid;
1400 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1401 root->root_key.offset = 0;
1402
4d75f8a9 1403 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
20897f5c
AJ
1404 if (IS_ERR(leaf)) {
1405 ret = PTR_ERR(leaf);
1dd05682 1406 leaf = NULL;
20897f5c
AJ
1407 goto fail;
1408 }
1409
b159fa28 1410 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
20897f5c
AJ
1411 btrfs_set_header_bytenr(leaf, leaf->start);
1412 btrfs_set_header_generation(leaf, trans->transid);
1413 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1414 btrfs_set_header_owner(leaf, objectid);
1415 root->node = leaf;
1416
d24ee97b
DS
1417 write_extent_buffer_fsid(leaf, fs_info->fsid);
1418 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
20897f5c
AJ
1419 btrfs_mark_buffer_dirty(leaf);
1420
1421 root->commit_root = btrfs_root_node(root);
27cdeb70 1422 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
20897f5c
AJ
1423
1424 root->root_item.flags = 0;
1425 root->root_item.byte_limit = 0;
1426 btrfs_set_root_bytenr(&root->root_item, leaf->start);
1427 btrfs_set_root_generation(&root->root_item, trans->transid);
1428 btrfs_set_root_level(&root->root_item, 0);
1429 btrfs_set_root_refs(&root->root_item, 1);
1430 btrfs_set_root_used(&root->root_item, leaf->len);
1431 btrfs_set_root_last_snapshot(&root->root_item, 0);
1432 btrfs_set_root_dirid(&root->root_item, 0);
6463fe58
SB
1433 uuid_le_gen(&uuid);
1434 memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE);
20897f5c
AJ
1435 root->root_item.drop_level = 0;
1436
1437 key.objectid = objectid;
1438 key.type = BTRFS_ROOT_ITEM_KEY;
1439 key.offset = 0;
1440 ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
1441 if (ret)
1442 goto fail;
1443
1444 btrfs_tree_unlock(leaf);
1445
1dd05682
TI
1446 return root;
1447
20897f5c 1448fail:
1dd05682
TI
1449 if (leaf) {
1450 btrfs_tree_unlock(leaf);
59885b39 1451 free_extent_buffer(root->commit_root);
1dd05682
TI
1452 free_extent_buffer(leaf);
1453 }
1454 kfree(root);
20897f5c 1455
1dd05682 1456 return ERR_PTR(ret);
20897f5c
AJ
1457}
1458
7237f183
YZ
1459static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1460 struct btrfs_fs_info *fs_info)
0f7d52f4
CM
1461{
1462 struct btrfs_root *root;
7237f183 1463 struct extent_buffer *leaf;
e02119d5 1464
74e4d827 1465 root = btrfs_alloc_root(fs_info, GFP_NOFS);
e02119d5 1466 if (!root)
7237f183 1467 return ERR_PTR(-ENOMEM);
e02119d5 1468
da17066c 1469 __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID);
e02119d5
CM
1470
1471 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1472 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1473 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
27cdeb70 1474
7237f183 1475 /*
27cdeb70
MX
1476 * DON'T set REF_COWS for log trees
1477 *
7237f183
YZ
1478 * log trees do not get reference counted because they go away
1479 * before a real commit is actually done. They do store pointers
1480 * to file data extents, and those reference counts still get
1481 * updated (along with back refs to the log tree).
1482 */
e02119d5 1483
4d75f8a9
DS
1484 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
1485 NULL, 0, 0, 0);
7237f183
YZ
1486 if (IS_ERR(leaf)) {
1487 kfree(root);
1488 return ERR_CAST(leaf);
1489 }
e02119d5 1490
b159fa28 1491 memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header));
5d4f98a2
YZ
1492 btrfs_set_header_bytenr(leaf, leaf->start);
1493 btrfs_set_header_generation(leaf, trans->transid);
1494 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
1495 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
7237f183 1496 root->node = leaf;
e02119d5 1497
0b246afa 1498 write_extent_buffer_fsid(root->node, fs_info->fsid);
e02119d5
CM
1499 btrfs_mark_buffer_dirty(root->node);
1500 btrfs_tree_unlock(root->node);
7237f183
YZ
1501 return root;
1502}
1503
1504int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1505 struct btrfs_fs_info *fs_info)
1506{
1507 struct btrfs_root *log_root;
1508
1509 log_root = alloc_log_tree(trans, fs_info);
1510 if (IS_ERR(log_root))
1511 return PTR_ERR(log_root);
1512 WARN_ON(fs_info->log_root_tree);
1513 fs_info->log_root_tree = log_root;
1514 return 0;
1515}
1516
1517int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root)
1519{
0b246afa 1520 struct btrfs_fs_info *fs_info = root->fs_info;
7237f183
YZ
1521 struct btrfs_root *log_root;
1522 struct btrfs_inode_item *inode_item;
1523
0b246afa 1524 log_root = alloc_log_tree(trans, fs_info);
7237f183
YZ
1525 if (IS_ERR(log_root))
1526 return PTR_ERR(log_root);
1527
1528 log_root->last_trans = trans->transid;
1529 log_root->root_key.offset = root->root_key.objectid;
1530
1531 inode_item = &log_root->root_item.inode;
3cae210f
QW
1532 btrfs_set_stack_inode_generation(inode_item, 1);
1533 btrfs_set_stack_inode_size(inode_item, 3);
1534 btrfs_set_stack_inode_nlink(inode_item, 1);
da17066c 1535 btrfs_set_stack_inode_nbytes(inode_item,
0b246afa 1536 fs_info->nodesize);
3cae210f 1537 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
7237f183 1538
5d4f98a2 1539 btrfs_set_root_node(&log_root->root_item, log_root->node);
7237f183
YZ
1540
1541 WARN_ON(root->log_root);
1542 root->log_root = log_root;
1543 root->log_transid = 0;
d1433deb 1544 root->log_transid_committed = -1;
257c62e1 1545 root->last_log_commit = 0;
e02119d5
CM
1546 return 0;
1547}
1548
35a3621b
SB
1549static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1550 struct btrfs_key *key)
e02119d5
CM
1551{
1552 struct btrfs_root *root;
1553 struct btrfs_fs_info *fs_info = tree_root->fs_info;
0f7d52f4 1554 struct btrfs_path *path;
84234f3a 1555 u64 generation;
cb517eab 1556 int ret;
0f7d52f4 1557
cb517eab
MX
1558 path = btrfs_alloc_path();
1559 if (!path)
0f7d52f4 1560 return ERR_PTR(-ENOMEM);
cb517eab 1561
74e4d827 1562 root = btrfs_alloc_root(fs_info, GFP_NOFS);
cb517eab
MX
1563 if (!root) {
1564 ret = -ENOMEM;
1565 goto alloc_fail;
0f7d52f4
CM
1566 }
1567
da17066c 1568 __setup_root(root, fs_info, key->objectid);
0f7d52f4 1569
cb517eab
MX
1570 ret = btrfs_find_root(tree_root, key, path,
1571 &root->root_item, &root->root_key);
0f7d52f4 1572 if (ret) {
13a8a7c8
YZ
1573 if (ret > 0)
1574 ret = -ENOENT;
cb517eab 1575 goto find_fail;
0f7d52f4 1576 }
13a8a7c8 1577
84234f3a 1578 generation = btrfs_root_generation(&root->root_item);
2ff7e61e
JM
1579 root->node = read_tree_block(fs_info,
1580 btrfs_root_bytenr(&root->root_item),
ce86cd59 1581 generation);
64c043de
LB
1582 if (IS_ERR(root->node)) {
1583 ret = PTR_ERR(root->node);
cb517eab
MX
1584 goto find_fail;
1585 } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1586 ret = -EIO;
64c043de
LB
1587 free_extent_buffer(root->node);
1588 goto find_fail;
416bc658 1589 }
5d4f98a2 1590 root->commit_root = btrfs_root_node(root);
13a8a7c8 1591out:
cb517eab
MX
1592 btrfs_free_path(path);
1593 return root;
1594
cb517eab
MX
1595find_fail:
1596 kfree(root);
1597alloc_fail:
1598 root = ERR_PTR(ret);
1599 goto out;
1600}
1601
1602struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
1603 struct btrfs_key *location)
1604{
1605 struct btrfs_root *root;
1606
1607 root = btrfs_read_tree_root(tree_root, location);
1608 if (IS_ERR(root))
1609 return root;
1610
1611 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
27cdeb70 1612 set_bit(BTRFS_ROOT_REF_COWS, &root->state);
08fe4db1
LZ
1613 btrfs_check_and_init_root_item(&root->root_item);
1614 }
13a8a7c8 1615
5eda7b5e
CM
1616 return root;
1617}
1618
cb517eab
MX
1619int btrfs_init_fs_root(struct btrfs_root *root)
1620{
1621 int ret;
8257b2dc 1622 struct btrfs_subvolume_writers *writers;
cb517eab
MX
1623
1624 root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
1625 root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
1626 GFP_NOFS);
1627 if (!root->free_ino_pinned || !root->free_ino_ctl) {
1628 ret = -ENOMEM;
1629 goto fail;
1630 }
1631
8257b2dc
MX
1632 writers = btrfs_alloc_subvolume_writers();
1633 if (IS_ERR(writers)) {
1634 ret = PTR_ERR(writers);
1635 goto fail;
1636 }
1637 root->subv_writers = writers;
1638
cb517eab 1639 btrfs_init_free_ino_ctl(root);
57cdc8db
DS
1640 spin_lock_init(&root->ino_cache_lock);
1641 init_waitqueue_head(&root->ino_cache_wait);
cb517eab
MX
1642
1643 ret = get_anon_bdev(&root->anon_dev);
1644 if (ret)
876d2cf1 1645 goto fail;
f32e48e9
CR
1646
1647 mutex_lock(&root->objectid_mutex);
1648 ret = btrfs_find_highest_objectid(root,
1649 &root->highest_objectid);
1650 if (ret) {
1651 mutex_unlock(&root->objectid_mutex);
876d2cf1 1652 goto fail;
f32e48e9
CR
1653 }
1654
1655 ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
1656
1657 mutex_unlock(&root->objectid_mutex);
1658
cb517eab
MX
1659 return 0;
1660fail:
876d2cf1 1661 /* the caller is responsible to call free_fs_root */
cb517eab
MX
1662 return ret;
1663}
1664
35bbb97f
JM
1665struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1666 u64 root_id)
cb517eab
MX
1667{
1668 struct btrfs_root *root;
1669
1670 spin_lock(&fs_info->fs_roots_radix_lock);
1671 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1672 (unsigned long)root_id);
1673 spin_unlock(&fs_info->fs_roots_radix_lock);
1674 return root;
1675}
1676
1677int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1678 struct btrfs_root *root)
1679{
1680 int ret;
1681
e1860a77 1682 ret = radix_tree_preload(GFP_NOFS);
cb517eab
MX
1683 if (ret)
1684 return ret;
1685
1686 spin_lock(&fs_info->fs_roots_radix_lock);
1687 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1688 (unsigned long)root->root_key.objectid,
1689 root);
1690 if (ret == 0)
27cdeb70 1691 set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
cb517eab
MX
1692 spin_unlock(&fs_info->fs_roots_radix_lock);
1693 radix_tree_preload_end();
1694
1695 return ret;
1696}
1697
c00869f1
MX
1698struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1699 struct btrfs_key *location,
1700 bool check_ref)
5eda7b5e
CM
1701{
1702 struct btrfs_root *root;
381cf658 1703 struct btrfs_path *path;
1d4c08e0 1704 struct btrfs_key key;
5eda7b5e
CM
1705 int ret;
1706
edbd8d4e
CM
1707 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1708 return fs_info->tree_root;
1709 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1710 return fs_info->extent_root;
8f18cf13
CM
1711 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1712 return fs_info->chunk_root;
1713 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1714 return fs_info->dev_root;
0403e47e
YZ
1715 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1716 return fs_info->csum_root;
bcef60f2
AJ
1717 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
1718 return fs_info->quota_root ? fs_info->quota_root :
1719 ERR_PTR(-ENOENT);
f7a81ea4
SB
1720 if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
1721 return fs_info->uuid_root ? fs_info->uuid_root :
1722 ERR_PTR(-ENOENT);
70f6d82e
OS
1723 if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
1724 return fs_info->free_space_root ? fs_info->free_space_root :
1725 ERR_PTR(-ENOENT);
4df27c4d 1726again:
cb517eab 1727 root = btrfs_lookup_fs_root(fs_info, location->objectid);
48475471 1728 if (root) {
c00869f1 1729 if (check_ref && btrfs_root_refs(&root->root_item) == 0)
48475471 1730 return ERR_PTR(-ENOENT);
5eda7b5e 1731 return root;
48475471 1732 }
5eda7b5e 1733
cb517eab 1734 root = btrfs_read_fs_root(fs_info->tree_root, location);
5eda7b5e
CM
1735 if (IS_ERR(root))
1736 return root;
3394e160 1737
c00869f1 1738 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
cb517eab 1739 ret = -ENOENT;
581bb050 1740 goto fail;
35a30d7c 1741 }
581bb050 1742
cb517eab 1743 ret = btrfs_init_fs_root(root);
ac08aedf
CM
1744 if (ret)
1745 goto fail;
3394e160 1746
381cf658
DS
1747 path = btrfs_alloc_path();
1748 if (!path) {
1749 ret = -ENOMEM;
1750 goto fail;
1751 }
1d4c08e0
DS
1752 key.objectid = BTRFS_ORPHAN_OBJECTID;
1753 key.type = BTRFS_ORPHAN_ITEM_KEY;
1754 key.offset = location->objectid;
1755
1756 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
381cf658 1757 btrfs_free_path(path);
d68fc57b
YZ
1758 if (ret < 0)
1759 goto fail;
1760 if (ret == 0)
27cdeb70 1761 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
d68fc57b 1762
cb517eab 1763 ret = btrfs_insert_fs_root(fs_info, root);
0f7d52f4 1764 if (ret) {
4df27c4d
YZ
1765 if (ret == -EEXIST) {
1766 free_fs_root(root);
1767 goto again;
1768 }
1769 goto fail;
0f7d52f4 1770 }
edbd8d4e 1771 return root;
4df27c4d
YZ
1772fail:
1773 free_fs_root(root);
1774 return ERR_PTR(ret);
edbd8d4e
CM
1775}
1776
04160088
CM
1777static int btrfs_congested_fn(void *congested_data, int bdi_bits)
1778{
1779 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
1780 int ret = 0;
04160088
CM
1781 struct btrfs_device *device;
1782 struct backing_dev_info *bdi;
b7967db7 1783
1f78160c
XG
1784 rcu_read_lock();
1785 list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
dfe25020
CM
1786 if (!device->bdev)
1787 continue;
04160088 1788 bdi = blk_get_backing_dev_info(device->bdev);
ff9ea323 1789 if (bdi_congested(bdi, bdi_bits)) {
04160088
CM
1790 ret = 1;
1791 break;
1792 }
1793 }
1f78160c 1794 rcu_read_unlock();
04160088
CM
1795 return ret;
1796}
1797
04160088
CM
1798static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1799{
ad081f14
JA
1800 int err;
1801
b4caecd4 1802 err = bdi_setup_and_register(bdi, "btrfs");
ad081f14
JA
1803 if (err)
1804 return err;
1805
09cbfeaf 1806 bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
04160088
CM
1807 bdi->congested_fn = btrfs_congested_fn;
1808 bdi->congested_data = info;
da2f0f74 1809 bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK;
04160088
CM
1810 return 0;
1811}
1812
8b712842
CM
1813/*
1814 * called by the kthread helper functions to finally call the bio end_io
1815 * functions. This is where read checksum verification actually happens
1816 */
1817static void end_workqueue_fn(struct btrfs_work *work)
ce9adaa5 1818{
ce9adaa5 1819 struct bio *bio;
97eb6b69 1820 struct btrfs_end_io_wq *end_io_wq;
ce9adaa5 1821
97eb6b69 1822 end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
8b712842 1823 bio = end_io_wq->bio;
ce9adaa5 1824
4246a0b6 1825 bio->bi_error = end_io_wq->error;
8b712842
CM
1826 bio->bi_private = end_io_wq->private;
1827 bio->bi_end_io = end_io_wq->end_io;
97eb6b69 1828 kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
4246a0b6 1829 bio_endio(bio);
44b8bd7e
CM
1830}
1831
a74a4b97
CM
1832static int cleaner_kthread(void *arg)
1833{
1834 struct btrfs_root *root = arg;
0b246afa 1835 struct btrfs_fs_info *fs_info = root->fs_info;
d0278245 1836 int again;
da288d28 1837 struct btrfs_trans_handle *trans;
a74a4b97
CM
1838
1839 do {
d0278245 1840 again = 0;
a74a4b97 1841
d0278245 1842 /* Make the cleaner go to sleep early. */
2ff7e61e 1843 if (btrfs_need_cleaner_sleep(fs_info))
d0278245
MX
1844 goto sleep;
1845
90c711ab
ZB
1846 /*
1847 * Do not do anything if we might cause open_ctree() to block
1848 * before we have finished mounting the filesystem.
1849 */
0b246afa 1850 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
90c711ab
ZB
1851 goto sleep;
1852
0b246afa 1853 if (!mutex_trylock(&fs_info->cleaner_mutex))
d0278245
MX
1854 goto sleep;
1855
dc7f370c
MX
1856 /*
1857 * Avoid the problem that we change the status of the fs
1858 * during the above check and trylock.
1859 */
2ff7e61e 1860 if (btrfs_need_cleaner_sleep(fs_info)) {
0b246afa 1861 mutex_unlock(&fs_info->cleaner_mutex);
dc7f370c 1862 goto sleep;
76dda93c 1863 }
a74a4b97 1864
0b246afa 1865 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
2ff7e61e 1866 btrfs_run_delayed_iputs(fs_info);
0b246afa 1867 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
c2d6cb16 1868
d0278245 1869 again = btrfs_clean_one_deleted_snapshot(root);
0b246afa 1870 mutex_unlock(&fs_info->cleaner_mutex);
d0278245
MX
1871
1872 /*
05323cd1
MX
1873 * The defragger has dealt with the R/O remount and umount,
1874 * needn't do anything special here.
d0278245 1875 */
0b246afa 1876 btrfs_run_defrag_inodes(fs_info);
67c5e7d4
FM
1877
1878 /*
1879 * Acquires fs_info->delete_unused_bgs_mutex to avoid racing
1880 * with relocation (btrfs_relocate_chunk) and relocation
1881 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
1882 * after acquiring fs_info->delete_unused_bgs_mutex. So we
1883 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
1884 * unused block groups.
1885 */
0b246afa 1886 btrfs_delete_unused_bgs(fs_info);
d0278245 1887sleep:
838fe188 1888 if (!again) {
a74a4b97 1889 set_current_state(TASK_INTERRUPTIBLE);
8929ecfa
YZ
1890 if (!kthread_should_stop())
1891 schedule();
a74a4b97
CM
1892 __set_current_state(TASK_RUNNING);
1893 }
1894 } while (!kthread_should_stop());
da288d28
FM
1895
1896 /*
1897 * Transaction kthread is stopped before us and wakes us up.
1898 * However we might have started a new transaction and COWed some
1899 * tree blocks when deleting unused block groups for example. So
1900 * make sure we commit the transaction we started to have a clean
1901 * shutdown when evicting the btree inode - if it has dirty pages
1902 * when we do the final iput() on it, eviction will trigger a
1903 * writeback for it which will fail with null pointer dereferences
1904 * since work queues and other resources were already released and
1905 * destroyed by the time the iput/eviction/writeback is made.
1906 */
1907 trans = btrfs_attach_transaction(root);
1908 if (IS_ERR(trans)) {
1909 if (PTR_ERR(trans) != -ENOENT)
0b246afa 1910 btrfs_err(fs_info,
da288d28
FM
1911 "cleaner transaction attach returned %ld",
1912 PTR_ERR(trans));
1913 } else {
1914 int ret;
1915
1916 ret = btrfs_commit_transaction(trans, root);
1917 if (ret)
0b246afa 1918 btrfs_err(fs_info,
da288d28
FM
1919 "cleaner open transaction commit returned %d",
1920 ret);
1921 }
1922
a74a4b97
CM
1923 return 0;
1924}
1925
1926static int transaction_kthread(void *arg)
1927{
1928 struct btrfs_root *root = arg;
0b246afa 1929 struct btrfs_fs_info *fs_info = root->fs_info;
a74a4b97
CM
1930 struct btrfs_trans_handle *trans;
1931 struct btrfs_transaction *cur;
8929ecfa 1932 u64 transid;
a74a4b97
CM
1933 unsigned long now;
1934 unsigned long delay;
914b2007 1935 bool cannot_commit;
a74a4b97
CM
1936
1937 do {
914b2007 1938 cannot_commit = false;
0b246afa
JM
1939 delay = HZ * fs_info->commit_interval;
1940 mutex_lock(&fs_info->transaction_kthread_mutex);
a74a4b97 1941
0b246afa
JM
1942 spin_lock(&fs_info->trans_lock);
1943 cur = fs_info->running_transaction;
a74a4b97 1944 if (!cur) {
0b246afa 1945 spin_unlock(&fs_info->trans_lock);
a74a4b97
CM
1946 goto sleep;
1947 }
31153d81 1948
a74a4b97 1949 now = get_seconds();
4a9d8bde 1950 if (cur->state < TRANS_STATE_BLOCKED &&
8b87dc17 1951 (now < cur->start_time ||
0b246afa
JM
1952 now - cur->start_time < fs_info->commit_interval)) {
1953 spin_unlock(&fs_info->trans_lock);
a74a4b97
CM
1954 delay = HZ * 5;
1955 goto sleep;
1956 }
8929ecfa 1957 transid = cur->transid;
0b246afa 1958 spin_unlock(&fs_info->trans_lock);
56bec294 1959
79787eaa 1960 /* If the file system is aborted, this will always fail. */
354aa0fb 1961 trans = btrfs_attach_transaction(root);
914b2007 1962 if (IS_ERR(trans)) {
354aa0fb
MX
1963 if (PTR_ERR(trans) != -ENOENT)
1964 cannot_commit = true;
79787eaa 1965 goto sleep;
914b2007 1966 }
8929ecfa 1967 if (transid == trans->transid) {
79787eaa 1968 btrfs_commit_transaction(trans, root);
8929ecfa
YZ
1969 } else {
1970 btrfs_end_transaction(trans, root);
1971 }
a74a4b97 1972sleep:
0b246afa
JM
1973 wake_up_process(fs_info->cleaner_kthread);
1974 mutex_unlock(&fs_info->transaction_kthread_mutex);
a74a4b97 1975
4e121c06 1976 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
0b246afa 1977 &fs_info->fs_state)))
2ff7e61e 1978 btrfs_cleanup_transaction(fs_info);
ce63f891
JK
1979 set_current_state(TASK_INTERRUPTIBLE);
1980 if (!kthread_should_stop() &&
0b246afa 1981 (!btrfs_transaction_blocked(fs_info) ||
ce63f891
JK
1982 cannot_commit))
1983 schedule_timeout(delay);
1984 __set_current_state(TASK_RUNNING);
a74a4b97
CM
1985 } while (!kthread_should_stop());
1986 return 0;
1987}
1988
af31f5e5
CM
1989/*
1990 * this will find the highest generation in the array of
1991 * root backups. The index of the highest array is returned,
1992 * or -1 if we can't find anything.
1993 *
1994 * We check to make sure the array is valid by comparing the
1995 * generation of the latest root in the array with the generation
1996 * in the super block. If they don't match we pitch it.
1997 */
1998static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
1999{
2000 u64 cur;
2001 int newest_index = -1;
2002 struct btrfs_root_backup *root_backup;
2003 int i;
2004
2005 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
2006 root_backup = info->super_copy->super_roots + i;
2007 cur = btrfs_backup_tree_root_gen(root_backup);
2008 if (cur == newest_gen)
2009 newest_index = i;
2010 }
2011
2012 /* check to see if we actually wrapped around */
2013 if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
2014 root_backup = info->super_copy->super_roots;
2015 cur = btrfs_backup_tree_root_gen(root_backup);
2016 if (cur == newest_gen)
2017 newest_index = 0;
2018 }
2019 return newest_index;
2020}
2021
2022
2023/*
2024 * find the oldest backup so we know where to store new entries
2025 * in the backup array. This will set the backup_root_index
2026 * field in the fs_info struct
2027 */
2028static void find_oldest_super_backup(struct btrfs_fs_info *info,
2029 u64 newest_gen)
2030{
2031 int newest_index = -1;
2032
2033 newest_index = find_newest_super_backup(info, newest_gen);
2034 /* if there was garbage in there, just move along */
2035 if (newest_index == -1) {
2036 info->backup_root_index = 0;
2037 } else {
2038 info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
2039 }
2040}
2041
2042/*
2043 * copy all the root pointers into the super backup array.
2044 * this will bump the backup pointer by one when it is
2045 * done
2046 */
2047static void backup_super_roots(struct btrfs_fs_info *info)
2048{
2049 int next_backup;
2050 struct btrfs_root_backup *root_backup;
2051 int last_backup;
2052
2053 next_backup = info->backup_root_index;
2054 last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) %
2055 BTRFS_NUM_BACKUP_ROOTS;
2056
2057 /*
2058 * just overwrite the last backup if we're at the same generation
2059 * this happens only at umount
2060 */
2061 root_backup = info->super_for_commit->super_roots + last_backup;
2062 if (btrfs_backup_tree_root_gen(root_backup) ==
2063 btrfs_header_generation(info->tree_root->node))
2064 next_backup = last_backup;
2065
2066 root_backup = info->super_for_commit->super_roots + next_backup;
2067
2068 /*
2069 * make sure all of our padding and empty slots get zero filled
2070 * regardless of which ones we use today
2071 */
2072 memset(root_backup, 0, sizeof(*root_backup));
2073
2074 info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
2075
2076 btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
2077 btrfs_set_backup_tree_root_gen(root_backup,
2078 btrfs_header_generation(info->tree_root->node));
2079
2080 btrfs_set_backup_tree_root_level(root_backup,
2081 btrfs_header_level(info->tree_root->node));
2082
2083 btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
2084 btrfs_set_backup_chunk_root_gen(root_backup,
2085 btrfs_header_generation(info->chunk_root->node));
2086 btrfs_set_backup_chunk_root_level(root_backup,
2087 btrfs_header_level(info->chunk_root->node));
2088
2089 btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start);
2090 btrfs_set_backup_extent_root_gen(root_backup,
2091 btrfs_header_generation(info->extent_root->node));
2092 btrfs_set_backup_extent_root_level(root_backup,
2093 btrfs_header_level(info->extent_root->node));
2094
7c7e82a7
CM
2095 /*
2096 * we might commit during log recovery, which happens before we set
2097 * the fs_root. Make sure it is valid before we fill it in.
2098 */
2099 if (info->fs_root && info->fs_root->node) {
2100 btrfs_set_backup_fs_root(root_backup,
2101 info->fs_root->node->start);
2102 btrfs_set_backup_fs_root_gen(root_backup,
af31f5e5 2103 btrfs_header_generation(info->fs_root->node));
7c7e82a7 2104 btrfs_set_backup_fs_root_level(root_backup,
af31f5e5 2105 btrfs_header_level(info->fs_root->node));
7c7e82a7 2106 }
af31f5e5
CM
2107
2108 btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
2109 btrfs_set_backup_dev_root_gen(root_backup,
2110 btrfs_header_generation(info->dev_root->node));
2111 btrfs_set_backup_dev_root_level(root_backup,
2112 btrfs_header_level(info->dev_root->node));
2113
2114 btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
2115 btrfs_set_backup_csum_root_gen(root_backup,
2116 btrfs_header_generation(info->csum_root->node));
2117 btrfs_set_backup_csum_root_level(root_backup,
2118 btrfs_header_level(info->csum_root->node));
2119
2120 btrfs_set_backup_total_bytes(root_backup,
2121 btrfs_super_total_bytes(info->super_copy));
2122 btrfs_set_backup_bytes_used(root_backup,
2123 btrfs_super_bytes_used(info->super_copy));
2124 btrfs_set_backup_num_devices(root_backup,
2125 btrfs_super_num_devices(info->super_copy));
2126
2127 /*
2128 * if we don't copy this out to the super_copy, it won't get remembered
2129 * for the next commit
2130 */
2131 memcpy(&info->super_copy->super_roots,
2132 &info->super_for_commit->super_roots,
2133 sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
2134}
2135
2136/*
2137 * this copies info out of the root backup array and back into
2138 * the in-memory super block. It is meant to help iterate through
2139 * the array, so you send it the number of backups you've already
2140 * tried and the last backup index you used.
2141 *
2142 * this returns -1 when it has tried all the backups
2143 */
2144static noinline int next_root_backup(struct btrfs_fs_info *info,
2145 struct btrfs_super_block *super,
2146 int *num_backups_tried, int *backup_index)
2147{
2148 struct btrfs_root_backup *root_backup;
2149 int newest = *backup_index;
2150
2151 if (*num_backups_tried == 0) {
2152 u64 gen = btrfs_super_generation(super);
2153
2154 newest = find_newest_super_backup(info, gen);
2155 if (newest == -1)
2156 return -1;
2157
2158 *backup_index = newest;
2159 *num_backups_tried = 1;
2160 } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) {
2161 /* we've tried all the backups, all done */
2162 return -1;
2163 } else {
2164 /* jump to the next oldest backup */
2165 newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) %
2166 BTRFS_NUM_BACKUP_ROOTS;
2167 *backup_index = newest;
2168 *num_backups_tried += 1;
2169 }
2170 root_backup = super->super_roots + newest;
2171
2172 btrfs_set_super_generation(super,
2173 btrfs_backup_tree_root_gen(root_backup));
2174 btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
2175 btrfs_set_super_root_level(super,
2176 btrfs_backup_tree_root_level(root_backup));
2177 btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
2178
2179 /*
2180 * fixme: the total bytes and num_devices need to match or we should
2181 * need a fsck
2182 */
2183 btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
2184 btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
2185 return 0;
2186}
2187
7abadb64
LB
2188/* helper to cleanup workers */
2189static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
2190{
dc6e3209 2191 btrfs_destroy_workqueue(fs_info->fixup_workers);
afe3d242 2192 btrfs_destroy_workqueue(fs_info->delalloc_workers);
5cdc7ad3 2193 btrfs_destroy_workqueue(fs_info->workers);
fccb5d86
QW
2194 btrfs_destroy_workqueue(fs_info->endio_workers);
2195 btrfs_destroy_workqueue(fs_info->endio_meta_workers);
2196 btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
8b110e39 2197 btrfs_destroy_workqueue(fs_info->endio_repair_workers);
d05a33ac 2198 btrfs_destroy_workqueue(fs_info->rmw_workers);
fccb5d86
QW
2199 btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
2200 btrfs_destroy_workqueue(fs_info->endio_write_workers);
2201 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
a8c93d4e 2202 btrfs_destroy_workqueue(fs_info->submit_workers);
5b3bc44e 2203 btrfs_destroy_workqueue(fs_info->delayed_workers);
e66f0bb1 2204 btrfs_destroy_workqueue(fs_info->caching_workers);
736cfa15 2205 btrfs_destroy_workqueue(fs_info->readahead_workers);
a44903ab 2206 btrfs_destroy_workqueue(fs_info->flush_workers);
fc97fab0 2207 btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
a79b7d4b 2208 btrfs_destroy_workqueue(fs_info->extent_workers);
7abadb64
LB
2209}
2210
2e9f5954
R
2211static void free_root_extent_buffers(struct btrfs_root *root)
2212{
2213 if (root) {
2214 free_extent_buffer(root->node);
2215 free_extent_buffer(root->commit_root);
2216 root->node = NULL;
2217 root->commit_root = NULL;
2218 }
2219}
2220
af31f5e5
CM
2221/* helper to cleanup tree roots */
2222static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
2223{
2e9f5954 2224 free_root_extent_buffers(info->tree_root);
655b09fe 2225
2e9f5954
R
2226 free_root_extent_buffers(info->dev_root);
2227 free_root_extent_buffers(info->extent_root);
2228 free_root_extent_buffers(info->csum_root);
2229 free_root_extent_buffers(info->quota_root);
2230 free_root_extent_buffers(info->uuid_root);
2231 if (chunk_root)
2232 free_root_extent_buffers(info->chunk_root);
70f6d82e 2233 free_root_extent_buffers(info->free_space_root);
af31f5e5
CM
2234}
2235
faa2dbf0 2236void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
171f6537
JB
2237{
2238 int ret;
2239 struct btrfs_root *gang[8];
2240 int i;
2241
2242 while (!list_empty(&fs_info->dead_roots)) {
2243 gang[0] = list_entry(fs_info->dead_roots.next,
2244 struct btrfs_root, root_list);
2245 list_del(&gang[0]->root_list);
2246
27cdeb70 2247 if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
cb517eab 2248 btrfs_drop_and_free_fs_root(fs_info, gang[0]);
171f6537
JB
2249 } else {
2250 free_extent_buffer(gang[0]->node);
2251 free_extent_buffer(gang[0]->commit_root);
b0feb9d9 2252 btrfs_put_fs_root(gang[0]);
171f6537
JB
2253 }
2254 }
2255
2256 while (1) {
2257 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2258 (void **)gang, 0,
2259 ARRAY_SIZE(gang));
2260 if (!ret)
2261 break;
2262 for (i = 0; i < ret; i++)
cb517eab 2263 btrfs_drop_and_free_fs_root(fs_info, gang[i]);
171f6537 2264 }
1a4319cc
LB
2265
2266 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
2267 btrfs_free_log_root_tree(NULL, fs_info);
2ff7e61e 2268 btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
1a4319cc 2269 }
171f6537 2270}
af31f5e5 2271
638aa7ed
ES
2272static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
2273{
2274 mutex_init(&fs_info->scrub_lock);
2275 atomic_set(&fs_info->scrubs_running, 0);
2276 atomic_set(&fs_info->scrub_pause_req, 0);
2277 atomic_set(&fs_info->scrubs_paused, 0);
2278 atomic_set(&fs_info->scrub_cancel_req, 0);
2279 init_waitqueue_head(&fs_info->scrub_pause_wait);
2280 fs_info->scrub_workers_refcnt = 0;
2281}
2282
779a65a4
ES
2283static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
2284{
2285 spin_lock_init(&fs_info->balance_lock);
2286 mutex_init(&fs_info->balance_mutex);
2287 atomic_set(&fs_info->balance_running, 0);
2288 atomic_set(&fs_info->balance_pause_req, 0);
2289 atomic_set(&fs_info->balance_cancel_req, 0);
2290 fs_info->balance_ctl = NULL;
2291 init_waitqueue_head(&fs_info->balance_wait_q);
2292}
2293
6bccf3ab 2294static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
f37938e0 2295{
2ff7e61e
JM
2296 struct inode *inode = fs_info->btree_inode;
2297
2298 inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
2299 set_nlink(inode, 1);
f37938e0
ES
2300 /*
2301 * we set the i_size on the btree inode to the max possible int.
2302 * the real end of the address space is determined by all of
2303 * the devices in the system
2304 */
2ff7e61e
JM
2305 inode->i_size = OFFSET_MAX;
2306 inode->i_mapping->a_ops = &btree_aops;
f37938e0 2307
2ff7e61e
JM
2308 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
2309 extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode->i_mapping);
2310 BTRFS_I(inode)->io_tree.track_uptodate = 0;
2311 extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
f37938e0 2312
2ff7e61e 2313 BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops;
f37938e0 2314
2ff7e61e
JM
2315 BTRFS_I(inode)->root = fs_info->tree_root;
2316 memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key));
2317 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
2318 btrfs_insert_inode_hash(inode);
f37938e0
ES
2319}
2320
ad618368
ES
2321static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
2322{
2323 fs_info->dev_replace.lock_owner = 0;
2324 atomic_set(&fs_info->dev_replace.nesting_level, 0);
2325 mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
73beece9
LB
2326 rwlock_init(&fs_info->dev_replace.lock);
2327 atomic_set(&fs_info->dev_replace.read_locks, 0);
2328 atomic_set(&fs_info->dev_replace.blocking_readers, 0);
ad618368 2329 init_waitqueue_head(&fs_info->replace_wait);
73beece9 2330 init_waitqueue_head(&fs_info->dev_replace.read_lock_wq);
ad618368
ES
2331}
2332
f9e92e40
ES
2333static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
2334{
2335 spin_lock_init(&fs_info->qgroup_lock);
2336 mutex_init(&fs_info->qgroup_ioctl_lock);
2337 fs_info->qgroup_tree = RB_ROOT;
2338 fs_info->qgroup_op_tree = RB_ROOT;
2339 INIT_LIST_HEAD(&fs_info->dirty_qgroups);
2340 fs_info->qgroup_seq = 1;
f9e92e40 2341 fs_info->qgroup_ulist = NULL;
d2c609b8 2342 fs_info->qgroup_rescan_running = false;
f9e92e40
ES
2343 mutex_init(&fs_info->qgroup_rescan_lock);
2344}
2345
2a458198
ES
2346static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
2347 struct btrfs_fs_devices *fs_devices)
2348{
2349 int max_active = fs_info->thread_pool_size;
6f011058 2350 unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
2a458198
ES
2351
2352 fs_info->workers =
cb001095
JM
2353 btrfs_alloc_workqueue(fs_info, "worker",
2354 flags | WQ_HIGHPRI, max_active, 16);
2a458198
ES
2355
2356 fs_info->delalloc_workers =
cb001095
JM
2357 btrfs_alloc_workqueue(fs_info, "delalloc",
2358 flags, max_active, 2);
2a458198
ES
2359
2360 fs_info->flush_workers =
cb001095
JM
2361 btrfs_alloc_workqueue(fs_info, "flush_delalloc",
2362 flags, max_active, 0);
2a458198
ES
2363
2364 fs_info->caching_workers =
cb001095 2365 btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
2a458198
ES
2366
2367 /*
2368 * a higher idle thresh on the submit workers makes it much more
2369 * likely that bios will be send down in a sane order to the
2370 * devices
2371 */
2372 fs_info->submit_workers =
cb001095 2373 btrfs_alloc_workqueue(fs_info, "submit", flags,
2a458198
ES
2374 min_t(u64, fs_devices->num_devices,
2375 max_active), 64);
2376
2377 fs_info->fixup_workers =
cb001095 2378 btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0);
2a458198
ES
2379
2380 /*
2381 * endios are largely parallel and should have a very
2382 * low idle thresh
2383 */
2384 fs_info->endio_workers =
cb001095 2385 btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4);
2a458198 2386 fs_info->endio_meta_workers =
cb001095
JM
2387 btrfs_alloc_workqueue(fs_info, "endio-meta", flags,
2388 max_active, 4);
2a458198 2389 fs_info->endio_meta_write_workers =
cb001095
JM
2390 btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags,
2391 max_active, 2);
2a458198 2392 fs_info->endio_raid56_workers =
cb001095
JM
2393 btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
2394 max_active, 4);
2a458198 2395 fs_info->endio_repair_workers =
cb001095 2396 btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0);
2a458198 2397 fs_info->rmw_workers =
cb001095 2398 btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);
2a458198 2399 fs_info->endio_write_workers =
cb001095
JM
2400 btrfs_alloc_workqueue(fs_info, "endio-write", flags,
2401 max_active, 2);
2a458198 2402 fs_info->endio_freespace_worker =
cb001095
JM
2403 btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
2404 max_active, 0);
2a458198 2405 fs_info->delayed_workers =
cb001095
JM
2406 btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2407 max_active, 0);
2a458198 2408 fs_info->readahead_workers =
cb001095
JM
2409 btrfs_alloc_workqueue(fs_info, "readahead", flags,
2410 max_active, 2);
2a458198 2411 fs_info->qgroup_rescan_workers =
cb001095 2412 btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0);
2a458198 2413 fs_info->extent_workers =
cb001095 2414 btrfs_alloc_workqueue(fs_info, "extent-refs", flags,
2a458198
ES
2415 min_t(u64, fs_devices->num_devices,
2416 max_active), 8);
2417
2418 if (!(fs_info->workers && fs_info->delalloc_workers &&
2419 fs_info->submit_workers && fs_info->flush_workers &&
2420 fs_info->endio_workers && fs_info->endio_meta_workers &&
2421 fs_info->endio_meta_write_workers &&
2422 fs_info->endio_repair_workers &&
2423 fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
2424 fs_info->endio_freespace_worker && fs_info->rmw_workers &&
2425 fs_info->caching_workers && fs_info->readahead_workers &&
2426 fs_info->fixup_workers && fs_info->delayed_workers &&
2427 fs_info->extent_workers &&
2428 fs_info->qgroup_rescan_workers)) {
2429 return -ENOMEM;
2430 }
2431
2432 return 0;
2433}
2434
63443bf5
ES
2435static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2436 struct btrfs_fs_devices *fs_devices)
2437{
2438 int ret;
63443bf5
ES
2439 struct btrfs_root *log_tree_root;
2440 struct btrfs_super_block *disk_super = fs_info->super_copy;
2441 u64 bytenr = btrfs_super_log_root(disk_super);
2442
2443 if (fs_devices->rw_devices == 0) {
f14d104d 2444 btrfs_warn(fs_info, "log replay required on RO media");
63443bf5
ES
2445 return -EIO;
2446 }
2447
74e4d827 2448 log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
63443bf5
ES
2449 if (!log_tree_root)
2450 return -ENOMEM;
2451
da17066c 2452 __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
63443bf5 2453
2ff7e61e
JM
2454 log_tree_root->node = read_tree_block(fs_info, bytenr,
2455 fs_info->generation + 1);
64c043de 2456 if (IS_ERR(log_tree_root->node)) {
f14d104d 2457 btrfs_warn(fs_info, "failed to read log tree");
0eeff236 2458 ret = PTR_ERR(log_tree_root->node);
64c043de 2459 kfree(log_tree_root);
0eeff236 2460 return ret;
64c043de 2461 } else if (!extent_buffer_uptodate(log_tree_root->node)) {
f14d104d 2462 btrfs_err(fs_info, "failed to read log tree");
63443bf5
ES
2463 free_extent_buffer(log_tree_root->node);
2464 kfree(log_tree_root);
2465 return -EIO;
2466 }
2467 /* returns with log_tree_root freed on success */
2468 ret = btrfs_recover_log_trees(log_tree_root);
2469 if (ret) {
0b246afa
JM
2470 btrfs_handle_fs_error(fs_info, ret,
2471 "Failed to recover log tree");
63443bf5
ES
2472 free_extent_buffer(log_tree_root->node);
2473 kfree(log_tree_root);
2474 return ret;
2475 }
2476
2477 if (fs_info->sb->s_flags & MS_RDONLY) {
6bccf3ab 2478 ret = btrfs_commit_super(fs_info);
63443bf5
ES
2479 if (ret)
2480 return ret;
2481 }
2482
2483 return 0;
2484}
2485
6bccf3ab 2486static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
4bbcaa64 2487{
6bccf3ab 2488 struct btrfs_root *tree_root = fs_info->tree_root;
a4f3d2c4 2489 struct btrfs_root *root;
4bbcaa64
ES
2490 struct btrfs_key location;
2491 int ret;
2492
6bccf3ab
JM
2493 BUG_ON(!fs_info->tree_root);
2494
4bbcaa64
ES
2495 location.objectid = BTRFS_EXTENT_TREE_OBJECTID;
2496 location.type = BTRFS_ROOT_ITEM_KEY;
2497 location.offset = 0;
2498
a4f3d2c4
DS
2499 root = btrfs_read_tree_root(tree_root, &location);
2500 if (IS_ERR(root))
2501 return PTR_ERR(root);
2502 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2503 fs_info->extent_root = root;
4bbcaa64
ES
2504
2505 location.objectid = BTRFS_DEV_TREE_OBJECTID;
a4f3d2c4
DS
2506 root = btrfs_read_tree_root(tree_root, &location);
2507 if (IS_ERR(root))
2508 return PTR_ERR(root);
2509 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2510 fs_info->dev_root = root;
4bbcaa64
ES
2511 btrfs_init_devices_late(fs_info);
2512
2513 location.objectid = BTRFS_CSUM_TREE_OBJECTID;
a4f3d2c4
DS
2514 root = btrfs_read_tree_root(tree_root, &location);
2515 if (IS_ERR(root))
2516 return PTR_ERR(root);
2517 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2518 fs_info->csum_root = root;
4bbcaa64
ES
2519
2520 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
a4f3d2c4
DS
2521 root = btrfs_read_tree_root(tree_root, &location);
2522 if (!IS_ERR(root)) {
2523 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
afcdd129 2524 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
a4f3d2c4 2525 fs_info->quota_root = root;
4bbcaa64
ES
2526 }
2527
2528 location.objectid = BTRFS_UUID_TREE_OBJECTID;
a4f3d2c4
DS
2529 root = btrfs_read_tree_root(tree_root, &location);
2530 if (IS_ERR(root)) {
2531 ret = PTR_ERR(root);
4bbcaa64
ES
2532 if (ret != -ENOENT)
2533 return ret;
2534 } else {
a4f3d2c4
DS
2535 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2536 fs_info->uuid_root = root;
4bbcaa64
ES
2537 }
2538
70f6d82e
OS
2539 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2540 location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID;
2541 root = btrfs_read_tree_root(tree_root, &location);
2542 if (IS_ERR(root))
2543 return PTR_ERR(root);
2544 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2545 fs_info->free_space_root = root;
2546 }
2547
4bbcaa64
ES
2548 return 0;
2549}
2550
ad2b2c80
AV
2551int open_ctree(struct super_block *sb,
2552 struct btrfs_fs_devices *fs_devices,
2553 char *options)
2e635a27 2554{
db94535d
CM
2555 u32 sectorsize;
2556 u32 nodesize;
87ee04eb 2557 u32 stripesize;
84234f3a 2558 u64 generation;
f2b636e8 2559 u64 features;
3de4586c 2560 struct btrfs_key location;
a061fc8d 2561 struct buffer_head *bh;
4d34b278 2562 struct btrfs_super_block *disk_super;
815745cf 2563 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
f84a8bd6 2564 struct btrfs_root *tree_root;
4d34b278 2565 struct btrfs_root *chunk_root;
eb60ceac 2566 int ret;
e58ca020 2567 int err = -EINVAL;
af31f5e5
CM
2568 int num_backups_tried = 0;
2569 int backup_index = 0;
5cdc7ad3 2570 int max_active;
6675df31 2571 int clear_free_space_tree = 0;
4543df7e 2572
74e4d827
DS
2573 tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
2574 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
cb517eab 2575 if (!tree_root || !chunk_root) {
39279cc3
CM
2576 err = -ENOMEM;
2577 goto fail;
2578 }
76dda93c
YZ
2579
2580 ret = init_srcu_struct(&fs_info->subvol_srcu);
2581 if (ret) {
2582 err = ret;
2583 goto fail;
2584 }
2585
2586 ret = setup_bdi(fs_info, &fs_info->bdi);
2587 if (ret) {
2588 err = ret;
2589 goto fail_srcu;
2590 }
2591
908c7f19 2592 ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
e2d84521
MX
2593 if (ret) {
2594 err = ret;
2595 goto fail_bdi;
2596 }
09cbfeaf 2597 fs_info->dirty_metadata_batch = PAGE_SIZE *
e2d84521
MX
2598 (1 + ilog2(nr_cpu_ids));
2599
908c7f19 2600 ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
963d678b
MX
2601 if (ret) {
2602 err = ret;
2603 goto fail_dirty_metadata_bytes;
2604 }
2605
908c7f19 2606 ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL);
c404e0dc
MX
2607 if (ret) {
2608 err = ret;
2609 goto fail_delalloc_bytes;
2610 }
2611
76dda93c
YZ
2612 fs_info->btree_inode = new_inode(sb);
2613 if (!fs_info->btree_inode) {
2614 err = -ENOMEM;
c404e0dc 2615 goto fail_bio_counter;
76dda93c
YZ
2616 }
2617
a6591715 2618 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
1561deda 2619
76dda93c 2620 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
f28491e0 2621 INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
8fd17795 2622 INIT_LIST_HEAD(&fs_info->trans_list);
facda1e7 2623 INIT_LIST_HEAD(&fs_info->dead_roots);
24bbcf04 2624 INIT_LIST_HEAD(&fs_info->delayed_iputs);
eb73c1b7 2625 INIT_LIST_HEAD(&fs_info->delalloc_roots);
11833d66 2626 INIT_LIST_HEAD(&fs_info->caching_block_groups);
eb73c1b7 2627 spin_lock_init(&fs_info->delalloc_root_lock);
a4abeea4 2628 spin_lock_init(&fs_info->trans_lock);
76dda93c 2629 spin_lock_init(&fs_info->fs_roots_radix_lock);
24bbcf04 2630 spin_lock_init(&fs_info->delayed_iput_lock);
4cb5300b 2631 spin_lock_init(&fs_info->defrag_inodes_lock);
2bf64758 2632 spin_lock_init(&fs_info->free_chunk_lock);
f29021b2 2633 spin_lock_init(&fs_info->tree_mod_seq_lock);
ceda0864 2634 spin_lock_init(&fs_info->super_lock);
fcebe456 2635 spin_lock_init(&fs_info->qgroup_op_lock);
f28491e0 2636 spin_lock_init(&fs_info->buffer_lock);
47ab2a6c 2637 spin_lock_init(&fs_info->unused_bgs_lock);
f29021b2 2638 rwlock_init(&fs_info->tree_mod_log_lock);
d7c15171 2639 mutex_init(&fs_info->unused_bg_unpin_mutex);
67c5e7d4 2640 mutex_init(&fs_info->delete_unused_bgs_mutex);
7585717f 2641 mutex_init(&fs_info->reloc_mutex);
573bfb72 2642 mutex_init(&fs_info->delalloc_root_mutex);
c2d6cb16 2643 mutex_init(&fs_info->cleaner_delayed_iput_mutex);
de98ced9 2644 seqlock_init(&fs_info->profiles_lock);
19c00ddc 2645
0b86a832 2646 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
6324fbf3 2647 INIT_LIST_HEAD(&fs_info->space_info);
f29021b2 2648 INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
47ab2a6c 2649 INIT_LIST_HEAD(&fs_info->unused_bgs);
0b86a832 2650 btrfs_mapping_init(&fs_info->mapping_tree);
66d8f3dd
MX
2651 btrfs_init_block_rsv(&fs_info->global_block_rsv,
2652 BTRFS_BLOCK_RSV_GLOBAL);
2653 btrfs_init_block_rsv(&fs_info->delalloc_block_rsv,
2654 BTRFS_BLOCK_RSV_DELALLOC);
2655 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
2656 btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
2657 btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
2658 btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
2659 BTRFS_BLOCK_RSV_DELOPS);
cb03c743 2660 atomic_set(&fs_info->nr_async_submits, 0);
771ed689 2661 atomic_set(&fs_info->async_delalloc_pages, 0);
8c8bee1d 2662 atomic_set(&fs_info->async_submit_draining, 0);
0986fe9e 2663 atomic_set(&fs_info->nr_async_bios, 0);
4cb5300b 2664 atomic_set(&fs_info->defrag_running, 0);
fcebe456 2665 atomic_set(&fs_info->qgroup_op_seq, 0);
2fefd558 2666 atomic_set(&fs_info->reada_works_cnt, 0);
fc36ed7e 2667 atomic64_set(&fs_info->tree_mod_seq, 0);
9e7cc91a 2668 fs_info->fs_frozen = 0;
e20d96d6 2669 fs_info->sb = sb;
95ac567a 2670 fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
9ed74f2d 2671 fs_info->metadata_ratio = 0;
4cb5300b 2672 fs_info->defrag_inodes = RB_ROOT;
2bf64758 2673 fs_info->free_chunk_space = 0;
f29021b2 2674 fs_info->tree_mod_log = RB_ROOT;
8b87dc17 2675 fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
f8c269d7 2676 fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */
90519d66 2677 /* readahead state */
d0164adc 2678 INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
90519d66 2679 spin_lock_init(&fs_info->reada_lock);
c8b97818 2680
b34b086c
CM
2681 fs_info->thread_pool_size = min_t(unsigned long,
2682 num_online_cpus() + 2, 8);
0afbaf8c 2683
199c2a9c
MX
2684 INIT_LIST_HEAD(&fs_info->ordered_roots);
2685 spin_lock_init(&fs_info->ordered_root_lock);
16cdcec7 2686 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
74e4d827 2687 GFP_KERNEL);
16cdcec7
MX
2688 if (!fs_info->delayed_root) {
2689 err = -ENOMEM;
2690 goto fail_iput;
2691 }
2692 btrfs_init_delayed_root(fs_info->delayed_root);
3eaa2885 2693
638aa7ed 2694 btrfs_init_scrub(fs_info);
21adbd5c
SB
2695#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2696 fs_info->check_integrity_print_mask = 0;
2697#endif
779a65a4 2698 btrfs_init_balance(fs_info);
21c7e756 2699 btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
a2de733c 2700
a061fc8d
CM
2701 sb->s_blocksize = 4096;
2702 sb->s_blocksize_bits = blksize_bits(4096);
32a88aa1 2703 sb->s_bdi = &fs_info->bdi;
a061fc8d 2704
6bccf3ab 2705 btrfs_init_btree_inode(fs_info);
76dda93c 2706
0f9dd46c 2707 spin_lock_init(&fs_info->block_group_cache_lock);
6bef4d31 2708 fs_info->block_group_cache_tree = RB_ROOT;
a1897fdd 2709 fs_info->first_logical_byte = (u64)-1;
0f9dd46c 2710
11833d66 2711 extent_io_tree_init(&fs_info->freed_extents[0],
f993c883 2712 fs_info->btree_inode->i_mapping);
11833d66 2713 extent_io_tree_init(&fs_info->freed_extents[1],
f993c883 2714 fs_info->btree_inode->i_mapping);
11833d66 2715 fs_info->pinned_extents = &fs_info->freed_extents[0];
afcdd129 2716 set_bit(BTRFS_FS_BARRIER, &fs_info->flags);
39279cc3 2717
5a3f23d5 2718 mutex_init(&fs_info->ordered_operations_mutex);
e02119d5 2719 mutex_init(&fs_info->tree_log_mutex);
925baedd 2720 mutex_init(&fs_info->chunk_mutex);
a74a4b97
CM
2721 mutex_init(&fs_info->transaction_kthread_mutex);
2722 mutex_init(&fs_info->cleaner_mutex);
7d9eb12c 2723 mutex_init(&fs_info->volume_mutex);
1bbc621e 2724 mutex_init(&fs_info->ro_block_group_mutex);
9e351cc8 2725 init_rwsem(&fs_info->commit_root_sem);
c71bf099 2726 init_rwsem(&fs_info->cleanup_work_sem);
76dda93c 2727 init_rwsem(&fs_info->subvol_sem);
803b2f54 2728 sema_init(&fs_info->uuid_tree_rescan_sem, 1);
fa9c0d79 2729
ad618368 2730 btrfs_init_dev_replace_locks(fs_info);
f9e92e40 2731 btrfs_init_qgroup(fs_info);
416ac51d 2732
fa9c0d79
CM
2733 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2734 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2735
e6dcd2dc 2736 init_waitqueue_head(&fs_info->transaction_throttle);
f9295749 2737 init_waitqueue_head(&fs_info->transaction_wait);
bb9c12c9 2738 init_waitqueue_head(&fs_info->transaction_blocked_wait);
4854ddd0 2739 init_waitqueue_head(&fs_info->async_submit_wait);
3768f368 2740
04216820
FM
2741 INIT_LIST_HEAD(&fs_info->pinned_chunks);
2742
da17066c
JM
2743 /* Usable values until the real ones are cached from the superblock */
2744 fs_info->nodesize = 4096;
2745 fs_info->sectorsize = 4096;
2746 fs_info->stripesize = 4096;
2747
53b381b3
DW
2748 ret = btrfs_alloc_stripe_hash_table(fs_info);
2749 if (ret) {
83c8266a 2750 err = ret;
53b381b3
DW
2751 goto fail_alloc;
2752 }
2753
da17066c 2754 __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
7eccb903 2755
3c4bb26b 2756 invalidate_bdev(fs_devices->latest_bdev);
1104a885
DS
2757
2758 /*
2759 * Read super block and check the signature bytes only
2760 */
a512bbf8 2761 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
92fc03fb
AJ
2762 if (IS_ERR(bh)) {
2763 err = PTR_ERR(bh);
16cdcec7 2764 goto fail_alloc;
20b45077 2765 }
39279cc3 2766
1104a885
DS
2767 /*
2768 * We want to check superblock checksum, the type is stored inside.
2769 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
2770 */
ab8d0fc4 2771 if (btrfs_check_super_csum(fs_info, bh->b_data)) {
05135f59 2772 btrfs_err(fs_info, "superblock checksum mismatch");
1104a885 2773 err = -EINVAL;
b2acdddf 2774 brelse(bh);
1104a885
DS
2775 goto fail_alloc;
2776 }
2777
2778 /*
2779 * super_copy is zeroed at allocation time and we never touch the
2780 * following bytes up to INFO_SIZE, the checksum is calculated from
2781 * the whole block of INFO_SIZE
2782 */
6c41761f
DS
2783 memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy));
2784 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2785 sizeof(*fs_info->super_for_commit));
a061fc8d 2786 brelse(bh);
5f39d397 2787
6c41761f 2788 memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
0b86a832 2789
1104a885
DS
2790 ret = btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY);
2791 if (ret) {
05135f59 2792 btrfs_err(fs_info, "superblock contains fatal errors");
1104a885
DS
2793 err = -EINVAL;
2794 goto fail_alloc;
2795 }
2796
6c41761f 2797 disk_super = fs_info->super_copy;
0f7d52f4 2798 if (!btrfs_super_root(disk_super))
16cdcec7 2799 goto fail_alloc;
0f7d52f4 2800
acce952b 2801 /* check FS state, whether FS is broken. */
87533c47
MX
2802 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
2803 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
acce952b 2804
af31f5e5
CM
2805 /*
2806 * run through our array of backup supers and setup
2807 * our ring pointer to the oldest one
2808 */
2809 generation = btrfs_super_generation(disk_super);
2810 find_oldest_super_backup(fs_info, generation);
2811
75e7cb7f
LB
2812 /*
2813 * In the long term, we'll store the compression type in the super
2814 * block, and it'll be used for per file compression control.
2815 */
2816 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2817
2ff7e61e 2818 ret = btrfs_parse_options(fs_info, options, sb->s_flags);
2b82032c
YZ
2819 if (ret) {
2820 err = ret;
16cdcec7 2821 goto fail_alloc;
2b82032c 2822 }
dfe25020 2823
f2b636e8
JB
2824 features = btrfs_super_incompat_flags(disk_super) &
2825 ~BTRFS_FEATURE_INCOMPAT_SUPP;
2826 if (features) {
05135f59
DS
2827 btrfs_err(fs_info,
2828 "cannot mount because of unsupported optional features (%llx)",
2829 features);
f2b636e8 2830 err = -EINVAL;
16cdcec7 2831 goto fail_alloc;
f2b636e8
JB
2832 }
2833
5d4f98a2 2834 features = btrfs_super_incompat_flags(disk_super);
a6fa6fae 2835 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
0b246afa 2836 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
a6fa6fae 2837 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
727011e0 2838
3173a18f 2839 if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
05135f59 2840 btrfs_info(fs_info, "has skinny extents");
3173a18f 2841
727011e0
CM
2842 /*
2843 * flag our filesystem as having big metadata blocks if
2844 * they are bigger than the page size
2845 */
09cbfeaf 2846 if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) {
727011e0 2847 if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
05135f59
DS
2848 btrfs_info(fs_info,
2849 "flagging fs with big metadata feature");
727011e0
CM
2850 features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
2851 }
2852
bc3f116f 2853 nodesize = btrfs_super_nodesize(disk_super);
bc3f116f 2854 sectorsize = btrfs_super_sectorsize(disk_super);
b7f67055 2855 stripesize = sectorsize;
707e8a07 2856 fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
963d678b 2857 fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
bc3f116f 2858
da17066c
JM
2859 /* Cache block sizes */
2860 fs_info->nodesize = nodesize;
2861 fs_info->sectorsize = sectorsize;
2862 fs_info->stripesize = stripesize;
2863
bc3f116f
CM
2864 /*
2865 * mixed block groups end up with duplicate but slightly offset
2866 * extent buffers for the same range. It leads to corruptions
2867 */
2868 if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
707e8a07 2869 (sectorsize != nodesize)) {
05135f59
DS
2870 btrfs_err(fs_info,
2871"unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
2872 nodesize, sectorsize);
bc3f116f
CM
2873 goto fail_alloc;
2874 }
2875
ceda0864
MX
2876 /*
2877 * Needn't use the lock because there is no other task which will
2878 * update the flag.
2879 */
a6fa6fae 2880 btrfs_set_super_incompat_flags(disk_super, features);
5d4f98a2 2881
f2b636e8
JB
2882 features = btrfs_super_compat_ro_flags(disk_super) &
2883 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
2884 if (!(sb->s_flags & MS_RDONLY) && features) {
05135f59
DS
2885 btrfs_err(fs_info,
2886 "cannot mount read-write because of unsupported optional features (%llx)",
c1c9ff7c 2887 features);
f2b636e8 2888 err = -EINVAL;
16cdcec7 2889 goto fail_alloc;
f2b636e8 2890 }
61d92c32 2891
5cdc7ad3 2892 max_active = fs_info->thread_pool_size;
61d92c32 2893
2a458198
ES
2894 ret = btrfs_init_workqueues(fs_info, fs_devices);
2895 if (ret) {
2896 err = ret;
0dc3b84a
JB
2897 goto fail_sb_buffer;
2898 }
4543df7e 2899
4575c9cc 2900 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
c8b97818 2901 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
09cbfeaf 2902 SZ_4M / PAGE_SIZE);
4575c9cc 2903
a061fc8d
CM
2904 sb->s_blocksize = sectorsize;
2905 sb->s_blocksize_bits = blksize_bits(sectorsize);
db94535d 2906
925baedd 2907 mutex_lock(&fs_info->chunk_mutex);
6bccf3ab 2908 ret = btrfs_read_sys_array(fs_info);
925baedd 2909 mutex_unlock(&fs_info->chunk_mutex);
84eed90f 2910 if (ret) {
05135f59 2911 btrfs_err(fs_info, "failed to read the system array: %d", ret);
5d4f98a2 2912 goto fail_sb_buffer;
84eed90f 2913 }
0b86a832 2914
84234f3a 2915 generation = btrfs_super_chunk_root_generation(disk_super);
0b86a832 2916
da17066c 2917 __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
0b86a832 2918
2ff7e61e 2919 chunk_root->node = read_tree_block(fs_info,
0b86a832 2920 btrfs_super_chunk_root(disk_super),
ce86cd59 2921 generation);
64c043de
LB
2922 if (IS_ERR(chunk_root->node) ||
2923 !extent_buffer_uptodate(chunk_root->node)) {
05135f59 2924 btrfs_err(fs_info, "failed to read chunk root");
e5fffbac 2925 if (!IS_ERR(chunk_root->node))
2926 free_extent_buffer(chunk_root->node);
95ab1f64 2927 chunk_root->node = NULL;
af31f5e5 2928 goto fail_tree_roots;
83121942 2929 }
5d4f98a2
YZ
2930 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
2931 chunk_root->commit_root = btrfs_root_node(chunk_root);
0b86a832 2932
e17cade2 2933 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
b308bc2f 2934 btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE);
e17cade2 2935
5b4aacef 2936 ret = btrfs_read_chunk_tree(fs_info);
2b82032c 2937 if (ret) {
05135f59 2938 btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
af31f5e5 2939 goto fail_tree_roots;
2b82032c 2940 }
0b86a832 2941
8dabb742
SB
2942 /*
2943 * keep the device that is marked to be the target device for the
2944 * dev_replace procedure
2945 */
9eaed21e 2946 btrfs_close_extra_devices(fs_devices, 0);
dfe25020 2947
a6b0d5c8 2948 if (!fs_devices->latest_bdev) {
05135f59 2949 btrfs_err(fs_info, "failed to read devices");
a6b0d5c8
CM
2950 goto fail_tree_roots;
2951 }
2952
af31f5e5 2953retry_root_backup:
84234f3a 2954 generation = btrfs_super_generation(disk_super);
0b86a832 2955
2ff7e61e 2956 tree_root->node = read_tree_block(fs_info,
db94535d 2957 btrfs_super_root(disk_super),
ce86cd59 2958 generation);
64c043de
LB
2959 if (IS_ERR(tree_root->node) ||
2960 !extent_buffer_uptodate(tree_root->node)) {
05135f59 2961 btrfs_warn(fs_info, "failed to read tree root");
e5fffbac 2962 if (!IS_ERR(tree_root->node))
2963 free_extent_buffer(tree_root->node);
95ab1f64 2964 tree_root->node = NULL;
af31f5e5 2965 goto recovery_tree_root;
83121942 2966 }
af31f5e5 2967
5d4f98a2
YZ
2968 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
2969 tree_root->commit_root = btrfs_root_node(tree_root);
69e9c6c6 2970 btrfs_set_root_refs(&tree_root->root_item, 1);
db94535d 2971
f32e48e9
CR
2972 mutex_lock(&tree_root->objectid_mutex);
2973 ret = btrfs_find_highest_objectid(tree_root,
2974 &tree_root->highest_objectid);
2975 if (ret) {
2976 mutex_unlock(&tree_root->objectid_mutex);
2977 goto recovery_tree_root;
2978 }
2979
2980 ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
2981
2982 mutex_unlock(&tree_root->objectid_mutex);
2983
6bccf3ab 2984 ret = btrfs_read_roots(fs_info);
4bbcaa64 2985 if (ret)
af31f5e5 2986 goto recovery_tree_root;
f7a81ea4 2987
8929ecfa
YZ
2988 fs_info->generation = generation;
2989 fs_info->last_trans_committed = generation;
8929ecfa 2990
68310a5e
ID
2991 ret = btrfs_recover_balance(fs_info);
2992 if (ret) {
05135f59 2993 btrfs_err(fs_info, "failed to recover balance: %d", ret);
68310a5e
ID
2994 goto fail_block_groups;
2995 }
2996
733f4fbb
SB
2997 ret = btrfs_init_dev_stats(fs_info);
2998 if (ret) {
05135f59 2999 btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
733f4fbb
SB
3000 goto fail_block_groups;
3001 }
3002
8dabb742
SB
3003 ret = btrfs_init_dev_replace(fs_info);
3004 if (ret) {
05135f59 3005 btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
8dabb742
SB
3006 goto fail_block_groups;
3007 }
3008
9eaed21e 3009 btrfs_close_extra_devices(fs_devices, 1);
8dabb742 3010
b7c35e81
AJ
3011 ret = btrfs_sysfs_add_fsid(fs_devices, NULL);
3012 if (ret) {
05135f59
DS
3013 btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
3014 ret);
b7c35e81
AJ
3015 goto fail_block_groups;
3016 }
3017
3018 ret = btrfs_sysfs_add_device(fs_devices);
3019 if (ret) {
05135f59
DS
3020 btrfs_err(fs_info, "failed to init sysfs device interface: %d",
3021 ret);
b7c35e81
AJ
3022 goto fail_fsdev_sysfs;
3023 }
3024
96f3136e 3025 ret = btrfs_sysfs_add_mounted(fs_info);
c59021f8 3026 if (ret) {
05135f59 3027 btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
b7c35e81 3028 goto fail_fsdev_sysfs;
c59021f8 3029 }
3030
c59021f8 3031 ret = btrfs_init_space_info(fs_info);
3032 if (ret) {
05135f59 3033 btrfs_err(fs_info, "failed to initialize space info: %d", ret);
2365dd3c 3034 goto fail_sysfs;
c59021f8 3035 }
3036
5b4aacef 3037 ret = btrfs_read_block_groups(fs_info);
1b1d1f66 3038 if (ret) {
05135f59 3039 btrfs_err(fs_info, "failed to read block groups: %d", ret);
2365dd3c 3040 goto fail_sysfs;
1b1d1f66 3041 }
5af3e8cc
SB
3042 fs_info->num_tolerated_disk_barrier_failures =
3043 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
292fd7fc
SB
3044 if (fs_info->fs_devices->missing_devices >
3045 fs_info->num_tolerated_disk_barrier_failures &&
3046 !(sb->s_flags & MS_RDONLY)) {
05135f59
DS
3047 btrfs_warn(fs_info,
3048"missing devices (%llu) exceeds the limit (%d), writeable mount is not allowed",
78fa1770
ZL
3049 fs_info->fs_devices->missing_devices,
3050 fs_info->num_tolerated_disk_barrier_failures);
2365dd3c 3051 goto fail_sysfs;
292fd7fc 3052 }
9078a3e1 3053
a74a4b97
CM
3054 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
3055 "btrfs-cleaner");
57506d50 3056 if (IS_ERR(fs_info->cleaner_kthread))
2365dd3c 3057 goto fail_sysfs;
a74a4b97
CM
3058
3059 fs_info->transaction_kthread = kthread_run(transaction_kthread,
3060 tree_root,
3061 "btrfs-transaction");
57506d50 3062 if (IS_ERR(fs_info->transaction_kthread))
3f157a2f 3063 goto fail_cleaner;
a74a4b97 3064
0b246afa
JM
3065 if (!btrfs_test_opt(fs_info, SSD) &&
3066 !btrfs_test_opt(fs_info, NOSSD) &&
c289811c 3067 !fs_info->fs_devices->rotating) {
05135f59 3068 btrfs_info(fs_info, "detected SSD devices, enabling SSD mode");
c289811c
CM
3069 btrfs_set_opt(fs_info->mount_opt, SSD);
3070 }
3071
572d9ab7 3072 /*
01327610 3073 * Mount does not set all options immediately, we can do it now and do
572d9ab7
DS
3074 * not have to wait for transaction commit
3075 */
3076 btrfs_apply_pending_changes(fs_info);
3818aea2 3077
21adbd5c 3078#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
0b246afa 3079 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
2ff7e61e 3080 ret = btrfsic_mount(fs_info, fs_devices,
0b246afa 3081 btrfs_test_opt(fs_info,
21adbd5c
SB
3082 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
3083 1 : 0,
3084 fs_info->check_integrity_print_mask);
3085 if (ret)
05135f59
DS
3086 btrfs_warn(fs_info,
3087 "failed to initialize integrity check module: %d",
3088 ret);
21adbd5c
SB
3089 }
3090#endif
bcef60f2
AJ
3091 ret = btrfs_read_qgroup_config(fs_info);
3092 if (ret)
3093 goto fail_trans_kthread;
21adbd5c 3094
96da0919
QW
3095 /* do not make disk changes in broken FS or nologreplay is given */
3096 if (btrfs_super_log_root(disk_super) != 0 &&
0b246afa 3097 !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
63443bf5 3098 ret = btrfs_replay_log(fs_info, fs_devices);
79787eaa 3099 if (ret) {
63443bf5 3100 err = ret;
28c16cbb 3101 goto fail_qgroup;
79787eaa 3102 }
e02119d5 3103 }
1a40e23b 3104
6bccf3ab 3105 ret = btrfs_find_orphan_roots(fs_info);
79787eaa 3106 if (ret)
28c16cbb 3107 goto fail_qgroup;
76dda93c 3108
7c2ca468 3109 if (!(sb->s_flags & MS_RDONLY)) {
d68fc57b 3110 ret = btrfs_cleanup_fs_roots(fs_info);
44c44af2 3111 if (ret)
28c16cbb 3112 goto fail_qgroup;
90c711ab
ZB
3113
3114 mutex_lock(&fs_info->cleaner_mutex);
5d4f98a2 3115 ret = btrfs_recover_relocation(tree_root);
90c711ab 3116 mutex_unlock(&fs_info->cleaner_mutex);
d7ce5843 3117 if (ret < 0) {
05135f59
DS
3118 btrfs_warn(fs_info, "failed to recover relocation: %d",
3119 ret);
d7ce5843 3120 err = -EINVAL;
bcef60f2 3121 goto fail_qgroup;
d7ce5843 3122 }
7c2ca468 3123 }
1a40e23b 3124
3de4586c
CM
3125 location.objectid = BTRFS_FS_TREE_OBJECTID;
3126 location.type = BTRFS_ROOT_ITEM_KEY;
cb517eab 3127 location.offset = 0;
3de4586c 3128
3de4586c 3129 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
3140c9a3
DC
3130 if (IS_ERR(fs_info->fs_root)) {
3131 err = PTR_ERR(fs_info->fs_root);
bcef60f2 3132 goto fail_qgroup;
3140c9a3 3133 }
c289811c 3134
2b6ba629
ID
3135 if (sb->s_flags & MS_RDONLY)
3136 return 0;
59641015 3137
f8d468a1
OS
3138 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
3139 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
6675df31
OS
3140 clear_free_space_tree = 1;
3141 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3142 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
3143 btrfs_warn(fs_info, "free space tree is invalid");
3144 clear_free_space_tree = 1;
3145 }
3146
3147 if (clear_free_space_tree) {
f8d468a1
OS
3148 btrfs_info(fs_info, "clearing free space tree");
3149 ret = btrfs_clear_free_space_tree(fs_info);
3150 if (ret) {
3151 btrfs_warn(fs_info,
3152 "failed to clear free space tree: %d", ret);
6bccf3ab 3153 close_ctree(fs_info);
f8d468a1
OS
3154 return ret;
3155 }
3156 }
3157
0b246afa 3158 if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
511711af 3159 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
05135f59 3160 btrfs_info(fs_info, "creating free space tree");
511711af
CM
3161 ret = btrfs_create_free_space_tree(fs_info);
3162 if (ret) {
05135f59
DS
3163 btrfs_warn(fs_info,
3164 "failed to create free space tree: %d", ret);
6bccf3ab 3165 close_ctree(fs_info);
511711af
CM
3166 return ret;
3167 }
3168 }
3169
2b6ba629
ID
3170 down_read(&fs_info->cleanup_work_sem);
3171 if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
3172 (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
e3acc2a6 3173 up_read(&fs_info->cleanup_work_sem);
6bccf3ab 3174 close_ctree(fs_info);
2b6ba629
ID
3175 return ret;
3176 }
3177 up_read(&fs_info->cleanup_work_sem);
59641015 3178
2b6ba629
ID
3179 ret = btrfs_resume_balance_async(fs_info);
3180 if (ret) {
05135f59 3181 btrfs_warn(fs_info, "failed to resume balance: %d", ret);
6bccf3ab 3182 close_ctree(fs_info);
2b6ba629 3183 return ret;
e3acc2a6
JB
3184 }
3185
8dabb742
SB
3186 ret = btrfs_resume_dev_replace_async(fs_info);
3187 if (ret) {
05135f59 3188 btrfs_warn(fs_info, "failed to resume device replace: %d", ret);
6bccf3ab 3189 close_ctree(fs_info);
8dabb742
SB
3190 return ret;
3191 }
3192
b382a324
JS
3193 btrfs_qgroup_rescan_resume(fs_info);
3194
4bbcaa64 3195 if (!fs_info->uuid_root) {
05135f59 3196 btrfs_info(fs_info, "creating UUID tree");
f7a81ea4
SB
3197 ret = btrfs_create_uuid_tree(fs_info);
3198 if (ret) {
05135f59
DS
3199 btrfs_warn(fs_info,
3200 "failed to create the UUID tree: %d", ret);
6bccf3ab 3201 close_ctree(fs_info);
f7a81ea4
SB
3202 return ret;
3203 }
0b246afa 3204 } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
4bbcaa64
ES
3205 fs_info->generation !=
3206 btrfs_super_uuid_tree_generation(disk_super)) {
05135f59 3207 btrfs_info(fs_info, "checking UUID tree");
70f80175
SB
3208 ret = btrfs_check_uuid_tree(fs_info);
3209 if (ret) {
05135f59
DS
3210 btrfs_warn(fs_info,
3211 "failed to check the UUID tree: %d", ret);
6bccf3ab 3212 close_ctree(fs_info);
70f80175
SB
3213 return ret;
3214 }
3215 } else {
afcdd129 3216 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
f7a81ea4 3217 }
afcdd129 3218 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
47ab2a6c 3219
8dcddfa0
QW
3220 /*
3221 * backuproot only affect mount behavior, and if open_ctree succeeded,
3222 * no need to keep the flag
3223 */
3224 btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
3225
ad2b2c80 3226 return 0;
39279cc3 3227
bcef60f2
AJ
3228fail_qgroup:
3229 btrfs_free_qgroup_config(fs_info);
7c2ca468
CM
3230fail_trans_kthread:
3231 kthread_stop(fs_info->transaction_kthread);
2ff7e61e 3232 btrfs_cleanup_transaction(fs_info);
faa2dbf0 3233 btrfs_free_fs_roots(fs_info);
3f157a2f 3234fail_cleaner:
a74a4b97 3235 kthread_stop(fs_info->cleaner_kthread);
7c2ca468
CM
3236
3237 /*
3238 * make sure we're done with the btree inode before we stop our
3239 * kthreads
3240 */
3241 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
7c2ca468 3242
2365dd3c 3243fail_sysfs:
6618a59b 3244 btrfs_sysfs_remove_mounted(fs_info);
2365dd3c 3245
b7c35e81
AJ
3246fail_fsdev_sysfs:
3247 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3248
1b1d1f66 3249fail_block_groups:
54067ae9 3250 btrfs_put_block_group_cache(fs_info);
1b1d1f66 3251 btrfs_free_block_groups(fs_info);
af31f5e5
CM
3252
3253fail_tree_roots:
3254 free_root_pointers(fs_info, 1);
2b8195bb 3255 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
af31f5e5 3256
39279cc3 3257fail_sb_buffer:
7abadb64 3258 btrfs_stop_all_workers(fs_info);
16cdcec7 3259fail_alloc:
4543df7e 3260fail_iput:
586e46e2
ID
3261 btrfs_mapping_tree_free(&fs_info->mapping_tree);
3262
4543df7e 3263 iput(fs_info->btree_inode);
c404e0dc
MX
3264fail_bio_counter:
3265 percpu_counter_destroy(&fs_info->bio_counter);
963d678b
MX
3266fail_delalloc_bytes:
3267 percpu_counter_destroy(&fs_info->delalloc_bytes);
e2d84521
MX
3268fail_dirty_metadata_bytes:
3269 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
ad081f14 3270fail_bdi:
7e662854 3271 bdi_destroy(&fs_info->bdi);
76dda93c
YZ
3272fail_srcu:
3273 cleanup_srcu_struct(&fs_info->subvol_srcu);
7e662854 3274fail:
53b381b3 3275 btrfs_free_stripe_hash_table(fs_info);
586e46e2 3276 btrfs_close_devices(fs_info->fs_devices);
ad2b2c80 3277 return err;
af31f5e5
CM
3278
3279recovery_tree_root:
0b246afa 3280 if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
af31f5e5
CM
3281 goto fail_tree_roots;
3282
3283 free_root_pointers(fs_info, 0);
3284
3285 /* don't use the log in recovery mode, it won't be valid */
3286 btrfs_set_super_log_root(disk_super, 0);
3287
3288 /* we can't trust the free space cache either */
3289 btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
3290
3291 ret = next_root_backup(fs_info, fs_info->super_copy,
3292 &num_backups_tried, &backup_index);
3293 if (ret == -1)
3294 goto fail_block_groups;
3295 goto retry_root_backup;
eb60ceac
CM
3296}
3297
f2984462
CM
3298static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
3299{
f2984462
CM
3300 if (uptodate) {
3301 set_buffer_uptodate(bh);
3302 } else {
442a4f63
SB
3303 struct btrfs_device *device = (struct btrfs_device *)
3304 bh->b_private;
3305
fb456252 3306 btrfs_warn_rl_in_rcu(device->fs_info,
b14af3b4 3307 "lost page write due to IO error on %s",
606686ee 3308 rcu_str_deref(device->name));
01327610 3309 /* note, we don't set_buffer_write_io_error because we have
1259ab75
CM
3310 * our own ways of dealing with the IO errors
3311 */
f2984462 3312 clear_buffer_uptodate(bh);
442a4f63 3313 btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
f2984462
CM
3314 }
3315 unlock_buffer(bh);
3316 put_bh(bh);
3317}
3318
29c36d72
AJ
3319int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
3320 struct buffer_head **bh_ret)
3321{
3322 struct buffer_head *bh;
3323 struct btrfs_super_block *super;
3324 u64 bytenr;
3325
3326 bytenr = btrfs_sb_offset(copy_num);
3327 if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
3328 return -EINVAL;
3329
3330 bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE);
3331 /*
3332 * If we fail to read from the underlying devices, as of now
3333 * the best option we have is to mark it EIO.
3334 */
3335 if (!bh)
3336 return -EIO;
3337
3338 super = (struct btrfs_super_block *)bh->b_data;
3339 if (btrfs_super_bytenr(super) != bytenr ||
3340 btrfs_super_magic(super) != BTRFS_MAGIC) {
3341 brelse(bh);
3342 return -EINVAL;
3343 }
3344
3345 *bh_ret = bh;
3346 return 0;
3347}
3348
3349
a512bbf8
YZ
3350struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3351{
3352 struct buffer_head *bh;
3353 struct buffer_head *latest = NULL;
3354 struct btrfs_super_block *super;
3355 int i;
3356 u64 transid = 0;
92fc03fb 3357 int ret = -EINVAL;
a512bbf8
YZ
3358
3359 /* we would like to check all the supers, but that would make
3360 * a btrfs mount succeed after a mkfs from a different FS.
3361 * So, we need to add a special mount option to scan for
3362 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3363 */
3364 for (i = 0; i < 1; i++) {
29c36d72
AJ
3365 ret = btrfs_read_dev_one_super(bdev, i, &bh);
3366 if (ret)
a512bbf8
YZ
3367 continue;
3368
3369 super = (struct btrfs_super_block *)bh->b_data;
a512bbf8
YZ
3370
3371 if (!latest || btrfs_super_generation(super) > transid) {
3372 brelse(latest);
3373 latest = bh;
3374 transid = btrfs_super_generation(super);
3375 } else {
3376 brelse(bh);
3377 }
3378 }
92fc03fb
AJ
3379
3380 if (!latest)
3381 return ERR_PTR(ret);
3382
a512bbf8
YZ
3383 return latest;
3384}
3385
4eedeb75
HH
3386/*
3387 * this should be called twice, once with wait == 0 and
3388 * once with wait == 1. When wait == 0 is done, all the buffer heads
3389 * we write are pinned.
3390 *
3391 * They are released when wait == 1 is done.
3392 * max_mirrors must be the same for both runs, and it indicates how
3393 * many supers on this one device should be written.
3394 *
3395 * max_mirrors == 0 means to write them all.
3396 */
a512bbf8
YZ
3397static int write_dev_supers(struct btrfs_device *device,
3398 struct btrfs_super_block *sb,
3399 int do_barriers, int wait, int max_mirrors)
3400{
3401 struct buffer_head *bh;
3402 int i;
3403 int ret;
3404 int errors = 0;
3405 u32 crc;
3406 u64 bytenr;
a512bbf8
YZ
3407
3408 if (max_mirrors == 0)
3409 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3410
a512bbf8
YZ
3411 for (i = 0; i < max_mirrors; i++) {
3412 bytenr = btrfs_sb_offset(i);
935e5cc9
MX
3413 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3414 device->commit_total_bytes)
a512bbf8
YZ
3415 break;
3416
3417 if (wait) {
3418 bh = __find_get_block(device->bdev, bytenr / 4096,
3419 BTRFS_SUPER_INFO_SIZE);
634554dc
JB
3420 if (!bh) {
3421 errors++;
3422 continue;
3423 }
a512bbf8 3424 wait_on_buffer(bh);
4eedeb75
HH
3425 if (!buffer_uptodate(bh))
3426 errors++;
3427
3428 /* drop our reference */
3429 brelse(bh);
3430
3431 /* drop the reference from the wait == 0 run */
3432 brelse(bh);
3433 continue;
a512bbf8
YZ
3434 } else {
3435 btrfs_set_super_bytenr(sb, bytenr);
3436
3437 crc = ~(u32)0;
b0496686 3438 crc = btrfs_csum_data((char *)sb +
a512bbf8
YZ
3439 BTRFS_CSUM_SIZE, crc,
3440 BTRFS_SUPER_INFO_SIZE -
3441 BTRFS_CSUM_SIZE);
3442 btrfs_csum_final(crc, sb->csum);
3443
4eedeb75
HH
3444 /*
3445 * one reference for us, and we leave it for the
3446 * caller
3447 */
a512bbf8
YZ
3448 bh = __getblk(device->bdev, bytenr / 4096,
3449 BTRFS_SUPER_INFO_SIZE);
634554dc 3450 if (!bh) {
fb456252 3451 btrfs_err(device->fs_info,
f14d104d
DS
3452 "couldn't get super buffer head for bytenr %llu",
3453 bytenr);
634554dc
JB
3454 errors++;
3455 continue;
3456 }
3457
a512bbf8
YZ
3458 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
3459
4eedeb75 3460 /* one reference for submit_bh */
a512bbf8 3461 get_bh(bh);
4eedeb75
HH
3462
3463 set_buffer_uptodate(bh);
a512bbf8
YZ
3464 lock_buffer(bh);
3465 bh->b_end_io = btrfs_end_buffer_write_sync;
442a4f63 3466 bh->b_private = device;
a512bbf8
YZ
3467 }
3468
387125fc
CM
3469 /*
3470 * we fua the first super. The others we allow
3471 * to go down lazy.
3472 */
e8117c26 3473 if (i == 0)
2a222ca9 3474 ret = btrfsic_submit_bh(REQ_OP_WRITE, WRITE_FUA, bh);
e8117c26 3475 else
2a222ca9 3476 ret = btrfsic_submit_bh(REQ_OP_WRITE, WRITE_SYNC, bh);
4eedeb75 3477 if (ret)
a512bbf8 3478 errors++;
a512bbf8
YZ
3479 }
3480 return errors < i ? 0 : -1;
3481}
3482
387125fc
CM
3483/*
3484 * endio for the write_dev_flush, this will wake anyone waiting
3485 * for the barrier when it is done
3486 */
4246a0b6 3487static void btrfs_end_empty_barrier(struct bio *bio)
387125fc 3488{
387125fc
CM
3489 if (bio->bi_private)
3490 complete(bio->bi_private);
3491 bio_put(bio);
3492}
3493
3494/*
3495 * trigger flushes for one the devices. If you pass wait == 0, the flushes are
3496 * sent down. With wait == 1, it waits for the previous flush.
3497 *
3498 * any device where the flush fails with eopnotsupp are flagged as not-barrier
3499 * capable
3500 */
3501static int write_dev_flush(struct btrfs_device *device, int wait)
3502{
3503 struct bio *bio;
3504 int ret = 0;
3505
3506 if (device->nobarriers)
3507 return 0;
3508
3509 if (wait) {
3510 bio = device->flush_bio;
3511 if (!bio)
3512 return 0;
3513
3514 wait_for_completion(&device->flush_wait);
3515
4246a0b6
CH
3516 if (bio->bi_error) {
3517 ret = bio->bi_error;
5af3e8cc
SB
3518 btrfs_dev_stat_inc_and_print(device,
3519 BTRFS_DEV_STAT_FLUSH_ERRS);
387125fc
CM
3520 }
3521
3522 /* drop the reference from the wait == 0 run */
3523 bio_put(bio);
3524 device->flush_bio = NULL;
3525
3526 return ret;
3527 }
3528
3529 /*
3530 * one reference for us, and we leave it for the
3531 * caller
3532 */
9c017abc 3533 device->flush_bio = NULL;
9be3395b 3534 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
387125fc
CM
3535 if (!bio)
3536 return -ENOMEM;
3537
3538 bio->bi_end_io = btrfs_end_empty_barrier;
3539 bio->bi_bdev = device->bdev;
37226b21 3540 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH);
387125fc
CM
3541 init_completion(&device->flush_wait);
3542 bio->bi_private = &device->flush_wait;
3543 device->flush_bio = bio;
3544
3545 bio_get(bio);
4e49ea4a 3546 btrfsic_submit_bio(bio);
387125fc
CM
3547
3548 return 0;
3549}
3550
3551/*
3552 * send an empty flush down to each device in parallel,
3553 * then wait for them
3554 */
3555static int barrier_all_devices(struct btrfs_fs_info *info)
3556{
3557 struct list_head *head;
3558 struct btrfs_device *dev;
5af3e8cc
SB
3559 int errors_send = 0;
3560 int errors_wait = 0;
387125fc
CM
3561 int ret;
3562
3563 /* send down all the barriers */
3564 head = &info->fs_devices->devices;
3565 list_for_each_entry_rcu(dev, head, dev_list) {
f88ba6a2
HS
3566 if (dev->missing)
3567 continue;
387125fc 3568 if (!dev->bdev) {
5af3e8cc 3569 errors_send++;
387125fc
CM
3570 continue;
3571 }
3572 if (!dev->in_fs_metadata || !dev->writeable)
3573 continue;
3574
3575 ret = write_dev_flush(dev, 0);
3576 if (ret)
5af3e8cc 3577 errors_send++;
387125fc
CM
3578 }
3579
3580 /* wait for all the barriers */
3581 list_for_each_entry_rcu(dev, head, dev_list) {
f88ba6a2
HS
3582 if (dev->missing)
3583 continue;
387125fc 3584 if (!dev->bdev) {
5af3e8cc 3585 errors_wait++;
387125fc
CM
3586 continue;
3587 }
3588 if (!dev->in_fs_metadata || !dev->writeable)
3589 continue;
3590
3591 ret = write_dev_flush(dev, 1);
3592 if (ret)
5af3e8cc 3593 errors_wait++;
387125fc 3594 }
5af3e8cc
SB
3595 if (errors_send > info->num_tolerated_disk_barrier_failures ||
3596 errors_wait > info->num_tolerated_disk_barrier_failures)
387125fc
CM
3597 return -EIO;
3598 return 0;
3599}
3600
943c6e99
ZL
3601int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
3602{
8789f4fe
ZL
3603 int raid_type;
3604 int min_tolerated = INT_MAX;
943c6e99 3605
8789f4fe
ZL
3606 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
3607 (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
3608 min_tolerated = min(min_tolerated,
3609 btrfs_raid_array[BTRFS_RAID_SINGLE].
3610 tolerated_failures);
943c6e99 3611
8789f4fe
ZL
3612 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
3613 if (raid_type == BTRFS_RAID_SINGLE)
3614 continue;
3615 if (!(flags & btrfs_raid_group[raid_type]))
3616 continue;
3617 min_tolerated = min(min_tolerated,
3618 btrfs_raid_array[raid_type].
3619 tolerated_failures);
3620 }
943c6e99 3621
8789f4fe 3622 if (min_tolerated == INT_MAX) {
ab8d0fc4 3623 pr_warn("BTRFS: unknown raid flag: %llu", flags);
8789f4fe
ZL
3624 min_tolerated = 0;
3625 }
3626
3627 return min_tolerated;
943c6e99
ZL
3628}
3629
5af3e8cc
SB
3630int btrfs_calc_num_tolerated_disk_barrier_failures(
3631 struct btrfs_fs_info *fs_info)
3632{
3633 struct btrfs_ioctl_space_info space;
3634 struct btrfs_space_info *sinfo;
3635 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3636 BTRFS_BLOCK_GROUP_SYSTEM,
3637 BTRFS_BLOCK_GROUP_METADATA,
3638 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
5af3e8cc
SB
3639 int i;
3640 int c;
3641 int num_tolerated_disk_barrier_failures =
3642 (int)fs_info->fs_devices->num_devices;
3643
2c458045 3644 for (i = 0; i < ARRAY_SIZE(types); i++) {
5af3e8cc
SB
3645 struct btrfs_space_info *tmp;
3646
3647 sinfo = NULL;
3648 rcu_read_lock();
3649 list_for_each_entry_rcu(tmp, &fs_info->space_info, list) {
3650 if (tmp->flags == types[i]) {
3651 sinfo = tmp;
3652 break;
3653 }
3654 }
3655 rcu_read_unlock();
3656
3657 if (!sinfo)
3658 continue;
3659
3660 down_read(&sinfo->groups_sem);
3661 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2c458045
ZL
3662 u64 flags;
3663
3664 if (list_empty(&sinfo->block_groups[c]))
3665 continue;
3666
3667 btrfs_get_block_group_info(&sinfo->block_groups[c],
3668 &space);
3669 if (space.total_bytes == 0 || space.used_bytes == 0)
3670 continue;
3671 flags = space.flags;
943c6e99
ZL
3672
3673 num_tolerated_disk_barrier_failures = min(
3674 num_tolerated_disk_barrier_failures,
3675 btrfs_get_num_tolerated_disk_barrier_failures(
3676 flags));
5af3e8cc
SB
3677 }
3678 up_read(&sinfo->groups_sem);
3679 }
3680
3681 return num_tolerated_disk_barrier_failures;
3682}
3683
2ff7e61e 3684static int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
f2984462 3685{
e5e9a520 3686 struct list_head *head;
f2984462 3687 struct btrfs_device *dev;
a061fc8d 3688 struct btrfs_super_block *sb;
f2984462 3689 struct btrfs_dev_item *dev_item;
f2984462
CM
3690 int ret;
3691 int do_barriers;
a236aed1
CM
3692 int max_errors;
3693 int total_errors = 0;
a061fc8d 3694 u64 flags;
f2984462 3695
0b246afa
JM
3696 do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
3697 backup_super_roots(fs_info);
f2984462 3698
0b246afa 3699 sb = fs_info->super_for_commit;
a061fc8d 3700 dev_item = &sb->dev_item;
e5e9a520 3701
0b246afa
JM
3702 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3703 head = &fs_info->fs_devices->devices;
3704 max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
387125fc 3705
5af3e8cc 3706 if (do_barriers) {
0b246afa 3707 ret = barrier_all_devices(fs_info);
5af3e8cc
SB
3708 if (ret) {
3709 mutex_unlock(
0b246afa
JM
3710 &fs_info->fs_devices->device_list_mutex);
3711 btrfs_handle_fs_error(fs_info, ret,
3712 "errors while submitting device barriers.");
5af3e8cc
SB
3713 return ret;
3714 }
3715 }
387125fc 3716
1f78160c 3717 list_for_each_entry_rcu(dev, head, dev_list) {
dfe25020
CM
3718 if (!dev->bdev) {
3719 total_errors++;
3720 continue;
3721 }
2b82032c 3722 if (!dev->in_fs_metadata || !dev->writeable)
dfe25020
CM
3723 continue;
3724
2b82032c 3725 btrfs_set_stack_device_generation(dev_item, 0);
a061fc8d
CM
3726 btrfs_set_stack_device_type(dev_item, dev->type);
3727 btrfs_set_stack_device_id(dev_item, dev->devid);
7df69d3e 3728 btrfs_set_stack_device_total_bytes(dev_item,
935e5cc9 3729 dev->commit_total_bytes);
ce7213c7
MX
3730 btrfs_set_stack_device_bytes_used(dev_item,
3731 dev->commit_bytes_used);
a061fc8d
CM
3732 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
3733 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
3734 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
3735 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
2b82032c 3736 memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
a512bbf8 3737
a061fc8d
CM
3738 flags = btrfs_super_flags(sb);
3739 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
3740
a512bbf8 3741 ret = write_dev_supers(dev, sb, do_barriers, 0, max_mirrors);
a236aed1
CM
3742 if (ret)
3743 total_errors++;
f2984462 3744 }
a236aed1 3745 if (total_errors > max_errors) {
0b246afa
JM
3746 btrfs_err(fs_info, "%d errors while writing supers",
3747 total_errors);
3748 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
79787eaa 3749
9d565ba4 3750 /* FUA is masked off if unsupported and can't be the reason */
0b246afa
JM
3751 btrfs_handle_fs_error(fs_info, -EIO,
3752 "%d errors while writing supers",
3753 total_errors);
9d565ba4 3754 return -EIO;
a236aed1 3755 }
f2984462 3756
a512bbf8 3757 total_errors = 0;
1f78160c 3758 list_for_each_entry_rcu(dev, head, dev_list) {
dfe25020
CM
3759 if (!dev->bdev)
3760 continue;
2b82032c 3761 if (!dev->in_fs_metadata || !dev->writeable)
dfe25020
CM
3762 continue;
3763
a512bbf8
YZ
3764 ret = write_dev_supers(dev, sb, do_barriers, 1, max_mirrors);
3765 if (ret)
3766 total_errors++;
f2984462 3767 }
0b246afa 3768 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
a236aed1 3769 if (total_errors > max_errors) {
0b246afa
JM
3770 btrfs_handle_fs_error(fs_info, -EIO,
3771 "%d errors while writing supers",
3772 total_errors);
79787eaa 3773 return -EIO;
a236aed1 3774 }
f2984462
CM
3775 return 0;
3776}
3777
a512bbf8 3778int write_ctree_super(struct btrfs_trans_handle *trans,
2ff7e61e 3779 struct btrfs_fs_info *fs_info, int max_mirrors)
eb60ceac 3780{
2ff7e61e 3781 return write_all_supers(fs_info, max_mirrors);
cfaa7295
CM
3782}
3783
cb517eab
MX
3784/* Drop a fs root from the radix tree and free it. */
3785void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
3786 struct btrfs_root *root)
2619ba1f 3787{
4df27c4d 3788 spin_lock(&fs_info->fs_roots_radix_lock);
2619ba1f
CM
3789 radix_tree_delete(&fs_info->fs_roots_radix,
3790 (unsigned long)root->root_key.objectid);
4df27c4d 3791 spin_unlock(&fs_info->fs_roots_radix_lock);
76dda93c
YZ
3792
3793 if (btrfs_root_refs(&root->root_item) == 0)
3794 synchronize_srcu(&fs_info->subvol_srcu);
3795
1c1ea4f7 3796 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
3321719e 3797 btrfs_free_log(NULL, root);
1c1ea4f7
LB
3798 if (root->reloc_root) {
3799 free_extent_buffer(root->reloc_root->node);
3800 free_extent_buffer(root->reloc_root->commit_root);
3801 btrfs_put_fs_root(root->reloc_root);
3802 root->reloc_root = NULL;
3803 }
3804 }
3321719e 3805
faa2dbf0
JB
3806 if (root->free_ino_pinned)
3807 __btrfs_remove_free_space_cache(root->free_ino_pinned);
3808 if (root->free_ino_ctl)
3809 __btrfs_remove_free_space_cache(root->free_ino_ctl);
4df27c4d 3810 free_fs_root(root);
4df27c4d
YZ
3811}
3812
3813static void free_fs_root(struct btrfs_root *root)
3814{
57cdc8db 3815 iput(root->ino_cache_inode);
4df27c4d 3816 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2ff7e61e 3817 btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv);
1cb048f5 3818 root->orphan_block_rsv = NULL;
0ee5dc67
AV
3819 if (root->anon_dev)
3820 free_anon_bdev(root->anon_dev);
8257b2dc
MX
3821 if (root->subv_writers)
3822 btrfs_free_subvolume_writers(root->subv_writers);
4df27c4d
YZ
3823 free_extent_buffer(root->node);
3824 free_extent_buffer(root->commit_root);
581bb050
LZ
3825 kfree(root->free_ino_ctl);
3826 kfree(root->free_ino_pinned);
d397712b 3827 kfree(root->name);
b0feb9d9 3828 btrfs_put_fs_root(root);
2619ba1f
CM
3829}
3830
cb517eab
MX
3831void btrfs_free_fs_root(struct btrfs_root *root)
3832{
3833 free_fs_root(root);
2619ba1f
CM
3834}
3835
c146afad 3836int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
cfaa7295 3837{
c146afad
YZ
3838 u64 root_objectid = 0;
3839 struct btrfs_root *gang[8];
65d33fd7
QW
3840 int i = 0;
3841 int err = 0;
3842 unsigned int ret = 0;
3843 int index;
e089f05c 3844
c146afad 3845 while (1) {
65d33fd7 3846 index = srcu_read_lock(&fs_info->subvol_srcu);
c146afad
YZ
3847 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
3848 (void **)gang, root_objectid,
3849 ARRAY_SIZE(gang));
65d33fd7
QW
3850 if (!ret) {
3851 srcu_read_unlock(&fs_info->subvol_srcu, index);
c146afad 3852 break;
65d33fd7 3853 }
5d4f98a2 3854 root_objectid = gang[ret - 1]->root_key.objectid + 1;
65d33fd7 3855
c146afad 3856 for (i = 0; i < ret; i++) {
65d33fd7
QW
3857 /* Avoid to grab roots in dead_roots */
3858 if (btrfs_root_refs(&gang[i]->root_item) == 0) {
3859 gang[i] = NULL;
3860 continue;
3861 }
3862 /* grab all the search result for later use */
3863 gang[i] = btrfs_grab_fs_root(gang[i]);
3864 }
3865 srcu_read_unlock(&fs_info->subvol_srcu, index);
66b4ffd1 3866
65d33fd7
QW
3867 for (i = 0; i < ret; i++) {
3868 if (!gang[i])
3869 continue;
c146afad 3870 root_objectid = gang[i]->root_key.objectid;
66b4ffd1
JB
3871 err = btrfs_orphan_cleanup(gang[i]);
3872 if (err)
65d33fd7
QW
3873 break;
3874 btrfs_put_fs_root(gang[i]);
c146afad
YZ
3875 }
3876 root_objectid++;
3877 }
65d33fd7
QW
3878
3879 /* release the uncleaned roots due to error */
3880 for (; i < ret; i++) {
3881 if (gang[i])
3882 btrfs_put_fs_root(gang[i]);
3883 }
3884 return err;
c146afad 3885}
a2135011 3886
6bccf3ab 3887int btrfs_commit_super(struct btrfs_fs_info *fs_info)
c146afad 3888{
6bccf3ab 3889 struct btrfs_root *root = fs_info->tree_root;
c146afad 3890 struct btrfs_trans_handle *trans;
a74a4b97 3891
0b246afa 3892 mutex_lock(&fs_info->cleaner_mutex);
2ff7e61e 3893 btrfs_run_delayed_iputs(fs_info);
0b246afa
JM
3894 mutex_unlock(&fs_info->cleaner_mutex);
3895 wake_up_process(fs_info->cleaner_kthread);
c71bf099
YZ
3896
3897 /* wait until ongoing cleanup work done */
0b246afa
JM
3898 down_write(&fs_info->cleanup_work_sem);
3899 up_write(&fs_info->cleanup_work_sem);
c71bf099 3900
7a7eaa40 3901 trans = btrfs_join_transaction(root);
3612b495
TI
3902 if (IS_ERR(trans))
3903 return PTR_ERR(trans);
d52c1bcc 3904 return btrfs_commit_transaction(trans, root);
c146afad
YZ
3905}
3906
6bccf3ab 3907void close_ctree(struct btrfs_fs_info *fs_info)
c146afad 3908{
6bccf3ab 3909 struct btrfs_root *root = fs_info->tree_root;
c146afad
YZ
3910 int ret;
3911
afcdd129 3912 set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
c146afad 3913
7343dd61 3914 /* wait for the qgroup rescan worker to stop */
d06f23d6 3915 btrfs_qgroup_wait_for_completion(fs_info, false);
7343dd61 3916
803b2f54
SB
3917 /* wait for the uuid_scan task to finish */
3918 down(&fs_info->uuid_tree_rescan_sem);
3919 /* avoid complains from lockdep et al., set sem back to initial state */
3920 up(&fs_info->uuid_tree_rescan_sem);
3921
837d5b6e 3922 /* pause restriper - we want to resume on mount */
aa1b8cd4 3923 btrfs_pause_balance(fs_info);
837d5b6e 3924
8dabb742
SB
3925 btrfs_dev_replace_suspend_for_unmount(fs_info);
3926
aa1b8cd4 3927 btrfs_scrub_cancel(fs_info);
4cb5300b
CM
3928
3929 /* wait for any defraggers to finish */
3930 wait_event(fs_info->transaction_wait,
3931 (atomic_read(&fs_info->defrag_running) == 0));
3932
3933 /* clear out the rbtree of defraggable inodes */
26176e7c 3934 btrfs_cleanup_defrag_inodes(fs_info);
4cb5300b 3935
21c7e756
MX
3936 cancel_work_sync(&fs_info->async_reclaim_work);
3937
c146afad 3938 if (!(fs_info->sb->s_flags & MS_RDONLY)) {
e44163e1
JM
3939 /*
3940 * If the cleaner thread is stopped and there are
3941 * block groups queued for removal, the deletion will be
3942 * skipped when we quit the cleaner thread.
3943 */
0b246afa 3944 btrfs_delete_unused_bgs(fs_info);
e44163e1 3945
6bccf3ab 3946 ret = btrfs_commit_super(fs_info);
acce952b 3947 if (ret)
04892340 3948 btrfs_err(fs_info, "commit super ret %d", ret);
acce952b 3949 }
3950
87533c47 3951 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
2ff7e61e 3952 btrfs_error_commit_super(fs_info);
0f7d52f4 3953
e3029d9f
AV
3954 kthread_stop(fs_info->transaction_kthread);
3955 kthread_stop(fs_info->cleaner_kthread);
8929ecfa 3956
afcdd129 3957 set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
f25784b3 3958
04892340 3959 btrfs_free_qgroup_config(fs_info);
bcef60f2 3960
963d678b 3961 if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
04892340 3962 btrfs_info(fs_info, "at unmount delalloc count %lld",
963d678b 3963 percpu_counter_sum(&fs_info->delalloc_bytes));
b0c68f8b 3964 }
bcc63abb 3965
6618a59b 3966 btrfs_sysfs_remove_mounted(fs_info);
b7c35e81 3967 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
5ac1d209 3968
faa2dbf0 3969 btrfs_free_fs_roots(fs_info);
d10c5f31 3970
1a4319cc
LB
3971 btrfs_put_block_group_cache(fs_info);
3972
2b1360da
JB
3973 btrfs_free_block_groups(fs_info);
3974
de348ee0
WS
3975 /*
3976 * we must make sure there is not any read request to
3977 * submit after we stopping all workers.
3978 */
3979 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
96192499
JB
3980 btrfs_stop_all_workers(fs_info);
3981
afcdd129 3982 clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
13e6c37b 3983 free_root_pointers(fs_info, 1);
9ad6b7bc 3984
13e6c37b 3985 iput(fs_info->btree_inode);
d6bfde87 3986
21adbd5c 3987#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
0b246afa 3988 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
2ff7e61e 3989 btrfsic_unmount(fs_info->fs_devices);
21adbd5c
SB
3990#endif
3991
dfe25020 3992 btrfs_close_devices(fs_info->fs_devices);
0b86a832 3993 btrfs_mapping_tree_free(&fs_info->mapping_tree);
b248a415 3994
e2d84521 3995 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
963d678b 3996 percpu_counter_destroy(&fs_info->delalloc_bytes);
c404e0dc 3997 percpu_counter_destroy(&fs_info->bio_counter);
04160088 3998 bdi_destroy(&fs_info->bdi);
76dda93c 3999 cleanup_srcu_struct(&fs_info->subvol_srcu);
0b86a832 4000
53b381b3
DW
4001 btrfs_free_stripe_hash_table(fs_info);
4002
cdfb080e 4003 __btrfs_free_block_rsv(root->orphan_block_rsv);
1cb048f5 4004 root->orphan_block_rsv = NULL;
04216820 4005
0b246afa 4006 lock_chunks(fs_info);
04216820
FM
4007 while (!list_empty(&fs_info->pinned_chunks)) {
4008 struct extent_map *em;
4009
4010 em = list_first_entry(&fs_info->pinned_chunks,
4011 struct extent_map, list);
4012 list_del_init(&em->list);
4013 free_extent_map(em);
4014 }
0b246afa 4015 unlock_chunks(fs_info);
eb60ceac
CM
4016}
4017
b9fab919
CM
4018int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
4019 int atomic)
5f39d397 4020{
1259ab75 4021 int ret;
727011e0 4022 struct inode *btree_inode = buf->pages[0]->mapping->host;
1259ab75 4023
0b32f4bb 4024 ret = extent_buffer_uptodate(buf);
1259ab75
CM
4025 if (!ret)
4026 return ret;
4027
4028 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
b9fab919
CM
4029 parent_transid, atomic);
4030 if (ret == -EAGAIN)
4031 return ret;
1259ab75 4032 return !ret;
5f39d397
CM
4033}
4034
5f39d397
CM
4035void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4036{
0b246afa 4037 struct btrfs_fs_info *fs_info;
06ea65a3 4038 struct btrfs_root *root;
5f39d397 4039 u64 transid = btrfs_header_generation(buf);
b9473439 4040 int was_dirty;
b4ce94de 4041
06ea65a3
JB
4042#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4043 /*
4044 * This is a fast path so only do this check if we have sanity tests
4045 * enabled. Normal people shouldn't be marking dummy buffers as dirty
4046 * outside of the sanity tests.
4047 */
4048 if (unlikely(test_bit(EXTENT_BUFFER_DUMMY, &buf->bflags)))
4049 return;
4050#endif
4051 root = BTRFS_I(buf->pages[0]->mapping->host)->root;
0b246afa 4052 fs_info = root->fs_info;
b9447ef8 4053 btrfs_assert_tree_locked(buf);
0b246afa 4054 if (transid != fs_info->generation)
5d163e0e 4055 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
0b246afa 4056 buf->start, transid, fs_info->generation);
0b32f4bb 4057 was_dirty = set_extent_buffer_dirty(buf);
e2d84521 4058 if (!was_dirty)
0b246afa 4059 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
e2d84521 4060 buf->len,
0b246afa 4061 fs_info->dirty_metadata_batch);
1f21ef0a
FM
4062#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4063 if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) {
2ff7e61e 4064 btrfs_print_leaf(fs_info, buf);
1f21ef0a
FM
4065 ASSERT(0);
4066 }
4067#endif
eb60ceac
CM
4068}
4069
2ff7e61e 4070static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
b53d3f5d 4071 int flush_delayed)
16cdcec7
MX
4072{
4073 /*
4074 * looks as though older kernels can get into trouble with
4075 * this code, they end up stuck in balance_dirty_pages forever
4076 */
e2d84521 4077 int ret;
16cdcec7
MX
4078
4079 if (current->flags & PF_MEMALLOC)
4080 return;
4081
b53d3f5d 4082 if (flush_delayed)
2ff7e61e 4083 btrfs_balance_delayed_items(fs_info);
16cdcec7 4084
0b246afa 4085 ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes,
e2d84521
MX
4086 BTRFS_DIRTY_METADATA_THRESH);
4087 if (ret > 0) {
0b246afa 4088 balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
16cdcec7 4089 }
16cdcec7
MX
4090}
4091
2ff7e61e 4092void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
35b7e476 4093{
2ff7e61e 4094 __btrfs_btree_balance_dirty(fs_info, 1);
b53d3f5d 4095}
585ad2c3 4096
2ff7e61e 4097void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
b53d3f5d 4098{
2ff7e61e 4099 __btrfs_btree_balance_dirty(fs_info, 0);
35b7e476 4100}
6b80053d 4101
ca7a79ad 4102int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
6b80053d 4103{
727011e0 4104 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
2ff7e61e
JM
4105 struct btrfs_fs_info *fs_info = root->fs_info;
4106
4107 return btree_read_extent_buffer_pages(fs_info, buf, parent_transid);
6b80053d 4108}
0da5468f 4109
fcd1f065 4110static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
acce952b 4111 int read_only)
4112{
c926093e 4113 struct btrfs_super_block *sb = fs_info->super_copy;
319e4d06
QW
4114 u64 nodesize = btrfs_super_nodesize(sb);
4115 u64 sectorsize = btrfs_super_sectorsize(sb);
c926093e
DS
4116 int ret = 0;
4117
319e4d06 4118 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
ab8d0fc4 4119 btrfs_err(fs_info, "no valid FS found");
319e4d06
QW
4120 ret = -EINVAL;
4121 }
4122 if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP)
ab8d0fc4 4123 btrfs_warn(fs_info, "unrecognized super flag: %llu",
319e4d06 4124 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
21e7626b 4125 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
ab8d0fc4 4126 btrfs_err(fs_info, "tree_root level too big: %d >= %d",
21e7626b 4127 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
c926093e
DS
4128 ret = -EINVAL;
4129 }
21e7626b 4130 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
ab8d0fc4 4131 btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
21e7626b 4132 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
c926093e
DS
4133 ret = -EINVAL;
4134 }
21e7626b 4135 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
ab8d0fc4 4136 btrfs_err(fs_info, "log_root level too big: %d >= %d",
21e7626b 4137 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
c926093e
DS
4138 ret = -EINVAL;
4139 }
4140
1104a885 4141 /*
319e4d06
QW
4142 * Check sectorsize and nodesize first, other check will need it.
4143 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
1104a885 4144 */
319e4d06
QW
4145 if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
4146 sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
ab8d0fc4 4147 btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
319e4d06
QW
4148 ret = -EINVAL;
4149 }
4150 /* Only PAGE SIZE is supported yet */
09cbfeaf 4151 if (sectorsize != PAGE_SIZE) {
ab8d0fc4
JM
4152 btrfs_err(fs_info,
4153 "sectorsize %llu not supported yet, only support %lu",
4154 sectorsize, PAGE_SIZE);
319e4d06
QW
4155 ret = -EINVAL;
4156 }
4157 if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
4158 nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
ab8d0fc4 4159 btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
319e4d06
QW
4160 ret = -EINVAL;
4161 }
4162 if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
ab8d0fc4
JM
4163 btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
4164 le32_to_cpu(sb->__unused_leafsize), nodesize);
319e4d06
QW
4165 ret = -EINVAL;
4166 }
4167
4168 /* Root alignment check */
4169 if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
ab8d0fc4
JM
4170 btrfs_warn(fs_info, "tree_root block unaligned: %llu",
4171 btrfs_super_root(sb));
319e4d06
QW
4172 ret = -EINVAL;
4173 }
4174 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
ab8d0fc4
JM
4175 btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
4176 btrfs_super_chunk_root(sb));
75d6ad38
DS
4177 ret = -EINVAL;
4178 }
319e4d06 4179 if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
ab8d0fc4
JM
4180 btrfs_warn(fs_info, "log_root block unaligned: %llu",
4181 btrfs_super_log_root(sb));
75d6ad38
DS
4182 ret = -EINVAL;
4183 }
4184
c926093e 4185 if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_UUID_SIZE) != 0) {
ab8d0fc4
JM
4186 btrfs_err(fs_info,
4187 "dev_item UUID does not match fsid: %pU != %pU",
4188 fs_info->fsid, sb->dev_item.fsid);
c926093e
DS
4189 ret = -EINVAL;
4190 }
4191
4192 /*
4193 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
4194 * done later
4195 */
99e3ecfc
LB
4196 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
4197 btrfs_err(fs_info, "bytes_used is too small %llu",
ab8d0fc4 4198 btrfs_super_bytes_used(sb));
99e3ecfc
LB
4199 ret = -EINVAL;
4200 }
b7f67055 4201 if (!is_power_of_2(btrfs_super_stripesize(sb))) {
99e3ecfc 4202 btrfs_err(fs_info, "invalid stripesize %u",
ab8d0fc4 4203 btrfs_super_stripesize(sb));
99e3ecfc
LB
4204 ret = -EINVAL;
4205 }
21e7626b 4206 if (btrfs_super_num_devices(sb) > (1UL << 31))
ab8d0fc4
JM
4207 btrfs_warn(fs_info, "suspicious number of devices: %llu",
4208 btrfs_super_num_devices(sb));
75d6ad38 4209 if (btrfs_super_num_devices(sb) == 0) {
ab8d0fc4 4210 btrfs_err(fs_info, "number of devices is 0");
75d6ad38
DS
4211 ret = -EINVAL;
4212 }
c926093e 4213
21e7626b 4214 if (btrfs_super_bytenr(sb) != BTRFS_SUPER_INFO_OFFSET) {
ab8d0fc4
JM
4215 btrfs_err(fs_info, "super offset mismatch %llu != %u",
4216 btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
c926093e
DS
4217 ret = -EINVAL;
4218 }
4219
ce7fca5f
DS
4220 /*
4221 * Obvious sys_chunk_array corruptions, it must hold at least one key
4222 * and one chunk
4223 */
4224 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
ab8d0fc4
JM
4225 btrfs_err(fs_info, "system chunk array too big %u > %u",
4226 btrfs_super_sys_array_size(sb),
4227 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
ce7fca5f
DS
4228 ret = -EINVAL;
4229 }
4230 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
4231 + sizeof(struct btrfs_chunk)) {
ab8d0fc4
JM
4232 btrfs_err(fs_info, "system chunk array too small %u < %zu",
4233 btrfs_super_sys_array_size(sb),
4234 sizeof(struct btrfs_disk_key)
4235 + sizeof(struct btrfs_chunk));
ce7fca5f
DS
4236 ret = -EINVAL;
4237 }
4238
c926093e
DS
4239 /*
4240 * The generation is a global counter, we'll trust it more than the others
4241 * but it's still possible that it's the one that's wrong.
4242 */
21e7626b 4243 if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
ab8d0fc4
JM
4244 btrfs_warn(fs_info,
4245 "suspicious: generation < chunk_root_generation: %llu < %llu",
4246 btrfs_super_generation(sb),
4247 btrfs_super_chunk_root_generation(sb));
21e7626b
DS
4248 if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
4249 && btrfs_super_cache_generation(sb) != (u64)-1)
ab8d0fc4
JM
4250 btrfs_warn(fs_info,
4251 "suspicious: generation < cache_generation: %llu < %llu",
4252 btrfs_super_generation(sb),
4253 btrfs_super_cache_generation(sb));
c926093e
DS
4254
4255 return ret;
acce952b 4256}
4257
2ff7e61e 4258static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
acce952b 4259{
0b246afa 4260 mutex_lock(&fs_info->cleaner_mutex);
2ff7e61e 4261 btrfs_run_delayed_iputs(fs_info);
0b246afa 4262 mutex_unlock(&fs_info->cleaner_mutex);
acce952b 4263
0b246afa
JM
4264 down_write(&fs_info->cleanup_work_sem);
4265 up_write(&fs_info->cleanup_work_sem);
acce952b 4266
4267 /* cleanup FS via transaction */
2ff7e61e 4268 btrfs_cleanup_transaction(fs_info);
acce952b 4269}
4270
143bede5 4271static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
acce952b 4272{
acce952b 4273 struct btrfs_ordered_extent *ordered;
acce952b 4274
199c2a9c 4275 spin_lock(&root->ordered_extent_lock);
779880ef
JB
4276 /*
4277 * This will just short circuit the ordered completion stuff which will
4278 * make sure the ordered extent gets properly cleaned up.
4279 */
199c2a9c 4280 list_for_each_entry(ordered, &root->ordered_extents,
779880ef
JB
4281 root_extent_list)
4282 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
199c2a9c
MX
4283 spin_unlock(&root->ordered_extent_lock);
4284}
4285
4286static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4287{
4288 struct btrfs_root *root;
4289 struct list_head splice;
4290
4291 INIT_LIST_HEAD(&splice);
4292
4293 spin_lock(&fs_info->ordered_root_lock);
4294 list_splice_init(&fs_info->ordered_roots, &splice);
4295 while (!list_empty(&splice)) {
4296 root = list_first_entry(&splice, struct btrfs_root,
4297 ordered_root);
1de2cfde
JB
4298 list_move_tail(&root->ordered_root,
4299 &fs_info->ordered_roots);
199c2a9c 4300
2a85d9ca 4301 spin_unlock(&fs_info->ordered_root_lock);
199c2a9c
MX
4302 btrfs_destroy_ordered_extents(root);
4303
2a85d9ca
LB
4304 cond_resched();
4305 spin_lock(&fs_info->ordered_root_lock);
199c2a9c
MX
4306 }
4307 spin_unlock(&fs_info->ordered_root_lock);
acce952b 4308}
4309
35a3621b 4310static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
2ff7e61e 4311 struct btrfs_fs_info *fs_info)
acce952b 4312{
4313 struct rb_node *node;
4314 struct btrfs_delayed_ref_root *delayed_refs;
4315 struct btrfs_delayed_ref_node *ref;
4316 int ret = 0;
4317
4318 delayed_refs = &trans->delayed_refs;
4319
4320 spin_lock(&delayed_refs->lock);
d7df2c79 4321 if (atomic_read(&delayed_refs->num_entries) == 0) {
cfece4db 4322 spin_unlock(&delayed_refs->lock);
0b246afa 4323 btrfs_info(fs_info, "delayed_refs has NO entry");
acce952b 4324 return ret;
4325 }
4326
d7df2c79
JB
4327 while ((node = rb_first(&delayed_refs->href_root)) != NULL) {
4328 struct btrfs_delayed_ref_head *head;
c6fc2454 4329 struct btrfs_delayed_ref_node *tmp;
e78417d1 4330 bool pin_bytes = false;
acce952b 4331
d7df2c79
JB
4332 head = rb_entry(node, struct btrfs_delayed_ref_head,
4333 href_node);
4334 if (!mutex_trylock(&head->mutex)) {
4335 atomic_inc(&head->node.refs);
4336 spin_unlock(&delayed_refs->lock);
eb12db69 4337
d7df2c79 4338 mutex_lock(&head->mutex);
e78417d1 4339 mutex_unlock(&head->mutex);
d7df2c79
JB
4340 btrfs_put_delayed_ref(&head->node);
4341 spin_lock(&delayed_refs->lock);
4342 continue;
4343 }
4344 spin_lock(&head->lock);
c6fc2454
QW
4345 list_for_each_entry_safe_reverse(ref, tmp, &head->ref_list,
4346 list) {
d7df2c79 4347 ref->in_tree = 0;
c6fc2454 4348 list_del(&ref->list);
1d57ee94
WX
4349 if (!list_empty(&ref->add_list))
4350 list_del(&ref->add_list);
d7df2c79
JB
4351 atomic_dec(&delayed_refs->num_entries);
4352 btrfs_put_delayed_ref(ref);
e78417d1 4353 }
d7df2c79
JB
4354 if (head->must_insert_reserved)
4355 pin_bytes = true;
4356 btrfs_free_delayed_extent_op(head->extent_op);
4357 delayed_refs->num_heads--;
4358 if (head->processing == 0)
4359 delayed_refs->num_heads_ready--;
4360 atomic_dec(&delayed_refs->num_entries);
4361 head->node.in_tree = 0;
4362 rb_erase(&head->href_node, &delayed_refs->href_root);
4363 spin_unlock(&head->lock);
4364 spin_unlock(&delayed_refs->lock);
4365 mutex_unlock(&head->mutex);
acce952b 4366
d7df2c79 4367 if (pin_bytes)
2ff7e61e 4368 btrfs_pin_extent(fs_info, head->node.bytenr,
d7df2c79
JB
4369 head->node.num_bytes, 1);
4370 btrfs_put_delayed_ref(&head->node);
acce952b 4371 cond_resched();
4372 spin_lock(&delayed_refs->lock);
4373 }
4374
4375 spin_unlock(&delayed_refs->lock);
4376
4377 return ret;
4378}
4379
143bede5 4380static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
acce952b 4381{
4382 struct btrfs_inode *btrfs_inode;
4383 struct list_head splice;
4384
4385 INIT_LIST_HEAD(&splice);
4386
eb73c1b7
MX
4387 spin_lock(&root->delalloc_lock);
4388 list_splice_init(&root->delalloc_inodes, &splice);
acce952b 4389
4390 while (!list_empty(&splice)) {
eb73c1b7
MX
4391 btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4392 delalloc_inodes);
acce952b 4393
4394 list_del_init(&btrfs_inode->delalloc_inodes);
df0af1a5
MX
4395 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
4396 &btrfs_inode->runtime_flags);
eb73c1b7 4397 spin_unlock(&root->delalloc_lock);
acce952b 4398
4399 btrfs_invalidate_inodes(btrfs_inode->root);
b216cbfb 4400
eb73c1b7 4401 spin_lock(&root->delalloc_lock);
acce952b 4402 }
4403
eb73c1b7
MX
4404 spin_unlock(&root->delalloc_lock);
4405}
4406
4407static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4408{
4409 struct btrfs_root *root;
4410 struct list_head splice;
4411
4412 INIT_LIST_HEAD(&splice);
4413
4414 spin_lock(&fs_info->delalloc_root_lock);
4415 list_splice_init(&fs_info->delalloc_roots, &splice);
4416 while (!list_empty(&splice)) {
4417 root = list_first_entry(&splice, struct btrfs_root,
4418 delalloc_root);
4419 list_del_init(&root->delalloc_root);
4420 root = btrfs_grab_fs_root(root);
4421 BUG_ON(!root);
4422 spin_unlock(&fs_info->delalloc_root_lock);
4423
4424 btrfs_destroy_delalloc_inodes(root);
4425 btrfs_put_fs_root(root);
4426
4427 spin_lock(&fs_info->delalloc_root_lock);
4428 }
4429 spin_unlock(&fs_info->delalloc_root_lock);
acce952b 4430}
4431
2ff7e61e 4432static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
acce952b 4433 struct extent_io_tree *dirty_pages,
4434 int mark)
4435{
4436 int ret;
acce952b 4437 struct extent_buffer *eb;
4438 u64 start = 0;
4439 u64 end;
acce952b 4440
4441 while (1) {
4442 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
e6138876 4443 mark, NULL);
acce952b 4444 if (ret)
4445 break;
4446
91166212 4447 clear_extent_bits(dirty_pages, start, end, mark);
acce952b 4448 while (start <= end) {
0b246afa
JM
4449 eb = find_extent_buffer(fs_info, start);
4450 start += fs_info->nodesize;
fd8b2b61 4451 if (!eb)
acce952b 4452 continue;
fd8b2b61 4453 wait_on_extent_buffer_writeback(eb);
acce952b 4454
fd8b2b61
JB
4455 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
4456 &eb->bflags))
4457 clear_extent_buffer_dirty(eb);
4458 free_extent_buffer_stale(eb);
acce952b 4459 }
4460 }
4461
4462 return ret;
4463}
4464
2ff7e61e 4465static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
acce952b 4466 struct extent_io_tree *pinned_extents)
4467{
4468 struct extent_io_tree *unpin;
4469 u64 start;
4470 u64 end;
4471 int ret;
ed0eaa14 4472 bool loop = true;
acce952b 4473
4474 unpin = pinned_extents;
ed0eaa14 4475again:
acce952b 4476 while (1) {
4477 ret = find_first_extent_bit(unpin, 0, &start, &end,
e6138876 4478 EXTENT_DIRTY, NULL);
acce952b 4479 if (ret)
4480 break;
4481
af6f8f60 4482 clear_extent_dirty(unpin, start, end);
2ff7e61e 4483 btrfs_error_unpin_extent_range(fs_info, start, end);
acce952b 4484 cond_resched();
4485 }
4486
ed0eaa14 4487 if (loop) {
0b246afa
JM
4488 if (unpin == &fs_info->freed_extents[0])
4489 unpin = &fs_info->freed_extents[1];
ed0eaa14 4490 else
0b246afa 4491 unpin = &fs_info->freed_extents[0];
ed0eaa14
LB
4492 loop = false;
4493 goto again;
4494 }
4495
acce952b 4496 return 0;
4497}
4498
c79a1751
LB
4499static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
4500{
4501 struct inode *inode;
4502
4503 inode = cache->io_ctl.inode;
4504 if (inode) {
4505 invalidate_inode_pages2(inode->i_mapping);
4506 BTRFS_I(inode)->generation = 0;
4507 cache->io_ctl.inode = NULL;
4508 iput(inode);
4509 }
4510 btrfs_put_block_group(cache);
4511}
4512
4513void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
2ff7e61e 4514 struct btrfs_fs_info *fs_info)
c79a1751
LB
4515{
4516 struct btrfs_block_group_cache *cache;
4517
4518 spin_lock(&cur_trans->dirty_bgs_lock);
4519 while (!list_empty(&cur_trans->dirty_bgs)) {
4520 cache = list_first_entry(&cur_trans->dirty_bgs,
4521 struct btrfs_block_group_cache,
4522 dirty_list);
4523 if (!cache) {
0b246afa 4524 btrfs_err(fs_info, "orphan block group dirty_bgs list");
c79a1751
LB
4525 spin_unlock(&cur_trans->dirty_bgs_lock);
4526 return;
4527 }
4528
4529 if (!list_empty(&cache->io_list)) {
4530 spin_unlock(&cur_trans->dirty_bgs_lock);
4531 list_del_init(&cache->io_list);
4532 btrfs_cleanup_bg_io(cache);
4533 spin_lock(&cur_trans->dirty_bgs_lock);
4534 }
4535
4536 list_del_init(&cache->dirty_list);
4537 spin_lock(&cache->lock);
4538 cache->disk_cache_state = BTRFS_DC_ERROR;
4539 spin_unlock(&cache->lock);
4540
4541 spin_unlock(&cur_trans->dirty_bgs_lock);
4542 btrfs_put_block_group(cache);
4543 spin_lock(&cur_trans->dirty_bgs_lock);
4544 }
4545 spin_unlock(&cur_trans->dirty_bgs_lock);
4546
4547 while (!list_empty(&cur_trans->io_bgs)) {
4548 cache = list_first_entry(&cur_trans->io_bgs,
4549 struct btrfs_block_group_cache,
4550 io_list);
4551 if (!cache) {
0b246afa 4552 btrfs_err(fs_info, "orphan block group on io_bgs list");
c79a1751
LB
4553 return;
4554 }
4555
4556 list_del_init(&cache->io_list);
4557 spin_lock(&cache->lock);
4558 cache->disk_cache_state = BTRFS_DC_ERROR;
4559 spin_unlock(&cache->lock);
4560 btrfs_cleanup_bg_io(cache);
4561 }
4562}
4563
49b25e05 4564void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
2ff7e61e 4565 struct btrfs_fs_info *fs_info)
49b25e05 4566{
2ff7e61e 4567 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
c79a1751
LB
4568 ASSERT(list_empty(&cur_trans->dirty_bgs));
4569 ASSERT(list_empty(&cur_trans->io_bgs));
4570
2ff7e61e 4571 btrfs_destroy_delayed_refs(cur_trans, fs_info);
49b25e05 4572
4a9d8bde 4573 cur_trans->state = TRANS_STATE_COMMIT_START;
0b246afa 4574 wake_up(&fs_info->transaction_blocked_wait);
49b25e05 4575
4a9d8bde 4576 cur_trans->state = TRANS_STATE_UNBLOCKED;
0b246afa 4577 wake_up(&fs_info->transaction_wait);
49b25e05 4578
ccdf9b30
JM
4579 btrfs_destroy_delayed_inodes(fs_info);
4580 btrfs_assert_delayed_root_empty(fs_info);
49b25e05 4581
2ff7e61e 4582 btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
49b25e05 4583 EXTENT_DIRTY);
2ff7e61e 4584 btrfs_destroy_pinned_extent(fs_info,
0b246afa 4585 fs_info->pinned_extents);
49b25e05 4586
4a9d8bde
MX
4587 cur_trans->state =TRANS_STATE_COMPLETED;
4588 wake_up(&cur_trans->commit_wait);
4589
49b25e05
JM
4590 /*
4591 memset(cur_trans, 0, sizeof(*cur_trans));
4592 kmem_cache_free(btrfs_transaction_cachep, cur_trans);
4593 */
4594}
4595
2ff7e61e 4596static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
acce952b 4597{
4598 struct btrfs_transaction *t;
acce952b 4599
0b246afa 4600 mutex_lock(&fs_info->transaction_kthread_mutex);
acce952b 4601
0b246afa
JM
4602 spin_lock(&fs_info->trans_lock);
4603 while (!list_empty(&fs_info->trans_list)) {
4604 t = list_first_entry(&fs_info->trans_list,
724e2315
JB
4605 struct btrfs_transaction, list);
4606 if (t->state >= TRANS_STATE_COMMIT_START) {
4607 atomic_inc(&t->use_count);
0b246afa 4608 spin_unlock(&fs_info->trans_lock);
2ff7e61e 4609 btrfs_wait_for_commit(fs_info, t->transid);
724e2315 4610 btrfs_put_transaction(t);
0b246afa 4611 spin_lock(&fs_info->trans_lock);
724e2315
JB
4612 continue;
4613 }
0b246afa 4614 if (t == fs_info->running_transaction) {
724e2315 4615 t->state = TRANS_STATE_COMMIT_DOING;
0b246afa 4616 spin_unlock(&fs_info->trans_lock);
724e2315
JB
4617 /*
4618 * We wait for 0 num_writers since we don't hold a trans
4619 * handle open currently for this transaction.
4620 */
4621 wait_event(t->writer_wait,
4622 atomic_read(&t->num_writers) == 0);
4623 } else {
0b246afa 4624 spin_unlock(&fs_info->trans_lock);
724e2315 4625 }
2ff7e61e 4626 btrfs_cleanup_one_transaction(t, fs_info);
4a9d8bde 4627
0b246afa
JM
4628 spin_lock(&fs_info->trans_lock);
4629 if (t == fs_info->running_transaction)
4630 fs_info->running_transaction = NULL;
acce952b 4631 list_del_init(&t->list);
0b246afa 4632 spin_unlock(&fs_info->trans_lock);
acce952b 4633
724e2315 4634 btrfs_put_transaction(t);
2ff7e61e 4635 trace_btrfs_transaction_commit(fs_info->tree_root);
0b246afa 4636 spin_lock(&fs_info->trans_lock);
724e2315 4637 }
0b246afa
JM
4638 spin_unlock(&fs_info->trans_lock);
4639 btrfs_destroy_all_ordered_extents(fs_info);
ccdf9b30
JM
4640 btrfs_destroy_delayed_inodes(fs_info);
4641 btrfs_assert_delayed_root_empty(fs_info);
2ff7e61e 4642 btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
0b246afa
JM
4643 btrfs_destroy_all_delalloc_inodes(fs_info);
4644 mutex_unlock(&fs_info->transaction_kthread_mutex);
acce952b 4645
4646 return 0;
4647}
4648
e8c9f186 4649static const struct extent_io_ops btree_extent_io_ops = {
ce9adaa5 4650 .readpage_end_io_hook = btree_readpage_end_io_hook,
4bb31e92 4651 .readpage_io_failed_hook = btree_io_failed_hook,
0b86a832 4652 .submit_bio_hook = btree_submit_bio_hook,
239b14b3
CM
4653 /* note we're sharing with inode.c for the merge bio hook */
4654 .merge_bio_hook = btrfs_merge_bio_hook,
0da5468f 4655};