blob: 6c7dc24d4031480aa7ff179b95410ed414c9cd74 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Masone20d96d2007-03-22 12:13:20 -04006#include <linux/fs.h>
Chris Masond98237b2007-03-28 13:57:48 -04007#include <linux/blkdev.h>
Chris Mason0f7d52f2007-04-09 10:42:37 -04008#include <linux/radix-tree.h>
Chris Mason35b7e472007-05-02 15:53:43 -04009#include <linux/writeback.h>
Chris Masond3977122009-01-05 21:25:51 -050010#include <linux/buffer_head.h>
Chris Masonce9adaa2008-04-09 16:28:12 -040011#include <linux/workqueue.h>
Chris Masona74a4b92008-06-25 16:01:31 -040012#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Chris Mason784b4e22010-11-21 22:20:49 -050014#include <linux/migrate.h>
David Sterba7a36dde2011-05-06 15:33:15 +020015#include <linux/ratelimit.h>
Stefan Behrens6463fe52013-04-19 15:08:05 +000016#include <linux/uuid.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020017#include <linux/semaphore.h>
Masami Hiramatsu540adea2018-01-13 02:55:03 +090018#include <linux/error-injection.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020019#include <linux/crc32c.h>
Filipe Mananab89f6d12018-12-13 21:16:45 +000020#include <linux/sched/mm.h>
David Sterba7e75bf32011-03-18 22:56:43 +000021#include <asm/unaligned.h>
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +020022#include <crypto/hash.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050023#include "ctree.h"
24#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040025#include "transaction.h"
Chris Mason0f7d52f2007-04-09 10:42:37 -040026#include "btrfs_inode.h"
Chris Mason0b86a832008-03-24 15:01:56 -040027#include "volumes.h"
Chris Masondb945352007-10-15 16:15:53 -040028#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Chris Masone02119d2008-09-05 16:13:11 -040030#include "tree-log.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040031#include "free-space-cache.h"
Omar Sandoval70f6d822015-09-29 20:50:38 -070032#include "free-space-tree.h"
Li Zefan581bb052011-04-20 10:06:11 +080033#include "inode-map.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010034#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040035#include "rcu-string.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010036#include "dev-replace.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040038#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070039#include "qgroup.h"
Anand Jainebb87652016-03-10 17:26:59 +080040#include "compression.h"
Qu Wenruo557ea5d2017-10-09 01:51:02 +000041#include "tree-checker.h"
Josef Bacikfd708b82017-09-29 15:43:50 -040042#include "ref-verify.h"
Chris Masoneb60cea2007-02-02 09:18:22 -050043
Qu Wenruo319e4d02015-12-15 09:14:36 +080044#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
45 BTRFS_HEADER_FLAG_RELOC |\
46 BTRFS_SUPER_FLAG_ERROR |\
47 BTRFS_SUPER_FLAG_SEEDING |\
Anand Jaine2731e52018-01-09 09:05:41 +080048 BTRFS_SUPER_FLAG_METADUMP |\
49 BTRFS_SUPER_FLAG_METADUMP_V2)
Qu Wenruo319e4d02015-12-15 09:14:36 +080050
David Sterbae8c9f182015-01-02 18:23:10 +010051static const struct extent_io_ops btree_extent_io_ops;
Chris Mason8b712842008-06-11 16:50:36 -040052static void end_workqueue_fn(struct btrfs_work *work);
Jeff Mahoney143bede2012-03-01 14:56:26 +010053static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
liuboacce9522011-01-06 19:30:25 +080054static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040055 struct btrfs_fs_info *fs_info);
Jeff Mahoney143bede2012-03-01 14:56:26 +010056static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040057static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
liuboacce9522011-01-06 19:30:25 +080058 struct extent_io_tree *dirty_pages,
59 int mark);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040060static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
liuboacce9522011-01-06 19:30:25 +080061 struct extent_io_tree *pinned_extents);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040062static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
63static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
Chris Masonce9adaa2008-04-09 16:28:12 -040064
Chris Masond352ac62008-09-29 15:18:18 -040065/*
David Sterba97eb6b62014-07-30 00:55:42 +020066 * btrfs_end_io_wq structs are used to do processing in task context when an IO
67 * is complete. This is used during reads to verify checksums, and it is used
Chris Masond352ac62008-09-29 15:18:18 -040068 * by writes to insert metadata for new file extents after IO is complete.
69 */
David Sterba97eb6b62014-07-30 00:55:42 +020070struct btrfs_end_io_wq {
Chris Masonce9adaa2008-04-09 16:28:12 -040071 struct bio *bio;
72 bio_end_io_t *end_io;
73 void *private;
74 struct btrfs_fs_info *info;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020075 blk_status_t status;
David Sterbabfebd8b2014-07-30 00:25:45 +020076 enum btrfs_wq_endio_type metadata;
Chris Mason8b712842008-06-11 16:50:36 -040077 struct btrfs_work work;
Chris Masonce9adaa2008-04-09 16:28:12 -040078};
Chris Mason0da54682007-11-07 21:08:01 -050079
David Sterba97eb6b62014-07-30 00:55:42 +020080static struct kmem_cache *btrfs_end_io_wq_cache;
81
82int __init btrfs_end_io_wq_init(void)
83{
84 btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
85 sizeof(struct btrfs_end_io_wq),
86 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +030087 SLAB_MEM_SPREAD,
David Sterba97eb6b62014-07-30 00:55:42 +020088 NULL);
89 if (!btrfs_end_io_wq_cache)
90 return -ENOMEM;
91 return 0;
92}
93
David Sterbae67c7182018-02-19 17:24:18 +010094void __cold btrfs_end_io_wq_exit(void)
David Sterba97eb6b62014-07-30 00:55:42 +020095{
Kinglong Mee5598e902016-01-29 21:36:35 +080096 kmem_cache_destroy(btrfs_end_io_wq_cache);
David Sterba97eb6b62014-07-30 00:55:42 +020097}
98
Chris Masond352ac62008-09-29 15:18:18 -040099/*
100 * async submit bios are used to offload expensive checksumming
101 * onto the worker threads. They checksum file and metadata bios
102 * just before they are sent down the IO stack.
103 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400104struct async_submit_bio {
Josef Bacikc6100a42017-05-05 11:57:13 -0400105 void *private_data;
Chris Mason44b8bd72008-04-16 11:14:51 -0400106 struct bio *bio;
David Sterbaa7587812017-06-23 03:05:23 +0200107 extent_submit_bio_start_t *submit_bio_start;
Chris Mason44b8bd72008-04-16 11:14:51 -0400108 int mirror_num;
Chris Masoneaf25d92010-05-25 09:48:28 -0400109 /*
110 * bio_offset is optional, can be used if the pages in the bio
111 * can't tell us where in the file the bio should go
112 */
113 u64 bio_offset;
Chris Mason8b712842008-06-11 16:50:36 -0400114 struct btrfs_work work;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200115 blk_status_t status;
Chris Mason44b8bd72008-04-16 11:14:51 -0400116};
117
Chris Mason85d4e462011-07-26 16:11:19 -0400118/*
119 * Lockdep class keys for extent_buffer->lock's in this root. For a given
120 * eb, the lockdep key is determined by the btrfs_root it belongs to and
121 * the level the eb occupies in the tree.
Chris Mason4008c042009-02-12 14:09:45 -0500122 *
Chris Mason85d4e462011-07-26 16:11:19 -0400123 * Different roots are used for different purposes and may nest inside each
124 * other and they require separate keysets. As lockdep keys should be
125 * static, assign keysets according to the purpose of the root as indicated
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900126 * by btrfs_root->root_key.objectid. This ensures that all special purpose
127 * roots have separate keysets.
Chris Mason4008c042009-02-12 14:09:45 -0500128 *
Chris Mason85d4e462011-07-26 16:11:19 -0400129 * Lock-nesting across peer nodes is always done with the immediate parent
130 * node locked thus preventing deadlock. As lockdep doesn't know this, use
131 * subclass to avoid triggering lockdep warning in such cases.
Chris Mason4008c042009-02-12 14:09:45 -0500132 *
Chris Mason85d4e462011-07-26 16:11:19 -0400133 * The key is set by the readpage_end_io_hook after the buffer has passed
134 * csum validation but before the pages are unlocked. It is also set by
135 * btrfs_init_new_buffer on freshly allocated blocks.
136 *
137 * We also add a check to make sure the highest level of the tree is the
138 * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
139 * needs update as well.
Chris Mason4008c042009-02-12 14:09:45 -0500140 */
141#ifdef CONFIG_DEBUG_LOCK_ALLOC
142# if BTRFS_MAX_LEVEL != 8
143# error
144# endif
Chris Mason85d4e462011-07-26 16:11:19 -0400145
146static struct btrfs_lockdep_keyset {
147 u64 id; /* root objectid */
148 const char *name_stem; /* lock name stem */
149 char names[BTRFS_MAX_LEVEL + 1][20];
150 struct lock_class_key keys[BTRFS_MAX_LEVEL + 1];
151} btrfs_lockdep_keysets[] = {
152 { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" },
153 { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" },
154 { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" },
155 { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" },
156 { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" },
157 { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" },
David Sterba60b62972013-04-30 17:29:29 +0000158 { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" },
Chris Mason85d4e462011-07-26 16:11:19 -0400159 { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" },
160 { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" },
161 { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" },
David Sterba13fd8da2013-09-03 18:28:57 +0200162 { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" },
David Sterba6b20e0a2016-01-25 16:30:22 +0100163 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" },
Chris Mason85d4e462011-07-26 16:11:19 -0400164 { .id = 0, .name_stem = "tree" },
Chris Mason4008c042009-02-12 14:09:45 -0500165};
Chris Mason85d4e462011-07-26 16:11:19 -0400166
167void __init btrfs_init_lockdep(void)
168{
169 int i, j;
170
171 /* initialize lockdep class names */
172 for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) {
173 struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i];
174
175 for (j = 0; j < ARRAY_SIZE(ks->names); j++)
176 snprintf(ks->names[j], sizeof(ks->names[j]),
177 "btrfs-%s-%02d", ks->name_stem, j);
178 }
179}
180
181void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
182 int level)
183{
184 struct btrfs_lockdep_keyset *ks;
185
186 BUG_ON(level >= ARRAY_SIZE(ks->keys));
187
188 /* find the matching keyset, id 0 is the default entry */
189 for (ks = btrfs_lockdep_keysets; ks->id; ks++)
190 if (ks->id == objectid)
191 break;
192
193 lockdep_set_class_and_name(&eb->lock,
194 &ks->keys[level], ks->names[level]);
195}
196
Chris Mason4008c042009-02-12 14:09:45 -0500197#endif
198
Chris Masond352ac62008-09-29 15:18:18 -0400199/*
200 * extents on the btree inode are pretty simple, there's one extent
201 * that covers the entire device
202 */
David Sterba6af49db2017-06-23 04:09:57 +0200203struct extent_map *btree_get_extent(struct btrfs_inode *inode,
David Sterba306e16c2011-04-19 14:29:38 +0200204 struct page *page, size_t pg_offset, u64 start, u64 len,
Christoph Hellwigb2950862008-12-02 09:54:17 -0500205 int create)
Chris Mason5f39d392007-10-15 16:14:19 -0400206{
David Sterba3ffbd682018-06-29 10:56:42 +0200207 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +0200208 struct extent_map_tree *em_tree = &inode->extent_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400209 struct extent_map *em;
210 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400211
Chris Mason890871b2009-09-02 16:24:52 -0400212 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500213 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -0400214 if (em) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400215 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -0400216 read_unlock(&em_tree->lock);
Chris Mason5f39d392007-10-15 16:14:19 -0400217 goto out;
Chris Masona061fc82008-05-07 11:43:44 -0400218 }
Chris Mason890871b2009-09-02 16:24:52 -0400219 read_unlock(&em_tree->lock);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400220
David Sterba172ddd62011-04-21 00:48:27 +0200221 em = alloc_extent_map();
Chris Mason5f39d392007-10-15 16:14:19 -0400222 if (!em) {
223 em = ERR_PTR(-ENOMEM);
224 goto out;
225 }
226 em->start = 0;
Chris Mason0afbaf82008-04-18 14:17:20 -0400227 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -0400228 em->block_len = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400229 em->block_start = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -0500231
Chris Mason890871b2009-09-02 16:24:52 -0400232 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400233 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400234 if (ret == -EEXIST) {
235 free_extent_map(em);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400236 em = lookup_extent_mapping(em_tree, start, len);
Tsutomu Itohb4f359a2012-09-13 03:32:54 -0600237 if (!em)
Tsutomu Itoh0433f202012-09-13 03:32:32 -0600238 em = ERR_PTR(-EIO);
Chris Mason5f39d392007-10-15 16:14:19 -0400239 } else if (ret) {
Chris Mason7b13b7b2008-04-18 10:29:50 -0400240 free_extent_map(em);
Tsutomu Itoh0433f202012-09-13 03:32:32 -0600241 em = ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400242 }
Chris Mason890871b2009-09-02 16:24:52 -0400243 write_unlock(&em_tree->lock);
Chris Mason7b13b7b2008-04-18 10:29:50 -0400244
Chris Mason5f39d392007-10-15 16:14:19 -0400245out:
246 return em;
247}
248
Chris Masond352ac62008-09-29 15:18:18 -0400249/*
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100250 * Compute the csum of a btree block and store the result to provided buffer.
251 *
252 * Returns error if the extent buffer cannot be mapped.
Chris Masond352ac62008-09-29 15:18:18 -0400253 */
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100254static int csum_tree_block(struct extent_buffer *buf, u8 *result)
Chris Mason19c00dd2007-10-15 16:19:22 -0400255{
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200256 struct btrfs_fs_info *fs_info = buf->fs_info;
257 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Chris Mason19c00dd2007-10-15 16:19:22 -0400258 unsigned long len;
259 unsigned long cur_len;
260 unsigned long offset = BTRFS_CSUM_SIZE;
Chris Mason19c00dd2007-10-15 16:19:22 -0400261 char *kaddr;
262 unsigned long map_start;
263 unsigned long map_len;
264 int err;
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200265
266 shash->tfm = fs_info->csum_shash;
267 crypto_shash_init(shash);
Chris Mason19c00dd2007-10-15 16:19:22 -0400268
269 len = buf->len - offset;
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200270
Chris Masond3977122009-01-05 21:25:51 -0500271 while (len > 0) {
Johannes Thumshirnd2e174d2018-11-28 09:54:56 +0100272 /*
273 * Note: we don't need to check for the err == 1 case here, as
274 * with the given combination of 'start = BTRFS_CSUM_SIZE (32)'
275 * and 'min_len = 32' and the currently implemented mapping
276 * algorithm we cannot cross a page boundary.
277 */
Chris Mason19c00dd2007-10-15 16:19:22 -0400278 err = map_private_extent_buffer(buf, offset, 32,
Chris Masona6591712011-07-19 12:04:14 -0400279 &kaddr, &map_start, &map_len);
Johannes Thumshirnc53839f2019-02-25 14:24:16 +0100280 if (WARN_ON(err))
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200281 return err;
Chris Mason19c00dd2007-10-15 16:19:22 -0400282 cur_len = min(len, map_len - (offset - map_start));
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200283 crypto_shash_update(shash, kaddr + offset - map_start, cur_len);
Chris Mason19c00dd2007-10-15 16:19:22 -0400284 len -= cur_len;
285 offset += cur_len;
Chris Mason19c00dd2007-10-15 16:19:22 -0400286 }
David Sterba71a63552017-11-06 19:23:00 +0100287 memset(result, 0, BTRFS_CSUM_SIZE);
Josef Bacik607d4322008-12-02 07:17:45 -0500288
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200289 crypto_shash_final(shash, result);
Chris Mason19c00dd2007-10-15 16:19:22 -0400290
Chris Mason19c00dd2007-10-15 16:19:22 -0400291 return 0;
292}
293
Chris Masond352ac62008-09-29 15:18:18 -0400294/*
295 * we can't consider a given block up to date unless the transid of the
296 * block matches the transid in the parent node's pointer. This is how we
297 * detect blocks that either didn't get written at all or got written
298 * in the wrong place.
299 */
Chris Mason1259ab72008-05-12 13:39:03 -0400300static int verify_parent_transid(struct extent_io_tree *io_tree,
Chris Masonb9fab912012-05-06 07:23:47 -0400301 struct extent_buffer *eb, u64 parent_transid,
302 int atomic)
Chris Mason1259ab72008-05-12 13:39:03 -0400303{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000304 struct extent_state *cached_state = NULL;
Chris Mason1259ab72008-05-12 13:39:03 -0400305 int ret;
David Sterba2755a0d2014-07-31 00:43:18 +0200306 bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB);
Chris Mason1259ab72008-05-12 13:39:03 -0400307
308 if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
309 return 0;
310
Chris Masonb9fab912012-05-06 07:23:47 -0400311 if (atomic)
312 return -EAGAIN;
313
Josef Bacika26e8c92014-03-28 17:07:27 -0400314 if (need_lock) {
315 btrfs_tree_read_lock(eb);
David Sterba300aa892018-04-04 02:00:17 +0200316 btrfs_set_lock_blocking_read(eb);
Josef Bacika26e8c92014-03-28 17:07:27 -0400317 }
318
Josef Bacik2ac55d42010-02-03 19:33:23 +0000319 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +0100320 &cached_state);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400321 if (extent_buffer_uptodate(eb) &&
Chris Mason1259ab72008-05-12 13:39:03 -0400322 btrfs_header_generation(eb) == parent_transid) {
323 ret = 0;
324 goto out;
325 }
David Sterba94647322015-10-08 11:01:36 +0200326 btrfs_err_rl(eb->fs_info,
327 "parent transid verify failed on %llu wanted %llu found %llu",
328 eb->start,
Wang Shilong29549ae2014-07-04 17:59:06 +0800329 parent_transid, btrfs_header_generation(eb));
Chris Mason1259ab72008-05-12 13:39:03 -0400330 ret = 1;
Josef Bacika26e8c92014-03-28 17:07:27 -0400331
332 /*
333 * Things reading via commit roots that don't have normal protection,
334 * like send, can have a really old block in cache that may point at a
Nicholas D Steeves01327612016-05-19 21:18:45 -0400335 * block that has been freed and re-allocated. So don't clear uptodate
Josef Bacika26e8c92014-03-28 17:07:27 -0400336 * if we find an eb that is under IO (dirty/writeback) because we could
337 * end up reading in the stale data and then writing it back out and
338 * making everybody very sad.
339 */
340 if (!extent_buffer_under_io(eb))
341 clear_extent_buffer_uptodate(eb);
Chris Mason33958dc2008-07-30 10:29:12 -0400342out:
Josef Bacik2ac55d42010-02-03 19:33:23 +0000343 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
David Sterbae43bbe52017-12-12 21:43:52 +0100344 &cached_state);
Josef Bacik472b9092014-06-25 13:45:41 -0700345 if (need_lock)
346 btrfs_tree_read_unlock_blocking(eb);
Chris Mason1259ab72008-05-12 13:39:03 -0400347 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400348}
349
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200350static bool btrfs_supported_super_csum(u16 csum_type)
351{
352 switch (csum_type) {
353 case BTRFS_CSUM_TYPE_CRC32:
354 return true;
355 default:
356 return false;
357 }
358}
359
Chris Masond352ac62008-09-29 15:18:18 -0400360/*
David Sterba1104a882013-03-06 15:57:46 +0100361 * Return 0 if the superblock checksum type matches the checksum value of that
362 * algorithm. Pass the raw disk superblock data.
363 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400364static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
365 char *raw_disk_sb)
David Sterba1104a882013-03-06 15:57:46 +0100366{
367 struct btrfs_super_block *disk_sb =
368 (struct btrfs_super_block *)raw_disk_sb;
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +0200369 char result[BTRFS_CSUM_SIZE];
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200370 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
371
372 shash->tfm = fs_info->csum_shash;
373 crypto_shash_init(shash);
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200374
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +0200375 /*
376 * The super_block structure does not span the whole
377 * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
378 * filled with zeros and is included in the checksum.
379 */
Johannes Thumshirnd5178572019-06-03 16:58:57 +0200380 crypto_shash_update(shash, raw_disk_sb + BTRFS_CSUM_SIZE,
381 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
382 crypto_shash_final(shash, result);
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +0200383
384 if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb)))
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200385 return 1;
David Sterba1104a882013-03-06 15:57:46 +0100386
Johannes Thumshirne7e16f42019-06-03 16:58:53 +0200387 return 0;
David Sterba1104a882013-03-06 15:57:46 +0100388}
389
David Sterbae064d5e2019-03-20 14:58:13 +0100390int btrfs_verify_level_key(struct extent_buffer *eb, int level,
Qu Wenruo448de472019-03-12 17:10:40 +0800391 struct btrfs_key *first_key, u64 parent_transid)
Qu Wenruo581c1762018-03-29 09:08:11 +0800392{
David Sterbae064d5e2019-03-20 14:58:13 +0100393 struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo581c1762018-03-29 09:08:11 +0800394 int found_level;
395 struct btrfs_key found_key;
396 int ret;
397
398 found_level = btrfs_header_level(eb);
399 if (found_level != level) {
Qu Wenruo63489052019-03-20 14:27:39 +0800400 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
401 KERN_ERR "BTRFS: tree level check failed\n");
Qu Wenruo581c1762018-03-29 09:08:11 +0800402 btrfs_err(fs_info,
403"tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
404 eb->start, level, found_level);
Qu Wenruo581c1762018-03-29 09:08:11 +0800405 return -EIO;
406 }
407
408 if (!first_key)
409 return 0;
410
Qu Wenruo5d41be62018-04-13 06:32:47 +0800411 /*
412 * For live tree block (new tree blocks in current transaction),
413 * we need proper lock context to avoid race, which is impossible here.
414 * So we only checks tree blocks which is read from disk, whose
415 * generation <= fs_info->last_trans_committed.
416 */
417 if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
418 return 0;
Qu Wenruo581c1762018-03-29 09:08:11 +0800419 if (found_level)
420 btrfs_node_key_to_cpu(eb, &found_key, 0);
421 else
422 btrfs_item_key_to_cpu(eb, &found_key, 0);
423 ret = btrfs_comp_cpu_keys(first_key, &found_key);
424
Qu Wenruo581c1762018-03-29 09:08:11 +0800425 if (ret) {
Qu Wenruo63489052019-03-20 14:27:39 +0800426 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
427 KERN_ERR "BTRFS: tree first key check failed\n");
Qu Wenruo581c1762018-03-29 09:08:11 +0800428 btrfs_err(fs_info,
Liu Boff76a862018-05-18 10:59:35 +0800429"tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
430 eb->start, parent_transid, first_key->objectid,
431 first_key->type, first_key->offset,
432 found_key.objectid, found_key.type,
433 found_key.offset);
Qu Wenruo581c1762018-03-29 09:08:11 +0800434 }
Qu Wenruo581c1762018-03-29 09:08:11 +0800435 return ret;
436}
437
David Sterba1104a882013-03-06 15:57:46 +0100438/*
Chris Masond352ac62008-09-29 15:18:18 -0400439 * helper to read a given tree block, doing retries as required when
440 * the checksums don't match and we have alternate mirrors to try.
Qu Wenruo581c1762018-03-29 09:08:11 +0800441 *
442 * @parent_transid: expected transid, skip check if 0
443 * @level: expected level, mandatory check
444 * @first_key: expected key of first slot, skip check if NULL
Chris Masond352ac62008-09-29 15:18:18 -0400445 */
David Sterba5ab12d12019-03-20 14:56:39 +0100446static int btree_read_extent_buffer_pages(struct extent_buffer *eb,
Qu Wenruo581c1762018-03-29 09:08:11 +0800447 u64 parent_transid, int level,
448 struct btrfs_key *first_key)
Chris Masonf1885912008-04-09 16:28:12 -0400449{
David Sterba5ab12d12019-03-20 14:56:39 +0100450 struct btrfs_fs_info *fs_info = eb->fs_info;
Chris Masonf1885912008-04-09 16:28:12 -0400451 struct extent_io_tree *io_tree;
Josef Bacikea466792012-03-26 21:57:36 -0400452 int failed = 0;
Chris Masonf1885912008-04-09 16:28:12 -0400453 int ret;
454 int num_copies = 0;
455 int mirror_num = 0;
Josef Bacikea466792012-03-26 21:57:36 -0400456 int failed_mirror = 0;
Chris Masonf1885912008-04-09 16:28:12 -0400457
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400458 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
Chris Masonf1885912008-04-09 16:28:12 -0400459 while (1) {
Nikolay Borisovf8397d62018-11-06 16:40:20 +0200460 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +0300461 ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num);
Stefan Behrens256dd1b2012-08-10 08:58:21 -0600462 if (!ret) {
Qu Wenruo581c1762018-03-29 09:08:11 +0800463 if (verify_parent_transid(io_tree, eb,
Chris Masonb9fab912012-05-06 07:23:47 -0400464 parent_transid, 0))
Stefan Behrens256dd1b2012-08-10 08:58:21 -0600465 ret = -EIO;
David Sterbae064d5e2019-03-20 14:58:13 +0100466 else if (btrfs_verify_level_key(eb, level,
Qu Wenruo448de472019-03-12 17:10:40 +0800467 first_key, parent_transid))
Qu Wenruo581c1762018-03-29 09:08:11 +0800468 ret = -EUCLEAN;
469 else
470 break;
Stefan Behrens256dd1b2012-08-10 08:58:21 -0600471 }
Chris Masond3977122009-01-05 21:25:51 -0500472
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400473 num_copies = btrfs_num_copies(fs_info,
Chris Masonf1885912008-04-09 16:28:12 -0400474 eb->start, eb->len);
Chris Mason42352982008-04-28 16:40:52 -0400475 if (num_copies == 1)
Josef Bacikea466792012-03-26 21:57:36 -0400476 break;
Chris Mason42352982008-04-28 16:40:52 -0400477
Josef Bacik5cf1ab52012-04-16 09:42:26 -0400478 if (!failed_mirror) {
479 failed = 1;
480 failed_mirror = eb->read_mirror;
481 }
482
Chris Masonf1885912008-04-09 16:28:12 -0400483 mirror_num++;
Josef Bacikea466792012-03-26 21:57:36 -0400484 if (mirror_num == failed_mirror)
485 mirror_num++;
486
Chris Mason42352982008-04-28 16:40:52 -0400487 if (mirror_num > num_copies)
Josef Bacikea466792012-03-26 21:57:36 -0400488 break;
Chris Masonf1885912008-04-09 16:28:12 -0400489 }
Josef Bacikea466792012-03-26 21:57:36 -0400490
Stefan Behrensc0901582012-07-10 07:30:17 -0600491 if (failed && !ret && failed_mirror)
David Sterba20a1fbf92019-03-20 11:23:44 +0100492 btrfs_repair_eb_io_failure(eb, failed_mirror);
Josef Bacikea466792012-03-26 21:57:36 -0400493
494 return ret;
Chris Masonf1885912008-04-09 16:28:12 -0400495}
Chris Mason19c00dd2007-10-15 16:19:22 -0400496
Chris Masond352ac62008-09-29 15:18:18 -0400497/*
Chris Masond3977122009-01-05 21:25:51 -0500498 * 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
Chris Masond352ac62008-09-29 15:18:18 -0400500 */
Chris Masond3977122009-01-05 21:25:51 -0500501
Daniel Dressler01d58472014-11-21 17:15:07 +0900502static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page)
Chris Mason19c00dd2007-10-15 16:19:22 -0400503{
Miao Xie4eee4fa2012-12-21 09:17:45 +0000504 u64 start = page_offset(page);
Chris Mason19c00dd2007-10-15 16:19:22 -0400505 u64 found_start;
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100506 u8 result[BTRFS_CSUM_SIZE];
507 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Chris Mason19c00dd2007-10-15 16:19:22 -0400508 struct extent_buffer *eb;
Qu Wenruo8d47a0d2019-04-04 11:47:08 +0800509 int ret;
Chris Masonf1885912008-04-09 16:28:12 -0400510
Josef Bacik4f2de97a2012-03-07 16:20:05 -0500511 eb = (struct extent_buffer *)page->private;
512 if (page != eb->pages[0])
513 return 0;
Alex Lyakas0f805532016-03-10 13:10:15 +0200514
Chris Mason19c00dd2007-10-15 16:19:22 -0400515 found_start = btrfs_header_bytenr(eb);
Alex Lyakas0f805532016-03-10 13:10:15 +0200516 /*
517 * Please do not consolidate these warnings into a single if.
518 * It is useful to know what went wrong.
519 */
520 if (WARN_ON(found_start != start))
521 return -EUCLEAN;
522 if (WARN_ON(!PageUptodate(page)))
523 return -EUCLEAN;
524
Nikolay Borisovde37aa52018-10-30 16:43:24 +0200525 ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
Alex Lyakas0f805532016-03-10 13:10:15 +0200526 btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0);
527
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100528 if (csum_tree_block(eb, result))
529 return -EINVAL;
530
Qu Wenruo8d47a0d2019-04-04 11:47:08 +0800531 if (btrfs_header_level(eb))
532 ret = btrfs_check_node(eb);
533 else
534 ret = btrfs_check_leaf_full(eb);
535
536 if (ret < 0) {
537 btrfs_err(fs_info,
538 "block=%llu write time tree block corruption detected",
539 eb->start);
540 return ret;
541 }
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100542 write_extent_buffer(eb, result, 0, csum_size);
Qu Wenruo8d47a0d2019-04-04 11:47:08 +0800543
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100544 return 0;
Chris Mason19c00dd2007-10-15 16:19:22 -0400545}
546
David Sterbab0c9b3b2019-03-20 13:12:00 +0100547static int check_tree_block_fsid(struct extent_buffer *eb)
Yan Zheng2b820322008-11-17 21:11:30 -0500548{
David Sterbab0c9b3b2019-03-20 13:12:00 +0100549 struct btrfs_fs_info *fs_info = eb->fs_info;
Daniel Dressler01d58472014-11-21 17:15:07 +0900550 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jain44880fd2017-07-29 17:50:09 +0800551 u8 fsid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -0500552 int ret = 1;
553
Ross Kirk0a4e5582013-09-24 10:12:38 +0100554 read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -0500555 while (fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200556 u8 *metadata_uuid;
557
558 /*
559 * Checking the incompat flag is only valid for the current
560 * fs. For seed devices it's forbidden to have their uuid
561 * changed so reading ->fsid in this case is fine
562 */
563 if (fs_devices == fs_info->fs_devices &&
564 btrfs_fs_incompat(fs_info, METADATA_UUID))
565 metadata_uuid = fs_devices->metadata_uuid;
566 else
567 metadata_uuid = fs_devices->fsid;
568
569 if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500570 ret = 0;
571 break;
572 }
573 fs_devices = fs_devices->seed;
574 }
575 return ret;
576}
577
Miao Xiefacc8a222013-07-25 19:22:34 +0800578static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
579 u64 phy_offset, struct page *page,
580 u64 start, u64 end, int mirror)
Chris Masonce9adaa2008-04-09 16:28:12 -0400581{
Chris Masonce9adaa2008-04-09 16:28:12 -0400582 u64 found_start;
583 int found_level;
Chris Masonce9adaa2008-04-09 16:28:12 -0400584 struct extent_buffer *eb;
585 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Zhao Lei02873e42015-12-31 22:46:45 +0800586 struct btrfs_fs_info *fs_info = root->fs_info;
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100587 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Chris Masonf1885912008-04-09 16:28:12 -0400588 int ret = 0;
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100589 u8 result[BTRFS_CSUM_SIZE];
Chris Mason727011e2010-08-06 13:21:20 -0400590 int reads_done;
Chris Masonce9adaa2008-04-09 16:28:12 -0400591
Chris Masonce9adaa2008-04-09 16:28:12 -0400592 if (!page->private)
593 goto out;
Chris Masond3977122009-01-05 21:25:51 -0500594
Josef Bacik4f2de97a2012-03-07 16:20:05 -0500595 eb = (struct extent_buffer *)page->private;
Chris Masond3977122009-01-05 21:25:51 -0500596
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400597 /* the pending IO might have been the only thing that kept this buffer
598 * in memory. Make sure we have a ref for all this other checks
599 */
600 extent_buffer_get(eb);
601
602 reads_done = atomic_dec_and_test(&eb->io_pages);
Chris Mason727011e2010-08-06 13:21:20 -0400603 if (!reads_done)
604 goto err;
Chris Masonf1885912008-04-09 16:28:12 -0400605
Josef Bacik5cf1ab52012-04-16 09:42:26 -0400606 eb->read_mirror = mirror;
Filipe Manana656f30d2014-09-26 12:25:56 +0100607 if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
Josef Bacikea466792012-03-26 21:57:36 -0400608 ret = -EIO;
609 goto err;
610 }
611
Chris Masonce9adaa2008-04-09 16:28:12 -0400612 found_start = btrfs_header_bytenr(eb);
Chris Mason727011e2010-08-06 13:21:20 -0400613 if (found_start != eb->start) {
Su Yue893bf4b2018-06-22 09:52:15 +0800614 btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu",
615 eb->start, found_start);
Chris Masonf1885912008-04-09 16:28:12 -0400616 ret = -EIO;
Chris Masonce9adaa2008-04-09 16:28:12 -0400617 goto err;
618 }
David Sterbab0c9b3b2019-03-20 13:12:00 +0100619 if (check_tree_block_fsid(eb)) {
Zhao Lei02873e42015-12-31 22:46:45 +0800620 btrfs_err_rl(fs_info, "bad fsid on block %llu",
621 eb->start);
Chris Mason1259ab72008-05-12 13:39:03 -0400622 ret = -EIO;
623 goto err;
624 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400625 found_level = btrfs_header_level(eb);
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400626 if (found_level >= BTRFS_MAX_LEVEL) {
Su Yue893bf4b2018-06-22 09:52:15 +0800627 btrfs_err(fs_info, "bad tree block level %d on %llu",
628 (int)btrfs_header_level(eb), eb->start);
Josef Bacik1c24c3c2013-04-23 11:30:14 -0400629 ret = -EIO;
630 goto err;
631 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400632
Chris Mason85d4e462011-07-26 16:11:19 -0400633 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb),
634 eb, found_level);
Chris Mason4008c042009-02-12 14:09:45 -0500635
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100636 ret = csum_tree_block(eb, result);
Alex Lyakas8bd98f02016-03-10 13:09:46 +0200637 if (ret)
Josef Bacika826d6d2011-03-16 13:42:43 -0400638 goto err;
Josef Bacika826d6d2011-03-16 13:42:43 -0400639
Johannes Thumshirn2996e1f2019-02-25 14:24:15 +0100640 if (memcmp_extent_buffer(eb, result, 0, csum_size)) {
641 u32 val;
642 u32 found = 0;
643
644 memcpy(&found, result, csum_size);
645
646 read_extent_buffer(eb, &val, 0, csum_size);
647 btrfs_warn_rl(fs_info,
648 "%s checksum verify failed on %llu wanted %x found %x level %d",
649 fs_info->sb->s_id, eb->start,
650 val, found, btrfs_header_level(eb));
651 ret = -EUCLEAN;
652 goto err;
653 }
654
Josef Bacika826d6d2011-03-16 13:42:43 -0400655 /*
656 * If this is a leaf block and it is corrupt, set the corrupt bit so
657 * that we don't try and read the other copies of this block, just
658 * return -EIO.
659 */
David Sterba1c4360e2019-03-20 16:23:29 +0100660 if (found_level == 0 && btrfs_check_leaf_full(eb)) {
Josef Bacika826d6d2011-03-16 13:42:43 -0400661 set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
662 ret = -EIO;
663 }
Chris Masonce9adaa2008-04-09 16:28:12 -0400664
David Sterba813fd1d2019-03-20 16:25:00 +0100665 if (found_level > 0 && btrfs_check_node(eb))
Liu Bo053ab70f2016-08-23 17:37:45 -0700666 ret = -EIO;
667
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400668 if (!ret)
669 set_extent_buffer_uptodate(eb);
Qu Wenruo75391f02019-03-20 14:27:40 +0800670 else
671 btrfs_err(fs_info,
672 "block=%llu read time tree block corruption detected",
673 eb->start);
Chris Masonce9adaa2008-04-09 16:28:12 -0400674err:
Josef Bacik79fb65a2013-04-20 10:18:27 -0400675 if (reads_done &&
676 test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
David Sterbad48d71a2017-03-02 19:43:30 +0100677 btree_readahead_hook(eb, ret);
Arne Jansen4bb31e92011-06-10 13:55:54 +0200678
David Woodhouse53b381b2013-01-29 18:40:14 -0500679 if (ret) {
680 /*
681 * our io error hook is going to dec the io pages
682 * again, we have to make sure it has something
683 * to decrement
684 */
685 atomic_inc(&eb->io_pages);
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400686 clear_extent_buffer_uptodate(eb);
David Woodhouse53b381b2013-01-29 18:40:14 -0500687 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400688 free_extent_buffer(eb);
Chris Masonce9adaa2008-04-09 16:28:12 -0400689out:
Chris Masonf1885912008-04-09 16:28:12 -0400690 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -0400691}
692
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200693static void end_workqueue_bio(struct bio *bio)
Chris Masonce9adaa2008-04-09 16:28:12 -0400694{
David Sterba97eb6b62014-07-30 00:55:42 +0200695 struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
Chris Masonce9adaa2008-04-09 16:28:12 -0400696 struct btrfs_fs_info *fs_info;
Liu Bo9e0af232014-08-15 23:36:53 +0800697 struct btrfs_workqueue *wq;
698 btrfs_work_func_t func;
Chris Masonce9adaa2008-04-09 16:28:12 -0400699
Chris Masonce9adaa2008-04-09 16:28:12 -0400700 fs_info = end_io_wq->info;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200701 end_io_wq->status = bio->bi_status;
Chris Masond20f7042008-12-08 16:58:54 -0500702
Mike Christie37226b22016-06-05 14:31:52 -0500703 if (bio_op(bio) == REQ_OP_WRITE) {
Liu Bo9e0af232014-08-15 23:36:53 +0800704 if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) {
705 wq = fs_info->endio_meta_write_workers;
706 func = btrfs_endio_meta_write_helper;
707 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) {
708 wq = fs_info->endio_freespace_worker;
709 func = btrfs_freespace_write_helper;
710 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
711 wq = fs_info->endio_raid56_workers;
712 func = btrfs_endio_raid56_helper;
713 } else {
714 wq = fs_info->endio_write_workers;
715 func = btrfs_endio_write_helper;
716 }
Chris Masond20f7042008-12-08 16:58:54 -0500717 } else {
Miao Xie8b110e32014-09-12 18:44:03 +0800718 if (unlikely(end_io_wq->metadata ==
719 BTRFS_WQ_ENDIO_DIO_REPAIR)) {
720 wq = fs_info->endio_repair_workers;
721 func = btrfs_endio_repair_helper;
722 } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
Liu Bo9e0af232014-08-15 23:36:53 +0800723 wq = fs_info->endio_raid56_workers;
724 func = btrfs_endio_raid56_helper;
725 } else if (end_io_wq->metadata) {
726 wq = fs_info->endio_meta_workers;
727 func = btrfs_endio_meta_helper;
728 } else {
729 wq = fs_info->endio_workers;
730 func = btrfs_endio_helper;
731 }
Chris Masond20f7042008-12-08 16:58:54 -0500732 }
Liu Bo9e0af232014-08-15 23:36:53 +0800733
734 btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL);
735 btrfs_queue_work(wq, &end_io_wq->work);
Chris Masonce9adaa2008-04-09 16:28:12 -0400736}
737
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200738blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
David Sterbabfebd8b2014-07-30 00:25:45 +0200739 enum btrfs_wq_endio_type metadata)
Chris Mason0b86a832008-03-24 15:01:56 -0400740{
David Sterba97eb6b62014-07-30 00:55:42 +0200741 struct btrfs_end_io_wq *end_io_wq;
Miao Xie8b110e32014-09-12 18:44:03 +0800742
David Sterba97eb6b62014-07-30 00:55:42 +0200743 end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
Chris Masonce9adaa2008-04-09 16:28:12 -0400744 if (!end_io_wq)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200745 return BLK_STS_RESOURCE;
Chris Masonce9adaa2008-04-09 16:28:12 -0400746
747 end_io_wq->private = bio->bi_private;
748 end_io_wq->end_io = bio->bi_end_io;
Chris Mason22c59942008-04-09 16:28:12 -0400749 end_io_wq->info = info;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200750 end_io_wq->status = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -0400751 end_io_wq->bio = bio;
Chris Mason22c59942008-04-09 16:28:12 -0400752 end_io_wq->metadata = metadata;
Chris Masonce9adaa2008-04-09 16:28:12 -0400753
754 bio->bi_private = end_io_wq;
755 bio->bi_end_io = end_workqueue_bio;
Chris Mason22c59942008-04-09 16:28:12 -0400756 return 0;
757}
758
Chris Mason4a69a412008-11-06 22:03:00 -0500759static void run_one_async_start(struct btrfs_work *work)
760{
Chris Mason4a69a412008-11-06 22:03:00 -0500761 struct async_submit_bio *async;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200762 blk_status_t ret;
Chris Mason4a69a412008-11-06 22:03:00 -0500763
764 async = container_of(work, struct async_submit_bio, work);
Josef Bacikc6100a42017-05-05 11:57:13 -0400765 ret = async->submit_bio_start(async->private_data, async->bio,
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100766 async->bio_offset);
767 if (ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200768 async->status = ret;
Chris Mason4a69a412008-11-06 22:03:00 -0500769}
770
David Sterba06ea01b2018-07-17 22:08:41 +0200771/*
772 * In order to insert checksums into the metadata in large chunks, we wait
773 * until bio submission time. All the pages in the bio are checksummed and
774 * sums are attached onto the ordered extent record.
775 *
776 * At IO completion time the csums attached on the ordered extent record are
777 * inserted into the tree.
778 */
Chris Mason4a69a412008-11-06 22:03:00 -0500779static void run_one_async_done(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400780{
Chris Mason8b712842008-06-11 16:50:36 -0400781 struct async_submit_bio *async;
David Sterba06ea01b2018-07-17 22:08:41 +0200782 struct inode *inode;
783 blk_status_t ret;
Chris Mason8b712842008-06-11 16:50:36 -0400784
785 async = container_of(work, struct async_submit_bio, work);
David Sterba06ea01b2018-07-17 22:08:41 +0200786 inode = async->private_data;
Chris Mason4854ddd2008-08-15 15:34:17 -0400787
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800788 /* If an error occurred we just want to clean up the bio and move on */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200789 if (async->status) {
790 async->bio->bi_status = async->status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200791 bio_endio(async->bio);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100792 return;
793 }
794
David Sterba06ea01b2018-07-17 22:08:41 +0200795 ret = btrfs_map_bio(btrfs_sb(inode->i_sb), async->bio,
796 async->mirror_num, 1);
797 if (ret) {
798 async->bio->bi_status = ret;
799 bio_endio(async->bio);
800 }
Chris Mason4a69a412008-11-06 22:03:00 -0500801}
802
803static void run_one_async_free(struct btrfs_work *work)
804{
805 struct async_submit_bio *async;
806
807 async = container_of(work, struct async_submit_bio, work);
Chris Mason8b712842008-06-11 16:50:36 -0400808 kfree(async);
809}
810
Linus Torvalds8c27cb32017-07-05 16:41:23 -0700811blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
812 int mirror_num, unsigned long bio_flags,
813 u64 bio_offset, void *private_data,
David Sterbae288c082018-07-18 17:36:24 +0200814 extent_submit_bio_start_t *submit_bio_start)
Chris Mason44b8bd72008-04-16 11:14:51 -0400815{
816 struct async_submit_bio *async;
817
818 async = kmalloc(sizeof(*async), GFP_NOFS);
819 if (!async)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200820 return BLK_STS_RESOURCE;
Chris Mason44b8bd72008-04-16 11:14:51 -0400821
Josef Bacikc6100a42017-05-05 11:57:13 -0400822 async->private_data = private_data;
Chris Mason44b8bd72008-04-16 11:14:51 -0400823 async->bio = bio;
824 async->mirror_num = mirror_num;
Chris Mason4a69a412008-11-06 22:03:00 -0500825 async->submit_bio_start = submit_bio_start;
Chris Mason4a69a412008-11-06 22:03:00 -0500826
Liu Bo9e0af232014-08-15 23:36:53 +0800827 btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start,
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800828 run_one_async_done, run_one_async_free);
Chris Mason4a69a412008-11-06 22:03:00 -0500829
Chris Masoneaf25d92010-05-25 09:48:28 -0400830 async->bio_offset = bio_offset;
Chris Mason8c8bee12008-09-29 11:19:10 -0400831
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200832 async->status = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100833
Christoph Hellwig67f055c2016-11-01 07:40:06 -0600834 if (op_is_sync(bio->bi_opf))
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800835 btrfs_set_work_high_priority(&async->work);
Chris Masond313d7a2009-04-20 15:50:09 -0400836
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +0800837 btrfs_queue_work(fs_info->workers, &async->work);
Chris Mason44b8bd72008-04-16 11:14:51 -0400838 return 0;
839}
840
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200841static blk_status_t btree_csum_one_bio(struct bio *bio)
Chris Masonce3ed712008-09-23 13:14:12 -0400842{
Kent Overstreet2c30c712013-11-07 12:20:26 -0800843 struct bio_vec *bvec;
Chris Masonce3ed712008-09-23 13:14:12 -0400844 struct btrfs_root *root;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200845 int ret = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +0800846 struct bvec_iter_all iter_all;
Chris Masonce3ed712008-09-23 13:14:12 -0400847
David Sterbac09abff2017-07-13 18:10:07 +0200848 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200849 bio_for_each_segment_all(bvec, bio, iter_all) {
Chris Masonce3ed712008-09-23 13:14:12 -0400850 root = BTRFS_I(bvec->bv_page->mapping->host)->root;
Daniel Dressler01d58472014-11-21 17:15:07 +0900851 ret = csum_dirty_buffer(root->fs_info, bvec->bv_page);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100852 if (ret)
853 break;
Chris Masonce3ed712008-09-23 13:14:12 -0400854 }
Kent Overstreet2c30c712013-11-07 12:20:26 -0800855
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200856 return errno_to_blk_status(ret);
Chris Masonce3ed712008-09-23 13:14:12 -0400857}
858
David Sterbad0ee3932018-03-08 14:35:48 +0100859static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio,
Linus Torvalds8c27cb32017-07-05 16:41:23 -0700860 u64 bio_offset)
Chris Mason22c59942008-04-09 16:28:12 -0400861{
Chris Mason8b712842008-06-11 16:50:36 -0400862 /*
863 * when we're called for a write, we're already in the async
Chris Mason5443be42008-08-15 15:34:16 -0400864 * submission context. Just jump into btrfs_map_bio
Chris Mason8b712842008-06-11 16:50:36 -0400865 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100866 return btree_csum_one_bio(bio);
Chris Mason4a69a412008-11-06 22:03:00 -0500867}
Chris Mason22c59942008-04-09 16:28:12 -0400868
David Sterba9b4e6752019-05-16 13:39:59 +0200869static int check_async_write(struct btrfs_fs_info *fs_info,
870 struct btrfs_inode *bi)
Josef Bacikde0022b2012-09-25 14:25:58 -0400871{
Liu Bo63004632017-08-21 15:49:59 -0600872 if (atomic_read(&bi->sync_writers))
873 return 0;
David Sterba9b4e6752019-05-16 13:39:59 +0200874 if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags))
Josef Bacikde0022b2012-09-25 14:25:58 -0400875 return 0;
Josef Bacikde0022b2012-09-25 14:25:58 -0400876 return 1;
877}
878
Nikolay Borisova56b1c72019-04-10 17:24:39 +0300879static blk_status_t btree_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +0300880 int mirror_num,
881 unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -0400882{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400883 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba9b4e6752019-05-16 13:39:59 +0200884 int async = check_async_write(fs_info, BTRFS_I(inode));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200885 blk_status_t ret;
Chris Masoncad321a2008-12-17 14:51:42 -0500886
Mike Christie37226b22016-06-05 14:31:52 -0500887 if (bio_op(bio) != REQ_OP_WRITE) {
Chris Mason4a69a412008-11-06 22:03:00 -0500888 /*
889 * called for a read, do the setup so that checksum validation
890 * can happen in the async kernel threads
891 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400892 ret = btrfs_bio_wq_end_io(fs_info, bio,
893 BTRFS_WQ_ENDIO_METADATA);
Chris Mason1d4284b2012-03-28 20:31:37 -0400894 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +0100895 goto out_w_error;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400896 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Josef Bacikde0022b2012-09-25 14:25:58 -0400897 } else if (!async) {
898 ret = btree_csum_one_bio(bio);
899 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +0100900 goto out_w_error;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400901 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +0100902 } else {
903 /*
904 * kthread helpers are used to submit writes so that
905 * checksumming can happen in parallel across all CPUs
906 */
Josef Bacikc6100a42017-05-05 11:57:13 -0400907 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0,
Nikolay Borisove68f2ee2019-04-10 17:24:42 +0300908 0, inode, btree_submit_bio_start);
Chris Mason44b8bd72008-04-16 11:14:51 -0400909 }
Chris Masond313d7a2009-04-20 15:50:09 -0400910
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200911 if (ret)
912 goto out_w_error;
913 return 0;
914
Stefan Behrens61891922012-11-05 18:51:52 +0100915out_w_error:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200916 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200917 bio_endio(bio);
Stefan Behrens61891922012-11-05 18:51:52 +0100918 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -0400919}
920
Jan Beulich3dd14622010-12-07 14:54:09 +0000921#ifdef CONFIG_MIGRATION
Chris Mason784b4e22010-11-21 22:20:49 -0500922static int btree_migratepage(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800923 struct page *newpage, struct page *page,
924 enum migrate_mode mode)
Chris Mason784b4e22010-11-21 22:20:49 -0500925{
926 /*
927 * we can't safely write a btree page from here,
928 * we haven't done the locking hook
929 */
930 if (PageDirty(page))
931 return -EAGAIN;
932 /*
933 * Buffers may be managed in a filesystem specific way.
934 * We must have no buffers or drop them.
935 */
936 if (page_has_private(page) &&
937 !try_to_release_page(page, GFP_KERNEL))
938 return -EAGAIN;
Mel Gormana6bc32b2012-01-12 17:19:43 -0800939 return migrate_page(mapping, newpage, page, mode);
Chris Mason784b4e22010-11-21 22:20:49 -0500940}
Jan Beulich3dd14622010-12-07 14:54:09 +0000941#endif
Chris Mason784b4e22010-11-21 22:20:49 -0500942
Chris Mason0da54682007-11-07 21:08:01 -0500943
944static int btree_writepages(struct address_space *mapping,
945 struct writeback_control *wbc)
946{
Miao Xiee2d84522013-01-29 10:09:20 +0000947 struct btrfs_fs_info *fs_info;
948 int ret;
949
Chris Masond8d5f3e2007-12-11 12:42:00 -0500950 if (wbc->sync_mode == WB_SYNC_NONE) {
Chris Mason448d6402007-11-27 07:52:01 -0800951
952 if (wbc->for_kupdate)
953 return 0;
954
Miao Xiee2d84522013-01-29 10:09:20 +0000955 fs_info = BTRFS_I(mapping->host)->root->fs_info;
Chris Masonb9473432009-03-13 11:00:37 -0400956 /* this is a bit racy, but that's ok */
Ethan Liend814a492018-07-02 15:44:58 +0800957 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
958 BTRFS_DIRTY_METADATA_THRESH,
959 fs_info->dirty_metadata_batch);
Miao Xiee2d84522013-01-29 10:09:20 +0000960 if (ret < 0)
Chris Mason793955b2007-11-26 16:34:41 -0800961 return 0;
Chris Mason793955b2007-11-26 16:34:41 -0800962 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400963 return btree_write_cache_pages(mapping, wbc);
Chris Mason0da54682007-11-07 21:08:01 -0500964}
965
Christoph Hellwigb2950862008-12-02 09:54:17 -0500966static int btree_readpage(struct file *file, struct page *page)
Chris Mason5f39d392007-10-15 16:14:19 -0400967{
Chris Masond1310b22008-01-24 16:13:08 -0500968 struct extent_io_tree *tree;
969 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +0200970 return extent_read_full_page(tree, page, btree_get_extent, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400971}
972
Chris Mason70dec802008-01-29 09:59:12 -0500973static int btree_releasepage(struct page *page, gfp_t gfp_flags)
Chris Mason5f39d392007-10-15 16:14:19 -0400974{
Chris Mason98509cf2008-09-11 15:51:43 -0400975 if (PageWriteback(page) || PageDirty(page))
Chris Masond3977122009-01-05 21:25:51 -0500976 return 0;
David Sterba0c4e5382012-01-26 15:01:12 -0500977
David Sterbaf7a52a42013-04-26 14:56:29 +0000978 return try_release_extent_buffer(page);
Chris Masone20d96d2007-03-22 12:13:20 -0400979}
Chris Mason123abc82007-03-14 14:14:43 -0400980
Lukas Czernerd47992f2013-05-21 23:17:23 -0400981static void btree_invalidatepage(struct page *page, unsigned int offset,
982 unsigned int length)
Chris Masond98237b2007-03-28 13:57:48 -0400983{
Chris Masond1310b22008-01-24 16:13:08 -0500984 struct extent_io_tree *tree;
985 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Mason5f39d392007-10-15 16:14:19 -0400986 extent_invalidatepage(tree, page, offset);
987 btree_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -0400988 if (PagePrivate(page)) {
Frank Holtonefe120a2013-12-20 11:37:06 -0500989 btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
990 "page private not zero on page %llu",
991 (unsigned long long)page_offset(page));
Chris Mason9ad6b7bc2008-04-18 16:11:30 -0400992 ClearPagePrivate(page);
993 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300994 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -0400995 }
Chris Masond98237b2007-03-28 13:57:48 -0400996}
997
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400998static int btree_set_page_dirty(struct page *page)
999{
Josef Bacikbb146eb2012-10-15 13:30:43 -04001000#ifdef DEBUG
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001001 struct extent_buffer *eb;
1002
1003 BUG_ON(!PagePrivate(page));
1004 eb = (struct extent_buffer *)page->private;
1005 BUG_ON(!eb);
1006 BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
1007 BUG_ON(!atomic_read(&eb->refs));
1008 btrfs_assert_tree_locked(eb);
Josef Bacikbb146eb2012-10-15 13:30:43 -04001009#endif
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001010 return __set_page_dirty_nobuffers(page);
1011}
1012
Alexey Dobriyan7f094102009-09-21 17:01:10 -07001013static const struct address_space_operations btree_aops = {
Chris Masond98237b2007-03-28 13:57:48 -04001014 .readpage = btree_readpage,
Chris Mason0da54682007-11-07 21:08:01 -05001015 .writepages = btree_writepages,
Chris Mason5f39d392007-10-15 16:14:19 -04001016 .releasepage = btree_releasepage,
1017 .invalidatepage = btree_invalidatepage,
Chris Mason5a92bc82010-11-29 09:49:11 -05001018#ifdef CONFIG_MIGRATION
Chris Mason784b4e22010-11-21 22:20:49 -05001019 .migratepage = btree_migratepage,
Chris Mason5a92bc82010-11-29 09:49:11 -05001020#endif
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001021 .set_page_dirty = btree_set_page_dirty,
Chris Masond98237b2007-03-28 13:57:48 -04001022};
1023
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001024void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr)
Chris Mason090d1872007-05-01 08:53:32 -04001025{
Chris Mason5f39d392007-10-15 16:14:19 -04001026 struct extent_buffer *buf = NULL;
Nikolay Borisov537f38f2019-03-14 09:52:35 +02001027 int ret;
Chris Mason090d1872007-05-01 08:53:32 -04001028
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001029 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07001030 if (IS_ERR(buf))
David Sterba6197d862014-06-15 00:49:36 +02001031 return;
Nikolay Borisov537f38f2019-03-14 09:52:35 +02001032
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03001033 ret = read_extent_buffer_pages(buf, WAIT_NONE, 0);
Nikolay Borisov537f38f2019-03-14 09:52:35 +02001034 if (ret < 0)
1035 free_extent_buffer_stale(buf);
1036 else
1037 free_extent_buffer(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001038}
1039
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001040int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
Arne Jansenab0fff02011-05-23 14:25:41 +02001041 int mirror_num, struct extent_buffer **eb)
1042{
1043 struct extent_buffer *buf = NULL;
Arne Jansenab0fff02011-05-23 14:25:41 +02001044 int ret;
1045
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001046 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07001047 if (IS_ERR(buf))
Arne Jansenab0fff02011-05-23 14:25:41 +02001048 return 0;
1049
1050 set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags);
1051
Nikolay Borisovc2ccfbc2019-04-10 17:24:40 +03001052 ret = read_extent_buffer_pages(buf, WAIT_PAGE_LOCK, mirror_num);
Arne Jansenab0fff02011-05-23 14:25:41 +02001053 if (ret) {
Nikolay Borisov537f38f2019-03-14 09:52:35 +02001054 free_extent_buffer_stale(buf);
Arne Jansenab0fff02011-05-23 14:25:41 +02001055 return ret;
1056 }
1057
1058 if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
Nikolay Borisov537f38f2019-03-14 09:52:35 +02001059 free_extent_buffer_stale(buf);
Arne Jansenab0fff02011-05-23 14:25:41 +02001060 return -EIO;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04001061 } else if (extent_buffer_uptodate(buf)) {
Arne Jansenab0fff02011-05-23 14:25:41 +02001062 *eb = buf;
1063 } else {
1064 free_extent_buffer(buf);
1065 }
1066 return 0;
1067}
1068
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001069struct extent_buffer *btrfs_find_create_tree_block(
1070 struct btrfs_fs_info *fs_info,
1071 u64 bytenr)
Chris Mason0999df52008-04-01 13:48:14 -04001072{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001073 if (btrfs_is_testing(fs_info))
1074 return alloc_test_extent_buffer(fs_info, bytenr);
1075 return alloc_extent_buffer(fs_info, bytenr);
Chris Mason0999df52008-04-01 13:48:14 -04001076}
1077
Qu Wenruo581c1762018-03-29 09:08:11 +08001078/*
1079 * Read tree block at logical address @bytenr and do variant basic but critical
1080 * verification.
1081 *
1082 * @parent_transid: expected transid of this tree block, skip check if 0
1083 * @level: expected level, mandatory check
1084 * @first_key: expected key in slot 0, skip check if NULL
1085 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001086struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
Qu Wenruo581c1762018-03-29 09:08:11 +08001087 u64 parent_transid, int level,
1088 struct btrfs_key *first_key)
Chris Masone20d96d2007-03-22 12:13:20 -04001089{
Chris Mason5f39d392007-10-15 16:14:19 -04001090 struct extent_buffer *buf = NULL;
Chris Mason19c00dd2007-10-15 16:19:22 -04001091 int ret;
1092
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001093 buf = btrfs_find_create_tree_block(fs_info, bytenr);
Liu Boc871b0f2016-06-06 12:01:23 -07001094 if (IS_ERR(buf))
1095 return buf;
Chris Masone4204de2008-01-08 15:46:27 -05001096
David Sterba5ab12d12019-03-20 14:56:39 +01001097 ret = btree_read_extent_buffer_pages(buf, parent_transid,
Qu Wenruo581c1762018-03-29 09:08:11 +08001098 level, first_key);
Filipe David Borba Manana0f0fe8f2013-07-31 00:39:56 +01001099 if (ret) {
Nikolay Borisov537f38f2019-03-14 09:52:35 +02001100 free_extent_buffer_stale(buf);
Liu Bo64c043d2015-05-25 17:30:15 +08001101 return ERR_PTR(ret);
Filipe David Borba Manana0f0fe8f2013-07-31 00:39:56 +01001102 }
Chris Mason5f39d392007-10-15 16:14:19 -04001103 return buf;
Chris Masonce9adaa2008-04-09 16:28:12 -04001104
Chris Masoneb60cea2007-02-02 09:18:22 -05001105}
1106
David Sterba6a884d7d2019-03-20 14:30:02 +01001107void btrfs_clean_tree_block(struct extent_buffer *buf)
Chris Masoned2ff2c2007-03-01 18:59:40 -05001108{
David Sterba6a884d7d2019-03-20 14:30:02 +01001109 struct btrfs_fs_info *fs_info = buf->fs_info;
Chris Mason55c69072008-01-09 15:55:33 -05001110 if (btrfs_header_generation(buf) ==
Miao Xiee2d84522013-01-29 10:09:20 +00001111 fs_info->running_transaction->transid) {
Chris Masonb9447ef82009-03-09 11:45:38 -04001112 btrfs_assert_tree_locked(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001113
Chris Masonb9473432009-03-13 11:00:37 -04001114 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001115 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
1116 -buf->len,
1117 fs_info->dirty_metadata_batch);
Josef Baciked7b63e2012-10-15 13:33:54 -04001118 /* ugh, clear_extent_buffer_dirty needs to lock the page */
David Sterba8bead252018-04-04 02:03:48 +02001119 btrfs_set_lock_blocking_write(buf);
Josef Baciked7b63e2012-10-15 13:33:54 -04001120 clear_extent_buffer_dirty(buf);
1121 }
Chris Mason925baed2008-06-25 16:01:30 -04001122 }
Chris Mason5f39d392007-10-15 16:14:19 -04001123}
1124
Miao Xie8257b2d2014-03-06 13:38:19 +08001125static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void)
1126{
1127 struct btrfs_subvolume_writers *writers;
1128 int ret;
1129
1130 writers = kmalloc(sizeof(*writers), GFP_NOFS);
1131 if (!writers)
1132 return ERR_PTR(-ENOMEM);
1133
Jeff Mahoney8a5a9162018-03-16 14:36:27 -04001134 ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS);
Miao Xie8257b2d2014-03-06 13:38:19 +08001135 if (ret < 0) {
1136 kfree(writers);
1137 return ERR_PTR(ret);
1138 }
1139
1140 init_waitqueue_head(&writers->wait);
1141 return writers;
1142}
1143
1144static void
1145btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers)
1146{
1147 percpu_counter_destroy(&writers->counter);
1148 kfree(writers);
1149}
1150
Jeff Mahoneyda170662016-06-15 09:22:56 -04001151static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001152 u64 objectid)
Chris Masond97e63b2007-02-20 16:40:44 -05001153{
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001154 bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
Chris Masoncfaa7292007-02-21 17:04:57 -05001155 root->node = NULL;
Chris Masona28ec192007-03-06 20:08:01 -05001156 root->commit_root = NULL;
Miao Xie27cdeb72014-04-02 19:51:05 +08001157 root->state = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04001158 root->orphan_cleanup_state = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001159
Chris Mason0f7d52f2007-04-09 10:42:37 -04001160 root->last_trans = 0;
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001161 root->highest_objectid = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00001162 root->nr_delalloc_inodes = 0;
Miao Xie199c2a92013-05-15 07:48:23 +00001163 root->nr_ordered_extents = 0;
Eric Paris6bef4d32010-02-23 19:43:04 +00001164 root->inode_tree = RB_ROOT;
Miao Xie16cdcec2011-04-22 18:12:22 +08001165 INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001166 root->block_rsv = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04001167
1168 INIT_LIST_HEAD(&root->dirty_list);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001169 INIT_LIST_HEAD(&root->root_list);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001170 INIT_LIST_HEAD(&root->delalloc_inodes);
1171 INIT_LIST_HEAD(&root->delalloc_root);
Miao Xie199c2a92013-05-15 07:48:23 +00001172 INIT_LIST_HEAD(&root->ordered_extents);
1173 INIT_LIST_HEAD(&root->ordered_root);
Qu Wenruod2311e62019-01-23 15:15:14 +08001174 INIT_LIST_HEAD(&root->reloc_dirty_list);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001175 INIT_LIST_HEAD(&root->logged_list[0]);
1176 INIT_LIST_HEAD(&root->logged_list[1]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001177 spin_lock_init(&root->inode_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001178 spin_lock_init(&root->delalloc_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00001179 spin_lock_init(&root->ordered_extent_lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001180 spin_lock_init(&root->accounting_lock);
Josef Bacik2ab28f32012-10-12 15:27:49 -04001181 spin_lock_init(&root->log_extents_lock[0]);
1182 spin_lock_init(&root->log_extents_lock[1]);
Qu Wenruo82874752017-12-12 15:34:34 +08001183 spin_lock_init(&root->qgroup_meta_rsv_lock);
Chris Masona2135012008-06-25 16:01:30 -04001184 mutex_init(&root->objectid_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001185 mutex_init(&root->log_mutex);
Miao Xie31f3d252014-03-06 13:55:02 +08001186 mutex_init(&root->ordered_extent_mutex);
Miao Xie573bfb72014-03-06 13:55:03 +08001187 mutex_init(&root->delalloc_mutex);
Yan Zheng7237f182009-01-21 12:54:03 -05001188 init_waitqueue_head(&root->log_writer_wait);
1189 init_waitqueue_head(&root->log_commit_wait[0]);
1190 init_waitqueue_head(&root->log_commit_wait[1]);
Miao Xie8b050d32014-02-20 18:08:58 +08001191 INIT_LIST_HEAD(&root->log_ctxs[0]);
1192 INIT_LIST_HEAD(&root->log_ctxs[1]);
Yan Zheng7237f182009-01-21 12:54:03 -05001193 atomic_set(&root->log_commit[0], 0);
1194 atomic_set(&root->log_commit[1], 0);
1195 atomic_set(&root->log_writers, 0);
Miao Xie2ecb7922012-09-06 04:04:27 -06001196 atomic_set(&root->log_batch, 0);
Elena Reshetova0700cea2017-03-03 10:55:18 +02001197 refcount_set(&root->refs, 1);
David Sterbaea14b57f2017-06-22 02:19:11 +02001198 atomic_set(&root->will_be_snapshotted, 0);
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001199 atomic_set(&root->snapshot_force_cow, 0);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001200 atomic_set(&root->nr_swapfiles, 0);
Yan Zheng7237f182009-01-21 12:54:03 -05001201 root->log_transid = 0;
Miao Xied1433de2014-02-20 18:08:59 +08001202 root->log_transid_committed = -1;
Chris Mason257c62e2009-10-13 13:21:08 -04001203 root->last_log_commit = 0;
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001204 if (!dummy)
Qu Wenruo43eb5f22019-03-01 10:47:59 +08001205 extent_io_tree_init(fs_info, &root->dirty_log_pages,
1206 IO_TREE_ROOT_DIRTY_LOG_PAGES, NULL);
Chris Mason017e5362008-07-28 15:32:51 -04001207
Chris Mason3768f362007-03-13 16:47:54 -04001208 memset(&root->root_key, 0, sizeof(root->root_key));
1209 memset(&root->root_item, 0, sizeof(root->root_item));
Chris Mason6702ed42007-08-07 16:15:09 -04001210 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001211 if (!dummy)
Josef Bacik06ea65a2013-09-19 16:07:01 -04001212 root->defrag_trans_start = fs_info->generation;
1213 else
1214 root->defrag_trans_start = 0;
Chris Mason4d775672007-04-20 20:23:12 -04001215 root->root_key.objectid = objectid;
Al Viro0ee5dc62011-07-07 15:44:25 -04001216 root->anon_dev = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +02001217
Anand Jain5f3ab902012-12-07 09:28:54 +00001218 spin_lock_init(&root->root_item_lock);
Qu Wenruo370a11b2019-01-23 15:15:16 +08001219 btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
Chris Mason3768f362007-03-13 16:47:54 -04001220}
1221
David Sterba74e4d822016-02-11 11:01:55 +01001222static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
1223 gfp_t flags)
Al Viro6f07e422011-11-17 00:46:16 -05001224{
David Sterba74e4d822016-02-11 11:01:55 +01001225 struct btrfs_root *root = kzalloc(sizeof(*root), flags);
Al Viro6f07e422011-11-17 00:46:16 -05001226 if (root)
1227 root->fs_info = fs_info;
1228 return root;
1229}
1230
Josef Bacik06ea65a2013-09-19 16:07:01 -04001231#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1232/* Should only be used by the testing infrastructure */
Jeff Mahoneyda170662016-06-15 09:22:56 -04001233struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
Josef Bacik06ea65a2013-09-19 16:07:01 -04001234{
1235 struct btrfs_root *root;
1236
Jeff Mahoney7c0260e2016-06-20 14:14:09 -04001237 if (!fs_info)
1238 return ERR_PTR(-EINVAL);
1239
1240 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Josef Bacik06ea65a2013-09-19 16:07:01 -04001241 if (!root)
1242 return ERR_PTR(-ENOMEM);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001243
Feifei Xub9ef22d2016-06-01 19:18:25 +08001244 /* We don't use the stripesize in selftest, set it as sectorsize */
Jeff Mahoneyda170662016-06-15 09:22:56 -04001245 __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001246 root->alloc_bytenr = 0;
Josef Bacik06ea65a2013-09-19 16:07:01 -04001247
1248 return root;
1249}
1250#endif
1251
Arne Jansen20897f52011-09-13 12:44:20 +02001252struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
Arne Jansen20897f52011-09-13 12:44:20 +02001253 u64 objectid)
1254{
David Sterba9b7a2442019-03-20 13:20:49 +01001255 struct btrfs_fs_info *fs_info = trans->fs_info;
Arne Jansen20897f52011-09-13 12:44:20 +02001256 struct extent_buffer *leaf;
1257 struct btrfs_root *tree_root = fs_info->tree_root;
1258 struct btrfs_root *root;
1259 struct btrfs_key key;
Filipe Mananab89f6d12018-12-13 21:16:45 +00001260 unsigned int nofs_flag;
Arne Jansen20897f52011-09-13 12:44:20 +02001261 int ret = 0;
Qu Wenruo33d85fd2017-10-31 14:08:16 +08001262 uuid_le uuid = NULL_UUID_LE;
Arne Jansen20897f52011-09-13 12:44:20 +02001263
Filipe Mananab89f6d12018-12-13 21:16:45 +00001264 /*
1265 * We're holding a transaction handle, so use a NOFS memory allocation
1266 * context to avoid deadlock if reclaim happens.
1267 */
1268 nofs_flag = memalloc_nofs_save();
David Sterba74e4d822016-02-11 11:01:55 +01001269 root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Filipe Mananab89f6d12018-12-13 21:16:45 +00001270 memalloc_nofs_restore(nofs_flag);
Arne Jansen20897f52011-09-13 12:44:20 +02001271 if (!root)
1272 return ERR_PTR(-ENOMEM);
1273
Jeff Mahoneyda170662016-06-15 09:22:56 -04001274 __setup_root(root, fs_info, objectid);
Arne Jansen20897f52011-09-13 12:44:20 +02001275 root->root_key.objectid = objectid;
1276 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1277 root->root_key.offset = 0;
1278
David Sterba4d75f8a2014-06-15 01:54:12 +02001279 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
Arne Jansen20897f52011-09-13 12:44:20 +02001280 if (IS_ERR(leaf)) {
1281 ret = PTR_ERR(leaf);
Tsutomu Itoh1dd05682013-03-21 04:32:32 +00001282 leaf = NULL;
Arne Jansen20897f52011-09-13 12:44:20 +02001283 goto fail;
1284 }
1285
Arne Jansen20897f52011-09-13 12:44:20 +02001286 root->node = leaf;
Arne Jansen20897f52011-09-13 12:44:20 +02001287 btrfs_mark_buffer_dirty(leaf);
1288
1289 root->commit_root = btrfs_root_node(root);
Miao Xie27cdeb72014-04-02 19:51:05 +08001290 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
Arne Jansen20897f52011-09-13 12:44:20 +02001291
1292 root->root_item.flags = 0;
1293 root->root_item.byte_limit = 0;
1294 btrfs_set_root_bytenr(&root->root_item, leaf->start);
1295 btrfs_set_root_generation(&root->root_item, trans->transid);
1296 btrfs_set_root_level(&root->root_item, 0);
1297 btrfs_set_root_refs(&root->root_item, 1);
1298 btrfs_set_root_used(&root->root_item, leaf->len);
1299 btrfs_set_root_last_snapshot(&root->root_item, 0);
1300 btrfs_set_root_dirid(&root->root_item, 0);
Qu Wenruo33d85fd2017-10-31 14:08:16 +08001301 if (is_fstree(objectid))
1302 uuid_le_gen(&uuid);
Stefan Behrens6463fe52013-04-19 15:08:05 +00001303 memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE);
Arne Jansen20897f52011-09-13 12:44:20 +02001304 root->root_item.drop_level = 0;
1305
1306 key.objectid = objectid;
1307 key.type = BTRFS_ROOT_ITEM_KEY;
1308 key.offset = 0;
1309 ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
1310 if (ret)
1311 goto fail;
1312
1313 btrfs_tree_unlock(leaf);
1314
Arne Jansen20897f52011-09-13 12:44:20 +02001315 return root;
Tsutomu Itoh1dd05682013-03-21 04:32:32 +00001316
1317fail:
1318 if (leaf) {
1319 btrfs_tree_unlock(leaf);
Tsutomu Itoh59885b32014-04-09 09:18:04 +09001320 free_extent_buffer(root->commit_root);
Tsutomu Itoh1dd05682013-03-21 04:32:32 +00001321 free_extent_buffer(leaf);
1322 }
1323 kfree(root);
1324
1325 return ERR_PTR(ret);
Arne Jansen20897f52011-09-13 12:44:20 +02001326}
1327
Yan Zheng7237f182009-01-21 12:54:03 -05001328static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1329 struct btrfs_fs_info *fs_info)
Chris Mason0f7d52f2007-04-09 10:42:37 -04001330{
1331 struct btrfs_root *root;
Yan Zheng7237f182009-01-21 12:54:03 -05001332 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04001333
David Sterba74e4d822016-02-11 11:01:55 +01001334 root = btrfs_alloc_root(fs_info, GFP_NOFS);
Chris Masone02119d2008-09-05 16:13:11 -04001335 if (!root)
Yan Zheng7237f182009-01-21 12:54:03 -05001336 return ERR_PTR(-ENOMEM);
Chris Masone02119d2008-09-05 16:13:11 -04001337
Jeff Mahoneyda170662016-06-15 09:22:56 -04001338 __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID);
Chris Masone02119d2008-09-05 16:13:11 -04001339
1340 root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
1341 root->root_key.type = BTRFS_ROOT_ITEM_KEY;
1342 root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
Miao Xie27cdeb72014-04-02 19:51:05 +08001343
Yan Zheng7237f182009-01-21 12:54:03 -05001344 /*
Miao Xie27cdeb72014-04-02 19:51:05 +08001345 * DON'T set REF_COWS for log trees
1346 *
Yan Zheng7237f182009-01-21 12:54:03 -05001347 * log trees do not get reference counted because they go away
1348 * before a real commit is actually done. They do store pointers
1349 * to file data extents, and those reference counts still get
1350 * updated (along with back refs to the log tree).
1351 */
Chris Masone02119d2008-09-05 16:13:11 -04001352
David Sterba4d75f8a2014-06-15 01:54:12 +02001353 leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
1354 NULL, 0, 0, 0);
Yan Zheng7237f182009-01-21 12:54:03 -05001355 if (IS_ERR(leaf)) {
1356 kfree(root);
1357 return ERR_CAST(leaf);
1358 }
Chris Masone02119d2008-09-05 16:13:11 -04001359
Yan Zheng7237f182009-01-21 12:54:03 -05001360 root->node = leaf;
Chris Masone02119d2008-09-05 16:13:11 -04001361
Chris Masone02119d2008-09-05 16:13:11 -04001362 btrfs_mark_buffer_dirty(root->node);
1363 btrfs_tree_unlock(root->node);
Yan Zheng7237f182009-01-21 12:54:03 -05001364 return root;
1365}
1366
1367int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1368 struct btrfs_fs_info *fs_info)
1369{
1370 struct btrfs_root *log_root;
1371
1372 log_root = alloc_log_tree(trans, fs_info);
1373 if (IS_ERR(log_root))
1374 return PTR_ERR(log_root);
1375 WARN_ON(fs_info->log_root_tree);
1376 fs_info->log_root_tree = log_root;
1377 return 0;
1378}
1379
1380int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root)
1382{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001383 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng7237f182009-01-21 12:54:03 -05001384 struct btrfs_root *log_root;
1385 struct btrfs_inode_item *inode_item;
1386
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001387 log_root = alloc_log_tree(trans, fs_info);
Yan Zheng7237f182009-01-21 12:54:03 -05001388 if (IS_ERR(log_root))
1389 return PTR_ERR(log_root);
1390
1391 log_root->last_trans = trans->transid;
1392 log_root->root_key.offset = root->root_key.objectid;
1393
1394 inode_item = &log_root->root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001395 btrfs_set_stack_inode_generation(inode_item, 1);
1396 btrfs_set_stack_inode_size(inode_item, 3);
1397 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001398 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001399 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001400 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Yan Zheng7237f182009-01-21 12:54:03 -05001401
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001402 btrfs_set_root_node(&log_root->root_item, log_root->node);
Yan Zheng7237f182009-01-21 12:54:03 -05001403
1404 WARN_ON(root->log_root);
1405 root->log_root = log_root;
1406 root->log_transid = 0;
Miao Xied1433de2014-02-20 18:08:59 +08001407 root->log_transid_committed = -1;
Chris Mason257c62e2009-10-13 13:21:08 -04001408 root->last_log_commit = 0;
Chris Masone02119d2008-09-05 16:13:11 -04001409 return 0;
1410}
1411
Stefan Behrens35a36212013-08-14 18:12:25 +02001412static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1413 struct btrfs_key *key)
Chris Masone02119d2008-09-05 16:13:11 -04001414{
1415 struct btrfs_root *root;
1416 struct btrfs_fs_info *fs_info = tree_root->fs_info;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001417 struct btrfs_path *path;
Yan Zheng84234f32008-10-29 14:49:05 -04001418 u64 generation;
Miao Xiecb517ea2013-05-15 07:48:19 +00001419 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08001420 int level;
Miao Xiecb517ea2013-05-15 07:48:19 +00001421
1422 path = btrfs_alloc_path();
1423 if (!path)
1424 return ERR_PTR(-ENOMEM);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001425
David Sterba74e4d822016-02-11 11:01:55 +01001426 root = btrfs_alloc_root(fs_info, GFP_NOFS);
Miao Xiecb517ea2013-05-15 07:48:19 +00001427 if (!root) {
1428 ret = -ENOMEM;
1429 goto alloc_fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001430 }
1431
Jeff Mahoneyda170662016-06-15 09:22:56 -04001432 __setup_root(root, fs_info, key->objectid);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001433
Miao Xiecb517ea2013-05-15 07:48:19 +00001434 ret = btrfs_find_root(tree_root, key, path,
1435 &root->root_item, &root->root_key);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001436 if (ret) {
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001437 if (ret > 0)
1438 ret = -ENOENT;
Miao Xiecb517ea2013-05-15 07:48:19 +00001439 goto find_fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001440 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001441
Yan Zheng84234f32008-10-29 14:49:05 -04001442 generation = btrfs_root_generation(&root->root_item);
Qu Wenruo581c1762018-03-29 09:08:11 +08001443 level = btrfs_root_level(&root->root_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001444 root->node = read_tree_block(fs_info,
1445 btrfs_root_bytenr(&root->root_item),
Qu Wenruo581c1762018-03-29 09:08:11 +08001446 generation, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08001447 if (IS_ERR(root->node)) {
1448 ret = PTR_ERR(root->node);
Miao Xiecb517ea2013-05-15 07:48:19 +00001449 goto find_fail;
1450 } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1451 ret = -EIO;
Liu Bo64c043d2015-05-25 17:30:15 +08001452 free_extent_buffer(root->node);
1453 goto find_fail;
Josef Bacik416bc652013-04-23 14:17:42 -04001454 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001455 root->commit_root = btrfs_root_node(root);
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001456out:
Miao Xiecb517ea2013-05-15 07:48:19 +00001457 btrfs_free_path(path);
1458 return root;
1459
Miao Xiecb517ea2013-05-15 07:48:19 +00001460find_fail:
1461 kfree(root);
1462alloc_fail:
1463 root = ERR_PTR(ret);
1464 goto out;
1465}
1466
1467struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
1468 struct btrfs_key *location)
1469{
1470 struct btrfs_root *root;
1471
1472 root = btrfs_read_tree_root(tree_root, location);
1473 if (IS_ERR(root))
1474 return root;
1475
1476 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Miao Xie27cdeb72014-04-02 19:51:05 +08001477 set_bit(BTRFS_ROOT_REF_COWS, &root->state);
Li Zefan08fe4db2011-03-28 02:01:25 +00001478 btrfs_check_and_init_root_item(&root->root_item);
1479 }
Yan, Zheng13a8a7c2009-09-21 15:56:00 -04001480
Chris Mason5eda7b52007-06-22 14:16:25 -04001481 return root;
1482}
1483
Miao Xiecb517ea2013-05-15 07:48:19 +00001484int btrfs_init_fs_root(struct btrfs_root *root)
1485{
1486 int ret;
Miao Xie8257b2d2014-03-06 13:38:19 +08001487 struct btrfs_subvolume_writers *writers;
Miao Xiecb517ea2013-05-15 07:48:19 +00001488
1489 root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
1490 root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
1491 GFP_NOFS);
1492 if (!root->free_ino_pinned || !root->free_ino_ctl) {
1493 ret = -ENOMEM;
1494 goto fail;
1495 }
1496
Miao Xie8257b2d2014-03-06 13:38:19 +08001497 writers = btrfs_alloc_subvolume_writers();
1498 if (IS_ERR(writers)) {
1499 ret = PTR_ERR(writers);
1500 goto fail;
1501 }
1502 root->subv_writers = writers;
1503
Miao Xiecb517ea2013-05-15 07:48:19 +00001504 btrfs_init_free_ino_ctl(root);
David Sterba57cdc8d2014-02-05 02:37:48 +01001505 spin_lock_init(&root->ino_cache_lock);
1506 init_waitqueue_head(&root->ino_cache_wait);
Miao Xiecb517ea2013-05-15 07:48:19 +00001507
1508 ret = get_anon_bdev(&root->anon_dev);
1509 if (ret)
Liu Bo876d2cf2016-06-28 13:44:38 -07001510 goto fail;
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301511
1512 mutex_lock(&root->objectid_mutex);
1513 ret = btrfs_find_highest_objectid(root,
1514 &root->highest_objectid);
1515 if (ret) {
1516 mutex_unlock(&root->objectid_mutex);
Liu Bo876d2cf2016-06-28 13:44:38 -07001517 goto fail;
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05301518 }
1519
1520 ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
1521
1522 mutex_unlock(&root->objectid_mutex);
1523
Miao Xiecb517ea2013-05-15 07:48:19 +00001524 return 0;
1525fail:
David Sterba84db5cc2018-07-20 16:30:25 +02001526 /* The caller is responsible to call btrfs_free_fs_root */
Miao Xiecb517ea2013-05-15 07:48:19 +00001527 return ret;
1528}
1529
Jeff Mahoney35bbb972016-08-17 21:58:33 -04001530struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
1531 u64 root_id)
Miao Xiecb517ea2013-05-15 07:48:19 +00001532{
1533 struct btrfs_root *root;
1534
1535 spin_lock(&fs_info->fs_roots_radix_lock);
1536 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1537 (unsigned long)root_id);
1538 spin_unlock(&fs_info->fs_roots_radix_lock);
1539 return root;
1540}
1541
1542int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
1543 struct btrfs_root *root)
1544{
1545 int ret;
1546
David Sterbae1860a72016-05-09 14:11:38 +02001547 ret = radix_tree_preload(GFP_NOFS);
Miao Xiecb517ea2013-05-15 07:48:19 +00001548 if (ret)
1549 return ret;
1550
1551 spin_lock(&fs_info->fs_roots_radix_lock);
1552 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1553 (unsigned long)root->root_key.objectid,
1554 root);
1555 if (ret == 0)
Miao Xie27cdeb72014-04-02 19:51:05 +08001556 set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
Miao Xiecb517ea2013-05-15 07:48:19 +00001557 spin_unlock(&fs_info->fs_roots_radix_lock);
1558 radix_tree_preload_end();
1559
1560 return ret;
1561}
1562
Miao Xiec00869f2013-09-25 21:47:44 +08001563struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1564 struct btrfs_key *location,
1565 bool check_ref)
Chris Mason5eda7b52007-06-22 14:16:25 -04001566{
1567 struct btrfs_root *root;
David Sterba381cf652015-01-02 18:45:16 +01001568 struct btrfs_path *path;
David Sterba1d4c08e2015-01-02 19:36:14 +01001569 struct btrfs_key key;
Chris Mason5eda7b52007-06-22 14:16:25 -04001570 int ret;
1571
Chris Masonedbd8d42007-12-21 16:27:24 -05001572 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1573 return fs_info->tree_root;
1574 if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1575 return fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001576 if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1577 return fs_info->chunk_root;
1578 if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1579 return fs_info->dev_root;
Yan Zheng0403e472008-12-10 20:32:51 -05001580 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1581 return fs_info->csum_root;
Arne Jansenbcef60f2011-09-13 15:23:30 +02001582 if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
1583 return fs_info->quota_root ? fs_info->quota_root :
1584 ERR_PTR(-ENOENT);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02001585 if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
1586 return fs_info->uuid_root ? fs_info->uuid_root :
1587 ERR_PTR(-ENOENT);
Omar Sandoval70f6d822015-09-29 20:50:38 -07001588 if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
1589 return fs_info->free_space_root ? fs_info->free_space_root :
1590 ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001591again:
Miao Xiecb517ea2013-05-15 07:48:19 +00001592 root = btrfs_lookup_fs_root(fs_info, location->objectid);
Stefan Behrens48475472013-08-23 10:34:42 +02001593 if (root) {
Miao Xiec00869f2013-09-25 21:47:44 +08001594 if (check_ref && btrfs_root_refs(&root->root_item) == 0)
Stefan Behrens48475472013-08-23 10:34:42 +02001595 return ERR_PTR(-ENOENT);
Chris Mason5eda7b52007-06-22 14:16:25 -04001596 return root;
Stefan Behrens48475472013-08-23 10:34:42 +02001597 }
Chris Mason5eda7b52007-06-22 14:16:25 -04001598
Miao Xiecb517ea2013-05-15 07:48:19 +00001599 root = btrfs_read_fs_root(fs_info->tree_root, location);
Chris Mason5eda7b52007-06-22 14:16:25 -04001600 if (IS_ERR(root))
1601 return root;
Chris Mason3394e162008-11-17 20:42:26 -05001602
Miao Xiec00869f2013-09-25 21:47:44 +08001603 if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04001604 ret = -ENOENT;
1605 goto fail;
1606 }
1607
Miao Xiecb517ea2013-05-15 07:48:19 +00001608 ret = btrfs_init_fs_root(root);
1609 if (ret)
1610 goto fail;
1611
David Sterba381cf652015-01-02 18:45:16 +01001612 path = btrfs_alloc_path();
1613 if (!path) {
1614 ret = -ENOMEM;
1615 goto fail;
1616 }
David Sterba1d4c08e2015-01-02 19:36:14 +01001617 key.objectid = BTRFS_ORPHAN_OBJECTID;
1618 key.type = BTRFS_ORPHAN_ITEM_KEY;
1619 key.offset = location->objectid;
1620
1621 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
David Sterba381cf652015-01-02 18:45:16 +01001622 btrfs_free_path(path);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001623 if (ret < 0)
1624 goto fail;
1625 if (ret == 0)
Miao Xie27cdeb72014-04-02 19:51:05 +08001626 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001627
Miao Xiecb517ea2013-05-15 07:48:19 +00001628 ret = btrfs_insert_fs_root(fs_info, root);
Chris Mason0f7d52f2007-04-09 10:42:37 -04001629 if (ret) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001630 if (ret == -EEXIST) {
David Sterba84db5cc2018-07-20 16:30:25 +02001631 btrfs_free_fs_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001632 goto again;
1633 }
1634 goto fail;
Chris Mason0f7d52f2007-04-09 10:42:37 -04001635 }
Chris Masonedbd8d42007-12-21 16:27:24 -05001636 return root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001637fail:
David Sterba84db5cc2018-07-20 16:30:25 +02001638 btrfs_free_fs_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04001639 return ERR_PTR(ret);
Chris Masonedbd8d42007-12-21 16:27:24 -05001640}
1641
Chris Mason04160082008-03-26 10:28:07 -04001642static int btrfs_congested_fn(void *congested_data, int bdi_bits)
1643{
1644 struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
1645 int ret = 0;
Chris Mason04160082008-03-26 10:28:07 -04001646 struct btrfs_device *device;
1647 struct backing_dev_info *bdi;
Chris Masonb7967db2009-04-27 07:29:04 -04001648
Xiao Guangrong1f781602011-04-20 10:09:16 +00001649 rcu_read_lock();
1650 list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04001651 if (!device->bdev)
1652 continue;
Jan Karaefa7c9f2017-02-02 15:56:53 +01001653 bdi = device->bdev->bd_bdi;
Tejun Heoff9ea322014-09-08 08:03:56 +09001654 if (bdi_congested(bdi, bdi_bits)) {
Chris Mason04160082008-03-26 10:28:07 -04001655 ret = 1;
1656 break;
1657 }
1658 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001659 rcu_read_unlock();
Chris Mason04160082008-03-26 10:28:07 -04001660 return ret;
1661}
1662
Chris Mason8b712842008-06-11 16:50:36 -04001663/*
1664 * called by the kthread helper functions to finally call the bio end_io
1665 * functions. This is where read checksum verification actually happens
1666 */
1667static void end_workqueue_fn(struct btrfs_work *work)
Chris Masonce9adaa2008-04-09 16:28:12 -04001668{
Chris Masonce9adaa2008-04-09 16:28:12 -04001669 struct bio *bio;
David Sterba97eb6b62014-07-30 00:55:42 +02001670 struct btrfs_end_io_wq *end_io_wq;
Chris Masonce9adaa2008-04-09 16:28:12 -04001671
David Sterba97eb6b62014-07-30 00:55:42 +02001672 end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
Chris Mason8b712842008-06-11 16:50:36 -04001673 bio = end_io_wq->bio;
Chris Masonce9adaa2008-04-09 16:28:12 -04001674
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001675 bio->bi_status = end_io_wq->status;
Chris Mason8b712842008-06-11 16:50:36 -04001676 bio->bi_private = end_io_wq->private;
1677 bio->bi_end_io = end_io_wq->end_io;
David Sterba97eb6b62014-07-30 00:55:42 +02001678 kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001679 bio_endio(bio);
Chris Mason44b8bd72008-04-16 11:14:51 -04001680}
1681
Chris Masona74a4b92008-06-25 16:01:31 -04001682static int cleaner_kthread(void *arg)
1683{
1684 struct btrfs_root *root = arg;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001685 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xied0278242013-05-14 10:20:40 +00001686 int again;
Chris Masona74a4b92008-06-25 16:01:31 -04001687
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07001688 while (1) {
Miao Xied0278242013-05-14 10:20:40 +00001689 again = 0;
David Sterba9d1a2a32013-03-12 15:13:28 +00001690
Josef Bacikfd340d02019-01-11 10:21:02 -05001691 set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
1692
Miao Xied0278242013-05-14 10:20:40 +00001693 /* Make the cleaner go to sleep early. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001694 if (btrfs_need_cleaner_sleep(fs_info))
Miao Xied0278242013-05-14 10:20:40 +00001695 goto sleep;
Chris Masona74a4b92008-06-25 16:01:31 -04001696
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001697 /*
1698 * Do not do anything if we might cause open_ctree() to block
1699 * before we have finished mounting the filesystem.
1700 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001701 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001702 goto sleep;
1703
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001704 if (!mutex_trylock(&fs_info->cleaner_mutex))
Miao Xied0278242013-05-14 10:20:40 +00001705 goto sleep;
1706
Miao Xiedc7f3702013-05-14 10:20:42 +00001707 /*
1708 * Avoid the problem that we change the status of the fs
1709 * during the above check and trylock.
1710 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001711 if (btrfs_need_cleaner_sleep(fs_info)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001712 mutex_unlock(&fs_info->cleaner_mutex);
Miao Xiedc7f3702013-05-14 10:20:42 +00001713 goto sleep;
Chris Masona74a4b92008-06-25 16:01:31 -04001714 }
1715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001716 btrfs_run_delayed_iputs(fs_info);
Filipe Mananac2d6cb12016-01-15 11:05:12 +00001717
Miao Xied0278242013-05-14 10:20:40 +00001718 again = btrfs_clean_one_deleted_snapshot(root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 mutex_unlock(&fs_info->cleaner_mutex);
Miao Xied0278242013-05-14 10:20:40 +00001720
1721 /*
Miao Xie05323cd2013-05-14 10:20:41 +00001722 * The defragger has dealt with the R/O remount and umount,
1723 * needn't do anything special here.
Miao Xied0278242013-05-14 10:20:40 +00001724 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001725 btrfs_run_defrag_inodes(fs_info);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01001726
1727 /*
1728 * Acquires fs_info->delete_unused_bgs_mutex to avoid racing
1729 * with relocation (btrfs_relocate_chunk) and relocation
1730 * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
1731 * after acquiring fs_info->delete_unused_bgs_mutex. So we
1732 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
1733 * unused block groups.
1734 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001735 btrfs_delete_unused_bgs(fs_info);
Miao Xied0278242013-05-14 10:20:40 +00001736sleep:
Josef Bacikfd340d02019-01-11 10:21:02 -05001737 clear_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07001738 if (kthread_should_park())
1739 kthread_parkme();
1740 if (kthread_should_stop())
1741 return 0;
Jiri Kosina838fe182016-03-15 11:28:54 +01001742 if (!again) {
Chris Masona74a4b92008-06-25 16:01:31 -04001743 set_current_state(TASK_INTERRUPTIBLE);
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07001744 schedule();
Chris Masona74a4b92008-06-25 16:01:31 -04001745 __set_current_state(TASK_RUNNING);
1746 }
Filipe Mananada288d22015-06-13 06:55:31 +01001747 }
Chris Masona74a4b92008-06-25 16:01:31 -04001748}
1749
1750static int transaction_kthread(void *arg)
1751{
1752 struct btrfs_root *root = arg;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masona74a4b92008-06-25 16:01:31 -04001754 struct btrfs_trans_handle *trans;
1755 struct btrfs_transaction *cur;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001756 u64 transid;
Allen Paisa9444422018-06-12 17:18:25 +05301757 time64_t now;
Chris Masona74a4b92008-06-25 16:01:31 -04001758 unsigned long delay;
Jan Kara914b2002012-03-12 16:05:50 +01001759 bool cannot_commit;
Chris Masona74a4b92008-06-25 16:01:31 -04001760
1761 do {
Jan Kara914b2002012-03-12 16:05:50 +01001762 cannot_commit = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001763 delay = HZ * fs_info->commit_interval;
1764 mutex_lock(&fs_info->transaction_kthread_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001765
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001766 spin_lock(&fs_info->trans_lock);
1767 cur = fs_info->running_transaction;
Chris Masona74a4b92008-06-25 16:01:31 -04001768 if (!cur) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001769 spin_unlock(&fs_info->trans_lock);
Chris Masona74a4b92008-06-25 16:01:31 -04001770 goto sleep;
1771 }
Yan Zheng31153d82008-07-28 15:32:19 -04001772
Arnd Bergmannafd48512018-06-21 18:04:05 +02001773 now = ktime_get_seconds();
Miao Xie4a9d8bd2013-05-17 03:53:43 +00001774 if (cur->state < TRANS_STATE_BLOCKED &&
Qu Wenruoa514d632017-12-22 16:06:39 +08001775 !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) &&
David Sterba8b87dc12013-08-01 18:14:52 +02001776 (now < cur->start_time ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 now - cur->start_time < fs_info->commit_interval)) {
1778 spin_unlock(&fs_info->trans_lock);
Chris Masona74a4b92008-06-25 16:01:31 -04001779 delay = HZ * 5;
1780 goto sleep;
1781 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001782 transid = cur->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001783 spin_unlock(&fs_info->trans_lock);
Chris Mason56bec292009-03-13 10:10:06 -04001784
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001785 /* If the file system is aborted, this will always fail. */
Miao Xie354aa0f2012-09-20 01:54:00 -06001786 trans = btrfs_attach_transaction(root);
Jan Kara914b2002012-03-12 16:05:50 +01001787 if (IS_ERR(trans)) {
Miao Xie354aa0f2012-09-20 01:54:00 -06001788 if (PTR_ERR(trans) != -ENOENT)
1789 cannot_commit = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790 goto sleep;
Jan Kara914b2002012-03-12 16:05:50 +01001791 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001792 if (transid == trans->transid) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001793 btrfs_commit_transaction(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001794 } else {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001795 btrfs_end_transaction(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001796 }
Chris Masona74a4b92008-06-25 16:01:31 -04001797sleep:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001798 wake_up_process(fs_info->cleaner_kthread);
1799 mutex_unlock(&fs_info->transaction_kthread_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001800
Josef Bacik4e121c02013-09-27 16:32:39 -04001801 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001802 &fs_info->fs_state)))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001803 btrfs_cleanup_transaction(fs_info);
Jiri Kosinace63f892016-03-15 11:28:59 +01001804 if (!kthread_should_stop() &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001805 (!btrfs_transaction_blocked(fs_info) ||
Jiri Kosinace63f892016-03-15 11:28:59 +01001806 cannot_commit))
Nikolay Borisovbc5511d0e2018-01-23 14:46:53 +02001807 schedule_timeout_interruptible(delay);
Chris Masona74a4b92008-06-25 16:01:31 -04001808 } while (!kthread_should_stop());
1809 return 0;
1810}
1811
Chris Masonaf31f5e2011-11-03 15:17:42 -04001812/*
1813 * this will find the highest generation in the array of
1814 * root backups. The index of the highest array is returned,
1815 * or -1 if we can't find anything.
1816 *
1817 * We check to make sure the array is valid by comparing the
1818 * generation of the latest root in the array with the generation
1819 * in the super block. If they don't match we pitch it.
1820 */
1821static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
1822{
1823 u64 cur;
1824 int newest_index = -1;
1825 struct btrfs_root_backup *root_backup;
1826 int i;
1827
1828 for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
1829 root_backup = info->super_copy->super_roots + i;
1830 cur = btrfs_backup_tree_root_gen(root_backup);
1831 if (cur == newest_gen)
1832 newest_index = i;
1833 }
1834
1835 /* check to see if we actually wrapped around */
1836 if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
1837 root_backup = info->super_copy->super_roots;
1838 cur = btrfs_backup_tree_root_gen(root_backup);
1839 if (cur == newest_gen)
1840 newest_index = 0;
1841 }
1842 return newest_index;
1843}
1844
1845
1846/*
1847 * find the oldest backup so we know where to store new entries
1848 * in the backup array. This will set the backup_root_index
1849 * field in the fs_info struct
1850 */
1851static void find_oldest_super_backup(struct btrfs_fs_info *info,
1852 u64 newest_gen)
1853{
1854 int newest_index = -1;
1855
1856 newest_index = find_newest_super_backup(info, newest_gen);
1857 /* if there was garbage in there, just move along */
1858 if (newest_index == -1) {
1859 info->backup_root_index = 0;
1860 } else {
1861 info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
1862 }
1863}
1864
1865/*
1866 * copy all the root pointers into the super backup array.
1867 * this will bump the backup pointer by one when it is
1868 * done
1869 */
1870static void backup_super_roots(struct btrfs_fs_info *info)
1871{
1872 int next_backup;
1873 struct btrfs_root_backup *root_backup;
1874 int last_backup;
1875
1876 next_backup = info->backup_root_index;
1877 last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) %
1878 BTRFS_NUM_BACKUP_ROOTS;
1879
1880 /*
1881 * just overwrite the last backup if we're at the same generation
1882 * this happens only at umount
1883 */
1884 root_backup = info->super_for_commit->super_roots + last_backup;
1885 if (btrfs_backup_tree_root_gen(root_backup) ==
1886 btrfs_header_generation(info->tree_root->node))
1887 next_backup = last_backup;
1888
1889 root_backup = info->super_for_commit->super_roots + next_backup;
1890
1891 /*
1892 * make sure all of our padding and empty slots get zero filled
1893 * regardless of which ones we use today
1894 */
1895 memset(root_backup, 0, sizeof(*root_backup));
1896
1897 info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
1898
1899 btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
1900 btrfs_set_backup_tree_root_gen(root_backup,
1901 btrfs_header_generation(info->tree_root->node));
1902
1903 btrfs_set_backup_tree_root_level(root_backup,
1904 btrfs_header_level(info->tree_root->node));
1905
1906 btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
1907 btrfs_set_backup_chunk_root_gen(root_backup,
1908 btrfs_header_generation(info->chunk_root->node));
1909 btrfs_set_backup_chunk_root_level(root_backup,
1910 btrfs_header_level(info->chunk_root->node));
1911
1912 btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start);
1913 btrfs_set_backup_extent_root_gen(root_backup,
1914 btrfs_header_generation(info->extent_root->node));
1915 btrfs_set_backup_extent_root_level(root_backup,
1916 btrfs_header_level(info->extent_root->node));
1917
Chris Mason7c7e82a2011-11-06 18:50:56 -05001918 /*
1919 * we might commit during log recovery, which happens before we set
1920 * the fs_root. Make sure it is valid before we fill it in.
1921 */
1922 if (info->fs_root && info->fs_root->node) {
1923 btrfs_set_backup_fs_root(root_backup,
1924 info->fs_root->node->start);
1925 btrfs_set_backup_fs_root_gen(root_backup,
Chris Masonaf31f5e2011-11-03 15:17:42 -04001926 btrfs_header_generation(info->fs_root->node));
Chris Mason7c7e82a2011-11-06 18:50:56 -05001927 btrfs_set_backup_fs_root_level(root_backup,
Chris Masonaf31f5e2011-11-03 15:17:42 -04001928 btrfs_header_level(info->fs_root->node));
Chris Mason7c7e82a2011-11-06 18:50:56 -05001929 }
Chris Masonaf31f5e2011-11-03 15:17:42 -04001930
1931 btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
1932 btrfs_set_backup_dev_root_gen(root_backup,
1933 btrfs_header_generation(info->dev_root->node));
1934 btrfs_set_backup_dev_root_level(root_backup,
1935 btrfs_header_level(info->dev_root->node));
1936
1937 btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
1938 btrfs_set_backup_csum_root_gen(root_backup,
1939 btrfs_header_generation(info->csum_root->node));
1940 btrfs_set_backup_csum_root_level(root_backup,
1941 btrfs_header_level(info->csum_root->node));
1942
1943 btrfs_set_backup_total_bytes(root_backup,
1944 btrfs_super_total_bytes(info->super_copy));
1945 btrfs_set_backup_bytes_used(root_backup,
1946 btrfs_super_bytes_used(info->super_copy));
1947 btrfs_set_backup_num_devices(root_backup,
1948 btrfs_super_num_devices(info->super_copy));
1949
1950 /*
1951 * if we don't copy this out to the super_copy, it won't get remembered
1952 * for the next commit
1953 */
1954 memcpy(&info->super_copy->super_roots,
1955 &info->super_for_commit->super_roots,
1956 sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
1957}
1958
1959/*
1960 * this copies info out of the root backup array and back into
1961 * the in-memory super block. It is meant to help iterate through
1962 * the array, so you send it the number of backups you've already
1963 * tried and the last backup index you used.
1964 *
1965 * this returns -1 when it has tried all the backups
1966 */
1967static noinline int next_root_backup(struct btrfs_fs_info *info,
1968 struct btrfs_super_block *super,
1969 int *num_backups_tried, int *backup_index)
1970{
1971 struct btrfs_root_backup *root_backup;
1972 int newest = *backup_index;
1973
1974 if (*num_backups_tried == 0) {
1975 u64 gen = btrfs_super_generation(super);
1976
1977 newest = find_newest_super_backup(info, gen);
1978 if (newest == -1)
1979 return -1;
1980
1981 *backup_index = newest;
1982 *num_backups_tried = 1;
1983 } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) {
1984 /* we've tried all the backups, all done */
1985 return -1;
1986 } else {
1987 /* jump to the next oldest backup */
1988 newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) %
1989 BTRFS_NUM_BACKUP_ROOTS;
1990 *backup_index = newest;
1991 *num_backups_tried += 1;
1992 }
1993 root_backup = super->super_roots + newest;
1994
1995 btrfs_set_super_generation(super,
1996 btrfs_backup_tree_root_gen(root_backup));
1997 btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
1998 btrfs_set_super_root_level(super,
1999 btrfs_backup_tree_root_level(root_backup));
2000 btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
2001
2002 /*
2003 * fixme: the total bytes and num_devices need to match or we should
2004 * need a fsck
2005 */
2006 btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
2007 btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
2008 return 0;
2009}
2010
Liu Bo7abadb62013-03-17 02:10:31 +00002011/* helper to cleanup workers */
2012static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
2013{
Qu Wenruodc6e3202014-02-28 10:46:14 +08002014 btrfs_destroy_workqueue(fs_info->fixup_workers);
Qu Wenruoafe3d242014-02-28 10:46:07 +08002015 btrfs_destroy_workqueue(fs_info->delalloc_workers);
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +08002016 btrfs_destroy_workqueue(fs_info->workers);
Qu Wenruofccb5d82014-02-28 10:46:10 +08002017 btrfs_destroy_workqueue(fs_info->endio_workers);
Qu Wenruofccb5d82014-02-28 10:46:10 +08002018 btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
Miao Xie8b110e32014-09-12 18:44:03 +08002019 btrfs_destroy_workqueue(fs_info->endio_repair_workers);
Qu Wenruod05a33a2014-02-28 10:46:11 +08002020 btrfs_destroy_workqueue(fs_info->rmw_workers);
Qu Wenruofccb5d82014-02-28 10:46:10 +08002021 btrfs_destroy_workqueue(fs_info->endio_write_workers);
2022 btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08002023 btrfs_destroy_workqueue(fs_info->submit_workers);
Qu Wenruo5b3bc442014-02-28 10:46:15 +08002024 btrfs_destroy_workqueue(fs_info->delayed_workers);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +08002025 btrfs_destroy_workqueue(fs_info->caching_workers);
Qu Wenruo736cfa12014-02-28 10:46:13 +08002026 btrfs_destroy_workqueue(fs_info->readahead_workers);
Qu Wenruoa44903a2014-02-28 10:46:09 +08002027 btrfs_destroy_workqueue(fs_info->flush_workers);
Qu Wenruofc97fab2014-02-28 10:46:16 +08002028 btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
Chris Masona79b7d42014-05-22 16:18:52 -07002029 btrfs_destroy_workqueue(fs_info->extent_workers);
Filipe Mananaa9b94772017-02-04 17:12:00 +00002030 /*
2031 * Now that all other work queues are destroyed, we can safely destroy
2032 * the queues used for metadata I/O, since tasks from those other work
2033 * queues can do metadata I/O operations.
2034 */
2035 btrfs_destroy_workqueue(fs_info->endio_meta_workers);
2036 btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
Liu Bo7abadb62013-03-17 02:10:31 +00002037}
2038
Rashika2e9f5952013-10-31 02:45:20 +05302039static void free_root_extent_buffers(struct btrfs_root *root)
2040{
2041 if (root) {
2042 free_extent_buffer(root->node);
2043 free_extent_buffer(root->commit_root);
2044 root->node = NULL;
2045 root->commit_root = NULL;
2046 }
2047}
2048
Chris Masonaf31f5e2011-11-03 15:17:42 -04002049/* helper to cleanup tree roots */
2050static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
2051{
Rashika2e9f5952013-10-31 02:45:20 +05302052 free_root_extent_buffers(info->tree_root);
Josef Bacik655b09f2013-05-17 14:06:51 -04002053
Rashika2e9f5952013-10-31 02:45:20 +05302054 free_root_extent_buffers(info->dev_root);
2055 free_root_extent_buffers(info->extent_root);
2056 free_root_extent_buffers(info->csum_root);
2057 free_root_extent_buffers(info->quota_root);
2058 free_root_extent_buffers(info->uuid_root);
2059 if (chunk_root)
2060 free_root_extent_buffers(info->chunk_root);
Omar Sandoval70f6d822015-09-29 20:50:38 -07002061 free_root_extent_buffers(info->free_space_root);
Chris Masonaf31f5e2011-11-03 15:17:42 -04002062}
2063
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04002064void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
Josef Bacik171f6532013-04-24 16:35:41 -04002065{
2066 int ret;
2067 struct btrfs_root *gang[8];
2068 int i;
2069
2070 while (!list_empty(&fs_info->dead_roots)) {
2071 gang[0] = list_entry(fs_info->dead_roots.next,
2072 struct btrfs_root, root_list);
2073 list_del(&gang[0]->root_list);
2074
Miao Xie27cdeb72014-04-02 19:51:05 +08002075 if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
Miao Xiecb517ea2013-05-15 07:48:19 +00002076 btrfs_drop_and_free_fs_root(fs_info, gang[0]);
Josef Bacik171f6532013-04-24 16:35:41 -04002077 } else {
2078 free_extent_buffer(gang[0]->node);
2079 free_extent_buffer(gang[0]->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00002080 btrfs_put_fs_root(gang[0]);
Josef Bacik171f6532013-04-24 16:35:41 -04002081 }
2082 }
2083
2084 while (1) {
2085 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2086 (void **)gang, 0,
2087 ARRAY_SIZE(gang));
2088 if (!ret)
2089 break;
2090 for (i = 0; i < ret; i++)
Miao Xiecb517ea2013-05-15 07:48:19 +00002091 btrfs_drop_and_free_fs_root(fs_info, gang[i]);
Josef Bacik171f6532013-04-24 16:35:41 -04002092 }
Liu Bo1a4319c2014-01-13 19:53:53 +08002093
2094 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
2095 btrfs_free_log_root_tree(NULL, fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002096 btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
Liu Bo1a4319c2014-01-13 19:53:53 +08002097 }
Josef Bacik171f6532013-04-24 16:35:41 -04002098}
Chris Masonaf31f5e2011-11-03 15:17:42 -04002099
Eric Sandeen638aa7e2014-08-01 18:12:38 -05002100static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
2101{
2102 mutex_init(&fs_info->scrub_lock);
2103 atomic_set(&fs_info->scrubs_running, 0);
2104 atomic_set(&fs_info->scrub_pause_req, 0);
2105 atomic_set(&fs_info->scrubs_paused, 0);
2106 atomic_set(&fs_info->scrub_cancel_req, 0);
2107 init_waitqueue_head(&fs_info->scrub_pause_wait);
Anand Jainff09c4c2019-01-30 14:45:02 +08002108 refcount_set(&fs_info->scrub_workers_refcnt, 0);
Eric Sandeen638aa7e2014-08-01 18:12:38 -05002109}
2110
Eric Sandeen779a65a2014-08-01 18:12:39 -05002111static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
2112{
2113 spin_lock_init(&fs_info->balance_lock);
2114 mutex_init(&fs_info->balance_mutex);
Eric Sandeen779a65a2014-08-01 18:12:39 -05002115 atomic_set(&fs_info->balance_pause_req, 0);
2116 atomic_set(&fs_info->balance_cancel_req, 0);
2117 fs_info->balance_ctl = NULL;
2118 init_waitqueue_head(&fs_info->balance_wait_q);
2119}
2120
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002121static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
Eric Sandeenf37938e2014-08-01 18:12:40 -05002122{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002123 struct inode *inode = fs_info->btree_inode;
2124
2125 inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
2126 set_nlink(inode, 1);
Eric Sandeenf37938e2014-08-01 18:12:40 -05002127 /*
2128 * we set the i_size on the btree inode to the max possible int.
2129 * the real end of the address space is determined by all of
2130 * the devices in the system
2131 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002132 inode->i_size = OFFSET_MAX;
2133 inode->i_mapping->a_ops = &btree_aops;
Eric Sandeenf37938e2014-08-01 18:12:40 -05002134
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002135 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08002136 extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
2137 IO_TREE_INODE_IO, inode);
David Sterba7b439732019-03-11 15:58:30 +01002138 BTRFS_I(inode)->io_tree.track_uptodate = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002139 extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
Eric Sandeenf37938e2014-08-01 18:12:40 -05002140
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002141 BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops;
Eric Sandeenf37938e2014-08-01 18:12:40 -05002142
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002143 BTRFS_I(inode)->root = fs_info->tree_root;
2144 memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key));
2145 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
2146 btrfs_insert_inode_hash(inode);
Eric Sandeenf37938e2014-08-01 18:12:40 -05002147}
2148
Eric Sandeenad618362014-08-01 18:12:41 -05002149static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
2150{
Eric Sandeenad618362014-08-01 18:12:41 -05002151 mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
David Sterba129827e2018-04-05 01:29:24 +02002152 init_rwsem(&fs_info->dev_replace.rwsem);
David Sterba7f8d2362018-04-05 01:04:49 +02002153 init_waitqueue_head(&fs_info->dev_replace.replace_wait);
Eric Sandeenad618362014-08-01 18:12:41 -05002154}
2155
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002156static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
2157{
2158 spin_lock_init(&fs_info->qgroup_lock);
2159 mutex_init(&fs_info->qgroup_ioctl_lock);
2160 fs_info->qgroup_tree = RB_ROOT;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002161 INIT_LIST_HEAD(&fs_info->dirty_qgroups);
2162 fs_info->qgroup_seq = 1;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002163 fs_info->qgroup_ulist = NULL;
Jeff Mahoneyd2c609b2016-08-15 12:10:33 -04002164 fs_info->qgroup_rescan_running = false;
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002165 mutex_init(&fs_info->qgroup_rescan_lock);
2166}
2167
Eric Sandeen2a458192015-02-16 16:29:26 +01002168static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
2169 struct btrfs_fs_devices *fs_devices)
2170{
Anand Jainf7b885b2018-02-13 17:50:42 +08002171 u32 max_active = fs_info->thread_pool_size;
David Sterba6f011052015-02-16 18:34:01 +01002172 unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
Eric Sandeen2a458192015-02-16 16:29:26 +01002173
2174 fs_info->workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002175 btrfs_alloc_workqueue(fs_info, "worker",
2176 flags | WQ_HIGHPRI, max_active, 16);
Eric Sandeen2a458192015-02-16 16:29:26 +01002177
2178 fs_info->delalloc_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002179 btrfs_alloc_workqueue(fs_info, "delalloc",
2180 flags, max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002181
2182 fs_info->flush_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002183 btrfs_alloc_workqueue(fs_info, "flush_delalloc",
2184 flags, max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002185
2186 fs_info->caching_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002187 btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002188
2189 /*
2190 * a higher idle thresh on the submit workers makes it much more
2191 * likely that bios will be send down in a sane order to the
2192 * devices
2193 */
2194 fs_info->submit_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002195 btrfs_alloc_workqueue(fs_info, "submit", flags,
Eric Sandeen2a458192015-02-16 16:29:26 +01002196 min_t(u64, fs_devices->num_devices,
2197 max_active), 64);
2198
2199 fs_info->fixup_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002200 btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002201
2202 /*
2203 * endios are largely parallel and should have a very
2204 * low idle thresh
2205 */
2206 fs_info->endio_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002207 btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4);
Eric Sandeen2a458192015-02-16 16:29:26 +01002208 fs_info->endio_meta_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002209 btrfs_alloc_workqueue(fs_info, "endio-meta", flags,
2210 max_active, 4);
Eric Sandeen2a458192015-02-16 16:29:26 +01002211 fs_info->endio_meta_write_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002212 btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags,
2213 max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002214 fs_info->endio_raid56_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002215 btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
2216 max_active, 4);
Eric Sandeen2a458192015-02-16 16:29:26 +01002217 fs_info->endio_repair_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002218 btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002219 fs_info->rmw_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002220 btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002221 fs_info->endio_write_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002222 btrfs_alloc_workqueue(fs_info, "endio-write", flags,
2223 max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002224 fs_info->endio_freespace_worker =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002225 btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
2226 max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002227 fs_info->delayed_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002228 btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
2229 max_active, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002230 fs_info->readahead_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002231 btrfs_alloc_workqueue(fs_info, "readahead", flags,
2232 max_active, 2);
Eric Sandeen2a458192015-02-16 16:29:26 +01002233 fs_info->qgroup_rescan_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002234 btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0);
Eric Sandeen2a458192015-02-16 16:29:26 +01002235 fs_info->extent_workers =
Jeff Mahoneycb001092016-06-09 16:22:11 -04002236 btrfs_alloc_workqueue(fs_info, "extent-refs", flags,
Eric Sandeen2a458192015-02-16 16:29:26 +01002237 min_t(u64, fs_devices->num_devices,
2238 max_active), 8);
2239
2240 if (!(fs_info->workers && fs_info->delalloc_workers &&
2241 fs_info->submit_workers && fs_info->flush_workers &&
2242 fs_info->endio_workers && fs_info->endio_meta_workers &&
2243 fs_info->endio_meta_write_workers &&
2244 fs_info->endio_repair_workers &&
2245 fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
2246 fs_info->endio_freespace_worker && fs_info->rmw_workers &&
2247 fs_info->caching_workers && fs_info->readahead_workers &&
2248 fs_info->fixup_workers && fs_info->delayed_workers &&
2249 fs_info->extent_workers &&
2250 fs_info->qgroup_rescan_workers)) {
2251 return -ENOMEM;
2252 }
2253
2254 return 0;
2255}
2256
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002257static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
2258{
2259 struct crypto_shash *csum_shash;
2260 const char *csum_name = btrfs_super_csum_name(csum_type);
2261
2262 csum_shash = crypto_alloc_shash(csum_name, 0, 0);
2263
2264 if (IS_ERR(csum_shash)) {
2265 btrfs_err(fs_info, "error allocating %s hash for checksum",
2266 csum_name);
2267 return PTR_ERR(csum_shash);
2268 }
2269
2270 fs_info->csum_shash = csum_shash;
2271
2272 return 0;
2273}
2274
2275static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
2276{
2277 crypto_free_shash(fs_info->csum_shash);
2278}
2279
Eric Sandeen63443bf2014-08-01 18:12:46 -05002280static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2281 struct btrfs_fs_devices *fs_devices)
2282{
2283 int ret;
Eric Sandeen63443bf2014-08-01 18:12:46 -05002284 struct btrfs_root *log_tree_root;
2285 struct btrfs_super_block *disk_super = fs_info->super_copy;
2286 u64 bytenr = btrfs_super_log_root(disk_super);
Qu Wenruo581c1762018-03-29 09:08:11 +08002287 int level = btrfs_super_log_root_level(disk_super);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002288
2289 if (fs_devices->rw_devices == 0) {
David Sterbaf14d1042015-10-08 11:37:06 +02002290 btrfs_warn(fs_info, "log replay required on RO media");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002291 return -EIO;
2292 }
2293
David Sterba74e4d822016-02-11 11:01:55 +01002294 log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002295 if (!log_tree_root)
2296 return -ENOMEM;
2297
Jeff Mahoneyda170662016-06-15 09:22:56 -04002298 __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002299
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002300 log_tree_root->node = read_tree_block(fs_info, bytenr,
Qu Wenruo581c1762018-03-29 09:08:11 +08002301 fs_info->generation + 1,
2302 level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08002303 if (IS_ERR(log_tree_root->node)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002304 btrfs_warn(fs_info, "failed to read log tree");
Liu Bo0eeff232015-06-11 14:16:44 +08002305 ret = PTR_ERR(log_tree_root->node);
Liu Bo64c043d2015-05-25 17:30:15 +08002306 kfree(log_tree_root);
Liu Bo0eeff232015-06-11 14:16:44 +08002307 return ret;
Liu Bo64c043d2015-05-25 17:30:15 +08002308 } else if (!extent_buffer_uptodate(log_tree_root->node)) {
David Sterbaf14d1042015-10-08 11:37:06 +02002309 btrfs_err(fs_info, "failed to read log tree");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002310 free_extent_buffer(log_tree_root->node);
2311 kfree(log_tree_root);
2312 return -EIO;
2313 }
2314 /* returns with log_tree_root freed on success */
2315 ret = btrfs_recover_log_trees(log_tree_root);
2316 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002317 btrfs_handle_fs_error(fs_info, ret,
2318 "Failed to recover log tree");
Eric Sandeen63443bf2014-08-01 18:12:46 -05002319 free_extent_buffer(log_tree_root->node);
2320 kfree(log_tree_root);
2321 return ret;
2322 }
2323
David Howellsbc98a422017-07-17 08:45:34 +01002324 if (sb_rdonly(fs_info->sb)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002325 ret = btrfs_commit_super(fs_info);
Eric Sandeen63443bf2014-08-01 18:12:46 -05002326 if (ret)
2327 return ret;
2328 }
2329
2330 return 0;
2331}
2332
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002333static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002334{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002335 struct btrfs_root *tree_root = fs_info->tree_root;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002336 struct btrfs_root *root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002337 struct btrfs_key location;
2338 int ret;
2339
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002340 BUG_ON(!fs_info->tree_root);
2341
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002342 location.objectid = BTRFS_EXTENT_TREE_OBJECTID;
2343 location.type = BTRFS_ROOT_ITEM_KEY;
2344 location.offset = 0;
2345
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002346 root = btrfs_read_tree_root(tree_root, &location);
Liu Bof50f4352018-03-29 06:11:45 +08002347 if (IS_ERR(root)) {
2348 ret = PTR_ERR(root);
2349 goto out;
2350 }
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002351 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2352 fs_info->extent_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002353
2354 location.objectid = BTRFS_DEV_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002355 root = btrfs_read_tree_root(tree_root, &location);
Liu Bof50f4352018-03-29 06:11:45 +08002356 if (IS_ERR(root)) {
2357 ret = PTR_ERR(root);
2358 goto out;
2359 }
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002360 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2361 fs_info->dev_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002362 btrfs_init_devices_late(fs_info);
2363
2364 location.objectid = BTRFS_CSUM_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002365 root = btrfs_read_tree_root(tree_root, &location);
Liu Bof50f4352018-03-29 06:11:45 +08002366 if (IS_ERR(root)) {
2367 ret = PTR_ERR(root);
2368 goto out;
2369 }
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002370 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2371 fs_info->csum_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002372
2373 location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002374 root = btrfs_read_tree_root(tree_root, &location);
2375 if (!IS_ERR(root)) {
2376 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
Josef Bacikafcdd122016-09-02 15:40:02 -04002377 set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002378 fs_info->quota_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002379 }
2380
2381 location.objectid = BTRFS_UUID_TREE_OBJECTID;
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002382 root = btrfs_read_tree_root(tree_root, &location);
2383 if (IS_ERR(root)) {
2384 ret = PTR_ERR(root);
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002385 if (ret != -ENOENT)
Liu Bof50f4352018-03-29 06:11:45 +08002386 goto out;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002387 } else {
David Sterbaa4f3d2c2015-02-16 18:44:34 +01002388 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2389 fs_info->uuid_root = root;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002390 }
2391
Omar Sandoval70f6d822015-09-29 20:50:38 -07002392 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2393 location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID;
2394 root = btrfs_read_tree_root(tree_root, &location);
Liu Bof50f4352018-03-29 06:11:45 +08002395 if (IS_ERR(root)) {
2396 ret = PTR_ERR(root);
2397 goto out;
2398 }
Omar Sandoval70f6d822015-09-29 20:50:38 -07002399 set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
2400 fs_info->free_space_root = root;
2401 }
2402
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002403 return 0;
Liu Bof50f4352018-03-29 06:11:45 +08002404out:
2405 btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
2406 location.objectid, ret);
2407 return ret;
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05002408}
2409
Qu Wenruo069ec952018-05-11 13:35:26 +08002410/*
2411 * Real super block validation
2412 * NOTE: super csum type and incompat features will not be checked here.
2413 *
2414 * @sb: super block to check
2415 * @mirror_num: the super block number to check its bytenr:
2416 * 0 the primary (1st) sb
2417 * 1, 2 2nd and 3rd backup copy
2418 * -1 skip bytenr check
2419 */
2420static int validate_super(struct btrfs_fs_info *fs_info,
2421 struct btrfs_super_block *sb, int mirror_num)
Qu Wenruo21a852b2018-05-11 13:35:25 +08002422{
Qu Wenruo21a852b2018-05-11 13:35:25 +08002423 u64 nodesize = btrfs_super_nodesize(sb);
2424 u64 sectorsize = btrfs_super_sectorsize(sb);
2425 int ret = 0;
2426
2427 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
2428 btrfs_err(fs_info, "no valid FS found");
2429 ret = -EINVAL;
2430 }
2431 if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) {
2432 btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu",
2433 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
2434 ret = -EINVAL;
2435 }
2436 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
2437 btrfs_err(fs_info, "tree_root level too big: %d >= %d",
2438 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
2439 ret = -EINVAL;
2440 }
2441 if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
2442 btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
2443 btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
2444 ret = -EINVAL;
2445 }
2446 if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
2447 btrfs_err(fs_info, "log_root level too big: %d >= %d",
2448 btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
2449 ret = -EINVAL;
2450 }
2451
2452 /*
2453 * Check sectorsize and nodesize first, other check will need it.
2454 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
2455 */
2456 if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
2457 sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
2458 btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
2459 ret = -EINVAL;
2460 }
2461 /* Only PAGE SIZE is supported yet */
2462 if (sectorsize != PAGE_SIZE) {
2463 btrfs_err(fs_info,
2464 "sectorsize %llu not supported yet, only support %lu",
2465 sectorsize, PAGE_SIZE);
2466 ret = -EINVAL;
2467 }
2468 if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
2469 nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
2470 btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
2471 ret = -EINVAL;
2472 }
2473 if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
2474 btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
2475 le32_to_cpu(sb->__unused_leafsize), nodesize);
2476 ret = -EINVAL;
2477 }
2478
2479 /* Root alignment check */
2480 if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
2481 btrfs_warn(fs_info, "tree_root block unaligned: %llu",
2482 btrfs_super_root(sb));
2483 ret = -EINVAL;
2484 }
2485 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
2486 btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
2487 btrfs_super_chunk_root(sb));
2488 ret = -EINVAL;
2489 }
2490 if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
2491 btrfs_warn(fs_info, "log_root block unaligned: %llu",
2492 btrfs_super_log_root(sb));
2493 ret = -EINVAL;
2494 }
2495
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002496 if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002497 BTRFS_FSID_SIZE) != 0) {
Qu Wenruo21a852b2018-05-11 13:35:25 +08002498 btrfs_err(fs_info,
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002499 "dev_item UUID does not match metadata fsid: %pU != %pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002500 fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
Qu Wenruo21a852b2018-05-11 13:35:25 +08002501 ret = -EINVAL;
2502 }
2503
2504 /*
2505 * Hint to catch really bogus numbers, bitflips or so, more exact checks are
2506 * done later
2507 */
2508 if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
2509 btrfs_err(fs_info, "bytes_used is too small %llu",
2510 btrfs_super_bytes_used(sb));
2511 ret = -EINVAL;
2512 }
2513 if (!is_power_of_2(btrfs_super_stripesize(sb))) {
2514 btrfs_err(fs_info, "invalid stripesize %u",
2515 btrfs_super_stripesize(sb));
2516 ret = -EINVAL;
2517 }
2518 if (btrfs_super_num_devices(sb) > (1UL << 31))
2519 btrfs_warn(fs_info, "suspicious number of devices: %llu",
2520 btrfs_super_num_devices(sb));
2521 if (btrfs_super_num_devices(sb) == 0) {
2522 btrfs_err(fs_info, "number of devices is 0");
2523 ret = -EINVAL;
2524 }
2525
Qu Wenruo069ec952018-05-11 13:35:26 +08002526 if (mirror_num >= 0 &&
2527 btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
Qu Wenruo21a852b2018-05-11 13:35:25 +08002528 btrfs_err(fs_info, "super offset mismatch %llu != %u",
2529 btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
2530 ret = -EINVAL;
2531 }
2532
2533 /*
2534 * Obvious sys_chunk_array corruptions, it must hold at least one key
2535 * and one chunk
2536 */
2537 if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
2538 btrfs_err(fs_info, "system chunk array too big %u > %u",
2539 btrfs_super_sys_array_size(sb),
2540 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
2541 ret = -EINVAL;
2542 }
2543 if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
2544 + sizeof(struct btrfs_chunk)) {
2545 btrfs_err(fs_info, "system chunk array too small %u < %zu",
2546 btrfs_super_sys_array_size(sb),
2547 sizeof(struct btrfs_disk_key)
2548 + sizeof(struct btrfs_chunk));
2549 ret = -EINVAL;
2550 }
2551
2552 /*
2553 * The generation is a global counter, we'll trust it more than the others
2554 * but it's still possible that it's the one that's wrong.
2555 */
2556 if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
2557 btrfs_warn(fs_info,
2558 "suspicious: generation < chunk_root_generation: %llu < %llu",
2559 btrfs_super_generation(sb),
2560 btrfs_super_chunk_root_generation(sb));
2561 if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
2562 && btrfs_super_cache_generation(sb) != (u64)-1)
2563 btrfs_warn(fs_info,
2564 "suspicious: generation < cache_generation: %llu < %llu",
2565 btrfs_super_generation(sb),
2566 btrfs_super_cache_generation(sb));
2567
2568 return ret;
2569}
2570
Qu Wenruo069ec952018-05-11 13:35:26 +08002571/*
2572 * Validation of super block at mount time.
2573 * Some checks already done early at mount time, like csum type and incompat
2574 * flags will be skipped.
2575 */
2576static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
2577{
2578 return validate_super(fs_info, fs_info->super_copy, 0);
2579}
2580
Qu Wenruo75cb8572018-05-11 13:35:27 +08002581/*
2582 * Validation of super block at write time.
2583 * Some checks like bytenr check will be skipped as their values will be
2584 * overwritten soon.
2585 * Extra checks like csum type and incompat flags will be done here.
2586 */
2587static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
2588 struct btrfs_super_block *sb)
2589{
2590 int ret;
2591
2592 ret = validate_super(fs_info, sb, -1);
2593 if (ret < 0)
2594 goto out;
Johannes Thumshirne7e16f42019-06-03 16:58:53 +02002595 if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
Qu Wenruo75cb8572018-05-11 13:35:27 +08002596 ret = -EUCLEAN;
2597 btrfs_err(fs_info, "invalid csum type, has %u want %u",
2598 btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
2599 goto out;
2600 }
2601 if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
2602 ret = -EUCLEAN;
2603 btrfs_err(fs_info,
2604 "invalid incompat flags, has 0x%llx valid mask 0x%llx",
2605 btrfs_super_incompat_flags(sb),
2606 (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
2607 goto out;
2608 }
2609out:
2610 if (ret < 0)
2611 btrfs_err(fs_info,
2612 "super block corruption detected before writing it to disk");
2613 return ret;
2614}
2615
Al Viroad2b2c82011-11-17 01:10:02 -05002616int open_ctree(struct super_block *sb,
2617 struct btrfs_fs_devices *fs_devices,
2618 char *options)
Chris Masoneb60cea2007-02-02 09:18:22 -05002619{
Chris Masondb945352007-10-15 16:15:53 -04002620 u32 sectorsize;
2621 u32 nodesize;
Chris Mason87ee04e2007-11-30 11:30:34 -05002622 u32 stripesize;
Yan Zheng84234f32008-10-29 14:49:05 -04002623 u64 generation;
Josef Bacikf2b636e2008-12-02 06:36:08 -05002624 u64 features;
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +02002625 u16 csum_type;
Chris Mason3de45862008-11-17 21:02:50 -05002626 struct btrfs_key location;
Chris Masona061fc82008-05-07 11:43:44 -04002627 struct buffer_head *bh;
Ilya Dryomov4d34b272011-11-09 00:08:15 +02002628 struct btrfs_super_block *disk_super;
Al Viro815745c2011-11-17 15:40:49 -05002629 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Al Virof84a8bd2011-11-17 15:57:57 -05002630 struct btrfs_root *tree_root;
Ilya Dryomov4d34b272011-11-09 00:08:15 +02002631 struct btrfs_root *chunk_root;
Chris Masoneb60cea2007-02-02 09:18:22 -05002632 int ret;
Yane58ca022008-04-01 11:21:34 -04002633 int err = -EINVAL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04002634 int num_backups_tried = 0;
2635 int backup_index = 0;
Omar Sandoval6675df32016-09-22 17:24:22 -07002636 int clear_free_space_tree = 0;
Qu Wenruo581c1762018-03-29 09:08:11 +08002637 int level;
Chris Mason4543df72008-06-11 21:47:56 -04002638
David Sterba74e4d822016-02-11 11:01:55 +01002639 tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
2640 chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
Miao Xiecb517ea2013-05-15 07:48:19 +00002641 if (!tree_root || !chunk_root) {
Chris Mason39279cc2007-06-12 06:35:45 -04002642 err = -ENOMEM;
2643 goto fail;
2644 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002645
2646 ret = init_srcu_struct(&fs_info->subvol_srcu);
2647 if (ret) {
2648 err = ret;
2649 goto fail;
2650 }
2651
Josef Bacik4297ff82019-04-10 15:56:09 -04002652 ret = percpu_counter_init(&fs_info->dio_bytes, 0, GFP_KERNEL);
Miao Xiee2d84522013-01-29 10:09:20 +00002653 if (ret) {
2654 err = ret;
Jan Kara9e11cee2017-04-12 12:24:32 +02002655 goto fail_srcu;
Miao Xiee2d84522013-01-29 10:09:20 +00002656 }
Josef Bacik4297ff82019-04-10 15:56:09 -04002657
2658 ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
2659 if (ret) {
2660 err = ret;
2661 goto fail_dio_bytes;
2662 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002663 fs_info->dirty_metadata_batch = PAGE_SIZE *
Miao Xiee2d84522013-01-29 10:09:20 +00002664 (1 + ilog2(nr_cpu_ids));
2665
Tejun Heo908c7f12014-09-08 09:51:29 +09002666 ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
Miao Xie963d6782013-01-29 10:10:51 +00002667 if (ret) {
2668 err = ret;
2669 goto fail_dirty_metadata_bytes;
2670 }
2671
David Sterba7f8d2362018-04-05 01:04:49 +02002672 ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
2673 GFP_KERNEL);
Miao Xiec404e0d2014-01-30 16:46:55 +08002674 if (ret) {
2675 err = ret;
2676 goto fail_delalloc_bytes;
2677 }
2678
Yan, Zheng76dda932009-09-21 16:00:26 -04002679 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
Josef Bacikf28491e2013-12-16 13:24:27 -05002680 INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
Chris Mason8fd17792007-04-19 21:01:03 -04002681 INIT_LIST_HEAD(&fs_info->trans_list);
Chris Masonfacda1e2007-06-08 18:11:48 -04002682 INIT_LIST_HEAD(&fs_info->dead_roots);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002683 INIT_LIST_HEAD(&fs_info->delayed_iputs);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002684 INIT_LIST_HEAD(&fs_info->delalloc_roots);
Yan Zheng11833d62009-09-11 16:11:19 -04002685 INIT_LIST_HEAD(&fs_info->caching_block_groups);
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002686 INIT_LIST_HEAD(&fs_info->pending_raid_kobjs);
2687 spin_lock_init(&fs_info->pending_raid_kobjs_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002688 spin_lock_init(&fs_info->delalloc_root_lock);
Josef Bacika4abeea2011-04-11 17:25:13 -04002689 spin_lock_init(&fs_info->trans_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04002690 spin_lock_init(&fs_info->fs_roots_radix_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002691 spin_lock_init(&fs_info->delayed_iput_lock);
Chris Mason4cb53002011-05-24 15:35:30 -04002692 spin_lock_init(&fs_info->defrag_inodes_lock);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002693 spin_lock_init(&fs_info->tree_mod_seq_lock);
Miao Xieceda0862013-04-11 10:30:16 +00002694 spin_lock_init(&fs_info->super_lock);
Josef Bacikf28491e2013-12-16 13:24:27 -05002695 spin_lock_init(&fs_info->buffer_lock);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002696 spin_lock_init(&fs_info->unused_bgs_lock);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002697 rwlock_init(&fs_info->tree_mod_log_lock);
Zhao Leid7c15172015-02-26 10:49:20 +08002698 mutex_init(&fs_info->unused_bg_unpin_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002699 mutex_init(&fs_info->delete_unused_bgs_mutex);
Chris Mason75857172011-06-13 20:00:16 -04002700 mutex_init(&fs_info->reloc_mutex);
Miao Xie573bfb72014-03-06 13:55:03 +08002701 mutex_init(&fs_info->delalloc_root_mutex);
Miao Xiede98ced2013-01-29 10:13:12 +00002702 seqlock_init(&fs_info->profiles_lock);
Chris Mason19c00dd2007-10-15 16:19:22 -04002703
Chris Mason0b86a832008-03-24 15:01:56 -04002704 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
Chris Mason6324fbf2008-03-24 15:01:59 -04002705 INIT_LIST_HEAD(&fs_info->space_info);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002706 INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002707 INIT_LIST_HEAD(&fs_info->unused_bgs);
David Sterbac8bf1b62019-05-17 11:43:17 +02002708 extent_map_tree_init(&fs_info->mapping_tree);
Miao Xie66d8f3d2012-09-06 04:02:28 -06002709 btrfs_init_block_rsv(&fs_info->global_block_rsv,
2710 BTRFS_BLOCK_RSV_GLOBAL);
Miao Xie66d8f3d2012-09-06 04:02:28 -06002711 btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
2712 btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
2713 btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
2714 btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
2715 BTRFS_BLOCK_RSV_DELOPS);
Josef Bacikba2c4d42018-12-03 10:20:33 -05002716 btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
2717 BTRFS_BLOCK_RSV_DELREFS);
2718
Chris Mason771ed682008-11-06 22:02:51 -05002719 atomic_set(&fs_info->async_delalloc_pages, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04002720 atomic_set(&fs_info->defrag_running, 0);
Zhao Lei2fefd552016-01-07 18:38:48 +08002721 atomic_set(&fs_info->reada_works_cnt, 0);
Josef Bacik034f7842018-12-03 11:06:52 -05002722 atomic_set(&fs_info->nr_delayed_iputs, 0);
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002723 atomic64_set(&fs_info->tree_mod_seq, 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002724 fs_info->sb = sb;
Filipe David Borba Manana95ac5672013-08-08 22:45:48 +01002725 fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
Josef Bacik9ed74f22009-09-11 16:12:44 -04002726 fs_info->metadata_ratio = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04002727 fs_info->defrag_inodes = RB_ROOT;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002728 atomic64_set(&fs_info->free_chunk_space, 0);
Jan Schmidtf29021b2012-05-16 17:55:38 +02002729 fs_info->tree_mod_log = RB_ROOT;
David Sterba8b87dc12013-08-01 18:14:52 +02002730 fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
David Sterbaf8c269d2015-01-16 17:21:12 +01002731 fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */
Arne Jansen90519d62011-05-23 14:30:00 +02002732 /* readahead state */
Mel Gormand0164ad2015-11-06 16:28:21 -08002733 INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Arne Jansen90519d62011-05-23 14:30:00 +02002734 spin_lock_init(&fs_info->reada_lock);
Josef Bacikfd708b82017-09-29 15:43:50 -04002735 btrfs_init_ref_verify(fs_info);
Chris Masonc8b97812008-10-29 14:49:59 -04002736
Chris Masonb34b0862008-12-19 15:43:22 -05002737 fs_info->thread_pool_size = min_t(unsigned long,
2738 num_online_cpus() + 2, 8);
Chris Mason0afbaf82008-04-18 14:17:20 -04002739
Miao Xie199c2a92013-05-15 07:48:23 +00002740 INIT_LIST_HEAD(&fs_info->ordered_roots);
2741 spin_lock_init(&fs_info->ordered_root_lock);
Josef Bacik69fe2d72017-10-19 14:15:57 -04002742
2743 fs_info->btree_inode = new_inode(sb);
2744 if (!fs_info->btree_inode) {
2745 err = -ENOMEM;
2746 goto fail_bio_counter;
2747 }
2748 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
2749
Miao Xie16cdcec2011-04-22 18:12:22 +08002750 fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
David Sterba74e4d822016-02-11 11:01:55 +01002751 GFP_KERNEL);
Miao Xie16cdcec2011-04-22 18:12:22 +08002752 if (!fs_info->delayed_root) {
2753 err = -ENOMEM;
2754 goto fail_iput;
2755 }
2756 btrfs_init_delayed_root(fs_info->delayed_root);
Chris Mason3eaa2882008-07-24 11:57:52 -04002757
Eric Sandeen638aa7e2014-08-01 18:12:38 -05002758 btrfs_init_scrub(fs_info);
Stefan Behrens21adbd52011-11-09 13:44:05 +01002759#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2760 fs_info->check_integrity_print_mask = 0;
2761#endif
Eric Sandeen779a65a2014-08-01 18:12:39 -05002762 btrfs_init_balance(fs_info);
Miao Xie21c7e752014-05-13 17:29:04 -07002763 btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
Arne Jansena2de7332011-03-08 14:14:00 +01002764
David Sterba9f6d2512017-06-16 01:48:05 +02002765 sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
2766 sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
Chris Masona061fc82008-05-07 11:43:44 -04002767
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002768 btrfs_init_btree_inode(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04002769
Chris Masone02119d2008-09-05 16:13:11 -04002770 spin_lock_init(&fs_info->block_group_cache_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002771 fs_info->block_group_cache_tree = RB_ROOT;
Liu Boa1897fd2012-12-27 09:01:23 +00002772 fs_info->first_logical_byte = (u64)-1;
Chris Mason925baed2008-06-25 16:01:30 -04002773
Qu Wenruo43eb5f22019-03-01 10:47:59 +08002774 extent_io_tree_init(fs_info, &fs_info->freed_extents[0],
2775 IO_TREE_FS_INFO_FREED_EXTENTS0, NULL);
2776 extent_io_tree_init(fs_info, &fs_info->freed_extents[1],
2777 IO_TREE_FS_INFO_FREED_EXTENTS1, NULL);
Yan Zheng11833d62009-09-11 16:11:19 -04002778 fs_info->pinned_extents = &fs_info->freed_extents[0];
Josef Bacikafcdd122016-09-02 15:40:02 -04002779 set_bit(BTRFS_FS_BARRIER, &fs_info->flags);
Chris Mason509659c2007-05-10 12:36:17 -04002780
Chris Mason5a3f23d2009-03-31 13:27:11 -04002781 mutex_init(&fs_info->ordered_operations_mutex);
Chris Mason79154b12007-03-22 15:59:16 -04002782 mutex_init(&fs_info->tree_log_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002783 mutex_init(&fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04002784 mutex_init(&fs_info->transaction_kthread_mutex);
2785 mutex_init(&fs_info->cleaner_mutex);
Chris Mason1bbc6212015-04-06 12:46:08 -07002786 mutex_init(&fs_info->ro_block_group_mutex);
Josef Bacik9e351cc2014-03-13 15:42:13 -04002787 init_rwsem(&fs_info->commit_root_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00002788 init_rwsem(&fs_info->cleanup_work_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -04002789 init_rwsem(&fs_info->subvol_sem);
Stefan Behrens803b2f52013-08-15 17:11:21 +02002790 sema_init(&fs_info->uuid_tree_rescan_sem, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002791
Eric Sandeenad618362014-08-01 18:12:41 -05002792 btrfs_init_dev_replace_locks(fs_info);
Eric Sandeenf9e92e42014-08-01 18:12:42 -05002793 btrfs_init_qgroup(fs_info);
Arne Jansen416ac512011-09-13 12:56:09 +02002794
Chris Masonfa9c0d792009-04-03 09:47:43 -04002795 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
2796 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
2797
Chris Masone6dcd2d2008-07-17 12:53:50 -04002798 init_waitqueue_head(&fs_info->transaction_throttle);
Chris Masonf9295742008-07-17 12:54:14 -04002799 init_waitqueue_head(&fs_info->transaction_wait);
Sage Weilbb9c12c2010-10-29 15:37:34 -04002800 init_waitqueue_head(&fs_info->transaction_blocked_wait);
Chris Mason4854ddd2008-08-15 15:34:17 -04002801 init_waitqueue_head(&fs_info->async_submit_wait);
Josef Bacik034f7842018-12-03 11:06:52 -05002802 init_waitqueue_head(&fs_info->delayed_iputs_wait);
Chris Mason3768f362007-03-13 16:47:54 -04002803
Jeff Mahoneyda170662016-06-15 09:22:56 -04002804 /* Usable values until the real ones are cached from the superblock */
2805 fs_info->nodesize = 4096;
2806 fs_info->sectorsize = 4096;
2807 fs_info->stripesize = 4096;
2808
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002809 spin_lock_init(&fs_info->swapfile_pins_lock);
2810 fs_info->swapfile_pins = RB_ROOT;
2811
David Woodhouse53b381b2013-01-29 18:40:14 -05002812 ret = btrfs_alloc_stripe_hash_table(fs_info);
2813 if (ret) {
David Sterba83c82662013-03-01 15:03:00 +00002814 err = ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05002815 goto fail_alloc;
2816 }
2817
Jeff Mahoneyda170662016-06-15 09:22:56 -04002818 __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
Chris Mason7eccb902007-04-11 15:53:25 -04002819
Chris Mason3c4bb262012-03-27 18:56:56 -04002820 invalidate_bdev(fs_devices->latest_bdev);
David Sterba1104a882013-03-06 15:57:46 +01002821
2822 /*
2823 * Read super block and check the signature bytes only
2824 */
Yan Zhenga512bbf2008-12-08 16:46:26 -05002825 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +08002826 if (IS_ERR(bh)) {
2827 err = PTR_ERR(bh);
Miao Xie16cdcec2011-04-22 18:12:22 +08002828 goto fail_alloc;
Dave Young20b45072011-01-08 10:09:13 +00002829 }
Chris Mason39279cc2007-06-12 06:35:45 -04002830
David Sterba1104a882013-03-06 15:57:46 +01002831 /*
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02002832 * Verify the type first, if that or the the checksum value are
2833 * corrupted, we'll find out
2834 */
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +02002835 csum_type = btrfs_super_csum_type((struct btrfs_super_block *)bh->b_data);
2836 if (!btrfs_supported_super_csum(csum_type)) {
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02002837 btrfs_err(fs_info, "unsupported checksum algorithm: %u",
Johannes Thumshirn51bce6c2019-06-03 16:58:55 +02002838 csum_type);
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02002839 err = -EINVAL;
2840 brelse(bh);
2841 goto fail_alloc;
2842 }
2843
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002844 ret = btrfs_init_csum_hash(fs_info, csum_type);
2845 if (ret) {
2846 err = ret;
2847 goto fail_alloc;
2848 }
2849
Johannes Thumshirn8dc3f222019-06-03 16:58:54 +02002850 /*
David Sterba1104a882013-03-06 15:57:46 +01002851 * We want to check superblock checksum, the type is stored inside.
2852 * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
2853 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002854 if (btrfs_check_super_csum(fs_info, bh->b_data)) {
David Sterba05135f52016-05-09 11:32:39 +02002855 btrfs_err(fs_info, "superblock checksum mismatch");
David Sterba1104a882013-03-06 15:57:46 +01002856 err = -EINVAL;
Anand Jainb2acddd2015-10-07 17:23:23 +08002857 brelse(bh);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002858 goto fail_csum;
David Sterba1104a882013-03-06 15:57:46 +01002859 }
2860
2861 /*
2862 * super_copy is zeroed at allocation time and we never touch the
2863 * following bytes up to INFO_SIZE, the checksum is calculated from
2864 * the whole block of INFO_SIZE
2865 */
David Sterba6c417612011-04-13 15:41:04 +02002866 memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy));
Chris Masona061fc82008-05-07 11:43:44 -04002867 brelse(bh);
Chris Mason5f39d392007-10-15 16:14:19 -04002868
Nikolay Borisovfbc6fea2018-10-30 16:43:25 +02002869 disk_super = fs_info->super_copy;
2870
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002871 ASSERT(!memcmp(fs_info->fs_devices->fsid, fs_info->super_copy->fsid,
2872 BTRFS_FSID_SIZE));
2873
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002874 if (btrfs_fs_incompat(fs_info, METADATA_UUID)) {
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002875 ASSERT(!memcmp(fs_info->fs_devices->metadata_uuid,
2876 fs_info->super_copy->metadata_uuid,
2877 BTRFS_FSID_SIZE));
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002878 }
Chris Mason0b86a832008-03-24 15:01:56 -04002879
Nikolay Borisovfbc6fea2018-10-30 16:43:25 +02002880 features = btrfs_super_flags(disk_super);
2881 if (features & BTRFS_SUPER_FLAG_CHANGING_FSID_V2) {
2882 features &= ~BTRFS_SUPER_FLAG_CHANGING_FSID_V2;
2883 btrfs_set_super_flags(disk_super, features);
2884 btrfs_info(fs_info,
2885 "found metadata UUID change in progress flag, clearing");
2886 }
2887
2888 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2889 sizeof(*fs_info->super_for_commit));
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002890
Qu Wenruo069ec952018-05-11 13:35:26 +08002891 ret = btrfs_validate_mount_super(fs_info);
David Sterba1104a882013-03-06 15:57:46 +01002892 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02002893 btrfs_err(fs_info, "superblock contains fatal errors");
David Sterba1104a882013-03-06 15:57:46 +01002894 err = -EINVAL;
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002895 goto fail_csum;
David Sterba1104a882013-03-06 15:57:46 +01002896 }
2897
Chris Mason0f7d52f2007-04-09 10:42:37 -04002898 if (!btrfs_super_root(disk_super))
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002899 goto fail_csum;
Chris Mason0f7d52f2007-04-09 10:42:37 -04002900
liuboacce9522011-01-06 19:30:25 +08002901 /* check FS state, whether FS is broken. */
Miao Xie87533c42013-01-29 10:14:48 +00002902 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
2903 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
liuboacce9522011-01-06 19:30:25 +08002904
Liu Bo75e7cb72011-03-22 10:12:20 +00002905 /*
Chris Masonaf31f5e2011-11-03 15:17:42 -04002906 * run through our array of backup supers and setup
2907 * our ring pointer to the oldest one
2908 */
2909 generation = btrfs_super_generation(disk_super);
2910 find_oldest_super_backup(fs_info, generation);
2911
2912 /*
Liu Bo75e7cb72011-03-22 10:12:20 +00002913 * In the long term, we'll store the compression type in the super
2914 * block, and it'll be used for per file compression control.
2915 */
2916 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2917
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002918 ret = btrfs_parse_options(fs_info, options, sb->s_flags);
Yan Zheng2b820322008-11-17 21:11:30 -05002919 if (ret) {
2920 err = ret;
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002921 goto fail_csum;
Yan Zheng2b820322008-11-17 21:11:30 -05002922 }
Chris Masondfe25022008-05-13 13:46:40 -04002923
Josef Bacikf2b636e2008-12-02 06:36:08 -05002924 features = btrfs_super_incompat_flags(disk_super) &
2925 ~BTRFS_FEATURE_INCOMPAT_SUPP;
2926 if (features) {
David Sterba05135f52016-05-09 11:32:39 +02002927 btrfs_err(fs_info,
2928 "cannot mount because of unsupported optional features (%llx)",
2929 features);
Josef Bacikf2b636e2008-12-02 06:36:08 -05002930 err = -EINVAL;
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002931 goto fail_csum;
Josef Bacikf2b636e2008-12-02 06:36:08 -05002932 }
2933
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002934 features = btrfs_super_incompat_flags(disk_super);
Li Zefana6fa6fa2010-10-25 15:12:26 +08002935 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002936 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
Li Zefana6fa6fa2010-10-25 15:12:26 +08002937 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
Nick Terrell5c1aab12017-08-09 19:39:02 -07002938 else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
2939 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
Chris Mason727011e2010-08-06 13:21:20 -04002940
Josef Bacik3173a182013-03-07 14:22:04 -05002941 if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
David Sterba05135f52016-05-09 11:32:39 +02002942 btrfs_info(fs_info, "has skinny extents");
Josef Bacik3173a182013-03-07 14:22:04 -05002943
Chris Mason727011e2010-08-06 13:21:20 -04002944 /*
2945 * flag our filesystem as having big metadata blocks if
2946 * they are bigger than the page size
2947 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002948 if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) {
Chris Mason727011e2010-08-06 13:21:20 -04002949 if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
David Sterba05135f52016-05-09 11:32:39 +02002950 btrfs_info(fs_info,
2951 "flagging fs with big metadata feature");
Chris Mason727011e2010-08-06 13:21:20 -04002952 features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
2953 }
2954
Chris Masonbc3f1162012-03-29 17:02:47 -04002955 nodesize = btrfs_super_nodesize(disk_super);
Chris Masonbc3f1162012-03-29 17:02:47 -04002956 sectorsize = btrfs_super_sectorsize(disk_super);
Chandan Rajendrab7f67052016-06-23 15:16:44 +05302957 stripesize = sectorsize;
David Sterba707e8a02014-06-04 19:22:26 +02002958 fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
Miao Xie963d6782013-01-29 10:10:51 +00002959 fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
Chris Masonbc3f1162012-03-29 17:02:47 -04002960
Jeff Mahoneyda170662016-06-15 09:22:56 -04002961 /* Cache block sizes */
2962 fs_info->nodesize = nodesize;
2963 fs_info->sectorsize = sectorsize;
2964 fs_info->stripesize = stripesize;
2965
Chris Masonbc3f1162012-03-29 17:02:47 -04002966 /*
2967 * mixed block groups end up with duplicate but slightly offset
2968 * extent buffers for the same range. It leads to corruptions
2969 */
2970 if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
David Sterba707e8a02014-06-04 19:22:26 +02002971 (sectorsize != nodesize)) {
David Sterba05135f52016-05-09 11:32:39 +02002972 btrfs_err(fs_info,
2973"unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
2974 nodesize, sectorsize);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002975 goto fail_csum;
Chris Masonbc3f1162012-03-29 17:02:47 -04002976 }
2977
Miao Xieceda0862013-04-11 10:30:16 +00002978 /*
2979 * Needn't use the lock because there is no other task which will
2980 * update the flag.
2981 */
Li Zefana6fa6fa2010-10-25 15:12:26 +08002982 btrfs_set_super_incompat_flags(disk_super, features);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983
Josef Bacikf2b636e2008-12-02 06:36:08 -05002984 features = btrfs_super_compat_ro_flags(disk_super) &
2985 ~BTRFS_FEATURE_COMPAT_RO_SUPP;
David Howellsbc98a422017-07-17 08:45:34 +01002986 if (!sb_rdonly(sb) && features) {
David Sterba05135f52016-05-09 11:32:39 +02002987 btrfs_err(fs_info,
2988 "cannot mount read-write because of unsupported optional features (%llx)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002989 features);
Josef Bacikf2b636e2008-12-02 06:36:08 -05002990 err = -EINVAL;
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02002991 goto fail_csum;
Josef Bacikf2b636e2008-12-02 06:36:08 -05002992 }
Chris Mason61d92c32009-10-02 19:11:56 -04002993
Eric Sandeen2a458192015-02-16 16:29:26 +01002994 ret = btrfs_init_workqueues(fs_info, fs_devices);
2995 if (ret) {
2996 err = ret;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002997 goto fail_sb_buffer;
2998 }
Chris Mason4543df72008-06-11 21:47:56 -04002999
Jan Kara9e11cee2017-04-12 12:24:32 +02003000 sb->s_bdi->congested_fn = btrfs_congested_fn;
3001 sb->s_bdi->congested_data = fs_info;
3002 sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK;
Nikolay Borisovb5420232019-03-11 23:28:13 -07003003 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
Jan Kara9e11cee2017-04-12 12:24:32 +02003004 sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
3005 sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
Chris Mason4575c9c2008-04-18 16:13:31 -04003006
Chris Masona061fc82008-05-07 11:43:44 -04003007 sb->s_blocksize = sectorsize;
3008 sb->s_blocksize_bits = blksize_bits(sectorsize);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003009 memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
Chris Masondb945352007-10-15 16:15:53 -04003010
Chris Mason925baed2008-06-25 16:01:30 -04003011 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003012 ret = btrfs_read_sys_array(fs_info);
Chris Mason925baed2008-06-25 16:01:30 -04003013 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason84eed902008-04-25 09:04:37 -04003014 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003015 btrfs_err(fs_info, "failed to read the system array: %d", ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003016 goto fail_sb_buffer;
Chris Mason84eed902008-04-25 09:04:37 -04003017 }
Chris Mason0b86a832008-03-24 15:01:56 -04003018
Yan Zheng84234f32008-10-29 14:49:05 -04003019 generation = btrfs_super_chunk_root_generation(disk_super);
Qu Wenruo581c1762018-03-29 09:08:11 +08003020 level = btrfs_super_chunk_root_level(disk_super);
Chris Mason0b86a832008-03-24 15:01:56 -04003021
Jeff Mahoneyda170662016-06-15 09:22:56 -04003022 __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
Chris Mason0b86a832008-03-24 15:01:56 -04003023
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003024 chunk_root->node = read_tree_block(fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04003025 btrfs_super_chunk_root(disk_super),
Qu Wenruo581c1762018-03-29 09:08:11 +08003026 generation, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08003027 if (IS_ERR(chunk_root->node) ||
3028 !extent_buffer_uptodate(chunk_root->node)) {
David Sterba05135f52016-05-09 11:32:39 +02003029 btrfs_err(fs_info, "failed to read chunk root");
chandane5fffba2015-10-05 22:14:25 +05303030 if (!IS_ERR(chunk_root->node))
3031 free_extent_buffer(chunk_root->node);
Zhao Lei95ab1f62015-07-15 21:02:09 +08003032 chunk_root->node = NULL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04003033 goto fail_tree_roots;
David Woodhouse83121942009-07-22 16:52:13 -04003034 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035 btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
3036 chunk_root->commit_root = btrfs_root_node(chunk_root);
Chris Mason0b86a832008-03-24 15:01:56 -04003037
Chris Masone17cade2008-04-15 15:41:47 -04003038 read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003039 btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003040
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003041 ret = btrfs_read_chunk_tree(fs_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003042 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003043 btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003044 goto fail_tree_roots;
Yan Zheng2b820322008-11-17 21:11:30 -05003045 }
Chris Mason0b86a832008-03-24 15:01:56 -04003046
Stefan Behrens8dabb742012-11-06 13:15:27 +01003047 /*
Anand Jain9b99b112018-02-27 12:41:59 +08003048 * Keep the devid that is marked to be the target device for the
3049 * device replace procedure
Stefan Behrens8dabb742012-11-06 13:15:27 +01003050 */
Anand Jain9b99b112018-02-27 12:41:59 +08003051 btrfs_free_extra_devids(fs_devices, 0);
Chris Masondfe25022008-05-13 13:46:40 -04003052
Chris Masona6b0d5c2012-02-20 20:53:43 -05003053 if (!fs_devices->latest_bdev) {
David Sterba05135f52016-05-09 11:32:39 +02003054 btrfs_err(fs_info, "failed to read devices");
Chris Masona6b0d5c2012-02-20 20:53:43 -05003055 goto fail_tree_roots;
3056 }
3057
Chris Masonaf31f5e2011-11-03 15:17:42 -04003058retry_root_backup:
Yan Zheng84234f32008-10-29 14:49:05 -04003059 generation = btrfs_super_generation(disk_super);
Qu Wenruo581c1762018-03-29 09:08:11 +08003060 level = btrfs_super_root_level(disk_super);
Chris Mason0b86a832008-03-24 15:01:56 -04003061
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003062 tree_root->node = read_tree_block(fs_info,
Chris Masondb945352007-10-15 16:15:53 -04003063 btrfs_super_root(disk_super),
Qu Wenruo581c1762018-03-29 09:08:11 +08003064 generation, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08003065 if (IS_ERR(tree_root->node) ||
3066 !extent_buffer_uptodate(tree_root->node)) {
David Sterba05135f52016-05-09 11:32:39 +02003067 btrfs_warn(fs_info, "failed to read tree root");
chandane5fffba2015-10-05 22:14:25 +05303068 if (!IS_ERR(tree_root->node))
3069 free_extent_buffer(tree_root->node);
Zhao Lei95ab1f62015-07-15 21:02:09 +08003070 tree_root->node = NULL;
Chris Masonaf31f5e2011-11-03 15:17:42 -04003071 goto recovery_tree_root;
David Woodhouse83121942009-07-22 16:52:13 -04003072 }
Chris Masonaf31f5e2011-11-03 15:17:42 -04003073
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003074 btrfs_set_root_node(&tree_root->root_item, tree_root->node);
3075 tree_root->commit_root = btrfs_root_node(tree_root);
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02003076 btrfs_set_root_refs(&tree_root->root_item, 1);
Chris Masondb945352007-10-15 16:15:53 -04003077
Chandan Rajendraf32e48e2016-01-07 18:56:59 +05303078 mutex_lock(&tree_root->objectid_mutex);
3079 ret = btrfs_find_highest_objectid(tree_root,
3080 &tree_root->highest_objectid);
3081 if (ret) {
3082 mutex_unlock(&tree_root->objectid_mutex);
3083 goto recovery_tree_root;
3084 }
3085
3086 ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
3087
3088 mutex_unlock(&tree_root->objectid_mutex);
3089
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003090 ret = btrfs_read_roots(fs_info);
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05003091 if (ret)
Chris Masonaf31f5e2011-11-03 15:17:42 -04003092 goto recovery_tree_root;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003093
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003094 fs_info->generation = generation;
3095 fs_info->last_trans_committed = generation;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003096
Qu Wenruocf90d882018-08-01 10:37:19 +08003097 ret = btrfs_verify_dev_extents(fs_info);
3098 if (ret) {
3099 btrfs_err(fs_info,
3100 "failed to verify dev extents against chunks: %d",
3101 ret);
3102 goto fail_block_groups;
3103 }
Ilya Dryomov68310a52012-06-22 12:24:12 -06003104 ret = btrfs_recover_balance(fs_info);
3105 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003106 btrfs_err(fs_info, "failed to recover balance: %d", ret);
Ilya Dryomov68310a52012-06-22 12:24:12 -06003107 goto fail_block_groups;
3108 }
3109
Stefan Behrens733f4fb2012-05-25 16:06:10 +02003110 ret = btrfs_init_dev_stats(fs_info);
3111 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003112 btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02003113 goto fail_block_groups;
3114 }
3115
Stefan Behrens8dabb742012-11-06 13:15:27 +01003116 ret = btrfs_init_dev_replace(fs_info);
3117 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003118 btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003119 goto fail_block_groups;
3120 }
3121
Anand Jain9b99b112018-02-27 12:41:59 +08003122 btrfs_free_extra_devids(fs_devices, 1);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003123
Anand Jainb7c35e812015-03-10 06:38:38 +08003124 ret = btrfs_sysfs_add_fsid(fs_devices, NULL);
3125 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003126 btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
3127 ret);
Anand Jainb7c35e812015-03-10 06:38:38 +08003128 goto fail_block_groups;
3129 }
3130
3131 ret = btrfs_sysfs_add_device(fs_devices);
3132 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003133 btrfs_err(fs_info, "failed to init sysfs device interface: %d",
3134 ret);
Anand Jainb7c35e812015-03-10 06:38:38 +08003135 goto fail_fsdev_sysfs;
3136 }
3137
Anand Jain96f31362015-08-14 18:32:46 +08003138 ret = btrfs_sysfs_add_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04003139 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003140 btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
Anand Jainb7c35e812015-03-10 06:38:38 +08003141 goto fail_fsdev_sysfs;
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04003142 }
3143
liuboc59021f2011-03-07 02:13:14 +00003144 ret = btrfs_init_space_info(fs_info);
3145 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003146 btrfs_err(fs_info, "failed to initialize space info: %d", ret);
Anand Jain2365dd32014-01-22 11:15:51 +08003147 goto fail_sysfs;
liuboc59021f2011-03-07 02:13:14 +00003148 }
3149
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003150 ret = btrfs_read_block_groups(fs_info);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003151 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003152 btrfs_err(fs_info, "failed to read block groups: %d", ret);
Anand Jain2365dd32014-01-22 11:15:51 +08003153 goto fail_sysfs;
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003154 }
Qu Wenruo4330e182017-03-09 09:34:37 +08003155
Anand Jain6528b992017-12-18 17:08:59 +08003156 if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) {
David Sterba05135f52016-05-09 11:32:39 +02003157 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01003158 "writable mount is not allowed due to too many missing devices");
Anand Jain2365dd32014-01-22 11:15:51 +08003159 goto fail_sysfs;
Stefan Behrens292fd7f2012-10-30 17:16:16 +00003160 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003161
Chris Masona74a4b92008-06-25 16:01:31 -04003162 fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
3163 "btrfs-cleaner");
Qinghuang Feng57506d52009-01-21 10:49:16 -05003164 if (IS_ERR(fs_info->cleaner_kthread))
Anand Jain2365dd32014-01-22 11:15:51 +08003165 goto fail_sysfs;
Chris Masona74a4b92008-06-25 16:01:31 -04003166
3167 fs_info->transaction_kthread = kthread_run(transaction_kthread,
3168 tree_root,
3169 "btrfs-transaction");
Qinghuang Feng57506d52009-01-21 10:49:16 -05003170 if (IS_ERR(fs_info->transaction_kthread))
Chris Mason3f157a22008-06-25 16:01:31 -04003171 goto fail_cleaner;
Chris Masona74a4b92008-06-25 16:01:31 -04003172
Hans van Kranenburg583b7232017-07-28 08:31:28 +02003173 if (!btrfs_test_opt(fs_info, NOSSD) &&
Chris Masonc2898112009-06-10 09:51:32 -04003174 !fs_info->fs_devices->rotating) {
Hans van Kranenburg583b7232017-07-28 08:31:28 +02003175 btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
Chris Masonc2898112009-06-10 09:51:32 -04003176 }
3177
David Sterba572d9ab2014-02-05 15:26:17 +01003178 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003179 * Mount does not set all options immediately, we can do it now and do
David Sterba572d9ab2014-02-05 15:26:17 +01003180 * not have to wait for transaction commit
3181 */
3182 btrfs_apply_pending_changes(fs_info);
Qu Wenruo3818aea2014-01-13 13:36:06 +08003183
Stefan Behrens21adbd52011-11-09 13:44:05 +01003184#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003185 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003186 ret = btrfsic_mount(fs_info, fs_devices,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003187 btrfs_test_opt(fs_info,
Stefan Behrens21adbd52011-11-09 13:44:05 +01003188 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
3189 1 : 0,
3190 fs_info->check_integrity_print_mask);
3191 if (ret)
David Sterba05135f52016-05-09 11:32:39 +02003192 btrfs_warn(fs_info,
3193 "failed to initialize integrity check module: %d",
3194 ret);
Stefan Behrens21adbd52011-11-09 13:44:05 +01003195 }
3196#endif
Arne Jansenbcef60f2011-09-13 15:23:30 +02003197 ret = btrfs_read_qgroup_config(fs_info);
3198 if (ret)
3199 goto fail_trans_kthread;
Stefan Behrens21adbd52011-11-09 13:44:05 +01003200
Josef Bacikfd708b82017-09-29 15:43:50 -04003201 if (btrfs_build_ref_tree(fs_info))
3202 btrfs_err(fs_info, "couldn't build ref tree");
3203
Qu Wenruo96da0912016-01-19 10:23:03 +08003204 /* do not make disk changes in broken FS or nologreplay is given */
3205 if (btrfs_super_log_root(disk_super) != 0 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
Eric Sandeen63443bf2014-08-01 18:12:46 -05003207 ret = btrfs_replay_log(fs_info, fs_devices);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003208 if (ret) {
Eric Sandeen63443bf2014-08-01 18:12:46 -05003209 err = ret;
Wang Shilong28c16cb2014-04-23 19:33:35 +08003210 goto fail_qgroup;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003211 }
Chris Masone02119d2008-09-05 16:13:11 -04003212 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003213
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003214 ret = btrfs_find_orphan_roots(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003215 if (ret)
Wang Shilong28c16cb2014-04-23 19:33:35 +08003216 goto fail_qgroup;
Yan, Zheng76dda932009-09-21 16:00:26 -04003217
David Howellsbc98a422017-07-17 08:45:34 +01003218 if (!sb_rdonly(sb)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 ret = btrfs_cleanup_fs_roots(fs_info);
Ilya Dryomov44c44af2012-06-22 12:14:13 -06003220 if (ret)
Wang Shilong28c16cb2014-04-23 19:33:35 +08003221 goto fail_qgroup;
Zygo Blaxell90c711a2016-06-12 23:39:58 -04003222
3223 mutex_lock(&fs_info->cleaner_mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003224 ret = btrfs_recover_relocation(tree_root);
Zygo Blaxell90c711a2016-06-12 23:39:58 -04003225 mutex_unlock(&fs_info->cleaner_mutex);
Miao Xied7ce5842010-02-02 08:46:44 +00003226 if (ret < 0) {
David Sterba05135f52016-05-09 11:32:39 +02003227 btrfs_warn(fs_info, "failed to recover relocation: %d",
3228 ret);
Miao Xied7ce5842010-02-02 08:46:44 +00003229 err = -EINVAL;
Arne Jansenbcef60f2011-09-13 15:23:30 +02003230 goto fail_qgroup;
Miao Xied7ce5842010-02-02 08:46:44 +00003231 }
Chris Mason7c2ca462008-11-19 15:13:35 -05003232 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003233
Chris Mason3de45862008-11-17 21:02:50 -05003234 location.objectid = BTRFS_FS_TREE_OBJECTID;
3235 location.type = BTRFS_ROOT_ITEM_KEY;
Miao Xiecb517ea2013-05-15 07:48:19 +00003236 location.offset = 0;
Chris Mason3de45862008-11-17 21:02:50 -05003237
Chris Mason3de45862008-11-17 21:02:50 -05003238 fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
Dan Carpenter3140c9a2010-05-29 09:44:10 +00003239 if (IS_ERR(fs_info->fs_root)) {
3240 err = PTR_ERR(fs_info->fs_root);
Liu Bof50f4352018-03-29 06:11:45 +08003241 btrfs_warn(fs_info, "failed to read fs tree: %d", err);
Arne Jansenbcef60f2011-09-13 15:23:30 +02003242 goto fail_qgroup;
Dan Carpenter3140c9a2010-05-29 09:44:10 +00003243 }
Chris Masonc2898112009-06-10 09:51:32 -04003244
David Howellsbc98a422017-07-17 08:45:34 +01003245 if (sb_rdonly(sb))
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003246 return 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003247
Omar Sandovalf8d468a2016-09-22 17:24:21 -07003248 if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
3249 btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
Omar Sandoval6675df32016-09-22 17:24:22 -07003250 clear_free_space_tree = 1;
3251 } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
3252 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
3253 btrfs_warn(fs_info, "free space tree is invalid");
3254 clear_free_space_tree = 1;
3255 }
3256
3257 if (clear_free_space_tree) {
Omar Sandovalf8d468a2016-09-22 17:24:21 -07003258 btrfs_info(fs_info, "clearing free space tree");
3259 ret = btrfs_clear_free_space_tree(fs_info);
3260 if (ret) {
3261 btrfs_warn(fs_info,
3262 "failed to clear free space tree: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003263 close_ctree(fs_info);
Omar Sandovalf8d468a2016-09-22 17:24:21 -07003264 return ret;
3265 }
3266 }
3267
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
Chris Mason511711a2015-12-30 07:52:35 -08003269 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
David Sterba05135f52016-05-09 11:32:39 +02003270 btrfs_info(fs_info, "creating free space tree");
Chris Mason511711a2015-12-30 07:52:35 -08003271 ret = btrfs_create_free_space_tree(fs_info);
3272 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003273 btrfs_warn(fs_info,
3274 "failed to create free space tree: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003275 close_ctree(fs_info);
Chris Mason511711a2015-12-30 07:52:35 -08003276 return ret;
3277 }
3278 }
3279
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003280 down_read(&fs_info->cleanup_work_sem);
3281 if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
3282 (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
Josef Bacike3acc2a2010-01-26 14:30:53 +00003283 up_read(&fs_info->cleanup_work_sem);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003284 close_ctree(fs_info);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003285 return ret;
3286 }
3287 up_read(&fs_info->cleanup_work_sem);
Ilya Dryomov59641012012-01-16 22:04:48 +02003288
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003289 ret = btrfs_resume_balance_async(fs_info);
3290 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003291 btrfs_warn(fs_info, "failed to resume balance: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003292 close_ctree(fs_info);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003293 return ret;
Josef Bacike3acc2a2010-01-26 14:30:53 +00003294 }
3295
Stefan Behrens8dabb742012-11-06 13:15:27 +01003296 ret = btrfs_resume_dev_replace_async(fs_info);
3297 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003298 btrfs_warn(fs_info, "failed to resume device replace: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003299 close_ctree(fs_info);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003300 return ret;
3301 }
3302
Jan Schmidtb382a322013-05-28 15:47:24 +00003303 btrfs_qgroup_rescan_resume(fs_info);
3304
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05003305 if (!fs_info->uuid_root) {
David Sterba05135f52016-05-09 11:32:39 +02003306 btrfs_info(fs_info, "creating UUID tree");
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003307 ret = btrfs_create_uuid_tree(fs_info);
3308 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003309 btrfs_warn(fs_info,
3310 "failed to create the UUID tree: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003311 close_ctree(fs_info);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003312 return ret;
3313 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003314 } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
Eric Sandeen4bbcaa62014-08-01 18:12:45 -05003315 fs_info->generation !=
3316 btrfs_super_uuid_tree_generation(disk_super)) {
David Sterba05135f52016-05-09 11:32:39 +02003317 btrfs_info(fs_info, "checking UUID tree");
Stefan Behrens70f80172013-08-15 17:11:23 +02003318 ret = btrfs_check_uuid_tree(fs_info);
3319 if (ret) {
David Sterba05135f52016-05-09 11:32:39 +02003320 btrfs_warn(fs_info,
3321 "failed to check the UUID tree: %d", ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003322 close_ctree(fs_info);
Stefan Behrens70f80172013-08-15 17:11:23 +02003323 return ret;
3324 }
3325 } else {
Josef Bacikafcdd122016-09-02 15:40:02 -04003326 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003327 }
Josef Bacikafcdd122016-09-02 15:40:02 -04003328 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003329
Qu Wenruo8dcddfa2016-01-19 10:23:02 +08003330 /*
3331 * backuproot only affect mount behavior, and if open_ctree succeeded,
3332 * no need to keep the flag
3333 */
3334 btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
3335
Al Viroad2b2c82011-11-17 01:10:02 -05003336 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003337
Arne Jansenbcef60f2011-09-13 15:23:30 +02003338fail_qgroup:
3339 btrfs_free_qgroup_config(fs_info);
Chris Mason7c2ca462008-11-19 15:13:35 -05003340fail_trans_kthread:
3341 kthread_stop(fs_info->transaction_kthread);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003342 btrfs_cleanup_transaction(fs_info);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003343 btrfs_free_fs_roots(fs_info);
Chris Mason3f157a22008-06-25 16:01:31 -04003344fail_cleaner:
Chris Masona74a4b92008-06-25 16:01:31 -04003345 kthread_stop(fs_info->cleaner_kthread);
Chris Mason7c2ca462008-11-19 15:13:35 -05003346
3347 /*
3348 * make sure we're done with the btree inode before we stop our
3349 * kthreads
3350 */
3351 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
Chris Mason7c2ca462008-11-19 15:13:35 -05003352
Anand Jain2365dd32014-01-22 11:15:51 +08003353fail_sysfs:
Anand Jain6618a592015-08-14 18:32:47 +08003354 btrfs_sysfs_remove_mounted(fs_info);
Anand Jain2365dd32014-01-22 11:15:51 +08003355
Anand Jainb7c35e812015-03-10 06:38:38 +08003356fail_fsdev_sysfs:
3357 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3358
Josef Bacik1b1d1f62010-03-19 20:49:55 +00003359fail_block_groups:
Josef Bacik54067ae2013-04-25 13:44:38 -04003360 btrfs_put_block_group_cache(fs_info);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003361
3362fail_tree_roots:
3363 free_root_pointers(fs_info, 1);
Miao Xie2b8195b2013-02-07 06:01:35 +00003364 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
Chris Masonaf31f5e2011-11-03 15:17:42 -04003365
Chris Mason39279cc2007-06-12 06:35:45 -04003366fail_sb_buffer:
Liu Bo7abadb62013-03-17 02:10:31 +00003367 btrfs_stop_all_workers(fs_info);
Filipe Manana5cdd7db2017-02-01 22:39:50 +00003368 btrfs_free_block_groups(fs_info);
Johannes Thumshirn6d97c6e2019-06-03 16:58:56 +02003369fail_csum:
3370 btrfs_free_csum_hash(fs_info);
Miao Xie16cdcec2011-04-22 18:12:22 +08003371fail_alloc:
Chris Mason4543df72008-06-11 21:47:56 -04003372fail_iput:
Ilya Dryomov586e46e2011-11-09 13:26:37 +02003373 btrfs_mapping_tree_free(&fs_info->mapping_tree);
3374
Chris Mason4543df72008-06-11 21:47:56 -04003375 iput(fs_info->btree_inode);
Miao Xiec404e0d2014-01-30 16:46:55 +08003376fail_bio_counter:
David Sterba7f8d2362018-04-05 01:04:49 +02003377 percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
Miao Xie963d6782013-01-29 10:10:51 +00003378fail_delalloc_bytes:
3379 percpu_counter_destroy(&fs_info->delalloc_bytes);
Miao Xiee2d84522013-01-29 10:09:20 +00003380fail_dirty_metadata_bytes:
3381 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
Josef Bacik4297ff82019-04-10 15:56:09 -04003382fail_dio_bytes:
3383 percpu_counter_destroy(&fs_info->dio_bytes);
Yan, Zheng76dda932009-09-21 16:00:26 -04003384fail_srcu:
3385 cleanup_srcu_struct(&fs_info->subvol_srcu);
Qinghuang Feng7e662852009-01-21 10:49:16 -05003386fail:
David Woodhouse53b381b2013-01-29 18:40:14 -05003387 btrfs_free_stripe_hash_table(fs_info);
Ilya Dryomov586e46e2011-11-09 13:26:37 +02003388 btrfs_close_devices(fs_info->fs_devices);
Al Viroad2b2c82011-11-17 01:10:02 -05003389 return err;
Chris Masonaf31f5e2011-11-03 15:17:42 -04003390
3391recovery_tree_root:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003392 if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
Chris Masonaf31f5e2011-11-03 15:17:42 -04003393 goto fail_tree_roots;
3394
3395 free_root_pointers(fs_info, 0);
3396
3397 /* don't use the log in recovery mode, it won't be valid */
3398 btrfs_set_super_log_root(disk_super, 0);
3399
3400 /* we can't trust the free space cache either */
3401 btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
3402
3403 ret = next_root_backup(fs_info, fs_info->super_copy,
3404 &num_backups_tried, &backup_index);
3405 if (ret == -1)
3406 goto fail_block_groups;
3407 goto retry_root_backup;
Chris Masoneb60cea2007-02-02 09:18:22 -05003408}
Masami Hiramatsu663faf92018-01-13 02:55:33 +09003409ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
Chris Masoneb60cea2007-02-02 09:18:22 -05003410
Chris Masonf2984462008-04-10 16:19:33 -04003411static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
3412{
Chris Masonf2984462008-04-10 16:19:33 -04003413 if (uptodate) {
3414 set_buffer_uptodate(bh);
3415 } else {
Stefan Behrens442a4f62012-05-25 16:06:08 +02003416 struct btrfs_device *device = (struct btrfs_device *)
3417 bh->b_private;
3418
Jeff Mahoneyfb456252016-06-22 18:54:56 -04003419 btrfs_warn_rl_in_rcu(device->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02003420 "lost page write due to IO error on %s",
Josef Bacik606686e2012-06-04 14:03:51 -04003421 rcu_str_deref(device->name));
Nicholas D Steeves01327612016-05-19 21:18:45 -04003422 /* note, we don't set_buffer_write_io_error because we have
Chris Mason1259ab72008-05-12 13:39:03 -04003423 * our own ways of dealing with the IO errors
3424 */
Chris Masonf2984462008-04-10 16:19:33 -04003425 clear_buffer_uptodate(bh);
Stefan Behrens442a4f62012-05-25 16:06:08 +02003426 btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
Chris Masonf2984462008-04-10 16:19:33 -04003427 }
3428 unlock_buffer(bh);
3429 put_bh(bh);
3430}
3431
Anand Jain29c36d72015-08-14 18:32:58 +08003432int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
3433 struct buffer_head **bh_ret)
3434{
3435 struct buffer_head *bh;
3436 struct btrfs_super_block *super;
3437 u64 bytenr;
3438
3439 bytenr = btrfs_sb_offset(copy_num);
3440 if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
3441 return -EINVAL;
3442
David Sterba9f6d2512017-06-16 01:48:05 +02003443 bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE);
Anand Jain29c36d72015-08-14 18:32:58 +08003444 /*
3445 * If we fail to read from the underlying devices, as of now
3446 * the best option we have is to mark it EIO.
3447 */
3448 if (!bh)
3449 return -EIO;
3450
3451 super = (struct btrfs_super_block *)bh->b_data;
3452 if (btrfs_super_bytenr(super) != bytenr ||
3453 btrfs_super_magic(super) != BTRFS_MAGIC) {
3454 brelse(bh);
3455 return -EINVAL;
3456 }
3457
3458 *bh_ret = bh;
3459 return 0;
3460}
3461
3462
Yan Zhenga512bbf2008-12-08 16:46:26 -05003463struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3464{
3465 struct buffer_head *bh;
3466 struct buffer_head *latest = NULL;
3467 struct btrfs_super_block *super;
3468 int i;
3469 u64 transid = 0;
Anand Jain92fc03f2015-08-14 18:32:51 +08003470 int ret = -EINVAL;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003471
3472 /* we would like to check all the supers, but that would make
3473 * a btrfs mount succeed after a mkfs from a different FS.
3474 * So, we need to add a special mount option to scan for
3475 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3476 */
3477 for (i = 0; i < 1; i++) {
Anand Jain29c36d72015-08-14 18:32:58 +08003478 ret = btrfs_read_dev_one_super(bdev, i, &bh);
3479 if (ret)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003480 continue;
3481
3482 super = (struct btrfs_super_block *)bh->b_data;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003483
3484 if (!latest || btrfs_super_generation(super) > transid) {
3485 brelse(latest);
3486 latest = bh;
3487 transid = btrfs_super_generation(super);
3488 } else {
3489 brelse(bh);
3490 }
3491 }
Anand Jain92fc03f2015-08-14 18:32:51 +08003492
3493 if (!latest)
3494 return ERR_PTR(ret);
3495
Yan Zhenga512bbf2008-12-08 16:46:26 -05003496 return latest;
3497}
3498
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003499/*
David Sterbaabbb3b82017-06-16 00:50:33 +02003500 * Write superblock @sb to the @device. Do not wait for completion, all the
3501 * buffer heads we write are pinned.
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003502 *
David Sterbaabbb3b82017-06-16 00:50:33 +02003503 * Write @max_mirrors copies of the superblock, where 0 means default that fit
3504 * the expected device size at commit time. Note that max_mirrors must be
3505 * same for write and wait phases.
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003506 *
David Sterbaabbb3b82017-06-16 00:50:33 +02003507 * Return number of errors when buffer head is not found or submission fails.
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003508 */
Yan Zhenga512bbf2008-12-08 16:46:26 -05003509static int write_dev_supers(struct btrfs_device *device,
David Sterbaabbb3b82017-06-16 00:50:33 +02003510 struct btrfs_super_block *sb, int max_mirrors)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003511{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003512 struct btrfs_fs_info *fs_info = device->fs_info;
3513 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003514 struct buffer_head *bh;
3515 int i;
3516 int ret;
3517 int errors = 0;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003518 u64 bytenr;
Omar Sandoval1b9e6192017-12-05 22:54:02 -08003519 int op_flags;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003520
3521 if (max_mirrors == 0)
3522 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3523
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003524 shash->tfm = fs_info->csum_shash;
3525
Yan Zhenga512bbf2008-12-08 16:46:26 -05003526 for (i = 0; i < max_mirrors; i++) {
3527 bytenr = btrfs_sb_offset(i);
Miao Xie935e5cc2014-09-03 21:35:33 +08003528 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3529 device->commit_total_bytes)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003530 break;
3531
David Sterbaabbb3b82017-06-16 00:50:33 +02003532 btrfs_set_super_bytenr(sb, bytenr);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003533
Johannes Thumshirnd5178572019-06-03 16:58:57 +02003534 crypto_shash_init(shash);
3535 crypto_shash_update(shash, (const char *)sb + BTRFS_CSUM_SIZE,
3536 BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
3537 crypto_shash_final(shash, sb->csum);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003538
David Sterbaabbb3b82017-06-16 00:50:33 +02003539 /* One reference for us, and we leave it for the caller */
David Sterba9f6d2512017-06-16 01:48:05 +02003540 bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE,
David Sterbaabbb3b82017-06-16 00:50:33 +02003541 BTRFS_SUPER_INFO_SIZE);
3542 if (!bh) {
3543 btrfs_err(device->fs_info,
3544 "couldn't get super buffer head for bytenr %llu",
3545 bytenr);
3546 errors++;
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003547 continue;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003548 }
3549
David Sterbaabbb3b82017-06-16 00:50:33 +02003550 memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
3551
3552 /* one reference for submit_bh */
3553 get_bh(bh);
3554
3555 set_buffer_uptodate(bh);
3556 lock_buffer(bh);
3557 bh->b_end_io = btrfs_end_buffer_write_sync;
3558 bh->b_private = device;
3559
Chris Mason387125f2011-11-18 15:07:51 -05003560 /*
3561 * we fua the first super. The others we allow
3562 * to go down lazy.
3563 */
Omar Sandoval1b9e6192017-12-05 22:54:02 -08003564 op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
3565 if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
3566 op_flags |= REQ_FUA;
3567 ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh);
Hisashi Hifumi4eedeb72009-06-10 15:28:55 -04003568 if (ret)
Yan Zhenga512bbf2008-12-08 16:46:26 -05003569 errors++;
Yan Zhenga512bbf2008-12-08 16:46:26 -05003570 }
3571 return errors < i ? 0 : -1;
3572}
3573
Chris Mason387125f2011-11-18 15:07:51 -05003574/*
David Sterbaabbb3b82017-06-16 00:50:33 +02003575 * Wait for write completion of superblocks done by write_dev_supers,
3576 * @max_mirrors same for write and wait phases.
3577 *
3578 * Return number of errors when buffer head is not found or not marked up to
3579 * date.
3580 */
3581static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
3582{
3583 struct buffer_head *bh;
3584 int i;
3585 int errors = 0;
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003586 bool primary_failed = false;
David Sterbaabbb3b82017-06-16 00:50:33 +02003587 u64 bytenr;
3588
3589 if (max_mirrors == 0)
3590 max_mirrors = BTRFS_SUPER_MIRROR_MAX;
3591
3592 for (i = 0; i < max_mirrors; i++) {
3593 bytenr = btrfs_sb_offset(i);
3594 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3595 device->commit_total_bytes)
3596 break;
3597
David Sterba9f6d2512017-06-16 01:48:05 +02003598 bh = __find_get_block(device->bdev,
3599 bytenr / BTRFS_BDEV_BLOCKSIZE,
David Sterbaabbb3b82017-06-16 00:50:33 +02003600 BTRFS_SUPER_INFO_SIZE);
3601 if (!bh) {
3602 errors++;
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003603 if (i == 0)
3604 primary_failed = true;
David Sterbaabbb3b82017-06-16 00:50:33 +02003605 continue;
3606 }
3607 wait_on_buffer(bh);
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003608 if (!buffer_uptodate(bh)) {
David Sterbaabbb3b82017-06-16 00:50:33 +02003609 errors++;
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003610 if (i == 0)
3611 primary_failed = true;
3612 }
David Sterbaabbb3b82017-06-16 00:50:33 +02003613
3614 /* drop our reference */
3615 brelse(bh);
3616
3617 /* drop the reference from the writing run */
3618 brelse(bh);
3619 }
3620
Howard McLauchlanb6a535f2018-02-02 11:09:01 -08003621 /* log error, force error return */
3622 if (primary_failed) {
3623 btrfs_err(device->fs_info, "error writing primary super block to device %llu",
3624 device->devid);
3625 return -1;
3626 }
3627
David Sterbaabbb3b82017-06-16 00:50:33 +02003628 return errors < i ? 0 : -1;
3629}
3630
3631/*
Chris Mason387125f2011-11-18 15:07:51 -05003632 * endio for the write_dev_flush, this will wake anyone waiting
3633 * for the barrier when it is done
3634 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003635static void btrfs_end_empty_barrier(struct bio *bio)
Chris Mason387125f2011-11-18 15:07:51 -05003636{
David Sterbae0ae9992017-06-06 17:06:06 +02003637 complete(bio->bi_private);
Chris Mason387125f2011-11-18 15:07:51 -05003638}
3639
3640/*
Anand Jain4fc64412017-06-13 17:05:41 +08003641 * Submit a flush request to the device if it supports it. Error handling is
3642 * done in the waiting counterpart.
Chris Mason387125f2011-11-18 15:07:51 -05003643 */
Anand Jain4fc64412017-06-13 17:05:41 +08003644static void write_dev_flush(struct btrfs_device *device)
Chris Mason387125f2011-11-18 15:07:51 -05003645{
Anand Jainc2a9c7a2017-04-06 11:22:53 +08003646 struct request_queue *q = bdev_get_queue(device->bdev);
David Sterbae0ae9992017-06-06 17:06:06 +02003647 struct bio *bio = device->flush_bio;
Chris Mason387125f2011-11-18 15:07:51 -05003648
Anand Jainc2a9c7a2017-04-06 11:22:53 +08003649 if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags))
Anand Jain4fc64412017-06-13 17:05:41 +08003650 return;
Chris Mason387125f2011-11-18 15:07:51 -05003651
David Sterbae0ae9992017-06-06 17:06:06 +02003652 bio_reset(bio);
Chris Mason387125f2011-11-18 15:07:51 -05003653 bio->bi_end_io = btrfs_end_empty_barrier;
Christoph Hellwig74d46992017-08-23 19:10:32 +02003654 bio_set_dev(bio, device->bdev);
Jan Kara8d910122017-05-02 17:03:50 +02003655 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
Chris Mason387125f2011-11-18 15:07:51 -05003656 init_completion(&device->flush_wait);
3657 bio->bi_private = &device->flush_wait;
Chris Mason387125f2011-11-18 15:07:51 -05003658
Lu Fengqi43a01112017-08-18 16:38:07 +08003659 btrfsic_submit_bio(bio);
Anand Jain1c3063b2017-12-04 12:54:56 +08003660 set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
Anand Jain4fc64412017-06-13 17:05:41 +08003661}
Chris Mason387125f2011-11-18 15:07:51 -05003662
Anand Jain4fc64412017-06-13 17:05:41 +08003663/*
3664 * If the flush bio has been submitted by write_dev_flush, wait for it.
3665 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07003666static blk_status_t wait_dev_flush(struct btrfs_device *device)
Anand Jain4fc64412017-06-13 17:05:41 +08003667{
Anand Jain4fc64412017-06-13 17:05:41 +08003668 struct bio *bio = device->flush_bio;
3669
Anand Jain1c3063b2017-12-04 12:54:56 +08003670 if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
Omar Sandoval58efbc92017-08-22 23:45:59 -07003671 return BLK_STS_OK;
Anand Jain4fc64412017-06-13 17:05:41 +08003672
Anand Jain1c3063b2017-12-04 12:54:56 +08003673 clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
David Sterba2980d572017-06-15 16:04:26 +02003674 wait_for_completion_io(&device->flush_wait);
Anand Jain4fc64412017-06-13 17:05:41 +08003675
Linus Torvalds8c27cb32017-07-05 16:41:23 -07003676 return bio->bi_status;
Chris Mason387125f2011-11-18 15:07:51 -05003677}
3678
Qu Wenruod10b82f2017-06-27 17:28:40 +08003679static int check_barrier_error(struct btrfs_fs_info *fs_info)
Anand Jain401b41e2017-05-06 07:17:54 +08003680{
Anand Jain6528b992017-12-18 17:08:59 +08003681 if (!btrfs_check_rw_degradable(fs_info, NULL))
Anand Jain401b41e2017-05-06 07:17:54 +08003682 return -EIO;
Chris Mason387125f2011-11-18 15:07:51 -05003683 return 0;
3684}
3685
3686/*
3687 * send an empty flush down to each device in parallel,
3688 * then wait for them
3689 */
3690static int barrier_all_devices(struct btrfs_fs_info *info)
3691{
3692 struct list_head *head;
3693 struct btrfs_device *dev;
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003694 int errors_wait = 0;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02003695 blk_status_t ret;
Chris Mason387125f2011-11-18 15:07:51 -05003696
David Sterba1538e6c2017-06-16 00:28:47 +02003697 lockdep_assert_held(&info->fs_devices->device_list_mutex);
Chris Mason387125f2011-11-18 15:07:51 -05003698 /* send down all the barriers */
3699 head = &info->fs_devices->devices;
David Sterba1538e6c2017-06-16 00:28:47 +02003700 list_for_each_entry(dev, head, dev_list) {
Anand Jaine6e674b2017-12-04 12:54:54 +08003701 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
Hidetoshi Setof88ba6a2014-02-05 16:34:38 +09003702 continue;
Anand Jaincea7c8b2017-06-13 17:05:40 +08003703 if (!dev->bdev)
Chris Mason387125f2011-11-18 15:07:51 -05003704 continue;
Anand Jaine12c9622017-12-04 12:54:53 +08003705 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08003706 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Mason387125f2011-11-18 15:07:51 -05003707 continue;
3708
Anand Jain4fc64412017-06-13 17:05:41 +08003709 write_dev_flush(dev);
Omar Sandoval58efbc92017-08-22 23:45:59 -07003710 dev->last_flush_error = BLK_STS_OK;
Chris Mason387125f2011-11-18 15:07:51 -05003711 }
3712
3713 /* wait for all the barriers */
David Sterba1538e6c2017-06-16 00:28:47 +02003714 list_for_each_entry(dev, head, dev_list) {
Anand Jaine6e674b2017-12-04 12:54:54 +08003715 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
Hidetoshi Setof88ba6a2014-02-05 16:34:38 +09003716 continue;
Chris Mason387125f2011-11-18 15:07:51 -05003717 if (!dev->bdev) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003718 errors_wait++;
Chris Mason387125f2011-11-18 15:07:51 -05003719 continue;
3720 }
Anand Jaine12c9622017-12-04 12:54:53 +08003721 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08003722 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Mason387125f2011-11-18 15:07:51 -05003723 continue;
3724
Anand Jain4fc64412017-06-13 17:05:41 +08003725 ret = wait_dev_flush(dev);
Anand Jain401b41e2017-05-06 07:17:54 +08003726 if (ret) {
3727 dev->last_flush_error = ret;
David Sterba66b49932017-06-15 16:20:43 +02003728 btrfs_dev_stat_inc_and_print(dev,
3729 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003730 errors_wait++;
Anand Jain401b41e2017-05-06 07:17:54 +08003731 }
Chris Mason387125f2011-11-18 15:07:51 -05003732 }
Anand Jain401b41e2017-05-06 07:17:54 +08003733
Anand Jaincea7c8b2017-06-13 17:05:40 +08003734 if (errors_wait) {
Anand Jain401b41e2017-05-06 07:17:54 +08003735 /*
3736 * At some point we need the status of all disks
3737 * to arrive at the volume status. So error checking
3738 * is being pushed to a separate loop.
3739 */
Qu Wenruod10b82f2017-06-27 17:28:40 +08003740 return check_barrier_error(info);
Anand Jain401b41e2017-05-06 07:17:54 +08003741 }
Chris Mason387125f2011-11-18 15:07:51 -05003742 return 0;
3743}
3744
Zhao Lei943c6e92015-08-19 15:54:15 +08003745int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
3746{
Zhao Lei8789f4f2015-09-15 21:08:07 +08003747 int raid_type;
3748 int min_tolerated = INT_MAX;
Zhao Lei943c6e92015-08-19 15:54:15 +08003749
Zhao Lei8789f4f2015-09-15 21:08:07 +08003750 if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
3751 (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
David Sterba8c3e3582019-05-17 11:43:36 +02003752 min_tolerated = min_t(int, min_tolerated,
Zhao Lei8789f4f2015-09-15 21:08:07 +08003753 btrfs_raid_array[BTRFS_RAID_SINGLE].
3754 tolerated_failures);
Zhao Lei943c6e92015-08-19 15:54:15 +08003755
Zhao Lei8789f4f2015-09-15 21:08:07 +08003756 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
3757 if (raid_type == BTRFS_RAID_SINGLE)
3758 continue;
Anand Jain41a6e892018-04-25 19:01:43 +08003759 if (!(flags & btrfs_raid_array[raid_type].bg_flag))
Zhao Lei8789f4f2015-09-15 21:08:07 +08003760 continue;
David Sterba8c3e3582019-05-17 11:43:36 +02003761 min_tolerated = min_t(int, min_tolerated,
Zhao Lei8789f4f2015-09-15 21:08:07 +08003762 btrfs_raid_array[raid_type].
3763 tolerated_failures);
3764 }
Zhao Lei943c6e92015-08-19 15:54:15 +08003765
Zhao Lei8789f4f2015-09-15 21:08:07 +08003766 if (min_tolerated == INT_MAX) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04003767 pr_warn("BTRFS: unknown raid flag: %llu", flags);
Zhao Lei8789f4f2015-09-15 21:08:07 +08003768 min_tolerated = 0;
3769 }
3770
3771 return min_tolerated;
Zhao Lei943c6e92015-08-19 15:54:15 +08003772}
3773
David Sterbaeece6a92017-02-10 19:04:32 +01003774int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
Chris Masonf2984462008-04-10 16:19:33 -04003775{
Chris Masone5e9a522009-06-10 15:17:02 -04003776 struct list_head *head;
Chris Masonf2984462008-04-10 16:19:33 -04003777 struct btrfs_device *dev;
Chris Masona061fc82008-05-07 11:43:44 -04003778 struct btrfs_super_block *sb;
Chris Masonf2984462008-04-10 16:19:33 -04003779 struct btrfs_dev_item *dev_item;
Chris Masonf2984462008-04-10 16:19:33 -04003780 int ret;
3781 int do_barriers;
Chris Masona236aed2008-04-29 09:38:00 -04003782 int max_errors;
3783 int total_errors = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003784 u64 flags;
Chris Masonf2984462008-04-10 16:19:33 -04003785
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003786 do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
Liu Bofed3b382017-09-13 12:25:21 -06003787
3788 /*
3789 * max_mirrors == 0 indicates we're from commit_transaction,
3790 * not from fsync where the tree roots in fs_info have not
3791 * been consistent on disk.
3792 */
3793 if (max_mirrors == 0)
3794 backup_super_roots(fs_info);
Chris Masonf2984462008-04-10 16:19:33 -04003795
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003796 sb = fs_info->super_for_commit;
Chris Masona061fc82008-05-07 11:43:44 -04003797 dev_item = &sb->dev_item;
Chris Masone5e9a522009-06-10 15:17:02 -04003798
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003799 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3800 head = &fs_info->fs_devices->devices;
3801 max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
Chris Mason387125f2011-11-18 15:07:51 -05003802
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003803 if (do_barriers) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003804 ret = barrier_all_devices(fs_info);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003805 if (ret) {
3806 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003807 &fs_info->fs_devices->device_list_mutex);
3808 btrfs_handle_fs_error(fs_info, ret,
3809 "errors while submitting device barriers.");
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003810 return ret;
3811 }
3812 }
Chris Mason387125f2011-11-18 15:07:51 -05003813
David Sterba1538e6c2017-06-16 00:28:47 +02003814 list_for_each_entry(dev, head, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04003815 if (!dev->bdev) {
3816 total_errors++;
3817 continue;
3818 }
Anand Jaine12c9622017-12-04 12:54:53 +08003819 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08003820 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Masondfe25022008-05-13 13:46:40 -04003821 continue;
3822
Yan Zheng2b820322008-11-17 21:11:30 -05003823 btrfs_set_stack_device_generation(dev_item, 0);
Chris Masona061fc82008-05-07 11:43:44 -04003824 btrfs_set_stack_device_type(dev_item, dev->type);
3825 btrfs_set_stack_device_id(dev_item, dev->devid);
Miao Xie7df69d32014-07-24 11:37:13 +08003826 btrfs_set_stack_device_total_bytes(dev_item,
Miao Xie935e5cc2014-09-03 21:35:33 +08003827 dev->commit_total_bytes);
Miao Xiece7213c2014-09-03 21:35:34 +08003828 btrfs_set_stack_device_bytes_used(dev_item,
3829 dev->commit_bytes_used);
Chris Masona061fc82008-05-07 11:43:44 -04003830 btrfs_set_stack_device_io_align(dev_item, dev->io_align);
3831 btrfs_set_stack_device_io_width(dev_item, dev->io_width);
3832 btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
3833 memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02003834 memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
3835 BTRFS_FSID_SIZE);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003836
Chris Masona061fc82008-05-07 11:43:44 -04003837 flags = btrfs_super_flags(sb);
3838 btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonf2984462008-04-10 16:19:33 -04003839
Qu Wenruo75cb8572018-05-11 13:35:27 +08003840 ret = btrfs_validate_write_super(fs_info, sb);
3841 if (ret < 0) {
3842 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3843 btrfs_handle_fs_error(fs_info, -EUCLEAN,
3844 "unexpected superblock corruption detected");
3845 return -EUCLEAN;
3846 }
3847
David Sterbaabbb3b82017-06-16 00:50:33 +02003848 ret = write_dev_supers(dev, sb, max_mirrors);
Chris Masona236aed2008-04-29 09:38:00 -04003849 if (ret)
3850 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04003851 }
Chris Masona236aed2008-04-29 09:38:00 -04003852 if (total_errors > max_errors) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003853 btrfs_err(fs_info, "%d errors while writing supers",
3854 total_errors);
3855 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003856
Stefan Behrens9d565ba2013-08-09 17:08:40 +02003857 /* FUA is masked off if unsupported and can't be the reason */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003858 btrfs_handle_fs_error(fs_info, -EIO,
3859 "%d errors while writing supers",
3860 total_errors);
Stefan Behrens9d565ba2013-08-09 17:08:40 +02003861 return -EIO;
Chris Masona236aed2008-04-29 09:38:00 -04003862 }
Chris Masonf2984462008-04-10 16:19:33 -04003863
Yan Zhenga512bbf2008-12-08 16:46:26 -05003864 total_errors = 0;
David Sterba1538e6c2017-06-16 00:28:47 +02003865 list_for_each_entry(dev, head, dev_list) {
Chris Masondfe25022008-05-13 13:46:40 -04003866 if (!dev->bdev)
3867 continue;
Anand Jaine12c9622017-12-04 12:54:53 +08003868 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08003869 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
Chris Masondfe25022008-05-13 13:46:40 -04003870 continue;
3871
David Sterbaabbb3b82017-06-16 00:50:33 +02003872 ret = wait_dev_supers(dev, max_mirrors);
Yan Zhenga512bbf2008-12-08 16:46:26 -05003873 if (ret)
3874 total_errors++;
Chris Masonf2984462008-04-10 16:19:33 -04003875 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003876 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Masona236aed2008-04-29 09:38:00 -04003877 if (total_errors > max_errors) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003878 btrfs_handle_fs_error(fs_info, -EIO,
3879 "%d errors while writing supers",
3880 total_errors);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003881 return -EIO;
Chris Masona236aed2008-04-29 09:38:00 -04003882 }
Chris Masonf2984462008-04-10 16:19:33 -04003883 return 0;
3884}
3885
Miao Xiecb517ea2013-05-15 07:48:19 +00003886/* Drop a fs root from the radix tree and free it. */
3887void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
3888 struct btrfs_root *root)
Chris Mason2619ba12007-04-10 16:58:11 -04003889{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003890 spin_lock(&fs_info->fs_roots_radix_lock);
Chris Mason2619ba12007-04-10 16:58:11 -04003891 radix_tree_delete(&fs_info->fs_roots_radix,
3892 (unsigned long)root->root_key.objectid);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003893 spin_unlock(&fs_info->fs_roots_radix_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003894
3895 if (btrfs_root_refs(&root->root_item) == 0)
3896 synchronize_srcu(&fs_info->subvol_srcu);
3897
Liu Bo1c1ea4f2016-07-19 15:36:05 -07003898 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
Liu Bo33217192013-02-27 13:28:24 +00003899 btrfs_free_log(NULL, root);
Liu Bo1c1ea4f2016-07-19 15:36:05 -07003900 if (root->reloc_root) {
3901 free_extent_buffer(root->reloc_root->node);
3902 free_extent_buffer(root->reloc_root->commit_root);
3903 btrfs_put_fs_root(root->reloc_root);
3904 root->reloc_root = NULL;
3905 }
3906 }
Liu Bo33217192013-02-27 13:28:24 +00003907
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04003908 if (root->free_ino_pinned)
3909 __btrfs_remove_free_space_cache(root->free_ino_pinned);
3910 if (root->free_ino_ctl)
3911 __btrfs_remove_free_space_cache(root->free_ino_ctl);
David Sterba84db5cc2018-07-20 16:30:25 +02003912 btrfs_free_fs_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913}
3914
David Sterba84db5cc2018-07-20 16:30:25 +02003915void btrfs_free_fs_root(struct btrfs_root *root)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003916{
David Sterba57cdc8d2014-02-05 02:37:48 +01003917 iput(root->ino_cache_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003918 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
Al Viro0ee5dc62011-07-07 15:44:25 -04003919 if (root->anon_dev)
3920 free_anon_bdev(root->anon_dev);
Miao Xie8257b2d2014-03-06 13:38:19 +08003921 if (root->subv_writers)
3922 btrfs_free_subvolume_writers(root->subv_writers);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003923 free_extent_buffer(root->node);
3924 free_extent_buffer(root->commit_root);
Li Zefan581bb052011-04-20 10:06:11 +08003925 kfree(root->free_ino_ctl);
3926 kfree(root->free_ino_pinned);
Miao Xieb0feb9d2013-05-15 07:48:20 +00003927 btrfs_put_fs_root(root);
Chris Mason2619ba12007-04-10 16:58:11 -04003928}
3929
Yan Zhengc146afa2008-11-12 14:34:12 -05003930int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
3931{
3932 u64 root_objectid = 0;
3933 struct btrfs_root *gang[8];
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003934 int i = 0;
3935 int err = 0;
3936 unsigned int ret = 0;
3937 int index;
Yan Zhengc146afa2008-11-12 14:34:12 -05003938
3939 while (1) {
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003940 index = srcu_read_lock(&fs_info->subvol_srcu);
Yan Zhengc146afa2008-11-12 14:34:12 -05003941 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
3942 (void **)gang, root_objectid,
3943 ARRAY_SIZE(gang));
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003944 if (!ret) {
3945 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan Zhengc146afa2008-11-12 14:34:12 -05003946 break;
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003947 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003948 root_objectid = gang[ret - 1]->root_key.objectid + 1;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003949
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003950 for (i = 0; i < ret; i++) {
3951 /* Avoid to grab roots in dead_roots */
3952 if (btrfs_root_refs(&gang[i]->root_item) == 0) {
3953 gang[i] = NULL;
3954 continue;
3955 }
3956 /* grab all the search result for later use */
3957 gang[i] = btrfs_grab_fs_root(gang[i]);
3958 }
3959 srcu_read_unlock(&fs_info->subvol_srcu, index);
3960
3961 for (i = 0; i < ret; i++) {
3962 if (!gang[i])
3963 continue;
Yan Zhengc146afa2008-11-12 14:34:12 -05003964 root_objectid = gang[i]->root_key.objectid;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003965 err = btrfs_orphan_cleanup(gang[i]);
3966 if (err)
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003967 break;
3968 btrfs_put_fs_root(gang[i]);
Yan Zhengc146afa2008-11-12 14:34:12 -05003969 }
3970 root_objectid++;
3971 }
Qu Wenruo65d33fd2014-04-22 17:13:51 +08003972
3973 /* release the uncleaned roots due to error */
3974 for (; i < ret; i++) {
3975 if (gang[i])
3976 btrfs_put_fs_root(gang[i]);
3977 }
3978 return err;
Yan Zhengc146afa2008-11-12 14:34:12 -05003979}
3980
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003981int btrfs_commit_super(struct btrfs_fs_info *fs_info)
Yan Zhengc146afa2008-11-12 14:34:12 -05003982{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003983 struct btrfs_root *root = fs_info->tree_root;
Yan Zhengc146afa2008-11-12 14:34:12 -05003984 struct btrfs_trans_handle *trans;
Yan Zhengc146afa2008-11-12 14:34:12 -05003985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003986 mutex_lock(&fs_info->cleaner_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003987 btrfs_run_delayed_iputs(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003988 mutex_unlock(&fs_info->cleaner_mutex);
3989 wake_up_process(fs_info->cleaner_kthread);
Yan, Zhengc71bf092009-11-12 09:34:40 +00003990
3991 /* wait until ongoing cleanup work done */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003992 down_write(&fs_info->cleanup_work_sem);
3993 up_write(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00003994
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003995 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00003996 if (IS_ERR(trans))
3997 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003998 return btrfs_commit_transaction(trans);
Yan Zhengc146afa2008-11-12 14:34:12 -05003999}
4000
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004001void close_ctree(struct btrfs_fs_info *fs_info)
Chris Masoneb60cea2007-02-02 09:18:22 -05004002{
Yan Zhengc146afa2008-11-12 14:34:12 -05004003 int ret;
Chris Masone089f052007-03-16 16:20:31 -04004004
Josef Bacikafcdd122016-09-02 15:40:02 -04004005 set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07004006 /*
4007 * We don't want the cleaner to start new transactions, add more delayed
4008 * iputs, etc. while we're closing. We can't use kthread_stop() yet
4009 * because that frees the task_struct, and the transaction kthread might
4010 * still try to wake up the cleaner.
4011 */
4012 kthread_park(fs_info->cleaner_kthread);
Chris Masona2135012008-06-25 16:01:30 -04004013
Justin Maggard7343dd62015-11-04 15:56:16 -08004014 /* wait for the qgroup rescan worker to stop */
Jeff Mahoneyd06f23d2016-08-08 22:08:06 -04004015 btrfs_qgroup_wait_for_completion(fs_info, false);
Justin Maggard7343dd62015-11-04 15:56:16 -08004016
Stefan Behrens803b2f52013-08-15 17:11:21 +02004017 /* wait for the uuid_scan task to finish */
4018 down(&fs_info->uuid_tree_rescan_sem);
4019 /* avoid complains from lockdep et al., set sem back to initial state */
4020 up(&fs_info->uuid_tree_rescan_sem);
4021
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004022 /* pause restriper - we want to resume on mount */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004023 btrfs_pause_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004024
Stefan Behrens8dabb742012-11-06 13:15:27 +01004025 btrfs_dev_replace_suspend_for_unmount(fs_info);
4026
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004027 btrfs_scrub_cancel(fs_info);
Chris Mason4cb53002011-05-24 15:35:30 -04004028
4029 /* wait for any defraggers to finish */
4030 wait_event(fs_info->transaction_wait,
4031 (atomic_read(&fs_info->defrag_running) == 0));
4032
4033 /* clear out the rbtree of defraggable inodes */
Miao Xie26176e72012-11-26 09:26:20 +00004034 btrfs_cleanup_defrag_inodes(fs_info);
Chris Mason4cb53002011-05-24 15:35:30 -04004035
Miao Xie21c7e752014-05-13 17:29:04 -07004036 cancel_work_sync(&fs_info->async_reclaim_work);
4037
David Howellsbc98a422017-07-17 08:45:34 +01004038 if (!sb_rdonly(fs_info->sb)) {
Jeff Mahoneye44163e2015-06-15 09:41:18 -04004039 /*
Omar Sandovald6fd0ae2018-10-31 10:06:08 -07004040 * The cleaner kthread is stopped, so do one final pass over
4041 * unused block groups.
Jeff Mahoneye44163e2015-06-15 09:41:18 -04004042 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 btrfs_delete_unused_bgs(fs_info);
Jeff Mahoneye44163e2015-06-15 09:41:18 -04004044
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004045 ret = btrfs_commit_super(fs_info);
liuboacce9522011-01-06 19:30:25 +08004046 if (ret)
Eric Sandeen04892342014-08-01 18:12:36 -05004047 btrfs_err(fs_info, "commit super ret %d", ret);
liuboacce9522011-01-06 19:30:25 +08004048 }
4049
Liu Boaf722732018-03-31 06:11:56 +08004050 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) ||
4051 test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004052 btrfs_error_commit_super(fs_info);
Chris Masoned2ff2c2007-03-01 18:59:40 -05004053
Al Viroe3029d92011-11-17 00:56:18 -05004054 kthread_stop(fs_info->transaction_kthread);
4055 kthread_stop(fs_info->cleaner_kthread);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004056
Josef Bacike1878312018-09-28 07:18:03 -04004057 ASSERT(list_empty(&fs_info->delayed_iputs));
Josef Bacikafcdd122016-09-02 15:40:02 -04004058 set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
Yan Zhengf25784b2009-07-28 08:41:57 -04004059
Eric Sandeen04892342014-08-01 18:12:36 -05004060 btrfs_free_qgroup_config(fs_info);
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004061 ASSERT(list_empty(&fs_info->delalloc_roots));
Arne Jansenbcef60f2011-09-13 15:23:30 +02004062
Miao Xie963d6782013-01-29 10:10:51 +00004063 if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
Eric Sandeen04892342014-08-01 18:12:36 -05004064 btrfs_info(fs_info, "at unmount delalloc count %lld",
Miao Xie963d6782013-01-29 10:10:51 +00004065 percpu_counter_sum(&fs_info->delalloc_bytes));
Chris Masonb0c68f82008-01-31 11:05:37 -05004066 }
Yanbcc63ab2008-07-30 16:29:20 -04004067
Josef Bacik4297ff82019-04-10 15:56:09 -04004068 if (percpu_counter_sum(&fs_info->dio_bytes))
4069 btrfs_info(fs_info, "at unmount dio bytes count %lld",
4070 percpu_counter_sum(&fs_info->dio_bytes));
4071
Anand Jain6618a592015-08-14 18:32:47 +08004072 btrfs_sysfs_remove_mounted(fs_info);
Anand Jainb7c35e812015-03-10 06:38:38 +08004073 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04004074
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04004075 btrfs_free_fs_roots(fs_info);
Chris Masond10c5f32007-12-17 20:14:04 -05004076
Liu Bo1a4319c2014-01-13 19:53:53 +08004077 btrfs_put_block_group_cache(fs_info);
4078
Wang Shilongde348ee2014-04-09 19:23:22 +08004079 /*
4080 * we must make sure there is not any read request to
4081 * submit after we stopping all workers.
4082 */
4083 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
Josef Bacik96192492013-10-16 13:53:28 -04004084 btrfs_stop_all_workers(fs_info);
4085
Filipe Manana5cdd7db2017-02-01 22:39:50 +00004086 btrfs_free_block_groups(fs_info);
4087
Josef Bacikafcdd122016-09-02 15:40:02 -04004088 clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
Josef Bacik13e6c372013-05-30 16:55:44 -04004089 free_root_pointers(fs_info, 1);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04004090
Josef Bacik13e6c372013-05-30 16:55:44 -04004091 iput(fs_info->btree_inode);
Chris Masond6bfde82008-04-30 13:59:35 -04004092
Stefan Behrens21adbd52011-11-09 13:44:05 +01004093#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004094 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004095 btrfsic_unmount(fs_info->fs_devices);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004096#endif
4097
Chris Mason0b86a832008-03-24 15:01:56 -04004098 btrfs_mapping_tree_free(&fs_info->mapping_tree);
Nikolay Borisov68c94e52019-02-12 16:13:14 +02004099 btrfs_close_devices(fs_info->fs_devices);
Chris Masonb248a412008-04-14 09:48:18 -04004100
Miao Xiee2d84522013-01-29 10:09:20 +00004101 percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
Miao Xie963d6782013-01-29 10:10:51 +00004102 percpu_counter_destroy(&fs_info->delalloc_bytes);
Josef Bacik4297ff82019-04-10 15:56:09 -04004103 percpu_counter_destroy(&fs_info->dio_bytes);
David Sterba7f8d2362018-04-05 01:04:49 +02004104 percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
Yan, Zheng76dda932009-09-21 16:00:26 -04004105 cleanup_srcu_struct(&fs_info->subvol_srcu);
Chris Mason0b86a832008-03-24 15:01:56 -04004106
David Woodhouse53b381b2013-01-29 18:40:14 -05004107 btrfs_free_stripe_hash_table(fs_info);
Josef Bacikfd708b82017-09-29 15:43:50 -04004108 btrfs_free_ref_cache(fs_info);
Chris Masoneb60cea2007-02-02 09:18:22 -05004109}
4110
Chris Masonb9fab912012-05-06 07:23:47 -04004111int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
4112 int atomic)
Chris Masonccd467d2007-06-28 15:57:36 -04004113{
Chris Mason1259ab72008-05-12 13:39:03 -04004114 int ret;
Chris Mason727011e2010-08-06 13:21:20 -04004115 struct inode *btree_inode = buf->pages[0]->mapping->host;
Chris Mason1259ab72008-05-12 13:39:03 -04004116
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004117 ret = extent_buffer_uptodate(buf);
Chris Mason1259ab72008-05-12 13:39:03 -04004118 if (!ret)
4119 return ret;
4120
4121 ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
Chris Masonb9fab912012-05-06 07:23:47 -04004122 parent_transid, atomic);
4123 if (ret == -EAGAIN)
4124 return ret;
Chris Mason1259ab72008-05-12 13:39:03 -04004125 return !ret;
Chris Mason5f39d392007-10-15 16:14:19 -04004126}
Chris Mason6702ed42007-08-07 16:15:09 -04004127
Chris Mason5f39d392007-10-15 16:14:19 -04004128void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4129{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004130 struct btrfs_fs_info *fs_info;
Josef Bacik06ea65a2013-09-19 16:07:01 -04004131 struct btrfs_root *root;
Chris Mason5f39d392007-10-15 16:14:19 -04004132 u64 transid = btrfs_header_generation(buf);
Chris Masonb9473432009-03-13 11:00:37 -04004133 int was_dirty;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004134
Josef Bacik06ea65a2013-09-19 16:07:01 -04004135#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4136 /*
4137 * This is a fast path so only do this check if we have sanity tests
Andrea Gelmini52042d82018-11-28 12:05:13 +01004138 * enabled. Normal people shouldn't be using unmapped buffers as dirty
Josef Bacik06ea65a2013-09-19 16:07:01 -04004139 * outside of the sanity tests.
4140 */
Nikolay Borisovb0132a32018-06-27 16:38:24 +03004141 if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
Josef Bacik06ea65a2013-09-19 16:07:01 -04004142 return;
4143#endif
4144 root = BTRFS_I(buf->pages[0]->mapping->host)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004145 fs_info = root->fs_info;
Chris Masonb9447ef82009-03-09 11:45:38 -04004146 btrfs_assert_tree_locked(buf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004147 if (transid != fs_info->generation)
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004148 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004149 buf->start, transid, fs_info->generation);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004150 was_dirty = set_extent_buffer_dirty(buf);
Miao Xiee2d84522013-01-29 10:09:20 +00004151 if (!was_dirty)
Nikolay Borisov104b4e52017-06-20 21:01:20 +03004152 percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
4153 buf->len,
4154 fs_info->dirty_metadata_batch);
Filipe Manana1f21ef02014-04-09 15:37:06 +01004155#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08004156 /*
4157 * Since btrfs_mark_buffer_dirty() can be called with item pointer set
4158 * but item data not updated.
4159 * So here we should only check item pointers, not item data.
4160 */
4161 if (btrfs_header_level(buf) == 0 &&
David Sterbacfdaad52019-03-20 16:24:18 +01004162 btrfs_check_leaf_relaxed(buf)) {
David Sterbaa4f78752017-06-29 18:37:49 +02004163 btrfs_print_leaf(buf);
Filipe Manana1f21ef02014-04-09 15:37:06 +01004164 ASSERT(0);
4165 }
4166#endif
Chris Masoneb60cea2007-02-02 09:18:22 -05004167}
4168
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004169static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
Liu Bob53d3f52012-11-14 14:34:34 +00004170 int flush_delayed)
Chris Mason35b7e472007-05-02 15:53:43 -04004171{
Chris Mason188de642008-05-09 11:52:25 -04004172 /*
4173 * looks as though older kernels can get into trouble with
4174 * this code, they end up stuck in balance_dirty_pages forever
4175 */
Miao Xiee2d84522013-01-29 10:09:20 +00004176 int ret;
Chris Masond6bfde82008-04-30 13:59:35 -04004177
Jens Axboe6933c022009-03-17 09:36:37 +01004178 if (current->flags & PF_MEMALLOC)
Chris Masond6bfde82008-04-30 13:59:35 -04004179 return;
4180
Liu Bob53d3f52012-11-14 14:34:34 +00004181 if (flush_delayed)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004182 btrfs_balance_delayed_items(fs_info);
Miao Xie16cdcec2011-04-22 18:12:22 +08004183
Ethan Liend814a492018-07-02 15:44:58 +08004184 ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4185 BTRFS_DIRTY_METADATA_THRESH,
4186 fs_info->dirty_metadata_batch);
Miao Xiee2d84522013-01-29 10:09:20 +00004187 if (ret > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004188 balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
Miao Xie16cdcec2011-04-22 18:12:22 +08004189 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004190}
4191
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004192void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
Miao Xie16cdcec2011-04-22 18:12:22 +08004193{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004194 __btrfs_btree_balance_dirty(fs_info, 1);
Liu Bob53d3f52012-11-14 14:34:34 +00004195}
Miao Xie16cdcec2011-04-22 18:12:22 +08004196
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004197void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
Liu Bob53d3f52012-11-14 14:34:34 +00004198{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004199 __btrfs_btree_balance_dirty(fs_info, 0);
Chris Mason35b7e472007-05-02 15:53:43 -04004200}
Chris Mason6b800532007-10-15 16:17:34 -04004201
Qu Wenruo581c1762018-03-29 09:08:11 +08004202int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
4203 struct btrfs_key *first_key)
Chris Mason6b800532007-10-15 16:17:34 -04004204{
David Sterba5ab12d12019-03-20 14:56:39 +01004205 return btree_read_extent_buffer_pages(buf, parent_transid,
Qu Wenruo581c1762018-03-29 09:08:11 +08004206 level, first_key);
Chris Mason6b800532007-10-15 16:17:34 -04004207}
Chris Mason0da54682007-11-07 21:08:01 -05004208
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004209static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
liuboacce9522011-01-06 19:30:25 +08004210{
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004211 /* cleanup FS via transaction */
4212 btrfs_cleanup_transaction(fs_info);
4213
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004214 mutex_lock(&fs_info->cleaner_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004215 btrfs_run_delayed_iputs(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004216 mutex_unlock(&fs_info->cleaner_mutex);
liuboacce9522011-01-06 19:30:25 +08004217
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004218 down_write(&fs_info->cleanup_work_sem);
4219 up_write(&fs_info->cleanup_work_sem);
liuboacce9522011-01-06 19:30:25 +08004220}
4221
Jeff Mahoney143bede2012-03-01 14:56:26 +01004222static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004223{
liuboacce9522011-01-06 19:30:25 +08004224 struct btrfs_ordered_extent *ordered;
liuboacce9522011-01-06 19:30:25 +08004225
Miao Xie199c2a92013-05-15 07:48:23 +00004226 spin_lock(&root->ordered_extent_lock);
Josef Bacik779880e2013-01-31 14:30:08 -05004227 /*
4228 * This will just short circuit the ordered completion stuff which will
4229 * make sure the ordered extent gets properly cleaned up.
4230 */
Miao Xie199c2a92013-05-15 07:48:23 +00004231 list_for_each_entry(ordered, &root->ordered_extents,
Josef Bacik779880e2013-01-31 14:30:08 -05004232 root_extent_list)
4233 set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
Miao Xie199c2a92013-05-15 07:48:23 +00004234 spin_unlock(&root->ordered_extent_lock);
4235}
4236
4237static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4238{
4239 struct btrfs_root *root;
4240 struct list_head splice;
4241
4242 INIT_LIST_HEAD(&splice);
4243
4244 spin_lock(&fs_info->ordered_root_lock);
4245 list_splice_init(&fs_info->ordered_roots, &splice);
4246 while (!list_empty(&splice)) {
4247 root = list_first_entry(&splice, struct btrfs_root,
4248 ordered_root);
Josef Bacik1de2cfd2013-09-27 16:36:02 -04004249 list_move_tail(&root->ordered_root,
4250 &fs_info->ordered_roots);
Miao Xie199c2a92013-05-15 07:48:23 +00004251
Liu Bo2a85d9c2014-02-10 17:07:16 +08004252 spin_unlock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00004253 btrfs_destroy_ordered_extents(root);
4254
Liu Bo2a85d9c2014-02-10 17:07:16 +08004255 cond_resched();
4256 spin_lock(&fs_info->ordered_root_lock);
Miao Xie199c2a92013-05-15 07:48:23 +00004257 }
4258 spin_unlock(&fs_info->ordered_root_lock);
Josef Bacik74d5d222018-11-21 14:05:45 -05004259
4260 /*
4261 * We need this here because if we've been flipped read-only we won't
4262 * get sync() from the umount, so we need to make sure any ordered
4263 * extents that haven't had their dirty pages IO start writeout yet
4264 * actually get run and error out properly.
4265 */
4266 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
liuboacce9522011-01-06 19:30:25 +08004267}
4268
Stefan Behrens35a36212013-08-14 18:12:25 +02004269static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004270 struct btrfs_fs_info *fs_info)
liuboacce9522011-01-06 19:30:25 +08004271{
4272 struct rb_node *node;
4273 struct btrfs_delayed_ref_root *delayed_refs;
4274 struct btrfs_delayed_ref_node *ref;
4275 int ret = 0;
4276
4277 delayed_refs = &trans->delayed_refs;
4278
4279 spin_lock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004280 if (atomic_read(&delayed_refs->num_entries) == 0) {
David Sterbacfece4d2011-04-25 19:43:52 -04004281 spin_unlock(&delayed_refs->lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004282 btrfs_info(fs_info, "delayed_refs has NO entry");
liuboacce9522011-01-06 19:30:25 +08004283 return ret;
4284 }
4285
Liu Bo5c9d0282018-08-23 03:51:49 +08004286 while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05004287 struct btrfs_delayed_ref_head *head;
Josef Bacik0e0adbc2017-10-19 14:16:00 -04004288 struct rb_node *n;
Josef Bacike78417d2013-06-03 16:42:36 -04004289 bool pin_bytes = false;
liuboacce9522011-01-06 19:30:25 +08004290
Josef Bacikd7df2c72014-01-23 09:21:38 -05004291 head = rb_entry(node, struct btrfs_delayed_ref_head,
4292 href_node);
Josef Bacik3069bd22018-11-21 14:05:39 -05004293 if (btrfs_delayed_ref_lock(delayed_refs, head))
Josef Bacikd7df2c72014-01-23 09:21:38 -05004294 continue;
Josef Bacik3069bd22018-11-21 14:05:39 -05004295
Josef Bacikd7df2c72014-01-23 09:21:38 -05004296 spin_lock(&head->lock);
Liu Boe3d03962018-08-23 03:51:50 +08004297 while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
Josef Bacik0e0adbc2017-10-19 14:16:00 -04004298 ref = rb_entry(n, struct btrfs_delayed_ref_node,
4299 ref_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004300 ref->in_tree = 0;
Liu Boe3d03962018-08-23 03:51:50 +08004301 rb_erase_cached(&ref->ref_node, &head->ref_tree);
Josef Bacik0e0adbc2017-10-19 14:16:00 -04004302 RB_CLEAR_NODE(&ref->ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +08004303 if (!list_empty(&ref->add_list))
4304 list_del(&ref->add_list);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004305 atomic_dec(&delayed_refs->num_entries);
4306 btrfs_put_delayed_ref(ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004307 }
4308 if (head->must_insert_reserved)
4309 pin_bytes = true;
4310 btrfs_free_delayed_extent_op(head->extent_op);
Josef Bacikfa781ce2018-11-21 14:05:40 -05004311 btrfs_delete_ref_head(delayed_refs, head);
Josef Bacikd7df2c72014-01-23 09:21:38 -05004312 spin_unlock(&head->lock);
4313 spin_unlock(&delayed_refs->lock);
4314 mutex_unlock(&head->mutex);
liuboacce9522011-01-06 19:30:25 +08004315
Josef Bacikd7df2c72014-01-23 09:21:38 -05004316 if (pin_bytes)
Josef Bacikd2788502017-09-29 15:43:57 -04004317 btrfs_pin_extent(fs_info, head->bytenr,
4318 head->num_bytes, 1);
Josef Bacik31890da2018-11-21 14:05:41 -05004319 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
Josef Bacikd2788502017-09-29 15:43:57 -04004320 btrfs_put_delayed_ref_head(head);
liuboacce9522011-01-06 19:30:25 +08004321 cond_resched();
4322 spin_lock(&delayed_refs->lock);
4323 }
4324
4325 spin_unlock(&delayed_refs->lock);
4326
4327 return ret;
4328}
4329
Jeff Mahoney143bede2012-03-01 14:56:26 +01004330static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
liuboacce9522011-01-06 19:30:25 +08004331{
4332 struct btrfs_inode *btrfs_inode;
4333 struct list_head splice;
4334
4335 INIT_LIST_HEAD(&splice);
4336
Miao Xieeb73c1b2013-05-15 07:48:22 +00004337 spin_lock(&root->delalloc_lock);
4338 list_splice_init(&root->delalloc_inodes, &splice);
liuboacce9522011-01-06 19:30:25 +08004339
4340 while (!list_empty(&splice)) {
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004341 struct inode *inode = NULL;
Miao Xieeb73c1b2013-05-15 07:48:22 +00004342 btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
4343 delalloc_inodes);
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004344 __btrfs_del_delalloc_inode(root, btrfs_inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004345 spin_unlock(&root->delalloc_lock);
liuboacce9522011-01-06 19:30:25 +08004346
Nikolay Borisovfe816d02018-04-27 12:21:53 +03004347 /*
4348 * Make sure we get a live inode and that it'll not disappear
4349 * meanwhile.
4350 */
4351 inode = igrab(&btrfs_inode->vfs_inode);
4352 if (inode) {
4353 invalidate_inode_pages2(inode->i_mapping);
4354 iput(inode);
4355 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00004356 spin_lock(&root->delalloc_lock);
liuboacce9522011-01-06 19:30:25 +08004357 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00004358 spin_unlock(&root->delalloc_lock);
4359}
4360
4361static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4362{
4363 struct btrfs_root *root;
4364 struct list_head splice;
4365
4366 INIT_LIST_HEAD(&splice);
4367
4368 spin_lock(&fs_info->delalloc_root_lock);
4369 list_splice_init(&fs_info->delalloc_roots, &splice);
4370 while (!list_empty(&splice)) {
4371 root = list_first_entry(&splice, struct btrfs_root,
4372 delalloc_root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00004373 root = btrfs_grab_fs_root(root);
4374 BUG_ON(!root);
4375 spin_unlock(&fs_info->delalloc_root_lock);
4376
4377 btrfs_destroy_delalloc_inodes(root);
4378 btrfs_put_fs_root(root);
4379
4380 spin_lock(&fs_info->delalloc_root_lock);
4381 }
4382 spin_unlock(&fs_info->delalloc_root_lock);
liuboacce9522011-01-06 19:30:25 +08004383}
4384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004385static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
liuboacce9522011-01-06 19:30:25 +08004386 struct extent_io_tree *dirty_pages,
4387 int mark)
4388{
4389 int ret;
liuboacce9522011-01-06 19:30:25 +08004390 struct extent_buffer *eb;
4391 u64 start = 0;
4392 u64 end;
liuboacce9522011-01-06 19:30:25 +08004393
4394 while (1) {
4395 ret = find_first_extent_bit(dirty_pages, start, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04004396 mark, NULL);
liuboacce9522011-01-06 19:30:25 +08004397 if (ret)
4398 break;
4399
David Sterba91166212016-04-26 23:54:39 +02004400 clear_extent_bits(dirty_pages, start, end, mark);
liuboacce9522011-01-06 19:30:25 +08004401 while (start <= end) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004402 eb = find_extent_buffer(fs_info, start);
4403 start += fs_info->nodesize;
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004404 if (!eb)
liuboacce9522011-01-06 19:30:25 +08004405 continue;
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004406 wait_on_extent_buffer_writeback(eb);
liuboacce9522011-01-06 19:30:25 +08004407
Josef Bacikfd8b2b62013-04-24 16:41:19 -04004408 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
4409 &eb->bflags))
4410 clear_extent_buffer_dirty(eb);
4411 free_extent_buffer_stale(eb);
liuboacce9522011-01-06 19:30:25 +08004412 }
4413 }
4414
4415 return ret;
4416}
4417
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004418static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
liuboacce9522011-01-06 19:30:25 +08004419 struct extent_io_tree *pinned_extents)
4420{
4421 struct extent_io_tree *unpin;
4422 u64 start;
4423 u64 end;
4424 int ret;
Liu Boed0eaa12012-06-14 02:23:21 -06004425 bool loop = true;
liuboacce9522011-01-06 19:30:25 +08004426
4427 unpin = pinned_extents;
Liu Boed0eaa12012-06-14 02:23:21 -06004428again:
liuboacce9522011-01-06 19:30:25 +08004429 while (1) {
Filipe Manana0e6ec382018-11-16 13:04:44 +00004430 struct extent_state *cached_state = NULL;
4431
Lu Fengqifcd5e742018-10-24 20:24:03 +08004432 /*
4433 * The btrfs_finish_extent_commit() may get the same range as
4434 * ours between find_first_extent_bit and clear_extent_dirty.
4435 * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
4436 * the same extent range.
4437 */
4438 mutex_lock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004439 ret = find_first_extent_bit(unpin, 0, &start, &end,
Filipe Manana0e6ec382018-11-16 13:04:44 +00004440 EXTENT_DIRTY, &cached_state);
Lu Fengqifcd5e742018-10-24 20:24:03 +08004441 if (ret) {
4442 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004443 break;
Lu Fengqifcd5e742018-10-24 20:24:03 +08004444 }
liuboacce9522011-01-06 19:30:25 +08004445
Filipe Manana0e6ec382018-11-16 13:04:44 +00004446 clear_extent_dirty(unpin, start, end, &cached_state);
4447 free_extent_state(cached_state);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004448 btrfs_error_unpin_extent_range(fs_info, start, end);
Lu Fengqifcd5e742018-10-24 20:24:03 +08004449 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
liuboacce9522011-01-06 19:30:25 +08004450 cond_resched();
4451 }
4452
Liu Boed0eaa12012-06-14 02:23:21 -06004453 if (loop) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004454 if (unpin == &fs_info->freed_extents[0])
4455 unpin = &fs_info->freed_extents[1];
Liu Boed0eaa12012-06-14 02:23:21 -06004456 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004457 unpin = &fs_info->freed_extents[0];
Liu Boed0eaa12012-06-14 02:23:21 -06004458 loop = false;
4459 goto again;
4460 }
4461
liuboacce9522011-01-06 19:30:25 +08004462 return 0;
4463}
4464
Liu Boc79a1752016-07-20 17:44:12 -07004465static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
4466{
4467 struct inode *inode;
4468
4469 inode = cache->io_ctl.inode;
4470 if (inode) {
4471 invalidate_inode_pages2(inode->i_mapping);
4472 BTRFS_I(inode)->generation = 0;
4473 cache->io_ctl.inode = NULL;
4474 iput(inode);
4475 }
4476 btrfs_put_block_group(cache);
4477}
4478
4479void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004480 struct btrfs_fs_info *fs_info)
Liu Boc79a1752016-07-20 17:44:12 -07004481{
4482 struct btrfs_block_group_cache *cache;
4483
4484 spin_lock(&cur_trans->dirty_bgs_lock);
4485 while (!list_empty(&cur_trans->dirty_bgs)) {
4486 cache = list_first_entry(&cur_trans->dirty_bgs,
4487 struct btrfs_block_group_cache,
4488 dirty_list);
Liu Boc79a1752016-07-20 17:44:12 -07004489
4490 if (!list_empty(&cache->io_list)) {
4491 spin_unlock(&cur_trans->dirty_bgs_lock);
4492 list_del_init(&cache->io_list);
4493 btrfs_cleanup_bg_io(cache);
4494 spin_lock(&cur_trans->dirty_bgs_lock);
4495 }
4496
4497 list_del_init(&cache->dirty_list);
4498 spin_lock(&cache->lock);
4499 cache->disk_cache_state = BTRFS_DC_ERROR;
4500 spin_unlock(&cache->lock);
4501
4502 spin_unlock(&cur_trans->dirty_bgs_lock);
4503 btrfs_put_block_group(cache);
Josef Bacikba2c4d42018-12-03 10:20:33 -05004504 btrfs_delayed_refs_rsv_release(fs_info, 1);
Liu Boc79a1752016-07-20 17:44:12 -07004505 spin_lock(&cur_trans->dirty_bgs_lock);
4506 }
4507 spin_unlock(&cur_trans->dirty_bgs_lock);
4508
Nikolay Borisov45ae2c12018-02-08 18:25:18 +02004509 /*
4510 * Refer to the definition of io_bgs member for details why it's safe
4511 * to use it without any locking
4512 */
Liu Boc79a1752016-07-20 17:44:12 -07004513 while (!list_empty(&cur_trans->io_bgs)) {
4514 cache = list_first_entry(&cur_trans->io_bgs,
4515 struct btrfs_block_group_cache,
4516 io_list);
Liu Boc79a1752016-07-20 17:44:12 -07004517
4518 list_del_init(&cache->io_list);
4519 spin_lock(&cache->lock);
4520 cache->disk_cache_state = BTRFS_DC_ERROR;
4521 spin_unlock(&cache->lock);
4522 btrfs_cleanup_bg_io(cache);
4523 }
4524}
4525
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004526void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004527 struct btrfs_fs_info *fs_info)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004528{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004529 struct btrfs_device *dev, *tmp;
4530
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004531 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
Liu Boc79a1752016-07-20 17:44:12 -07004532 ASSERT(list_empty(&cur_trans->dirty_bgs));
4533 ASSERT(list_empty(&cur_trans->io_bgs));
4534
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004535 list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
4536 post_commit_list) {
4537 list_del_init(&dev->post_commit_list);
4538 }
4539
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004540 btrfs_destroy_delayed_refs(cur_trans, fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004541
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004542 cur_trans->state = TRANS_STATE_COMMIT_START;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004543 wake_up(&fs_info->transaction_blocked_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004544
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004545 cur_trans->state = TRANS_STATE_UNBLOCKED;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004546 wake_up(&fs_info->transaction_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004547
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04004548 btrfs_destroy_delayed_inodes(fs_info);
4549 btrfs_assert_delayed_root_empty(fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004550
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004551 btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004552 EXTENT_DIRTY);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004553 btrfs_destroy_pinned_extent(fs_info,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004554 fs_info->pinned_extents);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004555
Miao Xie4a9d8bd2013-05-17 03:53:43 +00004556 cur_trans->state =TRANS_STATE_COMPLETED;
4557 wake_up(&cur_trans->commit_wait);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01004558}
4559
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004560static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
liuboacce9522011-01-06 19:30:25 +08004561{
4562 struct btrfs_transaction *t;
liuboacce9522011-01-06 19:30:25 +08004563
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004564 mutex_lock(&fs_info->transaction_kthread_mutex);
liuboacce9522011-01-06 19:30:25 +08004565
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004566 spin_lock(&fs_info->trans_lock);
4567 while (!list_empty(&fs_info->trans_list)) {
4568 t = list_first_entry(&fs_info->trans_list,
Josef Bacik724e2312013-09-30 11:36:38 -04004569 struct btrfs_transaction, list);
4570 if (t->state >= TRANS_STATE_COMMIT_START) {
Elena Reshetova9b64f572017-03-03 10:55:11 +02004571 refcount_inc(&t->use_count);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004572 spin_unlock(&fs_info->trans_lock);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004573 btrfs_wait_for_commit(fs_info, t->transid);
Josef Bacik724e2312013-09-30 11:36:38 -04004574 btrfs_put_transaction(t);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004575 spin_lock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004576 continue;
4577 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004578 if (t == fs_info->running_transaction) {
Josef Bacik724e2312013-09-30 11:36:38 -04004579 t->state = TRANS_STATE_COMMIT_DOING;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004580 spin_unlock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004581 /*
4582 * We wait for 0 num_writers since we don't hold a trans
4583 * handle open currently for this transaction.
4584 */
4585 wait_event(t->writer_wait,
4586 atomic_read(&t->num_writers) == 0);
4587 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004588 spin_unlock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004589 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004590 btrfs_cleanup_one_transaction(t, fs_info);
Josef Bacika4abeea2011-04-11 17:25:13 -04004591
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004592 spin_lock(&fs_info->trans_lock);
4593 if (t == fs_info->running_transaction)
4594 fs_info->running_transaction = NULL;
liuboacce9522011-01-06 19:30:25 +08004595 list_del_init(&t->list);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004596 spin_unlock(&fs_info->trans_lock);
liuboacce9522011-01-06 19:30:25 +08004597
Josef Bacik724e2312013-09-30 11:36:38 -04004598 btrfs_put_transaction(t);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004599 trace_btrfs_transaction_commit(fs_info->tree_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004600 spin_lock(&fs_info->trans_lock);
Josef Bacik724e2312013-09-30 11:36:38 -04004601 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004602 spin_unlock(&fs_info->trans_lock);
4603 btrfs_destroy_all_ordered_extents(fs_info);
Jeff Mahoneyccdf9b32016-06-22 18:54:23 -04004604 btrfs_destroy_delayed_inodes(fs_info);
4605 btrfs_assert_delayed_root_empty(fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004606 btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004607 btrfs_destroy_all_delalloc_inodes(fs_info);
4608 mutex_unlock(&fs_info->transaction_kthread_mutex);
liuboacce9522011-01-06 19:30:25 +08004609
4610 return 0;
4611}
4612
David Sterbae8c9f182015-01-02 18:23:10 +01004613static const struct extent_io_ops btree_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +01004614 /* mandatory callbacks */
Chris Mason0b86a832008-03-24 15:01:56 -04004615 .submit_bio_hook = btree_submit_bio_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +01004616 .readpage_end_io_hook = btree_readpage_end_io_hook,
Chris Mason0da54682007-11-07 21:08:01 -05004617};