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